From d7ff6b6242add979d20be3d7992028d8b4040f45 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sun, 14 Jul 2019 20:50:12 +0200 Subject: [PATCH] Aircon: MODBUS --- aircon/Makefile | 2 +- aircon/README | 2 +- aircon/config.h | 5 ++- aircon/main.c | 86 +++++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 85 insertions(+), 10 deletions(-) diff --git a/aircon/Makefile b/aircon/Makefile index 15d7d8e..1b9f247 100644 --- a/aircon/Makefile +++ b/aircon/Makefile @@ -1,6 +1,6 @@ ROOT=.. BINARY=main OBJS=main.o -LIB_OBJS=util-debug.o ds18b20.o +LIB_OBJS=util-debug.o ds18b20.o modbus.o include $(ROOT)/mk/bluepill.mk diff --git a/aircon/README b/aircon/README index fab35a7..e5a5dab 100644 --- a/aircon/README +++ b/aircon/README @@ -27,7 +27,7 @@ bypass opto-coupler | PC15 PB9 | RS485 TX enable | PA6 PA15 | TIM3_CH2 - DS18B20 bus | PA7 PA12 | bypass LED* (green) | PB0 PA11 | -RC active LED* (red) | PB1 PA10 | RXD1 - debugging console +MODBUS frame LED* (red) | PB1 PA10 | RXD1 - debugging console TXD3 - RS485 | PB10 PA9 | TXD1 - debugging console RXD3 - RS485 | PB11 PA8 | | RESET PB15 | diff --git a/aircon/config.h b/aircon/config.h index 77a5f33..a73fe50 100644 --- a/aircon/config.h +++ b/aircon/config.h @@ -11,6 +11,7 @@ // Debugging port #define DEBUG_USART USART1 +#define DEBUG_LED_BLUEPILL // MODBUS library parameters @@ -19,7 +20,7 @@ #define MODBUS_USART_ISR usart3_isr #define MODBUS_TXEN_GPIO_PORT GPIOA -#define MODBUS_TXEN_GPIO_PIN GPIO1 +#define MODBUS_TXEN_GPIO_PIN GPIO6 #define MODBUS_TIMER TIM2 #define MODBUS_NVIC_TIMER_IRQ NVIC_TIM2_IRQ @@ -29,6 +30,8 @@ #define MODBUS_BAUD_RATE 19200 +#define MODBUS_DEBUG + // DS18B20 library parameters #define DS_TIMER TIM3 diff --git a/aircon/main.c b/aircon/main.c index 09d563a..db09ae1 100644 --- a/aircon/main.c +++ b/aircon/main.c @@ -6,6 +6,7 @@ #include "util.h" #include "ds18b20.h" +#include "modbus.h" #include #include @@ -61,7 +62,7 @@ static void gpio_setup(void) gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO0); gpio_set(GPIOB, GPIO0); - // PB1 = IR RC active LED* + // PB1 = MODBUS frame LED* gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO1); gpio_set(GPIOB, GPIO1); @@ -174,17 +175,17 @@ int main(void) debug_puts("Hello, world!\n"); ds_init(); + modbus_init(); for (;;) { - gpio_toggle(GPIOC, GPIO13); - - gpio_clear(GPIOB, GPIO1); - delay_ms(100); - gpio_set(GPIOB, GPIO1); + debug_led_toggle(); gpio_clear(GPIOB, GPIO0); - delay_ms(100); + delay_ms(50); gpio_set(GPIOB, GPIO0); + delay_ms(50); + + modbus_loop(); } #if 0 @@ -232,3 +233,74 @@ 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 < 16); +} + +u16 modbus_get_holding_register(u16 addr UNUSED) +{ + return 0xbeef; +} + +void modbus_set_holding_register(u16 addr UNUSED, u16 value UNUSED) +{ +} + +void modbus_ready_hook(void) +{ + // Frame LED off + gpio_set(GPIOB, GPIO1); +} + +void modbus_frame_start_hook(void) +{ + // Frame LED on + gpio_clear(GPIOB, GPIO1); +} + +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, +}; -- 2.39.5