1 | ///////////////////////////////////////////////////////////////////////////
|
2 | /// Initialize MMC interface
|
3 | ///////////////////////////////////////////////////////////////////////////
|
4 | static void init_spi(void)
|
5 | {
|
6 | GPIO_InitTypeDef oGpioStruct;
|
7 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
|
8 |
|
9 | // Card Detect Pin
|
10 | oGpioStruct.GPIO_Pin = GPIO_Pin_0;
|
11 | oGpioStruct.GPIO_Mode = GPIO_Mode_IN;
|
12 | oGpioStruct.GPIO_Speed = GPIO_Speed_2MHz;
|
13 | oGpioStruct.GPIO_OType = GPIO_OType_PP;
|
14 | oGpioStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
15 | GPIO_Init(GPIOB, &oGpioStruct);
|
16 |
|
17 | // Chip Select
|
18 | oGpioStruct.GPIO_Pin = GPIO_Pin_12;
|
19 | oGpioStruct.GPIO_Mode = GPIO_Mode_OUT;
|
20 | oGpioStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
21 | oGpioStruct.GPIO_OType = GPIO_OType_PP;
|
22 | oGpioStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
23 | GPIO_Init(GPIOB, &oGpioStruct);
|
24 |
|
25 | // SPI
|
26 | oGpioStruct.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
27 | oGpioStruct.GPIO_Mode = GPIO_Mode_AF;
|
28 | oGpioStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
29 | oGpioStruct.GPIO_OType = GPIO_OType_PP;
|
30 | oGpioStruct.GPIO_PuPd = GPIO_PuPd_UP;
|
31 | GPIO_Init(GPIOB, &oGpioStruct);
|
32 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);
|
33 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0);
|
34 | GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
|
35 |
|
36 |
|
37 | SPI_InitTypeDef SPI_InitStructure;
|
38 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
39 | SPI_I2S_DeInit(SPI2);
|
40 | SPI_StructInit(&SPI_InitStructure);
|
41 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
42 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
43 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
44 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
45 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
46 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
|
47 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
48 | SPI_InitStructure.SPI_CRCPolynomial = 7;
|
49 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
50 | SPI_Init(SPI2, &SPI_InitStructure);
|
51 | SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF); // Set RXNE Flag at 8-Bit
|
52 | SPI_Cmd(SPI2, ENABLE);
|
53 |
|
54 | CS_HIGH(); // Set CS# high
|
55 |
|
56 | for(m__u32Timer1 = 10; m__u32Timer1; ); // Wait for 10ms
|
57 | }
|
58 |
|
59 |
|
60 | ///////////////////////////////////////////////////////////////////////////
|
61 | /// Exchange a byte
|
62 | ///
|
63 | /// \param dat Data to send
|
64 | ///////////////////////////////////////////////////////////////////////////
|
65 | inline static BYTE xchg_spi(BYTE dat)
|
66 | {
|
67 | const uint32_t spixbase = (uint32_t)SPI2 + 0x0C;
|
68 | while ((SPI2->SR & SPI_I2S_FLAG_TXE) == 0);
|
69 | *(__IO uint8_t *) spixbase = dat;
|
70 | while ((SPI2->SR & SPI_I2S_FLAG_RXNE) == 0); // Wait for end of the transaction
|
71 | return (*(__IO uint8_t *) spixbase); // Return received byte
|
72 | }
|
73 |
|
74 |
|
75 | ///////////////////////////////////////////////////////////////////////////
|
76 | /// Receive multiple byte
|
77 | ///
|
78 | /// \param buff Pointer to data buffer
|
79 | /// \param btr Number of bytes to receive (even number)
|
80 | ///////////////////////////////////////////////////////////////////////////
|
81 | static void rcvr_spi_multi(BYTE *buff, UINT btr)
|
82 | {
|
83 | do
|
84 | {
|
85 | *buff++ = xchg_spi(0xFF);
|
86 | *buff++ = xchg_spi(0xFF);
|
87 | }
|
88 | while(btr -= 2);
|
89 | }
|
90 |
|
91 |
|
92 | ///////////////////////////////////////////////////////////////////////////
|
93 | /// Send multiple byte
|
94 | ///
|
95 | /// \param buff Pointer to the data
|
96 | /// \param btx Number of bytes to send (even number)
|
97 | ///////////////////////////////////////////////////////////////////////////
|
98 | #if _USE_WRITE
|
99 | static void xmit_spi_multi(const BYTE *buff, UINT btx)
|
100 | {
|
101 | do
|
102 | {
|
103 | (void)xchg_spi(*buff++);
|
104 | (void)xchg_spi(*buff++);
|
105 | }
|
106 | while(btx -= 2);
|
107 | }
|
108 | #endif
|