andrejpodzimek

Slow I2C hacks for Omnibus F7 v2 against v3.3.3

Aug 27th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 7.85 KB | None | 0 0
  1. diff --git a/src/main/drivers/bus_i2c_hal.c b/src/main/drivers/bus_i2c_hal.c
  2. index e3bc13da9..97e6ba9ec 100644
  3. --- a/src/main/drivers/bus_i2c_hal.c
  4. +++ b/src/main/drivers/bus_i2c_hal.c
  5. @@ -32,7 +32,7 @@
  6.  #include "drivers/bus_i2c.h"
  7.  #include "drivers/bus_i2c_impl.h"
  8.  
  9. -#define CLOCKSPEED 800000    // i2c clockspeed 400kHz default (conform specs), 800kHz  and  1200kHz (Betaflight default)
  10. +#define CLOCKSPEED 400000    // i2c clockspeed 400kHz default (conform specs), 800kHz  and  1200kHz (Betaflight default)
  11.  
  12.  // Number of bits in I2C protocol phase
  13.  #define LEN_ADDR 7
  14. @@ -261,9 +261,9 @@ void i2cInit(I2CDevice device)
  15.  
  16.      if (pDev->overClock) {
  17.          // 800khz Maximum speed tested on various boards without issues
  18. -        pHandle->Init.Timing = 0x00500D1D;
  19. +        pHandle->Init.Timing = 0x00401B1B;
  20.      } else {
  21. -        pHandle->Init.Timing = 0x00500C6F;
  22. +        pHandle->Init.Timing = 0x00A01B5B;
  23.      }
  24.  
  25.      pHandle->Init.OwnAddress1 = 0x0;
  26. diff --git a/src/main/drivers/bus_i2c_impl.h b/src/main/drivers/bus_i2c_impl.h
  27. index 42cb32751..372b28a14 100644
  28. --- a/src/main/drivers/bus_i2c_impl.h
  29. +++ b/src/main/drivers/bus_i2c_impl.h
  30. @@ -24,7 +24,7 @@
  31.  
  32.  #define I2C_SHORT_TIMEOUT            ((uint32_t)0x1000)
  33.  #define I2C_LONG_TIMEOUT             ((uint32_t)(10 * I2C_SHORT_TIMEOUT))
  34. -#define I2C_DEFAULT_TIMEOUT          I2C_SHORT_TIMEOUT
  35. +#define I2C_DEFAULT_TIMEOUT          I2C_LONG_TIMEOUT
  36.  
  37.  #define I2C_PIN_SEL_MAX 4
  38.  
  39. diff --git a/src/main/drivers/compass/compass_hmc5883l.c b/src/main/drivers/compass/compass_hmc5883l.c
  40. index 1a074a658..91461ea05 100644
  41. --- a/src/main/drivers/compass/compass_hmc5883l.c
  42. +++ b/src/main/drivers/compass/compass_hmc5883l.c
  43. @@ -198,11 +201,6 @@ static void hmc5883SpiInit(busDevice_t *busdev)
  44.  }
  45.  #endif
  46.  
  47. -static int16_t parseMag(uint8_t *raw, int16_t gain) {
  48. -  int ret = (int16_t)(raw[0] << 8 | raw[1]) * gain / 256;
  49. -  return constrain(ret, INT16_MIN, INT16_MAX);
  50. -}
  51. -
  52.  static bool hmc5883lRead(magDev_t *mag, int16_t *magData)
  53.  {
  54.      uint8_t buf[6];
  55. @@ -214,94 +211,27 @@ static bool hmc5883lRead(magDev_t *mag, int16_t *magData)
  56.      if (!ack) {
  57.          return false;
  58.      }
  59. -    // During calibration, magGain is 1.0, so the read returns normal non-calibrated values.
  60. -    // After calibration is done, magGain is set to calculated gain values.
  61.  
  62. -    magData[X] = parseMag(buf + 0, mag->magGain[X]);
  63. -    magData[Z] = parseMag(buf + 2, mag->magGain[Z]);
  64. -    magData[Y] = parseMag(buf + 4, mag->magGain[Y]);
  65. +    magData[X] = (int16_t)(buf[0] << 8 | buf[1]);
  66. +    magData[Z] = (int16_t)(buf[2] << 8 | buf[3]);
  67. +    magData[Y] = (int16_t)(buf[4] << 8 | buf[5]);
  68.  
  69.      return true;
  70.  }
  71.  
  72.  static bool hmc5883lInit(magDev_t *mag)
  73.  {
  74. -    enum {
  75. -        polPos,
  76. -        polNeg
  77. -    };
  78.  
  79.      busDevice_t *busdev = &mag->busdev;
  80.  
  81. -    int16_t magADC[3];
  82. -    int i;
  83. -    int32_t xyz_total[3] = { 0, 0, 0 }; // 32 bit totals so they won't overflow.
  84. -    bool bret = true;           // Error indicator
  85. -
  86. -    mag->magGain[X] = 256;
  87. -    mag->magGain[Y] = 256;
  88. -    mag->magGain[Z] = 256;
  89. -
  90. -    delay(50);
  91. -
  92. -    busWriteRegister(busdev, HMC58X3_REG_CONFA, HMC_CONFA_DOR_15HZ | HMC_CONFA_POS_BIAS);   // Reg A DOR = 0x010 + MS1, MS0 set to pos bias
  93. -
  94. -    // Note that the  very first measurement after a gain change maintains the same gain as the previous setting.
  95. -    // The new gain setting is effective from the second measurement and on.
  96. -
  97. -    busWriteRegister(busdev, HMC58X3_REG_CONFB, HMC_CONFB_GAIN_2_5GA); // Set the Gain to 2.5Ga (7:5->011)
  98. -
  99. -    delay(100);
  100. -
  101. -    hmc5883lRead(mag, magADC);
  102. -
  103. -    for (int polarity = polPos; polarity <= polNeg; polarity++) {
  104. -        switch(polarity) {
  105. -        case polPos:
  106. -            busWriteRegister(busdev, HMC58X3_REG_CONFA, HMC_CONFA_DOR_15HZ | HMC_CONFA_POS_BIAS);   // Reg A DOR = 0x010 + MS1, MS0 set to pos bias
  107. -            break;
  108. -        case polNeg:
  109. -            busWriteRegister(busdev, HMC58X3_REG_CONFA, HMC_CONFA_DOR_15HZ | HMC_CONFA_NEG_BIAS);   // Reg A DOR = 0x010 + MS1, MS0 set to negative bias.
  110. -            break;
  111. -        }
  112. -        for (i = 0; i < 10; i++) {  // Collect 10 samples
  113. -            busWriteRegister(busdev, HMC58X3_REG_MODE, HMC_MODE_SINGLE);
  114. -            delay(20);
  115. -            hmc5883lRead(mag, magADC);       // Get the raw values in case the scales have already been changed.
  116. -
  117. -            // Since the measurements are noisy, they should be averaged rather than taking the max.
  118. -
  119. -            xyz_total[X] += ((polarity == polPos) ? 1 : -1) * magADC[X];
  120. -            xyz_total[Y] += ((polarity == polPos) ? 1 : -1) * magADC[Y];
  121. -            xyz_total[Z] += ((polarity == polPos) ? 1 : -1) * magADC[Z];
  122. -
  123. -            // Detect saturation.
  124. -            if (-4096 >= MIN(magADC[X], MIN(magADC[Y], magADC[Z]))) {
  125. -                bret = false;
  126. -                break;              // Breaks out of the for loop.  No sense in continuing if we saturated.
  127. -            }
  128. -            LED1_TOGGLE;
  129. -        }
  130. -    }
  131. -
  132. -    mag->magGain[X] = (int)(660.0f * HMC58X3_X_SELF_TEST_GAUSS * 2.0f * 10.0f * 256.0f) / xyz_total[X];
  133. -    mag->magGain[Y] = (int)(660.0f * HMC58X3_Y_SELF_TEST_GAUSS * 2.0f * 10.0f * 256.0f) / xyz_total[Y];
  134. -    mag->magGain[Z] = (int)(660.0f * HMC58X3_Z_SELF_TEST_GAUSS * 2.0f * 10.0f * 256.0f) / xyz_total[Z];
  135.  
  136.      // leave test mode
  137. -
  138.      busWriteRegister(busdev, HMC58X3_REG_CONFA, HMC_CONFA_8_SAMLES | HMC_CONFA_DOR_15HZ | HMC_CONFA_NORMAL);    // Configuration Register A  -- 0 11 100 00  num samples: 8 ; output rate: 15Hz ; normal measurement mode
  139.      busWriteRegister(busdev, HMC58X3_REG_CONFB, HMC_CONFB_GAIN_1_3GA);                                          // Configuration Register B  -- 001 00000    configuration gain 1.3Ga
  140.      busWriteRegister(busdev, HMC58X3_REG_MODE, HMC_MODE_CONTINOUS);                                             // Mode register             -- 000000 00    continuous Conversion Mode
  141.  
  142.      delay(100);
  143.  
  144. -    if (!bret) {                // Something went wrong so get a best guess
  145. -        mag->magGain[X] = 256;
  146. -        mag->magGain[Y] = 256;
  147. -        mag->magGain[Z] = 256;
  148. -    }
  149. -
  150.      hmc5883lConfigureDataReadyInterruptHandling(mag);
  151.      return true;
  152.  }
  153. diff --git a/src/main/target/OMNIBUSF7/target.c b/src/main/target/OMNIBUSF7/target.c
  154. index e559da763..2e3941d90 100644
  155. --- a/src/main/target/OMNIBUSF7/target.c
  156. +++ b/src/main/target/OMNIBUSF7/target.c
  157. @@ -43,7 +43,6 @@ const timerHardware_t timerHardware[USABLE_TIMER_CHANNEL_COUNT] = {
  158.      DEF_TIM(TIM8, CH2, PC7,  TIM_USE_NONE,  0, 0 ), // UART6_RX
  159.      DEF_TIM(TIM2, CH4, PA3,  TIM_USE_PPM,   0, 0 ), // UART2_RX, joined with PE13
  160.  
  161. -    // For ESC serial
  162. -    DEF_TIM(TIM9, CH1, PA2,  TIM_USE_NONE,  0, 0 ), // UART2_TX (unwired)
  163. -
  164. +    // SmartPort on Softserial
  165. +    DEF_TIM(TIM9, CH2, PE6,  TIM_USE_NONE,  0, 0 ), // MOSI -> Softserial
  166.  };
  167. diff --git a/src/main/target/common_fc_pre.h b/src/main/target/common_fc_pre.h
  168. index 7ca03d37a..428ab5735 100644
  169. --- a/src/main/target/common_fc_pre.h
  170. +++ b/src/main/target/common_fc_pre.h
  171. @@ -28,8 +28,8 @@
  172.  //#define SCHEDULER_DEBUG // define this to use scheduler debug[] values. Undefined by default for performance reasons
  173.  #define DEBUG_MODE DEBUG_NONE // change this to change initial debug mode
  174.  
  175. -#define I2C1_OVERCLOCK true
  176. -#define I2C2_OVERCLOCK true
  177. +#define I2C1_OVERCLOCK false
  178. +#define I2C2_OVERCLOCK false
  179.  
  180.  #ifdef STM32F1
  181.  #define MINIMAL_CLI
  182. @@ -47,7 +47,7 @@
  183.  #ifdef STM32F4
  184.  #define USE_DSHOT
  185.  #define USE_ESC_SENSOR
  186. -#define I2C3_OVERCLOCK true
  187. +#define I2C3_OVERCLOCK false
  188.  #define USE_GYRO_DATA_ANALYSE
  189.  #define USE_ADC
  190.  #define USE_ADC_INTERNAL
  191. @@ -64,8 +64,8 @@
  192.  #ifdef STM32F7
  193.  #define USE_DSHOT
  194.  #define USE_ESC_SENSOR
  195. -#define I2C3_OVERCLOCK true
  196. -#define I2C4_OVERCLOCK true
  197. +#define I2C3_OVERCLOCK false
  198. +#define I2C4_OVERCLOCK false
  199.  #define USE_GYRO_DATA_ANALYSE
  200.  #endif
Add Comment
Please, Sign In to add comment