forked from EmbeddedRPC/erpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erpc_spidev_master_transport.cpp
222 lines (191 loc) · 5.52 KB
/
erpc_spidev_master_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
/*
* Copyright 2020-2021 NXP
* Copyright 2021 ACRIOS Systems s.r.o.
* All rights reserved.
*
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "erpc_spidev_master_transport.hpp"
#include "erpc_message_buffer.hpp"
#include "erpc_spidev.h"
#include "erpc_sysgpio.h"
extern "C" {
#include <unistd.h>
}
using namespace erpc;
////////////////////////////////////////////////////////////////////////////////
// Definitions
////////////////////////////////////////////////////////////////////////////////
#ifndef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
#define ERPC_BOARD_SPI_SLAVE_READY_MARKER1 0xABU
#define ERPC_BOARD_SPI_SLAVE_READY_MARKER2 0xCDU
#else
#ifndef ERPC_BOARD_SPI_INT_PIN
#error "Please define the BOARD_SPI_INT_PIN used to notify when the SPI Slave is ready to transmit"
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// Variables
////////////////////////////////////////////////////////////////////////////////
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
static volatile int s_gpioHandle = 0;
#endif
////////////////////////////////////////////////////////////////////////////////
// Code
////////////////////////////////////////////////////////////////////////////////
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
static inline void SpidevMasterTransport_WaitForSlaveReadyGpio()
{
for (;;)
{
/*
* The GPIO pin has been configured to generate interrupts on edge event
* The poll() will return whenever the interrupt was triggered
*/
if (gpio_poll(s_gpioHandle, -1))
{
break;
}
}
}
#else
static inline void SpidevMasterTransport_WaitForSlaveReadyMarker(int spi_fd)
{
uint8_t detected = 0;
uint8_t data;
for (;;)
{
spidev_transfer(spi_fd, NULL, &data, 1);
if (ERPC_BOARD_SPI_SLAVE_READY_MARKER1 == data)
{
detected = 1;
}
else if (detected && (ERPC_BOARD_SPI_SLAVE_READY_MARKER2 == data))
{
break;
}
else
{
detected = 0;
usleep(100);
}
}
}
#endif
SpidevMasterTransport::SpidevMasterTransport(const char *spidev, uint32_t speed_Hz) :
m_spidevHandle(0), m_spidev(spidev), m_speed_Hz(speed_Hz)
{
}
SpidevMasterTransport::~SpidevMasterTransport(void)
{
if (m_spidevHandle > 0)
{
spidev_close(m_spidevHandle);
}
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
if (s_gpioHandle > 0)
{
gpio_close(s_gpioHandle);
}
#endif
}
erpc_status_t SpidevMasterTransport::init(void)
{
erpc_status_t status = kErpcStatus_Success;
/* Initialize the SPI device */
/* Open SPI device file descriptor */
m_spidevHandle = spidev_open(m_spidev);
if (m_spidevHandle < ERPC_SPIDEV_STATUS_SUCCESS)
{
status = kErpcStatus_InitFailed;
}
/* Set SPI mode to SPI_MODE_0 (CPOL = 0, CPHA = 0) */
if (status == kErpcStatus_Success)
{
if (ERPC_SPIDEV_STATUS_SUCCESS != spidev_set_mode(m_spidevHandle, 0))
{
status = kErpcStatus_InitFailed;
}
}
/* Set SPI default max speed */
if (status == kErpcStatus_Success)
{
if (ERPC_SPIDEV_STATUS_SUCCESS != spidev_set_speed(m_spidevHandle, m_speed_Hz))
{
status = kErpcStatus_InitFailed;
}
}
/* Set SPI device word length */
if (status == kErpcStatus_Success)
{
if ((ERPC_SPIDEV_STATUS_SUCCESS != spidev_set_wordbits(m_spidevHandle, 8)))
{
status = kErpcStatus_InitFailed;
}
}
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
/* Initialize the GPIO SPI_INT_PIN */
/* Export GPIO */
if (status == kErpcStatus_Success)
{
if (ERPC_SYSGPIO_STATUS_SUCCESS != gpio_export(ERPC_BOARD_SPI_INT_PIN))
{
status = kErpcStatus_InitFailed;
}
}
/* Set GPIO direction to input */
if (status == kErpcStatus_Success)
{
if (ERPC_SYSGPIO_STATUS_SUCCESS != gpio_direction(ERPC_BOARD_SPI_INT_PIN, 1))
{
status = kErpcStatus_InitFailed;
}
}
/* Set GPIO edge interrupt trigger */
if (status == kErpcStatus_Success)
{
if (ERPC_SYSGPIO_STATUS_SUCCESS != gpio_set_edge(ERPC_BOARD_SPI_INT_PIN, (char *)"falling"))
{
status = kErpcStatus_InitFailed;
}
}
if (status == kErpcStatus_Success)
{
/* Open GPIO file descriptor */
s_gpioHandle = gpio_open(ERPC_BOARD_SPI_INT_PIN);
if (s_gpioHandle < ERPC_SYSGPIO_STATUS_SUCCESS)
{
status = kErpcStatus_InitFailed;
}
}
#endif
return status;
}
erpc_status_t SpidevMasterTransport::underlyingSend(const uint8_t *data, uint32_t size)
{
erpc_status_t status = kErpcStatus_Success;
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpidevMasterTransport_WaitForSlaveReadyGpio();
#endif
if (ERPC_SPIDEV_STATUS_SUCCESS !=
spidev_transfer(m_spidevHandle, reinterpret_cast<const unsigned char *>(data), NULL, size))
{
status = kErpcStatus_SendFailed;
}
return status;
}
erpc_status_t SpidevMasterTransport::underlyingReceive(uint8_t *data, uint32_t size)
{
erpc_status_t status = kErpcStatus_Success;
#ifdef ERPC_BOARD_SPI_SLAVE_READY_USE_GPIO
SpidevMasterTransport_WaitForSlaveReadyGpio();
#else
SpidevMasterTransport_WaitForSlaveReadyMarker(m_spidevHandle);
#endif
if (ERPC_SPIDEV_STATUS_SUCCESS != spidev_transfer(m_spidevHandle, NULL, data, size))
{
status = kErpcStatus_ReceiveFailed;
}
return status;
}