iot_boardled.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2021 Huawei Device Co., Ltd.
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include "iot_demo_def.h"
  16. #include "iot_boardled.h"
  17. #include "iot_gpio.h"
  18. #include "iot_gpio_ex.h"
  19. #include "iot_pwm.h"
  20. #include "ohos_init.h"
  21. #include <cmsis_os2.h>
  22. #define DEVICE_STATUS_LED_GPIO 2
  23. #define DEVICE_SATTUS_LED_PWD 2
  24. #define CONFIG_FLASHLED_FREDEFAULT 5
  25. /**
  26. * @brief Initialize the board pwd led
  27. * @return Returns 0 success while others failed
  28. */
  29. int BOARD_InitPwmLed(void)
  30. {
  31. IoTGpioInit(DEVICE_STATUS_LED_GPIO);
  32. IoTGpioSetFunc(DEVICE_STATUS_LED_GPIO, IOT_GPIO_FUNC_GPIO_2_PWM2_OUT);
  33. IoTGpioSetDir(DEVICE_STATUS_LED_GPIO, IOT_GPIO_DIR_OUT);
  34. IoTPwmInit(DEVICE_SATTUS_LED_PWD);
  35. return 0;
  36. }
  37. /**
  38. * @brief Set Pwm led duty cycle
  39. * @param dutyCycle, the duty cycle to set, not max than 40000
  40. * @return Returns 0 success while others failed
  41. */
  42. int BOARD_SetPwmLedDutyCycle(int dutyCycle)
  43. {
  44. dutyCycle = dutyCycle < 0 ? 1 : dutyCycle;
  45. dutyCycle = dutyCycle >= 100 ? 99 : dutyCycle;
  46. IoTPwmStart(DEVICE_STATUS_LED_GPIO, dutyCycle, 40000);
  47. return 0;
  48. }
  49. // the led part
  50. int BOARD_InitIoLed(void)
  51. {
  52. //设置GPIO_2的复用功能为普通GPIO
  53. IoTGpioSetFunc(DEVICE_STATUS_LED_GPIO, IOT_GPIO_FUNC_GPIO_2_GPIO);
  54. //设置GPIO_2为输出模式
  55. IoTGpioSetDir(DEVICE_STATUS_LED_GPIO, IOT_GPIO_DIR_OUT);
  56. return 0;
  57. }
  58. int BOARD_SetIoLedStatus(int status)
  59. {
  60. if (status) {
  61. IoTGpioSetOutputVal(DEVICE_STATUS_LED_GPIO, 1);
  62. } else {
  63. IoTGpioSetOutputVal(DEVICE_STATUS_LED_GPIO, 0);
  64. }
  65. return 0;
  66. }
  67. #define CONFIG_TASK_MAIN_STACKSIZE 0x800 // main task stacksize must be bigger
  68. #define CONFIG_TASK_MAIN_PRIOR 22 // default task priority
  69. /**
  70. * @brief Convert miniseconds to system ticks
  71. * @param ms Indicates the mimiseconds to convert
  72. * @return Returns the corresponding ticks of specified time
  73. */
  74. static uint32_t Time2Tick(uint32_t ms)
  75. {
  76. uint64_t ret;
  77. ret = ((uint64_t)ms * osKernelGetTickFreq()) / CN_MINISECONDS_IN_SECOND;
  78. return (uint32_t)ret;
  79. }
  80. /**
  81. @ brief Set the main task flash frequency
  82. */
  83. typedef struct
  84. {
  85. int cycleTicks;
  86. int cycleLightTicks;
  87. osThreadId_t taskID;
  88. }LedFlashController;
  89. static LedFlashController g_ledFlashController;
  90. #define CN_LED_FLASH_HIGHBASE 200
  91. #define CN_LED_FLASH_CYCLEBASE 1000
  92. int LedFlashFrequencySet(float flashHz)
  93. {
  94. int cycleHighMs = 0;
  95. int cycleMs = 0;
  96. cycleMs = (int)(CN_MINISECONDS_IN_SECOND / flashHz);
  97. cycleHighMs = cycleMs *CN_LED_FLASH_HIGHBASE /CN_LED_FLASH_CYCLEBASE;
  98. g_ledFlashController.cycleTicks = Time2Tick(cycleMs);
  99. g_ledFlashController.cycleLightTicks = Time2Tick(cycleHighMs);
  100. RaiseLog(LOG_LEVEL_INFO, "cycle:%d cycleon:%d ",g_ledFlashController.cycleTicks, g_ledFlashController.cycleLightTicks);
  101. return 0;
  102. }
  103. int LedFlashTaskDeinit(void)
  104. {
  105. osThreadTerminate(g_ledFlashController.taskID);
  106. g_ledFlashController.taskID = NULL;
  107. return 0;
  108. }
  109. /**
  110. * @brief LED flashing task entry
  111. */
  112. static void LedTaskEntry(const void *arg)
  113. {
  114. (void)arg;
  115. BOARD_InitIoLed();
  116. LedFlashFrequencySet(CONFIG_FLASHLED_FREDEFAULT);
  117. while (1) {
  118. BOARD_SetIoLedStatus(CN_BOARD_SWITCH_ON);
  119. osDelay((uint32_t)g_ledFlashController.cycleLightTicks);
  120. BOARD_SetIoLedStatus(CN_BOARD_SWITCH_OFF);
  121. osDelay((uint32_t)(g_ledFlashController.cycleTicks - g_ledFlashController.cycleLightTicks));
  122. }
  123. return;
  124. }
  125. static void LedFlashTaskInit(void)
  126. {
  127. osThreadAttr_t attr;
  128. RaiseLog(LOG_LEVEL_INFO, "DATA:%s Time:%s \r\n", __DATE__, __TIME__);
  129. // Create the IoT Main task
  130. attr.attr_bits = 0U;
  131. attr.cb_mem = NULL;
  132. attr.cb_size = 0U;
  133. attr.stack_mem = NULL;
  134. attr.stack_size = CONFIG_TASK_MAIN_STACKSIZE;
  135. attr.priority = CONFIG_TASK_MAIN_PRIOR;
  136. attr.name = "LedFlashTask";
  137. g_ledFlashController.taskID = osThreadNew((osThreadFunc_t)LedTaskEntry, NULL, (const osThreadAttr_t *)&attr);
  138. if (g_ledFlashController.taskID == NULL) {
  139. RaiseLog(LOG_LEVEL_ERR, "Create the LED FLASH TASK failed");
  140. } else {
  141. RaiseLog(LOG_LEVEL_INFO, "Create the LED FLASH TASK success");
  142. }
  143. return;
  144. }
  145. APP_FEATURE_INIT(LedFlashTaskInit);