Atmel Start (ASFv4) change USART baudrate

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

Hi everybody,

 

I am using a SAM C21 and a Utility chip (PN532) connected over UART. When the PN532 is started up it is always set to 115200 baud, but can be switched by command up to 1288000 and it really does. After Sending the command to Switch I try so set the new baudrate on the SAM by "usart_async_set_baud_rate" but that does not seem to work.

Any idea on that?

 

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

Maybe it's changed but the way ASF used to work was that you would first populate a configuration struct with values such as baud rate, stop bits etc. Then you would make a call to some kind of uart_init(&config_struct) and that would setup the UART with all your chosen settings. Does it not work like that any more then?

 

EDIT: thought so:

 

http://asf.atmel.com/docs/latest...

 

#define USART_SERIAL                 USART0
#define USART_SERIAL_ID              ID_USART0  //USART0 for sam4l
#define USART_SERIAL_BAUDRATE        9600
#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
    sysclk_init();
    board_init();
    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
    };
#if SAM4L
  sysclk_enable_peripheral_clock(USART_SERIAL);
#else
    sysclk_enable_peripheral_clock(USART_SERIAL_ID);
#endif
    usart_init_rs232(USART_SERIAL, &usart_console_settings,
            sysclk_get_main_hz());
    usart_enable_tx(USART_SERIAL);
    usart_enable_rx(USART_SERIAL);

So like I say you put things like 9600 in a struct then call usart_init_rs232() with that.

Last Edited: Thu. Feb 1, 2018 - 03:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's ASF3 code.  For START/ASF4 take a look at <project>\hal\src\hal_usart_async.c or <project>\hal\src\hal_usart_sync.c.  These files are the hardware abstraction layer for ASF4's sync and async USART drivers.  You want these functions:

 

 

int32_t usart_sync_set_baud_rate(struct usart_sync_descriptor *const descr, const uint32_t baud_rate);  // synchronous driver version

int32_t usart_async_set_baud_rate(struct usart_async_descriptor *const descr, const uint32_t baud_rate);  // asynchronous driver version

 

Digging down into the usart_sync_set_baud_rate() it ultimately changes the Baud Rate Generator Register (US_BRGR).  This should switch to the new baud rate.  Be sure you delay slightly after switching baud rates, then toss any pending RX garbage data before trying to communicate at the new speed.

 

 

Last Edited: Thu. Feb 1, 2018 - 05:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Unfortunately usart_async_set_baud_rate does not do the Job. What's the trick?

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

chm wrote:
does not do the Job.

Be more specific:

 

  • Show what you've actually done.
  • Describe what actually happens - including how you observed it.

 

 

EDIT

 

ASF4 API Reference Manual - http://ww1.microchip.com/downloads/en/DeviceDoc/50002633A.pdf - tells that there is a status return value.

 

So what status return value do you get?

Last Edited: Wed. Feb 21, 2018 - 03:51 PM