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
|
/*
* OpenPSU firmware
* External UART control interface
*
* Copyright (C) 2007 Michael Buesch <m@bues.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "ext_control.h"
#include "calibration.h"
#include "util.h"
#include "main.h"
#include <avr/interrupt.h>
#include <stdint.h>
#define BAUDRATE 2400
/* The receive buffer. */
static uint8_t rx_buffer[sizeof(struct extctl_command)];
/* The number of bytes received. */
static uint8_t nr_rx_bytes;
/* Receive error flag */
static uint8_t rx_error;
static void usart_tx(uint8_t data)
{
while (!(UCSR0A & (1 << UDRE0)))
;
UDR0 = data;
}
static void usart_tx_buffer(void *_buf, uint8_t size)
{
uint8_t *buf = _buf;
uint8_t i;
for (i = 0; i < size; i++)
usart_tx(buf[i]);
}
static int8_t usart_rx(uint8_t *data)
{
uint8_t status;
status = UCSR0A;
if (!(status & (1 << RXC0)))
return 1;
*data = UDR0;
if (unlikely(status & ((1 << FE0) | (1 << UPE0) | (1 << DOR0))))
return -1;
return 0;
}
static uint8_t crc8(uint8_t crc, uint8_t data)
{
uint8_t i, tmp;
for (i = 8; i > 0; i--) {
tmp = ((crc ^ data) & 0x01);
if (tmp) {
crc ^= 0x18;
crc >>= 1;
crc |= 0x80;
} else
crc >>= 1;
data >>= 1;
}
return crc;
}
/* Create an XOR checksum out of the buffer. */
static uint8_t checksum_buffer(const void *_buf, uint8_t size)
{
uint8_t crc = 0;
const uint8_t *buf = _buf;
uint8_t i;
for (i = 0; i < size; i++)
crc = crc8(crc, buf[i]);
crc ^= 0xFF;
return crc;
}
static inline void command_result(uint8_t result_code)
{
usart_tx(result_code);
}
static void send_command_reply(uint32_t data)
{
struct extctl_reply reply;
reply.data = cpu_to_le32(data);
reply.checksum = checksum_buffer(&reply.data, sizeof(reply.data));
usart_tx_buffer(&reply, sizeof(reply));
}
static void cmd_nop(uint32_t data)
{
command_result(EXTCTL_CMD_RESULT_OK);
}
static void cmd_setvoltage(uint32_t data)
{
uint16_t voltage;
uint8_t profile;
profile = (data & 0x00FF0000) >> 16;
voltage = (data & 0x0000FFFF);
if ((profile >= NR_PROFILES) ||
(voltage > MAX_VOLTAGE)) {
command_result(EXTCTL_CMD_RESULT_EINVAL);
return;
}
set_voltage_in_prof(profile, voltage);
command_result(EXTCTL_CMD_RESULT_OK);
}
static void cmd_getvoltage(uint32_t data)
{
uint8_t profile;
uint16_t voltage;
profile = (data & 0x00FF0000) >> 16;
if (profile >= NR_PROFILES) {
command_result(EXTCTL_CMD_RESULT_EINVAL);
return;
}
command_result(EXTCTL_CMD_RESULT_OK);
voltage = get_voltage_from_prof(profile);
send_command_reply(voltage);
}
static void cmd_setmaxcur(uint32_t data)
{
uint16_t maxcur;
uint8_t profile;
profile = (data & 0x00FF0000) >> 16;
maxcur = (data & 0x0000FFFF);
if ((profile >= NR_PROFILES) ||
(maxcur > MAX_CURRENT)) {
command_result(EXTCTL_CMD_RESULT_EINVAL);
return;
}
set_maxcur_in_prof(profile, maxcur);
command_result(EXTCTL_CMD_RESULT_OK);
}
static void cmd_getmaxcur(uint32_t data)
{
uint8_t profile;
uint16_t maxcur;
profile = (data & 0x00FF0000) >> 16;
if (profile >= NR_PROFILES) {
command_result(EXTCTL_CMD_RESULT_EINVAL);
return;
}
command_result(EXTCTL_CMD_RESULT_OK);
maxcur = get_maxcur_from_prof(profile);
send_command_reply(maxcur);
}
static void cmd_switchprof(uint32_t data)
{
uint8_t profile;
profile = (data & 0x00FF0000) >> 16;
if (profile >= NR_PROFILES) {
command_result(EXTCTL_CMD_RESULT_EINVAL);
return;
}
switch_to_profile(profile);
command_result(EXTCTL_CMD_RESULT_OK);
}
static void cmd_getprof(uint32_t data)
{
uint8_t profile;
command_result(EXTCTL_CMD_RESULT_OK);
profile = get_active_profile();
send_command_reply((uint32_t)profile << 16);
}
/* We received a complete command. Handle it. */
static void handle_received_command(void)
{
struct extctl_command *cmd = (struct extctl_command *)rx_buffer;
uint32_t data;
uint8_t checksum;
checksum = checksum_buffer(rx_buffer,
sizeof(struct extctl_command) -
sizeof(uint8_t));
if (unlikely(rx_error || cmd->checksum != checksum)) {
/* Checksum error. */
command_result(EXTCTL_CMD_RESULT_ECSUM);
rx_error = 0;
return;
}
data = le32_to_cpu(cmd->data);
switch (cmd->id) {
case EXTCTL_CMD_NOP:
cmd_nop(data);
break;
case EXTCTL_CMD_SETVOLTAGE:
cmd_setvoltage(data);
break;
case EXTCTL_CMD_GETVOLTAGE:
cmd_getvoltage(data);
break;
case EXTCTL_CMD_SETMAXCUR:
cmd_setmaxcur(data);
break;
case EXTCTL_CMD_GETMAXCUR:
cmd_getmaxcur(data);
break;
case EXTCTL_CMD_SWITCHPROF:
cmd_switchprof(data);
break;
case EXTCTL_CMD_GETPROF:
cmd_getprof(data);
break;
default:
command_result(EXTCTL_CMD_RESULT_EINVAL);
return;
}
}
ISR(USART_RX_vect)
{
uint8_t data;
int8_t err;
while (1) {
err = usart_rx(&data);
if (err > 0)
break;
if (err)
rx_error = 1;
rx_buffer[nr_rx_bytes++] = data;
if (nr_rx_bytes == sizeof(struct extctl_command)) {
nr_rx_bytes = 0;
handle_received_command();
}
}
}
static void usart_init(void)
{
uint8_t dummy;
/* Set baud rate */
UBRR0 = (F_CPU / 16 / BAUDRATE);
/* 8 Data bits, 1 Stop bit, Even parity */
UCSR0C = (1 << UCSZ00) | (1 << UCSZ01) | (1 << UPM01);
/* Enable transceiver and RX IRQs */
UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
/* Drain the RX buffer */
while (usart_rx(&dummy) == 0)
mb();
}
void extctl_init(void)
{
nr_rx_bytes = 0;
rx_error = 0;
mb();
usart_init();
}
|