CalLib.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //  This file is part of MPU9150Lib
00004 //
00005 //  Copyright (c) 2013 Pansenti, LLC
00006 //
00007 //  Permission is hereby granted, free of charge, to any person obtaining a copy of 
00008 //  this software and associated documentation files (the "Software"), to deal in 
00009 //  the Software without restriction, including without limitation the rights to use, 
00010 //  copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
00011 //  Software, and to permit persons to whom the Software is furnished to do so, 
00012 //  subject to the following conditions:
00013 //
00014 //  The above copyright notice and this permission notice shall be included in all 
00015 //  copies or substantial portions of the Software.
00016 //
00017 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
00018 //  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
00019 //  PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
00020 //  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
00021 //  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
00022 //  SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023 
00024 #include "CalLib.h"
00025 #ifdef __SAM3X8E__
00026 
00027 // Due version
00028 
00029 #include "DueFlash.h"
00030 
00031 DueFlash flash;
00032 
00033 void calLibErase(byte device)
00034 {
00035     uint32_t data = 0;
00036 
00037     flash.write(CALLIB_START + sizeof(CALLIB_DATA) * device, &data, 1); // just destroy the valid byte
00038 }
00039 
00040 void calLibWrite(byte device, CALLIB_DATA *calData)
00041 {
00042     calData->valid = CALLIB_DATA_VALID;
00043 
00044     flash.write(CALLIB_START + sizeof(CALLIB_DATA) * device, (uint32_t *)calData, sizeof(CALLIB_DATA) / 4);
00045 }
00046 
00047 boolean calLibRead(byte device, CALLIB_DATA *calData)
00048 {
00049     memcpy(calData, CALLIB_START + sizeof(CALLIB_DATA) * device, sizeof(CALLIB_DATA));
00050     return calData->valid == CALLIB_DATA_VALID;
00051 }
00052 
00053 #else
00054 
00055 // AVR version
00056 
00057 #include <EEPROM.h>
00058 
00059 void calLibErase(byte device)
00060 {
00061     EEPROM.write(sizeof(CALLIB_DATA) * device, 0); // just destroy the valid byte
00062 }
00063 
00064 void calLibWrite(byte device, CALLIB_DATA *calData)
00065 {
00066   byte *ptr = (byte *)calData;
00067   byte length = sizeof(CALLIB_DATA);
00068   int eeprom = sizeof(CALLIB_DATA) * device;
00069 
00070   calData->valid = CALLIB_DATA_VALID;
00071   
00072   for (byte i = 0; i < length; i++)
00073     EEPROM.write(eeprom + i, *ptr++);
00074 }
00075 
00076 boolean calLibRead(byte device, CALLIB_DATA *calData)
00077 {
00078   byte *ptr = (byte *)calData;
00079   byte length = sizeof(CALLIB_DATA);
00080   int eeprom = sizeof(CALLIB_DATA) * device;
00081 
00082   calData->magValid = false;
00083   calData->accelValid = false;
00084 
00085   if ((EEPROM.read(eeprom) != CALLIB_DATA_VALID_LOW) ||
00086       (EEPROM.read(eeprom + 1) != CALLIB_DATA_VALID_HIGH))
00087     return false;                                  // invalid data
00088     
00089   for (byte i = 0; i < length; i++)
00090     *ptr++ = EEPROM.read(eeprom + i);
00091   return true;  
00092 }
00093 #endif


ric_mc
Author(s): RoboTiCan
autogenerated on Fri May 20 2016 03:48:55