STM32 V5 source code
guowenxue
2018-02-04 785deec23b4cb1e7c4c4d81eb808f195adb1d98a
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
 * \addtogroup trickle-timer
 * @{
 */
 
/**
 * \file
 *   Trickle timer library implementation.
 * \author
 *   George Oikonomou - <oikonomou@users.sourceforge.net>
 */
 
/*
 * Copyright (c) 2012, George Oikonomou - <oikonomou@users.sourceforge.net>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "contiki-conf.h"
#include "lib/trickle-timer.h"
#include "sys/ctimer.h"
#include "sys/cc.h"
#include "lib/random.h"
/*---------------------------------------------------------------------------*/
#define DEBUG 0
 
#if DEBUG
#include <stdio.h>
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif
/*---------------------------------------------------------------------------*/
/**
 * \brief Wide randoms for platforms using a 4-byte wide clock
 * (see ::TRICKLE_TIMER_WIDE_RAND)
 */
#if TRICKLE_TIMER_WIDE_RAND
#define tt_rand() wide_rand()
#else
#define tt_rand() random_rand()
#endif
/*---------------------------------------------------------------------------*/
/* Declarations of variables of local interest */
/*---------------------------------------------------------------------------*/
static struct trickle_timer *loctt;   /* Pointer to a struct for local use */
static clock_time_t loc_clock; /* A local, general-purpose placeholder */
 
static void fire(void *ptr);
static void double_interval(void *ptr);
/*---------------------------------------------------------------------------*/
/* Local utilities and functions to be used as ctimer callbacks */
/*---------------------------------------------------------------------------*/
#if TRICKLE_TIMER_WIDE_RAND
/* Returns a 4-byte wide, unsigned random number */
static uint32_t
wide_rand()
{
  return ((uint32_t)random_rand() << 16 | random_rand());
}
#endif
/*---------------------------------------------------------------------------*/
/*
 * Returns the maximum sane Imax value for a given Imin
 *
 * This function is a variant of a fairly standard 'Count Leading Zeros'. It
 * has three flavours. The most suitable one for a specific platform can be
 * configured by changing the value of TRICKLE_TIMER_CONF_MAX_IMAX_WIDTH
 * in the platform's contiki-conf.h
 */
#if TRICKLE_TIMER_ERROR_CHECKING
static uint8_t
max_imax(clock_time_t value)
{
  uint8_t zeros = 0;
#if (TRICKLE_TIMER_MAX_IMAX_WIDTH==TRICKLE_TIMER_MAX_IMAX_GENERIC)
  uint8_t i;
  clock_time_t mask = 0xFFFF;
 
  value--;
 
  for(i = sizeof(clock_time_t) << 2; i > 0; i >>= 1) {
    if((value & (mask <<= i)) == 0) {
      zeros += i;
      value <<= i;
    }
  }
 
#elif (TRICKLE_TIMER_MAX_IMAX_WIDTH==TRICKLE_TIMER_MAX_IMAX_16_BIT)
  if((value & 0xFF00) == 0) {
    zeros += 8;
    value <<= 8;
  }
  if((value & 0xF000) == 0) {
    zeros += 4;
    value <<= 4;
  }
  if((value & 0xC000) == 0) {
    zeros += 2;
    value <<= 2;
  }
  if((value & 0x8000) == 0) {
    zeros++;
  }
#elif (TRICKLE_TIMER_MAX_IMAX_WIDTH==TRICKLE_TIMER_MAX_IMAX_32_BIT)
  if((value & 0xFFFF0000) == 0) {
    zeros += 16;
    value <<= 16;
  }
  if((value & 0xFF000000) == 0) {
    zeros += 8;
    value <<= 8;
  }
  if((value & 0xF0000000) == 0) {
    zeros += 4;
    value <<= 4;
  }
  if((value & 0xC0000000) == 0) {
    zeros += 2;
    value <<= 2;
  }
  if((value & 0x80000000) == 0) {
    zeros += 1;
  }
#endif
 
  return zeros - 1; /* Always non-negative due to the range of 'value' */
}
#endif /* TRICKLE_TIMER_ERROR_CHECKING */
/*---------------------------------------------------------------------------*/
/* Returns a random time point t in [I/2 , I) */
static clock_time_t
get_t(clock_time_t i_cur)
{
  i_cur >>= 1;
 
  PRINTF("trickle_timer get t: [%lu, %lu)\n", (unsigned long)i_cur,
         (unsigned long)(i_cur << 1));
 
  return i_cur + (tt_rand() % i_cur);
}
/*---------------------------------------------------------------------------*/
static void
schedule_for_end(struct trickle_timer *tt)
{
  /* Reset our ctimer, schedule interval_end to run at time I */
  clock_time_t now = clock_time();
 
  loc_clock = TRICKLE_TIMER_INTERVAL_END(tt) - now;
 
  PRINTF("trickle_timer sched for end: at %lu, end in %ld\n",
         (unsigned long)clock_time(), (signed long)loc_clock);
 
  /* Interval's end will happen in loc_clock ticks. Make sure this isn't in
   * the past... */
  if(loc_clock > (TRICKLE_TIMER_CLOCK_MAX >> 1)) {
    loc_clock = 0; /* Interval ended in the past, schedule for in 0 */
    PRINTF("trickle_timer doubling: Was in the past. Compensating\n");
  }
 
  ctimer_set(&tt->ct, loc_clock, double_interval, tt);
}
/*---------------------------------------------------------------------------*/
/* This is used as a ctimer callback, thus its argument must be void *. ptr is
 * a pointer to the struct trickle_timer that fired */
static void
double_interval(void *ptr)
{
  clock_time_t last_end;
 
  /* 'cast' ptr to a struct trickle_timer */
  loctt = (struct trickle_timer *)ptr;
 
  loctt->c = 0;
 
  PRINTF("trickle_timer doubling: at %lu, (was for %lu), ",
         (unsigned long)clock_time(),
         (unsigned long)TRICKLE_TIMER_INTERVAL_END(loctt));
 
  /* Remember the previous interval's end (absolute time), before we double */
  last_end = TRICKLE_TIMER_INTERVAL_END(loctt);
 
  /* Double the interval if we have to */
  if(loctt->i_cur <= TRICKLE_TIMER_INTERVAL_MAX(loctt) >> 1) {
    /* If I <= Imax/2, we double */
    loctt->i_cur <<= 1;
    PRINTF("I << 1 = %lu\n", (unsigned long)loctt->i_cur);
  } else {
    /* We may have I > Imax/2 but I <> Imax, in which case we set to Imax
     * This will happen when I didn't start as Imin (before the first reset) */
    loctt->i_cur = TRICKLE_TIMER_INTERVAL_MAX(loctt);
    PRINTF("I = Imax = %lu\n", (unsigned long)loctt->i_cur);
  }
 
  /* Random t in [I/2, I) */
  loc_clock = get_t(loctt->i_cur);
 
  PRINTF("trickle_timer doubling: t=%lu\n", (unsigned long)loc_clock);
 
#if TRICKLE_TIMER_COMPENSATE_DRIFT
  /* Schedule for t ticks after the previous interval's end, not after now. If
   * that is in the past, schedule in 0 */
  loc_clock = (last_end + loc_clock) - clock_time();
  PRINTF("trickle_timer doubling: at %lu, in %ld ticks\n",
         (unsigned long)clock_time(), (signed long)loc_clock);
  if(loc_clock > (TRICKLE_TIMER_CLOCK_MAX >> 1)) {
    /* Oops, that's in the past */
    loc_clock = 0;
    PRINTF("trickle_timer doubling: Was in the past. Compensating\n");
  }
  ctimer_set(&loctt->ct, loc_clock, fire, loctt);
 
  /* Store the actual interval start (absolute time), we need it later.
   * We pretend that it started at the same time when the last one ended */
  loctt->i_start = last_end;
#else
  /* Assumed that the previous interval's end is 'now' and schedule in t ticks
   * after 'now', ignoring potential offsets */
  ctimer_set(&loctt->ct, loc_clock, fire, loctt);
  /* Store the actual interval start (absolute time), we need it later */
  loctt->i_start = loctt->ct.etimer.timer.start;
#endif
 
  PRINTF("trickle_timer doubling: Last end %lu, new end %lu, for %lu, I=%lu\n",
         (unsigned long)last_end,
         (unsigned long)TRICKLE_TIMER_INTERVAL_END(loctt),
         (unsigned long)(loctt->ct.etimer.timer.start +
                         loctt->ct.etimer.timer.interval),
         (unsigned long)(loctt->i_cur));
}
/*---------------------------------------------------------------------------*/
/* Called by the ctimer module at time t within the current interval. ptr is
 * a pointer to the struct trickle_timer of interest */
static void
fire(void *ptr)
{
  /* 'cast' c to a struct trickle_timer */
  loctt = (struct trickle_timer *)ptr;
 
  PRINTF("trickle_timer fire: at %lu (was for %lu)\n",
         (unsigned long)clock_time(),
         (unsigned long)(loctt->ct.etimer.timer.start +
                         loctt->ct.etimer.timer.interval));
 
  if(loctt->cb) {
    /*
     * Call the protocol's TX callback, with the suppression status as an
     * argument.
     */
    PRINTF("trickle_timer fire: Suppression Status %u (%u < %u)\n",
           TRICKLE_TIMER_PROTO_TX_ALLOW(loctt), loctt->c, loctt->k);
    loctt->cb(loctt->cb_arg, TRICKLE_TIMER_PROTO_TX_ALLOW(loctt));
  }
 
  if(trickle_timer_is_running(loctt)) {
    schedule_for_end(loctt);
  }
}
/*---------------------------------------------------------------------------*/
/* New trickle interval, either due to a newly set trickle timer or due to an
 * inconsistency. Schedule 'fire' to be called in t ticks. */
static void
new_interval(struct trickle_timer *tt)
{
  tt->c = 0;
 
  /* Random t in [I/2, I) */
  loc_clock = get_t(tt->i_cur);
 
  ctimer_set(&tt->ct, loc_clock, fire, tt);
 
  /* Store the actual interval start (absolute time), we need it later */
  tt->i_start = tt->ct.etimer.timer.start;
  PRINTF("trickle_timer new interval: at %lu, ends %lu, ",
         (unsigned long)clock_time(),
         (unsigned long)TRICKLE_TIMER_INTERVAL_END(tt));
  PRINTF("t=%lu, I=%lu\n", (unsigned long)loc_clock, (unsigned long)tt->i_cur);
}
/*---------------------------------------------------------------------------*/
/* Functions to be called by the protocol implementation */
/*---------------------------------------------------------------------------*/
void
trickle_timer_consistency(struct trickle_timer *tt)
{
  if(tt->c < 0xFF) {
    tt->c++;
  }
  PRINTF("trickle_timer consistency: c=%u\n", tt->c);
}
/*---------------------------------------------------------------------------*/
void
trickle_timer_inconsistency(struct trickle_timer *tt)
{
  /* "If I is equal to Imin when Trickle hears an "inconsistent" transmission,
   * Trickle does nothing." */
  if(tt->i_cur != tt->i_min) {
    PRINTF("trickle_timer inconsistency\n");
    tt->i_cur = tt->i_min;
 
    new_interval(tt);
  }
}
/*---------------------------------------------------------------------------*/
uint8_t
trickle_timer_config(struct trickle_timer *tt, clock_time_t i_min,
                     uint8_t i_max, uint8_t k)
{
#if TRICKLE_TIMER_ERROR_CHECKING
  /*
   * Although in theory Imin=1 is a valid value, it would break get_t() and
   * doesn't make sense anyway. Thus Valid Imin values are in the range:
   * 1 < Imin <= (TRICKLE_TIMER_CLOCK_MAX >> 1) + 1
   */
  if(TRICKLE_TIMER_IMIN_IS_BAD(i_min)) {
    PRINTF("trickle_timer config: Bad Imin value\n");
    return TRICKLE_TIMER_ERROR;
  }
 
  if(tt == NULL || i_max == 0 || k == 0) {
    PRINTF("trickle_timer config: Bad arguments\n");
    return TRICKLE_TIMER_ERROR;
  }
 
  /*
   * If clock_time_t is not wide enough to store Imin << Imax, we adjust Imax
   *
   * This means that 'we' are likely to have a different Imax than 'them'
   * See RFC 6206, sec 6.3 for the consequences of this situation
   */
  if(TRICKLE_TIMER_IPAIR_IS_BAD(i_min, i_max)) {
    PRINTF("trickle_timer config: %lu << %u would exceed clock boundaries. ",
           (unsigned long)i_min, i_max);
 
    /* For this Imin, get the maximum sane Imax */
    i_max = max_imax(i_min);
    PRINTF("trickle_timer config: Using Imax=%u\n", i_max);
  }
#endif
 
  tt->i_min = i_min;
  tt->i_max = i_max;
  tt->i_max_abs = i_min << i_max;
  tt->k = k;
 
  PRINTF("trickle_timer config: Imin=%lu, Imax=%u, k=%u\n",
         (unsigned long)tt->i_min, tt->i_max, tt->k);
 
  return TRICKLE_TIMER_SUCCESS;
}
/*---------------------------------------------------------------------------*/
uint8_t
trickle_timer_set(struct trickle_timer *tt, trickle_timer_cb_t proto_cb,
                  void *ptr)
{
#if TRICKLE_TIMER_ERROR_CHECKING
  /* Sanity checks */
  if(tt == NULL || proto_cb == NULL) {
    PRINTF("trickle_timer set: Bad arguments\n");
    return TRICKLE_TIMER_ERROR;
  }
#endif
 
  tt->cb = proto_cb;
  tt->cb_arg = ptr;
 
  /* Random I in [Imin , Imax] */
  tt->i_cur = tt->i_min +
    (tt_rand() % (TRICKLE_TIMER_INTERVAL_MAX(tt) - tt->i_min + 1));
 
  PRINTF("trickle_timer set: I=%lu in [%lu , %lu]\n", (unsigned long)tt->i_cur,
         (unsigned long)tt->i_min,
         (unsigned long)TRICKLE_TIMER_INTERVAL_MAX(tt));
 
  new_interval(tt);
 
  PRINTF("trickle_timer set: at %lu, ends %lu, t=%lu in [%lu , %lu)\n",
         (unsigned long)tt->i_start,
         (unsigned long)TRICKLE_TIMER_INTERVAL_END(tt),
         (unsigned long)tt->ct.etimer.timer.interval,
         (unsigned long)tt->i_cur >> 1, (unsigned long)tt->i_cur);
 
  return TRICKLE_TIMER_SUCCESS;
}
/*---------------------------------------------------------------------------*/
/** @} */