element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
RoadTests & Reviews
  • Products
  • More
RoadTests & Reviews
Review Blogs Zero Gecko follow up: talking to a microSD card via SPI
  • Blogs
  • RoadTest Forum
  • Documents
  • RoadTests
  • Reviews
  • Polls
  • Files
  • Members
  • Sub-Groups
  • More
  • Cancel
  • New
Join RoadTests & Reviews to participate - click to join for free!
  • Share
  • More
  • Cancel
  • Author Author: Jan Cumps
  • Date Created: 17 Nov 2014 8:29 PM Date Created
  • Views 4196 views
  • Likes 8 likes
  • Comments 12 comments
Related
Recommended
  • RoadTest
  • microsd
  • spi
  • zero_gecko_starter_kit
  • feature_tutorial
  • zero_gecko

Zero Gecko follow up: talking to a microSD card via SPI

Jan Cumps
Jan Cumps
17 Nov 2014

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.

image

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 image.

 

Then I morphed that into a working SPI example by taking the Serial Peripheral Interface (SPI) Example from eewiki.net.

 

 

 

 

 

 

 

Video:

 

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

 

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:

 

image

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:


image

and the SPI protocal analysis:

image

 

The project is available in the archive attached to this post.

 

Additional info

 

Hardware

I received the EFM32Tm 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.

image

 

image

 

 
imageimage

 

 

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 image ) 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

 


image

Attachments:
STK3200_sd_card.zip
  • Sign in to reply

Top Comments

  • baldengineer
    baldengineer over 11 years ago in reply to Jan Cumps +2
    Thanks Jan, I am still not sure what I did wrong, so I started over from scratch. Your code example now compiles. Notes for others: 1. If you've loaded this code previously, it isn't enough to close or…
  • withboobs
    withboobs over 8 years ago +2
    Please check my post at https://github.com/OneBoobLess/zerogecko-stk3200-datalogger Board: Silicon Labs EFM32ZG-STK3200 Development Kit Device: EFM32ZG222F32 Digital clock with Real Time Clock DS3231 …
  • baldengineer
    baldengineer over 11 years ago +1
    Nice work, seems strange that SPI and FAT wasn't included with a kit that begs to be a datalogger. I'm trying to follow your work but running into many problems. I've downloaded the ZIP file you attached…
