Logger to USB stick running on KL25Z

Dependencies:   F401RE-USBHost FastAnalogIn RTC mbed tsi_sensor2 mbed-rtos2

Fork of ReadSendData by Stuart Scott

Homepage for kXX_logger(was kl25x_usb_logger)

2014Nov12 - Added TSI - only works for kl25z, and fails after 4287seconds

Tests sequences for freezing 4287seconds 4287 4287 - Changed to USB write update every two seconds and lasted 4283seconds. Added mbed TSI_sensor - kudos to mbed as it turns out to be complex to do capacitive touch sensing. Kinetis L series Cortex-M0 (kl25z) has simpler capacitive sensing hardware which mbed/TSI_sensor lib uses, but doesn't support Cortex-M4 versions. K20D is older more complex interface. TSS hasn't had an open source support by Freescale in the past however that has changed. Currently I found KDS1.1.1 has a Processor Expert TSSlib that can be imported - but I'm getting a weird compile/include error - and not found any examples of how to make it work.

2014Oct05 - Published simple kl25z_usb_logger

Testing: Version (2:f06fbf86887e) has run over 28days starting 2014Sept07, reading a sensor data every seconds and then on the USB stick open file, write record, close file

This is over 2,419,200seconds ( 28days*24hrs*3600seconds) The stick data reflected 2,447,633 seconds, but only had 1,776,366 data records. After 407,650 data records started missing updates - analysis: the file could not be opened and read to the end in the allocated 1 second, and therefore missed the next ADC deadline.

By the end of the test period with 1,776,366 records on the USB Stick, ever read then write was causing it to miss two/three ADC updates - ie taking longer than two seconds.

Analysis: this was an aggressive test, and normally would not be opening, writing, closing every 1 second. However big plus the file mechanism held up and the program continued to operate, with a graceful failure of a missed ADC readings. The output used JSON, which reads nicely but turns out not to be supported by Excel (2014?) I was using.

Conclusion: Overall the USB Mechanisms and file read is stable. However the USB stack used doesn't have an event based management for stick insertion and removal, or OTG power management . This KL25Z has been a very useful test to prove the configuration of clocks for USB Stack is possible and stable. The target processor being looked at is the MK20 and is similar in clocks, but slightly more comprehensive USB OTG Next step is to investigate the full Freescale Kinetis USB OTG Stack with power management on a custom K20 board, using Kinetis Design Studio, and very low power capability.

Example https://github.com/ErichStyger/mcuoneclipse/tree/master/Examples/FRDM-K20D50M/FRDM-K20_MSD_Host http://mcuoneclipse.com/2014/10/25/usb-cdc-with-the-frdm-k64f-finally/#comment-37727 - Commentary on Freescale USB versions

Committer:
neilh20
Date:
Tue May 19 16:03:50 2015 +0000
Revision:
15:91ef840c442c
Parent:
13:729f5ae38c24
Initial pub to share

Who changed what in which revision?

