From: Martin Mares Date: Mon, 8 Jul 2019 17:08:40 +0000 (+0200) Subject: ModBus: Code re-arranged X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=77733f11da6abe26f28ed2cee76e5680ef436289;p=home-hw.git ModBus: Code re-arranged --- diff --git a/test-modbus/modbus.c b/test-modbus/modbus.c index 39bd039..df37adf 100644 --- a/test-modbus/modbus.c +++ b/test-modbus/modbus.c @@ -90,6 +90,11 @@ static byte tx_buf[MODBUS_TX_BUFSIZE]; static u16 tx_size; static u16 tx_pos; +static bool check_frame(void); +static void process_frame(void); + +/*** Low-level layer ***/ + static void rx_init(void) { state = STATE_RX; @@ -197,7 +202,32 @@ void MODBUS_TIMER_ISR(void) } } -// CRC tables +void modbus_loop(void) +{ + if (state != STATE_RX_DONE) + return; + state = STATE_PROCESSING; + + if (!check_frame()) { + rx_init(); + return; + } + + if (rx_buf[0] == MODBUS_OUR_ADDRESS) { + // Frame addressed to us: process and reply + process_frame(); + tx_init(); + } else if (rx_buf[0] == 0x00) { + // Broadcast frame: process, but do not reply + process_frame(); + rx_init(); + } else { + // Somebody else's frame: discard + rx_init(); + } +} + +/** CRC ***/ static const byte crc_hi[] = { 0x00, 0xc1, 0x81, 0x40, 0x01, 0xc0, 0x80, 0x41, 0x01, 0xc0, @@ -270,6 +300,8 @@ static u16 crc16(byte *buf, u16 len) return (hi << 8 | lo); } +/*** High-level layer ***/ + static bool check_frame(void) { if (rx_bad) { @@ -661,87 +693,3 @@ static void process_frame(void) // Finish reply frame write_u16(crc16(tx_buf, tx_size)); } - -void modbus_loop(void) -{ - if (state != STATE_RX_DONE) - return; - state = STATE_PROCESSING; - - if (!check_frame()) { - rx_init(); - return; - } - - if (rx_buf[0] == MODBUS_OUR_ADDRESS) { - // Frame addressed to us: process and reply - process_frame(); - tx_init(); - } else if (rx_buf[0] == 0x00) { - // Broadcast frame: process, but do not reply - process_frame(); - rx_init(); - } else { - // Somebody else's frame: discard - rx_init(); - } -} - -/*** Callbacks ***/ - -bool modbus_check_discrete_input(u16 addr UNUSED) -{ - return false; -} - -bool modbus_get_discrete_input(u16 addr UNUSED) -{ - return false; -} - -bool modbus_check_coil(u16 addr UNUSED) -{ - return false; -} - -bool modbus_get_coil(u16 addr UNUSED) -{ - return false; -} - -void modbus_set_coil(u16 addr UNUSED, bool value UNUSED) -{ -} - -bool modbus_check_input_register(u16 addr UNUSED) -{ - return false; -} - -u16 modbus_get_input_register(u16 addr UNUSED) -{ - return 0; -} - -bool modbus_check_holding_register(u16 addr UNUSED) -{ - return (addr == 0); -} - -u16 modbus_get_holding_register(u16 addr UNUSED) -{ - return 0xbeef; -} - -void modbus_set_holding_register(u16 addr UNUSED, u16 value UNUSED) -{ -} - -const char * const modbus_id_strings[MODBUS_ID_MAX] = { - [MODBUS_ID_VENDOR_NAME] = "United Computer Wizards", - [MODBUS_ID_PRODUCT_CODE] = "42", - [MODBUS_ID_MAJOR_MINOR_REVISION] = "1.0", - [MODBUS_ID_VENDOR_URL] = "http://www.ucw.cz/", - [MODBUS_ID_PRODUCT_NAME] = "Magic Gadget", - [MODBUS_ID_USER_APP_NAME] = NULL, -}; diff --git a/test-modbus/test.c b/test-modbus/test.c index 381b2ed..16ae1ac 100644 --- a/test-modbus/test.c +++ b/test-modbus/test.c @@ -114,3 +114,62 @@ int main(void) return 0; } + +/*** Modbus callbacks ***/ + +bool modbus_check_discrete_input(u16 addr UNUSED) +{ + return false; +} + +bool modbus_get_discrete_input(u16 addr UNUSED) +{ + return false; +} + +bool modbus_check_coil(u16 addr UNUSED) +{ + return false; +} + +bool modbus_get_coil(u16 addr UNUSED) +{ + return false; +} + +void modbus_set_coil(u16 addr UNUSED, bool value UNUSED) +{ +} + +bool modbus_check_input_register(u16 addr UNUSED) +{ + return false; +} + +u16 modbus_get_input_register(u16 addr UNUSED) +{ + return 0; +} + +bool modbus_check_holding_register(u16 addr UNUSED) +{ + return (addr == 0); +} + +u16 modbus_get_holding_register(u16 addr UNUSED) +{ + return 0xbeef; +} + +void modbus_set_holding_register(u16 addr UNUSED, u16 value UNUSED) +{ +} + +const char * const modbus_id_strings[MODBUS_ID_MAX] = { + [MODBUS_ID_VENDOR_NAME] = "United Computer Wizards", + [MODBUS_ID_PRODUCT_CODE] = "42", + [MODBUS_ID_MAJOR_MINOR_REVISION] = "1.0", + [MODBUS_ID_VENDOR_URL] = "http://www.ucw.cz/", + [MODBUS_ID_PRODUCT_NAME] = "Magic Gadget", + [MODBUS_ID_USER_APP_NAME] = NULL, +}; diff --git a/test-modbus/util.h b/test-modbus/util.h index 65eb435..09f8bbc 100644 --- a/test-modbus/util.h +++ b/test-modbus/util.h @@ -1,5 +1,6 @@ -#include #include +#include +#include typedef unsigned int uint; typedef uint8_t byte;