#include "USART1.h" // USARTs common settings : Speed and Timeouts const uint16_t USART_UBRR[8] PROGMEM={ (F_CPU/8UL)/19200-1, (F_CPU/8UL)/500000-1, (F_CPU/8UL)/2400-1, (F_CPU/8UL)/9600-1, (F_CPU/8UL)/115200-1, (F_CPU/8UL)/250000-1, (F_CPU/8UL)/1000000-1, /*(F_CPU/8UL)/10000000-1*/ 0}; // Fosc=16MHz : 19.2k, 500k, 2.4k, 9.6k, 115.2k, 250k, 1M, 1M const uint16_t USART_TIMEOUT[8] PROGMEM={ (IDIBUS_19200B_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_500K_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_2400B_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_9600B_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_115200B_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_250K_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_1M_INTERFRAME_TIMEOUT_US/8*16), (IDIBUS_1M_INTERFRAME_TIMEOUT_US/8*16) }; //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ISR(USART1_TX_vect){ if (USART1STR.TxSendedCount == USART1STR.TxBufCount){ *USART1STR.USART->UCRSB |= (1<UCRSB &= ~((1< Z USART1_DRE_DDR&=~(1< 0 } else { *USART1STR.USART->UDR=USART1STR.TX_BUF[USART1STR.TxSendedCount]; USART1STR.TxSendedCount++; } } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ISR(USART1_RX_vect){ TIMSK3=0; TCNT3=0; // Timeout Timer3 On, clkI/O/8 (From prescaller) TCCR3B=2; TIMSK3=1<UCRSA & ((1<UDR; USART1STR.RxBufCount++; } } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ISR(TIMER3_COMPA_vect) { TCCR3B=0; TCNT3=0; TIMSK3=0; // Timeout TIMER3 off *USART1STR.USART->UCRSB&=~((1< Z USART1_RX_DDR&=~(1< Z USART1_DRE_DDR&=~(1< 0 *USART1STR.USART->UCRSA=(1<UCRSB=(1<UCRSC=(1<UBRR = (F_CPU/16UL)/Baudrate-1; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint8_t USART1_IsNewRxMessage(void) { return USART1STR.RxComplete; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint8_t USART1_IsRxError(void) { return USART1STR.RxError; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint16_t USART1_getRxBufSize(void) { return USART1STR.RxBufCount; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint8_t *USART1_getRxBuf(void) { return &USART1STR.RX_BUF[0]; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint8_t *USART1_getTxBuf(void) { return &USART1STR.TX_BUF[0]; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void USART1_copyRxBuf(uint8_t *Dst, uint16_t StartPos, uint16_t Count) { memcpy(Dst, &USART1STR.RX_BUF[StartPos], Count); } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- uint8_t USART1_IsTxActive(void){ if (USART1STR.TxComplete) return 0; else return 1; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void USART1_SetIdiBusBoudrate(enum IDIBUS_SERIAL_BAUDRATE BoudrateCode){ *USART1STR.USART->UBRR = pgm_read_word(&USART_UBRR[BoudrateCode]); USART1STR.InterFrameTimeoutTicks = pgm_read_word(&USART_TIMEOUT[BoudrateCode]); OCR3A = USART1STR.InterFrameTimeoutTicks; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void USART1_SendByteBuf(uint8_t *Buf, uint16_t Count){ if ( (!USART1STR.TxComplete) || (Count == 0) || (Count > USART1_BUF_SIZE) ) { return; } memcpy(USART1STR.TX_BUF, Buf, Count); USART1_TX_DDR|=1< out USART1_DRE_DDR|=1<UCRSB&=~((1<UCRSB|=(1<UDR = USART1STR.TX_BUF[0]; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void USART1_SendTxBuf(uint16_t Count){ if ( (!USART1STR.TxComplete) || (Count == 0) || (Count > USART1_BUF_SIZE) ) { return; } USART1_TX_DDR|=1< out USART1_DRE_DDR|=1<UCRSB&=~((1<UCRSB|=(1<UDR = USART1STR.TX_BUF[0]; } //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- void USART1_RxTransferRestart(void){ //USART1_RX_TIMER_STOP(); //We are already doing this in IRQ //(void) USART1STR.USART->SR; //(void) USART1STR.USART->DR; *USART1STR.USART->UCRSB|=(1<CR1 |= (1U<