Skip to content

Commit

Permalink
litepix ready for attiny45 with 8mhz
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Nieß authored and Michael Nieß committed Apr 8, 2015
1 parent 658248e commit 98078c1
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 280 deletions.
6 changes: 3 additions & 3 deletions main/arch-avr.mk
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
# notice and this notice are preserved. This file is offered as-is,
# without any warranty.

MCU = atmega328p
DF_CPU = 16000000
AVRDUDE_FLAGS=-c usbasp
MCU = attiny45
DF_CPU = 8000000
AVRDUDE_FLAGS=-c avrisp -b 19200 -P /dev/ttyACM0

CC = avr-gcc
CFLAGS = -c -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(DF_CPU) -Os \
Expand Down
4 changes: 2 additions & 2 deletions main/core/avr/clock.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "../init.h"
#include "../clock.h"

#define TIMER_MAX 250
#define TIMER_MAX 125
#define TIMER_INC 4


Expand All @@ -40,7 +40,7 @@ void init_clock(void) {
TCCR0A |= (1 << WGM01); // CTC mode
TCCR0B |= (1 << CS02); // prescaler: 256
OCR0A = TIMER_MAX;
TIMSK0 |= (1 << OCIE0A);
TIMSK |= (1 << OCIE0A);
sei();
}

Expand Down
164 changes: 49 additions & 115 deletions main/core/avr/pix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* pix.c
* This file is part of litepix
*
* Copyright (C) 2014, 2015 - Florian Rommel
* Copyright (C) 2014, 2015 - Florian Rommel, Michael Nieß
*
* litepix 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 @@ -27,150 +27,84 @@

#define OUT_PORT (PORTB)
#define OUT_DR (DDRB)
#define OUT_PIN_1 (PB0)
#define OUT_PIN_2 (PB1)
#define OUT_PIN_3 (PB2)

#define START_1 (0)
#define START_2 (PIX_NUM_BYTES/3)
#define START_3 (PIX_NUM_BYTES/3*2)

#define OUT_PIN (PB4)

#define START (0)
uint8_t pix_canvas[PIX_NUM_BYTES] = {0};


