dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pios_flash_posix.c
Go to the documentation of this file.
1 #include <stdlib.h> /* abort */
2 #include <stdio.h> /* fopen/fread/fwrite/fseek */
3 #include <assert.h> /* assert */
4 #include <string.h> /* memset */
5 
6 #include <stdbool.h>
7 #include "pios_heap.h"
9 
10 #include <pios_semaphore.h>
11 
13  FLASH_POSIX_MAGIC = 0x321dabc1,
14 };
15 
18  const struct pios_flash_posix_cfg * cfg;
19  FILE * flash_file;
20 
22 };
23 
25 {
26  struct flash_posix_dev * flash_dev = PIOS_malloc(sizeof(struct flash_posix_dev));
27 
28  flash_dev->magic = FLASH_POSIX_MAGIC;
29 
30  return flash_dev;
31 }
32 
33 static const char *pios_flash_file_name = "theflash.bin";
34 
35 static const char *PIOS_Flash_Posix_GetFName()
36 {
37  return pios_flash_file_name;
38 }
39 
40 void PIOS_Flash_Posix_SetFName(const char *name)
41 {
42  pios_flash_file_name = name;
43 }
44 
45 int32_t PIOS_Flash_Posix_Init(uintptr_t * chip_id,
46  const struct pios_flash_posix_cfg * cfg,
47  bool force_recreate)
48 {
49  /* Check inputs */
50  assert(chip_id);
51  assert(cfg);
52  assert(cfg->size_of_flash);
53  assert(cfg->size_of_sector);
54  assert((cfg->size_of_flash % cfg->size_of_sector) == 0);
55 
56  struct flash_posix_dev * flash_dev = PIOS_Flash_Posix_Alloc();
57  assert(flash_dev);
58 
59  flash_dev->cfg = cfg;
60 
61  if (!force_recreate) {
62  flash_dev->flash_file = fopen(PIOS_Flash_Posix_GetFName(), "r+");
63  } else {
64  flash_dev->flash_file = NULL;
65  }
66 
67  if (flash_dev->flash_file == NULL) {
68  flash_dev->flash_file = fopen(PIOS_Flash_Posix_GetFName(), "w+");
69  if (!flash_dev->flash_file) {
70  perror("fopen flash");
71  return -1;
72  }
73 
74  uint8_t sector[cfg->size_of_sector];
75  memset(sector, 0xFF, sizeof(sector));
76  for (uint32_t i = 0;
77  i < cfg->size_of_flash / cfg->size_of_sector;
78  i++) {
79  fwrite(sector, sizeof(sector), 1, flash_dev->flash_file);
80  }
81  }
82 
83  fseek(flash_dev->flash_file, 0, SEEK_END);
84  if (ftell(flash_dev->flash_file) != flash_dev->cfg->size_of_flash) {
85  fclose(flash_dev->flash_file);
86  return -2;
87  }
88 
90 
91  *chip_id = (uintptr_t)flash_dev;
92 
93  return 0;
94 }
95 
96 void PIOS_Flash_Posix_Destroy(uintptr_t chip_id)
97 {
98  struct flash_posix_dev * flash_dev = (struct flash_posix_dev *)chip_id;
99 
100  fclose(flash_dev->flash_file);
101 
102  PIOS_free(flash_dev);
103 }
104 
105 /**********************************
106  *
107  * Provide a PIOS flash driver API
108  *
109  *********************************/
110 #include "pios_flash_priv.h"
111 
112 static int32_t PIOS_Flash_Posix_StartTransaction(uintptr_t chip_id)
113 {
114  struct flash_posix_dev * flash_dev = (struct flash_posix_dev *)chip_id;
115 
116  if (PIOS_Semaphore_Take(flash_dev->transaction_lock,
117  PIOS_SEMAPHORE_TIMEOUT_MAX) != true) {
118  return -2;
119  }
120 
121  return 0;
122 }
123 
124 static int32_t PIOS_Flash_Posix_EndTransaction(uintptr_t chip_id)
125 {
126  struct flash_posix_dev * flash_dev = (struct flash_posix_dev *)chip_id;
127 
128  if (PIOS_Semaphore_Give(flash_dev->transaction_lock) != true) {
129  assert(false);
130  }
131 
132  return 0;
133 }
134 
135 static int32_t PIOS_Flash_Posix_EraseSector(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
136 {
137  struct flash_posix_dev * flash_dev = (struct flash_posix_dev *)chip_id;
138 
139  /* assert(flash_dev->transaction_in_progress); */
140 
141  if (fseek (flash_dev->flash_file, chip_offset, SEEK_SET) != 0) {
142  assert(0);
143  }
144 
145  unsigned char buf[flash_dev->cfg->size_of_sector];
146 
147  memset((void *)buf, 0xFF, flash_dev->cfg->size_of_sector);
148 
149  size_t s;
150  s = fwrite (buf, 1, flash_dev->cfg->size_of_sector, flash_dev->flash_file);
151 
152  assert (s == flash_dev->cfg->size_of_sector);
153 
154  fflush(flash_dev->flash_file);
155 
156  return 0;
157 }
158 
159 static int32_t PIOS_Flash_Posix_WriteData(uintptr_t chip_id, uint32_t chip_offset, const uint8_t * data, uint16_t len)
160 {
161  /* Check inputs */
162  assert(data);
163 
164  struct flash_posix_dev * flash_dev = (struct flash_posix_dev *)chip_id;
165 
166  /* assert(flash_dev->transaction_in_progress); */
167 
168  if (fseek (flash_dev->flash_file, chip_offset, SEEK_SET) != 0) {
169  assert(0);
170  }
171 
172  size_t s;
173  s = fwrite (data, 1, len, flash_dev->flash_file);
174 
175  assert (s == len);
176 
177  fflush(flash_dev->flash_file);
178 
179  return 0;
180 }
181 
182 static int32_t PIOS_Flash_Posix_ReadData(uintptr_t chip_id, uint32_t chip_offset, uint8_t * data, uint16_t len)
183 {
184  /* Check inputs */
185  assert(data);
186 
187  struct flash_posix_dev * flash_dev = (struct flash_posix_dev *)chip_id;
188 
189  /* assert(flash_dev->transaction_in_progress); */
190 
191  if (fseek (flash_dev->flash_file, chip_offset, SEEK_SET) != 0) {
192  assert(0);
193  }
194 
195  size_t s;
196  s = fread (data, 1, len, flash_dev->flash_file);
197 
198  assert (s == len);
199 
200  return 0;
201 }
202 
203 /* Provide a flash driver to external drivers */
206  .end_transaction = PIOS_Flash_Posix_EndTransaction,
207  .erase_sector = PIOS_Flash_Posix_EraseSector,
208  .write_data = PIOS_Flash_Posix_WriteData,
209  .read_data = PIOS_Flash_Posix_ReadData,
210 };
211 
#define PIOS_SEMAPHORE_TIMEOUT_MAX
flash_posix_magic
static int32_t PIOS_Flash_Posix_EndTransaction(uintptr_t chip_id)
static int32_t PIOS_Flash_Posix_EraseSector(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
struct pios_semaphore * transaction_lock
static int32_t PIOS_Flash_Posix_WriteData(uintptr_t chip_id, uint32_t chip_offset, const uint8_t *data, uint16_t len)
enum flash_posix_magic magic
const struct pios_flash_driver pios_posix_flash_driver
void * PIOS_malloc(size_t size)
Definition: pios_heap.c:125
const struct pios_flash_posix_cfg * cfg
uint8_t data[XFER_BYTES_PER_PACKET]
Definition: bl_messages.h:129
static struct flyingpicmd_cfg_fa cfg
Definition: main.c:49
static int32_t PIOS_Flash_Posix_StartTransaction(uintptr_t chip_id)
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
void PIOS_Flash_Posix_SetFName(const char *name)
uint8_t i
Definition: msp_messages.h:97
bool PIOS_Semaphore_Give(struct pios_semaphore *sema)
Gives binary semaphore.
void PIOS_Flash_Posix_Destroy(uintptr_t chip_id)
void PIOS_free(void *buf)
Definition: pios_heap.c:174
static const char * PIOS_Flash_Posix_GetFName()
static const char * pios_flash_file_name
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
int32_t PIOS_Flash_Posix_Init(uintptr_t *chip_id, const struct pios_flash_posix_cfg *cfg, bool force_recreate)
static struct flash_posix_dev * PIOS_Flash_Posix_Alloc(void)
static int32_t PIOS_Flash_Posix_ReadData(uintptr_t chip_id, uint32_t chip_offset, uint8_t *data, uint16_t len)
int32_t(* start_transaction)(uintptr_t chip_id)