123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593 |
- #include <stdint.h>
- #include "system_stm32f10x.h"
- #include "system_stm32f10x.c"
- #define GPIO_Pin_0 ((uint16_t)0x0001) /*!< Pin 0 selected */
- #define GPIO_Pin_1 ((uint16_t)0x0002) /*!< Pin 1 selected */
- #define GPIO_Pin_2 ((uint16_t)0x0004) /*!< Pin 2 selected */
- #define GPIO_Pin_3 ((uint16_t)0x0008) /*!< Pin 3 selected */
- #define GPIO_Pin_4 ((uint16_t)0x0010) /*!< Pin 4 selected */
- #define GPIO_Pin_5 ((uint16_t)0x0020) /*!< Pin 5 selected */
- #define GPIO_Pin_6 ((uint16_t)0x0040) /*!< Pin 6 selected */
- #define GPIO_Pin_7 ((uint16_t)0x0080) /*!< Pin 7 selected */
- #define GPIO_Pin_8 ((uint16_t)0x0100) /*!< Pin 8 selected */
- #define GPIO_Pin_9 ((uint16_t)0x0200) /*!< Pin 9 selected */
- #define GPIO_Pin_10 ((uint16_t)0x0400) /*!< Pin 10 selected */
- #define GPIO_Pin_11 ((uint16_t)0x0800) /*!< Pin 11 selected */
- #define GPIO_Pin_12 ((uint16_t)0x1000) /*!< Pin 12 selected */
- #define GPIO_Pin_13 ((uint16_t)0x2000) /*!< Pin 13 selected */
- #define GPIO_Pin_14 ((uint16_t)0x4000) /*!< Pin 14 selected */
- #define GPIO_Pin_15 ((uint16_t)0x8000) /*!< Pin 15 selected */
- #define GPIO_Pin_All ((uint16_t)0xFFFF) /*!< All pins selected */
- #define SPI_Direction_2Lines_FullDuplex ((uint16_t)0x0000)
- #define SPI_Mode_Master ((uint16_t)0x0104)
- #define SPI_DataSize_8b ((uint16_t)0x0000)
- #define SPI_CPOL_High ((uint16_t)0x0002)
- #define SPI_CPHA_2Edge ((uint16_t)0x0001)
- #define SPI_NSS_Soft ((uint16_t)0x0200)
- #define SPI_NSS_Hard ((uint16_t)0x0000)
- #define IS_SPI_NSS(NSS) (((NSS) == SPI_NSS_Soft) || ((NSS) == SPI_NSS_Hard))
- #define SPI_BaudRatePrescaler_2 ((uint16_t)0x0000)
- #define SPI_BaudRatePrescaler_4 ((uint16_t)0x0008)
- #define SPI_BaudRatePrescaler_8 ((uint16_t)0x0010)
- #define SPI_BaudRatePrescaler_16 ((uint16_t)0x0018)
- #define SPI_BaudRatePrescaler_32 ((uint16_t)0x0020)
- #define SPI_BaudRatePrescaler_64 ((uint16_t)0x0028)
- #define SPI_BaudRatePrescaler_128 ((uint16_t)0x0030)
- #define SPI_BaudRatePrescaler_256 ((uint16_t)0x0038)
- #define IS_SPI_BAUDRATE_PRESCALER(PRESCALER) (((PRESCALER) == SPI_BaudRatePrescaler_2) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_4) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_8) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_16) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_32) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_64) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_128) || \
- ((PRESCALER) == SPI_BaudRatePrescaler_256))
- #define SPI_FirstBit_MSB ((uint16_t)0x0000)
- #define SPI_FirstBit_LSB ((uint16_t)0x0080)
- #define IS_SPI_FIRST_BIT(BIT) (((BIT) == SPI_FirstBit_MSB) || ((BIT) == SPI_FirstBit_LSB))
- #define SD_SPI SPI1
- #define SD_SPI_CLK RCC_APB2Periph_SPI1
- #define SD_SPI_SCK_PIN GPIO_Pin_5 /* PA.05 */
- #define SD_SPI_SCK_GPIO_PORT GPIOA /* GPIOA */
- #define SD_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA
- #define SD_SPI_MISO_PIN GPIO_Pin_6 /* PA.06 */
- #define SD_SPI_MISO_GPIO_PORT GPIOA /* GPIOA */
- #define SD_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
- #define SD_SPI_MOSI_PIN GPIO_Pin_7 /* PA.07 */
- #define SD_SPI_MOSI_GPIO_PORT GPIOA /* GPIOA */
- #define SD_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
- #define SD_CS_PIN GPIO_Pin_4 /* PC.12 */
- #define SD_CS_GPIO_PORT GPIOA /* GPIOC */
- #define SD_CS_GPIO_CLK RCC_APB2Periph_GPIOA
- #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004)
- #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000)
- #define IS_RCC_APB2_PERIPH(PERIPH) ((((PERIPH) & 0xFFC00002) == 0x00) && ((PERIPH) != 0x00))
- #define CR1_CLEAR_Mask ((uint16_t)0x3040)
- #define SPI_Mode_Select ((uint16_t)0xF7FF)
- #define CR1_SPE_Set ((uint16_t)0x0040)
- #define CR1_SPE_Reset ((uint16_t)0xFFBF)
- #define SD_CS_LOW() GPIO_ResetBits(SD_CS_GPIO_PORT, SD_CS_PIN)
- #define SD_CS_HIGH() GPIO_SetBits(SD_CS_GPIO_PORT, SD_CS_PIN)
- #define SD_DUMMY_BYTE 0xFF
- #define SPI_I2S_FLAG_RXNE ((uint16_t)0x0001)
- #define SPI_I2S_FLAG_TXE ((uint16_t)0x0002)
- #define I2S_FLAG_CHSIDE ((uint16_t)0x0004)
- #define I2S_FLAG_UDR ((uint16_t)0x0008)
- #define SPI_FLAG_CRCERR ((uint16_t)0x0010)
- #define SPI_FLAG_MODF ((uint16_t)0x0020)
- #define SPI_I2S_FLAG_OVR ((uint16_t)0x0040)
- #define SPI_I2S_FLAG_BSY ((uint16_t)0x0080)
- #define IS_SPI_I2S_CLEAR_FLAG(FLAG) (((FLAG) == SPI_FLAG_CRCERR))
- #define IS_SPI_I2S_GET_FLAG(FLAG) (((FLAG) == SPI_I2S_FLAG_BSY) || ((FLAG) == SPI_I2S_FLAG_OVR) || \
- ((FLAG) == SPI_FLAG_MODF) || ((FLAG) == SPI_FLAG_CRCERR) || \
- ((FLAG) == I2S_FLAG_UDR) || ((FLAG) == I2S_FLAG_CHSIDE) || \
- ((FLAG) == SPI_I2S_FLAG_TXE) || ((FLAG) == SPI_I2S_FLAG_RXNE))
- #ifdef USE_FULL_ASSERT
- #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__))
- void assert_failed(uint8_t* file, uint32_t line);
- #else
- #define assert_param(expr) ((void)0)
- #endif /* USE_FULL_ASSERT */
- int status = 0;
- volatile long int aa=0, bb;
- typedef struct
- {
- uint32_t SYSCLK_Frequency; /*!< returns SYSCLK clock frequency expressed in Hz */
- uint32_t HCLK_Frequency; /*!< returns HCLK clock frequency expressed in Hz */
- uint32_t PCLK1_Frequency; /*!< returns PCLK1 clock frequency expressed in Hz */
- uint32_t PCLK2_Frequency; /*!< returns PCLK2 clock frequency expressed in Hz */
- uint32_t ADCCLK_Frequency; /*!< returns ADCCLK clock frequency expressed in Hz */
- }RCC_ClocksTypeDef;
- typedef struct
- {
- uint16_t SPI_Direction; /*!< Specifies the SPI unidirectional or bidirectional data mode.
- This parameter can be a value of @ref SPI_data_direction */
- uint16_t SPI_Mode; /*!< Specifies the SPI operating mode.
- This parameter can be a value of @ref SPI_mode */
- uint16_t SPI_DataSize; /*!< Specifies the SPI data size.
- This parameter can be a value of @ref SPI_data_size */
- uint16_t SPI_CPOL; /*!< Specifies the serial clock steady state.
- This parameter can be a value of @ref SPI_Clock_Polarity */
- uint16_t SPI_CPHA; /*!< Specifies the clock active edge for the bit capture.
- This parameter can be a value of @ref SPI_Clock_Phase */
- uint16_t SPI_NSS; /*!< Specifies whether the NSS signal is managed by
- hardware (NSS pin) or by software using the SSI bit.
- This parameter can be a value of @ref SPI_Slave_Select_management */
-
- uint16_t SPI_BaudRatePrescaler; /*!< Specifies the Baud Rate prescaler value which will be
- used to configure the transmit and receive SCK clock.
- This parameter can be a value of @ref SPI_BaudRate_Prescaler.
- @note The communication clock is derived from the master
- clock. The slave clock does not need to be set. */
- uint16_t SPI_FirstBit; /*!< Specifies whether data transfers start from MSB or LSB bit.
- This parameter can be a value of @ref SPI_MSB_LSB_transmission */
- uint16_t SPI_CRCPolynomial; /*!< Specifies the polynomial used for the CRC calculation. */
- }SPI_InitTypeDef;
- typedef enum
- { Bit_RESET = 0,
- Bit_SET
- }BitAction;
- typedef enum
- {
- GPIO_Speed_10MHz = 1,
- GPIO_Speed_2MHz,
- GPIO_Speed_50MHz
- }GPIOSpeed_TypeDef;
- /**
- * @brief Configuration Mode enumeration
- */
- typedef enum
- { GPIO_Mode_AIN = 0x0,
- GPIO_Mode_IN_FLOATING = 0x04,
- GPIO_Mode_IPD = 0x28,
- GPIO_Mode_IPU = 0x48,
- GPIO_Mode_Out_OD = 0x14,
- GPIO_Mode_Out_PP = 0x10,
- GPIO_Mode_AF_OD = 0x1C,
- GPIO_Mode_AF_PP = 0x18
- }GPIOMode_TypeDef;
- typedef struct
- {
- uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
- This parameter can be any value of @ref GPIO_pins_define */
- GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
- This parameter can be a value of @ref GPIOSpeed_TypeDef */
- GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
- This parameter can be a value of @ref GPIOMode_TypeDef */
- }GPIO_InitTypeDef;
- void SD_LowLevel_Init(void);
- void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks);
- void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);
- void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
- void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);
- void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
- void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct);
- void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState);
- void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);
- uint8_t SD_WriteByte(uint8_t byte);
- FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG);
- void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data);
- uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx);
- FlagStatus SPI_I2S_GetFlagStatus(SPI_TypeDef* SPIx, uint16_t SPI_I2S_FLAG)
- {
- FlagStatus bitstatus = RESET;
- /* Check the parameters */
- assert_param(IS_SPI_ALL_PERIPH(SPIx));
- assert_param(IS_SPI_I2S_GET_FLAG(SPI_I2S_FLAG));
- /* Check the status of the specified SPI/I2S flag */
- if ((SPIx->SR & SPI_I2S_FLAG) != (uint16_t)RESET)
- {
- /* SPI_I2S_FLAG is set */
- bitstatus = SET;
- }
- else
- {
- /* SPI_I2S_FLAG is reset */
- bitstatus = RESET;
- }
- /* Return the SPI_I2S_FLAG status */
- return bitstatus;
- }
- void SPI_I2S_SendData(SPI_TypeDef* SPIx, uint16_t Data)
- {
- /* Check the parameters */
- assert_param(IS_SPI_ALL_PERIPH(SPIx));
-
- /* Write in the DR register the data to be sent */
- SPIx->DR = Data;
- }
- uint16_t SPI_I2S_ReceiveData(SPI_TypeDef* SPIx)
- {
- /* Check the parameters */
- assert_param(IS_SPI_ALL_PERIPH(SPIx));
-
- /* Return the data in the DR register */
- return SPIx->DR;
- }
- uint8_t SD_WriteByte(uint8_t Data)
- {
- /*!< Wait until the transmit buffer is empty */
- while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET)
- {
- }
-
- /*!< Send the byte */
- SPI_I2S_SendData(SD_SPI, Data);
-
- /*!< Wait to receive a byte*/
- while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
- {
- }
-
- /*!< Return the byte read from the SPI bus */
- return SPI_I2S_ReceiveData(SD_SPI);
- }
- void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
- {
- uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00;
- uint32_t tmpreg = 0x00, pinmask = 0x00;
- /* Check the parameters */
- assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
- assert_param(IS_GPIO_MODE(GPIO_InitStruct->GPIO_Mode));
- assert_param(IS_GPIO_PIN(GPIO_InitStruct->GPIO_Pin));
-
- /*---------------------------- GPIO Mode Configuration -----------------------*/
- currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F);
- if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00)
- {
- /* Check the parameters */
- assert_param(IS_GPIO_SPEED(GPIO_InitStruct->GPIO_Speed));
- /* Output mode */
- currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed;
- }
- /*---------------------------- GPIO CRL Configuration ------------------------*/
- /* Configure the eight low port pins */
- if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00)
- {
- tmpreg = GPIOx->CRL;
- for (pinpos = 0x00; pinpos < 0x08; pinpos++)
- {
- pos = ((uint32_t)0x01) << pinpos;
- /* Get the port pins position */
- currentpin = (GPIO_InitStruct->GPIO_Pin) & pos;
- if (currentpin == pos)
- {
- pos = pinpos << 2;
- /* Clear the corresponding low control register bits */
- pinmask = ((uint32_t)0x0F) << pos;
- tmpreg &= ~pinmask;
- /* Write the mode configuration in the corresponding bits */
- tmpreg |= (currentmode << pos);
- /* Reset the corresponding ODR bit */
- if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
- {
- GPIOx->BRR = (((uint32_t)0x01) << pinpos);
- }
- else
- {
- /* Set the corresponding ODR bit */
- if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
- {
- GPIOx->BSRR = (((uint32_t)0x01) << pinpos);
- }
- }
- }
- }
- GPIOx->CRL = tmpreg;
- }
- /*---------------------------- GPIO CRH Configuration ------------------------*/
- /* Configure the eight high port pins */
- if (GPIO_InitStruct->GPIO_Pin > 0x00FF)
- {
- tmpreg = GPIOx->CRH;
- for (pinpos = 0x00; pinpos < 0x08; pinpos++)
- {
- pos = (((uint32_t)0x01) << (pinpos + 0x08));
- /* Get the port pins position */
- currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos);
- if (currentpin == pos)
- {
- pos = pinpos << 2;
- /* Clear the corresponding high control register bits */
- pinmask = ((uint32_t)0x0F) << pos;
- tmpreg &= ~pinmask;
- /* Write the mode configuration in the corresponding bits */
- tmpreg |= (currentmode << pos);
- /* Reset the corresponding ODR bit */
- if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD)
- {
- GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08));
- }
- /* Set the corresponding ODR bit */
- if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU)
- {
- GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08));
- }
- }
- }
- GPIOx->CRH = tmpreg;
- }
- }
- void SPI_Init(SPI_TypeDef* SPIx, SPI_InitTypeDef* SPI_InitStruct)
- {
- uint16_t tmpreg = 0;
-
- /* check the parameters */
- assert_param(IS_SPI_ALL_PERIPH(SPIx));
-
- /* Check the SPI parameters */
- assert_param(IS_SPI_DIRECTION_MODE(SPI_InitStruct->SPI_Direction));
- assert_param(IS_SPI_MODE(SPI_InitStruct->SPI_Mode));
- assert_param(IS_SPI_DATASIZE(SPI_InitStruct->SPI_DataSize));
- assert_param(IS_SPI_CPOL(SPI_InitStruct->SPI_CPOL));
- assert_param(IS_SPI_CPHA(SPI_InitStruct->SPI_CPHA));
- assert_param(IS_SPI_NSS(SPI_InitStruct->SPI_NSS));
- assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_InitStruct->SPI_BaudRatePrescaler));
- assert_param(IS_SPI_FIRST_BIT(SPI_InitStruct->SPI_FirstBit));
- assert_param(IS_SPI_CRC_POLYNOMIAL(SPI_InitStruct->SPI_CRCPolynomial));
- /*---------------------------- SPIx CR1 Configuration ------------------------*/
- /* Get the SPIx CR1 value */
- tmpreg = SPIx->CR1;
- /* Clear BIDIMode, BIDIOE, RxONLY, SSM, SSI, LSBFirst, BR, MSTR, CPOL and CPHA bits */
- tmpreg &= CR1_CLEAR_Mask;
- /* Configure SPIx: direction, NSS management, first transmitted bit, BaudRate prescaler
- master/salve mode, CPOL and CPHA */
- /* Set BIDImode, BIDIOE and RxONLY bits according to SPI_Direction value */
- /* Set SSM, SSI and MSTR bits according to SPI_Mode and SPI_NSS values */
- /* Set LSBFirst bit according to SPI_FirstBit value */
- /* Set BR bits according to SPI_BaudRatePrescaler value */
- /* Set CPOL bit according to SPI_CPOL value */
- /* Set CPHA bit according to SPI_CPHA value */
- tmpreg |= (uint16_t)((uint32_t)SPI_InitStruct->SPI_Direction | SPI_InitStruct->SPI_Mode |
- SPI_InitStruct->SPI_DataSize | SPI_InitStruct->SPI_CPOL |
- SPI_InitStruct->SPI_CPHA | SPI_InitStruct->SPI_NSS |
- SPI_InitStruct->SPI_BaudRatePrescaler | SPI_InitStruct->SPI_FirstBit);
- /* Write to SPIx CR1 */
- SPIx->CR1 = tmpreg;
-
- /* Activate the SPI mode (Reset I2SMOD bit in I2SCFGR register) */
- SPIx->I2SCFGR &= SPI_Mode_Select;
- /*---------------------------- SPIx CRCPOLY Configuration --------------------*/
- /* Write to SPIx CRCPOLY */
- SPIx->CRCPR = SPI_InitStruct->SPI_CRCPolynomial;
- }
- void SPI_Cmd(SPI_TypeDef* SPIx, FunctionalState NewState)
- {
- /* Check the parameters */
- assert_param(IS_SPI_ALL_PERIPH(SPIx));
- assert_param(IS_FUNCTIONAL_STATE(NewState));
- if (NewState != DISABLE)
- {
- /* Enable the selected SPI peripheral */
- SPIx->CR1 |= CR1_SPE_Set;
- }
- else
- {
- /* Disable the selected SPI peripheral */
- SPIx->CR1 &= CR1_SPE_Reset;
- }
- }
- void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
- {
- /* Check the parameters */
- assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
- assert_param(IS_GPIO_PIN(GPIO_Pin));
-
- GPIOx->BSRR = GPIO_Pin;
- }
- void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState)
- {
- /* Check the parameters */
- assert_param(IS_RCC_APB2_PERIPH(RCC_APB2Periph));
- assert_param(IS_FUNCTIONAL_STATE(NewState));
- if (NewState != DISABLE)
- {
- RCC->APB2ENR |= RCC_APB2Periph;
- }
- else
- {
- RCC->APB2ENR &= ~RCC_APB2Periph;
- }
- }
- void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
- {
- /* Check the parameters */
- assert_param(IS_GPIO_ALL_PERIPH(GPIOx));
- assert_param(IS_GPIO_PIN(GPIO_Pin));
-
- GPIOx->BRR = GPIO_Pin;
- }
- void SD_LowLevel_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- /*!< SD_SPI_CS_GPIO, SD_SPI_MOSI_GPIO, SD_SPI_MISO_GPIO, SD_SPI_DETECT_GPIO
- and SD_SPI_SCK_GPIO Periph clock enable */
- RCC_APB2PeriphClockCmd(SD_CS_GPIO_CLK | SD_SPI_MOSI_GPIO_CLK | SD_SPI_MISO_GPIO_CLK |
- SD_SPI_SCK_GPIO_CLK, ENABLE);
- /*!< SD_SPI Periph clock enable */
- RCC_APB2PeriphClockCmd(SD_SPI_CLK, ENABLE);
-
- /*!< Configure SD_SPI pins: SCK */
- GPIO_InitStructure.GPIO_Pin = SD_SPI_SCK_PIN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(SD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
- /*!< Configure SD_SPI pins: MOSI */
- GPIO_InitStructure.GPIO_Pin = SD_SPI_MOSI_PIN;
- GPIO_Init(SD_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
- /*!< Configure SD_SPI pins: MISO */
- GPIO_InitStructure.GPIO_Pin = SD_SPI_MISO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(SD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
-
- /*!< Configure SD_SPI_CS_PIN pin: SD Card CS pin */
- GPIO_InitStructure.GPIO_Pin = SD_CS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructure);
- /*!< SD_SPI Config */
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SD_SPI, &SPI_InitStructure);
-
- SPI_Cmd(SD_SPI, ENABLE); /*!< SD_SPI enable */
- }
- void init_alt(){
- RCC->APB2ENR |= RCC_APB2Periph_GPIOA;
- }
- int main(void) {
- SystemCoreClockUpdate();
- SysTick_Config(SystemCoreClock/100);
- SD_LowLevel_Init();
- while (1) {
- bb=SystemCoreClock;
- }
- }
- void test(){
- if (status == 0){
- SD_CS_HIGH();
- status=1;
- }
- else
- {
- SD_CS_LOW();
- status=0;
- }
- }
- void SysTick_Handler(void) {
- //test();
- SD_CS_HIGH();
- for (int i = 0; i <= 9; i++)
- {
- /*!< Send dummy byte 0xFF */
- // SD_WriteByte(SD_DUMMY_BYTE);
- SPI1->DR = 0xFF;
- }
- // SD_CS_LOW();
- aa++;
- }
|