Create an USART communication between SAML21J <=> Arduino

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

Hi, I am currently fighting for getting to work an USART communication between the SAML21J and the Arduino, however the only exemple shown of the SERCOM USART is with the USB cable, not with the pin, so I don't really know how to configure the pins to be able to communicate with the Arduino. So here's my code that configures an USART to debug via USB (the config_usart_usb), which works, and also the code that configures the USART pins to communicate with the arduino, which doesn't work :

 

struct usart_module usart_instance_usb;
struct usart_module usart_instance_arduino;

void configure_usart(void)
{

    struct usart_config config_usart_usb;
    struct usart_config config_usart_arduino;
    
    usart_get_config_defaults(&config_usart_usb);
    usart_get_config_defaults(&config_usart_arduino);

    // usb usart (for debug)
    config_usart_usb.baudrate    = 9600;
    config_usart_usb.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
    config_usart_usb.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
    config_usart_usb.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
    config_usart_usb.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
    config_usart_usb.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
    
    // arduino usart
    config_usart_arduino.baudrate    = 9600;
    config_usart_arduino.mux_setting = USART_RX_1_TX_0_XCK_1;
    config_usart_arduino.receiver_enable = true;
    config_usart_arduino.transmitter_enable = true;
    config_usart_arduino.pinmux_pad0 = PINMUX_PB08D_SERCOM4_PAD0;
    config_usart_arduino.pinmux_pad1 = PINMUX_PB09D_SERCOM4_PAD1;
    config_usart_arduino.pinmux_pad2 = PINMUX_UNUSED;
    config_usart_arduino.pinmux_pad3 = PINMUX_UNUSED;

    while (usart_init(&usart_instance_usb,
    EDBG_CDC_MODULE, &config_usart_usb) != STATUS_OK) {
    }

    while (usart_init(&usart_instance_arduino,
    SERCOM4, &config_usart_arduino) != STATUS_OK) {
    }

    usart_enable(&usart_instance_usb);
    usart_enable(&config_usart_arduino);
}

 

This is how I do so that my TX pin is the PB08, and my RX pin is the PB09, but it doesn't work. What dit I miss in my configuration ?

And BTW, why some pins (like the PA08 and PA09) are multiple times on the board ?

 

Anyway, thanks for reading this, I hope that someone will be able to enlighten me on this one.

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

Hi there! The USART that you are trying to use is connected to USB Debugger! Try using other SERCOM pins to interface it with your arduino...

Salman Ali

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

Hi, thanks for the replay, you are talking about "config_usart_usb", but what about my config_usart_arduino ? Is it connected to the debugger too ?

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

Looks okay. But if you are trying to receive the data via interrupt mode then you need to enable interrupts too.

Salman Ali

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

Well, I am not trying to receive the data with the interrupt. I managed to get it to work not, but there is a remaining problem : I figured out that it works only with SERCOM3, which happens to also be the SERCOM of the debugger. So I guess i'm missing something, like... there's is a way to activate the SERCOM's, or maybe two SERCOM's can't work at the same time...

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

can I know what exactly you wanna do ? 

Salman Ali

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

I wanna be able to communicate with my debugger AND an Arduino at the same time, on 2 different Sercom interfaces, but first of all I wanna know if this is possible.

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

You mean like this ?

-------------       USART          ------------
- SAML21 <--------------------> Arduino -
-------------                         -------------

Salman Ali

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

Exactly. But it doesn't work any other SERCOM interface than the SERCOM 3.

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

If USB is must then you can  go for only SERCOM3 ! Otherwise you have plenty of options. 

Salman Ali

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

I know, but unfortunately I can't get it to work on SERCOM1 or SERCOM0. Is there like a special line to activate them ? Can we even make 2 SERCOMs interfaces work at the same time ? Like the SERCOM0 and the SERCOM3 ?

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

I have used 4 sercoms parallely ! There is no problem in running separate SERCOM channels parallely. May be you can try changing configuration for USB-UART bridge (this is the place where we are configuring SERCOM3 as UART)

Salman Ali

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

If you still have the code of the configuration and use of those 4 Sercoms, I would be very interested to take a look at it. :)

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

That is company proprietary so I can't share the code. But ill show you the use of four SERCOM channels.

SERCOM 3 - USB-UART Bridge
SERCOM 4 - UART Logger
SERCOM 1 - Bluetooth UART
SERCOM 2 - GPRS Module  

Salman Ali

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

All I need is really just the configuration of the pins, you know. :p

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

It's fine, I just found my problem : I was configuring the 2 usarts at the same time whereas I should have configured the 1st one, then the other one. Here's the working code, for those who might have the same problem in the future :

 

