LED driver work

This commit is contained in:
Mark Qvist 2019-01-12 15:12:51 +01:00
parent c22123b953
commit 372264d379
12 changed files with 162 additions and 29 deletions

View file

@ -1,6 +1,7 @@
#include <string.h>
#include "AFSK.h"
#include "util/time.h"
#include "hardware/LED.h"
#include "protocol/KISS.h"
// TODO: Remove testing vars ////
@ -28,10 +29,6 @@ void AFSK_hw_init(void) {
// Run DAC initialisation
AFSK_dac_init();
// Run LED initialisation
LED_TX_INIT();
LED_RX_INIT();
}
void AFSK_dac_init(void) {

View file

@ -224,19 +224,6 @@ typedef struct Afsk
#define AFSK_DAC_IRQ_START() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = true; } while (0)
#define AFSK_DAC_IRQ_STOP() do { extern bool hw_afsk_dac_isr; hw_afsk_dac_isr = false; } while (0)
// Here's some macros for controlling the RX/TX LEDs
// THE _INIT() functions writes to the DDR registers
// to configure the pins as output pins, and the _ON()
// and _OFF() functions writes to the PORT registers
// to turn the pins on or off.
#define LED_TX_INIT() do { LED_DDR |= _BV(1); } while (0)
#define LED_TX_ON() do { LED_PORT |= _BV(1); } while (0)
#define LED_TX_OFF() do { LED_PORT &= ~_BV(1); } while (0)
#define LED_RX_INIT() do { LED_DDR |= _BV(2); } while (0)
#define LED_RX_ON() do { LED_PORT |= _BV(2); } while (0)
#define LED_RX_OFF() do { LED_PORT &= ~_BV(2); } while (0)
void AFSK_init(Afsk *afsk);
void AFSK_adc_init(void);
void AFSK_dac_init(void);

57
hardware/LED.c Normal file
View file

@ -0,0 +1,57 @@
#include "LED.h"
#include "util/time.h"
uint8_t ledIntensity = CONFIG_LED_INTENSITY;
uint32_t com_led_timeout = 0;
void LED_init(void) {
// Enable output for LED pins and drain pin
LED_DDR |= _BV(0) | // RX
_BV(1) | // TX
_BV(2) | // Status
_BV(3) | // PWM drain
_BV(4); // COM
LED_PORT &= 0b11100000;
TCCR0A = _BV(WGM00) |
_BV(WGM01) |
_BV(COM0A1)|
_BV(COM0A0);
TCCR0B = _BV(CS00);
OCR0A = ledIntensity;
}
void LED_setIntensity(uint8_t value) {
ledIntensity = value;
OCR0A = ledIntensity;
}
void LED_COM_ON(void) {
LED_PORT |= _BV(4);
int32_t xa = timer_clock();
com_led_timeout = xa + ms_to_ticks(COM_LED_TIMEOUT_MS);
if (xa > com_led_timeout) {
while(true) {
LED_COM_ON();
LED_RX_ON();
LED_TX_ON();
}
}
}
void LED_COM_OFF(void) {
LED_PORT &= ~_BV(4);
}
void led_status(void) {
if (timer_clock() > com_led_timeout) {
if (LED_PORT & _BV(4)) {
printf("%d\r\n", timer_clock());
LED_COM_OFF();
}
}
}

21
hardware/LED.h Normal file
View file

@ -0,0 +1,21 @@
#ifndef LED_H
#define LED_H
#include <avr/io.h>
#include "device.h"
void LED_init(void);
void LED_setIntensity(uint8_t value);
#define LED_STATUS_ON() do { LED_PORT |= _BV(2); } while (0)
#define LED_STATUS_OFF() do { LED_PORT &= ~_BV(2); } while (0)
#define LED_TX_ON() do { LED_PORT |= _BV(1); } while (0)
#define LED_TX_OFF() do { LED_PORT &= ~_BV(1); } while (0)
#define LED_RX_ON() do { LED_PORT |= _BV(0); } while (0)
#define LED_RX_OFF() do { LED_PORT &= ~_BV(0); } while (0)
void LED_COM_ON(void);
void LED_COM_OFF(void);
void led_status(void);
#endif

View file

@ -36,6 +36,7 @@ bool serial_available(uint8_t index) {
int uart0_putchar(char c, FILE *stream) {
LED_COM_ON();
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 1;
@ -53,6 +54,7 @@ char uart0_getchar_nowait(void) {
ISR(USART0_RX_vect) {
if (serial_available(0)) {
LED_COM_ON();
char c = uart0_getchar_nowait();
fifo_push(&serialFIFO, c);
}

View file

@ -7,6 +7,7 @@
#include <stdbool.h>
#include <avr/io.h>
#include "util/FIFO.h"
#include "hardware/LED.h"
typedef struct Serial {
FILE uart0;

View file

@ -5,7 +5,7 @@ uint8_t dacReference = CONFIG_DAC_REF;
void VREF_init(void) {
// Enable output for OC2A and OC2B (PD7 and PD6)
DDRD |= _BV(7) | _BV(6);
VREF_DDR |= _BV(7) | _BV(6);
TCCR2A = _BV(WGM20) |
_BV(WGM21) |