hwsetup.c 46 KB

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