ATSAM4SA16B SPI not transmitting

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

psk67
Posts: 1
Joined: Tue Jul 29, 2014 10:29 am

ATSAM4SA16B SPI not transmitting

Tue Jul 29, 2014 11:53 am

Hi

I'm trying to send data to a device through SPI using the single SPI interface available on the ATSAM4SA16B (3-wire SPI, master mode). I've added the relevant ASF modules to the project through the ASF wizard. Clock and IO's are configured. My implementation is similar to the example code in the "spi_master.h" header file (http://asf.atmel.com/docs/latest/sam4s/ ... xmega.html).

The project builds without any errors. I'm sending some dummy data over the SPI bus, but when calling "spi_deselect_device()" the program stays in the while loop inside this function (it looks like the TXEMPTY flag in SPI_SR is never set). It seems like the data is never clocked out of the shift register. I measured the SPCK pin on an oscilloscope while sending data repeatedly over the SPI bus and don't see any clock signal. The hardware itself is fine because I can toggle this pin if I use it as a GPIO configured as output. It looks like it might be some initialization issue, but I'm using the correct ASF modules and implementing the example code. Am I missing something?

Right now the device to which I'm sending the data is not connected (so SAM SPI pins are open), but this should not influence the internal operation of the SPI as far as the SAM is concerned. I've also configured the MISO pin even though it will not be used (will only transmit from master).

These are the initialization functions:

Code: Select all

static void init_oled_spi_pins(void)
{
	pmc_enable_periph_clk(ID_PIOA);

	ioport_set_pin_dir(NPCSO, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MOSI, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MISO, IOPORT_DIR_INPUT);
	ioport_set_pin_mode(MISO, IOPORT_MODE_PULLUP);
	ioport_set_pin_dir(SPCK, IOPORT_DIR_OUTPUT);
}

static void init_oled_spi(void)
{
	pmc_enable_periph_clk(ID_SPI);
			  
	struct spi_device spi_device_conf = {
		.id = NPCSO
	};

	spi_master_init(SPI);
	spi_master_setup_device(SPI, &spi_device_conf, SPI_MODE_0, 1000000, 0);
	spi_enable(SPI);
}
And this is the implementation:

Code: Select all

int main (void)
{
	uint8_t data_buffer[1] = {0xAA};

	struct spi_device spi_device_conf = {
		.id = NPCSO
	};

	sysclk_init();
	
	wdt_disable(WDT);

	init_leds();
	init_gpio();
	init_oled_spi_pins();
	init_oled_spi();

	spi_select_device(SPI, &spi_device_conf);
	spi_write_packet(SPI, data_buffer, 1);
	spi_read_packet(SPI, data_buffer, 1);
	spi_deselect_device(SPI, &spi_device_conf);
}
These are the relevant pin definitions in a header file:

Code: Select all

// SPI pin definitions (OLED)
#define NPCSO		IOPORT_CREATE_PIN(PIOA, 11)
#define MOSI		IOPORT_CREATE_PIN(PIOA, 13)
#define MISO		IOPORT_CREATE_PIN(PIOA, 27)
#define SPCK		IOPORT_CREATE_PIN(PIOA, 14)
mccrashman
Posts: 7
Joined: Thu Jul 10, 2014 1:29 pm

Re: ATSAM4SA16B SPI not transmitting

Tue Jul 29, 2014 2:14 pm

I think, the spi_select device is unnecessary on SAM SPI device, because chip select lines are controlled by hardware.

I have written some projects with SPI device and usually use the following code to configure SPI in master mode.

Code: Select all

#define SPI_ID				ID_SPI
#define SPI_MASTER_BASE     SPI
#define SPI_MISO_GPIO  (PIO_PA12_IDX)
#define SPI_MISO_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
#define SPI_MOSI_GPIO  (PIO_PA13_IDX)
#define SPI_MOSI_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
#define SPI_SPCK_GPIO  (PIO_PA14_IDX)
#define SPI_SPCK_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)
#define SPI_NPCS0_GPIO            (PIO_PA11_IDX)
#define SPI_NPCS0_FLAGS           (PIO_PERIPH_A | PIO_DEFAULT)

