Hello,
I've been working on converting from lovely 8-bit ATMEGA controllers to this fancy 32bit stuff. Not gonna lie it is not the kind of fun i'd expect.
USB was easier than i thought to get up and working but i've been struggling a lot with SPI.
The IC I'm using is SAMD21G18A
Things I've spotted:
Using spi_set_baudrate(&config_spi_master, 1000000); causes it to crash, hangs at a "void Dummy_Handler(void)"
I stepped though the code and yes it does indeed cause it to go into interrupt.
Troubleshooting done:
-If I don't use the set_baudrate the code simply just runs but does not write anything to SPI, it just sits there.
-I can see that the slave CS pin is being pulled low and high within around 90us.
WHICH funny enough is close to what i'd expect. Default baudrate is 100KHz so 1/100K = 10us * 8 bytes = 80us.
AND then there is time for pulling the CS pin high and low so it does seem like it is transmitting.
- Mux config should be correctly. I've validated it against the AT03255 page 64 which shows pinout of DO/DI and mux settings.
- I tried configuring the SPI pins as input/output like a GPIO but that didn't solve anything.
- I checked the SS pin is not being pulled LOW so it goes into slave mode.
- Tried changing the GCLK to 8MHz internal.
FYI. logic analyser attached to the pins so I can check them.
Test procedure:
- SPI_TEST is the code that is being executed for testing. Simply just write 0x22 onto the SPI bus every 500ms.
- There are NO SPI devices connected to the MCU currently so there should not be any devices causing issues.
Code:
#include "asf.h"
#include "SPI2.h"struct spi_module spi_master_instance;
struct spi_slave_inst disp_slave;
struct spi_config config_spi_master;
struct spi_slave_inst_config slave_dev_config;// Configure slave device for display
void init_SPI_display() {
spi_slave_inst_get_config_defaults(&slave_dev_config);
slave_dev_config.ss_pin = PIN_PA21;
spi_attach_slave(&disp_slave, &slave_dev_config);
}void configure_spi_master(void) {
spi_get_config_defaults(&config_spi_master);config_spi_master.mode = SPI_MODE_MASTER;
config_spi_master.mux_setting = SPI_SIGNAL_MUX_SETTING_D;
config_spi_master.pinmux_pad0 = PIN_PA16C_SERCOM1_PAD0;
config_spi_master.pinmux_pad1 = PIN_PA17C_SERCOM1_PAD1;
config_spi_master.pinmux_pad2 = PIN_PA18C_SERCOM1_PAD2;
config_spi_master.pinmux_pad3 = PIN_PA19C_SERCOM1_PAD3;
spi_init(&spi_master_instance, Sercom1, &config_spi_master);
spi_enable(&spi_master_instance);}
void init_SPI(void) {
init_SPI_display();
configure_spi_master();
}uint8_t SPI_TEST(void) {
spi_select_slave(&spi_master_instance, &disp_slave, true);
uint8_t error = spi_write_buffer_wait(&spi_master_instance, 0x22, 1);
spi_select_slave(&spi_master_instance, &disp_slave, false);
return(error);
}