forked from dotnet/iot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ft232HI2cBus.cs
127 lines (109 loc) · 4.01 KB
/
Ft232HI2cBus.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
115
116
117
118
119
120
121
122
123
124
125
126
127
// 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.Collections.Generic;
using System.Device;
using System.Device.I2c;
using System.IO;
using System.Net.Http.Headers;
using Iot.Device.FtCommon;
namespace Iot.Device.Ft232H
{
/// <summary>
/// I2C Bus for FT232H
/// </summary>
internal class Ft232HI2cBus : I2cBus
{
private readonly Dictionary<int, I2cDevice> _usedAddresses;
/// <summary>
/// Store the FTDI Device Information
/// </summary>
public Ftx232HDevice DeviceInformation { get; private set; }
/// <summary>
/// Creates anI2C Bus
/// </summary>
/// <param name="deviceInformation">a FT232H device</param>
public Ft232HI2cBus(Ftx232HDevice deviceInformation)
{
DeviceInformation = deviceInformation;
DeviceInformation.I2cInitialize();
_usedAddresses = new Dictionary<int, I2cDevice>();
}
/// <inheritdoc/>
public override I2cDevice CreateDevice(int deviceAddress)
{
if (_usedAddresses.ContainsKey(deviceAddress))
{
throw new ArgumentException($"Device with address 0x{deviceAddress,0X2} is already open.", nameof(deviceAddress));
}
I2cDevice ret = CreateDeviceNoCheck(deviceAddress);
_usedAddresses[deviceAddress] = ret;
return ret;
}
internal I2cDevice CreateDeviceNoCheck(int deviceAddress)
{
return new Ft232HI2cDevice(this, deviceAddress);
}
/// <inheritdoc/>
public override void RemoveDevice(int deviceAddress)
{
if (!RemoveDeviceNoCheck(deviceAddress))
{
throw new ArgumentException($"Device with address 0x{deviceAddress,0X2} was not open.", nameof(deviceAddress));
}
}
internal bool RemoveDeviceNoCheck(int deviceAddress)
{
return _usedAddresses.Remove(deviceAddress);
}
internal void Read(int deviceAddress, Span<byte> buffer)
{
DeviceInformation.I2cStart();
var ack = DeviceInformation.I2cSendDeviceAddrAndCheckACK((byte)deviceAddress, true);
if (!ack)
{
DeviceInformation.I2cStop();
throw new IOException($"Error reading device while setting up address");
}
for (int i = 0; i < buffer.Length - 1; i++)
{
buffer[i] = DeviceInformation.I2CReadByte(true);
}
if (buffer.Length > 0)
{
buffer[buffer.Length - 1] = DeviceInformation.I2CReadByte(false);
}
DeviceInformation.I2cStop();
}
internal void Write(int deviceAddress, ReadOnlySpan<byte> buffer)
{
DeviceInformation.I2cStart();
var ack = DeviceInformation.I2cSendDeviceAddrAndCheckACK((byte)deviceAddress, false);
if (!ack)
{
DeviceInformation.I2cStop();
throw new IOException($"Error writing device while setting up address");
}
for (int i = 0; i < buffer.Length; i++)
{
ack = DeviceInformation.I2cSendByteAndCheckACK(buffer[i]);
if (!ack)
{
DeviceInformation.I2cStop();
throw new IOException($"Error writing device on byte {i}");
}
}
DeviceInformation.I2cStop();
}
public override ComponentInformation QueryComponentInformation()
{
var self = new ComponentInformation(this, "Ftx232HI2c I2C Bus driver");
self.Properties["BusNo"] = "0";
foreach (var device in _usedAddresses)
{
self.AddSubComponent(device.Value.QueryComponentInformation());
}
return self;
}
}
}