hal_key.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**************************************************************************************************
  2. Filename: hal_key.c
  3. Revised: $Date:$
  4. Revision: $Revision:$
  5. Description: This file contains the interface to the H/W Key driver.
  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. * Includes
  35. * ------------------------------------------------------------------------------------------------
  36. */
  37. #include "hal_board.h"
  38. #include "hal_drivers.h"
  39. #include "hal_key.h"
  40. #include "hal_types.h"
  41. #include "osal.h"
  42. #include "usb_interrupt.h"
  43. /* ------------------------------------------------------------------------------------------------
  44. * Macros
  45. * ------------------------------------------------------------------------------------------------
  46. */
  47. #define HAL_KEY_CLR_INT() \
  48. st ( \
  49. /* PxIFG has to be cleared before PxIF. */\
  50. P1IFG = 0; \
  51. P1IF = 0; \
  52. )
  53. /* ------------------------------------------------------------------------------------------------
  54. * Constants
  55. * ------------------------------------------------------------------------------------------------
  56. */
  57. /* ------------------------------------------------------------------------------------------------
  58. * Typedefs
  59. * ------------------------------------------------------------------------------------------------
  60. */
  61. /* ------------------------------------------------------------------------------------------------
  62. * Global Variables
  63. * ------------------------------------------------------------------------------------------------
  64. */
  65. uint8 Hal_KeyIntEnable;
  66. /* ------------------------------------------------------------------------------------------------
  67. * Global Functions
  68. * ------------------------------------------------------------------------------------------------
  69. */
  70. /* ------------------------------------------------------------------------------------------------
  71. * Local Variables
  72. * ------------------------------------------------------------------------------------------------
  73. */
  74. static halKeyCBack_t pHalKeyProcessFunction;
  75. static volatile uint8 isrKeys;
  76. static uint8 halKeys;
  77. /* ------------------------------------------------------------------------------------------------
  78. * Local Functions
  79. * ------------------------------------------------------------------------------------------------
  80. */
  81. /**************************************************************************************************
  82. * @fn HalKeyInit
  83. *
  84. * @brief This function is called by HalDriverInit to initialize the H/W keys.
  85. *
  86. * input parameters
  87. *
  88. * None.
  89. *
  90. * output parameters
  91. *
  92. * None.
  93. *
  94. * @return None.
  95. **************************************************************************************************
  96. */
  97. void HalKeyInit(void)
  98. {
  99. }
  100. /**************************************************************************************************
  101. * @fn HalKeyConfig
  102. *
  103. * @brief This function is called by HalDriverInit to initialize the H/W keys.
  104. *
  105. * input parameters
  106. *
  107. * @param interruptEnable - TRUE/FALSE to enable the key interrupt.
  108. * @param cback - The callback function for the key change event.
  109. *
  110. * output parameters
  111. *
  112. * None.
  113. *
  114. * @return None.
  115. **************************************************************************************************
  116. */
  117. void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback)
  118. {
  119. if ((Hal_KeyIntEnable = interruptEnable))
  120. {
  121. HAL_KEY_CLR_INT(); // Clear spurious ints.
  122. PICTL |= 0x01; // P1ICONL: Falling edge ints on pins 0-3.
  123. P1IEN |= PUSH1_BV | PUSH2_BV; // Enable specific P1 bits for ints by bit mask.
  124. IEN2 |= 0x10; // Enable general P1 interrupts.
  125. }
  126. else
  127. {
  128. (void)osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
  129. }
  130. pHalKeyProcessFunction = cback;
  131. }
  132. /**************************************************************************************************
  133. * @fn HalKeyPoll
  134. *
  135. * @brief This function is called by Hal_ProcessEvent() on a HAL_KEY_EVENT.
  136. *
  137. * input parameters
  138. *
  139. * None.
  140. *
  141. * output parameters
  142. *
  143. * None.
  144. *
  145. * @return None.
  146. **************************************************************************************************
  147. */
  148. void HalKeyPoll(void)
  149. {
  150. uint8 newKeys;
  151. if (Hal_KeyIntEnable)
  152. {
  153. halIntState_t intState;
  154. HAL_ENTER_CRITICAL_SECTION(intState);
  155. newKeys = isrKeys;
  156. isrKeys = 0;
  157. HAL_EXIT_CRITICAL_SECTION(intState);
  158. }
  159. else
  160. {
  161. uint8 keys = HalKeyRead();
  162. newKeys = (halKeys ^ keys) & keys;
  163. halKeys = keys;
  164. }
  165. if (newKeys && pHalKeyProcessFunction)
  166. {
  167. (pHalKeyProcessFunction)(newKeys, HAL_KEY_STATE_NORMAL);
  168. }
  169. }
  170. /**************************************************************************************************
  171. * @fn HalKeyRead
  172. *
  173. * @brief This function is called anywhere.
  174. *
  175. * input parameters
  176. *
  177. * None.
  178. *
  179. * output parameters
  180. *
  181. * None.
  182. *
  183. * @return The bit mask of all keys pressed.
  184. **************************************************************************************************
  185. */
  186. uint8 HalKeyRead(void)
  187. {
  188. uint8 keys = 0;
  189. if (HAL_PUSH_BUTTON1())
  190. {
  191. keys |= HAL_KEY_SW_1;
  192. }
  193. if (HAL_PUSH_BUTTON2())
  194. {
  195. keys |= HAL_KEY_SW_2;
  196. }
  197. return keys;
  198. }
  199. /**************************************************************************************************
  200. * @fn HalKeyEnterSleep
  201. *
  202. * @brief - Get called to enter sleep mode
  203. *
  204. * @param
  205. *
  206. * @return
  207. **************************************************************************************************/
  208. void HalKeyEnterSleep ( void )
  209. {
  210. }
  211. /**************************************************************************************************
  212. * @fn HalKeyExitSleep
  213. *
  214. * @brief - Get called when sleep is over
  215. *
  216. * @param
  217. *
  218. * @return - return saved keys
  219. **************************************************************************************************/
  220. uint8 HalKeyExitSleep ( void )
  221. {
  222. /* Wake up and read keys */
  223. return ( HalKeyRead () );
  224. }
  225. /**************************************************************************************************
  226. * @fn usbKeyISR
  227. *
  228. * @brief This function is the ISR for the Port2 USB/Key interrupt.
  229. *
  230. * input parameters
  231. *
  232. * None.
  233. *
  234. * output parameters
  235. *
  236. * None.
  237. *
  238. * @return None.
  239. **************************************************************************************************
  240. */
  241. HAL_ISR_FUNCTION( usbKeyISR, P1INT_VECTOR )
  242. {
  243. HAL_ENTER_ISR();
  244. if (P1IFG & PUSH1_BV)
  245. {
  246. isrKeys |= HAL_KEY_SW_1;
  247. }
  248. if (P1IFG & PUSH2_BV)
  249. {
  250. isrKeys |= HAL_KEY_SW_2;
  251. }
  252. HAL_KEY_CLR_INT();
  253. (void)osal_set_event(Hal_TaskID, HAL_KEY_EVENT);
  254. HAL_EXIT_ISR();
  255. }
  256. /**************************************************************************************************
  257. */