Skip to content

Commit 9b03b5d

Browse files
author
Richard Unger
committed
fix errors in MT6835 driver code
1 parent fa4aaa0 commit 9b03b5d

File tree

2 files changed

+36
-21
lines changed

2 files changed

+36
-21
lines changed

src/encoders/mt6835/MT6835.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ uint32_t MT6835::readRawAngle21(){
4646
spi->endTransaction();
4747
if (nCS >= 0)
4848
digitalWrite(nCS, HIGH);
49+
laststatus = data[4]&0x07;
4950
return (data[2] << 13) | (data[3] << 5) | (data[4] >> 3);
5051
};
5152

5253

54+
uint8_t MT6835::getStatus(){
55+
return laststatus;
56+
};
5357

5458

5559
bool MT6835::setZeroFromCurrentPosition(){
@@ -237,17 +241,25 @@ void MT6835::setOptions4(MT6835Options4 opts){
237241

238242

239243

244+
uint32_t swap_bytes(uint32_t net)
245+
{
246+
return __builtin_bswap32(net);
247+
}
248+
249+
240250

241251

242252

243253
void MT6835::transfer24(MT6835Command* outValue) {
254+
uint32_t buff = swap_bytes(outValue->val);
244255
if (nCS >= 0)
245256
digitalWrite(nCS, LOW);
246257
spi->beginTransaction(settings);
247-
spi->transfer(outValue, 3);
258+
spi->transfer(&buff, 3);
248259
spi->endTransaction();
249260
if (nCS >= 0)
250261
digitalWrite(nCS, HIGH);
262+
outValue->val = swap_bytes(buff);
251263
};
252264
uint8_t MT6835::readRegister(uint16_t reg) {
253265
MT6835Command cmd;
@@ -258,7 +270,7 @@ uint8_t MT6835::readRegister(uint16_t reg) {
258270
};
259271
bool MT6835::writeRegister(uint16_t reg, uint8_t value) {
260272
MT6835Command cmd;
261-
cmd.cmd = MT6835_OP_READ;
273+
cmd.cmd = MT6835_OP_WRITE;
262274
cmd.addr = reg;
263275
cmd.data = value;
264276
transfer24(&cmd);

src/encoders/mt6835/MT6835.h

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353

5454
union MT6835ABZRes {
5555
struct {
56-
uint8_t abz_res_low:6;
57-
uint8_t abz_off:1;
5856
uint8_t ab_swap:1;
57+
uint8_t abz_off:1;
58+
uint8_t abz_res_low:6;
5959
};
6060
uint8_t reg;
6161
};
@@ -64,9 +64,9 @@ union MT6835ABZRes {
6464

6565
union MT6835Options0 {
6666
struct {
67-
uint8_t zero_pos_low:4;
68-
uint8_t z_edge:1;
6967
uint8_t z_pul_wid:3;
68+
uint8_t z_edge:1;
69+
uint8_t zero_pos_low:4;
7070
};
7171
uint8_t reg;
7272
};
@@ -75,10 +75,10 @@ union MT6835Options0 {
7575

7676
union MT6835Options1 {
7777
struct {
78-
uint8_t z_phase:2;
79-
uint8_t uvw_mux:1;
80-
uint8_t uvw_off:1;
8178
uint8_t uvw_res:4;
79+
uint8_t uvw_off:1;
80+
uint8_t uvw_mux:1;
81+
uint8_t z_phase:2;
8282
};
8383
uint8_t reg;
8484
};
@@ -87,11 +87,11 @@ union MT6835Options1 {
8787

8888
union MT6835Options2 {
8989
struct {
90-
uint8_t reserved:2;
91-
uint8_t nlc_en:1;
92-
uint8_t pwm_fq:1;
93-
uint8_t pwm_pol:1;
9490
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;
9595
};
9696
uint8_t reg;
9797
};
@@ -100,9 +100,9 @@ union MT6835Options2 {
100100

101101
union MT6835Options3 {
102102
struct {
103-
uint8_t reserved:4;
104-
uint8_t rot_dir:1;
105103
uint8_t hyst:3;
104+
uint8_t rot_dir:1;
105+
uint8_t reserved:4;
106106
};
107107
uint8_t reg;
108108
};
@@ -111,9 +111,9 @@ union MT6835Options3 {
111111

112112
union MT6835Options4 {
113113
struct {
114-
uint8_t gpio_ds:1;
115-
uint8_t autocal_freq:3;
116114
uint8_t reserved:4;
115+
uint8_t autocal_freq:3;
116+
uint8_t gpio_ds:1;
117117
};
118118
uint8_t reg;
119119
};
@@ -122,8 +122,8 @@ union MT6835Options4 {
122122

123123
union MT6835Options5 {
124124
struct {
125-
uint8_t reserved:5;
126125
uint8_t bw:3;
126+
uint8_t reserved:5;
127127
};
128128
uint8_t reg;
129129
};
@@ -133,10 +133,10 @@ union MT6835Options5 {
133133

134134
union MT6835Command {
135135
struct {
136-
uint32_t cmd:4;
137-
uint32_t addr:12;
138-
uint32_t data:8;
139136
uint32_t unused:8;
137+
uint32_t data:8;
138+
uint32_t addr:12;
139+
uint32_t cmd:4;
140140
};
141141
uint32_t val;
142142
};
@@ -197,13 +197,16 @@ class MT6835 {
197197
MT6835Options4 getOptions4();
198198
void setOptions4(MT6835Options4 opts);
199199

200+
uint8_t getStatus();
201+
200202
bool setZeroFromCurrentPosition();
201203
bool writeEEPROM(); // wait 6s after calling this method
202204

203205
private:
204206
SPIClass* spi;
205207
SPISettings settings;
206208
int nCS = -1;
209+
uint8_t laststatus = 0;
207210

208211
void transfer24(MT6835Command* outValue);
209212
uint8_t readRegister(uint16_t reg);

0 commit comments

Comments
 (0)