Advertisement
Guest User

Untitled

a guest
Apr 8th, 2015
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include "i2c_rw.h"
  2. #include "tools.h"
  3. #ifdef I2C_RAW_DATA
  4. #   include <iostream>
  5. #   include <iomanip>
  6. #endif // I2C_RAW_DATA
  7.  
  8. void i2c_write8(uint8_t byte)
  9. {
  10.     uint8_t i2c_response;
  11.     i2c_response = bcm2835_i2c_write(reinterpret_cast<char*>(&byte), sizeof(byte));
  12.     if (i2c_response != BCM2835_I2C_REASON_OK)
  13.         throw std::system_error(i2c_response, std::generic_category(), "fail to write byte");
  14.     #ifdef I2C_RAW_DATA
  15.     std::cout << "device <--   " << std::setw(2) << std::setfill('0') << std::hex << (int)byte << std::dec << std::endl;
  16.     #endif // I2C_RAW_DATA
  17.     rpi_delayMicroseconds(50);
  18. }
  19.  
  20. uint8_t i2c_read8()
  21. {
  22.     uint8_t byte;
  23.     uint8_t i2c_response;
  24.     i2c_response = bcm2835_i2c_read(reinterpret_cast<char*>(&byte), sizeof(byte));
  25.     if (i2c_response != BCM2835_I2C_REASON_OK)
  26.         throw std::system_error(i2c_response, std::generic_category(), "fail to read byte");
  27.     #ifdef I2C_RAW_DATA
  28.     std::cout << "device   --> " << std::setw(2) << std::setfill('0') << std::hex << (int)byte << std::dec << std::endl;
  29.     #endif // I2C_RAW_DATA
  30.     rpi_delayMicroseconds(50);
  31.     return byte;
  32. }
  33.  
  34. uint16_t i2c_read16()
  35. {
  36.     uint16_t word;
  37.     uint8_t byte1 = i2c_read8();
  38.     uint8_t byte2 = i2c_read8();
  39.     word = byte1 * 256 + byte2;
  40.     return word;
  41. }
  42.  
  43. int16_t i2c_read16i()
  44. {
  45.     int16_t word;
  46.     int8_t byte1 = i2c_read8();
  47.     int8_t byte2 = i2c_read8();
  48.     word = byte1 * 256 + byte2;
  49.     return word;
  50. }
  51.  
  52. void i2c_reg_set8(uint8_t reg, uint8_t data)
  53. {
  54.     i2c_write8(reg);
  55.     i2c_write8(data);
  56. }
  57.  
  58. uint8_t i2c_reg_get8 (uint8_t reg)
  59. {
  60.     i2c_write8(reg);
  61.     return i2c_read8();
  62. }
  63.  
  64. uint16_t i2c_reg_get16 (uint8_t reg)
  65. {
  66.     i2c_write8(reg);
  67.     return i2c_read16();
  68. }
  69.  
  70. int16_t i2c_reg_get16i (uint8_t reg)
  71. {
  72.     i2c_write8(reg);
  73.     return i2c_read16i();
  74. }
  75.  
  76. size_t i2c_read_reg(uint8_t reg, char* buf, int len)
  77. {
  78.     if (len > 32)
  79.         len = 32;
  80.     uint8_t i2c_response;
  81.     i2c_response = bcm2835_i2c_read_register_rs(reinterpret_cast<char*>(&reg), buf, len);
  82.     if (i2c_response != BCM2835_I2C_REASON_OK)
  83.         throw std::system_error(i2c_response, std::generic_category(), "fail to read reg");
  84.     #ifdef I2C_RAW_DATA
  85.     std::cout << "device [" << std::setw(2) << std::setfill('0') << std::hex << (int)reg << "] -> ";
  86.     for(int i=0; i<len; i++)
  87.         std::cout << " " << std::setw(2) << std::setfill('0') << (int)buf[i];
  88.     std::cout << std::dec << std::endl;
  89.     #endif // I2C_RAW_DATA
  90.     return len;
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement