diff --git a/extras/I2C_Chip_Scanner/I2C_Chip_Scanner.ino b/extras/I2C_Chip_Scanner/I2C_Chip_Scanner.ino index 02b1c754..793be683 100644 --- a/extras/I2C_Chip_Scanner/I2C_Chip_Scanner.ino +++ b/extras/I2C_Chip_Scanner/I2C_Chip_Scanner.ino @@ -18,38 +18,43 @@ I2C: Found address: 0x77 (decimal 119) I2C: Found 4 device(s) ====================================================================*/ +#include "Wire.h" + +TwoWire *i2c = &Wire; + #define ESP32_SDA_PIN 23 #define ESP32_SCL_PIN 22 -#define RP2040_SDA_PIN 20 -#define RP2040_SCL_PIN 21 +#define RP2040_SDA_PIN 20 //Wire: 0, 4(default), 8, 12, 16, 20 Wire1: 2, 6, 10, 14, 18, 26(default) +#define RP2040_SCL_PIN 21 //Wire: 1, 5(default), 9, 13, 17, 21 Wire1: 3, 7, 11, 15, 19, 27(default) #define STM32_SDA_PIN PB9 #define STM32_SCL_PIN PB8 #define I2C_FRQ 400000 -#include "Wire.h" + + void setup() { Serial.begin(115200); - //start Wire + //start WIRE #if defined ARDUINO_ARCH_ESP32 - Wire.begin(ESP32_SDA_PIN, ESP32_SCL_PIN, I2C_FRQ); + i2c->begin(ESP32_SDA_PIN, ESP32_SCL_PIN, I2C_FRQ); #elif defined ARDUINO_ARCH_RP2040 - Wire.setSDA(RP2040_SDA_PIN); - Wire.setSCL(RP2040_SCL_PIN); - Wire.setClock(I2C_FRQ); - Wire.begin(); + i2c->setSDA(RP2040_SDA_PIN); + i2c->setSCL(RP2040_SCL_PIN); + i2c->setClock(I2C_FRQ); + i2c->begin(); #elif defined ARDUINO_ARCH_STM32 - Wire.setSDA(STM32_SDA_PIN); - Wire.setSCL(STM32_SCL_PIN); - Wire.setClock(I2C_FRQ); - Wire.begin(); + i2c->setSDA(STM32_SDA_PIN); + i2c->setSCL(STM32_SCL_PIN); + i2c->setClock(I2C_FRQ); + i2c->begin(); #else #warning "Using default I2C pins" - Wire.begin(); + i2c->begin(); #endif } @@ -59,8 +64,8 @@ void loop() { Serial.printf("I2C: Scanning ...\n"); int num_adr_found = 0; for (uint8_t adr = 8; adr < 120; adr++) { - Wire.beginTransmission(adr); // Begin I2C transmission Address (i) - if (Wire.endTransmission() == 0) { // Receive 0 = success (ACK response) + i2c->beginTransmission(adr); // Begin I2C transmission Address (i) + if (i2c->endTransmission() == 0) { // Receive 0 = success (ACK response) Serial.printf("I2C: Found address: 0x%02X (decimal %d)\n",adr,adr); num_adr_found++; i2c_identify_chip(adr); @@ -76,33 +81,38 @@ struct test_struct{ uint8_t reg; uint8_t len; uint32_t expected; - char descr[60]; + String descr; }; //table of addresses, register and expected reply test_struct tests[] = { // adr1 adr2 reg len exp descr - {0x0D, 0x0D, 0x0D, 1, 0xFF, "QMC5883L"}, - {0x1E, 0x1E, 0x0A, 3, 0x483433, "HMC5883L"}, // ID-Reg-A 'H', ID-Reg-B '4', ID-Reg-C '3' - {0x40, 0x4F, 0x00, 2, 0x399F, "INA219"}, - {0x40, 0x4F, 0x00, 2, 0x4127, "INA226"}, - {0x48, 0x4F, 0x01, 2, 0x8583, "ADS1113, ADS1114, or ADS1115"}, - {0x68, 0x69, 0x00, 1, 0xEA, "ICM-20948"}, + {0x0D, 0x0D, 0x0D, 1, 0xFF, "QMC5883L magnetometer"}, + {0x1E, 0x1E, 0x0A, 3, 0x483433, "HMC5883L magnetometer"}, // ID-Reg-A 'H', ID-Reg-B '4', ID-Reg-C '3' + {0x40, 0x4F, 0x00, 2, 0x399F, "INA219 current sensor"}, + {0x40, 0x4F, 0x00, 2, 0x4127, "INA226 current sensor"}, + {0x48, 0x4F, 0x01, 2, 0x8583, "ADS1113, ADS1114, or ADS1115 ADC"}, + {0x68, 0x69, 0x00, 1, 0xEA, "ICM-20948 9DOF"}, {0x68, 0x69, 0x00, 1, 0x68, "MPU3050"}, {0x68, 0x69, 0x00, 1, 0x69, "MPU3050"}, - {0x68, 0x69, 0x75, 1, 0x19, "MPU6886"}, - {0x68, 0x69, 0x75, 1, 0x68, "MPU6000, MPU6050, or MPU9150"}, - {0x68, 0x69, 0x75, 1, 0x70, "MPU6500"}, - {0x68, 0x69, 0x75, 1, 0x71, "MPU9250"}, - {0x68, 0x69, 0x75, 1, 0x72, "FAKE MPU6050, got WHO_AM_I=0x72, real chip returns 0x68"}, + {0x68, 0x69, 0x75, 1, 0x19, "MPU6886 6DOF"}, + {0x68, 0x69, 0x75, 1, 0x68, "MPU6000, MPU6050, or MPU9150 6/9DOF"}, + {0x68, 0x69, 0x75, 1, 0x69, "FAKE MPU6050, got WHO_AM_I=0x69, real chip returns 0x68"}, + {0x68, 0x69, 0x75, 1, 0x70, "MPU6500 6DOF"}, + {0x68, 0x69, 0x75, 1, 0x71, "MPU9250 9DOF"}, + {0x68, 0x69, 0x75, 1, 0x72, "FAKE MPU6050, got WHO_AM_I=0x72, real chip returns 0x68"}, + {0x68, 0x69, 0x75, 1, 0x73, "MPU9255 9DOF"}, + {0x68, 0x69, 0x75, 1, 0x74, "MPU9515"}, + {0x68, 0x69, 0x75, 1, 0x75, "FAKE MPU9250, got WHO_AM_I=0x75, real chip returns 0x71"}, + {0x68, 0x69, 0x75, 1, 0x78, "FAKE MPU9250, got WHO_AM_I=0x78, real chip returns 0x71"}, {0x68, 0x69, 0x75, 1, 0x98, "ICM-20689"}, - {0x76, 0x76, 0xD0, 1, 0x56, "BMP280"}, - {0x76, 0x76, 0xD0, 1, 0x57, "BMP280"}, - {0x76, 0x76, 0xD0, 1, 0x58, "BMP280"}, - {0x76, 0x76, 0xD0, 1, 0x60, "BME280"}, - {0x76, 0x76, 0xD0, 1, 0x61, "BMP680"}, - {0x76, 0x77, 0x0D, 1, 0x10, "DPS310 or HP303B"}, - {0x77, 0x77, 0xD0, 1, 0x55, "BMP180"}, + {0x76, 0x76, 0xD0, 1, 0x56, "BMP280 pressure"}, + {0x76, 0x76, 0xD0, 1, 0x57, "BMP280 pressure"}, + {0x76, 0x76, 0xD0, 1, 0x58, "BMP280 pressure"}, + {0x76, 0x76, 0xD0, 1, 0x60, "BME280 pressure"}, + {0x76, 0x76, 0xD0, 1, 0x61, "BMP680 pressure"}, + {0x76, 0x77, 0x0D, 1, 0x10, "DPS310 or HP303B pressure"}, + {0x77, 0x77, 0xD0, 1, 0x55, "BMP180 pressure"}, {0,0,0,0,0,""} //end @@ -125,6 +135,8 @@ adrtest_struct adrtests[] = { void i2c_identify_chip(uint8_t adr) { bool found = false; + uint32_t tried[256] = {0}; + //try adr,reg,result match int i = 0; while(tests[i].adr1) { @@ -138,8 +150,9 @@ void i2c_identify_chip(uint8_t adr) { uint8_t data[4]; i2c_ReadRegs(adr, reg, data, len); for(int i=0;i %s\n", reg, (int)received, tests[i].descr.c_str()); found = true; } } @@ -147,13 +160,18 @@ void i2c_identify_chip(uint8_t adr) { } if(!found) { + //show tries + for(int i=0;i<256;i++) if(tried[i]!=0) { + Serial.printf(" tried: reg=0x%02X result=0x%02X\n", i, (int)tried[i]); + } + //try address only match int i = 0; while(adrtests[i].adr1) { int adr1 = adrtests[i].adr1; int adr2 = adrtests[i].adr2; if(adr1<=adr && adr<=adr2) { - Serial.printf(" POTENTIAL MATCH (address 0x%02X match only): %s\n", adr, adrtests[i].descr); + Serial.printf(" POTENTIAL MATCH (address 0x%02X match only) -> %s\n", adr, adrtests[i].descr); } i++; } @@ -161,10 +179,10 @@ void i2c_identify_chip(uint8_t adr) { } void WriteReg( uint8_t adr, uint8_t reg, uint8_t data ) { - Wire.beginTransmission(adr); - Wire.write(reg); - Wire.write(data); - Wire.endTransmission(); + i2c->beginTransmission(adr); + i2c->write(reg); + i2c->write(data); + i2c->endTransmission(); } unsigned int i2c_ReadReg( uint8_t adr, uint8_t reg ) { @@ -174,22 +192,22 @@ unsigned int i2c_ReadReg( uint8_t adr, uint8_t reg ) { } void i2c_ReadRegs( uint8_t adr, uint8_t reg, uint8_t *data, uint8_t n ) { - Wire.beginTransmission(adr); - Wire.write(reg); - Wire.endTransmission(false); //false = repeated start - uint8_t bytesReceived = Wire.requestFrom(adr, n); + i2c->beginTransmission(adr); + i2c->write(reg); + i2c->endTransmission(false); //false = repeated start + uint8_t bytesReceived = i2c->requestFrom(adr, n); if(bytesReceived == n) { - Wire.readBytes(data, bytesReceived); + i2c->readBytes(data, bytesReceived); } } void i2c_scan() { Serial.printf("I2C: Scanning ...\n"); byte count = 0; - Wire.begin(); + i2c->begin(); for (byte i = 8; i < 120; i++) { - Wire.beginTransmission(i); // Begin I2C transmission Address (i) - if (Wire.endTransmission() == 0) { // Receive 0 = success (ACK response) + i2c->beginTransmission(i); // Begin I2C transmission Address (i) + if (i2c->endTransmission() == 0) { // Receive 0 = success (ACK response) Serial.printf("I2C: Found address: 0x%02X (%d)\n",i,i); count++; } diff --git a/extras/TestMadflight/imu.ino b/extras/TestMadflight/imu.ino deleted file mode 100644 index 6e438a7e..00000000 --- a/extras/TestMadflight/imu.ino +++ /dev/null @@ -1,72 +0,0 @@ -//test program for MPU sensor lib - -#include -#include "MPU9250.h" - -#define SS_PIN 17 - -MPU9250 mpu(&SPI, SS_PIN); - -void setup() { - Serial.begin(115200); - SPI.begin(); - mpu.begin(); -} - -void loop() { -// test_read(); - test_motion9NED(); -// test_hispeed(); -} - - -void test_read() { - mpu.read(); - Serial.printf("gx:%+.2f\tgy:%+.2f\tgz:%+.2f\t",mpu.gyro[0],mpu.gyro[1],mpu.gyro[2]); - Serial.printf("ax:%+.2f\tay:%+.2f\taz:%+.2f\t",mpu.accel[0],mpu.accel[1],mpu.accel[2]); - Serial.printf("mx:%+.2f\tmy:%+.2f\tmz:%+.2f\t",mpu.mag[0],mpu.mag[1],mpu.mag[2]); - Serial.printf("temp:%+.2f\n",mpu.temperature); - delay(10); -} - -void test_motion9NED() { - float ax,ay,az,gx,gy,gz,mx,my,mz; - mpu.getMotion9NED(&ax,&ay,&az,&gx,&gy,&gz,&mx,&my,&mz); - Serial.printf("gx:%+.2f\tgy:%+.2f\tgz:%+.2f\t",gx,gy,gz); - Serial.printf("ax:%+.2f\tay:%+.2f\taz:%+.2f\t",ax,ay,az); - Serial.printf("mx:%+.2f\tmy:%+.2f\tmz:%+.2f\n",mx,my,mz); - delay(10); -} - - -//high speed measurement -const int MEASCNT = 1000; -float meas[MEASCNT+10]; -int cnt = 0; - -void test_hispeed() { - uint32_t t0 = micros(); - uint32_t tt = t0; - while(cnt + + +void setup() { + Serial.begin(115200); + while(!Serial); + + hw_setup(); //hardware specific setup for spi and Wire (see hw_xxx.h) + + //IMU: keep on trying until no error is returned (some sensors need a couple of tries...) + while(true) { + int rv = imu.setup(); //request 1000 Hz sample rate, returns 0 on success, positive on error, negative on warning + if(rv<=0) break; + Serial.print("IMU: init failed rv= " + String(rv) + ". Retrying...\n"); + i2c->begin(); + delay(1000); + } + + //start IMU update handler + imu.onUpdate = imu_loop; + while(!imu.waitNewSample()) Serial.println("IMU interrupt not firing."); +} + +int imu_loop_cnt = 0; + +void imu_loop() { + imu_loop_cnt++; +} + +void loop() { + Serial.printf("imu_loop_cnt:%d az:%f gz:%f mz:%f\n",imu_loop_cnt, imu.az, imu.gz, imu.mz); + delay(1000); +} + diff --git a/extras/img/RP2040-Zero.jpg b/extras/img/RP2040-Zero.jpg new file mode 100644 index 00000000..07d8a4a5 Binary files /dev/null and b/extras/img/RP2040-Zero.jpg differ diff --git a/readme.md b/readme.md index c1d12c98..455c1714 100644 --- a/readme.md +++ b/readme.md @@ -115,10 +115,10 @@ A workaround is to use #define USE_ESP32_SOFTWIRE which enables software I2C, bu ## Pinout ESP32 -Default pinout for ESP32, using the Espressiv ESP32 DevKitC (38 pin) board. This pinout is defined in madflight_board_default_ESP32.h, but can be modified with `#define HW_PIN_XXX` in your program. +This is the default pinout for ESP32. It is optimized for the Espressiv ESP32 DevKitC (38 pin) board. This pinout is defined in madflight_board_default_ESP32.h, but can be modified with `#define HW_PIN_XXX` in your program. | Function | GPIO | Board | GPIO | Function | -| --: | :-- | :--: |--: | :-- | +| --: | :-- | :--: | --: | :-- | | 3V3 out | 3V3 | Antenna side | GND | GND | reset button | EN | | 23 | I2C_SDA | SPI_MISO | VP 36 input only | | 22 | I2C_SCL @@ -143,18 +143,16 @@ Note: During boot the input voltage levels (pull up/pull down) on strap pins hav (*) 5V input via diode from BEC. Without a diode take care not connect USB and the battery at the same time! -This pinout can be changed as needed in madflight_board_default_ESP32.h - ## Pinout ESP32-S3 -Default pinout for ESP32-S3, using the Espressiv ESP32-S3 DevKitC-1 (44 pin) board. This pinout is defined in madflight_board_default_ESP32-S3.h, but can be modified with `#define HW_PIN_XXX` in your program. +This is the default pinout for ESP32-S3. It is optimized for the Espressiv ESP32-S3 DevKitC-1 (44 pin) board. This pinout is defined in madflight_board_default_ESP32-S3.h, but can be modified with `#define HW_PIN_XXX` in your program. | Function | GPIO | Board | GPIO | Function | -| --: | :-- | :--: |--: | :-- | +| --: | :-- | :--: | --: | :-- | 3V3 out | 3V3 | Antenna side | G | GND 3V3 out | 3V3 | | 43 | TX serial debug UART port reset button | RST | | 44 | RX serial debug UART port @@ -195,18 +193,20 @@ RP2040 has dual core, no FPU, and FreeRTOS is optional. madflight uses FreeRTOS and executes the 1000Hz IMU loop on the second core, which is 80% loaded at default 133MHz CPU speed. You can overclock the CPU to get some more headroom. -#### Serial +#### PWM -madflight uses a custom high performance SerialIRQ library. +Consecutive even/odd PWM pins (e.g. pins 2,3 or 10,11) share the same timer and have the same frequency. +#### Serial +madflight uses a custom high performance SerialIRQ library. -## Pinout RP2040 +## Pinout RP2040 Raspberry Pi Pico -Default pinout for RP2040, using the Raspberry Pi Pico (40 pin) board. This pinout is defined in madflight_board_default_RP2040.h, but can be modified with `#define HW_PIN_XXX` in your program. +This is the default pinout for RP2040. It is optimized for the Raspberry Pi Pico (40 pin) board. This pinout is defined in madflight_board_default_RP2040.h, but can be modified with `#define HW_PIN_XXX` in your program. | Function | GPIO | Board | GPIO | Function | -| --: | :-- | :--: |--: | :-- | +| --: | :-- | :--: | --: | :-- | | RCIN_TX | 0 | USB connector | VBUS | nc | RCIN_RX | 1 | | VSYS | 5V input via diode (*) | GND | GND | | GND | GND @@ -228,12 +228,36 @@ Default pinout for RP2040, using the Raspberry Pi Pico (40 pin) board. This pino | PWM11 | 14 | | 17 | IMU_CS | PWM12 | 15 | JTAG pins | 16 | SPI_MISO -Consecutive even/odd PWM pins (e.g. pins 2,3 or 10,11) share the same timer and have the same frequency. - (*) 5V input via diode from BEC. Without a diode take care not connect USB and the battery at the same time! +## Pinout RP2040-Zero + +This pinout is optimized for the RP2040-Zero (21 pin) board. This pinout is defined in madflight_board_RP2040-Zero.h, but can be modified with `#define HW_PIN_XXX` in your program. + +| Function | GPIO | Board | GPIO | Function | +| --: | :-- | :--: | --: | :-- | +| 5V input (*) | 5V | USB connector | 0 | RCIN_TX +| GND | GND | | 1 | RCIN_RX +| 3V3 out | 3V3 | | 2 | PWM1 +| - | 29 | | 3 | PWM2 +| BAT_V | 28 | | 4 | PWM3 +| I2C1_SCL | 27 | | 5 | PWM4 +| I2C1_SDA | 26 | | 6 | PWM5 +| IMU_EXTI | 15 | | 7 | PWM6 +| | 14 | | 8 | GPS_TX +| | | | | +| | | | 9 | GPS_RX +| | | | 10 | SPI1_SCLK +| | | | 11 | SPI1_MISO +| | | | 12 | SPI1_MOSI +| | | | 13 | IMU_CS + +(*) 5V input via diode from BEC. Without a diode take care not connect USB and the battery at the same time! + + + @@ -251,16 +275,16 @@ madflight runs the IMU loop in interrupt context. ## Pinout STM32 Of-the-shelf Flight Controllers -In the `src` directory you'll find header files for 400+ commercial flight controllers. These are converted Betaflight configuration files. Include the header file of your board, and in your program set '#define HW_USE_XXX' to match your board. +In the `src` directory you'll find 400+ Betaflight configuration files for commercial flight controllers. Include the madflight_board_betaflight_XXX.h header file of your board, and in your program set '#define HW_USE_XXX' to match your board. -## Pinout STM32 +## Pinout STM32 Black Pill -Default pinout for STM32, using the WeAct STM32F411 Black Pill (40 pin) board. This pinout is defined in madflight_board_default_STM32.h, but can be modified with `#define HW_PIN_XXX` in your program. +This is the default pinout for STM32. It is optimized for the WeAct STM32F411 Black Pill (40 pin) board. This pinout is defined in madflight_board_default_STM32.h, but can be modified with `#define HW_PIN_XXX` in your program. | Function | GPIO | Board | GPIO | Function | -| --: | :-- | :--: |--: | :-- | +| --: | :-- | :--: | --: | :-- | | nc | VB | SWD pins | 3V3 | 3V3 out | LED | C13 | | G | GND | - | C14 | | 5V | 5V input (*) diff --git a/src/madflight.h b/src/madflight.h index 60e738b4..e6805d38 100644 --- a/src/madflight.h +++ b/src/madflight.h @@ -1,4 +1,4 @@ -#define MADFLIGHT_VERSION "madflight v1.1.2" +#define MADFLIGHT_VERSION "madflight v1.1.3-DEV" /*========================================================================================== madflight - Flight Controller for ESP32 / RP2040 / STM32 diff --git a/src/madflight/imu/MPUxxxx/AK8963.h b/src/madflight/imu/MPUxxxx/AK8963.h index fcb579ea..2d09b9d1 100644 --- a/src/madflight/imu/MPUxxxx/AK8963.h +++ b/src/madflight/imu/MPUxxxx/AK8963.h @@ -48,7 +48,8 @@ class AK8963 { int begin() { _iface->WriteReg(MPUREG_I2C_MST_CTRL, 0x0D); // I2C master clock speed 400KHz - _iface->WriteReg(MPUREG_USER_CTRL, 0x30); // Enable I2C master mode, disable slave mode I2C bus + //_iface->WriteReg(MPUREG_USER_CTRL, 0x30); // Enable I2C master mode, disable slave mode I2C bus --> ONLY DO THIS IN SPI MODE, disables the external I2C interface.... + _iface->WriteReg(MPUREG_USER_CTRL, 0x20); // Enable I2C master mode int rv = 0; diff --git a/src/madflight/imu/MPUxxxx/MPU_interface.h b/src/madflight/imu/MPUxxxx/MPU_interface.h index 9f7e67e9..6803b779 100644 --- a/src/madflight/imu/MPUxxxx/MPU_interface.h +++ b/src/madflight/imu/MPUxxxx/MPU_interface.h @@ -105,10 +105,10 @@ class MPU_InterfaceI2C : public MPU_Interface{ _i2c_adr = i2c_adr; freqSlow = MPU_I2C_FREQ_SLOW; freqFast = MPU_I2C_FREQ_FAST; - setFreq(freqSlow); } virtual void begin() { + setFreq(freqSlow); } void setFreq(int freq) { @@ -120,6 +120,7 @@ class MPU_InterfaceI2C : public MPU_Interface{ _i2c->write(reg); _i2c->write(data); _i2c->endTransmission(); + //Serial.printf("WriteReg(reg0x%02X, data=0x%02X) --> ", reg, data); ReadReg(reg); return 0; } @@ -131,6 +132,7 @@ class MPU_InterfaceI2C : public MPU_Interface{ if(bytesReceived == n) { _i2c->readBytes(data, bytesReceived); } + //Serial.printf("ReadRegs(reg=0x%02X, n=%d) --> data[%d]=0x%02X\n", reg, n, bytesReceived, data[0]); } private: @@ -242,18 +244,18 @@ class MPU_InterfaceI2C : public MPU_Interface{ #define MPUREG_I2C_MST_DELAY_CTRL 0x67 #define MPUREG_SIGNAL_PATH_RESET 0x68 #define MPUREG_MOT_DETECT_CTRL 0x69 -#define MPUREG_USER_CTRL 0x6A -#define MPUREG_PWR_MGMT_1 0x6B -#define MPUREG_PWR_MGMT_2 0x6C -#define MPUREG_BANK_SEL 0x6D -#define MPUREG_MEM_START_ADDR 0x6E -#define MPUREG_MEM_R_W 0x6F -#define MPUREG_DMP_CFG_1 0x70 -#define MPUREG_DMP_CFG_2 0x71 -#define MPUREG_FIFO_COUNTH 0x72 -#define MPUREG_FIFO_COUNTL 0x73 -#define MPUREG_FIFO_R_W 0x74 -#define MPUREG_WHOAMI 0x75 +#define MPUREG_USER_CTRL 0x6A +#define MPUREG_PWR_MGMT_1 0x6B +#define MPUREG_PWR_MGMT_2 0x6C +#define MPUREG_BANK_SEL 0x6D +#define MPUREG_MEM_START_ADDR 0x6E +#define MPUREG_MEM_R_W 0x6F +#define MPUREG_DMP_CFG_1 0x70 +#define MPUREG_DMP_CFG_2 0x71 +#define MPUREG_FIFO_COUNTH 0x72 +#define MPUREG_FIFO_COUNTL 0x73 +#define MPUREG_FIFO_R_W 0x74 +#define MPUREG_WHOAMI 0x75 #define MPUREG_XA_OFFSET_H 0x77 #define MPUREG_XA_OFFSET_L 0x78 #define MPUREG_YA_OFFSET_H 0x7A diff --git a/src/madflight/imu/imu.h b/src/madflight/imu/imu.h index eb4f4c48..eb601af6 100644 --- a/src/madflight/imu/imu.h +++ b/src/madflight/imu/imu.h @@ -172,6 +172,7 @@ configures gyro and accel with 1000 Hz sample rate (with on sensor 200 Hz low pa #include "../interface.h" //Imu class declaration void _imu_ll_interrupt_setup(); //prototype +volatile bool _imu_ll_interrupt_enabled = false; volatile bool _imu_ll_interrupt_busy = false; volatile uint32_t _imu_ll_interrupt_ts = 0; @@ -180,6 +181,7 @@ bool Imu::hasMag() { return IMU_HAS_MAG; } //returns 0 on success, positive on error, negative on warning int Imu::setup(uint32_t sampleRate) { + _imu_ll_interrupt_enabled = false; int rv = imu_Sensor.begin(IMU_GYRO_DPS, IMU_ACCEL_G, sampleRate); _sampleRate = imu_Sensor.get_rate(); Serial.printf("IMU: " IMU_TYPE " sample_rate=%dHz rv=%d\n", (int)_sampleRate, (int)rv); @@ -194,6 +196,7 @@ int Imu::setup(uint32_t sampleRate) { dt = 0; ts = micros(); _imu_ll_interrupt_setup(); + _imu_ll_interrupt_enabled = true; return rv; } @@ -293,15 +296,16 @@ void _imu_ll_interrupt_handler(); #error #define IMU_EXEC is needed, see imu.h for available options #endif - //main IRQ handler - timestamp + busy wrapper around _imu_ll_interrupt_handler2() void _imu_ll_interrupt_handler() { _imu_ll_interrupt_ts = micros(); - if (_imu_ll_interrupt_busy) { //note: time difference between check/update of _imu_ll_interrupt_busy can cause a race condition... - imu.overrun_cnt++; - } else { - _imu_ll_interrupt_busy = true; - _imu_ll_interrupt_handler2(); - _imu_ll_interrupt_busy = false; + if(_imu_ll_interrupt_enabled) { + if (_imu_ll_interrupt_busy) { //note: time difference between check/update of _imu_ll_interrupt_busy can cause a race condition... + imu.overrun_cnt++; + } else { + _imu_ll_interrupt_busy = true; + _imu_ll_interrupt_handler2(); + _imu_ll_interrupt_busy = false; + } } } diff --git a/src/madflight/led/led.h b/src/madflight/led/led.h index 065b1a09..06c69e84 100644 --- a/src/madflight/led/led.h +++ b/src/madflight/led/led.h @@ -51,11 +51,13 @@ class LedSingle : public Led { void setup(int pin, uint8_t led_on_value) { this->pin = pin; this->led_on_value = led_on_value; + if(pin<0) return; pinMode(pin, OUTPUT); on(); } void set(bool set_on) { + if(pin<0) return; //if(led_state != set_on) Serial.printf("led_SwitchON(%d)\n", set_on); state = set_on; digitalWrite( pin, (set_on ? led_on_value : !led_on_value) ); diff --git a/src/madflight_board_RP2040-Zero.h b/src/madflight_board_RP2040-Zero.h new file mode 100644 index 00000000..a488bad8 --- /dev/null +++ b/src/madflight_board_RP2040-Zero.h @@ -0,0 +1,125 @@ +//This pin layout is optimized for Raspberry Pi Pico board: UART, PWM on side; I2C, SPI, PPM on the other side + +#define HW_BOARD_NAME "RP2040-Zero" +#define HW_MCU "RP2040" //RP2040 - not all pin combinations are allowed, see datasheet + +//------------------------------------- +// PIN DEFINITIONS +//------------------------------------- +//NOTE: DON'T USE SAME PIN TWICE. All pins here get configured, even if they are not used. Set pin to -1 to disable. + +//LED: +#ifndef HW_PIN_LED + #define HW_PIN_LED -1 //16 is internal RGB LED on RP2040-Zero +#endif +#ifndef HW_LED_ON + #define HW_LED_ON 1 //0:low is on, 1:high is on +#endif + +//IMU SPI: +#ifndef HW_PIN_SPI_MOSI + #define HW_PIN_SPI_MOSI 12 //spi0: 0, 4, 16(default) spi1: 8, 12(default) +#endif +#ifndef HW_PIN_SPI_MISO + #define HW_PIN_SPI_MISO 11 //spi0: 3, 7, 19(default) spi1: 11, 15(default) +#endif +#ifndef HW_PIN_SPI_SCLK + #define HW_PIN_SPI_SCLK 10 //spi0: 2, 6, 18(default) spi1: 10, 14(default) +#endif +#ifndef HW_PIN_IMU_CS + #define HW_PIN_IMU_CS 13 //spi0: 1, 5, 17(default) spi1: 9, 13(default) +#endif +#ifndef HW_PIN_IMU_EXTI + #define HW_PIN_IMU_EXTI 15 +#endif + +//BARO/MAG I2C: +#ifndef HW_PIN_I2C_SDA + #define HW_PIN_I2C_SDA 26 //Wire: 0, 4(default), 8, 12, 16, 20 Wire1: 2, 6, 10, 14, 18, 26(default) +#endif +#ifndef HW_PIN_I2C_SCL + #define HW_PIN_I2C_SCL 27 //Wire: 1, 5(default), 9, 13, 17, 21 Wire1: 3, 7, 11, 15, 19, 27(default) +#endif + +//Outputs: +#ifndef HW_OUT_COUNT + #define HW_OUT_COUNT 6 +#endif +#ifndef HW_PIN_OUT_LIST + #define HW_PIN_OUT_LIST {2,3,4,5,6,7} +#endif + +//Serial debug on USB Serial port (no GPIO pins) + +//RC Receiver: +#ifndef HW_PIN_RCIN_RX + #define HW_PIN_RCIN_RX 1 //uart0: 1(default), 5, 13, 17 uart1: 5, 9(default) , this pin is also used as PPM input +#endif +#ifndef HW_PIN_RCIN_TX + #define HW_PIN_RCIN_TX 0 //uart0: 0(default), 4, 12, 16 uart1: 4, 8(default) +#endif + +//GPS: +#ifndef HW_PIN_GPS_RX + #define HW_PIN_GPS_RX 9 //uart0: 1(default), 5, 13, 17 uart1: 5, 9(default) +#endif +#ifndef HW_PIN_GPS_TX + #define HW_PIN_GPS_TX 8 //uart0: 0(default), 4, 12, 16 uart1: 4, 8(default) +#endif + +//Battery ADC +#ifndef HW_PIN_BAT_V + #define HW_PIN_BAT_V 28 //pin A2 +#endif +#ifndef HW_PIN_BAT_I + #define HW_PIN_BAT_I -1 +#endif + +//BlackBox SPI: +#ifndef HW_PIN_SPI2_MISO + #define HW_PIN_SPI2_MISO -1 +#endif +#ifndef HW_PIN_SPI2_MOSI + #define HW_PIN_SPI2_MOSI -1 +#endif +#ifndef HW_PIN_SPI2_SCLK + #define HW_PIN_SPI2_SCLK -1 +#endif +#ifndef HW_PIN_BB_CS + #define HW_PIN_BB_CS -1 +#endif + +const int HW_PIN_OUT[] = HW_PIN_OUT_LIST; + +//------------------------------------- +//Include Libraries +//------------------------------------- +#include //set_sys_clock_khz() +#include //I2C communication +#include //SPI communication +#include "madflight/hw_RP2040/RP2040_PWM.h" //Servo and onshot +#include "madflight/hw_RP2040/RP2040_SerialIRQ.h" //Replacement high performance serial driver + +//------------------------------------- +//Bus Setup +//------------------------------------- + +// GPS Serial - uncomment one: SerialUART, SerialIRQ or SerialPIO and use uart0 or uart1 +uint8_t gps_txbuf[256]; +uint8_t gps_rxbuf[256]; +SerialIRQ gps_Serial(uart1, HW_PIN_GPS_TX, gps_txbuf, sizeof(gps_txbuf), HW_PIN_GPS_RX, gps_rxbuf, sizeof(gps_rxbuf)); //SerialIRQ uses buffered tx and rx in addition to the 32 byte uart hardware fifo +//SerialUART gps_Serial = SerialUART(uart1, HW_PIN_GPS_TX, HW_PIN_GPS_RX); //SerialUART default Arduino impementation (had some problems with this) +//SerialPIO gps_Serial = SerialPIO(HW_PIN_GPS_TX, HW_PIN_GPS_RX, 32); //PIO uarts, any pin allowed (not tested) + +// RC Input Serial - uncomment one: SerialUART, SerialIRQ or SerialPIO and use uart0 or uart1 +uint8_t rcin_txbuf[256]; +uint8_t rcin_rxbuf[1024]; +SerialIRQ *rcin_Serial = new SerialIRQ(uart0, HW_PIN_RCIN_TX, rcin_txbuf, sizeof(rcin_txbuf), HW_PIN_RCIN_RX, rcin_rxbuf, sizeof(rcin_rxbuf)); //SerialIRQ uses buffered tx and rx in addition to the 32 byte uart hardware fifo +//SerialUART rcin_Serial = SerialUART(uart0, HW_PIN_RCIN_TX, HW_PIN_RCIN_RX); //SerialUART default Arduino impementation (had some problems with this) +//SerialPIO *rcin_Serial = new SerialPIO(HW_PIN_RCIN_TX, HW_PIN_RCIN_RX, 32); //PIO uarts, any pin allowed (not tested) + +typedef TwoWire HW_WIRETYPE; //define the class to use for I2C +HW_WIRETYPE *i2c = &Wire1; //&Wire or &Wire1 + +SPIClassRP2040 *spi = new SPIClassRP2040(spi1, HW_PIN_SPI_MISO, HW_PIN_IMU_CS, HW_PIN_SPI_SCLK, HW_PIN_SPI_MOSI); //spi0 or spi1 +SPIClassRP2040 *bb_spi = new SPIClassRP2040(spi0, HW_PIN_SPI2_MISO, HW_PIN_BB_CS, HW_PIN_SPI2_SCLK, HW_PIN_SPI2_MOSI); //spi0 or spi1 diff --git a/src/madflight_board_default_ESP32.h b/src/madflight_board_default_ESP32.h index 0f959b61..b40a5506 100644 --- a/src/madflight_board_default_ESP32.h +++ b/src/madflight_board_default_ESP32.h @@ -27,7 +27,7 @@ #define HW_PIN_IMU_CS 18 // defaults: VSPI 5, HSPI 15 #endif #ifndef HW_PIN_IMU_EXTI - #define HW_PIN_IMU_EXTI 39 //VN only used when USE_IMU_INTERRUPT is defined + #define HW_PIN_IMU_EXTI 39 // VN silkscreen label #endif //BARO/MAG I2C: diff --git a/src/madflight_board_default_RP2040.h b/src/madflight_board_default_RP2040.h index f5e721a6..7143a895 100644 --- a/src/madflight_board_default_RP2040.h +++ b/src/madflight_board_default_RP2040.h @@ -30,7 +30,7 @@ #define HW_PIN_IMU_CS 17 //spi0: 1, 5, 17(default) spi1: 9, 13(default) #endif #ifndef HW_PIN_IMU_EXTI - #define HW_PIN_IMU_EXTI 22 //only used when USE_IMU_INTERRUPT is defined + #define HW_PIN_IMU_EXTI 22 #endif //BARO/MAG I2C: @@ -123,8 +123,3 @@ HW_WIRETYPE *i2c = &Wire; //&Wire or &Wire1 SPIClassRP2040 *spi = new SPIClassRP2040(spi0, HW_PIN_SPI_MISO, HW_PIN_IMU_CS, HW_PIN_SPI_SCLK, HW_PIN_SPI_MOSI); //spi0 or spi1 SPIClassRP2040 *bb_spi = new SPIClassRP2040(spi1, HW_PIN_SPI2_MISO, HW_PIN_BB_CS, HW_PIN_SPI2_SCLK, HW_PIN_SPI2_MOSI); //spi0 or spi1 - -// Default overclocking -//#ifndef HW_RP2040_SYS_CLK_KHZ -// #define HW_RP2040_SYS_CLK_KHZ 266000 //double speed overclocking: 266MHz vs 133Mhz -//#endif \ No newline at end of file