You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.4 KiB

  1. /*
  2. Copyright 2016 Luiz Ribeiro <luizribeiro@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <avr/io.h>
  15. #include <util/twi.h>
  16. #include "i2c.h"
  17. void i2c_set_bitrate(uint16_t bitrate_khz) {
  18. uint8_t bitrate_div = ((F_CPU / 1000l) / bitrate_khz);
  19. if (bitrate_div >= 16) {
  20. bitrate_div = (bitrate_div - 16) / 2;
  21. }
  22. TWBR = bitrate_div;
  23. }
  24. void i2c_init(void) {
  25. // set pull-up resistors on I2C bus pins
  26. PORTC |= 0b11;
  27. i2c_set_bitrate(400);
  28. // enable TWI (two-wire interface)
  29. TWCR |= (1 << TWEN);
  30. // enable TWI interrupt and slave address ACK
  31. TWCR |= (1 << TWIE);
  32. TWCR |= (1 << TWEA);
  33. }
  34. uint8_t i2c_start(uint8_t address) {
  35. // reset TWI control register
  36. TWCR = 0;
  37. // begin transmission and wait for it to end
  38. TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
  39. while (!(TWCR & (1<<TWINT)));
  40. // check if the start condition was successfully transmitted
  41. if ((TWSR & 0xF8) != TW_START) {
  42. return 1;
  43. }
  44. // transmit address and wait
  45. TWDR = address;
  46. TWCR = (1<<TWINT) | (1<<TWEN);
  47. while (!(TWCR & (1<<TWINT)));
  48. // check if the device has acknowledged the READ / WRITE mode
  49. uint8_t twst = TW_STATUS & 0xF8;
  50. if ((twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK)) {
  51. return 1;
  52. }
  53. return 0;
  54. }
  55. void i2c_stop(void) {
  56. TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWSTO);
  57. }
  58. uint8_t i2c_write(uint8_t data) {
  59. TWDR = data;
  60. // transmit data and wait
  61. TWCR = (1<<TWINT) | (1<<TWEN);
  62. while (!(TWCR & (1<<TWINT)));
  63. if ((TWSR & 0xF8) != TW_MT_DATA_ACK) {
  64. return 1;
  65. }
  66. return 0;
  67. }
  68. uint8_t i2c_send(uint8_t address, uint8_t *data, uint16_t length) {
  69. if (i2c_start(address)) {
  70. return 1;
  71. }
  72. for (uint16_t i = 0; i < length; i++) {
  73. if (i2c_write(data[i])) {
  74. return 1;
  75. }
  76. }
  77. i2c_stop();
  78. return 0;
  79. }