@@ -99,27 +99,58 @@ bool QwDevKX13X::initialize(uint8_t settings)
9999//
100100// Resets the accelerometer
101101//
102- // To change the value of the SRST bit, the PC1 bit in CNTL1 register must first be set to 0.
102+ // Kionix Technical Reference Manual says:
103+ // "To change the value of the SRST bit, the PC1 bit in CNTL1 register must first be set to 0."
103104//
105+ // Kionix TN027 "Power On Procedure" says to:
106+ // Write 0x00 to register 0x7F
107+ // Write 0x00 to CNTL2
108+ // Write 0x80 (SRST) to CNTL2
109+ //
110+ // Kionix Technical Reference Manual says:
111+ // "For I2C Communication: Setting SRST = 1 will NOT result in an ACK, since the part immediately
112+ // enters the RAM reboot routine. NACK may be used to confirm this command."
113+ // However, we've not seen the NACK when writing the SRST bit. That write always seems to be ACK'd as normal.
114+ // But, the _next_ I2C transaction _does_ get NACK'd...
115+ // The solution seems to be to keep trying to read CNTL2 and wait for the SRST bit to be cleared.
116+
104117bool QwDevKX13X::softwareReset ()
105118{
106119 enableAccel (false ); // Clear the PC1 bit in CNTL1
107120
121+ int retVal;
122+
123+ retVal = writeRegisterByte (0x7F , 0 );
124+
125+ if (retVal != 0 )
126+ return false ;
127+
128+ retVal = writeRegisterByte (SFE_KX13X_CNTL2, 0 );
129+
130+ if (retVal != 0 )
131+ return false ;
132+
108133 sfe_kx13x_cntl2_bitfield_t cntl2;
109134 cntl2.all = 0 ;
110135 cntl2.bits .srst = 1 ; // This is a long winded, but definitive way of setting the software reset bit
111136
112- int retVal;
137+ retVal = writeRegisterByte (SFE_KX13X_CNTL2, cntl2. all ); // Do the reset
113138
114- retVal = writeRegisterByte (SFE_KX13X_CNTL2, cntl2.all );
139+ uint8_t loopCount = 0 ;
140+ while (loopCount < 10 ) // Reset takes about 2ms. Timeout after 10ms
141+ {
142+ retVal = readRegisterRegion (SFE_KX13X_CNTL2, &cntl2.all , 1 ); // Try to read CNTL2 (the first read gets NACK'd)
115143
116- // Logic is inverted here - if we reset using I2C the
117- // accelerometer immediately shuts off which results
118- // in a NACK.
119- if (retVal != 0 )
120- return true ;
144+ if ((retVal == 0 ) && (cntl2.bits .srst == 0 )) // Check if the software reset bit has been cleared
145+ loopCount = 10 ; // Exit the loop if it has
146+ else
147+ {
148+ loopCount++; // Increment the count and repeat
149+ delay (1 ); // Delay for 1ms: important for SPI
150+ }
151+ }
121152
122- return false ;
153+ return ((retVal == 0 ) && (cntl2. bits . srst == 0 )) ;
123154}
124155
125156// ////////////////////////////////////////////////
0 commit comments