SAM C21 Xplained board RS485 communication problem

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all,

 

I am using SAMC21 to do some RS485 related project. There is one RS485 example project in the Atmel Studio 7. But it is using ASF3.0. In my project I am using Atmel Start. I have been able to transmit character to another SAMC21 board. But the receive part is not working. The following is my code, which is basically implemented in Atmel start in the same way as the ASF3.0 example project. Could you guys please take a look and see what is wrong?  AGAIN, the transmit (write ) is working, but receive (read) not.  They are both implemented as interrupt.

 

Thank you.

 

#include <atmel_start.h>

#include <stdio.h>

#include <string.h>

#define RS485_MASTER 0x31

#define RS485_SLAVE 0x32

#define CONF_RS485_MODE RS485_MASTER

char rs485_mode = CONF_RS485_MODE;

//struct io_descriptor *usart_edbg_io;

struct io_descriptor *io;

struct io_descriptor *io_485;

//Data from RS485

uint16_t rec_data;

uint8_t tx_data;

void print_menu(void);

static void str_write(const char *s);

 

static void tx_cb_RS4851(const struct usart_async_descriptor *const io_descr)

{

/* Transfer completed */

if (rs485_mode==RS485_MASTER)

str_write("Transfer completed\r\n");

}

 

static void rx_cb_RS4851(const struct usart_async_descriptor *const io_descr)

{

/* Transfer completed */

if (rs485_mode == RS485_SLAVE){

         str_write("Receive completed\r\n");

         io_write(io, &rec_data, 1);

         //setup the next read...

         io_read(io_485,&rec_data,1);

         //Echo back to Master

         io_write(io_485,&rec_data,1);

        }

else{

        str_write("Receive completed\r\n");

        io_write(io, &rec_data, 1);

        //setup the next read...

       io_read(io_485,&rec_data,1);

      }

}

 

static void err_cb(const struct usart_async_descriptor *const io_descr)

{

    /* Transfer completed */

    str_write("Failed\r\n");

}

 

void RS485_example1(void)

{

//struct io_descriptor *io;

usart_async_register_callback(&RS485, USART_ASYNC_TXC_CB, tx_cb_RS4851);

usart_async_register_callback(&RS485, USART_ASYNC_RXC_CB, rx_cb_RS4851);

usart_async_register_callback(&RS485, USART_ASYNC_ERROR_CB, err_cb);

usart_async_get_io_descriptor(&RS485, &io_485);

usart_async_enable(&RS485);

//io_write(io, example_RS4851, 12);

}

/**

* Example of using EDBG to write "Hello World" using the IO abstraction.

*/

void EDBG_example1(void)

{

//struct io_descriptor *io;

usart_sync_get_io_descriptor(&EDBG, &io);

usart_sync_enable(&EDBG);

//io_write(io, (uint8_t *)"Hello World!\r\n", 12);

}

 

 

/**

* \brief Write string to console

*/

static void str_write(const char *s)

{

io_write(io, (const uint8_t *)s, strlen(s));

}

 

void print_menu(void){

str_write("RS485 Half-Duplex Test\r\n");

str_write("Press 1: Master Mode Set\r\n");

str_write("Press 2: Slave Mode Set\r\n");

str_write("Press ?: Display This Menu\r\n");

io_read(io,&rs485_mode,1);

if(rs485_mode == RS485_MASTER){

    str_write("Current Mode: Master\r\n");

    str_write("Send a character to the Slave: ");

}

else

    str_write("Current Mode: Slave\r\n");

}

 

 

int main(void)

{

/* Initializes MCU, drivers and middleware */

char txrx;

 

atmel_start_init();

 

EDBG_example1();

 

RS485_example1();

 

str_write("\r\nRS485 Demonstration\r\n");

print_menu();

 

//set up first read job

io_read(io_485, &rec_data, 1);

/* Replace with your application code */

while (true) {

 

if(rs485_mode == RS485_MASTER){

     io_read(io,&txrx,1);

     io_write(io, &txrx, 1);

     if(txrx == RS485_MASTER || txrx == RS485_SLAVE)

     rs485_mode = txrx;

     else if(txrx == 0x03F)

     print_menu();

     else{

          tx_data = txrx;

 

          str_write("\r\nSending data...... ");

 

          io_write(io_485, &tx_data, 1);

     }

}

else{

 

      str_write("\r\n\RS485----SLAVE\r\n ");

      io_read(io, &txrx, 1);

      if(txrx == RS485_MASTER){

      //Switch to Master

      rs485_mode = RS485_MASTER;

      }

else if(txrx == 0x03F)

      print_menu();

}

 

}

}