Code: Select all
#include "ch.h"
#include "hal.h"
// 'tick' values, only use standard (no overdrive) timing
int A = 6,B = 64,C = 60,D = 10,E = 9,F = 55,G = 0,H = 480,I = 70,J = 410;
// Pause for exactly 'tick' number of ticks = 0.25us
inline void tickDelay(int tick)
{
chThdSleepMicroseconds(tick);
}
//-----------------------------------------------------------------------------
// Generate a 1-Wire reset, return 1 if no presence detect was found,
// return 0 otherwise.
// (NOTE: Does not handle alarm presence from DS2404/DS1994)
//
int OWTouchReset(void)
{
int result;
tickDelay(G);
palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_OUTPUT_OPENDRAIN);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_LOW);
tickDelay(H); // Sleep 1920 microseconds
palWritePad(GPIOC, GPIOC_1WIRE, PAL_HIGH); // Releases the bus
tickDelay(I);
palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_INPUT);
result = palReadPad(GPIOC, GPIOC_1WIRE) ^ 0x01; // Sample for presence pulse from slave
tickDelay(J); // Complete the reset sequence recovery
return result; // Return sample presence pulse result
}
//-----------------------------------------------------------------------------
// Send a 1-Wire write bit. Provide 10us recovery time.
// int A = 6,B = 64,C = 60,D = 10,E = 9,F = 55,G = 0,H = 480,I = 70,J = 410;
void OWWriteBit(int bit)
{
if (bit)
{
// Write '1' bit
palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_OUTPUT_OPENDRAIN);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_LOW); // Drives DQ low
tickDelay(A);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_HIGH); // Releases the bus
tickDelay(B); // Complete the time slot and 10us recovery
}
else
{
// Write '0' bit
palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_OUTPUT_OPENDRAIN);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_LOW); // Drives DQ low
tickDelay(C);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_HIGH); // Releases the bus
tickDelay(D);
}
}
//-----------------------------------------------------------------------------
// Read a bit from the 1-Wire bus and return it. Provide 10us recovery time.
//
int OWReadBit(void)
{
int result;
palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_OUTPUT_OPENDRAIN);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_LOW); // Drives DQ low
tickDelay(A);
palWritePad(GPIOC, GPIOC_1WIRE, PAL_HIGH); // Releases the bus
tickDelay(E);
palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_INPUT);
result = palReadPad(GPIOC, GPIOC_1WIRE) & 0x01; // Sample the bit value from the slave
tickDelay(F); // Complete the time slot and 10us recovery
return result;
}
//-----------------------------------------------------------------------------
// Write 1-Wire data byte
//
void OWWriteByte(int data)
{
int loop;
// Loop to write each bit in the byte, LS-bit first
for (loop = 0; loop < 8; loop++)
{
OWWriteBit(data & 0x01);
// shift the data byte for the next bit
data >>= 1;
}
}
//-----------------------------------------------------------------------------
// Read 1-Wire data byte and return it
//
int OWReadByte(void)
int loop, result=0;
for (loop = 0; loop < 8; loop++)
{
// shift the result to get it ready for the next bit
result >>= 1;
// if result is one, then set MS bit
if (OWReadBit())
result |= 0x80;
}
return result;
}
//-----------------------------------------------------------------------------
// Write a 1-Wire data byte and return the sampled result.
//
int OWTouchByte(int data)
{
int loop, result=0;
for (loop = 0; loop < 8; loop++)
{
// shift the result to get it ready for the next bit
result >>= 1;
// If sending a '1' then read a bit else write a '0'
if (data & 0x01)
{
if (OWReadBit())
result |= 0x80;
}
else
OWWriteBit(0);
// shift the data byte for the next bit
data >>= 1;
}
return result;
}
//-----------------------------------------------------------------------------
// Write a block 1-Wire data bytes and return the sampled result in the same
// buffer.
//
void OWBlock(unsigned char *data, int data_len)
{
int loop;
for (loop = 0; loop < data_len; loop++)
{
data[loop] = OWTouchByte(data[loop]);
}
}
void Read_ROMCode(void)
{
int n;
char dat[9];
chprintf(&SD2, "\nReading ROM Code\n");
OWTouchReset();
OWWriteByte(0x33);
for (n=0;n<8;n++)
{
dat[n]=OWReadByte();
}
chprintf(&SD2, "\n ROM Code = %X%X%X%X\n",dat[7],dat[6],dat[5],dat[4],dat[3],dat[2],dat[1],dat[0]);
}
Finally, could this be a good addition to the ChibiOS HAL layer?