Thanks for the replies, hopefully I am getting closer. To restate what I am trying to do, I have a class that addresses an array of ADCs sharing SPI lines with separate chip selects. My class, which works fine in a NETMF application (not Gadgeteer), is created by passing a reference to the chip select pins, and the SPI module identifier. Periodically it samples the ADC where it instantiates an SPI, samples the array, then disposes of the SPI, so other classes can use this resource.
This was derived from the example “Accessing multiple SPI devices” at https://www.ghielectronics.com/docs/14/spi
Here is my code as it stands. This throws an exception near the bottom in the Try…Catch sequence, and the details of the exception is at the end.
Public Sub ProgramStarted()
bmap = New Bitmap(CInt(display_T43.Width), CInt(display_T43.Height))
Me.display_T43.SimpleGraphics.DisplayText("Biospherical Instruments Inc.", Resources.GetFont(Resources.FontResources.RockwellCondBoldBSI), Gadgeteer.Color.Blue, 1, 250)
' Assign the Window to MainWindow; rendering it to the LCD.
LabelTitle = New Library.Library.Labels(Me.display_T43, Gadgeteer.Color.Black, Gadgeteer.Color.FromRGB(0, 255, 255), 0, 25, 300, 25, Resources.GetFont(Resources.FontResources.Arial20))
LabelxPos = New Library.Library.Labels(Me.display_T43, Gadgeteer.Color.Black, Gadgeteer.Color.FromRGB(0, 255, 255), 0, 50, 300, 25, Resources.GetFont(Resources.FontResources.Arial20))
LabelyPos = New Library.Library.Labels(Me.display_T43, Gadgeteer.Color.Black, Gadgeteer.Color.FromRGB(0, 255, 255), 0, 75, 300, 25, Resources.GetFont(Resources.FontResources.Arial20))
LabelLength = New Library.Library.Labels(Me.display_T43, Gadgeteer.Color.Black, Gadgeteer.Color.FromRGB(0, 255, 255), 0, 100, 300, 25, Resources.GetFont(Resources.FontResources.Arial20))
bargraph = New Library.Library.BarGraph(led_Strip)
' Define the socket
Dim socket3 As Socket = Socket.GetSocket(3, True, Nothing, Nothing)
' define an arbituary configuration
Dim _spiConfig As gti.SPI.Configuration = New gti.SPI.Configuration(False, 0, 0, False, True, 12000)
' define a SPI so I can identify the module assignment - the socket.pin.# will be ignored and redefined later
Dim spiAD As gti.SPI = New gti.SPI(socket3, _spiConfig, Interfaces.SPI.Sharing.Shared, socket3, Gadgeteer.Socket.Pin.Three, Nothing)
' the module assignment to pass on to the class
Dim _spiModule As SPI_module = spiAD.SPIModule
' define the first chip select pin. Only 1 AD here
Dim cpupin(0) As Cpu.Pin
cpupin(0) = GHI.Hardware.G400.Pin.PB1 ' would be nicer to set to Gadgeteer.socket.pin.three
Dim Monitor = New Thread(Sub() ADC(cpupin, _spiModule))
Monitor.Start()
Exit Sub
...
end sub
Private Shared Sub ADC(pin() As Pin, spimodule As SPI_module)
Dim admonitor As New AD7799(pin, spimodule)
End Sub
...
End Class
Public Class AD7799
Dim ad As AD7799
Private _spi As SPI
Private _ChipSelectBarPin() As Pin
Private _spi_mod As Microsoft.SPOT.Hardware.SPI.SPI_module
Private _SPI_config() As SPI.Configuration
Public Sub New(pn() As Microsoft.SPOT.Hardware.Cpu.Pin, spi As SPI.SPI_module) ' spi_mod As Microsoft.SPOT.Hardware.SPI.SPI_module)
_ChipSelectBarPin = pn
ReDim _SPI_config(pn.Length - 1)
For i As Integer = 0 To CInt(_ChipSelectBarPin.Length - 1)
_SPI_config(i) = BuildConfiguration(_ChipSelectBarPin(i), spi)
Next
Try
_spi = New SPI(_SPI_config(0))
Catch ex As Exception
Debug.Print(ex.Message)
Debug.Print(ex.StackTrace)
Stop
End Try
' reset the 7799 here ************************
....
End Class
When I execute this, the correct CS pin is sent to the AD7799 class, and the correct SPI module is also seen. The problem is that the code throws an exception at the Try…Catch section as follows:
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (6) ####
#### Message:
#### Microsoft.SPOT.Hardware.Port::ReservePin [IP: 0000] ####
#### Microsoft.SPOT.Hardware.SPI::.ctor [IP: 0022] ####
#### THdeckboxProto1.AD7799::.ctor [IP: 0053] ####
#### THdeckboxProto1.THdeckboxProto1.Program::ADC [IP: 0005] ####
#### THdeckboxProto1.THdeckboxProto1.Program+_Closure$__1::_Lambda$__1 [IP: 000b] ####
A first chance exception of type ‘System.InvalidOperationException’ occurred in Microsoft.SPOT.Hardware.dll
Exception was thrown: System.InvalidOperationException
Microsoft.SPOT.Hardware.Port::ReservePin
Microsoft.SPOT.Hardware.SPI::.ctor
THdeckboxProto1.AD7799::.ctor
THdeckboxProto1.THdeckboxProto1.Program::ADC
THdeckboxProto1.THdeckboxProto1.Program+_Closure$__1::_Lambda$__1