-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc1101.cpp
110 lines (91 loc) · 2.9 KB
/
cc1101.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
/*
* this file is part of Somfy RTS communication protocol
*
* Copyright (c) 2017, yogui
*
* code inspired from culfw http://culfw.de/culfw.html
* and from this arduino forum thread : https://forum.arduino.cc/index.php?topic=208346.0
*
*
* 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 <COPYRIGHT HOLDER> 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 <stdio.h>
#include <string.h>
#include "mydelay.h"
#include "cc1101.h"
#include "somfyconfig.h"
#if ARDUINO >= 100
#include <Arduino.h> // Arduino 1.0
#else
#include <WProgram.h> // Arduino 0022
#endif
#include <SPI.h>
// send byte on SPI
uint8_t CC1101_sendbyte(uint8_t data)
{
#if defined (__arc__) || defined ESP8266
return SPI.transfer(data);
#else
SPDR = data; // send byte
while (!(SPSR & _BV (SPIF))); // wait until transfer finished
return SPDR;
#endif
}
//--------------------------------------------------------------------
// set CC1101 in Transfer mode
void ccTX(void)
{
uint8_t cnt = 0xff;
// Going from RX to TX does not work if there was a reception less than 0.5
// sec ago. Due to CCA? Using IDLE helps to shorten this period(?)
ccStrobe(CC1101_SIDLE);
while(cnt-- &&
(ccStrobe(CC1101_STX) & CC1101_STATUS_STATE_BM) != CC1101_STATE_TX)
delay_us(10);
}
//--------------------------------------------------------------------
// set CC1101 in recive mode
void ccRX(void)
{
uint8_t cnt = 0xff;
while(cnt-- &&
(ccStrobe(CC1101_SRX) & CC1101_STATUS_STATE_BM) != CC1101_STATE_RX)
delay_us(10);
}
//--------------------------------------------------------------------
// read single register on CC1101
uint8_t CC1101_readReg(uint8_t addr)
{
CC1101_ASSERT;
CC1101_sendbyte( addr|CC1101_READ_SINGLE );
uint8_t ret = CC1101_sendbyte( 0 );
CC1101_DEASSERT;
return ret;
}
//--------------------------------------------------------------------
// write single register on CC1101
void CC1101_writeReg(uint8_t addr, uint8_t data)
{
CC1101_ASSERT;
CC1101_sendbyte( addr );
CC1101_sendbyte( data );
CC1101_DEASSERT;
}
//--------------------------------------------------------------------
// send strobe commande
uint8_t ccStrobe(uint8_t strobe)
{
CC1101_ASSERT;
uint8_t ret = CC1101_sendbyte( strobe );
CC1101_DEASSERT;
return ret;
}