📄 stm3210c_eval_ioe.c
字号:
return (JOYState_TypeDef) JOY_DOWN;
}
else if (!(tmp & JOY_IO_LEFT))
{
return (JOYState_TypeDef) JOY_LEFT;
}
else if (!(tmp & JOY_IO_RIGHT))
{
return (JOYState_TypeDef) JOY_RIGHT;
}
else if (!(tmp & JOY_IO_UP))
{
return (JOYState_TypeDef) JOY_UP;
}
else
{
return (JOYState_TypeDef) JOY_NONE;
}
}
/**
* @brief Returns Status and positions of the Touch screen.
* @param None
* @retval Pointer to TS_STATE structure holding Touch Screen information.
*/
TS_STATE* IOE_TS_GetState(void)
{
uint32_t xDiff, yDiff , x , y, count;
static uint32_t _x = 0, _y = 0;
/* Check if the Touch detect event happenned */
TS_State.TouchDetected = (I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CTRL) & 0x80);
/* Wait till end of ADC conversion */
for (count = TS_CONVERSION_DELAY; count > 0; count--);
if(TS_State.TouchDetected)
{
x = IOE_TS_Read_X();
y = IOE_TS_Read_Y();
xDiff = x > _x? (x - _x): (_x - x);
yDiff = y > _y? (y - _y): (_y - y);
if (xDiff + yDiff > 5)
{
_x = x;
_y = y;
}
}
/* Update the X position */
TS_State.X = _x;
/* Update the Y position */
TS_State.Y = _y;
/* Update the Z Pression index */
TS_State.Z = IOE_TS_Read_Z();
/* Clear the interrupt pending bit and enable the FIFO again */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x01);
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x00);
/* Return pointer to the updated structure */
return &TS_State;
}
/**
* @brief Returns the temperature row value (in 16 bit format).
* @param None
* @retval The temperature row value.
*/
uint32_t IOE_TempSens_GetData(void)
{
static __IO uint32_t tmp = 0;
/* Aquire data enable */
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_TEMP_CTRL, 0x03);
/* Enable the TEMPSENS module */
tmp = (uint32_t)((I2C_ReadDeviceRegister(IOE_2_ADDR, IOE_REG_TEMP_DATA) & 0x03) << 8);
tmp |= (uint32_t)I2C_ReadDeviceRegister(IOE_2_ADDR, IOE_REG_TEMP_DATA + 1);
tmp = (uint32_t)((33 * tmp * 100) / 751);
tmp = (uint32_t)((tmp + 5) / 10);
/* return the temprature row value */
return tmp;
}
/**
* @brief Checks the selected Global interrupt source pending bit
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param Global_IT: the Global interrupt source to be checked, could be:
* @arg Global_IT_GPIO : All IOs interrupt
* @arg Global_IT_ADC : ADC interrupt
* @arg Global_IT_TEMP : Temperature Sensor interrupts
* @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt
* @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt
* @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt
* @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt
* @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt
* @retval Status of the checked flag. Could be SET or RESET.
*/
FlagStatus IOE_GetGITStatus(uint8_t DeviceAddr, uint8_t Global_IT)
{
__IO uint8_t tmp = 0;
/* get the Interrupt status */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_STA);
if ((tmp & (uint8_t)Global_IT) != 0)
{
return SET;
}
else
{
return RESET;
}
}
/**
* @brief Clears the selected Global interrupt pending bit(s)
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param Global_IT: the Global interrupt to be cleared, could be any combination
* of the following values:
* @arg Global_IT_GPIO : All IOs interrupt
* @arg Global_IT_ADC : ADC interrupt
* @arg Global_IT_TEMP : Temperature Sensor interrupts
* @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt
* @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt
* @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt
* @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt
* @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_ClearGITPending(uint8_t DeviceAddr, uint8_t Global_IT)
{
/* Write 1 to the bits that have to be cleared */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_STA, Global_IT);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Checks the status of the selected IO interrupt pending bit
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param IO_IT: the IO interrupt to be checked could be IO_ITx Where x can be
* from 0 to 7.
* @retval Status of the checked flag. Could be SET or RESET.
*/
FlagStatus IOE_GetIOITStatus(uint8_t DeviceAddr, uint8_t IO_IT)
{
uint8_t tmp = 0;
/* get the Interrupt status */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_STA);
if ((tmp & (uint8_t)IO_IT) != 0)
{
return SET;
}
else
{
return RESET;
}
}
/**
* @brief Clears the selected IO interrupt pending bit(s).
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param IO_IT: the IO interrupt to be checked could be IO_ITx Where x can be
* from 0 to 7.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_ClearIOITPending(uint8_t DeviceAddr, uint8_t IO_IT)
{
/* Write 1 to the bits that have to be cleared */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_STA, IO_IT);
/* Clear the Edge detection pending bit*/
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_GPIO_ED, IO_IT);
/* Clear the Rising edge pending bit */
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_GPIO_RE, IO_IT);
/* Clear the Falling edge pending bit */
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_GPIO_FE, IO_IT);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Checks if the selected device is correctly configured and
* communicates correctly ont the I2C bus.
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @retval IOE_OK if IOE is operational. Other value if failure.
*/
uint8_t IOE_IsOperational(uint8_t DeviceAddr)
{
/* Return Error if the ID is not correct */
if( IOE_ReadID(DeviceAddr) != (uint16_t)STMPE811_ID )
{
/* Check if a Timeout occured */
if (IOE_TimeOut == 0)
{
return (IOE_TimeoutUserCallback());
}
else
{
return IOE_FAILURE; /* ID is not Correct */
}
}
else
{
return IOE_OK; /* ID is correct */
}
}
/**
* @brief Resets the IO Expander by Software (SYS_CTRL1, RESET bit).
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_Reset(uint8_t DeviceAddr)
{
/* Power Down the IO_Expander */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL1, 0x02);
/* wait for a delay to insure registers erasing */
_delay_(2);
/* Power On the Codec after the power off => all registers are reinitialized*/
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL1, 0x00);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Reads the selected device's ID.
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @retval The Device ID (two bytes).
*/
uint16_t IOE_ReadID(uint8_t DeviceAddr)
{
uint16_t tmp = 0;
/* Read device ID */
tmp = I2C_ReadDeviceRegister(DeviceAddr, 0);
tmp = (uint32_t)(tmp << 8);
tmp |= (uint32_t)I2C_ReadDeviceRegister(DeviceAddr, 1);
/* Return the ID */
return (uint16_t)tmp;
}
/**
* @brief Configures the selcted 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 : Tempreature 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 Configures the selected pin direction (to be an input or an output)
* @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 Direction: could be Direction_IN or Direction_OUT.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_IOPinConfig(uint8_t DeviceAddr, uint8_t IO_Pin, uint8_t Direction)
{
uint8_t tmp = 0;
/* Get all the Pins direction */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_DIR);
if (Direction != Direction_IN)
{
tmp |= (uint8_t)IO_Pin;
}
else
{
tmp &= ~(uint8_t)IO_Pin;
}
/* Write the register new value */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_DIR, 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 source to generate or not a global interrupt
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param Global_IT: the interrupt source to be configured, could be:
* @arg Global_IT_GPIO : All IOs interrupt
* @arg Global_IT_ADC : ADC interrupt
* @arg Global_IT_TEMP : Temperature Sensor interrupts
* @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt
* @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt
* @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt
* @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt
* @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_GITConfig(uint8_t DeviceAddr, uint8_t Global_IT, FunctionalState NewState)
{
uint8_t tmp = 0;
/* Get the current value of the INT_EN register */
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_EN);
if (NewState != DISABLE)
{
/* Set the interrupts to be Enabled */
tmp |= (uint8_t)Global_IT;
}
else
{
/* Set the interrupts to be Disabled */
tmp &= ~(uint8_t)Global_IT;
}
/* Set the register */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_EN, tmp);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -