Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- while (!(I2C3->SR1 & I2C_SR1_BTF)) ;
- while (!(I2C3->SR1 & I2C_SR1_ADDR)) // Read Request Adress set
- unsigned int axI2CWriteRead(unsigned char bus_unused_param, unsigned char addr,
- unsigned char *pTx, unsigned short txLen,
- unsigned char *pRx, unsigned short *pRxLen)
- extern I2C_HandleTypeDef hi2c3;
- HAL_StatusTypeDef status;
- uint8_t t;
- *pRxLen = 0;
- memset(pRx, 0, 2);
- uint8_t rxData[255] = {0};
- uint8_t index = 0;
- I2C3->CR1 |= I2C_CR1_START;
- while(!(I2C3->SR1&I2C_SR1_SB));
- I2C3->DR = 0x90;
- while (!(I2C3->SR1 & I2C_SR1_ADDR))
- if (I2C3->SR1 & I2C_SR1_AF)
- return I2C_FAILED;
- t=I2C3->SR2;
- while(index < txLen)
- {
- unsigned char a = *(pTx+index);
- I2C3->DR = a;
- while (!(I2C3->SR1 & I2C_SR1_BTF)) ;
- index ++;
- }
- index = 0;
- I2C3->CR1 |= I2C_CR1_START;/* Generate Start */
- t = I2C3->DR;
- while(!(I2C3->SR1&I2C_SR1_SB));// Wait until SB is set
- I2C3->DR = 0x90|0x01;
- while (!(I2C3->SR1 & I2C_SR1_ADDR)) //Wait for flag ADDR=1
- if (I2C3->SR1 & I2C_SR1_AF)
- return I2C_FAILED;
- t = I2C3->SR2; //Read SR2 for deleting ADDR
- while(!(I2C3->SR1&I2C_SR1_RXNE));
- rxData[index]=I2C3->DR;
- uint8_t messageLength = rxData[index];
- while (index <= messageLength)
- {
- while(!(I2C3->SR1&I2C_SR1_RXNE));
- if(index == messageLength)
- {
- I2C3->CR1 &= ~I2C_CR1_ACK;
- }
- else
- {
- I2C3->CR1 |= I2C_CR1_ACK;
- }
- rxData[++index] = I2C3->DR;
- }
- I2C3->CR1 |= I2C_CR1_STOP;
- *pRxLen = rxData[0] + 1;
- memcpy(pRx, rxData, *pRxLen);
- if (status != HAL_OK)
- return I2C_FAILED;
- return I2C_OK;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement