Remove ownership in OSPI driver

pull/14437/head
George Psimenos 2021-03-18 17:12:19 +00:00
parent a28a8684c9
commit 0b4fdb5e70
2 changed files with 17 additions and 54 deletions

View File

@ -229,8 +229,6 @@ protected:
ospi_t _ospi;
bool acquire(void);
static OSPI *_owner;
static SingletonPtr<PlatformMutex> _mutex;
ospi_bus_width_t _inst_width; //Bus width for Instruction phase
ospi_inst_size_t _inst_size; //Instruction Size
@ -249,10 +247,6 @@ protected:
bool (OSPI::* _init_func)(void);
private:
/* Private acquire function without locking/unlocking
* Implemented in order to avoid duplicate locking and boost performance
*/
bool _acquire(void);
bool _initialize();
bool _initialize_direct();

View File

@ -23,7 +23,6 @@
namespace mbed {
OSPI *OSPI::_owner = NULL;
SingletonPtr<PlatformMutex> OSPI::_mutex;
uint8_t convert_bus_width_to_line_count(ospi_bus_width_t width)
@ -144,14 +143,8 @@ ospi_status_t OSPI::set_frequency(int hz)
if (_initialized) {
lock();
_hz = hz;
//If the same owner, just change freq.
//Otherwise we may have to change mode as well, so call _acquire
if (_owner == this) {
if (OSPI_STATUS_OK != ospi_frequency(&_ospi, _hz)) {
ret_status = OSPI_STATUS_ERROR;
}
} else {
_acquire();
if (OSPI_STATUS_OK != ospi_frequency(&_ospi, _hz)) {
ret_status = OSPI_STATUS_ERROR;
}
unlock();
} else {
@ -169,11 +162,9 @@ ospi_status_t OSPI::read(int address, char *rx_buffer, size_t *rx_length)
if ((rx_length != NULL) && (rx_buffer != NULL)) {
if (*rx_length != 0) {
lock();
if (true == _acquire()) {
_build_ospi_command(OSPI_NO_INST, address, -1);
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
ret_status = OSPI_STATUS_OK;
}
_build_ospi_command(OSPI_NO_INST, address, -1);
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
ret_status = OSPI_STATUS_OK;
}
unlock();
}
@ -193,11 +184,9 @@ ospi_status_t OSPI::write(int address, const char *tx_buffer, size_t *tx_length)
if ((tx_length != NULL) && (tx_buffer != NULL)) {
if (*tx_length != 0) {
lock();
if (true == _acquire()) {
_build_ospi_command(OSPI_NO_INST, address, -1);
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
ret_status = OSPI_STATUS_OK;
}
_build_ospi_command(OSPI_NO_INST, address, -1);
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
ret_status = OSPI_STATUS_OK;
}
unlock();
}
@ -217,11 +206,9 @@ ospi_status_t OSPI::read(ospi_inst_t instruction, int alt, int address, char *rx
if ((rx_length != NULL) && (rx_buffer != NULL)) {
if (*rx_length != 0) {
lock();
if (true == _acquire()) {
_build_ospi_command(instruction, address, alt);
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
ret_status = OSPI_STATUS_OK;
}
_build_ospi_command(instruction, address, alt);
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
ret_status = OSPI_STATUS_OK;
}
unlock();
}
@ -241,11 +228,9 @@ ospi_status_t OSPI::write(ospi_inst_t instruction, int alt, int address, const c
if ((tx_length != NULL) && (tx_buffer != NULL)) {
if (*tx_length != 0) {
lock();
if (true == _acquire()) {
_build_ospi_command(instruction, address, alt);
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
ret_status = OSPI_STATUS_OK;
}
_build_ospi_command(instruction, address, alt);
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
ret_status = OSPI_STATUS_OK;
}
unlock();
}
@ -263,11 +248,9 @@ ospi_status_t OSPI::command_transfer(ospi_inst_t instruction, int address, const
if (_initialized) {
lock();
if (true == _acquire()) {
_build_ospi_command(instruction, address, -1); //We just need the command
if (OSPI_STATUS_OK == ospi_command_transfer(&_ospi, &_ospi_command, (const void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length)) {
ret_status = OSPI_STATUS_OK;
}
_build_ospi_command(instruction, address, -1); //We just need the command
if (OSPI_STATUS_OK == ospi_command_transfer(&_ospi, &_ospi_command, (const void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length)) {
ret_status = OSPI_STATUS_OK;
}
unlock();
}
@ -296,7 +279,6 @@ bool OSPI::_initialize()
ospi_status_t ret = ospi_init(&_ospi, _ospi_io0, _ospi_io1, _ospi_io2, _ospi_io3, _ospi_io4, _ospi_io5, _ospi_io6, _ospi_io7, _ospi_clk, _ospi_cs, _ospi_dqs, _hz, _mode);
if (OSPI_STATUS_OK == ret) {
_initialized = true;
_owner = this;
} else {
_initialized = false;
}
@ -315,7 +297,6 @@ bool OSPI::_initialize_direct()
ospi_status_t ret = ospi_init_direct(&_ospi, _static_pinmap, _hz, _mode);
if (OSPI_STATUS_OK == ret) {
_initialized = true;
_owner = this;
} else {
_initialized = false;
}
@ -323,18 +304,6 @@ bool OSPI::_initialize_direct()
return _initialized;
}
// Note: Private function with no locking
bool OSPI::_acquire()
{
if (_owner != this) {
//This will set freq as well
(this->*_init_func)();
_owner = this;
}
return _initialized;
}
void OSPI::_build_ospi_command(ospi_inst_t instruction, int address, int alt)
{
memset(&_ospi_command, 0, sizeof(ospi_command_t));