Skip to content

Commit d851a63

Browse files
authored
Merge pull request #11602 from kyle-cypress/pr/qspi-arbitrary-alt-size
Allow for arbitrary QSPI alt sizes
2 parents b6266b5 + bb872ee commit d851a63

File tree

3 files changed

+53
-16
lines changed

3 files changed

+53
-16
lines changed

TESTS/mbed_hal/qspi/qspi_test_utils.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ struct Qspi {
102102
#define ADDR_SIZE_24 QSPI_CFG_ADDR_SIZE_24
103103
#define ADDR_SIZE_32 QSPI_CFG_ADDR_SIZE_32
104104

105-
#define ALT_SIZE_8 8u
106-
#define ALT_SIZE_16 16u
107-
#define ALT_SIZE_24 24u
108-
#define ALT_SIZE_32 32u
105+
#define ALT_SIZE_8 QSPI_CFG_ALT_SIZE_8
106+
#define ALT_SIZE_16 QSPI_CFG_ALT_SIZE_16
107+
#define ALT_SIZE_24 QSPI_CFG_ALT_SIZE_24
108+
#define ALT_SIZE_32 QSPI_CFG_ALT_SIZE_32
109109

110110
#define STATUS_REG QSPI_CMD_RDSR
111111
#define CONFIG_REG0 QSPI_CMD_RDCR0

hal/qspi_api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ typedef enum qspi_address_size {
6262
*/
6363
typedef uint8_t qspi_alt_size_t;
6464

65+
// The following defines are provided for backwards compatibilty. New code should explicitly
66+
// specify the required number of alt bits.
67+
#define QSPI_CFG_ALT_SIZE_8 8u
68+
#define QSPI_CFG_ALT_SIZE_16 16u
69+
#define QSPI_CFG_ALT_SIZE_24 24u
70+
#define QSPI_CFG_ALT_SIZE_32 32u
71+
6572
/** QSPI command
6673
*
6774
* Defines a frame format. It consists of instruction, address, alternative, dummy count and data

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)