COSA
An Object-Oriented Platform for Arduino Programming
HCI.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_CC3000_HCI_HH
22 #define COSA_CC3000_HCI_HH
23 
24 #include "Cosa/SPI.hh"
26 
38 class HCI : public SPI::Driver {
39 public:
40 
44  class Event {
45  public:
46  class Handler {
47  public:
55  virtual void on_event(uint16_t event, void* args, size_t len) = 0;
56  };
57  };
58 
60  static const uint16_t DEFAULT_TIMEOUT = 3000;
61 
72  SPI::Driver(cs, SPI::ACTIVE_LOW, rate, 1, SPI::MSB_ORDER, &m_irq),
73  m_irq(irq, this),
74  m_available(false),
75  m_timeout(DEFAULT_TIMEOUT),
77  {
78  }
79 
89  int read(uint16_t &op, void* args, uint8_t len);
90 
100  int write(uint8_t type, uint16_t op, const void* args, uint8_t len)
101  {
102  return (write(type, op, args, len, false));
103  }
104 
114  int write_P(uint8_t type, uint16_t op, const void* args, uint8_t len)
115  {
116  return (write(type, op, args, len, true));
117  }
118 
129  int write(uint8_t type, uint16_t op, const void* args, uint8_t len,
130  bool progmem);
131 
140  int issue(uint16_t op, const void* args = NULL, uint8_t len = 0)
141  {
142  return (write(HCI_TYPE_CMND, op, args, len, false));
143  }
144 
153  int issue_P(uint16_t op, const void* args, uint8_t len)
154  {
155  return (write(HCI_TYPE_CMND, op, args, len, true));
156  }
157 
168  int await(uint16_t op, void* args = NULL, uint8_t len = 0);
169 
181  int write_data(uint8_t op, const void* args, uint8_t args_len,
182  const void* data, uint16_t data_len)
183  {
184  return (write_data(op, args, args_len, data, data_len, false));
185  }
186 
198  int write_data_P(uint8_t op, const void* args, uint8_t args_len,
199  const void* data, uint16_t data_len)
200  {
201  return (write_data(op, args, args_len, data, data_len, true));
202  }
203 
216  int write_data(uint8_t op, const void* args, uint8_t args_len,
217  const void* data, uint16_t data_len, bool progmem);
218 
229  int read_data(uint8_t op, void* args, uint8_t args_len,
230  void* data, uint16_t data_len);
231 
235  void enable()
236  {
237  m_irq.enable();
238  }
239 
243  void disable()
244  {
245  m_irq.disable();
246  }
247 
253  {
254  return (m_available);
255  }
256 
263  {
264  m_event_handler = handler;
265  }
266 
267 protected:
272  class IRQPin : public ExternalInterrupt {
273  friend class HCI;
274  public:
283  ExternalInterrupt(pin, ExternalInterrupt::ON_FALLING_MODE, true),
284  m_hci(hci)
285  {}
286 
292  virtual void on_interrupt(uint16_t arg)
293  {
294  UNUSED(arg);
295  m_hci->m_available = true;
296  }
297 
298  private:
299  HCI* m_hci;
300  };
301 
305  enum {
306  SPI_OP_WRITE = 0x01,
307  SPI_OP_REPLY = 0x02,
308  SPI_OP_READ = 0x03
309  };
310 
314  struct cmnd_header_t {
315  uint8_t type;
316  uint16_t cmnd;
317  uint8_t len;
318  };
319 
323  struct data_header_t {
324  uint8_t type;
325  uint8_t cmnd;
326  uint8_t args_len;
327  uint16_t payload_len;
328  };
329 
333  enum {
334  HCI_TYPE_CMND = 0x01,
335  HCI_TYPE_DATA = 0x02,
336  HCI_TYPE_PATCH = 0x03,
338  };
339 
342 
344  volatile bool m_available;
345 
347  uint16_t m_timeout;
348 
351 
353  static const uint8_t EVNT_MAX = 64;
354 
356  uint8_t m_evnt[EVNT_MAX];
357 };
358 #endif
void disable()
Definition: HCI.hh:243
int read(uint16_t &op, void *args, uint8_t len)
Definition: HCI.cpp:25
void enable()
Definition: HCI.hh:235
IRQPin(Board::ExternalInterruptPin pin, HCI *hci)
Definition: HCI.hh:282
uint16_t cmnd
HCI Operation Code (little-endian).
Definition: HCI.hh:316
Clock
Definition: SPI.hh:60
SPI Read(0,Payload Length).
Definition: HCI.hh:308
HCI Event (SPI_OP_READ only).
Definition: HCI.hh:337
SPI Reply(device ready).
Definition: HCI.hh:307
Definition: SPI.hh:57
#define NULL
Definition: Types.h:101
uint8_t type
HCI Message Type.
Definition: HCI.hh:324
int await(uint16_t op, void *args=NULL, uint8_t len=0)
Definition: HCI.cpp:123
uint8_t len
HCI Arguments Length.
Definition: HCI.hh:317
int write_data(uint8_t op, const void *args, uint8_t args_len, const void *data, uint16_t data_len)
Definition: HCI.hh:181
virtual void disable()
uint8_t args_len
HCI Arguments Length.
Definition: HCI.hh:326
int write_P(uint8_t type, uint16_t op, const void *args, uint8_t len)
Definition: HCI.hh:114
HCI(Board::DigitalPin cs, Board::ExternalInterruptPin irq, SPI::Clock rate=SPI::DEFAULT_CLOCK)
Definition: HCI.hh:69
uint8_t cmnd
HCI Data Operation Code.
Definition: HCI.hh:325
IRQPin m_irq
Definition: HCI.hh:341
virtual void on_interrupt(uint16_t arg)
Definition: HCI.hh:292
bool is_available()
Definition: HCI.hh:252
static const uint16_t DEFAULT_TIMEOUT
Definition: HCI.hh:60
int read_data(uint8_t op, void *args, uint8_t args_len, void *data, uint16_t data_len)
Definition: HCI.cpp:158
uint16_t m_timeout
Definition: HCI.hh:347
ExternalInterruptPin
Definition: ATmega1284P.hh:190
int write(uint8_t type, uint16_t op, const void *args, uint8_t len)
Definition: HCI.hh:100
#define UNUSED(x)
Definition: ATmega328P.hh:31
void event_handler(Event::Handler *handler)
Definition: HCI.hh:262
HCI Command (SPI_OP_WRITE only).
Definition: HCI.hh:334
int write_data_P(uint8_t op, const void *args, uint8_t args_len, const void *data, uint16_t data_len)
Definition: HCI.hh:198
uint16_t payload_len
HCI Payload Length.
Definition: HCI.hh:327
Event::Handler * m_event_handler
Definition: HCI.hh:350
int issue(uint16_t op, const void *args=NULL, uint8_t len=0)
Definition: HCI.hh:140
volatile bool m_available
Definition: HCI.hh:344
SPI Write(Payload Length,0).
Definition: HCI.hh:306
HCI Data (both SPI_OP_WRITE/READ).
Definition: HCI.hh:335
HCI Patch (SPI_OP_WRITE only).
Definition: HCI.hh:336
Definition: HCI.hh:38
uint8_t m_evnt[EVNT_MAX]
Definition: HCI.hh:356
Default clock rate.
Definition: SPI.hh:68
uint8_t type
HCI Message Type.
Definition: HCI.hh:315
static const uint8_t EVNT_MAX
Definition: HCI.hh:353
Driver(Board::DigitalPin cs, Pulse pulse=DEFAULT_PULSE, Clock rate=DEFAULT_CLOCK, uint8_t mode=0, Order order=MSB_ORDER, Interrupt::Handler *irq=NULL)
Definition: SOFT_SPI.cpp:25
int issue_P(uint16_t op, const void *args, uint8_t len)
Definition: HCI.hh:153
virtual void on_event(uint16_t event, void *args, size_t len)=0