dshot.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #include "dshot.h"
  2. /*
  3. 使用案例:
  4. 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)
  5. */
  6. void Dshot_GPIO_Config(u32 RCC_APBnPeriphn, GPIO_TypeDef *GPIOx, u16 PIN, u8 GPIO_PinSource, u8 GPIO_AF)
  7. {
  8. RCC_AHB1PeriphClockCmd(RCC_APBnPeriphn,ENABLE);
  9. //引脚复用
  10. GPIO_PinAFConfig(GPIOx, GPIO_PinSource, GPIO_AF);
  11. //GPIO初始化
  12. GPIO_InitTypeDef GPIO_InitStructure;
  13. GPIO_InitStructure.GPIO_Pin = PIN;
  14. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能
  15. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 速度50MHz GPIO_Speed_50MHz
  16. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
  17. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 GPIO_PuPd_UP
  18. GPIO_Init(GPIOx,&GPIO_InitStructure);
  19. }
  20. void Dshot_NVIC_Config(u8 NVIC_IRQChannel, u8 pre, u8 sub)
  21. {
  22. //DMA中断设置
  23. NVIC_InitTypeDef NVIC_InitStructure;
  24. NVIC_InitStructure.NVIC_IRQChannel = NVIC_IRQChannel; //DMA2_Stream0中断
  25. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = pre; //抢占优先级1
  26. NVIC_InitStructure.NVIC_IRQChannelSubPriority = sub; //子优先级1
  27. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  28. NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
  29. }
  30. void Dshot_TIM_Config(void(*ClockCMD)(uint32_t, FunctionalState),u32 RCC_APBnPeriphn, //APB时钟使能设置
  31. TIM_TypeDef* TIMx, u16 psc, u16 per, //dshot频率设置
  32. void(*TIM_OCInit)(TIM_TypeDef*, TIM_OCInitTypeDef*), //OC通道设置
  33. void(*TIM_OCPreloadConfig)(TIM_TypeDef*, uint16_t)) //OC通道使能
  34. {
  35. //使能时钟
  36. (*ClockCMD)(RCC_APBnPeriphn,ENABLE);
  37. //时基初始化
  38. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  39. TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
  40. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  41. TIM_TimeBaseStructure.TIM_Period = per; //自动重装载值
  42. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  43. TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  44. TIM_TimeBaseInit(TIMx,&TIM_TimeBaseStructure);//初始化定时器
  45. //输出比较初始化
  46. TIM_OCInitTypeDef TIM_OCInitStructure;
  47. TIM_OCInitStructure.TIM_Pulse = 0;
  48. TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
  49. TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  50. TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
  51. (*TIM_OCInit)(TIMx,&TIM_OCInitStructure);
  52. (*TIM_OCPreloadConfig)(TIMx,TIM_OCPreload_Enable);
  53. TIM_ARRPreloadConfig(TIMx,ENABLE);//ARPE使能
  54. TIM_Cmd(TIMx, ENABLE);//定时器使能
  55. }
  56. //DSHOT_DMA_Config(TIM2,TIM_DMA_CC1,DMA1_Stream5,DMA_Channel_3,(uint32_t)&(TIM2->CCR1),(uint32_t)dd)
  57. void DSHOT_DMA_Config(TIM_TypeDef* TIMx,uint16_t TIM_DMASource, DMA_Stream_TypeDef *DMA_Streamx,u32 chx,u32 par,u32 mar)
  58. {
  59. DMA_InitTypeDef DMA_InitStructure;
  60. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE);
  61. DMA_DeInit(DMA_Streamx);
  62. while (DMA_GetCmdStatus(DMA_Streamx) != DISABLE){}
  63. DMA_InitStructure.DMA_Channel = chx;
  64. DMA_InitStructure.DMA_PeripheralBaseAddr = par;
  65. DMA_InitStructure.DMA_Memory0BaseAddr = mar;
  66. DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
  67. DMA_InitStructure.DMA_BufferSize = 18;
  68. DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  69. DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  70. DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
  71. DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
  72. DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
  73. DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
  74. DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
  75. DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
  76. DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
  77. DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
  78. DMA_Init(DMA_Streamx, &DMA_InitStructure);
  79. TIM_DMACmd(TIMx, TIM_DMASource, ENABLE);
  80. //设置DMA中断
  81. DMA_ClearITPendingBit(DMA_Streamx, DMA_IT_TC); //清除中断标志
  82. DMA_ITConfig(DMA_Streamx, DMA_IT_TC, ENABLE); //传输完成中断
  83. //DMA_Cmd(DMA_Streamx, ENABLE); //使能DMA
  84. DMA_Cmd(DMA_Streamx, DISABLE);
  85. }
  86. u16 add_checksum_and_telemetry(u16 packet) {
  87. u16 packet_telemetry = (packet << 1) | 0;
  88. u8 i;
  89. int csum = 0;
  90. int csum_data = packet_telemetry;
  91. for (i = 0; i < 3; i++) {
  92. csum ^= csum_data; // xor data by nibbles
  93. csum_data >>= 4;
  94. }
  95. csum &= 0xf;
  96. packet_telemetry = (packet_telemetry << 4) | csum;
  97. return packet_telemetry; //append checksum
  98. }
  99. void pwmWriteDigital(u32 *esc_cmd, u16 value, DMA_Stream_TypeDef *DMA_Streamx)
  100. {
  101. value = ( (value > 2047) ? 2047 : value );
  102. value = add_checksum_and_telemetry(value);
  103. esc_cmd[0] = (value & 0x8000) ? ESC_BIT_1 : ESC_BIT_0;
  104. esc_cmd[1] = (value & 0x4000) ? ESC_BIT_1 : ESC_BIT_0;
  105. esc_cmd[2] = (value & 0x2000) ? ESC_BIT_1 : ESC_BIT_0;
  106. esc_cmd[3] = (value & 0x1000) ? ESC_BIT_1 : ESC_BIT_0;
  107. esc_cmd[4] = (value & 0x0800) ? ESC_BIT_1 : ESC_BIT_0;
  108. esc_cmd[5] = (value & 0x0400) ? ESC_BIT_1 : ESC_BIT_0;
  109. esc_cmd[6] = (value & 0x0200) ? ESC_BIT_1 : ESC_BIT_0;
  110. esc_cmd[7] = (value & 0x0100) ? ESC_BIT_1 : ESC_BIT_0;
  111. esc_cmd[8] = (value & 0x0080) ? ESC_BIT_1 : ESC_BIT_0;
  112. esc_cmd[9] = (value & 0x0040) ? ESC_BIT_1 : ESC_BIT_0;
  113. esc_cmd[10] = (value & 0x0020) ? ESC_BIT_1 : ESC_BIT_0;
  114. esc_cmd[11] = (value & 0x0010) ? ESC_BIT_1 : ESC_BIT_0;
  115. esc_cmd[12] = (value & 0x8) ? ESC_BIT_1 : ESC_BIT_0;
  116. esc_cmd[13] = (value & 0x4) ? ESC_BIT_1 : ESC_BIT_0;
  117. esc_cmd[14] = (value & 0x2) ? ESC_BIT_1 : ESC_BIT_0;
  118. esc_cmd[15] = (value & 0x1) ? ESC_BIT_1 : ESC_BIT_0;
  119. DMA_Cmd(DMA_Streamx, ENABLE);// enable DMA channel 6
  120. }
  121. //void PWM_GPIO_Config(void)
  122. //{
  123. // GPIO_InitTypeDef GPIO_InitStructure;
  124. //
  125. // //使能GPIO时钟
  126. // PWM1_ClockCmd();
  127. // PWM2_ClockCmd();
  128. // PWM3_ClockCmd();
  129. // PWM4_ClockCmd();
  130. // GPIO_PinAFConfig(PWM1_PORT, PWM1_PinSource, PWM1_GPIO_AF);
  131. // GPIO_PinAFConfig(PWM2_PORT, PWM2_PinSource, PWM2_GPIO_AF);
  132. // GPIO_PinAFConfig(PWM3_PORT, PWM3_PinSource, PWM3_GPIO_AF);
  133. // GPIO_PinAFConfig(PWM4_PORT, PWM4_PinSource, PWM4_GPIO_AF);
  134. //
  135. // GPIO_InitStructure.GPIO_Pin = PWM1_PIN;
  136. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 复用功能
  137. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; // 速度50MHz GPIO_Speed_50MHz
  138. // GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 推挽复用输出
  139. // GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 上拉 GPIO_PuPd_UP
  140. // GPIO_Init(PWM1_PORT,&GPIO_InitStructure); // 初始化
  141. // GPIO_InitStructure.GPIO_Pin = PWM2_PIN;
  142. // GPIO_Init(PWM2_PORT,&GPIO_InitStructure);
  143. //
  144. // GPIO_InitStructure.GPIO_Pin = PWM3_PIN;
  145. // GPIO_Init(PWM3_PORT,&GPIO_InitStructure);
  146. //
  147. // GPIO_InitStructure.GPIO_Pin = PWM4_PIN;
  148. // GPIO_Init(PWM4_PORT,&GPIO_InitStructure);
  149. //}
  150. //void PWM_TIM_Config(u16 arr, u16 psc, u16 Pulse)
  151. //{
  152. // TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  153. // TIM_OCInitTypeDef TIM_OCInitStructure;
  154. //
  155. // TIM_TimeBaseStructure.TIM_Prescaler = psc; //定时器分频
  156. // TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
  157. // TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载值
  158. // TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  159. // TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  160. //
  161. // TIM_TimeBaseInit(PWM1_TIM,&TIM_TimeBaseStructure);//初始化定时器
  162. // TIM_TimeBaseInit(PWM2_TIM,&TIM_TimeBaseStructure);
  163. // TIM_TimeBaseInit(PWM3_TIM,&TIM_TimeBaseStructure);
  164. // TIM_TimeBaseInit(PWM4_TIM,&TIM_TimeBaseStructure);
  165. //
  166. // TIM_OCInitStructure.TIM_Pulse = Pulse;
  167. // TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
  168. // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
  169. // TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
  170. //
  171. // PWM1_OCInit(TIM_OCInitStructure);
  172. // PWM2_OCInit(TIM_OCInitStructure);
  173. // PWM3_OCInit(TIM_OCInitStructure);
  174. // PWM4_OCInit(TIM_OCInitStructure);
  175. //
  176. // TIM_ARRPreloadConfig(PWM1_TIM,ENABLE);//ARPE使能
  177. // TIM_ARRPreloadConfig(PWM2_TIM,ENABLE);
  178. // TIM_ARRPreloadConfig(PWM3_TIM,ENABLE);
  179. // TIM_ARRPreloadConfig(PWM4_TIM,ENABLE);
  180. //
  181. // TIM_Cmd(PWM1_TIM, ENABLE);
  182. // TIM_Cmd(PWM2_TIM, ENABLE);
  183. // TIM_Cmd(PWM3_TIM, ENABLE);
  184. // TIM_Cmd(PWM4_TIM, ENABLE);
  185. //}
  186. //void DSHOT_NVIC_Config(void)
  187. //{
  188. // NVIC_InitTypeDef NVIC_InitStructure;
  189. //
  190. // //DMA中断设置
  191. // NVIC_InitStructure.NVIC_IRQChannel = PWM1_DMA_Stream_IRQn; //DMA2_Stream0中断
  192. // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级1
  193. // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级1
  194. // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
  195. // NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
  196. //
  197. // NVIC_InitStructure.NVIC_IRQChannel = PWM2_DMA_Stream_IRQn;
  198. // NVIC_Init(&NVIC_InitStructure);
  199. //
  200. // NVIC_InitStructure.NVIC_IRQChannel = PWM3_DMA_Stream_IRQn;
  201. // NVIC_Init(&NVIC_InitStructure);
  202. //
  203. // NVIC_InitStructure.NVIC_IRQChannel = PWM4_DMA_Stream_IRQn;
  204. // NVIC_Init(&NVIC_InitStructure);
  205. //}
  206. //void OutPutPWM_Config(void)
  207. //{
  208. // PWM_GPIO_Config();
  209. //
  210. // PWM_TIM_Config(70-1,4-1,0); //DSHOT_1200
  211. //
  212. // DSHOT_NVIC_Config();
  213. //}