COSA
An Object-Oriented Platform for Arduino Programming
CC1101.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_CC1101_HH
22 #define COSA_CC1101_HH
23 
24 #include "Cosa/SPI.hh"
25 #include "Cosa/OutputPin.hh"
27 #include "Cosa/Wireless.hh"
28 
29 #if !defined(BOARD_ATTINYX5)
30 
59 class CC1101 : private SPI::Driver, public Wireless::Driver {
60 public:
64  static const size_t DEVICE_PAYLOAD_MAX = 64;
65 
72  static const size_t PAYLOAD_MAX = DEVICE_PAYLOAD_MAX - 4;
73 
84 #if defined(BOARD_ATTINYX4)
85  CC1101(uint16_t net, uint8_t dev,
88 #elif defined(BOARD_ATMEGA2560)
89  CC1101(uint16_t net, uint8_t dev,
92 #else
93  CC1101(uint16_t net, uint8_t dev,
96 #endif
97 
107  virtual bool begin(const void* config = NULL);
108 
115  virtual bool end();
116 
128  virtual int send(uint8_t dest, uint8_t port, const iovec_t* vec);
129 
143  virtual int send(uint8_t dest, uint8_t port, const void* buf, size_t len);
144 
160  virtual int recv(uint8_t& src, uint8_t& port, void* buf, size_t len,
161  uint32_t ms = 0L);
162 
167  virtual void powerdown();
168 
173  virtual void wakeup_on_radio();
174 
180  virtual void output_power_level(int8_t dBm);
181 
187  virtual int input_power_level();
188 
195  {
196  return (m_recv_status.lqi);
197  }
198 
199 protected:
204  union header_t {
205  uint8_t as_uint8;
206  struct {
207  uint8_t reg:6;
208  uint8_t burst:1;
209  uint8_t rw:1;
210  };
211 
219  header_t(uint8_t addr, uint8_t is_burst, uint8_t is_read)
220  {
221  reg = addr;
222  burst = is_burst;
223  rw = is_read;
224  }
225 
230  operator uint8_t()
231  {
232  return (as_uint8);
233  }
234  };
235 
242  uint8_t read(uint8_t reg)
243  {
244  m_status = spi.transfer(header_t(reg, 0, 1));
245  uint8_t res = spi.transfer(0);
246  return (res);
247  }
248 
256  void read(uint8_t reg, void* buf, size_t count)
257  {
258  m_status = spi.transfer(header_t(reg, 1, 1));
259  spi.read(buf, count);
260  }
261 
267  void write(uint8_t reg, uint8_t value)
268  {
269  m_status = spi.transfer(header_t(reg, 0, 0));
270  spi.transfer(value);
271  }
272 
280  void write(uint8_t reg, const void* buf, size_t count)
281  {
282  m_status = spi.transfer(header_t(reg, 1, 0));
283  spi.write(buf, count);
284  }
285 
293  void write_P(uint8_t reg, const uint8_t* buf, size_t count)
294  {
295  m_status = spi.transfer(header_t(reg, 1, 0));
296  spi.write_P(buf, count);
297  }
298 
302  enum Config {
303  IOCFG2 = 0x00,
304  IOCFG1 = 0x01,
305  IOCFG0 = 0x02,
306  FIFOTH = 0x03,
307  SYNC1 = 0x04,
308  SYNC0 = 0x05,
309  PKTLEN = 0x06,
310  PKTCTR = 0x07,
311  PKTCTRL0 = 0x08,
312  ADDR = 0x09,
313  CHANNR = 0x0A,
314  FSCTRL1 = 0x0B,
315  FSCTRL0 = 0x0C,
316  FREQ2 = 0x0D,
317  FREQ1 = 0x0E,
318  FREQ0 = 0x0F,
319  MDMCFG4 = 0x10,
320  MDMCFG3 = 0x11,
321  MDMCFG2 = 0x12,
322  MDMCFG1 = 0x13,
323  MDMCFG0 = 0x14,
324  DEVIATN = 0x15,
325  MCSM2 = 0x16,
326  MCSM1 = 0x17,
327  MCSM0 = 0x18,
328  FOCCFG = 0x19,
329  BSCFG = 0x1A,
330  AGCCTRL2 = 0x1B,
331  AGCCTRL1 = 0x1C,
332  AGCCTRL0 = 0x1D,
333  WOREVT1 = 0x1E,
334  WOREVT0 = 0x1F,
335  WORCTRL = 0x20,
336  FREND1 = 0x21,
337  FREND0 = 0x22,
338  FSCAL3 = 0x23,
339  FSCAL2 = 0x24,
340  FSCAL1 = 0x25,
341  FSCAL0 = 0x26,
342  RCCTRL1 = 0x27,
343  RCCTRL0 = 0x28,
344  FSTEST = 0x29,
345  PTEST = 0x2A,
346  AGCTEST = 0x2B,
347  TEST2 = 0x2C,
348  TEST1 = 0x2D,
349  TEST0 = 0x2E,
350  CONFIG_MAX = 0x29
351  } __attribute__((packed));
352 
358  uint8_t read(Config reg)
359  {
360  return (read((uint8_t) reg));
361  }
362 
370  void read(Config reg, void* buf, size_t count)
371  {
372  read((uint8_t) reg, buf, count);
373  }
374 
380  void write(Config reg, uint8_t value)
381  {
382  write((uint8_t) reg, value);
383  }
384 
391  void write(Config reg, const void* buf, size_t count)
392  {
393  write((uint8_t) reg, buf, count);
394  }
395 
403  void write_P(Config reg, const uint8_t* buf, size_t count)
404  {
405  write_P((uint8_t) reg, buf, count);
406  }
407 
411  enum Data {
412  PATABLE = 0x3E,
413  TXFIFO = 0x3F,
414  RXFIFO = 0x3F,
415  } __attribute__((packed));
416 
420  static const size_t PATABLE_MAX = 8;
421 
427  uint8_t read(Data reg)
428  {
429  return (read((uint8_t) reg));
430  }
431 
438  void read(Data reg, void* buf, size_t count)
439  {
440  read((uint8_t) reg, buf, count);
441  }
442 
448  void write(Data reg, uint8_t value)
449  {
450  write((uint8_t) reg, value);
451  }
452 
459  void write(Data reg, const void* buf, size_t count)
460  {
461  write((uint8_t) reg, buf, count);
462  }
463 
470  void write_P(Data reg, const uint8_t* buf, size_t count)
471  {
472  write_P((uint8_t) reg, buf, count);
473  }
474 
478  enum Status {
479  PARTNUM = 0x30,
480  VERSION = 0x31,
481  FREQEST = 0x32,
482  LQI = 0x33,
483  RSSI = 0x34,
484  MARCSTATE = 0x35,
485  WORTIME1 = 0x36,
486  WORTIME0 = 0x37,
487  PKTSTATUS = 0x38,
488  VCO = 0x39,
489  TXBYTES = 0x3A,
490  RXBYTES = 0x3B,
491  BYTES_MASK = 0x7f,
492  FIFO_MASK = 0x80,
493  RCCTRL1_STATUS = 0x3C,
494  RCCTRL0_STATUS = 0x3D,
495  STATUS_MAX = 0x0E,
496  } __attribute__((packed));
497 
503  uint8_t read(Status reg)
504  {
505  uint8_t res;
506  read((uint8_t) reg, &res, sizeof(res));
507  return (res);
508  }
509 
513  enum Command {
514  SRES = 0x30,
515  SFSTXON = 0x31,
516  SXOFF = 0x32,
517  SCAL = 0x33,
518  SRX = 0x34,
519  STX = 0x35,
520  SIDLE = 0x36,
521  SAFC = 0x37,
522  SWOR = 0x38,
523  SPWD = 0x39,
524  SFRX = 0x3A,
525  SFTX = 0x3B,
526  SWORRST = 0x3C,
527  SNOP = 0x3D
528  } __attribute__((packed));
529 
535  void strobe(Command cmd);
536 
540  enum Mode {
541  IDLE_MODE = 0,
549  } __attribute__((packed));
550 
551  union status_t {
552  uint8_t as_uint8;
553  struct {
554  uint8_t avail:4;
555  uint8_t mode:3;
556  uint8_t ready:1;
557  };
558 
559  status_t(uint8_t value)
560  {
561  as_uint8 = value;
562  }
563  };
564 
569  status_t status() const
570  {
571  return (m_status);
572  }
573 
579  status_t read_status(uint8_t fifo = 1)
580  {
581  spi.acquire(this);
582  spi.begin();
583  m_status = spi.transfer(header_t(0,0,fifo));
584  spi.end();
585  spi.release();
586  return (m_status);
587  }
588 
593  void await(Mode mode);
594 
598  enum State {
599  SLEEP_STATE = 0x00,
600  IDLE_STATE = 0x01,
601  XOFF_STATE = 0x02,
602  VCOON_MC_STATE = 0x03,
603  REGON_MC_STATE = 0x04,
604  MANCAL_STATE = 0x05,
605  VCOON_STATE = 0x06,
606  REGON_STATE = 0x07,
607  STARTCAL_STATE = 0x08,
608  BWBOOST_STATE = 0x09,
609  FS_LOCK_STATE = 0x0A,
610  IFADCON_STATE = 0x0B,
611  ENDCAL_STATE = 0x0C,
612  RX_STATE = 0x0D,
613  RX_END_STATE = 0x0E,
614  RX_RST_STATE = 0x0F,
617  FSTXON_STATE = 0x12,
618  TX_STATE = 0x13,
619  TX_END_STATE = 0x14,
622  } __attribute__((packed));
623 
629  {
630  return ((State) read(MARCSTATE));
631  }
632 
637  uint8_t status[2];
638  struct {
639  uint8_t rssi;
640  uint8_t lqi:7;
641  uint8_t crc:1;
642  };
643  };
644 
649  class IRQPin : public ExternalInterrupt {
650  friend class CC1101;
651  public:
660  ExternalInterrupt(pin, mode, true),
661  m_rf(rf)
662  {}
663 
669  virtual void on_interrupt(uint16_t arg = 0);
670 
671  private:
672  CC1101* m_rf;
673  };
674 
675 private:
677  static const uint8_t config[] __PROGMEM;
678 
679  IRQPin m_irq;
680  status_t m_status;
681  recv_status_t m_recv_status;
682 };
683 #endif
684 #endif
Exit RX/TX.
Definition: CC1101.hh:520
Modem deviation setting.
Definition: CC1101.hh:324
Receiver FIFO.
Definition: CC1101.hh:414
GDO0 output pin configuration.
Definition: CC1101.hh:305
Low byte of WOR timer.
Definition: CC1101.hh:486
Control state machine state.
Definition: CC1101.hh:484
Wake On Radio control.
Definition: CC1101.hh:335
uint8_t transfer(uint8_t data)
Definition: SOFT_SPI.cpp:87
void read(Config reg, void *buf, size_t count)
Definition: CC1101.hh:370
Modem configuration.
Definition: CC1101.hh:323
Receiver mode.
Definition: CC1101.hh:542
Fast Transmit ready.
Definition: CC1101.hh:544
AGC control.
Definition: CC1101.hh:330
status_t status() const
Definition: CC1101.hh:569
static const size_t PATABLE_MAX
Definition: CC1101.hh:420
#define __PROGMEM
Definition: Types.h:183
Frequency synthesizer cal control.
Definition: CC1101.hh:344
uint8_t rw
Read(1) or Write(0).
Definition: CC1101.hh:209
RXFIFO_OVERFLOW.
Definition: CC1101.hh:616
Command
Definition: CC1101.hh:513
Various test settings.
Definition: CC1101.hh:349
Front end TX configuration.
Definition: CC1101.hh:337
void write(Data reg, const void *buf, size_t count)
Definition: CC1101.hh:459
Frequency Offset Compensation config.
Definition: CC1101.hh:328
Packet automation control.
Definition: CC1101.hh:311
void read(uint8_t reg, void *buf, size_t count)
Definition: CC1101.hh:256
static const size_t DEVICE_PAYLOAD_MAX
Definition: CC1101.hh:64
Part number.
Definition: CC1101.hh:479
uint8_t reg
< Bit-field representation (little endian).
Definition: CC1101.hh:207
void acquire(Driver *dev)
Definition: SOFT_SPI.cpp:43
Reset real time clock.
Definition: CC1101.hh:526
#define NULL
Definition: Types.h:101
Bit Synchronization configuration.
Definition: CC1101.hh:329
status_t(uint8_t value)
Definition: CC1101.hh:559
Frequency control word, high byte.
Definition: CC1101.hh:316
State read_marc_state()
Definition: CC1101.hh:628
IRQPin(Board::ExternalInterruptPin pin, InterruptMode mode, CC1101 *rf)
Definition: CC1101.hh:659
PA control table.
Definition: CC1101.hh:412
Calibrate frequency synthesizer.
Definition: CC1101.hh:517
virtual bool end()
Definition: CC1101.cpp:164
FS_WAKEUP.
Definition: CC1101.hh:605
State (Figure 24, pp 50).
Definition: CC1101.hh:599
virtual int link_quality_indicator()
Definition: CC1101.hh:194
AGC control.
Definition: CC1101.hh:332
virtual bool begin(const void *config=NULL)
Definition: CC1101.cpp:127
Enable RX.
Definition: CC1101.hh:518
void write_P(Data reg, const uint8_t *buf, size_t count)
Definition: CC1101.hh:470
TXRX_SETTLING.
Definition: CC1101.hh:615
void write(uint8_t reg, uint8_t value)
Definition: CC1101.hh:267
virtual int send(uint8_t dest, uint8_t port, const iovec_t *vec)
Definition: CC1101.cpp:173
void write(uint8_t reg, const void *buf, size_t count)
Definition: CC1101.hh:280
Frequency synthesizer calibration running.
Definition: CC1101.hh:545
virtual int input_power_level()
Definition: CC1101.cpp:298
Frequency synthesizer control.
Definition: CC1101.hh:314
Power down mode when CSn goes high.
Definition: CC1101.hh:523
Definition: CC1101.hh:59
uint8_t read(Data reg)
Definition: CC1101.hh:427
Frequency synthesizer calibration.
Definition: CC1101.hh:339
void read(void *buf, size_t count)
Definition: SPI.hh:308
uint8_t rssi
< Bit-field representation (little endian).
Definition: CC1101.hh:639
Sync word, low byte.
Definition: CC1101.hh:308
Definition: Types.h:391
Enable TX.
Definition: CC1101.hh:519
Frequency synthesizer calibration.
Definition: CC1101.hh:341
header_t(uint8_t addr, uint8_t is_burst, uint8_t is_read)
Definition: CC1101.hh:219
Packet length.
Definition: CC1101.hh:309
GDO2 output pin configuration.
Definition: CC1101.hh:303
void write_P(Config reg, const uint8_t *buf, size_t count)
Definition: CC1101.hh:403
Flush the RX FIFO buffer.
Definition: CC1101.hh:524
Current GDOx status and packet status.
Definition: CC1101.hh:487
Last RC oscillator calibration result.
Definition: CC1101.hh:494
virtual int recv(uint8_t &src, uint8_t &port, void *buf, size_t len, uint32_t ms=0L)
Definition: CC1101.cpp:211
RXTX_SETTLING.
Definition: CC1101.hh:620
void strobe(Command cmd)
Definition: CC1101.cpp:110
High byte of WOR timer.
Definition: CC1101.hh:485
Frequency synthesizer calibration.
Definition: CC1101.hh:340
Mask fifo state.
Definition: CC1101.hh:492
Underflow and # of bytes in TXFIFO.
Definition: CC1101.hh:489
AFC adjustment of the frequency synthesizer.
Definition: CC1101.hh:521
RX FIFO has overflowed.
Definition: CC1101.hh:547
uint8_t lqi
Link Quality Indication.
Definition: CC1101.hh:640
FS_WAKEUP.
Definition: CC1101.hh:606
Low byte Event 0 timeout.
Definition: CC1101.hh:334
AGC test.
Definition: CC1101.hh:346
uint8_t read(Config reg)
Definition: CC1101.hh:358
Frequency synthesizer calibration.
Definition: CC1101.hh:338
Received signal strength indication.
Definition: CC1101.hh:483
Reset chip.
Definition: CC1101.hh:514
Last RC oscillator calibration result.
Definition: CC1101.hh:493
void write(Config reg, const void *buf, size_t count)
Definition: CC1101.hh:391
uint8_t read(uint8_t reg)
Definition: CC1101.hh:242
void write(const void *buf, size_t count)
Definition: SPI.hh:321
Current version number.
Definition: CC1101.hh:480
Front end RX configuration.
Definition: CC1101.hh:336
Turn off crystal oscillator.
Definition: CC1101.hh:516
ExternalInterruptPin
Definition: ATmega1284P.hh:190
Channel number.
Definition: CC1101.hh:313
Modem configuration.
Definition: CC1101.hh:320
Modem configuration.
Definition: CC1101.hh:321
uint8_t as_uint8
8-bit representation.
Definition: CC1101.hh:552
void begin()
Definition: SPI.hh:216
void end()
Definition: SPI.hh:226
Number of status registers.
Definition: CC1101.hh:495
Number of configuration registers.
Definition: CC1101.hh:350
Status
Definition: CC1101.hh:478
CC1101(uint16_t net, uint8_t dev, Board::DigitalPin csn=Board::D10, Board::ExternalInterruptPin irq=Board::EXT0)
Definition: CC1101.cpp:99
Config
Definition: CC1101.hh:302
Packet automation control.
Definition: CC1101.hh:310
High byte Event 0 timeout.
Definition: CC1101.hh:333
Various test settings.
Definition: CC1101.hh:348
virtual void wakeup_on_radio()
Definition: CC1101.cpp:272
Production test.
Definition: CC1101.hh:345
Frequency offset estimate.
Definition: CC1101.hh:481
void write(Config reg, uint8_t value)
Definition: CC1101.hh:380
Overflow and # of bytes in RXFIFO.
Definition: CC1101.hh:490
Flush the TX FIFO buffer.
Definition: CC1101.hh:525
status_t read_status(uint8_t fifo=1)
Definition: CC1101.hh:579
Frequency synthesizer control.
Definition: CC1101.hh:315
void write_P(uint8_t reg, const uint8_t *buf, size_t count)
Definition: CC1101.hh:293
No operation.
Definition: CC1101.hh:527
Frequency control word, middle byte.
Definition: CC1101.hh:317
uint8_t as_uint8
8-bit representation.
Definition: CC1101.hh:205
Start automatic Wake-on-Radio.
Definition: CC1101.hh:522
static const size_t PAYLOAD_MAX
Definition: CC1101.hh:72
PLL is settling.
Definition: CC1101.hh:546
void read(Data reg, void *buf, size_t count)
Definition: CC1101.hh:438
Main Radio Cntrl State Machine config.
Definition: CC1101.hh:326
Frequency control word, low byte.
Definition: CC1101.hh:318
void write_P(const uint8_t *buf, size_t count)
Definition: SPI.hh:334
TX FIFO har underflowed.
Definition: CC1101.hh:548
RC oscillator configuration.
Definition: CC1101.hh:342
SPI spi
Definition: SPI.cpp:29
Sync word, high byte.
Definition: CC1101.hh:307
Main Radio Cntrl State Machine config.
Definition: CC1101.hh:325
Current setting from PLL cal module.
Definition: CC1101.hh:488
Mask # bytes.
Definition: CC1101.hh:491
virtual void output_power_level(int8_t dBm)
Definition: CC1101.cpp:279
void release()
Definition: SOFT_SPI.cpp:64
Demodulator estimate for link quality.
Definition: CC1101.hh:482
void await(Mode mode)
Definition: CC1101.cpp:121
uint8_t read(Status reg)
Definition: CC1101.hh:503
Main State Machine Mode.
Definition: CC1101.hh:541
AGC control.
Definition: CC1101.hh:331
Device address.
Definition: CC1101.hh:312
Modem configuration.
Definition: CC1101.hh:319
CALIBRATE.
Definition: CC1101.hh:611
RX FIFO and TX FIFO thresholds.
Definition: CC1101.hh:306
Main Radio Cntrl State Machine config.
Definition: CC1101.hh:327
GDO1 output pin configuration.
Definition: CC1101.hh:304
void write(Data reg, uint8_t value)
Definition: CC1101.hh:448
Various test settings.
Definition: CC1101.hh:347
Transmit mode.
Definition: CC1101.hh:543
RC oscillator configuration.
Definition: CC1101.hh:343
Enable and calibrate frequency synthesizer.
Definition: CC1101.hh:515
TXFIFO_UNDERFLOW.
Definition: CC1101.hh:621
Modem configuration.
Definition: CC1101.hh:322
Transmitter FIFO.
Definition: CC1101.hh:413
virtual void powerdown()
Definition: CC1101.cpp:265
uint8_t burst
Burst(1) or Single(0) byte mode.
Definition: CC1101.hh:208