2008-12-03 05:40:16 +00:00
|
|
|
#include "../config.h"
|
|
|
|
#include "../makros.h"
|
|
|
|
|
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/io.h>
|
|
|
|
#include <avr/wdt.h>
|
|
|
|
#include "borg_hw.h"
|
|
|
|
|
|
|
|
/*
|
2011-08-17 01:16:25 +00:00
|
|
|
// those macros get defined via menuconfig, now
|
|
|
|
|
|
|
|
// 16 columns total directly controlled, therefore 2 ports
|
|
|
|
#define COLPORT1 PORTC
|
|
|
|
#define COLDDR1 DDRC
|
|
|
|
|
|
|
|
#define COLPORT2 PORTA
|
|
|
|
#define COLDDR2 DDRA
|
|
|
|
|
|
|
|
// the other port controls the shift registers
|
|
|
|
#define ROWPORT PORTD
|
|
|
|
#define ROWDDR DDRD
|
|
|
|
|
|
|
|
// both clock and reset are connected to each shift register
|
|
|
|
// reset pin is negated
|
|
|
|
#define PIN_MCLR PD4
|
|
|
|
#define PIN_CLK PD6
|
|
|
|
|
|
|
|
// these are the individual data input pins for the shift registers
|
|
|
|
#define PIN_DATA PD7
|
2008-12-03 05:40:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define COLDDR1 DDR(COLPORT1)
|
|
|
|
#define COLDDR2 DDR(COLPORT2)
|
|
|
|
#define ROWDDR DDR(ROWPORT)
|
|
|
|
|
2012-02-12 20:47:55 +00:00
|
|
|
#if defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644__)
|
2010-01-29 03:05:49 +00:00
|
|
|
/* more ifdef magic :-( */
|
|
|
|
#define OCR0 OCR0A
|
2012-03-23 01:27:59 +00:00
|
|
|
#define TIMER0_COMP_vect TIMER0_COMPA_vect
|
2010-01-29 03:05:49 +00:00
|
|
|
#endif
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// buffer which holds the currently shown frame
|
2008-12-03 05:40:16 +00:00
|
|
|
unsigned char pixmap[NUMPLANE][NUM_ROWS][LINEBYTES];
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// switch to next row
|
|
|
|
static void nextrow(uint8_t row) {
|
|
|
|
//reset states of preceding row
|
2008-12-03 05:40:16 +00:00
|
|
|
COLPORT1 = 0;
|
|
|
|
COLPORT2 = 0;
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// short delay loop, to ensure proper deactivation of the drivers
|
2008-12-03 05:40:16 +00:00
|
|
|
unsigned char i;
|
2011-08-17 01:16:25 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
2008-12-03 05:40:16 +00:00
|
|
|
asm volatile("nop");
|
|
|
|
}
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
if (row == 0) {
|
|
|
|
// row 0: initialize first shift register
|
2009-01-01 17:06:38 +00:00
|
|
|
#ifndef INVERT_ROWS
|
2011-08-17 01:16:25 +00:00
|
|
|
ROWPORT |= (1 << PIN_DATA);
|
|
|
|
ROWPORT |= (1 << PIN_CLK);
|
|
|
|
ROWPORT &= ~(1 << PIN_CLK);
|
|
|
|
ROWPORT &= ~(1 << PIN_DATA);
|
2008-12-03 05:40:16 +00:00
|
|
|
#else
|
|
|
|
ROWPORT&= ~(1<<PIN_DATA);
|
|
|
|
ROWPORT|= (1<<PIN_CLK);
|
|
|
|
ROWPORT&= ~(1<<PIN_CLK);
|
|
|
|
ROWPORT|= (1<<PIN_DATA);
|
|
|
|
|
|
|
|
#endif
|
2011-08-17 01:16:25 +00:00
|
|
|
} else {
|
|
|
|
// remaining rows: just shift forward
|
|
|
|
ROWPORT |= (1 << PIN_CLK);
|
|
|
|
ROWPORT &= ~(1 << PIN_CLK);
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// another delay loop, to ensure that the drivers are ready
|
|
|
|
for (i = 0; i < 20; i++) {
|
2008-12-03 05:40:16 +00:00
|
|
|
asm volatile("nop");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// show a row
|
|
|
|
static void rowshow(unsigned char row, unsigned char plane) {
|
|
|
|
// depending on the currently drawn plane, display the row for a specific
|
|
|
|
// amount of time
|
|
|
|
static unsigned char const ocr_table[] = {3, 4, 22};
|
|
|
|
OCR0 = ocr_table[plane];
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// output data of the current row to the column drivers
|
2008-12-03 05:40:16 +00:00
|
|
|
uint8_t tmp, tmp1;
|
|
|
|
#ifndef INTERLACED_ROWS
|
2011-08-17 01:16:25 +00:00
|
|
|
tmp = pixmap[plane][row][0];
|
2008-12-03 05:40:16 +00:00
|
|
|
tmp1 = pixmap[plane][row][1];
|
|
|
|
#else
|
|
|
|
row = (row>>1) + ((row & 0x01)?8:0 );
|
2011-08-17 01:16:25 +00:00
|
|
|
tmp = pixmap[plane][row][0];
|
2008-12-03 05:40:16 +00:00
|
|
|
tmp1 = pixmap[plane][row][1];
|
|
|
|
#endif
|
2009-01-01 17:06:38 +00:00
|
|
|
#ifdef REVERSE_COLS
|
2008-12-03 05:40:16 +00:00
|
|
|
tmp = (tmp >> 4) | (tmp << 4);
|
|
|
|
tmp = ((tmp & 0xcc) >> 2) | ((tmp & 0x33)<< 2); //0xcc = 11001100, 0x33 = 00110011
|
|
|
|
tmp = ((tmp & 0xaa) >> 1) | ((tmp & 0x55)<< 1); //0xaa = 10101010, 0x55 = 1010101
|
|
|
|
COLPORT2 = tmp;
|
|
|
|
tmp = tmp1;
|
|
|
|
tmp = (tmp >> 4) | (tmp << 4);
|
|
|
|
tmp = ((tmp & 0xcc) >> 2) | ((tmp & 0x33) << 2); //0xcc = 11001100, 0x33 = 00110011
|
|
|
|
tmp = ((tmp & 0xaa) >> 1) | ((tmp & 0x55) << 1); //0xaa = 10101010, 0x55 = 1010101
|
|
|
|
COLPORT1 = tmp;
|
|
|
|
#else
|
|
|
|
#ifdef INTERLACED_COLS
|
|
|
|
static uint8_t interlace_table[16] = {
|
2011-08-17 01:16:25 +00:00
|
|
|
0x00, 0x01, 0x04, 0x05, 0x10, 0x11, 0x14, 0x15, 0x40, 0x41, 0x44, 0x45,
|
|
|
|
0x50, 0x51, 0x54, 0x55
|
2008-12-03 05:40:16 +00:00
|
|
|
};
|
2011-08-17 01:16:25 +00:00
|
|
|
COLPORT1 = interlace_table[tmp&0x0f] | (interlace_table[tmp1&0x0f]<<1);
|
2008-12-03 05:40:16 +00:00
|
|
|
tmp>>=4; tmp1>>=4;
|
2011-08-17 01:16:25 +00:00
|
|
|
COLPORT2 = interlace_table[tmp] | (interlace_table[tmp1]<<1);
|
2008-12-03 05:40:16 +00:00
|
|
|
#else
|
|
|
|
COLPORT1 = tmp;
|
|
|
|
COLPORT2 = tmp1;
|
|
|
|
#endif
|
2012-02-12 20:47:55 +00:00
|
|
|
#endif
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// depending on the plane this interrupt triggers at 50 kHz, 31.25 kHz or
|
|
|
|
// 12.5 kHz
|
2012-03-23 01:27:59 +00:00
|
|
|
ISR(TIMER0_COMP_vect) {
|
2008-12-03 05:40:16 +00:00
|
|
|
static unsigned char plane = 0;
|
|
|
|
static unsigned char row = 0;
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// reset watchdog
|
2008-12-03 05:40:16 +00:00
|
|
|
wdt_reset();
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// increment both row and plane
|
|
|
|
if (++plane == NUMPLANE) {
|
|
|
|
plane = 0;
|
|
|
|
if (++row == NUM_ROWS) {
|
2008-12-03 05:40:16 +00:00
|
|
|
row = 0;
|
|
|
|
}
|
|
|
|
nextrow(row);
|
|
|
|
}
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// output current row according to current plane
|
2008-12-03 05:40:16 +00:00
|
|
|
rowshow(row, plane);
|
|
|
|
}
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
void timer0_off() {
|
2008-12-03 05:40:16 +00:00
|
|
|
cli();
|
|
|
|
|
|
|
|
COLPORT1 = 0;
|
|
|
|
COLPORT2 = 0;
|
|
|
|
ROWPORT = 0;
|
|
|
|
|
2012-02-12 20:47:55 +00:00
|
|
|
#if defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644__)
|
2011-08-17 01:16:25 +00:00
|
|
|
TCCR0A = 0x00;
|
|
|
|
TCCR0B = 0x00;
|
2010-01-29 03:05:49 +00:00
|
|
|
#else
|
2011-08-17 01:16:25 +00:00
|
|
|
|
2008-12-03 05:40:16 +00:00
|
|
|
TCCR0 = 0x00;
|
2010-01-29 03:05:49 +00:00
|
|
|
#endif
|
2008-12-03 05:40:16 +00:00
|
|
|
sei();
|
|
|
|
}
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// initialize timer which triggers the interrupt
|
|
|
|
static void timer0_on() {
|
|
|
|
/* TCCR0: FOC0 WGM00 COM01 COM00 WGM01 CS02 CS01 CS00
|
|
|
|
CS02 CS01 CS00
|
|
|
|
0 0 0 stop
|
|
|
|
0 0 1 clk
|
|
|
|
0 1 0 clk/8
|
|
|
|
0 1 1 clk/64
|
|
|
|
1 0 0 clk/256
|
|
|
|
1 0 1 clk/1024
|
|
|
|
*/
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2012-02-12 20:47:55 +00:00
|
|
|
#if defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644__)
|
2011-08-17 01:16:25 +00:00
|
|
|
TCCR0A = 0x02; // CTC Mode
|
|
|
|
TCCR0B = 0x04; // clk/256
|
|
|
|
TCNT0 = 0; // reset timer
|
|
|
|
OCR0 = 20; // compare with this value
|
|
|
|
TIMSK0 = 0x02; // compare match Interrupt on
|
2010-01-29 03:05:49 +00:00
|
|
|
#else
|
2011-08-17 01:16:25 +00:00
|
|
|
TCCR0 = 0x0C; // CTC Mode, clk/256
|
|
|
|
TCNT0 = 0; // reset timer
|
|
|
|
OCR0 = 20; // compare with this value
|
|
|
|
TIMSK = 0x02; // compare match Interrupt on
|
2010-01-29 03:05:49 +00:00
|
|
|
#endif
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
void borg_hw_init() {
|
|
|
|
// switch column ports to output mode
|
2008-12-03 05:40:16 +00:00
|
|
|
COLDDR1 = 0xFF;
|
|
|
|
COLDDR2 = 0xFF;
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// switch pins of the row port to output mode
|
2013-04-06 23:16:10 +00:00
|
|
|
ROWDDR = (1 << PIN_CLK) | (1 << PIN_DATA);
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// switch off all columns for now
|
2008-12-03 05:40:16 +00:00
|
|
|
COLPORT1 = 0;
|
|
|
|
COLPORT2 = 0;
|
2011-08-17 01:16:25 +00:00
|
|
|
|
|
|
|
// reset shift registers for the rows
|
2008-12-03 05:40:16 +00:00
|
|
|
ROWPORT = 0;
|
2011-08-17 01:16:25 +00:00
|
|
|
|
2008-12-03 05:40:16 +00:00
|
|
|
timer0_on();
|
|
|
|
|
2011-08-17 01:16:25 +00:00
|
|
|
// activate watchdog timer
|
2008-12-03 05:40:16 +00:00
|
|
|
wdt_reset();
|
2011-08-17 01:16:25 +00:00
|
|
|
wdt_enable(0x00); // 17ms watchdog
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|