From 61bad71df3543031acfe39ffab7388fb41d21165 Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Mon, 21 Oct 2013 08:28:44 +0000 Subject: [PATCH] i2c working. However, the app crash when accelerometer and gyroscope work together git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@125 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Makefile | 15 +- lib/HAL_L3Gx.c | 26 +-- lib/HAL_L3Gx.h | 8 +- lib/HAL_LSM303DLHC.h | 19 +- lib/L3Gx/src/L3Gx.dep | 12 -- lib/Makefile | 3 +- lib/i2c.c | 443 ++++++------------------------------------ lib/i2c.h | 18 +- lib/spi.c | 1 - lib/spi.h | 2 +- multitouchglove.c | 42 ++-- 11 files changed, 122 insertions(+), 467 deletions(-) delete mode 100644 lib/L3Gx/src/L3Gx.dep diff --git a/Makefile b/Makefile index 7a643c9..b128c7c 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,7 @@ ROOTDIR = $(CURDIR)/uC-sdk export BOARD = inemo TARGET = multitouchglove.bin -TARGET_OBJS = lib/spi.o lib/uart.o lib/i2c.o lib/HAL_L3Gx.o lib/L3Gx/src/L3Gx.o lib/LSM303DLHC/src/LSM303DLHC.o - -#lib/HAL_LSM303DLHC.o +TARGET_OBJS = lib/spi.o lib/uart.o lib/i2c.o lib/HAL_L3Gx.o lib/HAL_LSM303DLHC.o lib/L3Gx/src/L3Gx.o lib/LSM303DLHC/src/LSM303DLHC.o LIBDEPS = uC-sdk/FreeRTOS/libFreeRTOS.a uC-sdk/arch/libarch.a uC-sdk/os/libos.a uC-sdk/libc/libc.a uC-sdk/libm/libm.a uC-sdk/acorn/libacorn.a LIBS = -Wl,--start-group $(LIBDEPS) -Wl,--end-group @@ -17,14 +15,14 @@ export ROOTDIR = $(MAINDIR)/uC-sdk/ include $(MAINDIR)/lib/config.mk include $(ROOTDIR)/common.mk -all: uC-sdk lib $(TARGET) +all: deps uC-sdk lib $(TARGET) clean: clean-generic $(Q)rm -f $(TARGET) $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk clean $(Q)$(MAKE) $(MAKE_OPTS) -C lib clean -.PHONY: uC-sdk +.PHONY: uC-sdk lib uC-sdk/FreeRTOS/libFreeRTOS.a: uC-sdk uC-sdk/arch/libarch.a: uC-sdk @@ -41,14 +39,11 @@ uC-sdk: $(E) "[MAKE] Entering uC-sdk" $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk -romfs.o: - $(E) "[ROMFS] Building romfs" - $(Q)uC-sdk/tools/mkromfs -d romfs romfs.bin - $(Q)$(TARGET_OBJCOPY_BIN) --prefix-sections '.romfs' romfs.bin romfs.o - deps: ldeps $(E) "[DEPS] Creating dependency tree for uC-sdk" $(Q)$(MAKE) $(MAKE_OPTS) -C uC-sdk ldeps + $(E) "[DEPS] Creating dependency tree for lib" + $(Q)$(MAKE) $(MAKE_OPTS) -C lib ldeps include $(ROOTDIR)/FreeRTOS/config.mk include $(ROOTDIR)/arch/config.mk diff --git a/lib/HAL_L3Gx.c b/lib/HAL_L3Gx.c index 117ab94..1952de8 100644 --- a/lib/HAL_L3Gx.c +++ b/lib/HAL_L3Gx.c @@ -3,43 +3,37 @@ #include "stm32f10x.h" #include -void spi_get(uint8_t *buffer, uint8_t address, uint8_t nb) +void spi_get(uint8_t address, uint8_t *buffer, uint8_t nb) { - //set the read bit + //set the read bit address |= 0xC0; //reset multiple byte bit if necessary if (nb <= 1) address &= 0xBF; - - spi_start(2); - - //send the request + spi_start(2); + //send the request spi_write(2, &address, 1); //get the value spi_read(2, buffer, nb); - - spi_stop(2); + spi_stop(2); } -void spi_set(uint8_t *buffer, uint8_t address, uint8_t nb) +void spi_set(uint8_t address, uint8_t *buffer, uint8_t nb) { - //reset the read bit + //reset the read bit address &= 0x3F; //set multiple byte bit if necessary if (nb > 1) address |= 0x40; - - spi_start(2); - - //send the request + spi_start(2); + //send the request spi_write(2, &address, 1); //send the value spi_write(2, buffer, nb); - - spi_stop(2); + spi_stop(2); } diff --git a/lib/HAL_L3Gx.h b/lib/HAL_L3Gx.h index b6b3dbc..cceb362 100755 --- a/lib/HAL_L3Gx.h +++ b/lib/HAL_L3Gx.h @@ -8,10 +8,10 @@ #include "spi.h" #define L3gxCommInit() spi_init(2) -#define L3gxBufferRead(pVal,cAddress,nBytes) spi_get(pVal,cAddress,nBytes) -#define L3gxBufferWrite(pVal,cAddress,nBytes) spi_set(pVal,cAddress,nBytes) +#define L3gxBufferRead(buffer, address, nb) spi_get(address, buffer, nb) +#define L3gxBufferWrite(buffer, address, nb) spi_set(address, buffer, nb) -void spi_get(uint8_t *buffer, uint8_t address, uint8_t nb); -void spi_set(uint8_t *pcBuffer, uint8_t address, uint8_t nb); +void spi_get(uint8_t address, uint8_t *buffer, uint8_t nb); +void spi_set(uint8_t address, uint8_t *buffer, uint8_t nb); #endif diff --git a/lib/HAL_LSM303DLHC.h b/lib/HAL_LSM303DLHC.h index a623082..80bc74f 100755 --- a/lib/HAL_LSM303DLHC.h +++ b/lib/HAL_LSM303DLHC.h @@ -8,20 +8,11 @@ #include "stm32f10x.h" #include "i2c.h" - -#define LSM_I2C I2C2 -#define LSM_I2C_Speed 400000 +#define Lsm303dlhcI2CInit() i2c_init(2, LSM_I2C_Speed) +#define Lsm303dlhcI2CBufferRead(dest, buffer, address, nb) i2c_get(dest, address, buffer, nb) +#define Lsm303dlhcI2CBufferWrite(dest, buffer, address, nb) i2c_set(dest, address, buffer, nb) -#define LSM_A_INT1_Pin GPIO_Pin_2 -#define LSM_A_INT1_Port GPIOD -#define LSM_A_INT1_RCC_Port RCC_APB2Periph_GPIOD - -#define LSM_A_INT2_Pin GPIO_Pin_5 -#define LSM_A_INT2_Port GPIOB -#define LSM_A_INT2_RCC_Port RCC_APB2Periph_GPIOB - -#define Lsm303dlhcI2CInit() i2c_init(2, LSM_I2C_Speed) -#define Lsm303dlhcI2CBufferRead(cDevAddress,pVal,cAddress,nBytes) iNemoI2CBufferRead(cDevAddress,pVal,cAddress,nBytes) -#define Lsm303dlhcI2CBufferWrite(cDevAddress,pVal,cAddress,nBytes) iNemoI2CBufferWrite(cDevAddress,pVal,cAddress,nBytes) +void i2c_get(uint8_t destination, uint8_t address, uint8_t *buffer, uint8_t nb); +void i2c_set(uint8_t destination, uint8_t address, uint8_t *pcBuffer, uint8_t nb); #endif diff --git a/lib/L3Gx/src/L3Gx.dep b/lib/L3Gx/src/L3Gx.dep deleted file mode 100644 index a05f898..0000000 --- a/lib/L3Gx/src/L3Gx.dep +++ /dev/null @@ -1,12 +0,0 @@ -L3Gx/src/L3Gx.o: L3Gx/src/L3Gx.c \ - /Users/tom/Documents/src/multitouchglove/lib/L3Gx/inc/L3Gx.h \ - /usr/local/bin/../lib/gcc/arm-none-eabi/4.7.4/include/stdint.h \ - /usr/local/bin/../lib/gcc/arm-none-eabi/4.7.4/include/stdint-gcc.h \ - /Users/tom/Documents/src/multitouchglove/lib/HAL_L3Gx.h \ - /Users/tom/Documents/src/multitouchglove/lib/spi.h \ - /Users/tom/Documents/src/multitouchglove/uC-sdk/arch/arm/stm32f10x/Core/CM3/DeviceSupport/ST/STM32F10x/stm32f10x.h \ - /Users/tom/Documents/src/multitouchglove/uC-sdk/arch/arm/src/CM3/CoreSupport/core_cm3.h \ - /Users/tom/Documents/src/multitouchglove/uC-sdk/arch/arm/stm32f10x/Core/CM3/DeviceSupport/ST/STM32F10x/system_stm32f10x.h \ - /Users/tom/Documents/src/multitouchglove/uC-sdk/config/arm/stm32f10/stm32f10x_conf.h \ - /Users/tom/Documents/src/multitouchglove/uC-sdk/arch/arm/stm32f10x/inemo/BoardConsole.h \ - /usr/local/bin/../lib/gcc/arm-none-eabi/4.7.4/include/stdarg.h diff --git a/lib/Makefile b/lib/Makefile index 8f07f07..97e330c 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -1,6 +1,6 @@ #TARGET_LIB = libinemo.a -all: +all: $(TARGET_OBJ) # $(TARGET_LIB) export BOARD = inemo @@ -15,6 +15,7 @@ TARGET_SRCS += uart.c TARGET_SRCS += spi.c TARGET_SRCS += i2c.c TARGET_SRCS += HAL_L3Gx.c +TARGET_SRCS += HAL_LSM303DLHC.c #TARGET_SRCS += i2c.c #TARGET_SRCS = iNEMO_Compass/src/iNEMO_Compass.c #TARGET_SRCS += iNEMO_M1_SensorDrivers/src/iNEMO_I2C_Driver.c diff --git a/lib/i2c.c b/lib/i2c.c index 6dcb8cd..c72f126 100644 --- a/lib/i2c.c +++ b/lib/i2c.c @@ -7,20 +7,6 @@ #include -#define FORCE_CRITICAL_SEC - -#define I2C1_DR_Address 0x40005410 -#define I2C2_DR_Address 0x40005810 - -#define I2C1_DMA_CHANNEL_TX DMA1_Channel6 -#define I2C1_DMA_CHANNEL_RX DMA1_Channel7 -#define I2C2_DMA_CHANNEL_TX DMA1_Channel4 -#define I2C2_DMA_CHANNEL_RX DMA1_Channel5 - -#define I2C_DIRECTION_TX 0 -#define I2C_DIRECTION_RX 1 - -#define DMA_BUFFER_SIZE 196 struct i2cInitDef_t { // scl / sda @@ -44,11 +30,6 @@ static struct i2cInitDef_t i2cInitDefs[2] = { }; - - - - - void i2c_init(uint8_t id, uint32_t speed) { if (!((id >= 1) && (id <= 3))) @@ -88,400 +69,86 @@ void i2c_dma_init(uint8_t id) } -void uart_read_polling(uint8_t id, uint8_t *buffer, uint8_t nb) +void i2c_start_read(uint8_t id, uint8_t destination) { + if (!((id >= 1) && (id <= 3))) + return; -} - -void uart_write_polling(uint8_t id, uint8_t *buffer, uint8_t nb) -{ + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + I2C_GenerateSTART(i2cInitDef->id, ENABLE); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_MODE_SELECT)); + I2C_Send7bitAddress(i2cInitDef->id, destination, I2C_Direction_Receiver); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); } -void uart_read_dma(uint8_t id, uint8_t *buffer, uint8_t nb) +void i2c_start_write(uint8_t id, uint8_t destination) { + if (!((id >= 1) && (id <= 3))) + return; -} - -void uart_write_dma(uint8_t id, uint8_t *buffer, uint8_t nb) -{ + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + I2C_GenerateSTART(i2cInitDef->id, ENABLE); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_MODE_SELECT)); + I2C_Send7bitAddress(i2cInitDef->id, destination, I2C_Direction_Transmitter); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); } +void i2c_stop(uint8_t id) +{ + if (!((id >= 1) && (id <= 3))) + return; + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; - -#if 0 + I2C_GenerateSTOP(i2cInitDef->id, ENABLE); +} -/** - * @brief Reads a block of data from the device by DMA. - * @brief I2C2: I2C peripherial to use. - * @param cAddr: slave address. - * @param pcBuffer: pointer to the buffer that receives the data read. - * @param cReadAddr: register internal address to read from. - * @param nNumByteToRead: number of bytes to read. - * @retval None - */ -void iNemoI2CBufferReadDma(uint8_t cAddr, uint8_t* pcBuffer, uint8_t cReadAddr, uint8_t cNumByteToRead) +void i2c_read_polling(uint8_t id, uint8_t *buffer, uint8_t nb) { - - __IO uint32_t temp = 0; - __IO uint32_t Timeout = 0; - - /* Enable I2C errors interrupts */ - I2C2->CR2 |= I2C_IT_ERR; - - /* Set the MSb of the register address in case of multiple readings */ - if(cNumByteToRead>1) - cReadAddr |= 0x80; - -#ifdef FORCE_CRITICAL_SEC - __disable_irq(); -#endif - - /* While the bus is busy */ - while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)); - - /* Send START condition */ - I2C_GenerateSTART(I2C2, ENABLE); - - /* Test on EV5 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)); - - /* Send LSM303DLH address for read */ - I2C_Send7bitAddress(I2C2, cAddr, I2C_Direction_Transmitter); - - /* Test on EV6 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); - - /* Clear EV6 by setting again the PE bit */ - I2C_Cmd(I2C2, ENABLE); - - /* Send the LSM303DLH_Magn's internal address to write to */ - I2C_SendData(I2C2, cReadAddr); - - /* Test on EV8 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - - /* Configure I2C2 DMA channel */ - iNemoI2CDMAConfig(I2C2, pcBuffer, cNumByteToRead, I2C_DIRECTION_RX); - - /* Set Last bit to have a NACK on the last received byte */ - I2C2->CR2 |= 0x1000; - - /* Enable I2C DMA requests */ - I2C_DMACmd(I2C2, ENABLE); - Timeout = 0xFFFF; - - /* Send START condition */ - I2C_GenerateSTART(I2C2, ENABLE); - - /* Wait until SB flag is set: EV5 */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)) - { - if (Timeout-- == 0) - return; - } - Timeout = 0xFFFF; - - /* Send LSM303DLH address for read */ - I2C_Send7bitAddress(I2C2, cAddr, I2C_Direction_Receiver); - - /* Wait until ADDR is set: EV6 */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) - { - if (Timeout-- == 0) + if (!((id >= 1) && (id <= 3))) return; - } - /* Clear ADDR flag by reading SR2 register */ - temp = I2C2->SR2; - - - - if(I2C2 == I2C2) - { - /* Wait until DMA end of transfer */ - while (!DMA_GetFlagStatus(DMA1_FLAG_TC5)); - /* Disable DMA Channel */ - DMA_Cmd(I2C2_DMA_CHANNEL_RX, DISABLE); - - /* Clear the DMA Transfer Complete flag */ - DMA_ClearFlag(DMA1_FLAG_TC5); - } - else + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + while(nb--) { - /* Wait until DMA end of transfer */ - while (!DMA_GetFlagStatus(DMA1_FLAG_TC7)); - /* Disable DMA Channel */ - DMA_Cmd(I2C1_DMA_CHANNEL_RX, DISABLE); + if(nb == 0) + I2C_AcknowledgeConfig(i2cInitDef->id, DISABLE); - /* Clear the DMA Transfer Complete flag */ - DMA_ClearFlag(DMA1_FLAG_TC7); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_BYTE_RECEIVED)); + *buffer++ = I2C_ReceiveData(i2cInitDef->id); } - - /* Disable Ack for the last byte */ - I2C_AcknowledgeConfig(I2C2, DISABLE); - - /* Send STOP Condition */ - I2C_GenerateSTOP(I2C2, ENABLE); - - /* Make sure that the STOP bit is cleared by Hardware before CR1 write access */ - while ((I2C2->CR1 & 0x0200) == 0x0200); - - /* Enable Acknowledgement to be ready for another reception */ - I2C_AcknowledgeConfig(I2C2, ENABLE); - -#ifdef FORCE_CRITICAL_SEC - __enable_irq(); -#endif - + I2C_AcknowledgeConfig(i2cInitDef->id, ENABLE); } -#endif - -/** - * @brief Reads a block of data from the device by polling. - * @brief I2C2: I2C peripherial to use. - * @param cAddr: slave address. - * @param pcBuffer: pointer to the buffer that receives the data read. - * @param cReadAddr: register internal address to read from. - * @param nNumByteToRead: number of bytes to read. - * @retval None - */ -void iNemoI2CBufferRead(uint8_t cAddr, uint8_t* pcBuffer, uint8_t cReadAddr, uint8_t cNumByteToRead) + +void i2c_write_polling(uint8_t id, uint8_t *buffer, uint8_t nb) { - /* Set the MSb of the register address in case of multiple readings */ - if(cNumByteToRead>1) - cReadAddr |= 0x80; - -#ifdef FORCE_CRITICAL_SEC - __disable_irq(); -#endif - - /* While the bus is busy */ - while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)); - - /* Send START condition */ - I2C_GenerateSTART(I2C2, ENABLE); - - /* Test on EV5 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)); - - /* Send LSM303DLH address for write */ - I2C_Send7bitAddress(I2C2, cAddr, I2C_Direction_Transmitter); - - /* Test on EV6 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); - - /* Clear EV6 by setting again the PE bit */ - I2C_Cmd(I2C2, ENABLE); - - /* Send the LSM303DLH_Magn's internal address to write to */ - I2C_SendData(I2C2, cReadAddr); - - /* Test on EV8 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - - /* Send START condition a second time */ - I2C_GenerateSTART(I2C2, ENABLE); - - /* Test on EV5 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)); - - /* Send LSM303DLH address for read */ - I2C_Send7bitAddress(I2C2, cAddr, I2C_Direction_Receiver); - - /* Test on EV6 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); - - /* While there is data to be read */ - while(cNumByteToRead) + if (!((id >= 1) && (id <= 3))) + return; + + struct i2cInitDef_t * i2cInitDef = i2cInitDefs + id - 1; + + taskENTER_CRITICAL(); + + while (nb--) { - if(cNumByteToRead == 1) - { - /* Disable Acknowledgement */ - I2C_AcknowledgeConfig(I2C2, DISABLE); - - /* Send STOP Condition */ - I2C_GenerateSTOP(I2C2, ENABLE); - } - - /* Test on EV7 and clear it */ - if(I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_RECEIVED)) - { - /* Read a byte from the LSM303DLH */ - *pcBuffer = I2C_ReceiveData(I2C2); - - /* Point to the next location where the byte read will be saved */ - pcBuffer++; - - /* Decrement the read bytes counter */ - cNumByteToRead--; - } + I2C_SendData(i2cInitDef->id, *buffer++); + while(!I2C_CheckEvent(i2cInitDef->id, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); } - - /* Enable Acknowledgement to be ready for another reception */ - I2C_AcknowledgeConfig(I2C2, ENABLE); - -#ifdef FORCE_CRITICAL_SEC - __enable_irq(); -#endif - + taskEXIT_CRITICAL(); } - -#if 0 -/** - * @brief Writes a block of data to the device by DMA. - * @brief I2C2: I2C peripherial to use. - * @param cAddr : slave address. - * @param pcBuffer : pointer to the buffer containing the data to be written. - * @param cWriteAddr : register internal address to write to. - * @param nNumByteToWrite : number of bytes to write. - * @retval None - */ -void iNemoI2CBufferWriteDma(uint8_t cAddr, uint8_t* pcBuffer, uint8_t cWriteAddr, uint8_t cNumByteToWrite) + +void i2c_read_dma(uint8_t id, uint8_t *buffer, uint8_t nb) { - - __IO uint32_t temp = 0; - __IO uint32_t Timeout = 0; - - static uint8_t pcDmaBuffer[DMA_BUFFER_SIZE+1]; - - /* Set to 1 the MSb of the register address in case of multiple byte writing */ - if(cNumByteToWrite>1) - cWriteAddr |= 0x80; - - pcDmaBuffer[0]=cWriteAddr; - memcpy(&pcDmaBuffer[1],pcBuffer,cNumByteToWrite); - - /* Enable Error IT */ - I2C2->CR2 |= I2C_IT_ERR; - - Timeout = 0xFFFF; - /* Configure the DMA channel for I2C2 transmission */ - iNemoI2CDMAConfig(I2C2, pcDmaBuffer, cNumByteToWrite+1, I2C_DIRECTION_TX); - - /* Enable DMA for I2C */ - I2C_DMACmd(I2C2, ENABLE); - - /* Send START condition */ - I2C_GenerateSTART(I2C2, ENABLE); - - - /* Wait until SB flag is set: EV5 */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)) - { - if (Timeout-- == 0) - return; - } - - Timeout = 0xFFFF; - - /* Send LSM303DLH address for write */ - I2C_Send7bitAddress(I2C2, cAddr, I2C_Direction_Transmitter); - - /* Wait until ADDR is set: EV6 */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) - { - if (Timeout-- == 0) - return; - } - - /* Clear ADDR flag by reading SR2 register */ - temp = I2C2->SR2; - - - /* Disable the DMA1 channel */ - if(I2C2 == I2C2) - { - /* Wait until DMA end of transfer */ - while (!DMA_GetFlagStatus(DMA1_FLAG_TC4)); - /* Disable DMA Channel */ - DMA_Cmd(I2C2_DMA_CHANNEL_TX, DISABLE); - - /* Clear the DMA Transfer complete flag */ - DMA_ClearFlag(DMA1_FLAG_TC4); - } - else - { - /* Wait until DMA end of transfer */ - while (!DMA_GetFlagStatus(DMA1_FLAG_TC6)); - /* Disable DMA Channel */ - DMA_Cmd(I2C1_DMA_CHANNEL_TX, DISABLE); - - /* Clear the DMA Transfer complete flag */ - DMA_ClearFlag(DMA1_FLAG_TC6); - } - - - /* EV8_2: Wait until BTF is set before programming the STOP */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - - /* Send STOP Condition */ - I2C_GenerateSTOP(I2C2, ENABLE); - - /* Make sure that the STOP bit is cleared by Hardware before CR1 write access */ - while ((I2C2->CR1 & 0x0200) == 0x0200); - + } -#endif - -/** - * @brief Writes a block of data to the device by polling. - * @brief I2C2: I2C peripherial to use. - * @param cAddr : slave address. - * @param pcBuffer : pointer to the buffer containing the data to be written. - * @param cWriteAddr : register internal address to write to. - * @param nNumByteToWrite : number of bytes to write. - * @retval None - */ -void iNemoI2CBufferWrite(uint8_t cAddr, uint8_t* pcBuffer, uint8_t cWriteAddr, uint8_t cNumByteToWrite) -{ - /* Set to 1 the MSb of the register address in case of multiple byte writing */ - if(cNumByteToWrite>1) - cWriteAddr |= 0x80; - -#ifdef FORCE_CRITICAL_SEC - __disable_irq(); -#endif - - /* While the bus is busy */ - while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY)); - - /* Send START condition */ - I2C_GenerateSTART(I2C2, ENABLE); - - /* Test on EV5 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT)); - - /* Send LSM303DLH address for write */ - I2C_Send7bitAddress(I2C2, cAddr, I2C_Direction_Transmitter); - - /* Test on EV6 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)); - - /* Send the LSM303DLHC_internal register address to write */ - I2C_SendData(I2C2, cWriteAddr); - - /* Test on EV8 and clear it */ - while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED)); - - uint8_t i; - for(i=0 ; iid, SPI_I2S_FLAG_RXNE) == RESET); *buffer++ = (uint8_t) SPI_I2S_ReceiveData(spiInitDef->id); - //printf("%02x", *buffer); taskEXIT_CRITICAL(); } diff --git a/lib/spi.h b/lib/spi.h index 58ff450..ec05e59 100644 --- a/lib/spi.h +++ b/lib/spi.h @@ -14,6 +14,6 @@ void spi_stop(uint8_t id); //read data from SPI void spi_read(uint8_t id, uint8_t *buffer, uint8_t nb); //write data to SPI -void spi_write(uint8_t id, uint8_t *address, uint8_t nb); +void spi_write(uint8_t id, uint8_t *buffer, uint8_t nb); #endif diff --git a/multitouchglove.c b/multitouchglove.c index 7a6c34e..fbf35d2 100644 --- a/multitouchglove.c +++ b/multitouchglove.c @@ -15,7 +15,7 @@ #include #define LEDS -#define GYRO +//#define GYRO #define ACCELERO #ifdef LEDS @@ -72,16 +72,16 @@ void initGyro() void getGyroInfo() { - L3GInit L3GInitStructure; + L3GInit gyroinfo; - L3gd20GetInfo(&L3GInitStructure); + L3gd20GetInfo(&gyroinfo); - printf("Power mode: %02x\n", L3GInitStructure.xPowerMode); - printf("Output data rate: %02x\n", L3GInitStructure.xOutputDataRate); - printf("Enabled axes: %02x\n", L3GInitStructure.xEnabledAxes); - printf("Full scale: %02x\n", L3GInitStructure.xFullScale); - printf("Data update: %02x\n", L3GInitStructure.xDataUpdate); - printf("Endianness: %02x\n", L3GInitStructure.xEndianness); + printf("Power mode: %02x\n", gyroinfo.xPowerMode); + printf("Output data rate: %02x\n", gyroinfo.xOutputDataRate); + printf("Enabled axes: %02x\n", gyroinfo.xEnabledAxes); + printf("Full scale: %02x\n", gyroinfo.xFullScale); + printf("Data update: %02x\n", gyroinfo.xDataUpdate); + printf("Endianness: %02x\n", gyroinfo.xEndianness); } static void gyroTask(void *p) @@ -118,6 +118,21 @@ void initAccelero() Lsm303dlhcAccConfig(&accelerodef); } +void getAcceleroInfo() +{ + LSMAccInit acceleroinfo; + + Lsm303dlhcAccGetInfo(&acceleroinfo); + + printf("Power mode: %02x\n", acceleroinfo.xPowerMode); + printf("Output data rate: %02x\n", acceleroinfo.xOutputDataRate); + printf("Enabled axes: %02x\n", acceleroinfo.xEnabledAxes); + printf("Full scale: %02x\n", acceleroinfo.xFullScale); + printf("Data update: %02x\n", acceleroinfo.xDataUpdate); + printf("Endianness: %02x\n", acceleroinfo.xEndianness); + printf("High Resolution: %02x\n", acceleroinfo.xHighResolution); +} + static void acceleroTask(void *p) { printf("Start accelero task\n"); @@ -147,18 +162,21 @@ int main() #endif #ifdef GYRO - printf("Init SPI 2\n"); + printf("Init SPI2\n"); spi_init(2); - getGyroInfo(); printf("Init Gyro\n"); initGyro(); + printf("Get Gyro Information\n"); + getGyroInfo(); printf("Start gyroTask\n"); xTaskCreate(gyroTask, (const signed char *)NULL, configMINIMAL_STACK_SIZE, (void *)NULL, tskIDLE_PRIORITY, NULL); #endif #ifdef ACCELERO - printf("Init I2C2 2\n"); + printf("Init I2C2\n"); i2c_init(2, 400000); + printf("Get Accelero Information\n"); + getAcceleroInfo(); printf("Init Accelero\n"); initAccelero(); printf("Start acceleroTask\n"); -- 2.30.2