summaryrefslogtreecommitdiff
path: root/flash/uart_boot/client.c
blob: 71749a2c5535a5453425de4a4597981abf66214d (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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
// client.cpp : functions for monitor download and communication.
//

#include <stdio.h>
#include <stdlib.h>
#include "scalar_types.h" // (U)INT8/16/32
#include "Uart.h" // platform abstraction for UART
#include "minimon.h" // protocol of my little monitor

// do the baudrate configuration for the Player
int ConfigFirstlevelPlayer (tUartHandle serial_handle)
{
	UINT32 result_nbr;

	if(!UartConfig(serial_handle, 4800, eMARKPARITY, eTWOSTOPBITS, 8))
	{
		UINT32 dwErr = GET_LAST_ERR();
		printf("Error %lu setting up COM params for baudrate byte\n", dwErr);
		exit(1);
	}

	// this will read as 0x19 when viewed with 2300 baud like the player does
	result_nbr = UartWrite(serial_handle, (UINT8*)"\x86\xC0", 2);
	if (result_nbr != 2)
	{
		UINT32 dwErr = GET_LAST_ERR();
		printf("Error %lu setting up COM params for baudrate byte\n", dwErr);
	}

	SLEEP(100); // wait for the chars to be sent, is there a better way?

	// the read 0x19 means 14423 baud with 12 MHz
	if(!UartConfig(serial_handle, 14400, eNOPARITY, eONESTOPBIT, 8))
	{
		printf("Error setting up COM params for 1st level loader\n");
		exit(1);
	}

	return 0;
}


// do the baudrate configuration for the Recoder/FM
int ConfigFirstlevelRecorder (tUartHandle serial_handle)
{
	UINT32 result_nbr;

	if(!UartConfig(serial_handle, 4800, eNOPARITY, eTWOSTOPBITS, 8))
	{
		UINT32 dwErr = GET_LAST_ERR();
		printf("Error %lu setting up COM params for baudrate byte\n", dwErr);
		exit(1);
	}

	// this will read as 0x08 when viewed with 2120 baud like the recorder does
	result_nbr = UartWrite(serial_handle, (UINT8*)"\x00\x00", 2);
	if(result_nbr != 2)
	{
		printf("Error transmitting baudrate byte\n");
		exit(1);
	}

	SLEEP(100); // wait for the chars to be sent, is there a better way?

	// the read 0x08 means 38400 baud with 11.0592 MHz
	if(!UartConfig(serial_handle, 38400, eNOPARITY, eONESTOPBIT, 8))
	{
		UINT32 dwErr = GET_LAST_ERR();
		printf("Error %lu setting up COM params for 1st level loader\n", dwErr);
		exit(1);
	}

	return 0;
}


// transfer a byte for the monitor download, with or without acknowledge
int DownloadByte(tUartHandle serial_handle, unsigned char byte, bool bAck)
{
	unsigned char received;

	while (1)
	{
		UartWrite(serial_handle, &byte, 1);
		if (bAck)
		{
			UartRead(serial_handle, &received, 1);
			if (received == byte)
			{
				UartWrite(serial_handle, (UINT8*)"\x01", 1); // ack success
				break; // exit the loop
			}
			else
			{
				printf("Error transmitting monitor byte 0x%02X, got 0x%0X\n", byte, received);
				UartWrite(serial_handle, (UINT8*)"\x00", 1); // ack fail, try again
			}
		}
		else
			break; // no loop
	}
	return 1;
}


// download our little monitor, the box must have been just freshly switched on for this to work
int DownloadMonitor(tUartHandle serial_handle, bool bRecorder, char* szFilename)
{
	FILE* pFile;
	size_t filesize;
	UINT8 byte;
	unsigned i;

	// hard-coded parameters
	bool bAck = true; // configure if acknowledged download (without useful for remote pin boot)
	UINT32 TargetLoad = 0x0FFFF000; // target load address

	pFile = fopen(szFilename, "rb");
	if (pFile == NULL)
	{
		printf("\nMonitor file %s not found, exiting\n", szFilename);
		exit(1);
	}

	// determine file size
	fseek(pFile, 0, SEEK_END);
	filesize = ftell(pFile);
	fseek(pFile, 0, SEEK_SET);

	// This is _really_ tricky! The box expects a BRR value in a nonstandard baudrate,
	//	which a PC can't generate. I'm using a higher one with some wild settings
	//	to generate a pulse series that:
	//	1) looks like a stable byte when sampled with the nonstandard baudrate
	//	2) gives a BRR value to the box which results in a baudrate the PC can also use
	if (bRecorder)
	{
		ConfigFirstlevelRecorder(serial_handle);
	}
	else
	{
		ConfigFirstlevelPlayer(serial_handle);
	}

	UartWrite(serial_handle, bAck ? (UINT8*)"\x01" : (UINT8*)"\x00", 1); // ACK mode

	// transmit the size, little endian
	DownloadByte(serial_handle, (UINT8)( filesize	   & 0xFF), bAck);
	DownloadByte(serial_handle, (UINT8)((filesize>>8)  & 0xFF), bAck);
	DownloadByte(serial_handle, (UINT8)((filesize>>16) & 0xFF), bAck);
	DownloadByte(serial_handle, (UINT8)((filesize>>24) & 0xFF), bAck);

	// transmit the load address, little endian
	DownloadByte(serial_handle, (UINT8)( TargetLoad 	 & 0xFF), bAck);
	DownloadByte(serial_handle, (UINT8)((TargetLoad>>8)  & 0xFF), bAck);
	DownloadByte(serial_handle, (UINT8)((TargetLoad>>16) & 0xFF), bAck);
	DownloadByte(serial_handle, (UINT8)((TargetLoad>>24) & 0xFF), bAck);

	// transmit the command byte
	DownloadByte(serial_handle, 0xFF, bAck); // 0xFF means execute the transferred image

	// transmit the image
	for (i=0; i<filesize; i++)
	{
		fread(&byte, 1, 1, pFile);
		DownloadByte(serial_handle, byte, bAck);
	}

	fclose (pFile);

	// now the image should have been started, red LED off

	return 0;
}


// wait for a fixed string to be received (no foolproof algorithm,
//	may overlook if the searched string contains repeatitions)
int WaitForString(tUartHandle serial_handle, char* pszWait)
{
	int i = 0;
	unsigned char received;
	
	while(pszWait[i] != '\0')
	{
		UartRead(serial_handle, &received, 1);

		printf("%c", received); // debug

		if (received == pszWait[i])
			i++; // continue
		else 
			i=0; // mismatch, start over
	}
	return 0;
}


// send a sting and check the echo
int SendWithEcho(tUartHandle serial_handle, char* pszSend)
{
	int i = 0;
	unsigned char received;
	
	while(pszSend[i] != '\0')
	{
		UartWrite(serial_handle, (unsigned char*)(pszSend + i), 1); // send char
		do
		{
			UartRead(serial_handle, &received, 1); // receive echo
			printf("%c", received); // debug
		}
		while (received != pszSend[i]); // should normally be equal
		i++; // next char
	}
	return 0;
}


// rarely used variant: download our monitor using the built-in Archos monitor
int DownloadArchosMonitor(tUartHandle serial_handle, char* szFilename)
{
	FILE* pFile;
	size_t filesize;
	UINT8 byte;
	UINT16 checksum = 0;
	unsigned i;

	// the onboard monitor uses 115200 baud
	if(!UartConfig(serial_handle, 115200, eNOPARITY, eONESTOPBIT, 8))
	{
		UINT32 dwErr = GET_LAST_ERR();
		printf("Error %lu setting up COM params for baudrate %d\n", dwErr, 115200);
		exit(1);
	}

	// wait for receiving "#SERIAL#"
	WaitForString(serial_handle, "#SERIAL#");
	
	// send magic "SRL" command to get interactive mode
	SendWithEcho(serial_handle, "SRL\r");
	
	// wait for menu completion: "ROOT>" at the end
	WaitForString(serial_handle, "ROOT>");
	
	// send upload command "UP"
	SendWithEcho(serial_handle, "UP\r");
	
	pFile = fopen(szFilename, "rb");
	if (pFile == NULL)
	{
		printf("\nMonitor file %s not found, exiting\n", szFilename);
		exit(1);
	}

	// determine file size
	fseek(pFile, 0, SEEK_END);
	filesize = ftell(pFile);
	fseek(pFile, 0, SEEK_SET);
	
	// calculate checksum
	for (i=0; i<filesize; i++)
	{
		fread(&byte, 1, 1, pFile);
		checksum += byte;
	}
	fseek(pFile, 0, SEEK_SET);
	
	// send header

	// size as 32 bit little endian
	byte = (UINT8)( filesize	  & 0xFF);
	UartWrite(serial_handle, &byte, 1);
	byte = (UINT8)((filesize>>8)  & 0xFF);
	UartWrite(serial_handle, &byte, 1);
	byte = (UINT8)((filesize>>16) & 0xFF);
	UartWrite(serial_handle, &byte, 1);
	byte = (UINT8)((filesize>>24) & 0xFF);
	UartWrite(serial_handle, &byte, 1);

	// checksum as 16 bit little endian
	byte = (UINT8)( checksum	  & 0xFF);
	UartWrite(serial_handle, &byte, 1);
	byte = (UINT8)((checksum>>8)  & 0xFF);
	UartWrite(serial_handle, &byte, 1);
	
	UartWrite(serial_handle, (unsigned char*)"\x00", 1); // kind (3 means flash)
	UartWrite(serial_handle, (unsigned char*)"\x00", 1); // ignored byte

	// wait for monitor to accept data
	WaitForString(serial_handle, "#OKCTRL#");
	
	// transmit the image
	for (i=0; i<filesize; i++)
	{
		fread(&byte, 1, 1, pFile);
		UartWrite(serial_handle, &byte, 1); // payload
	}
	fclose (pFile);
	
	UartWrite(serial_handle, (unsigned char*)"\x00", 1); // ignored byte

	// wait for menu completion: "ROOT>" at the end
	WaitForString(serial_handle, "ROOT>");

	// send start program command "SPRO"
	SendWithEcho(serial_handle, "SPRO\r");

	SLEEP(100); // wait a little while for startup

	return 0;
}


/********** Target functions using the Monitor Protocol **********/

// read a byte using the target monitor
UINT8 ReadByte(tUartHandle serial_handle, UINT32 addr)
{
	UINT8 send;
	UINT8 received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	// send the read command
	send = BYTE_READ;
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response

	return received;
}


// write a byte using the target monitor
int WriteByte(tUartHandle serial_handle, UINT32 addr, UINT8 byte)
{
	UINT8 send;
	UINT8 received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error, receiced 0x%02X!\n", received);
		return 1;
	}

	// send the write command
	send = BYTE_WRITE;
	UartWrite(serial_handle, &send, 1);

	// transmit the data
	UartWrite(serial_handle, &byte, 1);
	
	UartRead(serial_handle, &received, 1); // response

	if (received != BYTE_WRITE)
	{
		printf("Protocol error!\n");
		return 1;
	}

	return 0;
}


