Showing posts with label SPI. Show all posts
Showing posts with label SPI. Show all posts

Tuesday, August 4, 2015

My Own Dreamboard: FRDM-KL25Z Motion Data Logger Using MBED

P1020934.JPG


Introduction
Element14 has been running a competition for a DreamBoard and I was wondering how available some of the setups people are requesting are, to work this out I decided to do a simple project. The project goal was to use an ARM processor and incorporate a few sensors and store the data. After looking around I decided to go with the FRDM-KL25Z development board from Freescale and use the FRDM-STBC-AGM01 add on board. As there was no easy way to add an SD card I created my own board for that and at the same time added a battery monitoring circuit to make it a true stand alone unit. All the code was written using mbed to better feature the ARM processor on the FRDM-KL25Z.


Usage Case
The idea behind this project originated from two usage cases with essentially the same basic idea. As I work on equipment that needs to be ruggedized both from the elements as well as from handling, I thought what better way to understand what the equipment goes through than to log it. Its one thing to be told that something needs to stand up to rough handling and abuse but, if that abuse can be quantized it makes designing for the worst case that much easier. The second case and somewhat similar is shipping of fragile packages. It is possible to attach fragile stickers and shock sensitive glass vials but that would only tell you that a threshold force was reached but not for how long or how often or even when during transportation. An active motion logger would resolve all these unanswered questions. Both of these use cases would allow for the use of the FRDM-KL25Z with just one or two add on boards keeping the system simple and as close to a Dreamboard design as possible


Project Goal
Following these two usage cases mentioned above it was important to understand the main goals that this project setou to achieve. As both cases are looking at the handling of an object over a relatively short period of time (1 ~ 8 days) battery life was not critical, a custom battery/SD card board was built but that was mainly because it wasn't until later I found the Freedom Battery Charger Expansion Board. The custom board follows the design of the TI Fuel Gauge BoosterPack but with updated components. Another aspect of the project was price, as it is not known what the units are really exposed to in terms of  abuse, keeping the cost low would mean a damaged or destroyed logger would be no real concern in terms of replacement. Along the lines of a damaged or destroyed unit is the issue of data loss, should an event happen to cause a logger to be destroyed that event should, as much as possible, be recorded. Lastly the project should be implemented quickly and kept simple so that any issues that may arise or any requirement changes should be easily dealt with by anyone capable of understanding simple hardware/software.


Data Acquisition and Volume of Data
Looking to log data for 1 ~ 8 days without missing any abusive handling there was a need to establish how fast the system could sample data and store it without losing any of that data. It was decided by a pseudo scientific method that 50 Hz sample rate should be adequate. This was reached by assuming a falling object from one foot or so would take approximately 0.247s to fall, using 50 Hz would give approximately 10 data points on the way down allowing for an approximate drop height to be calculated as well as the force the unit experienced on impact (depending on the surface)


The accelerometer can log data in either 14 bits or 8 bits. Assuming higher resolution and therefore 14 bits that needs 6 bytes of data per sample or 300 bytes per second. The gyroscope also can log data in 8 or 16 bits and again using the higher resolution gives us 300 bytes per second. Since I2C can be set to run at 375 kHz, this would give a data transfer time of ~6.4ms. The I2C bus was eventually set at 400 kHz with a realistic clock frequency of 375 kHz.


Once acquired from the sensors, the data is stored on a SD card for later analysis.The SD card is written too using the SPI protocol which can achieve a theoretical 40 MHz. After some testing it was determined that the realistic speed for the FRDM-KL25Z programed using mbed was only 1 MHz, while very much slower than the 40 MHz desired it posed to be no issue for the setup. Using an 8Gb SD card the unit would be able to log data for ~154 days or well after the batteries are no longer working. This is estimated from 300 bytes/s for each sensor(8Gb / 600 bytes/s / 60s / 60m /24h = 154.32).


The battery life for this project is estimated to be 5 days using a 1200 mAh battery and the full power run current of 7.1 mA for the FRDM-KL25Z, 35 μA for the FXOS8700CQ sampling at 50 Hz and 2.7 mA for the FXAS21002CQR1. This gives a total current consumption of 9.835 mA and for a 1200 mAh battery that gives ~122 hours or ~5 days, if low power mode was implemented for the FRDM-KL25Z this could be further extended. This does not account for SD writes or other components on the boards.


