Skip to content

Commit

Permalink
Improve uart.c/h support for various MCUs and allow overriding the Ma…
Browse files Browse the repository at this point in the history
…kefile

also, use newer libfrser (as a matter-of-fact update...)
  • Loading branch information
urjaman committed Aug 12, 2016
1 parent cd3af6a commit b8d47ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -24,7 +24,7 @@ SOURCES=main.c uart.c spihw.c
CC=avr-gcc
LD=avr-ld
OBJCOPY=avr-objcopy
MMCU=atmega328p
MMCU ?= atmega328p

# These defaults are for the U2-equipped arduino,
# feel free to change.
Expand Down
2 changes: 1 addition & 1 deletion libfrser
28 changes: 22 additions & 6 deletions uart.c
@@ -1,7 +1,7 @@
/*
* This file is part of the frser-duino project.
*
* Copyright (C) 2009,2011,2013 Urja Rannikko <urjaman@gmail.com>
* Copyright (C) 2009,2011,2013,2016 Urja Rannikko <urjaman@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -21,17 +21,33 @@
#include "main.h"
#include "uart.h"

// UART MODULE START
#if UART_BUFLEN > 256
typedef uint16_t urxbufoff_t;
typedef uint8_t utxbufoff_t;
#else
typedef uint8_t urxbufoff_t;
#endif

static uint8_t volatile uart_rcvbuf[UART_BUFLEN];
static urxbufoff_t volatile uart_rcvwptr;
static urxbufoff_t uart_rcvrptr;

ISR(USART_RX_vect) {
#if ((UART_BUFLEN-1) & UART_BUFLEN) == 0
#define RLIM_RL(r) do { r &= (UART_BUFLEN-1); } while(0)
#else
#define RLIM_RL(r) do { if (r>=UART_BUFLEN) r=0; } while(0)
#endif

#if (defined __AVR_ATmega328P__)||(defined __AVR_ATmega328__)||(defined __AVR_ATmega168P__)||(defined __AVR_ATmega168__)||(__AVR_ATmega88P__)||(__AVR_ATmega88__)||(defined __AVR_ATmega48P__)||(defined __AVR_ATmega48__)
#define RX_ISR USART_RX_vect
#else
/* We assume this is a newer avr with fancier USART ISR names, fix if it warns :) */
#define RX_ISR USART0_RX_vect
#endif

ISR(RX_ISR) {
urxbufoff_t reg = uart_rcvwptr;
uart_rcvbuf[reg++] = UDR0;
if(reg==UART_BUFLEN) reg = 0;
RLIM_RL(reg);
uart_rcvwptr = reg;
}

Expand Down Expand Up @@ -61,7 +77,7 @@ uint8_t uart_recv(void) {
while (!uart_isdata()) uart_waiting();
reg = uart_rcvrptr;
val = uart_rcvbuf[reg++];
if(reg==UART_BUFLEN) reg = 0;
RLIM_RL(reg);
uart_rcvrptr = reg;
return val;
}
Expand Down
4 changes: 3 additions & 1 deletion uart.h
Expand Up @@ -30,7 +30,9 @@ void uart_wait_txdone(void);

#define RECEIVE() uart_recv()
#define SEND(n) uart_send(n)
#define UART_BUFLEN 1024
#define RAM_BYTES_ (RAMEND-RAMSTART+1)
/* SPI only doesnt have opbuf, so can have bigger uart buf than others... */
#define UART_BUFLEN (RAM_BYTES_/2)
/* Compat; Int Tx support was stripped. */
#define UART_POLLED_TX
#define UARTTX_BUFLEN 0

0 comments on commit b8d47ac

Please sign in to comment.