stmpe811.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  1. /**
  2. ******************************************************************************
  3. * @file stmpe811.c
  4. * @author MCD Application Team
  5. * @version V2.0.0
  6. * @date 15-December-2014
  7. * @brief This file provides a set of functions needed to manage the STMPE811
  8. * IO Expander devices.
  9. ******************************************************************************
  10. * @attention
  11. *
  12. * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
  13. *
  14. * Redistribution and use in source and binary forms, with or without modification,
  15. * are permitted provided that the following conditions are met:
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  22. * may be used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  29. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  31. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  33. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  34. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ******************************************************************************
  37. */
  38. /* Includes ------------------------------------------------------------------*/
  39. #include "stmpe811.h"
  40. /** @addtogroup BSP
  41. * @{
  42. */
  43. /** @addtogroup Components
  44. * @{
  45. */
  46. /** @defgroup STMPE811
  47. * @{
  48. */
  49. /** @defgroup STMPE811_Private_Types_Definitions
  50. * @{
  51. */
  52. /** @defgroup STMPE811_Private_Defines
  53. * @{
  54. */
  55. #define STMPE811_MAX_INSTANCE 2
  56. /**
  57. * @}
  58. */
  59. /** @defgroup STMPE811_Private_Macros
  60. * @{
  61. */
  62. /**
  63. * @}
  64. */
  65. /** @defgroup STMPE811_Private_Variables
  66. * @{
  67. */
  68. /* Touch screen driver structure initialization */
  69. TS_DrvTypeDef stmpe811_ts_drv =
  70. {
  71. stmpe811_Init,
  72. stmpe811_ReadID,
  73. stmpe811_Reset,
  74. stmpe811_TS_Start,
  75. stmpe811_TS_DetectTouch,
  76. stmpe811_TS_GetXY,
  77. stmpe811_TS_EnableIT,
  78. stmpe811_TS_ClearIT,
  79. stmpe811_TS_ITStatus,
  80. stmpe811_TS_DisableIT,
  81. };
  82. /* IO driver structure initialization */
  83. IO_DrvTypeDef stmpe811_io_drv =
  84. {
  85. stmpe811_Init,
  86. stmpe811_ReadID,
  87. stmpe811_Reset,
  88. stmpe811_IO_Start,
  89. stmpe811_IO_Config,
  90. stmpe811_IO_WritePin,
  91. stmpe811_IO_ReadPin,
  92. stmpe811_IO_EnableIT,
  93. stmpe811_IO_DisableIT,
  94. stmpe811_IO_ITStatus,
  95. stmpe811_IO_ClearIT,
  96. };
  97. /* stmpe811 instances by address */
  98. uint8_t stmpe811[STMPE811_MAX_INSTANCE] = {0};
  99. /**
  100. * @}
  101. */
  102. /** @defgroup STMPE811_Private_Function_Prototypes
  103. * @{
  104. */
  105. static uint8_t stmpe811_GetInstance(uint16_t DeviceAddr);
  106. /**
  107. * @}
  108. */
  109. /** @defgroup STMPE811_Private_Functions
  110. * @{
  111. */
  112. /**
  113. * @brief Initialize the stmpe811 and configure the needed hardware resources
  114. * @param DeviceAddr: Device address on communication Bus.
  115. * @retval None
  116. */
  117. void stmpe811_Init(uint16_t DeviceAddr)
  118. {
  119. uint8_t instance;
  120. uint8_t empty;
  121. /* Check if device instance already exists */
  122. instance = stmpe811_GetInstance(DeviceAddr);
  123. /* To prevent double initialization */
  124. if(instance == 0xFF)
  125. {
  126. /* Look for empty instance */
  127. empty = stmpe811_GetInstance(0);
  128. if(empty < STMPE811_MAX_INSTANCE)
  129. {
  130. /* Register the current device instance */
  131. stmpe811[empty] = DeviceAddr;
  132. /* Initialize IO BUS layer */
  133. IOE_Init();
  134. /* Generate stmpe811 Software reset */
  135. stmpe811_Reset(DeviceAddr);
  136. }
  137. }
  138. }
  139. /**
  140. * @brief Reset the stmpe811 by Software.
  141. * @param DeviceAddr: Device address on communication Bus.
  142. * @retval None
  143. */
  144. void stmpe811_Reset(uint16_t DeviceAddr)
  145. {
  146. /* Power Down the stmpe811 */
  147. IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL1, 2);
  148. /* Wait for a delay to ensure registers erasing */
  149. IOE_Delay(10);
  150. /* Power On the Codec after the power off => all registers are reinitialized */
  151. IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL1, 0);
  152. /* Wait for a delay to ensure registers erasing */
  153. IOE_Delay(2);
  154. }
  155. /**
  156. * @brief Read the stmpe811 IO Expander device ID.
  157. * @param DeviceAddr: Device address on communication Bus.
  158. * @retval The Device ID (two bytes).
  159. */
  160. uint16_t stmpe811_ReadID(uint16_t DeviceAddr)
  161. {
  162. /* Initialize IO BUS layer */
  163. IOE_Init();
  164. /* Return the device ID value */
  165. return ((IOE_Read(DeviceAddr, STMPE811_REG_CHP_ID_LSB) << 8) |\
  166. (IOE_Read(DeviceAddr, STMPE811_REG_CHP_ID_MSB)));
  167. }
  168. /**
  169. * @brief Enable the Global interrupt.
  170. * @param DeviceAddr: Device address on communication Bus.
  171. * @retval None
  172. */
  173. void stmpe811_EnableGlobalIT(uint16_t DeviceAddr)
  174. {
  175. uint8_t tmp = 0;
  176. /* Read the Interrupt Control register */
  177. tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
  178. /* Set the global interrupts to be Enabled */
  179. tmp |= (uint8_t)STMPE811_GIT_EN;
  180. /* Write Back the Interrupt Control register */
  181. IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
  182. }
  183. /**
  184. * @brief Disable the Global interrupt.
  185. * @param DeviceAddr: Device address on communication Bus.
  186. * @retval None
  187. */
  188. void stmpe811_DisableGlobalIT(uint16_t DeviceAddr)
  189. {
  190. uint8_t tmp = 0;
  191. /* Read the Interrupt Control register */
  192. tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
  193. /* Set the global interrupts to be Disabled */
  194. tmp &= ~(uint8_t)STMPE811_GIT_EN;
  195. /* Write Back the Interrupt Control register */
  196. IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
  197. }
  198. /**
  199. * @brief Enable the interrupt mode for the selected IT source
  200. * @param DeviceAddr: Device address on communication Bus.
  201. * @param Source: The interrupt source to be configured, could be:
  202. * @arg STMPE811_GIT_IO: IO interrupt
  203. * @arg STMPE811_GIT_ADC : ADC interrupt
  204. * @arg STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
  205. * @arg STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt
  206. * @arg STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt
  207. * @arg STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt
  208. * @arg STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt
  209. * @retval None
  210. */
  211. void stmpe811_EnableITSource(uint16_t DeviceAddr, uint8_t Source)
  212. {
  213. uint8_t tmp = 0;
  214. /* Get the current value of the INT_EN register */
  215. tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_EN);
  216. /* Set the interrupts to be Enabled */
  217. tmp |= Source;
  218. /* Set the register */
  219. IOE_Write(DeviceAddr, STMPE811_REG_INT_EN, tmp);
  220. }
  221. /**
  222. * @brief Disable the interrupt mode for the selected IT source
  223. * @param DeviceAddr: Device address on communication Bus.
  224. * @param Source: The interrupt source to be configured, could be:
  225. * @arg STMPE811_GIT_IO: IO interrupt
  226. * @arg STMPE811_GIT_ADC : ADC interrupt
  227. * @arg STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
  228. * @arg STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt
  229. * @arg STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt
  230. * @arg STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt
  231. * @arg STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt
  232. * @retval None
  233. */
  234. void stmpe811_DisableITSource(uint16_t DeviceAddr, uint8_t Source)
  235. {
  236. uint8_t tmp = 0;
  237. /* Get the current value of the INT_EN register */
  238. tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_EN);
  239. /* Set the interrupts to be Enabled */
  240. tmp &= ~Source;
  241. /* Set the register */
  242. IOE_Write(DeviceAddr, STMPE811_REG_INT_EN, tmp);
  243. }
  244. /**
  245. * @brief Set the global interrupt Polarity.
  246. * @param DeviceAddr: Device address on communication Bus.
  247. * @param Polarity: the IT mode polarity, could be one of the following values:
  248. * @arg STMPE811_POLARITY_LOW: Interrupt line is active Low/Falling edge
  249. * @arg STMPE811_POLARITY_HIGH: Interrupt line is active High/Rising edge
  250. * @retval None
  251. */
  252. void stmpe811_SetITPolarity(uint16_t DeviceAddr, uint8_t Polarity)
  253. {
  254. uint8_t tmp = 0;
  255. /* Get the current register value */
  256. tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
  257. /* Mask the polarity bits */
  258. tmp &= ~(uint8_t)0x04;
  259. /* Modify the Interrupt Output line configuration */
  260. tmp |= Polarity;
  261. /* Set the new register value */
  262. IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
  263. }
  264. /**
  265. * @brief Set the global interrupt Type.
  266. * @param DeviceAddr: Device address on communication Bus.
  267. * @param Type: Interrupt line activity type, could be one of the following values:
  268. * @arg STMPE811_TYPE_LEVEL: Interrupt line is active in level model
  269. * @arg STMPE811_TYPE_EDGE: Interrupt line is active in edge model
  270. * @retval None
  271. */
  272. void stmpe811_SetITType(uint16_t DeviceAddr, uint8_t Type)
  273. {
  274. uint8_t tmp = 0;
  275. /* Get the current register value */
  276. tmp = IOE_Read(DeviceAddr, STMPE811_REG_INT_CTRL);
  277. /* Mask the type bits */
  278. tmp &= ~(uint8_t)0x02;
  279. /* Modify the Interrupt Output line configuration */
  280. tmp |= Type;
  281. /* Set the new register value */
  282. IOE_Write(DeviceAddr, STMPE811_REG_INT_CTRL, tmp);
  283. }
  284. /**
  285. * @brief Check the selected Global interrupt source pending bit
  286. * @param DeviceAddr: Device address on communication Bus.
  287. * @param Source: the Global interrupt source to be checked, could be:
  288. * @arg STMPE811_GIT_IO: IO interrupt
  289. * @arg STMPE811_GIT_ADC : ADC interrupt
  290. * @arg STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
  291. * @arg STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt
  292. * @arg STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt
  293. * @arg STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt
  294. * @arg STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt
  295. * @retval The checked Global interrupt source status.
  296. */
  297. uint8_t stmpe811_GlobalITStatus(uint16_t DeviceAddr, uint8_t Source)
  298. {
  299. /* Return the global IT source status */
  300. return((IOE_Read(DeviceAddr, STMPE811_REG_INT_STA) & Source) == Source);
  301. }
  302. /**
  303. * @brief Return the Global interrupts status
  304. * @param DeviceAddr: Device address on communication Bus.
  305. * @param Source: the Global interrupt source to be checked, could be:
  306. * @arg STMPE811_GIT_IO: IO interrupt
  307. * @arg STMPE811_GIT_ADC : ADC interrupt
  308. * @arg STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
  309. * @arg STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt
  310. * @arg STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt
  311. * @arg STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt
  312. * @arg STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt
  313. * @retval The checked Global interrupt source status.
  314. */
  315. uint8_t stmpe811_ReadGITStatus(uint16_t DeviceAddr, uint8_t Source)
  316. {
  317. /* Return the global IT source status */
  318. return((IOE_Read(DeviceAddr, STMPE811_REG_INT_STA) & Source));
  319. }
  320. /**
  321. * @brief Clear the selected Global interrupt pending bit(s)
  322. * @param DeviceAddr: Device address on communication Bus.
  323. * @param Source: the Global interrupt source to be cleared, could be any combination
  324. * of the following values:
  325. * @arg STMPE811_GIT_IO: IO interrupt
  326. * @arg STMPE811_GIT_ADC : ADC interrupt
  327. * @arg STMPE811_GIT_FE : Touch Screen Controller FIFO Error interrupt
  328. * @arg STMPE811_GIT_FF : Touch Screen Controller FIFO Full interrupt
  329. * @arg STMPE811_GIT_FOV : Touch Screen Controller FIFO Overrun interrupt
  330. * @arg STMPE811_GIT_FTH : Touch Screen Controller FIFO Threshold interrupt
  331. * @arg STMPE811_GIT_TOUCH : Touch Screen Controller Touch Detected interrupt
  332. * @retval None
  333. */
  334. void stmpe811_ClearGlobalIT(uint16_t DeviceAddr, uint8_t Source)
  335. {
  336. /* Write 1 to the bits that have to be cleared */
  337. IOE_Write(DeviceAddr, STMPE811_REG_INT_STA, Source);
  338. }
  339. /**
  340. * @brief Start the IO functionality use and disable the AF for selected IO pin(s).
  341. * @param DeviceAddr: Device address on communication Bus.
  342. * @param IO_Pin: The IO pin(s) to put in AF. This parameter can be one
  343. * of the following values:
  344. * @arg STMPE811_PIN_x: where x can be from 0 to 7.
  345. * @retval None
  346. */
  347. void stmpe811_IO_Start(uint16_t DeviceAddr, uint32_t IO_Pin)
  348. {
  349. uint8_t mode;
  350. /* Get the current register value */
  351. mode = IOE_Read(DeviceAddr, STMPE811_REG_SYS_CTRL2);
  352. /* Set the Functionalities to be Disabled */
  353. mode &= ~(STMPE811_IO_FCT | STMPE811_ADC_FCT);
  354. /* Write the new register value */
  355. IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode);
  356. /* Disable AF for the selected IO pin(s) */
  357. stmpe811_IO_DisableAF(DeviceAddr, (uint8_t)IO_Pin);
  358. }
  359. /**
  360. * @brief Configures the IO pin(s) according to IO mode structure value.
  361. * @param DeviceAddr: Device address on communication Bus.
  362. * @param IO_Pin: The output pin to be set or reset. This parameter can be one
  363. * of the following values:
  364. * @arg STMPE811_PIN_x: where x can be from 0 to 7.
  365. * @param IO_Mode: The IO pin mode to configure, could be one of the following values:
  366. * @arg IO_MODE_INPUT
  367. * @arg IO_MODE_OUTPUT
  368. * @arg IO_MODE_IT_RISING_EDGE
  369. * @arg IO_MODE_IT_FALLING_EDGE
  370. * @arg IO_MODE_IT_LOW_LEVEL
  371. * @arg IO_MODE_IT_HIGH_LEVEL
  372. * @retval 0 if no error, IO_Mode if error
  373. */
  374. uint8_t stmpe811_IO_Config(uint16_t DeviceAddr, uint32_t IO_Pin, IO_ModeTypedef IO_Mode)
  375. {
  376. uint8_t error_code = 0;
  377. /* Configure IO pin according to selected IO mode */
  378. switch(IO_Mode)
  379. {
  380. case IO_MODE_INPUT: /* Input mode */
  381. stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
  382. break;
  383. case IO_MODE_OUTPUT: /* Output mode */
  384. stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_OUT);
  385. break;
  386. case IO_MODE_IT_RISING_EDGE: /* Interrupt rising edge mode */
  387. stmpe811_IO_EnableIT(DeviceAddr);
  388. stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
  389. stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
  390. stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_EDGE);
  391. stmpe811_IO_SetEdgeMode(DeviceAddr, IO_Pin, STMPE811_EDGE_RISING);
  392. break;
  393. case IO_MODE_IT_FALLING_EDGE: /* Interrupt falling edge mode */
  394. stmpe811_IO_EnableIT(DeviceAddr);
  395. stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
  396. stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
  397. stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_EDGE);
  398. stmpe811_IO_SetEdgeMode(DeviceAddr, IO_Pin, STMPE811_EDGE_FALLING);
  399. break;
  400. case IO_MODE_IT_LOW_LEVEL: /* Low level interrupt mode */
  401. stmpe811_IO_EnableIT(DeviceAddr);
  402. stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
  403. stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
  404. stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_LEVEL);
  405. stmpe811_SetITPolarity(DeviceAddr, STMPE811_POLARITY_LOW);
  406. break;
  407. case IO_MODE_IT_HIGH_LEVEL: /* High level interrupt mode */
  408. stmpe811_IO_EnableIT(DeviceAddr);
  409. stmpe811_IO_EnablePinIT(DeviceAddr, IO_Pin);
  410. stmpe811_IO_InitPin(DeviceAddr, IO_Pin, STMPE811_DIRECTION_IN);
  411. stmpe811_SetITType(DeviceAddr, STMPE811_TYPE_LEVEL);
  412. stmpe811_SetITPolarity(DeviceAddr, STMPE811_POLARITY_HIGH);
  413. break;
  414. default:
  415. error_code = (uint8_t) IO_Mode;
  416. break;
  417. }
  418. return error_code;
  419. }
  420. /**
  421. * @brief Initialize the selected IO pin direction.
  422. * @param DeviceAddr: Device address on communication Bus.
  423. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  424. * combination of the following values:
  425. * @arg STMPE811_PIN_x: Where x can be from 0 to 7.
  426. * @param Direction: could be STMPE811_DIRECTION_IN or STMPE811_DIRECTION_OUT.
  427. * @retval None
  428. */
  429. void stmpe811_IO_InitPin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Direction)
  430. {
  431. uint8_t tmp = 0;
  432. /* Get all the Pins direction */
  433. tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_DIR);
  434. /* Set the selected pin direction */
  435. if (Direction != STMPE811_DIRECTION_IN)
  436. {
  437. tmp |= (uint8_t)IO_Pin;
  438. }
  439. else
  440. {
  441. tmp &= ~(uint8_t)IO_Pin;
  442. }
  443. /* Write the register new value */
  444. IOE_Write(DeviceAddr, STMPE811_REG_IO_DIR, tmp);
  445. }
  446. /**
  447. * @brief Disable the AF for the selected IO pin(s).
  448. * @param DeviceAddr: Device address on communication Bus.
  449. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  450. * combination of the following values:
  451. * @arg STMPE811_PIN_x: Where x can be from 0 to 7.
  452. * @retval None
  453. */
  454. void stmpe811_IO_DisableAF(uint16_t DeviceAddr, uint32_t IO_Pin)
  455. {
  456. uint8_t tmp = 0;
  457. /* Get the current state of the IO_AF register */
  458. tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_AF);
  459. /* Enable the selected pins alternate function */
  460. tmp |= (uint8_t)IO_Pin;
  461. /* Write back the new value in IO AF register */
  462. IOE_Write(DeviceAddr, STMPE811_REG_IO_AF, tmp);
  463. }
  464. /**
  465. * @brief Enable the AF for the selected IO pin(s).
  466. * @param DeviceAddr: Device address on communication Bus.
  467. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  468. * combination of the following values:
  469. * @arg STMPE811_PIN_x: Where x can be from 0 to 7.
  470. * @retval None
  471. */
  472. void stmpe811_IO_EnableAF(uint16_t DeviceAddr, uint32_t IO_Pin)
  473. {
  474. uint8_t tmp = 0;
  475. /* Get the current register value */
  476. tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_AF);
  477. /* Enable the selected pins alternate function */
  478. tmp &= ~(uint8_t)IO_Pin;
  479. /* Write back the new register value */
  480. IOE_Write(DeviceAddr, STMPE811_REG_IO_AF, tmp);
  481. }
  482. /**
  483. * @brief Configure the Edge for which a transition is detectable for the
  484. * selected pin.
  485. * @param DeviceAddr: Device address on communication Bus.
  486. * @param IO_Pin: The IO pin to be configured. This parameter could be any
  487. * combination of the following values:
  488. * @arg STMPE811_PIN_x: Where x can be from 0 to 7.
  489. * @param Edge: The edge which will be detected. This parameter can be one or
  490. * a combination of following values: STMPE811_EDGE_FALLING and STMPE811_EDGE_RISING .
  491. * @retval None
  492. */
  493. void stmpe811_IO_SetEdgeMode(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t Edge)
  494. {
  495. uint8_t tmp1 = 0, tmp2 = 0;
  496. /* Get the current registers values */
  497. tmp1 = IOE_Read(DeviceAddr, STMPE811_REG_IO_FE);
  498. tmp2 = IOE_Read(DeviceAddr, STMPE811_REG_IO_RE);
  499. /* Disable the Falling Edge */
  500. tmp1 &= ~(uint8_t)IO_Pin;
  501. /* Disable the Falling Edge */
  502. tmp2 &= ~(uint8_t)IO_Pin;
  503. /* Enable the Falling edge if selected */
  504. if (Edge & STMPE811_EDGE_FALLING)
  505. {
  506. tmp1 |= (uint8_t)IO_Pin;
  507. }
  508. /* Enable the Rising edge if selected */
  509. if (Edge & STMPE811_EDGE_RISING)
  510. {
  511. tmp2 |= (uint8_t)IO_Pin;
  512. }
  513. /* Write back the new registers values */
  514. IOE_Write(DeviceAddr, STMPE811_REG_IO_FE, tmp1);
  515. IOE_Write(DeviceAddr, STMPE811_REG_IO_RE, tmp2);
  516. }
  517. /**
  518. * @brief Write a new IO pin state.
  519. * @param DeviceAddr: Device address on communication Bus.
  520. * @param IO_Pin: The output pin to be set or reset. This parameter can be one
  521. * of the following values:
  522. * @arg STMPE811_PIN_x: where x can be from 0 to 7.
  523. * @param PinState: The new IO pin state.
  524. * @retval None
  525. */
  526. void stmpe811_IO_WritePin(uint16_t DeviceAddr, uint32_t IO_Pin, uint8_t PinState)
  527. {
  528. /* Apply the bit value to the selected pin */
  529. if (PinState != 0)
  530. {
  531. /* Set the register */
  532. IOE_Write(DeviceAddr, STMPE811_REG_IO_SET_PIN, (uint8_t)IO_Pin);
  533. }
  534. else
  535. {
  536. /* Set the register */
  537. IOE_Write(DeviceAddr, STMPE811_REG_IO_CLR_PIN, (uint8_t)IO_Pin);
  538. }
  539. }
  540. /**
  541. * @brief Return the state of the selected IO pin(s).
  542. * @param DeviceAddr: Device address on communication Bus.
  543. * @param IO_Pin: The output pin to be set or reset. This parameter can be one
  544. * of the following values:
  545. * @arg STMPE811_PIN_x: where x can be from 0 to 7.
  546. * @retval IO pin(s) state.
  547. */
  548. uint32_t stmpe811_IO_ReadPin(uint16_t DeviceAddr, uint32_t IO_Pin)
  549. {
  550. return((uint32_t)(IOE_Read(DeviceAddr, STMPE811_REG_IO_MP_STA) & (uint8_t)IO_Pin));
  551. }
  552. /**
  553. * @brief Enable the global IO interrupt source.
  554. * @param DeviceAddr: Device address on communication Bus.
  555. * @retval None
  556. */
  557. void stmpe811_IO_EnableIT(uint16_t DeviceAddr)
  558. {
  559. IOE_ITConfig();
  560. /* Enable global IO IT source */
  561. stmpe811_EnableITSource(DeviceAddr, STMPE811_GIT_IO);
  562. /* Enable global interrupt */
  563. stmpe811_EnableGlobalIT(DeviceAddr);
  564. }
  565. /**
  566. * @brief Disable the global IO interrupt source.
  567. * @param DeviceAddr: Device address on communication Bus.
  568. * @retval None
  569. */
  570. void stmpe811_IO_DisableIT(uint16_t DeviceAddr)
  571. {
  572. /* Disable the global interrupt */
  573. stmpe811_DisableGlobalIT(DeviceAddr);
  574. /* Disable global IO IT source */
  575. stmpe811_DisableITSource(DeviceAddr, STMPE811_GIT_IO);
  576. }
  577. /**
  578. * @brief Enable interrupt mode for the selected IO pin(s).
  579. * @param DeviceAddr: Device address on communication Bus.
  580. * @param IO_Pin: The IO interrupt to be enabled. This parameter could be any
  581. * combination of the following values:
  582. * @arg STMPE811_PIN_x: where x can be from 0 to 7.
  583. * @retval None
  584. */
  585. void stmpe811_IO_EnablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
  586. {
  587. uint8_t tmp = 0;
  588. /* Get the IO interrupt state */
  589. tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_EN);
  590. /* Set the interrupts to be enabled */
  591. tmp |= (uint8_t)IO_Pin;
  592. /* Write the register new value */
  593. IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_EN, tmp);
  594. }
  595. /**
  596. * @brief Disable interrupt mode for the selected IO pin(s).
  597. * @param DeviceAddr: Device address on communication Bus.
  598. * @param IO_Pin: The IO interrupt to be disabled. This parameter could be any
  599. * combination of the following values:
  600. * @arg STMPE811_PIN_x: where x can be from 0 to 7.
  601. * @retval None
  602. */
  603. void stmpe811_IO_DisablePinIT(uint16_t DeviceAddr, uint32_t IO_Pin)
  604. {
  605. uint8_t tmp = 0;
  606. /* Get the IO interrupt state */
  607. tmp = IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_EN);
  608. /* Set the interrupts to be Disabled */
  609. tmp &= ~(uint8_t)IO_Pin;
  610. /* Write the register new value */
  611. IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_EN, tmp);
  612. }
  613. /**
  614. * @brief Check the status of the selected IO interrupt pending bit
  615. * @param DeviceAddr: Device address on communication Bus.
  616. * @param IO_Pin: The IO interrupt to be checked could be:
  617. * @arg STMPE811_PIN_x Where x can be from 0 to 7.
  618. * @retval Status of the checked IO pin(s).
  619. */
  620. uint32_t stmpe811_IO_ITStatus(uint16_t DeviceAddr, uint32_t IO_Pin)
  621. {
  622. /* Get the Interrupt status */
  623. return(IOE_Read(DeviceAddr, STMPE811_REG_IO_INT_STA) & (uint8_t)IO_Pin);
  624. }
  625. /**
  626. * @brief Clear the selected IO interrupt pending bit(s).
  627. * @param DeviceAddr: Device address on communication Bus.
  628. * @param IO_Pin: the IO interrupt to be cleared, could be:
  629. * @arg STMPE811_PIN_x: Where x can be from 0 to 7.
  630. * @retval None
  631. */
  632. void stmpe811_IO_ClearIT(uint16_t DeviceAddr, uint32_t IO_Pin)
  633. {
  634. /* Clear the global IO IT pending bit */
  635. stmpe811_ClearGlobalIT(DeviceAddr, STMPE811_GIT_IO);
  636. /* Clear the IO IT pending bit(s) */
  637. IOE_Write(DeviceAddr, STMPE811_REG_IO_INT_STA, (uint8_t)IO_Pin);
  638. /* Clear the Edge detection pending bit*/
  639. IOE_Write(DeviceAddr, STMPE811_REG_IO_ED, (uint8_t)IO_Pin);
  640. /* Clear the Rising edge pending bit */
  641. IOE_Write(DeviceAddr, STMPE811_REG_IO_RE, (uint8_t)IO_Pin);
  642. /* Clear the Falling edge pending bit */
  643. IOE_Write(DeviceAddr, STMPE811_REG_IO_FE, (uint8_t)IO_Pin);
  644. }
  645. /**
  646. * @brief Configures the touch Screen Controller (Single point detection)
  647. * @param DeviceAddr: Device address on communication Bus.
  648. * @retval None.
  649. */
  650. void stmpe811_TS_Start(uint16_t DeviceAddr)
  651. {
  652. uint8_t mode;
  653. /* Get the current register value */
  654. mode = IOE_Read(DeviceAddr, STMPE811_REG_SYS_CTRL2);
  655. /* Set the Functionalities to be Enabled */
  656. mode &= ~(STMPE811_IO_FCT);
  657. /* Write the new register value */
  658. IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode);
  659. /* Select TSC pins in TSC alternate mode */
  660. stmpe811_IO_EnableAF(DeviceAddr, STMPE811_TOUCH_IO_ALL);
  661. /* Set the Functionalities to be Enabled */
  662. mode &= ~(STMPE811_TS_FCT | STMPE811_ADC_FCT);
  663. /* Set the new register value */
  664. IOE_Write(DeviceAddr, STMPE811_REG_SYS_CTRL2, mode);
  665. /* Select Sample Time, bit number and ADC Reference */
  666. IOE_Write(DeviceAddr, STMPE811_REG_ADC_CTRL1, 0x49);
  667. /* Wait for 2 ms */
  668. IOE_Delay(2);
  669. /* Select the ADC clock speed: 3.25 MHz */
  670. IOE_Write(DeviceAddr, STMPE811_REG_ADC_CTRL2, 0x01);
  671. /* Select 2 nF filter capacitor */
  672. /* Configuration:
  673. - Touch average control : 4 samples
  674. - Touch delay time : 500 uS
  675. - Panel driver setting time: 500 uS
  676. */
  677. IOE_Write(DeviceAddr, STMPE811_REG_TSC_CFG, 0x9A);
  678. /* Configure the Touch FIFO threshold: single point reading */
  679. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_TH, 0x01);
  680. /* Clear the FIFO memory content. */
  681. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
  682. /* Put the FIFO back into operation mode */
  683. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
  684. /* Set the range and accuracy pf the pressure measurement (Z) :
  685. - Fractional part :7
  686. - Whole part :1
  687. */
  688. IOE_Write(DeviceAddr, STMPE811_REG_TSC_FRACT_XYZ, 0x01);
  689. /* Set the driving capability (limit) of the device for TSC pins: 50mA */
  690. IOE_Write(DeviceAddr, STMPE811_REG_TSC_I_DRIVE, 0x01);
  691. /* Touch screen control configuration (enable TSC):
  692. - No window tracking index
  693. - XYZ acquisition mode
  694. */
  695. IOE_Write(DeviceAddr, STMPE811_REG_TSC_CTRL, 0x01);
  696. /* Clear all the status pending bits if any */
  697. IOE_Write(DeviceAddr, STMPE811_REG_INT_STA, 0xFF);
  698. /* Wait for 2 ms delay */
  699. IOE_Delay(2);
  700. }
  701. /**
  702. * @brief Return if there is touch detected or not.
  703. * @param DeviceAddr: Device address on communication Bus.
  704. * @retval Touch detected state.
  705. */
  706. uint8_t stmpe811_TS_DetectTouch(uint16_t DeviceAddr)
  707. {
  708. uint8_t state;
  709. uint8_t ret = 0;
  710. state = ((IOE_Read(DeviceAddr, STMPE811_REG_TSC_CTRL) & (uint8_t)STMPE811_TS_CTRL_STATUS) == (uint8_t)0x80);
  711. if(state > 0)
  712. {
  713. if(IOE_Read(DeviceAddr, STMPE811_REG_FIFO_SIZE) > 0)
  714. {
  715. ret = 1;
  716. }
  717. }
  718. else
  719. {
  720. /* Reset FIFO */
  721. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
  722. /* Enable the FIFO again */
  723. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
  724. }
  725. return ret;
  726. }
  727. /**
  728. * @brief Get the touch screen X and Y positions values
  729. * @param DeviceAddr: Device address on communication Bus.
  730. * @param X: Pointer to X position value
  731. * @param Y: Pointer to Y position value
  732. * @retval None.
  733. */
  734. void stmpe811_TS_GetXY(uint16_t DeviceAddr, uint16_t *X, uint16_t *Y)
  735. {
  736. uint8_t dataXYZ[4];
  737. uint32_t uldataXYZ;
  738. IOE_ReadMultiple(DeviceAddr, STMPE811_REG_TSC_DATA_NON_INC, dataXYZ, sizeof(dataXYZ)) ;
  739. /* Calculate positions values */
  740. uldataXYZ = (dataXYZ[0] << 24)|(dataXYZ[1] << 16)|(dataXYZ[2] << 8)|(dataXYZ[3] << 0);
  741. *X = (uldataXYZ >> 20) & 0x00000FFF;
  742. *Y = (uldataXYZ >> 8) & 0x00000FFF;
  743. /* Reset FIFO */
  744. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x01);
  745. /* Enable the FIFO again */
  746. IOE_Write(DeviceAddr, STMPE811_REG_FIFO_STA, 0x00);
  747. }
  748. /**
  749. * @brief Configure the selected source to generate a global interrupt or not
  750. * @param DeviceAddr: Device address on communication Bus.
  751. * @retval None
  752. */
  753. void stmpe811_TS_EnableIT(uint16_t DeviceAddr)
  754. {
  755. IOE_ITConfig();
  756. /* Enable global TS IT source */
  757. stmpe811_EnableITSource(DeviceAddr, STMPE811_TS_IT);
  758. /* Enable global interrupt */
  759. stmpe811_EnableGlobalIT(DeviceAddr);
  760. }
  761. /**
  762. * @brief Configure the selected source to generate a global interrupt or not
  763. * @param DeviceAddr: Device address on communication Bus.
  764. * @retval None
  765. */
  766. void stmpe811_TS_DisableIT(uint16_t DeviceAddr)
  767. {
  768. /* Disable global interrupt */
  769. stmpe811_DisableGlobalIT(DeviceAddr);
  770. /* Disable global TS IT source */
  771. stmpe811_DisableITSource(DeviceAddr, STMPE811_TS_IT);
  772. }
  773. /**
  774. * @brief Configure the selected source to generate a global interrupt or not
  775. * @param DeviceAddr: Device address on communication Bus.
  776. * @retval TS interrupts status
  777. */
  778. uint8_t stmpe811_TS_ITStatus(uint16_t DeviceAddr)
  779. {
  780. /* Return TS interrupts status */
  781. return(stmpe811_ReadGITStatus(DeviceAddr, STMPE811_TS_IT));
  782. }
  783. /**
  784. * @brief Configure the selected source to generate a global interrupt or not
  785. * @param DeviceAddr: Device address on communication Bus.
  786. * @retval None
  787. */
  788. void stmpe811_TS_ClearIT(uint16_t DeviceAddr)
  789. {
  790. /* Clear the global TS IT source */
  791. stmpe811_ClearGlobalIT(DeviceAddr, STMPE811_TS_IT);
  792. }
  793. /**
  794. * @brief Check if the device instance of the selected address is already registered
  795. * and return its index
  796. * @param DeviceAddr: Device address on communication Bus.
  797. * @retval Index of the device instance if registered, 0xFF if not.
  798. */
  799. static uint8_t stmpe811_GetInstance(uint16_t DeviceAddr)
  800. {
  801. uint8_t idx = 0;
  802. /* Check all the registered instances */
  803. for(idx = 0; idx < STMPE811_MAX_INSTANCE ; idx ++)
  804. {
  805. if(stmpe811[idx] == DeviceAddr)
  806. {
  807. return idx;
  808. }
  809. }
  810. return 0xFF;
  811. }
  812. /**
  813. * @}
  814. */
  815. /**
  816. * @}
  817. */
  818. /**
  819. * @}
  820. */
  821. /**
  822. * @}
  823. */
  824. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/