-
Notifications
You must be signed in to change notification settings - Fork 0
/
FUJICOM.C
139 lines (104 loc) · 2.35 KB
/
FUJICOM.C
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
/**
* #FUJINET Low Level Routines
*/
#include "com.h"
#include "fujicom.h"
PORT *port;
void fujicom_init(unsigned char p)
{
int base=0x3f8, i=12;
switch(p)
{
default:
case 1:
base = 0x3f8;
i = 12;
break;
case 2:
base = 0x2f8;
i = 11;
break;
}
port = port_open(base,i);
port_set(port,9600,'N',8,1);
}
unsigned char fujicom_cksum(unsigned char *buf, unsigned short len)
{
unsigned int chk = 0;
int i=0;
for (i=0;i<len;i++)
chk = ((chk+buf[i]) >> 8) + ((chk + buf[i]) & 0xFF);
return (unsigned char)chk;
}
/**
* @brief Internal function, send command, get response.
*
* @param c ptr to command frame to send
* @return 'A'ck, or 'N'ak.
*/
char _fujicom_send_command(cmdFrame_t *c)
{
int i=-1;
unsigned char *cc = (unsigned char *)c;
/* Calculate checksum and place in frame */
c->dcksum = fujicom_cksum(cc,4);
/* Assert DTR to indicate start of command frame */
port_set_dtr(port,1);
/* Write command frame */
port_put(port,cc,sizeof(cmdFrame_t));
/* Desert DTR to indicate end of command frame */
port_set_dtr(port,0);
i=port_getc_sync(port);
return (unsigned char)i;
}
char fujicom_command(cmdFrame_t *c)
{
_fujicom_send_command(c);
return port_getc_sync(port);
}
char fujicom_command_read(cmdFrame_t *c, unsigned char *buf, unsigned short len)
{
int r; /* response */
int i;
r = _fujicom_send_command(c);
if (r == 'N')
return r; /* Return NAK */
/* Get COMPLETE/ERROR */
r = port_getc_sync(port);
if (r == 'C')
{
/* Complete, get payload */
for (i=0;i<len;i++)
{
buf[i]=port_getc_sync(port);
}
/* Get Checksum byte, we don't use it. */
port_getc_sync(port);
}
/* Otherwise, we got an error, return it. */
return (unsigned char)r;
}
char fujicom_command_write(cmdFrame_t *c, unsigned char *buf, unsigned short len)
{
unsigned char r; /* response */
int i;
unsigned char ck;
r = _fujicom_send_command(c);
if (r == 'N')
return r;
/* Write the payload */
port_put(port,buf,len);
/* Write the checksum */
ck=fujicom_cksum(buf,len);
port_put(port,&ck,1);
/* Wait for ACK/NACK */
r = port_getc_sync(port);
if (r == 'N')
return r;
/* Wait for COMPLETE/ERROR */
return port_getc_sync(port);
}
void fujicom_done(void)
{
port_close(port);
}