board.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "board.h"
  2. #define RUN_LED_PORT GPIOB
  3. #define RUN_LED_PIN PIN4
  4. //运行状态灯的定时器
  5. sw_timer_t run_status_timer;
  6. unsigned long run_led_speed = 500000;
  7. /**
  8. * 初始化运行灯的IO口
  9. *
  10. * @author lxz
  11. */
  12. static void hw_run_status_init(void) {
  13. //使能时钟
  14. rcu_periph_clock_enable(rcu_periph_clock_bit(RUN_LED_PORT));
  15. //配置IO为输出
  16. gpio_mode_set(RUN_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_PULLUP, RUN_LED_PIN);
  17. gpio_output_options_set(RUN_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RUN_LED_PIN);
  18. hw_run_status_on();
  19. sw_timer_start(&run_status_timer, 1, 0);
  20. }
  21. /**
  22. * 初始化Boot脚
  23. *
  24. * @author Lim (2023/3/15)
  25. *
  26. * @param void
  27. */
  28. void hw_boot_key_init(void) {
  29. #if USING_BOOT_KEY
  30. //使能时钟
  31. rcu_periph_clock_enable(rcu_periph_clock_bit(BOOT_KEY_PORT));
  32. //配置IO为输出
  33. gpio_mode_set(BOOT_KEY_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, BOOT_KEY_PIN);
  34. gpio_output_options_set(BOOT_KEY_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, BOOT_KEY_PIN);
  35. #else
  36. return;
  37. #endif
  38. }
  39. /**
  40. * 初始化并读取按键状态
  41. *
  42. * @author Lim (2023/3/15)
  43. *
  44. * @return int
  45. */
  46. int hw_boot_key_read(void) {
  47. #if USING_BOOT_KEY
  48. volatile uint32_t i = 200000;
  49. //使能时钟
  50. rcu_periph_clock_enable(rcu_periph_clock_bit(BOOT_KEY_PORT));
  51. //配置IO为输出
  52. gpio_mode_set(BOOT_KEY_PORT, GPIO_MODE_INPUT, GPIO_PUPD_PULLUP, BOOT_KEY_PIN);
  53. gpio_output_options_set(BOOT_KEY_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, BOOT_KEY_PIN);
  54. while (i-- > 0);
  55. return ((GPIO_ISTAT(BOOT_KEY_PORT) & BOOT_KEY_PIN) == BOOT_KEY_LEVEL);
  56. #else
  57. return 0;
  58. #endif
  59. }
  60. /**
  61. * 控制板初始化
  62. *
  63. * @author lxz
  64. *
  65. * @param void
  66. */
  67. void hw_board_init(void) {
  68. //Sys_Clock_Init(HW_BOART_CORE_FREQ, HW_BOART_MAIN_FREQ, HW_BOART_CORE_FREQ);
  69. Sys_NVIC_Set_VectorTable(0x08000000, 0x0);
  70. //hw_delay_init(HW_BOART_CORE_FREQ);
  71. sw_timer_init(HW_BOART_CORE_FREQ);
  72. hw_spi_init();
  73. //hw_spi_flash_attach(0,0);
  74. //hw_power_off_init();
  75. hw_io_pin_init();
  76. hw_run_status_init();
  77. hw_dma_uart_init(HW_BOART_CORE_FREQ);
  78. //hw_pwm_init(HW_BOART_CORE_FREQ);
  79. //hw_encoder_init();
  80. INTX_ENABLE();
  81. }
  82. /**
  83. * 进入节点模式,立刻切断所有的输出,全部为输入模式
  84. *
  85. * @author lxz (2019/5/30/鍛ㄥ洓)
  86. */
  87. void hw_board_enter_powerless(void) {
  88. hw_run_status_init();
  89. }
  90. /**
  91. * 强制灭灯
  92. *
  93. * @author LXZ (121219)
  94. *
  95. * @param void
  96. */
  97. void hw_run_status_off(void) {
  98. GPIO_BOP(RUN_LED_PORT) = RUN_LED_PIN;
  99. }
  100. /**
  101. * 强制亮灯
  102. *
  103. * @author LXZ (121219)
  104. *
  105. * @param void
  106. */
  107. void hw_run_status_on(void) {
  108. GPIO_BOP(RUN_LED_PORT) = (RUN_LED_PIN) << 16;
  109. }
  110. /**
  111. * 显示用于运行等指示的状态
  112. *
  113. * @author lxz
  114. *
  115. * @param void
  116. */
  117. void hw_run_status_show(void)
  118. {
  119. if (sw_timer_expire(&run_status_timer))
  120. {
  121. GPIO_TG(RUN_LED_PORT) = RUN_LED_PIN;
  122. sw_timer_start(&run_status_timer, 0, run_led_speed);
  123. }
  124. }
  125. /**
  126. * 初始化看门狗,看门狗需要注册中断函数
  127. *
  128. * @author LXZ (121219)
  129. *
  130. * @param void
  131. */
  132. void wathcdog_init(void (*func)()) {
  133. }
  134. /**
  135. * 喂狗
  136. *
  137. * @author LXZ (121219)
  138. *
  139. * @param void
  140. */
  141. void watchdog_kick(void) {
  142. }
  143. //临时保存数据参数,由于是放在了非初始化,允许保存app参数给Bootloader调用
  144. static int hw_noinit_data[16]__attribute__((section(".noinit")));
  145. /**
  146. * 写入保存参数
  147. *
  148. * @author lxz
  149. *
  150. * @param no
  151. * @param value
  152. */
  153. void hw_noinit_write(int no, int value) {
  154. hw_noinit_data[no] = value;
  155. }
  156. /**
  157. * 读取临时保存参数
  158. *
  159. * @author lxz
  160. *
  161. * @param no
  162. *
  163. * @return int
  164. */
  165. int hw_noinit_read(int no) {
  166. return hw_noinit_data[no];
  167. }
  168. /**
  169. * 硬件复位
  170. *
  171. * @author LXZ (051820)
  172. *
  173. * @param void
  174. */
  175. void hw_board_reboot(void) {
  176. Sys_Soft_Reset();
  177. }