From 47319963e97cb380e6d6126e81b7bf5101bf63cf Mon Sep 17 00:00:00 2001 From: jaseg Date: Fri, 2 Jun 2023 09:19:22 +0200 Subject: [PATCH] add usb proto files --- include/proto_usb.h | 31 +++++++++++++++++++++++++++ src/proto_usb.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 include/proto_usb.h create mode 100644 src/proto_usb.c diff --git a/include/proto_usb.h b/include/proto_usb.h new file mode 100644 index 0000000..811532c --- /dev/null +++ b/include/proto_usb.h @@ -0,0 +1,31 @@ +#ifndef __PROTO_USB_H__ +#define __PROTO_USB_H__ + +enum usbp_pkt_type { + USBP_GET_STATUS = 0, + USBP_GET_MEASUREMENTS = 1, +}; + +struct __attribute__((__packed__)) usbp_status_pkt { + uint8_t packet_type; + uint64_t sys_time_us; + uint8_t has_lcd; + uint8_t has_adc; + uint8_t board_config; + uint8_t bus_addr; + ErrorCode last_uart_error; + uint64_t last_uart_error_timestamp; + uint64_t last_uart_rx; + uint64_t last_uart_tx; + ErrorCode last_bus_error; + uint64_t last_bus_error_timestamp; +}; + +struct __attribute__((__packed__)) usbp_measurement_pkt { + uint8_t packet_type; + uint8_t num_channels; + int32_t measurements[2][2][64]; +}; + + +#endif /* __PROTO_USB_H__ */ diff --git a/src/proto_usb.c b/src/proto_usb.c new file mode 100644 index 0000000..d6cfd5f --- /dev/null +++ b/src/proto_usb.c @@ -0,0 +1,51 @@ + +#include +#include +#include +#include +#include + +void handle_usb_packet(void *packet, size_t len) { + switch (*(uint8_t *)packet) { + case USBP_GET_STATUS: { + struct usbp_status_pkt response = { + .packet_type = USBP_GET_STATUS, + .sys_time_us = sys_time_us, + .has_adc = st_adc.has_adc, + .has_lcd = st_lcd.has_lcd, + .board_config = board_config, + .bus_addr = uart_st.addr, + .last_uart_error = uart_st.error, + .last_uart_error_timestamp = uart_st.last_error, + .last_uart_rx = uart_st.last_packet, + .last_uart_tx = uart_st.last_transmission, + .last_bus_error = st_proto.error, + .last_bus_error_timestamp = st_proto.last_error, + }; + usb_write_response(&response, sizeof(response)); + break; + } + + case USBP_GET_MEASUREMENTS: { + struct usbp_measurement_pkt response = { + .packet_type = USBP_GET_MEASUREMENTS, + .num_channels = 4, + }; + + for (size_t board=0; board=2; board++) { + + size_t src_idx = adc_sp_rd_idx[board]; + for (size_t i=0; i