forked

Committer:
AnnaBridge
Date:
Wed Jun 21 17:46:44 2017 +0100
Revision:
167:e84263d55307
This updates the lib to the mbed lib v 145

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AnnaBridge 167:e84263d55307 1 /* mbed Microcontroller Library
AnnaBridge 167:e84263d55307 2 * Copyright (c) 2013-2016 Realtek Semiconductor Corp.
AnnaBridge 167:e84263d55307 3 *
AnnaBridge 167:e84263d55307 4 * Licensed under the Apache License, Version 2.0 (the "License");
AnnaBridge 167:e84263d55307 5 * you may not use this file except in compliance with the License.
AnnaBridge 167:e84263d55307 6 * You may obtain a copy of the License at
AnnaBridge 167:e84263d55307 7 *
AnnaBridge 167:e84263d55307 8 * http://www.apache.org/licenses/LICENSE-2.0
AnnaBridge 167:e84263d55307 9 *
AnnaBridge 167:e84263d55307 10 * Unless required by applicable law or agreed to in writing, software
AnnaBridge 167:e84263d55307 11 * distributed under the License is distributed on an "AS IS" BASIS,
AnnaBridge 167:e84263d55307 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AnnaBridge 167:e84263d55307 13 * See the License for the specific language governing permissions and
AnnaBridge 167:e84263d55307 14 * limitations under the License.
AnnaBridge 167:e84263d55307 15 */
AnnaBridge 167:e84263d55307 16
AnnaBridge 167:e84263d55307 17 #include "objects.h"
AnnaBridge 167:e84263d55307 18 #include "PinNames.h"
AnnaBridge 167:e84263d55307 19 #include "hal_i2c.h"
AnnaBridge 167:e84263d55307 20 #include "i2c_api.h"
AnnaBridge 167:e84263d55307 21
AnnaBridge 167:e84263d55307 22 #if CONFIG_I2C_EN
AnnaBridge 167:e84263d55307 23
AnnaBridge 167:e84263d55307 24 #include "pinmap.h"
AnnaBridge 167:e84263d55307 25
AnnaBridge 167:e84263d55307 26
AnnaBridge 167:e84263d55307 27 static const PinMap PinMap_I2C_SDA[] = {
AnnaBridge 167:e84263d55307 28 {PD_4, RTL_PIN_PERI(I2C0, 0, S0), RTL_PIN_FUNC(I2C0, S0)},
AnnaBridge 167:e84263d55307 29 {PH_1, RTL_PIN_PERI(I2C0, 0, S1), RTL_PIN_FUNC(I2C0, S1)},
AnnaBridge 167:e84263d55307 30 {PC_8, RTL_PIN_PERI(I2C0, 0, S2), RTL_PIN_FUNC(I2C0, S2)},
AnnaBridge 167:e84263d55307 31 {PE_7, RTL_PIN_PERI(I2C0, 0, S3), RTL_PIN_FUNC(I2C0, S3)},
AnnaBridge 167:e84263d55307 32
AnnaBridge 167:e84263d55307 33 {PC_4, RTL_PIN_PERI(I2C1, 1, S0), RTL_PIN_FUNC(I2C1, S0)},
AnnaBridge 167:e84263d55307 34 {PH_3, RTL_PIN_PERI(I2C1, 1, S1), RTL_PIN_FUNC(I2C1, S1)},
AnnaBridge 167:e84263d55307 35 {PD_7, RTL_PIN_PERI(I2C1, 1, S2), RTL_PIN_FUNC(I2C1, S2)},
AnnaBridge 167:e84263d55307 36
AnnaBridge 167:e84263d55307 37 {PB_7, RTL_PIN_PERI(I2C2, 2, S0), RTL_PIN_FUNC(I2C2, S0)},
AnnaBridge 167:e84263d55307 38 {PE_1, RTL_PIN_PERI(I2C2, 2, S1), RTL_PIN_FUNC(I2C2, S1)},
AnnaBridge 167:e84263d55307 39 {PC_7, RTL_PIN_PERI(I2C2, 2, S2), RTL_PIN_FUNC(I2C2, S2)},
AnnaBridge 167:e84263d55307 40
AnnaBridge 167:e84263d55307 41 {PB_3, RTL_PIN_PERI(I2C3, 3, S0), RTL_PIN_FUNC(I2C3, S0)},
AnnaBridge 167:e84263d55307 42 {PE_3, RTL_PIN_PERI(I2C3, 3, S1), RTL_PIN_FUNC(I2C3, S1)},
AnnaBridge 167:e84263d55307 43 {PE_5, RTL_PIN_PERI(I2C3, 3, S2), RTL_PIN_FUNC(I2C3, S2)},
AnnaBridge 167:e84263d55307 44 {PD_9, RTL_PIN_PERI(I2C3, 3, S3), RTL_PIN_FUNC(I2C3, S3)},
AnnaBridge 167:e84263d55307 45
AnnaBridge 167:e84263d55307 46 {NC, NC, 0}
AnnaBridge 167:e84263d55307 47 };
AnnaBridge 167:e84263d55307 48
AnnaBridge 167:e84263d55307 49 static const PinMap PinMap_I2C_SCL[] = {
AnnaBridge 167:e84263d55307 50 {PD_5, RTL_PIN_PERI(I2C0, 0, S0), RTL_PIN_FUNC(I2C0, S0)},
AnnaBridge 167:e84263d55307 51 {PH_0, RTL_PIN_PERI(I2C0, 0, S1), RTL_PIN_FUNC(I2C0, S1)},
AnnaBridge 167:e84263d55307 52 {PC_9, RTL_PIN_PERI(I2C0, 0, S2), RTL_PIN_FUNC(I2C0, S2)},
AnnaBridge 167:e84263d55307 53 {PE_6, RTL_PIN_PERI(I2C0, 0, S3), RTL_PIN_FUNC(I2C0, S3)},
AnnaBridge 167:e84263d55307 54
AnnaBridge 167:e84263d55307 55 {PC_5, RTL_PIN_PERI(I2C1, 1, S0), RTL_PIN_FUNC(I2C1, S0)},
AnnaBridge 167:e84263d55307 56 {PH_2, RTL_PIN_PERI(I2C1, 1, S1), RTL_PIN_FUNC(I2C1, S1)},
AnnaBridge 167:e84263d55307 57 {PD_6, RTL_PIN_PERI(I2C1, 1, S2), RTL_PIN_FUNC(I2C1, S2)},
AnnaBridge 167:e84263d55307 58
AnnaBridge 167:e84263d55307 59 {PB_6, RTL_PIN_PERI(I2C2, 2, S0), RTL_PIN_FUNC(I2C2, S0)},
AnnaBridge 167:e84263d55307 60 {PE_0, RTL_PIN_PERI(I2C2, 2, S1), RTL_PIN_FUNC(I2C2, S1)},
AnnaBridge 167:e84263d55307 61 {PC_6, RTL_PIN_PERI(I2C2, 2, S2), RTL_PIN_FUNC(I2C2, S2)},
AnnaBridge 167:e84263d55307 62
AnnaBridge 167:e84263d55307 63 {PB_2, RTL_PIN_PERI(I2C3, 3, S0), RTL_PIN_FUNC(I2C3, S0)},
AnnaBridge 167:e84263d55307 64 {PE_2, RTL_PIN_PERI(I2C3, 3, S1), RTL_PIN_FUNC(I2C3, S1)},
AnnaBridge 167:e84263d55307 65 {PE_4, RTL_PIN_PERI(I2C3, 3, S2), RTL_PIN_FUNC(I2C3, S2)},
AnnaBridge 167:e84263d55307 66 {PD_8, RTL_PIN_PERI(I2C3, 3, S3), RTL_PIN_FUNC(I2C3, S3)},
AnnaBridge 167:e84263d55307 67
AnnaBridge 167:e84263d55307 68 {NC, NC, 0}
AnnaBridge 167:e84263d55307 69 };
AnnaBridge 167:e84263d55307 70
AnnaBridge 167:e84263d55307 71 static int address_save_int[4];
AnnaBridge 167:e84263d55307 72 static int Byte_count[4];
AnnaBridge 167:e84263d55307 73 static u32 address_save[4];
AnnaBridge 167:e84263d55307 74 static uint16_t i2c_target_addr[4];
AnnaBridge 167:e84263d55307 75 static SAL_I2C_TRANSFER_BUF i2ctxtranbuf[4];
AnnaBridge 167:e84263d55307 76 static SAL_I2C_TRANSFER_BUF i2crxtranbuf[4];
AnnaBridge 167:e84263d55307 77 extern u32 ConfigDebugErr;
AnnaBridge 167:e84263d55307 78 extern u32 ConfigDebuginfo;
AnnaBridge 167:e84263d55307 79 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
AnnaBridge 167:e84263d55307 80 {
AnnaBridge 167:e84263d55307 81 uint32_t i2c_sel;
AnnaBridge 167:e84263d55307 82 uint32_t i2c_idx;
AnnaBridge 167:e84263d55307 83 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 84 PSAL_I2C_USERCB_ADPT pSalI2CUserCBAdpt = NULL;
AnnaBridge 167:e84263d55307 85 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 86
AnnaBridge 167:e84263d55307 87 // Determine the I2C to use
AnnaBridge 167:e84263d55307 88 uint32_t i2c_sda = (uint32_t)pinmap_peripheral(sda, PinMap_I2C_SDA);
AnnaBridge 167:e84263d55307 89 uint32_t i2c_scl = (uint32_t)pinmap_peripheral(scl, PinMap_I2C_SCL);
AnnaBridge 167:e84263d55307 90 ConfigDebugErr &= (~(_DBG_I2C_|_DBG_GDMA_));
AnnaBridge 167:e84263d55307 91 ConfigDebugInfo&= (~(_DBG_I2C_|_DBG_GDMA_));
AnnaBridge 167:e84263d55307 92 i2c_sel = (uint32_t)pinmap_merge(i2c_sda, i2c_scl);
AnnaBridge 167:e84263d55307 93 i2c_idx = RTL_GET_PERI_IDX(i2c_sel);
AnnaBridge 167:e84263d55307 94 if (unlikely(i2c_idx == NC)) {
AnnaBridge 167:e84263d55307 95 DBG_8195A("%s: Cannot find matched UART\n", __FUNCTION__);
AnnaBridge 167:e84263d55307 96 return;
AnnaBridge 167:e84263d55307 97 }
AnnaBridge 167:e84263d55307 98
AnnaBridge 167:e84263d55307 99 /* Get I2C device handler */
AnnaBridge 167:e84263d55307 100 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 101 pSalI2CUserCBAdpt = (PSAL_I2C_USERCB_ADPT)&(obj->SalI2CUserCBAdpt);
AnnaBridge 167:e84263d55307 102
AnnaBridge 167:e84263d55307 103
AnnaBridge 167:e84263d55307 104 /*To assign the rest pointers*/
AnnaBridge 167:e84263d55307 105 pSalI2CMngtAdpt->MstRDCmdCnt = 0;
AnnaBridge 167:e84263d55307 106 pSalI2CMngtAdpt->InnerTimeOut = 2000; // inner time-out count, 2000 ms
AnnaBridge 167:e84263d55307 107 pSalI2CMngtAdpt->pSalHndPriv = &(obj->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 108 pSalI2CMngtAdpt->pSalHndPriv->ppSalI2CHnd = (void**)&(pSalI2CMngtAdpt->pSalHndPriv);
AnnaBridge 167:e84263d55307 109
AnnaBridge 167:e84263d55307 110 /* To assign the default (ROM) HAL OP initialization function */
AnnaBridge 167:e84263d55307 111 #if defined(CONFIG_CHIP_A_CUT) || defined(CONFIG_CHIP_B_CUT) || defined(CONFIG_CHIP_C_CUT)
AnnaBridge 167:e84263d55307 112 pSalI2CMngtAdpt->pHalOpInit = HalI2COpInit_Patch;
AnnaBridge 167:e84263d55307 113 #elif defined(CONFIG_CHIP_E_CUT)
AnnaBridge 167:e84263d55307 114 pSalI2CMngtAdpt->pHalOpInit = HalI2COpInit_V04;
AnnaBridge 167:e84263d55307 115 #endif
AnnaBridge 167:e84263d55307 116 /* To assign the default (ROM) HAL GDMA OP initialization function */
AnnaBridge 167:e84263d55307 117 pSalI2CMngtAdpt->pHalGdmaOpInit = HalGdmaOpInit;
AnnaBridge 167:e84263d55307 118
AnnaBridge 167:e84263d55307 119 /* To assign the default (ROM) SAL interrupt function */
AnnaBridge 167:e84263d55307 120 #if defined(CONFIG_CHIP_A_CUT) || defined(CONFIG_CHIP_B_CUT) || defined(CONFIG_CHIP_C_CUT)
AnnaBridge 167:e84263d55307 121 pSalI2CMngtAdpt->pSalIrqFunc = I2CISRHandle_Patch;
AnnaBridge 167:e84263d55307 122 #elif defined(CONFIG_CHIP_E_CUT)
AnnaBridge 167:e84263d55307 123 pSalI2CMngtAdpt->pSalIrqFunc = I2CISRHandle_V04;
AnnaBridge 167:e84263d55307 124 #endif
AnnaBridge 167:e84263d55307 125
AnnaBridge 167:e84263d55307 126 /* To assign the default (ROM) SAL DMA TX interrupt function */
AnnaBridge 167:e84263d55307 127 pSalI2CMngtAdpt->pSalDMATxIrqFunc = I2CTXGDMAISRHandle;
AnnaBridge 167:e84263d55307 128
AnnaBridge 167:e84263d55307 129 /* To assign the default (ROM) SAL DMA RX interrupt function */
AnnaBridge 167:e84263d55307 130 pSalI2CMngtAdpt->pSalDMARxIrqFunc = I2CRXGDMAISRHandle;
AnnaBridge 167:e84263d55307 131
AnnaBridge 167:e84263d55307 132 pSalI2CMngtAdpt->pHalInitDat = &(obj->HalI2CInitData);
AnnaBridge 167:e84263d55307 133 pSalI2CMngtAdpt->pHalOp = &(obj->HalI2COp);
AnnaBridge 167:e84263d55307 134 pSalI2CMngtAdpt->pIrqHnd = &(obj->I2CIrqHandleDat);
AnnaBridge 167:e84263d55307 135 pSalI2CMngtAdpt->pHalTxGdmaAdp = &(obj->HalI2CTxGdmaAdpt);
AnnaBridge 167:e84263d55307 136 pSalI2CMngtAdpt->pHalRxGdmaAdp = &(obj->HalI2CRxGdmaAdpt);
AnnaBridge 167:e84263d55307 137 pSalI2CMngtAdpt->pHalGdmaOp = &(obj->HalI2CGdmaOp);
AnnaBridge 167:e84263d55307 138 pSalI2CMngtAdpt->pIrqTxGdmaHnd = &(obj->I2CTxGdmaIrqHandleDat);
AnnaBridge 167:e84263d55307 139 pSalI2CMngtAdpt->pIrqRxGdmaHnd = &(obj->I2CRxGdmaIrqHandleDat);
AnnaBridge 167:e84263d55307 140 pSalI2CMngtAdpt->pUserCB = &(obj->SalI2CUserCB);
AnnaBridge 167:e84263d55307 141 pSalI2CMngtAdpt->pDMAConf = &(obj->SalI2CDmaUserDef);
AnnaBridge 167:e84263d55307 142
AnnaBridge 167:e84263d55307 143 /* Assign the private SAL handle to public SAL handle */
AnnaBridge 167:e84263d55307 144 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 145
AnnaBridge 167:e84263d55307 146 /* Assign the internal HAL initial data pointer to the SAL handle */
AnnaBridge 167:e84263d55307 147 pSalI2CHND->pInitDat = pSalI2CMngtAdpt->pHalInitDat;
AnnaBridge 167:e84263d55307 148
AnnaBridge 167:e84263d55307 149 /* Assign the internal user callback pointer to the SAL handle */
AnnaBridge 167:e84263d55307 150 pSalI2CHND->pUserCB = pSalI2CMngtAdpt->pUserCB;
AnnaBridge 167:e84263d55307 151
AnnaBridge 167:e84263d55307 152 /* Assign the internal user define DMA configuration to the SAL handle */
AnnaBridge 167:e84263d55307 153 pSalI2CHND->pDMAConf = pSalI2CMngtAdpt->pDMAConf;
AnnaBridge 167:e84263d55307 154
AnnaBridge 167:e84263d55307 155 /*To assign user callback pointers*/
AnnaBridge 167:e84263d55307 156 pSalI2CMngtAdpt->pUserCB->pTXCB = pSalI2CUserCBAdpt;
AnnaBridge 167:e84263d55307 157 pSalI2CMngtAdpt->pUserCB->pTXCCB = (pSalI2CUserCBAdpt+1);
AnnaBridge 167:e84263d55307 158 pSalI2CMngtAdpt->pUserCB->pRXCB = (pSalI2CUserCBAdpt+2);
AnnaBridge 167:e84263d55307 159 pSalI2CMngtAdpt->pUserCB->pRXCCB = (pSalI2CUserCBAdpt+3);
AnnaBridge 167:e84263d55307 160 pSalI2CMngtAdpt->pUserCB->pRDREQCB = (pSalI2CUserCBAdpt+4);
AnnaBridge 167:e84263d55307 161 pSalI2CMngtAdpt->pUserCB->pERRCB = (pSalI2CUserCBAdpt+5);
AnnaBridge 167:e84263d55307 162 pSalI2CMngtAdpt->pUserCB->pDMATXCB = (pSalI2CUserCBAdpt+6);
AnnaBridge 167:e84263d55307 163 pSalI2CMngtAdpt->pUserCB->pDMATXCCB = (pSalI2CUserCBAdpt+7);
AnnaBridge 167:e84263d55307 164 pSalI2CMngtAdpt->pUserCB->pDMARXCB = (pSalI2CUserCBAdpt+8);
AnnaBridge 167:e84263d55307 165 pSalI2CMngtAdpt->pUserCB->pDMARXCCB = (pSalI2CUserCBAdpt+9);
AnnaBridge 167:e84263d55307 166 pSalI2CMngtAdpt->pUserCB->pGENCALLCB= (pSalI2CUserCBAdpt+10);
AnnaBridge 167:e84263d55307 167
AnnaBridge 167:e84263d55307 168 /* Set I2C Device Number */
AnnaBridge 167:e84263d55307 169 pSalI2CHND->DevNum = i2c_idx;
AnnaBridge 167:e84263d55307 170
AnnaBridge 167:e84263d55307 171 /* Load I2C default value */
AnnaBridge 167:e84263d55307 172 RtkI2CLoadDefault(pSalI2CHND);
AnnaBridge 167:e84263d55307 173
AnnaBridge 167:e84263d55307 174 /* Assign I2C Pin Mux */
AnnaBridge 167:e84263d55307 175 pSalI2CHND->PinMux = RTL_GET_PERI_SEL(i2c_sel);
AnnaBridge 167:e84263d55307 176 pSalI2CHND->OpType = I2C_INTR_TYPE;
AnnaBridge 167:e84263d55307 177 pSalI2CHND->I2CMaster = I2C_MASTER_MODE;
AnnaBridge 167:e84263d55307 178 pSalI2CHND->I2CSpdMod = I2C_SS_MODE;
AnnaBridge 167:e84263d55307 179 pSalI2CHND->I2CClk = 100;
AnnaBridge 167:e84263d55307 180 pSalI2CHND->I2CAckAddr = 0;
AnnaBridge 167:e84263d55307 181 pSalI2CHND->TimeOut = 300;
AnnaBridge 167:e84263d55307 182 pSalI2CHND->AddRtyTimeOut = 3000;
AnnaBridge 167:e84263d55307 183 pSalI2CHND->I2CExd |= (I2C_EXD_MTR_ADDR_RTY);
AnnaBridge 167:e84263d55307 184
AnnaBridge 167:e84263d55307 185 pSalI2CMngtAdpt->InnerTimeOut = pSalI2CHND->TimeOut;
AnnaBridge 167:e84263d55307 186
AnnaBridge 167:e84263d55307 187 /* Init I2C now */
AnnaBridge 167:e84263d55307 188 pSalI2CHND->pInitDat->I2CAckAddr = i2c_target_addr[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 189 HalI2CSetTarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 190 HalI2CSetSarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 191 RtkI2CInitForPS(pSalI2CHND);
AnnaBridge 167:e84263d55307 192 }
AnnaBridge 167:e84263d55307 193
AnnaBridge 167:e84263d55307 194 void i2c_frequency(i2c_t *obj, int hz)
AnnaBridge 167:e84263d55307 195 {
AnnaBridge 167:e84263d55307 196 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 197 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 198 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 199 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 200
AnnaBridge 167:e84263d55307 201 uint16_t i2c_default_clk = (uint16_t) pSalI2CHND->I2CClk;
AnnaBridge 167:e84263d55307 202 uint16_t i2c_user_clk = (uint16_t) (hz/1000);
AnnaBridge 167:e84263d55307 203
AnnaBridge 167:e84263d55307 204
AnnaBridge 167:e84263d55307 205 if (i2c_default_clk != i2c_user_clk) {
AnnaBridge 167:e84263d55307 206 /* Deinit I2C first */
AnnaBridge 167:e84263d55307 207 i2c_reset(obj);
AnnaBridge 167:e84263d55307 208 if (i2c_user_clk <= 100) {
AnnaBridge 167:e84263d55307 209 pSalI2CHND->I2CSpdMod = I2C_SS_MODE;
AnnaBridge 167:e84263d55307 210 } else if ((i2c_user_clk > 100) && (i2c_user_clk <= 400)) {
AnnaBridge 167:e84263d55307 211 pSalI2CHND->I2CSpdMod = I2C_FS_MODE;
AnnaBridge 167:e84263d55307 212 } else if (i2c_user_clk > 400) {
AnnaBridge 167:e84263d55307 213 pSalI2CHND->I2CSpdMod = I2C_HS_MODE;
AnnaBridge 167:e84263d55307 214 } else {
AnnaBridge 167:e84263d55307 215 pSalI2CHND->I2CSpdMod = I2C_SS_MODE;
AnnaBridge 167:e84263d55307 216 }
AnnaBridge 167:e84263d55307 217
AnnaBridge 167:e84263d55307 218 /* Load the user defined I2C clock */
AnnaBridge 167:e84263d55307 219 pSalI2CHND->I2CClk = i2c_user_clk;
AnnaBridge 167:e84263d55307 220
AnnaBridge 167:e84263d55307 221 /* Init I2C now */
AnnaBridge 167:e84263d55307 222 pSalI2CHND->pInitDat->I2CAckAddr = i2c_target_addr[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 223 HalI2CSetTarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 224 HalI2CSetSarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 225 RtkI2CInitForPS(pSalI2CHND);
AnnaBridge 167:e84263d55307 226 }
AnnaBridge 167:e84263d55307 227 }
AnnaBridge 167:e84263d55307 228
AnnaBridge 167:e84263d55307 229 inline int i2c_start(i2c_t *obj)
AnnaBridge 167:e84263d55307 230 {
AnnaBridge 167:e84263d55307 231 memset(address_save_int , 0, sizeof(address_save_int));
AnnaBridge 167:e84263d55307 232 memset(Byte_count , 0, sizeof(Byte_count));
AnnaBridge 167:e84263d55307 233 memset(address_save, 0, sizeof(address_save));
AnnaBridge 167:e84263d55307 234 return 0;
AnnaBridge 167:e84263d55307 235 }
AnnaBridge 167:e84263d55307 236
AnnaBridge 167:e84263d55307 237 inline int i2c_stop(i2c_t *obj)
AnnaBridge 167:e84263d55307 238 {
AnnaBridge 167:e84263d55307 239 return 0;
AnnaBridge 167:e84263d55307 240 }
AnnaBridge 167:e84263d55307 241
AnnaBridge 167:e84263d55307 242 extern u32 HalDelayUs(IN u32 us);
AnnaBridge 167:e84263d55307 243
AnnaBridge 167:e84263d55307 244 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
AnnaBridge 167:e84263d55307 245 {
AnnaBridge 167:e84263d55307 246
AnnaBridge 167:e84263d55307 247 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 248 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 249 u32 I2CInTOTcnt = 0;
AnnaBridge 167:e84263d55307 250 u32 InTimeoutCount = 0;
AnnaBridge 167:e84263d55307 251 u32 InStartCount = 0;
AnnaBridge 167:e84263d55307 252 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 253 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 254 address = (address & 0xFE ) >>1;
AnnaBridge 167:e84263d55307 255
AnnaBridge 167:e84263d55307 256 if (i2c_target_addr[pSalI2CHND->DevNum] != address) {
AnnaBridge 167:e84263d55307 257 pSalI2CHND->pInitDat->I2CAckAddr = address;
AnnaBridge 167:e84263d55307 258 i2c_target_addr[pSalI2CHND->DevNum] = address;
AnnaBridge 167:e84263d55307 259 HalI2CSetTarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 260 }
AnnaBridge 167:e84263d55307 261
AnnaBridge 167:e84263d55307 262 /* Check if the it's the last byte or not */
AnnaBridge 167:e84263d55307 263 pSalI2CHND->I2CExd &= (~I2C_EXD_MTR_HOLD_BUS);
AnnaBridge 167:e84263d55307 264 if (!stop) {
AnnaBridge 167:e84263d55307 265 pSalI2CHND->I2CExd |= I2C_EXD_MTR_HOLD_BUS;
AnnaBridge 167:e84263d55307 266 }
AnnaBridge 167:e84263d55307 267
AnnaBridge 167:e84263d55307 268 pSalI2CHND->pRXBuf = &i2crxtranbuf[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 269 pSalI2CHND->pRXBuf->DataLen = length;
AnnaBridge 167:e84263d55307 270 pSalI2CHND->pRXBuf->TargetAddr= address;//pSalI2CHND->I2CAckAddr;
AnnaBridge 167:e84263d55307 271 pSalI2CHND->pRXBuf->RegAddr = 0;
AnnaBridge 167:e84263d55307 272 pSalI2CHND->pRXBuf->pDataBuf = (u8 *)data;
AnnaBridge 167:e84263d55307 273
AnnaBridge 167:e84263d55307 274 if (RtkI2CReceive(pSalI2CHND) != HAL_OK) {
AnnaBridge 167:e84263d55307 275 length = length - pSalI2CHND->pRXBuf->DataLen;
AnnaBridge 167:e84263d55307 276 return ((int)length);
AnnaBridge 167:e84263d55307 277 } else {
AnnaBridge 167:e84263d55307 278 /* Calculate user time out parameters */
AnnaBridge 167:e84263d55307 279 I2CInTOTcnt = 300;
AnnaBridge 167:e84263d55307 280 if ((I2CInTOTcnt != 0) && (I2CInTOTcnt != I2C_TIMEOOUT_ENDLESS)) {
AnnaBridge 167:e84263d55307 281 InTimeoutCount = (I2CInTOTcnt*1000/TIMER_TICK_US);
AnnaBridge 167:e84263d55307 282 InStartCount = HalTimerOp.HalTimerReadCount(1);
AnnaBridge 167:e84263d55307 283 }
AnnaBridge 167:e84263d55307 284 while((pSalI2CHND->DevSts != I2C_STS_IDLE) &&
AnnaBridge 167:e84263d55307 285 (pSalI2CHND->DevSts != I2C_STS_ERROR) &&
AnnaBridge 167:e84263d55307 286 (pSalI2CHND->DevSts != I2C_STS_TIMEOUT)) {
AnnaBridge 167:e84263d55307 287 /* Time-Out check */
AnnaBridge 167:e84263d55307 288 if (InTimeoutCount > 0) {
AnnaBridge 167:e84263d55307 289 if (HAL_TIMEOUT == I2CIsTimeout(InStartCount, InTimeoutCount)) {
AnnaBridge 167:e84263d55307 290 pSalI2CHND->DevSts = I2C_STS_TIMEOUT;
AnnaBridge 167:e84263d55307 291 pSalI2CHND->ErrType = I2C_ERR_RX_ADD_TO;
AnnaBridge 167:e84263d55307 292
AnnaBridge 167:e84263d55307 293 return ((int)(length));
AnnaBridge 167:e84263d55307 294 }
AnnaBridge 167:e84263d55307 295 } else {
AnnaBridge 167:e84263d55307 296 if (I2CInTOTcnt == 0) {
AnnaBridge 167:e84263d55307 297 pSalI2CHND->DevSts = I2C_STS_TIMEOUT;
AnnaBridge 167:e84263d55307 298 pSalI2CHND->ErrType = I2C_ERR_RX_ADD_TO;
AnnaBridge 167:e84263d55307 299
AnnaBridge 167:e84263d55307 300 return ((int)(length));
AnnaBridge 167:e84263d55307 301 }
AnnaBridge 167:e84263d55307 302 }
AnnaBridge 167:e84263d55307 303 }
AnnaBridge 167:e84263d55307 304 if (pSalI2CHND->DevSts != I2C_STS_TIMEOUT) {
AnnaBridge 167:e84263d55307 305 return ((int)(length - pSalI2CHND->pRXBuf->DataLen));
AnnaBridge 167:e84263d55307 306 } else {
AnnaBridge 167:e84263d55307 307 return ((int)(length));
AnnaBridge 167:e84263d55307 308 }
AnnaBridge 167:e84263d55307 309 }
AnnaBridge 167:e84263d55307 310 }
AnnaBridge 167:e84263d55307 311
AnnaBridge 167:e84263d55307 312 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
AnnaBridge 167:e84263d55307 313 {
AnnaBridge 167:e84263d55307 314 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 315 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 316 u32 I2CInTOTcnt = 0;
AnnaBridge 167:e84263d55307 317 u32 InTimeoutCount = 0;
AnnaBridge 167:e84263d55307 318 u32 InStartCount = 0;
AnnaBridge 167:e84263d55307 319
AnnaBridge 167:e84263d55307 320 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 321 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 322 address = (address & 0xFE ) >>1;
AnnaBridge 167:e84263d55307 323
AnnaBridge 167:e84263d55307 324 pSalI2CHND->pInitDat->I2CAckAddr = address;
AnnaBridge 167:e84263d55307 325 i2c_target_addr[pSalI2CHND->DevNum] = address;
AnnaBridge 167:e84263d55307 326 HalI2CSetTarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 327
AnnaBridge 167:e84263d55307 328 /* Check if the it's the last byte or not */
AnnaBridge 167:e84263d55307 329 pSalI2CHND->I2CExd &= (~I2C_EXD_MTR_HOLD_BUS);
AnnaBridge 167:e84263d55307 330 if (!stop) {
AnnaBridge 167:e84263d55307 331 pSalI2CHND->I2CExd |= I2C_EXD_MTR_HOLD_BUS;
AnnaBridge 167:e84263d55307 332 }
AnnaBridge 167:e84263d55307 333
AnnaBridge 167:e84263d55307 334 pSalI2CHND->pTXBuf = &i2ctxtranbuf[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 335 pSalI2CHND->pTXBuf->DataLen = length;
AnnaBridge 167:e84263d55307 336 pSalI2CHND->pTXBuf->TargetAddr= address;
AnnaBridge 167:e84263d55307 337 pSalI2CHND->pTXBuf->RegAddr = 0;
AnnaBridge 167:e84263d55307 338 pSalI2CHND->pTXBuf->pDataBuf = (u8 *)data;
AnnaBridge 167:e84263d55307 339
AnnaBridge 167:e84263d55307 340 if (RtkI2CSend(pSalI2CHND) != HAL_OK) {
AnnaBridge 167:e84263d55307 341 length = length - pSalI2CHND->pTXBuf->DataLen;
AnnaBridge 167:e84263d55307 342 return ((int)length);
AnnaBridge 167:e84263d55307 343 } else {
AnnaBridge 167:e84263d55307 344 /* Calculate user time out parameters */
AnnaBridge 167:e84263d55307 345 I2CInTOTcnt = 300;
AnnaBridge 167:e84263d55307 346 if ((I2CInTOTcnt != 0) && (I2CInTOTcnt != I2C_TIMEOOUT_ENDLESS)) {
AnnaBridge 167:e84263d55307 347 InTimeoutCount = (I2CInTOTcnt*1000/TIMER_TICK_US);
AnnaBridge 167:e84263d55307 348 InStartCount = HalTimerOp.HalTimerReadCount(1);
AnnaBridge 167:e84263d55307 349 }
AnnaBridge 167:e84263d55307 350 while((pSalI2CHND->DevSts != I2C_STS_IDLE) &&
AnnaBridge 167:e84263d55307 351 (pSalI2CHND->DevSts != I2C_STS_ERROR) &&
AnnaBridge 167:e84263d55307 352 (pSalI2CHND->DevSts != I2C_STS_TIMEOUT)) {
AnnaBridge 167:e84263d55307 353 /* Time-Out check */
AnnaBridge 167:e84263d55307 354 if (InTimeoutCount > 0) {
AnnaBridge 167:e84263d55307 355 if (HAL_TIMEOUT == I2CIsTimeout(InStartCount, InTimeoutCount)) {
AnnaBridge 167:e84263d55307 356 pSalI2CHND->DevSts = I2C_STS_TIMEOUT;
AnnaBridge 167:e84263d55307 357 pSalI2CHND->ErrType = I2C_ERR_TX_ADD_TO;
AnnaBridge 167:e84263d55307 358 return ((int)(length));
AnnaBridge 167:e84263d55307 359 }
AnnaBridge 167:e84263d55307 360 } else {
AnnaBridge 167:e84263d55307 361 if (I2CInTOTcnt == 0) {
AnnaBridge 167:e84263d55307 362 pSalI2CHND->DevSts = I2C_STS_TIMEOUT;
AnnaBridge 167:e84263d55307 363 pSalI2CHND->ErrType = I2C_ERR_TX_ADD_TO;
AnnaBridge 167:e84263d55307 364 return ((int)(length));
AnnaBridge 167:e84263d55307 365 }
AnnaBridge 167:e84263d55307 366 }
AnnaBridge 167:e84263d55307 367 }
AnnaBridge 167:e84263d55307 368
AnnaBridge 167:e84263d55307 369 if (pSalI2CHND->DevSts != I2C_STS_TIMEOUT) {
AnnaBridge 167:e84263d55307 370 return ((int)(length - pSalI2CHND->pTXBuf->DataLen));
AnnaBridge 167:e84263d55307 371 } else {
AnnaBridge 167:e84263d55307 372 return ((int)(length));
AnnaBridge 167:e84263d55307 373 }
AnnaBridge 167:e84263d55307 374 }
AnnaBridge 167:e84263d55307 375 }
AnnaBridge 167:e84263d55307 376
AnnaBridge 167:e84263d55307 377 int i2c_byte_read(i2c_t *obj, int last)
AnnaBridge 167:e84263d55307 378 {
AnnaBridge 167:e84263d55307 379 uint8_t i2cdatlocal;
AnnaBridge 167:e84263d55307 380 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 381 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 382 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 383 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 384
AnnaBridge 167:e84263d55307 385 /* Check if the it's the last byte or not */
AnnaBridge 167:e84263d55307 386 pSalI2CHND->I2CExd &= (~I2C_EXD_MTR_HOLD_BUS);
AnnaBridge 167:e84263d55307 387 if (!last) {
AnnaBridge 167:e84263d55307 388 pSalI2CHND->I2CExd |= I2C_EXD_MTR_HOLD_BUS;
AnnaBridge 167:e84263d55307 389 }
AnnaBridge 167:e84263d55307 390
AnnaBridge 167:e84263d55307 391 pSalI2CHND->pRXBuf = &i2crxtranbuf[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 392 pSalI2CHND->pRXBuf->DataLen = 1;
AnnaBridge 167:e84263d55307 393 pSalI2CHND->pRXBuf->TargetAddr= i2c_target_addr[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 394 pSalI2CHND->pRXBuf->RegAddr = 0;
AnnaBridge 167:e84263d55307 395 pSalI2CHND->pRXBuf->pDataBuf = &i2cdatlocal;
AnnaBridge 167:e84263d55307 396 RtkI2CReceive(pSalI2CHND);
AnnaBridge 167:e84263d55307 397
AnnaBridge 167:e84263d55307 398 return (int)i2cdatlocal;
AnnaBridge 167:e84263d55307 399 }
AnnaBridge 167:e84263d55307 400
AnnaBridge 167:e84263d55307 401 int i2c_byte_write(i2c_t *obj, int data)
AnnaBridge 167:e84263d55307 402 {
AnnaBridge 167:e84263d55307 403 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 404 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 405 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 406 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 407 u8 * dp = (u8 *)&address_save[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 408 if(Byte_count[pSalI2CHND->DevNum]<3){
AnnaBridge 167:e84263d55307 409 dp[Byte_count[pSalI2CHND->DevNum]] = data;
AnnaBridge 167:e84263d55307 410 Byte_count[pSalI2CHND->DevNum]++;
AnnaBridge 167:e84263d55307 411 if(Byte_count[pSalI2CHND->DevNum]==3){
AnnaBridge 167:e84263d55307 412 address_save_int[pSalI2CHND->DevNum] = (dp[1]<<8)+dp[2];
AnnaBridge 167:e84263d55307 413 }
AnnaBridge 167:e84263d55307 414 return 1;
AnnaBridge 167:e84263d55307 415 }
AnnaBridge 167:e84263d55307 416 int address = (dp[0] & 0xFE ) >> 1;
AnnaBridge 167:e84263d55307 417 dp[1]= (unsigned char)(address_save_int[pSalI2CHND->DevNum] >> 8);
AnnaBridge 167:e84263d55307 418 dp[2]= (unsigned char)(address_save_int[pSalI2CHND->DevNum] & 0xFF);
AnnaBridge 167:e84263d55307 419 dp[3]= (unsigned char)data;
AnnaBridge 167:e84263d55307 420
AnnaBridge 167:e84263d55307 421 pSalI2CHND->pInitDat->I2CAckAddr = address;
AnnaBridge 167:e84263d55307 422 i2c_target_addr[pSalI2CHND->DevNum] = address;
AnnaBridge 167:e84263d55307 423 HalI2CSetTarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 424
AnnaBridge 167:e84263d55307 425 pSalI2CHND->I2CExd &= (~I2C_EXD_MTR_HOLD_BUS);
AnnaBridge 167:e84263d55307 426 pSalI2CHND->pTXBuf = &i2ctxtranbuf[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 427 pSalI2CHND->pTXBuf->DataLen = 3;
AnnaBridge 167:e84263d55307 428 pSalI2CHND->pTXBuf->TargetAddr= i2c_target_addr[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 429 pSalI2CHND->pTXBuf->RegAddr = 0;
AnnaBridge 167:e84263d55307 430 pSalI2CHND->pTXBuf->pDataBuf = dp+1;
AnnaBridge 167:e84263d55307 431
AnnaBridge 167:e84263d55307 432 if (RtkI2CSend(pSalI2CHND) != HAL_OK) {
AnnaBridge 167:e84263d55307 433 return 0;
AnnaBridge 167:e84263d55307 434 }
AnnaBridge 167:e84263d55307 435 address_save_int[pSalI2CHND->DevNum]++;
AnnaBridge 167:e84263d55307 436 return 1;
AnnaBridge 167:e84263d55307 437 }
AnnaBridge 167:e84263d55307 438
AnnaBridge 167:e84263d55307 439 void i2c_reset(i2c_t *obj)
AnnaBridge 167:e84263d55307 440 {
AnnaBridge 167:e84263d55307 441 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 442 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 443 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 444 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 445
AnnaBridge 167:e84263d55307 446 /* Deinit I2C directly */
AnnaBridge 167:e84263d55307 447 RtkI2CDeInitForPS(pSalI2CHND);
AnnaBridge 167:e84263d55307 448 }
AnnaBridge 167:e84263d55307 449
AnnaBridge 167:e84263d55307 450 #if DEVICE_I2CSLAVE
AnnaBridge 167:e84263d55307 451
AnnaBridge 167:e84263d55307 452 void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
AnnaBridge 167:e84263d55307 453 {
AnnaBridge 167:e84263d55307 454 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 455 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 456 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 457 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 458 address = (address & 0xFE ) >>1;
AnnaBridge 167:e84263d55307 459
AnnaBridge 167:e84263d55307 460 uint16_t i2c_default_addr = (uint16_t) pSalI2CHND->I2CAckAddr;
AnnaBridge 167:e84263d55307 461 uint16_t i2c_user_addr = (uint16_t) address;
AnnaBridge 167:e84263d55307 462
AnnaBridge 167:e84263d55307 463 if (i2c_target_addr[pSalI2CHND->DevNum] != i2c_user_addr) {
AnnaBridge 167:e84263d55307 464 pSalI2CHND->pInitDat->I2CAckAddr = address;
AnnaBridge 167:e84263d55307 465 i2c_target_addr[pSalI2CHND->DevNum] = address;
AnnaBridge 167:e84263d55307 466 HalI2CSetSarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 467 }
AnnaBridge 167:e84263d55307 468 }
AnnaBridge 167:e84263d55307 469
AnnaBridge 167:e84263d55307 470 void i2c_slave_mode(i2c_t *obj, int enable_slave)
AnnaBridge 167:e84263d55307 471 {
AnnaBridge 167:e84263d55307 472 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 473 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 474 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 475 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 476
AnnaBridge 167:e84263d55307 477 /* Deinit I2C first */
AnnaBridge 167:e84263d55307 478 i2c_reset(obj);
AnnaBridge 167:e84263d55307 479
AnnaBridge 167:e84263d55307 480 /* Load the user defined I2C clock */
AnnaBridge 167:e84263d55307 481 pSalI2CHND->I2CMaster = I2C_MASTER_MODE;
AnnaBridge 167:e84263d55307 482 if (enable_slave)
AnnaBridge 167:e84263d55307 483 pSalI2CHND->I2CMaster = I2C_SLAVE_MODE;
AnnaBridge 167:e84263d55307 484
AnnaBridge 167:e84263d55307 485 /* Init I2C now */
AnnaBridge 167:e84263d55307 486 RtkI2CInitForPS(pSalI2CHND);
AnnaBridge 167:e84263d55307 487
AnnaBridge 167:e84263d55307 488 pSalI2CHND->pInitDat->I2CAckAddr = i2c_target_addr[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 489 HalI2CSetSarRtl8195a(pSalI2CHND->pInitDat);
AnnaBridge 167:e84263d55307 490 }
AnnaBridge 167:e84263d55307 491
AnnaBridge 167:e84263d55307 492 // See I2CSlave.h
AnnaBridge 167:e84263d55307 493 #define NoData 0 // the slave has not been addressed
AnnaBridge 167:e84263d55307 494 #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
AnnaBridge 167:e84263d55307 495 #define WriteGeneral 2 // the master is writing to all slave
AnnaBridge 167:e84263d55307 496 #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
AnnaBridge 167:e84263d55307 497
AnnaBridge 167:e84263d55307 498 int i2c_slave_receive(i2c_t *obj)
AnnaBridge 167:e84263d55307 499 {
AnnaBridge 167:e84263d55307 500 int i2cslvrevsts = NoData;
AnnaBridge 167:e84263d55307 501 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 502 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 503 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 504 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 505
AnnaBridge 167:e84263d55307 506 i2cslvrevsts = RtkSalI2CSts(pSalI2CHND);
AnnaBridge 167:e84263d55307 507 return i2cslvrevsts;
AnnaBridge 167:e84263d55307 508 }
AnnaBridge 167:e84263d55307 509
AnnaBridge 167:e84263d55307 510 int i2c_slave_read(i2c_t *obj, char *data, int length)
AnnaBridge 167:e84263d55307 511 {
AnnaBridge 167:e84263d55307 512 u32 I2CInTOTcnt = 0;
AnnaBridge 167:e84263d55307 513 u32 InTimeoutCount = 0;
AnnaBridge 167:e84263d55307 514 u32 InStartCount = 0;
AnnaBridge 167:e84263d55307 515
AnnaBridge 167:e84263d55307 516 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 517 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 518 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 519 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 520
AnnaBridge 167:e84263d55307 521 pSalI2CHND->pRXBuf = &i2crxtranbuf[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 522 pSalI2CHND->pRXBuf->DataLen = length;
AnnaBridge 167:e84263d55307 523 pSalI2CHND->pRXBuf->pDataBuf = (u8 *)data;
AnnaBridge 167:e84263d55307 524
AnnaBridge 167:e84263d55307 525 if (RtkI2CReceive(pSalI2CHND) != HAL_OK) {
AnnaBridge 167:e84263d55307 526 return 0; //error
AnnaBridge 167:e84263d55307 527 } else {
AnnaBridge 167:e84263d55307 528 /* Calculate user time out parameters */
AnnaBridge 167:e84263d55307 529 I2CInTOTcnt = 300;
AnnaBridge 167:e84263d55307 530 if ((I2CInTOTcnt != 0) && (I2CInTOTcnt != I2C_TIMEOOUT_ENDLESS)) {
AnnaBridge 167:e84263d55307 531 InTimeoutCount = (I2CInTOTcnt*1000/TIMER_TICK_US);
AnnaBridge 167:e84263d55307 532 InStartCount = HalTimerOp.HalTimerReadCount(1);
AnnaBridge 167:e84263d55307 533 }
AnnaBridge 167:e84263d55307 534 while((pSalI2CHND->DevSts != I2C_STS_IDLE) &&
AnnaBridge 167:e84263d55307 535 (pSalI2CHND->DevSts != I2C_STS_ERROR) &&
AnnaBridge 167:e84263d55307 536 (pSalI2CHND->DevSts != I2C_STS_TIMEOUT)) {
AnnaBridge 167:e84263d55307 537 /* Time-Out check */
AnnaBridge 167:e84263d55307 538 if (InTimeoutCount > 0) {
AnnaBridge 167:e84263d55307 539 if (HAL_TIMEOUT == I2CIsTimeout(InStartCount, InTimeoutCount)) {
AnnaBridge 167:e84263d55307 540 pSalI2CHND->DevSts = I2C_STS_TIMEOUT;
AnnaBridge 167:e84263d55307 541 pSalI2CHND->ErrType = I2C_ERR_RX_ADD_TO;
AnnaBridge 167:e84263d55307 542 return ((int)(length));
AnnaBridge 167:e84263d55307 543 }
AnnaBridge 167:e84263d55307 544 } else {
AnnaBridge 167:e84263d55307 545 if (I2CInTOTcnt == 0) {
AnnaBridge 167:e84263d55307 546 pSalI2CHND->DevSts = I2C_STS_TIMEOUT;
AnnaBridge 167:e84263d55307 547 pSalI2CHND->ErrType = I2C_ERR_RX_ADD_TO;
AnnaBridge 167:e84263d55307 548 return ((int)(length));
AnnaBridge 167:e84263d55307 549 }
AnnaBridge 167:e84263d55307 550 }
AnnaBridge 167:e84263d55307 551 }
AnnaBridge 167:e84263d55307 552
AnnaBridge 167:e84263d55307 553 if (pSalI2CHND->DevSts != I2C_STS_TIMEOUT) {
AnnaBridge 167:e84263d55307 554 return ((int)(length - pSalI2CHND->pTXBuf->DataLen));
AnnaBridge 167:e84263d55307 555 } else {
AnnaBridge 167:e84263d55307 556 return ((int)(length));
AnnaBridge 167:e84263d55307 557 }
AnnaBridge 167:e84263d55307 558 }
AnnaBridge 167:e84263d55307 559 }
AnnaBridge 167:e84263d55307 560
AnnaBridge 167:e84263d55307 561 int i2c_slave_write(i2c_t *obj, const char *data, int length)
AnnaBridge 167:e84263d55307 562 {
AnnaBridge 167:e84263d55307 563 PSAL_I2C_MNGT_ADPT pSalI2CMngtAdpt = NULL;
AnnaBridge 167:e84263d55307 564 PSAL_I2C_HND pSalI2CHND = NULL;
AnnaBridge 167:e84263d55307 565 pSalI2CMngtAdpt = &(obj->SalI2CMngtAdpt);
AnnaBridge 167:e84263d55307 566 pSalI2CHND = &(pSalI2CMngtAdpt->pSalHndPriv->SalI2CHndPriv);
AnnaBridge 167:e84263d55307 567
AnnaBridge 167:e84263d55307 568 pSalI2CHND->pTXBuf = &i2ctxtranbuf[pSalI2CHND->DevNum];
AnnaBridge 167:e84263d55307 569 pSalI2CHND->pTXBuf->DataLen = length;
AnnaBridge 167:e84263d55307 570 pSalI2CHND->pTXBuf->pDataBuf = (u8 *)data;
AnnaBridge 167:e84263d55307 571
AnnaBridge 167:e84263d55307 572 if (RtkI2CSend(pSalI2CHND) != HAL_OK) {
AnnaBridge 167:e84263d55307 573 return 0; //error
AnnaBridge 167:e84263d55307 574 }
AnnaBridge 167:e84263d55307 575
AnnaBridge 167:e84263d55307 576 return 1;
AnnaBridge 167:e84263d55307 577 }
AnnaBridge 167:e84263d55307 578
AnnaBridge 167:e84263d55307 579 #endif // CONFIG_I2C_SLAVE_EN
AnnaBridge 167:e84263d55307 580
AnnaBridge 167:e84263d55307 581 #endif // CONFIG_I2C_EN
AnnaBridge 167:e84263d55307 582