Blame view

RIOT/drivers/cc110x/cc110x.c 6.65 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2014 Freie Universitรคt Berlin
   * Copyright (C) 2013 INRIA
   * Copyright (C) 2015 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * 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       Basic functionality of cc110x driver
   *
   * @author      Oliver Hahm <oliver.hahm@inria.fr>
   * @author      Fabian Nack <nack@inf.fu-berlin.de>
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   * @}
   */
  
  #include "luid.h"
  #include "board.h"
  #include "periph/gpio.h"
  #include "periph/spi.h"
  #include "xtimer.h"
  #include "cpu.h"
  #include "log.h"
  
  #include "cc110x.h"
  #include "cc110x-defaultsettings.h"
  #include "cc110x-defines.h"
  #include "cc110x-interface.h"
  #include "cc110x-internal.h"
  #include "cc110x-spi.h"
  
  #define ENABLE_DEBUG    (0)
  #include "debug.h"
  
  /* Internal function prototypes */
  #ifndef CC110X_DONT_RESET
  static void _reset(cc110x_t *dev);
  static void _power_up_reset(cc110x_t *dev);
  #endif
  
  int cc110x_setup(cc110x_t *dev, const cc110x_params_t *params)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
  
  #ifdef MODULE_CC110X_HOOKS
      cc110x_hooks_init();
  #endif
  
      dev->params = *params;
  
      /* Configure chip-select */
      spi_init_cs(dev->params.spi, dev->params.cs);
  
      /* Configure GDO1 */
      gpio_init(dev->params.gdo1, GPIO_IN);
  
  #ifndef CC110X_DONT_RESET
      /* reset device*/
      _power_up_reset(dev);
  #endif
  
      /* set default state */
      dev->radio_state = RADIO_IDLE;
  
      /* Write configuration to configuration registers */
      cc110x_writeburst_reg(dev, 0x00, cc110x_default_conf, cc110x_default_conf_size);
  
      /* Write PATABLE (power settings) */
      cc110x_writeburst_reg(dev, CC110X_PATABLE, CC110X_DEFAULT_PATABLE, 8);
  
      /* set base frequency */
      cc110x_set_base_freq_raw(dev, CC110X_DEFAULT_FREQ);
  
      /* Set default channel number */
      cc110x_set_channel(dev, CC110X_DEFAULT_CHANNEL);
  
      /* set default node id */
      uint8_t addr;
      luid_get(&addr, 1);
      cc110x_set_address(dev, addr);
  
      LOG_INFO("cc110x: initialized with address=%u and channel=%i\n",
              (unsigned)dev->radio_address,
              dev->radio_channel);
  
      return 0;
  }
  
  uint8_t cc110x_set_address(cc110x_t *dev, uint8_t address)
  {
      DEBUG("%s:%s:%u setting address %u\n", RIOT_FILE_RELATIVE, __func__,
              __LINE__, (unsigned)address);
      if (!(address < MIN_UID) || (address > MAX_UID)) {
          if (dev->radio_state != RADIO_UNKNOWN) {
              cc110x_write_register(dev, CC110X_ADDR, address);
              dev->radio_address = address;
              return address;
          }
      }
  
      return 0;
  }
  
  void cc110x_set_base_freq_raw(cc110x_t *dev, const char* freq_array)
  {
  #if ENABLE_DEBUG == 1
      uint8_t _tmp[] = { freq_array[2], freq_array[1], freq_array[0], 0x00};
      uint32_t *FREQ = (uint32_t*) _tmp;
  
      DEBUG("cc110x_set_base_freq_raw(): setting base frequency to %uHz\n",
              (26000000>>16) * (unsigned)(*FREQ));
  #endif
      cc110x_writeburst_reg(dev, CC110X_FREQ2, freq_array, 3);
  }
  
  void cc110x_set_monitor(cc110x_t *dev, uint8_t mode)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
  
      cc110x_write_register(dev, CC110X_PKTCTRL1, mode ? 0x04 : 0x06);
  }
  
  void cc110x_setup_rx_mode(cc110x_t *dev)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
  
      /* Stay in RX mode until end of packet */
      cc110x_write_reg(dev, CC110X_MCSM2, 0x07);
      cc110x_switch_to_rx(dev);
  }
  
  void cc110x_switch_to_rx(cc110x_t *dev)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
  
  #ifdef MODULE_CC110X_HOOKS
      cc110x_hook_rx();
  #endif
  
      gpio_irq_disable(dev->params.gdo2);
  
      /* flush RX fifo */
      cc110x_strobe(dev, CC110X_SIDLE);
      cc110x_strobe(dev, CC110X_SFRX);
  
      dev->radio_state = RADIO_RX;
  
      cc110x_write_reg(dev, CC110X_IOCFG2, 0x6);
      cc110x_strobe(dev, CC110X_SRX);
  
      gpio_irq_enable(dev->params.gdo2);
  }
  
  void cc110x_wakeup_from_rx(cc110x_t *dev)
  {
      if (dev->radio_state != RADIO_RX) {
          return;
      }
  
      LOG_DEBUG("cc110x: switching to idle mode\n");
  
      cc110x_strobe(dev, CC110X_SIDLE);
      dev->radio_state = RADIO_IDLE;
  }
  
  void cc110x_switch_to_pwd(cc110x_t *dev)
  {
      LOG_DEBUG("cc110x: switching to powerdown mode\n");
      cc110x_wakeup_from_rx(dev);
      cc110x_strobe(dev, CC110X_SPWD);
      dev->radio_state = RADIO_PWD;
  
  #ifdef MODULE_CC110X_HOOKS
       cc110x_hook_off();
  #endif
  }
  
  #ifndef MODULE_CC110X_HOOKS
  int16_t cc110x_set_channel(cc110x_t *dev, uint8_t channr)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
  
      if (channr > MAX_CHANNR) {
          return -1;
      }
  
      cc110x_write_register(dev, CC110X_CHANNR, channr * 10);
      dev->radio_channel = channr;
  
      return channr;
  }
  #endif
  
  #ifndef CC110X_DONT_RESET
  static void _reset(cc110x_t *dev)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
      cc110x_wakeup_from_rx(dev);
      cc110x_cs(dev);
      cc110x_strobe(dev, CC110X_SRES);
      xtimer_usleep(100);
  }
  
  static void _power_up_reset(cc110x_t *dev)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
      gpio_set(dev->params.cs);
      gpio_clear(dev->params.cs);
      gpio_set(dev->params.cs);
      xtimer_usleep(RESET_WAIT_TIME);
      _reset(dev);
  }
  #endif
  
  void cc110x_write_register(cc110x_t *dev, uint8_t r, uint8_t value)
  {
      /* Save old radio state */
      uint8_t old_state = dev->radio_state;
  
      /* Wake up from RX (no effect if in other mode) */
      cc110x_wakeup_from_rx(dev);
      cc110x_write_reg(dev, r, value);
  
      /* Have to put radio back to RX if old radio state
       * was RX, otherwise no action is necessary */
      if (old_state == RADIO_RX) {
          cc110x_switch_to_rx(dev);
      }
  }
  
  int cc110x_rd_set_mode(cc110x_t *dev, int mode)
  {
      DEBUG("%s:%s:%u\n", RIOT_FILE_RELATIVE, __func__, __LINE__);
  
      int result;
  
      /* Get current radio mode */
      if ((dev->radio_state == RADIO_UNKNOWN) || (dev->radio_state == RADIO_PWD)) {
          result = RADIO_MODE_OFF;
      }
      else {
          result = RADIO_MODE_ON;
      }
  
      switch(mode) {
          case RADIO_MODE_ON:
              LOG_DEBUG("cc110x: switching to RX mode\n");
              cc110x_setup_rx_mode(dev);          /* Set chip to desired mode */
              break;
  
          case RADIO_MODE_OFF:
              gpio_irq_disable(dev->params.gdo2); /* Disable interrupts */
              cc110x_switch_to_pwd(dev);          /* Set chip to power down mode */
              break;
  
          case RADIO_MODE_GET:
              /* do nothing, just return current mode */
          default:
              /* do nothing */
              break;
      }
  
      /* Return previous mode */
      return result;
  }