Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 10 of 10

Thread: SPI Problem: Teensy 4.1 with SparkFun Qwiic KX13x library

  1. #1
    Junior Member
    Join Date
    Nov 2021
    Posts
    4

    SPI Problem: Teensy 4.1 with SparkFun Qwiic KX13x library

    Hello together,

    i want to use the SparkFun Qwiic KX13x library in combination with a Teensy 4.1. In a first test i didnt connect the Sensor board just to check if the library is compatible with the Teensy.
    I use newest versions available for Arduino IDE (1.8.16),Teensyduino and the Sparkfun library(1.0.6).

    The Example code for i2c interface is running (Serial Monitor shows "Could not communicate with the the KX13X. Freezing.", what is expected because i did not connect a Sensor).

    Code:
    #include <Wire.h>
    #include "SparkFun_Qwiic_KX13X.h"
    
    QwiicKX132 kxAccel;
    
    void setup() {
      while(!Serial){
        delay(50);
      }
      Serial.begin(115200);
      Serial.println("Welcome.");
    
      Wire.begin();
      if( !kxAccel.begin() ){
        Serial.println("Could not communicate with the the KX13X. Freezing.");
        while(1);
      }
      else
        Serial.println("Ready.");
        
      if( !kxAccel.initialize(DEFAULT_SETTINGS)){ // Loading default settings.
        Serial.println("Could not initialize the chip.");
        while(1);
      }
      else
        Serial.println("Initialized...");
    }
    In the next step i modified the programm to run with SPI Interface:
    Code:
    #include <SPI.h>
    #include "SparkFun_Qwiic_KX13X.h"
    QwiicKX132 kxAccel;
    const int CS = 10;
    
    void setup() {
      while(!Serial){
        delay(50);
      }
      Serial.begin(115200);
      Serial.println("Welcome.");
    
      SPI.begin();
      if( !kxAccel.beginSPI(CS, 1000000, SPI) ){
        Serial.println("Could not communicate with the the KX13X. Freezing.");
        while(1);
      }
      else
        Serial.println("Ready.");
        
      if( !kxAccel.initialize(DEFAULT_SETTINGS)){ // Loading default settings.
        Serial.println("Could not initialize the chip.");
        while(1);
      }
      else
        Serial.println("Initialized...");
    }
    The result is that the Serial monitor only shows "Welcome.", but nothing more. After some seconds the Teensy restarts. So i guess the beginSPI function seems not to work.
    I tested the same code withArduino Mega and there it seems to work (Serial Monitor shows "Could not communicate with the the KX13X. Freezing.").

    I found the beginSPI (bool beginSPI(uint*_t, uint32_t spiPortSpeed = 10000000, SPIClass &spiPort = SPI); - Start communication with the KX13x via SPI.) function at https://learn.sparkfun.com/tutorials...rduino-library

    Anyone has an idea what the issue could be or some experience with the Sparkfun KX13x library in combination with Teensy?

    Thank you in advance and best regards!

  2. #2
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,755
    I am assuming you have the 132 version: https://www.sparkfun.com/products/17871 ?

    Do youve pictures showing your setup? Or do you not have it setup yet?

  3. #3
    Junior Member
    Join Date
    Nov 2021
    Posts
    4
    yes exactly, i use the KX132 version. I dont have pictures yet, because if didnt set it up yet.

    I am just trying to setup the library on the Teensy.

  4. #4
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,755
    It looks like it may be faulting in the SPI library.

    Added to code: if (CrashReport) Serial.print(CrashReport);
    Code:
    CrashReport:
      A problem occurred at (system time) 12:56:35
      Code was executing from address 0x51C
      CFSR: 82
    	(DACCVIOL) Data Access Violation
    	(MMARVALID) Accessed Address: 0x13 (nullptr)
    	  Check code at 0x51C - very likely a bug!
    	  Run "addr2line -e mysketch.ino.elf 0x51C" for filename & line number.
      Temperature inside the chip was 38.61 C
      Startup CPU clock speed is 600MHz
      Reboot was caused by auto reboot after fault or bad interrupt detected
    Code:
    C:\Users\kurte\AppData\Local\Temp\arduino_build_967293>addr2line -e yyy.ino.elf 0x51c
    C:\arduino-1.8.16\hardware\teensy\avr\libraries\SPI/SPI.h:1168
    Code:
    	void beginTransaction(SPISettings settings) {
    		if (interruptMasksUsed) {
    1168 is the if statement.

    Note when I build this function, there are a lot of compiler warnings in their library:
    Code:
    C:\\arduino-1.8.16\\hardware\\teensy/../tools/arm/bin/arm-none-eabi-g++" -c -O2 -g -Wall -ffunction-sections -fdata-sections -nostdlib -MMD -std=gnu++14 -fno-exceptions -fpermissive -fno-rtti -fno-threadsafe-statics -felide-constructors -Wno-error=narrowing -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 -D__IMXRT1062__ -DTEENSYDUINO=156 -DARDUINO=10816 -DARDUINO_TEENSY41 -DF_CPU=600000000 -DUSB_SERIAL -DLAYOUT_US_ENGLISH "-IC:\\Users\\kurte\\AppData\\Local\\Temp\\arduino_build_967293/pch" "-IC:\\arduino-1.8.16\\hardware\\teensy\\avr\\cores\\teensy4" "-IC:\\arduino-1.8.16\\hardware\\teensy\\avr\\libraries\\SPI" "-IC:\\Users\\kurte\\Documents\\Arduino\\libraries\\SparkFun_KX13X_Arduino_Library\\src" "-IC:\\Users\\kurte\\Documents\\Arduino\\libraries\\Wire" "C:\\Users\\kurte\\Documents\\Arduino\\libraries\\SparkFun_KX13X_Arduino_Library\\src\\SparkFun_Qwiic_KX13X.cpp" -o "C:\\Users\\kurte\\AppData\\Local\\Temp\\arduino_build_967293\\libraries\\SparkFun_KX13X_Arduino_Library\\SparkFun_Qwiic_KX13X.cpp.o"
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::setRange(uint8_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:136:17: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       if( range < 0 | range > 3)
                     ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::setOutputDataRate(uint8_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:159:16: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       if( rate < 0 | rate > 15 )
                    ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::setInterruptPin(bool, uint8_t, uint8_t, bool)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:200:44: warning: comparison of constant '4' with boolean expression is always false [-Wbool-compare]
       else if( latchControl < 0 | latchControl > 4 )
                                                ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:200:29: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       else if( latchControl < 0 | latchControl > 4 )
                                 ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::routeHardwareInterrupt(uint8_t, uint8_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:223:15: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       if( rdr < 0 | rdr > 128 )
                   ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::setBufferThreshold(uint8_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:290:17: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       if( threshold < 2 | threshold > 171 )
                     ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:303:15: warning: statement has no effect [-Wunused-value]
         threshold == 86; 
                   ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::setBufferOperation(uint8_t, uint8_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:319:22: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       if( resolution < 0 | resolution > 1 )
                          ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:321:25: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
       if( operationMode < 0 | operationMode > 2 )
                             ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'KX13X_STATUS_t QwiicKX13xCore::readMultipleRegisters(uint8_t, uint8_t*, int16_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:449:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(size_t i = 1; i < numBytes; i++) {
                           ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:474:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
       for(size_t i = 0; i < numBytes; i++) {
                           ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'outputData QwiicKX132::getAccelData()':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:593:11: warning: unused variable 'tempRegVal' [-Wunused-variable]
       uint8_t tempRegVal;
               ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:594:18: warning: unused variable 'returnError' [-Wunused-variable]
       KX13X_STATUS_t returnError;
                      ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'outputData QwiicKX134::getAccelData()':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:682:11: warning: unused variable 'tempRegVal' [-Wunused-variable]
       uint8_t tempRegVal;
               ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:683:18: warning: unused variable 'returnError' [-Wunused-variable]
       KX13X_STATUS_t returnError;
                      ^
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp: In member function 'bool QwiicKX13xCore::initialize(uint8_t)':
    C:\Users\kurte\Documents\Arduino\libraries\SparkFun_KX13X_Arduino_Library\src\SparkFun_Qwiic_KX13X.cpp:97:3: warning: 'returnError' may be used uninitialized in this function [-Wmaybe-uninitialized]
       if( returnError == KX13X_SUCCESS )
       ^
    Not sure yet if any of these might be involved...

  5. #5
    Junior Member
    Join Date
    Nov 2021
    Posts
    4
    First of all: Thank you for your effort!

    Do you think it would be an alternative to set up the SPI interface manually?
    As far as i understood its mainly a problem with the beginSPI Function. So maybe the rest of the library could work like intended on the teensy?!

  6. #6
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,755
    Quote Originally Posted by adafruit333 View Post
    First of all: Thank you for your effort!

    Do you think it would be an alternative to set up the SPI interface manually?
    As far as i understood its mainly a problem with the beginSPI Function. So maybe the rest of the library could work like intended on the teensy?!
    Don't know yet... I need to isolate it down farther. If sparkfun library issue, may make changes and try seeing if they will take a pull request...

    Ordered one of these from Amazon, which should be here next week.

  7. #7
    Senior Member
    Join Date
    Jul 2020
    Posts
    1,368
    Code:
      if( range < 0 | range > 3)
    Clearly '||' or 'or' should be used here, its terrible style to use '|' for logical or and in many circumstances would be
    plain wrong.
    For instance:
    Code:
      if (pointer == NULL | pointer->field == 0)
        ....
    is broken (deferences NULL) whereas
    Code:
      if (pointer == NULL || pointer->field == 0)
        ....
    isn't.

    However this warning probably is indicative of a bug rather than style issues:
    Code:
    warning: 'returnError' may be used uninitialized in this function [-Wmaybe-uninitialized]

  8. #8
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,755
    @MarkT and ...

    Yep! Lots of issues with the code.

    I raised a new issue up on github: https://github.com/sparkfun/SparkFun...rary/issues/10

    But I believe the biggest issue. is their code works like: _spiPort->beginTransaction(kxSPISettings);

    But they never set spiPort...

    The code will run longer and then hang in the sketch files did not init (which is correct as I dont have one) If yo uedit the cpp file and add at about line 38:
    Code:
    uint8_t QwiicKX13xCore::beginSPICore(uint8_t CSPin, uint32_t spiPortSpeed, SPIClass &spiPort)
    {
    	_i2cPort = NULL;
    	_spiPortSpeed = spiPortSpeed;
      _spiPort = &spiPort;
    The last line here ...

  9. #9
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    9,755
    Quick update: Looks like the library owner has made changes to hopefully address this.

    https://github.com/sparkfun/SparkFun...ent-5641011568
    And I believe released a new updated version of the library

  10. #10
    Junior Member
    Join Date
    Nov 2021
    Posts
    4
    Hi KurtE,
    thank you for your support and forwarding the issue to the right place.

    I just tried with the Updated K13X Library (1.0.7) and it seems to run now!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •