Mercurial > ~darius > hgwebdir.cgi > stm32temp
comparison libs/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/STM32100E_EVAL/stm32100e_eval_ioe.c @ 0:c59513fd84fb
Initial commit of STM32 test code.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Mon, 03 Oct 2011 21:19:15 +1030 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c59513fd84fb |
---|---|
1 /** | |
2 ****************************************************************************** | |
3 * @file stm32100e_eval_ioe.c | |
4 * @author MCD Application Team | |
5 * @version V4.5.0 | |
6 * @date 07-March-2011 | |
7 * @brief This file includes the IO Expander driver for STMPE811 IO Expander | |
8 * devices. | |
9 ****************************************************************************** | |
10 * @attention | |
11 * | |
12 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS | |
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE | |
14 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY | |
15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING | |
16 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE | |
17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. | |
18 * | |
19 * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> | |
20 ****************************************************************************** | |
21 */ | |
22 | |
23 /* File Info : --------------------------------------------------------------- | |
24 | |
25 Note: | |
26 ----- | |
27 - This driver uses the DMA method for sending and receiving data on I2C bus | |
28 which allow higher efficiency and reliability of the communication. | |
29 | |
30 SUPPORTED FEATURES: | |
31 - Touch Screen Features: Single point mode (Polling/Interrupt). | |
32 - TempSensor Feature: accuracy not determined (Polling). | |
33 - IO Read/write : Set/Reset and Read (Polling/Interrupt). | |
34 | |
35 UNSUPPORTED FEATURES: | |
36 - Row ADC Feature is not supported (not implemented on STM32100E-EVAL board) | |
37 - Joystick: config and Read (Polling/Interrupt) | |
38 ----------------------------------------------------------------------------*/ | |
39 | |
40 /* Includes ------------------------------------------------------------------*/ | |
41 #include "stm32100e_eval_ioe.h" | |
42 | |
43 /** @addtogroup Utilities | |
44 * @{ | |
45 */ | |
46 | |
47 /** @addtogroup STM32_EVAL | |
48 * @{ | |
49 */ | |
50 | |
51 /** @addtogroup STM32100E_EVAL | |
52 * @{ | |
53 */ | |
54 | |
55 /** @defgroup STM32100E_EVAL_IOE | |
56 * @brief This file includes the IO Expander driver for STMPE811 IO Expander | |
57 * devices. | |
58 * @{ | |
59 */ | |
60 | |
61 /** @defgroup STM32100E_EVAL_IOE_Private_TypesDefinitions | |
62 * @{ | |
63 */ | |
64 /** | |
65 * @} | |
66 */ | |
67 | |
68 | |
69 /** @defgroup STM32100E_EVAL_IOE_Private_Defines | |
70 * @{ | |
71 */ | |
72 #define TIMEOUT_MAX 0x1000 /*<! The value of the maximal timeout for I2C waiting loops */ | |
73 /** | |
74 * @} | |
75 */ | |
76 | |
77 | |
78 /** @defgroup STM32100E_EVAL_IOE_Private_Macros | |
79 * @{ | |
80 */ | |
81 /** | |
82 * @} | |
83 */ | |
84 | |
85 | |
86 /** @defgroup STM32100E_EVAL_IOE_Private_Variables | |
87 * @{ | |
88 */ | |
89 TS_STATE TS_State; /*<! The global structure holding the TS state */ | |
90 | |
91 uint32_t IOE_TimeOut = TIMEOUT_MAX; /*<! Value of Timeout when I2C communication fails */ | |
92 /** | |
93 * @} | |
94 */ | |
95 | |
96 | |
97 /** @defgroup STM32100E_EVAL_IOE_Private_FunctionPrototypes | |
98 * @{ | |
99 */ | |
100 static uint16_t IOE_TS_Read_X(void); | |
101 static uint16_t IOE_TS_Read_Y(void); | |
102 static uint16_t IOE_TS_Read_Z(void); | |
103 | |
104 static void IOE_GPIO_Config(void); | |
105 static void IOE_I2C_Config(void); | |
106 static void IOE_DMA_Config(IOE_DMADirection_TypeDef Direction, uint8_t* buffer); | |
107 static void IOE_EXTI_Config(void); | |
108 | |
109 #ifndef USE_Delay | |
110 static void delay(__IO uint32_t nCount); | |
111 #endif /* USE_Delay*/ | |
112 /** | |
113 * @} | |
114 */ | |
115 | |
116 | |
117 /** @defgroup STM32100E_EVAL_IOE_Private_Functions | |
118 * @{ | |
119 */ | |
120 | |
121 | |
122 /** | |
123 * @brief Initializes and Configures the IO_Expanders Functionalities | |
124 * (Touch Screen ..) and configures all STM32100E-EVAL necessary | |
125 * hardware (GPIOs, APB clocks ..). | |
126 * @param None | |
127 * @retval IOE_OK if all initializations done correctly. Other value if error. | |
128 */ | |
129 uint8_t IOE_Config(void) | |
130 { | |
131 /* Configure the needed pins */ | |
132 IOE_GPIO_Config(); | |
133 | |
134 /* Configure I2C peripheral */ | |
135 IOE_I2C_Config(); | |
136 | |
137 /* Read IO Expander 1 ID */ | |
138 if(IOE_IsOperational(IOE_1_ADDR)) | |
139 { | |
140 return IOE1_NOT_OPERATIONAL; | |
141 } | |
142 | |
143 /* Generate IOExpander Software reset */ | |
144 IOE_Reset(IOE_1_ADDR); | |
145 | |
146 /* ---------------------- IO Expander configuration --------------------- */ | |
147 /* Enable the GPIO, Touch Screen and ADC functionalities */ | |
148 IOE_FnctCmd(IOE_1_ADDR, IOE_TS_FCT | IOE_ADC_FCT | IOE_IO_FCT | IOE_TEMPSENS_FCT , ENABLE); | |
149 | |
150 /* Touch Screen controller configuration */ | |
151 IOE_TS_Config(); | |
152 | |
153 /* Temperature Sensor configuration */ | |
154 IOE_TempSens_Config(); | |
155 | |
156 /* ------------------------------------------------------------------------ */ | |
157 | |
158 /* Configuration is OK */ | |
159 return IOE_OK; | |
160 } | |
161 | |
162 /** | |
163 * @brief Configures The selected interrupts on the IO Expanders. | |
164 * @param IOE_ITSRC_Source: the source of the interrupts. Could be one or a | |
165 * combination of the following parameters: | |
166 * @arg IOE_ITSRC_TSC: Touch Screen interrupts. | |
167 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
168 */ | |
169 uint8_t IOE_ITConfig(uint32_t IOE_ITSRC_Source) | |
170 { | |
171 /* Configure the Interrupt output pin to generate low level (INT_CTRL) */ | |
172 IOE_ITOutConfig(Polarity_Low, Type_Level); | |
173 | |
174 /* Manage the Touch Screen Interrupts */ | |
175 if (IOE_ITSRC_Source & IOE_ITSRC_TSC) | |
176 { | |
177 /* Enable the Global interrupt */ | |
178 IOE_GITCmd(IOE_1_ADDR, ENABLE); | |
179 | |
180 /* Enable the Global GPIO Interrupt */ | |
181 IOE_GITConfig(IOE_1_ADDR, (uint8_t)(IOE_GIT_TOUCH | IOE_GIT_FTH | IOE_GIT_FOV), ENABLE); | |
182 | |
183 /* Read the GPIO_IT_STA to clear all pending bits if any */ | |
184 I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_INT_STA); | |
185 } | |
186 | |
187 /* Configure the Interrupt line as EXTI source */ | |
188 IOE_EXTI_Config(); | |
189 | |
190 /* If all OK return IOE_OK */ | |
191 return IOE_OK; | |
192 } | |
193 | |
194 /** | |
195 * @brief Writes a bit value to an output IO pin. | |
196 * @param IO_Pin: The output pin to be set or reset. | |
197 * IO_Pin_x: Where x can be from 0 to 7. | |
198 * @param BitVal: The value to be set. This parameter can be one of the | |
199 * following values: BitSet or BitReset. See IOE_BitVal_TypeDef. | |
200 * @retval IOE_OK or PARAM_ERROR | |
201 */ | |
202 uint8_t IOE_WriteIOPin(uint8_t IO_Pin, IOE_BitValue_TypeDef BitVal) | |
203 { | |
204 uint8_t DeviceAddr = 0; | |
205 | |
206 /* Get the IO expander Address according to which pin is to be controlled */ | |
207 if (IO_Pin & IO1_OUT_ALL_PINS) | |
208 { | |
209 DeviceAddr = IOE_1_ADDR; | |
210 } | |
211 else | |
212 { | |
213 return PARAM_ERROR; | |
214 } | |
215 | |
216 /* Apply the bit value to the selected pin */ | |
217 if (BitVal == BitReset) | |
218 { | |
219 /* Set the register */ | |
220 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_CLR_PIN, IO_Pin); | |
221 } | |
222 else | |
223 { | |
224 /* Set the register */ | |
225 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_SET_PIN, IO_Pin); | |
226 } | |
227 | |
228 return IOE_OK; | |
229 } | |
230 | |
231 | |
232 /** | |
233 * @brief Returns the status of the selected input IO pin. | |
234 * @param IO_Pin: The input pin to be read. | |
235 * IO_Pin_x: Where x can be from 0 to 7. | |
236 * @arg JOY_IO_PINS: Joystick IO pins (use IOE_JoyStickGetState for these pins) | |
237 * @retval None | |
238 */ | |
239 uint8_t IOE_ReadIOPin(uint32_t IO_Pin) | |
240 { | |
241 uint8_t DeviceAddr = 0; | |
242 uint8_t tmp = 0; | |
243 if (IO_Pin & IO1_IN_ALL_PINS) | |
244 { | |
245 DeviceAddr = IOE_1_ADDR; | |
246 } | |
247 else | |
248 { | |
249 return PARAM_ERROR; | |
250 } | |
251 | |
252 /* Get all the Pins status */ | |
253 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_MP_STA); | |
254 if ((tmp & (uint8_t)IO_Pin) != 0) | |
255 { | |
256 return BitSet; | |
257 } | |
258 else | |
259 { | |
260 return BitReset; | |
261 } | |
262 } | |
263 | |
264 | |
265 /** | |
266 * @brief Returns Status and positions of the Touch screen. | |
267 * @param None | |
268 * @retval Pointer to TS_STATE structure holding Touch Screen information. | |
269 */ | |
270 TS_STATE* IOE_TS_GetState(void) | |
271 { | |
272 uint32_t xDiff, yDiff , x , y; | |
273 static uint32_t _x = 0, _y = 0; | |
274 | |
275 /* Check if the Touch detect event happenned */ | |
276 TS_State.TouchDetected = (I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CTRL) & 0x80); | |
277 if(TS_State.TouchDetected) | |
278 { | |
279 x = IOE_TS_Read_X(); | |
280 y = IOE_TS_Read_Y(); | |
281 xDiff = x > _x? (x - _x): (_x - x); | |
282 yDiff = y > _y? (y - _y): (_y - y); | |
283 if (xDiff + yDiff > 5) | |
284 { | |
285 _x = x; | |
286 _y = y; | |
287 } | |
288 } | |
289 /* Update the X position */ | |
290 TS_State.X = _x; | |
291 | |
292 /* Update the Y position */ | |
293 TS_State.Y = _y; | |
294 /* Update the Z Pression index */ | |
295 TS_State.Z = IOE_TS_Read_Z(); | |
296 | |
297 /* Clear the interrupt pending bit and enable the FIFO again */ | |
298 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x01); | |
299 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x00); | |
300 | |
301 /* Return pointer to the updated structure */ | |
302 return &TS_State; | |
303 } | |
304 | |
305 /** | |
306 * @brief Returns the temperature row value (in 16 bit format). | |
307 * @param None | |
308 * @retval The temperature row value. | |
309 */ | |
310 uint32_t IOE_TempSens_GetData(void) | |
311 { | |
312 static __IO uint32_t tmp = 0; | |
313 | |
314 /* Aquire data enable */ | |
315 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_CTRL, 0x03); | |
316 | |
317 /* Enable the TEMPSENS module */ | |
318 tmp = (uint32_t)((I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_DATA) & 0x03) << 8); | |
319 tmp |= (uint32_t)I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_DATA + 1); | |
320 | |
321 tmp = (uint32_t)((33 * tmp * 100) / 751); | |
322 tmp = (uint32_t)((tmp + 5) / 10); | |
323 | |
324 /* return the temprature row value */ | |
325 return tmp; | |
326 } | |
327 | |
328 /** | |
329 * @brief Checks the selected Global interrupt source pending bit | |
330 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
331 * @param Global_IT: the Global interrupt source to be checked, could be: | |
332 * @arg Global_IT_GPIO : All IOs interrupt | |
333 * @arg Global_IT_ADC : ADC interrupt | |
334 * @arg Global_IT_TEMP : Temperature Sensor interrupts | |
335 * @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt | |
336 * @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt | |
337 * @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt | |
338 * @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt | |
339 * @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt | |
340 * @retval Status of the checked flag. Could be SET or RESET. | |
341 */ | |
342 FlagStatus IOE_GetGITStatus(uint8_t DeviceAddr, uint8_t Global_IT) | |
343 { | |
344 __IO uint8_t tmp = 0; | |
345 | |
346 /* get the Interrupt status */ | |
347 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_STA); | |
348 | |
349 if ((tmp & (uint8_t)Global_IT) != 0) | |
350 { | |
351 return SET; | |
352 } | |
353 else | |
354 { | |
355 return RESET; | |
356 } | |
357 } | |
358 | |
359 /** | |
360 * @brief Clears the selected Global interrupt pending bit(s) | |
361 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
362 * @param Global_IT: the Global interrupt to be cleared, could be any combination | |
363 * of the following values: | |
364 * @arg Global_IT_GPIO : All IOs interrupt | |
365 * @arg Global_IT_ADC : ADC interrupt | |
366 * @arg Global_IT_TEMP : Temperature Sensor interrupts | |
367 * @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt | |
368 * @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt | |
369 * @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt | |
370 * @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt | |
371 * @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt | |
372 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
373 */ | |
374 uint8_t IOE_ClearGITPending(uint8_t DeviceAddr, uint8_t Global_IT) | |
375 { | |
376 /* Write 1 to the bits that have to be cleared */ | |
377 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_STA, Global_IT); | |
378 | |
379 /* If all OK return IOE_OK */ | |
380 return IOE_OK; | |
381 } | |
382 | |
383 /** | |
384 * @brief Checks the status of the selected IO interrupt pending bit | |
385 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
386 * @param IO_IT: the IO interrupt to be checked could be IO_ITx Where x can be | |
387 * from 0 to 7. | |
388 * @retval Status of the checked flag. Could be SET or RESET. | |
389 */ | |
390 FlagStatus IOE_GetIOITStatus(uint8_t DeviceAddr, uint8_t IO_IT) | |
391 { | |
392 uint8_t tmp = 0; | |
393 | |
394 /* get the Interrupt status */ | |
395 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_STA); | |
396 | |
397 if ((tmp & (uint8_t)IO_IT) != 0) | |
398 { | |
399 return SET; | |
400 } | |
401 else | |
402 { | |
403 return RESET; | |
404 } | |
405 } | |
406 | |
407 /** | |
408 * @brief Clears the selected IO interrupt pending bit(s). | |
409 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
410 * @param IO_IT: the IO interrupt to be checked could be IO_ITx Where x can be | |
411 * from 0 to 7. | |
412 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
413 */ | |
414 uint8_t IOE_ClearIOITPending(uint8_t DeviceAddr, uint8_t IO_IT) | |
415 { | |
416 /* Write 1 to the bits that have to be cleared */ | |
417 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_STA, IO_IT); | |
418 | |
419 /* Clear the Edge detection pending bit*/ | |
420 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_ED, IO_IT); | |
421 | |
422 /* Clear the Rising edge pending bit */ | |
423 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_RE, IO_IT); | |
424 | |
425 /* Clear the Falling edge pending bit */ | |
426 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_FE, IO_IT); | |
427 | |
428 return IOE_OK; | |
429 | |
430 } | |
431 /** | |
432 * @brief Checks if the selected device is correctly configured and | |
433 * communicates correctly ont the I2C bus. | |
434 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
435 * @retval IOE_OK if IOE is operational. Other value if failure. | |
436 */ | |
437 uint8_t IOE_IsOperational(uint8_t DeviceAddr) | |
438 { | |
439 /* Return Error if the ID is not correct */ | |
440 if( IOE_ReadID(DeviceAddr) != (uint16_t)STMPE811_ID ) | |
441 { | |
442 /* Check if a Timeout occured */ | |
443 if (IOE_TimeOut == 0) | |
444 { | |
445 return(IOE_TimeoutUserCallback()); | |
446 } | |
447 else | |
448 { | |
449 return IOE_FAILURE; /* ID is not Correct */ | |
450 } | |
451 } | |
452 else | |
453 { | |
454 return IOE_OK; /* ID is correct */ | |
455 } | |
456 } | |
457 | |
458 /** | |
459 * @brief Resets the IO Expander by Software (SYS_CTRL1, RESET bit). | |
460 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
461 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
462 */ | |
463 uint8_t IOE_Reset(uint8_t DeviceAddr) | |
464 { | |
465 /* Power Down the IO_Expander */ | |
466 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL1, 0x02); | |
467 | |
468 /* wait for a delay to insure registers erasing */ | |
469 _delay_(2); | |
470 | |
471 /* Power On the Codec after the power off => all registers are reinitialized*/ | |
472 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL1, 0x00); | |
473 | |
474 /* If all OK return IOE_OK */ | |
475 return IOE_OK; | |
476 } | |
477 | |
478 /** | |
479 * @brief Reads the selected device's ID. | |
480 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
481 * @retval The Device ID (two bytes). | |
482 */ | |
483 uint16_t IOE_ReadID(uint8_t DeviceAddr) | |
484 { | |
485 uint16_t tmp = 0; | |
486 | |
487 /* Read device ID */ | |
488 tmp = I2C_ReadDeviceRegister(DeviceAddr, 0); | |
489 tmp = (uint32_t)(tmp << 8); | |
490 tmp |= (uint32_t)I2C_ReadDeviceRegister(DeviceAddr, 1); | |
491 | |
492 /* Return the ID */ | |
493 return (uint16_t)tmp; | |
494 } | |
495 | |
496 /** | |
497 * @brief Configures the selcted IO Expander functionalities. | |
498 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
499 * @param Fct: the functions to be configured. could be any | |
500 * combination of the following values: | |
501 * @arg IOE_IO_FCT : IO function | |
502 * @arg IOE_TS_FCT : Touch Screen function | |
503 * @arg IOE_ADC_FCT : ADC function | |
504 * @arg IOE_TEMPSENS_FCT : Tempreature Sensor function | |
505 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
506 */ | |
507 uint8_t IOE_FnctCmd(uint8_t DeviceAddr, uint8_t Fct, FunctionalState NewState) | |
508 { | |
509 uint8_t tmp = 0; | |
510 | |
511 /* Get the register value */ | |
512 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL2); | |
513 | |
514 if (NewState != DISABLE) | |
515 { | |
516 /* Set the Functionalities to be Enabled */ | |
517 tmp &= ~(uint8_t)Fct; | |
518 } | |
519 else | |
520 { | |
521 /* Set the Functionalities to be Disabled */ | |
522 tmp |= (uint8_t)Fct; | |
523 } | |
524 | |
525 /* Set the register value */ | |
526 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_SYS_CTRL2, tmp); | |
527 | |
528 /* If all OK return IOE_OK */ | |
529 return IOE_OK; | |
530 } | |
531 | |
532 /** | |
533 * @brief Configures the selected pin direction (to be an input or an output) | |
534 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
535 * @param IO_Pin: IO_Pin_x: Where x can be from 0 to 7. | |
536 * @param Direction: could be Direction_IN or Direction_OUT. | |
537 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
538 */ | |
539 uint8_t IOE_IOPinConfig(uint8_t DeviceAddr, uint8_t IO_Pin, uint8_t Direction) | |
540 { | |
541 uint8_t tmp = 0; | |
542 | |
543 /* Get all the Pins direction */ | |
544 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_DIR); | |
545 | |
546 if (Direction != Direction_IN) | |
547 { | |
548 tmp |= (uint8_t)IO_Pin; | |
549 } | |
550 else | |
551 { | |
552 tmp &= ~(uint8_t)IO_Pin; | |
553 } | |
554 | |
555 /* Write the register new value */ | |
556 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_DIR, tmp); | |
557 | |
558 /* If all OK return IOE_OK */ | |
559 return IOE_OK; | |
560 } | |
561 | |
562 /** | |
563 * @brief Enables or disables the Global interrupt. | |
564 * @param DeviceAddr: The address of the IOExpander, could be :I OE_1_ADDR. | |
565 * @param NewState: could be ENABLE or DISABLE. | |
566 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
567 */ | |
568 uint8_t IOE_GITCmd(uint8_t DeviceAddr, FunctionalState NewState) | |
569 { | |
570 uint8_t tmp = 0; | |
571 | |
572 /* Read the Interrupt Control register */ | |
573 I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_CTRL); | |
574 | |
575 if (NewState != DISABLE) | |
576 { | |
577 /* Set the global interrupts to be Enabled */ | |
578 tmp |= (uint8_t)IOE_GIT_EN; | |
579 } | |
580 else | |
581 { | |
582 /* Set the global interrupts to be Disabled */ | |
583 tmp &= ~(uint8_t)IOE_GIT_EN; | |
584 } | |
585 | |
586 /* Write Back the Interrupt Control register */ | |
587 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_CTRL, tmp); | |
588 | |
589 /* If all OK return IOE_OK */ | |
590 return IOE_OK; | |
591 } | |
592 | |
593 /** | |
594 * @brief Configures the selected source to generate or not a global interrupt | |
595 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR | |
596 * @param Global_IT: the interrupt source to be configured, could be: | |
597 * @arg Global_IT_GPIO : All IOs interrupt | |
598 * @arg Global_IT_ADC : ADC interrupt | |
599 * @arg Global_IT_TEMP : Temperature Sensor interrupts | |
600 * @arg Global_IT_FE : Touch Screen Controller FIFO Error interrupt | |
601 * @arg Global_IT_FF : Touch Screen Controller FIFO Full interrupt | |
602 * @arg Global_IT_FOV : Touch Screen Controller FIFO Overrun interrupt | |
603 * @arg Global_IT_FTH : Touch Screen Controller FIFO Threshold interrupt | |
604 * @arg Global_IT_TOUCH : Touch Screen Controller Touch Detected interrupt | |
605 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
606 */ | |
607 uint8_t IOE_GITConfig(uint8_t DeviceAddr, uint8_t Global_IT, FunctionalState NewState) | |
608 { | |
609 uint8_t tmp = 0; | |
610 | |
611 /* Get the current value of the INT_EN register */ | |
612 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_INT_EN); | |
613 | |
614 if (NewState != DISABLE) | |
615 { | |
616 /* Set the interrupts to be Enabled */ | |
617 tmp |= (uint8_t)Global_IT; | |
618 } | |
619 else | |
620 { | |
621 /* Set the interrupts to be Disabled */ | |
622 tmp &= ~(uint8_t)Global_IT; | |
623 } | |
624 /* Set the register */ | |
625 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_INT_EN, tmp); | |
626 | |
627 /* If all OK return IOE_OK */ | |
628 return IOE_OK; | |
629 } | |
630 | |
631 /** | |
632 * @brief Configures the selected pins to generate an interrupt or not. | |
633 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
634 * @param IO_IT: The IO interrupt to be configured. This parameter could be any | |
635 * combination of the following values: | |
636 * @arg IO_IT_x: where x can be from 0 to 7. | |
637 * @param NewState: could be ENABLE or DISABLE. | |
638 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
639 */ | |
640 uint8_t IOE_IOITConfig(uint8_t DeviceAddr, uint8_t IO_IT, FunctionalState NewState) | |
641 { | |
642 uint8_t tmp = 0; | |
643 | |
644 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_EN); | |
645 | |
646 if (NewState != DISABLE) | |
647 { | |
648 /* Set the interrupts to be Enabled */ | |
649 tmp |= (uint8_t)IO_IT; | |
650 } | |
651 else | |
652 { | |
653 /* Set the interrupts to be Disabled */ | |
654 tmp &= ~(uint8_t)IO_IT; | |
655 } | |
656 | |
657 /* Set the register */ | |
658 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_INT_EN, tmp); | |
659 | |
660 /* If all OK return IOE_OK */ | |
661 return IOE_OK; | |
662 } | |
663 | |
664 /** | |
665 * @brief Configures the touch Screen Controller (Single point detection) | |
666 * @param None | |
667 * @retval IOE_OK if all initializations are OK. Other value if error. | |
668 */ | |
669 uint8_t IOE_TS_Config(void) | |
670 { | |
671 uint8_t tmp = 0; | |
672 | |
673 /* Enable TSC Fct: already done in IOE_Config */ | |
674 tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_SYS_CTRL2); | |
675 tmp &= ~(uint32_t)(IOE_TS_FCT | IOE_ADC_FCT); | |
676 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_SYS_CTRL2, tmp); | |
677 | |
678 /* Enable the TSC gloabl interrupts */ | |
679 tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_INT_EN); | |
680 tmp |= (uint32_t)(IOE_GIT_TOUCH | IOE_GIT_FTH | IOE_GIT_FOV); | |
681 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_INT_EN, tmp); | |
682 | |
683 /* Select Sample Time, bit number and ADC Reference */ | |
684 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_ADC_CTRL1, 0x49); | |
685 | |
686 /* Wait for ~20 ms */ | |
687 _delay_(2); | |
688 | |
689 /* Select the ADC clock speed: 3.25 MHz */ | |
690 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_ADC_CTRL2, 0x01); | |
691 | |
692 /* Select TSC pins in non default mode */ | |
693 tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_AF); | |
694 tmp &= ~(uint8_t)TOUCH_IO_ALL; | |
695 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_GPIO_AF, tmp); | |
696 | |
697 /* Select 2 nF filter capacitor */ | |
698 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CFG, 0x9A); | |
699 | |
700 /* Select single point reading */ | |
701 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_TH, 0x01); | |
702 | |
703 /* Write 0x01 to clear the FIFO memory content. */ | |
704 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x01); | |
705 | |
706 /* Write 0x00 to put the FIFO back into operation mode */ | |
707 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_FIFO_STA, 0x00); | |
708 | |
709 /* set the data format for Z value: 7 fractional part and 1 whole part */ | |
710 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_FRACT_XYZ, 0x01); | |
711 | |
712 /* set the driving capability of the device for TSC pins: 50mA */ | |
713 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_I_DRIVE, 0x01); | |
714 | |
715 /* Use no tracking index, touchscreen controller operation mode (XYZ) and | |
716 enable the TSC */ | |
717 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TSC_CTRL, 0x01); | |
718 | |
719 /* Clear all the status pending bits */ | |
720 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_INT_STA, 0xFF); | |
721 | |
722 /* Initialize the TS structure to their default values */ | |
723 TS_State.TouchDetected = TS_State.X = TS_State.Y = TS_State.Z = 0; | |
724 | |
725 /* All configuration done */ | |
726 return IOE_OK; | |
727 } | |
728 | |
729 /** | |
730 * @brief Configures and enables the Temperature sensor module. | |
731 * @param None | |
732 * @retval IOE_OK if all initializations are OK. Other value if error. | |
733 */ | |
734 uint8_t IOE_TempSens_Config(void) | |
735 { | |
736 __IO uint8_t tmp = 0; | |
737 | |
738 /* Enable Temperature Sensor Fct: already done in IOE_Config */ | |
739 tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_SYS_CTRL2); | |
740 tmp &= ~(uint32_t)(IOE_TEMPSENS_FCT | IOE_ADC_FCT); | |
741 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_SYS_CTRL2, tmp); | |
742 | |
743 /* Enable the TEMPSENS module */ | |
744 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_CTRL, 0x01); | |
745 | |
746 /* Aquire data enable */ | |
747 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_TEMP_CTRL, 0x3); | |
748 | |
749 /* All configuration done */ | |
750 return IOE_OK; | |
751 } | |
752 | |
753 /** | |
754 * @brief Configures the selected pin to be in Alternate function or not | |
755 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
756 * @param IO_Pin: IO_Pin_x, Where x can be from 0 to 7. | |
757 * @param NewState: State of the AF for the selected pin, could be | |
758 * ENABLE or DISABLE. | |
759 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
760 */ | |
761 uint8_t IOE_IOAFConfig(uint8_t DeviceAddr, uint8_t IO_Pin, FunctionalState NewState) | |
762 { | |
763 uint8_t tmp = 0; | |
764 | |
765 /* Get the current state of the GPIO_AF register */ | |
766 tmp = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_AF); | |
767 | |
768 if (NewState != DISABLE) | |
769 { | |
770 /* Enable the selected pins alternate function */ | |
771 tmp |= (uint8_t)IO_Pin; | |
772 } | |
773 else | |
774 { | |
775 /* Disable the selected pins alternate function */ | |
776 tmp &= ~(uint8_t)IO_Pin; | |
777 } | |
778 | |
779 /* Write back the new valu in GPIO_AF register */ | |
780 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_AF, tmp); | |
781 | |
782 /* If all OK return IOE_OK */ | |
783 return IOE_OK; | |
784 } | |
785 | |
786 /** | |
787 * @brief Configures the Edge for which a transition is detectable for the | |
788 * the selected pin. | |
789 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
790 * @param IO_Pin: IO_Pin_x, Where x can be from 0 to 7. | |
791 * @param Edge: The edge which will be detected. This parameter can be one or a | |
792 * a combination of follwing values: EDGE_FALLING and EDGE_RISING . | |
793 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
794 */ | |
795 uint8_t IOE_IOEdgeConfig(uint8_t DeviceAddr, uint8_t IO_Pin, uint8_t Edge) | |
796 { | |
797 uint8_t tmp1 = 0, tmp2 = 0; | |
798 | |
799 /* Get the registers values */ | |
800 tmp1 = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_FE); | |
801 tmp2 = I2C_ReadDeviceRegister(DeviceAddr, IOE_REG_GPIO_RE); | |
802 | |
803 /* Disable the Falling Edge */ | |
804 tmp1 &= ~(uint8_t)IO_Pin; | |
805 /* Disable the Falling Edge */ | |
806 tmp2 &= ~(uint8_t)IO_Pin; | |
807 | |
808 /* Enable the Falling edge if selected */ | |
809 if (Edge & EDGE_FALLING) | |
810 { | |
811 tmp1 |= (uint8_t)IO_Pin; | |
812 } | |
813 | |
814 /* Enable the Rising edge if selected */ | |
815 if (Edge & EDGE_RISING) | |
816 { | |
817 tmp2 |= (uint8_t)IO_Pin; | |
818 } | |
819 | |
820 /* Write back the registers values */ | |
821 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_FE, tmp1); | |
822 I2C_WriteDeviceRegister(DeviceAddr, IOE_REG_GPIO_RE, tmp2); | |
823 | |
824 /* if OK return 0 */ | |
825 return IOE_OK; | |
826 } | |
827 | |
828 /** | |
829 * @brief Configures the Interrupt line active state and format (level/edge) | |
830 * @param Polarity: could be | |
831 * @arg Polarity_Low: Interrupt line is active Low/Falling edge | |
832 * @arg Polarity_High: Interrupt line is active High/Rising edge | |
833 * @param Type: Interrupt line activity type, could be one of the following values | |
834 * @arg Type_Level: Interrupt line is active in level model | |
835 * @arg Type_Edge: Interrupt line is active in edge model | |
836 * @retval IOE_OK: if all initializations are OK. Other value if error. | |
837 */ | |
838 uint8_t IOE_ITOutConfig(uint8_t Polarity, uint8_t Type) | |
839 { | |
840 uint8_t tmp = 0; | |
841 | |
842 /* Get the register IOE_REG_INT_CTRL value */ | |
843 tmp = I2C_ReadDeviceRegister(IOE_1_ADDR, IOE_REG_INT_CTRL); | |
844 | |
845 /* Mask the polarity and type bits */ | |
846 tmp &= ~(uint8_t)0x06; | |
847 | |
848 /* Modify the Interrupt Output line configuration */ | |
849 tmp |= (uint8_t)(Polarity | Type); | |
850 | |
851 /* Set the register */ | |
852 I2C_WriteDeviceRegister(IOE_1_ADDR, IOE_REG_INT_CTRL, tmp); | |
853 | |
854 /* If all OK return IOE_OK */ | |
855 return IOE_OK; | |
856 } | |
857 | |
858 /** | |
859 * @brief Writes a value in a register of the device through I2C. | |
860 * @param DeviceAddr: The address of the IOExpander, could be : IOE_1_ADDR. | |
861 * @param RegisterAddr: The target register adress | |
862 * @param RegisterValue: The target register value to be written | |
863 * @retval IOE_OK: if all operations are OK. Other value if error. | |
864 */ | |
865 uint8_t I2C_WriteDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr, uint8_t RegisterValue) | |
866 { | |
867 uint32_t read_verif = 0; | |
868 uint8_t IOE_BufferTX = 0; | |
869 | |
870 /* Get Value to be written */ | |
871 IOE_BufferTX = RegisterValue; | |
872 | |
873 /* Configure DMA Peripheral */ | |
874 IOE_DMA_Config(IOE_DMA_TX, (uint8_t*)(&IOE_BufferTX)); | |
875 | |
876 /* Enable the I2C peripheral */ | |
877 I2C_GenerateSTART(IOE_I2C, ENABLE); | |
878 | |
879 /* Test on SB Flag */ | |
880 IOE_TimeOut = TIMEOUT_MAX; | |
881 while (I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB) == RESET) | |
882 { | |
883 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
884 } | |
885 | |
886 /* Transmit the slave address and enable writing operation */ | |
887 I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); | |
888 | |
889 /* Test on ADDR Flag */ | |
890 IOE_TimeOut = TIMEOUT_MAX; | |
891 while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) | |
892 { | |
893 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
894 } | |
895 | |
896 /* Transmit the first address for r/w operations */ | |
897 I2C_SendData(IOE_I2C, RegisterAddr); | |
898 | |
899 /* Test on TXE FLag (data dent) */ | |
900 IOE_TimeOut = TIMEOUT_MAX; | |
901 while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF))) | |
902 { | |
903 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
904 } | |
905 | |
906 /* Enable I2C DMA request */ | |
907 I2C_DMACmd(IOE_I2C,ENABLE); | |
908 | |
909 /* Enable DMA TX Channel */ | |
910 DMA_Cmd(IOE_DMA_TX_CHANNEL, ENABLE); | |
911 | |
912 /* Wait until DMA Transfer Complete */ | |
913 IOE_TimeOut = TIMEOUT_MAX; | |
914 while (!DMA_GetFlagStatus(IOE_DMA_TX_TCFLAG)) | |
915 { | |
916 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
917 } | |
918 | |
919 /* Wait until BTF Flag is set before generating STOP */ | |
920 IOE_TimeOut = 2 * TIMEOUT_MAX; | |
921 while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF))) | |
922 { | |
923 } | |
924 | |
925 /* Send STOP Condition */ | |
926 I2C_GenerateSTOP(IOE_I2C, ENABLE); | |
927 | |
928 /* Disable DMA TX Channel */ | |
929 DMA_Cmd(IOE_DMA_TX_CHANNEL, DISABLE); | |
930 | |
931 /* Disable I2C DMA request */ | |
932 I2C_DMACmd(IOE_I2C,DISABLE); | |
933 | |
934 /* Clear DMA TX Transfer Complete Flag */ | |
935 DMA_ClearFlag(IOE_DMA_TX_TCFLAG); | |
936 | |
937 #ifdef VERIFY_WRITTENDATA | |
938 /* Verify (if needed) that the loaded data is correct */ | |
939 | |
940 /* Read the just written register*/ | |
941 read_verif = I2C_ReadDeviceRegister(DeviceAddr, RegisterAddr); | |
942 /* Load the register and verify its value */ | |
943 if (read_verif != RegisterValue) | |
944 { | |
945 /* Control data wrongly tranfered */ | |
946 read_verif = IOE_FAILURE; | |
947 } | |
948 else | |
949 { | |
950 /* Control data correctly transfered */ | |
951 read_verif = 0; | |
952 } | |
953 #endif | |
954 | |
955 /* Return the verifying value: 0 (Passed) or 1 (Failed) */ | |
956 return read_verif; | |
957 } | |
958 | |
959 /** | |
960 * @brief Reads a register of the device through I2C. | |
961 * @param DeviceAddr: The address of the device, could be : IOE_1_ADDR. | |
962 * @param RegisterAddr: The target register adress (between 00x and 0x24) | |
963 * @retval The value of the read register (0xAA if Timout occured) | |
964 */ | |
965 uint8_t I2C_ReadDeviceRegister(uint8_t DeviceAddr, uint8_t RegisterAddr) | |
966 { | |
967 uint8_t IOE_BufferRX[2] = {0x00, 0x00}; | |
968 | |
969 /* Configure DMA Peripheral */ | |
970 IOE_DMA_Config(IOE_DMA_RX, (uint8_t*)IOE_BufferRX); | |
971 | |
972 /* Enable DMA NACK automatic generation */ | |
973 I2C_DMALastTransferCmd(IOE_I2C, ENABLE); | |
974 | |
975 /* Enable the I2C peripheral */ | |
976 I2C_GenerateSTART(IOE_I2C, ENABLE); | |
977 | |
978 /* Test on SB Flag */ | |
979 IOE_TimeOut = TIMEOUT_MAX; | |
980 while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB)) | |
981 { | |
982 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
983 } | |
984 | |
985 /* Send device address for write */ | |
986 I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); | |
987 | |
988 /* Test on ADDR Flag */ | |
989 IOE_TimeOut = TIMEOUT_MAX; | |
990 while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) | |
991 { | |
992 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
993 } | |
994 | |
995 /* Send the device's internal address to write to */ | |
996 I2C_SendData(IOE_I2C, RegisterAddr); | |
997 | |
998 /* Test on TXE FLag (data dent) */ | |
999 IOE_TimeOut = TIMEOUT_MAX; | |
1000 while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF))) | |
1001 { | |
1002 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1003 } | |
1004 | |
1005 /* Send START condition a second time */ | |
1006 I2C_GenerateSTART(IOE_I2C, ENABLE); | |
1007 | |
1008 /* Test on SB Flag */ | |
1009 IOE_TimeOut = TIMEOUT_MAX; | |
1010 while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB)) | |
1011 { | |
1012 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1013 } | |
1014 | |
1015 /* Send IOExpander address for read */ | |
1016 I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Receiver); | |
1017 | |
1018 /* Test on ADDR Flag */ | |
1019 IOE_TimeOut = TIMEOUT_MAX; | |
1020 while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) | |
1021 { | |
1022 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1023 } | |
1024 | |
1025 /* Enable I2C DMA request */ | |
1026 I2C_DMACmd(IOE_I2C,ENABLE); | |
1027 | |
1028 /* Enable DMA RX Channel */ | |
1029 DMA_Cmd(IOE_DMA_RX_CHANNEL, ENABLE); | |
1030 | |
1031 /* Wait until DMA Transfer Complete */ | |
1032 IOE_TimeOut = 2 * TIMEOUT_MAX; | |
1033 while (!DMA_GetFlagStatus(IOE_DMA_RX_TCFLAG)) | |
1034 { | |
1035 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1036 } | |
1037 | |
1038 /* Send STOP Condition */ | |
1039 I2C_GenerateSTOP(IOE_I2C, ENABLE); | |
1040 | |
1041 /* Disable DMA RX Channel */ | |
1042 DMA_Cmd(IOE_DMA_RX_CHANNEL, DISABLE); | |
1043 | |
1044 /* Disable I2C DMA request */ | |
1045 I2C_DMACmd(IOE_I2C,DISABLE); | |
1046 | |
1047 /* Clear DMA RX Transfer Complete Flag */ | |
1048 DMA_ClearFlag(IOE_DMA_RX_TCFLAG); | |
1049 | |
1050 /* return a pointer to the IOE_Buffer */ | |
1051 return (uint8_t)IOE_BufferRX[0]; | |
1052 } | |
1053 | |
1054 | |
1055 /** | |
1056 * @brief Reads a buffer of 2 bytes from the device registers. | |
1057 * @param DeviceAddr: The address of the device, could be : IOE_1_ADDR. | |
1058 * @param RegisterAddr: The target register adress (between 00x and 0x24) | |
1059 * @retval A pointer to the buffer containing the two returned bytes (in halfword). | |
1060 */ | |
1061 uint16_t I2C_ReadDataBuffer(uint8_t DeviceAddr, uint32_t RegisterAddr) | |
1062 { | |
1063 uint8_t tmp= 0; | |
1064 uint8_t IOE_BufferRX[2] = {0x00, 0x00}; | |
1065 | |
1066 /* Configure DMA Peripheral */ | |
1067 IOE_DMA_Config(IOE_DMA_RX, (uint8_t*)IOE_BufferRX); | |
1068 | |
1069 /* Enable DMA NACK automatic generation */ | |
1070 I2C_DMALastTransferCmd(IOE_I2C, ENABLE); | |
1071 | |
1072 /* Enable the I2C peripheral */ | |
1073 I2C_GenerateSTART(IOE_I2C, ENABLE); | |
1074 | |
1075 /* Test on SB Flag */ | |
1076 IOE_TimeOut = TIMEOUT_MAX; | |
1077 while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB)) | |
1078 { | |
1079 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1080 } | |
1081 | |
1082 /* Send device address for write */ | |
1083 I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Transmitter); | |
1084 | |
1085 /* Test on ADDR Flag */ | |
1086 IOE_TimeOut = TIMEOUT_MAX; | |
1087 while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) | |
1088 { | |
1089 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1090 } | |
1091 | |
1092 /* Send the device's internal address to write to */ | |
1093 I2C_SendData(IOE_I2C, RegisterAddr); | |
1094 | |
1095 /* Test on TXE FLag (data dent) */ | |
1096 IOE_TimeOut = TIMEOUT_MAX; | |
1097 while ((!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_BTF))) | |
1098 { | |
1099 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1100 } | |
1101 | |
1102 /* Send START condition a second time */ | |
1103 I2C_GenerateSTART(IOE_I2C, ENABLE); | |
1104 | |
1105 /* Test on SB Flag */ | |
1106 IOE_TimeOut = TIMEOUT_MAX; | |
1107 while (!I2C_GetFlagStatus(IOE_I2C,I2C_FLAG_SB)) | |
1108 { | |
1109 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1110 } | |
1111 | |
1112 /* Send IOExpander address for read */ | |
1113 I2C_Send7bitAddress(IOE_I2C, DeviceAddr, I2C_Direction_Receiver); | |
1114 | |
1115 /* Test on ADDR Flag */ | |
1116 IOE_TimeOut = TIMEOUT_MAX; | |
1117 while (!I2C_CheckEvent(IOE_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) | |
1118 { | |
1119 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1120 } | |
1121 | |
1122 /* Enable I2C DMA request */ | |
1123 I2C_DMACmd(IOE_I2C,ENABLE); | |
1124 | |
1125 /* Enable DMA RX Channel */ | |
1126 DMA_Cmd(IOE_DMA_RX_CHANNEL, ENABLE); | |
1127 | |
1128 /* Wait until DMA Transfer Complete */ | |
1129 IOE_TimeOut = 2 * TIMEOUT_MAX; | |
1130 while (!DMA_GetFlagStatus(IOE_DMA_RX_TCFLAG)) | |
1131 { | |
1132 if (IOE_TimeOut-- == 0) return(IOE_TimeoutUserCallback()); | |
1133 } | |
1134 | |
1135 /* Send STOP Condition */ | |
1136 I2C_GenerateSTOP(IOE_I2C, ENABLE); | |
1137 | |
1138 /* Disable DMA RX Channel */ | |
1139 DMA_Cmd(IOE_DMA_RX_CHANNEL, DISABLE); | |
1140 | |
1141 /* Disable I2C DMA request */ | |
1142 I2C_DMACmd(IOE_I2C,DISABLE); | |
1143 | |
1144 /* Clear DMA RX Transfer Complete Flag */ | |
1145 DMA_ClearFlag(IOE_DMA_RX_TCFLAG); | |
1146 | |
1147 /* Reorganize received data */ | |
1148 tmp = IOE_BufferRX[0]; | |
1149 IOE_BufferRX[0] = IOE_BufferRX[1]; | |
1150 IOE_BufferRX[1] = tmp; | |
1151 | |
1152 /* return a pointer to the IOE_Buffer */ | |
1153 return *(uint16_t *)IOE_BufferRX; | |
1154 } | |
1155 | |
1156 /** | |
1157 * @brief Return Touch Screen X position value | |
1158 * @param None | |
1159 * @retval X position. | |
1160 */ | |
1161 static uint16_t IOE_TS_Read_X(void) | |
1162 { | |
1163 int32_t x, xr; | |
1164 | |
1165 x = I2C_ReadDataBuffer(IOE_1_ADDR, IOE_REG_TSC_DATA_Y); | |
1166 | |
1167 /* first correction */ | |
1168 xr = (x * 320) >> 12; | |
1169 /* second correction */ | |
1170 xr = ((xr * 32)/29) - 17; | |
1171 | |
1172 if(xr <= 0) | |
1173 xr = 0; | |
1174 | |
1175 return (uint16_t)(xr); | |
1176 } | |
1177 | |
1178 /** | |
1179 * @brief Return Touch Screen Y position value | |
1180 * @param None | |
1181 * @retval Y position. | |
1182 */ | |
1183 static uint16_t IOE_TS_Read_Y(void) | |
1184 { | |
1185 int32_t y, yr; | |
1186 y= I2C_ReadDataBuffer(IOE_1_ADDR, IOE_REG_TSC_DATA_X); | |
1187 | |
1188 yr= (y * 240) >> 12; | |
1189 yr = ((yr * 240) / 217) - 12; | |
1190 | |
1191 if(yr <= 0) | |
1192 yr = 0; | |
1193 | |
1194 return (uint16_t)(yr); | |
1195 } | |
1196 | |
1197 /** | |
1198 * @brief Return Touch Screen Z position value | |
1199 * @param None | |
1200 * @retval Z position. | |
1201 */ | |
1202 static uint16_t IOE_TS_Read_Z(void) | |
1203 { | |
1204 uint32_t z; | |
1205 z = I2C_ReadDataBuffer(IOE_1_ADDR, IOE_REG_TSC_DATA_Z); | |
1206 | |
1207 | |
1208 if(z <= 0) | |
1209 z = 0; | |
1210 | |
1211 return (uint16_t)(z); | |
1212 } | |
1213 | |
1214 /** | |
1215 * @brief Initializes the GPIO pins used by the IO expander. | |
1216 * @param None | |
1217 * @retval None | |
1218 */ | |
1219 static void IOE_GPIO_Config(void) | |
1220 { | |
1221 GPIO_InitTypeDef GPIO_InitStructure; | |
1222 | |
1223 /* Enable IOE_I2C and IOE_I2C_PORT & Alternate Function clocks */ | |
1224 RCC_APB1PeriphClockCmd(IOE_I2C_CLK, ENABLE); | |
1225 RCC_APB2PeriphClockCmd(IOE_I2C_SCL_GPIO_CLK | IOE_I2C_SDA_GPIO_CLK | IOE_IT_GPIO_CLK | |
1226 | RCC_APB2Periph_AFIO, ENABLE); | |
1227 | |
1228 /* Reset IOE_I2C IP */ | |
1229 RCC_APB1PeriphResetCmd(IOE_I2C_CLK, ENABLE); | |
1230 | |
1231 /* Release reset signal of IOE_I2C IP */ | |
1232 RCC_APB1PeriphResetCmd(IOE_I2C_CLK, DISABLE); | |
1233 | |
1234 /* IOE_I2C SCL and SDA pins configuration */ | |
1235 GPIO_InitStructure.GPIO_Pin = IOE_I2C_SCL_PIN; | |
1236 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; | |
1237 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; | |
1238 GPIO_Init(IOE_I2C_SCL_GPIO_PORT, &GPIO_InitStructure); | |
1239 | |
1240 /* IOE_I2C SCL and SDA pins configuration */ | |
1241 GPIO_InitStructure.GPIO_Pin = IOE_I2C_SDA_PIN; | |
1242 GPIO_Init(IOE_I2C_SDA_GPIO_PORT, &GPIO_InitStructure); | |
1243 | |
1244 /* Set EXTI pin as Input PullUp - IO_Expander_INT */ | |
1245 GPIO_InitStructure.GPIO_Pin = IOE_IT_PIN; | |
1246 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; | |
1247 GPIO_Init(IOE_IT_GPIO_PORT, &GPIO_InitStructure); | |
1248 | |
1249 /* Connect IO Expander IT line to EXTI line */ | |
1250 GPIO_EXTILineConfig(IOE_IT_EXTI_PORT_SOURCE, IOE_IT_EXTI_PIN_SOURCE); | |
1251 } | |
1252 | |
1253 | |
1254 /** | |
1255 * @brief Configure the I2C Peripheral used to communicate with IO_Expanders. | |
1256 * @param None | |
1257 * @retval None | |
1258 */ | |
1259 static void IOE_I2C_Config(void) | |
1260 { | |
1261 I2C_InitTypeDef I2C_InitStructure; | |
1262 | |
1263 /* IOE_I2C configuration */ | |
1264 I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; | |
1265 I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; | |
1266 I2C_InitStructure.I2C_OwnAddress1 = 0x00; | |
1267 I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; | |
1268 I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; | |
1269 I2C_InitStructure.I2C_ClockSpeed = IOE_I2C_SPEED; | |
1270 | |
1271 I2C_Init(IOE_I2C, &I2C_InitStructure); | |
1272 } | |
1273 | |
1274 | |
1275 /** | |
1276 * @brief Configure the DMA Peripheral used to handle communication via I2C. | |
1277 * @param None | |
1278 * @retval None | |
1279 */ | |
1280 | |
1281 static void IOE_DMA_Config(IOE_DMADirection_TypeDef Direction, uint8_t* buffer) | |
1282 { | |
1283 DMA_InitTypeDef DMA_InitStructure; | |
1284 | |
1285 RCC_AHBPeriphClockCmd(IOE_DMA_CLK, ENABLE); | |
1286 | |
1287 /* Initialize the DMA_PeripheralBaseAddr member */ | |
1288 DMA_InitStructure.DMA_PeripheralBaseAddr = IOE_I2C_DR; | |
1289 /* Initialize the DMA_MemoryBaseAddr member */ | |
1290 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer; | |
1291 /* Initialize the DMA_PeripheralInc member */ | |
1292 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; | |
1293 /* Initialize the DMA_MemoryInc member */ | |
1294 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; | |
1295 /* Initialize the DMA_PeripheralDataSize member */ | |
1296 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; | |
1297 /* Initialize the DMA_MemoryDataSize member */ | |
1298 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; | |
1299 /* Initialize the DMA_Mode member */ | |
1300 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; | |
1301 /* Initialize the DMA_Priority member */ | |
1302 DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; | |
1303 /* Initialize the DMA_M2M member */ | |
1304 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; | |
1305 | |
1306 /* If using DMA for Reception */ | |
1307 if (Direction == IOE_DMA_RX) | |
1308 { | |
1309 /* Initialize the DMA_DIR member */ | |
1310 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; | |
1311 | |
1312 /* Initialize the DMA_BufferSize member */ | |
1313 DMA_InitStructure.DMA_BufferSize = 2; | |
1314 | |
1315 DMA_DeInit(IOE_DMA_RX_CHANNEL); | |
1316 | |
1317 DMA_Init(IOE_DMA_RX_CHANNEL, &DMA_InitStructure); | |
1318 } | |
1319 /* If using DMA for Transmission */ | |
1320 else if (Direction == IOE_DMA_TX) | |
1321 { | |
1322 /* Initialize the DMA_DIR member */ | |
1323 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; | |
1324 | |
1325 /* Initialize the DMA_BufferSize member */ | |
1326 DMA_InitStructure.DMA_BufferSize = 1; | |
1327 | |
1328 DMA_DeInit(IOE_DMA_TX_CHANNEL); | |
1329 | |
1330 DMA_Init(IOE_DMA_TX_CHANNEL, &DMA_InitStructure); | |
1331 } | |
1332 } | |
1333 | |
1334 /** | |
1335 * @brief Configures the IO expander Interrupt line and GPIO in EXTI mode. | |
1336 * @param None | |
1337 * @retval None | |
1338 */ | |
1339 static void IOE_EXTI_Config(void) | |
1340 { | |
1341 GPIO_InitTypeDef GPIO_InitStructure; | |
1342 NVIC_InitTypeDef NVIC_InitStructure; | |
1343 EXTI_InitTypeDef EXTI_InitStructure; | |
1344 | |
1345 /* Enable Button GPIO clock */ | |
1346 RCC_APB2PeriphClockCmd(IOE_IT_GPIO_CLK | RCC_APB2Periph_AFIO, ENABLE); | |
1347 | |
1348 /* Configure Button pin as input floating */ | |
1349 GPIO_InitStructure.GPIO_Pin = IOE_IT_PIN; | |
1350 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; | |
1351 GPIO_Init(IOE_IT_GPIO_PORT, &GPIO_InitStructure); | |
1352 | |
1353 /* Connect Button EXTI Line to Button GPIO Pin */ | |
1354 GPIO_EXTILineConfig(IOE_IT_EXTI_PORT_SOURCE, IOE_IT_EXTI_PIN_SOURCE); | |
1355 | |
1356 /* Configure Button EXTI line */ | |
1357 EXTI_InitStructure.EXTI_Line = IOE_IT_EXTI_LINE; | |
1358 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; | |
1359 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; | |
1360 EXTI_InitStructure.EXTI_LineCmd = ENABLE; | |
1361 EXTI_Init(&EXTI_InitStructure); | |
1362 | |
1363 /* Enable and set Button EXTI Interrupt to the lowest priority */ | |
1364 NVIC_InitStructure.NVIC_IRQChannel = IOE_IT_EXTI_IRQn; | |
1365 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; | |
1366 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; | |
1367 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; | |
1368 NVIC_Init(&NVIC_InitStructure); | |
1369 } | |
1370 | |
1371 #ifndef USE_Delay | |
1372 /** | |
1373 * @brief Inserts a delay time. | |
1374 * @param nCount: specifies the delay time length. | |
1375 * @retval None | |
1376 */ | |
1377 static void delay(__IO uint32_t nCount) | |
1378 { | |
1379 __IO uint32_t index = 0; | |
1380 for(index = (100000 * nCount); index != 0; index--) | |
1381 { | |
1382 } | |
1383 } | |
1384 #endif /* USE_Delay*/ | |
1385 /** | |
1386 * @} | |
1387 */ | |
1388 | |
1389 /** | |
1390 * @} | |
1391 */ | |
1392 | |
1393 /** | |
1394 * @} | |
1395 */ | |
1396 | |
1397 /** | |
1398 * @} | |
1399 */ | |
1400 | |
1401 /** | |
1402 * @} | |
1403 */ | |
1404 | |
1405 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |