forked from slightlynybbled/bootypic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootloader.h
196 lines (161 loc) · 4.8 KB
/
bootloader.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#ifndef _BOOTLOADER_H
#define _BOOTLOADER_H
#include "boot_user.h"
/** @brief the version of the transmission protocol
*/
#define VERSION_STRING "0.1"
#define TX_BUF_LEN ((MAX_PROG_SIZE * 4) + 0x10)
#define RX_BUF_LEN ((MAX_PROG_SIZE * 4) + 0x10)
/**
* @brief the byte that indicates the start of a frame
*/
#define START_OF_FRAME 0xf7
/**
* @brief the byte that indicates the end of a frame
*/
#define END_OF_FRAME 0x7f
/**
* @brief the escape byte, which indicates that the following byte will be
* XORed with the ESC_XOR byte before being transmitted
*/
#define ESC 0xf6
/**
* @brief the value to use to escape characters
*/
#define ESC_XOR 0x20
/**
* @brief commands available for the bootloader
*/
typedef enum{
/* textual commands */
CMD_READ_PLATFORM = 0x00,
CMD_READ_VERSION = 0x01,
CMD_READ_ROW_LEN = 0x02,
CMD_READ_PAGE_LEN = 0x03,
CMD_READ_PROG_LEN = 0x04,
CMD_READ_MAX_PROG_SIZE = 0x05,
CMD_READ_APP_START_ADDR = 0x06,
CMD_READ_BOOT_START_ADDR = 0x07,
/* erase operations */
CMD_ERASE_PAGE = 0x10,
/* flash read memory operations */
CMD_READ_ADDR = 0x20,
CMD_READ_MAX = 0x21,
/* flash write operations */
CMD_WRITE_ROW = 0x30,
CMD_WRITE_MAX_PROG_SIZE = 0x31,
/* application */
CMD_START_APP = 0x40
}CommCommand;
/**
* @brief initializes the pins
*/
void initPins(void);
/**
* @brief receives data from the UART
*/
void receiveBytes(void);
/**
* @brief processes the rx buffer into commands with parameters from packets
*/
void processReceived(void);
/**
* @brief processes commands
* @param data byte buffer of data that has been stripped of transmission
* protocols
*/
void processCommand(uint8_t* data);
/**
* @brief send the start byte and initialize fletcher checksum accumulator
*/
void txStart(void);
/**
* @brief receive a single UART byte, assigning it to outbyte
* @return true if successful
*/
extern bool tryRxByte(uint8_t *outbyte);
/**
* @brief transmits a single byte, escaping if necessary, along with
* accumulating the fletcher checksum
* @param byte a byte of data to transmit
*/
void txByte(uint8_t byte);
/**
* @brief convenience function for transmitting an array of bytes with the
* associated command
* @param cmd the type of message
* @param bytes an array of bytes to transmit
* @param len the number of bytes to transmit from the array
*/
void txBytes(uint8_t cmd, uint8_t* bytes, uint16_t len);
/**
* @brief convenience function for transmitting an array of bytes with the
* associated command
* @param cmd the type of message
* @param bytes an array of bytes to transmit
* @param len the number of bytes to transmit from the array
*/
void txArray8bit(uint8_t cmd, uint8_t* bytes, uint16_t len);
/**
* @brief convenience function for transmitting an array of 16-bit words
* with the associated command
* @param cmd the type of message
* @param bytes an array of 16-bit words to transmit
* @param len the number of bytes to transmit from the array
*/
void txArray16bit(uint8_t cmd, uint16_t* words, uint16_t len);
/**
* @brief convenience function for transmitting an array of 32-bit words
* with the associated command
* @param cmd the type of message
* @param bytes an array of 32-bit words to transmit
* @param len the number of bytes to transmit from the array
*/
void txArray32bit(uint8_t cmd, uint32_t* words, uint16_t len);
/**
* @brief convenience function for transmitting a string
* @param cmd the type of message
* @param str a pointer to a string buffer to transmit
*/
void txString(uint8_t cmd, char* str);
/**
* @brief appends the checksum, properly escaping the sequence where necessary,
* and sends the end byte
*/
void txEnd(void);
/**
* @brief accumulates a single byte into the running accumulator
* @param byte the byte to accumulate
* @return the current fletcher16 value
*/
uint16_t fletcher16Accum(uint8_t byte);
/**
* @brief calculate the fletcher16 value of an array given the array pointer
* and length
* @param data an 8-bit array pointer
* @param length the length of the array
* @return the fletcher16 value
*/
uint16_t fletcher16(uint8_t* data, uint16_t length);
/**
* @brief starts the application
* @param applicationAddress
*/
void startApp(uint16_t applicationAddress);
/**
* @brief initializes the oscillator
*/
extern void initOsc(void);
/**
* @brief initializes the pins
*/
extern void initPins(void);
/**
* @brief initializes the UART
*/
extern void initUart(void);
/**
* @brief initializes timers for bootloader timeout
*/
extern void initTimers(void);
#endif