Skip to content

Commit 628b749

Browse files
update dosfs_sdcard.c with the proper spi interface setup (was subject to bitrot a tad)
1 parent 84dc950 commit 628b749

File tree

5 files changed

+34
-27
lines changed

5 files changed

+34
-27
lines changed

system/libstm32l4_dragonfly/dosfs_sdcard.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ bool stm32l4_sdcard_spi_init(dosfs_sdcard_t *sdcard)
4545
pins.sck = GPIO_PIN_PC10_SPI3_SCK;
4646
pins.ss = GPIO_PIN_NONE;
4747

48-
stm32l4_gpio_pin_configure(GPIO_PIN_PD2, (GPIO_PUPD_NONE | GPIO_OSPEED_HIGH | GPIO_OTYPE_PUSHPULL | GPIO_MODE_INPUT));
49-
50-
stm32l4_spi_create(&sdcard->spi, SPI_INSTANCE_SPI3, &pins, 2, SPI_MODE_RX_DMA | SPI_MODE_TX_DMA | SPI_MODE_RX_DMA_SECONDARY | SPI_MODE_TX_DMA_SECONDARY);
48+
// ### FIXME: IRQ LEVEL needs to be adjusted !!!
49+
stm32l4_spi_create(&sdcard->spi, SPI_INSTANCE_SPI3, &pins, 11, SPI_MODE_RX_DMA | SPI_MODE_TX_DMA | SPI_MODE_RX_DMA_SECONDARY | SPI_MODE_TX_DMA_SECONDARY);
5150
stm32l4_spi_enable(&sdcard->spi, NULL, NULL, 0);
5251

5352
clock = stm32l4_spi_clock(&sdcard->spi) / 2;
@@ -62,13 +61,32 @@ bool stm32l4_sdcard_spi_init(dosfs_sdcard_t *sdcard)
6261
sdcard->control = SPI_CONTROL_MODE_3 | (divide << SPI_CONTROL_DIV_SHIFT);
6362
sdcard->pin_cs = GPIO_PIN_PD2;
6463

64+
stm32l4_gpio_pin_configure(sdcard->pin_cs, (GPIO_PUPD_NONE | GPIO_OSPEED_HIGH | GPIO_OTYPE_PUSHPULL | GPIO_MODE_OUTPUT));
65+
stm32l4_gpio_pin_write(sdcard->pin_cs, 1);
66+
6567
return true;
6668
}
6769

6870
bool stm32l4_sdcard_spi_present(dosfs_sdcard_t *sdcard)
6971
{
70-
// return !!stm32l4_gpio_pin_read(sdcard->pin_cs);
71-
return true;
72+
bool present;
73+
74+
/* This below is kind of fragile. The idea is to first set CS to 0, wait a tad
75+
* till the input settles to a 0. The switch the mode to input, while will make
76+
* the external pullup on CS take effect. If internal PULLDOWN does not work,
77+
* it overpowers the external pullupon CS. The delays are required to let
78+
* the signal on CS settle.
79+
*/
80+
81+
stm32l4_gpio_pin_write(sdcard->pin_cs, 0);
82+
armv7m_clock_spin(2000);
83+
stm32l4_gpio_pin_input(sdcard->pin_cs);
84+
armv7m_clock_spin(2000);
85+
present = !!stm32l4_gpio_pin_read(sdcard->pin_cs);
86+
stm32l4_gpio_pin_output(sdcard->pin_cs);
87+
stm32l4_gpio_pin_write(sdcard->pin_cs, 1);
88+
89+
return present;
7290
}
7391

