@@ -44,6 +44,8 @@ struct tmc51xx_config {
44
44
#endif
45
45
};
46
46
47
+ static int read_actual_position (const struct device * dev , int32_t * position );
48
+
47
49
static int tmc51xx_write (const struct device * dev , const uint8_t reg_addr , const uint32_t reg_val )
48
50
{
49
51
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
57
59
58
60
k_sem_give (& data -> sem );
59
61
60
- if (err ) {
62
+ if (err < 0 ) {
61
63
LOG_ERR ("Failed to write register 0x%x with value 0x%x" , reg_addr , reg_val );
62
64
return err ;
63
65
}
@@ -77,7 +79,7 @@ static int tmc51xx_read(const struct device *dev, const uint8_t reg_addr, uint32
77
79
78
80
k_sem_give (& data -> sem );
79
81
80
- if (err ) {
82
+ if (err < 0 ) {
81
83
LOG_ERR ("Failed to read register 0x%x" , reg_addr );
82
84
return err ;
83
85
}
@@ -94,6 +96,23 @@ static int tmc51xx_stepper_set_event_callback(const struct device *dev,
94
96
return 0 ;
95
97
}
96
98
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
+
97
116
static int stallguard_enable (const struct device * dev , const bool enable )
98
117
{
99
118
const struct tmc51xx_config * config = dev -> config ;
@@ -111,17 +130,10 @@ static int stallguard_enable(const struct device *dev, const bool enable)
111
130
112
131
int32_t actual_velocity ;
113
132
114
- err = tmc51xx_read (dev , TMC51XX_VACTUAL ,
115
- & actual_velocity );
133
+ err = read_vactual (dev , & actual_velocity );
116
134
if (err ) {
117
- LOG_ERR ("Failed to read VACTUAL register" );
118
135
return - EIO ;
119
136
}
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
-
125
137
if (abs (actual_velocity ) < config -> sg_threshold_velocity ) {
126
138
return - EAGAIN ;
127
139
}
@@ -133,6 +145,8 @@ static int stallguard_enable(const struct device *dev, const bool enable)
133
145
LOG_ERR ("Failed to write SWMODE register" );
134
146
return - EIO ;
135
147
}
148
+
149
+ LOG_DBG ("Stallguard %s" , enable ? "enabled" : "disabled" );
136
150
return 0 ;
137
151
}
138
152
@@ -147,7 +161,6 @@ static void stallguard_work_handler(struct k_work *work)
147
161
148
162
err = stallguard_enable (dev , true);
149
163
if (err == - EAGAIN ) {
150
- LOG_ERR ("retrying stallguard activation" );
151
164
k_work_reschedule (dwork , K_MSEC (config -> sg_velocity_check_interval_ms ));
152
165
}
153
166
if (err == - EIO ) {
@@ -169,6 +182,45 @@ static void execute_callback(const struct device *dev, const enum stepper_event
169
182
data -> callback (dev , event , data -> event_cb_user_data );
170
183
}
171
184
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
+
172
224
static void rampstat_work_handler (struct k_work * work )
173
225
{
174
226
struct k_work_delayable * dwork = k_work_delayable_from_work (work );
@@ -188,7 +240,9 @@ static void rampstat_work_handler(struct k_work *work)
188
240
LOG_ERR ("%s: Failed to read DRVSTATUS register" , dev -> name );
189
241
return ;
190
242
}
191
-
243
+ #ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL_STALLGUARD_LOG
244
+ log_stallguard (dev , drv_status );
245
+ #endif
192
246
if (FIELD_GET (TMC5XXX_DRV_STATUS_SG_STATUS_MASK , drv_status ) == 1U ) {
193
247
LOG_INF ("%s: Stall detected" , dev -> name );
194
248
err = tmc51xx_write (dev ,
@@ -202,8 +256,7 @@ static void rampstat_work_handler(struct k_work *work)
202
256
203
257
uint32_t rampstat_value ;
204
258
205
- err = tmc51xx_read (dev , TMC51XX_RAMPSTAT ,
206
- & rampstat_value );
259
+ err = rampstat_read_clear (dev , & rampstat_value );
207
260
if (err != 0 ) {
208
261
LOG_ERR ("%s: Failed to read RAMPSTAT register" , dev -> name );
209
262
return ;
@@ -241,9 +294,7 @@ static void rampstat_work_handler(struct k_work *work)
241
294
break ;
242
295
}
243
296
} 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 );
247
298
}
248
299
}
249
300
@@ -380,14 +431,25 @@ static int tmc51xx_stepper_set_reference_position(const struct device *dev, cons
380
431
return 0 ;
381
432
}
382
433
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 )
384
435
{
385
436
int err ;
386
437
387
438
err = tmc51xx_read (dev , TMC51XX_XACTUAL , position );
388
439
if (err != 0 ) {
389
440
return - EIO ;
390
441
}
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
+ }
391
453
LOG_DBG ("%s actual position: %d" , dev -> name , * position );
392
454
return 0 ;
393
455
}
@@ -419,9 +481,7 @@ static int tmc51xx_stepper_move_to(const struct device *dev, const int32_t micro
419
481
}
420
482
#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL
421
483
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 );
425
485
}
426
486
#endif
427
487
return 0 ;
@@ -481,9 +541,7 @@ static int tmc51xx_stepper_run(const struct device *dev, const enum stepper_dire
481
541
}
482
542
#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL
483
543
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 );
487
545
}
488
546
#endif
489
547
return 0 ;
@@ -617,12 +675,7 @@ static int tmc51xx_init(const struct device *dev)
617
675
if (err != 0 ) {
618
676
return - EIO ;
619
677
}
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 );
626
679
}
627
680
628
681
#ifdef CONFIG_STEPPER_ADI_TMC51XX_RAMP_GEN
@@ -634,8 +687,9 @@ static int tmc51xx_init(const struct device *dev)
634
687
635
688
#if CONFIG_STEPPER_ADI_TMC51XX_RAMPSTAT_POLL
636
689
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 );
639
693
#endif
640
694
err = tmc51xx_stepper_set_micro_step_res (dev , config -> default_micro_step_res );
641
695
if (err != 0 ) {
0 commit comments