From ff73ab5a80bfc57fe93f0eb49e248beb926af106 Mon Sep 17 00:00:00 2001 From: gussy Date: Wed, 12 May 2010 08:18:49 +0000 Subject: [PATCH] More updates to cross-target compatibility. Added PIOS_I2C into AHRS code. Added PIOS_HMC5843 Module. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@617 ebee16cc-31ac-478f-84a7-5cbb03baadba --- flight/AHRS/Makefile | 3 +- flight/AHRS/ahrs.c | 18 ++++- flight/AHRS/inc/pios_board.h | 18 +++++ flight/AHRS/inc/pios_config.h | 14 +--- flight/OpenPilot/System/inc/pios_board.h | 6 ++ flight/PiOS/Common/pios_bmp085.c | 2 +- flight/PiOS/Common/pios_hmc5843.c | 95 ++++++++++++++++++++++++ flight/PiOS/STM32F10x/pios_i2c.c | 14 ++-- flight/PiOS/inc/pios_hmc5843.h | 44 +++++++++++ flight/PiOS/inc/pios_i2c.h | 2 +- flight/PiOS/pios.h | 4 + 11 files changed, 197 insertions(+), 23 deletions(-) create mode 100644 flight/PiOS/Common/pios_hmc5843.c create mode 100644 flight/PiOS/inc/pios_hmc5843.h diff --git a/flight/AHRS/Makefile b/flight/AHRS/Makefile index bbec1dcd7..f9906b940 100644 --- a/flight/AHRS/Makefile +++ b/flight/AHRS/Makefile @@ -94,11 +94,12 @@ SRC += $(PIOSSTM32F10X)/pios_delay.c SRC += $(PIOSSTM32F10X)/pios_usart.c SRC += $(PIOSSTM32F10X)/pios_irq.c SRC += $(PIOSSTM32F10X)/pios_adc.c -#SRC += $(PIOSSTM32F10X)/pios_i2c.c +SRC += $(PIOSSTM32F10X)/pios_i2c.c #SRC += $(PIOSSTM32F10X)/pios_spi.c ## PIOS Hardware (Common) SRC += $(PIOSCOMMON)/pios_com.c +SRC += $(PIOSCOMMON)/pios_hmc5843.c SRC += $(PIOSCOMMON)/printf-stdarg.c ## CMSIS for STM32 diff --git a/flight/AHRS/ahrs.c b/flight/AHRS/ahrs.c index 2e4afa136..725ae3c1d 100644 --- a/flight/AHRS/ahrs.c +++ b/flight/AHRS/ahrs.c @@ -50,13 +50,29 @@ int main() /* ADC system */ PIOS_ADC_Init(); + /* Magnetic sensor system */ + //PIOS_I2C_Init(); + //PIOS_HMC5843_Init(); + /* Toggle LED's forever */ PIOS_LED_On(LED1); + uint8_t buffer[3] = {0}; + int32_t result; + for(;;) { PIOS_LED_Toggle(LED1); + + // Test ADC PIOS_COM_SendFormattedString(COM_USART1, "%d,%d,%d,%d,%d,%d,%d,%d\r", PIOS_ADC_PinGet(0), PIOS_ADC_PinGet(1), PIOS_ADC_PinGet(2), PIOS_ADC_PinGet(3), PIOS_ADC_PinGet(4), PIOS_ADC_PinGet(5), PIOS_ADC_PinGet(6), PIOS_ADC_PinGet(7)); - PIOS_DELAY_WaitmS(25); + + /* + result = PIOS_HMC5843_Read(0x0A, buffer, 3); + PIOS_COM_SendFormattedString(COM_USART1, "Result: %d\r", result); + PIOS_COM_SendFormattedString(COM_USART1, "Ident: %s\r", buffer); + */ + + PIOS_DELAY_WaitmS(10); } diff --git a/flight/AHRS/inc/pios_board.h b/flight/AHRS/inc/pios_board.h index dde17d88d..9e67278d8 100644 --- a/flight/AHRS/inc/pios_board.h +++ b/flight/AHRS/inc/pios_board.h @@ -97,6 +97,24 @@ TIM8 | | | | #define PIOS_IRQ_PRIO_HIGH 5 // for SPI, ADC, I2C etc... #define PIOS_IRQ_PRIO_HIGHEST 4 // for USART etc... +//------------------------ +// PIOS_I2C +//------------------------ +#define PIOS_I2C_PORT I2C1 +#define PIOS_I2C_CLK RCC_APB1Periph_I2C1 +#define PIOS_I2C_GPIO_PORT GPIOB +#define PIOS_I2C_SCL_PIN GPIO_Pin_6 +#define PIOS_I2C_SDA_PIN GPIO_Pin_7 +#define PIOS_I2C_DUTY_CYCLE I2C_DutyCycle_2 +#define PIOS_I2C_BUS_FREQ 400000 +#define PIOS_I2C_TIMEOUT_VALUE 5000 +#define PIOS_I2C_IRQ_EV_HANDLER void I2C1_EV_IRQHandler(void) +#define PIOS_I2C_IRQ_ER_HANDLER void I2C1_ER_IRQHandler(void) +#define PIOS_I2C_IRQ_EV_CHANNEL I2C1_EV_IRQn +#define PIOS_I2C_IRQ_ER_CHANNEL I2C1_EV_IRQn +#define PIOS_I2C_IRQ_EV_PRIORITY 2 +#define PIOS_I2C_IRQ_ER_PRIORITY 2 + //------------------------- // PIOS_USART1 //------------------------- diff --git a/flight/AHRS/inc/pios_config.h b/flight/AHRS/inc/pios_config.h index 39ac69aec..c066d1b23 100644 --- a/flight/AHRS/inc/pios_config.h +++ b/flight/AHRS/inc/pios_config.h @@ -31,23 +31,13 @@ /* Enable/Disable PiOS Modules */ #define PIOS_INCLUDE_ADC #define PIOS_INCLUDE_DELAY -//#define PIOS_INCLUDE_I2C +#define PIOS_INCLUDE_I2C #define PIOS_INCLUDE_IRQ #define PIOS_INCLUDE_LED -//#define PIOS_INCLUDE_PWM -//#define PIOS_INCLUDE_SERVO -//#define PIOS_INCLUDE_SPI #define PIOS_INCLUDE_SYS #define PIOS_INCLUDE_USART -//#define PIOS_INCLUDE_USB_COM -//#define PIOS_INCLUDE_USB_HID -//#define PIOS_INCLUDE_USB -//#define PIOS_INCLUDE_BMP085 #define PIOS_INCLUDE_COM -//#define PIOS_INCLUDE_SDCARD -//#define PIOS_INCLUDE_SETTINGS - -#define portBASE_TYPE int32_t +#define PIOS_INCLUDE_HMC5843 #endif /* PIOS_CONFIG_H */ diff --git a/flight/OpenPilot/System/inc/pios_board.h b/flight/OpenPilot/System/inc/pios_board.h index a5863cca0..611e97e62 100644 --- a/flight/OpenPilot/System/inc/pios_board.h +++ b/flight/OpenPilot/System/inc/pios_board.h @@ -79,12 +79,18 @@ TIM8 | Servo 5 | Servo 6 | Servo 7 | Servo 8 //------------------------ // PIOS_I2C //------------------------ +#define PIOS_I2C_PORT I2C2 +#define PIOS_I2C_CLK RCC_APB1Periph_I2C2 #define PIOS_I2C_GPIO_PORT GPIOB #define PIOS_I2C_SDA_PIN GPIO_Pin_11 #define PIOS_I2C_SCL_PIN GPIO_Pin_10 #define PIOS_I2C_DUTY_CYCLE I2C_DutyCycle_2 #define PIOS_I2C_BUS_FREQ 400000 #define PIOS_I2C_TIMEOUT_VALUE 5000 +#define PIOS_I2C_IRQ_EV_HANDLER void I2C2_EV_IRQHandler(void) +#define PIOS_I2C_IRQ_ER_HANDLER void I2C2_ER_IRQHandler(void) +#define PIOS_I2C_IRQ_EV_CHANNEL I2C2_EV_IRQn +#define PIOS_I2C_IRQ_ER_CHANNEL I2C2_EV_IRQn #define PIOS_I2C_IRQ_EV_PRIORITY 2 #define PIOS_I2C_IRQ_ER_PRIORITY 2 diff --git a/flight/PiOS/Common/pios_bmp085.c b/flight/PiOS/Common/pios_bmp085.c index 5ccd79272..a092a1ace 100644 --- a/flight/PiOS/Common/pios_bmp085.c +++ b/flight/PiOS/Common/pios_bmp085.c @@ -3,7 +3,7 @@ * * @file pios_bmp085.c * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. - * @brief IRQ Enable/Disable routines + * @brief BMP085 Pressure Sensor Routines * @see The GNU Public License (GPL) Version 3 * @defgroup PIOS_BMP085 BMP085 Functions * @{ diff --git a/flight/PiOS/Common/pios_hmc5843.c b/flight/PiOS/Common/pios_hmc5843.c new file mode 100644 index 000000000..99af24dde --- /dev/null +++ b/flight/PiOS/Common/pios_hmc5843.c @@ -0,0 +1,95 @@ +/** + ****************************************************************************** + * + * @file pios_hmc5843.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief HMC5843 Magnetic Sensor Functions + * @see The GNU Public License (GPL) Version 3 + * @defgroup PIOS_HMC5843 HMC5843 Functions + * @{ + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Project Includes */ +#include "pios.h" + +#if defined(PIOS_INCLUDE_HMC5843) + +/* Glocal Variables */ + + +/* Local Variables */ + + +/** +* Initialise the HMC5843 sensor +*/ +void PIOS_HMC5843_Init(void) +{ + // Set in continuous mode + PIOS_HMC5843_Write(0x02, 0x00); +} + + +/** +* Reads one or more bytes into a buffer +* \param[in] address HMC5843 register address (depends on size) +* \param[out] buffer destination buffer +* \param[in] len number of bytes which should be read +* \return 0 if operation was successful +* \return -1 if error during I2C transfer +* \return -4 if invalid length +*/ +int32_t PIOS_HMC5843_Read(uint8_t address, uint8_t *buffer, uint8_t len) +{ + /* Send I2C address and register address */ + /* To avoid issues copy address into temporary buffer */ + uint8_t addr_buffer[1] = {(uint8_t)address}; + int32_t error = PIOS_I2C_Transfer(I2C_Write_WithoutStop, HMC5843_I2C_ADDR, addr_buffer, 1); + + /* Now receive byte(s) */ + if(!error) { + error = PIOS_I2C_Transfer(I2C_Read, HMC5843_I2C_ADDR, buffer, len); + } + + /* Return error status */ + return error < 0 ? -1 : 0; +} + + +/** +* Writes one or more bytes to the HMC5843 +* \param[in] address Register address +* \param[in] buffer source buffer +* \return 0 if operation was successful +* \return -1 if error during I2C transfer +*/ +int32_t PIOS_HMC5843_Write(uint8_t address, uint8_t buffer) +{ + /* Send I2C address and data */ + uint8_t WriteBuffer[2]; + WriteBuffer[0] = address; + WriteBuffer[1] = buffer; + + int32_t error = PIOS_I2C_Transfer(I2C_Write, HMC5843_I2C_ADDR, WriteBuffer, 2); + + /* Return error status */ + return error < 0 ? -1 : 0; +} + +#endif diff --git a/flight/PiOS/STM32F10x/pios_i2c.c b/flight/PiOS/STM32F10x/pios_i2c.c index f0c1bf16c..499030bda 100644 --- a/flight/PiOS/STM32F10x/pios_i2c.c +++ b/flight/PiOS/STM32F10x/pios_i2c.c @@ -139,15 +139,15 @@ int32_t PIOS_I2C_Init(void) TransferEnd(&I2CRec); - /* Configure and enable I2C2 interrupts */ + /* Configure and enable I2C interrupts */ NVIC_InitTypeDef NVIC_InitStructure; - NVIC_InitStructure.NVIC_IRQChannel = I2C2_EV_IRQn; + NVIC_InitStructure.NVIC_IRQChannel = PIOS_I2C_IRQ_EV_CHANNEL; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_I2C_IRQ_EV_PRIORITY; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); - NVIC_InitStructure.NVIC_IRQChannel = I2C2_ER_IRQn; + NVIC_InitStructure.NVIC_IRQChannel = PIOS_I2C_IRQ_ER_CHANNEL; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = PIOS_I2C_IRQ_ER_PRIORITY; NVIC_Init(&NVIC_InitStructure); @@ -174,10 +174,10 @@ static void PIOS_I2C_InitPeripheral(void) I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; /* Define base address */ - i2cx->base = I2C2; + i2cx->base = PIOS_I2C_PORT; /* enable peripheral clock of I2C */ - RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); + RCC_APB1PeriphClockCmd(PIOS_I2C_CLK, ENABLE); /* Set I2C clock bus clock params */ /* Note that the STM32 driver handles value <= 100kHz differently! (duty cycle always 1:1) */ @@ -650,12 +650,12 @@ static void ER_IRQHandler(I2CRecTypeDef *i2cx) /* Interrupt vectors */ -void I2C2_EV_IRQHandler(void) +PIOS_I2C_IRQ_EV_HANDLER { EV_IRQHandler((I2CRecTypeDef *)&I2CRec); } -void I2C2_ER_IRQHandler(void) +PIOS_I2C_IRQ_ER_HANDLER { ER_IRQHandler((I2CRecTypeDef *)&I2CRec); } diff --git a/flight/PiOS/inc/pios_hmc5843.h b/flight/PiOS/inc/pios_hmc5843.h new file mode 100644 index 000000000..050411b2b --- /dev/null +++ b/flight/PiOS/inc/pios_hmc5843.h @@ -0,0 +1,44 @@ +/** + ****************************************************************************** + * + * @file pios_hmc5843.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief HMC5843 functions header. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PIOS_HMC5843_H +#define PIOS_HMC5843_H + +/* BMP085 Addresses */ +#define HMC5843_I2C_ADDR 0x3C + + +/* Local Types */ + + +/* Global Variables */ + + +/* Public Functions */ +extern void PIOS_HMC5843_Init(void); +extern int32_t PIOS_HMC5843_Read(uint8_t address, uint8_t *buffer, uint8_t len); +extern int32_t PIOS_HMC5843_Write(uint8_t address, uint8_t buffer); + +#endif /* PIOS_HMC5843_H */ diff --git a/flight/PiOS/inc/pios_i2c.h b/flight/PiOS/inc/pios_i2c.h index af535bc1b..d2e16270c 100644 --- a/flight/PiOS/inc/pios_i2c.h +++ b/flight/PiOS/inc/pios_i2c.h @@ -37,7 +37,7 @@ #define I2C_ERROR_SLAVE_NOT_CONNECTED -7 #define I2C_ERROR_UNEXPECTED_EVENT -8 #define I2C_ERROR_RX_BUFFER_OVERRUN -9 -#define I2C_ERROR_BUSY -10 +#define I2C_ERROR_BUSY -10 /* Global Types */ diff --git a/flight/PiOS/pios.h b/flight/PiOS/pios.h index b3eda9faf..811df2f20 100644 --- a/flight/PiOS/pios.h +++ b/flight/PiOS/pios.h @@ -89,6 +89,10 @@ #if defined(PIOS_INCLUDE_BMP085) #include #endif +#if defined(PIOS_INCLUDE_HMC5843) +#include +#endif + #if defined(PIOS_INCLUDE_USB) /* USB Libs */