Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
timm authored and timm committed Feb 11, 2013
0 parents commit 39e6c63
Show file tree
Hide file tree
Showing 18 changed files with 20,851 additions and 0 deletions.
13 changes: 13 additions & 0 deletions firmware/DaliMaster.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <avr/io.h>
#include <util/delay.h>
#include <avr/power.h>
#include <avr/interrupt.h>



int main()
{


return 0;
}
63 changes: 63 additions & 0 deletions firmware/SConstruct
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
""" SConstruct
#
# Copyright 2008 Markus Burrer & CJT, cjt@users.sourceforge.net
#
# 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
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
"""
avr = Environment()

Target="DaliMaster"
mcu="at90pwm316"
F_CPU=16e6

# Add all additional source files to compile
src="""lib_mcu/eusart/eusart_lib.c fifo.c suart.c"""

# Add Source Path here
cpppath=""" .
./lib_mcu/dali
./lib_mcu/eusart"""

cpath = """ .
./lib_mcu/dali
./lib_mcu/eusart"""

# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
opt = "s"

# Add Additional Compiler Flags
flags="""-I./lib_mcu/dali,./lib_mcu/eusart """

# Add Variant Dirs
avr.VariantDir('obj', './lib_mcu/Dali', './lib_mcu/eusart')


# Set Environment Parameters
avr['CC'] = "avr-gcc -mmcu=%s" % mcu
avr.Append(CPPPATH = Split(cpppath))
avr.Append(CCFLAGS = "-O%s" % opt)
avr.Append(CCFLAGS = "-Wall" )
avr.Append(CCFLAGS = "-DF_CPU=%i" % F_CPU)
avr.Append(CCFLAGS = Split(flags))

# Execute AVR-GCC
avr.Program(Target+".elf", Split(Target+".c " + src))

avr.Command(Target+".hex", Target+".elf", "avr-objcopy -j .text -j .data -O ihex $SOURCE $TARGET")
# Show memory usage
avr.Command(None, Target+".elf", "avr-size $SOURCE")


1 change: 1 addition & 0 deletions firmware/euart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "euart.h"
4 changes: 4 additions & 0 deletions firmware/euart.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef _EUART_H_
#define _EUART_H_

#endif
27 changes: 27 additions & 0 deletions firmware/fifo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "fifo.h"

void fifo_init (fifo_t *f, uint8_t *buffer, const uint8_t size)
{
f->count = 0;
f->pread = f->pwrite = buffer;
f->read2end = f->write2end = f->size = size;
}

uint8_t fifo_put (fifo_t *f, const uint8_t data)
{
return _inline_fifo_put (f, data);
}

uint8_t fifo_get_wait (fifo_t *f)
{
while (!f->count);

return _inline_fifo_get (f);
}

int fifo_get_nowait (fifo_t *f)
{
if (!f->count) return -1;

return (int) _inline_fifo_get (f);
}
75 changes: 75 additions & 0 deletions firmware/fifo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#ifndef _FIFO_H_
#define _FIFO_H_

#include <avr/io.h>
#include <avr/interrupt.h>

typedef struct
{
uint8_t volatile count; // # Zeichen im Puffer
uint8_t size; // Puffer-Größe
uint8_t *pread; // Lesezeiger
uint8_t *pwrite; // Schreibzeiger
uint8_t read2end, write2end; // # Zeichen bis zum Überlauf Lese-/Schreibzeiger
} fifo_t;

extern void fifo_init (fifo_t*, uint8_t* buf, const uint8_t size);
extern uint8_t fifo_put (fifo_t*, const uint8_t data);
extern uint8_t fifo_get_wait (fifo_t*);
extern int fifo_get_nowait (fifo_t*);

static inline uint8_t
_inline_fifo_put (fifo_t *f, const uint8_t data)
{
if (f->count >= f->size)
return 0;

uint8_t * pwrite = f->pwrite;

*(pwrite++) = data;

uint8_t write2end = f->write2end;

if (--write2end == 0)
{
write2end = f->size;
pwrite -= write2end;
}

f->write2end = write2end;
f->pwrite = pwrite;

uint8_t sreg = SREG;
cli();
f->count++;
SREG = sreg;

return 1;
}

static inline uint8_t
_inline_fifo_get (fifo_t *f)
{
uint8_t *pread = f->pread;
uint8_t data = *(pread++);
uint8_t read2end = f->read2end;

if (--read2end == 0)
{
read2end = f->size;
pread -= read2end;
}

f->pread = pread;
f->read2end = read2end;

uint8_t sreg = SREG;
cli();
f->count--;
SREG = sreg;

return data;
}

#endif /* FIFO_H */

201 changes: 201 additions & 0 deletions firmware/suart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
#include <avr/io.h>

#include "suart.h"

#include "fifo.h"

#define BAUDRATE 115200

#define nop() __asm volatile ("nop")

#ifdef SUART_TXD
#define SUART_TXD_PORT PORTB
#define SUART_TXD_DDR DDRB
#define SUART_TXD_BIT PB1
static volatile uint16_t outframe;
#endif // SUART_TXD

