spi full duplex mode problem

Discussions around product based on ARM Cortex M0+ core.

Moderator: nferre

sandeep
Posts: 13
Joined: Tue Aug 18, 2015 12:49 pm

spi full duplex mode problem

Fri Sep 11, 2015 3:06 pm

hi i have been trying to communicate between two SAMR21 boards in SPI FULL DUPLEX mode but the problem is i was receiving the data correctly in SLAVE side but coming to the master side garbage values are coming.please check the problem and help me to get through with this.......thanking you




MASTER CODE:

#include <asf.h>

//////////////////////////////////////////usart congif ///////////////////////////////////
//void delay(void);
void configure_usart(void);

//! [module_inst]
struct usart_module usart_instance;
//! [module_inst]

//! [setup]
void configure_usart(void)
{
//! [setup_config]
struct usart_config config_usart;
//! [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);
//! [setup_enable]
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////






//! [setup]
//! [buf_length]
#define BUF_LENGTH 20
//! [buf_length]
//! [slave_select_pin]
#define SLAVE_SELECT_PIN EXT1_PIN_SPI_SS_0
//! [slave_select_pin]
//! [buffer]
static uint8_t buffer[BUF_LENGTH] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0xAA, 0xBB, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13
};
//! [buffer]
uint8_t buffer_data[BUF_LENGTH];
uint8_t temp;
//! [dev_inst]
struct spi_module spi_master_instance;
//! [dev_inst]
//! [slave_dev_inst]
struct spi_slave_inst slave;
//! [slave_dev_inst]
//! [setup]
void delay(void);
void delay(void)
{
int i,j;
for (i=0;i<9210;i++)
for(j=0;j<9210;j++);

}


void configure_spi_master(void);

//! [configure_spi]
void configure_spi_master(void)
{
//! [config]
struct spi_config config_spi_master;
//! [config]
//! [slave_config]
struct spi_slave_inst_config slave_dev_config;
//! [slave_config]
/* Configure and initialize software device instance of peripheral slave */
//! [slave_conf_defaults]
spi_slave_inst_get_config_defaults(&slave_dev_config);
//! [slave_conf_defaults]
//! [ss_pin]
slave_dev_config.ss_pin = SLAVE_SELECT_PIN;
//! [ss_pin]
//! [slave_init]
spi_attach_slave(&slave, &slave_dev_config);
//! [slave_init]
/* Configure, initialize and enable SERCOM SPI module */
//! [conf_defaults]
spi_get_config_defaults(&config_spi_master);
//! [conf_defaults]
//! [mux_setting]
config_spi_master.mux_setting = EXT1_SPI_SERCOM_MUX_SETTING;
//! [mux_setting]
/* Configure pad 0 for data in */
//! [di]
config_spi_master.pinmux_pad0 = EXT1_SPI_SERCOM_PINMUX_PAD0;
//! [di]
/* Configure pad 1 as unused */
//! [ss]
config_spi_master.pinmux_pad1 = PINMUX_UNUSED;
//! [ss]
/* Configure pad 2 for data out */
//! [do]
config_spi_master.pinmux_pad2 = EXT1_SPI_SERCOM_PINMUX_PAD2;
//! [do]
/* Configure pad 3 for SCK */
//! [sck]
config_spi_master.pinmux_pad3 = EXT1_SPI_SERCOM_PINMUX_PAD3;
//! [sck]
//! [init]
spi_init(&spi_master_instance, EXT1_SPI_MODULE, &config_spi_master);
//! [init]

//! [enable]
spi_enable(&spi_master_instance);
//! [enable]

}
//! [configure_spi]

int main(void)
{
//! [main_setup]
//! [system_init]
system_init();
//! [system_init]
//! [run_config]
configure_spi_master();
//! [run_config]
//! [main_setup]
configure_usart();
//! [main_use_case]
//! [inf_loop]
while (true) {
/* Infinite loop */
if(!port_pin_get_input_level(BUTTON_0_PIN)) {
//! [select_slave]
spi_select_slave(&spi_master_instance, &slave, true);
//! [select_slave]
//! [write]

spi_transceive_buffer_wait(&spi_master_instance,buffer,buffer_data,BUF_LENGTH);

////////////////////////////////////////////////////////////////////////////////
for (uint8_t i = 0; i < BUF_LENGTH; i++) {

if(buffer_data==buffer)
{
usart_write_buffer_wait(&usart_instance,buffer_data, BUF_LENGTH);
volatile uint32_t delay = 200000;
while(delay--) {
}

if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {

while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
{
}
//! [main_write]
}
}

}





///////////////////////////////////////////////////////////////////////////////////












//! [write]
//! [deselect_slave]
spi_select_slave(&spi_master_instance, &slave, false);
//! [deselect_slave]
//! [light_up]
port_pin_set_output_level(LED_0_PIN, LED0_ACTIVE);
//! [light_up]
}
}
//! [inf_loop]
//! [main_use_case]
}





