SCL3300 sensor 3-axis inclinometer with angle output and digital SPI interface

Revision:
1:52b1117c65ea
Parent:
0:e8ba98a758d0
--- a/SCL3300.cpp	Mon Aug 15 19:17:10 2022 +0000
+++ b/SCL3300.cpp	Mon Sep 05 15:56:44 2022 +0000
@@ -1,28 +1,46 @@
 /******************************************************************************
 SCL3300.cpp
-SCL3300 Arduino Driver
 David Armstrong
 Version 3.3.0 - September 13, 2021
 https://github.com/DavidArmstrong/SCL3300
 
+MODIFIDER BY METRONICA (METRONIX)
+
 Resources:
 Uses SPI.h for SPI operation
 
 Development environment specifics:
-Arduino IDE 1.8.15
+MBED OS
 
 This code is released under the [MIT License](http://opensource.org/licenses/MIT).
 Please review the LICENSE.md file included with this example.
 Distributed as-is; no warranty is given.
 
+
 ******************************************************************************/
 
 // include this library's description file
 #include "SCL3300.h"
 
+SCL3300::SCL3300(PinName mosi, PinName miso, PinName sclk, PinName scl3300_csPin) : _spi(mosi, miso, sclk), _scl3300_csPin(scl3300_csPin) {
+    _scl3300_csPin = 1;
+
+    // Set default to and 4MHz for data transfer
+    _transfer_sck = 4000000;
+    
+    modeCMD[0]  =  0; 
+    modeCMD[1] = ChgMode1;
+    modeCMD[2] = ChgMode2;
+    modeCMD[3] = ChgMode3;
+    modeCMD[4] = ChgMode4;
+    
+    scl3300_mode=4;
+}
+
+
 // Public Methods //////////////////////////////////////////////////////////
 // Set the sensor mode to the number provided as modeNum.
