USB WatchDog ARU-P2 (TOIC) Наш самый функциональный сторожевой таймер: встроенный язык программирования TOIC позволяет модифицировать логику под самые разные задачи. |
//Main settings
#define TIMER_MAX 5500
#define TIMER_DELTA 50
#define RESET_TIME 200
#define POWER_OFF_T1 5000
#define POWER_OFF_DELAY 3000
#define POWER_OFF_T2 200
//hw defines
#define LED_PIN PF0
#define RESET_PIN PA3
#define POWER_PIN PA2
#define FW_STR "4.1U 01 Dec 2020"
#define CFG_STR "11111120300"
//global
var led_on();
var counter;
var phase = 0;
var t_answ = ["~GEEE\n", "~G%d\n", "~G0%d\n", "~G00%d\n", "~G000%d\n"];
//functions
var led_off(){
LED_PIN.VALUE = 0;
}
var reset() {
RESET_PIN.VALUE = 1;
sleep(RESET_TIME);
}
var reboot() {
led_off();
POWER_PIN.VALUE = 1;
sleep(POWER_OFF_T1);
POWER_PIN.VALUE = 0;
sleep(POWER_OFF_DELAY);
POWER_PIN.VALUE = 1;
sleep(POWER_OFF_T2);
}
var reinit() {
RESET_PIN.VALUE = POWER_PIN.VALUE = 0;
counter = TIMER_MAX;
timer(TIMER_MAX, led_on);
}
var get_temp()
{
DS18.INIT = DS18_PORT_B|8;
if(DS18.INIT == 1)
{
DS18.VALUE = 1;
delay(150);
return DS18.VALUE-2730; //K -> C
}
return 0;
}
var temp_sender(var temp) {
var idx = 4;
if (temp>1280) { idx = 0; }
else if (temp>1000) { idx = 1; }
else if (temp>100) { idx = 2; }
else if (temp>10) { idx = 3; }
sprintf(&UART0.TX, t_answ[idx], temp);
}
var led_on() {
LED_PIN.VALUE = 1;
if (counter) {
__disable_irq();
counter -= TIMER_DELTA;
__enable_irq();
timer(TIMER_DELTA-1, led_off);
timer(counter, led_on);
}
else { //time is up
if (phase) {
reboot();
phase = 0;
}
else {
reset();
phase = 1;
}
reinit();
}
}
var io_setup() {
LED_PIN.MODE = GPIO_MODE_OUTPUT|GPIO_OTYPE_PP|GPIO_INIT_LOW;
RESET_PIN.MODE = GPIO_MODE_OUTPUT|GPIO_OTYPE_PP|GPIO_INIT_LOW;
POWER_PIN.MODE = GPIO_MODE_OUTPUT|GPIO_OTYPE_PP|GPIO_INIT_LOW;
UART0.CFG = UART_MODE_PLAIN | UART_CONFIG_START;
__enable_irq();
}
var _msg() {
if(MSG.PORT == 4) {
memcpy(&RING.BUF, &MSG.RX, MSG.SIZE);
}
}
var main() {
var c;
var pstate = 0;
io_setup();
if((RING.ALLOC = 16) != 16) {
return 1;
}
SYS.WDG = 1;
reinit();
while(1){
if(RING.QUEUE > 0) {
c = RING.PULL;
if (c == '~') {pstate = 1;}
else {
if (pstate == 1) {
pstate = 0;
switch (c) {
case 'U':
tstop(led_on);
counter = TIMER_MAX;
LED_PIN.VALUE = 1;
delay(100);
LED_PIN.VALUE = 0;
delay(100);
LED_PIN.VALUE = 1;
delay(100);
LED_PIN.VALUE = 0;
timer(counter, led_on);
sprintf(&UART0.TX, "~A\n");
break;
case 'G':
temp_sender(get_temp());
break;
case 'I':
sprintf(&UART0.TX, "~I%s\n", FW_STR);
break;
case 'W':
case 'F':
sprintf(&UART0.TX, "~F%s\n", CFG_STR);
break;
case 'T':
pstate = 2;
break;
case '1':
pstate = 3;
break;
case '2':
pstate = 4;
break;
case 'P':
pstate = 5;
break;
case 'L':
pstate = 6;
break;
default:
break;
}
}else if (pstate>1)
{
switch (pstate) {
case 2: //~T
if (c == '1') {
reset();
reinit();
} else if (c == '2') {
reboot();
reinit();
}
break;
case 3: //~1
RESET_PIN.VALUE = (c == 'S');
break;
case 4: //~2
POWER_PIN.VALUE = (c == 'S');
break;
case 5: //~P
if (c == '1') {
tstop(led_on);
} else if (c == '0') {
reinit();
}
break;
case 6: //~L
if (c == '1') {
LED_PIN.MODE = GPIO_MODE_OUTPUT|GPIO_OTYPE_PP|GPIO_INIT_LOW;
} else if (c == '0') {
LED_PIN.MODE = GPIO_MODE_INPUT;
}
break;
default:
break;
}
pstate = 0;
}
}
}else delay(10);
SYS.WDG = 42;
}
return 0;
}