Blame view

RIOT/drivers/cc2420/cc2420_internal.c 3.65 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
  /*
   * Copyright (C) 2015-2016 Freie Universitรคt Berlin
   *               2016 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_cc2420
   * @{
   *
   * @file
   * @brief       Implementation of driver internal functions
   *
   * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
   * @author      Francisco Acosta <francisco.acosta@inria.fr>
   *
   * @}
   */
  
  #include "periph/spi.h"
  #include "periph/gpio.h"
  #include "xtimer.h"
  
  #include "cc2420_internal.h"
  #include "cc2420_registers.h"
  
  uint8_t cc2420_strobe(const cc2420_t *dev, const uint8_t command)
  {
      char res;
  
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_byte(dev->params.spi, (char)command, (char *)&res);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  
      return res;
  }
  
  void cc2420_reg_write(const cc2420_t *dev,
                        const uint8_t addr,
                        const uint16_t value)
  {
      uint16_t tmp = byteorder_htons(value).u16;
  
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_regs(dev->params.spi, CC2420_REG_WRITE | addr,
                        (char *)&tmp, NULL, 2);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  }
  
  uint16_t cc2420_reg_read(const cc2420_t *dev, const uint8_t addr)
  {
      network_uint16_t tmp;
  
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_regs(dev->params.spi, CC2420_REG_READ | addr,
                        NULL, (char *)&tmp, 2);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  
      return byteorder_ntohs(tmp);
  }
  
  void cc2420_ram_read(const cc2420_t *dev, const uint16_t addr,
                       uint8_t *data, const size_t len)
  {
      char tmp[] = { (CC2420_RAM      | (addr & 0x7f)),
                     (CC2420_RAM_READ | ((addr >> 1) & 0xc0)) };
  
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_bytes(dev->params.spi, tmp, NULL, 2);
      spi_transfer_bytes(dev->params.spi, NULL, (char*)data, len);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  }
  
  void cc2420_ram_write(const cc2420_t *dev, const uint16_t addr,
                        const uint8_t *data, const size_t len)
  {
      char tmp[] = { (CC2420_RAM       | (addr & 0x7f)),
                     (CC2420_RAM_WRITE | ((addr >> 1) & 0xc0)) };
  
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_bytes(dev->params.spi, tmp, NULL, 2);
      spi_transfer_bytes(dev->params.spi, (char*)data, NULL, len);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  }
  
  void cc2420_fifo_read(const cc2420_t *dev, uint8_t *data, const size_t len)
  {
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_regs(dev->params.spi, CC2420_FIFO_READ,
                        NULL, (char *)data, len);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  }
  
  void cc2420_fifo_write(const cc2420_t *dev, uint8_t *data, const size_t len)
  {
      spi_acquire(dev->params.spi);
      gpio_clear(dev->params.pin_cs);
      spi_transfer_regs(dev->params.spi, CC2420_FIFO_WRITE,
                        (char *)data, NULL, len);
      gpio_set(dev->params.pin_cs);
      spi_release(dev->params.spi);
  }
  
  uint8_t cc2420_status(cc2420_t *dev)
  {
      return cc2420_strobe(dev, CC2420_STROBE_NOP);
  }
  
  uint8_t cc2420_state(cc2420_t *dev)
  {
      return (uint8_t)cc2420_reg_read(dev, CC2420_REG_FSMSTATE);
  }
  
  void cc2420_en_xosc(cc2420_t *dev)
  {
      cc2420_strobe(dev, CC2420_STROBE_XOSCON);
      xtimer_usleep(CC2420_XOSCON_DELAY);
  }