DueFlash.cpp
Go to the documentation of this file.
00001 
00002 //
00003 //  This file is part of DueFlash
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 "DueFlash.h"
00025 
00026 DueFlash::DueFlash()
00027 {
00028   uint32_t retCode;
00029 
00030   /* Initialize flash: 6 wait states for flash writing. */
00031 
00032   retCode = flash_init(FLASH_ACCESS_MODE_128, 6);
00033   if (retCode != FLASH_RC_OK) {
00034     _FLASH_DEBUG("Flash init failed\n");
00035   }
00036 }
00037 
00038 boolean DueFlash::write(uint32_t *flashStart, uint32_t *data, uint32_t dataLength)
00039 {
00040   uint32_t retCode;
00041   uint32_t byteLength = dataLength * sizeof(uint32_t);
00042   
00043   if ((uint32_t)flashStart < IFLASH1_ADDR) {
00044     _FLASH_DEBUG("Flash write address too low\n");
00045     return false;
00046   }
00047 
00048   if ((uint32_t)flashStart >= (IFLASH1_ADDR + IFLASH1_SIZE)) {
00049     _FLASH_DEBUG("Flash write address too high\n");
00050     return false;
00051   }
00052 
00053   if (((uint32_t)flashStart & 3) != 0) {
00054       _FLASH_DEBUG("Flash start address must be on four byte boundary\n");
00055       return false;
00056   }
00057 
00058   // Unlock page
00059 
00060   retCode = flash_unlock((uint32_t)flashStart, (uint32_t)flashStart + byteLength - 1, 0, 0);
00061   if (retCode != FLASH_RC_OK) {
00062     _FLASH_DEBUG("Failed to unlock flash for write\n");
00063     return false;
00064   }
00065 
00066   // write data
00067 
00068   retCode = flash_write((uint32_t)flashStart, data, byteLength, 1);
00069 
00070   if (retCode != FLASH_RC_OK) {
00071     _FLASH_DEBUG("Flash write failed\n");
00072     return false;
00073   }
00074 
00075   // Lock page
00076  
00077   retCode = flash_lock((uint32_t)flashStart, (uint32_t)flashStart + byteLength - 1, 0, 0);
00078   if (retCode != FLASH_RC_OK) {
00079     _FLASH_DEBUG("Failed to lock flash page\n");
00080     return false;
00081   }
00082   return true;
00083 }


lizi_arduino
Author(s): RoboTiCan
autogenerated on Wed Aug 26 2015 12:24:22