1 #include <stdlib.h> /* abort */
2 #include <stdio.h> /* fopen/fread/fwrite/fseek */
3 #include <assert.h> /* assert */
4 #include <string.h> /* memset */
7 #include "pios_flash_ut_priv.h"
10 FLASH_UT_MAGIC
= 0x321dabc1,
14 enum flash_ut_magic magic
;
15 const struct pios_flash_ut_cfg
*cfg
;
16 bool transaction_in_progress
;
20 static struct flash_ut_dev
*PIOS_Flash_UT_Alloc(void)
22 struct flash_ut_dev
*flash_dev
= malloc(sizeof(struct flash_ut_dev
));
24 flash_dev
->magic
= FLASH_UT_MAGIC
;
29 int32_t PIOS_Flash_UT_Init(uintptr_t *flash_id
, const struct pios_flash_ut_cfg
*cfg
)
34 assert(cfg
->size_of_flash
);
35 assert(cfg
->size_of_sector
);
36 assert((cfg
->size_of_flash
% cfg
->size_of_sector
) == 0);
38 struct flash_ut_dev
*flash_dev
= PIOS_Flash_UT_Alloc();
42 flash_dev
->transaction_in_progress
= false;
44 flash_dev
->flash_file
= fopen(FLASH_IMAGE_FILE
, "rb+");
45 if (flash_dev
->flash_file
== NULL
) {
49 if (fseek(flash_dev
->flash_file
, flash_dev
->cfg
->size_of_flash
, SEEK_SET
) != 0) {
53 *flash_id
= (uintptr_t)flash_dev
;
58 int32_t PIOS_Flash_UT_Destroy(uintptr_t flash_id
)
62 struct flash_ut_dev
*flash_dev
= (void *)flash_id
;
64 if (flash_dev
->flash_file
== NULL
) {
68 fclose(flash_dev
->flash_file
);
72 unlink(FLASH_IMAGE_FILE
);
78 /**********************************
80 * Provide a PIOS flash driver API
82 *********************************/
83 #include "pios_flash.h"
85 static int32_t PIOS_Flash_UT_StartTransaction(uintptr_t flash_id
)
87 struct flash_ut_dev
*flash_dev
= (struct flash_ut_dev
*)flash_id
;
89 assert(!flash_dev
->transaction_in_progress
);
91 flash_dev
->transaction_in_progress
= true;
96 static int32_t PIOS_Flash_UT_EndTransaction(uintptr_t flash_id
)
98 struct flash_ut_dev
*flash_dev
= (struct flash_ut_dev
*)flash_id
;
100 assert(flash_dev
->transaction_in_progress
);
102 flash_dev
->transaction_in_progress
= false;
107 static int32_t PIOS_Flash_UT_EraseSector(uintptr_t flash_id
, uint32_t addr
)
109 struct flash_ut_dev
*flash_dev
= (struct flash_ut_dev
*)flash_id
;
111 assert(flash_dev
->transaction_in_progress
);
113 if (fseek(flash_dev
->flash_file
, addr
, SEEK_SET
) != 0) {
117 unsigned char *buf
= malloc(flash_dev
->cfg
->size_of_sector
);
119 memset((void *)buf
, 0xFF, flash_dev
->cfg
->size_of_sector
);
122 s
= fwrite(buf
, 1, flash_dev
->cfg
->size_of_sector
, flash_dev
->flash_file
);
124 assert(s
== flash_dev
->cfg
->size_of_sector
);
129 static int32_t PIOS_Flash_UT_WriteData(uintptr_t flash_id
, uint32_t addr
, uint8_t *data
, uint16_t len
)
134 struct flash_ut_dev
*flash_dev
= (struct flash_ut_dev
*)flash_id
;
136 assert(flash_dev
->transaction_in_progress
);
138 if (fseek(flash_dev
->flash_file
, addr
, SEEK_SET
) != 0) {
143 s
= fwrite(data
, 1, len
, flash_dev
->flash_file
);
150 static int32_t PIOS_Flash_UT_ReadData(uintptr_t flash_id
, uint32_t addr
, uint8_t *data
, uint16_t len
)
155 struct flash_ut_dev
*flash_dev
= (struct flash_ut_dev
*)flash_id
;
157 assert(flash_dev
->transaction_in_progress
);
159 if (fseek(flash_dev
->flash_file
, addr
, SEEK_SET
) != 0) {
164 s
= fread(data
, 1, len
, flash_dev
->flash_file
);
171 /* Provide a flash driver to external drivers */
172 const struct pios_flash_driver pios_ut_flash_driver
= {
173 .start_transaction
= PIOS_Flash_UT_StartTransaction
,
174 .end_transaction
= PIOS_Flash_UT_EndTransaction
,
175 .erase_sector
= PIOS_Flash_UT_EraseSector
,
176 .write_data
= PIOS_Flash_UT_WriteData
,
177 .read_data
= PIOS_Flash_UT_ReadData
,