summaryrefslogtreecommitdiff
path: root/rf433send/rf433send.c
blob: 6ec17098e3074a85abb510e50ee029b28820c1d2 (plain) (blame)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 *  spreadspace avr utils
 *
 *
 *  Copyright (C) 2013 Christian Pointner <equinox@spreadspace.org>
 *                     Othmar Gsenger <otti@gsenger.com>
 *
 *  This file is part of spreadspace avr utils.
 *
 *  spreadspace avr utils 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 3 of the License, or
 *  any later version.
 *
 *  spreadspace avr utils 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 spreadspace avr utils. If not, see <http://www.gnu.org/licenses/>.
 */


#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <stdio.h>

#include "util.h"
#include "led.h"
#include "usbio.h"

/*
  this program listens on a usb serial/acm interface. When the send command ('s') 
  is received the send buffer gets filled with the next 3 bytes. These bites are
  sent out serialized to a specified port using PWM. Before the data is sent, a
  sync bit is sent. The data is repeated multiple times.

  This version of the program is optimized for 433MHz radio controlled power plugs.
  To control them you need to add a ASK modulator to the output pin (default F0).
*/

// RF433 Sender is Connected to PF0
#define SEND_PORT PORTF
#define SEND_DDR DDRF
#define SEND_SERIAL 0
// Logical Zero (Sender turned off) is when output is low
#define SEND_ZERO 1
// rf433 command length is 3 bytes
#define SEND_CMD_LENGTH 3
// pwm is SEND_PWM_BASE timers high and SEND_PWN_LONG_MULT * SEND_PWM_BASE timers low for a low bit (low bit = low is longer)
#define SEND_PWM_BASE 1
#define SEND_PWN_LONG_MULT 3
#define SEND_PWN_SYNC_MULT 31

//bits are read LSB from first byte first
#define SEND_CURRENT_BIT (send_cmd_buffer[send_cmd_buffer_bit_pos/8] & (1<< (send_cmd_buffer_bit_pos % 8 )))

static char send_cmd_buffer[SEND_CMD_LENGTH];
static int8_t send_cmd_buffer_bit_pos=0;
static uint8_t send_pwm_remain=0;
static uint8_t send_pwm_output=0;
static uint8_t send_repeat=0;
static uint8_t send_sync=0;



static inline void sender_on(void)
{
  if(SEND_ZERO)
    SEND_PORT |= (1 << SEND_SERIAL);
  else  
    SEND_PORT &= ~(1 << SEND_SERIAL);
  send_pwm_output=1;
}

static inline void sender_off(void)
{
  if(SEND_ZERO)
    SEND_PORT &= ~(1 << SEND_SERIAL);
  else  
    SEND_PORT |= (1 << SEND_SERIAL);
  send_pwm_output=0;
}

inline void sender_toggle(void)
{
  SEND_PORT ^= (1 << SEND_SERIAL);
}

void sender_timer_enable(void)
{
    TCCR0A = 1<<WGM01;
    OCR0A = 22;        // (1+17)*16us = 352us
    TCCR0B = 1<<CS02 | 0<<CS01| 0<<CS00;   //Teiler 256
    TCNT0 = 0;
    TIMSK0 |= (1<<OCIE0A);
}

inline void sender_timer_disable(void)
{
    TCCR0B =0;
    TIMSK0 &= ~(1<<OCIE0A);
}

inline uint8_t sender_timer_status(void)
{
  return (TIMSK0 & (1<<OCIE0A))>>OCIE0A;
}

ISR(TIMER0_COMPA_vect)
{
  PORTF^=2; //debug;
  if(send_cmd_buffer_bit_pos < SEND_CMD_LENGTH * 8) {
    if(send_pwm_remain)
    {
      //NOTHING
    } else if (send_sync) {
      sender_off();
      send_pwm_remain=SEND_PWM_BASE*SEND_PWN_SYNC_MULT;
      send_sync=0;
      send_cmd_buffer_bit_pos=-1;
    } else if (send_pwm_output) {
      sender_off();
      if(SEND_CURRENT_BIT)
      {
        send_pwm_remain=SEND_PWM_BASE;
      } else {
        send_pwm_remain=SEND_PWM_BASE*SEND_PWN_LONG_MULT;
      }
    } else {
      send_cmd_buffer_bit_pos++;
      sender_on();
      if(SEND_CURRENT_BIT)
      {
        send_pwm_remain=SEND_PWM_BASE*SEND_PWN_LONG_MULT;
      } else {
        send_pwm_remain=SEND_PWM_BASE;
      }
    }
    send_pwm_remain--;
  } else {
    send_cmd_buffer_bit_pos=0;
    send_pwm_remain=0;
    if(send_repeat)
    {
      send_repeat--;
      send_sync=1;
      send_pwm_remain=SEND_PWM_BASE;
      sender_on();
    } else {
      sender_timer_disable();
      sender_off();
    }  
  }  
}

void pins_init(void)
{ 
    sender_off();
    SEND_DDR |= 1 << SEND_SERIAL;
    DDRF |= 2; //DEBUG
}

void timer_init(void)
{
}

static uint8_t command_pos=0;

void handle_cmd(uint8_t cmd)
{
  if (!command_pos)
  {
    switch(cmd) {
    case '0': led_off(); break;
    case '1': led_on(); break;
    case 't': led_toggle(); break;
    case 'r': reset2bootloader(); break;
    case 's': 
      printf("Expecting multibyte command now\n\r");
      command_pos=1;
      break;
    default: printf("error\n\r"); return;
    }
    printf("ok\n\r");
  } else {
    send_cmd_buffer[command_pos-1]=cmd;
    if (command_pos==SEND_CMD_LENGTH)
    {
      printf("Enabling Timer\n\r");
      command_pos=0;
      send_sync=1;
      send_pwm_remain=SEND_PWM_BASE;
      send_repeat=15;
      sender_on();
      sender_timer_enable();
    }
    else  
      command_pos++;
  }
}



int main(void)
{
  MCUSR &= ~(1 << WDRF);
  wdt_disable();

  cpu_init();
  led_init();
  usbio_init();
  pins_init();
  timer_init();
  sei();

  for(;;) {
    int16_t BytesReceived = usbio_bytes_received();
    while(BytesReceived > 0) {
      int16_t ReceivedByte = fgetc(stdin);
      if(ReceivedByte != EOF) {
        handle_cmd(ReceivedByte);
      }
      BytesReceived--;
    }

    usbio_task();
  }
}