Hardware Setup
The hardware consists of of three PCBs. The first board is the FRDM-KL25Z that contains the MKL25Z128VLK4 that is the brains behind the system and has the bootloader for the mbed environment.
xfrd_small.jpg.pagespeed.ic.6qSsxADs8d.jpg
Figure 2. FRDM-KL25Z Development Kit from Freescale


The second board is the FRDM-STBC-AGM01 this board houses the two sensors used in this project, the FXOS8700CQ is a 3 axis accelerometer and magnetometer and the FXAS21002CQR1 is a 3 axis gyroscope. The sensors both use the I2C and SPI protocols to communicate with the host processor. Since there are limited number of SPI modules on the host processor and the data rate of the sensors is relatively low the I2C module was selected. Another reason for this choice is the FRDM-STBC-AGM01 is configured for I2C and the the FXOS8700CQ only works with point to point SPI.

frdm-stbc-agm01_fig.1.jpg
Figure 3. FRDM-STBC-AGM01 Sensor Board from Freescale


The last board is an SD card connector breakout board like the DIGILENT  PMODSD  PERIPHERAL MODULE or the MIDAS  UNO32-SD  DAUGHTER BOARD. The board used in setting up this project was a breakout board from 43oh which was replaced by a custom board* that was designed specifically for the FRDM-KL25Z.


2015-07-28 00_46_46-1 Board - C__Users_Kasriel_Documents_eagle_projects_element14-projects-hardware_.jpg
Figure 4. Custom PCB Layout with Space for Both a SD Card & Battery Monitoring


The design of this boards borrows from the TI Fuel Gauge Boosterpack. The circuit is the same except for the BQ27510-G2 which has been upgraded to the G3. While the software for the BQ27510-G3 has not yet implemented when completed this will allow for the system to be truly stand alone and take precautions to ensure all data is saved before the battery is about to die.


P1020938.JPG
Figure 5. Custom Board with SD Card & Battery Management Circuit


P1020940.JPG
Figure 6. Custom Board with SD Card & Battery Management Circuit


While there is a expansion board from Freescale that has similar abilities, specifically the FRDM-BC3770-EVB, this board does not contain a SD Card slot and this board was only discovered after the custom board had be sent to the fab. The custom board includes both card detect and write protect pins to allowing the host controller to be sure there is an SD card available and in a usable state before starting the system.


P1020886.JPG
Figure 7. System Implementation Using Three Separate Boards


Software Outline
The software was written in a simple and easy to understand style to allow for anyone wanting to read over the or modify the code to be able to do so. For this reason the code follows a very simple progression as will be outlined below. The code was written in the mbed environment allowing for further simplification, at each stage in the flow diagram for the software there is some code that demonstrates what is required at that stage of the software flow. The full program can be found in the mbed repository here.


This project uses interrupt driven programming, this means that all events in the main loop only happen after an interrupt routine has set a flag signaling for the main loop to take action. Both interrupts that are used are fired when a sensors has recorded a preset number of samples in its FIFOs. When the interrupt occurs it sets a flag letting the mainloop know that there is data to be fetched from that sensors FIFO.


Figure 8. Interrupts That Drive the System


The first step in the code is to include the two libraries that are needed, mbed.h adds the basic functionality for the chosen platform and SDFileSystem adds the functionality for writing data to the connected Sd card. Once the needed libraries have been added, the registers used by both the FXOS and FXAS sensors are defined, this allows us to keep later I2C calls simple and self explanatory. The next step is to initialize the needed system modules.


Looking at the flowchart below we see the first module that is initialized is the serial port. This port is mainly used for debugging as it is a relatively slow port and in mbed serial communication is a blocking operation, it is therefore not desirable to use the serial port during high speed data acquisition. I2C is initialized to enable communication with the sensors and at a later stage the battery monitoring IC as well. The I2C port used is port PTE pin 0 for the data line (SDA) and pin 1 for the the clock (SCL).
Setting up the interrupts is a two step process, first we set pins to be interrupts in this project, pins PTD4 and PTA5 are set as FIFO full interrupts from the two sensors. The second step with regards to interrupts is done in the main function and that is to assign what to interrupt on (high -> low or low -> high transitions) and when an interrupt occurs what function should be called. In this case both interrupts are happen on a rising edge and they call functions called fxas_data and fxos_data respectively.


