?? stm3210c_eval_ioe.c
字號:
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 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;
}
/**
* @brief Reads a register of the audio Codec through I2C.
* @param DeviceAddr: The address of the IOExpander, 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)
{
uint32_t tmp = 0;
/* Disable the IOE_I2C peripheral */
I2C_Cmd(IOE_I2C, DISABLE);
/* Reset all I2C2 registers */
I2C_SoftwareResetCmd(IOE_I2C, ENABLE);
I2C_SoftwareResetCmd(IOE_I2C, DISABLE);
/* Configure the I2C peripheral */
IOE_I2C_Config();
/* Enable the I2C peripheral */
I2C_GenerateSTART(IOE_I2C, ENABLE);
TimeOut = TIMEOUT_MAX;
/* Test on EV5 and clear it */
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_MODE_SELECT))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(IOE_I2C, DISABLE);
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter);
TimeOut = TIMEOUT_MAX;
/* 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);
/* Test on EV8 and clear it */
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Regenerate a start condition */
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_Receiver);
/* Test on EV6 and clear it */
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Test on EV7 and clear it */
while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* End the configuration sequence */
I2C_GenerateSTOP(IOE_I2C, ENABLE);
/* Load the register value */
tmp = I2C_ReceiveData(IOE_I2C);
/* Enable Acknowledgement */
I2C_AcknowledgeConfig(IOE_I2C, ENABLE);
/* Return the read value */
return tmp;
}
/**
* @brief Reads a buffer of 4 bytes from IO_Expander registers.
* @param DeviceAddr: The address of the IOExpander, 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)
*/
uint32_t I2C_ReadDataBuffer(uint8_t DeviceAddr, uint32_t RegisterAddr)
{
uint8_t Buffer[4] , idx = 2;
/* Initialize the buffer */
Buffer[0] = 0;
Buffer[1] = 0;
Buffer[2] = 0;
Buffer[3] = 0;
/* Disable the I2C1 peripheral */
I2C_Cmd(I2C1, DISABLE);
/* Reset all I2C2 registers */
I2C_SoftwareResetCmd(I2C1, ENABLE);
I2C_SoftwareResetCmd(I2C1, DISABLE);
/* Configure the I2C peripheral */
IOE_I2C_Config();
/* Enable the I2C peripheral */
I2C_GenerateSTART(I2C1, ENABLE);
TimeOut = TIMEOUT_MAX;
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Send device address for write */
I2C_Send7bitAddress(I2C1, DeviceAddr, I2C_Direction_Transmitter);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);
/* Send the device's internal address to write to */
I2C_SendData(I2C1, RegisterAddr);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Send STRAT condition a second time */
I2C_GenerateSTART(I2C1, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* Send EEPROM address for read */
I2C_Send7bitAddress(I2C1, DeviceAddr, I2C_Direction_Receiver);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
{
if (TimeOut-- == 0) return IOE_TIEMOUT;
}
/* While there is data to be read */
while(idx)
{
if(idx == 1)
{
/* Disable Acknowledgement */
I2C_AcknowledgeConfig(I2C1, DISABLE);
/* Send STOP Condition */
I2C_GenerateSTOP(I2C1, ENABLE);
}
/* Test on EV7 and clear it */
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED))
{
/* Read a byte from the EEPROM */
Buffer[idx-1] = I2C_ReceiveData(I2C1);
/* Decrement the read bytes counter */
idx--;
}
}
/* Enable Acknowledgement to be ready for another reception */
I2C_AcknowledgeConfig(I2C1, ENABLE);
/* return a pointer to the buffer */
return *(uint32_t *)Buffer;
}
/**
* @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(RCC_APB1Periph_IOE_I2C, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB_IOE_I2C_PORT | RCC_APB_GPIO_IOE_ITPORT | RCC_APB2Periph_AFIO, ENABLE);
/* Reset IOE_I2C IP */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_IOE_I2C, ENABLE);
/* Release reset signal of IOE_I2C IP */
RCC_APB1PeriphResetCmd(RCC_APB1Periph_IOE_I2C, DISABLE);
/* IOE_I2C SCL and SDA pins configuration */
GPIO_InitStructure.GPIO_Pin = IOE_SCL_PIN | IOE_SDA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(IOE_I2C_PORT, &GPIO_InitStructure);
/* Set EXTI pin as Input PullUp - IO_Expander_INT */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_IOE_ITPIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIO_IOE_ITPORT, &GPIO_InitStructure);
/* Connect IO Expander IT line to EXTI line */
GPIO_EXTILineConfig(GPIO_PortSource_IOE_ITPORT, GPIO_PinSource_IOE_ITPIN);
}
/**
* @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 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(RCC_APB_GPIO_IOE_ITPORT | RCC_APB2Periph_AFIO, ENABLE);
/* Configure Button pin as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_IOE_ITPIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIO_IOE_ITPORT, &GPIO_InitStructure);
/* Connect Button EXTI Line to Button GPIO Pin */
GPIO_EXTILineConfig(GPIO_PortSource_IOE_ITPORT, GPIO_PinSource_IOE_ITPIN);
/* Configure Button EXTI line */
EXTI_InitStructure.EXTI_Line = EXTI_LINE_IOE_ITLINE;
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 2009 STMicroelectronics *****END OF FILE****/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -