Skip to content

Commit dddd634

Browse files
authored
Merge pull request ARMmbed#14437 from ARMmbed/qspi-fix
Remove ownership/acquire in QSPI/OSPI driver
2 parents 76f4704 + 0b4fdb5 commit dddd634

File tree

4 files changed

+34
-108
lines changed

4 files changed

+34
-108
lines changed

drivers/include/drivers/OSPI.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,6 @@ class OSPI : private NonCopyable<OSPI> {
229229

230230
ospi_t _ospi;
231231

232-
bool acquire(void);
233-
static OSPI *_owner;
234232
static SingletonPtr<PlatformMutex> _mutex;
235233
ospi_bus_width_t _inst_width; //Bus width for Instruction phase
236234
ospi_inst_size_t _inst_size; //Instruction Size
@@ -249,10 +247,6 @@ class OSPI : private NonCopyable<OSPI> {
249247
bool (OSPI::* _init_func)(void);
250248

251249
private:
252-
/* Private acquire function without locking/unlocking
253-
* Implemented in order to avoid duplicate locking and boost performance
254-
*/
255-
bool _acquire(void);
256250
bool _initialize();
257251
bool _initialize_direct();
258252

drivers/include/drivers/QSPI.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,6 @@ class QSPI : private NonCopyable<QSPI> {
223223

224224
qspi_t _qspi;
225225

226-
bool acquire(void);
227-
static QSPI *_owner;
228226
static SingletonPtr<PlatformMutex> _mutex;
229227
qspi_bus_width_t _inst_width; //Bus width for Instruction phase
230228
qspi_bus_width_t _address_width; //Bus width for Address phase
@@ -242,10 +240,6 @@ class QSPI : private NonCopyable<QSPI> {
242240
bool (QSPI::* _init_func)(void);
243241

244242
private:
245-
/* Private acquire function without locking/unlocking
246-
* Implemented in order to avoid duplicate locking and boost performance
247-
*/
248-
bool _acquire(void);
249243
bool _initialize();
250244
bool _initialize_direct();
251245

drivers/source/OSPI.cpp

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
namespace mbed {
2525

26-
OSPI *OSPI::_owner = NULL;
2726
SingletonPtr<PlatformMutex> OSPI::_mutex;
2827

2928
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)
144143
if (_initialized) {
145144
lock();
146145
_hz = hz;
147-
//If the same owner, just change freq.
148-
//Otherwise we may have to change mode as well, so call _acquire
149-
if (_owner == this) {
150-
if (OSPI_STATUS_OK != ospi_frequency(&_ospi, _hz)) {
151-
ret_status = OSPI_STATUS_ERROR;
152-
}
153-
} else {
154-
_acquire();
146+
if (OSPI_STATUS_OK != ospi_frequency(&_ospi, _hz)) {
147+
ret_status = OSPI_STATUS_ERROR;
155148
}
156149
unlock();
157150
} else {
@@ -169,11 +162,9 @@ ospi_status_t OSPI::read(int address, char *rx_buffer, size_t *rx_length)
169162
if ((rx_length != NULL) && (rx_buffer != NULL)) {
170163
if (*rx_length != 0) {
171164
lock();
172-
if (true == _acquire()) {
173-
_build_ospi_command(OSPI_NO_INST, address, -1);
174-
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
175-
ret_status = OSPI_STATUS_OK;
176-
}
165+
_build_ospi_command(OSPI_NO_INST, address, -1);
166+
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
167+
ret_status = OSPI_STATUS_OK;
177168
}
178169
unlock();
179170
}
@@ -193,11 +184,9 @@ ospi_status_t OSPI::write(int address, const char *tx_buffer, size_t *tx_length)
193184
if ((tx_length != NULL) && (tx_buffer != NULL)) {
194185
if (*tx_length != 0) {
195186
lock();
196-
if (true == _acquire()) {
197-
_build_ospi_command(OSPI_NO_INST, address, -1);
198-
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
199-
ret_status = OSPI_STATUS_OK;
200-
}
187+
_build_ospi_command(OSPI_NO_INST, address, -1);
188+
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
189+
ret_status = OSPI_STATUS_OK;
201190
}
202191
unlock();
203192
}
@@ -217,11 +206,9 @@ ospi_status_t OSPI::read(ospi_inst_t instruction, int alt, int address, char *rx
217206
if ((rx_length != NULL) && (rx_buffer != NULL)) {
218207
if (*rx_length != 0) {
219208
lock();
220-
if (true == _acquire()) {
221-
_build_ospi_command(instruction, address, alt);
222-
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
223-
ret_status = OSPI_STATUS_OK;
224-
}
209+
_build_ospi_command(instruction, address, alt);
210+
if (OSPI_STATUS_OK == ospi_read(&_ospi, &_ospi_command, rx_buffer, rx_length)) {
211+
ret_status = OSPI_STATUS_OK;
225212
}
226213
unlock();
227214
}
@@ -241,11 +228,9 @@ ospi_status_t OSPI::write(ospi_inst_t instruction, int alt, int address, const c
241228
if ((tx_length != NULL) && (tx_buffer != NULL)) {
242229
if (*tx_length != 0) {
243230
lock();
244-
if (true == _acquire()) {
245-
_build_ospi_command(instruction, address, alt);
246-
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
247-
ret_status = OSPI_STATUS_OK;
248-
}
231+
_build_ospi_command(instruction, address, alt);
232+
if (OSPI_STATUS_OK == ospi_write(&_ospi, &_ospi_command, tx_buffer, tx_length)) {
233+
ret_status = OSPI_STATUS_OK;
249234
}
250235
unlock();
251236
}
@@ -263,11 +248,9 @@ ospi_status_t OSPI::command_transfer(ospi_inst_t instruction, int address, const
263248

264249
if (_initialized) {
265250
lock();
266-
if (true == _acquire()) {
267-
_build_ospi_command(instruction, address, -1); //We just need the command
268-
if (OSPI_STATUS_OK == ospi_command_transfer(&_ospi, &_ospi_command, (const void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length)) {
269-
ret_status = OSPI_STATUS_OK;
270-
}
251+
_build_ospi_command(instruction, address, -1); //We just need the command
252+
if (OSPI_STATUS_OK == ospi_command_transfer(&_ospi, &_ospi_command, (const void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length)) {
253+
ret_status = OSPI_STATUS_OK;
271254
}
272255
unlock();
273256
}
@@ -296,7 +279,6 @@ bool OSPI::_initialize()
296279
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);
297280
if (OSPI_STATUS_OK == ret) {
298281
_initialized = true;
299-
_owner = this;
300282
} else {
301283
_initialized = false;
302284
}
@@ -315,26 +297,13 @@ bool OSPI::_initialize_direct()
315297
ospi_status_t ret = ospi_init_direct(&_ospi, _static_pinmap, _hz, _mode);
316298
if (OSPI_STATUS_OK == ret) {
317299
_initialized = true;
318-
_owner = this;
319300
} else {
320301
_initialized = false;
321302
}
322303

323304
return _initialized;
324305
}
325306

326-
// Note: Private function with no locking
327-
bool OSPI::_acquire()
328-
{
329-
if (_owner != this) {
330-
//This will set freq as well
331-
(this->*_init_func)();
332-
_owner = this;
333-
}
334-
335-
return _initialized;
336-
}
337-
338307
void OSPI::_build_ospi_command(ospi_inst_t instruction, int address, int alt)
339308
{
340309
memset(&_ospi_command, 0, sizeof(ospi_command_t));

drivers/source/QSPI.cpp

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
namespace mbed {
2525

26-
QSPI *QSPI::_owner = NULL;
2726
SingletonPtr<PlatformMutex> QSPI::_mutex;
2827

2928
uint8_t convert_bus_width_to_line_count(qspi_bus_width_t width)
@@ -124,14 +123,8 @@ qspi_status_t QSPI::set_frequency(int hz)
124123
if (_initialized) {
125124
lock();
126125
_hz = hz;
127-
//If the same owner, just change freq.
128-
//Otherwise we may have to change mode as well, so call _acquire
129-
if (_owner == this) {
130-
if (QSPI_STATUS_OK != qspi_frequency(&_qspi, _hz)) {
131-
ret_status = QSPI_STATUS_ERROR;
132-
}
133-
} else {
134-
_acquire();
126+
if (QSPI_STATUS_OK != qspi_frequency(&_qspi, _hz)) {
127+
ret_status = QSPI_STATUS_ERROR;
135128
}
136129
unlock();
137130
} else {
@@ -149,11 +142,9 @@ qspi_status_t QSPI::read(int address, char *rx_buffer, size_t *rx_length)
149142
if ((rx_length != NULL) && (rx_buffer != NULL)) {
150143
if (*rx_length != 0) {
151144
lock();
152-
if (true == _acquire()) {
153-
_build_qspi_command(QSPI_NO_INST, address, -1);
154-
if (QSPI_STATUS_OK == qspi_read(&_qspi, &_qspi_command, rx_buffer, rx_length)) {
155-
ret_status = QSPI_STATUS_OK;
156-
}
145+
_build_qspi_command(QSPI_NO_INST, address, -1);
146+
if (QSPI_STATUS_OK == qspi_read(&_qspi, &_qspi_command, rx_buffer, rx_length)) {
147+
ret_status = QSPI_STATUS_OK;
157148
}
158149
unlock();
159150
}
@@ -173,11 +164,9 @@ qspi_status_t QSPI::write(int address, const char *tx_buffer, size_t *tx_length)
173164
if ((tx_length != NULL) && (tx_buffer != NULL)) {
174165
if (*tx_length != 0) {
175166
lock();
176-
if (true == _acquire()) {
177-
_build_qspi_command(QSPI_NO_INST, address, -1);
178-
if (QSPI_STATUS_OK == qspi_write(&_qspi, &_qspi_command, tx_buffer, tx_length)) {
179-
ret_status = QSPI_STATUS_OK;
180-
}
167+
_build_qspi_command(QSPI_NO_INST, address, -1);
168+
if (QSPI_STATUS_OK == qspi_write(&_qspi, &_qspi_command, tx_buffer, tx_length)) {
169+
ret_status = QSPI_STATUS_OK;
181170
}
182171
unlock();
183172
}
@@ -197,11 +186,9 @@ qspi_status_t QSPI::read(qspi_inst_t instruction, int alt, int address, char *rx
197186
if ((rx_length != NULL) && (rx_buffer != NULL)) {
198187
if (*rx_length != 0) {
199188
lock();
200-
if (true == _acquire()) {
201-
_build_qspi_command(instruction, address, alt);
202-
if (QSPI_STATUS_OK == qspi_read(&_qspi, &_qspi_command, rx_buffer, rx_length)) {
203-
ret_status = QSPI_STATUS_OK;
204-
}
189+
_build_qspi_command(instruction, address, alt);
190+
if (QSPI_STATUS_OK == qspi_read(&_qspi, &_qspi_command, rx_buffer, rx_length)) {
191+
ret_status = QSPI_STATUS_OK;
205192
}
206193
unlock();
207194
}
@@ -221,11 +208,9 @@ qspi_status_t QSPI::write(qspi_inst_t instruction, int alt, int address, const c
221208
if ((tx_length != NULL) && (tx_buffer != NULL)) {
222209
if (*tx_length != 0) {
223210
lock();
224-
if (true == _acquire()) {
225-
_build_qspi_command(instruction, address, alt);
226-
if (QSPI_STATUS_OK == qspi_write(&_qspi, &_qspi_command, tx_buffer, tx_length)) {
227-
ret_status = QSPI_STATUS_OK;
228-
}
211+
_build_qspi_command(instruction, address, alt);
212+
if (QSPI_STATUS_OK == qspi_write(&_qspi, &_qspi_command, tx_buffer, tx_length)) {
213+
ret_status = QSPI_STATUS_OK;
229214
}
230215
unlock();
231216
}
@@ -243,11 +228,9 @@ qspi_status_t QSPI::command_transfer(qspi_inst_t instruction, int address, const
243228

244229
if (_initialized) {
245230
lock();
246-
if (true == _acquire()) {
247-
_build_qspi_command(instruction, address, -1); //We just need the command
248-
if (QSPI_STATUS_OK == qspi_command_transfer(&_qspi, &_qspi_command, (const void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length)) {
249-
ret_status = QSPI_STATUS_OK;
250-
}
231+
_build_qspi_command(instruction, address, -1); //We just need the command
232+
if (QSPI_STATUS_OK == qspi_command_transfer(&_qspi, &_qspi_command, (const void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length)) {
233+
ret_status = QSPI_STATUS_OK;
251234
}
252235
unlock();
253236
}
@@ -276,7 +259,6 @@ bool QSPI::_initialize()
276259
qspi_status_t ret = qspi_init(&_qspi, _qspi_io0, _qspi_io1, _qspi_io2, _qspi_io3, _qspi_clk, _qspi_cs, _hz, _mode);
277260
if (QSPI_STATUS_OK == ret) {
278261
_initialized = true;
279-
_owner = this;
280262
} else {
281263
_initialized = false;
282264
}
@@ -295,26 +277,13 @@ bool QSPI::_initialize_direct()
295277
qspi_status_t ret = qspi_init_direct(&_qspi, _static_pinmap, _hz, _mode);
296278
if (QSPI_STATUS_OK == ret) {
297279
_initialized = true;
298-
_owner = this;
299280
} else {
300281
_initialized = false;
301282
}
302283

303284
return _initialized;
304285
}
305286

306-
// Note: Private function with no locking
307-
bool QSPI::_acquire()
308-
{
309-
if (_owner != this) {
310-
//This will set freq as well
311-
(this->*_init_func)();
312-
_owner = this;
313-
}
314-
315-
return _initialized;
316-
}
317-
318287
void QSPI::_build_qspi_command(qspi_inst_t instruction, int address, int alt)
319288
{
320289
memset(&_qspi_command, 0, sizeof(qspi_command_t));

0 commit comments

Comments
 (0)