The last step in the initialization before going into the main function is to set up the Sd card communication. This is done by creating a SDFileSystem object and then giving that object the pins used for MOSI,  MISO,  SCLK and CS. In this project the default SPI port was used, PTD2, PTD3, PTD1 and PTD0 respectively. It has been verified that it is possible to use other SPI ports but for simplicity only one is pointed out on the platform pin descriptions.


After the initializations are completed the I2C clock frequency and serial baud rate are increased to allow for faster communications and reduced interruptions. For this reason the SDFileSystem used in this project has been modified to increase the clock frequency to 1 MHz to prevent the system from losing data during a SD card write cycle. The next step is to create a directory on the SD card in which to place or data and a file with heading. The last step in the main functions initializations is to set up and start the sensors, this is done by calling fxas_init() and fxos_init(). Once all these steps have been completed the system will start logging data.


FRDM-KL25Z System Initialization .jpg
Figure 9. System Initialization


The main loop runs in the background and does all the heavy lifting. The first step in the main loop is to look for a FIFO full flag set in one of the two interrupts. Once a flag has been set the main loop will read all available data samples and store them in a local buffer. This buffer was arbitrarily set at 23, for better performance and possibly longer sleep times by the processor this could be set to 30 or 32 samples. When all the samples have been read from the sensor a data ready flag is set, this flag indicates to the next step of the main loop that data is ready to be sent to the SD card.


After data from both sensors has been read in, which should happen very close in time as both sensors are running at 50Hz, the main loop will send all available samples to the SD card. To do this correctly and in somewhat of a understandable fashion the MSB and LSB of each sample are concatenated before sending the data to the SD card. As the accelerometer data is 14 bits long and the LSB is left justified, there is a need to first shift the MSB left by 8 bits then OR the two bytes and finally right shift  all 16 bits by two to arrive at 14 bits of data. As fractional operations are  computationally heavy on microprocessors multiplying the data by either 0.976mg or 7.8125mdps has been left to be completed on the PC.


Once all data has been sent to the SD card the file is closed, this is done once every 30 cycles to ensure minimal amounts of data are lost. It is not done more frequently as this adds extra overhead to the SD write cycle. After closing the file the buffers are cleared, while not essential this was done to remove any question as to the correctness of the sampled data.


FRDM-KL25Z Main Loop.jpg
Figure 10. Main Loop Where the Time Consuming Operations Occur


The software in its entirety can be found here, the project can be added to your project space and then downloaded to your own FRDM-KL25Z from that location.


MBED
As part of this project was to show of the ARM core at the center of all the processing, the mbed environment was used. Along with showing of the ARM core mbed also allows for implementing programs in relatively simple and quick ways to be used as a prototype or in some circumstances for get to the final product quickly. While using mbed for this project I realized the ease with which other wise long and complex tasks can be achieved. Setting up interrupts and along with what action should be taken when fired only takes two lines. Similarly setting up the SD file system also takes two lines of code.


That being said there are some thing to watch out for. During this project I was not able to use the blue LED for debugging, this was discovered only after I was no longer able to save data to the SD card. It was later noticed that the SD card clock and the blue LED are on the same pin. Other issues where setting clock frequencies. The clock for the SPI but does not appear to be able to be set above 1 MHz, while I have tried a few different ways to reach this I was not able to do so. The same issue is noticed with the I2C bus, going above 375 kHz does not appear possible, I was hoping to achieve 400 kHz but setting the frequency higher only seems to decrease the clock frequency after a certain point.


Also not having a built in debugger when you are used to depending on one is a huge change in the game. Another big change that may take getting used to is the bootloader. Where with a conventional IDE there is a one step compile and program, here there is a two step compile and download and then drag and drop into the platform. While not a huge game changer it does slow down the programing iterations. Both of these may have bothered me but in a weird way I feel doing more programing like this would make you a better programmer as there is a need to do this better and in a more thought out manner the first time and less by trial and error.


With a few issues encountered along the way there is one very big positive to mbed. There is a large and very helpful community. No matter how big, small or subtle the issue I found that the community was both willing and very capable of helping.


