📄 stm3210c_eval_ioe.c
字号:
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send IOExpander address for read */
I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Receiver);
/* Test on ADDR Flag */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Enable I2C DMA request */
I2C_DMACmd(IOE_I2C,ENABLE);
/* Enable DMA RX Channel */
DMA_Cmd(IOE_DMA_RX_CHANNEL, ENABLE);
/* Wait until DMA Transfer Complete */
IOE_TimeOut = 2 * TIMEOUT_MAX;
while (!DMA_GetFlagStatus(IOE_DMA_RX_TCFLAG))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send STOP Condition */
I2C_GenerateSTOP(IOE_I2C, ENABLE);
/* Disable DMA RX Channel */
DMA_Cmd(IOE_DMA_RX_CHANNEL, DISABLE);
/* Disable I2C DMA request */
I2C_DMACmd(IOE_I2C,DISABLE);
/* Clear DMA RX Transfer Complete Flag */
DMA_ClearFlag(IOE_DMA_RX_TCFLAG);
/* return a pointer to the IOE_Buffer */
return (uint8_t)IOE_BufferRX[0];
}
/**
* @brief Reads a buffer of 2 bytes from the device registers.
* @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 A pointer to the buffer containing the two returned bytes (in halfword).
*/
uint16_t I2C_ReadDataBuffer(uint8_t DeviceAddr, uint32_t RegisterAddr)
{
uint8_t tmp= 0;
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 */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send IOExpander address for read */
I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Receiver);
/* Test on ADDR Flag */
IOE_TimeOut = TIMEOUT_MAX;
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Enable I2C DMA request */
I2C_DMACmd(IOE_I2C,ENABLE);
/* Enable DMA RX Channel */
DMA_Cmd(IOE_DMA_RX_CHANNEL, ENABLE);
/* Wait until DMA Transfer Complete */
IOE_TimeOut = 2 * TIMEOUT_MAX;
while (!DMA_GetFlagStatus(IOE_DMA_RX_TCFLAG))
{
if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback());
}
/* Send STOP Condition */
I2C_GenerateSTOP(IOE_I2C, ENABLE);
/* Disable DMA RX Channel */
DMA_Cmd(IOE_DMA_RX_CHANNEL, DISABLE);
/* Disable I2C DMA request */
I2C_DMACmd(IOE_I2C,DISABLE);
/* Clear DMA RX Transfer Complete Flag */
DMA_ClearFlag(IOE_DMA_RX_TCFLAG);
/* Reorganize received data */
tmp = IOE_BufferRX[0];
IOE_BufferRX[0] = IOE_BufferRX[1];
IOE_BufferRX[1] = tmp;
/* return a pointer to the IOE_Buffer */
return *(uint16_t *)IOE_BufferRX;
}
/**
* @brief Return Touch Screen X position value
* @param None
* @retval X position.
*/
static uint16_t IOE_TS_Read_X(void)
{
int32_t x, xr;
x = I2C_ReadDataBuffer(IOE_1_ADDR, IOE_REG_TSC_DATA_Y);
/* first correction */
xr = (x * 320) >> 12;
/* second correction */
xr = ((xr * 32)/29) - 17;
if(xr <= 0)
xr = 0;
return (uint16_t)(xr);
}
/**
* @brief Return Touch Screen Y position value
* @param None
* @retval Y position.
*/
static uint16_t IOE_TS_Read_Y(void)
{
int32_t y, yr;
y= I2C_ReadDataBuffer(IOE_1_ADDR, IOE_REG_TSC_DATA_X);
yr= (y * 240) >> 12;
yr = ((yr * 240) / 217) - 12;
if(yr <= 0)
yr = 0;
return (uint16_t)(yr);
}
/**
* @brief Return Touch Screen Z position value
* @param None
* @retval Z position.
*/
static uint16_t IOE_TS_Read_Z(void)
{
uint32_t z;
z = I2C_ReadDataBuffer(IOE_1_ADDR, IOE_REG_TSC_DATA_Z);
if(z <= 0)
z = 0;
return (uint16_t)(z);
}
/**
* @brief Initializes the GPIO pins used by the IO expander.
* @param None
* @retval None
*/
static void IOE_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable IOE_I2C and IOE_I2C_PORT & Alternate Function clocks */
RCC_APB1PeriphClockCmd(IOE_I2C_CLK, ENABLE);
RCC_APB2PeriphClockCmd(IOE_I2C_SCL_GPIO_CLK | IOE_I2C_SDA_GPIO_CLK | IOE_IT_GPIO_CLK
| RCC_APB2Periph_AFIO, ENABLE);
/* Reset IOE_I2C IP */
RCC_APB1PeriphResetCmd(IOE_I2C_CLK, ENABLE);
/* Release reset signal of IOE_I2C IP */
RCC_APB1PeriphResetCmd(IOE_I2C_CLK, DISABLE);
/* IOE_I2C SCL and SDA pins configuration */
GPIO_InitStructure.GPIO_Pin = IOE_I2C_SCL_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(IOE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure);
/* IOE_I2C SCL and SDA pins configuration */
GPIO_InitStructure.GPIO_Pin = IOE_I2C_SDA_PIN;
GPIO_Init(IOE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure);
/* Set EXTI pin as Input PullUp - IO_Expander_INT */
GPIO_InitStructure.GPIO_Pin = IOE_IT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(IOE_IT_GPIO_PORT, &GPIO_InitStructure);
/* Connect IO Expander IT line to EXTI line */
GPIO_EXTILineConfig(IOE_IT_EXTI_PORT_SOURCE, IOE_IT_EXTI_PIN_SOURCE);
}
/**
* @brief Configure the I2C Peripheral used to communicate with IO_Expanders.
* @param None
* @retval None
*/
static void IOE_I2C_Config(void)
{
I2C_InitTypeDef I2C_InitStructure;
/* IOE_I2C configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = IOE_I2C_SPEED;
I2C_Init(IOE_I2C, &I2C_InitStructure);
}
/**
* @brief Configure the DMA Peripheral used to handle communication via I2C.
* @param None
* @retval None
*/
static void IOE_DMA_Config(IOE_DMADirection_TypeDef Direction, uint8_t* buffer)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(IOE_DMA_CLK, ENABLE);
/* Initialize the DMA_PeripheralBaseAddr member */
DMA_InitStructure.DMA_PeripheralBaseAddr = IOE_I2C_DR;
/* Initialize the DMA_MemoryBaseAddr member */
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer;
/* Initialize the DMA_PeripheralInc member */
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
/* Initialize the DMA_MemoryInc member */
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
/* Initialize the DMA_PeripheralDataSize member */
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
/* Initialize the DMA_MemoryDataSize member */
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
/* Initialize the DMA_Mode member */
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
/* Initialize the DMA_Priority member */
DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
/* Initialize the DMA_M2M member */
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
/* If using DMA for Reception */
if (Direction == IOE_DMA_RX)
{
/* Initialize the DMA_DIR member */
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
/* Initialize the DMA_BufferSize member */
DMA_InitStructure.DMA_BufferSize = 2;
DMA_DeInit(IOE_DMA_RX_CHANNEL);
DMA_Init(IOE_DMA_RX_CHANNEL, &DMA_InitStructure);
}
/* If using DMA for Transmission */
else if (Direction == IOE_DMA_TX)
{
/* Initialize the DMA_DIR member */
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
/* Initialize the DMA_BufferSize member */
DMA_InitStructure.DMA_BufferSize = 1;
DMA_DeInit(IOE_DMA_TX_CHANNEL);
DMA_Init(IOE_DMA_TX_CHANNEL, &DMA_InitStructure);
}
}
/**
* @brief Configures the IO expander Interrupt line and GPIO in EXTI mode.
* @param None
* @retval None
*/
static void IOE_EXTI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
/* Enable Button GPIO clock */
RCC_APB2PeriphClockCmd(IOE_IT_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE);
/* Configure Button pin as input floating */
GPIO_InitStructure.GPIO_Pin = IOE_IT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(IOE_IT_GPIO_PORT, &GPIO_InitStructure);
/* Connect Button EXTI Line to Button GPIO Pin */
GPIO_EXTILineConfig(IOE_IT_EXTI_PORT_SOURCE, IOE_IT_EXTI_PIN_SOURCE);
/* Configure Button EXTI line */
EXTI_InitStructure.EXTI_Line = IOE_IT_EXTI_LINE;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set Button EXTI Interrupt to the lowest priority */
NVIC_InitStructure.NVIC_IRQChannel = IOE_IT_EXTI_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#ifndef USE_Delay
/**
* @brief Inserts a delay time.
* @param nCount: specifies the delay time length.
* @retval None
*/
static void delay(__IO uint32_t nCount)
{
__IO uint32_t index = 0;
for(index = (100000 * nCount); index != 0; index--)
{
}
}
#endif /* USE_Delay*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -