dvk_boardcontrol.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /**************************************************************************//**
  2. * @file
  3. * @brief DVK Peripheral Board Control API implementation
  4. * @author Energy Micro AS
  5. * @version 2.0.1
  6. ******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. * 4. The source and compiled code may only be used on Energy Micro "EFM32"
  21. * microcontrollers and "EFR4" radios.
  22. *
  23. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  24. * obligation to support this Software. Energy Micro AS is providing the
  25. * Software "AS IS", with no express or implied warranties of any kind,
  26. * including, but not limited to, any implied warranties of merchantability
  27. * or fitness for any particular purpose or warranties against infringement
  28. * of any proprietary rights of a third party.
  29. *
  30. * Energy Micro AS will not be liable for any consequential, incidental, or
  31. * special damages, or any other relief, or for any claim by any third party,
  32. * arising from your use of this Software.
  33. *
  34. *****************************************************************************/
  35. /***************************************************************************//**
  36. * @addtogroup BSP
  37. * @{
  38. ******************************************************************************/
  39. #include "efm32.h"
  40. #include "dvk.h"
  41. #include "dvk_boardcontrol.h"
  42. #include "dvk_bcregisters.h"
  43. /**************************************************************************//**
  44. * @brief Enable EFM32 access to periheral on DVK board
  45. * @param peri Peripheral to enable
  46. *****************************************************************************/
  47. void DVK_enablePeripheral(DVKPeripheral peri)
  48. {
  49. uint16_t bit;
  50. uint16_t tmp;
  51. /* Calculate which bit to set */
  52. bit = (uint16_t) peri;
  53. /* Read peripheral control register */
  54. tmp = DVK_readRegister(BC_PERCTRL);
  55. /* Enable peripheral */
  56. tmp |= bit;
  57. /* Special case for RS232, if enabled disable shutdown */
  58. if ((peri == DVK_RS232A) || (peri == DVK_RS232B))
  59. {
  60. /* clear shutdown bit */
  61. tmp &= ~(BC_PERCTRL_RS232_SHUTDOWN);
  62. }
  63. /* Special case for IRDA if enabled disable shutdown */
  64. if (peri == DVK_IRDA)
  65. {
  66. /* clear shutdown bit */
  67. tmp &= ~(BC_PERCTRL_IRDA_SHUTDOWN);
  68. }
  69. DVK_writeRegister(BC_PERCTRL, tmp);
  70. }
  71. /**************************************************************************//**
  72. * @brief Disable EFM32 access to peripheral on DVK board
  73. * @param peri Peripheral to disable
  74. *****************************************************************************/
  75. void DVK_disablePeripheral(DVKPeripheral peri)
  76. {
  77. uint16_t bit;
  78. uint16_t tmp;
  79. /* Calculate which bit to set */
  80. bit = (uint16_t) peri;
  81. /* Read peripheral control register */
  82. tmp = DVK_readRegister(BC_PERCTRL);
  83. /* Disable peripheral */
  84. tmp &= ~(bit);
  85. /* Special case for RS232, if enabled disable shutdown */
  86. if ((peri == DVK_RS232A) || (peri == DVK_RS232B))
  87. {
  88. /* Set shutdown bit */
  89. tmp |= (BC_PERCTRL_RS232_SHUTDOWN);
  90. }
  91. /* Special case for IRDA */
  92. if (peri == DVK_IRDA)
  93. {
  94. /* Set shutdown bit */
  95. tmp |= (BC_PERCTRL_IRDA_SHUTDOWN);
  96. }
  97. DVK_writeRegister(BC_PERCTRL, tmp);
  98. }
  99. /**************************************************************************//**
  100. * @brief Enable BUS access
  101. *****************************************************************************/
  102. void DVK_enableBus(void)
  103. {
  104. /* Enable bus access */
  105. DVK_writeRegister(BC_BUS_CFG, 1);
  106. }
  107. /**************************************************************************//**
  108. * @brief Disable BUS access
  109. *****************************************************************************/
  110. void DVK_disableBus(void)
  111. {
  112. DVK_writeRegister(BC_BUS_CFG, 0);
  113. }
  114. /**************************************************************************//**
  115. * @brief Inform AEM about current energy mode
  116. * @param energyMode What energy mode we are going to use next
  117. *****************************************************************************/
  118. void DVK_setEnergyMode(uint16_t energyMode)
  119. {
  120. DVK_writeRegister(BC_EM, energyMode);
  121. }
  122. /**************************************************************************//**
  123. * @brief Get status of bush buttons
  124. * @return Status of push buttons
  125. *****************************************************************************/
  126. uint16_t DVK_getPushButtons(void)
  127. {
  128. uint16_t pb = 0;
  129. uint16_t aemState;
  130. /* Check state */
  131. aemState = DVK_readRegister(BC_AEMSTATE);
  132. /* Read pushbutton status */
  133. if ( aemState == BC_AEMSTATE_EFM )
  134. {
  135. pb = (~(DVK_readRegister(BC_PUSHBUTTON))) & 0x000f;
  136. }
  137. return pb;
  138. }
  139. /**************************************************************************//**
  140. * @brief Get joystick button status
  141. * @return Joystick controller status
  142. *****************************************************************************/
  143. uint16_t DVK_getJoystick(void)
  144. {
  145. uint16_t joyStick = 0;
  146. uint16_t aemState;
  147. /* Check state */
  148. aemState = DVK_readRegister(BC_AEMSTATE);
  149. /* Read pushbutton status */
  150. if ( aemState == BC_AEMSTATE_EFM )
  151. {
  152. joyStick = (~(DVK_readRegister(BC_JOYSTICK))) & 0x001f;
  153. }
  154. return joyStick;
  155. }
  156. /**************************************************************************//**
  157. * @brief Get dipswitch status
  158. * The DIP switches are free for user programmable purposes
  159. * @return Joystick controller status
  160. *****************************************************************************/
  161. uint16_t DVK_getDipSwitch(void)
  162. {
  163. uint16_t tmp;
  164. tmp = (~(DVK_readRegister(BC_DIPSWITCH))) & 0x00ff;
  165. return tmp;
  166. }
  167. /**************************************************************************//**
  168. * @brief Sets user leds
  169. * @param leds 16-bits which enables or disables the board "User leds"
  170. *****************************************************************************/
  171. void DVK_setLEDs(uint16_t leds)
  172. {
  173. DVK_writeRegister(BC_LED, leds);
  174. }
  175. /**************************************************************************//**
  176. * @brief Get status of user LEDs
  177. * @return Status of 16 user leds, bit 1 = on, bit 0 = off
  178. *****************************************************************************/
  179. uint16_t DVK_getLEDs(void)
  180. {
  181. return DVK_readRegister(BC_LED);
  182. }
  183. /**************************************************************************//**
  184. * @brief Enable "Control" buttons/joystick/dip switch interrupts
  185. * @param flags Board control interrupt flags, BC_INTEN_<something>
  186. *****************************************************************************/
  187. void DVK_enableInterrupt(uint16_t flags)
  188. {
  189. uint16_t tmp;
  190. /* Add flags to interrupt enable register */
  191. tmp = DVK_readRegister(BC_INTEN);
  192. tmp |= flags;
  193. DVK_writeRegister(BC_INTEN, tmp);
  194. }
  195. /**************************************************************************//**
  196. * @brief Disable "Control" buttons/joystick/dip switch interrupts
  197. * @param flags Board control interrupt flags, BC_INTEN_<something>
  198. *****************************************************************************/
  199. void DVK_disableInterrupt(uint16_t flags)
  200. {
  201. uint16_t tmp;
  202. /* Clear flags from interrupt enable register */
  203. tmp = DVK_readRegister(BC_INTEN);
  204. flags = ~(flags);
  205. tmp &= flags;
  206. DVK_writeRegister(BC_INTEN, tmp);
  207. }
  208. /**************************************************************************//**
  209. * @brief Clear interrupts
  210. * @param flags Board control interrupt flags, BC_INTEN_<something>
  211. *****************************************************************************/
  212. void DVK_clearInterruptFlags(uint16_t flags)
  213. {
  214. DVK_writeRegister(BC_INTFLAG, flags);
  215. }
  216. /**************************************************************************//**
  217. * @brief Read interrupt flags
  218. * @return Returns currently triggered interrupts
  219. *****************************************************************************/
  220. uint16_t DVK_getInterruptFlags(void)
  221. {
  222. return DVK_readRegister(BC_INTFLAG);
  223. }
  224. /** @} (end group BSP) */