mbed library sources. Supersedes mbed-src. Edited target satm32f446 for user USART3 pins
Fork of mbed-dev by
targets/TARGET_TOSHIBA/TARGET_TMPM46B/i2c_api.c@184:08ed48f1de7f, 2018-04-19 (annotated)
- Committer:
- AnnaBridge
- Date:
- Thu Apr 19 17:12:19 2018 +0100
- Revision:
- 184:08ed48f1de7f
mbed-dev library. Release version 161
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AnnaBridge | 184:08ed48f1de7f | 1 | /* mbed Microcontroller Library |
AnnaBridge | 184:08ed48f1de7f | 2 | * (C)Copyright TOSHIBA ELECTRONIC DEVICES & STORAGE CORPORATION 2017 All rights reserved |
AnnaBridge | 184:08ed48f1de7f | 3 | * |
AnnaBridge | 184:08ed48f1de7f | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
AnnaBridge | 184:08ed48f1de7f | 5 | * you may not use this file except in compliance with the License. |
AnnaBridge | 184:08ed48f1de7f | 6 | * You may obtain a copy of the License at |
AnnaBridge | 184:08ed48f1de7f | 7 | * |
AnnaBridge | 184:08ed48f1de7f | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AnnaBridge | 184:08ed48f1de7f | 9 | * |
AnnaBridge | 184:08ed48f1de7f | 10 | * Unless required by applicable law or agreed to in writing, software |
AnnaBridge | 184:08ed48f1de7f | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AnnaBridge | 184:08ed48f1de7f | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AnnaBridge | 184:08ed48f1de7f | 13 | * See the License for the specific language governing permissions and |
AnnaBridge | 184:08ed48f1de7f | 14 | * limitations under the License. |
AnnaBridge | 184:08ed48f1de7f | 15 | */ |
AnnaBridge | 184:08ed48f1de7f | 16 | #include "i2c_api.h" |
AnnaBridge | 184:08ed48f1de7f | 17 | #include "mbed_error.h" |
AnnaBridge | 184:08ed48f1de7f | 18 | #include "PeripheralNames.h" |
AnnaBridge | 184:08ed48f1de7f | 19 | #include "pinmap.h" |
AnnaBridge | 184:08ed48f1de7f | 20 | #include "tmpm46b_i2c.h" |
AnnaBridge | 184:08ed48f1de7f | 21 | #include <string.h> |
AnnaBridge | 184:08ed48f1de7f | 22 | #include <stdlib.h> |
AnnaBridge | 184:08ed48f1de7f | 23 | |
AnnaBridge | 184:08ed48f1de7f | 24 | static const PinMap PinMap_I2C_SDA[] = { |
AnnaBridge | 184:08ed48f1de7f | 25 | {PK2, I2C_0, PIN_DATA(3, 2)}, |
AnnaBridge | 184:08ed48f1de7f | 26 | {PF7, I2C_1, PIN_DATA(4, 2)}, |
AnnaBridge | 184:08ed48f1de7f | 27 | {PH0, I2C_2, PIN_DATA(4, 2)}, |
AnnaBridge | 184:08ed48f1de7f | 28 | {NC, NC, 0} |
AnnaBridge | 184:08ed48f1de7f | 29 | }; |
AnnaBridge | 184:08ed48f1de7f | 30 | |
AnnaBridge | 184:08ed48f1de7f | 31 | static const PinMap PinMap_I2C_SCL[] = { |
AnnaBridge | 184:08ed48f1de7f | 32 | {PK3, I2C_0, PIN_DATA(3, 2)}, |
AnnaBridge | 184:08ed48f1de7f | 33 | {PF6, I2C_1, PIN_DATA(4, 2)}, |
AnnaBridge | 184:08ed48f1de7f | 34 | {PH1, I2C_2, PIN_DATA(4, 2)}, |
AnnaBridge | 184:08ed48f1de7f | 35 | {NC, NC, 0} |
AnnaBridge | 184:08ed48f1de7f | 36 | }; |
AnnaBridge | 184:08ed48f1de7f | 37 | |
AnnaBridge | 184:08ed48f1de7f | 38 | #define SBI_I2C_SEND 0x00 |
AnnaBridge | 184:08ed48f1de7f | 39 | #define SBI_I2C_RECEIVE 0x01 |
AnnaBridge | 184:08ed48f1de7f | 40 | #define MAX_NUM_I2C 3 |
AnnaBridge | 184:08ed48f1de7f | 41 | #define DELAY_MS_MULTIPLIER 5500 |
AnnaBridge | 184:08ed48f1de7f | 42 | |
AnnaBridge | 184:08ed48f1de7f | 43 | struct i2c_xfer { |
AnnaBridge | 184:08ed48f1de7f | 44 | int32_t count; |
AnnaBridge | 184:08ed48f1de7f | 45 | int32_t len; |
AnnaBridge | 184:08ed48f1de7f | 46 | void *done; |
AnnaBridge | 184:08ed48f1de7f | 47 | char *buf; |
AnnaBridge | 184:08ed48f1de7f | 48 | }; |
AnnaBridge | 184:08ed48f1de7f | 49 | |
AnnaBridge | 184:08ed48f1de7f | 50 | // Clock setting structure definition |
AnnaBridge | 184:08ed48f1de7f | 51 | typedef struct { |
AnnaBridge | 184:08ed48f1de7f | 52 | uint32_t sck; |
AnnaBridge | 184:08ed48f1de7f | 53 | uint32_t prsck; |
AnnaBridge | 184:08ed48f1de7f | 54 | } I2C_clock_setting_t; |
AnnaBridge | 184:08ed48f1de7f | 55 | |
AnnaBridge | 184:08ed48f1de7f | 56 | static void DelayMS(uint32_t delay) |
AnnaBridge | 184:08ed48f1de7f | 57 | { |
AnnaBridge | 184:08ed48f1de7f | 58 | volatile uint32_t VarI; |
AnnaBridge | 184:08ed48f1de7f | 59 | for (VarI = 0; VarI < delay * DELAY_MS_MULTIPLIER; VarI++); |
AnnaBridge | 184:08ed48f1de7f | 60 | } |
AnnaBridge | 184:08ed48f1de7f | 61 | |
AnnaBridge | 184:08ed48f1de7f | 62 | static const uint32_t I2C_SCK_DIVIDER_TBL[8] = { |
AnnaBridge | 184:08ed48f1de7f | 63 | 20, 24, 32, 48, 80, 144, 272, 528 |
AnnaBridge | 184:08ed48f1de7f | 64 | }; // SCK Divider value table |
AnnaBridge | 184:08ed48f1de7f | 65 | |
AnnaBridge | 184:08ed48f1de7f | 66 | static I2C_clock_setting_t clk; |
AnnaBridge | 184:08ed48f1de7f | 67 | static I2C_InitTypeDef myi2c; |
AnnaBridge | 184:08ed48f1de7f | 68 | static int32_t start_flag = 1; |
AnnaBridge | 184:08ed48f1de7f | 69 | static struct i2c_xfer xfer[MAX_NUM_I2C]; |
AnnaBridge | 184:08ed48f1de7f | 70 | static TSB_I2C_TypeDef *i2c_lut[MAX_NUM_I2C] = {TSB_I2C0, TSB_I2C1, TSB_I2C2}; |
AnnaBridge | 184:08ed48f1de7f | 71 | static char *gI2C_TxData = NULL; |
AnnaBridge | 184:08ed48f1de7f | 72 | static char *gI2C_LTxData = NULL; |
AnnaBridge | 184:08ed48f1de7f | 73 | static uint8_t send_byte = 0; |
AnnaBridge | 184:08ed48f1de7f | 74 | static uint8_t byte_func = 0; |
AnnaBridge | 184:08ed48f1de7f | 75 | |
AnnaBridge | 184:08ed48f1de7f | 76 | // Initialize the I2C peripheral. It sets the default parameters for I2C |
AnnaBridge | 184:08ed48f1de7f | 77 | void i2c_init(i2c_t *obj, PinName sda, PinName scl) |
AnnaBridge | 184:08ed48f1de7f | 78 | { |
AnnaBridge | 184:08ed48f1de7f | 79 | MBED_ASSERT(obj != NULL); |
AnnaBridge | 184:08ed48f1de7f | 80 | I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); |
AnnaBridge | 184:08ed48f1de7f | 81 | I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); |
AnnaBridge | 184:08ed48f1de7f | 82 | I2CName i2c_name = (I2CName)pinmap_merge(i2c_sda, i2c_scl); |
AnnaBridge | 184:08ed48f1de7f | 83 | MBED_ASSERT((int)i2c_name != NC); |
AnnaBridge | 184:08ed48f1de7f | 84 | |
AnnaBridge | 184:08ed48f1de7f | 85 | switch(i2c_name) { |
AnnaBridge | 184:08ed48f1de7f | 86 | case I2C_0: |
AnnaBridge | 184:08ed48f1de7f | 87 | CG_SetFcPeriphB(CG_FC_PERIPH_I2C0, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 88 | CG_SetFcPeriphA(CG_FC_PERIPH_PORTK, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 89 | obj->i2c = TSB_I2C0; |
AnnaBridge | 184:08ed48f1de7f | 90 | obj->index = 0; |
AnnaBridge | 184:08ed48f1de7f | 91 | obj->IRQn = INTI2C0_IRQn; |
AnnaBridge | 184:08ed48f1de7f | 92 | break; |
AnnaBridge | 184:08ed48f1de7f | 93 | case I2C_1: |
AnnaBridge | 184:08ed48f1de7f | 94 | CG_SetFcPeriphB(CG_FC_PERIPH_I2C1, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 95 | CG_SetFcPeriphA(CG_FC_PERIPH_PORTF, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 96 | obj->i2c = TSB_I2C1; |
AnnaBridge | 184:08ed48f1de7f | 97 | obj->index = 1; |
AnnaBridge | 184:08ed48f1de7f | 98 | obj->IRQn = INTI2C1_IRQn; |
AnnaBridge | 184:08ed48f1de7f | 99 | break; |
AnnaBridge | 184:08ed48f1de7f | 100 | case I2C_2: |
AnnaBridge | 184:08ed48f1de7f | 101 | CG_SetFcPeriphB(CG_FC_PERIPH_I2C2, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 102 | CG_SetFcPeriphA(CG_FC_PERIPH_PORTH, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 103 | obj->i2c = TSB_I2C2; |
AnnaBridge | 184:08ed48f1de7f | 104 | obj->index = 2; |
AnnaBridge | 184:08ed48f1de7f | 105 | obj->IRQn = INTI2C2_IRQn; |
AnnaBridge | 184:08ed48f1de7f | 106 | break; |
AnnaBridge | 184:08ed48f1de7f | 107 | default: |
AnnaBridge | 184:08ed48f1de7f | 108 | error("I2C is not available"); |
AnnaBridge | 184:08ed48f1de7f | 109 | break; |
AnnaBridge | 184:08ed48f1de7f | 110 | } |
AnnaBridge | 184:08ed48f1de7f | 111 | |
AnnaBridge | 184:08ed48f1de7f | 112 | pinmap_pinout(sda, PinMap_I2C_SDA); |
AnnaBridge | 184:08ed48f1de7f | 113 | pin_mode(sda, OpenDrain); |
AnnaBridge | 184:08ed48f1de7f | 114 | pin_mode(sda, PullUp); |
AnnaBridge | 184:08ed48f1de7f | 115 | |
AnnaBridge | 184:08ed48f1de7f | 116 | pinmap_pinout(scl, PinMap_I2C_SCL); |
AnnaBridge | 184:08ed48f1de7f | 117 | pin_mode(scl, OpenDrain); |
AnnaBridge | 184:08ed48f1de7f | 118 | pin_mode(scl, PullUp); |
AnnaBridge | 184:08ed48f1de7f | 119 | |
AnnaBridge | 184:08ed48f1de7f | 120 | i2c_reset(obj); |
AnnaBridge | 184:08ed48f1de7f | 121 | i2c_frequency(obj, 100000); |
AnnaBridge | 184:08ed48f1de7f | 122 | } |
AnnaBridge | 184:08ed48f1de7f | 123 | |
AnnaBridge | 184:08ed48f1de7f | 124 | // Configure the I2C frequency |
AnnaBridge | 184:08ed48f1de7f | 125 | void i2c_frequency(i2c_t *obj, int hz) |
AnnaBridge | 184:08ed48f1de7f | 126 | { |
AnnaBridge | 184:08ed48f1de7f | 127 | uint32_t sck = 0; |
AnnaBridge | 184:08ed48f1de7f | 128 | uint32_t tmp_sck = 0; |
AnnaBridge | 184:08ed48f1de7f | 129 | uint32_t prsck = 1; |
AnnaBridge | 184:08ed48f1de7f | 130 | uint32_t tmp_prsck = 1; |
AnnaBridge | 184:08ed48f1de7f | 131 | uint32_t fscl = 0; |
AnnaBridge | 184:08ed48f1de7f | 132 | uint32_t tmp_fscl = 0; |
AnnaBridge | 184:08ed48f1de7f | 133 | uint64_t fx; |
AnnaBridge | 184:08ed48f1de7f | 134 | |
AnnaBridge | 184:08ed48f1de7f | 135 | if (hz <= 400000) { // Maximum 400khz clock frequency supported by M46B |
AnnaBridge | 184:08ed48f1de7f | 136 | for (prsck = 1; prsck <= 32; prsck++) { |
AnnaBridge | 184:08ed48f1de7f | 137 | fx = ((uint64_t)SystemCoreClock / prsck); |
AnnaBridge | 184:08ed48f1de7f | 138 | if ((fx < 20000000U) && (fx > 6666666U)) { |
AnnaBridge | 184:08ed48f1de7f | 139 | for (sck = 0; sck <= 7; sck++) { |
AnnaBridge | 184:08ed48f1de7f | 140 | fscl = (fx / (uint64_t)I2C_SCK_DIVIDER_TBL[sck]); |
AnnaBridge | 184:08ed48f1de7f | 141 | if ((fscl <= (uint64_t)hz) && (fscl > tmp_fscl)) { |
AnnaBridge | 184:08ed48f1de7f | 142 | tmp_fscl = fscl; |
AnnaBridge | 184:08ed48f1de7f | 143 | tmp_sck = sck; |
AnnaBridge | 184:08ed48f1de7f | 144 | tmp_prsck = (prsck < 32)? prsck: 1; |
AnnaBridge | 184:08ed48f1de7f | 145 | } |
AnnaBridge | 184:08ed48f1de7f | 146 | } |
AnnaBridge | 184:08ed48f1de7f | 147 | } |
AnnaBridge | 184:08ed48f1de7f | 148 | } |
AnnaBridge | 184:08ed48f1de7f | 149 | clk.sck = (uint32_t)tmp_sck; |
AnnaBridge | 184:08ed48f1de7f | 150 | clk.prsck = (tmp_prsck < 32)? (uint32_t)tmp_prsck - 1 : 1; |
AnnaBridge | 184:08ed48f1de7f | 151 | } else { |
AnnaBridge | 184:08ed48f1de7f | 152 | clk.sck = I2C_SCK_CLK_DIV_24; |
AnnaBridge | 184:08ed48f1de7f | 153 | clk.prsck = I2C_PRESCALER_DIV_4; |
AnnaBridge | 184:08ed48f1de7f | 154 | } |
AnnaBridge | 184:08ed48f1de7f | 155 | myi2c.I2CSelfAddr = 0xE0; // Self Address |
AnnaBridge | 184:08ed48f1de7f | 156 | myi2c.I2CDataLen = I2C_DATA_LEN_8; |
AnnaBridge | 184:08ed48f1de7f | 157 | myi2c.I2CACKState = ENABLE; |
AnnaBridge | 184:08ed48f1de7f | 158 | myi2c.I2CClkDiv = clk.sck; |
AnnaBridge | 184:08ed48f1de7f | 159 | myi2c.PrescalerClkDiv = clk.prsck; |
AnnaBridge | 184:08ed48f1de7f | 160 | |
AnnaBridge | 184:08ed48f1de7f | 161 | I2C_SWReset(obj->i2c); |
AnnaBridge | 184:08ed48f1de7f | 162 | I2C_Init(obj->i2c, &myi2c); |
AnnaBridge | 184:08ed48f1de7f | 163 | NVIC_EnableIRQ(obj->IRQn); |
AnnaBridge | 184:08ed48f1de7f | 164 | I2C_SetINTReq(obj->i2c, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 165 | } |
AnnaBridge | 184:08ed48f1de7f | 166 | |
AnnaBridge | 184:08ed48f1de7f | 167 | int i2c_start(i2c_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 168 | { |
AnnaBridge | 184:08ed48f1de7f | 169 | start_flag = 1; |
AnnaBridge | 184:08ed48f1de7f | 170 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 171 | } |
AnnaBridge | 184:08ed48f1de7f | 172 | |
AnnaBridge | 184:08ed48f1de7f | 173 | int i2c_stop(i2c_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 174 | { |
AnnaBridge | 184:08ed48f1de7f | 175 | I2C_GenerateStop(obj->i2c); |
AnnaBridge | 184:08ed48f1de7f | 176 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 177 | } |
AnnaBridge | 184:08ed48f1de7f | 178 | |
AnnaBridge | 184:08ed48f1de7f | 179 | void i2c_reset(i2c_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 180 | { |
AnnaBridge | 184:08ed48f1de7f | 181 | I2C_SWReset(obj->i2c); |
AnnaBridge | 184:08ed48f1de7f | 182 | } |
AnnaBridge | 184:08ed48f1de7f | 183 | |
AnnaBridge | 184:08ed48f1de7f | 184 | static void wait_i2c_bus_free(i2c_t *obj) |
AnnaBridge | 184:08ed48f1de7f | 185 | { |
AnnaBridge | 184:08ed48f1de7f | 186 | I2C_State status; |
AnnaBridge | 184:08ed48f1de7f | 187 | |
AnnaBridge | 184:08ed48f1de7f | 188 | do { |
AnnaBridge | 184:08ed48f1de7f | 189 | status = I2C_GetState(obj->i2c); |
AnnaBridge | 184:08ed48f1de7f | 190 | } while (status.Bit.BusState); |
AnnaBridge | 184:08ed48f1de7f | 191 | } |
AnnaBridge | 184:08ed48f1de7f | 192 | |
AnnaBridge | 184:08ed48f1de7f | 193 | int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) |
AnnaBridge | 184:08ed48f1de7f | 194 | { |
AnnaBridge | 184:08ed48f1de7f | 195 | TSB_I2C_TypeDef *sbi = obj->i2c; |
AnnaBridge | 184:08ed48f1de7f | 196 | uint32_t i2c_num = 0; |
AnnaBridge | 184:08ed48f1de7f | 197 | obj->address = address; |
AnnaBridge | 184:08ed48f1de7f | 198 | |
AnnaBridge | 184:08ed48f1de7f | 199 | i2c_num = obj->index; |
AnnaBridge | 184:08ed48f1de7f | 200 | |
AnnaBridge | 184:08ed48f1de7f | 201 | // receive data |
AnnaBridge | 184:08ed48f1de7f | 202 | xfer[i2c_num].count = 0; |
AnnaBridge | 184:08ed48f1de7f | 203 | xfer[i2c_num].len = length; |
AnnaBridge | 184:08ed48f1de7f | 204 | xfer[i2c_num].buf = data; |
AnnaBridge | 184:08ed48f1de7f | 205 | |
AnnaBridge | 184:08ed48f1de7f | 206 | I2C_SetSendData(sbi, address | SBI_I2C_RECEIVE); |
AnnaBridge | 184:08ed48f1de7f | 207 | I2C_GenerateStart(sbi); |
AnnaBridge | 184:08ed48f1de7f | 208 | |
AnnaBridge | 184:08ed48f1de7f | 209 | wait_i2c_bus_free(obj); |
AnnaBridge | 184:08ed48f1de7f | 210 | return (xfer[i2c_num].count - 1); |
AnnaBridge | 184:08ed48f1de7f | 211 | } |
AnnaBridge | 184:08ed48f1de7f | 212 | |
AnnaBridge | 184:08ed48f1de7f | 213 | int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) |
AnnaBridge | 184:08ed48f1de7f | 214 | { |
AnnaBridge | 184:08ed48f1de7f | 215 | int8_t i = 0; |
AnnaBridge | 184:08ed48f1de7f | 216 | TSB_I2C_TypeDef *sbi = obj->i2c; |
AnnaBridge | 184:08ed48f1de7f | 217 | uint32_t i2c_num = 0; |
AnnaBridge | 184:08ed48f1de7f | 218 | obj->address = address; |
AnnaBridge | 184:08ed48f1de7f | 219 | |
AnnaBridge | 184:08ed48f1de7f | 220 | i2c_num = obj->index; |
AnnaBridge | 184:08ed48f1de7f | 221 | gI2C_TxData = (char *)calloc(length, sizeof(int8_t)); |
AnnaBridge | 184:08ed48f1de7f | 222 | |
AnnaBridge | 184:08ed48f1de7f | 223 | for (i = 0; i < length; i++) { |
AnnaBridge | 184:08ed48f1de7f | 224 | gI2C_TxData[i] = data[i]; |
AnnaBridge | 184:08ed48f1de7f | 225 | } |
AnnaBridge | 184:08ed48f1de7f | 226 | // receive data |
AnnaBridge | 184:08ed48f1de7f | 227 | xfer[i2c_num].count = 0; |
AnnaBridge | 184:08ed48f1de7f | 228 | xfer[i2c_num].len = length; |
AnnaBridge | 184:08ed48f1de7f | 229 | xfer[i2c_num].buf = gI2C_TxData; |
AnnaBridge | 184:08ed48f1de7f | 230 | |
AnnaBridge | 184:08ed48f1de7f | 231 | I2C_SetSendData(sbi, address | SBI_I2C_SEND); |
AnnaBridge | 184:08ed48f1de7f | 232 | I2C_GenerateStart(sbi); // Start condition |
AnnaBridge | 184:08ed48f1de7f | 233 | |
AnnaBridge | 184:08ed48f1de7f | 234 | wait_i2c_bus_free(obj); |
AnnaBridge | 184:08ed48f1de7f | 235 | free(gI2C_TxData); |
AnnaBridge | 184:08ed48f1de7f | 236 | DelayMS(8); |
AnnaBridge | 184:08ed48f1de7f | 237 | if (((xfer[i2c_num].count - 1) == 0) && (byte_func == 1)) { |
AnnaBridge | 184:08ed48f1de7f | 238 | send_byte = 1; |
AnnaBridge | 184:08ed48f1de7f | 239 | i2c_byte_write(obj, 0x00); |
AnnaBridge | 184:08ed48f1de7f | 240 | xfer[i2c_num].count = 1; |
AnnaBridge | 184:08ed48f1de7f | 241 | byte_func = 0; |
AnnaBridge | 184:08ed48f1de7f | 242 | } |
AnnaBridge | 184:08ed48f1de7f | 243 | return (xfer[i2c_num].count - 1); |
AnnaBridge | 184:08ed48f1de7f | 244 | } |
AnnaBridge | 184:08ed48f1de7f | 245 | |
AnnaBridge | 184:08ed48f1de7f | 246 | int i2c_byte_read(i2c_t *obj, int last) |
AnnaBridge | 184:08ed48f1de7f | 247 | { |
AnnaBridge | 184:08ed48f1de7f | 248 | char i2c_ret = 0; |
AnnaBridge | 184:08ed48f1de7f | 249 | i2c_read(obj, obj->address, &i2c_ret, 1, last); |
AnnaBridge | 184:08ed48f1de7f | 250 | return i2c_ret; |
AnnaBridge | 184:08ed48f1de7f | 251 | } |
AnnaBridge | 184:08ed48f1de7f | 252 | |
AnnaBridge | 184:08ed48f1de7f | 253 | int i2c_byte_write(i2c_t *obj, int data) |
AnnaBridge | 184:08ed48f1de7f | 254 | { |
AnnaBridge | 184:08ed48f1de7f | 255 | uint32_t wb = 1; |
AnnaBridge | 184:08ed48f1de7f | 256 | static size_t counter = 1; |
AnnaBridge | 184:08ed48f1de7f | 257 | |
AnnaBridge | 184:08ed48f1de7f | 258 | byte_func = 1; |
AnnaBridge | 184:08ed48f1de7f | 259 | if (start_flag == 0 && send_byte == 0) { |
AnnaBridge | 184:08ed48f1de7f | 260 | gI2C_LTxData = (char *)realloc(gI2C_LTxData, counter++); |
AnnaBridge | 184:08ed48f1de7f | 261 | gI2C_LTxData[counter - 2] = data; |
AnnaBridge | 184:08ed48f1de7f | 262 | } |
AnnaBridge | 184:08ed48f1de7f | 263 | |
AnnaBridge | 184:08ed48f1de7f | 264 | if (send_byte == 1) { |
AnnaBridge | 184:08ed48f1de7f | 265 | wb = i2c_write(obj, obj->address, gI2C_LTxData, (counter - 1), 0); |
AnnaBridge | 184:08ed48f1de7f | 266 | start_flag = 1; |
AnnaBridge | 184:08ed48f1de7f | 267 | send_byte = 0; |
AnnaBridge | 184:08ed48f1de7f | 268 | byte_func = 0; |
AnnaBridge | 184:08ed48f1de7f | 269 | counter = 1; |
AnnaBridge | 184:08ed48f1de7f | 270 | return wb; |
AnnaBridge | 184:08ed48f1de7f | 271 | } else { |
AnnaBridge | 184:08ed48f1de7f | 272 | if (start_flag == 1) { |
AnnaBridge | 184:08ed48f1de7f | 273 | obj->address = data; |
AnnaBridge | 184:08ed48f1de7f | 274 | start_flag = 0; |
AnnaBridge | 184:08ed48f1de7f | 275 | } else { |
AnnaBridge | 184:08ed48f1de7f | 276 | // Store the number of written bytes |
AnnaBridge | 184:08ed48f1de7f | 277 | wb = i2c_write(obj, obj->address, (char*)&data, 1, 0); |
AnnaBridge | 184:08ed48f1de7f | 278 | } |
AnnaBridge | 184:08ed48f1de7f | 279 | if (wb == 1) |
AnnaBridge | 184:08ed48f1de7f | 280 | return 1; |
AnnaBridge | 184:08ed48f1de7f | 281 | else |
AnnaBridge | 184:08ed48f1de7f | 282 | return 0; |
AnnaBridge | 184:08ed48f1de7f | 283 | } |
AnnaBridge | 184:08ed48f1de7f | 284 | } |
AnnaBridge | 184:08ed48f1de7f | 285 | |
AnnaBridge | 184:08ed48f1de7f | 286 | static void i2c_irq_handler(int i2c_num) |
AnnaBridge | 184:08ed48f1de7f | 287 | { |
AnnaBridge | 184:08ed48f1de7f | 288 | uint32_t tmp = 0U; |
AnnaBridge | 184:08ed48f1de7f | 289 | TSB_I2C_TypeDef *sbi = i2c_lut[i2c_num]; |
AnnaBridge | 184:08ed48f1de7f | 290 | I2C_State sbi_sr; |
AnnaBridge | 184:08ed48f1de7f | 291 | |
AnnaBridge | 184:08ed48f1de7f | 292 | sbi_sr = I2C_GetState(sbi); |
AnnaBridge | 184:08ed48f1de7f | 293 | |
AnnaBridge | 184:08ed48f1de7f | 294 | // we don't support slave mode |
AnnaBridge | 184:08ed48f1de7f | 295 | if (!sbi_sr.Bit.MasterSlave) |
AnnaBridge | 184:08ed48f1de7f | 296 | return; |
AnnaBridge | 184:08ed48f1de7f | 297 | |
AnnaBridge | 184:08ed48f1de7f | 298 | if (sbi_sr.Bit.TRx) { // Tx mode |
AnnaBridge | 184:08ed48f1de7f | 299 | if (sbi_sr.Bit.LastRxBit) { // LRB=1: the receiver requires no further data. |
AnnaBridge | 184:08ed48f1de7f | 300 | I2C_GenerateStop(sbi); |
AnnaBridge | 184:08ed48f1de7f | 301 | } else { // LRB=0: the receiver requires further data. |
AnnaBridge | 184:08ed48f1de7f | 302 | if (xfer[i2c_num].count < xfer[i2c_num].len) { |
AnnaBridge | 184:08ed48f1de7f | 303 | I2C_SetSendData(sbi, xfer[i2c_num].buf[xfer[i2c_num].count]); // Send next data |
AnnaBridge | 184:08ed48f1de7f | 304 | } else if (xfer[i2c_num].count == xfer[i2c_num].len) { // I2C data send finished. |
AnnaBridge | 184:08ed48f1de7f | 305 | I2C_GenerateStop(sbi); |
AnnaBridge | 184:08ed48f1de7f | 306 | } else { |
AnnaBridge | 184:08ed48f1de7f | 307 | // Do nothing |
AnnaBridge | 184:08ed48f1de7f | 308 | } |
AnnaBridge | 184:08ed48f1de7f | 309 | xfer[i2c_num].count++; |
AnnaBridge | 184:08ed48f1de7f | 310 | } |
AnnaBridge | 184:08ed48f1de7f | 311 | } else { // Rx Mode |
AnnaBridge | 184:08ed48f1de7f | 312 | if (xfer[i2c_num].count > xfer[i2c_num].len) { |
AnnaBridge | 184:08ed48f1de7f | 313 | I2C_GenerateStop(sbi); |
AnnaBridge | 184:08ed48f1de7f | 314 | I2C_SetACK(sbi, ENABLE); |
AnnaBridge | 184:08ed48f1de7f | 315 | } else { |
AnnaBridge | 184:08ed48f1de7f | 316 | if (xfer[i2c_num].count == xfer[i2c_num].len) { // Rx last data |
AnnaBridge | 184:08ed48f1de7f | 317 | I2C_SetBitNum(sbi, I2C_DATA_LEN_1); |
AnnaBridge | 184:08ed48f1de7f | 318 | } else if (xfer[i2c_num].count == (xfer[i2c_num].len - 1)) { // Rx the data second to last |
AnnaBridge | 184:08ed48f1de7f | 319 | // Not generate ACK for next data Rx end. |
AnnaBridge | 184:08ed48f1de7f | 320 | I2C_SetACK(sbi, DISABLE); |
AnnaBridge | 184:08ed48f1de7f | 321 | } else { |
AnnaBridge | 184:08ed48f1de7f | 322 | // Do nothing |
AnnaBridge | 184:08ed48f1de7f | 323 | } |
AnnaBridge | 184:08ed48f1de7f | 324 | tmp = I2C_GetReceiveData(sbi); |
AnnaBridge | 184:08ed48f1de7f | 325 | if (xfer[i2c_num].count > 0) { |
AnnaBridge | 184:08ed48f1de7f | 326 | xfer[i2c_num].buf[xfer[i2c_num].count - 1U] = tmp; |
AnnaBridge | 184:08ed48f1de7f | 327 | } else { |
AnnaBridge | 184:08ed48f1de7f | 328 | // first read is dummy read |
AnnaBridge | 184:08ed48f1de7f | 329 | } |
AnnaBridge | 184:08ed48f1de7f | 330 | xfer[i2c_num].count++; |
AnnaBridge | 184:08ed48f1de7f | 331 | } |
AnnaBridge | 184:08ed48f1de7f | 332 | } |
AnnaBridge | 184:08ed48f1de7f | 333 | } |
AnnaBridge | 184:08ed48f1de7f | 334 | |
AnnaBridge | 184:08ed48f1de7f | 335 | void INTI2C0_IRQHandler(void) |
AnnaBridge | 184:08ed48f1de7f | 336 | { |
AnnaBridge | 184:08ed48f1de7f | 337 | i2c_irq_handler(0); |
AnnaBridge | 184:08ed48f1de7f | 338 | } |
AnnaBridge | 184:08ed48f1de7f | 339 | |
AnnaBridge | 184:08ed48f1de7f | 340 | void INTI2C1_IRQHandler(void) |
AnnaBridge | 184:08ed48f1de7f | 341 | { |
AnnaBridge | 184:08ed48f1de7f | 342 | i2c_irq_handler(1); |
AnnaBridge | 184:08ed48f1de7f | 343 | } |
AnnaBridge | 184:08ed48f1de7f | 344 | |
AnnaBridge | 184:08ed48f1de7f | 345 | void INTI2C2_IRQHandler(void) |
AnnaBridge | 184:08ed48f1de7f | 346 | { |
AnnaBridge | 184:08ed48f1de7f | 347 | i2c_irq_handler(2); |
AnnaBridge | 184:08ed48f1de7f | 348 | } |