antenna.c 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2015 Daniel Campora
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdint.h>
  27. #include "mpconfigboard.h"
  28. #include "inc/hw_types.h"
  29. #include "inc/hw_gpio.h"
  30. #include "inc/hw_ints.h"
  31. #include "inc/hw_memmap.h"
  32. #include "rom_map.h"
  33. #include "pin.h"
  34. #include "prcm.h"
  35. #include "gpio.h"
  36. #include "antenna.h"
  37. #if MICROPY_HW_ANTENNA_DIVERSITY
  38. /******************************************************************************
  39. DEFINE CONSTANTS
  40. ******************************************************************************/
  41. #define REG_PAD_CONFIG_26 (0x4402E108)
  42. #define REG_PAD_CONFIG_27 (0x4402E10C)
  43. /******************************************************************************
  44. DEFINE PRIVATE DATA
  45. ******************************************************************************/
  46. static antenna_type_t antenna_type_selected = ANTENNA_TYPE_INTERNAL;
  47. /******************************************************************************
  48. DEFINE PUBLIC FUNCTIONS
  49. ******************************************************************************/
  50. void antenna_init0(void) {
  51. // enable the peripheral clock and set the gpio direction for
  52. // both antenna 1 and antenna 2 pins
  53. MAP_PRCMPeripheralClkEnable(PRCM_GPIOA3, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK);
  54. MAP_GPIODirModeSet(GPIOA3_BASE, 0x0C, GPIO_DIR_MODE_OUT);
  55. // configure antenna 1 pin type and strength
  56. HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) | (0x00000020 | 0x00000000));
  57. // set the mode
  58. HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~PAD_MODE_MASK) | 0x00000000) & ~(3 << 10);
  59. // set the direction
  60. HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~0xC00) | 0x00000800);
  61. // configure antenna 2 pin type and strength
  62. HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~(PAD_STRENGTH_MASK | PAD_TYPE_MASK)) | (0x00000020 | 0x00000000));
  63. // set the mode
  64. HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~PAD_MODE_MASK) | 0x00000000) & ~(3 << 10);
  65. // set the direction
  66. HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~0xC00) | 0x00000800);
  67. // select the currently active antenna
  68. antenna_select(antenna_type_selected);
  69. }
  70. void antenna_select (antenna_type_t _antenna) {
  71. if (_antenna == ANTENNA_TYPE_INTERNAL) {
  72. MAP_GPIOPinWrite(GPIOA3_BASE, 0x0C, 0x04);
  73. // also configure the pull-up and pull-down accordingly
  74. HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PU;
  75. HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PD;
  76. } else {
  77. MAP_GPIOPinWrite(GPIOA3_BASE, 0x0C, 0x08);
  78. // also configure the pull-up and pull-down accordingly
  79. HWREG(REG_PAD_CONFIG_26) = ((HWREG(REG_PAD_CONFIG_26) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PD;
  80. HWREG(REG_PAD_CONFIG_27) = ((HWREG(REG_PAD_CONFIG_27) & ~PAD_TYPE_MASK)) | PIN_TYPE_STD_PU;
  81. }
  82. antenna_type_selected = _antenna;
  83. }
  84. #endif