9
9
* See the file COPYING for more details, or visit <http://unlicense.org>.
10
10
*/
11
11
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 */
21
20
#endif
21
+ #define SPI_PIN_SPEED _50MHz
22
22
23
23
#if 0
24
24
#define TRC (f , a ...) printk("SD: " f, ## a)
@@ -66,13 +66,13 @@ static void spi_release(void)
66
66
67
67
static uint8_t wait_ready (void )
68
68
{
69
- stk_time_t start = stk_now ();
69
+ time_t start = time_now ();
70
70
uint8_t res ;
71
71
72
72
/* Wait 500ms for card to be ready. */
73
73
do {
74
74
res = spi_recv8 (spi );
75
- } while ((res != 0xff ) && (stk_timesince (start ) < stk_ms (500 )));
75
+ } while ((res != 0xff ) && (time_since (start ) < time_ms (500 )));
76
76
77
77
return res ;
78
78
}
@@ -172,13 +172,13 @@ static uint8_t send_cmd(uint8_t cmd, uint32_t arg)
172
172
static bool_t datablock_recv (BYTE * buff , uint16_t bytes )
173
173
{
174
174
uint8_t token , _crc [2 ];
175
- uint32_t start = stk_now ();
176
175
uint16_t todo , w , crc ;
176
+ time_t start = time_now ();
177
177
178
178
/* Wait 100ms for data to be ready. */
179
179
do {
180
180
token = spi_recv8 (spi );
181
- } while ((token == 0xff ) && (stk_timesince (start ) < stk_ms (100 )));
181
+ } while ((token == 0xff ) && (time_since (start ) < time_ms (100 )));
182
182
if (token != 0xfe ) /* valid data token? */
183
183
return FALSE;
184
184
@@ -270,9 +270,18 @@ static bool_t sd_inserted(void)
270
270
return gpio_read_pin (gpioc , 9 );
271
271
}
272
272
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
+
273
281
static DSTATUS sd_disk_initialize (BYTE pdrv )
274
282
{
275
- uint32_t start , cr1 ;
283
+ time_t start ;
284
+ uint32_t cr1 ;
276
285
uint16_t rcv ;
277
286
uint8_t i ;
278
287
@@ -288,16 +297,24 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
288
297
289
298
/* Enable external I/O pins. */
290
299
gpio_configure_pin (gpiob , PIN_CS , GPO_pushpull (SPI_PIN_SPEED , HIGH ));
300
+ #if MCU == STM32F105
291
301
gpio_configure_pin (gpiob , 13 , AFO_pushpull (SPI_PIN_SPEED )); /* CK */
292
302
gpio_configure_pin (gpiob , 14 , GPI_pull_up ); /* MISO */
293
303
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
294
312
295
313
/* Configure SPI: 8-bit mode, MSB first, CPOL Low, CPHA Leading Edge. */
296
- spi -> cr2 = 0 ;
297
314
cr1 = (SPI_CR1_MSTR | /* master */
298
315
SPI_CR1_SSM | SPI_CR1_SSI | /* software NSS */
299
316
SPI_CR1_SPE );
300
- spi -> cr1 = cr1 | SPI_CR1_BR_DIV128 ; /* ~281kHz (<400kHz) */
317
+ sd_spi_set_cr ( cr1 , INIT_SPEED_DIV );
301
318
302
319
/* Drain SPI I/O. */
303
320
spi_quiesce (spi );
@@ -328,9 +345,9 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
328
345
}
329
346
330
347
/* Request SDHC/SDXC and start card initialisation. */
331
- start = stk_now ();
348
+ start = time_now ();
332
349
while (send_cmd (ACMD (41 ), 1u <<30 )) {
333
- if (stk_timesince (start ) >= stk_ms (1000 ))
350
+ if (time_since (start ) >= time_ms (1000 ))
334
351
goto out ; /* initialisation timeout */
335
352
}
336
353
@@ -359,9 +376,9 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
359
376
}
360
377
361
378
/* Wait for card initialisation. */
362
- start = stk_now ();
379
+ start = time_now ();
363
380
while (send_cmd (cmd , 0 )) {
364
- if (stk_timesince (start ) >= stk_ms (1000 ))
381
+ if (time_since (start ) >= time_ms (1000 ))
365
382
goto out ; /* initialisation timeout */
366
383
}
367
384
@@ -379,7 +396,7 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
379
396
380
397
if (!(status & STA_NOINIT )) {
381
398
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 ) ;
383
400
printk ("SD Card configured\n" );
384
401
dump_cid_info ();
385
402
} else {
0 commit comments