forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erpc_spi_slave_transport.cpp
299 lines (254 loc) · 8.32 KB
/
erpc_spi_slave_transport.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Copyright (c) 2014-2016, Freescale Semiconductor, Inc.
* Copyright 2016-2023 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_spi_slave_transport.hpp"
#include <cstdio>
#include <new>
extern "C" {
#include "board.h"
#include "fsl_gpio.h"
#include "fsl_spi.h"
}
using namespace std;
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
#ifndef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
#define ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN 2U
#define ERPC_BOARD_SPI_SLAVE_READY_MARKER1 0xABU
#define ERPC_BOARD_SPI_SLAVE_READY_MARKER2 0xCDU
#else
#ifndef ERPC_BOARD_SPI_INT_GPIO
#error "Please define the ERPC_BOARD_SPI_INT_GPIO used to notify when the SPI Slave is ready to transmit"
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
static spi_slave_handle_t s_handle;
static volatile bool s_isTransferCompleted = false;
static SpiSlaveTransport *s_spi_slave_instance = NULL;
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
/* @brief Initialize the GPIO used to notify the SPI Master */
static inline void SpiSlaveTransport_NotifyTransferGpioInit(void)
{
gpio_pin_config_t gpioConfig;
gpioConfig.pinDirection = kGPIO_DigitalOutput;
gpioConfig.outputLogic = 1U;
#ifdef ERPC_BOARD_SPI_INT_GPIO_LPC
/* NXP LPC parts with the MCUXpressoSDK LPC GPIO driver */
GPIO_PinInit(ERPC_BOARD_SPI_INT_GPIO, ERPC_BOARD_SPI_INT_PORT, ERPC_BOARD_SPI_INT_PIN, &gpioConfig);
#else
/* NXP Kinetis/iMX parts with the MCUXpressoSDK GPIO driver */
GPIO_PinInit(ERPC_BOARD_SPI_INT_GPIO, ERPC_BOARD_SPI_INT_PIN, &gpioConfig);
#endif
}
/* @brief Notify the SPI Master that the Slave is ready for a new transfer */
static inline void SpiSlaveTransport_NotifyTransferGpioReady(void)
{
#ifdef ERPC_BOARD_SPI_INT_GPIO_LPC
/* NXP LPC parts with the MCUXpressoSDK LPC GPIO driver */
GPIO_PortClear(ERPC_BOARD_SPI_INT_GPIO, ERPC_BOARD_SPI_INT_PORT, 1U << ERPC_BOARD_SPI_INT_PIN);
#else
/* NXP Kinetis/iMX parts with the MCUXpressoSDK GPIO driver */
GPIO_PortClear(ERPC_BOARD_SPI_INT_GPIO, 1U << ERPC_BOARD_SPI_INT_PIN);
#endif
}
/* @brief Notify the SPI Master that the Slave has finished the transfer */
static inline void SpiSlaveTransport_NotifyTransferGpioCompleted(void)
{
#ifdef ERPC_BOARD_SPI_INT_GPIO_LPC
/* NXP LPC parts with the MCUXpressoSDK LPC GPIO driver */
GPIO_PortSet(ERPC_BOARD_SPI_INT_GPIO, ERPC_BOARD_SPI_INT_PORT, 1U << ERPC_BOARD_SPI_INT_PIN);
#else
/* NXP Kinetis/iMX parts with the MCUXpressoSDK GPIO driver */
GPIO_PortSet(ERPC_BOARD_SPI_INT_GPIO, 1U << ERPC_BOARD_SPI_INT_PIN);
#endif
}
#endif
void SpiSlaveTransport::transfer_cb(void)
{
#if ERPC_THREADS
m_txrxSemaphore.putFromISR();
#else
s_isTransferCompleted = true;
#endif
}
static void SPI_SlaveUserCallback(SPI_Type *base, spi_slave_handle_t *handle, status_t status, void *userData)
{
(void)base;
(void)handle;
(void)status;
(void)userData;
SpiSlaveTransport *transport = s_spi_slave_instance;
transport->transfer_cb();
}
SpiSlaveTransport::SpiSlaveTransport(SPI_Type *spiBaseAddr, uint32_t baudRate, uint32_t srcClock_Hz) :
m_spiBaseAddr(spiBaseAddr), m_baudRate(baudRate), m_srcClock_Hz(srcClock_Hz), m_isInited(false)
#if ERPC_THREADS
,
m_txrxSemaphore()
#endif
{
s_spi_slave_instance = this;
}
SpiSlaveTransport::~SpiSlaveTransport(void)
{
if (m_isInited)
{
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioCompleted();
#endif
SPI_Deinit(m_spiBaseAddr);
m_isInited = false;
}
}
erpc_status_t SpiSlaveTransport::init(void)
{
spi_slave_config_t spiConfig;
SPI_SlaveGetDefaultConfig(&spiConfig);
(void)SPI_SlaveInit(m_spiBaseAddr, &spiConfig);
(void)SPI_SlaveTransferCreateHandle(m_spiBaseAddr, &s_handle, SPI_SlaveUserCallback, NULL);
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioInit();
#endif
m_isInited = true;
return kErpcStatus_Success;
}
erpc_status_t SpiSlaveTransport::underlyingReceive(uint8_t *data, uint32_t size)
{
status_t status;
spi_transfer_t slaveXfer = { 0 };
slaveXfer.txData = NULL;
slaveXfer.rxData = data;
slaveXfer.dataSize = size;
s_isTransferCompleted = false;
status = SPI_SlaveTransferNonBlocking(m_spiBaseAddr, &s_handle, &slaveXfer);
if (kStatus_Success == status)
{
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioReady();
#endif
/* wait until the receiving is finished */
#if ERPC_THREADS
m_txrxSemaphore.get();
#else
while (!s_isTransferCompleted)
{
}
#endif
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioCompleted();
#endif
}
return (status != kStatus_Success) ? kErpcStatus_ReceiveFailed : kErpcStatus_Success;
}
erpc_status_t SpiSlaveTransport::underlyingSend(const uint8_t *data, uint32_t size)
{
status_t status;
spi_transfer_t slaveXfer = { 0 };
s_isTransferCompleted = false;
uint32_t header_size = reserveHeaderSize();
/* send the header first */
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
slaveXfer.txData = (uint8_t *)data;
slaveXfer.rxData = NULL;
slaveXfer.dataSize = header_size;
{
#else
uint8_t *spiData = new (nothrow) uint8_t[header_size + ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN];
if (spiData != NULL)
{
spiData[0] = ERPC_BOARD_SPI_SLAVE_READY_MARKER1;
spiData[1] = ERPC_BOARD_SPI_SLAVE_READY_MARKER2;
(void)memcpy(&spiData[ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN], data, header_size);
slaveXfer.txData = spiData;
slaveXfer.rxData = NULL;
slaveXfer.dataSize = header_size + ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN;
#endif
status = SPI_SlaveTransferNonBlocking(m_spiBaseAddr, &s_handle, &slaveXfer);
if (kStatus_Success == status)
{
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioReady();
#endif
/* wait until the sending is finished */
#if ERPC_THREADS
m_txrxSemaphore.get();
#else
while (!s_isTransferCompleted)
{
}
#endif
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioCompleted();
#endif
}
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
}
#else
delete[] spiData;
}
else
{
status = kErpcStatus_SendFailed;
}
#endif
/* send the payload now */
s_isTransferCompleted = false;
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
slaveXfer.txData = (uint8_t *)data + header_size;
slaveXfer.rxData = NULL;
slaveXfer.dataSize = size - header_size;
{
#else
uint8_t *spiData = new (nothrow) uint8_t[size - header_size + ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN];
if (spiData != NULL)
{
spiData[0] = ERPC_BOARD_SPI_SLAVE_READY_MARKER1;
spiData[1] = ERPC_BOARD_SPI_SLAVE_READY_MARKER2;
(void)memcpy(&spiData[ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN], data + header_size, size - header_size);
slaveXfer.txData = spiData;
slaveXfer.rxData = NULL;
slaveXfer.dataSize = size - header_size + ERPC_BOARD_SPI_SLAVE_READY_MARKER_LEN;
#endif
status = SPI_SlaveTransferNonBlocking(m_spiBaseAddr, &s_handle, &slaveXfer);
if (kStatus_Success == status)
{
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioReady();
#endif
/* wait until the sending is finished */
#if ERPC_THREADS
m_txrxSemaphore.get();
#else
while (!s_isTransferCompleted)
{
}
#endif
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpiSlaveTransport_NotifyTransferGpioCompleted();
#endif
}
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
}
#else
delete[] spiData;
}
else
{
status = kErpcStatus_SendFailed;
}
#endif
return (status != kStatus_Success) ? kErpcStatus_SendFailed : kErpcStatus_Success;
}