main.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Copyright (c) 2017 - 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 "nrfx_spim.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 NRFX_SPIM_SCK_PIN 3
  51. #define NRFX_SPIM_MOSI_PIN 4
  52. #define NRFX_SPIM_MISO_PIN 28
  53. #define NRFX_SPIM_SS_PIN 29
  54. #define NRFX_SPIM_DCX_PIN 30
  55. #define SPI_INSTANCE 3 /**< SPI instance index. */
  56. static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
  57. static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
  58. #define TEST_STRING "Nordic123456789012345678901234567890"
  59. static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
  60. static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
  61. static const uint8_t m_length = sizeof(m_tx_buf); /**< Transfer length. */
  62. void spim_event_handler(nrfx_spim_evt_t const * p_event,
  63. void * p_context)
  64. {
  65. spi_xfer_done = true;
  66. NRF_LOG_INFO("Transfer completed.");
  67. if (m_rx_buf[0] != 0)
  68. {
  69. NRF_LOG_INFO(" Received:");
  70. NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
  71. }
  72. }
  73. int main(void)
  74. {
  75. bsp_board_init(BSP_INIT_LEDS);
  76. APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  77. NRF_LOG_DEFAULT_BACKENDS_INIT();
  78. nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf, m_length);
  79. nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
  80. spi_config.frequency = NRF_SPIM_FREQ_1M;
  81. spi_config.ss_pin = NRFX_SPIM_SS_PIN;
  82. spi_config.miso_pin = NRFX_SPIM_MISO_PIN;
  83. spi_config.mosi_pin = NRFX_SPIM_MOSI_PIN;
  84. spi_config.sck_pin = NRFX_SPIM_SCK_PIN;
  85. spi_config.dcx_pin = NRFX_SPIM_DCX_PIN;
  86. spi_config.use_hw_ss = true;
  87. spi_config.ss_active_high = false;
  88. APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));
  89. NRF_LOG_INFO("NRFX SPIM example started.");
  90. while (1)
  91. {
  92. // Reset rx buffer and transfer done flag
  93. memset(m_rx_buf, 0, m_length);
  94. spi_xfer_done = false;
  95. APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));
  96. while (!spi_xfer_done)
  97. {
  98. __WFE();
  99. }
  100. NRF_LOG_FLUSH();
  101. bsp_board_led_invert(BSP_BOARD_LED_0);
  102. nrf_delay_ms(200);
  103. }
  104. }