|
|
|
@ -1,508 +1,272 @@
|
|
|
|
/*
|
|
|
|
|
|
|
|
* main.c
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Created on: 22 нояб. 2018 г.
|
|
|
|
|
|
|
|
* Author: maxx
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <util/delay.h>
|
|
|
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include <avr/wdt.h> // WatchDog
|
|
|
|
#include <compat/deprecated.h> //sbi, cbi etc..
|
|
|
|
#include <string.h>
|
|
|
|
#include "avr/wdt.h" // WatchDog
|
|
|
|
|
|
|
|
#include <stdio.h> // printf etc..
|
|
|
|
|
|
|
|
#include "string.h"
|
|
|
|
|
|
|
|
#include "uart_extd.h"
|
|
|
|
|
|
|
|
#include "spi.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "globals.h" //Global definitions for project
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "stdbool.h"
|
|
|
|
|
|
|
|
#include "Ethernet/socket.h"
|
|
|
|
#include "Ethernet/socket.h"
|
|
|
|
#include "Ethernet/wizchip_conf.h"
|
|
|
|
#include "Ethernet/wizchip_conf.h"
|
|
|
|
#include "Application/loopback/loopback.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "Internet/MQTT/mqtt_interface.h"
|
|
|
|
#include "Internet/MQTT/mqtt_interface.h"
|
|
|
|
#include "Internet/MQTT/MQTTClient.h"
|
|
|
|
#include "Internet/MQTT/MQTTClient.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define _MAIN_DEBUG_
|
|
|
|
#include "avrIOhelper/io-helper.h"
|
|
|
|
|
|
|
|
#include "millis.h"
|
|
|
|
/*
|
|
|
|
#include "uart.h"
|
|
|
|
* 22. MQTT + Mosquitto in LAN test
|
|
|
|
#include "spi.h"
|
|
|
|
*
|
|
|
|
#include "mqtt.h"
|
|
|
|
* Briefly:
|
|
|
|
|
|
|
|
* 1. Pub every 1 sec Analog && Digital values only:
|
|
|
|
#include "util/delay.h"
|
|
|
|
* sta/analog/0 - A6/PA6
|
|
|
|
|
|
|
|
* sta/digital/0 - SW1/PC5
|
|
|
|
#define PLC_MQTT_ENABLED 0
|
|
|
|
* 2. Pub every 10 sec Uptime && FreeRAM device statistic:
|
|
|
|
|
|
|
|
* /w5500_avr_dbg - Uptime: xxxx sec; Free RAM: xxxx bytes
|
|
|
|
Client mqtt_client;
|
|
|
|
* 3. Sub: Print out all received messages (Subscribe for all that begin from "/")
|
|
|
|
|
|
|
|
* 4. On lost connection with MQTT broker reboot device after ~ 20 sec.
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Used as base code from:
|
|
|
|
|
|
|
|
* Nadyrshin Ruslan - MQTTPacket (MQTT client/server v3.1.1 adapted for AVR MCU).
|
|
|
|
|
|
|
|
* YouTube-channel: https://www.youtube.com/channel/UChButpZaL5kUUl_zTyIDFkQ
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Author of unofficial porting to AVR Mega1284p/644p + W5500 Ethernet NIC (Wiznet sockets library using without Arduino):
|
|
|
|
|
|
|
|
* Ibragimov Maxim aka maxxir, Russia Togliatty 07.04.2019
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN
|
|
|
|
//***********Prologue for fast WDT disable & and save reason of reset/power-up: BEGIN
|
|
|
|
uint8_t mcucsr_mirror __attribute__ ((section (".noinit")));
|
|
|
|
uint8_t mcucsr_mirror __attribute__ ((section (".noinit")));
|
|
|
|
|
|
|
|
|
|
|
|
// This is for fast WDT disable & and save reason of reset/power-up
|
|
|
|
// This is for fast WDT disable & and save reason of reset/power-up
|
|
|
|
void get_mcusr(void) \
|
|
|
|
void get_mcusr(void) \
|
|
|
|
__attribute__((naked)) \
|
|
|
|
__attribute__((naked)) \
|
|
|
|
__attribute__((section(".init3")));
|
|
|
|
__attribute__((section(".init3")));
|
|
|
|
void get_mcusr(void)
|
|
|
|
void get_mcusr(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
mcucsr_mirror = MCUSR;
|
|
|
|
mcucsr_mirror = MCUSR;
|
|
|
|
MCUSR = 0;
|
|
|
|
MCUSR = 0;
|
|
|
|
wdt_disable();
|
|
|
|
wdt_disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
|
|
|
|
//***********Prologue for fast WDT disable & and save reason of reset/power-up: END
|
|
|
|
|
|
|
|
|
|
|
|
//*********Global vars
|
|
|
|
|
|
|
|
#define TICK_PER_SEC 1000UL
|
|
|
|
|
|
|
|
volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//*********Program metrics
|
|
|
|
|
|
|
|
const char compile_date[] PROGMEM = __DATE__; // Mmm dd yyyy - Дата компиляции
|
|
|
|
|
|
|
|
const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - Время компиляции
|
|
|
|
|
|
|
|
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.2a Static IP MQTT && Loop-back WIZNET_5500 ETHERNET 07/04/2019\r\n"; // Program name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(__AVR_ATmega128__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega128"; //CPU is m128
|
|
|
|
|
|
|
|
#elif defined (__AVR_ATmega2560__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega2560"; //CPU is m2560
|
|
|
|
|
|
|
|
#elif defined (__AVR_ATmega2561__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega2561"; //CPU is m2561
|
|
|
|
|
|
|
|
#elif defined (__AVR_ATmega328P__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega328P"; //CPU is m328p
|
|
|
|
|
|
|
|
#elif defined (__AVR_ATmega32U4__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega32u4"; //CPU is m32u4
|
|
|
|
|
|
|
|
#elif defined (__AVR_ATmega644P__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega644p"; //CPU is m644p
|
|
|
|
|
|
|
|
#elif defined (__AVR_ATmega1284P__)
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "ATmega1284p"; //CPU is m1284p
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
const char PROGMEM str_mcu[] = "Unknown CPU"; //CPU is unknown
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//******************* MQTT: BEGIN
|
|
|
|
|
|
|
|
#define SOCK_MQTT 2
|
|
|
|
|
|
|
|
// Receive Buffer
|
|
|
|
|
|
|
|
#define MQTT_BUFFER_SIZE 512 // 2048
|
|
|
|
|
|
|
|
uint8_t mqtt_readBuffer[MQTT_BUFFER_SIZE];
|
|
|
|
|
|
|
|
volatile uint16_t mes_id;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define PUBLISH_ANALOG_0 "sta/analog/0"
|
|
|
|
|
|
|
|
#define PUBLISH_DIGITAL_0 "sta/digital/0"
|
|
|
|
|
|
|
|
#define PUBLISH_AVR_DEBUG "/w5500_avr_dbg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//MQTT subscribe call-back is here
|
|
|
|
|
|
|
|
void messageArrived(MessageData* md)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
char _topic_name[64] = "\0";
|
|
|
|
|
|
|
|
char _message[128] = "\0";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MQTTMessage* message = md->message;
|
|
|
|
|
|
|
|
MQTTString* topic = md->topicName;
|
|
|
|
|
|
|
|
strncpy(_topic_name, topic->lenstring.data, topic->lenstring.len);
|
|
|
|
|
|
|
|
strncpy(_message, message->payload, message->payloadlen);
|
|
|
|
|
|
|
|
PRINTF("<<MQTT Sub: [%s] %s", _topic_name , _message);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//md->topicName->
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < md->topicName->lenstring.len; i++)
|
|
|
|
|
|
|
|
putchar(*(md->topicName->lenstring.data + i));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
printf(" (%.*s)\r\n", (int32_t)message->payloadlen, (char*)message->payload);
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_msg_len)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
static uint32_t mqtt_pub_count = 0;
|
|
|
|
|
|
|
|
static uint8_t mqtt_err_cnt = 0;
|
|
|
|
|
|
|
|
int32_t mqtt_rc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
wdt_reset();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PRINTF(">>MQTT pub msg №%lu ", ++mqtt_pub_count);
|
|
|
|
|
|
|
|
MQTTMessage pubMessage;
|
|
|
|
|
|
|
|
pubMessage.qos = QOS0;
|
|
|
|
|
|
|
|
pubMessage.id = mes_id++;
|
|
|
|
|
|
|
|
pubMessage.payloadlen = (size_t)mqtt_msg_len;
|
|
|
|
|
|
|
|
pubMessage.payload = mqtt_msg;
|
|
|
|
|
|
|
|
mqtt_rc = MQTTPublish(mqtt_client, mqtt_topic , &pubMessage);
|
|
|
|
|
|
|
|
//Analize MQTT publish result (for MQTT failover mode)
|
|
|
|
|
|
|
|
if (mqtt_rc == SUCCESSS)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mqtt_err_cnt = 0;
|
|
|
|
|
|
|
|
PRINTF(" - OK\r\n");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PRINTF(" - ERROR\r\n");
|
|
|
|
|
|
|
|
//Reboot device after 20 continuous errors (~ 20sec)
|
|
|
|
|
|
|
|
if(mqtt_err_cnt++ > 20)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
PRINTF("Connection with MQTT Broker was lost!!\r\nReboot the board..\r\n");
|
|
|
|
|
|
|
|
while(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//******************* MQTT: END
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//FUNC headers
|
|
|
|
//FUNC headers
|
|
|
|
static void avr_init(void);
|
|
|
|
static void avr_init(void);
|
|
|
|
void timer0_init(void);
|
|
|
|
void timer0_init(void);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Wiznet FUNC headers
|
|
|
|
|
|
|
|
void print_network_information(void);
|
|
|
|
void print_network_information(void);
|
|
|
|
|
|
|
|
|
|
|
|
// RAM Memory usage test
|
|
|
|
void IO_LIBRARY_Init(void) {
|
|
|
|
int freeRam (void)
|
|
|
|
uint8_t bufSize[] = {2, 2, 2, 2, 2, 2, 2, 2};
|
|
|
|
{
|
|
|
|
|
|
|
|
extern int __heap_start, *__brkval;
|
|
|
|
|
|
|
|
int v;
|
|
|
|
|
|
|
|
int _res = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
|
|
|
|
|
|
|
|
return _res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
//******************* MILLIS ENGINE: BEGIN
|
|
|
|
wizchip_init(bufSize, bufSize);
|
|
|
|
//ISR (TIMER0_COMP_vect )
|
|
|
|
wizchip_setnetinfo(&netInfo);
|
|
|
|
ISR (TIMER0_COMPA_vect)
|
|
|
|
//wizchip_setinterruptmask(IK_SOCK_0);
|
|
|
|
{
|
|
|
|
|
|
|
|
// Compare match Timer0
|
|
|
|
|
|
|
|
// Here every 1ms
|
|
|
|
|
|
|
|
_millis++; // INC millis tick
|
|
|
|
|
|
|
|
// Тест мигаем при в ходе в прерывание
|
|
|
|
|
|
|
|
// 500Hz FREQ OUT
|
|
|
|
|
|
|
|
// LED_TGL;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long millis(void)
|
|
|
|
#define STEP_SIZE 5
|
|
|
|
{
|
|
|
|
#define TOP_VALUE 40
|
|
|
|
unsigned long i;
|
|
|
|
void do_luefter(){
|
|
|
|
cli();
|
|
|
|
static uint8_t fan_value = TOP_VALUE*0.8;
|
|
|
|
// Atomic tick reading
|
|
|
|
static uint8_t fan_state = 0;
|
|
|
|
i = _millis;
|
|
|
|
|
|
|
|
sei();
|
|
|
|
if(!read_Input(IN_ANLAGE_EIN, LEVEL)){
|
|
|
|
return i;
|
|
|
|
fan_state = 0;
|
|
|
|
}
|
|
|
|
ioHelperSetBit(outStates, LED_LUEFTER, 0);
|
|
|
|
//******************* MILLIS ENGINE: END
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
//***************** UART0: BEGIN
|
|
|
|
if (read_Input(BTN_LUEFTER_EIN, RISING)) {
|
|
|
|
// Assign I/O stream to UART
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
/* define CPU frequency in Mhz here if not defined in Makefile */
|
|
|
|
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/Luefter", "ein", 3);
|
|
|
|
//#ifndef F_CPU
|
|
|
|
#endif
|
|
|
|
//#define F_CPU 16000000UL
|
|
|
|
OCR3B = TOP_VALUE/2;
|
|
|
|
//#endif
|
|
|
|
fan_state = 1;
|
|
|
|
|
|
|
|
ioHelperSetBit(outStates, LED_LUEFTER, 1);
|
|
|
|
/* 19200 baud */
|
|
|
|
|
|
|
|
//#define UART_BAUD_RATE 19200
|
|
|
|
|
|
|
|
//#define UART_BAUD_RATE 38400
|
|
|
|
|
|
|
|
#define UART_BAUD_RATE 115200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int uart0_putchar(char ch,FILE *stream);
|
|
|
|
|
|
|
|
static void uart0_rx_flash(void);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static FILE uart0_stdout = FDEV_SETUP_STREAM(uart0_putchar, NULL, _FDEV_SETUP_WRITE);
|
|
|
|
|
|
|
|
//PS. stdin не переназначаю, т.к. удобнее с ним работать через uart.h - api:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Т.е. например так
|
|
|
|
|
|
|
|
c = uart1_getc();
|
|
|
|
|
|
|
|
if (( c & UART_NO_DATA ) == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
uart1_putc( (unsigned char)c );
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
При этом чекаем что буфер приема не пуст и опрос идет неблокирующий (+ работаем через UART RX RINGBUFFER),
|
|
|
|
|
|
|
|
а если работаем в стиле stdin->getchar() там опрос блокируется пока символ не будет принят (поллинг)
|
|
|
|
|
|
|
|
через UART1_RX, т.е. неудобно.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// STDOUT UART0 TX handler
|
|
|
|
|
|
|
|
static int uart0_putchar(char ch,FILE *stream)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
uart_putc(ch);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Очищаем буфер приема UART1 RX (иногда нужно)
|
|
|
|
|
|
|
|
static void uart0_rx_flash(void)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
// Считываем все из ring-buffer UART1 RX
|
|
|
|
|
|
|
|
unsigned int c;
|
|
|
|
|
|
|
|
do
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
c = uart_getc();
|
|
|
|
|
|
|
|
} while (( c & UART_NO_DATA ) == 0); // Check RX1 none-empty
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
if (read_Input(BTN_LUEFTER_AUS, RISING)) {
|
|
|
|
//***************** UART0: END
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/Luefter", "aus", 3);
|
|
|
|
//***************** ADC: BEGIN
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ADC_DIV
|
|
|
|
|
|
|
|
//12.5MHz or over use this ADC reference clock
|
|
|
|
|
|
|
|
#define ADC_DIV (1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0) //:128 ADC Prescaler
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
fan_state = 0;
|
|
|
|
|
|
|
|
ioHelperSetBit(outStates, LED_LUEFTER, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef ADC_REF
|
|
|
|
if (read_Input(BTN_LUEFTER_PLUS, RISING) && (fan_value+STEP_SIZE <= TOP_VALUE)) {
|
|
|
|
// vcc voltage ref default
|
|
|
|
fan_value += STEP_SIZE;
|
|
|
|
#define ADC_REF (1<<REFS0)
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
|
|
|
|
char _msg[3];
|
|
|
|
|
|
|
|
sprintf(_msg, "%d", fan_value);
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/Speed", _msg, 3);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
printf("luefter %d\n\r", fan_value);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void adc_init(void)
|
|
|
|
if (read_Input(BTN_LUEFTER_MINUS, RISING) && (fan_value-STEP_SIZE >= STEP_SIZE)) {
|
|
|
|
{
|
|
|
|
fan_value -= STEP_SIZE;
|
|
|
|
ADCSRA = 0;
|
|
|
|
printf("luefter %d\n\r", fan_value);
|
|
|
|
ADCSRA |= (ADC_DIV); // ADC reference clock
|
|
|
|
}
|
|
|
|
ADMUX |= (ADC_REF); // Voltage reference
|
|
|
|
}
|
|
|
|
ADCSRA |= (1<<ADEN); // Turn on ADC
|
|
|
|
|
|
|
|
ADCSRA |= (1<<ADSC); // Do an initial conversion because this one is the
|
|
|
|
|
|
|
|
// slowest and to ensure that everything is up
|
|
|
|
|
|
|
|
// and running
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t adc_read(uint8_t channel)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
ADMUX &= 0b11100000; //Clear the older channel that was read
|
|
|
|
|
|
|
|
ADMUX |= channel; //Defines the new ADC channel to be read
|
|
|
|
|
|
|
|
ADCSRA |= (1<<ADSC); //Starts a new conversion
|
|
|
|
|
|
|
|
while(ADCSRA & (1<<ADSC)); //Wait until the conversion is done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ADCW; //Returns the ADC value of the chosen channel
|
|
|
|
if(fan_state)
|
|
|
|
|
|
|
|
OCR3B = fan_value;
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
OCR3B = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//***************** ADC: END
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//***************** WIZCHIP INIT: BEGIN
|
|
|
|
void do_zumbach(){
|
|
|
|
#define SOCK_TCPS 0
|
|
|
|
|
|
|
|
#define SOCK_UDPS 1
|
|
|
|
|
|
|
|
#define PORT_TCPS 5000
|
|
|
|
|
|
|
|
#define PORT_UDPS 3000
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define ETH_MAX_BUF_SIZE LOOPBACK_DATA_BUF_SIZE
|
|
|
|
if (read_Input(BTN_ZUMBACH_EIN, RISING) && read_Input(IN_ANLAGE_EIN, LEVEL)) {
|
|
|
|
|
|
|
|
ioHelperSetBit(outStates, SCHUETZ_ZUMBACH, 1);
|
|
|
|
unsigned char ethBuf0[ETH_MAX_BUF_SIZE];
|
|
|
|
ioHelperSetBit(outStates, LED_ZUMBACH, 1);
|
|
|
|
unsigned char ethBuf1[ETH_MAX_BUF_SIZE];
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/Zumbach", "ein", 3);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cs_sel() {
|
|
|
|
if (read_Input(BTN_ZUMBACH_AUS, RISING) || read_Input(IN_ANLAGE_EIN, FALLING)) {
|
|
|
|
SPI_WIZNET_ENABLE();
|
|
|
|
ioHelperSetBit(outStates, SCHUETZ_ZUMBACH, 0);
|
|
|
|
|
|
|
|
ioHelperSetBit(outStates, LED_ZUMBACH, 0);
|
|
|
|
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/Zumbach", "aus", 3);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void cs_desel() {
|
|
|
|
void do_notaus(){
|
|
|
|
SPI_WIZNET_DISABLE();
|
|
|
|
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, LEVEL)){
|
|
|
|
|
|
|
|
set_Output(LED_GRN_NOTAUS_ANLAGE, ON);
|
|
|
|
|
|
|
|
set_Output(LED_GRN_NOTAUS_SCHRANK, ON);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else{
|
|
|
|
|
|
|
|
set_Output(LED_GRN_NOTAUS_ANLAGE, BLINK);
|
|
|
|
|
|
|
|
set_Output(LED_GRN_NOTAUS_SCHRANK, BLINK);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t spi_rb(void) {
|
|
|
|
int main()
|
|
|
|
uint8_t rbuf;
|
|
|
|
{
|
|
|
|
//HAL_SPI_Receive(&hspi1, &rbuf, 1, HAL_MAX_DELAY);
|
|
|
|
// INIT MCU
|
|
|
|
SPI_READ(rbuf);
|
|
|
|
avr_init();
|
|
|
|
return rbuf;
|
|
|
|
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";
|
|
|
|
|
|
|
|
data.username.cstring = (char*)"Messmodul";
|
|
|
|
|
|
|
|
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/04_Messmodul/set/#";
|
|
|
|
|
|
|
|
mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
|
|
|
|
|
|
|
|
printf("Subscribed (%s) %ld\r\n", SubString, mqtt_rc);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void spi_wb(uint8_t b) {
|
|
|
|
|
|
|
|
//HAL_SPI_Transmit(&hspi1, &b, 1, HAL_MAX_DELAY);
|
|
|
|
|
|
|
|
SPI_WRITE(b);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void spi_rb_burst(uint8_t *buf, uint16_t len) {
|
|
|
|
ioHelperSetBit(outStatesBlinking, LED_PLC_OK, 1);
|
|
|
|
//HAL_SPI_Receive_DMA(&hspi1, buf, len);
|
|
|
|
|
|
|
|
//while(HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_BUSY_RX);
|
|
|
|
|
|
|
|
for (uint16_t var = 0; var < len; var++) {
|
|
|
|
|
|
|
|
SPI_READ(*buf++);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void spi_wb_burst(uint8_t *buf, uint16_t len) {
|
|
|
|
uint32_t timer_blink_outs = millis();
|
|
|
|
//HAL_SPI_Transmit_DMA(&hspi1, buf, len);
|
|
|
|
uint32_t timer_send_uptime = millis();
|
|
|
|
//while(HAL_SPI_GetState(&hspi1) == HAL_SPI_STATE_BUSY_TX);
|
|
|
|
|
|
|
|
for (uint16_t var = 0; var < len; var++) {
|
|
|
|
|
|
|
|
SPI_WRITE(*buf++);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void IO_LIBRARY_Init(void) {
|
|
|
|
OCR3B = 127;
|
|
|
|
uint8_t bufSize[] = {2, 2, 2, 2, 2, 2, 2, 2};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
|
|
|
|
while(1)
|
|
|
|
reg_wizchip_spi_cbfunc(spi_rb, spi_wb);
|
|
|
|
{
|
|
|
|
reg_wizchip_spiburst_cbfunc(spi_rb_burst, spi_wb_burst);
|
|
|
|
wdt_reset(); // WDT reset at least every sec
|
|
|
|
|
|
|
|
|
|
|
|
wizchip_init(bufSize, bufSize);
|
|
|
|
//if (flag_refresh_inStates) {
|
|
|
|
wizchip_setnetinfo(&netInfo);
|
|
|
|
ioHelperReadPins();
|
|
|
|
//wizchip_setinterruptmask(IK_SOCK_0);
|
|
|
|
ioHelperDebounce();
|
|
|
|
}
|
|
|
|
ioHelperEdgeDetector();
|
|
|
|
//***************** WIZCHIP INIT: END
|
|
|
|
// flag_refresh_inStates = 0;
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
int main()
|
|
|
|
// Toggle all outs which are set to blinking
|
|
|
|
{
|
|
|
|
if(millis() - timer_blink_outs > 500){
|
|
|
|
//uint8_t prev_sw1 = 1; // VAR for sw1 pressing detect
|
|
|
|
outStates[0] ^= outStatesBlinking[0];
|
|
|
|
|
|
|
|
outStates[1] ^= outStatesBlinking[1];
|
|
|
|
// INIT MCU
|
|
|
|
timer_blink_outs = millis();
|
|
|
|
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)
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
|
|
|
|
// send misc info
|
|
|
|
|
|
|
|
if(millis() - timer_send_uptime > 5000){
|
|
|
|
// Print program metrics
|
|
|
|
timer_send_uptime += 5000;
|
|
|
|
PRINTF("%S", str_prog_name);// Название программы
|
|
|
|
char msg[64];
|
|
|
|
PRINTF("Compiled at: %S %S\r\n", compile_time, compile_date);// Время Дата компиляции
|
|
|
|
sprintf(msg, "%ld", millis()/1000);
|
|
|
|
PRINTF(">> MCU is: %S; CLK is: %luHz\r\n", str_mcu, F_CPU);// MCU Name && FREQ
|
|
|
|
mqtt_pub(&mqtt_client, "/Filamentanlage/04_Messmodul/state/uptime", msg, strlen(msg));
|
|
|
|
PRINTF(">> Free RAM is: %d bytes\r\n", freeRam());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Wizchip WIZ5500 Ethernet initialize
|
|
|
|
|
|
|
|
IO_LIBRARY_Init(); //After that ping must working
|
|
|
|
|
|
|
|
print_network_information();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Short Blink LED 3 times on startup
|
|
|
|
|
|
|
|
unsigned char i = 3;
|
|
|
|
|
|
|
|
while(i--)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
led1_high();
|
|
|
|
|
|
|
|
_delay_ms(100);
|
|
|
|
|
|
|
|
led1_low();
|
|
|
|
|
|
|
|
_delay_ms(400);
|
|
|
|
|
|
|
|
wdt_reset();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//****************MQTT client initialize
|
|
|
|
|
|
|
|
//Find MQTT broker and connect with it
|
|
|
|
|
|
|
|
uint8_t mqtt_buf[100];
|
|
|
|
|
|
|
|
int32_t mqtt_rc = 0;
|
|
|
|
|
|
|
|
Network mqtt_network;
|
|
|
|
|
|
|
|
Client mqtt_client;
|
|
|
|
|
|
|
|
mqtt_network.my_socket = SOCK_MQTT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Можно определить IP узла по DNS-имени, IP узла будет в массиве targetIP
|
|
|
|
|
|
|
|
//DNS_init(1, tempBuffer);
|
|
|
|
|
|
|
|
//DNS_run(gWIZNETINFO.dns, "test.mosquitto.org", targetIP);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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*)"w5500_avr_client";
|
|
|
|
|
|
|
|
data.username.cstring = (char*)"user1234";
|
|
|
|
|
|
|
|
data.password.cstring = (char*)"\0";
|
|
|
|
|
|
|
|
data.keepAliveInterval = 60;
|
|
|
|
|
|
|
|
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[] = "/#";// Subscribe for all that begin from "/"
|
|
|
|
|
|
|
|
mqtt_rc = MQTTSubscribe(&mqtt_client, SubString, QOS0, messageArrived);
|
|
|
|
|
|
|
|
PRINTF("Subscribed (%s) %d\r\n", SubString, mqtt_rc);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t timer_link_1sec = millis();
|
|
|
|
|
|
|
|
uint32_t timer_uptime_60sec = millis();
|
|
|
|
|
|
|
|
uint32_t timer_mqtt_pub_1sec = millis();
|
|
|
|
|
|
|
|
while(1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//Here at least every 1sec
|
|
|
|
|
|
|
|
wdt_reset(); // WDT reset at least every sec
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Use Hercules Terminal to check loopback tcp:5000 and udp:3000
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* https://www.hw-group.com/software/hercules-setup-utility
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
loopback_tcps(SOCK_TCPS,ethBuf0,PORT_TCPS);
|
|
|
|
|
|
|
|
loopback_udps(SOCK_UDPS,ethBuf0,PORT_UDPS);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MQTT pub event every 1 sec
|
|
|
|
|
|
|
|
if((millis()-timer_mqtt_pub_1sec)> 1000)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//here every 1 sec
|
|
|
|
|
|
|
|
timer_mqtt_pub_1sec = millis();
|
|
|
|
|
|
|
|
static uint8_t mqtt_10sec_cnt =0;
|
|
|
|
|
|
|
|
static char _msg[64] = "\0";
|
|
|
|
|
|
|
|
static int _len;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Every 1sec send status POT A6 (ADC input)
|
|
|
|
|
|
|
|
_len = SPRINTF(_msg, "%u", adc_read(6));
|
|
|
|
|
|
|
|
if(_len > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, PUBLISH_ANALOG_0, _msg, _len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// && SW1 (GPIO input)
|
|
|
|
|
|
|
|
uint16_t val = sw1_read()?0:!0;
|
|
|
|
|
|
|
|
_len = SPRINTF(_msg, "%u", val);
|
|
|
|
|
|
|
|
if(_len > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, PUBLISH_DIGITAL_0, _msg, _len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Every 10sec public message: "Uptime: xxx sec; Free RAM: xxxxx bytes" to "/w5500_avr_dbg"
|
|
|
|
|
|
|
|
if(++mqtt_10sec_cnt>9)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mqtt_10sec_cnt = 0;
|
|
|
|
|
|
|
|
_len = SPRINTF(_msg, "Uptime: %lu sec; Free RAM: %d bytes\r\n", millis()/1000, freeRam());
|
|
|
|
|
|
|
|
if(_len > 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
mqtt_pub(&mqtt_client, PUBLISH_AVR_DEBUG, _msg, _len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// MQTT broker connection and sub receive
|
|
|
|
|
|
|
|
MQTTYield(&mqtt_client, 100);//~100msec blocking here
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((millis()-timer_link_1sec)> 1000)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//here every 1 sec
|
|
|
|
|
|
|
|
timer_link_1sec = millis();
|
|
|
|
|
|
|
|
if(wizphy_getphylink() == PHY_LINK_ON)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
led1_high();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
led1_low();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((millis()-timer_uptime_60sec)> 60000)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
//here every 60 sec
|
|
|
|
|
|
|
|
timer_uptime_60sec = millis();
|
|
|
|
|
|
|
|
#ifdef CHK_RAM_LEAKAGE
|
|
|
|
|
|
|
|
//Printout RAM usage every 1 minute
|
|
|
|
|
|
|
|
PRINTF(">> Free RAM is: %d bytes\r\n", freeRam());
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CHK_UPTIME
|
|
|
|
|
|
|
|
//Printout RAM usage every 1 minute
|
|
|
|
if(read_Input(BTN_ANLAGE_EIN, RISING)){
|
|
|
|
PRINTF(">> Uptime %lu sec\r\n", millis()/1000);
|
|
|
|
printf("anlage in: %x\n\r", read_Input(IN_ANLAGE_EIN, LEVEL));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 1);
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
//else{
|
|
|
|
|
|
|
|
// ioHelperSetBit(outStates, LED_GRN_NOTAUS_SCHRANK, 0);
|
|
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do_luefter();
|
|
|
|
|
|
|
|
do_notaus();
|
|
|
|
|
|
|
|
do_zumbach();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if PLC_MQTT_ENABLED
|
|
|
|
|
|
|
|
ioHelperSetBit(outStates, LED_BUS_OK, 1);
|
|
|
|
|
|
|
|
ioHelperSetOuts();
|
|
|
|
|
|
|
|
MQTTYield(&mqtt_client, 10); //blocking call
|
|
|
|
|
|
|
|
ioHelperSetBit(outStates, LED_BUS_OK, 0);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
ioHelperSetOuts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Timer0
|
|
|
|
// Timer0
|
|
|
|
@ -510,80 +274,58 @@ int main()
|
|
|
|
// Used for millis() timing
|
|
|
|
// Used for millis() timing
|
|
|
|
void timer0_init(void)
|
|
|
|
void timer0_init(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
TCCR0A = (1<<WGM01); //TIMER0 SET-UP: CTC MODE
|
|
|
|
*
|
|
|
|
TCCR0B = (1<<CS01)|(1<<CS00); // PS 1:64
|
|
|
|
* For M128
|
|
|
|
OCR0A = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
|
|
|
|
TCCR0 = (1<<CS02)|(1<<WGM01); //TIMER0 SET-UP: CTC MODE & PS 1:64
|
|
|
|
TIMSK0 |= 1<<OCIE0A; //IRQ on TIMER0 output compareA
|
|
|
|
OCR0 = 249; // 1ms reach for clear (16mz:64=>250kHz:250-=>1kHz)
|
|
|
|
|
|
|
|
TIMSK |= 1<<OCIE0; //IRQ on TIMER0 output compare
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
//For M664p
|
|
|
|
|
|
|
|
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)
|
|
|
|
void timer3_init(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Initialize device here.
|
|
|
|
TCCR3A |= (1<<WGM30); // PWM Mode with ocra top
|
|
|
|
// WatchDog INIT
|
|
|
|
TCCR3B |= (1<<WGM33);
|
|
|
|
wdt_enable(WDTO_8S); // set up wdt reset interval 2 second
|
|
|
|
OCR3A = TOP_VALUE;
|
|
|
|
wdt_reset(); // wdt reset ~ every <2000ms
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
timer0_init();// Timer0 millis engine init
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Initial UART Peripheral
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
* Initialize uart11 library, pass baudrate and AVR cpu clock
|
|
|
|
|
|
|
|
* with the macro
|
|
|
|
|
|
|
|
* uart1_BAUD_SELECT() (normal speed mode )
|
|
|
|
|
|
|
|
* or
|
|
|
|
|
|
|
|
* uart1_BAUD_SELECT_DOUBLE_SPEED() ( double speed mode)
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if (UART_BAUD_RATE == 115200)
|
|
|
|
|
|
|
|
uart_init( UART_BAUD_SELECT_DOUBLE_SPEED(UART_BAUD_RATE,F_CPU) ); // To works without error on 115200 bps/F_CPU=16Mhz
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Define Output/Input Stream
|
|
|
|
|
|
|
|
stdout = &uart0_stdout;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//ADC init
|
|
|
|
|
|
|
|
adc_init();
|
|
|
|
|
|
|
|
adc_read(0); //Dummy read
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TCCR3B |= (0<<CS32)|(1<<CS31)|(0<<CS30); // PS 1:1
|
|
|
|
|
|
|
|
TCCR3A |= (1<<COM3B1) | (0<<COM3B0);
|
|
|
|
|
|
|
|
DDRE |= 1 << 4;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
led1_conf();
|
|
|
|
static void avr_init(void)
|
|
|
|
led1_low();// LED1 is OFF
|
|
|
|
{
|
|
|
|
|
|
|
|
// Initialize device here.
|
|
|
|
|
|
|
|
// WatchDog INIT
|
|
|
|
|
|
|
|
wdt_enable(WDTO_8S); // set up wdt reset interval 2 second
|
|
|
|
|
|
|
|
wdt_reset(); // wdt reset ~ every <2000ms
|
|
|
|
|
|
|
|
|
|
|
|
sw1_conf();//SW1 internal pull-up
|
|
|
|
timer0_init();// Timer0 millis engine init
|
|
|
|
|
|
|
|
timer3_init();
|
|
|
|
|
|
|
|
uart_init();
|
|
|
|
|
|
|
|
|
|
|
|
sei(); //re-enable global interrupts
|
|
|
|
sei(); //re-enable global interrupts
|
|
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void print_network_information(void)
|
|
|
|
void print_network_information(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t tmpstr[6] = {0,};
|
|
|
|
uint8_t tmpstr[6] = {0,};
|
|
|
|
ctlwizchip(CW_GET_ID,(void*)tmpstr); // Get WIZCHIP name
|
|
|
|
ctlwizchip(CW_GET_ID,(void*)tmpstr); // Get WIZCHIP name
|
|
|
|
PRINTF("\r\n=======================================\r\n");
|
|
|
|
printf("\r\n=======================================\r\n");
|
|
|
|
PRINTF(" WIZnet chip: %s \r\n", tmpstr);
|
|
|
|
printf(" WIZnet chip: %s \r\n", tmpstr);
|
|
|
|
PRINTF("=======================================\r\n");
|
|
|
|
printf("=======================================\r\n");
|
|
|
|
|
|
|
|
|
|
|
|
wiz_NetInfo gWIZNETINFO;
|
|
|
|
wiz_NetInfo gWIZNETINFO;
|
|
|
|
wizchip_getnetinfo(&gWIZNETINFO);
|
|
|
|
wizchip_getnetinfo(&gWIZNETINFO);
|
|
|
|
if (gWIZNETINFO.dhcp == NETINFO_STATIC)
|
|
|
|
if (gWIZNETINFO.dhcp == NETINFO_STATIC)
|
|
|
|
PRINTF("STATIC IP\r\n");
|
|
|
|
printf("STATIC IP\r\n");
|
|
|
|
else
|
|
|
|
else
|
|
|
|
PRINTF("DHCP IP\r\n");
|
|
|
|
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("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("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("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("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]);
|
|
|
|
printf("DNS Server : %d.%d.%d.%d\n\r",gWIZNETINFO.dns[0],gWIZNETINFO.dns[1],gWIZNETINFO.dns[2],gWIZNETINFO.dns[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|