main.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * Copyright (c) 2014 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. /** @file
  41. *
  42. * @defgroup gpiote_example_main main.c
  43. * @{
  44. * @ingroup nrf_gpiote_example
  45. * @brief GPIOTE Example Application main file.
  46. *
  47. * This file contains the source code for a sample application using GPIOTE.
  48. */
  49. #include <stdbool.h>
  50. #include <stdint.h>
  51. #include "nrf.h"
  52. #include "nrf_gpiote.h"
  53. #include "nrf_gpio.h"
  54. #include "boards.h"
  55. #include "nrf_drv_ppi.h"
  56. #include "nrf_drv_timer.h"
  57. #include "nrf_drv_gpiote.h"
  58. #include "app_error.h"
  59. #ifdef BSP_LED_0
  60. #define GPIO_OUTPUT_PIN_NUMBER BSP_LED_0 /**< Pin number for output. */
  61. #endif
  62. #ifndef GPIO_OUTPUT_PIN_NUMBER
  63. #error "Please indicate output pin"
  64. #endif
  65. static nrf_drv_timer_t timer = NRF_DRV_TIMER_INSTANCE(0);
  66. void timer_dummy_handler(nrf_timer_event_t event_type, void * p_context){}
  67. static void led_blinking_setup()
  68. {
  69. uint32_t compare_evt_addr;
  70. uint32_t gpiote_task_addr;
  71. nrf_ppi_channel_t ppi_channel;
  72. ret_code_t err_code;
  73. nrf_drv_gpiote_out_config_t config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false);
  74. err_code = nrf_drv_gpiote_out_init(GPIO_OUTPUT_PIN_NUMBER, &config);
  75. APP_ERROR_CHECK(err_code);
  76. nrf_drv_timer_extended_compare(&timer, (nrf_timer_cc_channel_t)0, 200 * 1000UL, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, false);
  77. err_code = nrf_drv_ppi_channel_alloc(&ppi_channel);
  78. APP_ERROR_CHECK(err_code);
  79. compare_evt_addr = nrf_drv_timer_event_address_get(&timer, NRF_TIMER_EVENT_COMPARE0);
  80. gpiote_task_addr = nrf_drv_gpiote_out_task_addr_get(GPIO_OUTPUT_PIN_NUMBER);
  81. err_code = nrf_drv_ppi_channel_assign(ppi_channel, compare_evt_addr, gpiote_task_addr);
  82. APP_ERROR_CHECK(err_code);
  83. err_code = nrf_drv_ppi_channel_enable(ppi_channel);
  84. APP_ERROR_CHECK(err_code);
  85. nrf_drv_gpiote_out_task_enable(GPIO_OUTPUT_PIN_NUMBER);
  86. }
  87. /**
  88. * @brief Function for application main entry.
  89. */
  90. int main(void)
  91. {
  92. ret_code_t err_code;
  93. err_code = nrf_drv_ppi_init();
  94. APP_ERROR_CHECK(err_code);
  95. err_code = nrf_drv_gpiote_init();
  96. APP_ERROR_CHECK(err_code);
  97. nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
  98. err_code = nrf_drv_timer_init(&timer, &timer_cfg, timer_dummy_handler);
  99. APP_ERROR_CHECK(err_code);
  100. #ifdef NRF51
  101. //Workaround for PAN-73.
  102. *(uint32_t *)0x40008C0C = 1;
  103. #endif
  104. // Setup PPI channel with event from TIMER compare and task GPIOTE pin toggle.
  105. led_blinking_setup();
  106. // Enable timer
  107. nrf_drv_timer_enable(&timer);
  108. while (true)
  109. {
  110. // Do Nothing - GPIO can be toggled without software intervention.
  111. }
  112. }
  113. /** @} */