-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_FRAM_I2C.cpp
executable file
·153 lines (128 loc) · 4.69 KB
/
Adafruit_FRAM_I2C.cpp
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
/**************************************************************************/
/*!
@file Adafruit_FRAM_I2C.cpp
@author KTOWN (Adafruit Industries)
@license BSD (see license.txt)
Driver for the Adafruit I2C FRAM breakout.
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
@section HISTORY
v1.0 - First release
*/
/**************************************************************************/
//#include <avr/pgmspace.h>
//#include <util/delay.h>
//#include <stdlib.h>
#include <math.h>
#include "Adafruit_FRAM_I2C.h"
#include "Particle.h"
/*========================================================================*/
/* CONSTRUCTORS */
/*========================================================================*/
/**************************************************************************/
/*!
Constructor
*/
/**************************************************************************/
Adafruit_FRAM_I2C::Adafruit_FRAM_I2C(void)
{
_framInitialised = false;
}
/*========================================================================*/
/* PUBLIC FUNCTIONS */
/*========================================================================*/
/**************************************************************************/
/*!
Initializes I2C and configures the chip (call this function before
doing anything else)
*/
/**************************************************************************/
boolean Adafruit_FRAM_I2C::begin(uint8_t addr)
{
i2c_addr = addr;
Wire.begin();
/* Make sure we're actually connected */
uint16_t manufID, prodID;
getDeviceID(&manufID, &prodID);
if (manufID != 0x00A)
{
//Serial.print("Unexpected Manufacturer ID: 0x");
//Serial.println(manufID, HEX);
return false;
}
if (prodID != 0x510)
{
//Serial.print("Unexpected Product ID: 0x");
//Serial.println(prodID, HEX);
return false;
}
/* Everything seems to be properly initialised and connected */
_framInitialised = true;
return true;
}
/**************************************************************************/
/*!
@brief Writes a byte at the specific FRAM address
@params[in] i2cAddr
The I2C address of the FRAM memory chip (1010+A2+A1+A0)
@params[in] framAddr
The 16-bit address to write to in FRAM memory
@params[in] i2cAddr
The 8-bit value to write at framAddr
*/
/**************************************************************************/
void Adafruit_FRAM_I2C::write8 (uint16_t framAddr, uint8_t value)
{
Wire.beginTransmission(i2c_addr);
Wire.write(framAddr >> 8);
Wire.write(framAddr & 0xFF);
Wire.write(value);
Wire.endTransmission();
}
/**************************************************************************/
/*!
@brief Reads an 8 bit value from the specified FRAM address
@params[in] i2cAddr
The I2C address of the FRAM memory chip (1010+A2+A1+A0)
@params[in] framAddr
The 16-bit address to read from in FRAM memory
@returns The 8-bit value retrieved at framAddr
*/
/**************************************************************************/
uint8_t Adafruit_FRAM_I2C::read8 (uint16_t framAddr)
{
Wire.beginTransmission(i2c_addr);
Wire.write(framAddr >> 8);
Wire.write(framAddr & 0xFF);
Wire.endTransmission();
Wire.requestFrom(i2c_addr, (uint8_t)1);
return Wire.read();
}
/**************************************************************************/
/*!
@brief Reads the Manufacturer ID and the Product ID frm the IC
@params[out] manufacturerID
The 12-bit manufacturer ID (Fujitsu = 0x00A)
@params[out] productID
The memory density (bytes 11..8) and proprietary
Product ID fields (bytes 7..0). Should be 0x510 for
the MB85RC256V.
*/
/**************************************************************************/
void Adafruit_FRAM_I2C::getDeviceID(uint16_t *manufacturerID, uint16_t *productID)
{
uint8_t a[3] = { 0, 0, 0 };
uint8_t results;
Wire.beginTransmission(MB85RC_SLAVE_ID >> 1);
Wire.write(i2c_addr << 1);
results = Wire.endTransmission(false);
Wire.requestFrom(MB85RC_SLAVE_ID >> 1, 3);
a[0] = Wire.read();
a[1] = Wire.read();
a[2] = Wire.read();
/* Shift values to separate manuf and prod IDs */
/* See p.10 of http://www.fujitsu.com/downloads/MICRO/fsa/pdf/products/memory/fram/MB85RC256V-DS501-00017-3v0-E.pdf */
*manufacturerID = (a[0] << 4) + (a[1] >> 4);
*productID = ((a[1] & 0x0F) << 8) + a[2];
}