First functioning version

This commit is contained in:
2022-01-21 12:30:33 +01:00
parent 0584796a67
commit cc9dd6eb19
4 changed files with 190 additions and 274 deletions

40
adc.cpp
View File

@ -1,40 +0,0 @@
#include "adc.h"
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(ADC_vect)
{
uint16_t sample = ADCL;
sample |= ADCH << 8;
if(Adc::sampleCallback_) Adc::sampleCallback_(sample, Adc::userData_);
}
Adc::Adc(void (*sampleCallback)(uint16_t sample, void* userData), void* userData, uint8_t input, uint8_t referance)
{
userData_ = userData;
sampleCallback_ = sampleCallback;
ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0);
setInput(input);
setReferance(referance);
}
Adc::~Adc()
{
ADCSRA = 0;
}
void Adc::setInput(const uint8_t input)
{
ADMUX = (ADMUX & 0b11110000) | (input & 0b00001111);
}
void Adc::setReferance(const uint8_t input)
{
ADMUX = (ADMUX & 0b00101111) | ((input & 0b00000100) << 2) | ((input & 0b00000011) << 6);
}
void Adc::aquire()
{
ADCSRA |= 1 << ADSC;
}

28
adc.h
View File

@ -1,28 +0,0 @@
#pragma once
#include <stdint.h>
class Adc
{
public:
inline static void* userData_ = nullptr;
inline static void (*sampleCallback_)(uint16_t sample, void* userData) = nullptr;
static constexpr uint8_t VREF_VCC = 0b0000;
static constexpr uint8_t VREF_1_1 = 0b0010;
static constexpr uint8_t VREF_2_56 = 0b0110;
static constexpr uint8_t VREF_EXT = 0b0001;
static constexpr uint8_t IN_ADC0 = 0b0000;
static constexpr uint8_t IN_ADC1 = 0b0001;
static constexpr uint8_t IN_ADC2 = 0b0010;
static constexpr uint8_t IN_ADC3 = 0b0011;
static constexpr uint8_t IN_ADC4 = 0b1111;
Adc(void (*sampleCallback)(uint16_t sample, void* userData), void* userData = nullptr, uint8_t input = IN_ADC0, uint8_t referance = VREF_2_56);
~Adc();
void setInput(const uint8_t input);
void setReferance(const uint8_t referance);
void aquire();
};

162
main.cpp
View File

