atmelrs485transmission

Can't send and receive data from my own usart - SAM4SD16C


I'm trying to use USART 0 on the SAM4SD16C processor. I got some help to start, from this website : SAM4S Xplained USART

Here's the code I'm using (which compiles):

#define COM_Handler             USART0_Handler

#define USART_SERIAL                 USART0
#define USART_SERIAL_ID              ID_USART0  
#define USART_SERIAL_BAUDRATE        115200
#define USART_SERIAL_CHAR_LENGTH     US_MR_CHRL_8_BIT
#define USART_SERIAL_PARITY          US_MR_PAR_NO
#define USART_SERIAL_STOP_BIT        US_MR_NBSTOP_1_BIT

#define PINS_USART0_PIO      PIOA
#define PINS_USART0_ID       ID_USART0
#define PINS_USART0_TYPE     PIO_PERIPH_A
#define PINS_USART0_ATTR     PIO_DEFAULT
#define PINS_USART0_MASK     (PIO_PA5A_RXD0| PIO_PA6A_TXD0)


uint32_t received_byte;
uint32_t dw_status ;

int main(void)
{
    sysclk_init();
    board_init();
    sysclk_enable_peripheral_clock(USART_SERIAL_ID);
    pio_configure(PINS_USART0_PIO, PINS_USART0_TYPE, PINS_USART0_MASK, PINS_USART0_ATTR);

    const sam_usart_opt_t usart_console_settings = {
        USART_SERIAL_BAUDRATE,
        USART_SERIAL_CHAR_LENGTH,
        USART_SERIAL_PARITY,
        USART_SERIAL_STOP_BIT,
        US_MR_CHMODE_NORMAL
    };
    
    usart_init_rs485(USART_SERIAL, &usart_console_settings, sysclk_get_peripheral_hz());
    usart_enable_tx(USART_SERIAL);
    usart_enable_rx(USART_SERIAL);
    usart_enable_interrupt(USART_SERIAL, US_IER_RXRDY);
    NVIC_EnableIRQ(USART0_IRQn);
    
    while (1) 
    {
        
        while(US_CSR_TXRDY != 0 );
        do
        {
            usart_write(USART_SERIAL, 1);
        }while(US_CSR_RXRDY==0);
        /*dw_status = usart_get_status(USART_SERIAL);
        if(dw_status & US_CSR_RXRDY){
            usart_read(USART_SERIAL, &received_byte);
        }*/
        
    }
}

When I debug the program, it stays in the while(US_CSR_TXRDY != 0) loop... I wrote this line because I've seen somewhere in asf library that I should check if TX is ready to send before sending anything.. Once this will be resolved, I'll try to receive on the same board what I'm transmitting by connecting RX and TX together. I only begin working on this processor and I'm not pretty familiar with it...

Thank you for your help


Solution

  • US_CSR_TXRDY is a constant, so while(US_CSR_TXRDY != 0 ) is a infinite loop. I think it should be while( (USART0->US_CSR & US_CSR_TXRDY) != 0 ).