Skip to content

Commit f440ead

Browse files
martinbuddendigitalentity
authored andcommitted
New IO for UARTs, LED_STRIP and BIND_PIN - not ready to merge (#422)
* Initial preparation * Further uart IO conversion * Jetiexbus fixup * New IO for UARTs, LED_STRIP and BIND_PIN * Further updates to new IO * Further betaflight driver merges
1 parent 4bbd176 commit f440ead

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+1387
-1180
lines changed

Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,7 @@ STM32F10x_COMMON_SRC = \
485485
startup_stm32f10x_md_gcc.S \
486486
drivers/adc_stm32f10x.c \
487487
drivers/bus_i2c_stm32f10x.c \
488+
drivers/dma.c \
488489
drivers/gpio_stm32f10x.c \
489490
drivers/inverter.c \
490491
drivers/serial_softserial.c \
@@ -497,8 +498,8 @@ STM32F30x_COMMON_SRC = \
497498
target/system_stm32f30x.c \
498499
drivers/adc_stm32f30x.c \
499500
drivers/bus_i2c_stm32f30x.c \
501+
drivers/dma.c \
500502
drivers/gpio_stm32f30x.c \
501-
drivers/light_ws2811strip.c \
502503
drivers/light_ws2811strip_stm32f30x.c \
503504
drivers/serial_uart_stm32f30x.c \
504505
drivers/system_stm32f30x.c \

src/main/config/config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -932,10 +932,10 @@ static void validateAndFixConfig(void)
932932
if (featureConfigured(FEATURE_SOFTSERIAL) && (
933933
0
934934
#ifdef USE_SOFTSERIAL1
935-
|| (LED_STRIP_TIMER == SOFTSERIAL_1_TIMER)
935+
|| (WS2811_TIMER == SOFTSERIAL_1_TIMER)
936936
#endif
937937
#ifdef USE_SOFTSERIAL2
938-
|| (LED_STRIP_TIMER == SOFTSERIAL_2_TIMER)
938+
|| (WS2811_TIMER == SOFTSERIAL_2_TIMER)
939939
#endif
940940
)) {
941941
// led strip needs the same timer as softserial

src/main/drivers/accgyro_lsm303dlhc.h

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,11 @@ typedef struct {
7575
* @brief LSM303DLHC I2C Interface pins
7676
*/
7777
#define LSM303DLHC_I2C I2C1
78-
#define LSM303DLHC_I2C_CLK RCC_APB1Periph_I2C1
79-
80-
#define LSM303DLHC_I2C_SCK_PIN GPIO_Pin_6 /* PB.06 */
81-
#define LSM303DLHC_I2C_SCK_GPIO_PORT GPIOB /* GPIOB */
82-
#define LSM303DLHC_I2C_SCK_GPIO_CLK RCC_AHBPeriph_GPIOB
83-
#define LSM303DLHC_I2C_SCK_SOURCE GPIO_PinSource6
84-
#define LSM303DLHC_I2C_SCK_AF GPIO_AF_4
85-
86-
#define LSM303DLHC_I2C_SDA_PIN GPIO_Pin_7 /* PB.7 */
87-
#define LSM303DLHC_I2C_SDA_GPIO_PORT GPIOB /* GPIOB */
88-
#define LSM303DLHC_I2C_SDA_GPIO_CLK RCC_AHBPeriph_GPIOB
89-
#define LSM303DLHC_I2C_SDA_SOURCE GPIO_PinSource7
90-
#define LSM303DLHC_I2C_SDA_AF GPIO_AF_4
91-
92-
#define LSM303DLHC_DRDY_PIN GPIO_Pin_2 /* PE.02 */
93-
#define LSM303DLHC_DRDY_GPIO_PORT GPIOE /* GPIOE */
94-
#define LSM303DLHC_DRDY_GPIO_CLK RCC_AHBPeriph_GPIOE
95-
#define LSM303DLHC_DRDY_EXTI_LINE EXTI_Line2
96-
#define LSM303DLHC_DRDY_EXTI_PORT_SOURCE EXTI_PortSourceGPIOE
97-
#define LSM303DLHC_DRDY_EXTI_PIN_SOURCE EXTI_PinSource2
98-
#define LSM303DLHC_DRDY_EXTI_IRQn EXTI2_TS_IRQn
99-
100-
#define LSM303DLHC_I2C_INT1_PIN GPIO_Pin_4 /* PE.04 */
101-
#define LSM303DLHC_I2C_INT1_GPIO_PORT GPIOE /* GPIOE */
102-
#define LSM303DLHC_I2C_INT1_GPIO_CLK RCC_AHBPeriph_GPIOE
103-
#define LSM303DLHC_I2C_INT1_EXTI_LINE EXTI_Line4
104-
#define LSM303DLHC_I2C_INT1_EXTI_PORT_SOURCE EXTI_PortSourceGPIOE
105-
#define LSM303DLHC_I2C_INT1_EXTI_PIN_SOURCE EXTI_PinSource4
106-
#define LSM303DLHC_I2C_INT1_EXTI_IRQn EXTI4_IRQn
107-
108-
#define LSM303DLHC_I2C_INT2_PIN GPIO_Pin_5 /* PE.05 */
109-
#define LSM303DLHC_I2C_INT2_GPIO_PORT GPIOE /* GPIOE */
110-
#define LSM303DLHC_I2C_INT2_GPIO_CLK RCC_AHBPeriph_GPIOE
111-
#define LSM303DLHC_I2C_INT2_EXTI_LINE EXTI_Line5
112-
#define LSM303DLHC_I2C_INT2_EXTI_PORT_SOURCE EXTI_PortSourceGPIOE
113-
#define LSM303DLHC_I2C_INT2_EXTI_PIN_SOURCE EXTI_PinSource5ss
114-
#define LSM303DLHC_I2C_INT2_EXTI_IRQn EXTI9_5_IRQn
78+
#define LSM303DLHC_I2C_SCK_PIN PB6 /* PB.06 */
79+
#define LSM303DLHC_I2C_SDA_PIN PB7 /* PB.7 */
80+
#define LSM303DLHC_DRDY_PIN PE2 /* PE.02 */
81+
#define LSM303DLHC_I2C_INT1_PIN PE4 /* PE.04 */
82+
#define LSM303DLHC_I2C_INT2_PIN PE5 /* PE.05 */
11583

11684
/******************************************************************************/
11785
/*************************** START REGISTER MAPPING **************************/

src/main/drivers/accgyro_mpu.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "accgyro_mpu6500.h"
4141
#include "accgyro_spi_mpu6000.h"
4242
#include "accgyro_spi_mpu6500.h"
43+
#include "accgyro_spi_mpu9250.h"
4344
#include "accgyro_mpu.h"
4445

4546
//#define DEBUG_MPU_DATA_READY_INTERRUPT
@@ -152,6 +153,19 @@ static bool detectSPISensorsAndUpdateDetectionResult(void)
152153
}
153154
#endif
154155

156+
#ifdef USE_GYRO_SPI_MPU9250
157+
if (mpu9250SpiDetect()) {
158+
mpuDetectionResult.sensor = MPU_9250_SPI;
159+
mpuConfiguration.gyroReadXRegister = MPU_RA_GYRO_XOUT_H;
160+
mpuConfiguration.read = mpu9250ReadRegister;
161+
mpuConfiguration.slowread = mpu9250SlowReadRegister;
162+
mpuConfiguration.verifywrite = verifympu9250WriteRegister;
163+
mpuConfiguration.write = mpu9250WriteRegister;
164+
mpuConfiguration.reset = mpu9250ResetGyro;
165+
return true;
166+
}
167+
#endif
168+
155169
return false;
156170
}
157171
#endif

src/main/drivers/accgyro_mpu6500.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#define MPU6500_WHO_AM_I_CONST (0x70)
1919
#define MPU9250_WHO_AM_I_CONST (0x71)
20+
#define ICM20608G_WHO_AM_I_CONST (0xAF)
2021

2122
#define MPU6500_BIT_RESET (0x80)
2223

Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
/*
2+
* This file is part of Cleanflight.
3+
*
4+
* Cleanflight is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Cleanflight is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
19+
/*
20+
* Authors:
21+
* Dominic Clifton - Cleanflight implementation
22+
* John Ihlein - Initial FF32 code
23+
* Kalyn Doerr (RS2K) - Robust rewrite
24+
*/
25+
26+
#include <stdbool.h>
27+
#include <stdint.h>
28+
29+
#include "platform.h"
30+
#include "light_led.h"
31+
32+
#include "common/axis.h"
33+
#include "common/maths.h"
34+
35+
#include "io.h"
36+
37+
#include "system.h"
38+
#include "exti.h"
39+
#include "bus_spi.h"
40+
#include "gyro_sync.h"
41+
#include "debug.h"
42+
43+
#include "sensor.h"
44+
#include "accgyro.h"
45+
#include "accgyro_mpu.h"
46+
#include "accgyro_spi_mpu9250.h"
47+
48+
static void mpu9250AccAndGyroInit(uint8_t lpf);
49+
50+
static bool mpuSpi9250InitDone = false;
51+
52+
static IO_t mpuSpi9250CsPin = IO_NONE;
53+
54+
#define DISABLE_MPU9250 IOHi(mpuSpi9250CsPin)
55+
#define ENABLE_MPU9250 IOLo(mpuSpi9250CsPin)
56+
57+
void mpu9250ResetGyro(void)
58+
{
59+
// Device Reset
60+
mpu9250WriteRegister(MPU_RA_PWR_MGMT_1, MPU9250_BIT_RESET);
61+
delay(150);
62+
}
63+
64+
bool mpu9250WriteRegister(uint8_t reg, uint8_t data)
65+
{
66+
ENABLE_MPU9250;
67+
delayMicroseconds(1);
68+
spiTransferByte(MPU9250_SPI_INSTANCE, reg);
69+
spiTransferByte(MPU9250_SPI_INSTANCE, data);
70+
DISABLE_MPU9250;
71+
delayMicroseconds(1);
72+
73+
return true;
74+
}
75+
76+
bool mpu9250ReadRegister(uint8_t reg, uint8_t length, uint8_t *data)
77+
{
78+
ENABLE_MPU9250;
79+
spiTransferByte(MPU9250_SPI_INSTANCE, reg | 0x80); // read transaction
80+
spiTransfer(MPU9250_SPI_INSTANCE, data, NULL, length);
81+
DISABLE_MPU9250;
82+
83+
return true;
84+
}
85+
86+
bool mpu9250SlowReadRegister(uint8_t reg, uint8_t length, uint8_t *data)
87+
{
88+
ENABLE_MPU9250;
89+
delayMicroseconds(1);
90+
spiTransferByte(MPU9250_SPI_INSTANCE, reg | 0x80); // read transaction
91+
spiTransfer(MPU9250_SPI_INSTANCE, data, NULL, length);
92+
DISABLE_MPU9250;
93+
delayMicroseconds(1);
94+
95+
return true;
96+
}
97+
98+
void mpu9250SpiGyroInit(uint8_t lpf)
99+
{
100+
(void)(lpf);
101+
102+
mpuIntExtiInit();
103+
104+
mpu9250AccAndGyroInit(lpf);
105+
106+
spiResetErrorCounter(MPU9250_SPI_INSTANCE);
107+
108+
spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_FAST); //high speed now that we don't need to write to the slow registers
109+
110+
int16_t data[3];
111+
mpuGyroRead(data);
112+
113+
if ((((int8_t)data[1]) == -1 && ((int8_t)data[0]) == -1) || spiGetErrorCounter(MPU9250_SPI_INSTANCE) != 0) {
114+
spiResetErrorCounter(MPU9250_SPI_INSTANCE);
115+
failureMode(FAILURE_GYRO_INIT_FAILED);
116+
}
117+
}
118+
119+
void mpu9250SpiAccInit(acc_t *acc)
120+
{
121+
mpuIntExtiInit();
122+
123+
acc->acc_1G = 512 * 8;
124+
}
125+
126+
bool verifympu9250WriteRegister(uint8_t reg, uint8_t data)
127+
{
128+
uint8_t in;
129+
uint8_t attemptsRemaining = 20;
130+
131+
mpu9250WriteRegister(reg, data);
132+
delayMicroseconds(100);
133+
134+
do {
135+
mpu9250SlowReadRegister(reg, 1, &in);
136+
if (in == data) {
137+
return true;
138+
} else {
139+
debug[3]++;
140+
mpu9250WriteRegister(reg, data);
141+
delayMicroseconds(100);
142+
}
143+
} while (attemptsRemaining--);
144+
return false;
145+
}
146+
147+
static void mpu9250AccAndGyroInit(uint8_t lpf) {
148+
149+
if (mpuSpi9250InitDone) {
150+
return;
151+
}
152+
153+
spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_INITIALIZATON); //low speed for writing to slow registers
154+
155+
mpu9250WriteRegister(MPU_RA_PWR_MGMT_1, MPU9250_BIT_RESET);
156+
delay(50);
157+
158+
verifympu9250WriteRegister(MPU_RA_PWR_MGMT_1, INV_CLK_PLL);
159+
160+
verifympu9250WriteRegister(MPU_RA_GYRO_CONFIG, INV_FSR_2000DPS << 3 | FCB_DISABLED); //Fchoice_b defaults to 00 which makes fchoice 11
161+
162+
if (lpf == 4) {
163+
verifympu9250WriteRegister(MPU_RA_CONFIG, 1); //1KHz, 184DLPF
164+
} else if (lpf < 4) {
165+
verifympu9250WriteRegister(MPU_RA_CONFIG, 7); //8KHz, 3600DLPF
166+
} else if (lpf > 4) {
167+
verifympu9250WriteRegister(MPU_RA_CONFIG, 0); //8KHz, 250DLPF
168+
}
169+
170+
verifympu9250WriteRegister(MPU_RA_SMPLRT_DIV, gyroMPU6xxxGetDividerDrops()); // Get Divider Drops
171+
172+
verifympu9250WriteRegister(MPU_RA_ACCEL_CONFIG, INV_FSR_8G << 3);
173+
verifympu9250WriteRegister(MPU_RA_INT_PIN_CFG, 0 << 7 | 0 << 6 | 0 << 5 | 1 << 4 | 0 << 3 | 0 << 2 | 1 << 1 | 0 << 0); // INT_ANYRD_2CLEAR, BYPASS_EN
174+
175+
#if defined(USE_MPU_DATA_READY_SIGNAL)
176+
verifympu9250WriteRegister(MPU_RA_INT_ENABLE, 0x01); //this resets register MPU_RA_PWR_MGMT_1 and won't read back correctly.
177+
#endif
178+
179+
spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_FAST);
180+
181+
mpuSpi9250InitDone = true; //init done
182+
}
183+
184+
bool mpu9250SpiDetect(void)
185+
{
186+
uint8_t in;
187+
uint8_t attemptsRemaining = 20;
188+
189+
/* not the best place for this - should really have an init method */
190+
#ifdef MPU9250_CS_PIN
191+
mpuSpi9250CsPin = IOGetByTag(IO_TAG(MPU9250_CS_PIN));
192+
#endif
193+
IOInit(mpuSpi9250CsPin, OWNER_MPU, RESOURCE_SPI_CS, 0);
194+
IOConfigGPIO(mpuSpi9250CsPin, SPI_IO_CS_CFG);
195+
196+
spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_INITIALIZATON); //low speed
197+
mpu9250WriteRegister(MPU_RA_PWR_MGMT_1, MPU9250_BIT_RESET);
198+
199+
do {
200+
delay(150);
201+
202+
mpu9250ReadRegister(MPU_RA_WHO_AM_I, 1, &in);
203+
if (in == MPU9250_WHO_AM_I_CONST) {
204+
break;
205+
}
206+
if (!attemptsRemaining) {
207+
return false;
208+
}
209+
} while (attemptsRemaining--);
210+
211+
spiSetDivisor(MPU9250_SPI_INSTANCE, SPI_CLOCK_FAST);
212+
213+
return true;
214+
}
215+
216+
bool mpu9250SpiAccDetect(acc_t *acc)
217+
{
218+
if (mpuDetectionResult.sensor != MPU_9250_SPI) {
219+
return false;
220+
}
221+
222+
acc->init = mpu9250SpiAccInit;
223+
acc->read = mpuAccRead;
224+
225+
return true;
226+
}
227+
228+
bool mpu9250SpiGyroDetect(gyro_t *gyro)
229+
{
230+
if (mpuDetectionResult.sensor != MPU_9250_SPI) {
231+
return false;
232+
}
233+
234+
gyro->init = mpu9250SpiGyroInit;
235+
gyro->read = mpuGyroRead;
236+
gyro->intStatus = checkMPUDataReady;
237+
238+
// 16.4 dps/lsb scalefactor
239+
gyro->scale = 1.0f / 16.4f;
240+
241+
return true;
242+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
#pragma once
3+
4+
#define mpu9250_CONFIG 0x1A
5+
6+
/* We should probably use these. :)
7+
#define BITS_DLPF_CFG_256HZ 0x00
8+
#define BITS_DLPF_CFG_188HZ 0x01
9+
#define BITS_DLPF_CFG_98HZ 0x02
10+
#define BITS_DLPF_CFG_42HZ 0x03
11+
#define BITS_DLPF_CFG_20HZ 0x04
12+
#define BITS_DLPF_CFG_10HZ 0x05
13+
#define BITS_DLPF_CFG_5HZ 0x06
14+
#define BITS_DLPF_CFG_2100HZ_NOLPF 0x07
15+
*/
16+
17+
#define GYRO_SCALE_FACTOR 0.00053292f // (4/131) * pi/180 (32.75 LSB = 1 DPS)
18+
19+
#define MPU9250_WHO_AM_I_CONST (0x71)
20+
21+
#define MPU9250_BIT_RESET (0x80)
22+
23+
// RF = Register Flag
24+
#define MPU_RF_DATA_RDY_EN (1 << 0)
25+
26+
void mpu9250ResetGyro(void);
27+
28+
bool mpu9250SpiDetect(void);
29+
30+
bool mpu9250SpiAccDetect(acc_t *acc);
31+
bool mpu9250SpiGyroDetect(gyro_t *gyro);
32+
33+
bool mpu9250WriteRegister(uint8_t reg, uint8_t data);
34+
bool verifympu9250WriteRegister(uint8_t reg, uint8_t data);
35+
bool mpu9250ReadRegister(uint8_t reg, uint8_t length, uint8_t *data);
36+
bool mpu9250SlowReadRegister(uint8_t reg, uint8_t length, uint8_t *data);

0 commit comments

Comments
 (0)