board.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 GPIOE
  6. #define RUN_LED_PIN PIN6
  7. #define BOOT_KEY_PORT GPIOC
  8. #define BOOT_KEY_PIN PIN14
  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(9); //72Mhz
  63. //配置向量表
  64. Sys_NVIC_SetVectorTable(ST_FLASH_SECTOR_5,0x00000200);
  65. st_dma_init();
  66. hw_delay_init(72);
  67. sw_timer_init(72);
  68. //开启IO复用功能
  69. RCC->APB2ENR |= 1<< 0;
  70. hw_io_pin_init();
  71. hw_run_status_init();
  72. //hw_dma_uart_init(72);
  73. // hw_iic_init();
  74. INTX_ENABLE();
  75. }
  76. void hw_boot_key_init(void)
  77. {
  78. #if USING_BOOT_KEY
  79. uint32_t port = (uint32_t)BOOT_KEY_PORT;
  80. unsigned short pin = BOOT_KEY_PIN;
  81. if (port >= GPIOA_BASE &&
  82. port <= GPIOG_BASE)
  83. {
  84. RCC->APB2ENR |= 0x04 << ((port - GPIOA_BASE) / 0x400);
  85. GPIO_Set((GPIO_TypeDef *)port, pin,
  86. GPIO_MODE_IPU,
  87. GPIO_SPEED_50M,
  88. GPIO_PUPD_PU);
  89. ((GPIO_TypeDef *)port)->BRR = pin;
  90. }
  91. #else
  92. return ;
  93. #endif
  94. }
  95. int hw_boot_key_read(void)
  96. {
  97. #if USING_BOOT_KEY
  98. uint32_t port = (uint32_t)BOOT_KEY_PORT;
  99. unsigned short pin = BOOT_KEY_PIN;
  100. volatile uint32_t i = 200000;
  101. if (port >= GPIOA_BASE &&
  102. port <= GPIOG_BASE)
  103. {
  104. RCC->APB2ENR |= 0x04 << ((port - GPIOA_BASE) / 0x400);
  105. GPIO_Set((GPIO_TypeDef *)port, pin,
  106. GPIO_MODE_IPU,
  107. GPIO_SPEED_50M,
  108. GPIO_PUPD_PU);
  109. ((GPIO_TypeDef *)port)->BRR = pin;
  110. }
  111. while (i-- > 0);
  112. return (BOOT_KEY_PORT->IDR & BOOT_KEY_PIN) == 0;
  113. #else
  114. return 0;
  115. #endif
  116. }
  117. /**
  118. * 显示用于运行等指示的状态
  119. *
  120. * @author lxz
  121. *
  122. * @param void
  123. */
  124. void hw_run_status_show(void)
  125. {
  126. if (sw_timer_expire(&run_status_timer))
  127. {
  128. if (RUN_LED_PORT->ODR & RUN_LED_PIN)
  129. {
  130. RUN_LED_PORT->BRR = RUN_LED_PIN;
  131. } else
  132. {
  133. RUN_LED_PORT->BSRR = RUN_LED_PIN;
  134. }
  135. sw_timer_start(&run_status_timer, 0, run_led_speed);
  136. }
  137. }
  138. //临时保存数据参数,由于是放在了非初始化,允许保存app参数给Bootloader调用
  139. static int hw_noinit_data[16] __attribute__((section(".noinit")));
  140. /**
  141. * 写入保存参数
  142. *
  143. * @author lxz
  144. *
  145. * @param no
  146. * @param value
  147. */
  148. void hw_noinit_write(int no, int value)
  149. {
  150. hw_noinit_data[no] = value;
  151. }
  152. /**
  153. * 读取临时保存参数
  154. *
  155. * @author lxz
  156. *
  157. * @param no
  158. *
  159. * @return int
  160. */
  161. int hw_noinit_read(int no)
  162. {
  163. return hw_noinit_data[no];
  164. }
  165. void hw_board_enter_powerless(void)
  166. {
  167. int index = 0;
  168. //立刻所有IO高阻态
  169. while (index < 5)
  170. {
  171. GPIO_TypeDef *gpio = (GPIO_TypeDef *)(GPIOA_BASE + index * 0x400);
  172. gpio->CRL = 0x44444444;
  173. gpio->CRH = 0x44444444;
  174. index++;
  175. }
  176. hw_run_status_init();
  177. }
  178. /**
  179. * 硬件复位
  180. *
  181. * @author LXZ (051820)
  182. *
  183. * @param void
  184. */
  185. void hw_board_reboot(void)
  186. {
  187. Sys_Soft_Reset();
  188. }