Skip to content

Commit 111719d

Browse files
authored
Add methods to add and remove ADC channel definition (#104)
1 parent 173f43b commit 111719d

File tree

10 files changed

+251
-2
lines changed

10 files changed

+251
-2
lines changed

.runsettings

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<RunSettings>
3+
<!-- Configurations that affect the Test Framework -->
4+
<RunConfiguration>
5+
<MaxCpuCount>1</MaxCpuCount>
6+
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
7+
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
8+
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
9+
<TargetPlatform>x64</TargetPlatform>
10+
</RunConfiguration>
11+
<nanoFrameworkAdapter>
12+
<Logging>None</Logging>
13+
<IsRealHardware>False</IsRealHardware>
14+
</nanoFrameworkAdapter>
15+
</RunSettings>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<ItemGroup>
8+
<ProjectCapability Include="TestContainer" />
9+
</ItemGroup>
10+
<PropertyGroup>
11+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
12+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
13+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<ProjectGuid>d8745376-1940-4362-afef-0103e266e0f9</ProjectGuid>
15+
<OutputType>Library</OutputType>
16+
<AppDesignerFolder>Properties</AppDesignerFolder>
17+
<FileAlignment>512</FileAlignment>
18+
<RootNamespace>ConfigTests</RootNamespace>
19+
<AssemblyName>NFUnitTest</AssemblyName>
20+
<IsCodedUITest>False</IsCodedUITest>
21+
<IsTestProject>true</IsTestProject>
22+
<TestProjectType>UnitTest</TestProjectType>
23+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
24+
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
25+
<RestoreLockedMode Condition="'$(TF_BUILD)' == 'True' or '$(ContinuousIntegrationBuild)' == 'True'">true</RestoreLockedMode>
26+
</PropertyGroup>
27+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
28+
<ItemGroup>
29+
<Compile Include="ConfigurationUnitTests.cs" />
30+
<Compile Include="Properties\AssemblyInfo.cs" />
31+
</ItemGroup>
32+
<ItemGroup>
33+
<ProjectReference Include="..\..\nanoFramework.Hardware.Stm32\nanoFramework.Hardware.Stm32.nfproj" />
34+
</ItemGroup>
35+
<ItemGroup>
36+
<Reference Include="mscorlib">
37+
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.14.2\lib\mscorlib.dll</HintPath>
38+
</Reference>
39+
<Reference Include="nanoFramework.TestFramework">
40+
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.59\lib\nanoFramework.TestFramework.dll</HintPath>
41+
</Reference>
42+
<Reference Include="nanoFramework.UnitTestLauncher">
43+
<HintPath>..\..\packages\nanoFramework.TestFramework.2.1.59\lib\nanoFramework.UnitTestLauncher.exe</HintPath>
44+
</Reference>
45+
</ItemGroup>
46+
<ItemGroup>
47+
<None Include="packages.config" />
48+
</ItemGroup>
49+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
50+
<!-- MANUAL UPDATE HERE -->
51+
<Import Project="..\..\packages\nanoFramework.TestFramework.2.1.59\build\nanoFramework.TestFramework.targets" Condition="Exists('..\..\packages\nanoFramework.TestFramework.2.1.59\build\nanoFramework.TestFramework.targets')" />
52+
<ProjectExtensions>
53+
<ProjectCapabilities>
54+
<ProjectConfigurationsDeclaredAsItems />
55+
</ProjectCapabilities>
56+
</ProjectExtensions>
57+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
58+
<PropertyGroup>
59+
<WarningText>Update the Import path in nfproj to the correct nanoFramework.TestFramework NuGet package folder.</WarningText>
60+
</PropertyGroup>
61+
<Warning Condition="!Exists('..\..\packages\nanoFramework.TestFramework.2.1.59\build\nanoFramework.TestFramework.targets')" Text="'$(WarningText)'" />
62+
</Target>
63+
</Project>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using nanoFramework.TestFramework;
7+
8+
namespace nanoFramework.Hardware.Stm32.UnitTests
9+
{
10+
[TestClass]
11+
public class ConfigurationTests
12+
{
13+
private static bool _runningOnHardware = false;
14+
15+
[Setup]
16+
public void CheckRunningOnRealDevice()
17+
{
18+
try
19+
{
20+
// try configuring a GPIO (will fail if not running on a real device)
21+
Configuration.ConfigurePin(1, new GpioConfiguration());
22+
23+
_runningOnHardware = true;
24+
}
25+
catch
26+
{
27+
// nothing to do here
28+
}
29+
}
30+
31+
[TestMethod]
32+
public void AddRevemoAdcChannels()
33+
{
34+
// skip test if not running on a real device
35+
if (!_runningOnHardware)
36+
{
37+
Assert.SkipTest();
38+
}
39+
40+
// add a new channel
41+
var newChannel1 = Configuration.AddAdcChannel(1, 1, 1);
42+
// and another one
43+
var newChannel2 = Configuration.AddAdcChannel(3, 2, 2);
44+
// and another one
45+
var newChannel3 = Configuration.AddAdcChannel(1, 3, 5);
46+
47+
// assert for different channel indexes
48+
Assert.AreNotEqual(newChannel1, newChannel2, "12 Two newly created channels can't have the same index.");
49+
Assert.AreNotEqual(newChannel3, newChannel2, "32 Two newly created channels can't have the same index.");
50+
Assert.AreNotEqual(newChannel3, newChannel1, "31 Two newly created channels can't have the same index.");
51+
52+
// remove 2nd channel
53+
Configuration.RemoveAdcChannel(newChannel2);
54+
55+
// add a new channel
56+
var newChannel4 = Configuration.AddAdcChannel(1, 5, 10);
57+
58+
// assert for different channel indexes
59+
Assert.AreNotEqual(newChannel1, newChannel4, "14 Two newly created channels can't have the same index.");
60+
Assert.AreNotEqual(newChannel3, newChannel4, "34 Two newly created channels can't have the same index.");
61+
62+
// assert for same indexe as the one previously removed
63+
Assert.AreEqual(newChannel2, newChannel4, "A new channel should go into the same index of another one that has been freed up.");
64+
}
65+
}
66+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyDescription("")]
9+
[assembly: AssemblyConfiguration("")]
10+
[assembly: AssemblyCompany("")]
11+
[assembly: AssemblyCopyright("Copyright (c) 2021 nanoFramework contributors")]
12+
[assembly: AssemblyTrademark("")]
13+
[assembly: AssemblyCulture("")]
14+
15+
// Setting ComVisible to false makes the types in this assembly not visible
16+
// to COM components. If you need to access a type in this assembly from
17+
// COM, set the ComVisible attribute to true on that type.
18+
[assembly: ComVisible(false)]
19+
20+
// Version information for an assembly consists of the following four values:
21+
//
22+
// Major Version
23+
// Minor Version
24+
// Build Number
25+
// Revision
26+
//
27+
// You can specify all the values or you can default the Build and Revision Numbers
28+
// by using the '*' as shown below:
29+
// [assembly: AssemblyVersion("1.0.*")]
30+
[assembly: AssemblyVersion("1.0.0.0")]
31+
[assembly: AssemblyFileVersion("1.0.0.0")]

Tests/ConfigTests/packages.config

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.14.2" targetFramework="netnano1.0" />
4+
<package id="nanoFramework.TestFramework" version="2.1.59" targetFramework="netnano1.0" developmentDependency="true" />
5+
</packages>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": 1,
3+
"dependencies": {
4+
".NETnanoFramework,Version=v1.0": {
5+
"nanoFramework.CoreLibrary": {
6+
"type": "Direct",
7+
"requested": "[1.14.2, 1.14.2]",
8+
"resolved": "1.14.2",
9+
"contentHash": "j1mrz4mitl5LItvmHMsw1aHzCAfvTTgIkRxA0mhs5mSpctJ/BBcuNwua5j3MspfRNKreCQPy/qZy/D9ADLL/PA=="
10+
},
11+
"nanoFramework.TestFramework": {
12+
"type": "Direct",
13+
"requested": "[2.1.59, 2.1.59]",
14+
"resolved": "2.1.59",
15+
"contentHash": "aVGOs2nzgCtfd3o0ORPZPm63Shkyo1027k6PobVyFQpSAKYzfo7y1VQGHoy4Owzyupk0KUgfg+V96lsvErcn4g=="
16+
}
17+
}
18+
}
19+
}

