29 #if defined(PIOS_INCLUDE_FLASH_INTERNAL)
31 #include "stm32f30x_flash.h"
37 #define STM32F30X_FLASH_SECTOR_SIZE 2048
39 enum pios_internal_flash_dev_magic {
40 PIOS_INTERNAL_FLASH_DEV_MAGIC = 0xae82c65d,
43 struct pios_internal_flash_dev {
44 enum pios_internal_flash_dev_magic
magic;
51 static bool PIOS_Flash_Internal_Validate(
struct pios_internal_flash_dev *flash_dev) {
52 return (flash_dev && (flash_dev->magic == PIOS_INTERNAL_FLASH_DEV_MAGIC));
55 static struct pios_internal_flash_dev *PIOS_Flash_Internal_alloc(
void)
57 struct pios_internal_flash_dev *flash_dev;
59 flash_dev = (
struct pios_internal_flash_dev *)
PIOS_malloc(
sizeof(*flash_dev));
60 if (!flash_dev)
return (NULL);
62 flash_dev->magic = PIOS_INTERNAL_FLASH_DEV_MAGIC;
73 struct pios_internal_flash_dev *flash_dev;
75 flash_dev = PIOS_Flash_Internal_alloc();
76 if (flash_dev == NULL)
87 NVIC_InitTypeDef intr = {
88 .NVIC_IRQChannel = FLASH_IRQn,
90 .NVIC_IRQChannelSubPriority = 0,
91 .NVIC_IRQChannelCmd = ENABLE,
95 FLASH_ITConfig(FLASH_IT_ERR, ENABLE);
97 *chip_id = (uintptr_t) flash_dev;
109 static int32_t PIOS_Flash_Internal_StartTransaction(uintptr_t chip_id)
111 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
113 if (!PIOS_Flash_Internal_Validate(flash_dev))
125 static int32_t PIOS_Flash_Internal_EndTransaction(uintptr_t chip_id)
127 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
129 if (!PIOS_Flash_Internal_Validate(flash_dev))
141 static int32_t PIOS_Flash_Internal_EraseSector(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
143 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
145 if (!PIOS_Flash_Internal_Validate(flash_dev))
148 #if defined(PIOS_INCLUDE_WDG)
152 FLASH_Status ret = FLASH_ErasePage(FLASH_BASE + chip_offset);
153 if (ret != FLASH_COMPLETE)
159 static int32_t PIOS_Flash_Internal_ReadData(uintptr_t chip_id, uint32_t chip_offset, uint8_t *
data, uint16_t len)
163 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
165 if (!PIOS_Flash_Internal_Validate(flash_dev))
169 memcpy(data, (
void *)(FLASH_BASE + chip_offset), len);
174 static int32_t PIOS_Flash_Internal_WriteData(uintptr_t chip_id, uint32_t chip_offset,
const uint8_t *data, uint16_t len)
179 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
181 if (!PIOS_Flash_Internal_Validate(flash_dev))
185 for (uint16_t
i = 0;
i < (len & ~1);
i += 2) {
190 if (PIOS_Flash_Internal_ReadData(chip_id, chip_offset + i, temp,
sizeof(temp)) != 0)
193 if (temp[0] == data[i] && temp[1] == data[i + 1])
197 status = FLASH_ProgramHalfWord(FLASH_BASE + chip_offset + i, data[i + 1] << 8 | data[i]);
201 if ((len & 1) != 0) {
203 status = FLASH_ProgramHalfWord(FLASH_BASE + chip_offset + len - 1, 0xff << 8 | data[len - 1]);
210 static void * PIOS_Flash_Internal_GetPointer(uintptr_t chip_id, uint32_t chip_offset)
212 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
214 PIOS_Assert(PIOS_Flash_Internal_Validate(flash_dev));
216 return (
void *)(FLASH_BASE + chip_offset);
222 .end_transaction = PIOS_Flash_Internal_EndTransaction,
223 .erase_sector = PIOS_Flash_Internal_EraseSector,
224 .write_data = PIOS_Flash_Internal_WriteData,
225 .read_data = PIOS_Flash_Internal_ReadData,
226 .get_pointer = PIOS_Flash_Internal_GetPointer,
int32_t PIOS_Flash_Internal_Init(uintptr_t *flash_id, const struct pios_flash_internal_cfg *cfg)
#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.
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