PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/boards/arm/stm32f0l0g0/nucleo-l073rz/src/stm32_sx127x.c

https://bitbucket.org/zapparello/nuttx
C | 225 lines | 91 code | 53 blank | 81 comment | 5 complexity | 085c095f6863455df229cb9586cd88e4 MD5 | raw file
Possible License(s): Apache-2.0
  1. /****************************************************************************
  2. * boards/arm/stm32f0l0g0/nucleo-l073rz/src/stm32_sx127x.c
  3. * This logic is specific for the RFM98 modules (433MHz)
  4. *
  5. * Copyright (C) 2019 Gregory Nutt. All rights reserved.
  6. * Authors: Mateusz Szafoni <raiden00@railab.me>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdio.h>
  41. #include <stdint.h>
  42. #include <errno.h>
  43. #include <assert.h>
  44. #include <debug.h>
  45. #include <nuttx/board.h>
  46. #include <nuttx/signal.h>
  47. #include <nuttx/wireless/lpwan/sx127x.h>
  48. #include <arch/board/board.h>
  49. #include "stm32_gpio.h"
  50. #include "stm32_exti.h"
  51. #include "stm32_spi.h"
  52. #include "nucleo-l073rz.h"
  53. /****************************************************************************
  54. * Pre-processor Definitions
  55. ****************************************************************************/
  56. /* SX127X on SPI1 bus */
  57. #define SX127X_SPI 1
  58. /****************************************************************************
  59. * Private Function Prototypes
  60. ****************************************************************************/
  61. static void sx127x_chip_reset(void);
  62. static int sx127x_opmode_change(int opmode);
  63. static int sx127x_freq_select(uint32_t freq);
  64. static int sx127x_pa_select(bool enable);
  65. static int sx127x_irq0_attach(xcpt_t isr, FAR void *arg);
  66. /****************************************************************************
  67. * Private Data
  68. ****************************************************************************/
  69. struct sx127x_lower_s lower =
  70. {
  71. .irq0attach = sx127x_irq0_attach,
  72. .reset = sx127x_chip_reset,
  73. .opmode_change = sx127x_opmode_change,
  74. .freq_select = sx127x_freq_select,
  75. .pa_select = sx127x_pa_select,
  76. .pa_force = true
  77. };
  78. /****************************************************************************
  79. * Private Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: sx127x_irq0_attach
  83. ****************************************************************************/
  84. static int sx127x_irq0_attach(xcpt_t isr, FAR void *arg)
  85. {
  86. wlinfo("Attach DIO0 IRQ\n");
  87. /* IRQ on rising edge */
  88. stm32_gpiosetevent(GPIO_SX127X_DIO0, true, false, false, isr, arg);
  89. return OK;
  90. }
  91. /****************************************************************************
  92. * Name: sx127x_chip_reset
  93. ****************************************************************************/
  94. static void sx127x_chip_reset(void)
  95. {
  96. wlinfo("SX127X RESET\n");
  97. /* Configure reset as output */
  98. stm32_configgpio(GPIO_SX127X_RESET | GPIO_OUTPUT | GPIO_SPEED_HIGH |
  99. GPIO_OUTPUT_CLEAR);
  100. /* Set pin to zero */
  101. stm32_gpiowrite(GPIO_SX127X_RESET, false);
  102. /* Wait 1 ms */
  103. nxsig_usleep(1000);
  104. /* Configure reset as input */
  105. stm32_configgpio(GPIO_SX127X_RESET | GPIO_INPUT | GPIO_FLOAT);
  106. /* Wait 10 ms */
  107. nxsig_usleep(10000);
  108. }
  109. /****************************************************************************
  110. * Name: sx127x_opmode_change
  111. ****************************************************************************/
  112. static int sx127x_opmode_change(int opmode)
  113. {
  114. /* Do nothing */
  115. return OK;
  116. }
  117. /****************************************************************************
  118. * Name: sx127x_freq_select
  119. ****************************************************************************/
  120. static int sx127x_freq_select(uint32_t freq)
  121. {
  122. int ret = OK;
  123. /* NOTE: this depends on your module version */
  124. if (freq > SX127X_HFBAND_THR)
  125. {
  126. ret = -EINVAL;
  127. wlerr("HF band not supported\n");
  128. }
  129. return ret;
  130. }
  131. /****************************************************************************
  132. * Name: sx127x_pa_select
  133. ****************************************************************************/
  134. static int sx127x_pa_select(bool enable)
  135. {
  136. int ret = OK;
  137. /* Only PA_BOOST output connected to antenna */
  138. if (enable == false)
  139. {
  140. ret = -EINVAL;
  141. wlerr("Module supports only PA_BOOST pin, so PA_SELECT must be enabled!\n");
  142. }
  143. return ret;
  144. }
  145. /****************************************************************************
  146. * Public Functions
  147. ****************************************************************************/
  148. int stm32_lpwaninitialize(void)
  149. {
  150. FAR struct spi_dev_s *spidev;
  151. int ret = OK;
  152. wlinfo("Register the sx127x module\n");
  153. /* Setup DIO0 */
  154. stm32_configgpio(GPIO_SX127X_DIO0);
  155. /* Init SPI bus */
  156. spidev = stm32_spibus_initialize(SX127X_SPI);
  157. if (!spidev)
  158. {
  159. wlerr("ERROR: Failed to initialize SPI %d bus\n", SX127X_SPI);
  160. ret = -ENODEV;
  161. goto errout;
  162. }
  163. /* Initialize SX127X */
  164. ret = sx127x_register(spidev, &lower);
  165. if (ret < 0)
  166. {
  167. wlerr("ERROR: Failed to register sx127x\n");
  168. goto errout;
  169. }
  170. errout:
  171. return ret;
  172. }