Open FFBoard
Open source force feedback firmware
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#define MAGNTEK_READ 0x80
20
22 enum class MtEncoderSPI_commands : uint32_t{
24 };
25public:
27 virtual ~MtEncoderSPI();
28
29 static bool inUse;
32 static bool isCreatable() {return ext3_spi.getFreeCsPins().size() > 0 && !inUse;};
33
35 void Run();
36
37 void restoreFlash() override;
38 void saveFlash() override;
39
40 int32_t getPos() override;
41 uint32_t getCpr() override; // Encoder counts per rotation
42
43 int32_t getPosAbs() override;
44
45 void setPos(int32_t pos);
46
47 void initSPI();
48 void updateAngleStatus();
50
51 CommandStatus command(const ParsedCommand& cmd,std::vector<CommandReply>& replies);
52 void setCsPin(uint8_t cspin);
53
54 //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
55
56private:
57 uint8_t readSpi(uint8_t addr);
58 void writeSpi(uint8_t addr,uint8_t data);
59 void spiTxRxCompleted(SPIPort* port);
60
61
62 bool nomag = false; // Magnet lost in last report
63 bool overspeed = false; // Overspeed flag set in last report
64 int32_t lastAngleInt = 0;
65 int32_t curAngleInt = 0;
66 int32_t curPos = 0;
67 int32_t rotations = 0;
68 int32_t offset = 0;
69 uint8_t cspin = 0;
70 bool updateInProgress = false;
71 uint32_t errors = 0;
72
73
74 uint8_t txbuf[4] = {0x03 | MAGNTEK_READ,0,0,0};
75 uint8_t rxbuf[4] = {0,0,0,0};
76 uint8_t rxbuf_t[4] = {0,0,0,0};
79};
80
81#endif /* USEREXTENSIONS_SRC_MTENCODERSPI_H_ */
82#endif
CommandStatus
EncoderType
Definition: Encoder.h:27
int32_t getPos() override
virtual ~MtEncoderSPI()
static bool isCreatable()
Definition: MtEncoderSPI.h:32
bool updateInProgress
Definition: MtEncoderSPI.h:70
uint8_t rxbuf[4]
Definition: MtEncoderSPI.h:75
uint32_t getCpr() override
cpp_freertos::BinarySemaphore requestNewDataSem
Definition: MtEncoderSPI.h:77
void saveFlash() override
static bool inUse
Definition: MtEncoderSPI.h:29
cpp_freertos::BinarySemaphore waitForUpdateSem
Definition: MtEncoderSPI.h:78
int32_t getPosAbs() override
uint8_t cspin
Definition: MtEncoderSPI.h:69
void setPos(int32_t pos)
int32_t curPos
Definition: MtEncoderSPI.h:66
void writeSpi(uint8_t addr, uint8_t data)
int32_t curAngleInt
Definition: MtEncoderSPI.h:65
const ClassIdentifier getInfo()
Command handlers always have class infos. Works well with ChoosableClass.
void restoreFlash() override
uint32_t errors
Definition: MtEncoderSPI.h:71
EncoderType getEncoderType()
Definition: MtEncoderSPI.h:34
void updateAngleStatus()
int32_t rotations
Definition: MtEncoderSPI.h:67
int32_t lastAngleInt
Definition: MtEncoderSPI.h:64
void spiTxRxCompleted(SPIPort *port)
int32_t offset
Definition: MtEncoderSPI.h:68
uint8_t txbuf[4]
Definition: MtEncoderSPI.h:74
uint8_t readSpi(uint8_t addr)
bool updateAngleStatusCb()
void setCsPin(uint8_t cspin)
static ClassIdentifier info
Definition: MtEncoderSPI.h:30
CommandStatus command(const ParsedCommand &cmd, std::vector< CommandReply > &replies)
uint8_t rxbuf_t[4]
Definition: MtEncoderSPI.h:76
Definition: SPI.h:100
Definition: SPI.h:43
std::vector< OutputPin > & getFreeCsPins()
Definition: SPI.cpp:55
SPIPort ext3_spi
static struct @612 data