add helper stuff
parent
979856fd12
commit
09e741a3ea
@ -0,0 +1,55 @@
|
|||||||
|
TARGET = main
|
||||||
|
SRCS := $(shell find -name '*.c')
|
||||||
|
FILES = $(SRCS:%.c=%) #main uart avrIOhelper/io-helper #uart#hier alle c-Datein reinschreiben, trennung durch " " und ohne .c-Endung
|
||||||
|
MCU = atmega2560
|
||||||
|
PROGC = m2560
|
||||||
|
CC = avr-gcc
|
||||||
|
#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
|
||||||
|
|
||||||
|
CFLAGS =-mmcu=$(MCU) -O2 -Wall -Wpedantic $(DEFINES) -std=c99 -ffunction-sections -fdata-sections
|
||||||
|
LDFLAGS =-mmcu=$(MCU) -Wl,--gc-sections
|
||||||
|
|
||||||
|
LDFILES = $(foreach FILE,$(FILES),$(BUILDDIR)/$(FILE).o)
|
||||||
|
|
||||||
|
all: clean $(BUILDDIR)/$(TARGET).elf
|
||||||
|
|
||||||
|
$(BUILDDIR)/%.o: %.c
|
||||||
|
mkdir -p $(dir $@)
|
||||||
|
$(CC) $(CFLAGS) -c $< -o $(BUILDDIR)/$*.o
|
||||||
|
|
||||||
|
$(BUILDDIR)/$(TARGET).elf: $(LDFILES)
|
||||||
|
mkdir -p $(dir $@)
|
||||||
|
$(CC) $(LDFLAGS) $(LDFILES) -o $(BUILDDIR)/$(TARGET).elf
|
||||||
|
|
||||||
|
$(BUILDDIR)/$(TARGET).hex : $(BUILDDIR)/$(TARGET).elf
|
||||||
|
avr-objcopy -j .data -j .text -O ihex $< $@
|
||||||
|
|
||||||
|
fuse:
|
||||||
|
avrdude -p $(PROGC) -c $(TOOL) -U lfuse:w:0xE8:m -U hfuse:w:0xD1:m
|
||||||
|
|
||||||
|
load: $(BUILDDIR)/$(TARGET).hex
|
||||||
|
avrdude -p $(PROGC) -c $(TOOL) -U flash:w:$(BUILDDIR)/$(TARGET).hex -v -B 4MHz
|
||||||
|
|
||||||
|
program: clean load
|
||||||
|
|
||||||
|
size: $(BUILDDIR)/$(TARGET).elf
|
||||||
|
avr-size -C --mcu=$(MCU) $(BUILDDIR)/$(TARGET).elf
|
||||||
|
|
||||||
|
.PHONY=clean
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILDDIR)
|
||||||
|
|
||||||
|
|
||||||
|
#Fuse m1284p external Osz. Long startuptime
|
||||||
|
# avrdude -c usbasp-clone -p m1284p -U lfuse:w:0xff:m -U hfuse:w:0xd9:m -U efuse:w:0xff:m
|
||||||
|
|
||||||
|
#Fuse m1284p internal Osz. Long startuptime
|
||||||
|
# avrdude -c usbasp-clone -p m1284p -U lfuse:w:0xe2:m -U hfuse:w:0xd9:m -U efuse:w:0xff:m
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
#include <avr/io.h>
|
||||||
|
#include <avr/interrupt.h>
|
||||||
|
#include "millis.h"
|
||||||
|
|
||||||
|
volatile unsigned long _millis; // for millis tick !! Overflow every ~49.7 days
|
||||||
|
|
||||||
|
ISR (TIMER0_COMPA_vect)
|
||||||
|
{
|
||||||
|
_millis++; // INC millis tick
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned long millis(void)
|
||||||
|
{
|
||||||
|
unsigned long i;
|
||||||
|
cli();
|
||||||
|
// Atomic tick reading
|
||||||
|
i = _millis;
|
||||||
|
sei();
|
||||||
|
return i;
|
||||||
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef _MILLIS_H_
|
||||||
|
#define _MILLIS_H_
|
||||||
|
|
||||||
|
#define TICK_PER_SEC 1000UL
|
||||||
|
unsigned long millis(void);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
#include <string.h>
|
||||||
|
#include "mqtt.h"
|
||||||
|
|
||||||
|
uint8_t mqtt_readBuffer[MQTT_BUFFER_SIZE];
|
||||||
|
volatile uint16_t mes_id;
|
||||||
|
|
||||||
|
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
|
||||||
|
.ip = {192, 168, 4, 1}, // IP address
|
||||||
|
.sn = {255, 255, 0, 0}, // Subnet mask
|
||||||
|
.dns = {0,0,0,0}, // DNS address (google dns)
|
||||||
|
.gw = {192, 168, 0, 1}, // Gateway address
|
||||||
|
.dhcp = NETINFO_STATIC}; //Static IP configuration
|
||||||
|
uint8_t MQTT_targetIP[4] = {192, 168, 5, 2};
|
||||||
|
|
||||||
|
//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;
|
||||||
|
|
||||||
|
printf(">>MQTT pub msg nr%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)
|
||||||
|
while(1);
|
||||||
|
//if(mqtt_err_cnt++ > 20)
|
||||||
|
//{
|
||||||
|
// printf("Connection with MQTT Broker was lost!!\r\nReboot the board..\r\n");
|
||||||
|
// while(1);
|
||||||
|
//}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
#ifndef _MQTT_H_
|
||||||
|
#define _MQTT_H_
|
||||||
|
|
||||||
|
#include "Ethernet/socket.h"
|
||||||
|
#include "Internet/MQTT/mqtt_interface.h"
|
||||||
|
#include "Internet/MQTT/MQTTClient.h"
|
||||||
|
|
||||||
|
#define SOCK_MQTT 2
|
||||||
|
#define MQTT_BUFFER_SIZE 512 // 2048
|
||||||
|
|
||||||
|
extern uint8_t mqtt_readBuffer[MQTT_BUFFER_SIZE];
|
||||||
|
extern wiz_NetInfo netInfo;
|
||||||
|
extern uint8_t MQTT_targetIP[4];
|
||||||
|
|
||||||
|
void messageArrived(MessageData* md);
|
||||||
|
void mqtt_pub(Client* mqtt_client, char * mqtt_topic, char * mqtt_msg, int mqtt_msg_len);
|
||||||
|
|
||||||
|
#endif
|
||||||
@ -0,0 +1,53 @@
|
|||||||
|
#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');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -0,0 +1,37 @@
|
|||||||
|
#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
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue