WXK
2024-12-16 78e84fcf264afd731cd66c807d9fcb690fe12126
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 <sys/mman.h>
#include <fcntl.h>
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include <stdlib.h>
#include <unistd.h>
 
#include "os/mynewt.h"
 
#include "hal/hal_flash_int.h"
#include "mcu/mcu_sim.h"
 
char *native_flash_file;
static int file = -1;
static void *file_loc;
 
static int native_flash_init(const struct hal_flash *dev);
static int native_flash_read(const struct hal_flash *dev, uint32_t address,
        void *dst, uint32_t length);
static int native_flash_write(const struct hal_flash *dev, uint32_t address,
        const void *src, uint32_t length);
static int native_flash_erase_sector(const struct hal_flash *dev,
        uint32_t sector_address);
static int native_flash_sector_info(const struct hal_flash *dev, int idx,
        uint32_t *address, uint32_t *size);
 
static const struct hal_flash_funcs native_flash_funcs = {
    .hff_read = native_flash_read,
    .hff_write = native_flash_write,
    .hff_erase_sector = native_flash_erase_sector,
    .hff_sector_info = native_flash_sector_info,
    .hff_init = native_flash_init
};
 
#if MYNEWT_VAL(MCU_FLASH_STYLE_ST)
static const uint32_t native_flash_sectors[] = {
    0x00000000, /* 16 * 1024 */
    0x00004000, /* 16 * 1024 */
    0x00008000, /* 16 * 1024 */
    0x0000c000, /* 16 * 1024 */
    0x00010000, /* 64 * 1024 */
    0x00020000, /* 128 * 1024 */
    0x00040000, /* 128 * 1024 */
    0x00060000, /* 128 * 1024 */
    0x00080000, /* 128 * 1024 */
    0x000a0000, /* 128 * 1024 */
    0x000c0000, /* 128 * 1024 */
    0x000e0000, /* 128 * 1024 */
};
#elif MYNEWT_VAL(MCU_FLASH_STYLE_NORDIC)
static uint32_t native_flash_sectors[1024 * 1024 / 2048];
#else
#error "Need to specify either MCU_FLASH_STYLE_NORDIC or MCU_FLASH_STYLE_ST"
#endif
 
#define FLASH_NUM_AREAS   (int)(sizeof native_flash_sectors /           \
                                sizeof native_flash_sectors[0])
 
const struct hal_flash native_flash_dev = {
    .hf_itf = &native_flash_funcs,
    .hf_base_addr = 0,
    .hf_size = 1024 * 1024,
    .hf_sector_cnt = FLASH_NUM_AREAS,
    .hf_align = MYNEWT_VAL(MCU_FLASH_MIN_WRITE_SIZE),
    .hf_erased_val = 0xff,
};
 
static void
flash_native_erase(uint32_t addr, uint32_t len)
{
    memset(file_loc + addr, 0xff, len);
}
 
static void
flash_native_file_open(char *name)
{
    int created = 0;
    char tmpl[] = "/tmp/native_flash.XXXXXX";
 
    extern int ftruncate(int fd, off_t length);
 
    if (file != -1) {
        close(file);
        file = -1;
    }
 
    if (name) {
        file = open(name, O_RDWR);
        if (file < 0) {
            file = open(name, O_RDWR | O_CREAT, 0660);
            assert(file > 0);
            created = 1;
        }
    } else {
        file = mkstemp(tmpl);
        assert(file > 0);
        created = 1;
    }
 
    if (created) {
        if (ftruncate(file, native_flash_dev.hf_size) < 0) {
            assert(0);
        }
    }
 
    if (file_loc != NULL) {
        munmap(file_loc, native_flash_dev.hf_size);
    }
 
    file_loc = mmap(0, native_flash_dev.hf_size,
          PROT_READ | PROT_WRITE, MAP_SHARED, file, 0);
    assert(file_loc != MAP_FAILED);
    if (created) {
        flash_native_erase(0, native_flash_dev.hf_size);
    }
 
    /* If using a temporary file, unlink it immediately. */
    if (name == NULL) {
        remove(tmpl);
    }
}
 
static void
flash_native_ensure_file_open(void)
{
    if (file == 0) {
        flash_native_file_open(NULL);
    }
}
 
static int
flash_native_write_internal(uint32_t address, const void *src, uint32_t length,
                            int allow_overwrite)
{
    static uint8_t buf[256];
    uint32_t cur;
    uint32_t end;
    int chunk_sz;
    int rc;
    int i;
 
    if (length == 0) {
        return 0;
    }
 
    end = address + length;
 
    flash_native_ensure_file_open();
 
    cur = address;
    while (cur < end) {
        if (end - cur < sizeof buf) {
            chunk_sz = end - cur;
        } else {
            chunk_sz = sizeof buf;
        }
 
        /* Ensure data is not being overwritten. */
        if (!allow_overwrite) {
            rc = native_flash_read(NULL, cur, buf, chunk_sz);
            assert(rc == 0);
            for (i = 0; i < chunk_sz; i++) {
                assert(buf[i] == 0xff);
            }
        }
 
        cur += chunk_sz;
    }
 
    memcpy((char *)file_loc + address, src, length);
 
    return 0;
}
 
static int
native_flash_write(const struct hal_flash *dev, uint32_t address,
        const void *src, uint32_t length)
{
    assert(address % native_flash_dev.hf_align == 0);
    return flash_native_write_internal(address, src, length, 0);
}
 
int
flash_native_memset(uint32_t offset, uint8_t c, uint32_t len)
{
    memset(file_loc + offset, c, len);
    return 0;
}
 
static int
native_flash_read(const struct hal_flash *dev, uint32_t address, void *dst,
        uint32_t length)
{
    flash_native_ensure_file_open();
    memcpy(dst, (char *)file_loc + address, length);
 
    return 0;
}
 
static int
find_area(uint32_t address)
{
    int i;
 
    for (i = 0; i < FLASH_NUM_AREAS; i++) {
        if (native_flash_sectors[i] == address) {
            return i;
        }
    }
 
    return -1;
}
 
static int
flash_sector_len(int sector)
{
    uint32_t end;
 
    if (sector == FLASH_NUM_AREAS - 1) {
        end = native_flash_dev.hf_size + native_flash_sectors[0];
    } else {
        end = native_flash_sectors[sector + 1];
    }
    return end - native_flash_sectors[sector];
}
 
static int
native_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address)
{
    int area_id;
    uint32_t len;
 
    flash_native_ensure_file_open();
 
    area_id = find_area(sector_address);
    if (area_id == -1) {
        return -1;
    }
    len = flash_sector_len(area_id);
    flash_native_erase(sector_address, len);
    return 0;
}
 
static int
native_flash_sector_info(const struct hal_flash *dev, int idx,
        uint32_t *address, uint32_t *size)
{
    assert(idx < FLASH_NUM_AREAS);
 
    *address = native_flash_sectors[idx];
    *size = flash_sector_len(idx);
    return 0;
}
 
static int
native_flash_init(const struct hal_flash *dev)
{
    //if (native_flash_file) {
        flash_native_file_open(native_flash_file);
    //}
#if MYNEWT_VAL(MCU_FLASH_STYLE_NORDIC)
    int i;
 
    for (i = 0; i < FLASH_NUM_AREAS; i++) {
        native_flash_sectors[i] = i * 2048;
    }
#endif
    return 0;
}