summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMichael Buesch <mb@bu3sch.de>2009-11-22 15:01:38 +0100
committerMichael Buesch <mb@bu3sch.de>2009-11-22 15:01:38 +0100
commit9af43462bd0989a2930afb9e1fdb7bb58fbdf5c4 (patch)
tree9c0c0c1f60f85378a82092ec4b9bd32dbe1d52ac
parentf6e560e764e4315c8d50ce26b4894da680efb43a (diff)
downloadcnc-9af43462bd0989a2930afb9e1fdb7bb58fbdf5c4.tar.xz
cnc-9af43462bd0989a2930afb9e1fdb7bb58fbdf5c4.zip
pressure_control: Use 8bit CRC
Signed-off-by: Michael Buesch <mb@bu3sch.de>
-rw-r--r--pressure_control/firmware/remote.c18
-rw-r--r--pressure_control/firmware/remote.h2
-rw-r--r--pressure_control/firmware/util.c9
-rw-r--r--pressure_control/firmware/util.h2
-rwxr-xr-xpressure_control/remote/pctl-remote24
5 files changed, 26 insertions, 29 deletions
diff --git a/pressure_control/firmware/remote.c b/pressure_control/firmware/remote.c
index 7863b55..a80683b 100644
--- a/pressure_control/firmware/remote.c
+++ b/pressure_control/firmware/remote.c
@@ -2,7 +2,7 @@
* Pneumatic pressure controller.
* Remote control.
*
- * Copyright (C) 2008 Michael Buesch <mb@bu3sch.de>
+ * Copyright (C) 2008-2009 Michael Buesch <mb@bu3sch.de>
*
* 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
@@ -87,9 +87,9 @@ static inline int8_t usart_rx(uint8_t *data)
static void send_message(struct remote_message *msg)
{
/* Calculate the CRC. */
- msg->crc = crc16_block_update(0xFFFF, msg,
- sizeof(*msg) - sizeof(msg->crc));
- msg->crc ^= 0xFFFF;
+ msg->crc = crc8_block_update(0xFF, msg,
+ sizeof(*msg) - sizeof(msg->crc));
+ msg->crc ^= 0xFF;
/* And transmit the bits. */
usart_tx_buf(msg, sizeof(*msg));
}
@@ -97,12 +97,12 @@ static void send_message(struct remote_message *msg)
static void handle_received_message(void)
{
struct remote_message reply;
- uint16_t calc_crc;
+ uint8_t calc_crc;
uint8_t err = MSG_ERR_NONE;
- calc_crc = crc16_block_update(0xFFFF, &rx_msg,
- sizeof(rx_msg) - sizeof(rx_msg.crc));
- calc_crc ^= 0xFFFF;
+ calc_crc = crc8_block_update(0xFF, &rx_msg,
+ sizeof(rx_msg) - sizeof(rx_msg.crc));
+ calc_crc ^= 0xFF;
if (calc_crc != rx_msg.crc) {
/* CRC mismatch. */
err = MSG_ERR_CHKSUM;
@@ -460,7 +460,7 @@ static void usart_init(void)
void remote_init(void)
{
/* The remote tool depends on the exact size (and layout). */
- BUILD_BUG_ON(sizeof(struct remote_message) != 7);
+ BUILD_BUG_ON(sizeof(struct remote_message) != 6);
usart_init();
}
diff --git a/pressure_control/firmware/remote.h b/pressure_control/firmware/remote.h
index 944dc43..36caf40 100644
--- a/pressure_control/firmware/remote.h
+++ b/pressure_control/firmware/remote.h
@@ -78,7 +78,7 @@ struct remote_message {
uint8_t __padding[4];
} __attribute__((packed));
- uint16_t crc;
+ uint8_t crc;
} __attribute__((packed));
diff --git a/pressure_control/firmware/util.c b/pressure_control/firmware/util.c
index 0b0bbab..7497740 100644
--- a/pressure_control/firmware/util.c
+++ b/pressure_control/firmware/util.c
@@ -87,15 +87,12 @@ void infinite_sleep(void)
sleep_mode();
}
-uint16_t crc16_block_update(uint16_t crc, const void *_data, uint16_t size)
+uint8_t crc8_block_update(uint8_t crc, const void *_data, uint8_t size)
{
const uint8_t *data = _data;
- while (size) {
- crc = _crc16_update(crc, *data);
- data++;
- size--;
- }
+ while (size--)
+ crc = _crc_ibutton_update(crc, *data++);
return crc;
}
diff --git a/pressure_control/firmware/util.h b/pressure_control/firmware/util.h
index 28e98f8..992df0e 100644
--- a/pressure_control/firmware/util.h
+++ b/pressure_control/firmware/util.h
@@ -81,7 +81,7 @@ static inline void irq_restore(uint8_t sreg_flags)
#define irqs_disabled() (!(SREG & (1 << SREG_I)))
-uint16_t crc16_block_update(uint16_t crc, const void *data, uint16_t size);
+uint8_t crc8_block_update(uint8_t crc, const void *data, uint8_t size);
/* Convenient casting macros. */
#define U8(value) ((uint8_t)(value))
diff --git a/pressure_control/remote/pctl-remote b/pressure_control/remote/pctl-remote
index b4040cc..fa12431 100755
--- a/pressure_control/remote/pctl-remote
+++ b/pressure_control/remote/pctl-remote
@@ -35,7 +35,7 @@ CONFIG_PARITY = PARITY_NONE
CONFIG_STOPBITS = 2
# The size of one message
-MSG_SIZE = 7
+MSG_SIZE = 6
MSG_PAYLOAD_SIZE = 4
# Message IDs
MSG_ID_MASK = 0x7F
@@ -225,9 +225,9 @@ class RemoteProtocol(QObject):
self.devRestarted = False
def checksumMessage(self, msg):
- calc_crc = self.__crc16_update_buffer(0xFFFF, msg[0:-2])
- calc_crc ^= 0xFFFF
- want_crc = (ord(msg[-2]) | (ord(msg[-1]) << 8))
+ calc_crc = self.__crc8_update_buffer(0xFF, msg[0:-1])
+ calc_crc ^= 0xFF
+ want_crc = ord(msg[-1])
if calc_crc != want_crc:
text = self.tr("ERROR: message CRC mismatch\n")
mainwnd.centralWidget().log.hostLog(text)
@@ -249,7 +249,7 @@ class RemoteProtocol(QObject):
self.devRestarted = True
def getPayload(self, msg):
- return msg[1:-2]
+ return msg[1:-1]
def sendMessage(self, id, flags, payload):
"""Send a message"""
@@ -261,10 +261,10 @@ class RemoteProtocol(QObject):
# Pad the payload up to the constant size
msg += '\0' * (MSG_PAYLOAD_SIZE - len(payload))
# Calculate the CRC
- crc = self.__crc16_update_buffer(0xFFFF, msg)
- crc ^= 0xFFFF
+ crc = self.__crc8_update_buffer(0xFF, msg)
+ crc ^= 0xFF
# Add the CRC to the message
- msg += "%c%c" % ((crc & 0xFF), ((crc >> 8) & 0xFF))
+ msg += "%c" % crc
# Send the message
assert(len(msg) == MSG_SIZE)
self.serial.write(msg)
@@ -326,15 +326,15 @@ class RemoteProtocol(QObject):
i -= 1
return err
- def __crc16_update_buffer(self, crc, buf):
+ def __crc8_update_buffer(self, crc, buf):
for c in buf:
crc ^= ord(c)
for i in range(0, 8):
if crc & 1:
- crc = (crc >> 1) ^ 0xA001
+ crc = (crc >> 1) ^ 0x8C
else:
- crc = (crc >> 1)
- return crc
+ crc >>= 1
+ return crc & 0xFF
class StatusBar(QStatusBar):
def showMessage(self, msg):
bues.ch cgit interface