Project Testing and Usage
The project while on a basic level is complete this is room for further progress. At this point there is a need to use the project to log how a package that is shipped is handled so as to better understand the design constraints to build a resilient outer shell.
In order to use this unit in a somewhat meaningful way the data obtained from this project is put through a graphing program to determine how the unit has handled. My program of choice is GNUPlot or Excel. While Excel has a limitation on the number of points it will plot for you (1048576) GNUPlot does not. Below I have included data samples from some test with this project to give an idea of what the project is capable of.


2015-07-28 13_42_20-Gnuplot (window id _ 0).png
Figure 11. Accelerometer Data Recorded on a 15 Minute Drive


2015-07-28 13_41_04-Gnuplot (window id _ 0).png
Figure 12. Gyroscope Data Recorded on a 15 Minute Drive



Issues experienced and their resolutions

Pin mapping and peripheral usage

FRDM-KL25Z not capturing Interrupts

Non-blocking interrupts

Arithmetic or Logical Shift in mbed

SDFile system working in standalone but not with other code

Logical operator frustrating me


*Rev A of the board is on branch Board_Inverted and not on the main branch at this point in time.




Original post on Element14 can be found here

Thursday, November 20, 2014

LPRS eRIC Development Kit - Review


p1010278.jpg

I would like to thank Element14 and LPRS for selecting me for this Road Test Review. This was an interesting and fun road test due to my past experience with the Texas Instruments CC3x00 SimpleLink family, LS Research ProFlex Zigbee and Anaren AIR Zigbee modules amongst others. I was very much looking forward at comparing the ease of use of the eRIC modules as well as the range such modules could give.



Unboxing
The packaging for these module was truly surprising. The box fits everything snuggly inside with almost no space to spare, this is a nice consideration as it allows for easy storage when the kit is no longer needed. The box is made from anti-static cardboard and anti-static foam. The kit contains two eRIC modules, two development boards, two antenna for the specified frequency (433/868/915 MHz),  two type A to Micro-B USB connectors, two 9V batteries and a Wireless Mike memory stick.

P1010206.JPGP1010208.JPG  p1010253.jpgp1010271.jpg
Figure 1. Opening the eRIC Development Kit and displaying the kits contents

The models are location specific, this means that a different frequency is used depending on the country the modules are sent to. Europe models use 434 MHz while North America modules use 868/915 MHz. The modules are easily set apart by the markings on the individual module. The Europe modules have a “4” in the lower right corner while those for North America have a “9”, this can be seen in the picture below.

p1010262.jpgp1010267.jpg
Figure 2. eRIC module with a distinct “9” announcing its North American designation



First Impressions
After opening the kit it is a simple step to get the kit working to see what the kit is capable of. It is this step that gives the user there first impressions of the kit and modules and aid in deciding where to move forward with such a unit or not.

The onboard demo allows for either choosing a predefined flashing sequence or an echo of the button press of a tester. The predefined sequence is useful in quickly and simply determining the range the modules can handle. To get this demo working it is a matter of 3 button presses on each development board respectively. This is a simple and easy process, button #1 is pressed and held for 2 seconds, LED #3 & #4 will flash twice and then either button #2 (transmitter) or #4 (receiver) is pressed to choose the functionality that module is to take.

p1010256.jpg
Figure 3. Test buttons located on the left side

Hardware overview
Modules
The eRIC modules are encased in a distinct gold plated RF shielding can with an opening for the u.fl connector and have 22 gold plated connection points. The connection points allow for the modules to be fully and easily used for a vast array of applications. The modules on the uC are also fully accessible allowing for high speed communication over SPI and I2C as well as lower speed communication over UART. There is also the ability to use the  GPIOs, ADCs and WDT all of this makes the eRIC an easy module to integrate into most projects. With 22 connection points at standard 0.1” pitch the module can easily be used with standard headers and breadboarded if needed. The different modules (North America vs. Europe) are easily distinguished from the number after the module’s eRIC marking.

p1010269.jpg
Figure 4. Development Kit with eRIC 9 module and Antenna (Battery not present)



Development Board
The modules fit easily and snugly into the development board and provides all necessary connections to the modules peripherals. The boards allow for a module to be soldered to the board or to use those pads as permanent test points. The development boards also provide a method to program the modules either through USB or through JTAG. There are 4 buttons to use for GPIO input and 4 LEDs for GPIO output allowing for crude debugging or program testing. Also included on the development board is an SMA connection for an external antenna. The antenna has been previously matched removing any need for set up by the end user. There is also a variable resistor for use with the modules accessible ADC. The on board battery connector allows for the development kit to work remotely and be a prototype substitute for a planned product.

