-
Notifications
You must be signed in to change notification settings - Fork 0
/
com.c
174 lines (140 loc) · 2.68 KB
/
com.c
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
/*
* com_neu.c
*
* Created on: 19.03.2012
* Author: gabm
*/
#include "com.h"
unsigned char com_send_buffer[MAX_CHUNK_SIZE];
unsigned char com_receive_buffer[MAX_CHUNK_SIZE];
uint8_t com_send_buffer_size =0;
uint8_t com_receive_buffer_size =0;
void com_wait_low()
{
loop_until_bit_is_clear(BUS_PIN, COM_SCK_IN);
}
void com_wait_high()
{
loop_until_bit_is_set(BUS_PIN, COM_SCK_IN);
}
void com_send_ack()
{
cbi(BUS_PORT,COM_SCK_OUT);
com_wait_high();
sbi(BUS_PORT,COM_SCK_OUT);
}
void com_mark_active_and_wait_ack()
{
cbi(BUS_PORT,COM_SCK_OUT);
com_wait_low();
sbi(BUS_PORT, COM_SCK_OUT);
com_wait_high();
}
unsigned char com_get_command()
{
com_wait_low();
unsigned char cCommand = CMD_PIN & 0x03;
com_send_ack();
return cCommand;
}
void com_connect()
{
//SCK IN as input and pull up
sbi(BUS_PORT, COM_SCK_IN);
cbi(BUS_DDR, COM_SCK_IN);
//SCK out as output and high
sbi(BUS_PORT, COM_SCK_OUT);
sbi(BUS_DDR, COM_SCK_OUT);
//CMD as input and pull up
cbi(CMD_DDR, CMD_PIN_0);
sbi(CMD_PORT, CMD_PIN_0);
cbi(CMD_DDR, CMD_PIN_1);
sbi(CMD_PORT, CMD_PIN_1);
com_config(BUS_INPUT);
_delay_ms(10);
com_wait_low();
com_send_ack();
}
void com_disconnect()
{
BUS_DDR = 0x00;
BUS_PORT = 0xFF;
cbi(CMD_DDR, CMD_PIN_0);
sbi(CMD_PORT, CMD_PIN_0);
cbi(CMD_DDR, CMD_PIN_1);
sbi(CMD_PORT, CMD_PIN_1);
_delay_ms(10);
}
void com_config(char cDirection)
{
if (cDirection == BUS_OUTPUT)
{
BUS_DDR |= 0x0F;
BUS_PORT &= ~(0x0F);
} else {
BUS_DDR &= ~(0x0F);
BUS_PORT |= 0x0F;
}
}
void com_init()
{
DDRC = 0x00;
PORTC = 0xFF;
cbi(DDRB, PB1);
sbi(PORTB, PB1);
//initialize interrupt as high
sbi(COM_INT_PORT, COM_INT_PIN);
sbi(COM_INT_DDR, COM_INT_PIN);
}
void com_write_nibble(const unsigned char cData)
{
BUS_PORT = (BUS_PORT & 0xF0) | (0x0F & cData);
com_mark_active_and_wait_ack();
}
unsigned char com_read_nibble()
{
unsigned char cResult = 0x00;
com_wait_low();
cResult |= (0x0F & BUS_PIN);
com_send_ack();
return cResult;
}
void com_write_byte(const unsigned char cData)
{
com_write_nibble(cData >> 4);
com_write_nibble(cData);
}
unsigned char com_read_byte()
{
unsigned char cResult = 0x00;
cResult |= (com_read_nibble() << 4);
cResult |= com_read_nibble();
return cResult;
}
void com_send_chunk(uint8_t uiLen)
{
unsigned char* itr = com_send_buffer;
com_write_byte(uiLen);
for (uint8_t i =0; i<uiLen;i++)
{
com_write_byte(*itr);
itr++;
}
}
void com_receive_chunk( uint8_t* uiLen)
{
//init and read first byte
unsigned char* itr = com_receive_buffer;
*uiLen = com_read_byte();
//receive the proposed length of bytes
uint8_t i=0;
while(i < *uiLen)
{
*itr++ = com_read_byte();
i++;
}
}
char com_check()
{
return bit_is_set(PINB, PB1);
}