uC/AVR/PIO_Project/MainControlBoard/lib/uart/uart.c
uint8_t uart_init(uint8_t uart_if, uint32_t baudrate)
{
    uint16_t ubrr;

    if(uart_if > (N_UARTS-1)){
        return 0;                         // uart > 3 doesn't exist!
    }
    
    // check if already initialized, otherwise memory will be allocated multible times
    if((uarttx[uart_if].initialized == 1) | (uartrx[uart_if].initialized == 1)){
        return 1;           // already initialized
    }

    switch(uart_if){

        case 0:
            
            ubrr = F_CPU / 16 / baudrate - 1;               // calcualte baud value
            
            UBRR0H = (uint8_t) (ubrr >> 8);
            UBRR0L = (uint8_t) (ubrr & 0xFF);               // set baud rate control register (high and low)

            UCSR0B = (1 << RXCIE0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << TXEN0);           // activate tx and rx, receive interrupt and transmit complete interrupt

            UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);         // 1 stop bit, 8 data bit, no parity

            break;


        case 1:
            ubrr = F_CPU / 16 / baudrate - 1;               // calcualte baud value
            
            UBRR1H = (uint8_t) (ubrr >> 8);
            UBRR1L = (uint8_t) (ubrr & 0xFF);               // set baud rate control register (high and low)

            UCSR1B = (1 << RXCIE1) | (1 << TXCIE1) | (1 << RXEN1) | (1 << TXEN1);           // activate tx and rx, receive interrupt and transmit complete interrupt

            UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);         // 1 stop bit, 8 data bit, no parity
            break;

        case 2: 
            ubrr = F_CPU / 16 / baudrate - 1;               // calcualte baud value
            
            UBRR2H = (uint8_t) (ubrr >> 8);
            UBRR2L = (uint8_t) (ubrr & 0xFF);               // set baud rate control register (high and low)

            UCSR2B = (1 << RXCIE2) | (1 << TXCIE2) | (1 << RXEN2) | (1 << TXEN2);           // activate tx and rx, receive interrupt and transmit complete interrupt

            UCSR2C = (1 << UCSZ21) | (1 << UCSZ20);         // 1 stop bit, 8 data bit, no parity
            break;
        
        case 3: 
            ubrr = F_CPU / 16 / baudrate - 1;               // calcualte baud value
            
            UBRR3H = (uint8_t) (ubrr >> 8);
            UBRR3L = (uint8_t) (ubrr & 0xFF);               // set baud rate control register (high and low)

            UCSR3B = (1 << RXCIE3) | (1 << TXCIE3) | (1 << RXEN3) | (1 << TXEN3);           // activate tx and rx, receive interrupt and transmit complete interrupt

            UCSR3C = (1 << UCSZ31) | (1 << UCSZ30);         // 1 stop bit, 8 data bit, no parity
            break;

        default:
            return 0;           // specified uart doesn_t exist: will fail in memory allocation
            break;
    }


    char* c = malloc(BUFFER_SIZE);  // allocate memory for buffer
    if(c != NULL){
        rbuffer_init(&uarttx[uart_if], c);
    }
    else{
        return 0;       // failed to allocate space for buffer
    }
    c = malloc(BUFFER_SIZE);
    if(c != NULL){
        rbuffer_init(&uartrx[uart_if], c);
        
    }
    else{
        free(uarttx[uart_if].buf);
        return 0;       // failed to allocate space for buffer
    }   

    
    return 1;

}
/**
 *  @brief init function for uart driver, allocates memory for transmit and receive function
 * 
 *  @param uart_if: select uart interface (uart 1 - 4)
 *  @param baudrate: select desired baudrate
 * 
 *  @return int: 0 on fail, 1 on success
 * 
 * */
@brief init function for uart driver, allocates memory for transmit and receive function

@param uart_if: select uart interface (uart 1 - 4)
@param baudrate: select desired baudrate

@return int: 0 on fail, 1 on success
@brief init function for uart driver allocates memory for transmit and receive function @param uart_if : select uart interface ( uart 1 - 4 ) @param baudrate : select desired baudrate @return int : 0 on fail 1 on success
uint8_t uart_init(uint8_t uart_if, uint32_t baudrate)
{
    uint16_t ubrr;
    if(uart_if > (N_UARTS-1)){
        return 0;                         
    }
    if((uarttx[uart_if].initialized == 1) | (uartrx[uart_if].initialized == 1)){
        return 1;           
    }
    switch(uart_if){
        case 0:
            ubrr = F_CPU / 16 / baudrate - 1;               
            UBRR0H = (uint8_t) (ubrr >> 8);
            UBRR0L = (uint8_t) (ubrr & 0xFF);               
            UCSR0B = (1 << RXCIE0) | (1 << TXCIE0) | (1 << RXEN0) | (1 << TXEN0);           
            UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);         
            break;
        case 1:
            ubrr = F_CPU / 16 / baudrate - 1;               
            UBRR1H = (uint8_t) (ubrr >> 8);
            UBRR1L = (uint8_t) (ubrr & 0xFF);               
            UCSR1B = (1 << RXCIE1) | (1 << TXCIE1) | (1 << RXEN1) | (1 << TXEN1);           
            UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);         
            break;
        case 2: 
            ubrr = F_CPU / 16 / baudrate - 1;               
            UBRR2H = (uint8_t) (ubrr >> 8);
            UBRR2L = (uint8_t) (ubrr & 0xFF);               
            UCSR2B = (1 << RXCIE2) | (1 << TXCIE2) | (1 << RXEN2) | (1 << TXEN2);           
            UCSR2C = (1 << UCSZ21) | (1 << UCSZ20);         
            break;
        case 3: 
            ubrr = F_CPU / 16 / baudrate - 1;               
            UBRR3H = (uint8_t) (ubrr >> 8);
            UBRR3L = (uint8_t) (ubrr & 0xFF);               
            UCSR3B = (1 << RXCIE3) | (1 << TXCIE3) | (1 << RXEN3) | (1 << TXEN3);           
            UCSR3C = (1 << UCSZ31) | (1 << UCSZ30);         
            break;
        default:
            return 0;           
            break;
    }
    char* c = malloc(BUFFER_SIZE);  
    if(c != NULL){
        rbuffer_init(&uarttx[uart_if], c);
    }
    else{
        return 0;       
    }
    c = malloc(BUFFER_SIZE);
    if(c != NULL){
        rbuffer_init(&uartrx[uart_if], c);
    }
    else{
        free(uarttx[uart_if].buf);
        return 0;       
    }   
    return 1;
}
uint8_t uart_init ( uint8_t uart_if , uint32_t baudrate ) { uint16_t ubrr ; if ( uart_if > ( N_UARTS - 1 ) ) { return 0 ; } if ( ( uarttx [ uart_if ] . initialized == 1 ) | ( uartrx [ uart_if ] . initialized == 1 ) ) { return 1 ; } switch ( uart_if ) { case 0 : ubrr = F_CPU / 16 / baudrate - 1 ; UBRR0H = ( uint8_t ) ( ubrr >> 8 ) ; UBRR0L = ( uint8_t ) ( ubrr & 0xFF ) ; UCSR0B = ( 1 << RXCIE0 ) | ( 1 << TXCIE0 ) | ( 1 << RXEN0 ) | ( 1 << TXEN0 ) ; UCSR0C = ( 1 << UCSZ01 ) | ( 1 << UCSZ00 ) ; break ; case 1 : ubrr = F_CPU / 16 / baudrate - 1 ; UBRR1H = ( uint8_t ) ( ubrr >> 8 ) ; UBRR1L = ( uint8_t ) ( ubrr & 0xFF ) ; UCSR1B = ( 1 << RXCIE1 ) | ( 1 << TXCIE1 ) | ( 1 << RXEN1 ) | ( 1 << TXEN1 ) ; UCSR1C = ( 1 << UCSZ11 ) | ( 1 << UCSZ10 ) ; break ; case 2 : ubrr = F_CPU / 16 / baudrate - 1 ; UBRR2H = ( uint8_t ) ( ubrr >> 8 ) ; UBRR2L = ( uint8_t ) ( ubrr & 0xFF ) ; UCSR2B = ( 1 << RXCIE2 ) | ( 1 << TXCIE2 ) | ( 1 << RXEN2 ) | ( 1 << TXEN2 ) ; UCSR2C = ( 1 << UCSZ21 ) | ( 1 << UCSZ20 ) ; break ; case 3 : ubrr = F_CPU / 16 / baudrate - 1 ; UBRR3H = ( uint8_t ) ( ubrr >> 8 ) ; UBRR3L = ( uint8_t ) ( ubrr & 0xFF ) ; UCSR3B = ( 1 << RXCIE3 ) | ( 1 << TXCIE3 ) | ( 1 << RXEN3 ) | ( 1 << TXEN3 ) ; UCSR3C = ( 1 << UCSZ31 ) | ( 1 << UCSZ30 ) ; break ; default : return 0 ; break ; } char * c = malloc ( BUFFER_SIZE ) ; if ( c != NULL ) { rbuffer_init ( & uarttx [ uart_if ] , c ) ; } else { return 0 ; } c = malloc ( BUFFER_SIZE ) ; if ( c != NULL ) { rbuffer_init ( & uartrx [ uart_if ] , c ) ; } else { free ( uarttx [ uart_if ] . buf ) ; return 0 ; } return 1 ; }
@brief init function for uart driver, allocates memory for transmit and receive function
@brief init function for uart driver allocates memory for transmit and receive function
// uart > 3 doesn't exist! // check if already initialized, otherwise memory will be allocated multible times // already initialized // calcualte baud value // set baud rate control register (high and low) // activate tx and rx, receive interrupt and transmit complete interrupt // 1 stop bit, 8 data bit, no parity // calcualte baud value // set baud rate control register (high and low) // activate tx and rx, receive interrupt and transmit complete interrupt // 1 stop bit, 8 data bit, no parity // calcualte baud value // set baud rate control register (high and low) // activate tx and rx, receive interrupt and transmit complete interrupt // 1 stop bit, 8 data bit, no parity // calcualte baud value // set baud rate control register (high and low) // activate tx and rx, receive interrupt and transmit complete interrupt // 1 stop bit, 8 data bit, no parity // specified uart doesn_t exist: will fail in memory allocation // allocate memory for buffer // failed to allocate space for buffer // failed to allocate space for buffer