Dependencies: DHT
tm_stm32f4_i2c.c@1:68a123e2d60b, 2018-09-18 (annotated)
- Committer:
- binoit
- Date:
- Tue Sep 18 08:06:44 2018 +0000
- Revision:
- 1:68a123e2d60b
- Parent:
- 0:3adb31fbd547
Add_DHT22
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
binoit | 0:3adb31fbd547 | 1 | /** |
binoit | 0:3adb31fbd547 | 2 | * |---------------------------------------------------------------------- |
binoit | 0:3adb31fbd547 | 3 | * | Copyright (C) Tilen Majerle, 2014 |
binoit | 0:3adb31fbd547 | 4 | * | |
binoit | 0:3adb31fbd547 | 5 | * | This program is free software: you can redistribute it and/or modify |
binoit | 0:3adb31fbd547 | 6 | * | it under the terms of the GNU General Public License as published by |
binoit | 0:3adb31fbd547 | 7 | * | the Free Software Foundation, either version 3 of the License, or |
binoit | 0:3adb31fbd547 | 8 | * | any later version. |
binoit | 0:3adb31fbd547 | 9 | * | |
binoit | 0:3adb31fbd547 | 10 | * | This program is distributed in the hope that it will be useful, |
binoit | 0:3adb31fbd547 | 11 | * | but WITHOUT ANY WARRANTY; without even the implied warranty of |
binoit | 0:3adb31fbd547 | 12 | * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
binoit | 0:3adb31fbd547 | 13 | * | GNU General Public License for more details. |
binoit | 0:3adb31fbd547 | 14 | * | |
binoit | 0:3adb31fbd547 | 15 | * | You should have received a copy of the GNU General Public License |
binoit | 0:3adb31fbd547 | 16 | * | along with this program. If not, see <http://www.gnu.org/licenses/>. |
binoit | 0:3adb31fbd547 | 17 | * |---------------------------------------------------------------------- |
binoit | 0:3adb31fbd547 | 18 | */ |
binoit | 0:3adb31fbd547 | 19 | #include "tm_stm32f4_i2c.h" |
binoit | 0:3adb31fbd547 | 20 | |
binoit | 0:3adb31fbd547 | 21 | /* Private variables */ |
binoit | 0:3adb31fbd547 | 22 | static uint32_t TM_I2C_Timeout; |
binoit | 0:3adb31fbd547 | 23 | static uint32_t TM_I2C_INT_Clocks[3] = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF}; |
binoit | 0:3adb31fbd547 | 24 | |
binoit | 0:3adb31fbd547 | 25 | /* Private defines */ |
binoit | 0:3adb31fbd547 | 26 | #define I2C_TRANSMITTER_MODE 0 |
binoit | 0:3adb31fbd547 | 27 | #define I2C_RECEIVER_MODE 1 |
binoit | 0:3adb31fbd547 | 28 | #define I2C_ACK_ENABLE 1 |
binoit | 0:3adb31fbd547 | 29 | #define I2C_ACK_DISABLE 0 |
binoit | 0:3adb31fbd547 | 30 | |
binoit | 0:3adb31fbd547 | 31 | /* Private functions */ |
binoit | 0:3adb31fbd547 | 32 | static void TM_I2C1_INT_InitPins(TM_I2C_PinsPack_t pinspack); |
binoit | 0:3adb31fbd547 | 33 | static void TM_I2C2_INT_InitPins(TM_I2C_PinsPack_t pinspack); |
binoit | 0:3adb31fbd547 | 34 | static void TM_I2C3_INT_InitPins(TM_I2C_PinsPack_t pinspack); |
binoit | 0:3adb31fbd547 | 35 | |
binoit | 0:3adb31fbd547 | 36 | void TM_I2C_Init(I2C_TypeDef* I2Cx, TM_I2C_PinsPack_t pinspack, uint32_t clockSpeed) { |
binoit | 0:3adb31fbd547 | 37 | I2C_InitTypeDef I2C_InitStruct; |
binoit | 0:3adb31fbd547 | 38 | |
binoit | 0:3adb31fbd547 | 39 | if (I2Cx == I2C1) { |
binoit | 0:3adb31fbd547 | 40 | /* Enable clock */ |
binoit | 0:3adb31fbd547 | 41 | RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; |
binoit | 0:3adb31fbd547 | 42 | |
binoit | 0:3adb31fbd547 | 43 | /* Enable pins */ |
binoit | 0:3adb31fbd547 | 44 | TM_I2C1_INT_InitPins(pinspack); |
binoit | 0:3adb31fbd547 | 45 | |
binoit | 0:3adb31fbd547 | 46 | /* Check clock, set the lowest clock your devices support on the same I2C but */ |
binoit | 0:3adb31fbd547 | 47 | if (clockSpeed < TM_I2C_INT_Clocks[0]) { |
binoit | 0:3adb31fbd547 | 48 | TM_I2C_INT_Clocks[0] = clockSpeed; |
binoit | 0:3adb31fbd547 | 49 | } |
binoit | 0:3adb31fbd547 | 50 | |
binoit | 0:3adb31fbd547 | 51 | /* Set values */ |
binoit | 0:3adb31fbd547 | 52 | I2C_InitStruct.I2C_ClockSpeed = TM_I2C_INT_Clocks[0]; |
binoit | 0:3adb31fbd547 | 53 | I2C_InitStruct.I2C_AcknowledgedAddress = TM_I2C1_ACKNOWLEDGED_ADDRESS; |
binoit | 0:3adb31fbd547 | 54 | I2C_InitStruct.I2C_Mode = TM_I2C1_MODE; |
binoit | 0:3adb31fbd547 | 55 | I2C_InitStruct.I2C_OwnAddress1 = TM_I2C1_OWN_ADDRESS; |
binoit | 0:3adb31fbd547 | 56 | I2C_InitStruct.I2C_Ack = TM_I2C1_ACK; |
binoit | 0:3adb31fbd547 | 57 | I2C_InitStruct.I2C_DutyCycle = TM_I2C1_DUTY_CYCLE; |
binoit | 0:3adb31fbd547 | 58 | } else if (I2Cx == I2C2) { |
binoit | 0:3adb31fbd547 | 59 | /* Enable clock */ |
binoit | 0:3adb31fbd547 | 60 | RCC->APB1ENR |= RCC_APB1ENR_I2C2EN; |
binoit | 0:3adb31fbd547 | 61 | |
binoit | 0:3adb31fbd547 | 62 | /* Enable pins */ |
binoit | 0:3adb31fbd547 | 63 | TM_I2C2_INT_InitPins(pinspack); |
binoit | 0:3adb31fbd547 | 64 | |
binoit | 0:3adb31fbd547 | 65 | /* Check clock, set the lowest clock your devices support on the same I2C but */ |
binoit | 0:3adb31fbd547 | 66 | if (clockSpeed < TM_I2C_INT_Clocks[1]) { |
binoit | 0:3adb31fbd547 | 67 | TM_I2C_INT_Clocks[1] = clockSpeed; |
binoit | 0:3adb31fbd547 | 68 | } |
binoit | 0:3adb31fbd547 | 69 | |
binoit | 0:3adb31fbd547 | 70 | /* Set values */ |
binoit | 0:3adb31fbd547 | 71 | I2C_InitStruct.I2C_ClockSpeed = TM_I2C_INT_Clocks[1]; |
binoit | 0:3adb31fbd547 | 72 | I2C_InitStruct.I2C_AcknowledgedAddress = TM_I2C2_ACKNOWLEDGED_ADDRESS; |
binoit | 0:3adb31fbd547 | 73 | I2C_InitStruct.I2C_Mode = TM_I2C2_MODE; |
binoit | 0:3adb31fbd547 | 74 | I2C_InitStruct.I2C_OwnAddress1 = TM_I2C2_OWN_ADDRESS; |
binoit | 0:3adb31fbd547 | 75 | I2C_InitStruct.I2C_Ack = TM_I2C2_ACK; |
binoit | 0:3adb31fbd547 | 76 | I2C_InitStruct.I2C_DutyCycle = TM_I2C2_DUTY_CYCLE; |
binoit | 0:3adb31fbd547 | 77 | } else if (I2Cx == I2C3) { |
binoit | 0:3adb31fbd547 | 78 | /* Enable clock */ |
binoit | 0:3adb31fbd547 | 79 | RCC->APB1ENR |= RCC_APB1ENR_I2C3EN; |
binoit | 0:3adb31fbd547 | 80 | |
binoit | 0:3adb31fbd547 | 81 | /* Enable pins */ |
binoit | 0:3adb31fbd547 | 82 | TM_I2C3_INT_InitPins(pinspack); |
binoit | 0:3adb31fbd547 | 83 | |
binoit | 0:3adb31fbd547 | 84 | /* Check clock, set the lowest clock your devices support on the same I2C but */ |
binoit | 0:3adb31fbd547 | 85 | if (clockSpeed < TM_I2C_INT_Clocks[2]) { |
binoit | 0:3adb31fbd547 | 86 | TM_I2C_INT_Clocks[2] = clockSpeed; |
binoit | 0:3adb31fbd547 | 87 | } |
binoit | 0:3adb31fbd547 | 88 | |
binoit | 0:3adb31fbd547 | 89 | /* Set values */ |
binoit | 0:3adb31fbd547 | 90 | I2C_InitStruct.I2C_ClockSpeed = TM_I2C_INT_Clocks[2]; |
binoit | 0:3adb31fbd547 | 91 | I2C_InitStruct.I2C_AcknowledgedAddress = TM_I2C3_ACKNOWLEDGED_ADDRESS; |
binoit | 0:3adb31fbd547 | 92 | I2C_InitStruct.I2C_Mode = TM_I2C3_MODE; |
binoit | 0:3adb31fbd547 | 93 | I2C_InitStruct.I2C_OwnAddress1 = TM_I2C3_OWN_ADDRESS; |
binoit | 0:3adb31fbd547 | 94 | I2C_InitStruct.I2C_Ack = TM_I2C3_ACK; |
binoit | 0:3adb31fbd547 | 95 | I2C_InitStruct.I2C_DutyCycle = TM_I2C3_DUTY_CYCLE; |
binoit | 0:3adb31fbd547 | 96 | } |
binoit | 0:3adb31fbd547 | 97 | |
binoit | 0:3adb31fbd547 | 98 | /* Disable I2C first */ |
binoit | 0:3adb31fbd547 | 99 | I2Cx->CR1 &= ~I2C_CR1_PE; |
binoit | 0:3adb31fbd547 | 100 | |
binoit | 0:3adb31fbd547 | 101 | /* Initialize I2C */ |
binoit | 0:3adb31fbd547 | 102 | I2C_Init(I2Cx, &I2C_InitStruct); |
binoit | 0:3adb31fbd547 | 103 | |
binoit | 0:3adb31fbd547 | 104 | /* Enable I2C */ |
binoit | 0:3adb31fbd547 | 105 | I2Cx->CR1 |= I2C_CR1_PE; |
binoit | 0:3adb31fbd547 | 106 | } |
binoit | 0:3adb31fbd547 | 107 | |
binoit | 0:3adb31fbd547 | 108 | uint8_t TM_I2C_Read(I2C_TypeDef* I2Cx, uint8_t address, uint8_t reg) { |
binoit | 0:3adb31fbd547 | 109 | uint8_t received_data; |
binoit | 0:3adb31fbd547 | 110 | TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_DISABLE); |
binoit | 0:3adb31fbd547 | 111 | TM_I2C_WriteData(I2Cx, reg); |
binoit | 0:3adb31fbd547 | 112 | TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 113 | TM_I2C_Start(I2Cx, address, I2C_RECEIVER_MODE, I2C_ACK_DISABLE); |
binoit | 0:3adb31fbd547 | 114 | received_data = TM_I2C_ReadNack(I2Cx); |
binoit | 0:3adb31fbd547 | 115 | return received_data; |
binoit | 0:3adb31fbd547 | 116 | } |
binoit | 0:3adb31fbd547 | 117 | |
binoit | 0:3adb31fbd547 | 118 | void TM_I2C_ReadMulti(I2C_TypeDef* I2Cx, uint8_t address, uint8_t reg, uint8_t* data, uint16_t count) { |
binoit | 0:3adb31fbd547 | 119 | TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_ENABLE); |
binoit | 0:3adb31fbd547 | 120 | TM_I2C_WriteData(I2Cx, reg); |
binoit | 0:3adb31fbd547 | 121 | //TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 122 | TM_I2C_Start(I2Cx, address, I2C_RECEIVER_MODE, I2C_ACK_ENABLE); |
binoit | 0:3adb31fbd547 | 123 | while (count--) { |
binoit | 0:3adb31fbd547 | 124 | if (!count) { |
binoit | 0:3adb31fbd547 | 125 | /* Last byte */ |
binoit | 0:3adb31fbd547 | 126 | *data++ = TM_I2C_ReadNack(I2Cx); |
binoit | 0:3adb31fbd547 | 127 | } else { |
binoit | 0:3adb31fbd547 | 128 | *data++ = TM_I2C_ReadAck(I2Cx); |
binoit | 0:3adb31fbd547 | 129 | } |
binoit | 0:3adb31fbd547 | 130 | } |
binoit | 0:3adb31fbd547 | 131 | } |
binoit | 0:3adb31fbd547 | 132 | |
binoit | 0:3adb31fbd547 | 133 | uint8_t TM_I2C_ReadNoRegister(I2C_TypeDef* I2Cx, uint8_t address) { |
binoit | 0:3adb31fbd547 | 134 | uint8_t data; |
binoit | 0:3adb31fbd547 | 135 | TM_I2C_Start(I2Cx, address, I2C_RECEIVER_MODE, I2C_ACK_ENABLE); |
binoit | 0:3adb31fbd547 | 136 | /* Also stop condition happens */ |
binoit | 0:3adb31fbd547 | 137 | data = TM_I2C_ReadNack(I2Cx); |
binoit | 0:3adb31fbd547 | 138 | return data; |
binoit | 0:3adb31fbd547 | 139 | } |
binoit | 0:3adb31fbd547 | 140 | |
binoit | 0:3adb31fbd547 | 141 | void TM_I2C_ReadMultiNoRegister(I2C_TypeDef* I2Cx, uint8_t address, uint8_t* data, uint16_t count) { |
binoit | 0:3adb31fbd547 | 142 | TM_I2C_Start(I2Cx, address, I2C_RECEIVER_MODE, I2C_ACK_ENABLE); |
binoit | 0:3adb31fbd547 | 143 | while (count--) { |
binoit | 0:3adb31fbd547 | 144 | if (!count) { |
binoit | 0:3adb31fbd547 | 145 | /* Last byte */ |
binoit | 0:3adb31fbd547 | 146 | *data = TM_I2C_ReadNack(I2Cx); |
binoit | 0:3adb31fbd547 | 147 | } else { |
binoit | 0:3adb31fbd547 | 148 | *data = TM_I2C_ReadAck(I2Cx); |
binoit | 0:3adb31fbd547 | 149 | } |
binoit | 0:3adb31fbd547 | 150 | } |
binoit | 0:3adb31fbd547 | 151 | } |
binoit | 0:3adb31fbd547 | 152 | |
binoit | 0:3adb31fbd547 | 153 | void TM_I2C_Write(I2C_TypeDef* I2Cx, uint8_t address, uint8_t reg, uint8_t data) { |
binoit | 0:3adb31fbd547 | 154 | TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_DISABLE); |
binoit | 0:3adb31fbd547 | 155 | TM_I2C_WriteData(I2Cx, reg); |
binoit | 0:3adb31fbd547 | 156 | TM_I2C_WriteData(I2Cx, data); |
binoit | 0:3adb31fbd547 | 157 | TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 158 | } |
binoit | 0:3adb31fbd547 | 159 | |
binoit | 0:3adb31fbd547 | 160 | void TM_I2C_WriteMulti(I2C_TypeDef* I2Cx, uint8_t address, uint8_t reg, uint8_t* data, uint16_t count) { |
binoit | 0:3adb31fbd547 | 161 | TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_DISABLE); |
binoit | 0:3adb31fbd547 | 162 | TM_I2C_WriteData(I2Cx, reg); |
binoit | 0:3adb31fbd547 | 163 | while (count--) { |
binoit | 0:3adb31fbd547 | 164 | TM_I2C_WriteData(I2Cx, *data++); |
binoit | 0:3adb31fbd547 | 165 | } |
binoit | 0:3adb31fbd547 | 166 | TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 167 | } |
binoit | 0:3adb31fbd547 | 168 | |
binoit | 0:3adb31fbd547 | 169 | void TM_I2C_WriteNoRegister(I2C_TypeDef* I2Cx, uint8_t address, uint8_t data) { |
binoit | 0:3adb31fbd547 | 170 | TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_DISABLE); |
binoit | 0:3adb31fbd547 | 171 | TM_I2C_WriteData(I2Cx, data); |
binoit | 0:3adb31fbd547 | 172 | TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 173 | } |
binoit | 0:3adb31fbd547 | 174 | |
binoit | 0:3adb31fbd547 | 175 | void TM_I2C_WriteMultiNoRegister(I2C_TypeDef* I2Cx, uint8_t address, uint8_t* data, uint16_t count) { |
binoit | 0:3adb31fbd547 | 176 | TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_DISABLE); |
binoit | 0:3adb31fbd547 | 177 | while (count--) { |
binoit | 0:3adb31fbd547 | 178 | TM_I2C_WriteData(I2Cx, *data++); |
binoit | 0:3adb31fbd547 | 179 | } |
binoit | 0:3adb31fbd547 | 180 | TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 181 | } |
binoit | 0:3adb31fbd547 | 182 | |
binoit | 0:3adb31fbd547 | 183 | |
binoit | 0:3adb31fbd547 | 184 | uint8_t TM_I2C_IsDeviceConnected(I2C_TypeDef* I2Cx, uint8_t address) { |
binoit | 0:3adb31fbd547 | 185 | uint8_t connected = 0; |
binoit | 0:3adb31fbd547 | 186 | /* Try to start, function will return 0 in case device will send ACK */ |
binoit | 0:3adb31fbd547 | 187 | if (!TM_I2C_Start(I2Cx, address, I2C_TRANSMITTER_MODE, I2C_ACK_ENABLE)) { |
binoit | 0:3adb31fbd547 | 188 | connected = 1; |
binoit | 0:3adb31fbd547 | 189 | } |
binoit | 0:3adb31fbd547 | 190 | |
binoit | 0:3adb31fbd547 | 191 | /* STOP I2C */ |
binoit | 0:3adb31fbd547 | 192 | TM_I2C_Stop(I2Cx); |
binoit | 0:3adb31fbd547 | 193 | |
binoit | 0:3adb31fbd547 | 194 | /* Return status */ |
binoit | 0:3adb31fbd547 | 195 | return connected; |
binoit | 0:3adb31fbd547 | 196 | } |
binoit | 0:3adb31fbd547 | 197 | |
binoit | 0:3adb31fbd547 | 198 | __weak void TM_I2C_InitCustomPinsCallback(I2C_TypeDef* I2Cx, uint16_t AlternateFunction) { |
binoit | 0:3adb31fbd547 | 199 | /* Custom user function. */ |
binoit | 0:3adb31fbd547 | 200 | /* In case user needs functionality for custom pins, this function should be declared outside this library */ |
binoit | 0:3adb31fbd547 | 201 | } |
binoit | 0:3adb31fbd547 | 202 | |
binoit | 0:3adb31fbd547 | 203 | /* Private functions */ |
binoit | 0:3adb31fbd547 | 204 | int16_t TM_I2C_Start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction, uint8_t ack) { |
binoit | 0:3adb31fbd547 | 205 | /* Generate I2C start pulse */ |
binoit | 0:3adb31fbd547 | 206 | I2Cx->CR1 |= I2C_CR1_START; |
binoit | 0:3adb31fbd547 | 207 | |
binoit | 0:3adb31fbd547 | 208 | /* Wait till I2C is busy */ |
binoit | 0:3adb31fbd547 | 209 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 210 | while (!(I2Cx->SR1 & I2C_SR1_SB)) { |
binoit | 0:3adb31fbd547 | 211 | if (--TM_I2C_Timeout == 0x00) { |
binoit | 0:3adb31fbd547 | 212 | return 1; |
binoit | 0:3adb31fbd547 | 213 | } |
binoit | 0:3adb31fbd547 | 214 | } |
binoit | 0:3adb31fbd547 | 215 | |
binoit | 0:3adb31fbd547 | 216 | /* Enable ack if we select it */ |
binoit | 0:3adb31fbd547 | 217 | if (ack) { |
binoit | 0:3adb31fbd547 | 218 | I2Cx->CR1 |= I2C_CR1_ACK; |
binoit | 0:3adb31fbd547 | 219 | } |
binoit | 0:3adb31fbd547 | 220 | |
binoit | 0:3adb31fbd547 | 221 | /* Send write/read bit */ |
binoit | 0:3adb31fbd547 | 222 | if (direction == I2C_TRANSMITTER_MODE) { |
binoit | 0:3adb31fbd547 | 223 | /* Send address with zero last bit */ |
binoit | 0:3adb31fbd547 | 224 | I2Cx->DR = address & ~I2C_OAR1_ADD0; |
binoit | 0:3adb31fbd547 | 225 | |
binoit | 0:3adb31fbd547 | 226 | /* Wait till finished */ |
binoit | 0:3adb31fbd547 | 227 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 228 | while (!(I2Cx->SR1 & I2C_SR1_ADDR)) { |
binoit | 0:3adb31fbd547 | 229 | if (--TM_I2C_Timeout == 0x00) { |
binoit | 0:3adb31fbd547 | 230 | return 1; |
binoit | 0:3adb31fbd547 | 231 | } |
binoit | 0:3adb31fbd547 | 232 | } |
binoit | 0:3adb31fbd547 | 233 | } |
binoit | 0:3adb31fbd547 | 234 | if (direction == I2C_RECEIVER_MODE) { |
binoit | 0:3adb31fbd547 | 235 | /* Send address with 1 last bit */ |
binoit | 0:3adb31fbd547 | 236 | I2Cx->DR = address | I2C_OAR1_ADD0; |
binoit | 0:3adb31fbd547 | 237 | |
binoit | 0:3adb31fbd547 | 238 | /* Wait till finished */ |
binoit | 0:3adb31fbd547 | 239 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 240 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) { |
binoit | 0:3adb31fbd547 | 241 | if (--TM_I2C_Timeout == 0x00) { |
binoit | 0:3adb31fbd547 | 242 | return 1; |
binoit | 0:3adb31fbd547 | 243 | } |
binoit | 0:3adb31fbd547 | 244 | } |
binoit | 0:3adb31fbd547 | 245 | } |
binoit | 0:3adb31fbd547 | 246 | |
binoit | 0:3adb31fbd547 | 247 | /* Read status register to clear ADDR flag */ |
binoit | 0:3adb31fbd547 | 248 | I2Cx->SR2; |
binoit | 0:3adb31fbd547 | 249 | |
binoit | 0:3adb31fbd547 | 250 | /* Return 0, everything ok */ |
binoit | 0:3adb31fbd547 | 251 | return 0; |
binoit | 0:3adb31fbd547 | 252 | } |
binoit | 0:3adb31fbd547 | 253 | |
binoit | 0:3adb31fbd547 | 254 | void TM_I2C_WriteData(I2C_TypeDef* I2Cx, uint8_t data) { |
binoit | 0:3adb31fbd547 | 255 | /* Wait till I2C is not busy anymore */ |
binoit | 0:3adb31fbd547 | 256 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 257 | while (!(I2Cx->SR1 & I2C_SR1_TXE) && TM_I2C_Timeout) { |
binoit | 0:3adb31fbd547 | 258 | TM_I2C_Timeout--; |
binoit | 0:3adb31fbd547 | 259 | } |
binoit | 0:3adb31fbd547 | 260 | |
binoit | 0:3adb31fbd547 | 261 | /* Send I2C data */ |
binoit | 0:3adb31fbd547 | 262 | I2Cx->DR = data; |
binoit | 0:3adb31fbd547 | 263 | } |
binoit | 0:3adb31fbd547 | 264 | |
binoit | 0:3adb31fbd547 | 265 | uint8_t TM_I2C_ReadAck(I2C_TypeDef* I2Cx) { |
binoit | 0:3adb31fbd547 | 266 | uint8_t data; |
binoit | 0:3adb31fbd547 | 267 | |
binoit | 0:3adb31fbd547 | 268 | /* Enable ACK */ |
binoit | 0:3adb31fbd547 | 269 | I2Cx->CR1 |= I2C_CR1_ACK; |
binoit | 0:3adb31fbd547 | 270 | |
binoit | 0:3adb31fbd547 | 271 | /* Wait till not received */ |
binoit | 0:3adb31fbd547 | 272 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 273 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) { |
binoit | 0:3adb31fbd547 | 274 | if (--TM_I2C_Timeout == 0x00) { |
binoit | 0:3adb31fbd547 | 275 | return 1; |
binoit | 0:3adb31fbd547 | 276 | } |
binoit | 0:3adb31fbd547 | 277 | } |
binoit | 0:3adb31fbd547 | 278 | |
binoit | 0:3adb31fbd547 | 279 | /* Read data */ |
binoit | 0:3adb31fbd547 | 280 | data = I2Cx->DR; |
binoit | 0:3adb31fbd547 | 281 | |
binoit | 0:3adb31fbd547 | 282 | /* Return data */ |
binoit | 0:3adb31fbd547 | 283 | return data; |
binoit | 0:3adb31fbd547 | 284 | } |
binoit | 0:3adb31fbd547 | 285 | |
binoit | 0:3adb31fbd547 | 286 | uint8_t TM_I2C_ReadNack(I2C_TypeDef* I2Cx) { |
binoit | 0:3adb31fbd547 | 287 | uint8_t data; |
binoit | 0:3adb31fbd547 | 288 | |
binoit | 0:3adb31fbd547 | 289 | /* Disable ACK */ |
binoit | 0:3adb31fbd547 | 290 | I2Cx->CR1 &= ~I2C_CR1_ACK; |
binoit | 0:3adb31fbd547 | 291 | |
binoit | 0:3adb31fbd547 | 292 | /* Generate stop */ |
binoit | 0:3adb31fbd547 | 293 | I2Cx->CR1 |= I2C_CR1_STOP; |
binoit | 0:3adb31fbd547 | 294 | |
binoit | 0:3adb31fbd547 | 295 | /* Wait till received */ |
binoit | 0:3adb31fbd547 | 296 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 297 | while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)) { |
binoit | 0:3adb31fbd547 | 298 | if (--TM_I2C_Timeout == 0x00) { |
binoit | 0:3adb31fbd547 | 299 | return 1; |
binoit | 0:3adb31fbd547 | 300 | } |
binoit | 0:3adb31fbd547 | 301 | } |
binoit | 0:3adb31fbd547 | 302 | |
binoit | 0:3adb31fbd547 | 303 | /* Read data */ |
binoit | 0:3adb31fbd547 | 304 | data = I2Cx->DR; |
binoit | 0:3adb31fbd547 | 305 | |
binoit | 0:3adb31fbd547 | 306 | /* Return data */ |
binoit | 0:3adb31fbd547 | 307 | return data; |
binoit | 0:3adb31fbd547 | 308 | } |
binoit | 0:3adb31fbd547 | 309 | |
binoit | 0:3adb31fbd547 | 310 | uint8_t TM_I2C_Stop(I2C_TypeDef* I2Cx) { |
binoit | 0:3adb31fbd547 | 311 | /* Wait till transmitter not empty */ |
binoit | 0:3adb31fbd547 | 312 | TM_I2C_Timeout = TM_I2C_TIMEOUT; |
binoit | 0:3adb31fbd547 | 313 | while (((!(I2Cx->SR1 & I2C_SR1_TXE)) || (!(I2Cx->SR1 & I2C_SR1_BTF)))) { |
binoit | 0:3adb31fbd547 | 314 | if (--TM_I2C_Timeout == 0x00) { |
binoit | 0:3adb31fbd547 | 315 | return 1; |
binoit | 0:3adb31fbd547 | 316 | } |
binoit | 0:3adb31fbd547 | 317 | } |
binoit | 0:3adb31fbd547 | 318 | |
binoit | 0:3adb31fbd547 | 319 | /* Generate stop */ |
binoit | 0:3adb31fbd547 | 320 | I2Cx->CR1 |= I2C_CR1_STOP; |
binoit | 0:3adb31fbd547 | 321 | |
binoit | 0:3adb31fbd547 | 322 | /* Return 0, everything ok */ |
binoit | 0:3adb31fbd547 | 323 | return 0; |
binoit | 0:3adb31fbd547 | 324 | } |
binoit | 0:3adb31fbd547 | 325 | |
binoit | 0:3adb31fbd547 | 326 | |
binoit | 0:3adb31fbd547 | 327 | |
binoit | 0:3adb31fbd547 | 328 | |
binoit | 0:3adb31fbd547 | 329 | /* Private functions */ |
binoit | 0:3adb31fbd547 | 330 | static void TM_I2C1_INT_InitPins(TM_I2C_PinsPack_t pinspack) { |
binoit | 0:3adb31fbd547 | 331 | /* Init pins */ |
binoit | 0:3adb31fbd547 | 332 | #if defined(GPIOB) |
binoit | 0:3adb31fbd547 | 333 | if (pinspack == TM_I2C_PinsPack_1) { |
binoit | 0:3adb31fbd547 | 334 | TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_6 | GPIO_PIN_7, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C1); |
binoit | 0:3adb31fbd547 | 335 | } |
binoit | 0:3adb31fbd547 | 336 | #endif |
binoit | 0:3adb31fbd547 | 337 | #if defined(GPIOB) |
binoit | 0:3adb31fbd547 | 338 | if (pinspack == TM_I2C_PinsPack_2) { |
binoit | 0:3adb31fbd547 | 339 | TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_8 | GPIO_PIN_9, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C1); |
binoit | 0:3adb31fbd547 | 340 | } |
binoit | 0:3adb31fbd547 | 341 | #endif |
binoit | 0:3adb31fbd547 | 342 | #if defined(GPIOB) |
binoit | 0:3adb31fbd547 | 343 | if (pinspack == TM_I2C_PinsPack_3) { |
binoit | 0:3adb31fbd547 | 344 | TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_6 | GPIO_PIN_9, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C1); |
binoit | 0:3adb31fbd547 | 345 | } |
binoit | 0:3adb31fbd547 | 346 | #endif |
binoit | 0:3adb31fbd547 | 347 | if (pinspack == TM_I2C_PinsPack_Custom) { |
binoit | 0:3adb31fbd547 | 348 | /* Init custom pins, callback function */ |
binoit | 0:3adb31fbd547 | 349 | TM_I2C_InitCustomPinsCallback(I2C1, GPIO_AF_I2C1); |
binoit | 0:3adb31fbd547 | 350 | } |
binoit | 0:3adb31fbd547 | 351 | } |
binoit | 0:3adb31fbd547 | 352 | |
binoit | 0:3adb31fbd547 | 353 | static void TM_I2C2_INT_InitPins(TM_I2C_PinsPack_t pinspack) { |
binoit | 0:3adb31fbd547 | 354 | /* Init pins */ |
binoit | 0:3adb31fbd547 | 355 | #if defined(GPIOB) |
binoit | 0:3adb31fbd547 | 356 | if (pinspack == TM_I2C_PinsPack_1) { |
binoit | 0:3adb31fbd547 | 357 | TM_GPIO_InitAlternate(GPIOB, GPIO_PIN_10 | GPIO_PIN_11, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C2); |
binoit | 0:3adb31fbd547 | 358 | } |
binoit | 0:3adb31fbd547 | 359 | #endif |
binoit | 0:3adb31fbd547 | 360 | #if defined(GPIOF) |
binoit | 0:3adb31fbd547 | 361 | if (pinspack == TM_I2C_PinsPack_2) { |
binoit | 0:3adb31fbd547 | 362 | TM_GPIO_InitAlternate(GPIOF, GPIO_PIN_0 | GPIO_PIN_1, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C2); |
binoit | 0:3adb31fbd547 | 363 | } |
binoit | 0:3adb31fbd547 | 364 | #endif |
binoit | 0:3adb31fbd547 | 365 | #if defined(GPIOH) |
binoit | 0:3adb31fbd547 | 366 | if (pinspack == TM_I2C_PinsPack_3) { |
binoit | 0:3adb31fbd547 | 367 | TM_GPIO_InitAlternate(GPIOH, GPIO_PIN_4 | GPIO_PIN_5, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C2); |
binoit | 0:3adb31fbd547 | 368 | } |
binoit | 0:3adb31fbd547 | 369 | #endif |
binoit | 0:3adb31fbd547 | 370 | if (pinspack == TM_I2C_PinsPack_Custom) { |
binoit | 0:3adb31fbd547 | 371 | /* Init custom pins, callback function */ |
binoit | 0:3adb31fbd547 | 372 | TM_I2C_InitCustomPinsCallback(I2C2, GPIO_AF_I2C2); |
binoit | 0:3adb31fbd547 | 373 | } |
binoit | 0:3adb31fbd547 | 374 | } |
binoit | 0:3adb31fbd547 | 375 | |
binoit | 0:3adb31fbd547 | 376 | static void TM_I2C3_INT_InitPins(TM_I2C_PinsPack_t pinspack) { |
binoit | 0:3adb31fbd547 | 377 | /* Init pins */ |
binoit | 0:3adb31fbd547 | 378 | #if defined(GPIOA) && defined(GPIOC) |
binoit | 0:3adb31fbd547 | 379 | if (pinspack == TM_I2C_PinsPack_1) { |
binoit | 0:3adb31fbd547 | 380 | TM_GPIO_InitAlternate(GPIOA, GPIO_PIN_8, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C3); |
binoit | 0:3adb31fbd547 | 381 | TM_GPIO_InitAlternate(GPIOC, GPIO_PIN_9, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C3); |
binoit | 0:3adb31fbd547 | 382 | } |
binoit | 0:3adb31fbd547 | 383 | #endif |
binoit | 0:3adb31fbd547 | 384 | #if defined(GPIOH) |
binoit | 0:3adb31fbd547 | 385 | if (pinspack == TM_I2C_PinsPack_2) { |
binoit | 0:3adb31fbd547 | 386 | TM_GPIO_InitAlternate(GPIOH, GPIO_PIN_7 | GPIO_PIN_8, TM_GPIO_OType_OD, TM_GPIO_PuPd_UP, TM_GPIO_Speed_Medium, GPIO_AF_I2C3); |
binoit | 0:3adb31fbd547 | 387 | } |
binoit | 0:3adb31fbd547 | 388 | #endif |
binoit | 0:3adb31fbd547 | 389 | if (pinspack == TM_I2C_PinsPack_Custom) { |
binoit | 0:3adb31fbd547 | 390 | /* Init custom pins, callback function */ |
binoit | 0:3adb31fbd547 | 391 | TM_I2C_InitCustomPinsCallback(I2C3, GPIO_AF_I2C3); |
binoit | 0:3adb31fbd547 | 392 | } |
binoit | 0:3adb31fbd547 | 393 | } |