add modbus (crashing)

master
Eggert Jung 3 years ago
parent becc43adf8
commit a3a7810658

@ -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

@ -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

248
main.c

@ -1,155 +1,87 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h> // WatchDog
#include <stdint.h>
#include <string.h>
#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;
//***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN
uint8_t mcucsr_mirror __attribute__ ((section (".noinit")));
// 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)
{
mcucsr_mirror = MCUSR;
MCUSR = 0;
wdt_disable();
}
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
#include "modbus.h"
//FUNC headers
static void avr_init(void);
void timer0_init(void);
void timer2_init(void);
void print_network_information(void);
void IO_LIBRARY_Init(void) {
uint8_t bufSize[] = {2, 2, 2, 2, 2, 2, 2, 2};
reg_wizchip_cs_cbfunc(spi_select, spi_deselect);
reg_wizchip_spi_cbfunc(spi_read, spi_write);
//reg_wizchip_spiburst_cbfunc(spi_rb_burst, spi_wb_burst);
wizchip_init(bufSize, bufSize);
wizchip_setnetinfo(&netInfo);
//wizchip_setinterruptmask(IK_SOCK_0);
}
void do_notaus(){
if(read_Input(IN_NOTAUS_ANLAGE, LEVEL) || read_Input(IN_NOTAUS_SCHRANK, LEVEL)){ // NOTAUS
set_Output(LED_GRN_NOTAUS_SCHRANK, OFF); // disable green lamps
set_Output(LED_GRN_NOTAUS_ANLAGE, OFF);
}
if(read_Input(IN_NOTAUS_ANLAGE, LEVEL) && read_Input(IN_NOTAUS_SCHRANK, LEVEL)){ // both activated
set_Output(LED_ROT_NOTAUS_ANLAGE, BLINK);
set_Output(LED_ROT_NOTAUS_SCHRANK, BLINK);
}
else if(read_Input(IN_NOTAUS_ANLAGE, LEVEL)){ // top one activated
set_Output(LED_ROT_NOTAUS_ANLAGE, BLINK);
set_Output(LED_ROT_NOTAUS_SCHRANK, ON);
}
else if(read_Input(IN_NOTAUS_SCHRANK, LEVEL)){ // bottom one activated
set_Output(LED_ROT_NOTAUS_SCHRANK, BLINK);
set_Output(LED_ROT_NOTAUS_ANLAGE, ON);
}
else{ // none activated
set_Output(LED_ROT_NOTAUS_SCHRANK, OFF);
set_Output(LED_ROT_NOTAUS_ANLAGE, OFF);
if(!read_Input(IN_ANLAGE_EIN_INV, LEVEL)){
set_Output(LED_GRN_NOTAUS_ANLAGE, ON);
set_Output(LED_GRN_NOTAUS_SCHRANK, ON);
}
else{
set_Output(LED_GRN_NOTAUS_ANLAGE, ON);
set_Output(LED_GRN_NOTAUS_SCHRANK, ON);
set_Output(LED_ROT_NOTAUS_ANLAGE, ON);
set_Output(LED_ROT_NOTAUS_SCHRANK, ON);
}
}
void modbusGet(void) {
if (modbusGetBusState() & (1<<ReceiveCompleted))
{
switch(rxbuffer[1]) {
case fcPresetSingleRegister:
case fcPresetMultipleRegisters:
case fcReadHoldingRegisters:
//;
//uint16_t test;
//modbusExchangeRegisters(&test,0,1);
modbusExchangeRegisters((void *)outStates,0,NUMBER_OF_INPUT_BYTES/2);
break;
case fcReadInputRegisters:
if(modbusRequestedAddress() < 100)
modbusExchangeRegisters((void *)inStates,0,NUMBER_OF_INPUT_BYTES/2);
else if(modbusRequestedAddress() < 200)
modbusExchangeRegisters((void *)inStatesBothEdges,100,NUMBER_OF_INPUT_BYTES/2);
else if(modbusRequestedAddress() < 300)
modbusExchangeRegisters((void *)inStatesFallingEdge,200,NUMBER_OF_INPUT_BYTES/2);
else if(modbusRequestedAddress() < 400)
modbusExchangeRegisters((void *)inStatesRisingEdge,300,NUMBER_OF_INPUT_BYTES/2);
break;
case fcReadInputStatus:
if(modbusRequestedAddress() < 100)
modbusExchangeBits(inStates,0,NUMBER_OF_INPUT_BYTES*8);
else if(modbusRequestedAddress() < 200)
modbusExchangeBits(inStatesBothEdges,100,NUMBER_OF_INPUT_BYTES*8);
else if(modbusRequestedAddress() < 300)
modbusExchangeBits(inStatesFallingEdge,200,NUMBER_OF_INPUT_BYTES*8);
else if(modbusRequestedAddress() < 400)
modbusExchangeBits(inStatesRisingEdge,300,NUMBER_OF_INPUT_BYTES*8);
break;
case fcForceMultipleCoils:
case fcForceSingleCoil:
case fcReadCoilStatus:
if(modbusRequestedAddress() < 100)
modbusExchangeBits(outStates,0,NUMBER_OF_INPUT_BYTES*8);
else if(modbusRequestedAddress() < 200)
modbusExchangeBits(outStatesBlinking,100,NUMBER_OF_INPUT_BYTES*8);
break;
default:
modbusSendException(ecIllegalFunction);
break;
}
}
}
int main()
{
// INIT MCU
avr_init();
spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5)
//spi_speed_tst(); / Here on SPI pins: MOSI 400Khz freq out, on SCLK 3.2MhzOUT (Witk SPI CLK 4Mhz)
ioHelperInitBuffer();
ioHelperIoConf();
//Wizchip WIZ5500 Ethernet initialize
IO_LIBRARY_Init(); //After that ping must working
print_network_information();
#if PLC_MQTT_ENABLED
//****************MQTT client initialize
//Find MQTT broker and connect with it
uint8_t mqtt_buf[100];
int32_t mqtt_rc = 0;
Network mqtt_network;
mqtt_network.my_socket = SOCK_MQTT;
printf(">>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);
modbusSetAddress(4);
modbusInit();
timer2_init();
uint32_t timer_blink_outs = millis();
uint32_t timer_send_uptime = millis();
printf("anlage: %x\n\r", read_Input(IN_ANLAGE_EIN_INV, LEVEL));
set_Output(LED_EXTR_FEHLER, BLINK);
printf("moin\n");
while(1)
{
wdt_reset(); // WDT reset at least every sec
@ -158,49 +90,28 @@ int main()
ioHelperDebounce();
ioHelperEdgeDetector();
modbusGet();
// 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));
printf("bus state: %d\n", modbusGetBusState());
printf("DE /RE state: %d\n", PORTJ & (_BV(5)|_BV(6)) );
}
#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)
void timer0_init()
{
TCCR0A = (1<<WGM01); //TIMER0 SET-UP: CTC MODE
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
@ -208,6 +119,16 @@ void timer0_init(void)
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
}
void timer2_init()
{
TCCR2A = (1<<WGM21); //TIMER0 SET-UP: CTC MODE
TCCR2B|=(1<<CS21); //prescaler 8
OCR2A = 200;
TIMSK2|=(1<<OCIE2A);
}
static void avr_init(void)
{
// Initialize device here.
@ -216,6 +137,7 @@ static void avr_init(void)
wdt_reset(); // wdt reset ~ every <2000ms
timer0_init();// Timer0 millis engine init
//timer3_init();
uart_init();
sei(); //re-enable global interrupts
@ -223,25 +145,7 @@ static void avr_init(void)
return;
}
void print_network_information(void)
{
uint8_t tmpstr[6] = {0,};
ctlwizchip(CW_GET_ID,(void*)tmpstr); // Get WIZCHIP name
printf("\r\n=======================================\r\n");
printf(" WIZnet chip: %s \r\n", tmpstr);
printf("=======================================\r\n");
wiz_NetInfo gWIZNETINFO;
wizchip_getnetinfo(&gWIZNETINFO);
if (gWIZNETINFO.dhcp == NETINFO_STATIC)
printf("STATIC IP\r\n");
else
printf("DHCP IP\r\n");
printf("Mac address: %02x:%02x:%02x:%02x:%02x:%02x\n\r",gWIZNETINFO.mac[0],gWIZNETINFO.mac[1],gWIZNETINFO.mac[2],gWIZNETINFO.mac[3],gWIZNETINFO.mac[4],gWIZNETINFO.mac[5]);
printf("IP address : %d.%d.%d.%d\n\r",gWIZNETINFO.ip[0],gWIZNETINFO.ip[1],gWIZNETINFO.ip[2],gWIZNETINFO.ip[3]);
printf("SM Mask : %d.%d.%d.%d\n\r",gWIZNETINFO.sn[0],gWIZNETINFO.sn[1],gWIZNETINFO.sn[2],gWIZNETINFO.sn[3]);
printf("Gate way : %d.%d.%d.%d\n\r",gWIZNETINFO.gw[0],gWIZNETINFO.gw[1],gWIZNETINFO.gw[2],gWIZNETINFO.gw[3]);
printf("DNS Server : %d.%d.%d.%d\n\r",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
ISR(TIMER2_COMPA_vect) { //this ISR is called 9765.625 times per second
modbusTickTimer();
}