mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-02-20 10:54:14 +01:00
Add a transaction semaphore to flash and flashfs to make sure multiple saves
or loads can't be attempted between threads
This commit is contained in:
parent
8b15fdd88b
commit
4fc907ad0d
@ -61,6 +61,7 @@ struct jedec_flash_dev {
|
||||
uint32_t device_type;
|
||||
uint32_t capacity;
|
||||
const struct pios_flash_jedec_cfg * cfg;
|
||||
xSemaphoreHandle transaction_lock;
|
||||
enum pios_jedec_dev_magic magic;
|
||||
};
|
||||
|
||||
@ -88,6 +89,7 @@ static struct jedec_flash_dev * PIOS_Flash_Jedec_alloc(void)
|
||||
|
||||
jedec_dev->claimed = false;
|
||||
jedec_dev->magic = PIOS_JEDEC_DEV_MAGIC;
|
||||
jedec_dev->transaction_lock = xSemaphoreCreateMutex();
|
||||
return(jedec_dev);
|
||||
}
|
||||
|
||||
@ -184,6 +186,35 @@ int32_t PIOS_Flash_Jedec_Init(uint32_t spi_id, uint32_t slave_num, const struct
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Grab the semaphore to perform a transaction
|
||||
* @return 0 for success, -1 for timeout
|
||||
*/
|
||||
int32_t PIOS_Flash_Jedec_StartTransaction()
|
||||
{
|
||||
if(PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
||||
return -1;
|
||||
|
||||
if(xSemaphoreTake(flash_dev->transaction_lock, portMAX_DELAY) != pdTRUE)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Release the semaphore to perform a transaction
|
||||
* @return 0 for success, -1 for timeout
|
||||
*/
|
||||
int32_t PIOS_Flash_Jedec_EndTransaction()
|
||||
{
|
||||
if(PIOS_Flash_Jedec_Validate(flash_dev) != 0)
|
||||
return -1;
|
||||
|
||||
if(xSemaphoreGive(flash_dev->transaction_lock) != pdTRUE)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read the status register from flash chip and return it
|
||||
|
@ -229,6 +229,9 @@ int32_t PIOS_FLASHFS_ObjSave(UAVObjHandle obj, uint16_t instId, uint8_t * data)
|
||||
uint32_t objId = UAVObjGetID(obj);
|
||||
uint8_t crc = 0;
|
||||
|
||||
if(PIOS_Flash_Jedec_StartTransaction() != 0)
|
||||
return -1;
|
||||
|
||||
int32_t addr = PIOS_FLASHFS_GetObjAddress(objId, instId);
|
||||
|
||||
// Object currently not saved
|
||||
@ -267,6 +270,9 @@ int32_t PIOS_FLASHFS_ObjSave(UAVObjHandle obj, uint16_t instId, uint8_t * data)
|
||||
if(PIOS_Flash_Jedec_WriteData(addr + sizeof(header) + UAVObjGetNumBytes(obj), (uint8_t *) &crc, sizeof(crc)) != 0)
|
||||
return -4;
|
||||
|
||||
if(PIOS_Flash_Jedec_EndTransaction() != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -293,6 +299,9 @@ int32_t PIOS_FLASHFS_ObjLoad(UAVObjHandle obj, uint16_t instId, uint8_t * data)
|
||||
const uint8_t crc_read_step = 8;
|
||||
uint8_t crc_read_buffer[crc_read_step];
|
||||
|
||||
if(PIOS_Flash_Jedec_StartTransaction() != 0)
|
||||
return -1;
|
||||
|
||||
int32_t addr = PIOS_FLASHFS_GetObjAddress(objId, instId);
|
||||
|
||||
// Object currently not saved
|
||||
@ -331,6 +340,9 @@ int32_t PIOS_FLASHFS_ObjLoad(UAVObjHandle obj, uint16_t instId, uint8_t * data)
|
||||
if (PIOS_Flash_Jedec_ReadData(addr + sizeof(header), data, objSize) != 0)
|
||||
return -4;
|
||||
|
||||
if(PIOS_Flash_Jedec_EndTransaction() != 0)
|
||||
return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -40,3 +40,5 @@ int32_t PIOS_Flash_Jedec_EraseChip();
|
||||
int32_t PIOS_Flash_Jedec_EraseSector(uint32_t add);
|
||||
int32_t PIOS_Flash_Jedec_WriteData(uint32_t addr, uint8_t * data, uint16_t len);
|
||||
int32_t PIOS_Flash_Jedec_ReadData(uint32_t addr, uint8_t * data, uint16_t len);
|
||||
int32_t PIOS_Flash_Jedec_StartTransaction();
|
||||
int32_t PIOS_Flash_Jedec_EndTransaction();
|
||||
|
Loading…
x
Reference in New Issue
Block a user