blob: 733cf6fef3b543fa60154620f97844387692ff3f [file] [log] [blame]
Brian Silverman003a4732018-03-11 14:02:15 -07001#ifndef FRC971_WPILIB_SPI_RX_CLEARER_H_
2#define FRC971_WPILIB_SPI_RX_CLEARER_H_
3
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cstdint>
Brian Silverman003a4732018-03-11 14:02:15 -07005
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08006namespace frc971::wpilib {
Brian Silverman003a4732018-03-11 14:02:15 -07007
8// Allows clearing the RX FIFO of the roboRIO's SPI peripheral on demand. This
9// is necessary to work around a driver bug. See
10// https://docs.google.com/document/d/1ANV4LtnVcku2fk84Y31pIIqrxUD_x_dYyCQgp10NB0k/edit
11// for details.
12class SpiRxClearer {
13 public:
14 SpiRxClearer();
15 ~SpiRxClearer();
16
17 // Actually clears the RX FIFO. This should be very fast. Don't do this while
18 // any operations via the kernel driver are ongoing.
19 void ClearRxFifo();
20
21 private:
22 // How big of a mapping we do.
23 static constexpr uint32_t kMappingSize = 0x1000;
24
25 // The physical base address of the SPI instance we're going to mess with.
26 const uint32_t spi_peripheral_base_ = 0xe0006000;
27 volatile uint32_t *mapping_;
28
29 uint32_t ReadRegister(uint32_t offset) { return mapping_[offset / 4]; }
30 void WriteRegister(uint32_t offset, uint32_t value) {
31 mapping_[offset / 4] = value;
32 }
33
34 bool RxFifoIsEmpty() { return !(ReadRegister(4) & (1 << 4)); }
35};
36
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080037} // namespace frc971::wpilib
Brian Silverman003a4732018-03-11 14:02:15 -070038
Tyler Chatowbf0609c2021-07-31 16:13:27 -070039#endif // FRC971_WPILIB_SPI_RX_CLEARER_H_