7492
uint32_t stm32l4_sdcard_spi_mode(dosfs_sdcard_t *sdcard, int mode)
@@ -78,29 +96,15 @@ uint32_t stm32l4_sdcard_spi_mode(dosfs_sdcard_t *sdcard, int mode)
7896
if (mode == DOSFS_SDCARD_MODE_NONE)
7997
{
8098
speed = 0;
81-
82-
/* Switch CS_SDCARD to be input
83-
*/
84-
stm32l4_gpio_pin_configure(sdcard->pin_cs, (GPIO_PUPD_NONE | GPIO_OSPEED_HIGH | GPIO_OTYPE_PUSHPULL | GPIO_MODE_INPUT));
8599
}
86100
else
87101
{
88102
if (mode == DOSFS_SDCARD_MODE_IDENTIFY)
89103
{
90104
speed = 400000;
91-
92-
/* Switch CS_SDCARD to be output
93-
*/
94-
95-
stm32l4_gpio_pin_configure(sdcard->pin_cs, (GPIO_PUPD_NONE | GPIO_OSPEED_HIGH | GPIO_OTYPE_PUSHPULL | GPIO_MODE_OUTPUT));
96-
stm32l4_gpio_pin_write(sdcard->pin_cs, 1);
97105
}
98106
else
99107
{
100-
// speed = 20000000;
101-
// speed = 10000000;
102-
speed = 5000000;
103-
104108
if (mode == DOSFS_SDCARD_MODE_DATA_TRANSFER)
105109
{
106110
speed = 25000000;
@@ -110,15 +114,13 @@ uint32_t stm32l4_sdcard_spi_mode(dosfs_sdcard_t *sdcard, int mode)
110114
speed = 50000000;
111115
}
112116

113-
// speed = 5000000;
114-
115117
stm32l4_sdcard_spi_deselect(sdcard);
116118
}
117119

118120
clock = stm32l4_spi_clock(&sdcard->spi) / 2;
119121
divide = 0;
120122

121-
while ((clock > 400000) && (divide < 7))
123+
while ((clock > speed) && (divide < 7))
122124
{
123125
clock /= 2;
124126
divide++;
@@ -198,7 +200,7 @@ uint8_t stm32l4_sdcard_spi_receive(dosfs_sdcard_t *sdcard)
198200

199201
void stm32l4_sdcard_spi_send_block(dosfs_sdcard_t *sdcard, const uint8_t *data)
200202
{
201-
stm32l4_spi_transmit(&sdcard->spi, data, 512, SPI_CONTROL_CRC16);
203+
stm32l4_spi_transmit(&sdcard->spi, data, 512, sdcard->control | SPI_CONTROL_CRC16);
202204

203205
while (!stm32l4_spi_done(&sdcard->spi))
204206
{
@@ -208,7 +210,7 @@ void stm32l4_sdcard_spi_send_block(dosfs_sdcard_t *sdcard, const uint8_t *data)
208210

209211
uint32_t stm32l4_sdcard_spi_receive_block(dosfs_sdcard_t *sdcard, uint8_t *data)
210212
{
211-
stm32l4_spi_receive(&sdcard->spi, data, 512, SPI_CONTROL_CRC16);
213+
stm32l4_spi_receive(&sdcard->spi, data, 512, sdcard->control | SPI_CONTROL_CRC16);
212214

213215
while (!stm32l4_spi_done(&sdcard->spi))
214216
{

system/libstm32l4_dragonfly/dosfs_sflash.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ static uint32_t dosfs_sflash_nor_identify(dosfs_sflash_t *sflash)
7272
qspi_pins.io2 = GPIO_PIN_PA7_QUADSPI_BK1_IO2;
7373
qspi_pins.io3 = GPIO_PIN_PA6_QUADSPI_BK1_IO3;
7474

75-
stm32l4_qspi_create(&sflash->qspi, QSPI_INSTANCE_QUADSPI, &qspi_pins, 2, QSPI_MODE_DMA);
75+
// ### FIXME: IRQ LEVEL needs to be adjusted !!!
76+
stm32l4_qspi_create(&sflash->qspi, QSPI_INSTANCE_QUADSPI, &qspi_pins, 11, QSPI_MODE_DMA);
7677
stm32l4_qspi_enable(&sflash->qspi, 40000000, QSPI_OPTION_MODE_3, NULL, NULL, 0);
7778

7879
stm32l4_qspi_select(&sflash->qspi);

system/libstm32l4_dragonfly/stm32l4_exti.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bool stm32l4_exti_resume(stm32l4_exti_t *exti, uint32_t mask)
154154

155155
bool stm32l4_exti_notify(stm32l4_exti_t *exti, uint16_t pin, uint32_t control, stm32l4_exti_callback_t callback, void *context)
156156
{
157-
unsigned int mask, index, group, o_group;
157+
unsigned int mask, index, group;
158158

159159
if (exti->state != EXTI_STATE_READY)
160160
{

system/libstm32l4_dragonfly/stm32l4_gpio.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -786,7 +786,11 @@ static inline void stm32l4_gpio_pin_write(unsigned int pin, unsigned int data)
786786

787787
GPIO = (GPIO_TypeDef *)(GPIOA_BASE + (GPIOB_BASE - GPIOA_BASE) * group);
788788

789-
GPIO->BSRR = (data ? 0x00000001 : 0x00010000) << index;
789+
if (data) {
790+
GPIO->BSRR = (1ul << index);
791+
} else {
792+
GPIO->BRR = (1ul << index);
793+
}
790794
}
791795

792796
static inline unsigned int stm32l4_gpio_pin_read(unsigned int pin)
1.91 KB
Binary file not shown.

0 commit comments

Comments
 (0)