Dangerous Prototypes

Dangerous Prototypes => Bus Pirate Support => Topic started by: Scorpia on July 26, 2009, 03:37:56 am

Title: which compiler
Post by: Scorpia on July 26, 2009, 03:37:56 am
Hi,

i decided to download the source and see if i could add my 1-wire chip id's myself.

i have downloaded the v2 source file and loaded into MPLAB fine. edited the file as i think it needs.
When i hit the make button i got a "Couldn't locate build tool.  Check tool locations." message.

i checked the MPLAB install options and i have everything ticked.

I was wondering whatr compiler you are using ? hope fully once i have that i can work out where to point the project to the compiler.

thanks again

Peter
Title: Re: which compiler
Post by: Scorpia on July 26, 2009, 03:52:56 am
ok i downloaded the microchip MPLAB C for PIC24 which i think is the new version of MPLAB C30.

it seems to compile now. just working out how to export the .hex file.
Title: Re: which compiler
Post by: Scorpia on July 26, 2009, 04:09:19 am
ok, i exported the .hex file i unticked the config bits from the export.

fired up the bootloaded and imported. it seems to work but i got 2 extra verify errors other than the expected range, they are

Quote
Verify Error at 0xABFC should be: 0xFFFFFF but read: 0xF9DF
Verify Error at 0xABFE should be: 0xFFFFFF but read: 0x3F7F

since i cant see a way to attach a file. here is the modified 1wire.c source file
 maybe you can add it to the sovn, hopefully save you 5 min work :)

