diff --git a/ked/csl/interfaces/i2c.h b/ked/csl/interfaces/i2c.h index 59bc4ef..5d466b7 100755 --- a/ked/csl/interfaces/i2c.h +++ b/ked/csl/interfaces/i2c.h @@ -256,7 +256,7 @@ void i2cRead(i2c_t *i2cHardware, uint16_t *devAddress, uint8_t *registerAddress, * @param registerAddress is the regiter to be red from the device * @param data is the data whic has been red */ -void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t *registerAddress, uint8_t *data); +void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAddress, uint8_t *data); /** * @brief Sends a Single Byte as master to the given devices register. * this function will adapt himself to the selected oppeartion mode (Polling / Int / DMA) diff --git a/ked/csl/stm32f042/Src/i2c.c b/ked/csl/stm32f042/Src/i2c.c index 34fc6e6..ca346d7 100644 --- a/ked/csl/stm32f042/Src/i2c.c +++ b/ked/csl/stm32f042/Src/i2c.c @@ -34,26 +34,29 @@ void i2cInit(i2c_t *i2cHardware) I2C_BASE->CR1 &= ~I2C_CR1_PE; //Configure analog filter. Anlalog filter is on - I2C_BASE->CR1 &=~ I2C_CR1_ANFOFF; + //I2C_BASE->CR1 |= I2C_CR1_ANFOFF; //Configure NoStrech Streching mode is disabled (slave only) - I2C_BASE->CR1 |= I2C_CR1_NOSTRETCH; + //I2C_BASE->CR1 |= I2C_CR1_NOSTRETCH; //Configure the clock I2C_BASE->TIMINGR = i2cHardware->timing; //Automatic end mode (master mode) Enablede as default - I2C_BASE->CR2 |= I2C_CR2_AUTOEND; + I2C_BASE->CR2 &= ~I2C_CR2_AUTOEND; + //I2C_BASE->CR2 |= I2C_CR2_AUTOEND; if(i2cHardware->mode == i2cModeMaster) { if(i2cHardware->addressSize == i2cAddressSizeTenBits) { I2C_BASE->CR2 |= I2C_CR2_ADD10; // 10 Bit addressing + I2C_BASE->CR2 &= ~I2C_CR2_HEAD10R; // 7 Bit header read turned on DEFAULT } else { I2C_BASE->CR2 &= ~I2C_CR2_ADD10; // 7 Bit addressing DEFAULT + I2C_BASE->CR2 |= I2C_CR2_HEAD10R; // 7 Bit header read turned off DEFAULT } } @@ -97,36 +100,40 @@ void i2cRead( i2c_t *i2cHardware, } // this function still doesn't implment 10 bit oopeartion TODO -void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t *registerAddress, uint8_t *data) +void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAddress, uint8_t *data) { // Wait until no communication is ongoign - while(I2C_BASE->ISR & (I2C_ISR_BUSY)); + while((I2C_BASE->ISR & (I2C_ISR_BUSY))==I2C_ISR_BUSY); i2cHardware->state = i2cRecieving; //Slave address - I2C_BASE->CR2 |= devAddress << 1; // The bit no 0 is not taken in concideration in 7bit mode + I2C_BASE->CR2 |= 0x40 << 1; // The bit no 0 is not taken in concideration in 7bit mode //Read Mode - I2C_BASE->CR2 |= I2C_CR2_RD_WRN; + I2C_BASE->CR2 &= ~I2C_CR2_RD_WRN; - data = I2C_BASE->TXDR; + //Set Buffer size + I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos; + + //Check if the TX buffer is empty + while(!(I2C_BASE->ISR & (I2C_ISR_TXE))); + //Register to be sent + I2C_BASE->TXDR |= 0xCE; + //Generate start condition I2C_BASE->CR2 |= I2C_CR2_START; - + //Wait until the start condition in generated. while(!(I2C_BASE->ISR & (I2C_ISR_BUSY))); - //Check if the TX buffer is empty - while(!(I2C_BASE->ISR & (I2C_ISR_TXE))); - - //write the register that we wahtn to read - I2C_BASE->TXDR = registerAddress; + + //while(!(I2C_BASE->ISR & (I2C_ISR_RXNE))); i2cCR1= I2C_BASE->CR1; i2cCR2= I2C_BASE->CR2; i2cISR= I2C_BASE->ISR; - //while(!(I2C_BASE->ISR & (I2C_ISR_RXNE))); - //data = I2C_BASE->RXDR; + + data = I2C_BASE->RXDR; } diff --git a/main.c b/main.c index 32eab53..a0b3fce 100644 --- a/main.c +++ b/main.c @@ -114,12 +114,12 @@ int main(int argc, char *argv[]) pinWrite(pinB3,0); // Pin configuration for i2c - pinInit(pinB6); - pinInit(pinB7); - pinConfig(pinB6, alternate, openDrain, none, veryFast);// I2C SCL - pinConfig(pinB7, alternate, openDrain, none, veryFast);// I2C CSA - pinSetAlternate(pinB6, 1); - pinSetAlternate(pinB7, 1); + pinInit(pinA9); + pinInit(pinA10); + pinConfig(pinA9, alternate, openDrain, pullUp, fast);// I2C SCL + pinConfig(pinA10, alternate, openDrain, pullUp, fast);// I2C CSA + pinSetAlternate(pinA9, 4); + pinSetAlternate(pinA10, 4); i2c_t i2c_1; i2c_1.channelNo = I2C_CH_1; @@ -156,7 +156,7 @@ int main(int argc, char *argv[]) print_Usart(usart2, "\n\r"); print_Usart(usart2, "I2C Send Mode\n\r"); - i2cMasterRecieve(&i2c_1,slaveAddress,®isterToRead,&i2cRecieved); + i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved); print_Usart(usart2, "\n\r"); print_Usart(usart2, "CR1 : ");