Skip to content

Commit cbd64db

Browse files
committed
Added reg and save debug commands to mtenc
1 parent 597efc1 commit cbd64db

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### Changes this version:
22
- Added SPI speed selector to MagnTek encoders
3+
- Added "reg" and "save" commands to MagnTek encoder. Allows programming MT6835 encoders (debug=1 mode required!)
34

45
### Changes in 1.16:
56

Firmware/FFBoard/UserExtensions/Inc/MtEncoderSPI.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
class MtEncoderSPI: public Encoder, public SPIDevice, public PersistentStorage, public CommandHandler,cpp_freertos::Thread{
2121
enum class MtEncoderSPI_commands : uint32_t{
22-
cspin,pos,errors,mode,speed
22+
cspin,pos,errors,mode,speed,reg,save
2323
};
2424
enum class MtEncoderSPI_mode : uint8_t{
2525
mt6825,mt6835
@@ -64,6 +64,7 @@ class MtEncoderSPI: public Encoder, public SPIDevice, public PersistentStorage,
6464
uint8_t readSpi(uint16_t addr);
6565
void writeSpi(uint16_t addr,uint8_t data);
6666
void spiTxRxCompleted(SPIPort* port);
67+
bool saveEeprom();
6768

6869

6970
bool nomag = false; // Magnet lost in last report

Firmware/FFBoard/UserExtensions/Src/MtEncoderSPI.cpp

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ MtEncoderSPI::MtEncoderSPI() : SPIDevice(ENCODER_SPI_PORT,ENCODER_SPI_PORT.getFr
4141
registerCommand("errors", MtEncoderSPI_commands::errors, "Parity error count",CMDFLAG_GET);
4242
registerCommand("mode", MtEncoderSPI_commands::mode, "Encoder mode (MT6825=0;MT6835=1)",CMDFLAG_GET | CMDFLAG_SET | CMDFLAG_INFOSTRING);
4343
registerCommand("speed", MtEncoderSPI_commands::speed, "SPI speed preset",CMDFLAG_GET | CMDFLAG_SET | CMDFLAG_INFOSTRING);
44+
registerCommand("reg", MtEncoderSPI_commands::reg, "Read/Write register",CMDFLAG_GETADR | CMDFLAG_SETADR | CMDFLAG_DEBUG);
45+
registerCommand("save", MtEncoderSPI_commands::save, "Save to memory",CMDFLAG_GET | CMDFLAG_DEBUG);
4446
this->Start();
4547
}
4648

@@ -117,13 +119,13 @@ uint8_t MtEncoderSPI::readSpi(uint16_t addr){
117119
uint8_t txbuf[2] = {(uint8_t)(addr | 0x80),0};
118120
uint8_t rxbuf[2] = {0,0};
119121
spiPort.transmitReceive(txbuf, rxbuf, 2, this,100);
122+
return rxbuf[1];
120123
}else if(mode == MtEncoderSPI_mode::mt6835){
121124
uint8_t txbuf[3] = {(uint8_t)((addr & 0xf00) | 0x30),(uint8_t)(addr & 0xff),0};
122125
uint8_t rxbuf[3] = {0,0,0};
123126
spiPort.transmitReceive(txbuf, rxbuf, 3, this,100);
127+
return rxbuf[2];
124128
}
125-
126-
return rxbuf[1];
127129
}
128130

129131
void MtEncoderSPI::writeSpi(uint16_t addr,uint8_t data){
@@ -137,6 +139,22 @@ void MtEncoderSPI::writeSpi(uint16_t addr,uint8_t data){
137139

138140
}
139141

142+
/**
143+
* Saves current configuration to permanent storage
144+
*/
145+
bool MtEncoderSPI::saveEeprom(){
146+
if(mode != MtEncoderSPI_mode::mt6835){
147+
return false;
148+
}
149+
if(mode == MtEncoderSPI_mode::mt6835){
150+
uint8_t txbuf[3] = {0xC0,0x00,0x00};
151+
uint8_t rxbuf[3] = {0,0,0};
152+
spiPort.transmitReceive(txbuf, rxbuf, 3, this,100);
153+
return rxbuf[2] == 0x55;
154+
}
155+
return false;
156+
}
157+
140158
void MtEncoderSPI::setPos(int32_t pos){
141159
offset = curPos - pos;
142160
}
@@ -157,8 +175,6 @@ void MtEncoderSPI::spiTxRxCompleted(SPIPort* port){
157175
*/
158176
void MtEncoderSPI::updateAngleStatus(){
159177

160-
161-
162178
if(mode == MtEncoderSPI_mode::mt6825){
163179
uint8_t txbufNew[5] = {0x03 | 0x80,0,0,0,0};
164180
memcpy(this->txbuf,txbufNew,5);
@@ -326,6 +342,24 @@ CommandStatus MtEncoderSPI::command(const ParsedCommand& cmd,std::vector<Command
326342
}
327343
break;
328344
}
345+
case MtEncoderSPI_commands::reg:
346+
{
347+
if(cmd.type == CMDtype::getat){
348+
replies.emplace_back(readSpi(cmd.adr));
349+
}else if(cmd.type == CMDtype::setat){
350+
writeSpi(cmd.adr, cmd.val);
351+
}else{
352+
return CommandStatus::ERR;
353+
}
354+
break;
355+
}
356+
case MtEncoderSPI_commands::save:
357+
{
358+
if(cmd.type == CMDtype::get){
359+
replies.emplace_back(saveEeprom() ? 1 : 0);
360+
}
361+
break;
362+
}
329363
default:
330364
return CommandStatus::NOT_FOUND;
331365
}

0 commit comments

Comments
 (0)