-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
468 lines (371 loc) · 9 KB
/
Copy pathmain.c
File metadata and controls
468 lines (371 loc) · 9 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
* MyBootloader.c
*
* Created: 1/4/2020 11:59:39 PM
* Author : user
*/
// TODO: debug code cleanup
// TODO: use interrupt for receive
// TODO: introduce timeouts for commands to reset to default state if no data received during that time
#define F_CPU (16000000UL)
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/boot.h>
#include <stdbool.h>
#include <util/delay.h>
// Ext High Low
// FUSES: FF D2 CE
#define BOOTLOADER_ADDRESS (0x7800)
#define MAIN_FW_ADDRESS (0)
#define SUPPORT_FW_UPDATE (0)
#define SUPPORT_GCODE_UPDATE (0)
#define BAUD_RATE_115200 (115200L)
#define BAUD_RATE_57600 (57600L)
#define BAUD_RATE_38400 (38400L)
#define FLASH_PAGE_SIZE_WORDS (64)
#define FLASH_PAGE_SIZE_BYTES (FLASH_PAGE_SIZE_WORDS * sizeof(uint16_t))
#define GCODE_MAX_SIZE_BYTES (2048)
#define GCODE_MAX_SIZE_PAGES (GCODE_MAX_SIZE_BYTES / SPM_PAGESIZE)
#define GCODE_BASE_ADDR (BOOTLOADER_ADDRESS - GCODE_MAX_SIZE_BYTES)
#define BIT(x) (1 << x)
#define BIT_GET(data, bt) (data & BIT(bt) ? 1 : 0)
#define BIT_SET(data, bt) (data |= BIT(bt))
#define BIT_CLR(data, bt) (data &= ~BIT(bt))
#define BIT_INV(data, bt) (data ^= BIT(bt))
#define PIN_VALUE(port, pin) (BIT_GET(port, pin))
#define UP (1)
#define DOWN (0)
#define PGM_PORT (PINC)
#define PGM_PIN (PORTC0)
#define LED_PORT (PORTB)
#define LED_PIN (PORTB5)
#define SET_PIN(port, pin, val) (val ? BIT_SET(port, pin) : BIT_CLR(port, pin))
#define GET_PIN(port, pin) (BIT_GET(port, pin))
#define TOGGLE_PIN(port, pin) (BIT_INV(port, pin))
#define LED_ON (BIT_SET(LED_PORT, LED_PIN))
#define LED_OFF (BIT_CLR(LED_PORT, LED_PIN))
#define LED_TOGGLE (BIT_INV(LED_PORT, LED_PIN))
#define WDT_PERIOD (15)
#define LED_TIME_DEFAULT (500 / WDT_PERIOD)
#define LED_TIME_CONNECTED (100 / WDT_PERIOD)
#define AUTO_DETECT_FEATURE (1)
#define AUTO_DETECT_TIME (100 / WDT_PERIOD)
#define BUTTON (BIT_GET(PGM_PORT, PGM_PIN))
#define ACK_CMD ("[ack]")
#define NACK_CMD ("[nak]")
#define NACK_CMD_CRC ("[nak CRC")
#define SYNC ("[MyGrbl]")
#define IDENTIFY_CMD ('I')
#define PAGE_PROGRAM_CMD ('P')
#define RESTART_CMD ('R')
#define INVALID_CMD (0xFF)
#define UNUSED_BYTE (0xFF)
// declare jump to main FW
typedef void (*p_void_func)(void);
p_void_func reset = (p_void_func) MAIN_FW_ADDRESS;
typedef struct
{
uint16_t address;
uint8_t buffer[SPM_PAGESIZE];
} page_program_cmd_packet_t;
typedef struct
{
uint8_t length;
page_program_cmd_packet_t cmd_packet;
uint16_t crc16;
} cmd_packet_t;
uint8_t uart_buffer[sizeof(cmd_packet_t)];
typedef struct
{
uint8_t ticks_15ms;
uint8_t compare;
bool done;
} gp_timer_t;
enum
{
LED_TIMER = 0,
TIMEOUT_TIMER,
SYNC_TIMER,
MAX_TIMER
};
volatile gp_timer_t gp_timer[MAX_TIMER];
void gp_timer_start(uint8_t timer, uint8_t ticks)
{
cli();
gp_timer[timer].ticks_15ms = 0;
gp_timer[timer].compare = ticks;
gp_timer[timer].done = false;
sei();
}
void gp_timer_restart(uint8_t timer)
{
cli();
gp_timer[timer].ticks_15ms = 0;
gp_timer[timer].done = false;
sei();
}
inline void gp_timer_count(void)
{
for (uint8_t timer = LED_TIMER; timer < MAX_TIMER; timer++)
{
if (++gp_timer[timer].ticks_15ms == gp_timer[timer].compare)
{
gp_timer[timer].done = true;
gp_timer[timer].ticks_15ms = 0;
}
}
}
bool gp_timer_get_rdy(uint8_t timer)
{
return gp_timer[timer].done;
}
bool gp_timer_get_clr_rdy(uint8_t timer)
{
if (gp_timer[timer].done)
{
gp_timer_restart(timer);
return true;
}
return false;
}
void wdt_init(uint8_t timeout, bool isr_en, bool rst_en)
{
uint8_t wdt_settings = (isr_en << WDIE) | (rst_en << WDE) | ((timeout > WDTO_2S) << WDP3) | (timeout & 0x07);
wdt_reset();
MCUSR &= ~(1 << WDRF);
WDTCSR |= (1 << WDCE) | (1 << WDE);
WDTCSR = wdt_settings;
}
void isr_init(bool bls)
{
MCUCR = _BV(IVCE);
MCUCR = (bls << IVSEL);
}
void clock_init(void)
{
// no external clock divider
CLKPR = BIT(CLKPCE);
CLKPR = 0;
}
void gpio_init(void)
{
// reset/abort button, input, pull-up enabled
PORTC = BIT(PORTC0);
BIT_CLR(DDRC, PORTC0);
// led pin PB5, output, off by default
DDRB = BIT(PORTB5);
PORTB = BIT(PORTB5);
}
void uart_init(uint32_t baud_rate)
{
uint16_t ubrr0_value;
// Set baud rate
if (baud_rate < BAUD_RATE_57600)
{
ubrr0_value = ((F_CPU / (8L * baud_rate)) - 1)/2 ;
UCSR0A &= ~(1 << U2X0); // baud doubler off
}
else
{
ubrr0_value = ((F_CPU / (4L * baud_rate)) - 1)/2;
UCSR0A |= (1 << U2X0); // baud doubler on for high baud rates, i.e. 115200
}
UBRR0H = ubrr0_value >> 8;
UBRR0L = ubrr0_value;
// defaults to 8-bit, no parity, 1 stop bit
UCSR0C = (3 << UCSZ00); // Asynchronous mode 8-bit data and 1-stop bit
UCSR0B = _BV(RXEN0) | _BV(TXEN0); // Enable Receiver and Transmitter
}
void uart_putch(uint8_t data)
{
// Wait for empty transmit buffer
while (!(UCSR0A & BIT(UDRE0)));
// Put data into buffer, sends the data
UDR0 = data;
}
uint8_t uart_getch(void)
{
// Wait for data to be received
while (!(UCSR0A & BIT(RXC0)));
// Get and return received data from buffer
return UDR0;
}
void uart_print(const char * str)
{
while (*str)
{
if (*str == '\n')
{
uart_putch('\r');
}
uart_putch(*str);
str++;
}
}
uint8_t get_cmd_code(const uint8_t * buffer)
{
// use simple single-byte commands for now
switch(*buffer)
{
case PAGE_PROGRAM_CMD:
case RESTART_CMD:
case IDENTIFY_CMD:
break;
default:
uart_print(NACK_CMD);
return 0;
}
return *buffer;
}
uint8_t uart_get_cmd(void)
{
uint8_t *buffer = uart_buffer;
bool start = false, cmd_received = false;
*buffer = 0;
while (true)
{
*buffer = uart_getch();
if (*buffer == '[')
{
start = true;
}
else if((*buffer == ']') && start)
{
cmd_received = true;
break;
}
else if (start)
{
buffer++;
}
}
return cmd_received ? get_cmd_code(uart_buffer) : INVALID_CMD;
}
void fill_page_buffer(uint16_t address, uint8_t * buffer)
{
uint16_t data;
for (uint8_t offset = 0; offset < SPM_PAGESIZE; offset += sizeof(uint16_t))
{
data = *buffer++;
data |= (*buffer++) << 8;
boot_page_fill(address + offset, data);
}
}
void program_page(uint16_t address, uint8_t *buffer)
{
uint8_t sreg = SREG;
cli();
eeprom_busy_wait ();
boot_page_erase (address);
boot_spm_busy_wait (); // Wait until the memory is erased.
fill_page_buffer(address, buffer);
boot_page_write (address); // Store buffer in flash page.
boot_spm_busy_wait(); // Wait until the memory is written.
boot_rww_enable ();
// Re-enable interrupts (if they were ever enabled).
SREG = sreg;
}
unsigned short Crc16(uint8_t *pcBlock, uint16_t len)
{
uint16_t crc = 0xFFFF;
uint8_t i;
while (len--)
{
crc ^= *pcBlock++ << 8;
for (i = 0; i < 8; i++)
crc = crc & 0x8000 ? (crc << 1) ^ 0x1021 : crc << 1;
}
return crc;
}
bool page_program_receive(cmd_packet_t * cmd)
{
uint8_t * buffer = (uint8_t *) &cmd->cmd_packet;
cmd->length = uart_getch();
for (uint8_t bytes = 0; bytes < cmd->length; bytes++)
{
*buffer++ = uart_getch();
}
uint8_t padding = (SPM_PAGESIZE + sizeof(uint16_t)) - cmd->length;
while (padding)
{
*buffer++ = UNUSED_BYTE;
padding--;
}
cmd->crc16 = uart_getch();
cmd->crc16 |= uart_getch() << 8;
uint16_t crc16 = Crc16((unsigned char*)&cmd->cmd_packet, cmd->length);
return (crc16 == cmd->crc16);
}
bool page_program_handle(void)
{
cmd_packet_t * cmd_ptr = (cmd_packet_t *) uart_buffer;
bool status = page_program_receive(cmd_ptr);
if (status)
{
program_page(cmd_ptr->cmd_packet.address, cmd_ptr->cmd_packet.buffer);
}
uart_print(status ? ACK_CMD : NACK_CMD_CRC);
return status;
}
void restart(void)
{
// cold reset using WDT
wdt_disable();
wdt_enable(WDTO_15MS);
while(1);
}
ISR(WDT_vect)
{
wdt_reset();
WDTCSR |= (1 << WDIE);
gp_timer_count();
if (gp_timer_get_clr_rdy(LED_TIMER))
{
LED_TOGGLE;
}
}
int main(void)
{
wdt_disable();
gpio_init();
if (BUTTON == DOWN)
{
bool connected = false;
isr_init(true);
sei();
uart_init(BAUD_RATE_115200);
wdt_init(WDTO_15MS, true, false);
#if (AUTO_DETECT_FEATURE == 1)
gp_timer_start(SYNC_TIMER, AUTO_DETECT_TIME);
#endif
gp_timer_start(LED_TIMER, LED_TIME_DEFAULT);
while (1)
{
switch (uart_get_cmd())
{
case IDENTIFY_CMD:
uart_print("[MyGrbl v1.0]");
gp_timer_start(LED_TIMER, LED_TIME_CONNECTED);
connected = true;
break;
case PAGE_PROGRAM_CMD:
page_program_handle();
break;
case RESTART_CMD:
restart();
break;
default:
break;
}
#if (AUTO_DETECT_FEATURE == 1)
if (!connected && gp_timer_get_clr_rdy(SYNC_TIMER))
{
uart_print(SYNC);
}
#endif
}
isr_init(false);
}
reset();
}