2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using NativeSpi = Interop.Peripheral.Spi;
20 namespace Tizen.Peripheral.Spi
23 /// Enumeration of SPI transfer modes.
28 /// CPOL = 0, CPHa = 0 Mode.
33 /// CPOL = 0, CPHa = 1 Mode.
38 /// CPOL = 1, CPHa = 0 Mode.
43 /// CPOL = 1, CPHa = 1 Mode.
49 /// Enumeration of bit orders.
52 /// Currently only LSB order is supported!
57 /// Use most siginificant bit first.
62 /// Use least siginificant bit first.
68 /// The class allows applications to communicate via SPI platform's bus.
70 /// <privilege>http://tizen.org/privilege/peripheralio</privilege>
71 public class Spi : IDisposable
74 //TODO Provide default values.
75 private SpiMode _transferMode;
76 private BitOrder _bitOrder;
77 private byte _bitsPerWord;
78 private uint _frequency;
81 /// Native handle to Spi.
83 private IntPtr _handle;
84 private bool _disposed = false;
87 /// Opens a SPI slave device.
89 /// <param name="bus">The SPI bus number.</param>
90 /// <param name="chip">The SPI chip select number.</param>
91 public Spi(int bus, int chip)
93 var ret = NativeSpi.Open(bus, chip, out IntPtr handle);
94 if (ret != Internals.Errors.ErrorCode.None)
95 throw ExceptionFactory.CreateException(ret);
101 /// Closes the SPI slave device.
109 /// Closes the SPI slave device.
111 public void Close() => Dispose();
114 /// Disposes the Spi.
116 public void Dispose()
119 GC.SuppressFinalize(this);
123 /// Disposes the Spi.
125 protected virtual void Dispose(bool disposing)
132 NativeSpi.Close(_handle);
137 /// Reads the bytes data from the SPI slave device.
139 /// <param name="buffer">The Data buffer.</param>
140 public void Read(byte[] buffer)
142 var length = Convert.ToUInt32(buffer.Length);
143 var ret = NativeSpi.Read(_handle, buffer, length);
144 if (ret != Internals.Errors.ErrorCode.None)
145 throw ExceptionFactory.CreateException(ret);
149 /// Writes the bytes data to the SPI slave device.
151 /// <param name="data">The data buffer to write.</param>
152 public void Write(byte[] data)
154 var length = Convert.ToUInt32(data.Length);
155 var ret = NativeSpi.Write(_handle, data, length);
156 if (ret != Internals.Errors.ErrorCode.None)
157 throw ExceptionFactory.CreateException(ret);
161 /// Exchanges the bytes data to the SPI slave device.
162 /// writeBuffer.Length and readBuffer.Length must be equal.
164 /// <param name="writeBuffer">Array containing data to write to the device.</param>
165 /// <param name="readBuffer">Array containing data read from the dievice.</param>
166 public void TransferSequential(byte[] writeBuffer, byte[] readBuffer)
168 if (writeBuffer.Length != readBuffer.Length)
169 throw new Exception("writeBuffer.Length is not equal to readBuffer.Length");
171 var buffersLength = Convert.ToUInt32(writeBuffer.Length);
173 var ret = NativeSpi.Transfer(_handle, writeBuffer, readBuffer, buffersLength);
174 if (ret != Internals.Errors.ErrorCode.None)
175 throw ExceptionFactory.CreateException(ret);
179 /// Sets or gets the SPI transfer mode.
181 /// <remarks>Get value is initialized after successful Set call.</remarks>
184 get => _transferMode;
187 var ret = NativeSpi.SetMode(_handle, (NativeSpi.Mode)value);
188 if (ret != Internals.Errors.ErrorCode.None)
189 throw ExceptionFactory.CreateException(ret);
191 _transferMode = value;
196 /// Sets or gets the SPI bit order.
198 /// <remarks>Get value is initialized after successful Set call.</remarks>
199 public BitOrder BitOrder
204 var ret = NativeSpi.SetBitOrder(_handle, (NativeSpi.BitOrder)value);
205 if (ret != Internals.Errors.ErrorCode.None)
206 throw ExceptionFactory.CreateException(ret);
213 /// Sets or gets the number of bits per word.
215 /// <remarks>Get value is initialized after successful Set call.</remarks>
216 public byte BitsPerWord
221 var ret = NativeSpi.SetBitsPerWord(_handle, value);
222 if (ret != Internals.Errors.ErrorCode.None)
223 throw ExceptionFactory.CreateException(ret);
225 _bitsPerWord = value;
230 /// Sets or gets the frequency of the SPI bus.
232 /// <remarks>Get value is initialized after successful Set call.</remarks>
233 public uint ClockFrequency
238 var ret = NativeSpi.SetFrequency(_handle, value);
239 if (ret != Internals.Errors.ErrorCode.None)
240 throw ExceptionFactory.CreateException(ret);