diff options
author | Thomas Albers <thomas@thomaslabs.org> | 2023-08-06 10:01:18 +0200 |
---|---|---|
committer | Thomas Albers <thomas@thomaslabs.org> | 2023-08-06 10:01:18 +0200 |
commit | b50e1f74202e18ea10f1f2cd46e9e02a9d4f0e65 (patch) | |
tree | 3b408615595bf82c2ac4f6ec62009ab2f05a6bd5 /zup.h | |
parent | 56157d2cc9385e5df65ba310abef2873429c8af5 (diff) |
Rewrite for new serial protocol
Diffstat (limited to 'zup.h')
-rw-r--r-- | zup.h | 128 |
1 files changed, 128 insertions, 0 deletions
@@ -0,0 +1,128 @@ +#ifndef ZUP_H +#define ZUP_H + +#include <stdio.h> +#include <stdarg.h> +#include <stdint.h> +#include <stddef.h> +#include <assert.h> + +#include <termios.h> + +#define LEN(x) (sizeof(x) / sizeof(x[0])) + +enum error { + ERR_TIMEOUT = -1, + ERR_READ = -2, + ERR_WRITE = -3, + ERR_NACK = -4, + ERR_ARGS = -5 +}; + +struct param { + char port[32]; + speed_t baud; + unsigned int repl : 1; + unsigned int human_readable : 1; + unsigned int verbose : 2; +}; + +#ifdef __GNUC__ +#define PACKED __attribute__((packed)) +#else +#define PACKED +#endif + +enum header_type { + CMD_PING, + CMD_INFO, + CMD_BOOT, + CMD_READ, + CMD_WRITE, + CMD_IO_READ, + CMD_IO_WRITE, + CMD_ECHO +}; + +struct header { + uint8_t type; + uint8_t bank; + uint16_t address; + uint16_t length; + uint16_t checksum; +} PACKED; + +static_assert(sizeof(struct header) == 8, "struct header is not PACKED"); + +enum ack { + ACK = 0x00, + NACK = 0xFF +}; + +static inline int +max(int x, int y) +{ + return x > y ? x : y; +} + +static inline int +min(int x, int y) +{ + return x > y ? y : x; +} + +extern int verbose; + +static inline void +msg(int level, const char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + if (verbose >= level) { + fprintf(stderr, "DEBUG [%d]: ", level); + vfprintf(stderr, fmt, ap); + } + va_end(ap); +} + +uint16_t +crc16(const void *buf, size_t len); + +int +z_boot(int fd, uint8_t bank, uint16_t address); + +int +z_read(int fd, uint8_t bank, uint16_t address, uint16_t length, uint8_t *buf); + +int +z_write(int fd, uint8_t bank, uint16_t address, uint16_t length, + const uint8_t *buf); + +int +z_echo(int fd, uint16_t length, uint8_t *buf); + +int +open_tty(const char *port, int baud); + +int +run_commands(int fd, const struct param *param, int ncmd, char **cmds); + +void +repl(int fd, struct param *param); + +void +print_error(int error); + +void +hexdump(size_t start_address, size_t len, const uint8_t *buf); + +#define MAX_ARGS 5 +#define MAX_PACKET_SIZE 256 +#define MAX_ATTEMPTS 3 +#define MAX_TRANS_ATTEMPTS 5 + +#define VERSION "1.0.0" + +#define DEBUG_MODE 1 + +#endif // ZUP_H |