Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2013
1,778
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.76 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file OptimizedI2Cexamples/src/main.c
  4.   * @author  MCD Application Team
  5.   * @version  V4.0.0
  6.   * @date     06/18/2010
  7.   * @brief  Main program body
  8.   ******************************************************************************
  9.   * @copy
  10.   *
  11.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  12.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  13.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  14.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  15.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  16.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  17.   *
  18.   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  19.   */
  20.  
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "stm32f10x.h"
  23. #include "I2CRoutines.h"
  24.  
  25. /** @addtogroup Optimized I2C examples
  26.   * @{
  27.   */
  28.  
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* Private define ------------------------------------------------------------*/
  31. /* Private macro -------------------------------------------------------------*/
  32. /* Private variables ---------------------------------------------------------*/
  33. ErrorStatus HSEStartUpStatus;
  34. /* Buffer of data to be received by I2C1 */
  35. uint8_t Buffer_Rx1[255];
  36. /* Buffer of data to be transmitted by I2C1 */
  37. uint8_t Buffer_Tx1[255] = {0x5, 0x6,0x8,0xA};
  38. /* Buffer of data to be received by I2C2 */
  39. uint8_t Buffer_Rx2[255];
  40. /* Buffer of data to be transmitted by I2C2 */
  41. uint8_t Buffer_Tx2[255] = {0xF, 0xB, 0xC,0xD};
  42. extern __IO uint8_t Tx_Idx1 , Rx_Idx1;
  43. extern __IO uint8_t Tx_Idx2 , Rx_Idx2;
  44.    
  45. /* Private function prototypes -----------------------------------------------*/
  46. void GPIO_Configuration(void);
  47. void NVIC_Configuration(void);
  48. /* Private functions ---------------------------------------------------------*/
  49. #define GYR_ADDRESS0 (0xD2)
  50. #define ACC_ADDRESS 0x30
  51. #define GYR_ADDRESS (0x69<<1)
  52. #define MAG_ADDRESS (0x3C)
  53. #define GYR_OUT 0x28
  54. #define ACC_OUT 0x28
  55. #define MAG_OUT 0x03
  56. #define LSM303_CTRL_REG1_A  0x20
  57. #define LSM303_MR_REG_M  0x02
  58. #define GYR_REG1 0x20
  59. #define GYR_REG4 0x23
  60.  
  61. short x,y,z;
  62. static __IO uint32_t TimingDelay;
  63.  
  64. void Delay(uint32_t nTime);
  65. void TimingDelay_Decrement(void);
  66. void InitSysClock(void);
  67. void GWriteReg(unsigned char reg,unsigned char value) // Write gyro reg
  68. {
  69.     Buffer_Tx1[0]=reg;
  70.     Buffer_Tx1[1]=value;
  71.    
  72.     I2C_Master_BufferWrite(I2C1, Buffer_Tx1,2,Polling, GYR_ADDRESS);
  73. }
  74. void AWriteReg(unsigned char reg,unsigned char value) // Write accel reg
  75. {
  76.     Buffer_Tx1[0]=reg;
  77.     Buffer_Tx1[1]=value;
  78.    
  79.     if (I2C_Master_BufferWrite(I2C1, Buffer_Tx1,2,Polling, ACC_ADDRESS)==Error)
  80.     {
  81.        
  82.     }
  83. }
  84. void MWriteReg(unsigned char reg,unsigned char value) // Write mag reg
  85. {
  86.     Buffer_Tx1[0]=reg;
  87.     Buffer_Tx1[1]=value;
  88.    
  89.     if (I2C_Master_BufferWrite(I2C1, Buffer_Tx1,2,Polling, MAG_ADDRESS)==Error)
  90.     {
  91.    
  92.     }
  93. }
  94. unsigned char GReadReg(unsigned char reg) // Read gyro reg
  95. {
  96.     Buffer_Tx1[0]=reg;
  97.     I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,Interrupt, GYR_ADDRESS);
  98.     I2C_Master_BufferRead(I2C1,Buffer_Rx1,1,Polling, GYR_ADDRESS);
  99.     return Buffer_Rx1[0];
  100. }
  101. void GDataRead() // Read data from gyro
  102. {
  103.     Buffer_Tx1[0]=GYR_OUT|(1<<7);
  104.     if(I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,DMA, GYR_ADDRESS)==Success)
  105.     {
  106.             if(I2C_Master_BufferRead(I2C1,Buffer_Rx1,6,DMA, GYR_ADDRESS)==Success)
  107.             {
  108.                     x=Buffer_Rx1[1]<<8|Buffer_Rx1[0];
  109.                     y=Buffer_Rx1[3]<<8|Buffer_Rx1[2];
  110.                     z=Buffer_Rx1[5]<<8|Buffer_Rx1[4];
  111.             }
  112.     }
  113. }
  114. void ADataRead() // Read data from accel
  115. {
  116.     Buffer_Tx1[0]=ACC_OUT|(1<<7);
  117.     if(I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,DMA, ACC_ADDRESS)==Success)
  118.     {
  119.             if(I2C_Master_BufferRead(I2C1,Buffer_Rx1,6,DMA,ACC_ADDRESS)==Success)
  120.             {
  121.                     x=Buffer_Rx1[1]<<8|Buffer_Rx1[0];
  122.                     y=Buffer_Rx1[3]<<8|Buffer_Rx1[2];
  123.                     z=Buffer_Rx1[5]<<8|Buffer_Rx1[4];
  124.             }
  125.     }
  126. }
  127. void MDataRead() // Read data from mag
  128. {
  129.     Buffer_Tx1[0]=MAG_OUT;
  130.     if(I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,DMA, MAG_ADDRESS)==Success)
  131.     {
  132.             if(I2C_Master_BufferRead(I2C1,Buffer_Rx1,6,DMA, MAG_ADDRESS)==Success)
  133.             {
  134.                     x = Buffer_Rx1[1]<<8|Buffer_Rx1[0];
  135.                     z = Buffer_Rx1[3]<<8|Buffer_Rx1[2];
  136.                     y = Buffer_Rx1[5]<<8|Buffer_Rx1[4];
  137.             }
  138.     }
  139. }
  140. void GInit() // Init gyro
  141. {
  142.     GWriteReg(GYR_REG1,0x0F);
  143.     GWriteReg(GYR_REG4,0x20);
  144. }
  145.  
  146. /**
  147.   * @brief  Main program
  148.   * @param  None
  149.   * @retval : None
  150.   */
  151. void AInit() // Init accel
  152. {
  153.     AWriteReg(LSM303_CTRL_REG1_A,0x27);
  154. }
  155.  
  156. void MInit() // Init mag
  157. {
  158.     MWriteReg(LSM303_MR_REG_M, 0x00);
  159. }
  160. int main(void)
  161. {
  162.     NVIC_Configuration();
  163.         InitSysClock();
  164.         I2C_LowLevel_Init(I2C1);
  165.         AInit(); // Init accel, gyro and mag
  166.       GInit();
  167.       MInit();
  168.         /* Use I2C1 as Master which is communicating with I2C1 of another STM32F10x device */
  169.         while(1)
  170.         {
  171.             GDataRead(); // Read data to variables x,y,z
  172.             ADataRead();
  173.             MDataRead();
  174.         }
  175.    
  176.         /* Use I2C1 as Slave */
  177.         /*! When using Slave with DMA, uncomment //#define SLAVE_DMA_USE in the stm32f10x_it.c file.*/
  178.         /*I2C_Slave_BufferReadWrite(I2C1, DMA);
  179.         while(1); */
  180. }
  181.  
  182. /**
  183.   * @brief  Configures NVIC and Vector Table base location.
  184.   * @param  None
  185.   * @retval : None
  186.   */
  187. void InitSysClock(void)
  188. {
  189.     if (SysTick_Config(SystemCoreClock / 1000))
  190.   {
  191.      while (1);
  192.   }
  193. }
  194. void NVIC_Configuration(void)
  195. {
  196.     /* 1 bit for pre-emption priority, 3 bits for subpriority */
  197.     NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
  198.  
  199.     NVIC_SetPriority(I2C1_EV_IRQn, 0x00);
  200.     NVIC_EnableIRQ(I2C1_EV_IRQn);
  201.  
  202.     NVIC_SetPriority(I2C1_ER_IRQn, 0x01);
  203.     NVIC_EnableIRQ(I2C1_ER_IRQn);
  204.    
  205.     NVIC_SetPriority(I2C2_EV_IRQn, 0x00);
  206.     NVIC_EnableIRQ(I2C2_EV_IRQn);
  207.  
  208.     NVIC_SetPriority(I2C2_ER_IRQn, 0x01);
  209.     NVIC_EnableIRQ(I2C2_ER_IRQn);
  210. }
  211.  
  212. #ifdef  USE_FULL_ASSERT
  213.  
  214.  
  215. /**
  216.   * @brief  Reports the name of the source file and the source line number
  217.   *   where the assert_param error has occurred.
  218.   * @param file: pointer to the source file name
  219.   * @param line: assert_param error line source number
  220.   * @retval : None
  221.   */
  222. void assert_failed(uint8_t* file, uint32_t line)
  223. {
  224.     /* User can add his own implementation to report the file name and line number,
  225.        ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  226.  
  227.     /* Infinite loop */
  228.     while (1)
  229.     {
  230.     }
  231. }
  232. #endif
  233. /**
  234.   * @}
  235.   */
  236. void Delay(uint32_t nTime)
  237. {
  238.   TimingDelay = nTime;
  239.  
  240.   while(TimingDelay != 0);
  241. }
  242. void TimingDelay_Decrement(void)
  243. {
  244.   if (TimingDelay != 0x00)
  245.       TimingDelay--;
  246. }
  247.  
  248. /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement