-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspibus.cpp
More file actions
83 lines (70 loc) · 2.01 KB
/
spibus.cpp
File metadata and controls
83 lines (70 loc) · 2.01 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
#include <driver/spi_common.h>
#include "spibus.h"
#include "spidevice.h"
#include <algorithm>
using namespace libesp;
static SPIBus * ESP32SPIBuses[3] = {0};
//statics
ErrorType SPIBus::initializeBus(const spi_host_device_t &shd, const spi_bus_config_t &buscfg, int dmachannel) {
esp_err_t ret;
ErrorType et = shutdown(shd);
if(et.ok()) {
ret=::spi_bus_initialize(shd, &buscfg, dmachannel);
if(ret!=ESP_OK) return ErrorType(ret);
ESP32SPIBuses[shd] = new SPIBus(shd, buscfg, dmachannel);
return ErrorType(ret);
}
return et;
}
SPIBus *SPIBus::get(const spi_host_device_t &shd) {
return ESP32SPIBuses[shd];
}
ErrorType SPIBus::shutdown(const spi_host_device_t &shd) {
ErrorType et;
if(ESP32SPIBuses[shd]!=0) {
et = ESP32SPIBuses[shd]->shutdown();
if(et.ok()) {
ESP32SPIBuses[shd] = 0;
}
}
return et;
}
//non-statics
SPIBus::SPIBus(const spi_host_device_t &shd, const spi_bus_config_t &buscfg, int dma) :
SPIBusID(shd), BusConfig(buscfg), DMAChannel(dma) {
}
ErrorType SPIBus::removeDevice(SPIDevice *d) {
esp_err_t ret = spi_bus_remove_device(d->getDeviceHandle());
if(ret==ESP_OK) {
ATTACHED_DEVICE_TYPE_IT it = std::find(AttachedDevices.begin(),AttachedDevices.end(),d);
if(it!=AttachedDevices.end()) {
AttachedDevices.erase(it);
}
}
return ErrorType(ret);
}
ErrorType SPIBus::shutdown() {
ATTACHED_DEVICE_TYPE_IT it = AttachedDevices.begin();
for(;it!=AttachedDevices.end();++it) {
removeDevice((*it));
delete (*it);
}
AttachedDevices.clear();
return spi_bus_free(SPIBusID);
}
SPIBus::~SPIBus() {
shutdown();
}
SPIDevice *SPIBus::createMasterDevice(const spi_device_interface_config_t &devcfg) {
return createMasterDevice(devcfg,nullptr);
}
SPIDevice *SPIBus::createMasterDevice(const spi_device_interface_config_t &devcfg, SemaphoreHandle_t spiSemaphore) {
spi_device_handle_t spi;
esp_err_t ret=spi_bus_add_device(SPIBusID, &devcfg, &spi);
if(ret==ESP_OK) {
SPIMaster *s = new SPIMaster(this,spi,devcfg, spiSemaphore);
AttachedDevices.push_back(s);
return s;
}
return nullptr;
}