// read many bytes using the target monitor
int ReadByteMultiple(tUartHandle serial_handle, UINT32 addr, UINT32 size, UINT8* pBuffer)
{
	UINT8 send, received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	while (size)
	{
		if (size >= 16)
		{	// we can use a "burst" command
			send = BYTE_READ16;
			UartWrite(serial_handle, &send, 1); // send the read command
			UartRead(serial_handle, pBuffer, 16); // data response
			pBuffer += 16;
			size -= 16;
		}
		else
		{	// use single byte command
			send = BYTE_READ;
			UartWrite(serial_handle, &send, 1); // send the read command
			UartRead(serial_handle, pBuffer++, 1); // data response
			size--;
		}
	}

	return 0;
}


// write many bytes using the target monitor
int WriteByteMultiple(tUartHandle serial_handle, UINT32 addr, UINT32 size, UINT8* pBuffer)
{
	UINT8 send, received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	while (size)
	{
		if (size >= 16)
		{	// we can use a "burst" command
			send = BYTE_WRITE16;
			UartWrite(serial_handle, &send, 1); // send the write command
			UartWrite(serial_handle, pBuffer, 16); // transmit the data
			UartRead(serial_handle, &received, 1); // response
			if (received != BYTE_WRITE16)
			{
				printf("Protocol error!\n");
				return 1;
			}
			pBuffer += 16;
			size -= 16;
		}
		else
		{	// use single byte command
			send = BYTE_WRITE;
			UartWrite(serial_handle, &send, 1); // send the write command
			UartWrite(serial_handle, pBuffer++, 1); // transmit the data
			UartRead(serial_handle, &received, 1); // response
			if (received != BYTE_WRITE)
			{
				printf("Protocol error!\n");
				return 1;
			}
			size--;
		}
	}

	return 0;
}


