chen
2024-07-29 13ee763a011697633a072a74a25c3eee9f40bb4f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*************************************************************************************************/
/*!
 *  \file
 *
 *  \brief  PAL Flash driver.
 *
 *  Copyright (c) 2018-2019 Arm Ltd. All Rights Reserved.
 *
 *  Copyright (c) 2019-2020 Packetcraft, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
/*************************************************************************************************/
 
#include <string.h>
#include "pal_flash.h"
#include "mk_flash.h"
 
/**************************************************************************************************
  Macros
**************************************************************************************************/
// The size of flash space available to users is 8K
#define MK8000_USER_TOTAL_SIZE 0x2000
// The starting address is 0x0407E000, 126th sector
#define MK8000_USER_START_ADDR 0x0407E000
// The end address is 0x0407FFFF
#define MK8000_USER_END_ADDR (MK8000_USER_START_ADDR + MK8000_USER_TOTAL_SIZE - 1)
 
/*! Flash block size. */
#define PAL_FLASH_SECTOR4K 1
// #define PAL_FLASH_SECTOR64K 16
 
#define PAL_FLASH_SECTOR4K_SIZE FLASH_SECTOR_SIZE
// #define PAL_FLASH_SECTOR64K_SIZE 0x10000
 
/*! Flash Total size. */
#define PAL_FLASH_TOTAL_SIZE MK8000_USER_TOTAL_SIZE
 
/*! Flash internal cache buffer size. Note: should be at least 2. */
#define PAL_FLASH_CACHE_BUF_SIZE 11
 
/*! Flash word size. */
#define PAL_FLASH_WORD_SIZE 4
 
/*! Aligns a value to word size. */
#define PAL_FLASH_WORD_ALIGN(value) (((value) + (PAL_FLASH_WORD_SIZE - 1)) & ~(PAL_FLASH_WORD_SIZE - 1))
/*! Validates if a value is aligned to word. */
#define PAL_FLASH_IS_WORD_ALIGNED(value) (((uint32_t)(value) & (PAL_FLASH_WORD_SIZE - 1)) == 0)
 
/*! QSPI flash commands. */
#define QSPI_STD_CMD_WRSR 0x01
#define QSPI_STD_CMD_RSTEN 0x66
#define QSPI_STD_CMD_RST 0x99
 
#ifdef DEBUG
 
/*! \brief      Parameter check. */
#define PAL_FLASH_PARAM_CHECK(expr)                   \
    {                                                 \
        if (!(expr))                                  \
        {                                             \
            palFlashCb.state = PAL_FLASH_STATE_ERROR; \
            return;                                   \
        }                                             \
    }
 
#else
 
/*! \brief      Parameter check (disabled). */
#define PAL_FLASH_PARAM_CHECK(expr)
 
#endif
 
/**************************************************************************************************
  Local Variables
**************************************************************************************************/
 
/*! flash cache buffer. */
static uint32_t palFlashCacheBuf[PAL_FLASH_CACHE_BUF_SIZE];
static enum FLASH_DEV_T MK8000_FLASH_ID = FLASH_ID0;
static enum FLASH_DEV_T flash_id = FLASH_MAX_NUM;
 
/*! \brief      Control block. */
static struct
{
    PalFlashState_t state; /*!< State. */
    uint32_t writeAddr;    /*!< Write address. */
} palFlashCb;
 
/**************************************************************************************************
  Global Functions
**************************************************************************************************/
 
/*************************************************************************************************/
/*!
 *  \brief  Initialize flash.
 *
 *  \param[in] actCback     Callback function.
 */
/*************************************************************************************************/
void PalFlashInit(PalFlashCback_t actCback)
{
    (void)palFlashCacheBuf;
    if (DRV_OK == flash_open(MK8000_FLASH_ID, NULL))
    {
        flash_id = FLASH_ID0;
    }
}
 
/*************************************************************************************************/
/*!
 *  \brief  De-initialize flash.
 */
/*************************************************************************************************/
void PalFlashDeInit(void)
{
    flash_close(flash_id);
}
 
/*************************************************************************************************/
/*!
 *  \brief     Reads data from flash storage.
 *
 *  \param[in] pBuf     Pointer to memory buffer where data will be stored.
 *  \param[in] size     Data size in bytes to be read.
 *  \param[in] srcAddr  Word aligned address from where data is read.
 */
/*************************************************************************************************/
void PalFlashRead(void *pBuf, uint32_t size, uint32_t srcAddr)
{
    uint32_t realAddr;
    memset(pBuf, 0xFF, size);
    realAddr = MK8000_USER_START_ADDR + srcAddr;
    flash_read(flash_id, realAddr, (uint8_t *)pBuf, size);
}
 
/*************************************************************************************************/
/*!
 *  \brief     Writes data to flash storage.
 *
 *  \param[in] pBuf     Pointer to memory buffer from where data will be written.
 *  \param[in] size     Data size in bytes to be written.
 *  \param[in] dstAddr  Word aligned address to write data.
 */
/*************************************************************************************************/
void PalFlashWrite(const uint8_t *pBuf, uint32_t size, uint32_t dstAddr)
{
#if FLASH_WRITE_EN
    uint32_t realAddr;
    realAddr = MK8000_USER_START_ADDR + dstAddr;
    flash_write_nbytes(flash_id, realAddr, pBuf, size);
#endif
}
 
/*************************************************************************************************/
/*!
 *  \brief  Erase sector.
 *
 *  \param[in] numOfSectors       Number of sectors to be erased.
 *  \param[in] startAddr          Word aligned address.
 */
/*************************************************************************************************/
void PalFlashEraseSector(uint32_t numOfSectors, uint32_t startAddr)
{
#if FLASH_WRITE_EN
    uint32_t realAddr;
    realAddr = MK8000_USER_START_ADDR + startAddr;
    if (PAL_FLASH_IS_WORD_ALIGNED(realAddr) && (numOfSectors > 0) && (numOfSectors <= (PAL_FLASH_TOTAL_SIZE / PAL_FLASH_SECTOR4K_SIZE)))
    {
        uint32_t sector_idx = (realAddr - FLASH_BASE) / PAL_FLASH_SECTOR4K_SIZE;
        if ((sector_idx + numOfSectors) <= (FLASH_SIZE / PAL_FLASH_SECTOR4K_SIZE))
        {
            for (uint32_t i = 0; i < numOfSectors; i++, sector_idx++)
            {
                flash_sector_erase(flash_id, sector_idx);
            }
        }
    }
#endif
}
 
/*************************************************************************************************/
/*!
 *  \brief  Erase chip. It is not recommended to use since it takes up to 240s.
 */
/*************************************************************************************************/
void PalFlashEraseChip(void)
{
}
 
/*************************************************************************************************/
/*!
 *  \brief  Get total size of NVM storage.
 *
 *  \return Total size.
 */
/*************************************************************************************************/
uint32_t PalNvmGetTotalSize(void)
{
    return PAL_FLASH_TOTAL_SIZE;
}
 
/*************************************************************************************************/
/*!
 *  \brief  Get sector size of NVM storage.
 *
 *  \return Sector size.
 */
/*************************************************************************************************/
uint32_t PalNvmGetSectorSize(void)
{
    return PAL_FLASH_SECTOR4K_SIZE;
}
 
/*************************************************************************************************/
/*!
 *  \brief     Get flash state.
 *
 *  \return    flash state.
 */
/*************************************************************************************************/
PalFlashState_t PalFlashGetState(void)
{
    return palFlashCb.state;
}