at32f403a_407_board.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. gpio_default_para_init(&gpio_init_struct);
  123. /* configure the uart tx pin */
  124. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  125. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  126. gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  127. gpio_init_struct.gpio_pins = PRINT_UART_TX_PIN;
  128. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  129. gpio_init(PRINT_UART_TX_GPIO, &gpio_init_struct);
  130. /* configure uart param */
  131. usart_init(PRINT_UART, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
  132. usart_transmitter_enable(PRINT_UART, TRUE);
  133. usart_enable(PRINT_UART, TRUE);
  134. }
  135. /**
  136. * @brief board initialize interface init led and button
  137. * @param none
  138. * @retval none
  139. */
  140. void at32_board_init()
  141. {
  142. /* initialize delay function */
  143. delay_init();
  144. /* configure led in at_start_board */
  145. at32_led_init(LED2);
  146. at32_led_init(LED3);
  147. at32_led_init(LED4);
  148. at32_led_off(LED2);
  149. at32_led_off(LED3);
  150. at32_led_off(LED4);
  151. /* configure button in at_start board */
  152. at32_button_init();
  153. }
  154. /**
  155. * @brief configure button gpio
  156. * @param button: specifies the button to be configured.
  157. * @retval none
  158. */
  159. void at32_button_init(void)
  160. {
  161. gpio_init_type gpio_init_struct;
  162. /* enable the button clock */
  163. crm_periph_clock_enable(USER_BUTTON_CRM_CLK, TRUE);
  164. /* set default parameter */
  165. gpio_default_para_init(&gpio_init_struct);
  166. /* configure button pin as input with pull-up/pull-down */
  167. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  168. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  169. gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
  170. gpio_init_struct.gpio_pins = USER_BUTTON_PIN;
  171. gpio_init_struct.gpio_pull = GPIO_PULL_DOWN;
  172. gpio_init(USER_BUTTON_PORT, &gpio_init_struct);
  173. }
  174. /**
  175. * @brief returns the selected button state
  176. * @param none
  177. * @retval the button gpio pin value
  178. */
  179. uint8_t at32_button_state(void)
  180. {
  181. return gpio_input_data_bit_read(USER_BUTTON_PORT, USER_BUTTON_PIN);
  182. }
  183. /**
  184. * @brief returns which button have press down
  185. * @param none
  186. * @retval the button have press down
  187. */
  188. button_type at32_button_press()
  189. {
  190. static uint8_t pressed = 1;
  191. /* get button state in at_start board */
  192. if((pressed == 1) && (at32_button_state() != RESET))
  193. {
  194. /* debounce */
  195. pressed = 0;
  196. delay_ms(10);
  197. if(at32_button_state() != RESET)
  198. return USER_BUTTON;
  199. }
  200. else if(at32_button_state() == RESET)
  201. {
  202. pressed = 1;
  203. }
  204. return NO_BUTTON;
  205. }
  206. /**
  207. * @brief configure led gpio
  208. * @param led: specifies the led to be configured.
  209. * @retval none
  210. */
  211. void at32_led_init(led_type led)
  212. {
  213. gpio_init_type gpio_init_struct;
  214. /* enable the led clock */
  215. crm_periph_clock_enable(led_gpio_crm_clk[led], TRUE);
  216. /* set default parameter */
  217. gpio_default_para_init(&gpio_init_struct);
  218. /* configure the led gpio */
  219. gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  220. gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  221. gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  222. gpio_init_struct.gpio_pins = led_gpio_pin[led];
  223. gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  224. gpio_init(led_gpio_port[led], &gpio_init_struct);
  225. }
  226. /**
  227. * @brief turns selected led on.
  228. * @param led: specifies the led to be set on.
  229. * this parameter can be one of following parameters:
  230. * @arg LED2
  231. * @arg LED3
  232. * @arg LED4
  233. * @retval none
  234. */
  235. void at32_led_on(led_type led)
  236. {
  237. if(led > (LED_NUM - 1))
  238. return;
  239. if(led_gpio_pin[led])
  240. led_gpio_port[led]->clr = led_gpio_pin[led];
  241. }
  242. /**
  243. * @brief turns selected led off.
  244. * @param led: specifies the led to be set off.
  245. * this parameter can be one of following parameters:
  246. * @arg LED2
  247. * @arg LED3
  248. * @arg LED4
  249. * @retval none
  250. */
  251. void at32_led_off(led_type led)
  252. {
  253. if(led > (LED_NUM - 1))
  254. return;
  255. if(led_gpio_pin[led])
  256. led_gpio_port[led]->scr = led_gpio_pin[led];
  257. }
  258. /**
  259. * @brief turns selected led toggle.
  260. * @param led: specifies the led to be set off.
  261. * this parameter can be one of following parameters:
  262. * @arg LED2
  263. * @arg LED3
  264. * @arg LED4
  265. * @retval none
  266. */
  267. void at32_led_toggle(led_type led)
  268. {
  269. if(led > (LED_NUM - 1))
  270. return;
  271. if(led_gpio_pin[led])
  272. led_gpio_port[led]->odt ^= led_gpio_pin[led];
  273. }
  274. /**
  275. * @brief initialize delay function
  276. * @param none
  277. * @retval none
  278. */
  279. void delay_init()
  280. {
  281. /* configure systick */
  282. systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  283. fac_us = system_core_clock / (1000000U);
  284. fac_ms = fac_us * (1000U);
  285. }
  286. /**
  287. * @brief inserts a delay time.
  288. * @param nus: specifies the delay time length, in microsecond.
  289. * @retval none
  290. */
  291. void delay_us(uint32_t nus)
  292. {
  293. uint32_t temp = 0;
  294. SysTick->LOAD = (uint32_t)(nus * fac_us);
  295. SysTick->VAL = 0x00;
  296. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk ;
  297. do
  298. {
  299. temp = SysTick->CTRL;
  300. }while((temp & 0x01) && !(temp & (1 << 16)));
  301. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  302. SysTick->VAL = 0x00;
  303. }
  304. /**
  305. * @brief inserts a delay time.
  306. * @param nms: specifies the delay time length, in milliseconds.
  307. * @retval none
  308. */
  309. void delay_ms(uint16_t nms)
  310. {
  311. uint32_t temp = 0;
  312. while(nms)
  313. {
  314. if(nms > STEP_DELAY_MS)
  315. {
  316. SysTick->LOAD = (uint32_t)(STEP_DELAY_MS * fac_ms);
  317. nms -= STEP_DELAY_MS;
  318. }
  319. else
  320. {
  321. SysTick->LOAD = (uint32_t)(nms * fac_ms);
  322. nms = 0;
  323. }
  324. SysTick->VAL = 0x00;
  325. SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk;
  326. do
  327. {
  328. temp = SysTick->CTRL;
  329. }while((temp & 0x01) && !(temp & (1 << 16)));
  330. SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
  331. SysTick->VAL = 0x00;
  332. }
  333. }
  334. /**
  335. * @brief inserts a delay time.
  336. * @param sec: specifies the delay time, in seconds.
  337. * @retval none
  338. */
  339. void delay_sec(uint16_t sec)
  340. {
  341. uint16_t index;
  342. for(index = 0; index < sec; index++)
  343. {
  344. delay_ms(500);
  345. delay_ms(500);
  346. }
  347. }
  348. /**
  349. * @}
  350. */
  351. /**
  352. * @}
  353. */