Compare commits

...

2 Commits

Author SHA1 Message Date
Sebastian e9fe44fad0 Fixed logging 2018-07-16 21:53:49 +02:00
Sebastian 4dd13016e4 Working fully automatic wspr beacon 2018-07-15 14:54:33 +02:00
7 changed files with 233 additions and 6 deletions

View File

@ -4,8 +4,8 @@ ISPPORT ?= /dev/kaboard
VERSION = 0.1
HEADERS = include/twi.h include/si5351.h include/wspr.h
SRC = main.c twi.c si5351.c wspr.c
HEADERS = include/twi.h include/timer.h include/uart.h include/si5351.h include/wspr.h
SRC = main.c twi.c timer.c uart.c si5351.c wspr.c
TARGET = cube-kl
OBJDIR = bin

39
firmware/include/timer.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef TIMER_H_
#define TIMER_H_ TIMER_H_
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h>
extern volatile uint32_t timestamp;
extern volatile uint16_t milliseconds;
/*
* Timer setting:
* MCU running at 16MHz:
* Prescaler is 64 which results in 250000 ticks per second
* Preloading the counter with 6 leads to 1000 overflow interrupts per second
* or one overflow every millisecond.
*/
static inline void timer_init(void) {
TCNT0 = 6; //Preload for 250 ticks to overflow
TIMSK |= (1 << TOIE0);
TCCR0 = (1 << CS00) | (1 << CS01); // Prescaler 64
timestamp = 0;
milliseconds = 0;
}
static inline void timer_set(uint32_t stamp) {
TCCR0 &= ~((1 << CS00) | (1 << CS01)); // stop the timer
TCNT0 = 6; //Preload for 250 ticks to overflow
timestamp = stamp;
milliseconds = 0;
TCCR0 = (1 << CS00) | (1 << CS01); // Restart timer
}
#endif

58
firmware/include/uart.h Normal file
View File

@ -0,0 +1,58 @@
#ifndef UART_H_
#define UART_H_ UART_H_
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include <string.h>
//#define BAUD 76800UL // baudrate
#define BAUD 38400UL
#define UART_TIMEOUT 100 // Timeout in ms
// Some calculations ...
#define UBRR_VAL ((F_CPU+BAUD*8)/(BAUD*16)-1) // Rounding magic
#define BAUD_REAL (F_CPU/(16*(UBRR_VAL+1))) // Real baudrate
#define BAUD_ERROR ((BAUD_REAL*1000)/BAUD)
#if ((BAUD_ERROR<950) || (BAUD_ERROR>1050)) // Make sure your UBRR_VAL will work
#error Baudrate error is bigger then 1% !
#endif
extern uint8_t uart_timed_out;
void uart_init(void);
uint8_t uart_getc_timeout(void);
uint8_t uart_get_line(char buffer[], uint8_t maxlen);
static inline void uart_putc(uint8_t data) {
UDR = data; // write byte to data register
while (!(UCSRA & (1<< UDRE))); // waiting for the uart to finish transmission
UCSRA |= (1 << UDRE);
}
static inline void uart_puts(char *data) {
uint8_t i;
for(i = 0; i < strlen(data); i++) {
uart_putc(data[i]);
}
}
static inline uint8_t uart_getc(void) {
while (!(UCSRA & (1<<RXC)));
return UDR;
}
static inline uint8_t uart_has_timed_out(void) {
return uart_timed_out;
}
static inline void uart_clear_time_out(void) {
uart_timed_out = 0;
}
#endif

View File

@ -1,28 +1,51 @@
#include <avr/io.h>
#include <util/delay.h>
#include "timer.h"
#include "uart.h"
#include "twi.h"
#include "si5351.h"
#include "wspr.h"
int main(void) {
uart_init();
twi_init();
timer_init();
sei();
si5351_init(25000000, 800000000, 500000000);
si5351_ms_set_source(SI5351_MS0, SI5351_PLLA);
//si5351_ms_set_freq(SI5351_MS0, 7040100);
//si5351_ms_enable_output(SI5351_MS0);
//si5351_ms_write_params(SI5351_MS0, WSPR_SYMBOLS[3]);
uint8_t wspr_buffer[WSPR_LENGTH];
wspr_encode("DL1SSK", "JN39", 27, wspr_buffer);
wspr_transmit(SI5351_MS0, wspr_buffer);
char line[16];
while(1) {
uint8_t res = 0;
while(res != 1) {
uart_puts("?\n");
res = uart_get_line(line, 16);
}
uint32_t realtime = strtoul(line,NULL,10);
timer_set(realtime);
uart_puts("Time set\n");
while(timestamp % 120 != 1) {
_delay_ms(25);
}
wspr_transmit(SI5351_MS0, wspr_buffer);
uart_puts("Transmission send\n");
}
while(1);

View File

@ -0,0 +1,25 @@
#/usr/bin/env python3
import serial
import time
def main():
ser = serial.Serial('/dev/kaboard', 38400, timeout=10)
ser.flush()
while True:
line = ser.readline()
while len(line) == 0:
line = ser.readline()
line = line.decode("ascii")[:-1]
print("[%s] >> %s" % (time.strftime('%H:%M:%S'), line))
if line == "?":
timestamp = "%d\n" % int(time.time())
print("[%s] << %s" % (time.strftime('%H:%M:%S'),timestamp[:-1]))
ser.write(timestamp.encode('ascii'))
if __name__ == '__main__':
main()

14
firmware/timer.c Normal file
View File

@ -0,0 +1,14 @@
#include "timer.h"
volatile uint32_t timestamp;
volatile uint16_t milliseconds;
ISR(TIMER0_OVF_vect) {
TCNT0 = 6; //Preload for 250 ticks to overflow
milliseconds++;
if(milliseconds > 999) {
timestamp++;
milliseconds = 0;
}
}

68
firmware/uart.c Normal file
View File

@ -0,0 +1,68 @@
#include "uart.h"
uint8_t uart_timed_out = 0;
void uart_init(void) {
UBRRH = UBRR_VAL >> 8; //Setting baudrate
UBRRL = UBRR_VAL & 0xFF;
UCSRB |= (1<<TXEN) | (1<<RXEN); // UART TX
UCSRC = (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // Asynchronous 8N1
// flush receive buffer
do
{
UDR;
}
while (UCSRA & (1 << RXC));
//reset tx and rx completeflags
UCSRA = (1 << RXC) | (1 << TXC) | (1 << UDRE);
}
uint8_t uart_getc_timeout(void) {
uint8_t retries = UART_TIMEOUT;
uint8_t delays = 0;
while (!(UCSRA & (1<<RXC)) && (retries > 0)) {
if(delays == 0) {
retries--;
}
delays = (delays + 1) % 250;
_delay_us(4);
}
if(retries > 0) {
uart_timed_out = 0;
return UDR;
}
uart_timed_out = 1;
return 0;
}
uint8_t uart_get_line(char buffer[], uint8_t maxlen) {
char t = 0;
uint8_t pos = 0;
buffer[0] = 0;
//maxlen needs to be at least big enough for one character + null byte.
if(maxlen < 2) {
return 0;
}
uart_clear_time_out();
while(pos < maxlen && t != '\n' && !uart_has_timed_out()) {
t = uart_getc_timeout();
buffer[pos] = t;
pos++;
}
// We passed the loop at least once, so pos can not be 0
if(buffer[pos-1] != '\n') {
return 0;
}
buffer[pos-1] = 0;
return 1;
}