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

Revision:
11:fefb12811584
Parent:
10:b74fa5ec4af4
Child:
12:d2e6f79d6fdb
--- a/main.cpp	Tue Nov 11 18:01:25 2014 +0000
+++ b/main.cpp	Tue Nov 11 18:59:48 2014 +0000
@@ -61,10 +61,14 @@
 #elif defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
   #define TsiActive 0
   #include "tsi_sensor.h"
+  //Cap Sensor B0=CH0, B1=CH6
+  //#define ELEC0 0
+  //#define ELEC1 6
+  //FRDM_KL25x slider electrodes - TSI0_CH9=PTB16, CH10=B17
   #define ELEC0 9
   #define ELEC1 10
    TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
-   //#define TsiRead()  tsi.readDistance()
+   //#define TsiRead()  tsi.readDistance() - has problesm
    #define TsiRead()  tsi.readPercentage()
 #elif defined (TARGET_KL05Z)
   #define TsiActive 0
@@ -80,6 +84,7 @@
 #endif
 
 float tsiSlider_dist;
+
 //size of sample  buffer 
 #define BuffSize 16  //16 to test, upto kl25z spare ram of about xxxx?, !2000, linker warns if too large, but also fails
 //set the delay used by the ticker
@@ -89,7 +94,8 @@
 
 //Device Init - per FRDM-KL25Z
 DigitalOut ledRed(LED1); 
-DigitalOut ledGrn(LED2); 
+//DigitalOut ledGrn(LED2); 
+PwmOut ledPwmGrn(LED2);
 DigitalOut ledBlue(LED3); 
 FastAnalogIn ainPinAA(PTC2); ///AnalogAin pin
 
@@ -184,11 +190,11 @@
  adcNorm:<float number 0.0-1.0>, 
  adcV:<Volts 0-VRef>
  */
-void manageUsbWrite() {
+bool manageUsbWrite() {
 #define ADC_NORMALIZE (1.0f/65535.0f)
 #define ADC_NORM_VREF 3.3
     float data_n=( ((float)sample_u16[lp2i]) * ADC_NORMALIZE);
-    
+    bool writeUsb=0;
     //Future redesign to use USBHALHost::wait_attach()
     if (outputHeaderUsb()) 
     {
@@ -202,13 +208,15 @@
            fprintf(fpUsb, "{\"time\":{\"elapsed\":%10d},\"adcN\":  %5.4f, \"adcV\":%6.4f}\n",time_[lp2i],data_n,(ADC_NORM_VREF*data_n));
 #endif
            fclose(fpUsb); fpUsb=NULL;
-           ledGrn = !ledGrn;    
+           writeUsb=1;
        } else {
            //Potential Error
            UsbPresent=0; //Can't say its dissappeared as will cause USB find problem.
            pc.printf("  Error: USB unexpected can't write!!\n\r");
+           //writeUsb=0;
        }
     } 
+    return writeUsb;
  } 
 //--------------------------------------------------------------
 /* Manage the sample
@@ -225,9 +233,13 @@
 void manageSample() {
     float data_n=( ((float)sample_u16[lp2i]) * ADC_NORMALIZE);
     pc.printf("\n\r%10d  %5.4f (%6.4fV) %4.2f %i",time_[lp2i],data_n,(ADC_NORM_VREF*data_n),tsiSlider_dist, lp2i);
-    ledBlue = !ledBlue;   
-    
-    manageUsbWrite();
+
+    //ledGrn = !(lp2i&0x01);//!ledGrn; syncs with blue led flashing    
+    ledPwmGrn = ((lp2i&0x01) ? (1.0-tsiSlider_dist) : 1.0 ); //LEDGrn=1 off, =0 On
+
+    if (1==manageUsbWrite()) {
+        ledBlue = (lp2i&0x01);//!ledBlue;   
+    } else {ledBlue =1;}//off
 
 }//end manageSample
 
@@ -255,12 +267,14 @@
     }
 
     ledRed =0;//ON
-    ledGrn =1; //off
+    //ledGrn =1; //off
+    ledPwmGrn =1.0; //off
     ledBlue = 1; //on    
     wait_ms(5000); 
     //Thread::wait(2000);
     ledRed =1;//Off
-    ledGrn =1; //off
+    //ledGrn =1; //off
+    ledPwmGrn =1.0; //off
     ledBlue = 0; //on
     pc.baud(115200);//Opt 57600