Skip to content

Commit f3ecc0b

Browse files
author
Cruz Monrreal
authored
Merge pull request #9940 from davidsaada/david_psoc6_reduce_prog_size
Reduce flash page size from 512 to 32 bytes in PSOC6 based boards
2 parents c37ac83 + 9cacd02 commit f3ecc0b

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

targets/TARGET_Cypress/TARGET_PSOC6/flash_api.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
4747
{
4848
(void)(obj);
4949
int32_t status = 0;
50-
if (Cy_Flash_ProgramRow(address, (const uint32_t *)data) != CY_FLASH_DRV_SUCCESS) {
51-
status = -1;
50+
static uint8_t prog_buf[CY_FLASH_SIZEOF_ROW];
51+
while (size) {
52+
uint32_t offset = address % CY_FLASH_SIZEOF_ROW;
53+
uint32_t chunk_size;
54+
if (offset + size > CY_FLASH_SIZEOF_ROW) {
55+
chunk_size = CY_FLASH_SIZEOF_ROW - offset;
56+
} else {
57+
chunk_size = size;
58+
}
59+
uint32_t row_address = address / CY_FLASH_SIZEOF_ROW * CY_FLASH_SIZEOF_ROW;
60+
memcpy(prog_buf, (const void *)row_address, CY_FLASH_SIZEOF_ROW);
61+
memcpy(prog_buf + offset, data, chunk_size);
62+
63+
if (Cy_Flash_ProgramRow(row_address, (const uint32_t *)prog_buf) != CY_FLASH_DRV_SUCCESS) {
64+
status = -1;
65+
}
66+
address += chunk_size;
67+
size -= chunk_size;
5268
}
5369

70+
Cy_SysLib_ClearFlashCacheAndBuffer();
5471
return status;
5572
}
5673

@@ -67,7 +84,7 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
6784
uint32_t flash_get_page_size(const flash_t *obj)
6885
{
6986
(void)(obj);
70-
return CY_FLASH_SIZEOF_ROW;
87+
return CY_FLASH_EFFECTIVE_PAGE_SIZE;
7188
}
7289

7390
uint32_t flash_get_start_address(const flash_t *obj)

targets/TARGET_Cypress/TARGET_PSOC6/psoc6pdl/drivers/include/cy_flash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ extern "C" {
377377

378378
/** Flash row size */
379379
#define CY_FLASH_SIZEOF_ROW (CPUSS_FLASHC_PA_SIZE * 4u)
380+
/** Flash effective page size */
381+
#define CY_FLASH_EFFECTIVE_PAGE_SIZE 32
380382
/** Long words flash row size */
381383
#define CY_FLASH_SIZEOF_ROW_LONG_UNITS (CY_FLASH_SIZEOF_ROW / sizeof(uint32_t))
382384

targets/TARGET_Cypress/TARGET_PSOC6_FUTURE/device/drivers/peripheral/flash/cy_flash.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,8 @@ extern "C" {
332332

333333
/** Flash row size */
334334
#define CY_FLASH_SIZEOF_ROW (CPUSS_FLASHC_PA_SIZE * 4u)
335+
/** Flash effective page size */
336+
#define CY_FLASH_EFFECTIVE_PAGE_SIZE 32
335337
/** Number of flash rows */
336338
#define CY_FLASH_NUMBER_ROWS (CY_FLASH_SIZE / CY_FLASH_SIZEOF_ROW)
337339
/** Long words flash row size */

targets/TARGET_Cypress/TARGET_PSOC6_FUTURE/flash_api.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,27 @@ int32_t flash_program_page(flash_t *obj, uint32_t address, const uint8_t *data,
4747
{
4848
(void)(obj);
4949
int32_t status = 0;
50-
if (Cy_Flash_ProgramRow(address, (const uint32_t *)data) != CY_FLASH_DRV_SUCCESS) {
51-
status = -1;
50+
static uint8_t prog_buf[CY_FLASH_SIZEOF_ROW];
51+
while (size) {
52+
uint32_t offset = address % CY_FLASH_SIZEOF_ROW;
53+
uint32_t chunk_size;
54+
if (offset + size > CY_FLASH_SIZEOF_ROW) {
55+
chunk_size = CY_FLASH_SIZEOF_ROW - offset;
56+
} else {
57+
chunk_size = size;
58+
}
59+
uint32_t row_address = address / CY_FLASH_SIZEOF_ROW * CY_FLASH_SIZEOF_ROW;
60+
memcpy(prog_buf, (const void *)row_address, CY_FLASH_SIZEOF_ROW);
61+
memcpy(prog_buf + offset, data, chunk_size);
62+
63+
if (Cy_Flash_ProgramRow(row_address, (const uint32_t *)prog_buf) != CY_FLASH_DRV_SUCCESS) {
64+
status = -1;
65+
}
66+
address += chunk_size;
67+
size -= chunk_size;
5268
}
5369

70+
Cy_SysLib_ClearFlashCacheAndBuffer();
5471
return status;
5572
}
5673

@@ -67,7 +84,7 @@ uint32_t flash_get_sector_size(const flash_t *obj, uint32_t address)
6784
uint32_t flash_get_page_size(const flash_t *obj)
6885
{
6986
(void)(obj);
70-
return CY_FLASH_SIZEOF_ROW;
87+
return CY_FLASH_EFFECTIVE_PAGE_SIZE;
7188
}
7289

7390
uint32_t flash_get_start_address(const flash_t *obj)

0 commit comments

Comments
 (0)