From 2df78ba7e5749545a3989603ee84c017b79385cc Mon Sep 17 00:00:00 2001 From: Joseph Coffland Date: Fri, 26 Apr 2019 13:24:29 -0700 Subject: [PATCH] Becareful not to print during RTC callback --- src/avr/src/command.c | 1 - src/avr/src/io.c | 6 ++---- src/avr/src/io.h | 2 +- src/avr/src/log.h | 35 ----------------------------------- src/avr/src/main.c | 4 ++++ src/avr/src/modbus.c | 6 +++--- src/avr/src/modbus.h | 2 +- src/avr/src/rtc.c | 4 ---- src/avr/src/status.c | 3 --- src/avr/src/status.h | 4 ---- src/avr/src/util.c | 6 ------ src/avr/src/util.h | 1 - 12 files changed, 11 insertions(+), 63 deletions(-) delete mode 100644 src/avr/src/log.h diff --git a/src/avr/src/command.c b/src/avr/src/command.c index a9d65dc..8733edb 100644 --- a/src/avr/src/command.c +++ b/src/avr/src/command.c @@ -39,7 +39,6 @@ #include "base64.h" #include "rtc.h" #include "stepper.h" -#include "log.h" #include "cpp_magic.h" #include diff --git a/src/avr/src/io.c b/src/avr/src/io.c index 6cb8146..94ffcda 100644 --- a/src/avr/src/io.c +++ b/src/avr/src/io.c @@ -51,13 +51,12 @@ static input_cmd_t active_cmd = {-1,}; static uint32_t timeout; -void io_rtc_callback() { +void io_callback() { if (active_cmd.port == -1) return; bool done = false; float result = 0; - if (active_cmd.mode == INPUT_IMMEDIATE || - rtc_expired(active_cmd.timeout)) done = true; + if (active_cmd.mode == INPUT_IMMEDIATE || rtc_expired(timeout)) done = true; // TODO handle modes @@ -65,7 +64,6 @@ void io_rtc_callback() { if (active_cmd.digital) { // TODO } else result = analog_get(active_cmd.port); - // TODO find a better way to send this printf_P(PSTR("{\"result\": %f}\n"), (double)result); active_cmd.port = -1; } diff --git a/src/avr/src/io.h b/src/avr/src/io.h index f3f2df5..1f8948b 100644 --- a/src/avr/src/io.h +++ b/src/avr/src/io.h @@ -37,4 +37,4 @@ typedef enum { } input_mode_t; -void io_rtc_callback(); +void io_callback(); diff --git a/src/avr/src/log.h b/src/avr/src/log.h deleted file mode 100644 index 36c353b..0000000 --- a/src/avr/src/log.h +++ /dev/null @@ -1,35 +0,0 @@ -/******************************************************************************\ - - This file is part of the Buildbotics firmware. - - Copyright (c) 2015 - 2018, Buildbotics LLC - All rights reserved. - - This file ("the software") is free software: you can redistribute it - and/or modify it under the terms of the GNU General Public License, - version 2 as published by the Free Software Foundation. You should - have received a copy of the GNU General Public License, version 2 - along with the software. If not, see . - - The software 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the software. If not, see - . - - For information regarding this software email: - "Joseph Coffland" - -\******************************************************************************/ - -#pragma once - -#ifdef __AVR__ -#define LOG(...) -#else -#include -#define LOG(...) fprintf(stderr, __VA_ARGS__) -#endif diff --git a/src/avr/src/main.c b/src/avr/src/main.c index 1eaec24..21a7296 100644 --- a/src/avr/src/main.c +++ b/src/avr/src/main.c @@ -40,6 +40,8 @@ #include "pgmspace.h" #include "outputs.h" #include "analog.h" +#include "modbus.h" +#include "io.h" #include "exec.h" #include "state.h" #include "emu.h" @@ -90,6 +92,8 @@ int main(int argc, char *argv[]) { hw_reset_handler(); // handle hard reset requests state_callback(); // manage state command_callback(); // process next command + modbus_callback(); // handle modbus events + io_callback(); // handle io input report_callback(); // report changes } diff --git a/src/avr/src/modbus.c b/src/avr/src/modbus.c index fe9a3a1..4d95e78 100644 --- a/src/avr/src/modbus.c +++ b/src/avr/src/modbus.c @@ -355,8 +355,7 @@ static void _start_write() { // // At 9600 baud the minimum delay is 4.01ms. At 19200 it's 2.005ms. All // supported higher baud rates require the 1.75ms minimum delay. We round up - // and add 1ms to ensure the delay is never less than the required minimum - // using our 1ms RTC clock callback. + // and add 1ms to ensure the delay is never less than the required minimum. if (state.last_read && !rtc_expired(state.last_read + (cfg.baud == USART_BAUD_9600 ? 5 : 3))) return; @@ -411,7 +410,7 @@ void modbus_write(uint16_t addr, uint16_t value, modbus_rw_cb_t cb) { } -void modbus_rtc_callback() { +void modbus_callback() { if (state.transmit_complete) { state.last_write = rtc_get_time(); state.transmit_complete = false; @@ -420,6 +419,7 @@ void modbus_rtc_callback() { _handle_response(); _start_write(); + // Timeout out writes if (!state.last_write || !rtc_expired(state.last_write + MODBUS_TIMEOUT)) return; state.last_write = 0; diff --git a/src/avr/src/modbus.h b/src/avr/src/modbus.h index 90cdb12..1db34b2 100644 --- a/src/avr/src/modbus.h +++ b/src/avr/src/modbus.h @@ -108,4 +108,4 @@ void modbus_func(uint8_t func, uint8_t send, const uint8_t *data, uint8_t receive, modbus_cb_t cb); void modbus_read(uint16_t addr, uint16_t count, modbus_rw_cb_t cb); void modbus_write(uint16_t addr, uint16_t value, modbus_rw_cb_t cb); -void modbus_rtc_callback(); +void modbus_callback(); diff --git a/src/avr/src/rtc.c b/src/avr/src/rtc.c index a377959..8020895 100644 --- a/src/avr/src/rtc.c +++ b/src/avr/src/rtc.c @@ -29,8 +29,6 @@ #include "switch.h" #include "analog.h" -#include "io.h" -#include "modbus.h" #include "motor.h" #include "lcd.h" #include "vfd_spindle.h" @@ -51,8 +49,6 @@ ISR(RTC_OVF_vect) { lcd_rtc_callback(); switch_rtc_callback(); analog_rtc_callback(); - io_rtc_callback(); - modbus_rtc_callback(); vfd_spindle_rtc_callback(); if (!(ticks & 255)) motor_rtc_callback(); wdt_reset(); diff --git a/src/avr/src/status.c b/src/avr/src/status.c index f1aaa11..4279637 100644 --- a/src/avr/src/status.c +++ b/src/avr/src/status.c @@ -33,9 +33,6 @@ #include -stat_t status_code; // allocate a variable for the RITORNO macro - - #define STAT_MSG(NAME, TEXT) static const char stat_##NAME[] PROGMEM = TEXT; #include "messages.def" #undef STAT_MSG diff --git a/src/avr/src/status.h b/src/avr/src/status.h index f9fda83..d363a1d 100644 --- a/src/avr/src/status.h +++ b/src/avr/src/status.h @@ -30,10 +30,6 @@ #include "pgmspace.h" -// RITORNO is a handy way to provide exception returns -// It returns only if an error occurred. (ritorno is Italian for return) -#define RITORNO(a) if ((status_code = a) != STAT_OK) {return status_code;} - typedef enum { #define STAT_MSG(NAME, TEXT) STAT_##NAME, #include "messages.def" diff --git a/src/avr/src/util.c b/src/avr/src/util.c index f39be93..61137fe 100644 --- a/src/avr/src/util.c +++ b/src/avr/src/util.c @@ -93,9 +93,3 @@ void format_hex_buf(char *buf, const uint8_t *data, unsigned len) { buf[i * 2] = 0; } - - -void print_vector(float v[4]) { - printf_P(PSTR("%f %f %f %f\n"), - (double)v[0], (double)v[1], (double)v[2], (double)v[3]); -} diff --git a/src/avr/src/util.h b/src/avr/src/util.h index dd1b754..300fb1a 100644 --- a/src/avr/src/util.h +++ b/src/avr/src/util.h @@ -69,7 +69,6 @@ int8_t decode_hex_nibble(char c); bool decode_float(char **s, float *f); stat_t decode_axes(char **cmd, float axes[AXES]); void format_hex_buf(char *buf, const uint8_t *data, unsigned len); -void print_vector(float v[4]); // Constants #define MM_PER_INCH 25.4 -- 2.27.0