/boards/arm/stm32/common/src/stm32_nrf24l01.c

https://github.com/apache/incubator-nuttx · C · 155 lines · 57 code · 25 blank · 73 comment · 3 complexity · cd703ce7e2347dfb1fab10ec8d7f0d55 MD5 · raw file

  1. /****************************************************************************
  2. * boards/arm/stm32/common/src/stm32_nrf24l01.c
  3. *
  4. * Copyright (C) 2017 Gregory Nutt. All rights reserved.
  5. * Author: Laurent Latil <laurent@latil.nom.fr>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <stdbool.h>
  41. #include <debug.h>
  42. #include <errno.h>
  43. #include <stdio.h>
  44. #include <string.h>
  45. #include <nuttx/spi/spi.h>
  46. #include <nuttx/wireless/nrf24l01.h>
  47. #include <arch/board/board.h>
  48. #include "arm_arch.h"
  49. #include "chip.h"
  50. #include "stm32.h"
  51. #include "stm32_nrf24l01.h"
  52. /****************************************************************************
  53. * Pre-processor Definitions
  54. ****************************************************************************/
  55. /****************************************************************************
  56. * Private Types
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Private Function Prototypes
  60. ****************************************************************************/
  61. static int nrf24l01_irq_attach(xcpt_t isr, FAR void *arg);
  62. static void nrf24l01_chip_enable(bool enable);
  63. /****************************************************************************
  64. * Private Data
  65. ****************************************************************************/
  66. static FAR struct nrf24l01_config_s nrf_cfg =
  67. {
  68. .irqattach = nrf24l01_irq_attach,
  69. .chipenable = nrf24l01_chip_enable,
  70. };
  71. static xcpt_t g_isr;
  72. static FAR void *g_arg;
  73. /****************************************************************************
  74. * Public Data
  75. ****************************************************************************/
  76. /****************************************************************************
  77. * Private Functions
  78. ****************************************************************************/
  79. static int nrf24l01_irq_attach(xcpt_t isr, FAR void *arg)
  80. {
  81. wlinfo("Attach IRQ\n");
  82. g_isr = isr;
  83. g_arg = arg;
  84. stm32_gpiosetevent(BOARD_NRF24L01_GPIO_IRQ, false, true, false,
  85. g_isr, g_arg);
  86. return OK;
  87. }
  88. static void nrf24l01_chip_enable(bool enable)
  89. {
  90. wlinfo("CE:%d\n", enable);
  91. stm32_gpiowrite(BOARD_NRF24L01_GPIO_CE, enable);
  92. }
  93. /****************************************************************************
  94. * Public Functions
  95. ****************************************************************************/
  96. /****************************************************************************
  97. * Name: board_nrf24l01_initialize
  98. *
  99. * Description:
  100. * Initialize the NRF24L01 wireless module
  101. *
  102. * Input Parameters:
  103. * busno - The SPI bus number
  104. *
  105. * Returned Value:
  106. * Zero (OK) on success; a negated errno value on failure.
  107. *
  108. ****************************************************************************/
  109. int board_nrf24l01_initialize(int busno)
  110. {
  111. FAR struct spi_dev_s *spidev;
  112. int result;
  113. /* Setup CE & IRQ line IOs */
  114. stm32_configgpio(BOARD_NRF24L01_GPIO_CE);
  115. stm32_configgpio(BOARD_NRF24L01_GPIO_IRQ);
  116. /* Init SPI bus */
  117. spidev = stm32_spibus_initialize(busno);
  118. if (!spidev)
  119. {
  120. wlerr("ERROR: Failed to initialize SPI bus\n");
  121. return -ENODEV;
  122. }
  123. result = nrf24l01_register(spidev, &nrf_cfg);
  124. if (result != OK)
  125. {
  126. wlerr("ERROR: Failed to register initialize SPI bus\n");
  127. return -ENODEV;
  128. }
  129. return OK;
  130. }