Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | /*
* Copyright (c) 2015 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <errno.h>
#include <zephyr.h>
#include <sys/printk.h>
#include <device.h>
#include <drivers/i2c.h>
/**
* @file Sample app using the Fujitsu MB85RC256V FRAM through I2C.
*/
#define FRAM_I2C_ADDR 0x50
static int write_bytes(const struct device *i2c_dev, uint16_t addr,
uint8_t *data, uint32_t num_bytes)
{
uint8_t wr_addr[2];
struct i2c_msg msgs[2];
/* FRAM address */
wr_addr[0] = (addr >> 8) & 0xFF;
wr_addr[1] = addr & 0xFF;
/* Setup I2C messages */
/* Send the address to write to */
msgs[0].buf = wr_addr;
msgs[0].len = 2U;
msgs[0].flags = I2C_MSG_WRITE;
/* Data to be written, and STOP after this. */
msgs[1].buf = data;
msgs[1].len = num_bytes;
msgs[1].flags = I2C_MSG_WRITE | I2C_MSG_STOP;
return i2c_transfer(i2c_dev, &msgs[0], 2, FRAM_I2C_ADDR);
}
static int read_bytes(const struct device *i2c_dev, uint16_t addr,
uint8_t *data, uint32_t num_bytes)
{
uint8_t wr_addr[2];
struct i2c_msg msgs[2];
/* Now try to read back from FRAM */
/* FRAM address */
wr_addr[0] = (addr >> 8) & 0xFF;
wr_addr[1] = addr & 0xFF;
/* Setup I2C messages */
/* Send the address to read from */
msgs[0].buf = wr_addr;
msgs[0].len = 2U;
msgs[0].flags = I2C_MSG_WRITE;
/* Read from device. STOP after this. */
msgs[1].buf = data;
msgs[1].len = num_bytes;
msgs[1].flags = I2C_MSG_READ | I2C_MSG_STOP;
return i2c_transfer(i2c_dev, &msgs[0], 2, FRAM_I2C_ADDR);
}
void main(void)
{
const struct device *i2c_dev = DEVICE_DT_GET(DT_NODELABEL(i2c0));
uint8_t cmp_data[16];
uint8_t data[16];
int i, ret;
if (!device_is_ready(i2c_dev)) {
printk("I2C: Device is not ready.\n");
return;
}
/* Do one-byte read/write */
data[0] = 0xAE;
ret = write_bytes(i2c_dev, 0x00, &data[0], 1);
if (ret) {
printk("Error writing to FRAM! error code (%d)\n", ret);
return;
} else {
printk("Wrote 0xAE to address 0x00.\n");
}
data[0] = 0x86;
ret = write_bytes(i2c_dev, 0x01, &data[0], 1);
if (ret) {
printk("Error writing to FRAM! error code (%d)\n", ret);
return;
} else {
printk("Wrote 0x86 to address 0x01.\n");
}
data[0] = 0x00;
ret = read_bytes(i2c_dev, 0x00, &data[0], 1);
if (ret) {
printk("Error reading from FRAM! error code (%d)\n", ret);
return;
} else {
printk("Read 0x%X from address 0x00.\n", data[0]);
}
data[1] = 0x00;
ret = read_bytes(i2c_dev, 0x01, &data[0], 1);
if (ret) {
printk("Error reading from FRAM! error code (%d)\n", ret);
return;
} else {
printk("Read 0x%X from address 0x01.\n", data[0]);
}
/* Do multi-byte read/write */
/* get some random data, and clear out data[] */
for (i = 0; i < sizeof(cmp_data); i++) {
cmp_data[i] = k_cycle_get_32() & 0xFF;
data[i] = 0x00;
}
/* write them to the FRAM */
ret = write_bytes(i2c_dev, 0x00, cmp_data, sizeof(cmp_data));
if (ret) {
printk("Error writing to FRAM! error code (%d)\n", ret);
return;
} else {
printk("Wrote %zu bytes to address 0x00.\n", sizeof(cmp_data));
}
ret = read_bytes(i2c_dev, 0x00, data, sizeof(data));
if (ret) {
printk("Error reading from FRAM! error code (%d)\n", ret);
return;
} else {
printk("Read %zu bytes from address 0x00.\n", sizeof(data));
}
ret = 0;
for (i = 0; i < sizeof(cmp_data); i++) {
/* uncomment below if you want to see all the bytes */
/* printk("0x%X ?= 0x%X\n", cmp_data[i], data[i]); */
if (cmp_data[i] != data[i]) {
printk("Data comparison failed @ %d.\n", i);
ret = -EIO;
}
}
if (ret == 0) {
printk("Data comparison successful.\n");
}
}
|