32 #if defined(PIOS_INCLUDE_FLASH_JEDEC)
37 #if defined(PIOS_INCLUDE_RTOS)
41 #define JEDEC_WRITE_ENABLE 0x06
42 #define JEDEC_WRITE_DISABLE 0x04
43 #define JEDEC_READ_STATUS 0x05
44 #define JEDEC_WRITE_STATUS 0x01
45 #define JEDEC_READ_DATA 0x03
46 #define JEDEC_FAST_READ 0x0b
47 #define JEDEC_DEVICE_ID 0x9F
48 #define JEDEC_PAGE_WRITE 0x02
50 #define JEDEC_STATUS_BUSY 0x01
51 #define JEDEC_STATUS_WRITEPROTECT 0x02
52 #define JEDEC_STATUS_BP0 0x04
53 #define JEDEC_STATUS_BP1 0x08
54 #define JEDEC_STATUS_BP2 0x10
55 #define JEDEC_STATUS_TP 0x20
56 #define JEDEC_STATUS_SEC 0x40
57 #define JEDEC_STATUS_SRP0 0x80
59 #define JEDED_READ_OPT_DATA 0x4B
61 enum pios_jedec_dev_magic {
62 PIOS_JEDEC_DEV_MAGIC = 0xcb55aa55,
66 struct jedec_flash_dev {
69 uint32_t capacity_bytes;
77 enum pios_jedec_dev_magic
magic;
81 static int32_t PIOS_Flash_Jedec_Validate(
struct jedec_flash_dev *flash_dev);
82 static struct jedec_flash_dev *PIOS_Flash_Jedec_alloc(
void);
84 static int32_t PIOS_Flash_Jedec_ReadID(
struct jedec_flash_dev *flash_dev);
85 static int32_t PIOS_Flash_Jedec_ReadStatus(
struct jedec_flash_dev *flash_dev);
86 static int32_t PIOS_Flash_Jedec_ClaimBus(
struct jedec_flash_dev *flash_dev);
87 static int32_t PIOS_Flash_Jedec_ReleaseBus(
struct jedec_flash_dev *flash_dev);
88 static int32_t PIOS_Flash_Jedec_WriteEnable(
struct jedec_flash_dev *flash_dev);
89 static int32_t PIOS_Flash_Jedec_Busy(
struct jedec_flash_dev *flash_dev);
94 static struct jedec_flash_dev *PIOS_Flash_Jedec_alloc(
void)
96 struct jedec_flash_dev *flash_dev;
98 flash_dev = (
struct jedec_flash_dev *)
PIOS_malloc(
sizeof(*flash_dev));
99 if (!flash_dev)
return (NULL);
101 flash_dev->magic = PIOS_JEDEC_DEV_MAGIC;
109 static int32_t PIOS_Flash_Jedec_Validate(
struct jedec_flash_dev *flash_dev) {
110 if (flash_dev == NULL)
112 if (flash_dev->magic != PIOS_JEDEC_DEV_MAGIC)
114 if (flash_dev->spi_id == 0)
121 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
123 if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
126 return flash_dev->capacity_bytes;
134 struct jedec_flash_dev *flash_dev = PIOS_Flash_Jedec_alloc();
135 if (flash_dev == NULL)
139 if (flash_dev->transaction_lock == NULL)
142 flash_dev->spi_id = spi_id;
143 flash_dev->slave_num = slave_num;
144 flash_dev->cfg =
cfg;
146 (void) PIOS_Flash_Jedec_ReadID(flash_dev);
148 if (flash_dev->manufacturer != flash_dev->cfg->expect_manufacturer) {
150 if (flash_dev->cfg->expect_manufacturer !=
156 if (flash_dev->memorytype != flash_dev->cfg->expect_memorytype) {
157 if (flash_dev->cfg->expect_memorytype !=
163 if (flash_dev->capacity != flash_dev->cfg->expect_capacity) {
168 *chip_id = (uintptr_t) flash_dev;
178 static int32_t PIOS_Flash_Jedec_ClaimBus(
struct jedec_flash_dev *flash_dev)
191 static int32_t PIOS_Flash_Jedec_ReleaseBus(
struct jedec_flash_dev *flash_dev)
202 static int32_t PIOS_Flash_Jedec_Busy(
struct jedec_flash_dev *flash_dev)
204 int32_t
status = PIOS_Flash_Jedec_ReadStatus(flash_dev);
207 return status & JEDEC_STATUS_BUSY;
214 static int32_t PIOS_Flash_Jedec_WriteEnable(
struct jedec_flash_dev *flash_dev)
216 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
223 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
232 static int32_t PIOS_Flash_Jedec_ReadStatus(
struct jedec_flash_dev *flash_dev)
234 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) < 0)
241 uint8_t in[2] = {0,0};
243 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
247 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
255 static int32_t PIOS_Flash_Jedec_ReadID(
struct jedec_flash_dev *flash_dev)
257 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) < 0)
268 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
272 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
274 flash_dev->manufacturer = in[1];
275 flash_dev->memorytype = in[2];
276 flash_dev->capacity = in[3];
277 flash_dev->capacity_bytes = 1 << flash_dev->capacity;
279 return flash_dev->manufacturer;
294 static int32_t PIOS_Flash_Jedec_StartTransaction(uintptr_t chip_id)
296 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
298 if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
312 static int32_t PIOS_Flash_Jedec_EndTransaction(uintptr_t chip_id)
314 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
316 if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
334 static int32_t PIOS_Flash_Jedec_EraseSector(uintptr_t chip_id, uint32_t chip_sector, uint32_t chip_offset)
336 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
338 if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
343 flash_dev->cfg->sector_erase,
344 (chip_offset >> 16) & 0xff,
345 (chip_offset >> 8) & 0xff,
346 (chip_offset >> 0) & 0xff,
349 if ((ret = PIOS_Flash_Jedec_WriteEnable(flash_dev)) != 0)
352 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
356 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
360 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
363 while (PIOS_Flash_Jedec_Busy(flash_dev) != 0) {
364 #if defined(PIOS_INCLUDE_RTOS)
383 static int32_t PIOS_Flash_Jedec_WriteData(uintptr_t chip_id, uint32_t chip_offset,
const uint8_t *
data, uint16_t len)
385 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
387 if(PIOS_Flash_Jedec_Validate(flash_dev) != 0)
393 (chip_offset >> 16) & 0xff,
394 (chip_offset >> 8) & 0xff,
395 (chip_offset >> 0) & 0xff,
403 if (((chip_offset & 0xff) + len) > 0x100)
406 if ((ret = PIOS_Flash_Jedec_WriteEnable(flash_dev)) != 0)
410 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) != 0)
414 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
420 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
424 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
427 #if defined(PIOS_INCLUDE_RTOS)
428 while (PIOS_Flash_Jedec_Busy(flash_dev) != 0) {
434 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) < 0)
440 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
455 static int32_t PIOS_Flash_Jedec_ReadData(uintptr_t chip_id, uint32_t chip_offset, uint8_t *data, uint16_t len)
457 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
459 if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
462 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) == -1)
468 (chip_offset >> 16) & 0xff,
469 (chip_offset >> 8) & 0xff,
470 (chip_offset >> 0) & 0xff,
474 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
480 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
484 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
500 struct jedec_flash_dev *flash_dev = (
struct jedec_flash_dev *)chip_id;
502 if (PIOS_Flash_Jedec_Validate(flash_dev) != 0)
505 if (PIOS_Flash_Jedec_ClaimBus(flash_dev) == -1)
511 (chip_offset >> 16) & 0xff,
512 (chip_offset >> 8) & 0xff,
513 (chip_offset >> 0) & 0xff,
517 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
523 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
527 PIOS_Flash_Jedec_ReleaseBus(flash_dev);
535 .end_transaction = PIOS_Flash_Jedec_EndTransaction,
536 .erase_sector = PIOS_Flash_Jedec_EraseSector,
537 .write_data = PIOS_Flash_Jedec_WriteData,
538 .read_data = PIOS_Flash_Jedec_ReadData,
#define PIOS_SEMAPHORE_TIMEOUT_MAX
Main PiOS header to include all the compiled in PiOS options.
const struct pios_flash_driver pios_jedec_flash_driver
int32_t PIOS_SPI_RC_PinSet(pios_spi_t spi_dev, uint32_t slave_id, bool pin_value)
int32_t PIOS_Flash_Jedec_Init(uintptr_t *flash_id, pios_spi_t spi_id, uint32_t slave_num, const struct pios_flash_jedec_cfg *cfg)
int32_t PIOS_SPI_ClaimBus(pios_spi_t spi_dev)
void * PIOS_malloc(size_t size)
uint8_t data[XFER_BYTES_PER_PACKET]
int32_t PIOS_Flash_Jedec_ReadOTPData(uintptr_t chip_id, uint32_t chip_offset, uint8_t *data, uint16_t len)
static struct flyingpicmd_cfg_fa cfg
#define JEDEC_MEMORYTYPE_ANY
Driver for talking to most JEDEC flash chips.
struct pios_semaphore * PIOS_Semaphore_Create(void)
Creates a binary semaphore.
uint8_t PIOS_SPI_TransferByte(pios_spi_t spi_dev, uint8_t b)
int32_t PIOS_SPI_TransferBlock(pios_spi_t spi_dev, const uint8_t *send_buffer, uint8_t *receive_buffer, uint16_t len)
bool PIOS_Semaphore_Give(struct pios_semaphore *sema)
Gives binary semaphore.
void PIOS_Thread_Sleep(uint32_t time_ms)
bool PIOS_Semaphore_Take(struct pios_semaphore *sema, uint32_t timeout_ms)
Takes binary semaphore.
int32_t PIOS_SPI_ReleaseBus(pios_spi_t spi_dev)
uint32_t PIOS_Flash_Jedec_GetCapacity(uintptr_t chip_id)
#define JEDEC_MANUFACTURER_ANY
int32_t(* start_transaction)(uintptr_t chip_id)