summaryrefslogtreecommitdiff
path: root/main.c
blob: 64b811d8cafac1de36abc17d7176aed78372159e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <avr/io.h>
#include <avr/delay.h>

static char buffer[300];

#define OUT0 "sbi %2, 5 \n"  /* pull line to GND */
#define OUT1 "cbi %2, 5 \n"  /* tristate the line */

#define WAIT10 "ldi r16, 1 \n rcall delay_loop \n"
#define WAIT34 "ldi r16, 9 \n rcall delay_loop \n"
#define WAIT10MINUS10 ""
#define WAIT34MINUS10 "ldi r16,5 \n rcall delay_loop \n nop \n nop \n"


int send_recv_gc(char* bytes, int len)
{
	char* buf_ptr = buffer;

	/****** SEND PART ******/

	int k=0;
	for (int i=0; i<len; i++)
		for (int j=0x80; j!=0; j=j>>1, k++)
			buffer[k] = ((bytes[i] & j)!=0)?1:0 ;
	
	len=len*8+1;
	
	asm volatile(
		"push r31   ; save Z\n"
		"push r30 \n\n"

		";;;;;;;; SEND PART ;;;;;;;;\n\n"

		"send_loop:   \n"
		"sbiw %3, 1 \n" // 2
		"breq send_done \n" // 1 if not done, 2 if done
		"ld r16, z+ \n" // 2
		"tst r16 \n" // 1
		"brne send_one \n\n" // 1 if zero, 2 if nonzero

		"; otherwise, send zero \n"
		"nop \n" // 1
		OUT0
		WAIT34
		OUT1
		WAIT10MINUS10
		"rjmp send_loop \n\n" // 2

		"send_one: \n"
		OUT0
		WAIT10
		OUT1
		WAIT34MINUS10
		"rjmp send_loop \n\n"

		"delay_loop:\n"
		"; this costs 7 + 3*(r16) cycles \n"
		"dec r16\n"
		"brne delay_loop\n"
		"ret\n\n"

		"send_done: \n"
		"; now send the stop bit and release the line \n"
		"nop \n nop \n nop \n"
		OUT0
		"; instead of WAIT10, do sensible work \n"
		"pop r30    ; restore Z \n"          // 2
		"pop r31 \n"                         // 2
		"clr r16 \n"                         // 1
		"nop \n nop \n nop \n nop \n nop \n" // 5
		OUT1
		"; done :) \n\n\n"


		"; now the final thing is to wait for DATA become high again (should be immediately anyway) \n"
		"send_final_loop: \n"
		"inc r16 \n"
		"breq timeout \n"
		"sbis %1, 5 \n"
		"rjmp send_final_loop \n\n\n"



		";;;;;;;; RECEIVE PART ;;;;;;;;\n\n"


		"clr r16 \n"
		"recv_wait_low_initial_loop: \n"
		"inc r16 \n"
		"breq timeout \n"
		"sbic %1, 5 \n"
		"rjmp recv_wait_low_initial_loop \n" // from low to the start of counting: 6 cycles = 0.5us
		"nop \n"
		"nop \n"
		"nop \n"
		"nop \n"


		"recv_loop: \n"
		"recv_low_begin: \n"
		"ldi r16, 128 \n" // 1
		"recv_low_loop: \n"
		"inc r16 \n"      // 1
		"breq timeout \n" // 1 if no timeout
		"sbis %1, 5 \n"   // 1               // von high auf dec: 6
		"rjmp recv_low_loop \n\n" // 2 if executed, 1 if skipped.
		"nop \n"          // to account for the rjmp recv_loop below.
		"nop \n"
		"nop \n"
		"nop \n"

		"recv_high_begin: \n"
		"nop \n" // to account for the ldi in recv_low_begin.
		"recv_high_loop: \n"
		"dec r16 \n" // 1
		"breq timeout \n" // 1 if no timeout
		"sbic %1, 5 \n" // 1                 // von low auf inc: 6
		"rjmp recv_high_loop \n\n" // 2 if executed, 1 if skipped

		"st z+, r16 \n" // 2
		"rjmp recv_loop \n\n" // 2

		"timeout: \n"
		:	"+z" ((unsigned char volatile*) buf_ptr)
		:	"I" (_SFR_IO_ADDR(PINC)), 
			"I" (_SFR_IO_ADDR(DDRC)),
			"w" (len)
		: "r16", "memory"
		);

		return buf_ptr-buffer;

		// a value of >=128 means "0", <127 means "1" bit.
}


int main (void)
{
	DDRC=0x00;
	PORTC=0x00;

	DDRB=0xFF;
	DDRD=0x00;
	PORTB=0x55;


	int temp=0;
	int n_received;

	while(1)
	{
		temp++;
		if (!(PIND & 0x08))
		{
			//PORTB=~(1 << ((temp>>9)%6));
			PORTB=~temp;
		}
		else if (!(PIND & 0x20))
		{
			PORTB=~n_received>>3;
		}
		else if (!(PIND & 0x40))
		{
			PORTB=~0x00;
		}
		else
		{
			//PORTB=~buffer[4];
			char tmp=0;
			for (int i=0;i<8;i++)
				tmp|= ( (buffer[i]&0x80)?0:(1<<i) );
			PORTB=~tmp;
		}
               
		_delay_ms(1);

	char foo[] = { 0x40, 0x03, 0x02 };

	n_received=send_recv_gc(foo, 3);


	}


	return 0; // never reached
}