Skip to content

Commit

Permalink
First code.
Browse files Browse the repository at this point in the history
  • Loading branch information
vogelchr committed Sep 8, 2012
1 parent c44fd10 commit 92832b6
Show file tree
Hide file tree
Showing 14 changed files with 841 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
DEVICE_CC = atmega328p
DEVICE_DUDE = m328p

# PROGRAMMER_DUDE = -Pusb -c dragon_isp
PROGRAMMER_DUDE = -P/dev/ttyUSB0 -c stk500v1 -b 57600

AVRDUDE=avrdude
OBJCOPY=avr-objcopy
OBJDUMP=avr-objdump
CC=avr-gcc
LD=avr-gcc

LDFLAGS=-Wall -g -mmcu=$(DEVICE_CC)
CPPFLAGS=
CFLAGS=-mmcu=$(DEVICE_CC) -Os -Wall -g -DF_CPU=16000000UL

MYNAME=avr-cobalt-panel

OBJS=$(MYNAME).o hd44780.o cobalt_buttons.o cobalt_leds.o \
serial.o menu.o protocol.o

all : $(MYNAME).hex $(MYNAME).lst

$(MYNAME).bin : $(OBJS)

%.hex : %.bin
$(OBJCOPY) -j .text -j .data -O ihex $^ $@ || (rm -f $@ ; false )

%.lst : %.bin
$(OBJDUMP) -S $^ >$@ || (rm -f $@ ; false )

%.bin : %.o
$(LD) $(LDFLAGS) -o $@ $^

include $(OBJS:.o=.d)

%.d : %.c
$(CC) -o $@ -MM $^

.PHONY : clean burn
burn : $(MYNAME).hex
$(AVRDUDE) $(PROGRAMMER_DUDE) -p $(DEVICE_DUDE) -U flash:w:$^
clean :
rm -f *.bak *~ *.bin *.hex *.lst *.o *.d
67 changes: 67 additions & 0 deletions avr-cobalt-panel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* ----------------------------------------------------------------------------
This file is part of avr_cobalt_panel.
(c) 2012 Christian Vogel <[email protected]>
avr_cobalt_panel 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 3 of the License, or
(at your option) any later version.
avr_cobalt_panel 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.
For a copy of the GNU General Public License see http://www.gnu.org/licenses/.
---------------------------------------------------------------------------- */

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <util/delay.h>

#include "hd44780.h"
#include "cobalt_leds.h"
#include "cobalt_buttons.h"
#include "serial.h"
#include "menu.h"
#include "protocol.h"

prog_char hello_one[] = "avr-cobalt-panel";
prog_char hello_two[] = "github/vogelchr";

int
main(void)
{
uint8_t button;
uint16_t i;

serial_init();

hd44780_init();
cobalt_leds_init();
cobalt_buttons_init();

hd44780_reset();
hd44780_data_PSTR(hello_one,16);
hd44780_cursor(0,1,1,0); /* x,y,cursor,blink */
hd44780_data_PSTR(hello_two,15);

while(1){
i++;
_delay_us(10);

if(serial_status())
protocol_eat_char(getchar());

if(cobalt_buttons_debounce(&button,200)){
char c = cobalt_buttons_simple_char(button);
/* we are only interested in single key presses,
not combinations */

if(c)
putchar(c);
}
}
}
96 changes: 96 additions & 0 deletions cobalt_buttons.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* ----------------------------------------------------------------------------
This file is part of avr_cobalt_panel.
(c) 2012 Christian Vogel <[email protected]>
avr_cobalt_panel 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 3 of the License, or
(at your option) any later version.
avr_cobalt_panel 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.
For a copy of the GNU General Public License see http://www.gnu.org/licenses/.
---------------------------------------------------------------------------- */

#include "cobalt_buttons.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

#include "hd44780.h"

#define PORT_CTRL PORTB
#define DDR_CTRL DDRB
#define BIT_nOE _BV(5)


void
cobalt_buttons_init()
{
PORT_CTRL |= BIT_nOE;
DDR_CTRL |= BIT_nOE;
}

static uint8_t cobalt_buttons_last=0; /* last read button value */
static uint8_t cobalt_buttons_ctr=0; /* debounce timer */

uint8_t
cobalt_buttons_debounce(uint8_t *buttons,uint8_t debounce_counter)
{
uint8_t b = cobalt_buttons_get();

/* different than last time? Set debounce timer to given value. */
if(b != cobalt_buttons_last){
cobalt_buttons_last = b; /* update last read button value */
cobalt_buttons_ctr = debounce_counter;
return 0;
}

if(cobalt_buttons_ctr == 0)
return 0; /* debounce counter has already expired, no changes */

cobalt_buttons_ctr--;

if(cobalt_buttons_ctr > 0)
return 0; /* debounce counter has not yet expired */

/* debounce counter has expired, return button to user */
*buttons = b;
return 1;
}

