Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. f_bool i2c_send_data(t_i2c_bus bus_addr,
  2. f_uint8 addr, f_uint16 offset, f_uint8 *data, f_uint16 len)
  3. {
  4. volatile t_i2c *bus = (volatile t_i2c *)bus_addr;
  5. int i = 0;
  6.  
  7. i2c_start(bus_addr);
  8.  
  9. /* send address byte, write mode */
  10. bus->ibdr = ((addr << 1) | I2C_WRITE);
  11.  
  12. if (!i2c_wait_eot_ack(bus_addr)) {
  13. i2c_stop(bus_addr);
  14. return I2C_SEND_ERROR;
  15. }
  16.  
  17. /* offset field */
  18. if (offset > 0xff) {
  19. /* it is a 16 bit address, first H then L */
  20. bus->ibdr = (offset >> 8);
  21.  
  22. if (!i2c_wait_eot_ack(bus_addr)) {
  23. i2c_stop(bus_addr);
  24. return I2C_SEND_ERROR;
  25. }
  26. }
  27.  
  28. bus->ibdr = (offset & 0xff);
  29.  
  30. if (!i2c_wait_eot_ack(bus_addr)) {
  31. i2c_stop(bus_addr);
  32. return I2C_SEND_ERROR;
  33. }
  34.  
  35. /* single address byte sent ? */
  36. if (!len || !data) {
  37. i2c_stop(bus_addr);
  38. return I2C_SEND_SUCCESS;
  39. }
  40.  
  41. /* send data block, if any */
  42. while (len--) {
  43. bus->ibdr = 0x44; //data[i++];
  44.  
  45. if (!i2c_wait_eot_ack(bus_addr)) {
  46. i2c_stop(bus_addr);
  47. return I2C_SEND_ERROR;
  48. }
  49. }
  50.  
  51. i2c_stop(bus_addr);
  52.  
  53. /* wait twr now */
  54. msleep(10);
  55.  
  56. return I2C_SEND_SUCCESS;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement