Skip to content

Commit c1f1240

Browse files
committed
AT32F435: Fix SD support over SPI.
1 parent 080063e commit c1f1240

File tree

2 files changed

+49
-30
lines changed

2 files changed

+49
-30
lines changed

inc/mcu/common_regs.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,17 @@ struct spi {
323323
uint32_t i2spr; /* 20: I2S prescaler */
324324
};
325325

326+
#define SPI_BR_DIV2 0
327+
#define SPI_BR_DIV4 1
328+
#define SPI_BR_DIV8 2
329+
#define SPI_BR_DIV16 3
330+
#define SPI_BR_DIV32 4
331+
#define SPI_BR_DIV64 5
332+
#define SPI_BR_DIV128 6
333+
#define SPI_BR_DIV256 7
334+
#define SPI_BR_DIV512 8
335+
#define SPI_BR_DIV1024 9
336+
326337
#define SPI_CR1_BIDIMODE (1u<<15)
327338
#define SPI_CR1_BIDIOE (1u<<14)
328339
#define SPI_CR1_CRCEN (1u<<13)
@@ -333,15 +344,6 @@ struct spi {
333344
#define SPI_CR1_SSI (1u<< 8)
334345
#define SPI_CR1_LSBFIRST (1u<< 7)
335346
#define SPI_CR1_SPE (1u<< 6)
336-
#define SPI_CR1_BR_DIV2 (0u<< 3)
337-
#define SPI_CR1_BR_DIV4 (1u<< 3)
338-
#define SPI_CR1_BR_DIV8 (2u<< 3)
339-
#define SPI_CR1_BR_DIV16 (3u<< 3)
340-
#define SPI_CR1_BR_DIV32 (4u<< 3)
341-
#define SPI_CR1_BR_DIV64 (5u<< 3)
342-
#define SPI_CR1_BR_DIV128 (6u<< 3)
343-
#define SPI_CR1_BR_DIV256 (7u<< 3)
344-
#define SPI_CR1_BR_MASK (7u<< 3)
345347
#define SPI_CR1_MSTR (1u<< 2)
346348
#define SPI_CR1_CPOL (1u<< 1)
347349
#define SPI_CR1_CPHA (1u<< 0)

src/sd_spi.c

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
* See the file COPYING for more details, or visit <http://unlicense.org>.
1010
*/
1111

12-
#if 1
13-
/* We can now switch to Default Speed (25MHz). Closest we can get is 36Mhz/2 =
14-
* 18MHz. */
15-
#define DEFAULT_SPEED_DIV SPI_CR1_BR_DIV2 /* 18MHz */
16-
#define SPI_PIN_SPEED _50MHz
17-
#else
18-
/* Best speed I can reliably achieve right now is 9Mbit/s. */
19-
#define DEFAULT_SPEED_DIV SPI_CR1_BR_DIV4 /* 9MHz */
20-
#define SPI_PIN_SPEED _10MHz
12+
#if MCU == STM32F105
13+
/* Default Speed (25MHz). Closest we can get is 36Mhz/2 = 18MHz. */
14+
#define INIT_SPEED_DIV SPI_BR_DIV128 /* ~281kHz (<400kHz) */
15+
#define DEFAULT_SPEED_DIV SPI_BR_DIV2 /* 18MHz */
16+
#elif MCU == AT32F435
17+
/* Default Speed (25MHz). Closest we can get is 144Mhz/8 = 18MHz. */
18+
#define INIT_SPEED_DIV SPI_BR_DIV512 /* ~281kHz (<400kHz) */
19+
#define DEFAULT_SPEED_DIV SPI_BR_DIV8 /* 18MHz */
2120
#endif
21+
#define SPI_PIN_SPEED _50MHz
2222

2323
#if 0
2424
#define TRC(f, a...) printk("SD: " f, ## a)
@@ -66,13 +66,13 @@ static void spi_release(void)
6666

6767
static uint8_t wait_ready(void)
6868
{
69-
stk_time_t start = stk_now();
69+
time_t start = time_now();
7070
uint8_t res;
7171

7272
/* Wait 500ms for card to be ready. */
7373
do {
7474
res = spi_recv8(spi);
75-
} while ((res != 0xff) && (stk_timesince(start) < stk_ms(500)));
75+
} while ((res != 0xff) && (time_since(start) < time_ms(500)));
7676

7777
return res;
7878
}
@@ -172,13 +172,13 @@ static uint8_t send_cmd(uint8_t cmd, uint32_t arg)
172172
static bool_t datablock_recv(BYTE *buff, uint16_t bytes)
173173
{
174174
uint8_t token, _crc[2];
175-
uint32_t start = stk_now();
176175
uint16_t todo, w, crc;
176+
time_t start = time_now();
177177

178178
/* Wait 100ms for data to be ready. */
179179
do {
180180
token = spi_recv8(spi);
181-
} while ((token == 0xff) && (stk_timesince(start) < stk_ms(100)));
181+
} while ((token == 0xff) && (time_since(start) < time_ms(100)));
182182
if (token != 0xfe) /* valid data token? */
183183
return FALSE;
184184

@@ -270,9 +270,18 @@ static bool_t sd_inserted(void)
270270
return gpio_read_pin(gpioc, 9);
271271
}
272272

273+
static void sd_spi_set_cr(uint32_t cr1, uint32_t br)
274+
{
275+
/* BR is called MDIV in AT32F435 documentation, and is extended by one
276+
* bit which resides in CR2. */
277+
spi->cr2 = (br & 8) << (8 - 3); /* CR2[8] := MDIV[3] */
278+
spi->cr1 = cr1 | ((br & 7) << 3); /* CR1[5:4] := MDIV[2:0] */
279+
}
280+
273281
static DSTATUS sd_disk_initialize(BYTE pdrv)
274282
{
275-
uint32_t start, cr1;
283+
time_t start;
284+
uint32_t cr1;
276285
uint16_t rcv;
277286
uint8_t i;
278287

@@ -288,16 +297,24 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
288297

289298
/* Enable external I/O pins. */
290299
gpio_configure_pin(gpiob, PIN_CS, GPO_pushpull(SPI_PIN_SPEED, HIGH));
300+
#if MCU == STM32F105
291301
gpio_configure_pin(gpiob, 13, AFO_pushpull(SPI_PIN_SPEED)); /* CK */
292302
gpio_configure_pin(gpiob, 14, GPI_pull_up); /* MISO */
293303
gpio_configure_pin(gpiob, 15, AFO_pushpull(SPI_PIN_SPEED)); /* MOSI */
304+
#elif MCU == AT32F435
305+
gpio_set_af(gpiob, 13, 5);
306+
gpio_configure_pin(gpiob, 13, AFO_pushpull(SPI_PIN_SPEED)); /* CK */
307+
gpio_set_af(gpiob, 14, 5);
308+
gpio_configure_pin(gpiob, 14, AFI(PUPD_up)); /* MISO */
309+
gpio_set_af(gpiob, 15, 5);
310+
gpio_configure_pin(gpiob, 15, AFO_pushpull(SPI_PIN_SPEED)); /* MOSI */
311+
#endif
294312

295313
/* Configure SPI: 8-bit mode, MSB first, CPOL Low, CPHA Leading Edge. */
296-
spi->cr2 = 0;
297314
cr1 = (SPI_CR1_MSTR | /* master */
298315
SPI_CR1_SSM | SPI_CR1_SSI | /* software NSS */
299316
SPI_CR1_SPE);
300-
spi->cr1 = cr1 | SPI_CR1_BR_DIV128; /* ~281kHz (<400kHz) */
317+
sd_spi_set_cr(cr1, INIT_SPEED_DIV);
301318

302319
/* Drain SPI I/O. */
303320
spi_quiesce(spi);
@@ -328,9 +345,9 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
328345
}
329346

330347
/* Request SDHC/SDXC and start card initialisation. */
331-
start = stk_now();
348+
start = time_now();
332349
while (send_cmd(ACMD(41), 1u<<30)) {
333-
if (stk_timesince(start) >= stk_ms(1000))
350+
if (time_since(start) >= time_ms(1000))
334351
goto out; /* initialisation timeout */
335352
}
336353

@@ -359,9 +376,9 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
359376
}
360377

361378
/* Wait for card initialisation. */
362-
start = stk_now();
379+
start = time_now();
363380
while (send_cmd(cmd, 0)) {
364-
if (stk_timesince(start) >= stk_ms(1000))
381+
if (time_since(start) >= time_ms(1000))
365382
goto out; /* initialisation timeout */
366383
}
367384

@@ -379,7 +396,7 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
379396

380397
if (!(status & STA_NOINIT)) {
381398
delay_us(10); /* XXX small delay here stops SPI getting stuck?? */
382-
spi->cr1 = cr1 | DEFAULT_SPEED_DIV;
399+
sd_spi_set_cr(cr1, DEFAULT_SPEED_DIV);
383400
printk("SD Card configured\n");
384401
dump_cid_info();
385402
} else {

0 commit comments

Comments
 (0)