Add [06_m1284p_WIZNET_DNS_SNTP_client] prj
This commit is contained in:
539
06_m1284p_WIZNET_DNS_SNTP_client/main.c
Normal file
539
06_m1284p_WIZNET_DNS_SNTP_client/main.c
Normal file
@@ -0,0 +1,539 @@
|
||||
/*
|
||||
* main.c
|
||||
*
|
||||
* Created on: 22 <20><><EFBFBD><EFBFBD>. 2018 <20>.
|
||||
* Author: maxx
|
||||
*/
|
||||
#include <avr/io.h>
|
||||
#include <util/delay.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include <compat/deprecated.h> //sbi, cbi etc..
|
||||
#include "avr/wdt.h" // WatchDog
|
||||
#include <stdio.h> // printf etc..
|
||||
#include "uart_extd.h"
|
||||
#include "spi.h"
|
||||
|
||||
#include "Ethernet/socket.h"
|
||||
#include "Ethernet/wizchip_conf.h"
|
||||
#include "Application/loopback/loopback.h"
|
||||
#include "Internet/DNS/dns.h"
|
||||
#include "Internet/SNTP/sntp.h"
|
||||
|
||||
//#include <stdlib.h> // itoa etc..
|
||||
/*
|
||||
* (06)Added SNTP Client example for russian public ntp server: <ntp.mobatime.ru>
|
||||
* (05)Added DNS Lookup example <www.google.com> on start-up device
|
||||
* (03)Trying WIZNET5500 init with using official Wiznet ioLibrary_Driver
|
||||
* working ping, assign static IP
|
||||
* LED1 = ON when phy_link detected
|
||||
* and loopback test on TCP-IP:5000 and UDP:3000 ports.
|
||||
* use Hercules terminal utility to check network connection see:
|
||||
*
|
||||
* https://wizwiki.net/wiki/doku.php?id=osh:cookie:loopback_test
|
||||
* https://www.hw-group.com/software/hercules-setup-utility
|
||||
*
|
||||
*/
|
||||
#define _MAIN_DEBUG_
|
||||
|
||||
#define PRINTF_EN 1
|
||||
#if PRINTF_EN
|
||||
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
|
||||
#else
|
||||
#define PRINTF(...)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* m1284p minimum template, with one button & one led
|
||||
*/
|
||||
|
||||
//M644P/M1284p Users LEDS:
|
||||
//LED1/PORTC.4- m644p/m1284p maxxir
|
||||
#define led1_conf() DDRC |= (1<<DDC4)
|
||||
#define led1_high() PORTC |= (1<<PORTC4)
|
||||
#define led1_low() PORTC &= ~(1<<PORTC4)
|
||||
#define led1_tgl() PORTC ^= (1<<PORTC4)
|
||||
#define led1_read() (PORTC & (1<<PORTC4))
|
||||
|
||||
#define sw1_conf() {DDRC &= ~(1<<DDC5); PORTC |= (1<<PORTC5);}
|
||||
#define sw1_read() (PINC & (1<<PINC5))
|
||||
|
||||
//*********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 - <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
const char compile_time[] PROGMEM = __TIME__; // hh:mm:ss - <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
const char str_prog_name[] PROGMEM = "\r\nAtMega1284p v1.0 Static IP DNS Client & SNTP Client WIZNET_5500 ETHERNET 27/11/2018\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
|
||||
|
||||
|
||||
//FUNC headers
|
||||
static void avr_init(void);
|
||||
void timer0_init(void);
|
||||
static inline unsigned long millis(void);
|
||||
|
||||
//Wiznet FUNC headers
|
||||
void print_network_information(void);
|
||||
|
||||
// RAM Memory usage test
|
||||
static int freeRam (void)
|
||||
{
|
||||
extern int __heap_start, *__brkval;
|
||||
int v;
|
||||
int _res = (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
|
||||
return _res;
|
||||
}
|
||||
|
||||
|
||||
//******************* MILLIS ENGINE: BEGIN
|
||||
//ISR (TIMER0_COMP_vect )
|
||||
ISR (TIMER0_COMPA_vect)
|
||||
{
|
||||
// Compare match Timer0
|
||||
// Here every 1ms
|
||||
_millis++; // INC millis tick
|
||||
// <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// 500Hz FREQ OUT
|
||||
// LED_TGL;
|
||||
}
|
||||
|
||||
static inline unsigned long millis(void)
|
||||
{
|
||||
unsigned long i;
|
||||
cli();
|
||||
// Atomic tick reading
|
||||
i = _millis;
|
||||
sei();
|
||||
return i;
|
||||
}
|
||||
//******************* MILLIS ENGINE: END
|
||||
|
||||
//***************** UART0: BEGIN
|
||||
// Assign I/O stream to UART
|
||||
/* define CPU frequency in Mhz here if not defined in Makefile */
|
||||
//#ifndef F_CPU
|
||||
//#define F_CPU 16000000UL
|
||||
//#endif
|
||||
|
||||
/* 19200 baud */
|
||||
#define UART_BAUD_RATE 19200
|
||||
//#define UART_BAUD_RATE 38400
|
||||
|
||||
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 <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20>.<2E>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> uart.h - api:
|
||||
|
||||
/*
|
||||
* <20>.<2E>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>
|
||||
c = uart1_getc();
|
||||
if (( c & UART_NO_DATA ) == 0)
|
||||
{
|
||||
uart1_putc( (unsigned char)c );
|
||||
}
|
||||
<20><><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (+ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> UART RX RINGBUFFER),
|
||||
<20> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD> stdin->getchar() <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
|
||||
<20><><EFBFBD><EFBFBD><EFBFBD> UART1_RX, <20>.<2E>. <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
|
||||
*/
|
||||
|
||||
// STDOUT UART0 TX handler
|
||||
static int uart0_putchar(char ch,FILE *stream)
|
||||
{
|
||||
uart_putc(ch);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> UART1 RX (<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>)
|
||||
static void uart0_rx_flash(void)
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><> ring-buffer UART1 RX
|
||||
unsigned int c;
|
||||
do
|
||||
{
|
||||
c = uart_getc();
|
||||
} while (( c & UART_NO_DATA ) == 0); // Check RX1 none-empty
|
||||
|
||||
}
|
||||
//***************** UART0: END
|
||||
|
||||
//***************** 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
|
||||
|
||||
#ifndef ADC_REF
|
||||
// vcc voltage ref default
|
||||
#define ADC_REF (1<<REFS0)
|
||||
#endif
|
||||
|
||||
void adc_init(void)
|
||||
{
|
||||
ADCSRA = 0;
|
||||
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
|
||||
}
|
||||
//***************** ADC: END
|
||||
|
||||
|
||||
//***************** WIZCHIP INIT: BEGIN
|
||||
#define SOCK_TCPS 0
|
||||
#define SOCK_UDPS 1
|
||||
#define PORT_TCPS 5000
|
||||
#define PORT_UDPS 3000
|
||||
|
||||
#define ETH_MAX_BUF_SIZE 2048
|
||||
|
||||
unsigned char ethBuf0[ETH_MAX_BUF_SIZE];
|
||||
unsigned char ethBuf1[ETH_MAX_BUF_SIZE];
|
||||
|
||||
wiz_NetInfo netInfo = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef}, // Mac address
|
||||
.ip = {192, 168, 0, 199}, // IP address
|
||||
.sn = {255, 255, 255, 0}, // Subnet mask
|
||||
.dns = {8,8,8,8}, // DNS address (google dns)
|
||||
.gw = {192, 168, 0, 1}, // Gateway address
|
||||
.dhcp = NETINFO_STATIC}; //Dynamic IP configruation from a DHCP sever
|
||||
|
||||
void cs_sel() {
|
||||
SPI_WIZNET_ENABLE();
|
||||
}
|
||||
|
||||
void cs_desel() {
|
||||
SPI_WIZNET_DISABLE();
|
||||
}
|
||||
|
||||
uint8_t spi_rb(void) {
|
||||
uint8_t rbuf;
|
||||
//HAL_SPI_Receive(&hspi1, &rbuf, 1, HAL_MAX_DELAY);
|
||||
SPI_READ(rbuf);
|
||||
return rbuf;
|
||||
}
|
||||
|
||||
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) {
|
||||
//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) {
|
||||
//HAL_SPI_Transmit_DMA(&hspi1, buf, len);
|
||||
//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) {
|
||||
uint8_t bufSize[] = {2, 2, 2, 2, 2, 2, 2, 2};
|
||||
|
||||
reg_wizchip_cs_cbfunc(cs_sel, cs_desel);
|
||||
reg_wizchip_spi_cbfunc(spi_rb, spi_wb);
|
||||
reg_wizchip_spiburst_cbfunc(spi_rb_burst, spi_wb_burst);
|
||||
|
||||
wizchip_init(bufSize, bufSize);
|
||||
wizchip_setnetinfo(&netInfo);
|
||||
//wizchip_setinterruptmask(IK_SOCK_0);
|
||||
}
|
||||
|
||||
//***************** DNS: BEGIN
|
||||
//////////////////////////////////////////////////
|
||||
// Socket & Port number definition for Examples //
|
||||
//////////////////////////////////////////////////
|
||||
#define SOCK_DNS 6
|
||||
|
||||
unsigned char gDATABUF_DNS[ETH_MAX_BUF_SIZE];
|
||||
|
||||
////////////////
|
||||
// DNS client //
|
||||
////////////////
|
||||
uint8_t DNS_2nd[4] = {192, 168, 0, 1}; // Secondary DNS server IP
|
||||
//uint8_t Domain_name[] = "www.google.com"; // for Example domain name
|
||||
//uint8_t Domain_name[] = "ntp.mobatime.ru"; // Public russian ntp server - NO works via GSM Modem (PING not respond too)
|
||||
//uint8_t Domain_name[] = "time.nist.gov"; // Public international ntp server - NO works via GSM Modem (PING not respond too)
|
||||
uint8_t Domain_name[] = "ntp5.stratum2.ru"; // Public russian ntp server - works good via GSM Modem
|
||||
uint8_t Domain_IP[4] = {0, }; // Translated IP address by DNS Server
|
||||
//***************** DNS: END
|
||||
|
||||
//***************** SNTP: BEGIN
|
||||
#define SOCK_SNTP 7
|
||||
unsigned char gDATABUF_SNTP[MAX_SNTP_BUF_SIZE];
|
||||
const uint8_t SNTP_TIMESONE = 31;//31) UTC+04:00 - Russia SAMARA (look at sntp.c)
|
||||
uint8_t sntp_client_enabled = 0;
|
||||
datetime time;
|
||||
|
||||
//***************** SNTP: END
|
||||
|
||||
//***************** WIZCHIP INIT: END
|
||||
|
||||
int main()
|
||||
{
|
||||
uint8_t prev_sw1 = 1; // VAR for sw1 pressing detect
|
||||
|
||||
// INIT MCU
|
||||
avr_init();
|
||||
spi_init(); //SPI Master, MODE0, 4Mhz(DIV4), CS_PB.3=HIGH - suitable for WIZNET 5x00(1/2/5)
|
||||
|
||||
|
||||
// Print program metrics
|
||||
PRINTF("%S", str_prog_name);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
PRINTF("Compiled at: %S %S\r\n", compile_time, compile_date);// <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
PRINTF(">> MCU is: %S; CLK is: %luHz\r\n", str_mcu, F_CPU);// MCU Name && FREQ
|
||||
PRINTF(">> Free RAM is: %d bytes\r\n", freeRam());
|
||||
|
||||
//Wizchip WIZ5500 Ethernet initialize
|
||||
IO_LIBRARY_Init(); //After that ping must working
|
||||
print_network_information();
|
||||
|
||||
#ifdef _MAIN_DEBUG_
|
||||
printf("\r\n===== DNS Servers =====\r\n");
|
||||
printf("> DNS 1st : %d.%d.%d.%d\r\n", netInfo.dns[0], netInfo.dns[1], netInfo.dns[2], netInfo.dns[3]);
|
||||
printf("> DNS 2nd : %d.%d.%d.%d\r\n", DNS_2nd[0], DNS_2nd[1], DNS_2nd[2], DNS_2nd[3]);
|
||||
printf("=======================================\r\n");
|
||||
printf("> [NTP] Target Domain Name : %s\r\n", Domain_name);
|
||||
#endif
|
||||
|
||||
/* DNS client Initialization */
|
||||
DNS_init(SOCK_DNS, gDATABUF_DNS);
|
||||
|
||||
/* DNS processing */
|
||||
int32_t ret;
|
||||
if ((ret = DNS_run(netInfo.dns, Domain_name, Domain_IP)) > 0) // try to 1st DNS
|
||||
{
|
||||
#ifdef _MAIN_DEBUG_
|
||||
printf("> 1st DNS Respond\r\n");
|
||||
#endif
|
||||
}
|
||||
else if ((ret != -1) && ((ret = DNS_run(DNS_2nd, Domain_name, Domain_IP))>0)) // retry to 2nd DNS
|
||||
{
|
||||
#ifdef _MAIN_DEBUG_
|
||||
printf("> 2nd DNS Respond\r\n");
|
||||
#endif
|
||||
}
|
||||
else if(ret == -1)
|
||||
{
|
||||
#ifdef _MAIN_DEBUG_
|
||||
printf("> MAX_DOMAIN_NAME is too small. Should be redefined it.\r\n");
|
||||
#endif
|
||||
;
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef _MAIN_DEBUG_
|
||||
printf("> DNS Failed\r\n");
|
||||
#endif
|
||||
;
|
||||
}
|
||||
|
||||
if(ret > 0)
|
||||
{
|
||||
#ifdef _MAIN_DEBUG_
|
||||
printf("> Translated %s to [%d.%d.%d.%d]\r\n\r\n",Domain_name,Domain_IP[0],Domain_IP[1],Domain_IP[2],Domain_IP[3]);
|
||||
#endif
|
||||
//
|
||||
// TODO: To be executed User's code after a successful DNS process
|
||||
// sntp server ip resolved, so init SNTP client
|
||||
SNTP_init(SOCK_SNTP, Domain_IP, SNTP_TIMESONE, gDATABUF_SNTP); // timezone: Korea, Republic of
|
||||
//Query SNTP time
|
||||
do {}
|
||||
while (SNTP_run(&time) != 1);
|
||||
sntp_client_enabled = 1;
|
||||
PRINTF(">SNTP TIME from: %s\r\n", Domain_name);
|
||||
PRINTF(">%02d/%02d/%d, %02d:%02d:%02d\r\n\r\n", time.dd, time.mo, time.yy, time.hh, time.mm, time.ss);
|
||||
|
||||
}
|
||||
|
||||
|
||||
//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();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Loopback Test: TCP Server and UDP */
|
||||
// Test for Ethernet data transfer validation
|
||||
uint32_t timer_link_1sec = millis();
|
||||
uint32_t timer_sntpc_60sec = 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);
|
||||
|
||||
//loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport);
|
||||
//if(loopback_ret < 0) printf("loopback ret: %ld\r\n", loopback_ret); // TCP Socket Error code
|
||||
|
||||
//SNTP every 1min query
|
||||
if(sntp_client_enabled)
|
||||
{
|
||||
if((millis()-timer_sntpc_60sec)> 60000)
|
||||
{
|
||||
//here every 60 sec
|
||||
timer_sntpc_60sec = millis();
|
||||
//TODO: this is ugly method (because blocking query), better rewrite with FSM method/protothreads (or use RTOS delay)
|
||||
do {}
|
||||
while (SNTP_run(&time) != 1);
|
||||
sntp_client_enabled = 1;
|
||||
PRINTF(">SNTP TIME from: %s\r\n", Domain_name);
|
||||
PRINTF(">%02d/%02d/%d, %02d:%02d:%02d\r\n\r\n", time.dd, time.mo, time.yy, time.hh, time.mm, time.ss);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//PHY Link detect
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Timer0
|
||||
// 1ms IRQ
|
||||
// Used for millis() timing
|
||||
void timer0_init(void)
|
||||
{
|
||||
/*
|
||||
*
|
||||
* For M128
|
||||
TCCR0 = (1<<CS02)|(1<<WGM01); //TIMER0 SET-UP: CTC MODE & PS 1:64
|
||||
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)
|
||||
{
|
||||
// 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
|
||||
|
||||
// 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
|
||||
|
||||
|
||||
led1_conf();
|
||||
led1_low();// LED1 is OFF
|
||||
|
||||
|
||||
sw1_conf();//SW1 internal pull-up
|
||||
|
||||
sei(); //re-enable global interrupts
|
||||
|
||||
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]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user