添加I2C

This commit is contained in:
2024-03-02 01:06:06 +08:00
parent deafcfed1a
commit bef8342c93
21 changed files with 20646 additions and 18411 deletions
+62 -1
View File
@@ -13,8 +13,69 @@ void i2c_init()
}
void i2c_config()
void i2c_config(I2C_ptr ptr, I2C_TypeDef *I2C, GPIO_TypeDef *GPIO_SDA, uint32_t Pin_SDA, GPIO_TypeDef *GPIO_SCL, uint32_t Pin_SCL, uint8_t address,uint16_t bound)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
I2C_InitTypeDef I2C_InitTSturcture = {0};
//DMA_InitTypeDef DMA_InitStructure = {0};
NVIC_InitTypeDef NVIC_InitStructure = {0};
uint32_t RCC_Periph = RCC_APB2Periph_USART1;
ptr->I2C = I2C;
ptr->GPIO_SDA = GPIO_SDA;
ptr->Pin_SDA = Pin_SDA;
ptr->GPIO_SCL = GPIO_SCL;
ptr->Pin_SCL = Pin_SCL;
if(ptr->I2C == I2C1)RCC_Periph = RCC_APB1Periph_I2C1;
else if(ptr->I2C == I2C2)RCC_Periph = RCC_APB1Periph_I2C2;
RCC_APB1PeriphClockCmd(RCC_Periph, ENABLE);
//初始化IO口
//init sda
if(ptr->GPIO_SDA == GPIOA)RCC_Periph = RCC_APB2Periph_GPIOA;
else if(ptr->GPIO_SDA == GPIOB)RCC_Periph = RCC_APB2Periph_GPIOB;
else if(ptr->GPIO_SDA == GPIOC)RCC_Periph = RCC_APB2Periph_GPIOC;
else if(ptr->GPIO_SDA == GPIOD)RCC_Periph = RCC_APB2Periph_GPIOD;
else if(ptr->GPIO_SDA == GPIOE)RCC_Periph = RCC_APB2Periph_GPIOE;
RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);
GPIO_InitStructure.GPIO_Pin = ptr->Pin_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ptr->GPIO_SDA, &GPIO_InitStructure);
//init scl
if(ptr->GPIO_SCL == GPIOA)RCC_Periph = RCC_APB2Periph_GPIOA;
else if(ptr->GPIO_SCL == GPIOB)RCC_Periph = RCC_APB2Periph_GPIOB;
else if(ptr->GPIO_SCL == GPIOC)RCC_Periph = RCC_APB2Periph_GPIOC;
else if(ptr->GPIO_SCL == GPIOD)RCC_Periph = RCC_APB2Periph_GPIOD;
else if(ptr->GPIO_SCL == GPIOE)RCC_Periph = RCC_APB2Periph_GPIOE;
RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);
GPIO_InitStructure.GPIO_Pin = ptr->Pin_SCL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ptr->GPIO_SCL, &GPIO_InitStructure);
//init i2c
I2C_InitTSturcture.I2C_ClockSpeed = bound;
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_16_9;
I2C_InitTSturcture.I2C_OwnAddress1 = address;
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init( I2C1, &I2C_InitTSturcture );
//I2C_DMACmd( I2C1, ENABLE );
I2C_Cmd( I2C1, ENABLE );
#if (I2C_MODE == HOST_MODE)
I2C_AcknowledgeConfig( I2C1, ENABLE );
#endif
}
+19 -3
View File
@@ -11,9 +11,25 @@
#include "ch32v20x.h"
#include "Global.h"
void AT24CXX_Init(void);
typedef struct sI2C
{
I2C_TypeDef *I2C;
GPIO_TypeDef *GPIO_SDA;
GPIO_TypeDef *GPIO_SCL;
uint32_t Pin_SDA;
uint32_t Pin_SCL;
uint8_t address;
}I2C_t;
typedef I2C_t* I2C_ptr;
void AT24CXX_Read(u16 ReadAddr, u8 *pBuffer, u16 NumToRead);
void AT24CXX_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite);
#endif /* BSP_DRV_I2C_H_ */
+21
View File
@@ -32,6 +32,11 @@ void key_thread(void)
void key_config(KEY_ptr ptr, GPIO_TypeDef *GPIO, uint32_t Pin, uint8_t type)
{
if (ptr == NULL)
{
return ;
}
ptr->GPIO = GPIO;
ptr->Pin = Pin;
@@ -87,6 +92,11 @@ void key_update(KEY_ptr ptr)
if(ptr->last == ptr->state)
{
ptr->state = pressed;
if(ptr->pressed != NULL)
{
ptr->pressed();
}
}
}
}
@@ -97,6 +107,17 @@ void key_update(KEY_ptr ptr)
if(ptr->last == ptr->state)
{
ptr->state = release;
if(ptr->released != NULL)
{
ptr->released();
}
if(ptr->clicked != NULL)
{
ptr->clicked();
}
}
}
}
+5
View File
@@ -38,6 +38,11 @@ typedef struct sKEY
struct sKEY *next_ptr;
void ( *pressed )( void );
void ( *released )( void );
void ( *clicked )( void );
void ( *doubleclicked )( void );
}KEY_t;
typedef KEY_t* KEY_ptr;
+94 -25
View File
@@ -94,13 +94,10 @@ void USART1_Init(uint32_t BaudRate,uint16_t WordLength,uint16_t StopBits,uint16_
void usart_init(void)
{
usart_config(&uart1,USART1 ,GPIOA, GPIO_Pin_9, GPIOA, GPIO_Pin_10,115200, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No,128,128);
usart_config(&uart3,USART3 ,GPIOC, GPIO_Pin_10, GPIOC, GPIO_Pin_11,19200, USART_WordLength_8b, USART_StopBits_1, USART_Parity_No,128,128);
}
void usart_config(UART_ptr ptr,USART_TypeDef *UART, GPIO_TypeDef *GPIO_Tx, uint32_t Pin_Tx, GPIO_TypeDef *GPIO_Rx, uint32_t Pin_Rx,
uint32_t BaudRate, uint16_t WordLength, uint16_t StopBits, uint16_t Parity,
uint32_t buff_tx_len,uint32_t buff_rx_len)
@@ -128,14 +125,12 @@ void usart_config(UART_ptr ptr,USART_TypeDef *UART, GPIO_TypeDef *GPIO_Tx, uint3
ptr->buffer_tx_len = buff_tx_len;
ptr->buffer_tx_len_2 = buff_tx_len / 2;
if(ptr->USART == USART1){RCC_Periph = RCC_APB2Periph_USART1;RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);}
else if(ptr->USART == USART2){RCC_Periph = RCC_APB1Periph_USART2;RCC_APB1PeriphClockCmd(RCC_Periph, ENABLE);}
else if(ptr->USART == USART3){RCC_Periph = RCC_APB1Periph_USART3;RCC_APB1PeriphClockCmd(RCC_Periph, ENABLE);}
else if(ptr->USART == UART4){RCC_Periph = RCC_APB1Periph_UART4;RCC_APB1PeriphClockCmd(RCC_Periph, ENABLE);}
if(ptr->USART == USART1)RCC_Periph = RCC_APB2Periph_USART1;
else if(ptr->USART == USART2)RCC_Periph = RCC_APB1Periph_USART2;
else if(ptr->USART == USART3)RCC_Periph = RCC_APB1Periph_USART3;
else if(ptr->USART == UART4)RCC_Periph = RCC_APB1Periph_UART4;
RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);
//初始化IO口
//init tx
if(ptr->GPIO_Tx == GPIOA)RCC_Periph = RCC_APB2Periph_GPIOA;
@@ -158,7 +153,7 @@ void usart_config(UART_ptr ptr,USART_TypeDef *UART, GPIO_TypeDef *GPIO_Tx, uint3
else if(ptr->GPIO_Rx == GPIOE)RCC_Periph = RCC_APB2Periph_GPIOE;
RCC_APB2PeriphClockCmd(RCC_Periph, ENABLE);
GPIO_InitStructure.GPIO_Pin = ptr->GPIO_Rx;
GPIO_InitStructure.GPIO_Pin = ptr->Pin_Rx;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ptr->GPIO_Rx, &GPIO_InitStructure);
@@ -173,13 +168,11 @@ void usart_config(UART_ptr ptr,USART_TypeDef *UART, GPIO_TypeDef *GPIO_Tx, uint3
USART_Init(ptr->USART, &USART_InitStructure);
/*打开接收中断*/
USART_ITConfig(ptr->USART, USART_IT_RXNE, ENABLE);//读取数据
/*打开IDLE数据检测中断*/
USART_ITConfig(ptr->USART, USART_IT_IDLE, ENABLE);//读取整包数据(读取fifo
USART_ITConfig(ptr->USART, USART_IT_TXE, DISABLE);//发送可以不开中断,只要传到dma即可
uint8_t NVIC_IRQChannel = USART1_IRQn;
@@ -201,19 +194,74 @@ void usart_config(UART_ptr ptr,USART_TypeDef *UART, GPIO_TypeDef *GPIO_Tx, uint3
size_t usart_read(UART_ptr ptr,unsigned char *buff,const unsigned int len)
{
size_t size = 0;
size_t i = 0;
return size;
if (ptr->buffer_rx_len)
{
size_t blen = ptr->buffer_rx_head - ptr->buffer_rx_tail;
if (blen < 0)
{
blen += ptr->buffer_rx_len;
}
if (blen > ptr->buffer_rx_max)
{
ptr->buffer_rx_max = blen;
}
for (; i < len; ++i)
{
if (ptr->buffer_rx_tail != ptr->buffer_rx_head)
{
buff[i] = ptr->buffer_rx[ptr->buffer_rx_tail++];
if (ptr->buffer_rx_tail >= ptr->buffer_rx_len)
{
ptr->buffer_rx_tail = 0;
}
}
else {
break;
}
}
}
return i;
}
size_t usart_write(UART_ptr ptr,const unsigned char *buff,const unsigned int len)
{
size_t size = 0;
size_t size = len;
ptr->pkg_tx++;
if (ptr->buffer_tx_len)
{
for (size_t i = 0; i < len; ++i)
{
ptr->buffer_tx[ptr->buffer_tx_head++] = buff[i];
if (ptr->buffer_tx_head >= ptr->buffer_tx_len)
{
ptr->buffer_tx_head = 0;
}
}
int blen = ptr->buffer_tx_head - ptr->buffer_tx_tail;
if (blen < 0)
{
blen += ptr->buffer_tx_len;
}
if (blen > ptr->buffer_tx_max)
{
ptr->buffer_tx_max = blen;
}
if (ptr->buffer_tx_tail != ptr->buffer_tx_head)
{
if (ptr->GPIOx)
{
GPIO_WriteBit(ptr->GPIOx, ptr->GPIO_Pin, Bit_SET);
}
/* enable the USARTx transmit interrupt */
USART_ITConfig(ptr->USART, USART_IT_TXE, ENABLE);
}
}
USART_ITConfig(ptr->USART, USART_IT_TXE, ENABLE);//发送可以不开中断,只要传到dma即可
return size;
}
@@ -221,18 +269,14 @@ void UART_IRQHandler(UART_ptr ptr)
{
if (RESET != USART_GetITStatus(ptr->USART, USART_IT_RXNE))
{
/* Read one byte from the receive data register */
ptr->buffer_rx[ptr->buffer_rx_head++] = (uint8_t)USART_ReceiveData(ptr->USART);
//ptr->byt_rx++;
ptr->byt_rx++;
if (ptr->buffer_rx_head >= ptr->buffer_rx_len)
{
ptr->buffer_rx_head = 0;
}
USART_ClearITPendingBit(ptr->USART, USART_IT_RXNE);
}
if ((RESET != USART_GetITStatus(ptr->USART, USART_IT_IDLE)))
@@ -247,8 +291,33 @@ void UART_IRQHandler(UART_ptr ptr)
{
USART_ClearITPendingBit(ptr->USART, USART_IT_TXE);
/*打开发送中断*/
USART_ITConfig(ptr->USART, USART_IT_TXE, DISABLE);//发送可以不开中断,只要传到dma即可
if (ptr->buffer_tx_tail == ptr->buffer_tx_head)
{
/* disable the USARTx transmit interrupt */
USART_ITConfig(ptr->USART, USART_IT_TXE, DISABLE);
USART_ITConfig(ptr->USART, USART_IT_TC, ENABLE);
}
else
{
/* Write one byte to the transmit data register */
USART_SendData(ptr->USART, ptr->buffer_tx[ptr->buffer_tx_tail++]);
ptr->byt_tx++;
if (ptr->buffer_tx_tail >= ptr->buffer_tx_len)
{
ptr->buffer_tx_tail = 0;
}
}
}
//发送完成中断
if (RESET != USART_GetITStatus(ptr->USART, USART_IT_TC))
{
USART_ClearITPendingBit(ptr->USART, USART_IT_TC);
USART_ITConfig(ptr->USART, USART_IT_TC, DISABLE);
if (ptr->GPIOx)
{
GPIO_WriteBit(ptr->GPIOx, ptr->GPIO_Pin, Bit_RESET);
}
}
}
+25 -3
View File
@@ -9,7 +9,9 @@
#define BSP_DRV_UART_H_
#include "ch32v20x.h"
#include "string.h"
#include <string.h>
#include <stdlib.h>
#include <string.h>
#define RingBuffSize 128
@@ -65,14 +67,34 @@ typedef struct sUART
size_t buffer_tx_len_2;
size_t byt_rx;
size_t byt_tx;
size_t pkg_rx;
size_t pkg_tx;
size_t last_byt_rx;
size_t last_byt_tx;
size_t last_pkg_rx;
size_t last_pkg_tx;
size_t Bps_rx;
size_t Pps_rx;
size_t Bps_tx;
size_t Pps_tx;
size_t err_tx;
size_t err_rx;
size_t buffer_rx_max;
size_t buffer_tx_max;
GPIO_TypeDef *GPIOx;
uint32_t GPIO_Pin;
struct sUART *next_ptr;
}UART_t;
typedef UART_t* UART_ptr;
extern UART_t uart1;
extern UART_t uart2;
extern UART_t uart3;
void usart_init(void);
+9
View File
@@ -11,6 +11,7 @@
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
#include "debug.h"
#include "drv_uart.h"
static uint8_t p_us = 0;
static uint16_t p_ms = 0;
@@ -209,10 +210,12 @@ void USART_Printf_Init(uint32_t baudrate)
__attribute__((used))
int _write(int fd, char *buf, int size)
{
#if (0)
int i;
for(i = 0; i < size; i++){
#if(DEBUG == DEBUG_UART1)
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, *buf++);
#elif(DEBUG == DEBUG_UART2)
@@ -224,6 +227,12 @@ int _write(int fd, char *buf, int size)
#endif
}
#else
usart_write(&uart1, buf, size);
#endif
return size;
}
Binary file not shown.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 360 KiB

