1 /* 2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #ifndef _HARDWARE_FLASH_H 8 #define _HARDWARE_FLASH_H 9 10 #include "pico.h" 11 12 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_FLASH, Enable/disable assertions in the flash module, type=bool, default=0, group=hardware_flash 13 #ifndef PARAM_ASSERTIONS_ENABLED_FLASH 14 #define PARAM_ASSERTIONS_ENABLED_FLASH 0 15 #endif 16 17 #define FLASH_PAGE_SIZE (1u << 8) 18 #define FLASH_SECTOR_SIZE (1u << 12) 19 #define FLASH_BLOCK_SIZE (1u << 16) 20 21 #define FLASH_UNIQUE_ID_SIZE_BYTES 8 22 23 /** \file flash.h 24 * \defgroup hardware_flash hardware_flash 25 * 26 * Low level flash programming and erase API 27 * 28 * Note these functions are *unsafe* if you have two cores concurrently 29 * executing from flash. In this case you must perform your own 30 * synchronisation to make sure no XIP accesses take place during flash 31 * programming. 32 * 33 * Likewise they are *unsafe* if you have interrupt handlers or an interrupt 34 * vector table in flash, so you must disable interrupts before calling in 35 * this case. 36 * 37 * If PICO_NO_FLASH=1 is not defined (i.e. if the program is built to run from 38 * flash) then these functions will make a static copy of the second stage 39 * bootloader in SRAM, and use this to reenter execute-in-place mode after 40 * programming or erasing flash, so that they can safely be called from 41 * flash-resident code. 42 * 43 * \subsection flash_example Example 44 * \include flash_program.c 45 */ 46 47 48 /*! \brief Erase areas of flash 49 * \ingroup hardware_flash 50 * 51 * \param flash_offs Offset into flash, in bytes, to start the erase. Must be aligned to a 4096-byte flash sector. 52 * \param count Number of bytes to be erased. Must be a multiple of 4096 bytes (one sector). 53 */ 54 void flash_range_erase(uint32_t flash_offs, size_t count); 55 56 /*! \brief Program flash 57 * \ingroup hardware_flash 58 * 59 * \param flash_offs Flash address of the first byte to be programmed. Must be aligned to a 256-byte flash page. 60 * \param data Pointer to the data to program into flash 61 * \param count Number of bytes to program. Must be a multiple of 256 bytes (one page). 62 */ 63 64 void flash_range_program(uint32_t flash_offs, const uint8_t *data, size_t count); 65 66 /*! \brief Get flash unique 64 bit identifier 67 * \ingroup hardware_flash 68 * 69 * Use a standard 4Bh RUID instruction to retrieve the 64 bit unique 70 * identifier from a flash device attached to the QSPI interface. Since there 71 * is a 1:1 association between the MCU and this flash, this also serves as a 72 * unique identifier for the board. 73 * 74 * \param id_out Pointer to an 8-byte buffer to which the ID will be written 75 */ 76 void flash_get_unique_id(uint8_t *id_out); 77 78 #endif 79