Skip to content

Commit e398f5b

Browse files
committed
adding support for MA730, exposing clock_speed and providing additional constructors
1 parent 73961f7 commit e398f5b

File tree

4 files changed

+65
-26
lines changed

4 files changed

+65
-26
lines changed

src/MagneticSensorI2C.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,22 @@ MagneticSensorI2C::MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution,
2222
// extraction masks
2323
lsb_mask = (uint8_t)( (2 << lsb_used) - 1 );
2424
msb_mask = (uint8_t)( (2 << _bits_used_msb) - 1 );
25+
clock_speed = 400000;
26+
sda_pin = SDA;
27+
scl_pin = SCL;
28+
29+
}
30+
31+
MagneticSensorI2C MagneticSensorI2C::AS5600() {
32+
MagneticSensorI2C* sensor = new MagneticSensorI2C(0x36, 12, 0x0E, 4);
33+
return *sensor;
2534
}
2635

2736

2837
void MagneticSensorI2C::init(){
2938

3039
//I2C communication begin
31-
Wire.begin();
40+
Wire.begin(sda_pin, scl_pin, clock_speed);
3241

3342
// velocity calculation init
3443
angle_prev = 0;

src/MagneticSensorI2C.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ class MagneticSensorI2C: public Sensor{
1717
* @param _bits_used_msb number of used bits in msb
1818
*/
1919
MagneticSensorI2C(uint8_t _chip_address, int _bit_resolution, uint8_t _angle_register_msb, int _msb_bits_used);
20-
2120

21+
static MagneticSensorI2C AS5600();
22+
2223
/** sensor initialise pins */
2324
void init();
2425

@@ -41,7 +42,15 @@ class MagneticSensorI2C: public Sensor{
4142
int hasAbsoluteZero();
4243
/** returns 0 maning it doesn't need search for absolute zero */
4344
int needsAbsoluteZeroSearch();
45+
46+
/* the speed of the i2c clock signal */
47+
long clock_speed;
48+
49+
/* the pin used for i2c data */
50+
int sda_pin;
4451

52+
/* the pin used for i2c clock */
53+
int scl_pin;
4554

4655
private:
4756
float cpr; //!< Maximum range of the magnetic sensor

src/MagneticSensorSPI.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,42 @@ MagneticSensorSPI::MagneticSensorSPI(int cs, float _bit_resolution, int _angle_r
1111
angle_register = _angle_register ? _angle_register : DEF_ANGLE_REGISTAR;
1212
// register maximum value (counts per revolution)
1313
cpr = pow(2,_bit_resolution);
14+
spi_mode = SPI_MODE1;
15+
clock_speed = 1000000;
16+
17+
command_parity_bit = 15; // for backwards compatibilty
18+
command_read_bit = 14; // for backwards compatibilty
19+
}
20+
21+
MagneticSensorSPI MagneticSensorSPI::MA730(int cs) {
22+
MagneticSensorSPI* sensor = new MagneticSensorSPI(cs, 14, 0xFFFF);
23+
sensor->spi_mode = SPI_MODE0;
24+
sensor->command_parity_bit = 0; // parity bit insertion not implemented
25+
sensor->command_read_bit = 0; // read flag bit insertion not implemented
26+
return *sensor;
27+
}
1428

29+
MagneticSensorSPI MagneticSensorSPI::AS5147(int cs) {
30+
MagneticSensorSPI* sensor = new MagneticSensorSPI(cs, 14, 0xCFFF);
31+
sensor->spi_mode = SPI_MODE1;
32+
sensor->command_parity_bit = 15;
33+
sensor->command_read_bit = 14;
34+
return *sensor;
1535
}
1636

1737

1838
void MagneticSensorSPI::init(){
1939
// 1MHz clock (AMS should be able to accept up to 10MHz)
20-
settings = SPISettings(1000000, MSBFIRST, SPI_MODE1);
40+
settings = SPISettings(clock_speed, MSBFIRST, spi_mode);
2141

2242
//setup pins
2343
pinMode(chip_select_pin, OUTPUT);
24-
2544

2645
//SPI has an internal SPI-device counter, it is possible to call "begin()" from different devices
2746
SPI.begin();
2847
#ifndef ESP_H // if not ESP32 board
2948
SPI.setBitOrder(MSBFIRST); // Set the SPI_1 bit order
30-
SPI.setDataMode(SPI_MODE1) ;
49+
SPI.setDataMode(spi_mode) ;
3150
SPI.setClockDivider(SPI_CLOCK_DIV8);
3251
#endif
3352

@@ -77,7 +96,7 @@ float MagneticSensorSPI::getVelocity(){
7796
float angle_c = getAngle();
7897
// velocity calculation
7998
float vel = (angle_c - angle_prev)/Ts;
80-
99+
81100
// save variables for future pass
82101
angle_prev = angle_c;
83102
velocity_calc_timestamp = now_us;
@@ -147,35 +166,28 @@ byte MagneticSensorSPI::spiCalcEvenParity(word value){
147166
* Returns the value of the register
148167
*/
149168
word MagneticSensorSPI::read(word angle_register){
150-
word command = 0b0100000000000000; // PAR=0 R/W=R
151-
command = command | angle_register;
152169

153-
//Add a parity bit on the the MSB
154-
command |= ((word)spiCalcEvenParity(command)<<15);
155-
156-
//Split the command into two bytes
157-
byte right_byte = command & 0xFF;
158-
byte left_byte = ( command >> 8 ) & 0xFF;
170+
word command = angle_register;
159171

172+
if (command_read_bit > 0) {
173+
command = angle_register | (1 << command_read_bit);
174+
}
175+
if (command_parity_bit > 0) {
176+
//Add a parity bit on the the MSB
177+
command |= ((word)spiCalcEvenParity(command) << command_parity_bit);
178+
}
160179

161180
#if !defined(_STM32_DEF_) // if not stm chips
162181
//SPI - begin transaction
163182
SPI.beginTransaction(settings);
164183
#endif
165-
//SPI - begin transaction
166-
//SPI.beginTransaction(settings);
167184

168185
//Send the command
169186
digitalWrite(chip_select_pin, LOW);
170-
#ifndef ESP_H // if not ESP32 board
171187
digitalWrite(chip_select_pin, LOW);
172-
#endif
173-
SPI.transfer(left_byte);
174-
SPI.transfer(right_byte);
188+
SPI.transfer16(command);
175189
digitalWrite(chip_select_pin,HIGH);
176-
#ifndef ESP_H // if not ESP32 board
177190
digitalWrite(chip_select_pin,HIGH);
178-
#endif
179191

180192
#if defined( ESP_H ) // if ESP32 board
181193
delayMicroseconds(50);
@@ -186,8 +198,7 @@ word MagneticSensorSPI::read(word angle_register){
186198
//Now read the response
187199
digitalWrite(chip_select_pin, LOW);
188200
digitalWrite(chip_select_pin, LOW);
189-
left_byte = SPI.transfer(0x00);
190-
right_byte = SPI.transfer(0x00);
201+
word register_value = SPI.transfer16(0x00);
191202
digitalWrite(chip_select_pin, HIGH);
192203
digitalWrite(chip_select_pin,HIGH);
193204

@@ -197,7 +208,7 @@ word MagneticSensorSPI::read(word angle_register){
197208
#endif
198209

199210
// Return the data, stripping the parity and error bits
200-
return (( ( left_byte & 0xFF ) << 8 ) | ( right_byte & 0xFF )) & ~0xC000;
211+
return register_value & ~0xC000;
201212
}
202213

203214
/**

src/MagneticSensorSPI.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ class MagneticSensorSPI: public Sensor{
1717
* @param angle_register (optional) angle read register - default 0x3FFF
1818
*/
1919
MagneticSensorSPI(int cs, float bit_resolution, int angle_register = 0);
20-
20+
21+
static MagneticSensorSPI MA730(int cs);
22+
static MagneticSensorSPI AS5147(int cs);
2123

2224
/** sensor initialise pins */
2325
void init();
@@ -41,6 +43,11 @@ class MagneticSensorSPI: public Sensor{
4143
int hasAbsoluteZero();
4244
/** returns 0 maning it doesn't need search for absolute zero */
4345
int needsAbsoluteZeroSearch();
46+
// returns the spi mode (phase/polarity of read/writes) i.e one of SPI_MODE0 | SPI_MODE1 | SPI_MODE2 | SPI_MODE3
47+
int spi_mode;
48+
49+
/* returns the speed of the SPI clock signal */
50+
long clock_speed;
4451

4552

4653
private:
@@ -72,6 +79,9 @@ class MagneticSensorSPI: public Sensor{
7279
// velocity calculation variables
7380
float angle_prev; //!< angle in previous velocity calculation step
7481
long velocity_calc_timestamp; //!< last velocity calculation timestamp
82+
83+
int command_parity_bit; //!< the bit where parity flag is stored in command
84+
int command_read_bit; //!< the bit where read flag is stored in command
7585

7686
};
7787

0 commit comments

Comments
 (0)