Page 1 of 2

Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 4:26 am
by CheBuzz
I am attempting to communicate to a 1-wire sensor using the STM32F4 Discovery board. I have this code from the examples provided by Maxim on how to talk to their 1-wire bus. The problem comes from implementing the uS delay resolution. What is the best way to implement the tickDelay function (as I have noticed the chThdSleepMicrosecond() funciton does not provide enough granularity).

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?

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 6:27 am
by barthess
To achieve uS resolution the best way is using dedicated hardware timer, GPT in terms of ChibiOS. About inclusion in HAL is better to ask Giovanni.

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 6:54 am
by CheBuzz
I had figured that uS resolution is going to require a hardware timer. I guess the question is more of a best way to implement it. Here is what I have come up with so far, which is causing the processor to hang, so I must be forgetting something:

Code: Select all

uint8_t uS_ticks;

/*
 * GPT3 callback.
 */
static void gpt3cb(GPTDriver *gptp)
{
  (void)gptp;
  uS_ticks++;
}

/*
 * GPT3 configuration.
 */
static const GPTConfig gpt3cfg = {
  100000,    /* 100KHz timer clock.*/
  gpt3cb    /* Timer callback.*/
};

void OWInit(void)
{
   uS_ticks = 0;
   gptStart(&GPTD3, &gpt3cfg);
   gptPolledDelay(&GPTD3, 10); /* Small delay.*/
}

// '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;
// Since the tick counter is incremented every 10uS, use these values
int A = 1,B = 6,C = 6,D = 1,E = 1,F = 6,G = 0,H = 48,I = 7,J = 41;

// Pause for exactly 'tick' number of ticks = 0.25us
inline void tickDelay(int tick)
{
   uS_ticks = 0;
   while(uS_ticks < tick);
}


It seems like the timer is not running, despite calling gptStart(). Am I missing something obvious?

And would there be a "better way" than spinning in a while loop waiting for the ticks to come in? Perhaps a sleep and having the ISR wake the task when X number of ticks come in?

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 7:58 am
by barthess
What MCU you use?
1. do not use gptPolledDelay() and while(uS_ticks < tick).

How would I:

Code: Select all

timer callback(){
signal OW_semaphore;
}


Code: Select all

int OWReadBit(void)
{
set pad LOW;
write A to counter;
run timer in one shot mode;
wait in OW_semaphore;

set pad HIGH
write E to counter;
run timer in one shot mode;
wait in OW_semaphore;

etc.
}


edit:
I have never use one wire bus previously. All aforesaid words is my theoretic understanding.

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 9:06 am
by Giovanni
Hi,

If you want a non-polled delay you need a semaphore, for example a Binary Semaphore.

1)Initialize a Binary semaphore as non-taken in your application initialization.
2)Initialize a GPT.

In the GPT callback:

1)chSysLockFromIsr();
2)chBSemSignalI(semaphore);
3)chSysUnlockFromIsr();

In order to delay:

1)chSysLock();
2)gptStartOneShotI(...);
3)chBSemWaitS(semaphore);
4)chSysUnlock();

Note that for very small delays a polled approach is recommended because the overhead of ISR handling at high rates.

Giovanni

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 7:00 pm
by CheBuzz
1-wire protocol requires waits anywhere from 6 uS to 480 uS. Because of the higher upper end, I imagine it would be more efficient to implement a non-polled approach? Is there a good "rule of thumb" of when to use polled and when to use interrupts considering ChibiOS's overhead to each?

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 7:33 pm
by Giovanni
You could do a mix, from measurements done using the IRQ_STORM test an STM32 at 72MHz should be able to handle IRQ rates down to 10uS (waking a single thread) but keep in mind that IRQs have response time jitter so consider also this aspect. I would set a threshold at 100uS but you can experimentally optimize the value.

Giovanni

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 10:08 pm
by CheBuzz
Since the 1-wire bus only has very long waits (> 100uS) during bus reset, I implemented a polled wait for all but the two long waits for the bus reset. Using exclusively polled waits seems just fine. But when I use the mixed-method, the processor is hanging. Here is the code:

Code: Select all

#include "ch.h"
#include "hal.h"

#include "chprintf.h"

static BSEMAPHORE_DECL(bsem, FALSE);

/*
 * GPT3 callback.
 */
static void gpt3cb(GPTDriver *gptp) {
   (void) gptp;
   chSysLockFromIsr();
   chBSemSignalI(&bsem);
   chSysUnlockFromIsr();
}

static const GPTConfig gpt3cfg = { 200000, /* 100KHz timer clock.*/
gpt3cb /* Timer callback.*/
};

const uint8_t A = 2, B = 13, C = 12, D = 2, E = 2, F = 11, G = 0, H = 96, I = 14, J = 82;

// Pause for exactly 'tick' number of ticks = 5uS
void tickDelay(int tick)
{
   gptStart(&GPTD3, &gpt3cfg);
   gptPolledDelay(&GPTD3, tick);
}

// Used for long waits (> 100uS) to free up processor to do other things
void tickSleep(int tick) {
   chBSemReset(&bsem, FALSE);
   chSysLock();
   gptStartOneShotI(&GPTD3, tick);
   chBSemWaitS(&bsem);
   chSysUnlock();
}

//-----------------------------------------------------------------------------
// Generate a 1-Wire reset, return 1 if no presence detect was found,
// return 0 otherwise.
//
uint8_t OWTouchReset(void) {
   uint8_t result;

   palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_OUTPUT_OPENDRAIN);
   palWritePad(GPIOC, GPIOC_1WIRE, PAL_LOW);
   tickSleep(H);
   palWritePad(GPIOC, GPIOC_1WIRE, PAL_HIGH); // Releases the bus
   tickDelay(I);
   palSetPadMode(GPIOC, GPIOC_1WIRE, PAL_MODE_INPUT);
   result = palReadPad(GPIOC, GPIOC_1WIRE); // Sample for presence pulse from slave
   tickSleep(J); // Complete the reset sequence recovery
   return result; // Return sample presence pulse result
}


This code is hanging upon calling tickSleep(). Any ideas why?

You'll notice also that I set a 200KHz timer and doubled the amount of 'ticks' to wait. It seems like there is a bug that was causing it to hang when asking to wait just 1 'tick'.

Re: Maxim 1-wire with ChibiOS

Posted: Mon Dec 12, 2011 10:17 pm
by Giovanni
Probably because the semaphore should be initialized as "taken" instead of "not taken" as I suggested, my fault.

Giovanni

Re: Maxim 1-wire with ChibiOS

Posted: Wed Dec 14, 2011 9:32 am
by Jacon
Considering problems described in above posts, Maxim recommends another
(but uncoventional :) ) solution for 32bit MCUs in their own AppNote:

http://www.maxim-ic.com/app-notes/index.mvp/id/214

It's definitely worth to try, especially using our Chibi UART driver with
DMA & properly sized buffers 8-)