-
Notifications
You must be signed in to change notification settings - Fork 3
/
serial.h
73 lines (61 loc) · 1.95 KB
/
serial.h
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "mobile.h"
#include "atomic.h"
#define MOBILE_MAX_DATA_SIZE 0xFF
enum mobile_serial_state {
MOBILE_SERIAL_INIT,
MOBILE_SERIAL_WAITING,
MOBILE_SERIAL_HEADER,
MOBILE_SERIAL_DATA,
MOBILE_SERIAL_DATA_PAD,
MOBILE_SERIAL_CHECKSUM,
MOBILE_SERIAL_ACKNOWLEDGE,
MOBILE_SERIAL_ACKNOWLEDGE_PAD,
MOBILE_SERIAL_IDLE_CHECK,
MOBILE_SERIAL_RESPONSE_WAITING,
MOBILE_SERIAL_RESPONSE_INIT,
MOBILE_SERIAL_RESPONSE_START,
MOBILE_SERIAL_RESPONSE_HEADER,
MOBILE_SERIAL_RESPONSE_DATA,
MOBILE_SERIAL_RESPONSE_DATA_PAD,
MOBILE_SERIAL_RESPONSE_CHECKSUM,
MOBILE_SERIAL_RESPONSE_ACKNOWLEDGE
}
#if __GNUC__ && __AVR__
// Required for AVR _Atomic (it has no libatomic).
__attribute__((packed))
#endif
;
enum mobile_serial_error {
MOBILE_SERIAL_ERROR_UNKNOWN_COMMAND = 0xF0,
MOBILE_SERIAL_ERROR_CHECKSUM,
// Returned when:
// - Transfer buffer is full and the transfer command is used
// - Current command was canceled before sending a reply due to a serial
// timeout bigger than the command's timeout (>2s), but the device
// wasn't reset yet (>3s).
MOBILE_SERIAL_ERROR_INTERNAL
};
struct mobile_buffer_serial {
enum mobile_serial_error error;
unsigned char current;
unsigned char data_size;
uint16_t checksum;
unsigned char header[4];
unsigned char footer[2];
};
struct mobile_adapter_serial {
_Atomic volatile enum mobile_serial_state state;
_Atomic volatile bool active;
unsigned char buffer[MOBILE_MAX_DATA_SIZE];
bool mode_32bit : 1;
bool device_unmetered : 1;
enum mobile_adapter_device device;
};
void mobile_serial_init(struct mobile_adapter *adapter);
uint8_t mobile_serial_transfer(struct mobile_adapter *adapter, uint8_t c);
uint32_t mobile_serial_transfer_32bit(struct mobile_adapter *adapter, uint32_t c);
#undef _Atomic // "atomic.h"