STM32F10x_StdPeriph_Examples/DMA/I2C_RAM/main.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    DMA/I2C_RAM/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 
00024 /** @addtogroup STM32F10x_StdPeriph_Examples
00025   * @{
00026   */
00027 
00028 /** @addtogroup DMA_I2C_RAM
00029   * @{
00030   */ 
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
00034 
00035 /* Private define ------------------------------------------------------------*/
00036 #define I2C1_DR_Address        0x40005410
00037 #define I2C2_DR_Address        0x40005810
00038 #define I2C1_SLAVE_ADDRESS7    0x30
00039 #define I2C2_SLAVE_ADDRESS7    0x30
00040 #define BufferSize             8
00041 #define ClockSpeed             100000
00042 
00043 /* Private macro -------------------------------------------------------------*/
00044 /* Private variables ---------------------------------------------------------*/
00045 I2C_InitTypeDef  I2C_InitStructure;
00046 DMA_InitTypeDef  DMA_InitStructure;
00047 uint8_t I2C1_Buffer_Tx[BufferSize] = {1, 2, 3, 4, 5, 6, 7, 8};
00048 uint8_t I2C2_Buffer_Rx[BufferSize];
00049 uint8_t Tx_Idx = 0, Rx_Idx = 0;
00050 volatile TestStatus TransferStatus;
00051     
00052 /* Private function prototypes -----------------------------------------------*/
00053 void RCC_Configuration(void);
00054 void GPIO_Configuration(void);
00055 TestStatus Buffercmp(uint8_t* pBuffer, uint8_t* pBuffer1, uint16_t BufferLength);
00056     
00057 /* Private functions ---------------------------------------------------------*/
00058 
00059 /**
00060   * @brief   Main program
00061   * @param  None
00062   * @retval None
00063   */
00064 int main(void)
00065 {
00066   /*!< At this stage the microcontroller clock setting is already configured, 
00067        this is done through SystemInit() function which is called from startup
00068        file (startup_stm32f10x_xx.s) before to branch to application main.
00069        To reconfigure the default setting of SystemInit() function, refer to
00070        system_stm32f10x.c file
00071      */     
00072        
00073   /* System Clocks Configuration */
00074   RCC_Configuration();
00075        
00076   /* Configure the GPIO ports */
00077   GPIO_Configuration();
00078 
00079   /* DMA1 channel5 configuration ----------------------------------------------*/
00080   DMA_DeInit(DMA1_Channel5);
00081   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C2_DR_Address;
00082   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2C2_Buffer_Rx;
00083   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
00084   DMA_InitStructure.DMA_BufferSize = BufferSize;
00085   DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
00086   DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
00087   DMA_InitStructure.DMA_PeripheralDataSize = DMA_MemoryDataSize_Byte;
00088   DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
00089   DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
00090   DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
00091   DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
00092   DMA_Init(DMA1_Channel5, &DMA_InitStructure);
00093 
00094   /* DMA1 channel6 configuration ----------------------------------------------*/
00095   DMA_DeInit(DMA1_Channel6);
00096   DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)I2C1_DR_Address;
00097   DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)I2C1_Buffer_Tx;
00098   DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
00099   DMA_InitStructure.DMA_Priority = DMA_Priority_High;
00100   DMA_Init(DMA1_Channel6, &DMA_InitStructure);
00101 
00102   /* Enable I2C1 and I2C2 ----------------------------------------------------*/
00103   I2C_Cmd(I2C1, ENABLE);
00104   I2C_Cmd(I2C2, ENABLE);
00105 
00106   /* I2C1 configuration ------------------------------------------------------*/
00107   I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
00108   I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
00109   I2C_InitStructure.I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
00110   I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
00111   I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
00112   I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
00113   I2C_Init(I2C1, &I2C_InitStructure);
00114   /* I2C2 configuration ------------------------------------------------------*/
00115   I2C_InitStructure.I2C_OwnAddress1 = I2C2_SLAVE_ADDRESS7;
00116   I2C_Init(I2C2, &I2C_InitStructure);
00117   
00118   /*----- Transmission Phase -----*/
00119   /* Send I2C1 START condition */
00120   I2C_GenerateSTART(I2C1, ENABLE);
00121   /* Test on I2C1 EV5 and clear it */
00122   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));  
00123   /* Send I2C2 slave Address for write */
00124   I2C_Send7bitAddress(I2C1, I2C2_SLAVE_ADDRESS7, I2C_Direction_Transmitter);
00125   /* Test on I2C2 EV1 and clear it */
00126   while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED));  
00127   /* Test on I2C1 EV6 and clear it */
00128   while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));  
00129 
00130   /* Enable I2C2 DMA */
00131   I2C_DMACmd(I2C2, ENABLE);
00132   /* Enable I2C1 DMA */
00133   I2C_DMACmd(I2C1, ENABLE);
00134 
00135   /* Enable DMA1 Channel5 */
00136   DMA_Cmd(DMA1_Channel5, ENABLE);
00137   /* Enable DMA1 Channel6 */
00138   DMA_Cmd(DMA1_Channel6, ENABLE);
00139 
00140   /* DMA1 Channel5 transfer complete test */
00141   while(!DMA_GetFlagStatus(DMA1_FLAG_TC5));
00142   /* DMA1 Channel6 transfer complete test */
00143   while(!DMA_GetFlagStatus(DMA1_FLAG_TC6));
00144 
00145   /* Send I2C1 STOP Condition */
00146   I2C_GenerateSTOP(I2C1, ENABLE);
00147   /* Test on I2C2 EV4 */
00148   while(!I2C_CheckEvent(I2C2, I2C_EVENT_SLAVE_STOP_DETECTED)); 
00149   /* Clear I2C2 STOPF flag: read operation to I2C_SR1 followed by a 
00150   write operation to I2C_CR1 */
00151   (void)(I2C_GetFlagStatus(I2C2, I2C_FLAG_STOPF));
00152   I2C_Cmd(I2C2, ENABLE);
00153 
00154 
00155   /* Check if the transmitted and received data are equal */
00156   TransferStatus = Buffercmp(I2C1_Buffer_Tx, I2C2_Buffer_Rx, BufferSize);
00157   /* TransferStatus = PASSED, if the transmitted and received data 
00158      are the same */
00159   /* TransferStatus = FAILED, if the transmitted and received data 
00160      are different */
00161 
00162   while (1)
00163   {
00164   }
00165 }
00166 
00167 /**
00168   * @brief  Configures the different system clocks.
00169   * @param  None
00170   * @retval None
00171   */
00172 void RCC_Configuration(void)
00173 {
00174   /* Enable DMA1 clock */
00175   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
00176   /* Enable GPIOB clock */
00177   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
00178   /* Enable I2C1 and I2C2 clock */
00179   RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1 | RCC_APB1Periph_I2C2, ENABLE);
00180 }
00181 
00182 /**
00183   * @brief  Configures the different GPIO ports.
00184   * @param  None
00185   * @retval None
00186   */
00187 void GPIO_Configuration(void)
00188 {
00189   GPIO_InitTypeDef GPIO_InitStructure;
00190 
00191   /* Configure I2C1 pins: SCL and SDA */
00192   GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
00193   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00194   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
00195   GPIO_Init(GPIOB, &GPIO_InitStructure);
00196 
00197   /* Configure I2C2 pins: SCL and SDA */
00198   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
00199   GPIO_Init(GPIOB, &GPIO_InitStructure);
00200 }
00201 
00202 /**
00203   * @brief  Compares two buffers.
00204   * @param  pBuffer, pBuffer1: buffers to be compared.
00205   * @param  BufferLength: buffer's length
00206   * @retval PASSED: pBuffer identical to pBuffer1
00207   *   FAILED: pBuffer differs from pBuffer1
00208   */
00209 TestStatus Buffercmp(uint8_t* pBuffer, uint8_t* pBuffer1, uint16_t BufferLength)
00210 {
00211   while(BufferLength--)
00212   {
00213     if(*pBuffer != *pBuffer1)
00214     {
00215       return FAILED;
00216     }
00217     
00218     pBuffer++;
00219     pBuffer1++;
00220   }
00221 
00222   return PASSED;  
00223 }
00224 
00225 #ifdef  USE_FULL_ASSERT
00226 /**
00227   * @brief  Reports the name of the source file and the source line number
00228   *         where the assert_param error has occurred.
00229   * @param  file: pointer to the source file name
00230   * @param  line: assert_param error line source number
00231   * @retval None
00232   */
00233 void assert_failed(uint8_t* file, uint32_t line)
00234 { 
00235   /* User can add his own implementation to report the file name and line number,
00236      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00237 
00238   /* Infinite loop */
00239   while (1)
00240   {
00241   }
00242 }
00243 #endif
00244 
00245 /**
00246   * @}
00247   */ 
00248 
00249 /**
00250   * @}
00251   */
00252 
00253 /******************* (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