ISSDK  1.7
IoT Sensing Software Development Kit
calibration_storage.c
Go to the documentation of this file.
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015 - 2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  * that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  * of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  * list of conditions and the following disclaimer in the documentation and/or
16  * other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  * contributors may be used to endorse or promote products derived from this
20  * software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 
36 /*! \file calibration_storage.c
37  \brief Provides functions to store calibration to NVM
38 
39  Users who are not using NXP hardware will need to supply their own drivers
40  in place of those defined here.
41 */
42 #include <stdio.h>
43 #include "sensor_fusion.h"
44 #include "driver_KSDK_NVM.h"
45 #include "calibration_storage.h"
46 
48 {
49 #if F_USING_MAG
50  uint8_t *pSrc, *pDst; // scratch pointers
51  int16_t i; // loop counter
52  uint8_t iNVMBuffer[256]; // NVM write buffer (smallest size writeable to flash)
53  uint32_t itmp32;
54 
55  // copy existing magnetic, gyro and accelerometer calibrations to buffer
56  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
57  pDst = iNVMBuffer;
58  for (i = 0; i < 256; i++)
59  *(pDst++) = *(pSrc++);
60  // default to no magnetic calibration in header
61  iNVMBuffer[MAG_NVM_OFFSET] = iNVMBuffer[MAG_NVM_OFFSET + 1] = iNVMBuffer[MAG_NVM_OFFSET + 2] = iNVMBuffer[MAG_NVM_OFFSET + 3] = 0xFF;
62 
63  // fill the buffer with the magnetic calibration in bytes 0 to 67 (total 68 bytes)
64  // [0-3]: four byte header denoting magnetic calibration present
65  itmp32 = 0x12345678;
66  pSrc = (uint8 *) &itmp32;
67  pDst = iNVMBuffer + MAG_NVM_OFFSET;
68  for (i = 0; i < 4; i++)
69  *(pDst++) = *(pSrc++);
70  // [4-67]: magnetic calibration: 15x float + 1x int32 total 64 bytes
71  pSrc = (uint8 *) &(sfg->MagCal);
72  for (i = 0; i < 64; i++)
73  *(pDst++) = *(pSrc++);
74 
75  // write the whole buffer contents to NVM
76  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
77 #endif // if F_USING_MAG
78  return;
79 }
80 
82 {
83 #if F_USING_GYRO && (F_9DOF_GBY_KALMAN || F_6DOF_GY_KALMAN)
84  uint8_t *pSrc, *pDst; // scratch pointers
85  int16_t i; // loop counter
86  uint8_t iNVMBuffer[256]; // NVM write buffer
87  uint32_t itmp32;
88 
89  // copy existing magnetic, gyro and accelerometer calibrations to buffer
90  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
91  pDst = iNVMBuffer;
92  for (i = 0; i < 256; i++)
93  *(pDst++) = *(pSrc++);
94  // default to no gyroscope calibration in header
95  iNVMBuffer[GYRO_NVM_OFFSET] = iNVMBuffer[GYRO_NVM_OFFSET + 1] = iNVMBuffer[GYRO_NVM_OFFSET + 2] = iNVMBuffer[GYRO_NVM_OFFSET + 3] = 0xFF;
96 
97  // define the four header bytes
98  // [0-3]: four byte header denoting gyro calibration present
99  itmp32 = 0x12345678;
100  pSrc = (uint8 *) &itmp32;
101  pDst = iNVMBuffer + GYRO_NVM_OFFSET;
102  for (i = 0; i < 4; i++)
103  *(pDst++) = *(pSrc++);
104 
105  // [4-15]: 3 gyro offset floats totalling 12 bytes
106 #if F_9DOF_GBY_KALMAN
107  pSrc = (uint8 *) sfg->SV_9DOF_GBY_KALMAN.fbPl;
108 #elif F_6DOF_GY_KALMAN
109  pSrc = (uint8 *) sfg->SV_6DOF_GY_KALMAN.fbPl;
110 #endif
111  for (i = 0; i < 12; i++)
112  *(pDst++) = *(pSrc++);
113 
114  // write the buffer contents to NVM
115  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
116 #endif
117  return;
118 }
119 
121 {
122 #if F_USING_ACCEL
123  uint8_t *pSrc, *pDst; // scratch pointers
124  int16_t i; // loop counter
125  uint8_t iNVMBuffer[256]; // NVM write buffer
126  uint32_t itmp32;
127 
128  // copy existing magnetic, gyro and accelerometer calibrations to buffer
129  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
130  pDst = iNVMBuffer;
131  for (i = 0; i < 256; i++)
132  *(pDst++) = *(pSrc++);
133  // default to no accelerometer calibration in header
134  iNVMBuffer[ACCEL_NVM_OFFSET] = iNVMBuffer[ACCEL_NVM_OFFSET + 1] = iNVMBuffer[ACCEL_NVM_OFFSET + 2] = iNVMBuffer[ACCEL_NVM_OFFSET + 3] = 0xFF;
135 
136  // [0-3]: four byte header denoting accelerometer calibration present
137  itmp32 = 0x12345678;
138  pSrc = (uint8 *) &itmp32;
139  pDst = iNVMBuffer + ACCEL_NVM_OFFSET;
140  for (i = 0; i < 4; i++)
141  *(pDst++) = *(pSrc++);
142 
143  // [4-87]: 21 precision accelerometer calibration floats totalling 84 bytes
144  pSrc = (uint8 *) &(sfg->AccelCal);
145  for (i = 0; i < 84; i++)
146  *(pDst++) = *(pSrc++);
147 
148  // write the buffer contents to NVM
149  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
150 #endif
151  return;
152 }
153 
155 {
156  uint8_t *pSrc, *pDst; // scratch pointers
157  int16_t i; // loop counter
158  uint8_t iNVMBuffer[256]; // NVM write buffer
159 
160  // copy existing magnetic, gyro and accelerometer calibrations to buffer
161  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
162  pDst = iNVMBuffer;
163  for (i = 0; i < 256; i++)
164  *(pDst++) = *(pSrc++);
165 
166  // set no magnetic calibration in header
167  iNVMBuffer[MAG_NVM_OFFSET] = iNVMBuffer[MAG_NVM_OFFSET + 1] = iNVMBuffer[MAG_NVM_OFFSET + 2] = iNVMBuffer[MAG_NVM_OFFSET + 3] = 0xFF;
168 
169  // write the buffer to flash
170  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
171 
172  return;
173 }
174 
176 {
177  uint8_t *pSrc, *pDst; // scratch pointers
178  int16_t i; // loop counter
179  uint8_t iNVMBuffer[256]; // NVM write buffer
180 
181  // copy existing magnetic, gyro and accelerometer calibrations to buffer
182  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
183  pDst = iNVMBuffer;
184  for (i = 0; i < 256; i++)
185  *(pDst++) = *(pSrc++);
186 
187  // set no gyroscope calibration in header
188  iNVMBuffer[GYRO_NVM_OFFSET] = iNVMBuffer[GYRO_NVM_OFFSET + 1] = iNVMBuffer[GYRO_NVM_OFFSET + 2] = iNVMBuffer[GYRO_NVM_OFFSET + 3] = 0xFF;
189 
190  // write the buffer to flash
191  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
192 
193  return;
194 }
195 
197 {
198  uint8_t *pSrc, *pDst; // scratch pointers
199  int16_t i; // loop counter
200  uint8_t iNVMBuffer[256]; // NVM write buffer
201 
202  // copy existing magnetic, gyro and accelerometer calibrations to buffer
203  pSrc = (uint8 *) CALIBRATION_NVM_ADDR;
204  pDst = iNVMBuffer;
205  for (i = 0; i < 256; i++)
206  *(pDst++) = *(pSrc++);
207 
208  // set no gyroscope calibration in header
209  iNVMBuffer[ACCEL_NVM_OFFSET] = iNVMBuffer[ACCEL_NVM_OFFSET + 1] = iNVMBuffer[ACCEL_NVM_OFFSET + 2] = iNVMBuffer[ACCEL_NVM_OFFSET + 3] = 0xFF;
210 
211  // write the buffer to flash
212  NVM_SetBlockFlash(iNVMBuffer, CALIBRATION_NVM_ADDR, 256);
213 
214  return;
215 }
void EraseGyroCalibrationFromNVM(void)
#define GYRO_NVM_OFFSET
Definition: frdm_k64f.h:180
#define CALIBRATION_NVM_ADDR
start of final 4K (sector size) of 1M flash
Definition: frdm_k64f.h:173
void SaveMagCalibrationToNVM(SensorFusionGlobals *sfg)
middleware driver for NVM on Kinetis devices
SensorFusionGlobals sfg
void SaveAccelCalibrationToNVM(SensorFusionGlobals *sfg)
void EraseMagCalibrationFromNVM(void)
uint8_t uint8
Definition: sensor_fusion.h:68
The top level fusion structure.
void SaveGyroCalibrationToNVM(SensorFusionGlobals *sfg)
byte NVM_SetBlockFlash(uint8_t *Source, uint32_t Dest, uint16_t Count)
#define F_6DOF_GY_KALMAN
6DOF accel and gyro (Kalman) algorithm selector - 0x2000 to include, 0x0000 otherwise ...
#define MAG_NVM_OFFSET
Definition: frdm_k64f.h:179
The sensor_fusion.h file implements the top level programming interface.
Provides functions to store calibration to NVM.
struct MagCalibration MagCal
mag cal storage
void EraseAccelCalibrationFromNVM(void)
#define ACCEL_NVM_OFFSET
Definition: frdm_k64f.h:181