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

@ -28,7 +28,7 @@ FORMAT = ihex
# List C source files here. (C dependencies are automatically generated.)
#SRC = $(TARGET).c
SRC = main.c hardware/Serial.c hardware/AFSK.c hardware/VREF.c util/CRC-CCIT.c protocol/AX25.c protocol/KISS.c
SRC = main.c hardware/Serial.c hardware/AFSK.c hardware/VREF.c hardware/LED.c util/CRC-CCIT.c protocol/AX25.c protocol/KISS.c
# If there is more than one source file, append them above, or modify and
# uncomment the following:

View File

@ -13,6 +13,11 @@
#define CONFIG_ADC_REF 128
#define CONFIG_DAC_REF 255
// TODO: Change this back to default
#define CONFIG_LED_INTENSITY 35
//#define CONFIG_LED_INTENSITY 192
#define COM_LED_TIMEOUT_MS 40
// Demodulator settings
#define OPEN_SQUELCH true
@ -37,12 +42,54 @@
// Port settings
#if TARGET_CPU == m1284p
#define ADC_PORT PORTA
#define ADC_DDR DDRA
#define DAC_PORT PORTB
#define DAC_DDR DDRB
#define LED_PORT PORTC
#define LED_DDR DDRC
#define ADC_PORT PORTA
#define ADC_DDR DDRA
#define DAC_PORT PORTC
#define DAC_DDR DDRC
#define VREF_PORT PORTD
#define VREF_DDR DDRD
#define LED_PORT PORTB
#define LED_DDR DDRB
#endif
#endif
#endif
/*
PA0 ANALOG_IN
PA1
PA2
PA3
PA4
PA5
PA6
PA7
PB0 LED_RX
PB1 LED_TX
PB2 LED_STATUS
PB3 LED_DRAIN_PWM
PB4 LED_COM / SPI_SS PGM
PB5 SPI_MOSI SD/PGM
PB6 SPI_MISO SD/PGM
PB7 SPI_CLK SD/PGM
PC0 DAC_0
PC1 DAC_1
PC2 DAC_2
PC3 DAC_3
PC4 DAC_4
PC5 DAC_5
PC6 DAC_6
PC7 DAC_7
PD0 UART0_RX
PD1 UART0_TX
PD2 UART1_RX GPS
PD3 UART1_TX GPS
PD4
PD5
PD6 REF_DAC
PD7 REF_ADC
*/

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) |

12
main.c
View File

@ -5,6 +5,7 @@
#include "hardware/VREF.h"
#include "hardware/AFSK.h"
#include "hardware/Serial.h"
#include "hardware/LED.h"
#include "protocol/AX25.h"
#include "protocol/KISS.h"
#include "util/time.h"
@ -18,19 +19,25 @@ static void ax25_callback(struct AX25Ctx *ctx) {
kiss_messageCallback(ctx);
}
void system_check(void) {
// TODO: Implement this
LED_STATUS_ON();
}
void init(void) {
sei();
// TODO: serial init was last before
serial_init(&serial);
stdout = &serial.uart0;
stdin = &serial.uart0;
VREF_init();
LED_init();
AFSK_init(&modem);
ax25_init(&AX25, &modem, &modem.fd, ax25_callback);
kiss_init(&AX25, &modem, &serial);
system_check();
}
int main (void) {
@ -40,6 +47,7 @@ int main (void) {
ax25_poll(&AX25);
kiss_poll();
kiss_csma();
led_status();
}
return(0);

View File

@ -3,6 +3,7 @@
#include "device.h"
#include "hardware/Serial.h"
#include "hardware/LED.h"
#include "util/FIFO16.h"
#include "util/time.h"
#include "KISS.h"
@ -23,7 +24,7 @@ size_t packet_lengths_buf[CONFIG_QUEUE_MAX_LENGTH+1];
AX25Ctx *ax25ctx;
Afsk *channel;
Serial *serial;
volatile ticks_t last_serial_read = 0;
volatile last_serial_read = 0;
size_t frame_len;
bool IN_FRAME;
bool ESCAPE;
@ -222,6 +223,17 @@ void kiss_serialCallback(uint8_t sbyte) {
// TODO: Remove this
} else if (command == CMD_FLUSHQUEUE_DEBUG) {
kiss_flushQueueDebug();
} else if (command == CMD_LED_INTENSITY) {
if (sbyte == FESC) {
ESCAPE = true;
} else {
if (ESCAPE) {
if (sbyte == TFEND) sbyte = FEND;
if (sbyte == TFESC) sbyte = FESC;
ESCAPE = false;
}
LED_setIntensity(sbyte);
}
}
}

View File

@ -21,6 +21,7 @@
#define CMD_SETHARDWARE 0x06
#define CMD_FLUSHQUEUE 0x07
#define CMD_FLUSHQUEUE_DEBUG 0x08
#define CMD_LED_INTENSITY 0x09
#define CMD_READY 0x0F
#define CMD_RETURN 0xFF