Skip to content

Instantly share code, notes, and snippets.

@jkiv
Created July 7, 2020 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkiv/9518161d4e2b25c852ab2787a80d6264 to your computer and use it in GitHub Desktop.
Save jkiv/9518161d4e2b25c852ab2787a80d6264 to your computer and use it in GitHub Desktop.
#include "sam.h"
#include "system.h"
#include "uart.h"
#include "flash.h"
#include "bitstream.h"
#include "print.h"
#define FLASH_DATA_START (0x00020000UL)
#define FLASH_DATA_END (0x00040000UL)
int main_nvm_test()
{
// Set up system
system_init();
// Set up UART
uart_init(9600);
// Enable the UART
uart_enable_interrupts();
uart_enable();
uint32_t* nvm_val = (uint32_t*) (0x00020000UL);
print_cstring(uart_write, "main_nvm_test.c\r\n");
print_cstring(uart_write, "\tInitial NVM value: ");
print_hex32(uart_write, *nvm_val);
print_cstring(uart_write, "\r\n");
// Clear the NVM row
// TODO
// Enable manual writes to NVM
NVMCTRL->CTRLB.bit.MANW = 1;
// Print ADDR
print_cstring(uart_write, "\tADDR before access: ");
print_hex32(uart_write, NVMCTRL->ADDR.reg);
print_cstring(uart_write, "\r\n");
// Write a special value to NVM
*nvm_val = 0xDEADBEEF;
// Print ADDR
print_cstring(uart_write, "\tADDR before WP: ");
print_hex32(uart_write, NVMCTRL->ADDR.reg);
print_cstring(uart_write, "\r\n");
// Commit to NVM (manual write)
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY |
NVMCTRL_CTRLA_CMD_WP;
while(NVMCTRL->INTFLAG.bit.READY == 0);
if (NVMCTRL->INTFLAG.bit.ERROR == 1) {
NVMCTRL->INTFLAG.bit.ERROR = 1;
print_cstring(uart_write, "\tINTFLAG.ERROR set.\r\n");
}
// Print ADDR
print_cstring(uart_write, "\tADDR after WP: ");
print_hex32(uart_write, NVMCTRL->ADDR.reg);
print_cstring(uart_write, "\r\n");
print_cstring(uart_write, "\tNVM value after WP: ");
print_hex32(uart_write, *nvm_val);
print_cstring(uart_write, "\r\n");
// Clear NVM...
// TODO how?
// 1. Update ADDR by accessing memory
*nvm_val = 0x00000000;
print_cstring(uart_write, "\tADDR before ER: ");
print_hex32(uart_write, NVMCTRL->ADDR.reg);
print_cstring(uart_write, "\r\n");
// 2. Clear whatever row ADDR points to...
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY |
NVMCTRL_CTRLA_CMD_ER;
print_cstring(uart_write, "\tNVM value after ER: ");
print_hex32(uart_write, *nvm_val);
print_cstring(uart_write, "\r\n");
// Try another write...
*nvm_val = 0xDEFEC8ED;
// Commit to NVM (manual write)
NVMCTRL->CTRLA.reg = NVMCTRL_CTRLA_CMDEX_KEY |
NVMCTRL_CTRLA_CMD_WP;
while(NVMCTRL->INTFLAG.bit.READY == 0);
if (NVMCTRL->INTFLAG.bit.ERROR == 1) {
NVMCTRL->INTFLAG.bit.ERROR = 1;
print_cstring(uart_write, "\tINTFLAG.ERROR set.\r\n");
}
print_cstring(uart_write, "\tNVM value after second CMDEX: ");
print_hex32(uart_write, *nvm_val);
print_cstring(uart_write, "\r\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment