Skip to content

Commit 98078c1

Browse files
Michael NießMichael Nieß
Michael Nieß
authored and
Michael Nieß
committed
litepix ready for attiny45 with 8mhz
1 parent 658248e commit 98078c1

File tree

9 files changed

+154
-280
lines changed

9 files changed

+154
-280
lines changed

main/arch-avr.mk

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
# notice and this notice are preserved. This file is offered as-is,
44
# without any warranty.
55

6-
MCU = atmega328p
7-
DF_CPU = 16000000
8-
AVRDUDE_FLAGS=-c usbasp
6+
MCU = attiny45
7+
DF_CPU = 8000000
8+
AVRDUDE_FLAGS=-c avrisp -b 19200 -P /dev/ttyACM0
99

1010
CC = avr-gcc
1111
CFLAGS = -c -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(DF_CPU) -Os \

main/core/avr/clock.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "../init.h"
2525
#include "../clock.h"
2626

27-
#define TIMER_MAX 250
27+
#define TIMER_MAX 125
2828
#define TIMER_INC 4
2929

3030

@@ -40,7 +40,7 @@ void init_clock(void) {
4040
TCCR0A |= (1 << WGM01); // CTC mode
4141
TCCR0B |= (1 << CS02); // prescaler: 256
4242
OCR0A = TIMER_MAX;
43-
TIMSK0 |= (1 << OCIE0A);
43+
TIMSK |= (1 << OCIE0A);
4444
sei();
4545
}
4646

main/core/avr/pix.c

+49-115
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* pix.c
33
* This file is part of litepix
44
*
5-
* Copyright (C) 2014, 2015 - Florian Rommel
5+
* Copyright (C) 2014, 2015 - Florian Rommel, Michael Nieß
66
*
77
* litepix is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
@@ -27,150 +27,84 @@
2727

2828
#define OUT_PORT (PORTB)
2929
#define OUT_DR (DDRB)
30-
#define OUT_PIN_1 (PB0)
31-
#define OUT_PIN_2 (PB1)
32-
#define OUT_PIN_3 (PB2)
33-
34-
#define START_1 (0)
35-
#define START_2 (PIX_NUM_BYTES/3)
36-
#define START_3 (PIX_NUM_BYTES/3*2)
37-
30+
#define OUT_PIN (PB4)
3831

32+
#define START (0)
3933
uint8_t pix_canvas[PIX_NUM_BYTES] = {0};
4034

