15 .name =
"MagnTek SPI" ,
16 .id=CLSID_ENCODER_MTSPI,
26 this->
spiConfig.peripheral.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
27 this->
spiConfig.peripheral.FirstBit = SPI_FIRSTBIT_MSB;
28 this->
spiConfig.peripheral.CLKPhase = SPI_PHASE_2EDGE;
29 this->
spiConfig.peripheral.CLKPolarity = SPI_POLARITY_HIGH;
54 uint8_t
cspin = conf_int & 0xF;
61 uint16_t conf_int = this->
cspin & 0xF;
62 conf_int |= ((uint8_t)
mode & 0xf) << 8;
76 int overflowLim =
getCpr() >> 1;
96 this->cspin = std::min<uint8_t>(
spiPort.getCsPins().size(),
cspin);
114 uint8_t
txbuf[2] = {(uint8_t)(addr | 0x80),0};
115 uint8_t
rxbuf[2] = {0,0};
118 uint8_t
txbuf[3] = {(uint8_t)((addr & 0xf00) | 0x30),(uint8_t)(addr & 0xff),0};
119 uint8_t
rxbuf[3] = {0,0,0};
128 uint8_t
txbuf[2] = {(uint8_t)(addr & 0xff),
data};
131 uint8_t
txbuf[3] = {(uint8_t)((addr & 0xf00) | 0x60),(uint8_t)(addr & 0xff),
data};
160 uint8_t txbufNew[5] = {0x03 | 0x80,0,0,0,0};
164 uint8_t txbufNew[6] = {0xA0,0x03,0,0,0,0};
173 bool parity_ok =
false;
176 uint32_t angle17_10 =
rxbuf[1];
177 uint32_t angle9_4 =
rxbuf[2];
178 uint32_t angle3_0 =
rxbuf[3];
181 uint8_t pc1 = angle17_10 ^ angle17_10 >> 1;
182 pc1 = pc1 ^ pc1 >> 2;
183 pc1 = pc1 ^ pc1 >> 4;
185 uint8_t pc1_2 = angle9_4 ^ angle9_4 >> 1;
186 pc1_2 = pc1_2 ^ pc1_2 >> 2;
187 pc1_2 = pc1_2 ^ pc1_2 >> 4;
190 angle3_0 = angle3_0 >> 2;
191 uint8_t pc2 = (angle3_0) ^ (angle3_0) >> 1;
192 pc2 = pc2 ^ pc2 >> 2;
193 pc2 = pc2 ^ pc2 >> 4;
195 nomag = (angle9_4 & 0x02) >> 1;
197 angle9_4 = (angle9_4 & 0xFC) >> 2;
198 angle3_0 = angle3_0 >> 2;
201 parity_ok = !(pc2 & 1) && ((pc1 & 1) == (pc1_2 & 1));
203 curAngleInt = (angle17_10 << 10) | (angle9_4 << 4) | (angle3_0);
206 uint32_t angle20_13 =
rxbuf[2];
207 uint32_t angle12_5 =
rxbuf[3];
208 uint32_t angle4_0 = (
rxbuf[4] & 0xF8) >> 3;
210 uint8_t status =
rxbuf[4] & 0x7;
211 nomag = (status & 0x02) >> 1;
215 curAngleInt = (angle20_13 << 13) | (angle12_5 << 5) | (angle4_0);
217 parity_ok = calccrc ==
crc;
270 replies.emplace_back(this->
cspin+1);
280 replies.emplace_back(
getPos());
288 replies.emplace_back(
errors);
292 replies.emplace_back((uint8_t)
mode);
296 replies.emplace_back(
"MT6825:0\nMT6835:1");
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
void writeSpi(uint16_t addr, uint8_t data)
void setMode(MtEncoderSPI_mode mode)
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)