STM32F10x_StdPeriph_Examples/SDIO/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    SDIO/main.c 
00004   * @author  MCD Application Team
00005   * @version V3.4.0
00006   * @date    10/15/2010
00007   * @brief   Main program body
00008   ******************************************************************************
00009   * @copy
00010   *
00011   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017   *
00018   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00019   */ 
00020 
00021 /* Includes ------------------------------------------------------------------*/
00022 #include "stm32f10x.h"
00023 #include "stm32_eval_sdio_sd.h"
00024 
00025 /** @addtogroup STM32F10x_StdPeriph_Examples
00026   * @{
00027   */
00028 
00029 /** @addtogroup SDIO_Example
00030   * @{
00031   */
00032 
00033 /* Private typedef -----------------------------------------------------------*/
00034 typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
00035 
00036 /* Private define ------------------------------------------------------------*/
00037 #define BLOCK_SIZE            512 /* Block Size in Bytes */
00038 
00039 #define NUMBER_OF_BLOCKS      2  /* For Multi Blocks operation (Read/Write) */
00040 #define MULTI_BUFFER_SIZE    (BLOCK_SIZE * NUMBER_OF_BLOCKS)
00041 
00042 /* Private macro -------------------------------------------------------------*/
00043 /* Private variables ---------------------------------------------------------*/
00044 SD_CardInfo SDCardInfo;
00045 uint8_t Buffer_Block_Tx[BLOCK_SIZE], Buffer_Block_Rx[BLOCK_SIZE];
00046 uint8_t Buffer_MultiBlock_Tx[MULTI_BUFFER_SIZE], Buffer_MultiBlock_Rx[MULTI_BUFFER_SIZE];
00047 volatile TestStatus EraseStatus = FAILED, TransferStatus1 = FAILED, TransferStatus2 = FAILED;
00048 SD_Error Status = SD_OK;
00049 
00050 /* Private function prototypes -----------------------------------------------*/
00051 void NVIC_Configuration(void);
00052 void Fill_Buffer(uint8_t *pBuffer, uint32_t BufferLength, uint32_t Offset);
00053 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength);
00054 TestStatus eBuffercmp(uint8_t* pBuffer, uint32_t BufferLength);
00055 
00056 /* Private functions ---------------------------------------------------------*/
00057 
00058 /**
00059   * @brief   Main program.
00060   * @param  None
00061   * @retval None
00062   */
00063 int main(void)
00064 {
00065   /*!< At this stage the microcontroller clock setting is already configured, 
00066        this is done through SystemInit() function which is called from startup
00067        file (startup_stm32f10x_xx.s) before to branch to application main.
00068        To reconfigure the default setting of SystemInit() function, refer to
00069        system_stm32f10x.c file
00070      */     
00071 
00072   /* Interrupt Config */
00073   NVIC_Configuration();
00074 
00075   /*-------------------------- SD Init ----------------------------- */
00076   Status = SD_Init();
00077 
00078   /*------------------- Block Erase -------------------------------*/
00079   if (Status == SD_OK)
00080   {
00081     /* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */
00082     Status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS));
00083   }
00084 
00085   if (Status == SD_OK)
00086   {
00087     Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
00088   }
00089 
00090   if (Status == SD_OK)
00091   {
00092     EraseStatus = eBuffercmp(Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
00093   }
00094   
00095   /*------------------- Block Read/Write --------------------------*/
00096   /* Fill the buffer to send */
00097   Fill_Buffer(Buffer_Block_Tx, BLOCK_SIZE, 0xFFFF);
00098 
00099 
00100   if (Status == SD_OK)
00101   {
00102     /* Write block of 512 bytes on address 0 */
00103     Status = SD_WriteBlock(Buffer_Block_Tx, 0x00, BLOCK_SIZE);
00104   }
00105 
00106   if (Status == SD_OK)
00107   {
00108     /* Read block of 512 bytes from address 0 */
00109     Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BLOCK_SIZE);
00110   }
00111 
00112   if (Status == SD_OK)
00113   {
00114     /* Check the corectness of written dada */
00115     TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BLOCK_SIZE);
00116   }
00117 
00118   /*--------------- Multiple Block Read/Write ---------------------*/
00119   /* Fill the buffer to send */
00120   Fill_Buffer(Buffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0);
00121 
00122   if (Status == SD_OK)
00123   {
00124     /* Write multiple block of many bytes on address 0 */
00125     Status = SD_WriteMultiBlocks(Buffer_MultiBlock_Tx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
00126   }
00127 
00128   if (Status == SD_OK)
00129   {
00130     /* Read block of many bytes from address 0 */
00131     Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS);
00132   }
00133 
00134   if (Status == SD_OK)
00135   {
00136     /* Check the corectness of written dada */
00137     TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE);
00138   }
00139 
00140   /* Infinite loop */
00141   while (1)
00142   {}
00143 }
00144 
00145 /**
00146   * @brief  Configures SDIO IRQ channel.
00147   * @param  None
00148   * @retval None
00149   */
00150 void NVIC_Configuration(void)
00151 {
00152   NVIC_InitTypeDef NVIC_InitStructure;
00153 
00154   /* Configure the NVIC Preemption Priority Bits */
00155   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
00156 
00157   NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
00158   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00159   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00160   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00161   NVIC_Init(&NVIC_InitStructure);
00162 }
00163 
00164 /**
00165   * @brief  Compares two buffers.
00166   * @param  pBuffer1, pBuffer2: buffers to be compared.
00167   * @param  BufferLength: buffer's length
00168   * @retval PASSED: pBuffer1 identical to pBuffer2
00169   *         FAILED: pBuffer1 differs from pBuffer2
00170   */
00171 TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint32_t BufferLength)
00172 {
00173   while (BufferLength--)
00174   {
00175     if (*pBuffer1 != *pBuffer2)
00176     {
00177       return FAILED;
00178     }
00179 
00180     pBuffer1++;
00181     pBuffer2++;
00182   }
00183 
00184   return PASSED;
00185 }
00186 
00187 /**
00188   * @brief  Fills buffer with user predefined data.
00189   * @param  pBuffer: pointer on the Buffer to fill
00190   * @param  BufferLength: size of the buffer to fill
00191   * @param  Offset: first value to fill on the Buffer
00192   * @retval None
00193   */
00194 void Fill_Buffer(uint8_t *pBuffer, uint32_t BufferLength, uint32_t Offset)
00195 {
00196   uint16_t index = 0;
00197 
00198   /* Put in global buffer same values */
00199   for (index = 0; index < BufferLength; index++ )
00200   {
00201     pBuffer[index] = index + Offset;
00202   }
00203 }
00204 
00205 /**
00206   * @brief  Checks if a buffer has all its values are equal to zero.
00207   * @param  pBuffer: buffer to be compared.
00208   * @param  BufferLength: buffer's length
00209   * @retval PASSED: pBuffer values are zero
00210   *         FAILED: At least one value from pBuffer buffer is diffrent from zero.
00211   */
00212 TestStatus eBuffercmp(uint8_t* pBuffer, uint32_t BufferLength)
00213 {
00214   while (BufferLength--)
00215   {
00216     /* In some SD Cards the erased state is 0xFF, in others it's 0x00 */
00217     if ((*pBuffer != 0xFF) && (*pBuffer != 0x00))
00218     {
00219       return FAILED;
00220     }
00221 
00222     pBuffer++;
00223   }
00224 
00225   return PASSED;
00226 }
00227 
00228 #ifdef  USE_FULL_ASSERT
00229 /**
00230   * @brief  Reports the name of the source file and the source line number
00231   *         where the assert_param error has occurred.
00232   * @param  file: pointer to the source file name
00233   * @param  line: assert_param error line source number
00234   * @retval None
00235   */
00236 void assert_failed(uint8_t* file, uint32_t line)
00237 {
00238   /* User can add his own implementation to report the file name and line number,
00239      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00240 
00241   /* Infinite loop */
00242   while (1)
00243   {}
00244 }
00245 #endif
00246 
00247 /**
00248   * @}
00249   */
00250 
00251 /**
00252   * @}
00253   */
00254 
00255 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32