Skip to content

Commit dfa902e

Browse files
committed
STM32 QSPI: frequency calculation update
1 parent 5b5dcd8 commit dfa902e

File tree

1 file changed

+25
-18
lines changed

1 file changed

+25
-18
lines changed

targets/TARGET_STM/qspi_api.c

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,7 @@ qspi_status_t qspi_init(qspi_t *obj, PinName io0, PinName io1, PinName io2, PinN
192192
obj->ssel = ssel;
193193
pinmap_pinout(ssel, PinMap_QSPI_SSEL);
194194

195-
if (HAL_QSPI_Init(&obj->handle) != HAL_OK) {
196-
return QSPI_STATUS_ERROR;
197-
}
198-
qspi_frequency(obj, hz);
199-
return QSPI_STATUS_OK;
195+
return qspi_frequency(obj, hz);
200196
}
201197

202198
qspi_status_t qspi_free(qspi_t *obj)
@@ -228,18 +224,29 @@ qspi_status_t qspi_frequency(qspi_t *obj, int hz)
228224
{
229225
qspi_status_t status = QSPI_STATUS_OK;
230226

231-
// HCLK drives QSPI
227+
/* HCLK drives QSPI. QSPI clock depends on prescaler value:
228+
* 0: Freq = HCLK
229+
* 1: Freq = HCLK/2
230+
* ...
231+
* 255: Freq = HCLK/256 (minimum value)
232+
*/
233+
232234
int div = HAL_RCC_GetHCLKFreq() / hz;
233-
if (div > 256 || div < 1) {
234-
status = QSPI_STATUS_INVALID_PARAMETER;
235-
return status;
235+
if (div > 255) {
236+
div = 255;
237+
}
238+
else {
239+
if ((HAL_RCC_GetHCLKFreq() % hz) == 0) {
240+
div = div - 1;
241+
}
236242
}
237243

238-
obj->handle.Init.ClockPrescaler = div - 1;
244+
obj->handle.Init.ClockPrescaler = div;
239245

240246
if (HAL_QSPI_Init(&obj->handle) != HAL_OK) {
241247
status = QSPI_STATUS_ERROR;
242248
}
249+
243250
return status;
244251
}
245252

@@ -253,11 +260,11 @@ qspi_status_t qspi_write(qspi_t *obj, const qspi_command_t *command, const void
253260

254261
if (HAL_QSPI_Command(&obj->handle, &st_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
255262
status = QSPI_STATUS_ERROR;
256-
return status;
257263
}
258-
259-
if (HAL_QSPI_Transmit(&obj->handle, (uint8_t *)data, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
260-
status = QSPI_STATUS_ERROR;
264+
else {
265+
if (HAL_QSPI_Transmit(&obj->handle, (uint8_t *)data, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
266+
status = QSPI_STATUS_ERROR;
267+
}
261268
}
262269

263270
return status;
@@ -273,11 +280,11 @@ qspi_status_t qspi_read(qspi_t *obj, const qspi_command_t *command, void *data,
273280

274281
if (HAL_QSPI_Command(&obj->handle, &st_command, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
275282
status = QSPI_STATUS_ERROR;
276-
return status;
277283
}
278-
279-
if (HAL_QSPI_Receive(&obj->handle, data, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
280-
status = QSPI_STATUS_ERROR;
284+
else {
285+
if (HAL_QSPI_Receive(&obj->handle, data, HAL_QPSI_TIMEOUT_DEFAULT_VALUE) != HAL_OK) {
286+
status = QSPI_STATUS_ERROR;
287+
}
281288
}
282289

283290
return status;

0 commit comments

Comments
 (0)