2025-03-31 v1.0
首次提交,初始化仓库 这是一个可用版本,后续会更新完善此库
This commit is contained in:
commit
3d203901b6
118
i2c.c
Normal file
118
i2c.c
Normal file
@ -0,0 +1,118 @@
|
||||
#include "i2c.h"
|
||||
|
||||
// 初始化软件 I2C
|
||||
void i2cInit(void){
|
||||
/* 初始化需要操作的 I2C IO */
|
||||
GPIO_InitTypeDef GPIO_InitStruct;
|
||||
|
||||
// enable gpiob for rcc_ahb1
|
||||
I2C_RCC_CLOCK_CMD;
|
||||
|
||||
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
||||
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
|
||||
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
||||
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
||||
|
||||
GPIO_InitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
|
||||
GPIO_Init(I2C_GROUP,&GPIO_InitStruct);
|
||||
|
||||
// 初始化 I2C 引脚为高电平
|
||||
GPIO_SetBits(I2C_GROUP, I2C_SCL_PIN);
|
||||
GPIO_SetBits(I2C_GROUP, I2C_SDA_PIN);
|
||||
|
||||
// tim2 init
|
||||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
|
||||
|
||||
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
||||
|
||||
TIM_TimeBaseStructure.TIM_Period = 0xFFFFFFFF;
|
||||
TIM_TimeBaseStructure.TIM_Prescaler = 83;
|
||||
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
||||
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
||||
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
|
||||
|
||||
TIM_Cmd(TIM2, ENABLE);
|
||||
|
||||
// 初始化完毕后等待 1ms 可酌情删除
|
||||
//i2cDelay_us(1000);
|
||||
}
|
||||
|
||||
/* us 级延时 */
|
||||
void i2cDelay_us(uint32_t us){
|
||||
TIM_SetCounter(TIM2, 0);
|
||||
// wait counter to target
|
||||
while(TIM_GetCounter(TIM2) < us);
|
||||
}
|
||||
|
||||
/* 基本控制函数 */
|
||||
// 总线电平控制
|
||||
void i2cCon_SCL(uint8_t BitValue){
|
||||
GPIO_WriteBit(I2C_GROUP, I2C_SCL_PIN, (BitAction)(BitValue));
|
||||
i2cDelay_us(i2cSpeedDelay);
|
||||
}
|
||||
void i2cCon_SDA(uint8_t BitValue){
|
||||
GPIO_WriteBit(I2C_GROUP, I2C_SDA_PIN, (BitAction)(BitValue));
|
||||
i2cDelay_us(i2cSpeedDelay);
|
||||
}
|
||||
|
||||
// 读取 SDA 数据
|
||||
uint8_t i2cReceive_SDA(void){
|
||||
uint8_t bitValue = GPIO_ReadInputDataBit(I2C_GROUP, I2C_SDA_PIN);
|
||||
i2cDelay_us(i2cSpeedDelay);
|
||||
return bitValue;
|
||||
}
|
||||
|
||||
/* 基本时序单元 */
|
||||
// 开始信号和结束信号
|
||||
void i2cSignal_Start(void){
|
||||
i2cCon_SDA(1);
|
||||
i2cCon_SCL(1);
|
||||
i2cCon_SDA(0);
|
||||
i2cCon_SCL(0);
|
||||
}
|
||||
void i2cSignal_Stop(void){
|
||||
i2cCon_SDA(0);
|
||||
i2cCon_SCL(1);
|
||||
i2cCon_SDA(1);
|
||||
}
|
||||
|
||||
/* 数据收发 */
|
||||
// 发送一个字节
|
||||
void i2cSend_Byte(uint8_t byte){
|
||||
for(int i = 0; i < 8; i++){
|
||||
i2cCon_SDA(!!(byte & (0x80 >> i)));
|
||||
i2cCon_SCL(1);
|
||||
i2cCon_SCL(0);
|
||||
}
|
||||
}
|
||||
// 接收一个字节
|
||||
uint8_t i2cReceive_Byte(void){
|
||||
uint8_t rByte = 0x00;
|
||||
i2cCon_SDA(1);
|
||||
|
||||
for(int i = 0; i < 8; i++){
|
||||
i2cCon_SCL(1);
|
||||
|
||||
if(i2cReceive_SDA() == 1){
|
||||
rByte |= (0x80 >> i);
|
||||
}
|
||||
|
||||
i2cCon_SCL(0);
|
||||
}
|
||||
|
||||
return rByte;
|
||||
}
|
||||
|
||||
/* 应答位操作 */
|
||||
void i2cSend_ACK(uint8_t ackBit){
|
||||
i2cCon_SDA(ackBit);
|
||||
i2cCon_SCL(1);
|
||||
i2cCon_SCL(0);
|
||||
}
|
||||
uint8_t i2cReceive_ACK(void){
|
||||
i2cCon_SDA(1);
|
||||
i2cCon_SCL(1);
|
||||
uint8_t ackBit = i2cReceive_SDA();
|
||||
i2cCon_SCL(0);
|
||||
return ackBit;
|
||||
}
|
51
i2c.h
Normal file
51
i2c.h
Normal file
@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 软件 I2C 库,基于 TIM 定时器的软件 I2C 实现
|
||||
*
|
||||
* [ DATE ]
|
||||
* CreateDate : 2025-03-31
|
||||
* LasetModified : 2025-03-31
|
||||
*
|
||||
* [ COPYRIGHT ]
|
||||
* @ 2025 MAShiroSoftware by JRNitre
|
||||
*
|
||||
* [ VERSION ]
|
||||
* Version : v1.0
|
||||
*
|
||||
* [ ILLUSTRATE ]
|
||||
* 当前库编写于 STM32F401CCU6 截至 v1.0 版本还并未对其它平台进行验证
|
||||
* 该库基于 STM32 的定时器自带延时函数,在 84Mhz 频率下,采用了 5us 的延时 I2C 通讯速率为 100kb/s
|
||||
*/
|
||||
|
||||
#ifndef __I2C_H
|
||||
#define __I2C_H
|
||||
|
||||
/* INCLUDE FILE */
|
||||
// STM32F401CCU6
|
||||
#include "stm32f4xx.h"
|
||||
|
||||
/* I2C 引脚&时钟配置 [在这里修改引脚 标准库下可以直接使用函数进行 I2C 通讯] */
|
||||
#define I2C_SCL_PIN GPIO_Pin_3
|
||||
#define I2C_SDA_PIN GPIO_Pin_4
|
||||
#define I2C_GROUP GPIOB
|
||||
#define I2C_RCC_CLOCK_CMD RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
|
||||
|
||||
/* 这个参数用于配置延时时间, 84Mhz 下这里可以实现 5us 的延时 */
|
||||
// TODO 这里后续会提供计算公式,也许会修改工作逻辑让代码更加通用
|
||||
static uint8_t i2cSpeedDelay = 5;
|
||||
|
||||
/* FUNCTION */
|
||||
void i2cInit(void);
|
||||
|
||||
void i2cSignal_Start(void);
|
||||
void i2cSignal_Stop(void);
|
||||
|
||||
void i2cSend_Byte(uint8_t byte);
|
||||
uint8_t i2cReceive_Byte(void);
|
||||
|
||||
void i2cSend_ACK(uint8_t ackBit);
|
||||
uint8_t i2cReceive_ACK(void);
|
||||
|
||||
// 这个延时函数默认是不提供的,可以自行取消注释在其它位置调用
|
||||
//void i2cDelay_us(uint32_t us);
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user