Saturday, 15 February 2014

AVR USART Queue Snippet

This snippet defines queue buffers to be used with the USART RX/TX for the Atmel AVR ATmega 8/32, it uses interruptions to release the MCU from continuous polling. This was based on AVR306 app note source code from Atmel.

Defines & Includes:
#define F_CPU 16000000
#define BAUD 9600

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/interrupt.h>

Source:
#define USART_RX_SIZE   64  // Receive queue max size
#define USART_TX_SIZE   64  // Transmit queue max size

#define USART_RX_MASK ( USART_RX_SIZE - 1 )
#define USART_TX_MASK ( USART_TX_SIZE - 1 )

static uint8_t USART_RxBuf  [USART_RX_SIZE];
static uint8_t USART_TxBuf  [USART_TX_SIZE];

int USART_Tx_File(char ch, FILE *stream);
static FILE USART_StdOut = FDEV_SETUP_STREAM(USART_Tx_File, NULL, _FDEV_SETUP_WRITE);

static volatile uint8_t USART_RxHead;
static volatile uint8_t USART_RxTail;
static volatile uint8_t USART_TxHead;
static volatile uint8_t USART_TxTail;

void (*USART_RxCallback)(uint8_t ch) = NULL;

void USART_Init(long baudrate){
    long baud_setting = (F_CPU / 8 / baudrate - 1) / 2;

    memset(USART_RxBuf,0,USART_RX_SIZE);
    memset(USART_TxBuf,0,USART_TX_SIZE);

    // Set STDOUT to custom file stream.
    stdout = &USART_StdOut;

    UBRRH = (baud_setting >> 8);
    UBRRL = baud_setting;

    // Don't use U2X
    UCSRA &= ~(1 << U2X);
    // Enable TX(TXEN), Enable RX(RXEN), Enable RX Interrupt(RXCIE)
    UCSRB =  (1<<TXEN) | (1<<RXEN) | (1<<RXCIE);
     // Asyncronous, Write to  UCSRC(URSEL), 8 bit
    UCSRC = (1<<URSEL) | (3<<UCSZ0);

    USART_RxTail = 0;
    USART_RxHead = 0;
    USART_TxTail = 0;
    USART_TxHead = 0;
}

void USART_Tx(uint8_t data){
    uint8_t tmphead;
    tmphead = ( USART_TxHead + 1 ) & USART_TX_MASK;
    // Wait while full
    while ( tmphead == USART_TxTail ){};
    USART_TxBuf[tmphead] = data;
    USART_TxHead = tmphead;
    // If the UART ain't busy sending bytes, send the first byte.
    if((UCSRB & (1<<TXCIE)) != (1<<TXCIE)){
        // Enable Tx interrupt for the following bytes
        UCSRB |= (1<<TXCIE);
         // Emulate the TX interruption for the first character
        uint8_t tmptail = ( USART_TxTail + 1 ) & USART_TX_MASK;
        USART_TxTail = tmptail;
        UDR = USART_TxBuf[tmptail];
    }
}

int USART_Tx_File(char ch, FILE *stream){
    USART_Tx(ch);
    return 0;
}

uint8_t USART_RxByte(void){
    uint8_t tmptail;
    while ( USART_RxHead == USART_RxTail ){};
    tmptail = ( USART_RxTail + 1 ) & USART_RX_MASK;
    USART_RxTail = tmptail;
    return USART_RxBuf[tmptail];
}

ISR(USART_RXC_vect){
    uint8_t tmphead;
    uint8_t data = UDR;
    tmphead = ( USART_RxHead + 1 ) & USART_RX_MASK;
    USART_RxHead = tmphead;
    if ( tmphead != USART_RxTail )
        USART_RxBuf[tmphead] = data;
    if( USART_RxCallback != NULL )
        USART_RxCallback(data);
}

ISR(USART_TXC_vect){
    uint8_t tmptail;
    if ( USART_TxHead != USART_TxTail ){
        tmptail = ( USART_TxTail + 1 ) & USART_TX_MASK;
        USART_TxTail = tmptail;
        UDR = USART_TxBuf[tmptail];
    }
    else{
        // Empty queue, disable interrupt.
        UCSRB &= ~(1<<TXCIE);  
    }
}

Updated: 2014/12/20, changed to use stdout and removed malloc usage.

No comments: