1 /*
2 * Copyright (c) 2022 - 2024, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the copyright holder nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <nrfx_example.h>
35 #include <nrfx_spim.h>
36
37 #define NRFX_LOG_MODULE EXAMPLE
38 #define NRFX_EXAMPLE_CONFIG_LOG_ENABLED 1
39 #define NRFX_EXAMPLE_CONFIG_LOG_LEVEL 3
40 #include <nrfx_log.h>
41
42 /**
43 * @defgroup nrfx_spim_blocking_example Blocking SPIM example
44 * @{
45 * @ingroup nrfx_spim_examples
46 *
47 * @brief Example showing the basic functionality of nrfx_spim driver operating in the blocking mode.
48 *
49 * @details Application initializes nrfx_spim driver and starts operating in the blocking mode.
50 * Specified message ( @ref MSG_TO_SEND ) from @ref m_tx_buffer is transmitted and when
51 * the transfer is finished received message is read from @ref m_rx_buffer.
52 */
53
54 /** @brief Symbol specifying SPIM instance to be used. */
55 #define SPIM_INST_IDX 1
56
57 /** @brief Symbol specifying pin number for MOSI. */
58 #define MOSI_PIN LOOPBACK_PIN_1A
59
60 /** @brief Symbol specifying pin number for MISO. */
61 #define MISO_PIN LOOPBACK_PIN_1B
62
63 /** @brief Symbol specifying pin number for SCK. */
64 #define SCK_PIN LOOPBACK_PIN_2A
65
66 /** @brief Symbol specifying message to be sent via SPIM data transfer. */
67 #define MSG_TO_SEND "Nordic Semiconductor"
68
69 /** @brief Transmit buffer initialized with the specified message ( @ref MSG_TO_SEND ). */
70 static uint8_t m_tx_buffer[] = MSG_TO_SEND;
71
72 /** @brief Receive buffer defined with the size to store specified message ( @ref MSG_TO_SEND ). */
73 static uint8_t m_rx_buffer[sizeof(MSG_TO_SEND)];
74
75 /**
76 * @brief Function for application main entry.
77 *
78 * @return Nothing.
79 */
main(void)80 int main(void)
81 {
82 nrfx_err_t status;
83 (void)status;
84
85 NRFX_EXAMPLE_LOG_INIT();
86
87 NRFX_LOG_INFO("Starting nrfx_spim basic blocking example.");
88 NRFX_EXAMPLE_LOG_PROCESS();
89
90 nrfx_spim_t spim_inst = NRFX_SPIM_INSTANCE(SPIM_INST_IDX);
91
92 nrfx_spim_config_t spim_config = NRFX_SPIM_DEFAULT_CONFIG(SCK_PIN,
93 MOSI_PIN,
94 MISO_PIN,
95 NRF_SPIM_PIN_NOT_CONNECTED);
96
97 status = nrfx_spim_init(&spim_inst, &spim_config, NULL, NULL);
98 NRFX_ASSERT(status == NRFX_SUCCESS);
99
100 nrfx_spim_xfer_desc_t spim_xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buffer, sizeof(m_tx_buffer),
101 m_rx_buffer, sizeof(m_rx_buffer));
102
103 status = nrfx_spim_xfer(&spim_inst, &spim_xfer_desc, 0);
104 NRFX_ASSERT(status == NRFX_SUCCESS);
105
106 NRFX_LOG_INFO("Message received: %s", m_rx_buffer);
107 NRFX_EXAMPLE_LOG_PROCESS();
108
109 while (1)
110 {
111 NRFX_EXAMPLE_LOG_PROCESS();
112 }
113 }
114 /** @} */
115