UserRevisionLine numberNew contents of line
neilh20 2:f06fbf86887e 1 /* low power data logger. [FRDM-KL25Z]
neilh20 2:f06fbf86887e 2
neilh20 2:f06fbf86887e 3 Periodically read data from ADC and when present write it in JSON format to USB Stick
neilh20 2:f06fbf86887e 4
neilh20 2:f06fbf86887e 5 Copyright http://www.apache.org/licenses/LICENSE-2.0
neilh20 2:f06fbf86887e 6 and all subsystems per their own licenses which are believed to be GPL 2.0
neilh20 2:f06fbf86887e 7
neilh20 2:f06fbf86887e 8 [c]Record an analoge value to an internal buffer
neilh20 2:f06fbf86887e 9 [p]and when a USB stick is plugged in store the values on the USB stick in JSON format
neilh20 2:f06fbf86887e 10 (currently requires USB stick at reset)
neilh20 2:f06fbf86887e 11 [c]Debug output the events as they happen.
neilh20 2:f06fbf86887e 12 [c]The ticker delay sets the sampling rate.
neilh20 2:f06fbf86887e 13 [F]Date is set when user sets it,
neilh20 2:f06fbf86887e 14 [c]or elapsed time used in the beginning
neilh20 2:f06fbf86887e 15
neilh20 2:f06fbf86887e 16 [F]If OpenSDA USB power supplied, then flash LEDs to indicate activity as follows
neilh20 2:f06fbf86887e 17 [c]1) the Blue led when taking a sample
neilh20 2:f06fbf86887e 18 [c]2) the green led when delivering samples to the USB stick
neilh20 2:f06fbf86887e 19 [F]3)no led shall be on at same time for clear colours
neilh20 2:f06fbf86887e 20
neilh20 2:f06fbf86887e 21 [F] when not doing anything else sleep to conserve power.
sas37 0:eb0dc2b5bc51 22
neilh20 2:f06fbf86887e 23 To modify this for your application, assuming a ratiometric input to the Vref=3.3V
neilh20 2:f06fbf86887e 24 1) Determine physical pins ADC performed on and type of conversion,
neilh20 2:f06fbf86887e 25 1) Determine the "multiplier" from the incoming 16bit ADC wrt to 3.3V to your units.
neilh20 2:f06fbf86887e 26 2) Write the output code changing the JSON headers to define your headers, dataConversion and units
neilh20 2:f06fbf86887e 27
neilh20 2:f06fbf86887e 28 Thanks to other authors for setting up KL25z for
neilh20 2:f06fbf86887e 29 * FastAnalogIn Library mbed.org/users/Sissors/code/FastAnalogIn/
neilh20 2:f06fbf86887e 30 * USB Host mbed.org/users/va009039/code/F401RE-USBHost/
neilh20 2:f06fbf86887e 31
neilh20 2:f06fbf86887e 32 [] 1st three character nomeclature.
neilh20 2:f06fbf86887e 33 [x] indicates implemented state of feature
neilh20 2:f06fbf86887e 34 [c] - complete and unit tested
neilh20 2:f06fbf86887e 35 [p] - partial in progress or not unit tested
neilh20 2:f06fbf86887e 36 [f] - future idea
sas37 0:eb0dc2b5bc51 37
neilh20 2:f06fbf86887e 38 Testing: Basic tests performed, see attached file logger_tests.docx
neilh20 2:f06fbf86887e 39
neilh20 2:f06fbf86887e 40 Modifications to Hardware
neilh20 2:f06fbf86887e 41 1) FRDM-KL25z add J21 and R8 per the instruction manual for USB host
sas37 0:eb0dc2b5bc51 42
neilh20 2:f06fbf86887e 43 Issues:
neilh20 2:f06fbf86887e 44 1) FRDM-KL25z hardware not designed to switch the USB power so saving power on a connected USB device.
neilh20 2:f06fbf86887e 45 Solution: Add switched 5V power.
neilh20 2:f06fbf86887e 46 2) FRDM-KL25z Hardware not designed for low power accurate wall time clock - wastes power keeping the 8Mhz going.
neilh20 2:f06fbf86887e 47 Solution: add an external low power 32KHz oscilator into hardware pin RTC_IN (An osc is the next step up from a raw Xtal)
sas37 0:eb0dc2b5bc51 48
sas37 0:eb0dc2b5bc51 49
sas37 0:eb0dc2b5bc51 50 */
sas37 0:eb0dc2b5bc51 51
neilh20 2:f06fbf86887e 52 #include "mbed.h"
neilh20 15:91ef840c442c 53 //#include "rtos.h"
neilh20 2:f06fbf86887e 54 #include "rtc_api.h"
neilh20 2:f06fbf86887e 55 #include "FastAnalogIn.h"
neilh20 2:f06fbf86887e 56 #include "USBHostMSD.h"
neilh20 15:91ef840c442c 57 #include "board_cust.h"
neilh20 2:f06fbf86887e 58
neilh20 9:f3f7ca1da2a8 59
neilh20 9:f3f7ca1da2a8 60 #if defined(TARGET_K20D50M)
neilh20 9:f3f7ca1da2a8 61 #define TsiActive 0
neilh20 9:f3f7ca1da2a8 62
neilh20 9:f3f7ca1da2a8 63 #elif defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
neilh20 12:d2e6f79d6fdb 64 #define TsiActive 1
neilh20 9:f3f7ca1da2a8 65 #include "tsi_sensor.h"
neilh20 11:fefb12811584 66 //Cap Sensor B0=CH0, B1=CH6
neilh20 12:d2e6f79d6fdb 67 #define ELEC0 0
neilh20 12:d2e6f79d6fdb 68 #define ELEC1 6
neilh20 11:fefb12811584 69 //FRDM_KL25x slider electrodes - TSI0_CH9=PTB16, CH10=B17
neilh20 12:d2e6f79d6fdb 70 // #define ELEC0 9
neilh20 12:d2e6f79d6fdb 71 // #define ELEC1 10
neilh20 9:f3f7ca1da2a8 72 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
neilh20 12:d2e6f79d6fdb 73 //TSIElectrode tsiElec0(ELEC0);
neilh20 12:d2e6f79d6fdb 74 //TSIElectrode tsiElec1(ELEC1);
neilh20 11:fefb12811584 75 //#define TsiRead() tsi.readDistance() - has problesm
neilh20 10:b74fa5ec4af4 76 #define TsiRead() tsi.readPercentage()
neilh20 15:91ef840c442c 77
neilh20 9:f3f7ca1da2a8 78 #elif defined (TARGET_KL05Z)
neilh20 9:f3f7ca1da2a8 79 #define TsiActive 0
neilh20 9:f3f7ca1da2a8 80 #include "TSISensor.h"
neilh20 9:f3f7ca1da2a8 81
neilh20 9:f3f7ca1da2a8 82 #define ELEC0 9
neilh20 9:f3f7ca1da2a8 83 #define ELEC1 8
neilh20 9:f3f7ca1da2a8 84 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
neilh20 10:b74fa5ec4af4 85 //#define TsiRead(void) tsi.Distance()
neilh20 9:f3f7ca1da2a8 86 #else
neilh20 9:f3f7ca1da2a8 87 #define TsiActive 0
neilh20 9:f3f7ca1da2a8 88 #error TARGET NOT DEFINED
neilh20 9:f3f7ca1da2a8 89 #endif
neilh20 9:f3f7ca1da2a8 90
neilh20 13:729f5ae38c24 91
neilh20 15:91ef840c442c 92 //size of Customer ADC sample buffer
neilh20 2:f06fbf86887e 93 #define BuffSize 16 //16 to test, upto kl25z spare ram of about xxxx?, !2000, linker warns if too large, but also fails
neilh20 13:729f5ae38c24 94 #define SAMPLE_WIN 8 // Should be less than BuffSize
neilh20 13:729f5ae38c24 95 #if BuffSize < SAMPLE_WIN
neilh20 13:729f5ae38c24 96 #error Buffsize < SAMPLE_WIN
neilh20 13:729f5ae38c24 97 #endif
neilh20 2:f06fbf86887e 98 //set the delay used by the ticker
neilh20 2:f06fbf86887e 99 const int sysTockInit_sec = 1; //Test 1, Real life 15minutes or 900sec
sas37 0:eb0dc2b5bc51 100
sas37 0:eb0dc2b5bc51 101 using namespace mbed;
sas37 0:eb0dc2b5bc51 102
neilh20 15:91ef840c442c 103
neilh20 2:f06fbf86887e 104 //Device Init - per FRDM-KL25Z
neilh20 2:f06fbf86887e 105 DigitalOut ledRed(LED1);
neilh20 11:fefb12811584 106 //DigitalOut ledGrn(LED2);
neilh20 11:fefb12811584 107 PwmOut ledPwmGrn(LED2);
neilh20 2:f06fbf86887e 108 DigitalOut ledBlue(LED3);
neilh20 2:f06fbf86887e 109 FastAnalogIn ainPinAA(PTC2); ///AnalogAin pin
sas37 0:eb0dc2b5bc51 110
sas37 0:eb0dc2b5bc51 111 Serial pc(USBTX, USBRX);
sas37 0:eb0dc2b5bc51 112
neilh20 2:f06fbf86887e 113 //Samples stored here - needs to be compact as can use a lot of internal ram
neilh20 2:f06fbf86887e 114 unsigned short sample_u16[BuffSize]; //critical resource for number of samples
neilh20 13:729f5ae38c24 115 unsigned short publish_u16[BuffSize]; //value published
neilh20 13:729f5ae38c24 116 unsigned short sampleInst_raw, sampleAvg_raw;
neilh20 13:729f5ae38c24 117 float tsiSlider_dist;
neilh20 13:729f5ae38c24 118 uint32_t elec0p,electr[BuffSize];
neilh20 13:729f5ae38c24 119 //uint32_t elec1p;
neilh20 2:f06fbf86887e 120 unsigned int time_[BuffSize];
sas37 0:eb0dc2b5bc51 121
neilh20 13:729f5ae38c24 122
neilh20 15:91ef840c442c 123
neilh20 2:f06fbf86887e 124 bool UsbPresent=0;
neilh20 2:f06fbf86887e 125 bool warmBoot = rtc_isenabled();
neilh20 2:f06fbf86887e 126 long lp2i =0;
neilh20 2:f06fbf86887e 127 bool sysEventA=0; //Local event
neilh20 2:f06fbf86887e 128 unsigned int tmrE_evt; //Elapsed
neilh20 2:f06fbf86887e 129 Ticker tock; //periodic tick
neilh20 2:f06fbf86887e 130
neilh20 15:91ef840c442c 131
neilh20 6:261c5054fd51 132 #if defined(TARGET_K20D50M)
neilh20 6:261c5054fd51 133 const char meProc[] = "K20D50M";
neilh20 6:261c5054fd51 134 #elif defined(TARGET_KL25Z)
neilh20 6:261c5054fd51 135 const char meProc[]= "KL25Z";
neilh20 6:261c5054fd51 136 #else //#if defined(TARGET_KL46Z)||defined(TARGET_K64F)
neilh20 6:261c5054fd51 137 #error Undef processor
neilh20 6:261c5054fd51 138 #endif //
neilh20 9:f3f7ca1da2a8 139
neilh20 9:f3f7ca1da2a8 140
neilh20 9:f3f7ca1da2a8 141
neilh20 9:f3f7ca1da2a8 142
neilh20 9:f3f7ca1da2a8 143
neilh20 2:f06fbf86887e 144 //--------------------------------------------------------------
neilh20 2:f06fbf86887e 145 void tockEvent()
sas37 0:eb0dc2b5bc51 146 {
neilh20 2:f06fbf86887e 147 tmrE_evt++;
neilh20 2:f06fbf86887e 148 sysEventA=1;
sas37 0:eb0dc2b5bc51 149 }
sas37 0:eb0dc2b5bc51 150
neilh20 2:f06fbf86887e 151 //--------------------------------------------------------------
neilh20 2:f06fbf86887e 152 /* Process output Header to USB,
neilh20 2:f06fbf86887e 153 return !0 if valid USB present
neilh20 2:f06fbf86887e 154 */
neilh20 15:91ef840c442c 155 bool outputHeaderUsb(FILE* fpUsb) {
neilh20 2:f06fbf86887e 156 // JSON {"headings":{"time":{"elapsed":"seconds"}, "adcN":"normalized_to_1.0", "adcV":"Volts"}}
neilh20 2:f06fbf86887e 157
neilh20 2:f06fbf86887e 158 if (!UsbPresent)
neilh20 2:f06fbf86887e 159 {
neilh20 15:91ef840c442c 160 if (1/*ToDo msd.connect()*/) //Attempt to find a device
neilh20 2:f06fbf86887e 161 {//USB Event: newly connected so attempt write header
neilh20 2:f06fbf86887e 162 if (NULL !=(fpUsb = fopen("/usbAa/test1.txt", "a")))
neilh20 2:f06fbf86887e 163 {
neilh20 2:f06fbf86887e 164 pc.printf("USB File test1.txt opened.\n\r");
neilh20 9:f3f7ca1da2a8 165 fprintf(fpUsb,"{\"headings\":{\"time\":{\"elapsed\":\"seconds\"}, \"adcN\":\"normalized_to_1.0\", \"adcV\":\"Volts\"}"
neilh20 9:f3f7ca1da2a8 166 #if TsiActive
neilh20 9:f3f7ca1da2a8 167 "\"tsi\":\"[0-40]\""
neilh20 9:f3f7ca1da2a8 168 #endif
neilh20 9:f3f7ca1da2a8 169 "}}\n");
neilh20 2:f06fbf86887e 170 UsbPresent=1;
neilh20 2:f06fbf86887e 171 fclose(fpUsb); fpUsb=NULL;
neilh20 2:f06fbf86887e 172 } else {
neilh20 2:f06fbf86887e 173 //Can't find a file for some reason. Could be USB full or failed software
neilh20 2:f06fbf86887e 174 pc.printf(" Error: USB File not opened.\n\r");
neilh20 2:f06fbf86887e 175 UsbPresent=0;
neilh20 2:f06fbf86887e 176 }
neilh20 2:f06fbf86887e 177 } //else //Can't find USB device
neilh20 2:f06fbf86887e 178 } else {
neilh20 2:f06fbf86887e 179 // UsbPresent and assume headers written
neilh20 15:91ef840c442c 180 if (1/*ToDo !msd.connected()*/)
neilh20 2:f06fbf86887e 181 {
neilh20 2:f06fbf86887e 182 pc.printf(" USB Flash drive removed.\n\r");
neilh20 2:f06fbf86887e 183 UsbPresent=0;
neilh20 2:f06fbf86887e 184 }
neilh20 2:f06fbf86887e 185 }
neilh20 2:f06fbf86887e 186
neilh20 2:f06fbf86887e 187 return UsbPresent;
neilh20 2:f06fbf86887e 188 }
neilh20 2:f06fbf86887e 189 //--------------------------------------------------------------
neilh20 2:f06fbf86887e 190 void outputHeaders(void) {
neilh20 15:91ef840c442c 191 //ToDo - outputHeaderUsb(*fp);
neilh20 2:f06fbf86887e 192 pc.printf(" Time(S) depthR(norm) Volts Idx %d\n\r",(unsigned int) rtc_read());
neilh20 2:f06fbf86887e 193 }//outputHeaders
sas37 0:eb0dc2b5bc51 194
neilh20 2:f06fbf86887e 195
neilh20 2:f06fbf86887e 196
neilh20 2:f06fbf86887e 197 //--------------------------------------------------------------
neilh20 2:f06fbf86887e 198 /* Manage the write to USB stick
neilh20 2:f06fbf86887e 199 [FUT] detect USB stick and if not present write it when inserted
neilh20 2:f06fbf86887e 200
neilh20 2:f06fbf86887e 201 JSON output json.org
neilh20 2:f06fbf86887e 202 {key:value, key:value, key:value}
neilh20 2:f06fbf86887e 203 {time:[elapsed:<secs 0-2**32>, epoch2014:<secs 0-2**32>,wall:[date:<>,clock:<>],],
neilh20 2:f06fbf86887e 204 adcNorm:<float number 0.0-1.0>,
neilh20 2:f06fbf86887e 205 adcV:<Volts 0-VRef>
neilh20 2:f06fbf86887e 206 */
neilh20 15:91ef840c442c 207 bool manageUsbWrite(FILE* fpUsb) {
neilh20 2:f06fbf86887e 208 #define ADC_NORMALIZE (1.0f/65535.0f)
neilh20 2:f06fbf86887e 209 #define ADC_NORM_VREF 3.3
neilh20 2:f06fbf86887e 210 float data_n=( ((float)sample_u16[lp2i]) * ADC_NORMALIZE);
neilh20 11:fefb12811584 211 bool writeUsb=0;
neilh20 6:261c5054fd51 212 //Future redesign to use USBHALHost::wait_attach()
neilh20 15:91ef840c442c 213 if (outputHeaderUsb(fpUsb))
neilh20 2:f06fbf86887e 214 {
neilh20 2:f06fbf86887e 215 if (NULL !=(fpUsb = fopen("/usbAa/test1.txt", "a")))
neilh20 2:f06fbf86887e 216 {
neilh20 9:f3f7ca1da2a8 217 #if TsiActive
neilh20 9:f3f7ca1da2a8 218 //del pc.printf("%10d %5.4f (%6.4fV) %i\n\r",time_[lp2i],data_n,(ADC_NORM_VREF*data_n),lp2i);
neilh20 13:729f5ae38c24 219 fprintf(fpUsb, "{\"time\":{\"elapsed\":%10d},\"adcN\": %5.4f, \"adcV\":%6.4f, \"e0\":%i }\n",time_[lp2i],
neilh20 12:d2e6f79d6fdb 220 data_n,(ADC_NORM_VREF*data_n),
neilh20 13:729f5ae38c24 221 elec0p);
neilh20 9:f3f7ca1da2a8 222 #else
neilh20 2:f06fbf86887e 223 fprintf(fpUsb, "{\"time\":{\"elapsed\":%10d},\"adcN\": %5.4f, \"adcV\":%6.4f}\n",time_[lp2i],data_n,(ADC_NORM_VREF*data_n));
neilh20 9:f3f7ca1da2a8 224 #endif
neilh20 2:f06fbf86887e 225 fclose(fpUsb); fpUsb=NULL;
neilh20 11:fefb12811584 226 writeUsb=1;
neilh20 2:f06fbf86887e 227 } else {
neilh20 2:f06fbf86887e 228 //Potential Error
neilh20 2:f06fbf86887e 229 UsbPresent=0; //Can't say its dissappeared as will cause USB find problem.
neilh20 2:f06fbf86887e 230 pc.printf(" Error: USB unexpected can't write!!\n\r");
neilh20 11:fefb12811584 231 //writeUsb=0;
neilh20 2:f06fbf86887e 232 }
neilh20 2:f06fbf86887e 233 }
neilh20 11:fefb12811584 234 return writeUsb;
neilh20 2:f06fbf86887e 235 }
neilh20 2:f06fbf86887e 236 //--------------------------------------------------------------
neilh20 2:f06fbf86887e 237 /* Manage the sample
neilh20 2:f06fbf86887e 238 - store it
neilh20 2:f06fbf86887e 239 - and write it to USB stick
neilh20 2:f06fbf86887e 240 [FUT] detect USB stick and if not present write it when inserted
neilh20 2:f06fbf86887e 241
neilh20 2:f06fbf86887e 242 JSON output json.org
neilh20 2:f06fbf86887e 243 {key:value, key:value, key:value}
neilh20 2:f06fbf86887e 244 {time:[elapsed:<secs 0-2**32>, epoch2014:<secs 0-2**32>,wall:[date:<>,clock:<>],],
neilh20 2:f06fbf86887e 245 adcNorm:<float number 0.0-1.0>,
neilh20 2:f06fbf86887e 246 adcV:<Volts 0-VRef>}
neilh20 2:f06fbf86887e 247 */
neilh20 2:f06fbf86887e 248 void manageSample() {
neilh20 13:729f5ae38c24 249 float data_n;
neilh20 13:729f5ae38c24 250 int i_lp,idx;
neilh20 13:729f5ae38c24 251 int publishInst_raw;
neilh20 13:729f5ae38c24 252 bool noisey=0;
neilh20 13:729f5ae38c24 253
neilh20 13:729f5ae38c24 254 /* WaterDepth eTape sensor is noisey - average input for noise and compare against changes by capactive sensor
neilh20 13:729f5ae38c24 255 * Average published sensor and check if new reading changes by more than 1/300 of fsd if it does check against capacitive sensor and see if it is trending same direction
neilh20 13:729f5ae38c24 256 */
neilh20 13:729f5ae38c24 257 publishInst_raw = sample_u16[lp2i]; //over X previous
neilh20 13:729f5ae38c24 258 sampleInst_raw = sample_u16[lp2i]; //over X previous
neilh20 13:729f5ae38c24 259 elec0p = electr[lp2i];
neilh20 13:729f5ae38c24 260 for (i_lp =1; i_lp< SAMPLE_WIN; i_lp++) {
neilh20 13:729f5ae38c24 261 idx = lp2i-i_lp;
neilh20 13:729f5ae38c24 262 if (idx <0) idx+= BuffSize;
neilh20 13:729f5ae38c24 263 publishInst_raw += publish_u16[i_lp]; //over X previous
neilh20 13:729f5ae38c24 264 sampleInst_raw += sample_u16[i_lp]; //over X previous
neilh20 13:729f5ae38c24 265 elec0p += electr[i_lp];
neilh20 13:729f5ae38c24 266 }
neilh20 13:729f5ae38c24 267 publishInst_raw /= SAMPLE_WIN;
neilh20 13:729f5ae38c24 268 sampleInst_raw /= SAMPLE_WIN;
neilh20 13:729f5ae38c24 269 elec0p /= SAMPLE_WIN;
neilh20 13:729f5ae38c24 270 #if 0 //future
neilh20 13:729f5ae38c24 271 #define SAMPLES_ABS_CNT 5
neilh20 13:729f5ae38c24 272 if (SAMPLES_ABS_CNT < abs(sampleInst_raw - publishInst_raw) ) {
neilh20 13:729f5ae38c24 273 //Need to check if this is noise
neilh20 13:729f5ae38c24 274 //if ( abs(abs(elec0p) - electr[lp21]);
neilh20 13:729f5ae38c24 275 noisey = 1;
neilh20 13:729f5ae38c24 276 }
neilh20 13:729f5ae38c24 277 #endif //0
neilh20 13:729f5ae38c24 278 data_n=sampleInst_raw * ADC_NORMALIZE;
neilh20 13:729f5ae38c24 279 pc.printf("\n\r%10d %5.4f (%6.4fV) %i %i N%i %i",time_[lp2i],data_n,(ADC_NORM_VREF*data_n),
neilh20 13:729f5ae38c24 280 electr[lp2i],elec0p, noisey,
neilh20 12:d2e6f79d6fdb 281 lp2i);
neilh20 11:fefb12811584 282
neilh20 11:fefb12811584 283 //ledGrn = !(lp2i&0x01);//!ledGrn; syncs with blue led flashing
neilh20 13:729f5ae38c24 284 //ledPwmGrn = ((lp2i&0x01) ? (1.0-? ? ?) : 1.0 ); //LEDGrn=1 off, =0 On
neilh20 13:729f5ae38c24 285 if (lp2i&0x01) {//Only write every two times
neilh20 15:91ef840c442c 286 if (1/*ToDo==manageUsbWrite(*fp)*/) {
neilh20 13:729f5ae38c24 287 ledBlue = !ledBlue; //(lp2i&0x03);
neilh20 13:729f5ae38c24 288 } else {ledBlue =1;}//off
neilh20 13:729f5ae38c24 289 }
neilh20 2:f06fbf86887e 290
neilh20 2:f06fbf86887e 291 }//end manageSample
neilh20 2:f06fbf86887e 292
neilh20 2:f06fbf86887e 293 //--------------------------------------------------------------
neilh20 2:f06fbf86887e 294 /* Manage the sample
neilh20 2:f06fbf86887e 295 - take an ADC sample and
neilh20 2:f06fbf86887e 296 */
neilh20 2:f06fbf86887e 297 void manageAdcIn(void){
neilh20 2:f06fbf86887e 298 //Take samples and store in buffer
neilh20 2:f06fbf86887e 299 if (++lp2i >= BuffSize) lp2i=0;
neilh20 2:f06fbf86887e 300 sample_u16[lp2i]=ainPinAA.read_u16();
neilh20 2:f06fbf86887e 301 time_[lp2i]=(unsigned int)tmrE_evt; //TODO - replace with wall time
neilh20 15:91ef840c442c 302 #if TsiActive
neilh20 13:729f5ae38c24 303 tsiSlider_dist = TsiRead();
neilh20 13:729f5ae38c24 304 electr[lp2i]= tsi.getDelta0();
neilh20 15:91ef840c442c 305 #endif //TsiActive
neilh20 13:729f5ae38c24 306
neilh20 13:729f5ae38c24 307 //elec1p = tsi.getDelta1();
neilh20 13:729f5ae38c24 308 //tsiSlider_dist = (float)tsi.readPercentage();
neilh20 12:d2e6f79d6fdb 309
neilh20 2:f06fbf86887e 310 }//manageAdcIn
neilh20 2:f06fbf86887e 311
neilh20 15:91ef840c442c 312 //--------------------------------------------------------------
neilh20 15:91ef840c442c 313 void UsbHostMsd_task(void const *) {
neilh20 15:91ef840c442c 314 USBHostMSD msd("usbAa"); //defines the file system access
neilh20 15:91ef840c442c 315 int i = 0;
neilh20 15:91ef840c442c 316
neilh20 15:91ef840c442c 317 while(1) {
neilh20 15:91ef840c442c 318
neilh20 15:91ef840c442c 319 // try to connect a MSD device
neilh20 15:91ef840c442c 320 while(!msd.connect()) {
neilh20 15:91ef840c442c 321 //Thread::wait(500);
neilh20 15:91ef840c442c 322 }
neilh20 15:91ef840c442c 323
neilh20 15:91ef840c442c 324 // in a loop, append a file
neilh20 15:91ef840c442c 325 // if the device is disconnected, we try to connect it again
neilh20 15:91ef840c442c 326 while(1) {
neilh20 15:91ef840c442c 327
neilh20 15:91ef840c442c 328 // append a file
neilh20 15:91ef840c442c 329 FILE * fp = fopen("/usb/test1.txt", "a");
neilh20 15:91ef840c442c 330
neilh20 15:91ef840c442c 331 if (fp != NULL) {
neilh20 15:91ef840c442c 332 fprintf(fp, "Hello fun SD Card World: %d!\r\n", i++);
neilh20 15:91ef840c442c 333 printf("Goodbye World!\r\n");
neilh20 15:91ef840c442c 334 fclose(fp);
neilh20 15:91ef840c442c 335 } else {
neilh20 15:91ef840c442c 336 printf("FILE == NULL\r\n");
neilh20 15:91ef840c442c 337 }
neilh20 15:91ef840c442c 338
neilh20 15:91ef840c442c 339 //Thread::wait(500);
neilh20 15:91ef840c442c 340
neilh20 15:91ef840c442c 341 // if device disconnected, try to connect again
neilh20 15:91ef840c442c 342 if (!msd.connected())
neilh20 15:91ef840c442c 343 break;
neilh20 15:91ef840c442c 344 }
neilh20 15:91ef840c442c 345
neilh20 15:91ef840c442c 346 }
neilh20 15:91ef840c442c 347 }
neilh20 15:91ef840c442c 348 /* Input parser from usb - but how large is buffer
neilh20 15:91ef840c442c 349 char buffer[128];
neilh20 15:91ef840c442c 350 pc.gets(buffer, 4);
neilh20 15:91ef840c442c 351 pc.printf("I got '%s'\n", buffer);
neilh20 15:91ef840c442c 352 */
neilh20 2:f06fbf86887e 353 //--------------------------------------------------------------
neilh20 3:d94384ab6f12 354 int main() {
neilh20 2:f06fbf86887e 355
neilh20 2:f06fbf86887e 356 //TODO: check RCM_SRS0 &RS1
neilh20 2:f06fbf86887e 357 warmBoot = rtc_isenabled();
neilh20 2:f06fbf86887e 358 if (!warmBoot) {
neilh20 2:f06fbf86887e 359 //TODO figure out how to manage wall time
neilh20 2:f06fbf86887e 360 rtc_init(); //Assumes external clock - TODO: options int32Khz RTC_CLKIN ext32KhzXtal
neilh20 2:f06fbf86887e 361 rtc_write(0x0); //Init to some default;
neilh20 2:f06fbf86887e 362 }
neilh20 4:a3692e095388 363
neilh20 4:a3692e095388 364 ledRed =0;//ON
neilh20 11:fefb12811584 365 //ledGrn =1; //off
neilh20 11:fefb12811584 366 ledPwmGrn =1.0; //off
neilh20 4:a3692e095388 367 ledBlue = 1; //on
neilh20 15:91ef840c442c 368
neilh20 15:91ef840c442c 369 //Thread msdTask(UsbHostMsd_task, NULL, osPriorityNormal, 1024 * 4);
neilh20 15:91ef840c442c 370
neilh20 6:261c5054fd51 371 wait_ms(5000);
neilh20 4:a3692e095388 372 //Thread::wait(2000);
neilh20 2:f06fbf86887e 373 ledRed =1;//Off
neilh20 11:fefb12811584 374 //ledGrn =1; //off
neilh20 11:fefb12811584 375 ledPwmGrn =1.0; //off
neilh20 2:f06fbf86887e 376 ledBlue = 0; //on
neilh20 2:f06fbf86887e 377 pc.baud(115200);//Opt 57600
neilh20 6:261c5054fd51 378
neilh20 6:261c5054fd51 379 pc.printf("\n\r%s Logger v0.1 \n\r Sample [time=%dsec, size=%d] Clock=%d\n\r",meProc, sysTockInit_sec,BuffSize,SystemCoreClock);
neilh20 2:f06fbf86887e 380
neilh20 2:f06fbf86887e 381 outputHeaders();
neilh20 2:f06fbf86887e 382
neilh20 2:f06fbf86887e 383 tock.attach(&tockEvent,sysTockInit_sec);
neilh20 2:f06fbf86887e 384
sas37 0:eb0dc2b5bc51 385 while (true) {
neilh20 2:f06fbf86887e 386 if(sysEventA)
neilh20 2:f06fbf86887e 387 {
neilh20 2:f06fbf86887e 388 sysEventA=0;
neilh20 2:f06fbf86887e 389 manageAdcIn();
neilh20 2:f06fbf86887e 390 manageSample();
neilh20 2:f06fbf86887e 391 }//else
neilh20 2:f06fbf86887e 392 // TODO go into power down till next event
sas37 0:eb0dc2b5bc51 393 }
neilh20 2:f06fbf86887e 394
sas37 0:eb0dc2b5bc51 395 }