The choice of battery is interesting due to its lower mAh (~500 mAh) compared to that of a AA battery (~1500 mAh). Considering the voltage regulators need to reduce the voltage from 9V to 3.3V instead of 4.5V to 3.3V the efficiency would definitely be lower. At the same time the use of a 9V battery allows for one battery to be used in a more compact form.  Since this is mainly for development this shorter run time would be acceptable.



easyRadio companion software
The kit comes with easy to use test software. The software includes easy configuration of all the basic modules wireless systems. These systems include UART baud rate, transmit power level, desired channel (frequency) as well as the over the air data rate. All of these settings allow for easy testing of the module in desired scenarios. This may include how the modules may interfere with or be disturbed by other systems, what is the optimal data rate so as not to lose data packets. Also included in the software is a test tab allowing for selecting high/low side and modulated carrier allowing for further testing of the RF signal with other systems. Also available in this tab is the ability to get the module version and the raw data being returned from the modules.

2014-11-18 01_04_21-Program Manager.jpg2014-11-18 01_06_09-Greenshot.jpg2014-11-18 01_05_39-Greenshot.jpg2014-11-18 01_06_26-Greenshot.jpg2014-11-18 01_06_41-Greenshot.jpg2014-11-18 01_07_16-Greenshot.jpg
Figure 5. The six screens viewable on the easyRadio Companion Software

The last tab allows for over the air transmission. This tab has an option for repeated sending at a rate of 1Hz as well a for a local echo. The one thing missing would be the ability to send large quantities of data in this test method so as to see what limits your unique situation encounter.

API
The modules come with what LPRS calls eROS (easyRadio operating system). This is a simple “operating system” to allow for a greater use of the modules subsystems. There is a large API (~100 functions) that allows for setting up the wireless, the UART, SPI, ADC, clock speed, interrupts etc. I have tested some of these calls and they seem to work with little issue, and it really helps to reduce the setup and prototyping stages.

The modules are coded using CCS from Texas Instruments and as this is not a foreign IDE there was little trouble using that as well. The code uses a predefined linker file as there is a bootloader on the modules that needs to be accommodated for. While this allows for security for LPRS’ code it makes coding slightly clumsy, its not a showstopper but it has some drawbacks. Over all the API it well defined, easy to follow and gives a lot of control of the module.


Range
Some basic testing was conducted with these units to see what type of range they have (a Spectrum analyzer would be awesome Element4… wink, wink). Since this is supposed to be a IoT module and presumably used for sensor networks there were two basic test done. The first was to place one unit  transmitting in one corner of a house while walking around with the second to determine when bits in the sequence where skipped. In a standard North American house both the the main floor as well as the basement where traversed with no missing bits in the sequence. The second test was a line of sight this was done by placing the unit first at shoulder height on a plastic stand and then on the ground transmitting. Again the second unit was receiving and was used to look for missing bits. The first test resulted in approximately 165 meters while the second test resulted in approximately 110 meters. While these numbers may seem small for the size and power consumption these are respectable numbers. Lastly both antennae were removed and as expected barely a meter out there was no longer any reception.

Comparison to CC3x00
As I have just completed a road test of the CC3x00 SimpleLink family it would be somewhat acceptable to compare at least the control of both products. For the most part the CC3x00s are a lot more complex and take longer to get up and running. Even with their added functionality they are more complex over all and harder to use than the eRIC modules. That being said both modules have their place in the IoT arena. For small sensor networks that could then relay information back to a single point the eRIC modules would be sufficient. However, for anything more complex I would suggest a CC3x00. Part of the reason for this is the lack of protocol on the eRIC modules. All communication is done via broadcast and therefore all addressing, message type would need to be implemented by the end user. While some will say this allows for flexibility it also can slow down some development.

Price point comparison vs features
For the price ($25) of these modules (not kits) they deliver a lot. They are easy to use, simple to set up and work out of the box. In comparison to the CC3100/CC3200 ($34/$23) they would be lacking a programer as at least one of them includes one and the other for an extra $15 it would be included. The eRIC kit on the other hand is $190 for a kit that only includes two modules any extra modules would need an antenna as well as a way to connect to the rest of your project, I’m not sure the extra $120 is worth it for the simplicity in the software.



Original post on Element14 can be found here