@ Brett, @ RoSchmi
OK, stripped the code down to the bare minimum.
Also installed fresh VS2013 SP5 and the latest GHI SDKs etc
None of this helped…
As I figured, it seems to be the write to the I2C. The first Reset command in fact.
Putting in a long delay ahead of this doesn’t seem to help.
However I put in a retry and that works fine - though it’s a nasty hack.
Here’s the code:
This one doesn’t work even with a 1 second delay before the reset command.
And Just the reset command kills the ticker timer too.
using System.Threading;
using Gadgeteer.SocketInterfaces;
using GT = Gadgeteer;
namespace SimpleDrumTestApp
{
public partial class Program
{
void ProgramStarted()
{
GT.Timer timer = new GT.Timer(1000);
timer.Tick += Timer_Tick;
timer.Start();
SetUpSn3218();
}
private void Timer_Tick(GT.Timer timer)
{
PulseDebugLED();
}
private const int I2CAddress = 0x54;
private I2CBus _device;
private void SetUpSn3218()
{
var socket = GT.Socket.GetSocket(1, true, null, null);
_device = I2CBusFactory.Create(socket, I2CAddress, 100, null);
Thread.Sleep(1000); // even with this, it kills the timer
Reset();
Enable();
EnableLeds(0xFFFFF);
Output(new[] {255, 255, 255});
}
private enum Command
{
EnableOutput = 0x00,
SetPwmValues = 0x01,
EnableLeds = 0x13,
Update = 0x16,
Reset = 0x17
}
public void Enable()
{
WriteBlockData(Command.EnableOutput, 0x01);
}
public void Disable()
{
WriteBlockData(Command.EnableOutput, 0x00);
}
public void Reset()
{
WriteBlockData(Command.Reset, 0xff);
}
public void EnableLeds(int bitmask)
{
WriteBlockData(Command.EnableLeds, bitmask & 0x3f, (bitmask >> 6) & 0x3f, (bitmask >> 12) & 0X3f);
WriteBlockData(Command.Update, 0xff);
}
public void Output(params int[] levels)
{
WriteBlockData(Command.SetPwmValues, levels);
WriteBlockData(Command.Update, 0xff);
}
private void WriteBlockData(Command command, params int[] data)
{
if (_device == null)
return;
var buffer = new byte[data.Length + 1];
buffer[0] = (byte) command;
for (var idx = 1; idx <= data.Length; idx++)
{
buffer[idx] = (byte) data[idx - 1];
}
_device.Write(buffer);
}
}
}
And, here’s my kludgy fix:
private void WriteBlockData(Command command, params int[] data)
{
if (_device == null)
return;
var buffer = new byte[data.Length + 1];
buffer[0] = (byte) command;
for (var idx = 1; idx <= data.Length; idx++)
{
buffer[idx] = (byte) data[idx - 1];
}
bool done = false;
while (!done)
{
try
{
_device.Write(buffer);
done = true;
}
catch (Exception)
{
Thread.Sleep(100);
}
}
}
Any ideas how to properly start up the I2C interface for this chip?
Chip spec sheet here:
(In fact some of the code was ‘borrowed’ from Mr Stov - he uses an async block… clue there… however I presumed that await/async isn’t part of .Net MF?
private async Task<I2cDevice> InitializeDevice()
{
// initialize I2C communications
try
{
var deviceSelector = I2cDevice.GetDeviceSelector(I2CControllerName);
var i2CDeviceControllers = await DeviceInformation.FindAllAsync(deviceSelector);
var i2CSettings = new I2cConnectionSettings(I2CAddress) {BusSpeed = I2cBusSpeed.FastMode};
return await I2cDevice.FromIdAsync(i2CDeviceControllers[0].Id, i2CSettings);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("Exception: {0}", e.Message);
return null;
}
}