From 372264d3791bcd78aa09a1bad2385a6513fc39c5 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 12 Jan 2019 15:12:51 +0100 Subject: [PATCH] LED driver work --- Makefile | 2 +- device.h | 61 +++++++++++++++++++++++++++++++++++++++++------ hardware/AFSK.c | 5 +--- hardware/AFSK.h | 13 ---------- hardware/LED.c | 57 +++++++++++++++++++++++++++++++++++++++++++ hardware/LED.h | 21 ++++++++++++++++ hardware/Serial.c | 2 ++ hardware/Serial.h | 1 + hardware/VREF.c | 2 +- main.c | 12 ++++++++-- protocol/KISS.c | 14 ++++++++++- protocol/KISS.h | 1 + 12 files changed, 162 insertions(+), 29 deletions(-) create mode 100644 hardware/LED.c create mode 100644 hardware/LED.h diff --git a/Makefile b/Makefile index e93c4ce..06dc904 100755 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/device.h b/device.h index 7b30c29..23fe5a0 100755 --- a/device.h +++ b/device.h @@ -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 \ No newline at end of file +#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 + +*/ \ No newline at end of file diff --git a/hardware/AFSK.c b/hardware/AFSK.c index 5c8c977..f8686e3 100755 --- a/hardware/AFSK.c +++ b/hardware/AFSK.c @@ -1,6 +1,7 @@ #include #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) { diff --git a/hardware/AFSK.h b/hardware/AFSK.h index 5bb32cf..3012ec7 100755 --- a/hardware/AFSK.h +++ b/hardware/AFSK.h @@ -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); diff --git a/hardware/LED.c b/hardware/LED.c new file mode 100644 index 0000000..7402557 --- /dev/null +++ b/hardware/LED.c @@ -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(); + } + } +} \ No newline at end of file diff --git a/hardware/LED.h b/hardware/LED.h new file mode 100644 index 0000000..55b0ed7 --- /dev/null +++ b/hardware/LED.h @@ -0,0 +1,21 @@ +#ifndef LED_H +#define LED_H + +#include +#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 \ No newline at end of file diff --git a/hardware/Serial.c b/hardware/Serial.c index bfc9d42..9efe7d8 100755 --- a/hardware/Serial.c +++ b/hardware/Serial.c @@ -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); } diff --git a/hardware/Serial.h b/hardware/Serial.h index 70e1201..98da57a 100755 --- a/hardware/Serial.h +++ b/hardware/Serial.h @@ -7,6 +7,7 @@ #include #include #include "util/FIFO.h" +#include "hardware/LED.h" typedef struct Serial { FILE uart0; diff --git a/hardware/VREF.c b/hardware/VREF.c index d463b79..5544e07 100644 --- a/hardware/VREF.c +++ b/hardware/VREF.c @@ -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) | diff --git a/main.c b/main.c index 822ee6a..94c90b0 100755 --- a/main.c +++ b/main.c @@ -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); diff --git a/protocol/KISS.c b/protocol/KISS.c index d883c5a..e5a87d4 100755 --- a/protocol/KISS.c +++ b/protocol/KISS.c @@ -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); + } } } diff --git a/protocol/KISS.h b/protocol/KISS.h index fe97011..01dcd01 100755 --- a/protocol/KISS.h +++ b/protocol/KISS.h @@ -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