Merge pull request #3031 from sambas/betaflightF7

HAL ledstrip N channel support
This commit is contained in:
Martin Budden 2017-05-06 11:02:51 +01:00 committed by GitHub
commit 63bae61e64
1 changed files with 35 additions and 27 deletions

View File

@ -36,11 +36,11 @@ bool ws2811Initialised = false;
static TIM_HandleTypeDef TimHandle; static TIM_HandleTypeDef TimHandle;
static uint16_t timerChannel = 0; static uint16_t timerChannel = 0;
static bool timerNChannel = false;
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{ {
if(htim->Instance == TimHandle.Instance) if(htim->Instance == TimHandle.Instance) {
{
//HAL_TIM_PWM_Stop_DMA(&TimHandle,WS2811_TIMER_CHANNEL); //HAL_TIM_PWM_Stop_DMA(&TimHandle,WS2811_TIMER_CHANNEL);
ws2811LedDataTransferInProgress = 0; ws2811LedDataTransferInProgress = 0;
} }
@ -77,8 +77,7 @@ void ws2811LedStripHardwareInit(ioTag_t ioTag)
TimHandle.Init.Period = period; // 800kHz TimHandle.Init.Period = period; // 800kHz
TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
{
/* Initialization Error */ /* Initialization Error */
return; return;
} }
@ -117,8 +116,7 @@ void ws2811LedStripHardwareInit(ioTag_t ioTag)
dmaSetHandler(timerHardware->dmaIrqHandler, WS2811_DMA_IRQHandler, NVIC_PRIO_WS2811_DMA, dmaSource); dmaSetHandler(timerHardware->dmaIrqHandler, WS2811_DMA_IRQHandler, NVIC_PRIO_WS2811_DMA, dmaSource);
/* Initialize TIMx DMA handle */ /* Initialize TIMx DMA handle */
if(HAL_DMA_Init(TimHandle.hdma[dmaSource]) != HAL_OK) if(HAL_DMA_Init(TimHandle.hdma[dmaSource]) != HAL_OK) {
{
/* Initialization Error */ /* Initialization Error */
return; return;
} }
@ -127,15 +125,21 @@ void ws2811LedStripHardwareInit(ioTag_t ioTag)
/* PWM1 Mode configuration: Channel1 */ /* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.OCMode = TIM_OCMODE_PWM1; TIM_OCInitStructure.OCMode = TIM_OCMODE_PWM1;
TIM_OCInitStructure.Pulse = 0; if (timerHardware->output & TIMER_OUTPUT_N_CHANNEL) {
TIM_OCInitStructure.OCPolarity = TIM_OCPOLARITY_HIGH; timerNChannel = true;
TIM_OCInitStructure.OCNPolarity = TIM_OCPOLARITY_HIGH;
TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_RESET; TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_RESET;
TIM_OCInitStructure.OCPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TIM_OCPOLARITY_HIGH: TIM_OCPOLARITY_LOW;
TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET; TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
TIM_OCInitStructure.OCNPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TIM_OCNPOLARITY_HIGH : TIM_OCNPOLARITY_LOW;
} else {
TIM_OCInitStructure.OCIdleState = TIM_OCIDLESTATE_RESET;
TIM_OCInitStructure.OCPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TIM_OCPOLARITY_LOW : TIM_OCPOLARITY_HIGH;
TIM_OCInitStructure.OCNIdleState = TIM_OCNIDLESTATE_RESET;
TIM_OCInitStructure.OCNPolarity = (timerHardware->output & TIMER_OUTPUT_INVERTED) ? TIM_OCNPOLARITY_LOW : TIM_OCNPOLARITY_HIGH;
}
TIM_OCInitStructure.Pulse = 0;
TIM_OCInitStructure.OCFastMode = TIM_OCFAST_DISABLE; TIM_OCInitStructure.OCFastMode = TIM_OCFAST_DISABLE;
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &TIM_OCInitStructure, timerChannel) != HAL_OK) {
if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &TIM_OCInitStructure, timerChannel) != HAL_OK)
{
/* Configuration Error */ /* Configuration Error */
return; return;
} }
@ -143,21 +147,25 @@ void ws2811LedStripHardwareInit(ioTag_t ioTag)
ws2811Initialised = true; ws2811Initialised = true;
} }
void ws2811LedStripDMAEnable(void) void ws2811LedStripDMAEnable(void)
{ {
if (!ws2811Initialised) if (!ws2811Initialised) {
{
ws2811LedDataTransferInProgress = 0; ws2811LedDataTransferInProgress = 0;
return; return;
} }
if (HAL_TIM_PWM_Start_DMA(&TimHandle, timerChannel, ledStripDMABuffer, WS2811_DMA_BUFFER_SIZE) != HAL_OK) if(timerNChannel) {
{ if(HAL_TIMEx_PWMN_Start_DMA(&TimHandle, timerChannel, ledStripDMABuffer, WS2811_DMA_BUFFER_SIZE) != HAL_OK) {
/* Starting PWM generation Error */ /* Starting PWM generation Error */
ws2811LedDataTransferInProgress = 0; ws2811LedDataTransferInProgress = 0;
return; return;
} }
} else {
if (HAL_TIM_PWM_Start_DMA(&TimHandle, timerChannel, ledStripDMABuffer, WS2811_DMA_BUFFER_SIZE) != HAL_OK) {
/* Starting PWM generation Error */
ws2811LedDataTransferInProgress = 0;
return;
}
}
} }
#endif #endif