Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. while (!(I2C3->SR1 & I2C_SR1_BTF)) ;
  2.  
  3. while (!(I2C3->SR1 & I2C_SR1_ADDR)) // Read Request Adress set
  4.  
  5. unsigned int axI2CWriteRead(unsigned char bus_unused_param, unsigned char addr,
  6. unsigned char *pTx, unsigned short txLen,
  7. unsigned char *pRx, unsigned short *pRxLen)
  8.  
  9. extern I2C_HandleTypeDef hi2c3;
  10. HAL_StatusTypeDef status;
  11. uint8_t t;
  12. *pRxLen = 0;
  13. memset(pRx, 0, 2);
  14. uint8_t rxData[255] = {0};
  15. uint8_t index = 0;
  16.  
  17. I2C3->CR1 |= I2C_CR1_START;
  18. while(!(I2C3->SR1&I2C_SR1_SB));
  19. I2C3->DR = 0x90;
  20. while (!(I2C3->SR1 & I2C_SR1_ADDR))
  21. if (I2C3->SR1 & I2C_SR1_AF)
  22. return I2C_FAILED;
  23.  
  24. t=I2C3->SR2;
  25. while(index < txLen)
  26. {
  27. unsigned char a = *(pTx+index);
  28. I2C3->DR = a;
  29. while (!(I2C3->SR1 & I2C_SR1_BTF)) ;
  30. index ++;
  31. }
  32.  
  33. index = 0;
  34. I2C3->CR1 |= I2C_CR1_START;/* Generate Start */
  35. t = I2C3->DR;
  36. while(!(I2C3->SR1&I2C_SR1_SB));// Wait until SB is set
  37. I2C3->DR = 0x90|0x01;
  38.  
  39. while (!(I2C3->SR1 & I2C_SR1_ADDR)) //Wait for flag ADDR=1
  40. if (I2C3->SR1 & I2C_SR1_AF)
  41. return I2C_FAILED;
  42.  
  43. t = I2C3->SR2; //Read SR2 for deleting ADDR
  44.  
  45. while(!(I2C3->SR1&I2C_SR1_RXNE));
  46. rxData[index]=I2C3->DR;
  47.  
  48. uint8_t messageLength = rxData[index];
  49. while (index <= messageLength)
  50. {
  51. while(!(I2C3->SR1&I2C_SR1_RXNE));
  52. if(index == messageLength)
  53. {
  54. I2C3->CR1 &= ~I2C_CR1_ACK;
  55. }
  56. else
  57. {
  58. I2C3->CR1 |= I2C_CR1_ACK;
  59. }
  60.  
  61. rxData[++index] = I2C3->DR;
  62. }
  63. I2C3->CR1 |= I2C_CR1_STOP;
  64.  
  65. *pRxLen = rxData[0] + 1;
  66. memcpy(pRx, rxData, *pRxLen);
  67.  
  68.  
  69. if (status != HAL_OK)
  70. return I2C_FAILED;
  71.  
  72. return I2C_OK;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement