Blame view

RIOT/drivers/cc110x/cc110x-rxtx.c 7.73 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
  /*
   * Copyright (C) 2014 Freie Universitรคt Berlin
   * Copyright (C) 2013 INRIA
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     drivers_cc110x
   * @{
   * @file
   * @brief       Functions for packet reception and transmission on cc110x devices
   *
   * @author      Oliver Hahm <oliver.hahm@inria.fr>
   * @author      Fabian Nack <nack@inf.fu-berlin.de>
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   * @}
   */
  
  #include <errno.h>
  #include <stdio.h>
  #include <string.h>
  
  #include "cc110x.h"
  #include "cc110x-spi.h"
  #include "cc110x-internal.h"
  #include "cc110x-interface.h"
  #include "cc110x-defines.h"
  
  #include "periph/gpio.h"
  #include "irq.h"
  
  #include "kernel_types.h"
  #include "msg.h"
  
  #include "cpu_conf.h"
  #include "cpu.h"
  
  #include "log.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  static void _rx_abort(cc110x_t *dev)
  {
      gpio_irq_disable(dev->params.gdo2);
  
      cc110x_strobe(dev, CC110X_SIDLE);    /* Switch to IDLE (should already be)... */
      cc110x_strobe(dev, CC110X_SFRX);     /* ...for flushing the RX FIFO */
  
      cc110x_switch_to_rx(dev);
  }
  
  static void _rx_start(cc110x_t *dev)
  {
      dev->radio_state = RADIO_RX_BUSY;
  
      cc110x_pkt_buf_t *pkt_buf = &dev->pkt_buf;
      pkt_buf->pos = 0;
  
      gpio_irq_disable(dev->params.gdo2);
      cc110x_write_reg(dev, CC110X_IOCFG2, 0x01);
      gpio_irq_enable(dev->params.gdo2);
  }
  
  static void _rx_read_data(cc110x_t *dev, void(*callback)(void*), void*arg)
  {
      int fifo = cc110x_get_reg_robust(dev, 0xfb);
  
      if (fifo & 0x80) {
          DEBUG("%s:%s:%u rx overflow\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
          _rx_abort(dev);
          return;
      }
  
      if (!fifo) {
          gpio_irq_enable(dev->params.gdo2);
          return;
      }
  
      cc110x_pkt_buf_t *pkt_buf = &dev->pkt_buf;
      if (!pkt_buf->pos) {
          pkt_buf->pos = 1;
          pkt_buf->packet.length = cc110x_read_reg(dev, CC110X_RXFIFO);
  
          /* Possible packet received, RX -> IDLE (0.1 us) */
          dev->cc110x_statistic.packets_in++;
      }
  
      int left = pkt_buf->packet.length+1 - pkt_buf->pos;
  
      /* if the fifo doesn't contain the rest of the packet,
       * leav at least one byte as per spec sheet. */
      int to_read = (fifo < left) ? (fifo-1) : fifo;
      if (to_read > left) {
          to_read = left;
      }
  
      if (to_read) {
          cc110x_readburst_reg(dev, CC110X_RXFIFO,
                  ((char *)&pkt_buf->packet)+pkt_buf->pos, to_read);
          pkt_buf->pos += to_read;
      }
  
      if (to_read == left) {
          uint8_t status[2];
          /* full packet received. */
          /* Read the 2 appended status bytes (status[0] = RSSI, status[1] = LQI) */
          cc110x_readburst_reg(dev, CC110X_RXFIFO, (char *)status, 2);
  
          /* Store RSSI value of packet */
          pkt_buf->rssi = status[I_RSSI];
  
          /* Bit 0-6 of LQI indicates the link quality (LQI) */
          pkt_buf->lqi = status[I_LQI] & LQI_EST;
  
          /* MSB of LQI is the CRC_OK bit */
          int crc_ok = (status[I_LQI] & CRC_OK) >> 7;
  
          if (crc_ok) {
                      LOG_DEBUG("cc110x: received packet from=%u to=%u payload "
                              "len=%u\n",
                      (unsigned)pkt_buf->packet.phy_src,
                      (unsigned)pkt_buf->packet.address,
                      pkt_buf->packet.length-3);
              /* let someone know that we've got a packet */
              callback(arg);
  
              cc110x_switch_to_rx(dev);
          }
          else {
              DEBUG("%s:%s:%u crc-error\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
              dev->cc110x_statistic.packets_in_crc_fail++;
              _rx_abort(dev);
          }
      }
  }
  
  static void _rx_continue(cc110x_t *dev, void(*callback)(void*), void*arg)
  {
  
      if (dev->radio_state != RADIO_RX_BUSY) {
          DEBUG("%s:%s:%u _rx_continue in invalid state\n", RIOT_FILE_RELATIVE,
                  __func__, __LINE__);
          _rx_abort(dev);
          return;
      }
  
      gpio_irq_disable(dev->params.gdo2);
  
      do {
          _rx_read_data(dev, callback, arg);
      }
      while (gpio_read(dev->params.gdo2));
  }
  
  static void _tx_abort(cc110x_t *dev)
  {
      cc110x_switch_to_rx(dev);
  }
  
  static void _tx_continue(cc110x_t *dev)
  {
      gpio_irq_disable(dev->params.gdo2);
  
      cc110x_pkt_t *pkt = &dev->pkt_buf.packet;
      int size = pkt->length + 1;
      int left = size - dev->pkt_buf.pos;
  
      if (!left) {
          dev->cc110x_statistic.raw_packets_out++;
  
          LOG_DEBUG("cc110x: packet successfully sent.\n");
  
          cc110x_switch_to_rx(dev);
          return;
      }
  
      int fifo = 64 - cc110x_get_reg_robust(dev, 0xfa);
  
      if (fifo & 0x80) {
          DEBUG("%s:%s:%u tx underflow!\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
          _tx_abort(dev);
          return;
      }
  
      if (!fifo) {
          DEBUG("%s:%s:%u fifo full!?\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
          _tx_abort(dev);
          return;
      }
  
      int to_send = left > fifo ? fifo : left;
  
      /* Write packet into TX FIFO */
      cc110x_writeburst_reg(dev, CC110X_TXFIFO, ((char *)pkt)+dev->pkt_buf.pos, to_send);
      dev->pkt_buf.pos += to_send;
  
      if (left == size) {
          /* Switch to TX mode */
          cc110x_strobe(dev, CC110X_STX);
      }
  
      if (to_send < left) {
          /* set GDO2 to 0x2 -> will deassert at TX FIFO below threshold */
          gpio_irq_enable(dev->params.gdo2);
          cc110x_write_reg(dev, CC110X_IOCFG2, 0x02);
      }
      else {
          /* set GDO2 to 0x6 -> will deassert at packet end */
          cc110x_write_reg(dev, CC110X_IOCFG2, 0x06);
          gpio_irq_enable(dev->params.gdo2);
      }
  }
  
  void cc110x_isr_handler(cc110x_t *dev, void(*callback)(void*), void*arg)
  {
      switch (dev->radio_state) {
          case RADIO_RX:
              if (gpio_read(dev->params.gdo2)) {
                  _rx_start(dev);
              }
              else {
                  DEBUG("cc110x_isr_handler((): isr handled too slow?\n");
                  _rx_abort(dev);
              }
              break;
          case RADIO_RX_BUSY:
              _rx_continue(dev, callback, arg);
              break;
          case RADIO_TX_BUSY:
              if (!gpio_read(dev->params.gdo2)) {
                  _tx_continue(dev);
              }
              else {
                  DEBUG("cc110x_isr_handler() RADIO_TX_BUSY + GDO2\n");
              }
              break;
          default:
              DEBUG("%s:%s:%u: unhandled mode\n", RIOT_FILE_RELATIVE,
                      __func__, __LINE__);
      }
  }
  
  int cc110x_send(cc110x_t *dev, cc110x_pkt_t *packet)
  {
      DEBUG("cc110x: snd pkt to %u payload_length=%u\n",
              (unsigned)packet->address, (unsigned)packet->length-3);
      uint8_t size;
  
      switch (dev->radio_state) {
          case RADIO_RX_BUSY:
          case RADIO_TX_BUSY:
              DEBUG("cc110x: invalid state for sending: %s\n",
                      cc110x_state_to_text(dev->radio_state));
              return -EAGAIN;
      }
  
      /*
       * Number of bytes to send is:
       * length of phy payload (packet->length)
       * + size of length field (1 byte)
       */
      size = packet->length + 1;
  
      if (size > CC110X_PACKET_LENGTH) {
          DEBUG("%s:%s:%u trying to send oversized packet\n",
                  RIOT_FILE_RELATIVE, __func__, __LINE__);
          return -ENOSPC;
      }
  
      /* set source address */
      packet->phy_src = dev->radio_address;
  
      /* Disable RX interrupt */
      gpio_irq_disable(dev->params.gdo2);
      dev->radio_state = RADIO_TX_BUSY;
  
  #ifdef MODULE_CC110X_HOOKS
      cc110x_hook_tx();
  #endif
  
      cc110x_write_reg(dev, CC110X_IOCFG2, 0x02);
  
      /* Put CC110x in IDLE mode to flush the FIFO */
      cc110x_strobe(dev, CC110X_SIDLE);
      /* Flush TX FIFO to be sure it is empty */
      cc110x_strobe(dev, CC110X_SFTX);
  
      memcpy((char*)&dev->pkt_buf.packet, packet, size);
      dev->pkt_buf.pos = 0;
  
      _tx_continue(dev);
  
      return size;
  }