main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "nrf_drv_spi.h"
  41. #include "app_util_platform.h"
  42. #include "nrf_gpio.h"
  43. #include "nrf_delay.h"
  44. #include "boards.h"
  45. #include "app_error.h"
  46. #include <string.h>
  47. #include "nrf_log.h"
  48. #include "nrf_log_ctrl.h"
  49. #include "nrf_log_default_backends.h"
  50. #define SPI_INSTANCE 0 /**< SPI instance index. */
  51. static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
  52. static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
  53. #define TEST_STRING "Nordic"
  54. static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
  55. static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
  56. static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
  57. /**
  58. * @brief SPI user event handler.
  59. * @param event
  60. */
  61. void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
  62. void * p_context)
  63. {
  64. spi_xfer_done = true;
  65. NRF_LOG_INFO("Transfer completed.");
  66. if (m_rx_buf[0] != 0)
  67. {
  68. NRF_LOG_INFO(" Received:");
  69. NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
  70. }
  71. }
  72. int main(void)
  73. {
  74. bsp_board_init(BSP_INIT_LEDS);
  75. APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  76. NRF_LOG_DEFAULT_BACKENDS_INIT();
  77. nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
  78. spi_config.ss_pin = SPI_SS_PIN;
  79. spi_config.miso_pin = SPI_MISO_PIN;
  80. spi_config.mosi_pin = SPI_MOSI_PIN;
  81. spi_config.sck_pin = SPI_SCK_PIN;
  82. APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
  83. NRF_LOG_INFO("SPI example started.");
  84. while (1)
  85. {
  86. // Reset rx buffer and transfer done flag
  87. memset(m_rx_buf, 0, m_length);
  88. spi_xfer_done = false;
  89. APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
  90. while (!spi_xfer_done)
  91. {
  92. __WFE();
  93. }
  94. NRF_LOG_FLUSH();
  95. bsp_board_led_invert(BSP_BOARD_LED_0);
  96. nrf_delay_ms(200);
  97. }
  98. }