void spi_master_initialize(void)
{
	spi_enable_clock(SPI_MASTER_BASE);
	spi_disable(SPI_MASTER_BASE);
	spi_reset(SPI_MASTER_BASE);
	spi_set_lastxfer(SPI_MASTER_BASE);
	spi_set_master_mode(SPI_MASTER_BASE);
	spi_disable_mode_fault_detect(SPI_MASTER_BASE);
	spi_disable_loopback(SPI_MASTER_BASE);
	spi_set_peripheral_chip_select_value(SPI_MASTER_BASE, SPI_CHIP_SEL);
	spi_configure_cs_behavior(SPI_MASTER_BASE, SPI_CHIP_SEL, SPI_CSR_CSNAAT);
	spi_set_clock_polarity(SPI_MASTER_BASE, SPI_CHIP_SEL, SPI_CLK_POLARITY);
	spi_set_clock_phase(SPI_MASTER_BASE, SPI_CHIP_SEL, SPI_CLK_PHASE);
	spi_set_bits_per_transfer(SPI_MASTER_BASE, SPI_CHIP_SEL, SPI_CSR_BITS_16_BIT);
	spi_set_baudrate_div(SPI_MASTER_BASE, SPI_CHIP_SEL,	(sysclk_get_cpu_hz() / gs_ul_spi_clock));
	spi_set_transfer_delay(SPI_MASTER_BASE, SPI_CHIP_SEL, SPI_DLYBS, SPI_DLYBCT);
	spi_enable(SPI_MASTER_BASE);
}
jharley
Posts: 238
Joined: Thu Dec 06, 2012 6:40 am

Re: ATSAM4SA16B SPI not transmitting

Tue Jul 29, 2014 4:30 pm

psk67 wrote:

Code: Select all

static void init_oled_spi_pins(void)
{
	pmc_enable_periph_clk(ID_PIOA);

	ioport_set_pin_dir(NPCSO, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MOSI, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MISO, IOPORT_DIR_INPUT);
	ioport_set_pin_mode(MISO, IOPORT_MODE_PULLUP);
	ioport_set_pin_dir(SPCK, IOPORT_DIR_OUTPUT);
}
The pins for peripherals (i.e. spi) need to be properly configured via the peripheral select register in the PIO and the PIO needs to be disabled for the pins you wish to connect to the peripheral.
You can do this by bringing in the PIO driver via the ASF wizard, if you haven't already... then use the pio_configure_pin().
Something like this...

Code: Select all

#define SPI_MOSI_GPIO (PIO_PA13_IDX)
#define SPI_MOSI_GPIO_FLAGS (PIO_PERIPH_A | PIO_DEFAULT)

static void init_oled_spi_pins(void)
{
	pmc_enable_periph_clk(ID_PIOA);

	pio_configure_pin( SPI_MOSI_GPIO, SPI_MOSI_GPIO_FLAGS );
	
	// add code to configure the remaining pins...
}
riz_068
Posts: 5
Joined: Wed Mar 11, 2015 3:09 pm

Re: ATSAM4SA16B SPI not transmitting

Wed Mar 11, 2015 3:16 pm

hi,
I have the same problem.did you get any solution ??

thanks in advance!
bakcsa
Posts: 1
Joined: Thu Oct 08, 2015 12:05 am

Re: ATSAM4SA16B SPI not transmitting

Thu Oct 08, 2015 12:12 am

riz_068 wrote:hi,
I have the same problem.did you get any solution ??

thanks in advance!
Hi,
If you still have issues with SPI implementation check out this post:
http://sam4dev.blogspot.co.at/2015/10/spi-with-asf.html

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 2 guests