ASF SPI (service) troubles with CS

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

davezap
Posts: 3
Joined: Mon Feb 16, 2015 11:07 pm

ASF SPI (service) troubles with CS

Mon Feb 16, 2015 11:16 pm

Hi all, 

I have just started with FreeRTOS and ASF and ATSAM4S - after previously just doing 8 bit mega in C with no libraries or OS. 

Having a problem getting the SPI to run on my board. I have followed all the quick start instructions and confirmed my digital lines are correct but am not getting any clock or data out.I have been tracing code and will describe here what I have noticed.. but to be honest I am stuck. 

Am using FreeRTOS 7.3.0, SPI service, IOPORT, ... 

From main.c

Code: Select all

void spi_init_pins(void)
{	
	ioport_set_pin_dir(ddR_CS, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(ddR_CS, true);

	ioport_set_pin_dir(ddR_SDI, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(ddR_SDI, true);

	ioport_set_pin_dir(ddR_SDO, IOPORT_DIR_INPUT);
	ioport_set_pin_mode(ddR_SDO, IOPORT_MODE_PULLUP);

	ioport_set_pin_dir(ddR_CLK, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(ddR_CLK, true);
}


void spi_init_module(void)
{
	struct spi_device spi_device_conf = {
		.id = IOPORT_CREATE_PIN(PIOA, 11)
	};	

	spi_master_init(SPI);
	spi_master_setup_device(SPI, &spi_device_conf, SPI_MODE_0, 1000000, 0);
	spi_enable(SPI);
	
}

conf_clock.h

Code: Select all

#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_PLLACK
#define CONFIG_SYSCLK_PRES          SYSCLK_PRES_2
#define CONFIG_PLL0_SOURCE          PLL_SRC_MAINCK_XTAL
#define CONFIG_PLL0_MUL             20
#define CONFIG_PLL0_DIV             1
#define CONFIG_PLL1_SOURCE          PLL_SRC_MAINCK_XTAL
#define CONFIG_PLL1_MUL             16
#define CONFIG_PLL1_DIV             2
#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_PLL1
#define CONFIG_USBCLK_DIV           2

Here is the sample write / read code.

Code: Select all

uint8_t data_buffer[2] = {0x0500, 0x0100};

struct spi_device spi_device_conf = {
   .id = IOPORT_CREATE_PIN(PIOA, 11)
};

spi_select_device(SPI, &spi_device_conf);
spi_write_packet(SPI, data_buffer, 2);
spi_read_packet(SPI, data_buffer, 2);
spi_deselect_device(SPI, &spi_device_conf);
First of all in spi_select_device (spi_master.c) it just leaves doing nothing because if (device->id < MAX_NUM_WITHOUT_DECODER) is false. My device ID = 11 based on device CS pin id, and MAX_NUM_WITHOUT_DECODER = 4

I guess it's all down hill from that point.???'

So should I be specifying -    ....id = IOPORT_CREATE_PIN(PIOA, 11)  and what is spi_get_peripheral_select_decode_setting trying to do? 


Thanks
davezap
Posts: 3
Joined: Mon Feb 16, 2015 11:07 pm

Re: ASF SPI (service) troubles with CS

Wed Feb 18, 2015 10:24 pm

I have tried zero for the ID as my chip select is NPCS0

Code: Select all

struct spi_device spi_device_conf = {
   .id = 0
};
I have also explicitly set mux a for all lines. Below I also toggle the line so I can test it is working with the scope. 

Code: Select all

	ioport_set_pin_dir(ddR_CS, IOPORT_DIR_OUTPUT);
	ioport_set_pin_mode(ddR_CS, IOPORT_MODE_MUX_A);
	ioport_set_pin_level(ddR_CS, false);
	ioport_set_pin_level(ddR_CS, true);
The CS line works fine but is still not driven by SPI service. ?? 
davezap
Posts: 3
Joined: Mon Feb 16, 2015 11:07 pm

Re: ASF SPI (service) troubles with CS

Wed Feb 18, 2015 10:58 pm

:oops: :oops:

One chip on the bus has MOSI MISO reversed. I'll fix this and report back.

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 1 guest