mirror of
https://github.com/markqvist/RNode_Firmware.git
synced 2025-08-07 05:52:20 -04:00
Implement Serial
This commit is contained in:
parent
d01a4b293f
commit
04c8523619
3 changed files with 55 additions and 18 deletions
8
LoRa.cpp
8
LoRa.cpp
|
@ -125,8 +125,10 @@ int LoRaClass::begin(long frequency)
|
||||||
// start SPI
|
// start SPI
|
||||||
SPI.begin();
|
SPI.begin();
|
||||||
#elif LIBRARY_TYPE == LIBRARY_C
|
#elif LIBRARY_TYPE == LIBRARY_C
|
||||||
_fd = open("/dev/spidev0.0", O_RDWR);
|
const char* spi_filename = "/dev/spidev0.0";
|
||||||
if (_fd < 0) {
|
std::cerr << "Opening SPI device " << spi_filename << std::endl;
|
||||||
|
_fd = open(spi_filename, O_RDWR);
|
||||||
|
if (_fd <= 0) {
|
||||||
perror("could not open SPI device");
|
perror("could not open SPI device");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -703,6 +705,8 @@ uint8_t ISR_VECT LoRaClass::singleTransfer(uint8_t address, uint8_t value)
|
||||||
|
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
|
std::cerr << "Access SPI at " << _fd << std::endl;
|
||||||
|
|
||||||
// Configure SPI speed and mode to match settings
|
// Configure SPI speed and mode to match settings
|
||||||
status = ioctl(_fd, SPI_IOC_WR_MODE, &_spiSettings.mode);
|
status = ioctl(_fd, SPI_IOC_WR_MODE, &_spiSettings.mode);
|
||||||
if (status < 0) {
|
if (status < 0) {
|
||||||
|
|
2
Makefile
2
Makefile
|
@ -152,4 +152,4 @@ obj/RNode_Firmware.o: RNode_Firmware.ino Utilities.h Config.h LoRa.h ROM.h Frami
|
||||||
|
|
||||||
bin/rnode: obj/RNode_Firmware.o obj/LoRa.o obj/MD5.o
|
bin/rnode: obj/RNode_Firmware.o obj/LoRa.o obj/MD5.o
|
||||||
mkdir -p bin
|
mkdir -p bin
|
||||||
$(CC) -o $@ $^ -lstdc++
|
$(CC) -o $@ $^ -lstdc++ -lutil
|
||||||
|
|
63
Utilities.h
63
Utilities.h
|
@ -10,6 +10,9 @@
|
||||||
|
|
||||||
#if LIBRARY_TYPE == LIBRARY_C
|
#if LIBRARY_TYPE == LIBRARY_C
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <poll.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <pty.h>
|
||||||
// We need a delay()
|
// We need a delay()
|
||||||
void delay(int ms) {
|
void delay(int ms) {
|
||||||
struct timespec interval;
|
struct timespec interval;
|
||||||
|
@ -40,39 +43,69 @@
|
||||||
// We also need a Serial
|
// We also need a Serial
|
||||||
class SerialClass {
|
class SerialClass {
|
||||||
public:
|
public:
|
||||||
const char* fifoPath = "rnode_socket";
|
|
||||||
void begin(int baud) {
|
void begin(int baud) {
|
||||||
int status = mkfifo(fifoPath, 0666);
|
int other_end = 0;
|
||||||
|
int status = openpty(&_fd, &other_end, NULL, NULL, NULL);
|
||||||
if (status) {
|
if (status) {
|
||||||
perror("Making fifo failed");
|
perror("could not open PTY");
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
// TODO: Need a bidirectional thing here: openpty???
|
|
||||||
_fd = open(fifoPath, O_RDWR);
|
|
||||||
if (_fd < 0) {
|
|
||||||
perror("could not open fifo");
|
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "Listening on " << ttyname(other_end) << std::endl;
|
||||||
}
|
}
|
||||||
// Be truthy if connected
|
|
||||||
operator bool() {
|
operator bool() {
|
||||||
return _fd > 0;
|
return _fd > 0;
|
||||||
}
|
}
|
||||||
void write(int b) {
|
void write(int b) {
|
||||||
ssize_t written = ::write(_fd,
|
uint8_t to_write = b;
|
||||||
|
ssize_t written = ::write(_fd, &to_write, 1);
|
||||||
|
while (written != 1) {
|
||||||
|
if (written < 0) {
|
||||||
|
perror("could not write to PTY");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
written = ::write(_fd, &to_write, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
void write(const char* data) {
|
void write(const char* data) {
|
||||||
throw std::runtime_error("Unimplemented");
|
while(*data) {
|
||||||
|
write(*data);
|
||||||
|
++data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
bool available() {
|
bool available() {
|
||||||
throw std::runtime_error("Unimplemented");
|
struct pollfd request;
|
||||||
|
request.fd = _fd;
|
||||||
|
request.events = POLLIN;
|
||||||
|
request.revents = 0;
|
||||||
|
|
||||||
|
int result = poll(&request, 1, 0);
|
||||||
|
|
||||||
|
if (result == -1) {
|
||||||
|
perror("could not poll");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result > 0;
|
||||||
}
|
}
|
||||||
uint8_t read() {
|
uint8_t read() {
|
||||||
throw std::runtime_error("Unimplemented");
|
uint8_t buffer;
|
||||||
|
ssize_t count = ::read(_fd, &buffer, 1);
|
||||||
|
while (count != 1) {
|
||||||
|
if (count < 0) {
|
||||||
|
perror("could not read from PTY");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
count = ::read(_fd, &buffer, 1);
|
||||||
|
}
|
||||||
|
return buffer;
|
||||||
}
|
}
|
||||||
protected:
|
protected:
|
||||||
int _fd = -1;
|
int _fd;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
SerialClass Serial;
|
SerialClass Serial;
|
||||||
|
|
||||||
// And random(below);
|
// And random(below);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue