hal_key.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /**************************************************************************************************
  2. Filename: hal_key.c
  3. Revised: $Date: 2010-09-15 19:02:45 -0700 (Wed, 15 Sep 2010) $
  4. Revision: $Revision: 23815 $
  5. Description: This file contains the interface to the HAL KEY Service.
  6. Copyright 2006-2010 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. /* CPU port interrupt */
  91. #define HAL_KEY_CPU_PORT_0_IF P0IF
  92. #define HAL_KEY_CPU_PORT_2_IF P2IF
  93. /* SW_6 is at P0.1 */
  94. #define HAL_KEY_SW_6_PORT P0
  95. #define HAL_KEY_SW_6_BIT BV(1)
  96. #define HAL_KEY_SW_6_SEL P0SEL
  97. #define HAL_KEY_SW_6_DIR P0DIR
  98. /* edge interrupt */
  99. #define HAL_KEY_SW_6_EDGEBIT BV(0)
  100. #define HAL_KEY_SW_6_EDGE HAL_KEY_FALLING_EDGE
  101. /* SW_6 interrupts */
  102. #define HAL_KEY_SW_6_IEN IEN1 /* CPU interrupt mask register */
  103. #define HAL_KEY_SW_6_IENBIT BV(5) /* Mask bit for all of Port_0 */
  104. #define HAL_KEY_SW_6_ICTL P0IEN /* Port Interrupt Control register */
  105. #define HAL_KEY_SW_6_ICTLBIT BV(1) /* P0IEN - P0.1 enable/disable bit */
  106. #define HAL_KEY_SW_6_PXIFG P0IFG /* Interrupt flag at source */
  107. /* Joy stick move at P2.0 */
  108. #define HAL_KEY_JOY_MOVE_PORT P2
  109. #define HAL_KEY_JOY_MOVE_BIT BV(0)
  110. #define HAL_KEY_JOY_MOVE_SEL P2SEL
  111. #define HAL_KEY_JOY_MOVE_DIR P2DIR
  112. /* edge interrupt */
  113. #define HAL_KEY_JOY_MOVE_EDGEBIT BV(3)
  114. #define HAL_KEY_JOY_MOVE_EDGE HAL_KEY_FALLING_EDGE
  115. /* Joy move interrupts */
  116. #define HAL_KEY_JOY_MOVE_IEN IEN2 /* CPU interrupt mask register */
  117. #define HAL_KEY_JOY_MOVE_IENBIT BV(1) /* Mask bit for all of Port_2 */
  118. #define HAL_KEY_JOY_MOVE_ICTL P2IEN /* Port Interrupt Control register */
  119. #define HAL_KEY_JOY_MOVE_ICTLBIT BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */
  120. #define HAL_KEY_JOY_MOVE_PXIFG P2IFG /* Interrupt flag at source */
  121. #define HAL_KEY_JOY_CHN HAL_ADC_CHANNEL_6
  122. /**************************************************************************************************
  123. * TYPEDEFS
  124. **************************************************************************************************/
  125. /**************************************************************************************************
  126. * GLOBAL VARIABLES
  127. **************************************************************************************************/
  128. static uint8 halKeySavedKeys; /* used to store previous key state in polling mode */
  129. static halKeyCBack_t pHalKeyProcessFunction;
  130. static uint8 HalKeyConfigured;
  131. bool Hal_KeyIntEnable; /* interrupt enable/disable flag */
  132. /**************************************************************************************************
  133. * FUNCTIONS - Local
  134. **************************************************************************************************/
  135. void halProcessKeyInterrupt(void);
  136. uint8 halGetJoyKeyInput(void);
  137. /**************************************************************************************************
  138. * FUNCTIONS - API
  139. **************************************************************************************************/
  140. /**************************************************************************************************
  141. * @fn HalKeyInit
  142. *
  143. * @brief Initilize Key Service
  144. *
  145. * @param none
  146. *
  147. * @return None
  148. **************************************************************************************************/
  149. void HalKeyInit( void )
  150. {
  151. /* Initialize previous key to 0 */
  152. halKeySavedKeys = 0;
  153. HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT); /* Set pin function to GPIO */
  154. HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT); /* Set pin direction to Input */
  155. HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */
  156. HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input */
  157. /* Initialize callback function */
  158. pHalKeyProcessFunction = NULL;
  159. /* Start with key is not configured */
  160. HalKeyConfigured = FALSE;
  161. }
  162. /**************************************************************************************************
  163. * @fn HalKeyConfig
  164. *
  165. * @brief Configure the Key serivce
  166. *
  167. * @param interruptEnable - TRUE/FALSE, enable/disable interrupt
  168. * cback - pointer to the CallBack function
  169. *
  170. * @return None
  171. **************************************************************************************************/
  172. void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
  173. {
  174. /* Enable/Disable Interrupt or */
  175. Hal_KeyIntEnable = interruptEnable;
  176. /* Register the callback fucntion */
  177. pHalKeyProcessFunction = cback;
  178. /* Determine if interrupt is enable or not */
  179. if (Hal_KeyIntEnable)
  180. {
  181. /* Rising/Falling edge configuratinn */
  182. PICTL &= ~(HAL_KEY_SW_6_EDGEBIT); /* Clear the edge bit */
  183. /* For falling edge, the bit must be set. */
  184. #if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)
  185. PICTL |= HAL_KEY_SW_6_EDGEBIT;
  186. #endif
  187. /* Interrupt configuration:
  188. * - Enable interrupt generation at the port
  189. * - Enable CPU interrupt
  190. * - Clear any pending interrupt
  191. */
  192. HAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;
  193. HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;
  194. HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);
  195. /* Rising/Falling edge configuratinn */
  196. HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT); /* Clear the edge bit */
  197. /* For falling edge, the bit must be set. */
  198. #if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)
  199. HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;
  200. #endif
  201. /* Interrupt configuration:
  202. * - Enable interrupt generation at the port
  203. * - Enable CPU interrupt
  204. * - Clear any pending interrupt
  205. */
  206. HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;
  207. HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;
  208. HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);
  209. /* Do this only after the hal_key is configured - to work with sleep stuff */
  210. if (HalKeyConfigured == TRUE)
  211. {
  212. osal_stop_timerEx(Hal_TaskID, HAL_KEY_EVENT); /* Cancel polling if active */
  213. }
  214. }
  215. else /* Interrupts NOT enabled */
  216. {
  217. HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */
  218. HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT); /* Clear interrupt enable bit */
  219. osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
  220. }
  221. /* Key now is configured */
  222. HalKeyConfigured = TRUE;
  223. }
  224. /**************************************************************************************************
  225. * @fn HalKeyRead
  226. *
  227. * @brief Read the current value of a key
  228. *
  229. * @param None
  230. *
  231. * @return keys - current keys status
  232. **************************************************************************************************/
  233. uint8 HalKeyRead ( void )
  234. {
  235. uint8 keys = 0;
  236. if (HAL_PUSH_BUTTON1())
  237. {
  238. keys |= HAL_KEY_SW_6;
  239. }
  240. if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active low */
  241. {
  242. keys |= halGetJoyKeyInput();
  243. }
  244. return keys;
  245. }
  246. /**************************************************************************************************
  247. * @fn HalKeyPoll
  248. *
  249. * @brief Called by hal_driver to poll the keys
  250. *
  251. * @param None
  252. *
  253. * @return None
  254. **************************************************************************************************/
  255. void HalKeyPoll (void)
  256. {
  257. uint8 keys = 0;
  258. if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT)) /* Key is active HIGH */
  259. {
  260. keys = halGetJoyKeyInput();
  261. }
  262. /* If interrupts are not enabled, previous key status and current key status
  263. * are compared to find out if a key has changed status.
  264. */
  265. if (!Hal_KeyIntEnable)
  266. {
  267. if (keys == halKeySavedKeys)
  268. {
  269. /* Exit - since no keys have changed */
  270. return;
  271. }
  272. /* Store the current keys for comparation next time */
  273. halKeySavedKeys = keys;
  274. }
  275. else
  276. {
  277. /* Key interrupt handled here */
  278. }
  279. if (HAL_PUSH_BUTTON1())
  280. {
  281. keys |= HAL_KEY_SW_6;
  282. }
  283. /* Invoke Callback if new keys were depressed */
  284. if (keys && (pHalKeyProcessFunction))
  285. {
  286. (pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);
  287. }
  288. }
  289. /**************************************************************************************************
  290. * @fn halGetJoyKeyInput
  291. *
  292. * @brief Map the ADC value to its corresponding key.
  293. *
  294. * @param None
  295. *
  296. * @return keys - current joy key status
  297. **************************************************************************************************/
  298. uint8 halGetJoyKeyInput(void)
  299. {
  300. /* The joystick control is encoded as an analog voltage.
  301. * Read the JOY_LEVEL analog value and map it to joy movement.
  302. */
  303. uint8 adc;
  304. uint8 ksave0 = 0;
  305. uint8 ksave1;
  306. /* Keep on reading the ADC until two consecutive key decisions are the same. */
  307. do
  308. {
  309. ksave1 = ksave0; /* save previouse key reading */
  310. adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);
  311. if ((adc >= 2) && (adc <= 38))
  312. {
  313. ksave0 |= HAL_KEY_UP;
  314. }
  315. else if ((adc >= 74) && (adc <= 88))
  316. {
  317. ksave0 |= HAL_KEY_RIGHT;
  318. }
  319. else if ((adc >= 60) && (adc <= 73))
  320. {
  321. ksave0 |= HAL_KEY_LEFT;
  322. }
  323. else if ((adc >= 39) && (adc <= 59))
  324. {
  325. ksave0 |= HAL_KEY_DOWN;
  326. }
  327. else if ((adc >= 89) && (adc <= 100))
  328. {
  329. ksave0 |= HAL_KEY_CENTER;
  330. }
  331. } while (ksave0 != ksave1);
  332. return ksave0;
  333. }
  334. /**************************************************************************************************
  335. * @fn halProcessKeyInterrupt
  336. *
  337. * @brief Checks to see if it's a valid key interrupt, saves interrupt driven key states for
  338. * processing by HalKeyRead(), and debounces keys by scheduling HalKeyRead() 25ms later.
  339. *
  340. * @param
  341. *
  342. * @return
  343. **************************************************************************************************/
  344. void halProcessKeyInterrupt (void)
  345. {
  346. bool valid=FALSE;
  347. if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT) /* Interrupt Flag has been set */
  348. {
  349. HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */
  350. valid = TRUE;
  351. }
  352. if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT) /* Interrupt Flag has been set */
  353. {
  354. HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */
  355. valid = TRUE;
  356. }
  357. if (valid)
  358. {
  359. osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);
  360. }
  361. }
  362. /**************************************************************************************************
  363. * @fn HalKeyEnterSleep
  364. *
  365. * @brief - Get called to enter sleep mode
  366. *
  367. * @param
  368. *
  369. * @return
  370. **************************************************************************************************/
  371. void HalKeyEnterSleep ( void )
  372. {
  373. }
  374. /**************************************************************************************************
  375. * @fn HalKeyExitSleep
  376. *
  377. * @brief - Get called when sleep is over
  378. *
  379. * @param
  380. *
  381. * @return - return saved keys
  382. **************************************************************************************************/
  383. uint8 HalKeyExitSleep ( void )
  384. {
  385. /* Wake up and read keys */
  386. return ( HalKeyRead () );
  387. }
  388. /***************************************************************************************************
  389. * INTERRUPT SERVICE ROUTINE
  390. ***************************************************************************************************/
  391. /**************************************************************************************************
  392. * @fn halKeyPort0Isr
  393. *
  394. * @brief Port0 ISR
  395. *
  396. * @param
  397. *
  398. * @return
  399. **************************************************************************************************/
  400. HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
  401. {
  402. HAL_ENTER_ISR();
  403. if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)
  404. {
  405. halProcessKeyInterrupt();
  406. }
  407. /*
  408. Clear the CPU interrupt flag for Port_0
  409. PxIFG has to be cleared before PxIF
  410. */
  411. HAL_KEY_SW_6_PXIFG = 0;
  412. HAL_KEY_CPU_PORT_0_IF = 0;
  413. CLEAR_SLEEP_MODE();
  414. HAL_EXIT_ISR();
  415. }
  416. /**************************************************************************************************
  417. * @fn halKeyPort2Isr
  418. *
  419. * @brief Port2 ISR
  420. *
  421. * @param
  422. *
  423. * @return
  424. **************************************************************************************************/
  425. HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
  426. {
  427. HAL_ENTER_ISR();
  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. CLEAR_SLEEP_MODE();
  440. HAL_EXIT_ISR();
  441. }
  442. #else
  443. void HalKeyInit(void){}
  444. void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
  445. uint8 HalKeyRead(void){ return 0;}
  446. void HalKeyPoll(void){}
  447. #endif /* HAL_KEY */
  448. /**************************************************************************************************
  449. **************************************************************************************************/