12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376 |
- /************************************************************************
- Project: Welling Motor Control Paltform
- Filename: hwsetup.c
- Partner Filename: hwsetup.h
- Description: Hardware setup
- Complier: IAR Embedded Workbench for ARM 8.40.2
- CPU TYPE : GD32F30x
- *************************************************************************
- Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
- All rights reserved.
- *************************************************************************
- *************************************************************************
- Revising History (ECL of this file):
- ************************************************************************/
- /************************************************************************
- Beginning of File, do not put anything above here except notes
- Compiler Directives:
- *************************************************************************/
- #ifndef _HWSETUP_C_
- #define _HWSETUP_C_
- #endif
- /************************************************************************
- Included File
- *************************************************************************/
- #include "syspar.h"
- #include "user.h"
- #include "can.h"
- #include "api_rt.h"
- /************************************************************************
- Constant Table (N/A)
- *************************************************************************/
- /*************************************************************************
- Exported Functions:
- *************************************************************************/
- /*************************************************************************
- Function: hw_voHardwareSetup;
- Description: Hardware Setup function.
- Call by: main();
- Input Variables: All HW registers
- Output/Return Variables: All HW registers
- Subroutine Call: ...;
- Reference: N/A
- *************************************************************************/
- void hw_voHardwareSetup1(void)
- {
- // Initialize System Control registers, WatchDog to a know state
- hw_voInitSysCtrl();
- // Select GPIO for the device or for the specific application:
- hw_voInitGPIO();
- }
- void hw_voHardwareSetup2(void)
- {
- // Initialize all the Device Peripherals to a known state:
- hw_voInitPeri();
- // Initialize interrupt priority of peripherals:
- hw_voInitInt();
- }
- /*************************************************************************
- Local Functions (N/A)
- *************************************************************************/
- /*************************************************************************
- Function: hw_voInitPWM;
- Description: MTU Setup function.
- Call by: main();
- Input Variables: MTU HW registers
- Output/Return Variables: MTU HW registers
- Subroutine Call: ...;
- Reference: N/A
- *************************************************************************/
- void hw_voInitPeri(void)
- {
- // Initialize Exti
- hw_voInitEXTI();
-
- // Initialize DMA
- hw_voInitDMA();
- // Initialize ADC
- hw_voInitADC();
- // Initialize Timer0
- hw_voInitTim0();
- // Initialize Timer1
- hw_voInitTim1();
- // Initialize Timer3
- hw_voInitTim2();
- // Initialize Timer4
- hw_voInitTim3();
- // Initialize Timer6
- hw_voInitTim5();
- // Initialize Timer16
- hw_voInitTim6();
- // Initialize USART
- hw_voInitUART3();
- // Initialize SPI2
- hw_voInitSPI2();
- // Initialize I2C1
- hw_voInitI2C0();
- // Initialize SysTick
- hw_voInitSysTick();
- // Initialize CAN
- hw_voInitCAN();
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitSysCtrl(void)
- {
- /* Enable the DMA0 clock */
- rcu_periph_clock_enable(RCU_DMA0);
- /* Enable the DMA1 clock */
- rcu_periph_clock_enable(RCU_DMA1);
- /* Enable the Alternate Function clock */
- rcu_periph_clock_enable(RCU_AF);
-
- /* Enable the GPIOA clock */
- rcu_periph_clock_enable(RCU_GPIOA);
- /* Enable the GPIOB clock */
- rcu_periph_clock_enable(RCU_GPIOB);
- /* Enable the GPIOC clock */
- rcu_periph_clock_enable(RCU_GPIOC);
- /* Enable the GPIOD clock */
- rcu_periph_clock_enable(RCU_GPIOD);
- /* Enable the GPIOF clock */
- rcu_periph_clock_enable(RCU_GPIOF);
- /* Enable the ADC0 ADC1 clock */
- rcu_periph_clock_enable(RCU_ADC0);
- rcu_periph_clock_enable(RCU_ADC1);
- rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV2);
-
- /* Enable the TIM1 clock */
- rcu_periph_clock_enable(RCU_TIMER0);
- /* Enable the TIM2 clock */
- rcu_periph_clock_enable(RCU_TIMER1);
- /* Enable the TIM3 clock */
- rcu_periph_clock_enable(RCU_TIMER2);
- /* Enable the TIM4 clock */
- rcu_periph_clock_enable(RCU_TIMER3);
- /* Enable the TIM6 clock */
- rcu_periph_clock_enable(RCU_TIMER5);
-
- /* Enable the TIM16 clock */
- rcu_periph_clock_enable(RCU_TIMER6);
- /* Enable the UART3 clock */
- rcu_periph_clock_enable(RCU_UART3);
- /* Enable the SPI2 clock */
- rcu_periph_clock_enable(RCU_SPI2);
- /* Enable the I2C0 clock */
- rcu_periph_clock_enable(RCU_I2C0);
- /* Enable the CAN clock */
- rcu_periph_clock_enable(RCU_CAN0);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitGPIO(void)
- {
- /*=======================================================================
- GPIO A
- =======================================================================*/
- 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*/
- 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*/
- 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 */
-
- gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_11); /* TIM0_CH3: ADC Trigger */
-
- //gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_11);
- gpio_init(GPIOA,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_12); /* CAN STB */
- gpio_bit_reset(GPIOA, GPIO_PIN_12);
-
- /* Dont config PA13, for it is used for SWDIO FOR DEBUG*/
- /* Dont config PA14, for it is used for SWCLK FOR DEBUG*/
- gpio_init(GPIOA,GPIO_MODE_IPU,GPIO_OSPEED_50MHZ, GPIO_PIN_15); /* ASSIST STATE IN */
- gpio_bit_reset(GPIOA, GPIO_PIN_12);
-
- /*=======================================================================
- GPIO B
- =======================================================================*/
- gpio_init(GPIOB,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_0|GPIO_PIN_1); /* Position Sensor Magnet Error Detect: LO, HI*/
-
- gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_2); /* Cadence direction */
-
- /* JTAG-DP disabled and SW-DP enabled, so SPI2 can use PB3 and PB4*/
- gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP,ENABLE);
- gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3 | GPIO_PIN_5); /* SPI2_SCK, SPI2_MOSI*/
- gpio_init(GPIOB, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_4); /* SPI2_MISO */
-
- gpio_init(GPIOB, GPIO_MODE_AF_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_6 | GPIO_PIN_7); /* I2C0_SCL, I2C0_SDA*/
- /* Remap CAN0 GPIO */
- gpio_pin_remap_config(GPIO_CAN0_PARTIAL_REMAP,ENABLE);
-
- gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_8); /* CAN_RX */
-
- gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9); /* CAN_TX*/
- /* Remap Timer1 GPIO */
- gpio_pin_remap_config(GPIO_TIMER1_FULL_REMAP,ENABLE);
- gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_10); /* TIM1_CH2: Capture For Candence Frequency */
- gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_11); /* TIM1_CH3: Capture For Bike Speed */
-
- gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_12); /* TIM0_BRKIN */
- 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 */
- /*=======================================================================
- GPIO C
- =======================================================================*/
- gpio_init(GPIOC,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_0); /* Switch State */
-
- gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_1); /* Power Lock */
-
- 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 */
- gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8); /* Test Pins*/
- //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 */
- gpio_pin_remap_config(GPIO_TIMER2_FULL_REMAP,ENABLE);
- gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_9); /* TIM2_CH3: 70KHz For Torque Sensor*/
- gpio_init(GPIOC,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_10); /* UART3_TX*/
-
- gpio_init(GPIOC,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_11); /* UART3_RX */
-
- // gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_10); // test
- //
- // gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_11); // test
-
- gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_12); /* I2C0 Write Protect*/
- gpio_init(GPIOC,GPIO_MODE_IPD,GPIO_OSPEED_50MHZ,GPIO_PIN_14); /* Bike Brake */
- gpio_init(GPIOC,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_15); /* Light Enable */
- /*=======================================================================
- GPIO D
- =======================================================================*/
- gpio_init(GPIOD,GPIO_MODE_OUT_PP,GPIO_OSPEED_50MHZ, GPIO_PIN_2); /* SPI Chip Select */
- gpio_bit_set(GPIOD, GPIO_PIN_2);
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\I2C\I2C_EEPROM_dma\at24cxx.c
- E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\EXTI\Key_external_interrupt_mode\gd32f30x_it.c
- ***************************************E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\I2C\I2C_EEPROM_dma\at24cxx.c
- E:\General Software Platform\GD32F30x_Firmware_Library_V2.1.4\Examples\EXTI\Key_external_interrupt_mode\gd32f30x_it.c**********************************/
- void hw_voInitEXTI(void)
- {
- // EXTI_InitTypeDef EXTI_InitStructure;
- //
- // SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOC, EXTI_PinSource14); // Break extern interrupt extern interrupt trigger source PIN
- // EXTI_InitStructure.EXTI_Line = EXTI_Line14;
- // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- // EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- // EXTI_Init(&EXTI_InitStructure);
-
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitDMA(void)
- {
- dma_parameter_struct dma_init_struct;
-
- /*=======================================================================
- DMA0 Ch4 for Timer0
- =======================================================================*/
- dma_deinit(DMA0,DMA_CH4);
- dma_init_struct.periph_addr = (uint32_t)(&TIMER_DMATB(TIMER0));
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_init_struct.memory_addr = (uint32_t)(Pwms[0].ActiveCompareValues);
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
- dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
- dma_init_struct.number = TIM1_DMA_NUM;
- dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;
- dma_init(DMA0,DMA_CH4,&dma_init_struct);
-
- dma_circulation_enable(DMA0,DMA_CH4);
- /* enable DMA0 channel4 */
- //dma_channel_enable(DMA0,DMA_CH4);
-
- /*=======================================================================
- DMA0 Ch0 for ADC0
- =======================================================================*/
- dma_deinit(DMA0, DMA_CH0);
-
- /* initialize DMA single data mode */
- dma_init_struct.periph_addr = (uint32_t)(&ADC_RDATA(ADC0));
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_init_struct.memory_addr = (uint32_t)(Adcs[0].Results);
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
- dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
- dma_init_struct.number = ADC0_DMA_NUM;
- dma_init_struct.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA0, DMA_CH0, &dma_init_struct);
- dma_circulation_enable(DMA0, DMA_CH0);
-
- /* enable DMA0 channel0 */
- dma_channel_enable(DMA0, DMA_CH0);
- /*=======================================================================
- DMA1 Ch4 for ADC1
- =======================================================================*/
- /////////// !!!no dma for adc1
-
- // dma_deinit(DMA1, DMA_CH4);
- //
- // /* initialize DMA single data mode */
- // dma_init_struct.periph_addr = (ULONG)(&ADC_RDATA(ADC1));
- // dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- // dma_init_struct.memory_addr = (ULONG)(hw_uwADC2);
- // dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- // dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
- // dma_init_struct.memory_width = DMA_MEMORY_WIDTH_16BIT;
- // dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
- // dma_init_struct.number = ADC2_DMA_NUM;
- // dma_init_struct.priority = DMA_PRIORITY_HIGH;
- // dma_init(DMA1, DMA_CH4, &dma_init_struct);
- //
- // dma_circulation_enable(DMA1, DMA_CH4);
- //
- // /* enable DMA1 channel4 */
- // dma_channel_enable(DMA1, DMA_CH4);
- /*=======================================================================
- DMA0 Ch5 for I2C0 transmission
- =======================================================================*/
- dma_deinit(DMA0, DMA_CH5);
-
- dma_init_struct.periph_addr = (uint32_t)(&I2C_DATA(I2C0));
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_init_struct.memory_addr = (uint32_t)(I2C_ubWriteBuffer);
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
- dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
- dma_init_struct.number = I2C1_TX_DMA_NUM;
- dma_init_struct.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA0, DMA_CH5, &dma_init_struct);
-
- dma_circulation_disable(DMA0, DMA_CH5);
- /* enable DMA0 channel5 */
- //dma_channel_enable(DMA0, DMA_CH5);
- /*=======================================================================
- DMA0 Ch6 for I2C0 receive
- =======================================================================*/
- dma_deinit(DMA0, DMA_CH6);
-
- dma_init_struct.periph_addr = (uint32_t)(&I2C_DATA(I2C0));
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_init_struct.memory_addr = (uint32_t)(I2C_ubReadBuffer[0]);
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
- dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
- dma_init_struct.number = I2C1_RX_DMA_NUM;
- dma_init_struct.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA0, DMA_CH6, &dma_init_struct);
-
- dma_circulation_disable(DMA0, DMA_CH6);
- /* enable DMA0 channel6 */
- //dma_channel_enable(DMA0, DMA_CH6);
-
- /*=======================================================================
- DMA1 Ch4 for UART3 transmission
- =======================================================================*/
- dma_deinit(DMA1, DMA_CH4);
-
- dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
- dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- dma_init_struct.memory_addr = (uint32_t)(0x20000000);
- dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
- dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
- dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;
- dma_init_struct.number = 64;
- dma_init_struct.priority = DMA_PRIORITY_HIGH;
- dma_init(DMA1, DMA_CH4, &dma_init_struct);
-
- dma_circulation_disable(DMA1, DMA_CH4);
- /* enable DMA1 channel4 */
- //dma_channel_enable(DMA1, DMA_CH4);
-
- // /*=======================================================================
- // DMA1 Ch2 for UART3 receive
- // =======================================================================*/
- // dma_deinit(DMA1, DMA_CH2);
- //
- // dma_init_struct.periph_addr = (uint32_t)(&USART_DATA(UART3));
- // dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
- // dma_init_struct.memory_addr = (uint32_t)(UART_ubReadBuffer);
- // dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;
- // dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;
- // dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;
- // dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;
- // dma_init_struct.number = 22;
- // dma_init_struct.priority = DMA_PRIORITY_HIGH;
- // dma_init(DMA1, DMA_CH2, &dma_init_struct);
- //
- // dma_circulation_disable(DMA1, DMA_CH2);
- //
- // /* enable DMA1 channel2 */
- // dma_channel_enable(DMA1, DMA_CH2);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitADC(void)
- {
- /*=======================================================================
- ADC0
- =======================================================================*/
- /* ADC reset */
- adc_deinit(ADC0);
-
- /* ADC mode config */
- adc_mode_config(ADC_MODE_FREE);
-
- /* ADC special function config */
- adc_special_function_config(ADC0, ADC_SCAN_MODE, ENABLE);
- adc_special_function_config(ADC0, ADC_CONTINUOUS_MODE, DISABLE);
-
- /* ADC data alignment config */
- adc_data_alignment_config(ADC0, ADC_DATAALIGN_RIGHT);
-
- /* ADC channel length config */
- adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 8);
- adc_channel_length_config(ADC0, ADC_INSERTED_CHANNEL, 3);
-
- /* ADC regular channel config */
- adc_regular_channel_config(ADC0, 0, ADC_CHANNEL_5, ADC_SAMPLETIME_7POINT5); //PA5 Ubus
- adc_regular_channel_config(ADC0, 1, ADC_CHANNEL_6, ADC_SAMPLETIME_7POINT5); //PA6 U_6V
- adc_regular_channel_config(ADC0, 2, ADC_CHANNEL_7, ADC_SAMPLETIME_7POINT5); //PA7 U_5V
- adc_regular_channel_config(ADC0, 3, ADC_CHANNEL_14, ADC_SAMPLETIME_7POINT5); //PC4 PCB_Temp
- adc_regular_channel_config(ADC0, 4, ADC_CHANNEL_15, ADC_SAMPLETIME_7POINT5); //PC5 Motor_Temp
- adc_regular_channel_config(ADC0, 5, ADC_CHANNEL_12, ADC_SAMPLETIME_7POINT5); //PC2 U_12V
- adc_regular_channel_config(ADC0, 6, ADC_CHANNEL_13, ADC_SAMPLETIME_7POINT5); //PC3 Throtttle
- adc_regular_channel_config(ADC0, 7, ADC_CHANNEL_3, ADC_SAMPLETIME_7POINT5); //PA3 Torque
- /* 8· = 0.55*8 = 4.44us */
-
- /* ADC inserted channel config */
- adc_inserted_channel_config(ADC0, 0, ADC_CHANNEL_0, ADC_SAMPLETIME_7POINT5); //PA0 Iu
- adc_inserted_channel_config(ADC0, 1, ADC_CHANNEL_1, ADC_SAMPLETIME_7POINT5); //PA1 Iv
- adc_inserted_channel_config(ADC0, 2, ADC_CHANNEL_2, ADC_SAMPLETIME_7POINT5); //PA2 Iw
-
- /* ADC trigger config */
- adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
- adc_external_trigger_source_config(ADC0, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);
-
- /* ADC external trigger enable */
- adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
- adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
-
- /* ADC DMA function enable */
- adc_dma_mode_enable(ADC0);
-
- /* enable ADC interface */
- adc_enable(ADC0);
-
- /* ADC calibration and reset calibration */
- adc_calibration_enable(ADC0);
- /* ADC software trigger enable */
- adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
-
-
- /*=======================================================================
- ADC1
- =======================================================================*/
- /* ADC reset */
- adc_deinit(ADC1);
-
- /* ADC special function config */
- adc_special_function_config(ADC1, ADC_SCAN_MODE, ENABLE);
- adc_special_function_config(ADC1, ADC_CONTINUOUS_MODE, DISABLE);
-
- /* ADC data alignment config */
- adc_data_alignment_config(ADC1, ADC_DATAALIGN_RIGHT);
-
- /* ADC channel length config */
- adc_channel_length_config(ADC1, ADC_INSERTED_CHANNEL, 1);
-
- /* ADC inserted channel config */
- adc_inserted_channel_config(ADC1, 0, ADC_CHANNEL_4, ADC_SAMPLETIME_7POINT5); //PA4 Ibus
-
- /* ADC trigger config */
- adc_external_trigger_source_config(ADC1, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3);
-
- /* ADC external trigger enable */
- adc_external_trigger_config(ADC1, ADC_INSERTED_CHANNEL, ENABLE);
- /* enable ADC interface */
- adc_enable(ADC1);
-
- /* ADC calibration and reset calibration */
- adc_calibration_enable(ADC1);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitTim0(void)
- {
-
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
- timer_break_parameter_struct timer_breakpara;
-
- /* Timer reset */
- timer_deinit(TIMER0);
- timer_struct_para_init(&timer_initpara); // must init, or timer_initpara.counterdirection will be arbitrary
-
- /* TIMER0 configuration */
- timer_initpara.prescaler = 0;
- timer_initpara.alignedmode = TIMER_COUNTER_CENTER_BOTH;
- timer_initpara.period = HW_HPWM_PERIOD;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER0,&timer_initpara);
- /* CH0, CH1 and CH2 configuration as PWM complementary output */
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
- timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
- timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_0, HW_HHPWM_PERIOD);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_0, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_1, HW_HHPWM_PERIOD);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_1, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, HW_HHPWM_PERIOD);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_2, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
-
- /* CH3 configuration as ADC trigger signal */
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER0, TIMER_CH_3, &timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_3, 200);
- timer_channel_output_mode_config(TIMER0, TIMER_CH_3, TIMER_OC_MODE_PWM1);
- //timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
- timer_channel_output_shadow_config(TIMER0, TIMER_CH_3, TIMER_OC_SHADOW_ENABLE);
-
- /* automatic output enable, break, dead time and lock configuration*/
- timer_breakpara.runoffstate = TIMER_ROS_STATE_ENABLE;
- timer_breakpara.ideloffstate = TIMER_IOS_STATE_ENABLE ;
- timer_breakpara.deadtime = cp_stControlPara.swIPMDeadTimeNs / 100 * TIM0CLK_KHZ / 1000 / 10;;
- timer_breakpara.breakpolarity = TIMER_BREAK_POLARITY_LOW;
- timer_breakpara.outputautostate = TIMER_OUTAUTO_DISABLE;
- timer_breakpara.protectmode = TIMER_CCHP_PROT_OFF;
- timer_breakpara.breakstate = TIMER_BREAK_ENABLE;
- timer_break_config(TIMER0,&timer_breakpara);
- /* TIMER0 primary output enable */
- timer_primary_output_config(TIMER0,ENABLE);
- /* TIMER0 update DMA request enable */
- timer_dma_transfer_config(TIMER0,TIMER_DMACFG_DMATA_CH0CV,TIMER_DMACFG_DMATC_3TRANSFER);
- //timer_dma_enable(TIMER0,TIMER_DMA_UPD);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER0);
-
- /* select the master slave mode */
- timer_master_slave_mode_config(TIMER0, TIMER_MASTER_SLAVE_MODE_ENABLE);
- /* Update event is used as trigger output */
- timer_master_output_trigger_source_select(TIMER0, TIMER_TRI_OUT_SRC_UPDATE);
-
- /* TIMER0 counter enable */
- ////timer_enable(TIMER0);
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitTim1(void)
- {
- timer_ic_parameter_struct timer_icinitpara;
- timer_parameter_struct timer_initpara;
- timer_deinit(TIMER1);
- /* TIMER1 configuration */
- timer_initpara.prescaler = 99;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = TIM1CLK_KHZ * 1000 / (1000 / 25); // period = 25ms
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER1,&timer_initpara);
- /* TIMER1 CH0 input capture configuration */
- timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
- timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
- timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
- timer_icinitpara.icfilter = 0x0;
- timer_input_capture_config(TIMER1,TIMER_CH_2,&timer_icinitpara);
- timer_input_capture_config(TIMER1,TIMER_CH_3,&timer_icinitpara);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER1);
- /* TIMER1 counter enable */
- ////timer_enable(TIMER1);
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitTim2(void)
- {
- timer_oc_parameter_struct timer_ocintpara;
- timer_parameter_struct timer_initpara;
-
- /* Timer reset */
- timer_deinit(TIMER2);
- /* TIMER2 configuration */
- timer_initpara.prescaler = 0;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = TIM2CLK_KHZ / 70; // 70kHz for torque sensor
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER2,&timer_initpara);
- /* CH3 configuration for 70kHz output */
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
- timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
- timer_ocintpara.ocnpolarity = TIMER_OCN_POLARITY_HIGH;
- timer_ocintpara.ocidlestate = TIMER_OC_IDLE_STATE_LOW;
- timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
- timer_channel_output_config(TIMER2, TIMER_CH_3, &timer_ocintpara);
- timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, (1028>>1));
- timer_channel_output_mode_config(TIMER2, TIMER_CH_3, TIMER_OC_MODE_PWM1);
- timer_channel_output_shadow_config(TIMER2, TIMER_CH_3, TIMER_OC_SHADOW_DISABLE);
-
- /* TIMER2 primary output enable */
- timer_primary_output_config(TIMER2,ENABLE);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER2);
-
- /* TIMER2 counter enable */
- ////timer_enable(TIMER2);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitTim3(void)
- {
-
- timer_parameter_struct timer_initpara;
- timer_deinit(TIMER3);
- /* TIMER configuration */
- timer_initpara.prescaler = 0;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = (FTBC_HZ<<2)/FTBS_HZ-1;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER3,&timer_initpara);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER3);
- /* slave mode selection: TIMER3 */
- timer_slave_mode_select(TIMER3, TIMER_SLAVE_MODE_EXTERNAL0);
- timer_input_trigger_source_select(TIMER3, TIMER_SMCFG_TRGSEL_ITI0);
- /* select the master slave mode */
- timer_master_slave_mode_config(TIMER3, TIMER_MASTER_SLAVE_MODE_ENABLE);
-
- /* TIMER3 counter enable */
- ////timer_enable(TIMER3);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitTim5(void)
- {
- timer_parameter_struct timer_initpara;
-
- /* Timer reset */
- timer_deinit(TIMER5);
- /* TIMER5 configuration */
- timer_initpara.prescaler = 1;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = HW_1MS_PERIOD;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER5,&timer_initpara);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER5);
-
- /* TIMER5 counter enable */
- ////timer_enable(TIMER5);
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitTim6(void)
- {
- timer_parameter_struct timer_initpara;
-
- /* Timer reset */
- timer_deinit(TIMER6);
- /* configuration */
- timer_initpara.prescaler = 0;
- timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
- timer_initpara.counterdirection = TIMER_COUNTER_UP;
- timer_initpara.period = (HW_PWM_PERIOD << 1) - 1;
- timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
- timer_initpara.repetitioncounter = 0;
- timer_init(TIMER6,&timer_initpara);
- /* auto-reload preload enable */
- timer_auto_reload_shadow_enable(TIMER6);
-
- /* counter enable */
- ////timer_enable(TIMER6);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitUART3(void)
- {
- /* reset USART */
- usart_deinit(UART3);
- /* configure USART baud rate value */
- usart_baudrate_set(UART3, 1500000U);
- /* configure USART parity function */
- usart_parity_config(UART3, USART_PM_NONE);
- /* configure USART word length */
- usart_word_length_set(UART3, USART_WL_8BIT);
- /* configure USART stop bit length */
- usart_stop_bit_set(UART3, USART_STB_1BIT);
- /* configure USART transmitter */
- usart_transmit_config(UART3, USART_TRANSMIT_ENABLE);
- /* configure USART receiver */
- usart_receive_config(UART3, USART_RECEIVE_ENABLE);
- /* USART DMA enable for transmission and receive */
- usart_dma_transmit_config(UART3, USART_DENT_ENABLE);
- usart_dma_receive_config(UART3, USART_DENR_ENABLE);
- /* enable USART */
- usart_enable(UART3);
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitSPI2(void)
- {
- spi_parameter_struct spi_init_struct;
- /* reset SPI */
- spi_i2s_deinit(SPI2);
- /* SPI2 parameter config */
- spi_init_struct.device_mode = SPI_MASTER;
- spi_init_struct.trans_mode = SPI_TRANSMODE_FULLDUPLEX;
- spi_init_struct.frame_size = SPI_FRAMESIZE_16BIT;
- spi_init_struct.nss = SPI_NSS_SOFT;
- spi_init_struct.endian = SPI_ENDIAN_MSB;
- spi_init_struct.clock_polarity_phase = SPI_CK_PL_HIGH_PH_2EDGE;
- spi_init_struct.prescale = SPI_PSC_8; // 36M/8=4.5Mhz
- spi_init(SPI2, &spi_init_struct);
-
- /* configure SPI CRC function */
- spi_crc_polynomial_set(SPI2, 7);
- spi_crc_on(SPI2);
-
- /* SPI DMA enable */
- // spi_dma_enable(SPI2, SPI_DMA_TRANSMIT);
- // spi_dma_enable(SPI2, SPI_DMA_RECEIVE);
-
- /* SPI enable */
- spi_enable(SPI2);
-
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitI2C0(void)
- {
- i2c_deinit(I2C0);
-
- /* configure I2C0 clock */
- i2c_clock_config(I2C0, 100000, I2C_DTCY_2);
-
- /* configure I2C0 address */
- i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_SLAVEADDR_BLOCK3);
-
- /* enable acknowledge */
- i2c_ack_config(I2C0, I2C_ACK_ENABLE);
-
- // /* enable I2C0 DMA */
- // i2c_dma_config(I2C0, I2C_DMA_ON);
-
- /* enable I2C0 */
- i2c_enable(I2C0);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitCAN(void)
- {
- can_parameter_struct can_parameter;
- can_filter_parameter_struct can_filter;
-
- can_struct_para_init(CAN_INIT_STRUCT, &can_parameter);
- can_struct_para_init(CAN_INIT_STRUCT, &can_filter);
-
- /* initialize CAN register */
- can_deinit(CAN0);
-
- /* initialize CAN */
- can_parameter.time_triggered = DISABLE;
- can_parameter.auto_bus_off_recovery = ENABLE;
- can_parameter.auto_wake_up = DISABLE;
- can_parameter.auto_retrans = DISABLE;
- can_parameter.rec_fifo_overwrite = DISABLE;
- can_parameter.trans_fifo_order = DISABLE;
- can_parameter.working_mode = CAN_NORMAL_MODE;
- can_parameter.resync_jump_width = CAN_BT_SJW_1TQ;
- can_parameter.time_segment_1 = CAN_BT_BS1_4TQ;
- can_parameter.time_segment_2 = CAN_BT_BS2_1TQ;
- /* baudrate 1Mbps */
- can_parameter.prescaler = 24; //36M/((1+4+1)*24)=250K
- can_init(CAN0, &can_parameter);
-
- /* initialize filter 0 */
- can_filter.filter_number = 0;
- can_filter.filter_mode = CAN_FILTERMODE_MASK;
- can_filter.filter_bits = CAN_FILTERBITS_32BIT;
- can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_TO_MC_FILTER << 21) & 0xFFFF0000U) >> 16);
- can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_TO_MC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
- can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF0000U) >> 16);
- can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_TO_MC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
- can_filter.filter_fifo_number = CAN_FIFO0;
- can_filter.filter_enable = ENABLE;
- can_filter_init(&can_filter);
-
- /* initialize filter 1 */
- can_filter.filter_number = 1;
- can_filter.filter_mode = CAN_FILTERMODE_MASK;
- can_filter.filter_bits = CAN_FILTERBITS_32BIT;
- can_filter.filter_list_high = (uint16_t)((((uint32_t)ID_BC_FILTER << 21) & 0xFFFF0000U) >> 16);
- can_filter.filter_list_low = (uint16_t)(((uint32_t)ID_BC_FILTER << 21) | CAN_FF_STANDARD | CAN_FT_DATA) & 0xFFFF;
- can_filter.filter_mask_high = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF0000U) >> 16);
- can_filter.filter_mask_low = (uint16_t)((((uint32_t)ID_BC_MASK << 21) & 0xFFFF) | CAN_FF_STANDARD | CAN_FT_DATA);
- can_filter.filter_fifo_number = CAN_FIFO0;
- can_filter.filter_enable = ENABLE;
- can_filter_init(&can_filter);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitSysTick(void)
- {
- /* Setup systick timer interrupts */
- /* Do not exceed 2^24-1=16777215 */
- SysTick_Config(SystemCoreClock / FSYSTICK_HZ);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voInitInt(void)
- {
- /* Set the priority group */
- nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
- /* ADC, CMP0 and CMP1 interrupts */
- nvic_irq_enable((uint8_t)ADC0_1_IRQn, 0, 3);
- /* TIMER0 break, update, trigger and commutation interrupts */
- nvic_irq_enable((uint8_t)TIMER0_UP_TIMER9_IRQn, 1, 1);
-
- /* TIMER1 interrupt: CAP */
- nvic_irq_enable((uint8_t)TIMER1_IRQn, 2, 1);
- /* TIMER3 interrupt: TBS */
- nvic_irq_enable((uint8_t)TIMER3_IRQn, 3, 2);
-
- /* TIMER5 interrupt: 1ms */
- nvic_irq_enable((uint8_t)TIMER5_IRQn, 2, 3);
- //nvic_irq_enable(DMA0_Channel4_IRQn, 0, 1); // test
- /* DMA1 CH2 interrupt: Uart */
- nvic_irq_enable((uint8_t)DMA1_Channel2_IRQn, 3, 3);
-
- /* DMA1 CH4 interrupt: Uart */
- nvic_irq_enable((uint8_t)DMA1_Channel4_IRQn, 3, 3);
-
- /* CAN0 RX0 interrupt */
- nvic_irq_enable((uint8_t)CAN0_RX0_IRQn, 3, 3);
-
- /* CAN0 RX1 interrupt */
- nvic_irq_enable((uint8_t)CAN0_RX1_IRQn, 3, 3);
-
- nvic_irq_enable((uint8_t)EXTI10_15_IRQn, 3, 3);
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voEnInt(void)
- {
- /* ADC group conversion Interrupts enable */
- if(cp_stFlg.CurrentSampleModelSelect == COMBINATION)
- {
- adc_interrupt_enable(ADC0 , ADC_INT_EOIC);
- adc_interrupt_enable(ADC1 , ADC_INT_EOIC);
- //adc_interrupt_enable(ADC1 , ADC_INT_EOC);
- }
- else
- {
- //do nothing
- }
- /* TIM0 Interrupts enable */
- timer_interrupt_enable(TIMER0,TIMER_INT_UP);
- /* TIM1 Interrupts enable */
- timer_interrupt_enable(TIMER1,TIMER_INT_UP);
- timer_interrupt_enable(TIMER1,TIMER_INT_CH2);
- timer_interrupt_enable(TIMER1,TIMER_INT_CH3);
- /* TIM3 Interrupts enable */
- timer_interrupt_enable(TIMER3,TIMER_INT_UP);
- /* TIM5 Interrupts enable */
- timer_interrupt_enable(TIMER5,TIMER_INT_UP);
- //dma_interrupt_enable(DMA0, DMA_CH4, DMA_INT_FTF);/* test */
- /* DMA1 channel2 full transfer finish interrupt */
- dma_interrupt_enable(DMA1, DMA_CH2, DMA_INT_FTF);
-
- /* DMA1 channel4 full transfer finish interrupt */
- dma_interrupt_enable(DMA1, DMA_CH4, DMA_INT_FTF);
-
- /* CAN0 receive FIFO0 not empty interrupt */
- can_interrupt_enable(CAN0, CAN_INT_RFNE0);
-
- /* CAN0 receive FIFO0 overfull interrupt */
- can_interrupt_enable(CAN0, CAN_INT_RFO0);
-
- /* CAN0 receive FIFO1 not empty interrupt */
- can_interrupt_enable(CAN0, CAN_INT_RFNE1);
-
- /* CAN0 receive FIFO1 overfull interrupt */
- can_interrupt_enable(CAN0, CAN_INT_RFO1);
-
- /* CAN0 error interrupt */
- can_interrupt_enable(CAN0, CAN_INT_ERR);
-
- /* CAN0 bus-off interrupt */
- can_interrupt_enable(CAN0, CAN_INT_BO);
-
- /* CAN0 warning error interrupt */
- can_interrupt_enable(CAN0, CAN_INT_WERR);
-
- /* CAN0 passive error interrupt */
- can_interrupt_enable(CAN0, CAN_INT_PERR);
- // CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // FIFO 0 message pending Interrupt
- // CAN_ITConfig(CAN1, CAN_IT_FOV0, ENABLE); // FIFO 0 overrun Interrupt
- // CAN_ITConfig(CAN1, CAN_IT_FMP1, ENABLE); // FIFO 1 message pending Interrupt
- // CAN_ITConfig(CAN1, CAN_IT_FOV1, ENABLE); // FIFO 1 overrun Interrupt
- // //CAN_ITConfig(CAN1, CAN_IT_ERR, ENABLE); // Error Interrupt
- // CAN_ITConfig(CAN1, CAN_IT_LEC, ENABLE); // Last error code Interrupt
- // //CAN_ITConfig(CAN1, CAN_IT_BOF, ENABLE); // Bus-off Interrupt
- // //CAN_ITConfig(CAN1, CAN_IT_EPV, ENABLE); // Error passive Interrupt
- // //CAN_ITConfig(CAN1, CAN_IT_EWG, ENABLE); // Error warning Interrupt
- }
- /*************************************************************************
- Function:
- Description:
- Call by:
- Input Variables:
- Output/Return Variables:
- Subroutine Call:
- Reference:
- *************************************************************************/
- void hw_voTimEn(void)
- {
- /* TIMER0 counter enable */
- timer_enable(TIMER0);
- /* TIMER1 counter enable */
- timer_enable(TIMER1);
- /* TIMER2 counter enable */
- timer_enable(TIMER2);
- /* TIMER3 counter enable */
- timer_enable(TIMER3);
- /* TIMER5 counter enable */
- timer_enable(TIMER5);
- /* counter enable */
- timer_enable(TIMER6);
- }
- void hw_voPWMInit(void)
- {
- /* Set 50% duty */
- UWORD prd = iPwm_GetCountMax(0);
- iPwm_SetCompareValue(0, 0, prd >> 1);
- iPwm_SetCompareValue(0, 1, prd >> 1);
- iPwm_SetCompareValue(0, 2, prd >> 1);
- iPwm_SetCompareValueDelay(0, 0, prd >> 1);
- iPwm_SetCompareValueDelay(0, 1, prd >> 1);
- iPwm_SetCompareValueDelay(0, 2, prd >> 1);
- /* PWM off */
- hw_voPWMOff();
- /* Charge init */
- hw_uwChrgCt = 0;
- hw_blChrgOvrFlg = FALSE;
- }
- void hw_voPWMOn(void)
- {
- #if (0)
- timer_oc_parameter_struct timer_ocintpara;
- if (hw_blPWMOnFlg == FALSE)
- {
- /* CH0, CH1 and CH2 output enable */
- timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
- timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
- timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
- timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
- timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
- /* Flag set */
- hw_blPWMOnFlg = TRUE;
- }
- #endif
- if (hw_blPWMOnFlg == FALSE)
- {
- /* CH0, CH1 set to "PWM mode 2" */
- TIMER_CHCTL0(TIMER0) |= 0x7070;
- /* CH2, CH3 set to "PWM mode 2" */
- TIMER_CHCTL1(TIMER0) |= 0x7070;
- /* CH0, CH1 and CH2, CH3 output enable */
- TIMER_CHCTL2(TIMER0) |= 0x5555;
- /* Flag set */
- hw_blPWMOnFlg = TRUE;
- }
- }
- void hw_voPWMOff(void)
- {
- #if (0)
- timer_oc_parameter_struct timer_ocintpara;
- if (hw_blPWMOnFlg == TRUE)
- {
- /* CH0, CH1 and CH2 output disable */
- timer_ocintpara.outputstate = TIMER_CCX_DISABLE;
- timer_ocintpara.outputnstate = TIMER_CCXN_DISABLE;
- timer_channel_output_config(TIMER0, TIMER_CH_0, &timer_ocintpara);
- timer_channel_output_config(TIMER0, TIMER_CH_1, &timer_ocintpara);
- timer_channel_output_config(TIMER0, TIMER_CH_2, &timer_ocintpara);
- /* Flag set */
- hw_blPWMOnFlg = FALSE;
- }
- #endif
- if (hw_blPWMOnFlg == TRUE)
- {
- /* CH0, CH1 set to "Frozen" */
- TIMER_CHCTL0(TIMER0) &= ~0x7070;
- /* CH2, CH3 set to "Frozen" */
- TIMER_CHCTL1(TIMER0) &= ~0x7070;
- /* CH0, CH1 set to "Force inactive level" */
- TIMER_CHCTL0(TIMER0) |= 0x4040;
- /* CH2, CH3 set to "Force inactive level" */
- TIMER_CHCTL1(TIMER0) |= 0x4040;
- /* CH0, CH1 and CH2 complementary output disable */
- TIMER_CHCTL2(TIMER0) &= ~0x0444;
- /* Flag set */
- hw_blPWMOnFlg = FALSE;
- }
- }
- void hw_voCharge(void)
- {
- /* Set 50% duty */
- UWORD prd = iPwm_GetCountMax(0);
- iPwm_SetCompareValue(0, 0, prd >> 1);
- iPwm_SetCompareValue(0, 1, prd >> 1);
- iPwm_SetCompareValue(0, 2, prd >> 1);
- iPwm_SetCompareValueDelay(0, 0, prd >> 1);
- iPwm_SetCompareValueDelay(0, 1, prd >> 1);
- iPwm_SetCompareValueDelay(0, 2, prd >> 1);
- /* PWM on */
- hw_voPWMOn();
- ++hw_uwChrgCt;
- if (hw_uwChrgCt > cp_stControlPara.swIPMHvicChrgMs)
- {
- hw_uwChrgCt = 0;
- hw_blChrgOvrFlg = TRUE;
- }
- }
- /* Three phase short */
- void hw_voThrPhsShrt(void)
- {
- /* Set 0% duty */
- iPwm_SetCompareValue(0, 0, 0);
- iPwm_SetCompareValue(0, 1, 0);
- iPwm_SetCompareValue(0, 2, 0);
- iPwm_SetCompareValueDelay(0, 2, 0);
- iPwm_SetCompareValueDelay(0, 2, 0);
- iPwm_SetCompareValueDelay(0, 2, 0);
- iPwm_SetCompareValueImmediate(0, 2, 0);
- iPwm_SetCompareValueImmediate(0, 2, 0);
- iPwm_SetCompareValueImmediate(0, 2, 0);
- /* PWM on */
- hw_voPWMOn();
- }
- /*************************************************************************
- Function
- Description: Initialize UART
- Call by: ;
- Input Variables: N/A
- Output/Return Variables: N/A
- Subroutine Call: ...;
- Reference: N/A
- ****************************************************************/
- void hw_voIWDGInit(UWORD prer, UWORD rlr)
- {
- /* enable RCU_IRC40K */
- rcu_osci_on(RCU_IRC40K);
- /* wait till RCU_IRC40K is ready */
- rcu_osci_stab_wait(RCU_IRC40K);
- fwdgt_write_enable();
- fwdgt_prescaler_value_config(prer);
- fwdgt_reload_value_config(rlr);
- fwdgt_counter_reload();
- fwdgt_enable();
- }
- /*************************************************************************
- Copyright (c) 2022 Welling Motor Technology(Shanghai) Co. Ltd.
- All rights reserved.
- *************************************************************************/
- #ifdef _HWSETUP_C_
- #undef _HWSETUP_C_ /* parasoft-suppress MISRA2004-19_6 "本项目中无法更改,后续避免使用" */
- #endif
- /************************************************************************
- End of this File (EOF)!
- Do not put anything after this part!
- *************************************************************************/
|