at32f413_board.c 9.7 KB

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