Skip to content

Commit 2e13a1d

Browse files
Initial attempt at SPI setModule - but it doesn't seem to work for SPI2 etc, even when not using setModule to select the SPI channel, when I tested with a VS1053 board attached to SPI2 and SD on SPI1
1 parent cd9e17d commit 2e13a1d

2 files changed

Lines changed: 74 additions & 84 deletions

File tree

STM32F1/libraries/SPI/src/SPI.cpp

Lines changed: 61 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -91,30 +91,29 @@ static const spi_pins board_spi_pins[] __FLASH__ = {
9191
*/
9292

9393
SPIClass::SPIClass(uint32 spi_num) {
94+
95+
_currentSetting=&_settings[spi_num];
9496
switch (spi_num) {
9597
#if BOARD_NR_SPI >= 1
9698
case 1:
97-
this->spi_d = SPI1;
99+
_currentSetting->spi_d = SPI1;
98100
break;
99101
#endif
100102
#if BOARD_NR_SPI >= 2
101103
case 2:
102-
this->spi_d = SPI2;
104+
_currentSetting->spi_d = SPI2;
103105
break;
104106
#endif
105107
#if BOARD_NR_SPI >= 3
106108
case 3:
107-
this->spi_d = SPI3;
109+
_currentSetting->spi_d = SPI3;
108110
break;
109111
#endif
110112
default:
111113
ASSERT(0);
112114
}
113-
114-
bitOrder=MSBFIRST;
115-
//pinMode(BOARD_SPI_DEFAULT_SS,OUTPUT);
116-
117115

116+
//pinMode(BOARD_SPI_DEFAULT_SS,OUTPUT);
118117
}
119118

120119
/*
@@ -123,45 +122,45 @@ SPIClass::SPIClass(uint32 spi_num) {
123122

124123
void SPIClass::begin(void) {
125124

126-
uint32 flags = ((bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE | SPI_SOFT_SS);
127-
spi_init(spi_d);
128-
configure_gpios(spi_d, 1);
125+
uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE | SPI_SOFT_SS);
126+
spi_init(_currentSetting->spi_d);
127+
configure_gpios(_currentSetting->spi_d, 1);
129128
#ifdef SPI_DEBUG
130-
Serial.print("spi_master_enable("); Serial.print(clockDivider); Serial.print(","); Serial.print(dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
129+
Serial.print("spi_master_enable("); Serial.print(_currentSetting->clock); Serial.print(","); Serial.print(_currentSetting->dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
131130
#endif
132-
spi_master_enable(spi_d, (spi_baud_rate)clockDivider, (spi_mode)dataMode, flags);
131+
spi_master_enable(_currentSetting->spi_d, (spi_baud_rate)_currentSetting->clock, (spi_mode)_currentSetting->dataMode, flags);
133132
}
134133

135134
void SPIClass::beginSlave(void) {
136-
if (dataMode >= 4) {
135+
if (_currentSetting->dataMode >= 4) {
137136
ASSERT(0);
138137
return;
139138
}
140-
uint32 flags = ((bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE);
141-
spi_init(spi_d);
142-
configure_gpios(spi_d, 0);
139+
uint32 flags = ((_currentSetting->bitOrder == MSBFIRST ? SPI_FRAME_MSB : SPI_FRAME_LSB) | SPI_DFF_8_BIT | SPI_SW_SLAVE);
140+
spi_init(_currentSetting->spi_d);
141+
configure_gpios(_currentSetting->spi_d, 0);
143142
#ifdef SPI_DEBUG
144-
Serial.print("spi_slave_enable("); Serial.print(dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
143+
Serial.print("spi_slave_enable("); Serial.print(_currentSetting->dataMode); Serial.print(","); Serial.print(flags); Serial.println(")");
145144
#endif
146-
spi_slave_enable(spi_d, (spi_mode)dataMode, flags);
145+
spi_slave_enable(_currentSetting->spi_d, (spi_mode)_currentSetting->dataMode, flags);
147146
}
148147

149148
void SPIClass::end(void) {
150-
if (!spi_is_enabled(this->spi_d)) {
149+
if (!spi_is_enabled(_currentSetting->spi_d)) {
151150
return;
152151
}
153152

154153
// Follows RM0008's sequence for disabling a SPI in master/slave
155154
// full duplex mode.
156-
while (spi_is_rx_nonempty(this->spi_d)) {
155+
while (spi_is_rx_nonempty(_currentSetting->spi_d)) {
157156
// FIXME [0.1.0] remove this once you have an interrupt based driver
158-
volatile uint16 rx __attribute__((unused)) = spi_rx_reg(this->spi_d);
157+
volatile uint16 rx __attribute__((unused)) = spi_rx_reg(_currentSetting->spi_d);
159158
}
160-
while (!spi_is_tx_empty(this->spi_d))
159+
while (!spi_is_tx_empty(_currentSetting->spi_d))
161160
;
162-
while (spi_is_busy(this->spi_d))
161+
while (spi_is_busy(_currentSetting->spi_d))
163162
;
164-
spi_peripheral_disable(this->spi_d);
163+
spi_peripheral_disable(_currentSetting->spi_d);
165164
}
166165

167166
/* Roger Clark added 3 functions */
@@ -170,7 +169,7 @@ void SPIClass::setClockDivider(uint32_t clockDivider)
170169
#ifdef SPI_DEBUG
171170
Serial.print("Clock divider set to "); Serial.println(clockDivider);
172171
#endif
173-
this->clockDivider = clockDivider;
172+
_currentSetting->clock = clockDivider;
174173
this->begin();
175174
}
176175

@@ -179,7 +178,7 @@ void SPIClass::setBitOrder(BitOrder bitOrder)
179178
#ifdef SPI_DEBUG
180179
Serial.print("Bit order set to "); Serial.println(bitOrder);
181180
#endif
182-
this->bitOrder = bitOrder;
181+
_currentSetting->bitOrder = bitOrder;
183182
this->begin();
184183
}
185184

@@ -189,11 +188,11 @@ void SPIClass::setBitOrder(BitOrder bitOrder)
189188
*/
190189
void SPIClass::setDataSize(uint32 datasize)
191190
{
192-
uint32 cr1 = this->spi_d->regs->CR1;
191+
uint32 cr1 = _currentSetting->spi_d->regs->CR1;
193192
datasize &= SPI_CR1_DFF;
194193
cr1 &= ~(SPI_CR1_DFF);
195194
cr1 |= datasize;
196-
this->spi_d->regs->CR1 = cr1;
195+
_currentSetting->spi_d->regs->CR1 = cr1;
197196
}
198197

199198
void SPIClass::setDataMode(uint8_t dataMode)
@@ -228,7 +227,7 @@ If someone finds this is not the case or sees a logic error with this let me kno
228227
#ifdef SPI_DEBUG
229228
Serial.print("Data mode set to "); Serial.println(dataMode);
230229
#endif
231-
this->dataMode = dataMode;
230+
_currentSetting->dataMode = dataMode;
232231
this->begin();
233232
}
234233

@@ -243,29 +242,9 @@ void SPIClass::beginTransaction(uint8_t pin, SPISettings settings)
243242
//digitalWrite(_SSPin,LOW);
244243
setBitOrder(settings.bitOrder);
245244
setDataMode(settings.dataMode);
246-
setClockDivider(determine_baud_rate(spi_d, settings.clock));
245+
setClockDivider(determine_baud_rate(_currentSetting->spi_d, settings.clock));
247246
begin();
248-
#if 0
249-
// code from SAM core
250-
uint8_t mode = interruptMode;
251-
if (mode > 0) {
252-
if (mode < 16) {
253-
if (mode & 1) PIOA->PIO_IDR = interruptMask[0];
254-
if (mode & 2) PIOB->PIO_IDR = interruptMask[1];
255-
if (mode & 4) PIOC->PIO_IDR = interruptMask[2];
256-
if (mode & 8) PIOD->PIO_IDR = interruptMask[3];
257-
} else {
258-
interruptSave = interruptsStatus();
259-
noInterrupts();
260-
}
261-
}
262-
uint32_t ch = BOARD_PIN_TO_SPI_CHANNEL(pin);
263-
bitOrder[ch] = settings.border;
264-
SPI_ConfigureNPCS(spi, ch, settings.config);
265-
//setBitOrder(pin, settings.border);
266-
//setDataMode(pin, settings.datamode);
267-
//setClockDivider(pin, settings.clockdiv);
268-
#endif
247+
269248
}
270249

