@@ -44,6 +44,8 @@ struct tmc51xx_config {
4444#endif
4545};
4646
47+ static int read_actual_position (const struct device * dev , int32_t * position );
48+
4749static int tmc51xx_write (const struct device * dev , const uint8_t reg_addr , const uint32_t reg_val )
4850{
4951 const struct tmc51xx_config * config = dev -> config ;
@@ -57,7 +59,7 @@ static int tmc51xx_write(const struct device *dev, const uint8_t reg_addr, const
5759
5860 k_sem_give (& data -> sem );
5961
60- if (err ) {
62+ if (err < 0 ) {
6163 LOG_ERR ("Failed to write register 0x%x with value 0x%x" , reg_addr , reg_val );
6264 return err ;
6365 }
@@ -77,7 +79,7 @@ static int tmc51xx_read(const struct device *dev, const uint8_t reg_addr, uint32
7779
7880 k_sem_give (& data -> sem );
7981
80- if (err ) {
82+ if (err < 0 ) {
8183 LOG_ERR ("Failed to read register 0x%x" , reg_addr );
8284 return err ;
8385 }
@@ -94,6 +96,23 @@ static int tmc51xx_stepper_set_event_callback(const struct device *dev,
9496 return 0 ;
9597}
9698
99+ static int read_vactual (const struct device * dev , int32_t * actual_velocity )
100+ {
101+ int err ;
102+
103+ err = tmc51xx_read (dev , TMC51XX_VACTUAL , actual_velocity );
104+ if (err ) {
105+ LOG_ERR ("Failed to read VACTUAL register" );
106+ return err ;
107+ }
108+
109+ * actual_velocity = sign_extend (* actual_velocity , TMC_RAMP_VACTUAL_SHIFT );
110+ if (* actual_velocity ) {
111+ LOG_DBG ("actual velocity: %d" , * actual_velocity );
112+ }
113+ return 0 ;
114+ }
115+
97116static int stallguard_enable (const struct device * dev , const bool enable )
98117{
99118 const struct tmc51xx_config * config = dev -> config ;
@@ -111,17 +130,10 @@ static int stallguard_enable(const struct device *dev, const bool enable)
111130
112131 int32_t actual_velocity ;
113132
114- err = tmc51xx_read (dev , TMC51XX_VACTUAL ,
115- & actual_velocity );
133+ err = read_vactual (dev , & actual_velocity );
116134 if (err ) {
117- LOG_ERR ("Failed to read VACTUAL register" );
118135 return - EIO ;
119136 }
120-
121- actual_velocity = (actual_velocity << (31 - TMC_RAMP_VACTUAL_SHIFT )) >>
122- (31 - TMC_RAMP_VACTUAL_SHIFT );
123- LOG_DBG ("actual velocity: %d" , actual_velocity );
124-
125137 if (abs (actual_velocity ) < config -> sg_threshold_velocity ) {
126138 return - EAGAIN ;
127139 }
@@ -133,6 +145,8 @@ static int stallguard_enable(const struct device *dev, const bool enable)
133145 LOG_ERR ("Failed to write SWMODE register" );
134146 return - EIO ;
135147 }
148+
149+ LOG_DBG ("Stallguard %s" , enable ? "enabled" : "disabled" );
136150 return 0 ;
137151}
138152
@@ -147,7 +161,6 @@ static void stallguard_work_handler(struct k_work *work)
147161
148162 err = stallguard_enable (dev , true);
149163 if (err == - EAGAIN ) {
150- LOG_ERR ("retrying stallguard activation" );
151164 k_work_reschedule (dwork , K_MSEC (config -> sg_velocity_check_interval_ms ));
152165 }
153166 if (err == - EIO ) {
@@ -169,6 +182,45 @@ static void execute_callback(const struct device *dev, const enum stepper_event
169182 data -> callback (dev , event , data -> event_cb_user_data );
170183}
171184
185+ #ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_STALLGUARD_LOG
186+
187+ static void log_stallguard (const struct device * dev , const uint32_t drv_status )
188+ {
189+ int32_t position ;
190+ int err ;
191+
192+ err = read_actual_position (dev , & position );
193+ if (err != 0 ) {
194+ LOG_ERR ("%s: Failed to read XACTUAL register" , dev -> name );
195+ return ;
196+ }
197+
198+ const uint8_t sg_result = FIELD_GET (TMC5XXX_DRV_STATUS_SG_RESULT_MASK , drv_status );
199+ const bool sg_status = FIELD_GET (TMC5XXX_DRV_STATUS_SG_STATUS_MASK , drv_status );
200+
201+ LOG_DBG ("%s position: %d | sg result: %3d status: %d" ,
202+ dev -> name , position , sg_result , sg_status );
203+ }
204+
205+ #endif
206+
207+ static void rampstat_work_reschedule (struct k_work_delayable * rampstat_callback_dwork )
208+ {
209+ k_work_reschedule (rampstat_callback_dwork ,
210+ K_MSEC (CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC ));
211+ }
212+
213+ static int rampstat_read_clear (const struct device * dev , uint32_t * rampstat_value )
214+ {
215+ int err ;
216+
217+ err = tmc51xx_read (dev , TMC51XX_RAMPSTAT , rampstat_value );
218+ if (err == 0 ) {
219+ err = tmc51xx_write (dev , TMC51XX_RAMPSTAT , * rampstat_value );
220+ }
221+ return err ;
222+ }
223+
172224static void rampstat_work_handler (struct k_work * work )
173225{
174226 struct k_work_delayable * dwork = k_work_delayable_from_work (work );
@@ -188,7 +240,9 @@ static void rampstat_work_handler(struct k_work *work)
188240 LOG_ERR ("%s: Failed to read DRVSTATUS register" , dev -> name );
189241 return ;
190242 }
191-
243+ #ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_STALLGUARD_LOG
244+ log_stallguard (dev , drv_status );
245+ #endif
192246 if (FIELD_GET (TMC5XXX_DRV_STATUS_SG_STATUS_MASK , drv_status ) == 1U ) {
193247 LOG_INF ("%s: Stall detected" , dev -> name );
194248 err = tmc51xx_write (dev ,
@@ -202,8 +256,7 @@ static void rampstat_work_handler(struct k_work *work)
202256
203257 uint32_t rampstat_value ;
204258
205- err = tmc51xx_read (dev , TMC51XX_RAMPSTAT ,
206- & rampstat_value );
259+ err = rampstat_read_clear (dev , & rampstat_value );
207260 if (err != 0 ) {
208261 LOG_ERR ("%s: Failed to read RAMPSTAT register" , dev -> name );
209262 return ;
@@ -241,9 +294,7 @@ static void rampstat_work_handler(struct k_work *work)
241294 break ;
242295 }
243296 } else {
244- k_work_reschedule (
245- & stepper_data -> rampstat_callback_dwork ,
246- K_MSEC (CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC ));
297+ rampstat_work_reschedule (& stepper_data -> rampstat_callback_dwork );
247298 }
248299}
249300
@@ -380,14 +431,25 @@ static int tmc51xx_stepper_set_reference_position(const struct device *dev, cons
380431 return 0 ;
381432}
382433
383- static int tmc51xx_stepper_get_actual_position (const struct device * dev , int32_t * position )
434+ static int read_actual_position (const struct device * dev , int32_t * position )
384435{
385436 int err ;
386437
387438 err = tmc51xx_read (dev , TMC51XX_XACTUAL , position );
388439 if (err != 0 ) {
389440 return - EIO ;
390441 }
442+ return 0 ;
443+ }
444+
445+ static int tmc51xx_stepper_get_actual_position (const struct device * dev , int32_t * position )
446+ {
447+ int err ;
448+
449+ err = read_actual_position (dev , position );
450+ if (err != 0 ) {
451+ return - EIO ;
452+ }
391453 LOG_DBG ("%s actual position: %d" , dev -> name , * position );
392454 return 0 ;
393455}
@@ -419,9 +481,7 @@ static int tmc51xx_stepper_move_to(const struct device *dev, const int32_t micro
419481 }
420482#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL
421483 if (data -> callback ) {
422- k_work_reschedule (
423- & data -> rampstat_callback_dwork ,
424- K_MSEC (CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC ));
484+ rampstat_work_reschedule (& data -> rampstat_callback_dwork );
425485 }
426486#endif
427487 return 0 ;
@@ -481,9 +541,7 @@ static int tmc51xx_stepper_run(const struct device *dev, const enum stepper_dire
481541 }
482542#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL
483543 if (data -> callback ) {
484- k_work_reschedule (
485- & data -> rampstat_callback_dwork ,
486- K_MSEC (CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC ));
544+ rampstat_work_reschedule (& data -> rampstat_callback_dwork );
487545 }
488546#endif
489547 return 0 ;
@@ -617,12 +675,7 @@ static int tmc51xx_init(const struct device *dev)
617675 if (err != 0 ) {
618676 return - EIO ;
619677 }
620- err = stallguard_enable (dev , true);
621- if (err == - EAGAIN ) {
622- LOG_ERR ("retrying stallguard activation" );
623- k_work_reschedule (& data -> stallguard_dwork ,
624- K_MSEC (config -> sg_velocity_check_interval_ms ));
625- }
678+ k_work_reschedule (& data -> stallguard_dwork , K_NO_WAIT );
626679 }
627680
628681#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMP_GEN
@@ -634,8 +687,9 @@ static int tmc51xx_init(const struct device *dev)
634687
635688#if CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL
636689 k_work_init_delayable (& data -> rampstat_callback_dwork , rampstat_work_handler );
637- k_work_reschedule (& data -> rampstat_callback_dwork ,
638- K_MSEC (CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_INTERVAL_IN_MSEC ));
690+ uint32_t rampstat_value ;
691+
692+ (void )rampstat_read_clear (dev , & rampstat_value );
639693#endif
640694 err = tmc51xx_stepper_set_micro_step_res (dev , config -> default_micro_step_res );
641695 if (err != 0 ) {
0 commit comments