-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSpiDispatcher.cpp
175 lines (142 loc) · 5.2 KB
/
SpiDispatcher.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
/*
* This file is part of the Arduino_ThreadsafeIO library.
* Copyright (c) 2021 Arduino SA.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**************************************************************************************
* INCLUDE
**************************************************************************************/
#include "SpiDispatcher.h"
#include <SPI.h>
/**************************************************************************************
* STATIC MEMBER DEFINITION
**************************************************************************************/
SpiDispatcher * SpiDispatcher::_p_instance{nullptr};
rtos::Mutex SpiDispatcher::_mutex;
/**************************************************************************************
* CTOR/DTOR
**************************************************************************************/
SpiDispatcher::SpiDispatcher()
: _thread(osPriorityRealtime, 4096, nullptr, "SpiDispatcher")
, _has_tread_started{false}
, _terminate_thread{false}
{
begin();
}
SpiDispatcher::~SpiDispatcher()
{
end();
}
/**************************************************************************************
* PUBLIC MEMBER FUNCTIONS
**************************************************************************************/
SpiDispatcher & SpiDispatcher::instance()
{
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
if (!_p_instance) {
_p_instance = new SpiDispatcher();
}
return *_p_instance;
}
void SpiDispatcher::destroy()
{
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
delete _p_instance;
_p_instance = nullptr;
}
IoResponse SpiDispatcher::dispatch(IoRequest * req, SpiBusDeviceConfig * config)
{
mbed::ScopedLock<rtos::Mutex> lock(_mutex);
SpiIoTransaction * spi_io_transaction = _spi_io_transaction_mailbox.try_alloc();
if (!spi_io_transaction)
return nullptr;
IoResponse rsp(new impl::IoResponse());
spi_io_transaction->req = req;
spi_io_transaction->rsp = rsp;
spi_io_transaction->config = config;
_spi_io_transaction_mailbox.put(spi_io_transaction);
return rsp;
}
/**************************************************************************************
* PRIVATE MEMBER FUNCTIONS
**************************************************************************************/
void SpiDispatcher::begin()
{
SPI.begin();
_thread.start(mbed::callback(this, &SpiDispatcher::threadFunc)); /* TODO: Check return code */
/* It is necessary to wait until the SpiDispatcher::threadFunc()
* has started, otherwise other threads might trigger IO requests
* before this thread is actually running.
*/
while (!_has_tread_started) { }
}
void SpiDispatcher::end()
{
_terminate_thread = true;
_thread.join(); /* TODO: Check return code */
SPI.end();
}
void SpiDispatcher::threadFunc()
{
_has_tread_started = true;
while(!_terminate_thread)
{
/* Wait blocking for the next IO transaction
* request to be posted to the mailbox.
*/
SpiIoTransaction * spi_io_transaction = _spi_io_transaction_mailbox.try_get_for(rtos::Kernel::wait_for_u32_forever);
if (spi_io_transaction)
{
processSpiIoRequest(spi_io_transaction);
/* Free the allocated memory (memory allocated
* during dispatch(...)
*/
_spi_io_transaction_mailbox.free(spi_io_transaction);
}
}
}
void SpiDispatcher::processSpiIoRequest(SpiIoTransaction * spi_io_transaction)
{
IoRequest * io_request = spi_io_transaction->req;
IoResponse io_response = spi_io_transaction->rsp;
SpiBusDeviceConfig * config = spi_io_transaction->config;
config->select();
config->spi().beginTransaction(config->settings());
/* In a first step transmit the complete write buffer and
* write back the receive data directly into the write buffer
*/
size_t bytes_sent = 0;
for(; bytes_sent < io_request->bytes_to_write; bytes_sent++)
{
uint8_t const tx_byte = io_request->write_buf[bytes_sent];
uint8_t const rx_byte = config->spi().transfer(tx_byte);
io_request->write_buf[bytes_sent] = rx_byte;
}
/* In a second step, transmit the fill symbol and write the
* received data into the read buffer.
*/
size_t bytes_received = 0;
for(; bytes_received < io_request->bytes_to_read; bytes_received++)
{
uint8_t const tx_byte = config->fill_symbol();
uint8_t const rx_byte = config->spi().transfer(tx_byte);
io_request->read_buf[bytes_received] = rx_byte;
}
config->spi().endTransaction();
config->deselect();
io_response->bytes_written = bytes_sent;
io_response->bytes_read = bytes_received;
io_response->done();
}