Commit eb010c08 authored by Martin Vítek's avatar Martin Vítek

Fixes, add brightness controll, add ARP retransmit

parent 2cb4a1d5
......@@ -84,8 +84,8 @@ class adc_class
//2,93kHz
TC4->COUNT8.CTRLA.reg = TC_CTRLA_MODE_COUNT8 | TC_CTRLA_PRESCALER_DIV256 | TC_CTRLA_PRESCSYNC_PRESC;
// -> ~1.25kHz
TC4->COUNT8.PER.reg = 150;
// -> 2.5kHz
TC4->COUNT8.PER.reg = 75-1;
while (TC4->COUNT8.STATUS.bit.SYNCBUSY) ;;
......
......@@ -40,15 +40,7 @@ class BoostConverter
uint32_t minimum_voltage;
uint32_t maximum_voltage;
uint8_t duty;
//PID
int32_t pozadavek;
int64_t integrator_ureg;
uint32_t U_regu;
const int32_t K = 100;
const int32_t I = 10;
public:
BoostConverter(volatile TcCount8& timer, adc_class& adc): timer(timer),
adc(adc),
......@@ -157,48 +149,7 @@ class BoostConverter
while (timer.STATUS.bit.SYNCBUSY) ;;
}
}
void PID_regulator()
{
pozadavek = (requested_voltage - adc.value[adc.VOLTAGE_180V]);
U_regu = pi_reg(pozadavek, K, I, 10, 10, (245<<6)/*16256*/, 1, &integrator_ureg);
duty = U_regu>>6;
timer.CC[0].reg = duty;
while (timer.STATUS.bit.SYNCBUSY) ;;
}
int pi_reg( // podminka Qi>=Qp
int32_t epsilon,
int32_t Kpf,
int32_t Kif,
uint32_t Qp,
uint32_t Qi,
int32_t MAX,
int32_t MIN,
int64_t *S)
{
int64_t out;
out=((int64_t)Kpf*epsilon+(*S>>(Qi-Qp)))>>Qp;
if (out>MAX) {
out=MAX;
if(epsilon<0) *S+=(int64_t)Kif*epsilon;
}else if (out<MIN){
out=MIN;
if(epsilon>0) *S+=(int64_t)Kif*epsilon;
}else{
*S+=(int64_t)Kif*epsilon;
}
return (int32_t)out;
}
void protections_handler()
{
if (adc.value[adc.VOLTAGE_180V] > MAXIMUM_VOLTAGE_180V)
......@@ -229,7 +180,6 @@ class BoostConverter
if (TC3->COUNT8.INTFLAG.bit.OVF)
{
//regulator_handler();
//PID_regulator();
TC3->COUNT8.INTFLAG.reg = TC_INTFLAG_OVF;
}
......
......@@ -577,6 +577,8 @@ namespace ENC28J60_namespace
void ENC28J60::get_interrupt_source()
{
if (!pending_interrupt) return;
interrupt_flags flags;
flags.value = read_control_register(EIR);
uint8_t flags_to_clear = 0;
......@@ -631,6 +633,8 @@ namespace ENC28J60_namespace
}
if (flags_to_clear) bit_field_clear_in_register(EIR, flags_to_clear);
pending_interrupt = false;
}
/*
......
......@@ -108,6 +108,30 @@ void NixieClock::display_handler()
}
}
void NixieClock::screen_handler()
{
if (clock() >= milis)
{
switch (nixie.state)
{
//Show date for 5s
case NixieClock::DISPLAYING_TIME: nixie.display_date();
milis = clock() + 5'000;
break;
//Show temperature for 10s
case NixieClock::DISPLAYING_DATE: nixie.display_temperature();
milis = clock() + 10'000;
break;
//Show time for 30s
case NixieClock::DISPLAYING_TEMPERATURE: nixie.display_time();
milis = clock() + 30'000;
break;
}
}
}
void NixieClock::sntp_handler()
{
//FIXME: There are 2 instances of SNTP_client, temporary fixed by static variable
......@@ -131,6 +155,9 @@ void NixieClock::buttons_handler()
{
//Add hour
seconds += 60*60;
nixie.display_time();
milis = clock() + 30'000;
}
button_hour.serviced();
......@@ -143,12 +170,26 @@ void NixieClock::buttons_handler()
//Add minute, reset seconds
localtime_r(&old_time, &time_date);
seconds += 60 - time_date.tm_sec;
nixie.display_time();
milis = clock() + 30'000;
}
button_min.serviced();
}
}
void NixieClock::brightness_handler()
{
if (clock() > brightness_delay)
{
//500ms delay
brightness_delay = clock() + 500;
display.set_brightness(adc.value[adc.AMBIENT_LIGHT]);
}
}
void NixieClock::get_mac()
{
eeprom.read_EUI(mac);
......
......@@ -43,7 +43,9 @@ class NixieClock
public:
time_t old_time;
tm time_date;
time_t sntp_last_update;
time_t sntp_last_update;
clock_t milis;
clock_t brightness_delay;
display_states state;
......@@ -57,8 +59,10 @@ class NixieClock
void display_temperature();
void display_handler();
void screen_handler();
void sntp_handler();
void buttons_handler();
void brightness_handler();
void get_mac();
void print_eeprom();
......
......@@ -80,6 +80,16 @@ void NixieDisplay::set(const uint8_t temperature)
buffer[5] = numbers[temperature%10];
}
void NixieDisplay::set_brightness(const uint16_t brightness)
{
//1000lx is maximum -> displaying_time=3,3ms; blanking_time=0,2ms => sum=3,5ms
//0lx is minimum -> displaying_time=0,2ms; blanking_time=3,3ms
displaying_time = brightness/15;
if (displaying_time < 4) displaying_time = 4;
blanking_time = 70 - displaying_time;
}
void NixieDisplay::multiplex_handler()
{
if (TC1->COUNT8.INTFLAG.bit.OVF)
......@@ -88,7 +98,7 @@ void NixieDisplay::multiplex_handler()
if (state == DISPLAING_NUMBER)
{
if (elapsed >= BLANKING_TIME)
if (elapsed >= blanking_time)
{
bcd.set(buffer[digit]);
......@@ -101,7 +111,7 @@ void NixieDisplay::multiplex_handler()
else
{
if (elapsed >= DISPLAYING_TIME)
if (elapsed >= displaying_time)
{
digits[digit].off();
......
......@@ -63,6 +63,9 @@ class NixieDisplay
volatile multiplex_states state;
volatile uint8_t digit;
volatile uint8_t displaying_time;
volatile uint8_t blanking_time;
volatile uint8_t buffer[6];
......@@ -72,5 +75,6 @@ class NixieDisplay
void init();
void set(const uint8_t hod, const uint8_t min, const uint8_t sec);
void set(const uint8_t temperature);
void set_brightness(const uint16_t brightness);
void multiplex_handler();
};
......@@ -263,6 +263,8 @@ void NixieStack::send_sntp_request(const IP ntp_server_ip)
}
}
void NixieStack::sntp_arp_handler()
{
if (pending_sntp_request && new_arp_in_cache)
......@@ -299,12 +301,32 @@ void NixieStack::sntp_arp_handler()
tx_buffer.write_advance();
pending_sntp_request = false;
pending_arp_request = false;
arp_count = 0;
}
new_arp_in_cache = false;
}
}
void NixieStack::arp_timeout_handler()
{
if (clock() > arp_timeout)
{
//Every 750ms
arp_timeout = clock() + 750;
if (pending_arp_request && (arp_count<10) && link_is_up)
{
send_arp_request(pending_ip);
debug.error("ARP timeout");
arp_count++;
}
}
}
void NixieStack::test_tx()
{
ethernet_frame* frame = tx_buffer.write();
......
#pragma once
#include <cstdint>
#include <ctime>
#include <machine/endian.h>
#include "CircularBuffer.h"
......@@ -9,6 +10,7 @@
#include "ARP.h"
#include "IP_protocol.h"
#include "ENC28J60.h"
#include "Time.h"
using namespace ENC28J60_namespace;
......@@ -30,6 +32,10 @@ class NixieStack
bool new_arp_in_cache;
IP pending_ip;
IP pending_sntp_server_ip;
bool pending_arp_request;
clock_t arp_timeout;
uint8_t arp_count;
public:
ARP_protocol arp;
......@@ -61,6 +67,7 @@ class NixieStack
void send_sntp_request(const IP ntp_server_ip);
void sntp_arp_handler();
void arp_timeout_handler();
//For testing
void test_tx();
......
......@@ -106,10 +106,12 @@ int main(void)
while (true)
{
nixie.screen_handler();
nixie.display_handler();
nixie.separator.pulsing_handler();
nixie.sntp_handler();
nixie.buttons_handler();
nixie.brightness_handler();
eth.get_interrupt_source();
stack.check_link();
......@@ -117,6 +119,7 @@ int main(void)
stack.tx_handler();
stack.packet_handler();
stack.sntp_arp_handler();
stack.arp_timeout_handler();
if (adc.done)
{
......@@ -127,23 +130,6 @@ int main(void)
adc.done = false;
}
if (time(NULL) >= old)
{
old = time(NULL) + 10;
switch (nixie.state)
{
case NixieClock::DISPLAYING_TIME: nixie.display_date();
break;
case NixieClock::DISPLAYING_DATE: nixie.display_temperature();
break;
case NixieClock::DISPLAYING_TEMPERATURE: nixie.display_time();
break;
}
}
if ((clock() % 1000) == 0 )
{
//Send ADC values
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment