VirtualWire.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. // VirtualWire.cpp
  2. //
  3. // Virtual Wire implementation for Arduino
  4. // See the README file in this directory fdor documentation
  5. // See also
  6. // ASH Transceiver Software Designer's Guide of 2002.08.07
  7. // http://www.rfm.com/products/apnotes/tr_swg05.pdf
  8. //
  9. // Changes:
  10. // 1.5 2008-05-25: fixed a bug that could prevent messages with certain
  11. // bytes sequences being received (false message start detected)
  12. // 1.6 2011-09-10: Patch from David Bath to prevent unconditional reenabling of the receiver
  13. // at end of transmission.
  14. //
  15. // Author: Mike McCauley (mikem@airspayce.com)
  16. // Copyright (C) 2008 Mike McCauley
  17. // $Id: VirtualWire.cpp,v 1.9 2013/02/14 22:02:11 mikem Exp mikem $
  18. #if defined(ARDUINO)
  19. #if (ARDUINO < 100)
  20. #include "WProgram.h"
  21. #endif
  22. #elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific
  23. #include "legacymsp430.h"
  24. #include "Energia.h"
  25. #else // error
  26. #error Platform not defined
  27. #endif
  28. #include "VirtualWire.h"
  29. #include <stdint.h>
  30. #define lo8(x) ((x)&0xff)
  31. #define hi8(x) ((x)>>8)
  32. uint16_t crc16_update(uint16_t crc, uint8_t a)
  33. {
  34. int i;
  35. crc ^= a;
  36. for (i = 0; i < 8; ++i)
  37. {
  38. if (crc & 1)
  39. crc = (crc >> 1) ^ 0xA001;
  40. else
  41. crc = (crc >> 1);
  42. }
  43. return crc;
  44. }
  45. uint16_t crc_xmodem_update (uint16_t crc, uint8_t data)
  46. {
  47. int i;
  48. crc = crc ^ ((uint16_t)data << 8);
  49. for (i=0; i<8; i++)
  50. {
  51. if (crc & 0x8000)
  52. crc = (crc << 1) ^ 0x1021;
  53. else
  54. crc <<= 1;
  55. }
  56. return crc;
  57. }
  58. uint16_t _crc_ccitt_update (uint16_t crc, uint8_t data)
  59. {
  60. data ^= lo8 (crc);
  61. data ^= data << 4;
  62. return ((((uint16_t)data << 8) | hi8 (crc)) ^ (uint8_t)(data >> 4)
  63. ^ ((uint16_t)data << 3));
  64. }
  65. uint8_t _crc_ibutton_update(uint8_t crc, uint8_t data)
  66. {
  67. uint8_t i;
  68. crc = crc ^ data;
  69. for (i = 0; i < 8; i++)
  70. {
  71. if (crc & 0x01)
  72. crc = (crc >> 1) ^ 0x8C;
  73. else
  74. crc >>= 1;
  75. }
  76. return crc;
  77. }
  78. static uint8_t vw_tx_buf[(VW_MAX_MESSAGE_LEN * 2) + VW_HEADER_LEN]
  79. = {0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x2a, 0x38, 0x2c};
  80. // Number of symbols in vw_tx_buf to be sent;
  81. static uint8_t vw_tx_len = 0;
  82. // Index of the next symbol to send. Ranges from 0 to vw_tx_len
  83. static uint8_t vw_tx_index = 0;
  84. // Bit number of next bit to send
  85. static uint8_t vw_tx_bit = 0;
  86. // Sample number for the transmitter. Runs 0 to 7 during one bit interval
  87. static uint8_t vw_tx_sample = 0;
  88. // Flag to indicated the transmitter is active
  89. static volatile uint8_t vw_tx_enabled = 0;
  90. // Total number of messages sent
  91. static uint16_t vw_tx_msg_count = 0;
  92. // The digital IO pin number of the press to talk, enables the transmitter hardware
  93. static uint8_t vw_ptt_pin = 10;
  94. static uint8_t vw_ptt_inverted = 0;
  95. // The digital IO pin number of the receiver data
  96. static uint8_t vw_rx_pin = 11;
  97. // The digital IO pin number of the transmitter data
  98. static uint8_t vw_tx_pin = 12;
  99. // Current receiver sample
  100. static uint8_t vw_rx_sample = 0;
  101. // Last receiver sample
  102. static uint8_t vw_rx_last_sample = 0;
  103. // PLL ramp, varies between 0 and VW_RX_RAMP_LEN-1 (159) over
  104. // VW_RX_SAMPLES_PER_BIT (8) samples per nominal bit time.
  105. // When the PLL is synchronised, bit transitions happen at about the
  106. // 0 mark.
  107. static uint8_t vw_rx_pll_ramp = 0;
  108. // This is the integrate and dump integral. If there are <5 0 samples in the PLL cycle
  109. // the bit is declared a 0, else a 1
  110. static uint8_t vw_rx_integrator = 0;
  111. // Flag indictate if we have seen the start symbol of a new message and are
  112. // in the processes of reading and decoding it
  113. static uint8_t vw_rx_active = 0;
  114. // Flag to indicate that a new message is available
  115. static volatile uint8_t vw_rx_done = 0;
  116. // Flag to indicate the receiver PLL is to run
  117. static uint8_t vw_rx_enabled = 0;
  118. // Last 12 bits received, so we can look for the start symbol
  119. static uint16_t vw_rx_bits = 0;
  120. // How many bits of message we have received. Ranges from 0 to 12
  121. static uint8_t vw_rx_bit_count = 0;
  122. // The incoming message buffer
  123. static uint8_t vw_rx_buf[VW_MAX_MESSAGE_LEN];
  124. // The incoming message expected length
  125. static uint8_t vw_rx_count = 0;
  126. // The incoming message buffer length received so far
  127. static volatile uint8_t vw_rx_len = 0;
  128. // Number of bad messages received and dropped due to bad lengths
  129. static uint8_t vw_rx_bad = 0;
  130. // Number of good messages received
  131. static uint8_t vw_rx_good = 0;
  132. // 4 bit to 6 bit symbol converter table
  133. // Used to convert the high and low nybbles of the transmitted data
  134. // into 6 bit symbols for transmission. Each 6-bit symbol has 3 1s and 3 0s
  135. // with at most 3 consecutive identical bits
  136. static uint8_t symbols[] =
  137. {
  138. 0xd, 0xe, 0x13, 0x15, 0x16, 0x19, 0x1a, 0x1c,
  139. 0x23, 0x25, 0x26, 0x29, 0x2a, 0x2c, 0x32, 0x34
  140. };
  141. // Cant really do this as a real C++ class, since we need to have
  142. // Compute CRC over count bytes.
  143. // This should only be ever called at user level, not interrupt level
  144. uint16_t vw_crc(uint8_t *ptr, uint8_t count)
  145. {
  146. uint16_t crc = 0xffff;
  147. while (count-- > 0)
  148. crc = _crc_ccitt_update(crc, *ptr++);
  149. return crc;
  150. }
  151. // Convert a 6 bit encoded symbol into its 4 bit decoded equivalent
  152. uint8_t vw_symbol_6to4(uint8_t symbol)
  153. {
  154. uint8_t i;
  155. // Linear search :-( Could have a 64 byte reverse lookup table?
  156. for (i = 0; i < 16; i++)
  157. if (symbol == symbols[i]) return i;
  158. return 0; // Not found
  159. }
  160. // Set the output pin number for transmitter data
  161. void vw_set_tx_pin(uint8_t pin)
  162. {
  163. vw_tx_pin = pin;
  164. }
  165. // Set the pin number for input receiver data
  166. void vw_set_rx_pin(uint8_t pin)
  167. {
  168. vw_rx_pin = pin;
  169. }
  170. // Set the output pin number for transmitter PTT enable
  171. void vw_set_ptt_pin(uint8_t pin)
  172. {
  173. vw_ptt_pin = pin;
  174. }
  175. // Set the ptt pin inverted (low to transmit)
  176. void vw_set_ptt_inverted(uint8_t inverted)
  177. {
  178. vw_ptt_inverted = inverted;
  179. }
  180. // Called 8 times per bit period
  181. // Phase locked loop tries to synchronise with the transmitter so that bit
  182. // transitions occur at about the time vw_rx_pll_ramp is 0;
  183. // Then the average is computed over each bit period to deduce the bit value
  184. void vw_pll()
  185. {
  186. // Integrate each sample
  187. if (vw_rx_sample)
  188. vw_rx_integrator++;
  189. if (vw_rx_sample != vw_rx_last_sample)
  190. {
  191. // Transition, advance if ramp > 80, retard if < 80
  192. vw_rx_pll_ramp += ((vw_rx_pll_ramp < VW_RAMP_TRANSITION)
  193. ? VW_RAMP_INC_RETARD
  194. : VW_RAMP_INC_ADVANCE);
  195. vw_rx_last_sample = vw_rx_sample;
  196. }
  197. else
  198. {
  199. // No transition
  200. // Advance ramp by standard 20 (== 160/8 samples)
  201. vw_rx_pll_ramp += VW_RAMP_INC;
  202. }
  203. if (vw_rx_pll_ramp >= VW_RX_RAMP_LEN)
  204. {
  205. // Add this to the 12th bit of vw_rx_bits, LSB first
  206. // The last 12 bits are kept
  207. vw_rx_bits >>= 1;
  208. // Check the integrator to see how many samples in this cycle were high.
  209. // If < 5 out of 8, then its declared a 0 bit, else a 1;
  210. if (vw_rx_integrator >= 5)
  211. vw_rx_bits |= 0x800;
  212. vw_rx_pll_ramp -= VW_RX_RAMP_LEN;
  213. vw_rx_integrator = 0; // Clear the integral for the next cycle
  214. if (vw_rx_active)
  215. {
  216. // We have the start symbol and now we are collecting message bits,
  217. // 6 per symbol, each which has to be decoded to 4 bits
  218. if (++vw_rx_bit_count >= 12)
  219. {
  220. // Have 12 bits of encoded message == 1 byte encoded
  221. // Decode as 2 lots of 6 bits into 2 lots of 4 bits
  222. // The 6 lsbits are the high nybble
  223. uint8_t this_byte =
  224. (vw_symbol_6to4(vw_rx_bits & 0x3f)) << 4
  225. | vw_symbol_6to4(vw_rx_bits >> 6);
  226. // The first decoded byte is the byte count of the following message
  227. // the count includes the byte count and the 2 trailing FCS bytes
  228. // REVISIT: may also include the ACK flag at 0x40
  229. if (vw_rx_len == 0)
  230. {
  231. // The first byte is the byte count
  232. // Check it for sensibility. It cant be less than 4, since it
  233. // includes the bytes count itself and the 2 byte FCS
  234. vw_rx_count = this_byte;
  235. if (vw_rx_count < 4 || vw_rx_count > VW_MAX_MESSAGE_LEN)
  236. {
  237. // Stupid message length, drop the whole thing
  238. vw_rx_active = false;
  239. vw_rx_bad++;
  240. return;
  241. }
  242. }
  243. vw_rx_buf[vw_rx_len++] = this_byte;
  244. if (vw_rx_len >= vw_rx_count)
  245. {
  246. // Got all the bytes now
  247. vw_rx_active = false;
  248. vw_rx_good++;
  249. vw_rx_done = true; // Better come get it before the next one starts
  250. }
  251. vw_rx_bit_count = 0;
  252. }
  253. }
  254. // Not in a message, see if we have a start symbol
  255. else if (vw_rx_bits == 0xb38)
  256. {
  257. // Have start symbol, start collecting message
  258. vw_rx_active = true;
  259. vw_rx_bit_count = 0;
  260. vw_rx_len = 0;
  261. vw_rx_done = false; // Too bad if you missed the last message
  262. }
  263. }
  264. }
  265. // Common function for setting timer ticks @ prescaler values for speed
  266. // Returns prescaler index into {0, 1, 8, 64, 256, 1024} array
  267. // and sets nticks to compare-match value if lower than max_ticks
  268. // returns 0 & nticks = 0 on fault
  269. static uint8_t _timer_calc(uint16_t speed, uint16_t max_ticks, uint16_t *nticks)
  270. {
  271. // Clock divider (prescaler) values - 0/3333: error flag
  272. uint16_t prescalers[] = {0, 1, 8, 64, 256, 1024, 3333};
  273. uint8_t prescaler=0; // index into array & return bit value
  274. unsigned long ulticks; // calculate by ntick overflow
  275. // Div-by-zero protection
  276. if (speed == 0)
  277. {
  278. // signal fault
  279. *nticks = 0;
  280. return 0;
  281. }
  282. // test increasing prescaler (divisor), decreasing ulticks until no overflow
  283. for (prescaler=1; prescaler < 7; prescaler += 1)
  284. {
  285. // Amount of time per CPU clock tick (in seconds)
  286. float clock_time = (1.0 / (((float)F_CPU) / (float)(prescalers[prescaler])));
  287. // Fraction of second needed to xmit one bit
  288. float bit_time = ((1.0 / ((float)speed)) / 8.0);
  289. // number of prescaled ticks needed to handle bit time @ speed
  290. ulticks = (long)(bit_time / clock_time);
  291. // Test if ulticks fits in nticks bitwidth (with 1-tick safety margin)
  292. if ((ulticks > 1) && (ulticks < max_ticks))
  293. {
  294. break; // found prescaler
  295. }
  296. // Won't fit, check with next prescaler value
  297. }
  298. // Check for error
  299. if ((prescaler == 6) || (ulticks < 2) || (ulticks > max_ticks))
  300. {
  301. // signal fault
  302. *nticks = 0;
  303. return 0;
  304. }
  305. *nticks = ulticks;
  306. return prescaler;
  307. }
  308. #if defined(__arm__) && defined(CORE_TEENSY)
  309. // This allows the AVR interrupt code below to be run from an
  310. // IntervalTimer object. It must be above vw_setup(), so the
  311. // the TIMER1_COMPA_vect function name is defined.
  312. #ifdef SIGNAL
  313. #undef SIGNAL
  314. #endif
  315. #define SIGNAL(f) void f(void)
  316. #ifdef TIMER1_COMPA_vect
  317. #undef TIMER1_COMPA_vect
  318. #endif
  319. void TIMER1_COMPA_vect(void);
  320. #endif
  321. // Speed is in bits per sec RF rate
  322. #if defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific
  323. void vw_setup(uint16_t speed)
  324. {
  325. // Calculate the counter overflow count based on the required bit speed
  326. // and CPU clock rate
  327. uint16_t ocr1a = (F_CPU / 8UL) / speed;
  328. // This code is for Energia/MSP430
  329. TA0CCR0 = ocr1a; // Ticks for 62,5 us
  330. TA0CTL = TASSEL_2 + MC_1; // SMCLK, up mode
  331. TA0CCTL0 |= CCIE; // CCR0 interrupt enabled
  332. // Set up digital IO pins
  333. pinMode(vw_tx_pin, OUTPUT);
  334. pinMode(vw_rx_pin, INPUT);
  335. pinMode(vw_ptt_pin, OUTPUT);
  336. digitalWrite(vw_ptt_pin, vw_ptt_inverted);
  337. }
  338. #elif defined (ARDUINO) // Arduino specific
  339. void vw_setup(uint16_t speed)
  340. {
  341. uint16_t nticks; // number of prescaled ticks needed
  342. uint8_t prescaler; // Bit values for CS0[2:0]
  343. #ifdef __AVR_ATtiny85__
  344. // figure out prescaler value and counter match value
  345. prescaler = _timer_calc(speed, (uint8_t)-1, &nticks);
  346. if (!prescaler)
  347. {
  348. return; // fault
  349. }
  350. TCCR0A = 0;
  351. TCCR0A = _BV(WGM01); // Turn on CTC mode / Output Compare pins disconnected
  352. // convert prescaler index to TCCRnB prescaler bits CS00, CS01, CS02
  353. TCCR0B = 0;
  354. TCCR0B = prescaler; // set CS00, CS01, CS02 (other bits not needed)
  355. // Number of ticks to count before firing interrupt
  356. OCR0A = (uint8_t)nticks;
  357. // Set mask to fire interrupt when OCF0A bit is set in TIFR0
  358. TIMSK |= _BV(OCIE0A);
  359. #elif defined(__arm__) && defined(CORE_TEENSY)
  360. // on Teensy 3.0 (32 bit ARM), use an interval timer
  361. IntervalTimer *t = new IntervalTimer();
  362. t->begin(TIMER1_COMPA_vect, 125000.0 / (float)(speed));
  363. #else // ARDUINO
  364. // This is the path for most Arduinos
  365. // figure out prescaler value and counter match value
  366. prescaler = _timer_calc(speed, (uint16_t)-1, &nticks);
  367. if (!prescaler)
  368. {
  369. return; // fault
  370. }
  371. TCCR1A = 0; // Output Compare pins disconnected
  372. TCCR1B = _BV(WGM12); // Turn on CTC mode
  373. // convert prescaler index to TCCRnB prescaler bits CS10, CS11, CS12
  374. TCCR1B |= prescaler;
  375. // Caution: special procedures for setting 16 bit regs
  376. // is handled by the compiler
  377. OCR1A = nticks;
  378. // Enable interrupt
  379. #ifdef TIMSK1
  380. // atmega168
  381. TIMSK1 |= _BV(OCIE1A);
  382. #else
  383. // others
  384. TIMSK |= _BV(OCIE1A);
  385. #endif // TIMSK1
  386. #endif // __AVR_ATtiny85__
  387. // Set up digital IO pins
  388. pinMode(vw_tx_pin, OUTPUT);
  389. pinMode(vw_rx_pin, INPUT);
  390. pinMode(vw_ptt_pin, OUTPUT);
  391. digitalWrite(vw_ptt_pin, vw_ptt_inverted);
  392. }
  393. #endif // ARDUINO
  394. // Start the transmitter, call when the tx buffer is ready to go and vw_tx_len is
  395. // set to the total number of symbols to send
  396. void vw_tx_start()
  397. {
  398. vw_tx_index = 0;
  399. vw_tx_bit = 0;
  400. vw_tx_sample = 0;
  401. // Enable the transmitter hardware
  402. digitalWrite(vw_ptt_pin, true ^ vw_ptt_inverted);
  403. // Next tick interrupt will send the first bit
  404. vw_tx_enabled = true;
  405. }
  406. // Stop the transmitter, call when all bits are sent
  407. void vw_tx_stop()
  408. {
  409. // Disable the transmitter hardware
  410. digitalWrite(vw_ptt_pin, false ^ vw_ptt_inverted);
  411. digitalWrite(vw_tx_pin, false);
  412. // No more ticks for the transmitter
  413. vw_tx_enabled = false;
  414. }
  415. // Enable the receiver. When a message becomes available, vw_rx_done flag
  416. // is set, and vw_wait_rx() will return.
  417. void vw_rx_start()
  418. {
  419. if (!vw_rx_enabled)
  420. {
  421. vw_rx_enabled = true;
  422. vw_rx_active = false; // Never restart a partial message
  423. }
  424. }
  425. // Disable the receiver
  426. void vw_rx_stop()
  427. {
  428. vw_rx_enabled = false;
  429. }
  430. // Return true if the transmitter is active
  431. uint8_t vx_tx_active()
  432. {
  433. return vw_tx_enabled;
  434. }
  435. // Wait for the transmitter to become available
  436. // Busy-wait loop until the ISR says the message has been sent
  437. void vw_wait_tx()
  438. {
  439. while (vw_tx_enabled)
  440. ;
  441. }
  442. // Wait for the receiver to get a message
  443. // Busy-wait loop until the ISR says a message is available
  444. // can then call vw_get_message()
  445. void vw_wait_rx()
  446. {
  447. while (!vw_rx_done)
  448. ;
  449. }
  450. // Wait at most max milliseconds for the receiver to receive a message
  451. // Return the truth of whether there is a message
  452. uint8_t vw_wait_rx_max(unsigned long milliseconds)
  453. {
  454. unsigned long start = millis();
  455. while (!vw_rx_done && ((millis() - start) < milliseconds))
  456. ;
  457. return vw_rx_done;
  458. }
  459. // Wait until transmitter is available and encode and queue the message
  460. // into vw_tx_buf
  461. // The message is raw bytes, with no packet structure imposed
  462. // It is transmitted preceded a byte count and followed by 2 FCS bytes
  463. uint8_t vw_send(uint8_t* buf, uint8_t len)
  464. {
  465. uint8_t i;
  466. uint8_t index = 0;
  467. uint16_t crc = 0xffff;
  468. uint8_t *p = vw_tx_buf + VW_HEADER_LEN; // start of the message area
  469. uint8_t count = len + 3; // Added byte count and FCS to get total number of bytes
  470. if (len > VW_MAX_PAYLOAD)
  471. return false;
  472. // Wait for transmitter to become available
  473. vw_wait_tx();
  474. // Encode the message length
  475. crc = _crc_ccitt_update(crc, count);
  476. p[index++] = symbols[count >> 4];
  477. p[index++] = symbols[count & 0xf];
  478. // Encode the message into 6 bit symbols. Each byte is converted into
  479. // 2 6-bit symbols, high nybble first, low nybble second
  480. for (i = 0; i < len; i++)
  481. {
  482. crc = _crc_ccitt_update(crc, buf[i]);
  483. p[index++] = symbols[buf[i] >> 4];
  484. p[index++] = symbols[buf[i] & 0xf];
  485. }
  486. // Append the fcs, 16 bits before encoding (4 6-bit symbols after encoding)
  487. // Caution: VW expects the _ones_complement_ of the CCITT CRC-16 as the FCS
  488. // VW sends FCS as low byte then hi byte
  489. crc = ~crc;
  490. p[index++] = symbols[(crc >> 4) & 0xf];
  491. p[index++] = symbols[crc & 0xf];
  492. p[index++] = symbols[(crc >> 12) & 0xf];
  493. p[index++] = symbols[(crc >> 8) & 0xf];
  494. // Total number of 6-bit symbols to send
  495. vw_tx_len = index + VW_HEADER_LEN;
  496. // Start the low level interrupt handler sending symbols
  497. vw_tx_start();
  498. return true;
  499. }
  500. // Return true if there is a message available
  501. uint8_t vw_have_message()
  502. {
  503. return vw_rx_done;
  504. }
  505. // Get the last message received (without byte count or FCS)
  506. // Copy at most *len bytes, set *len to the actual number copied
  507. // Return true if there is a message and the FCS is OK
  508. uint8_t vw_get_message(uint8_t* buf, uint8_t* len)
  509. {
  510. uint8_t rxlen;
  511. // Message available?
  512. if (!vw_rx_done)
  513. return false;
  514. // Wait until vw_rx_done is set before reading vw_rx_len
  515. // then remove bytecount and FCS
  516. rxlen = vw_rx_len - 3;
  517. // Copy message (good or bad)
  518. if (*len > rxlen)
  519. *len = rxlen;
  520. memcpy(buf, vw_rx_buf + 1, *len);
  521. vw_rx_done = false; // OK, got that message thanks
  522. // Check the FCS, return goodness
  523. return (vw_crc(vw_rx_buf, vw_rx_len) == 0xf0b8); // FCS OK?
  524. }
  525. // This is the interrupt service routine called when timer1 overflows
  526. // Its job is to output the next bit from the transmitter (every 8 calls)
  527. // and to call the PLL code if the receiver is enabled
  528. //ISR(SIG_OUTPUT_COMPARE1A)
  529. #if defined (ARDUINO) // Arduino specific
  530. #ifdef __AVR_ATtiny85__
  531. SIGNAL(TIM0_COMPA_vect)
  532. #else // Assume Arduino Uno (328p or similar)
  533. SIGNAL(TIMER1_COMPA_vect)
  534. #endif // __AVR_ATtiny85__
  535. {
  536. if (vw_rx_enabled && !vw_tx_enabled)
  537. vw_rx_sample = digitalRead(vw_rx_pin);
  538. // Do transmitter stuff first to reduce transmitter bit jitter due
  539. // to variable receiver processing
  540. if (vw_tx_enabled && vw_tx_sample++ == 0)
  541. {
  542. // Send next bit
  543. // Symbols are sent LSB first
  544. // Finished sending the whole message? (after waiting one bit period
  545. // since the last bit)
  546. if (vw_tx_index >= vw_tx_len)
  547. {
  548. vw_tx_stop();
  549. vw_tx_msg_count++;
  550. }
  551. else
  552. {
  553. digitalWrite(vw_tx_pin, vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++));
  554. if (vw_tx_bit >= 6)
  555. {
  556. vw_tx_bit = 0;
  557. vw_tx_index++;
  558. }
  559. }
  560. }
  561. if (vw_tx_sample > 7)
  562. vw_tx_sample = 0;
  563. if (vw_rx_enabled && !vw_tx_enabled)
  564. vw_pll();
  565. }
  566. #elif defined(__MSP430G2452__) || defined(__MSP430G2553__) // LaunchPad specific
  567. void vw_Int_Handler()
  568. {
  569. if (vw_rx_enabled && !vw_tx_enabled)
  570. vw_rx_sample = digitalRead(vw_rx_pin);
  571. // Do transmitter stuff first to reduce transmitter bit jitter due
  572. // to variable receiver processing
  573. if (vw_tx_enabled && vw_tx_sample++ == 0)
  574. {
  575. // Send next bit
  576. // Symbols are sent LSB first
  577. // Finished sending the whole message? (after waiting one bit period
  578. // since the last bit)
  579. if (vw_tx_index >= vw_tx_len)
  580. {
  581. vw_tx_stop();
  582. vw_tx_msg_count++;
  583. }
  584. else
  585. {
  586. digitalWrite(vw_tx_pin, vw_tx_buf[vw_tx_index] & (1 << vw_tx_bit++));
  587. if (vw_tx_bit >= 6)
  588. {
  589. vw_tx_bit = 0;
  590. vw_tx_index++;
  591. }
  592. }
  593. }
  594. if (vw_tx_sample > 7)
  595. vw_tx_sample = 0;
  596. if (vw_rx_enabled && !vw_tx_enabled)
  597. vw_pll();
  598. }
  599. interrupt(TIMER0_A0_VECTOR) Timer_A_int(void)
  600. {
  601. vw_Int_Handler();
  602. };
  603. #endif