29 #ifdef PIOS_INCLUDE_FLASH_INTERNAL
31 #include "stm32f10x_flash.h"
37 enum pios_internal_flash_dev_magic {
38 PIOS_INTERNAL_FLASH_DEV_MAGIC = 0xae82c65d,
41 struct pios_internal_flash_dev {
42 enum pios_internal_flash_dev_magic
magic;
49 static bool PIOS_Flash_Internal_Validate(
struct pios_internal_flash_dev *flash_dev) {
50 return (flash_dev && (flash_dev->magic == PIOS_INTERNAL_FLASH_DEV_MAGIC));
53 static struct pios_internal_flash_dev *PIOS_Flash_Internal_alloc(
void)
55 struct pios_internal_flash_dev *flash_dev;
57 flash_dev = (
struct pios_internal_flash_dev *)
PIOS_malloc(
sizeof(*flash_dev));
58 if (!flash_dev)
return (NULL);
60 flash_dev->magic = PIOS_INTERNAL_FLASH_DEV_MAGIC;
72 struct pios_internal_flash_dev *flash_dev;
74 flash_dev = PIOS_Flash_Internal_alloc();
75 if (flash_dev == NULL)
86 NVIC_InitTypeDef intr = {
87 .NVIC_IRQChannel = FLASH_IRQn,
89 .NVIC_IRQChannelSubPriority = 0,
90 .NVIC_IRQChannelCmd = ENABLE,
94 FLASH_ITConfig(FLASH_IT_ERROR, ENABLE);
96 *chip_id = (uintptr_t) flash_dev;
108 static int32_t PIOS_Flash_Internal_StartTransaction(uintptr_t chip_id)
110 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
112 if (!PIOS_Flash_Internal_Validate(flash_dev))
124 static int32_t PIOS_Flash_Internal_EndTransaction(uintptr_t chip_id)
126 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
128 if (!PIOS_Flash_Internal_Validate(flash_dev))
140 static int32_t PIOS_Flash_Internal_EraseSector(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
142 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
144 if (!PIOS_Flash_Internal_Validate(flash_dev))
147 #if defined(PIOS_INCLUDE_WDG)
151 FLASH_Status ret = FLASH_ErasePage(FLASH_BASE + chip_offset);
152 if (ret != FLASH_COMPLETE)
158 static int32_t PIOS_Flash_Internal_ReadData(uintptr_t chip_id, uint32_t chip_offset, uint8_t *
data, uint16_t len)
162 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
164 if (!PIOS_Flash_Internal_Validate(flash_dev))
168 memcpy(data, (
void *)(FLASH_BASE + chip_offset), len);
173 static int32_t PIOS_Flash_Internal_WriteData(uintptr_t chip_id, uint32_t chip_offset,
const uint8_t *data, uint16_t len)
177 struct pios_internal_flash_dev *flash_dev = (
struct pios_internal_flash_dev *)chip_id;
179 if (!PIOS_Flash_Internal_Validate(flash_dev))
183 uint16_t numberOfhWords = len / 2;
186 for ( x = 0; x < numberOfhWords; ++x) {
188 uint32_t hword_data = (data[offset + 1] << 8) | data[offset];
190 if (hword_data != *(uint16_t *)(FLASH_BASE + chip_offset +
offset)) {
191 status = FLASH_ProgramHalfWord(FLASH_BASE + chip_offset + offset, hword_data);
193 status = FLASH_COMPLETE;
198 uint16_t mod = len % 2;
200 uint32_t offset = 2 * x;
201 uint32_t hword_data = 0xFF00 | data[
offset];
202 if (hword_data != *(uint16_t *)(FLASH_BASE + chip_offset + offset)) {
203 status = FLASH_ProgramHalfWord(FLASH_BASE + chip_offset + offset, hword_data);
205 status = FLASH_COMPLETE;
216 .end_transaction = PIOS_Flash_Internal_EndTransaction,
217 .erase_sector = PIOS_Flash_Internal_EraseSector,
218 .write_data = PIOS_Flash_Internal_WriteData,
219 .read_data = PIOS_Flash_Internal_ReadData,
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