123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
-
-
- #include <avr/io.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include "twi.h"
- #include "mem.h"
- #include "serial.h"
- #include "strings.h"
-
-
- uint8_t memGetByte(uint32_t address) {
- uint8_t addA, addB, memAddress = MEMTWIADDRESS, ret;
- if (address >= 65536) {
-
- memAddress |= 2;
- }
- addA = address & 0xFF00;
- addB = address & 0xFF;
-
- if (i2c_start(memAddress | I2C_WRITE) == 0) {
- i2c_write(addA);
- i2c_write(addB);
- i2c_rep_start(memAddress | I2C_READ);
- ret = i2c_readNak();
- i2c_stop();
- return ret;
- } else {
- return 0;
- }
- }
-
-
- uint8_t *memGetBytes(uint32_t address, uint8_t length) {
-
- uint8_t addA, addB, memAddress = MEMTWIADDRESS, i, *ret;
- if (address >= 65536) {
-
- memAddress |= 2;
- }
- addA = address & 0xFF00;
- addB = address & 0xFF;
- ret = (uint8_t *)malloc(length);
- if (ret == NULL) {
- serialWriteString(getString(24));
- return NULL;
- }
-
- if (i2c_start(memAddress | I2C_WRITE) == 0) {
- i2c_write(addA);
- i2c_write(addB);
- i2c_rep_start(memAddress | I2C_READ);
- for (i = 0; i < (length - 1); i++) {
- ret[i] = i2c_readAck();
- }
- ret[length - 1] = i2c_readNak();
- i2c_stop();
- return ret;
- } else {
- return NULL;
- }
- }
-
- void memWriteByte(uint32_t address, uint8_t data) {
- uint8_t addA, addB, memAddress = MEMTWIADDRESS;
- if (address >= 65536) {
-
- memAddress |= 2;
- }
- addA = address & 0xFF00;
- addB = address & 0xFF;
- if (i2c_start(memAddress | I2C_WRITE) == 0) {
- i2c_write(addA);
- i2c_write(addB);
- i2c_write(data);
- i2c_stop();
- }
- }
-
- void memWriteBytes(uint32_t address, uint8_t *data, uint8_t length) {
- uint8_t addA, addB, memAddress = MEMTWIADDRESS, i;
- if (address >= 65536) {
-
- memAddress |= 2;
- }
- addA = address & 0xFF00;
- addB = address & 0xFF;
- if (i2c_start(memAddress | I2C_WRITE) == 0) {
- i2c_write(addA);
- i2c_write(addB);
- for (i = 0; i < length; i++) {
- i2c_write(data[i]);
- }
- i2c_stop();
- }
- }
|