SlideShare a Scribd company logo
1 of 33
Download to read offline
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 1 of 33 [Lab1/2/3=8/17/26]
Kinetis L Hands-on Manual
With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy)
Libor GECNUK
Lab0 – mbed.org (v0.0)
Lab1 – USB/HID-Mouse (v0.1)
Lab2 – Sensors & USB/CDC-Serial (v0.1)
Lab3 – USB/MSD-Drive & eCompass (v0.0)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 2 of 33 [Lab1/2/3=8/17/26]
Introduction
This hands-on investigates on generic functionality on the
FRDM-KL46Z board using on board sensor set (acc-
accelerometer, mag-magnetometer, tsi-touch sensing
interface, light-sensor) used with couple USB Classes.
Lab1&2 presents USB-device functionality (HID, CDC) and
Lab3 USB-Host functionality. Coding and compiling is done
in mbed.org. Labs shows mbed setup and initialization of
FRDM and MCU programming (not debugging, if it is
needed mbed allows export project into IDE’s e.g. Keil).
Lab1 description
This Lab uses FRDM-KL64Z board, coding is in mbed.org.
Lab0 shows the way how to enable mbed @OpenSDA,
mbed.org login and compiler usage, selecting right
platform. Then we create first Hello World “blinky”
program in 60seconds using Import and then Export to
save it.
Lab0 Teaser
1/ mbed @OpenSDA
2/ mbed start/login
3/ platform select
4/ first blinky program using Import
 a/ blinky Green LED
5/ export program
6/ Q&A
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 3 of 33 [Lab1/2/3=8/17/26]
0.1 mbed @OpenSDA:
http://mbed.org/handbook/Firmware-FRDM-KL46Z
Press&Hold Reset@FRDM … plug mUSB cable
into OpenSDA debug interface and hold Reset till
green LED starts blinking (~1s).
Then Drag&Drop (or SendTo) file
mbed_if_v2.1_frdm_kl46z.s19 onto disk
BOOTLOADER and wait untill OpenSDA firmware is
updated (so then FRDM will be ready for Drag&Drop
proramming). Unplug and plug USB to finalize.
0.2 mbed start: Go to https://mbed.org/ and Login or
signup.
Put your Username and your Password and Login.
When you are logged in then click on Compiler.
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 4 of 33 [Lab1/2/3=8/17/26]
So you can see mbed Workspace Managem
0.3 mbed Select Platform: at No device selected
at FRDM KL46Z or Add a device (for the first
time user)
at Select Platoform
(for the first time user)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 5 of 33 [Lab1/2/3=8/17/26]
(for the first time) in Platform window select FRDM-
KL46Z and (click at Freescale filter on right side
of this web page to see just Freescale platforms)
(for the first time) in Platform window “FRDM-KL46Z
selected” at Add to your mbed Compiler
Go back to the Compiler window and check that
you have selected right platform FRDM-KL46Z
/done! (Platform selection, from this point will be
created all handson)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 6 of 33 [Lab1/2/3=8/17/26]
0.4 first mbed program Blinky
Click on Compiler
Click on New
Enter ”60s” and use Template “gpio example …” and
click OK
Click Compile!
Save file 60s_KL64Z.bin onto the MBED drive
Press the reset button and the RED LED blinks
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab0: mbed.org
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 7 of 33 [Lab1/2/3=8/17/26]
0.5 mbed Export Program: @KL46Z-Lab0 and
select “Export Program…”
0.6 Q&A … Thanks!!!
/End of Lab0
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 8 of 33 [Lab1/2/3=8/17/26]
Kinetis L Hands-on Manual
With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy)
Libor GECNUK
Lab0 – mbed.org (v0.0)
Lab1 – USB/HID-Mouse (v0.1)
Lab2 – Sensors & USB/CDC-Serial (v0.1)
Lab3 – USB/MSD-Drive & eCompass (v0.0)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 9 of 33 [Lab1/2/3=8/17/26]
Introduction
This hands-on investigates on generic functionality on the
FRDM-KL46Z board using on board sensor set (acc-
accelerometer, mag-magnetometer, tsi-touch sensing
interface, light-sensor) used with couple USB Classes.
Lab1&2 presents USB-device functionality (HID, CDC) and
Lab3 USB-Host functionality. Coding and compiling is done
in mbed.org. Labs shows mbed setup and initialization of
FRDM and MCU programming (not debugging, if it is
needed mbed allows export project into IDE’s e.g. Keil).
Lab1 description
This Lab uses FRDM-KL64Z board, coding is in mbed.org.
Lab1 shows a/ accelerometer as source of X&Y
coordination for the USB HID (Mouse) application while
Red LED shows Z-axis and b/ usage of switch SW1, SW3
utilized as Left&Right click.
Lab1 Teaser
1/ new program
2/ import Libs (mbed+acc)
3/ coding: from mouse example
4/ compiling, flashing mcu
 a/ mouse
5/ coding: buttons
 b/ mouse & buttons
6/ export program
7/ homework
8/ Q&A
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 10 of 33 [Lab1/2/3=8/17/26]
1.1 mbed New Program: In the mbed Program
Workspace, and go for [My Programs | New
Program… ].
Use Template ‘Empty Program’. Into Program
Name type ‘KL46Z-Lab1’ @OK
at KL46Z-Lab1 and at New File
Type main_lab1.cpp and @OK
/done! (Project name, main.cpp)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 11 of 33 [Lab1/2/3=8/17/26]
1.2 mbed import Library (mbed): In the mbed
Program Workspace, @Import
And select mbed and (or drag&drop) to add
this Library into actual project (KL46Z-Lab#)
… so you will have this view …
..then .. Import Library (USB Device): In the
mbed Program Workspace, @Import and type
‘USB’ @Search . Select first
one ‘USB Device’ (from author ‘mbed official’) and
to add it into actual project.
..then.. Import Library (MMA8451Q): In the
mbed Program Workspace, @Import and type
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 12 of 33 [Lab1/2/3=8/17/26]
‘MMA8451Q’ and Search. .
Select first (from author ‘Johan Kritzinger’) and .
/done! (mbed Libraries imports)
1.3 Writing code: Goto USBHIDUSBMouse.h and
find USBMouse example …
… and copy&paste this example into main.cpp …
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 13 of 33 [Lab1/2/3=8/17/26]
... then goto MMA8451QMMA8451Q.h and copy
example from there and paste it into main.cpp …
… then before main()-loop add code: #defines and
#definitions required for the program …
.. then in main()-loop add code and modify code ..
.. then review your code and check if is ok.
/done! (added DigitalOut gLED, PwmOut rLED,
Structure sensorData, linked accXY sensor data as
mouse XY coordination, changed main loop period to
50ms for suitable mouse response)
int main(void)
{
gLED = LED_ON; //Green LED ON to indicate running program
rLED = LED_OFF; //Red LED OFF
rLED.period(rLEDperiod); //Red LED (rLED) blinkig shows accZ
while (1) {
sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1)
sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1)
sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED
//accXY -> mouse movements from FRDM XY inclination
mouse.move(sD.accValX*16, sD.accValY*16);
//acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking
rLED = abs(sD.accValZ);
wait(0.05); //wait 50ms
}
}
#include "mbed.h"
#include "MMA8451Q.h"
#include "USBMouse.h" //Lab1-Hid
#define LED_ON 0 //outON, pwmON
#define LED_OFF 1 //outOFF,pwmOFF
DigitalOut gLED(LED_GREEN); //PTD5
#define rLEDperiod 150 //[ms]
PwmOut rLED(LED_RED); //PTE29
#define MMA8451_I2C_ADDRESS (0x1d<<1)
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
USBMouse mouse;
struct KL46_SENSOR_DATA {
float accValX;
float accValY;
float accValZ;
} sensorData;
#define sD sensorData
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 14 of 33 [Lab1/2/3=8/17/26]
1.4 mbed Compile & Program FRDM board: In the
mbed Program Workspace, @Compile (or
CTRL-D; note: CTRL-B is just build)
.. ..
(be sure that Cable/mUSB is at OpenSDA/mUSB.)
... and @SaveAs and store *.bin into
(when FRDM board is USB plugged to PC) …
.. wait for stop blinking of OpenSDA-green-LED to finish!
/done! (FRDM board programming, flashing target mcu)
#include "mbed.h"
#include "MMA8451Q.h"
#include "USBMouse.h"
#define LED_ON 0 //outON, pwmON
#define LED_OFF 1 //outOFF,pwmOFF
DigitalOut gLED(LED_GREEN); //PTD5
#define rLEDperiod 150 //[ms]
PwmOut rLED(LED_RED); //PTE29
#define MMA8451_I2C_ADDRESS (0x1d<<1)
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
USBMouse mouse;
struct KL46_SENSOR_DATA {
float accValX;
float accValY;
float accValZ;
} sensorData;
#define sD sensorData
int main(void)
{
gLED = LED_ON; //Green LED ON to indicate running program
rLED = LED_OFF; //Red LED OFF
rLED.period(rLEDperiod); //Red LED (rLED) blinkig shows accZ
while (1) {
sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1)
sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1)
sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED
//accXY -> mouse movements from FRDM XY inclination
mouse.move(sD.accValX*16, sD.accValY*16);
//acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking
rLED = abs(sD.accValZ);
wait(0.05); //wait 50ms
}
}
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 15 of 33 [Lab1/2/3=8/17/26]
1.5 Mouse example (emulate PC mouse by FRDM
using accelerometer): swap mUSB-Cable from
OpenSDA/mUSB to KL46/mUSB connector and wait
for Windows messages (Installing device driver
software -> HID or USB input device installation ->
Your device is ready to use). Note: no drivers are
needed as this USB HID class is already natively in
Windows.
..
/done! (FRDM as PC mouse XY pointer, no touch yet)
Add mouse buttons: In the mbed add defines into
main_lab1.cpp.
.. then add cooperative code for mouse-click into
main()-loop ..
#include "mbed.h"
#include "USBMouse.h"
#include "MMA8451Q.h"
#define LED_ON 0 //outON, pwmON
#define LED_OFF 1 //outOFF,pwmOFF
DigitalOut gLED(LED_GREEN); //PTD5
#define PRESS_ON 0
#define PRESS_OFF 1
DigitalIn sw1(PTC3); //if(sw1) Release else Press
DigitalIn sw3(PTC12); //while(sw3); wait for Press
#define rLEDperiod 150 //[ms]
PwmOut rLED(LED_RED); //PTE29
#define MMA8451_I2C_ADDRESS (0x1d<<1)
MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
USBMouse mouse;
struct KL46_SENSOR_DATA {
int sw1State;
int sw3State;
float accValX;
float accValY;
float accValZ;
} sensorData;
#define sD sensorData
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab1: USB/HID-Mouse
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 16 of 33 [Lab1/2/3=8/17/26]
.. Compile & Program FRDM board using
OpenSDA/mUSB connector and try mouse example
with click left & right buttons using KL46/mUSB
connector.
1.6 Mbed Export Program: @KL46Z-Lab# and
select “Export Program…”
.. and select “mbed Online IDE” -> @ Export.
SaveAs and use e.g. predefined name “KL46Z-
Lab1_mouse.zip” and store it to local disc.
1.7 Homework …
1/ Try composite usb class “keyboard + mouse” (e.g.
open window)
2/ Try implementing mouse with dynamic speed
3/ Try detect light. Use it for enabling dynamically
mouse functionality only when hand is above board.
4/ Try to use eCompass for X-axis coordination
1.8 Q&A … Thanks!!!
/End of Lab1
int main(void)
{
sw1.mode(PullUp); sw3.mode(PullUp);
gLED = LED_ON; //Green LED ON to indicate running program
rLED = LED_OFF; //Red LED OFF
rLED.period(rLEDperiod); //Red LED (rLED) blinkig shows accZ
while (1) {
sD.sw1State = sw1; sD.sw3State = sw3;
sensorData.accValX = acc.getAccX(); //accX[-1..1]->mouse
sensorData.accValY = acc.getAccY(); //accY[-1..1]->mouse
sensorData.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED
if(sD.sw1State==PRESS_ON) //mouse left button
mouse.press(MOUSE_LEFT);
else
mouse.release(MOUSE_LEFT);
if(sD.sw3State==PRESS_ON) //mouse right button
mouse.press(MOUSE_RIGHT);
else
mouse.release(MOUSE_RIGHT);
//accXY -> mouse movements from FRDM XY inclination
mouse.move(sensorData.accValX*16, sensorData.accValY*16);
//accZ -> red blinking duty-cycle (maxZ -> maxDutyCycle)
rLED = 1 - abs(sensorData.accValZ);
wait(0.05); //wait 50ms
}
}
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 17 of 33 [Lab1/2/3=8/17/26]
Kinetis L Hands-on Manual
With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy)
Libor GECNUK
Lab0 – mbed.org (v0.0)
Lab1 – USB/HID-Mouse (v0.1)
Lab2 – Sensors & USB/CDC-Serial (v0.1)
Lab3 – USB/MSD-Drive & eCompass (v0.0)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 18 of 33 [Lab1/2/3=8/17/26]
Introduction
This hands-on investigates on generic functionality on the
FRDM-KL46Z board using on board sensor set (acc-
accelerometer, mag-magnetometer, tsi-touch sensing
interface, light-sensor) used with couple USB Classes.
Lab1&2 presents USB-device functionality (HID, CDC) and
Lab3 USB-Host functionality. Coding and compiling is done
in mbed.org. Labs shows mbed setup and initialization of
FRDM and MCU programming (not debugging, if it is
needed mbed allows export project into IDE’s e.g. Keil).
Lab2 description
This Lab uses FRDM-KL64Z board, coding is in mbed.org.
Lab2 shows a/ print out capability using PC terminal using
OpenSDA composite port, b/ sensing capability and using
same port and c/ print out at sensing capability using
Virtual Com port at KL46Z.
Lab2 Teaser
1/ copy program | import
2/ remove unwanted code from Lab1
3/ import mag+tsi+slcd and add serial i/o for PC-terminal
4/ coding: all sensor support, add wait/slcd(), serial,
sensing, printout
5/ install mbed terminal drivers @ Windows …
 a/ sensor data at PC-console via OpenSDA
