hal_key.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. /**************************************************************************************************
  2. Filename: hal_key.c
  3. Revised: $Date: 2009-12-16 17:44:49 -0800 (Wed, 16 Dec 2009) $
  4. Revision: $Revision: 21351 $
  5. Description: This file contains the interface to the HAL KEY Service.
  6. Copyright 2006-2009 Texas Instruments Incorporated. All rights reserved.
  7. IMPORTANT: Your use of this Software is limited to those specific rights
  8. granted under the terms of a software license agreement between the user
  9. who downloaded the software, his/her employer (which must be your employer)
  10. and Texas Instruments Incorporated (the "License"). You may not use this
  11. Software unless you agree to abide by the terms of the License. The License
  12. limits your use, and you acknowledge, that the Software may not be modified,
  13. copied or distributed unless embedded on a Texas Instruments microcontroller
  14. or used solely and exclusively in conjunction with a Texas Instruments radio
  15. frequency transceiver, which is integrated into your product. Other than for
  16. the foregoing purpose, you may not use, reproduce, copy, prepare derivative
  17. works of, modify, distribute, perform, display or sell this Software and/or
  18. its documentation for any purpose.
  19. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  20. PROVIDED “AS IS?WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  21. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  22. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  23. TEXAS INSTRUMENTS OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER CONTRACT,
  24. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  25. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  26. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  27. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  28. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  29. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  30. Should you have any questions regarding your right to use this Software,
  31. contact Texas Instruments Incorporated at www.TI.com.
  32. **************************************************************************************************/
  33. /*********************************************************************
  34. NOTE: If polling is used, the hal_driver task schedules the KeyRead()
  35. to occur every 100ms. This should be long enough to naturally
  36. debounce the keys. The KeyRead() function remembers the key
  37. state of the previous poll and will only return a non-zero
  38. value if the key state changes.
  39. NOTE: If interrupts are used, the KeyRead() function is scheduled
  40. 25ms after the interrupt occurs by the ISR. This delay is used
  41. for key debouncing. The ISR disables any further Key interrupt
  42. until KeyRead() is executed. KeyRead() will re-enable Key
  43. interrupts after executing. Unlike polling, when interrupts
  44. are enabled, the previous key state is not remembered. This
  45. means that KeyRead() will return the current state of the keys
  46. (not a change in state of the keys).
  47. NOTE: If interrupts are used, the KeyRead() fucntion is scheduled by
  48. the ISR. Therefore, the joystick movements will only be detected
  49. during a pushbutton interrupt caused by S1 or the center joystick
  50. pushbutton.
  51. NOTE: When a switch like S1 is pushed, the S1 signal goes from a normally
  52. high state to a low state. This transition is typically clean. The
  53. duration of the low state is around 200ms. When the signal returns
  54. to the high state, there is a high likelihood of signal bounce, which
  55. causes a unwanted interrupts. Normally, we would set the interrupt
  56. edge to falling edge to generate an interrupt when S1 is pushed, but
  57. because of the signal bounce, it is better to set the edge to rising
  58. edge to generate an interrupt when S1 is released. The debounce logic
  59. can then filter out the signal bounce. The result is that we typically
  60. get only 1 interrupt per button push. This mechanism is not totally
  61. foolproof because occasionally, signal bound occurs during the falling
  62. edge as well. A similar mechanism is used to handle the joystick
  63. pushbutton on the DB. For the EB, we do not have independent control
  64. of the interrupt edge for the S1 and center joystick pushbutton. As
  65. a result, only one or the other pushbuttons work reasonably well with
  66. interrupts. The default is the make the S1 switch on the EB work more
  67. reliably.
  68. *********************************************************************/
  69. /**************************************************************************************************
  70. * INCLUDES
  71. **************************************************************************************************/
  72. #include "hal_mcu.h"
  73. #include "hal_defs.h"
  74. #include "hal_types.h"
  75. #include "hal_board.h"
  76. #include "hal_drivers.h"
  77. #include "hal_adc.h"
  78. #include "hal_key.h"
  79. #include "osal.h"
  80. #if (defined HAL_KEY) && (HAL_KEY == TRUE)
  81. /**************************************************************************************************
  82. * MACROS
  83. **************************************************************************************************/
  84. /**************************************************************************************************
  85. * CONSTANTS
  86. **************************************************************************************************/
  87. #define HAL_KEY_RISING_EDGE 0
  88. #define HAL_KEY_FALLING_EDGE 1
  89. #define HAL_KEY_DEBOUNCE_VALUE 25
  90. #define HAL_KEY_POLLING_VALUE 100
  91. /* CPU port interrupt */
  92. #define HAL_KEY_CPU_PORT_0_IF P0IF
  93. #define HAL_KEY_CPU_PORT_2_IF P2IF
  94. /* SW_6 is at P0.1 */
  95. #define HAL_KEY_SW_6_PORT P0
  96. #define HAL_KEY_SW_6_BIT BV(1)
  97. #define HAL_KEY_SW_6_SEL P0SEL
  98. #define HAL_KEY_SW_6_DIR P0DIR
  99. /* edge interrupt */
  100. #define HAL_KEY_SW_6_EDGEBIT BV(0)
  101. #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE
  102. /* SW_6 interrupts */
  103. #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
  104. #define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */
  105. #define HAL_KEY_SW_6_ICTL P0IEN /* Port Interrupt Control register */
  106. #define HAL_KEY_SW_6_ICTLBIT BV(1) /* P0IEN - P0.1 enable/disable bit */
  107. #define HAL_KEY_SW_6_PXIFG P0IFG /* Interrupt flag at source */
  108. /* Joy stick move at P2.0 */
  109. #define HAL_KEY_JOY_MOVE_PORT P2
  110. #define HAL_KEY_JOY_MOVE_BIT BV(0)
  111. #define HAL_KEY_JOY_MOVE_SEL P2SEL
  112. #define HAL_KEY_JOY_MOVE_DIR P2DIR
  113. /* edge interrupt */
  114. #define HAL_KEY_JOY_MOVE_EDGEBIT BV(3)
  115. #define HAL_KEY_JOY_MOVE_EDGE HAL_KEY_FALLING_EDGE
  116. /* Joy move interrupts */
  117. #define HAL_KEY_JOY_MOVE_IEN IEN2 /* CPU interrupt mask register */
  118. #define HAL_KEY_JOY_MOVE_IENBIT BV(1) /* Mask bit for all of Port_2 */
  119. #define HAL_KEY_JOY_MOVE_ICTL P2IEN /* Port Interrupt Control register */
  120. #define HAL_KEY_JOY_MOVE_ICTLBIT BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */
  121. #define HAL_KEY_JOY_MOVE_PXIFG P2IFG /* Interrupt flag at source */
  122. #define HAL_KEY_JOY_CHN HAL_ADC_CHANNEL_6
  123. /**************************************************************************************************
  124. * TYPEDEFS
  125. **************************************************************************************************/
  126. /**************************************************************************************************
  127. * GLOBAL VARIABLES
  128. **************************************************************************************************/
  129. static uint8 halKeySavedKeys; /* used to store previous key state in polling mode */
  130. static halKeyCBack_t pHalKeyProcessFunction;
  131. static uint8 HalKeyConfigured;
  132. bool Hal_KeyIntEnable; /* interrupt enable/disable flag */
  133. /**************************************************************************************************
  134. * FUNCTIONS - Local
  135. **************************************************************************************************/
  136. void halProcessKeyInterrupt(void);
  137. uint8 halGetJoyKeyInput(void);
  138. /**************************************************************************************************
  139. * FUNCTIONS - API
  140. **************************************************************************************************/
  141. /**************************************************************************************************
  142. * @fn HalKeyInit
  143. *
  144. * @brief Initilize Key Service
  145. *
  146. * @param none
  147. *
  148. * @return None
  149. **************************************************************************************************/
  150. void HalKeyInit( void )
  151. {
  152. /* Initialize previous key to 0 */
  153. halKeySavedKeys = 0;
  154. HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT); /* Set pin function to GPIO */
  155. HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT); /* Set pin direction to Input */
  156. HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */
  157. HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */
  158. /* Initialize callback function */
  159. pHalKeyProcessFunction = NULL;
  160. /* Start with key is not configured */
  161. HalKeyConfigured = FALSE;
  162. }
  163. /**************************************************************************************************
  164. * @fn HalKeyConfig
  165. *
  166. * @brief Configure the Key serivce
  167. *
  168. * @param interruptEnable - TRUE/FALSE, enable/disable interrupt
  169. * cback - pointer to the CallBack function
  170. *
  171. * @return None
  172. **************************************************************************************************/
  173. void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
  174. {
  175. /* Enable/Disable Interrupt or */
  176. Hal_KeyIntEnable = interruptEnable;
  177. /* Register the callback fucntion */
  178. pHalKeyProcessFunction = cback;
  179. /* Determine if interrupt is enable or not */
  180. if (Hal_KeyIntEnable)
  181. {
  182. /* Rising/Falling edge configuratinn */
  183. PICTL &= ~(HAL_KEY_SW_6_EDGEBIT); /* Clear the edge bit */
  184. /* For falling edge, the bit must be set. */
  185. #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)
  186. PICTL |= HAL_KEY_SW_6_EDGEBIT;
  187. #endif
  188. /* Interrupt configuration:
  189. * - Enable interrupt generation at the port
  190. * - Enable CPU interrupt
  191. * - Clear any pending interrupt
  192. */
  193. HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;
  194. HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;
  195. HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);
  196. /* Rising/Falling edge configuratinn */
  197. HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT); /* Clear the edge bit */
  198. /* For falling edge, the bit must be set. */
  199. #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)
  200. HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;
  201. #endif
  202. /* Interrupt configuration:
  203. * - Enable interrupt generation at the port
  204. * - Enable CPU interrupt
  205. * - Clear any pending interrupt
  206. */
  207. HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;
  208. HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;
  209. HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);
  210. /* Do this only after the hal_key is configured - to work with sleep stuff */
  211. if (HalKeyConfigured == TRUE)
  212. {
  213. osal_stop_timerEx( Hal_TaskID, HAL_KEY_EVENT); /* Cancel polling if active */
  214. }
  215. }
  216. else /* Interrupts NOT enabled */
  217. {
  218. HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */
  219. HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT); /* Clear interrupt enable bit */
  220. osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE); /* Kick off polling */
  221. }
  222. /* Key now is configured */
  223. HalKeyConfigured = TRUE;
  224. }
  225. /**************************************************************************************************
  226. * @fn HalKeyRead
  227. *
  228. * @brief Read the current value of a key
  229. *
  230. * @param None
  231. *
  232. * @return keys - current keys status
  233. **************************************************************************************************/
  234. uint8 HalKeyRead ( void )
  235. {
  236. uint8 keys = 0;
  237. if (HAL_PUSH_BUTTON1())
  238. {
  239. keys |= HAL_KEY_SW_6;
  240. }
  241. /* if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) // Key is active low
  242. {
  243. keys |= halGetJoyKeyInput();
  244. }
  245. */
  246. return keys;
  247. }
  248. /**************************************************************************************************
  249. * @fn HalKeyPoll
  250. *
  251. * @brief Called by hal_driver to poll the keys
  252. *
  253. * @param None
  254. *
  255. * @return None
  256. **************************************************************************************************/
  257. void HalKeyPoll (void)
  258. {
  259. uint8 keys = 0;
  260. /* if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) // Key is active HIGH
  261. {
  262. keys = halGetJoyKeyInput();
  263. }
  264. */
  265. if (!HAL_PUSH_BUTTON2())//S0
  266. {
  267. keys |= HAL_KEY_SW_1;
  268. }
  269. if (!HAL_PUSH_BUTTON1())//S1
  270. {
  271. keys |= HAL_KEY_SW_6;
  272. }
  273. if (!Hal_KeyIntEnable)
  274. {
  275. if (keys == halKeySavedKeys)
  276. {
  277. /* Exit - since no keys have changed */
  278. return;
  279. }
  280. /* Store the current keys for comparation next time */
  281. halKeySavedKeys = keys;
  282. }
  283. else
  284. {
  285. /* Key interrupt handled here */
  286. }
  287. /* Invoke Callback if new keys were depressed */
  288. if (keys && (pHalKeyProcessFunction))
  289. {
  290. (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
  291. }
  292. }
  293. /**************************************************************************************************
  294. * @fn halGetJoyKeyInput
  295. *
  296. * @brief Map the ADC value to its corresponding key.
  297. *
  298. * @param None
  299. *
  300. * @return keys - current joy key status
  301. **************************************************************************************************/
  302. uint8 halGetJoyKeyInput(void)
  303. {
  304. /* The joystick control is encoded as an analog voltage.
  305. * Read the JOY_LEVEL analog value and map it to joy movement.
  306. */
  307. uint8 adc;
  308. uint8 ksave0 = 0;
  309. uint8 ksave1;
  310. /* Keep on reading the ADC until two consecutive key decisions are the same. */
  311. do
  312. {
  313. ksave1 = ksave0; /* save previouse key reading */
  314. adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);
  315. if ((adc >= 2) && (adc <= 38))
  316. {
  317. ksave0 |= HAL_KEY_UP;
  318. }
  319. else if ((adc >= 74) && (adc <= 88))
  320. {
  321. ksave0 |= HAL_KEY_RIGHT;
  322. }
  323. else if ((adc >= 60) && (adc <= 73))
  324. {
  325. ksave0 |= HAL_KEY_LEFT;
  326. }
  327. else if ((adc >= 39) && (adc <= 59))
  328. {
  329. ksave0 |= HAL_KEY_DOWN;
  330. }
  331. else if ((adc >= 89) && (adc <= 100))
  332. {
  333. ksave0 |= HAL_KEY_CENTER;
  334. }
  335. } while (ksave0 != ksave1);
  336. return ksave0;
  337. }
  338. /**************************************************************************************************
  339. * @fn halProcessKeyInterrupt
  340. *
  341. * @brief Checks to see if it's a valid key interrupt, saves interrupt driven key states for
  342. * processing by HalKeyRead(), and debounces keys by scheduling HalKeyRead() 25ms later.
  343. *
  344. * @param
  345. *
  346. * @return
  347. **************************************************************************************************/
  348. void halProcessKeyInterrupt (void)
  349. {
  350. bool valid=FALSE;
  351. if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT) /* Interrupt Flag has been set */
  352. {
  353. HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */
  354. valid = TRUE;
  355. }
  356. if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT) /* Interrupt Flag has been set */
  357. {
  358. HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */
  359. valid = TRUE;
  360. }
  361. if (valid)
  362. {
  363. osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);
  364. }
  365. }
  366. /**************************************************************************************************
  367. * @fn HalKeyEnterSleep
  368. *
  369. * @brief - Get called to enter sleep mode
  370. *
  371. * @param
  372. *
  373. * @return
  374. **************************************************************************************************/
  375. void HalKeyEnterSleep ( void )
  376. {
  377. }
  378. /**************************************************************************************************
  379. * @fn HalKeyExitSleep
  380. *
  381. * @brief - Get called when sleep is over
  382. *
  383. * @param
  384. *
  385. * @return - return saved keys
  386. **************************************************************************************************/
  387. uint8 HalKeyExitSleep ( void )
  388. {
  389. /* Wake up and read keys */
  390. return ( HalKeyRead () );
  391. }
  392. /***************************************************************************************************
  393. * INTERRUPT SERVICE ROUTINE
  394. ***************************************************************************************************/
  395. /**************************************************************************************************
  396. * @fn halKeyPort0Isr
  397. *
  398. * @brief Port0 ISR
  399. *
  400. * @param
  401. *
  402. * @return
  403. **************************************************************************************************/
  404. HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
  405. {
  406. if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
  407. {
  408. halProcessKeyInterrupt();
  409. }
  410. /*
  411. Clear the CPU interrupt flag for Port_0
  412. PxIFG has to be cleared before PxIF
  413. */
  414. HAL_KEY_SW_6_PXIFG = 0;
  415. HAL_KEY_CPU_PORT_0_IF = 0;
  416. }
  417. /**************************************************************************************************
  418. * @fn halKeyPort2Isr
  419. *
  420. * @brief Port2 ISR
  421. *
  422. * @param
  423. *
  424. * @return
  425. **************************************************************************************************/
  426. HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
  427. {
  428. if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)
  429. {
  430. halProcessKeyInterrupt();
  431. }
  432. /*
  433. Clear the CPU interrupt flag for Port_2
  434. PxIFG has to be cleared before PxIF
  435. Notes: P2_1 and P2_2 are debug lines.
  436. */
  437. HAL_KEY_JOY_MOVE_PXIFG = 0;
  438. HAL_KEY_CPU_PORT_2_IF = 0;
  439. }
  440. #else
  441. void HalKeyInit(void){}
  442. void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
  443. uint8 HalKeyRead(void){ return 0;}
  444. void HalKeyPoll(void){}
  445. #endif /* HAL_KEY */
  446. /**************************************************************************************************
  447. **************************************************************************************************/