After I finished my RoadTest review of the Zero Gecko starter kit, I still had some functionality that I want to try out. One of them is SPI support.
I have an SD card breakout board in my toolbox. That's what I'm connecting to the starter kit.
Simplicity Studio comes with an application note that explains interfacing Gecko to SD Cards. That AN0030 comes with an example for several kits, but not for the Zero Gecko.
I'm porting that example to my starter kit.
My first naive attempt was to start from the application note and try to change that to the Zero Gecko. I haven't succeeded to create a source case that compiles from that exercise.
So I decided to take a different approach. I Created a new project based on the ZG Blinky example. I knew that one compiles without issues .
Then I morphed that into a working SPI example by taking the Serial Peripheral Interface (SPI) Example from eewiki.net.
Video:
Getting that example to compile was not that difficult. This is the code:
#include "em_device.h" #include "em_system.h" #include "em_chip.h" #include "em_cmu.h" #include "em_gpio.h" #include "em_usart.h" #define SPI_PORTD gpioPortD // USART1 (location #3) MISO and MOSI are on PORTD #define SPI_PORTC gpioPortC // USART1 (location #3) SS and SCLK are on PORTC #define SPI_MISO_PIN 6 // PD6 #define SPI_MOSI_PIN 7 // PD7 #define SPI_CS_PIN 14 // PC14 #define SPI_SCLK_PIN 15 // PC15 #define SPI_nHLD_PIN 13 // PC13 #define SPI_nWP_PIN 11 // PC11 #define RX_BUFFER_SIZE 2 /* SPI functions */ void CS_pin_clr(void); void CS_pin_set(void); /* Global variables */ uint8_t rx_buffer[RX_BUFFER_SIZE]; int main () { CHIP_Init(); uint16_t i; // Clear software buffer (optional) for(i=0; i<RX_BUFFER_SIZE; i++) { rx_buffer[i] = 0; } /* Setup clock tree */ /* Use 24MHz crystal as HF system clock source */ /* Run Low Energy Peripheral B system as a high speed peripheral (use CORECLCK/2 as source) */ CMU_OscillatorEnable(cmuOsc_HFXO, true, true); // enable XTAL osc and wait for it to stabilize CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFXO); // select HF XTAL osc as system clock source (24MHz) if(cmuSelect_HFRCO != CMU_ClockSelectGet(cmuClock_HF)) { // make sure HFRCO is no longer the selected clock source CMU_OscillatorEnable(cmuOsc_HFRCO, false, false); // disable HFRCO to save power } CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_CORELEDIV2); // select HFCORECLK/2 as clock source to LFB CMU_ClockEnable(cmuClock_CORELE, true); // enable the Low Energy Peripheral Interface clock CMU_ClockEnable(cmuClock_GPIO, true); // enable GPIO peripheral clock CMU_ClockEnable(cmuClock_USART1, true); // enable USART1 peripheral clock /* Configure GPIO */ GPIO_PinModeSet(SPI_PORTD, SPI_MISO_PIN, gpioModeInput, 0); // configure MISO pin as input, no filter GPIO_PinModeSet(SPI_PORTD, SPI_MOSI_PIN, gpioModePushPull, 1); // configure MOSI pin as output, initialize high GPIO_PinModeSet(SPI_PORTC, SPI_CS_PIN, gpioModePushPull, 1); // configure CS pin as output, initialize high GPIO_PinModeSet(SPI_PORTC, SPI_SCLK_PIN, gpioModePushPull, 0); // configure SCLK pin as output, initialize low GPIO_PinModeSet(SPI_PORTC, SPI_nHLD_PIN, gpioModePushPull, 1); // configure nHLD pin as output, initialize high GPIO_PinModeSet(SPI_PORTC, SPI_nWP_PIN, gpioModePushPull, 0); // configure nWP pin as output, initialize low /* Configure SPI Port (USART1 in sync mode) */ USART_InitSync_TypeDef spi_init = { .enable = usartEnable, // enable bidirectional data (TX and RX) .refFreq = 0, // measure source clock .baudrate = 12000000, // 12Mbps is max data rate with 24MHz clock source .databits = usartDatabits8, // 8 data bits per frame .master = true, // configure as SPI master .msbf = true, // transmit msb first (requirement of W25X40CL) .clockMode = usartClockMode0, // clock idles low, data setup on rising edge, sampled on falling edge }; USART_IntClear(USART1, 0x1FF9); // clear interrupt flags (optional) USART_InitSync(USART1, &spi_init); // apply configuration to USART1 USART1->ROUTE = (3 << 8) | ( 1 << 3)| (3 << 0); // use location #3, enable TX/RX/CLK pins /* Get Device ID from SPI Slave (see W25X40CL datasheet for details) */ CS_pin_clr(); // select slave device rx_buffer[0] = USART_SpiTransfer(USART1, 0x90); // send 'Device ID' command and clear RX buffer for(i=0; i<3; i++) { rx_buffer[0] = USART_SpiTransfer(USART1, 0x00); // send 3 '00' bytes } rx_buffer[0] = USART_SpiTransfer(USART1, 0xFF); // send dummy byte '0xFF' to receive manuf. id rx_buffer[1] = USART_SpiTransfer(USART1, 0xFF); // send dummy byte '0xFF' to receive device id CS_pin_set(); // deselect slave device while(1); } // This function drives the CS pin low void CS_pin_clr(void) { GPIO_PinOutClear(SPI_PORTC, SPI_CS_PIN); } // This function drives the CS pin high void CS_pin_set(void) { GPIO_PinOutSet(SPI_PORTC, SPI_CS_PIN); }
I was pleasantly surprised that this simple trick already resulted in a 2-way communication with the SD Card:
Then I moved the SILabs AN0030 main file's code over to that example, and worked on getting the right pins addressed.
Step by step I removed non-applicable code, added header and include files, made small mods, resolving one build error after the other.
The code that I removed was related to development kit specific (BSP) calls. The remaining tasks were to find additional .c and .h files in the Simplicity Studio install folders and add them to the project.
The toughest one to resolve was a 'region RAM overflowed with stack' error. There was a solution documented for the Tiny Gecko on the SILabs forum. This solution works for the Zero Gecko too.
I had to alter line 17 in the ffconf.h file in the reptile/fatfs/inc folder:
#define _FS_TINY 1 /* 0:Normal or 1:Tiny */ /* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system / object instead of the sector buffer in the individual file object for file / data transfer. This reduces memory consumption 512 bytes each file object. */
All is well that end well. My example compiles and I have successful communication with the SD card. Here's the capture of the SPI communication:
and the SPI protocal analysis:
The project is available in the archive attached to this post.
Additional info
Hardware
I received the EFM32 Zero Gecko Starter Kit w/ Sensor Card as part of the element14 RoadTest.
The SD Card breakout board is from LC Studio. I used an unbranded 4 GB micro SD card.
Logic Analyzer
I use the Gadget Factory Papilio Pro FPGA board as logic analyzer hardware. I made my own IO buffer based on their 16-bitIOBufferWing.
The firmware (or whatever that is called on an FPGA ) is work from Magnus, that I have slightly adapted. It's available from Porting Logic Analyser from One to Pro - Page 3 - Papilio Pro - Gadget Factory Forum .
The logic analyzer client software is available on OLS - alternative Java client .
Read more about this on Make a Logic Analyzer from your Dev Kit Part 2: Papilio FPGA
Top Comments