SLAVE CODE:

#include <asf.h>

//////////////////////////////////////////usart congif ///////////////////////////////////
//void delay(void);
void configure_usart(void);

//! [module_inst]
struct usart_module usart_instance;
//! [module_inst]

//! [setup]
void configure_usart(void)
{
//! [setup_config]
struct usart_config config_usart;
//! [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);
//! [setup_enable]
}


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void configure_spi_slave(void);

//! [setup]
//! [buf_length]
#define BUF_LENGTH 20
//! [buf_length]

//! [buffer]
static uint8_t buffer_expect[BUF_LENGTH] = {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0xAA, 0xBB, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13
};
static uint8_t buffer_rx[BUF_LENGTH] ;
//! [buffer]

//! [dev_inst]
struct spi_module spi_slave_instance;
//! [dev_inst]
//! [setup]

//! [configure_spi]
void configure_spi_slave(void)
{
//! [config]
struct spi_config config_spi_slave;
//! [config]
/* Configure, initialize and enable SERCOM SPI module */
//! [conf_defaults]
spi_get_config_defaults(&config_spi_slave);
//! [conf_defaults]
//! [conf_spi_slave_instance]
config_spi_slave.mode = SPI_MODE_SLAVE;
//! [conf_spi_slave_instance]
//! [conf_preload]
config_spi_slave.mode_specific.slave.preload_enable = true;
//! [conf_preload]
//! [conf_format]
config_spi_slave.mode_specific.slave.frame_format = SPI_FRAME_FORMAT_SPI_FRAME;
//! [conf_format]
//! [mux_setting]
config_spi_slave.mux_setting = EXT1_SPI_SERCOM_MUX_SETTING;
//! [mux_setting]
/* Configure pad 0 for data in */
//! [di]
config_spi_slave.pinmux_pad0 = EXT1_SPI_SERCOM_PINMUX_PAD0;
//! [di]
/* Configure pad 1 as unused */
//! [ss]
config_spi_slave.pinmux_pad1 = EXT1_SPI_SERCOM_PINMUX_PAD1;
//! [ss]
/* Configure pad 2 for data out */
//! [do]
config_spi_slave.pinmux_pad2 = EXT1_SPI_SERCOM_PINMUX_PAD2;
//! [do]
/* Configure pad 3 for SCK */
//! [sck]
config_spi_slave.pinmux_pad3 = EXT1_SPI_SERCOM_PINMUX_PAD3;
//! [sck]
//! [init]
spi_init(&spi_slave_instance, EXT1_SPI_MODULE, &config_spi_slave);
//! [init]

//! [enable]
spi_enable(&spi_slave_instance);
//! [enable]

}
//! [configure_spi]

int main(void)
{
//! [main_start]
uint8_t result = 0;
uint16_t temp;

/* Initialize system */
//! [system_init]
system_init();
//! [system_init]
configure_usart();
//! [run_config]
configure_spi_slave();
//! [run_config]
//! [main_start]

//! [main_use_case]
//! [read]
while(spi_read_buffer_wait(&spi_slave_instance, buffer_rx, BUF_LENGTH,
0x00) != STATUS_OK) {
/* Wait for transfer from the master */
}
spi_transceive_buffer_wait(&spi_slave_instance,buffer_expect,buffer_rx,BUF_LENGTH);
//! [read]
//! [compare]
for (uint8_t i = 0; i < BUF_LENGTH; i++) {
if(buffer_rx == buffer_expect)
{
usart_write_buffer_wait(&usart_instance,buffer_expect, BUF_LENGTH);
volatile uint32_t delay = 200000;
while(delay--) {
}

if (usart_read_wait(&usart_instance, &temp) == STATUS_OK) {

while (usart_write_wait(&usart_instance, temp) != STATUS_OK)
{
}
//! [main_write]
}
}
result=1;
}

//! [compare]
//! [inf_loop]
while (true) {
/* Infinite loop */
if (result) {
port_pin_toggle_output_level(LED_0_PIN);
/* Add a short delay to see LED toggle */
volatile uint32_t delay = 300000;
while(delay--) {
}
} else {
port_pin_toggle_output_level(LED_0_PIN);
/* Add a short delay to see LED toggle */
volatile uint32_t delay = 10000;
while(delay--) {
}
}
}
//! [inf_loop]
//! [main_use_case]
}


Return to “SAM D20 Cortex-M0+ MCU”

Who is online

Users browsing this forum: No registered users and 2 guests