File tree Expand file tree Collapse file tree 2 files changed +36
-21
lines changed Expand file tree Collapse file tree 2 files changed +36
-21
lines changed Original file line number Diff line number Diff line change @@ -46,10 +46,14 @@ uint32_t MT6835::readRawAngle21(){
46
46
spi->endTransaction ();
47
47
if (nCS >= 0 )
48
48
digitalWrite (nCS, HIGH);
49
+ laststatus = data[4 ]&0x07 ;
49
50
return (data[2 ] << 13 ) | (data[3 ] << 5 ) | (data[4 ] >> 3 );
50
51
};
51
52
52
53
54
+ uint8_t MT6835::getStatus (){
55
+ return laststatus;
56
+ };
53
57
54
58
55
59
bool MT6835::setZeroFromCurrentPosition (){
@@ -237,17 +241,25 @@ void MT6835::setOptions4(MT6835Options4 opts){
237
241
238
242
239
243
244
+ uint32_t swap_bytes (uint32_t net)
245
+ {
246
+ return __builtin_bswap32 (net);
247
+ }
248
+
249
+
240
250
241
251
242
252
243
253
void MT6835::transfer24 (MT6835Command* outValue) {
254
+ uint32_t buff = swap_bytes (outValue->val );
244
255
if (nCS >= 0 )
245
256
digitalWrite (nCS, LOW);
246
257
spi->beginTransaction (settings);
247
- spi->transfer (outValue , 3 );
258
+ spi->transfer (&buff , 3 );
248
259
spi->endTransaction ();
249
260
if (nCS >= 0 )
250
261
digitalWrite (nCS, HIGH);
262
+ outValue->val = swap_bytes (buff);
251
263
};
252
264
uint8_t MT6835::readRegister (uint16_t reg) {
253
265
MT6835Command cmd;
@@ -258,7 +270,7 @@ uint8_t MT6835::readRegister(uint16_t reg) {
258
270
};
259
271
bool MT6835::writeRegister (uint16_t reg, uint8_t value) {
260
272
MT6835Command cmd;
261
- cmd.cmd = MT6835_OP_READ ;
273
+ cmd.cmd = MT6835_OP_WRITE ;
262
274
cmd.addr = reg;
263
275
cmd.data = value;
264
276
transfer24 (&cmd);
Original file line number Diff line number Diff line change 53
53
54
54
union MT6835ABZRes {
55
55
struct {
56
- uint8_t abz_res_low:6 ;
57
- uint8_t abz_off:1 ;
58
56
uint8_t ab_swap:1 ;
57
+ uint8_t abz_off:1 ;
58
+ uint8_t abz_res_low:6 ;
59
59
};
60
60
uint8_t reg;
61
61
};
@@ -64,9 +64,9 @@ union MT6835ABZRes {
64
64
65
65
union MT6835Options0 {
66
66
struct {
67
- uint8_t zero_pos_low:4 ;
68
- uint8_t z_edge:1 ;
69
67
uint8_t z_pul_wid:3 ;
68
+ uint8_t z_edge:1 ;
69
+ uint8_t zero_pos_low:4 ;
70
70
};
71
71
uint8_t reg;
72
72
};
@@ -75,10 +75,10 @@ union MT6835Options0 {
75
75
76
76
union MT6835Options1 {
77
77
struct {
78
- uint8_t z_phase:2 ;
79
- uint8_t uvw_mux:1 ;
80
- uint8_t uvw_off:1 ;
81
78
uint8_t uvw_res:4 ;
79
+ uint8_t uvw_off:1 ;
80
+ uint8_t uvw_mux:1 ;
81
+ uint8_t z_phase:2 ;
82
82
};
83
83
uint8_t reg;
84
84
};
@@ -87,11 +87,11 @@ union MT6835Options1 {
87
87
88
88
union MT6835Options2 {
89
89
struct {
90
- uint8_t reserved:2 ;
91
- uint8_t nlc_en:1 ;
92
- uint8_t pwm_fq:1 ;
93
- uint8_t pwm_pol:1 ;
94
90
uint8_t pwm_sel:3 ;
91
+ uint8_t pwm_pol:1 ;
92
+ uint8_t pwm_fq:1 ;
93
+ uint8_t nlc_en:1 ;
94
+ uint8_t reserved:2 ;
95
95
};
96
96
uint8_t reg;
97
97
};
@@ -100,9 +100,9 @@ union MT6835Options2 {
100
100
101
101
union MT6835Options3 {
102
102
struct {
103
- uint8_t reserved:4 ;
104
- uint8_t rot_dir:1 ;
105
103
uint8_t hyst:3 ;
104
+ uint8_t rot_dir:1 ;
105
+ uint8_t reserved:4 ;
106
106
};
107
107
uint8_t reg;
108
108
};
@@ -111,9 +111,9 @@ union MT6835Options3 {
111
111
112
112
union MT6835Options4 {
113
113
struct {
114
- uint8_t gpio_ds:1 ;
115
- uint8_t autocal_freq:3 ;
116
114
uint8_t reserved:4 ;
115
+ uint8_t autocal_freq:3 ;
116
+ uint8_t gpio_ds:1 ;
117
117
};
118
118
uint8_t reg;
119
119
};
@@ -122,8 +122,8 @@ union MT6835Options4 {
122
122
123
123
union MT6835Options5 {
124
124
struct {
125
- uint8_t reserved:5 ;
126
125
uint8_t bw:3 ;
126
+ uint8_t reserved:5 ;
127
127
};
128
128
uint8_t reg;
129
129
};
@@ -133,10 +133,10 @@ union MT6835Options5 {
133
133
134
134
union MT6835Command {
135
135
struct {
136
- uint32_t cmd:4 ;
137
- uint32_t addr:12 ;
138
- uint32_t data:8 ;
139
136
uint32_t unused:8 ;
137
+ uint32_t data:8 ;
138
+ uint32_t addr:12 ;
139
+ uint32_t cmd:4 ;
140
140
};
141
141
uint32_t val;
142
142
};
@@ -197,13 +197,16 @@ class MT6835 {
197
197
MT6835Options4 getOptions4 ();
198
198
void setOptions4 (MT6835Options4 opts);
199
199
200
+ uint8_t getStatus ();
201
+
200
202
bool setZeroFromCurrentPosition ();
201
203
bool writeEEPROM (); // wait 6s after calling this method
202
204
203
205
private:
204
206
SPIClass* spi;
205
207
SPISettings settings;
206
208
int nCS = -1 ;
209
+ uint8_t laststatus = 0 ;
207
210
208
211
void transfer24 (MT6835Command* outValue);
209
212
uint8_t readRegister (uint16_t reg);
You can’t perform that action at this time.
0 commit comments