Mike Fruge / OneWire

Dependents:   Max32630_One_Wire_Interface

OneWire_Masters/GPIO/owgpio.cpp

Committer:
j3
Date:
2016-03-16
Revision:
15:f6cb0d906fb6
Parent:
14:7b2886a50321
Child:
17:b646b1e3970b

File content as of revision 15:f6cb0d906fb6:

/******************************************************************//**
* @file owgpio.cpp
*
* @author Justin Jordan
*
* @version 0.0.0
*
* Started: 31JAN16
*
* Updated: 
*
* @brief Source file for bit-banging a 1-wire master
***********************************************************************
* Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Maxim Integrated
* Products, Inc. shall not be used except as stated in the Maxim Integrated
* Products, Inc. Branding Policy.
*
* The mere transfer of this software does not imply any licenses
* of trade secrets, proprietary technology, copyrights, patents,
* trademarks, maskwork rights, or any other form of intellectual
* property whatsoever. Maxim Integrated Products, Inc. retains all
* ownership rights.
**********************************************************************/


//Bit-Bang Master only supported on MAX32600MBED Board


#if(TARGET_MAX32600)


#include "owgpio.h"
#include "tmr_regs.h"
#include "gpio_regs.h"
#include "clkman_regs.h"


//counts instances of this class being created
uint32_t OwGpio::ow_instance_cnt = 0;


//*********************************************************************
OwGpio::OwGpio(PinName ow_gpio, PinName ext_spu)
:_ext_spu(ext_spu, 1), _ow_speed(SPEED_STANDARD), _ow_level(LEVEL_NORMAL),
_ow_port(PINNAME_TO_PORT(ow_gpio)), _ow_pin(PINNAME_TO_PIN(ow_gpio))
{
    _ow_instance = OwGpio::ow_instance_cnt++;
}


//*********************************************************************
OwGpio::~OwGpio()
{
    release_ow_gpio();

    if(_ow_instance == OwGpio::ow_instance_cnt)
    {
        release_ow_timer();
    }
    
    OwGpio::ow_instance_cnt--;
}


//*********************************************************************
bool OwGpio::OWInitMaster()
{
    bool rtn_val = true;
    
    init_ow_gpio(_ow_port, _ow_pin);
    init_ow_timer();
    
    return(rtn_val);
}


//*********************************************************************
bool OwGpio::OWReset()
{
    bool rtn_val = false;
    uint16_t t0;
    uint16_t trstl;
    uint16_t tmsp;
    uint16_t tslot, tslot_0;
    uint32_t ow_micro_second(SystemCoreClock/1000000);
    
    if(_ow_speed == SPEED_STANDARD)
    {
        trstl = (ow_micro_second*TRSTL_STD);
        tmsp = (ow_micro_second*TMSP_STD);
    }
    else
    {
        trstl = (ow_micro_second*TRSTL_OVD);
        tmsp = (ow_micro_second*TMSP_OVD);
    }
    tslot = (2*trstl);
    
    //prevent erratic behavior due to rollover of counter
    //reset counter to 0, this will take less than 65.535ms
    MXC_TMR3->count16_0 = 0;
    
    tslot_0 = MXC_TMR3->count16_0;
    
    //pull-low
    MXC_GPIO->out_val[_ow_port] &= ~(1 << _ow_pin);
    
    //get current timer val
    t0 = MXC_TMR3->count16_0;
    //wait specified time for reset pulse
    while((MXC_TMR3->count16_0 - t0) < trstl);
    
    //let go of pin
    MXC_GPIO->out_val[_ow_port] |= (1 << _ow_pin);
    
    //get current timer val
    t0 = MXC_TMR3->count16_0;
    //wait specified time for master sample
    while((MXC_TMR3->count16_0 - t0) < tmsp);
    
    //get sample
    rtn_val =  (MXC_GPIO->in_val[_ow_port] & (1 << _ow_pin));
    
    //wait for slot time to finish, includes recovery
    while((MXC_TMR3->count16_0 - tslot_0) < tslot);
        
    return(!rtn_val);
}


