@@ -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
1838void 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 */
149168word 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/* *
0 commit comments