twi.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. twi.h - Stripped-down TWI/I2C library
  3. Copyright (c) 2006 Nicholas Zambetti. All right reserved.
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  15. */
  16. #pragma once
  17. #include <inttypes.h>
  18. #include <compat/twi.h>
  19. #ifndef TWI_FREQ
  20. #define TWI_FREQ 400000L
  21. #endif
  22. #define TWI_TIMEOUT_MS 10
  23. /*
  24. * Function twi_init
  25. * Desc readys twi pins and sets twi bitrate
  26. * Input none
  27. * Output none
  28. */
  29. void twi_init(void);
  30. /*
  31. * Function twi_disable
  32. * Desc disables twi pins
  33. * Input none
  34. * Output none
  35. */
  36. void twi_disable(void);
  37. /*
  38. * Function twi_check
  39. * Desc checks if a device exists on the bus
  40. * Input address: 7bit i2c device address
  41. * Output 0 on device found at address
  42. */
  43. uint8_t twi_check(uint8_t address);
  44. /*
  45. * Function twi_r8
  46. * Desc read a single byte from a device
  47. * Input address: 7bit i2c device address
  48. * reg: register address
  49. * data: pointer to byte for result
  50. * Output 0 on success
  51. */
  52. uint8_t twi_r8(uint8_t address, uint8_t reg, uint8_t* data);
  53. /*
  54. * Function twi_w8
  55. * Desc write a single byte from a device
  56. * Input address: 7bit i2c device address
  57. * reg: register address
  58. * data: byte to write
  59. * Output 0 on success
  60. */
  61. uint8_t twi_w8(uint8_t address, uint8_t reg, uint8_t data);