@ -3,122 +3,96 @@
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include "adc.h"
#include "writepin.h"
#include "ringbuffer.h"
extern "C"
#include "decoder.h"
static constexpr uint8_t T0_A = PB0;
static constexpr uint8_t T0_B = PB1;
static constexpr uint8_t T1_A = PB3;
static constexpr uint8_t T1_B = PB4;
static volatile bool turnout0Flag = false;
static volatile bool turnout1Flag = false;
void handler(uint16_t data, void *user_data)
{
#include "usbdrv.h"
(void)user_data;
if(((bool)(data & (1 << 9))) != ((bool)(data & (1 << 8))) ||
((bool)(data & (1 << 7))) != ((bool)(data & (1 << 6))) ||
((bool)(data & (1 << 5))) != ((bool)(data & (1 << 4))))
return;
bool set = data & (1 << 9);
if(data & (1 << 7))
turnout0Flag = set;
if(data & (1 << 5))
turnout1Flag = set;
}
static void setTurnout0(bool in)
{
writePin(&PORTB, T0_A, in);
writePin(&PORTB, T0_B, !in);
_delay_ms(50);
writePin(&PORTB, T0_A, false);
writePin(&PORTB, T0_B, false);
}
volatile uint16_t sampleId = 0;
void adcConversionCallback(uint16_t sample, void* userData);
Adc adc(&adcConversionCallback, nullptr, Adc::IN_ADC1, Adc::VREF_VCC);
RingBuffer<32, uint16_t> buffer;
ISR(TIMER0_OVF_vect)
static void setTurnout1(bool in)
{
adc.aquire();
writePin(&PORTB, T1_A, in);
writePin(&PORTB, T1_B, !in);
_delay_ms(50);
writePin(&PORTB, T1_A, false);
writePin(&PORTB, T1_B, false);
}
Decoder decoder(&PINB, PB2, &TCNT1, &handler);
extern "C" usbMsgLen_t usbFunctionSetup(uchar data[8])
ISR(TIMER1_OVF_vect)
{
usbRequest_t *rq = (usbRequest_t *)data;
static uchar dataBuffer[8];
if(rq->bRequest == 0)
{
if(!buffer.isEmpty() && !buffer.isEmpty() && rq->wLength.word >= 4 )
{
usbMsgLen_t length = buffer.remaining()+2;
if(length > 8) length = 8;
if(length > rq->wLength.word) length = rq->wLength.word;
dataBuffer[0] = sampleId-buffer.remaining() >> 8;
dataBuffer[1] = sampleId-buffer.remaining() & 0x00FF;
for(usbMsgLen_t i = 2; i+1 < length; i+=2)
{
uint16_t sample = buffer.read();
dataBuffer[i] = sample >> 8;
dataBuffer[i+1] = sample & 0x00FF;
}
usbMsgPtr = (short unsigned int)dataBuffer;
return length;
}
}
if(rq->bRequest == 1)
{
if(rq->wValue.word == 0) adc.setInput(Adc::IN_ADC1);
else adc.setInput(Adc::IN_ADC0);
buffer.flush();
return 0;
}
if(rq->bRequest == 2)
{
if(rq->wValue.word == 0) adc.setReferance(Adc::VREF_VCC);
else if(rq->wValue.word == 1) adc.setReferance(Adc::VREF_1_1);
else adc.setReferance(Adc::VREF_2_56);
buffer.flush();
return 0;
}
else if(rq->bRequest == 128)
{
dataBuffer[0] = 55;
dataBuffer[1] = 55;
dataBuffer[2] = 55;
dataBuffer[3] = 55;
usbMsgPtr = (short unsigned int)dataBuffer;
return 4;
}
else if(rq->bRequest == 129)
{
dataBuffer[0] = ADCSRA;
dataBuffer[1] = ADMUX;
usbMsgPtr = (short unsigned int)dataBuffer;
return 2;
}
if(rq->bRequest == 130)
{
constexpr uint16_t freq = 30518;
dataBuffer[0] = freq >> 8;
dataBuffer[1] = freq & 0x00FF;
usbMsgPtr = (short unsigned int)dataBuffer;
return 2;
}
return 0;
decoder.overflowInterrupt();
}
void adcConversionCallback(uint16_t sample, void* userData)
ISR(INT0_vect)
{
buffer.write(sample);
sampleId++;
}
void timer0InterruptEnable(const bool enable)
{
if(enable) TIMSK = TIMSK | (1 << TOIE0);
else TIMSK = TIMSK & ~(1 << TOIE0);
decoder.interrupt();
}
int main()
{
DIDR0 |= (1 << PB2);
TCCR0B = (1<<CS02); // run timer0 with /256 scaler
timer0InterruptEnable(true);
usbInit();
usbDeviceDisconnect(); // enforce re-enumeration, do this while interrupts are disabled!
_delay_ms(250);
DDRB = (1 << PB0) | (1 << PB1) | (1 << PB3) | (1 << PB4);
usbDeviceConnect();
bool turnout0 = false;
bool turnout1 = false;
_delay_ms(100);
setTurnout0(true);
setTurnout1(true);
_delay_ms(500);
setTurnout0(false);
setTurnout1(false);
MCUCR = (1 << ISC00); //enable rising and falling irq on INT0
GIMSK = (1 << INT0); //unmask INT0
TIMSK = (1 << TOIE1); //enable timer 1 overflow irq
TCCR1 = (1 << CS12) | (1 << CS10); //enable timer 1 with /16 timer
sei();
while(true)
{
usbPoll();
if(turnout0 != turnout0Flag)
{
turnout0 = turnout0Flag;
setTurnout0(turnout0);
}
if(turnout1 != turnout1Flag)
{
turnout1 = turnout1Flag;
setTurnout1(turnout1);
}
}
}

View File

@ -1,18 +1,18 @@
/*UVOS*/
/* This file is part of TelemetrySystem.
/* This file is part of UsbLedController.
*
* TelemetrySystem is free software: you can redistribute it and/or modify
* UsbLedController is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License (LGPL) version 3 as published by
* the Free Software Foundation.
*
* TelemetrySystem is distributed in the hope that it will be useful,
* UsbLedController is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with TelemetrySystem. If not, see <http://www.gnu.org/licenses/>.
* along with UsbLedController. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
@ -45,10 +45,14 @@ public:
return BUFFER_SIZE - (_headIndex-_tailIndex);
}
unsigned capacity() const volatile
{
return BUFFER_SIZE;
}
bool isOverun() volatile
{
bool returnVal = _overrun;
_overrun = false;
return returnVal;
}
@ -57,8 +61,14 @@ public:
return _tailIndex >= _headIndex;
}
const T operator[](uint_fast16_t idx) const volatile
{
return _buffer[idx];
}
T read() volatile
{
_overrun = false;
if(!isEmpty())
{
_tailIndex++;
@ -79,7 +89,7 @@ public:
void write( T in ) volatile
{
if (_headIndex - BUFFER_SIZE > 0 && _tailIndex - BUFFER_SIZE > 0)
if ((int)_headIndex - BUFFER_SIZE > 0 && (int)_tailIndex - BUFFER_SIZE > 0)
{
_headIndex -= BUFFER_SIZE;
_tailIndex -= BUFFER_SIZE;