Skip to content

Commit 71d4554

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 9b32c0f commit 71d4554

File tree

1 file changed

+44
-12
lines changed

1 file changed

+44
-12
lines changed

targets/TARGET_STM/qspi_api.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,42 @@
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+
{
50+
case 1:
51+
return HAL_OSPI_ALTERNATE_BYTES_8_BITS;
52+
case 2:
53+
return HAL_OSPI_ALTERNATE_BYTES_16_BITS;
54+
case 3:
55+
return HAL_OSPI_ALTERNATE_BYTES_24_BITS;
56+
case 4:
57+
return HAL_OSPI_ALTERNATE_BYTES_32_BITS;
58+
}
59+
error("Invalid alt bytes size");
60+
return 0xFFFFFFFF;
61+
}
62+
#else /* OCTOSPI1 */
63+
static uint32_t get_alt_bytes_size(const uint32_t num_bytes)
64+
{
65+
switch(num_bytes)
66+
{
67+
case 1:
68+
return QSPI_ALTERNATE_BYTES_8_BITS;
69+
case 2:
70+
return QSPI_ALTERNATE_BYTES_16_BITS;
71+
case 3:
72+
return QSPI_ALTERNATE_BYTES_24_BITS;
73+
case 4:
74+
return QSPI_ALTERNATE_BYTES_32_BITS;
75+
}
76+
error("Invalid alt bytes size");
77+
return 0xFFFFFFFF;
78+
}
79+
#endif /* OCTOSPI1 */
80+
4581
#if defined(OCTOSPI1)
4682
qspi_status_t qspi_prepare_command(const qspi_command_t *command, OSPI_RegularCmdTypeDef *st_command)
4783
{
@@ -151,15 +187,15 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, OSPI_RegularCm
151187
}
152188

153189
// Round up to nearest byte - unused parts of byte act as dummy cycles
154-
uint32_t rounded_size = ((command->alt.size - 1) >> 3) + 1;
190+
uint32_t alt_bytes = ((command->alt.size - 1) >> 3) + 1;
155191
// Maximum of 4 alt bytes
156-
if (rounded_size > 4) {
192+
if (alt_bytes > 4) {
157193
error("Command param error: alt size exceeds maximum of 32 bits\n");
158194
return QSPI_STATUS_ERROR;
159195
}
160196

161197
// Unused bits in most significant byte of alt
162-
uint8_t leftover_bits = (rounded_size << 3) - command->alt.size;
198+
uint8_t leftover_bits = (alt_bytes << 3) - command->alt.size;
163199
if (leftover_bits != 0) {
164200
// Account for dummy cycles that will be spent in the alt portion of the command
165201
uint8_t integrated_dummy_cycles = leftover_bits / alt_lines;
@@ -177,9 +213,7 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, OSPI_RegularCm
177213
st_command->AlternateBytes = command->alt.value;
178214
}
179215

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

185219
switch (command->data.bus_width) {
@@ -284,14 +318,14 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, QSPI_CommandTy
284318
}
285319

286320
// Round up to nearest byte - unused parts of byte act as dummy cycles
287-
uint32_t rounded_size = ((command->alt.size - 1) >> 3) + 1;
321+
uint32_t alt_bytes = ((command->alt.size - 1) >> 3) + 1;
288322
// Maximum of 4 alt bytes
289-
if (rounded_size > 4) {
323+
if (alt_bytes > 4) {
290324
return QSPI_STATUS_ERROR;
291325
}
292326

293327
// Unused bits in most significant byte of alt
294-
uint8_t leftover_bits = (rounded_size << 3) - command->alt.size;
328+
uint8_t leftover_bits = (alt_bytes << 3) - command->alt.size;
295329
if (leftover_bits != 0) {
296330
// Account for dummy cycles that will be spent in the alt portion of the command
297331
uint8_t integrated_dummy_cycles = leftover_bits / alt_lines;
@@ -308,9 +342,7 @@ qspi_status_t qspi_prepare_command(const qspi_command_t *command, QSPI_CommandTy
308342
st_command->AlternateBytes = command->alt.value;
309343
}
310344

311-
/* command->AlternateBytesSize needs to be shifted by QUADSPI_CCR_ABSIZE_Pos */
312-
// 0b00 = 1 byte, 0b01 = 2 bytes, 0b10 = 3 bytes, 0b11 = 4 bytes
313-
st_command->AlternateBytesSize = ((rounded_size - 1) << QUADSPI_CCR_ABSIZE_Pos) & QUADSPI_CCR_ABSIZE_Msk;
345+
st_command->AlternateBytesSize = get_alt_bytes_size(alt_bytes);
314346
}
315347

316348
switch (command->data.bus_width) {

0 commit comments

Comments
 (0)