Sorry, this will be a bit of a long post. First of all, a bit of background:
I am using the PSoC 5LP Prototyping Kit (CY8C5888LTI-LP097). I am currently working on a data-logging application that logs to an SD card using the EMFile component available.
The data is in the form of accelerometer data (ADXL362) and I have got it to generate interrupts and alert the PSoC when new data measurements are available. In my code, the PSoC application enters the interrupt service routine on these interrupts, retrieves the data (8-bit x, y & z) over the SPI interface and then stores this data in a buffer (uint8 accelBuffer[BUFFER_SIZE]). An instance variable (bufferIndex) keeps track of the current position of the buffer -- it increments to the next empty position within the buffer after each successive save of data.
Within the main application, the buffer level is checked through this index variable (bufferIndex). When bufferIndex == BUFFER_SIZE, the program enters a critical section in which interrupts are disabled. Here PSoC performs SD Card saving via the EMFile interface.
When the saving is completed, the program exits the critical section (interrupts are re-enabled), and the index variable is reset to 0 (first position of accelBuffer - i.e. accelBuffer(0)).
The program continues operating in this manner.
My question is whether there are more efficient ways of handling this SD Card saving -- i.e. being able to put the PSoC into low-power mode when nothing is going on (i.e. no interrupts generated by the accelerometer and no SD Card saving taking place). The current technique is constantly evaluating if the bufferIndex == BUFFER_SIZE in the main loop.
I am not sure how I can restructure this code so that:
(1) PSoC enters low-power mode when SD Card saving is not taking place. When an interrupt occurs, PSoC wakes up and services the interrupts (i.e. retrieving accelerometer data and saving to the buffer) and enter low-power mode again.
(2) When the buffer size is full, the PSoC 'wakes' up and saves the buffer contents to the SD Card, then enters low-power mode again.
The only way I imagine doing this is checking to see if the buffer is full (i.e. if(bufferIndex == BUFFER_SIZE) ...) within the mentioned ISR immediately after the new accelerometer data has been saved to the buffer. If indeed the buffer is full (i.e. bufferIndex == BUFFER_SIZE), I think I can software trigger another interrupt which saves the buffer contents to the SD Card. At all other times, PSoC will be in low-power mode. However, what I have heard is not to do this type of thing (SD card saving) within ISRs as they are meant for short pieces of code.
So, how best can I optimize the application and make use of PSoC's low-power mode capability?
Any advice, suggestions, recommendations will be highly appreciated! Thank you in advance!