Simple single-color 8x8x8 LED Cube with AVRs
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mem.c 3.3KB

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