This is file HX711HAL.c

This file is part of HX711 routines

by Isaac Marino Bavaresco

/*======================================================================================*/
/*
 Copyright (c) 2016-2019, Isaac Marino Bavaresco
 All rights reserved.
 isaacbavaresco@yahoo.com.br

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions are met:
     * Redistributions of source code must retain the above copyright
       notice, this list of conditions and the following disclaimer.
     * Neither the name of the author nor the
       names of its contributors may be used to endorse or promote products
       derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY
 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*============================================================================*/
/*======================================================================================*/
#include "HX711.h"
#include "HX711HAL.h"
/*======================================================================================*/
#define HX711_RESET_MODE    HX711_MODE_A128
/*======================================================================================*/
static hx711mode_t  HX711Mode   = HX711_RESET_MODE;
/*======================================================================================*/
int HX711IsOn( void )
    {
    return PDSCKQueryPin() == 0;
    }
/*======================================================================================*/
int HX711IsReady( void )
    {
    return HX711IsOn() && DOUTQueryPin() == 0;
    }
/*======================================================================================*/
static signed long _HX711Read( void )
    {
    signed long Result;
    int         i;

    /* Wait until the conversion is finished */
    while( !HX711IsReady() )
        SystemYield();

    DelayAtLeast100ns();

    /* Transfer the 24 bits of the result */
    for( Result = 0, i = 24; i > 0; i-- )
        {
        /*
        Shift the Result variable to the left to prepare its LSBit to receive the new
        bit from pin DOUT.
        */
        Result  <<= 1;
        /*
        Here we must guarantee that an interrupt or context switch will not keep pin
        PDSCK high for more than 60 us or else the HX711 will enter power down mode,
        so we use a critical section.
        */
        SystemEnterCritical();
        /* Set pin PDSCK high to start a bit transfer. */
        PDSCKSetPin();
        /* Wait for the PDSCK high time and for pin DOUT to stabilize. */
        DelayAtLeast200ns();
        /* Insert the bit from DOUT into the LSBit of Result. */
        Result  |= DOUTQueryPin();
        /* Set pin PDSCK low to finish the bit cycle. */
        PDSCKClearPin();
        /*
        Now that pin PDSCK is back to low we can exit the critical section, so
        interrupts or context switches can happen again.
        */
        SystemExitCritical();
        /* Wait for the PDSCK low time. */
        DelayAtLeast200ns();
        }

    for( i = HX711Mode + 1; i > 0; i-- )
        {
        /*
        Here we must guarantee that an interrupt or context switch will not keep pin
        PDSCK high for more than 60 us or else the HX711 will enter power down mode,
        so we use a critical section.
        */
        SystemEnterCritical();
        PDSCKSetPin();
        DelayAtLeast200ns();
        PDSCKClearPin();
        /*
        Now that pin PDSCK is back to low we can exit the critical section, so
        interrupts or context switches can happen again.
        */
        SystemExitCritical();
        DelayAtLeast200ns();
        }

    /* Extend the sign bit to the MSByte */
    Result  = ( Result & 0x00800000 ) ? ( Result | 0xff000000 ) : Result;

    return Result;
    }
/*======================================================================================*/
void HX711PowerUp( void )
    {
    PDSCKClearPin();
    if( HX711Mode != HX711_RESET_MODE )
        _HX711Read();
    }
/*======================================================================================*/
signed long HX711Read( void )
    {
    if( !HX711IsOn() )
        HX711PowerUp();

    return _HX711Read();
    }
/*======================================================================================*/
void HX711SetMode( hx711mode_t Mode )
    {
    HX711Mode   = Mode;
    if( HX711IsOn() )
        _HX711Read();
    }
/*======================================================================================*/
void HX711PowerDown( void )
    {
    PDSCKSetPin();
    DelayAtLeast60us();
    }
/*======================================================================================*/
void HX711Init( void )
    {
    HX711Mode   = HX711_RESET_MODE;

    DOUTSetAsInput();

    PDSCKSetPin();
    PDSCKSetAsOutput();

    HX711PowerDown();
    HX711PowerUp();
    }
/*======================================================================================*/