From 204d25d491885dd9396735ff89eed5dcc2a287be Mon Sep 17 00:00:00 2001 From: timecop Date: Tue, 13 Mar 2012 15:32:38 +0000 Subject: [PATCH] added i2cWriteBuffer() for ledring stuff fixed ledring stuff fixed mag orientation (thanks GrootWitBaas) git-svn-id: https://afrodevices.googlecode.com/svn/trunk/baseflight@111 7c89a4a9-59b9-e629-4cfe-3a2d53b20e61 --- baseflight.uvopt | 44 ++++++++++++++++++++++---------------------- src/drv_i2c.c | 22 +++++++++++++++++----- src/drv_i2c.h | 1 + src/drv_ledring.c | 10 +++++----- src/sensors.c | 9 ++++++++- 5 files changed, 53 insertions(+), 33 deletions(-) diff --git a/baseflight.uvopt b/baseflight.uvopt index e72f7e5c9..252100a03 100755 --- a/baseflight.uvopt +++ b/baseflight.uvopt @@ -502,10 +502,10 @@ 1 0 0 - 0 + 30 0 - 1 - 1 + 55 + 68 0 .\src\config.c config.c @@ -572,10 +572,10 @@ 1 0 0 - 1 + 0 0 - 264 - 264 + 216 + 229 0 .\src\sensors.c sensors.c @@ -602,8 +602,8 @@ 0 0 0 - 0 - 0 + 35 + 72 0 .\src\board.h board.h @@ -614,10 +614,10 @@ 5 0 0 - 20 + 14 0 - 151 - 167 + 241 + 249 0 .\src\mw.h mw.h @@ -679,8 +679,8 @@ 0 0 0 - 0 - 0 + 28 + 51 0 .\src\drv_hmc5883l.c drv_hmc5883l.c @@ -689,12 +689,12 @@ 2 15 1 - 0 + 1 0 - 11 + 81 0 - 121 - 121 + 106 + 140 0 .\src\drv_i2c.c drv_i2c.c @@ -719,10 +719,10 @@ 1 0 0 - 0 + 5 0 - 0 - 0 + 71 + 87 0 .\src\drv_pwm.c drv_pwm.c @@ -761,10 +761,10 @@ 1 0 0 - 7 + 17 0 17 - 52 + 35 0 .\src\drv_ledring.c drv_ledring.c diff --git a/src/drv_i2c.c b/src/drv_i2c.c index f032b535c..5677b2016 100755 --- a/src/drv_i2c.c +++ b/src/drv_i2c.c @@ -73,20 +73,27 @@ static void i2c_er_handler(void) busy = 0; } -bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data) +bool i2cWriteBuffer(uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data) { - uint8_t my_data[1]; + uint8_t i; + uint8_t my_data[8]; uint32_t timeout = I2C_DEFAULT_TIMEOUT; addr = addr_ << 1; reg = reg_; writing = 1; reading = 0; - my_data[0] = data; write_p = my_data; read_p = my_data; - bytes = 1; + bytes = len_; busy = 1; + + // too long + if (len_ > 7) + return false; + + for (i = 0; i < len_; i++) + my_data[i] = data[i]; if (!(I2Cx->CR2 & I2C_IT_EVT)) { //if we are restarting the driver if (!(I2Cx->CR1 & 0x0100)) { // ensure sending a start @@ -95,7 +102,7 @@ bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data) } I2C_ITConfig(I2Cx, I2C_IT_EVT | I2C_IT_ERR, ENABLE); //allow the interrupts to fire off again } - + while (busy && --timeout > 0); if (timeout == 0) { i2cErrorCount++; @@ -107,6 +114,11 @@ bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data) return true; } +bool i2cWrite(uint8_t addr_, uint8_t reg_, uint8_t data) +{ + return i2cWriteBuffer(addr_, reg_, 1, &data); +} + bool i2cRead(uint8_t addr_, uint8_t reg_, uint8_t len, uint8_t* buf) { uint32_t timeout = I2C_DEFAULT_TIMEOUT; diff --git a/src/drv_i2c.h b/src/drv_i2c.h index f7148d701..1a9fe114d 100755 --- a/src/drv_i2c.h +++ b/src/drv_i2c.h @@ -1,6 +1,7 @@ #pragma once void i2cInit(I2C_TypeDef *I2Cx); +bool i2cWriteBuffer(uint8_t addr_, uint8_t reg_, uint8_t len_, uint8_t *data); bool i2cWrite(uint8_t addr_, uint8_t reg, uint8_t data); bool i2cRead(uint8_t addr_, uint8_t reg, uint8_t len, uint8_t* buf); uint16_t i2cGetErrorCounter(void); diff --git a/src/drv_ledring.c b/src/drv_ledring.c index 71e345299..6d9f56e4c 100644 --- a/src/drv_ledring.c +++ b/src/drv_ledring.c @@ -9,7 +9,7 @@ bool ledringDetect(void) bool ack = false; uint8_t sig = 'e'; - ack = 0; // i2cWrite(LED_RING_ADDRESS, 0xFF, 1, &sig); + ack = i2cWrite(LED_RING_ADDRESS, 0xFF, sig); if (!ack) return false; return true; @@ -23,13 +23,13 @@ void ledringState(void) if (state == 0) { b[0] = 'z'; b[1] = (180 - heading) / 2; // 1 unit = 2 degrees; - // i2cWrite(LED_RING_ADDRESS, 0xFF, 2, b); + i2cWriteBuffer(LED_RING_ADDRESS, 0xFF, 2, b); state = 1; } else if (state == 1) { b[0] = 'y'; b[1] = constrain(angle[ROLL] / 10 + 90, 0, 180); b[2] = constrain(angle[PITCH] / 10 + 90, 0, 180); - // i2cWrite(LED_RING_ADDRESS, 0xFF, 3, b); + i2cWriteBuffer(LED_RING_ADDRESS, 0xFF, 3, b); state = 2; } else if (state == 2) { b[0] = 'd'; // all unicolor GREEN @@ -38,7 +38,7 @@ void ledringState(void) b[2] = 1; else b[2] = 0; - // i2cWrite(LED_RING_ADDRESS, 0xFF, 3, b); + i2cWriteBuffer(LED_RING_ADDRESS, 0xFF, 3, b); state = 0; } } @@ -49,5 +49,5 @@ void ledringBlink(void) b[0] = 'k'; b[1] = 10; b[2] = 10; - // i2cWrite(LED_RING_ADDRESS, 0xFF, 3, b); + i2cWriteBuffer(LED_RING_ADDRESS, 0xFF, 3, b); } diff --git a/src/sensors.c b/src/sensors.c index 37f7941fa..2de2f48c9 100755 --- a/src/sensors.c +++ b/src/sensors.c @@ -228,10 +228,17 @@ static void Mag_getRawADC(void) static int16_t rawADC[3]; hmc5883lRead(rawADC); - // no way? is this finally the proper orientation?? +#if 0 + // no way? is this finally the proper orientation?? (seems -180 swapped) magADC[ROLL] = -rawADC[2]; // X magADC[PITCH] = rawADC[0]; // Y magADC[YAW] = rawADC[1]; // Z +#else + // no way? is THIS finally the proper orientation?? (by GrootWitBaas) + magADC[ROLL] = rawADC[2]; // X + magADC[PITCH] = -rawADC[0]; // Y + magADC[YAW] = -rawADC[1]; // Z +#endif } void Mag_init(void)