Hi,
I am experiencing a problem with an external ADC connected through SPI. The values are coming in perfectly, however after some random time, evrytime the G120 controller freezes and the USB interface also becomes unavailable. No exception information is coming in the serial debug interface as well.
I am using a G120 with tinyCLR 1.0. I am stuck with a current PCB design where a ADC is connected to the SPI2 bus.
I know it is more or less reserved voor ethernet, but former design team has connected it to a MCP3301-BI/SN ADC and used P1_31 as chip select.
I have tried lots of things, reduced clock frequency of the device etc. nothing seems to help. I noticed also, when I put in a delay of 1 second after reading out the device, it takes longer for the G120 to freeze and become unresponsive.
The goal is to read out the analogue cyclically every 200ms. Can anybody give me some tips or point me in the right direction? Thanks in advance, this is how my code looks approximately:
using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using GHIElectronics.TinyCLR.IO;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Pwm;
using GHIElectronics.TinyCLR.Devices.Adc;
using GHIElectronics.TinyCLR.Devices.Can;
using GHIElectronics.TinyCLR.Native;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Devices.Spi;
//ChipSelectLine = G120.GpioPin.P1_31
//SPI bus
static SpiConnectionSettings connectionSettings = new SpiConnectionSettings()
{
ChipSelectType = SpiChipSelectType.Gpio,
ChipSelectLine = G120.GpioPin.P1_31,
Mode = SpiMode.Mode1,
ClockFrequency = 125_000,
DataBitLength = 8,
};
//SPI1 = actually SPI2 in the datasheet
static SpiController controller = SpiController.FromName(G120.SpiBus.Spi1);
static SpiDevice device = controller.GetDevice(connectionSettings);
static void Cyclic200ms(object o)
{
byte[] writeBuffer = new byte[2];
byte[] readBuffer = new byte[2];
writeBuffer[0] = 0x00; //Initialise write buffer to make sure no random number gets written
writeBuffer[1] = 0x00;
int value = 0;
device.TransferSequential(writeBuffer, readBuffer);
value = (readBuffer[0] & 0x0F); //4 bits masking
value <<= 8;
value |= readBuffer[1];
if ((readBuffer[0] & 0x10) == 0x10) //signbit = 1, value is negative
{
value = 0;
}
//Debug.WriteLine("Value: " + value.ToString());
//double dWaarde = Convert.ToDouble(value.ToString());
double offsetCorrection = 0.0248;
measuredPressure = ((value / 4096.0) * Config.MaxSensPressure) - offsetCorrection;
}
static void Main()
{
Patch();
int CyclicTime3 = 200; //200 ms
Timer CyclicInterrupt3 = new Timer(Cyclic200ms, null, 0, CyclicTime3); //Cyclic interrupt for pressure control
}
while (!_exitProgram) {
}