summaryrefslogtreecommitdiff
path: root/simpad-utilities/serload/serialdownload.h
blob: 1dd05892b136049e0577be853d0fb36740338e9a (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
//=============================================================================
// Project:      SIMpad
//=============================================================================
// FILE-NAME:    serialdownload.hpp
// FUNCTION:     Serial download interface.
//
// AUTHOR:       Juergen Messerer, Peter Voser
// CREAT.-DATE:  01.04.2001 (dd.mm.yy)
//
// NOTES:        -
//               
//=============================================================================

#ifndef __SERIAL_DOWNLOAD
#define __SERIAL_DOWNLOAD

#include <errno.h> 
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

const unsigned char STX     = 2;
const unsigned char ETX     = 3;
const unsigned char BEL     = 7;
const unsigned char ACK_BD  = 11;
const unsigned char ACK_OK  = 6;
const unsigned char ACK_NOK = 15;
const unsigned char FILLER  = 0xff;

class SerialDownload
{
public:
  SerialDownload();
  ~SerialDownload();
//=============================================================================
//  PURPOSE:      Opening a serial port.
//
//  PARAMETERS: 
//                portDev: (IN) port device to open.
//                errorNumber: (OUT) error number determined with 
//                             GetLastError().
//  RETURN VALUE: 
//                serialPort: Filedescriptor of opened serial port. 
//                If the function fails, it returns -1. 
//
//  COMMENTS:     -
//=============================================================================
  int openSerialPort(const char* portDev, int &errorNumber);
  
//=============================================================================
//  PURPOSE:      Loading file with a image 
//
//  PARAMETERS: 
//                fileName: (IN) name of file to open
//                buffer: (OUT) pointer to loaded image file.
//                numberOfBytes: (OUT) size of file.
//
//  RETURN VALUE: 
//                 0: success
//                -1: specified file not found
//                -2: not enough memory to load file
//                -3: cannot read file
//
//  COMMENTS:     -
//=============================================================================
  int loadFile(const char *fileName, char *&buffer, int &numberOfBytes);

//=============================================================================
//  PURPOSE:      Connecting to the SIMpad.
//
//  PARAMETERS:
//                fastBaudRate: (IN) value of fast baud rate. 
//                errorNumber: (OUT) error number errno
//
//  RETURN VALUE: 
//                0: success
//               -1: switching to connection baud rate 38400baud failed. 
//               -2: writing to serial port failed. 
//               -3: switching to fast baud rate failed. 
//               -4: writing to serial port with fast baud rate failed. 
//
//  COMMENTS:     The connection is set up according to the bootloader's 
//                serial download protocoll.
//=============================================================================
  int connectToSimpad(const int fastBaudRate, 
		      int& errorNumber);

//=============================================================================
//  PURPOSE:      Sending a block of 512byte.
//
//  PARAMETERS: 
//                b: (IN) pointer to the beginning of the 512byte buffer.
//                len: (IN) length of the buffer.
//                errorNumber: (OUT) error number determined with 
//                             GetLastError().
//  RETURN VALUE: 
//                TRUE: success
//                FALSE: error. See errorNumber for the reason.
//
//  COMMENTS:     The block, which is sent, is always 512byte long. If the 
//                buffer counts less than 512byte, the block is filled with 
//                the FILLER pattern. 
//=============================================================================
  bool sendBlock(const char *b, 
                 const int len,  
	         int& errorNumber);

//=============================================================================
//  PURPOSE:      Waiting for the end of burning.
//
//  PARAMETERS:   -
//
//  RETURN VALUE: -
//
//  COMMENTS:     -
//=============================================================================
  void waitForEndOfBurning(void);

private:
  // File descriptor of open serial port.
  int _serialPort;

//=============================================================================
//  PURPOSE:      Changing baud rate.
//
//  PARAMETERS:
//                newBaudRate: (IN) new baud rate to switch to.
//                errorNumber: (OUT) error number determined with 
//                             GetLastError().
//
//  RETURN VALUE: 
//                TRUE: success
//                FALSE: error. See errorNumber for the reason.
//
//  serialMENTS:     -
//=============================================================================
  bool changeBaudRate(const int newBaudRate, 
                      int &errorNumber);

//=============================================================================
//  PURPOSE:      Waiting for control character.
//
//  PARAMETERS: 
//                transparent: (IN) 0 = received characters are sent to 
//                                  stdout.
//
//  RETURN VALUE: 
//                c: control character.
//
//  COMMENTS:     -
//=============================================================================
  unsigned char waitForReply(const int transparent);

};
#endif // __SERIAL_DOWNLOAD