/
SPI.cpp
292 lines (232 loc) · 5.96 KB
/
SPI.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
/***************************************************************************
* Artekit PropBoard
* https://www.artekit.eu/products/devboards/propboard
*
* Written by Ivan Meleca
* Copyright (c) 2017 Artekit Labs
* https://www.artekit.eu
### SPI.cpp
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
***************************************************************************/
#include "SPI.h"
SPIClass SPI;
SPIClass::SPIClass()
{
callback = NULL;
initialized = false;
irq_mask = 0;
init_mode = SPI_NORMAL;
}
SPIClass::~SPIClass()
{
}
void SPIClass::begin(spiInitMode mode)
{
GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_Cmd(SPI1, DISABLE);
// MOSI = PB5
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_SPI1);
switch (mode)
{
case SPI_NORMAL:
// MISO = PA6
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
/* no break */
case SPI_MOSI_CLK_ONLY:
// CLK = PA5
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
/* no break */
case SPI_MOSI_ONLY:
break;
}
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_Direction = (mode == SPI_NORMAL) ? SPI_Direction_2Lines_FullDuplex : SPI_Direction_1Line_Tx;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_Init(SPI1, &SPI_InitStruct);
SPI_Cmd(SPI1, ENABLE);
}
void SPIClass::begin()
{
if (initialized)
end();
begin(SPI_NORMAL);
init_mode = SPI_NORMAL;
initialized = true;
}
void SPIClass::beginTxOnly(bool with_clock)
{
if (initialized)
end();
init_mode = with_clock ? SPI_MOSI_CLK_ONLY : SPI_MOSI_ONLY;
begin(init_mode);
initialized = true;
}
void SPIClass::end()
{
GPIO_InitTypeDef GPIO_InitStruct;
if (!initialized)
return;
SPI_Cmd(SPI1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, DISABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
switch(init_mode)
{
case SPI_NORMAL:
// MISO
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* no break */
case SPI_MOSI_CLK_ONLY:
// CLK
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOA, &GPIO_InitStruct);
/* no break */
case SPI_MOSI_ONLY:
break;
}
initialized = false;
}
void SPIClass::usingInterrupt(uint8_t interruptNumber)
{
if (interruptNumber > PINS_COUNT)
return;
if (g_APinDescription[interruptNumber].irq_num == NIRQ)
return;
irq_mask |= (1 << g_APinDescription[interruptNumber].irq_num);
}
void SPIClass::notUsingInterrupt(uint8_t interruptNumber)
{
if (interruptNumber > PINS_COUNT)
return;
if (g_APinDescription[interruptNumber].irq_num == NIRQ)
return;
irq_mask &= ~(1 << g_APinDescription[interruptNumber].irq_num);
}
void SPIClass::maskInterrupts()
{
for (uint8_t i = 0; i <= NUM_MAX_EXT_IRQ; i++)
{
if ((irq_mask >> i) & 0x01)
maskInterrupt(i);
}
}
void SPIClass::unmaskInterrupts()
{
for (uint8_t i = 0; i <= NUM_MAX_EXT_IRQ; i++)
{
if ((irq_mask >> i) & 0x01)
unmaskInterrupt(i);
}
}
void SPIClass::beginTransaction(SPISettings settings)
{
if (irq_mask)
maskInterrupts();
SPI1->CR1 &= ~SPI_CR1_SPE;
SPI1->CR1 = settings.config;
SPI1->CR1 |= SPI_CR1_SPE;
}
uint8_t SPIClass::transfer(uint8_t data)
{
return transfer16(data);
}
uint16_t SPIClass::transfer16(uint16_t data)
{
if (!initialized)
return 0;
SPI1->DR = data;
if (init_mode == SPI_NORMAL)
while (!(SPI1->SR & SPI_SR_RXNE));
else
while (!(SPI1->SR & SPI_SR_TXE));
return SPI1->DR;
}
void SPIClass::transfer(void *buf, size_t count)
{
uint8_t* ptr = (uint8_t*) buf;
while (count)
{
transfer(*ptr++);
count--;
}
}
void SPIClass::endTransaction(void)
{
// SPI1->CR1 &= ~SPI_CR1_SPE;
if (irq_mask)
unmaskInterrupts();
}
void SPIClass::setBitOrder(uint8_t bitOrder)
{
if (!initialized)
return;
bool enabled = (SPI1->CR1 & SPI_CR1_SPE);
SPI1->CR1 &= SPI_CR1_SPE;
if (bitOrder == LSBFIRST)
SPI1->CR1 |= SPI_CR1_LSBFIRST;
else
SPI1->CR1 &= ~SPI_CR1_LSBFIRST;
if (enabled)
SPI1->CR1 |= SPI_CR1_SPE;
}
void SPIClass::setDataMode(uint8_t dataMode)
{
if (!initialized)
return;
// Data mode
bool enabled = (SPI1->CR1 & SPI_CR1_SPE);
SPI1->CR1 &= ~(0x03 | SPI_CR1_SPE);
SPI1->CR1 |= dataMode & 0x03;
if (enabled)
SPI1->CR1 |= SPI_CR1_SPE;
}
void SPIClass::setClockDivider(uint8_t clockDiv)
{
if (!initialized)
return;
bool enabled = (SPI1->CR1 & SPI_CR1_SPE);
SPI1->CR1 &= ~(0x38 | SPI_CR1_SPE);
SPI1->CR1 |= clockDiv << 3;
if (enabled)
SPI1->CR1 |= SPI_CR1_SPE;
}
void SPIClass::setCallback(SPICallback* function)
{
if (!initialized)
return;
__disable_irq();
callback = function;
__enable_irq();
}