void configure_usart(void)
{

    // ##### USART ATMEL <===> USB #####

    struct usart_config config_usart_usb;
    usart_get_config_defaults(&config_usart_usb);

    config_usart_usb.baudrate    = 9600;
    config_usart_usb.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
    config_usart_usb.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
    config_usart_usb.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
    config_usart_usb.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
    config_usart_usb.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;

    while (usart_init(&usart_instance_usb,
    EDBG_CDC_MODULE, &config_usart_usb) != STATUS_OK) {
    }

    usart_enable(&usart_instance_usb);
    
    
    // ##### USART ATMEL <===> ARDUINO #####
    
    struct usart_config config_usart_arduino;
    usart_get_config_defaults(&config_usart_arduino);

    config_usart_arduino.baudrate    = 9600;
    config_usart_arduino.mux_setting = USART_RX_1_TX_0_XCK_1;
    config_usart_arduino.pinmux_pad0 = PINMUX_PA08C_SERCOM0_PAD0;
    config_usart_arduino.pinmux_pad1 = PINMUX_PA09C_SERCOM0_PAD1;
    config_usart_arduino.pinmux_pad2 = PINMUX_UNUSED;
    config_usart_arduino.pinmux_pad3 = PINMUX_UNUSED;

    while (usart_init(&usart_instance_arduino,
    SERCOM0, &config_usart_arduino) != STATUS_OK) {
    }

    usart_enable(&usart_instance_arduino);
}

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

Hello,

 

I would like to create an UART communication beetwen ATSAML21 and ATSAMB11. I started followuing the usart quick start callback (to realize the UART communication on SAML21) and Find Me example (on BLE module). Could you help me with this project?
Thanks,

Gennaro

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

Hello friends,

 

Good Day :)

 

I'm also facing same kind of issue with SAML21J X plained board. My MCU ver is SAML21J18A.

 

Basic requirement:

I want to use a uart communication (expect Virtual port communication) with GSM module. I selected EXT2 session PA19 and PA18 for USART.

I downloaded Example code USART_QUICK_START_CALLBACK2 for SAML21J18A. ( Currently in this example a "hello word" text will print thru Virtual com port ).

I've modified same code and put it inside the while(1) loop for continuous printing.

Above use cases working fine. Hello word text continuously printing in my tera term terminal (baud rate =9600).

 

Same example code I've modified for my GSM UART test.

struct usart_config config_usart;
    struct usart_config config_uart;
//! [setup_config]
//! [setup_config_defaults]
/*
    usart_get_config_defaults(&config_usart);
//! [setup_config_defaults]

//! [setup_change_config]
    config_usart.baudrate    = 9600;
    config_usart.mux_setting = EDBG_CDC_SERCOM_MUX_SETTING;
    config_usart.pinmux_pad0 = EDBG_CDC_SERCOM_PINMUX_PAD0;
    config_usart.pinmux_pad1 = EDBG_CDC_SERCOM_PINMUX_PAD1;
    config_usart.pinmux_pad2 = EDBG_CDC_SERCOM_PINMUX_PAD2;
    config_usart.pinmux_pad3 = EDBG_CDC_SERCOM_PINMUX_PAD3;
//! [setup_change_config]

//! [setup_set_config]
    while (usart_init(&usart_instance,
            EDBG_CDC_MODULE, &config_usart) != STATUS_OK) {
    }
//! [setup_set_config]

//! [setup_enable]

    usart_enable(&usart_instance);

Virtual com configuration I've modified as per my requirement like below

    usart_get_config_defaults(&config_uart);
        config_uart.baudrate    = 9600;
    config_uart.mux_setting = EXT2_UART_SERCOM_MUX_SETTING;
    config_uart.pinmux_pad0 = EXT2_UART_SERCOM_PINMUX_PAD0;
    config_uart.pinmux_pad1 = EXT2_UART_SERCOM_PINMUX_PAD1;
    config_uart.pinmux_pad2 = EXT2_UART_SERCOM_PINMUX_PAD2;
    config_uart.pinmux_pad3 = EXT2_UART_SERCOM_PINMUX_PAD3;
    config_uart.data_order =  USART_DATAORDER_MSB;
    
    //! [setup_set_config]
    while (usart_init(&uart_instance,
            EXT2_UART_MODULE, &config_uart) != STATUS_OK) {
    }
usart_enable(&uart_instance);

I tried to display same "Hello word" text thru UART(PA19 and PA18) but I'm not getting any data. some junk data is coming in tera terminal display. I've directly connected PA19 and PA18 pin to a MAX232(TTL converter ) and which will connect to my PC.

 

Note: I have written duplicate function for call back also. I've attached my code and screen shot of tera terminal display

 

Can any one help me whats wrong in my code.

 

Thanks in advance.

JP

Attachment(s): 

Jins Paul