@@ -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
129131void 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+
140158void MtEncoderSPI::setPos (int32_t pos){
141159 offset = curPos - pos;
142160}
@@ -157,8 +175,6 @@ void MtEncoderSPI::spiTxRxCompleted(SPIPort* port){
157175 */
158176void 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