Simple single-color 8x8x8 LED Cube with AVRs
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

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. #endif
  31. // address is a number between (inclusive) zero and 131071
  32. uint8_t memGetByte(uint32_t address) {
  33. uint8_t addA, addB, memAddress = MEMTWIADDRESS, ret;
  34. if (address >= 65536) {
  35. // Address needs more than 16 bits, we have to set the PAGE bit in i2c address
  36. memAddress |= 2;
  37. }
  38. addA = address & 0xFF00;
  39. addB = address & 0xFF;
  40. if (i2c_start(memAddress | I2C_WRITE) == 0) { // Send start, write address to read
  41. i2c_write(addA);
  42. i2c_write(addB); // Give address to memory
  43. i2c_rep_start(memAddress | I2C_READ); // Start again, this time reading
  44. ret = i2c_readNak(); // Read one byte from memory
  45. i2c_stop();
  46. return ret;
  47. } else {
  48. return 0;
  49. }
  50. }
  51. // Free returned memory!
  52. uint8_t *memGetBytes(uint32_t address, uint8_t length) {
  53. // We could use the High-Speed Mode of the FM24V10 here, but we don't, right now...
  54. uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
  55. uint8_t *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("memGetBytes: No memory!\n");
  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. }