@@ -112,32 +112,39 @@ NamedEnum(FilamentSensorStatus, uint8_t,
112112
113113NamedEnum (LogLevel, uint8_t , off, warn, info, debug);
114114
115- // Meaning of the driver status bits. The lowest 8 bits of these have the same bit positions as in the TMC2209 DRV_STATUS register. The TMC5160 DRV_STATUS is different.
115+ // Meaning of the driver status bits.
116+ // The lowest 8 bits of these have the same bit positions as in the TMC2209 DRV_STATUS register.
117+ // The TMC5160 DRV_STATUS is different so the bits are translated to this. Similarly for TMC2660.
118+ // Only the lowest 16 bits are passed in driver event messages
116119union StandardDriverStatus
117120{
118121 uint32_t all;
119122 struct
120123 {
121- uint32_t otpw : 1 , // over temperature warning
122- ot : 1 , // over temperature error
123- s2ga : 1 , // short to ground phase A
124- s2gb : 1 , // short to ground phase B
125- s2vsa : 1 , // short to VS phase A
126- s2vsb : 1 , // short to VS phase B
127- ola : 1 , // open load phase A
128- olb : 1 , // open load phase B
129- // The remaining bit assignments do not correspond to TMC2209 bit positions
130- standstill : 1 , // standstill indicator
131- stall : 1 , // stall, or closed loop error exceeded
132- notPresent : 1 , // smart driver not present
133- externalDriverError : 1 , // external driver signalled error
134- closedLoopPositionWarning : 1 , // close to stall, or closed loop warning
135- closedLoopPositionNotMaintained : 1 , // failed to achieve position
136- closedLoopNotTuned : 1 , // closed loop driver has not been tuned
137- closedLoopTuningError : 1 , // closed loop tuning failed
138- closedLoopIllegalMove : 1 , // move attempted in closed loop mode when driver not tuned
139- zero : 5 , // reserved for future use - don't use the MSB because it will make the value negative in the OM
140- sgresultMin : 10 ; // minimum stallguard result seen
124+ uint32_t
125+ // bits 0-7 (these match the TMC2209 status bits for easy translation)
126+ otpw : 1 , // over temperature warning
127+ ot : 1 , // over temperature error
128+ s2ga : 1 , // short to ground phase A
129+ s2gb : 1 , // short to ground phase B
130+ s2vsa : 1 , // short to VS phase A
131+ s2vsb : 1 , // short to VS phase B
132+ ola : 1 , // open load phase A
133+ olb : 1 , // open load phase B
134+ // bits 8-15
135+ stall : 1 , // stall, or closed loop error exceeded
136+ externalDriverError : 1 , // external driver signalled error
137+ closedLoopPositionWarning : 1 , // close to stall, or closed loop warning
138+ closedLoopPositionNotMaintained : 1 , // failed to achieve position
139+ closedLoopNotTuned : 1 , // closed loop driver has not been tuned
140+ closedLoopTuningError : 1 , // closed loop tuning failed
141+ closedLoopIllegalMove : 1 , // move attempted in closed loop mode when driver not tuned
142+ zero1 : 1 , // reserved for future use
143+ // bits 16-31 (these are not passed in driver event messages)
144+ standstill : 1 , // standstill indicator
145+ notPresent : 1 , // smart driver not present
146+ zero2 : 4 , // reserved for future use - don't use the MSB because it will make the value negative in the OM
147+ sgresultMin : 10 ; // minimum stallguard result seen
141148 };
142149
143150 static constexpr unsigned int OtBitPos = 0 ;
@@ -146,15 +153,26 @@ union StandardDriverStatus
146153 static constexpr unsigned int StallBitPos = 10 ;
147154 static constexpr unsigned int SgresultBitPos = 16 ;
148155
149- static constexpr uint32_t ErrorMask = 0b10010101000111110 ; // bit positions that usually correspond to errors
150- static constexpr uint32_t WarningMask = 0b01001000011000001 ; // bit positions that correspond to warnings
151- static constexpr uint32_t InfoMask = 0b00100010100000000 ; // bit positions that correspond to information
156+ static constexpr uint32_t ErrorMask = 0b1'0010'1010'0011'1110 ; // bit positions that usually correspond to errors
157+ static constexpr uint32_t WarningMask = 0b0'1001'0000'1100'0001 ; // bit positions that correspond to warnings
158+ static constexpr uint32_t InfoMask = 0b0'0100'0101'0000'0000 ; // bit positions that correspond to information
152159
153160 static_assert ((ErrorMask & WarningMask) == 0 );
154161 static_assert ((ErrorMask & InfoMask) == 0 );
155162 static_assert ((InfoMask & WarningMask) == 0 );
156163
164+ StandardDriverStatus () noexcept : all (0 ) { }
165+ explicit StandardDriverStatus (uint32_t val) : all (val) { }
166+ void Clear () noexcept { all = 0 ; }
167+
157168 void AppendText (const StringRef& str, unsigned int severity) const noexcept ;
169+ bool HasNewErrorSince (StandardDriverStatus prev) const noexcept { return ((all & ~prev.all ) & ErrorMask) != 0 ; }
170+ bool HasNewWarningSince (StandardDriverStatus prev) const noexcept { return ((all & ~prev.all ) & WarningMask) != 0 ; }
171+ bool HasNewStallSince (StandardDriverStatus prev) const noexcept { return ((all & ~prev.all ) & (1u << StallBitPos)) != 0 ; }
172+ bool IsAnyOpenLoadBitSet () const noexcept { return (all & OpenLoadMask) != 0 ; }
173+ void ClearOpenLoadBits () noexcept { all &= ~OpenLoadMask; }
174+ uint16_t AsU16 () const noexcept { return (uint16_t )all; } // this includes the error ands warning bits but no the others
175+ uint32_t AsU32 () const noexcept { return all; }
158176
159177private:
160178 // Strings representing the meaning of each bit in DriverStatus
@@ -180,6 +198,8 @@ union StandardDriverStatus
180198 };
181199
182200 static_assert ((1u << ARRAY_SIZE (BitMeanings)) - 1 == (ErrorMask | WarningMask | InfoMask));
201+
202+ static constexpr uint32_t OpenLoadMask = 0b0'0000'0000'1100'0000 ; // bit positions that correspond to open load bits
183203};
184204
185205static_assert (sizeof (StandardDriverStatus) == sizeof (uint32_t ));
0 commit comments