static void rearrange(void) {
uint8_t *ps = &pix_canvas[60];
uint8_t *pe = &pix_canvas[120];

asm (
" ldi r18, 10\n\t"
" ldi r19, 6\n\t"

"rearr: ld r20, %a[ps]\n\t"
" ldd r21, %a[ps]+1\n\t"
" ldd r22, %a[ps]+2\n\t"
" ld r25, -%a[pe]\n\t"
" ld r24, -%a[pe]\n\t"
" ld r23, -%a[pe]\n\t"
" st %a[pe], r20\n\t"
" std %a[pe]+1, r21\n\t"
" std %a[pe]+2, r22\n\t"
" st %a[ps]+, r23\n\t"
" st %a[ps]+, r24\n\t"
" st %a[ps]+, r25\n\t"
" dec r18\n\t"
" brne rearr\n\t"
" subi r28,-90\n\t" //TODO
" sbci r29,-1\n\t"
" subi r30,-150\n\t" //TODO
" sbci r31,-1\n\t"
" ldi r18, 10\n\t"
" dec r19\n\t"
" brne rearr\n\t"
::
[ps] "y" (ps),
[pe] "z" (pe)
: "r18", "r19", "r20", "r21",
"r22", "r23", "r24", "r25"
);
//switch pixel 3 with 5
static void rearrange(void) {
uint8_t tmp1 = pix_canvas[9];
uint8_t tmp2 = pix_canvas[10];
uint8_t tmp3 = pix_canvas[11];
pix_canvas[9] = pix_canvas[15];
pix_canvas[10] = pix_canvas[16];
pix_canvas[11] = pix_canvas[17];
pix_canvas[15] = tmp1;
pix_canvas[16] = tmp2;
pix_canvas[17] = tmp3;
}


void init_pix(void) {
OUT_DR |= (1 << OUT_PIN_1);
OUT_DR |= (1 << OUT_PIN_2);
OUT_DR |= (1 << OUT_PIN_3);
OUT_PORT &= ~(1 << OUT_PIN_1);
OUT_PORT &= ~(1 << OUT_PIN_2);
OUT_PORT &= ~(1 << OUT_PIN_3);
OUT_DR |= (1 << OUT_PIN);
OUT_PORT &= ~(1 << OUT_PIN);
}


void pix_render(void) {
rearrange();

rearrange();
uint8_t tmp_sreg = SREG;
cli();

uint8_t *p1 = &pix_canvas[START_1];
uint8_t *p2 = &pix_canvas[START_2];
uint8_t *p3 = &pix_canvas[START_3];
uint8_t d1 = *p1++;
uint8_t d2 = *p2++;
uint8_t d3 = *p3++;
uint8_t *p = &pix_canvas[START];
uint8_t d = *p++;

const uint8_t mask = _BV(OUT_PIN_1) | _BV(OUT_PIN_2) | _BV(OUT_PIN_3);
const uint8_t mask = _BV(OUT_PIN);
const uint8_t low = OUT_PORT & (~mask);
const uint8_t high = OUT_PORT | mask;

uint8_t nbits = 7;
uint8_t tmp = low;
uint16_t nbytes = PIX_NUM_BYTES/3;
uint8_t nbytes = PIX_NUM_BYTES;

asm volatile(
"start: nop\n\t"
" nop\n\t"
" nop\n\t"
"set1: out %[ioport], %[portdown]\n\t"
" lsl %[data1]\n\t"
" brcc a1\n\t"
" sbr %[tmp], %[bit1]\n\t"
"a1: lsl %[data2]\n\t"
" brcc a2\n\t"
" sbr %[tmp], %[bit2]\n\t"
"a2: out %[ioport], %[portup]\n\t"
" lsl %[data3]\n\t"
" brcc a3\n\t"
" sbr %[tmp], %[bit3]\n\t"
"a3: nop\n\t"
" dec %[bitcount]\n\t"
"set0: out %[ioport], %[tmp] \n\t"
" mov %[tmp], %[portdown]\n\t"
" brne start\n\t"

" lsl %[data1]\n\t"
" brcc b1\n\t"
" sbr %[tmp], %[bit1]\n\t"
"b1: lsl %[data2]\n\t"
" out %[ioport], %[portdown]\n\t"
" brcc b2\n\t"
" sbr %[tmp], %[bit2]\n\t"
"b2: ld %[data1], %a[ptr1]+\n\t"
"set1: out %[ioport], %[portdown]\n\t"
" lsl %[data1]\n\t"
" brcc a1\n\t"
" sbr %[tmp], %[bit1]\n\t"
"a1: out %[ioport], %[portup]\n\t"
" nop\n\t"
" dec %[bitcount]\n\t"
"set0: out %[ioport], %[tmp]\n\t"
" mov %[tmp], %[portdown]\n\t"
" brne set1\n\t"
" nop\n\t"
"b1: out %[ioport], %[portdown]\n\t"
" lsl %[data1]\n\t"
" brcc b2\n\t"
" sbr %[tmp], %[bit1]\n\t"
"b2: ld %[data1], %a[ptr1]+\n\t"
// ^^^
" ld %[data2], %a[ptr2]+\n\t"
// ^^^
" out %[ioport], %[portup]\n\t"
" lsl %[data3]\n\t"
" brcc b3\n\t"
" sbr %[tmp], %[bit3]\n\t"
"b3: ld %[data3], %a[ptr3]+\n\t"
// ^^^
" out %[ioport], %[tmp] \n\t"
" ldi %[bitcount], 7\n\t"
" nop\n\t"
" out %[ioport], %[portup]\n\t"
" nop\n\t"
" nop\n\t"
"b3: ldi %[bitcount], 7\n\t"
" out %[ioport], %[tmp]\n\t"
" mov %[tmp], %[portdown]\n\t"
" sbiw %[bytecount], 1\n\t"
// ^^^
" brne set1\n\t"

" nop\n\t"
" subi %[bytecount], 1\n\t"
" brne set1\n\t"
" out %[ioport], %[portdown]\n\t"
::
[ioport] "I" (_SFR_IO_ADDR(OUT_PORT)),
[portup] "l" (high),
[portdown] "l" (low),
[bitcount] "d" (nbits),
[ptr1] "e" (p1),
[ptr2] "e" (p2),
[ptr3] "e" (p3),
[data1] "d" (d1),
[data2] "d" (d2),
[data3] "d" (d3),
[bit1] "I" (1 << OUT_PIN_1),
[bit2] "I" (1 << OUT_PIN_2),
[bit3] "I" (1 << OUT_PIN_3),
[ptr1] "e" (p),
[data1] "d" (d),
[bit1] "I" (1 << OUT_PIN),
[tmp] "d" (tmp),
[bytecount] "w" (nbytes)
[bytecount] "d" (nbytes)
);

SREG = tmp_sreg;
Expand Down
6 changes: 3 additions & 3 deletions main/core/pix.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
#include <stdint.h>


#define PIX_NUM_PIXELS (240)
#define PIX_WIDTH (20)
#define PIX_HEIGHT (12)
#define PIX_NUM_PIXELS (9)
#define PIX_WIDTH (3)
#define PIX_HEIGHT (3)

#define PIX_NUM_BYTES (PIX_NUM_PIXELS*3)

Expand Down
17 changes: 8 additions & 9 deletions main/core/sim/pix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* pix.c
* This file is part of litepix
*
* Copyright (C) 2015 - Florian Rommel
* Copyright (C) 2015 - Florian Rommel, Michael Nieß
*
* litepix is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -34,27 +34,26 @@ static uint8_t save_canvas[PIX_NUM_BYTES] = {0};
static GMutex save_canvas_mutex;

static GtkWidget *window;

const int size = 100;
const int offset = 10;

static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer data) {
const int offset = 10;

cairo_set_source_rgb(cr, 0, 0, 0);
cairo_rectangle(cr, 0, 0, 620, 380);
cairo_rectangle(cr, 0, 0, (size*PIX_HEIGHT)+2*offset, (size*PIX_WIDTH)+2*offset);
cairo_fill(cr);

g_mutex_lock(&save_canvas_mutex);
uint8_t *p = save_canvas;
int i, j;
for (i = 0; i < 12; i++) {
for (j = 0; j < 20; j++) {
for (i = 0; i < PIX_WIDTH; i++) {
for (j = 0; j < PIX_HEIGHT; j++) {
uint8_t g = *p++;
uint8_t r = *p++;
uint8_t b = *p++;
cairo_set_source_rgb(cr, ((double)r)/256,
((double)g)/256,
((double)b)/256);
cairo_rectangle(cr, j*30+offset, i*30+offset, 28, 28);
cairo_rectangle(cr, j*size+offset, i*size+offset, size-2, size-2);
cairo_fill(cr);
}
}
Expand Down Expand Up @@ -90,7 +89,7 @@ void init_pix(void) {
G_CALLBACK(on_window_destroy), NULL);

gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 620, 380);
gtk_window_set_default_size(GTK_WINDOW(window), (size*PIX_HEIGHT)+2*offset, (size*PIX_WIDTH)+2*offset);
gtk_window_set_title(GTK_WINDOW(window), "Simulator");

gtk_widget_show_all(window);
Expand Down
55 changes: 55 additions & 0 deletions main/util/setter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* setter.c
* This file is part of litepix
*
* Copyright (C) 2015 - Michael Nieß
*
* litepix 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.
*
* litepix 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 litepix. If not, see <http://www.gnu.org/licenses/>.
*/


#include <stdlib.h>
#include <stdbool.h>
#include "setter.h"
#include "../core/pix.h"

//Set a pixels color
void set_pixel(uint8_t* canvas, uint8_t index, uint8_t color[3]){
uint8_t i = index * 3;
canvas[i++] = color[1];
canvas[i++] = color[0];
canvas[i] = color[2];
}

//Set the whole canvas to one color
void set_full(uint8_t* canvas, uint8_t color[3]) {
uint8_t i;
uint8_t* p = canvas;
for (i = 0; i < PIX_NUM_PIXELS; i++) {
*p++ = color[1];
*p++ = color[0];
*p++ = color[2];
}
}
//Set random colors in whole canvas
void set_random(uint8_t* canvas){
uint8_t i;
uint8_t* p = canvas;
for (i = 0; i < PIX_NUM_PIXELS; i++) {
*p++ = rand() % 256;
*p++ = rand() % 256;
*p++ = rand() % 256;
}
}

19 changes: 7 additions & 12 deletions main/core/input.h → main/util/setter.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/*
* input.h
* setter.h
* This file is part of litepix
*
* Copyright (C) 2015 - Florian Rommel
* Copyright (C) 2015 - Michael Nieß
*
* litepix 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 @@ -19,20 +19,15 @@
*/


#ifndef _CORE__INPUT_H_
#define _CORE__INPUT_H_
#ifndef _UTIL__SETTER_H_
#define _UTIL__SETTER_H_

#include <stdint.h>
#include <stdbool.h>

void set_pixel(uint8_t* pic, uint8_t index, uint8_t color[3]);

typedef struct {
uint8_t dev;
uint8_t data;
} t_input;


bool input_test(t_input* input);
void set_full(uint8_t* pic, uint8_t color[3]);

void set_random(uint8_t* pic);

#endif
Loading

0 comments on commit 98078c1

Please sign in to comment.