to github
[svn42.git] / rf433rcv / teensy / example.c
index 635b16f..df393ab 100644 (file)
 #include <avr/interrupt.h>
 #include <util/delay.h>
 #include "usb_rawhid.h"
-#include "analog.h"
 
 #define CPU_PRESCALE(n)        (CLKPR = 0x80, CLKPR = (n))
+#define RF_SEND_BUFFER_LEN_MAX 502
 
-volatile uint8_t do_output=0;
-uint8_t buffer[64];
+union multiint {
+  uint16_t u16;
+  uint8_t u8[2];
+} __attribute__((packed));
 
-int main(void)
-{
-       int8_t r;
-       uint8_t i;
-       uint16_t val, count=0;
-
-       // set for 16 MHz clock
-       CPU_PRESCALE(0);
+volatile uint16_t output_count=0;
+volatile uint8_t active_buffer=0;
+volatile uint16_t send_buffer=0;
+volatile uint8_t capture=0;
+volatile uint16_t rf_send_buf_pos=0; //count of bits in rf_send_buffer that should be sent
+volatile union multiint rf_send_buf_len; //count of bits in rf_send_buffer that should be sent
+volatile uint8_t rf_send_count=0; // number of repetitions (times rf_send gets reloaded;
+uint8_t read_buffer[64]; // buffer for reading usb signals
+uint8_t write_buffer[2][64]; // buffer for writing usb signals
+uint8_t rf_send_buffer[RF_SEND_BUFFER_LEN_MAX]; // buffer for sending rf433 signals
+uint16_t rf_send_buf_offset=0;
 
-       // Initialize the USB, and then wait for the host to set configuration.
-       // If the Teensy is powered without a PC connected to the USB port,
-       // this will wait forever.
-       usb_init();
-       while (!usb_configured()) /* wait */ ;
 
-       // Wait an extra second for the PC's operating system to load drivers
-       // and do whatever it does to actually be ready for input
-       _delay_ms(1000);
+void reset()
+{
+  cli();
+  // disable watchdog, if enabled
+  // disable all peripherals
+  UDCON = 1;
+  USBCON = (1<<FRZCLK);  // disable USB
+  UCSR1B = 0;
+  _delay_ms(5);
+  #if defined(__AVR_AT90USB162__)                // Teensy 1.0
+      EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0;
+      TIMSK0 = 0; TIMSK1 = 0; UCSR1B = 0;
+      DDRB = 0; DDRC = 0; DDRD = 0;
+      PORTB = 0; PORTC = 0; PORTD = 0;
+      asm volatile("jmp 0x3E00");
+  #elif defined(__AVR_ATmega32U4__)              // Teensy 2.0
+      EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
+      TIMSK0 = 0; TIMSK1 = 0; TIMSK3 = 0; TIMSK4 = 0; UCSR1B = 0; TWCR = 0;
+      DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0; TWCR = 0;
+      PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
+      asm volatile("jmp 0x7E00");
+  #elif defined(__AVR_AT90USB646__)              // Teensy++ 1.0
+      EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
+      TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
+      DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
+      PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
+      asm volatile("jmp 0xFC00");
+  #elif defined(__AVR_AT90USB1286__)             // Teensy++ 2.0
+      EIMSK = 0; PCICR = 0; SPCR = 0; ACSR = 0; EECR = 0; ADCSRA = 0;
+      TIMSK0 = 0; TIMSK1 = 0; TIMSK2 = 0; TIMSK3 = 0; UCSR1B = 0; TWCR = 0;
+      DDRA = 0; DDRB = 0; DDRC = 0; DDRD = 0; DDRE = 0; DDRF = 0;
+      PORTA = 0; PORTB = 0; PORTC = 0; PORTD = 0; PORTE = 0; PORTF = 0;
+      asm volatile("jmp 0x1FC00");
+  #endif 
+}
 
-        // Configure timer 0 to generate a timer overflow interrupt every
-        // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
-        TCCR0A = 0x00;
-        TCCR0B = 0x05;
-        TIMSK0 = (1<<TOIE0);
+int main(void)
+{
+  // set for 16 MHz clock
+  CPU_PRESCALE(0);
+  DDRF|=3;
+  PORTF&=~1;
+  // Configure timer 0 to generate a timer overflow interrupt every
+  // 100*8 clock cycles, 50us
+  TCCR0A = 1<<WGM01;
+  TCCR0B = 1<<CS01;
+  OCR0A = 99;
+  TCNT0 = 0;
+  TIMSK0 = (1<<OCIE0A);
 
-       while (1) {
-               // if received data, do something with it
-               r = usb_rawhid_recv(buffer, 0);
-               if (r > 0) {
-                       // output 4 bits to D0, D1, D2, D3 pins
-                       DDRD = 0x0F;
-                       PORTD = (PORTD & 0xF0) | (buffer[0] & 0x0F);
-                       // ignore the other 63.5 bytes....
-               }
-               // if time to send output, transmit something interesting
-               if (do_output) {
-                       do_output = 0;
-                       // send a packet, first 2 bytes 0xABCD
-                       buffer[0] = 0xAB;
-                       buffer[1] = 0xCD;
-                       // put A/D measurements into next 24 bytes
-                       for (i=0; i<12; i++) {
-                               val = analogRead(i);
-                               buffer[i * 2 + 2] = val >> 8;
-                               buffer[i * 2 + 3] = val & 255;
-                       }
-                       // most of the packet filled with zero
-                       for (i=26; i<62; i++) {
-                               buffer[i] = 0;
-                       }
-                       // put a count in the last 2 bytes
-                       buffer[62] = count >> 8;
-                       buffer[63] = count & 255;
-                       // send the packet
-                       usb_rawhid_send(buffer, 50);
-                       count++;
-               }
-       }
+  // Initialize the USB, and then wait for the host to set configuration.
+  // If the Teensy is powered without a PC connected to the USB port,
+  // this will wait forever.
+  usb_init();
+  while (!usb_configured()) /* wait */ ;
+  // Wait an extra second for the PC's operating system to load drivers
+  // and do whatever it does to actually be ready for input
+  _delay_ms(1000);
+  while (1) {
+    // if received data, do something with it
+    int8_t r = usb_rawhid_recv(read_buffer, 0);
+    if (r>0)
+    {
+      if (read_buffer[0]=='r') //reset
+        reset();
+      else if (read_buffer[0]=='b') //begin capture
+        capture=1;
+      else if (read_buffer[0]=='e') //end capture
+        capture=0;
+      else if (read_buffer[0]=='f') //fill send buffer
+      {
+        int8_t buffer_pos = 1;
+        if(!rf_send_buf_offset) {
+          rf_send_buf_len.u8[1]=read_buffer[1];
+          rf_send_buf_len.u8[0]=read_buffer[2];
+          buffer_pos+=2;
+        }
+        while(buffer_pos < r && rf_send_buf_offset<RF_SEND_BUFFER_LEN_MAX)
+        {
+          rf_send_buffer[rf_send_buf_offset]=read_buffer[buffer_pos];
+          rf_send_buf_offset++;
+          buffer_pos++;
+        }
+      }
+      else if (read_buffer[0]=='c') // clear send buffer
+      {
+        rf_send_buf_offset=0;
+      }
+      else if (read_buffer[0]=='s') //send
+      {
+        //write_buffer[0][0]=rf_send_buf_len.u8[1];
+        //write_buffer[0][1]=rf_send_buf_len.u8[0];
+        //usb_rawhid_send(write_buffer, 23);
+        capture=0;
+        rf_send_buf_pos=0;
+        rf_send_count=read_buffer[1]+1;  
+      }
+    }
+    if (send_buffer)
+    {
+      send_buffer=0;
+      usb_rawhid_send(write_buffer[active_buffer?0:1], 23);
+    }
+  }
 }
 
 // This interrupt routine is run approx 61 times per second.
-ISR(TIMER0_OVF_vect)
+ISR(TIMER0_COMPA_vect)
 {
-       static uint8_t count=0;
-
-       // set the do_output variable every 2 seconds
-       if (++count > 122) {
-               count = 0;
-               do_output = 1;
-       }
+  PORTF^=2;
+  if (rf_send_count && rf_send_buf_pos<rf_send_buf_len.u16)
+  {
+    if ( rf_send_buffer[rf_send_buf_pos/8] & (1<< (rf_send_buf_pos%8)))
+    {
+     PORTF|=1;
+    } else {
+     PORTF&=~1;
+    }
+    //rf_send_buffer[rf_send/8]>>=1;
+    rf_send_buf_pos++;
+  } else if (rf_send_count) {
+    rf_send_buf_pos=0;
+    rf_send_count--;
+  } else {
+    PORTF&=~1;
+    if (capture) {
+      write_buffer[active_buffer][output_count/8]<<=1;
+      write_buffer[active_buffer][output_count++/8]|=PIND&1;
+      if (output_count>=64*8)
+      {
+        output_count=0;
+        active_buffer=active_buffer?0:1;
+        send_buffer=1;
+      }
+    }
+  }
 }
-
-
-