Open FFBoard
Open source force feedback firmware
Loading...
Searching...
No Matches
MtEncoderSPI.h
Go to the documentation of this file.
1/*
2 * MtEncoderSPI.h
3 *
4 * Created on: 02.11.2021
5 * Author: Yannick
6 */
7
8#ifndef USEREXTENSIONS_SRC_MTENCODERSPI_H_
9#define USEREXTENSIONS_SRC_MTENCODERSPI_H_
10#include "constants.h"
11#ifdef MTENCODERSPI
12#include "SPI.h"
13#include "cpp_target_config.h"
14#include "Encoder.h"
15#include "PersistentStorage.h"
16#include "CommandHandler.h"
17#include "thread.hpp"
18
19
21 enum class MtEncoderSPI_commands : uint32_t{
23 };
24 enum class MtEncoderSPI_mode : uint8_t{
26 };
27public:
29 virtual ~MtEncoderSPI();
30
31 static bool inUse;
32 static ClassIdentifier info;
34 static bool isCreatable() {return ext3_spi.getFreeCsPins().size() > 0 && !inUse;};
35
37 void Run();
38
39 void restoreFlash() override;
40 void saveFlash() override;
41
42 int32_t getPos() override;
43 uint32_t getCpr() override; // Encoder counts per rotation
44
45 int32_t getPosAbs() override;
46
47 void setPos(int32_t pos);
48
49 void initSPI();
50 void updateAngleStatus();
52
53 CommandStatus command(const ParsedCommand& cmd,std::vector<CommandReply>& replies);
54 void setCsPin(uint8_t cspin);
55
57
58 //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
59
60private:
61 uint8_t readSpi(uint16_t addr);
62 void writeSpi(uint16_t addr,uint8_t data);
63 void spiTxRxCompleted(SPIPort* port);
64
65
66 bool nomag = false; // Magnet lost in last report
67 bool overspeed = false; // Overspeed flag set in last report
68 int32_t lastAngleInt = 0;
69 int32_t curAngleInt = 0;
70 int32_t curPos = 0;
71 int32_t rotations = 0;
72 int32_t offset = 0;
73 uint8_t cspin = 0;
74 bool updateInProgress = false;
75 uint32_t errors = 0;
76
77
78 uint8_t txbuf[6] = {0};
79 uint8_t rxbuf[6] = {0};
80 uint8_t rxbuf_t[6] = {0};
83
85
86 static std::array<uint8_t,256> tableCRC;
87 const uint8_t POLY = 0x07;
88};
89
90#endif /* USEREXTENSIONS_SRC_MTENCODERSPI_H_ */
91#endif
CommandStatus
EncoderType
Definition Encoder.h:27
CommandHandler(const char *clsname, uint16_t clsid, uint8_t instance=0)
Encoder()
Definition Encoder.cpp:18
int32_t getPos() override
virtual ~MtEncoderSPI()
MtEncoderSPI_mode mode
static bool isCreatable()
bool updateInProgress
uint8_t rxbuf_t[6]
uint8_t rxbuf[6]
uint32_t getCpr() override
cpp_freertos::BinarySemaphore requestNewDataSem
void saveFlash() override
static bool inUse
cpp_freertos::BinarySemaphore waitForUpdateSem
uint8_t readSpi(uint16_t addr)
int32_t getPosAbs() override
uint8_t cspin
void setPos(int32_t pos)
const uint8_t POLY
void writeSpi(uint16_t addr, uint8_t data)
int32_t curPos
void setMode(MtEncoderSPI_mode mode)
int32_t curAngleInt
const ClassIdentifier getInfo()
void restoreFlash() override
uint32_t errors
EncoderType getEncoderType()
void updateAngleStatus()
int32_t rotations
int32_t lastAngleInt
void spiTxRxCompleted(SPIPort *port)
int32_t offset
bool updateAngleStatusCb()
void setCsPin(uint8_t cspin)
static ClassIdentifier info
uint8_t txbuf[6]
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
static std::array< uint8_t, 256 > tableCRC
SPIDevice(SPIPort &port, OutputPin csPin)
Definition SPI.cpp:332
Definition SPI.h:43
SPIPort ext3_spi
static struct @024127060247016123033304002117326322243354210111 data