whiskers 0.2.0
RP2350 BadUSB Tool
Loading...
Searching...
No Matches
flash.h
Go to the documentation of this file.
1/* Copyright (c) 2026 Zane A. Maples
2 Licensed under the MIT license. See LICENSE in the main repository for more information. */
3#ifndef W_HAL_FLASH_H
4#define W_HAL_FLASH_H
5
6/* "Hello fbom flach memobi!" */
7
35#include "shared.h"
36
37/* Some extremely important info to anyone that decides they want to edit this:
38 Before ANY flash operation (erase or program) interrupts MUST be saved and the other core MUST not be using ANY flash
39 operations! If they are we could have serious errors in reading and writing flash putting the device in an
40 UNRECOVERABLE state as instructions are also read from flash.
41
42 All of the functions below are setup to be *safe* to use! If you are to access flash AT ALL for WHATEVER REASON please
43 use the functions below! (or you better know what you're doing!)
44
45 Also please minimize the WRITES to the flash device as much as possible, as there is a limited amount of lifetime to
46 these chips. Any use of flash should be used primarily as a way to store often non-changing system information
47 (ie. configuration) that also does not need to be secure. For secure data storage or one-time-use keys, please see the
48 Raspberry Pi RP2350 datasheet for more info on OTP and flash encryption!
49 */
50
51/* Note: FLASH_PAGE_SIZE & FLASH_SECTOR_SIZE are macros provided by pico-sdk. When switching boards they will need to be re-defined with a wrapper */
52
53#ifdef W_HAL_RP2350
54/* I can't be bothered to define these myself so we just gonna include the mf */
55#include <hardware/flash.h>
56#define WHISKERS_FLASH_SIZE PICO_FLASH_SIZE_BYTES
57#else
58#define WHISKERS_FLASH_SIZE 0
59#define FLASH_PAGE_SIZE 0
60#define FLASH_SECTOR_SIZE 0
61#endif
62
67
72
77
78/* Basic helper functions */
79/* Note: all page/sector indexing goes reverse from the bottom of the flash memory stack! */
87HAL_ERROR read_page(uint32_t page, char buf[FLASH_PAGE_SIZE]);
95HAL_ERROR read_sector(uint32_t sector, char buf[FLASH_SECTOR_SIZE]);
103HAL_ERROR check_memory_integrity_sector(uint32_t sector, char checkagainst[FLASH_SECTOR_SIZE]);
111HAL_ERROR check_memory_integrity_page(uint32_t page, char checkagainst[FLASH_PAGE_SIZE]);
121HAL_ERROR write_page(uint32_t page, char buf[FLASH_PAGE_SIZE]);
131HAL_ERROR write_sector(uint32_t sector, char buf[FLASH_SECTOR_SIZE]);
138HAL_ERROR erase_page(uint32_t page);
145HAL_ERROR erase_sector(uint32_t sector);
146#endif
HAL_ERROR check_memory_integrity_page(uint32_t page, char checkagainst[FLASH_PAGE_SIZE])
Checks a page against a known correct value to see if that page is still good to use.
#define FLASH_PAGE_SIZE
This is 256 bytes for board versions(s) 1.0.
Definition flash.h:59
HAL_ERROR read_sector(uint32_t sector, char buf[FLASH_SECTOR_SIZE])
Reads a sector from memory.
HAL_ERROR read_page(uint32_t page, char buf[FLASH_PAGE_SIZE])
Reads a page from memory.
HAL_ERROR write_sector(uint32_t sector, char buf[FLASH_SECTOR_SIZE])
Writes a sector-sized amount of data.
HAL_ERROR erase_page(uint32_t page)
Erases a page.
HAL_ERROR erase_sector(uint32_t sector)
Erases a sector.
HAL_ERROR check_memory_integrity_sector(uint32_t sector, char checkagainst[FLASH_SECTOR_SIZE])
Checks a sector against a known correct value to see if that sector is still good to use.
#define FLASH_SECTOR_SIZE
This is 4Kb (4096b) for board version(s) 1.0.
Definition flash.h:60
HAL_ERROR write_page(uint32_t page, char buf[FLASH_PAGE_SIZE])
Writes a page-sized amount of data.
Shared structs, error codes, and functions for all HAL files.
uint8_t HAL_ERROR
This type represents a HAL_* error. See the defines in the file to read up on them.
Definition shared.h:31