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.

86 lines
2.8 KiB

  1. /* Copyright (C) 2019 Elia Ritterbusch
  2. +
  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 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. /* Library made by: g4lvanix
  17. * GitHub repository: https://github.com/g4lvanix/I2C-slave-lib
  18. */
  19. #include <avr/io.h>
  20. #include <util/twi.h>
  21. #include <avr/interrupt.h>
  22. #include <stdbool.h>
  23. #include "i2c_slave.h"
  24. volatile uint8_t i2c_slave_reg[I2C_SLAVE_REG_COUNT];
  25. static volatile uint8_t buffer_address;
  26. static volatile bool slave_has_register_set = false;
  27. void i2c_slave_init(uint8_t address) {
  28. // load address into TWI address register
  29. TWAR = address;
  30. // set the TWCR to enable address matching and enable TWI, clear TWINT, enable TWI interrupt
  31. TWCR = (1 << TWIE) | (1 << TWEA) | (1 << TWINT) | (1 << TWEN);
  32. }
  33. void i2c_slave_stop(void) {
  34. // clear acknowledge and enable bits
  35. TWCR &= ~((1 << TWEA) | (1 << TWEN));
  36. }
  37. ISR(TWI_vect) {
  38. uint8_t ack = 1;
  39. switch (TW_STATUS) {
  40. case TW_SR_SLA_ACK:
  41. // The device is now a slave receiver
  42. slave_has_register_set = false;
  43. break;
  44. case TW_SR_DATA_ACK:
  45. // This device is a slave receiver and has received data
  46. // First byte is the location then the bytes will be writen in buffer with auto-incriment
  47. if (!slave_has_register_set) {
  48. buffer_address = TWDR;
  49. if (buffer_address >= I2C_SLAVE_REG_COUNT) { // address out of bounds dont ack
  50. ack = 0;
  51. buffer_address = 0;
  52. }
  53. slave_has_register_set = true; // address has been receaved now fill in buffer
  54. } else {
  55. i2c_slave_reg[buffer_address] = TWDR;
  56. buffer_address++;
  57. }
  58. break;
  59. case TW_ST_SLA_ACK:
  60. case TW_ST_DATA_ACK:
  61. // This device is a slave transmitter and master has requested data
  62. TWDR = i2c_slave_reg[buffer_address];
  63. buffer_address++;
  64. break;
  65. case TW_BUS_ERROR:
  66. // We got an error, reset i2c
  67. TWCR = 0;
  68. default:
  69. break;
  70. }
  71. // Reset i2c state machine to be ready for next interrupt
  72. TWCR |= (1 << TWIE) | (1 << TWINT) | (ack << TWEA) | (1 << TWEN);
  73. }