From 85e6382088ed63f90686e90bb25cde5f3d2822c3 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Fri, 14 Oct 2022 03:23:53 +0200 Subject: [PATCH] minimal modbus example on controllino mega --- Makefile | 5 +- README.md | 40 -------- avrIOhelper/io-helper.h | 27 ++--- main.c | 256 +++++++++--------------------------------------- spi.c | 37 ------- spi.h | 20 ---- uart.c | 53 ---------- uart.h | 37 ------- 8 files changed, 64 insertions(+), 411 deletions(-) delete mode 100644 README.md delete mode 100644 spi.c delete mode 100644 spi.h delete mode 100644 uart.c delete mode 100644 uart.h diff --git a/Makefile b/Makefile index e5279be..3c3e30c 100644 --- a/Makefile +++ b/Makefile @@ -4,13 +4,16 @@ FILES = $(SRCS:%.c=%) #main uart avrIOhelper/io-helper #uart#hier alle c-Datei MCU = atmega2560 PROGC = m2560 CC = avr-gcc +#TOOL = stk500 -P /dev/ttyUSB0 +#TOOL = dragon_isp +#TOOL = atmelice TOOL = atmelice_isp #TOOL = avrispmkii #TOOL = usbasp-clone BUILDDIR = Builds -DEFINES = -I . -IInternet/MQTT -I Internet/MQTT/MQTTPacket/src -I Ethernet/W5500 -I Ethernet -DF_CPU=16000000UL -D_WIZCHIP_=W5100 +DEFINES = -I . -DF_CPU=16000000UL -D_WIZCHIP_=W5100 CFLAGS =-mmcu=$(MCU) -O2 -Wall -Wpedantic $(DEFINES) -std=c99 -ffunction-sections -fdata-sections LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections diff --git a/README.md b/README.md deleted file mode 100644 index cfdb7d5..0000000 --- a/README.md +++ /dev/null @@ -1,40 +0,0 @@ -## Network Config - -|Parameter| Value -|---------|------------- -| IP Adr. | 192.168.2.1 -| Subnet | 255.255.0.0 -| MAC | 00-08-dc-ab-cd-f1 - -### MQTT - -#### Connect to MQTT Host 192.168.5.2 - -##### Publish -|Topic |Values | Unit -|--------------------------------------------------------|------------|------- -| TODO | | - - -##### Subscribe - -|Topic |Values | Unit -|--------------------------------------------------------|------------|------- -| TODO | | - -## Hardware usage - -### Timers - -|Timer| Type | Usage | Mode | used ISRs | Output connected -|-----|--------|-------------------------------|------------------------|----------------|------------------ -| 0 | 8 bit | Millis Tick Timer (1kHz) | 2 (CTC) | COMPA | - - -### UART - -|UART| Usage -|----|---------------------------------- -| 0 | Debug Prints (connected to USB) -| 1 | -| 2 | -| 3 | diff --git a/avrIOhelper/io-helper.h b/avrIOhelper/io-helper.h index 71071f1..6cd3047 100644 --- a/avrIOhelper/io-helper.h +++ b/avrIOhelper/io-helper.h @@ -68,14 +68,7 @@ void ioHelperEdgeDetector(void); #define BitPD6 22 //D22 #define BitPJ4 23 //D23 - -#define LED_GRN_NOTAUS_ANLAGE BitPE4 -#define LED_ROT_NOTAUS_ANLAGE BitPE5 -#define LED_GRN_NOTAUS_SCHRANK BitPG5 -#define LED_ROT_NOTAUS_SCHRANK BitPE3 -#define LED_PLC_OK BitPH5 -#define LED_BUS_OK BitPD4 - +#define LED_EXTR_FEHLER BitPB6 //Inputs //Verknüpfen von Pin | Bit mit Bitposition (0...n) inStates[0...n/8]. @@ -90,11 +83,23 @@ void ioHelperEdgeDetector(void); #define BitPinK0 8 //A8 #define BitPinK1 9 //A9 +#define BitPinK2 10 //A10 +#define BitPinK3 11 //A11 +#define BitPinK4 12 //A12 +#define BitPinK5 13 //A13 +#define BitPinK6 14 //A14 +#define BitPinK7 15 //A15 + +#define BitPinD7 16 //I16 +#define BitPinG2 17 //I17 +#define BitPinG1 18 //I18 #define BitPinD3 10 //INO #define BitPinD2 11 //IN1 -#define IN_ANLAGE_EIN_INV BitPinF5 -#define IN_NOTAUS_ANLAGE BitPinF7 -#define IN_NOTAUS_SCHRANK BitPinK0 + +#define BTN_DREHZAHL_GROB_PLUS BitPinF4 // modbus +#define BTN_DREHZAHL_GROB_MINUS BitPinF5 // modbus + +#define IN_ANLAGE_EIN_INV BitPinK3 // modbus #endif diff --git a/main.c b/main.c index 7ca309c..36d1b64 100644 --- a/main.c +++ b/main.c @@ -1,247 +1,79 @@ #include #include -#include // WatchDog -#include -#include "Ethernet/socket.h" -#include "Ethernet/wizchip_conf.h" - -#include "Internet/MQTT/mqtt_interface.h" -#include "Internet/MQTT/MQTTClient.h" - -#include "avrIOhelper/io-helper.h" #include "millis.h" -#include "uart.h" -#include "spi.h" -#include "mqtt.h" - -#include "util/delay.h" - -#define PLC_MQTT_ENABLED 0 - -Client mqtt_client; +#include "modbus.h" +#include "avrIOhelper/io-helper.h" -//***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN -uint8_t mcucsr_mirror __attribute__ ((section (".noinit"))); +uint16_t holdingregister; -// This is for fast WDT disable & and save reason of reset/power-up -void get_mcusr(void) \ - __attribute__((naked)) \ - __attribute__((section(".init3"))); -void get_mcusr(void) +void timer0_init() { - mcucsr_mirror = MCUSR; - MCUSR = 0; - wdt_disable(); + TCCR0A = (1<250kHz:250-=>1kHz) + TIMSK0 |= 1<>Trying connect to MQTT broker: %d.%d.%d.%d ..\r\n", MQTT_targetIP[0], MQTT_targetIP[1], MQTT_targetIP[2], MQTT_targetIP[3]); - NewNetwork(&mqtt_network); - ConnectNetwork(&mqtt_network, MQTT_targetIP, 1883); - MQTTClient(&mqtt_client, &mqtt_network, 1000, mqtt_buf, 100, mqtt_readBuffer, MQTT_BUFFER_SIZE); - - //Connection to MQTT broker - MQTTPacket_connectData data = MQTTPacket_connectData_initializer; - data.willFlag = 0; - data.MQTTVersion = 4;//3; - data.clientID.cstring = (char*)"controllino_wasserbecken"; - data.username.cstring = (char*)"Wasserbecken"; - data.password.cstring = (char*)"\0"; - data.keepAliveInterval = 10; - data.cleansession = 1; - mqtt_rc = MQTTConnect(&mqtt_client, &data); - if (mqtt_rc == SUCCESSS) - { - printf("++MQTT Connected SUCCESS: %ld\r\n", mqtt_rc); - } - else - { - printf("--MQTT Connected ERROR: %ld\r\n", mqtt_rc); - while(1); //Reboot the board - } - - // Subscribe to all topics - char SubString[] = "/Filamentanlage/02_Wasserbecken/#"; - //char SubString[] = "/Filamentanlage/03_Wasserbecken/#"; - mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived); - printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc); -#endif - - - ioHelperSetBit(outStatesBlinking, LED_PLC_OK, 1); - + timer0_init(); + + modbusSetAddress(4); + modbusInit(); + timer2_init(); + sei(); + + set_Output(LED_EXTR_FEHLER, BLINK); uint32_t timer_blink_outs = millis(); - uint32_t timer_send_uptime = millis(); - - printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL)); while(1) { - wdt_reset(); // WDT reset at least every sec - ioHelperReadPins(); ioHelperDebounce(); ioHelperEdgeDetector(); - - // Toggle all outs which are set to blinking + if(millis() - timer_blink_outs > 500){ outStates[0] ^= outStatesBlinking[0]; outStates[1] ^= outStatesBlinking[1]; + outStates[2] ^= outStatesBlinking[2]; + outStates[2] ^= outStatesBlinking[3]; timer_blink_outs = millis(); } - -#if PLC_MQTT_ENABLED - // send misc info - if(millis() - timer_send_uptime > 5000){ - timer_send_uptime += 5000; - char msg[64]; - sprintf(msg, "%ld", millis()/1000); - mqtt_pub(&mqtt_client, "/Filamentanlage/02_Wasserbecken/uptime", msg, strlen(msg)); - } -#endif - - - if(read_Input(IN_ANLAGE_EIN_INV, FALLING)){ - printf("anlage ein\n\r"); - } - // ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 1); - //} - //else{ - // ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 0); - //} - do_notaus(); - -#if PLC_MQTT_ENABLED - ioHelperSetBit(outStates, LED_BUS_OK, 1); - ioHelperSetOuts(); - MQTTYield(&mqtt_client, 10); //blocking call - ioHelperSetBit(outStates, LED_BUS_OK, 0); -#endif + ioHelperSetOuts(); - } - return 0; -} - -// Timer0 -// 1ms IRQ -// Used for millis() timing -void timer0_init(void) -{ - TCCR0A = (1<250kHz:250-=>1kHz) - TIMSK0 |= 1< - -#define SPI_PORT PORTB /* target-specific port containing the SPI lines */ -#define SPI_DDR DDRB /* target-specific DDR for the SPI port lines */ - -#define CS_DDR DDRJ /* target-specific DDR for chip-select */ -#define CS_PORT PORTJ /* target-specific port used as chip-select */ -#define CS_BIT 3 /* target-specific port line used as chip-select */ - -uint8_t spi_read(); -void spi_write(uint8_t d); -void spi_select(void); -void spi_deselect(void); -unsigned char spi_xchg(unsigned char val); -void spi_init(void); - -#endif diff --git a/uart.c b/uart.c deleted file mode 100644 index aa928c8..0000000 --- a/uart.c +++ /dev/null @@ -1,53 +0,0 @@ -#include -#include "uart.h" - -static int uart_putchar(char c, FILE *stream); - -FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); - -void uart_init() -{ - DDRD |= 1 << 1; // TX - UART_BAUD_REGH = (BAUDRATE>>8); - UART_BAUD_REGL = BAUDRATE; // set baud rate - - UART_CTRL_REGB |= (1< - -#define BAUD 9600 -#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) - -#define UART_BAUD_REGH UBRR0H -#define UART_BAUD_REGL UBRR0L - -#define UART_CTRL_REGA UCSR0A -#define UART_CTRL_REGB UCSR0B -#define UART_CTRL_REGC UCSR0C - -// UCSRA -#define UART_UDRE_BM UDRE0 -#define UART_RXC_BM RXC0 - -// UCSRB -#define UART_TXEN_BM TXEN0 -#define UART_RXEN_BM RXEN0 -#define UART_RXCIE_BM RXCIE0 - -// UCSRC -#define UART_URSEL_BM 0 /* only for old atmega */ -#define UART_UCSZ0_BM UCSZ00 -#define UART_UCSZ1_BM UCSZ01 - -#define UART_DATA_REG UDR0 - -void uart_init (void); -void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex); - -#endif - -