blob: fda8d7cc12e4180a11e5a07d697a8d7dfd81abd4 [file] [log] [blame]
Brian Silverman003a4732018-03-11 14:02:15 -07001#include "frc971/wpilib/spi_rx_clearer.h"
2
3#include <fcntl.h>
4#include <inttypes.h>
5#include <sys/mman.h>
6#include <unistd.h>
7
8#include "aos/common/logging/logging.h"
9
10namespace frc971 {
11namespace wpilib {
12
13SpiRxClearer::SpiRxClearer() {
14 const int fd = PCHECK(open("/dev/mem", O_RDWR));
15 void *const mmap_result = mmap(nullptr, kMappingSize, PROT_READ | PROT_WRITE,
16 MAP_SHARED, fd, spi_peripheral_base_);
17 if (mmap_result == MAP_FAILED) {
18 PLOG(FATAL, "mmap the SPI peripheral from /dev/mem failed\n");
19 }
20 PCHECK(close(fd));
21 mapping_ = static_cast<volatile uint32_t *>(mmap_result);
22}
23
24SpiRxClearer::~SpiRxClearer() {
25 PCHECK(munmap(const_cast<uint32_t *>(mapping_), kMappingSize));
26}
27
28void SpiRxClearer::ClearRxFifo() {
29 // The FIFO has 128 entries, so if we do that many reads and don't get it all,
30 // something weird is going on.
31 for (int i = 0; i < 128; ++i) {
32 if (RxFifoIsEmpty()) {
33 // If there's nothing more, we're done.
34 return;
35 }
36 // Read the next byte.
37 LOG(DEBUG, "Read from RX FIFO: %" PRIx32 "\n", ReadRegister(0x20));
38 }
39 LOG(FATAL, "Failed to clear the RX FIFO\n");
40}
41
42} // namespace wpilib
43} // namespace frc971