pyrorf/main.c
2019-10-22 11:56:33 +02:00

140 lines
2 KiB
C

#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/pgmspace.h>
//#include <avr/eeprom.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include "defines.h"
/*#include "spi.h"
#include "rf24.h"
#include "Hamming.h"
#include "tools.h"
*/
#include "status.h"
#include "analog.h"
#include "radio.h"
#include "ui.h"
int main (void);
void setup (void);
void master_loop (void);
uint8_t patch[16];
uint16_t lines[16];
bool TIR_ON=0;
volatile uint8_t skip_sleep;
int main(void)
{
setup();
while (1)
master_loop();
}
void setup(void)
{
// Setup ports
// Port A => Input, no pull-ups
DDRA = 0; PORTA=0;
// Port B => Input, Pull up on INT2(PB2) , SLAVE-SELECT (PB3)
DDRB = 0; PORTB = _BV(PB2)|_BV(PB3);
// Port C => All outputs Low
PORTC=0; DDRC=0xFF;
// Port D => Output : BATT_ENABLE (PD6), pullup on (PD2-4)
PORTD= _BV(PD2)|_BV(PD3)|_BV(PD4);
DDRD= _BV(PD6);
// Timer1 => 1 tick toutes les 5ms
TCCR1A = 0;
TCNT1=0;
OCR1A = 5000; // Compare A => 5000
TIMSK = _BV(OCIE1A); // Interuption sur Compare A
TCCR1B = _BV(WGM12)|_BV(CS11); // Mode CTC (retour a zero sur CompA) et prescaler = 1/8 (1MHz)
sei();
}
enum master_states state = INIT;
void master_loop(void)
{
skip_sleep=0;
analog_run();
radio_run();
ui_run();
// Automate
switch(state){
case INIT:
state=STD;
break;
case STD:
if(RADIO_ASSOC==1)
state= ASSOC;
if(TIR_ON==1)
state= STD_TIR;
break;
case STD_TIR:
if(TIR_ON==0)
state= STD;
break;
case ASSOC:
if (RADIO_ASSOC==0)
state=STD;
if (TIR_ON==1)
state=ASSOC_TIR;
break;
case ASSOC_TIR:
if (RADIO_ASSOC==0)
state=STD;
if (TIR_ON==0)
state= ASSOC;
break;
}
if(!skip_sleep){
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_mode();
}
}
ISR(TIMER1_COMPA_vect){
skip_sleep=1;
if(analog_cpt) analog_cpt--;
if(radio_cpt) radio_cpt--;
if(ui_cpt) ui_cpt--;
}