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
| #include "stm32f4xx_hal.h"
#define SDA_GPIO_PORT GPIOA #define SDA_PIN GPIO_PIN_0
#define SCL_GPIO_PORT GPIOA #define SCL_PIN GPIO_PIN_1
typedef struct { GPIO_TypeDef *SDA_GPIO_Port; uint16_t SDA_Pin; GPIO_TypeDef *SCL_GPIO_Port; uint16_t SCL_Pin; } SimulatedI2C_HandleTypeDef;
void SimulatedI2C_Init(SimulatedI2C_HandleTypeDef *hi2c); void SimulatedI2C_Write(SimulatedI2C_HandleTypeDef *hi2c, uint8_t dev_addr, uint8_t *data, uint16_t size); void SimulatedI2C_Read(SimulatedI2C_HandleTypeDef *hi2c, uint8_t dev_addr, uint8_t *data, uint16_t size);
void I2C_Delay(void) { for (int i = 0; i < 100; i++); }
void I2C_Start(SimulatedI2C_HandleTypeDef *hi2c) { HAL_GPIO_WritePin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin, GPIO_PIN_SET); HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_SET); I2C_Delay(); HAL_GPIO_WritePin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin, GPIO_PIN_RESET); I2C_Delay(); HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_RESET); I2C_Delay(); }
void I2C_Stop(SimulatedI2C_HandleTypeDef *hi2c) { HAL_GPIO_WritePin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin, GPIO_PIN_RESET); HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_SET); I2C_Delay(); HAL_GPIO_WritePin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin, GPIO_PIN_SET); I2C_Delay(); }
void I2C_WriteByte(SimulatedI2C_HandleTypeDef *hi2c, uint8_t byte) { for (int8_t i = 7; i >= 0; i--) { if (byte & (1 << i)) { HAL_GPIO_WritePin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin, GPIO_PIN_RESET); } I2C_Delay(); HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_SET); I2C_Delay(); HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_RESET); I2C_Delay(); } }
uint8_t I2C_ReadByte(SimulatedI2C_HandleTypeDef *hi2c) { uint8_t byte = 0; for (int8_t i = 7; i >= 0; i--) { HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_SET); I2C_Delay(); if (HAL_GPIO_ReadPin(hi2c->SDA_GPIO_Port, hi2c->SDA_Pin)) { byte |= (1 << i); } HAL_GPIO_WritePin(hi2c->SCL_GPIO_Port, hi2c->SCL_Pin, GPIO_PIN_RESET); I2C_Delay(); } return byte; }
void SimulatedI2C_Init(SimulatedI2C_HandleTypeDef *hi2c) { GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = hi2c->SDA_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(hi2c->SDA_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = hi2c->SCL_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(hi2c->SCL_GPIO_Port, &GPIO_InitStruct); }
void SimulatedI2C_Write(SimulatedI2C_HandleTypeDef *hi2c, uint8_t dev_addr, uint8_t *data, uint16_t size) { I2C_Start(hi2c);
I2C_WriteByte(hi2c, dev_addr << 1);
for (uint16_t i = 0; i < size; i++) { I2C_WriteByte(hi2c, data[i]); }
I2C_Stop(hi2c); }
void SimulatedI2C_Read(SimulatedI2C_HandleTypeDef *hi2c, uint8_t dev_addr, uint8_t *data, uint16_t size) { I2C_Start(hi2c);
I2C_WriteByte(hi2c, (dev_addr << 1) | 0x01);
for (uint16_t i = 0; i < size; i++) { data[i] = I2C_ReadByte(hi2c); }
I2C_Stop(hi2c); }
int main(void) { HAL_Init();
SimulatedI2C_HandleTypeDef hi2c = {0}; hi2c.SDA_GPIO_Port = SDA_GPIO_PORT; hi2c.SDA_Pin = SDA_PIN; hi2c.SCL_GPIO_Port = SCL_GPIO_PORT; hi2c.SCL_Pin = SCL_PIN;
SimulatedI2C_Init(&hi2c);
uint8_t writeData[] = {0x12, 0x34, 0x56}; uint8_t readData[3];
SimulatedI2C_Write(&hi2c, 0x50, writeData, sizeof(writeData)); SimulatedI2C_Read(&hi2c, 0x50, readData, sizeof(readData));
while (1) { } }
|