-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspidevice.cpp
More file actions
217 lines (183 loc) · 6.29 KB
/
spidevice.cpp
File metadata and controls
217 lines (183 loc) · 6.29 KB
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
#include <string.h>
#include "driver/spi_common.h"
#include "spidevice.h"
#include "error_type.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/spi_master.h"
#include "soc/gpio_struct.h"
#include "driver/gpio.h"
#include "system.h"
#include <esp_log.h>
#include "locker.h"
using namespace libesp;
//base spi device
SPIDevice::SPIDevice(SPIBus *b, const spi_device_handle_t &sdh, const spi_device_interface_config_t &devcfg) : MyBus(b), SPIHandle(sdh), DevCfg(devcfg), Debug(false) {
}
bool SPIDevice::init() {
return onInit();
}
ErrorType SPIDevice::shutdown() {
return onShutdown();
}
ErrorType SPIDevice::sendAndReceive(uint8_t out, uint8_t &in) {
return onSendAndReceive(out,in);
}
ErrorType SPIDevice::sendAndReceive(uint8_t *p, int32_t len) {
return onSendAndReceive(p,len);
}
ErrorType SPIDevice::sendAndReceive(uint8_t *out, uint8_t *in, int32_t len) {
return onSendAndReceive(out,in,len, nullptr);
}
ErrorType SPIDevice::sendAndReceive(uint8_t *out, uint8_t *in, int32_t len, void *userData) {
return onSendAndReceive(out,in,len, userData);
}
ErrorType SPIDevice::send(const uint8_t *p, int32_t len) {
return send(p,len, nullptr);
}
ErrorType SPIDevice::send(const uint8_t *p, int32_t len, void *userData) {
return onSend(p,len, userData);
}
SPIDevice::~SPIDevice() {
}
////////////////////////////////
//master
const char *SPIMaster::LOGTAG = "SPIMASTER";
static const int32_t MAX_DMA_LEN = SPI_MAX_DMA_LEN;
ErrorType SPIMaster::onSendAndReceive(uint8_t out, uint8_t &in) {
//ESP_LOGI(LOGTAG,"send and receive same buffer 1 byte");
ErrorType et;
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
t.length=8; //Len is in bytes, transaction length is in bits.
t.tx_data[0]=out; //Data
t.flags = SPI_TRANS_USE_RXDATA | SPI_TRANS_USE_TXDATA;
//ESP_LOGI(LOGTAG,"before spi transmit");
{
MutexLocker ml(MySemaphore, MillisToWait);
if(ml.take()) {
et = spi_device_transmit(SPIHandle, &t); //Transmit!
in = t.rx_data[0];
} else {
et = ErrorType::TIMEOUT_ERROR;
}
}
if (!et.ok()) {
ESP_LOGE(LOGTAG,"%s", et.toString());
}
return et;
}
ErrorType SPIMaster::onSendAndReceive(uint8_t *p, int32_t len) {
//ESP_LOGI(LOGTAG,"send and receive same buffer");
ErrorType et;
if (len==0) return ESP_OK; //no need to send anything
int offset =0;
do {
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
int tx_len = ((len - offset) < MAX_DMA_LEN) ? (len - offset) : MAX_DMA_LEN-1;
t.length=tx_len * 8; //Len is in bytes, transaction length is in bits.
t.tx_buffer= p + offset; //Data
t.rx_buffer= p + offset; //Data
//esp_err_t r = spi_device_polling_transmit(SPIHandle, &t); //Transmit!
{
MutexLocker ml(MySemaphore, MillisToWait);
if(ml.take()) {
et = spi_device_transmit(SPIHandle, &t); //Transmit!
if(!et.ok()) {
break;
}
} else {
et = ErrorType::TIMEOUT_ERROR;
break;
}
}
offset += tx_len; // Add the transmission length to the offset
} while (offset < len);
if (!et.ok()) {
ESP_LOGE(LOGTAG,"%s", et.toString());
}
return et;
}
ErrorType SPIMaster::onSendAndReceive(uint8_t *out, uint8_t *in, int32_t len, void *userData) {
//ESP_LOGI(LOGTAG,"send and receive");
ErrorType et;
if (len==0) return ESP_OK; //no need to send anything
int offset = 0;
do {
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
int tx_len = ((len - offset) < MAX_DMA_LEN) ? (len - offset) : MAX_DMA_LEN-1;
t.length=tx_len * 8; //Len is in bytes, transaction length is in bits.
t.tx_buffer= out + offset; //Data
t.rx_buffer= in + offset; //Data
t.user = userData;
{
MutexLocker ml(MySemaphore, MillisToWait);
if(ml.take()) {
et = spi_device_transmit(SPIHandle, &t); //Transmit!
if(!et.ok()) {
break;
}
} else {
et = ErrorType::TIMEOUT_ERROR;
break;
}
}
offset += tx_len; // Add the transmission length to the offset
} while (offset < len);
if (!et.ok()) {
ESP_LOGE(LOGTAG,"%s", et.toString());
}
return et;
}
ErrorType SPIMaster::onSend(const uint8_t *p, int32_t len, void *userData) {
if(getDebug()) ESP_LOGI(LOGTAG,"send with ptr %p", p);
ErrorType et;
if (len==0) return ESP_OK; //no need to send anything
/*
* On certain MC's the max SPI DMA transfer length might be smaller than the buffer.
* We then have to split the transmissions.
*/
int offset = 0;
do {
spi_transaction_t t;
memset(&t, 0, sizeof(t)); //Zero out the transaction
int tx_len = ((len - offset) < MAX_DMA_LEN) ? (len - offset) : MAX_DMA_LEN-1;
if(getDebug()) {
ESP_LOGI(LOGTAG, "end_len=%d tx_len=%d max = %d offset = %d", len, tx_len, MAX_DMA_LEN, offset);
ESP_LOG_BUFFER_HEX(LOGTAG, p+offset, 6);
}
t.length=tx_len * 8; //Len is in bytes, transaction length is in bits.
t.tx_buffer= p + offset; //Data
t.user=userData;
{
MutexLocker ml(MySemaphore, MillisToWait);
if(ml.take()) {
//et=spi_device_polling_transmit(spi, &t); //Transmit!
et = spi_device_transmit(SPIHandle, &t); //Transmit!
if(!et.ok()) {
break;
}
} else {
et = ErrorType::TIMEOUT_ERROR;
break;
}
offset += tx_len; // Add the transmission length to the offset
}
} while (offset < len);
return et;
}
SPIMaster::~SPIMaster() {
shutdown();
}
ErrorType SPIMaster::onShutdown() {
return getBus()->removeDevice(this);
}
SPIMaster::SPIMaster(SPIBus* s, const spi_device_handle_t &sdh, const spi_device_interface_config_t &devcfg, SemaphoreHandle_t sem)
: SPIDevice(s,sdh,devcfg), MySemaphore(sem), MillisToWait(MILLIS_DEFAULT_WAIT) {
}
bool SPIMaster::onInit() {
return true;
}