15 .name =
"MagnTek SPI" ,
16 .id=CLSID_ENCODER_MTSPI,
26 this->
spiConfig.peripheral.BaudRatePrescaler =
spiPort.getClosestPrescaler(10e6).first;
27 this->
spiConfig.peripheral.FirstBit = SPI_FIRSTBIT_MSB;
28 this->
spiConfig.peripheral.CLKPhase = SPI_PHASE_2EDGE;
29 this->
spiConfig.peripheral.CLKPolarity = SPI_POLARITY_HIGH;
57 uint8_t
cspin = conf_int & 0x3;
65 uint16_t conf_int = this->
cspin & 0x3;
67 conf_int |= ((uint8_t)
mode & 0xf) << 8;
81 int overflowLim =
getCpr() >> 1;
101 this->cspin = std::min<uint8_t>(
spiPort.getCsPins().size(),
cspin);
119 uint8_t
txbuf[2] = {(uint8_t)(addr | 0x80),0};
120 uint8_t
rxbuf[2] = {0,0};
124 uint8_t
txbuf[3] = {(uint8_t)((addr & 0xf00) | 0x30),(uint8_t)(addr & 0xff),0};
125 uint8_t
rxbuf[3] = {0,0,0};
133 uint8_t
txbuf[2] = {(uint8_t)(addr & 0xff),
data};
136 uint8_t
txbuf[3] = {(uint8_t)((addr & 0xf00) | 0x60),(uint8_t)(addr & 0xff),
data};
150 uint8_t
txbuf[3] = {0xC0,0x00,0x00};
151 uint8_t
rxbuf[3] = {0,0,0};
153 return rxbuf[2] == 0x55;
179 uint8_t txbufNew[5] = {0x03 | 0x80,0,0,0,0};
183 uint8_t txbufNew[6] = {0xA0,0x03,0,0,0,0};
192 bool parity_ok =
false;
195 uint32_t angle17_10 =
rxbuf[1];
196 uint32_t angle9_4 =
rxbuf[2];
197 uint32_t angle3_0 =
rxbuf[3];
200 uint8_t pc1 = angle17_10 ^ angle17_10 >> 1;
201 pc1 = pc1 ^ pc1 >> 2;
202 pc1 = pc1 ^ pc1 >> 4;
204 uint8_t pc1_2 = angle9_4 ^ angle9_4 >> 1;
205 pc1_2 = pc1_2 ^ pc1_2 >> 2;
206 pc1_2 = pc1_2 ^ pc1_2 >> 4;
209 angle3_0 = angle3_0 >> 2;
210 uint8_t pc2 = (angle3_0) ^ (angle3_0) >> 1;
211 pc2 = pc2 ^ pc2 >> 2;
212 pc2 = pc2 ^ pc2 >> 4;
214 nomag = (angle9_4 & 0x02) >> 1;
216 angle9_4 = (angle9_4 & 0xFC) >> 2;
217 angle3_0 = angle3_0 >> 2;
220 parity_ok = !(pc2 & 1) && ((pc1 & 1) == (pc1_2 & 1));
222 curAngleInt = (angle17_10 << 10) | (angle9_4 << 4) | (angle3_0);
225 uint32_t angle20_13 =
rxbuf[2];
226 uint32_t angle12_5 =
rxbuf[3];
227 uint32_t angle4_0 = (
rxbuf[4] & 0xF8) >> 3;
229 uint8_t status =
rxbuf[4] & 0x7;
230 nomag = (status & 0x02) >> 1;
234 curAngleInt = (angle20_13 << 13) | (angle12_5 << 5) | (angle4_0);
236 parity_ok = calccrc ==
crc;
298 replies.emplace_back(this->
cspin+1);
308 replies.emplace_back(
getPos());
316 replies.emplace_back(
errors);
320 replies.emplace_back((uint8_t)
mode);
324 replies.emplace_back(
"MT6825:0\nMT6835:1");
337 for(uint8_t i = 0; i<
spispeeds.size();i++){
338 replies.emplace_back(std::to_string(this->
spiPort.getClosestPrescaler(
spispeeds[i]).second) +
":" + std::to_string(i)+
"\n");
void makeCrcTable(std::array< T, LEN > &table, const T crcpoly, const uint8_t bits, const bool refin=false, const bool refout=false)
uint8_t calculateCrc8(std::array< uint8_t, 256 > &crctable, uint8_t *buf, uint16_t len, uint8_t crc=0)
std::array< uint8_t, 256 > MtEncoderSPI::tableCRC __attribute__((section(".ccmram")))
void registerCommand(const char *cmd, const ID cmdid, const char *help=nullptr, uint32_t flags=0)
CommandHandler(const char *clsname, uint16_t clsid, uint8_t instance=0)
int32_t getPos() override
uint32_t getCpr() override
cpp_freertos::BinarySemaphore requestNewDataSem
void saveFlash() override
cpp_freertos::BinarySemaphore waitForUpdateSem
uint8_t readSpi(uint16_t addr)
int32_t getPosAbs() override
const std::array< float, 3 > spispeeds
void writeSpi(uint16_t addr, uint8_t data)
void setMode(MtEncoderSPI_mode mode)
void setSpiSpeed(uint8_t preset)
const ClassIdentifier getInfo()
void restoreFlash() override
void spiTxRxCompleted(SPIPort *port)
bool updateAngleStatusCb()
void setCsPin(uint8_t cspin)
static ClassIdentifier info
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
static std::array< uint8_t, 256 > tableCRC
SPIDevice(SPIPort &port, OutputPin csPin)
uint32_t WaitForNotification(TickType_t Timeout=portMAX_DELAY)
Thread(const std::string Name, uint16_t StackDepth, UBaseType_t Priority)
static struct @024127060247016123033304002117326322243354210111 data
TVal Flash_ReadDefault(uint16_t adr, TVal def)
bool Flash_Write(uint16_t adr, uint16_t dat)
static void * memcpy(void *dst, const void *src, size_t n)