uint8_t
cobalt_buttons_get()
{
uint8_t ret;

cli();
PORT_CTRL &= ~BIT_nOE;
_delay_us(2);
ret = hd44780_dbus_read();
PORT_CTRL |= BIT_nOE;
sei();

ret ^= 0xff;
return ret & COBALT_BUTTONS_ALL;
}

prog_char button_chars[8]="0PLUDRES";

char
cobalt_buttons_simple_char(uint8_t buttons){
uint8_t k;
char * p = button_chars;

for(k=0;k<8;k++){
if(buttons == (1<<k))
return pgm_read_byte(p);
p++;
}
return '\0';
}
45 changes: 45 additions & 0 deletions cobalt_buttons.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#ifndef COBALT_BUTTONS_H
#define COBALT_BUTTONS_H

#include <stdint.h>

/* see README for connection of buttons to port pins */

#define COBALT_BUTTON_PIN (1<<1) /* hidden beneath hole right of display */
#define COBALT_BUTTON_LEFT (1<<2)
#define COBALT_BUTTON_UP (1<<3)
#define COBALT_BUTTON_DOWN (1<<4)
#define COBALT_BUTTON_RIGHT (1<<5)
#define COBALT_BUTTON_E (1<<6)
#define COBALT_BUTTON_S (1<<7)

#define COBALT_BUTTONS_ALL 0xfe

/* setup data direction registers etc... */
extern void cobalt_buttons_init();

/* get bitfield of currently pressed buttons */
extern uint8_t cobalt_buttons_get();

/* debounce buttons:
This function is called repeatedly with a constant "debounce counter"
argument. The currently pressed buttons are compared with the state
of buttons the last time this function has been called.
If the button inputs have been on a stable value for "debounce_counter"
invocations of this function, then it will return 1 *once*.
*/

extern uint8_t cobalt_buttons_debounce(uint8_t *buttons,uint8_t debounce_counter);

/* convert the button mask buttons to a single char
LEFT -> 'L', RIGHT -> 'R',
UP -> 'U', DOWN -> 'D',
E -> 'E', S -> 'S',
PIN -> 'P'
*/

extern char cobalt_buttons_simple_char(uint8_t buttons);

#endif
45 changes: 45 additions & 0 deletions cobalt_leds.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* ----------------------------------------------------------------------------
This file is part of avr_cobalt_panel.
(c) 2012 Christian Vogel <[email protected]>
avr_cobalt_panel 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 3 of the License, or
(at your option) any later version.
avr_cobalt_panel 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.
For a copy of the GNU General Public License see http://www.gnu.org/licenses/.
---------------------------------------------------------------------------- */
#include "cobalt_leds.h"

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

void cobalt_leds_init(){
cli();
DDRB |= 0x07; /* PB0 = Web, PB1 = Disk, PB2 = Logo */
DDRD |= 0x3c; /* PD2..PD5 */
sei();

cobalt_leds_set(0);
}

void cobalt_leds_set(uint8_t leds){

printf("Set LEDs 0x%02hhx.\n",leds);

leds ^= (COBALT_LED_DISK | COBALT_LED_NET);

cli();
/* Web, Disk, Logo on portb, not inverted */
PORTB = (PORTB & ~ 0x07) | ( leds & 0x07);
/* upper 4 bits leds correspond to bits D2..D5 */
PORTD = (PORTD & ~ 0x3c) | ( 0x3c & (leds >> 2) );
sei();
}
22 changes: 22 additions & 0 deletions cobalt_leds.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef COBALT_LEDS
#define COBALT_LEDS

#include <stdint.h>

#define COBALT_LED_WEB (1<<0)
#define COBALT_LED_DISK (1<<1)
#define COBALT_LED_LOGO (1<<2)

#define COBALT_LED_NET (0xf0)
#define COBALT_LED_TXRX (1<<4) /* Network LEDs */
#define COBALT_LED_100M (1<<5)
#define COBALT_LED_LINK (1<<6)
#define COBALT_LED_COL (1<<7)

/* initialize ports */
extern void cobalt_leds_init();

/* light up LEDs, 1=on 0=off */
extern void cobalt_leds_set(uint8_t leds);

#endif
Loading

0 comments on commit 92832b6

Please sign in to comment.