Skip to content

Commit

Permalink
Not sure about these changes, but gonna go ahead and commit them...
Browse files Browse the repository at this point in the history
  • Loading branch information
crashkopf committed Oct 4, 2017
1 parent 43a2ce2 commit 87386b1
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 20 deletions.
Empty file added include/atmega640/timer.h
Empty file.
Empty file added include/avr/timer.h
Empty file.
Empty file added include/kitchensink/timer.h
Empty file.
23 changes: 20 additions & 3 deletions include/timer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
#ifndef TIMER_H
#define TIMER_H

// Note: it would be really great to use ATOMIC_BLOCK to turn off interrupts
// around these register accesses, but unfortunately we'd have to switch
// to compiling this with C99, which isn't supported by the Arduino IDE.
#define xstringify(x) #x
#define stringify(x) xstringify(x)
#define pathinclude(path, file) stringify(path/file)

#ifdef _TARGET
#define inc stringify(_TARGET/timer.h)
#include inc
#undef inc
#endif
#ifdef _CPU
#define inc stringify(_CPU/timer.h)
#include inc
#undef inc
#endif
#ifdef _ARCH
#define inc stringify(_ARCH/timer.h)
#include inc
#undef inc
#endif

//#include <util/atomic.h>

typedef struct timer_s {
Expand Down
35 changes: 18 additions & 17 deletions src/atmega640/serio.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ typedef struct SIO_port {
volatile unsigned char *CTS_port;
volatile unsigned char *CTS_DDR;
unsigned char CTS_pin;
SIO_rx_callback_t rx_callback;
SIO_tx_callback_t tx_callback;
SIO_dre_callback_t dre_callback;
buffer rdbuf;
buffer wrbuf;
char rddata[BSIZE];
Expand All @@ -43,14 +46,6 @@ SIO_port_t USART1 = {
&UDR1
};

SIO_rx_callback_t USART0_rx;
SIO_tx_callback_t USART0_tx;
SIO_dre_callback_t USART0_dre;

SIO_rx_callback_t USART1_rx;
SIO_tx_callback_t USART1_tx;
SIO_dre_callback_t USART1_dre;

static inline void SIO_txstart(SIO_port_t *port) {
*port->UCSRB |= _BV(UDRIE) | _BV(TXEN); // Turn on UDR empty interrupt
}
Expand All @@ -69,37 +64,37 @@ ISR(USART0_RX_vect) {
// Always read from UDR, otherwise the interrupt will keep firing
char c = UDR0;
writeb(&USART0.rdbuf, &c, 1);
if (USART0_rx) USART0_rx(&USART0);
if (USART0.rx_callback) USART0.rx_callback(&USART0);
}

ISR(USART0_TX_vect) {
if (USART0_tx) USART0_tx(&USART0);
if (USART0.tx_callback) USART0.tx_callback(&USART0);
}

ISR(USART0_UDRE_vect) {
char c;
if (readb(&USART0.wrbuf, &c, 1) > 0) UDR0 = c;
else SIO_txstop(&USART0); // Turn off interrupt when the buffer is empty, otherwise it will keep firing
if (USART0_dre) USART0_dre(&USART0);
if (USART0.dre_callback) USART0.dre_callback(&USART0);
}

// USART1 interrupt handlers
ISR(USART1_RX_vect) {
// Always read from UDR, otherwise the interrupt will keep firing
char c = UDR1;
writeb(&USART1.rdbuf, &c, 1);
if (USART1_rx) USART1_rx(&USART1);
if (USART1.rx_callback) USART1.rx_callback(&USART1);
}

ISR(USART1_TX_vect) {
if (USART1_tx) USART1_tx(&USART0);
if (USART1.tx_callback) USART1.tx_callback(&USART0);
}

ISR(USART1_UDRE_vect) {
char c;
if (readb(&USART1.wrbuf, &c, 1) > 0) UDR1 = c;
else SIO_txstop(&USART1); // Turn off interrupt when the buffer is empty, otherwise it will keep firing
if (USART1_dre) USART1_dre(&USART1);
if (USART1.dre_callback) USART1.dre_callback(&USART1);
}


Expand Down Expand Up @@ -164,6 +159,12 @@ int SIO_write(SIO_port_t *port, char * s, unsigned int m) {
return n;
}

void SIO_set_rx_callback(SIO_port_t *port, SIO_rx_callback_t cb) {}
void SIO_set_tx_callback(SIO_port_t *port, SIO_tx_callback_t cb) {}
void SIO_set_dre_callback(SIO_port_t *port, SIO_dre_callback_t cb) {}
void SIO_set_rx_callback(SIO_port_t *port, SIO_rx_callback_t cb) {
port->rx_callback = cb;
}
void SIO_set_tx_callback(SIO_port_t *port, SIO_tx_callback_t cb) {
port->tx_callback = cb;
}
void SIO_set_dre_callback(SIO_port_t *port, SIO_dre_callback_t cb) {
port->dre_callback = cb;
}
27 changes: 27 additions & 0 deletions src/attic/test/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
INCLUDES = -I/usr/lib/avr/include
OBJS = serio.o adc.o adcmap.o buffer.o event.o shell/shell.o shell/command.o unitest.o
CPU = atmega1280
SYSCLOCK = 16000000

CC = avr-gcc
CFLAGS = -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=$(CPU) -DF_CPU=$(SYSCLOCK)L $(INCLUDES)

ISP = arduino
PORTDEV = /dev/ttyUSB0
PORTSPEED = 57600

%.o: %.c *.h
$(CC) $(CFLAGS) -o $@ $<

project.elf: $(OBJS)
$(CC) -mmcu=$(CPU) -o project.elf $^

project.hex: project.elf
avr-objcopy -j .text -j .data -O ihex $< $@

install: project.hex
avrdude -p$(CPU) -c$(ISP) -P$(PORTDEV) -b$(PORTSPEED) -D -Uflash:w:project.hex:i

clean:
rm $(OBJS) project.elf project.hex

0 comments on commit 87386b1

Please sign in to comment.