Binary file not shown.
+14 -54
View File
@@ -38,7 +38,7 @@ uint8_t Green_Count = 0;
uint8_t Blue_Count = 0;
uint8_t tx[20] = "123467890asdfghjklq";
uint8_t rx[20] = {0};
uint8_t rx[150] = {0};
/*********************************************************************
* @fn main
@@ -57,7 +57,7 @@ int main(void)
//printf("SystemClk:%d\t\n", SystemCoreClock);
//ÍâÉè³õʼ»¯
led_init();
tim_init();
adc_init();
@@ -65,20 +65,8 @@ int main(void)
usart_init();
//É豸³õʼ»¯
/*
AT24CXX_Init();
AT24CXX_Write(0x0010, tx, 20);
Delay_Ms(2000);
*/
//AT24CXX_Read(0x0010, rx, 20);//读取之后整个系统都变慢了
//printf("i2c:%s\t\n", rx);
@@ -92,19 +80,24 @@ int main(void)
adc_thread(tick);
key_thread();
/*
size_t len = usart_read(&uart1, rx, sizeof(rx));
if (len) {
usart_write(&uart1, rx, len);
}
*/
//usart_write(&uart1, tx, 20);
//printf( "tim1:UPDATE %d,%d\r\n",get_ticks_msec(),get_ticks());
//printf("keys:%d,%d,%d,%d\n",keys[0],keys[1],keys[2],keys[3]);
//printf("keys:%d,%d,%d,%d adc:%4.4f, %4.4f, %4.4f, %4.4f tick:%u\n",key1.state,key2.state,key3.state,key4.state, voltage.Voltage[0],voltage.Voltage[1],voltage.Voltage[2],voltage.Voltage[3],tick);
printf("keys:%d,%d,%d,%d\tadc:%4.4f,%4.4f,%4.4f,%4.4f\t tick:%u\n",key1.state,key2.state,key3.state,key4.state, voltage.Voltage[0],voltage.Voltage[1],voltage.Voltage[2],voltage.Voltage[3],tick);
Red_Count += 1;
Green_Count += 1;
Blue_Count += 1;
if(Red_Count % 2){
led_setToggle(0);
@@ -112,48 +105,15 @@ int main(void)
led_setToggle(4);
led_setToggle(5);
led_setToggle(6);
}
else {
}
if(Green_Count % 3){
led_setToggle(1);
}
if(Blue_Count % 5){
led_setToggle(2);
}
TIM_SetCompare3(TIM4, 1000);
TIM_SetCompare4(TIM4, 1100);
TIM_SetCompare1(TIM2, 1200);
TIM_SetCompare2(TIM2, 1300);
TIM_SetCompare3(TIM2, 1400);
TIM_SetCompare4(TIM2, 1500);
TIM_SetCompare1(TIM3, 300);
TIM_SetCompare2(TIM3, 300);
TIM_SetCompare3(TIM3, 300);
TIM_SetCompare4(TIM3, 300);
}
}
Binary file not shown.
+2961 -2725
View File
File diff suppressed because it is too large Load Diff
+16478 -14933
View File
File diff suppressed because it is too large Load Diff
+954 -666
View File
File diff suppressed because it is too large Load Diff
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
+4 -1
View File
@@ -24,7 +24,8 @@ Debug/debug.o: ../Debug/debug.c ../Debug/debug.h \
D:\2_code\4_ch\CH32V208WBU6\Peripheral\inc/ch32v20x_wwdg.h \
D:\2_code\4_ch\CH32V208WBU6\User/ch32v20x_it.h \
D:\2_code\4_ch\CH32V208WBU6\Debug/debug.h \
D:\2_code\4_ch\CH32V208WBU6\Peripheral\inc/ch32v20x_misc.h
D:\2_code\4_ch\CH32V208WBU6\Peripheral\inc/ch32v20x_misc.h \
D:\2_code\4_ch\CH32V208WBU6\DRV/drv_uart.h
../Debug/debug.h:
@@ -79,3 +80,5 @@ D:\2_code\4_ch\CH32V208WBU6\User/ch32v20x_it.h:
D:\2_code\4_ch\CH32V208WBU6\Debug/debug.h:
D:\2_code\4_ch\CH32V208WBU6\Peripheral\inc/ch32v20x_misc.h:
D:\2_code\4_ch\CH32V208WBU6\DRV/drv_uart.h:
BIN
View File
Binary file not shown.
BIN
View File
Binary file not shown.