Basic setup for UC3 devices
The SPI module will be set up as master:
- SPI on module SPI0
- 1MHz SPI clock speed
- Slave Chip Select connected on NPCS1
- 8 bits per transfer
- SPI mode 0 (data on rising clock edge)
Workflow
Example code
Add to application C-file (e.g. main.c):
#define SPI_EXAMPLE (&AVR32_SPI0)
};
void spi_init_module(void)
{
}
Workflow
- Ensure that board_init() has configured selected I/Os for SPI function.
- Ensure that conf_spi_master.h is present for the driver.
- Note
- This file is only for the driver and should not be included by the user. In this example, the file can be left empty.
- Define an alias for the SPI module you want to use :
#define SPI_EXAMPLE (&AVR32_SPI0)
- Create the structure for the device id (Chip select)
- Note
- As this struct is the same for both the initializing part and the usage part it could be a good idea to make the struct global, and hence accessible for both the initializing part and usage part.
- Write the initialization function to setup the module :
void spi_init_module(void)
{
}
- Note
- The last argument of spi_master_setup_device, which is zero in this case, can be ignored and is only included for compatibility purposes.
- Call the initialization routine from your application.
Usage steps
Example code
Use in application C-file:
uint8_t txdata[1]={0xD7};
uint8_t rxdata[1];
...
Workflow
- Create two buffers for data to be sent/received on the SPI bus, The buffer can be of arbitrary size as long as there is space left in SRAM:
uint8_t txdata[1]={0xD7};
uint8_t rxdata[1];
- Note
- In this case, we have set a default value for the txdata which is a status read command to a spi memory chip.
- Call the spi_select_device routine to enable the chip select line for the slave you want to communicate with. The chip select enabled is defined by the value of the id field in the spi_device_conf struct
- Write data to the SPI slave device, in this case write one byte from the data buffer txdata:
- Read data from the SPI slave device, in this case read one byte and put it into the data buffer rxdata:
- Attention
- As the SPI works as a shift register so that data is shifted in at the same time as data is shifted out a read operation will mean that a dummy byte CONFIG_SPI_MASTER_DUMMY is written to the SPI bus. CONFIG_SPI_MASTER_DUMMY defaults to 0xFF, but can be changed by defining it inside the conf_spi_master.h file.
- When read and write operations is done, de-select the slave:
- Now you can use the data read from the slave which is in rxdata
if(rxdata[0]<0x3C)
do_something();