Parents
  • baldengineer
    baldengineer over 11 years ago

    Nice work, seems strange that SPI and FAT wasn't included with a kit that begs to be a datalogger.

     

    I'm trying to follow your work but running into many problems.

     

    I've downloaded the ZIP file you attached and created a new Project using the Gecko Starter Kit and Proc type.

     

    I get errors about missing ff.h and diskio.h, but not really sure which ones I'm suppose to include.  Do I need to add an include directory to my project from the SDK?

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 11 years ago in reply to baldengineer

    It seems that the files that your installation is complaining about are those that I linked in stead of copied into the project. They are the files that are stored in the Simplicity Studio installation directory.

     

    I'll check and post the location of those source files here. if you upload your build error log, I'll do a validation.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • baldengineer
    baldengineer over 11 years ago in reply to Jan Cumps

    Hi Jan, I progressed a little farther, but now stuck on a seemingly obvious issue.  CMU_ClockEnable, CMU_ClockFreqGet, and USART_BaudrateSyncSet are giving "undefined reference", even though em_emu.h and em_usart.h seem to be in place.

     

    Any ideas?

     

    00:49:43 **** Incremental Build of configuration GNU ARM v4.8.3 - Debug for project sdcard_try2 ****
    make -j7 all 
    Building target: sdcard_try2.axf
    Invoking: GNU ARM C Linker
    arm-none-eabi-gcc -g -gdwarf-2 -mcpu=cortex-m0plus -mthumb -T "sdcard_try2.ld" -Xlinker --gc-sections -Xlinker -Map="sdcard_try2.map" --specs=nano.specs -o sdcard_try2.axf "./src/sd_card.o" "./emlib/em_system.o" "./FatFS/diskio.o" "./FatFS/ff.o" "./Drivers/microsd.o" "./CMSIS/efm32zg/startup_efm32zg.o" "./CMSIS/efm32zg/system_efm32zg.o" -Wl,--start-group -lgcc -lc -lnosys -Wl,--end-group
    ./src/sd_card.o: In function `main':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../src/sd_card.c:134: undefined reference to `CMU_ClockSelectSet'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../src/sd_card.c:140: undefined reference to `CMU_ClockFreqGet'
    ./Drivers/microsd.o: In function `MICROSD_Init':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:62: undefined reference to `CMU_ClockEnable'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:63: undefined reference to `CMU_ClockEnable'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:69: undefined reference to `USART_InitSync'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:87: undefined reference to `GPIO_PinModeSet'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:88: undefined reference to `GPIO_PinModeSet'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:89: undefined reference to `GPIO_PinModeSet'
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:90: undefined reference to `GPIO_PinModeSet'
    ./Drivers/microsd.o: In function `MICROSD_XferSpi':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:127: undefined reference to `USART_SpiTransfer'
    ./Drivers/microsd.o: In function `MICROSD_PowerOn':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:161: undefined reference to `CMU_ClockEnable'
    ./Drivers/microsd.o: In function `MICROSD_PowerOff':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:174: undefined reference to `CMU_ClockEnable'
    ./Drivers/microsd.o: In function `MICROSD_SpiClkSlow':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:409: undefined reference to `USART_BaudrateSyncSet'
    ./Drivers/microsd.o: In function `MICROSD_SpiClkFast':
    /Users/james/SimplicityStudio/v2_workspace/sdcard_try2/GNU ARM v4.8.3 - Debug/../Drivers/microsd.c:418: undefined reference to `USART_BaudrateSyncSet'
    collect2: error: ld returned 1 exit status
    make: *** [sdcard_try2.axf] Error 1
    
    00:49:44 Build Finished (took 212ms)

     

    Thanks,
    James

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Jan Cumps
    Jan Cumps over 11 years ago in reply to baldengineer

    I'm validating.

     

    Can you check if you have imported my project by using

    File -> Import - Existing Project in Workspace

    -> Select Archive File -> My zip file

    -> Select the project

    -> select Copy to Workspace

    -> Finish

     

    I have the following files linked in my project:

    in CMSIS/efm32zg

    • startup_gcc_efm32zg.s
    • system_efm32zg.c

    in emlib

    • em_assert.c
    • em_cmu.c
    • em_emu.c
    • em_gpio.c
    • em_leuart.c
    • em_usart.c

    includes:

    • "${workspace_loc:/${ProjName}/src}"
    • "${StudioSdkPath}/kits/common/drivers"
    • "${StudioSdkPath}/reptile/fatfs/inc"
    • "${StudioSdkPath}/CMSIS/Include"
    • "${StudioSdkPath}/emlib/inc"
    • "${StudioSdkPath}/kits/common/bsp"
    • "${StudioSdkPath}/Device/SiliconLabs/EFM32ZG/Include"
    • "${StudioSdkPath}/kits/EFM32ZG_STK3200/config"

     

    I made this change to line 17 of the ffconf.h file in /reptile/fatfs/inc/ to work around a memory size issue I had during compiling:

    #define _FS_TINY 1 /* 0:Normal or 1:Tiny */

     

    There will certainly be a better way to do this than changing a file in the install directory of SimplicityStudio image but I haven't looked into that.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • baldengineer
    baldengineer over 11 years ago in reply to Jan Cumps

    Thanks Jan, I am still not sure what I did wrong, so I started over from scratch.

     

    Your code example now compiles.

     

    Notes for others:

    1.  If you've loaded this code previously, it isn't enough to close or remove projects from the Project Explorer to load into a new project.  Previous projects need to be physically deleted other wise, the IDE will complain "Some projects cannot be imported because they already exist in the Workspace."  (I feel like I'm missing something obvious here, but to frustrated to figure it out.)

     

    2.  I had to re-link em_leuart.c and em_usart.c for some reason.  Otherwise I got the error:

    make: *** No rule to make target `/SiliconLabs/SimplicityStudio/v2/developer/sdks/efm32/v2/emlib/src/em_leuart.c', needed by `emlib/em_leuart.o'.

     

    Also when attempting to load the files in the IDE, the IDE gave an error.

     

    Doing Jan's steps and re-linking the files results in a successful compile.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • baldengineer
    baldengineer over 11 years ago in reply to Jan Cumps

    Thanks Jan, I am still not sure what I did wrong, so I started over from scratch.

     

    Your code example now compiles.

     

    Notes for others:

    1.  If you've loaded this code previously, it isn't enough to close or remove projects from the Project Explorer to load into a new project.  Previous projects need to be physically deleted other wise, the IDE will complain "Some projects cannot be imported because they already exist in the Workspace."  (I feel like I'm missing something obvious here, but to frustrated to figure it out.)

     

    2.  I had to re-link em_leuart.c and em_usart.c for some reason.  Otherwise I got the error:

    make: *** No rule to make target `/SiliconLabs/SimplicityStudio/v2/developer/sdks/efm32/v2/emlib/src/em_leuart.c', needed by `emlib/em_leuart.o'.

     

    Also when attempting to load the files in the IDE, the IDE gave an error.

     

    Doing Jan's steps and re-linking the files results in a successful compile.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • Jan Cumps
    Jan Cumps over 11 years ago in reply to baldengineer

    (I feel like I'm missing something obvious here, but to frustrated to figure it out.)

    The way to do this without frustration is from the IDE:

    Right Click on the project you want to delete

    Select Delete

    (Check 3 times if you have selected the right project image )

    Check "Delete Contents from disk"

    OK

    Gonsky for ever

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube