hwsetup.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /************************************************************************
  2. Project: Welling Motor Control Paltform
  3. Filename: hwsetup.c
  4. Partner Filename: hwsetup.h
  5. Description: Hardware setup
  6. Complier: IAR Embedded Workbench for ARM 8.40.2
  7. CPU TYPE : GD32F30x
  8. *************************************************************************
  9. Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  10. All rights reserved.
  11. *************************************************************************
  12. *************************************************************************
  13. Revising History (ECL of this file):
  14. ************************************************************************/
  15. /************************************************************************
  16. Beginning of File, do not put anything above here except notes
  17. Compiler Directives:
  18. *************************************************************************/
  19. #ifndef _HWSETUP_C_
  20. #define _HWSETUP_C_
  21. #endif
  22. /************************************************************************
  23. Included File
  24. *************************************************************************/
  25. /*#include "user.h"
  26. #include "FuncLayerAPI.h"
  27. #include "can.h"*/
  28. #include "hwsetup.h"
  29. #include "uart_driver.h"
  30. #include "RefreshMenu.h"
  31. /************************************************************************
  32. Constant Table (N/A)
  33. *************************************************************************/
  34. /*************************************************************************
  35. Exported Functions:
  36. *************************************************************************/
  37. /*************************************************************************
  38. Function: hw_voHardwareSetup;
  39. Description: Hardware Setup function.
  40. Call by: main();
  41. Input Variables: All HW registers
  42. Output/Return Variables: All HW registers
  43. Subroutine Call: ...;
  44. Reference: N/A
  45. *************************************************************************/
  46. void hw_voHardwareSetup(void)
  47. {
  48. // Initialize System Control registers, WatchDog to a know state
  49. hw_voInitSysCtrl();
  50. // nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
  51. // Select GPIO for the device or for the specific application:
  52. hw_voInitGPIO();
  53. hw_voInitSysTick();
  54. hw_voInitUART();
  55. }
  56. /*************************************************************************
  57. Function:
  58. Description:
  59. Call by:
  60. Input Variables:
  61. Output/Return Variables:
  62. Subroutine Call:
  63. Reference:
  64. *************************************************************************/
  65. void hw_voInitSysCtrl(void)
  66. {
  67. // /* reset crm */
  68. // crm_reset();
  69. //
  70. // /* config flash psr register */
  71. // flash_psr_set(FLASH_WAIT_CYCLE_2);
  72. //
  73. // /* enable lick */
  74. // crm_clock_source_enable(CRM_CLOCK_SOURCE_LICK, TRUE);
  75. //
  76. // /* wait till lick is ready */
  77. // while(crm_flag_get(CRM_LICK_STABLE_FLAG) != SET)
  78. // {
  79. // }
  80. //
  81. // /* enable hick */
  82. // crm_clock_source_enable(CRM_CLOCK_SOURCE_HICK, TRUE);
  83. //
  84. // /* wait till hick is ready */
  85. // while(crm_flag_get(CRM_HICK_STABLE_FLAG) != SET)
  86. // {
  87. // }
  88. //
  89. // /* config pll clock resource */
  90. // crm_pll_config(CRM_PLL_SOURCE_HICK, CRM_PLL_MULT_18);
  91. //
  92. // /* enable pll */
  93. // crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE);
  94. //
  95. // /* wait till pll is ready */
  96. // while(crm_flag_get(CRM_PLL_STABLE_FLAG) != SET)
  97. // {
  98. // }
  99. //
  100. // /* config ahbclk */
  101. // crm_ahb_div_set(CRM_AHB_DIV_1);
  102. //
  103. // /* config apb2clk */
  104. // crm_apb2_div_set(CRM_APB2_DIV_1);
  105. //
  106. // /* config apb1clk */
  107. // crm_apb1_div_set(CRM_APB1_DIV_1);
  108. //
  109. // /* select pll as system clock source */
  110. // crm_sysclk_switch(CRM_SCLK_PLL);
  111. //
  112. // /* wait till pll is used as system clock source */
  113. // while(crm_sysclk_switch_status_get() != CRM_SCLK_PLL)
  114. // {
  115. // }
  116. //
  117. // /* update system_core_clock global variable */
  118. // system_core_clock_update();
  119. }
  120. /*************************************************************************
  121. Function:
  122. Description:
  123. Call by:
  124. Input Variables:
  125. Output/Return Variables:
  126. Subroutine Call:
  127. Reference:
  128. *************************************************************************/
  129. void hw_voInitGPIO(void)
  130. {
  131. // gpio_init_type gpio_init_struct;
  132. //
  133. // /* enable the gpioa clock */
  134. // crm_periph_clock_enable(CRM_GPIOC_PERIPH_CLOCK, TRUE);
  135. //
  136. // /* set default parameter */
  137. //// gpio_default_para_init(&gpio_init_struct);
  138. //
  139. // /* configure the gpio */
  140. // gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  141. // gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  142. // gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  143. // gpio_init_struct.gpio_pins = GPIO_PINS_15|GPIO_PINS_13;
  144. // gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  145. // gpio_init(GPIOC, &gpio_init_struct);
  146. //
  147. // gpio_bits_reset(GPIOC,GPIO_PINS_15);
  148. // gpio_bits_reset(GPIOC,GPIO_PINS_13);
  149. }
  150. /*************************************************************************
  151. Function:
  152. Description:
  153. Call by:
  154. Input Variables:
  155. Output/Return Variables:
  156. Subroutine Call:
  157. Reference:
  158. *************************************************************************/
  159. void hw_voInitUART()
  160. {
  161. // gpio_init_type gpio_init_struct;
  162. //
  163. // /* enable the usart2 and gpio clock */
  164. //
  165. // /* enable the usart1 and gpio clock */
  166. // crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
  167. // crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);
  168. //
  169. //// gpio_default_para_init(&gpio_init_struct);
  170. //
  171. // /* configure the usart2 tx/rx pin */
  172. // gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  173. // gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  174. // gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  175. // gpio_init_struct.gpio_pins = GPIO_PINS_6 | GPIO_PINS_7;
  176. // gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  177. // gpio_init(GPIOB, &gpio_init_struct);
  178. //
  179. // /* config usart1 iomux */
  180. // gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE6, GPIO_MUX_0);
  181. // gpio_pin_mux_config(GPIOB, GPIO_PINS_SOURCE7, GPIO_MUX_0);
  182. //
  183. // /* config usart2 and usart1 nvic interrupt */
  184. // nvic_irq_enable(USART1_IRQn, 0, 0);
  185. //
  186. // /* configure usart1 param */
  187. // usart_init(USART1, 19200, USART_DATA_8BITS, USART_STOP_1_BIT);
  188. // usart_transmitter_enable(USART1, TRUE);
  189. // usart_receiver_enable(USART1, TRUE);
  190. //
  191. // usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
  192. // usart_enable(USART1, TRUE);
  193. //
  194. // usart_interrupt_enable(USART1, USART_TDBE_INT, TRUE);
  195. //
  196. //
  197. //#if UART2_ENABLE
  198. // crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
  199. // crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  200. //
  201. // gpio_init_struct.gpio_pins = GPIO_PINS_14 | GPIO_PINS_15;
  202. // gpio_init(GPIOA, &gpio_init_struct);
  203. //
  204. // gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE14, GPIO_MUX_1);
  205. // gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE15, GPIO_MUX_1);
  206. //
  207. // nvic_irq_enable(USART2_IRQn, 0, 0);
  208. //
  209. // usart_init(USART2, 57600, USART_DATA_8BITS, USART_STOP_1_BIT);
  210. // usart_transmitter_enable(USART2, TRUE);
  211. // usart_receiver_enable(USART2, TRUE);
  212. //
  213. // usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
  214. // usart_enable(USART2, TRUE);
  215. //
  216. // usart_interrupt_enable(USART2, USART_TDBE_INT, TRUE);
  217. //#endif
  218. }
  219. /*************************************************************************
  220. Function:
  221. Description:
  222. Call by:
  223. Input Variables:
  224. Output/Return Variables:
  225. Subroutine Call:
  226. Reference:
  227. *************************************************************************/
  228. #define FSYSTICK_HZ 1000
  229. void hw_voInitSysTick(void)
  230. {
  231. // /* Setup systick timer interrupts */
  232. // /* Do not exceed 2^24-1=16777215 */
  233. // systick_clock_source_config(SYSTICK_CLOCK_SOURCE_AHBCLK_NODIV);
  234. //
  235. // SysTick_Config(SystemCoreClock / FSYSTICK_HZ);
  236. }
  237. volatile static uint32_t delay;
  238. uint32_t delaytest;
  239. /*!
  240. \brief delay a time in milliseconds
  241. \param[in] count: count in milliseconds
  242. \param[out] none
  243. \retval none
  244. */
  245. void delay_1ms(uint32_t count)
  246. {
  247. delay = count;
  248. while(0U != delay){
  249. }
  250. }
  251. /*!
  252. \brief delay decrement
  253. \param[in] none
  254. \param[out] none
  255. \retval none
  256. */
  257. //void delay_decrement(void)
  258. //{
  259. // if (0U != delay){
  260. // delay--;
  261. // }
  262. //}
  263. /*!
  264. \brief this function handles SysTick exception
  265. \param[in] none
  266. \param[out] none
  267. \retval none
  268. */
  269. void SysTick_Handler(void)
  270. {
  271. if (0U != delay){
  272. delay--;
  273. }
  274. delaytest=delay;
  275. // delay_decrement();
  276. Period_Stick();
  277. }
  278. /*************************************************************************
  279. Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  280. All rights reserved.
  281. *************************************************************************/
  282. #ifdef _HWSETUP_C_
  283. #undef _HWSETUP_C_
  284. #endif
  285. /************************************************************************
  286. End of this File (EOF)!
  287. Do not put anything after this part!
  288. *************************************************************************/