From b5c120a2f9fe19997af486c5046fdf0cf1eaa44b Mon Sep 17 00:00:00 2001 From: xerrni Date: Tue, 15 Sep 2020 12:14:22 +0200 Subject: [PATCH] Add Tizen.Peripheral API (#1939) TizenFX integration prepared by Wiktor Gerstenstein, Ernest Borowski Original authors: - Jakub Sobczuk - Michal Lesniak - Michal Kolodziejski - Marcin Romaniuk - Kamil Stepaniuk Co-authored-by: Wiktor Gerstenstein --- internals/src/Tizen.Peripheral/Interop/Adc.cs | 37 +++ internals/src/Tizen.Peripheral/Interop/Gpio.cs | 70 ++++ internals/src/Tizen.Peripheral/Interop/I2c.cs | 51 +++ .../src/Tizen.Peripheral/Interop/Libraries.cs | 23 ++ internals/src/Tizen.Peripheral/Interop/Pwm.cs | 51 +++ internals/src/Tizen.Peripheral/Interop/Spi.cs | 68 ++++ .../src/Tizen.Peripheral/Interop/Uart.Enum.cs | 80 +++++ internals/src/Tizen.Peripheral/Interop/Uart.cs | 58 ++++ .../src/Tizen.Peripheral/Tizen.Peripheral.csproj | 12 + .../src/Tizen.Peripheral/Tizen.Peripheral.sln | 34 ++ .../src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs | 96 ++++++ .../Tizen.Peripheral/ExceptionFactory.cs | 65 ++++ .../src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs | 249 ++++++++++++++ .../src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs | 160 +++++++++ .../Tizen.Peripheral/PinUpdatedEventArgs.cs | 48 +++ .../src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs | 175 ++++++++++ .../src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs | 246 ++++++++++++++ .../src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs | 363 +++++++++++++++++++++ 18 files changed, 1886 insertions(+) create mode 100644 internals/src/Tizen.Peripheral/Interop/Adc.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/Gpio.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/I2c.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/Libraries.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/Pwm.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/Spi.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/Uart.Enum.cs create mode 100644 internals/src/Tizen.Peripheral/Interop/Uart.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral.csproj create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral.sln create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/ExceptionFactory.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/PinUpdatedEventArgs.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs create mode 100644 internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs diff --git a/internals/src/Tizen.Peripheral/Interop/Adc.cs b/internals/src/Tizen.Peripheral/Interop/Adc.cs new file mode 100644 index 0000000..02ffc62 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Adc.cs @@ -0,0 +1,37 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Runtime.InteropServices; +using Tizen.Internals.Errors; + +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class Adc + { + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_adc_open")] + internal static extern ErrorCode Open(int device, int channel, out IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_adc_close")] + internal static extern ErrorCode Close(IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_adc_read")] + internal static extern ErrorCode Read(IntPtr handle, out uint value); + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/Gpio.cs b/internals/src/Tizen.Peripheral/Interop/Gpio.cs new file mode 100644 index 0000000..5c7d688 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Gpio.cs @@ -0,0 +1,70 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Runtime.InteropServices; +using Tizen.Internals.Errors; + +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class Gpio + { + public enum Direction + { + In = 0, + OutHigh, + OutLow + } + + public enum EdgeType + { + None = 0, + Rising, + Falling, + Both + } + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + internal delegate void InterruptedEventCallback(IntPtr handle, ErrorCode error, IntPtr data); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_open")] + internal static extern ErrorCode Open(int pin, out IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_close")] + internal static extern ErrorCode Close(IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_set_direction")] + internal static extern ErrorCode SetDirection(IntPtr handle, Direction direction); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_write")] + internal static extern ErrorCode Write(IntPtr handle, uint value); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_read")] + internal static extern ErrorCode Read(IntPtr handle, out uint value); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_set_edge_mode")] + internal static extern ErrorCode SetEdgeMode(IntPtr handle, EdgeType type); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_set_interrupted_cb")] + internal static extern ErrorCode SetInterruptedCb(IntPtr handle, InterruptedEventCallback callback, IntPtr data); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_gpio_unset_interrupted_cb")] + internal static extern ErrorCode UnsetInterruptedCb(IntPtr handle); + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/I2c.cs b/internals/src/Tizen.Peripheral/Interop/I2c.cs new file mode 100644 index 0000000..708a308 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/I2c.cs @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Runtime.InteropServices; +using Tizen.Internals.Errors; +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class I2c + { + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_open")] + internal static extern ErrorCode Open(int bus, int address, out IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_close")] + internal static extern ErrorCode Close(IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_read")] + internal static extern ErrorCode Read(IntPtr handle, byte[] value, uint size); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_write")] + internal static extern ErrorCode Write(IntPtr handle, byte[] value, uint size); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_read_register_byte")] + internal static extern ErrorCode ReadRegisterByte(IntPtr handle, byte register, out byte data); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_write_register_byte")] + internal static extern ErrorCode WriteRegisterByte(IntPtr handle, byte register, byte data); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_read_register_word")] + internal static extern ErrorCode ReadRegisterWord(IntPtr handle, byte register, out ushort data); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_i2c_write_register_word")] + internal static extern ErrorCode WriteRegisterWord(IntPtr handle, byte register, ushort data); + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/Libraries.cs b/internals/src/Tizen.Peripheral/Interop/Libraries.cs new file mode 100644 index 0000000..5306ab6 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Libraries.cs @@ -0,0 +1,23 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +internal static partial class Interop +{ + internal static partial class Libraries + { + public const string Peripherial = "libcapi-system-peripheral-io.so.0.1.0"; + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/Pwm.cs b/internals/src/Tizen.Peripheral/Interop/Pwm.cs new file mode 100644 index 0000000..728e7ca --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Pwm.cs @@ -0,0 +1,51 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Runtime.InteropServices; +using Tizen.Internals.Errors; +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class Pwm + { + public enum Polarity + { + ActiveHigh = 0, + ActiveLow + } + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_pwm_open")] + internal static extern ErrorCode Open(int chip, int pin, out IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_pwm_close")] + internal static extern ErrorCode Close(IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_pwm_set_period")] + internal static extern ErrorCode SetPeriod(IntPtr handle, uint period); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_pwm_set_duty_cycle")] + internal static extern ErrorCode SetDutyCycle(IntPtr handle, uint dutyCycle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_pwm_set_polarity")] + internal static extern ErrorCode SetPolarity(IntPtr handle, Polarity polarity); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_pwm_set_enabled")] + internal static extern ErrorCode SetEnabled(IntPtr handle, bool enabled); + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/Spi.cs b/internals/src/Tizen.Peripheral/Interop/Spi.cs new file mode 100644 index 0000000..ea15938 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Spi.cs @@ -0,0 +1,68 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Runtime.InteropServices; +using Tizen.Internals.Errors; +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class Spi + { + public enum Mode + { + Mode0 = 0, + Mode1, + Mode2, + Mode3 + } + + public enum BitOrder + { + Msb = 0, + Lsb + } + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_open")] + internal static extern ErrorCode Open(int bus, int chipSelectNumber, out IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_close")] + internal static extern ErrorCode Close(IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_set_mode")] + internal static extern ErrorCode SetMode(IntPtr handle, Mode mode); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_set_bit_order")] + internal static extern ErrorCode SetBitOrder(IntPtr handle, BitOrder order); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_set_bits_per_word")] + internal static extern ErrorCode SetBitsPerWord(IntPtr handle, byte bits); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_set_frequency")] + internal static extern ErrorCode SetFrequency(IntPtr handle, uint frequency); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_read")] + internal static extern ErrorCode Read(IntPtr handle, byte[] data, uint size); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_write")] + internal static extern ErrorCode Write(IntPtr handle, byte[] data, uint size); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_spi_transfer", CallingConvention = CallingConvention.Cdecl)] + internal static extern ErrorCode Transfer(IntPtr handle, byte[] txdata, byte[] rxdata, uint size); + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/Uart.Enum.cs b/internals/src/Tizen.Peripheral/Interop/Uart.Enum.cs new file mode 100644 index 0000000..dc77ba6 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Uart.Enum.cs @@ -0,0 +1,80 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class Uart + { + public enum BaudRate + { + Rate0 = 0, + Rate50, + Rate75, + Rate110, + Rate134, + Rate150, + Rate200, + Rate300, + Rate600, + Rate1200, + Rate1800, + Rate2400, + Rate4800, + Rate9600, + Rate19200, + Rate38400, + Rate57600, + Rate115200, + Rate230400 + } + + public enum ByteSize + { + Size5Bit = 0, + Size6Bit, + Size7Bit, + Size8Bit + } + + public enum Parity + { + None = 0, + Even, + Odd + } + + public enum StopBits + { + Bits1Bit = 0, + Bits2Bit + } + + public enum HardwareFlowControl + { + None = 0, + AutoRtscts + } + + public enum SoftwareFlowControl + { + None = 0, + XonXoff, + } + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Interop/Uart.cs b/internals/src/Tizen.Peripheral/Interop/Uart.cs new file mode 100644 index 0000000..a87e0e0 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Interop/Uart.cs @@ -0,0 +1,58 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using System.Runtime.InteropServices; +using Tizen.Internals.Errors; + +internal static partial class Interop +{ + internal static partial class Peripheral + { + internal static partial class Uart + { + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_open")] + internal static extern ErrorCode Open(int port, out IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_close")] + internal static extern ErrorCode Close(IntPtr handle); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_set_baud_rate")] + internal static extern ErrorCode SetBaudRate(IntPtr handle, BaudRate rate); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_set_byte_size")] + internal static extern ErrorCode SetByteSize(IntPtr handle, ByteSize size); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_set_parity")] + internal static extern ErrorCode SetParity(IntPtr handle, Parity parity); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_set_stop_bits")] + internal static extern ErrorCode SetStopBits(IntPtr handle, StopBits bits); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_set_flow_control")] + internal static extern ErrorCode SetFlowControl( + IntPtr handle, + SoftwareFlowControl softwareFlowControl, + HardwareFlowControl hardwareFlowControl); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_read")] + internal static extern ErrorCode Read(IntPtr handle, byte[] data, uint size); + + [DllImport(Libraries.Peripherial, EntryPoint = "peripheral_uart_write")] + internal static extern ErrorCode Write(IntPtr handle, byte[] data, uint size); + } + } +} \ No newline at end of file diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral.csproj b/internals/src/Tizen.Peripheral/Tizen.Peripheral.csproj new file mode 100644 index 0000000..4c2e6a0 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral.csproj @@ -0,0 +1,12 @@ + + + + netstandard2.0 + mobile,wearable + + + + + + + diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral.sln b/internals/src/Tizen.Peripheral/Tizen.Peripheral.sln new file mode 100644 index 0000000..fdefd54 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral.sln @@ -0,0 +1,34 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.136 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen.Peripheral", "Tizen.Peripheral.csproj", "{5F19BF50-D2E2-4602-81AB-F6677A0EF88D}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen", "..\Tizen\Tizen.csproj", "{9FFD6275-D54A-4F6B-9066-619F82D8B2C6}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tizen.Log", "..\Tizen.Log\Tizen.Log.csproj", "{6F685D6D-FD89-4F05-AC1B-BBB8473B5F5F}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5F19BF50-D2E2-4602-81AB-F6677A0EF88D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5F19BF50-D2E2-4602-81AB-F6677A0EF88D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5F19BF50-D2E2-4602-81AB-F6677A0EF88D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5F19BF50-D2E2-4602-81AB-F6677A0EF88D}.Release|Any CPU.Build.0 = Release|Any CPU + {9FFD6275-D54A-4F6B-9066-619F82D8B2C6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9FFD6275-D54A-4F6B-9066-619F82D8B2C6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9FFD6275-D54A-4F6B-9066-619F82D8B2C6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9FFD6275-D54A-4F6B-9066-619F82D8B2C6}.Release|Any CPU.Build.0 = Release|Any CPU + {6F685D6D-FD89-4F05-AC1B-BBB8473B5F5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {6F685D6D-FD89-4F05-AC1B-BBB8473B5F5F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {6F685D6D-FD89-4F05-AC1B-BBB8473B5F5F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {6F685D6D-FD89-4F05-AC1B-BBB8473B5F5F}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs new file mode 100644 index 0000000..be1ea35 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Adc.cs @@ -0,0 +1,96 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using NativeAdc = Interop.Peripheral.Adc; + +namespace Tizen.Peripheral +{ + /// + /// The class allows applications to use the platform ADC peripheral. + /// + /// http://tizen.org/privilege/peripheralio + public class Adc : IDisposable + { + /// + /// Native handle to ADC. + /// + private IntPtr _handle; + private bool _disposed = false; + + /// + /// Opens the ADC pin. + /// + /// The ADC device number. + /// The ADC channel number to control. + public Adc(int device, int channel) + { + var ret = NativeAdc.Open(device, channel, out IntPtr handle); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _handle= handle; + } + + /// + /// Closes the ADC pin. + /// + ~Adc() + { + Dispose(false); + } + + /// + /// Gets the current value of the ADC pin. + /// + public uint ReadValue() + { + var ret = NativeAdc.Read(_handle, out uint adcValue); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + return adcValue; + } + + /// + /// Closes the ADC pin. + /// + public void Close() => Dispose(); + + /// + /// Disposes the ADC pin. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes the ADC pin. + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + NativeAdc.Close(_handle); + _disposed = true; + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/ExceptionFactory.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/ExceptionFactory.cs new file mode 100644 index 0000000..949b137 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/ExceptionFactory.cs @@ -0,0 +1,65 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using Tizen.Internals.Errors; + +namespace Tizen.Peripheral +{ + /// + /// The class that represents the exception which will be thrown when the application can not control the peripherial. + /// + internal static class ExceptionFactory + { + internal static Exception CreateException(ErrorCode errorCode) + { + Log.Error("Peripheral", $"Error {errorCode}"); + + switch (errorCode) + { + case ErrorCode.IoError: + return new InvalidOperationException("I/O Error occured"); + + case ErrorCode.NoSuchDevice: + return new InvalidOperationException("No such device"); + + case ErrorCode.TryAgain: + return new InvalidOperationException("Try again"); + + case ErrorCode.OutOfMemory: + return new Exception("Out of memory"); + + case ErrorCode.PermissionDenied: + return new UnauthorizedAccessException("Permission denied"); + + case ErrorCode.ResourceBusy: + return new InvalidOperationException("Resource is busy"); + + case ErrorCode.InvalidParameter: + return new ArgumentException("Invalid parameters provided"); + + case ErrorCode.NotSupported: + return new InvalidOperationException("I/O Error occured"); + + case ErrorCode.Unknown: + return new InvalidOperationException("Unknown exception"); + + default: + return new Exception(""); + } + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs new file mode 100644 index 0000000..eb0d3e5 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Gpio.cs @@ -0,0 +1,249 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using Tizen.Internals.Errors; +using NativeGpio = Interop.Peripheral.Gpio; + +namespace Tizen.Peripheral.Gpio +{ + /// + /// Enumeration of GPIO direction options. + /// + public enum GpioPinDriveMode + { + /// + /// Input Mode. + /// + Input, + + /// + /// Output mode with high value. + /// + OutputInitiallyLow, + + /// + /// Output mode with low value. + /// + OutputInitiallyHigh, + } + + /// + /// Enumeration of GPIO values. + /// + public enum GpioPinValue + { + /// + /// Low value. + /// + Low = 0, + + /// + /// High value. + /// + High + } + + /// + /// Enumeration of edge types for the GPIO interrupt. + /// + public enum GpioChangePolarity + { + /// + /// No interrupt on GPIO. + /// + None = 0, + + /// + /// Interrupt on rising only. + /// + Rising, + + /// + /// Interrupt on falling only. + /// + Falling, + + /// + /// Interrupt on rising and falling. + /// + Both + } + + /// + /// The class allows applications to use the platform Digital Pins as Input/Output. + /// + /// http://tizen.org/privilege/peripheralio + public class Gpio : IDisposable + { + + private GpioChangePolarity _polarityType; + private NativeGpio.InterruptedEventCallback _interruptedEventCallback; + + /// + /// Native handle to Gpio. + /// + private IntPtr _handle; + private bool _disposed = false; + + /// + /// Invoked when Gpio pin was interrupted. + /// + public event EventHandler ValueChanged; + + /// + /// Opens a GPIO pin. + /// + /// The GPIO pin number. + /// GPIO direction. + public Gpio(int pinNumber, GpioPinDriveMode mode) + { + var ret = NativeGpio.Open(pinNumber, out IntPtr handle); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _handle = handle; + PinNumber = pinNumber; + try + { + switch (mode) + { + case GpioPinDriveMode.Input: + ret = NativeGpio.SetEdgeMode(handle, NativeGpio.EdgeType.Both); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.In); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + SetIntteruptedCallback(); + break; + case GpioPinDriveMode.OutputInitiallyLow: + ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.OutLow); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + break; + case GpioPinDriveMode.OutputInitiallyHigh: + ret = NativeGpio.SetDirection(handle, NativeGpio.Direction.OutHigh); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + break; + } + } catch(Exception e) { + Dispose(); + throw; + } + } + + + /// + /// Closes the GPIO pin. + /// + ~Gpio() + { + Dispose(false); + } + + private void SetIntteruptedCallback() + { + _interruptedEventCallback = OnInterrupted; + var ret = NativeGpio.SetInterruptedCb(_handle, _interruptedEventCallback, IntPtr.Zero); + if (ret != ErrorCode.None) + { + throw ExceptionFactory.CreateException(ret); + } + } + + private void OnInterrupted(IntPtr handle, ErrorCode error, IntPtr data) + { + ValueChanged?.Invoke(this, new PinUpdatedEventArgs(PinNumber, Read())); + } + + /// + /// Closes a GPIO pin. + /// + public void Close() => Dispose(); + + /// + /// Disposes GPIO. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes GPIO. + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + NativeGpio.UnsetInterruptedCb(_handle); + NativeGpio.Close(_handle); + _disposed = true; + } + + /// + /// GPIO pin number. + /// + public int PinNumber { get; } + + /// + /// Gets pin value. + /// + /// Pin value + public GpioPinValue Read() + { + var ret = NativeGpio.Read(_handle, out uint value); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + return value == 1 ? GpioPinValue.High : GpioPinValue.Low; + } + + /// + /// Sets pin value. + /// + public void Write(GpioPinValue value) + { + var ret = NativeGpio.Write(_handle, value == GpioPinValue.High ? 1 : (uint)0); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Sets or gets the GPIO edge mode. + /// + /// Get value is initialized after successful Set call. + public GpioChangePolarity Polarity + { + get => _polarityType; + set + { + var ret = NativeGpio.SetEdgeMode(_handle, (NativeGpio.EdgeType)value); + if (ret != ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _polarityType = value; + } + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs new file mode 100644 index 0000000..44b3368 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/I2c.cs @@ -0,0 +1,160 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using NativeI2c = Interop.Peripheral.I2c; + +namespace Tizen.Peripheral +{ + /// + /// The class allows applications to communicate via i2c platform's bus. + /// + /// http://tizen.org/privilege/peripheralio + public class I2c : IDisposable + { + /// + /// Native handle to I2c. + /// + private IntPtr _handle; + private bool _disposed = false; + + /// + /// Opens the connection to the I2C slave device. + /// + /// The I2C bus number that the slave device is connected. + /// The address of the slave device. + public I2c(int bus, int address) + { + var ret = NativeI2c.Open(bus, address, out IntPtr handle); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _handle = handle; + } + + /// + /// Closes the connection to the I2C slave device. + /// + ~I2c() + { + Dispose(false); + } + + /// + /// Closes the connection to the I2C slave device. + /// + public void Close() => Dispose(); + + /// + /// Disposes the I2c. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes the I2c. + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + NativeI2c.Close(_handle); + _disposed = true; + } + + /// + /// Reads the bytes data from the I2C slave device. + /// + /// The output byte array. + public void Read(byte[] dataOut) + { + var length = Convert.ToUInt32(dataOut.Length); + var ret = NativeI2c.Read(_handle, dataOut, length); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Writes the bytes data to the I2C slave device. + /// + /// + public void Write(byte[] data) + { + var length = Convert.ToUInt32(data.Length); + var ret = NativeI2c.Write(_handle, data, length); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Reads single byte data from the register of the I2C slave device. + /// + /// The register address of the I2C slave device to read. + /// The single byte data. + public byte ReadRegisterByte(byte register) + { + var ret = NativeI2c.ReadRegisterByte(_handle, register, out byte data); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + return data; + } + + /// + /// Writes single byte data to the register of the I2C slave device. + /// + /// The register address of the I2C slave device to write. + /// The single byte data to write. + public void WriteRegisterByte(byte register, byte data) + { + var ret = NativeI2c.WriteRegisterByte(_handle, register, data); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Reads word data from the register of the I2C slave device. + /// + /// The register address of the I2C slave device to read. + /// The word (2 bytes) data. + public ushort ReadRegisterWord(byte register) + { + var ret = NativeI2c.ReadRegisterWord(_handle, register, out ushort data); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + return data; + } + + /// + /// Writes word data to the register of the I2C slave device. + /// + /// The register address of the I2C slave device to write. + /// The word (2 bytes) data to write. + public void WriteRegisterWord(byte register, ushort data) + { + var ret = NativeI2c.WriteRegisterWord(_handle, register, data); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/PinUpdatedEventArgs.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/PinUpdatedEventArgs.cs new file mode 100644 index 0000000..261971f --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/PinUpdatedEventArgs.cs @@ -0,0 +1,48 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; + +namespace Tizen.Peripheral.Gpio +{ + /// + /// This custom class extend from EventArgs to obtain Gpio object Input Pin update. + /// + public class PinUpdatedEventArgs : EventArgs + { + internal PinUpdatedEventArgs(int pinNumber, GpioPinValue pinValue) + { + PinNumber = pinNumber; + PinValue = pinValue; + } + + /// + /// Gets the Input Pin number which state has changed. + /// + public int PinNumber { get; } + + /// + /// Gets the current state of the Input Pin. + /// + public GpioPinValue PinValue { get; } + + /// + public override string ToString() + { + return $"Pin number {PinNumber} has changed its value to {PinValue}"; + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs new file mode 100644 index 0000000..2e89017 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Pwm.cs @@ -0,0 +1,175 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using NativePwm = Interop.Peripheral.Pwm; + +namespace Tizen.Peripheral.Pwm +{ + /// + /// Enumeration for PWM polarity. + /// + public enum PwmPulsePolarity + { + /// + /// PWM signal start in the active high state. + /// + ActiveHigh = 0, + + /// + /// PWM signal start in the active low state. + /// + ActiveLow + } + + /// + /// The class allows applications to use the platform PWM peripheral. + /// + /// http://tizen.org/privilege/peripheralio + public class Pwm : IDisposable + { + + //TODO provide default values. + private uint _period; + private uint _dutyCycle; + private PwmPulsePolarity _polarity; + private bool _enabled; + private bool _disposed = false; + + /// + /// Native handle to PWM. + /// + private IntPtr _handle; + + /// + /// Opens the PWM pin. + /// + /// The PWM chip number. + /// The PWM pin (channel) number to control. + public Pwm(int chip, int pin) + { + var ret = NativePwm.Open(chip, pin, out IntPtr handle); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _handle = handle; + } + + /// + /// Closes the PWM pin. + /// + ~Pwm() + { + Dispose(false); + } + + /// + /// Closes the PWM pin. + /// + public void Close() => Dispose(); + + /// + /// Disposes the PWM. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes the PWM. + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + NativePwm.Close(_handle); + _disposed = true; + } + + /// + /// Sets or gets period of the PWM pin. + /// + /// Get value is initialized after successful Set call. + public uint Period + { + get => _period; + set + { + var ret = NativePwm.SetPeriod(_handle, value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _period = value; + } + } + + /// + /// Sets or gets duty cycle of the PWM pin. + /// + /// Get value is initialized after successful Set call. + public uint DutyCycle + { + get => _dutyCycle; + set + { + var ret = NativePwm.SetDutyCycle(_handle, value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _dutyCycle = value; + } + } + + /// + /// Sets or gets polarity of the PWM pin. + /// + /// Get value is initialized after successful Set call. + public PwmPulsePolarity Polarity + { + get => _polarity; + set + { + var ret = NativePwm.SetPolarity(_handle, (NativePwm.Polarity)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _polarity = value; + } + } + + /// + /// Enables or disables the PWM pin. + /// + /// Get value is initialized after successful Set call. + public bool Enabled + { + get => _enabled; + set + { + var ret = NativePwm.SetEnabled(_handle, value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _enabled = value; + } + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs new file mode 100644 index 0000000..67cd251 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Spi.cs @@ -0,0 +1,246 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using NativeSpi = Interop.Peripheral.Spi; + +namespace Tizen.Peripheral.Spi +{ + /// + /// Enumeration of SPI transfer modes. + /// + public enum SpiMode + { + /// + /// CPOL = 0, CPHa = 0 Mode. + /// + Mode0 = 0, + + /// + /// CPOL = 0, CPHa = 1 Mode. + /// + Mode1, + + /// + /// CPOL = 1, CPHa = 0 Mode. + /// + Mode2, + + /// + /// CPOL = 1, CPHa = 1 Mode. + /// + Mode3 + } + + /// + /// Enumeration of bit orders. + /// + /// + /// Currently only LSB order is supported! + /// + public enum BitOrder + { + /// + /// Use most siginificant bit first. + /// + MSB = 0, + + /// + /// Use least siginificant bit first. + /// + LSB + } + + /// + /// The class allows applications to communicate via SPI platform's bus. + /// + /// http://tizen.org/privilege/peripheralio + public class Spi : IDisposable + { + + //TODO Provide default values. + private SpiMode _transferMode; + private BitOrder _bitOrder; + private byte _bitsPerWord; + private uint _frequency; + + /// + /// Native handle to Spi. + /// + private IntPtr _handle; + private bool _disposed = false; + + /// + /// Opens a SPI slave device. + /// + /// The SPI bus number. + /// The SPI chip select number. + public Spi(int bus, int chip) + { + var ret = NativeSpi.Open(bus, chip, out IntPtr handle); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _handle = handle; + } + + /// + /// Closes the SPI slave device. + /// + ~Spi() + { + Dispose(false); + } + + /// + /// Closes the SPI slave device. + /// + public void Close() => Dispose(); + + /// + /// Disposes the Spi. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes the Spi. + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + NativeSpi.Close(_handle); + _disposed = true; + } + + /// + /// Reads the bytes data from the SPI slave device. + /// + /// The Data buffer. + public void Read(byte[] buffer) + { + var length = Convert.ToUInt32(buffer.Length); + var ret = NativeSpi.Read(_handle, buffer, length); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Writes the bytes data to the SPI slave device. + /// + /// The data buffer to write. + public void Write(byte[] data) + { + var length = Convert.ToUInt32(data.Length); + var ret = NativeSpi.Write(_handle, data, length); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Exchanges the bytes data to the SPI slave device. + /// writeBuffer.Length and readBuffer.Length must be equal. + /// + /// Array containing data to write to the device. + /// Array containing data read from the dievice. + public void TransferSequential(byte[] writeBuffer, byte[] readBuffer) + { + if (writeBuffer.Length != readBuffer.Length) + throw new Exception("writeBuffer.Length is not equal to readBuffer.Length"); + + var buffersLength = Convert.ToUInt32(writeBuffer.Length); + + var ret = NativeSpi.Transfer(_handle, writeBuffer, readBuffer, buffersLength); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + } + + /// + /// Sets or gets the SPI transfer mode. + /// + /// Get value is initialized after successful Set call. + public SpiMode Mode + { + get => _transferMode; + set + { + var ret = NativeSpi.SetMode(_handle, (NativeSpi.Mode)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _transferMode = value; + } + } + + /// + /// Sets or gets the SPI bit order. + /// + /// Get value is initialized after successful Set call. + public BitOrder BitOrder + { + get => _bitOrder; + set + { + var ret = NativeSpi.SetBitOrder(_handle, (NativeSpi.BitOrder)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _bitOrder = value; + } + } + + /// + /// Sets or gets the number of bits per word. + /// + /// Get value is initialized after successful Set call. + public byte BitsPerWord + { + get => _bitsPerWord; + set + { + var ret = NativeSpi.SetBitsPerWord(_handle, value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _bitsPerWord = value; + } + } + + /// + /// Sets or gets the frequency of the SPI bus. + /// + /// Get value is initialized after successful Set call. + public uint ClockFrequency + { + get => _frequency; + set + { + var ret = NativeSpi.SetFrequency(_handle, value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _frequency = value; + } + } + } +} diff --git a/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs new file mode 100644 index 0000000..2173ae1 --- /dev/null +++ b/internals/src/Tizen.Peripheral/Tizen.Peripheral/Uart.cs @@ -0,0 +1,363 @@ +/* +* Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved +* +* Licensed under the Apache License, Version 2.0 (the License); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an AS IS BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +using System; +using NativeUart = Interop.Peripheral.Uart; + +namespace Tizen.Peripheral.Uart +{ + /// + /// Enumeration for baud rate. + /// + public enum BaudRate + { + /// The number of signal in one second is 0. + Rate0 = 0, + + /// The number of signal in one second is 50. + Rate50, + + /// The number of signal in one second is 75. + Rate75, + + /// The number of signal in one second is 110. + Rate110, + + /// The number of signal in one second is 134. + Rate134, + + /// The number of signal in one second is 150. + Rate150, + + /// The number of signal in one second is 200. + Rate200, + + /// The number of signal in one second is 300. + Rate300, + + /// The number of signal in one second is 600. + Rate600, + + /// The number of signal in one second is 1200. + Rate1200, + + /// The number of signal in one second is 1800. + Rate1800, + + /// The number of signal in one second is 2400. + Rate2400, + + /// The number of signal in one second is 4800. + Rate4800, + + /// The number of signal in one second is 9600. + Rate9600, + + /// The number of signal in one second is 19200. + Rate19200, + + /// The number of signal in one second is 38400. + Rate38400, + + /// The number of signal in one second is 57600. + Rate57600, + + /// The number of signal in one second is 115200. + Rate115200, + + /// The number of signal in one second is 230400. + Rate230400 + } + + /// + /// Enumeration for byte size. + /// + public enum DataBits + { + /// 5 data bits. + Size5Bit = 0, + + /// 6 data bits. + Size6Bit, + + /// 7 data bits. + Size7Bit, + + /// 8 data bits. + Size8Bit + } + + /// + /// Enumeration for parity bit. + /// + public enum Parity + { + /// No parity is used. + None = 0, + + /// Even parity is used. + Even, + + /// Odd parity is used. + Odd + } + + /// + /// Enumeration for stop bits. + /// + public enum StopBits + { + /// One stop bit. + One = 0, + + /// Two stop bits. + Two + } + + /// + /// Enumeration for hardware flow control. + /// + public enum HardwareFlowControl + { + /// No hardware flow control. + None = 0, + + /// Automatic RTS/CTS hardware flow control. + AutoRtsCts + } + + /// + /// Enumeration for software flow control. + /// + public enum SoftwareFlowControl + { + /// No software flow control. + None = 0, + + /// XON/XOFF software flow control. + XonXoff, + } + + /// + /// The class allows applications to communicate via UART platform's bus. + /// + /// http://tizen.org/privilege/peripheralio + public class Uart : IDisposable + { + private BaudRate _baudRate; + private DataBits _dataBits; + private Parity _parity; + private StopBits _stopBits; + private HardwareFlowControl _hardwareFlowControl; + private SoftwareFlowControl _softwareFlowControl; + private bool _disposed = false; + + /// + /// Native handle to I2c. + /// + private IntPtr _handle; + + /// + /// Opens the UART slave device. + /// + /// The UART port number that the slave device is connected. + public Uart(int port) + { + var ret = NativeUart.Open(port, out IntPtr handle); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _handle = handle; + } + + /// + /// Closes the UART slave device. + /// + ~Uart() + { + Dispose(false); + } + + /// + /// Closes the UART slave device. + /// + public void Close() => Dispose(); + + /// + /// Disposes Uart object. + /// + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + /// + /// Disposes Uart object. + /// + protected virtual void Dispose(bool disposing) + { + if (_disposed) + { + return; + } + + NativeUart.Close(_handle); + _disposed = true; + } + + /// + /// Reads the bytes data from the UART slave device. + /// + /// The output byte array. + /// The offset in buffer at which to write the bytes. + /// The number of bytes to read. + /// The number of bytes read. + public int Read(byte[] buffer, int offset, int count) + { + if (count > buffer.Length - offset) + throw new Exception("Can not read more bytes than the buffer can hold."); + byte[] tmpBuffer = new byte[count]; + var length = Convert.ToUInt32(count); + var ret = NativeUart.Read(_handle, tmpBuffer, length); + if (ret < Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + Array.Copy(tmpBuffer, 0, buffer, offset, (int)ret); + return (int)ret; + } + + /// + /// Writes the bytes data to the UART slave device. + /// + /// The byte array that contains the data to write to the device. + /// The offset in buffer at which to begin copying bytes. + /// The number of bytes to write + public int Write(byte[] buffer, int offset, int count) + { + if (count > buffer.Length - offset) + throw new Exception("Can not write more bytes than the buffer holds."); + byte[] tmpBuffer = new byte[count]; + Array.Copy(buffer, offset, tmpBuffer, 0, count); + var length = Convert.ToUInt32(count); + var ret = NativeUart.Write(_handle, tmpBuffer, length); + if (ret < Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + return (int)ret; + } + + /// + /// Sets or gets baud rate of the UART slave device. + /// + /// Get value is initialized after successful Set call. + public BaudRate BaudRate + { + get => _baudRate; + set + { + var ret = NativeUart.SetBaudRate(_handle, (NativeUart.BaudRate)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _baudRate = value; + } + } + + /// + /// Sets or gets byte size of the UART slave device. + /// + /// Get value is initialized after successful Set call. + public DataBits DataBits + { + get => _dataBits; + set + { + var ret = NativeUart.SetByteSize(_handle, (NativeUart.ByteSize)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _dataBits = value; + } + } + + /// + /// Sets or gets parity bit of the UART slave device. + /// + /// Get value is initialized after successful Set call. + public Parity Parity + { + get => _parity; + set + { + var ret = NativeUart.SetParity(_handle, (NativeUart.Parity)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _parity = value; + } + } + + /// + /// Sets or gets stop bits of the UART slave device. + /// + /// Get value is initialized after successful Set call. + public StopBits StopBits + { + get => _stopBits; + set + { + var ret = NativeUart.SetStopBits(_handle, (NativeUart.StopBits)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _stopBits = value; + } + } + + /// + /// Sets or gets hardware flow control of the UART slave device. + /// + /// Get value is initialized after successful Set call. + public HardwareFlowControl HardwareFlowControl + { + get => _hardwareFlowControl; + set + { + var ret = NativeUart.SetFlowControl(_handle, (NativeUart.SoftwareFlowControl)_softwareFlowControl, (NativeUart.HardwareFlowControl)value); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _hardwareFlowControl = value; + } + } + + /// + /// Sets or gets software flow control of the UART slave device. + /// + /// Get value is initialized after successful Set call. + public SoftwareFlowControl SoftwareFlowControl + { + get => _softwareFlowControl; + set + { + var ret = NativeUart.SetFlowControl(_handle, (NativeUart.SoftwareFlowControl)value, (NativeUart.HardwareFlowControl)_hardwareFlowControl); + if (ret != Internals.Errors.ErrorCode.None) + throw ExceptionFactory.CreateException(ret); + + _softwareFlowControl = value; + } + } + } +} -- 2.7.4