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.5KB

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