Becareful not to print during RTC callback
authorJoseph Coffland <joseph@cauldrondevelopment.com>
Fri, 26 Apr 2019 20:24:29 +0000 (13:24 -0700)
committerJoseph Coffland <joseph@cauldrondevelopment.com>
Fri, 26 Apr 2019 20:24:29 +0000 (13:24 -0700)
12 files changed:
src/avr/src/command.c
src/avr/src/io.c
src/avr/src/io.h
src/avr/src/log.h [deleted file]
src/avr/src/main.c
src/avr/src/modbus.c
src/avr/src/modbus.h
src/avr/src/rtc.c
src/avr/src/status.c
src/avr/src/status.h
src/avr/src/util.c
src/avr/src/util.h

index a9d65dcaf07bab5d63f9277c400d4211bca9cd38..8733edb985cec2f1d3b0d1d771fb72392efd5531 100644 (file)
@@ -39,7 +39,6 @@
 #include "base64.h"
 #include "rtc.h"
 #include "stepper.h"
-#include "log.h"
 #include "cpp_magic.h"
 
 #include <util/atomic.h>
index 6cb81460b54a1885851a382695ce5df6ec79a2c1..94ffcda31f7ff305874720141ec2e93803694941 100644 (file)
@@ -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;
   }
index f3f2df5164f3c4b3db686e810e621bf63a9ae2db..1f8948b31859fce0e266cb5d4e48251820e624ce 100644 (file)
@@ -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 (file)
index 36c353b..0000000
+++ /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 <http://www.gnu.org/licenses/>.
-
-      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
-                        <http://www.gnu.org/licenses/>.
-
-                 For information regarding this software email:
-                   "Joseph Coffland" <joseph@buildbotics.com>
-
-\******************************************************************************/
-
-#pragma once
-
-#ifdef __AVR__
-#define LOG(...)
-#else
-#include <stdio.h>
-#define LOG(...) fprintf(stderr, __VA_ARGS__)
-#endif
index 1eaec24fdf8be7869ae5930c22dd932dce166240..21a7296cc082f3d1d7393e6fcc335acf965204a5 100644 (file)
@@ -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
   }
 
index fe9a3a1c7d77e2d9be3aa1289c4dfacc2f10b17a..4d95e784b2b129202f42e310dd10fbd0e7ff6e73 100644 (file)
@@ -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;
index 90cdb12f1da3e4fc7a3f94cf8cba30de8ba2951a..1db34b2a094e35b47f696bdfffdcdfcb7e54c13e 100644 (file)
@@ -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();
index a3779598704110533f42c111141c4ca5e886c897..80208953143bb9be16e80605a8bf314c214f0452 100644 (file)
@@ -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();
index f1aaa11fdff68600c1ed3814bb58e53224017151..427963785affaafc5dba1d67f68dca7c71d577ba 100644 (file)
@@ -33,9 +33,6 @@
 #include <stdarg.h>
 
 
-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
index f9fda83a4610107bd60bc9bdc5e57de9bf8c02a8..d363a1d5582f25b4c037f07116180c2408ada464 100644 (file)
 #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"
index f39be9345d0c5263b4ea0b072b0d5bdcc1c61651..61137fe04d2ebfe3dedde1214144558090205dd1 100644 (file)
@@ -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]);
-}
index dd1b754e40d3a3f0b434ae06d0b1db6cb85672f5..300fb1a9c9f692d71ea04ce91558fbd1472ecbf8 100644 (file)
@@ -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