-
Notifications
You must be signed in to change notification settings - Fork 11
/
at86rf2xx-internal.cpp
130 lines (114 loc) · 3.58 KB
/
at86rf2xx-internal.cpp
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
/*
* Copyright (C) 2013 Alaeddine Weslati <[email protected]>
* Copyright (C) 2015 Freie Universität Berlin
*
* 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_at86rf2xx
* @{
*
* @file
* @brief Implementation of driver internal functions
*
* @author Alaeddine Weslati <[email protected]>
* @author Thomas Eichinger <[email protected]>
* @author Joakim Nohlgård <[email protected]>
* @author Hauke Petersen <[email protected]>
* @author Mark Solters <[email protected]>
*
* @}
*/
#include <SPI.h>
#include "at86rf2xx.h"
void AT86RF2XX::reg_write(const uint8_t addr,
const uint8_t value)
{
byte writeCommand = addr | AT86RF2XX_ACCESS_REG | AT86RF2XX_ACCESS_WRITE;
digitalWrite(cs_pin, LOW);
SPI.transfer(writeCommand);
SPI.transfer(value);
digitalWrite(cs_pin, HIGH);
}
uint8_t AT86RF2XX::reg_read(const uint8_t addr)
{
byte value;
byte readCommand = addr | AT86RF2XX_ACCESS_REG | AT86RF2XX_ACCESS_READ;
digitalWrite(cs_pin, LOW);
SPI.transfer(readCommand);
value = SPI.transfer(0x00);
digitalWrite(cs_pin, HIGH);
return (uint8_t)value;
}
void AT86RF2XX::sram_read(const uint8_t offset,
uint8_t *data,
const size_t len)
{
byte readCommand = AT86RF2XX_ACCESS_SRAM | AT86RF2XX_ACCESS_READ;
digitalWrite(cs_pin, LOW);
SPI.transfer(readCommand);
SPI.transfer((char)offset);
for (int b=0; b<len; b++) {
data[b] = SPI.transfer(0x00);
}
digitalWrite(cs_pin, HIGH);
}
void AT86RF2XX::sram_write(const uint8_t offset,
const uint8_t *data,
const size_t len)
{
byte writeCommand = AT86RF2XX_ACCESS_SRAM | AT86RF2XX_ACCESS_WRITE;
digitalWrite(cs_pin, LOW);
SPI.transfer(writeCommand);
SPI.transfer((char)offset);
for (int b=0; b<len; b++) {
SPI.transfer(data[b]);
}
digitalWrite(cs_pin, HIGH);
}
void AT86RF2XX::fb_read(uint8_t *data,
const size_t len)
{
byte readCommand = AT86RF2XX_ACCESS_FB | AT86RF2XX_ACCESS_READ;
digitalWrite(cs_pin, LOW);
SPI.transfer(readCommand);
for (int b=0; b<len; b++) {
data[b] = SPI.transfer(0x00);
}
digitalWrite(cs_pin, HIGH);
}
uint8_t AT86RF2XX::get_status()
{
/* if sleeping immediately return state */
if(state == AT86RF2XX_STATE_SLEEP)
return state;
return reg_read(AT86RF2XX_REG__TRX_STATUS) & AT86RF2XX_TRX_STATUS_MASK__TRX_STATUS;
}
void AT86RF2XX::assert_awake()
{
if(get_status() == AT86RF2XX_STATE_SLEEP) {
/* wake up and wait for transition to TRX_OFF */
digitalWrite(sleep_pin, LOW);
delayMicroseconds(AT86RF2XX_WAKEUP_DELAY);
/* update state */
state = reg_read(AT86RF2XX_REG__TRX_STATUS) & AT86RF2XX_TRX_STATUS_MASK__TRX_STATUS;
}
}
void AT86RF2XX::hardware_reset()
{
/* wake up from sleep in case radio is sleeping */
//delayMicroseconds(50); // Arduino seems to hang without some minimum pause here
assert_awake();
/* trigger hardware reset */
digitalWrite(reset_pin, LOW);
delayMicroseconds(AT86RF2XX_RESET_PULSE_WIDTH);
digitalWrite(reset_pin, HIGH);
delayMicroseconds(AT86RF2XX_RESET_DELAY);
}
void AT86RF2XX::force_trx_off()
{
reg_write(AT86RF2XX_REG__TRX_STATE, AT86RF2XX_TRX_STATE__FORCE_TRX_OFF);
while (get_status() != AT86RF2XX_STATE_TRX_OFF);
}