Initial Release
This commit is contained in:
14
flash/program/CMakeLists.txt
Normal file
14
flash/program/CMakeLists.txt
Normal file
@@ -0,0 +1,14 @@
|
||||
add_executable(flash_program
|
||||
flash_program.c
|
||||
)
|
||||
|
||||
target_link_libraries(flash_program
|
||||
pico_stdlib
|
||||
hardware_flash
|
||||
)
|
||||
|
||||
# create map/bin/hex file etc.
|
||||
pico_add_extra_outputs(flash_program)
|
||||
|
||||
# add url via pico_set_program_url
|
||||
example_auto_set_url(flash_program)
|
||||
58
flash/program/flash_program.c
Normal file
58
flash/program/flash_program.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-3-Clause
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "pico/stdlib.h"
|
||||
#include "hardware/flash.h"
|
||||
|
||||
// We're going to erase and reprogram a region 256k from the start of flash.
|
||||
// Once done, we can access this at XIP_BASE + 256k.
|
||||
#define FLASH_TARGET_OFFSET (256 * 1024)
|
||||
|
||||
const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
|
||||
|
||||
void print_buf(const uint8_t *buf, size_t len) {
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
printf("%02x", buf[i]);
|
||||
if (i % 16 == 15)
|
||||
printf("\n");
|
||||
else
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
stdio_init_all();
|
||||
uint8_t random_data[FLASH_PAGE_SIZE];
|
||||
for (int i = 0; i < FLASH_PAGE_SIZE; ++i)
|
||||
random_data[i] = rand() >> 16;
|
||||
|
||||
printf("Generated random data:\n");
|
||||
print_buf(random_data, FLASH_PAGE_SIZE);
|
||||
|
||||
// Note that a whole number of sectors must be erased at a time.
|
||||
printf("\nErasing target region...\n");
|
||||
flash_range_erase(FLASH_TARGET_OFFSET, FLASH_SECTOR_SIZE);
|
||||
printf("Done. Read back target region:\n");
|
||||
print_buf(flash_target_contents, FLASH_PAGE_SIZE);
|
||||
|
||||
printf("\nProgramming target region...\n");
|
||||
flash_range_program(FLASH_TARGET_OFFSET, random_data, FLASH_PAGE_SIZE);
|
||||
printf("Done. Read back target region:\n");
|
||||
print_buf(flash_target_contents, FLASH_PAGE_SIZE);
|
||||
|
||||
bool mismatch = false;
|
||||
for (int i = 0; i < FLASH_PAGE_SIZE; ++i) {
|
||||
if (random_data[i] != flash_target_contents[i])
|
||||
mismatch = true;
|
||||
}
|
||||
if (mismatch)
|
||||
printf("Programming failed!\n");
|
||||
else
|
||||
printf("Programming successful!\n");
|
||||
}
|
||||
Reference in New Issue
Block a user