Skip to content

Commit

Permalink
Moving stuff around, making external build work
Browse files Browse the repository at this point in the history
  • Loading branch information
crashkopf committed Aug 10, 2016
1 parent e67b9b7 commit 43a2ce2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 86 deletions.
54 changes: 28 additions & 26 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
#PROJECT ?=

SRCDIR ?= src
BUILDDIR ?= build
INCLUDEDIR ?= include
LIBCRASHKOPF ?= .
SRCDIR ?= $(LIBCRASHKOPF)/src
INCLUDEDIR ?= $(LIBCRASHKOPF)/include

TARGET ?=
CPU ?=
ARCH ?=
INCLUDES = $(INCLUDEDIR)

-include $(SRCDIR)/$(TARGET)/profile.mk
-include $(SRCDIR)/$(CPU)/profile.mk
-include $(SRCDIR)/$(ARCH)/profile.mk

INCLUDES = $(INCLUDEDIR) $(INCLUDEDIR)/$(TARGET) $(INCLUDEDIR)/$(CPU) $(INCLUDEDIR)/$(ARCH)
VPATH = $(SRCDIR)

DEFINES = -D_ARCH=$(ARCH) -D_CPU=$(CPU) -D_TARGET=$(TARGET) -D_PROJECT=$(PROJECT)
CFLAGS = -c -Os -Wall -Wno-main

CFLAGS = -c -Os -Wall -Wno-main -fno-exceptions -ffunction-sections -fdata-sections $(DEFINES) $(addprefix -iquote , $(INCLUDES)) $(ARCH_FLAGS) $(HWDEFS)
ifdef TARGET
include $(SRCDIR)/$(TARGET)/profile.mk
INCLUDES += $(INCLUDEDIR)/$(TARGET)
DEFINES += _ARCH=$(ARCH)
VPATH += $(SRCDIR)/$(TARGET)
endif

#.IGNORE $(addsuffix /serio.h, $(INCLUDES))
ifdef CPU
include $(SRCDIR)/$(CPU)/profile.mk
INCLUDES += $(INCLUDEDIR)/$(CPU)
DEFINES += _CPU=$(CPU)
VPATH += $(SRCDIR)/$(CPU)
endif

VPATH = $(SRCDIR)
ifdef ARCH
include $(SRCDIR)/$(ARCH)/profile.mk
INCLUDES += $(INCLUDEDIR)/$(ARCH)
DEFINES += _TARGET=$(TARGET)
VPATH += $(SRCDIR)/$(ARCH)
endif

library := libcrashkopf.a
#CFLAGS = -c -Os -Wall -Wno-main -fno-exceptions -ffunction-sections -fdata-sections $(addprefix -D, $(DEFINES)) $(addprefix -I, $(INCLUDES)) $(ARCH_FLAGS) $(HWDEFS)
CFLAGS += $(addprefix -D, $(DEFINES)) $(addprefix -I, $(INCLUDES)) $(ARCH_FLAGS) $(HWDEFS)

all: serio.o buffer.o

serio.o: $(CPU)/serio.c $(addsuffix /serio.h, $(INCLUDES)) buffer.o
serio.o: serio.c $(addsuffix /serio.h, $(INCLUDES)) buffer.o
buffer.o: buffer.c $(INCLUDEDIR)/buffer.h

#$(objs): $(BUILDDIR)/%.o: %.c
# mkdir -p $(@D)
# $(CC) $(CFLAGS) -MD -o $@ $<

#$(library): $(DEVICE) $(objs)
#
#$(AR) rcs $@ $^

#-include $(objs:.o=.d)

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

clean:
Expand Down
12 changes: 11 additions & 1 deletion include/atmega640/serio.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,28 @@
#define UPM0 4
#define UPM1 5

