Skip to content
Snippets Groups Projects
Commit a1f69485 authored by bbartels's avatar bbartels
Browse files

quad: use FIFOs for the virtual quad UART

parent 7da3ec62
No related branches found
No related tags found
No related merge requests found
...@@ -6,7 +6,7 @@ WS = $(CURDIR)/xsdk_workspace ...@@ -6,7 +6,7 @@ WS = $(CURDIR)/xsdk_workspace
BOOT = $(OUTDIR)/BOOT.bin BOOT = $(OUTDIR)/BOOT.bin
.PHONY: all libs zybo boot test clean deep-clean .PHONY: all libs zybo boot run-virt-quad test clean deep-clean
all: libs bins all: libs bins
...@@ -31,6 +31,9 @@ gen_diagram: ...@@ -31,6 +31,9 @@ gen_diagram:
boot: $(BOOT) boot: $(BOOT)
run-virt-quad:
$(MAKE) -C src/virt_quad run
test: test:
$(MAKE) -C src/queue test $(MAKE) -C src/queue test
$(MAKE) -C src/computation_graph test $(MAKE) -C src/computation_graph test
...@@ -47,7 +50,6 @@ deep-clean: ...@@ -47,7 +50,6 @@ deep-clean:
$(MAKE) -C src/graph_blocks clean $(MAKE) -C src/graph_blocks clean
$(MAKE) -C src/commands clean $(MAKE) -C src/commands clean
$(MAKE) -C src/quad_app clean $(MAKE) -C src/quad_app clean
bash scripts/xsdk/clean_xsdk_workspace.sh
$(OUTDIR): $(OUTDIR):
mkdir $(OUTDIR) mkdir $(OUTDIR)
......
...@@ -23,7 +23,7 @@ CLEANUP = $(TARGET) $(OBJDIR) ...@@ -23,7 +23,7 @@ CLEANUP = $(TARGET) $(OBJDIR)
default: $(TARGET) default: $(TARGET)
run: $(TARGET) run: $(TARGET)
$(EXEDIR)/$(NAME) cd $(EXEDIR) && ./$(NAME)
clean: clean:
rm -rf $(CLEANUP) rm -rf $(CLEANUP)
......
#!/usr/bin/env python
import sys
import time
import os
import threading
path = os.path.dirname(__file__) + '/../../bin/virt-quad-fifos/'
def create_msg():
msg = bytes()
msg += b'\xBE'
msg += b'\x01'
msg += b'\x00'
msg += b'\x00'
msg += b'\x00'
msg += b'\x00'
msg += b'\x00'
print msg
checksum = 0
for b in msg:
checksum ^= b
msg += checksum.to_bytes(1, 'little')
return msg
def listen():
with open(path + 'uart-tx', 'r') as fifo:
while True:
c = fifo.read()
if c:
print c
def do_test():
# Start a listener
t = threading.Thread(target=listen)
t.daemon = True
t.start()
fifo = open(path + 'uart-rx', 'w')
fifo.write(b'\xBE\x01\x00\x00\x00\x00\x00\xBF')
fifo.write(b'\xBE\x01\x00\x00\x00\x00\x00\xBF')
fifo.write(b'\xBE\x01\x00\x00\x00\x00\x00\xBF')
if __name__ == '__main__':
for i in range(1):
print("Test ", i)
do_test()
time.sleep(1)
...@@ -36,8 +36,11 @@ int cb_debug(struct modular_structs *structs, struct metadata *meta, unsigned ch ...@@ -36,8 +36,11 @@ int cb_debug(struct modular_structs *structs, struct metadata *meta, unsigned ch
* counts the number of packet logs. * counts the number of packet logs.
*/ */
int cb_packetlog(struct modular_structs* structs, struct metadata *meta, u8 *data, u16 length) { int cb_packetlog(struct modular_structs* structs, struct metadata *meta, u8 *data, u16 length) {
char buf[64];
n_msg_received += 1; n_msg_received += 1;
total_payload_received += length; total_payload_received += length;
int len = sprintf(buf, "Packets received: %d", n_msg_received);
send_data(&structs->hardware_struct.uart, PACKETLOG_ID, 0, buf, len);
return 0; return 0;
} }
......
...@@ -29,7 +29,7 @@ int unix_pwm_output_write(struct PWMOutputDriver *self, ...@@ -29,7 +29,7 @@ int unix_pwm_output_write(struct PWMOutputDriver *self,
int fifo = open(output_pwms[channel], O_WRONLY | O_NONBLOCK); int fifo = open(output_pwms[channel], O_WRONLY | O_NONBLOCK);
if (fifo >= 0) { if (fifo >= 0) {
sprintf(buff, "%d\0", pulse_width_us); sprintf(buff, "%d\0", pulse_width_us);
int bytes_read = write(fifo, buff, strlen(buff)); write(fifo, buff, strlen(buff));
} }
close(fifo); close(fifo);
return 0; return 0;
......
#include "hw_impl_unix.h" #include "hw_impl_unix.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h> #include <stdio.h>
#include <sys/un.h> #include <sys/stat.h>
#include <sys/ioctl.h> #include <sys/types.h>
#include <err.h> #include <fcntl.h>
#include <netinet/in.h>
#define DEFAULT_SOCKET "../../groundStation/virtquad.socket" static char *fifo_name_rx;
#define SOCKET_ENV "VIRT_QUAD_SOCKET" static char *fifo_full_name_rx;
static char *fifo_full_name_tx;
static int backendSocket; static int fifo_rx;
static int client;
int unix_uart_reset(struct UARTDriver *self) { int unix_uart_reset(struct UARTDriver *self) {
char * backend_socket_path = DEFAULT_SOCKET; fifo_name_rx = "uart-rx";
if (getenv(SOCKET_ENV)) { fifo_full_name_rx = VIRT_QUAD_FIFOS_DIR "/uart-rx";
backend_socket_path = getenv(SOCKET_ENV); fifo_full_name_tx = VIRT_QUAD_FIFOS_DIR "/uart-tx";
}
/* Unlink if it exists */
unlink(backend_socket_path);
printf("using socket '%s'\n", backend_socket_path);
/* Create socket */
backendSocket = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
char fifoname[64];
mkdir(VIRT_QUAD_FIFOS_DIR, 0777);
/* Create sockaddr and bind */ unlink(fifo_full_name_rx);
struct sockaddr_un sa; mkfifo(fifo_full_name_rx, 0666);
sa.sun_family = AF_UNIX; fifo_rx = open(fifo_full_name_rx, O_RDONLY | O_NONBLOCK);
strncpy(sa.sun_path, backend_socket_path, strlen(backend_socket_path));
sa.sun_path[strlen(backend_socket_path)+1] = '\0';
if (bind(backendSocket, (struct sockaddr *) &sa, sizeof(sa))) {
err(-1, "bind");
}
/* Listen */ unlink(fifo_full_name_tx);
if (listen(backendSocket, 1)) { mkfifo(fifo_full_name_tx, 0666);
err(-1, "listen");
}
printf("Waiting for backend to connect\n");
while (1) {
client = accept(backendSocket, NULL, NULL);
if (client > 0)
break;
}
printf("backend connection found on socket %d.\n", client);
return 0; return 0;
} }
int unix_uart_write(struct UARTDriver *self, unsigned char c) { int unix_uart_write(struct UARTDriver *self, unsigned char c) {
send(client, &c, 1, MSG_DONTWAIT); int fifo = open(fifo_full_name_tx, O_WRONLY | O_NONBLOCK);
return 0; if (fifo >= 0) {
write(fifo, &c, 1);
}
close(fifo);
return 0;
} }
int unix_uart_read(struct UARTDriver *self, unsigned char *c) { int unix_uart_read(struct UARTDriver *self, unsigned char *c) {
int bytes_available; int err = read(fifo_rx, c, 1);
ioctl(client,FIONREAD,&bytes_available); return err <= 0;
}
if (bytes_available > 0) {
int bytes = recv(client, c, 1, 0);
return 0;
} else {
return 1;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment