My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
///////////////////////////////////////////////////////////////////////////////
// main.c - Home automation library
//
// Main firmware entry point for the attiny-based rf master controller which communicates
// with an arduino via as a i2c/twi slave at address 0x1.
//
// Copyright (C) 2009 Jochen Toppe <jochen@jtoee.com>. All right reserved.
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library 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
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
///////////////////////////////////////////////////////////////////////////////

//
// relay receiver hardware setup:
//
// 16MHz external clocked attiny2313
// RX: data out of rf receiver @ 1200 baud
// TX: data in of rf transmitter @ 1200 baud
//
// PORTB:
// PB7: SCL (i2c)
// PB5: SDA (i2c)
// PB4: cmd received status (OUT)
// PB3: Transmission circuit enable (OUT)

#ifdef TEST
#include <stdio.h>
#else
#include <avr/io.h>
#include <util/delay.h>
#endif

#include "../common/config.h"
#include "../common/uart.h"
#include "../common/commands.h"
#include "../common/protocol.h"
#include "../common/util.h"
#include "serial_master.h"

// twi to communicate with master arduino
#ifndef TEST
#include <../i2c/usiTwiSlave.h>
#include "util/twi.h"

// interrupts
#include <avr/interrupt.h>
#endif

///////////////////////////////////////////////////////////////////////
// common definitions
///////////////////////////////////////////////////////////////////////
// rf command receiver led (pb)
#define REC_STATUS_LED 4
// transmitter enable pin (pb)
#define TRANSMITTER_ENABLE 3
// twi slave id
#define TWI_ID 1

#ifndef TEST

///////////////////////////////////////////////////////////////////////
// enable/disable transmission circuits
///////////////////////////////////////////////////////////////////////

// turn transmitter on prior to calling the rf send
void pre_hook(void)
{
enable_bit(PORTB,TRANSMITTER_ENABLE);
// let the transmitter settle
_delay_ms(100);
}

// turn transmitter off after sending the rf command out
void post_hook(void)
{
// wait for it to be finished (really, this took me HOURS to f'in debug, I was
// turning the transmitter off before it was done)
_delay_ms(100);
// transmitter off
disable_bit(PORTB,TRANSMITTER_ENABLE);
}

///////////////////////////////////////////////////////////////////////
void twiDataRequested(void)
{
if(!usiTwiDataInTransmitBuffer()) {
// N for nothing, just so we return something (TWI freezes up on the
// master in a blocking read otherwise and a little "Hello, I'm here"
// is always a nice thing to do :-)
usiTwiTransmitByte(COMMAND_TYPE_NOOP);
}
}

///////////////////////////////////////////////////////////////////////
// handle rf commands
///////////////////////////////////////////////////////////////////////

// handle an incoming rf command
uint8_t _outBuffer[MAX_SERIAL_CMD_LEN+1];
void doHandleCommand(struct Command *aCmd)
{
// signal
enable_bit(PORTB,REC_STATUS_LED);

// transmit via wire back to master
uint8_t byte_count = serialize_command(aCmd, _outBuffer);
uint8_t i = 0;
for(; i<byte_count;i++) {
usiTwiTransmitByte(_outBuffer[i]);
}

// led off
_delay_ms(50);
disable_bit(PORTB,REC_STATUS_LED);
}

void init(void)
{
// register hooks
setPreSendHook(pre_hook);
setPostSendHook(post_hook);

// determine devide id to master device (0xF)
setDeviceId(MASTER_DEVICE_ID);

// set LED/tx enable as output
enable_bit(DDRB,TRANSMITTER_ENABLE);
enable_bit(DDRB,REC_STATUS_LED);

// initialize as slave
usiTwiSlaveInit(TWI_ID);

// twi callback when master requests data
_onTwiDataRequest = twiDataRequested;

// turn on uart for RF
uart_initialize(1,1);

// enable interrupts
sei();
}

//
// code for the relay receiver module.
//

int main(void)
{
init();

// handle commands
while (1)
{
// grab all bytes waiting in the twi rx buffer
while(usiTwiDataInReceiveBuffer())
{
// see if they resemble a command..
uint8_t bt = usiTwiReceiveByte();
parse_serial_byte(bt);
}
// check the uart for rf transmissions
if (uart_has_bytes())
{
uint8_t havePackage = parse_incoming(uart_receive());
if (havePackage)
{
// weed out stuff not for me when in slave mode
if(getDeviceId() == MASTER_DEVICE_ID || getDeviceId() == _incomingCommand.targetDevice)
{
// process command
doHandleCommand(&_incomingCommand);
}
}
}
}

return 0;
}

#else
// unit tests
// ////////////////

extern int commonSuite();
extern int masterSuite();
int main(void)
{
return (commonSuite() || masterSuite());
}

#endif

Change log

r117 by jochen.toppe on Jan 2, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r114 by jochen.toppe on Jan 2, 2010   Diff
[No log message]
r108 by jochen.toppe on Jan 1, 2010   Diff
damn, this is working
r106 by jochen.toppe on Jan 1, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 5529 bytes, 202 lines
Powered by Google Project Hosting