// write many bytes using the target monitor
int FlashByteMultiple(tUartHandle serial_handle, UINT32 addr, UINT32 size, UINT8* pBuffer)
{
	UINT8 send, received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	while (size)
	{
		if (size >= 16)
		{	// we can use a "burst" command
			send = BYTE_FLASH16;
			UartWrite(serial_handle, &send, 1); // send the write command
			UartWrite(serial_handle, pBuffer, 16); // transmit the data
			UartRead(serial_handle, &received, 1); // response
			if (received != BYTE_FLASH16)
			{
				printf("Protocol error!\n");
				return 1;
			}
			pBuffer += 16;
			size -= 16;
		}
		else
		{	// use single byte command
			send = BYTE_FLASH;
			UartWrite(serial_handle, &send, 1); // send the write command
			UartWrite(serial_handle, pBuffer++, 1); // transmit the data
			UartRead(serial_handle, &received, 1); // response
			if (received != BYTE_FLASH)
			{
				printf("Protocol error!\n");
				return 1;
			}
			size--;
		}
	}

	return 0;
}


// read a 16bit halfword using the target monitor
UINT16 ReadHalfword(tUartHandle serial_handle, UINT32 addr)
{
	UINT8 send;
	UINT8 received;
	UINT16 halfword;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	// send the read command
	send = HALFWORD_READ;
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	halfword = received << 8; // highbyte
	UartRead(serial_handle, &received, 1);
	halfword |= received; // lowbyte

	return halfword;
}


