diff --git a/UAV-ControlSystem/inc/drivers/I2C.h b/UAV-ControlSystem/inc/drivers/I2C.h index 07750b3..01c7e65 100644 --- a/UAV-ControlSystem/inc/drivers/I2C.h +++ b/UAV-ControlSystem/inc/drivers/I2C.h @@ -1,9 +1,15 @@ -/* - * I2C.h - * - * Created on: 21 sep. 2016 - * Author: len12007 - */ +/*************************************************************************** +* NAME: I2C.h * +* AUTHOR: Lennart Eriksson * +* PURPOSE: Enabole the I2C Communication channel on the Revo board * +* INFORMATION: * +* This file initilizes the I2C communication that can be used by barometer * +* communication etc. * +* * +* GLOBAL VARIABLES: * +* Variable Type Description * +* -------- ---- ----------- * +***************************************************************************/ #ifndef DRIVERS_I2C_H_ #define DRIVERS_I2C_H_ @@ -12,9 +18,12 @@ /****************************************************************************** * BRIEF: Configure the I2C bus to be used * -* INFORMATION: * +* INFORMATION: This function only implements I2C1 or I2C2 which are available * +* on the REVO board * ******************************************************************************/ -void i2c_configure(I2C_TypeDef* i2c, uint16_t sda_pin, uint16_t scl_pin, uint32_t my_address); +bool i2c_configure(I2C_TypeDef* i2c, + I2C_HandleTypeDef* out_profile, + uint32_t my_address); /****************************************************************************** * BRIEF: Get data over the I2C bus * @@ -25,7 +34,10 @@ void i2c_configure(I2C_TypeDef* i2c, uint16_t sda_pin, uint16_t scl_pin, uint32_ * the data is sent one byte at a time with an acknowledge in between * * every byte, as per the standard. Returns true if successful * ******************************************************************************/ -bool i2c_receive(uint8_t slave_address, uint8_t* buffer, uint32_t length); +bool i2c_receive(I2C_HandleTypeDef* profile, + uint8_t slave_address, + uint8_t* buffer, + uint32_t length); /****************************************************************************** * BRIEF: Send data over the I2C bus * @@ -36,7 +48,64 @@ bool i2c_receive(uint8_t slave_address, uint8_t* buffer, uint32_t length); * the data is sent one byte at a time with an acknowledge in between * * every byte, as per the standard. Returns true if successful * ******************************************************************************/ -bool i2c_send(uint8_t slave_address, uint8_t* data, uint32_t length); +bool i2c_send(I2C_HandleTypeDef* profile, + uint8_t slave_address, + uint8_t* data, + uint32_t length); #endif /* DRIVERS_I2C_H_ */ + + + + +// ---------------------- I2C Working with the compas on the REVO board ------------------------------ + +/* +int main(void) +{ + + // Initialize the Hardware Abstraction Layer + HAL_Init(); + + // Configure the system clock to 100 MHz + system_clock_config(); + + I2C_HandleTypeDef i2c_profile; + + + //---- COMPAS WORKING ---- + i2c_configure(I2C1, &i2c_profile, 0x56); + + + + uint32_t address = 0b00011110; + uint8_t start_request_1[2] = { 0b00000000, 0b01110000 }; + uint8_t start_request_2[2] = { 0b00000001, 0b10100000 }; + uint8_t start_request_3[2] = { 0b00000010, 0b00000000 }; + + + uint8_t request_data[1] = { 0b00000110 }; + uint8_t reset_pointer_data[1] = { 0b00000011 }; + uint8_t response_data[6] = { 0x0 }; + + + i2c_send(&i2c_profile, address, &start_request_1, 2); + i2c_send(&i2c_profile, address, &start_request_2, 2); + i2c_send(&i2c_profile, address, &start_request_3, 2); + + // Delay for at least 6 ms for system startup to finish + HAL_Delay(10); + + while (1) + { + i2c_send(&i2c_profile, address, &request_data, 1); + i2c_receive(&i2c_profile, address, &response_data, 6); + i2c_send(&i2c_profile, address, &reset_pointer_data, 1); + + // HAL_Delay(100); + if(response_data[0] != 0) + response_data[0] = 0; + } +} +*/ diff --git a/UAV-ControlSystem/inc/stm32f4xx_revo.h b/UAV-ControlSystem/inc/stm32f4xx_revo.h index fa46876..6854d54 100644 --- a/UAV-ControlSystem/inc/stm32f4xx_revo.h +++ b/UAV-ControlSystem/inc/stm32f4xx_revo.h @@ -83,6 +83,16 @@ #define USART6_TX_PORT GPIOC +/* I2C */ +#define I2C1_SDA_PIN GPIO_PIN_9 +#define I2C1_SCL_PIN GPIO_PIN_8 +#define I2C1_PORT GPIOB + +#define I2C2_SCL_PIN GPIO_PIN_10 +#define I2C2_SDA_PIN GPIO_PIN_11 +#define I2C2_PORT GPIOB + + /* Gyro */ #define GYRO #define MPU6000_CS_PIN GPIO_PIN_4 diff --git a/UAV-ControlSystem/src/drivers/I2C.c b/UAV-ControlSystem/src/drivers/I2C.c index c93dd0a..f978e40 100644 --- a/UAV-ControlSystem/src/drivers/I2C.c +++ b/UAV-ControlSystem/src/drivers/I2C.c @@ -1,39 +1,59 @@ -/* - * I2C.c - * - * Created on: 21 sep. 2016 - * Author: len12007 - */ +/*************************************************************************** +* NAME: I2C.c * +* AUTHOR: Lennart Eriksson * +* PURPOSE: Enabole the I2C Communication channel on the Revo board * +* INFORMATION: * +* This file initilizes the I2C communication that can be used by barometer * +* communication etc. * +* * +* GLOBAL VARIABLES: * +* Variable Type Description * +* -------- ---- ----------- * +***************************************************************************/ #include "drivers/I2C.h" - -// the I2C handle sending data over -I2C_HandleTypeDef I2C_handle; +#include "stm32f4xx_revo.h" /****************************************************************************** * BRIEF: Configure the I2C bus to be used * * INFORMATION: * ******************************************************************************/ -void i2c_configure(I2C_TypeDef* i2c, uint16_t sda_pin, uint16_t scl_pin, uint32_t my_address) +bool i2c_configure(I2C_TypeDef *i2c, + I2C_HandleTypeDef *out_profile, + uint32_t my_address) { uint8_t i2c_af; + uint16_t sda_pin, scl_pin; + GPIO_TypeDef *i2c_port; - // get the correct alternate function for the selected I2C + // get the correct alternate function and pins for the selected I2C + // Only I2C1 and I2C2 is available on the REVO board if(i2c == I2C1) + { i2c_af = GPIO_AF4_I2C1; + i2c_port = I2C1_PORT; + sda_pin = I2C1_SDA_PIN; + scl_pin = I2C1_SCL_PIN; + if(__HAL_RCC_I2C1_IS_CLK_DISABLED()) + __HAL_RCC_I2C1_CLK_ENABLE(); + } else if(i2c == I2C2) + { i2c_af = GPIO_AF4_I2C2; - else if(i2c == I2C3) - i2c_af = GPIO_AF4_I2C3; + i2c_port = I2C2_PORT; + sda_pin = I2C2_SDA_PIN; + scl_pin = I2C2_SCL_PIN; + if(__HAL_RCC_I2C2_IS_CLK_DISABLED()) + __HAL_RCC_I2C2_CLK_ENABLE(); + } else - i2c_af = GPIO_AF4_I2C1; - + return false; // The provided I2C is not correct + if(__HAL_RCC_GPIOB_IS_CLK_DISABLED()) + __HAL_RCC_GPIOB_CLK_ENABLE(); //Initialize pins for SCL and SDA GPIO_InitTypeDef GPIO_InitStruct; - __HAL_RCC_I2C1_CLK_ENABLE(); - __GPIOB_CLK_ENABLE(); GPIO_InitStruct.Pin = sda_pin | scl_pin; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; @@ -42,16 +62,19 @@ void i2c_configure(I2C_TypeDef* i2c, uint16_t sda_pin, uint16_t scl_pin, uint32_ HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); //Initialize I2C communication - I2C_handle.Instance = i2c; - I2C_handle.Init.ClockSpeed = 100000; - I2C_handle.Init.DutyCycle = I2C_DUTYCYCLE_2; - I2C_handle.Init.OwnAddress1 = my_address; - I2C_handle.Init.OwnAddress2 = 0; - I2C_handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; - I2C_handle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; - I2C_handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; - I2C_handle.Init.NoStretchMode = I2C_NOSTRETCH_ENABLE; - HAL_I2C_Init(&I2C_handle); + out_profile->Instance = i2c; + out_profile->Init.ClockSpeed = 100000; + out_profile->Init.DutyCycle = I2C_DUTYCYCLE_2; + out_profile->Init.OwnAddress1 = my_address; + out_profile->Init.OwnAddress2 = 0; + out_profile->Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; + out_profile->Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; + out_profile->Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; + out_profile->Init.NoStretchMode = I2C_NOSTRETCH_ENABLE; + if(HAL_I2C_Init(out_profile) != HAL_OK) + return false; + + return true; } @@ -64,12 +87,15 @@ void i2c_configure(I2C_TypeDef* i2c, uint16_t sda_pin, uint16_t scl_pin, uint32_ * the data is sent one byte at a time with an acknowledge in between * * every byte, as per the standard. Returns true if successful * ******************************************************************************/ -bool i2c_receive(uint8_t slave_address, uint8_t* buffer, uint32_t length) +bool i2c_receive(I2C_HandleTypeDef* profile, + uint8_t slave_address, + uint8_t* buffer, + uint32_t length) { uint8_t i = 0; - while(HAL_I2C_Master_Receive(&I2C_handle, (slave_address << 1) | 1, buffer, length, 1000)!= HAL_OK && i++ < 10) + while(HAL_I2C_Master_Receive(profile, (slave_address << 1) | 1, buffer, length, 1000)!= HAL_OK && i++ < 10) {} - while (HAL_I2C_GetState(&I2C_handle) != HAL_I2C_STATE_READY) + while (HAL_I2C_GetState(profile) != HAL_I2C_STATE_READY) {} return (i < 10); @@ -84,16 +110,19 @@ bool i2c_receive(uint8_t slave_address, uint8_t* buffer, uint32_t length) * the data is sent one byte at a time with an acknowledge in between * * every byte, as per the standard. Returns true if successful * ***************************************************************************/ -bool i2c_send(uint8_t slave_address, uint8_t* data, uint32_t length) +bool i2c_send(I2C_HandleTypeDef* profile, + uint8_t slave_address, + uint8_t* data, + uint32_t length) { uint8_t i = 0; // try to send the data 10 times and if no acknowledge is received during these 10 messages we stop trying so that // the system don't gets stuck forever because a slave is unreachable - while(HAL_I2C_Master_Transmit(&I2C_handle,(slave_address << 1), (uint8_t*)data, length, 1000) != HAL_OK && i++ < 10) + while(HAL_I2C_Master_Transmit(profile,(slave_address << 1), (uint8_t*)data, length, 1000) != HAL_OK && i++ < 10) {} //Wait til the I2C bus is done with all sending - while (HAL_I2C_GetState(&I2C_handle) != HAL_I2C_STATE_READY){} + while (HAL_I2C_GetState(profile) != HAL_I2C_STATE_READY){} return (i < 10); } diff --git a/UAV-ControlSystem/src/drivers/i2c_test.c b/UAV-ControlSystem/src/drivers/i2c_test.c deleted file mode 100644 index dca5bc6..0000000 --- a/UAV-ControlSystem/src/drivers/i2c_test.c +++ /dev/null @@ -1,297 +0,0 @@ -// -//#include "stm32f4xx.h" -// -////_____ M A C R O S -//#define TRUE 1 -//#define FALSE 0 -//#define F_CPU 4000000UL // 4 MHz external XTAL -//#define SCL_CLOCK 100000L // I2C clock in Hz -//#define ADDR_W 0xEE // Module address write mode -//#define ADDR_R 0xEF // Module address read mode -//#define CMD_RESET 0x1E // ADC reset command -//#define CMD_ADC_READ 0x00 // ADC read command -//#define CMD_ADC_CONV 0x40 // ADC conversion command -//#define CMD_ADC_D1 0x00 // ADC D1 conversion -//#define CMD_ADC_D2 0x10 // ADC D2 conversion -//#define CMD_ADC_256 0x00 // ADC OSR=256 -//#define CMD_ADC_512 0x02 // ADC OSR=512 -//#define CMD_ADC_1024 0x04 // ADC OSR=1024 -//#define CMD_ADC_2048 0x06 // ADC OSR=2048 -//#define CMD_ADC_4096 0x08 // ADC OSR=4096 -//#define CMD_PROM_RD 0xA0 // Prom read command -////_____ I N C L U D E S -//#include -////#include -////#include -////#include -//#include -////_____ D E F I N I T I O N S -//unsigned char i2c_start(unsigned char address); -//void i2c_stop(void); -//unsigned char i2c_write( unsigned char data ); -//unsigned char i2c_readAck(void); -//unsigned char i2c_readNak(void); -//void cmd_reset(void); -//unsigned long cmd_adc(char cmd); -//unsigned int cmd_prom(char coef_num); -//unsigned char crc4(unsigned int n_prom[]); -////******************************************************** -////! @brief send I2C start condition and the address byte -////! -////! @return 0 -////******************************************************** -//unsigned char i2c_start(unsigned char address) -//{ -// unsigned char twst; -// -// I2C1->CR1 = 1 << 8 | 1 << 0; -// -// TWCR = (1<>1]) & 0x00FF); -//else n_rem ^= (unsigned short) (n_prom[cnt>>1]>>8); -//for (n_bit = 8; n_bit > 0; n_bit--) -//{ -//if (n_rem & (0x8000)) -//{ -//n_rem = (n_rem << 1) ^ 0x3000; } -//else -//{ -//n_rem = (n_rem << 1); -//} -//} -//} -//n_rem= (0x000F & (n_rem >> 12)); // final 4-bit reminder is CRC code -//n_prom[7]=crc_read; // restore the crc_read to its original place -//return (n_rem ^ 0x0); -//} -////******************************************************** -////! @brief main program -////! -////! @return 0 -////******************************************************** -//int main (void) -//{ -//unsigned long D1; // ADC value of the pressure conversion -//unsigned long D2; // ADC value of the temperature conversion -//unsigned int C[8]; // calibration coefficients -//double P; // compensated pressure value -//double T; // compensated temperature value -//double dT; // difference between actual and measured temperature -//double OFF; // offset at actual temperature -//double SENS; // sensitivity at actual temperature -//int i; -//unsigned char n_crc; // crc value of the prom -// -//// setup the ports -//DDRA = 0xFE; -//DDRB = 0x0F; //SPI pins as input -//DDRC = 0x03; // I2C pins as output -//DDRD = 0x82; // RS out and tx out; -// -//PORTA = 0x1F; // I2C pin high -//PORTB = 0xF0; -//PORTC = 0x01; -//PORTD = 0x00; -// -//// initialize the I2C hardware module -//TWSR = 0; // no prescaler -//TWBR = ((F_CPU/SCL_CLOCK)-16)/2; // set the I2C speed -// -//D1=0; -//D2=0; -//cmd_reset(); // reset IC -//for (i=0;i<8;i++){ C[i]=cmd_prom(i);} // read coefficients -//n_crc=crc4(C); // calculate the CRC -//for(;;) // loop without stopping -//{ -//D2=cmd_adc(CMD_ADC_D2+CMD_ADC_4096); // read D2 -//D1=cmd_adc(CMD_ADC_D1+CMD_ADC_4096); // read D1 -//// calcualte 1st order pressure and temperature (MS5607 1st order algorithm) -//dT=D2-C[5]*pow(2,8); -//OFF=C[2]*pow(2,17)+dT*C[4]/pow(2,6); -//SENS=C[1]*pow(2,16)+dT*C[3]/pow(2,7); -// -//T=(2000+(dT*C[6])/pow(2,23))/100; -//P=(((D1*SENS)/pow(2,21)-OFF)/pow(2,15))/100; -//// place to use P, T, put them on LCD, send them trough RS232 interface... -//} -// -//return 0; -//} diff --git a/UAV-ControlSystem/src/main.c b/UAV-ControlSystem/src/main.c index fe16f0b..e69de29 100644 --- a/UAV-ControlSystem/src/main.c +++ b/UAV-ControlSystem/src/main.c @@ -1,108 +0,0 @@ -/** - ****************************************************************************** - * @file main.c - * @author Ac6 - * @version V1.0 - * @date 01-December-2013 - * @brief Default main function. - * Awesome ADC fix start project here to be good awesome!! - ****************************************************************************** -*/ - - -#include "drivers/adc.h" -#include "drivers/system_clock.h" -#include "drivers/I2C.h" -#include "stm32f4xx.h" -#include "system_variables.h" -#include "utilities.h" -#include - - -int main(void) -{ - - // Initialize the Hardware Abstraction Layer - HAL_Init(); - - // Configure the system clock to 100 MHz - system_clock_config(); - - - -/* COMPAS WORKING - i2c_configure(I2C1, GPIO_PIN_8, GPIO_PIN_9, 0x56); - - - - uint32_t address = 0b00011110; - uint8_t start_request_1[2] = { 0b00000000, 0b01110000 }; - uint8_t start_request_2[2] = { 0b00000001, 0b10100000 }; - uint8_t start_request_3[2] = { 0b00000010, 0b00000000 }; - - - uint8_t request_data[1] = { 0b00000110 }; - uint8_t reset_pointer_data[1] = { 0b00000011 }; - uint8_t response_data[6] = { 0x0 }; - - - i2c_send(address, &start_request_1, 2); - i2c_send(address, &start_request_2, 2); - i2c_send(address, &start_request_3, 2); - - // Delay for at least 6 ms for system startup to finish - HAL_Delay(10); - - while (1) - { - i2c_send(address, &request_data, 1); - i2c_receive(address, &response_data, 6); - i2c_send(address, &reset_pointer_data, 1); - - // HAL_Delay(100); - if(response_data[0] != 0) - response_data[0] = 0; - } -*/ - - - i2c_configure(I2C1, GPIO_PIN_8, GPIO_PIN_9, 0x56); - uint32_t address = 0x77; - uint8_t reset_data[1] = { 0x1E }; - - uint8_t init_data[1] = { 0xA2 }; - uint8_t init_rsp_data[2] = { 0x00 }; - - uint8_t request_data[500] = { 0xFF }; - uint8_t response_data[2] = { 0x0 }; - -// while(count--) - i2c_send(address, address, 1); - i2c_send(reset_data[0], &reset_data, 1); - - HAL_Delay(50); - - for(int i = 0; i < 6; i++) - { - if(i2c_send(address, &init_data, 1) == true) - i2c_receive(address, &response_data, 2); - init_data[0] += 2; - } - - - HAL_Delay(20); - - while (1) - { - if(i2c_send(address, &request_data, 1) == true) - i2c_receive(address, &response_data, 2); - -// HAL_Delay(100); - if(response_data[0] != 0) - response_data[0] = 0; - } - - for(;;); -} - -