#ifdef SUART_RXD
#define SUART_RXD_PORT PORTB
#define SUART_RXD_PIN PINB
#define SUART_RXD_DDR DDRB
#define SUART_RXD_BIT PB0
static volatile uint16_t inframe;
static volatile uint8_t inbits, received;

#ifdef _FIFO_H_
#define INBUF_SIZE 4
static uint8_t inbuf[INBUF_SIZE];
fifo_t infifo;
#else // _FIFO_H_
static volatile uint8_t indata;
#endif // _FIFO_H_
#endif // SUART_RXD

void suart_init()
{
uint8_t tifr = 0;
uint8_t sreg = SREG;
cli();

// Mode #4 für Timer1
// und volle MCU clock
// IC Noise Cancel
// IC on Falling Edge
TCCR1A = 0;
TCCR1B = (1 << WGM12) | (1 << CS10) | (0 << ICES1) | (1 << ICNC1);

// OutputCompare für gewünschte Timer1 Frequenz
OCR1A = (uint16_t) ((uint32_t) F_CPU/BAUDRATE);

#ifdef SUART_RXD
SUART_RXD_DDR &= ~(1 << SUART_RXD_BIT);
SUART_RXD_PORT |= (1 << SUART_RXD_BIT);
TIMSK1 |= (1 << ICIE1);
tifr |= (1 << ICF1) | (1 << OCF1B);
#else
TIMSK1 &= ~(1 << ICIE1);
#endif // SUART_RXD

#ifdef SUART_TXD
tifr |= (1 << OCF1A);
SUART_TXD_PORT |= (1 << SUART_TXD_BIT);
SUART_TXD_DDR |= (1 << SUART_TXD_BIT);
outframe = 0;
#endif // SUART_TXD

TIFR1 = tifr;

SREG = sreg;

#ifdef _FIFO_H_
fifo_init (&infifo, inbuf, INBUF_SIZE);
#endif // _FIFO_H_

}

#ifdef SUART_TXD
void suart_putc (const char c)
{
do
{
sei(); nop(); cli(); // yield();
} while (outframe);

// frame = *.P.7.6.5.4.3.2.1.0.S S=Start(0), P=Stop(1), *=Endemarke(1)
outframe = (3 << 9) | (((uint8_t) c) << 1);

TIMSK1 |= (1 << OCIE1A);
TIFR1 = (1 << OCF1A);

sei();
}
#endif // SUART_TXD

#ifdef SUART_TXD
ISR ( TIMER1_COMPA_vect)
{
uint16_t data = outframe;

if (data & 1) SUART_TXD_PORT |= (1 << SUART_TXD_BIT);
else SUART_TXD_PORT &= ~(1 << SUART_TXD_BIT);

if (1 == data)
{
TIMSK1 &= ~(1 << OCIE1A);
}

outframe = data >> 1;
}
#endif // SUART_TXD

#ifdef SUART_RXD
ISR ( TIMER1_CAPT_vect)
{
uint16_t icr1 = ICR1;
uint16_t ocr1a = OCR1A;

// Eine halbe Bitzeit zu ICR1 addieren (modulo OCR1A) und nach OCR1B
uint16_t ocr1b = icr1 + ocr1a/2;
if (ocr1b >= ocr1a)
ocr1b -= ocr1a;
OCR1B = ocr1b;

TIFR1 = (1 << OCF1B);
TIMSK1 = (TIMSK1 & ~(1 << ICIE1)) | (1 << OCIE1B);
inframe = 0;
inbits = 0;
}
#endif // SUART_RXD

#ifdef SUART_RXD
ISR (TIMER1_COMPB_vect)
{
uint16_t data = inframe >> 1;

if (SUART_RXD_PIN & (1 << SUART_RXD_BIT))
data |= (1 << 9);

uint8_t bits = inbits+1;

if (10 == bits)
{
if ((data & 1) == 0)
if (data >= (1 << 9))
{
#ifdef _FIFO_H_
_inline_fifo_put (&infifo, data >> 1);
#else
indata = data >> 1;
#endif // _FIFO_H_
received = 1;
}

TIMSK1 = (TIMSK1 & ~(1 << OCIE1B)) | (1 << ICIE1);
TIFR1 = (1 << ICF1);
}
else
{
inbits = bits;
inframe = data;
}
}
#endif // SUART_RXD

#ifdef SUART_RXD
#ifdef _FIFO_H_

int suart_getc_wait()
{
return (int) fifo_get_wait (&infifo);
}

int suart_getc_nowait()
{
return fifo_get_nowait (&infifo);
}

#else // _FIFO_H_

int suart_getc_wait()
{
while (!received) {}
received = 0;

return (int) indata;
}

int suart_getc_nowait()
{
if (received)
{
received = 0;
return (int) indata;
}

return -1;
}

#endif // _FIFO_H_
#endif // SUART_RXD

Loading

0 comments on commit 39e6c63

Please sign in to comment.