Simple USART code for ATSAME54P20A

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just need a code that can receive data on pin PA05 with a baud rate of 5555.

I tried looking at the existing projects on atmel START, but they proved to be too complicated.

Any help would be much appreciated.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would recommend you go back and look at Atmel START.. Don't use a demo project.. Just create a new project using START, and add/configure your UART on the desired SERCOM..

 

The code generated and provided in the Example folder will be as basic as it gets and will provide you with what you need.

murph

Debugging - Being a detective in a crime movie where you are also the murderer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thanks murph

 

thats exactly what I did before and I wrote code that can send data through those pins, but i dont know how receive the data.

The driver examples dont provide the command to receive data.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look for io_read(...)

 

You saw the function for writing data.. Look where that function is defined and look at the other functions defined with it...

 

// Also, I would recommend async over a sync UART driver.. Utilize an RX callback which then calls io_read.. (or sets a flag that causes your main program to call an io_read)

murph

Debugging - Being a detective in a crime movie where you are also the murderer.

Last Edited: Tue. Jun 25, 2019 - 12:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hey murph,

 

The Io_read called in the while loop doesn't read anything. Or maybe it does read but it doesn't send the data that it read. The program does compile and run on the board, but after it outputs "hello world", the tera term window does not receive any input. Ill leave the code bellow. I know that the device I am trying to read data from is outputing something because I tested it with a rs232 to usb adapter and tera term was able to capture the input. Also, I don't really know what an RX callback is, but I will look into it.

 

Thanks

 

#include <atmel_start.h>

 

int main(void)
{
    /* Initializes MCU, drivers and middleware */
    atmel_start_init();
    
    //the message sent to the pc
    const uint8_t *var_send_to_pc=(uint8_t *)"Hello World!";
    uint8_t *var_received_from_meter;
    
    //init USART (from mc to pc)
    struct io_descriptor *io;
    usart_sync_get_io_descriptor(&USART_0, &io);
    usart_sync_enable(&USART_0);
    
    //init USART (from meter to mc)
    struct io_descriptor *io2;
    usart_sync_get_io_descriptor(&USART_1, &io2);
    usart_sync_enable(&USART_1);

 
    while (true) {
        io_write(io, var_send_to_pc, 12);
        io_read(io2, var_received_from_meter, 8);
        var_send_to_pc=var_received_from_meter;
        }
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Couple things.. 1.) If you have a debugger, it would be a good idea to set a few breakpoints and see if you are getting data at the micro. 2.) The io_read function has a return val which tells you how many bytes it read from the fifo, I would utilize that and make your io_write conditional based on how many bytes you read.. 3.) I believe TeraTerm will expect a null terminator before display.. Is the device you are capturing data from, sending one?

 


io_write(io, var_send_to_pc, strlen(var_send_to_pc));

while (true) 
{
    if(io_read(io2, var_received_from_meter, 8) > 0)
    {
        var_send_to_pc=var_received_from_meter;
        
        // Utilizing strlen assumes that a null terminator is being
        // sent by the device you are capturing from.. Otherwise this won't
        // work and needs to be a constant
        io_write(io, var_send_to_pc, strlen(var_send_to_pc));
    }
} 

 

murph

Debugging - Being a detective in a crime movie where you are also the murderer.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I dont think the device is sending a null terminator. When I had it connected via rs232 it just constantly sent data to tera term without stopping.

I tried using the debugger but the data was unreadable for some reason. The device outputs serial data in hex and not decimal, so maybe thats why?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You're reading into unallocated memory on the first iteration and into a constant on the second. Try this:

 

// Send greeting
const char* hello[] = "Hello World!";
io_write(io, (const uint8_t*) hello, strlen(hello));

// Start forwarding data
uint8_t data[8];
while(true) {
	// Read up to 8 bytes from meter
	size_t bytes_read = io_read(io2, data, sizeof(data));

	// Forward read data (if any) to PC.
	io_write(io, data, bytes_read);
}

 

Last Edited: Thu. Jun 27, 2019 - 05:34 PM