123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- #include "dshot.h"
- /*
- 使用案例:
- DshotInit(TIM2, GPIOA, GPIO_Pin_5, GPIO_AF_TIM2, GPIO_PinSource5,70-1, 4-1, DMA1_Stream5_IRQn, RCC_APB1Periph_TIM2, RCC_AHB1Periph_GPIOA,RCC_AHB1PeriphClockCmd,TIM_OC1Init,TIM_OC1PreloadConfig)
- */
- void Dshot_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef *GPIOx, u16 PIN, u8 GPIO_PinSource, u8 GPIO_AF)
- {
- RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn,ENABLE);
- //引脚复用
- GPIO_PinAFConfig(GPIOx, GPIO_PinSource, GPIO_AF);
- //GPIO初始化
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 速度50MHz GPIO_Speed_50MHz
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 GPIO_PuPd_UP
- GPIO_Init(GPIOx,&GPIO_InitStructure);
- }
- void Dshot_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
- {
- //DMA中断设置
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel; //DMA2_Stream0中断
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; //抢占优先级1
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; //子优先级1
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
- }
- void Dshot_TIM_Config(void(*ClockCMD)(uint32_t, FunctionalState),u32 RCC_APBnPeriphn, //APB时钟使能设置
- TIM_TypeDef* TIMx, u16 psc, u16 per, //dshot频率设置
- void(*TIM_OCInit)(TIM_TypeDef*, TIM_OCInitTypeDef*), //OC通道设置
- void(*TIM_OCPreloadConfig)(TIM_TypeDef*, uint16_t)) //OC通道使能
- {
- //使能时钟
- (*ClockCMD)(RCC_APBnPeriphn,ENABLE);
- //时基初始化
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- TIM_TimeBaseStructure.TIM_Period = per; //自动重装载值
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);//初始化定时器
- //输出比较初始化
- TIM_OCInitTypeDef TIM_OCInitStructure;
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
-
- (*TIM_OCInit)(TIMx,&TIM_OCInitStructure);
-
- (*TIM_OCPreloadConfig)(TIMx,TIM_OCPreload_Enable);
-
- TIM_ARRPreloadConfig(TIMx,ENABLE);//ARPE使能
-
- TIM_Cmd(TIMx, ENABLE);//定时器使能
- }
- //DSHOT_DMA_Config(TIM2,TIM_DMA_CC1,DMA1_Stream5,DMA_Channel_3,(uint32_t)&(TIM2->CCR1),(uint32_t)dd)
- void DSHOT_DMA_Config(TIM_TypeDef* TIMx,uint16_t TIM_DMASource, DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar)
- {
- DMA_InitTypeDef DMA_InitStructure;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
-
- DMA_DeInit(DMA_Streamx);
-
- while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
-
- DMA_InitStructure.DMA_Channel = chx;
- DMA_InitStructure.DMA_PeripheralBaseAddr = par;
- DMA_InitStructure.DMA_Memory0BaseAddr = mar;
- DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
- DMA_InitStructure.DMA_BufferSize = 18;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
- DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_Init(DMA_Streamx, &DMA_InitStructure);
-
- TIM_DMACmd(TIMx, TIM_DMASource, ENABLE);
-
- //设置DMA中断
- DMA_ClearITPendingBit(DMA_Streamx, DMA_IT_TC); //清除中断标志
- DMA_ITConfig(DMA_Streamx, DMA_IT_TC, ENABLE); //传输完成中断
- //DMA_Cmd(DMA_Streamx, ENABLE); //使能DMA
- DMA_Cmd(DMA_Streamx, DISABLE);
- }
- u16 add_checksum_and_telemetry(u16 packet) {
- u16 packet_telemetry = (packet << 1) | 0;
- u8 i;
- int csum = 0;
- int csum_data = packet_telemetry;
-
- for (i = 0; i < 3; i++) {
- csum ^= csum_data; // xor data by nibbles
- csum_data >>= 4;
- }
- csum &= 0xf;
- packet_telemetry = (packet_telemetry << 4) | csum;
-
- return packet_telemetry; //append checksum
- }
- void pwmWriteDigital(u32 *esc_cmd, u16 value, DMA_Stream_TypeDef *DMA_Streamx)
- {
- value = ( (value > 2047) ? 2047 : value );
- value = add_checksum_and_telemetry(value);
-
- esc_cmd[0] = (value & 0x8000) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[1] = (value & 0x4000) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[2] = (value & 0x2000) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[3] = (value & 0x1000) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[4] = (value & 0x0800) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[5] = (value & 0x0400) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[6] = (value & 0x0200) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[7] = (value & 0x0100) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[8] = (value & 0x0080) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[9] = (value & 0x0040) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[10] = (value & 0x0020) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[11] = (value & 0x0010) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[12] = (value & 0x8) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[13] = (value & 0x4) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[14] = (value & 0x2) ? ESC_BIT_1 : ESC_BIT_0;
- esc_cmd[15] = (value & 0x1) ? ESC_BIT_1 : ESC_BIT_0;
-
- DMA_Cmd(DMA_Streamx, ENABLE);// enable DMA channel 6
- }
- //void PWM_GPIO_Config(void)
- //{
- // GPIO_InitTypeDef GPIO_InitStructure;
- //
- // //使能GPIO时钟
- // PWM1_ClockCmd();
- // PWM2_ClockCmd();
- // PWM3_ClockCmd();
- // PWM4_ClockCmd();
- // GPIO_PinAFConfig(PWM1_PORT, PWM1_PinSource, PWM1_GPIO_AF);
- // GPIO_PinAFConfig(PWM2_PORT, PWM2_PinSource, PWM2_GPIO_AF);
- // GPIO_PinAFConfig(PWM3_PORT, PWM3_PinSource, PWM3_GPIO_AF);
- // GPIO_PinAFConfig(PWM4_PORT, PWM4_PinSource, PWM4_GPIO_AF);
- //
- // GPIO_InitStructure.GPIO_Pin = PWM1_PIN;
- // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能
- // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 速度50MHz GPIO_Speed_50MHz
- // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
- // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 GPIO_PuPd_UP
- // GPIO_Init(PWM1_PORT,&GPIO_InitStructure); // 初始化
- // GPIO_InitStructure.GPIO_Pin = PWM2_PIN;
- // GPIO_Init(PWM2_PORT,&GPIO_InitStructure);
- //
- // GPIO_InitStructure.GPIO_Pin = PWM3_PIN;
- // GPIO_Init(PWM3_PORT,&GPIO_InitStructure);
- //
- // GPIO_InitStructure.GPIO_Pin = PWM4_PIN;
- // GPIO_Init(PWM4_PORT,&GPIO_InitStructure);
- //}
- //void PWM_TIM_Config(u16 arr, u16 psc, u16 Pulse)
- //{
- // TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- // TIM_OCInitTypeDef TIM_OCInitStructure;
- //
- // TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
- // TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
- // TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载值
- // TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- // TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- //
- // TIM_TimeBaseInit(PWM1_TIM,&TIM_TimeBaseStructure);//初始化定时器
- // TIM_TimeBaseInit(PWM2_TIM,&TIM_TimeBaseStructure);
- // TIM_TimeBaseInit(PWM3_TIM,&TIM_TimeBaseStructure);
- // TIM_TimeBaseInit(PWM4_TIM,&TIM_TimeBaseStructure);
- //
- // TIM_OCInitStructure.TIM_Pulse = Pulse;
- // TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
- // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
- // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
- //
- // PWM1_OCInit(TIM_OCInitStructure);
- // PWM2_OCInit(TIM_OCInitStructure);
- // PWM3_OCInit(TIM_OCInitStructure);
- // PWM4_OCInit(TIM_OCInitStructure);
- //
- // TIM_ARRPreloadConfig(PWM1_TIM,ENABLE);//ARPE使能
- // TIM_ARRPreloadConfig(PWM2_TIM,ENABLE);
- // TIM_ARRPreloadConfig(PWM3_TIM,ENABLE);
- // TIM_ARRPreloadConfig(PWM4_TIM,ENABLE);
- //
- // TIM_Cmd(PWM1_TIM, ENABLE);
- // TIM_Cmd(PWM2_TIM, ENABLE);
- // TIM_Cmd(PWM3_TIM, ENABLE);
- // TIM_Cmd(PWM4_TIM, ENABLE);
- //}
- //void DSHOT_NVIC_Config(void)
- //{
- // NVIC_InitTypeDef NVIC_InitStructure;
- //
- // //DMA中断设置
- // NVIC_InitStructure.NVIC_IRQChannel = PWM1_DMA_Stream_IRQn; //DMA2_Stream0中断
- // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级1
- // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级1
- // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- // NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
- //
- // NVIC_InitStructure.NVIC_IRQChannel = PWM2_DMA_Stream_IRQn;
- // NVIC_Init(&NVIC_InitStructure);
- //
- // NVIC_InitStructure.NVIC_IRQChannel = PWM3_DMA_Stream_IRQn;
- // NVIC_Init(&NVIC_InitStructure);
- //
- // NVIC_InitStructure.NVIC_IRQChannel = PWM4_DMA_Stream_IRQn;
- // NVIC_Init(&NVIC_InitStructure);
- //}
- //void OutPutPWM_Config(void)
- //{
- // PWM_GPIO_Config();
- //
- // PWM_TIM_Config(70-1,4-1,0); //DSHOT_1200
- //
- // DSHOT_NVIC_Config();
- //}
|