Skip to content

Commit 3a2d607

Browse files
committed
Merge branch 'spi_2_bus' of git://github.com/owennewo/Arduino-FOC into owennewo-spi_2_bus
2 parents 765a8b9 + 8cab7cd commit 3a2d607

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <SimpleFOC.h>
2+
3+
// MagneticSensorSPI(int cs, float _cpr, int _angle_register)
4+
// config - SPI config
5+
// cs - SPI chip select pin
6+
MagneticSensorSPI sensor = MagneticSensorSPI(AS5147_SPI, PA15);
7+
8+
// these are valid pins (mosi, miso, sclk) for 2nd SPI bus on storm32 board (stm32f107rc)
9+
SPIClass SPI_2(PB15, PB14, PB13);
10+
11+
void setup() {
12+
// monitoring port
13+
Serial.begin(115200);
14+
15+
// initialise magnetic sensor hardware
16+
sensor.init(&SPI_2);
17+
18+
Serial.println("Sensor ready");
19+
_delay(1000);
20+
}
21+
22+
void loop() {
23+
// display the angle and the angular velocity to the terminal
24+
Serial.print(sensor.getAngle());
25+
Serial.print("\t");
26+
Serial.println(sensor.getVelocity());
27+
}

src/sensors/MagneticSensorSPI.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,22 @@ MagneticSensorSPI::MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs){
5959
data_start_bit = config.data_start_bit; // for backwards compatibilty
6060
}
6161

62-
void MagneticSensorSPI::init(){
62+
void MagneticSensorSPI::init(SPIClass* _spi){
63+
64+
spi = _spi;
65+
6366
// 1MHz clock (AMS should be able to accept up to 10MHz)
6467
settings = SPISettings(clock_speed, MSBFIRST, spi_mode);
6568

6669
//setup pins
6770
pinMode(chip_select_pin, OUTPUT);
6871

6972
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
70-
SPI.begin();
73+
spi->begin();
7174
#ifndef ESP_H // if not ESP32 board
72-
SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
73-
SPI.setDataMode(spi_mode) ;
74-
SPI.setClockDivider(SPI_CLOCK_DIV8);
75+
spi->setBitOrder(MSBFIRST); // Set the SPI_1 bit order
76+
spi->setDataMode(spi_mode) ;
77+
spi->setClockDivider(SPI_CLOCK_DIV8);
7578
#endif
7679

7780
digitalWrite(chip_select_pin, HIGH);
@@ -203,13 +206,13 @@ word MagneticSensorSPI::read(word angle_register){
203206

204207
#if !defined(_STM32_DEF_) // if not stm chips
205208
//SPI - begin transaction
206-
SPI.beginTransaction(settings);
209+
spi->beginTransaction(settings);
207210
#endif
208211

209212
//Send the command
210213
digitalWrite(chip_select_pin, LOW);
211214
digitalWrite(chip_select_pin, LOW);
212-
SPI.transfer16(command);
215+
spi->transfer16(command);
213216
digitalWrite(chip_select_pin,HIGH);
214217
digitalWrite(chip_select_pin,HIGH);
215218

@@ -222,13 +225,13 @@ word MagneticSensorSPI::read(word angle_register){
222225
//Now read the response
223226
digitalWrite(chip_select_pin, LOW);
224227
digitalWrite(chip_select_pin, LOW);
225-
word register_value = SPI.transfer16(0x00);
228+
word register_value = spi->transfer16(0x00);
226229
digitalWrite(chip_select_pin, HIGH);
227230
digitalWrite(chip_select_pin,HIGH);
228231

229232
#if !defined(_STM32_DEF_) // if not stm chips
230233
//SPI - end transaction
231-
SPI.endTransaction();
234+
spi->endTransaction();
232235
#endif
233236

234237
register_value = register_value >> (1 + data_start_bit - bit_resolution); //this should shift data to the rightmost bits of the word
@@ -243,5 +246,5 @@ word MagneticSensorSPI::read(word angle_register){
243246
* SPI has an internal SPI-device counter, for each init()-call the close() function must be called exactly 1 time
244247
*/
245248
void MagneticSensorSPI::close(){
246-
SPI.end();
249+
spi->end();
247250
}

src/sensors/MagneticSensorSPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MagneticSensorSPI: public Sensor{
3838
MagneticSensorSPI(MagneticSensorSPIConfig_s config, int cs);
3939

4040
/** sensor initialise pins */
41-
void init();
41+
void init(SPIClass* _spi = &SPI);
4242

4343
// implementation of abstract functions of the Sensor class
4444
/** get current angle (rad) */
@@ -103,6 +103,7 @@ class MagneticSensorSPI: public Sensor{
103103
int command_rw_bit; //!< the bit where read/write flag is stored in command
104104
int data_start_bit; //!< the the position of first bit
105105

106+
SPIClass* spi;
106107
};
107108

108109

0 commit comments

Comments
 (0)