main.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2015 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f1xx_hal.h"
  35. /* USER CODE BEGIN Includes */
  36. #include <string.h>
  37. /* USER CODE END Includes */
  38. /* Private variables ---------------------------------------------------------*/
  39. SPI_HandleTypeDef hspi2;
  40. /* USER CODE BEGIN PV */
  41. /* Private variables ---------------------------------------------------------*/
  42. uint8_t dump[10]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF},
  43. cmd0[8]={0x40,0x00,0x00,0x00,0x00,0x95,0xFF,0xFF},
  44. cmd1[8]={0x41,0x00,0x00,0x00,0x00,0x01,0xFF,0xFF}, response_0[128], response_1[2048], SD_OK_FLAG = 0;
  45. uint32_t response1;
  46. /* USER CODE END PV */
  47. /* Private function prototypes -----------------------------------------------*/
  48. void SystemClock_Config(void);
  49. static void MX_GPIO_Init(void);
  50. static void MX_SPI2_Init(void);
  51. /* USER CODE BEGIN PFP */
  52. /* Private function prototypes -----------------------------------------------*/
  53. /* USER CODE END PFP */
  54. /* USER CODE BEGIN 0 */
  55. /* USER CODE END 0 */
  56. int main(void)
  57. {
  58. /* USER CODE BEGIN 1 */
  59. memset(response_0, 0x03, 4);
  60. memset(response_1, 0x03, 512);
  61. /* USER CODE END 1 */
  62. /* MCU Configuration----------------------------------------------------------*/
  63. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  64. HAL_Init();
  65. /* Configure the system clock */
  66. SystemClock_Config();
  67. /* Initialize all configured peripherals */
  68. MX_GPIO_Init();
  69. MX_SPI2_Init();
  70. /* USER CODE BEGIN 2 */
  71. GPIOB -> BRR = GPIO_PIN_2;
  72. /* USER CODE END 2 */
  73. /* Infinite loop */
  74. /* USER CODE BEGIN WHILE */
  75. for (int k=0; k< 10000; k++);
  76. GPIOB -> BSRR = GPIO_PIN_12;
  77. HAL_SPI_Transmit(&hspi2, dump, 7, 0xF);
  78. // for (int k=0; k< 100; k++);
  79. GPIOB -> BRR = GPIO_PIN_12;
  80. while (1);
  81. HAL_SPI_Transmit(&hspi2, cmd0, 6, 0xF);
  82. HAL_SPI_Receive(&hspi2, response_0, 64, 0xF);
  83. GPIOB -> BSRR = GPIO_PIN_12;
  84. for (int i = 0; i < 64; i++){
  85. if (response_0[i] == 0x01){
  86. SD_OK_FLAG=1;
  87. break;
  88. }
  89. }
  90. if (SD_OK_FLAG != 1){
  91. GPIOB -> BSRR = GPIO_PIN_2;
  92. while (1);
  93. } else {
  94. }
  95. //while(1){
  96. GPIOB -> BRR = GPIO_PIN_12;
  97. // for (int i=0; i<2048;i++){
  98. HAL_SPI_Transmit(&hspi2, cmd1, 6 , 0xFF);
  99. HAL_SPI_Receive(&hspi2, response_1,2048, 0xF);
  100. GPIOB -> BSRR = GPIO_PIN_12;
  101. // for (int k=0; k<10;k++);
  102. //}
  103. while (1)
  104. {
  105. /* USER CODE END WHILE */
  106. /* USER CODE BEGIN 3 */
  107. }
  108. /* USER CODE END 3 */
  109. }
  110. /** System Clock Configuration
  111. */
  112. void SystemClock_Config(void)
  113. {
  114. RCC_OscInitTypeDef RCC_OscInitStruct;
  115. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  116. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  117. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  118. RCC_OscInitStruct.HSICalibrationValue = 16;
  119. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  120. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  121. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  122. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  123. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  124. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV4;
  125. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV8;
  126. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV8;
  127. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  128. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  129. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  130. }
  131. /* SPI2 init function */
  132. void MX_SPI2_Init(void)
  133. {
  134. hspi2.Instance = SPI2;
  135. hspi2.Init.Mode = SPI_MODE_MASTER;
  136. hspi2.Init.Direction = SPI_DIRECTION_2LINES;
  137. hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
  138. hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
  139. hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
  140. hspi2.Init.NSS = SPI_NSS_SOFT;
  141. hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
  142. hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
  143. hspi2.Init.TIMode = SPI_TIMODE_DISABLED;
  144. hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
  145. hspi2.Init.CRCPolynomial = 10;
  146. HAL_SPI_Init(&hspi2);
  147. }
  148. /** Configure pins as
  149. * Analog
  150. * Input
  151. * Output
  152. * EVENT_OUT
  153. * EXTI
  154. */
  155. void MX_GPIO_Init(void)
  156. {
  157. /* GPIO Ports Clock Enable */
  158. __GPIOB_CLK_ENABLE();
  159. __GPIOA_CLK_ENABLE();
  160. }
  161. /* USER CODE BEGIN 4 */
  162. /* USER CODE END 4 */
  163. #ifdef USE_FULL_ASSERT
  164. /**
  165. * @brief Reports the name of the source file and the source line number
  166. * where the assert_param error has occurred.
  167. * @param file: pointer to the source file name
  168. * @param line: assert_param error line source number
  169. * @retval None
  170. */
  171. void assert_failed(uint8_t* file, uint32_t line)
  172. {
  173. /* USER CODE BEGIN 6 */
  174. /* User can add his own implementation to report the file name and line number,
  175. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  176. /* USER CODE END 6 */
  177. }
  178. #endif
  179. /**
  180. * @}
  181. */
  182. /**
  183. * @}
  184. */
  185. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/