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.

mem.c 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. if (i2c_start(memAddress | I2C_WRITE) == 0) { // 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. } else {
  45. return 0;
  46. }
  47. }
  48. // Free returned memory!
  49. uint8_t *memGetBytes(uint32_t address, uint8_t length) {
  50. // We could use the High-Speed Mode of the FM24V10 here, but we don't, right now...
  51. uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
  52. uint8_t *ret;
  53. if (address >= 65536) {
  54. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  55. memAddress |= 2;
  56. }
  57. addA = memAddress & 0xFF00;
  58. addB = memAddress & 0xFF;
  59. ret = (uint8_t *)malloc(length); // Allocate memory for values read
  60. if (i2c_start(memAddress | I2C_WRITE) == 0) {
  61. i2c_write(addA);
  62. i2c_write(addB);
  63. i2c_rep_start(memAddress | I2C_READ);
  64. for (i = 0; i < (length - 1); i++) {
  65. ret[i] = i2c_readAck();
  66. }
  67. ret[length - 1] = i2c_readNak();
  68. i2c_stop();
  69. return ret;
  70. } else {
  71. return NULL;
  72. }
  73. }
  74. void memWriteByte(uint32_t address, uint8_t data) {
  75. uint8_t addA, addB, memAddress = MEMTWIADDRESS;
  76. if (address >= 65536) {
  77. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  78. memAddress |= 2;
  79. }
  80. addA = memAddress & 0xFF00;
  81. addB = memAddress & 0xFF;
  82. if (i2c_start(memAddress | I2C_WRITE) == 0) {
  83. i2c_write(addA);
  84. i2c_write(addB); // Give address to memory
  85. i2c_write(data);
  86. i2c_stop();
  87. }
  88. }
  89. void memWriteBytes(uint32_t address, uint8_t *data, uint8_t length) {
  90. uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
  91. if (address >= 65536) {
  92. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  93. memAddress |= 2;
  94. }
  95. addA = memAddress & 0xFF00;
  96. addB = memAddress & 0xFF;
  97. if (i2c_start(memAddress | I2C_WRITE) == 0) {
  98. i2c_write(addA);
  99. i2c_write(addB); // Give address to memory
  100. for (i = 0; i < length; i++) {
  101. i2c_write(data[i]);
  102. }
  103. i2c_stop();
  104. }
  105. }