mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_Maxim/TARGET_MAX32620C/mxc/owm.c@188:3f10722804f9, 2018-07-30 (annotated)
- Committer:
- ua1arn
- Date:
- Mon Jul 30 12:31:10 2018 +0000
- Revision:
- 188:3f10722804f9
- Parent:
- 186:707f6e361f3e
before add multi-configuration USB descriptors
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Anna Bridge |
186:707f6e361f3e | 1 | /** |
Anna Bridge |
186:707f6e361f3e | 2 | * @file |
Anna Bridge |
186:707f6e361f3e | 3 | * @brief 1-Wire Master (OWM) API Function Implementations. |
Anna Bridge |
186:707f6e361f3e | 4 | */ |
Anna Bridge |
186:707f6e361f3e | 5 | /* ***************************************************************************** |
Anna Bridge |
186:707f6e361f3e | 6 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
Anna Bridge |
186:707f6e361f3e | 7 | * |
Anna Bridge |
186:707f6e361f3e | 8 | * Permission is hereby granted, free of charge, to any person obtaining a |
Anna Bridge |
186:707f6e361f3e | 9 | * copy of this software and associated documentation files (the "Software"), |
Anna Bridge |
186:707f6e361f3e | 10 | * to deal in the Software without restriction, including without limitation |
Anna Bridge |
186:707f6e361f3e | 11 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
Anna Bridge |
186:707f6e361f3e | 12 | * and/or sell copies of the Software, and to permit persons to whom the |
Anna Bridge |
186:707f6e361f3e | 13 | * Software is furnished to do so, subject to the following conditions: |
Anna Bridge |
186:707f6e361f3e | 14 | * |
Anna Bridge |
186:707f6e361f3e | 15 | * The above copyright notice and this permission notice shall be included |
Anna Bridge |
186:707f6e361f3e | 16 | * in all copies or substantial portions of the Software. |
Anna Bridge |
186:707f6e361f3e | 17 | * |
Anna Bridge |
186:707f6e361f3e | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
Anna Bridge |
186:707f6e361f3e | 19 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
Anna Bridge |
186:707f6e361f3e | 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
Anna Bridge |
186:707f6e361f3e | 21 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
Anna Bridge |
186:707f6e361f3e | 22 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
Anna Bridge |
186:707f6e361f3e | 23 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
Anna Bridge |
186:707f6e361f3e | 24 | * OTHER DEALINGS IN THE SOFTWARE. |
Anna Bridge |
186:707f6e361f3e | 25 | * |
Anna Bridge |
186:707f6e361f3e | 26 | * Except as contained in this notice, the name of Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 27 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
Anna Bridge |
186:707f6e361f3e | 28 | * Products, Inc. Branding Policy. |
Anna Bridge |
186:707f6e361f3e | 29 | * |
Anna Bridge |
186:707f6e361f3e | 30 | * The mere transfer of this software does not imply any licenses |
Anna Bridge |
186:707f6e361f3e | 31 | * of trade secrets, proprietary technology, copyrights, patents, |
Anna Bridge |
186:707f6e361f3e | 32 | * trademarks, maskwork rights, or any other form of intellectual |
Anna Bridge |
186:707f6e361f3e | 33 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
Anna Bridge |
186:707f6e361f3e | 34 | * ownership rights. |
Anna Bridge |
186:707f6e361f3e | 35 | * |
Anna Bridge |
186:707f6e361f3e | 36 | * $Date: 2016-03-14 10:08:53 -0500 (Mon, 14 Mar 2016) $ |
Anna Bridge |
186:707f6e361f3e | 37 | * $Revision: 21855 $ |
Anna Bridge |
186:707f6e361f3e | 38 | * |
Anna Bridge |
186:707f6e361f3e | 39 | **************************************************************************** */ |
Anna Bridge |
186:707f6e361f3e | 40 | |
Anna Bridge |
186:707f6e361f3e | 41 | /* **** Includes **** */ |
Anna Bridge |
186:707f6e361f3e | 42 | #include <string.h> |
Anna Bridge |
186:707f6e361f3e | 43 | #include "mxc_assert.h" |
Anna Bridge |
186:707f6e361f3e | 44 | #include "mxc_sys.h" |
Anna Bridge |
186:707f6e361f3e | 45 | #include "owm.h" |
Anna Bridge |
186:707f6e361f3e | 46 | |
Anna Bridge |
186:707f6e361f3e | 47 | /** |
Anna Bridge |
186:707f6e361f3e | 48 | * @ingroup owm |
Anna Bridge |
186:707f6e361f3e | 49 | * @{ |
Anna Bridge |
186:707f6e361f3e | 50 | */ |
Anna Bridge |
186:707f6e361f3e | 51 | ///@cond |
Anna Bridge |
186:707f6e361f3e | 52 | /* **** Definitions **** */ |
Anna Bridge |
186:707f6e361f3e | 53 | #define OWM_CLK_FREQ 1000000 //1-Wire requires 1MHz clock |
Anna Bridge |
186:707f6e361f3e | 54 | |
Anna Bridge |
186:707f6e361f3e | 55 | /* **** Globals **** */ |
Anna Bridge |
186:707f6e361f3e | 56 | int LastDiscrepancy; |
Anna Bridge |
186:707f6e361f3e | 57 | int LastDeviceFlag; |
Anna Bridge |
186:707f6e361f3e | 58 | |
Anna Bridge |
186:707f6e361f3e | 59 | /* **** Functions **** */ |
Anna Bridge |
186:707f6e361f3e | 60 | static uint8_t CalculateCRC8(uint8_t* data, int len); |
Anna Bridge |
186:707f6e361f3e | 61 | static uint8_t update_crc8(uint8_t crc, uint8_t value); |
Anna Bridge |
186:707f6e361f3e | 62 | ///@endcond |
Anna Bridge |
186:707f6e361f3e | 63 | |
Anna Bridge |
186:707f6e361f3e | 64 | |
Anna Bridge |
186:707f6e361f3e | 65 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 66 | int OWM_Init(mxc_owm_regs_t *owm, const owm_cfg_t *cfg, const sys_cfg_owm_t *sys_cfg) |
Anna Bridge |
186:707f6e361f3e | 67 | { |
Anna Bridge |
186:707f6e361f3e | 68 | int err = 0; |
Anna Bridge |
186:707f6e361f3e | 69 | uint32_t owm_clk, clk_div = 0; |
Anna Bridge |
186:707f6e361f3e | 70 | uint32_t ext_pu_mode = 0; |
Anna Bridge |
186:707f6e361f3e | 71 | uint32_t ext_pu_polarity = 0; |
Anna Bridge |
186:707f6e361f3e | 72 | |
Anna Bridge |
186:707f6e361f3e | 73 | // Check the OWM register pointer is valid |
Anna Bridge |
186:707f6e361f3e | 74 | MXC_ASSERT(MXC_OWM_GET_IDX(owm) >= 0); |
Anna Bridge |
186:707f6e361f3e | 75 | |
Anna Bridge |
186:707f6e361f3e | 76 | if(cfg == NULL) { |
Anna Bridge |
186:707f6e361f3e | 77 | return E_NULL_PTR; |
Anna Bridge |
186:707f6e361f3e | 78 | } |
Anna Bridge |
186:707f6e361f3e | 79 | |
Anna Bridge |
186:707f6e361f3e | 80 | // Set system level configurations |
Anna Bridge |
186:707f6e361f3e | 81 | if ((err = SYS_OWM_Init(owm, sys_cfg)) != E_NO_ERROR) { |
Anna Bridge |
186:707f6e361f3e | 82 | return err; |
Anna Bridge |
186:707f6e361f3e | 83 | } |
Anna Bridge |
186:707f6e361f3e | 84 | |
Anna Bridge |
186:707f6e361f3e | 85 | // Configure clk divisor to get 1MHz OWM clk |
Anna Bridge |
186:707f6e361f3e | 86 | owm_clk = SYS_OWM_GetFreq(owm); |
Anna Bridge |
186:707f6e361f3e | 87 | |
Anna Bridge |
186:707f6e361f3e | 88 | if(owm_clk == 0) { |
Anna Bridge |
186:707f6e361f3e | 89 | return E_UNINITIALIZED; |
Anna Bridge |
186:707f6e361f3e | 90 | } |
Anna Bridge |
186:707f6e361f3e | 91 | |
Anna Bridge |
186:707f6e361f3e | 92 | // Return error if clk doesn't divide evenly to 1MHz |
Anna Bridge |
186:707f6e361f3e | 93 | if(owm_clk % OWM_CLK_FREQ) { |
Anna Bridge |
186:707f6e361f3e | 94 | return E_NOT_SUPPORTED; |
Anna Bridge |
186:707f6e361f3e | 95 | } |
Anna Bridge |
186:707f6e361f3e | 96 | |
Anna Bridge |
186:707f6e361f3e | 97 | clk_div = (owm_clk / (OWM_CLK_FREQ)); |
Anna Bridge |
186:707f6e361f3e | 98 | |
Anna Bridge |
186:707f6e361f3e | 99 | // Can not support lower frequencies |
Anna Bridge |
186:707f6e361f3e | 100 | if(clk_div == 0) { |
Anna Bridge |
186:707f6e361f3e | 101 | return E_NOT_SUPPORTED; |
Anna Bridge |
186:707f6e361f3e | 102 | } |
Anna Bridge |
186:707f6e361f3e | 103 | |
Anna Bridge |
186:707f6e361f3e | 104 | // Select the PU mode and polarity based on cfg input |
Anna Bridge |
186:707f6e361f3e | 105 | switch(cfg->ext_pu_mode) |
Anna Bridge |
186:707f6e361f3e | 106 | { |
Anna Bridge |
186:707f6e361f3e | 107 | case OWM_EXT_PU_ACT_HIGH: |
Anna Bridge |
186:707f6e361f3e | 108 | ext_pu_mode = MXC_V_OWM_CFG_EXT_PULLUP_MODE_USED; |
Anna Bridge |
186:707f6e361f3e | 109 | ext_pu_polarity = MXC_V_OWM_CTRL_STAT_EXT_PULLUP_POL_ACT_HIGH; |
Anna Bridge |
186:707f6e361f3e | 110 | break; |
Anna Bridge |
186:707f6e361f3e | 111 | case OWM_EXT_PU_ACT_LOW: |
Anna Bridge |
186:707f6e361f3e | 112 | ext_pu_mode = MXC_V_OWM_CFG_EXT_PULLUP_MODE_USED; |
Anna Bridge |
186:707f6e361f3e | 113 | ext_pu_polarity = MXC_V_OWM_CTRL_STAT_EXT_PULLUP_POL_ACT_LOW; |
Anna Bridge |
186:707f6e361f3e | 114 | break; |
Anna Bridge |
186:707f6e361f3e | 115 | case OWM_EXT_PU_UNUSED: |
Anna Bridge |
186:707f6e361f3e | 116 | ext_pu_mode = MXC_V_OWM_CFG_EXT_PULLUP_MODE_UNUSED; |
Anna Bridge |
186:707f6e361f3e | 117 | ext_pu_polarity = MXC_V_OWM_CTRL_STAT_EXT_PULLUP_POL_ACT_HIGH; |
Anna Bridge |
186:707f6e361f3e | 118 | break; |
Anna Bridge |
186:707f6e361f3e | 119 | default: |
Anna Bridge |
186:707f6e361f3e | 120 | return E_BAD_PARAM; |
Anna Bridge |
186:707f6e361f3e | 121 | } |
Anna Bridge |
186:707f6e361f3e | 122 | |
Anna Bridge |
186:707f6e361f3e | 123 | // Set clk divisor |
Anna Bridge |
186:707f6e361f3e | 124 | owm->clk_div_1us = (clk_div << MXC_F_OWM_CLK_DIV_1US_DIVISOR_POS) & MXC_F_OWM_CLK_DIV_1US_DIVISOR; |
Anna Bridge |
186:707f6e361f3e | 125 | |
Anna Bridge |
186:707f6e361f3e | 126 | // Set configuration |
Anna Bridge |
186:707f6e361f3e | 127 | owm->cfg = (((cfg->int_pu_en << MXC_F_OWM_CFG_INT_PULLUP_ENABLE_POS) & MXC_F_OWM_CFG_INT_PULLUP_ENABLE) | |
Anna Bridge |
186:707f6e361f3e | 128 | ((ext_pu_mode << MXC_F_OWM_CFG_EXT_PULLUP_MODE_POS) & MXC_F_OWM_CFG_EXT_PULLUP_MODE) | |
Anna Bridge |
186:707f6e361f3e | 129 | ((cfg->long_line_mode << MXC_F_OWM_CFG_LONG_LINE_MODE) & MXC_F_OWM_CFG_LONG_LINE_MODE_POS)); |
Anna Bridge |
186:707f6e361f3e | 130 | |
Anna Bridge |
186:707f6e361f3e | 131 | owm->ctrl_stat = (((ext_pu_polarity << MXC_F_OWM_CTRL_STAT_EXT_PULLUP_POL_POS) & MXC_F_OWM_CTRL_STAT_EXT_PULLUP_POL) | |
Anna Bridge |
186:707f6e361f3e | 132 | ((cfg->overdrive_spec << MXC_F_OWM_CTRL_STAT_OD_SPEC_MODE_POS) & MXC_F_OWM_CTRL_STAT_OD_SPEC_MODE)); |
Anna Bridge |
186:707f6e361f3e | 133 | |
Anna Bridge |
186:707f6e361f3e | 134 | // Clear all interrupt flags |
Anna Bridge |
186:707f6e361f3e | 135 | owm->intfl = owm->intfl; |
Anna Bridge |
186:707f6e361f3e | 136 | |
Anna Bridge |
186:707f6e361f3e | 137 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 138 | } |
Anna Bridge |
186:707f6e361f3e | 139 | |
Anna Bridge |
186:707f6e361f3e | 140 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 141 | int OWM_Shutdown(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 142 | { |
Anna Bridge |
186:707f6e361f3e | 143 | int err; |
Anna Bridge |
186:707f6e361f3e | 144 | |
Anna Bridge |
186:707f6e361f3e | 145 | // Disable and clear interrupts |
Anna Bridge |
186:707f6e361f3e | 146 | owm->inten = 0; |
Anna Bridge |
186:707f6e361f3e | 147 | owm->intfl = owm->intfl; |
Anna Bridge |
186:707f6e361f3e | 148 | |
Anna Bridge |
186:707f6e361f3e | 149 | // Release IO pins and disable clk |
Anna Bridge |
186:707f6e361f3e | 150 | if ((err = SYS_OWM_Shutdown(owm)) != E_NO_ERROR) { |
Anna Bridge |
186:707f6e361f3e | 151 | return err; |
Anna Bridge |
186:707f6e361f3e | 152 | } |
Anna Bridge |
186:707f6e361f3e | 153 | |
Anna Bridge |
186:707f6e361f3e | 154 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 155 | } |
Anna Bridge |
186:707f6e361f3e | 156 | |
Anna Bridge |
186:707f6e361f3e | 157 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 158 | int OWM_Reset(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 159 | { |
Anna Bridge |
186:707f6e361f3e | 160 | owm->intfl = MXC_F_OWM_INTFL_OW_RESET_DONE; // Clear the reset flag |
Anna Bridge |
186:707f6e361f3e | 161 | owm->ctrl_stat |= MXC_F_OWM_CTRL_STAT_START_OW_RESET; // Generate a reset pulse |
Anna Bridge |
186:707f6e361f3e | 162 | while((owm->intfl & MXC_F_OWM_INTFL_OW_RESET_DONE) == 0); // Wait for reset time slot to complete |
Anna Bridge |
186:707f6e361f3e | 163 | |
Anna Bridge |
186:707f6e361f3e | 164 | return (!!(owm->ctrl_stat & MXC_F_OWM_CTRL_STAT_PRESENCE_DETECT)); // Return presence pulse detect status |
Anna Bridge |
186:707f6e361f3e | 165 | } |
Anna Bridge |
186:707f6e361f3e | 166 | |
Anna Bridge |
186:707f6e361f3e | 167 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 168 | int OWM_TouchByte(mxc_owm_regs_t *owm, uint8_t data) |
Anna Bridge |
186:707f6e361f3e | 169 | { |
Anna Bridge |
186:707f6e361f3e | 170 | owm->cfg &= ~MXC_F_OWM_CFG_SINGLE_BIT_MODE; // Set to 8 bit mode |
Anna Bridge |
186:707f6e361f3e | 171 | owm->intfl = (MXC_F_OWM_INTFL_TX_DATA_EMPTY | MXC_F_OWM_INTFL_RX_DATA_READY); // Clear the flags |
Anna Bridge |
186:707f6e361f3e | 172 | owm->data = (data << MXC_F_OWM_DATA_TX_RX_POS) & MXC_F_OWM_DATA_TX_RX; // Write data |
Anna Bridge |
186:707f6e361f3e | 173 | while((owm->intfl & MXC_F_OWM_INTFL_TX_DATA_EMPTY) == 0); // Wait for data to be sent |
Anna Bridge |
186:707f6e361f3e | 174 | while((owm->intfl & MXC_F_OWM_INTFL_RX_DATA_READY) == 0); // Wait for data to be read |
Anna Bridge |
186:707f6e361f3e | 175 | |
Anna Bridge |
186:707f6e361f3e | 176 | return (owm->data >> MXC_F_OWM_DATA_TX_RX_POS) & 0xFF; // Return the data read |
Anna Bridge |
186:707f6e361f3e | 177 | } |
Anna Bridge |
186:707f6e361f3e | 178 | |
Anna Bridge |
186:707f6e361f3e | 179 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 180 | int OWM_WriteByte(mxc_owm_regs_t *owm, uint8_t data) |
Anna Bridge |
186:707f6e361f3e | 181 | { |
Anna Bridge |
186:707f6e361f3e | 182 | // Send one byte of data and verify the data sent = data parameter |
Anna Bridge |
186:707f6e361f3e | 183 | return (OWM_TouchByte(owm, data) == data) ? E_NO_ERROR : E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 184 | } |
Anna Bridge |
186:707f6e361f3e | 185 | |
Anna Bridge |
186:707f6e361f3e | 186 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 187 | int OWM_ReadByte(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 188 | { |
Anna Bridge |
186:707f6e361f3e | 189 | // Read one byte of data |
Anna Bridge |
186:707f6e361f3e | 190 | return OWM_TouchByte(owm, 0xFF); |
Anna Bridge |
186:707f6e361f3e | 191 | } |
Anna Bridge |
186:707f6e361f3e | 192 | |
Anna Bridge |
186:707f6e361f3e | 193 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 194 | int OWM_TouchBit(mxc_owm_regs_t *owm, uint8_t bit) |
Anna Bridge |
186:707f6e361f3e | 195 | { |
Anna Bridge |
186:707f6e361f3e | 196 | MXC_OWM->cfg |= MXC_F_OWM_CFG_SINGLE_BIT_MODE; // Set to 1 bit mode |
Anna Bridge |
186:707f6e361f3e | 197 | owm->intfl = (MXC_F_OWM_INTFL_TX_DATA_EMPTY | MXC_F_OWM_INTFL_RX_DATA_READY); // Clear the flags |
Anna Bridge |
186:707f6e361f3e | 198 | owm->data = (bit << MXC_F_OWM_DATA_TX_RX_POS) & MXC_F_OWM_DATA_TX_RX; // Write data |
Anna Bridge |
186:707f6e361f3e | 199 | while((owm->intfl & MXC_F_OWM_INTFL_TX_DATA_EMPTY) == 0); // Wait for data to be sent |
Anna Bridge |
186:707f6e361f3e | 200 | while((owm->intfl & MXC_F_OWM_INTFL_RX_DATA_READY) == 0); // Wait for data to be read |
Anna Bridge |
186:707f6e361f3e | 201 | |
Anna Bridge |
186:707f6e361f3e | 202 | return (owm->data >> MXC_F_OWM_DATA_TX_RX_POS) & 0x1; // Return the bit read |
Anna Bridge |
186:707f6e361f3e | 203 | } |
Anna Bridge |
186:707f6e361f3e | 204 | |
Anna Bridge |
186:707f6e361f3e | 205 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 206 | int OWM_WriteBit(mxc_owm_regs_t *owm, uint8_t bit) |
Anna Bridge |
186:707f6e361f3e | 207 | { |
Anna Bridge |
186:707f6e361f3e | 208 | // Send a bit and verify the bit sent = bit parameter |
Anna Bridge |
186:707f6e361f3e | 209 | return (OWM_TouchBit(owm, bit) == bit) ? E_NO_ERROR : E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 210 | } |
Anna Bridge |
186:707f6e361f3e | 211 | |
Anna Bridge |
186:707f6e361f3e | 212 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 213 | int OWM_ReadBit(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 214 | { |
Anna Bridge |
186:707f6e361f3e | 215 | // Read a bit |
Anna Bridge |
186:707f6e361f3e | 216 | return OWM_TouchBit(owm, 1); |
Anna Bridge |
186:707f6e361f3e | 217 | } |
Anna Bridge |
186:707f6e361f3e | 218 | |
Anna Bridge |
186:707f6e361f3e | 219 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 220 | int OWM_Write(mxc_owm_regs_t *owm, uint8_t* data, int len) |
Anna Bridge |
186:707f6e361f3e | 221 | { |
Anna Bridge |
186:707f6e361f3e | 222 | int num = 0; |
Anna Bridge |
186:707f6e361f3e | 223 | |
Anna Bridge |
186:707f6e361f3e | 224 | owm->cfg &= ~MXC_F_OWM_CFG_SINGLE_BIT_MODE; // Set to 8 bit mode |
Anna Bridge |
186:707f6e361f3e | 225 | |
Anna Bridge |
186:707f6e361f3e | 226 | while(num < len) // Loop for number of bytes to write |
Anna Bridge |
186:707f6e361f3e | 227 | { |
Anna Bridge |
186:707f6e361f3e | 228 | owm->intfl = (MXC_F_OWM_INTFL_TX_DATA_EMPTY | MXC_F_OWM_INTFL_RX_DATA_READY | MXC_F_OWM_INTEN_LINE_SHORT); // Clear the flags |
Anna Bridge |
186:707f6e361f3e | 229 | owm->data = (data[num] << MXC_F_OWM_DATA_TX_RX_POS) & MXC_F_OWM_DATA_TX_RX; // Write data |
Anna Bridge |
186:707f6e361f3e | 230 | while((owm->intfl & MXC_F_OWM_INTFL_TX_DATA_EMPTY) == 0); // Wait for data to be sent |
Anna Bridge |
186:707f6e361f3e | 231 | while((owm->intfl & MXC_F_OWM_INTFL_RX_DATA_READY) == 0); // Wait for data to be read |
Anna Bridge |
186:707f6e361f3e | 232 | |
Anna Bridge |
186:707f6e361f3e | 233 | // Verify data sent is correct |
Anna Bridge |
186:707f6e361f3e | 234 | if(owm->data != data[num]) { |
Anna Bridge |
186:707f6e361f3e | 235 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 236 | } |
Anna Bridge |
186:707f6e361f3e | 237 | |
Anna Bridge |
186:707f6e361f3e | 238 | // Check error flag |
Anna Bridge |
186:707f6e361f3e | 239 | if(owm->intfl & MXC_F_OWM_INTEN_LINE_SHORT) { |
Anna Bridge |
186:707f6e361f3e | 240 | return E_COMM_ERR; // Wire was low before transaction |
Anna Bridge |
186:707f6e361f3e | 241 | } |
Anna Bridge |
186:707f6e361f3e | 242 | |
Anna Bridge |
186:707f6e361f3e | 243 | num++; // Keep track of how many bytes written |
Anna Bridge |
186:707f6e361f3e | 244 | } |
Anna Bridge |
186:707f6e361f3e | 245 | |
Anna Bridge |
186:707f6e361f3e | 246 | return num; // Return number of bytes written |
Anna Bridge |
186:707f6e361f3e | 247 | } |
Anna Bridge |
186:707f6e361f3e | 248 | |
Anna Bridge |
186:707f6e361f3e | 249 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 250 | int OWM_Read(mxc_owm_regs_t *owm, uint8_t* data, int len) |
Anna Bridge |
186:707f6e361f3e | 251 | { |
Anna Bridge |
186:707f6e361f3e | 252 | int num = 0; |
Anna Bridge |
186:707f6e361f3e | 253 | |
Anna Bridge |
186:707f6e361f3e | 254 | owm->cfg &= ~MXC_F_OWM_CFG_SINGLE_BIT_MODE; // Set to 8 bit mode |
Anna Bridge |
186:707f6e361f3e | 255 | |
Anna Bridge |
186:707f6e361f3e | 256 | while(num < len) // Loop for number of bytes to read |
Anna Bridge |
186:707f6e361f3e | 257 | { |
Anna Bridge |
186:707f6e361f3e | 258 | owm->intfl = (MXC_F_OWM_INTFL_TX_DATA_EMPTY | MXC_F_OWM_INTFL_RX_DATA_READY | MXC_F_OWM_INTEN_LINE_SHORT); // Clear the flags |
Anna Bridge |
186:707f6e361f3e | 259 | owm->data = 0xFF; // Write 0xFF for a read |
Anna Bridge |
186:707f6e361f3e | 260 | while((owm->intfl & MXC_F_OWM_INTFL_TX_DATA_EMPTY) == 0); // Wait for data to be sent |
Anna Bridge |
186:707f6e361f3e | 261 | while((owm->intfl & MXC_F_OWM_INTFL_RX_DATA_READY) == 0); // Wait for data to be read |
Anna Bridge |
186:707f6e361f3e | 262 | |
Anna Bridge |
186:707f6e361f3e | 263 | // Check error flag |
Anna Bridge |
186:707f6e361f3e | 264 | if(owm->intfl & MXC_F_OWM_INTEN_LINE_SHORT) { |
Anna Bridge |
186:707f6e361f3e | 265 | return E_COMM_ERR; // Wire was low before transaction |
Anna Bridge |
186:707f6e361f3e | 266 | } |
Anna Bridge |
186:707f6e361f3e | 267 | |
Anna Bridge |
186:707f6e361f3e | 268 | // Store read data into buffer |
Anna Bridge |
186:707f6e361f3e | 269 | data[num] = (owm->data >> MXC_F_OWM_DATA_TX_RX_POS) & MXC_F_OWM_DATA_TX_RX; |
Anna Bridge |
186:707f6e361f3e | 270 | |
Anna Bridge |
186:707f6e361f3e | 271 | num++; // Keep track of how many bytes read |
Anna Bridge |
186:707f6e361f3e | 272 | } |
Anna Bridge |
186:707f6e361f3e | 273 | |
Anna Bridge |
186:707f6e361f3e | 274 | return num; // Return number of bytes read |
Anna Bridge |
186:707f6e361f3e | 275 | } |
Anna Bridge |
186:707f6e361f3e | 276 | |
Anna Bridge |
186:707f6e361f3e | 277 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 278 | int OWM_ReadROM(mxc_owm_regs_t *owm, uint8_t* ROMCode) |
Anna Bridge |
186:707f6e361f3e | 279 | { |
Anna Bridge |
186:707f6e361f3e | 280 | int num_read = 0; |
Anna Bridge |
186:707f6e361f3e | 281 | |
Anna Bridge |
186:707f6e361f3e | 282 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 283 | if(OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 284 | { |
Anna Bridge |
186:707f6e361f3e | 285 | // Send Read ROM command code |
Anna Bridge |
186:707f6e361f3e | 286 | if(OWM_WriteByte(owm, READ_ROM_COMMAND) == E_NO_ERROR) |
Anna Bridge |
186:707f6e361f3e | 287 | { |
Anna Bridge |
186:707f6e361f3e | 288 | // Read 8 bytes and store in buffer |
Anna Bridge |
186:707f6e361f3e | 289 | num_read = OWM_Read(owm, ROMCode, 8); |
Anna Bridge |
186:707f6e361f3e | 290 | |
Anna Bridge |
186:707f6e361f3e | 291 | // Check the number of bytes read |
Anna Bridge |
186:707f6e361f3e | 292 | if(num_read != 8) { |
Anna Bridge |
186:707f6e361f3e | 293 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 294 | } |
Anna Bridge |
186:707f6e361f3e | 295 | } |
Anna Bridge |
186:707f6e361f3e | 296 | else |
Anna Bridge |
186:707f6e361f3e | 297 | { |
Anna Bridge |
186:707f6e361f3e | 298 | // Write failed |
Anna Bridge |
186:707f6e361f3e | 299 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 300 | } |
Anna Bridge |
186:707f6e361f3e | 301 | } |
Anna Bridge |
186:707f6e361f3e | 302 | else |
Anna Bridge |
186:707f6e361f3e | 303 | { |
Anna Bridge |
186:707f6e361f3e | 304 | // No presence pulse |
Anna Bridge |
186:707f6e361f3e | 305 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 306 | } |
Anna Bridge |
186:707f6e361f3e | 307 | |
Anna Bridge |
186:707f6e361f3e | 308 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 309 | } |
Anna Bridge |
186:707f6e361f3e | 310 | |
Anna Bridge |
186:707f6e361f3e | 311 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 312 | int OWM_MatchROM(mxc_owm_regs_t *owm, uint8_t* ROMCode) |
Anna Bridge |
186:707f6e361f3e | 313 | { |
Anna Bridge |
186:707f6e361f3e | 314 | int num_wrote = 0; |
Anna Bridge |
186:707f6e361f3e | 315 | |
Anna Bridge |
186:707f6e361f3e | 316 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 317 | if(OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 318 | { |
Anna Bridge |
186:707f6e361f3e | 319 | // Send match ROM command code |
Anna Bridge |
186:707f6e361f3e | 320 | if(OWM_WriteByte(owm, MATCH_ROM_COMMAND) == E_NO_ERROR) |
Anna Bridge |
186:707f6e361f3e | 321 | { |
Anna Bridge |
186:707f6e361f3e | 322 | // Write 8 bytes in ROMCode buffer |
Anna Bridge |
186:707f6e361f3e | 323 | num_wrote = OWM_Write(owm, ROMCode, 8); |
Anna Bridge |
186:707f6e361f3e | 324 | |
Anna Bridge |
186:707f6e361f3e | 325 | // Check the number of bytes written |
Anna Bridge |
186:707f6e361f3e | 326 | if(num_wrote != 8) { |
Anna Bridge |
186:707f6e361f3e | 327 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 328 | } |
Anna Bridge |
186:707f6e361f3e | 329 | } |
Anna Bridge |
186:707f6e361f3e | 330 | else |
Anna Bridge |
186:707f6e361f3e | 331 | { |
Anna Bridge |
186:707f6e361f3e | 332 | // Write failed |
Anna Bridge |
186:707f6e361f3e | 333 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 334 | } |
Anna Bridge |
186:707f6e361f3e | 335 | } |
Anna Bridge |
186:707f6e361f3e | 336 | else |
Anna Bridge |
186:707f6e361f3e | 337 | { |
Anna Bridge |
186:707f6e361f3e | 338 | // No presence pulse |
Anna Bridge |
186:707f6e361f3e | 339 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 340 | } |
Anna Bridge |
186:707f6e361f3e | 341 | |
Anna Bridge |
186:707f6e361f3e | 342 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 343 | } |
Anna Bridge |
186:707f6e361f3e | 344 | |
Anna Bridge |
186:707f6e361f3e | 345 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 346 | int OWM_ODMatchROM(mxc_owm_regs_t *owm, uint8_t* ROMCode) |
Anna Bridge |
186:707f6e361f3e | 347 | { |
Anna Bridge |
186:707f6e361f3e | 348 | int num_wrote = 0; |
Anna Bridge |
186:707f6e361f3e | 349 | |
Anna Bridge |
186:707f6e361f3e | 350 | // Set to standard speed |
Anna Bridge |
186:707f6e361f3e | 351 | owm->cfg &= ~(MXC_F_OWM_CFG_OVERDRIVE); |
Anna Bridge |
186:707f6e361f3e | 352 | |
Anna Bridge |
186:707f6e361f3e | 353 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 354 | if(OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 355 | { |
Anna Bridge |
186:707f6e361f3e | 356 | // Send Overdrive match ROM command code |
Anna Bridge |
186:707f6e361f3e | 357 | if(OWM_WriteByte(owm, OD_MATCH_ROM_COMMAND) == E_NO_ERROR) |
Anna Bridge |
186:707f6e361f3e | 358 | { |
Anna Bridge |
186:707f6e361f3e | 359 | // Set overdrive |
Anna Bridge |
186:707f6e361f3e | 360 | owm->cfg |= MXC_F_OWM_CFG_OVERDRIVE; |
Anna Bridge |
186:707f6e361f3e | 361 | |
Anna Bridge |
186:707f6e361f3e | 362 | // Write 8 bytes in ROMCode buffer |
Anna Bridge |
186:707f6e361f3e | 363 | num_wrote = OWM_Write(owm, ROMCode, 8); |
Anna Bridge |
186:707f6e361f3e | 364 | |
Anna Bridge |
186:707f6e361f3e | 365 | // Check the number of bytes written |
Anna Bridge |
186:707f6e361f3e | 366 | if(num_wrote != 8) { |
Anna Bridge |
186:707f6e361f3e | 367 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 368 | } |
Anna Bridge |
186:707f6e361f3e | 369 | } |
Anna Bridge |
186:707f6e361f3e | 370 | else |
Anna Bridge |
186:707f6e361f3e | 371 | { |
Anna Bridge |
186:707f6e361f3e | 372 | // Write failed |
Anna Bridge |
186:707f6e361f3e | 373 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 374 | } |
Anna Bridge |
186:707f6e361f3e | 375 | } |
Anna Bridge |
186:707f6e361f3e | 376 | else |
Anna Bridge |
186:707f6e361f3e | 377 | { |
Anna Bridge |
186:707f6e361f3e | 378 | // No presence pulse |
Anna Bridge |
186:707f6e361f3e | 379 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 380 | } |
Anna Bridge |
186:707f6e361f3e | 381 | |
Anna Bridge |
186:707f6e361f3e | 382 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 383 | } |
Anna Bridge |
186:707f6e361f3e | 384 | |
Anna Bridge |
186:707f6e361f3e | 385 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 386 | int OWM_SkipROM(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 387 | { |
Anna Bridge |
186:707f6e361f3e | 388 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 389 | if(OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 390 | { |
Anna Bridge |
186:707f6e361f3e | 391 | // Send skip ROM command code |
Anna Bridge |
186:707f6e361f3e | 392 | return OWM_WriteByte(owm, SKIP_ROM_COMMAND); |
Anna Bridge |
186:707f6e361f3e | 393 | } |
Anna Bridge |
186:707f6e361f3e | 394 | else |
Anna Bridge |
186:707f6e361f3e | 395 | { |
Anna Bridge |
186:707f6e361f3e | 396 | // No presence pulse |
Anna Bridge |
186:707f6e361f3e | 397 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 398 | } |
Anna Bridge |
186:707f6e361f3e | 399 | } |
Anna Bridge |
186:707f6e361f3e | 400 | |
Anna Bridge |
186:707f6e361f3e | 401 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 402 | int OWM_ODSkipROM(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 403 | { |
Anna Bridge |
186:707f6e361f3e | 404 | // Set to standard speed |
Anna Bridge |
186:707f6e361f3e | 405 | owm->cfg &= ~(MXC_F_OWM_CFG_OVERDRIVE); |
Anna Bridge |
186:707f6e361f3e | 406 | |
Anna Bridge |
186:707f6e361f3e | 407 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 408 | if(OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 409 | { |
Anna Bridge |
186:707f6e361f3e | 410 | // Send Overdrive skip ROM command code |
Anna Bridge |
186:707f6e361f3e | 411 | if(OWM_WriteByte(owm, OD_SKIP_ROM_COMMAND) == E_NO_ERROR) |
Anna Bridge |
186:707f6e361f3e | 412 | { |
Anna Bridge |
186:707f6e361f3e | 413 | // Set overdrive speed |
Anna Bridge |
186:707f6e361f3e | 414 | owm->cfg |= MXC_F_OWM_CFG_OVERDRIVE; |
Anna Bridge |
186:707f6e361f3e | 415 | |
Anna Bridge |
186:707f6e361f3e | 416 | return E_NO_ERROR; |
Anna Bridge |
186:707f6e361f3e | 417 | } |
Anna Bridge |
186:707f6e361f3e | 418 | else |
Anna Bridge |
186:707f6e361f3e | 419 | { |
Anna Bridge |
186:707f6e361f3e | 420 | // Write failed |
Anna Bridge |
186:707f6e361f3e | 421 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 422 | } |
Anna Bridge |
186:707f6e361f3e | 423 | } |
Anna Bridge |
186:707f6e361f3e | 424 | else |
Anna Bridge |
186:707f6e361f3e | 425 | { |
Anna Bridge |
186:707f6e361f3e | 426 | // No presence pulse |
Anna Bridge |
186:707f6e361f3e | 427 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 428 | } |
Anna Bridge |
186:707f6e361f3e | 429 | } |
Anna Bridge |
186:707f6e361f3e | 430 | |
Anna Bridge |
186:707f6e361f3e | 431 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 432 | int OWM_Resume(mxc_owm_regs_t *owm) |
Anna Bridge |
186:707f6e361f3e | 433 | { |
Anna Bridge |
186:707f6e361f3e | 434 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 435 | if(OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 436 | { |
Anna Bridge |
186:707f6e361f3e | 437 | // Send resume command code |
Anna Bridge |
186:707f6e361f3e | 438 | return OWM_WriteByte(owm, RESUME_COMMAND); |
Anna Bridge |
186:707f6e361f3e | 439 | } |
Anna Bridge |
186:707f6e361f3e | 440 | else |
Anna Bridge |
186:707f6e361f3e | 441 | { |
Anna Bridge |
186:707f6e361f3e | 442 | // No presence pulse |
Anna Bridge |
186:707f6e361f3e | 443 | return E_COMM_ERR; |
Anna Bridge |
186:707f6e361f3e | 444 | } |
Anna Bridge |
186:707f6e361f3e | 445 | } |
Anna Bridge |
186:707f6e361f3e | 446 | |
Anna Bridge |
186:707f6e361f3e | 447 | /* ************************************************************************* */ |
Anna Bridge |
186:707f6e361f3e | 448 | int OWM_SearchROM(mxc_owm_regs_t *owm, int newSearch, uint8_t* ROMCode) |
Anna Bridge |
186:707f6e361f3e | 449 | { |
Anna Bridge |
186:707f6e361f3e | 450 | int nibble_start_bit = 1; |
Anna Bridge |
186:707f6e361f3e | 451 | int rom_byte_number = 0; |
Anna Bridge |
186:707f6e361f3e | 452 | uint8_t rom_nibble_mask = 0x0F; |
Anna Bridge |
186:707f6e361f3e | 453 | uint8_t search_direction = 0; |
Anna Bridge |
186:707f6e361f3e | 454 | int readValue = 0; |
Anna Bridge |
186:707f6e361f3e | 455 | int sentBits = 0; |
Anna Bridge |
186:707f6e361f3e | 456 | int discrepancy = 0; |
Anna Bridge |
186:707f6e361f3e | 457 | int bit_position = 0; |
Anna Bridge |
186:707f6e361f3e | 458 | int discrepancy_mask = 0; |
Anna Bridge |
186:707f6e361f3e | 459 | int last_zero = 0; |
Anna Bridge |
186:707f6e361f3e | 460 | uint8_t crc8 = 0; |
Anna Bridge |
186:707f6e361f3e | 461 | int search_result = 0; |
Anna Bridge |
186:707f6e361f3e | 462 | |
Anna Bridge |
186:707f6e361f3e | 463 | // Clear ROM array |
Anna Bridge |
186:707f6e361f3e | 464 | memset(ROMCode, 0x0, 8); |
Anna Bridge |
186:707f6e361f3e | 465 | |
Anna Bridge |
186:707f6e361f3e | 466 | if(newSearch) |
Anna Bridge |
186:707f6e361f3e | 467 | { |
Anna Bridge |
186:707f6e361f3e | 468 | // Reset all global variables to start search from begining |
Anna Bridge |
186:707f6e361f3e | 469 | LastDiscrepancy = 0; |
Anna Bridge |
186:707f6e361f3e | 470 | LastDeviceFlag = 0; |
Anna Bridge |
186:707f6e361f3e | 471 | } |
Anna Bridge |
186:707f6e361f3e | 472 | |
Anna Bridge |
186:707f6e361f3e | 473 | // Check if the last call was the last device |
Anna Bridge |
186:707f6e361f3e | 474 | if(LastDeviceFlag) |
Anna Bridge |
186:707f6e361f3e | 475 | { |
Anna Bridge |
186:707f6e361f3e | 476 | // Reset the search |
Anna Bridge |
186:707f6e361f3e | 477 | LastDiscrepancy = 0; |
Anna Bridge |
186:707f6e361f3e | 478 | LastDeviceFlag = 0; |
Anna Bridge |
186:707f6e361f3e | 479 | return 0; |
Anna Bridge |
186:707f6e361f3e | 480 | } |
Anna Bridge |
186:707f6e361f3e | 481 | |
Anna Bridge |
186:707f6e361f3e | 482 | // Send reset and wait for presence pulse |
Anna Bridge |
186:707f6e361f3e | 483 | if (OWM_Reset(owm)) |
Anna Bridge |
186:707f6e361f3e | 484 | { |
Anna Bridge |
186:707f6e361f3e | 485 | // Send the search command |
Anna Bridge |
186:707f6e361f3e | 486 | OWM_WriteByte(owm, SEARCH_ROM_COMMAND); |
Anna Bridge |
186:707f6e361f3e | 487 | |
Anna Bridge |
186:707f6e361f3e | 488 | // Set search ROM accelerator bit |
Anna Bridge |
186:707f6e361f3e | 489 | owm->ctrl_stat |= MXC_F_OWM_CTRL_STAT_SRA_MODE; |
Anna Bridge |
186:707f6e361f3e | 490 | |
Anna Bridge |
186:707f6e361f3e | 491 | // Loop until through all ROM bytes 0-7 (this loops 2 times per byte) |
Anna Bridge |
186:707f6e361f3e | 492 | while(rom_byte_number < 8) |
Anna Bridge |
186:707f6e361f3e | 493 | { |
Anna Bridge |
186:707f6e361f3e | 494 | // Each loop finds the discrepancy bits and finds 4 bits (nibble) of the ROM |
Anna Bridge |
186:707f6e361f3e | 495 | |
Anna Bridge |
186:707f6e361f3e | 496 | // Set the search direction the same as last time for the nibble masked |
Anna Bridge |
186:707f6e361f3e | 497 | search_direction = ROMCode[rom_byte_number] & rom_nibble_mask; |
Anna Bridge |
186:707f6e361f3e | 498 | |
Anna Bridge |
186:707f6e361f3e | 499 | // If the upper nibble is the mask then shift bits to lower nibble |
Anna Bridge |
186:707f6e361f3e | 500 | if(rom_nibble_mask > 0x0F) { |
Anna Bridge |
186:707f6e361f3e | 501 | search_direction = search_direction >> 4; |
Anna Bridge |
186:707f6e361f3e | 502 | } |
Anna Bridge |
186:707f6e361f3e | 503 | |
Anna Bridge |
186:707f6e361f3e | 504 | // Get the last discrepancy bit position relative to the nibble start bit |
Anna Bridge |
186:707f6e361f3e | 505 | bit_position = LastDiscrepancy - nibble_start_bit; |
Anna Bridge |
186:707f6e361f3e | 506 | |
Anna Bridge |
186:707f6e361f3e | 507 | // Check if last discrepancy is witin this nibble |
Anna Bridge |
186:707f6e361f3e | 508 | if( (bit_position >= 0) && (bit_position < 4) ) |
Anna Bridge |
186:707f6e361f3e | 509 | { |
Anna Bridge |
186:707f6e361f3e | 510 | // Last discrepancy is within this nibble |
Anna Bridge |
186:707f6e361f3e | 511 | // Set the bit of the last discrepancy bit |
Anna Bridge |
186:707f6e361f3e | 512 | search_direction |= (1 << (bit_position)); |
Anna Bridge |
186:707f6e361f3e | 513 | } |
Anna Bridge |
186:707f6e361f3e | 514 | |
Anna Bridge |
186:707f6e361f3e | 515 | // Performs two read bits and a write bit for 4 bits of the ROM |
Anna Bridge |
186:707f6e361f3e | 516 | readValue = OWM_TouchByte(owm, search_direction); |
Anna Bridge |
186:707f6e361f3e | 517 | // Get discrepancy flags |
Anna Bridge |
186:707f6e361f3e | 518 | discrepancy = readValue & 0xF; |
Anna Bridge |
186:707f6e361f3e | 519 | // Get the 4 bits sent to select the ROM |
Anna Bridge |
186:707f6e361f3e | 520 | sentBits = (readValue >> 4) & 0xF; |
Anna Bridge |
186:707f6e361f3e | 521 | |
Anna Bridge |
186:707f6e361f3e | 522 | // Store the bit location of the MSB discrepancy with sentbit = 0 |
Anna Bridge |
186:707f6e361f3e | 523 | if(discrepancy) |
Anna Bridge |
186:707f6e361f3e | 524 | { |
Anna Bridge |
186:707f6e361f3e | 525 | // Initialize bit_position to MSB of nibble |
Anna Bridge |
186:707f6e361f3e | 526 | bit_position = 3; |
Anna Bridge |
186:707f6e361f3e | 527 | |
Anna Bridge |
186:707f6e361f3e | 528 | while(bit_position >= 0) |
Anna Bridge |
186:707f6e361f3e | 529 | { |
Anna Bridge |
186:707f6e361f3e | 530 | // Get discrepancy flag of the current bit position |
Anna Bridge |
186:707f6e361f3e | 531 | discrepancy_mask = discrepancy & (1 << bit_position); |
Anna Bridge |
186:707f6e361f3e | 532 | |
Anna Bridge |
186:707f6e361f3e | 533 | // If there is a discrepancy and the sent bit is 0 save this bit position |
Anna Bridge |
186:707f6e361f3e | 534 | if( (discrepancy_mask) && !(sentBits & discrepancy_mask)) |
Anna Bridge |
186:707f6e361f3e | 535 | { |
Anna Bridge |
186:707f6e361f3e | 536 | last_zero = nibble_start_bit + bit_position; |
Anna Bridge |
186:707f6e361f3e | 537 | break; |
Anna Bridge |
186:707f6e361f3e | 538 | } |
Anna Bridge |
186:707f6e361f3e | 539 | |
Anna Bridge |
186:707f6e361f3e | 540 | bit_position--; |
Anna Bridge |
186:707f6e361f3e | 541 | } |
Anna Bridge |
186:707f6e361f3e | 542 | } |
Anna Bridge |
186:707f6e361f3e | 543 | |
Anna Bridge |
186:707f6e361f3e | 544 | // Clear the nibble |
Anna Bridge |
186:707f6e361f3e | 545 | ROMCode[rom_byte_number] &= ~rom_nibble_mask; |
Anna Bridge |
186:707f6e361f3e | 546 | |
Anna Bridge |
186:707f6e361f3e | 547 | // Store the sentBits in the ROMCode |
Anna Bridge |
186:707f6e361f3e | 548 | if(rom_nibble_mask > 0x0F) { |
Anna Bridge |
186:707f6e361f3e | 549 | ROMCode[rom_byte_number] |= (sentBits << 4); |
Anna Bridge |
186:707f6e361f3e | 550 | } |
Anna Bridge |
186:707f6e361f3e | 551 | else { |
Anna Bridge |
186:707f6e361f3e | 552 | ROMCode[rom_byte_number] |= sentBits; |
Anna Bridge |
186:707f6e361f3e | 553 | } |
Anna Bridge |
186:707f6e361f3e | 554 | |
Anna Bridge |
186:707f6e361f3e | 555 | // Increment the nibble start bit and shift mask |
Anna Bridge |
186:707f6e361f3e | 556 | nibble_start_bit += 4; |
Anna Bridge |
186:707f6e361f3e | 557 | rom_nibble_mask <<= 4; |
Anna Bridge |
186:707f6e361f3e | 558 | |
Anna Bridge |
186:707f6e361f3e | 559 | // If the mask is 0 then go to new ROM byte rom_byte_number and reset mask |
Anna Bridge |
186:707f6e361f3e | 560 | if (rom_nibble_mask == 0) |
Anna Bridge |
186:707f6e361f3e | 561 | { |
Anna Bridge |
186:707f6e361f3e | 562 | rom_byte_number++; |
Anna Bridge |
186:707f6e361f3e | 563 | rom_nibble_mask = 0x0F; |
Anna Bridge |
186:707f6e361f3e | 564 | } |
Anna Bridge |
186:707f6e361f3e | 565 | |
Anna Bridge |
186:707f6e361f3e | 566 | } // End while(rom_byte_number < 8) |
Anna Bridge |
186:707f6e361f3e | 567 | |
Anna Bridge |
186:707f6e361f3e | 568 | // Clear search ROM accelerator |
Anna Bridge |
186:707f6e361f3e | 569 | owm->ctrl_stat &= ~(MXC_F_OWM_CTRL_STAT_SRA_MODE); |
Anna Bridge |
186:707f6e361f3e | 570 | |
Anna Bridge |
186:707f6e361f3e | 571 | // Calculate CRC to verify ROM code is correct |
Anna Bridge |
186:707f6e361f3e | 572 | crc8 = CalculateCRC8(ROMCode, 7); |
Anna Bridge |
186:707f6e361f3e | 573 | |
Anna Bridge |
186:707f6e361f3e | 574 | // If the search was successful then |
Anna Bridge |
186:707f6e361f3e | 575 | if ((nibble_start_bit >= 65) && (crc8 == ROMCode[7])) |
Anna Bridge |
186:707f6e361f3e | 576 | { |
Anna Bridge |
186:707f6e361f3e | 577 | // Search successful so set LastDiscrepancy,LastDeviceFlag,search_result |
Anna Bridge |
186:707f6e361f3e | 578 | LastDiscrepancy = last_zero; |
Anna Bridge |
186:707f6e361f3e | 579 | |
Anna Bridge |
186:707f6e361f3e | 580 | // Check for last device |
Anna Bridge |
186:707f6e361f3e | 581 | if (LastDiscrepancy == 0) { |
Anna Bridge |
186:707f6e361f3e | 582 | LastDeviceFlag = 1; |
Anna Bridge |
186:707f6e361f3e | 583 | } |
Anna Bridge |
186:707f6e361f3e | 584 | |
Anna Bridge |
186:707f6e361f3e | 585 | search_result = 1; |
Anna Bridge |
186:707f6e361f3e | 586 | } |
Anna Bridge |
186:707f6e361f3e | 587 | } // End if (OWM_Reset) |
Anna Bridge |
186:707f6e361f3e | 588 | |
Anna Bridge |
186:707f6e361f3e | 589 | // If no device found then reset counters so next 'search' will be like a first |
Anna Bridge |
186:707f6e361f3e | 590 | if (!search_result || !ROMCode[0]) |
Anna Bridge |
186:707f6e361f3e | 591 | { |
Anna Bridge |
186:707f6e361f3e | 592 | LastDiscrepancy = 0; |
Anna Bridge |
186:707f6e361f3e | 593 | LastDeviceFlag = 0; |
Anna Bridge |
186:707f6e361f3e | 594 | search_result = 0; |
Anna Bridge |
186:707f6e361f3e | 595 | } |
Anna Bridge |
186:707f6e361f3e | 596 | |
Anna Bridge |
186:707f6e361f3e | 597 | return search_result; |
Anna Bridge |
186:707f6e361f3e | 598 | } |
Anna Bridge |
186:707f6e361f3e | 599 | |
Anna Bridge |
186:707f6e361f3e | 600 | /* |
Anna Bridge |
186:707f6e361f3e | 601 | * Calcualate CRC8 of the buffer of data provided |
Anna Bridge |
186:707f6e361f3e | 602 | */ |
Anna Bridge |
186:707f6e361f3e | 603 | uint8_t CalculateCRC8(uint8_t* data, int len) |
Anna Bridge |
186:707f6e361f3e | 604 | { |
Anna Bridge |
186:707f6e361f3e | 605 | int i; |
Anna Bridge |
186:707f6e361f3e | 606 | uint8_t crc = 0; |
Anna Bridge |
186:707f6e361f3e | 607 | |
Anna Bridge |
186:707f6e361f3e | 608 | for(i = 0; i < len; i++) |
Anna Bridge |
186:707f6e361f3e | 609 | { |
Anna Bridge |
186:707f6e361f3e | 610 | crc = update_crc8(crc, data[i]); |
Anna Bridge |
186:707f6e361f3e | 611 | } |
Anna Bridge |
186:707f6e361f3e | 612 | |
Anna Bridge |
186:707f6e361f3e | 613 | return crc; |
Anna Bridge |
186:707f6e361f3e | 614 | } |
Anna Bridge |
186:707f6e361f3e | 615 | |
Anna Bridge |
186:707f6e361f3e | 616 | /* |
Anna Bridge |
186:707f6e361f3e | 617 | * Calculate the CRC8 of the byte value provided with the current crc value |
Anna Bridge |
186:707f6e361f3e | 618 | * provided Returns updated crc value |
Anna Bridge |
186:707f6e361f3e | 619 | */ |
Anna Bridge |
186:707f6e361f3e | 620 | uint8_t update_crc8(uint8_t crc, uint8_t val) |
Anna Bridge |
186:707f6e361f3e | 621 | { |
Anna Bridge |
186:707f6e361f3e | 622 | uint8_t inc, tmp; |
Anna Bridge |
186:707f6e361f3e | 623 | |
Anna Bridge |
186:707f6e361f3e | 624 | for (inc = 0; inc < 8; inc++) |
Anna Bridge |
186:707f6e361f3e | 625 | { |
Anna Bridge |
186:707f6e361f3e | 626 | tmp = (uint8_t)(crc << 7); // Save X7 bit value |
Anna Bridge |
186:707f6e361f3e | 627 | crc >>= 1; // Shift crc |
Anna Bridge |
186:707f6e361f3e | 628 | if (((tmp >> 7) ^ (val & 0x01)) == 1) // If X7 xor X8 (input data) |
Anna Bridge |
186:707f6e361f3e | 629 | { |
Anna Bridge |
186:707f6e361f3e | 630 | crc ^= 0x8c; // XOR crc with X4 and X5, X1 = X7^X8 |
Anna Bridge |
186:707f6e361f3e | 631 | crc |= 0x80; // Carry |
Anna Bridge |
186:707f6e361f3e | 632 | } |
Anna Bridge |
186:707f6e361f3e | 633 | val >>= 1; |
Anna Bridge |
186:707f6e361f3e | 634 | } |
Anna Bridge |
186:707f6e361f3e | 635 | |
Anna Bridge |
186:707f6e361f3e | 636 | return crc; |
Anna Bridge |
186:707f6e361f3e | 637 | } |
Anna Bridge |
186:707f6e361f3e | 638 | |
Anna Bridge |
186:707f6e361f3e | 639 | /**@} end of group owm */ |