6/ add USB-cdc, install mbed/serial.inf @ Windows
 c/ sensor data at PC-console directly via KL46Z
7/ export program
8/ homework
9/ Q&A
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 19 of 33 [Lab1/2/3=8/17/26]
2.1 mbed Import Program (from previous Lab1):
@Import in mbed Workspace -> @Upload ->
@Browse… and find “KL46Z-Lab1export.zip”.
Select this file and @Import in Import Wizard.
.. and change the Import Name to “KL46Z-Lab2”
@Import button.
.. then rename main_lab1.cpp -> main_lab2.cpp
(Note: its previous example but with added
formatting for better orientation in next examples)
You can click at ‘The document is out of date’ to
have Classes help in the Program Workspace (Note
that Classes will be grouped in workspace)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 20 of 33 [Lab1/2/3=8/17/26]
2.2 remove unwanted code from Lab1
2.3 mbed Import Library (MAG3110): In the mbed
Program Workspace, @Import and type ‘mag’
@Search . Select first one
‘MAG3110’ (from author ‘Andrew Lindsay’
http://mbed.org/users/SomeRandomBloke/code/MA
G3110/ ) and to add it into actual project.
Note: please make sure you make selection at
project before adding this Lib into this project.
In the same way …
Import Library ‘TSI’ from ‘First Last’ and
Import Library ‘SLCD-’ from ‘’Libor GECNUK’
(added support for sigh ‘-’ from original SLCD lib)
… so you will have next view …
#include "USBMouse.h" //Lab1-Hid
USBMouse mouse; //Lab1-Hid
if(sD.sw1State==PRESS_ON) //mouse left button
mouse.press(MOUSE_LEFT);
else
mouse.release(MOUSE_LEFT);
if(sD.sw3State==PRESS_ON) //mouse right button
mouse.press(MOUSE_RIGHT);
else
mouse.release(MOUSE_RIGHT);
//accXY -> mouse movements from FRDM XY inclination
mouse.move(sensorData.accValX*16, sensorData.accValY*16);
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 21 of 33 [Lab1/2/3=8/17/26]
2.4 coding all sensors support: add include, defines … …add function SLCD_blinking_msg_wait(): it will
be used to wait for start program until pressed &
released SW1 switch @FRDM and also to show
@SLCD custom messages …
… in main() add wait&slcd() and serial coop-code …
//---- MAIN/Inits (Wait4SW1) -> Start! --------------------//
//wait for Press SW1 - e.g. for HID/CDC/MSD Windows install.
SLCD_blinking_msg_wait(" o","PrES");//Lab1=Hid;2=cdc;3=Msd
//---- MAIN/Inits Interface -------------------------------//
usb_osda.baud(115200);
usb_osda.printf("n___________________________________rn");
usb_osda.printf("nFRDM-KL46Z_Labrn rn I am a CDC serial port
@OpenSDA/mUSB. Baud=115200 rn");
//_____________________________________________________________//
//======== FUNC() =============================================//
//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
//Lab2add
void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2)
{
char wait4sw1=0; //~500ms blinking
//wait for Press SW1 - to start mag calibration
while(sw1 == PRESS_ON); //wait for release
while(sw1 == PRESS_OFF) { //wait for press
if (++wait4sw1 < 150) //300ms
slcd.printf(slcd_msg1);
else //200ms
slcd.printf(slcd_msg2);
wait(0.002);
}
while(sw1 == PRESS_ON); //wait for release
}
//======== INCLUDES ===========================================//
#include "MAG3110.h"
#include "SLCD.h"
#include "TSISensor.h"
//======== DEFINES & VARIABLES ================================//
SLCD slcd; //[88:88][8.8.8.8] SegmentLCD
AnalogIn light(PTE22); //analog-light input
TSISensor slider; //Capacitive Touch Slider
MAG3110 mag(PTE25, PTE24); //Magnetometer
Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal
#define pf usb_osda //printf out -> osda (lab1,2,3)
struct KL46_SENSOR_DATA {
int sw1State;
int sw3State;
float accValX;
float accValY;
float accValZ;
float slider;
float light;
int magValX;
int magValY;
int magValZ;
} sensorData;
#define sD sensorData
void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2);
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 22 of 33 [Lab1/2/3=8/17/26]
… in main() also add program flow and sensing code
for data acquisition …
2.5 install mbed Serial-PC-terminal support from
hands-on materials mbedWinSerial_16466.exe
(Note you can find latest version @mbed.org
http://mbed.org/handbook/SerialPC and install it)
Install PC-terminal (e.g. TeraTerm, putty)
.. after installations …
Open PC-terminal and configure communication for
115200/8/N/1 ... open port!
/done! (Serial communication through OpenSDA)
Save -> @Compile -> Save As and download
KL46Z-Lab2_KL46Z.bin into FRDM (note: after
download the program flow is waiting for your SW1-
press & release to start send full sensing data into
PC-terminal via OpenSDA/mUSB when SW1-
pressed)
// MAIN/Loop/Processing and Actions -------------------//
//disable all SLCD DPs
slcd.DP(0, false); slcd.DP(1, false); slcd.DP(2, false);
// MAIN/Loop/Sensing and Storing data -----------------//
sD.sw1State = sw1; sD.sw3State = sw3;
sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1)
sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1)
sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED
sD.slider = slider.readPercentage() * 100;
sD.light = light;
sD.magValX = mag.readVal(MAG_OUT_X_MSB);
sD.magValY = mag.readVal(MAG_OUT_Y_MSB);
sD.magValZ = mag.readVal(MAG_OUT_Z_MSB);
// MAIN/Loop/Processing and Actions -------------------//
//sensor -> terminal
if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking
pf.printf(" rn");
pf.printf(" Switches. Light . Slider . Accelerometer .
Magnetometer . Compassrn");
pf.printf(" SW1:SW2| LUX | TSI | accX : accY : accZ | magX:
magY: maxZ | Headingrn");
pf.printf(" %d : %d | %1.3f | %2.0f %% | %+1.3f:%+1.3f:%+1.3f|
%5d:%5d:%5d | Lab3 rn",
sD.sw1State, sD.sw3State, sD.light, sD.slider, sD.accValX,
sD.accValY, sD.accValZ,
(short)sD.magValX, (short)sD.magValY, (short)sD.magValZ);
} gLED = LED_OFF;
//acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking
rLED = abs(sD.accValZ);
if (sD.slider)
slcd.printf(" %3.0f", sD.slider); //night->light .. 1->0
else
slcd.printf("%1.3f", sD.light); //left->right .. 0->100%
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 23 of 33 [Lab1/2/3=8/17/26]
SLCD shows Light and when you touch slider it will
show finger position at slider @FRDM.
Red LED is still showing z-axis inclination from
accelerometer.
/done! (All sensors data)
2.6 add CDC-serial class support: includes, and
//comment line ‘…pf usb_osda …’ (so the printf() will
send data into PC not via OpenSDA/mUSB but
through KL46Z/mUSB) and add USBSerial support …
Save -> @Compile -> Save As and download
KL46Z-Lab2_KL46Z.bin into FRDM.
//======== INCLUDES ===========================================//
#include "USBSerial.h" //Lab2-cdc
//======== DEFINES & VARIABLES ================================//
Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal
//#define pf usb_osda //printf out -> osda (lab1,2,3)
USBSerial usb_kl46; //KL46 Terminal
#define pf usb_kl46 //printf out -> kl46 (lab2 cdc)
//---- MAIN/Inits Labs ------------------------------------//
usb_kl46.printf("n___________________________________rn");
usb_kl46.printf("nFRDM-KL46Z_Labrn rn I am a "Virtual" CDC
serial port @KL46Z/mUSB. Baud=NA rn");
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 24 of 33 [Lab1/2/3=8/17/26]
… swap mUSB-Cable from OpenSDA/mUSB to
KL46/mUSB (Note: first-time users, pls wait for
proper installation of Virtual-CDC-Serial driver
serial.inf into Windows) …
..
… so then you can check it in device manager …
or ( )
(Left: “one” cable in the KL46/mUSB)
(Right: “two” cables, KL46/mUSB &
mbed:OpenSDA/mUSB … is the set-up which could
be used e.g. to see additional statuses about USB
module, communications etc)
.. in the PC-terminal (e.g. TeraTerm) refresh
available Serial Ports and select “Mbed Virtual
Serial Port” and open this port for communication
…
Click SW1 to see All sensor data @PC-terminal …
Note: that baud rate is not important information for
Virtual Serial Port (using CDC-Class) as it
communicates always at max speed of USB.
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab2: Sensors & USB/CDC-Serial
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 25 of 33 [Lab1/2/3=8/17/26]
2.7 Mbed Export Program: @KL46Z-Lab# and
select “Export
Program…”
->
.. and select “mbed Online IDE” -> @ Export.
SaveAs and use e.g. predefined name “KL46Z-
Lab2_serial.zip”
2.8 Homework …
1/ Try sending date only when lower light indicate
human hand proximity
2/ Try recognize by your embedded code that CDC-
Serial is ready and after this send that info to
terminal.
3/ Try scan commands from terminal (e.g. getc())
4/ Try use composite usb class “keyboard + mouse”
to open e.g notepad in Windows, put few sensor
messages, save it, and close it.
5/ Try to use usb analyzer 
2.9 Q&A … Thanks!!!
/End of Lab2
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 26 of 33 [Lab1/2/3=8/17/26]
Kinetis L Hands-on Manual
With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy)
Libor GECNUK
Lab0 – mbed.org (v0.0)
Lab1 – USB/HID-Mouse (v0.1)
Lab2 – Sensors & USB/Serial (v0.1)
Lab3 – USB/MSD-Drive & eCompass(v0.0)
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 27 of 33 [Lab1/2/3=8/17/26]
Introduction
This hands-on investigates on generic functionality on the
FRDM-KL46Z board using on board sensor set (acc-
accelerometer, mag-magnetometer, tsi-touch sensing
interface, light-sensor) used with couple USB Classes.
Lab1&2 presents USB-device functionality (HID, CDC) and
Lab3 USB-Host functionality. Coding and compiling is done
in mbed.org. Labs shows mbed setup and initialization of
FRDM and MCU programming (not debugging, if it is
needed mbed allows export project into IDE’s e.g. Keil).
Lab3 description
This Lab uses FRDM-KL64Z board, coding is in mbed.org.
Lab3 shows a/ FRDM as USB host application with
capability write and read USB-drive destination and b/
eCompass calibration and usage of FRDM and tracker.
Lab3 Teaser
1/ new & copy (drag & drop) mbed program
2/ remove unwanted code from Lab2
3/ coding, adding USB/MSD
 a/ writing acc Z-axis@USB-drive
