hwsetup.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386
  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 "syspar.h"
  26. #include "user.h"
  27. #include "FuncLayerAPI.h"
  28. #include "can.h"
  29. #include "api_rt.h"
  30. /************************************************************************
  31. Constant Table (N/A)
  32. *************************************************************************/
  33. /*************************************************************************
  34. Exported Functions:
  35. *************************************************************************/
  36. /*************************************************************************
  37. Function: hw_voHardwareSetup;
  38. Description: Hardware Setup function.
  39. Call by: main();
  40. Input Variables: All HW registers
  41. Output/Return Variables: All HW registers
  42. Subroutine Call: ...;
  43. Reference: N/A
  44. *************************************************************************/
  45. void hw_voHardwareSetup1(void)
  46. {
  47. // Initialize System Control registers, WatchDog to a know state
  48. hw_voInitSysCtrl();
  49. // Select GPIO for the device or for the specific application:
  50. hw_voInitGPIO();
  51. }
  52. void hw_voHardwareSetup2(void)
  53. {
  54. // Initialize all the Device Peripherals to a known state:
  55. hw_voInitPeri();
  56. // Initialize interrupt priority of peripherals:
  57. hw_voInitInt();
  58. }
  59. /*************************************************************************
  60. Local Functions (N/A)
  61. *************************************************************************/
  62. /*************************************************************************
  63. Function: hw_voInitPWM;
  64. Description: MTU Setup function.
  65. Call by: main();
  66. Input Variables: MTU HW registers
  67. Output/Return Variables: MTU HW registers
  68. Subroutine Call: ...;
  69. Reference: N/A
  70. *************************************************************************/
  71. void hw_voInitPeri(void)
  72. {
  73. // Initialize Exti
  74. hw_voInitEXTI();
  75. // Initialize DMA
  76. hw_voInitDMA();
  77. // Initialize ADC
  78. hw_voInitADC();
  79. // Initialize Timer0
  80. hw_voInitTim0();
  81. // Initialize Timer1
  82. hw_voInitTim1();
  83. // Initialize Timer3
  84. hw_voInitTim2();
  85. // Initialize Timer4
  86. hw_voInitTim3();
  87. // Initialize Timer6
  88. hw_voInitTim5();
  89. // Initialize Timer16
  90. hw_voInitTim6();
  91. // Initialize USART
  92. hw_voInitUART3();
  93. // Initialize SPI2
  94. hw_voInitSPI2();
  95. // Initialize I2C1
  96. hw_voInitI2C0();
  97. // Initialize SysTick
  98. hw_voInitSysTick();
  99. // Initialize CAN
  100. hw_voInitCAN();
  101. }
  102. /*************************************************************************
  103. Function:
  104. Description:
  105. Call by:
  106. Input Variables:
  107. Output/Return Variables:
  108. Subroutine Call:
  109. Reference:
  110. *************************************************************************/
  111. void hw_voInitSysCtrl(void)
  112. {
  113. /* Enable the DMA0 clock */
  114. rcu_periph_clock_enable(RCU_DMA0);
  115. /* Enable the DMA1 clock */
  116. rcu_periph_clock_enable(RCU_DMA1);
  117. /* Enable the Alternate Function clock */
  118. rcu_periph_clock_enable(RCU_AF);
  119. /* Enable the GPIOA clock */
  120. rcu_periph_clock_enable(RCU_GPIOA);
  121. /* Enable the GPIOB clock */
  122. rcu_periph_clock_enable(RCU_GPIOB);
  123. /* Enable the GPIOC clock */
  124. rcu_periph_clock_enable(RCU_GPIOC);
  125. /* Enable the GPIOD clock */
  126. rcu_periph_clock_enable(RCU_GPIOD);
  127. /* Enable the GPIOF clock */
  128. rcu_periph_clock_enable(RCU_GPIOF);
  129. /* Enable the ADC0 ADC1 clock */
  130. rcu_periph_clock_enable(RCU_ADC0);
  131. rcu_periph_clock_enable(RCU_ADC1);
  132. rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV2);
  133. /* Enable the TIM1 clock */
  134. rcu_periph_clock_enable(RCU_TIMER0);
  135. /* Enable the TIM2 clock */
  136. rcu_periph_clock_enable(RCU_TIMER1);
  137. /* Enable the TIM3 clock */
  138. rcu_periph_clock_enable(RCU_TIMER2);
  139. /* Enable the TIM4 clock */
  140. rcu_periph_clock_enable(RCU_TIMER3);
  141. /* Enable the TIM6 clock */
  142. rcu_periph_clock_enable(RCU_TIMER5);
  143. /* Enable the TIM16 clock */
  144. rcu_periph_clock_enable(RCU_TIMER6);
  145. /* Enable the UART3 clock */
  146. rcu_periph_clock_enable(RCU_UART3);
  147. /* Enable the SPI2 clock */
  148. rcu_periph_clock_enable(RCU_SPI2);
  149. /* Enable the I2C0 clock */
  150. rcu_periph_clock_enable(RCU_I2C0);
  151. /* Enable the CAN clock */
  152. rcu_periph_clock_enable(RCU_CAN0);
  153. }
  154. /*************************************************************************
  155. Function:
  156. Description:
  157. Call by:
  158. Input Variables:
  159. Output/Return Variables:
  160. Subroutine Call:
  161. Reference:
  162. *************************************************************************/
  163. void hw_voInitGPIO(void)
  164. {
  165. /*=======================================================================
  166. GPIO A
  167. =======================================================================*/
  168. gpio_init(GPIOA,GPIO_MODE_AIN,GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3); /* ADC01_IN0,1,2,3: Iu, Iv, Iw, Torque*/
  169. gpio_init(GPIOA,GPIO_MODE_AIN,GPIO_OSPEED_50MHZ, GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7); /* ADC01_IN4,5,6,7: Ibus Ubus U_6V U_5V*/
  170. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10); /* TIM0_CH0,1,2: UH, VH, WH */
  171. gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_11); /* TIM0_CH3: ADC Trigger */
  172. //gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_11);
  173. gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_12); /* CAN STB */
  174. gpio_bit_reset(GPIOA, GPIO_PIN_12);
  175. /* Dont config PA13, for it is used for SWDIO FOR DEBUG*/
  176. /* Dont config PA14, for it is used for SWCLK FOR DEBUG*/
  177. gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ, GPIO_PIN_15); /* ASSIST STATE IN */
  178. gpio_bit_reset(GPIOA, GPIO_PIN_12);
  179. /*=======================================================================
  180. GPIO B
  181. =======================================================================*/
  182. gpio_init(GPIOB,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_0|GPIO_PIN_1); /* Position Sensor Magnet Error Detect: LO, HI*/
  183. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_2); /* Cadence direction */
  184. /* JTAG-DP disabled and SW-DP enabled, so SPI2 can use PB3 and PB4*/
  185. gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP,ENABLE);
  186. gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5); /* SPI2_SCK, SPI2_MOSI*/
  187. gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4); /* SPI2_MISO */
  188. gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7); /* I2C0_SCL, I2C0_SDA*/
  189. /* Remap CAN0 GPIO */
  190. gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP,ENABLE);
  191. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_8); /* CAN_RX */
  192. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9); /* CAN_TX*/
  193. /* Remap Timer1 GPIO */
  194. gpio_pin_remap_config(GPIO_TIMER1_FULL_REMAP,ENABLE);
  195. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_10); /* TIM1_CH2: Capture For Candence Frequency */
  196. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_11); /* TIM1_CH3: Capture For Bike Speed */
  197. gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_12); /* TIM0_BRKIN */
  198. gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15); /* TIM0_CH0N,1N,2N: UL, VL, WL */
  199. /*=======================================================================
  200. GPIO C
  201. =======================================================================*/
  202. gpio_init(GPIOC,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_0); /* Switch State */
  203. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1); /* Power Lock */
  204. gpio_init(GPIOC,GPIO_MODE_AIN,GPIO_OSPEED_50MHZ, GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5); /* ADC01_IN12,13,14,15: U_12V, Throttle, PCB_Temp, Motor_Temp */
  205. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8); /* Test Pins*/
  206. //gpio_init(GPIOC,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8); /* TIM2_CH0,1,2: HallA, HallB, HallC */
  207. gpio_pin_remap_config(GPIO_TIMER2_FULL_REMAP,ENABLE);
  208. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_9); /* TIM2_CH3: 70KHz For Torque Sensor*/
  209. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10); /* UART3_TX*/
  210. gpio_init(GPIOC,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_11); /* UART3_RX */
  211. // gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_10); // test
  212. //
  213. // gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_11); // test
  214. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_12); /* I2C0 Write Protect*/
  215. gpio_init(GPIOC,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_14); /* Bike Brake */
  216. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_15); /* Light Enable */
  217. /*=======================================================================
  218. GPIO D
  219. =======================================================================*/
  220. gpio_init(GPIOD,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_2); /* SPI Chip Select */
  221. gpio_bit_set(GPIOD, GPIO_PIN_2);
  222. }
  223. /*************************************************************************
  224. Function:
  225. Description:
  226. Call by:
  227. Input Variables:
  228. Output/Return Variables:
  229. Subroutine Call:
  230. Reference:E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\I2C\I2C_EEPROM_dma\at24cxx.c
  231. E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\EXTI\Key_external_interrupt_mode\gd32f30x_it.c
  232. ***************************************E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\I2C\I2C_EEPROM_dma\at24cxx.c
  233. E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\EXTI\Key_external_interrupt_mode\gd32f30x_it.c**********************************/
  234. void hw_voInitEXTI(void)
  235. {
  236. // EXTI_InitTypeDef EXTI_InitStructure;
  237. //
  238. // SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource14); // Break extern interrupt extern interrupt trigger source PIN
  239. // EXTI_InitStructure.EXTI_Line = EXTI_Line14;
  240. // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  241. // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  242. // EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  243. // EXTI_Init(&EXTI_InitStructure);
  244. }
  245. /*************************************************************************
  246. Function:
  247. Description:
  248. Call by:
  249. Input Variables:
  250. Output/Return Variables:
  251. Subroutine Call:
  252. Reference:
  253. *************************************************************************/
  254. void hw_voInitDMA(void)
  255. {
  256. dma_parameter_struct dma_init_struct;
  257. /*=======================================================================
  258. DMA0 Ch4 for Timer0
  259. =======================================================================*/
  260. dma_deinit(DMA0,DMA_CH4);
  261. dma_init_struct.periph_addr = (uint32_t)(&TIMER_DMATB(TIMER0));
  262. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  263. dma_init_struct.memory_addr = (uint32_t)(hw_uwPWMCmpr);
  264. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  265. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  266. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  267. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  268. dma_init_struct.number = TIM1_DMA_NUM;
  269. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  270. dma_init(DMA0,DMA_CH4,&dma_init_struct);
  271. dma_circulation_enable(DMA0,DMA_CH4);
  272. /* enable DMA0 channel4 */
  273. //dma_channel_enable(DMA0,DMA_CH4);
  274. /*=======================================================================
  275. DMA0 Ch0 for ADC0
  276. =======================================================================*/
  277. dma_deinit(DMA0, DMA_CH0);
  278. /* initialize DMA single data mode */
  279. dma_init_struct.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
  280. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  281. dma_init_struct.memory_addr = (uint32_t)(Adcs[0].Results);
  282. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  283. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  284. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  285. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  286. dma_init_struct.number = ADC0_DMA_NUM;
  287. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  288. dma_init(DMA0, DMA_CH0, &dma_init_struct);
  289. dma_circulation_enable(DMA0, DMA_CH0);
  290. /* enable DMA0 channel0 */
  291. dma_channel_enable(DMA0, DMA_CH0);
  292. /*=======================================================================
  293. DMA1 Ch4 for ADC1
  294. =======================================================================*/
  295. /////////// !!!no dma for adc1
  296. // dma_deinit(DMA1, DMA_CH4);
  297. //
  298. // /* initialize DMA single data mode */
  299. // dma_init_struct.periph_addr = (ULONG)(&ADC_RDATA(ADC1));
  300. // dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  301. // dma_init_struct.memory_addr = (ULONG)(hw_uwADC2);
  302. // dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  303. // dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  304. // dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  305. // dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  306. // dma_init_struct.number = ADC2_DMA_NUM;
  307. // dma_init_struct.priority = DMA_PRIORITY_HIGH;
  308. // dma_init(DMA1, DMA_CH4, &dma_init_struct);
  309. //
  310. // dma_circulation_enable(DMA1, DMA_CH4);
  311. //
  312. // /* enable DMA1 channel4 */
  313. // dma_channel_enable(DMA1, DMA_CH4);
  314. /*=======================================================================
  315. DMA0 Ch5 for I2C0 transmission
  316. =======================================================================*/
  317. dma_deinit(DMA0, DMA_CH5);
  318. dma_init_struct.periph_addr = (uint32_t)(&I2C_DATA(I2C0));
  319. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  320. dma_init_struct.memory_addr = (uint32_t)(I2C_ubWriteBuffer);
  321. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  322. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  323. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  324. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  325. dma_init_struct.number = I2C1_TX_DMA_NUM;
  326. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  327. dma_init(DMA0, DMA_CH5, &dma_init_struct);
  328. dma_circulation_disable(DMA0, DMA_CH5);
  329. /* enable DMA0 channel5 */
  330. //dma_channel_enable(DMA0, DMA_CH5);
  331. /*=======================================================================
  332. DMA0 Ch6 for I2C0 receive
  333. =======================================================================*/
  334. dma_deinit(DMA0, DMA_CH6);
  335. dma_init_struct.periph_addr = (uint32_t)(&I2C_DATA(I2C0));
  336. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  337. dma_init_struct.memory_addr = (uint32_t)(I2C_ubReadBuffer[0]);
  338. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  339. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  340. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  341. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  342. dma_init_struct.number = I2C1_RX_DMA_NUM;
  343. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  344. dma_init(DMA0, DMA_CH6, &dma_init_struct);
  345. dma_circulation_disable(DMA0, DMA_CH6);
  346. /* enable DMA0 channel6 */
  347. //dma_channel_enable(DMA0, DMA_CH6);
  348. /*=======================================================================
  349. DMA1 Ch4 for UART3 transmission
  350. =======================================================================*/
  351. dma_deinit(DMA1, DMA_CH4);
  352. dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
  353. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  354. dma_init_struct.memory_addr = (uint32_t)(UART_pWriteBuffer);
  355. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  356. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  357. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  358. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  359. dma_init_struct.number = 64;
  360. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  361. dma_init(DMA1, DMA_CH4, &dma_init_struct);
  362. dma_circulation_disable(DMA1, DMA_CH4);
  363. /* enable DMA1 channel4 */
  364. //dma_channel_enable(DMA1, DMA_CH4);
  365. /*=======================================================================
  366. DMA1 Ch2 for UART3 receive
  367. =======================================================================*/
  368. dma_deinit(DMA1, DMA_CH2);
  369. dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
  370. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  371. dma_init_struct.memory_addr = (uint32_t)(UART_ubReadBuffer);
  372. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  373. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  374. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  375. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  376. dma_init_struct.number = 22;
  377. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  378. dma_init(DMA1, DMA_CH2, &dma_init_struct);
  379. dma_circulation_disable(DMA1, DMA_CH2);
  380. /* enable DMA1 channel2 */
  381. dma_channel_enable(DMA1, DMA_CH2);
  382. }
  383. /*************************************************************************
  384. Function:
  385. Description:
  386. Call by:
  387. Input Variables:
  388. Output/Return Variables:
  389. Subroutine Call:
  390. Reference:
  391. *************************************************************************/
  392. void hw_voInitADC(void)
  393. {
  394. /*=======================================================================
  395. ADC0
  396. =======================================================================*/
  397. /* ADC reset */
  398. adc_deinit(ADC0);
  399. /* ADC mode config */
  400. adc_mode_config(ADC_MODE_FREE);
  401. /* ADC special function config */
  402. adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
  403. adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
  404. /* ADC data alignment config */
  405. adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
  406. /* ADC channel length config */
  407. adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 8);
  408. adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 3);
  409. /* ADC regular channel config */
  410. adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_5, ADC_SAMPLETIME_7POINT5); //PA5 Ubus
  411. adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_6, ADC_SAMPLETIME_7POINT5); //PA6 U_6V
  412. adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_7, ADC_SAMPLETIME_7POINT5); //PA7 U_5V
  413. adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_14, ADC_SAMPLETIME_7POINT5); //PC4 PCB_Temp
  414. adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_15, ADC_SAMPLETIME_7POINT5); //PC5 Motor_Temp
  415. adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_12, ADC_SAMPLETIME_7POINT5); //PC2 U_12V
  416. adc_regular_channel_config(ADC0, 6, ADC_CHANNEL_13, ADC_SAMPLETIME_7POINT5); //PC3 Throtttle
  417. adc_regular_channel_config(ADC0, 7, ADC_CHANNEL_3, ADC_SAMPLETIME_7POINT5); //PA3 Torque
  418. /* 8· = 0.55*8 = 4.44us */
  419. /* ADC inserted channel config */
  420. adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_7POINT5); //PA0 Iu
  421. adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_7POINT5); //PA1 Iv
  422. adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_2, ADC_SAMPLETIME_7POINT5); //PA2 Iw
  423. /* ADC trigger config */
  424. adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
  425. adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);
  426. /* ADC external trigger enable */
  427. adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
  428. adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
  429. /* ADC DMA function enable */
  430. adc_dma_mode_enable(ADC0);
  431. /* enable ADC interface */
  432. adc_enable(ADC0);
  433. /* ADC calibration and reset calibration */
  434. adc_calibration_enable(ADC0);
  435. /* ADC software trigger enable */
  436. adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
  437. /*=======================================================================
  438. ADC1
  439. =======================================================================*/
  440. /* ADC reset */
  441. adc_deinit(ADC1);
  442. /* ADC special function config */
  443. adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);
  444. adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, DISABLE);
  445. /* ADC data alignment config */
  446. adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
  447. /* ADC channel length config */
  448. adc_channel_length_config(ADC1, ADC_INSERTED_CHANNEL, 1);
  449. /* ADC inserted channel config */
  450. adc_inserted_channel_config(ADC1, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_7POINT5); //PA4 Ibus
  451. /* ADC trigger config */
  452. adc_external_trigger_source_config(ADC1, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);
  453. /* ADC external trigger enable */
  454. adc_external_trigger_config(ADC1, ADC_INSERTED_CHANNEL, ENABLE);
  455. /* enable ADC interface */
  456. adc_enable(ADC1);
  457. /* ADC calibration and reset calibration */
  458. adc_calibration_enable(ADC1);
  459. }
  460. /*************************************************************************
  461. Function:
  462. Description:
  463. Call by:
  464. Input Variables:
  465. Output/Return Variables:
  466. Subroutine Call:
  467. Reference:
  468. *************************************************************************/
  469. void hw_voInitTim0(void)
  470. {
  471. timer_oc_parameter_struct timer_ocintpara;
  472. timer_parameter_struct timer_initpara;
  473. timer_break_parameter_struct timer_breakpara;
  474. /* Timer reset */
  475. timer_deinit(TIMER0);
  476. timer_struct_para_init(&timer_initpara); // must init, or timer_initpara.counterdirection will be arbitrary
  477. /* TIMER0 configuration */
  478. timer_initpara.prescaler = 0;
  479. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
  480. timer_initpara.period = HW_HPWM_PERIOD;
  481. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  482. timer_initpara.repetitioncounter = 0;
  483. timer_init(TIMER0,&timer_initpara);
  484. /* CH0, CH1 and CH2 configuration as PWM complementary output */
  485. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  486. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  487. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  488. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  489. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  490. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  491. timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
  492. timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
  493. timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
  494. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, HW_HHPWM_PERIOD);
  495. timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM1);
  496. timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
  497. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, HW_HHPWM_PERIOD);
  498. timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM1);
  499. timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
  500. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, HW_HHPWM_PERIOD);
  501. timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM1);
  502. timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
  503. /* CH3 configuration as ADC trigger signal */
  504. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  505. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  506. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  507. timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocintpara);
  508. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 200);
  509. timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM1);
  510. //timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
  511. timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_ENABLE);
  512. /* automatic output enable, break, dead time and lock configuration*/
  513. timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
  514. timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
  515. timer_breakpara.deadtime = cp_stControlPara.swIPMDeadTimeNs / 100 * TIM0CLK_KHZ / 1000 / 10;;
  516. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  517. timer_breakpara.outputautostate = TIMER_OUTAUTO_DISABLE;
  518. timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
  519. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  520. timer_break_config(TIMER0,&timer_breakpara);
  521. /* TIMER0 primary output enable */
  522. timer_primary_output_config(TIMER0,ENABLE);
  523. /* TIMER0 update DMA request enable */
  524. timer_dma_transfer_config(TIMER0,TIMER_DMACFG_DMATA_CH0CV,TIMER_DMACFG_DMATC_3TRANSFER);
  525. //timer_dma_enable(TIMER0,TIMER_DMA_UPD);
  526. /* auto-reload preload enable */
  527. timer_auto_reload_shadow_enable(TIMER0);
  528. /* select the master slave mode */
  529. timer_master_slave_mode_config(TIMER0, TIMER_MASTER_SLAVE_MODE_ENABLE);
  530. /* Update event is used as trigger output */
  531. timer_master_output_trigger_source_select(TIMER0, TIMER_TRI_OUT_SRC_UPDATE);
  532. /* TIMER0 counter enable */
  533. ////timer_enable(TIMER0);
  534. }
  535. /*************************************************************************
  536. Function:
  537. Description:
  538. Call by:
  539. Input Variables:
  540. Output/Return Variables:
  541. Subroutine Call:
  542. Reference:
  543. *************************************************************************/
  544. void hw_voInitTim1(void)
  545. {
  546. timer_ic_parameter_struct timer_icinitpara;
  547. timer_parameter_struct timer_initpara;
  548. timer_deinit(TIMER1);
  549. /* TIMER1 configuration */
  550. timer_initpara.prescaler = 99;
  551. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  552. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  553. timer_initpara.period = TIM1CLK_KHZ * 1000 / (1000 / 25); // period = 25ms
  554. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  555. timer_initpara.repetitioncounter = 0;
  556. timer_init(TIMER1,&timer_initpara);
  557. /* TIMER1 CH0 input capture configuration */
  558. timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
  559. timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
  560. timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
  561. timer_icinitpara.icfilter = 0x0;
  562. timer_input_capture_config(TIMER1,TIMER_CH_2,&timer_icinitpara);
  563. timer_input_capture_config(TIMER1,TIMER_CH_3,&timer_icinitpara);
  564. /* auto-reload preload enable */
  565. timer_auto_reload_shadow_enable(TIMER1);
  566. /* TIMER1 counter enable */
  567. ////timer_enable(TIMER1);
  568. }
  569. /*************************************************************************
  570. Function:
  571. Description:
  572. Call by:
  573. Input Variables:
  574. Output/Return Variables:
  575. Subroutine Call:
  576. Reference:
  577. *************************************************************************/
  578. void hw_voInitTim2(void)
  579. {
  580. timer_oc_parameter_struct timer_ocintpara;
  581. timer_parameter_struct timer_initpara;
  582. /* Timer reset */
  583. timer_deinit(TIMER2);
  584. /* TIMER2 configuration */
  585. timer_initpara.prescaler = 0;
  586. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  587. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  588. timer_initpara.period = TIM2CLK_KHZ / 70; // 70kHz for torque sensor
  589. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  590. timer_initpara.repetitioncounter = 0;
  591. timer_init(TIMER2,&timer_initpara);
  592. /* CH3 configuration for 70kHz output */
  593. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  594. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  595. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  596. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  597. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  598. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  599. timer_channel_output_config(TIMER2, TIMER_CH_3, &timer_ocintpara);
  600. timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, (1028>>1));
  601. timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM1);
  602. timer_channel_output_shadow_config(TIMER2, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
  603. /* TIMER2 primary output enable */
  604. timer_primary_output_config(TIMER2,ENABLE);
  605. /* auto-reload preload enable */
  606. timer_auto_reload_shadow_enable(TIMER2);
  607. /* TIMER2 counter enable */
  608. ////timer_enable(TIMER2);
  609. }
  610. /*************************************************************************
  611. Function:
  612. Description:
  613. Call by:
  614. Input Variables:
  615. Output/Return Variables:
  616. Subroutine Call:
  617. Reference:
  618. *************************************************************************/
  619. void hw_voInitTim3(void)
  620. {
  621. timer_parameter_struct timer_initpara;
  622. timer_deinit(TIMER3);
  623. /* TIMER configuration */
  624. timer_initpara.prescaler = 0;
  625. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  626. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  627. timer_initpara.period = (FTBC_HZ<<2)/FTBS_HZ-1;
  628. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  629. timer_initpara.repetitioncounter = 0;
  630. timer_init(TIMER3,&timer_initpara);
  631. /* auto-reload preload enable */
  632. timer_auto_reload_shadow_enable(TIMER3);
  633. /* slave mode selection: TIMER3 */
  634. timer_slave_mode_select(TIMER3, TIMER_SLAVE_MODE_EXTERNAL0);
  635. timer_input_trigger_source_select(TIMER3, TIMER_SMCFG_TRGSEL_ITI0);
  636. /* select the master slave mode */
  637. timer_master_slave_mode_config(TIMER3, TIMER_MASTER_SLAVE_MODE_ENABLE);
  638. /* TIMER3 counter enable */
  639. ////timer_enable(TIMER3);
  640. }
  641. /*************************************************************************
  642. Function:
  643. Description:
  644. Call by:
  645. Input Variables:
  646. Output/Return Variables:
  647. Subroutine Call:
  648. Reference:
  649. *************************************************************************/
  650. void hw_voInitTim5(void)
  651. {
  652. timer_parameter_struct timer_initpara;
  653. /* Timer reset */
  654. timer_deinit(TIMER5);
  655. /* TIMER5 configuration */
  656. timer_initpara.prescaler = 1;
  657. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  658. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  659. timer_initpara.period = HW_1MS_PERIOD;
  660. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  661. timer_initpara.repetitioncounter = 0;
  662. timer_init(TIMER5,&timer_initpara);
  663. /* auto-reload preload enable */
  664. timer_auto_reload_shadow_enable(TIMER5);
  665. /* TIMER5 counter enable */
  666. ////timer_enable(TIMER5);
  667. }
  668. /*************************************************************************
  669. Function:
  670. Description:
  671. Call by:
  672. Input Variables:
  673. Output/Return Variables:
  674. Subroutine Call:
  675. Reference:
  676. *************************************************************************/
  677. void hw_voInitTim6(void)
  678. {
  679. timer_parameter_struct timer_initpara;
  680. /* Timer reset */
  681. timer_deinit(TIMER6);
  682. /* configuration */
  683. timer_initpara.prescaler = 0;
  684. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  685. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  686. timer_initpara.period = (HW_PWM_PERIOD << 1) - 1;
  687. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  688. timer_initpara.repetitioncounter = 0;
  689. timer_init(TIMER6,&timer_initpara);
  690. /* auto-reload preload enable */
  691. timer_auto_reload_shadow_enable(TIMER6);
  692. /* counter enable */
  693. ////timer_enable(TIMER6);
  694. }
  695. /*************************************************************************
  696. Function:
  697. Description:
  698. Call by:
  699. Input Variables:
  700. Output/Return Variables:
  701. Subroutine Call:
  702. Reference:
  703. *************************************************************************/
  704. void hw_voInitUART3(void)
  705. {
  706. /* reset USART */
  707. usart_deinit(UART3);
  708. /* configure USART baud rate value */
  709. usart_baudrate_set(UART3, 1500000U);
  710. /* configure USART parity function */
  711. usart_parity_config(UART3, USART_PM_NONE);
  712. /* configure USART word length */
  713. usart_word_length_set(UART3, USART_WL_8BIT);
  714. /* configure USART stop bit length */
  715. usart_stop_bit_set(UART3, USART_STB_1BIT);
  716. /* configure USART transmitter */
  717. usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
  718. /* configure USART receiver */
  719. usart_receive_config(UART3, USART_RECEIVE_ENABLE);
  720. /* USART DMA enable for transmission and receive */
  721. usart_dma_transmit_config(UART3, USART_DENT_ENABLE);
  722. usart_dma_receive_config(UART3, USART_DENR_ENABLE);
  723. /* enable USART */
  724. usart_enable(UART3);
  725. }
  726. /*************************************************************************
  727. Function:
  728. Description:
  729. Call by:
  730. Input Variables:
  731. Output/Return Variables:
  732. Subroutine Call:
  733. Reference:
  734. *************************************************************************/
  735. void hw_voInitSPI2(void)
  736. {
  737. spi_parameter_struct spi_init_struct;
  738. /* reset SPI */
  739. spi_i2s_deinit(SPI2);
  740. /* SPI2 parameter config */
  741. spi_init_struct.device_mode = SPI_MASTER;
  742. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  743. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  744. spi_init_struct.nss = SPI_NSS_SOFT;
  745. spi_init_struct.endian = SPI_ENDIAN_MSB;
  746. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  747. spi_init_struct.prescale = SPI_PSC_8; // 36M/8=4.5Mhz
  748. spi_init(SPI2, &spi_init_struct);
  749. /* configure SPI CRC function */
  750. spi_crc_polynomial_set(SPI2, 7);
  751. spi_crc_on(SPI2);
  752. /* SPI DMA enable */
  753. // spi_dma_enable(SPI2, SPI_DMA_TRANSMIT);
  754. // spi_dma_enable(SPI2, SPI_DMA_RECEIVE);
  755. /* SPI enable */
  756. spi_enable(SPI2);
  757. }
  758. /*************************************************************************
  759. Function:
  760. Description:
  761. Call by:
  762. Input Variables:
  763. Output/Return Variables:
  764. Subroutine Call:
  765. Reference:
  766. *************************************************************************/
  767. void hw_voInitI2C0(void)
  768. {
  769. i2c_deinit(I2C0);
  770. /* configure I2C0 clock */
  771. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  772. /* configure I2C0 address */
  773. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_SLAVEADDR_BLOCK3);
  774. /* enable acknowledge */
  775. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  776. // /* enable I2C0 DMA */
  777. // i2c_dma_config(I2C0, I2C_DMA_ON);
  778. /* enable I2C0 */
  779. i2c_enable(I2C0);
  780. }
  781. /*************************************************************************
  782. Function:
  783. Description:
  784. Call by:
  785. Input Variables:
  786. Output/Return Variables:
  787. Subroutine Call:
  788. Reference:
  789. *************************************************************************/
  790. void hw_voInitCAN(void)
  791. {
  792. can_parameter_struct can_parameter;
  793. can_filter_parameter_struct can_filter;
  794. can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  795. can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
  796. /* initialize CAN register */
  797. can_deinit(CAN0);
  798. /* initialize CAN */
  799. can_parameter.time_triggered = DISABLE;
  800. can_parameter.auto_bus_off_recovery = ENABLE;
  801. can_parameter.auto_wake_up = DISABLE;
  802. can_parameter.auto_retrans = DISABLE;
  803. can_parameter.rec_fifo_overwrite = DISABLE;
  804. can_parameter.trans_fifo_order = DISABLE;
  805. can_parameter.working_mode = CAN_NORMAL_MODE;
  806. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  807. can_parameter.time_segment_1 = CAN_BT_BS1_4TQ;
  808. can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
  809. /* baudrate 1Mbps */
  810. can_parameter.prescaler = 24; //36M/((1+4+1)*24)=250K
  811. can_init(CAN0, &can_parameter);
  812. /* initialize filter 0 */
  813. can_filter.filter_number = 0;
  814. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  815. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  816. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000U) >> 16);
  817. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
  818. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000U) >> 16);
  819. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  820. can_filter.filter_fifo_number = CAN_FIFO0;
  821. can_filter.filter_enable = ENABLE;
  822. can_filter_init(&can_filter);
  823. /* initialize filter 1 */
  824. can_filter.filter_number = 1;
  825. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  826. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  827. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000U) >> 16);
  828. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
  829. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000U) >> 16);
  830. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  831. can_filter.filter_fifo_number = CAN_FIFO0;
  832. can_filter.filter_enable = ENABLE;
  833. can_filter_init(&can_filter);
  834. }
  835. /*************************************************************************
  836. Function:
  837. Description:
  838. Call by:
  839. Input Variables:
  840. Output/Return Variables:
  841. Subroutine Call:
  842. Reference:
  843. *************************************************************************/
  844. void hw_voInitSysTick(void)
  845. {
  846. /* Setup systick timer interrupts */
  847. /* Do not exceed 2^24-1=16777215 */
  848. SysTick_Config(SystemCoreClock / FSYSTICK_HZ);
  849. }
  850. /*************************************************************************
  851. Function:
  852. Description:
  853. Call by:
  854. Input Variables:
  855. Output/Return Variables:
  856. Subroutine Call:
  857. Reference:
  858. *************************************************************************/
  859. void hw_voInitInt(void)
  860. {
  861. /* Set the priority group */
  862. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  863. /* ADC, CMP0 and CMP1 interrupts */
  864. nvic_irq_enable((uint8_t)ADC0_1_IRQn, 0, 3);
  865. /* TIMER0 break, update, trigger and commutation interrupts */
  866. nvic_irq_enable((uint8_t)TIMER0_UP_TIMER9_IRQn, 1, 1);
  867. /* TIMER1 interrupt: CAP */
  868. nvic_irq_enable((uint8_t)TIMER1_IRQn, 2, 1);
  869. /* TIMER3 interrupt: TBS */
  870. nvic_irq_enable((uint8_t)TIMER3_IRQn, 3, 2);
  871. /* TIMER5 interrupt: 1ms */
  872. nvic_irq_enable((uint8_t)TIMER5_IRQn, 2, 3);
  873. //nvic_irq_enable(DMA0_Channel4_IRQn, 0, 1); // test
  874. /* DMA1 CH2 interrupt: Uart */
  875. nvic_irq_enable((uint8_t)DMA1_Channel2_IRQn, 3, 3);
  876. /* DMA1 CH4 interrupt: Uart */
  877. nvic_irq_enable((uint8_t)DMA1_Channel4_IRQn, 3, 3);
  878. /* CAN0 RX0 interrupt */
  879. nvic_irq_enable((uint8_t)CAN0_RX0_IRQn, 3, 3);
  880. /* CAN0 RX1 interrupt */
  881. nvic_irq_enable((uint8_t)CAN0_RX1_IRQn, 3, 3);
  882. nvic_irq_enable((uint8_t)EXTI10_15_IRQn, 3, 3);
  883. }
  884. /*************************************************************************
  885. Function:
  886. Description:
  887. Call by:
  888. Input Variables:
  889. Output/Return Variables:
  890. Subroutine Call:
  891. Reference:
  892. *************************************************************************/
  893. void hw_voEnInt(void)
  894. {
  895. /* ADC group conversion Interrupts enable */
  896. if(cp_stFlg.CurrentSampleModelSelect == SINGLERESISITANCE)
  897. {
  898. adc_interrupt_enable(ADC1 , ADC_INT_EOIC);
  899. }
  900. else if(cp_stFlg.CurrentSampleModelSelect == RDSON)
  901. {
  902. adc_interrupt_enable(ADC0 , ADC_INT_EOIC);
  903. }
  904. else if(cp_stFlg.CurrentSampleModelSelect == COMBINATION)
  905. {
  906. adc_interrupt_enable(ADC0 , ADC_INT_EOIC);
  907. adc_interrupt_enable(ADC1 , ADC_INT_EOIC);
  908. //adc_interrupt_enable(ADC1 , ADC_INT_EOC);
  909. }
  910. else
  911. {
  912. //do nothing
  913. }
  914. /* TIM0 Interrupts enable */
  915. timer_interrupt_enable(TIMER0,TIMER_INT_UP);
  916. /* TIM1 Interrupts enable */
  917. timer_interrupt_enable(TIMER1,TIMER_INT_UP);
  918. timer_interrupt_enable(TIMER1,TIMER_INT_CH2);
  919. timer_interrupt_enable(TIMER1,TIMER_INT_CH3);
  920. /* TIM3 Interrupts enable */
  921. timer_interrupt_enable(TIMER3,TIMER_INT_UP);
  922. /* TIM5 Interrupts enable */
  923. timer_interrupt_enable(TIMER5,TIMER_INT_UP);
  924. //dma_interrupt_enable(DMA0, DMA_CH4, DMA_INT_FTF);/* test */
  925. /* DMA1 channel2 full transfer finish interrupt */
  926. dma_interrupt_enable(DMA1, DMA_CH2, DMA_INT_FTF);
  927. /* DMA1 channel4 full transfer finish interrupt */
  928. dma_interrupt_enable(DMA1, DMA_CH4, DMA_INT_FTF);
  929. /* CAN0 receive FIFO0 not empty interrupt */
  930. can_interrupt_enable(CAN0, CAN_INT_RFNE0);
  931. /* CAN0 receive FIFO0 overfull interrupt */
  932. can_interrupt_enable(CAN0, CAN_INT_RFO0);
  933. /* CAN0 receive FIFO1 not empty interrupt */
  934. can_interrupt_enable(CAN0, CAN_INT_RFNE1);
  935. /* CAN0 receive FIFO1 overfull interrupt */
  936. can_interrupt_enable(CAN0, CAN_INT_RFO1);
  937. /* CAN0 error interrupt */
  938. can_interrupt_enable(CAN0, CAN_INT_ERR);
  939. /* CAN0 bus-off interrupt */
  940. can_interrupt_enable(CAN0, CAN_INT_BO);
  941. /* CAN0 warning error interrupt */
  942. can_interrupt_enable(CAN0, CAN_INT_WERR);
  943. /* CAN0 passive error interrupt */
  944. can_interrupt_enable(CAN0, CAN_INT_PERR);
  945. // CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // FIFO 0 message pending Interrupt
  946. // CAN_ITConfig(CAN1, CAN_IT_FOV0, ENABLE); // FIFO 0 overrun Interrupt
  947. // CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE); // FIFO 1 message pending Interrupt
  948. // CAN_ITConfig(CAN1, CAN_IT_FOV1, ENABLE); // FIFO 1 overrun Interrupt
  949. // //CAN_ITConfig(CAN1, CAN_IT_ERR, ENABLE); // Error Interrupt
  950. // CAN_ITConfig(CAN1, CAN_IT_LEC, ENABLE); // Last error code Interrupt
  951. // //CAN_ITConfig(CAN1, CAN_IT_BOF, ENABLE); // Bus-off Interrupt
  952. // //CAN_ITConfig(CAN1, CAN_IT_EPV, ENABLE); // Error passive Interrupt
  953. // //CAN_ITConfig(CAN1, CAN_IT_EWG, ENABLE); // Error warning Interrupt
  954. }
  955. /*************************************************************************
  956. Function:
  957. Description:
  958. Call by:
  959. Input Variables:
  960. Output/Return Variables:
  961. Subroutine Call:
  962. Reference:
  963. *************************************************************************/
  964. void hw_voTimEn(void)
  965. {
  966. /* TIMER0 counter enable */
  967. timer_enable(TIMER0);
  968. /* TIMER1 counter enable */
  969. timer_enable(TIMER1);
  970. /* TIMER2 counter enable */
  971. timer_enable(TIMER2);
  972. /* TIMER3 counter enable */
  973. timer_enable(TIMER3);
  974. /* TIMER5 counter enable */
  975. timer_enable(TIMER5);
  976. /* counter enable */
  977. timer_enable(TIMER6);
  978. }
  979. void hw_voPWMInit(void)
  980. {
  981. /* Set 50% duty */
  982. pwm_stGenOut.uwNewTIM1COMPR[0] = HW_HHPWM_PERIOD;
  983. pwm_stGenOut.uwNewTIM1COMPR[1] = HW_HHPWM_PERIOD;
  984. pwm_stGenOut.uwNewTIM1COMPR[2] = HW_HHPWM_PERIOD;
  985. pwm_stGenOut.uwNewTIM1COMPR[3] = HW_HHPWM_PERIOD;
  986. pwm_stGenOut.uwNewTIM1COMPR[4] = HW_HHPWM_PERIOD;
  987. pwm_stGenOut.uwNewTIM1COMPR[5] = HW_HHPWM_PERIOD;
  988. /* PWM off */
  989. hw_voPWMOff();
  990. /* Charge init */
  991. hw_uwChrgCt = 0;
  992. hw_blChrgOvrFlg = FALSE;
  993. }
  994. void hw_voPWMOn(void)
  995. {
  996. #if (0)
  997. timer_oc_parameter_struct timer_ocintpara;
  998. if (hw_blPWMOnFlg == FALSE)
  999. {
  1000. /* CH0, CH1 and CH2 output enable */
  1001. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  1002. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  1003. timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
  1004. timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
  1005. timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
  1006. /* Flag set */
  1007. hw_blPWMOnFlg = TRUE;
  1008. }
  1009. #endif
  1010. if (hw_blPWMOnFlg == FALSE)
  1011. {
  1012. /* CH0, CH1 set to "PWM mode 2" */
  1013. TIMER_CHCTL0(TIMER0) |= 0x7070;
  1014. /* CH2, CH3 set to "PWM mode 2" */
  1015. TIMER_CHCTL1(TIMER0) |= 0x7070;
  1016. /* CH0, CH1 and CH2, CH3 output enable */
  1017. TIMER_CHCTL2(TIMER0) |= 0x5555;
  1018. /* Flag set */
  1019. hw_blPWMOnFlg = TRUE;
  1020. }
  1021. }
  1022. void hw_voPWMOff(void)
  1023. {
  1024. #if (0)
  1025. timer_oc_parameter_struct timer_ocintpara;
  1026. if (hw_blPWMOnFlg == TRUE)
  1027. {
  1028. /* CH0, CH1 and CH2 output disable */
  1029. timer_ocintpara.outputstate = TIMER_CCX_DISABLE;
  1030. timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
  1031. timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
  1032. timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
  1033. timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
  1034. /* Flag set */
  1035. hw_blPWMOnFlg = FALSE;
  1036. }
  1037. #endif
  1038. if (hw_blPWMOnFlg == TRUE)
  1039. {
  1040. /* CH0, CH1 set to "Frozen" */
  1041. TIMER_CHCTL0(TIMER0) &= ~0x7070;
  1042. /* CH2, CH3 set to "Frozen" */
  1043. TIMER_CHCTL1(TIMER0) &= ~0x7070;
  1044. /* CH0, CH1 set to "Force inactive level" */
  1045. TIMER_CHCTL0(TIMER0) |= 0x4040;
  1046. /* CH2, CH3 set to "Force inactive level" */
  1047. TIMER_CHCTL1(TIMER0) |= 0x4040;
  1048. /* CH0, CH1 and CH2 complementary output disable */
  1049. TIMER_CHCTL2(TIMER0) &= ~0x0444;
  1050. /* Flag set */
  1051. hw_blPWMOnFlg = FALSE;
  1052. }
  1053. }
  1054. void hw_voCharge(void)
  1055. {
  1056. /* Set 50% duty */
  1057. pwm_stGenOut.uwNewTIM1COMPR[0] = HW_HHPWM_PERIOD;
  1058. pwm_stGenOut.uwNewTIM1COMPR[1] = HW_HHPWM_PERIOD;
  1059. pwm_stGenOut.uwNewTIM1COMPR[2] = HW_HHPWM_PERIOD;
  1060. pwm_stGenOut.uwNewTIM1COMPR[3] = HW_HHPWM_PERIOD;
  1061. pwm_stGenOut.uwNewTIM1COMPR[4] = HW_HHPWM_PERIOD;
  1062. pwm_stGenOut.uwNewTIM1COMPR[5] = HW_HHPWM_PERIOD;
  1063. /* PWM on */
  1064. hw_voPWMOn();
  1065. ++hw_uwChrgCt;
  1066. if (hw_uwChrgCt > cp_stControlPara.swIPMHvicChrgMs)
  1067. {
  1068. hw_uwChrgCt = 0;
  1069. hw_blChrgOvrFlg = TRUE;
  1070. }
  1071. }
  1072. /* Three phase short */
  1073. void hw_voThrPhsShrt(void)
  1074. {
  1075. /* Set 0% duty */
  1076. pwm_stGenOut.uwNewTIM1COMPR[0] = HW_HPWM_PERIOD;
  1077. pwm_stGenOut.uwNewTIM1COMPR[1] = HW_HPWM_PERIOD;
  1078. pwm_stGenOut.uwNewTIM1COMPR[2] = HW_HPWM_PERIOD;
  1079. pwm_stGenOut.uwNewTIM1COMPR[3] = HW_HPWM_PERIOD;
  1080. pwm_stGenOut.uwNewTIM1COMPR[4] = HW_HPWM_PERIOD;
  1081. pwm_stGenOut.uwNewTIM1COMPR[5] = HW_HPWM_PERIOD;
  1082. pwm_stGenOut.uwFirstTrigCOMPR = HW_HHHPWM_PERIOD;
  1083. pwm_stGenOut.uwSecondTrigCOMPR = HW_HPWM_PERIOD - HW_HHHPWM_PERIOD;
  1084. /* PWM on */
  1085. hw_voPWMOn();
  1086. }
  1087. /*************************************************************************
  1088. Function
  1089. Description: Initialize UART
  1090. Call by: ;
  1091. Input Variables: N/A
  1092. Output/Return Variables: N/A
  1093. Subroutine Call: ...;
  1094. Reference: N/A
  1095. ****************************************************************/
  1096. void hw_voIWDGInit(UWORD prer, UWORD rlr)
  1097. {
  1098. /* enable RCU_IRC40K */
  1099. rcu_osci_on(RCU_IRC40K);
  1100. /* wait till RCU_IRC40K is ready */
  1101. rcu_osci_stab_wait(RCU_IRC40K);
  1102. fwdgt_write_enable();
  1103. fwdgt_prescaler_value_config(prer);
  1104. fwdgt_reload_value_config(rlr);
  1105. fwdgt_counter_reload();
  1106. fwdgt_enable();
  1107. }
  1108. /*************************************************************************
  1109. Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  1110. All rights reserved.
  1111. *************************************************************************/
  1112. #ifdef _HWSETUP_C_
  1113. #undef _HWSETUP_C_ /* parasoft-suppress MISRA2004-19_6 "本项目中无法更改,后续避免使用" */
  1114. #endif
  1115. /************************************************************************
  1116. End of this File (EOF)!
  1117. Do not put anything after this part!
  1118. *************************************************************************/