|
9 | 9 | * See the file COPYING for more details, or visit <http://unlicense.org>.
|
10 | 10 | */
|
11 | 11 |
|
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 */ |
20 |
| -#endif |
| 12 | +#define PCLK_MHZ APB1_MHZ |
| 13 | +#define INIT_SPEED_KHZ 400 |
| 14 | +#define DEFAULT_SPEED_KHZ 25000 |
21 | 15 | #define SPI_PIN_SPEED _50MHz
|
22 | 16 |
|
23 | 17 | #if 0
|
@@ -270,8 +264,16 @@ static bool_t sd_inserted(void)
|
270 | 264 | return gpio_read_pin(gpioc, 9);
|
271 | 265 | }
|
272 | 266 |
|
273 |
| -static void sd_spi_set_cr(uint32_t cr1, uint32_t br) |
| 267 | +static void sd_spi_set_cr(uint32_t cr1, unsigned int max_khz) |
274 | 268 | {
|
| 269 | + /* Set the divisor to satisfy maximum communications frequency. */ |
| 270 | + uint32_t br = SPI_BR_DIV2; |
| 271 | + unsigned int khz = (PCLK_MHZ * 1000) >> 1; |
| 272 | + while (khz > max_khz) { |
| 273 | + khz >>= 1; |
| 274 | + br++; |
| 275 | + } |
| 276 | + |
275 | 277 | /* BR is called MDIV in AT32F435 documentation, and is extended by one
|
276 | 278 | * bit which resides in CR2. */
|
277 | 279 | spi->cr2 = (br & 8) << (8 - 3); /* CR2[8] := MDIV[3] */
|
@@ -314,7 +316,7 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
|
314 | 316 | cr1 = (SPI_CR1_MSTR | /* master */
|
315 | 317 | SPI_CR1_SSM | SPI_CR1_SSI | /* software NSS */
|
316 | 318 | SPI_CR1_SPE);
|
317 |
| - sd_spi_set_cr(cr1, INIT_SPEED_DIV); |
| 319 | + sd_spi_set_cr(cr1, INIT_SPEED_KHZ); |
318 | 320 |
|
319 | 321 | /* Drain SPI I/O. */
|
320 | 322 | spi_quiesce(spi);
|
@@ -396,7 +398,7 @@ static DSTATUS sd_disk_initialize(BYTE pdrv)
|
396 | 398 |
|
397 | 399 | if (!(status & STA_NOINIT)) {
|
398 | 400 | delay_us(10); /* XXX small delay here stops SPI getting stuck?? */
|
399 |
| - sd_spi_set_cr(cr1, DEFAULT_SPEED_DIV); |
| 401 | + sd_spi_set_cr(cr1, DEFAULT_SPEED_KHZ); |
400 | 402 | printk("SD Card configured\n");
|
401 | 403 | dump_cid_info();
|
402 | 404 | } else {
|
|
0 commit comments