41-
42-
static void rearrange(void) {
43-
uint8_t *ps = &pix_canvas[60];
44-
uint8_t *pe = &pix_canvas[120];
45-
46-
asm (
47-
" ldi r18, 10\n\t"
48-
" ldi r19, 6\n\t"
49-
50-
"rearr: ld r20, %a[ps]\n\t"
51-
" ldd r21, %a[ps]+1\n\t"
52-
" ldd r22, %a[ps]+2\n\t"
53-
" ld r25, -%a[pe]\n\t"
54-
" ld r24, -%a[pe]\n\t"
55-
" ld r23, -%a[pe]\n\t"
56-
" st %a[pe], r20\n\t"
57-
" std %a[pe]+1, r21\n\t"
58-
" std %a[pe]+2, r22\n\t"
59-
" st %a[ps]+, r23\n\t"
60-
" st %a[ps]+, r24\n\t"
61-
" st %a[ps]+, r25\n\t"
62-
" dec r18\n\t"
63-
" brne rearr\n\t"
64-
" subi r28,-90\n\t" //TODO
65-
" sbci r29,-1\n\t"
66-
" subi r30,-150\n\t" //TODO
67-
" sbci r31,-1\n\t"
68-
" ldi r18, 10\n\t"
69-
" dec r19\n\t"
70-
" brne rearr\n\t"
71-
::
72-
[ps] "y" (ps),
73-
[pe] "z" (pe)
74-
: "r18", "r19", "r20", "r21",
75-
"r22", "r23", "r24", "r25"
76-
);
35+
//switch pixel 3 with 5
36+
static void rearrange(void) {
37+
uint8_t tmp1 = pix_canvas[9];
38+
uint8_t tmp2 = pix_canvas[10];
39+
uint8_t tmp3 = pix_canvas[11];
40+
pix_canvas[9] = pix_canvas[15];
41+
pix_canvas[10] = pix_canvas[16];
42+
pix_canvas[11] = pix_canvas[17];
43+
pix_canvas[15] = tmp1;
44+
pix_canvas[16] = tmp2;
45+
pix_canvas[17] = tmp3;
7746
}
7847

7948

8049
void init_pix(void) {
81-
OUT_DR |= (1 << OUT_PIN_1);
82-
OUT_DR |= (1 << OUT_PIN_2);
83-
OUT_DR |= (1 << OUT_PIN_3);
84-
OUT_PORT &= ~(1 << OUT_PIN_1);
85-
OUT_PORT &= ~(1 << OUT_PIN_2);
86-
OUT_PORT &= ~(1 << OUT_PIN_3);
50+
OUT_DR |= (1 << OUT_PIN);
51+
OUT_PORT &= ~(1 << OUT_PIN);
8752
}
8853

89-
9054
void pix_render(void) {
91-
rearrange();
92-
55+
rearrange();
9356
uint8_t tmp_sreg = SREG;
9457
cli();
9558

96-
uint8_t *p1 = &pix_canvas[START_1];
97-
uint8_t *p2 = &pix_canvas[START_2];
98-
uint8_t *p3 = &pix_canvas[START_3];
99-
uint8_t d1 = *p1++;
100-
uint8_t d2 = *p2++;
101-
uint8_t d3 = *p3++;
59+
uint8_t *p = &pix_canvas[START];
60+
uint8_t d = *p++;
10261

103-
const uint8_t mask = _BV(OUT_PIN_1) | _BV(OUT_PIN_2) | _BV(OUT_PIN_3);
62+
const uint8_t mask = _BV(OUT_PIN);
10463
const uint8_t low = OUT_PORT & (~mask);
10564
const uint8_t high = OUT_PORT | mask;
10665

10766
uint8_t nbits = 7;
10867
uint8_t tmp = low;
109-
uint16_t nbytes = PIX_NUM_BYTES/3;
68+
uint8_t nbytes = PIX_NUM_BYTES;
11069

11170
asm volatile(
112-
"start: nop\n\t"
113-
" nop\n\t"
114-
" nop\n\t"
115-
"set1: out %[ioport], %[portdown]\n\t"
116-
" lsl %[data1]\n\t"
117-
" brcc a1\n\t"
118-
" sbr %[tmp], %[bit1]\n\t"
119-
"a1: lsl %[data2]\n\t"
120-
" brcc a2\n\t"
121-
" sbr %[tmp], %[bit2]\n\t"
122-
"a2: out %[ioport], %[portup]\n\t"
123-
" lsl %[data3]\n\t"
124-
" brcc a3\n\t"
125-
" sbr %[tmp], %[bit3]\n\t"
126-
"a3: nop\n\t"
127-
" dec %[bitcount]\n\t"
128-
"set0: out %[ioport], %[tmp] \n\t"
129-
" mov %[tmp], %[portdown]\n\t"
130-
" brne start\n\t"
131-
132-
" lsl %[data1]\n\t"
133-
" brcc b1\n\t"
134-
" sbr %[tmp], %[bit1]\n\t"
135-
"b1: lsl %[data2]\n\t"
136-
" out %[ioport], %[portdown]\n\t"
137-
" brcc b2\n\t"
138-
" sbr %[tmp], %[bit2]\n\t"
139-
"b2: ld %[data1], %a[ptr1]+\n\t"
71+
"set1: out %[ioport], %[portdown]\n\t"
72+
" lsl %[data1]\n\t"
73+
" brcc a1\n\t"
74+
" sbr %[tmp], %[bit1]\n\t"
75+
"a1: out %[ioport], %[portup]\n\t"
76+
" nop\n\t"
77+
" dec %[bitcount]\n\t"
78+
"set0: out %[ioport], %[tmp]\n\t"
79+
" mov %[tmp], %[portdown]\n\t"
80+
" brne set1\n\t"
81+
" nop\n\t"
82+
"b1: out %[ioport], %[portdown]\n\t"
83+
" lsl %[data1]\n\t"
84+
" brcc b2\n\t"
85+
" sbr %[tmp], %[bit1]\n\t"
86+
"b2: ld %[data1], %a[ptr1]+\n\t"
14087
// ^^^
141-
" ld %[data2], %a[ptr2]+\n\t"
142-
// ^^^
143-
" out %[ioport], %[portup]\n\t"
144-
" lsl %[data3]\n\t"
145-
" brcc b3\n\t"
146-
" sbr %[tmp], %[bit3]\n\t"
147-
"b3: ld %[data3], %a[ptr3]+\n\t"
148-
// ^^^
149-
" out %[ioport], %[tmp] \n\t"
150-
" ldi %[bitcount], 7\n\t"
88+
" nop\n\t"
89+
" out %[ioport], %[portup]\n\t"
90+
" nop\n\t"
91+
" nop\n\t"
92+
"b3: ldi %[bitcount], 7\n\t"
93+
" out %[ioport], %[tmp]\n\t"
15194
" mov %[tmp], %[portdown]\n\t"
152-
" sbiw %[bytecount], 1\n\t"
153-
// ^^^
154-
" brne set1\n\t"
155-
156-
" nop\n\t"
95+
" subi %[bytecount], 1\n\t"
96+
" brne set1\n\t"
15797
" out %[ioport], %[portdown]\n\t"
15898
::
15999
[ioport] "I" (_SFR_IO_ADDR(OUT_PORT)),
160100
[portup] "l" (high),
161101
[portdown] "l" (low),
162102
[bitcount] "d" (nbits),
163-
[ptr1] "e" (p1),
164-
[ptr2] "e" (p2),
165-
[ptr3] "e" (p3),
166-
[data1] "d" (d1),
167-
[data2] "d" (d2),
168-
[data3] "d" (d3),
169-
[bit1] "I" (1 << OUT_PIN_1),
170-
[bit2] "I" (1 << OUT_PIN_2),
171-
[bit3] "I" (1 << OUT_PIN_3),
103+
[ptr1] "e" (p),
104+
[data1] "d" (d),
105+
[bit1] "I" (1 << OUT_PIN),
172106
[tmp] "d" (tmp),
173-
[bytecount] "w" (nbytes)
107+
[bytecount] "d" (nbytes)
174108
);
175109

176110
SREG = tmp_sreg;

main/core/pix.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#include <stdint.h>
2727

2828

29-
#define PIX_NUM_PIXELS (240)
30-
#define PIX_WIDTH (20)
31-
#define PIX_HEIGHT (12)
29+
#define PIX_NUM_PIXELS (9)
30+
#define PIX_WIDTH (3)
31+
#define PIX_HEIGHT (3)
3232

3333
#define PIX_NUM_BYTES (PIX_NUM_PIXELS*3)
3434

main/core/sim/pix.c

+8-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* pix.c
33
* This file is part of litepix
44
*
5-
* Copyright (C) 2015 - Florian Rommel
5+
* Copyright (C) 2015 - Florian Rommel, Michael Nieß
66
*
77
* litepix is free software; you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
@@ -34,27 +34,26 @@ static uint8_t save_canvas[PIX_NUM_BYTES] = {0};
3434
static GMutex save_canvas_mutex;
3535

3636
static GtkWidget *window;
37-
37+
const int size = 100;
38+
const int offset = 10;
3839

3940
static gboolean on_draw_event(GtkWidget *widget, cairo_t *cr, gpointer data) {
40-
const int offset = 10;
41-
4241
cairo_set_source_rgb(cr, 0, 0, 0);
43-
cairo_rectangle(cr, 0, 0, 620, 380);
42+
cairo_rectangle(cr, 0, 0, (size*PIX_HEIGHT)+2*offset, (size*PIX_WIDTH)+2*offset);
4443
cairo_fill(cr);
4544

4645
g_mutex_lock(&save_canvas_mutex);
4746
uint8_t *p = save_canvas;
4847
int i, j;
49-
for (i = 0; i < 12; i++) {
50-
for (j = 0; j < 20; j++) {
48+
for (i = 0; i < PIX_WIDTH; i++) {
49+
for (j = 0; j < PIX_HEIGHT; j++) {
5150
uint8_t g = *p++;
5251
uint8_t r = *p++;
5352
uint8_t b = *p++;
5453
cairo_set_source_rgb(cr, ((double)r)/256,
5554
((double)g)/256,
5655
((double)b)/256);
57-
cairo_rectangle(cr, j*30+offset, i*30+offset, 28, 28);
56+
cairo_rectangle(cr, j*size+offset, i*size+offset, size-2, size-2);
5857
cairo_fill(cr);
5958
}
6059
}
@@ -90,7 +89,7 @@ void init_pix(void) {
9089
G_CALLBACK(on_window_destroy), NULL);
9190

9291
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
93-
gtk_window_set_default_size(GTK_WINDOW(window), 620, 380);
92+
gtk_window_set_default_size(GTK_WINDOW(window), (size*PIX_HEIGHT)+2*offset, (size*PIX_WIDTH)+2*offset);
9493
gtk_window_set_title(GTK_WINDOW(window), "Simulator");
9594

9695
gtk_widget_show_all(window);

main/util/setter.c

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* setter.c
3+
* This file is part of litepix
4+
*
5+
* Copyright (C) 2015 - Michael Nieß
6+
*
7+
* litepix is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* litepix is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with litepix. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
22+
#include <stdlib.h>
23+
#include <stdbool.h>
24+
#include "setter.h"
25+
#include "../core/pix.h"
26+
27+
//Set a pixels color
28+
void set_pixel(uint8_t* canvas, uint8_t index, uint8_t color[3]){
29+
uint8_t i = index * 3;
30+
canvas[i++] = color[1];
31+
canvas[i++] = color[0];
32+
canvas[i] = color[2];
33+
}
34+
35+
//Set the whole canvas to one color
36+
void set_full(uint8_t* canvas, uint8_t color[3]) {
37+
uint8_t i;
38+
uint8_t* p = canvas;
39+
for (i = 0; i < PIX_NUM_PIXELS; i++) {
40+
*p++ = color[1];
41+
*p++ = color[0];
42+
*p++ = color[2];
43+
}
44+
}
45+
//Set random colors in whole canvas
46+
void set_random(uint8_t* canvas){
47+
uint8_t i;
48+
uint8_t* p = canvas;
49+
for (i = 0; i < PIX_NUM_PIXELS; i++) {
50+
*p++ = rand() % 256;
51+
*p++ = rand() % 256;
52+
*p++ = rand() % 256;
53+
}
54+
}
55+
+7-12
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/*
2-
* input.h
2+
* setter.h
33
* This file is part of litepix
44
*
5-
* Copyright (C) 2015 - Florian Rommel
5+
* Copyright (C) 2015 - Michael Nieß
66
*
77
* litepix is free software: you can redistribute it and/or modify
88
* it under the terms of the GNU General Public License as published by
@@ -19,20 +19,15 @@
1919
*/
2020

2121

22-
#ifndef _CORE__INPUT_H_
23-
#define _CORE__INPUT_H_
22+
#ifndef _UTIL__SETTER_H_
23+
#define _UTIL__SETTER_H_
2424

2525
#include <stdint.h>
26-
#include <stdbool.h>
2726

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

29-
typedef struct {
30-
uint8_t dev;
31-
uint8_t data;
32-
} t_input;
33-
34-
35-
bool input_test(t_input* input);
29+
void set_full(uint8_t* pic, uint8_t color[3]);
3630

31+
void set_random(uint8_t* pic);
3732

3833
#endif

0 commit comments

Comments
 (0)