Brian Silverman | 003a473 | 2018-03-11 14:02:15 -0700 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_SPI_RX_CLEARER_H_ |
| 2 | #define FRC971_WPILIB_SPI_RX_CLEARER_H_ |
| 3 | |
| 4 | #include <stdint.h> |
| 5 | |
| 6 | namespace frc971 { |
| 7 | namespace wpilib { |
| 8 | |
| 9 | // Allows clearing the RX FIFO of the roboRIO's SPI peripheral on demand. This |
| 10 | // is necessary to work around a driver bug. See |
| 11 | // https://docs.google.com/document/d/1ANV4LtnVcku2fk84Y31pIIqrxUD_x_dYyCQgp10NB0k/edit |
| 12 | // for details. |
| 13 | class SpiRxClearer { |
| 14 | public: |
| 15 | SpiRxClearer(); |
| 16 | ~SpiRxClearer(); |
| 17 | |
| 18 | // Actually clears the RX FIFO. This should be very fast. Don't do this while |
| 19 | // any operations via the kernel driver are ongoing. |
| 20 | void ClearRxFifo(); |
| 21 | |
| 22 | private: |
| 23 | // How big of a mapping we do. |
| 24 | static constexpr uint32_t kMappingSize = 0x1000; |
| 25 | |
| 26 | // The physical base address of the SPI instance we're going to mess with. |
| 27 | const uint32_t spi_peripheral_base_ = 0xe0006000; |
| 28 | volatile uint32_t *mapping_; |
| 29 | |
| 30 | uint32_t ReadRegister(uint32_t offset) { return mapping_[offset / 4]; } |
| 31 | void WriteRegister(uint32_t offset, uint32_t value) { |
| 32 | mapping_[offset / 4] = value; |
| 33 | } |
| 34 | |
| 35 | bool RxFifoIsEmpty() { return !(ReadRegister(4) & (1 << 4)); } |
| 36 | }; |
| 37 | |
| 38 | } // namespace wpilib |
| 39 | } // namespace frc971 |
| 40 | |
| 41 | #endif // FRC971_WPILIB_SPI_RX_CLEARER_H_ |