From 6258e1e62e0012b7b11cb263850cbc1fb40f46c0 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sat, 12 Jan 2019 16:30:26 +0100 Subject: [PATCH] LED driver implemented --- device.h | 4 +++- hardware/AFSK.c | 8 ++++++-- hardware/LED.c | 26 ++++++++++++-------------- hardware/LED.h | 2 +- main.c | 2 +- protocol/KISS.c | 44 +++++++++++++++++++++++--------------------- util/time.h | 16 +++++++--------- 7 files changed, 53 insertions(+), 49 deletions(-) diff --git a/device.h b/device.h index 23fe5a0..437d7da 100755 --- a/device.h +++ b/device.h @@ -16,7 +16,8 @@ // TODO: Change this back to default #define CONFIG_LED_INTENSITY 35 //#define CONFIG_LED_INTENSITY 192 -#define COM_LED_TIMEOUT_MS 40 +#define CONFIG_COM_LED_TIMEOUT_MS 40 +#define CONFIG_LED_UPDATE_INTERVAL_MS 40 // Demodulator settings #define OPEN_SQUELCH true @@ -29,6 +30,7 @@ #define CONFIG_QUEUE_MAX_LENGTH 15 #define CONFIG_SERIAL_BUFFER_SIZE 1532 // TODO: Tune this, what is actually required? #define CONFIG_SERIAL_TIMEOUT_MS 10 +#define CONFIG_BENCHMARK_MODE false // CSMA Settings #define CONFIG_FULL_DUPLEX false // TODO: Actually implement fdx diff --git a/hardware/AFSK.c b/hardware/AFSK.c index ca03f6d..4a1b407 100755 --- a/hardware/AFSK.c +++ b/hardware/AFSK.c @@ -6,11 +6,11 @@ // TODO: Remove testing vars //// #define SAMPLES_TO_CAPTURE 128 -uint32_t capturedsamples = 0; +ticks_t capturedsamples = 0; uint8_t samplebuf[SAMPLES_TO_CAPTURE]; ///////////////////////////////// -extern volatile uint32_t _clock; +extern volatile ticks_t _clock; extern unsigned long custom_preamble; extern unsigned long custom_tail; @@ -24,6 +24,8 @@ int afsk_putchar(char c, FILE *stream); // ADC and clock setup void AFSK_hw_init(void) { + _clock = 0; + // Run ADC initialisation AFSK_adc_init(); @@ -613,5 +615,7 @@ ISR(ADC_vect) { AFSK_adc_isr(AFSK_modem, (ADCH - 128)); } + update_led_status(); + ++_clock; } \ No newline at end of file diff --git a/hardware/LED.c b/hardware/LED.c index 7402557..d6790ca 100644 --- a/hardware/LED.c +++ b/hardware/LED.c @@ -2,9 +2,13 @@ #include "util/time.h" uint8_t ledIntensity = CONFIG_LED_INTENSITY; -uint32_t com_led_timeout = 0; +ticks_t led_status_ticks_top = 0; +ticks_t led_status_ticks = 0; +ticks_t com_led_timeout = 0; void LED_init(void) { + led_status_ticks_top = ms_to_ticks(CONFIG_LED_UPDATE_INTERVAL_MS); + // Enable output for LED pins and drain pin LED_DDR |= _BV(0) | // RX _BV(1) | // TX @@ -32,26 +36,20 @@ void LED_setIntensity(uint8_t value) { 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(); - } - } + com_led_timeout = timer_clock() + ms_to_ticks(CONFIG_COM_LED_TIMEOUT_MS); } 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()); +void update_led_status(void) { + if (led_status_ticks >= led_status_ticks_top) { + if (timer_clock() > com_led_timeout) { LED_COM_OFF(); } + led_status_ticks = 0; + } else { + led_status_ticks++; } } \ No newline at end of file diff --git a/hardware/LED.h b/hardware/LED.h index 55b0ed7..5fd318d 100644 --- a/hardware/LED.h +++ b/hardware/LED.h @@ -16,6 +16,6 @@ void LED_setIntensity(uint8_t value); void LED_COM_ON(void); void LED_COM_OFF(void); -void led_status(void); +void update_led_status(void); #endif \ No newline at end of file diff --git a/main.c b/main.c index 94c90b0..be4ac47 100755 --- a/main.c +++ b/main.c @@ -40,6 +40,7 @@ void init(void) { system_check(); } + int main (void) { init(); @@ -47,7 +48,6 @@ 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 0d8ed56..18b9dbe 100755 --- a/protocol/KISS.c +++ b/protocol/KISS.c @@ -24,7 +24,7 @@ size_t packet_lengths_buf[CONFIG_QUEUE_MAX_LENGTH+1]; AX25Ctx *ax25ctx; Afsk *channel; Serial *serial; -volatile uint32_t last_serial_read = 0; +volatile ticks_t last_serial_read = 0; size_t frame_len; bool IN_FRAME; bool ESCAPE; @@ -57,28 +57,30 @@ void kiss_poll(void) { } } -// TODO: Remove debug functions -//size_t decodes = 0; +#if CONFIG_BENCHMARK_MODE + size_t decodes = 0; +#endif void kiss_messageCallback(AX25Ctx *ctx) { - //decodes++; - //printf("%d\r\n", decodes); - - fputc(FEND, &serial->uart0); - fputc(0x00, &serial->uart0); - for (unsigned i = 0; i < ctx->frame_len-2; i++) { - uint8_t b = ctx->buf[i]; - if (b == FEND) { - fputc(FESC, &serial->uart0); - fputc(TFEND, &serial->uart0); - } else if (b == FESC) { - fputc(FESC, &serial->uart0); - fputc(TFESC, &serial->uart0); - } else { - fputc(b, &serial->uart0); + #if CONFIG_BENCHMARK_MODE + decodes++; + printf("%d\r\n", decodes); + #else + fputc(FEND, &serial->uart0); + fputc(0x00, &serial->uart0); + for (unsigned i = 0; i < ctx->frame_len-2; i++) { + uint8_t b = ctx->buf[i]; + if (b == FEND) { + fputc(FESC, &serial->uart0); + fputc(TFEND, &serial->uart0); + } else if (b == FESC) { + fputc(FESC, &serial->uart0); + fputc(TFESC, &serial->uart0); + } else { + fputc(b, &serial->uart0); + } } - } - fputc(FEND, &serial->uart0); - + fputc(FEND, &serial->uart0); + #endif } void kiss_csma(void) { diff --git a/util/time.h b/util/time.h index d84ef4c..decc79d 100755 --- a/util/time.h +++ b/util/time.h @@ -6,13 +6,12 @@ #define DIV_ROUND(dividend, divisor) (((dividend) + (divisor) / 2) / (divisor)) -//typedef int32_t ticks_t; -//typedef int32_t mtime_t; +typedef int32_t ticks_t; +typedef int32_t mtime_t; +volatile ticks_t _clock; -volatile uint32_t _clock; - -static inline uint32_t timer_clock(void) { - uint32_t result; +static inline ticks_t timer_clock(void) { + ticks_t result; ATOMIC_BLOCK(ATOMIC_RESTORESTATE) { result = _clock; @@ -22,7 +21,7 @@ static inline uint32_t timer_clock(void) { } -inline uint32_t ms_to_ticks(mtime_t ms) { +inline ticks_t ms_to_ticks(mtime_t ms) { return ms * DIV_ROUND(CLOCK_TICKS_PER_SEC, 1000); } @@ -38,5 +37,4 @@ static inline void delay_ms(unsigned long ms) { } } - -#endif +#endif \ No newline at end of file