31 #if defined(PIOS_INCLUDE_FLASH_INTERNAL)
33 #include "stm32f4xx_flash.h"
44 static const uint16_t sector_to_st_sector_map[] = {
55 [10] = FLASH_Sector_10,
56 [11] = FLASH_Sector_11,
59 enum pios_internal_flash_dev_magic {
60 PIOS_INTERNAL_FLASH_DEV_MAGIC = 0x33445902,
63 struct pios_internal_flash_dev {
64 enum pios_internal_flash_dev_magic
magic;
71 static bool PIOS_Flash_Internal_Validate(
struct pios_internal_flash_dev *flash_dev) {
72 return (flash_dev && (flash_dev->magic == PIOS_INTERNAL_FLASH_DEV_MAGIC));
75 static struct pios_internal_flash_dev *PIOS_Flash_Internal_alloc(
void)
77 struct pios_internal_flash_dev *flash_dev;
79 flash_dev = (
struct pios_internal_flash_dev *)
PIOS_malloc(
sizeof(*flash_dev));
80 if (!flash_dev)
return (NULL);
82 flash_dev->magic = PIOS_INTERNAL_FLASH_DEV_MAGIC;
93 struct pios_internal_flash_dev *flash_dev;
95 flash_dev = PIOS_Flash_Internal_alloc();
96 if (flash_dev == NULL)
101 flash_dev->cfg =
cfg;
107 NVIC_InitTypeDef intr = {
108 .NVIC_IRQChannel = FLASH_IRQn,
110 .NVIC_IRQChannelSubPriority = 0,
111 .NVIC_IRQChannelCmd = ENABLE,
115 FLASH_ITConfig(FLASH_IT_ERR, ENABLE);
117 *chip_id = (uintptr_t) flash_dev;
129 static int32_t PIOS_Flash_Internal_StartTransaction(uintptr_t chip_id)
131 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
133 if (!PIOS_Flash_Internal_Validate(flash_dev))
144 static int32_t PIOS_Flash_Internal_EndTransaction(uintptr_t chip_id)
146 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
148 if (!PIOS_Flash_Internal_Validate(flash_dev))
160 int32_t PIOS_Flash_Internal_EraseSector_FromRam(uint16_t st_sector)
163 static int32_t PIOS_Flash_Internal_EraseSector(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
165 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
167 if (!PIOS_Flash_Internal_Validate(flash_dev))
170 #if defined(PIOS_INCLUDE_WDG)
175 if (chip_sector >=
NELEMENTS(sector_to_st_sector_map))
178 uint32_t st_sector = sector_to_st_sector_map[chip_sector];
182 int32_t ret = PIOS_Flash_Internal_EraseSector_FromRam(st_sector);
190 if (FLASH_GetStatus() != FLASH_COMPLETE) {
197 int32_t PIOS_Flash_Internal_EraseSector_FromRam(uint16_t st_sector)
203 #define KR_KEY_RELOAD ((uint16_t)0xAAAA)
206 while ((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) {
207 IWDG->KR = KR_KEY_RELOAD;
210 uint32_t cr = FLASH->CR;
213 cr |= FLASH_PSIZE_WORD;
215 #define SECTOR_MASK ((uint32_t)0xFFFFFF07)
218 cr |= FLASH_CR_SER | st_sector;
223 FLASH->CR = cr | FLASH_CR_STRT;
225 while ((FLASH->SR & FLASH_FLAG_BSY) == FLASH_FLAG_BSY) {
226 IWDG->KR = KR_KEY_RELOAD;
229 FLASH->CR &= (~FLASH_CR_SER) & SECTOR_MASK;
234 static int32_t PIOS_Flash_Internal_WriteData(uintptr_t chip_id, uint32_t chip_offset,
const uint8_t *
data, uint16_t len)
238 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
240 if (!PIOS_Flash_Internal_Validate(flash_dev))
244 for (uint16_t
i = 0;
i < len;
i++) {
250 status = FLASH_ProgramByte(FLASH_BASE + chip_offset +
i, data[
i]);
257 static int32_t PIOS_Flash_Internal_ReadData(uintptr_t chip_id, uint32_t chip_offset, uint8_t *data, uint16_t len)
261 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
263 if (!PIOS_Flash_Internal_Validate(flash_dev))
267 memcpy(data, (
void *)(FLASH_BASE + chip_offset), len);
272 static void * PIOS_Flash_Internal_GetPointer(uintptr_t chip_id, uint32_t chip_offset)
274 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
276 PIOS_Assert(PIOS_Flash_Internal_Validate(flash_dev));
278 return (
void *)(FLASH_BASE + chip_offset);
284 .end_transaction = PIOS_Flash_Internal_EndTransaction,
285 .erase_sector = PIOS_Flash_Internal_EraseSector,
286 .write_data = PIOS_Flash_Internal_WriteData,
287 .read_data = PIOS_Flash_Internal_ReadData,
288 .get_pointer = PIOS_Flash_Internal_GetPointer,
int32_t PIOS_Flash_Internal_Init(uintptr_t *flash_id, const struct pios_flash_internal_cfg *cfg)
int32_t PIOS_IRQ_Enable(void)
#define PIOS_SEMAPHORE_TIMEOUT_MAX
Main PiOS header to include all the compiled in PiOS options.
void * PIOS_malloc(size_t size)
#define PIOS_IRQ_PRIO_HIGHEST
uint8_t data[XFER_BYTES_PER_PACKET]
void PIOS_WDG_Clear(void)
Clear the watchdog timer.
static struct flyingpicmd_cfg_fa cfg
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
bool PIOS_Semaphore_Give(struct pios_semaphore *sema)
Gives binary semaphore.
int32_t PIOS_IRQ_Disable(void)
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
#define PIOS_Assert(test)
int32_t(* start_transaction)(uintptr_t chip_id)
const struct pios_flash_driver pios_internal_flash_driver