1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
| #include "hal_i2c.h"
void HAL_I2C_Init(I2C_Device i2c_dev, uint32_t speed_kHz) { I2C_InitTypeDef I2C_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; I2C_TypeDef* I2Cx; uint32_t RCC_APB1Periph_I2Cx; uint32_t RCC_APB2Periph_GPIO_I2Cx; GPIO_TypeDef* GPIO_I2Cx; uint16_t GPIO_Pin_SCL, GPIO_Pin_SDA;
switch (i2c_dev) { case I2C1_DEV: I2Cx = I2C1; RCC_APB1Periph_I2Cx = RCC_APB1Periph_I2C1; RCC_APB2Periph_GPIO_I2Cx = RCC_APB2Periph_GPIOB; GPIO_I2Cx = GPIOB; GPIO_Pin_SCL = GPIO_Pin_6; GPIO_Pin_SDA = GPIO_Pin_7; break; case I2C2_DEV: I2Cx = I2C2; RCC_APB1Periph_I2Cx = RCC_APB1Periph_I2C2; RCC_APB2Periph_GPIO_I2Cx = RCC_APB2Periph_GPIOB; GPIO_I2Cx = GPIOB; GPIO_Pin_SCL = GPIO_Pin_10; GPIO_Pin_SDA = GPIO_Pin_11; break; default: return; }
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2Cx, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_I2Cx, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_SCL | GPIO_Pin_SDA; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIO_I2Cx, &GPIO_InitStructure);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0xA0; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = speed_kHz * 1000;
I2C_Init(I2Cx, &I2C_InitStructure); I2C_Cmd(I2Cx, ENABLE); }
uint8_t HAL_I2C_WriteByte(I2C_Device i2c_dev, uint8_t slave_addr, uint8_t reg_addr, uint8_t data) { I2C_TypeDef* I2Cx; switch (i2c_dev) { case I2C1_DEV: I2Cx = I2C1; break; case I2C2_DEV: I2Cx = I2C2; break; default: return 1; }
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr << 1, I2C_Direction_Transmitter); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2Cx, reg_addr); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTING));
I2C_SendData(I2Cx, data); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2Cx, ENABLE);
return 0; }
uint8_t HAL_I2C_ReadByte(I2C_Device i2c_dev, uint8_t slave_addr, uint8_t reg_addr, uint8_t *data) { I2C_TypeDef* I2Cx; switch (i2c_dev) { case I2C1_DEV: I2Cx = I2C1; break; case I2C2_DEV: I2Cx = I2C2; break; default: return 1; }
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr << 1, I2C_Direction_Transmitter); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2Cx, reg_addr); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr << 1, I2C_Direction_Receiver); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2Cx, DISABLE); (void) I2Cx->SR2; I2C_GenerateSTOP(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); *data = I2C_ReceiveData(I2Cx); I2C_AcknowledgeConfig(I2Cx, ENABLE);
return 0; }
uint8_t HAL_I2C_ReadBytes(I2C_Device i2c_dev, uint8_t slave_addr, uint8_t reg_addr, uint8_t *data, uint16_t count) { I2C_TypeDef* I2Cx; switch (i2c_dev) { case I2C1_DEV: I2Cx = I2C1; break; case I2C2_DEV: I2Cx = I2C2; break; default: return 1; }
while (I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr << 1, I2C_Direction_Transmitter); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2Cx, reg_addr); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2Cx, ENABLE); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2Cx, slave_addr << 1, I2C_Direction_Receiver); while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
for (uint16_t i = 0; i < count; i++) { if (i == count - 1) { I2C_AcknowledgeConfig(I2Cx, DISABLE); I2C_GenerateSTOP(I2Cx, ENABLE); } else { I2C_AcknowledgeConfig(I2Cx, ENABLE); }
while (!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED)); data[i] = I2C_ReceiveData(I2Cx); } I2C_AcknowledgeConfig(I2Cx, ENABLE);
return 0; }
|