Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • leger/avrcte
  • leger/avrcwa
2 results
Show changes
Commits on Source (28)
...@@ -5,3 +5,5 @@ __pycache__/ ...@@ -5,3 +5,5 @@ __pycache__/
*.hex *.hex
*.elf *.elf
commitid.h commitid.h
id.h
ID
DEVICE = atxmega64a3u DEVICE = atxmega256a3u
PROGRAMMER = -c avrispmkII PROGRAMMER = -c avrispmkII
OBJECTS = main.o clock.o debug.o bus.o OBJECTS = main.o clock.o debug.o bus.o config.o status.o utils.o messages.o vanne.o debit.o orders.o thermometer.o median_filter.o leds.o main_timer.o
PORT = usb PORT = usb
# For computing fuse byte values for other devices and options see # For computing fuse byte values for other devices and options see
...@@ -16,20 +16,22 @@ COMPILE = avr-gcc -Wall -Os -mmcu=$(DEVICE) ...@@ -16,20 +16,22 @@ COMPILE = avr-gcc -Wall -Os -mmcu=$(DEVICE)
.PHONY: clean all .PHONY: clean all
.INTERMEDIATE: commitid.h main.o .INTERMEDIATE: commitid.h id.h message.o main.o
# symbolic targets: # symbolic targets:
all: main.hex all: main.hex
clean: clean:
rm -f *.o *.elf *.hex commitid.h rm -f *.o *.elf *.hex commitid.h id.h
commitid.h: .git/refs/heads commitid.h: .git/refs/heads
cog -r *.c *.h
./define_commit_id.py > $@ ./define_commit_id.py > $@
%.o: %.c common.h commitid.h id.h: ID
cog -r *.h ./define_id.py > $@
cog -r $<
%.o: %.c common.h commitid.h id.h
$(COMPILE) -c $< -o $@ $(COMPILE) -c $< -o $@
.S.o: .S.o:
......
Subproject commit 9b92a602eb7145ca70167555cd3cb7d36b742ead Subproject commit 1bbb9871f4a1f20de73f9e90b63757595e530c4a
...@@ -13,6 +13,8 @@ cog.out( ...@@ -13,6 +13,8 @@ cog.out(
prefix="bus", prefix="bus",
**conf.BUS, **conf.BUS,
header=False, header=False,
rxnotify="led_alert.rx = 2;",
txnotify="led_alert.tx = 2;",
) )
) )
]]]*/ ]]]*/
...@@ -69,6 +71,7 @@ void bus_init() ...@@ -69,6 +71,7 @@ void bus_init()
ISR(BUS_RXC_vect) ISR(BUS_RXC_vect)
{ {
uint8_t received = BUS_USART.DATA; uint8_t received = BUS_USART.DATA;
led_alert.rx = 2;
if(received==BUS_rxtrigger) if(received==BUS_rxtrigger)
{ {
bus_rxbuffer[bus_rxidx]=0; bus_rxbuffer[bus_rxidx]=0;
...@@ -77,6 +80,8 @@ ISR(BUS_RXC_vect) ...@@ -77,6 +80,8 @@ ISR(BUS_RXC_vect)
} }
else else
{ {
if(received == 'S' || received == 'M')
bus_rxidx=0;
bus_rxbuffer[bus_rxidx] = received; bus_rxbuffer[bus_rxidx] = received;
bus_rxidx++; bus_rxidx++;
if(bus_rxidx>=BUS_rxlen) if(bus_rxidx>=BUS_rxlen)
...@@ -86,6 +91,7 @@ ISR(BUS_RXC_vect) ...@@ -86,6 +91,7 @@ ISR(BUS_RXC_vect)
ISR(BUS_DRE_vect) ISR(BUS_DRE_vect)
{ {
led_alert.tx = 2;
BUS_deport.OUTSET = BUS_depin; BUS_deport.OUTSET = BUS_depin;
if(bus_txidxA == bus_txidxB) if(bus_txidxA == bus_txidxB)
{ {
...@@ -180,4 +186,6 @@ void bus_printf(const char *fmt, ...) ...@@ -180,4 +186,6 @@ void bus_printf(const char *fmt, ...)
///[[[end]]] ///[[[end]]]
void bus_msg_handler(const char * msg) void bus_msg_handler(const char * msg)
{} {
message_get(msg);
}
...@@ -21,9 +21,9 @@ cog.out( ...@@ -21,9 +21,9 @@ cog.out(
#define BUS_port PORTC #define BUS_port PORTC
#define BUS_txpin PIN3_bm #define BUS_txpin PIN3_bm
#define BUS_rxlen 256 #define BUS_rxlen 384
#define BUS_txlen 256 #define BUS_txlen 384
#define BUS_baudrate 9600 #define BUS_baudrate 1200
#define BUS_chsize 8 #define BUS_chsize 8
#define BUS_rxtrigger '\n' #define BUS_rxtrigger '\n'
#define BUS_rxen true #define BUS_rxen true
......
...@@ -8,9 +8,21 @@ ...@@ -8,9 +8,21 @@
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <string.h> #include <string.h>
#include "utils.h"
#include "commitid.h" #include "commitid.h"
#include "id.h"
#include "clock.h" #include "clock.h"
#include "debug.h" #include "debug.h"
#include "bus.h" #include "bus.h"
#include "config.h"
#include "status.h"
#include "messages.h"
#include "vanne.h"
#include "orders.h"
#include "debit.h"
#include "thermometer.h"
#include "median_filter.h"
#include "leds.h"
#include "main_timer.h"
#endif #endif
...@@ -6,15 +6,14 @@ from avrxmegastuff import PinDesc ...@@ -6,15 +6,14 @@ from avrxmegastuff import PinDesc
VANNE = { VANNE = {
"cmd_open_close_pindesc": (PinDesc("E0"), PinDesc("E1")), "cmd_open_close_pindesc": (PinDesc("E0"), PinDesc("E1")),
"sensor_open_close_pindesc": (PinDesc("E3"), PinDesc("E4")), "sensor_open_close_pindesc": (PinDesc("E3"), PinDesc("E4")),
"action_limit": "config.calc_action_limit", "action_limit": "cconfig.minimum_step",
} }
FLOW = PinDesc("E2") FLOW = PinDesc("E2")
FLOW_EVSYS_CHANNEL = 1 FLOW_EVSYS_CHANNEL = 1
FLOW_COUNTER = "TCD0" FLOW_COUNTER = "TCD0"
FLOW_FACTOR_FILTRE_5 = 1-math.exp(-1/5) FLOW_FACTOR_FILTRE_5 = 1 - math.exp(-1 / 5)
FLOW_FACTOR_FILTRE_60 = 1-math.exp(-1/60) FLOW_FACTOR_FILTRE_60 = 1 - math.exp(-1 / 60)
FLOW_RATTRAPAGE = 15
SENSOR_T = PinDesc("E5") SENSOR_T = PinDesc("E5")
...@@ -30,15 +29,24 @@ LED = { ...@@ -30,15 +29,24 @@ LED = {
DEBUG = {"name": "C1", "txpin": 7, "tx": True} DEBUG = {"name": "C1", "txpin": 7, "tx": True}
BUS = {"name": "C0", "txpin": 3, "tx": True, BUS = {
"rx": True, "de_pindesc": PinDesc("C0"), "name": "C0",
"rxhandler": "bus_msg_handler", "txpin": 3,
"txlen": 256, "rxlen": 256, "tx": True,
"bandrate": 1200} "rx": True,
"de_pindesc": PinDesc("C0"),
"rxhandler": "bus_msg_handler",
"txlen": 384,
"rxlen": 384,
"baudrate": 1200,
"rxtriggerbegin": ("S", "M"),
}
THERMO_PERIOD = 5 THERMO_PERIOD = 5
THERMO_MEDIAN_FILTER_LEN = 13 THERMO_MEDIAN_FILTER_LEN = 13
THERMO_FACTOR_FILTRE = 1-math.exp(-5/60) THERMO_FACTOR_FILTRE = 1 - math.exp(-5 / 60)
THERMO_FAIL_NUMBER = 180
BID_WATER = 0xc0
ACTION_TIMER_MAIN = "TCC0" ACTION_TIMER_MAIN = "TCC0"
......
#include "config.h"
config_t config;
cconfig_t cconfig;
void config_write()
{
uint16_t k;
for(k=0;k<sizeof(config_t);k++)
eeprom_write_byte(EEPROM_ADDR_CONFIG+k, *(((uint8_t*)&config)+k));
}
void config_read()
{
uint16_t k;
for(k=0;k<sizeof(config_t);k++)
*(((uint8_t*)&config)+k) = eeprom_read_byte(EEPROM_ADDR_CONFIG+k);
}
void config_init()
{
config_read();
cconfig_update(&config, &cconfig);
if(!config_valid(&cconfig))
{
config_default(&config);
cconfig_update(&config, &cconfig);
}
config_mode_change();
}
void cconfig_update(config_t * c, cconfig_t * cc)
{
cc->mode = c->mode;
cc->Kp = (0.01 * c->Kp * c->vol_cuve) / 300;
cc->ti = c->ti;
cc->consigne_temperature = 0.01 * c->consigne_temperature;
cc->debit_max = (0.01 * c->debit_max * c->vol_cuve) / 300;
cc->cst_ev = c->cst_ev;
cc->debit_min1 = 0.01 * c->debit_min1;
cc->debit_min2 = 0.01 * c->debit_min2;
cc->minimum_step = c->minimum_step;
cc->debit_no_action = cc->minimum_step/cc->cst_ev;
cc->volume_manque_max = 0.01 * c->volume_manque_max;
cc->m_rattrapage = c->m_rattrapage;
cc->consigne_debit = 0.01 * c->consigne_debit;
cc->post_refroi_T1 = 0.01 * c->post_refroi_T1;
cc->post_refroi_T2 = 0.01 * c->post_refroi_T2;
cc->post_refroi_cold_water_T1 = 0.01 * c->post_refroi_cold_water_T1;
cc->post_refroi_cold_water_T2 = 0.01 * c->post_refroi_cold_water_T2;
cc->post_refroi_hot_water_T1 = 0.01 * c->post_refroi_hot_water_T1;
cc->post_refroi_hot_water_T2 = 0.01 * c->post_refroi_hot_water_T2;
cc->post_refroi_dmax = (0.01 * c->post_refroi_dmax * c->vol_cuve)/300;
}
uint8_t config_valid(cconfig_t * cc)
{
if(cc->mode<1 || cc->mode>6)
return 0;
if(cc->Kp<1 || cc->Kp>100)
return 0;
if(cc->ti<60 || cc->ti > 86400)
return 0;
if(cc->consigne_temperature < 10 || cc->consigne_temperature > 40)
return 0;
if(cc->debit_max < 5 || cc->debit_max > 50)
return 0;
if(cc->cst_ev < 1 || cc->cst_ev>100)
return 0;
if(cc->debit_min1 < 1 || cc->debit_min1 > 40)
return 0;
if(cc->debit_min2 < 1 || cc->debit_min2 > 40)
return 0;
if(cc->debit_min1 >= cc->debit_min2)
return 0;
if(cc->debit_min2 >= cc->debit_max)
return 0;
if(cc->minimum_step < 1 || cc->minimum_step > 150)
return 0;
if(cc->volume_manque_max < 1 || cc->volume_manque_max>250)
return 0;
if(cc->m_rattrapage < 1 || cc->m_rattrapage > 60)
return 0;
if(cc->consigne_debit<0 || cc->consigne_debit>50)
return 0;
if(cc->post_refroi_T1 < 7 || cc->post_refroi_T2 > 40 || cc->post_refroi_T2-cc->post_refroi_T1<1)
return 0;
if(cc->post_refroi_cold_water_T1 < 1 || cc->post_refroi_cold_water_T2 > 40 || cc->post_refroi_cold_water_T2-cc->post_refroi_cold_water_T1<1)
return 0;
if(cc->post_refroi_hot_water_T1 < 1 || cc->post_refroi_hot_water_T2 > 40 || cc->post_refroi_hot_water_T2-cc->post_refroi_hot_water_T1<1)
return 0;
if(cc->post_refroi_dmax < 0 || cc->post_refroi_dmax>40)
return 0;
return 1;
}
void config_default(config_t* c)
{
c->mode = (uint8_t)MODE_FORCE_CLOSE;
c->vol_cuve = 300;
c->Kp = 32*100;
c->ti = 3600;
c->consigne_temperature = 2395;
c->debit_max = 1500;
c->cst_ev = 10;
c->debit_min1 = 200;
c->debit_min2 = 400;
c->minimum_step = 10;
c->volume_manque_max = 7500;
c->m_rattrapage = 15;
c->consigne_debit = 0;
c->post_refroi_T1 = 1200;
c->post_refroi_T2 = 1400;
c->post_refroi_cold_water_T1 = 750;
c->post_refroi_cold_water_T2 = 1050;
c->post_refroi_hot_water_T1 = 1050;
c->post_refroi_hot_water_T2 = 1350;
c->post_refroi_dmax = 1000;
}
void config_apply_new_config(config_t * pnew_config)
{
cconfig_t new_cconfig;
cconfig_update(pnew_config, &new_cconfig);
if(config_valid(&new_cconfig))
{
uint8_t mode_change = 0;
if(pnew_config->mode != config.mode)
mode_change = 1;
memcpy(&config, pnew_config, sizeof(config_t));
orders.write_config = 1;
cconfig_update(&config, &cconfig);
if(mode_change)
config_mode_change();
if(config.mode == MODE_DEBIT)
status.consigne_debit = cconfig.consigne_debit;
}
}
void config_mode_change()
{
if(config.mode == MODE_FORCE_CLOSE)
{
orders.close_ev = 1;
status.consigne_debit = 0;
status.volume_manque = 0;
}
else if(config.mode == MODE_FORCE_OPEN)
{
orders.open_ev = 1;
status.consigne_debit = 0;
status.volume_manque = 0;
}
else if(config.mode == MODE_FORCE_VIDANGE)
{
orders.vidange_ev = 1;
status.consigne_debit = 0;
status.volume_manque = 0;
}
else if(config.mode == MODE_DEBIT)
{
orders.close_ev = 1;
status.volume_manque = 0;
status.consigne_debit = cconfig.consigne_debit;
}
else if(config.mode == MODE_AUTO)
{
orders.close_ev = 1;
status.volume_manque = 0;
status.accu_integrateur = 0;
}
else if(config.mode == MODE_POST_REFROI)
{
orders.close_ev = 1;
status.volume_manque = 0;
status.consigne_debit = 0;
}
}
...@@ -3,6 +3,16 @@ ...@@ -3,6 +3,16 @@
#ifndef CONFIG_H #ifndef CONFIG_H
#define CONFIG_H 1 #define CONFIG_H 1
/*[[[cog
import avrxmegastuff
import conf
cog.out(f"#define EEPROM_ADDR_CONFIG {conf.EEPROM_ADDR_CONFIG}\n")
]]]*/
#define EEPROM_ADDR_CONFIG ((uint8_t*)0x0000)
//[[[end]]]
#include <avr/eeprom.h>
typedef enum mode_enum typedef enum mode_enum
{ {
MODE_FORCE_CLOSE = 0x01, MODE_FORCE_CLOSE = 0x01,
...@@ -10,6 +20,7 @@ typedef enum mode_enum ...@@ -10,6 +20,7 @@ typedef enum mode_enum
MODE_FORCE_VIDANGE = 0x03, MODE_FORCE_VIDANGE = 0x03,
MODE_DEBIT = 0x04, MODE_DEBIT = 0x04,
MODE_AUTO = 0x05, MODE_AUTO = 0x05,
MODE_POST_REFROI = 0x06,
} mode_t; } mode_t;
typedef struct config_struct typedef struct config_struct
...@@ -26,12 +37,19 @@ typedef struct config_struct ...@@ -26,12 +37,19 @@ typedef struct config_struct
uint8_t minimum_step; uint8_t minimum_step;
uint16_t volume_manque_max; uint16_t volume_manque_max;
uint8_t m_rattrapage; uint8_t m_rattrapage;
uint16_t consigne_debit;
int16_t post_refroi_T1;
int16_t post_refroi_T2;
int16_t post_refroi_cold_water_T1;
int16_t post_refroi_cold_water_T2;
int16_t post_refroi_hot_water_T1;
int16_t post_refroi_hot_water_T2;
uint16_t post_refroi_dmax;
} config_t; } config_t;
typedef struct config_calc_struct typedef struct cconfig_struct
{ {
mode_t mode; mode_t mode;
float vol_cuve;
float Kp; float Kp;
float ti; float ti;
float consigne_temperature; float consigne_temperature;
...@@ -43,13 +61,26 @@ typedef struct config_calc_struct ...@@ -43,13 +61,26 @@ typedef struct config_calc_struct
float debit_no_action; float debit_no_action;
float volume_manque_max; float volume_manque_max;
float m_rattrapage; float m_rattrapage;
} config_calc_t; float consigne_debit;
float post_refroi_T1;
float post_refroi_T2;
float post_refroi_cold_water_T1;
float post_refroi_cold_water_T2;
float post_refroi_hot_water_T1;
float post_refroi_hot_water_T2;
float post_refroi_dmax;
} cconfig_t;
void config_read(); void config_read();
void config_write(); void config_write();
void config_calc_update(); void cconfig_update(config_t *, cconfig_t *);
void config_init();
uint8_t config_valid(cconfig_t *);
void config_default(config_t *);
void config_apply_new_config(config_t *);
void config_mode_change();
extern config_t config; extern config_t config;
extern config_calc_t config_calc; extern cconfig_t cconfig;
#endif #endif
#include "debit.h"
void debit_init()
{
FLOW_PORT.DIRCLR = FLOW_PIN_bm;
FLOW_PINCTRL = PORT_OPC_PULLUP_gc | PORT_ISC_BOTHEDGES_gc;
FLOW_EVSYS_CHANNELMUX = FLOW_EVSYS_CHMUX;
FLOW_EVSYS_CHANNELCTRL = EVSYS_DIGFILT_2SAMPLES_gc;
FLOW_COUNTER.PER=0xffff;
FLOW_COUNTER.CNT = 0;
FLOW_COUNTER.CTRLA = (FLOW_COUNTER.CTRLA & ~TC0_CLKSEL_gm) | FLOW_COUNTER_EVENT;
status.debit5.v1 = 0;
status.debit5.v2 = 0;
status.debit60.v1 = 0;
status.debit60.v2 = 0;
status.volume_manque = 0;
status.consigne_debit = 0;
status.consigne_adj = 0;
}
void debit_update()
{
uint16_t count = FLOW_COUNTER.CNT;
FLOW_COUNTER.CNT = 0;
float vol_sur_seconde = 1.8500e-04*count+(2.9027e-06*count)*count;
float debit_insta = vol_sur_seconde*60;
status.volume_manque += status.consigne_debit/60-vol_sur_seconde;
status.fail &= ~(STATUS_FAIL_UNDERFLOW|STATUS_FAIL_OVERFLOW);
if(status.volume_manque > cconfig.volume_manque_max)
{
status.volume_manque = cconfig.volume_manque_max;
status.fail |= STATUS_FAIL_UNDERFLOW;
}
else if(status.volume_manque < - cconfig.volume_manque_max)
{
status.volume_manque = - cconfig.volume_manque_max;
status.fail |= STATUS_FAIL_OVERFLOW;
}
update_filtre2(&(status.debit5), FLOW_FACTOR_FILTRE_5, debit_insta);
update_filtre2(&(status.debit60), FLOW_FACTOR_FILTRE_60, debit_insta);
status.consigne_adj = status.consigne_debit + status.volume_manque/cconfig.m_rattrapage;
if(status.consigne_adj<0)
status.consigne_adj = 0;
if(vanne_internal_status == EV_CLOSED)
{
if(count==0)
{
status.consecutive_closed_debit = 0;
}
else
{
if(status.consecutive_closed_debit == 5)
{
orders.force_close_ev = 1;
status.consecutive_closed_debit = 0;
}
else
status.consecutive_closed_debit++;
}
}
else
status.consecutive_closed_debit = 0;
}
void debit_vanne_update()
{
if(cconfig.mode == MODE_AUTO || cconfig.mode == MODE_DEBIT || cconfig.mode == MODE_POST_REFROI)
{
if( (status.consigne_adj>cconfig.debit_min2) ||
((status.consigne_adj>cconfig.debit_min1) && (vanne_internal_status != EV_CLOSED)))
{
float erreur = status.consigne_adj - status.debit5.v2;
if(erreur>0)
{
// open step
orders.open_ev_step_value = cconfig.cst_ev * erreur;
orders.open_ev_step = 1;
}
else
{
// close step
orders.close_ev_step_value = cconfig.cst_ev * (-erreur);
orders.close_ev_step = 1;
}
}
else
{
// close ev
orders.close_ev = 1;
}
}
}
#include "common.h"
#ifndef H_DEBIT
#define H_DEBIT 1
/*[[[cog
import conf
import avrxmegastuff
cog.out(f"#define FLOW_PORT {conf.FLOW.port}\n")
cog.out(f"#define FLOW_PIN_bm {conf.FLOW.pin_bm}\n")
cog.out(f"#define FLOW_PINCTRL {conf.FLOW.pinctrl}\n")
cog.out(f"#define FLOW_EVSYS_CHMUX {conf.FLOW.evsys_chmux_gc}\n")
cog.out(f"#define FLOW_EVSYS_CHANNELMUX EVSYS.CH{conf.FLOW_EVSYS_CHANNEL}MUX\n")
cog.out(f"#define FLOW_EVSYS_CHANNELCTRL EVSYS.CH{conf.FLOW_EVSYS_CHANNEL}CTRL\n")
cog.out(f"#define FLOW_COUNTER_EVENT TC_CLKSEL_EVCH{conf.FLOW_EVSYS_CHANNEL}_gc\n")
cog.out(f"#define FLOW_COUNTER {conf.FLOW_COUNTER}\n")
cog.out("\n")
cog.out(f"#define FLOW_FACTOR_FILTRE_5 {conf.FLOW_FACTOR_FILTRE_5:e}\n")
cog.out(f"#define FLOW_FACTOR_FILTRE_60 {conf.FLOW_FACTOR_FILTRE_60:e}\n")
]]] */
#define FLOW_PORT PORTE
#define FLOW_PIN_bm PIN2_bm
#define FLOW_PINCTRL PORTE.PIN2CTRL
#define FLOW_EVSYS_CHMUX EVSYS_CHMUX_PORTE_PIN2_gc
#define FLOW_EVSYS_CHANNELMUX EVSYS.CH1MUX
#define FLOW_EVSYS_CHANNELCTRL EVSYS.CH1CTRL
#define FLOW_COUNTER_EVENT TC_CLKSEL_EVCH1_gc
#define FLOW_COUNTER TCD0
#define FLOW_FACTOR_FILTRE_5 1.812692e-01
#define FLOW_FACTOR_FILTRE_60 1.652855e-02
// [[[end]]]
void debit_init();
void debit_consigne_update(float consigne);
void debit_update();
void debit_vanne_update();
#endif
...@@ -3,22 +3,23 @@ ...@@ -3,22 +3,23 @@
import os import os
import time import time
import subprocess import subprocess
import re
import sys
CMDCOMMIT = ('git', 'describe', '--abbr=5', '--dirty=+') CMDCOMMIT = ('git', 'describe', '--abbr=7', '--dirty', '--long')
def retain_out(x): def retain_out(x):
return subprocess.check_output(x).strip().decode('utf8') return subprocess.check_output(x).strip().decode('utf8')
if False:
commit = retain_out(CMDCOMMIT)
mts = max(os.path.getmtime(x) for x in os.listdir() if x.endswith('.c') or x.endswith('.h') or x.endswith('.py'))
mtime = time.strftime('%Y-%m-%d %H:%M',time.localtime(mts))
print(f'#define COMMITID_LINE0 "{commit}"')
print(f'#define COMMITID_LINE1 "{mtime}"')
if __name__ == '__main__': if __name__ == '__main__':
print(f'#define COMMITID_MAJOR 0') commit = retain_out(CMDCOMMIT)
print(f'#define COMMITID_MINOR 0') a = re.match("v([0-9]+)\.([0-9]+)-([0-9]+)-g([0-9a-f]{7})((?:-dirty)?)$",commit)
print(f'#define COMMITID_NBCOMMITS 0x78') if a:
print(f'#define COMMITID_HASH 0xB00B') major, minor, nbc, commithash, dirty = a.groups()
print(f'#define COMMITID_DIRTY 7') dirtyhash = (int(commithash,16)<<4) + (1 if dirty else 0)
print(f'#define COMMITID_MAJOR {major}')
print(f'#define COMMITID_MINOR {minor}')
print(f'#define COMMITID_NBCOMMITS 0x{int(nbc):02x}')
print(f'#define COMMITID_DIRTYHASH 0x{dirtyhash:08x}')
else:
sys.exit(1)
#!/usr/bin/python3
import re
with open("ID") as f:
content = f.read().strip()
a = re.match("^0x([0-9a-fA-F]{2})$", content)
if not a:
raise SyntaxError
(hbid,) = a.groups()
bid = int(hbid, 16)
hbid = f"{bid:02x}"
print(f"#define BID 0x{hbid}")
print(f"#define BID_C0 '{hbid[0]}'")
print(f"#define BID_C1 '{hbid[1]}'")
#include "leds.h"
/*[[[cog
import conf
import avrxmegastuff
]]] */
// [[[end]]]
/*[[[cog
cog.out("""
void led_init()
{
""")
for led_name in conf.LED.keys():
cog.out(f" LED_{led_name}_PORT.DIRSET = LED_{led_name}_PIN_bm;\n");
cog.out("""
}
""")
]]] */
void led_init()
{
LED_STATUS_PORT.DIRSET = LED_STATUS_PIN_bm;
LED_TX_PORT.DIRSET = LED_TX_PIN_bm;
LED_RX_PORT.DIRSET = LED_RX_PIN_bm;
LED_MOTOR_1_PORT.DIRSET = LED_MOTOR_1_PIN_bm;
LED_MOTOR_2_PORT.DIRSET = LED_MOTOR_2_PIN_bm;
LED_OVERFLOW_PORT.DIRSET = LED_OVERFLOW_PIN_bm;
LED_UNDERFLOW_PORT.DIRSET = LED_UNDERFLOW_PIN_bm;
}
// [[[end]]]
/*[[[cog
for act, reg in (("on", "OUTSET"), ("off", "OUTCLR"), ("toogle", "OUTTGL")):
cog.out(f"void led_{act}(led_t led)\n")
cog.out("{\n")
cog.out(" ")
for led_name in conf.LED.keys():
cog.out(f"if(led == LED_{led_name})\n");
cog.out(f" LED_{led_name}_PORT.{reg} = LED_{led_name}_PIN_bm;\n");
cog.out(f" else ");
cog.out("{}\n");
cog.out("}\n\n")
]]] */
void led_on(led_t led)
{
if(led == LED_STATUS)
LED_STATUS_PORT.OUTSET = LED_STATUS_PIN_bm;
else if(led == LED_TX)
LED_TX_PORT.OUTSET = LED_TX_PIN_bm;
else if(led == LED_RX)
LED_RX_PORT.OUTSET = LED_RX_PIN_bm;
else if(led == LED_MOTOR_1)
LED_MOTOR_1_PORT.OUTSET = LED_MOTOR_1_PIN_bm;
else if(led == LED_MOTOR_2)
LED_MOTOR_2_PORT.OUTSET = LED_MOTOR_2_PIN_bm;
else if(led == LED_OVERFLOW)
LED_OVERFLOW_PORT.OUTSET = LED_OVERFLOW_PIN_bm;
else if(led == LED_UNDERFLOW)
LED_UNDERFLOW_PORT.OUTSET = LED_UNDERFLOW_PIN_bm;
else {}
}
void led_off(led_t led)
{
if(led == LED_STATUS)
LED_STATUS_PORT.OUTCLR = LED_STATUS_PIN_bm;
else if(led == LED_TX)
LED_TX_PORT.OUTCLR = LED_TX_PIN_bm;
else if(led == LED_RX)
LED_RX_PORT.OUTCLR = LED_RX_PIN_bm;
else if(led == LED_MOTOR_1)
LED_MOTOR_1_PORT.OUTCLR = LED_MOTOR_1_PIN_bm;
else if(led == LED_MOTOR_2)
LED_MOTOR_2_PORT.OUTCLR = LED_MOTOR_2_PIN_bm;
else if(led == LED_OVERFLOW)
LED_OVERFLOW_PORT.OUTCLR = LED_OVERFLOW_PIN_bm;
else if(led == LED_UNDERFLOW)
LED_UNDERFLOW_PORT.OUTCLR = LED_UNDERFLOW_PIN_bm;
else {}
}
void led_toogle(led_t led)
{
if(led == LED_STATUS)
LED_STATUS_PORT.OUTTGL = LED_STATUS_PIN_bm;
else if(led == LED_TX)
LED_TX_PORT.OUTTGL = LED_TX_PIN_bm;
else if(led == LED_RX)
LED_RX_PORT.OUTTGL = LED_RX_PIN_bm;
else if(led == LED_MOTOR_1)
LED_MOTOR_1_PORT.OUTTGL = LED_MOTOR_1_PIN_bm;
else if(led == LED_MOTOR_2)
LED_MOTOR_2_PORT.OUTTGL = LED_MOTOR_2_PIN_bm;
else if(led == LED_OVERFLOW)
LED_OVERFLOW_PORT.OUTTGL = LED_OVERFLOW_PIN_bm;
else if(led == LED_UNDERFLOW)
LED_UNDERFLOW_PORT.OUTTGL = LED_UNDERFLOW_PIN_bm;
else {}
}
//[[[end]]]
volatile led_alert_t led_alert;
void led_update()
{
if(status.volume_manque > cconfig.volume_manque_max - cconfig.debit_no_action)
led_toogle(LED_UNDERFLOW);
else
{
if(status.debit5.v2 < status.consigne_adj - cconfig.debit_no_action)
led_on(LED_UNDERFLOW);
else
led_off(LED_UNDERFLOW);
}
if(status.volume_manque < -cconfig.volume_manque_max + cconfig.debit_no_action)
led_toogle(LED_OVERFLOW);
else
{
if(status.debit5.v2 > status.consigne_adj + cconfig.debit_no_action)
led_on(LED_OVERFLOW);
else
led_off(LED_OVERFLOW);
}
if(led_alert.open)
{
led_toogle(LED_MOTOR_2);
led_alert.open--;
}
else if(vanne_internal_status == EV_OPENED)
led_on(LED_MOTOR_2);
else
led_off(LED_MOTOR_2);
if(led_alert.close)
{
led_toogle(LED_MOTOR_1);
led_alert.close--;
}
else if(vanne_internal_status == EV_CLOSED)
led_on(LED_MOTOR_1);
else
led_off(LED_MOTOR_1);
if(led_alert.tx)
{
led_toogle(LED_TX);
led_alert.tx--;
}
else
led_off(LED_TX);
if(led_alert.rx)
{
led_toogle(LED_RX);
led_alert.rx--;
}
else
led_off(LED_RX);
}
#include "common.h"
#ifndef H_LEDS
#define H_LEDS 1
/*[[[cog
import conf
import avrxmegastuff
for led_name, led_pd in conf.LED.items():
cog.out(f"#define LED_{led_name}_PORT {led_pd.port}\n")
cog.out(f"#define LED_{led_name}_PIN_bm {led_pd.pin_bm}\n")
]]] */
#define LED_STATUS_PORT PORTF
#define LED_STATUS_PIN_bm PIN0_bm
#define LED_TX_PORT PORTF
#define LED_TX_PIN_bm PIN1_bm
#define LED_RX_PORT PORTF
#define LED_RX_PIN_bm PIN2_bm
#define LED_MOTOR_1_PORT PORTF
#define LED_MOTOR_1_PIN_bm PIN3_bm
#define LED_MOTOR_2_PORT PORTF
#define LED_MOTOR_2_PIN_bm PIN4_bm
#define LED_OVERFLOW_PORT PORTF
#define LED_OVERFLOW_PIN_bm PIN5_bm
#define LED_UNDERFLOW_PORT PORTF
#define LED_UNDERFLOW_PIN_bm PIN6_bm
// [[[end]]]
/*[[[cog
cog.out("typedef enum leds_enum\n{\n")
for led_name in conf.LED.keys():
cog.out(f" LED_{led_name},\n")
cog.out("} led_t;\n")
]]] */
typedef enum leds_enum
{
LED_STATUS,
LED_TX,
LED_RX,
LED_MOTOR_1,
LED_MOTOR_2,
LED_OVERFLOW,
LED_UNDERFLOW,
} led_t;
// [[[end]]]
void led_init();
/*[[[cog
for act in ("on", "off", "toogle"):
cog.out(f"void led_{act}(led_t led);\n")
]]] */
void led_on(led_t led);
void led_off(led_t led);
void led_toogle(led_t led);
// [[[end]]]
typedef struct led_alert_struct
{
uint8_t open;
uint8_t close;
uint8_t tx;
uint8_t rx;
} led_alert_t;
extern volatile led_alert_t led_alert;
void led_update();
#endif
...@@ -7,17 +7,22 @@ int main(void) ...@@ -7,17 +7,22 @@ int main(void)
sei(); sei();
debug_init(); debug_init();
//lcd_init(); status_init();
//vanne_init(); config_init();
//config_init(); vanne_init();
//debit_init(); debit_init();
//status_init(); led_init();
//main_timer_init(); main_timer_init();
//led_init();
uint8_t k;
for(k=0; k<BID; k++)
_delay_ms(2500);
bus_init();
while(1) while(1)
{ {
//do_orders(); do_orders();
_delay_ms(10); _delay_ms(10);
} }
} }
#include "main_timer.h"
volatile uint8_t timer_each10;
volatile uint8_t timer_each50;
volatile uint8_t timer_each100;
void main_timer_init()
{
ACTION_TIMER_MAIN.PER = 3125; // 10 Hz
ACTION_TIMER_MAIN.CTRLA = ( ACTION_TIMER_MAIN.CTRLA & ~TC0_CLKSEL_gm ) | TC_CLKSEL_DIV1024_gc;
ACTION_TIMER_MAIN.INTCTRLA = ( ACTION_TIMER_MAIN.INTCTRLA & ~TC0_OVFINTLVL_gm ) | TC_OVFINTLVL_MED_gc;
timer_each10 = 1;
timer_each50 = 23;
timer_each100 = 48;
}
ISR(ACTION_TIMER_MAIN_OVF_vect)
{
timer_each10++;
if(timer_each10 == 10)
{
timer_each10=0;
debit_update();
led_toogle(LED_STATUS);
}
timer_each50++;
if(timer_each50 == 50)
{
timer_each50=0;
orders.get_T = 1;
}
timer_each100++;
if(timer_each100 == 100)
{
timer_each100=0;
debit_vanne_update();
}
led_update();
};
#include "common.h"
#ifndef H_ACTION
#define H_ACTION 1
/*[[[cog
import conf
import avrxmegastuff
cog.out(f"#define ACTION_TIMER_MAIN {conf.ACTION_TIMER_MAIN}\n")
cog.out(f"#define ACTION_TIMER_MAIN_OVF_vect {conf.ACTION_TIMER_MAIN}_OVF_vect\n")
]]] */
#define ACTION_TIMER_MAIN TCC0
#define ACTION_TIMER_MAIN_OVF_vect TCC0_OVF_vect
// [[[end]]]
void main_timer_init();
#endif
#include "common.h"
/*[[[cog
import conf
import avrxmegastuff as ams
]]]*/
//[[[end]]]
/*[[[cog
cog.out(
ams.median_filter(
prefix="median_filter_thermometer",
filter_len=conf.THERMO_MEDIAN_FILTER_LEN,
data_type="int16_t",
idx_type="uint8_t",
header=False,
)
)
]]]*/
volatile int16_t median_filter_thermometer_circbuf[MEDIAN_FILTER_THERMOMETER_len];
volatile uint8_t median_filter_thermometer_circidx;
volatile uint8_t median_filter_thermometer_filled;
void median_filter_thermometer_fill(int16_t value)
{
for(median_filter_thermometer_circidx = 0; median_filter_thermometer_circidx<MEDIAN_FILTER_THERMOMETER_len; median_filter_thermometer_circidx++)
median_filter_thermometer_circbuf[median_filter_thermometer_circidx] = value;
median_filter_thermometer_circidx=0;
median_filter_thermometer_filled = 1;
}
void median_filter_thermometer_update(int16_t value)
{
median_filter_thermometer_circbuf[median_filter_thermometer_circidx] = value;
median_filter_thermometer_circidx++;
median_filter_thermometer_circidx%=MEDIAN_FILTER_THERMOMETER_len;
}
int16_t median_filter_thermometer_read()
{
int16_t temp[MEDIAN_FILTER_THERMOMETER_len];
uint8_t i;
for(i=0; i<MEDIAN_FILTER_THERMOMETER_len; i++)
temp[i] = median_filter_thermometer_circbuf[i];
uint8_t j;
for(i=0; i<=MEDIAN_FILTER_THERMOMETER_len/2; i++)
{
for(j=i+1; j<MEDIAN_FILTER_THERMOMETER_len; j++)
{
if(temp[i]>temp[j])
{
int16_t swap = temp[i];
temp[i] = temp[j];
temp[j] = swap;
}
}
}
return temp[MEDIAN_FILTER_THERMOMETER_len/2];
}
//[[[end]]]
#include "common.h"
/*[[[cog
import conf
import avrxmegastuff as ams
]]]*/
//[[[end]]]
/*[[[cog
cog.out(
ams.median_filter(
prefix="median_filter_thermometer",
filter_len=conf.THERMO_MEDIAN_FILTER_LEN,
data_type="int16_t",
idx_type="uint8_t",
header=True,
)
)
]]]*/
#ifndef H_MF_median_filter_thermometer
#define H_MF_median_filter_thermometer 1
#define MEDIAN_FILTER_THERMOMETER_len 13
void median_filter_thermometer_fill(int16_t);
void median_filter_thermometer_update(int16_t);
int16_t median_filter_thermometer_read();
extern volatile uint8_t median_filter_thermometer_filled;
#endif
//[[[end]]]