hwsetup.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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 "hwsetup.h"
  26. #include "syspar.h"
  27. #include "user.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_15);
  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. #if GEARSENSOR_ENABLE
  210. gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_10); /* Gear sensor*/
  211. gpio_init(GPIOC,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ,GPIO_PIN_11);
  212. #else
  213. gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10); /* UART3_TX*/
  214. gpio_init(GPIOC,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_11); /* UART3_RX */
  215. #endif
  216. // gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_10); // test
  217. //
  218. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_12); /* I2C0 Write Protect*/
  219. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_13); /* F_6V_12V_Enable */
  220. gpio_init(GPIOC,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_14); /* Bike Brake */
  221. gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_15); /* Light Enable */
  222. /*=======================================================================
  223. GPIO D
  224. =======================================================================*/
  225. gpio_init(GPIOD,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_2); /* SPI Chip Select */
  226. gpio_bit_set(GPIOD, GPIO_PIN_2);
  227. #if(GIANT_ENABLE == 1)
  228. gpio_bit_set(GPIOC, GPIO_PIN_1);//Power On
  229. #endif
  230. }
  231. /*************************************************************************
  232. Function:
  233. Description:
  234. Call by:
  235. Input Variables:
  236. Output/Return Variables:
  237. Subroutine Call:
  238. Reference:E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\I2C\I2C_EEPROM_dma\at24cxx.c
  239. E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\EXTI\Key_external_interrupt_mode\gd32f30x_it.c
  240. ***************************************E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\I2C\I2C_EEPROM_dma\at24cxx.c
  241. E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\EXTI\Key_external_interrupt_mode\gd32f30x_it.c**********************************/
  242. void hw_voInitEXTI(void)
  243. {
  244. // EXTI_InitTypeDef EXTI_InitStructure;
  245. //
  246. // SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource14); // Break extern interrupt extern interrupt trigger source PIN
  247. // EXTI_InitStructure.EXTI_Line = EXTI_Line14;
  248. // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  249. // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  250. // EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  251. // EXTI_Init(&EXTI_InitStructure);
  252. }
  253. /*************************************************************************
  254. Function:
  255. Description:
  256. Call by:
  257. Input Variables:
  258. Output/Return Variables:
  259. Subroutine Call:
  260. Reference:
  261. *************************************************************************/
  262. void hw_voInitDMA(void)
  263. {
  264. dma_parameter_struct dma_init_struct;
  265. /*=======================================================================
  266. DMA0 Ch4 for Timer0
  267. =======================================================================*/
  268. dma_deinit(DMA0,DMA_CH4);
  269. dma_init_struct.periph_addr = (uint32_t)(&TIMER_DMATB(TIMER0));
  270. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  271. dma_init_struct.memory_addr = (uint32_t)(Pwms[0].ActiveCompareValues);
  272. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  273. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  274. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  275. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  276. dma_init_struct.number = TIM1_DMA_NUM;
  277. dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
  278. dma_init(DMA0,DMA_CH4,&dma_init_struct);
  279. dma_circulation_enable(DMA0,DMA_CH4);
  280. /* enable DMA0 channel4 */
  281. //dma_channel_enable(DMA0,DMA_CH4);
  282. /*=======================================================================
  283. DMA0 Ch0 for ADC0
  284. =======================================================================*/
  285. dma_deinit(DMA0, DMA_CH0);
  286. /* initialize DMA single data mode */
  287. dma_init_struct.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
  288. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  289. dma_init_struct.memory_addr = (uint32_t)(Adcs[0].Results);
  290. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  291. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  292. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  293. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  294. dma_init_struct.number = ADC0_DMA_NUM;
  295. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  296. dma_init(DMA0, DMA_CH0, &dma_init_struct);
  297. dma_circulation_enable(DMA0, DMA_CH0);
  298. /* enable DMA0 channel0 */
  299. dma_channel_enable(DMA0, DMA_CH0);
  300. /*=======================================================================
  301. DMA1 Ch4 for ADC1
  302. =======================================================================*/
  303. /////////// !!!no dma for adc1
  304. // dma_deinit(DMA1, DMA_CH4);
  305. //
  306. // /* initialize DMA single data mode */
  307. // dma_init_struct.periph_addr = (ULONG)(&ADC_RDATA(ADC1));
  308. // dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  309. // dma_init_struct.memory_addr = (ULONG)(hw_uwADC2);
  310. // dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  311. // dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  312. // dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
  313. // dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  314. // dma_init_struct.number = ADC2_DMA_NUM;
  315. // dma_init_struct.priority = DMA_PRIORITY_HIGH;
  316. // dma_init(DMA1, DMA_CH4, &dma_init_struct);
  317. //
  318. // dma_circulation_enable(DMA1, DMA_CH4);
  319. //
  320. // /* enable DMA1 channel4 */
  321. // dma_channel_enable(DMA1, DMA_CH4);
  322. /*=======================================================================
  323. DMA0 Ch5 for I2C0 transmission
  324. =======================================================================*/
  325. dma_deinit(DMA0, DMA_CH5);
  326. dma_init_struct.periph_addr = (uint32_t)(&I2C_DATA(I2C0));
  327. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  328. dma_init_struct.memory_addr = (uint32_t)(I2C_ubWriteBuffer);
  329. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  330. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  331. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  332. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  333. dma_init_struct.number = I2C1_TX_DMA_NUM;
  334. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  335. dma_init(DMA0, DMA_CH5, &dma_init_struct);
  336. dma_circulation_disable(DMA0, DMA_CH5);
  337. /* enable DMA0 channel5 */
  338. //dma_channel_enable(DMA0, DMA_CH5);
  339. /*=======================================================================
  340. DMA0 Ch6 for I2C0 receive
  341. =======================================================================*/
  342. dma_deinit(DMA0, DMA_CH6);
  343. dma_init_struct.periph_addr = (uint32_t)(&I2C_DATA(I2C0));
  344. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  345. dma_init_struct.memory_addr = (uint32_t)(I2C_ubReadBuffer[0]);
  346. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  347. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  348. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  349. dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  350. dma_init_struct.number = I2C1_RX_DMA_NUM;
  351. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  352. dma_init(DMA0, DMA_CH6, &dma_init_struct);
  353. dma_circulation_disable(DMA0, DMA_CH6);
  354. /* enable DMA0 channel6 */
  355. //dma_channel_enable(DMA0, DMA_CH6);
  356. /*=======================================================================
  357. DMA1 Ch4 for UART3 transmission
  358. =======================================================================*/
  359. dma_deinit(DMA1, DMA_CH4);
  360. dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
  361. dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  362. dma_init_struct.memory_addr = (uint32_t)(0x20000000);
  363. dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  364. dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  365. dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  366. dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
  367. dma_init_struct.number = 64;
  368. dma_init_struct.priority = DMA_PRIORITY_HIGH;
  369. dma_init(DMA1, DMA_CH4, &dma_init_struct);
  370. dma_circulation_disable(DMA1, DMA_CH4);
  371. /* enable DMA1 channel4 */
  372. //dma_channel_enable(DMA1, DMA_CH4);
  373. // /*=======================================================================
  374. // DMA1 Ch2 for UART3 receive
  375. // =======================================================================*/
  376. // dma_deinit(DMA1, DMA_CH2);
  377. //
  378. // dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
  379. // dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  380. // dma_init_struct.memory_addr = (uint32_t)(UART_ubReadBuffer);
  381. // dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
  382. // dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
  383. // dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
  384. // dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
  385. // dma_init_struct.number = 22;
  386. // dma_init_struct.priority = DMA_PRIORITY_HIGH;
  387. // dma_init(DMA1, DMA_CH2, &dma_init_struct);
  388. //
  389. // dma_circulation_disable(DMA1, DMA_CH2);
  390. //
  391. // /* enable DMA1 channel2 */
  392. // dma_channel_enable(DMA1, DMA_CH2);
  393. }
  394. /*************************************************************************
  395. Function:
  396. Description:
  397. Call by:
  398. Input Variables:
  399. Output/Return Variables:
  400. Subroutine Call:
  401. Reference:
  402. *************************************************************************/
  403. void hw_voInitADC(void)
  404. {
  405. /*=======================================================================
  406. ADC0
  407. =======================================================================*/
  408. /* ADC reset */
  409. adc_deinit(ADC0);
  410. /* ADC mode config */
  411. adc_mode_config(ADC_MODE_FREE);
  412. /* ADC special function config */
  413. adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
  414. adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
  415. /* ADC data alignment config */
  416. adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
  417. /* ADC channel length config */
  418. adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 8);
  419. adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 3);
  420. /* ADC regular channel config */
  421. adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_5, ADC_SAMPLETIME_7POINT5); //PA5 Ubus
  422. adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_6, ADC_SAMPLETIME_7POINT5); //PA6 U_6V
  423. adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_7, ADC_SAMPLETIME_7POINT5); //PA7 U_5V
  424. adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_14, ADC_SAMPLETIME_7POINT5); //PC4 PCB_Temp
  425. adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_15, ADC_SAMPLETIME_7POINT5); //PC5 Motor_Temp
  426. adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_12, ADC_SAMPLETIME_7POINT5); //PC2 U_12V
  427. adc_regular_channel_config(ADC0, 6, ADC_CHANNEL_13, ADC_SAMPLETIME_7POINT5); //PC3 Throtttle
  428. adc_regular_channel_config(ADC0, 7, ADC_CHANNEL_3, ADC_SAMPLETIME_7POINT5); //PA3 Torque
  429. /* 8· = 0.55*8 = 4.44us */
  430. /* ADC inserted channel config */
  431. adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_7POINT5); //PA0 Iu
  432. adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_7POINT5); //PA1 Iv
  433. adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_2, ADC_SAMPLETIME_7POINT5); //PA2 Iw
  434. /* ADC trigger config */
  435. adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
  436. adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);
  437. /* ADC external trigger enable */
  438. adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
  439. adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
  440. /* ADC DMA function enable */
  441. adc_dma_mode_enable(ADC0);
  442. /* enable ADC interface */
  443. adc_enable(ADC0);
  444. /* ADC calibration and reset calibration */
  445. adc_calibration_enable(ADC0);
  446. /* ADC software trigger enable */
  447. adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
  448. /*=======================================================================
  449. ADC1
  450. =======================================================================*/
  451. /* ADC reset */
  452. adc_deinit(ADC1);
  453. /* ADC special function config */
  454. adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);
  455. adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, DISABLE);
  456. /* ADC data alignment config */
  457. adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
  458. /* ADC channel length config */
  459. adc_channel_length_config(ADC1, ADC_INSERTED_CHANNEL, 1);
  460. /* ADC inserted channel config */
  461. adc_inserted_channel_config(ADC1, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_7POINT5); //PA4 Ibus
  462. /* ADC trigger config */
  463. adc_external_trigger_source_config(ADC1, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);
  464. /* ADC external trigger enable */
  465. adc_external_trigger_config(ADC1, ADC_INSERTED_CHANNEL, ENABLE);
  466. /* enable ADC interface */
  467. adc_enable(ADC1);
  468. /* ADC calibration and reset calibration */
  469. adc_calibration_enable(ADC1);
  470. }
  471. /*************************************************************************
  472. Function:
  473. Description:
  474. Call by:
  475. Input Variables:
  476. Output/Return Variables:
  477. Subroutine Call:
  478. Reference:
  479. *************************************************************************/
  480. void hw_voInitTim0(void)
  481. {
  482. timer_oc_parameter_struct timer_ocintpara;
  483. timer_parameter_struct timer_initpara;
  484. timer_break_parameter_struct timer_breakpara;
  485. /* Timer reset */
  486. timer_deinit(TIMER0);
  487. timer_struct_para_init(&timer_initpara); // must init, or timer_initpara.counterdirection will be arbitrary
  488. /* TIMER0 configuration */
  489. timer_initpara.prescaler = 0;
  490. timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
  491. timer_initpara.period = HW_HPWM_PERIOD;
  492. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  493. timer_initpara.repetitioncounter = 0;
  494. timer_init(TIMER0,&timer_initpara);
  495. /* CH0, CH1 and CH2 configuration as PWM complementary output */
  496. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  497. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  498. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  499. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  500. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  501. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  502. timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
  503. timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
  504. timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
  505. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, HW_HHPWM_PERIOD);
  506. timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM1);
  507. timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
  508. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, HW_HHPWM_PERIOD);
  509. timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM1);
  510. timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
  511. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, HW_HHPWM_PERIOD);
  512. timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM1);
  513. timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
  514. /* CH3 configuration as ADC trigger signal */
  515. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  516. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  517. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  518. timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocintpara);
  519. timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 200);
  520. timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM1);
  521. //timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
  522. timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_ENABLE);
  523. /* automatic output enable, break, dead time and lock configuration*/
  524. timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
  525. timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
  526. timer_breakpara.deadtime = 72;//cp_stControlPara.swIPMDeadTimeNs / 100 * TIM0CLK_KHZ / 1000 / 10;;
  527. timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
  528. timer_breakpara.outputautostate = TIMER_OUTAUTO_DISABLE;
  529. timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
  530. timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
  531. timer_break_config(TIMER0,&timer_breakpara);
  532. /* TIMER0 primary output enable */
  533. timer_primary_output_config(TIMER0,ENABLE);
  534. /* TIMER0 update DMA request enable */
  535. timer_dma_transfer_config(TIMER0,TIMER_DMACFG_DMATA_CH0CV,TIMER_DMACFG_DMATC_3TRANSFER);
  536. //timer_dma_enable(TIMER0,TIMER_DMA_UPD);
  537. /* auto-reload preload enable */
  538. timer_auto_reload_shadow_enable(TIMER0);
  539. /* select the master slave mode */
  540. timer_master_slave_mode_config(TIMER0, TIMER_MASTER_SLAVE_MODE_ENABLE);
  541. /* Update event is used as trigger output */
  542. timer_master_output_trigger_source_select(TIMER0, TIMER_TRI_OUT_SRC_UPDATE);
  543. /* TIMER0 counter enable */
  544. ////timer_enable(TIMER0);
  545. }
  546. /*************************************************************************
  547. Function:
  548. Description:
  549. Call by:
  550. Input Variables:
  551. Output/Return Variables:
  552. Subroutine Call:
  553. Reference:
  554. *************************************************************************/
  555. void hw_voInitTim1(void)
  556. {
  557. timer_ic_parameter_struct timer_icinitpara;
  558. timer_parameter_struct timer_initpara;
  559. timer_deinit(TIMER1);
  560. /* TIMER1 configuration */
  561. timer_initpara.prescaler = 99;
  562. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  563. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  564. timer_initpara.period = TIM1CLK_KHZ * 1000 / (1000 / 25); // period = 25ms
  565. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  566. timer_initpara.repetitioncounter = 0;
  567. timer_init(TIMER1,&timer_initpara);
  568. /* TIMER1 CH0 input capture configuration */
  569. timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
  570. timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
  571. timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
  572. timer_icinitpara.icfilter = 0x0;
  573. timer_input_capture_config(TIMER1,TIMER_CH_2,&timer_icinitpara);
  574. timer_input_capture_config(TIMER1,TIMER_CH_3,&timer_icinitpara);
  575. /* auto-reload preload enable */
  576. timer_auto_reload_shadow_enable(TIMER1);
  577. /* TIMER1 counter enable */
  578. ////timer_enable(TIMER1);
  579. }
  580. /*************************************************************************
  581. Function:
  582. Description:
  583. Call by:
  584. Input Variables:
  585. Output/Return Variables:
  586. Subroutine Call:
  587. Reference:
  588. *************************************************************************/
  589. void hw_voInitTim2(void)
  590. {
  591. timer_oc_parameter_struct timer_ocintpara;
  592. timer_parameter_struct timer_initpara;
  593. /* Timer reset */
  594. timer_deinit(TIMER2);
  595. /* TIMER2 configuration */
  596. timer_initpara.prescaler = 0;
  597. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  598. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  599. timer_initpara.period = TIM2CLK_KHZ / 70; // 70kHz for torque sensor
  600. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  601. timer_initpara.repetitioncounter = 0;
  602. timer_init(TIMER2,&timer_initpara);
  603. /* CH3 configuration for 70kHz output */
  604. timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
  605. timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
  606. timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
  607. timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  608. timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  609. timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  610. timer_channel_output_config(TIMER2, TIMER_CH_3, &timer_ocintpara);
  611. timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, (1028>>1));
  612. timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM1);
  613. timer_channel_output_shadow_config(TIMER2, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
  614. /* TIMER2 primary output enable */
  615. timer_primary_output_config(TIMER2,ENABLE);
  616. /* auto-reload preload enable */
  617. timer_auto_reload_shadow_enable(TIMER2);
  618. /* TIMER2 counter enable */
  619. ////timer_enable(TIMER2);
  620. }
  621. /*************************************************************************
  622. Function:
  623. Description:
  624. Call by:
  625. Input Variables:
  626. Output/Return Variables:
  627. Subroutine Call:
  628. Reference:
  629. *************************************************************************/
  630. void hw_voInitTim3(void)
  631. {
  632. timer_parameter_struct timer_initpara;
  633. timer_deinit(TIMER3);
  634. /* TIMER configuration */
  635. timer_initpara.prescaler = 0;
  636. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  637. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  638. timer_initpara.period = (FTBC_HZ<<2)/FTBS_HZ-1;
  639. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  640. timer_initpara.repetitioncounter = 0;
  641. timer_init(TIMER3,&timer_initpara);
  642. /* auto-reload preload enable */
  643. timer_auto_reload_shadow_enable(TIMER3);
  644. /* slave mode selection: TIMER3 */
  645. timer_slave_mode_select(TIMER3, TIMER_SLAVE_MODE_EXTERNAL0);
  646. timer_input_trigger_source_select(TIMER3, TIMER_SMCFG_TRGSEL_ITI0);
  647. /* select the master slave mode */
  648. timer_master_slave_mode_config(TIMER3, TIMER_MASTER_SLAVE_MODE_ENABLE);
  649. /* TIMER3 counter enable */
  650. ////timer_enable(TIMER3);
  651. }
  652. /*************************************************************************
  653. Function:
  654. Description:
  655. Call by:
  656. Input Variables:
  657. Output/Return Variables:
  658. Subroutine Call:
  659. Reference:
  660. *************************************************************************/
  661. void hw_voInitTim5(void)
  662. {
  663. timer_parameter_struct timer_initpara;
  664. /* Timer reset */
  665. timer_deinit(TIMER5);
  666. /* TIMER5 configuration */
  667. timer_initpara.prescaler = 1;
  668. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  669. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  670. timer_initpara.period = HW_1MS_PERIOD;
  671. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  672. timer_initpara.repetitioncounter = 0;
  673. timer_init(TIMER5,&timer_initpara);
  674. /* auto-reload preload enable */
  675. timer_auto_reload_shadow_enable(TIMER5);
  676. /* TIMER5 counter enable */
  677. ////timer_enable(TIMER5);
  678. }
  679. /*************************************************************************
  680. Function:
  681. Description:
  682. Call by:
  683. Input Variables:
  684. Output/Return Variables:
  685. Subroutine Call:
  686. Reference:
  687. *************************************************************************/
  688. void hw_voInitTim6(void)
  689. {
  690. timer_parameter_struct timer_initpara;
  691. /* Timer reset */
  692. timer_deinit(TIMER6);
  693. /* configuration */
  694. timer_initpara.prescaler = 0;
  695. timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
  696. timer_initpara.counterdirection = TIMER_COUNTER_UP;
  697. timer_initpara.period = (HW_PWM_PERIOD << 1) - 1;
  698. timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
  699. timer_initpara.repetitioncounter = 0;
  700. timer_init(TIMER6,&timer_initpara);
  701. /* auto-reload preload enable */
  702. timer_auto_reload_shadow_enable(TIMER6);
  703. /* counter enable */
  704. ////timer_enable(TIMER6);
  705. }
  706. /*************************************************************************
  707. Function:
  708. Description:
  709. Call by:
  710. Input Variables:
  711. Output/Return Variables:
  712. Subroutine Call:
  713. Reference:
  714. *************************************************************************/
  715. void hw_voInitUART3(void)
  716. {
  717. /* reset USART */
  718. usart_deinit(UART3);
  719. /* configure USART baud rate value */
  720. usart_baudrate_set(UART3, 1500000U);
  721. /* configure USART parity function */
  722. usart_parity_config(UART3, USART_PM_NONE);
  723. /* configure USART word length */
  724. usart_word_length_set(UART3, USART_WL_8BIT);
  725. /* configure USART stop bit length */
  726. usart_stop_bit_set(UART3, USART_STB_1BIT);
  727. /* configure USART transmitter */
  728. usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
  729. /* configure USART receiver */
  730. usart_receive_config(UART3, USART_RECEIVE_ENABLE);
  731. /* USART DMA enable for transmission and receive */
  732. usart_dma_transmit_config(UART3, USART_DENT_ENABLE);
  733. usart_dma_receive_config(UART3, USART_DENR_ENABLE);
  734. /* enable USART */
  735. usart_enable(UART3);
  736. }
  737. /*************************************************************************
  738. Function:
  739. Description:
  740. Call by:
  741. Input Variables:
  742. Output/Return Variables:
  743. Subroutine Call:
  744. Reference:
  745. *************************************************************************/
  746. void hw_voInitSPI2(void)
  747. {
  748. spi_parameter_struct spi_init_struct;
  749. /* reset SPI */
  750. spi_i2s_deinit(SPI2);
  751. /* SPI2 parameter config */
  752. spi_init_struct.device_mode = SPI_MASTER;
  753. spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
  754. spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
  755. spi_init_struct.nss = SPI_NSS_SOFT;
  756. spi_init_struct.endian = SPI_ENDIAN_MSB;
  757. spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
  758. spi_init_struct.prescale = SPI_PSC_8; // 36M/8=4.5Mhz
  759. spi_init(SPI2, &spi_init_struct);
  760. /* configure SPI CRC function */
  761. spi_crc_polynomial_set(SPI2, 7);
  762. spi_crc_on(SPI2);
  763. /* SPI DMA enable */
  764. // spi_dma_enable(SPI2, SPI_DMA_TRANSMIT);
  765. // spi_dma_enable(SPI2, SPI_DMA_RECEIVE);
  766. /* SPI enable */
  767. spi_enable(SPI2);
  768. }
  769. /*************************************************************************
  770. Function:
  771. Description:
  772. Call by:
  773. Input Variables:
  774. Output/Return Variables:
  775. Subroutine Call:
  776. Reference:
  777. *************************************************************************/
  778. void hw_voInitI2C0(void)
  779. {
  780. i2c_deinit(I2C0);
  781. /* configure I2C0 clock */
  782. i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
  783. /* configure I2C0 address */
  784. i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_SLAVEADDR_BLOCK3);
  785. /* enable acknowledge */
  786. i2c_ack_config(I2C0, I2C_ACK_ENABLE);
  787. // /* enable I2C0 DMA */
  788. // i2c_dma_config(I2C0, I2C_DMA_ON);
  789. /* enable I2C0 */
  790. i2c_enable(I2C0);
  791. }
  792. /*************************************************************************
  793. Function:
  794. Description:
  795. Call by:
  796. Input Variables:
  797. Output/Return Variables:
  798. Subroutine Call:
  799. Reference:
  800. *************************************************************************/
  801. void hw_voInitCAN(void)
  802. {
  803. can_parameter_struct can_parameter;
  804. can_filter_parameter_struct can_filter;
  805. can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
  806. can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
  807. /* initialize CAN register */
  808. can_deinit(CAN0);
  809. /* initialize CAN */
  810. can_parameter.time_triggered = DISABLE;
  811. can_parameter.auto_bus_off_recovery = ENABLE;
  812. can_parameter.auto_wake_up = DISABLE;
  813. can_parameter.auto_retrans = ENABLE;
  814. can_parameter.rec_fifo_overwrite = DISABLE;
  815. can_parameter.trans_fifo_order = DISABLE;
  816. can_parameter.working_mode = CAN_NORMAL_MODE;
  817. can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
  818. can_parameter.time_segment_1 = CAN_BT_BS1_4TQ;
  819. can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
  820. /* baudrate 1Mbps */
  821. #if(GIANT_ENABLE == 1)
  822. can_parameter.prescaler = 12; //36M/((1+4+1)*12)=500K
  823. can_init(CAN0, &can_parameter);
  824. /* initialize filter 0 */
  825. can_filter.filter_number = 0;
  826. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  827. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  828. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000U) >> 16);
  829. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
  830. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000U) >> 16);
  831. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  832. can_filter.filter_fifo_number = CAN_FIFO0;
  833. can_filter.filter_enable = ENABLE;
  834. can_filter_init(&can_filter);
  835. /* initialize filter 1 */
  836. can_filter.filter_number = 1;
  837. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  838. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  839. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000U) >> 16);
  840. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
  841. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000U) >> 16);
  842. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  843. can_filter.filter_fifo_number = CAN_FIFO1;
  844. can_filter.filter_enable = ENABLE;
  845. can_filter_init(&can_filter);
  846. /* initialize filter 2 */
  847. //配置过滤器2,Enviolo自动变速器
  848. can_filter.filter_number = 2;
  849. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  850. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  851. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_ENVIOLO_FILTER << 21) & 0xFFFF0000U) >> 16);// ((ulExtId<<3)>>16)&0xFFFF;
  852. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_ENVIOLO_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;//((ulExtId<<3)&0xFFFF)|CAN_ID_EXT|CAN_RTR_Data;
  853. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_ENVIOLO_MASK << 21) & 0xFFFF0000U) >> 16); //((ulFilter<<3)>>16)&0xFFFF;
  854. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_ENVIOLO_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  855. can_filter.filter_fifo_number = CAN_FIFO1;//过滤器2关联到FIFO1
  856. can_filter.filter_enable = ENABLE;//激活过滤器0
  857. can_filter_init(&can_filter);//滤波器初始化
  858. /* initialize filter 3 */
  859. can_filter.filter_number = 3;
  860. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  861. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  862. can_filter.filter_list_high = 0x0000;
  863. can_filter.filter_list_low = 0x0000;
  864. can_filter.filter_mask_high = 0x0000;
  865. can_filter.filter_mask_low = 0x0000;
  866. can_filter.filter_fifo_number = CAN_FIFO1;//过滤器2关联到FIFO1
  867. can_filter.filter_enable = ENABLE;//激活过滤器0
  868. can_filter_init(&can_filter);//滤波器初始化
  869. #else
  870. can_parameter.prescaler = 24; //36M/((1+4+1)*24)=250K
  871. can_init(CAN0, &can_parameter);
  872. /* initialize filter 0 */
  873. can_filter.filter_number = 0;
  874. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  875. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  876. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000U) >> 16);
  877. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
  878. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000U) >> 16);
  879. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  880. can_filter.filter_fifo_number = CAN_FIFO0;
  881. can_filter.filter_enable = ENABLE;
  882. can_filter_init(&can_filter);
  883. /* initialize filter 1 */
  884. can_filter.filter_number = 1;
  885. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  886. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  887. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000U) >> 16);
  888. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
  889. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000U) >> 16);
  890. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  891. can_filter.filter_fifo_number = CAN_FIFO0;
  892. can_filter.filter_enable = ENABLE;
  893. can_filter_init(&can_filter);
  894. /* initialize filter 2 */
  895. //配置过滤器2,Enviolo自动变速器
  896. can_filter.filter_number = 2;
  897. can_filter.filter_mode = CAN_FILTERMODE_MASK;
  898. can_filter.filter_bits = CAN_FILTERBITS_32BIT;
  899. can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_ENVIOLO_FILTER << 21) & 0xFFFF0000U) >> 16);// ((ulExtId<<3)>>16)&0xFFFF;
  900. can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_ENVIOLO_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;//((ulExtId<<3)&0xFFFF)|CAN_ID_EXT|CAN_RTR_Data;
  901. can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_ENVIOLO_MASK << 21) & 0xFFFF0000U) >> 16); //((ulFilter<<3)>>16)&0xFFFF;
  902. can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_ENVIOLO_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
  903. can_filter.filter_fifo_number = CAN_FIFO1;//过滤器2关联到FIFO1
  904. can_filter.filter_enable = ENABLE;//激活过滤器0
  905. can_filter_init(&can_filter);//滤波器初始化
  906. #endif
  907. }
  908. /*************************************************************************
  909. Function:
  910. Description:
  911. Call by:
  912. Input Variables:
  913. Output/Return Variables:
  914. Subroutine Call:
  915. Reference:
  916. *************************************************************************/
  917. void hw_voInitSysTick(void)
  918. {
  919. /* Setup systick timer interrupts */
  920. /* Do not exceed 2^24-1=16777215 */
  921. SysTick_Config(SystemCoreClock / FSYSTICK_HZ);
  922. }
  923. /*************************************************************************
  924. Function:
  925. Description:
  926. Call by:
  927. Input Variables:
  928. Output/Return Variables:
  929. Subroutine Call:
  930. Reference:
  931. *************************************************************************/
  932. void hw_voInitInt(void)
  933. {
  934. /* Set the priority group */
  935. nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
  936. /* ADC, CMP0 and CMP1 interrupts */
  937. nvic_irq_enable((uint8_t)ADC0_1_IRQn, 0, 3);
  938. /* TIMER0 break, update, trigger and commutation interrupts */
  939. nvic_irq_enable((uint8_t)TIMER0_UP_TIMER9_IRQn, 1, 1);
  940. /* TIMER1 interrupt: CAP */
  941. nvic_irq_enable((uint8_t)TIMER1_IRQn, 2, 1);
  942. /* TIMER3 interrupt: TBS */
  943. nvic_irq_enable((uint8_t)TIMER3_IRQn, 3, 2);
  944. /* TIMER5 interrupt: 1ms */
  945. nvic_irq_enable((uint8_t)TIMER5_IRQn, 2, 3);
  946. //nvic_irq_enable(DMA0_Channel4_IRQn, 0, 1); // test
  947. /* DMA1 CH2 interrupt: Uart */
  948. nvic_irq_enable((uint8_t)DMA1_Channel2_IRQn, 3, 3);
  949. /* DMA1 CH4 interrupt: Uart */
  950. nvic_irq_enable((uint8_t)DMA1_Channel4_IRQn, 3, 3);
  951. /* CAN0 RX0 interrupt */
  952. nvic_irq_enable((uint8_t)CAN0_RX0_IRQn, 3, 3);
  953. /* CAN0 RX1 interrupt */
  954. nvic_irq_enable((uint8_t)CAN0_RX1_IRQn, 3, 3);
  955. nvic_irq_enable((uint8_t)EXTI10_15_IRQn, 3, 3);
  956. }
  957. /*************************************************************************
  958. Function:
  959. Description:
  960. Call by:
  961. Input Variables:
  962. Output/Return Variables:
  963. Subroutine Call:
  964. Reference:
  965. *************************************************************************/
  966. void hw_voEnInt(void)
  967. {
  968. /* ADC group conversion Interrupts enable */
  969. if(cp_stFlg.CurrentSampleModelSelect == COMBINATION)
  970. {
  971. adc_interrupt_enable(ADC0 , ADC_INT_EOIC);
  972. adc_interrupt_enable(ADC1 , ADC_INT_EOIC);
  973. //adc_interrupt_enable(ADC1 , ADC_INT_EOC);
  974. }
  975. else
  976. {
  977. //do nothing
  978. }
  979. /* TIM0 Interrupts enable */
  980. timer_interrupt_enable(TIMER0,TIMER_INT_UP);
  981. /* TIM1 Interrupts enable */
  982. timer_interrupt_enable(TIMER1,TIMER_INT_UP);
  983. timer_interrupt_enable(TIMER1,TIMER_INT_CH2);
  984. timer_interrupt_enable(TIMER1,TIMER_INT_CH3);
  985. /* TIM3 Interrupts enable */
  986. timer_interrupt_enable(TIMER3,TIMER_INT_UP);
  987. /* TIM5 Interrupts enable */
  988. timer_interrupt_enable(TIMER5,TIMER_INT_UP);
  989. //dma_interrupt_enable(DMA0, DMA_CH4, DMA_INT_FTF);/* test */
  990. /* DMA1 channel2 full transfer finish interrupt */
  991. dma_interrupt_enable(DMA1, DMA_CH2, DMA_INT_FTF);
  992. /* DMA1 channel4 full transfer finish interrupt */
  993. dma_interrupt_enable(DMA1, DMA_CH4, DMA_INT_FTF);
  994. /* CAN0 receive FIFO0 not empty interrupt */
  995. can_interrupt_enable(CAN0, CAN_INT_RFNE0);
  996. /* CAN0 receive FIFO0 overfull interrupt */
  997. can_interrupt_enable(CAN0, CAN_INT_RFO0);
  998. /* CAN0 receive FIFO1 not empty interrupt */
  999. can_interrupt_enable(CAN0, CAN_INT_RFNE1);
  1000. /* CAN0 receive FIFO1 overfull interrupt */
  1001. can_interrupt_enable(CAN0, CAN_INT_RFO1);
  1002. /* CAN0 error interrupt */
  1003. can_interrupt_enable(CAN0, CAN_INT_ERR);
  1004. /* CAN0 bus-off interrupt */
  1005. can_interrupt_enable(CAN0, CAN_INT_BO);
  1006. /* CAN0 warning error interrupt */
  1007. can_interrupt_enable(CAN0, CAN_INT_WERR);
  1008. /* CAN0 passive error interrupt */
  1009. can_interrupt_enable(CAN0, CAN_INT_PERR);
  1010. // CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // FIFO 0 message pending Interrupt
  1011. // CAN_ITConfig(CAN1, CAN_IT_FOV0, ENABLE); // FIFO 0 overrun Interrupt
  1012. // CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE); // FIFO 1 message pending Interrupt
  1013. // CAN_ITConfig(CAN1, CAN_IT_FOV1, ENABLE); // FIFO 1 overrun Interrupt
  1014. // //CAN_ITConfig(CAN1, CAN_IT_ERR, ENABLE); // Error Interrupt
  1015. // CAN_ITConfig(CAN1, CAN_IT_LEC, ENABLE); // Last error code Interrupt
  1016. // //CAN_ITConfig(CAN1, CAN_IT_BOF, ENABLE); // Bus-off Interrupt
  1017. // //CAN_ITConfig(CAN1, CAN_IT_EPV, ENABLE); // Error passive Interrupt
  1018. // //CAN_ITConfig(CAN1, CAN_IT_EWG, ENABLE); // Error warning Interrupt
  1019. }
  1020. /*************************************************************************
  1021. Function:
  1022. Description:
  1023. Call by:
  1024. Input Variables:
  1025. Output/Return Variables:
  1026. Subroutine Call:
  1027. Reference:
  1028. *************************************************************************/
  1029. void hw_voTimEn(void)
  1030. {
  1031. /* TIMER0 counter enable */
  1032. timer_enable(TIMER0);
  1033. /* TIMER1 counter enable */
  1034. timer_enable(TIMER1);
  1035. /* TIMER2 counter enable */
  1036. timer_enable(TIMER2);
  1037. /* TIMER3 counter enable */
  1038. timer_enable(TIMER3);
  1039. /* TIMER5 counter enable */
  1040. timer_enable(TIMER5);
  1041. /* counter enable */
  1042. timer_enable(TIMER6);
  1043. }
  1044. /*************************************************************************
  1045. Function
  1046. Description: Initialize UART
  1047. Call by: ;
  1048. Input Variables: N/A
  1049. Output/Return Variables: N/A
  1050. Subroutine Call: ...;
  1051. Reference: N/A
  1052. ****************************************************************/
  1053. void hw_voIWDGInit(UWORD prer, UWORD rlr)
  1054. {
  1055. /* enable RCU_IRC40K */
  1056. rcu_osci_on(RCU_IRC40K);
  1057. /* wait till RCU_IRC40K is ready */
  1058. rcu_osci_stab_wait(RCU_IRC40K);
  1059. fwdgt_write_enable();
  1060. fwdgt_prescaler_value_config(prer);
  1061. fwdgt_reload_value_config(rlr);
  1062. fwdgt_counter_reload();
  1063. fwdgt_enable();
  1064. }
  1065. /*************************************************************************
  1066. Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
  1067. All rights reserved.
  1068. *************************************************************************/
  1069. #ifdef _HWSETUP_C_
  1070. #undef _HWSETUP_C_ /* parasoft-suppress MISRA2004-19_6 "本项目中无法更改,后续避免使用" */
  1071. #endif
  1072. /************************************************************************
  1073. End of this File (EOF)!
  1074. Do not put anything after this part!
  1075. *************************************************************************/