8000 Update hal_flash itf functions for compatibility · runtimeco/mynewt_arduino_zero@240d057 · GitHub
[go: up one dir, main page]

Skip to content

Commit 240d057

Browse files
committed
Update hal_flash itf functions for compatibility
hal_flash itf functions now get the hal_flash struct pointer as first parameter to allow customization for external memories.
1 parent 29a2916 commit 240d057

File tree

1 file changed

+46
-42
lines changed

1 file changed

+46
-42
lines changed

hw/mcu/atmel/samd21xx/src/hal_flash.c

Lines changed: 46 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,15 @@
3838

3939
#define SAMD21_FLASH_PAGES_PER_SECTOR (SAMD21_FLASH_PAGES_PER_ROW*SAMD21_FLASH_ROWS_PER_SECTOR)
4040

41-
static int samd21_flash_read(uint32_t address, void *dst, uint32_t num_bytes);
42-
static int samd21_flash_write(uint32_t address, const void *src,
43-
uint32_t num_bytes);
44-
static int samd21_flash_erase_sector(uint32_t sector_address);
45-
static int samd21_flash_sector_info(int idx, uint32_t *addr, uint32_t *sz);
46-
static int samd21_flash_init(void);
41+
static int samd21_flash_read(const struct hal_flash *dev, uint32_t address,
42+
void *dst, uint32_t num_bytes);
43+
static int samd21_flash_write(const struct hal_flash *dev, uint32_t address,
44+
const void *src, uint32_t num_bytes);
45+
static int samd21_flash_erase_sector(const struct hal_flash *dev,
46+
uint32_t sector_address);
47+
static int samd21_flash_sector_info(const struct hal_flash *dev, int idx,
48+
uint32_t *addr, uint32_t *sz);
49+
static int samd21_flash_init(const struct hal_flash *dev);
4750

4851
static const struct hal_flash_funcs samd21_flash_funcs = {
4952
.hff_read = samd21_flash_read,
@@ -59,51 +62,53 @@ struct hal_flash samd21_flash_dev = {
5962
};
6063

6164
static int
62-
samd21_flash_read(uint32_t address, void *dst, uint32_t num_bytes)
65+
samd21_flash_read(const struct hal_flash *dev, uint32_t address,
66+
void *dst, uint32_t num_bytes)
6367
{
6468
int base_address;
6569
int offset;
6670
struct nvm_parameters params;
6771
uint8_t page_buffer[64];
6872
uint8_t *pdst = dst;
69-
73+
7074
/* make sure this fits into our stack buffer */
71-
nvm_get_parameters(&params);
75+
nvm_get_parameters(&params);
7276
assert(params.page_size <= sizeof(page_buffer));
73-
77+
7478
/* get the page address (this is not a sector, there are 4 pages per
7579
* row */
76-
while(num_bytes) {
80+
while(num_bytes) {
7781
int read_len;
78-
82+
7983
base_address = address & ~(params.page_size - 1);
80-
84+
8185
offset = address - base_address;
8286
read_len = params.page_size - offset;
83-
87+
8488
if(read_len > num_bytes) {
8589
read_len = num_bytes;
8690
}
8791

88-
if(nvm_read_buffer(base_address, page_buffer, params.page_size)
92+
if(nvm_read_buffer(base_address, page_buffer, params.page_size)
8993
!= STATUS_OK) {
9094
return -1;
9195
}
92-
96+
9397
/* move the pointers since this is a sure thing now */
9498
num_bytes -= read_len;
95-
address += read_len;
99+
address += read_len;
96100

97101
/* copy it into the page buffer */
98102
while(read_len--) {
99103
*pdst++ = page_buffer[offset++];
100-
}
101-
}
104+
}
105+
}
102106
return 0;
103107
}
104108

105109
static int
106-
samd21_flash_write(uint32_t address, const void *src, uint32_t len)
110+
samd21_flash_write(const struct hal_flash *dev, uint32_t address,
111+
const void *src, uint32_t len)
107112
{
108113
int base_address;
109114
int offset;
@@ -162,19 +167,19 @@ samd21_flash_write(uint32_t address, const void *src, uint32_t len)
162167
}
163168

164169
static int
165-
samd21_flash_erase_sector(uint32_t sector_address)
170+
samd21_flash_erase_sector(const struct hal_flash *dev, uint32_t sector_address)
166171
{
167-
struct nvm_parameters params;
172+
struct nvm_parameters params;
168173
int rc;
169174
int i;
170175

171-
nvm_get_parameters(&params);
172-
176+
nvm_get_parameters(&params);
177+
173178
/* erase all rows in the sector */
174179
for(i = 0; i < SAMD21_FLASH_ROWS_PER_SECTOR; i++) {
175-
uint32_t row_address = sector_address +
180+
uint32_t row_address = sector_address +
176181
i*SAMD21_FLASH_PAGES_PER_ROW*params.page_size;
177-
rc = nvm_erase_row(row_address);
182+
rc = nvm_erase_row(row_address);
178183
if(rc != STATUS_OK) {
179184
return -1;
180185
}
@@ -184,53 +189,52 @@ samd21_flash_erase_sector(uint32_t sector_address)
184189

185190
/* samd21 flash always starts as 0x000000 */
186191
static int
187-
samd21_flash_sector_info(int idx, uint32_t *addr, uint32_t *sz)
192+
samd21_flash_sector_info(const struct hal_flash *dev, int idx,
193+
uint32_t *addr, uint32_t *sz)
188194
{
189195
struct nvm_parameters params;
190196
int sector_size;
191197
int sector_cnt;
192-
198+
193199
nvm_get_parameters(&params);
194200
sector_cnt = params.nvm_number_of_pages/SAMD21_FLASH_PAGES_PER_SECTOR;
195201
sector_size = params.page_size*SAMD21_FLASH_PAGES_PER_SECTOR;
196-
202+
197203
if((idx >= sector_cnt) || (idx < 0)){
198204
return -1;
199205
}
200-
206+
201207
*sz = sector_size;
202-
*addr = idx * sector_size + SAMD21_FLASH_START_ADDR;
208+
*addr = idx * sector_size + SAMD21_FLASH_START_ADDR;
203209
return 0;
204210
}
205211

206212
static int
207-
samd21_flash_init(void)
213+
samd21_flash_init(const struct hal_flash *dev)
208214
{
209215
int rc;
210216
struct nvm_config cfg;
211217
struct nvm_parameters params;
212218
nvm_get_config_defaults(&cfg);
213-
219+
214220
cfg.manual_page_write = false;
215221
rc = nvm_set_config(&cfg);
216222
if(rc != STATUS_OK) {
217223
return -1;
218224
}
219-
220-
nvm_get_parameters(&params);
221225

222-
/* the samd21 flash doesn't use sector terminology. They use Row and
226+
nvm_get_parameters(&params);
227+
228+
/* the samd21 flash doesn't use sector terminology. They use Row and
223229
* page. A row contains 4 pages. Each pages is a fixed size. You can
224-
* only erase based on row. Here I will map the rows to sectors and
225-
* deal with pages inside this driver. */
230+
* only erase based on row. Here I will map the rows to sectors and
231+
* deal with pages inside this driver. */
226232
samd21_flash_dev.hf_itf = &samd21_flash_funcs;
227233
samd21_flash_dev.hf_base_addr = SAMD21_FLASH_START_ADDR;
228234
samd21_flash_dev.hf_size = params.nvm_number_of_pages * params.page_size;
229-
samd21_flash_dev.hf_sector_cnt =
235+
samd21_flash_dev.hf_sector_cnt =
230236
params.nvm_number_of_pages/SAMD21_FLASH_PAGES_PER_SECTOR;
231237
samd21_flash_dev.hf_align = 1;
232-
238+
233239
return 0;
234240
}
235-
236-

0 commit comments

Comments
 (0)
0