Simple single-color 8x8x8 LED Cube with AVRs
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * mem.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 <stdlib.h>
  25. #include <stdint.h>
  26. #include "twi.h"
  27. #include "mem.h"
  28. // address is a number between (inclusive) zero and 131071
  29. uint8_t memGetByte(uint32_t address) {
  30. uint8_t addA, addB, memAddress = MEMTWIADDRESS, ret;
  31. if (address >= 65536) {
  32. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  33. memAddress |= 2;
  34. }
  35. addA = memAddress & 0xFF00;
  36. addB = memAddress & 0xFF;
  37. i2c_start(memAddress | I2C_WRITE); // Send start, write address to read
  38. i2c_write(addA);
  39. i2c_write(addB); // Give address to memory
  40. i2c_rep_start(memAddress | I2C_READ); // Start again, this time reading
  41. ret = i2c_readNak(); // Read one byte from memory
  42. i2c_stop();
  43. return ret;
  44. }
  45. // Free returned memory!
  46. uint8_t *memGetBytes(uint32_t address, uint8_t length) {
  47. // We could use the High-Speed Mode of the FM24V10 here, but we don't, right now...
  48. uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
  49. uint8_t *ret;
  50. if (address >= 65536) {
  51. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  52. memAddress |= 2;
  53. }
  54. addA = memAddress & 0xFF00;
  55. addB = memAddress & 0xFF;
  56. ret = (uint8_t *)malloc(length); // Allocate memory for values read
  57. i2c_start(memAddress | I2C_WRITE);
  58. i2c_write(addA);
  59. i2c_write(addB);
  60. i2c_rep_start(memAddress | I2C_READ);
  61. for (i = 0; i < (length - 1); i++) {
  62. ret[i] = i2c_readAck();
  63. }
  64. ret[length - 1] = i2c_readNak();
  65. i2c_stop();
  66. return ret;
  67. }
  68. void memWriteByte(uint32_t address, uint8_t data) {
  69. uint8_t addA, addB, memAddress = MEMTWIADDRESS;
  70. if (address >= 65536) {
  71. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  72. memAddress |= 2;
  73. }
  74. addA = memAddress & 0xFF00;
  75. addB = memAddress & 0xFF;
  76. i2c_write(addA);
  77. i2c_write(addB); // Give address to memory
  78. i2c_write(data);
  79. i2c_stop();
  80. }
  81. void memWriteBytes(uint32_t address, uint8_t *data, uint8_t length) {
  82. uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
  83. if (address >= 65536) {
  84. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  85. memAddress |= 2;
  86. }
  87. addA = memAddress & 0xFF00;
  88. addB = memAddress & 0xFF;
  89. i2c_write(addA);
  90. i2c_write(addB); // Give address to memory
  91. for (i = 0; i < length; i++) {
  92. i2c_write(data[i]);
  93. }
  94. i2c_stop();
  95. }