Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ******************************************************************************
- * @file OptimizedI2Cexamples/src/main.c
- * @author MCD Application Team
- * @version V4.0.0
- * @date 06/18/2010
- * @brief Main program body
- ******************************************************************************
- * @copy
- *
- * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
- * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
- * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
- * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
- * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
- * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
- *
- * <h2><center>© COPYRIGHT 2010 STMicroelectronics</center></h2>
- */
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "I2CRoutines.h"
- /** @addtogroup Optimized I2C examples
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- ErrorStatus HSEStartUpStatus;
- /* Buffer of data to be received by I2C1 */
- uint8_t Buffer_Rx1[255];
- /* Buffer of data to be transmitted by I2C1 */
- uint8_t Buffer_Tx1[255] = {0x5, 0x6,0x8,0xA};
- /* Buffer of data to be received by I2C2 */
- uint8_t Buffer_Rx2[255];
- /* Buffer of data to be transmitted by I2C2 */
- uint8_t Buffer_Tx2[255] = {0xF, 0xB, 0xC,0xD};
- extern __IO uint8_t Tx_Idx1 , Rx_Idx1;
- extern __IO uint8_t Tx_Idx2 , Rx_Idx2;
- /* Private function prototypes -----------------------------------------------*/
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- /* Private functions ---------------------------------------------------------*/
- #define GYR_ADDRESS0 (0xD2)
- #define ACC_ADDRESS 0x30
- #define GYR_ADDRESS (0x69<<1)
- #define MAG_ADDRESS (0x3C)
- #define GYR_OUT 0x28
- #define ACC_OUT 0x28
- #define MAG_OUT 0x03
- #define LSM303_CTRL_REG1_A 0x20
- #define LSM303_MR_REG_M 0x02
- #define GYR_REG1 0x20
- #define GYR_REG4 0x23
- short x,y,z;
- static __IO uint32_t TimingDelay;
- void Delay(uint32_t nTime);
- void TimingDelay_Decrement(void);
- void InitSysClock(void);
- void GWriteReg(unsigned char reg,unsigned char value) // Write gyro reg
- {
- Buffer_Tx1[0]=reg;
- Buffer_Tx1[1]=value;
- I2C_Master_BufferWrite(I2C1, Buffer_Tx1,2,Polling, GYR_ADDRESS);
- }
- void AWriteReg(unsigned char reg,unsigned char value) // Write accel reg
- {
- Buffer_Tx1[0]=reg;
- Buffer_Tx1[1]=value;
- if (I2C_Master_BufferWrite(I2C1, Buffer_Tx1,2,Polling, ACC_ADDRESS)==Error)
- {
- }
- }
- void MWriteReg(unsigned char reg,unsigned char value) // Write mag reg
- {
- Buffer_Tx1[0]=reg;
- Buffer_Tx1[1]=value;
- if (I2C_Master_BufferWrite(I2C1, Buffer_Tx1,2,Polling, MAG_ADDRESS)==Error)
- {
- }
- }
- unsigned char GReadReg(unsigned char reg) // Read gyro reg
- {
- Buffer_Tx1[0]=reg;
- I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,Interrupt, GYR_ADDRESS);
- I2C_Master_BufferRead(I2C1,Buffer_Rx1,1,Polling, GYR_ADDRESS);
- return Buffer_Rx1[0];
- }
- void GDataRead() // Read data from gyro
- {
- Buffer_Tx1[0]=GYR_OUT|(1<<7);
- if(I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,DMA, GYR_ADDRESS)==Success)
- {
- if(I2C_Master_BufferRead(I2C1,Buffer_Rx1,6,DMA, GYR_ADDRESS)==Success)
- {
- x=Buffer_Rx1[1]<<8|Buffer_Rx1[0];
- y=Buffer_Rx1[3]<<8|Buffer_Rx1[2];
- z=Buffer_Rx1[5]<<8|Buffer_Rx1[4];
- }
- }
- }
- void ADataRead() // Read data from accel
- {
- Buffer_Tx1[0]=ACC_OUT|(1<<7);
- if(I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,DMA, ACC_ADDRESS)==Success)
- {
- if(I2C_Master_BufferRead(I2C1,Buffer_Rx1,6,DMA,ACC_ADDRESS)==Success)
- {
- x=Buffer_Rx1[1]<<8|Buffer_Rx1[0];
- y=Buffer_Rx1[3]<<8|Buffer_Rx1[2];
- z=Buffer_Rx1[5]<<8|Buffer_Rx1[4];
- }
- }
- }
- void MDataRead() // Read data from mag
- {
- Buffer_Tx1[0]=MAG_OUT;
- if(I2C_Master_BufferWrite(I2C1, Buffer_Tx1,1,DMA, MAG_ADDRESS)==Success)
- {
- if(I2C_Master_BufferRead(I2C1,Buffer_Rx1,6,DMA, MAG_ADDRESS)==Success)
- {
- x = Buffer_Rx1[1]<<8|Buffer_Rx1[0];
- z = Buffer_Rx1[3]<<8|Buffer_Rx1[2];
- y = Buffer_Rx1[5]<<8|Buffer_Rx1[4];
- }
- }
- }
- void GInit() // Init gyro
- {
- GWriteReg(GYR_REG1,0x0F);
- GWriteReg(GYR_REG4,0x20);
- }
- /**
- * @brief Main program
- * @param None
- * @retval : None
- */
- void AInit() // Init accel
- {
- AWriteReg(LSM303_CTRL_REG1_A,0x27);
- }
- void MInit() // Init mag
- {
- MWriteReg(LSM303_MR_REG_M, 0x00);
- }
- int main(void)
- {
- NVIC_Configuration();
- InitSysClock();
- I2C_LowLevel_Init(I2C1);
- AInit(); // Init accel, gyro and mag
- GInit();
- MInit();
- /* Use I2C1 as Master which is communicating with I2C1 of another STM32F10x device */
- while(1)
- {
- GDataRead(); // Read data to variables x,y,z
- ADataRead();
- MDataRead();
- }
- /* Use I2C1 as Slave */
- /*! When using Slave with DMA, uncomment //#define SLAVE_DMA_USE in the stm32f10x_it.c file.*/
- /*I2C_Slave_BufferReadWrite(I2C1, DMA);
- while(1); */
- }
- /**
- * @brief Configures NVIC and Vector Table base location.
- * @param None
- * @retval : None
- */
- void InitSysClock(void)
- {
- if (SysTick_Config(SystemCoreClock / 1000))
- {
- while (1);
- }
- }
- void NVIC_Configuration(void)
- {
- /* 1 bit for pre-emption priority, 3 bits for subpriority */
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
- NVIC_SetPriority(I2C1_EV_IRQn, 0x00);
- NVIC_EnableIRQ(I2C1_EV_IRQn);
- NVIC_SetPriority(I2C1_ER_IRQn, 0x01);
- NVIC_EnableIRQ(I2C1_ER_IRQn);
- NVIC_SetPriority(I2C2_EV_IRQn, 0x00);
- NVIC_EnableIRQ(I2C2_EV_IRQn);
- NVIC_SetPriority(I2C2_ER_IRQn, 0x01);
- NVIC_EnableIRQ(I2C2_ER_IRQn);
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval : None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* Infinite loop */
- while (1)
- {
- }
- }
- #endif
- /**
- * @}
- */
- void Delay(uint32_t nTime)
- {
- TimingDelay = nTime;
- while(TimingDelay != 0);
- }
- void TimingDelay_Decrement(void)
- {
- if (TimingDelay != 0x00)
- TimingDelay--;
- }
- /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement