@ Gus
buffer overflow or ASSERT stands on the LCD.
I use the lastest SDK whats published yesterday.
I used two programs till now:
Hello world:
using Microsoft.SPOT;
using System.Threading;
namespace TestG120_2
{
public class Program
{
public static void Main()
{
Debug.Print("Hallo world");
Thread.Sleep(Timeout.Infinite);
}
}
}
Onewire test program:
Program.cs
using System;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHI.Hardware.G120;
using System.Collections;
using System.Threading;
namespace TestOnewire
{
public class Program
{
public static void Main()
{
OutputPort port = new OutputPort(Pin.P0_13, true);
OneWire bus = new OneWire(port);
string text;
float tempdata;
ArrayList sensors = null;
while (true)
{
sensors = bus.FindAllDevices();
if (sensors.Count == 0)
{
Microsoft.SPOT.Debug.Print("No sensors found");
Thread.Sleep(1000);
}
else
{
bus.AcquireEx();
Debug.Print("Anzahl: " + sensors.Count);
DS1820 sensor;
foreach (byte[] id in sensors)
{
sensor = new DS1820(id, bus, DS1820.Mode.Single);
tempdata = sensor.GetValue(true);
text = "Sensors id: " + DS1820.ConvertToLong(id);
if (!sensor.IsCorrect)
{
text += "; CRC scratchpad false";
}
else
{
text += "; Temp: " + tempdata.ToString("F4") + " °C";
}
if (CRC8.Compute(id, 0, 7) != id[7])
{
text += "; CRC Rom false";
}
Microsoft.SPOT.Debug.Print(text);
}
bus.Release();
}
}
}
}
}
DS1820.cs
namespace TestOnewire
{
using System.Threading;
using Microsoft.SPOT.Hardware;
public sealed class DS1820
{
public const byte SKIP_ROM = 0xCC;
public const byte CONVERT = 0x44;
public const byte MATCH_ROM = 0x55;
public const byte READ_SCRATCHPAD = 0xBE;
private readonly byte[] id;
private readonly OneWire oneWireBus;
private readonly Mode mode;
private readonly Precision precision;
private static object tempLock = new object();
private byte[] scratchPad = new byte[9];
private float value = 0.0F;
private short temp_value;
private bool isCorrect;
public DS1820(byte[] sensorId, OneWire bus, Mode mode, Precision precision)
{
this.id = sensorId;
this.oneWireBus = bus;
this.mode = mode;
this.precision = precision;
}
public DS1820(byte[] sensorId, OneWire bus, Mode mode)
{
this.id = sensorId;
this.oneWireBus = bus;
this.mode = mode;
this.precision = Precision.Accurate;
}
public enum Mode
{
Single,
Multi
}
public enum Precision
{
Simple,
Accurate
}
public byte[] Id
{
get { return this.id; }
}
public bool IsCorrect
{
get { return this.isCorrect; }
}
public static ulong ConvertToLong(byte[] input)
{
return (ulong)input[0] | (ulong)input[1] << 8 | (ulong)input[2] << 16 | (ulong)input[3] << 24 | (ulong)input[4] << 32 | (ulong)input[5] << 40 | (ulong)input[6] << 48 | (ulong)input[7] << 56;
}
public float GetValue()
{
return this.GetValue(false);
}
public float GetValue(bool refresh)
{
if (refresh)
{
lock (tempLock)
{
if (this.mode == Mode.Single)
{
this.RefreshTemperatureData();
}
else
{
this.ReadScratchpad();
}
}
}
lock (this)
{
return this.value;
}
}
public ulong GetId()
{
return ConvertToLong(this.id);
}
private void RefreshTemperatureData()
{
if (this.SelectSensor())
{
this.StartAdcConversion();
this.ReadScratchpad();
}
}
private void ReadScratchpad()
{
if (this.SelectSensor())
{
this.ReadSensorData();
this.isCorrect = this.CheckCRC();
if (this.isCorrect)
{
this.CalculateTemperature();
}
}
}
private bool SelectSensor()
{
if (this.oneWireBus.TouchReset() != 0)
{
this.oneWireBus.WriteByte(MATCH_ROM);
this.oneWireBus.WriteByte(this.id[0]);
this.oneWireBus.WriteByte(this.id[1]);
this.oneWireBus.WriteByte(this.id[2]);
this.oneWireBus.WriteByte(this.id[3]);
this.oneWireBus.WriteByte(this.id[4]);
this.oneWireBus.WriteByte(this.id[5]);
this.oneWireBus.WriteByte(this.id[6]);
this.oneWireBus.WriteByte(this.id[7]);
return true;
}
return false;
}
private void ReadSensorData()
{
this.oneWireBus.WriteByte(READ_SCRATCHPAD);
this.scratchPad[0] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[1] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[2] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[3] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[4] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[5] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[6] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[7] = (byte)this.oneWireBus.ReadByte();
this.scratchPad[8] = (byte)this.oneWireBus.ReadByte();
}
private void StartAdcConversion()
{
this.oneWireBus.WriteByte(CONVERT);
while (this.oneWireBus.ReadByte() == 0)
{
}
}
private void CalculateTemperature()
{
temp_value = (short)((this.scratchPad[1] << 8) | this.scratchPad[0]);
lock (this)
{
if (this.precision == Precision.Accurate)
{
this.value = (float)((temp_value >> 1) - 0.25 + (16.0F - this.scratchPad[6]) / 16.0F);
}
else
{
this.value = temp_value / 2.0F;
}
}
}
/// <summary>
/// Checks if the CRC is correct
/// </summary>
/// <returns>true if the CRC is correct, otherwise false</returns>
/// <remarks>not implemented yet</remarks>
private bool CheckCRC()
{
return CRC8.Compute(this.scratchPad, 0, 8) == this.scratchPad[8];
}
}
}
CRC8.cs
/*---------------------------------------------------------------------------
* Copyright (C) 1999,2000 Maxim Integrated Products, All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated Products
* shall not be used except as stated in the Maxim Integrated Products
* Branding Policy.
*---------------------------------------------------------------------------
*/
namespace TestOnewire
{
using System;
/// <summary>
/// CRC8 is a class to contain an implementation of the
/// Cyclic-Redundency-Check CRC8 for the iButton. The CRC8 is used
/// in the 1-Wire Network address of all iButtons and 1-Wire devices.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
public class CRC8
{
//--------
//-------- Variables
//--------
/// <summary>
/// CRC 8 lookup table
/// </summary>
private static byte[] dscrc_table;
//--------
//-------- Constructor
//--------
/// <summary> Private constructor to prevent instantiation.</summary>
private CRC8()
{
}
//--------
//-------- Methods
//--------
/// <summary>
/// Perform the CRC8 on the data element based on the provided seed.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
/// <param name="dataToCrc">data element on which to perform the CRC8</param>
/// <param name="seed">seed the CRC8 with this value</param>
/// <returns>CRC8 value</returns>
public static uint Compute(uint dataToCRC, uint seed)
{
return (uint)(dscrc_table[(seed ^ dataToCRC) & 0x0FF] & 0x0FF);
}
/// <summary>
/// Perform the CRC8 on the data element based on a zero seed.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
/// <param name="dataToCrc">data element on which to perform the CRC8</param>
/// <returns>CRC8 value</returns>
public static uint Compute(uint dataToCRC)
{
return (uint)(dscrc_table[dataToCRC & 0x0FF] & 0x0FF);
}
/// <summary>
/// Perform the CRC8 on an array of data elements based on a zero seed.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
/// <param name="dataToCrc">array of data elements on which to perform the CRC8</param>
/// <returns>CRC8 value</returns>
public static uint Compute(byte[] dataToCrc)
{
return (uint)Compute(dataToCrc, 0, dataToCrc.Length);
}
/// <summary>
/// Perform the CRC8 on an array of data elements based on a zero seed.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
/// <param name="dataToCrc">array of data elements on which to perform the CRC8</param>
/// <param name="off">offset into array</param>
/// <param name="len">length of data to crc</param>
/// <returns>CRC8 value</returns>
public static uint Compute(byte[] dataToCrc, int off, int len)
{
return (uint)Compute(dataToCrc, off, len, 0);
}
/// <summary>
/// Perform the CRC8 on an array of data elements based on the provided seed.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
/// <param name="dataToCrc">array of data elements on which to perform the CRC8</param>
/// <param name="off">offset into array</param>
/// <param name="len">length of data to crc</param>
/// <param name="seed">seed to use for CRC8</param>
/// <returns>CRC8 value</returns>
public static uint Compute(byte[] dataToCrc, int off, int len, uint seed)
{
// loop to do the crc on each data element
uint CRC8 = seed;
for (int i = 0; i < len; i++)
CRC8 = dscrc_table[(CRC8 ^ dataToCrc[i + off]) & 0x0FF];
return (CRC8 & 0x0FF);
}
/// <summary>
/// Perform the CRC8 on an array of data elements based on the provided seed.
/// CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
/// </summary>
/// <param name="dataToCrc">array of data elements on which to perform the CRC8</param>
/// <param name="seed">seed to use for CRC8</param>
/// <returns>CRC8 value</returns>
public static uint Compute(byte[] dataToCrc, uint seed)
{
return Compute(dataToCrc, 0, dataToCrc.Length, seed);
}
/// <summary>
/// Initializes the <see cref="CRC8"/> class.
/// </summary>
static CRC8()
{
/*
* Create the lookup table
*/
//Translated from the assembly code in iButton Standards, page 129.
dscrc_table = new byte[256];
int acc;
int crc;
for (int i = 0; i < 256; i++)
{
acc = i;
crc = 0;
for (int j = 0; j < 8; j++)
{
if (((acc ^ crc) & 0x01) == 0x01)
{
crc = ((crc ^ 0x18) >> 1) | 0x80;
}
else
crc = crc >> 1;
acc = acc >> 1;
}
dscrc_table[i] = (byte)crc;
}
}
}
}
I think it isnt the CP7, its more the cobra II board. When I look at the G120 module it isn’t perfect alignt 1-2 mm offset on the long side.
I configure the CP7 display threw fezconfig. Then I’am able to programm once and then sometimes it works then I get ASSERT or Bufferoverflow or it steps in the code but says Out of memory. very strange
So what can I check? with detailed description what to do.