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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * mem.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 <stdlib.h>
  23. #include <stdint.h>
  24. #include "twi.h"
  25. #include "mem.h"
  26. #include "serial.h"
  27. #include "strings.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 = (address & 0xFF00) >> 8;
  36. addB = address & 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, *ret;
  52. if (address >= 65536) {
  53. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  54. memAddress |= 2;
  55. }
  56. addA = (address & 0xFF00) >> 8;
  57. addB = address & 0xFF;
  58. ret = (uint8_t *)malloc(length); // Allocate memory for values read
  59. if (ret == NULL) {
  60. serialWriteString(getString(24));
  61. return NULL;
  62. }
  63. if (i2c_start(memAddress | I2C_WRITE) == 0) {
  64. i2c_write(addA);
  65. i2c_write(addB);
  66. i2c_rep_start(memAddress | I2C_READ);
  67. for (i = 0; i < (length - 1); i++) {
  68. ret[i] = i2c_readAck();
  69. }
  70. ret[length - 1] = i2c_readNak();
  71. i2c_stop();
  72. return ret;
  73. } else {
  74. return NULL;
  75. }
  76. }
  77. void memWriteByte(uint32_t address, uint8_t data) {
  78. uint8_t addA, addB, memAddress = MEMTWIADDRESS;
  79. if (address >= 65536) {
  80. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  81. memAddress |= 2;
  82. }
  83. addA = (address & 0xFF00) >> 8;
  84. addB = address & 0xFF;
  85. if (i2c_start(memAddress | I2C_WRITE) == 0) {
  86. i2c_write(addA);
  87. i2c_write(addB); // Give address to memory
  88. i2c_write(data);
  89. i2c_stop();
  90. }
  91. }
  92. void memWriteBytes(uint32_t address, uint8_t *data, uint8_t length) {
  93. uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
  94. if (address >= 65536) {
  95. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  96. memAddress |= 2;
  97. }
  98. addA = (address & 0xFF00) >> 8;
  99. addB = address & 0xFF;
  100. if (i2c_start(memAddress | I2C_WRITE) == 0) {
  101. i2c_write(addA);
  102. i2c_write(addB); // Give address to memory
  103. for (i = 0; i < length; i++) {
  104. i2c_write(data[i]);
  105. }
  106. i2c_stop();
  107. }
  108. }