{
if(recv_overrun)
{
- serial_write(0xFE);
- serial_write(RECEIVE_OVERRUN);
+ interface_send1(RECEIVE_OVERRUN);
recv_overrun = 0;
}
if(recv_fill>0)
else
{
length = 0;
- serial_write(0xFE);
- serial_write(FRAMING_ERROR);
+ interface_send1(FRAMING_ERROR);
}
recv_tail += length+1;
if(length>0)
{
uint8_t result = dispatch_command(cmd, length);
- serial_write(0xFE);
- serial_write(result);
+ interface_send1(result);
}
}
}
return INVALID_COMMAND;
}
+void interface_send(const uint8_t *cmd, uint8_t length)
+{
+ uint8_t i;
+
+ serial_write(~length);
+ for(i=0; i<length; ++i)
+ serial_write(cmd[i]);
+}
+
+void interface_send1(uint8_t cmd)
+{
+ serial_write(0xFE);
+ serial_write(cmd);
+}
+
static inline void receive(uint8_t c)
{
if(recv_fill>=sizeof(recv_buf))
void interface_init(void);
void interface_check(void);
+void interface_send(const uint8_t *, uint8_t);
+void interface_send1(uint8_t);
#endif
#include "adc.h"
-#include "commands.h"
+#include "interface.h"
#include "monitor.h"
#include "output.h"
#include "serial.h"
if(!overcurrent_sent)
{
overcurrent_sent = 1;
- serial_write(0xFE);
- serial_write(OVERCURRENT);
+ interface_send1(OVERCURRENT);
}
}
else
{
if(cmd_buf[0]==READ_TRACK_CURRENT)
{
+ uint8_t reply[3];
+
if(cmd_length!=1)
return LENGTH_ERROR;
- serial_write(0xFC);
- serial_write(TRACK_CURRENT);
uint16_t value = track_current_milliamps();
- serial_write(value>>8);
- serial_write(value);
+ reply[0] = TRACK_CURRENT;
+ reply[1] = value>>8;
+ reply[2] = value;
+ interface_send(reply, sizeof(reply));
}
else if(cmd_buf[0]==SET_OVERCURRENT_LIMIT)
{
}
else if(cmd_buf[0]==READ_INPUT_VOLTAGE)
{
+ uint8_t reply[3];
+
if(cmd_length!=1)
return LENGTH_ERROR;
- serial_write(0xFC);
- serial_write(INPUT_VOLTAGE);
uint16_t value = input_voltage_millivolts();
- serial_write(value>>8);
- serial_write(value);
+ reply[0] = INPUT_VOLTAGE;
+ reply[1] = value>>8;
+ reply[2] = value;
+ interface_send(reply, sizeof(reply));
}
else
return INVALID_COMMAND;