From 94f1efaab73304094228edc8b3763ac9a987db68 Mon Sep 17 00:00:00 2001 From: midelic Date: Sat, 27 Aug 2016 10:43:59 +0100 Subject: [PATCH] reorganize telemetry code bash serial --- Multiprotocol/Telemetry.ino | 378 ++++++++++++++++++++++++++++++++++++ 1 file changed, 378 insertions(+) diff --git a/Multiprotocol/Telemetry.ino b/Multiprotocol/Telemetry.ino index ccd1c3b..417d579 100644 --- a/Multiprotocol/Telemetry.ino +++ b/Multiprotocol/Telemetry.ino @@ -446,4 +446,382 @@ void frskyUpdate() } + +//Serial TX + #ifdef XMEGA + ISR(USARTC0_DRE_vect) + #else + #if defined STM32_board + #ifdef __cplusplus + extern "C" { + #endif + void __irq_usart3() + #else + ISR(USART_UDRE_vect) + #endif + #endif + + { // Transmit interrupt + #if defined STM32_board + if(USART3_BASE->SR & USART_SR_TXE) { + #endif + if(tx_head!=tx_tail) + { + if(++tx_tail>=TXBUFFER_SIZE)//head + tx_tail=0; + #ifdef XMEGA + USARTC0.DATA = tx_buff[tx_tail] ; + #else + #if defined STM32_board + USART3_BASE->DR=tx_buff[tx_tail];//clears TXE bit + #else + UDR0=tx_buff[tx_tail]; + #endif + #endif + } + if (tx_tail == tx_head) + #ifdef XMEGA + USARTC0.CTRLA &= ~0x03 ; + #else + #if defined STM32_board + USART3_BASE->CR1 &= ~USART_CR1_TXEIE;//disable interrupt + } + #else + UCSR0B &= ~(1<=TXBUFFER_SIZE) + tx_head=0; + tx_buff[tx_head]=data; + #ifdef XMEGA + USARTC0.CTRLA = (USARTC0.CTRLA & 0xFC) | 0x01 ; + #else + #if defined STM32_board + USART3_BASE->CR1 |= USART_CR1_TXEIE; + #else + UCSR0B |= (1<CR1 |= USART_CR1_PCE_BIT; + USART3_BASE->CR1 &= ~ USART_CR1_RE;//disable + USART2_BASE->CR1 &= ~ USART_CR1_TE;//disable transmit + #else + #include + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; + UCSR0A = 0 ; // Clear X2 bit + //Set frame format to 8 data bits, even parity, 2 stop bits + UCSR0C = (1<>= 7 ; // Top bit + if ( SerialControl.speed == SPEED_100K ) + { +#ifdef INVERT_SERIAL + byteLo |= 0x02 ; // Parity bit +#else + byteLo |= 0xFC ; // Stop bits +#endif + // calc parity + temp = byte ; + temp >>= 4 ; + temp = byte ^ temp ; + temp1 = temp ; + temp1 >>= 2 ; + temp = temp ^ temp1 ; + temp1 = temp ; + temp1 <<= 1 ; + temp ^= temp1 ; + temp &= 0x02 ; +#ifdef INVERT_SERIAL + byteLo ^= temp ; +#else + byteLo |= temp ; +#endif + } + else + { + byteLo |= 0xFE ; // Stop bit + } + byte <<= 1 ; +#ifdef INVERT_SERIAL + byte |= 1 ; // Start bit +#endif + uint8_t next = (SerialControl.head + 2) & 0x3f ; + if ( next != SerialControl.tail ) + { + SerialControl.data[SerialControl.head] = byte ; + SerialControl.data[SerialControl.head+1] = byteLo ; + SerialControl.head = next ; + } + cli() ; + if ( SerialControl.busy == 0 ) + { + sei() ; + // Start the transmission here +#ifdef INVERT_SERIAL + GPIOR2 = 0 ; +#else + GPIOR2 = 0x01 ; +#endif + if ( SerialControl.speed == SPEED_100K ) + { + GPIOR1 = 1 ; + OCR0B = TCNT0 + 40 ; + OCR0A = OCR0B + 210 ; + TIFR0 = (1<>= 1 + GPIOR0 = byte ; + if ( --GPIOR1 == 0 ) + { + TIMSK0 &= ~(1<>= 1 + GPIOR2 = byte ; + if ( --GPIOR1 == 0 ) + { + // prepare next byte and allow for 2 stop bits + struct t_serial_bash *ptr = &SerialControl ; + if ( ptr->head != ptr->tail ) + { + GPIOR0 = ptr->data[ptr->tail] ; + GPIOR2 = ptr->data[ptr->tail+1] ; + ptr->tail = ( ptr->tail + 2 ) & 0x3F ; + GPIOR1 = 8 ; + OCR0A = OCR0B + 40 ; + OCR0B = OCR0A + 8 * 20 ; + TIMSK0 |= (1< 2 ) + { + byte = GPIOR0 ; + } + else + { + byte = GPIOR2 ; + } + if ( byte & 0x01 ) + { + PORTD |= 0x02 ; + } + else + { + PORTD &= ~0x02 ; + } + byte /= 2 ; // Generates shorter code than byte >>= 1 + if ( GPIOR1 > 2 ) + { + GPIOR0 = byte ; + } + else + { + GPIOR2 = byte ; + } + if ( --GPIOR1 == 0 ) + { + // prepare next byte + struct t_serial_bash *ptr = &SerialControl ; + if ( ptr->head != ptr->tail ) + { + GPIOR0 = ptr->data[ptr->tail] ; + GPIOR2 = ptr->data[ptr->tail+1] ; + ptr->tail = ( ptr->tail + 2 ) & 0x3F ; + GPIOR1 = 10 ; + } + else + { + SerialControl.busy = 0 ; + TIMSK0 &= ~(1<