-boolean SCL3300::setMode(int modeNum) {
+bool SCL3300::setMode(int modeNum) {
   // Set Sensor mode - If not called, the default is mode 4, as set in header file
   // Only allowed values are: 1,2,3,4
   if (modeNum > 0 && modeNum < 5) {
@@ -39,30 +57,34 @@
 }
 
 // Current Version of begin() to initialize the library and the SCL3300
-boolean SCL3300::begin(void) {
+bool SCL3300::begin(void) {
   //This is the updated Version 3 begin function
-  // Determine if we need to set up to use the default SPI interface, or some other one
-  if (_spiPort == nullptr) _spiPort = &SPI;
-  
+
   //Wait the required 1 ms before initializing the SCL3300 inclinomenter
-  unsigned long startmillis = millis();
-  while (millis() - startmillis < 1) ;
+  timer2.start();
+  unsigned long startmillis = timer2.read_ms();
+  while (timer2.read_ms() - startmillis < 1) ;
+  timer2.stop();
   
   initSPI();    // Initialize SPI Library
   if (!setFastRead) beginTransmission(); //Set up this SPI port/bus
   //Write SW Reset command
   transfer(SwtchBnk0);
   transfer(SWreset);
-  startmillis = millis();
-  while (millis() - startmillis < 1) ;
+  timer2.start();
+  startmillis = timer2.read_ms();
+  while (timer2.read_ms() - startmillis < 1) ;
+  timer2.stop();
   //Set measurement mode
   transfer(modeCMD[scl3300_mode]); //Set mode on hardware
   //We're good, so Enable angle outputs
   transfer(EnaAngOut);
   //The first response after reset is undefined and shall be discarded
   //wait 5 ms to stablize
-  startmillis = millis();
-  while (millis() - startmillis < 100) ;
+  timer2.start();
+  startmillis = timer2.read_ms();
+  while (timer2.read_ms() - startmillis < 100) ;
+  timer2.stop();
 
   //Read Status to clear the status summary
   transfer(RdStatSum);
@@ -81,22 +103,9 @@
   return (SCL3300_DATA == 0xc1); //Let the caller know if this worked
 }
 
-// Set up the SPI communication with the SCL3300 with provided Chip Select pin number, and provided SPI port
-boolean SCL3300::begin(SPIClass &spiPort, uint8_t csPin) {
-  scl3300_csPin = csPin;
-  _spiPort = &spiPort; //Grab the port the user wants us to use
-  return begin();
-} // begin
-
-// Set up the SPI communication with the SCL3300 with provided Chip Select pin number
-boolean SCL3300::begin(uint8_t csPin) {
-  scl3300_csPin = csPin;
-  _spiPort = &SPI; // With this call, we do the default SPI interface
-  return begin();
-} // begin
 
 //Check to validate that the sensor is still reachable and ready to provide data
-boolean SCL3300::isConnected() {
+bool SCL3300::isConnected() {
   if (!setFastRead) beginTransmission(); //Set up this SPI port/bus
   transfer(SwtchBnk0);
   //Read the WHOAMI register
@@ -112,9 +121,9 @@
 
 //Read all the sensor data together to keep it consistent
 //This is required according to the datasheet
-boolean SCL3300::available(void) {
+bool SCL3300::available(void) {
   //Version 3 of this function
-  boolean errorflag = false;
+  bool errorflag = false;
   //Read all Sensor Data, as per Datasheet requirements
   if (!setFastRead) beginTransmission(); //Set up this SPI port/bus
   transfer(SwtchBnk0);
@@ -262,7 +271,7 @@
 // Read the sensor Serial Number as created by the manufacturer
 unsigned long SCL3300::getSerialNumber(void) {
   //Return Device Serial number
-  boolean errorflag = false;
+  bool errorflag = false;
   unsigned long serialNum = 0;
   if (!setFastRead) beginTransmission(); //Set up this SPI port/bus
   transfer(SwtchBnk1);
@@ -353,26 +362,26 @@
 //private functions for serial transmission
 // Begin SPI bus transmission to the device
 void SCL3300::beginTransmission() {
-  _spiPort->beginTransaction(spiSettings);
+   _scl3300_csPin=0;
 } //beginTransmission
 
 // End SPI bus transmission to the device
 void SCL3300::endTransmission() {
   // take the chip/slave select high to de-select:
-  digitalWrite(scl3300_csPin, HIGH);
-  _spiPort->endTransaction();
-  unsigned long startmillis = millis();
-  while (millis() - startmillis < 1) ; //wait a bit
+  _scl3300_csPin=1;
+ timer2.start();
+  unsigned long startmillis = timer2.read_ms();
+  while (timer2.read_ms() - startmillis < 1) ; //wait a bit
+  timer2.stop();
 } //endTransmission
 
 //Initialize the Arduino SPI library for the SCL3300 hardware
 void SCL3300::initSPI() {
   //Initialize the Arduino SPI library for the SCL3300 hardware
-  _spiPort->begin();
+  _spi.frequency(_transfer_sck);
   // Maximum SPI frequency is 2 MHz - 4 MHz to achieve the best performance
   // initialize the chip select pin:
-  pinMode(scl3300_csPin, OUTPUT);
-  digitalWrite(scl3300_csPin, HIGH);
+  _scl3300_csPin=1;
   // Data is read and written MSb first.
   // Data is captured on rising edge of clock (CPHA = 0)
   // Data is propagated on the falling edge (MISO line) of the SCK. (CPOL = 0)
@@ -416,34 +425,35 @@
   
   dataorig.bit32 = value; //Allow 32 bit value to be sent 8 bits at a time
   #ifdef debug_scl3300
-  Serial_SCL.print(dataorig.bit32, HEX);
-  Serial_SCL.print(" ");
+  _spi.print(dataorig.bit32, HEX);
+  _spi.print(" ");
   for (int j = 3; j >= 0; j--) {
-    Serial_SCL.print(dataorig.bit8[j], HEX);
-    Serial_SCL.print(" ");
+    _spi.print(dataorig.bit8[j], HEX);
+    _spi.print(" ");
   }
   #endif
   //Must allow at least 10 uSec between SPI transfers
   //The datasheet shows the CS line must be high during this time
-  if (!setFastRead) startmicros = micros();
+  timer1.start();
+  if (!setFastRead) startmicros = timer1.read_us();
   //while ((micros() - startmicros < 10) && (micros() > 10)) ;
-  if (!setFastRead) while ((micros() - startmicros < 10)) ;
-  
-  digitalWrite(scl3300_csPin, LOW); //Now chip select can be enabled for the full 32 bit xfer
+  if (!setFastRead) while ((timer1.read_us() - startmicros < 10)) ;
+  timer1.stop();
+  _scl3300_csPin=0; //Now chip select can be enabled for the full 32 bit xfer
   SCL3300_DATA = 0;
   for (int i = 3; i >= 0; i--) { //Xfers are done MSB first
-    dataorig.bit8[i] = _spiPort->transfer(dataorig.bit8[i]);
+    dataorig.bit8[i] = transfer(dataorig.bit8[i]);
   }
   SCL3300_DATA = dataorig.bit8[1] + (dataorig.bit8[2] << 8);
   SCL3300_CRC = dataorig.bit8[0];
   SCL3300_CMD = dataorig.bit8[3];
-  digitalWrite(scl3300_csPin, HIGH); //And we are done
+  _scl3300_csPin=1; //And we are done
   #ifdef debug_scl3300
   for (int i = 3; i >= 0; i--) {
-    Serial_SCL.print(" ");
-    Serial_SCL.print(dataorig.bit8[i], HEX);
+    _spi.print(" ");
+    _spi.print(dataorig.bit8[i], HEX);
   }
-  Serial_SCL.print("  ");
+  _spi.print("  ");
   #endif
   if (SCL3300_CRC == CalculateCRC(dataorig.bit32))
     crcerr = false;
@@ -455,15 +465,15 @@
   else
     statuserr = true;
   #ifdef debug_scl3300
-  Serial_SCL.print((SCL3300_CMD & 0x03));
-  Serial_SCL.print(" ");
-  Serial_SCL.print(SCL3300_DATA, HEX);
-  Serial_SCL.print(" ");
-  Serial_SCL.print(SCL3300_CRC, HEX);
-  Serial_SCL.print(" ");
-  Serial_SCL.print(CalculateCRC(dataorig.bit32), HEX);
-  Serial_SCL.print(" ");
-  Serial_SCL.println(crcerr);
+  _spi.print((SCL3300_CMD & 0x03));
+  _spi.print(" ");
+  _spi.print(SCL3300_DATA, HEX);
+  _spi.print(" ");
+  _spi.print(SCL3300_CRC, HEX);
+  _spi.print(" ");
+  _spi.print(CalculateCRC(dataorig.bit32), HEX);
+  _spi.print(" ");
+  _spi.println(crcerr);
   #endif
   return dataorig.bit32;
 }