typedef struct {
/*
typedef struct SIO_port {
volatile unsigned char *UCSRA;
volatile unsigned char *UCSRB;
volatile unsigned char *UCSRC;
volatile unsigned char *UBRRL;
volatile unsigned char *UBRRH;
volatile unsigned char *UDR;
volatile unsigned char *RTS_port;
volatile unsigned char *RTS_DDR;
unsigned char RTS_pin;
volatile unsigned char *CTS_port;
volatile unsigned char *CTS_DDR;
unsigned char CTS_pin;
buffer rdbuf;
buffer wrbuf;
char rddata[BSIZE];
char wrdata[BSIZE];
} SIO_port_t;
*/

typedef char SIO_byte_t; // Some serial systems support 9-bit bytes, so we can't rely on it to be a char

typedef unsigned long SIO_baud_t;

Expand Down
8 changes: 5 additions & 3 deletions include/serio.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#endif
*/

typedef struct SIO_port SIO_port_t;

#ifdef _TARGET
#define inc stringify(_TARGET/serio.h)
#include inc
Expand All @@ -36,9 +38,9 @@
#define SIO_PARITY_NONE 0
#define SIO_STOPBITS_1 0

typedef void (* SIO_rx_callback_t)(SIO_port_t * port, volatile unsigned char * data);
typedef void (* SIO_tx_callback_t)(SIO_port_t * port, volatile unsigned char * data);
typedef void (* SIO_dre_callback_t)(SIO_port_t * port, volatile unsigned char * data);
typedef void (* SIO_rx_callback_t)(SIO_port_t * port);
typedef void (* SIO_tx_callback_t)(SIO_port_t * port);
typedef void (* SIO_dre_callback_t)(SIO_port_t * port);

void SIO_init(SIO_port_t * port, SIO_baud_t baud, unsigned char framedef);
void SIO_set_baud(SIO_port_t *port, SIO_baud_t baud);
Expand Down
113 changes: 57 additions & 56 deletions src/atmega640/serio.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@

#include "serio.h"

typedef struct SIO_port {
volatile unsigned char *UCSRA;
volatile unsigned char *UCSRB;
volatile unsigned char *UCSRC;
volatile unsigned char *UBRRL;
volatile unsigned char *UBRRH;
volatile unsigned char *UDR;
volatile unsigned char *RTS_port;
volatile unsigned char *RTS_DDR;
unsigned char RTS_pin;
volatile unsigned char *CTS_port;
volatile unsigned char *CTS_DDR;
unsigned char CTS_pin;
buffer rdbuf;
buffer wrbuf;
char rddata[BSIZE];
char wrdata[BSIZE];
} SIO_port_t;

SIO_port_t USART0 = {
&UCSR0A,
&UCSR0B,
Expand All @@ -24,24 +43,6 @@ SIO_port_t USART1 = {
&UDR1
};

SIO_port_t USART2 = {
&UCSR2A,
&UCSR2B,
&UCSR2C,
&UBRR2L,
&UBRR2H,
&UDR2
};

SIO_port_t USART3 = {
&UCSR3A,
&UCSR3B,
&UCSR3C,
&UBRR3L,
&UBRR3H,
&UDR3
};

SIO_rx_callback_t USART0_rx;
SIO_tx_callback_t USART0_tx;
SIO_dre_callback_t USART0_dre;
Expand All @@ -50,45 +51,58 @@ SIO_rx_callback_t USART1_rx;
SIO_tx_callback_t USART1_tx;
SIO_dre_callback_t USART1_dre;

SIO_rx_callback_t USART1_rx;
SIO_tx_callback_t USART1_tx;
SIO_dre_callback_t USART1_dre;

SIO_rx_callback_t USART2_rx;
SIO_tx_callback_t USART2_tx;
SIO_dre_callback_t USART2_dre;

static inline void txstart(SIO_port_t *port) {
static inline void SIO_txstart(SIO_port_t *port) {
*port->UCSRB |= _BV(UDRIE) | _BV(TXEN); // Turn on UDR empty interrupt
}
static inline void txstop(SIO_port_t *port) {
static inline void SIO_txstop(SIO_port_t *port) {
*port->UCSRB &= ~(_BV(UDRIE) | _BV(TXEN)); // Turn off UDR empty interrupt
}
static inline void rxstart(SIO_port_t *port) {
static inline void SIO_rxstart(SIO_port_t *port) {
*port->UCSRB |= _BV(RXCIE) | _BV(RXEN); // Turn on RX complete interrupt
}
static inline void rxstop(SIO_port_t *port) {
static inline void SIO_rxstop(SIO_port_t *port) {
*port->UCSRB &= ~(_BV(RXCIE) | _BV(RXEN)); // Turn off RX complete interrupt
}

// USART0 interrupt handlers
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, &UDR0);
if (USART0_rx) USART0_rx(&USART0);
}

ISR(USART0_TX_vect) {
if (USART0_tx) USART0_tx(&USART0, &UDR0);
if (USART0_tx) USART0_tx(&USART0);
}

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

ISR(USART1_TX_vect) {
if (USART1_tx) USART1_tx(&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);
}


void SIO_init(SIO_port_t * port, SIO_baud_t baud, unsigned char framedef) {

SIO_set_baud(port, baud);
Expand All @@ -104,17 +118,14 @@ void SIO_init(SIO_port_t * port, SIO_baud_t baud, unsigned char framedef) {
port->wrbuf.size = BSIZE;
port->wrbuf.data = port->wrdata;

rxstart(port); // enable interrupts
txstart(port);
SIO_rxstart(port); // enable interrupts
SIO_txstart(port);
}

void SIO_set_baud(SIO_port_t *port, SIO_baud_t baud) {
int use2x;
unsigned long div;

rxstop(port);
txstop(port);

div = (((F_CPU) + 8UL * (baud)) / (16UL * (baud)) - 1UL);

if (100 * (F_CPU) > (16 * ((div) + 1)) * (100 * (baud) + (baud) * (BAUD_TOL)))
Expand All @@ -130,39 +141,29 @@ void SIO_set_baud(SIO_port_t *port, SIO_baud_t baud) {
}
else
*port->UBRRL = (F_CPU / (16UL * baud)) - 1;

rxstart(port);
txstart(port);
}

void SIO_set_frame(SIO_port_t *port, unsigned char framedef) {

rxstop(port);
txstop(port);

// This just sets us to 8N1. I'll fix it later.
*port->UCSRC = _BV(UCSZ0) | _BV(UCSZ1);

rxstart(port);
txstart(port);
}

int SIO_read(SIO_port_t *port, char * s, unsigned int m) {
int n;
rxstop(port); // Stop RX so buffer isn't modified
SIO_rxstop(port); // Stop RX so buffer isn't modified
n = readb(&port->rdbuf, s, m); // Read from buffer
rxstart(port); // Start RX again
SIO_rxstart(port); // Start RX again
return n;
}

int SIO_write(SIO_port_t *port, char * s, unsigned int m) {
int n;
txstop(port); // Stop TX so buffer isn't modified
SIO_txstop(port); // Stop TX so buffer isn't modified
n = writeb(&port->wrbuf, s, m); // Write to buffer
txstart(port); // Start TX again
SIO_txstart(port); // Start TX again
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) {}
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) {}

0 comments on commit 43a2ce2

Please sign in to comment.