From 777a0b817bc01425d562e42354bf09463a1d7852 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Mon, 11 Jul 2016 11:41:49 -0400 Subject: [PATCH] serialhdl: Calculate baudadjust from MCU's baud instead of host baud Store the baud rate the MCU is configured for in the "identify" data and use that rate when calculating the baudadjust parameter. Signed-off-by: Kevin O'Connor --- klippy/chelper.py | 4 ++-- klippy/serialhdl.py | 16 ++++++++-------- klippy/serialqueue.c | 11 +++++++++-- klippy/serialqueue.h | 4 ++-- scripts/buildcommands.py | 2 +- 5 files changed, 22 insertions(+), 15 deletions(-) diff --git a/klippy/chelper.py b/klippy/chelper.py index 99ec3f4f..5b7837ee 100644 --- a/klippy/chelper.py +++ b/klippy/chelper.py @@ -39,8 +39,7 @@ defs_serialqueue = """ double sent_time, receive_time; }; - struct serialqueue *serialqueue_alloc(int serial_fd, double baud_adjust - , int write_only); + struct serialqueue *serialqueue_alloc(int serial_fd, int write_only); void serialqueue_exit(struct serialqueue *sq); struct command_queue *serialqueue_alloc_commandqueue(void); void serialqueue_send(struct serialqueue *sq, struct command_queue *cq @@ -49,6 +48,7 @@ defs_serialqueue = """ , struct command_queue *cq, uint32_t *data, int len , uint64_t min_clock, uint64_t req_clock); void serialqueue_pull(struct serialqueue *sq, struct pull_queue_message *pqm); + void serialqueue_set_baud_adjust(struct serialqueue *sq, double baud_adjust); void serialqueue_set_clock_est(struct serialqueue *sq, double est_clock , double last_ack_time, uint64_t last_ack_clock); void serialqueue_flush_ready(struct serialqueue *sq); diff --git a/klippy/serialhdl.py b/klippy/serialhdl.py index cac2ef9f..619d46ce 100644 --- a/klippy/serialhdl.py +++ b/klippy/serialhdl.py @@ -9,7 +9,7 @@ import serial import msgproto, chelper class SerialReader: - BITS_PER_BYTE = 10 + BITS_PER_BYTE = 10. def __init__(self, reactor, serialport, baud): self.reactor = reactor self.serialport = serialport @@ -60,22 +60,17 @@ class SerialReader: logging.info("Starting serial connect") self.ser = serial.Serial(self.serialport, self.baud, timeout=0) stk500v2_leave(self.ser) - baud_adjust = float(self.BITS_PER_BYTE) / self.baud - self.serialqueue = self.ffi_lib.serialqueue_alloc( - self.ser.fileno(), baud_adjust, 0) + self.serialqueue = self.ffi_lib.serialqueue_alloc(self.ser.fileno(), 0) SerialBootStrap(self) self.background_thread = threading.Thread(target=self._bg_thread) self.background_thread.start() def connect_file(self, debugoutput, dictionary, pace=False): self.ser = debugoutput self.msgparser.process_identify(dictionary, decompress=False) - baud_adjust = 0. est_clock = 1000000000000. if pace: - baud_adjust = float(self.BITS_PER_BYTE) / self.baud est_clock = self.msgparser.config['CLOCK_FREQ'] - self.serialqueue = self.ffi_lib.serialqueue_alloc( - self.ser.fileno(), baud_adjust, 1) + self.serialqueue = self.ffi_lib.serialqueue_alloc(self.ser.fileno(), 1) self.est_clock = est_clock self.last_ack_time = time.time() self.last_ack_clock = 0 @@ -241,6 +236,11 @@ class SerialBootStrap: logging.info("MCU version: %s" % (self.serial.msgparser.version,)) self.serial.unregister_callback('identify_response') self.serial.register_callback(self.serial.handle_unknown, '#unknown') + mcu_baud = float(self.serial.msgparser.config.get('SERIAL_BAUD', 0.)) + if mcu_baud: + baud_adjust = self.serial.BITS_PER_BYTE / mcu_baud + self.serial.ffi_lib.serialqueue_set_baud_adjust( + self.serial.serialqueue, baud_adjust) get_status = self.serial.msgparser.lookup_command('get_status') self.serial.status_cmd = get_status.encode() with self.serial.lock: diff --git a/klippy/serialqueue.c b/klippy/serialqueue.c index 60ceeb4b..b3451ad6 100644 --- a/klippy/serialqueue.c +++ b/klippy/serialqueue.c @@ -770,11 +770,10 @@ background_thread(void *data) // Create a new 'struct serialqueue' object struct serialqueue * -serialqueue_alloc(int serial_fd, double baud_adjust, int write_only) +serialqueue_alloc(int serial_fd, int write_only) { struct serialqueue *sq = malloc(sizeof(*sq)); memset(sq, 0, sizeof(*sq)); - sq->baud_adjust = baud_adjust; // Reactor setup sq->serial_fd = serial_fd; @@ -961,6 +960,14 @@ exit: pthread_mutex_unlock(&sq->lock); } +void +serialqueue_set_baud_adjust(struct serialqueue *sq, double baud_adjust) +{ + pthread_mutex_lock(&sq->lock); + sq->baud_adjust = baud_adjust; + pthread_mutex_unlock(&sq->lock); +} + // Set the estimated clock rate of the mcu on the other end of the // serial port void diff --git a/klippy/serialqueue.h b/klippy/serialqueue.h index 229fb216..80ecd9c5 100644 --- a/klippy/serialqueue.h +++ b/klippy/serialqueue.h @@ -43,8 +43,7 @@ struct pull_queue_message { }; struct serialqueue; -struct serialqueue *serialqueue_alloc(int serial_fd, double baud_adjust - , int write_only); +struct serialqueue *serialqueue_alloc(int serial_fd, int write_only); void serialqueue_exit(struct serialqueue *sq); struct command_queue *serialqueue_alloc_commandqueue(void); void serialqueue_send_batch(struct serialqueue *sq, struct command_queue *cq @@ -56,6 +55,7 @@ void serialqueue_encode_and_send(struct serialqueue *sq, struct command_queue *c , uint32_t *data, int len , uint64_t min_clock, uint64_t req_clock); void serialqueue_pull(struct serialqueue *sq, struct pull_queue_message *pqm); +void serialqueue_set_baud_adjust(struct serialqueue *sq, double baud_adjust); void serialqueue_set_clock_est(struct serialqueue *sq, double est_clock , double last_ack_time, uint64_t last_ack_clock); void serialqueue_flush_ready(struct serialqueue *sq); diff --git a/scripts/buildcommands.py b/scripts/buildcommands.py index 6bb47341..af9926ca 100644 --- a/scripts/buildcommands.py +++ b/scripts/buildcommands.py @@ -153,7 +153,7 @@ def build_identify(cmd_by_id, msg_to_id, responses, static_strings data['commands'] = sorted(cmd_by_id.keys()) data['responses'] = sorted(responses) data['static_strings'] = static_strings - configlist = ['MCU', 'CLOCK_FREQ'] + configlist = ['MCU', 'CLOCK_FREQ', 'SERIAL_BAUD'] data['config'] = dict((i, config['CONFIG_'+i]) for i in configlist if 'CONFIG_'+i in config) data['version'] = version