SPI - Only first transfered byte is shifted by 1

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

 

EDIT: The first byte is actually petty unstable but most often shifted by one.

 

As title suggest the first byte to be send is shifted by a bit/cycle while the remainder are perfectly fine. i dont know what the issue is or how to correct it. Attaching projects for master and slave and a signal analyzer screenshot code has been stripped. The communication is one directional from slave to master but still sticking to the transfer function.

 

SLAVE:

#include <atmel_start.h>
#include <stdlib.h>

void spi_com_initialize();
struct spi_xfer spi_com_xfer;
uint8_t spi_com_buffer_tx[4];

int main(void) {
	atmel_start_init();
	spi_com_initialize();
}

void spi_com_initialize() {
	//printf("SPI_SLAVE\r\n");

	spi_com_xfer.size = 4;
	spi_com_xfer.txbuf = spi_com_buffer_tx;
	spi_com_xfer.rxbuf = NULL;

	SERCOM4->SPI.INTENSET.reg = SERCOM_SPI_INTFLAG_SSL | SERCOM_SPI_INTFLAG_DRE;
	NVIC_SetPriority(SERCOM4_IRQn, 0);
	NVIC_EnableIRQ(SERCOM4_IRQn);

	struct io_descriptor *spi_com_descriptor;
	spi_s_sync_get_io_descriptor(&SPI_1, &spi_com_descriptor);
	spi_s_sync_enable(&SPI_1);
}

void SERCOM4_Handler() {
	for (uint8_t i = 0; i < 64; i++) {
		spi_com_buffer_tx[0] = 0x12;
		spi_com_buffer_tx[1] = 0x34;
		spi_com_buffer_tx[2] = 0x56;
		spi_com_buffer_tx[3] = 0x78;
		spi_s_sync_transfer(&SPI_1, &spi_com_xfer);
	}

	while (SERCOM4->SPI.INTFLAG.bit.SSL == 1) {
		SERCOM4->SPI.INTFLAG.reg = SERCOM_SPI_INTFLAG_SSL;
	}
}

 

MASTER:

#include <atmel_start.h>
void spi_com_initialize();
void spi_com_receive();

struct spi_xfer spi_com_xfer;
uint8_t spi_com_buffer_rx[4];

int main(void) {
	atmel_start_init();
	spi_com_initialize();

	while (1) {
		spi_com_receive();
		break;
	}
}

void spi_com_initialize() {
	printf("SPI_MASTER\r\n");

	spi_com_xfer.size=4;
	spi_com_xfer.rxbuf=spi_com_buffer_rx;
	spi_com_xfer.txbuf=NULL;

	struct io_descriptor *spi_com_descriptor;
	spi_m_sync_get_io_descriptor(&SPI_0, &spi_com_descriptor);
	spi_m_sync_enable(&SPI_0);
}

void spi_com_receive() {
	printf("SPI_COM RECEIVE");

	for (uint8_t i = 0; i < 64; i++) {
		gpio_set_pin_level(SPI_SS, false);
		delay_us(100);
		spi_m_sync_transfer(&SPI_0, &spi_com_xfer);
		delay_us(100);
		gpio_set_pin_level(SPI_SS, true);
		delay_us(100);
	}
}

 

 

 

 

thanks for any help in adavance!

Attachment(s): 

Last Edited: Mon. Jan 4, 2021 - 01:21 PM