board.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. #include "board.h"
  2. #define USING_BOOT_KEY 1
  3. //#define RUN_LED_PORT GPIOB
  4. //#define RUN_LED_PIN PIN10
  5. #define RUN_LED_PORT GPIOD
  6. #define RUN_LED_PIN PIN10
  7. #define BOOT_KEY_PORT GPIOC
  8. #define BOOT_KEY_PIN PIN0
  9. //运行状态灯的定时器
  10. static sw_timer_t run_status_timer;
  11. unsigned long run_led_speed = 500000;
  12. /**
  13. * 强制灭灯
  14. *
  15. * @author LXZ (121219)
  16. *
  17. * @param void
  18. */
  19. void hw_run_status_off(void) {
  20. RUN_LED_PORT->BSRR = RUN_LED_PIN;
  21. }
  22. /**
  23. * 强制亮灯
  24. *
  25. * @author LXZ (121219)
  26. *
  27. * @param void
  28. */
  29. void hw_run_status_on(void) {
  30. RUN_LED_PORT->BRR = RUN_LED_PIN;
  31. }
  32. /**
  33. * 初始化运行灯的IO口
  34. *
  35. * @author lxz
  36. */
  37. static void hw_run_status_init(void)
  38. {
  39. uint32_t port = (uint32_t)RUN_LED_PORT;
  40. unsigned short pin = RUN_LED_PIN;
  41. if (port >= GPIOA_BASE &&
  42. port <= GPIOG_BASE)
  43. {
  44. RCC->APB2ENR |= 0x04 << ((port - GPIOA_BASE) / 0x400);
  45. GPIO_Set((GPIO_TypeDef *)port, pin,
  46. GPIO_MODE_OUT_PP,
  47. GPIO_SPEED_50M,
  48. GPIO_PUPD_PU);
  49. ((GPIO_TypeDef *)port)->BRR = pin;
  50. }
  51. sw_timer_start(&run_status_timer, 1, 0);
  52. }
  53. /**
  54. * 控制板初始化
  55. *
  56. * @author lxz
  57. *
  58. * @param void
  59. */
  60. void hw_board_init(void)
  61. {
  62. Stm32_Clock_Init(10); //120Mhz
  63. //配置向量表
  64. Sys_NVIC_SetVectorTable(ST_FLASH_SECTOR_5,0x00000200);
  65. //Sys_NVIC_SetVectorTable(ST_FLASH_SECTOR_0,0x00000000);
  66. st_dma_init();
  67. hw_delay_init(120);
  68. sw_timer_init(120);
  69. //开启IO复用功能
  70. RCC->APB2ENR |= 1<< 0;
  71. hw_io_pin_init();
  72. hw_run_status_init();
  73. hw_dma_uart_init(120);
  74. hw_power_off_init();
  75. // hw_iic_init();
  76. INTX_ENABLE();
  77. }
  78. void hw_boot_key_init(void)
  79. {
  80. #if USING_BOOT_KEY
  81. uint32_t port = (uint32_t)BOOT_KEY_PORT;
  82. unsigned short pin = BOOT_KEY_PIN;
  83. if (port >= GPIOA_BASE &&
  84. port <= GPIOG_BASE)
  85. {
  86. RCC->APB2ENR |= 0x04 << ((port - GPIOA_BASE) / 0x400);
  87. GPIO_Set((GPIO_TypeDef *)port, pin,
  88. GPIO_MODE_IPU,
  89. GPIO_SPEED_50M,
  90. GPIO_PUPD_PU);
  91. ((GPIO_TypeDef *)port)->BRR = pin;
  92. }
  93. #else
  94. return ;
  95. #endif
  96. }
  97. int hw_boot_key_read(void)
  98. {
  99. #if USING_BOOT_KEY
  100. uint32_t port = (uint32_t)BOOT_KEY_PORT;
  101. unsigned short pin = BOOT_KEY_PIN;
  102. volatile uint32_t i = 200000;
  103. if (port >= GPIOA_BASE &&
  104. port <= GPIOG_BASE)
  105. {
  106. RCC->APB2ENR |= 0x04 << ((port - GPIOA_BASE) / 0x400);
  107. GPIO_Set((GPIO_TypeDef *)port, pin,
  108. GPIO_MODE_IPU,
  109. GPIO_SPEED_50M,
  110. GPIO_PUPD_PU);
  111. ((GPIO_TypeDef *)port)->BRR = pin;
  112. }
  113. while (i-- > 0);
  114. return (BOOT_KEY_PORT->IDR & BOOT_KEY_PIN) == 0;
  115. #else
  116. return 0;
  117. #endif
  118. }
  119. /**
  120. * 显示用于运行等指示的状态
  121. *
  122. * @author lxz
  123. *
  124. * @param void
  125. */
  126. void hw_run_status_show(void)
  127. {
  128. if (sw_timer_expire(&run_status_timer))
  129. {
  130. if (RUN_LED_PORT->ODR & RUN_LED_PIN)
  131. {
  132. RUN_LED_PORT->BRR = RUN_LED_PIN;
  133. } else
  134. {
  135. RUN_LED_PORT->BSRR = RUN_LED_PIN;
  136. }
  137. sw_timer_start(&run_status_timer, 0, run_led_speed);
  138. }
  139. }
  140. //临时保存数据参数,由于是放在了非初始化,允许保存app参数给Bootloader调用
  141. static int hw_noinit_data[16] __attribute__((section(".noinit")));
  142. /**
  143. * 写入保存参数
  144. *
  145. * @author lxz
  146. *
  147. * @param no
  148. * @param value
  149. */
  150. void hw_noinit_write(int no, int value)
  151. {
  152. hw_noinit_data[no] = value;
  153. }
  154. /**
  155. * 读取临时保存参数
  156. *
  157. * @author lxz
  158. *
  159. * @param no
  160. *
  161. * @return int
  162. */
  163. int hw_noinit_read(int no)
  164. {
  165. return hw_noinit_data[no];
  166. }
  167. void hw_board_enter_powerless(void)
  168. {
  169. int index = 0;
  170. //立刻所有IO高阻态
  171. while (index < 5)
  172. {
  173. GPIO_TypeDef *gpio = (GPIO_TypeDef *)(GPIOA_BASE + index * 0x400);
  174. gpio->CRL = 0x44444444;
  175. gpio->CRH = 0x44444444;
  176. index++;
  177. }
  178. hw_run_status_init();
  179. }
  180. /**
  181. * 硬件复位
  182. *
  183. * @author LXZ (051820)
  184. *
  185. * @param void
  186. */
  187. void hw_board_reboot(void)
  188. {
  189. Sys_Soft_Reset();
  190. }