azure-pipelines.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ steps:
5353
- template: azure-pipelines-templates/class-lib-build.yml@templates
5454
parameters:
5555
sonarCloudProject: 'nanoframework_lib-nanoFramework.Hardware.Stm32'
56+
runUnitTests: true
57+
unitTestRunsettings: '$(System.DefaultWorkingDirectory)\.runsettings'
5658

5759
# step from template @ nf-tools repo
5860
# report error

nanoFramework.Hardware.Stm32.sln

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "nanoFramework.Hardware.Stm3
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{DDA58EDA-BA28-4DD5-8E4A-8240C3EA68FE}"
99
ProjectSection(SolutionItems) = preProject
10+
.runsettings = .runsettings
1011
nanoFramework.Hardware.Stm32.DELIVERABLES.nuspec = nanoFramework.Hardware.Stm32.DELIVERABLES.nuspec
1112
nanoFramework.Hardware.Stm32.nuspec = nanoFramework.Hardware.Stm32.nuspec
12-
readme.txt = readme.txt
1313
version.json = version.json
1414
EndProjectSection
1515
EndProject
16+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "ConfigurationTests", "Tests\ConfigTests\ConfigurationTests.nfproj", "{D8745376-1940-4362-AFEF-0103E266E0F9}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -25,6 +27,12 @@ Global
2527
{9EFDB37C-4294-4987-A2B7-6226006AEDB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
2628
{9EFDB37C-4294-4987-A2B7-6226006AEDB3}.Release|Any CPU.Build.0 = Release|Any CPU
2729
{9EFDB37C-4294-4987-A2B7-6226006AEDB3}.Release|Any CPU.Deploy.0 = Release|Any CPU
30+
{D8745376-1940-4362-AFEF-0103E266E0F9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
31+
{D8745376-1940-4362-AFEF-0103E266E0F9}.Debug|Any CPU.Build.0 = Debug|Any CPU
32+
{D8745376-1940-4362-AFEF-0103E266E0F9}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
33+
{D8745376-1940-4362-AFEF-0103E266E0F9}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{D8745376-1940-4362-AFEF-0103E266E0F9}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{D8745376-1940-4362-AFEF-0103E266E0F9}.Release|Any CPU.Deploy.0 = Release|Any CPU
2836
EndGlobalSection
2937
GlobalSection(SolutionProperties) = preSolution
3038
HideSolutionNode = FALSE

nanoFramework.Hardware.Stm32/Configuration.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See LICENSE file in the project root for full license information.
44
//
55

6+
using System;
67
using System.Runtime.CompilerServices;
78

89
namespace nanoFramework.Hardware.Stm32
@@ -27,6 +28,45 @@ public static extern void ConfigurePin(
2728
int pin,
2829
GpioConfiguration configuration);
2930

31+
32+
/// <summary>
33+
/// Add definition of an ADC channel.
34+
/// </summary>
35+
/// <param name="adc">Index of ADC block.</param>
36+
/// <param name="adcChannel">Channel number for the ADC block.</param>
37+
/// <param name="pin">The pin number to connect to ADC channel.</param>
38+
/// <returns>The index of the ADC channel that has been created.</returns>
39+
/// <exception cref="ArgumentException">
40+
/// <para>
41+
/// If the ADC block doesn't exist.
42+
/// </para>
43+
/// <para>
44+
/// - or -
45+
/// </para>
46+
/// <para>
47+
/// If the ADC channel doesn't exist.
48+
/// </para>
49+
/// </exception>
50+
/// <exception cref="NotSupportedException">If the target doesn't have support for ADC.</exception>
51+
/// <remarks>
52+
/// No validation is performed on the usage of the GPIO pin. This will override any other pre-existing configuration for it.
53+
/// </remarks>
54+
[MethodImpl(MethodImplOptions.InternalCall)]
55+
public static extern uint AddAdcChannel(
56+
uint adc,
57+
uint adcChannel,
58+
uint pin);
59+
60+
/// <summary>
61+
/// Removes an existing ADC channel definition created with <see cref="AddAdcChannel"/>.
62+
/// </summary>
63+
/// <param name="channel">Channel number for an ADC definition.</param>
64+
/// <exception cref="ArgumentException">If the ADC <paramref name="channel"/> doesn't exist.</exception>
65+
/// <exception cref="NotSupportedException">If the target doesn't have support for ADC.</exception>
66+
/// <remarks>Upon return the respective GPIO pin is configured to the default configuration: input, without any other activation.</remarks>
67+
[MethodImpl(MethodImplOptions.InternalCall)]
68+
public static extern void RemoveAdcChannel(uint channel);
69+
3070
#pragma warning restore S4200 // Native methods should be wrapped
3171

3272
}

nanoFramework.Hardware.Stm32/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
////////////////////////////////////////////////////////////////
1414
// update this whenever the native assembly signature changes //
15-
[assembly: AssemblyNativeVersion("100.0.5.0")]
15+
[assembly: AssemblyNativeVersion("100.0.5.1")]
1616
////////////////////////////////////////////////////////////////
1717

1818
// Setting ComVisible to false makes the types in this assembly not visible

0 commit comments

Comments
 (0)