-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnrf_drv_mpu_spi.c
184 lines (138 loc) · 5.27 KB
/
nrf_drv_mpu_spi.c
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
/*
* The library is not extensively tested and only
* meant as a simple explanation and for inspiration.
* NO WARRANTY of ANY KIND is provided.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include "nrf_drv_spi.h"
#include "nrf_drv_mpu.h"
#include "app_util_platform.h"
#include "nrf_gpio.h"
#if defined(MPU9150)
#error "MPU9150 does not support SPI"
#endif
/* Pins to connect MPU. Pinout is different for nRF51 DK and nRF52 DK
* and therefore I have added a conditional statement defining different pins
* for each board. This is only for my own convenience and might be subject to changes as I devleop.
*/
#if defined(BOARD_CUSTOM)
#define MPU_SPI_MISO_PIN 3 // MPU SDO. 'AD0' on MPU breakout board silk screen
#define MPU_SPI_MOSI_PIN 4 // MPU SDI. 'SDA' on MPU breakout board silk screen
#define MPU_SPI_SCL_PIN 5 // MPU SCLK. 'SCL' on MPU breakout board silk screen
#define MPU_SPI_CS_PIN 6 // MPU nCS. 'NCS' on MPU breakout board silk screen
#else // If PCA10028
#define MPU_SPI_MISO_PIN 3 // MPU SDO. 'AD0' on MPU breakout board silk screen
#define MPU_SPI_MOSI_PIN 2 // MPU SDI. 'SDA' on MPU breakout board silk screen
#define MPU_SPI_SCL_PIN 1 // MPU SCLK. 'SCL' on MPU breakout board silk screen
#define MPU_SPI_CS_PIN 4 // MPU nCS. 'NCS' on MPU breakout board silk screen
#endif
#define MPU_SPI_BUFFER_SIZE 14 // 14 byte buffers will suffice to read acceleromter, gyroscope and temperature data in one transmission.
#define MPU_SPI_WRITE_BIT 0x00
#define MPU_SPI_READ_BIT 0x80
#define MPU_SPI_TIMEOUT 5000
static const nrf_drv_spi_t m_spi_instance = NRF_DRV_SPI_INSTANCE(0);
volatile static bool spi_tx_done = false;
uint8_t spi_tx_buffer[MPU_SPI_BUFFER_SIZE];
uint8_t spi_rx_buffer[MPU_SPI_BUFFER_SIZE];
void nrf_drv_mpu_spi_event_handler(const nrf_drv_spi_evt_t *evt, void * p_context)
{
if(evt->type == NRF_DRV_SPI_EVENT_DONE)
{
spi_tx_done = true;
}
else
{
// Something is wrong
}
}
/**
* @brief SPI initialization.
* Just the usual way. Nothing special here
*/
uint32_t nrf_drv_mpu_init(void)
{
nrf_drv_spi_config_t spi_mpu_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_mpu_config.sck_pin = MPU_SPI_SCL_PIN;
spi_mpu_config.mosi_pin = MPU_SPI_MOSI_PIN;
spi_mpu_config.miso_pin = MPU_SPI_MISO_PIN;
spi_mpu_config.ss_pin = MPU_SPI_CS_PIN;
spi_mpu_config.irq_priority = APP_IRQ_PRIORITY_HIGH;
spi_mpu_config.orc = 0xFF;
spi_mpu_config.frequency = NRF_DRV_SPI_FREQ_1M;
spi_mpu_config.mode = NRF_DRV_SPI_MODE_0;
spi_mpu_config.bit_order = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST;
return nrf_drv_spi_init(&m_spi_instance, &spi_mpu_config, nrf_drv_mpu_spi_event_handler, NULL);
}
void nrf_drv_spi_disable(void)
{
// NRF_SPI_Type *p = m_spi_instance.p_registers;
// nrf_spi_disable(p);
}
void nrf_drv_spi_enable(void)
{
// NRF_SPI_Type *p = m_spi_instance.p_registers;
// nrf_spi_enable(p);
}
/**@brief Function to merge a register and a buffer of data
*/
static void merge_register_and_data(uint8_t * new_buffer, uint8_t reg, uint8_t * p_data, uint32_t length)
{
new_buffer[0] = reg;
memcpy((new_buffer + 1), p_data, length);
}
/**@brief Function to write a series of bytes. The function merges
* the register and the data.
*/
uint32_t nrf_drv_mpu_write_registers(uint8_t reg, uint8_t * p_data, uint32_t length)
{
uint32_t err_code;
if(length > MPU_SPI_BUFFER_SIZE - 1) // Must be space for register byte in buffer
{
return NRF_ERROR_DATA_SIZE;
}
uint32_t timeout = MPU_SPI_TIMEOUT;
// Add write bit to register.
reg = reg | MPU_SPI_WRITE_BIT;
merge_register_and_data(spi_tx_buffer, reg, p_data, length + 1);
err_code = nrf_drv_spi_transfer(&m_spi_instance, spi_tx_buffer, length + 1, NULL, 0);
if(err_code != NRF_SUCCESS) return err_code;
while((!spi_tx_done) && --timeout);
if(!timeout) return NRF_ERROR_TIMEOUT;
spi_tx_done = false;
return err_code;
}
uint32_t nrf_drv_mpu_write_single_register(uint8_t reg, uint8_t data)
{
uint32_t err_code;
uint32_t timeout = MPU_SPI_TIMEOUT;
uint8_t packet[2] = {reg, data};
// Add write bit to register.
reg = reg | MPU_SPI_WRITE_BIT;
err_code = nrf_drv_spi_transfer(&m_spi_instance, packet, 2, NULL, 0);
if(err_code != NRF_SUCCESS) return err_code;
while((!spi_tx_done) && --timeout);
if(!timeout) return NRF_ERROR_TIMEOUT;
spi_tx_done = false;
return err_code;
}
uint32_t nrf_drv_mpu_read_registers(uint8_t reg, uint8_t * p_data, uint32_t length)
{
uint32_t err_code;
uint32_t timeout = MPU_SPI_TIMEOUT;
// Add read bit to register.
reg = reg | MPU_SPI_READ_BIT;
// Read data over SPI and store incomming data in spi_rx_buffer
err_code = nrf_drv_spi_transfer(&m_spi_instance, ®, 1, spi_rx_buffer, length + 1); // Length + 1 because register byte has to be clocked out before MPU returns data of length 'length'
if(err_code != NRF_SUCCESS) return err_code;
while((!spi_tx_done) && --timeout);
if(!timeout) return NRF_ERROR_TIMEOUT;
spi_tx_done = false;
// Copy data in spi_rx_buffer over to p_data
memcpy(p_data, &spi_rx_buffer[1], length);
return NRF_SUCCESS;
}
/**
@}
*/