Code: [Select]
/*
 * This file is part of the Bus Pirate project (buspirate.com).
 *
 * One wire search code taken from here: http://www.maxim-ic.com/appnotes.cfm/appnote_number/187
 *
 * We claim no copyright on our code, but there may be different licenses for some of the code in this file.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

//abstraction for m_i2c_1.c
#include "m_1wire_213.h"
#include "1wire.h"
#include "base.h"

extern struct _modeConfig modeConfig;

//the roster stores the first OW_DEV_ROSTER_SLOTS 1-wire addresses found during a ROM SEARCH command
//these addresses are available as MACROs for quick address entry
#define OW_DEV_ROSTER_SLOTS 10 //how many devices max to store addresses as MACROs
struct _OWID{
// unsigned char familyID; //to lazy to do it right, for now...
unsigned char id[8];
// unsigned char crc;
};

struct _OWIDREG{
unsigned char num;
struct _OWID dev[OW_DEV_ROSTER_SLOTS];
} ;

struct _OWIDREG OWroster;

void DS1wireID(unsigned char famID);

unsigned char OWFirst(void);
unsigned char OWNext(void);
unsigned char OWSearch(void);
unsigned char OWVerify(void);
unsigned char docrc8(unsigned char value);

//because 1wire uses bit times, setting the data line high or low with (_-) has no effect
//we have to save the desired bus state, and then clock in the proper value during a clock(^)
static unsigned char DS1wireDataState=0;//data bits are low by default.

// global search state,
//these lovely globals are provided courtesy of MAXIM's code
//need to be put in a struct....
unsigned char ROM_NO[8];
unsigned char SearchChar=0xf0; //toggle between ROM and ALARM search types
unsigned char LastDiscrepancy;
unsigned char LastFamilyDiscrepancy;
unsigned char LastDeviceFlag;
unsigned char crc8;

void DS1wireReset(void);

//this function links the underlying library functions to generic commands that the bus pirate issues
//put most used functions first for best performance
void DS1wireProcess(unsigned char cmd, unsigned int numVal, unsigned int repeatVal){
static unsigned char c,j;
static unsigned int i;
static unsigned char devID[8];
switch(cmd){
case CMD_READ:
if(repeatVal==1){
bpWmessage(MSG_READ);
c=OWReadByte();
bpWbyte(c);
}else{
bpWmessage(MSG_READBULK);
bpWbyte(repeatVal);
bpWstring(" BYTES:x0Dx0A");
for(i=0;i<repeatVal;i++){
c=OWReadByte();
bpWbyte(c);
bpWstring(" ");
}
}
bpWBR;
break;
case CMD_WRITE:
bpWmessage(MSG_WRITE);
bpWbyte(numVal);
OWWriteByte(numVal);
bpWBR;
break;
case CMD_STARTR:
case CMD_START:
//bus reset
DS1wireReset();
break;
case CMD_BIT_READ:
c=OWReadBit();
bpWmessage(MSG_BIT_READ);
bpEchoState(c);
bpWstring(" *now input directionx0Dx0A");
break;
case CMD_BIT_CLK: //we use clock as the 1wire write slot, use _- to set data state
bpWstring("1WIRE ");
bpWbyte(repeatVal);
bpWstring(" WRITE BIT(S) (");
for(i=0;i<repeatVal;i++) OWWriteBit(DS1wireDataState);
bpEchoState(DS1wireDataState);
bpWline(")");
break;
case CMD_BIT_DATH://this also sets a bit direction variable that sets the bits that are clocked with ^
case CMD_BIT_DATL:
bpWstring("1WIRE WRITE BIT: ");
if(cmd==CMD_BIT_DATL){
DS1wireDataState=0;
bpEchoState(0);
}else{
DS1wireDataState=1;
bpEchoState(1);
}
bpWstring(" *next clock (^) will also have this valuex0Dx0A");
OWWriteBit(DS1wireDataState);
break;
case CMD_PRESETUP: //set the options avaiable here....
modeConfig.allowlsb=0;
modeConfig.allowpullup=1;
modeConfig.HiZ=1;//yes, always HiZ
break;
case CMD_SETUP:
OWroster.num=0;//clear any old 1-wire bus enumeration rosters
//-- Ensure pins are in high impedance mode --
SDA_TRIS=1;
//writes to the PORTs write to the LATCH
SDA=0; //B9 sda
bpWline("1WIRE routines Copyright (C) 2000 Michael Pearce");
bpWline("Released under GNU General Public License");
bpWline("1WIRE READY");
break;
case CMD_CLEANUP: //no cleanup needed...
break;
case CMD_MACRO:
if(numVal>0 && numVal<51){
numVal--;//adjust down one for roster array index
if(numVal>=OWroster.num){//no device #X on the bus, try ROM SEARCH (0xF0)
bpWstring("1WIRE MACRO: NO DEVICE #");
bpWdec(numVal);
bpWline(" ENUMERATED, TRY SEARCH ROM MACRO FIRST (0xF0)");
return;
}
//write out the address of the device in the macro
bpWstring("1WIRE ADDRESS MACRO ");//xxx WRITE BUS #X ID:
bpWdec(numVal+1);
bpWstring(": ");
for(j=0;j<8;j++){
bpWbyte(OWroster.dev[numVal].id[j]);
bpWstring(" ");
OWWriteByte(OWroster.dev[numVal].id[j]);
} //write address
bpWline(" ");
return;
}
switch(numVal){
case 0://menu
bpWline(" 0.Macro menu");
bpWline("Macro     1WIRE address");
//write out roster of devices and macros, or SEARCH ROM NOT RUN, TRY (0xf0)
if(OWroster.num==0){
bpWline(" NO DEVICES ENUMERTED, TRY (ALARM) SEARCH ROM FIRST");
}else{
for(c=0;c<OWroster.num; c++){
bpWstring(" ");
bpWdec(c+1);
bpWstring(".");
for(j=0;j<8;j++){bpWbyte(OWroster.dev[c].id[j]); bpWstring(" ");}
bpWstring("x0Dx0A   *");
DS1wireID(OWroster.dev[c].id[0]); //print the device family identity (if known)
}
}
bpWstring("1WIRE ROM COMMAND MACROs:x0Dx0A 51.READ ROM (0x33) *for single device busx0Dx0A 85.MATCH ROM (0x55) *followed by 64bit addressx0Dx0A 204.SKIP ROM (0xCC) *followed by commandx0Dx0A 236.ALARM SEARCH (0xEC)x0Dx0A 240.SEARCH ROM (0xF0)x0Dx0A");
break;
//1WIRE ROM COMMANDS
case 0xec://ALARM SEARCH
case 0xf0: //SEARCH ROM
if(numVal==0xec){
SearchChar=0xEC;//use ROM ALARM SEARCH type
bpWline("1WIRE ROM COMMAND: ALARM SEARCH (0xEC)");
}else{//SEARCH ROM command...
SearchChar=0xF0;//use ROM SEARCH type
bpWline("1WIRE ROM COMMAND: SEARCH (0xF0)");
}

bpWline("Found devices at:");
bpWline("Macro     1WIRE address");
// find ALL devices
j = 0;
c = OWFirst();
OWroster.num=0;
while (c){
//the roster number is the shortcut macro
bpWstring(" ");
bpWdec(j+1);
bpWstring(".");

// print address
for (i = 0; i <8; i++){
bpWbyte(ROM_NO[i]);
bpWstring(" ");
}
bpWstring("x0Dx0A   *");
DS1wireID(ROM_NO[0]); //print the device family identity (if known)

//keep the first X number of one wire IDs in a roster
//so we can refer to them by macro, rather than ID
if(j<OW_DEV_ROSTER_SLOTS){//only as many as we have room for
for(i=0;i<8;i++) OWroster.dev[OWroster.num].id[i]=ROM_NO[i];
OWroster.num++;//increment the roster count
}

j++;

c = OWNext();
}

bpWstring("Found ");
bpWbyte(j);
bpWline(" devices.");

bpWstring("The first ");
bpWdec(OW_DEV_ROSTER_SLOTS);
bpWline(" device IDs are available by MACRO, see (0).");
break;
case 0x33://READ ROM
DS1wireReset();
bpWline("1WIRE WRITE ROM COMMAND: READ (0x33) *for single device bus only");
OWWriteByte(0x33);
bpWline("1WIRE BULK READ 8 BYTES:");
for(i=0; i<8; i++){
devID[i]=OWReadByte();
bpWbyte(devID[i]);
bpWstring(" ");
}
bpWBR;
DS1wireID(devID[0]);
break;
case 0x55://MATCH ROM
DS1wireReset();
bpWline("1WIRE WRITE ROM COMMAND: MATCH (0x55) *follow with 64bit address");
OWWriteByte(0x55);
break;
case 0xcc://SKIP ROM
DS1wireReset();
bpWline("1WIRE WRITE ROM COMMAND: SKIP (0xCC) *follow with command");
OWWriteByte(0xCC);
break;
default:
bpWmessage(MSG_ERROR_MACRO);
}
break;
default:
bpWmessage(MSG_ERROR_MODE);
}

}

void DS1wireReset(void){
unsigned char c;

c=OWReset();
bpWstring("1WIRE BUS RESET ");
if(c==0){
bpWline("OK");
}else{
bpWstring("ERROR: ");
if(c&0b1)  bpWstring("*SHORT OR NO PULLUP ");
if(c&0b10) bpWstring("*NO DEVICE DETECTED ");
bpWstring("(");
bpWbyte(c);
bpWline(")");
}
}

void DS1wireID(unsigned char famID){
//some devices, according to:
//http://owfs.sourceforge.net/commands.html
#define DS2404 0x04
#define DS18S20 0x10
#define DS1822 0x22
#define DS18B20 0x28
#define DS2431 0x2D
switch(famID){//check for device type
case DS18S20:
bpWline("DS18S20 High Pres Dig Therm");
break;
case DS18B20:
bpWline("DS18B20 Prog Res Dig Therm");
break;
case DS1822:
bpWline("DS1822 Econ Dig Therm");
break;
case DS2404:
bpWline("DS2404 Econram time Chip");
break;
case DS2431:
bpWline("DS2431 1K EEPROM");
break;
default:
bpWline("Unknown device family ID");

}
}


//the 1-wire search algo taken from:
//http://www.maxim-ic.com/appnotes.cfm/appnote_number/187
#define TRUE 1 //if !=0
#define FALSE 0

//--------------------------------------------------------------------------
// Find the 'first' devices on the 1-Wire bus
// Return TRUE  : device found, ROM number in ROM_NO buffer
//        FALSE : no device present
//
unsigned char OWFirst()
{
   // reset the search state
   LastDiscrepancy = 0;
   LastDeviceFlag = FALSE;
   LastFamilyDiscrepancy = 0;

   return OWSearch();
}

//--------------------------------------------------------------------------
// Find the 'next' devices on the 1-Wire bus
// Return TRUE  : device found, ROM number in ROM_NO buffer
//        FALSE : device not found, end of search
//
unsigned char OWNext()
{
   // leave the search state alone
   return OWSearch();
}

//--------------------------------------------------------------------------
// Perform the 1-Wire Search Algorithm on the 1-Wire bus using the existing
// search state.
// Return TRUE  : device found, ROM number in ROM_NO buffer
//        FALSE : device not found, end of search
//
unsigned char OWSearch()
{
   unsigned char id_bit_number;
   unsigned char last_zero, rom_byte_number, search_result;
   unsigned char id_bit, cmp_id_bit;
   unsigned char rom_byte_mask, search_direction;

   // initialize for search
   id_bit_number = 1;
   last_zero = 0;
   rom_byte_number = 0;
   rom_byte_mask = 1;
   search_result = 0;
   crc8 = 0;

   // if the last call was not the last one
   if (!LastDeviceFlag)
   {
      // 1-Wire reset
      if (OWReset())
      {
         // reset the search
         LastDiscrepancy = 0;
         LastDeviceFlag = FALSE;
         LastFamilyDiscrepancy = 0;
         return FALSE;
      }

      // issue the search command
      OWWriteByte(SearchChar);  //!!!!!!!!!!!!!!!

      // loop to do the search
      do
      {
         // read a bit and its complement
         id_bit = OWReadBit();
         cmp_id_bit = OWReadBit();

         // check for no devices on 1-wire
         if ((id_bit == 1) && (cmp_id_bit == 1))
            break;
         else
         {
            // all devices coupled have 0 or 1
            if (id_bit != cmp_id_bit)
               search_direction = id_bit;  // bit write value for search
            else
            {
               // if this discrepancy if before the Last Discrepancy
               // on a previous next then pick the same as last time
               if (id_bit_number < LastDiscrepancy)
                  search_direction = ((ROM_NO[rom_byte_number] & rom_byte_mask) > 0);
               else
                  // if equal to last pick 1, if not then pick 0
                  search_direction = (id_bit_number == LastDiscrepancy);

               // if 0 was picked then record its position in LastZero
               if (search_direction == 0)
               {
                  last_zero = id_bit_number;

                  // check for Last discrepancy in family
                  if (last_zero < 9)
                     LastFamilyDiscrepancy = last_zero;
               }
            }

            // set or clear the bit in the ROM byte rom_byte_number
            // with mask rom_byte_mask
            if (search_direction == 1)
              ROM_NO[rom_byte_number] |= rom_byte_mask;
            else
              ROM_NO[rom_byte_number] &= ~rom_byte_mask;

            // serial number search direction write bit
            OWWriteBit(search_direction);

            // increment the byte counter id_bit_number
            // and shift the mask rom_byte_mask
            id_bit_number++;
            rom_byte_mask <<= 1;

            // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask
            if (rom_byte_mask == 0)
            {
                docrc8(ROM_NO[rom_byte_number]);  // accumulate the CRC
                rom_byte_number++;
                rom_byte_mask = 1;
            }
         }
      }
      while(rom_byte_number < 8);  // loop until through all ROM bytes 0-7

      // if the search was successful then
      if (!((id_bit_number < 65) || (crc8 != 0)))
      {
         // search successful so set LastDiscrepancy,LastDeviceFlag,search_result
         LastDiscrepancy = last_zero;

         // check for last device
         if (LastDiscrepancy == 0)
            LastDeviceFlag = TRUE;
        
         search_result = TRUE;
      }
   }

   // if no device found then reset counters so next 'search' will be like a first
   if (!search_result || !ROM_NO[0])
   {
      LastDiscrepancy = 0;
      LastDeviceFlag = FALSE;
      LastFamilyDiscrepancy = 0;
      search_result = FALSE;
   }

   return search_result;
}

//--------------------------------------------------------------------------
// Verify the device with the ROM number in ROM_NO buffer is present.
// Return TRUE  : device verified present
//        FALSE : device not present
//
unsigned char OWVerify()
{
   unsigned char rom_backup[8];
   unsigned char i,rslt,ld_backup,ldf_backup,lfd_backup;

   // keep a backup copy of the current state
   for (i = 0; i < 8; i++)
      rom_backup[i] = ROM_NO[i];
   ld_backup = LastDiscrepancy;
   ldf_backup = LastDeviceFlag;
   lfd_backup = LastFamilyDiscrepancy;

   // set search to find the same device
   LastDiscrepancy = 64;
   LastDeviceFlag = FALSE;

   if (OWSearch())
   {
      // check if same device found
      rslt = TRUE;
      for (i = 0; i < 8; i++)
      {
         if (rom_backup[i] != ROM_NO[i])
         {
            rslt = FALSE;
            break;
         }
      }
   }
   else
     rslt = FALSE;

   // restore the search state
   for (i = 0; i < 8; i++)
      ROM_NO[i] = rom_backup[i];
   LastDiscrepancy = ld_backup;
   LastDeviceFlag = ldf_backup;
   LastFamilyDiscrepancy = lfd_backup;

   // return the result of the verify
   return rslt;
}

// TEST BUILD
static unsigned char dscrc_table[] = {
        0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
      157,195, 33,127,252,162, 64, 30, 95,  1,227,189, 62, 96,130,220,
       35,125,159,193, 66, 28,254,160,225,191, 93,  3,128,222, 60, 98,
      190,224,  2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
       70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89,  7,
      219,133,103, 57,186,228,  6, 88, 25, 71,165,251,120, 38,196,154,
      101, 59,217,135,  4, 90,184,230,167,249, 27, 69,198,152,122, 36,
      248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91,  5,231,185,
      140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
       17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
      175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
       50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
      202,148,118, 40,171,245, 23, 73,  8, 86,180,234,105, 55,213,139,
       87,  9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
      233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
      116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53};

//--------------------------------------------------------------------------
// Calculate the CRC8 of the byte value provided with the current
// global 'crc8' value.
// Returns current global crc8 value
//
unsigned char docrc8(unsigned char value)
{
   // See Application Note 27
   // TEST BUILD
   crc8 = dscrc_table[crc8 ^ value];
   return crc8;
}

here is the output from the (240) macro with the new update

Quote
1-WIRE>(240)_
1WIRE ROM COMMAND: SEARCH (0xF0)
Found devices at:
Macro     1WIRE address
 1.0x10 0x36 0x00 0x83 0x00 0x08 0x00 0x83
   *DS18S20 High Pres Dig Therm
 2.0x10 0xF1 0x00 0x83 0x00 0x08 0x00 0x99
   *DS18S20 High Pres Dig Therm
 3.0x28 0x6E 0x49 0x66 0x00 0x00 0x00 0x4D
   *DS18B20 Prog Res Dig Therm
 4.0x28 0x9E 0x45 0x15 0x00 0x00 0x00 0x3E
   *DS18B20 Prog Res Dig Therm
 5.0x04 0x9D 0x46 0x4A 0x00 0x00 0x00 0x81
   *DS2404 Econram time Chip
Found 0x05 devices.
The first 10 device IDs are available by MACRO, see (0).

i hope this information helps someone. and if anyone knows how to fix those 2 verify errors it would be great

thanks

Peter
Title: Re: which compiler
Post by: ian on July 26, 2009, 08:24:21 am
You need to include the config bits in the export, it's the location of the config bits that's reporting the error.

For anyone else that wants to compile the source:
First, compile.
Then, go file->export.
On the Memory Areas tab check Configuration bit and Program memory, make sure 0-0xabfa are selected.
On the File Format tab choose INHX32. Click OK to export.
Now follow the normal procedure (http://http://dangerousprototypes.com/2009/07/24/bus-pirate-firmware-upgrades/) to bootload.

I added the update and compiled. You can get my update from the nightly folder (http://http://code.google.com/p/the-bus-pirate/source/browse/#svn/trunk/firmware/) of the SVN trunk.

Would you like SVN write access?
Title: Re: which compiler
Post by: Scorpia on July 26, 2009, 09:45:16 am
Ian,

ok i have exported again with the config bits and as expected those errors are gone.

strange i would have expected that the bootloader would set any config bits required so that the main program wouldnt need the config bits.
ah well live and learn

Also if someone else has some different 1-wire chips that thay would like added im quiet happy to add them .

As for SVN access honestly i cant really see the point. the changes i am likely to make are very minor and few and far between i would think.
i dont have any other 1wire chips to test atm so i think that will be me for  a little while.
Title: Re: which compiler
Post by: ian on July 26, 2009, 02:20:30 pm
Thanks for all your help. I'm glad to get a lot of the quirks documented and the bugs squashed before Bus Pirates start arriving in mass.

( ! ) Fatal error: Uncaught exception 'Elk_Exception' with message 'Please try again. If you come back to this error screen, report the error to an administrator.' in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
( ! ) Elk_Exception: Please try again. If you come back to this error screen, report the error to an administrator. in /var/www/dangerousprototypes/forum/sources/database/Db-mysql.class.php on line 696
Call Stack
#TimeMemoryFunctionLocation
10.00982170376session_write_close ( )...(null):0
20.01012301960ElkArte\sources\subs\SessionHandler\DatabaseHandler->write( )...(null):0
30.01012302736Database_MySQL->query( ).../DatabaseHandler.php:119
40.05372441464Database_MySQL->error( ).../Db-mysql.class.php:273