Advertisement
tmax

Untitled

Feb 11th, 2018
400
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. bool i2c_master_open(void) {
  2. return i2c1_driver_open();
  3. }
  4.  
  5. bool i2c_master_write(uint8_t* data, uint8_t length, uint8_t address) {
  6. uint8_t n;
  7. I2C_INTERRUPT_FLAG = 0;
  8. I2C_COLLISION_FLAG = 0;
  9.  
  10. // Start condition
  11. i2c1_driver_start();
  12. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  13.  
  14. // If no collision occurred
  15. if(I2C_INTERRUPT_FLAG) {
  16. // Send write address
  17. I2C_INTERRUPT_FLAG = 0;
  18. i2c1_driver_TXData(address & 0b11111110);
  19. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  20.  
  21. // No collision occurred?
  22. if(I2C_INTERRUPT_FLAG) {
  23. I2C_INTERRUPT_FLAG = 0;
  24. // Check the ACK status from the slave. If ACK was received, continue
  25. if(!i2c1_driver_isNACK()) {
  26.  
  27. // Send all the bytes
  28. for(n = 0; n < length; ++n) {
  29. i2c1_driver_TXData(data[n]);
  30. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  31. I2C_INTERRUPT_FLAG = 0;
  32.  
  33. // Collision? Return
  34. if(I2C_COLLISION_FLAG) {
  35. i2c1_driver_stop();
  36. return false;
  37. }
  38.  
  39. // NACK received from the slave device? Break!
  40. if(i2c1_driver_isNACK()) {
  41. break;
  42. }
  43. }
  44.  
  45. // Generate the stop condition
  46. i2c1_driver_stop();
  47. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  48. if(I2C_INTERRUPT_FLAG) {
  49. return true;
  50. }
  51. }
  52. }
  53. }
  54.  
  55. return false;
  56. }
  57.  
  58. bool i2c_master_read(uint8_t* data, uint8_t length, uint8_t address) {
  59. uint8_t n;
  60. I2C_INTERRUPT_FLAG = 0;
  61. I2C_COLLISION_FLAG = 0;
  62.  
  63. // Start condition
  64. i2c1_driver_start();
  65. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  66.  
  67. // If no collision occurred
  68. if(I2C_INTERRUPT_FLAG) {
  69. // Send read address
  70. I2C_INTERRUPT_FLAG = 0;
  71. i2c1_driver_TXData(address | 0x01);
  72. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  73.  
  74. // No collision occurred?
  75. if(I2C_INTERRUPT_FLAG) {
  76. I2C_INTERRUPT_FLAG = 0;
  77. // Check the ACK status from the slave. If ACK was received, continue
  78. if(!i2c1_driver_isNACK()) {
  79.  
  80. // Read all the bytes
  81. for(n = 0; n < length; ++n) {
  82. i2c1_driver_startRX();
  83. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  84. I2C_INTERRUPT_FLAG = 0;
  85.  
  86. data[n] = i2c1_driver_getRXData();
  87.  
  88. // Collision? Return
  89. if(I2C_COLLISION_FLAG) {
  90. i2c1_driver_stop();
  91. return false;
  92. }
  93.  
  94. // If this is the last byte we should read, send a NACK to the
  95. // slave device now
  96. if(n == length - 1) {
  97. i2c1_driver_sendNACK();
  98. }
  99. else {
  100. // Send ACK to keep receiving data
  101. i2c1_driver_sendACK();
  102. }
  103. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  104.  
  105. // Collision? Return
  106. if(I2C_COLLISION_FLAG) {
  107. i2c1_driver_stop();
  108. return false;
  109. }
  110.  
  111. I2C_INTERRUPT_FLAG = 0;
  112. }
  113.  
  114. // Generate the stop condition
  115. i2c1_driver_stop();
  116. while(!I2C_INTERRUPT_FLAG && !I2C_COLLISION_FLAG);
  117. if(I2C_INTERRUPT_FLAG) {
  118. return true;
  119. }
  120. }
  121. }
  122. }
  123.  
  124. return false;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement