![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
final firmware
Dependencies: USBMSD_BD max32630fthr USBDevice
Revision 0:a15c76864d7d, committed 2021-03-30
- Comitter:
- laumung
- Date:
- Tue Mar 30 18:07:30 2021 +0000
- Commit message:
- EVM-BIOZ
Changed in this revision
diff -r 000000000000 -r a15c76864d7d HSP/Devices/HspLed/HspLed/HspLed.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/HspLed/HspLed/HspLed.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,144 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "HspLed.h" +#include "Peripherals.h" + +void hspled_int_handler(void); + +//******************************************************************************* +HspLed::HspLed(PinName ledPin) : + redLed(LED_RED, 0), isStarted(false), timerIntervalLast(-1), timerInterval(500) { +} + + +//******************************************************************************* +void HspLed::state(int state) { + redLed.write(state); +} + + +//******************************************************************************* +void HspLed::toggle(void) { + state(!redLed.read()); +} + + +//******************************************************************************* +void HspLed::setMode(eMode mode) { + this->mode = mode; +} + +//******************************************************************************* +void HspLed::blink(uint32_t mSeconds) { + mode = eLedPeriod; + this->timerInterval = (float)mSeconds / 1000.0f; + start(); +} + +//******************************************************************************* +void HspLed::pattern(uint32_t bitPattern, uint32_t mSeconds) { + mode = eLedPattern; + this->bitPattern = bitPattern; + this->timerInterval = (float)mSeconds / 1000.0f; + start(); +} + +//******************************************************************************* +void HspLed::on(void) { + mode = eLedOn; + state(HSP_LED_ON); + start(); +} + + +//******************************************************************************* +void HspLed::off(void) { + mode = eLedOff; + state(HSP_LED_OFF); + start(); +} + + +//******************************************************************************* +void HspLed::patternToLed(void) { + uint32_t bit; + bit = bitPattern & 1; + state(bit); + // rotate the pattern + bitPattern = bitPattern >> 1; + bitPattern = bitPattern | (bit << 31); +} + + +//******************************************************************************* +void HspLed::service(void) { + switch (mode) { + case eLedOn: + state(HSP_LED_ON); + break; + case eLedOff: + state(HSP_LED_OFF); + break; + case eLedPeriod: + toggle(); + break; + case eLedPattern: + patternToLed(); + break; + } +} + +//******************************************************************************* +void HspLed::start(void) { + if (timerInterval != timerIntervalLast && isStarted == true) { + stop(); + } + ticker.attach(&hspled_int_handler, timerInterval); + timerIntervalLast = timerInterval; + + isStarted = true; +} + +//******************************************************************************* +void HspLed::stop(void) { + ticker.detach(); + isStarted = false; +} + +//******************************************************************************* +void hspled_int_handler(void) { + HspLed *hspLed = Peripherals::hspLed(); + hspLed->service(); +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/HspLed/HspLed/HspLed.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/HspLed/HspLed/HspLed.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,170 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _LED_H_ +#define _LED_H_ + +#include "mbed.h" + +/** + * Driver for the HSP Led, supports different blink rates and patterns + * + * @code + * #include <stdio.h> + * #include "mbed.h" + * #include "xxx.h" + * + * I2C i2c(I2C_SDA, I2C_SCL); + * xxx xxx(&i2c); + * + * int main(void) { + * printf("Initialized xxx\n"); + * while(1) { + * if (xxx.init() != 0) { + * printf("Error communicating with xxx\n"); + * } else { + * printf("Initialized xxx\n"); + * break; + * } + * wait(1); + * } + * + * while(1) { + * printf(""); + * wait(1); + * } + * } + * @endcode + */ + +class HspLed { +public: + static const int LED_ON = 0; + static const int LED_OFF = 1; + + /// define all of the modes the LED can support + typedef enum eMode { + eLedOn, + eLedOff, + eLedPeriod, + eLedPattern + } eMode; + /// define the values that turn the LED on or off at the pin + #define HSP_LED_ON 0 + #define HSP_LED_OFF 1 + + /* + * @brief Constructor where you specify the LED pin name + */ + HspLed(PinName ledPin); + + /** + * Blink the HSP LED at a set time interval + * @param mSeconds Number of seconds to set the timer interval + */ + void blink(uint32_t mSeconds); + + /** + * @brief Start rotating the LED through a 32-bit pattern at a mS rate specified + * @param pattern 32-bit pattern to rotate through + * @param mSeconds the amount of time to take per bit in the pattern + */ + void pattern(uint32_t pattern, uint32_t mSeconds); + + /** + * @brief Turn the LED on + */ + void on(void); + + /** + * @brief Turn the LED off + */ + void off(void); + + /** + * @brief Update the LED + */ + void service(void); + +private: + + /** + * Set the mode of the LED, the mode include blinking at a set rate or blinking + * according to a pattern + * @param mode Mode to set the LED to + */ + void setMode(eMode state); + + /** + * Toggle the state of the LED + */ + void toggle(void); + + /** + * Start the LED blinking or rotating through a pattern + */ + void start(void); + + /** + * Stop blinking or rotating through a pattern + */ + void stop(void); + + /** + * Write the LED pin to a state + * @param state A one or zero value to write to the LED pin + */ + void state(int state); + + /* + * @brief Single step through the pattern and output to the LED + */ + void patternToLed(void); + + /// timer interval in mS + float timerInterval; + /// last timer interval set to... used to prevent resetting the timer to the same value + float timerIntervalLast; + /// local state of the pattern to rotate through + uint32_t bitPattern; + /// current mode of the LED + eMode mode; + /// the LED digital output + DigitalOut redLed; + /// Timer service used to update the LED + Ticker ticker; + /// Flag to indicate if the timer has been started + bool isStarted; +}; + +#endif /* _LED_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/HspLed/HspLed_RPC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/HspLed/HspLed_RPC.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,74 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "HspLed_RPC.h" +#include "HspLed.h" +#include "StringHelper.h" +#include "Peripherals.h" + +int Led_On(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::hspLed()->on(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int Led_Off(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::hspLed()->off(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int Led_BlinkHz(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[1]; + uint32_t reply[1]; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + Peripherals::hspLed()->blink(args[0]); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int Led_BlinkPattern(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[2]; + uint32_t reply[1]; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + Peripherals::hspLed()->pattern(args[0], args[1]); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/HspLed/HspLed_RPC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/HspLed/HspLed_RPC.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _LED_RPC_H_ +#define _LED_RPC_H_ + +#include "mbed.h" + +int Led_On(char argStrs[32][32], char replyStrs[32][32]); +int Led_Off(char argStrs[32][32], char replyStrs[32][32]); +int Led_BlinkHz(char argStrs[32][32], char replyStrs[32][32]); +int Led_BlinkPattern(char argStrs[32][32], char replyStrs[32][32]); + +#endif /* _LED_RPC_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/MAX30001/MAX30001/MAX30001.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/MAX30001/MAX30001/MAX30001.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1299 @@ + +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "mbed.h" +#include "MAX30001.h" + +MAX30001 *MAX30001::instance = NULL; + + +//****************************************************************************** +MAX30001::MAX30001(PinName mosi, PinName miso, PinName sclk, PinName cs) +{ + m_spi = new SPI(mosi, miso, sclk); + m_cs = new DigitalOut(cs, 1); + + m_spi->frequency(3000000); + spi_owner = true; + functionpointer.attach(&spiHandler); + onDataAvailableCallback = NULL; + instance = this; +} + + +//****************************************************************************** +MAX30001::MAX30001(SPI *spi, DigitalOut *cs) +{ + m_spi = spi; + m_cs = cs; + spi->frequency(3000000); + spi_owner = false; + functionpointer.attach(&spiHandler); + onDataAvailableCallback = NULL; + instance = this; +} + + +//****************************************************************************** +MAX30001::~MAX30001(void) { + + if (spi_owner) { + delete m_spi; + delete m_cs; + } +} + +//****************************************************************************** +int MAX30001::max30001_Rbias_FMSTR_Init(uint8_t En_rbias, uint8_t Rbiasv, + uint8_t Rbiasp, uint8_t Rbiasn, + uint8_t Fmstr) { + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_rbias = En_rbias; + max30001_cnfg_gen.bit.rbiasv = Rbiasv; + max30001_cnfg_gen.bit.rbiasp = Rbiasp; + max30001_cnfg_gen.bit.rbiasn = Rbiasn; + max30001_cnfg_gen.bit.fmstr = Fmstr; + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_CAL_InitStart(uint8_t En_Vcal, uint8_t Vmode, + uint8_t Vmag, uint8_t Fcal, uint16_t Thigh, + uint8_t Fifty) { + // CNFG_CAL + if (max30001_reg_read(CNFG_CAL, &max30001_cnfg_cal.all) == -1) { + return -1; + } + + max30001_cnfg_cal.bit.vmode = Vmode; + max30001_cnfg_cal.bit.vmag = Vmag; + max30001_cnfg_cal.bit.fcal = Fcal; + max30001_cnfg_cal.bit.thigh = Thigh; + max30001_cnfg_cal.bit.fifty = Fifty; + + if (max30001_reg_write(CNFG_CAL, max30001_cnfg_cal.all) == -1) { + return -1; + } + + // RTOS uses a 32768HZ clock. 32768ticks represents 1secs. 1sec/10 = + // 100msecs. + wait(1.0 / 10.0); + + if (max30001_reg_read(CNFG_CAL, &max30001_cnfg_cal.all) == -1) { + return -1; + } + + max30001_cnfg_cal.bit.en_vcal = En_Vcal; + + if (max30001_reg_write(CNFG_CAL, max30001_cnfg_cal.all) == -1) { + return -1; + } + + // RTOS uses a 32768HZ clock. 32768ticks represents 1secs. 1sec/10 = + // 100msecs. + wait(1.0 / 10.0); + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_CAL_Stop(void) { + + if (max30001_reg_read(CNFG_CAL, &max30001_cnfg_cal.all) == -1) { + return -1; + } + + max30001_cnfg_cal.bit.en_vcal = 0; // Disable VCAL, all other settings are left unaffected + + if (max30001_reg_write(CNFG_CAL, max30001_cnfg_cal.all) == -1) { + return -1; + } + + return 0; +} +//****************************************************************************** +//****************************************************************************** +int MAX30001::max30001_INT_assignment(max30001_intrpt_Location_t en_enint_loc, max30001_intrpt_Location_t en_eovf_loc, max30001_intrpt_Location_t en_fstint_loc, + max30001_intrpt_Location_t en_dcloffint_loc, max30001_intrpt_Location_t en_bint_loc, max30001_intrpt_Location_t en_bovf_loc, + max30001_intrpt_Location_t en_bover_loc, max30001_intrpt_Location_t en_bundr_loc, max30001_intrpt_Location_t en_bcgmon_loc, + max30001_intrpt_Location_t en_pint_loc, max30001_intrpt_Location_t en_povf_loc, max30001_intrpt_Location_t en_pedge_loc, + max30001_intrpt_Location_t en_lonint_loc, max30001_intrpt_Location_t en_rrint_loc, max30001_intrpt_Location_t en_samp_loc, + max30001_intrpt_type_t intb_Type, max30001_intrpt_type_t int2b_Type) + + +{ + // INT1 + + if (max30001_reg_read(EN_INT, &max30001_en_int.all) == -1) { + return -1; + } + + // max30001_en_int2.bit.en_pint = 0b1; // Keep this off... + + max30001_en_int.bit.en_eint = 0b1 & en_enint_loc; + max30001_en_int.bit.en_eovf = 0b1 & en_eovf_loc; + max30001_en_int.bit.en_fstint = 0b1 & en_fstint_loc; + + max30001_en_int.bit.en_dcloffint = 0b1 & en_dcloffint_loc; + max30001_en_int.bit.en_bint = 0b1 & en_bint_loc; + max30001_en_int.bit.en_bovf = 0b1 & en_bovf_loc; + + max30001_en_int.bit.en_bover = 0b1 & en_bover_loc; + max30001_en_int.bit.en_bundr = 0b1 & en_bundr_loc; + max30001_en_int.bit.en_bcgmon = 0b1 & en_bcgmon_loc; + + max30001_en_int.bit.en_pint = 0b1 & en_pint_loc; + max30001_en_int.bit.en_povf = 0b1 & en_povf_loc; + max30001_en_int.bit.en_pedge = 0b1 & en_pedge_loc; + + max30001_en_int.bit.en_lonint = 0b1 & en_lonint_loc; + max30001_en_int.bit.en_rrint = 0b1 & en_rrint_loc; + max30001_en_int.bit.en_samp = 0b1 & en_samp_loc; + + max30001_en_int.bit.intb_type = intb_Type; + + if (max30001_reg_write(EN_INT, max30001_en_int.all) == -1) { + return -1; + } + + // INT2 + + if (max30001_reg_read(EN_INT2, &max30001_en_int2.all) == -1) { + return -1; + } + + max30001_en_int2.bit.en_eint = 0b1 & (en_enint_loc >> 1); + max30001_en_int2.bit.en_eovf = 0b1 & (en_eovf_loc >> 1); + max30001_en_int2.bit.en_fstint = 0b1 & (en_fstint_loc >> 1); + + max30001_en_int2.bit.en_dcloffint = 0b1 & (en_dcloffint_loc >> 1); + max30001_en_int2.bit.en_bint = 0b1 & (en_bint_loc >> 1); + max30001_en_int2.bit.en_bovf = 0b1 & (en_bovf_loc >> 1); + + max30001_en_int2.bit.en_bover = 0b1 & (en_bover_loc >> 1); + max30001_en_int2.bit.en_bundr = 0b1 & (en_bundr_loc >> 1); + max30001_en_int2.bit.en_bcgmon = 0b1 & (en_bcgmon_loc >> 1); + + max30001_en_int2.bit.en_pint = 0b1 & (en_pint_loc >> 1); + max30001_en_int2.bit.en_povf = 0b1 & (en_povf_loc >> 1); + max30001_en_int2.bit.en_pedge = 0b1 & (en_pedge_loc >> 1); + + max30001_en_int2.bit.en_lonint = 0b1 & (en_lonint_loc >> 1); + max30001_en_int2.bit.en_rrint = 0b1 & (en_rrint_loc >> 1); + max30001_en_int2.bit.en_samp = 0b1 & (en_samp_loc >> 1); + + max30001_en_int2.bit.intb_type = int2b_Type; + + if (max30001_reg_write(EN_INT2, max30001_en_int2.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_ECG_InitStart(uint8_t En_ecg, uint8_t Openp, + uint8_t Openn, uint8_t Pol, + uint8_t Calp_sel, uint8_t Caln_sel, + uint8_t E_fit, uint8_t Rate, uint8_t Gain, + uint8_t Dhpf, uint8_t Dlpf) { + + // CNFG_EMUX + + if (max30001_reg_read(CNFG_EMUX, &max30001_cnfg_emux.all) == -1) { + return -1; + } + + max30001_cnfg_emux.bit.openp = Openp; + max30001_cnfg_emux.bit.openn = Openn; + max30001_cnfg_emux.bit.pol = Pol; + max30001_cnfg_emux.bit.calp_sel = Calp_sel; + max30001_cnfg_emux.bit.caln_sel = Caln_sel; + + if (max30001_reg_write(CNFG_EMUX, max30001_cnfg_emux.all) == -1) { + return -1; + } + + /**** ENABLE CHANNELS ****/ + // CNFG_GEN + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_ecg = En_ecg; // 0b1 + + // fmstr is default + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + /**** Wait for PLL Lock & References to settle down ****/ + + max30001_timeout = 0; + + do { + if (max30001_reg_read(STATUS, &max30001_status.all) == -1) // Wait and spin for PLL to lock... + { + return -1; + } + } while (max30001_status.bit.pllint == 1 && max30001_timeout++ <= 1000); + + // MNGR_INT + + if (max30001_reg_read(MNGR_INT, &max30001_mngr_int.all) == -1) { + return -1; + } + + max30001_mngr_int.bit.e_fit = E_fit; // 31 + + if (max30001_reg_write(MNGR_INT, max30001_mngr_int.all) == -1) { + return -1; + } + + // CNFG_ECG + + if (max30001_reg_read(CNFG_ECG, &max30001_cnfg_ecg.all) == -1) { + return -1; + } + + max30001_cnfg_ecg.bit.rate = Rate; + max30001_cnfg_ecg.bit.gain = Gain; + max30001_cnfg_ecg.bit.dhpf = Dhpf; + max30001_cnfg_ecg.bit.dlpf = Dlpf; + + if (max30001_reg_write(CNFG_ECG, max30001_cnfg_ecg.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_ECGFast_Init(uint8_t Clr_Fast, uint8_t Fast, uint8_t Fast_Th) { + if (max30001_reg_read(MNGR_INT, &max30001_mngr_int.all) == -1) { + return -1; + } + + max30001_mngr_int.bit.clr_fast = Clr_Fast; + + if (max30001_reg_write(MNGR_INT, max30001_mngr_int.all) == -1) { + return -1; + } + + if (max30001_reg_read(MNGR_DYN, &max30001_mngr_dyn.all) == -1) { + return -1; + } + + max30001_mngr_dyn.bit.fast = Fast; + max30001_mngr_dyn.bit.fast_th = Fast_Th; + + if (max30001_reg_write(MNGR_INT, max30001_mngr_int.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_Stop_ECG(void) { + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_ecg = 0; // Stop ECG + + // fmstr is default + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_PACE_InitStart(uint8_t En_pace, uint8_t Clr_pedge, + uint8_t Pol, uint8_t Gn_diff_off, + uint8_t Gain, uint8_t Aout_lbw, + uint8_t Aout, uint8_t Dacp, + uint8_t Dacn) { + + /**** SET MASTER FREQUENCY, ENABLE CHANNELS ****/ + + // CNFG_GEN + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_pace = En_pace; // 0b1; + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + /**** Wait for PLL Lock & References to settle down ****/ + max30001_timeout = 0; + + do { + if (max30001_reg_read(STATUS, &max30001_status.all) == + -1) // Wait and spin for PLL to lock... + { + return -1; + } + + } while (max30001_status.bit.pllint == 1 && max30001_timeout++ <= 1000); + + // MNGR_INT + + if (max30001_reg_read(MNGR_INT, &max30001_mngr_int.all) == -1) { + return -1; + } + + max30001_mngr_int.bit.clr_pedge = Clr_pedge; // 0b0; + + if (max30001_reg_write(MNGR_INT, max30001_mngr_int.all) == -1) { + return -1; + } + + /* Put: CNFG_PACE */ + + max30001_reg_read(CNFG_PACE, &max30001_cnfg_pace.all); + + max30001_cnfg_pace.bit.pol = Pol; + max30001_cnfg_pace.bit.gn_diff_off = Gn_diff_off; + max30001_cnfg_pace.bit.gain = Gain; + max30001_cnfg_pace.bit.aout_lbw = Aout_lbw; + max30001_cnfg_pace.bit.aout = Aout; + max30001_cnfg_pace.bit.dacp = Dacp; + max30001_cnfg_pace.bit.dacn = Dacn; + + max30001_reg_write(CNFG_PACE, max30001_cnfg_pace.all); + + return 0; +} +//****************************************************************************** +int MAX30001::max30001_Stop_PACE(void) { + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_pace = 0; // Stop PACE + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_BIOZ_InitStart( + uint8_t En_bioz, uint8_t Openp, uint8_t Openn, uint8_t Calp_sel, + uint8_t Caln_sel, uint8_t CG_mode, uint8_t B_fit, uint8_t Rate, + uint8_t Ahpf, uint8_t Ext_rbias, uint8_t Gain, uint8_t Dhpf, uint8_t Dlpf, + uint8_t Fcgen, uint8_t Cgmon, uint8_t Cgmag, uint8_t Phoff, uint8_t Inapow_mode) { + + // CNFG_BMUX + + if (max30001_reg_read(CNFG_BMUX, &max30001_cnfg_bmux.all) == -1) { + return -1; + } + + max30001_cnfg_bmux.bit.openp = Openp; // 0b1; + max30001_cnfg_bmux.bit.openn = Openn; // 0b1; + max30001_cnfg_bmux.bit.calp_sel = Calp_sel; // 0b10; + max30001_cnfg_bmux.bit.caln_sel = Caln_sel; // 0b11; + max30001_cnfg_bmux.bit.cg_mode = CG_mode; // 0b00; + + if (max30001_reg_write(CNFG_BMUX, max30001_cnfg_bmux.all) == -1) { + return -1; + } + + /**** SET MASTER FREQUENCY, ENABLE CHANNELS ****/ + + // CNFG_GEN + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_bioz = En_bioz; + + // fmstr is default + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + /**** Wait for PLL Lock & References to settle down ****/ + + max30001_timeout = 0; + + do { + if (max30001_reg_read(STATUS, &max30001_status.all) == + -1) // Wait and spin for PLL to lock... + { + return -1; + } + + } while (max30001_status.bit.pllint == 1 && max30001_timeout++ <= 1000); + + /**** Start of CNFG_BIOZ ****/ + + // MNGR_INT + + if (max30001_reg_read(MNGR_INT, &max30001_mngr_int.all) == -1) { + return -1; + } + + max30001_mngr_int.bit.b_fit = B_fit; //; + + if (max30001_reg_write(MNGR_INT, max30001_mngr_int.all) == -1) { + return -1; + } + + // CNFG_BIOZ + + if (max30001_reg_read(CNFG_BIOZ, &max30001_cnfg_bioz.all) == -1) { + return -1; + } + + max30001_cnfg_bioz.bit.rate = Rate; + max30001_cnfg_bioz.bit.ahpf = Ahpf; + max30001_cnfg_bioz.bit.ext_rbias = Ext_rbias; + max30001_cnfg_bioz.bit.gain = Gain; + max30001_cnfg_bioz.bit.dhpf = Dhpf; + max30001_cnfg_bioz.bit.dlpf = Dlpf; + max30001_cnfg_bioz.bit.fcgen = Fcgen; + max30001_cnfg_bioz.bit.cgmon = Cgmon; + max30001_cnfg_bioz.bit.cgmag = Cgmag; + max30001_cnfg_bioz.bit.phoff = Phoff; + max30001_cnfg_bioz.bit.inapow_mode = Inapow_mode; + + if (max30001_reg_write(CNFG_BIOZ, max30001_cnfg_bioz.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_Stop_BIOZ(void) { + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_bioz = 0; // Stop BIOZ + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_BIOZ_InitBist(uint8_t En_bist, uint8_t Rnom, + uint8_t Rmod, uint8_t Fbist) { + + // CNFG_BMUX + + if (max30001_reg_read(CNFG_BMUX, &max30001_cnfg_bmux.all) == -1) { + return -1; + } + + max30001_cnfg_bmux.bit.en_bist = En_bist; + max30001_cnfg_bmux.bit.rnom = Rnom; + max30001_cnfg_bmux.bit.rmod = Rmod; + max30001_cnfg_bmux.bit.fbist = Fbist; + + if (max30001_reg_write(CNFG_BMUX, max30001_cnfg_bmux.all) == -1) { + return -1; + } + + return 0; +} +//****************************************************************************** +int MAX30001::max30001_RtoR_InitStart(uint8_t En_rtor, uint8_t Wndw, + uint8_t Gain, uint8_t Pavg, uint8_t Ptsf, + uint8_t Hoff, uint8_t Ravg, uint8_t Rhsf, + uint8_t Clr_rrint) { + + // MNGR_INT + + if (max30001_reg_read(MNGR_INT, &max30001_mngr_int.all) == -1) { + return -1; + } + + max30001_mngr_int.bit.clr_rrint = + Clr_rrint; // 0b01 & 0b00 are for interrupt mode... + // 0b10 is for monitoring mode... it just overwrites the data... + + if (max30001_reg_write(MNGR_INT, max30001_mngr_int.all) == -1) { + return -1; + } + + // RTOR1 + if (max30001_reg_read(CNFG_RTOR1, &max30001_cnfg_rtor1.all) == -1) { + return -1; + } + + max30001_cnfg_rtor1.bit.wndw = Wndw; + max30001_cnfg_rtor1.bit.gain = Gain; + max30001_cnfg_rtor1.bit.en_rtor = En_rtor; + max30001_cnfg_rtor1.bit.pavg = Pavg; + max30001_cnfg_rtor1.bit.ptsf = Ptsf; + + if (max30001_reg_write(CNFG_RTOR1, max30001_cnfg_rtor1.all) == -1) { + return -1; + } + // RTOR2 + + if (max30001_reg_read(CNFG_RTOR2, &max30001_cnfg_rtor2.all) == -1) { + return -1; + } + max30001_cnfg_rtor2.bit.hoff = Hoff; + max30001_cnfg_rtor2.bit.ravg = Ravg; + max30001_cnfg_rtor2.bit.rhsf = Rhsf; + + if (max30001_reg_write(CNFG_RTOR2, max30001_cnfg_rtor2.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_Stop_RtoR(void) { + + if (max30001_reg_read(CNFG_RTOR1, &max30001_cnfg_rtor1.all) == -1) { + return -1; + } + + max30001_cnfg_rtor1.bit.en_rtor = 0; // Stop RtoR + + if (max30001_reg_write(CNFG_RTOR1, max30001_cnfg_rtor1.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_PLL_lock(void) { + // Spin to see PLLint become zero to indicate a lock. + + max30001_timeout = 0; + + do { + if (max30001_reg_read(STATUS, &max30001_status.all) == + -1) // Wait and spin for PLL to lock... + { + return -1; + } + + } while (max30001_status.bit.pllint == 1 && max30001_timeout++ <= 1000); + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_sw_rst(void) { + // SW reset for the MAX30001 chip + + if (max30001_reg_write(SW_RST, 0x000000) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_synch(void) { // For synchronization + if (max30001_reg_write(SYNCH, 0x000000) == -1) { + return -1; + } + return 0; +} + +//****************************************************************************** +int MAX30001::max300001_fifo_rst(void) { // Resets the FIFO + if (max30001_reg_write(FIFO_RST, 0x000000) == -1) { + return -1; + } + return 0; +} + +//****************************************************************************** +// int MAX30001::max30001_reg_write(uint8_t addr, uint32_t data) +int MAX30001::max30001_reg_write(MAX30001_REG_map_t addr, uint32_t data) { + + uint8_t result[4]; + uint8_t data_array[4]; + int32_t success = 0; + + data_array[0] = (addr << 1) & 0xff; + + data_array[3] = data & 0xff; + data_array[2] = (data >> 8) & 0xff; + data_array[1] = (data >> 16) & 0xff; + + success = SPI_Transmit(&data_array[0], 4, &result[0], 4); + + if (success != 0) { + return -1; + } else { + return 0; + } +} + +//****************************************************************************** +// int MAX30001::max30001_reg_read(uint8_t addr, uint32_t *return_data) +int MAX30001::max30001_reg_read(MAX30001_REG_map_t addr, + uint32_t *return_data) { + uint8_t result[4]; + uint8_t data_array[1]; + int32_t success = 0; + + data_array[0] = ((addr << 1) & 0xff) | 1; // For Read, Or with 1 + success = SPI_Transmit(&data_array[0], 1, &result[0], 4); + *return_data = /*result[0] + */ (uint32_t)(result[1] << 16) + + (result[2] << 8) + result[3]; + if (success != 0) { + return -1; + } else { + return 0; + } +} + +//****************************************************************************** +int MAX30001::max30001_Enable_DcLeadOFF_Init(int8_t En_dcloff, int8_t Ipol, + int8_t Imag, int8_t Vth) { + // the leads are not touching the body + + // CNFG_EMUX, Set ECGP and ECGN for external hook up... + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_dcloff = En_dcloff; + max30001_cnfg_gen.bit.ipol = Ipol; + max30001_cnfg_gen.bit.imag = Imag; + max30001_cnfg_gen.bit.vth = Vth; + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_Disable_DcLeadOFF(void) { + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_dcloff = 0; // Turned off the dc lead off. + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_BIOZ_Enable_ACLeadOFF_Init(uint8_t En_bloff, + uint8_t Bloff_hi_it, + uint8_t Bloff_lo_it) { + + // CNFG_GEN + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_bloff = En_bloff; + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + // MNGR_DYN + if (max30001_reg_read(MNGR_DYN, &max30001_mngr_dyn.all) == -1) { + return -1; + } + + max30001_mngr_dyn.bit.bloff_hi_it = Bloff_hi_it; + max30001_mngr_dyn.bit.bloff_lo_it = Bloff_lo_it; + + if (max30001_reg_write(MNGR_DYN, max30001_mngr_dyn.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_BIOZ_Disable_ACleadOFF(void) { + // CNFG_GEN + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_bloff = 0b0; // Turns of the BIOZ AC Lead OFF feature + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30001::max30001_BIOZ_Enable_BCGMON(void) { + // CNFG_BIOZ + if (max30001_reg_read(CNFG_BIOZ, &max30001_cnfg_bioz.all) == -1) { + return -1; + } + + max30001_cnfg_bioz.bit.cgmon = 1; + + if (max30001_reg_write(CNFG_BIOZ, max30001_cnfg_bioz.all) == -1) { + return -1; + } + + max30001_reg_read(CNFG_BIOZ, &max30001_cnfg_bioz.all); + + return 0; +} + +#if 1 +//****************************************************************************** +int MAX30001::max30001_Enable_LeadON(int8_t Channel) // Channel: ECG = 0b01, BIOZ = 0b10, Disable = 0b00 +{ + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_ecg = 0b0; + max30001_cnfg_gen.bit.en_bioz = 0b0; + max30001_cnfg_gen.bit.en_pace = 0b0; + + max30001_cnfg_gen.bit.en_ulp_lon = Channel; // BIOZ ULP lead on detection... + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all); + + max30001_reg_read(STATUS, &max30001_status.all); + + return 0; +} +//****************************************************************************** +int MAX30001::max30001_Disable_LeadON(void) { + + if (max30001_reg_read(CNFG_GEN, &max30001_cnfg_gen.all) == -1) { + return -1; + } + + max30001_cnfg_gen.bit.en_ulp_lon = 0b0; + + if (max30001_reg_write(CNFG_GEN, max30001_cnfg_gen.all) == -1) { + return -1; + } + + return 0; +} +#endif +//****************************************************************************** +#define LEADOFF_SERVICE_TIME 0x2000 // 0x1000 = 1 second +#define LEADOFF_NUMSTATES 2 +uint32_t leadoffState = 0; +uint32_t max30001_LeadOffoldTime = 0; +void MAX30001::max30001_ServiceLeadoff(uint32_t currentTime) { + + uint32_t delta_Time; + + delta_Time = currentTime - max30001_LeadOffoldTime; + + if (delta_Time > LEADOFF_SERVICE_TIME) { + switch (leadoffState) { + case 0: /* switch to ECG DC Lead OFF */ + max30001_Enable_DcLeadOFF_Init(0b01, 0b0, 0b001, 0b00); + break; + + case 1: /* switch to BIOZ DC Lead OFF */ + max30001_Enable_DcLeadOFF_Init(0b10, 0b0, 0b001, 0b00); + break; + } + + leadoffState++; + leadoffState %= LEADOFF_NUMSTATES; + + max30001_LeadOffoldTime = currentTime; + } +} +//****************************************************************************** +#define LEADON_SERVICE_TIME 0x2000 // 0x1000 = 1 second +#define LEADON_NUMSTATES 2 +uint32_t leadOnState = 0; +uint32_t max30001_LeadOnoldTime = 0; +void MAX30001::max30001_ServiceLeadON(uint32_t currentTime) { + + uint32_t delta_Time; + + delta_Time = currentTime - max30001_LeadOnoldTime; + + if (delta_Time > LEADON_SERVICE_TIME) { + switch (leadOnState) { + case 0: /* switch to ECG DC Lead ON */ + max30001_Enable_LeadON(0b01); + break; + + case 1: /* switch to BIOZ DC Lead ON */ + max30001_Enable_LeadON(0b10); + break; + } + + leadOnState++; + leadOnState %= LEADON_NUMSTATES; + + max30001_LeadOnoldTime = currentTime; + } +} + +//****************************************************************************** +int MAX30001::max30001_FIFO_LeadONOff_Read(void) { + + uint8_t result[32 * 3]; // 32words - 3bytes each + uint8_t paceResult[6 * 3]; + uint8_t data_array[4]; + int32_t success = 0; + int i, j; + + uint32_t total_databytes; + uint8_t i_index; + uint8_t data_chunk; + uint8_t loop_logic; + + uint8_t etag, ptag, btag; + + uint8_t adr; + + //int8_t ReadAllPaceOnce; + bool anyPaceDetected = false; + static uint8_t dcloffint_OneShot = 0; + static uint8_t acloffint_OneShot = 0; + static uint8_t bcgmon_OneShot = 0; + static uint8_t acleadon_OneShot = 0; + + int8_t ret_val; + + if (max30001_status.bit.eint == 1) { + adr = ECG_FIFO_BURST; + data_array[0] = ((adr << 1) & 0xff) | 1; + + // The SPI routine only sends out data of 32 bytes in size. Therefore the + // data is being read in + // smaller chunks in this routine... + + total_databytes = (max30001_mngr_int.bit.e_fit + 1) * 3; + + i_index = 0; + loop_logic = 1; + + while (loop_logic) { + if (total_databytes > 30) { + data_chunk = 30; + total_databytes = total_databytes - 30; + } else { + data_chunk = total_databytes; + loop_logic = 0; + } + + /* The extra 1 byte is for the extra byte that comes out of the SPI */ + success = SPI_Transmit(&data_array[0], 1, &result[i_index], (data_chunk + 1)); // Make a copy of the FIFO over here... + + if (success != 0) { + return -1; + } + + /* This is important, because every transaction above creates an empty + * redundant data at result[0] */ + for (j = i_index; j < (data_chunk + i_index); j++) /* get rid of the 1 extra byte by moving the whole array up one */ + { + result[j] = result[j + 1]; + } + + i_index = i_index + 30; /* point to the next array location to put the data in */ + } + + //ReadAllPaceOnce = 0; + + /* Put the content of the FIFO based on the EFIT value, We ignore the + * result[0] and start concatenating indexes: 1,2,3 - 4,5,6 - 7,8,9 - */ + for (i = 0, j = 0; i < max30001_mngr_int.bit.e_fit + 1; i++, j = j + 3) // index1=23-16 bit, index2=15-8 bit, index3=7-0 bit + { + max30001_ECG_FIFO_buffer[i] = ((uint32_t)result[j] << 16) + (result[j + 1] << 8) + result[j + 2]; + + etag = (0b00111000 & result[j + 2]) >> 3; + ptag = 0b00000111 & result[j + 2]; + + if (ptag != 0b111 ){//&& ReadAllPaceOnce == 0) { + + //ReadAllPaceOnce = 1; // This will prevent extra read of PACE, once group + // 0-5 is read ONCE. + readPace(ptag, paceResult); // BUG: result data from ECG is being overwritten by the PACE data + anyPaceDetected = true; + } + } + + if (anyPaceDetected) + dataAvailable(MAX30001_DATA_PACE, max30001_PACE, 18); // Send out the Pace data once only + + if (etag != 0b110) { + + dataAvailable(MAX30001_DATA_ECG, max30001_ECG_FIFO_buffer, (max30001_mngr_int.bit.e_fit + 1)); + } + + } /* End of ECG init */ + + /* RtoR */ + + if (max30001_status.bit.rrint == 1) { + if (max30001_reg_read(RTOR, &max30001_RtoR_data) == -1) { + return -1; + } + + max30001_RtoR_data = (0x00FFFFFF & max30001_RtoR_data) >> 10; + + hspValMax30001.R2R = (uint16_t)max30001_RtoR_data; + hspValMax30001.fmstr = (uint16_t)max30001_cnfg_gen.bit.fmstr; + + dataAvailable(MAX30001_DATA_RTOR, &max30001_RtoR_data, 1); + } + + // Handling BIOZ data... + + if (max30001_status.bit.bint == 1) { + adr = 0x22; + data_array[0] = ((adr << 1) & 0xff) | 1; + + /* [(BFIT+1)*3byte]+1extra byte due to the addr */ + + if (SPI_Transmit(&data_array[0], 1, &result[0],((max30001_mngr_int.bit.b_fit + 1) * 3) + 1) == -1) // Make a copy of the FIFO over here... + + { + return -1; + } + + btag = 0b00000111 & result[3]; + + /* Put the content of the FIFO based on the BFIT value, We ignore the + * result[0] and start concatenating indexes: 1,2,3 - 4,5,6 - 7,8,9 - */ + for (i = 0, j = 0; i < max30001_mngr_int.bit.b_fit + 1; i++, j = j + 3) // index1=23-16 bit, index2=15-8 bit, index3=7-0 bit + { + max30001_BIOZ_FIFO_buffer[i] = ((uint32_t)result[j + 1] << 16) + (result[j + 2] << 8) + result[j + 3]; + } + + if (btag != 0b110) { + dataAvailable(MAX30001_DATA_BIOZ, max30001_BIOZ_FIFO_buffer, 8); + } + } + + ret_val = 0; + + if (max30001_status.bit.dcloffint == 1) // ECG/BIOZ Lead Off + { + dcloffint_OneShot = 1; + max30001_DCLeadOff = 0; + max30001_DCLeadOff = max30001_DCLeadOff | (max30001_cnfg_gen.bit.en_dcloff << 8) | (max30001_status.all & 0x00000F); + dataAvailable(MAX30001_DATA_LEADOFF_DC, &max30001_DCLeadOff, 1); + + ret_val = 0b100; + + } else if (dcloffint_OneShot == 1 && max30001_status.bit.dcloffint == 0) // Just send once when it comes out of dc lead off + { + max30001_DCLeadOff = 0; + max30001_DCLeadOff = max30001_DCLeadOff | (max30001_cnfg_gen.bit.en_dcloff << 8) | (max30001_status.all & 0x00000F); + dataAvailable(MAX30001_DATA_LEADOFF_DC, &max30001_DCLeadOff, 1); + dcloffint_OneShot = 0; + } + + if (max30001_status.bit.bover == 1 || max30001_status.bit.bundr == 1) // BIOZ AC Lead Off + { + acloffint_OneShot = 1; + max30001_ACLeadOff = 0; + max30001_ACLeadOff = + max30001_ACLeadOff | ((max30001_status.all & 0x030000) >> 16); + dataAvailable(MAX30001_DATA_LEADOFF_AC, &max30001_ACLeadOff, 1); + + ret_val = 0b1000; + } else if (acloffint_OneShot == 1 && max30001_status.bit.bover == 0 && max30001_status.bit.bundr == 0) // Just send once when it comes out of ac lead off + { + max30001_ACLeadOff = 0; + max30001_ACLeadOff = max30001_ACLeadOff | ((max30001_status.all & 0x030000) >> 16); + dataAvailable(MAX30001_DATA_LEADOFF_AC, &max30001_ACLeadOff, 1); + acloffint_OneShot = 0; + } + + if (max30001_status.bit.bcgmon == 1) // BIOZ BCGMON check + { + bcgmon_OneShot = 1; + max30001_bcgmon = 0; + max30001_bcgmon = max30001_bcgmon | ((max30001_status.all & 0x000030) >> 4); + dataAvailable(MAX30001_DATA_BCGMON, &max30001_bcgmon, 1); + + ret_val = 0b10000; + } else if (bcgmon_OneShot == 1 && max30001_status.bit.bcgmon == 0) { + max30001_bcgmon = 0; + max30001_bcgmon = max30001_bcgmon | ((max30001_status.all & 0x000030) >> 4); + bcgmon_OneShot = 0; + dataAvailable(MAX30001_DATA_BCGMON, &max30001_bcgmon, 1); + } + +#if 0 +if(max30001_status.bit.lonint == 1) // AC LeadON Check +{ + max30001_LeadOn = 0; + max30001_reg_read(STATUS,&max30001_status.all); // Reading is important + max30001_LeadOn = max30001_LeadOn | (max30001_cnfg_gen.bit.en_ulp_lon << 8) | ((max30001_status.all & 0x000800) >> 11); // 0b01 will mean ECG Lead On, 0b10 will mean BIOZ Lead On + // LEAD ON has been detected... Now take actions +} +#endif + + if (max30001_status.bit.lonint == 1 && + acleadon_OneShot == 0) // AC LeadON Check, when lead is on + { + max30001_LeadOn = 0; + max30001_reg_read(STATUS, &max30001_status.all); // Reading is important + max30001_LeadOn = + max30001_LeadOn | (max30001_cnfg_gen.bit.en_ulp_lon << 8) | + ((max30001_status.all & 0x000800) >> + 11); // 0b01 will mean ECG Lead On, 0b10 will mean BIOZ Lead On + + // LEAD ON has been detected... Now take actions + acleadon_OneShot = 1; + dataAvailable(MAX30001_DATA_ACLEADON, &max30001_LeadOn, 1); // One shot data will be sent... + } else if (max30001_status.bit.lonint == 0 && acleadon_OneShot == 1) { + max30001_LeadOn = 0; + max30001_reg_read(STATUS, &max30001_status.all); + max30001_LeadOn = + max30001_LeadOn | (max30001_cnfg_gen.bit.en_ulp_lon << 8) | ((max30001_status.all & 0x000800) >> 11); // 0b01 will mean ECG Lead On, 0b10 will mean BIOZ Lead On + dataAvailable(MAX30001_DATA_ACLEADON, &max30001_LeadOn, 1); // One shot data will be sent... + acleadon_OneShot = 0; + } + + return ret_val; +} + +//****************************************************************************** +uint32_t MAX30001::readPace(int group, uint8_t* result) { + uint8_t data_array[4]; + uint32_t success; + int adr = PACE0_FIFO_BURST + group*4; + + if (group >= 6) + return (uint32_t)-1; + + data_array[0] = ((adr << 1) & 0xff) | 1; // For Read Or with 1 + success = SPI_Transmit(&data_array[0], 1, &result[0], 10); + + max30001_PACE[group * 3 + 0] = (uint32_t)(result[1] << 16) + (result[2] << 8) + result[3]; + max30001_PACE[group * 3 + 1] = (uint32_t)(result[4] << 16) + (result[5] << 8) + result[6]; + max30001_PACE[group * 3 + 2] = (uint32_t)(result[7] << 16) + (result[8] << 8) + result[9]; + + return success; +} + +//****************************************************************************** + +//****************************************************************************** + +int MAX30001::max30001_int_handler(void) { + + static uint32_t InitReset = 0; + + int8_t return_value; + bool check_one_more = true; + + status_check: + max30001_reg_read(STATUS, &max30001_status.all); + + // Inital Reset and any FIFO over flow invokes a FIFO reset + if (InitReset == 0 || max30001_status.bit.eovf == 1 || max30001_status.bit.bovf == 1) { + // Do a FIFO Reset + max30001_reg_write(FIFO_RST, 0x000000); + + InitReset++; + return 2; + } + + return_value = 0; + + // The four data handling goes on over here + if (max30001_status.bit.eint == 1 || max30001_status.bit.pint == 1 || max30001_status.bit.bint == 1 || max30001_status.bit.rrint == 1 + || max30001_status.bit.dcloffint == 1 || max30001_status.bit.bover == 1 || max30001_status.bit.bundr == 1 + || max30001_status.bit.bcgmon == 1 || max30001_status.bit.lonint == 1) { + return_value = return_value | max30001_FIFO_LeadONOff_Read(); + } +/* + // ECG/BIOZ DC Lead Off test + if (max30001_status.bit.dcloffint == 1) { + return_value = return_value | max30001_FIFO_LeadONOff_Read(); + } + + // BIOZ AC Lead Off test + if (max30001_status.bit.bover == 1 || max30001_status.bit.bundr == 1) { + return_value = return_value | max30001_FIFO_LeadONOff_Read(); + } + + // BIOZ DRVP/N test using BCGMON. + if (max30001_status.bit.bcgmon == 1) { + return_value = return_value | max30001_FIFO_LeadONOff_Read(); + } + + if (max30001_status.bit.lonint == 1) // ECG Lead ON test: i.e. the leads are touching the body... + { + + max30001_FIFO_LeadONOff_Read(); + } +*/ + if (check_one_more) { + check_one_more = false; + goto status_check; + } + return return_value; +} + +/// function pointer to the async callback +static event_callback_t functionpointer; +/// flag used to indicate an async xfer has taken place +static volatile int xferFlag = 0; + +/** +* @brief Callback handler for SPI async events +* @param events description of event that occurred +*/ + + +static void spiHandler(int events) { xferFlag = 1; } + +/** +* @brief Transmit and recieve QUAD SPI data +* @param tx_buf pointer to transmit byte buffer +* @param tx_size number of bytes to transmit +* @param rx_buf pointer to the recieve buffer +* @param rx_size number of bytes to recieve +*/ +int MAX30001::SPI_Transmit(const uint8_t *tx_buf, uint32_t tx_size, uint8_t *rx_buf, uint32_t rx_size) +{ + m_cs->write(0); + for(int i = 0; i < tx_size; i++) + { + m_spi->write(tx_buf[i]); + } + for(int i = 0; i < (rx_size - tx_size); i++) + { + rx_buf[i + 1] = m_spi->write(0xFF); + } + m_cs->write(1); + + return 0; +} + +//****************************************************************************** +void MAX30001::max30001_ReadHeartrateData(max30001_t *_hspValMax30001) { + _hspValMax30001->R2R = hspValMax30001.R2R; + _hspValMax30001->fmstr = hspValMax30001.fmstr; +} + +//****************************************************************************** +void MAX30001::onDataAvailable(PtrFunction _onDataAvailable) { + onDataAvailableCallback = _onDataAvailable; +} + +/** +* @brief Used to notify an external function that interrupt data is available +* @param id type of data available +* @param buffer 32-bit buffer that points to the data +* @param length length of 32-bit elements available +*/ +void MAX30001::dataAvailable(uint32_t id, uint32_t *buffer, uint32_t length) { + if (onDataAvailableCallback != NULL) { + (*onDataAvailableCallback)(id, buffer, length); + } +} + +/** +* @brief Callback handler for SPI async events +* @param events description of event that occurred +*/ +void MAX30001::spiHandler(int events) { xferFlag = 1; } + +//****************************************************************************** +static int allowInterrupts = 0; + +void MAX30001Mid_IntB_Handler(void) { + if (allowInterrupts == 0) return; + MAX30001::instance->max30001_int_handler(); +} + +void MAX30001Mid_Int2B_Handler(void) { + if (allowInterrupts == 0) return; + MAX30001::instance->max30001_int_handler(); +} + +void MAX30001_AllowInterrupts(int state) { +allowInterrupts = state; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/MAX30001/MAX30001/MAX30001.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/MAX30001/MAX30001/MAX30001.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1083 @@ +/******************************************************************************* +* Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +*******************************************************************************/ +/* + * max30001.h + * + * Created on: Oct 9, 2015 + * Author: faisal.tariq + */ + +#ifndef MAX30001_H_ +#define MAX30001_H_ + +#include "mbed.h" + +#define mbed_COMPLIANT // Uncomment to Use timer for MAX30001 FCLK (for mbed) + // Comment to use the RTC clock + +#define ASYNC_SPI_BUFFER_SIZE (32 * 3) // Maximimum buffer size for async byte transfers + +// Defines for data callbacks +#define MAX30001_DATA_ECG 0x30 +#define MAX30001_DATA_PACE 0x31 +#define MAX30001_DATA_RTOR 0x32 +#define MAX30001_DATA_BIOZ 0x33 +#define MAX30001_DATA_LEADOFF_DC 0x34 +#define MAX30001_DATA_LEADOFF_AC 0x35 +#define MAX30001_DATA_BCGMON 0x36 +#define MAX30001_DATA_ACLEADON 0x37 + +#define MAX30001_SPI_MASTER_PORT 0 +#define MAX30001_SPI_SS_INDEX 0 + +#define MAX30001_INT_PORT_B 3 +#define MAX30001_INT_PIN_B 6 + +#define MAX30001_INT_PORT_2B 4 +#define MAX30001_INT_PIN_2B 5 + +#define MAX30001_INT_PORT_FCLK 1 +#define MAX30001_INT_PIN_FCLK 7 + +#define MAX30001_FUNC_SEL_TMR 2 // 0=FW Control, 1= Pulse Train, 2=Timer + +#define MAX30001_INDEX 3 +#define MAX30001_POLARITY 0 +#define MAX30001_PERIOD 30518 +#define MAX30001_CYCLE 50 + +#define MAX30001_IOMUX_IO_ENABLE 1 + +#define MAX30001_SPI_PORT 0 +#define MAX30001_CS_PIN 0 +#define MAX30001_CS_POLARITY 0 +#define MAX30001_CS_ACTIVITY_DELAY 0 +#define MAX30001_CS_INACTIVITY_DELAY 0 +#define MAX30001_CLK_HI 1 +#define MAX30001_CLK_LOW 1 +#define MAX30001_ALT_CLK 0 +#define MAX30001_CLK_POLARITY 0 +#define MAX30001_CLK_PHASE 0 +#define MAX30001_WRITE 1 +#define MAX30001_READ 0 + +#define MAX30001_INT_PORT_B 3 +#define MAX30001INT_PIN_B 6 + +void MAX30001_AllowInterrupts(int state); + +/** +* Maxim Integrated MAX30001 ECG/BIOZ chip +*/ +class MAX30001 { + +public: + typedef enum { // MAX30001 Register addresses + STATUS = 0x01, + EN_INT = 0x02, + EN_INT2 = 0x03, + MNGR_INT = 0x04, + MNGR_DYN = 0x05, + SW_RST = 0x08, + SYNCH = 0x09, + FIFO_RST = 0x0A, + INFO = 0x0F, + CNFG_GEN = 0x10, + CNFG_CAL = 0x12, + CNFG_EMUX = 0x14, + CNFG_ECG = 0x15, + CNFG_BMUX = 0x17, + CNFG_BIOZ = 0x18, + CNFG_PACE = 0x1A, + CNFG_RTOR1 = 0x1D, + CNFG_RTOR2 = 0x1E, + + // Data locations + ECG_FIFO_BURST = 0x20, + ECG_FIFO = 0x21, + FIFO_BURST = 0x22, + BIOZ_FIFO = 0x23, + RTOR = 0x25, + + PACE0_FIFO_BURST = 0x30, + PACE0_A = 0x31, + PACE0_B = 0x32, + PACE0_C = 0x33, + + PACE1_FIFO_BURST = 0x34, + PACE1_A = 0x35, + PACE1_B = 0x36, + PACE1_C = 0x37, + + PACE2_FIFO_BURST = 0x38, + PACE2_A = 0x39, + PACE2_B = 0x3A, + PACE2_C = 0x3B, + + PACE3_FIFO_BURST = 0x3C, + PACE3_A = 0x3D, + PACE3_B = 0x3E, + PACE3_C = 0x3F, + + PACE4_FIFO_BURST = 0x40, + PACE4_A = 0x41, + PACE4_B = 0x42, + PACE4_C = 0x43, + + PACE5_FIFO_BURST = 0x44, + PACE5_A = 0x45, + PACE5_B = 0x46, + PACE5_C = 0x47, + + } MAX30001_REG_map_t; + + /** + * @brief STATUS (0x01) + */ + union max30001_status_reg { + uint32_t all; + + struct { + uint32_t loff_nl : 1; + uint32_t loff_nh : 1; + uint32_t loff_pl : 1; + uint32_t loff_ph : 1; + + uint32_t bcgmn : 1; + uint32_t bcgmp : 1; + uint32_t reserved1 : 1; + uint32_t reserved2 : 1; + + uint32_t pllint : 1; + uint32_t samp : 1; + uint32_t rrint : 1; + uint32_t lonint : 1; + + uint32_t pedge : 1; + uint32_t povf : 1; + uint32_t pint : 1; + uint32_t bcgmon : 1; + + uint32_t bundr : 1; + uint32_t bover : 1; + uint32_t bovf : 1; + uint32_t bint : 1; + + uint32_t dcloffint : 1; + uint32_t fstint : 1; + uint32_t eovf : 1; + uint32_t eint : 1; + + uint32_t reserved : 8; + + } bit; + + } max30001_status; + + + /** + * @brief EN_INT (0x02) + */ + + union max30001_en_int_reg { + uint32_t all; + + struct { + uint32_t intb_type : 2; + uint32_t reserved1 : 1; + uint32_t reserved2 : 1; + + uint32_t reserved3 : 1; + uint32_t reserved4 : 1; + uint32_t reserved5 : 1; + uint32_t reserved6 : 1; + + uint32_t en_pllint : 1; + uint32_t en_samp : 1; + uint32_t en_rrint : 1; + uint32_t en_lonint : 1; + + uint32_t en_pedge : 1; + uint32_t en_povf : 1; + uint32_t en_pint : 1; + uint32_t en_bcgmon : 1; + + uint32_t en_bundr : 1; + uint32_t en_bover : 1; + uint32_t en_bovf : 1; + uint32_t en_bint : 1; + + uint32_t en_dcloffint : 1; + uint32_t en_fstint : 1; + uint32_t en_eovf : 1; + uint32_t en_eint : 1; + + uint32_t reserved : 8; + + } bit; + + } max30001_en_int; + + + /** + * @brief EN_INT2 (0x03) + */ + union max30001_en_int2_reg { + uint32_t all; + + struct { + uint32_t intb_type : 2; + uint32_t reserved1 : 1; + uint32_t reserved2 : 1; + + uint32_t reserved3 : 1; + uint32_t reserved4 : 1; + uint32_t reserved5 : 1; + uint32_t reserved6 : 1; + + uint32_t en_pllint : 1; + uint32_t en_samp : 1; + uint32_t en_rrint : 1; + uint32_t en_lonint : 1; + + uint32_t en_pedge : 1; + uint32_t en_povf : 1; + uint32_t en_pint : 1; + uint32_t en_bcgmon : 1; + + uint32_t en_bundr : 1; + uint32_t en_bover : 1; + uint32_t en_bovf : 1; + uint32_t en_bint : 1; + + uint32_t en_dcloffint : 1; + uint32_t en_fstint : 1; + uint32_t en_eovf : 1; + uint32_t en_eint : 1; + + uint32_t reserved : 8; + + } bit; + + } max30001_en_int2; + + /** + * @brief MNGR_INT (0x04) + */ + union max30001_mngr_int_reg { + uint32_t all; + + struct { + uint32_t samp_it : 2; + uint32_t clr_samp : 1; + uint32_t clr_pedge : 1; + uint32_t clr_rrint : 2; + uint32_t clr_fast : 1; + uint32_t reserved1 : 1; + uint32_t reserved2 : 4; + uint32_t reserved3 : 4; + + uint32_t b_fit : 3; + uint32_t e_fit : 5; + + uint32_t reserved : 8; + + } bit; + + } max30001_mngr_int; + + /** + * @brief MNGR_DYN (0x05) + */ + union max30001_mngr_dyn_reg { + uint32_t all; + + struct { + uint32_t bloff_lo_it : 8; + uint32_t bloff_hi_it : 8; + uint32_t fast_th : 6; + uint32_t fast : 2; + uint32_t reserved : 8; + } bit; + + } max30001_mngr_dyn; + + // 0x08 + // uint32_t max30001_sw_rst; + + // 0x09 + // uint32_t max30001_synch; + + // 0x0A + // uint32_t max30001_fifo_rst; + + + /** + * @brief INFO (0x0F) + */ + union max30001_info_reg { + uint32_t all; + struct { + uint32_t serial : 12; + uint32_t part_id : 2; + uint32_t sample : 1; + uint32_t reserved1 : 1; + uint32_t rev_id : 4; + uint32_t pattern : 4; + uint32_t reserved : 8; + } bit; + + } max30001_info; + + /** + * @brief CNFG_GEN (0x10) + */ + union max30001_cnfg_gen_reg { + uint32_t all; + struct { + uint32_t rbiasn : 1; + uint32_t rbiasp : 1; + uint32_t rbiasv : 2; + uint32_t en_rbias : 2; + uint32_t vth : 2; + uint32_t imag : 3; + uint32_t ipol : 1; + uint32_t en_dcloff : 2; + uint32_t en_bloff : 2; + uint32_t reserved1 : 1; + uint32_t en_pace : 1; + uint32_t en_bioz : 1; + uint32_t en_ecg : 1; + uint32_t fmstr : 2; + uint32_t en_ulp_lon : 2; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_gen; + + + /** + * @brief CNFG_CAL (0x12) + */ + union max30001_cnfg_cal_reg { + uint32_t all; + struct { + uint32_t thigh : 11; + uint32_t fifty : 1; + uint32_t fcal : 3; + uint32_t reserved1 : 5; + uint32_t vmag : 1; + uint32_t vmode : 1; + uint32_t en_vcal : 1; + uint32_t reserved2 : 1; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_cal; + + /** + * @brief CNFG_EMUX (0x14) + */ + union max30001_cnfg_emux_reg { + uint32_t all; + struct { + uint32_t reserved1 : 16; + uint32_t caln_sel : 2; + uint32_t calp_sel : 2; + uint32_t openn : 1; + uint32_t openp : 1; + uint32_t reserved2 : 1; + uint32_t pol : 1; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_emux; + + + /** + * @brief CNFG_ECG (0x15) + */ + union max30001_cnfg_ecg_reg { + uint32_t all; + struct { + uint32_t reserved1 : 12; + uint32_t dlpf : 2; + uint32_t dhpf : 1; + uint32_t reserved2 : 1; + uint32_t gain : 2; + uint32_t reserved3 : 4; + uint32_t rate : 2; + + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_ecg; + + /** + * @brief CNFG_BMUX (0x17) + */ + union max30001_cnfg_bmux_reg { + uint32_t all; + struct { + uint32_t fbist : 2; + uint32_t reserved1 : 2; + uint32_t rmod : 3; + uint32_t reserved2 : 1; + uint32_t rnom : 3; + uint32_t en_bist : 1; + uint32_t cg_mode : 2; + uint32_t reserved3 : 2; + uint32_t caln_sel : 2; + uint32_t calp_sel : 2; + uint32_t openn : 1; + uint32_t openp : 1; + uint32_t reserved4 : 2; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_bmux; + + /** + * @brief CNFG_BIOZ (0x18) + */ + union max30001_bioz_reg { + uint32_t all; + struct { + uint32_t phoff : 4; + uint32_t cgmag : 3; + uint32_t cgmon : 1; + uint32_t fcgen : 4; + uint32_t dlpf : 2; + uint32_t dhpf : 2; + uint32_t gain : 2; + uint32_t inapow_mode : 1; + uint32_t ext_rbias : 1; + uint32_t ahpf : 3; + uint32_t rate : 1; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_bioz; + + + /** + * @brief CNFG_PACE (0x1A) + */ + union max30001_cnfg_pace_reg { + uint32_t all; + + struct { + uint32_t dacn : 4; + uint32_t dacp : 4; + uint32_t reserved1 : 4; + uint32_t aout : 2; + uint32_t aout_lbw : 1; + uint32_t reserved2 : 1; + uint32_t gain : 3; + uint32_t gn_diff_off : 1; + uint32_t reserved3 : 3; + uint32_t pol : 1; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_pace; + + /** + * @brief CNFG_RTOR1 (0x1D) + */ + union max30001_cnfg_rtor1_reg { + uint32_t all; + struct { + uint32_t reserved1 : 8; + uint32_t ptsf : 4; + uint32_t pavg : 2; + uint32_t reserved2 : 1; + uint32_t en_rtor : 1; + uint32_t gain : 4; + uint32_t wndw : 4; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_rtor1; + + /** + * @brief CNFG_RTOR2 (0x1E) + */ + union max30001_cnfg_rtor2_reg { + uint32_t all; + struct { + uint32_t reserved1 : 8; + uint32_t rhsf : 3; + uint32_t reserved2 : 1; + uint32_t ravg : 2; + uint32_t reserved3 : 2; + uint32_t hoff : 6; + uint32_t reserved4 : 2; + uint32_t reserved : 8; + } bit; + + } max30001_cnfg_rtor2; + + /*********************************************************************************/ + + typedef enum { + MAX30001_NO_INT = 0, // No interrupt + MAX30001_INT_B = 1, // INTB selected for interrupt + MAX30001_INT_2B = 2 // INT2B selected for interrupt + } max30001_intrpt_Location_t; + + typedef enum { + MAX30001_INT_DISABLED = 0b00, + MAX30001_INT_CMOS = 0b01, + MAX30001_INT_ODN = 0b10, + MAX30001_INT_ODNR = 0b11 + } max30001_intrpt_type_t; + + typedef enum { // Input Polarity selection + MAX30001_NON_INV = 0, // Non-Inverted + MAX30001_INV = 1 // Inverted + } max30001_emux_pol; + + typedef enum { // OPENP and OPENN setting + MAX30001_ECG_CON_AFE = 0, // ECGx is connected to AFE channel + MAX30001_ECG_ISO_AFE = 1 // ECGx is isolated from AFE channel + } max30001_emux_openx; + + typedef enum { // EMUX_CALP_SEL & EMUX_CALN_SEL + MAX30001_NO_CAL_SIG = 0b00, // No calibration signal is applied + MAX30001_INPT_VMID = 0b01, // Input is connected to VMID + MAX30001_INPT_VCALP = 0b10, // Input is connected to VCALP + MAX30001_INPT_VCALN = 0b11 // Input is connected to VCALN + } max30001_emux_calx_sel; + + typedef enum { // EN_ECG, EN_BIOZ, EN_PACE + MAX30001_CHANNEL_DISABLED = 0b0, // + MAX30001_CHANNEL_ENABLED = 0b1 + } max30001_en_feature; + + /*********************************************************************************/ + // Data + uint32_t max30001_ECG_FIFO_buffer[32]; // (303 for internal test) + uint32_t max30001_BIOZ_FIFO_buffer[8]; // (303 for internal test) + + uint32_t max30001_PACE[18]; // Pace Data 0-5 + + uint32_t max30001_RtoR_data; // This holds the RtoR data + + uint32_t max30001_DCLeadOff; // This holds the LeadOff data, Last 4 bits give + // the status, BIT3=LOFF_PH, BIT2=LOFF_PL, + // BIT1=LOFF_NH, BIT0=LOFF_NL + // 8th and 9th bits tell Lead off is due to ECG or BIOZ. + // 0b01 = ECG Lead Off and 0b10 = BIOZ Lead off + + uint32_t max30001_ACLeadOff; // This gives the state of the BIOZ AC Lead Off + // state. BIT 1 = BOVER, BIT 0 = BUNDR + + uint32_t max30001_bcgmon; // This holds the BCGMON data, BIT 1 = BCGMP, BIT0 = + // BCGMN + + uint32_t max30001_LeadOn; // This holds the LeadOn data, BIT1 = BIOZ Lead ON, + // BIT0 = ECG Lead ON, BIT8= Lead On Status Bit + + uint32_t max30001_timeout; // If the PLL does not respond, timeout and get out. + + typedef struct { // Creating a structure for BLE data + int16_t R2R; + int16_t fmstr; + } max30001_t; + + max30001_t hspValMax30001; // R2R, FMSTR + + //jjj 14MAR17 + //added DigitalOut so we can use any pin for cs + //jjj + MAX30001(SPI *spi, DigitalOut *cs); + + + /** + * @brief Constructor that accepts pin names for the SPI interface + * @param mosi master out slave in pin name + * @param miso master in slave out pin name + * @param sclk serial clock pin name + * @param cs chip select pin name + */ + MAX30001(PinName mosi, PinName miso, PinName sclk, PinName cs); + + /** + * MAX30001 destructor + */ + ~MAX30001(void); + + /** + * @brief This function sets up the Resistive Bias mode and also selects the master clock frequency. + * @brief Uses Register: CNFG_GEN-0x10 + * @param En_rbias: Enable and Select Resitive Lead Bias Mode + * @param Rbiasv: Resistive Bias Mode Value Selection + * @param Rbiasp: Enables Resistive Bias on Positive Input + * @param Rbiasn: Enables Resistive Bias on Negative Input + * @param Fmstr: Selects Master Clock Frequency + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Rbias_FMSTR_Init(uint8_t En_rbias, uint8_t Rbiasv, + uint8_t Rbiasp, uint8_t Rbiasn, uint8_t Fmstr); + + /** + * @brief This function uses sets up the calibration signal internally. If it is desired to use the internal signal, then + * @brief this function must be called and the registers set, prior to setting the CALP_SEL and CALN_SEL in the ECG_InitStart + * @brief and BIOZ_InitStart functions. + * @brief Uses Register: CNFG_CAL-0x12 + * @param En_Vcal: Calibration Source (VCALP and VCALN) Enable + * @param Vmode: Calibration Source Mode Selection + * @param Vmag: Calibration Source Magnitude Selection (VMAG) + * @param Fcal: Calibration Source Frequency Selection (FCAL) + * @param Thigh: Calibration Source Time High Selection + * @param Fifty: Calibration Source Duty Cycle Mode Selection + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_CAL_InitStart(uint8_t En_Vcal, uint8_t Vmode, uint8_t Vmag, + uint8_t Fcal, uint16_t Thigh, uint8_t Fifty); + + /** + * @brief This function disables the VCAL signal + * @returns 0-if no error. A non-zero value indicates an error. + */ + int max30001_CAL_Stop(void); + + /** + * @brief This function handles the assignment of the two interrupt pins (INTB & INT2B) with various + * @brief functions/behaviors of the MAX30001. Also, each pin can be configured for different drive capability. + * @brief Uses Registers: EN_INT-0x02 and EN_INT2-0x03. + * @param max30001_intrpt_Locatio_t <argument>: All the arguments with the aforementioned enumeration essentially + * can be configured to generate an interrupt on either INTB or INT2B or NONE. + * @param max30001_intrpt_type_t intb_Type: INTB Port Type (EN_INT Selections). + * @param max30001_intrpt_type _t int2b_Type: INT2B Port Type (EN_INT2 Selections) + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_INT_assignment(max30001_intrpt_Location_t en_enint_loc, max30001_intrpt_Location_t en_eovf_loc, max30001_intrpt_Location_t en_fstint_loc, + max30001_intrpt_Location_t en_dcloffint_loc, max30001_intrpt_Location_t en_bint_loc, max30001_intrpt_Location_t en_bovf_loc, + max30001_intrpt_Location_t en_bover_loc, max30001_intrpt_Location_t en_bundr_loc, max30001_intrpt_Location_t en_bcgmon_loc, + max30001_intrpt_Location_t en_pint_loc, max30001_intrpt_Location_t en_povf_loc, max30001_intrpt_Location_t en_pedge_loc, + max30001_intrpt_Location_t en_lonint_loc, max30001_intrpt_Location_t en_rrint_loc, max30001_intrpt_Location_t en_samp_loc, + max30001_intrpt_type_t intb_Type, max30001_intrpt_type_t int2b_Type); + + + + /** + * @brief For MAX30001/3 ONLY + * @brief This function sets up the MAX30001 for the ECG measurements. + * @brief Registers used: CNFG_EMUX, CNFG_GEN, MNGR_INT, CNFG_ECG. + * @param En_ecg: ECG Channel Enable <CNFG_GEN register bits> + * @param Openp: Open the ECGN Input Switch (most often used for testing and calibration studies) <CNFG_EMUX register bits> + * @param Openn: Open the ECGN Input Switch (most often used for testing and calibration studies) <CNFG_EMUX register bits> + * @param Calp_sel: ECGP Calibration Selection <CNFG_EMUX register bits> + * @param Caln_sel: ECGN Calibration Selection <CNFG_EMUX register bits> + * @param E_fit: ECG FIFO Interrupt Threshold (issues EINT based on number of unread FIFO records) <CNFG_GEN register bits> + * @param Clr_rrint: RTOR R Detect Interrupt (RRINT) Clear Behavior <CNFG_GEN register bits> + * @param Rate: ECG Data Rate + * @param Gain: ECG Channel Gain Setting + * @param Dhpf: ECG Channel Digital High Pass Filter Cutoff Frequency + * @param Dlpf: ECG Channel Digital Low Pass Filter Cutoff Frequency + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_ECG_InitStart(uint8_t En_ecg, uint8_t Openp, uint8_t Openn, + uint8_t Pol, uint8_t Calp_sel, uint8_t Caln_sel, + uint8_t E_fit, uint8_t Rate, uint8_t Gain, + uint8_t Dhpf, uint8_t Dlpf); + + /** + * @brief For MAX30001/3 ONLY + * @brief This function enables the Fast mode feature of the ECG. + * @brief Registers used: MNGR_INT-0x04, MNGR_DYN-0x05 + * @param Clr_Fast: FAST MODE Interrupt Clear Behavior <MNGR_INT Register> + * @param Fast: ECG Channel Fast Recovery Mode Selection (ECG High Pass Filter Bypass) <MNGR_DYN Register> + * @param Fast_Th: Automatic Fast Recovery Threshold + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_ECGFast_Init(uint8_t Clr_Fast, uint8_t Fast, uint8_t Fast_Th); + + /** + * @brief For MAX30001/3 ONLY + * @brief This function disables the ECG. + * @brief Uses Register CNFG_GEN-0x10. + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Stop_ECG(void); + + /** + * @brief For MAX30001 ONLY + * @brief This function sets up the MAX30001 for pace signal detection. + * @brief If both PACE and BIOZ are turned ON, then make sure Fcgen is set for 80K or 40K in the + * @brief max30001_BIOZ_InitStart() function. However, if Only PACE is on but BIOZ off, then Fcgen can be set + * @brief for 80K only, in the max30001_BIOZ_InitStart() function + * @brief Registers used: MNGR_INT-0x04, CNFG_GEN-0x37, CNFG_PACE-0x1A. + * @param En_pace : PACE Channel Enable <CNFG_GEN Register> + * @param Clr_pedge : PACE Edge Detect Interrupt (PEDGE) Clear Behavior <MNGR_INT Register> + * @param Pol: PACE Input Polarity Selection <CNFG_PACE Register> + * @param Gn_diff_off: PACE Differentiator Mode <CNFG_PACE Register> + * @param Gain: PACE Channel Gain Selection <CNFG_PACE Register> + * @param Aout_lbw: PACE Analog Output Buffer Bandwidth Mode <CNFG_PACE Register> + * @param Aout: PACE Single Ended Analog Output Buffer Signal Monitoring Selection <CNFG_PACE Register> + * @param Dacp (4bits): PACE Detector Positive Comparator Threshold <CNFG_PACE Register> + * @param Dacn(4bits): PACE Detector Negative Comparator Threshold <CNFG_PACE Register> + * @returns 0-if no error. A non-zero value indicates an error <CNFG_PACE Register> + * + */ + int max30001_PACE_InitStart(uint8_t En_pace, uint8_t Clr_pedge, uint8_t Pol, + uint8_t Gn_diff_off, uint8_t Gain, + uint8_t Aout_lbw, uint8_t Aout, uint8_t Dacp, + uint8_t Dacn); + + /** + *@brief For MAX30001 ONLY + *@param This function disables the PACE. Uses Register CNFG_GEN-0x10. + *@returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Stop_PACE(void); + + /** + * @brief For MAX30001/2 ONLY + * @brief This function sets up the MAX30001 for BIOZ measurement. + * @brief Registers used: MNGR_INT-0x04, CNFG_GEN-0X10, CNFG_BMUX-0x17,CNFG_BIOZ-0x18. + * @param En_bioz: BIOZ Channel Enable <CNFG_GEN Register> + * @param Openp: Open the BIP Input Switch <CNFG_BMUX Register> + * @param Openn: Open the BIN Input Switch <CNFG_BMUX Register> + * @param Calp_sel: BIP Calibration Selection <CNFG_BMUX Register> + * @param Caln_sel: BIN Calibration Selection <CNFG_BMUX Register> + * @param CG_mode: BIOZ Current Generator Mode Selection <CNFG_BMUX Register> + * @param B_fit: BIOZ FIFO Interrupt Threshold (issues BINT based on number of unread FIFO records) <MNGR_INT Register> + * @param Rate: BIOZ Data Rate <CNFG_BIOZ Register> + * @param Ahpf: BIOZ/PACE Channel Analog High Pass Filter Cutoff Frequency and Bypass <CNFG_BIOZ Register> + * @param Ext_rbias: External Resistor Bias Enable <CNFG_BIOZ Register> + * @param Gain: BIOZ Channel Gain Setting <CNFG_BIOZ Register> + * @param Dhpf: BIOZ Channel Digital High Pass Filter Cutoff Frequency <CNFG_BIOZ Register> + * @param Dlpf: BIOZ Channel Digital Low Pass Filter Cutoff Frequency <CNFG_BIOZ Register> + * @param Fcgen: BIOZ Current Generator Modulation Frequency <CNFG_BIOZ Register> + * @param Cgmon: BIOZ Current Generator Monitor <CNFG_BIOZ Register> + * @param Cgmag: BIOZ Current Generator Magnitude <CNFG_BIOZ Register> + * @param Phoff: BIOZ Current Generator Modulation Phase Offset <CNFG_BIOZ Register> + * @param Inapow_mode: BIOZ Channel Instrumentation Amplifier (INA) Power Mode <CNFG_BIOZ Register> + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_BIOZ_InitStart(uint8_t En_bioz, uint8_t Openp, uint8_t Openn, + uint8_t Calp_sel, uint8_t Caln_sel, + uint8_t CG_mode, + /* uint8_t En_bioz,*/ uint8_t B_fit, uint8_t Rate, + uint8_t Ahpf, uint8_t Ext_rbias, uint8_t Gain, + uint8_t Dhpf, uint8_t Dlpf, uint8_t Fcgen, + uint8_t Cgmon, uint8_t Cgmag, uint8_t Phoff, uint8_t Inapow_mode); + + /** + * @brief For MAX30001/2 ONLY + * @brief This function disables the BIOZ. Uses Register CNFG_GEN-0x10. + * @returns 0-if no error. A non-zero value indicates an error. + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Stop_BIOZ(void); + + /** + * @brief For MAX30001/2 ONLY + * @brief BIOZ modulated Resistance Built-in-Self-Test, Registers used: CNFG_BMUX-0x17 + * @param En_bist: Enable Modulated Resistance Built-in-Self-test <CNFG_BMUX Register> + * @param Rnom: BIOZ RMOD BIST Nominal Resistance Selection <CNFG_BMUX Register> + * @param Rmod: BIOZ RMOD BIST Modulated Resistance Selection <CNFG_BMUX Register> + * @param Fbist: BIOZ RMOD BIST Frequency Selection <CNFG_BMUX Register> + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_BIOZ_InitBist(uint8_t En_bist, uint8_t Rnom, uint8_t Rmod, + uint8_t Fbist); + + /** + * @brief For MAX30001/3/4 ONLY + * @brief Sets up the device for RtoR measurement + * @param EN_rtor: ECG RTOR Detection Enable <RTOR1 Register> + * @param Wndw: R to R Window Averaging (Window Width = RTOR_WNDW[3:0]*8mS) <RTOR1 Register> + * @param Gain: R to R Gain (where Gain = 2^RTOR_GAIN[3:0], plus an auto-scale option) <RTOR1 Register> + * @param Pavg: R to R Peak Averaging Weight Factor <RTOR1 Register> + * @param Ptsf: R to R Peak Threshold Scaling Factor <RTOR1 Register> + * @param Hoff: R to R minimum Hold Off <RTOR2 Register> + * @param Ravg: R to R Interval Averaging Weight Factor <RTOR2 Register> + * @param Rhsf: R to R Interval Hold Off Scaling Factor <RTOR2 Register> + * @param Clr_rrint: RTOR Detect Interrupt Clear behaviour <MNGR_INT Register> + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_RtoR_InitStart(uint8_t En_rtor, uint8_t Wndw, uint8_t Gain, + uint8_t Pavg, uint8_t Ptsf, uint8_t Hoff, + uint8_t Ravg, uint8_t Rhsf, uint8_t Clr_rrint); + + /** + * @brief For MAX30001/3/4 ONLY + * @brief This function disables the RtoR. Uses Register CNFG_RTOR1-0x1D + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Stop_RtoR(void); + + /** + * @brief This is a function that waits for the PLL to lock; once a lock is achieved it exits out. (For convenience only) + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_PLL_lock(void); + + /** + * @brief This function causes the MAX30001 to reset. Uses Register SW_RST-0x08 + * @return 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_sw_rst(void); + + /** + * @brief This function provides a SYNCH operation. Uses Register SYCNH-0x09. Please refer to the data sheet for + * @brief the details on how to use this. + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_synch(void); + + /** + * @brief This function performs a FIFO Reset. Uses Register FIFO_RST-0x0A. Please refer to the data sheet + * @brief for the details on how to use this. + * @returns 0-if no error. A non-zero value indicates an error. + */ + int max300001_fifo_rst(void); + + /** + * + * @brief This is a callback function which collects all the data from the ECG, BIOZ, PACE and RtoR. It also handles + * @brief Lead On/Off. This function is passed through the argument of max30001_COMMinit(). + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_int_handler(void); + + /** + * @brief This is function called from the max30001_int_handler() function and processes all the ECG, BIOZ, PACE + * @brief and the RtoR data and sticks them in appropriate arrays and variables each unsigned 32 bits. + * @param ECG data will be in the array (input): max30001_ECG_FIFO_buffer[] + * @param Pace data will be in the array (input): max30001_PACE[] + * @param RtoRdata will be in the variable (input): max30001_RtoR_data + * @param BIOZ data will be in the array (input): max30001_BIOZ_FIFO_buffer[] + * @param global max30001_ECG_FIFO_buffer[] + * @param global max30001_PACE[] + * @param global max30001_BIOZ_FIFO_buffer[] + * @param global max30001_RtoR_data + * @param global max30001_DCLeadOff + * @param global max30001_ACLeadOff + * @param global max30001_LeadON + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_FIFO_LeadONOff_Read(void); + + /** + * @brief This function allows writing to a register. + * @param addr: Address of the register to write to + * @param data: 24-bit data read from the register. + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_reg_write(MAX30001_REG_map_t addr, uint32_t data); + + /** + * @brief This function allows reading from a register + * @param addr: Address of the register to read from. + * @param *return_data: pointer to the value read from the register. + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_reg_read(MAX30001_REG_map_t addr, uint32_t *return_data); + + /** + * @brief This function enables the DC Lead Off detection. Either ECG or BIOZ can be detected, one at a time. + * @brief Registers Used: CNFG_GEN-0x10 + * @param En_dcloff: BIOZ Digital Lead Off Detection Enable + * @param Ipol: DC Lead Off Current Polarity (if current sources are enabled/connected) + * @param Imag: DC Lead off current Magnitude Selection + * @param Vth: DC Lead Off Voltage Threshold Selection + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Enable_DcLeadOFF_Init(int8_t En_dcloff, int8_t Ipol, int8_t Imag, + int8_t Vth); + + /** + * @brief This function disables the DC Lead OFF feature, whichever is active. + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_Disable_DcLeadOFF(void); + + /** + * @brief This function sets up the BIOZ for AC Lead Off test. + * @brief Registers Used: CNFG_GEN-0x10, MNGR_DYN-0x05 + * @param En_bloff: BIOZ Digital Lead Off Detection Enable <CNFG_GEN register> + * @param Bloff_hi_it: DC Lead Off Current Polarity (if current sources are enabled/connected) <MNGR_DYN register> + * @param Bloff_lo_it: DC Lead off current Magnitude Selection <MNGR_DYN register> + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_BIOZ_Enable_ACLeadOFF_Init(uint8_t En_bloff, uint8_t Bloff_hi_it, + uint8_t Bloff_lo_it); + + /** + * @brief This function Turns of the BIOZ AC Lead OFF feature + * @brief Registers Used: CNFG_GEN-0x10 + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_BIOZ_Disable_ACleadOFF(void); + + /** + * @brief This function enables the Current Gnerator Monitor + * @brief Registers Used: CNFG_BIOZ-0x18 + * @returns 0-if no error. A non-zero value indicates an error. + * + */ + int max30001_BIOZ_Enable_BCGMON(void); + + /** + * + * @brief This function enables the Lead ON detection. Either ECG or BIOZ can be detected, one at a time. + * @brief Also, the en_bioz, en_ecg, en_pace setting is saved so that when this feature is disabled through the + * @brief max30001_Disable_LeadON() function (or otherwise) the enable/disable state of those features can be retrieved. + * @param Channel: ECG or BIOZ detection + * @returns 0-if everything is good. A non-zero value indicates an error. + * + */ + int max30001_Enable_LeadON(int8_t Channel); + + /** + * @brief This function turns off the Lead ON feature, whichever one is active. Also, retrieves the en_bioz, + * @brief en_ecg, en_pace and sets it back to as it was. + * @param 0-if everything is good. A non-zero value indicates an error. + * + */ + int max30001_Disable_LeadON(void); + + /** + * + * @brief This function is toggled every 2 seconds to switch between ECG Lead ON and BIOZ Lead ON detect + * @brief Adjust LEADOFF_SERVICE_TIME to determine the duration between the toggles. + * @param CurrentTime - This gets fed the time by RTC_GetValue function + * + */ + void max30001_ServiceLeadON(uint32_t currentTime); + + /** + * + * @brief This function is toggled every 2 seconds to switch between ECG DC Lead Off and BIOZ DC Lead Off + * @brief Adjust LEADOFF_SERVICE_TIME to determine the duration between the toggles. + * @param CurrentTime - This gets fed the time by RTC_GetValue function + * + */ + void max30001_ServiceLeadoff(uint32_t currentTime); + + /** + * + * @brief This function sets current RtoR values and fmstr values in a pointer structure + * @param hspValMax30001 - Pointer to a structure where to store the values + * + */ + void max30001_ReadHeartrateData(max30001_t *_hspValMax30001); + + /** + * @brief type definition for data interrupt + */ + typedef void (*PtrFunction)(uint32_t id, uint32_t *buffer, uint32_t length); + + /** + * @brief Used to connect a callback for when interrupt data is available + */ + void onDataAvailable(PtrFunction _onDataAvailable); + + static MAX30001 *instance; + +private: + void dataAvailable(uint32_t id, uint32_t *buffer, uint32_t length); + /// interrupt handler for async spi events + static void spiHandler(int events); + /// wrapper method to transmit and recieve SPI data + int SPI_Transmit(const uint8_t *tx_buf, uint32_t tx_size, uint8_t *rx_buf, + uint32_t rx_size); + uint32_t readPace(int group, uint8_t* result); + + //jjj 14MAR17 + //pointer to DigitalOut for cs + DigitalOut * m_cs; + //jjj + /// pointer to mbed SPI object + SPI *m_spi; + /// is this object the owner of the spi object + bool spi_owner; + /// buffer to use for async transfers + uint8_t buffer[ASYNC_SPI_BUFFER_SIZE]; + /// function pointer to the async callback + event_callback_t functionpointer; + /// callback function when interrupt data is available + PtrFunction onDataAvailableCallback; + +}; // End of MAX30001 Class + +/** + * @brief Preventive measure used to dismiss interrupts that fire too early during + * @brief initialization on INTB line + * + */ +void MAX30001Mid_IntB_Handler(void); + +/** + * @brief Preventive measure used to dismiss interrupts that fire too early during + * @brief initialization on INT2B line + * + */ +void MAX30001Mid_Int2B_Handler(void); + +/** + * @brief Allows Interrupts to be accepted as valid. + * @param state: 1-Allow interrupts, Any-Don't allow interrupts. + * + */ +void MAX30001_AllowInterrupts(int state); + +#endif /* MAX30001_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/MAX30001/MAX30001_RPC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/MAX30001/MAX30001_RPC.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,471 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include <stdio.h> +#include "StringHelper.h" +#include "MAX30001.h" +#include "Streaming.h" +#include "StringInOut.h" +#include "MAX30001_helper.h" +#include "RpcFifo.h" +#include "RpcServer.h" +#include "Peripherals.h" +#include "DataLoggingService.h" + +int highDataRate = 0; + +uint32_t max30001_RegRead(MAX30001::MAX30001_REG_map_t addr) { + uint32_t data; + Peripherals::max30001()->max30001_reg_read(addr, &data); + return data; +} + +void max30001_RegWrite(MAX30001::MAX30001_REG_map_t addr, uint32_t data) { + Peripherals::max30001()->max30001_reg_write(addr, data); +} + +int MAX30001_WriteReg(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[2]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + max30001_RegWrite((MAX30001::MAX30001_REG_map_t)args[0], args[1]); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_ReadReg(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[1]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + value = max30001_RegRead((MAX30001::MAX30001_REG_map_t)args[0]); + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_Rbias_FMSTR_Init(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[5]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + value = Peripherals::max30001()->max30001_Rbias_FMSTR_Init(args[0], // En_rbias + args[1], // Rbiasv + args[2], // Rbiasp + args[3], // Rbiasn + args[4]); // Fmstr + + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_CAL_InitStart(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[6]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + // Peripherals::serial()->printf("MAX30001_CAL_InitStart 0 "); + value = Peripherals::max30001()->max30001_CAL_InitStart(args[0], // En_Vcal + args[1], // Vmag + args[2], // Fcal + args[3], // Thigh + args[4], // Fifty + args[5]); // Vmode + + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_ECG_InitStart(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[11]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + // Peripherals::serial()->printf("MAX30001_ECG_InitStart 0 "); + value = Peripherals::max30001()->max30001_ECG_InitStart(args[0], // En_ecg + args[1], // Openp + args[2], // Openn + args[3], // Pol + args[4], // Calp_sel + args[5], // Caln_sel + args[6], // E_fit + args[7], // Rate + args[8], // Gain + args[9], // Dhpf + args[10]); // Dlpf + // Peripherals::serial()->printf("MAX30001_ECG_InitStart 1 "); + MAX30001_Helper_SetStreamingFlag(eStreaming_ECG, 1); + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_ECGFast_Init(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[3]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + value = Peripherals::max30001()->max30001_ECGFast_Init(args[0], // Clr_Fast + args[1], // Fast + args[2]); // Fast_Th + + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_PACE_InitStart(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[9]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + value = + Peripherals::max30001()->max30001_PACE_InitStart(args[0], // En_pace + args[1], // Clr_pedge + args[2], // Pol + args[3], // Gn_diff_off + args[4], // Gain + args[5], // Aout_lbw + args[6], // Aout + args[7], // Dacp + args[8]); // Dacn + + MAX30001_Helper_SetStreamingFlag(eStreaming_PACE, 1); + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_BIOZ_InitStart(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[18]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + value = Peripherals::max30001()->max30001_BIOZ_InitStart(args[0], // En_bioz + args[1], // Openp + args[2], // Openn + args[3], // Calp_sel + args[4], // Caln_sel + args[5], // CG_mode + args[6], // B_fit + args[7], // Rate + args[8], // Ahpf + args[9], // Ext_rbias + args[10], // Gain + args[11], // Dhpf + args[12], // Dlpf + args[13], // Fcgen + args[14], // Cgmon + args[15], // Cgmag + args[16], // Phoff + args[17]); //INAPow_mode + + MAX30001_Helper_SetStreamingFlag(eStreaming_BIOZ, 1); + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_RtoR_InitStart(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[9]; + uint32_t reply[1]; + uint32_t value; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + value = Peripherals::max30001()->max30001_RtoR_InitStart(args[0], // En_rtor + args[1], // Wndw + args[2], // Gain + args[3], // Pavg + args[4], // Ptsf + args[5], // Hoff + args[6], // Ravg + args[7], // Rhsf + args[8]); // Clr_rrint + + MAX30001_Helper_SetStreamingFlag(eStreaming_RtoR, 1); + reply[0] = value; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_Stop_ECG(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::max30001()->max30001_Stop_ECG(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +int MAX30001_Stop_PACE(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::max30001()->max30001_Stop_PACE(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +int MAX30001_Stop_BIOZ(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::max30001()->max30001_Stop_BIOZ(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +int MAX30001_Stop_RtoR(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::max30001()->max30001_Stop_RtoR(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +int MAX30001_Stop_Cal(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // max30001_Stop_Cal(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +void max30001_ServiceStreaming() { + char ch; + uint32_t val; + USBSerial *usbSerial = Peripherals::usbSerial(); + + fifo_clear(GetStreamOutFifo()); + + SetStreaming(TRUE); + clearOutReadFifo(); + while (IsStreaming() == TRUE) { + + if (fifo_empty(GetStreamOutFifo()) == 0) { + fifo_get32(GetStreamOutFifo(), &val); + + usbSerial->printf("%02X ", val); + + } + if (usbSerial->available()) { + ch = usbSerial->_getc(); + + MAX30001_Helper_Stop(); + SetStreaming(FALSE); + fifo_clear(GetUSBIncomingFifo()); // clear USB serial incoming fifo + fifo_clear(GetStreamOutFifo()); + } + + } +} + +int MAX30001_Start(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + uint32_t all; + fifo_clear(GetUSBIncomingFifo()); + Peripherals::max30001()->max30001_synch(); + // max30001_ServiceStreaming(); + highDataRate = 0; + Peripherals::max30001()->max30001_reg_read(MAX30001::STATUS, &all); + LoggingService_StartLoggingUsb(); + + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_Stop(char argStrs[32][32], char replyStrs[32][32]) { + /* uint32_t args[1]; + uint32_t reply[1]; + uint32_t value; + //ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + max30001_StopTest(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs);*/ + return 0; +} + +int MAX30001_INT_assignment(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[17]; + uint32_t reply[1]; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + /* + printf("MAX30001_INT_assignment "); + printf("%d ",args[0]); + printf("%d ",args[1]); + printf("%d ",args[2]); + printf("%d ",args[3]); + printf("%d ",args[4]); + printf("%d ",args[5]); + printf("%d ",args[6]); + printf("%d ",args[7]); + printf("%d ",args[8]); + printf("%d ",args[9]); + printf("%d ",args[10]); + printf("%d ",args[11]); + printf("%d ",args[12]); + printf("%d ",args[13]); + printf("%d ",args[14]); + printf("%d ",args[15]); + printf("%d ",args[16]); + printf("\n"); + fflush(stdout); + */ + + Peripherals::max30001()->max30001_INT_assignment( + (MAX30001::max30001_intrpt_Location_t)args[0], + (MAX30001::max30001_intrpt_Location_t)args[1], + (MAX30001::max30001_intrpt_Location_t)args[2], + (MAX30001::max30001_intrpt_Location_t)args[3], + (MAX30001::max30001_intrpt_Location_t)args[4], + (MAX30001::max30001_intrpt_Location_t)args[5], + (MAX30001::max30001_intrpt_Location_t)args[6], + (MAX30001::max30001_intrpt_Location_t)args[7], + (MAX30001::max30001_intrpt_Location_t)args[8], + (MAX30001::max30001_intrpt_Location_t)args[9], + (MAX30001::max30001_intrpt_Location_t)args[10], + (MAX30001::max30001_intrpt_Location_t)args[11], + (MAX30001::max30001_intrpt_Location_t)args[12], + (MAX30001::max30001_intrpt_Location_t)args[13], + (MAX30001::max30001_intrpt_Location_t)args[14], + (MAX30001::max30001_intrpt_type_t)args[15], + (MAX30001::max30001_intrpt_type_t)args[16]); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int MAX30001_StartTest(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + + /*** Set FMSTR over here ****/ + + /*** Set and Start the VCAL input ***/ + /* NOTE VCAL must be set first if VCAL is to be used */ + Peripherals::max30001()->max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + + /**** ECG Initialization ****/ + Peripherals::max30001()->max30001_ECG_InitStart(0b1, 0b1, 0b1, 0b0, 0b10, 0b11, 31, 0b00, 0b00, 0b0, 0b01); + + /***** PACE Initialization ***/ + Peripherals::max30001()->max30001_PACE_InitStart(0b1, 0b0, 0b0, 0b1, 0b000, 0b0, 0b00, 0b0, 0b0); + + /**** BIOZ Initialization ****/ + Peripherals::max30001()->max30001_BIOZ_InitStart( + 0b1, 0b1, 0b1, 0b10, 0b11, 0b00, 7, 0b0, 0b111, 0b0, 0b10, 0b00, 0b00, 0b0001, 0b0, 0b111, 0b0000, 0b0000); + + /*** Set RtoR registers ***/ + Peripherals::max30001()->max30001_RtoR_InitStart( + 0b1, 0b0011, 0b1111, 0b00, 0b0011, 0b000001, 0b00, 0b000, 0b01); + + /*** Set Rbias & FMSTR over here ****/ + Peripherals::max30001()->max30001_Rbias_FMSTR_Init(0b01, 0b10, 0b1, 0b1, 0b00); + + /**** Interrupt Setting ****/ + + /*** Set ECG Lead ON/OFF ***/ + // max30001_ECG_LeadOnOff(); + + /*** Set BIOZ Lead ON/OFF ***/ + // max30001_BIOZ_LeadOnOff(); Does not work yet... + + /**** Do a Synch ****/ + Peripherals::max30001()->max30001_synch(); + + fifo_clear(GetUSBIncomingFifo()); + max30001_ServiceStreaming(); + + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +/* +static void StopAll() { + if (startedEcg == 1) { + max30001_Stop_ECG(); + } + if (startedCal == 1) { + } + if (startedBioz == 1) { + max30001_Stop_BIOZ(); + } + if (startedPace == 1) { + max30001_Stop_PACE(); + } + if (startedRtor == 1) { + max30001_Stop_RtoR(); + } + startedEcg = 0; + startedBioz = 0; + startedCal = 0; + startedPace = 0; + startedRtor = 0; +} +*/ +/* +// switch to ECG DC Lead ON +max30001_Enable_LeadON(0b01); +// switch to BIOZ DC Lead ON +max30001_Enable_LeadON(0b10); +*/ +int MAX30001_Enable_ECG_LeadON(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // switch to ECG DC Lead ON + Peripherals::max30001()->max30001_Enable_LeadON(0b01); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +int MAX30001_Enable_BIOZ_LeadON(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // switch to BIOZ DC Lead ON + Peripherals::max30001()->max30001_Enable_LeadON(0b10); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +// uint32_t max30001_LeadOn; // This holds the LeadOn data, BIT1 = BIOZ Lead ON, BIT0 = ECG Lead ON +int MAX30001_Read_LeadON(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // return the max30001_LeadOn var from the MAX30001 driver + reply[0] = Peripherals::max30001()->max30001_LeadOn; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/MAX30001/MAX30001_RPC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/MAX30001/MAX30001_RPC.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef __MAX300001_RPC_H +#define __MAX300001_RPC_H + +int MAX30001_WriteReg(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_ReadReg(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Start(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Stop(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Rbias_FMSTR_Init(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_CAL_InitStart(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_ECG_InitStart(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_ECGFast_Init(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_PACE_InitStart(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_BIOZ_InitStart(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_RtoR_InitStart(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Stop_ECG(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Stop_PACE(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Stop_BIOZ(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Stop_RtoR(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Stop_Cal(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Enable_ECG_LeadON(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Enable_BIOZ_LeadON(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_Read_LeadON(char argStrs[32][32], char replyStrs[32][32]); + +int MAX30001_StartTest(char argStrs[32][32], char replyStrs[32][32]); +int MAX30001_INT_assignment(char argStrs[32][32], char replyStrs[32][32]); + +#endif /* __MAX300001_RPC_H */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/MAX30001/MAX30001_helper.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/MAX30001/MAX30001_helper.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,126 @@ + +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "MAX30001_helper.h" +#include "MAX30001.h" +#include "StringInOut.h" +#include "Peripherals.h" + +static uint8_t flags[4]; + +int MAX30001_Helper_IsStreaming(eFlags flag) { + return flags[(uint32_t)flag]; +} + +void MAX30001_Helper_SetStreamingFlag(eFlags flag, uint8_t state) { + flags[(uint32_t)flag] = state; +} + +void MAX30001_Helper_Stop(void) { + if (flags[(uint32_t)eStreaming_ECG] == 1) { + Peripherals::max30001()->max30001_Stop_ECG(); + } + if (flags[(uint32_t)eStreaming_PACE] == 1) { + Peripherals::max30001()->max30001_Stop_PACE(); + } + if (flags[(uint32_t)eStreaming_BIOZ] == 1) { + Peripherals::max30001()->max30001_Stop_BIOZ(); + } + if (flags[(uint32_t)eStreaming_RtoR] == 1) { + Peripherals::max30001()->max30001_Stop_RtoR(); + } + MAX30001_Helper_ClearStreamingFlags(); +} + +int MAX30001_AnyStreamingSet(void) { + uint32_t i; + for (i = 0; i < 4; i++) { + if (flags[i] == 1) return 1; + } + return 0; +} + +void MAX30001_Helper_StartSync(void) { + if (MAX30001_AnyStreamingSet() == 1) { + Peripherals::max30001()->max30001_synch(); + } +} + +void MAX30001_Helper_ClearStreamingFlags(void) { + uint32_t i; + for (i = 0; i < 4; i++) { + flags[i] = 0; + } +} + +void MAX30001_Helper_Debug_ShowStreamFlags(void) { + putStr("\r\n"); + if (flags[(uint32_t)eStreaming_ECG] == 1) { + putStr("eStreaming_ECG, "); + } + if (flags[(uint32_t)eStreaming_PACE] == 1) { + putStr("eStreaming_PACE, "); + } + if (flags[(uint32_t)eStreaming_BIOZ] == 1) { + putStr("eStreaming_BIOZ, "); + } + if (flags[(uint32_t)eStreaming_RtoR] == 1) { + putStr("eStreaming_RtoR, "); + } + putStr("\r\n"); +} + +void MAX30001_Helper_SetupInterrupts() { + // We removed this baed on the assumption that user provides a INT_assignment command + /* + Peripherals::max30001()->max30001_INT_assignment(MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_enint_loc, en_eovf_loc, en_fstint_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_dcloffint_loc, en_bint_loc, en_bovf_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_bover_loc, en_bundr_loc, en_bcgmon_loc, + MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_pint_loc, en_povf_loc, en_pedge_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, // en_lonint_loc, en_rrint_loc, en_samp_loc, + MAX30001::MAX30001_INT_ODNR, MAX30001::MAX30001_INT_ODNR); // intb_Type, int2b_Type) + */ +} + + + +static uint8_t serialNumber[6]; +uint8_t *MAX30001_Helper_getVersion(void) { + // read the id + Peripherals::max30001()->max30001_reg_read(MAX30001::INFO, (uint32_t *)serialNumber); + // read id twice because it needs to be read twice + Peripherals::max30001()->max30001_reg_read(MAX30001::INFO, (uint32_t *)serialNumber); + return serialNumber; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/MAX30001/MAX30001_helper.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/MAX30001/MAX30001_helper.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef MAX30001_HELPER_H_ +#define MAX30001_HELPER_H_ + +#include "mbed.h" + +typedef enum eFlags { + eStreaming_ECG, + eStreaming_PACE, + eStreaming_BIOZ, + eStreaming_RtoR +} eFlags; + +int MAX30001_Helper_IsStreaming(eFlags flag); +void MAX30001_Helper_SetStreamingFlag(eFlags flag, uint8_t state); +void MAX30001_Helper_Stop(void); +void MAX30001_Helper_ClearStreamingFlags(void); +int MAX30001_AnyStreamingSet(void); +void MAX30001_Helper_Debug_ShowStreamFlags(void); +void MAX30001_Helper_StartSync(void); +void MAX30001_Helper_SetupInterrupts(void); +uint8_t *MAX30001_Helper_getVersion(void); + +#endif /* MAX30001_HELPER_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/PushButton/PushButton.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/PushButton/PushButton.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,127 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "PushButton.h" +#include "Peripherals.h" + +extern MAX30001 max30001; +extern uint32_t global_key_judge; +extern int current_log_number; + +PushButton::PushButton(PinName pin) : interruptButton(pin) { + this->pin = pin; + + interruptButton.mode(PullUp); + debouncingState = false; + // Delay for initial pullup to take effect + wait(0.01f); + // Attach the address of the interrupt handler routine for pushbutton + interruptButton.fall(callback(this, &PushButton::pb_hit_interrupt_fall)); +} + +bool PushButton::GetButtonFallState(void) { + return buttonFallState; +} + +void PushButton::SetButtonFallState(bool state) { + buttonFallState = state; +} + +void PushButton::clearButtonFallState(void) { + buttonFallState = false; +} + +int PushButton::Read(void) { + return interruptButton.read(); +} + +void PushButton::pb_hit_interrupt_fall(void) { + /* switch debouncing */ + if (!debouncingState) + { + timeout.attach(callback(this, &PushButton::pb_debounce_timeout), debounceTime); + } + + if ( global_key_judge&0x01 == 1 ) + { + max30001.max30001_BIOZ_InitStart(0b1, 0b0, 0b0, 0b00, + 0b00, 0b00, 7, 0b0, + 0b010, 0b0, 0b00, 0b00, 0b00, + 2, 0b0, 0b011, 0b0000, 0b0000); + max30001.max30001_synch(); + + /* + printf("global_key_judge = %d\n", global_key_judge); + FILE *fp_index = fopen("/sd/index.txt", "r+"); + int index_number = 0; + int err = fscanf(fp_index, "%d", &index_number); + printf("Scan err = %d\n", err); + index_number += 1; + err = fprintf(fp_index, "%d\n", index_number); + printf("err = %d\n", err); + printf("index_number = %d\n", index_number); + printf("Start Sampling \n"); + printf("index_number = %d \n", index_number); + fclose(fp_index); + */ + printf("Save to Bioz_%d.txt", current_log_number); + current_log_number += 1; + wait_ms(200); + } + else + { + max30001.max30001_Stop_ECG(); + max30001.max30001_Stop_PACE(); + max30001.max30001_Stop_BIOZ(); + max30001.max30001_Stop_RtoR(); + printf("Stop Sampling \n"); + wait_ms(200); + } + + global_key_judge += 1; + + + //max30001.max30001_ECG_InitStart(0b1, 0b1, 0b1, 0b0, 0b10, 0b11, 0x1F, 0b00, + // 0b00, 0b0, 0b01); +} + +void PushButton::pb_debounce_timeout(void) { + if (!interruptButton.read()) + { + if (Peripherals::pushButton() != NULL){ + Peripherals::pushButton()->SetButtonFallState(true); + } + } + debouncingState = false; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/PushButton/PushButton.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/PushButton/PushButton.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _PUSHBUTTON_H +#define _PUSHBUTTON_H + +#include "mbed.h" + +class PushButton { +public: + /** + * @brief Constructor with a PinName + */ + PushButton(PinName pin); + /** + * @brief Get the pushed state of the button + */ + bool GetButtonFallState(void); + /** + * @brief Clear the pushed state of the button + */ + void clearButtonFallState(void); + /** + * @brief Read the button input + * @return Represented as 0 or 1 (int) + */ + int Read(void); + /** + * @brief Set the state + * @param state Set the button state to this value + */ + void SetButtonFallState(bool state); + +private: + InterruptIn interruptButton; + bool buttonFallState; + PinName pin; + Timeout timeout; + bool debouncingState; + const static float debounceTime = 0.02; + + void pb_hit_interrupt_fall(void); + void pb_debounce_timeout(void); +}; + +#endif /* _PUSHBUTTON_H */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/S25FS256/S25FS512.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/S25FS256/S25FS512.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,473 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +// +// Flash Non-Volatile Memory +// U27 S25FS512 +// Nimitz SPIM1 +// + +#include "mbed.h" +#include "S25FS512.h" +#include "QuadSpiInterface.h" + +#define IOMUX_IO_ENABLE 1 + +#define S25FS512_SPI_PORT 1 +#define S25FS512_CS_PIN 0 +#define S25FS512_CS_POLARITY 0 +#define S25FS512_CS_ACTIVITY_DELAY 0 +#define S25FS512_CS_INACTIVITY_DELAY 0 +#define S25FS512_CLK_HI 4 +#define S25FS512_CLK_LOW 4 +#define S25FS512_ALT_CLK 0 +#define S25FS512_CLK_POLARITY 0 +#define S25FS512_CLK_PHASE 0 +#define S25FS512_WRITE 1 +#define S25FS512_READ 0 + +#define INT_PORT_B 3 +#define INT_PIN_B 6 + +uint8_t flashBuffer[257 + 10]; + +//****************************************************************************** +S25FS512::S25FS512(QuadSpiInterface *_quadSpiInterface) { + this->quadSpiInterface = _quadSpiInterface; +} + +//****************************************************************************** +S25FS512::~S25FS512(void) { +} + +//****************************************************************************** +int S25FS512::init(void) { + setQuadMode(); + return 0; +} + +//****************************************************************************** +int S25FS512::wren4Wire(void) { + uint8_t cmdArray[8]; + // Send WREN + cmdArray[0] = 0x06; + wait_1mS(); + return reg_write_read_multiple_4Wire(cmdArray, 1, flashBuffer, 1); +} + +//****************************************************************************** +uint8_t S25FS512::wren(void) { + uint8_t cmdArray[8]; + // Send WREN + cmdArray[0] = 0x06; + wait_1mS(); + return reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); +} + +//****************************************************************************** +int8_t S25FS512::reg_write_read_multiple_quad_last(uint8_t *bufferOut, + uint8_t numberOut, + uint8_t *bufferIn, + uint8_t numberIn, + uint8_t last) { + int32_t success = 0; + + success = quadSpiInterface->SPI_Transmit( + bufferOut, numberOut, + bufferIn, numberIn, (int)last); + + if (success != 0) return -1; + return 0; +} + +//****************************************************************************** +int8_t S25FS512::reg_write_read_multiple_4Wire(uint8_t *bufferOut, + uint8_t numberOut, + uint8_t *bufferIn, + uint8_t numberIn) { + int32_t success = 0; + success = quadSpiInterface->SPI_Transmit4Wire(bufferOut, numberOut, bufferIn, + numberIn, (int)1); + + if (success != 0) return -1; + return 0; +} + +//****************************************************************************** +int8_t S25FS512::reg_write_read_multiple_quad(uint8_t *bufferOut, + uint8_t numberOut, + uint8_t *bufferIn, + uint8_t numberIn) { + int8_t ret; + ret = reg_write_read_multiple_quad_last(bufferOut, numberOut, bufferIn, + numberIn, 1); + return ret; +} + +//****************************************************************************** +void S25FS512::readID(uint8_t *id) { + uint8_t cmd = 0x9F; + reg_write_read_multiple_quad(&cmd, 1, id, 4); +} + +//****************************************************************************** +int8_t S25FS512::writeAnyRegister(uint32_t address, uint8_t data) { + uint8_t cmdArray[5]; + cmdArray[0] = 0x71; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + cmdArray[4] = data; + return reg_write_read_multiple_quad(cmdArray, 5, flashBuffer, 0); +} + +int8_t S25FS512::writeAnyRegister4Wire(uint32_t address, uint8_t data) { + uint8_t cmdArray[5]; + cmdArray[0] = 0x71; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + cmdArray[4] = data; + return reg_write_read_multiple_4Wire(cmdArray, 5, flashBuffer, 5); +} + +//****************************************************************************** +int8_t S25FS512::writeRegisters(void) { + uint8_t cmdArray[3]; + wait_1mS(); + cmdArray[0] = 0x01; + cmdArray[1] = 0x00; + cmdArray[2] = 0x02; // set Quad to 1 + reg_write_read_multiple_quad(cmdArray, 3, flashBuffer, 0); + return 0; +} + +//****************************************************************************** +int8_t S25FS512::readAnyRegister(uint32_t address, uint8_t *data, + uint32_t length) { + uint8_t cmdArray[4]; + cmdArray[0] = 0x65; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + return reg_write_read_multiple_quad(cmdArray, 4, data, length); +} + +//****************************************************************************** +int8_t S25FS512::bulkErase(void) { + uint8_t cmdArray[1]; + cmdArray[0] = 0x60; + return reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); +} + +//****************************************************************************** +int8_t S25FS512::pageProgram(uint32_t address, uint8_t *buffer) { + uint32_t i; + uint8_t cmdArray[5 + 256]; + uint8_t *ptr; + + // for (i = 0; i < 256; i++) { + // dataArray[i] = i; + //} + cmdArray[0] = 0x02; // 0x71; + // cmdArray[1] = (address >> 24) & 0xFF; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + for (i = 0; i < 256; i++) { + cmdArray[4 + i] = buffer[i]; + } + // reg_write_read_multiple_quad(cmdArray,256 + 4,flashBuffer,256 + 4); + + ptr = cmdArray; + reg_write_read_multiple_quad_last(ptr, 4 + 64, flashBuffer, 0, 0); + wait_1mS(); + ptr += (4 + 64); + reg_write_read_multiple_quad_last(ptr, 64, flashBuffer, 0, 0); + wait_1mS(); + ptr += 64; + reg_write_read_multiple_quad_last(ptr, 64, flashBuffer, 0, 0); + wait_1mS(); + ptr += 64; + reg_write_read_multiple_quad_last(ptr, 64, flashBuffer, 0, 1); + wait_1mS(); + return 0; +} + +//****************************************************************************** +int8_t S25FS512::quadIoRead_Pages(uint32_t address, uint8_t *buffer, + uint32_t numberOfPages) { + uint8_t cmdArray[5]; + uint8_t *ptr; + uint8_t last; + uint32_t i; + + cmdArray[0] = 0xEB; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + ptr = buffer; + last = 0; + // only send the command + reg_write_read_multiple_quad_last(cmdArray, 4, ptr, 0, 0); + wait_1mS(); + reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 5, 0); + wait_1mS(); + for (i = 0; i < numberOfPages; i++) { + reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, 0); + wait_1mS(); + ptr += 64; + reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, 0); + wait_1mS(); + ptr += 64; + reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, 0); + wait_1mS(); + ptr += 64; + // check if this is the last page + if ((i + 1) == numberOfPages) { + last = 1; + } + reg_write_read_multiple_quad_last(cmdArray, 0, ptr, 64, last); + wait_1mS(); + ptr += 64; + } + return 0; +} + +//****************************************************************************** +int8_t S25FS512::checkBusy(void) { + uint8_t cmdArray[5]; + cmdArray[0] = 0x05; + reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 2); + return flashBuffer[1] & 0x1; +} + +//****************************************************************************** +void S25FS512::waitTillNotBusy(void) { + while (checkBusy() == 1) { + } +} + +//****************************************************************************** +int8_t S25FS512::sectorErase(uint32_t address) { + uint8_t cmdArray[5]; + cmdArray[0] = 0xD8; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + return reg_write_read_multiple_quad(cmdArray, 4, flashBuffer, 0); +} + +//****************************************************************************** +int8_t S25FS512::parameterSectorErase(uint32_t address) { + uint8_t cmdArray[5]; + cmdArray[0] = 0x20; + cmdArray[1] = (address >> 16) & 0xFF; + cmdArray[2] = (address >> 8) & 0xFF; + cmdArray[3] = (address >> 0) & 0xFF; + reg_write_read_multiple_quad(cmdArray, 4, flashBuffer, 0); + return 0; +} + +#define ONE_MS (32768 / 500) +#define ONEHUNDRED_US (32768 / 1000) +#define TEM_MS (32768 / 50) + +//****************************************************************************** +void S25FS512::wait_1mS(void) { + wait_ms(1); +} + +//****************************************************************************** +void S25FS512::wait_100uS(void) { +wait_us(100); +} + +//****************************************************************************** +void S25FS512::wait_10mS(void) { +wait_ms(10); +} + +//****************************************************************************** +int8_t S25FS512::readIdentification(uint8_t *dataArray, uint8_t length) { + // 4QIOR = 0x9F + uint8_t cmdArray[1]; + cmdArray[0] = 0x9F; // read ID command + return reg_write_read_multiple_quad(cmdArray, 1, dataArray, length); +} + +//****************************************************************************** +uint8_t S25FS512::reset(void) { + uint8_t cmdArray[8]; + wait_1mS(); + cmdArray[0] = 0x66; + reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); + wait_1mS(); + cmdArray[0] = 0x99; + reg_write_read_multiple_quad(cmdArray, 1, flashBuffer, 0); + return 0; +} + +//****************************************************************************** +uint8_t S25FS512::enableHWReset(void) { + uint8_t data[8]; + wait_1mS(); + // CR2V Configuration Register-2 Volatile + // bit 5 + readAnyRegister(0x00800003, data, 8); + writeAnyRegister(0x00800003, 0x64); + return 0; +} + +//****************************************************************************** +uint8_t S25FS512::detect(void) { + uint8_t array[8]; + uint8_t array2[8]; + + // Send WREN + wren(); + // Send WREN + wren(); + // delay + wait_1mS(); + // Send WREN + wren(); + // delay + wait_1mS(); + + // Send write any register cmd + writeAnyRegister(0x0003, 0x48); + // delay + wait_1mS(); + array[0] = 0x9F; // read ID command + reg_write_read_multiple_quad(array, 1, array2, 7); + return 0; +} + +//****************************************************************************** +int S25FS512::setQuadMode(void) { + wait_1mS(); + wren4Wire(); + wait_1mS(); + writeAnyRegister4Wire(0x800002, 0x02); // set Quad = 1 + wait_1mS(); + wren4Wire(); + wait_1mS(); + writeAnyRegister4Wire(0x800003, 0x48); // set 8 latency, set QPI 4-4-4 +} + +//****************************************************************************** +uint32_t S25FS512::isPageEmpty(uint8_t *ptr) { + int i; + for (i = 0; i < 256; i++) { + if (ptr[i] != 0xFF) + return 0; + } + return 1; +} + +//****************************************************************************** +int8_t S25FS512::parameterSectorErase_Helper(uint32_t address) { + waitTillNotBusy(); + wait_100uS(); + wren(); + wait_100uS(); + parameterSectorErase(address); + wait_100uS(); + waitTillNotBusy(); + wait_100uS(); + return 0; +} + +//****************************************************************************** +int8_t S25FS512::sectorErase_Helper(uint32_t address) { + waitTillNotBusy(); + wait_100uS(); + wren(); + wait_100uS(); + if (address < 0x8000) { + parameterSectorErase(address); + } else { + sectorErase(address); + } + wait_100uS(); + waitTillNotBusy(); + wait_100uS(); + return 0; +} + +//****************************************************************************** +int8_t S25FS512::bulkErase_Helper(void) { + waitTillNotBusy(); + wait_100uS(); + wren(); + wait_100uS(); + bulkErase(); + wait_100uS(); + waitTillNotBusy(); + wait_100uS(); + return 0; +} + +//****************************************************************************** +// write a page worth of data (256 bytes) from buffer, offset defined where in +// the buffer to begin write +int8_t S25FS512::writePage_Helper(uint32_t pageNumber, uint8_t *buffer, + uint32_t offset) { + uint8_t *ptr; + waitTillNotBusy(); + wait_1mS(); + wren(); + ptr = &buffer[offset]; + wait_1mS(); + pageProgram(pageNumber << 8, ptr); + wait_1mS(); + return 0; +} + +//****************************************************************************** +// read pages from flash into buffer, offset defined where in the buffer use +int8_t S25FS512::readPages_Helper(uint32_t startPageNumber, + uint32_t endPageNumber, uint8_t *buffer, + uint32_t offset) { + uint8_t *ptr; + uint32_t page; + ptr = &buffer[offset]; + for (page = startPageNumber; page <= endPageNumber; page++) { + wait_100uS(); + quadIoRead_Pages((uint32_t)(page << 8), (uint8_t *)ptr, 1); + ptr += 0x100; + } + return 0; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/S25FS256/S25FS512.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/S25FS256/S25FS512.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,128 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef S25FS512_H_ +#define S25FS512_H_ + +#include "mbed.h" +#include "QuadSpiInterface.h" + +class S25FS512 { +public: + S25FS512(QuadSpiInterface *_quadSpiInterface); + ~S25FS512(void); + + QuadSpiInterface *quadSpiInterface; + + /** Initialize the driver + */ + int init(void); + + /** Detect the presence of the flash device + */ + uint8_t detect(void); + + /** Read the identification of the flash + */ + int8_t readIdentification(uint8_t *dataArray, uint8_t length); + + /** Bulk erase the flash device + */ + int8_t bulkErase_Helper(void); + + /** Erase Parameter Sectors + */ + int8_t parameterSectorErase_Helper(uint32_t address); + + /** Write a Page + */ + int8_t writePage_Helper(uint32_t pageNumber, uint8_t *buffer, + uint32_t offset); + + /** Read a Page + * @param + */ + int8_t readPages_Helper(uint32_t startPageNumber, uint32_t endPageNumber, + uint8_t *buffer, uint32_t offset); + + /** Erase a Sector + @param address Address of sector to erase + */ + + int8_t sectorErase_Helper(uint32_t address); + /** Scans through byte pointer for a page worth of data to see if the page is all FFs + @param ptr Byte pointer to buffer to scan + @return Returns a 1 if the page is empty, 0 if it is not all FFs + */ + uint32_t isPageEmpty(uint8_t *ptr); + + /** Issue a software reset to the flash device + */ + + uint8_t reset(void); + /** Enable a hardware reset + */ + + uint8_t enableHWReset(void); + /** Read the id byte of this device + */ + + void readID(uint8_t *id); + +private: + int8_t reg_write_read_multiple_quad_last(uint8_t *dataIn, uint8_t numberIn, uint8_t *dataOut, uint8_t numberOut, uint8_t last); + int8_t reg_write_read_multiple_quad(uint8_t *dataIn, uint8_t numberIn, uint8_t *dataOut, uint8_t numberOut); + int8_t reg_write_read_multiple_4Wire(uint8_t *bufferOut, uint8_t numberOut, uint8_t *bufferIn, uint8_t numberIn); + uint8_t spiWriteRead (uint8_t writeNumber,uint8_t *writeData, uint8_t readNumber, uint8_t *readData); + uint8_t spiWriteRead4Wire(uint8_t writeNumber,uint8_t *writeData, uint8_t readNumber, uint8_t *readData); + + int8_t writeAnyRegister(uint32_t address, uint8_t data); + int8_t writeAnyRegister4Wire(uint32_t address, uint8_t data); + int8_t writeRegisters(void); + uint8_t wren(void); + int setQuadMode(void); + int wren4Wire(void); + // int8_t setQuadMode(); + int8_t readAnyRegister(uint32_t address, uint8_t *data, uint32_t length); + int8_t bulkErase(void); + int8_t pageProgram(uint32_t address, uint8_t *buffer); + int8_t quadIoRead_Pages(uint32_t address, uint8_t *buffer, uint32_t numberOfPages); + int8_t checkBusy(void); + void waitTillNotBusy(void); + int8_t sectorErase(uint32_t address); + int8_t parameterSectorErase(uint32_t address); + void wait_1mS(void); + void wait_100uS(void); + void wait_10mS(void); +}; +#endif /* S25FS512_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Devices/S25FS256/S25FS512_RPC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/S25FS256/S25FS512_RPC.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,191 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "S25FS512_RPC.h" +#include "S25FS512.h" +#include "StringInOut.h" +#include "StringHelper.h" +#include "Peripherals.h" +#include "SDBlockDevice.h" + +int S25FS512_Reset(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::s25FS512()->reset(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int S25FS512_EnableHWReset(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Peripherals::s25FS512()->enableHWReset(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int S25FS512_SpiWriteRead(char argStrs[32][32], char replyStrs[32][32]) { + uint8_t args[16]; + uint8_t reply[16]; + uint8_t writeNumber; + uint8_t readNumber; + // get the number of bytes to write + ProcessArgs(argStrs, args, 1); + writeNumber = args[0]; + ProcessArgs(argStrs, args, writeNumber + 2); + readNumber = args[writeNumber + 1]; + FormatReply(reply, readNumber, replyStrs); + return 0; +} + +int S25FS512_SpiWriteRead4Wire(char argStrs[32][32], char replyStrs[32][32]) { + uint8_t args[16]; + uint8_t reply[16]; + uint8_t writeNumber; + uint8_t readNumber; + // get the number of bytes to write + ProcessArgs(argStrs, args, 1); + writeNumber = args[0]; + ProcessArgs(argStrs, args, writeNumber + 2); + readNumber = args[writeNumber + 1]; + FormatReply(reply, readNumber, replyStrs); + return 0; +} + +int S25FS512_ReadPage(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[2]; + uint32_t reply[1]; + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//int S25FS512_ReadPagesBinary(char argStrs[32][32], char replyStrs[32][32]) { +// uint32_t args[2]; +// uint32_t reply[1]; +// uint8_t pageData[256]; +// +// uint32_t startPage; +// uint32_t endPage; +// uint32_t page; +// +// ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); +// startPage = args[0]; +// endPage = args[1]; +// for (page = startPage; page <= endPage; page++) { +// Peripherals::s25FS512()->readPages_Helper(page, page, pageData, 0); +// putBytes256Block(pageData, 1); +// } +// reply[0] = 0x80; +// FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); +// return 0; +//} +extern char dataFileName[32]; +int S25FS512_ReadPagesBinary(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t args[2]; + uint32_t reply[1]; + uint8_t pageData[256]; + + uint32_t startPage; + uint32_t endPage; + uint32_t page; + + ProcessArgs32(argStrs, args, sizeof(args) / sizeof(uint32_t)); + startPage = args[0]; + endPage = args[1]; + FILE *fp = NULL; + fp = fopen(dataFileName, "rb"); + for (page = startPage-0x12; page <= endPage-0x12; page++) { + memset(pageData, 0xffffffff, sizeof(pageData)); + printf("reading from page %d\r\n", page); + if (fp != NULL) { + fseek(fp,page*256,SEEK_SET); + uint8_t count = 0; + while(!feof(fp)) + { + printf("."); + pageData[count++] = (unsigned char) fgetc(fp); + if (count == 0) break; + }; + } + printf("\r\nEND\r\n"); + putBytes256Block(pageData, 1); + } + if (fp != NULL) fclose(fp); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +int S25FS512_ReadId(char argStrs[32][32], char replyStrs[32][32]) { + char str[32]; + uint8_t data[128]; + Peripherals::s25FS512()->readIdentification(data, sizeof(data)); + Peripherals::s25FS512()->readIdentification(data, sizeof(data)); + sprintf(str, "%02X%02X%02X%02X", data[0], data[1], data[2], data[3]); + strcpy(replyStrs[0], str); + return 0; +} +int SDCard_IsReady(char argStrs[32][32], char replyStrs[32][32]) { + + DigitalIn *detect = Peripherals::SDDetect(); + + bool isReady = false; + + if(detect->read()) + { + isReady = false; + strcpy(replyStrs[0], "not_ok"); + return 0; + } + + Peripherals::sdFS()->init(); + FILE *fp = fopen("/sd/test", "r"); + if ( fp != NULL) + isReady = true; + else + { + FILE *fp = fopen("/sd/test", "w"); + if ( fp != NULL) + isReady = true; + } + + if (fp != NULL) fclose(fp); + + if (isReady) + strcpy(replyStrs[0], "ok"); + else + strcpy(replyStrs[0], "not_ok"); + return 0; +}
diff -r 000000000000 -r a15c76864d7d HSP/Devices/S25FS256/S25FS512_RPC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Devices/S25FS256/S25FS512_RPC.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _S25FS512_RPC_H_ +#define _S25FS512_RPC_H_ + +#include "mbed.h" + +int S25FS512_ReadPagesBinary(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_ReadPage(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_ReadPagesBinary(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_ReadId(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_Reset(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_EnableHWReset(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_SpiWriteRead(char argStrs[32][32], char replyStrs[32][32]); +int S25FS512_SpiWriteRead4Wire(char argStrs[32][32], char replyStrs[32][32]); +int SDCard_IsReady(char argStrs[32][32], char replyStrs[32][32]); + +#endif /* _S25FS512_RPC_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Interfaces/QuadSpiInterface.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Interfaces/QuadSpiInterface.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "QuadSpiInterface.h" + +/** +* @brief Constructor that accepts pin names for the QUAD SPI interface +* @param mosi master out slave in pin name +* @param miso master in slave out pin name +* @param sclk serial clock pin name +* @param cs chip select pin name +*/ +QuadSpiInterface::QuadSpiInterface(PinName mosi, PinName miso, PinName sclk, + PinName cs) + : spi(mosi, miso, sclk), csPin(cs) { + + } + +/** +* @brief Transmit and recieve QUAD SPI data +* @param tx_buf pointer to transmit byte buffer +* @param tx_size number of bytes to transmit +* @param rx_buf pointer to the recieve buffer +* @param rx_size number of bytes to recieve +* @param last flag to indicate if this is the last QUAD SPI transaction for the +* current chip select cycle +*/ +int QuadSpiInterface::SPI_Transmit(const uint8_t *tx_buf, uint32_t tx_size, + uint8_t *rx_buf, uint32_t rx_size, + int last) { + uint32_t i; + int result = 0; + int index = 0; + // lower chip select + csPin = 0; + // write bytes out QUAD SPI + spi.setQuadMode(); + for (i = 0; i < tx_size; i++) { + rx_buf[index] = spi.write((int)tx_buf[i]); + index++; + } + // read in bytes from QUAD SPI + for (i = 0; i < rx_size; i++) { + rx_buf[index] = (uint8_t)spi.read(); + index++; + } + // raise chip select if this is the last transaction + if (last) csPin = 1; + return result; +} + +/** +* @brief Transmit and recieve QUAD SPI data +* @param tx_buf pointer to transmit byte buffer +* @param tx_size number of bytes to transmit +* @param rx_buf pointer to the recieve buffer +* @param rx_size number of bytes to recieve +* @param last flag to indicate if this is the last QUAD SPI transaction for the +* current chip select cycle +*/ +int QuadSpiInterface::SPI_Transmit4Wire(const uint8_t *tx_buf, uint32_t tx_size, + uint8_t *rx_buf, uint32_t rx_size, + int last) { + uint32_t i; + int result = 0; + int index = 0; + // lower chip select + csPin = 0; + // write bytes out Single SPI + spi.setSingleMode(); + for (i = 0; i < tx_size; i++) { + rx_buf[index] = spi.write((int)tx_buf[i]); + index++; + } + // raise chip select if this is the last transaction + if (last) csPin = 1; + return result; +} +
diff -r 000000000000 -r a15c76864d7d HSP/Interfaces/QuadSpiInterface.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Interfaces/QuadSpiInterface.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,81 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _QUADSPIINTERFACE_H_ +#define _QUADSPIINTERFACE_H_ + +#include "mbed.h" +#include "QuadSpi.h" + +class QuadSpiInterface { +public: + /** + * @brief Constructor that accepts pin names for the QUAD SPI interface + * @param mosi master out slave in pin name + * @param miso master in slave out pin name + * @param sclk serial clock pin name + * @param cs chip select pin name + */ + QuadSpiInterface(PinName mosi, PinName miso, PinName sclk, PinName cs); + /** + * @brief Transmit and recieve QUAD SPI data + * @param tx_buf pointer to transmit byte buffer + * @param tx_size number of bytes to transmit + * @param rx_buf pointer to the recieve buffer + * @param rx_size number of bytes to recieve + * @param last flag to indicate if this is the last QUAD SPI transaction for + * the current chip select cycle + */ + int SPI_Transmit(const uint8_t *tx_buf, uint32_t tx_size, uint8_t *rx_buf, + uint32_t rx_size, int last = 1); + + /** + * @brief Transmit and recieve Four Wrire SPI data + * @param tx_buf pointer to transmit byte buffer + * @param tx_size number of bytes to transmit + * @param rx_buf pointer to the recieve buffer + * @param rx_size number of bytes to recieve + * @param last flag to indicate if this is the last QUAD SPI transaction for + * the current chip select cycle + */ + int SPI_Transmit4Wire(const uint8_t *tx_buf, uint32_t tx_size, + uint8_t *rx_buf, uint32_t rx_size, int last = 1); + +private: + // QUAD SPI object + QuadSPI spi; + // chip select object + DigitalOut csPin; +}; + +#endif // _QUADSPIINTERFACE_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/DataLoggingService.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/DataLoggingService.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,613 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" +#include "Logging.h" +#include "Streaming.h" +#include "RpcServer.h" +#include "S25FS512.h" +#include "PacketFifo.h" +#include "DataLoggingService.h" +#include "HspLed.h" +#include "MAX30001_helper.h" +#include "StringInOut.h" +#include "StringHelper.h" +#include "Peripherals.h" +#include "Device_Logging.h" + +/// BMP280 logging object reference +extern Device_Logging *bmp280_Logging; +/// MAX14720 instance 0 logging object reference +extern Device_Logging *MAX30205_0_Logging; +/// MAX14720 instance 1 logging object reference +extern Device_Logging *MAX30205_1_Logging; + +#define PING_PONG_BUFFER_SIZE 512 +#define HALF_OF_PING_PONG_BUFFER_SIZE PING_PONG_BUFFER_SIZE / 2 +#define MISSION_DEFINITION_SIZE 4096 +#define MISSION_FILE_NAME_LEN 32 + +eLoggingTrigger loggingTrigger; + +/// file on SDCard where mission strings are stored +char missionFileName[MISSION_FILE_NAME_LEN] = "/sd/missions.txt"; + +/// data file on SDCard where mission strings are stored +char dataFileName[MISSION_FILE_NAME_LEN] = "/sd/data.txt"; + +/// buffer where mission strings are stored +char loggingMissionCmds[MISSION_DEFINITION_SIZE]; +/// This houses two 256 byte ram concatenated to act as a ping-pong +uint8_t PingPong_SRAM[PING_PONG_BUFFER_SIZE]; +uint32_t buttonTrigger = 0; + +eLoggingOutput loggingOutput; +// extern int bleStartCommand; +bool volatile globalFlag; +extern int highDataRate; +static uint32_t currentPage; +static uint32_t sramIndex; +/// flag to indicate that sram buffer 0 is dirty and will need to be flushed +static uint32_t sram_buffer_0_dirty; +/// flag to indicate that sram buffer 1 is dirty and will need to be flushed +static uint32_t sram_buffer_1_dirty; +/// usb byte buffer for sending out a bulk transfer +static uint8_t usb_block[64]; +/// running index used to accumulate bytes to send as a block via bulk transfer +static uint16_t usb_block_index = 0; + +typedef enum { + eStartEvent_NULL, + eStartEvent_BLE, + eStartEvent_BUTTON, + eStartEvent_RPC_TO_USB, + eStartEvent_RPC_TO_FLASH +} eStartEvent; +static eStartEvent startEvent; + +/** +* @brief Sets a flag to start USB logging (streaming) +*/ +void LoggingService_StartLoggingUsb(void) { + loggingTrigger = eTriggerLog_RPC_USB; +} + +/** +* @brief Sets a flag to start flash logging +*/ +void LoggingService_StartLoggingFlash(void) { + loggingTrigger = eTriggerLog_RPC_FLASH; +} + +/** +* @brief Checks the various logging start condition +* @return 1 if a start condition is true, 0 if there is no start condition +*/ +static bool _LoggingService_CheckStartCondition(void) { + bool buttonPressed; + buttonPressed = Peripherals::pushButton()->GetButtonFallState(); + + // default not logging USB or flash + loggingOutput = eLogToNothing; + startEvent = eStartEvent_NULL; + if (buttonPressed) { + Peripherals::pushButton()->clearButtonFallState(); + // a falling state has been detected... wait for a fraction of a second and + // re-read the pin + // only start datalogging if the pin was released within this wait time + wait(0.75f); + int buttonRead = Peripherals::pushButton()->Read(); + // if after a period of time the button is still pressed then get out + if (buttonRead == 0) + return 0; + buttonTrigger = 0; + + loggingTrigger = eTriggerLog_BUTTON; + loggingOutput = eLogToFlash; + startEvent = eStartEvent_BUTTON; + return true; + } + if (loggingTrigger == eTriggerLog_RPC_FLASH) { + loggingOutput = eLogToFlash; + startEvent = eStartEvent_RPC_TO_FLASH; + return true; + } + /*if (Peripherals::hspBLE()->getStartDataLogging()) { + loggingTrigger = eTriggerLog_BLE; + loggingOutput = eLogToFlash; + startEvent = eStartEvent_BLE; + return true; + }*/ + // check if start is from RPC call for USB streaming + if (loggingTrigger == eTriggerLog_RPC_USB) { + loggingOutput = eLogtoUsb; + startEvent = eStartEvent_RPC_TO_USB; + return true; + } + return false; +} + +/** +* @brief Read the mission string from flash into a buffer +* @return false if a mission was not defined, true if mission was read and +* buffered +*/ +static bool _LoggingService_ReadMissionFromFlash(void) { + // get mission from flash + Logging_ReadMissionFromFlash((uint8_t *)loggingMissionCmds); + if (Logging_IsMissionDefined((uint8_t *)loggingMissionCmds) == 0) { + return false; + } + printf(loggingMissionCmds); + fflush(stdout); + RPC_ProcessCmds(loggingMissionCmds); + return true; +} + +/** +* @brief Read the mission string from SDCARD into a buffer +* @return false if a mission was not defined, true if mission was read and +* buffered +*/ +static bool _LoggingService_ReadMissionFromSDCard(void) { + // get mission from flash + Logging_ReadMissionFromSDCard((uint8_t *)loggingMissionCmds); + if (Logging_IsMissionDefined((uint8_t *)loggingMissionCmds) == 0) { + return false; + } + printf(loggingMissionCmds); + fflush(stdout); + RPC_ProcessCmds(loggingMissionCmds); + return true; +} + +/** +* @brief Process a RPC command that is pointed to. +* @param cmd RPC string to process +*/ +void ProcessCmd(char *cmd) { + char cmd_[256]; + char reply[512]; + strcpy(cmd_, cmd); + RPC_call(cmd_, reply); +} + +/** +* @brief Buffer sensor fifo data in ram buffers, when a ram buffer is full (a +* flash page worth of data is accumulated) then flash that buffer. +* A buffer ping pong method is used so that one buffer can be flashing as +* the other buffer fills with sensor fifo data. +* @param fifoData Sensor data taken from the fifo to be stored into flash +*/ +static void _LoggingServer_OutputToFlash(uint32_t fifoData) { + uint32_t index; + char str[128]; + uint8_t *ptr; + // + // Log To Flash + // + // i.e. there is data, read one 32-bit size data at a time. + // put the fifo data into the ping-pong SRAM + PingPong_SRAM[sramIndex++] = fifoData & 0xFF; // LSByte goes into index N + PingPong_SRAM[sramIndex++] = (fifoData >> 8) & 0xFF; + PingPong_SRAM[sramIndex++] = (fifoData >> 16) & 0xFF; + PingPong_SRAM[sramIndex++] = (fifoData >> 24) & 0xFF; // MSByte goes into index N+3 + + // flag this buffer as dirty + if (sramIndex <= 256) + sram_buffer_0_dirty = 1; + else + sram_buffer_1_dirty = 1; + + if (sramIndex == 256 || + sramIndex == 512) // Either Ping SRAM or Pong SRAM location is full + { // therefore write to Flash + + index = sramIndex - 256; + ptr = &PingPong_SRAM[index]; + sprintf(str, "currentPage=%d", currentPage); + Peripherals::s25FS512()->writePage_Helper(currentPage, ptr, 0); + + // this page is no longer dirty + if (index == 0) + sram_buffer_0_dirty = 0; + if (index == 256) + sram_buffer_1_dirty = 0; + + currentPage++; + } + sramIndex = sramIndex % 512; // Wrap around the index +} + +/** +* @brief Buffer sensor fifo data in ram buffers, when a ram buffer is full (a +* flash page worth of data is accumulated) then flash that buffer. +* A buffer ping pong method is used so that one buffer can be flashing as +* the other buffer fills with sensor fifo data. +* @param fifoData Sensor data taken from the fifo to be stored into flash +*/ +static void _LoggingServer_OutputToSDCard(FILE* fp, uint32_t fifoData) { + if (fp != NULL){ + fwrite(&fifoData,sizeof(fifoData),1,fp); + } +} + +/** +* @brief If flash ram buffers are flagged as dirty, flush to flash +*/ +static void _LoggingServer_WriteDirtySramBufferToFlash(void) { + uint8_t *ptr = PingPong_SRAM; + if (sram_buffer_0_dirty == 0 && sram_buffer_1_dirty == 0) + return; + if (sram_buffer_0_dirty == 1) { + ptr += 0; + } + if (sram_buffer_1_dirty == 1) { + ptr += 256; + } + printf("_LoggingServer_WriteDirtySramBufferToFlash:%d,%d\n", + sram_buffer_0_dirty, sram_buffer_1_dirty); + fflush(stdout); + // s25fs512_WritePage_Helper(currentPage, ptr, 0); + Peripherals::s25FS512()->writePage_Helper(currentPage, ptr, 0); +} + +/** +* @brief Initialize the USB block running index +* @param fifoData Sensor data taken from the fifo to be sent out USB +*/ +static void _LoggingServer_OutputToCdcAcm(uint32_t fifoData) { + uint8_t *ptr; + uint8_t str[16]; + sprintf((char *)str, "%X ", fifoData); + ptr = str; + usb_block_index = 0; + while (*ptr != 0) { + usb_block[usb_block_index] = *ptr; + ptr++; + usb_block_index++; + } + Peripherals::usbSerial()->writeBlock(usb_block, usb_block_index); +} + +/** +* @brief Initialize the USB block running index +*/ +static void _LoggingServer_OutputToCdcAcm_Start(void) { usb_block_index = 0; } + +/** +* @brief Buffer up fifoData from sensors, do a USB block transfer if buffer is +* full +* @param fifoData Sensor data taken from the fifo to be send out USB within a +* bulk block transfer +* @return Return the success status of the writeblock operation +*/ +static bool _LoggingServer_OutputToCdcAcm_Block(uint32_t fifoData) { + uint8_t str[64]; + uint8_t *ptr; + bool result; + // + // Log to CDCACM + // + result = true; + sprintf((char *)str, "%X ", fifoData); + ptr = str; + while (*ptr != 0) { + usb_block[usb_block_index] = *ptr; + ptr++; + usb_block_index++; + if (usb_block_index >= 64) { + result = Peripherals::usbSerial()->writeBlock(usb_block, 64); + usb_block_index = 0; + } + } + return result; +} + +/** +* @brief Output a full USB block via bulk transfer +*/ +static void _LoggingServer_OutputToCdcAcm_End(void) { + if (usb_block_index == 0) + return; + Peripherals::usbSerial()->writeBlock(usb_block, usb_block_index - 1); +} + +/** +* @brief Blink LED pattern that indicates that the flash end boundary has been +* reached +*/ +static void BlinkEndOfDatalogging(void) { + // blink to signal end of logging + Peripherals::hspLed()->pattern(0x55555555, 20); + wait(2); +} + +/** +* @brief Reads the first data page of flash, if all FF's then the page is empty +* @return 1 if the flash is empty as indicated by the first data page of the +* flash, 0 if not +*/ +int isFlashEmpty(void) { + int i; + uint8_t data[256]; + int firstDataPage = Logging_GetLoggingStartPage(); + Peripherals::s25FS512()->readPages_Helper(firstDataPage, firstDataPage, data, 0); + for (i = 0; i < 256; i++) { + if (data[i] != 0xFF) + return 0; + } + return 1; +} + +/** +* @brief Reads the first data from SDCard, if all FF's then the page is empty +* @return 1 if the flash is empty as indicated by the first data page of the +* flash, 0 if not +*/ +int isSDCardWithoutDataLog(void) { + FILE *fp = NULL; + fp = fopen(dataFileName, "rb"); + if (fp != NULL) { + uint8_t count = 0; + do + { + count ++; + char c = (char)fgetc(fp); + if (count > 2) + { + fclose(fp); + return 0; + } + } while(!feof(fp)); + fclose(fp); + } + return 1; + +} + +/** +* @brief Blink LED pattern that indicates that the flash is not empty and a new +* flash logging session can not occur +*/ +void BlinkFlashNotEmpty(void) { + Peripherals::hspLed()->pattern(0x55555555, 20); + wait(1); +} + +void ExecuteDefaultMission(void) { + ProcessCmd("/MAX30001/CAL_InitStart 01 01 01 03 7FF 00"); + ProcessCmd("/MAX30001/ECG_InitStart 01 01 01 00 02 03 1F 0 00 00 01"); + ProcessCmd("/MAX30001/RtoR_InitStart 01 03 0F 00 03 01 00 00 01"); + ProcessCmd("/MAX30001/Rbias_FMSTR_Init 01 02 01 01 00"); +} + +void LoggingService_Init(void) { loggingTrigger = eTriggerLog_NULL; } + +/** +* @brief This routine checks to see if a USB or flash logging action needs to be taken +* The routine checks for a start condition via button press, USB command, or BLE command +* Once one of these start conditions is present, the logging begins until stopped or memory is full +* @return 1 if successful, 0 if error or logging was aborted and no logging occurred +*/ +uint8_t LoggingService_ServiceRoutine(void) { + uint32_t fifoData; + uint32_t endPage; + FILE *fp; + USBSerial *usbSerial = Peripherals::usbSerial(); + // BMP280 *bmp280 = Peripherals::bmp280(); + bool buttonPressed; + bool endSDLogging = false; + int packetBurstCount = 0; + HspLed *hspLed = Peripherals::hspLed(); + + sramIndex = 0; + // only start logging if conditions exist + + if (_LoggingService_CheckStartCondition() == false) return 0; + printf("Begin Logging..."); + if (startEvent == eStartEvent_NULL) printf("eStartEvent_NULL..."); + if (startEvent == eStartEvent_BLE) printf("eStartEvent_BLE..."); + if (startEvent == eStartEvent_BUTTON) printf("eStartEvent_BUTTON..."); + if (startEvent == eStartEvent_RPC_TO_USB) printf("eStartEvent_RPC_TO_USB..."); + if (startEvent == eStartEvent_RPC_TO_FLASH) printf("eStartEvent_RPC_TO_FLASH..."); + fflush(stdout); + + // start logging stuttered blink pattern + hspLed->pattern(0xA0F3813, 20); + + if (startEvent == eStartEvent_RPC_TO_FLASH || + startEvent == eStartEvent_BUTTON) { + // check to see if datalog already in flash... abort and force user to erase + // flash if needed + if (loggingOutput == eLogToFlash) { + if (isSDCardWithoutDataLog() == 0) { + Logging_SetStart(false); + // bleStartCommand = 0x00; + BlinkFlashNotEmpty(); + hspLed->blink(1000); + printf("Abort Logging, flash log exists. "); + fflush(stdout); + return 0; + } + } + } + + if (startEvent == eStartEvent_BLE) { + // check for mission in flash + if (_LoggingService_ReadMissionFromSDCard() == false) { + // if there is no mission in flash then do a default mission for the sake + // of ble Android app working "out-of-the-box" and stream RtoR and Accel + printf("No Mission in Flash...ExecuteDefaultMission..."); + fflush(stdout); + ExecuteDefaultMission(); + // do not log this data + loggingOutput = eLogToNothing; + } else { + // there is a mission in flash check if there is already logged data + if (isSDCardWithoutDataLog() == 0) { + // just do default mission + printf("Logged Data Detected...ExecuteDefaultMission..."); + fflush(stdout); + ExecuteDefaultMission(); + // do not log this data + loggingOutput = eLogToNothing; + } else { + // flag that we are logging to flash + loggingOutput = eLogToFlash; + } + } + } + + // if we are logging to flash then read mission in flash + if (loggingOutput == eLogToFlash) { + if (_LoggingService_ReadMissionFromSDCard() == + false) { // if there is no mission in flash then get out + Logging_SetStart(false); + Peripherals::hspLed()->pattern(0xC3C3C3C3, 20); + wait(2); + printf("Abort Logging, Mission does not exist. "); + fflush(stdout); + return 0; + } + currentPage = Logging_GetLoggingStartPage(); + endPage = Logging_GetLoggingEndPage(); + } + + MAX30001_Helper_SetupInterrupts(); + if (MAX30001_AnyStreamingSet() == 1) { + MAX30001_Helper_StartSync(); + } + + SetDataLoggingStream(TRUE); + + while (usbSerial->readable()) { + usbSerial->_getc(); + } + fifo_clear(GetUSBIncomingFifo()); // clear USB serial incoming fifo + fifo_clear(GetStreamOutFifo()); + + sram_buffer_0_dirty = 0; + sram_buffer_1_dirty = 0; + + + if (loggingOutput == eLogToNothing) printf("eLogToNothing..."); fflush(stdout); + if (loggingOutput == eLogToFlash) printf("eLogToFlash..."); fflush(stdout); + if (loggingOutput == eLogtoUsb) printf("eLogtoUsb..."); fflush(stdout); + printf("highDataRate=%d...",highDataRate); fflush(stdout); + + + Peripherals::timestampTimer()->reset(); + Peripherals::timestampTimer()->start(); + + _LoggingServer_OutputToCdcAcm_Start(); + while (1) { + if (loggingOutput == eLogToFlash) { + // check if we are at the end of flash + if (currentPage >= endPage) { + BlinkEndOfDatalogging(); // blink for 3 seconds to signal end of logging + break; + } + } + + if (startEvent == eStartEvent_BUTTON) { + buttonPressed = Peripherals::pushButton()->GetButtonFallState(); + if (buttonPressed) { + printf("button pressed, flush the FIFO buffer to SDCard before ending logging\r\n"); + Peripherals::pushButton()->clearButtonFallState(); + // if there is a dirty sram buffer... flush it to flash + _LoggingServer_WriteDirtySramBufferToFlash(); + endSDLogging = true; + } + } + + if (startEvent == eStartEvent_RPC_TO_USB || + startEvent == eStartEvent_RPC_TO_FLASH) { + if (usbSerial->available()) { + if (loggingOutput == eLogToFlash) { + _LoggingServer_WriteDirtySramBufferToFlash(); + } + wait(0.2f); + while (usbSerial->available()) { + usbSerial->_getc(); + } + fifo_clear(GetUSBIncomingFifo()); // clear USB serial incoming fifo + fifo_clear(GetStreamOutFifo()); + break; + } + } + if (fp == NULL && loggingOutput == eLogToFlash) + fp = fopen(dataFileName, "ab+"); + // check to see if data is available + packetBurstCount = 0; + while (PacketFifo_Empty() == 0) { + printf("*"); + if (packetBurstCount >= 100 && endSDLogging == false) + break; + fifoData = PacketFifo_GetUint32(); + if (loggingOutput == eLogToFlash) { + //_LoggingServer_OutputToFlash(fifoData); + _LoggingServer_OutputToSDCard(fp, fifoData); + } + if (loggingOutput == eLogtoUsb) { + if (highDataRate == 0) + _LoggingServer_OutputToCdcAcm(fifoData); + else + _LoggingServer_OutputToCdcAcm_Block(fifoData); + } + packetBurstCount++; + } + + if (endSDLogging) { + endSDLogging = false; + if (fp != NULL) fclose(fp); + fp == NULL; + BlinkEndOfDatalogging(); // blink for 3 seconds to signal end of logging + break; + } + + } + _LoggingServer_OutputToCdcAcm_End(); + printf("End Logging.\n"); + fflush(stdout); + + MAX30001_Helper_Stop(); // if any MAX30001 streams have been started, stop + // them + SetDataLoggingStream(FALSE); + Peripherals::timestampTimer()->stop(); + hspLed->blink(1000); + // default to non-usb packet speed optimizing + highDataRate = 0; + loggingTrigger = eTriggerLog_NULL; + return 1; +} +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/DataLoggingService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/DataLoggingService.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,85 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _LOGGINGSERVICE_H_ +#define _LOGGINGSERVICE_H_ + +#include "mbed.h" + +/// types of logging +typedef enum { + /// do not log + eLogToNothing, + /// log to USB + eLogtoUsb, + /// Log to external flash memory + eLogToFlash +} eLoggingOutput; + +/// types of logging +typedef enum { + eTriggerLog_NULL, + eTriggerLog_RPC_USB, + eTriggerLog_RPC_FLASH, + eTriggerLog_BUTTON, + eTriggerLog_BLE, +} eLoggingTrigger; + +/// extern that indicates the hardware button on the HSP was pressed +extern uint32_t buttonTrigger; + +void LoggingService_Init(void); + +/** +* @brief This routine checks to see if a USB or flash logging action needs to be +* taken +* The routine checks for a start condition via button press, USB +* command, or BLE command +* Once one of these start conditions is present, the logging begins +* until stopped or memory is full +* @return 1 if successful, 0 if error or logging was aborted and no logging +* occurred +*/ +uint8_t LoggingService_ServiceRoutine(void); +/** +* @brief This is called via one of the RPC USB functions to set start conditons +* to start streaming USB +*/ +void LoggingService_StartLoggingUsb(void); +/** +* @brief This is called via one of the RPC USB functions to set start conditons +* to start logging to flash +*/ +void LoggingService_StartLoggingFlash(void); + +#endif /* _LOGGINGSERVICE_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/Device_Logging.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/Device_Logging.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,73 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "Device_Logging.h" + +/** +* @brief Check if logging is enabled for this device +*/ +int Device_Logging::isLoggingEnabled(void) { return enabled; } + +/** +* @brief Returns the sample rate for the device, rate is in seconds +*/ +int Device_Logging::getLoggingSampleRate(void) { return sampleRate; } + +/** +* @brief Initialize the sampling rate for the device +* @param sampleRate Rate to log device output in seconds +*/ +void Device_Logging::initStart(int sampleRate) { + this->sampleRate = sampleRate; + enabled = 1; +} + +/** +* @brief Disables further datalog and streaming sampling for the device +* @param time Time for next sample in seconds, time is relative to a timer +*/ +void Device_Logging::stop(void) { enabled = 0; } + +/** +* @brief Gets a value that represents when device needs to be sampled again, +* used for datalogging and usb streaming +*/ +int Device_Logging::getNextSampleTime(void) { return nextSampleTime; } + +/** +* @brief Sets a value that represents when device needs to be sampled again, +* used for datalogging and usb streaming +* @param time Time for next sample in seconds, time is relative to a timer +*/ +void Device_Logging::setNextSampleTime(int time) { nextSampleTime = time; } +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/Device_Logging.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/Device_Logging.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _DEVICE_LOGGING_H_ +#define _DEVICE_LOGGING_H_ + +#include "mbed.h" + +/** +* @brief Class that is used to store device logging parameters when logging to +* flash or streaming usb +*/ +class Device_Logging { +public: + /** + * @brief Check if logging is enabled for this device + */ + int isLoggingEnabled(void); + /** + * @brief Returns the sample rate for the device, rate is in seconds + */ + int getLoggingSampleRate(void); + /** + * @brief Initialize the sampling rate for the device + * @param sampleRate Rate to log device output in seconds + */ + void initStart(int sampleRate); + /** + * @brief Gets a value that represents when device needs to be sampled again, + * used for datalogging and usb streaming + */ + int getNextSampleTime(void); + /** + * @brief Sets a value that represents when device needs to be sampled again, + * used for datalogging and usb streaming + * @param time Time for next sample in seconds, time is relative to a timer + */ + void setNextSampleTime(int time); + /** + * @brief Disables further datalog and streaming sampling for the device + * @param time Time for next sample in seconds, time is relative to a timer + */ + void stop(void); + +private: + /// The sample rate in seconds + int sampleRate; + /// If logging is enabled or not + int enabled; + /// Bookkeeping var to keep track of the next sample time + int nextSampleTime; +}; + +#endif /* _DEVICE_LOGGING_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/Logging.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/Logging.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,130 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" +#include "RpcServer.h" +#include "Logging.h" +#include "S25FS512.h" +#include "Peripherals.h" + +/// length of flash page as dictated by the device +#define LENGTH_OF_FLASH_PAGE 256 // length of flash page in bytes +/// size in bytes of the external flash device on the HSP platform +#define SIZE_OF_EXTERNAL_FLASH \ + (16777216 / 2) // length of external flash in bytes +/// start page of where the mission is defined +#define MISSION_DEFINITION_START_PAGE 0x00 +/// end page of the mission +#define MISSION_DEFINITION_END_PAGE 0x0F +/// page of where the logging data starts +#define LOGGING_START_PAGE 0x12 +/// the last logging page +#define LOGGING_END_PAGE (SIZE_OF_EXTERNAL_FLASH / LENGTH_OF_FLASH_PAGE) + +/// static flag to know if logging was started via RPC +static bool startLoggingViaRpc = false; + +extern char missionFileName[32]; + +/** +* @brief This will read the mission location and if there is something valid, +* then run the Logging_ProcessMissionCmds() +* @param cmdBuffer buffer +*/ +uint32_t Logging_IsMissionDefined(uint8_t *cmdBuffer) { + uint32_t valid = 1; + if ((cmdBuffer[0] == 0xFF) || (cmdBuffer[0] == 0x0)) + valid = 0; + return valid; +} + +/** +* @brief Read the mission from flash and place in buffer +* @param buffer pointer to byte array that will contain the read results +*/ +int8_t Logging_ReadMissionFromFlash(uint8_t *buffer) { + return Peripherals::s25FS512()->readPages_Helper( + MISSION_DEFINITION_START_PAGE, MISSION_DEFINITION_END_PAGE, buffer, 0); +} + +/** +* @brief Read the mission from SDCard and place in buffer +* @param buffer pointer to byte array that will contain the read results +*/ +int8_t Logging_ReadMissionFromSDCard(uint8_t *buffer) { + FILE *fp = NULL; + if (buffer == NULL) + return -1; + fp = fopen(missionFileName, "r"); + if (fp != NULL) { + do + { + char c = (char)fgetc(fp); + *buffer++ = c; + } while(!feof(fp)); + fclose(fp); + } + else + return -1; + return 0; +} + +//****************************************************************************** +// return the page where mission is defined, Mission specific +uint32_t Logging_GetMissionStartPage(void) { + return MISSION_DEFINITION_START_PAGE; +} + +//****************************************************************************** +// return the page where the mission definition ends, Mission specific +uint32_t Logging_GetMissionEndPage(void) { return MISSION_DEFINITION_END_PAGE; } + +//****************************************************************************** +// Returns the location where the Writing can start from, for data logging... +uint32_t Logging_GetLoggingStartPage(void) { return LOGGING_START_PAGE; } + +//****************************************************************************** +// Returns the end location available where the Flash ends essentially.... for +// data logging. +uint32_t Logging_GetLoggingEndPage(void) { return LOGGING_END_PAGE; } + +//****************************************************************************** +void Logging_SetStart(bool state) { startLoggingViaRpc = state; } + +//****************************************************************************** +bool Logging_GetStart(void) { return startLoggingViaRpc; } + +//****************************************************************************** +// for debugging... always say that usb is not connected... for easy bench +// testing +uint32_t Usb_IsConnected(void) { return 0; } +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/Logging.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/Logging.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,38 @@ +#ifndef _LOGGING_H_ +#define _LOGGING_H_ + +#include "mbed.h" + +/** +* @brief This will read the mission location and if there is something valid, +* then run the Logging_ProcessMissionCmds() +* @param cmdBuffer buffer +*/ +uint32_t Logging_IsMissionDefined(uint8_t *cmdBuffer); + +int8_t Logging_ReadMissionFromSDCard(uint8_t *buffer); + +int8_t Logging_ReadMissionFromFlash(uint8_t *buffer); + +// return the page where mission is defined, Mission specific +uint32_t Logging_GetMissionStartPage(void); + +// return the page where the mission definition ends, Mission specific +uint32_t Logging_GetMissionEndPage(void); + +// Returns the location where the Writing can start from, for data logging... +uint32_t Logging_GetLoggingStartPage(void); + +// Returns the end location available where the Flash ends essentially.... for +// data logging. +uint32_t Logging_GetLoggingEndPage(void); + +// returns one if the usb is connected, zero if not +uint32_t Usb_IsConnected(void); + +void Logging_SetStart(bool state); + +bool Logging_GetStart(void); + +#endif /* _LOGGING_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/Logging_RPC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/Logging_RPC.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,259 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ****************************************************************************** + */ +#include "StringHelper.h" +#include <stdint.h> +#include "Streaming.h" +#include "StringInOut.h" +#include "DataLoggingService.h" +#include "Peripherals.h" +#include "Logging.h" + +extern char loggingMissionCmds[4096]; +uint32_t missionCmdIndex; +extern char missionFileName[32]; +extern char dataFileName[32]; + //****************************************************************************** + int Logging_RPC_StartMissionDefine(char argStrs[32][32], + char replyStrs[32][32]) { + uint32_t i; + uint32_t reply[1]; + + // reset the missionCmdIndex to the beginning of the cmd buffer + missionCmdIndex = 0; + // clear the mission command buffer, fill with zeros + for (i = 0; i < sizeof(loggingMissionCmds); i++) { + loggingMissionCmds[i] = 0; + } + + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//****************************************************************************** +int Logging_RPC_AppendMissionCmd(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + char *strPtr; + uint32_t count = 0; + uint8_t result = 0x80; + // append the string to the mission cmd log + strPtr = argStrs[0]; + while (*strPtr != 0) { + loggingMissionCmds[missionCmdIndex] = *strPtr; + missionCmdIndex++; + strPtr++; + // do not overrun buffer + if (missionCmdIndex > (sizeof(loggingMissionCmds) - 2)) { + result = 0xFF; + break; + } + count++; + // do not read more than max count in incoming string + if (count > (32 * 32)) { + result = 0xFF; + break; + } + } + if (result != 0x80) { + reply[0] = 0xFF; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; + } + // add cr/lf to the end of this cmd string + loggingMissionCmds[missionCmdIndex++] = 13; + loggingMissionCmds[missionCmdIndex++] = 10; + + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//****************************************************************************** +int Logging_RPC_EndMissionDefine(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//****************************************************************************** +int Logging_RPC_WriteMission(char argStrs[32][32], char replyStrs[32][32]) { + char *ptr; + uint32_t reply[1]; + + ptr = loggingMissionCmds; + FILE *fp = fopen(missionFileName, "w"); + if (fp != NULL){ + fprintf(fp, ptr); + fclose(fp); + } + + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//****************************************************************************** +int Logging_RPC_ReadMission(char argStrs[32][32], char replyStrs[32][32]) { + char *ptr; + uint32_t i; + FILE *fp; + ptr = loggingMissionCmds; + fp = fopen(missionFileName, "r"); + if (fp != NULL) { + do + { + char c = (char)fgetc(fp); + *ptr++ = c; + } while(!feof(fp)); + ptr--; + *ptr = 0; + fclose(fp); + } + ptr = loggingMissionCmds; + for (i = 0; i < sizeof(loggingMissionCmds); i++) { + if (*ptr == 13) { + *ptr = ':'; + } else if (*ptr == 10) { + *ptr = ' '; + } + ptr++; + } + + // send it out via uart + putStr(loggingMissionCmds); + replyStrs[0][0] = 0; + return 0; +} + +//****************************************************************************** +int Logging_RPC_EraseMission(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + FILE *fp = fopen(missionFileName, "w"); + if (fp != NULL){ + fprintf(fp, ""); + fclose(fp); + } + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +#define SECTOR_SIZE_256K 0x10000 +#define PAGE_INC_256K 0x400 +#define SECTOR_SIZE_4K 0x1000 +#define PAGE_INC_4K 0x10 +#define TOTAL_SECTOR_NUMBER 263 + +//****************************************************************************** +int Logging_EraseWrittenSectors(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + uint8_t data[512]; + + printf("Logging_EraseWrittenSectors "); + FILE *fp = fopen(missionFileName, "w"); + if (fp != NULL){ + fprintf(fp, ""); + fclose(fp); + } + + fp = fopen(dataFileName, "w"); + if (fp != NULL){ + fprintf(fp, ""); + fclose(fp); + } + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + printf("Logging_EraseWrittenSectors done. \n"); + fflush(stdout); + return 0; +} + +//****************************************************************************** +int Logging_Start(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + Logging_SetStart(true); + reply[0] = 0x80; // indicate success + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//****************************************************************************** +int Logging_GetLastWrittenPage(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + + uint32_t page; + uint32_t lastPage; + uint32_t pageEmpty; + uint8_t data[512]; + + printf("Logging_GetLastWrittenPage "); + fflush(stdout); + lastPage = Logging_GetLoggingEndPage(); + for (page = 2; page <= lastPage; page++) { + // Peripherals::serial()->printf("checking page %d ",page); fflush(stdout); + // sample the page + Peripherals::s25FS512()->readPages_Helper(page, page, data, 0); + pageEmpty = Peripherals::s25FS512()->isPageEmpty(data); + if (pageEmpty != 0) { + break; + } + } + if (page > lastPage) + page = lastPage; + printf("last page %d, 0x%X ", page, page); + fflush(stdout); + reply[0] = page; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +extern int highDataRate; +//****************************************************************************** +int Logging_StartLoggingUsb(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // highDataRate = 0; + LoggingService_StartLoggingUsb(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} +//****************************************************************************** +int Logging_StartLoggingFlash(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + // highDataRate = 0; + LoggingService_StartLoggingFlash(); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +}
diff -r 000000000000 -r a15c76864d7d HSP/LoggingService/Logging_RPC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/LoggingService/Logging_RPC.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _LOGGING_H_ +#define _LOGGING_H_ + +int Logging_RPC_StartMissionDefine(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_AppendMissionCmd(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_EndMissionDefine(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_WriteMission(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_ReadMission(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_EraseMission(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestMission(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestWriteLog(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestReadLog(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestBulkErase(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestSectorsErase(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestReadPage(char argStrs[32][32], char replyStrs[32][32]); +int Logging_RPC_TestWritePage(char argStrs[32][32], char replyStrs[32][32]); +int Logging_EraseWrittenSectors(char argStrs[32][32], char replyStrs[32][32]); +int Logging_StartLoggingUsb(char argStrs[32][32], char replyStrs[32][32]); +int Logging_StartLoggingFlash(char argStrs[32][32], char replyStrs[32][32]); +int Logging_GetLastWrittenPage(char argStrs[32][32], char replyStrs[32][32]); +int Logging_Start(char argStrs[32][32], char replyStrs[32][32]); + +#endif /* _LOGGING_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/QuadSPI/QuadSpi.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/QuadSPI/QuadSpi.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,71 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "QuadSpi.h" +//#include "spi_multi_api.h" + +/** Initialize a SPI master for Quad SPI + * + * @param mosi Pin used for Master Out Slave In + * @param miso Pin used for Master In Slave Out + * @param sclk Pin used for Clock + * @param ssel Pin used for Chip Select + */ +QuadSPI::QuadSPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) + : SPI(mosi, miso, sclk, ssel) { +} + +/******************************************************************************/ +void QuadSPI::setQuadMode() {}//spi_master_width(&_spi, WidthQuad); } +/******************************************************************************/ +void QuadSPI::setSingleMode() {}//spi_master_width(&_spi, WidthSingle); } + +/** Write a byte out in master mode and receive a value + * + * @param value Byte Value to send + * @return Returns Zero + */ +int QuadSPI::write(int value) { + aquire(); + spi_master_write(&_spi, value); + return 0; +} + +/** Read a byte in master mode using Quad SPI simplex transfer + * + * @return Returns the value received from Quad SPI + */ +int QuadSPI::read(void) { + aquire(); + return 0;//spi_master_read(&_spi); +} +
diff -r 000000000000 -r a15c76864d7d HSP/QuadSPI/QuadSpi.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/QuadSPI/QuadSpi.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,89 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _QUADSPI_H_ +#define _QUADSPI_H_ + +#include "mbed.h" + + + + +/** +* This class provides a Quad SPI interface for quad spi devices +* the class also allows single (4-Wire) communication +*/ +class QuadSPI : SPI { + +public: + /** Create a QuadSPI master connected to the specified pins + * + * mosi or miso can be specfied as NC if not used + * + * @param mosi QuadSPI Master Out, Slave In pin + * @param miso QuadSPI Master In, Slave Out pin + * @param sclk QuadSPI Clock pin + * @param ssel QuadSPI chip select pin + */ + QuadSPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC); + + /** Write to the Quad SPI Slave and return the response + * + * @param value Data to be sent to the SPI slave + * + * @returns + * none + */ + int write(int value); + + /** Read from the Quad SPI Slave and return the response + * + * @param none + * + * @returns + * Response from the SPI slave + */ + int read(void); + + /** Allow switching to and from Single SPI and Quad SPI + * + * @param none + * + * @returns + * Response from the SPI slave + */ + void setSingleMode(); + void setQuadMode(); +}; + +#endif /* _QUADSPI_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/PacketFifo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/PacketFifo.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,68 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "RpcFifo.h" +#include "Streaming.h" +#include "RpcServer.h" + +// this will create a packet and insert it on the "jFifo" to be streamed out or +// saved in flash +void PacketFifo_InsertPacket(uint32_t packetId, uint32_t *buffer, + uint32_t numberInBuffer) { + int i, j; + StreamPacketUint32(packetId, buffer, numberInBuffer); + + printf("PacketFifo_InsertPacket \n"); + printf("%d \n", packetId); + for (j=0; j<numberInBuffer; j++){ + for (i=0; i<sizeof(*buffer); i++){ + printf("%d \n" , buffer[i]); + } + } + printf("%d \n", sizeof(*buffer)); + printf("%d \n", numberInBuffer); +} + +// clears the packet fifo "jFifo" +void PacketFifo_Clear(void) { fifo_clear(GetStreamOutFifo()); } + +// returns one if fifo is empty, zero if not empty +int PacketFifo_Empty(void) { return fifo_empty(GetStreamOutFifo()); } + +// returns a uint32 from the fifo, this uint32 is destined to be streamed out +// USB or saved in flash +uint32_t PacketFifo_GetUint32(void) { + uint32_t val; + fifo_get32(GetStreamOutFifo(), &val); + return val; +} +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/PacketFifo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/PacketFifo.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,62 @@ +/******************************************************************************* +* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. +* +* Permission is hereby granted, free of charge, to any person obtaining a +* copy of this software and associated documentation files (the "Software"), +* to deal in the Software without restriction, including without limitation +* the rights to use, copy, modify, merge, publish, distribute, sublicense, +* and/or sell copies of the Software, and to permit persons to whom the +* Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES +* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, +* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +* OTHER DEALINGS IN THE SOFTWARE. +* +* Except as contained in this notice, the name of Maxim Integrated +* Products, Inc. shall not be used except as stated in the Maxim Integrated +* Products, Inc. Branding Policy. +* +* The mere transfer of this software does not imply any licenses +* of trade secrets, proprietary technology, copyrights, patents, +* trademarks, maskwork rights, or any other form of intellectual +* property whatsoever. Maxim Integrated Products, Inc. retains all +* ownership rights. +******************************************************************************* +*/ +#ifndef _PACKETFIFO_H_ +#define _PACKETFIFO_H_ + +#include "mbed.h" + +/** +* this will create a packet and insert it into an outbound fifo to be streamed out or saved in flash +* @param packetId number id to assign to this packet +* @param buffer a 32-bit buffer that contains data that will be used in the packet +* @param numberInBuffer the number of 32-bit elements to be copied from the buffer +*/ +void PacketFifo_InsertPacket(uint32_t packetId, uint32_t *buffer, uint32_t numberInBuffer); + +/** +* clears the packet outbound fifo +*/ +void PacketFifo_Clear(void); + +/** +* returns one if outbound fifo is empty, zero if not empty +*/ +uint32_t PacketFifo_Empty(void); + +/** +* returns a uint32 from the fifo, this uint32 is destined to be streamed out USB or saved in flash +*/ +uint32_t PacketFifo_GetUint32(void); + +#endif /* _PACKETFIFO_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/RpcDeclarations.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/RpcDeclarations.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,444 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _RPCDECLARATIONS_H_ +#define _RPCDECLARATIONS_H_ + +/// define the parts of a RPC. ObjectName, MethodName and function +struct RPC_registeredProcedure { + const char *objectName; + const char *methodName; + //enum eArgType argTypes[4]; + int (*func)(char args[32][32], char results[32][32]); + struct RPC_registeredProcedure *next; +}; + +/// used to keep track of the head of the list and the end of a list +struct RPC_Object { + struct RPC_registeredProcedure *head; + struct RPC_registeredProcedure *last; +}; + +//example /I2c/WriteRead 1 A0 3 11 22 33 2 +#define System_NAME "System" + +/** +* @brief /System/ReadVer +* @details Returns the version string of the FW that is currently running +* @details Example: /System/ReadVer +* @details The command returns a version string similar to this: "HSP FW Version 2.0.1f 8/23/16" +*/ +struct RPC_registeredProcedure Define_System_ReadVer = { System_NAME, "ReadVer", System_ReadVer }; +/** +* @brief /System/ReadBuildTime +* @details Returns the build string of the FW that is currently running, this is the time and date that the firmware was built +* @details Example: /System/ReadBuildTime +* @details The command returns a build string similar to this: "Build Time: Fri Jul 1 15:48:31 2016" +*/ +struct RPC_registeredProcedure Define_System_ReadBuildTime = { System_NAME, "ReadBuildTime", System_ReadBuildTime }; + +#define MAX30001_NAME "MAX30001" +#define MAX30003_NAME "MAX30003" + +#define MAX31725_NAME "MAX31725" +#define MAX30205_NAME "MAX30205" + +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_WriteReg = { MAX30001_NAME, "WriteReg", MAX30001_WriteReg }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_ReadReg = { MAX30001_NAME, "ReadReg", MAX30001_ReadReg }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Start = { MAX30001_NAME, "Start", MAX30001_Start }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Stop = { MAX30001_NAME, "Stop", MAX30001_Stop }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Rbias_FMSTR_Init = { MAX30001_NAME, "Rbias_FMSTR_Init", MAX30001_Rbias_FMSTR_Init }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_CAL_InitStart = { MAX30001_NAME, "CAL_InitStart", MAX30001_CAL_InitStart }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_ECG_InitStart = { MAX30001_NAME, "ECG_InitStart", MAX30001_ECG_InitStart }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_ECGFast_Init = { MAX30001_NAME, "ECGFast_Init", MAX30001_ECGFast_Init }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_PACE_InitStart = { MAX30001_NAME, "PACE_InitStart", MAX30001_PACE_InitStart }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_BIOZ_InitStart = { MAX30001_NAME, "BIOZ_InitStart", MAX30001_BIOZ_InitStart }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_RtoR_InitStart = { MAX30001_NAME, "RtoR_InitStart", MAX30001_RtoR_InitStart }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Stop_ECG = { MAX30001_NAME, "Stop_ECG", MAX30001_Stop_ECG }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Stop_PACE = { MAX30001_NAME, "Stop_PACE", MAX30001_Stop_PACE }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Stop_BIOZ = { MAX30001_NAME, "Stop_BIOZ", MAX30001_Stop_BIOZ }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Stop_RtoR = { MAX30001_NAME, "Stop_RtoR", MAX30001_Stop_RtoR }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Stop_Cal = { MAX30001_NAME, "Stop_Cal", MAX30001_Stop_Cal }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Enable_ECG_LeadON = { MAX30001_NAME, "Enable_ECG_LeadON", MAX30001_Enable_ECG_LeadON }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Enable_BIOZ_LeadON = { MAX30001_NAME, "Enable_BIOZ_LeadON", MAX30001_Enable_BIOZ_LeadON }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_Read_LeadON = { MAX30001_NAME, "Read_LeadON", MAX30001_Read_LeadON }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_StartTest = { MAX30001_NAME, "StartTest", MAX30001_StartTest }; +/** +* @brief /MAX30101/SpO2mode_init fifo_waterlevel_mark sample_avg sample_rate pulse_width red_led_current ir_led_current +* @details This function sets up for the SpO2 mode. +* @param fifo_waterlevel_mark +* @param sample_avg +* @param sample_rate +* @param pulse_width +* @param red_led_current +* @param ir_led_current +*/ +struct RPC_registeredProcedure Define_MAX30001_INT_assignment = { MAX30001_NAME, "INT_assignment", MAX30001_INT_assignment }; + +#define LOGGING_NAME "Logging" +/** +* @brief /Logging/StartMissionDefine +* @details A command to send when you are starting to define a mission +*/ +struct RPC_registeredProcedure Define_Logging_StartMissionDefine = { LOGGING_NAME, "StartMissionDefine", Logging_RPC_StartMissionDefine }; +/** +* @brief /Logging/AppendMissionCmd missionString +* @details Specify a RPC command that is part of a mission +*/ +struct RPC_registeredProcedure Define_Logging_AppendMissionCmd = { LOGGING_NAME, "AppendMissionCmd", Logging_RPC_AppendMissionCmd }; +/** +* @brief /Logging/EndMissionDefine +* @details RPC command that indicated the end of defining a mission +*/ +struct RPC_registeredProcedure Define_Logging_EndMissionDefine = { LOGGING_NAME, "EndMissionDefine", Logging_RPC_EndMissionDefine }; +/** +* @brief /Logging/WriteMission +* @details Write the described mission to flash +*/ +struct RPC_registeredProcedure Define_Logging_WriteMission = { LOGGING_NAME, "WriteMission", Logging_RPC_WriteMission }; +/** +* @brief /Logging/ReadMission +* @details Read the mission from flash +*/ +struct RPC_registeredProcedure Define_Logging_ReadMission = { LOGGING_NAME, "ReadMission", Logging_RPC_ReadMission }; +/** +* @brief /Logging/EraseMission +* @details Erase the mission in flash +*/ +struct RPC_registeredProcedure Define_Logging_EraseMission = { LOGGING_NAME, "EraseMission", Logging_RPC_EraseMission }; +/** +* @brief /Logging/EraseWrittenSectors +* @details Erase the datalog in flash, this erases all of the datalog that has been written to the flash +*/ +struct RPC_registeredProcedure Define_Logging_EraseWrittenSectors = { LOGGING_NAME, "EraseWrittenSectors", Logging_EraseWrittenSectors }; +/** +* @brief /Logging/StartLoggingUsb +* @details Start streaming data through USB +*/ +struct RPC_registeredProcedure Define_Logging_StartLoggingUsb = { LOGGING_NAME, "StartLoggingUsb", Logging_StartLoggingUsb }; +/** +* @brief /Logging/StartLoggingFlash +* @details Start logging data to flash +*/ +struct RPC_registeredProcedure Define_Logging_StartLoggingFlash = { LOGGING_NAME, "StartLoggingFlash", Logging_StartLoggingFlash }; +/** +* @brief /Logging/GetLastWrittenPage +* @details Returns the last page that has been written to flash, this call searchs until it finds an empty flash page +*/ +struct RPC_registeredProcedure Define_Logging_GetLastWrittenPage = { LOGGING_NAME, "GetLastWrittenPage", Logging_GetLastWrittenPage }; +/** +* @brief /Logging/Start +* @details Starts a datalogging session into flash, allows the ability to start datalogging via RPC call +*/ +struct RPC_registeredProcedure Define_Logging_Start = { LOGGING_NAME, "Start", Logging_Start }; + +#define LED_NAME "Led" +/** +* @brief /Led/On +* @details Turn on the HSP onboard LED +*/ +struct RPC_registeredProcedure Define_Led_On = { LED_NAME, "On", Led_On }; +/** +* @brief /Led/Off +* @details Turn off the HSP onboard LED +*/ +struct RPC_registeredProcedure Define_Led_Off = { LED_NAME, "Off", Led_Off }; +/** +* @brief /Led/Blink mS +* @details Start blinking the HSP onboard LED +* @param mS Blink using a mS period +*/ +struct RPC_registeredProcedure Define_Led_BlinkHz = { LED_NAME, "Blink", Led_BlinkHz }; +/** +* @brief /Led/Pattern pattern +* @details Rotate a 32-bit pattern through the LED so that specific blink patterns can be obtained +* @param pattern A 32-bit pattern to rotate through +*/ +struct RPC_registeredProcedure Define_Led_BlinkPattern = { LED_NAME, "Pattern", Led_BlinkPattern }; + +#define S25FS512_NAME "S25FS512" +#define SDCARD_NAME "SDCard" +/** +* @brief /S25FS512/ReadId +* @details Rotate a 32-bit pattern through the LED so that specific blink patterns can be obtained +* @param pattern A 32-bit pattern to rotate through +*/ +struct RPC_registeredProcedure Define_S25FS512_ReadId = { S25FS512_NAME, "ReadId", S25FS512_ReadId }; +/** +* @brief /S25FS512/ReadPagesBinary startPage endPage +* @details Read a page from flash, return the data in binary (non-ascii) +* @param startPage The Starting page to read from +* @param endPage The last page to read from +*/ +struct RPC_registeredProcedure Define_S25FS512_ReadPagesBinary = { S25FS512_NAME, "ReadPagesBinary", S25FS512_ReadPagesBinary }; +/** +* @brief /S25FS512/Reset +* @details Issue a soft reset to the flash device +*/ +struct RPC_registeredProcedure Define_S25FS512_Reset = { S25FS512_NAME, "Reset", S25FS512_Reset }; +/** +* @brief /S25FS512/EnableHWReset +* @details Enable HW resets to the device +*/ +struct RPC_registeredProcedure Define_S25FS512_EnableHWReset = { S25FS512_NAME, "EnableHWReset", S25FS512_EnableHWReset }; +/** +* @brief /S25FS512/SpiWriteRead +* @details Write and read SPI to the flash device using Quad SPI +*/ +struct RPC_registeredProcedure Define_S25FS512_SpiWriteRead = { S25FS512_NAME, "SpiWriteRead", S25FS512_SpiWriteRead }; +/** +* @brief /S25FS512/SpiWriteRead4Wire +* @details Write and read SPI to the flash device using 4 wire +*/ +struct RPC_registeredProcedure Define_S25FS512_SpiWriteRead4Wire = { S25FS512_NAME, "SpiWriteRead4Wire", S25FS512_SpiWriteRead4Wire }; +/** +* @brief /SDCard/IsReady +* @details Check whether SD Card File System is accessible +*/ +struct RPC_registeredProcedure Define_SDCard_IsReady = { SDCARD_NAME, "IsReady", SDCard_IsReady }; + + +#define TESTING_NAME "Testing" +/** +* @brief /Testing/Test_S25FS512 +* @details Start a testing sequence for this device, returns PASS and FAIL strings and detailed results of the test +*/ +struct RPC_registeredProcedure Define_Testing_Test_S25FS512 = { TESTING_NAME, "Test_S25FS512", Test_S25FS512}; + +/** +* @brief /Testing/Test_MAX30001 +* @details Start a testing sequence for this device, returns PASS and FAIL strings and detailed results of the test +*/ +struct RPC_registeredProcedure Define_Testing_Test_MAX30001 = { TESTING_NAME, "Test_MAX30001", Test_MAX30001 }; + +#endif /* _RPCDECLARATIONS_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/RpcFifo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/RpcFifo.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,254 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" +#include "RpcFifo.h" + +/****************************************************************************/ +void fifo_init(fifo_t *fifo, void *mem, unsigned int length) { + // atomic FIFO access + __disable_irq(); + + fifo->rindex = 0; + fifo->windex = 0; + fifo->data = mem; + fifo->length = length; + + __enable_irq(); +} + +/****************************************************************************/ +int fifo_put8(fifo_t *fifo, uint8_t element) { + // Check if FIFO is full + if ((fifo->windex == (fifo->rindex - 1)) || + ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))) { + return -1; + } + + // atomic FIFO access + __disable_irq(); + + // Put data into FIFO + ((uint8_t *)(fifo->data))[fifo->windex] = element; + + // Increment pointer + fifo->windex++; + if (fifo->windex == fifo->length) { + fifo->windex = 0; + } + + __enable_irq(); + + return 0; +} + +/****************************************************************************/ +int fifo_get8(fifo_t *fifo, uint8_t *element) { + // Check if FIFO is empty + if (fifo->rindex == fifo->windex) + return -1; + + // atomic FIFO access + __disable_irq(); + + // Get data from FIFO + *element = ((uint8_t *)(fifo->data))[fifo->rindex]; + + // Increment pointer + fifo->rindex++; + if (fifo->rindex == fifo->length) { + fifo->rindex = 0; + } + + __enable_irq(); + + return 0; +} + +/****************************************************************************/ +int fifo_put16(fifo_t *fifo, uint16_t element) { + // Check if FIFO is full + if ((fifo->windex == (fifo->rindex - 1)) || + ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))) { + return -1; + } + + // atomic FIFO access + __disable_irq(); + + // Put data into FIFO + ((uint16_t *)(fifo->data))[fifo->windex] = element; + + // Increment pointer + fifo->windex++; + if (fifo->windex == fifo->length) { + fifo->windex = 0; + } + + __enable_irq(); + + return 0; +} + +/****************************************************************************/ +int fifo_get16(fifo_t *fifo, uint16_t *element) { + // Check if FIFO is empty + if (fifo->rindex == fifo->windex) + return -1; + + // atomic FIFO access + __disable_irq(); + + // Get data from FIFO + *element = ((uint16_t *)(fifo->data))[fifo->rindex]; + + // Increment pointer + fifo->rindex++; + if (fifo->rindex == fifo->length) { + fifo->rindex = 0; + } + + __enable_irq(); + + return 0; +} + +/****************************************************************************/ +int fifo_put32(fifo_t *fifo, uint32_t element) { + // Check if FIFO is full + if ((fifo->windex == (fifo->rindex - 1)) || + ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))) { + return -1; + } + + // atomic FIFO access + __disable_irq(); + + // Put data into FIFO + ((uint32_t *)(fifo->data))[fifo->windex] = element; + + // Increment pointer + fifo->windex++; + if (fifo->windex == fifo->length) { + fifo->windex = 0; + } + + __enable_irq(); + + return 0; +} + +/****************************************************************************/ +int fifo_get32(fifo_t *fifo, uint32_t *element) { + // Check if FIFO is empty + if (fifo->rindex == fifo->windex) + return -1; + + // atomic FIFO access + __disable_irq(); + + // Get data from FIFO + *element = ((uint32_t *)(fifo->data))[fifo->rindex]; + + // Increment pointer + fifo->rindex++; + if (fifo->rindex == fifo->length) { + fifo->rindex = 0; + } + + __enable_irq(); + + return 0; +} +/****************************************************************************/ +void fifo_clear(fifo_t *fifo) { + // atomic FIFO access + __disable_irq(); + + fifo->rindex = 0; + fifo->windex = 0; + + __enable_irq(); +} + +/****************************************************************************/ +int fifo_empty(fifo_t *fifo) { return (fifo->rindex == fifo->windex); } + +/****************************************************************************/ +int fifo_full(fifo_t *fifo) { + int retval; + + // atomic FIFO access + __disable_irq(); + retval = ((fifo->windex == (fifo->rindex - 1)) || + ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1)))); + __enable_irq(); + + return retval; +} + +/****************************************************************************/ +unsigned int fifo_level(fifo_t *fifo) { + uint16_t value; + + // atomic FIFO access + __disable_irq(); + + if (fifo->windex >= fifo->rindex) { + value = fifo->windex - fifo->rindex; + } else { + value = fifo->length - fifo->rindex + fifo->windex; + } + + __enable_irq(); + + return value; +} + +/****************************************************************************/ +unsigned int fifo_remaining(fifo_t *fifo) { + uint16_t value; + + // atomic FIFO access + __disable_irq(); + + if (fifo->rindex > fifo->windex) { + value = fifo->rindex - fifo->windex - 1; + } else { + value = fifo->length - fifo->windex + fifo->rindex - 1; + } + + __enable_irq(); + + return value; +} +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/RpcFifo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/RpcFifo.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,138 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + * + ******************************************************************************** + */ +#ifndef _RPCFIFO_H_ +#define _RPCFIFO_H_ + +#include <stdint.h> + +/// Structure used for FIFO management +typedef struct { + unsigned int length; ///< FIFO size (number of elements) + void *data; ///< pointer to the FIFO buffer + unsigned int rindex; ///< current FIFO read index + unsigned int windex; ///< current FIFO write index +} fifo_t; + +/** +* @param fifo FIFO on which to perform the operation +* @param mem memory buffer to use for FIFO element storage +* @param length number of elements that the memory buffer can contain +* @returns 0 if successful, -1 upon failure +*/ +void fifo_init(fifo_t *fifo, void *mem, unsigned int length); + +/** +* @brief Adds and 8-bit element to the FIFO +* @param fifo FIFO on which to perform the operation +* @param element element to add to the FIFO +* @returns 0 if successful, -1 upon failure +*/ +int fifo_put8(fifo_t *fifo, uint8_t element); + +/** +* @brief Gets the next 8-bit element to the FIFO +* @param fifo FIFO on which to perform the operation +* @param element pointer to where to store the element from the FIFO +* @returns 0 if successful, -1 upon failure +*/ +int fifo_get8(fifo_t *fifo, uint8_t *element); + +/** +* @brief Adds the next 16-bit element to the FIFO +* @param fifo FIFO on which to perform the operation +* @param element element to add to the FIFO +* @returns 0 if successful, -1 upon failure +*/ +int fifo_put16(fifo_t *fifo, uint16_t element); + +/** +* @brief Gets the next 16-bit element to the FIFO +* @param fifo FIFO on which to perform the operation +* @param element pointer to where to store the element from the FIFO +* @returns 0 if successful, -1 upon failure +*/ +int fifo_get16(fifo_t *fifo, uint16_t *element); + +/** +* @brief Adds the next 16-bit element to the FIFO +* @param fifo FIFO on which to perform the operation +* @param element element to add to the FIFO +* @returns 0 if successful, -1 upon failure +*/ +int fifo_put32(fifo_t *fifo, uint32_t element); + +/** +* @brief Gets the next 16-bit element to the FIFO +* @param fifo FIFO on which to perform the operation +* @param element pointer to where to store the element from the FIFO +* @returns 0 if successful, -1 upon failure +*/ +int fifo_get32(fifo_t *fifo, uint32_t *element); + +/** +* @brief Immediately resets the FIFO to the empty state +* @param fifo FIFO on which to perform the operation +*/ +void fifo_clear(fifo_t *fifo); + +/** +* @brief Determines if the FIFO is empty +* @param fifo FIFO on which to perform the operation +* @returns #TRUE if FIFO is empty, #FALSE otherwise +*/ +int fifo_empty(fifo_t *fifo); + +/** +* @brief FIFO status function +* @param fifo FIFO on which to perform the operation +* @returns #TRUE if FIFO is full, #FALSE otherwise +*/ +int fifo_full(fifo_t *fifo); + +/** +* @brief FIFO status function +* @param fifo FIFO on which to perform the operation +* @returns the number of elements currently in the FIFO +*/ +unsigned int fifo_level(fifo_t *fifo); + +/** +* @brief FIFO status function +* @param fifo FIFO on which to perform the operation +* @returns the remaining elements that can be added to the FIFO +*/ +unsigned int fifo_remaining(fifo_t *fifo); + +#endif // _RPCFIFO_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/RpcServer.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/RpcServer.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,399 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "RpcServer.h" +#include "StringInOut.h" +#include "StringHelper.h" +#include "MAX30001_RPC.h" +#include "Logging_RPC.h" +#include "Peripherals.h" +#include "HspLed_RPC.h" +#include "S25FS512_RPC.h" +#include "Testing_RPC.h" +#include "RpcDeclarations.h" +#include "Device_Logging.h" +#include "../version.h" + +/// define the version string that is reported with a RPC "ReadVer" command +#define FW_VERSION_STRING "MAX30001 FW Ver" + +char args[32][32]; +char results[32][32]; + +/// define a fifo for incoming USB data +static fifo_t fifo; +/// define a buffer for incoming USB data +static uint8_t fifoBuffer[128]; +/// define stream out fifo +static fifo_t fifoStreamOut; +/// allocate a large fifo buffer for streaming out +static uint32_t streamOutBuffer[0xC000 / 4]; + +/// define a device log for the BMP280, keeps track of mission and loggin status +Device_Logging *bmp280_Logging; +/// define a device log for the MAX30205 (instance 0), keeps track of mission +/// and loggin status +Device_Logging *MAX30205_0_Logging; +/// define a device log for the MAX30205 (instance 1), keeps track of mission +/// and loggin status +Device_Logging *MAX30205_1_Logging; + +//****************************************************************************** +fifo_t *GetUSBIncomingFifo(void) { return &fifo; } + +//****************************************************************************** +fifo_t *GetStreamOutFifo(void) { return &fifoStreamOut; } + +//****************************************************************************** +int System_ReadVer(char argStrs[32][32], char replyStrs[32][32]) { + char version[32]; + snprintf(version, sizeof(version)/sizeof(char), "%s %d.%d.%d %02d/%02d/%02d", + FW_VERSION_STRING, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, + VERSION_MONTH, VERSION_DAY, VERSION_SHORT_YEAR); + strcpy(replyStrs[0], version); + strcpy(replyStrs[1], "\0"); + return 0; +} + +//****************************************************************************** +int System_ReadBuildTime(char argStrs[32][32], char replyStrs[32][32]) { + // strcpy(replyStrs[0],buildTime); + // strcpy(replyStrs[1],"\0"); + return 0; +} + +//****************************************************************************** +int System_SystemCoreClock(char argStrs[32][32], char replyStrs[32][32]) { + sprintf(replyStrs[0], "SystemCoreClock = %d", SystemCoreClock); + strcpy(replyStrs[1], "\0"); + return 0; +} + +//****************************************************************************** +int System_GetTimestamp(char argStrs[32][32], char replyStrs[32][32]) { + sprintf(replyStrs[0], "GetTimestamp = %d", 0); + strcpy(replyStrs[1], "\0"); + return 0; +} + +static struct RPC_Object RPC_Procedures = {NULL, NULL}; + +//****************************************************************************** +void RPC_addProcedure(struct RPC_registeredProcedure *procedure) { + struct RPC_Object *obj = &RPC_Procedures; + if (obj->last != NULL) { + obj->last->next = procedure; + } + if (obj->head == NULL) { + obj->head = procedure; + } + procedure->next = NULL; + obj->last = procedure; +} + +//****************************************************************************** +void RPC_init(void) { + + fifo_init(&fifo, fifoBuffer, sizeof(fifoBuffer)); + fifo_init(&fifoStreamOut, streamOutBuffer, + sizeof(streamOutBuffer) / sizeof(uint32_t)); + + // MAX30001 + RPC_addProcedure(&Define_MAX30001_WriteReg); + RPC_addProcedure(&Define_MAX30001_ReadReg); + RPC_addProcedure(&Define_MAX30001_Start); + RPC_addProcedure(&Define_MAX30001_Stop); + RPC_addProcedure(&Define_MAX30001_Enable_ECG_LeadON); + RPC_addProcedure(&Define_MAX30001_Enable_BIOZ_LeadON); + RPC_addProcedure(&Define_MAX30001_Read_LeadON); + RPC_addProcedure(&Define_MAX30001_StartTest); + RPC_addProcedure(&Define_MAX30001_INT_assignment); + RPC_addProcedure(&Define_MAX30001_Rbias_FMSTR_Init); + RPC_addProcedure(&Define_MAX30001_CAL_InitStart); + RPC_addProcedure(&Define_MAX30001_ECG_InitStart); + RPC_addProcedure(&Define_MAX30001_ECGFast_Init); + RPC_addProcedure(&Define_MAX30001_PACE_InitStart); + RPC_addProcedure(&Define_MAX30001_BIOZ_InitStart); + RPC_addProcedure(&Define_MAX30001_RtoR_InitStart); + RPC_addProcedure(&Define_MAX30001_Stop_ECG); + RPC_addProcedure(&Define_MAX30001_Stop_PACE); + RPC_addProcedure(&Define_MAX30001_Stop_BIOZ); + RPC_addProcedure(&Define_MAX30001_Stop_RtoR); + RPC_addProcedure(&Define_MAX30001_Stop_Cal); + + // Logging + RPC_addProcedure(&Define_Logging_StartMissionDefine); + RPC_addProcedure(&Define_Logging_AppendMissionCmd); + RPC_addProcedure(&Define_Logging_EndMissionDefine); + RPC_addProcedure(&Define_Logging_WriteMission); + RPC_addProcedure(&Define_Logging_ReadMission); + RPC_addProcedure(&Define_Logging_EraseMission); + RPC_addProcedure(&Define_Logging_EraseWrittenSectors); + RPC_addProcedure(&Define_Logging_StartLoggingUsb); + RPC_addProcedure(&Define_Logging_StartLoggingFlash); + RPC_addProcedure(&Define_Logging_GetLastWrittenPage); + RPC_addProcedure(&Define_Logging_Start); + + // led + RPC_addProcedure(&Define_Led_On); + RPC_addProcedure(&Define_Led_Off); + RPC_addProcedure(&Define_Led_BlinkHz); + RPC_addProcedure(&Define_Led_BlinkPattern); + + // S25FS512 + RPC_addProcedure(&Define_S25FS512_ReadId); + RPC_addProcedure(&Define_S25FS512_ReadPagesBinary); + RPC_addProcedure(&Define_S25FS512_Reset); + RPC_addProcedure(&Define_S25FS512_EnableHWReset); + RPC_addProcedure(&Define_S25FS512_SpiWriteRead); + RPC_addProcedure(&Define_S25FS512_SpiWriteRead4Wire); + + RPC_addProcedure(&Define_Testing_Test_MAX30001); + + // SDCard + RPC_addProcedure(&Define_SDCard_IsReady); + + // System + RPC_addProcedure(&Define_System_ReadVer); + RPC_addProcedure(&Define_System_ReadBuildTime); +} + +//****************************************************************************** +struct RPC_registeredProcedure *RPC_lookup(char *objectName, char *methodName) { + struct RPC_registeredProcedure *ptr; + // lookup all registered methods + ptr = RPC_Procedures.head; + while (ptr != NULL) { + if (strcmp(ptr->objectName, objectName) == 0 && + strcmp(ptr->methodName, methodName) == 0) { + // we found a match... return with it + return ptr; + } + ptr = ptr->next; + } + return NULL; +} + +//****************************************************************************** +char *GetToken(char *inStr, char *outStr, int start, char ch) { + int i; + int index = 0; + int length = strlen(inStr); + for (i = start; i < length; i++) { + if (inStr[i] != ch) { + outStr[index++] = inStr[i]; + } else { + break; + } + } + outStr[index++] = 0; + return outStr; +} + +//****************************************************************************** +void SendCommandList(char *reply) { + struct RPC_registeredProcedure *ptr; + reply[0] = 0; + ptr = RPC_Procedures.head; + while (ptr != NULL) { + strcat(reply, "/"); + strcat(reply, ptr->objectName); + strcat(reply, "/"); + strcat(reply, ptr->methodName); + strcat(reply, ","); + ptr = ptr->next; + } + strcat(reply, "\r\n"); +} + +//****************************************************************************** +int CheckForDoubleQuote(char *str) { + int doubleQuoteFound; + // scan through arguments, see if there is a double quote for a string + // argument + doubleQuoteFound = 0; + while (*str != 0) { + if (*str == '\"') { + doubleQuoteFound = 1; + break; + } + str++; + } + return doubleQuoteFound; +} + +//****************************************************************************** +void ExtractDoubleQuoteStr(char *src, char *dst) { + int start; + + dst[0] = 0; + start = 0; + while (*src != 0) { + // look for start + if ((*src == '\"') && (start == 0)) { + start = 1; + src++; + continue; + } + // look for end + if ((*src == '\"') && (start == 1)) { + *dst = 0; // terminate the string + break; + } + if (start == 1) { + *dst = *src; + dst++; + } + src++; + } +} + +//****************************************************************************** +void RPC_call_test(void) { + int doubleQuoteFound; + char doubleQuoteStr[64]; + char *request = "/Logging/AppendMissionCmd \"BMP280 InitStart 1\""; + + // scan through arguments, see if there is a double quote for a string + // argument + doubleQuoteFound = CheckForDoubleQuote(request); + if (doubleQuoteFound) { + ExtractDoubleQuoteStr(request, doubleQuoteStr); + } +} + +//****************************************************************************** +void RPC_call(char *request, char *reply) { + const char slash[2] = "/"; + const char space[2] = " "; + char *objectName; + char *methodName; + char doubleQuoteStr[128]; + char requestCpy[256]; + char *token; + int argIndex; + int resultIndex; + int doubleQuoteFound; + struct RPC_registeredProcedure *procedurePtr; + + // clear out the reply + reply[0] = 0; + // copy the request for scanning and extraction later + strcpy(requestCpy, request); + // check for beginning forward slash + if (request[0] != '/') { + return; + } + // check for only a forward slash + if (request[0] == '/' && request[1] == 0) { + SendCommandList(reply); + return; + } + strcat(request, " "); + // get the object name + token = strtok(request, slash); + // token = GetToken(request, tokenBuffer, 1, '/'); + objectName = token; + if (objectName == NULL) + return; // must have an object name + // get the method name + token = strtok(NULL, space); + methodName = token; + if (methodName == NULL) + return; // must have a method name + + // scan through arguments, see if there is a double quote for a string + // argument + doubleQuoteFound = CheckForDoubleQuote(requestCpy); + + if (doubleQuoteFound == 0) { + // walk through arguments + argIndex = 0; + token = strtok(NULL, space); + while (token != NULL) { + // save this arg in array + strcpy(args[argIndex++], token); + // read next token arg if any + token = strtok(NULL, space); + } + // terminate the end of the string array with an empty string + strcpy(args[argIndex], "\0"); + strcpy(results[0], "\0"); + } else { + // grab out the double quote string + ExtractDoubleQuoteStr(requestCpy, doubleQuoteStr); + argIndex = 0; + // token = strtok(NULL, quote); + strcpy(args[argIndex++], doubleQuoteStr); + } + + // + // alias the MAX30001 and MAX30003 names + // + if (strcmp(objectName, MAX30003_NAME) == 0) { + strcpy(objectName, MAX30001_NAME); + } + + procedurePtr = RPC_lookup(objectName, methodName); + if (procedurePtr != NULL) { + // printf("RPC_call: %s processing\n",requestCpy); + procedurePtr->func(args, results); + } else { + printf("RPC_call: %s not found\n", requestCpy); + // printf("Unable to lookup %s %s", objectName, methodName); + } + + // loop while (if) there are results to return + resultIndex = 0; + strcpy(reply, "\0"); + while (results[resultIndex][0] != '\0') { + strcat(reply, results[resultIndex++]); + strcat(reply, " "); + } + strcat(reply, "\r\n"); +} + +//****************************************************************************** +void RPC_ProcessCmds(char *cmds) { + char cmd[32 * 32]; + char *ptrCmds; + char reply[512]; + ptrCmds = cmds; + + while (*ptrCmds != 0) { + ptrCmds = ParseUntilCRLF(ptrCmds, cmd, sizeof(cmd)); + if (*cmd != 0) { + RPC_call(cmd, reply); + } + } +} +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/RpcServer.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/RpcServer.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _RPCSERVER_H_ +#define _RPCSERVER_H_ + +#include "mbed.h" +#include "RpcFifo.h" +#include "Device_Logging.h" + +/** +* @brief Reads the Version of the HSP FCache_Writel +*/ +int System_ReadVer(char argStrs[32][32], char replyStrs[32][32]); +/** +* @brief Reads the built time of the RPC FW +*/ +int System_ReadBuildTime(char argStrs[32][32], char replyStrs[32][32]); + +void RPC__init(void); +void RPC__call(char *request, char *reply); +fifo_t *GetUSBIncomingFifo(void); +fifo_t *GetStreamOutFifo(void); +/** +* @brief Batch process RPC commands +*/ +void RPC_ProcessCmds(char *cmds); +/** +* @brief Initialize the RPC server with all of the commands that it supports +*/ +void RPC_init(void); +/** +* @brief Initialize the RPC server with all of the commands that it supports +*/ +void RPC_call(char *request, char *reply); + +#endif // _RPCSERVER_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/Streaming.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/Streaming.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,263 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" +#include "USBMSD_BD.h" +#include "SDBlockDevice.h" +#include "HeapBlockDevice.h" +#include "FATFileSystem.h" + +#include "RpcServer.h" +#include "RpcFifo.h" +#include "Streaming.h" +#include "Peripherals.h" +#include "MAX30001.h" +#include "Test_MAX30001.h" +#include "Peripherals.h" + +//Another define the FAT File system and SD Card +extern SDBlockDevice sd; +extern FATFileSystem fs; + + +bool streaming = FALSE; +bool dataLogging = FALSE; + +extern uint32_t ECG_Data[5120]; +extern uint32_t BIOZ_Data[1280]; +extern uint32_t PACE_Data[1280]; +extern uint32_t RTOR_Data[1280]; + +extern uint32_t ECG_Data_Size; +extern uint32_t BIOZ_Data_Size; +extern uint32_t PACE_Data_Size; +extern uint32_t RTOR_Data_Size; + +/** +* @brief Encodes a 0x55 0xAA signature and a simple checksum to the id byte in +* the 32 bit field +* @param id Streaming ID +*/ +uint32_t StreamIdChecksumCalculate(uint32_t id) { + uint32_t sum; + uint32_t calculated; + sum = 0x55; + sum += 0xAA; + sum += id; + sum &= 0xFF; + sum = sum << 8; + calculated = 0x55AA0000 + sum + id; + return calculated; +} + +/** +* @brief Creates a packet that will be streamed via USB or saved into flash +* datalog memory +* @brief the packet created will be inserted into a fifo to be streamed at a +* later time +* @param id Streaming ID +* @param buffer Pointer to a uint32 array that contains the data to include in +* the packet +* @param number Number of elements in the buffer +*/ +void StreamPacketUint32(uint32_t id, uint32_t *buffer, uint32_t number) { + uint32_t checksumId; + int i; + if (streaming == TRUE || dataLogging == TRUE) { + checksumId = StreamIdChecksumCalculate(id); + StreamFifoId(checksumId); + StreamFifoTimeStamp(); + StreamFifoLength(number); + StreamFifoUint32Array(buffer, number); + } + /* + if (testing_max30001 == 1) { + if (id == MAX30001_DATA_ECG) + testing_ecg_flags[TESTING_ECG_FLAG] = 1; + if (id == MAX30001_DATA_BIOZ) + testing_ecg_flags[TESTING_BIOZ_FLAG] = 1; + if (id == MAX30001_DATA_PACE) + testing_ecg_flags[TESTING_PACE_FLAG] = 1; + if (id == MAX30001_DATA_RTOR) + testing_ecg_flags[TESTING_RTOR_FLAG] = 1; + } + */ + //printf("streaming \n"); + /* + FILE *fp = fopen("/sd/myfile888.txt", "a"); + fprintf(fp, "%s %d %s","Hello World!!!!", i, "\n"); + fclose(fp); printf("id=%d num=%d\n", id, number); + */ + // printf("id1=%x \n", id); + + if(id == 0x30) + { + for(i=0; i<number; i++) + { + ECG_Data[ECG_Data_Size+i] = buffer[i]>>6; + + } + ECG_Data_Size = ECG_Data_Size + number; + + } + // printf("E_S1 = %d \n", ECG_Data_Size); + + if(id == 0x33) + { + for(i=0; i<number; i++) + { + BIOZ_Data[BIOZ_Data_Size+i] = buffer[i]*4+5; + + } + BIOZ_Data_Size = BIOZ_Data_Size + number; + // printf("Get BIOZ_Data \n"); + } + // printf("B_S1 = %d \n", BIOZ_Data_Size); + + /* + if(id == 0x31) + { + FILE *fp = fopen("/sd/PACE.txt", "a"); + for(i=0 ; i<number ; i++){ + fprintf(fp, "%s %d %s","PACE", buffer[i], "\n"); + } + fclose(fp); + printf("Save PACE \n"); + } + if(id == 0x32) + { + FILE *fp = fopen("/sd/RTOR.txt", "a"); + for(i=0 ; i<number ; i++){ + fprintf(fp, "%s %d %s","RTOR", buffer[i], "\n"); + } + fclose(fp); + printf("Save RTOR \n"); + } + if(id == 0x33) + { + FILE *fp = fopen("/sd/BIOZ.txt", "a"); + for(i=0 ; i<number ; i++){ + fprintf(fp, "%s %d %s","BIOZ", buffer[i], "\n"); + } + fclose(fp); + printf("Save BIOZ \n"); + } + */ +} + +/** +* @brief Insert a buffer into the out going fifo +* @param buffer Array of uint32 to send to the fifo +* @param len Length of the array +*/ +int StreamFifoUint32Array(uint32_t buffer[], uint32_t len) { + int status; + uint32_t i; + for (i = 0; i < len; i++) { + status = fifo_put32(GetStreamOutFifo(), buffer[i]); + if (status == -1) { + printf("FIFO_OF!"); + fflush(stdout); + while (1) + ; + } + } + return 0; +} + +/** +* @brief Insert a timestamp into the out going fifo +*/ +int StreamFifoTimeStamp(void) { + int status; + // uint32_t timer = timestamp_GetCurrent(); //RTC_GetVal(); + uint32_t timer = (uint32_t)Peripherals::timestampTimer()->read_us(); + status = fifo_put32(GetStreamOutFifo(), timer); + if (status == -1) { + printf("FIFO_OF!"); + fflush(stdout); + while (1) + ; + } + return 0; +} + +/** +* @brief Insert a packet id into the out going fifo +* @param id The uint32 packet id +*/ +int StreamFifoId(uint32_t id) { + int status; + status = fifo_put32(GetStreamOutFifo(), id); + if (status == -1) { + printf("FIFO_OF!"); + fflush(stdout); + while (1) + ; + } + return 0; +} + +/** +* @brief Insert a length value into the out going fifo +* @param length A uint32 number representing a length +*/ +int StreamFifoLength(uint32_t length) { + int status; + status = fifo_put32(GetStreamOutFifo(), length); + if (status == -1) { + printf("FIFO_OF!"); + fflush(stdout); + while (1) + ; + } + return 0; +} + +/** +* @brief Return a value that indicates if the system is streaming data +* @returns Returns a one or zero value +*/ +uint8_t IsStreaming(void) { return streaming; } + +/** +* @brief Set a flag to indicate if streaming is enabled +* @param state A one or zero value +*/ +void SetStreaming(uint8_t state) { streaming = state; } + +/** +* @brief Set a flag to indicate if datalogging is enabled +* @param state A one or zero value +*/ +void SetDataLoggingStream(uint8_t state) { dataLogging = state; } +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/Streaming.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/Streaming.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _STREAMING_H_ +#define _STREAMING_H_ + +#include "mbed.h" + +#define PACKET_LIS2DH 0x20 + +#define PACKET_MAX30205_TEMP_TOP 0x40 +#define PACKET_MAX30205_TEMP_BOTTOM 0x50 +#define PACKET_BMP280_PRESSURE 0x60 +#define PACKET_LSM6DS3_ACCEL 0x70 +#define PACKET_MAX30205_TEMP 0x80 +#define PACKET_NOP 0x90 + +/** +* @brief Creates a packet that will be streamed via USB or saved into flash +* datalog memory +* @brief the packet created will be inserted into a fifo to be streamed at a +* later time +* @param id Streaming ID +* @param buffer Pointer to a uint32 array that contains the data to include in +* the packet +* @param number Number of elements in the buffer +*/ +void StreamPacketUint32(uint32_t id, uint32_t *buffer, uint32_t number); +/** +* @brief Insert a buffer into the out going fifo +* @param buffer Array of uint32 to send to the fifo +* @param len Length of the array +*/ +int StreamFifoUint32Array(uint32_t buffer[], uint32_t len); +/** +* @brief Insert a timestamp into the out going fifo +*/ +int StreamFifoTimeStamp(void); +/** +* @brief Insert a packet id into the out going fifo +* @param id The uint32 packet id +*/ +int StreamFifoId(uint32_t id); +/** +* @brief Return a value that indicates if the system is streaming data +* @returns Returns a one or zero value +*/ +uint8_t IsStreaming(void); +/** +* @brief Set a flag to indicate if streaming is enabled +* @param state A one or zero value +*/ +void SetStreaming(uint8_t state); +/** +* @brief Set a flag to indicate if datalogging is enabled +* @param state A one or zero value +*/ +void SetDataLoggingStream(uint8_t state); +/** +* @brief Insert a length value into the out going fifo +* @param length A uint32 number representing a length +*/ +int StreamFifoLength(uint32_t length); + +#endif // _STREAMING_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/StringHelper.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/StringHelper.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,241 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" +#include "StringHelper.h" + +/** +* @brief Process an array of hex alpha numeric strings representing arguments of +* type uint8 +* @param args Array of strings to process +* @param argsUintPtr Pointer of uint8 to save converted arguments +* @param numberOf Number of strings to convert +*/ +void ProcessArgs(char args[32][32], uint8_t *argsUintPtr, int numberOf) { + int i; + int val; + for (i = 0; i < numberOf; i++) { + sscanf(args[i], "%x", &val); + argsUintPtr[i] = (uint8_t)val; + } +} + +/** +* @brief Process an array of hex alpha numeric strings representing arguments of +* type uint32 +* @param args Array of strings to process +* @param argsUintPtr Pointer of uint32 to save converted arguments +* @param numberOf Number of strings to convert +*/ +void ProcessArgs32(char args[32][32], uint32_t *argsUintPtr, int numberOf) { + int i; + int val; + for (i = 0; i < numberOf; i++) { + sscanf(args[i], "%x", &val); + argsUintPtr[i] = val; + } +} + +/** +* @brief Process an array of decimal numeric strings representing arguments of +* type uint32 +* @param args Array of strings to process +* @param argsUintPtr Pointer of uint32 to save converted arguments +* @param numberOf Number of strings to convert +*/ +void ProcessArgs32Dec(char args[32][32], uint32_t *argsUintPtr, int numberOf) { + int i; + int val; + for (i = 0; i < numberOf; i++) { + sscanf(args[i], "%d", &val); + argsUintPtr[i] = val; + } +} + +/** +* @brief Parse a single string in decimal format to a uint32 number +* @param str String to process +* @returns Returns the converted number of type uint32 +*/ +uint32_t ParseAsciiDecU32(char *str) { + uint32_t val; + sscanf(str, "%d", &val); + return val; +} + +/** +* @brief Parse a single string in hex format to a uint32 number +* @param str String to process +* @returns Returns the converted number of type uint32 +*/ +uint32_t ParseAsciiHexU32(char *str) { + uint32_t val; + sscanf(str, "%x", &val); + return val; +} + +/** +* @brief Process a string that is "embedded" within another string using the /" +* delimiters +* @brief Extract the string +* @param str String to process +* @returns Returns the string +*/ +void ProcessString(char args[32][32], uint8_t *str, int numberOf) { + int i; + int start; + uint8_t *ptr; + uint8_t *strPtr; + ptr = (uint8_t *)args; + strPtr = str; + start = 0; + for (i = 0; i < numberOf; i++) { + if ((start == 0) && (*ptr == '/"')) { + start = 1; + ptr++; + continue; + } + if ((start == 1) && (*ptr == '/"')) { + break; + } + if (start == 1) { + *strPtr = *ptr; + strPtr++; + } + ptr++; + } + // terminate the string + *strPtr = 0; +} + +/** +* @brief Parse an incoming string until a CRLF is encountered, dst will contain +* the parsed string +* @param src source string to process +* @param dst destination string to contain the parsed incoming string +* @param length length of incoming src string +* @returns updated pointer in src string +*/ +char *ParseUntilCRLF(char *src, char *dst, int length) { + int i; + char *srcPtr; + + srcPtr = src; + i = 0; + *dst = 0; + while ((*srcPtr != 0) && (*srcPtr != 13)) { + dst[i] = *srcPtr; + i++; + if (i >= length) + break; + srcPtr++; + } + if (*srcPtr == 13) + srcPtr++; + if (*srcPtr == 10) + srcPtr++; + dst[i] = 0; // terminate the string + return srcPtr; +} + +/** +* @brief Parse an incoming string hex value into an 8-bit value +* @param str string to process +* @returns 8-bit byte that is parsed from the string +*/ +uint8_t StringToByte(char str[32]) { + int val; + uint8_t byt; + sscanf(str, "%x", &val); + byt = (uint8_t)val; + return byt; +} + +/** +* @brief Parse an incoming string hex value into 32-bit value +* @param str string to process +* @returns 32-bit value that is parsed from the string +*/ +uint32_t StringToInt(char str[32]) { + int val; + uint32_t byt; + sscanf(str, "%x", &val); + byt = (uint32_t)val; + return byt; +} + +/** +* @brief Format a binary 8-bit array into a string +* @param uint8Ptr byte buffer to process +* @param numberOf number of bytes in the buffer +* @param reply an array of strings to place the converted array values into +*/ +void FormatReply(uint8_t *uint8Ptr, int numberOf, char reply[32][32]) { + int i; + for (i = 0; i < numberOf; i++) { + sprintf(reply[i], "%02X", uint8Ptr[i]); + } + strcpy(reply[i], "\0"); +} + +/** +* @brief Format a binary 32-bit array into a string +* @param uint32Ptr 32-bit value buffer to process +* @param numberOf number of values in the buffer +* @param reply an array of strings to place the converted array values into +*/ +void FormatReply32(uint32_t *uint32Ptr, int numberOf, char reply[32][32]) { + int i; + for (i = 0; i < numberOf; i++) { + sprintf(reply[i], "%02X", uint32Ptr[i]); + } + strcpy(reply[i], "\0"); +} + +/** +* @brief Format a binary 8-bit array into a string +* @param data 8-bit value buffer to process +* @param length number of values in the buffer +* @param str output string buffer +* @return output string +*/ +char *BytesToHexStr(uint8_t *data, uint32_t length, char *str) { + uint32_t i; + char tmpStr[8]; + str[0] = 0; + for (i = 0; i < length; i++) { + sprintf(tmpStr, "%02X ", data[i]); + strcat(str, tmpStr); + } + return str; +} +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/StringHelper.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/StringHelper.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,119 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _STRINGHELPER_H_ +#define _STRINGHELPER_H_ + +#include "mbed.h" + +/** +* @brief Process an array of hex alpha numeric strings representing arguments of +* type uint8 +* @param args Array of strings to process +* @param argsUintPtr Pointer of uint8 to save converted arguments +* @param numberOf Number of strings to convert +*/ +void ProcessArgs(char args[32][32], uint8_t *argsUintPtr, int numberOf); +/** +* @brief Format a binary 8-bit array into a string +* @param uint8Ptr byte buffer to process +* @param numberOf number of bytes in the buffer +* @param reply an array of strings to place the converted array values into +*/ +void FormatReply(uint8_t *uint8Ptr, int numberOf, char reply[32][32]); +/** +* @brief Process an array of hex alpha numeric strings representing arguments of +* type uint32 +* @param args Array of strings to process +* @param argsUintPtr Pointer of uint32 to save converted arguments +* @param numberOf Number of strings to convert +*/ +void ProcessArgs32(char args[32][32], uint32_t *argsUintPtr, int numberOf); +/** +* @brief Process an array of decimal numeric strings representing arguments of +* type uint32 +* @param args Array of strings to process +* @param argsUintPtr Pointer of uint32 to save converted arguments +* @param numberOf Number of strings to convert +*/ +void ProcessArgs32Dec(char args[32][32], uint32_t *argsUintPtr, int numberOf); +/** +* @brief Format a binary 32-bit array into a string +* @param uint32Ptr 32-bit value buffer to process +* @param numberOf number of values in the buffer +* @param reply an array of strings to place the converted array values into +*/ +void FormatReply32(uint32_t *uint32Ptr, int numberOf, char reply[32][32]); +/** +* @brief Parse an incoming string hex value into an 8-bit value +* @param str string to process +* @returns 8-bit byte that is parsed from the string +*/ +uint8_t StringToByte(char str[32]); +/** +* @brief Parse an incoming string hex value into 32-bit value +* @param str string to process +* @returns 32-bit value that is parsed from the string +*/ +uint32_t StringToInt(char str[32]); +/** +* @brief Parse a single string in decimal format to a uint32 number +* @param str String to process +* @returns Returns the converted number of type uint32 +*/ +uint32_t ParseAsciiDecU32(char *str); +/** +* @brief Parse a single string in hex format to a uint32 number +* @param str String to process +* @returns Returns the converted number of type uint32 +*/ +uint32_t ParseAsciiHexU32(char *str); +/** +* @brief Format a binary 8-bit array into a string +* @param data 8-bit value buffer to process +* @param length number of values in the buffer +* @param str output string buffer +* @return output string +*/ +char *BytesToHexStr(uint8_t *data, uint32_t length, char *str); +/** +* @brief Parse an incoming string until a CRLF is encountered, dst will contain +* the parsed string +* @param src source string to process +* @param dst destination string to contain the parsed incoming string +* @param length length of incoming src string +* @returns updated pointer in src string +*/ +char *ParseUntilCRLF(char *src, char *dst, int length); + +#endif // _STRINGHELPER_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/StringInOut.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/StringInOut.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,196 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" +#include "USBSerial.h" +#include "RpcFifo.h" +#include "RpcServer.h" +#include "StringInOut.h" +#include "Peripherals.h" + +/// a running index that keeps track of where an incoming string has been +/// buffered to +static int lineBuffer_index = 0; +/// a flag that keeps track of the state of accumulating a string +static int getLine_State = GETLINE_WAITING; + +/** +* @brief Place incoming USB characters into a fifo +* @param data_IN buffer of characters +* @param len length of data +*/ +int fifoIncomingChars(uint8_t data_IN[], unsigned int len) { + int i; + for (i = 0; i < len; i++) { + fifo_put8(GetUSBIncomingFifo(), data_IN[i]); + } + return 0; +} + +/** +* @brief Check the USB incoming fifo to see if there is data to be read +* @return 1 if there is data to be read, 0 if data is not available +*/ +int isReadReady(void) { + if (fifo_empty(GetUSBIncomingFifo()) == 0) + return 1; + return 0; +} + +/** +* @brief Clear the incoming USB read fifo +*/ +void clearOutReadFifo(void) { fifo_clear(GetUSBIncomingFifo()); } + +/** +* @brief Block until a character can be read from the USB +* @return the character read +*/ +char getch(void) { + uint8_t ch; + // block until char is ready + while (isReadReady() == 0) { + } + // read a char from buffer + fifo_get8(GetUSBIncomingFifo(), &ch); + return ch; +} + +/** +* @brief Place incoming USB characters into a fifo +* @param lineBuffer buffer to place the incoming characters +* @param bufferLength length of buffer +* @return GETLINE_WAITING if still waiting for a CRLF, GETLINE_DONE +*/ +int getLine(char *lineBuffer, int bufferLength) { + uint8_t ch; + + USBSerial *serial = Peripherals::usbSerial(); + if (getLine_State == GETLINE_DONE) { + getLine_State = GETLINE_WAITING; + } + if (serial->available() != 0) { + ch = serial->_getc(); + if (ch != 0x0A && ch != 0x0D) { + lineBuffer[lineBuffer_index++] = ch; + } + if (ch == 0x0D) { + lineBuffer[lineBuffer_index++] = 0; + lineBuffer_index = 0; + getLine_State = GETLINE_DONE; + } + if (lineBuffer_index > bufferLength) { + lineBuffer[bufferLength - 1] = 0; + getLine_State = GETLINE_DONE; + } + } + return getLine_State; +} + +/** +* @brief Block until a fixed number of characters has been accumulated from the +* incoming USB +* @param lineBuffer buffer to place the incoming characters +* @param maxLength length of buffer +*/ +void getStringFixedLength(uint8_t *lineBuffer, int maxLength) { + uint8_t ch; + int index = 0; + // block until maxLength is captured + while (1) { + ch = getch(); + lineBuffer[index++] = ch; + if (index == maxLength) + return; + } +} + +/** +* @brief Output a string out the USB serial port +* @param str output this str the USB channel +*/ +int putStr(const char *str) { + Peripherals::usbSerial()->printf("%s", str); // fflush(stdout); + // uint8_t *ptr; + // uint8_t buffer[256]; + // int index = 0; + /* int length; + ptr = (uint8_t *)str; + length = strlen(str); + + Peripherals::usbSerial()->writeBlock(ptr,length); */ + return 0; +} + +/** +* @brief Outut an array of bytes out the USB serial port +* @param data buffer to output +* @param length length of buffer +*/ +int putBytes(uint8_t *data, uint32_t length) { + int sendThis = 64; + int sent = 0; + int thisLeft; + uint8_t *ptr = data; + if (length < 64) + sendThis = length; + do { + Peripherals::usbSerial()->writeBlock(ptr, sendThis); + sent += sendThis; + ptr += sendThis; + thisLeft = length - sent; + sendThis = 64; + if (thisLeft < 64) + sendThis = thisLeft; + } while (sent != length); + return 0; +} + +/** +* @brief Outut 256 byte blocks out the USB serial using writeBlock bulk +* transfers +* @param data buffer of blocks to output +* @param length length of 256-byte blocks +*/ +int putBytes256Block(uint8_t *data, int numberBlocks) { + int i; + uint8_t *ptr; + ptr = data; + const int BLOCK_SIZE = 32; + const int FLASH_PAGE_SIZE = 256; + for (i = 0; i < numberBlocks * (FLASH_PAGE_SIZE / BLOCK_SIZE); i++) { + Peripherals::usbSerial()->writeBlock(ptr, BLOCK_SIZE); + ptr += BLOCK_SIZE; + } + return 0; +} +
diff -r 000000000000 -r a15c76864d7d HSP/RpcServer/StringInOut.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/RpcServer/StringInOut.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _STRINGINOUT_H_ +#define _STRINGINOUT_H_ + +#include "mbed.h" +#include "USBSerial.h" + +/// indicates that a string up to a CRLF is being accumulated +#define GETLINE_WAITING 1 +/// indicates that a string is being processes +#define GETLINE_PROCESSING 2 +/// indicates that a CRLF string has been buffered and can be processed +#define GETLINE_DONE 3 + +/** +* @brief Clear the incoming USB read fifo +*/ +void clearOutReadFifo(void); +/** +* @brief Block until a character can be read from the USB +* @return the character read +*/ +char getch(void); +/** +* @brief Place incoming USB characters into a fifo +* @param lineBuffer buffer to place the incoming characters +* @param bufferLength length of buffer +* @return GETLINE_WAITING if still waiting for a CRLF, GETLINE_DONE +*/ +int getLine(char *lineBuffer, int bufferLength); +/** +* @brief Block until a fixed number of characters has been accumulated from the +* incoming USB +* @param lineBuffer buffer to place the incoming characters +* @param maxLength length of buffer +*/ +void getStringFixedLength(uint8_t *lineBuffer, int maxLength); +/** +* @brief Output a string out the USB serial port +* @param str output this str the USB channel +*/ +int putStr(const char *str); +/** +* @brief Place incoming USB characters into a fifo +* @param data_IN buffer of characters +* @param len length of data +*/ +int fifoIncomingChars(uint8_t data_IN[], unsigned int len); +/** +* @brief Outut an array of bytes out the USB serial port +* @param data buffer to output +* @param length length of buffer +*/ +int putBytes(uint8_t *data, uint32_t length); +/** +* @brief Outut 256 byte blocks out the USB serial using writeBlock bulk +* transfers +* @param data buffer of blocks to output +* @param length length of 256-byte blocks +*/ +int putBytes256Block(uint8_t *data, int numberBlocks); + +#endif // _STRINGINOUT_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/System/Peripherals.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/System/Peripherals.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "Peripherals.h" + +USBSerial *Peripherals::mUSBSerial = NULL; +MAX30001 *Peripherals::mMAX30001 = NULL; +HspLed *Peripherals::mHspLed = NULL; +Timer *Peripherals::mTimestampTimer = NULL; +S25FS512 *Peripherals::mS25FS512 = NULL; +PushButton *Peripherals::mPushButton = NULL; +SDBlockDevice *Peripherals::mSD = NULL; +DigitalIn *Peripherals::mSDDetect = NULL;
diff -r 000000000000 -r a15c76864d7d HSP/System/Peripherals.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/System/Peripherals.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,88 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _PERIPHERALS_H_ +#define _PERIPHERALS_H_ + +#include "mbed.h" +#include "USBSerial.h" +#include "HspLed.h" +#include "MAX30001.h" +#include "S25FS512.h" +#include "PushButton.h" +#include "SDBlockDevice.h" +/** +* This static class is used as a central locatoin for all devices on the HSP platform +* it gives (in-effect) a singleton interface for each device so that anywhere in code +* one can reference on of these devices +*/ +class Peripherals { +public: + static USBSerial *setUSBSerial(USBSerial * device) { mUSBSerial = device; return device; } + static USBSerial *usbSerial(void) { return mUSBSerial; } + + static HspLed *hspLed(void) { return mHspLed; } + static HspLed *setHspLed(HspLed *device) { mHspLed = device; return device; } + + static MAX30001 *max30001(void) { return mMAX30001; } + static MAX30001 *setMAX30001(MAX30001 *device) { mMAX30001 = device; return device; } + + static Timer *timestampTimer(void) { return mTimestampTimer; } + static Timer *setTimestampTimer(Timer *timer) { mTimestampTimer = timer; return timer; } + + static S25FS512 *s25FS512(void) { return mS25FS512; } + static S25FS512 *setS25FS512(S25FS512 *s25FS512) { mS25FS512 = s25FS512; return s25FS512; } + + static PushButton *pushButton(void) { return mPushButton; } + static PushButton *setPushButton(PushButton *pushButton) { mPushButton = pushButton; return pushButton; } + + static SDBlockDevice *sdFS(void) { return mSD; } + static SDBlockDevice *setSdFS(SDBlockDevice *SD) { mSD = SD; return SD; } + + static DigitalIn *SDDetect(void) { return mSDDetect; } + static DigitalIn *setSDDetect(DigitalIn *SDDetect) { mSDDetect = SDDetect; return SDDetect; } + +private: + static USBSerial *mUSBSerial; + static MAX30001 *mMAX30001; + static HspLed *mHspLed; + static Serial *mSerial; + static Timer *mTimestampTimer; + static S25FS512 *mS25FS512; + static PushButton *mPushButton; + static SDBlockDevice *mSD; + static DigitalIn *mSDDetect; +}; + +#endif // _PERIPHERALS_H_ + +
diff -r 000000000000 -r a15c76864d7d HSP/System/System.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/System/System.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "mbed.h" + +//****************************************************************************** +void I2CM_Init_Reset(uint8_t index, int speed) { + mxc_i2cm_regs_t *regs = MXC_I2CM_GET_I2CM(index); + /* reset module */ + regs->ctrl = MXC_F_I2CM_CTRL_MSTR_RESET_EN; + regs->ctrl = 0; + /* enable tx_fifo and rx_fifo */ + regs->ctrl |= (MXC_F_I2CM_CTRL_TX_FIFO_EN | MXC_F_I2CM_CTRL_RX_FIFO_EN); +} +
diff -r 000000000000 -r a15c76864d7d HSP/System/System.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/System/System.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _SYSTEM_H_ +#define _SYSTEM_H_ + +/** +* This issues a reset to the I2C Peripheral +*/ +void I2CM_Init_Reset(uint8_t index, int speed); + +#endif // _SYSTEM_H_ +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Test_MAX30001.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Test_MAX30001.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,302 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "Test_MAX30001.h" +#include "Test_Utilities.h" + +uint32_t testing_max30001 = 0; +uint32_t testing_ecg_flags[4]; + +//****************************************************************************** +void test_MAX30001(void (*outputString)(const char *)) { + int totalPass = 1; + int pass; + uint32_t foundEcg = 0; + uint32_t foundBioz = 0; + uint32_t foundPace = 0; + uint32_t foundRtoR = 0; + uint32_t id; + char str2[128]; + int partVersion; // 0 = 30004 + // 1 = 30001 + // 2 = 30002 + // 3 = 30003 + Timer timer; + MAX30001 *max30001; + max30001 = Peripherals::max30001(); + + // read the id + max30001->max30001_reg_read(MAX30001::INFO, &id); + // read id twice because it needs to be read twice + max30001->max30001_reg_read(MAX30001::INFO, &id); + partVersion = id >> 12; + partVersion = partVersion & 0x3; + + // display header + if (partVersion == 0) + outputString("Testing MAX30004|"); + if (partVersion == 1) { + outputString("Testing MAX30001|"); + outputString("Testing ECG, RtoR, BioZ, PACE|"); + } + if (partVersion == 2) + outputString("Testing MAX30002|"); + if (partVersion == 3) { + outputString("Testing MAX30003|"); + outputString("Only Testing ECG and RtoR|"); + } + sprintf(str2, "Device ID = 0x%06X|", id); + outputString(str2); + + // clear testing flags + testing_ecg_flags[TESTING_ECG_FLAG] = 0; + testing_ecg_flags[TESTING_BIOZ_FLAG] = 0; + testing_ecg_flags[TESTING_PACE_FLAG] = 0; + testing_ecg_flags[TESTING_RTOR_FLAG] = 0; + + // start streams + testing_max30001 = 1; + if (partVersion == 1) + outputString("Start Streaming ECG, RtoR, PACE, BIOZ, CAL enabled, " + "verifying streams...|"); + if (partVersion == 3) + outputString( + "Start Streaming ECG, RtoR, CAL enabled, verifying streams...|"); + // max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + max30001->max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + max30001->max30001_ECG_InitStart(0b1, 0b1, 0b1, 0b0, 0b10, 0b11, 0x1F, 0b00, + 0b00, 0b0, 0b01); + if (partVersion == 1) + max30001->max30001_PACE_InitStart(0b1, 0b0, 0b0, 0b1, 0x0, 0b0, 0b00, 0b0, + 0b0); + if (partVersion == 1) + max30001->max30001_BIOZ_InitStart(0b1, 0b1, 0b1, 0b10, 0b11, 0b00, 7, 0b0, + 0b010, 0b0, 0b10, 0b00, 0b00, 2, 0b0, + 0b111, 0b0000, 0b0000); + max30001->max30001_RtoR_InitStart(0b1, 0b0011, 0b1111, 0b00, 0b0011, 0b000001, + 0b00, 0b000, 0b01); + max30001->max30001_Rbias_FMSTR_Init(0b01, 0b10, 0b1, 0b1, 0b00); + max30001->max30001_synch(); + + // look for each stream + timer.start(); + while (1) { + if ((foundEcg == 0) && (testing_ecg_flags[TESTING_ECG_FLAG] == 1)) { + foundEcg = 1; + outputString("ECG Stream: PASS|"); + } + if ((foundBioz == 0) && (testing_ecg_flags[TESTING_BIOZ_FLAG] == 1)) { + foundBioz = 1; + outputString("Bioz Stream: PASS|"); + } + if ((foundPace == 0) && (testing_ecg_flags[TESTING_PACE_FLAG] == 1)) { + foundPace = 1; + outputString("PACE Stream: PASS|"); + } + if ((foundRtoR == 0) && (testing_ecg_flags[TESTING_RTOR_FLAG] == 1)) { + foundRtoR = 1; + outputString("RtoR Stream: PASS|"); + } + if ((foundEcg == 1) && (foundBioz == 1) && (foundPace == 1) && + (foundRtoR == 1) && (partVersion == 1)) { + break; + } + if ((foundEcg == 1) && (foundRtoR == 1) && (partVersion == 3)) { + break; + } + if (timer.read() >= TESTING_MAX30001_TIMEOUT_SECONDS) { + break; + } + } + timer.stop(); + if (foundEcg == 0) { + outputString("ECG Stream: FAIL|"); + totalPass &= pass; + } + if ((foundBioz == 0) && (partVersion == 1)) { + outputString("Bioz Stream: FAIL|"); + totalPass &= pass; + } + if ((foundPace == 0) && (partVersion == 1)) { + outputString("PACE Stream: FAIL|"); + totalPass &= pass; + } + if (foundRtoR == 0) { + outputString("RtoR Stream: FAIL|"); + totalPass &= pass; + } + + // stop all streams + max30001->max30001_Stop_ECG(); + if (partVersion == 1) + max30001->max30001_Stop_PACE(); + if (partVersion == 1) + max30001->max30001_Stop_BIOZ(); + max30001->max30001_Stop_RtoR(); + testing_max30001 = 0; + // final results + printf("Result: "); + _printPassFail(totalPass, 0, outputString); +} + +//****************************************************************************** +void test_MAX30001_1(void) { + int totalPass = 1; + int pass; + uint32_t foundEcg = 0; + uint32_t foundBioz = 0; + uint32_t foundPace = 0; + uint32_t foundRtoR = 0; + uint32_t id; + char str2[128]; + int partVersion; // 0 = 30004 + // 1 = 30001 + // 2 = 30002 + // 3 = 30003 + Timer timer; + MAX30001 *max30001; + max30001 = Peripherals::max30001(); + + // read the id + max30001->max30001_reg_read(MAX30001::INFO, &id); + // read id twice because it needs to be read twice + max30001->max30001_reg_read(MAX30001::INFO, &id); + partVersion = id >> 12; + partVersion = partVersion & 0x3; + + // display header + if (partVersion == 0) + printf("Testing MAX30004|"); + if (partVersion == 1) { + printf("Testing MAX30001|"); + printf("Testing ECG, RtoR, BioZ, PACE|"); + } + if (partVersion == 2) + printf("Testing MAX30002|"); + if (partVersion == 3) { + printf("Testing MAX30003|"); + printf("Only Testing ECG and RtoR|"); + } + sprintf(str2, "Device ID = 0x%06X|", id); + printf(str2); + + // clear testing flags + testing_ecg_flags[TESTING_ECG_FLAG] = 0; + testing_ecg_flags[TESTING_BIOZ_FLAG] = 0; + testing_ecg_flags[TESTING_PACE_FLAG] = 0; + testing_ecg_flags[TESTING_RTOR_FLAG] = 0; + + // start streams + testing_max30001 = 1; + if (partVersion == 1) + printf("Start Streaming ECG, RtoR, PACE, BIOZ, CAL enabled, " + "verifying streams...|"); + if (partVersion == 3) + printf( + "Start Streaming ECG, RtoR, CAL enabled, verifying streams...|"); + // max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + max30001->max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + max30001->max30001_ECG_InitStart(0b1, 0b1, 0b1, 0b0, 0b10, 0b11, 0x1F, 0b00, + 0b00, 0b0, 0b01); + if (partVersion == 1) + max30001->max30001_PACE_InitStart(0b1, 0b0, 0b0, 0b1, 0x0, 0b0, 0b00, 0b0, + 0b0); + if (partVersion == 1) + max30001->max30001_BIOZ_InitStart(0b1, 0b1, 0b1, 0b10, 0b11, 0b00, 7, 0b0, + 0b010, 0b0, 0b10, 0b00, 0b00, 2, 0b0, + 0b111, 0b0000, 0b0000); + max30001->max30001_RtoR_InitStart(0b1, 0b0011, 0b1111, 0b00, 0b0011, 0b000001, + 0b00, 0b000, 0b01); + max30001->max30001_Rbias_FMSTR_Init(0b01, 0b10, 0b1, 0b1, 0b00); + max30001->max30001_synch(); + + // look for each stream + timer.start(); + while (1) { + if ((foundEcg == 0) && (testing_ecg_flags[TESTING_ECG_FLAG] == 1)) { + foundEcg = 1; + printf("ECG Stream: PASS|"); + } + if ((foundBioz == 0) && (testing_ecg_flags[TESTING_BIOZ_FLAG] == 1)) { + foundBioz = 1; + printf("Bioz Stream: PASS|"); + } + if ((foundPace == 0) && (testing_ecg_flags[TESTING_PACE_FLAG] == 1)) { + foundPace = 1; + printf("PACE Stream: PASS|"); + } + if ((foundRtoR == 0) && (testing_ecg_flags[TESTING_RTOR_FLAG] == 1)) { + foundRtoR = 1; + printf("RtoR Stream: PASS|"); + } + if ((foundEcg == 1) && (foundBioz == 1) && (foundPace == 1) && + (foundRtoR == 1) && (partVersion == 1)) { + break; + } + if ((foundEcg == 1) && (foundRtoR == 1) && (partVersion == 3)) { + break; + } + if (timer.read() >= TESTING_MAX30001_TIMEOUT_SECONDS) { + break; + } + } + timer.stop(); + if (foundEcg == 0) { + printf("ECG Stream: FAIL|"); + totalPass &= pass; + } + if ((foundBioz == 0) && (partVersion == 1)) { + printf("Bioz Stream: FAIL|"); + totalPass &= pass; + } + if ((foundPace == 0) && (partVersion == 1)) { + printf("PACE Stream: FAIL|"); + totalPass &= pass; + } + if (foundRtoR == 0) { + printf("RtoR Stream: FAIL|"); + totalPass &= pass; + } + + // stop all streams + max30001->max30001_Stop_ECG(); + if (partVersion == 1) + max30001->max30001_Stop_PACE(); + if (partVersion == 1) + max30001->max30001_Stop_BIOZ(); + max30001->max30001_Stop_RtoR(); + testing_max30001 = 0; + // final results + printf("Result: "); + //_printPassFail(totalPass, 0, outputString); +} +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Test_MAX30001.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Test_MAX30001.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _TEST_MAX30001_H_ +#define _TEST_MAX30001_H_ + +#include "mbed.h" +#include "MAX30001.h" +#include "Peripherals.h" + +#define TESTING_ECG_FLAG 0 +#define TESTING_BIOZ_FLAG 1 +#define TESTING_PACE_FLAG 2 +#define TESTING_RTOR_FLAG 3 + +#define TESTING_MAX30001_TIMEOUT_SECONDS 10 + +extern uint32_t testing_max30001; +extern uint32_t testing_ecg_flags[4]; + +/** +* @brief Selftest the MAX30001 and output the results as strings using the pointer function +* @param outputString Pointer to the function used to output the test results +*/ +void test_MAX30001(void (*outputString)(const char *)); + +void test_MAX30001_1(void); + + +#endif /* _TEST_MAX30101_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Test_S25FS512.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Test_S25FS512.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "Test_S25FS512.h" +#include "Test_Utilities.h" + +//****************************************************************************** +void test_S25FS512(void (*outputString)(const char *)) { + char tempStr[32]; + uint8_t page[264]; + int totalPass = 1; + int pass; + int status; + int i; + uint8_t data[128]; + S25FS512 *s25FS512; + + s25FS512 = Peripherals::s25FS512(); + + // display header + outputString("Testing S25FS512|"); + + // read id test + s25FS512->readIdentification(data, sizeof(data)); + s25FS512->readIdentification(data, sizeof(data)); + if ((data[1] == 0x01) && (data[2] == 0x02) && (data[3] == 0x19) && + (data[4] == 0x4D)) { + sprintf(tempStr, "Read ID: Pass "); + outputString(tempStr); + } else { + sprintf(tempStr, "Read ID: Fail "); + outputString(tempStr); + totalPass = 0; + } + sprintf(tempStr, "(%02X%02X%02X%02X)|", data[1], data[2], data[3], data[4]); + outputString(tempStr); + + if (totalPass == 1) { + // format sector 0 + outputString("Formatting Sector 0, "); + s25FS512->sectorErase_Helper(0); + + // verify format sector 0 + outputString("Verifying Format of Sector 0: "); + s25FS512->readPages_Helper(0, 0, page, 0); + pass = s25FS512->isPageEmpty(page); + _printPassFail(pass, 1, outputString); + totalPass &= pass; + + // fill page with pattern + for (i = 0; i < 256; i++) { + page[i] = i; + } + // write to page 0 + outputString("Writing Page 0 to pattern, "); + s25FS512->writePage_Helper(0, page, 0); + // clear pattern in memory + for (i = 0; i < 256; i++) { + page[i] = 0x00; + } + // read back page and verify + outputString("Read Page Verify: "); + s25FS512->readPages_Helper(0, 0, page, 0); + // compare memory for pattern + pass = 1; + for (i = 0; i < 256; i++) { + if (page[i] != i) + pass = 0; + } + _printPassFail(pass, 1, outputString); + totalPass &= pass; + + // format sector 0 to clean up after tests + s25FS512->sectorErase_Helper(0); + } else { + outputString("Read Id Failed, Skipping rest of test|"); + } + + // final results + outputString("Result: "); + _printPassFail(totalPass, 0, outputString); +} +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Test_S25FS512.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Test_S25FS512.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _TEST_S25FS512_H_ +#define _TEST_S25FS512_H_ + +#include "mbed.h" +#include "S25FS512.h" +#include "Peripherals.h" + +/** +* @brief Selftest the S25FS512 and output the results as strings using the pointer function +* @param outputString Pointer to the function used to output the test results +*/ +void test_S25FS512(void (*outputString)(const char *)); + +#endif /* _TEST_S25FS512_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Test_Utilities.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Test_Utilities.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include "Test_Utilities.h" + +//****************************************************************************** +void _printPassFail(int status, int newLine, void (*outputString)(char const *)) { + if (status == 1) { + outputString("PASS"); + } else { + outputString("FAIL"); + } + if (newLine == 1) { + outputString("|"); + } +} +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Test_Utilities.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Test_Utilities.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,47 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _TEST_UTILITIES_H_ +#define _TEST_UTILITIES_H_ + +#include "mbed.h" + +/** +* Function that will output a pass/fail formated string based on the incoming status value +* @param status Value that indicated pass or fail status +* @param newLine Should this call output a new line +* @param outputString Pointer to a function that will do the actual outputting of the string +*/ +void _printPassFail(int status, int newLine, void (*outputString)(const char *)); + +#endif /* _TEST_UTILITIES_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Testing_RPC.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Testing_RPC.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#include <stdint.h> +#include "StringHelper.h" +#include "Test_MAX30001.h" +#include "Test_S25FS512.h" +#include "StringInOut.h" + +void outputTestResultString(const char *resultStr); + +//****************************************************************************** +int Test_S25FS512(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + test_S25FS512(outputTestResultString); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +//****************************************************************************** +int Test_MAX30001(char argStrs[32][32], char replyStrs[32][32]) { + uint32_t reply[1]; + test_MAX30001(outputTestResultString); + reply[0] = 0x80; + FormatReply32(reply, sizeof(reply) / sizeof(uint32_t), replyStrs); + return 0; +} + +void outputTestResultString(const char *resultStr) { putStr(resultStr); } +
diff -r 000000000000 -r a15c76864d7d HSP/Test/Testing_RPC.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/Test/Testing_RPC.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,46 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ +#ifndef _TESTING_RPC_H_ +#define _TESTING_RPC_H_ + +/** +* RPC executed routine that will perform platform self testing for the S25FS512 +*/ +int Test_S25FS512(char argStrs[32][32], char replyStrs[32][32]); +/** +* RPC executed routine that will perform platform self testing for the MAX30001 +*/ +int Test_MAX30001(char argStrs[32][32], char replyStrs[32][32]); + +#endif /* _TESTING_RPC_H_ */ +
diff -r 000000000000 -r a15c76864d7d HSP/version.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HSP/version.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,7 @@ +#define VERSION_MAJOR 3 +#define VERSION_MINOR 1 +#define VERSION_PATCH 0 + +#define VERSION_SHORT_YEAR 17 +#define VERSION_MONTH 8 +#define VERSION_DAY 23 \ No newline at end of file
diff -r 000000000000 -r a15c76864d7d USBDevice.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBDevice.lib Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/teams/MaximIntegrated/code/USBDevice/#dad310740b28
diff -r 000000000000 -r a15c76864d7d USBMSD_BD.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/USBMSD_BD.lib Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/switches/code/USBMSD_BD/#13fed87fb66f
diff -r 000000000000 -r a15c76864d7d main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,404 @@ +#include "mbed.h" +#include "max32630fthr.h" +#include "USBMSD_BD.h" +#include "SDBlockDevice.h" +#include "HeapBlockDevice.h" +#include "FATFileSystem.h" + +#include "StringInOut.h" +#include "Streaming.h" +#include "Peripherals.h" +#include "MAX30001.h" +#include "RpcServer.h" +#include "DataLoggingService.h" + +#include "Test_MAX30001.h" +#include "Test_Utilities.h" + +#define BLOCK_SIZE 512 + + +MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); + +DigitalOut rLED(LED1); +DigitalOut gLED(LED2); +DigitalOut bLED(LED3); + +DigitalOut outLED(P3_4); +DigitalIn Button(P3_5); + +// Physical block device, can be any device that supports the BlockDevice API +// HeapBlockDevice bd(512*BLOCK_SIZE, BLOCK_SIZE); +//SDBlockDevice bd(P0_5, P0_6, P0_4, P0_7); + +// File system declaration +//FATFileSystem fs("fs"); + +//Another define the FAT File system and SD Card +SDBlockDevice sd(P0_5, P0_6, P0_4, P0_7); +FATFileSystem fs("sd", &sd); + + + +// USB MSD +//USBMSD_BD msd(&bd); + + +/// DigitalOut for CS +DigitalOut cs(P5_6); +/// SPI Master 2 with SPI0_SS for use with MAX30001 +SPI spi(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // used by MAX30001 + +/// ECG device +MAX30001 max30001(&spi, &cs); +InterruptIn max30001_InterruptB(P5_5); +InterruptIn max30001_Interrupt2B(P5_4); + +// main() runs in its own thread in the OS +// (note the calls to Thread::wait below for delays) + +uint32_t ECG_Data[5120]; +uint32_t BIOZ_Data[1280]; +uint32_t PACE_Data[1280]; +uint32_t RTOR_Data[1280]; + +uint32_t ECG_Data_Size = 0; +uint32_t BIOZ_Data_Size = 0; +uint32_t PACE_Data_Size = 0; +uint32_t RTOR_Data_Size = 0; + +uint32_t global_key_judge = 0; +int current_log_number = 0; +char BIOZ_file_name[20] = "/sd/BIOZ"; +int event_tag = 0; + + +int main() +{ + uint32_t i, id; + uint32_t reg; + int partVersion; // 0 = 30004 + + int totalPass = 1; + int pass; + uint32_t foundEcg = 0; + uint32_t foundBioz = 0; + uint32_t foundPace = 0; + uint32_t foundRtoR = 0; + + // local input state of the RPC + int inputState; + // RPC request buffer + char request[128]; + // RPC reply buffer + char reply[128]; + int err; + + //int32_t i; + printf("SD Card Example\n"); + gLED = LED_ON; + rLED = LED_ON; + + FILE *fp = fopen("/sd/myfile12345678.txt", "a"); + for(i=0 ; i< 3000 ; i++) + fprintf(fp, "%s %d %s","Hello World!!!!", i, "\n"); + fclose(fp); + + rLED = LED_OFF; + printf("SD Card Writing Over\n"); + + // Open the index file + printf("Opening \"/sd/index.txt\"... "); + fflush(stdout); + FILE *f_index = fopen("/sd/index.txt", "r+"); + //FILE *f_index; + printf("%s\n", (!f_index ? "Fail :(" : "OK")); + int index_number; + + if(!f_index) + { + printf("No index file found, creating a new index file...\n"); + fflush(stdout); + f_index = fopen("/sd/index.txt", "w+"); + printf("Creat index file %s\n", (!f_index ? "Fail :(" : "OK")); + index_number = current_log_number; + fprintf(f_index, "%d", index_number); + + fclose(f_index); + } + + int index_number2 = 0; + printf("Index file found...\n"); + fflush(stdout); + f_index = fopen("/sd/index.txt", "r+"); + + printf("Open index.txt \n"); + printf("Openning %s\n", (!f_index ? "Fail :(" : "OK")); + + //Get current stream position + long pos = ftell(f_index); + + // rewind(f_index); + + err = fscanf(f_index, "%d", &index_number2); + printf("Scanning %s\n", (err < 0 ? "Fail :(" : "OK")); + printf("err = %d\n", err); + + //index_number2 = fgetc(f_index); + printf("index_number1 = %d \n", index_number2); + index_number2 += 1; + + fseek(f_index, pos, SEEK_SET); + printf("index_number2 = %d \n", index_number2); + err = fprintf(f_index, "%d\n", index_number2); + printf("Saving %s\n", (err < 0 ? "Fail :(" : "OK")); + + fflush(stdout); + err = fclose(f_index); + printf("Closing %s\n", (err < 0 ? "Fail :(" : "OK")); + current_log_number = index_number2; + + int j = 0; + /* + while (true) { + gLED = !gLED; + fp = fopen("/sd/myfile222.txt", "a"); + for(i=0 ; i< 300 ; i++) + fprintf(fp, "%s %d %d %s","Hello World!!!!", j, i, "\n"); + fclose(fp); + j++; + printf("write j %d\n", j); + // ThisThread::sleep_for(500ms); + } + */ + + printf("--- Mbed OS filesystem example 1 ---\n"); + rLED = LED_ON; + gLED = LED_OFF; + bLED = LED_OFF; + + // set NVIC priorities for GPIO to prevent priority inversion + printf("Init NVIC Priorities...\n"); + fflush(stdout); + NVIC_SetPriority(GPIO_P0_IRQn, 5); + NVIC_SetPriority(GPIO_P1_IRQn, 5); + NVIC_SetPriority(GPIO_P2_IRQn, 5); + NVIC_SetPriority(GPIO_P3_IRQn, 5); + NVIC_SetPriority(GPIO_P4_IRQn, 5); + NVIC_SetPriority(GPIO_P5_IRQn, 5); + NVIC_SetPriority(GPIO_P6_IRQn, 5); + + printf("Init MAX30001 callbacks, interrupts...\n"); + fflush(stdout); + max30001_InterruptB.disable_irq(); + max30001_Interrupt2B.disable_irq(); + max30001_InterruptB.mode(PullUp); + max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler); + max30001_Interrupt2B.mode(PullUp); + max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler); + max30001_InterruptB.enable_irq(); + max30001_Interrupt2B.enable_irq(); + MAX30001_AllowInterrupts(1); + max30001.max30001_sw_rst(); // Do a software reset of the MAX30001 + + PushButton pushButton(SW1); + Timer timer; + + max30001.max30001_INT_assignment(MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_enint_loc, en_eovf_loc, en_fstint_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_dcloffint_loc, en_bint_loc, en_bovf_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_NO_INT, // en_bover_loc, en_bundr_loc, en_bcgmon_loc, + MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, MAX30001::MAX30001_NO_INT, // en_pint_loc, en_povf_loc, en_pedge_loc, + MAX30001::MAX30001_INT_2B, MAX30001::MAX30001_INT_B, MAX30001::MAX30001_NO_INT, // en_lonint_loc, en_rrint_loc, en_samp_loc, + MAX30001::MAX30001_INT_ODNR, MAX30001::MAX30001_INT_ODNR); // intb_Type, int2b_Type) + + max30001.onDataAvailable(&StreamPacketUint32); + + Thread::wait(100); + Peripherals::setMAX30001(&max30001); + + max30001.max30001_reg_read(max30001.INFO, &id); + // read id twice because it needs to be read twice + max30001.max30001_reg_read(max30001.INFO, &id); + printf("INFO_01 = %x \n", id); + + partVersion = id >> 12; + partVersion = partVersion & 0x3; + + partVersion = 1; + // display header + if (partVersion == 0) + printf("Testing MAX30004|\n"); + if (partVersion == 1) { + printf("Testing MAX30001|\n"); + printf("Testing ECG, RtoR, BioZ, PACE|\n"); + } + + // initialize the RPC server + printf("Init RPC Server...\n"); + fflush(stdout); + RPC_init(); + // initialize the logging service + printf("Init LoggingService...\n"); + fflush(stdout); + + // start main loop + printf("Start main loop...\n"); + fflush(stdout); + + printf("partVersion = %d \n", partVersion); + // clear testing flags + testing_ecg_flags[TESTING_ECG_FLAG] = 0; + testing_ecg_flags[TESTING_BIOZ_FLAG] = 0; + testing_ecg_flags[TESTING_PACE_FLAG] = 0; + testing_ecg_flags[TESTING_RTOR_FLAG] = 0; + + // start streams + // testing_max30001 = 1; + if (partVersion == 1) + printf("Start Streaming ECG, RtoR, PACE, BIOZ, CAL enabled, " + "verifying streams...|\n"); + + if (partVersion == 3) + printf( + "Start Streaming ECG, RtoR, CAL enabled, verifying streams...|\n"); + // max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + // max30001.max30001_CAL_InitStart(0b1, 0b1, 0b1, 0b011, 0x7FF, 0b0); + // max30001.max30001_reg_read(max30001.CNFG_CAL, ®); + // printf("CNFG_CAL = %x \n", reg); + + //max30001.max30001_ECG_InitStart(0b1, 0b1, 0b1, 0b0, 0b10, 0b11, 0x1F, 0b00, + // 0b00, 0b0, 0b01); + //max30001.max30001_reg_read(max30001.CNFG_GEN, ®); + //printf("CNFG_GEN_1 = %x \n", reg); + + //if (partVersion == 1) + // max30001.max30001_PACE_InitStart(0b1, 0b0, 0b0, 0b1, 0x0, 0b0, 0b00, 0b0, + // 0b0); + //max30001.max30001_reg_read(max30001.CNFG_GEN, ®); + //printf("CNFG_GEN_2 = %x \n", reg); + + if (partVersion == 1) + max30001.max30001_BIOZ_InitStart(0b1, 0b0, 0b0, 0b00, + 0b00, 0b00, 7, 0b0, + 0b010, 0b0, 0b11, 0b00, 0b00, + 2, 0b0, 0b011, 0b0000, 0b0000); + /* + int MAX30001::max30001_BIOZ_InitStart( + uint8_t En_bioz, uint8_t Openp, uint8_t Openn, uint8_t Calp_sel, + uint8_t Caln_sel, uint8_t CG_mode, uint8_t B_fit, uint8_t Rate, + uint8_t Ahpf, uint8_t Ext_rbias, uint8_t Gain, uint8_t Dhpf, uint8_t Dlpf, + uint8_t Fcgen, uint8_t Cgmon, uint8_t Cgmag, uint8_t Phoff, uint8_t Inapow_mode) { + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + En_bioz: CNFG_GEN D[18], 1; 0 = Bioz disable, 1 = Bioz enable; + Openp: CNFG_BMUX D[21], 0; 0 = BIP is internally connected to BioZ channel, 1 = BIP is isolate from Bioz Channel + Openn: CNFG_BMUX D[20], 0; 0 = BIN is internally connected to BioZ channel, 1 = BIN is isolate from Bioz Channel + Calp_sel: CNFG_BMUX D[19:18]: 00; 00 = No calibration signal applied; 01 = input connected to VMID, 10 = connected to VCALP, 11= VCALN + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Caln_sel: CNFG_BMUX D[17:16]: 00; 00 = No calibration signal applied; 01 = input connected to VMID, 10 = connected to VCALP, 11= VCALN + CG_mode: CNFG_BMUX D[13:12]: 00; 00 = Unchopped sources with low pass filter + B_fit: MNGR_INT D[18:16]: 0x111; 000 to 111 = 1 to 8 Bioz FIFO interrupt Threshold + Rate: CNFG_BioZ D[23]: 0; BioZ Data Rate; when FMSTR = 00, 0=64sps, 1=32; FMSTR=01, 0=62.5, 1=31.25; FMSTR=10, 0=50,1=25; FMSTR=11, 0=49.95, 1=24.98 + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Ahpf: CNFG_BioZ D[22:20]: BioZ/PACE Channel Analog High-Pass Filter Cutoff Frequency and Bypass + 0b010; 000=125Hz, 001=300, 010=800, 011=2000, 100=3700, 101=7200 11x=Bypass AHPF + Ext_rbias: CNFG_BioZ D[19]: 0; 0 = Internal Bias Generator used; 1 = External Bias Generator used; + Gain: CNFG_BioZ D[17:16]:00; BioZ Channel Gain Setting, 00=10V/V; 01=20V/V; 10=40V/V; 11=80V/V + Dhpf: CNFG_BioZ D[15:14]:00; BioZ Channel Digital High-Pass Filter Cutoff Frequency 00=Bypass(DC); 01=0.05Hz; 1x=0.50Hz + Dlpf: CNFG_BioZ D[13:12]:00; BioZ Channel Digital Low-Pass Filter Cutoff Frequency: 00=Bypass, 01=4Hz, 10=8Hz, 11=16Hz + ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + Fcgen: CNFG_BioZ D[11:8]:2; BioZ Current Generator Modulation Frequency: 0000=4*fMSTR, 0001=2*fMSTR, 0010=fMSTR(40000Hz) + Cgmon: CNFG_BioZ D[7]:0; BioZ Current Generator Monitor: 0 = Current Generator Monistor disable; 1 = enable + Cgmag: CNFG_BioZ D[6:4]:0b001; BioZ Current Generator Magnitude: + 000=Off, 001 = 8uA, 010=16uA, 011=32uA, 100=48uA, 101=64uA; 110=80uA, 111=96uA + Phoff: CNFG_BioZ D[3:0]:0000; BioZ Current Generator Modulation Phase Offset: + BIOZ_FCGEN[3:0] = 0000: Phase Offset = BIOZ_PHOFF[3:2]*45.00° (0 to 135.00) + Inapow_mode: CNFG_BioZ D[18]:0; BioZ Channel Instrumentation Amplifier (INA) Power Mode, 0=BioZ INA is in low power mode, 1=in low noise mode + */ + + max30001.max30001_reg_read(max30001.CNFG_GEN, ®); + printf("CNFG_GEN = %x \n", reg); + + //max30001.max30001_RtoR_InitStart(0b1, 0b0011, 0b1111, 0b00, 0b0011, 0b000001, + // 0b00, 0b000, 0b01); + //max30001.max30001_reg_read(max30001.CNFG_GEN, ®); + printf("CNFG_GEN_4 = %x \n", reg); + + + max30001.max30001_Rbias_FMSTR_Init(0b01, 0b10, 0b1, 0b1, 0b00); + + max30001.max30001_reg_read(max30001.CNFG_ECG, ®); + printf("CNFG_ECG = %x \n", reg); + max30001.max30001_reg_read(max30001.CNFG_GEN, ®); + printf("CNFG_GEN_5 = %x \n", reg); + max30001.max30001_reg_read(max30001.MNGR_INT, ®); + printf("MNGR_INT = %x \n", reg); + max30001.max30001_reg_read(max30001.CNFG_EMUX, ®); + printf("CNFG_EMUX = %x \n", reg); + max30001.max30001_reg_read(max30001.CNFG_BIOZ, ®); + printf("CNFG_BIOZ = %x \n", reg); + + max30001.max30001_synch(); + + timer.start(); + + wait_us(20); + + timer.stop(); + + rLED = LED_OFF; + bLED = LED_OFF; + gLED = LED_OFF; + + sprintf(BIOZ_file_name, "%s_%d", BIOZ_file_name, current_log_number); + strcat(BIOZ_file_name, ".txt"); + printf("%s\n", BIOZ_file_name); + + while (true) { + //wait_ms(50); + //printf("%% \n"); + rLED = LED_OFF; + gLED = LED_ON; + + if ( Button == 0 ) + event_tag = 1; + else + event_tag = 0; + + //printf("ECG_Data_Size = %d \n", ECG_Data_Size); + fp = fopen("/sd/ECG.txt", "a"); + // fp = fopen(ECG_file_name, "a"); + if( ECG_Data_Size > 60 ) + { + + for(i=0 ; i<ECG_Data_Size ; i++) + { + fprintf(fp, "%d %d %x %s",ECG_Data_Size, i, ECG_Data[i], "\n"); + + // printf("%s %x %s","ECG", ECG_Data[i], "\n"); + } + ECG_Data_Size = 0; + // printf("Save ECG \n"); + rLED = !rLED; + + } + fclose(fp); + + fp = fopen(BIOZ_file_name, "a"); + // fp = fopen("/sd/BIOZ.txt", "a"); + if( BIOZ_Data_Size > 30 ) + { + for(i=0 ; i<BIOZ_Data_Size ; i++) + { + fprintf(fp, "%d %d %x %d %s",BIOZ_Data_Size, i, BIOZ_Data[i], event_tag, "\n"); + + } + BIOZ_Data_Size = 0; + printf("Save BIOZ \n"); + rLED = !rLED; + outLED = !outLED; + } + fclose(fp); + + } +} +
diff -r 000000000000 -r a15c76864d7d max32630fthr.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/max32630fthr.lib Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/teams/MaximIntegrated/code/max32630fthr/#8f6e6a800f2f
diff -r 000000000000 -r a15c76864d7d mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#569159b784f70feaa32ce226aaca896fb83452f7
diff -r 000000000000 -r a15c76864d7d sd-driver.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver.lib Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/sd-driver/#f4ab55df7768cfcb049b522bebd30218ee729c81
diff -r 000000000000 -r a15c76864d7d sd-driver/.travis.yml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/.travis.yml Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,24 @@ +script: + # Check that examples compile + - sed -n '/``` cpp/,${/```$/q;/```/d;p}' README.md > main.cpp && + PYTHONPATH=mbed-os python mbed-os/tools/make.py -t GCC_ARM -m K64F + --source=. --build=BUILD/K64F/GCC_ARM -j0 && + rm main.cpp + + # Check that tests compile + - rm -rf BUILD && PYTHONPATH=mbed-os python mbed-os/tools/test.py + -t GCC_ARM -m K64F --source=. --build=BUILD/TESTS/K64F/GCC_ARM -j0 + -n tests* + +python: + - "2.7" + +install: + # Get arm-none-eabi-gcc + - sudo add-apt-repository -y ppa:terry.guo/gcc-arm-embedded + - sudo apt-get update -qq + - sudo apt-get install -qq gcc-arm-none-eabi --force-yes + # Get dependencies + - git clone https://github.com/armmbed/mbed-os.git + # Install python dependencies + - pip install --user -r mbed-os/requirements.txt
diff -r 000000000000 -r a15c76864d7d sd-driver/LICENSE --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/LICENSE Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,165 @@ +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities +that control, are controlled by, or are under common control with that entity. +For the purposes of this definition, "control" means (i) the power, direct or +indirect, to cause the direction or management of such entity, whether by +contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising +permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including +but not limited to software source code, documentation source, and configuration +files. + +"Object" form shall mean any form resulting from mechanical transformation or +translation of a Source form, including but not limited to compiled object code, +generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made +available under the License, as indicated by a copyright notice that is included +in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that +is based on (or derived from) the Work and for which the editorial revisions, +annotations, elaborations, or other modifications represent, as a whole, an +original work of authorship. For the purposes of this License, Derivative Works +shall not include works that remain separable from, or merely link (or bind by +name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version +of the Work and any modifications or additions to that Work or Derivative Works +thereof, that is intentionally submitted to Licensor for inclusion in the Work +by the copyright owner or by an individual or Legal Entity authorized to submit +on behalf of the copyright owner. For the purposes of this definition, +"submitted" means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, and +issue tracking systems that are managed by, or on behalf of, the Licensor for +the purpose of discussing and improving the Work, but excluding communication +that is conspicuously marked or otherwise designated in writing by the copyright +owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf +of whom a Contribution has been received by Licensor and subsequently +incorporated within the Work. + +2. Grant of Copyright License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the Work and such +Derivative Works in Source or Object form. + +3. Grant of Patent License. + +Subject to the terms and conditions of this License, each Contributor hereby +grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, +irrevocable (except as stated in this section) patent license to make, have +made, use, offer to sell, sell, import, and otherwise transfer the Work, where +such license applies only to those patent claims licensable by such Contributor +that are necessarily infringed by their Contribution(s) alone or by combination +of their Contribution(s) with the Work to which such Contribution(s) was +submitted. If You institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work or a +Contribution incorporated within the Work constitutes direct or contributory +patent infringement, then any patent licenses granted to You under this License +for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. + +You may reproduce and distribute copies of the Work or Derivative Works thereof +in any medium, with or without modifications, and in Source or Object form, +provided that You meet the following conditions: + +You must give any other recipients of the Work or Derivative Works a copy of +this License; and +You must cause any modified files to carry prominent notices stating that You +changed the files; and +You must retain, in the Source form of any Derivative Works that You distribute, +all copyright, patent, trademark, and attribution notices from the Source form +of the Work, excluding those notices that do not pertain to any part of the +Derivative Works; and +If the Work includes a "NOTICE" text file as part of its distribution, then any +Derivative Works that You distribute must include a readable copy of the +attribution notices contained within such NOTICE file, excluding those notices +that do not pertain to any part of the Derivative Works, in at least one of the +following places: within a NOTICE text file distributed as part of the +Derivative Works; within the Source form or documentation, if provided along +with the Derivative Works; or, within a display generated by the Derivative +Works, if and wherever such third-party notices normally appear. The contents of +the NOTICE file are for informational purposes only and do not modify the +License. You may add Your own attribution notices within Derivative Works that +You distribute, alongside or as an addendum to the NOTICE text from the Work, +provided that such additional attribution notices cannot be construed as +modifying the License. +You may add Your own copyright statement to Your modifications and may provide +additional or different license terms and conditions for use, reproduction, or +distribution of Your modifications, or for any such Derivative Works as a whole, +provided Your use, reproduction, and distribution of the Work otherwise complies +with the conditions stated in this License. + +5. Submission of Contributions. + +Unless You explicitly state otherwise, any Contribution intentionally submitted +for inclusion in the Work by You to the Licensor shall be under the terms and +conditions of this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify the terms of +any separate license agreement you may have executed with Licensor regarding +such Contributions. + +6. Trademarks. + +This License does not grant permission to use the trade names, trademarks, +service marks, or product names of the Licensor, except as required for +reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. + +Unless required by applicable law or agreed to in writing, Licensor provides the +Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, +including, without limitation, any warranties or conditions of TITLE, +NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are +solely responsible for determining the appropriateness of using or +redistributing the Work and assume any risks associated with Your exercise of +permissions under this License. + +8. Limitation of Liability. + +In no event and under no legal theory, whether in tort (including negligence), +contract, or otherwise, unless required by applicable law (such as deliberate +and grossly negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, incidental, +or consequential damages of any character arising as a result of this License or +out of the use or inability to use the Work (including but not limited to +damages for loss of goodwill, work stoppage, computer failure or malfunction, or +any and all other commercial damages or losses), even if such Contributor has +been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. + +While redistributing the Work or Derivative Works thereof, You may choose to +offer, and charge a fee for, acceptance of support, warranty, indemnity, or +other liability obligations and/or rights consistent with this License. However, +in accepting such obligations, You may act only on Your own behalf and on Your +sole responsibility, not on behalf of any other Contributor, and only if You +agree to indemnify, defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason of your +accepting any such warranty or additional liability.
diff -r 000000000000 -r a15c76864d7d sd-driver/README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/README.md Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,612 @@ +# mbed OS SDCard Driver (sd-driver) for FAT32 Filesystem Support + + +Simon Hughes + +20170329 + +Version 0.1.2 + + +# Executive Summary + +The purpose of this document is to describe how to use the mbed OS SDCard +driver (sd-driver) so applications can read/write +data to flash storage cards using the standard POSIX File API +programming interface. The sd-driver uses the SDCard SPI-mode of operation +which is a subset of possible SDCard functionality. + +This repository contains the mbed-os SDCard driver for generic SPI +SDCard support and other resources, as outlined below: + +- `SDBlockDevice.h` and `SDBlockDevice.cpp`. This is the SDCard driver module presenting + a Block Device API (derived from BlockDevice) to the underlying SDCard. +- POSIX File API test cases for testing the FAT32 filesystem on SDCard. + - basic.cpp, a basic set of functional test cases. + - fopen.cpp, more functional tests reading/writing greater volumes of data to SDCard, for example. +- `mbed_lib.json` mbed-os application configuration file with SPI pin configurations for the CI shield and overrides for specific targets. + This file allows the SPI pins to be specified for the target without having to edit the implementation files. +- This README which includes [Summary of POSIX File API Documentation](#summary-posix-api-documentation) + including detailed instruction on how to use the FAT filesystem and SDBlockDevice driver. + +The SDCard driver is maintained in this repository as a component separate from the main mbed OS repository. +Hence the 2 repositories (mbed-os and sd-driver) have to be used together +to deliver the FAT32 Filesystem/SDCard support. This document explains how to do this. + + +# Introduction + +### Overview + +The scope of this document is to describe how applications use the FAT filesystem and sd-driver +components to persistently store data on SDCards. The document is intended to help developers adopt the +mbed OS POSIX File API support, and in particular to help explain: + +- How the software components work together to deliver the storage functionality. +- How to work with the sd-driver and mbed OS to build the examples. The example code can easily + be copied into your new application code. +- How to work with the CI Test Shield, which adds an SDCard slot to those targets that do not have already have one. +- How to run the POSIX File API mbed Greentea test cases, which provide further example code of how to use + the POSIX File API. + +Section 1 provides an Executive Summary, describing the purpose of the sd-driver, the supporting +software, examples, test cases and documentation. + +Section 2 provides an an overview of the material covered including descriptions of the major sections. + +Section 3 provides an overview of the mbed OS filesystem software components, +including the inter-relationships between the application, POSIX file API, the standard c-library, +the mbed OS filesystem and the SDCard driver (sd-driver). + +Section 4 describes how to build and run an example application for reading +and writing data to an SDCard using the POSIX File API. The example begins by describing +the procedure for building and testing on the K64F target. The final sub-sections +describe how to use the test shield to add an SDCard slot to any mbed target, +and hence enable the persistent storage of data on any supported target. + +Section 5 describes an example application which uses the raw +BlockDevice API to read and write data to the SDCard. + +Section 6 describes how to build and run the SDCard POSIX File API mbed Greentea test cases. +There are a number of functional test cases demonstrating how to use the +mbed OS POSIX File API. + +Section 7 describes the POSIX File API and provides links to useful API documentation web pages. + + +### Known mbed-os and sd-driver Compatible Versions + +The following versions of the mbed-os and sd-driver repositories are known to work together: + +- {mbed-os, sd-driver} = {mbed-os-5.4.0-rc2, sd-driver-0.0.1-mbed-os-5.4.0-rc2}. + `K64F`, `NUCLEO_F429ZI` and `UBLOX_EVK_ODIN_W2` fopen and basic filesystem tests working. +- {mbed-os, sd-driver} = {mbed-os-5.4.0, sd-driver-0.0.2-mbed-os-5.4.0}. + `K64F`, `NUCLEO_F429ZI` and `UBLOX_EVK_ODIN_W2` fopen and basic filesystem tests working. +- {mbed-os, sd-driver} = {mbed-os-5.4.1, sd-driver-0.0.3-mbed-os-5.4.1}. +- {mbed-os, sd-driver} = {mbed-os-5.5.1, sd-driver-0.1.0-mbed-os-5.5.1}. +- {mbed-os, sd-driver} = {mbed-os-5.5.4, sd-driver-0.1.1-mbed-os-5.5.4}. +- {mbed-os, sd-driver} = {mbed-os-5.6.1, sd-driver-0.1.2-mbed-os-5.6.1}. + +To find the latest compatible versions, use the following command to see the messages attached to the tags +in the sd-driver repository: + + ex_app7/$ cd sd-driver + ex_app7/sd-driver$ git tag -n + sd-driver-0.0.1-mbed-os-5.3.4 Version compatible with mbed-os-5.3.4, and private_mbedos_filesystems-0.0.1-mbed-os-5.3.4. + sd-driver-0.0.2-mbed-os-5.4.0 Updated README.md to include worked exmaples and restructuring of information. + sd-driver-0.0.3-mbed-os-5.4.1 Version compatible with mbed-os-5.4.1. + sd-driver-0.1.1-mbed-os-5.5.4 Version compatible with mbed-os-5.5.4 + sd-driver-0.1.2-mbed-os-5.6.1 Version compatible with mbed-os-5.6.1 + + +### Known Issues With This Document + +There are no known issues with this document. + + +# Overview of mbed OS Filesystem Software Component Stack + + + ------------------------ + | | + | Application | // This application uses the POSIX File API + | | // to read/write data to persistent storage backends. + ------------------------ + + ------------------------ // POSIX File API (ISO). + + ------------------------ + | | + | libc | // The standard c library implementation + | | // e.g. newlib. + ------------------------ + + ------------------------ // sys_xxx equivalent API. + + ------------------------ + | | + | mbed_retarget.cpp | // Target specific mapping layer. + | | + ------------------------ + + ------------------------ // Filesystem Upper Edge API. + + ------------------------ + | | + | File System | // File system wrappers and implementation. + | | + ------------------------ + + ------------------------ // FS Lower Edge API (Block Store Interface). + + ------------------------ + | Block API | + | Device Driver | // The SDCard driver, for example. + | e.g. sd-driver | + ------------------------ + + ------------------------ // SPI.h interface. + + ------------------------ + | | + | SPI | // SPI subsystem (C++ classes and C-HAL implementation). + | | + ------------------------ + + Figure 1. mbedOS generic architecture of filesystem software stack. + +The figure above shows the mbed OS software component stack used for data +storage on SDCard: + +- At the top level is the application component which uses the standard POSIX File API + to read and write application data to persistent storage. +- The newlib standard library (libc) stdio.h interface (POSIX File API) + implementation is used as it's optimised for resource limited embedded systems. +- mbed_retarget.cpp implements the libc back-end file OS handlers and maps them + to the FileSystem. +- The File System code (hosted in mbed-os) is composed of 2 parts: + - The mbed OS file system wrapper classes (e.g. FileSystem, File, FileBase classes) + which are used to present a consistent API to the retarget module for different + (third-party) file system implementations. + - The FAT filesystem implementation code. + The [FATFS: Generic FAT File System Module](http://elm-chan.org/fsw/ff/00index_e.html) + (ChanFS) has been integrated within mbed-os. +- The Block API Device Driver. The SDCard driver is an example of a persistent storage driver. + It's maintained as a separate component from the mbed OS repository (in this repository). +- The SPI module provides the mbed OS generic SPI API. This functionality is maintained in + mbed OS. + + +# SDCard POSIX File API Example App for Reading/Writing Data + +Refer to [SD driver Example](https://github.com/ARMmbed/mbed-os-example-sd-driver) + + +### <a name="testing-with-an-sdcard-on-target-xyx"></a> Testing with an SDCard on Target XYZ + +The standard way to test is with the mbed CI Test Shield plugged into the +target board. This pin mapping for this configuration is parameterised in +the `mbed_lib.json` file. + +The following is an example of the `mbed_lib.json` file available in the repository: + + { + "config": { + "SPI_CS": "D10", + "SPI_MOSI": "D11", + "SPI_MISO": "D12", + "SPI_CLK": "D13", + "DEVICE_SPI": 1, + "FSFAT_SDCARD_INSTALLED": 1 + }, + "target_overrides": { + "DISCO_F051R8": { + "SPI_MOSI": "SPI_MOSI", + "SPI_MISO": "SPI_MISO", + "SPI_CLK": "SPI_SCK", + "SPI_CS": "SPI_CS" + }, + "KL46Z": { + "SPI_MOSI": "PTD6", + "SPI_MISO": "PTD7", + "SPI_CLK": "PTD5", + "SPI_CS": "PTD4" + }, + "K64F": { + "SPI_MOSI": "PTE3", + "SPI_MISO": "PTE1", + "SPI_CLK": "PTE2", + "SPI_CS": "PTE4" + } + } + +Note the following things about the `mbed_lib.json` file: + +- The `mbed_lib.json` file is used to define target specific symbols for the SPI pins connecting the SDCard slot to the target MCU: + - "SPI\_CS". This is the Chip Select line. + - "SPI\_MOSI". This is the Master Out Slave In data line. + - "SPI\_MISO". This is the Master In Slave Out data line. + - "SPI\_CLK". This is the serial Clock line. +- The default configuration defined in the "config" section is for the standard Arduino header pin mappings for the SPI bus. + The "config" section defines a dictionary mapping functional names to target board Arduino header pins: + - "SPI\_CS": "D10". This causes the MBED\_CONF\_APP\_SPI\_CS symbol to be defined in mbed\_config.h as D10, which is used in the filesystem test implementation. + D10 is defined in the target specific PinNames.h file. + - "SPI\_MOSI": "D11". This causes the MBED\_CONF\_APP\_SPI\_MOSI symbol to be defined in mbed\_config.h. + - "SPI\_MISO": "D12". This causes the MBED\_CONF\_APP\_SPI\_MISO symbol to be defined in mbed\_config.h. + - "SPI\_CLK": "D13". This causes the MBED\_CONF\_APP\_SPI\_CLK symbol to be defined in mbed\_config.h. +- The `"target_overrides"` section is used to override the "SPI\_xxx" symbols for specific target boards, which may have an SDCard slot, for example. + This is the case for the K64F, where the "SPI\_xxx" are mapped to the pin names for the on-board SDCard. + + ``` + "K64F": { + "SPI_MOSI": "PTE3", + "SPI_MISO": "PTE1", + "SPI_CLK": "PTE2", + "SPI_CS": "PTE4" + } + ``` +- Thus, in the absence of any target specific definitions in the `"target_overrides"` section, all boards will default to + using the Arduino header configuration. For those platforms with a `"target_overrides"` section then this configuration + will be used in preference. +- Hence in the case that you want to test a platform with an SDCard inserted into a + fitted CI test shield (rather than the on-board SDCard slot) + and there is a `"target_overrides"` section present in the `mbed_lib.json` file, you must then delete the `"target_overrides"` + section before building. This will result in the default configuration being used (suitable for the CI + Test Shield). +- Note when inserting the v1.0.0 CI Test Shield into the Arduino header of the target platform, the shield pins D0 and + D1 should be bent to be parallel to the shield PCB so they are not inserted into the Arduino header. This is because + some boards use the same UART on DAPLINK and D0/D1, which means the serial debug channel breaks and hence the mbed greentea + test suite will not work correctly. This is mainly on older ST boards and should not be a problem on + `K64F`, `NUCLEO_F429ZI` and `UBLOX_EVK_ODIN_W2`. Note also that the v2.0.0 CI Test Shield doesn't suffer from this + problem and the pins don't need to be bent. +- When inserting the SDCard into the card slot on the CI test shield, make sure the card is fully inserted. + On insertion, there should be a small clicking sound when the card registers, and the back edge of the card + should protrude no more than ~1mm over the edge of the CI test shield PCB. If the SDCard fails to register, + try gently pushing the metal flexible strip in the shape of a spade at the top edge of the SDCard metal slot + casing with a pair of tweezers, bending it a little to lower it into the slot casing. This helps with the + insertion mechanism. + +### Wiring instructions for target NUCLEO_F429ZI with CI Test Shield +![alt text](docs/pics/NUCLEO_F429ZI_wiring_with_ci_test_shield.png "unseen title text") + +**Figure 3. The figure shows how to connect the NUCLEO_F429ZI platform with the CI shield.** + +The above figure shows how to connect the NUCLEO_F429ZI with the v1.0.0 CI test shield. Note: + +- To get the SD Card to work with this platform the CI test shield cannot be connected directly to this board, instead follow the instructions above. +- Any SD-card adapter will work as long as you connect all the relevant pins (MOSI, MISO, SCLK, CS, 3.3V and GND) as illustrated in figure 3. +- The SDCard is fully inserted into the slot and overhangs the PCB by ~1mm. + +# SDBlockDevice Example Application + +The following sample code illustrates how to use the sd-driver Block Device API: + +``` cpp +#include "mbed.h" +#include "SDBlockDevice.h" + +// Instantiate the SDBlockDevice by specifying the SPI pins connected to the SDCard +// socket. The PINS are: +// MOSI (Master Out Slave In) +// MISO (Master In Slave Out) +// SCLK (Serial Clock) +// CS (Chip Select) +SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +uint8_t block[512] = "Hello World!\n"; + +int main() +{ + // call the SDBlockDevice instance initialisation method. + if ( 0 != sd.init()) { + printf("Init failed \n"); + return -1; + } + printf("sd size: %llu\n", sd.size()); + printf("sd read size: %llu\n", sd.get_read_size()); + printf("sd program size: %llu\n", sd.get_program_size()); + printf("sd erase size: %llu\n", sd.get_erase_size()); + + // set the frequency + if ( 0 != sd.frequency(5000000)) { + printf("Error setting frequency \n"); + } + + if ( 0 != sd.erase(0, sd.get_erase_size())) { + printf("Error Erasing block \n"); + } + + // Write some the data block to the device + if ( 0 == sd.program(block, 0, 512)) { + // read the data block from the device + if ( 0 == sd.read(block, 0, 512)) { + // print the contents of the block + printf("%s", block); + } + } + + // call the SDBlockDevice instance de-initialisation method. + sd.deinit(); +} +``` + +# SDCard POSIX File API mbed Greentea Test Cases + +This section describes how to build and run the POSIX file API test cases. +The following steps are covered: + +- [Create the FAT/SDCard Application Project](#create-fat-sdcard-application-project). + This section describes how to git clone the mbed OS and sd-driver repositories containing the + code and test cases of interest. +- [Build the mbed OS Test Cases](#build-the-mbedos-test-cases). This section + describes how to build the mbed OS test cases. +- [Insert a microSD Card Into the K64F for Greentea Testing](#greentea-insert-sdcard-into-k64f).This section + describes how to format (if required) a microSD card prior to running the tests. +- [Run the POSIX File Test Case](#run-the-posix-file-test-cases).This section + describes how to run the POSIX file test cases. + + +### <a name="create-fat-sdcard-application-project"></a> Create the FAT/SDCard Application Project + +This section describes how to create an application project combining the mbed-os and +sd-driver repositories into a single project. +In summary the following steps will be covered in this section: + +- A top level application project directory is created. The directory name is ex_app1. +- In the ex_app1 directory, the mbed-os repository is cloned. +- In the ex_app1 directory at the same level as the mbed-os directory, the sd-driver repository is cloned. +- The `mbed_lib.json` file is copied from the `sd-driver/config/mbed_lib.json` to the ex_app1 directory. + +First create the top level application directory ex_app1 and move into it: + + shell:/d/demo_area$ mkdir ex_app1 + shell:/d/demo_area$ pushd ex_app1 + +Next, get a clone of public mbed OS repository in the following way: + + shell:/d/demo_area/ex_app1$ git clone git@github.com:/armmbed/mbed-os + <trace removed> + shell:/d/demo_area/ex_app1$ + +Next, get a clone of the sd-driver repository: + + shell:/d/demo_area/ex_app1$ git clone git@github.com:/armmbed/sd-driver + <trace removed> + shell:/d/demo_area/ex_app1$ + +Note: The `mbed_lib.json` file specifies the SPI bus pin configuration for different targets, +and is discussed in the [Testing with an SDCard on Target XYZ](#testing-with-an-sdcard-on-target-xyx) section. + +### <a name="build-the-mbedos-test-cases"></a> Build the mbed OS Test Cases + +Build the test cases for the K64F target using the following command: + + shell:/d/demo_area/ex_app1$ mbed -v test --compile -t GCC_ARM -m K64F + <trace removed> + shell:/d/demo_area/ex_app1$ + +The build trace is quite extensive but on a successful build you should see the following output at the end of the log: + + Build successes: + * K64F::GCC_ARM::MBED-BUILD + * K64F::GCC_ARM::MBED-OS-FEATURES-FEATURE_LWIP-TESTS-MBEDMICRO-NET-CONNECTIVITY + <trace removed> + * K64F::GCC_ARM::MBED-OS-FEATURES-TESTS-FILESYSTEM-FAT_FILE_SYSTEM + * K64F::GCC_ARM::MBED-OS-FEATURES-TESTS-FILESYSTEM-HEAP_BLOCK_DEVICE + * K64F::GCC_ARM::MBED-OS-FEATURES-TESTS-FILESYSTEM-UTIL_BLOCK_DEVICE + <trace removed> + * K64F::GCC_ARM::SD-DRIVER-TESTS-BLOCK_DEVICE-BASIC + * K64F::GCC_ARM::SD-DRIVER-TESTS-FILESYSTEM-BASIC + * K64F::GCC_ARM::SD-DRIVER-TESTS-FILESYSTEM-DIRS + * K64F::GCC_ARM::SD-DRIVER-TESTS-FILESYSTEM-FILES + * K64F::GCC_ARM::SD-DRIVER-TESTS-FILESYSTEM-FOPEN + * K64F::GCC_ARM::SD-DRIVER-TESTS-FILESYSTEM-PARALLEL + * K64F::GCC_ARM::SD-DRIVER-TESTS-FILESYSTEM-SEEK + + Build skips: + * K64F::GCC_ARM::MBED-OS-FEATURES-FEATURE_LWIP-TESTS-MBEDMICRO-NET-TCP_PACKET_PRESSURE + <trace removed> + + +Notice the following tests in the sd-driver tree are listed above: + +- `SD-DRIVER-TESTS-BLOCK_DEVICE-BASIC` +- `SD-DRIVER-TESTS-FILESYSTEM-BASIC` +- `SD-DRIVER-TESTS-FILESYSTEM-DIRS` +- `SD-DRIVER-TESTS-FILESYSTEM-FILES` +- `SD-DRIVER-TESTS-FILESYSTEM-FOPEN` +- `SD-DRIVER-TESTS-FILESYSTEM-PARALLEL` +- `SD-DRIVER-TESTS-FILESYSTEM-SEEK` + +The FAT32/SDCard test cases are at following locations in the source code tree: + + /d/demo_area/ex_app1/sd-driver/TESTS/filesystem/basic/basic.cpp + /d/demo_area/ex_app1/sd-driver/TESTS/filesystem/fopen/fopen.cpp + /d/demo_area/ex_app1/sd-driver/TESTS/block_device/basic/basic.cpp + /d/demo_area/ex_app1/sd-driver/TESTS/filesystem/dirs/main.cpp + /d/demo_area/ex_app1/sd-driver/TESTS/filesystem/files/main.cpp + /d/demo_area/ex_app1/sd-driver/TESTS/filesystem/parallel/main.cpp + /d/demo_area/ex_app1/sd-driver/TESTS/filesystem/seek/main.cpp + +#### <a name="settting-repos-to-compatible-versions"></a> Setting mbed-os/sd-driver Repositories To Compatible Versions + +The sd-driver master HEAD and the mbed-os master HEAD should be compatible +with one another and therefore no specific tagged versions need to be checked out. +However, in the case that you experience problems building, checkout out the compatible +tagged version of each repository, as shown below: + + shell:/d/demo_area/ex_app1$ pushd mbed-os + shell:/d/demo_area/ex_app1$ git checkout tags/mbed-os-5.4.0 + shell:/d/demo_area/ex_app1$ popd + shell:/d/demo_area/ex_app1$ pushd sd-driver + shell:/d/demo_area/ex_app1$ git checkout tags/sd-driver-0.0.2-mbed-os-5.4.0 + shell:/d/demo_area/ex_app1$ popd + +In the above: + +- `mbed-os-5.4.0` should be replaced with the latest mbed-os release tag. +- For an mbed-os release tag `mbed-os-x.y.z`, use the equivalent sd-driver tag `sd-driver-a.b.c-mbed-os-x.y.z` + where `a.b.c` is the latest version code for the `mbed-os-x.y.z` tag. + +### <a name="greentea-insert-sdcard-into-k64f"></a> Insert SDCard into K64F for Greentea Testing + +See the previous section for [Insert SDCard into K64F](#insert-sdcard-into-k64f) for details. + + +### <a name="run-the-posix-file-test-cases"></a> Run the POSIX File Test Case + +To setup for running the test cases, connect the K64F development board to your +PC using a suitable USB cable. + +All tests can be run using the following command: + + shell:/d/demo_area/ex_app1$ mbedgt -VS + <trace removed> + +However, it's possible to run a particular test case using the following form of the mbedgt command: + + shell:/d/demo_area/ex_app1$ mbedgt -VS --test-by-names=<test-name> + +The names of the tests can be listed using: + + shell:/d/demo_area/ex_app1$ mbedgt -VS --list + +For example, to run the basic test use: + + shell:/d/demo_area/ex_app1$ mbedgt -VS --test-by-names=sd-driver-tests-filesystem-basic + +To run the fopen test use: + + shell:/d/demo_area/ex_app1$ mbedgt -VS --test-by-names=sd-driver-tests-filesystem-fopen + +On a successful run, results similar to the following will be shown: + + mbedgt: test suite report: + +--------------+---------------+-------------------------------------------+--------+--------------------+-------------+ + | target | platform_name | test suite | result | elapsed_time (sec) | copy_method | + +--------------+---------------+-------------------------------------------+--------+--------------------+-------------+ + | K64F-GCC_ARM | K64F | sd-driver-features-tests-filesystem-fopen | OK | 151.46 | shell | + +--------------+---------------+-------------------------------------------+--------+--------------------+-------------+ + mbedgt: test suite results: 1 OK + mbedgt: test case report: + +--------------+---------------+------------------------------------+----------------------------------------------------------------------------------------+--------+--------+--------+--------------------+ + | target | platform_name | test suite | test case | passed | failed | result | elapsed_time (sec) | + +--------------+---------------+------------------------------------+----------------------------------------------------------------------------------------+--------+--------+--------+--------------------+ + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_01: fopen()/fwrite()/fclose() directories/file in multi-dir filepath. | 1 | 0 | OK | 7.57 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_02: fopen(r) pre-existing file try to write it. | 1 | 0 | OK | 0.2 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_03: fopen(w+) pre-existing file try to write it. | 1 | 0 | OK | 0.41 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_04: fopen() with a filename exceeding the maximum length. | 1 | 0 | OK | 0.11 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_06: fopen() with bad filenames (minimal). | 1 | 0 | OK | 0.1 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_07: fopen()/errno handling. | 1 | 0 | OK | 0.07 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_08: ferror()/clearerr()/errno handling. | 1 | 0 | OK | 0.1 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_09: ftell() handling. | 1 | 0 | OK | 0.17 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_10: remove() test. | 1 | 0 | OK | 1.28 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_11: rename(). | 1 | 0 | OK | 2.3 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_12: opendir(), readdir(), closedir() test. | 1 | 0 | OK | 3.57 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_13: mkdir() test. | 1 | 0 | OK | 1.21 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_14: stat() test. | 1 | 0 | OK | 1.47 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_15: format() test. | 1 | 0 | OK | 26.12 | + | K64F-GCC_ARM | K64F | sd-driver-tests-filesystem-fopen | FSFAT_FOPEN_TEST_16: write/check n x 25kB data files. | 1 | 0 | OK | 87.11 | + +--------------+---------------+------------------------------------+----------------------------------------------------------------------------------------+--------+--------+--------+--------------------+ + mbedgt: test case results: 15 OK + mbedgt: completed in 152.35 sec + + +# <a name="summary-posix-api-documentation"></a> Summary of POSIX File API Documentation + +### POSIX File API + +mbed OS supports a subset of the POSIX File API, as outlined below: + +- [clearerr()](https://linux.die.net/man/3/clearerr). + - STATUS: Basic testing implemented. Working. +- [fclose()](https://linux.die.net/man/3/fclose). + - STATUS: Basic testing implemented. Working. +- [ferror()](https://linux.die.net/man/3/clearerr). + - STATUS: Basic testing implemented. + - STATUS: GCC_ARM: Working. + - STATUS: ARMCC: ARMCC has problem with ferror(filep) where filep is NULL. Appears to work for non-NULL pointer. +- [fgetc()](https://linux.die.net/man/3/fgets). + - STATUS: Basic testing implemented. Working. +- [fgets()](https://linux.die.net/man/3/fgets). + - STATUS: Basic testing implemented. Working. +- [fputc()](https://linux.die.net/man/3/fputs). + - STATUS: Unknown. +- [fputs()](https://linux.die.net/man/3/fputs). + - STATUS: Basic testing implemented. Working. +- [fprintf()](https://linux.die.net/man/3/fprintf). + - STATUS: Basic testing implemented. Working. +- [fopen()](https://linux.die.net/man/3/fopen). + - STATUS: Basic testing implemented. Working. +- [freopen()](https://linux.die.net/man/3/fopen). + - STATUS: This is not tested. +- [fread()](https://linux.die.net/man/3/fread). + - STATUS: Basic testing implemented. Working. + - STATUS: n x 25kB stress test working. +- [ftell()](https://linux.die.net/man/3/ftell). + - STATUS: Basic testing implemented. Working. +- [fwrite()](https://linux.die.net/man/3/fwrite). + - STATUS: Basic testing implemented. Working. + - STATUS: n x 25kB stress test working. +- [fseek()](https://linux.die.net/man/3/fseek) + - STATUS: Basic testing implemented. Working. +- [getc()](https://linux.die.net/man/3/fgets). + - STATUS: Basic testing implemented. Working. +- [gets()](https://linux.die.net/man/3/fgets). + - STATUS: Unknown. +- [putc()](https://linux.die.net/man/3/fputs). + - STATUS: Unknown. +- [puts()](https://linux.die.net/man/3/fputs). + - STATUS: Unknown. +- [remove()](https://linux.die.net/man/3/remove) + - STATUS: Basic testing implemented. Working. +- [rewind()](https://linux.die.net/man/3/rewind). + - STATUS: Basic testing implemented. Working. +- [stat()](https://linux.die.net/man/2/stat) + - STATUS: Implemented. Working. + - STATUS: Not supported by ARMCC/IAR libc. +- [tmpfile()](https://linux.die.net/man/3/tmpfile). + - STATUS: Not implemented. +- [tmpnam()](https://linux.die.net/man/3/tmpnam). + - STATUS: Not implemented. + +Supported directory related operations are as follows: + +- [closedir()](https://linux.die.net/man/3/closedir). + - STATUS: Implemented. Working. +- [mkdir()](https://linux.die.net/man/3/mkdir). + - STATUS: Basic testing implemented. Working. +- [opendir()](https://linux.die.net/man/3/opendir). + - STATUS: Implemented. Working. +- [readdir()](https://linux.die.net/man/3/readdir). + - STATUS: Implemented. Working. +- [remove()](https://linux.die.net/man/3/remove). + - STATUS: Basic testing implemented. Working. +- [rename()](https://linux.die.net/man/3/rename). + - STATUS: Implemented. Not tested. +- [rewinddir()](https://linux.die.net/man/3/rewinddir). + - STATUS: Implemented. Found not to work. Test case not present in repo. +- [seekdir()](https://linux.die.net/man/3/seekdir). + - STATUS: Implemented. Found not to work. Test case not present in repo. +- [telldir()](https://linux.die.net/man/3/telldir). + - STATUS: Implemented. Found not to work. Test case not present in repo. + +### errno + +Basic errno reporting is supported, tested and known to be working. + + +# Related Projects Resources + +The following are related mbed storage projects and useful resources: + +- The [mbed-os](https://github.com/ARMmbed/mbed-os) main repository. +- The [mbed-os-example-fat-filesystem](https://github.com/ARMmbed/mbed-os-example-fat-filesystem) repository. + This is an example project for the mbed OS FAT filesystem. +- The [spiflash-driver](https://github.com/armmbed/spiflash-driver) repository. +- The [i2ceeprom-driver](https://github.com/ARMmbed/i2ceeprom-driver.git) repository. +- The [ci-test-shield](https://github.com/ARMmbed/ci-test-shield) repository. This is the project describing + the mbed-os Continuous Integration test shield, together with standard tests. +- The [mbed-HDK](https://github.com/ARMmbed/mbed-HDK) repository containing Hardware Development Kit resources + including the schematics for the CI test shield. +- [POSIX File Interface ISO/IEC 9899:TC2 Documentation](http://www.eng.utah.edu/~cs5785/slides-f10/n1124.pdf). +- [FATFS: Generic FAT File System Module used in mbed OS](http://elm-chan.org/fsw/ff/00index_e.html)
diff -r 000000000000 -r a15c76864d7d sd-driver/SDBlockDevice.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/SDBlockDevice.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1004 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/* Introduction + * ------------ + * SD and MMC cards support a number of interfaces, but common to them all + * is one based on SPI. Since we already have the mbed SPI Interface, it will + * be used for SD cards. + * + * The main reference I'm using is Chapter 7, "SPI Mode" of: + * http://www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf + * + * SPI Startup + * ----------- + * The SD card powers up in SD mode. The start-up procedure is complicated + * by the requirement to support older SDCards in a backwards compatible + * way with the new higher capacity variants SDHC and SDHC. + * + * The following figures from the specification with associated text describe + * the SPI mode initialisation process: + * - Figure 7-1: SD Memory Card State Diagram (SPI mode) + * - Figure 7-2: SPI Mode Initialization Flow + * + * Firstly, a low initial clock should be selected (in the range of 100- + * 400kHZ). After initialisation has been completed, the switch to a + * higher clock speed can be made (e.g. 1MHz). Newer cards will support + * higher speeds than the default _transfer_sck defined here. + * + * Next, note the following from the SDCard specification (note to + * Figure 7-1): + * + * In any of the cases CMD1 is not recommended because it may be difficult for the host + * to distinguish between MultiMediaCard and SD Memory Card + * + * Hence CMD1 is not used for the initialisation sequence. + * + * The SPI interface mode is selected by asserting CS low and sending the + * reset command (CMD0). The card will respond with a (R1) response. + * In practice many cards initially respond with 0xff or invalid data + * which is ignored. Data is read until a valid response is received + * or the number of re-reads has exceeded a maximim count. If a valid + * response is not received then the CMD0 can be retried. This + * has been found to successfully initialise cards where the SPI master + * (on MCU) has been reset but the SDCard has not, so the first + * CMD0 may be lost. + * + * CMD8 is optionally sent to determine the voltage range supported, and + * indirectly determine whether it is a version 1.x SD/non-SD card or + * version 2.x. I'll just ignore this for now. + * + * ACMD41 is repeatedly issued to initialise the card, until "in idle" + * (bit 0) of the R1 response goes to '0', indicating it is initialised. + * + * You should also indicate whether the host supports High Capicity cards, + * and check whether the card is high capacity - i'll also ignore this + * + * SPI Protocol + * ------------ + * The SD SPI protocol is based on transactions made up of 8-bit words, with + * the host starting every bus transaction by asserting the CS signal low. The + * card always responds to commands, data blocks and errors. + * + * The protocol supports a CRC, but by default it is off (except for the + * first reset CMD0, where the CRC can just be pre-calculated, and CMD8) + * I'll leave the CRC off I think! + * + * Standard capacity cards have variable data block sizes, whereas High + * Capacity cards fix the size of data block to 512 bytes. I'll therefore + * just always use the Standard Capacity cards with a block size of 512 bytes. + * This is set with CMD16. + * + * You can read and write single blocks (CMD17, CMD25) or multiple blocks + * (CMD18, CMD25). For simplicity, I'll just use single block accesses. When + * the card gets a read command, it responds with a response token, and then + * a data token or an error. + * + * SPI Command Format + * ------------------ + * Commands are 6-bytes long, containing the command, 32-bit argument, and CRC. + * + * +---------------+------------+------------+-----------+----------+--------------+ + * | 01 | cmd[5:0] | arg[31:24] | arg[23:16] | arg[15:8] | arg[7:0] | crc[6:0] | 1 | + * +---------------+------------+------------+-----------+----------+--------------+ + * + * As I'm not using CRC, I can fix that byte to what is needed for CMD0 (0x95) + * + * All Application Specific commands shall be preceded with APP_CMD (CMD55). + * + * SPI Response Format + * ------------------- + * The main response format (R1) is a status byte (normally zero). Key flags: + * idle - 1 if the card is in an idle state/initialising + * cmd - 1 if an illegal command code was detected + * + * +-------------------------------------------------+ + * R1 | 0 | arg | addr | seq | crc | cmd | erase | idle | + * +-------------------------------------------------+ + * + * R1b is the same, except it is followed by a busy signal (zeros) until + * the first non-zero byte when it is ready again. + * + * Data Response Token + * ------------------- + * Every data block written to the card is acknowledged by a byte + * response token + * + * +----------------------+ + * | xxx | 0 | status | 1 | + * +----------------------+ + * 010 - OK! + * 101 - CRC Error + * 110 - Write Error + * + * Single Block Read and Write + * --------------------------- + * + * Block transfers have a byte header, followed by the data, followed + * by a 16-bit CRC. In our case, the data will always be 512 bytes. + * + * +------+---------+---------+- - - -+---------+-----------+----------+ + * | 0xFE | data[0] | data[1] | | data[n] | crc[15:8] | crc[7:0] | + * +------+---------+---------+- - - -+---------+-----------+----------+ + */ + +/* If the target has no SPI support then SDCard is not supported */ +#ifdef DEVICE_SPI + +#include "SDBlockDevice.h" +#include "mbed_debug.h" +#include <errno.h> + +/* Required version: 5.6.1 and above */ +#if defined(MBED_MAJOR_VERSION) && MBED_MAJOR_VERSION >= 5 +#if (MBED_VERSION < MBED_ENCODE_VERSION(5,6,1)) +#error "Incompatible mbed-os version detected! Required 5.6.1 and above" +#endif +#else +#warning "mbed-os version 5.6.1 or above required" +#endif + +#define SD_COMMAND_TIMEOUT 5000 /*!< Timeout in ms for response */ +#define SD_CMD0_GO_IDLE_STATE_RETRIES 5 /*!< Number of retries for sending CMDO */ +#define SD_DBG 0 /*!< 1 - Enable debugging */ +#define SD_CMD_TRACE 0 /*!< 1 - Enable SD command tracing */ + +#define SD_BLOCK_DEVICE_ERROR_WOULD_BLOCK -5001 /*!< operation would block */ +#define SD_BLOCK_DEVICE_ERROR_UNSUPPORTED -5002 /*!< unsupported operation */ +#define SD_BLOCK_DEVICE_ERROR_PARAMETER -5003 /*!< invalid parameter */ +#define SD_BLOCK_DEVICE_ERROR_NO_INIT -5004 /*!< uninitialized */ +#define SD_BLOCK_DEVICE_ERROR_NO_DEVICE -5005 /*!< device is missing or not connected */ +#define SD_BLOCK_DEVICE_ERROR_WRITE_PROTECTED -5006 /*!< write protected */ +#define SD_BLOCK_DEVICE_ERROR_UNUSABLE -5007 /*!< unusable card */ +#define SD_BLOCK_DEVICE_ERROR_NO_RESPONSE -5008 /*!< No response from device */ +#define SD_BLOCK_DEVICE_ERROR_CRC -5009 /*!< CRC error */ +#define SD_BLOCK_DEVICE_ERROR_ERASE -5010 /*!< Erase error: reset/sequence */ +#define SD_BLOCK_DEVICE_ERROR_WRITE -5011 /*!< SPI Write error: !SPI_DATA_ACCEPTED */ + +#define BLOCK_SIZE_HC 512 /*!< Block size supported for SD card is 512 bytes */ +#define WRITE_BL_PARTIAL 0 /*!< Partial block write - Not supported */ +#define CRC_SUPPORT 0 /*!< CRC - Not supported */ +#define SPI_CMD(x) (0x40 | (x & 0x3f)) + +/* R1 Response Format */ +#define R1_NO_RESPONSE (0xFF) +#define R1_RESPONSE_RECV (0x80) +#define R1_IDLE_STATE (1 << 0) +#define R1_ERASE_RESET (1 << 1) +#define R1_ILLEGAL_COMMAND (1 << 2) +#define R1_COM_CRC_ERROR (1 << 3) +#define R1_ERASE_SEQUENCE_ERROR (1 << 4) +#define R1_ADDRESS_ERROR (1 << 5) +#define R1_PARAMETER_ERROR (1 << 6) + +// Types +#define SDCARD_NONE 0 /**< No card is present */ +#define SDCARD_V1 1 /**< v1.x Standard Capacity */ +#define SDCARD_V2 2 /**< v2.x Standard capacity SD card */ +#define SDCARD_V2HC 3 /**< v2.x High capacity SD card */ +#define CARD_UNKNOWN 4 /**< Unknown or unsupported card */ + +/* SIZE in Bytes */ +#define PACKET_SIZE 6 /*!< SD Packet size CMD+ARG+CRC */ +#define R1_RESPONSE_SIZE 1 /*!< Size of R1 response */ +#define R2_RESPONSE_SIZE 2 /*!< Size of R2 response */ +#define R3_R7_RESPONSE_SIZE 5 /*!< Size of R3/R7 response */ + +/* R1b Response */ +#define DEVICE_BUSY (0x00) + +/* R2 Response Format */ +#define R2_CARD_LOCKED (1 << 0) +#define R2_CMD_FAILED (1 << 1) +#define R2_ERROR (1 << 2) +#define R2_CC_ERROR (1 << 3) +#define R2_CC_FAILED (1 << 4) +#define R2_WP_VIOLATION (1 << 5) +#define R2_ERASE_PARAM (1 << 6) +#define R2_OUT_OF_RANGE (1 << 7) + +/* R3 Response : OCR Register */ +#define OCR_HCS_CCS (0x1 << 30) +#define OCR_LOW_VOLTAGE (0x01 << 24) +#define OCR_3_3V (0x1 << 20) + +/* R7 response pattern for CMD8 */ +#define CMD8_PATTERN (0xAA) + +/* CRC Enable */ +#define CRC_ENABLE (0) /*!< CRC 1 - Enable 0 - Disable */ + +/* Control Tokens */ +#define SPI_DATA_RESPONSE_MASK (0x1F) +#define SPI_DATA_ACCEPTED (0x05) +#define SPI_DATA_CRC_ERROR (0x0B) +#define SPI_DATA_WRITE_ERROR (0x0D) +#define SPI_START_BLOCK (0xFE) /*!< For Single Block Read/Write and Multiple Block Read */ +#define SPI_START_BLK_MUL_WRITE (0xFC) /*!< Start Multi-block write */ +#define SPI_STOP_TRAN (0xFD) /*!< Stop Multi-block write */ + +#define SPI_DATA_READ_ERROR_MASK (0xF) /*!< Data Error Token: 4 LSB bits */ +#define SPI_READ_ERROR (0x1 << 0) /*!< Error */ +#define SPI_READ_ERROR_CC (0x1 << 1) /*!< CC Error*/ +#define SPI_READ_ERROR_ECC_C (0x1 << 2) /*!< Card ECC failed */ +#define SPI_READ_ERROR_OFR (0x1 << 3) /*!< Out of Range */ + +SDBlockDevice::SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz) + : _sectors(0), _spi(mosi, miso, sclk), _cs(cs), _is_initialized(0) +{ + _cs = 1; + _card_type = SDCARD_NONE; + + // Set default to 100kHz for initialisation and 1MHz for data transfer + _init_sck = 100000; + _transfer_sck = hz; + + // Only HC block size is supported. + _block_size = BLOCK_SIZE_HC; + _erase_size = BLOCK_SIZE_HC; +} + +SDBlockDevice::~SDBlockDevice() +{ + if (_is_initialized) { + deinit(); + } +} + +int SDBlockDevice::_initialise_card() +{ + // Detail debugging is for commands + _dbg = SD_DBG ? SD_CMD_TRACE : 0; + int32_t status = BD_ERROR_OK; + uint32_t response, arg; + + // Initialize the SPI interface: Card by default is in SD mode + _spi_init(); + + // The card is transitioned from SDCard mode to SPI mode by sending the CMD0 + CS Asserted("0") + if (_go_idle_state() != R1_IDLE_STATE) { + debug_if(SD_DBG, "No disk, or could not put SD card in to SPI idle state\n"); + return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; + } + + // Send CMD8, if the card rejects the command then it's probably using the + // legacy protocol, or is a MMC, or just flat-out broken + status = _cmd8(); + if (BD_ERROR_OK != status && SD_BLOCK_DEVICE_ERROR_UNSUPPORTED != status) { + return status; + } + + // Read OCR - CMD58 Response contains OCR register + if (BD_ERROR_OK != (status = _cmd(CMD58_READ_OCR, 0x0, 0x0, &response))) { + return status; + } + + // Check if card supports voltage range: 3.3V + if (!(response & OCR_3_3V)) { + _card_type = CARD_UNKNOWN; + status = SD_BLOCK_DEVICE_ERROR_UNUSABLE; + return status; + } + + // HCS is set 1 for HC/XC capacity cards for ACMD41, if supported + arg = 0x0; + if (SDCARD_V2 == _card_type) { + arg |= OCR_HCS_CCS; + } + + /* Idle state bit in the R1 response of ACMD41 is used by the card to inform the host + * if initialization of ACMD41 is completed. "1" indicates that the card is still initializing. + * "0" indicates completion of initialization. The host repeatedly issues ACMD41 until + * this bit is set to "0". + */ + _spi_timer.start(); + do { + status = _cmd(ACMD41_SD_SEND_OP_COND, arg, 1, &response); + } while ((response & R1_IDLE_STATE) && (_spi_timer.read_ms() < SD_COMMAND_TIMEOUT)); + _spi_timer.stop(); + + // Initialization complete: ACMD41 successful + if ((BD_ERROR_OK != status) || (0x00 != response)) { + _card_type = CARD_UNKNOWN; + debug_if(SD_DBG, "Timeout waiting for card\n"); + return status; + } + + if (SDCARD_V2 == _card_type) { + // Get the card capacity CCS: CMD58 + if (BD_ERROR_OK == (status = _cmd(CMD58_READ_OCR, 0x0, 0x0, &response))) { + // High Capacity card + if (response & OCR_HCS_CCS) { + _card_type = SDCARD_V2HC; + debug_if(SD_DBG, "Card Initialized: High Capacity Card \n"); + } else { + debug_if(SD_DBG, "Card Initialized: Standard Capacity Card: Version 2.x \n"); + } + } + } else { + _card_type = SDCARD_V1; + debug_if(SD_DBG, "Card Initialized: Version 1.x Card\n"); + } + + // Disable CRC + status = _cmd(CMD59_CRC_ON_OFF, 0); + + return status; +} + + +int SDBlockDevice::init() +{ + lock(); + int err = _initialise_card(); + _is_initialized = (err == BD_ERROR_OK); + if (!_is_initialized) { + debug_if(SD_DBG, "Fail to initialize card\n"); + unlock(); + return err; + } + debug_if(SD_DBG, "init card = %d\n", _is_initialized); + _sectors = _sd_sectors(); + // CMD9 failed + if (0 == _sectors) { + unlock(); + return BD_ERROR_DEVICE_ERROR; + } + + // Set block length to 512 (CMD16) + if (_cmd(CMD16_SET_BLOCKLEN, _block_size) != 0) { + debug_if(SD_DBG, "Set %d-byte block timed out\n", _block_size); + unlock(); + return BD_ERROR_DEVICE_ERROR; + } + + // Set SCK for data transfer + err = _freq(); + if (err) { + unlock(); + return err; + } + unlock(); + return BD_ERROR_OK; +} + +int SDBlockDevice::deinit() +{ + lock(); + _is_initialized = false; + _sectors = 0; + unlock(); + return 0; +} + + +int SDBlockDevice::program(const void *b, bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_program(addr, size)) { + return SD_BLOCK_DEVICE_ERROR_PARAMETER; + } + + lock(); + if (!_is_initialized) { + unlock(); + return SD_BLOCK_DEVICE_ERROR_NO_INIT; + } + + const uint8_t *buffer = static_cast<const uint8_t*>(b); + int status = BD_ERROR_OK; + uint8_t response; + + // Get block count + bd_addr_t blockCnt = size / _block_size; + + // SDSC Card (CCS=0) uses byte unit address + // SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit) + if(SDCARD_V2HC == _card_type) { + addr = addr / _block_size; + } + + // Send command to perform write operation + if (blockCnt == 1) { + // Single block write command + if (BD_ERROR_OK != (status = _cmd(CMD24_WRITE_BLOCK, addr))) { + unlock(); + return status; + } + + // Write data + response = _write(buffer, SPI_START_BLOCK, _block_size); + + // Only CRC and general write error are communicated via response token + if ((response == SPI_DATA_CRC_ERROR) || (response == SPI_DATA_WRITE_ERROR)) { + debug_if(SD_DBG, "Single Block Write failed: 0x%x \n", response); + status = SD_BLOCK_DEVICE_ERROR_WRITE; + } + } else { + // Pre-erase setting prior to multiple block write operation + _cmd(ACMD23_SET_WR_BLK_ERASE_COUNT, blockCnt, 1); + + // Multiple block write command + if (BD_ERROR_OK != (status = _cmd(CMD25_WRITE_MULTIPLE_BLOCK, addr))) { + unlock(); + return status; + } + + // Write the data: one block at a time + do { + response = _write(buffer, SPI_START_BLK_MUL_WRITE, _block_size); + if (response != SPI_DATA_ACCEPTED) { + debug_if(SD_DBG, "Multiple Block Write failed: 0x%x \n", response); + break; + } + buffer += _block_size; + }while (--blockCnt); // Receive all blocks of data + + /* In a Multiple Block write operation, the stop transmission will be done by + * sending 'Stop Tran' token instead of 'Start Block' token at the beginning + * of the next block + */ + _spi.write(SPI_STOP_TRAN); + } + + _deselect(); + unlock(); + return status; +} + +int SDBlockDevice::read(void *b, bd_addr_t addr, bd_size_t size) +{ + if (!is_valid_read(addr, size)) { + return SD_BLOCK_DEVICE_ERROR_PARAMETER; + } + + lock(); + if (!_is_initialized) { + unlock(); + return SD_BLOCK_DEVICE_ERROR_PARAMETER; + } + + uint8_t *buffer = static_cast<uint8_t *>(b); + int status = BD_ERROR_OK; + bd_addr_t blockCnt = size / _block_size; + + // SDSC Card (CCS=0) uses byte unit address + // SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit) + if (SDCARD_V2HC == _card_type) { + addr = addr / _block_size; + } + + // Write command ro receive data + if (blockCnt > 1) { + status = _cmd(CMD18_READ_MULTIPLE_BLOCK, addr); + } else { + status = _cmd(CMD17_READ_SINGLE_BLOCK, addr); + } + if (BD_ERROR_OK != status) { + unlock(); + return status; + } + + // receive the data : one block at a time + while (blockCnt) { + if (0 != _read(buffer, _block_size)) { + status = SD_BLOCK_DEVICE_ERROR_NO_RESPONSE; + break; + } + buffer += _block_size; + --blockCnt; + } + _deselect(); + + // Send CMD12(0x00000000) to stop the transmission for multi-block transfer + if (size > _block_size) { + status = _cmd(CMD12_STOP_TRANSMISSION, 0x0); + } + unlock(); + return status; +} + +bool SDBlockDevice::_is_valid_trim(bd_addr_t addr, bd_size_t size) +{ + return ( + addr % _erase_size == 0 && + size % _erase_size == 0 && + addr + size <= this->size()); +} + +int SDBlockDevice::trim(bd_addr_t addr, bd_size_t size) +{ + if (!_is_valid_trim(addr, size)) { + return SD_BLOCK_DEVICE_ERROR_PARAMETER; + } + + lock(); + if (!_is_initialized) { + unlock(); + return SD_BLOCK_DEVICE_ERROR_NO_INIT; + } + int status = BD_ERROR_OK; + + size -= _block_size; + // SDSC Card (CCS=0) uses byte unit address + // SDHC and SDXC Cards (CCS=1) use block unit address (512 Bytes unit) + if (SDCARD_V2HC == _card_type) { + size = size / _block_size; + addr = addr / _block_size; + } + + // Start lba sent in start command + if (BD_ERROR_OK != (status = _cmd(CMD32_ERASE_WR_BLK_START_ADDR, addr))) { + unlock(); + return status; + } + + // End lba = addr+size sent in end addr command + if (BD_ERROR_OK != (status = _cmd(CMD33_ERASE_WR_BLK_END_ADDR, addr+size))) { + unlock(); + return status; + } + status = _cmd(CMD38_ERASE, 0x0); + unlock(); + return status; +} + +bd_size_t SDBlockDevice::get_read_size() const +{ + return _block_size; +} + +bd_size_t SDBlockDevice::get_program_size() const +{ + return _block_size; +} + +bd_size_t SDBlockDevice::size() const +{ + return _block_size*_sectors; +} + +void SDBlockDevice::debug(bool dbg) +{ + _dbg = dbg; +} + +int SDBlockDevice::frequency(uint64_t freq) +{ + lock(); + _transfer_sck = freq; + int err = _freq(); + unlock(); + return err; +} + +// PRIVATE FUNCTIONS +int SDBlockDevice::_freq(void) +{ + // Max frequency supported is 25MHZ + if (_transfer_sck <= 25000000) { + _spi.frequency(_transfer_sck); + return 0; + } else { // TODO: Switch function to be implemented for higher frequency + _transfer_sck = 25000000; + _spi.frequency(_transfer_sck); + return -EINVAL; + } +} + +uint8_t SDBlockDevice::_cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg) { + uint8_t response; + char cmdPacket[PACKET_SIZE]; + + // Prepare the command packet + cmdPacket[0] = SPI_CMD(cmd); + cmdPacket[1] = (arg >> 24); + cmdPacket[2] = (arg >> 16); + cmdPacket[3] = (arg >> 8); + cmdPacket[4] = (arg >> 0); + // CMD0 is executed in SD mode, hence should have correct CRC + // CMD8 CRC verification is always enabled + switch(cmd) { + case CMD0_GO_IDLE_STATE: + cmdPacket[5] = 0x95; + break; + case CMD8_SEND_IF_COND: + cmdPacket[5] = 0x87; + break; + default: + cmdPacket[5] = 0xFF; // Make sure bit 0-End bit is high + break; + } + + // send a command + for (int i = 0; i < PACKET_SIZE; i++) { + _spi.write(cmdPacket[i]); + } + + // The received byte immediataly following CMD12 is a stuff byte, + // it should be discarded before receive the response of the CMD12. + if (CMD12_STOP_TRANSMISSION == cmd) { + _spi.write(SPI_FILL_CHAR); + } + + // Loop for response: Response is sent back within command response time (NCR), 0 to 8 bytes for SDC + for (int i = 0; i < 0x10; i++) { + response = _spi.write(SPI_FILL_CHAR); + // Got the response + if (!(response & R1_RESPONSE_RECV)) { + break; + } + } + return response; +} + +int SDBlockDevice::_cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd, uint32_t *resp) { + int32_t status = BD_ERROR_OK; + uint32_t response; + + // Select card and wait for card to be ready before sending next command + // Note: next command will fail if card is not ready + _select(); + + // No need to wait for card to be ready when sending the stop command + if (CMD12_STOP_TRANSMISSION != cmd) { + if (false == _wait_ready(SD_COMMAND_TIMEOUT)) { + debug_if(SD_DBG, "Card not ready yet \n"); + } + } + + // Re-try command + for(int i = 0; i < 3; i++) { + // Send CMD55 for APP command first + if (isAcmd) { + response = _cmd_spi(CMD55_APP_CMD, 0x0); + // Wait for card to be ready after CMD55 + if (false == _wait_ready(SD_COMMAND_TIMEOUT)) { + debug_if(SD_DBG, "Card not ready yet \n"); + } + } + + // Send command over SPI interface + response = _cmd_spi(cmd, arg); + if (R1_NO_RESPONSE == response) { + debug_if(SD_DBG, "No response CMD:%d \n", cmd); + continue; + } + break; + } + + // Pass the response to the command call if required + if (NULL != resp) { + *resp = response; + } + + // Process the response R1 : Exit on CRC/Illegal command error/No response + if (R1_NO_RESPONSE == response) { + _deselect(); + debug_if(SD_DBG, "No response CMD:%d response: 0x%x\n",cmd, response); + return SD_BLOCK_DEVICE_ERROR_NO_DEVICE; // No device + } + if (response & R1_COM_CRC_ERROR) { + _deselect(); + debug_if(SD_DBG, "CRC error CMD:%d response 0x%x \n",cmd, response); + return SD_BLOCK_DEVICE_ERROR_CRC; // CRC error + } + if (response & R1_ILLEGAL_COMMAND) { + _deselect(); + debug_if(SD_DBG, "Illegal command CMD:%d response 0x%x\n",cmd, response); + if (CMD8_SEND_IF_COND == cmd) { // Illegal command is for Ver1 or not SD Card + _card_type = CARD_UNKNOWN; + } + return SD_BLOCK_DEVICE_ERROR_UNSUPPORTED; // Command not supported + } + + debug_if(_dbg, "CMD:%d \t arg:0x%x \t Response:0x%x \n", cmd, arg, response); + // Set status for other errors + if ((response & R1_ERASE_RESET) || (response & R1_ERASE_SEQUENCE_ERROR)) { + status = SD_BLOCK_DEVICE_ERROR_ERASE; // Erase error + }else if ((response & R1_ADDRESS_ERROR) || (response & R1_PARAMETER_ERROR)) { + // Misaligned address / invalid address block length + status = SD_BLOCK_DEVICE_ERROR_PARAMETER; + } + + // Get rest of the response part for other commands + switch(cmd) { + case CMD8_SEND_IF_COND: // Response R7 + debug_if(_dbg, "V2-Version Card\n"); + _card_type = SDCARD_V2; + // Note: No break here, need to read rest of the response + case CMD58_READ_OCR: // Response R3 + response = (_spi.write(SPI_FILL_CHAR) << 24); + response |= (_spi.write(SPI_FILL_CHAR) << 16); + response |= (_spi.write(SPI_FILL_CHAR) << 8); + response |= _spi.write(SPI_FILL_CHAR); + debug_if(_dbg, "R3/R7: 0x%x \n", response); + break; + + case CMD12_STOP_TRANSMISSION: // Response R1b + case CMD38_ERASE: + _wait_ready(SD_COMMAND_TIMEOUT); + break; + + case ACMD13_SD_STATUS: // Response R2 + response = _spi.write(SPI_FILL_CHAR); + debug_if(_dbg, "R2: 0x%x \n", response); + break; + + default: // Response R1 + break; + } + + // Pass the updated response to the command + if (NULL != resp) { + *resp = response; + } + + // Do not deselect card if read is in progress. + if (((CMD9_SEND_CSD == cmd) || (ACMD22_SEND_NUM_WR_BLOCKS == cmd) || + (CMD24_WRITE_BLOCK == cmd) || (CMD25_WRITE_MULTIPLE_BLOCK == cmd) || + (CMD17_READ_SINGLE_BLOCK == cmd) || (CMD18_READ_MULTIPLE_BLOCK == cmd)) + && (BD_ERROR_OK == status)) { + return BD_ERROR_OK; + } + // Deselect card + _deselect(); + return status; +} + +int SDBlockDevice::_cmd8() { + uint32_t arg = (CMD8_PATTERN << 0); // [7:0]check pattern + uint32_t response = 0; + int32_t status = BD_ERROR_OK; + + arg |= (0x1 << 8); // 2.7-3.6V // [11:8]supply voltage(VHS) + + status = _cmd(CMD8_SEND_IF_COND, arg, 0x0, &response); + // Verify voltage and pattern for V2 version of card + if ((BD_ERROR_OK == status) && (SDCARD_V2 == _card_type)) { + // If check pattern is not matched, CMD8 communication is not valid + if((response & 0xFFF) != arg) + { + debug_if(SD_DBG, "CMD8 Pattern mismatch 0x%x : 0x%x\n", arg, response); + _card_type = CARD_UNKNOWN; + status = SD_BLOCK_DEVICE_ERROR_UNUSABLE; + } + } + return status; +} + +uint32_t SDBlockDevice::_go_idle_state() { + uint32_t response; + + /* Reseting the MCU SPI master may not reset the on-board SDCard, in which + * case when MCU power-on occurs the SDCard will resume operations as + * though there was no reset. In this scenario the first CMD0 will + * not be interpreted as a command and get lost. For some cards retrying + * the command overcomes this situation. */ + for (int i = 0; i < SD_CMD0_GO_IDLE_STATE_RETRIES; i++) { + _cmd(CMD0_GO_IDLE_STATE, 0x0, 0x0, &response); + if (R1_IDLE_STATE == response) + break; + wait_ms(1); + } + return response; +} + +int SDBlockDevice::_read_bytes(uint8_t *buffer, uint32_t length) { + uint16_t crc; + + // read until start byte (0xFE) + if (false == _wait_token(SPI_START_BLOCK)) { + debug_if(SD_DBG, "Read timeout\n"); + _deselect(); + return SD_BLOCK_DEVICE_ERROR_NO_RESPONSE; + } + + // read data + for (uint32_t i = 0; i < length; i++) { + buffer[i] = _spi.write(SPI_FILL_CHAR); + } + + // Read the CRC16 checksum for the data block + crc = (_spi.write(SPI_FILL_CHAR) << 8); + crc |= _spi.write(SPI_FILL_CHAR); + + _deselect(); + return 0; +} + +int SDBlockDevice::_read(uint8_t *buffer, uint32_t length) { + uint16_t crc; + + // read until start byte (0xFE) + if (false == _wait_token(SPI_START_BLOCK)) { + debug_if(SD_DBG, "Read timeout\n"); + _deselect(); + return SD_BLOCK_DEVICE_ERROR_NO_RESPONSE; + } + + // read data + _spi.write(NULL, 0, (char*)buffer, length); + + // Read the CRC16 checksum for the data block + crc = (_spi.write(SPI_FILL_CHAR) << 8); + crc |= _spi.write(SPI_FILL_CHAR); + + return 0; +} + +uint8_t SDBlockDevice::_write(const uint8_t *buffer, uint8_t token, uint32_t length) { + uint16_t crc = 0xFFFF; + uint8_t response = 0xFF; + + // indicate start of block + _spi.write(token); + + // write the data + _spi.write((char*)buffer, length, NULL, 0); + + // write the checksum CRC16 + _spi.write(crc >> 8); + _spi.write(crc); + + // check the response token + response = _spi.write(SPI_FILL_CHAR); + + // Wait for last block to be written + if (false == _wait_ready(SD_COMMAND_TIMEOUT)) { + debug_if(SD_DBG, "Card not ready yet \n"); + } + + return (response & SPI_DATA_RESPONSE_MASK); +} + +static uint32_t ext_bits(unsigned char *data, int msb, int lsb) { + uint32_t bits = 0; + uint32_t size = 1 + msb - lsb; + for (uint32_t i = 0; i < size; i++) { + uint32_t position = lsb + i; + uint32_t byte = 15 - (position >> 3); + uint32_t bit = position & 0x7; + uint32_t value = (data[byte] >> bit) & 1; + bits |= value << i; + } + return bits; +} + +bd_size_t SDBlockDevice::_sd_sectors() { + uint32_t c_size, c_size_mult, read_bl_len; + uint32_t block_len, mult, blocknr; + uint32_t hc_c_size; + bd_size_t blocks = 0, capacity = 0; + + // CMD9, Response R2 (R1 byte + 16-byte block read) + if (_cmd(CMD9_SEND_CSD, 0x0) != 0x0) { + debug_if(SD_DBG, "Didn't get a response from the disk\n"); + return 0; + } + uint8_t csd[16]; + if (_read_bytes(csd, 16) != 0) { + debug_if(SD_DBG, "Couldn't read csd response from disk\n"); + return 0; + } + + // csd_structure : csd[127:126] + int csd_structure = ext_bits(csd, 127, 126); + switch (csd_structure) { + case 0: + c_size = ext_bits(csd, 73, 62); // c_size : csd[73:62] + c_size_mult = ext_bits(csd, 49, 47); // c_size_mult : csd[49:47] + read_bl_len = ext_bits(csd, 83, 80); // read_bl_len : csd[83:80] - the *maximum* read block length + block_len = 1 << read_bl_len; // BLOCK_LEN = 2^READ_BL_LEN + mult = 1 << (c_size_mult + 2); // MULT = 2^C_SIZE_MULT+2 (C_SIZE_MULT < 8) + blocknr = (c_size + 1) * mult; // BLOCKNR = (C_SIZE+1) * MULT + capacity = blocknr * block_len; // memory capacity = BLOCKNR * BLOCK_LEN + blocks = capacity / _block_size; + debug_if(SD_DBG, "Standard Capacity: c_size: %d \n", c_size); + debug_if(SD_DBG, "Sectors: 0x%x : %llu\n", blocks, blocks); + debug_if(SD_DBG, "Capacity: 0x%x : %llu MB\n", capacity, (capacity/(1024U*1024U))); + + // ERASE_BLK_EN = 1: Erase in multiple of 512 bytes supported + if (ext_bits(csd, 46, 46)) { + _erase_size = BLOCK_SIZE_HC; + } else { + // ERASE_BLK_EN = 1: Erase in multiple of SECTOR_SIZE supported + _erase_size = BLOCK_SIZE_HC * (ext_bits(csd, 45, 39) + 1); + } + break; + + case 1: + hc_c_size = ext_bits(csd, 69, 48); // device size : C_SIZE : [69:48] + blocks = (hc_c_size+1) << 10; // block count = C_SIZE+1) * 1K byte (512B is block size) + debug_if(SD_DBG, "SDHC/SDXC Card: hc_c_size: %d \n", hc_c_size); + debug_if(SD_DBG, "Sectors: 0x%x : %llu\n", blocks, blocks); + debug_if(SD_DBG, "Capacity: %llu MB\n", (blocks/(2048U))); + // ERASE_BLK_EN is fixed to 1, which means host can erase one or multiple of 512 bytes. + _erase_size = BLOCK_SIZE_HC; + break; + + default: + debug_if(SD_DBG, "CSD struct unsupported\r\n"); + return 0; + }; + return blocks; +} + +// SPI function to wait till chip is ready and sends start token +bool SDBlockDevice::_wait_token(uint8_t token) { + _spi_timer.reset(); + _spi_timer.start(); + + do { + if (token == _spi.write(SPI_FILL_CHAR)) { + _spi_timer.stop(); + return true; + } + } while (_spi_timer.read_ms() < 300); // Wait for 300 msec for start token + _spi_timer.stop(); + debug_if(SD_DBG, "_wait_token: timeout\n"); + return false; +} + +// SPI function to wait till chip is ready +// The host controller should wait for end of the process until DO goes high (a 0xFF is received). +bool SDBlockDevice::_wait_ready(uint16_t ms) { + uint8_t response; + _spi_timer.reset(); + _spi_timer.start(); + do { + response = _spi.write(SPI_FILL_CHAR); + if (response == 0xFF) { + _spi_timer.stop(); + return true; + } + } while (_spi_timer.read_ms() < ms); + _spi_timer.stop(); + return false; +} + +// SPI function to wait for count +void SDBlockDevice::_spi_wait(uint8_t count) +{ + for (uint8_t i = 0; i < count; ++i) { + _spi.write(SPI_FILL_CHAR); + } +} + +void SDBlockDevice::_spi_init() { + _spi.lock(); + // Set to SCK for initialization, and clock card with cs = 1 + _spi.frequency(_init_sck); + _spi.format(8, 0); + _spi.set_default_write_value(SPI_FILL_CHAR); + // Initial 74 cycles required for few cards, before selecting SPI mode + _cs = 1; + _spi_wait(10); + _spi.unlock(); +} + +void SDBlockDevice::_select() { + _spi.lock(); + _spi.write(SPI_FILL_CHAR); + _cs = 0; +} + +void SDBlockDevice::_deselect() { + _cs = 1; + _spi.write(SPI_FILL_CHAR); + _spi.unlock(); +} + +#endif /* DEVICE_SPI */
diff -r 000000000000 -r a15c76864d7d sd-driver/SDBlockDevice.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/SDBlockDevice.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,232 @@ +/* mbed Microcontroller Library + * Copyright (c) 2006-2013 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef MBED_SD_BLOCK_DEVICE_H +#define MBED_SD_BLOCK_DEVICE_H + +/* If the target has no SPI support then SDCard is not supported */ +#ifdef DEVICE_SPI + +#include "BlockDevice.h" +#include "mbed.h" +#include "platform/PlatformMutex.h" + +/** Access an SD Card using SPI + * + * @code + * #include "mbed.h" + * #include "SDBlockDevice.h" + * + * SDBlockDevice sd(p5, p6, p7, p12); // mosi, miso, sclk, cs + * uint8_t block[512] = "Hello World!\n"; + * + * int main() { + * sd.init(); + * sd.write(block, 0, 512); + * sd.read(block, 0, 512); + * printf("%s", block); + * sd.deinit(); + * } + */ +class SDBlockDevice : public BlockDevice { +public: + /** Lifetime of an SD card + */ + SDBlockDevice(PinName mosi, PinName miso, PinName sclk, PinName cs, uint64_t hz=1000000); + virtual ~SDBlockDevice(); + + /** Initialize a block device + * + * @return 0 on success or a negative error code on failure + */ + virtual int init(); + + /** Deinitialize a block device + * + * @return 0 on success or a negative error code on failure + */ + virtual int deinit(); + + /** Read blocks from a block device + * + * @param buffer Buffer to write blocks to + * @param addr Address of block to begin reading from + * @param size Size to read in bytes, must be a multiple of read block size + * @return 0 on success, negative error code on failure + */ + virtual int read(void *buffer, bd_addr_t addr, bd_size_t size); + + /** Program blocks to a block device + * + * The blocks must have been erased prior to being programmed + * + * @param buffer Buffer of data to write to blocks + * @param addr Address of block to begin writing to + * @param size Size to write in bytes, must be a multiple of program block size + * @return 0 on success, negative error code on failure + */ + virtual int program(const void *buffer, bd_addr_t addr, bd_size_t size); + + /** Mark blocks as no longer in use + * + * This function provides a hint to the underlying block device that a region of blocks + * is no longer in use and may be erased without side effects. Erase must still be called + * before programming, but trimming allows flash-translation-layers to schedule erases when + * the device is not busy. + * + * @param addr Address of block to mark as unused + * @param size Size to mark as unused in bytes, must be a multiple of erase block size + * @return 0 on success, negative error code on failure + */ + virtual int trim(bd_addr_t addr, bd_size_t size); + + /** Get the size of a readable block + * + * @return Size of a readable block in bytes + */ + virtual bd_size_t get_read_size() const; + + /** Get the size of a programable block + * + * @return Size of a programable block in bytes + * @note Must be a multiple of the read size + */ + virtual bd_size_t get_program_size() const; + + /** Get the total size of the underlying device + * + * @return Size of the underlying device in bytes + */ + virtual bd_size_t size() const; + + /** Enable or disable debugging + * + * @param State of debugging + */ + virtual void debug(bool dbg); + + /** Set the transfer frequency + * + * @param Transfer frequency + * @note Max frequency supported is 25MHZ + */ + virtual int frequency(uint64_t freq); + + +private: + /* Commands : Listed below are commands supported + * in SPI mode for SD card : Only Mandatory ones + */ + enum cmdSupported { + CMD_NOT_SUPPORTED = -1, /**< Command not supported error */ + CMD0_GO_IDLE_STATE = 0, /**< Resets the SD Memory Card */ + CMD1_SEND_OP_COND = 1, /**< Sends host capacity support */ + CMD6_SWITCH_FUNC = 6, /**< Check and Switches card function */ + CMD8_SEND_IF_COND = 8, /**< Supply voltage info */ + CMD9_SEND_CSD = 9, /**< Provides Card Specific data */ + CMD10_SEND_CID = 10, /**< Provides Card Identification */ + CMD12_STOP_TRANSMISSION = 12, /**< Forces the card to stop transmission */ + CMD13_SEND_STATUS = 13, /**< Card responds with status */ + CMD16_SET_BLOCKLEN = 16, /**< Length for SC card is set */ + CMD17_READ_SINGLE_BLOCK = 17, /**< Read single block of data */ + CMD18_READ_MULTIPLE_BLOCK = 18, /**< Card transfers data blocks to host until interrupted + by a STOP_TRANSMISSION command */ + CMD24_WRITE_BLOCK = 24, /**< Write single block of data */ + CMD25_WRITE_MULTIPLE_BLOCK = 25, /**< Continuously writes blocks of data until + 'Stop Tran' token is sent */ + CMD27_PROGRAM_CSD = 27, /**< Programming bits of CSD */ + CMD32_ERASE_WR_BLK_START_ADDR = 32, /**< Sets the address of the first write + block to be erased. */ + CMD33_ERASE_WR_BLK_END_ADDR = 33, /**< Sets the address of the last write + block of the continuous range to be erased.*/ + CMD38_ERASE = 38, /**< Erases all previously selected write blocks */ + CMD55_APP_CMD = 55, /**< Extend to Applications specific commands */ + CMD56_GEN_CMD = 56, /**< General Purpose Command */ + CMD58_READ_OCR = 58, /**< Read OCR register of card */ + CMD59_CRC_ON_OFF = 59, /**< Turns the CRC option on or off*/ + // App Commands + ACMD6_SET_BUS_WIDTH = 6, + ACMD13_SD_STATUS = 13, + ACMD22_SEND_NUM_WR_BLOCKS = 22, + ACMD23_SET_WR_BLK_ERASE_COUNT = 23, + ACMD41_SD_SEND_OP_COND = 41, + ACMD42_SET_CLR_CARD_DETECT = 42, + ACMD51_SEND_SCR = 51, + }; + + uint8_t _card_type; + int _cmd(SDBlockDevice::cmdSupported cmd, uint32_t arg, bool isAcmd=0, uint32_t *resp=NULL); + int _cmd8(); + + /* Move the SDCard into the SPI Mode idle state + * + * The card is transitioned from SDCard mode to SPI mode by sending the + * CMD0 (GO_IDLE_STATE) command with CS asserted. See the notes in the + * "SPI Startup" section of the comments at the head of the + * implementation file for further details and specification references. + * + * @return Response form the card. R1_IDLE_STATE (0x1), the successful + * response from CMD0. R1_XXX_XXX for more response + */ + uint32_t _go_idle_state(); + int _initialise_card(); + + bd_size_t _sectors; + bd_size_t _sd_sectors(); + + bool _is_valid_trim(bd_addr_t addr, bd_size_t size); + + /* SPI functions */ + Timer _spi_timer; /**< Timer Class object used for busy wait */ + uint32_t _init_sck; /**< Intial SPI frequency */ + uint32_t _transfer_sck; /**< SPI frequency during data transfer/after initialization */ + SPI _spi; /**< SPI Class object */ + + /* SPI initialization function */ + void _spi_init(); + uint8_t _cmd_spi(SDBlockDevice::cmdSupported cmd, uint32_t arg); + void _spi_wait(uint8_t count); + + bool _wait_token(uint8_t token); /**< Wait for token */ + bool _wait_ready(uint16_t ms=300); /**< 300ms default wait for card to be ready */ + int _read(uint8_t * buffer, uint32_t length); + int _read_bytes(uint8_t * buffer, uint32_t length); + uint8_t _write(const uint8_t *buffer,uint8_t token, uint32_t length); + int _freq(void); + + /* Chip Select and SPI mode select */ + DigitalOut _cs; + void _select(); + void _deselect(); + + virtual void lock() { + _mutex.lock(); + } + + virtual void unlock() { + _mutex.unlock(); + } + + PlatformMutex _mutex; + bd_size_t _block_size; + bd_size_t _erase_size; + bool _is_initialized; + bool _dbg; +}; + +#endif /* DEVICE_SPI */ + +#endif /* MBED_SD_BLOCK_DEVICE_H */
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/block_device/basic/basic.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/block_device/basic/basic.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,179 @@ +/* + * mbed Microcontroller Library + * Copyright (c) 2006-2016 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/* The following copyright notice is reproduced from the glibc project + * REF_LICENCE_GLIBC + * + * Copyright (C) 1991, 1992 Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * The GNU C Library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with the GNU C Library; see the file COPYING.LIB. If + * not, write to the Free Software Foundation, Inc., 675 Mass Ave, + * Cambridge, MA 02139, USA. + */ + + +/** @file main.cpp Basic SD Driver Test + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" + +#include "SDBlockDevice.h" +#include <stdlib.h> + +using namespace utest::v1; + +#define TEST_BLOCK_COUNT 10 +#define TEST_ERROR_MASK 16 +#define TEST_BLOCK_SIZE 2048 + +const struct { + const char *name; + bd_size_t (BlockDevice::*method)() const; +} ATTRS[] = { + {"read size", &BlockDevice::get_read_size}, + {"program size", &BlockDevice::get_program_size}, + {"erase size", &BlockDevice::get_erase_size}, + {"total size", &BlockDevice::size}, +}; + +void test_read_write() { + SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); + + int err = sd.init(); + TEST_ASSERT_EQUAL(0, err); + + err = sd.frequency(8000000); + TEST_ASSERT_EQUAL(0, err); + + for (unsigned a = 0; a < sizeof(ATTRS)/sizeof(ATTRS[0]); a++) { + static const char *prefixes[] = {"", "k", "M", "G"}; + for (int i = 3; i >= 0; i--) { + bd_size_t size = (sd.*ATTRS[a].method)(); + if (size >= (1ULL << 10*i)) { + printf("%s: %llu%sbytes (%llubytes)\n", + ATTRS[a].name, size >> 10*i, prefixes[i], size); + break; + } + } + } + + bd_size_t erase_size = sd.get_erase_size(); + bd_size_t block_size = erase_size > TEST_BLOCK_SIZE ? erase_size : TEST_BLOCK_SIZE; + + uint8_t *write_block = new uint8_t[block_size]; + uint8_t *read_block = new uint8_t[block_size]; + uint8_t *error_mask = new uint8_t[TEST_ERROR_MASK]; + unsigned addrwidth = ceil(log(float(sd.size()-1)) / log(float(16)))+1; + + for (int b = 0; b < TEST_BLOCK_COUNT; b++) { + // Find a random block + bd_addr_t block = (rand()*block_size) % sd.size(); + + // Use next random number as temporary seed to keep + // the address progressing in the pseudorandom sequence + unsigned seed = rand(); + + // Fill with random sequence + srand(seed); + for (bd_size_t i = 0; i < block_size; i++) { + write_block[i] = 0xff & rand(); + } + + // Write, sync, and read the block + printf("test %0*llx:%llu...\n", addrwidth, block, block_size); + + err = sd.trim(block, block_size); + TEST_ASSERT_EQUAL(0, err); + + err = sd.program(write_block, block, block_size); + TEST_ASSERT_EQUAL(0, err); + + printf("write %0*llx:%llu ", addrwidth, block, block_size); + for (int i = 0; i < 16; i++) { + printf("%02x", write_block[i]); + } + printf("...\n"); + + err = sd.read(read_block, block, block_size); + TEST_ASSERT_EQUAL(0, err); + + printf("read %0*llx:%llu ", addrwidth, block, block_size); + for (int i = 0; i < 16; i++) { + printf("%02x", read_block[i]); + } + printf("...\n"); + + // Find error mask for debugging + memset(error_mask, 0, TEST_ERROR_MASK); + bd_size_t error_scale = block_size / (TEST_ERROR_MASK*8); + + srand(seed); + for (bd_size_t i = 0; i < TEST_ERROR_MASK*8; i++) { + for (bd_size_t j = 0; j < error_scale; j++) { + if ((0xff & rand()) != read_block[i*error_scale + j]) { + error_mask[i/8] |= 1 << (i%8); + } + } + } + + printf("error %0*llx:%llu ", addrwidth, block, block_size); + for (int i = 0; i < 16; i++) { + printf("%02x", error_mask[i]); + } + printf("\n"); + + // Check that the data was unmodified + srand(seed); + for (bd_size_t i = 0; i < block_size; i++) { + TEST_ASSERT_EQUAL(0xff & rand(), read_block[i]); + } + } + + err = sd.deinit(); + TEST_ASSERT_EQUAL(0, err); +} + +// Test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(120, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Testing read write random blocks", test_read_write), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/filesystem/basic/basic.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/filesystem/basic/basic.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,933 @@ +/* + * mbed Microcontroller Library + * Copyright (c) 2006-2016 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +/* The following copyright notice is reproduced from the glibc project + * REF_LICENCE_GLIBC + * + * Copyright (C) 1991, 1992 Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * The GNU C Library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with the GNU C Library; see the file COPYING.LIB. If + * not, write to the Free Software Foundation, Inc., 675 Mass Ave, + * Cambridge, MA 02139, USA. + */ + + +/** @file basic.cpp POSIX File API (stdio) test cases + * + * Consult the documentation under the test-case functions for + * a description of the individual test case. + * + * this file includes ports for the mbed 2 test cases from the following locations: + * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/dir_sd/main.cpp. + * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/file/main.cpp. + * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/sd/main.cpp + * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_handle/main.cpp + * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp. + */ + +#include "mbed.h" +#include "mbed_config.h" +#include "FATFileSystem.h" +#include "SDBlockDevice.h" +#include "test_env.h" +#include "fsfat_debug.h" +#include "fsfat_test.h" +#include "utest/utest.h" +#include "unity/unity.h" +#include "greentea-client/test_env.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <algorithm> +/* retarget.h is included after errno.h so symbols are mapped to + * consistent values for all toolchains */ +#include "platform/mbed_retarget.h" + +using namespace utest::v1; + +/* DEVICE_SPI + * This symbol is defined in targets.json if the target has a SPI interface, which is required for SDCard support. + * + * MBED_CONF_APP_FSFAT_SDCARD_INSTALLED + * For testing purposes, an SDCard must be installed on the target for the test cases in this file to succeed. + * If the target has an SD card installed then the MBED_CONF_APP_FSFAT_SDCARD_INSTALLED will be generated + * from the mbed_app.json, which includes the line + * { + * "config": { + * "UART_RX": "D0", + * <<< lines removed >>> + * "DEVICE_SPI": 1, + * "MBED_CONF_APP_FSFAT_SDCARD_INSTALLED": 1 + * }, + * <<< lines removed >>> + */ +#if defined(DEVICE_SPI) && ( defined(MBED_CONF_APP_FSFAT_SDCARD_INSTALLED) || (MBED_CONF_SD_FSFAT_SDCARD_INSTALLED)) + +#define FSFAT_BASIC_TEST_00 fsfat_basic_test_00 +#define FSFAT_BASIC_TEST_01 fsfat_basic_test_01 +#define FSFAT_BASIC_TEST_02 fsfat_basic_test_02 +#define FSFAT_BASIC_TEST_03 fsfat_basic_test_03 +#define FSFAT_BASIC_TEST_04 fsfat_basic_test_04 +#define FSFAT_BASIC_TEST_05 fsfat_basic_test_05 +#define FSFAT_BASIC_TEST_06 fsfat_basic_test_06 +#define FSFAT_BASIC_TEST_07 fsfat_basic_test_07 +#define FSFAT_BASIC_TEST_08 fsfat_basic_test_08 +#define FSFAT_BASIC_TEST_09 fsfat_basic_test_09 +#define FSFAT_BASIC_TEST_10 fsfat_basic_test_10 + +#define FSFAT_BASIC_MSG_BUF_SIZE 256 +#define FSFAT_BASIC_TEST_05_TEST_STRING "Hello World!" + +static const char *sd_file_path = "/sd/out.txt"; +static const char *sd_mount_pt = "sd"; +static const int FSFAT_BASIC_DATA_SIZE = 256; +static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE]; +static char fsfat_basic_buffer[1024]; +static const int FSFAT_BASIC_KIB_RW = 128; +static Timer fsfat_basic_timer; +static const char *fsfat_basic_bin_filename = "/sd/testfile.bin"; +static const char *fsfat_basic_bin_filename_test_08 = "testfile.bin"; +static const char *fsfat_basic_bin_filename_test_10 = "0:testfile.bin"; + + + +SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +FATFileSystem fs(sd_mount_pt, &sd); + +#define FSFAT_BASIC_MSG(_buf, _max_len, _fmt, ...) \ + do \ + { \ + snprintf((_buf), (_max_len), (_fmt), __VA_ARGS__); \ + }while(0); + +/** @brief fopen test case + * + * - open a file + * - generate random data items, write the item to the file and store a coy in a buffer for later use. + * - close the file. + * - open the file. + * - read the data items from the file and check they are the same as write. + * - close the file. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_00() +{ + + uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 }; + bool read_result = false; + bool write_result = false; + + // Fill data_written buffer with random data + // Write these data into the file + FSFAT_FENTRYLOG("%s:entered\n", __func__); + { + FSFAT_DBGLOG("%s:SD: Writing ... ", __func__); + FILE *f = fopen(sd_file_path, "w"); + if (f) { + for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) { + data_written[i] = rand() % 0XFF; + fprintf(f, "%c", data_written[i]); + } + write_result = true; + fclose(f); + } + FSFAT_DBGLOG("[%s]\n", write_result ? "OK" : "FAIL"); + } + TEST_ASSERT_MESSAGE(write_result == true, "Error: write_result is set to false."); + + // Read back the data from the file and store them in data_read + { + FSFAT_DBGLOG("%s:SD: Reading data ... ", __func__); + FILE *f = fopen(sd_file_path, "r"); + if (f) { + read_result = true; + for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) { + uint8_t data = fgetc(f); + if (data != data_written[i]) { + read_result = false; + break; + } + } + fclose(f); + } + FSFAT_DBGLOG("[%s]\n", read_result ? "OK" : "FAIL"); + } + TEST_ASSERT_MESSAGE(read_result == true, "Error: read_result is set to false."); + return CaseNext; +} + + +/** @brief test-fseek.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_01() +{ + FILE *fp, *fp1; + int i, j; + int ret = 0; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + fp = fopen (sd_file_path, "w+"); + if (fp == NULL) { + FSFAT_DBGLOG("errno=%d\n", errno); + TEST_ASSERT_MESSAGE(false, "error"); + return CaseNext; + } + + for (i = 0; i < 256; i++) { + putc (i, fp); + } + /* FIXME: freopen() should open the specified file closing the first stream. As can be seen from the + * code below, the old file descriptor fp can still be used, and this should not happen. + */ + fp1 = freopen (sd_file_path, "r", fp); + TEST_ASSERT_MESSAGE(fp1 == fp, "Error: cannot open file for reading"); + + for (i = 1; i <= 255; i++) { + ret = fseek (fp, (long) -i, SEEK_END); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s:Error: fseek() failed (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + + if ((j = getc (fp)) != 256 - i) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: SEEK_END failed (j=%d)\n", __func__, j); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + ret = fseek (fp, (long) i, SEEK_SET); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + + if ((j = getc (fp)) != i) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (j=%d).\n", __func__, j); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + } + if ((ret = fseek (fp, (long) i, SEEK_SET))) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + } + if ((ret = fseek (fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR))) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_CUR (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + } + if ((j = getc (fp)) != (i >= 128 ? i - 128 : i + 128)) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_CUR (j=%d).\n", __func__, j); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + } + } + fclose (fp); + remove(sd_file_path); + return CaseNext; +} + + +/** @brief test_rdwr.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC. + * + * WARNING: this test does not currently work. See WARNING comments below. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_02() +{ + static const char hello[] = "Hello, world.\n"; + static const char replace[] = "Hewwo, world.\n"; + static const size_t replace_from = 2, replace_to = 4; + const char *filename = sd_file_path; + char buf[BUFSIZ]; + FILE *f; + int lose = 0; + int32_t ret = 0; + char *rets = NULL; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + f = fopen(filename, "w+"); + if (f == NULL) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot open file for writing (filename=%s).\n", __func__, filename); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + + ret = fputs(hello, f); + if (ret == EOF) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, hello); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + + rewind(f); + rets = fgets(buf, sizeof(buf), f); + if (rets == NULL) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fgets() failed to get string from file (filename=%s).\n", __func__, filename); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + rets = NULL; + + rewind(f); + ret = fputs(buf, f); + if (ret == EOF) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, buf); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + + rewind(f); + { + register size_t i; + for (i = 0; i < replace_from; ++i) + { + int c = getc(f); + if (c == EOF) + { + FSFAT_DBGLOG("EOF at %u.\n", i); + lose = 1; + break; + } + else if (c != hello[i]) + { + FSFAT_DBGLOG("Got '%c' instead of '%c' at %u.\n", + (unsigned char) c, hello[i], i); + lose = 1; + break; + } + } + } + /* WARNING: printf("%s: here1. (lose = %d)\n", __func__, lose); */ + { + long int where = ftell(f); + if (where == replace_from) + { + register size_t i; + for (i = replace_from; i < replace_to; ++i) { + if (putc(replace[i], f) == EOF) { + FSFAT_DBGLOG("putc('%c') got %s at %u.\n", + replace[i], strerror(errno), i); + lose = 1; + break; + } + /* WARNING: The problem seems to be that putc() is not writing the 'w' chars into the file + * FSFAT_DBGLOG("%s: here1.5. (char = %c, char as int=%d, ret=%d) \n", __func__, replace[i], (int) replace[i], ret); + */ + } + } + else if (where == -1L) + { + FSFAT_DBGLOG("ftell got %s (should be at %u).\n", + strerror(errno), replace_from); + lose = 1; + } + else + { + FSFAT_DBGLOG("ftell returns %ld; should be %u.\n", where, replace_from); + lose = 1; + } + } + + if (!lose) + { + rewind(f); + memset(buf, 0, BUFSIZ); + if (fgets(buf, sizeof(buf), f) == NULL) + { + FSFAT_DBGLOG("fgets got %s.\n", strerror(errno)); + lose = 1; + } + else if (strcmp(buf, replace)) + { + FSFAT_DBGLOG("Read \"%s\" instead of \"%s\".\n", buf, replace); + lose = 1; + } + } + + if (lose) { + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Test Failed. Losing file (filename=%s).\n", __func__, filename); + TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g); + } + remove(filename); + return CaseNext; +} + +/** @brief temptest.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC. + * + * tmpnam() is currently not implemented + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_03() +{ + char *fn = NULL; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + fn = tmpnam((char *) NULL); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: appeared to generate a filename when function is not implemented.\n", __func__); + TEST_ASSERT_MESSAGE(fn == NULL, fsfat_basic_msg_g); + return CaseNext; +} + + +static bool fsfat_basic_fileno_check(const char *name, FILE *stream, int fd) +{ + /* ARMCC stdio.h currently does not define fileno() */ +#ifndef __ARMCC_VERSION + int sfd = fileno (stream); + FSFAT_DBGLOG("(fileno (%s) = %d) %c= %d\n", name, sfd, sfd == fd ? '=' : '!', fd); + + if (sfd == fd) { + return true; + } else { + return false; + } +#else + /* For ARMCC behave as though test had passed. */ + return true; +#endif /* __ARMCC_VERSION */ +} + +/* defines for next test case */ +#ifndef STDIN_FILENO +#define STDIN_FILENO 0 +#endif + +#ifndef STDOUT_FILENO +#define STDOUT_FILENO 1 +#endif + +#ifndef STDERR_FILENO +#define STDERR_FILENO 2 +#endif + + +/** @brief tst-fileno.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC. + * + * WARNING: this test does not currently work. See WARNING comments below. + * + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_04() +{ + /* ARMCC stdio.h currently does not define fileno() */ +#ifndef __ARMCC_VERSION + int ret = -1; + ret = fsfat_basic_fileno_check("stdin", stdin, STDIN_FILENO); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: stdin does not have expected file number (expected=%d, fileno=%d.\n", __func__, (int) stdin, fileno(stdin)); + TEST_ASSERT_MESSAGE(ret == true, fsfat_basic_msg_g); + + ret = fsfat_basic_fileno_check("stdout", stdout, STDOUT_FILENO); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: stdout does not have expected file number (expected=%d, fileno=%d.\n", __func__, (int) stdout, fileno(stdout)); + TEST_ASSERT_MESSAGE(ret == true, fsfat_basic_msg_g); + + ret = fsfat_basic_fileno_check("stderr", stderr, STDERR_FILENO); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: stderr does not have expected file number (expected=%d, fileno=%d.\n", __func__, (int) stderr, fileno(stderr)); + TEST_ASSERT_MESSAGE(ret == true, fsfat_basic_msg_g); +#endif /* __ARMCC_VERSION */ + return CaseNext; +} + + +/** @brief basic test to opendir() on a directory. + * + * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/dir_sd/main.cpp. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_05() +{ + FILE *f; + const char *str = FSFAT_BASIC_TEST_05_TEST_STRING; + int ret = 0; + + FSFAT_DBGLOG("%s:Write files\n", __func__); + char filename[32]; + for (int i = 0; i < 10; i++) { + sprintf(filename, "/sd/test_%d.txt", i); + FSFAT_DBGLOG("Creating file: %s\n", filename); + f = fopen(filename, "w"); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fopen() failed.\n", __func__); + TEST_ASSERT_MESSAGE(f != NULL, fsfat_basic_msg_g); + + ret = fprintf(f, str); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: writing file.\n", __func__); + TEST_ASSERT_MESSAGE(ret == (int) strlen(str), fsfat_basic_msg_g); + + ret = fclose(f); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fclose() failed.\n", __func__); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + } + + FSFAT_DBGLOG("%s:List files:\n", __func__); + DIR *d = opendir("/sd"); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: opendir() failed.\n", __func__); + TEST_ASSERT_MESSAGE(d != NULL, fsfat_basic_msg_g); + + struct dirent *p; + while ((p = readdir(d)) != NULL) + FSFAT_DBGLOG("%s\n", p->d_name); + closedir(d); + + return CaseNext; +} + + +/** @brief basic test to write a file to sd card, and read it back again + * + * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/file/main.cpp. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_06() +{ + int ret = -1; + char mac[16]; + mbed_mac_address(mac); + FSFAT_DBGLOG("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + FILE *f; + const char *str = FSFAT_BASIC_TEST_05_TEST_STRING; + int str_len = strlen(FSFAT_BASIC_TEST_05_TEST_STRING); + + f = fopen(sd_file_path, "w"); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fopen() failed.\n", __func__); + TEST_ASSERT_MESSAGE(f != NULL, fsfat_basic_msg_g); + + ret = fprintf(f, str); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: writing file.\n", __func__); + TEST_ASSERT_MESSAGE(ret == (int) strlen(str), fsfat_basic_msg_g); + + ret = fclose(f); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fclose() failed.\n", __func__); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + + // Read + f = fopen(sd_file_path, "r"); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fopen() failed.\n", __func__); + TEST_ASSERT_MESSAGE(f != NULL, fsfat_basic_msg_g); + + int n = fread(fsfat_basic_buffer, sizeof(unsigned char), str_len, f); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fread() failed.\n", __func__); + TEST_ASSERT_MESSAGE(n == str_len, fsfat_basic_msg_g); + + ret = fclose(f); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fclose() failed.\n", __func__); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + + return CaseNext; +} + + +/** @brief basic test to write a file to sd card. + * + * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd/main.cpp. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_07() +{ + uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 }; + + // Fill data_written buffer with random data + // Write these data into the file + bool write_result = false; + { + FSFAT_DBGLOG("%s:SD: Writing ... ", __func__); + FILE *f = fopen(sd_file_path, "w"); + if (f) { + for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) { + data_written[i] = rand() % 0XFF; + fprintf(f, "%c", data_written[i]); + } + write_result = true; + fclose(f); + } + FSFAT_DBGLOG("[%s]\n", write_result ? "OK" : "FAIL"); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: unexpected write failure.\n", __func__); + TEST_ASSERT_MESSAGE(write_result == true, fsfat_basic_msg_g); + } + + // Read back the data from the file and store them in data_read + bool read_result = false; + { + FSFAT_DBGLOG("%s:SD: Reading data ... ", __func__); + FILE *f = fopen(sd_file_path, "r"); + if (f) { + read_result = true; + for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) { + uint8_t data = fgetc(f); + if (data != data_written[i]) { + read_result = false; + break; + } + } + fclose(f); + } + FSFAT_DBGLOG("[%s]\n", read_result ? "OK" : "FAIL"); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: unexpected read failure.\n", __func__); + TEST_ASSERT_MESSAGE(read_result == true, fsfat_basic_msg_g); + } + return CaseNext; +} + + +static bool fsfat_basic_test_file_write_fhandle(const char *filename, const int kib_rw) +{ + int ret = -1; + File file; + + ret = file.open(&fs, filename, O_WRONLY | O_CREAT | O_TRUNC); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + + int byte_write = 0; + fsfat_basic_timer.start(); + for (int i = 0; i < kib_rw; i++) { + ret = file.write(fsfat_basic_buffer, sizeof(fsfat_basic_buffer)); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__); + TEST_ASSERT_MESSAGE(ret == sizeof(fsfat_basic_buffer), fsfat_basic_msg_g); + byte_write++; + } + fsfat_basic_timer.stop(); + file.close(); +#ifdef FSFAT_DEBUG + double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0; + double speed = kib_rw / test_time_sec; + FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed); +#endif + fsfat_basic_timer.reset(); + return true; +} + + +static bool fsfat_basic_test_file_read_fhandle(const char *filename, const int kib_rw) +{ + int ret = -1; + File file; + ret = file.open(&fs, filename, O_RDONLY); + + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g); + + fsfat_basic_timer.start(); + int byte_read = 0; + while (file.read(fsfat_basic_buffer, sizeof(fsfat_basic_buffer)) == sizeof(fsfat_basic_buffer)) { + byte_read++; + } + fsfat_basic_timer.stop(); + file.close(); +#ifdef FSFAT_DEBUG + double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0; + double speed = kib_rw / test_time_sec; + FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed); +#endif + fsfat_basic_timer.reset(); + return true; +} + + +static char fsfat_basic_test_random_char() +{ + return rand() % 100; +} + + +/** @brief basic sd card performance test + * + * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_handle/main.cpp. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_08() +{ + // Test header + FSFAT_DBGLOG("\n%s:SD Card FileHandle Performance Test\n", __func__); + FSFAT_DBGLOG("File name: %s\n", fsfat_basic_bin_filename); + FSFAT_DBGLOG("Buffer size: %d KiB\n", (FSFAT_BASIC_KIB_RW * sizeof(fsfat_basic_buffer)) / 1024); + + // Initialize buffer + srand(0); + char *buffer_end = fsfat_basic_buffer + sizeof(fsfat_basic_buffer); + std::generate (fsfat_basic_buffer, buffer_end, fsfat_basic_test_random_char); + + bool result = true; + for (;;) { + FSFAT_DBGLOG("%s:Write test...\n", __func__); + if (fsfat_basic_test_file_write_fhandle(fsfat_basic_bin_filename_test_08, FSFAT_BASIC_KIB_RW) == false) { + result = false; + break; + } + + FSFAT_DBGLOG("%s:Read test...\n", __func__); + if (fsfat_basic_test_file_read_fhandle(fsfat_basic_bin_filename_test_08, FSFAT_BASIC_KIB_RW) == false) { + result = false; + break; + } + break; + } + TEST_ASSERT_MESSAGE(result == true, "something went wrong"); + return CaseNext; +} + + +bool fsfat_basic_test_sf_file_write_stdio(const char *filename, const int kib_rw) +{ + int ret = -1; + FILE* file = fopen(filename, "w"); + + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__); + TEST_ASSERT_MESSAGE(file != NULL, fsfat_basic_msg_g); + + int byte_write = 0; + fsfat_basic_timer.start(); + for (int i = 0; i < kib_rw; i++) { + ret = fwrite(fsfat_basic_buffer, sizeof(char), sizeof(fsfat_basic_buffer), file); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__); + TEST_ASSERT_MESSAGE(ret == sizeof(fsfat_basic_buffer), fsfat_basic_msg_g); + byte_write++; + } + fsfat_basic_timer.stop(); + fclose(file); +#ifdef FSFAT_DEBUG + double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0; + double speed = kib_rw / test_time_sec; + FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed); +#endif + fsfat_basic_timer.reset(); + return true; +} + + +bool fsfat_basic_test_sf_file_read_stdio(const char *filename, const int kib_rw) +{ + FILE* file = fopen(filename, "r"); + + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__); + TEST_ASSERT_MESSAGE(file != NULL, fsfat_basic_msg_g); + fsfat_basic_timer.start(); + int byte_read = 0; + while (fread(fsfat_basic_buffer, sizeof(char), sizeof(fsfat_basic_buffer), file) == sizeof(fsfat_basic_buffer)) { + byte_read++; + } + fsfat_basic_timer.stop(); + fclose(file); +#ifdef FSFAT_DEBUG + double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0; + double speed = kib_rw / test_time_sec; + FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed); +#endif + fsfat_basic_timer.reset(); + return true; +} + + +/** @brief basic test to write a file to sd card. + * + * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_09() +{ + // Test header + FSFAT_DBGLOG("\n%s:SD Card Stdio Performance Test\n", __func__); + FSFAT_DBGLOG("File name: %s\n", fsfat_basic_bin_filename); + FSFAT_DBGLOG("Buffer size: %d KiB\n", (FSFAT_BASIC_KIB_RW * sizeof(fsfat_basic_buffer)) / 1024); + + // Initialize buffer + srand(0); + char *buffer_end = fsfat_basic_buffer + sizeof(fsfat_basic_buffer); + std::generate (fsfat_basic_buffer, buffer_end, fsfat_basic_test_random_char); + + bool result = true; + for (;;) { + FSFAT_DBGLOG("%s:Write test...\n", __func__); + if (fsfat_basic_test_sf_file_write_stdio(fsfat_basic_bin_filename, FSFAT_BASIC_KIB_RW) == false) { + result = false; + break; + } + + FSFAT_DBGLOG("%s:Read test...\n", __func__); + if (fsfat_basic_test_sf_file_read_stdio(fsfat_basic_bin_filename, FSFAT_BASIC_KIB_RW) == false) { + result = false; + break; + } + break; + } + TEST_ASSERT_MESSAGE(result == true, "Expected true result not found"); + return CaseNext; +} + + +bool fsfat_basic_test_file_write_fatfs(const char *filename, const int kib_rw) +{ + FIL file; + FRESULT res = f_open(&file, filename, FA_WRITE | FA_CREATE_ALWAYS); + + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__); + TEST_ASSERT_MESSAGE(res == FR_OK, fsfat_basic_msg_g); + + int byte_write = 0; + unsigned int bytes = 0; + fsfat_basic_timer.start(); + for (int i = 0; i < kib_rw; i++) { + res = f_write(&file, fsfat_basic_buffer, sizeof(fsfat_basic_buffer), &bytes); + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__); + TEST_ASSERT_MESSAGE(res == FR_OK, fsfat_basic_msg_g); + byte_write++; + } + fsfat_basic_timer.stop(); + f_close(&file); +#ifdef FSFAT_DEBUG + double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0; + double speed = kib_rw / test_time_sec; + FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed); +#endif + fsfat_basic_timer.reset(); + return true; +} + +bool fsfat_basic_test_file_read_fatfs(const char *filename, const int kib_rw) +{ + FIL file; + FRESULT res = f_open(&file, filename, FA_READ | FA_OPEN_EXISTING); + + FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__); + TEST_ASSERT_MESSAGE(res == FR_OK, fsfat_basic_msg_g); + + fsfat_basic_timer.start(); + int byte_read = 0; + unsigned int bytes = 0; + do { + res = f_read(&file, fsfat_basic_buffer, sizeof(fsfat_basic_buffer), &bytes); + byte_read++; + } while (res == FR_OK && bytes == sizeof(fsfat_basic_buffer)); + fsfat_basic_timer.stop(); + f_close(&file); +#ifdef FSFAT_DEBUG + double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0; + double speed = kib_rw / test_time_sec; + FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed); +#endif + fsfat_basic_timer.reset(); + return true; +} + +/** @brief basic test to write a file to sd card. + * + * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_basic_test_10() +{ + // Test header + FSFAT_DBGLOG("\n%sSD Card FatFS Performance Test\n", __func__); + FSFAT_DBGLOG("File name: %s\n", fsfat_basic_bin_filename_test_10); + FSFAT_DBGLOG("Buffer size: %d KiB\n", (FSFAT_BASIC_KIB_RW * sizeof(fsfat_basic_buffer)) / 1024); + + // Initialize buffer + srand(1); + char *buffer_end = fsfat_basic_buffer + sizeof(fsfat_basic_buffer); + std::generate (fsfat_basic_buffer, buffer_end, fsfat_basic_test_random_char); + + bool result = true; + for (;;) { + FSFAT_DBGLOG("%s:Write test...\n", __func__); + if (fsfat_basic_test_file_write_fatfs(fsfat_basic_bin_filename_test_10, FSFAT_BASIC_KIB_RW) == false) { + result = false; + break; + } + + FSFAT_DBGLOG("%s:Read test...\n", __func__); + if (fsfat_basic_test_file_read_fatfs(fsfat_basic_bin_filename_test_10, FSFAT_BASIC_KIB_RW) == false) { + result = false; + break; + } + break; + } + TEST_ASSERT_MESSAGE(result == true, "Expected true result not found"); + return CaseNext; +} + +#else + +#define FSFAT_BASIC_TEST_00 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_01 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_02 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_03 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_04 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_05 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_06 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_07 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_08 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_09 fsfat_basic_test_dummy +#define FSFAT_BASIC_TEST_10 fsfat_basic_test_dummy + + +/** @brief fsfat_basic_test_dummy Dummy test case for testing when platform doesnt have an SDCard installed. + * + * @return success always + */ +static control_t fsfat_basic_test_dummy() +{ + printf("Null test\n"); + return CaseNext; +} + +#endif + +utest::v1::status_t greentea_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(300, "default_auto"); + return greentea_test_setup_handler(number_of_cases); +} + + +Case cases[] = { + /* 1 2 3 4 5 6 7 */ + /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ + Case("FSFAT_BASIC_TEST_00: fopen()/fgetc()/fprintf()/fclose() test.", FSFAT_BASIC_TEST_00), + Case("FSFAT_BASIC_TEST_01: fopen()/fseek()/fclose() test.", FSFAT_BASIC_TEST_01), + /* WARNING: Test case not working but currently not required for PAL support + * Case("FSFAT_BASIC_TEST_02: fopen()/fgets()/fputs()/ftell()/rewind()/remove() test.", FSFAT_BASIC_TEST_02) */ + Case("FSFAT_BASIC_TEST_03: tmpnam() test.", FSFAT_BASIC_TEST_03), + Case("FSFAT_BASIC_TEST_04: fileno() test.", FSFAT_BASIC_TEST_04), + Case("FSFAT_BASIC_TEST_05: opendir() basic test.", FSFAT_BASIC_TEST_05), + Case("FSFAT_BASIC_TEST_06: fread()/fwrite() file to sdcard.", FSFAT_BASIC_TEST_06), + Case("FSFAT_BASIC_TEST_07: sdcard fwrite() file test.", FSFAT_BASIC_TEST_07), + Case("FSFAT_BASIC_TEST_08: FATFileSystem::read()/write() test.", FSFAT_BASIC_TEST_08), + Case("FSFAT_BASIC_TEST_09: POSIX FILE API fread()/fwrite() test.", FSFAT_BASIC_TEST_09), + Case("FSFAT_BASIC_TEST_10: ChanFS read()/write()) test.", FSFAT_BASIC_TEST_10), +}; + + +/* Declare your test specification with a custom setup handler */ +Specification specification(greentea_setup, cases); + +int main() +{ + return !Harness::run(specification); +}
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/filesystem/dirs/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/filesystem/dirs/main.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,472 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" +#include <stdlib.h> +#include <errno.h> + +using namespace utest::v1; + +// test configuration +#ifndef MBED_TEST_FILESYSTEM +#define MBED_TEST_FILESYSTEM FATFileSystem +#endif + +#ifndef MBED_TEST_FILESYSTEM_DECL +#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs") +#endif + +#ifndef MBED_TEST_BLOCKDEVICE +#define MBED_TEST_BLOCKDEVICE SDBlockDevice +#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +#endif + +#ifndef MBED_TEST_BLOCKDEVICE_DECL +#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd +#endif + +#ifndef MBED_TEST_FILES +#define MBED_TEST_FILES 4 +#endif + +#ifndef MBED_TEST_DIRS +#define MBED_TEST_DIRS 4 +#endif + +#ifndef MBED_TEST_BUFFER +#define MBED_TEST_BUFFER 8192 +#endif + +#ifndef MBED_TEST_TIMEOUT +#define MBED_TEST_TIMEOUT 120 +#endif + + +// declarations +#define STRINGIZE(x) STRINGIZE2(x) +#define STRINGIZE2(x) #x +#define INCLUDE(x) STRINGIZE(x.h) + +#include INCLUDE(MBED_TEST_FILESYSTEM) +#include INCLUDE(MBED_TEST_BLOCKDEVICE) + +MBED_TEST_FILESYSTEM_DECL; +MBED_TEST_BLOCKDEVICE_DECL; + +Dir dir[MBED_TEST_DIRS]; +File file[MBED_TEST_FILES]; +DIR *dd[MBED_TEST_DIRS]; +FILE *fd[MBED_TEST_FILES]; +struct dirent ent; +struct dirent *ed; +size_t size; +uint8_t buffer[MBED_TEST_BUFFER]; +uint8_t rbuffer[MBED_TEST_BUFFER]; +uint8_t wbuffer[MBED_TEST_BUFFER]; + + +// tests + +void test_directory_tests() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = MBED_TEST_FILESYSTEM::format(&bd); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_root_directory() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "/"); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_directory_creation() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("potato", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_file_creation() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "burito", O_CREAT | O_WRONLY); + TEST_ASSERT_EQUAL(0, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void dir_file_check(char *list[], uint32_t elements) { + int res; + while(1) { + res = dir[0].read(&ent); + if (0 == res) { + break; + } + for (int i = 0; i < elements ; i++) { + res = strcmp(ent.d_name, list[i]); + if (0 == res) { + res = ent.d_type; + if ((DT_DIR != res) && (DT_REG != res)) { + TEST_ASSERT(1); + } + break; + } + else if( i == elements) { + TEST_ASSERT_EQUAL(0, res); + } + } + } +} + +void test_directory_iteration() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "/"); + TEST_ASSERT_EQUAL(0, res); + char *dir_list[] = {"potato", "burito", ".", ".."}; + + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); + + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_directory_failures() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("potato", 0777); + TEST_ASSERT_EQUAL(-EEXIST, res); + res = dir[0].open(&fs, "tomato"); + TEST_ASSERT_EQUAL(-ENOENT, res); + res = dir[0].open(&fs, "burito"); + TEST_ASSERT_NOT_EQUAL(0, res); + res = file[0].open(&fs, "tomato", O_RDONLY); + TEST_ASSERT_EQUAL(-ENOENT, res); + res = file[0].open(&fs, "potato", O_RDONLY); + TEST_ASSERT_NOT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_nested_directories() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("potato/baked", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("potato/sweet", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("potato/fried", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "/"); + TEST_ASSERT_EQUAL(0, res); + char *dir_list[] = {"potato", "baked", "sweet", "fried", ".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_multi_block_directory() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("cactus", 0777); + TEST_ASSERT_EQUAL(0, res); + for (int i = 0; i < 128; i++) { + sprintf((char*)buffer, "cactus/test%d", i); + res = fs.mkdir((char*)buffer, 0777); + TEST_ASSERT_EQUAL(0, res); + } + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "cactus"); + TEST_ASSERT_EQUAL(0, res); + +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + char *dir_list[] = {".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); +#endif + + for (int i = 0; i < 128; i++) { + sprintf((char*)buffer, "test%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + } + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_directory_remove() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.remove("potato"); + TEST_ASSERT_NOT_EQUAL(0, res); + res = fs.remove("potato/sweet"); + TEST_ASSERT_EQUAL(0, res); + res = fs.remove("potato/baked"); + TEST_ASSERT_EQUAL(0, res); + res = fs.remove("potato/fried"); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "potato"); + TEST_ASSERT_EQUAL(0, res); + +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + char *dir_list[] = {".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); +#endif + + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.remove("potato"); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "/"); + TEST_ASSERT_EQUAL(0, res); + char *dir_list[] = {"burito", "cactus", ".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_directory_rename() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("coldpotato", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("coldpotato/baked", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("coldpotato/sweet", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("coldpotato/fried", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.rename("coldpotato", "hotpotato"); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "hotpotato"); + TEST_ASSERT_EQUAL(0, res); + char *dir_list[] = {"baked", "sweet", "fried", ".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("warmpotato", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("warmpotato/mushy", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.rename("hotpotato", "warmpotato"); + TEST_ASSERT_NOT_EQUAL(0, res); + res = fs.remove("warmpotato/mushy"); + TEST_ASSERT_EQUAL(0, res); + res = fs.remove("warmpotato"); + TEST_ASSERT_EQUAL(0, res); + res = fs.rename("hotpotato", "warmpotato"); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "warmpotato"); + TEST_ASSERT_EQUAL(0, res); + char *dir_list[] = {"baked", "sweet", "fried", ".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("coldpotato", 0777); + TEST_ASSERT_EQUAL(0, res); + res = fs.rename("warmpotato/baked", "coldpotato/baked"); + TEST_ASSERT_EQUAL(0, res); + res = fs.rename("warmpotato/sweet", "coldpotato/sweet"); + TEST_ASSERT_EQUAL(0, res); + res = fs.rename("warmpotato/fried", "coldpotato/fried"); + TEST_ASSERT_EQUAL(0, res); + res = fs.remove("coldpotato"); + TEST_ASSERT_NOT_EQUAL(0, res); + res = fs.remove("warmpotato"); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "coldpotato"); + TEST_ASSERT_EQUAL(0, res); + char *dir_list[] = {"baked", "sweet", "fried", ".", ".."}; + dir_file_check(dir_list, (sizeof(dir_list)/sizeof(dir_list[0]))); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + + + +// test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Directory tests", test_directory_tests), + Case("Root directory", test_root_directory), + Case("Directory creation", test_directory_creation), + Case("File creation", test_file_creation), + Case("Directory iteration", test_directory_iteration), + Case("Directory failures", test_directory_failures), + Case("Nested directories", test_nested_directories), + Case("Multi-block directory", test_multi_block_directory), + Case("Directory remove", test_directory_remove), + Case("Directory rename", test_directory_rename), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/filesystem/files/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/filesystem/files/main.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,314 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" +#include <stdlib.h> +#include <errno.h> + +using namespace utest::v1; + +// test configuration +#ifndef MBED_TEST_FILESYSTEM +#define MBED_TEST_FILESYSTEM FATFileSystem +#endif + +#ifndef MBED_TEST_FILESYSTEM_DECL +#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs") +#endif + +#ifndef MBED_TEST_BLOCKDEVICE +#define MBED_TEST_BLOCKDEVICE SDBlockDevice +#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +#endif + +#ifndef MBED_TEST_BLOCKDEVICE_DECL +#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd +#endif + +#ifndef MBED_TEST_FILES +#define MBED_TEST_FILES 4 +#endif + +#ifndef MBED_TEST_DIRS +#define MBED_TEST_DIRS 4 +#endif + +#ifndef MBED_TEST_BUFFER +#define MBED_TEST_BUFFER 8192 +#endif + +#ifndef MBED_TEST_TIMEOUT +#define MBED_TEST_TIMEOUT 120 +#endif + + +// declarations +#define STRINGIZE(x) STRINGIZE2(x) +#define STRINGIZE2(x) #x +#define INCLUDE(x) STRINGIZE(x.h) + +#include INCLUDE(MBED_TEST_FILESYSTEM) +#include INCLUDE(MBED_TEST_BLOCKDEVICE) + +MBED_TEST_FILESYSTEM_DECL; +MBED_TEST_BLOCKDEVICE_DECL; + +Dir dir[MBED_TEST_DIRS]; +File file[MBED_TEST_FILES]; +DIR *dd[MBED_TEST_DIRS]; +FILE *fd[MBED_TEST_FILES]; +struct dirent ent; +struct dirent *ed; +size_t size; +uint8_t buffer[MBED_TEST_BUFFER]; +uint8_t rbuffer[MBED_TEST_BUFFER]; +uint8_t wbuffer[MBED_TEST_BUFFER]; + +static char file_counter = 0; +const char *filenames[] = {"smallavacado", "mediumavacado", "largeavacado", + "blockfile", "bigblockfile", "hello", ".", ".."}; + +// tests + +void test_file_tests() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = MBED_TEST_FILESYSTEM::format(&bd); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_simple_file_test() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello", O_WRONLY | O_CREAT); + TEST_ASSERT_EQUAL(0, res); + size = strlen("Hello World!\n"); + memcpy(wbuffer, "Hello World!\n", size); + res = file[0].write(wbuffer, size); + TEST_ASSERT_EQUAL(size, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello", O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + size = strlen("Hello World!\n"); + res = file[0].read(rbuffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(rbuffer, wbuffer, size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +template <int file_size, int write_size, int read_size> +void test_write_file_test() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + size_t size = file_size; + size_t chunk = write_size; + srand(0); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, filenames[file_counter], O_WRONLY | O_CREAT); + TEST_ASSERT_EQUAL(0, res); + for (size_t i = 0; i < size; i += chunk) { + chunk = (chunk < size - i) ? chunk : size - i; + for (size_t b = 0; b < chunk; b++) { + buffer[b] = rand() & 0xff; + } + res = file[0].write(buffer, chunk); + TEST_ASSERT_EQUAL(chunk, res); + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + size_t size = file_size; + size_t chunk = read_size; + srand(0); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, filenames[file_counter], O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + for (size_t i = 0; i < size; i += chunk) { + chunk = (chunk < size - i) ? chunk : size - i; + res = file[0].read(buffer, chunk); + TEST_ASSERT_EQUAL(chunk, res); + for (size_t b = 0; b < chunk && i+b < size; b++) { + res = buffer[b]; + TEST_ASSERT_EQUAL(rand() & 0xff, res); + } + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + file_counter++; + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_non_overlap_check() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + size_t size = 32; + size_t chunk = 29; + srand(0); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "smallavacado", O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + for (size_t i = 0; i < size; i += chunk) { + chunk = (chunk < size - i) ? chunk : size - i; + res = file[0].read(buffer, chunk); + TEST_ASSERT_EQUAL(chunk, res); + for (size_t b = 0; b < chunk && i+b < size; b++) { + res = buffer[b]; + TEST_ASSERT_EQUAL(rand() & 0xff, res); + } + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + size_t size = 8192; + size_t chunk = 29; + srand(0); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "mediumavacado", O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + for (size_t i = 0; i < size; i += chunk) { + chunk = (chunk < size - i) ? chunk : size - i; + res = file[0].read(buffer, chunk); + TEST_ASSERT_EQUAL(chunk, res); + for (size_t b = 0; b < chunk && i+b < size; b++) { + res = buffer[b]; + TEST_ASSERT_EQUAL(rand() & 0xff, res); + } + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + { + size_t size = 262144; + size_t chunk = 29; + srand(0); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "largeavacado", O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + for (size_t i = 0; i < size; i += chunk) { + chunk = (chunk < size - i) ? chunk : size - i; + res = file[0].read(buffer, chunk); + TEST_ASSERT_EQUAL(chunk, res); + for (size_t b = 0; b < chunk && i+b < size; b++) { + res = buffer[b]; + TEST_ASSERT_EQUAL(rand() & 0xff, res); + } + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_dir_check() { + + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "/"); + TEST_ASSERT_EQUAL(0, res); + int numFiles = sizeof(filenames)/sizeof(filenames[0]); + // Check the filenames in directory + while(1) { + res = dir[0].read(&ent); + if (0 == res) { + break; + } + for (int i=0; i < numFiles ; i++) { + res = strcmp(ent.d_name, filenames[i]); + if (0 == res) { + res = ent.d_type; + if ((DT_REG != res) && (DT_DIR != res)) { + TEST_ASSERT(1); + } + break; + } + else if( i == numFiles) { + TEST_ASSERT_EQUAL(0, res); + } + } + } + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + + +// test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("File tests", test_file_tests), + Case("Simple file test", test_simple_file_test), + Case("Small file test", test_write_file_test<32, 31, 29>), + Case("Medium file test", test_write_file_test<8192, 31, 29>), + Case("Large file test", test_write_file_test<262144, 31, 29>), + Case("Block Size file test", test_write_file_test<9000, 512, 512>), + Case("Multiple block size file test", test_write_file_test<26215, MBED_TEST_BUFFER, MBED_TEST_BUFFER>), + Case("Non-overlap check", test_non_overlap_check), + Case("Dir check", test_dir_check), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/filesystem/fopen/fopen.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/filesystem/fopen/fopen.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,1519 @@ +/* + * mbed Microcontroller Library + * Copyright (c) 2006-2016 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** @file fopen.cpp Test cases to POSIX file fopen() interface. + * + * Please consult the documentation under the test-case functions for + * a description of the individual test case. + */ + +#include "mbed.h" +#include "mbed_config.h" +#include "SDBlockDevice.h" +#include "FATFileSystem.h" +#include "fsfat_debug.h" +#include "fsfat_test.h" +#include "utest/utest.h" +#include "unity/unity.h" +#include "greentea-client/test_env.h" + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> /*rand()*/ +#include <inttypes.h> +#include <errno.h> +/* mbed_retarget.h is included after errno.h so symbols are mapped to + * consistent values for all toolchains */ +#include "platform/mbed_retarget.h" + +using namespace utest::v1; + +/// @cond FSFAT_DOXYGEN_DISABLE +#ifdef FSFAT_DEBUG +#define FSFAT_FOPEN_GREENTEA_TIMEOUT_S 3000 +#else +#define FSFAT_FOPEN_GREENTEA_TIMEOUT_S 1000 +#endif +/// @endcond + + +/* DEVICE_SPI + * This symbol is defined in targets.json if the target has a SPI interface, which is required for SDCard support. + * + * MBED_CONF_APP_FSFAT_SDCARD_INSTALLED + * For testing purposes, an SDCard must be installed on the target for the test cases in this file to succeed. + * If the target has an SD card installed then the MBED_CONF_APP_FSFAT_SDCARD_INSTALLED will be generated + * from the mbed_app.json, which includes the line + * { + * "config": { + * "UART_RX": "D0", + * <<< lines removed >>> + * "DEVICE_SPI": 1, + * "FSFAT_SDCARD_INSTALLED": 1 + * }, + * <<< lines removed >>> + */ + +#if defined(DEVICE_SPI) && ( defined(MBED_CONF_APP_FSFAT_SDCARD_INSTALLED) || (MBED_CONF_SD_FSFAT_SDCARD_INSTALLED)) +static char fsfat_fopen_utest_msg_g[FSFAT_UTEST_MSG_BUF_SIZE]; +#define FSFAT_FOPEN_TEST_MOUNT_PT_NAME "sd" +#define FSFAT_FOPEN_TEST_MOUNT_PT_PATH "/" FSFAT_FOPEN_TEST_MOUNT_PT_NAME +#define FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1 64 +#define FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH 20 +static const char *sd_badfile_path = "/sd/badfile.txt"; +static const char *sd_testfile_path = "/sd/test.txt"; + +SDBlockDevice sd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +FATFileSystem fs("sd", &sd); + +#define FSFAT_FOPEN_TEST_01 fsfat_fopen_test_01 +#define FSFAT_FOPEN_TEST_02 fsfat_fopen_test_02 +#define FSFAT_FOPEN_TEST_03 fsfat_fopen_test_03 +#define FSFAT_FOPEN_TEST_04 fsfat_fopen_test_04 +#define FSFAT_FOPEN_TEST_05 fsfat_fopen_test_05 +#define FSFAT_FOPEN_TEST_06 fsfat_fopen_test_06 +#define FSFAT_FOPEN_TEST_07 fsfat_fopen_test_07 +#define FSFAT_FOPEN_TEST_08 fsfat_fopen_test_08 +#define FSFAT_FOPEN_TEST_09 fsfat_fopen_test_09 +#define FSFAT_FOPEN_TEST_10 fsfat_fopen_test_10 +#define FSFAT_FOPEN_TEST_11 fsfat_fopen_test_11 +#define FSFAT_FOPEN_TEST_12 fsfat_fopen_test_12 +#define FSFAT_FOPEN_TEST_13 fsfat_fopen_test_13 +#define FSFAT_FOPEN_TEST_14 fsfat_fopen_test_14 +#define FSFAT_FOPEN_TEST_15 fsfat_fopen_test_15 +#define FSFAT_FOPEN_TEST_16 fsfat_fopen_test_16 +#define FSFAT_FOPEN_TEST_17 fsfat_fopen_test_17 +#define FSFAT_FOPEN_TEST_18 fsfat_fopen_test_18 +#define FSFAT_FOPEN_TEST_19 fsfat_fopen_test_19 +#define FSFAT_FOPEN_TEST_20 fsfat_fopen_test_20 +#define FSFAT_FOPEN_TEST_21 fsfat_fopen_test_21 +#define FSFAT_FOPEN_TEST_22 fsfat_fopen_test_22 +#define FSFAT_FOPEN_TEST_23 fsfat_fopen_test_23 +#define FSFAT_FOPEN_TEST_24 fsfat_fopen_test_24 +#define FSFAT_FOPEN_TEST_25 fsfat_fopen_test_25 +#define FSFAT_FOPEN_TEST_26 fsfat_fopen_test_26 +#define FSFAT_FOPEN_TEST_27 fsfat_fopen_test_27 +#define FSFAT_FOPEN_TEST_28 fsfat_fopen_test_28 +#define FSFAT_FOPEN_TEST_29 fsfat_fopen_test_29 +#define FSFAT_FOPEN_TEST_30 fsfat_fopen_test_30 + + +/* support functions */ + +/* + * open tests that focus on testing fopen() + * fsfat_handle_t fopen(const char* filename, char* data, size_t* len, fsfat_key_desc_t* kdesc) + */ + +/* file data for test_01 */ +static fsfat_kv_data_t fsfat_fopen_test_01_kv_data[] = { + { "/sd/fopentst/hello/world/animal/wobbly/dog/foot/frontlft.txt", "missing"}, + { NULL, NULL}, +}; + + +/** @brief + * Split a file path into its component parts, setting '/' characters to '\0', and returning + * pointers to the file path components in the parts array. For example, if + * filepath = "/sd/fopentst/hello/world/animal/wobbly/dog/foot/frontlft.txt" then + * *parts[0] = "sd" + * *parts[1] = "fopentst" + * *parts[2] = "hello" + * *parts[3] = "world" + * *parts[4] = "animal" + * *parts[5] = "wobbly" + * *parts[6] = "dog" + * *parts[7] = "foot" + * *parts[8] = "frontlft.txt" + * parts[9] = NULL + * + * ARGUMENTS + * @param filepath IN file path string to split into component parts. Expected to start with '/' + * @param parts IN OUT array to hold pointers to parts + * @param num IN number of components available in parts + * + * @return On success, this returns the number of components in the filepath Returns number of compoee + */ +static int32_t fsfat_filepath_split(char* filepath, char* parts[], uint32_t num) +{ + uint32_t i = 0; + int32_t ret = -1; + char* z = filepath; + + while (i < num && *z != '\0') { + if (*z == '/' ) { + *z = '\0'; + parts[i] = ++z; + i++; + } else { + z++; + } + } + if (*z == '\0' && i > 0) { + ret = (int32_t) i; + } + return ret; +} + + +/** @brief + * remove all directories and file in the given filepath + * + * ARGUMENTS + * @param filepath IN file path string to split into component parts. Expected to start with '/' + * + * @return On success, this returns 0, otherwise < 0 is returned; + */ +int32_t fsfat_filepath_remove_all(char* filepath) +{ + int32_t ret = -1; + int32_t len = 0; + char *fpathbuf = NULL; + char *pos = NULL; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + len = strlen(filepath); + fpathbuf = (char*) malloc(len+1); + if (fpathbuf == NULL) { + FSFAT_DBGLOG("%s: failed to duplicate string (out of memory)\n", __func__); + return ret; + } + memset(fpathbuf, 0, len+1); + memcpy(fpathbuf, filepath, len); + + /* delete the leaf node first, and then successively parent directories. */ + pos = fpathbuf + strlen(fpathbuf); + while (pos != fpathbuf) { + /* If the remaining file path is the mount point path then finish as the mount point cannot be removed */ + if (strlen(fpathbuf) == strlen(FSFAT_FOPEN_TEST_MOUNT_PT_PATH)) { + if( strncmp(fpathbuf, FSFAT_FOPEN_TEST_MOUNT_PT_PATH, strlen(fpathbuf)) == 0) { + break; + } + } + ret = remove(fpathbuf); + pos = strrchr(fpathbuf, '/'); + *pos = '\0'; + } + if (fpathbuf) { + free(fpathbuf); + } + return ret; +} + + +/** @brief + * make all directories in the given filepath. Do not create the file if present at end of filepath + * + * ARGUMENTS + * @param filepath IN file path containing directories and file + * @param do_asserts IN set to true if function should assert on errors + * + * @return On success, this returns 0, otherwise < 0 is returned; + */ +static int32_t fsfat_filepath_make_dirs(char* filepath, bool do_asserts) +{ + int32_t i = 0; + int32_t num_parts = 0; + int32_t len = 0; + int32_t ret = -1; + char *fpathbuf = NULL; + char *buf = NULL; + int pos = 0; + char *parts[FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH]; + + FSFAT_DBGLOG("%s:entered\n", __func__); + /* find the dirs to create*/ + memset(parts, 0, sizeof(parts)); + len = strlen(filepath); + fpathbuf = (char*) malloc(len+1); + if (fpathbuf == NULL) { + FSFAT_DBGLOG("%s: failed to duplicate string (out of memory)\n", __func__); + return ret; + } + memset(fpathbuf, 0, len+1); + memcpy(fpathbuf, filepath, len); + num_parts = fsfat_filepath_split(fpathbuf, parts, FSFAT_FOPEN_TEST_FILEPATH_MAX_DEPTH); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to split filepath (filename=\"%s\", num_parts=%d)\n", __func__, filepath, (int) num_parts); + TEST_ASSERT_MESSAGE(num_parts > 0, fsfat_fopen_utest_msg_g); + + /* Now create the directories on the directory path. + * Skip creating dir for "/sd" which must be present */ + buf = (char*) malloc(strlen(filepath)+1); + memset(buf, 0, strlen(filepath)+1); + pos = sprintf(buf, "/%s", parts[0]); + for (i = 1; i < num_parts - 1; i++) { + pos += sprintf(buf+pos, "/%s", parts[i]); + FSFAT_DBGLOG("mkdir(%s)\n", buf); + ret = mkdir(buf, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); + if (do_asserts == true) { + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create directory (filepath2=\"%s\", ret=%d, errno=%d)\n", __func__, buf, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + } + } + + if (buf) { + free(buf); + } + if (fpathbuf) { + free(fpathbuf); + } + return ret; +} + + +/* FIX ME: errno not set correctly when error occurs. This indicates a problem with the implementation. */ + +/** @brief + * Basic fopen test which does the following: + * - creates file and writes some data to the value blob. + * - closes the newly created file. + * - opens the file (r-only) + * - reads the file data and checks its the same as the previously created data. + * - closes the opened file + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +static control_t fsfat_fopen_test_01(const size_t call_count) +{ + char* read_buf; + int32_t ret = 0; + size_t len = 0; + fsfat_kv_data_t *node; + FILE *fp = NULL; + + FSFAT_DBGLOG("%s:entered\n", __func__); + (void) call_count; + node = fsfat_fopen_test_01_kv_data; + + /* remove file and directory from a previous failed test run, if present */ + fsfat_filepath_remove_all((char*) node->filename); + + /* create dirs */ + ret = fsfat_filepath_make_dirs((char*) node->filename, true); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dirs for filename (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + FSFAT_DBGLOG("%s:About to create new file (filename=\"%s\", data=\"%s\")\n", __func__, node->filename, node->value); + fp = fopen(node->filename, "w+"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (filename=\"%s\", data=\"%s\")(ret=%d, errno=%d)\n", __func__, node->filename, node->value, (int) ret, errno); + TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g); + + FSFAT_DBGLOG("%s:length of file=%d (filename=\"%s\", data=\"%s\")\n", __func__, (int) len, node->filename, node->value); + len = strlen(node->value); + ret = fwrite((const void*) node->value, len, 1, fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to write file (filename=\"%s\", data=\"%s\")(ret=%d)\n", __func__, node->filename, node->value, (int) ret); + TEST_ASSERT_MESSAGE(ret == 1, fsfat_fopen_utest_msg_g); + + FSFAT_DBGLOG("Created file successfully (filename=\"%s\", data=\"%s\")\n", node->filename, node->value); + ret = fclose(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close file (ret=%d, errno=%d)\n", __func__, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* now open the newly created key */ + fp = NULL; + fp = fopen(node->filename, "r"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open file for reading (filename=\"%s\", data=\"%s\")(ret=%d)\n", __func__, node->filename, node->value, (int) ret); + TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g); + + len = strlen(node->value) + 1; + read_buf = (char*) malloc(len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to allocated read buffer \n", __func__); + TEST_ASSERT_MESSAGE(read_buf != NULL, fsfat_fopen_utest_msg_g); + + FSFAT_DBGLOG("Opened file successfully (filename=\"%s\", data=\"%s\")\n", node->filename, node->value); + memset(read_buf, 0, len); + ret = fread((void*) read_buf, len, 1, fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to read file (filename=\"%s\", data=\"%s\", read_buf=\"%s\", ret=%d)\n", __func__, node->filename, node->value, read_buf, (int) ret); + /* FIX ME: fread should return the number of items read, not 0 when an item is read successfully. + * This indicates a problem with the implementation, as the correct data is read. The correct assert should be: + * TEST_ASSERT_MESSAGE(ret == 1, fsfat_fopen_utest_msg_g); + * The following assert is curerntly used until the implementation is fixed + */ + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* check read data is as expected */ + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: read value data (%s) != expected value data (filename=\"%s\", data=\"%s\", read_buf=\"%s\", ret=%d)\n", __func__, read_buf, node->filename, node->value, read_buf, (int) ret); + TEST_ASSERT_MESSAGE(strncmp(read_buf, node->value, strlen(node->value)) == 0, fsfat_fopen_utest_msg_g); + + if(read_buf){ + free(read_buf); + } + ret = fclose(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fclose() call failed (ret=%d, errno=%d).\n", __func__, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + return CaseNext; +} + +static fsfat_kv_data_t fsfat_fopen_test_02_data[] = { + FSFAT_INIT_1_TABLE_MID_NODE, + { NULL, NULL}, +}; + +/** + * @brief test to fopen() a pre-existing key and try to write it, which should fail + * as by default pre-existing keys are opened read-only + * + * Basic open test which does the following: + * - creates file with default rw perms and writes some data to the value blob. + * - closes the newly created file. + * - opens the file with the default permissions (read-only) + * - tries to write the file data which should fail because file was not opened with write flag set. + * - closes the opened key + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_02(const size_t call_count) +{ + int32_t ret = -1; + size_t len = 0; + FILE *fp = NULL; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + len = strlen(fsfat_fopen_test_02_data[0].value); + ret = fsfat_test_create(fsfat_fopen_test_02_data[0].filename, (char*) fsfat_fopen_test_02_data[0].value, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + /* by default, owner of key opens with read-only permissions*/ + fp = fopen(fsfat_fopen_test_02_data[0].filename, "r"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open file (filename=\"%s\", ret=%d)\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret); + TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g); + + len = strlen(fsfat_fopen_test_02_data[0].value); + ret = fwrite((const void*) fsfat_fopen_test_02_data[0].value, len, 1, fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: call to fwrite() succeeded when should have failed for read-only file (filename=\"%s\")(ret=%d).\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret); + TEST_ASSERT_MESSAGE(ret <= 0, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fclose() call failed.\n", __func__); + TEST_ASSERT_MESSAGE(fclose(fp) == 0, fsfat_fopen_utest_msg_g); + + return CaseNext; +} + + +/** + * @brief test to fopen() a pre-existing file and try to write it, which should succeed + * because the key was opened read-write permissions explicitly + * + * Basic open test which does the following: + * - creates file with default rw perms and writes some data to the value blob. + * - closes the newly created file. + * - opens the file with the rw permissions (non default) + * - tries to write the file data which should succeeds because file was opened with write flag set. + * - closes the opened key + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_03(const size_t call_count) +{ + int32_t ret = -1; + size_t len = 0; + FILE *fp = NULL; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + len = strlen(fsfat_fopen_test_02_data[0].value); + ret = fsfat_test_create(fsfat_fopen_test_02_data[0].filename, (char*) fsfat_fopen_test_02_data[0].value, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file in store (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + /* opens with read-write permissions*/ + fp = fopen(fsfat_fopen_test_02_data[0].filename, "w+"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open file (filename=\"%s\")(ret=%d)\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + len = strlen(fsfat_fopen_test_02_data[0].value); + ret = fwrite((const void*) fsfat_fopen_test_02_data[0].value, len, 1, fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: call to fwrite() failed when should have succeeded (filename=\"%s\", ret=%d).\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fclose() call failed.\n", __func__); + TEST_ASSERT_MESSAGE(fclose(fp) >= 0, fsfat_fopen_utest_msg_g); + + /* clean-up */ + ret = remove(fsfat_fopen_test_02_data[0].filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unable to delete file (filename=%s, ret=%d) .\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + return CaseNext; +} + + +/** @brief test to call fopen() with a filename string that exceeds the maximum length + * - chanFS supports the exFAT format which should support 255 char filenames + * - check that filenames of this length can be created + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_04(const size_t call_count) +{ + char filename_good[FSFAT_FILENAME_MAX_LENGTH+1]; + char filename_bad[FSFAT_FILENAME_MAX_LENGTH+2]; + int32_t ret = -1; + size_t len = 0; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + memset(filename_good, 0, FSFAT_FILENAME_MAX_LENGTH+1); + memset(filename_bad, 0, FSFAT_FILENAME_MAX_LENGTH+2); + ret = fsfat_test_filename_gen(filename_good, FSFAT_FILENAME_MAX_LENGTH); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unable to generate filename_good.\n", __func__); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: filename_good is not the correct length (filename_good=%s, len=%d, expected=%d).\n", __func__, filename_good, (int) strlen(filename_good), (int) FSFAT_FILENAME_MAX_LENGTH); + TEST_ASSERT_MESSAGE(strlen(filename_good) == FSFAT_FILENAME_MAX_LENGTH, fsfat_fopen_utest_msg_g); + + ret = fsfat_test_filename_gen(filename_bad, FSFAT_FILENAME_MAX_LENGTH+1); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unable to generate filename_bad.\n", __func__); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: filename_bad is not the correct length (len=%d, expected=%d).\n", __func__, (int) strlen(filename_bad), (int) FSFAT_FILENAME_MAX_LENGTH+1); + TEST_ASSERT_MESSAGE(strlen(filename_bad) == FSFAT_FILENAME_MAX_LENGTH+1, fsfat_fopen_utest_msg_g); + + len = strlen(filename_good); + ret = fsfat_test_create(filename_good, filename_good, len); + /* FIXME: + * The current implementation can create file with a filename with 9 chars (more than the 8 restriction of FAT32 Short File Names). + * However, the exFAT 255 char filesnames is not supported and hence the following is commented out. Find out what is + * the supported max filename length and change this testcase according. + * + * FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (filename=%s, ret=%d).\n", __func__, filename_good, (int) ret); + * TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + */ + + len = strlen(filename_bad); + ret = fsfat_test_create(filename_bad, filename_bad, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: created file in store for filename_bad when should have failed (filename=%s, ret=%d).\n", __func__, filename_bad, (int) ret); + TEST_ASSERT_MESSAGE(ret < 0, fsfat_fopen_utest_msg_g); + return CaseNext; +} + + +/// @cond FSFAT_DOXYGEN_DISABLE +typedef struct fsfat_fopen_kv_name_ascii_node { + uint32_t code; + uint32_t f_allowed : 1; +} fsfat_fopen_kv_name_ascii_node; +/// @endcond + +static const uint32_t fsfat_fopen_kv_name_ascii_table_code_sentinel_g = 256; + +/*@brief table recording ascii character codes permitted in kv names */ +static fsfat_fopen_kv_name_ascii_node fsfat_fopen_kv_name_ascii_table[] = +{ + {0 , true}, /* code 0-33 allowed*/ + {34, false}, /* '"' not allowed */ + {35, true}, /* allowed */ + {42, false}, /* '*' not allowed */ + {43, true}, /* allowed */ + {47, false}, /* '/' not allowed */ + {48, true}, /* allowed */ + {58, false}, /* ':' not allowed */ + {59, true}, /* allowed */ + {60, false}, /* '<' not allowed */ + {61, true}, /* allowed */ + {62, false}, /* '?', '>' not allowed */ + {64, true}, /* allowed */ + {92, false}, /* '\' not allowed */ + {93, true}, /* allowed */ + {124, false}, /* '!' not allowed */ + {125, true}, /* allowed */ + {127, false}, /* DEL not allowed */ + {128, true}, /* allowed */ + {fsfat_fopen_kv_name_ascii_table_code_sentinel_g, false}, /* sentinel */ +}; + + +/// @cond FSFAT_DOXYGEN_DISABLE +enum fsfat_fopen_kv_name_pos { + fsfat_fopen_kv_name_pos_start = 0x0, + fsfat_fopen_kv_name_pos_mid, + fsfat_fopen_kv_name_pos_end, + fsfat_fopen_kv_name_pos_max +}; +/// @endcond + +/** @brief test to call fopen() with filename that in includes illegal characters + * - the character(s) can be at the beginning of the filename + * - the character(s) can be at the end of the filename + * - the character(s) can be somewhere within the filename string + * - a max-length string of random characters (legal and illegal) + * - a max-length string of random illegal characters only + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_05(const size_t call_count) +{ + bool f_allowed = false; + const char *mnt_pt = FSFAT_FOPEN_TEST_MOUNT_PT_PATH; + const char *basename = "goodfile"; + const char *extname = "txt"; + const size_t basename_len = strlen(basename); + const size_t filename_len = strlen(mnt_pt)+strlen(basename)+strlen(extname)+2; /* extra 2 chars for '/' and '.' in "/sd/goodfile.txt" */ + char filename[FSFAT_BUF_MAX_LENGTH]; + size_t len = 0; + uint32_t j = 0; + int32_t ret = 0; + fsfat_fopen_kv_name_ascii_node* node = NULL; + uint32_t pos; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + +#ifdef FSFAT_DEBUG + /* symbol only used why debug is enabled */ + const char* pos_str = NULL; +#endif + + /* create bad keyname strings with invalid character code at start of keyname */ + node = fsfat_fopen_kv_name_ascii_table; + memset(filename, 0, FSFAT_BUF_MAX_LENGTH); + while(node->code != fsfat_fopen_kv_name_ascii_table_code_sentinel_g) + { + /* loop over range */ + for(j = node->code; j < (node+1)->code; j++) + { + if( (j >= 48 && j <= 57) || (j >= 65 && j <= 90) || (j >= 97 && j <= 122)) { + FSFAT_DBGLOG("%s: skipping alpha-numeric ascii character code %d (%c).\n", __func__, (int) j, (char) j); + continue; + } + + /* set the start, mid, last character of the name to the test char code */ + for(pos = (uint32_t) fsfat_fopen_kv_name_pos_start; pos < (uint32_t) fsfat_fopen_kv_name_pos_max; pos++) + { + len = snprintf(filename, filename_len+1, "%s/%s.%s", mnt_pt, basename, extname); + /* overwrite a char at the pos start, mid, end of the filename with an ascii char code (both illegal and legal)*/ + switch(pos) + { + case fsfat_fopen_kv_name_pos_start: + filename[5] = (char) j; /* 5 so at to write the second basename char (bad chars as first char not accepted)*/ + break; + case fsfat_fopen_kv_name_pos_mid: + /* create bad keyname strings with invalid character code in the middle of keyname */ + filename[5+basename_len/2] = (char) j; + break; + case fsfat_fopen_kv_name_pos_end: + /* create bad keyname strings with invalid character code at end of keyname */ + filename[5+basename_len-1] = (char) j; + break; + default: + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unexpected value of pos (pos=%d).\n", __func__, (int) pos); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + break; + } + +#ifdef FSFAT_DEBUG + /* processing only required when debug trace enabled */ + switch(pos) + { + case fsfat_fopen_kv_name_pos_start: + pos_str = "start"; + break; + case fsfat_fopen_kv_name_pos_mid: + pos_str = "middle"; + break; + case fsfat_fopen_kv_name_pos_end: + pos_str = "end"; + break; + default: + break; + } +#endif + ret = fsfat_test_create(filename, (const char*) filename, len); + + /* special cases */ + switch(j) + { + //case 0 : + //case 46 : + // switch(pos) + // { + // /* for code = 0 (null terminator). permitted at mid and end of string */ + // /* for code = 46 ('.'). permitted at mid and end of string but not at start */ + // case fsfat_fopen_kv_name_pos_start: + // f_allowed = false; + // break; + // case fsfat_fopen_kv_name_pos_mid: + // case fsfat_fopen_kv_name_pos_end: + // default: + // f_allowed = true; + // break; + // } + // break; + default: + f_allowed = node->f_allowed; + break; + } + if(f_allowed == true) + { + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file in store when filename contains valid characters (code=%d, ret=%d).\n", __func__, (int) j, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + /* revert FSFAT_LOG for more trace */ + FSFAT_DBGLOG("Successfully created a file with valid keyname containing ascii character code %d (%c) at the %s of the keyname.\n", (int) j, (int) j, pos_str); + FSFAT_LOG("%c", '.'); + + ret = fsfat_test_delete(filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to delete file previously created (code=%d, ret=%d).\n", __func__, (int) j, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + } + else + { /*node->f_allowed == false => not allowed to create kv name with ascii code */ + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: created file in store when filename contains an invalid character (code=%d, ret=%d).\n", __func__, (int) j, (int) ret); + TEST_ASSERT_MESSAGE(ret < 0, fsfat_fopen_utest_msg_g); + /* revert FSFAT_LOG for more trace */ + FSFAT_DBGLOG("Successfully failed to create a file with an invalid keyname containing ascii character code %d at the %s of the keyname.\n", (int) j, pos_str); + FSFAT_LOG("%c", '.'); + } + } + } + node++; + } + + FSFAT_LOG("%c", '\n'); + return CaseNext; +} + + +static const char fsfat_fopen_ascii_illegal_buf_g[] = "\"�'*+,./:;<=>?[\\]|"; + +/** @brief test to call fopen() with filename that in includes + * illegal characters + * - a max-length string of random illegal characters only + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_06(const size_t call_count) +{ + const char *mnt_pt = FSFAT_FOPEN_TEST_MOUNT_PT_PATH; + const char *extname = "txt"; + const size_t filename_len = strlen(mnt_pt)+FSFAT_MAX_FILE_BASENAME+strlen(extname)+2; /* extra 2 chars for '/' and '.' in "/sd/goodfile.txt" */ + char filename[FSFAT_BUF_MAX_LENGTH]; + int32_t i = 0; + int32_t j = 0; + uint32_t pos = 0; + uint32_t len = 0; + int32_t ret = -1; + size_t buf_data_max = 0; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + memset(filename, 0, FSFAT_BUF_MAX_LENGTH); + /* create bad keyname strings with invalid character code at start of keyname */ + buf_data_max = strlen(fsfat_fopen_ascii_illegal_buf_g); + + /* generate a number of illegal filenames */ + for (j = 0; i < FSFAT_MAX_FILE_BASENAME; j++) { + /* generate a kv name of illegal chars*/ + len = snprintf(filename, filename_len+1, "%s/", mnt_pt); + for (i = 0; i < FSFAT_MAX_FILE_BASENAME; i++) { + pos = rand() % (buf_data_max+1); + len += snprintf(filename+len, filename_len+1, "%c", fsfat_fopen_ascii_illegal_buf_g[pos]); + + } + len += snprintf(filename+len, filename_len+1, ".%s", extname); + ret = fsfat_test_create(filename, filename, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: created file when filename contains invalid characters (filename=%s, ret=%d).\n", __func__, filename, (int) ret); + TEST_ASSERT_MESSAGE(ret < 0, fsfat_fopen_utest_msg_g); + } + return CaseNext; +} + + +/** @brief test for errno reporting on a failed fopen()call + * + * This test does the following: + * - tries to open a file that does not exist for reading, and checks that a NULL pointer is returned. + * - checks that errno is not 0 as there is an error. + * - checks that ferror() returns 1 indicating an error exists. + * + * Note: see NOTE_1 below. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_07(const size_t call_count) +{ + FILE *f = NULL; + int ret = -1; + int errno_val = 0; + const char *filename = sd_badfile_path; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + errno = 0; + /* this is expect to fail as the file doesnt exist */ + f = fopen(filename,"r"); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: opened non-existent file for reading (filename=%s, f=%p).\n", __func__, filename, f); + TEST_ASSERT_MESSAGE(f == NULL, fsfat_fopen_utest_msg_g); + + /* check errno is set correctly */ +#if ! defined(__ARMCC_VERSION) && defined(__GNUC__) + /* Store errno so the current value set is not changed by new function call */ + errno_val = errno; + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: errno has unexpected value (errno != 0 expected) (filename=%s, errno=%d).\n", __func__, filename, errno); + TEST_ASSERT_MESSAGE(errno_val != 0, fsfat_fopen_utest_msg_g); +#endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */ + return CaseNext; +} + + +/** @brief test for operation of clearerr() and ferror() + * + * The test does the following: + * - opens and then closes a file, but keeps a copy of the FILE pointer fp. + * - set errno to 0. + * - write to the close file with fwrite(fp) which should return 0 (no writes) and set the errno. + * - check the error condition is set with ferror(). + * - clear the error with clearerr(). + * - check the error condition is reset with ferror(). + * + * NOTE_1: GCC/ARMCC support for setting errno + * - Documentation (e.g. fwrite() man page) does not explicity say fwrite() sets errno + * (e.g. for an fwrite() on a read-only file). + * - GCC libc fwrite() appears to set errno as expected. + * - ARMCC & IAR libc fwrite() appears not to set errno. + * + * The following ARMCC documents are silent on whether fwrite() sets errno: + * - "ARM C and C++ Libraries and Floating-Point Support". + * - "RL-ARM User Guide fwrite() section". + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_08(const size_t call_count) +{ + FILE *fp = NULL; + int ret = -1; + int ret_ferror = -1; + const char *filename = sd_testfile_path; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + errno = 0; + fp = fopen(filename,"w+"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open file (filename=%s, f=%p).\n", __func__, filename, fp); + TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g); + + /* close the fp but then try to read or write it */ + ret = fclose(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close file (ret=%d, errno=%d)\n", __func__, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* open file */ + errno = 0; + fp = fopen(filename, "r"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open file for reading (filename=\"%s\", ret=%d)\n", __func__, filename, (int) ret); + TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g); + + /* Perform fwrite() operation that will fail. */ + errno = 0; + ret = fwrite("42!", 4, 1, fp); + + ret_ferror = ferror(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: ferror() failed to report error (filename=%s, ret_ferror=%d).\n", __func__, filename, (int) ret_ferror); + TEST_ASSERT_MESSAGE(ret_ferror != 0, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fwrite successfully wrote to read-only file (filename=%s, ret=%d).\n", __func__, filename, (int) ret); + /* the fwrite() should fail and return 0. */ + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + +#if ! defined(__ARMCC_VERSION) && defined(__GNUC__) + /* check that errno is set. ARMCC appears not to set errno for fwrite() failure. */ + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unexpected zero value for errno (filename=%s, ret=%d, errno=%d).\n", __func__, filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(errno != 0, fsfat_fopen_utest_msg_g); + + /* check that errno is set to the expected value (this may change differ for different libc's) */ + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: errno != EBADF (filename=%s, ret=%d, errno=%d).\n", __func__, filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(errno == EBADF, fsfat_fopen_utest_msg_g); +#endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */ + + /* check clearerr() return clears the error */ + clearerr(fp); + ret = ferror(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: ferror() did not return zero value when error has been cleared (filename=%s, ret=%d).\n", __func__, filename, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + fclose(fp); + return CaseNext; +} + + +/** @brief test for operation of ftell() + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_09(const size_t call_count) +{ + FILE *fp = NULL; + int ret = -1; + int32_t len = 0; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + /* create a file of a certain length */ + len = strlen(fsfat_fopen_test_02_data[0].value); + ret = fsfat_test_create(fsfat_fopen_test_02_data[0].filename, (char*) fsfat_fopen_test_02_data[0].value, len); + + errno = 0; + /* Open the file for reading so the file is not truncated to 0 length. */ + fp = fopen(fsfat_fopen_test_02_data[0].filename, "r"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to open file (filename=%s, fp=%p, errno=%d).\n", __func__, fsfat_fopen_test_02_data[0].filename, fp, errno); + TEST_ASSERT_MESSAGE(fp != NULL, fsfat_fopen_utest_msg_g); + + errno = 0; + ret = fseek(fp, 0, SEEK_END); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: fseek() failed to SEEK_END (filename=%s, ret=%d, errno=%d).\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + errno = 0; + ret = ftell(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: ftell() failed to report correct offset value (filename=%s, ret=%d, errno=%d).\n", __func__, fsfat_fopen_test_02_data[0].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == len, fsfat_fopen_utest_msg_g); + + errno = 0; + ret = fclose(fp); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to close file (ret=%d, errno=%d)\n", __func__, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + return CaseNext; +} + + +/* file data for test_10 */ +static fsfat_kv_data_t fsfat_fopen_test_10_kv_data[] = { + { "/sd/test_10/testfile.txt", "test_data"}, + { NULL, NULL}, +}; + +/** @brief test for operation of remove() + * + * Performs the following tests: + * 1. test remove() on a file that exists. This should succeed. + * 2. test remove() on a dir that exists. This should succeed. + * 3. test remove() on a file that doesnt exist. This should fail. check errno set. + * 4. test remove() on a dir that doesnt exist. This should fail. check errno set. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_10(const size_t call_count) +{ + char buf[FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1]; + char *pos = NULL; + int32_t ret = -1; + size_t len = 0; + fsfat_kv_data_t *node = fsfat_fopen_test_10_kv_data; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + TEST_ASSERT(strlen(node->filename) < FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1); + + /* start from a known state i.e. directory to be created in not present */ + fsfat_filepath_remove_all((char*) node->filename); + + /* (1) */ + errno = 0; + ret = fsfat_filepath_make_dirs((char*) node->filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + len = strlen(node->value); + ret = fsfat_test_create(node->filename, (char*) node->value, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + ret = remove(node->filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: delete file operation failed (filename=%s, ret=%d) .\n", __func__, node->filename, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* (3) */ + ret = remove(node->filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: deleted a file that doesn't exist (filename=%s, ret=%d, errno=%d) .\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret != 0, fsfat_fopen_utest_msg_g); + + /* (2) */ + memset(buf, 0, FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1); + memcpy(buf, node->filename, strlen(node->filename)); + pos = strrchr(buf, '/'); + *pos = '\0'; + ret = remove(buf); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: delete directory operation failed (directory name=%s, ret=%d, errno=%d).\n", __func__, buf, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* (4) */ + ret = remove(buf); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: deleted a directory that doesn't exist (directory name=%s, ret=%d, errno=%d).\n", __func__, buf, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret != 0, fsfat_fopen_utest_msg_g); + + return CaseNext; +} + + +/* file data for test_11 */ +static fsfat_kv_data_t fsfat_fopen_test_11_kv_data[] = { + { "/sd/test_11/step0.txt", "test_data"}, + { "/sd/test_11/step1.txt", "test_data"}, + { "/sd/test_11/subdir/step3.txt", "test_data"}, + { NULL, NULL}, +}; + +/** @brief test for operation of rename() + * + * This test does the following: + * 1) test rename() on a file that exists to a new filename within the same directory. + * 2) test rename() on a file that exists to a new filename within a different directory. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_11(const size_t call_count) +{ + int32_t ret = -1; + size_t len = 0; + fsfat_kv_data_t *node = fsfat_fopen_test_11_kv_data; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + TEST_ASSERT(strlen(node->filename) < FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1); + + /* start from a known state i.e. directory to be created in not present, files not present */ + while(node->filename != NULL) { + fsfat_filepath_remove_all((char*) node->filename); + node++; + } + + /* create file and directories ready for rename() tests */ + errno = 0; + node = fsfat_fopen_test_11_kv_data; + ret = fsfat_filepath_make_dirs((char*) node->filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + len = strlen(node->value); + ret = fsfat_test_create(node->filename, (char*) node->value, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + errno = 0; + node = &fsfat_fopen_test_11_kv_data[2]; + ret = fsfat_filepath_make_dirs((char*) node->filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* (1) */ + ret = rename(fsfat_fopen_test_11_kv_data[0].filename, fsfat_fopen_test_11_kv_data[1].filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unable to rename file from (%s) to (%s) (ret=%d, errno=%d).\n", __func__, fsfat_fopen_test_11_kv_data[0].filename, fsfat_fopen_test_11_kv_data[1].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* (2) */ + ret = rename(fsfat_fopen_test_11_kv_data[1].filename, fsfat_fopen_test_11_kv_data[2].filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unable to rename file from (%s) to (%s) (ret=%d, errno=%d).\n", __func__, fsfat_fopen_test_11_kv_data[1].filename, fsfat_fopen_test_11_kv_data[2].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + return CaseNext; +} + + +/* file data for test_12 */ +static fsfat_kv_data_t fsfat_fopen_test_12_kv_data[] = { + { "/sd/test_12/subdir/testfil1.txt", "testfil1.txt"}, + { "/sd/test_12/testfil2.txt", "testfil2.txt"}, + { "/sd/test_12/testfil3.txt", "testfil3.txt"}, + { "/sd/test_12/testfil4.txt", "testfil4.txt"}, + { "/sd/test_12/testfil5.txt", "testfil5.txt"}, + { NULL, NULL}, +}; + +/** @brief test for operation of readdir(). + * + * Note, rewinddir(), telldir() and seekdir() dont appear to work reliably. + * opendir() not available on ARM/IAR toolchains. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_12(const size_t call_count) +{ + char buf[FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1]; + char *pos = NULL; + int32_t count = 0; + int32_t ret = -1; + size_t len = 0; + DIR *dir; + struct dirent *dp; + fsfat_kv_data_t *node = fsfat_fopen_test_12_kv_data; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + +#if ! defined(__ARMCC_VERSION) && defined(__GNUC__) + + /* start from a known state i.e. directory to be created in not present */ + while(node->filename != NULL) { + fsfat_filepath_remove_all((char*) node->filename); + node++; + } + + /* create a file */ + node = fsfat_fopen_test_12_kv_data; + errno = 0; + ret = fsfat_filepath_make_dirs((char*) node->filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + node = fsfat_fopen_test_12_kv_data; + while(node->filename != NULL) { + len = strlen(node->value); + ret = fsfat_test_create(node->filename, (char*) node->value, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + node++; + } + + node = fsfat_fopen_test_12_kv_data; + memset(buf, 0, FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1); + memcpy(buf, node->filename, strlen(node->filename)); + pos = strrchr(buf, '/'); + *pos = '\0'; + dir = opendir(buf); + + while ((dp = readdir(dir)) != NULL) { + FSFAT_DBGLOG("%s: filename: \"%s\"\n", __func__, dp->d_name); + TEST_ASSERT_MESSAGE(dp != 0, "Error: readdir() failed\n"); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unexpected object name (name=%s, expected=%s).\n", __func__, dp->d_name, fsfat_fopen_test_12_kv_data[count].value); + TEST_ASSERT_MESSAGE(strncmp(dp->d_name, fsfat_fopen_test_12_kv_data[count].value, strlen(fsfat_fopen_test_12_kv_data[count].value)) == 0, fsfat_fopen_utest_msg_g); + count++; + } + closedir(dir); + + /* cleanup */ + node = fsfat_fopen_test_12_kv_data; + while(node->filename != NULL) { + fsfat_filepath_remove_all((char*) node->filename); + node++; + } +#endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */ + return CaseNext; +} + + +/* file data for test_13 */ +static fsfat_kv_data_t fsfat_fopen_test_13_kv_data[] = { + /* a file is included in the filepath even though its not created by the test, + * as the fsfat_filepath_make_dirs() works with it present. */ + { "/sd/test_13/dummy.txt", "testdir"}, + { NULL, NULL}, +}; +/** @brief test for operation of mkdir()/remove() + * + * This test checks that: + * - The mkdir() function successfully creates a directory that is not already present. + * - The mkdir() function returns EEXIST when trying to create a directory thats already present. + * - The remove() function successfully removes a directory that is present. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_13(const size_t call_count) +{ + int32_t ret = 0; + + FSFAT_DBGLOG("%s:entered\n", __func__); + (void) call_count; + + /* start from a known state i.e. directory to be created in not present */ + fsfat_filepath_remove_all((char*) fsfat_fopen_test_13_kv_data[0].filename); + + errno = 0; + ret = fsfat_filepath_make_dirs((char*) fsfat_fopen_test_13_kv_data[0].filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, fsfat_fopen_test_13_kv_data[0].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + /* check that get a suitable error when try to create it again.*/ + errno = 0; + ret = fsfat_filepath_make_dirs((char*) fsfat_fopen_test_13_kv_data[0].filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: permitted to create directory when already exists (dirname=%s, ret=%d, errno=%d)\n", __func__, fsfat_fopen_test_13_kv_data[0].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret != 0, fsfat_fopen_utest_msg_g); + + /* check errno is as expected */ + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: errno != EEXIST (dirname=%s, ret=%d, errno=%d)\n", __func__, fsfat_fopen_test_13_kv_data[0].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(errno == EEXIST, fsfat_fopen_utest_msg_g); + + ret = fsfat_filepath_remove_all((char*) fsfat_fopen_test_13_kv_data[0].filename); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to remove directory (dirname=%s, ret=%d, errno=%d)\n", __func__, fsfat_fopen_test_13_kv_data[0].filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + return CaseNext; +} + +/* file data for test_14 */ +static fsfat_kv_data_t fsfat_fopen_test_14_kv_data[] = { + /* a file is included in the filepath even though its not created by the test, + * as the fsfat_filepath_make_dirs() works with it present. */ + { "/sd/test_14/testfile.txt", "testdata"}, + { NULL, NULL}, +}; + +/** @brief test for operation of stat() + * + * stat() is currently no supported by ARMCC and IAR toolchains libc. + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_14(const size_t call_count) +{ +#if ! defined(__ARMCC_VERSION) && defined(__GNUC__) + + char buf[FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1]; + char *pos = NULL; + int32_t ret = -1; + size_t len = 0; + struct stat file_stat; + fsfat_kv_data_t *node = fsfat_fopen_test_14_kv_data; + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + + TEST_ASSERT(strlen(node->filename) < FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1); + + /* start from a known state i.e. directory to be created in not present */ + fsfat_filepath_remove_all((char*) node->filename); + + /* Create file in a directory. */ + errno = 0; + ret = fsfat_filepath_make_dirs((char*) node->filename, false); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dir (dirname=%s, ret=%d, errno=%d)\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + len = strlen(node->value); + ret = fsfat_test_create(node->filename, (char*) node->value, len); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create file (ret=%d).\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret >= 0, fsfat_fopen_utest_msg_g); + + /* Test stat() on the file returns the correct attribute set */ + memset(&file_stat, 0, sizeof(file_stat)); + ret = stat(node->filename, &file_stat); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: stat() operation on file failed (filename=%s, ret=%d, errno=%d).\n", __func__, node->filename, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: expected st_mode S_IFREG flag not set (filename=%s).\n", __func__, node->filename); + TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFREG) == S_IFREG, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unexpected st_mode S_IFDIR flag set (filename=%s).\n", __func__, node->filename); + TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFDIR) != S_IFDIR, fsfat_fopen_utest_msg_g); + + /* Test stat() on the directory returns the correct attribute set */ + memset(&file_stat, 0, sizeof(file_stat)); + memset(buf, 0, FSFAT_FOPEN_TEST_WORK_BUF_SIZE_1); + memcpy(buf, node->filename, strlen(node->filename)); + pos = strrchr(buf, '/'); + *pos = '\0'; + ret = stat(buf, &file_stat); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: stat() operation on directory failed (directory name=%s, ret=%d, errno=%d).\n", __func__, buf, (int) ret, errno); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: unexpected st_mode S_IFREG flag set (directory name=%s).\n", __func__, buf); + TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFREG) != S_IFREG, fsfat_fopen_utest_msg_g); + + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: expected st_mode S_IFDIR flag not set (directory name=%s).\n", __func__, buf); + TEST_ASSERT_MESSAGE((file_stat.st_mode & S_IFDIR) == S_IFDIR, fsfat_fopen_utest_msg_g); + + /* clean up after successful test */ + fsfat_filepath_remove_all((char*) node->filename); + +#endif /* ! defined(__ARMCC_VERSION) && defined(__GNUC__) */ + return CaseNext; +} + +/** @brief test for operation of SDFileSystem::format() + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_15(const size_t call_count) +{ + + FSFAT_FENTRYLOG("%s:entered\n", __func__); + (void) call_count; + int32_t ret = -1; + + /* the allocation_unit of 0 means chanFS will use the default for the card (varies according to capacity). */ + fs.unmount(); + ret = fs.format(&sd); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to format sdcard (ret=%d)\n", __func__, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + fs.mount(&sd); + return CaseNext; +} + + +/* @brief test utility function to create a file of a given size. + * + * A reference data table is used of so that the data file can be later be + * checked with fsfat_test_check_data_file(). + * + * @param filename name of the file including path + * @param data data to store in file + * @param len number of bytes of data present in the data buffer. + */ +int32_t fsfat_test_create_data_file(const char* filename, size_t len) +{ + int32_t ret = -1; + FILE *fp = NULL; + size_t write_len = 0; + size_t written_len = 0; + int32_t exp = 0; + const int32_t exp_max = 8; /* so as not to exceed FSFAT_TEST_BYTE_DATA_TABLE_SIZE/2 */ + + FSFAT_FENTRYLOG("%s:entered (filename=%s, len=%d).\n", __func__, filename, (int) len); + TEST_ASSERT(len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE == 0); + fp = fopen(filename, "a"); + if(fp == NULL){ + return ret; + } + + while(written_len < len) { + /* write fsfat_test_byte_data_table or part thereof, in 9 writes of sizes + * 1, 2, 4, 8, 16, 32, 64, 128, 1, totalling 256 bytes len permitting. */ + for(exp = 0; (exp <= exp_max) && (written_len < len); exp++){ + write_len = 0x1 << (exp % exp_max); + write_len = len - written_len > write_len ? write_len : len - written_len; + ret = fwrite((const void*) &fsfat_test_byte_data_table[written_len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE], write_len, 1, fp); + written_len += write_len; + if(ret != 1){ + FSFAT_DBGLOG("%s:Error: fwrite() failed (ret=%d)\n", __func__, (int) ret); + ret = -1; + goto out0; + } + } + } + if(written_len == len) { + ret = 0; + } else { + ret = -1; + } +out0: + fclose(fp); + return ret; +} + + +/* @brief test utility function to check the data in the specified file is correct. + * + * The data read from the file is check that it agrees with the data written by + * fsfat_test_create_data_file(). + * + * @param filename name of the file including path + * @param data data to store in file + * @param len number of bytes of data present in the data buffer. + */ +int32_t fsfat_test_check_data_file(const char* filename, size_t len) +{ + int32_t ret = -1; + FILE *fp = NULL; + size_t read_len = 0; + uint8_t buf[FSFAT_TEST_BYTE_DATA_TABLE_SIZE]; + + FSFAT_FENTRYLOG("%s:entered (filename=%s, len=%d).\n", __func__, filename, (int) len); + TEST_ASSERT(len % FSFAT_TEST_BYTE_DATA_TABLE_SIZE == 0); + fp = fopen(filename, "r"); + if(fp == NULL){ + return ret; + } + + while(read_len < len) { + ret = fread((void*) buf, FSFAT_TEST_BYTE_DATA_TABLE_SIZE, 1, fp); + read_len += FSFAT_TEST_BYTE_DATA_TABLE_SIZE; + if(ret == 0){ + /* end of read*/ + FSFAT_DBGLOG("%s:unable to read data\n", __func__); + break; + } + if(memcmp(buf, fsfat_test_byte_data_table, FSFAT_TEST_BYTE_DATA_TABLE_SIZE) != 0) { + FSFAT_DBGLOG("%s:Error: read data not as expected (0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x, 0x%2x\n", __func__, + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]); + ret = -1; + goto out0; + } + } + if(read_len == len) { + ret = 0; + } +out0: + fclose(fp); + return ret; +} + +/* file data for test_16 */ +static fsfat_kv_data_t fsfat_fopen_test_16_kv_data[] = { + { "/sd/tst16_0/testfil0.txt", "dummy_data"}, + { "/sd/tst16_1/subdir0/testfil0.txt", "dummy_data"}, + { "/sd/tst16_2/subdir0/subdir1/testfil0.txt", "dummy_data"}, + { "/sd/tst16_3/subdir0/subdir1/subdir2/subdir3/testfil0.txt", "dummy_data"}, + { "/sd/tst16_4/subdir0/subdir1/subdir2/subdir3/subdir4/testfil0.txt", "dummy_data"}, + { "/sd/tst16_5/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/testfil0.txt", "dummy_data"}, + { "/sd/tst16_6/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/testfil0.txt", "dummy_data"}, + { "/sd/tst16_7/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/testfil0.txt", "dummy_data"}, + { "/sd/tst16_8/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/subdir8/testfil0.txt", "dummy_data"}, + { "/sd/tst16_9/subdir0/subdir1/subdir2/subdir3/subdir4/subdir5/subdir6/subdir7/subdir8/subdir9/testfil0.txt", "dummy_data"}, + { NULL, NULL}, +}; + + +/** @brief stress test to write data to fs + * + * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors. + */ +control_t fsfat_fopen_test_16(const size_t call_count) +{ + int32_t ret = 0; + fsfat_kv_data_t *node = fsfat_fopen_test_16_kv_data; + const int32_t num_blocks = 100; /* each file ~25kB */ + + FSFAT_DBGLOG("%s:entered\n", __func__); + (void) call_count; + + /* remove file and directory from a previous failed test run, if present */ + while(node->filename != NULL) { + fsfat_filepath_remove_all((char*) node->filename); + node++; + } + + /* create dirs */ + node = fsfat_fopen_test_16_kv_data; + while(node->filename != NULL) { + ret = fsfat_filepath_make_dirs((char*) node->filename, true); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create dirs for filename (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + node++; + } + + /* create the data files */ + node = fsfat_fopen_test_16_kv_data; + while(node->filename != NULL) { + ret = fsfat_test_create_data_file(node->filename, num_blocks * FSFAT_TEST_BYTE_DATA_TABLE_SIZE); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to create data file (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + node++; + } + + /* read the data back and check its as expected */ + node = fsfat_fopen_test_16_kv_data; + while(node->filename != NULL) { + ret = fsfat_test_check_data_file(node->filename, num_blocks * FSFAT_TEST_BYTE_DATA_TABLE_SIZE); + FSFAT_TEST_UTEST_MESSAGE(fsfat_fopen_utest_msg_g, FSFAT_UTEST_MSG_BUF_SIZE, "%s:Error: failed to check data file (filename=\"%s\")(ret=%d)\n", __func__, node->filename, (int) ret); + TEST_ASSERT_MESSAGE(ret == 0, fsfat_fopen_utest_msg_g); + node++; + } + + /* clean up */ + node = fsfat_fopen_test_16_kv_data; + while(node->filename != NULL) { + fsfat_filepath_remove_all((char*) node->filename); + node++; + } + return CaseNext; +} + + +#else + + +#define FSFAT_FOPEN_TEST_01 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_02 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_03 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_04 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_05 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_06 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_07 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_08 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_09 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_10 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_11 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_12 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_13 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_14 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_15 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_16 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_17 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_18 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_19 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_20 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_21 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_22 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_23 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_24 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_25 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_26 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_27 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_28 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_29 fsfat_fopen_test_dummy +#define FSFAT_FOPEN_TEST_30 fsfat_fopen_test_dummy + +/** @brief fsfat_fopen_test_dummy Dummy test case for testing when platform doesnt have an SDCard installed. + * + * @return success always + */ +static control_t fsfat_fopen_test_dummy() +{ + printf("Null test\n"); + return CaseNext; +} + +#endif + + +/// @cond FSFAT_DOXYGEN_DISABLE +utest::v1::status_t greentea_setup(const size_t number_of_cases) +{ + GREENTEA_SETUP(FSFAT_FOPEN_GREENTEA_TIMEOUT_S, "default_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Case cases[] = { + /* 1 2 3 4 5 6 7 */ + /* 1234567890123456789012345678901234567890123456789012345678901234567890 */ + Case("FSFAT_FOPEN_TEST_01: fopen()/fwrite()/fclose() directories/file in multi-dir filepath.", FSFAT_FOPEN_TEST_01), + Case("FSFAT_FOPEN_TEST_02: fopen(r) pre-existing file try to write it.", FSFAT_FOPEN_TEST_02), + Case("FSFAT_FOPEN_TEST_03: fopen(w+) pre-existing file try to write it.", FSFAT_FOPEN_TEST_03), + Case("FSFAT_FOPEN_TEST_04: fopen() with a filename exceeding the maximum length.", FSFAT_FOPEN_TEST_04), +#ifdef FOPEN_EXTENDED_TESTING + Case("FSFAT_FOPEN_TEST_05: fopen() with bad filenames (extended).", FSFAT_FOPEN_TEST_05), +#endif + Case("FSFAT_FOPEN_TEST_06: fopen() with bad filenames (minimal).", FSFAT_FOPEN_TEST_06), + Case("FSFAT_FOPEN_TEST_07: fopen()/errno handling.", FSFAT_FOPEN_TEST_07), + Case("FSFAT_FOPEN_TEST_08: ferror()/clearerr()/errno handling.", FSFAT_FOPEN_TEST_08), + Case("FSFAT_FOPEN_TEST_09: ftell() handling.", FSFAT_FOPEN_TEST_09), + Case("FSFAT_FOPEN_TEST_10: remove() test.", FSFAT_FOPEN_TEST_10), + Case("FSFAT_FOPEN_TEST_11: rename().", FSFAT_FOPEN_TEST_11), + Case("FSFAT_FOPEN_TEST_12: opendir(), readdir(), closedir() test.", FSFAT_FOPEN_TEST_12), + Case("FSFAT_FOPEN_TEST_13: mkdir() test.", FSFAT_FOPEN_TEST_13), + Case("FSFAT_FOPEN_TEST_14: stat() test.", FSFAT_FOPEN_TEST_14), + Case("FSFAT_FOPEN_TEST_15: format() test.", FSFAT_FOPEN_TEST_15), + Case("FSFAT_FOPEN_TEST_16: write/check n x 25kB data files.", FSFAT_FOPEN_TEST_16), +}; + + +/* Declare your test specification with a custom setup handler */ +Specification specification(greentea_setup, cases); + +int main() +{ + return !Harness::run(specification); +} +/// @endcond
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/filesystem/parallel/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/filesystem/parallel/main.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,184 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" +#include <stdlib.h> +#include <errno.h> + +using namespace utest::v1; + +// test configuration +#ifndef MBED_TEST_FILESYSTEM +#define MBED_TEST_FILESYSTEM FATFileSystem +#endif + +#ifndef MBED_TEST_FILESYSTEM_DECL +#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs") +#endif + +#ifndef MBED_TEST_BLOCKDEVICE +#define MBED_TEST_BLOCKDEVICE SDBlockDevice +#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +#endif + +#ifndef MBED_TEST_BLOCKDEVICE_DECL +#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd +#endif + +#ifndef MBED_TEST_FILES +#define MBED_TEST_FILES 4 +#endif + +#ifndef MBED_TEST_DIRS +#define MBED_TEST_DIRS 4 +#endif + +#ifndef MBED_TEST_BUFFER +#define MBED_TEST_BUFFER 512 +#endif + +#ifndef MBED_TEST_TIMEOUT +#define MBED_TEST_TIMEOUT 120 +#endif + +#ifndef MBED_THREAD_COUNT +#define MBED_THREAD_COUNT MBED_TEST_FILES +#endif + +// declarations +#define STRINGIZE(x) STRINGIZE2(x) +#define STRINGIZE2(x) #x +#define INCLUDE(x) STRINGIZE(x.h) + +#include INCLUDE(MBED_TEST_FILESYSTEM) +#include INCLUDE(MBED_TEST_BLOCKDEVICE) + +MBED_TEST_FILESYSTEM_DECL; +MBED_TEST_BLOCKDEVICE_DECL; + +Dir dir[MBED_TEST_DIRS]; +File file[MBED_TEST_FILES]; +DIR *dd[MBED_TEST_DIRS]; +FILE *fd[MBED_TEST_FILES]; +struct dirent ent; +struct dirent *ed; + +volatile bool count_done = 0; + +// tests + +void test_file_tests() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = MBED_TEST_FILESYSTEM::format(&bd); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void write_file_data (char count) { + + char filename[10]; + uint8_t wbuffer[MBED_TEST_BUFFER]; + int res; + + sprintf(filename, "%s%d", "data", count); + res = file[count].open(&fs, filename, O_WRONLY | O_CREAT); + TEST_ASSERT_EQUAL(0, res); + + char letter = 'A'+ count; + for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) { + wbuffer[i] = letter++; + if ('z' == letter) { + letter = 'A' + count; + } + } + + for (uint32_t i = 0; i < 5; i++) { + res = file[count].write(wbuffer, MBED_TEST_BUFFER); + TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res); + } + + res = file[count].close(); + TEST_ASSERT_EQUAL(0, res); +} + +void read_file_data (char count) { + char filename[10]; + uint8_t rbuffer[MBED_TEST_BUFFER]; + int res; + + sprintf(filename, "%s%d", "data", count); + res = file[count].open(&fs, filename, O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + + for (uint32_t i = 0; i < 5; i++) { + res = file[count].read(rbuffer, MBED_TEST_BUFFER); + TEST_ASSERT_EQUAL(MBED_TEST_BUFFER, res); + char letter = 'A' + count; + for (uint32_t i = 0; i < MBED_TEST_BUFFER; i++) { + res = rbuffer[i]; + TEST_ASSERT_EQUAL(letter++, res); + if ('z' == letter) { + letter = 'A' + count; + } + } + } + + res = file[count].close(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_thread_access_test() { + + Thread *data[MBED_THREAD_COUNT]; + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + + // Write threads in parallel + for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) { + data[thread_count] = new Thread(osPriorityNormal); + data[thread_count]->start(callback((void(*)(void*))write_file_data, (void*)thread_count)); + } + + // Wait for write thread to join before creating read thread + for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) { + data[thread_count]->join(); + delete data[thread_count]; + data[thread_count] = new Thread(osPriorityNormal); + data[thread_count]->start(callback((void(*)(void*))read_file_data, (void*)thread_count)); + } + + // Wait for read threads to join + for (char thread_count = 0; thread_count < MBED_THREAD_COUNT; thread_count++) { + data[thread_count]->join(); + delete data[thread_count]; + } + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +// test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("File tests", test_file_tests), + Case("Filesystem access from multiple threads", test_thread_access_test), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}
diff -r 000000000000 -r a15c76864d7d sd-driver/TESTS/filesystem/seek/main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/TESTS/filesystem/seek/main.cpp Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,629 @@ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity.h" +#include "utest.h" +#include <stdlib.h> +#include <errno.h> + +using namespace utest::v1; + +// test configuration +#ifndef MBED_TEST_FILESYSTEM +#define MBED_TEST_FILESYSTEM FATFileSystem +#endif + +#ifndef MBED_TEST_FILESYSTEM_DECL +#define MBED_TEST_FILESYSTEM_DECL MBED_TEST_FILESYSTEM fs("fs") +#endif + +#ifndef MBED_TEST_BLOCKDEVICE +#define MBED_TEST_BLOCKDEVICE SDBlockDevice +#define MBED_TEST_BLOCKDEVICE_DECL SDBlockDevice bd(MBED_CONF_SD_SPI_MOSI, MBED_CONF_SD_SPI_MISO, MBED_CONF_SD_SPI_CLK, MBED_CONF_SD_SPI_CS); +#endif + +#ifndef MBED_TEST_BLOCKDEVICE_DECL +#define MBED_TEST_BLOCKDEVICE_DECL MBED_TEST_BLOCKDEVICE bd +#endif + +#ifndef MBED_TEST_FILES +#define MBED_TEST_FILES 4 +#endif + +#ifndef MBED_TEST_DIRS +#define MBED_TEST_DIRS 4 +#endif + +#ifndef MBED_TEST_BUFFER +#define MBED_TEST_BUFFER 8192 +#endif + +#ifndef MBED_TEST_TIMEOUT +#define MBED_TEST_TIMEOUT 120 +#endif + + +// declarations +#define STRINGIZE(x) STRINGIZE2(x) +#define STRINGIZE2(x) #x +#define INCLUDE(x) STRINGIZE(x.h) + +#include INCLUDE(MBED_TEST_FILESYSTEM) +#include INCLUDE(MBED_TEST_BLOCKDEVICE) + +MBED_TEST_FILESYSTEM_DECL; +MBED_TEST_BLOCKDEVICE_DECL; + +Dir dir[MBED_TEST_DIRS]; +File file[MBED_TEST_FILES]; +DIR *dd[MBED_TEST_DIRS]; +FILE *fd[MBED_TEST_FILES]; +struct dirent ent; +struct dirent *ed; +size_t size; +uint8_t buffer[MBED_TEST_BUFFER]; +uint8_t rbuffer[MBED_TEST_BUFFER]; +uint8_t wbuffer[MBED_TEST_BUFFER]; + + +// tests + +void test_seek_tests() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = MBED_TEST_FILESYSTEM::format(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = fs.mkdir("hello", 0777); + TEST_ASSERT_EQUAL(0, res); + for (int i = 0; i < 132; i++) { + sprintf((char*)buffer, "hello/kitty%d", i); + res = file[0].open(&fs, (char*)buffer, + O_WRONLY | O_CREAT | O_APPEND); + TEST_ASSERT_EQUAL(0, res); + + size = strlen("kittycatcat"); + memcpy(buffer, "kittycatcat", size); + for (int j = 0; j < 132; j++) { + file[0].write(buffer, size); + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + } + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_simple_dir_seek() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "hello"); + TEST_ASSERT_EQUAL(0, res); +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, "."); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, ".."); + TEST_ASSERT_EQUAL(0, res); +#endif + + off_t pos; + int i; + for (i = 0; i < 4; i++) { + sprintf((char*)buffer, "kitty%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + pos = dir[0].tell(); + } + res = pos >= 0; + TEST_ASSERT_EQUAL(1, res); + + dir[0].seek(pos); + sprintf((char*)buffer, "kitty%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + + dir[0].rewind(); + sprintf((char*)buffer, "kitty%d", 0); +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, "."); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, ".."); + TEST_ASSERT_EQUAL(0, res); +#endif + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + + dir[0].seek(pos); + sprintf((char*)buffer, "kitty%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_large_dir_seek() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].open(&fs, "hello"); + TEST_ASSERT_EQUAL(0, res); +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, "."); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, ".."); + TEST_ASSERT_EQUAL(0, res); +#endif + + off_t pos; + int i; + for (i = 0; i < 128; i++) { + sprintf((char*)buffer, "kitty%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + pos = dir[0].tell(); + } + res = pos >= 0; + TEST_ASSERT_EQUAL(1, res); + + dir[0].seek(pos); + sprintf((char*)buffer, "kitty%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + + dir[0].rewind(); + sprintf((char*)buffer, "kitty%d", 0); +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, "."); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, ".."); + TEST_ASSERT_EQUAL(0, res); +#endif + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + + dir[0].seek(pos); + sprintf((char*)buffer, "kitty%d", i); + res = dir[0].read(&ent); + TEST_ASSERT_EQUAL(1, res); + res = strcmp(ent.d_name, (char*)buffer); + TEST_ASSERT_EQUAL(0, res); + res = dir[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_simple_file_seek() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello/kitty42", O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + + off_t pos; + size = strlen("kittycatcat"); + for (int i = 0; i < 4; i++) { + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + pos = file[0].tell(); + } + res = pos >= 0; + TEST_ASSERT_EQUAL(1, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + + file[0].rewind(); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(-size, SEEK_CUR); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(-size, SEEK_END) >= 0; + TEST_ASSERT_EQUAL(1, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + + size_t size = file[0].size(); + res = file[0].seek(0, SEEK_CUR); + TEST_ASSERT_EQUAL(size, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_large_file_seek() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello/kitty42", O_RDONLY); + TEST_ASSERT_EQUAL(0, res); + + off_t pos; + size = strlen("kittycatcat"); + for (int i = 0; i < 128; i++) { + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + pos = file[0].tell(); + } + res = pos >= 0; + TEST_ASSERT_EQUAL(1, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + + file[0].rewind(); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(-size, SEEK_CUR); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(-size, SEEK_END) >= 0; + TEST_ASSERT_EQUAL(1, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + + size_t size = file[0].size(); + res = file[0].seek(0, SEEK_CUR); + TEST_ASSERT_EQUAL(size, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_simple_file_seek_and_write() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello/kitty42", O_RDWR); + TEST_ASSERT_EQUAL(0, res); + + off_t pos; + size = strlen("kittycatcat"); + for (int i = 0; i < 4; i++) { + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + pos = file[0].tell(); + } + res = pos >= 0; + TEST_ASSERT_EQUAL(1, res); + + memcpy(buffer, "doggodogdog", size); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].write(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "doggodogdog", size); + TEST_ASSERT_EQUAL(0, res); + + file[0].rewind(); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "doggodogdog", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(-size, SEEK_END) >= 0; + TEST_ASSERT_EQUAL(1, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + + size_t size = file[0].size(); + res = file[0].seek(0, SEEK_CUR); + TEST_ASSERT_EQUAL(size, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_large_file_seek_and_write() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello/kitty42", O_RDWR); + TEST_ASSERT_EQUAL(0, res); + + off_t pos; + size = strlen("kittycatcat"); + for (int i = 0; i < 128; i++) { + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + if (i != 4) { + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + } + pos = file[0].tell(); + } + res = pos >= 0; + TEST_ASSERT_EQUAL(1, res); + + memcpy(buffer, "doggodogdog", size); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].write(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "doggodogdog", size); + TEST_ASSERT_EQUAL(0, res); + + file[0].rewind(); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(pos, SEEK_SET); + TEST_ASSERT_EQUAL(pos, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "doggodogdog", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(-size, SEEK_END) >= 0; + TEST_ASSERT_EQUAL(1, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + + size_t size = file[0].size(); + res = file[0].seek(0, SEEK_CUR); + TEST_ASSERT_EQUAL(size, res); + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_boundary_seek_and_write() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello/kitty42", O_RDWR); + TEST_ASSERT_EQUAL(0, res); + + size = strlen("hedgehoghog"); + const off_t offsets[] = {512, 1020, 513, 1021, 511, 1019}; + + for (int i = 0; i < sizeof(offsets) / sizeof(offsets[0]); i++) { + off_t off = offsets[i]; + memcpy(buffer, "hedgehoghog", size); + res = file[0].seek(off, SEEK_SET); + TEST_ASSERT_EQUAL(off, res); + res = file[0].write(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = file[0].seek(off, SEEK_SET); + TEST_ASSERT_EQUAL(off, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "hedgehoghog", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(0, SEEK_SET); + TEST_ASSERT_EQUAL(0, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "kittycatcat", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].sync(); + TEST_ASSERT_EQUAL(0, res); + } + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + +void test_out_of_bounds_seek() { + int res = bd.init(); + TEST_ASSERT_EQUAL(0, res); + + { + res = fs.mount(&bd); + TEST_ASSERT_EQUAL(0, res); + res = file[0].open(&fs, "hello/kitty42", O_RDWR); + TEST_ASSERT_EQUAL(0, res); + + size = strlen("kittycatcat"); + res = file[0].size(); + TEST_ASSERT_EQUAL(132*size, res); + res = file[0].seek((132+4)*size, + SEEK_SET); + TEST_ASSERT_EQUAL((132+4)*size, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(0, res); + + memcpy(buffer, "porcupineee", size); + res = file[0].write(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = file[0].seek((132+4)*size, + SEEK_SET); + TEST_ASSERT_EQUAL((132+4)*size, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); + res = memcmp(buffer, "porcupineee", size); + TEST_ASSERT_EQUAL(0, res); + res = file[0].seek(132*size, + SEEK_SET); + TEST_ASSERT_EQUAL(132*size, res); + res = file[0].read(buffer, size); + TEST_ASSERT_EQUAL(size, res); +#if (MBED_TEST_FILESYSTEM != FATFileSystem) + // FatFs does not guarantee empty expanded buffer + res = memcmp(buffer, "\0\0\0\0\0\0\0\0\0\0\0", size); + TEST_ASSERT_EQUAL(0, res); +#endif + res = file[0].close(); + TEST_ASSERT_EQUAL(0, res); + res = fs.unmount(); + TEST_ASSERT_EQUAL(0, res); + } + + res = bd.deinit(); + TEST_ASSERT_EQUAL(0, res); +} + + + +// test setup +utest::v1::status_t test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(MBED_TEST_TIMEOUT, "default_auto"); + return verbose_test_setup_handler(number_of_cases); +} + +Case cases[] = { + Case("Seek tests", test_seek_tests), + Case("Simple dir seek", test_simple_dir_seek), + Case("Large dir seek", test_large_dir_seek), + Case("Simple file seek", test_simple_file_seek), + Case("Large file seek", test_large_file_seek), + Case("Simple file seek and write", test_simple_file_seek_and_write), + Case("Large file seek and write", test_large_file_seek_and_write), + Case("Boundary seek and write", test_boundary_seek_and_write), + Case("Out-of-bounds seek", test_out_of_bounds_seek), +}; + +Specification specification(test_setup, cases); + +int main() { + return !Harness::run(specification); +}
diff -r 000000000000 -r a15c76864d7d sd-driver/config/mbed_lib.json --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/config/mbed_lib.json Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,145 @@ +{ + "name": "sd", + "config": { + "SPI_CS": "D10", + "SPI_MOSI": "D11", + "SPI_MISO": "D12", + "SPI_CLK": "D13", + "DEVICE_SPI": 1, + "FSFAT_SDCARD_INSTALLED": 1 + }, + "target_overrides": { + "DISCO_F051R8": { + "SPI_MOSI": "SPI_MOSI", + "SPI_MISO": "SPI_MISO", + "SPI_CLK": "SPI_SCK", + "SPI_CS": "SPI_CS" + }, + "DISCO_L476VG": { + "SPI_MOSI": "PE_15", + "SPI_MISO": "PE_14", + "SPI_CLK": "PE_13", + "SPI_CS": "PE_12" + }, + "K20D50M": { + "SPI_MOSI": "PTD2", + "SPI_MISO": "PTD3", + "SPI_CLK": "PTD1", + "SPI_CS": "PTC2" + }, + "KL22F": { + "SPI_MOSI": "PTD6", + "SPI_MISO": "PTD7", + "SPI_CLK": "PTD5", + "SPI_CS": "PTD4" + }, + "KL25Z": { + "SPI_MOSI": "PTD2", + "SPI_MISO": "PTD3", + "SPI_CLK": "PTD1", + "SPI_CS": "PTD0" + }, + "KL43Z": { + "SPI_MOSI": "PTD6", + "SPI_MISO": "PTD7", + "SPI_CLK": "PTD5", + "SPI_CS": "PTD4" + }, + "KL46Z": { + "SPI_MOSI": "PTD6", + "SPI_MISO": "PTD7", + "SPI_CLK": "PTD5", + "SPI_CS": "PTD4" + }, + "K64F": { + "SPI_MOSI": "PTE3", + "SPI_MISO": "PTE1", + "SPI_CLK": "PTE2", + "SPI_CS": "PTE4" + }, + "K66F": { + "SPI_MOSI": "PTE3", + "SPI_MISO": "PTE1", + "SPI_CLK": "PTE2", + "SPI_CS": "PTE4" + }, + "LPC11U37H_401": { + "SPI_MOSI": "SDMOSI", + "SPI_MISO": "SDMISO", + "SPI_CLK": "SDSCLK", + "SPI_CS": "SDSSEL" + }, + "LPC2368": { + "SPI_MOSI": "p11", + "SPI_MISO": "p12", + "SPI_CLK": "p13", + "SPI_CS": "p14" + }, + "NUCLEO_F429ZI": { + "SPI_MOSI": "PC_12", + "SPI_MISO": "PC_11", + "SPI_CLK": "PC_10", + "SPI_CS": "PA_15" + }, + "DISCO_F429ZI": { + "SPI_MOSI": "PC_12", + "SPI_MISO": "PC_11", + "SPI_CLK": "PC_10", + "SPI_CS": "PA_15" + }, + "NUCLEO_L031K6": { + "SPI_MOSI": "SPI_MOSI", + "SPI_MISO": "SPI_MISO", + "SPI_CLK": "SPI_SCK", + "SPI_CS": "SPI_CS" + }, + "NUMAKER_PFM_M453": { + "SPI_MOSI": "PD_13", + "SPI_MISO": "PD_14", + "SPI_CLK": "PD_15", + "SPI_CS": "PD_12" + }, + "NUMAKER_PFM_NUC472": { + "SPI_MOSI": "PF_0", + "SPI_MISO": "PD_15", + "SPI_CLK": "PD_14", + "SPI_CS": "PD_13" + }, + "nRF51822": { + "SPI_MOSI": "p12", + "SPI_MISO": "p13", + "SPI_CLK": "p15", + "SPI_CS": "p14" + }, + "UBLOX_EVK_ODIN_W2": { + "SPI_CS": "D9", + "SPI_MOSI": "D11", + "SPI_MISO": "D12", + "SPI_CLK": "D13" + }, + "RZ_A1H": { + "SPI_MOSI": "P8_5", + "SPI_MISO": "P8_6", + "SPI_CLK": "P8_3", + "SPI_CS": "P8_4" + }, + "GR_LYCHEE": { + "SPI_MOSI": "P5_6", + "SPI_MISO": "P5_7", + "SPI_CLK": "P5_4", + "SPI_CS": "P5_5" + }, + "HEXIWEAR": { + "SPI_MOSI": "PTE3", + "SPI_MISO": "PTE1", + "SPI_CLK": "PTE2", + "SPI_CS": "PTE4" + }, + "TB_SENSE_12": { + "SPI_MOSI": "PC6", + "SPI_MISO": "PC7", + "SPI_CLK": "PC8", + "SPI_CS": "PC9" + } + } +}
diff -r 000000000000 -r a15c76864d7d sd-driver/docs/pics/NUCLEO_F429ZI_wiring_with_ci_test_shield.png Binary file sd-driver/docs/pics/NUCLEO_F429ZI_wiring_with_ci_test_shield.png has changed
diff -r 000000000000 -r a15c76864d7d sd-driver/util/fsfat_debug.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/util/fsfat_debug.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,88 @@ +/** @file fsfat_debug.h + * + * component debug header file. + */ + + +#ifndef __FSFAT_DEBUG +#define __FSFAT_DEBUG + +#include <stdint.h> +#include <assert.h> +#include <stdio.h> + + +/* Debug Support */ + +#define FSFAT_LOG_NONE 0 +#define FSFAT_LOG_ERR 1 +#define FSFAT_LOG_WARN 2 +#define FSFAT_LOG_NOTICE 3 +#define FSFAT_LOG_INFO 4 +#define FSFAT_LOG_DEBUG 5 +#define FSFAT_LOG_FENTRY 6 + +#define FSFAT_LOG(_fmt, ...) \ + do \ + { \ + printf(_fmt, __VA_ARGS__); \ + }while(0); + +#define noFSFAT_DEBUG +#ifdef FSFAT_DEBUG + +extern uint32_t fsfat_optDebug_g; +extern uint32_t fsfat_optLogLevel_g; + + +/* uncomment for asserts to work */ +/* #undef NDEBUG */ +// todo: port to mbedOSV3++ #include <core-util/assert.h> + +#define FSFAT_INLINE +// todo: port to mbedOSV3++ #define FSFAT_ASSERT CORE_UTIL_ASSERT +#define FSFAT_ASSERT(...) + +#define FSFAT_DBGLOG(_fmt, ...) \ + do \ + { \ + if(fsfat_optDebug_g && (fsfat_optLogLevel_g >= FSFAT_LOG_DEBUG)) \ + { \ + printf(_fmt, __VA_ARGS__); \ + } \ + }while(0); + + +#define FSFAT_ERRLOG(_fmt, ...) \ + do \ + { \ + if(fsfat_optDebug_g && (fsfat_optLogLevel_g >= FSFAT_LOG_ERR)) \ + { \ + printf(_fmt, __VA_ARGS__); \ + } \ + }while(0); + + +#define FSFAT_FENTRYLOG(_fmt, ...) \ + do \ + { \ + if(fsfat_optDebug_g && (fsfat_optLogLevel_g >= FSFAT_LOG_FENTRY)) \ + { \ + printf(_fmt, __VA_ARGS__); \ + } \ + }while(0); + + + + + +#else +#define FSFAT_ASSERT(_x) do { } while(0) +#define FSFAT_INLINE inline +#define FSFAT_DBGLOG(_fmt, ...) do { } while(0) +#define FSFAT_ERRLOG(_fmt, ...) do { } while(0) +#define FSFAT_FENTRYLOG(_fmt, ...) do { } while(0) +#endif /* FSFAT_DEBUG */ + + +#endif /*__FSFAT_DEBUG*/
diff -r 000000000000 -r a15c76864d7d sd-driver/util/fsfat_test.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/util/fsfat_test.c Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,117 @@ +/* @file fsfat_test.c + * + * mbed Microcontroller Library + * Copyright (c) 2006-2016 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * test support code implementation file. + */ + +#include "fsfat_debug.h" +#include "fsfat_test.h" + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <assert.h> +#include <inttypes.h> +#include <ctype.h> + + +#ifdef FSFAT_DEBUG +uint32_t fsfat_optDebug_g = 1; +uint32_t fsfat_optLogLevel_g = FSFAT_LOG_NONE; /*FSFAT_LOG_NONE|FSFAT_LOG_ERR|FSFAT_LOG_DEBUG|FSFAT_LOG_FENTRY; */ +#endif + +/* ruler for measuring text strings */ +/* 1 1 1 1 1 1 1 1 1 1 2 2 2 */ +/* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 */ +/* 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890 */ + +const uint8_t fsfat_test_byte_data_table[FSFAT_TEST_BYTE_DATA_TABLE_SIZE] = { + 0x2d, 0xf3, 0x31, 0x4c, 0x11, 0x4f, 0xde, 0x0d, 0xbd, 0xbc, 0xa6, 0x78, 0x36, 0x5c, 0x1d, 0x28, + 0x5f, 0xa9, 0x10, 0x65, 0x54, 0x45, 0x21, 0x1a, 0x88, 0xfe, 0x76, 0x45, 0xb9, 0xac, 0x65, 0x9a, + 0x34, 0x9d, 0x73, 0x10, 0xb4, 0xa9, 0x2e, 0x90, 0x95, 0x68, 0xac, 0xfe, 0xc5, 0x2d, 0x15, 0x03, + 0x34, 0x70, 0xf1, 0x1d, 0x48, 0xa1, 0xa0, 0xed, 0x5c, 0x2f, 0xf5, 0x2b, 0xb9, 0x84, 0xbb, 0x45, + 0x32, 0xdd, 0xb1, 0x33, 0x95, 0x2a, 0xbc, 0x26, 0xf0, 0x89, 0xba, 0xf4, 0xbd, 0xf9, 0x5d, 0x2e, + 0x6e, 0x11, 0xc6, 0xa7, 0x78, 0xfc, 0xc9, 0x0e, 0x6b, 0x38, 0xba, 0x14, 0x1b, 0xab, 0x4c, 0x20, + 0x91, 0xe4, 0xb0, 0xf1, 0x2b, 0x14, 0x07, 0x6b, 0xb5, 0xcd, 0xe3, 0x49, 0x75, 0xac, 0xe8, 0x98, + 0xf1, 0x58, 0x8f, 0xd9, 0xc4, 0x8f, 0x00, 0x17, 0xb5, 0x06, 0x6a, 0x33, 0xbd, 0xa7, 0x40, 0x5a, + 0xbf, 0x49, 0xf7, 0x27, 0x1b, 0x4c, 0x3e, 0x6f, 0xe3, 0x08, 0x1f, 0xfd, 0xa6, 0xd4, 0xc7, 0x5f, + 0xa4, 0xa6, 0x82, 0xad, 0x19, 0xd5, 0x5c, 0xd8, 0x3a, 0x49, 0x85, 0xc9, 0x21, 0x83, 0xf6, 0xc6, + 0x84, 0xf9, 0x76, 0x89, 0xf3, 0x2d, 0x17, 0x50, 0x97, 0x38, 0x48, 0x9a, 0xe1, 0x82, 0xcd, 0xac, + 0xa8, 0x1d, 0xd7, 0x96, 0x5e, 0xb3, 0x08, 0xa8, 0x3a, 0xc7, 0x2b, 0x05, 0xaf, 0xdc, 0x16, 0xdf, + 0x48, 0x0f, 0x2a, 0x7e, 0x3a, 0x82, 0xd7, 0x80, 0xd6, 0x49, 0x27, 0x5d, 0xe3, 0x07, 0x62, 0xb3, + 0xc3, 0x6c, 0xba, 0xb2, 0xaa, 0x9f, 0xd9, 0x03, 0x0d, 0x27, 0xa8, 0xe0, 0xd6, 0xee, 0x79, 0x4b, + 0xd6, 0x97, 0x99, 0xb7, 0x11, 0xd6, 0x0d, 0x34, 0xae, 0x99, 0x4a, 0x93, 0x95, 0xd0, 0x5a, 0x34, + 0x19, 0xa2, 0x69, 0x57, 0xcf, 0x7c, 0x3d, 0x98, 0x88, 0x5d, 0x04, 0xf2, 0xd7, 0xac, 0xa5, 0x63 +}; + + +/* @brief test utility function to delete the file identified by filename + */ +int32_t fsfat_test_delete(const char* filename) +{ + FSFAT_FENTRYLOG("%s:entered.\r\n", __func__); + return remove(filename); +} + + +/* @brief test utility function to create a file + * + * @param filename name of the file including path + * @param data data to store in file + * @param len number of bytes of data present in the data buffer. + */ +int32_t fsfat_test_create(const char* filename, const char* data, size_t len) +{ + int32_t ret = -1; + FILE *fp = NULL; + + FSFAT_FENTRYLOG("%s:entered (filename=%s, len=%d).\n", __func__, filename, (int) len); + fp = fopen(filename, "w+"); + if(fp == NULL){ + return ret; + } + ret = fwrite((const void*) data, len, 1, fp); + if(ret < 0){ + fclose(fp); + return ret; + } + fclose(fp); + return ret; +} + + +/* @brief support function for generating a kv_name + * @param name buffer to hold kv name + * @param len length of kv name to generate + * + */ +int32_t fsfat_test_filename_gen(char* name, const size_t len) +{ + size_t i; + uint32_t pos = 0; + + const char* buf = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!$-_@"; + const int buf_len = strlen(buf); + FSFAT_FENTRYLOG("%s:entered\n", __func__); + for(i = 0; i < len; i++) + { + pos = rand() % (buf_len); + name[i] = buf[pos]; + } + return 0; +} +
diff -r 000000000000 -r a15c76864d7d sd-driver/util/fsfat_test.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sd-driver/util/fsfat_test.h Tue Mar 30 18:07:30 2021 +0000 @@ -0,0 +1,74 @@ +/** @file fsfat_test.h + * + * mbed Microcontroller Library + * Copyright (c) 2006-2016 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Header file for test support data structures and function API. + */ +#ifndef __FSFAT_TEST_H +#define __FSFAT_TEST_H + +#include <stdint.h> +#include <stdlib.h> +#include <stdbool.h> + +#ifdef __cplusplus +extern "C" { +#endif + +/* Defines */ +//#define FSFAT_INIT_1_TABLE_HEAD { "a", ""} +#define FSFAT_INIT_1_TABLE_MID_NODE { "/sd/01234567.txt", "abcdefghijklmnopqrstuvwxyz"} +//#define FSFAT_INIT_1_TABLE_TAIL { "/sd/fopentst/hello/world/animal/wobbly/dog/foot/backrght.txt", "present"} +#define FSFAT_TEST_RW_TABLE_SENTINEL 0xffffffff +#define FSFAT_TEST_BYTE_DATA_TABLE_SIZE 256 +#define FSFAT_UTEST_MSG_BUF_SIZE 256 +#define FSFAT_UTEST_DEFAULT_TIMEOUT_MS 10000 +#define FSFAT_MBED_HOSTTEST_TIMEOUT 60 +#define FSFAT_MAX_FILE_BASENAME 8 +#define FSFAT_MAX_FILE_EXTNAME 3 +#define FSFAT_BUF_MAX_LENGTH 64 +#define FSFAT_FILENAME_MAX_LENGTH 255 + + +/* support macro for make string for utest _MESSAGE macros, which dont support formatted output */ +#define FSFAT_TEST_UTEST_MESSAGE(_buf, _max_len, _fmt, ...) \ + do \ + { \ + snprintf((_buf), (_max_len), (_fmt), __VA_ARGS__); \ + }while(0); + + +/* + * Structures + */ + +/* kv data for test */ +typedef struct fsfat_kv_data_t { + const char* filename; + const char* value; +} fsfat_kv_data_t; + + +extern const uint8_t fsfat_test_byte_data_table[FSFAT_TEST_BYTE_DATA_TABLE_SIZE]; + +int32_t fsfat_test_create(const char* filename, const char* data, size_t len); +int32_t fsfat_test_delete(const char* key_name); +int32_t fsfat_test_filename_gen(char* name, const size_t len); +#ifdef __cplusplus +} +#endif + +#endif /* __FSFAT_TEST_H */