📄 tschal.c
字号:
/* Release reset signal of IOE_I2C IP */
RCC_APB1PeriphResetCmd(pTscHwParam.TSC_I2C_Clk, DISABLE);
/* IOE_I2C SCL and SDA pins configuration */
GPIO_InitStructure.GPIO_Pin = pTscHwParam.TSC_I2C_Scl_Gpio_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GL_GPIO_Init(pTscHwParam.TSC_I2C_Scl_Gpio_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = pTscHwParam.TSC_I2C_Sda_Gpio_Pin;
GL_GPIO_Init(pTscHwParam.TSC_I2C_Sda_Gpio_Port, &GPIO_InitStructure);
GPIO_PinAFConfig(pTscHwParam.TSC_I2C_Scl_Gpio_Port, pTscHwParam.TSC_I2C_Scl_PinSource, pTscHwParam.TSC_I2C_Scl_AltFunc);
GPIO_PinAFConfig(pTscHwParam.TSC_I2C_Sda_Gpio_Port, pTscHwParam.TSC_I2C_Sda_PinSource, pTscHwParam.TSC_I2C_Sda_AltFunc);
/* Set EXTI pin as Input PullUp - IO_Expander_INT */
GPIO_InitStructure.GPIO_Pin = pTscHwParam.TSC_IT_Gpio_Pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GL_GPIO_Init(pTscHwParam.TSC_IT_Gpio_Port, &GPIO_InitStructure);
#endif
/* Connect IO Expander IT line to EXTI line */
GL_GPIO_EXTILineConfig(pTscHwParam.TSC_PortSource, pTscHwParam.TSC_PinSource);
}
#endif
/**
* @brief Init the TS interface
* @param None
* @retval None
*/
void TSC_Init(void)
{
#if TOUCH_SCREEN_CAPABILITY
/* Reset the STMPE811 using the serial communication interface */
IOE_Config();
#endif
}
/**
* @brief Initializes the IO Expander registers.
* @param None
* @retval - 0: if all initializations are OK.
*/
uint32_t GL_TSC_Interface_Init(void)
{
#if TOUCH_SCREEN_CAPABILITY
/* Configure the needed pins */
TSC_GPIO_Configuration();
/* Read IO Expander 1 ID */
if(IOE_IsOperational(pTscHwParam.TSC_DeviceRegister))
{
return 1;
}
/* Generate IOExpander Software reset */
IOE_Reset(pTscHwParam.TSC_DeviceRegister);
/* Disable all the Functionnalities */
IOE_FnctCmd(pTscHwParam.TSC_DeviceRegister, IOE_IO_FCT | IOE_TEMPSENS_FCT | IOE_TS_FCT | IOE_ADC_FCT, DISABLE);
#endif
return 0; /* Configuration is OK */
}
/**
* @brief Configure NVIC for the Touchscreen
* @param None
* @retval None
*/
void TSC_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable and configure the priority of the IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannel = pTscHwParam.TSC_Exti_IrqChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
GL_NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief Configure the EXTI for the Touchscreen
* @param None
* @retval None
*/
void TSC_EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GL_EXTI_DeInit();
EXTI_InitStructure.EXTI_Line = pTscHwParam.TSC_Exti_Line;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
GL_EXTI_Init(&EXTI_InitStructure);
GL_GPIO_EXTILineConfig(pTscHwParam.TSC_PortSource, pTscHwParam.TSC_PinSource);
}
/**
* @brief Configures the used IRQ Channels and sets their priority.
* @param None
* @retval None
*/
void TSC_InterruptConfig(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Deinitializes the NVIC */
GL_NVIC_DeInit();
/* Set the Vector Table base address at 0x08000000 */
GL_NVIC_SetVectorTable(GL_NVIC_VectTab_FLASH, 0x00);
/* Configure the Priority Group to 2 bits */
GL_NVIC_PriorityGroupConfig(GL_NVIC_PriorityGroup_2);
/* Enable and configure RCC global IRQ channel */
NVIC_InitStructure.NVIC_IRQChannel = GL_RCC_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
GL_NVIC_Init(&NVIC_InitStructure);
/* Configure one bit for preemption priority */
GL_NVIC_PriorityGroupConfig(GL_NVIC_PriorityGroup_1);
/* Enable the EXTI15_10 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = pTscHwParam.TSC_Exti_IrqChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
GL_NVIC_Init(&NVIC_InitStructure);
GL_EXTI_DeInit();
}
/**
* @brief Enables or disables the High Speed APB (APB1 or APB2) peripheral clock.
* @param RCC_APBPeriph: specifies the APB peripheral to gates its clock.
* @param NewState: new state of the specified peripheral clock.
* This parameter can be one of the following values:
* @arg ENABLE
* @arg DISABLE
* @retval None
*/
void GL_RCC_APBPeriphClockCmd(uint32_t RCC_APBPeriph, GL_FunctionalState NewState, uint8_t APB_Selector)
{
#ifndef STM32F2XX
if (APB_Selector == 1)
RCC_APB1PeriphClockCmd(RCC_APBPeriph, (FunctionalState)NewState);
else
RCC_APB2PeriphClockCmd(RCC_APBPeriph, (FunctionalState)NewState);
#endif
}
/**
* @brief Enables or disables the AHB peripheral clock.
* @param RCC_APBPeriph: specifies the APB peripheral to gates its clock.
* For @b STM32_Connectivity_line_devices, this parameter can be any combination
* of the following values:
* @arg RCC_AHBPeriph_DMA1
* @arg RCC_AHBPeriph_DMA2
* @arg RCC_AHBPeriph_SRAM
* @arg RCC_AHBPeriph_FLITF
* @arg RCC_AHBPeriph_CRC
* @arg RCC_AHBPeriph_OTG_FS
* @arg RCC_AHBPeriph_ETH_MAC
* @arg RCC_AHBPeriph_ETH_MAC_Tx
* @arg RCC_AHBPeriph_ETH_MAC_Rx
* For @b other_STM32_devices, this parameter can be any combination
* of the following values:
* @arg RCC_AHBPeriph_DMA1
* @arg RCC_AHBPeriph_DMA2
* @arg RCC_AHBPeriph_SRAM
* @arg RCC_AHBPeriph_FLITF
* @arg RCC_AHBPeriph_CRC
* @arg RCC_AHBPeriph_FSMC
* @arg RCC_AHBPeriph_SDIO
* @param NewState: new state of the specified peripheral clock.
* This parameter can be one of the following values:
* @arg ENABLE
* @arg DISABLE
* @retval None
*/
void GL_RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, GL_FunctionalState NewState)
{
#ifndef STM32F2XX
RCC_AHBPeriphClockCmd(RCC_AHBPeriph, (FunctionalState)NewState);
#endif
}
/**
* @brief Write Data in data buffer to Flash.
* @param FlashFree_Address: Page address
* @param Data: pointer to data buffer
* @param Size: data buffer size in bytes
* @retval FLASH programming status
*/
TSC_FLASH_Status TSC_WriteDataToNVM(uint32_t FlashFree_Address, int32_t *Data, uint32_t Size)
{
TSC_FLASH_Status TSC_FlashStatus = TSC_FLASH_COMPLETE;
#if TOUCH_SCREEN_CAPABILITY
uint32_t words = (Size/sizeof(uint32_t)) + ((Size%sizeof(uint32_t))?1:0);
uint32_t index = 0;
/* Unlock the Flash Program Erase controller */
TSC_FLASH_Unlock();
/* Erase Flash sectors ******************************************************/
/* Clear All pending flags */
TSC_FLASH_ClearFlag( TSC_FLASH_FLAG_BSY | TSC_FLASH_FLAG_EOP | TSC_FLASH_FLAG_PGERR | TSC_FLASH_FLAG_WRPRTERR);
/* Erase Last Flash Page */
TSC_FlashStatus = TSC_FLASH_ErasePage( FlashFree_Address );
for(index = 0; index < words; index++)
{
/* Writing calibration parameters to the Flash Memory */
TSC_FlashStatus = TSC_FLASH_ProgramWord( FlashFree_Address, Data[index]);
/* Increasing Flash Memory Page Address */
FlashFree_Address = FlashFree_Address + 4;
}
#endif
return TSC_FlashStatus;
}
/**
* @brief Unlocks the FLASH Program Erase Controller.
* @param None
* @retval None
*/
void TSC_FLASH_Unlock(void)
{
FLASH_Unlock();
}
/**
* @brief Clears the FLASH抯 pending flags.
* @param LASH_FLAG: specifies the FLASH flags to clear.
* This parameter can be any combination of the following values:
* @arg TSC_FLASH_FLAG_PGERR: FLASH Program error flag
* @arg TSC_FLASH_FLAG_WRPRTERR: FLASH Write protected error flag
* @arg TSC_FLASH_FLAG_EOP: FLASH End of Operation flag
* @retval None
*/
void TSC_FLASH_ClearFlag(uint32_t FLASH_FLAG)
{
FLASH_ClearFlag(FLASH_FLAG);
}
/**
* @brief Erases a specified FLASH page.
* @param Page_Address: The page address to be erased.
* @retval None
*/
TSC_FLASH_Status TSC_FLASH_ErasePage(uint32_t Page_Address)
{
TSC_FLASH_Status TSC_FlashStatus = TSC_FLASH_COMPLETE;
#ifndef STM32F2XX
TSC_FlashStatus = FLASH_ErasePage(Page_Address);
#else
uint8_t index = 0;
uint32_t size = 0;
while(size < Page_Address && index < 12)
{
size += FLASH_Sectors[1][index];
index++;
}
if(size != Page_Address && index != 0)
{
index--;
}
TSC_FlashStatus = FLASH_EraseSector(FLASH_Sectors[0][index], VoltageRange_3);
#endif
return TSC_FlashStatus;
}
/**
* @brief Programs a word at a specified address.
* @param Address: specifies the address to be programmed.
* @param Data: specifies the data to be programmed
* @retval None
*/
TSC_FLASH_Status TSC_FLASH_ProgramWord(uint32_t Address, uint32_t Data)
{
return FLASH_ProgramWord(Address, Data);
}
/**
* @brief Set the last Flash Memory address
* @param address: Pointer to the penultimate memory page
* @retval None
*/
void Set_LastFlashMemoryAddress( uint32_t address)
{
EndAddr = address;
/* Calculate the address of the Penultimate Flash Memory Page, where the calibration parameters will be saved. */
CalibrationAddr = (uint32_t)(EndAddr - 0x800);
}
/**
* @brief Read the coordinate of the point touched and assign their
* value to the variables u32_TSXCoordinate and u32_TSYCoordinate
* @param None
* @retval None
*/
void TSC_Read(void)
{
#if TOUCH_SCREEN_CAPABILITY
TSC_Value_X = 0x00;
TSC_Value_Y = 0x00;
if((I2C_ReadDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_TSC_CTRL) & 0x80))
{
GL_Delay(TOUCH_DELAY);
/* Stop touchscreen controller */
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_TSC_CTRL, 0x00);
TSC_Value_X = I2C_ReadDataBuffer(pTscHwParam.TSC_DeviceRegister, GL_TSC_DATA_Y);
TSC_Value_Y = I2C_ReadDataBuffer(pTscHwParam.TSC_DeviceRegister, GL_TSC_DATA_X);
I2C_ReadDataBuffer(pTscHwParam.TSC_DeviceRegister, GL_TSC_DATA_Z);
u32_TSXCoordinate = getDisplayCoordinateX( TSC_Value_X, TSC_Value_Y );
u32_TSYCoordinate = getDisplayCoordinateY( TSC_Value_X, TSC_Value_Y );
touch_done = 1;
/* Clear the interrupt pending bit and enable the FIFO again */
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_FIFO_CTRL_STA, 0x01);
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_INT_STA, IOE_TS_IT);
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_FIFO_CTRL_STA, 0x00);
GL_Delay(0x02);
/* Enable touchscreen controller */
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_TSC_CTRL, 0x01);
GL_Delay(0x05);
/* check if the FIFO is not empty */
if(I2C_ReadDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_FIFO_CTRL_STA) != 0x20)
{
/* Flush the FIFO */
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_FIFO_CTRL_STA, 0x01);
I2C_WriteDeviceRegister(pTscHwParam.TSC_DeviceRegister, GL_FIFO_CTRL_STA, 0x00);
}
}
else
{
GL_Delay(1);
}
#endif
}
/**
* @}
*/
/**
* @}
*/
/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -