Skip to content

Commit f5ec780

Browse files
committed
ports: analog: Update internal_flash.c to allow for multiple banks
Signed-off-by: Brandon <[email protected]>
1 parent 8fa4d8d commit f5ec780

File tree

1 file changed

+44
-40
lines changed

1 file changed

+44
-40
lines changed

ports/analog/supervisor/internal_flash.c

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ typedef struct {
5757
const uint32_t num_sectors;
5858
} flash_layout_t;
5959

60-
#ifdef MAX32690
60+
#if defined(MAX32690)
6161
// struct layout is the actual layout of flash
6262
// FS Code will use INTERNAL_FLASH_FILESYSTEM_START_ADDR
6363
// and won't conflict with ISR vector in first 16 KiB of flash
@@ -67,7 +67,21 @@ static const flash_layout_t flash_layout[] = {
6767
};
6868
// must be able to hold a full page (for re-writing upon erase)
6969
static uint32_t page_buffer[FLASH_PAGE_SIZE / 4] = {0x0};
70-
70+
#elif defined(MAX32650)
71+
static const flash_layout_t flash_layout[] = {
72+
{ 0x10000000, FLASH_PAGE_SIZE, 192},
73+
};
74+
// must be able to hold a full page (for re-writing upon erase)
75+
static uint32_t page_buffer[FLASH_PAGE_SIZE / 4] = {0x0};
76+
#elif defined(MAX32666)
77+
// MAX32666 has two flash banks, but we do not actually need to
78+
// treat them separately
79+
static const flash_layout_t flash_layout[] = {
80+
{ 0x10000000, FLASH_PAGE_SIZE, 64},
81+
{ 0x10080000, FLASH_PAGE_SIZE, 64},
82+
};
83+
// must be able to hold a full page (for re-writing upon erase)
84+
static uint32_t page_buffer[FLASH_PAGE_SIZE / 4] = {0x0};
7185
#else
7286
#error "Invalid BOARD. Please set BOARD equal to any board under 'boards/'."
7387
#endif
@@ -82,44 +96,28 @@ static inline int32_t block2addr(uint32_t block) {
8296

8397
// Get index, start addr, & size of the flash sector where addr lies
8498
int flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
85-
// This function should return -1 in the event of errors.
86-
if (addr >= flash_layout[0].base_addr) {
87-
uint32_t sector_index = 0;
88-
if (MP_ARRAY_SIZE(flash_layout) == 1) {
89-
sector_index = (addr - flash_layout[0].base_addr) / flash_layout[0].sector_size;
90-
if (sector_index >= flash_layout[0].num_sectors) {
91-
return -1; // addr is not in flash
92-
}
93-
if (start_addr) {
94-
*start_addr = flash_layout[0].base_addr + (sector_index * flash_layout[0].sector_size);
95-
} else {
96-
return -1; // start_addr is NULL
97-
}
98-
if (size) {
99-
*size = flash_layout[0].sector_size;
100-
} else {
101-
return -1; // size is NULL
102-
}
103-
return sector_index;
104-
}
99+
if (start_addr == NULL) {
100+
return -1;
101+
}
105102

106-
// algorithm for multiple flash sections
107-
for (uint8_t i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) {
108-
for (uint8_t j = 0; j < flash_layout[i].num_sectors; ++j) {
109-
uint32_t sector_start_next = flash_layout[i].base_addr
110-
+ (j + 1) * flash_layout[i].sector_size;
111-
if (addr < sector_start_next) {
112-
if (start_addr) {
113-
*start_addr = flash_layout[i].base_addr
114-
+ j * flash_layout[i].sector_size;
115-
}
116-
if (size) {
117-
*size = flash_layout[i].sector_size;
118-
}
119-
return sector_index;
120-
}
121-
++sector_index;
122-
}
103+
if (size == NULL) {
104+
return -1;
105+
}
106+
107+
// Search flash layout for a hit
108+
uint32_t sector_index = 0;
109+
for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) {
110+
flash_layout_t bank = flash_layout[i];
111+
112+
// Determine if the flash bank is a hit for this address
113+
if ((addr >= bank.base_addr) &&
114+
(addr < bank.base_addr + bank.sector_size * bank.num_sectors)
115+
) {
116+
// Assign the sector index assuming uniform sector sizes
117+
sector_index = i * bank.num_sectors + ((addr - bank.base_addr) / bank.sector_size);
118+
*start_addr = flash_layout[0].base_addr + (sector_index * bank.sector_size);
119+
*size = flash_layout[i].sector_size;
120+
return sector_index;
123121
}
124122
}
125123
return -1;
@@ -141,7 +139,7 @@ uint32_t supervisor_flash_get_block_count(void) {
141139
void port_internal_flash_flush(void) {
142140

143141
// Flush all instruction cache
144-
// ME18 has bug where top-level sysctrl flush bit only works one.
142+
// ME18 has bug where top-level sysctrl flush bit only works once.
145143
// Have to use low-level flush bits for each ICC instance.
146144
MXC_ICC_Flush(MXC_ICC0);
147145
MXC_ICC_Flush(MXC_ICC1);
@@ -213,6 +211,9 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t block_num,
213211
if (error != E_NO_ERROR) {
214212
// lock flash & reset
215213
MXC_FLC0->ctrl = (MXC_FLC0->ctrl & ~MXC_F_FLC_REVA_CTRL_UNLOCK) | MXC_S_FLC_REVA_CTRL_UNLOCK_LOCKED;
214+
#if defined(MAX32666)
215+
MXC_FLC1->ctrl = (MXC_FLC1->ctrl & ~MXC_F_FLC_REVA_CTRL_UNLOCK) | MXC_S_FLC_REVA_CTRL_UNLOCK_LOCKED;
216+
#endif
216217
reset_into_safe_mode(SAFE_MODE_FLASH_WRITE_FAIL);
217218
}
218219

@@ -228,6 +229,9 @@ mp_uint_t supervisor_flash_write_blocks(const uint8_t *src, uint32_t block_num,
228229
if (error != E_NO_ERROR) {
229230
// lock flash & reset
230231
MXC_FLC0->ctrl = (MXC_FLC0->ctrl & ~MXC_F_FLC_REVA_CTRL_UNLOCK) | MXC_S_FLC_REVA_CTRL_UNLOCK_LOCKED;
232+
#if defined(MAX32666)
233+
MXC_FLC1->ctrl = (MXC_FLC1->ctrl & ~MXC_F_FLC_REVA_CTRL_UNLOCK) | MXC_S_FLC_REVA_CTRL_UNLOCK_LOCKED;
234+
#endif
231235
reset_into_safe_mode(SAFE_MODE_FLASH_WRITE_FAIL);
232236
}
233237

0 commit comments

Comments
 (0)