at32f403a_407_board.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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 defined (__GNUC__) && !defined (__clang__)
  75. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  76. #else
  77. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  78. #endif
  79. /**
  80. * @brief retargets the c library printf function to the usart.
  81. * @param none
  82. * @retval none
  83. */
  84. PUTCHAR_PROTOTYPE
  85. {
  86. while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
  87. usart_data_transmit(PRINT_UART, (uint16_t)ch);
  88. while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
  89. return ch;
  90. }
  91. #if (defined (__GNUC__) && !defined (__clang__)) || (defined (__ICCARM__))
  92. #if defined (__GNUC__) && !defined (__clang__)
  93. int _write(int fd, char *pbuffer, int size)
  94. #elif defined ( __ICCARM__ )
  95. #pragma module_name = "?__write"
  96. int __write(int fd, char *pbuffer, int size)
  97. #endif
  98. {
  99. for(int i = 0; i < size; i ++)
  100. {
  101. while(usart_flag_get(PRINT_UART, USART_TDBE_FLAG) == RESET);
  102. usart_data_transmit(PRINT_UART, (uint16_t)(*pbuffer++));
  103. while(usart_flag_get(PRINT_UART, USART_TDC_FLAG) == RESET);
  104. }
  105. return size;
  106. }
  107. #endif
  108. /**
  109. * @brief initialize uart
  110. * @param baudrate: uart baudrate
  111. * @retval none
  112. */
  113. void uart_print_init(uint32_t baudrate)
  114. {
  115. gpio_init_type gpio_init_struct;
  116. #if defined (__GNUC__) && !defined (__clang__)
  117. setvbuf(stdout, NULL, _IONBF, 0);
  118. #endif
  119. /* enable the uart and gpio clock */
  120. crm_periph_clock_enable(PRINT_UART_CRM_CLK, TRUE);
  121. crm_periph_clock_enable(PRINT_UART_TX_GPIO_CRM_CLK, TRUE);
  122. /*
  123. crm_periph_clock_enable(CRM_IOMUX_PERIPH_CLOCK, TRUE);
  124. gpio_pin_remap_config(USART3_GMUX_0011, TRUE);
  125. */
  126. gpio_default_para_init(&gpio_init_struct);
  127. /* configure the uart tx pin */
  128. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  129. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  130. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  131. gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN;
  132. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  133. gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);
  134. /* configure uart param */
  135. usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
  136. usart_transmitter_enable(PRINT_UART, TRUE);
  137. usart_receiver_enable(PRINT_UART, TRUE);
  138. usart_enable(PRINT_UART, TRUE);
  139. nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  140. NVIC_ClearPendingIRQ(USART1_IRQn);
  141. nvic_irq_enable(USART1_IRQn, 0, 5);
  142. usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
  143. }
  144. #if 0
  145. void USART1_IRQHandler(void)
  146. {
  147. static char data = 0;
  148. if (USART1->ctrl1_bit.rdbfien == SET)
  149. {
  150. if (usart_flag_get(USART1, USART_RDBF_FLAG) == SET)
  151. {
  152. data = usart_data_receive(USART1);
  153. }
  154. }
  155. }
  156. #endif
  157. /**
  158. * @brief board initialize interface init led and button
  159. * @param none
  160. * @retval none
  161. */
  162. void at32_board_init()
  163. {
  164. /* initialize delay function */
  165. delay_init();
  166. /* configure led in at_start_board */
  167. at32_led_init(LED2);
  168. at32_led_init(LED3);
  169. at32_led_init(LED4);
  170. at32_led_off(LED2);
  171. at32_led_off(LED3);
  172. at32_led_off(LED4);
  173. /* configure button in at_start board */
  174. at32_button_init();
  175. }
  176. /**
  177. * @brief configure button gpio
  178. * @param button: specifies the button to be configured.
  179. * @retval none
  180. */
  181. void at32_button_init(void)
  182. {
  183. gpio_init_type gpio_init_struct;
  184. /* enable the button clock */
  185. crm_periph_clock_enable(USER_BUTTON_CRM_CLK, TRUE);
  186. /* set default parameter */
  187. gpio_default_para_init(&gpio_init_struct);
  188. /* configure button pin as input with pull-up/pull-down */
  189. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  190. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  191. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  192. gpio_init_struct.gpio_pins = USER_BUTTON_PIN;
  193. gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
  194. gpio_init(USER_BUTTON_PORT, &gpio_init_struct);
  195. }
  196. /**
  197. * @brief returns the selected button state
  198. * @param none
  199. * @retval the button gpio pin value
  200. */
  201. uint8_t at32_button_state(void)
  202. {
  203. return gpio_input_data_bit_read(USER_BUTTON_PORT, USER_BUTTON_PIN);
  204. }
  205. /**
  206. * @brief returns which button have press down
  207. * @param none
  208. * @retval the button have press down
  209. */
  210. button_type at32_button_press()
  211. {
  212. static uint8_t pressed = 1;
  213. /* get button state in at_start board */
  214. if((pressed == 1) && (at32_button_state() != RESET))
  215. {
  216. /* debounce */
  217. pressed = 0;
  218. delay_ms(10);
  219. if(at32_button_state() != RESET)
  220. return USER_BUTTON;
  221. }
  222. else if(at32_button_state() == RESET)
  223. {
  224. pressed = 1;
  225. }
  226. return NO_BUTTON;
  227. }
  228. /**
  229. * @brief configure led gpio
  230. * @param led: specifies the led to be configured.
  231. * @retval none
  232. */
  233. void at32_led_init(led_type led)
  234. {
  235. gpio_init_type gpio_init_struct;
  236. /* enable the led clock */
  237. crm_periph_clock_enable(led_gpio_crm_clk[led], TRUE);
  238. /* set default parameter */
  239. gpio_default_para_init(&gpio_init_struct);
  240. /* configure the led gpio */
  241. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  242. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  243. gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  244. gpio_init_struct.gpio_pins = led_gpio_pin[led];
  245. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  246. gpio_init(led_gpio_port[led], &gpio_init_struct);
  247. }
  248. /**
  249. * @brief turns selected led on.
  250. * @param led: specifies the led to be set on.
  251. * this parameter can be one of following parameters:
  252. * @arg LED2
  253. * @arg LED3
  254. * @arg LED4
  255. * @retval none
  256. */
  257. void at32_led_on(led_type led)
  258. {
  259. if(led > (LED_NUM - 1))
  260. return;
  261. if(led_gpio_pin[led])
  262. led_gpio_port[led]->clr = led_gpio_pin[led];
  263. }
  264. /**
  265. * @brief turns selected led off.
  266. * @param led: specifies the led to be set off.
  267. * this parameter can be one of following parameters:
  268. * @arg LED2
  269. * @arg LED3
  270. * @arg LED4
  271. * @retval none
  272. */
  273. void at32_led_off(led_type led)
  274. {
  275. if(led > (LED_NUM - 1))
  276. return;
  277. if(led_gpio_pin[led])
  278. led_gpio_port[led]->scr = led_gpio_pin[led];
  279. }
  280. /**
  281. * @brief turns selected led toggle.
  282. * @param led: specifies the led to be set off.
  283. * this parameter can be one of following parameters:
  284. * @arg LED2
  285. * @arg LED3
  286. * @arg LED4
  287. * @retval none
  288. */
  289. void at32_led_toggle(led_type led)
  290. {
  291. if(led > (LED_NUM - 1))
  292. return;
  293. if(led_gpio_pin[led])
  294. led_gpio_port[led]->odt ^= led_gpio_pin[led];
  295. }
  296. /**
  297. * @brief initialize delay function
  298. * @param none
  299. * @retval none
  300. */
  301. void delay_init()
  302. {
  303. /* configure systick */
  304. systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  305. fac_us = system_core_clock / (1000000U);
  306. fac_ms = fac_us * (1000U);
  307. }
  308. /**
  309. * @brief inserts a delay time.
  310. * @param nus: specifies the delay time length, in microsecond.
  311. * @retval none
  312. */
  313. void delay_us(uint32_t nus)
  314. {
  315. uint32_t temp = 0;
  316. SysTick->LOAD = (uint32_t)(nus * fac_us);
  317. SysTick->VAL = 0x00;
  318. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
  319. do
  320. {
  321. temp = SysTick->CTRL;
  322. }while((temp & 0x01) && !(temp & (1 << 16)));
  323. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  324. SysTick->VAL = 0x00;
  325. }
  326. /**
  327. * @brief inserts a delay time.
  328. * @param nms: specifies the delay time length, in milliseconds.
  329. * @retval none
  330. */
  331. void delay_ms(uint16_t nms)
  332. {
  333. uint32_t temp = 0;
  334. while(nms)
  335. {
  336. if(nms > STEP_DELAY_MS)
  337. {
  338. SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
  339. nms -= STEP_DELAY_MS;
  340. }
  341. else
  342. {
  343. SysTick->LOAD = (uint32_t)(nms * fac_ms);
  344. nms = 0;
  345. }
  346. SysTick->VAL = 0x00;
  347. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  348. do
  349. {
  350. temp = SysTick->CTRL;
  351. }while((temp & 0x01) && !(temp & (1 << 16)));
  352. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  353. SysTick->VAL = 0x00;
  354. }
  355. }
  356. /**
  357. * @brief inserts a delay time.
  358. * @param sec: specifies the delay time, in seconds.
  359. * @retval none
  360. */
  361. void delay_sec(uint16_t sec)
  362. {
  363. uint16_t index;
  364. for(index = 0; index < sec; index++)
  365. {
  366. delay_ms(500);
  367. delay_ms(500);
  368. }
  369. }
  370. /**
  371. * @}
  372. */
  373. /**
  374. * @}
  375. */