Simple single-color 8x8x8 LED Cube with AVRs
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.

twislave.c 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * twislave.c
  3. *
  4. * Copyright 2012 Thomas Buck <xythobuz@me.com>
  5. *
  6. * This file is part of LED-Cube.
  7. *
  8. * LED-Cube is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * LED-Cube is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with LED-Cube. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <avr/io.h>
  22. #include <avr/interrupt.h>
  23. #include <stdint.h>
  24. uint8_t data[7] = { 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42 };
  25. uint8_t dataSent = 0;
  26. ISR(TWI_vect) {
  27. static uint8_t dataPos = 0;
  28. switch (TWSR & ~(7)) {
  29. case 0xB0: case 0xA8: // We've been called
  30. dataPos = 1; // Prepare next position
  31. TWDR = data[0]; // Transmit first byte
  32. TWCR |= (1 << TWEA); // More follows
  33. dataSent = 0;
  34. break;
  35. case 0xB8: // We're doing fine
  36. if (dataPos >= 6) {
  37. // whoops, this shouldn't happen
  38. // we transmit the last byte again and stop as if nothing happened
  39. // we do this also if we reached the last byte
  40. dataPos = 0;
  41. TWDR = data[6];
  42. TWCR &= ~(1 << TWEA); // Nothing follows
  43. dataSent = 1;
  44. } else {
  45. TWDR = data[dataPos++]; // Transmit next byte
  46. TWCR |= (1 << TWEA); // More follows
  47. dataSent = 0;
  48. }
  49. break;
  50. case 0xC0: case 0xC8: // We're done
  51. TWCR |= (1 << TWEA);
  52. dataSent = 1;
  53. break;
  54. }
  55. TWCR |= (1 << TWINT); // Continue with bus operation
  56. }
  57. void twiInit(uint8_t address) {
  58. if (address == 0) { // set slave address
  59. TWAR = 1;
  60. } else {
  61. TWAR = address & ~(1);
  62. }
  63. TWCR = (1 << TWEN) | (1 << TWEA) | (1 << TWIE); // Enable twi, interrupt and acknowledge bit
  64. }
  65. void twiSetDataToSend(uint8_t *d) { // 7 bytes
  66. // We don't care if it interrupts while we update the data.
  67. // It will cause a single faulty frame. This will not be visible while showing many
  68. // readouts in a short time.
  69. uint8_t i;
  70. for (i = 0; i < 7; i++) {
  71. data[i] = d[i];
  72. }
  73. dataSent = 0;
  74. }
  75. uint8_t twiDataWasSent(void) {
  76. if (dataSent != 0) {
  77. return 1;
  78. } else {
  79. return 0;
  80. }
  81. }