MicroAPRS/Modem/hardware.c

54 lines
1.3 KiB
C
Raw Normal View History

2014-04-03 14:21:37 -06:00
#include "hardware.h"
#include <net/afsk.h>
#include <cpu/irq.h>
#include <avr/io.h>
#include <avr/interrupt.h>
2014-04-03 16:14:09 -06:00
static Afsk *context;
2014-04-03 14:21:37 -06:00
2014-04-03 16:14:09 -06:00
void hw_afsk_adcInit(int ch, Afsk *_context)
2014-04-03 14:21:37 -06:00
{
2014-04-03 16:14:09 -06:00
context = _context;
2014-04-03 14:21:37 -06:00
ASSERT(ch <= 5);
2014-04-03 16:14:09 -06:00
// Timer/Counter Control Register 1 (Timer1 settings, for short)
// Set prescaler to clk/8 (2 MHz), CTC, top = ICR1
2014-04-03 14:21:37 -06:00
TCCR1A = 0;
TCCR1B = BV(CS11) | BV(WGM13) | BV(WGM12);
2014-04-03 16:14:09 -06:00
// Configure ICR1 to get 9.6KHz sampling rate
ICR1 = ((CPU_FREQ / 8) / 9600) - 1; // Input capture register
2014-04-03 14:21:37 -06:00
2014-04-03 16:14:09 -06:00
// Set reference to AVCC (5V), select pin
2014-04-03 14:21:37 -06:00
ADMUX = BV(REFS0) | ch;
DDRC &= ~BV(ch);
PORTC &= ~BV(ch);
DIDR0 |= BV(ch);
2014-04-03 16:14:09 -06:00
// Set autotrigger on Timer1 Input capture flag
ADCSRB = BV(ADTS2) | // Setting these three on (1-1-1) sets the ADC to
BV(ADTS1) | // "Timer1 capture event"
BV(ADTS0); //
2014-04-03 14:21:37 -06:00
ADCSRA = BV(ADEN) | // ADC Enable
BV(ADSC) | // ADC Start converting
BV(ADATE) | // Enable autotriggering
BV(ADIE) | // ADC Interrupt enable
BV(ADPS2); // Enable prescaler flag 2 (1-0-0 = division by 16 = 1MHz)
}
2014-04-03 16:14:09 -06:00
// Declare ADC ISR
2014-04-03 14:21:37 -06:00
bool hw_afsk_dac_isr;
2014-04-03 16:14:09 -06:00
DECLARE_ISR(ADC_vect) {
2014-04-03 14:21:37 -06:00
TIFR1 = BV(ICF1);
2014-04-03 16:14:09 -06:00
afsk_adc_isr(context, ((int16_t)((ADC) >> 2) - 128));
2014-04-03 14:21:37 -06:00
if (hw_afsk_dac_isr)
2014-04-03 16:14:09 -06:00
PORTD = afsk_dac_isr(context) & 0xF0;
2014-04-03 14:21:37 -06:00
else
PORTD = 128;
2014-04-03 16:14:09 -06:00
}