Master read byte function is now working

i2c
kerem 3 years ago
parent f708b410a9
commit e048155400

@ -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);
uint8_t 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)

@ -34,7 +34,7 @@ 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;
@ -43,7 +43,7 @@ void i2cInit(i2c_t *i2cHardware)
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)
@ -100,7 +100,7 @@ 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)
uint8_t 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))==I2C_ISR_BUSY);
@ -108,15 +108,14 @@ void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAd
i2cHardware->state = i2cRecieving;
//Slave address
I2C_BASE->CR2 |= 0x40 << 1; // The bit no 0 is not taken in concideration in 7bit mode
I2C_BASE->CR2 |= devAddress << 1; // The bit no 0 is not taken in concideration in 7bit mode
//Read Mode
//Write Mode
I2C_BASE->CR2 &= ~I2C_CR2_RD_WRN;
//Set Buffer size
I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
//Generate start condition
I2C_BASE->CR2 |= I2C_CR2_START;
@ -125,15 +124,42 @@ void i2cMasterRecieve(i2c_t *i2cHardware, uint8_t devAddress, uint8_t registerAd
//Check if the TX buffer is empty
while(!(I2C_BASE->ISR & (I2C_ISR_TXE)));
//Register to be sent
I2C_BASE->TXDR |= 0xf0;
I2C_BASE->TXDR |= registerAddress;
while(!(I2C_BASE->ISR & (I2C_ISR_RXNE)));
//Wait untill all is Transfered
while(!(I2C_BASE->ISR & (I2C_ISR_TXE)));
// working well up to this point
//Is the tranfes complete ?
while(!(I2C_BASE->ISR & (I2C_ISR_TC)));
//Read Mode
I2C_BASE->CR2 |= I2C_CR2_RD_WRN;
//Set Buffer size
I2C_BASE->CR2 |= 1 << I2C_CR2_NBYTES_Pos;
//New start condition to read from the slave
I2C_BASE->CR2 |= I2C_CR2_START;
//Wait untill all is Transfered
while(!(I2C_BASE->ISR & (I2C_ISR_TXE)));
//Wait for the input buffer to be full
while(!(I2C_BASE->ISR & (I2C_ISR_RXNE)));
//NACK is generated automatically in master mode.
i2cCR1= I2C_BASE->CR1;
i2cCR2= I2C_BASE->CR2;
i2cISR= I2C_BASE->ISR;
// Sned stop command
I2C_BASE->CR2 |= I2C_CR2_STOP;
//read dtaa from the input register to clear it out
data = I2C_BASE->RXDR;
return data;
}

@ -64,7 +64,7 @@ void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
usartSendChar(usart2, '|');
for(i=0; i < 32; i++)
{
pt = (toPrint >> 31-i) & 1;
pt = (toPrint >> (31-i)) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
@ -73,6 +73,39 @@ void printBinary32(uint32_t toPrint, uint8_t lsbFirst)
}
void printBinary8(uint8_t toPrint, uint8_t lsbFirst)
{
int i;
char pt;
print_Usart(usart2, "\n\r");
if(lsbFirst)
{
print_Usart(usart2, "| 0| 1| 2| 3| 4| 5| 6| 7|");
print_Usart(usart2, "\n\r");
usartSendChar(usart2, '|');
for(i=0; i < 8; i++)
{
pt = (toPrint >> i) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
}
}
else
{
print_Usart(usart2, "| 7| 6| 5| 4| 3| 2| 1| 0|");
print_Usart(usart2, "\n\r");
usartSendChar(usart2, '|');
for(i=0; i < 8; i++)
{
pt = (toPrint >> (7-i)) & 1;
usartSendChar(usart2, ' ');
usartSendChar(usart2, pt + 0x30);
usartSendChar(usart2, '|');
}
}
}
int main(int argc, char *argv[])
{
@ -80,7 +113,8 @@ int main(int argc, char *argv[])
uint8_t slaveAddress = 0xC0;
uint8_t registerToRead = 0x00;
uint8_t i2cRecieved = 0;
char buffer [8];
uint8_t buffer [8];
uint8_t data = 0;
// making array with all available timers
//timerNo_t timers[MAX_TIMER_CHANNEL_COUNT] = {timer_1, timer_2, timer_3, timer_14, timer_16, timer_17};
@ -124,8 +158,8 @@ int main(int argc, char *argv[])
i2c_t i2c_1;
i2c_1.channelNo = I2C_CH_1;
i2c_1.mode = i2cModeMaster;
i2c_1.address = 0x33;
i2c_1.addressSecond = 0;
i2c_1.address = 0x0;
i2c_1.addressSecond = 0x00;
i2c_1.addressCount = i2cAddressCountSingle;
i2c_1.addressSize = i2cAddressSizeSevenBits;
i2c_1.speed = i2cSpeedStandart;
@ -136,56 +170,12 @@ int main(int argc, char *argv[])
i2c_1.state = i2cNotInitialized;
i2cInit(&i2c_1);
print_Usart(usart2, "I2C initialized\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "CR1 : ");
printBinary32(i2cCR1,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "CR2 : ");
printBinary32(i2cCR2,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "ISR : ");
printBinary32(i2cISR,0);
data = i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "I2C Send Mode\n\r");
i2cMasterRecieve(&i2c_1,slaveAddress,registerToRead,&i2cRecieved);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "CR1 : ");
printBinary32(i2cCR1,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "CR2 : ");
printBinary32(i2cCR2,0);
print_Usart(usart2, "\n\r");
printBinary8(data,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "ISR : ");
printBinary32(i2cISR,0);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "\n\r");
print_Usart(usart2, "I2C Recieved : ");
buffer[0] = i2cRecieved>>4; //4 MSB
buffer[1] = i2cRecieved& ((1 << 3)); //4 LSB
usartSendChar(usart2, 0x30);
usartSendChar(usart2, 'x');
usartSendChar(usart2, buffer[0]+0x30);
usartSendChar(usart2, buffer[1]+0x30);
print_Usart(usart2, "\n\r");
print_Usart(usart2, "All is working fine\n\r");

Loading…
Cancel
Save