summaryrefslogtreecommitdiff
path: root/zbootloader.h
blob: 2d1aff6a82188d128b29df831ad626fc72fd4e51 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#ifndef ZBOOTLOADER_H
#define ZBOOTLOADER_H

#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <assert.h>

#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
};

enum error {
    ERR_TIMEOUT = -1
};

#define LEN(x) ((size_t)(sizeof(x) / sizeof(x[0])))

#define FIFO_LEN 32
#define MAX_PACKET_SIZE 256
#define TIMEOUT_MS 500
#define MAX_ATTEMPTS 3
#define MAX_TRANS_ATTEMPTS 5

// static_assert(FIFO_LEN % 2 == 0, "FIFO_LEN is not a power of 2");

uint16_t
crc16(const void *buf, size_t len);

#endif // ZBOOTLOADER_H