//*********************************************************************
uint8_t OwGpio::OWTouchBit(uint8_t sendbit)
{
    uint8_t rtn_val;
    uint16_t low_time;
    uint32_t ow_micro_second(SystemCoreClock/1000000);
    uint32_t tmsr, tslot, tslot_0, t0;

    if(sendbit & 0x01)
    {
        if(_ow_speed == SPEED_STANDARD)
        {
            //write one and read low time
            //are the same for 1-wire
            low_time = (ow_micro_second*TW1L_STD);
            tslot = (ow_micro_second*TSLOT_STD);
            tmsr = (ow_micro_second*TMSR_STD);
        }
        else
        {
            low_time = (ow_micro_second*TW1L_OVD);
            tslot = (ow_micro_second*TSLOT_OVD);
            tmsr = (ow_micro_second*TMSR_OVD);
        }
    }
    else
    {
        if(_ow_speed == SPEED_STANDARD)
        {
            //write 0
            low_time = (ow_micro_second*TW0L_STD);
            tslot = (ow_micro_second*TSLOT_STD);
            tmsr = (ow_micro_second*TMSR_STD);
        }
        else
        {
            low_time = (ow_micro_second*TW0L_OVD);
            tslot = (ow_micro_second*TSLOT_OVD);
            tmsr = (ow_micro_second*TMSR_OVD);
        }
    }
    
    //prevent erratic behavior due to rollover of counter
    //reset counter to 0, this will take less than 65.535ms
    MXC_TMR3->count16_0 = 0;
    
    tslot_0 = MXC_TMR3->count16_0;
    
    //pull-low
    MXC_GPIO->out_val[_ow_port] &= ~(1 << _ow_pin);
    
    //get current timer val
    t0 = MXC_TMR3->count16_0;
    //wait specified time for reset pulse
    while((MXC_TMR3->count16_0 - t0) < low_time);
    
    //let go of pin
    MXC_GPIO->out_val[_ow_port] |= (1 << _ow_pin);
    
    //get current timer val
    t0 = MXC_TMR3->count16_0;
    //wait specified time for reset pulse
    while((MXC_TMR3->count16_0 - t0) < tmsr);
    
    //get sample
    rtn_val =  (MXC_GPIO->in_val[_ow_port] & (1 << _ow_pin));
    
    //wait for slot time to finish, includes recovery
    while((MXC_TMR3->count16_0 - tslot_0) < tslot);
    
    return(rtn_val >> _ow_pin);
}



//*********************************************************************
bool OwGpio::OWWriteByte(uint8_t sendbyte)
{
    bool rtn_val = true; //no echo with bit-bang
    uint8_t idx;
    
    for(idx = 0; idx < 8; idx++)
    {
        OWWriteBit(0x01 & (sendbyte >> idx));
    }
    
    return rtn_val;
}


//*********************************************************************
uint8_t OwGpio::OWReadByte(void)
{
    uint8_t rtn_val = 0;
    uint8_t idx;
    
    for(idx = 0; idx < 8; idx++)
    {
        rtn_val |= ((0x01 & OWReadBit()) << idx);
    }
    
    return(rtn_val);
}


//*********************************************************************
bool OwGpio::OWSearch(void)
{
   //stolen from AN187 
   //https://www.maximintegrated.com/en/app-notes/index.mvp/id/187
   
   uint32_t id_bit_number;
   int last_zero, rom_byte_number;
   int id_bit, cmp_id_bit;
   uint8_t rom_byte_mask, search_direction;
   
   bool search_result = false;

   // 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 (!_last_device_flag)
   {
      // 1-Wire reset
      if (!OWReset())
      {
         // reset the search
         _last_discrepancy = 0;
         _last_device_flag = false;
         _last_family_discrepancy = 0;
         return false;
      }

      // issue the search command 
      OWWriteByte(0xF0);  

      // 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 < _last_discrepancy)
                  search_direction = ((_rom_number[rom_byte_number] & rom_byte_mask) > 0);
               else
                  // if equal to last pick 1, if not then pick 0
                  search_direction = (id_bit_number == _last_discrepancy);

               // 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)
                     _last_family_discrepancy = last_zero;
               }
            }

            // set or clear the bit in the ROM byte rom_byte_number
            // with mask rom_byte_mask
            if (search_direction == 1)
              _rom_number[rom_byte_number] |= rom_byte_mask;
            else
              _rom_number[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)
            {
                OWCalc_crc8(_rom_number[rom_byte_number], _crc8);  // 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 _last_discrepancy,_last_device_flag,search_result
         _last_discrepancy = last_zero;

         // check for last device
         if (_last_discrepancy == 0)
            _last_device_flag = true;
         
         search_result = true;
      }
   }

   // if no device found then reset counters so next 'search' will be like a first
   if (!search_result || !_rom_number[0])
   {
      _last_discrepancy = 0;
      _last_device_flag = false;
      _last_family_discrepancy = 0;
      search_result = false;
   }

   return search_result;
}


//*********************************************************************
uint8_t OwGpio::OWSpeed(OW_SPEED new_speed)
{
    _ow_speed = new_speed;
    
    return(_ow_speed);
}


//*********************************************************************
uint8_t OwGpio::OWLevel(OW_LEVEL new_level)
{
    uint8_t rtn_val = 0;

    return(rtn_val);
}


//*********************************************************************
bool OwGpio::OWWriteBytePower(uint8_t sendbyte)
{
    bool rtn_val = false;
    
    //TODO
    
    return rtn_val;
}


//*********************************************************************
bool OwGpio::OWReadBitPower(uint8_t applyPowerResponse)
{
    bool rtn_val = false;
    
    //TODO
    
    return rtn_val;
}


//*********************************************************************
void OwGpio::init_ow_gpio(uint32_t ow_port, uint32_t ow_pin)
{
    if (MXC_CLKMAN->clk_ctrl_1_gpio == MXC_E_CLKMAN_CLK_SCALE_DISABLED) 
    {
        MXC_CLKMAN->clk_ctrl_1_gpio = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
    }

    /* Set function */
    MXC_GPIO->func_sel[ow_port] &= ~(0xF << (4 * ow_pin));

    /* Normal input is always enabled */
    MXC_GPIO->in_mode[ow_port] &= ~(0xF << (4 * ow_pin));
    
    //config pin for open drain output
    //read port out_mode
    uint32_t ow_out_mode = MXC_GPIO->out_mode[ow_port];
    //clear the mode for ow_pin
    ow_out_mode &= ~(0xF << (ow_pin * 4));
    //write ow_pin mode and original data back
    MXC_GPIO->out_mode[ow_port] |= (ow_out_mode | (MXC_V_GPIO_OUT_MODE_OPEN_DRAIN_WEAK_PULLUP << (ow_pin * 4)));
}


//*********************************************************************
void OwGpio::init_ow_timer(void)
{
    static bool first_entry = true;
    
    if(first_entry)
    {
        first_entry = false;
        
        //disable timer, set 16bit mode, continuous mode, set prescale of 1
        MXC_TMR3->ctrl = ((0 << MXC_F_TMR_CTRL_ENABLE0_POS) | 
                          (1 << MXC_F_TMR_CTRL_TMR2X16_POS) | 
                          (MXC_E_TMR_MODE_CONTINUOUS << MXC_F_TMR_CTRL_MODE_POS) | 
                          (0 << MXC_F_TMR_CTRL_PRESCALE_POS));
        
        //init to count of 0                  
        MXC_TMR3->count16_0 = 0;
        //set terminal value
        MXC_TMR3->term_cnt16_0 = 0xFFFF;
        //Start hardware timer
        MXC_TMR3->ctrl |= MXC_F_TMR_CTRL_ENABLE0;
    }
}


//*********************************************************************
void OwGpio::release_ow_timer(void)
{
}


//*********************************************************************
void OwGpio::release_ow_gpio()
{
}

#endif/* TARGET_MAX32600*/