X-Git-Url: https://git.realraum.at/?p=svn42.git;a=blobdiff_plain;f=rf433rcv%2Fteensy%2Fexample.c;h=df393abe7bce9fa37e628c37aad41c98c9765792;hp=635b16f613bc97f797305e92f0207f7c655b1aac;hb=5b00dab1a7d2ac2d8a593f0bd6e85a7194e2c9bd;hpb=deb2b278d460a716e0e2d8be370b1ee8f8963e34 diff --git a/rf433rcv/teensy/example.c b/rf433rcv/teensy/example.c index 635b16f..df393ab 100644 --- a/rf433rcv/teensy/example.c +++ b/rf433rcv/teensy/example.c @@ -26,84 +26,162 @@ #include #include #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< 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 122) { - count = 0; - do_output = 1; - } + PORTF^=2; + if (rf_send_count && rf_send_buf_pos>=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; + } + } + } } - - -