Skip to content

Commit

Permalink
periph/flashpage: extend API
Browse files Browse the repository at this point in the history
  • Loading branch information
Ollrogge committed Oct 19, 2021
1 parent 1ba0bdf commit 72d4701
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
10 changes: 10 additions & 0 deletions drivers/include/periph/flashpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ unsigned flashpage_page(const void *addr);
*/
void flashpage_erase(unsigned page);

/**
* @brief Get number of first free flashpage
*/
unsigned flashpage_first_free(void);

/**
* @brief Get number of last free flashpage
*/
unsigned flashpage_last_free(void);

/**
* @brief Write the given page with the given data
*
Expand Down
41 changes: 19 additions & 22 deletions tests/periph_flashpage/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,6 @@

#define LINE_LEN (16)

/* For MSP430 cpu's the last page holds the interrupt vector, although the api
should not limit erasing that page, we don't want to break when testing */
#if defined(CPU_CC430) || defined(CPU_MSP430FXYZ)
#define TEST_LAST_AVAILABLE_PAGE (FLASHPAGE_NUMOF - 2)
#else
#define TEST_LAST_AVAILABLE_PAGE (FLASHPAGE_NUMOF - 1)
#endif

/* When writing raw bytes on flash, data must be correctly aligned. */
#define ALIGNMENT_ATTR __attribute__((aligned(FLASHPAGE_WRITE_BLOCK_ALIGNMENT)))

Expand Down Expand Up @@ -115,24 +107,27 @@ static int cmd_info(int argc, char **argv)
(void)argc;
(void)argv;

printf("Flash start addr:\t0x%08x\n", (int)CPU_FLASH_BASE);
printf("Flash start addr:\t\t0x%08x\n", (int)CPU_FLASH_BASE);
#ifdef FLASHPAGE_SIZE
printf("Page size:\t\t%i\n", (int)FLASHPAGE_SIZE);
printf("Page size:\t\t\t%i\n", (int)FLASHPAGE_SIZE);
#else
puts("Page size:\t\tvariable");
puts("Page size:\t\t\tvariable");
#endif
printf("Number of pages:\t%i\n", (int)FLASHPAGE_NUMOF);
printf("Number of pages:\t\t%i\n", (int)FLASHPAGE_NUMOF);

#ifdef FLASHPAGE_RWWEE_NUMOF
printf("RWWEE Flash start addr:\t0x%08x\n", (int)CPU_FLASH_RWWEE_BASE);
printf("RWWEE Number of pages:\t%i\n", (int)FLASHPAGE_RWWEE_NUMOF);
printf("RWWEE Flash start addr:\t\t0x%08x\n", (int)CPU_FLASH_RWWEE_BASE);
printf("RWWEE Number of pages:\t\t%i\n", (int)FLASHPAGE_RWWEE_NUMOF);
#endif

#ifdef NVMCTRL_USER
printf("AUX page size:\t%i\n", FLASH_USER_PAGE_AUX_SIZE + sizeof(nvm_user_page_t));
printf(" user area:\t%i\n", FLASH_USER_PAGE_AUX_SIZE);
printf("AUX page size:\t\t%i\n", FLASH_USER_PAGE_AUX_SIZE + sizeof(nvm_user_page_t));
printf(" user area:\t\t%i\n", FLASH_USER_PAGE_AUX_SIZE);
#endif

printf("Number of first free page: \t%u \n", flashpage_first_free());
printf("Number of last free page: \t%u \n", flashpage_last_free());

return 0;
}

Expand Down Expand Up @@ -349,6 +344,7 @@ static int cmd_test_last(int argc, char **argv)
(void) argc;
(void) argv;
char fill = 'a';
unsigned last_free = flashpage_last_free();

for (unsigned i = 0; i < sizeof(page_mem); i++) {
page_mem[i] = (uint8_t)fill++;
Expand All @@ -357,9 +353,9 @@ static int cmd_test_last(int argc, char **argv)
}
}
#if defined(CPU_CC430) || defined(CPU_MSP430FXYZ)
printf("The last page holds the ISR vector, so test page %d\n", TEST_LAST_AVAILABLE_PAGE);
printf("The last page holds the ISR vector, so test page %u\n", last_free);
#endif
if (flashpage_write_and_verify(TEST_LAST_AVAILABLE_PAGE, page_mem) != FLASHPAGE_OK) {
if (flashpage_write_and_verify(last_free, page_mem) != FLASHPAGE_OK) {
puts("error verifying the content of last page");
return 1;
}
Expand All @@ -380,22 +376,23 @@ static int cmd_test_last_raw(int argc, char **argv)
{
(void) argc;
(void) argv;
unsigned last_free = flashpage_last_free();

memset(raw_buf, 0, sizeof(raw_buf));

/* try to align */
memcpy(raw_buf, "test12344321tset", 16);
#if defined(CPU_CC430) || defined(CPU_MSP430FXYZ)
printf("The last page holds the ISR vector, so test page %d\n", TEST_LAST_AVAILABLE_PAGE);
printf("The last page holds the ISR vector, so test page %u\n", last_free);
#endif

/* erase the page first */
flashpage_erase(TEST_LAST_AVAILABLE_PAGE);
flashpage_erase(last_free);

flashpage_write(flashpage_addr(TEST_LAST_AVAILABLE_PAGE), raw_buf, sizeof(raw_buf));
flashpage_write(flashpage_addr(last_free), raw_buf, sizeof(raw_buf));

/* verify that previous write_raw effectively wrote the desired data */
if (memcmp(flashpage_addr(TEST_LAST_AVAILABLE_PAGE), raw_buf, strlen(raw_buf)) != 0) {
if (memcmp(flashpage_addr(last_free), raw_buf, strlen(raw_buf)) != 0) {
puts("error verifying the content of last page");
return 1;
}
Expand Down

0 comments on commit 72d4701

Please sign in to comment.