// write a 16bit halfword using the target monitor
int WriteHalfword(tUartHandle serial_handle, UINT32 addr, UINT16 halfword)
{
	UINT8 send;
	UINT8 received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	// send the write command
	send = HALFWORD_WRITE;
	UartWrite(serial_handle, &send, 1);

	// transmit the data
	send = halfword >> 8; // highbyte
	UartWrite(serial_handle, &send, 1);
	send = halfword & 0xFF; // lowbyte
	UartWrite(serial_handle, &send, 1);
	
	UartRead(serial_handle, &received, 1); // response

	if (received != HALFWORD_WRITE)
	{
		printf("Protocol error!\n");
		return 1;
	}

	return 0;
}


// change baudrate using target monitor
int SetTargetBaudrate(tUartHandle serial_handle, long lClock, long lBaudrate)
{
	UINT8 send;
	UINT8 received;
	UINT8 brr;
	long lBRR;

	lBRR = lClock / lBaudrate;
	lBRR = ((lBRR + 16) / 32) - 1; // with rounding
	brr = (UINT8)lBRR;

	// send the command
	send = BAUDRATE;
	UartWrite(serial_handle, &send, 1);
	UartWrite(serial_handle, &brr, 1); // send the BRR value
	UartRead(serial_handle, &received, 1); // response ack

	if (received != BAUDRATE)
	{	// bad situation, now we're unclear about the baudrate of the target
		printf("Protocol error!\n");
		return 1;
	}

	SLEEP(100); // give it some time to settle

	// change our baudrate, too
	UartConfig(serial_handle, lBaudrate, eNOPARITY, eONESTOPBIT, 8); 

	return 0;
}


// call a subroutine using the target monitor
int Execute(tUartHandle serial_handle, UINT32 addr, bool bReturns)
{
	UINT8 send;
	UINT8 received;

	// send the address command
	send = ADDRESS;
	UartWrite(serial_handle, &send, 1);

	// transmit the address, big endian
	send = (UINT8)((addr>>24) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>16) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)((addr>>8) & 0xFF);
	UartWrite(serial_handle, &send, 1);
	send = (UINT8)(addr & 0xFF);
	UartWrite(serial_handle, &send, 1);

	UartRead(serial_handle, &received, 1); // response
	if (received != ADDRESS)
	{
		printf("Protocol error!\n");
		return 1;
	}

	// send the execute command
	send = EXECUTE;
	UartWrite(serial_handle, &send, 1);
	if (bReturns)
	{	// we expect the call to return control to minimon
		UartRead(serial_handle, &received, 1); // response

		if (received != EXECUTE)
		{
			printf("Protocol error!\n");
			return 1;
		}
	}

	return 0;
}