4/ coding eCompass (need calibration)
 a/ eCompass Heading @USB-drive
5/ homework
6/ Q&A
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 28 of 33 [Lab1/2/3=8/17/26]
3.1 mbed New&Copy Program: In the mbed
Program Workspace, and go for [My Programs
| New Program… ].
Use Template ‘Empty Program’. Into Program
Name type ‘KL46Z-Lab3’ @OK
mbed Copy Program: @KL46Z-Lab2 and select
all content (except USB Device) using CTRL+
and copy that using CTRL+C and CTRL+V @KL46Z-
Lab3. (Note: also is possible use drag&drop and
then select from context menu ‘Copy Here’)
… and rename -> …
… import “USB Host” Library ( serach ‘usb kl46’ USB
host by ‘Norimasa Okamoto’) and to add it into
actual project.
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 29 of 33 [Lab1/2/3=8/17/26]
3.2 remove unwanted code from Lab2
3.3 coding, adding USB/MSD support: add include
(USBHostMSD.h),
… defines (enable line ‘…pf usb_osda …’ by remove
//comment and so the printf() will send data into PC
via OpenSDA/mUSB -> PC terminal)
… modify SLCD message to “MSd ” …
//---- MAIN/Inits (Wait4SW1) -> Start! --------------------//
//wait for Press SW1 - e.g. for HID/CDC/MSD Windows install.
SLCD_blinking_msg_wait(" o","MSd ");//Lab1=Hid;2=cdc;3=Msd
#include "USBHostMSD.h" //Lab3-MSd
Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal
#define pf usb_osda //printf out -> osda (lab1,2,3)
int main(void)
{
FILE* fp;
//---- MAIN/Inits Labs ------------------------------------//
pf.printf("rn Lab3: pls plug USB-stick into mUSB/KL46Z rn");
slcd.printf("USb~"); //Lab1=Hid;2=cdc;3=Msd
USBHostMSD msd("usb"); //wait for plugged USB-stick
if (!msd.connect()) {
error(" USB Flash drive not found.rn");
}
//Attempt to crete file /usb/usb_lab3.txt @USB-stick.
fp = fopen("/usb/usb_lab3.txt", "w"); //rewrite, or create
if (fp) {
pf.printf(" ... sucess file-open (/usb/usb_lab3.txt @USB-
stick)!rnrn");
fprintf(fp, " Lab3: from FRDM-KL46Z rnrn");
fclose(fp); fp=NULL;
} else pf.printf(" ... failed file-open (/usb/usb_lab3.txt @USB-
stick)!rnrn");
if (sD.slider)
slcd.printf(" %3.0f", sD.slider); //night->light .. 1->0
else
slcd.printf("%1.3f", sD.light); //left->right .. 0->100%
//sensor -> terminal
if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking
pf.printf(" rn");
pf.printf(" Switches. Light . Slider . Accelerometer .
Magnetometer . Compassrn");
pf.printf(" SW1:SW2| LUX | TSI | accX : accY : accZ | magX:
magY: maxZ | Headingrn");
pf.printf(" %d : %d | %1.3f | %2.0f %% | %+1.3f:%+1.3f:%+1.3f|
%5d:%5d:%5d | Lab3 rn",
sD.sw1State, sD.sw3State, sD.light, sD.slider, sD.accValX,
sD.accValY, sD.accValZ,
(short)sD.magValX, (short)sD.magValY, (short)sD.magValZ);
} gLED = LED_OFF;
#include "USBSerial.h" //Lab2-cdc
USBSerial usb_kl46; //KL46 Terminal
#define pf usb_kl46 //printf out -> kl46 (lab2 cdc)
//---- MAIN/Inits Labs ------------------------------------//
usb_kl46.printf("n___________________________________rn");
usb_kl46.printf("nFRDM-KL46Z_Labrn rn I am a "Virtual" CDC
serial port @KL46Z/mUSB. Baud=NA rn");
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 30 of 33 [Lab1/2/3=8/17/26]
… in the main loop, processing part, use dual-print
out (to terminal and also to usb/msd at the same
time), add also acc value shown at SLCD …
Save -> @Compile -> Save As and download
KL46Z-Lab3_KL46Z.bin into FRDM -> Press reset,
and follow instructions at PC-terminal …
Once the USB-drive is plugged during start process
pls do not detach/attach after as this example is not
implemented for dynamical device mapping.
Press and hold SW1 to write down sensor data onto
USB-drive.// MAIN/Loop/Processing and Actions -------------------//
//sensor -> terminal
if (fp) { gLED = !gLED; //blinkig
pf.printf("% 1.2frn", sD.accValZ); //->terminal
fprintf(fp,"% 1.2frn", sD.accValZ); //->usb_file
} else gLED = LED_OFF;
if(!fp && sw1==PRESS_ON) fp=fopen("/usb/usb_lab3.txt", "a");
else
if(fp && sw1==PRESS_OFF) {fclose(fp); fp=NULL; gLED=LED_OFF;}
rLED = abs(sD.accValZ);
slcd.CharPosition=0; //prevent slcd rolling
slcd.printf("% 3.0f", sD.accValZ);
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 31 of 33 [Lab1/2/3=8/17/26]
3.4 coding eCompass:
… modify in Defines section by add ‘magHeading’
into sensorData structure and put ‘prototype’ of the
MagCalibrationXY() and also put ‘mag_calib’
variable required for calibration init …
… put the calibration function into function section …
//Lab3add
int MagCalibrationXY(void)
{
int newX, tempXmax, tempXmin;
int newY, tempYmax, tempYmin;
int newZ, tempZmax, tempZmin;
int delta_avg=0, delta_avg_min=0, delta_avg_max=0, delta_calib_limit=800;
// Read initial values of magnetomoter
//read it here to create a slight delay for calPin to settle!!!
tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB);
tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB);
tempZmax = tempZmin = mag.readVal(MAG_OUT_Z_MSB);
// Update min and max values until calPin asserted again
while(sw1) {//wait for Press == manual calib stop
newX = mag.readVal(MAG_OUT_X_MSB);
newY = mag.readVal(MAG_OUT_Y_MSB);
newZ = mag.readVal(MAG_OUT_Z_MSB);
if (newX > tempXmax) tempXmax = newX;
if (newX < tempXmin) tempXmin = newX; newX = tempXmax - tempXmin;
if (newY > tempYmax) tempYmax = newY;
if (newY < tempYmin) tempYmin = newY; newY = tempYmax - tempYmin;
if (newZ > tempZmax) tempZmax = newZ;
if (newZ < tempZmin) tempZmin = newZ; newZ = tempZmax - tempZmin;
delta_avg = (newX + newY + newZ);
//delta is too high? -> error, try new calib!!!
if (delta_avg > 5 * delta_calib_limit) {delta_avg = 0; break; }
//div3 with error +0.8 pct
delta_avg = (21*(newX + newY + newZ))>>6;
//calib ok for delat ~800-1200 (~80-120uT)
if (delta_avg > delta_calib_limit){
delta_avg_min = delta_avg - (delta_avg>>2);
delta_avg_max = delta_avg + (delta_avg>>1);
if (delta_avg_min < newX && delta_avg_max > newX
&& delta_avg_min < newY && delta_avg_max > newY
&& delta_avg_min < newZ && delta_avg_max > newZ) break;
}
//show calib progress 0->100%
slcd.printf("C%3.0f", (float)delta_avg/delta_calib_limit*100);
}
if (sw1 == PRESS_ON || delta_avg ) {
mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax );
if (sw1 == PRESS_ON) return -1; //==-1 ..user skip
return delta_avg; //>0 .. done, ok calib
}
return 0; //==0 .. error, no calib
}
struct KL46_SENSOR_DATA {
int sw1State;
int sw3State;
float accValX;
float accValY;
float accValZ;
float slider;
float light;
int magValX;
int magValY;
int magValZ;
float magHeading;
} sensorData;
#define sD sensorData
void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2);
int MagCalibrationXY(void); //mag calib
//_____________________________________________________________//
//======== MAIN() =============================================//
//¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯//
int main(void)
{
int mag_calib = 0;
FILE* fp;
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 32 of 33 [Lab1/2/3=8/17/26]
Note: if you facing lot of “erro” messages, pls adjust this sw line: if
(delta_avg > 5 * delta_calib_limit)
Note: if you facing long/short calibration pls change delta_calib_limit=800;
uto lower/higher value.
… and insert calibration process into main() into
calib. section just after initialization Inits Lab section …
… add code which will calculate and print heading
and modify SLCD output value from accValZ to
magHeading .
Save -> @Compile -> Save As and download
KL46Z-Lab3_KL46Z.bin into FRDM -> Press reset,
and follow instruction from your embedded code …
(Calibration can be skipped also by click SW1)
sD.magHeading = mag.getHeading(); //calculate heading
// MAIN/Loop/Processing and Actions -------------------//
//sensor -> terminal
if (fp) { gLED = !gLED; //blinking
pf.printf("%.2ft", sD.magHeading); //->terminal
fprintf(fp,"%.2ft ", sD.magHeading); //->usb_file
pf.printf("% 1.2frn", sD.accValZ); //->terminal
fprintf(fp,"% 1.2frn", sD.accValZ); //->usb_file
} else gLED = LED_OFF;
if(!fp && sw1==PRESS_ON) fp=fopen("/usb/usb_lab3.txt", "a");
else
if(fp && sw1==PRESS_OFF) {fclose(fp); fp=NULL; gLED=LED_OFF;}
slcd.CharPosition=0; //prevent slcd rolling
slcd.printf("% 3.0f", sD.magHeading);
//slcd.printf("% 3.0f", sD.accValZ);
//---- MAIN/Inits Labs ------------------------------------//
//usb connection
//try file open
//---- MAIN/Inits (Wait4SW1) -> Calib. eCompass -----------//
while (!mag_calib) {
pf.printf(" Press and release SW1 to calibrate eCompass.rn");
SLCD_blinking_msg_wait(" o","CAL ");
// Calibrate Magnetometer to eCompass
pf.printf(" ... r o t a t e the FRDM board in 3d/360° until
[donE].rn");
mag_calib = MagCalibrationXY(); //to wait untill calib or
cancel!!!
if (mag_calib) {
if (mag_calib == -1)
{
pf.printf(" [SKiP]! Calibration skipped! ... See
Accelerometer Z-axis.rn");
SLCD_blinking_msg_wait(" o","SKiP"); //->acc
} else {
pf.printf(" [donE]! Calibration completed! ... Press
and release SW1 to try eCompass.rn");
SLCD_blinking_msg_wait(" o","donE"); //->mag
}
break; //while (!mag_calib)
} else {
slcd.printf("erro");
pf.printf("rn Error Calib !!! try again !!!rnrn");
} //repet calib
}
Kinetis L (FRDM-KL46Z) Hands-on Manual
Lab3: USB/MSD-Drive & eCompass
2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 33 of 33 [Lab1/2/3=8/17/26]
3.5 Homework …
1/ Try customize used USB host stack for
dynamically mapped USB-drive. Try to show USB
status info at SLCD display (capacity, writing
progress etc)
2/ Try create USB bootloader. For example when
embedded recognize attached USB-drive and
contains predefined file and format re-program mcu.
3/ Try use stored acc or mag data and use it for
post-processing and based on these find algorithms
for e.g. freefall detection, antitheft systems,
gestures etc.
4/ Try compensate eCompass using Roll and Pitch of
by using accelerometer
5/ Try create Virtual gyroscope (acc+mag) as the
Yaw is not detectable by acc but mag. Example
application could be e.g. ‘Dead reckoning’ for
walk/trail/track movements without absolute GPS.
6/ Try to compensate magnetic declination in your
region (difference between magnetic vs.
geographical North Pole)
3.6 Q&A … Thanks!!!
/End of Lab3

More Related Content

What's hot

IDI 1300 Cut Sheet
IDI 1300 Cut SheetIDI 1300 Cut Sheet
IDI 1300 Cut Sheetguestd76ad1
 
Micrcontroller iv sem lab manual
Micrcontroller iv sem lab manualMicrcontroller iv sem lab manual
Micrcontroller iv sem lab manualRohiniHM2
 
Arduino and its hw architecture
Arduino and its hw architectureArduino and its hw architecture
Arduino and its hw architectureZeeshan Rafiq
 
Introduction to Arduino Microcontroller
Introduction to Arduino MicrocontrollerIntroduction to Arduino Microcontroller
Introduction to Arduino MicrocontrollerMujahid Hussain
 
Basics of open source embedded development board (
Basics of open source embedded development board (Basics of open source embedded development board (
Basics of open source embedded development board (Dhruwank Vankawala
 
LinuxCNC 入門簡介
LinuxCNC 入門簡介LinuxCNC 入門簡介
LinuxCNC 入門簡介roboard
 
Microcontroller from basic_to_advanced
Microcontroller from basic_to_advancedMicrocontroller from basic_to_advanced
Microcontroller from basic_to_advancedImran Sheikh
 
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery BoardProgramming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery BoardGaurav Verma
 
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERSPIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERSVISHNU KP
 
PIC Introduction and explained in detailed
PIC Introduction and explained in detailedPIC Introduction and explained in detailed
PIC Introduction and explained in detailedAnkita Tiwari
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller Gaurav Verma
 
FSEC 2014 - I can haz your board with JTAG
FSEC 2014 - I can haz your board with JTAGFSEC 2014 - I can haz your board with JTAG
FSEC 2014 - I can haz your board with JTAGDobrica Pavlinušić
 
Introduction to Vortex86DX2 Motion-Control Evaluation Board
Introduction to Vortex86DX2 Motion-Control Evaluation BoardIntroduction to Vortex86DX2 Motion-Control Evaluation Board
Introduction to Vortex86DX2 Motion-Control Evaluation Boardroboard
 
LinuxCNC for Fun & Profit
LinuxCNC for Fun & ProfitLinuxCNC for Fun & Profit
LinuxCNC for Fun & ProfitAlastairDSilva
 
Pic 16f877 a
Pic 16f877 aPic 16f877 a
Pic 16f877 abmsjh
 
Interrupts at AVR
Interrupts at AVRInterrupts at AVR
Interrupts at AVRHamdy Fouad
 
Let's Play STM32
Let's Play STM32Let's Play STM32
Let's Play STM32Jay Chen
 

What's hot (20)

IDI 1300 Cut Sheet
IDI 1300 Cut SheetIDI 1300 Cut Sheet
IDI 1300 Cut Sheet
 
Micrcontroller iv sem lab manual
Micrcontroller iv sem lab manualMicrcontroller iv sem lab manual
Micrcontroller iv sem lab manual
 
Arduino and its hw architecture
Arduino and its hw architectureArduino and its hw architecture
Arduino and its hw architecture
 
Introduction to Arduino Microcontroller
Introduction to Arduino MicrocontrollerIntroduction to Arduino Microcontroller
Introduction to Arduino Microcontroller
 
Basics of open source embedded development board (
Basics of open source embedded development board (Basics of open source embedded development board (
Basics of open source embedded development board (
 
LinuxCNC 入門簡介
LinuxCNC 入門簡介LinuxCNC 入門簡介
LinuxCNC 入門簡介
 
Microcontroller from basic_to_advanced
Microcontroller from basic_to_advancedMicrocontroller from basic_to_advanced
Microcontroller from basic_to_advanced
 
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery BoardProgramming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
Programming the ARM CORTEX M3 based STM32F100RBT6 Value Line Discovery Board
 
STM32 MCU Family
STM32 MCU FamilySTM32 MCU Family
STM32 MCU Family
 
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERSPIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
PIC-MICROCONTROLLER TUTORIALS FOR BEGINNERS
 
PIC Introduction and explained in detailed
PIC Introduction and explained in detailedPIC Introduction and explained in detailed
PIC Introduction and explained in detailed
 
PIC and LCD
PIC and LCDPIC and LCD
PIC and LCD
 
8051 microcontroller
8051 microcontroller 8051 microcontroller
8051 microcontroller
 
FSEC 2014 - I can haz your board with JTAG
FSEC 2014 - I can haz your board with JTAGFSEC 2014 - I can haz your board with JTAG
FSEC 2014 - I can haz your board with JTAG
 
Introduction to Vortex86DX2 Motion-Control Evaluation Board
Introduction to Vortex86DX2 Motion-Control Evaluation BoardIntroduction to Vortex86DX2 Motion-Control Evaluation Board
Introduction to Vortex86DX2 Motion-Control Evaluation Board
 
LinuxCNC for Fun & Profit
LinuxCNC for Fun & ProfitLinuxCNC for Fun & Profit
LinuxCNC for Fun & Profit
 
Pic 16f877 a
Pic 16f877 aPic 16f877 a
Pic 16f877 a
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
Interrupts at AVR
Interrupts at AVRInterrupts at AVR
Interrupts at AVR
 
Let's Play STM32
Let's Play STM32Let's Play STM32
Let's Play STM32
 

Similar to FRDM-KL46Z_Hands-On_Lab0123_v02

MicroPython for LEGO Spike - introduction
MicroPython for LEGO Spike - introductionMicroPython for LEGO Spike - introduction
MicroPython for LEGO Spike - introductionsdoro58
 
Share the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardShare the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardJian-Hong Pan
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunpcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunJingfeng Liu
 
Chapter+1 +the+adventure+begins
Chapter+1 +the+adventure+beginsChapter+1 +the+adventure+begins
Chapter+1 +the+adventure+beginsnoor020202
 
Hardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopHardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopSlawomir Jasek
 
Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Codemotion
 
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...Toradex
 
Trying and evaluating the new features of GlusterFS 3.5
Trying and evaluating the new features of GlusterFS 3.5Trying and evaluating the new features of GlusterFS 3.5
Trying and evaluating the new features of GlusterFS 3.5Keisuke Takahashi
 
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfAdvanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfWiseNaeem
 
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfAdvanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfWiseNaeem
 
Introduction to FreeRTOS
Introduction to FreeRTOSIntroduction to FreeRTOS
Introduction to FreeRTOSICS
 
Testing CAN network with help of CANToolz
Testing CAN network with help of CANToolzTesting CAN network with help of CANToolz
Testing CAN network with help of CANToolzAlexey Sintsov
 
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfAdvanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfWiseNaeem
 
NodeMCU with Blynk and Firebase
NodeMCU with Blynk and FirebaseNodeMCU with Blynk and Firebase
NodeMCU with Blynk and FirebaseEueung Mulyana
 

Similar to FRDM-KL46Z_Hands-On_Lab0123_v02 (20)

MicroPython for LEGO Spike - introduction
MicroPython for LEGO Spike - introductionMicroPython for LEGO Spike - introduction
MicroPython for LEGO Spike - introduction
 
Share the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development BoardShare the Experience of Using Embedded Development Board
Share the Experience of Using Embedded Development Board
 
pcDuino Presentation at SparkFun
pcDuino Presentation at SparkFunpcDuino Presentation at SparkFun
pcDuino Presentation at SparkFun
 
HPC_MPI_CICD.pptx
HPC_MPI_CICD.pptxHPC_MPI_CICD.pptx
HPC_MPI_CICD.pptx
 
Chapter+1 +the+adventure+begins
Chapter+1 +the+adventure+beginsChapter+1 +the+adventure+begins
Chapter+1 +the+adventure+begins
 
Hardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshopHardwear.io 2018 BLE Security Essentials workshop
Hardwear.io 2018 BLE Security Essentials workshop
 
Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!Lab Handson: Power your Creations with Intel Edison!
Lab Handson: Power your Creations with Intel Edison!
 
STM -32
STM -32STM -32
STM -32
 
learning STM -32
learning STM -32 learning STM -32
learning STM -32
 
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
First Steps Developing Embedded Applications using Heterogeneous Multi-core P...
 
Trying and evaluating the new features of GlusterFS 3.5
Trying and evaluating the new features of GlusterFS 3.5Trying and evaluating the new features of GlusterFS 3.5
Trying and evaluating the new features of GlusterFS 3.5
 
Touch your NetBSD
Touch your NetBSDTouch your NetBSD
Touch your NetBSD
 
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfAdvanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
 
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfAdvanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
 
Backtrack Manual Part4
Backtrack Manual Part4Backtrack Manual Part4
Backtrack Manual Part4
 
Introduction to FreeRTOS
Introduction to FreeRTOSIntroduction to FreeRTOS
Introduction to FreeRTOS
 
Testing CAN network with help of CANToolz
Testing CAN network with help of CANToolzTesting CAN network with help of CANToolz
Testing CAN network with help of CANToolz
 
ucOS
ucOSucOS
ucOS
 
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdfAdvanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
Advanced View of Atmega Microcontroller Projects List - ATMega32 AVR.pdf
 
NodeMCU with Blynk and Firebase
NodeMCU with Blynk and FirebaseNodeMCU with Blynk and Firebase
NodeMCU with Blynk and Firebase
 

FRDM-KL46Z_Hands-On_Lab0123_v02

  • 1. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 1 of 33 [Lab1/2/3=8/17/26] Kinetis L Hands-on Manual With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy) Libor GECNUK Lab0 – mbed.org (v0.0) Lab1 – USB/HID-Mouse (v0.1) Lab2 – Sensors & USB/CDC-Serial (v0.1) Lab3 – USB/MSD-Drive & eCompass (v0.0)
  • 2. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 2 of 33 [Lab1/2/3=8/17/26] Introduction This hands-on investigates on generic functionality on the FRDM-KL46Z board using on board sensor set (acc- accelerometer, mag-magnetometer, tsi-touch sensing interface, light-sensor) used with couple USB Classes. Lab1&2 presents USB-device functionality (HID, CDC) and Lab3 USB-Host functionality. Coding and compiling is done in mbed.org. Labs shows mbed setup and initialization of FRDM and MCU programming (not debugging, if it is needed mbed allows export project into IDE’s e.g. Keil). Lab1 description This Lab uses FRDM-KL64Z board, coding is in mbed.org. Lab0 shows the way how to enable mbed @OpenSDA, mbed.org login and compiler usage, selecting right platform. Then we create first Hello World “blinky” program in 60seconds using Import and then Export to save it. Lab0 Teaser 1/ mbed @OpenSDA 2/ mbed start/login 3/ platform select 4/ first blinky program using Import  a/ blinky Green LED 5/ export program 6/ Q&A
  • 3. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 3 of 33 [Lab1/2/3=8/17/26] 0.1 mbed @OpenSDA: http://mbed.org/handbook/Firmware-FRDM-KL46Z Press&Hold Reset@FRDM … plug mUSB cable into OpenSDA debug interface and hold Reset till green LED starts blinking (~1s). Then Drag&Drop (or SendTo) file mbed_if_v2.1_frdm_kl46z.s19 onto disk BOOTLOADER and wait untill OpenSDA firmware is updated (so then FRDM will be ready for Drag&Drop proramming). Unplug and plug USB to finalize. 0.2 mbed start: Go to https://mbed.org/ and Login or signup. Put your Username and your Password and Login. When you are logged in then click on Compiler.
  • 4. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 4 of 33 [Lab1/2/3=8/17/26] So you can see mbed Workspace Managem 0.3 mbed Select Platform: at No device selected at FRDM KL46Z or Add a device (for the first time user) at Select Platoform (for the first time user)
  • 5. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 5 of 33 [Lab1/2/3=8/17/26] (for the first time) in Platform window select FRDM- KL46Z and (click at Freescale filter on right side of this web page to see just Freescale platforms) (for the first time) in Platform window “FRDM-KL46Z selected” at Add to your mbed Compiler Go back to the Compiler window and check that you have selected right platform FRDM-KL46Z /done! (Platform selection, from this point will be created all handson)
  • 6. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 6 of 33 [Lab1/2/3=8/17/26] 0.4 first mbed program Blinky Click on Compiler Click on New Enter ”60s” and use Template “gpio example …” and click OK Click Compile! Save file 60s_KL64Z.bin onto the MBED drive Press the reset button and the RED LED blinks
  • 7. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab0: mbed.org 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 7 of 33 [Lab1/2/3=8/17/26] 0.5 mbed Export Program: @KL46Z-Lab0 and select “Export Program…” 0.6 Q&A … Thanks!!! /End of Lab0
  • 8. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 8 of 33 [Lab1/2/3=8/17/26] Kinetis L Hands-on Manual With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy) Libor GECNUK Lab0 – mbed.org (v0.0) Lab1 – USB/HID-Mouse (v0.1) Lab2 – Sensors & USB/CDC-Serial (v0.1) Lab3 – USB/MSD-Drive & eCompass (v0.0)
  • 9. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 9 of 33 [Lab1/2/3=8/17/26] Introduction This hands-on investigates on generic functionality on the FRDM-KL46Z board using on board sensor set (acc- accelerometer, mag-magnetometer, tsi-touch sensing interface, light-sensor) used with couple USB Classes. Lab1&2 presents USB-device functionality (HID, CDC) and Lab3 USB-Host functionality. Coding and compiling is done in mbed.org. Labs shows mbed setup and initialization of FRDM and MCU programming (not debugging, if it is needed mbed allows export project into IDE’s e.g. Keil). Lab1 description This Lab uses FRDM-KL64Z board, coding is in mbed.org. Lab1 shows a/ accelerometer as source of X&Y coordination for the USB HID (Mouse) application while Red LED shows Z-axis and b/ usage of switch SW1, SW3 utilized as Left&Right click. Lab1 Teaser 1/ new program 2/ import Libs (mbed+acc) 3/ coding: from mouse example 4/ compiling, flashing mcu  a/ mouse 5/ coding: buttons  b/ mouse & buttons 6/ export program 7/ homework 8/ Q&A
  • 10. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 10 of 33 [Lab1/2/3=8/17/26] 1.1 mbed New Program: In the mbed Program Workspace, and go for [My Programs | New Program… ]. Use Template ‘Empty Program’. Into Program Name type ‘KL46Z-Lab1’ @OK at KL46Z-Lab1 and at New File Type main_lab1.cpp and @OK /done! (Project name, main.cpp)
  • 11. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 11 of 33 [Lab1/2/3=8/17/26] 1.2 mbed import Library (mbed): In the mbed Program Workspace, @Import And select mbed and (or drag&drop) to add this Library into actual project (KL46Z-Lab#) … so you will have this view … ..then .. Import Library (USB Device): In the mbed Program Workspace, @Import and type ‘USB’ @Search . Select first one ‘USB Device’ (from author ‘mbed official’) and to add it into actual project. ..then.. Import Library (MMA8451Q): In the mbed Program Workspace, @Import and type
  • 12. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 12 of 33 [Lab1/2/3=8/17/26] ‘MMA8451Q’ and Search. . Select first (from author ‘Johan Kritzinger’) and . /done! (mbed Libraries imports) 1.3 Writing code: Goto USBHIDUSBMouse.h and find USBMouse example … … and copy&paste this example into main.cpp …
  • 13. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 13 of 33 [Lab1/2/3=8/17/26] ... then goto MMA8451QMMA8451Q.h and copy example from there and paste it into main.cpp … … then before main()-loop add code: #defines and #definitions required for the program … .. then in main()-loop add code and modify code .. .. then review your code and check if is ok. /done! (added DigitalOut gLED, PwmOut rLED, Structure sensorData, linked accXY sensor data as mouse XY coordination, changed main loop period to 50ms for suitable mouse response) int main(void) { gLED = LED_ON; //Green LED ON to indicate running program rLED = LED_OFF; //Red LED OFF rLED.period(rLEDperiod); //Red LED (rLED) blinkig shows accZ while (1) { sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1) sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1) sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED //accXY -> mouse movements from FRDM XY inclination mouse.move(sD.accValX*16, sD.accValY*16); //acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking rLED = abs(sD.accValZ); wait(0.05); //wait 50ms } } #include "mbed.h" #include "MMA8451Q.h" #include "USBMouse.h" //Lab1-Hid #define LED_ON 0 //outON, pwmON #define LED_OFF 1 //outOFF,pwmOFF DigitalOut gLED(LED_GREEN); //PTD5 #define rLEDperiod 150 //[ms] PwmOut rLED(LED_RED); //PTE29 #define MMA8451_I2C_ADDRESS (0x1d<<1) MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); USBMouse mouse; struct KL46_SENSOR_DATA { float accValX; float accValY; float accValZ; } sensorData; #define sD sensorData
  • 14. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 14 of 33 [Lab1/2/3=8/17/26] 1.4 mbed Compile & Program FRDM board: In the mbed Program Workspace, @Compile (or CTRL-D; note: CTRL-B is just build) .. .. (be sure that Cable/mUSB is at OpenSDA/mUSB.) ... and @SaveAs and store *.bin into (when FRDM board is USB plugged to PC) … .. wait for stop blinking of OpenSDA-green-LED to finish! /done! (FRDM board programming, flashing target mcu) #include "mbed.h" #include "MMA8451Q.h" #include "USBMouse.h" #define LED_ON 0 //outON, pwmON #define LED_OFF 1 //outOFF,pwmOFF DigitalOut gLED(LED_GREEN); //PTD5 #define rLEDperiod 150 //[ms] PwmOut rLED(LED_RED); //PTE29 #define MMA8451_I2C_ADDRESS (0x1d<<1) MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); USBMouse mouse; struct KL46_SENSOR_DATA { float accValX; float accValY; float accValZ; } sensorData; #define sD sensorData int main(void) { gLED = LED_ON; //Green LED ON to indicate running program rLED = LED_OFF; //Red LED OFF rLED.period(rLEDperiod); //Red LED (rLED) blinkig shows accZ while (1) { sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1) sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1) sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED //accXY -> mouse movements from FRDM XY inclination mouse.move(sD.accValX*16, sD.accValY*16); //acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking rLED = abs(sD.accValZ); wait(0.05); //wait 50ms } }
  • 15. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 15 of 33 [Lab1/2/3=8/17/26] 1.5 Mouse example (emulate PC mouse by FRDM using accelerometer): swap mUSB-Cable from OpenSDA/mUSB to KL46/mUSB connector and wait for Windows messages (Installing device driver software -> HID or USB input device installation -> Your device is ready to use). Note: no drivers are needed as this USB HID class is already natively in Windows. .. /done! (FRDM as PC mouse XY pointer, no touch yet) Add mouse buttons: In the mbed add defines into main_lab1.cpp. .. then add cooperative code for mouse-click into main()-loop .. #include "mbed.h" #include "USBMouse.h" #include "MMA8451Q.h" #define LED_ON 0 //outON, pwmON #define LED_OFF 1 //outOFF,pwmOFF DigitalOut gLED(LED_GREEN); //PTD5 #define PRESS_ON 0 #define PRESS_OFF 1 DigitalIn sw1(PTC3); //if(sw1) Release else Press DigitalIn sw3(PTC12); //while(sw3); wait for Press #define rLEDperiod 150 //[ms] PwmOut rLED(LED_RED); //PTE29 #define MMA8451_I2C_ADDRESS (0x1d<<1) MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); USBMouse mouse; struct KL46_SENSOR_DATA { int sw1State; int sw3State; float accValX; float accValY; float accValZ; } sensorData; #define sD sensorData
  • 16. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab1: USB/HID-Mouse 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 16 of 33 [Lab1/2/3=8/17/26] .. Compile & Program FRDM board using OpenSDA/mUSB connector and try mouse example with click left & right buttons using KL46/mUSB connector. 1.6 Mbed Export Program: @KL46Z-Lab# and select “Export Program…” .. and select “mbed Online IDE” -> @ Export. SaveAs and use e.g. predefined name “KL46Z- Lab1_mouse.zip” and store it to local disc. 1.7 Homework … 1/ Try composite usb class “keyboard + mouse” (e.g. open window) 2/ Try implementing mouse with dynamic speed 3/ Try detect light. Use it for enabling dynamically mouse functionality only when hand is above board. 4/ Try to use eCompass for X-axis coordination 1.8 Q&A … Thanks!!! /End of Lab1 int main(void) { sw1.mode(PullUp); sw3.mode(PullUp); gLED = LED_ON; //Green LED ON to indicate running program rLED = LED_OFF; //Red LED OFF rLED.period(rLEDperiod); //Red LED (rLED) blinkig shows accZ while (1) { sD.sw1State = sw1; sD.sw3State = sw3; sensorData.accValX = acc.getAccX(); //accX[-1..1]->mouse sensorData.accValY = acc.getAccY(); //accY[-1..1]->mouse sensorData.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED if(sD.sw1State==PRESS_ON) //mouse left button mouse.press(MOUSE_LEFT); else mouse.release(MOUSE_LEFT); if(sD.sw3State==PRESS_ON) //mouse right button mouse.press(MOUSE_RIGHT); else mouse.release(MOUSE_RIGHT); //accXY -> mouse movements from FRDM XY inclination mouse.move(sensorData.accValX*16, sensorData.accValY*16); //accZ -> red blinking duty-cycle (maxZ -> maxDutyCycle) rLED = 1 - abs(sensorData.accValZ); wait(0.05); //wait 50ms } }
  • 17. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 17 of 33 [Lab1/2/3=8/17/26] Kinetis L Hands-on Manual With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy) Libor GECNUK Lab0 – mbed.org (v0.0) Lab1 – USB/HID-Mouse (v0.1) Lab2 – Sensors & USB/CDC-Serial (v0.1) Lab3 – USB/MSD-Drive & eCompass (v0.0)
  • 18. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 18 of 33 [Lab1/2/3=8/17/26] Introduction This hands-on investigates on generic functionality on the FRDM-KL46Z board using on board sensor set (acc- accelerometer, mag-magnetometer, tsi-touch sensing interface, light-sensor) used with couple USB Classes. Lab1&2 presents USB-device functionality (HID, CDC) and Lab3 USB-Host functionality. Coding and compiling is done in mbed.org. Labs shows mbed setup and initialization of FRDM and MCU programming (not debugging, if it is needed mbed allows export project into IDE’s e.g. Keil). Lab2 description This Lab uses FRDM-KL64Z board, coding is in mbed.org. Lab2 shows a/ print out capability using PC terminal using OpenSDA composite port, b/ sensing capability and using same port and c/ print out at sensing capability using Virtual Com port at KL46Z. Lab2 Teaser 1/ copy program | import 2/ remove unwanted code from Lab1 3/ import mag+tsi+slcd and add serial i/o for PC-terminal 4/ coding: all sensor support, add wait/slcd(), serial, sensing, printout 5/ install mbed terminal drivers @ Windows …  a/ sensor data at PC-console via OpenSDA 6/ add USB-cdc, install mbed/serial.inf @ Windows  c/ sensor data at PC-console directly via KL46Z 7/ export program 8/ homework 9/ Q&A
  • 19. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 19 of 33 [Lab1/2/3=8/17/26] 2.1 mbed Import Program (from previous Lab1): @Import in mbed Workspace -> @Upload -> @Browse… and find “KL46Z-Lab1export.zip”. Select this file and @Import in Import Wizard. .. and change the Import Name to “KL46Z-Lab2” @Import button. .. then rename main_lab1.cpp -> main_lab2.cpp (Note: its previous example but with added formatting for better orientation in next examples) You can click at ‘The document is out of date’ to have Classes help in the Program Workspace (Note that Classes will be grouped in workspace)
  • 20. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 20 of 33 [Lab1/2/3=8/17/26] 2.2 remove unwanted code from Lab1 2.3 mbed Import Library (MAG3110): In the mbed Program Workspace, @Import and type ‘mag’ @Search . Select first one ‘MAG3110’ (from author ‘Andrew Lindsay’ http://mbed.org/users/SomeRandomBloke/code/MA G3110/ ) and to add it into actual project. Note: please make sure you make selection at project before adding this Lib into this project. In the same way … Import Library ‘TSI’ from ‘First Last’ and Import Library ‘SLCD-’ from ‘’Libor GECNUK’ (added support for sigh ‘-’ from original SLCD lib) … so you will have next view … #include "USBMouse.h" //Lab1-Hid USBMouse mouse; //Lab1-Hid if(sD.sw1State==PRESS_ON) //mouse left button mouse.press(MOUSE_LEFT); else mouse.release(MOUSE_LEFT); if(sD.sw3State==PRESS_ON) //mouse right button mouse.press(MOUSE_RIGHT); else mouse.release(MOUSE_RIGHT); //accXY -> mouse movements from FRDM XY inclination mouse.move(sensorData.accValX*16, sensorData.accValY*16);
  • 21. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 21 of 33 [Lab1/2/3=8/17/26] 2.4 coding all sensors support: add include, defines … …add function SLCD_blinking_msg_wait(): it will be used to wait for start program until pressed & released SW1 switch @FRDM and also to show @SLCD custom messages … … in main() add wait&slcd() and serial coop-code … //---- MAIN/Inits (Wait4SW1) -> Start! --------------------// //wait for Press SW1 - e.g. for HID/CDC/MSD Windows install. SLCD_blinking_msg_wait(" o","PrES");//Lab1=Hid;2=cdc;3=Msd //---- MAIN/Inits Interface -------------------------------// usb_osda.baud(115200); usb_osda.printf("n___________________________________rn"); usb_osda.printf("nFRDM-KL46Z_Labrn rn I am a CDC serial port @OpenSDA/mUSB. Baud=115200 rn"); //_____________________________________________________________// //======== FUNC() =============================================// //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// //Lab2add void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2) { char wait4sw1=0; //~500ms blinking //wait for Press SW1 - to start mag calibration while(sw1 == PRESS_ON); //wait for release while(sw1 == PRESS_OFF) { //wait for press if (++wait4sw1 < 150) //300ms slcd.printf(slcd_msg1); else //200ms slcd.printf(slcd_msg2); wait(0.002); } while(sw1 == PRESS_ON); //wait for release } //======== INCLUDES ===========================================// #include "MAG3110.h" #include "SLCD.h" #include "TSISensor.h" //======== DEFINES & VARIABLES ================================// SLCD slcd; //[88:88][8.8.8.8] SegmentLCD AnalogIn light(PTE22); //analog-light input TSISensor slider; //Capacitive Touch Slider MAG3110 mag(PTE25, PTE24); //Magnetometer Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal #define pf usb_osda //printf out -> osda (lab1,2,3) struct KL46_SENSOR_DATA { int sw1State; int sw3State; float accValX; float accValY; float accValZ; float slider; float light; int magValX; int magValY; int magValZ; } sensorData; #define sD sensorData void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2);
  • 22. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 22 of 33 [Lab1/2/3=8/17/26] … in main() also add program flow and sensing code for data acquisition … 2.5 install mbed Serial-PC-terminal support from hands-on materials mbedWinSerial_16466.exe (Note you can find latest version @mbed.org http://mbed.org/handbook/SerialPC and install it) Install PC-terminal (e.g. TeraTerm, putty) .. after installations … Open PC-terminal and configure communication for 115200/8/N/1 ... open port! /done! (Serial communication through OpenSDA) Save -> @Compile -> Save As and download KL46Z-Lab2_KL46Z.bin into FRDM (note: after download the program flow is waiting for your SW1- press & release to start send full sensing data into PC-terminal via OpenSDA/mUSB when SW1- pressed) // MAIN/Loop/Processing and Actions -------------------// //disable all SLCD DPs slcd.DP(0, false); slcd.DP(1, false); slcd.DP(2, false); // MAIN/Loop/Sensing and Storing data -----------------// sD.sw1State = sw1; sD.sw3State = sw3; sD.accValX = acc.getAccX(); //accX[-1..1]->mouse (Lab1) sD.accValY = acc.getAccY(); //accY[-1..1]->mouse (Lab1) sD.accValZ = acc.getAccZ(); //accZ[-1..1]->rLED sD.slider = slider.readPercentage() * 100; sD.light = light; sD.magValX = mag.readVal(MAG_OUT_X_MSB); sD.magValY = mag.readVal(MAG_OUT_Y_MSB); sD.magValZ = mag.readVal(MAG_OUT_Z_MSB); // MAIN/Loop/Processing and Actions -------------------// //sensor -> terminal if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking pf.printf(" rn"); pf.printf(" Switches. Light . Slider . Accelerometer . Magnetometer . Compassrn"); pf.printf(" SW1:SW2| LUX | TSI | accX : accY : accZ | magX: magY: maxZ | Headingrn"); pf.printf(" %d : %d | %1.3f | %2.0f %% | %+1.3f:%+1.3f:%+1.3f| %5d:%5d:%5d | Lab3 rn", sD.sw1State, sD.sw3State, sD.light, sD.slider, sD.accValX, sD.accValY, sD.accValZ, (short)sD.magValX, (short)sD.magValY, (short)sD.magValZ); } gLED = LED_OFF; //acc: z-axis 1g min-blinking//acc: z-axis 1g min-blinking rLED = abs(sD.accValZ); if (sD.slider) slcd.printf(" %3.0f", sD.slider); //night->light .. 1->0 else slcd.printf("%1.3f", sD.light); //left->right .. 0->100%
  • 23. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 23 of 33 [Lab1/2/3=8/17/26] SLCD shows Light and when you touch slider it will show finger position at slider @FRDM. Red LED is still showing z-axis inclination from accelerometer. /done! (All sensors data) 2.6 add CDC-serial class support: includes, and //comment line ‘…pf usb_osda …’ (so the printf() will send data into PC not via OpenSDA/mUSB but through KL46Z/mUSB) and add USBSerial support … Save -> @Compile -> Save As and download KL46Z-Lab2_KL46Z.bin into FRDM. //======== INCLUDES ===========================================// #include "USBSerial.h" //Lab2-cdc //======== DEFINES & VARIABLES ================================// Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal //#define pf usb_osda //printf out -> osda (lab1,2,3) USBSerial usb_kl46; //KL46 Terminal #define pf usb_kl46 //printf out -> kl46 (lab2 cdc) //---- MAIN/Inits Labs ------------------------------------// usb_kl46.printf("n___________________________________rn"); usb_kl46.printf("nFRDM-KL46Z_Labrn rn I am a "Virtual" CDC serial port @KL46Z/mUSB. Baud=NA rn");
  • 24. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 24 of 33 [Lab1/2/3=8/17/26] … swap mUSB-Cable from OpenSDA/mUSB to KL46/mUSB (Note: first-time users, pls wait for proper installation of Virtual-CDC-Serial driver serial.inf into Windows) … .. … so then you can check it in device manager … or ( ) (Left: “one” cable in the KL46/mUSB) (Right: “two” cables, KL46/mUSB & mbed:OpenSDA/mUSB … is the set-up which could be used e.g. to see additional statuses about USB module, communications etc) .. in the PC-terminal (e.g. TeraTerm) refresh available Serial Ports and select “Mbed Virtual Serial Port” and open this port for communication … Click SW1 to see All sensor data @PC-terminal … Note: that baud rate is not important information for Virtual Serial Port (using CDC-Class) as it communicates always at max speed of USB.
  • 25. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab2: Sensors & USB/CDC-Serial 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 25 of 33 [Lab1/2/3=8/17/26] 2.7 Mbed Export Program: @KL46Z-Lab# and select “Export Program…” -> .. and select “mbed Online IDE” -> @ Export. SaveAs and use e.g. predefined name “KL46Z- Lab2_serial.zip” 2.8 Homework … 1/ Try sending date only when lower light indicate human hand proximity 2/ Try recognize by your embedded code that CDC- Serial is ready and after this send that info to terminal. 3/ Try scan commands from terminal (e.g. getc()) 4/ Try use composite usb class “keyboard + mouse” to open e.g notepad in Windows, put few sensor messages, save it, and close it. 5/ Try to use usb analyzer  2.9 Q&A … Thanks!!! /End of Lab2
  • 26. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 26 of 33 [Lab1/2/3=8/17/26] Kinetis L Hands-on Manual With FReeDoM Board FRDM-KL46Z – USB & Sensors (USB Academy) Libor GECNUK Lab0 – mbed.org (v0.0) Lab1 – USB/HID-Mouse (v0.1) Lab2 – Sensors & USB/Serial (v0.1) Lab3 – USB/MSD-Drive & eCompass(v0.0)
  • 27. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 27 of 33 [Lab1/2/3=8/17/26] Introduction This hands-on investigates on generic functionality on the FRDM-KL46Z board using on board sensor set (acc- accelerometer, mag-magnetometer, tsi-touch sensing interface, light-sensor) used with couple USB Classes. Lab1&2 presents USB-device functionality (HID, CDC) and Lab3 USB-Host functionality. Coding and compiling is done in mbed.org. Labs shows mbed setup and initialization of FRDM and MCU programming (not debugging, if it is needed mbed allows export project into IDE’s e.g. Keil). Lab3 description This Lab uses FRDM-KL64Z board, coding is in mbed.org. Lab3 shows a/ FRDM as USB host application with capability write and read USB-drive destination and b/ eCompass calibration and usage of FRDM and tracker. Lab3 Teaser 1/ new & copy (drag & drop) mbed program 2/ remove unwanted code from Lab2 3/ coding, adding USB/MSD  a/ writing acc Z-axis@USB-drive 4/ coding eCompass (need calibration)  a/ eCompass Heading @USB-drive 5/ homework 6/ Q&A
  • 28. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 28 of 33 [Lab1/2/3=8/17/26] 3.1 mbed New&Copy Program: In the mbed Program Workspace, and go for [My Programs | New Program… ]. Use Template ‘Empty Program’. Into Program Name type ‘KL46Z-Lab3’ @OK mbed Copy Program: @KL46Z-Lab2 and select all content (except USB Device) using CTRL+ and copy that using CTRL+C and CTRL+V @KL46Z- Lab3. (Note: also is possible use drag&drop and then select from context menu ‘Copy Here’) … and rename -> … … import “USB Host” Library ( serach ‘usb kl46’ USB host by ‘Norimasa Okamoto’) and to add it into actual project.
  • 29. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 29 of 33 [Lab1/2/3=8/17/26] 3.2 remove unwanted code from Lab2 3.3 coding, adding USB/MSD support: add include (USBHostMSD.h), … defines (enable line ‘…pf usb_osda …’ by remove //comment and so the printf() will send data into PC via OpenSDA/mUSB -> PC terminal) … modify SLCD message to “MSd ” … //---- MAIN/Inits (Wait4SW1) -> Start! --------------------// //wait for Press SW1 - e.g. for HID/CDC/MSD Windows install. SLCD_blinking_msg_wait(" o","MSd ");//Lab1=Hid;2=cdc;3=Msd #include "USBHostMSD.h" //Lab3-MSd Serial usb_osda(USBTX, USBRX); //OpenSDA Terminal #define pf usb_osda //printf out -> osda (lab1,2,3) int main(void) { FILE* fp; //---- MAIN/Inits Labs ------------------------------------// pf.printf("rn Lab3: pls plug USB-stick into mUSB/KL46Z rn"); slcd.printf("USb~"); //Lab1=Hid;2=cdc;3=Msd USBHostMSD msd("usb"); //wait for plugged USB-stick if (!msd.connect()) { error(" USB Flash drive not found.rn"); } //Attempt to crete file /usb/usb_lab3.txt @USB-stick. fp = fopen("/usb/usb_lab3.txt", "w"); //rewrite, or create if (fp) { pf.printf(" ... sucess file-open (/usb/usb_lab3.txt @USB- stick)!rnrn"); fprintf(fp, " Lab3: from FRDM-KL46Z rnrn"); fclose(fp); fp=NULL; } else pf.printf(" ... failed file-open (/usb/usb_lab3.txt @USB- stick)!rnrn"); if (sD.slider) slcd.printf(" %3.0f", sD.slider); //night->light .. 1->0 else slcd.printf("%1.3f", sD.light); //left->right .. 0->100% //sensor -> terminal if (sD.sw1State != PRESS_OFF) { gLED = !gLED; //blinking pf.printf(" rn"); pf.printf(" Switches. Light . Slider . Accelerometer . Magnetometer . Compassrn"); pf.printf(" SW1:SW2| LUX | TSI | accX : accY : accZ | magX: magY: maxZ | Headingrn"); pf.printf(" %d : %d | %1.3f | %2.0f %% | %+1.3f:%+1.3f:%+1.3f| %5d:%5d:%5d | Lab3 rn", sD.sw1State, sD.sw3State, sD.light, sD.slider, sD.accValX, sD.accValY, sD.accValZ, (short)sD.magValX, (short)sD.magValY, (short)sD.magValZ); } gLED = LED_OFF; #include "USBSerial.h" //Lab2-cdc USBSerial usb_kl46; //KL46 Terminal #define pf usb_kl46 //printf out -> kl46 (lab2 cdc) //---- MAIN/Inits Labs ------------------------------------// usb_kl46.printf("n___________________________________rn"); usb_kl46.printf("nFRDM-KL46Z_Labrn rn I am a "Virtual" CDC serial port @KL46Z/mUSB. Baud=NA rn");
  • 30. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 30 of 33 [Lab1/2/3=8/17/26] … in the main loop, processing part, use dual-print out (to terminal and also to usb/msd at the same time), add also acc value shown at SLCD … Save -> @Compile -> Save As and download KL46Z-Lab3_KL46Z.bin into FRDM -> Press reset, and follow instructions at PC-terminal … Once the USB-drive is plugged during start process pls do not detach/attach after as this example is not implemented for dynamical device mapping. Press and hold SW1 to write down sensor data onto USB-drive.// MAIN/Loop/Processing and Actions -------------------// //sensor -> terminal if (fp) { gLED = !gLED; //blinkig pf.printf("% 1.2frn", sD.accValZ); //->terminal fprintf(fp,"% 1.2frn", sD.accValZ); //->usb_file } else gLED = LED_OFF; if(!fp && sw1==PRESS_ON) fp=fopen("/usb/usb_lab3.txt", "a"); else if(fp && sw1==PRESS_OFF) {fclose(fp); fp=NULL; gLED=LED_OFF;} rLED = abs(sD.accValZ); slcd.CharPosition=0; //prevent slcd rolling slcd.printf("% 3.0f", sD.accValZ);
  • 31. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 31 of 33 [Lab1/2/3=8/17/26] 3.4 coding eCompass: … modify in Defines section by add ‘magHeading’ into sensorData structure and put ‘prototype’ of the MagCalibrationXY() and also put ‘mag_calib’ variable required for calibration init … … put the calibration function into function section … //Lab3add int MagCalibrationXY(void) { int newX, tempXmax, tempXmin; int newY, tempYmax, tempYmin; int newZ, tempZmax, tempZmin; int delta_avg=0, delta_avg_min=0, delta_avg_max=0, delta_calib_limit=800; // Read initial values of magnetomoter //read it here to create a slight delay for calPin to settle!!! tempXmax = tempXmin = mag.readVal(MAG_OUT_X_MSB); tempYmax = tempYmin = mag.readVal(MAG_OUT_Y_MSB); tempZmax = tempZmin = mag.readVal(MAG_OUT_Z_MSB); // Update min and max values until calPin asserted again while(sw1) {//wait for Press == manual calib stop newX = mag.readVal(MAG_OUT_X_MSB); newY = mag.readVal(MAG_OUT_Y_MSB); newZ = mag.readVal(MAG_OUT_Z_MSB); if (newX > tempXmax) tempXmax = newX; if (newX < tempXmin) tempXmin = newX; newX = tempXmax - tempXmin; if (newY > tempYmax) tempYmax = newY; if (newY < tempYmin) tempYmin = newY; newY = tempYmax - tempYmin; if (newZ > tempZmax) tempZmax = newZ; if (newZ < tempZmin) tempZmin = newZ; newZ = tempZmax - tempZmin; delta_avg = (newX + newY + newZ); //delta is too high? -> error, try new calib!!! if (delta_avg > 5 * delta_calib_limit) {delta_avg = 0; break; } //div3 with error +0.8 pct delta_avg = (21*(newX + newY + newZ))>>6; //calib ok for delat ~800-1200 (~80-120uT) if (delta_avg > delta_calib_limit){ delta_avg_min = delta_avg - (delta_avg>>2); delta_avg_max = delta_avg + (delta_avg>>1); if (delta_avg_min < newX && delta_avg_max > newX && delta_avg_min < newY && delta_avg_max > newY && delta_avg_min < newZ && delta_avg_max > newZ) break; } //show calib progress 0->100% slcd.printf("C%3.0f", (float)delta_avg/delta_calib_limit*100); } if (sw1 == PRESS_ON || delta_avg ) { mag.setCalibration( tempXmin, tempXmax, tempYmin, tempYmax ); if (sw1 == PRESS_ON) return -1; //==-1 ..user skip return delta_avg; //>0 .. done, ok calib } return 0; //==0 .. error, no calib } struct KL46_SENSOR_DATA { int sw1State; int sw3State; float accValX; float accValY; float accValZ; float slider; float light; int magValX; int magValY; int magValZ; float magHeading; } sensorData; #define sD sensorData void SLCD_blinking_msg_wait(char *slcd_msg1, char *slcd_msg2); int MagCalibrationXY(void); //mag calib //_____________________________________________________________// //======== MAIN() =============================================// //¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯// int main(void) { int mag_calib = 0; FILE* fp;
  • 32. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 32 of 33 [Lab1/2/3=8/17/26] Note: if you facing lot of “erro” messages, pls adjust this sw line: if (delta_avg > 5 * delta_calib_limit) Note: if you facing long/short calibration pls change delta_calib_limit=800; uto lower/higher value. … and insert calibration process into main() into calib. section just after initialization Inits Lab section … … add code which will calculate and print heading and modify SLCD output value from accValZ to magHeading . Save -> @Compile -> Save As and download KL46Z-Lab3_KL46Z.bin into FRDM -> Press reset, and follow instruction from your embedded code … (Calibration can be skipped also by click SW1) sD.magHeading = mag.getHeading(); //calculate heading // MAIN/Loop/Processing and Actions -------------------// //sensor -> terminal if (fp) { gLED = !gLED; //blinking pf.printf("%.2ft", sD.magHeading); //->terminal fprintf(fp,"%.2ft ", sD.magHeading); //->usb_file pf.printf("% 1.2frn", sD.accValZ); //->terminal fprintf(fp,"% 1.2frn", sD.accValZ); //->usb_file } else gLED = LED_OFF; if(!fp && sw1==PRESS_ON) fp=fopen("/usb/usb_lab3.txt", "a"); else if(fp && sw1==PRESS_OFF) {fclose(fp); fp=NULL; gLED=LED_OFF;} slcd.CharPosition=0; //prevent slcd rolling slcd.printf("% 3.0f", sD.magHeading); //slcd.printf("% 3.0f", sD.accValZ); //---- MAIN/Inits Labs ------------------------------------// //usb connection //try file open //---- MAIN/Inits (Wait4SW1) -> Calib. eCompass -----------// while (!mag_calib) { pf.printf(" Press and release SW1 to calibrate eCompass.rn"); SLCD_blinking_msg_wait(" o","CAL "); // Calibrate Magnetometer to eCompass pf.printf(" ... r o t a t e the FRDM board in 3d/360° until [donE].rn"); mag_calib = MagCalibrationXY(); //to wait untill calib or cancel!!! if (mag_calib) { if (mag_calib == -1) { pf.printf(" [SKiP]! Calibration skipped! ... See Accelerometer Z-axis.rn"); SLCD_blinking_msg_wait(" o","SKiP"); //->acc } else { pf.printf(" [donE]! Calibration completed! ... Press and release SW1 to try eCompass.rn"); SLCD_blinking_msg_wait(" o","donE"); //->mag } break; //while (!mag_calib) } else { slcd.printf("erro"); pf.printf("rn Error Calib !!! try again !!!rnrn"); } //repet calib }
  • 33. Kinetis L (FRDM-KL46Z) Hands-on Manual Lab3: USB/MSD-Drive & eCompass 2014/04 FRDM-KL46Z_Hands-On_Lab0123_v02.docx Page 33 of 33 [Lab1/2/3=8/17/26] 3.5 Homework … 1/ Try customize used USB host stack for dynamically mapped USB-drive. Try to show USB status info at SLCD display (capacity, writing progress etc) 2/ Try create USB bootloader. For example when embedded recognize attached USB-drive and contains predefined file and format re-program mcu. 3/ Try use stored acc or mag data and use it for post-processing and based on these find algorithms for e.g. freefall detection, antitheft systems, gestures etc. 4/ Try compensate eCompass using Roll and Pitch of by using accelerometer 5/ Try create Virtual gyroscope (acc+mag) as the Yaw is not detectable by acc but mag. Example application could be e.g. ‘Dead reckoning’ for walk/trail/track movements without absolute GPS. 6/ Try to compensate magnetic declination in your region (difference between magnetic vs. geographical North Pole) 3.6 Q&A … Thanks!!! /End of Lab3