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