初级会员

- 积分
- 61
- 金钱
- 61
- 注册时间
- 2019-8-1
- 在线时间
- 14 小时
|
1金钱
本帖最后由 jinnxf 于 2021-5-20 11:21 编辑
利用原子哥的stm32f411飞控中的ws2812代码,修改端口为PA1,TIM5-CH2,DMA1-CH6-STREAM4,无法点亮,没有输出,但是用端口PC6,TIM3-CH1,DMA1-CH5-STREAM4是正常的,附代码如下,看了一下午不知哪里配置的不对,请各位大神帮忙过过目。- #define TIMING_ONE 80 //80 0.8us电平时间
- #define TIMING_ZERO 30 //30 0.3us电平时间
- //#define TIMING_ONE 143 //80 0.8us电平时间
- //#define TIMING_ZERO 67 //30 0.3us电平时间
- u16 dmaBuffer0[24];
- u16 dmaBuffer1[24];
- static bool isInit = false;
- static xSemaphoreHandle allLedDone = NULL;
- void ws2812Init_SIG2(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- DMA_InitTypeDef DMA_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能TIM5时钟
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); //使能DMA时钟
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PA1
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //GPIO_PuPd_NOPULL
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM5); //配置PA1为定时器5复用
-
- TIM_TimeBaseStructure.TIM_Period = (100 - 1); //800KHz 84M / 100
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
-
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_Pulse = 0;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
- TIM_OC2Init(TIM5, &TIM_OCInitStructure);
-
- TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);
- TIM_DMACmd(TIM5, TIM_DMA_CC2, ENABLE);
-
-
- DMA_DeInit(DMA1_Stream4);
- DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&TIM5->CCR2;
- DMA_InitStructure.DMA_Memory0BaseAddr = (u32)dmaBuffer0;
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
- DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
- DMA_InitStructure.DMA_BufferSize = 24;
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
- DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
- DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
- DMA_InitStructure.DMA_Priority = DMA_Priority_High;
- DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
- DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_1QuarterFull ;
- DMA_InitStructure.DMA_Channel = DMA_Channel_6;
-
- DMA_DoubleBufferModeCmd(DMA1_Stream4, ENABLE);//使能双缓冲
- DMA_DoubleBufferModeConfig(DMA1_Stream4, (u32)dmaBuffer1, DMA_Memory_0);
- DMA_Init(DMA1_Stream4, &DMA_InitStructure);
- DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream4_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 9;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- if(!isInit)
- {
- vSemaphoreCreateBinary(allLedDone);
- }
- else
- {
- xSemaphoreGive(allLedDone);
- }
-
- // ws2812PowerControl(true);
- isInit = true;
- }
- static void fillLed(u16 *buffer, u8 *color)
- {
- int i;
- for(i=0; i<8; i++) // GREEN
- {
- buffer[i] = ((color[1]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
- }
- for(i=0; i<8; i++) // RED
- {
- buffer[8+i] = ((color[0]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
- }
- for(i=0; i<8; i++) // BLUE
- {
- buffer[16+i] = ((color[2]<<i) & 0x0080) ? TIMING_ONE:TIMING_ZERO;
- }
- }
- static int current_led = 0;
- static int total_led = 0;
- static u8(*color_led)[3] = NULL;
- //ws2812颜色发送至DMA
- void ws2812Send(u8 (*color)[3], u16 len)
- {
- if(len<1) return;
- xSemaphoreTake(allLedDone, portMAX_DELAY);//等待上一次发送完成
- current_led = 0;
- total_led = len;
- color_led = color;
-
- fillLed(dmaBuffer0, color_led[current_led]);
- current_led++;
- fillLed(dmaBuffer1, color_led[current_led]);
- current_led++;
-
- DMA_Cmd(DMA1_Stream4, ENABLE); //使能DMA
- TIM_Cmd(TIM5, ENABLE); //使能定时器
- }
- void ws2812DmaIsr(void)
- {
- portBASE_TYPE xHigherPriorityTaskWoken;
- if (total_led == 0)
- {
- TIM_Cmd(TIM5, DISABLE);
- DMA_Cmd(DMA1_Stream4, DISABLE);
- }
-
- if (DMA_GetITStatus(DMA1_Stream4, DMA_IT_TCIF4))
- {
- DMA_ClearITPendingBit(DMA1_Stream4, DMA_IT_TCIF4);
- if(DMA_GetCurrentMemoryTarget(DMA1_Stream4) == DMA_Memory_0)//DMA当前使用内存0
- {
- if (current_led<total_led)
- fillLed(dmaBuffer1, color_led[current_led]);
- else
- memset(dmaBuffer1, 0, sizeof(dmaBuffer1));
- }
- else//DMA当前使用内存1
- {
- if (current_led<total_led)
- fillLed(dmaBuffer0, color_led[current_led]);
- else
- memset(dmaBuffer0, 0, sizeof(dmaBuffer0));
- }
- current_led++;
- }
- if (current_led >= total_led + 2) //多传输2个LED产生60us的低电平
- {
- xSemaphoreGiveFromISR(allLedDone, &xHigherPriorityTaskWoken);
- TIM_Cmd(TIM5, DISABLE);
- DMA_Cmd(DMA1_Stream4, DISABLE);
- total_led = 0;
- }
- }
- void DMA1_Stream4_IRQHandler(void)
- {
- ws2812DmaIsr();
- // else if(getModuleID() == NO_MODULE)
- // {
- // if(lastModuleID == LED_RING)
- // {
- // ws2812DmaIsr();
- // DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, DISABLE);
- // DMA_Cmd(DMA1_Stream4,DISABLE);
- // }
- // else if(lastModuleID == OPTICAL_FLOW)
- // {
- // spiTxDmaIsr();
- // DMA_ITConfig(DMA1_Stream3, DMA_IT_TC, DISABLE);
- // DMA_ITConfig(DMA1_Stream4, DMA_IT_TC, DISABLE);
- // DMA_Cmd(DMA1_Stream3,DISABLE);
- // DMA_Cmd(DMA1_Stream4,DISABLE);
- // }
- // }
- }
复制代码
|
|