main.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright (c) 2015 - 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. * @defgroup blinky_example_main main.c
  42. * @{
  43. * @ingroup blinky_example_freertos
  44. *
  45. * @brief Blinky FreeRTOS Example Application main file.
  46. *
  47. * This file contains the source code for a sample application using FreeRTOS to blink LEDs.
  48. *
  49. */
  50. #include <stdbool.h>
  51. #include <stdint.h>
  52. #include "FreeRTOS.h"
  53. #include "task.h"
  54. #include "timers.h"
  55. #include "bsp.h"
  56. #include "nordic_common.h"
  57. #include "nrf_drv_clock.h"
  58. #include "sdk_errors.h"
  59. #include "app_error.h"
  60. #if LEDS_NUMBER <= 2
  61. #error "Board is not equipped with enough amount of LEDs"
  62. #endif
  63. #define TASK_DELAY 200 /**< Task delay. Delays a LED0 task for 200 ms */
  64. #define TIMER_PERIOD 1000 /**< Timer period. LED1 timer will expire after 1000 ms */
  65. TaskHandle_t led_toggle_task_handle; /**< Reference to LED0 toggling FreeRTOS task. */
  66. TimerHandle_t led_toggle_timer_handle; /**< Reference to LED1 toggling FreeRTOS timer. */
  67. /**@brief LED0 task entry function.
  68. *
  69. * @param[in] pvParameter Pointer that will be used as the parameter for the task.
  70. */
  71. static void led_toggle_task_function (void * pvParameter)
  72. {
  73. UNUSED_PARAMETER(pvParameter);
  74. while (true)
  75. {
  76. bsp_board_led_invert(BSP_BOARD_LED_0);
  77. /* Delay a task for a given number of ticks */
  78. vTaskDelay(TASK_DELAY);
  79. /* Tasks must be implemented to never return... */
  80. }
  81. }
  82. /**@brief The function to call when the LED1 FreeRTOS timer expires.
  83. *
  84. * @param[in] pvParameter Pointer that will be used as the parameter for the timer.
  85. */
  86. static void led_toggle_timer_callback (void * pvParameter)
  87. {
  88. UNUSED_PARAMETER(pvParameter);
  89. bsp_board_led_invert(BSP_BOARD_LED_1);
  90. }
  91. int main(void)
  92. {
  93. ret_code_t err_code;
  94. /* Initialize clock driver for better time accuracy in FREERTOS */
  95. err_code = nrf_drv_clock_init();
  96. APP_ERROR_CHECK(err_code);
  97. /* Configure LED-pins as outputs */
  98. bsp_board_init(BSP_INIT_LEDS);
  99. /* Create task for LED0 blinking with priority set to 2 */
  100. UNUSED_VARIABLE(xTaskCreate(led_toggle_task_function, "LED0", configMINIMAL_STACK_SIZE + 200, NULL, 2, &led_toggle_task_handle));
  101. /* Start timer for LED1 blinking */
  102. led_toggle_timer_handle = xTimerCreate( "LED1", TIMER_PERIOD, pdTRUE, NULL, led_toggle_timer_callback);
  103. UNUSED_VARIABLE(xTimerStart(led_toggle_timer_handle, 0));
  104. /* Activate deep sleep mode */
  105. SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
  106. /* Start FreeRTOS scheduler. */
  107. vTaskStartScheduler();
  108. while (true)
  109. {
  110. /* FreeRTOS should not be here... FreeRTOS goes back to the start of stack
  111. * in vTaskStartScheduler function. */
  112. }
  113. }
  114. /**
  115. *@}
  116. **/