Skip to content

Commit 8e9877c

Browse files
author
Kyle Kearney
committed
Update STM driver changes for clarity
- Use a switch statement rather than shifting and masking to compute the AlternateBytes value. - Rename rounded_size to alt_bytes to clarify its purpose.
1 parent 8c86052 commit 8e9877c

File tree

1 file changed

+42
-12
lines changed

1 file changed

+42
-12
lines changed

targets/TARGET_STM/qspi_api.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,40 @@
4242
/* hence 2^(31+1), then FLASH_SIZE_DEFAULT = 1<<31 */
4343
#define QSPI_FLASH_SIZE_DEFAULT 0x80000000
4444

45+
#if defined(OCTOSPI1)
46+
static uint32_t get_alt_bytes_size(const uint32_t num_bytes)
47+
{
48+
switch (num_bytes) {
49+
case 1:
50+
return HAL_OSPI_ALTERNATE_BYTES_8_BITS;
51+
case 2:
52+
return HAL_OSPI_ALTERNATE_BYTES_16_BITS;
53+
case 3:
54+
return HAL_OSPI_ALTERNATE_BYTES_24_BITS;
55+
case 4:
56+
return HAL_OSPI_ALTERNATE_BYTES_32_BITS;
57+
}
58+
error("Invalid alt bytes size");
59+
return 0xFFFFFFFF;
60+
}
61+
#else /* OCTOSPI1 */
62+
static uint32_t get_alt_bytes_size(const uint32_t num_bytes)
63+
{
64+
switch (num_bytes) {
65+
case 1:
66+
return QSPI_ALTERNATE_BYTES_8_BITS;
67+
case 2:
68+
return QSPI_ALTERNATE_BYTES_16_BITS;
69+
case 3:
70+
return QSPI_ALTERNATE_BYTES_24_BITS;
71+
case 4:
72+
return QSPI_ALTERNATE_BYTES_32_BITS;
73+
}
74+
error("Invalid alt bytes size");
75+
return 0xFFFFFFFF;
76+
}
77+
#endif /* OCTOSPI1 */
78+
4579
#if defined(OCTOSPI1)
4680
qspi_status_t qspi_prepare_command(const qspi_command_t *command, OSPI_RegularCmdTypeDef *st_command)
4781
{
@@ -151,15 +185,15 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, OSPI_RegularCm
151185
}
152186

153187
// Round up to nearest byte - unused parts of byte act as dummy cycles
154-
uint32_t rounded_size = ((command->alt.size - 1) >> 3) + 1;
188+
uint32_t alt_bytes = ((command->alt.size - 1) >> 3) + 1;
155189
// Maximum of 4 alt bytes
156-
if (rounded_size > 4) {
190+
if (alt_bytes > 4) {
157191
error("Command param error: alt size exceeds maximum of 32 bits\n");
158192
return QSPI_STATUS_ERROR;
159193
}
160194

161195
// Unused bits in most significant byte of alt
162-
uint8_t leftover_bits = (rounded_size << 3) - command->alt.size;
196+
uint8_t leftover_bits = (alt_bytes << 3) - command->alt.size;
163197
if (leftover_bits != 0) {
164198
// Account for dummy cycles that will be spent in the alt portion of the command
165199
uint8_t integrated_dummy_cycles = leftover_bits / alt_lines;
@@ -176,9 +210,7 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, OSPI_RegularCm
176210
st_command->AlternateBytes = command->alt.value;
177211
}
178212

179-
/* command->AlternateBytesSize needs to be shifted by OCTOSPI_CCR_ABSIZE_Pos */
180-
// 0b00 = 1 byte, 0b01 = 2 bytes, 0b10 = 3 bytes, 0b11 = 4 bytes
181-
st_command->AlternateBytesSize = ((rounded_size - 1) << OCTOSPI_CCR_ABSIZE_Pos) & OCTOSPI_CCR_ABSIZE_Msk;
213+
st_command->AlternateBytesSize = get_alt_bytes_size(alt_bytes);
182214
}
183215

184216
switch (command->data.bus_width) {
@@ -283,14 +315,14 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, QSPI_CommandTy
283315
}
284316

285317
// Round up to nearest byte - unused parts of byte act as dummy cycles
286-
uint32_t rounded_size = ((command->alt.size - 1) >> 3) + 1;
318+
uint32_t alt_bytes = ((command->alt.size - 1) >> 3) + 1;
287319
// Maximum of 4 alt bytes
288-
if (rounded_size > 4) {
320+
if (alt_bytes > 4) {
289321
return QSPI_STATUS_ERROR;
290322
}
291323

292324
// Unused bits in most significant byte of alt
293-
uint8_t leftover_bits = (rounded_size << 3) - command->alt.size;
325+
uint8_t leftover_bits = (alt_bytes << 3) - command->alt.size;
294326
if (leftover_bits != 0) {
295327
// Account for dummy cycles that will be spent in the alt portion of the command
296328
uint8_t integrated_dummy_cycles = leftover_bits / alt_lines;
@@ -306,9 +338,7 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, QSPI_CommandTy
306338
st_command->AlternateBytes = command->alt.value;
307339
}
308340

309-
/* command->AlternateBytesSize needs to be shifted by QUADSPI_CCR_ABSIZE_Pos */
310-
// 0b00 = 1 byte, 0b01 = 2 bytes, 0b10 = 3 bytes, 0b11 = 4 bytes
311-
st_command->AlternateBytesSize = ((rounded_size - 1) << QUADSPI_CCR_ABSIZE_Pos) & QUADSPI_CCR_ABSIZE_Msk;
341+
st_command->AlternateBytesSize = get_alt_bytes_size(alt_bytes);
312342
}
313343

314344
switch (command->data.bus_width) {

0 commit comments

Comments
 (0)