at32f403a_407_board.c 10 KB

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