📄 stm3210c_eval_ioe.c
字号:
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Configures the selected pins to generate an interrupt or not.
* @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param IO_IT: The IO interrupt to be configured. This parameter could be any
* combination of the following values:
* @arg IO_IT_x: where x can be from 0 to 7.
* @param NewState: could be ENABLE or DISABLE.
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_IOITConfig(uint8_t DeviceAddr, uint8_t IO_IT, FunctionalState NewState)
{
uint8_t tmp = 0;
tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_EN);
if (NewState != DISABLE)
{
/* Set the interrupts to be Enabled */
tmp |= (uint8_t)IO_IT;
}
else
{
/* Set the interrupts to be Disabled */
tmp &= ~(uint8_t)IO_IT;
}
/* Set the register */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_EN, tmp);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Configures the touch Screen Controller (Single point detection)
* @param None
* @retval IOE_OK if all initializations are OK. Other value if error.
*/
uint8_t IOE_TS_Config(void)
{
uint8_t tmp = 0;
/* Enable TSC Fct: already done in IOE_Config */
tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_SYS_CTRL2);
tmp &= ~(uint32_t)(IOE_TS_FCT | IOE_ADC_FCT);
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_SYS_CTRL2, tmp);
/* Enable the TSC gloabl interrupts */
tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_INT_EN);
tmp |= (uint32_t)(IOE_GIT_TOUCH | IOE_GIT_FTH | IOE_GIT_FOV);
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_INT_EN, tmp);
/* Select Sample Time, bit number and ADC Reference */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_ADC_CTRL1, 0x49);
/* Wait for ~20 ms */
_delay_(2);
/* Select the ADC clock speed: 3.25 MHz */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_ADC_CTRL2, 0x01);
/* Select TSC pins in non default mode */
tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_AF);
tmp &= ~(uint8_t)TOUCH_IO_ALL;
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_AF, tmp);
/* Select 2 nF filter capacitor */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CFG, 0x9A);
/* Select single point reading */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_TH, 0x01);
/* Write 0x01 to clear the FIFO memory content. */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x01);
/* Write 0x00 to put the FIFO back into operation mode */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x00);
/* set the data format for Z value: 7 fractional part and 1 whole part */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_FRACT_XYZ, 0x01);
/* set the driving capability of the device for TSC pins: 50mA */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_I_DRIVE, 0x01);
/* Use no tracking index, touchscreen controller operation mode (XYZ) and
enable the TSC */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CTRL, 0x01);
/* Clear all the status pending bits */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_INT_STA, 0xFF);
/* Initialize the TS structure to their default values */
TS_State.TouchDetected = TS_State.X = TS_State.Y = TS_State.Z = 0;
/* All configuration done */
return IOE_OK;
}
/**
* @brief Configures and enables the Temperature sensor module.
* @param None
* @retval IOE_OK if all initializations are OK. Other value if error.
*/
uint8_t IOE_TempSens_Config(void)
{
__IO uint8_t tmp = 0;
/* Enable Temperature Sensor Fct: already done in IOE_Config */
tmp = I2C_ReadDeviceRegister(IOE_2_ADDR, IOE_REG_SYS_CTRL2);
tmp &= ~(uint32_t)(IOE_TEMPSENS_FCT | IOE_ADC_FCT);
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_SYS_CTRL2, tmp);
/* Enable the TEMPSENS module */
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_TEMP_CTRL, 0x01);
/* Aquire data enable */
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_TEMP_CTRL, 0x3);
/* All configuration done */
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 valu in GPIO_AF register */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_AF, tmp);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @brief Configures the Edge for which a transition is detectable for the
* the selected pin.
* @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 Edge: The edge which will be detected. This parameter can be one or a
* a combination of follwing values: EDGE_FALLING and EDGE_RISING .
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_IOEdgeConfig(uint8_t DeviceAddr, uint8_t IO_Pin, uint8_t Edge)
{
uint8_t tmp1 = 0, tmp2 = 0;
/* Get the registers values */
tmp1 = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_FE);
tmp2 = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_RE);
/* Disable the Falling Edge */
tmp1 &= ~(uint8_t)IO_Pin;
/* Disable the Falling Edge */
tmp2 &= ~(uint8_t)IO_Pin;
/* Enable the Falling edge if selected */
if (Edge & EDGE_FALLING)
{
tmp1 |= (uint8_t)IO_Pin;
}
/* Enable the Rising edge if selected */
if (Edge & EDGE_RISING)
{
tmp2 |= (uint8_t)IO_Pin;
}
/* Write back the registers values */
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_FE, tmp1);
I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_RE, tmp2);
/* if OK return 0 */
return IOE_OK;
}
/**
* @brief Configures the Interrupt line active state and format (level/edge)
* @param Polarity: could be
* @arg Polarity_Low: Interrupt line is active Low/Falling edge
* @arg Polarity_High: Interrupt line is active High/Rising edge
* @param Type: Interrupt line activity type, could be one of the following values
* @arg Type_Level: Interrupt line is active in level model
* @arg Type_Edge: Interrupt line is active in edge model
* @retval IOE_OK: if all initializations are OK. Other value if error.
*/
uint8_t IOE_ITOutConfig(uint8_t Polarity, uint8_t Type)
{
uint8_t tmp = 0;
/* Get the register IOE_REG_INT_CTRL value */
tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_INT_CTRL);
/* Mask the polarity and type bits */
tmp &= ~(uint8_t)0x06;
/* Modify the Interrupt Output line configuration */
tmp |= (uint8_t)(Polarity | Type);
/* Set the register */
I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_INT_CTRL, tmp);
/* Get the register IOE_REG_INT_CTRL value */
tmp = I2C_ReadDeviceRegister(IOE_2_ADDR, IOE_REG_INT_CTRL);
/* Mask the polarity and type bits */
tmp &= ~(uint8_t)0x06;
/* Modify the Interrupt Output line configuration */
tmp |= (uint8_t)(Polarity | Type);
/* Set the register */
I2C_WriteDeviceRegister(IOE_2_ADDR, IOE_REG_INT_CTRL, tmp);
/* If all OK return IOE_OK */
return IOE_OK;
}
/**
* @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;
}
/**
* @brief Reads a register of the device through I2C.
* @param DeviceAddr: The address of the device, could be : IOE_1_ADDR
* or IOE_2_ADDR.
* @param RegisterAddr: The target register adress (between 00x and 0x24)
* @retval The value of the read register (0xAA if Timout occured)
*/
uint8_t I2C_ReadDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr)
{
uint8_t IOE_BufferRX[2] = {0x00, 0x00};
/* Configure DMA Peripheral */
IOE_DMA_Config(IOE_DMA_RX, (uint8_t*)IOE_BufferRX);
/* Enable DMA NACK automatic generation */
I2C_DMALastTransferCmd(IOE_I2C, ENABLE);
/* 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))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send device address for write */
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());
}
/* Send the device's internal address to write to */
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());
}
/* Send START condition a second time */
I2C_GenerateSTART(IOE_I2C, ENABLE);
/* Test on SB Flag */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -