forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mcp79400.cs
114 lines (99 loc) · 4.27 KB
/
Mcp79400.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.I2c;
using Iot.Device.Common;
namespace Iot.Device.Mcp7940xx
{
/// <summary>
/// Battery-Backed I2C Real-Time Clock/Calendar with SRAM and EEPROM.
/// </summary>
public class Mcp79400 : Mcp7940n
{
/// <summary>
/// Initializes a new instance of the <see cref="Mcp79400" /> class.
/// </summary>
/// <param name="i2cDevice">The I2C device to use for communication.</param>
/// <param name="clockSource">The clocks oscillator configuration.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="i2cDevice"/> is <c>null</c>.</exception>
public Mcp79400(I2cDevice i2cDevice, ClockSource clockSource)
: base(i2cDevice, clockSource)
{
}
/// <summary>
/// Provides access to the protected EEPROM of the Mcp79400.
/// </summary>
public class Eeprom : IDisposable
{
/// <summary>
/// Upper address of EEPROM memory region.
/// </summary>
private const byte UpperAddressBound = 0xEF;
/// <summary>
/// Default I2C address for Mcp79400x protected EEPROM.
/// </summary>
public const byte DefaultI2cAddress = 0b1010_0111;
/// <summary>
/// The underlying I2C device used for accessing the EEPROM address space.
/// </summary>
protected readonly I2cDevice _i2cDevice;
/// <summary>
/// Initializes a new instance of the <see cref="Eeprom" /> class.
/// </summary>
/// <param name="i2cDevice">The I2C device to use for communication.</param>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="i2cDevice"/> is <c>null</c>.</exception>
public Eeprom(I2cDevice i2cDevice)
{
if (i2cDevice == null)
{
throw new ArgumentNullException();
}
_i2cDevice = i2cDevice;
}
#region EEPROM
/// <summary>
/// Reads a single byte from the devices EEPROM at the given address.
/// </summary>
/// <param name="address">The address to read from.</param>
/// <returns>The byte read from the device.</returns>
/// <remarks>
/// Parameter <paramref name="address"/> must be in the range 0 to 63.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown when address falls outside of the addressable range for this device.</exception>
public byte ReadByte(byte address)
{
if (address > UpperAddressBound)
{
throw new ArgumentOutOfRangeException();
}
return RegisterHelper.ReadRegister(_i2cDevice, address);
}
/// <summary>
/// Writes a single byte to the devices EEPROM at the given address.
/// </summary>
/// <param name="address">The address to write to.</param>
/// <param name="value">The byte to be written into the device.</param>
/// <remarks>
/// Parameter <paramref name="address"/> must be in the range 0 to 63.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown when address falls outside of the addressable range for this device.</exception>
public void WriteByte(byte address, byte value)
{
if (address > UpperAddressBound)
{
throw new ArgumentOutOfRangeException();
}
// Send the unlock sequence to device to be able to write to the protected EEPROM block.
_i2cDevice.WriteByte(0x55);
_i2cDevice.WriteByte(0xAA);
RegisterHelper.WriteRegister(_i2cDevice, address, value);
}
#endregion
/// <inheritdoc/>
public void Dispose()
{
_i2cDevice.Dispose();
}
}
}
}