2025-04-02 v1.1

对之前遗留下来的乱码用 utf-8 重新编写
增加了标准的函数&文件注释
This commit is contained in:
JRNitre 2025-04-02 09:10:11 +08:00
parent 3d203901b6
commit 7bd40527b1
3 changed files with 78 additions and 36 deletions

View File

@ -1 +1,8 @@
# 基于 STM32 标准库的软件 I2C # 基于 STM32 标准库的软件 I2C
## 待办
- 重构代码,解耦各个模块之间的联系,便于移植
- 将原本在函数内部的 IO 配置通过回调函数引出函数
- 重新编写延时部分,将延时方式转为回调 1us 心跳函数

74
i2c.c
View File

@ -1,11 +1,13 @@
#include "i2c.h" #include "i2c.h"
// 初始化软件 I2C /*
* @brief I2C
* @param void
*
* @return void
*/
void i2cInit(void){ void i2cInit(void){
/* 初始化需要操作的 I2C IO */
GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitTypeDef GPIO_InitStruct;
// enable gpiob for rcc_ahb1
I2C_RCC_CLOCK_CMD; I2C_RCC_CLOCK_CMD;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
@ -16,7 +18,6 @@ void i2cInit(void){
GPIO_InitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN; GPIO_InitStruct.GPIO_Pin = I2C_SCL_PIN | I2C_SDA_PIN;
GPIO_Init(I2C_GROUP,&GPIO_InitStruct); GPIO_Init(I2C_GROUP,&GPIO_InitStruct);
// 初始化 I2C 引脚为高电平
GPIO_SetBits(I2C_GROUP, I2C_SCL_PIN); GPIO_SetBits(I2C_GROUP, I2C_SCL_PIN);
GPIO_SetBits(I2C_GROUP, I2C_SDA_PIN); GPIO_SetBits(I2C_GROUP, I2C_SDA_PIN);
@ -32,20 +33,26 @@ void i2cInit(void){
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM2, ENABLE);
// 初始化完毕后等待 1ms 可酌情删除
//i2cDelay_us(1000);
} }
/* us 级延时 */ /*
* @brief
* @param uint32_t us us
*
* @return void
*/
void i2cDelay_us(uint32_t us){ void i2cDelay_us(uint32_t us){
TIM_SetCounter(TIM2, 0); TIM_SetCounter(TIM2, 0);
// wait counter to target // wait counter to target
while(TIM_GetCounter(TIM2) < us); while(TIM_GetCounter(TIM2) < us);
} }
/* 基本控制函数 */ /*
// 总线电平控制 * @brief i2c
* @param uint8_t BitValue 0->/1->
*
* @return void
*/
void i2cCon_SCL(uint8_t BitValue){ void i2cCon_SCL(uint8_t BitValue){
GPIO_WriteBit(I2C_GROUP, I2C_SCL_PIN, (BitAction)(BitValue)); GPIO_WriteBit(I2C_GROUP, I2C_SCL_PIN, (BitAction)(BitValue));
i2cDelay_us(i2cSpeedDelay); i2cDelay_us(i2cSpeedDelay);
@ -55,15 +62,24 @@ void i2cCon_SDA(uint8_t BitValue){
i2cDelay_us(i2cSpeedDelay); i2cDelay_us(i2cSpeedDelay);
} }
// 读取 SDA 数据 /*
* @brief i2c SDA bit
* @param void
*
* @return uint8_t 线
*/
uint8_t i2cReceive_SDA(void){ uint8_t i2cReceive_SDA(void){
uint8_t bitValue = GPIO_ReadInputDataBit(I2C_GROUP, I2C_SDA_PIN); uint8_t bitValue = GPIO_ReadInputDataBit(I2C_GROUP, I2C_SDA_PIN);
i2cDelay_us(i2cSpeedDelay); i2cDelay_us(i2cSpeedDelay);
return bitValue; return bitValue;
} }
/* 基本时序单元 */ /*
// 开始信号和结束信号 * @brief &
* @param void
*
* @return void
*/
void i2cSignal_Start(void){ void i2cSignal_Start(void){
i2cCon_SDA(1); i2cCon_SDA(1);
i2cCon_SCL(1); i2cCon_SCL(1);
@ -76,8 +92,12 @@ void i2cSignal_Stop(void){
i2cCon_SDA(1); i2cCon_SDA(1);
} }
/* 数据收发 */ /*
// 发送一个字节 * @brief byte
* @param uint8_t byte
*
* @return void
*/
void i2cSend_Byte(uint8_t byte){ void i2cSend_Byte(uint8_t byte){
for(int i = 0; i < 8; i++){ for(int i = 0; i < 8; i++){
i2cCon_SDA(!!(byte & (0x80 >> i))); i2cCon_SDA(!!(byte & (0x80 >> i)));
@ -85,7 +105,13 @@ void i2cSend_Byte(uint8_t byte){
i2cCon_SCL(0); i2cCon_SCL(0);
} }
} }
// 接收一个字节
/*
* @brief byte
* @param void
*
* @return uint8_t
*/
uint8_t i2cReceive_Byte(void){ uint8_t i2cReceive_Byte(void){
uint8_t rByte = 0x00; uint8_t rByte = 0x00;
i2cCon_SDA(1); i2cCon_SDA(1);
@ -103,12 +129,24 @@ uint8_t i2cReceive_Byte(void){
return rByte; return rByte;
} }
/* 应答位操作 */ /*
* @brief
* @param uint8_t ackBit 0->/1->
*
* @return void
*/
void i2cSend_ACK(uint8_t ackBit){ void i2cSend_ACK(uint8_t ackBit){
i2cCon_SDA(ackBit); i2cCon_SDA(ackBit);
i2cCon_SCL(1); i2cCon_SCL(1);
i2cCon_SCL(0); i2cCon_SCL(0);
} }
/*
* @brief
* @param void
*
* @return uint8_t
*/
uint8_t i2cReceive_ACK(void){ uint8_t i2cReceive_ACK(void){
i2cCon_SDA(1); i2cCon_SDA(1);
i2cCon_SCL(1); i2cCon_SCL(1);

25
i2c.h
View File

@ -1,21 +1,18 @@
/* /*
* I2C TIM I2C * Copyright (C) 2025 JRNitre nichinichisou67@outlook.com
* @file i2c.h
* @brief STM32 i2c
* *
* [ DATE ] * @author JRNitre
* CreateDate : 2025-03-31 * @email nichinichisou67@outlook.com
* LasetModified : 2025-03-31 * @version v1.0
* * @date 2025-03-31
* [ COPYRIGHT ] * @license LGPL 3.0
* @ 2025 MAShiroSoftware by JRNitre
*
* [ VERSION ]
* Version : v1.0
*
* [ ILLUSTRATE ]
* STM32F401CCU6 v1.0
* STM32 84Mhz 5us I2C 100kb/s
*/ */
//TODO 重构代码,对核心功能进行封装,方便移植
//TODO 调整延时函数,使得可以灵活适应不同主频和延时方式
#ifndef __I2C_H #ifndef __I2C_H
#define __I2C_H #define __I2C_H