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_non_blocking_example Non-blocking SPIM example
44  * @{
45  * @ingroup nrfx_spim_examples
46  *
47  * @brief Example showing the basic functionality of nrfx_spim driver operating in the non-blocking mode.
48  *
49  * @details Application initializes nrfx_spim driver and starts operating in the non-blocking mode.
50  *          Specified message ( @ref MSG_TO_SEND ) from @ref m_tx_buffer is transmitted. When the transfer
51  *          is finished @ref spim_handler() is executed and the 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 handling SPIM driver events.
77  *
78  * @param[in] p_event   Pointer to the SPIM driver event.
79  * @param[in] p_context Pointer to the context passed from the driver.
80  */
spim_handler(nrfx_spim_evt_t const * p_event,void * p_context)81 static void spim_handler(nrfx_spim_evt_t const * p_event, void * p_context)
82 {
83     if (p_event->type == NRFX_SPIM_EVENT_DONE)
84     {
85         char * p_msg = p_context;
86         NRFX_LOG_INFO("SPIM finished. Context passed to the handler: >%s<", p_msg);
87         NRFX_LOG_INFO("Message received: %s", p_event->xfer_desc.p_rx_buffer);
88     }
89 }
90 
91 /**
92  * @brief Function for application main entry.
93  *
94  * @return Nothing.
95  */
main(void)96 int main(void)
97 {
98     nrfx_err_t status;
99     (void)status;
100 
101 #if defined(__ZEPHYR__)
102     IRQ_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPIM_INST_GET(SPIM_INST_IDX)), IRQ_PRIO_LOWEST,
103                 NRFX_SPIM_INST_HANDLER_GET(SPIM_INST_IDX), 0, 0);
104 #endif
105 
106     NRFX_EXAMPLE_LOG_INIT();
107 
108     NRFX_LOG_INFO("Starting nrfx_spim basic non-blocking example.");
109     NRFX_EXAMPLE_LOG_PROCESS();
110 
111     nrfx_spim_t spim_inst = NRFX_SPIM_INSTANCE(SPIM_INST_IDX);
112 
113     nrfx_spim_config_t spim_config = NRFX_SPIM_DEFAULT_CONFIG(SCK_PIN,
114                                                               MOSI_PIN,
115                                                               MISO_PIN,
116                                                               NRF_SPIM_PIN_NOT_CONNECTED);
117 
118     void * p_context = "Some context";
119     status = nrfx_spim_init(&spim_inst, &spim_config, spim_handler, p_context);
120     NRFX_ASSERT(status == NRFX_SUCCESS);
121 
122     nrfx_spim_xfer_desc_t spim_xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buffer, sizeof(m_tx_buffer),
123                                                               m_rx_buffer, sizeof(m_rx_buffer));
124 
125     status = nrfx_spim_xfer(&spim_inst, &spim_xfer_desc, 0);
126     NRFX_ASSERT(status == NRFX_SUCCESS);
127 
128     while (1)
129     {
130         NRFX_EXAMPLE_LOG_PROCESS();
131     }
132 }
133 
134 /** @} */
135