Tuesday, May 29, 2012

SD Card based bootloader - PSoC 3 : Part 2

So here is the second part of this exploration.

The workflow I've planned is:
1.Get SD Card Reading working.
2.Write functions to convert ASCII->Hex and process that data into a useable form.
3.Take a stock USB Bootloader,and add this functionality to it,which could be invoked by the presence of an SD card,or any other desired stimulus.

So,the SD card thing.Its all very well described in the component datasheet(right click on the component after placing it on the top design canvas) and between that datasheet and its example project,you really dont need a "tutorial".However,would really like to emphasize how easily and quickly one can be to interface an SD card with the PSoC3.Do refer to page 5 of the datasheet(PDF) since they have it in detail there.

What you'll need is the actual emFile Library,from here(4MB Zip File.) Check out the example project for the component.What I did was,I added the emFile Library to the example project for the component,and then set the pin assignments and programmed the PSoC3 with it.On the hardware side,I used a MicroSD module from embeddedmarket.com.Neat little board.(You can read my review here.)

MicroSD card module from www.embeddedmarket.com

The emFile documentation (inside that zip file.) details the API functions with examples and all,so its really easy to understand them.

It took me all of 5minutes to get SD read-writes going.Yes,just 5.

SD Card Interfacing with the PSoC3.

PSoC Rocks.

Part 3 will deal with the reading and parsing of CYACD files concludes this series,and is now posted!

SD Card based bootloader - PSoC 3 : Part 1

So thats the idea basically,wherein on power-up the PSoC3 would check for an SD card,and if its available,read a cyacd file (cy-what? ..I'll get to it.) and program the contents of that into its flash,and fire a software reset and get going.

I'll make this a 3 part series,and here are the links for Part 2 and Part 3.

So lets start.

A couple of points here.

Whats the CYACD file?
Its basically a "smarter" hex file,I'd say."CY-ACD" is short for Cypress application and code data.

When you have a bootloader,and a bootloadable,the bootloadable is fed to the bootloader(over USB,I2C,UART whatever.) as this file.

The contents of this file as described in AN60317 (page 11)

Interesting.So its basically then reading this file,and parsing its data to get our hands on the actual data thats to be written to the flash,and then write it to flash using some APIs.

The one that does the actual writing is

(this screenshot is from the System Reference Guide for cy_boot v2.40 included with PSoC Creator 2)

I guess you can see where I'm going with this.Infact,its all pretty convenient.(Kudos to the guys at CY for making it so! ;-P ).Read the Array Id,Row Address and the data from the CYACD file,and then feed those directly to this API.
(note that we need to call the CySetTemp and CySetFlashEEBuffer functions first. The temperature is needed to adjust the write times to the flash for optimal performance. The Buffer is used to store intermediate data while communicating with the System Performance Controller).

How are we going to interface with the SD card?
Well,2 ways here really.Either you write your own routines,or use the powerful emFile component in PSoC Creator 2 (introduced in Component Pack 1 specifically.) and use that directly.I'm going for the latter,since its already there and is very easy to use and setup,with a ton of API functions ready-to-go outta the box.

If you're still reading,then you've probably noted that parsing the CYACD file isn't just reading text and then calling that Flash writing API.The data in this CYACD file,as mentioned in the first screenshot,is in ASCII and Big Endian.So we will need to read that ASCII stuff,and then convert it to Hex,and then also account for the big-endian-ness of it.

Now that the basic functionality is understood,let me go onto discussing the implementation I had in mind.

What I plan to do is take a stock USB bootloader,and add this SD stuff to it,wherein the code checks for an SD card and a specifically named cyacd file(this could be made smarter,obviously.) and then opens it,reads data,parses it,and writes its data to flash.Thats it! :-)

I'll discuss SD Card interfacing in Part-2.