ISSDK  1.7
IoT Sensing Software Development Kit
driver_KSDK_NVM.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 /*! \file driver_KSDK_NVM.c
36  \brief middleware driver for NVM on Kinetis devices
37 */
38 #include "sensor_fusion.h"
39 #include "driver_KSDK_NVM.h"
40 
41 #ifndef CPU_LPC54114J256BD64_cm4
42 #include "fsl_flash.h"
43 #else
44 #include "fsl_flashiap.h"
45 #endif
46 
47 #define ERROR 1
48 #define SUCCESS 0;
49 #ifndef CPU_LPC54114J256BD64_cm4
50 byte NVM_SetBlockFlash(uint8_t *Source, uint32_t Dest, uint16_t Count)
51 {
52  byte retVal=0;
53  status_t result;
54  uint32_t pflashSectorSize = ERROR;
55  flash_config_t flashDriver; // Flash driver Structure
56 
57  // Clean up Flash driver Structure
58  memset(&flashDriver, 0, sizeof(flash_config_t));
59  // Setup flash driver structure for device and initialize variables.
60  result = FLASH_Init(&flashDriver);
61  if (kStatus_FLASH_Success == result)
62  {
63  FLASH_GetProperty(&flashDriver, FLASH_SECTOR_SIZE_PROPERTY, &pflashSectorSize);
64  result = FLASH_Erase(&flashDriver, Dest, pflashSectorSize, FLASH_ERASE_KEY);
65 
66  if (kStatus_FLASH_Success == result) {
67  result = FLASH_Program(&flashDriver, Dest, (uint8_t*) Source, Count);
68  if (kStatus_FLASH_Success == result) retVal=SUCCESS;
69  }
70  }
71  return(retVal);
72 }
73 #else
74 byte checkIAPStatus(status_t result) {
75  byte retVal=0;
76  switch (result) {
77  case kStatus_FLASHIAP_Success:
78  retVal = SUCCESS;
79  break;
80  case kStatus_FLASHIAP_InvalidCommand :
81  retVal = ERROR;
82  break;
83  case kStatus_FLASHIAP_SrcAddrError :
84  retVal = ERROR;
85  break;
86  case kStatus_FLASHIAP_DstAddrError :
87  retVal = ERROR;
88  break;
89  case kStatus_FLASHIAP_SrcAddrNotMapped :
90  retVal = ERROR;
91  break;
92  case kStatus_FLASHIAP_DstAddrNotMapped :
93  retVal = ERROR;
94  break;
95  case kStatus_FLASHIAP_CountError :
96  retVal = ERROR;
97  break;
98  case kStatus_FLASHIAP_InvalidSector :
99  retVal = ERROR;
100  break;
101  case kStatus_FLASHIAP_NotPrepared :
102  retVal = ERROR;
103  break;
104  case kStatus_FLASHIAP_CompareError :
105  retVal = ERROR;
106  break;
107  case kStatus_FLASHIAP_Busy :
108  retVal = ERROR;
109  break;
110  case kStatus_FLASHIAP_ParamError :
111  retVal = ERROR;
112  break;
113  case kStatus_FLASHIAP_AddrError :
114  retVal = ERROR;
115  break;
116  case kStatus_FLASHIAP_AddrNotMapped :
117  retVal = ERROR;
118  break;
119  case kStatus_FLASHIAP_NoPower :
120  retVal = ERROR;
121  break;
122  case kStatus_FLASHIAP_NoClock :
123  retVal = ERROR;
124  break;
125  default:
126  break;
127  }
128  return retVal;
129 }
130 
131 byte NVM_SetBlockFlash(uint8_t *Source, uint32_t Dest, uint16_t Count)
132 {
133  byte retVal=ERROR;
134  status_t result;
135  result = FLASHIAP_PrepareSectorForWrite(NVM_SECTOR_NUMBER, NVM_SECTOR_NUMBER);
136  if (result == kStatus_FLASHIAP_Success) {
137  result = FLASHIAP_ErasePage(NVM_PAGE_NUMBER, NVM_PAGE_NUMBER, SystemCoreClock);
138  if (result == kStatus_FLASHIAP_Success) {
139  result = FLASHIAP_PrepareSectorForWrite(NVM_SECTOR_NUMBER, NVM_SECTOR_NUMBER);
140  if (result == kStatus_FLASHIAP_Success) {
141 
142  result = FLASHIAP_CopyRamToFlash(CALIBRATION_NVM_ADDR, (uint32_t *) Source, (uint32_t) Count, SystemCoreClock);
143  //if (result == kStatus_FLASHIAP_Success) retVal = SUCCESS;
144  retVal = checkIAPStatus(result);
145  }
146  }
147  }
148  return(retVal);
149 }
150 #endif
#define CALIBRATION_NVM_ADDR
start of final 4K (sector size) of 1M flash
Definition: frdm_k64f.h:173
middleware driver for NVM on Kinetis devices
byte NVM_SetBlockFlash(uint8_t *Source, uint32_t Dest, uint16_t Count)
#define ERROR
The sensor_fusion.h file implements the top level programming interface.
unsigned char byte
Definition: sensor_fusion.h:64
uint32_t SystemCoreClock
#define NVM_SECTOR_NUMBER
Sector number in flash where we will store parameters.
Definition: lpc54114.h:154
#define NVM_PAGE_NUMBER
Page number in flash where we will store parameters.
Definition: lpc54114.h:155
#define FLASH_ERASE_KEY
Definition: frdm_k64f.h:176
#define SUCCESS
#define FLASH_SECTOR_SIZE_PROPERTY
Definition: frdm_k64f.h:175