Skip to content

Commit 7e0e29b

Browse files
committed
Added magntek spi speed command
1 parent a708735 commit 7e0e29b

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

CHANGELOG.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
### Changes this version:
2-
- Reformatted USB serial string as hex and added command to request UID as hex string
3-
- Added device name to USB Product name
4-
- Added support for F407 OTP section
5-
- Added support for MagnTek MT6835 via SPI (SPI3 port, MagnTek encoder class)
2+
- Added SPI speed selector to MagnTek encoders
63

74
### Changes in 1.16:
85

@@ -19,4 +16,8 @@ Internal changes:
1916
- Using analog VREF for voltage sensing (better accuracy with unstable 3.3V)
2017
- Added chip temperature readout
2118
- Added remote CAN button/analog source mainclass
22-
- Added exponential torque postprocessing for game effects
19+
- Added exponential torque postprocessing for game effects
20+
- Reformatted USB serial string as hex and added command to request UID as hex string
21+
- Added device name to USB Product name
22+
- Added support for F407 OTP section
23+
- Added support for MagnTek MT6835 via SPI (SPI3 port, MagnTek encoder class)

Firmware/FFBoard/Inc/constants.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* For more settings see target_constants.h in a target specific folder
99
*/
1010

11-
static const uint8_t SW_VERSION_INT[3] = {1,16,5}; // Version as array. 8 bit each!
11+
static const uint8_t SW_VERSION_INT[3] = {1,16,6}; // Version as array. 8 bit each!
1212
#ifndef MAX_AXIS
1313
#define MAX_AXIS 2 // ONLY USE 2 for now else screws HID Reports
1414
#endif

Firmware/FFBoard/UserExtensions/Inc/MtEncoderSPI.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,12 @@
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
22+
cspin,pos,errors,mode,speed
2323
};
2424
enum class MtEncoderSPI_mode : uint8_t{
2525
mt6825,mt6835
2626
};
27+
const std::array<float,3> spispeeds = {10e6,5e6,2.5e6}; // Target speeds. Must double each entry
2728
public:
2829
MtEncoderSPI();
2930
virtual ~MtEncoderSPI();
@@ -57,6 +58,8 @@ class MtEncoderSPI: public Encoder, public SPIDevice, public PersistentStorage,
5758

5859
//bool useDMA = false; // if true uses DMA for angle updates instead of polling SPI. TODO when used with tmc external encoder using DMA will hang the interrupt randomly
5960

61+
void setSpiSpeed(uint8_t preset);
62+
6063
private:
6164
uint8_t readSpi(uint16_t addr);
6265
void writeSpi(uint16_t addr,uint8_t data);
@@ -85,6 +88,8 @@ class MtEncoderSPI: public Encoder, public SPIDevice, public PersistentStorage,
8588

8689
static std::array<uint8_t,256> tableCRC;
8790
const uint8_t POLY = 0x07;
91+
92+
uint8_t spiSpeedPreset = 0;
8893
};
8994

9095
#endif /* USEREXTENSIONS_SRC_MTENCODERSPI_H_ */

