STM8S/A Standard Peripherals Firmware Library
|
STM8S_StdPeriph_Examples/SPI/SPI_FastCommunicationMicroSD/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file SPI_FastCommunicationMicroSD\main.c 00004 * @author MCD Application Team 00005 * @version V2.2.0 00006 * @date 30-September-2014 00007 * @brief This file contains the main function for SPI fast communication example. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "stm8s.h" 00030 #include "stm8s_eval.h" 00031 #include "stm8s_eval_spi_sd.h" 00032 00033 /** 00034 * @addtogroup SPI_FastCommunicationMicroSD 00035 * @{ 00036 */ 00037 00038 /* Private macro -------------------------------------------------------------*/ 00039 #define countof(a) (sizeof(a) / sizeof(*(a))) 00040 00041 /* Private define ------------------------------------------------------------*/ 00042 #define BUFFER_SIZE (countof(TxBuffer)-1) 00043 00044 /* Private variables ---------------------------------------------------------*/ 00045 __IO uint16_t Status = 0; 00046 uint8_t TxBuffer[] = "STM8S SPI Firmware Library Example: communication with a microSD card"; 00047 uint8_t RxBuffer[BUFFER_SIZE] = {0}; 00048 __IO ErrorStatus TransferStatus = ERROR; 00049 00050 /* Private function prototypes -----------------------------------------------*/ 00051 void Delay (uint16_t nCount); 00052 static ErrorStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength); 00053 static void CLK_Config(void); 00054 static void GPIO_Config(void); 00055 /* Private functions ---------------------------------------------------------*/ 00056 /* Global variables ----------------------------------------------------------*/ 00057 /* Public functions ----------------------------------------------------------*/ 00058 00059 /** 00060 * @brief Main program. 00061 * @param None 00062 * @retval None 00063 */ 00064 void main(void) 00065 { 00066 /* Clock configuration -----------------------------------------*/ 00067 CLK_Config(); 00068 00069 /* GPIO Configuration ------------------------------------------*/ 00070 GPIO_Config(); 00071 00072 00073 /***********************SPI and MSD Card initialization******************/ 00074 00075 while (SD_Detect() == SD_NOT_PRESENT); 00076 { 00077 /* Wait MicroSD card insertion */ 00078 } 00079 00080 Delay(0xFFFF); 00081 /* Init the flash micro SD*/ 00082 Status = SD_Init(); 00083 00084 /***************************Block Read/Write******************************/ 00085 /* Write block of 512 bytes on address 0 */ 00086 SD_WriteBlock(TxBuffer, 0, BUFFER_SIZE); 00087 00088 /* Read block of 512 bytes from address 0 */ 00089 SD_ReadBlock(RxBuffer, 0, BUFFER_SIZE); 00090 00091 /* Check data */ 00092 TransferStatus = Buffercmp(TxBuffer, RxBuffer, BUFFER_SIZE); 00093 if (TransferStatus != SUCCESS) 00094 { 00095 while (1) /* Go to infinite loop when there is mismatch in data programming*/ 00096 { 00097 STM_EVAL_LEDToggle(LED1); 00098 Delay((uint16_t)0xFFFF); 00099 Delay((uint16_t)0xFFFF); 00100 } 00101 } 00102 while (1) 00103 { 00104 STM_EVAL_LEDToggle(LED1); 00105 STM_EVAL_LEDToggle(LED2); 00106 STM_EVAL_LEDToggle(LED3); 00107 STM_EVAL_LEDToggle(LED4); 00108 Delay((uint16_t)0xFFFF); 00109 Delay((uint16_t)0xFFFF); 00110 } 00111 } 00112 00113 /** 00114 * @brief Configure system clock to run at 16Mhz 00115 * @param None 00116 * @retval None 00117 */ 00118 static void CLK_Config(void) 00119 { 00120 /* Initialization of the clock */ 00121 /* Clock divider to HSI/1 */ 00122 CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 00123 } 00124 00125 /** 00126 * @brief Configure GPIO for LEDs available on the evaluation board 00127 * @param None 00128 * @retval None 00129 */ 00130 static void GPIO_Config(void) 00131 { 00132 /* Initialize LEDs mounted on STM8-128 EVAL board */ 00133 STM_EVAL_LEDInit(LED1); 00134 STM_EVAL_LEDInit(LED2); 00135 STM_EVAL_LEDInit(LED3); 00136 STM_EVAL_LEDInit(LED4); 00137 00138 /* Switch LEDs On */ 00139 STM_EVAL_LEDOff(LED1); 00140 STM_EVAL_LEDOff(LED2); 00141 STM_EVAL_LEDOff(LED3); 00142 STM_EVAL_LEDOff(LED4); 00143 } 00144 00145 /** 00146 * @brief Compares two buffers. 00147 * @Param pBufferx: buffers to be compared. 00148 * @param BufferLength: buffer's length 00149 * @retval ErrorStatus : result of the comparison 00150 * - PASSED: pBuffer1 identical to pBuffer2 00151 * - FAILED: pBuffer1 differs from pBuffer2 00152 */ 00153 ErrorStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength) 00154 { 00155 while (BufferLength--) 00156 { 00157 if (*pBuffer1 != *pBuffer2) 00158 { 00159 return ERROR; 00160 } 00161 00162 pBuffer1++; 00163 pBuffer2++; 00164 } 00165 00166 return SUCCESS; 00167 } 00168 00169 /** 00170 * @brief Delay. 00171 * @param nCount 00172 * @retval None 00173 */ 00174 void Delay(uint16_t nCount) 00175 { 00176 /* Decrement nCount value */ 00177 while (nCount != 0) 00178 { 00179 nCount--; 00180 } 00181 } 00182 00183 #ifdef USE_FULL_ASSERT 00184 00185 /** 00186 * @brief Reports the name of the source file and the source line number 00187 * where the assert_param error has occurred. 00188 * @param file: pointer to the source file name 00189 * @param line: assert_param error line source number 00190 * @retval None 00191 */ 00192 void assert_failed(uint8_t* file, uint32_t line) 00193 { 00194 /* User can add his own implementation to report the file name and line number, 00195 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00196 00197 /* Infinite loop */ 00198 while (1) 00199 { 00200 } 00201 } 00202 #endif 00203 00204 /** 00205 * @} 00206 */ 00207 00208 00209 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/