at32f403a_407_board.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /**
  2. **************************************************************************
  3. * @file at32f403a_407_board.c
  4. * @brief set of firmware functions to manage leds and push-button.
  5. * initialize delay function.
  6. **************************************************************************
  7. * Copyright notice & Disclaimer
  8. *
  9. * The software Board Support Package (BSP) that is made available to
  10. * download from Artery official website is the copyrighted work of Artery.
  11. * Artery authorizes customers to use, copy, and distribute the BSP
  12. * software and its related documentation for the purpose of design and
  13. * development in conjunction with Artery microcontrollers. Use of the
  14. * software is governed by this copyright notice and the following disclaimer.
  15. *
  16. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  17. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  18. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  19. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  20. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  22. *
  23. **************************************************************************
  24. */
  25. #include "at32f403a_407_board.h"
  26. /** @addtogroup AT32F403A_407_board
  27. * @{
  28. */
  29. /** @defgroup BOARD
  30. * @brief onboard periph driver
  31. * @{
  32. */
  33. /* delay macros */
  34. #define STEP_DELAY_MS 50
  35. /* at-start led resouce array */
  36. gpio_type *led_gpio_port[LED_NUM] = {LED2_GPIO, LED3_GPIO, LED4_GPIO};
  37. uint16_t led_gpio_pin[LED_NUM] = {LED2_PIN, LED3_PIN, LED4_PIN};
  38. crm_periph_clock_type led_gpio_crm_clk[LED_NUM] = {LED2_GPIO_CRM_CLK, LED3_GPIO_CRM_CLK, LED4_GPIO_CRM_CLK};
  39. /* delay variable */
  40. static __IO uint32_t fac_us;
  41. static __IO uint32_t fac_ms;
  42. /* support printf function, usemicrolib is unnecessary */
  43. #if (__ARMCC_VERSION > 6000000)
  44. __asm (".global __use_no_semihosting\n\t");
  45. void _sys_exit(int x)
  46. {
  47. x = x;
  48. }
  49. /* __use_no_semihosting was requested, but _ttywrch was */
  50. void _ttywrch(int ch)
  51. {
  52. ch = ch;
  53. }
  54. FILE __stdout;
  55. #else
  56. #ifdef __CC_ARM
  57. #pragma import(__use_no_semihosting)
  58. struct __FILE
  59. {
  60. int handle;
  61. };
  62. FILE __stdout;
  63. void _sys_exit(int x)
  64. {
  65. x = x;
  66. }
  67. /* __use_no_semihosting was requested, but _ttywrch was */
  68. void _ttywrch(int ch)
  69. {
  70. ch = ch;
  71. }
  72. #endif
  73. #endif
  74. #if 0
  75. #if defined (__GNUC__) && !defined (__clang__)
  76. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  77. #else
  78. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  79. #endif
  80. /**
  81. * @brief retargets the c library printf function to the usart.
  82. * @param none
  83. * @retval none
  84. */
  85. PUTCHAR_PROTOTYPE
  86. {
  87. while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
  88. usart_data_transmit(PRINT_UART, (uint16_t)ch);
  89. while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
  90. return ch;
  91. }
  92. #endif
  93. #if (defined (__GNUC__) && !defined (__clang__)) || (defined (__ICCARM__))
  94. #if defined (__GNUC__) && !defined (__clang__)
  95. int _write(int fd, char *pbuffer, int size)
  96. #elif defined ( __ICCARM__ )
  97. #pragma module_name = "?__write"
  98. int __write(int fd, char *pbuffer, int size)
  99. #endif
  100. {
  101. for(int i = 0; i < size; i ++)
  102. {
  103. while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
  104. usart_data_transmit(PRINT_UART, (uint16_t)(*pbuffer++));
  105. while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
  106. }
  107. return size;
  108. }
  109. #endif
  110. /**
  111. * @brief initialize uart
  112. * @param baudrate: uart baudrate
  113. * @retval none
  114. */
  115. void uart_print_init(uint32_t baudrate)
  116. {
  117. gpio_init_type gpio_init_struct;
  118. #if defined (__GNUC__) && !defined (__clang__)
  119. setvbuf(stdout, NULL, _IONBF, 0);
  120. #endif
  121. /* enable the uart and gpio clock */
  122. crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE);
  123. crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE);
  124. /*
  125. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  126. gpio_pin_remap_config(USART3_GMUX_0011, TRUE);
  127. */
  128. gpio_default_para_init(&gpio_init_struct);
  129. /* configure the uart tx pin */
  130. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  131. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  132. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  133. gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN;
  134. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  135. gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);
  136. /* configure uart param */
  137. usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
  138. usart_transmitter_enable(PRINT_UART, TRUE);
  139. usart_receiver_enable(PRINT_UART, TRUE);
  140. usart_enable(PRINT_UART, TRUE);
  141. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  142. NVIC_ClearPendingIRQ(USART1_IRQn);
  143. nvic_irq_enable(USART1_IRQn, 0, 5);
  144. usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
  145. }
  146. #if 0
  147. void USART1_IRQHandler(void)
  148. {
  149. static char data = 0;
  150. if (USART1->ctrl1_bit.rdbfien == SET)
  151. {
  152. if (usart_flag_get(USART1, USART_RDBF_FLAG) == SET)
  153. {
  154. data = usart_data_receive(USART1);
  155. }
  156. }
  157. }
  158. #endif
  159. /**
  160. * @brief board initialize interface init led and button
  161. * @param none
  162. * @retval none
  163. */
  164. void at32_board_init()
  165. {
  166. /* initialize delay function */
  167. delay_init();
  168. /* configure led in at_start_board */
  169. at32_led_init(LED2);
  170. at32_led_init(LED3);
  171. at32_led_init(LED4);
  172. at32_led_off(LED2);
  173. at32_led_off(LED3);
  174. at32_led_off(LED4);
  175. /* configure button in at_start board */
  176. at32_button_init();
  177. }
  178. /**
  179. * @brief configure button gpio
  180. * @param button: specifies the button to be configured.
  181. * @retval none
  182. */
  183. void at32_button_init(void)
  184. {
  185. gpio_init_type gpio_init_struct;
  186. /* enable the button clock */
  187. crm_periph_clock_enable(USER_BUTTON_CRM_CLK, TRUE);
  188. /* set default parameter */
  189. gpio_default_para_init(&gpio_init_struct);
  190. /* configure button pin as input with pull-up/pull-down */
  191. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  192. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  193. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  194. gpio_init_struct.gpio_pins = USER_BUTTON_PIN;
  195. gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
  196. gpio_init(USER_BUTTON_PORT, &gpio_init_struct);
  197. }
  198. /**
  199. * @brief returns the selected button state
  200. * @param none
  201. * @retval the button gpio pin value
  202. */
  203. uint8_t at32_button_state(void)
  204. {
  205. return gpio_input_data_bit_read(USER_BUTTON_PORT, USER_BUTTON_PIN);
  206. }
  207. /**
  208. * @brief returns which button have press down
  209. * @param none
  210. * @retval the button have press down
  211. */
  212. button_type at32_button_press()
  213. {
  214. static uint8_t pressed = 1;
  215. /* get button state in at_start board */
  216. if((pressed == 1) && (at32_button_state() != RESET))
  217. {
  218. /* debounce */
  219. pressed = 0;
  220. delay_ms(10);
  221. if(at32_button_state() != RESET)
  222. return USER_BUTTON;
  223. }
  224. else if(at32_button_state() == RESET)
  225. {
  226. pressed = 1;
  227. }
  228. return NO_BUTTON;
  229. }
  230. /**
  231. * @brief configure led gpio
  232. * @param led: specifies the led to be configured.
  233. * @retval none
  234. */
  235. void at32_led_init(led_type led)
  236. {
  237. gpio_init_type gpio_init_struct;
  238. /* enable the led clock */
  239. crm_periph_clock_enable(led_gpio_crm_clk[led], TRUE);
  240. /* set default parameter */
  241. gpio_default_para_init(&gpio_init_struct);
  242. /* configure the led gpio */
  243. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  244. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  245. gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  246. gpio_init_struct.gpio_pins = led_gpio_pin[led];
  247. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  248. gpio_init(led_gpio_port[led], &gpio_init_struct);
  249. }
  250. /**
  251. * @brief turns selected led on.
  252. * @param led: specifies the led to be set on.
  253. * this parameter can be one of following parameters:
  254. * @arg LED2
  255. * @arg LED3
  256. * @arg LED4
  257. * @retval none
  258. */
  259. void at32_led_on(led_type led)
  260. {
  261. if(led > (LED_NUM - 1))
  262. return;
  263. if(led_gpio_pin[led])
  264. led_gpio_port[led]->clr = led_gpio_pin[led];
  265. }
  266. /**
  267. * @brief turns selected led off.
  268. * @param led: specifies the led to be set off.
  269. * this parameter can be one of following parameters:
  270. * @arg LED2
  271. * @arg LED3
  272. * @arg LED4
  273. * @retval none
  274. */
  275. void at32_led_off(led_type led)
  276. {
  277. if(led > (LED_NUM - 1))
  278. return;
  279. if(led_gpio_pin[led])
  280. led_gpio_port[led]->scr = led_gpio_pin[led];
  281. }
  282. /**
  283. * @brief turns selected led toggle.
  284. * @param led: specifies the led to be set off.
  285. * this parameter can be one of following parameters:
  286. * @arg LED2
  287. * @arg LED3
  288. * @arg LED4
  289. * @retval none
  290. */
  291. void at32_led_toggle(led_type led)
  292. {
  293. if(led > (LED_NUM - 1))
  294. return;
  295. if(led_gpio_pin[led])
  296. led_gpio_port[led]->odt ^= led_gpio_pin[led];
  297. }
  298. /**
  299. * @brief initialize delay function
  300. * @param none
  301. * @retval none
  302. */
  303. void delay_init()
  304. {
  305. /* configure systick */
  306. systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  307. fac_us = system_core_clock / (1000000U);
  308. fac_ms = fac_us * (1000U);
  309. }
  310. /**
  311. * @brief inserts a delay time.
  312. * @param nus: specifies the delay time length, in microsecond.
  313. * @retval none
  314. */
  315. void delay_us(uint32_t nus)
  316. {
  317. uint32_t temp = 0;
  318. SysTick->LOAD = (uint32_t)(nus * fac_us);
  319. SysTick->VAL = 0x00;
  320. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
  321. do
  322. {
  323. temp = SysTick->CTRL;
  324. }while((temp & 0x01) && !(temp & (1 << 16)));
  325. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  326. SysTick->VAL = 0x00;
  327. }
  328. /**
  329. * @brief inserts a delay time.
  330. * @param nms: specifies the delay time length, in milliseconds.
  331. * @retval none
  332. */
  333. void delay_ms(uint16_t nms)
  334. {
  335. uint32_t temp = 0;
  336. while(nms)
  337. {
  338. if(nms > STEP_DELAY_MS)
  339. {
  340. SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
  341. nms -= STEP_DELAY_MS;
  342. }
  343. else
  344. {
  345. SysTick->LOAD = (uint32_t)(nms * fac_ms);
  346. nms = 0;
  347. }
  348. SysTick->VAL = 0x00;
  349. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  350. do
  351. {
  352. temp = SysTick->CTRL;
  353. }while((temp & 0x01) && !(temp & (1 << 16)));
  354. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  355. SysTick->VAL = 0x00;
  356. }
  357. }
  358. /**
  359. * @brief inserts a delay time.
  360. * @param sec: specifies the delay time, in seconds.
  361. * @retval none
  362. */
  363. void delay_sec(uint16_t sec)
  364. {
  365. uint16_t index;
  366. for(index = 0; index < sec; index++)
  367. {
  368. delay_ms(500);
  369. delay_ms(500);
  370. }
  371. }
  372. /**
  373. * @}
  374. */
  375. /**
  376. * @}
  377. */