Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Max32630_One_Wire_Interface
OneWire_Masters/GPIO/owgpio.cpp
- Committer:
- j3
- Date:
- 2016-03-05
- Revision:
- 11:2833f27ba319
- Parent:
- 10:0df2cc66fc47
- Child:
- 12:707d2c9ce749
File content as of revision 11:2833f27ba319:
/******************************************************************//**
* @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"
//*********************************************************************
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))
{
init_ow_gpio(ow_gpio);
}
//*********************************************************************
OwGpio::~OwGpio()
{
}
//*********************************************************************
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);
}
//*********************************************************************
void OwGpio::OWWriteBit(uint8_t sendbit)
{
OWTouchBit(sendbit);
}
//*********************************************************************
uint8_t OwGpio::OWReadBit()
{
return(OWTouchBit(0x01));
}
//*********************************************************************
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 = 0;
tslot = (ow_micro_second*TSLOT_OVD);
tmsr = 0;
}
}
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 = 0;
}
}
//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);
}
//*********************************************************************
uint8_t OwGpio::OWTouchByte(uint8_t sendbyte)
{
uint8_t rtn_val;
if (sendbyte == 0xFF)
{
rtn_val = OWReadByte();
}
else
{
OWWriteByte(sendbyte);
rtn_val = sendbyte;
}
return(rtn_val);
}
//*********************************************************************
void OwGpio::OWBlock(uint8_t *tran_buf, uint8_t tran_len)
{
uint8_t i;
for (i = 0; i < tran_len; i++)
{
tran_buf[i] = OWTouchByte(tran_buf[i]);
}
}
//*********************************************************************
void OwGpio::OWWriteBlock(const uint8_t *tran_buf, uint8_t tran_len)
{
uint8_t idx;
for(idx = 0; idx < tran_len; idx++)
{
OWWriteByte(tran_buf[idx]);
}
}
//*********************************************************************
void OwGpio::OWReadBlock(uint8_t *recv_buf, uint8_t recv_len)
{
uint8_t idx;
for(idx = 0; idx < recv_len; idx++)
{
recv_buf[idx] = OWReadByte();
}
}
//*********************************************************************
bool OwGpio::OWFirst(void)
{
// reset the search state
_last_discrepancy = 0;
_last_device_flag = false;
_last_family_discrepancy = 0;
return OWSearch();
}
//*********************************************************************
bool OwGpio::OWNext(void)
{
// leave the search state alone
return OWSearch();
}
//*********************************************************************
bool OwGpio::OWVerify(void)
{
bool rtn_val = false;
uint8_t rom_backup[8];
uint8_t i,rslt,ld_backup,ldf_backup,lfd_backup;
// keep a backup copy of the current state
for (i = 0; i < 8; i++)
{
rom_backup[i] = _rom_number[i];
}
ld_backup = _last_discrepancy;
ldf_backup = _last_device_flag;
lfd_backup = _last_family_discrepancy;
// set search to find the same device
_last_discrepancy = 64;
_last_device_flag = false;
if (OWSearch())
{
// check if same device found
rslt = true;
for (i = 0; i < 8; i++)
{
if (rom_backup[i] != _rom_number[i])
{
rslt = false;
break;
}
}
}
else
{
rslt = false;
}
// restore the search state
for (i = 0; i < 8; i++)
{
_rom_number[i] = rom_backup[i];
}
_last_discrepancy = ld_backup;
_last_device_flag = ldf_backup;
_last_family_discrepancy = lfd_backup;
// return the result of the verify
rtn_val = rslt;
return(rtn_val);
}
//*********************************************************************
void OwGpio::OWTargetSetup(uint8_t family_code)
{
uint8_t i;
// set the search state to find SearchFamily type devices
_rom_number[0] = family_code;
for (i = 1; i < 8; i++)
{
_rom_number[i] = 0;
}
_last_discrepancy = 64;
_last_family_discrepancy = 0;
_last_device_flag = false;
}
//*********************************************************************
void OwGpio::OWFamilySkipSetup(void)
{
// set the Last discrepancy to last family discrepancy
_last_discrepancy = _last_family_discrepancy;
// clear the last family discrpepancy
_last_family_discrepancy = 0;
// check for end of list
if (_last_discrepancy == 0)
{
_last_device_flag = true;
}
}
//*********************************************************************
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;
}
//*********************************************************************
bool OwGpio::OWReadROM(void)
{
bool rtn_val = false;
if(!OWReset())
{
rtn_val = false;
}
else
{
if(!OWWriteByte(READ_ROM))
{
rtn_val = false;
}
else
{
OWReadBlock(_rom_number, ROMnumberLen);
rtn_val = true;
}
}
return rtn_val;
}
//*********************************************************************
bool OwGpio::OWSkipROM(void)
{
bool rtn_val = false;
if(!OWReset())
{
rtn_val = false;
}
else
{
if(!OWWriteByte(SKIP_ROM))
{
rtn_val = false;
}
else
{
rtn_val = true;
}
}
return rtn_val;
}
//*********************************************************************
bool OwGpio::OWMatchROM(void)
{
bool rtn_val = false;
uint8_t idx;
if(!OWReset())
{
rtn_val = false;
}
else
{
if(!OWWriteByte(MATCH_ROM))
{
rtn_val = false;
}
else
{
for(idx = 0; idx < ROMnumberLen; idx++)
{
OWWriteByte(_rom_number[idx]);
}
rtn_val = true;
}
}
return rtn_val;
}
//*********************************************************************
bool OwGpio::OWOverdriveSkipROM(void)
{
bool rtn_val = false;
if(!OWReset())
{
rtn_val = false;
}
else
{
if(!OWWriteByte(OVERDRIVE_SKIP))
{
rtn_val = false;
}
else
{
//change speed for subsequent comands
OWSpeed(SPEED_OVERDRIVE);
rtn_val = true;
}
}
return rtn_val;
}
//*********************************************************************
bool OwGpio::OWOverdriveMatchROM(void)
{
bool rtn_val = false;
uint8_t idx;
if(!OWReset())
{
rtn_val = false;
}
else
{
if(!OWWriteByte(OVERDRIVE_MATCH))
{
rtn_val = false;
}
else
{
//change speed before sending ROM number
OWSpeed(SPEED_OVERDRIVE);
for(idx = 0; idx < ROMnumberLen; idx++)
{
OWWriteByte(_rom_number[idx]);
}
rtn_val = true;
}
}
return rtn_val;
}
//*********************************************************************
bool OwGpio::OWResume(void)
{
bool rtn_val = false;
if(!OWReset())
{
rtn_val = false;
}
else
{
if(!OWWriteByte(RESUME))
{
rtn_val = false;
}
else
{
rtn_val = true;
}
}
return rtn_val;
}
//*********************************************************************
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;
return(rtn_val);
}
//*********************************************************************
bool OwGpio::OWWriteBytePower(uint8_t sendbyte)
{
bool rtn_val;
//TODO
return rtn_val;
}
//*********************************************************************
bool OwGpio::OWReadBitPower(uint8_t applyPowerResponse)
{
bool rtn_val;
//TODO
return rtn_val;
}
//*********************************************************************
const uint8_t (&OwGpio::OWgetROMnumber() const)[ROMnumberLen]
{
return _rom_number;
}
//*********************************************************************
void OwGpio::init_ow_gpio(PinName ow_gpio)
{
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;
}
MBED_ASSERT(ow_gpio != (PinName)NC);
uint32_t ow_port = PINNAME_TO_PORT(ow_gpio);
uint32_t ow_pin = PINNAME_TO_PIN(ow_gpio);
/* 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;
}
}
#endif/* TARGET_MAX32600*/