diff --git a/quad/Makefile b/quad/Makefile index 0c73163a280352c721413ac4f56e284ce420d9a8..5b25b37be9cca2c8cdb93df1af8de5f4021f861c 100644 --- a/quad/Makefile +++ b/quad/Makefile @@ -6,7 +6,7 @@ WS = $(CURDIR)/xsdk_workspace 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 @@ -31,6 +31,9 @@ gen_diagram: boot: $(BOOT) +run-virt-quad: + $(MAKE) -C src/virt_quad run + test: $(MAKE) -C src/queue test $(MAKE) -C src/computation_graph test @@ -47,7 +50,6 @@ deep-clean: $(MAKE) -C src/graph_blocks clean $(MAKE) -C src/commands clean $(MAKE) -C src/quad_app clean - bash scripts/xsdk/clean_xsdk_workspace.sh $(OUTDIR): mkdir $(OUTDIR) diff --git a/quad/executable.mk b/quad/executable.mk index 0f5f6435227675076c41ae9cdafe964c1bdec8f6..9a5cce878ecb1e0cc55d209dc6acf3c7ec2e6396 100644 --- a/quad/executable.mk +++ b/quad/executable.mk @@ -23,7 +23,7 @@ CLEANUP = $(TARGET) $(OBJDIR) default: $(TARGET) run: $(TARGET) - $(EXEDIR)/$(NAME) + cd $(EXEDIR) && ./$(NAME) clean: rm -rf $(CLEANUP) diff --git a/quad/scripts/tests/test_unix_uart.py b/quad/scripts/tests/test_unix_uart.py new file mode 100755 index 0000000000000000000000000000000000000000..760f545a150849fe6131bd783e2ac9b9fef0e77b --- /dev/null +++ b/quad/scripts/tests/test_unix_uart.py @@ -0,0 +1,54 @@ +#!/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) diff --git a/quad/src/quad_app/callbacks.c b/quad/src/quad_app/callbacks.c index 8acc3cc7c77cf97b3e3d4365d8de546baa382779..cb99d83ad9d108e297121666df94159e96fbaedd 100644 --- a/quad/src/quad_app/callbacks.c +++ b/quad/src/quad_app/callbacks.c @@ -36,8 +36,11 @@ int cb_debug(struct modular_structs *structs, struct metadata *meta, unsigned ch * counts the number of packet logs. */ int cb_packetlog(struct modular_structs* structs, struct metadata *meta, u8 *data, u16 length) { + char buf[64]; n_msg_received += 1; 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; } diff --git a/quad/src/virt_quad/hw_impl_unix_pwm_output.c b/quad/src/virt_quad/hw_impl_unix_pwm_output.c index 3bd757f96d1c146d5bd3e1724dea0e2fe8222618..94ad65f0fb860260c4206bd4210e5913af9f09f1 100644 --- a/quad/src/virt_quad/hw_impl_unix_pwm_output.c +++ b/quad/src/virt_quad/hw_impl_unix_pwm_output.c @@ -29,7 +29,7 @@ int unix_pwm_output_write(struct PWMOutputDriver *self, int fifo = open(output_pwms[channel], O_WRONLY | O_NONBLOCK); if (fifo >= 0) { sprintf(buff, "%d\0", pulse_width_us); - int bytes_read = write(fifo, buff, strlen(buff)); + write(fifo, buff, strlen(buff)); } close(fifo); return 0; diff --git a/quad/src/virt_quad/hw_impl_unix_uart.c b/quad/src/virt_quad/hw_impl_unix_uart.c index 27bdfcb8d2c0271ea8a9f4d0c22e131519e866ef..5790bb2f4968ecbdddbcd23570c38feb444e1368 100644 --- a/quad/src/virt_quad/hw_impl_unix_uart.c +++ b/quad/src/virt_quad/hw_impl_unix_uart.c @@ -1,70 +1,42 @@ #include "hw_impl_unix.h" -#include <sys/types.h> -#include <sys/socket.h> #include <stdio.h> -#include <sys/un.h> -#include <sys/ioctl.h> -#include <err.h> -#include <netinet/in.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <fcntl.h> -#define DEFAULT_SOCKET "../../groundStation/virtquad.socket" -#define SOCKET_ENV "VIRT_QUAD_SOCKET" - -static int backendSocket; -static int client; +static char *fifo_name_rx; +static char *fifo_full_name_rx; +static char *fifo_full_name_tx; +static int fifo_rx; int unix_uart_reset(struct UARTDriver *self) { - char * backend_socket_path = DEFAULT_SOCKET; - if (getenv(SOCKET_ENV)) { - backend_socket_path = getenv(SOCKET_ENV); - } - - /* 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); + fifo_name_rx = "uart-rx"; + fifo_full_name_rx = VIRT_QUAD_FIFOS_DIR "/uart-rx"; + fifo_full_name_tx = VIRT_QUAD_FIFOS_DIR "/uart-tx"; + char fifoname[64]; + mkdir(VIRT_QUAD_FIFOS_DIR, 0777); - /* Create sockaddr and bind */ - struct sockaddr_un sa; - sa.sun_family = AF_UNIX; - 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"); - } + unlink(fifo_full_name_rx); + mkfifo(fifo_full_name_rx, 0666); + fifo_rx = open(fifo_full_name_rx, O_RDONLY | O_NONBLOCK); - /* Listen */ - if (listen(backendSocket, 1)) { - err(-1, "listen"); - } + unlink(fifo_full_name_tx); + mkfifo(fifo_full_name_tx, 0666); - 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; } int unix_uart_write(struct UARTDriver *self, unsigned char c) { - send(client, &c, 1, MSG_DONTWAIT); - return 0; + int fifo = open(fifo_full_name_tx, O_WRONLY | O_NONBLOCK); + if (fifo >= 0) { + write(fifo, &c, 1); + } + close(fifo); + return 0; } int unix_uart_read(struct UARTDriver *self, unsigned char *c) { - int bytes_available; - ioctl(client,FIONREAD,&bytes_available); - - if (bytes_available > 0) { - int bytes = recv(client, c, 1, 0); - return 0; - } else { - return 1; - } -} \ No newline at end of file + int err = read(fifo_rx, c, 1); + return err <= 0; +}