271250
void SPIClass::endTransaction(void)
@@ -304,9 +283,9 @@ uint8 SPIClass::read(void) {
304283
void SPIClass::read(uint8 *buf, uint32 len) {
305284
uint32 rxed = 0;
306285
while (rxed < len) {
307-
while (!spi_is_rx_nonempty(this->spi_d))
286+
while (!spi_is_rx_nonempty(_currentSetting->spi_d))
308287
;
309-
buf[rxed++] = (uint8)spi_rx_reg(this->spi_d);
288+
buf[rxed++] = (uint8)spi_rx_reg(_currentSetting->spi_d);
310289
}
311290
}
312291

@@ -319,9 +298,9 @@ void SPIClass::write(uint16 data) {
319298
* This almost doubles the speed of this function.
320299
*/
321300

322-
spi_tx_reg(this->spi_d, data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
323-
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
324-
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
301+
spi_tx_reg(_currentSetting->spi_d, data); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
302+
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
303+
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
325304
}
326305

327306
//void SPIClass::write(uint8 byte) {
@@ -333,27 +312,27 @@ void SPIClass::write(uint16 data) {
333312
* This almost doubles the speed of this function.
334313
*/
335314

336-
// spi_tx_reg(this->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
337-
// while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
338-
// while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
315+
// spi_tx_reg(_currentSetting->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
316+
// while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
317+
// while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
339318
//}
340319

341320
void SPIClass::write(const uint8 *data, uint32 length) {
342321
uint32 txed = 0;
343322
while (txed < length) {
344-
txed += spi_tx(this->spi_d, data + txed, length - txed);
323+
txed += spi_tx(_currentSetting->spi_d, data + txed, length - txed);
345324
}
346-
while (spi_is_tx_empty(this->spi_d) == 0); // "4. After writing the last data item into the SPI_DR register, wait until TXE=1 ..."
347-
while (spi_is_busy(this->spi_d) != 0); // "... then wait until BSY=0, this indicates that the transmission of the last data is complete."
325+
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "4. After writing the last data item into the SPI_DR register, wait until TXE=1 ..."
326+
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... then wait until BSY=0, this indicates that the transmission of the last data is complete."
348327
}
349328

350329
uint8 SPIClass::transfer(uint8 byte) const {
351330
uint8 b;
352-
spi_tx_reg(this->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
353-
while (spi_is_rx_nonempty(this->spi_d) == 0); // "4. Wait until RXNE=1 ..."
354-
b = spi_rx_reg(this->spi_d); // "... and read the last received data."
355-
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
356-
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
331+
spi_tx_reg(_currentSetting->spi_d, byte); // "2. Write the first data item to be transmitted into the SPI_DR register (this clears the TXE flag)."
332+
while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..."
333+
b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
334+
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
335+
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
357336
return b;
358337
}
359338
/* Roger Clark and Victor Perez, 2015
@@ -365,7 +344,7 @@ uint8 SPIClass::transfer(uint8 byte) const {
365344
uint8 SPIClass::dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length) {
366345
if (length == 0) return 0;
367346
uint8 b;
368-
if (spi_is_rx_nonempty(this->spi_d) == 1) b = spi_rx_reg(this->spi_d); //Clear the RX buffer in case a byte is waiting on it.
347+
if (spi_is_rx_nonempty(_currentSetting->spi_d) == 1) b = spi_rx_reg(_currentSetting->spi_d); //Clear the RX buffer in case a byte is waiting on it.
369348
dma1_ch3_Active=true;
370349
dma_init(DMA1);
371350
dma_attach_interrupt(DMA1, DMA_CH3, &SPIClass::DMA1_CH3_Event);
@@ -405,8 +384,8 @@ uint8 SPIClass::dmaTransfer(uint8 *transmitBuf, uint8 *receiveBuf, uint16 length
405384
}
406385

407386
// }
408-
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
409-
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
387+
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
388+
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
410389
dma_disable(DMA1, DMA_CH3);
411390
dma_disable(DMA1, DMA_CH2);
412391
spi_rx_dma_disable(SPI1);
@@ -435,10 +414,10 @@ uint8 SPIClass::dmaSend(uint8 *transmitBuf, uint16 length, bool minc) {
435414
dma_enable(DMA1, DMA_CH3);// enable transmit
436415

437416
while (dma1_ch3_Active);
438-
while (spi_is_rx_nonempty(this->spi_d) == 0); // "4. Wait until RXNE=1 ..."
439-
b = spi_rx_reg(this->spi_d); // "... and read the last received data."
440-
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
441-
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
417+
while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..."
418+
b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
419+
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
420+
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
442421
dma_disable(DMA1, DMA_CH3);
443422
spi_tx_dma_disable(SPI1);
444423
return b;
@@ -460,10 +439,10 @@ uint8 SPIClass::dmaSend(uint16 *transmitBuf, uint16 length, bool minc) {
460439
dma_enable(DMA1, DMA_CH3);// enable transmit
461440

462441
while (dma1_ch3_Active);
463-
while (spi_is_rx_nonempty(this->spi_d) == 0); // "4. Wait until RXNE=1 ..."
464-
b = spi_rx_reg(this->spi_d); // "... and read the last received data."
465-
while (spi_is_tx_empty(this->spi_d) == 0); // "5. Wait until TXE=1 ..."
466-
while (spi_is_busy(this->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
442+
while (spi_is_rx_nonempty(_currentSetting->spi_d) == 0); // "4. Wait until RXNE=1 ..."
443+
b = spi_rx_reg(_currentSetting->spi_d); // "... and read the last received data."
444+
while (spi_is_tx_empty(_currentSetting->spi_d) == 0); // "5. Wait until TXE=1 ..."
445+
while (spi_is_busy(_currentSetting->spi_d) != 0); // "... and then wait until BSY=0 before disabling the SPI."
467446
dma_disable(DMA1, DMA_CH3);
468447
spi_tx_dma_disable(SPI1);
469448
return b;
@@ -483,19 +462,19 @@ void SPIClass::detachInterrupt(void) {
483462
*/
484463

485464
uint8 SPIClass::misoPin(void) {
486-
return dev_to_spi_pins(this->spi_d)->miso;
465+
return dev_to_spi_pins(_currentSetting->spi_d)->miso;
487466
}
488467

489468
uint8 SPIClass::mosiPin(void) {
490-
return dev_to_spi_pins(this->spi_d)->mosi;
469+
return dev_to_spi_pins(_currentSetting->spi_d)->mosi;
491470
}
492471

493472
uint8 SPIClass::sckPin(void) {
494-
return dev_to_spi_pins(this->spi_d)->sck;
473+
return dev_to_spi_pins(_currentSetting->spi_d)->sck;
495474
}
496475

497476
uint8 SPIClass::nssPin(void) {
498-
return dev_to_spi_pins(this->spi_d)->nss;
477+
return dev_to_spi_pins(_currentSetting->spi_d)->nss;
499478
}
500479

501480
/*

STM32F1/libraries/SPI/src/SPI.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ class SPISettings {
118118
uint32_t clock;
119119
BitOrder bitOrder;
120120
uint8_t dataMode;
121+
122+
spi_dev *spi_d;
123+
uint8_t _SSPin;
124+
//uint32_t clockDivider;
125+
121126
friend class SPIClass;
122127
};
123128

@@ -304,7 +309,7 @@ class SPIClass {
304309
* @brief Get a pointer to the underlying libmaple spi_dev for
305310
* this HardwareSPI instance.
306311
*/
307-
spi_dev* c_dev(void) { return this->spi_d; }
312+
spi_dev* c_dev(void) { return _currentSetting->spi_d; }
308313

309314
/* -- The following methods are deprecated --------------------------- */
310315

@@ -338,7 +343,7 @@ class SPIClass {
338343
*/
339344
uint8 recv(void);
340345

341-
spi_dev *dev(){ return spi_d;}
346+
spi_dev *dev(){ return _currentSetting->spi_d;}
342347

343348

344349

@@ -351,11 +356,17 @@ class SPIClass {
351356

352357
// To Do. Need to wait for
353358
}
359+
360+
SPISettings _settings[BOARD_NR_SPI];
361+
SPISettings *_currentSetting;
362+
363+
/*
354364
spi_dev *spi_d;
355365
uint8_t _SSPin;
356366
uint32_t clockDivider;
357367
uint8_t dataMode;
358368
BitOrder bitOrder;
369+
*/
359370
};
360371

361372

0 commit comments

Comments
 (0)