cc110x-spi.c
4.75 KB
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
/*
* Copyright (C) 2014 Freie Universität Berlin
* 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 TI Chipcon CC110x spi driver
*
* @author Thomas Hillebrandt <hillebra@inf.fu-berlin.de>
* @author Heiko Will <hwill@inf.fu-berlin.de>
* @author Fabian Nack <nack@inf.fu-berlin.de>
* @author Joakim Gebart <joakim.gebart@eistec.se>
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @}
*/
#include <stdio.h>
#include "cc110x.h"
#include "cc110x-spi.h"
#include "cc110x-internal.h"
#include "cc110x-defines.h"
#include "periph/gpio.h"
#include "periph/spi.h"
#include "xtimer.h"
#include "irq.h"
/**********************************************************************
* CC110x spi access
**********************************************************************/
void cc110x_cs(cc110x_t *dev)
{
volatile int retry_count = 0;
/* Switch MISO/GDO1 to GPIO input mode */
#ifndef GPIO_READS_SPI_PINS
gpio_init(dev->params.gdo1, GPIO_IN);
#endif
/* CS to low */
gpio_clear(dev->params.cs);
/* Wait for SO to go low (voltage regulator
* has stabilized and the crystal is running) */
while (gpio_read(dev->params.gdo1)) {
/* Wait ~500us and try again */
xtimer_usleep(CS_SO_WAIT_TIME);
if (gpio_read(dev->params.gdo1)) {
retry_count++;
if (retry_count > CC110X_GDO1_LOW_RETRY) {
puts("[CC110X spi] fatal error\n");
break;
}
gpio_set(dev->params.cs);
gpio_clear(dev->params.cs);
}
}
/* Switch MISO/GDO1 to spi mode */
#ifndef GPIO_READS_SPI_PINS
spi_conf_pins(dev->params.spi);
#endif
}
void cc110x_writeburst_reg(cc110x_t *dev, uint8_t addr, const char *src, uint8_t count)
{
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
spi_transfer_regs(dev->params.spi, addr | CC110X_WRITE_BURST, (char *)src, 0, count);
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
}
void cc110x_readburst_reg(cc110x_t *dev, uint8_t addr, char *buffer, uint8_t count)
{
int i = 0;
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
spi_transfer_byte(dev->params.spi, addr | CC110X_READ_BURST, 0);
while (i < count) {
spi_transfer_byte(dev->params.spi, CC110X_NOBYTE, &buffer[i]);
i++;
}
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
}
void cc110x_write_reg(cc110x_t *dev, uint8_t addr, uint8_t value)
{
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
spi_transfer_reg(dev->params.spi, addr, value, 0);
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
}
uint8_t cc110x_read_reg(cc110x_t *dev, uint8_t addr)
{
char result;
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
spi_transfer_reg(dev->params.spi, addr | CC110X_READ_SINGLE, CC110X_NOBYTE, &result);
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
return (uint8_t) result;
}
uint8_t cc110x_read_status(cc110x_t *dev, uint8_t addr)
{
char result;
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
spi_transfer_reg(dev->params.spi, addr | CC110X_READ_BURST, CC110X_NOBYTE, &result);
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
return (uint8_t) result;
}
uint8_t cc110x_get_reg_robust(cc110x_t *dev, uint8_t addr)
{
char result, result2;
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
do {
spi_transfer_reg(dev->params.spi, addr | CC110X_READ_BURST, CC110X_NOBYTE, &result);
spi_transfer_reg(dev->params.spi, addr | CC110X_READ_BURST, CC110X_NOBYTE, &result2);
} while (result != result2);
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
return (uint8_t) result;
}
uint8_t cc110x_strobe(cc110x_t *dev, uint8_t c)
{
#ifdef CC110X_DONT_RESET
if (c == CC110X_SRES) {
return 0;
}
#endif
char result;
unsigned int cpsr;
spi_acquire(dev->params.spi);
cpsr = irq_disable();
cc110x_cs(dev);
spi_transfer_byte(dev->params.spi, c, &result);
gpio_set(dev->params.cs);
irq_restore(cpsr);
spi_release(dev->params.spi);
return (uint8_t) result;
}