Firmware/FFBoard/UserExtensions/Src/MtEncoderSPI.cpp

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ std::array<uint8_t,256> MtEncoderSPI::tableCRC __attribute__((section (".ccmram"
2323

2424
MtEncoderSPI::MtEncoderSPI() : SPIDevice(ENCODER_SPI_PORT,ENCODER_SPI_PORT.getFreeCsPins()[0]), CommandHandler("mtenc",CLSID_ENCODER_MTSPI,0),cpp_freertos::Thread("MTENC",256,42) {
2525
MtEncoderSPI::inUse = true;
26-
this->spiConfig.peripheral.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; // 4 = 10MHz 8 = 5MHz
26+
this->spiConfig.peripheral.BaudRatePrescaler = spiPort.getClosestPrescaler(10e6).first; // 10MHz max
2727
this->spiConfig.peripheral.FirstBit = SPI_FIRSTBIT_MSB;
2828
this->spiConfig.peripheral.CLKPhase = SPI_PHASE_2EDGE;
2929
this->spiConfig.peripheral.CLKPolarity = SPI_POLARITY_HIGH;
@@ -32,14 +32,15 @@ MtEncoderSPI::MtEncoderSPI() : SPIDevice(ENCODER_SPI_PORT,ENCODER_SPI_PORT.getFr
3232
//Init CRC-8 table
3333
makeCrcTable(tableCRC,POLY,8); // Mt6825, Poly X8+X2+X (+1)
3434

35-
restoreFlash();
35+
restoreFlash(); // Also configures SPI port
3636
spiPort.reserveCsPin(this->spiConfig.cs);
3737

3838
CommandHandler::registerCommands();
3939
registerCommand("cs", MtEncoderSPI_commands::cspin, "CS pin",CMDFLAG_GET | CMDFLAG_SET);
4040
registerCommand("pos", MtEncoderSPI_commands::pos, "Position",CMDFLAG_GET | CMDFLAG_SET);
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);
43+
registerCommand("speed", MtEncoderSPI_commands::speed, "SPI speed preset",CMDFLAG_GET | CMDFLAG_SET | CMDFLAG_INFOSTRING);
4344
this->Start();
4445
}
4546

@@ -51,14 +52,16 @@ MtEncoderSPI::~MtEncoderSPI() {
5152
void MtEncoderSPI::restoreFlash(){
5253
uint16_t conf_int = Flash_ReadDefault(ADR_MTENC_CONF1, 0);
5354
offset = Flash_ReadDefault(ADR_MTENC_OFS, 0) << 2;
54-
uint8_t cspin = conf_int & 0xF;
55+
uint8_t cspin = conf_int & 0x3;
5556
MtEncoderSPI_mode mode = static_cast<MtEncoderSPI_mode>(conf_int >> 8);
5657
setMode(mode);
5758
setCsPin(cspin);
59+
setSpiSpeed((conf_int >> 2) & 0x3);
5860
}
5961

6062
void MtEncoderSPI::saveFlash(){
61-
uint16_t conf_int = this->cspin & 0xF;
63+
uint16_t conf_int = this->cspin & 0x3;
64+
conf_int |= (this->spiSpeedPreset & 0x3) << 2;
6265
conf_int |= ((uint8_t)mode & 0xf) << 8;
6366
Flash_Write(ADR_MTENC_CONF1, conf_int);
6467
Flash_Write(ADR_MTENC_OFS, offset >> 2);
@@ -262,6 +265,15 @@ void MtEncoderSPI::setMode(MtEncoderSPI::MtEncoderSPI_mode mode){
262265
this->overspeed = false;
263266
}
264267

268+
void MtEncoderSPI::setSpiSpeed(uint8_t preset){
269+
if(preset == spiSpeedPreset){
270+
return; // Ignore if no change
271+
}
272+
spiSpeedPreset = clip<uint8_t,uint8_t>(preset,0,spispeeds.size());
273+
this->spiConfig.peripheral.BaudRatePrescaler = spiPort.getClosestPrescaler(spispeeds[spiSpeedPreset]).first;
274+
initSPI();
275+
}
276+
265277

266278
CommandStatus MtEncoderSPI::command(const ParsedCommand& cmd,std::vector<CommandReply>& replies){
267279
switch(static_cast<MtEncoderSPI_commands>(cmd.cmdId)){
@@ -298,6 +310,22 @@ CommandStatus MtEncoderSPI::command(const ParsedCommand& cmd,std::vector<Command
298310
return CommandStatus::ERR;
299311
}
300312
break;
313+
314+
case MtEncoderSPI_commands::speed:
315+
{
316+
if(cmd.type == CMDtype::get){
317+
replies.emplace_back(spiSpeedPreset);
318+
}else if(cmd.type == CMDtype::set){
319+
setSpiSpeed(cmd.val);
320+
}else if(cmd.type == CMDtype::info){
321+
for(uint8_t i = 0; i<spispeeds.size();i++){
322+
replies.emplace_back(std::to_string(this->spiPort.getClosestPrescaler(spispeeds[i]).second) + ":" + std::to_string(i)+"\n");
323+
}
324+
}else{
325+
return CommandStatus::ERR;
326+
}
327+
break;
328+
}
301329
default:
302330
return CommandStatus::NOT_FOUND;
303331
}

0 commit comments

Comments
 (0)