Advertisement
Guest User

Untitled

a guest
Aug 27th, 2012
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.96 KB | None | 0 0
  1. #define SD_SPI                           SPI1
  2. #define SD_SPI_CLK                       RCC_APB2Periph_SPI1
  3. #define SD_SPI_SCK_PIN                   GPIO_Pin_5                  /* PA.05 */
  4. #define SD_SPI_SCK_GPIO_PORT             GPIOA                       /* GPIOA */
  5. #define SD_SPI_SCK_GPIO_CLK              RCC_APB2Periph_GPIOA
  6. #define SD_SPI_MISO_PIN                  GPIO_Pin_6                  /* PA.06 */
  7. #define SD_SPI_MISO_GPIO_PORT            GPIOA                       /* GPIOA */
  8. #define SD_SPI_MISO_GPIO_CLK             RCC_APB2Periph_GPIOA
  9. #define SD_SPI_MOSI_PIN                  GPIO_Pin_7                  /* PA.07 */
  10. #define SD_SPI_MOSI_GPIO_PORT            GPIOA                       /* GPIOA */
  11. #define SD_SPI_MOSI_GPIO_CLK             RCC_APB2Periph_GPIOA
  12. #define SD_CS_PIN                        GPIO_Pin_4                  /* PA.04 */
  13. #define SD_CS_GPIO_PORT                  GPIOA                       /* GPIOA */
  14. #define SD_CS_GPIO_CLK                   RCC_APB2Periph_GPIOA
  15. #define SD_DETECT_PIN                    GPIO_Pin_3                  /* PA.03 */
  16. #define SD_DETECT_GPIO_PORT              GPIOA                       /* GPIOA */
  17. #define SD_DETECT_GPIO_CLK               RCC_APB2Periph_GPIOA
  18.  
  19.  
  20. void SD_LowLevel_Init(void)
  21. {
  22.     GPIO_InitTypeDef  GPIO_InitStructure;
  23.     SPI_InitTypeDef   SPI_InitStructure;
  24.  
  25.     /*!< SD_SPI_CS_GPIO, SD_SPI_MOSI_GPIO, SD_SPI_MISO_GPIO, SD_SPI_DETECT_GPIO
  26.        and SD_SPI_SCK_GPIO Periph clock enable */
  27.     RCC_APB2PeriphClockCmd(SD_CS_GPIO_CLK | SD_SPI_MOSI_GPIO_CLK | SD_SPI_MISO_GPIO_CLK |
  28.                          SD_SPI_SCK_GPIO_CLK | SD_DETECT_GPIO_CLK, ENABLE);
  29.  
  30.     /*!< SD_SPI Periph clock enable */
  31.     RCC_APB1PeriphClockCmd(SD_SPI_CLK, ENABLE);
  32.     /*!< AFIO Periph clock enable */
  33.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
  34.  
  35.     /*!< Configure SD_SPI pins: SCK */
  36.     GPIO_InitStructure.GPIO_Pin = SD_SPI_SCK_PIN;
  37.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  38.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  39.     GPIO_Init(SD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
  40.  
  41.     /*!< Configure SD_SPI pins: MOSI */
  42.     GPIO_InitStructure.GPIO_Pin = SD_SPI_MOSI_PIN;
  43.     GPIO_Init(SD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
  44.  
  45.     /*!< Configure SD_SPI pins: MISO */
  46.     GPIO_InitStructure.GPIO_Pin = SD_SPI_MISO_PIN;
  47.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
  48.     GPIO_Init(SD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
  49.  
  50.     /*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */
  51.     GPIO_InitStructure.GPIO_Pin = SD_CS_PIN;
  52.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  53.     GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructure);
  54.  
  55.     /*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */
  56.     GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
  57.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  58.     GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
  59.  
  60.     /* De-select the Card: Chip Select high */  
  61.     SD_CS_HIGH();
  62.  
  63.     /*!< SD_SPI Config */
  64.     SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  65.     SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  66.     SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  67.     SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
  68.     SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  69.     SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  70.     SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  71.     SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  72.     SPI_InitStructure.SPI_CRCPolynomial = 7;
  73.     SPI_Init(SD_SPI, &SPI_InitStructure);
  74.  
  75.     SPI_Cmd(SD_SPI, ENABLE); /*!< SD_SPI enable */
  76. }
  77.  
  78.  
  79. uint8_t SD_WriteByte(uint8_t Data)
  80. {
  81.     /*!< Wait until the transmit buffer is empty */
  82.     while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET);
  83.  
  84.     /*!< Send the byte */
  85.     SPI_I2S_SendData(SD_SPI, Data);
  86.  
  87.     /*!< Wait to receive a byte*/
  88.     while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET);
  89.     /*!< Return the byte read from the SPI bus */
  90.     return SPI_I2S_ReceiveData(SD_SPI);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement