From 7f2a5cafd3086b7e5a48c804fb21bedd92acb54c Mon Sep 17 00:00:00 2001 From: janek Date: Tue, 10 Dec 2024 21:05:47 +0100 Subject: [PATCH] Disable IDLE interrupt for telemetry, cleanup buzzer init, --- radio/src/targets/flysky/board.cpp | 32 +++++------------- radio/src/targets/flysky/board.h | 1 + radio/src/targets/flysky/telemetry_driver.cpp | 22 ++++++++----- radio/src/telemetry/telemetry.cpp | 33 ++++++++++++++----- 4 files changed, 46 insertions(+), 42 deletions(-) diff --git a/radio/src/targets/flysky/board.cpp b/radio/src/targets/flysky/board.cpp index dddcd3ce2f..bb6e096154 100644 --- a/radio/src/targets/flysky/board.cpp +++ b/radio/src/targets/flysky/board.cpp @@ -49,10 +49,6 @@ void buzzerInit() GPIO_PinAFConfig(BUZZER_GPIO_PORT, BUZZER_GPIO_PinSource, GPIO_AF_2); } -void referenceSystemAudioFiles() -{ -} - #define __HAL_SYSCFG_REMAPMEMORY_SYSTEMFLASH() do {SYSCFG->CFGR1 &= ~(SYSCFG_CFGR1_MEM_MODE); \ SYSCFG->CFGR1 |= SYSCFG_CFGR1_MEM_MODE_0; \ }while(0) @@ -95,26 +91,14 @@ void watchdogInit(unsigned int duration) void initBuzzerTimer() { - PWM_TIMER->PSC = 48 - 1; // 48MHz -> 1MHz - /* set counter mode */ - PWM_TIMER->CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS); - PWM_TIMER->CR1 |= TIM_CounterMode_Up; - /* Auto-Reload Register */ - PWM_TIMER->ARR = 400; // count up to - /* Set Clock Division */ - PWM_TIMER->CR1 &= ~ TIM_CR1_CKD; - PWM_TIMER->CR1 |= TIM_CKD_DIV1; - PWM_TIMER->CCR1 = 200; // ARR/2 = PWM duty 50% - /* Set repetition counter */ - PWM_TIMER->RCR = 0; - - // Timer output mode PWM - /* Select the Output Compare (OC) Mode 1 */ - PWM_TIMER->CCMR1 |= (TIM_CCMR1_OC1M_2 | TIM_CCMR1_OC1M_1); // = TIM_OCMODE_PWM1 - /* Reset and set the Output N Polarity level to LOW */ - // TIM1->CCER &= ~TIM_CCER_CC1P; - PWM_TIMER->CCER |= TIM_CCER_CC1P | TIM_CCER_CC1E; // = TIM_OCPOLARITY_LOW + enable Capture compare channel - /* Enable the main output */ + PWM_TIMER->PSC = 48 - 1; // 48MHz -> 1MHz + PWM_TIMER->CR1 &= ~(TIM_CR1_DIR | TIM_CR1_CMS | TIM_CR1_CKD); + PWM_TIMER->CR1 |= TIM_CounterMode_Up | TIM_CKD_DIV1; + PWM_TIMER->ARR = 400; // count up to + PWM_TIMER->CCR1 = 200; // ARR/2 = PWM duty 50% + // PWM_TIMER->RCR = 0; + PWM_TIMER->CCMR1 |= TIM_OCMode_PWM1; + PWM_TIMER->CCER |= TIM_OCPolarity_Low | TIM_CCER_CC1E; // TIM_OCPOLARITY_LOW + enable Capture compare channel PWM_TIMER->BDTR |= TIM_BDTR_MOE; } diff --git a/radio/src/targets/flysky/board.h b/radio/src/targets/flysky/board.h index fd5042d509..7e0882083c 100644 --- a/radio/src/targets/flysky/board.h +++ b/radio/src/targets/flysky/board.h @@ -423,6 +423,7 @@ void telemetryPortSetDirectionOutput(void); //void sportSendBuffer(uint8_t * buffer, uint32_t count); void sportSendBuffer(const uint8_t* buffer, unsigned long count); uint8_t telemetryGetByte(uint8_t * byte); +bool telemetryIsEmpty(); // extern uint32_t telemetryErrors; extern volatile bool pendingTelemetryPollFrame; diff --git a/radio/src/targets/flysky/telemetry_driver.cpp b/radio/src/targets/flysky/telemetry_driver.cpp index a9d36231f1..eb131b90d9 100644 --- a/radio/src/targets/flysky/telemetry_driver.cpp +++ b/radio/src/targets/flysky/telemetry_driver.cpp @@ -67,7 +67,7 @@ void telemetryPortInit(uint32_t baudrate, uint8_t mode) { USART_DeInit(TELEMETRY_USART); // OverSampling + IDLE - TELEMETRY_USART->CR1 |= ( USART_CR1_OVER8 | USART_CR1_IDLEIE ); + TELEMETRY_USART->CR1 |= ( USART_CR1_OVER8 /*| USART_CR1_IDLEIE*/ ); GPIO_PinAFConfig(TELEMETRY_GPIO, TELEMETRY_GPIO_PinSource_TX, TELEMETRY_TX_GPIO_AF); @@ -121,7 +121,7 @@ void telemetryPortSetDirectionOutput() { // Disable RX #if !defined(CRSF_FULLDUPLEX) TELEMETRY_DMA_Channel_RX->CCR &= ~DMA_CCR_EN; - TELEMETRY_USART->CR1 &= ~USART_CR1_RE; + TELEMETRY_USART->CR1 &= ~(USART_CR1_RE /* | USART_CR1_IDLEIE*/); #endif // Enable TX @@ -136,7 +136,7 @@ void telemetryPortSetDirectionInput() { #endif // Enable RX - TELEMETRY_USART->CR1 |= USART_CR1_RE; + TELEMETRY_USART->CR1 |= (USART_CR1_RE/* | USART_CR1_IDLEIE*/); TELEMETRY_DMA_Channel_RX->CCR |= DMA_CCR_EN; } @@ -192,15 +192,19 @@ extern "C" void TELEMETRY_USART_IRQHandler(void) { // RX, handled by DMA - // IDLE - if (status & USART_FLAG_IDLE) { - TELEMETRY_USART->ICR = USART_ICR_IDLECF; - pendingTelemetryPollFrame = true; - TELEMETRY_USART->CR1 |= USART_CR1_IDLEIE; - } + // TODO IDLE disabled, it is always triggering, spamming ISR +// if ((TELEMETRY_USART->CR1 & USART_CR1_IDLEIE) && (status & USART_ISR_IDLE)) { +// TRACE_NOCRLF("x"); +// pendingTelemetryPollFrame = true; +// TELEMETRY_USART->ICR = USART_ICR_IDLECF; +// } } // TODO we should have telemetry in an higher layer, functions above should move to a sport_driver.cpp uint8_t telemetryGetByte(uint8_t* byte) { return telemetryDMAFifo.pop(*byte); } + +bool telemetryIsEmpty() { + return telemetryDMAFifo.isEmpty(); +} diff --git a/radio/src/telemetry/telemetry.cpp b/radio/src/telemetry/telemetry.cpp index e1a5eb1915..9c2a00fa1a 100644 --- a/radio/src/telemetry/telemetry.cpp +++ b/radio/src/telemetry/telemetry.cpp @@ -33,6 +33,8 @@ uint8_t telemetryProtocol = 255; volatile bool pendingTelemetryPollFrame = false; +extern DMAFifo telemetryDMAFifo __DMA; + void processTelemetryData(uint8_t data) { @@ -76,6 +78,16 @@ void telemetryWakeup() telemetryInit(requiredTelemetryProtocol); } +#if defined(CROSSFIRE) + if (telemetryProtocol == PROTOCOL_TELEMETRY_CROSSFIRE && !telemetryIsEmpty()) { + uint8_t data; + while (telemetryGetByte(&data)) { + processCrossfireTelemetryData(data); // TODO handle full frame as in EdgeTX + LOG_TELEMETRY_WRITE_BYTE(data); + } + } +#endif + // Handle complete telemetry frame if (pendingTelemetryPollFrame) { pendingTelemetryPollFrame = false; @@ -86,15 +98,18 @@ void telemetryWakeup() break; #endif #if defined(CROSSFIRE) - case PROTOCOL_TELEMETRY_CROSSFIRE: - { - uint8_t data; - while (telemetryGetByte(&data)) { - processCrossfireTelemetryData(data); // TODO handle full frame as in EdgeTX - LOG_TELEMETRY_WRITE_BYTE(data); - } - } - break; + // case PROTOCOL_TELEMETRY_CROSSFIRE: + // { + // uint8_t data; + // uint8_t count = 0; + // while (telemetryGetByte(&data)) { + // processCrossfireTelemetryData(data); // TODO handle full frame as in EdgeTX + // LOG_TELEMETRY_WRITE_BYTE(data); + // count++; + // } + // TRACE("c%d", count); + // } + // break; #endif default: // TRACE("Unhandled telemetry %d", telemetryProtocol);