/** * @brief Configures the selected IO Expander functionalities. * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param IOE_TEMPSENS_FCT: the functions to be configured. could be any * combination of the following values: * @arg IOE_IO_FCT : IO function * @arg IOE_TS_FCT : Touch Screen function * @arg IOE_ADC_FCT : ADC function * @arg IOE_TEMPSENS_FCT : Temperature Sensor function * @retval IOE_OK: if all initializations are OK. Other value if error. */ uint8_t IOE_FnctCmd(uint8_t DeviceAddr, uint8_t Fct, FunctionalState NewState) { uint8_t tmp = 0; /* Get the register value */ tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL2); if (NewState != DISABLE) { /* Set the Functionalities to be Enabled */ tmp &= ~(uint8_t)Fct; } else { /* Set the Functionalities to be Disabled */ tmp |= (uint8_t)Fct; } /* Set the register value */ I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL2, tmp); /* If all OK return IOE_OK */ return IOE_OK; }
/** * @brief Enables or disables the Global interrupt. * @param DeviceAddr: The address of the IOExpander, could be :I OE_1_ADDR * or IOE_2_ADDR. * @param NewState: could be ENABLE or DISABLE. * @retval IOE_OK: if all initializations are OK. Other value if error. */ uint8_t IOE_GITCmd(uint8_t DeviceAddr, FunctionalState NewState) { uint8_t tmp = 0; /* Read the Interrupt Control register */ I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_CTRL); if (NewState != DISABLE) { /* Set the global interrupts to be Enabled */ tmp |= (uint8_t)IOE_GIT_EN; } else { /* Set the global interrupts to be Disabled */ tmp &= ~(uint8_t)IOE_GIT_EN; } /* Write Back the Interrupt Control register */ I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_CTRL, tmp); /* If all OK return IOE_OK */ return IOE_OK; }
/** * @brief Configures the selected pin to be in Alternate function or not * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param IO_Pin: IO_Pin_x, Where x can be from 0 to 7. * @param NewState: State of the AF for the selected pin, could be * ENABLE or DISABLE. * @retval IOE_OK: if all initializations are OK. Other value if error. */ uint8_t IOE_IOAFConfig(uint8_t DeviceAddr, uint8_t IO_Pin, FunctionalState NewState) { uint8_t tmp = 0; /* Get the current state of the GPIO_AF register */ tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_AF); if (NewState != DISABLE) { /* Enable the selected pins alternate function */ tmp |= (uint8_t)IO_Pin; } else { /* Disable the selected pins alternate function */ tmp &= ~(uint8_t)IO_Pin; } /* Write back the new value in GPIO_AF register */ I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_AF, tmp); /* If all OK return IOE_OK */ return IOE_OK; }
/** * @brief Returns the status of the selected input IO pin. * @param IO_Pin: The input pin to be read. * IO_Pin_x: Where x can be from 0 to 7. * @arg JOY_IO_PINS: Joystick IO pins (use IOE_JoyStickGetState for these pins) * @retval None */ uint8_t IOE_ReadIOPin(uint32_t IO_Pin) { uint8_t DeviceAddr = 0; uint8_t tmp = 0; if (IO_Pin & IO1_IN_ALL_PINS) { DeviceAddr = IOE_1_ADDR; } else { return PARAM_ERROR; } /* Get all the Pins status */ tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_MP_STA); if ((tmp & (uint8_t)IO_Pin) != 0) { return BitSet; } else { return BitReset; } }
/** * @brief Configures The selected interrupts on the IO Expanders. * @param IOE_ITSRC_Source: the source of the interrupts. Could be one or a * combination of the following parameters: * @arg IOE_ITSRC_TSC: Touch Screen interrupts. * @retval IOE_OK: if all initializations are OK. Other value if error. */ uint8_t IOE_ITConfig(uint32_t IOE_ITSRC_Source) { /* Configure the Interrupt output pin to generate low level (INT_CTRL) */ IOE_ITOutConfig(Polarity_Low, Type_Level); /* Manage the Touch Screen Interrupts */ if (IOE_ITSRC_Source & IOE_ITSRC_TSC) { /* Enable the Global interrupt */ IOE_GITCmd(IOE_1_ADDR, ENABLE); /* Enable the Global GPIO Interrupt */ IOE_GITConfig(IOE_1_ADDR, (uint8_t)(IOE_GIT_TOUCH | IOE_GIT_FTH | IOE_GIT_FOV), ENABLE); /* Read the GPIO_IT_STA to clear all pending bits if any */ I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_INT_STA); } /* Configure the Interrupt line as EXTI source */ IOE_EXTI_Config(); /* If all OK return IOE_OK */ return IOE_OK; }
/** * @brief Returns the current Joystick status. * @param None * @retval The code of the Joystick key pressed: * @arg JOY_NONE * @arg JOY_IO_CENTER * @arg JOY_DOWN * @arg JOY_LEFT * @arg JOY_RIGHT * @arg JOY_UP */ JOY_State_TypeDef IOE_JoyStickGetState(void) { uint8_t tmp = 0; /* Read the status of all pins */ tmp = (uint32_t)I2C_ReadDeviceRegister(IOE_2_ADDR, IOE_REG_GPIO_MP_STA); /* Check the pressed keys */ if ((tmp & JOY_IO_NONE) == JOY_IO_NONE) { return (JOY_State_TypeDef)JOY_NONE; } else if (!(tmp & JOY_IO_CENTER)) { return (JOY_State_TypeDef)JOY_CENTER; } else if (!(tmp & JOY_IO_DOWN)) { return (JOY_State_TypeDef)JOY_DOWN; } else if (!(tmp & JOY_IO_LEFT)) { return (JOY_State_TypeDef)JOY_LEFT; } else if (!(tmp & JOY_IO_RIGHT)) { return (JOY_State_TypeDef)JOY_RIGHT; } else if (!(tmp & JOY_IO_UP)) { return (JOY_State_TypeDef)JOY_UP; } else { return (JOY_State_TypeDef)JOY_NONE; } }
/** * @brief Writes a value in a register of the device through I2C. * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param RegisterAddr: The target register adress * @param RegisterValue: The target register value to be written * @retval IOE_OK: if all operations are OK. Other value if error. */ uint8_t I2C_WriteDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t RegisterValue) { uint32_t read_verif = 0; uint8_t IOE_BufferTX = 0; /* Get Value to be written */ IOE_BufferTX = RegisterValue; /* Configure DMA Peripheral */ IOE_DMA_Config(IOE_DMA_TX, (uint8_t*)(&IOE_BufferTX)); /* Enable the I2C peripheral */ I2C_GenerateSTART(IOE_I2C, ENABLE); /* Test on SB Flag */ IOE_TimeOut = TIMEOUT_MAX; while (I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB) == RESET) { if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); } /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); /* Test on ADDR Flag */ IOE_TimeOut = TIMEOUT_MAX; while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); } /* Transmit the first address for r/w operations */ I2C_SendData(IOE_I2C, RegisterAddr); /* Test on TXE FLag (data dent) */ IOE_TimeOut = TIMEOUT_MAX; while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF))) { if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); } /* Enable I2C DMA request */ I2C_DMACmd(IOE_I2C,ENABLE); /* Enable DMA TX Channel */ DMA_Cmd(IOE_DMA_TX_CHANNEL, ENABLE); /* Wait until DMA Transfer Complete */ IOE_TimeOut = TIMEOUT_MAX; while (!DMA_GetFlagStatus(IOE_DMA_TX_TCFLAG)) { if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); } /* Wait until BTF Flag is set before generating STOP */ IOE_TimeOut = 2 * TIMEOUT_MAX; while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF))) { } /* Send STOP Condition */ I2C_GenerateSTOP(IOE_I2C, ENABLE); /* Disable DMA TX Channel */ DMA_Cmd(IOE_DMA_TX_CHANNEL, DISABLE); /* Disable I2C DMA request */ I2C_DMACmd(IOE_I2C,DISABLE); /* Clear DMA TX Transfer Complete Flag */ DMA_ClearFlag(IOE_DMA_TX_TCFLAG); #ifdef VERIFY_WRITTENDATA /* Verify (if needed) that the loaded data is correct */ /* Read the just written register*/ read_verif = I2C_ReadDeviceRegister(DeviceAddr, RegisterAddr); /* Load the register and verify its value */ if (read_verif != RegisterValue) { /* Control data wrongly tranfered */ read_verif = IOE_FAILURE; } else { /* Control data correctly transfered */ read_verif = 0; } #endif /* Return the verifying value: 0 (Passed) or 1 (Failed) */ return read_verif; }
/* Register 3 - Configuration register This register configures the directions of the I/O pins. If a bit in this register is set, the corresponding port pin is enabled as an input with high-impedance output driver. If a bit in this register is cleared, the corresponding port pin is enabled as an output. At reset, the I/Os are configured as inputs with a weak pull-up to VDD. */ uint8_t PCA9536_REG_CX_READ(void) { /* Get all the Pins direction */ return I2C_ReadDeviceRegister(PCA9536_ADDRESS, PCA9536_REG_CX);; }
/** * @brief Writes a value in a register of the IOE through I2C. * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR * or IOE_2_ADDR. * @param RegisterAddr: The target register adress * @param RegisterValue: The target register value to be written * @retval IOE_OK: if all operations are OK. Other value if error. */ uint8_t I2C_WriteDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t RegisterValue) { uint32_t read_verif = 0; /* Reset all I2C2 registers */ I2C_SoftwareResetCmd(IOE_I2C, ENABLE); I2C_SoftwareResetCmd(IOE_I2C, DISABLE); TimeOut = TIMEOUT_MAX; /* Enable the IOE_I2C peripheral */ I2C_Cmd(IOE_I2C, ENABLE); /* Configure the I2C peripheral */ IOE_I2C_Config(); /* Begin the config sequence */ I2C_GenerateSTART(IOE_I2C, ENABLE); /* Test on EV5 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_MODE_SELECT)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Transmit the slave address and enable writing operation */ I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); /* Test on EV6 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Transmit the first address for r/w operations */ I2C_SendData(IOE_I2C, RegisterAddr); TimeOut = TIMEOUT_MAX; /* Test on EV8 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* Prepare the register value to be sent */ I2C_SendData(IOE_I2C, RegisterValue); /* Test on EV8 and clear it */ while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED)) { if (TimeOut-- == 0) return IOE_TIEMOUT; } /* End the configuration sequence */ I2C_GenerateSTOP(IOE_I2C, ENABLE); #ifdef VERIFY_WRITTENDATA /* Verify (if needed) that the loaded data is correct */ /* Read the just written register*/ read_verif = I2C_ReadDeviceRegister(DeviceAddr, RegisterAddr); /* Load the register and verify its value */ if (read_verif != RegisterValue) { /* Control data wrongly tranfered */ read_verif = IOE_FAILURE; } else { /* Control data correctly transfered */ read_verif = 0; } #endif /* Return the verifying value: 0 (Passed) or 1 (Failed) */ return read_verif; }
void Sensors_Read(uint8_t SensorType) { int g_x, g_y, g_z, a_x, a_y, a_z, m_x, m_y, m_z; switch (SensorType) { case GYRO: I2C_ReadDeviceRegister(I2C_COM1, ITG3200_ADDRESS, GYRO_XOUT_H_ITG | (1 << 7), MARG_SENSORS[GYRO]->ReadLength, (uint32_t) MARG_SENSORS[GYRO]->SensorRawValue); //I2C_ReadDeviceRegister_async(I2C_COM1,L3G3200_ADDRESS, OUT_X_L_G | (1 << 7) ,MARG_SENSORS[GYRO]->ReadLength,(uint32_t)MARG_SENSORS[GYRO]->SensorRawValue); g_x = ((int) ((int16_t) ((MARG_SENSORS[GYRO]->SensorRawValue[1]) | (MARG_SENSORS[GYRO]->SensorRawValue[0] << 8)))); g_y = ((int) ((int16_t) ((MARG_SENSORS[GYRO]->SensorRawValue[3]) | (MARG_SENSORS[GYRO]->SensorRawValue[2] << 8)))); g_z = ((int) ((int16_t) ((MARG_SENSORS[GYRO]->SensorRawValue[5]) | (MARG_SENSORS[GYRO]->SensorRawValue[4] << 8)))); sync_printf("GYRO: %d %d %d\r\n", g_x, g_y, g_z); break; /*init ITG3200*/ case ACC: //I2C_ReadDeviceRegister(I2C_COM1,LSM303_ACC_ADDRESS, OUT_X_H_A | (1 << 7) ,MARG_SENSORS[ACC]->ReadLength,(uint32_t)MARG_SENSORS[ACC]->SensorRawValue); I2C_ReadDeviceRegister(I2C_COM1, ADXL345_ADDRESS, Register_DataX_L | (1 << 7), MARG_SENSORS[ACC]->ReadLength, (uint32_t) MARG_SENSORS[ACC]->SensorRawValue); //ADXL345 a_x = ((int) ((int16_t) ((MARG_SENSORS[ACC]->SensorRawValue[0]) | (MARG_SENSORS[ACC]->SensorRawValue[1] << 8)))); a_y = ((int) ((int16_t) ((MARG_SENSORS[ACC]->SensorRawValue[2]) | (MARG_SENSORS[ACC]->SensorRawValue[3] << 8)))); a_z = ((int) ((int16_t) ((MARG_SENSORS[ACC]->SensorRawValue[4]) | (MARG_SENSORS[ACC]->SensorRawValue[5] << 8)))); sync_printf("ACC: %d %d %d\r\n", a_x, a_y, a_z); break; case MAG: //I2C_ReadDeviceRegister(I2C_COM1,LSM303_MAG_ADDRESS, OUT_X_H_M | (1 << 7) ,MARG_SENSORS[MAG]->ReadLength,(uint32_t)MARG_SENSORS[MAG]->SensorRawValue); I2C_ReadDeviceRegister(I2C_COM1, HMC5883L_ADDRESS, OUT_X_H_M | (1 << 7), MARG_SENSORS[MAG]->ReadLength, (uint32_t) MARG_SENSORS[MAG]->SensorRawValue); m_x = ((int) ((int16_t) ((MARG_SENSORS[MAG]->SensorRawValue[1]) | (MARG_SENSORS[MAG]->SensorRawValue[0] << 8)))); m_y = ((int) ((int16_t) ((MARG_SENSORS[MAG]->SensorRawValue[3]) | (MARG_SENSORS[MAG]->SensorRawValue[2] << 8)))); m_z = ((int) ((int16_t) ((MARG_SENSORS[MAG]->SensorRawValue[5]) | (MARG_SENSORS[MAG]->SensorRawValue[4] << 8)))); sync_printf("%d %d %d\r\n", m_x, m_y, m_z); break; default: I2C_ReadDeviceRegister(I2C_COM1, ITG3200_ADDRESS, GYRO_XOUT_H_ITG | (1 << 7), MARG_SENSORS[GYRO]->ReadLength, (uint32_t) MARG_SENSORS[GYRO]->SensorRawValue); //I2C_ReadDeviceRegister_async(I2C_COM1,L3G3200_ADDRESS, OUT_X_L_G | (1 << 7) ,MARG_SENSORS[GYRO]->ReadLength,(uint32_t)MARG_SENSORS[GYRO]->SensorRawValue); g_x = ((int) ((int16_t) ((MARG_SENSORS[GYRO]->SensorRawValue[1]) | (MARG_SENSORS[GYRO]->SensorRawValue[0] << 8)))); g_y = ((int) ((int16_t) ((MARG_SENSORS[GYRO]->SensorRawValue[3]) | (MARG_SENSORS[GYRO]->SensorRawValue[2] << 8)))); g_z = ((int) ((int16_t) ((MARG_SENSORS[GYRO]->SensorRawValue[5]) | (MARG_SENSORS[GYRO]->SensorRawValue[4] << 8)))); I2C_ReadDeviceRegister(I2C_COM1, ADXL345_ADDRESS, Register_DataX_L | (1 << 7), MARG_SENSORS[ACC]->ReadLength, (uint32_t) MARG_SENSORS[ACC]->SensorRawValue); //ADXL345 a_x = ((int) ((int16_t) ((MARG_SENSORS[ACC]->SensorRawValue[0]) | (MARG_SENSORS[ACC]->SensorRawValue[1] << 8)))); a_y = ((int) ((int16_t) ((MARG_SENSORS[ACC]->SensorRawValue[2]) | (MARG_SENSORS[ACC]->SensorRawValue[3] << 8)))); a_z = ((int) ((int16_t) ((MARG_SENSORS[ACC]->SensorRawValue[4]) | (MARG_SENSORS[ACC]->SensorRawValue[5] << 8)))); I2C_ReadDeviceRegister(I2C_COM1, HMC5883L_ADDRESS, OUT_X_H_M | (1 << 7), MARG_SENSORS[MAG]->ReadLength, (uint32_t) MARG_SENSORS[MAG]->SensorRawValue); m_x = ((int) ((int16_t) ((MARG_SENSORS[MAG]->SensorRawValue[1]) | (MARG_SENSORS[MAG]->SensorRawValue[0] << 8)))); m_y = ((int) ((int16_t) ((MARG_SENSORS[MAG]->SensorRawValue[3]) | (MARG_SENSORS[MAG]->SensorRawValue[2] << 8)))); m_z = ((int) ((int16_t) ((MARG_SENSORS[MAG]->SensorRawValue[5]) | (MARG_SENSORS[MAG]->SensorRawValue[4] << 8)))); async_printf("%d %d %d %d %d %d %d %d %d\r\n", a_x, a_y, a_z, g_x, g_y, g_z, m_x, m_y, m_z); break; } }