#include <stdio.h>
-#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
};
-
-
-
-
-
void i2c_init(uint8_t id, uint32_t speed)
{
if (!((id >= 1) && (id <= 3)))
}
-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 ; i<cNumByteToWrite ; i++)
- {
- /* Send the byte to be written */
- I2C_SendData(I2C2, pcBuffer[i]);
-
- /* Test on EV8 and clear it */
- while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
- }
-
- /* Send STOP condition */
- I2C_GenerateSTOP(I2C2, ENABLE);
-
-#ifdef FORCE_CRITICAL_SEC
- __enable_irq();
-#endif
-
+
+void i2c_write_dma(uint8_t id, uint8_t *buffer, uint8_t nb)
+{
+
}
+