| ??? 04/28/08 21:59 Modified: 04/28/08 22:58 Read: times Msg Score: +1 +1 Informative |
#154128 - Followup on slave enumeration algorithm |
For what it's worth, I had a bit of spare time today and wrote a simulation of the slave searching algorithm that we were discussing a few days ago here. From the brief testing that I did, it appears that the algorithm works as expected. Maybe this program could serve as a starting point for somebody who wants to implement it for real.
-- Russ
/* ////////////////////////////////////////////////////////////////////////////
enum.c
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: Simulation of Erik Malund's slave enumeration scheme as
described here:
http://www.8052.com/forumchat/read.phtml?id=153935
You'll want to read that description and the thread it's in to
fully understand this program.
REVISIONS: 28 Apr 08 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */
#include <stdio.h>
/* ////////////////////////////////////////////////////////////////////////////
Names for Numbers
//////////////////////////////////////////////////////////////////////////// */
#define ADDRESS_WIDTH 5 // Must be 32 or less
/* ////////////////////////////////////////////////////////////////////////////
Local Function Prototypes
//////////////////////////////////////////////////////////////////////////// */
void DoNode (int, int);
void SetBit (unsigned *, int);
void ClearBit(unsigned *, int);
int CheckBit(unsigned, int);
void Dump(unsigned);
int CheckSlaves(void);
/* ////////////////////////////////////////////////////////////////////////////
Module Variables
//////////////////////////////////////////////////////////////////////////// */
unsigned testAddress;
unsigned mask;
unsigned slaveAddresses[] = { 0x00000000, // Addresses of slaves present
0x10000000,
0x20000000,
0xD0000000,
0xE0000000,
0xF0000000
};
#define NUM_SLAVES (sizeof(slaveAddresses) / sizeof(unsigned))
/* ////////////////////////////////////////////////////////////////////////////
main()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: This function drives the simulation.
REVISIONS: 28 Apr 08 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */
void main() {
DoNode(0, 0);
DoNode(0, 1);
}
/* ////////////////////////////////////////////////////////////////////////////
DoNode()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: This function recursively processes the nodes in the tree of
slave addresses.
REVISIONS: 28 Apr 08 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */
void DoNode(int level, int bit) {
int matches;
bit ? SetBit (&testAddress, level) : // Cook up a new test address
ClearBit(&testAddress, level); // bit
SetBit(&mask, level); // Cook up a new mask bit
matches = CheckSlaves(); // Get count of matching slaves
printf("Address: "); Dump(testAddress); // Dump info for inspection to
printf(" Mask: "); Dump(mask); // see how the algorithm works
printf(" Matches: %d\n", matches);
if ((level < ADDRESS_WIDTH - 1) && // If not at bottom of tree,
(matches > 1)) { // and still need to search
DoNode(level + 1, 0); // further, descend one level
DoNode(level + 1, 1); // and keep searching
}
ClearBit(&mask, level); // Restore to previous level
ClearBit(&testAddress, level);
} // End DoNode()
/* ////////////////////////////////////////////////////////////////////////////
SetBit()
ClearBit()
CheckBit()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: Bit manipulators.
REVISIONS: 28 Apr 08 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */
void SetBit(unsigned *bits, int position) {
*bits |= (0x80000000U >> position);
}
void ClearBit(unsigned *bits, int position) {
*bits &= ~(0x80000000U >> position);
}
int CheckBit(unsigned bits, int position) {
return (bits & (0x80000000U >> position)) ? 1 : 0;
}
/* ////////////////////////////////////////////////////////////////////////////
Dump()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: Dumps a bit string
REVISIONS: 28 Apr 08 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */
void Dump(unsigned bits) {
int i;
for (i=0; i<ADDRESS_WIDTH; i++) {
printf(CheckBit(bits, i) ? "1" : "0");
}
} // End Dump()
/* ////////////////////////////////////////////////////////////////////////////
CheckSlaves()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: This function checks the current testAddress and mask against
a hard-coded list of slave addresses. It returns the number of
matches.
REVISIONS: 28 Apr 08 - RAC - Genesis
//////////////////////////////////////////////////////////////////////////// */
int CheckSlaves() {
int count; // Count matches here
int j; // A generic integer
count = 0; // No matches yet
for (j=0; j<NUM_SLAVES; j++) { // For each slave present
if ((testAddress & mask) ==
(slaveAddresses[j] & mask)) { // This slave matches
count++; // Count it
printf("Match with slave address " // Dump its address so we can
"%08X\n", slaveAddresses[j]); // see how the algorithm works
} // End 'this slave matches'
}
return count; // Count of matching slaves
} // End CheckSlaves()
|
| Topic | Author | Date |
| Followup on slave enumeration algorithm | 01/01/70 00:00 | |
| algorithm | 01/01/70 00:00 | |
| well, it's simple | 01/01/70 00:00 | |
| Typo | 01/01/70 00:00 | |
| a matter of time | 01/01/70 00:00 | |
| Time | 01/01/70 00:00 | |
| figure it out this way | 01/01/70 00:00 | |
| Guessing the time | 01/01/70 00:00 | |
| More on theory vs. practice | 01/01/70 00:00 | |
| the practical reality ... | 01/01/70 00:00 | |
| of course .... | 01/01/70 00:00 | |
| Well done..starting for third countries | 01/01/70 00:00 | |
| But still | 01/01/70 00:00 | |
| Starting for third countries | 01/01/70 00:00 | |
| Jecksons please read the previous thread | 01/01/70 00:00 | |
| Third countries | 01/01/70 00:00 | |
| I am feeling Dizzy | 01/01/70 00:00 | |
| and what does that tell us | 01/01/70 00:00 | |
Keeping this thread alive | 01/01/70 00:00 |



