Simple single-color 8x8x8 LED Cube with AVRs
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

mem.c 3.4KB

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