SAMD21 Xplained Pro(atsamd21j18a) usart configuration without using EDBG

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

Hi guys,

 

I started working on SAMD21  xplained pro board. I gone through one of USART example provided in atmel studio 7 i.e quick_usart_callback.

 

You can get this example here.

 

I got stuck in "config_usart"  function. In example they are using EDBG i.e sercom 3 but i want to use extension header 1/2 usart pins.

 

I tried with their macros to configure that but i am not getting any output.

Can any one guide me in this issue.

 

I just want to connect my board with "rs232-TTL converter" using either extension header 1 or 2 usart pins and want to send-receive between them.

 

here is the main.c of my code:


#include <asf.h>
#include <stdio.h>

void usart_read_callback(struct usart_module *const usart_module);
void usart_write_callback(struct usart_module *const usart_module);

void configure_usart(void);
void configure_usart_callbacks(void);

struct usart_module usart_instance;


#define MAX_RX_BUFFER_LENGTH   256
volatile uint8_t rx_buffer[MAX_RX_BUFFER_LENGTH];


void usart_read_callback(struct usart_module *const usart_module)
{
    usart_write_buffer_job(&usart_instance,
            (uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
}


void usart_write_callback(struct usart_module *const usart_module)
{
    port_pin_toggle_output_level(LED_0_PIN);
}

void configure_usart(void)
{
    struct usart_config config_usart;
    usart_get_config_defaults(&config_usart);


//! [setup_change_config]
    config_usart.baudrate    = 115200;
    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_enable]
    usart_enable(&usart_instance);

}

void configure_usart_callbacks(void)
{
//! [setup_register_callbacks]
    usart_register_callback(&usart_instance,usart_write_callback, USART_CALLBACK_BUFFER_TRANSMITTED);
    usart_register_callback(&usart_instance, usart_read_callback, USART_CALLBACK_BUFFER_RECEIVED);


//! [setup_enable_callbacks]
    usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_TRANSMITTED);
    usart_enable_callback(&usart_instance, USART_CALLBACK_BUFFER_RECEIVED);

}
//! [setup]
int main(void)
{
    system_init();

//! [setup_init]
    configure_usart();
    configure_usart_callbacks();


//! [enable_global_interrupts]
    system_interrupt_enable_global();

//! [main_send_string]
    uint8_t string[] = "Hello World!\r\n";

    usart_write_buffer_wait(&usart_instance, string, sizeof(string));

    while (true) 
   {
      usart_read_buffer_job(&usart_instance,(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
   }

}

    I replaced  [setup_change_config]  with below code .

config_usart.baudrate           = 115200;
    config_usart.mux_setting        = EXT1_UART_SERCOM_MUX_SETTING; 
    config_usart.pinmux_pad0        = EXT1_UART_SERCOM_PINMUX_PAD0;
    config_usart.pinmux_pad1        = EXT1_UART_SERCOM_PINMUX_PAD1;
    config_usart.pinmux_pad2        = EXT1_UART_SERCOM_PINMUX_PAD2;
    config_usart.pinmux_pad3        = EXT1_UART_SERCOM_PINMUX_PAD3;
    config_usart.transmitter_enable = EXT1_UART_SERCOM_DMAC_ID_TX;  
    config_usart.receiver_enable    = EXT1_UART_SERCOM_DMAC_ID_RX;
#endif

  and also in this usart_init () I changed  EDBG_CDC_MODULE with EXT1_UART_MODULE.

 while (usart_init(&usart_instance, EXT1_UART_MODULE, &config_usart) != STATUS_OK) ;

But still I am not getting  any data from board to rs232-ttl converter which is connected to serial emulator(docklight).

 

These are the definition of usart in samd21_xplained_pro.h in example code.

 

/** \name Embedded debugger CDC Gateway USART interface definitions
 * @{
 */
#define EDBG_CDC_MODULE              SERCOM3
#define EDBG_CDC_SERCOM_MUX_SETTING  USART_RX_1_TX_0_XCK_1                                                              // USART_RX_1_TX_0_XCK_1 detailed information is here
#define EDBG_CDC_SERCOM_PINMUX_PAD0  PINMUX_PA22C_SERCOM3_PAD0
#define EDBG_CDC_SERCOM_PINMUX_PAD1  PINMUX_PA23C_SERCOM3_PAD1
#define EDBG_CDC_SERCOM_PINMUX_PAD2  PINMUX_UNUSED
#define EDBG_CDC_SERCOM_PINMUX_PAD3  PINMUX_UNUSED
#define EDBG_CDC_SERCOM_DMAC_ID_TX   SERCOM3_DMAC_ID_TX
#define EDBG_CDC_SERCOM_DMAC_ID_RX   SERCOM3_DMAC_ID_RX
/** @} */
/** \name Extension header #1 UART definitions
 *  @{
 */
#define EXT1_UART_MODULE              SERCOM4
#define EXT1_UART_SERCOM_MUX_SETTING  USART_RX_1_TX_0_XCK_1
#define EXT1_UART_SERCOM_PINMUX_PAD0  PINMUX_PB08D_SERCOM4_PAD0
#define EXT1_UART_SERCOM_PINMUX_PAD1  PINMUX_PB09D_SERCOM4_PAD1
#define EXT1_UART_SERCOM_PINMUX_PAD2  PINMUX_UNUSED
#define EXT1_UART_SERCOM_PINMUX_PAD3  PINMUX_UNUSED
#define EXT1_UART_SERCOM_DMAC_ID_TX   SERCOM4_DMAC_ID_TX
#define EXT1_UART_SERCOM_DMAC_ID_RX   SERCOM4_DMAC_ID_RX
/** @} */

Please assist me I am waiting for your suggestions.Thanks in advance.

This topic has a solution.

Pallav Joshi

Last Edited: Mon. Jun 6, 2016 - 12:30 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for your support its working fine now.

But I have some more queries.

Can you clear them?

 

In your above shared code we are using I2C right.what about Usart(Pin 13/14) can you explain or can you share a piece of code for usart configuration.

And we have to proceed further with your code so using that I2C are we able to send data from RS232-TTL converter and receive it into board.

 

If yes then how to check that DATA in code which we are sending from RS232-TTL converter.

 

Please give some suggestions.

Pallav Joshi

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

Is this piece of code in my main function(as shown in above discussion) is working for receiving the data which it will get on RX line(PA09: pin12 in EXT1) .

 while (true) 
   {
      usart_read_buffer_job(&usart_instance,(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
   }

If yes then how can I check this data in code.I mean in which buffer/variable I will get this data.

Pallav Joshi

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

I had a problem with SERCOM 4 using the UART pins which is why I tried I2C. The rest of the code is as per the sample.  You need to connect to EXT1 11/12. 

 

David

 

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

Thanks David for your support.

Hey guys anyone who can explain more about my queries solution please respond.

 

I will wait.

Thanks for your support in advance

 

Pallav Joshi

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
 while (true) 
   {
      usart_read_buffer_job(&usart_instance,(uint8_t *)rx_buffer, MAX_RX_BUFFER_LENGTH);
   }

If yes then how can I check this data in code.I mean in which buffer/variable I will get this data.

 

usart_read_buffer_job() is a function to read data into a buffer.  The first parameter says where from and the third define the size of the buffer.  Guess what the second parameter is?  

 

 

David

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

Yes David I checked this rx_buffer by adding it into the watch but it is not collecting complete data  it is just collecting some bytes not the complete message.

ff 14 03 00 00 10 11 16 00 18 00 00 01 20 16 04 20 01 21 01 07 00 00 00 f4 34 bb

This above shown sequence should be collected by usart_read_buffer_job() in buffer rx_buffer as this above sequence we are providing to the usart rx line pin1(PB09). 

But it is just collecting  ff 14 bb(first two bytes of sequence and last byte of sequence )

Pallav Joshi

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

Have you confirmed that the other end receives 'Hello World!'? What are you using to send the data?  What happens if you drop the Baud rate to 9600? 

 

David

 

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

Yes David I am probing both rx and tx lines and checking data through rs232-ttl converter in serial window.

 

Actually I am connecting my atmel board with a device with that devices rx-tx line.

 

And if that device receives a command sequence 0xFF, 0x00, 0x03, 0x1D, 0x0C 

into its rx line than it transmitts this sequence ff 14 03 00 00 10 11 16 00 18 00 00 01 20 16 04 20 01 21 01 07 00 00 00 f4 34 bb.

 

So in place of hello world I am sending above command like this.

uint8_t string[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x03, 0x1D, 0x0C };

So that device is returning above response sequence to the atmel board.

As I told you that I am cross checking TX-RX line of atmel board by probing it with rs232-ttl converter.

 

I am getting expected data in both tx-rx line of board but not in code.

In code if I am adding   rx_buffer  into the watch it is not collecting complete data.

Pallav Joshi

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

Hey David thanks a lot for your support.

Its working fine now. 

Pallav Joshi

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

What was the fix then? 

 

David

 

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

I didn't get exact fix for that I just changed my approach I am using  usart_serial_getchar and  usart_serial_putchar.

 

And looping them with my required condition for getting complete buffer data.

Pallav Joshi

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

Ok so you have given up trying to read multiple chars and dropping to single char operation.  Always good to tidy up a thread so others are no left hanging. 

 

David

 

 

 

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

Hello David,

 

Good day.

 

I'm also facing same kind of issue with SAMD21 Xplanied board(MCU is SAMD21j18a).

First i used same sample code and its working with EDBG i.e sercom.

I've modified my code as above suggestions. I can see my led0 is blinking but not output in hyper terminal. I just try to send hello word text continuously.  I connected  MY TTL converter RX and TX pins to EXT1 11/12. 

can any one help whats wrong in my code. I have attached code.

 

Please help.

 

Thanks in advance.

Attachment(s): 

Jins Paul

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

Works for me, this is also with a D21 Xplained.

There is some serious confusion in the code but this will not prevent it working:

config_usart.transmitter_enable = EXT1_UART_SERCOM_DMAC_ID_TX;
config_usart.receiver_enable    = EXT1_UART_SERCOM_DMAC_ID_RX;

The enable settings are bools (and the default is true), makes no sense setting them to the DMAC ids.

Also, in my opinion, it makes no sense to use the I2C definitions like this:

    config_usart.pinmux_pad0 = EXT1_I2C_SERCOM_PINMUX_PAD0;
    config_usart.pinmux_pad1 = EXT1_I2C_SERCOM_PINMUX_PAD1;

I would replace them with the actual used settings PINMUX_PA08D_SERCOM2_PAD0 and PINMUX_PA09D_SERCOM2_PAD1. Same for the use of EXT1_I2C_MODULE, that helps no one trying to understand what is going on, should be replaced with SERCOM2.
/Lars

 

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

Hello

 

Can you please share me the final code?

I am using SAMR21G18A(PA22, PA23 pins for uart communication ).

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

Please share me the final code