minimal modbus example on controllino mega
parent
4cf2f0d3fd
commit
85e6382088
@ -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 |
|
||||
@ -1,247 +1,79 @@
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/wdt.h> // WatchDog
|
||||
#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;
|
||||
#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<<WGM01); //TIMER0 SET-UP: CTC MODE
|
||||
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
|
||||
OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
|
||||
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
|
||||
}
|
||||
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
|
||||
|
||||
|
||||
//FUNC headers
|
||||
static void avr_init(void);
|
||||
void timer0_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 timer2_init()
|
||||
{
|
||||
TCCR2A = (1<<WGM21); //TIMER0 SET-UP: CTC MODE
|
||||
TCCR2B|=(1<<CS21); //prescaler 8
|
||||
OCR2A = 200;
|
||||
TIMSK2|=(1<<OCIE2A);
|
||||
}
|
||||
|
||||
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:
|
||||
modbusExchangeRegisters(outStates,0,1);
|
||||
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);
|
||||
|
||||
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<<WGM01); //TIMER0 SET-UP: CTC MODE
|
||||
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
|
||||
OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
|
||||
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
|
||||
}
|
||||
|
||||
static void avr_init(void)
|
||||
{
|
||||
// Initialize device here.
|
||||
// WatchDog INIT
|
||||
wdt_enable(WDTO_8S); // set up wdt reset interval 2 second
|
||||
wdt_reset(); // wdt reset ~ every <2000ms
|
||||
|
||||
timer0_init();// Timer0 millis engine init
|
||||
uart_init();
|
||||
|
||||
sei(); //re-enable global interrupts
|
||||
|
||||
return;
|
||||
modbusGet();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
#include "spi.h"
|
||||
|
||||
void spi_select(void)
|
||||
{
|
||||
CS_PORT&=~(1<<CS_BIT);
|
||||
}
|
||||
|
||||
void spi_deselect(void)
|
||||
{
|
||||
CS_PORT|=(1<<CS_BIT);
|
||||
}
|
||||
|
||||
unsigned char spi_xchg(unsigned char val)
|
||||
{
|
||||
SPDR = val;
|
||||
while (!(SPSR & (1 << SPIF))) ;
|
||||
return SPDR;
|
||||
}
|
||||
|
||||
uint8_t spi_read(){
|
||||
return spi_xchg(0x00);
|
||||
}
|
||||
|
||||
void spi_write(uint8_t d){
|
||||
spi_xchg(d);
|
||||
}
|
||||
|
||||
void spi_init(){
|
||||
CS_PORT |= (1 << CS_BIT); // pull CS pin high
|
||||
CS_DDR |= (1 << CS_BIT); // now make it an output
|
||||
|
||||
SPI_PORT |= (1 << 0); // make sure SS is high
|
||||
SPI_DDR = (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0); // set MOSI, SCK and SS as output, others as input
|
||||
SPCR = (1 << SPE) | (1 << MSTR); // enable SPI, master mode 0
|
||||
SPCR |= (1 << SPR0) | (0<<SPR1); // div 128
|
||||
SPSR |= (0 << SPI2X); // set the clock rate fck/2
|
||||
}
|
||||
@ -1,20 +0,0 @@
|
||||
#ifndef _SPI_H_
|
||||
#define _SPI_H_
|
||||
|
||||
#include <avr/io.h>
|
||||
|
||||
#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
|
||||
@ -1,53 +0,0 @@
|
||||
#include <avr/io.h>
|
||||
#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<<UART_TXEN_BM)
|
||||
//|(1<<UART_RXEN_BM)
|
||||
|(1<<UART_RXCIE_BM); // enable receiver and transmitter
|
||||
|
||||
UART_CTRL_REGC |= (1<<UART_URSEL_BM)
|
||||
|(1<<UART_UCSZ0_BM)
|
||||
|(1<<UART_UCSZ1_BM); // 8bit data format
|
||||
|
||||
stdout = &uart_output;
|
||||
}
|
||||
|
||||
static int uart_putchar(char c, FILE *stream) {
|
||||
if (c == '\n') {
|
||||
uart_putchar('\r', stream);
|
||||
}
|
||||
loop_until_bit_is_set(UART_CTRL_REGA, UART_UDRE_BM);
|
||||
UART_DATA_REG = c;
|
||||
return 0;
|
||||
}
|
||||
|
||||
void read_sync(char buffer[], uint8_t buffersize, uint8_t * bufferindex){
|
||||
for(int i=0; i < buffersize; i++){
|
||||
buffer[i]=0;
|
||||
}
|
||||
*bufferindex=0;
|
||||
|
||||
char input;
|
||||
do{
|
||||
while ((UART_CTRL_REGA & (1 << UART_RXC_BM)) == 0);
|
||||
input = UART_DATA_REG;
|
||||
|
||||
putchar(input); //echo
|
||||
buffer[*bufferindex]=input;
|
||||
*bufferindex=*bufferindex+1;
|
||||
}while(!(input == '\n' || input == '\r'));
|
||||
buffer[*bufferindex]=0;
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
|
||||
@ -1,37 +0,0 @@
|
||||
#ifndef _UART_H_
|
||||
#define _UART_H_
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#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
|
||||
|
||||
|
||||
Loading…
Reference in New Issue