diff --git a/.gitignore b/.gitignore index 3181b7a..0e65fce 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -EWARM/ \ No newline at end of file +/EWARM/* +!/EWARM/iar/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index e22f4d0..2c9544c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,10 @@ "core_cm4.h": "c", "gd32f4xx_usart.h": "c", "stdlib.h": "c", - "gd32f4xx_gpio.h": "c" + "gd32f4xx_gpio.h": "c", + "gd32f4xx_i2c.h": "c", + "drv_i2c.h": "c", + "mb85rc64.h": "c", + "gd32f4xx_rcu.h": "c" } } \ No newline at end of file diff --git a/EWARM/iar/GD32F450xE.icf b/EWARM/iar/GD32F450xE.icf new file mode 100644 index 0000000..3074568 --- /dev/null +++ b/EWARM/iar/GD32F450xE.icf @@ -0,0 +1,35 @@ +/*###ICF### Section handled by ICF editor, don't touch! ****/ +/*-Editor annotation file-*/ +/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */ +/*-Specials-*/ +define symbol __ICFEDIT_intvec_start__ = 0x08000000; +/*-Memory Regions-*/ +define symbol __ICFEDIT_region_ROM_start__ = 0x08000000; +define symbol __ICFEDIT_region_ROM_end__ = 0x0807FFFF; +define symbol __ICFEDIT_region_RAM_start__ = 0x20000000; +define symbol __ICFEDIT_region_RAM_end__ = 0x2002FFFF; +/*-Sizes-*/ +define symbol __ICFEDIT_size_cstack__ = 0x2000; +define symbol __ICFEDIT_size_heap__ = 0x2000; +/**** End of ICF editor section. ###ICF###*/ + +define symbol __region_RAM1_start__ = 0x10000000; +define symbol __region_RAM1_end__ = 0x1000FFFF; + +define memory mem with size = 4G; +define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to __ICFEDIT_region_ROM_end__]; +define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__]; +define region RAM1_region = mem:[from __region_RAM1_start__ to __region_RAM1_end__]; + +define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { }; +define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { }; + +initialize by copy { readwrite }; +do not initialize { section .noinit }; + +place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec }; + +place in ROM_region { readonly }; +place in RAM_region { readwrite, + block CSTACK, block HEAP }; +place in RAM1_region { section .sram }; \ No newline at end of file diff --git a/EWARM/iar/GD32F450xE_APP.icf b/EWARM/iar/GD32F450xE_APP.icf new file mode 100644 index 0000000..67e4732 --- /dev/null +++ b/EWARM/iar/GD32F450xE_APP.icf @@ -0,0 +1,35 @@ +/*###ICF### Section handled by ICF editor, don't touch! ****/ +/*-Editor annotation file-*/ +/* IcfEditorFile="$TOOLKIT_DIR$\config\ide\IcfEditor\cortex_v1_0.xml" */ +/*-Specials-*/ +define symbol __ICFEDIT_intvec_start__ = 0x08010000; +/*-Memory Regions-*/ +define symbol __ICFEDIT_region_ROM_start__ = 0x08010000; +define symbol __ICFEDIT_region_ROM_end__ = 0x0807FFFF; +define symbol __ICFEDIT_region_RAM_start__ = 0x20000000; +define symbol __ICFEDIT_region_RAM_end__ = 0x2002FFFF; +/*-Sizes-*/ +define symbol __ICFEDIT_size_cstack__ = 0x2000; +define symbol __ICFEDIT_size_heap__ = 0x2000; +/**** End of ICF editor section. ###ICF###*/ + +define symbol __region_RAM1_start__ = 0x10000000; +define symbol __region_RAM1_end__ = 0x1000FFFF; + +define memory mem with size = 4G; +define region ROM_region = mem:[from __ICFEDIT_region_ROM_start__ to __ICFEDIT_region_ROM_end__]; +define region RAM_region = mem:[from __ICFEDIT_region_RAM_start__ to __ICFEDIT_region_RAM_end__]; +define region RAM1_region = mem:[from __region_RAM1_start__ to __region_RAM1_end__]; + +define block CSTACK with alignment = 8, size = __ICFEDIT_size_cstack__ { }; +define block HEAP with alignment = 8, size = __ICFEDIT_size_heap__ { }; + +initialize by copy { readwrite }; +do not initialize { section .noinit }; + +place at address mem:__ICFEDIT_intvec_start__ { readonly section .intvec }; + +place in ROM_region { readonly }; +place in RAM_region { readwrite, + block CSTACK, block HEAP }; +place in RAM1_region { section .sram }; \ No newline at end of file diff --git a/GD32F4xx_Firmware_Library/GD32F4xx_standard_peripheral/Source/gd32f4xx_i2c.c b/GD32F4xx_Firmware_Library/GD32F4xx_standard_peripheral/Source/gd32f4xx_i2c.c index 2972073..32b949b 100644 --- a/GD32F4xx_Firmware_Library/GD32F4xx_standard_peripheral/Source/gd32f4xx_i2c.c +++ b/GD32F4xx_Firmware_Library/GD32F4xx_standard_peripheral/Source/gd32f4xx_i2c.c @@ -244,9 +244,9 @@ void i2c_master_addressing(uint32_t i2c_periph, uint32_t addr, uint32_t trandire { /* master is a transmitter or a receiver */ if(I2C_TRANSMITTER == trandirection) { - addr = addr & I2C_TRANSMITTER; + addr = addr & I2C_TRANSMITTER;//1110 } else { - addr = addr | I2C_RECEIVER; + addr = addr | I2C_RECEIVER;//0001 } /* send slave address */ I2C_DATA(i2c_periph) = addr; diff --git a/applications/main/main.c b/applications/main/main.c index ce1d499..523d6b5 100644 --- a/applications/main/main.c +++ b/applications/main/main.c @@ -38,8 +38,10 @@ OF SUCH DAMAGE. #include "rtservice.h" #include "drv_adc.h" #include "mb85rc64.h" -#include "tmp75.h" +// #include "tmp75.h" #include "drv_usart.h" +#include "drv_i2c.h" + float voltage; float current; @@ -78,31 +80,34 @@ int main(void) // 其他应用程序初始化代码... - __enable_irq(); - gd32_usart_init(); + // __enable_irq(); + + // gd32_usart_init(); + + i2c_init(); adc_init(); - temp75_gpio_init(); + // temp75_gpio_init(); //测试mb铁电读写 memset(&ttest, 0, sizeof(ttest)); - // mb85rc64_page_write(); - mb85rc64_add_read(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest)); - ttest.status = 0; - ttest.ttt++; - if (ttest.ggg >= 1000) - { - ttest.ggg -= 2; - ttest.ggg /= 3; - } - else - { - ttest.ggg += 2; - ttest.ggg *= 3; - } + + // mb85rc64_add_read(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest)); + // ttest.status = 0; + // ttest.ttt++; + // if (ttest.ggg >= 1000) + // { + // ttest.ggg -= 2; + // ttest.ggg /= 3; + // } + // else + // { + // ttest.ggg += 2; + // ttest.ggg *= 3; + // } - mb85rc64_page_write(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest)); + // mb85rc64_page_write(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest)); // 进入主循环或启动其他任务 while (1) @@ -121,11 +126,10 @@ int main(void) #endif //测试mb铁电读写 - read_mb_id((uint8_t*)&tiedian_id);//读取铁电ID + // read_mb_id((uint8_t*)&tiedian_id);//读取铁电ID memset(&ttest, 0, sizeof(ttest)); - // mb85rc64_page_write(); - mb85rc64_add_read(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest)); + // mb85rc64_add_read(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest)); ttest.tmperature = ptz_temperature_collect_tmp75_task(); // usart_puts(ttest.ttt); diff --git a/doc/sbaa588a.pdf b/doc/sbaa588a.pdf new file mode 100644 index 0000000..5910c42 Binary files /dev/null and b/doc/sbaa588a.pdf differ diff --git a/doc/tmp75.pdf b/doc/tmp75.pdf new file mode 100644 index 0000000..47a9873 Binary files /dev/null and b/doc/tmp75.pdf differ diff --git a/drivers/drv_adc.c b/drivers/drv_adc.c index 344d3fe..3119a30 100644 --- a/drivers/drv_adc.c +++ b/drivers/drv_adc.c @@ -302,7 +302,28 @@ float ptz_Current_collect_adc1_task() /* 温度采集 */ float ptz_temperature_collect_tmp75_task() { - tmp75[tmp75_num] = tmp75_read_temp(); + uint8_t tempHL[2]; + uint16_t tempCode = 0; + float temp = 0; + + i2c_read(tempHL, 2); + // tmp75[tmp75_num] = tmp75_read_temp(); + tempCode = (tempHL[0] << 8) | tempHL[1]; + tempCode = tempCode >> 6; + + if (tempCode & 0x200) // 负温度 + { + tempCode &= 0x1ff; + temp = ((float)tempCode - 512) / 4; + } + else + { + temp = (float)tempCode / 4; + } + + tmp75[tmp75_num] = temp; + // tmp75[tmp75_num] = tmp75_read_temp(); + tmp75_num ++; if(tmp75_num >= LB_T_TIMES) diff --git a/drivers/drv_adc.h b/drivers/drv_adc.h index 8534fd4..0191e01 100644 --- a/drivers/drv_adc.h +++ b/drivers/drv_adc.h @@ -1,7 +1,8 @@ #ifndef __DRV_ADC_H_ #define __DRV_ADC_H_ -#include "tmp75.h" +// #include "tmp75.h" +#include "drv_i2c.h" #include "stdlib.h" #include "stdbool.h" #include "string.h" diff --git a/drivers/drv_i2c.c b/drivers/drv_i2c.c index 990543a..6409cdb 100644 --- a/drivers/drv_i2c.c +++ b/drivers/drv_i2c.c @@ -16,16 +16,16 @@ All rights reserved. #include "rtthread.h" #include -/* - @ brief 延时函数 - @ param - @ return - @ note 2025-09-15 -*/ -static void delay_us(int us) -{ - rt_thread_udelay(us); -} +// /* +// @ brief 延时函数 +// @ param +// @ return +// @ note 2025-09-15 +// */ +// static void delay_us(int us) +// { +// rt_thread_udelay(us); +// } void i2c_init(void) { @@ -53,7 +53,9 @@ void i2c_config(void) { // 启用 I2C 外设的时钟 rcu_periph_clock_enable(RCU_I2C0); - + + i2c_deinit(I2C_PERIPH); + // 配置 I2C 的时钟参数: // I2C_PERIPH:这里使用的I2C1 // I2C_SPEED:通信速率(单位为 Hz,常用为 100000 或 400000),这里使用100000 @@ -62,60 +64,104 @@ void i2c_config(void) // 配置 I2C 工作模式和地址: // I2C_ADDFORMAT_7BITS:使用 7 位地址模式 // I2C_BUS_ADDRESS:SD2068器件代码0110010 = 0x32 - i2c_mode_addr_config(I2C_PERIPH, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, I2C_BUS_ADDRESS); - - // 启用 I2C 外设 - i2c_enable(I2C_PERIPH); + i2c_mode_addr_config(I2C_PERIPH, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, TMP75_ADDRESS); + // 使能 ACK 应答功能,确保在接收数据后自动发送 ACK i2c_ack_config(I2C_PERIPH, I2C_ACK_ENABLE); + + // 启用 I2C 外设 + i2c_enable(I2C_PERIPH); + } -/* - @ brief 读取温度值 - @ param - @ return - @ note 2025-09-15 -*/ -float tmp75_read_temp(void) + + +void i2c_write(uint8_t *buff, int len) { - uint8_t tempH = 0; - uint8_t tempL = 0; - uint16_t tempCode = 0; - float temp = 0; - - // 起始信号 - i2c_start(); - // 写tmp75地址 - tmp75_write_byte(TMP75_ADDRESS); - // 接收tmp75的ack信息 - tmp75_ack(); - // 发送需读取数据的地址 - tmp75_write_byte(TEMP_REGISTER_ADDRESS); - tmp75_ack(); - - i2c_start(); - // 写tmp75地址 - tmp75_write_byte(TMP75_ADDRESS + 1); // 读地址数据 - tmp75_ack(); - tempH = tmp75_read_byte(); - master_ack(); - tempL = tmp75_read_byte(); - master_noack(); - i2c_stop(); - - tempCode = (tempH << 8) | tempL; - tempCode = tempCode >> 6; - if (tempCode & 0x200) // 负温度 - { - tempCode &= 0x1ff; - temp = ((float)tempCode - 512) / 4; - } - else - { - temp = (float)tempCode / 4; - } - TMP75_SDA_LOW; - TMP75_SCL_LOW; - return (temp); + /* 等待总线空闲 */ + while(i2c_flag_get(I2C_PERIPH, I2C_FLAG_I2CBSY)); + /* 发送起始条件 */ + i2c_start_on_bus(I2C_PERIPH); + /* 等待起始条件已发送标志 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_SBSEND)); + /* 发送从机地址(写模式) */ + i2c_master_addressing(I2C_PERIPH, TMP75_ADDRESS, I2C_TRANSMITTER); + /* 等待地址已发送标志 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_ADDSEND)); + /* 清除地址发送标志 */ + i2c_flag_clear(I2C_PERIPH, I2C_FLAG_ADDSEND); + + for(int i = 0; i < len; i++) + { + /* 等待发送数据缓冲区为空 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_TBE)); + /* 发送一个字节数据 */ + i2c_data_transmit(I2C_PERIPH, buff[i]); + } + /* 等待数据传输完成 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_TBE)); + /* 发送停止条件 */ + i2c_stop_on_bus(I2C_PERIPH); + /* 等待停止条件发送完成 */ + while(I2C_CTL0(I2C_PERIPH) & I2C_CTL0_STOP); // 检查STPDET位或类似标志 +} + +//接收1字节数据 +void i2c_read(uint8_t *buff, uint16_t len) +{ + /* 等待I2C总线空闲 */ + while(i2c_flag_get(I2C_PERIPH, I2C_FLAG_I2CBSY)); + /* 向I2C总线发送启动信号 */ + i2c_start_on_bus(I2C_PERIPH); + /* 等待,直到设置SBSEND位 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_SBSEND));//设置主机发送启动条件 + + /* 将从机地址发送到I2C总线 */ + i2c_master_addressing(I2C_PERIPH, TMP75_ADDRESS, I2C_RECEIVER);//设置主机接收 + /* 等待地址位被设置 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_ADDSEND)); + /* 清除地址位 */ + i2c_flag_clear(I2C_PERIPH, I2C_FLAG_ADDSEND); + + /* 使能ACK(多个字节接收时需要应答) */ + i2c_ack_config(I2C_PERIPH, I2C_ACK_ENABLE); + + /* 5. 接收数据 */ + if(len > 1) + { + /* 使能ACK(多个字节接收时需要应答) */ + i2c_ack_config(I2C_PERIPH, I2C_ACK_ENABLE); + + for(int i = 0; i < len; i++) + { + if(i == (len - 1)) + { + /* 最后一个字节前禁用ACK */ + i2c_ack_config(I2C_PERIPH, I2C_ACK_DISABLE); + } + + /* 等待RBNE标志(接收缓冲区非空) */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_RBNE)); + + /* 读取数据 */ + buff[i] = i2c_data_receive(I2C_PERIPH); + } + } + else + { + /* 单字节接收:直接禁用ACK */ + i2c_ack_config(I2C_PERIPH, I2C_ACK_DISABLE); + + /* 等待RBNE标志 */ + while(!i2c_flag_get(I2C_PERIPH, I2C_FLAG_RBNE)); + + /* 读取数据 */ + buff[0] = i2c_data_receive(I2C_PERIPH); + } + + /* 发送停止信号 */ + i2c_stop_on_bus(I2C_PERIPH); + /* 等待停止信号完成 */ + while(I2C_CTL0(I2C_PERIPH) & I2C_CTL0_STOP); } diff --git a/drivers/drv_i2c.h b/drivers/drv_i2c.h index 5ecf346..2151062 100644 --- a/drivers/drv_i2c.h +++ b/drivers/drv_i2c.h @@ -16,13 +16,16 @@ #include "gd32f4xx_gpio.h" #include "gd32f4xx_i2c.h" - - #define I2C_PERIPH I2C0 #define I2C_SCL_PIN GPIO_PIN_6 #define I2C_SDA_PIN GPIO_PIN_7 #define I2C_SPEED 100000 +#define TMP75_ADDRESS 0x48 //1001000 +// #define TMP75_ADDRESS 0x09 +#define TEMP_REGISTER_ADDRESS 0x00 //mcu作为从机时被寻址的地址 + + // #define I2C_SCL_HIGH gpio_bit_set(GPIOB, I2C_SCL_PIN) // #define I2C_SCL_LOW gpio_bit_reset(GPIOB, I2C_SCL_PIN) @@ -34,8 +37,10 @@ void i2c_init(void); void i2c_gpio_config(void); void i2c_config(void); +void i2c_write(uint8_t *buff, int len); +void i2c_read(uint8_t *buff, uint16_t len); -void temp75_gpio_init(); -float tmp75_read_temp(void); +// void temp75_gpio_init(); +// float tmp75_read_temp(void); #endif \ No newline at end of file diff --git a/drivers/mb85rc64/mb85rc64.c b/drivers/mb85rc64/mb85rc64.c index f177c40..80f33a0 100644 --- a/drivers/mb85rc64/mb85rc64.c +++ b/drivers/mb85rc64/mb85rc64.c @@ -26,27 +26,27 @@ static void delay_us(int us) rt_thread_udelay(us); } -/* - @ brief оƬʼ - @ param - @ return - @ note 2022-05-26 -*/ -void mb85rc64_gpio_init() -{ - //ʱ - rcu_periph_clock_enable(RCU_GPIOB); - //Ϊģʽ:PB6 - gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6); - gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6); +// /* +// @ brief оƬʼ +// @ param +// @ return +// @ note 2022-05-26 +// */ +// void mb85rc64_gpio_init() +// { +// //ʱ +// rcu_periph_clock_enable(RCU_GPIOB); +// //Ϊģʽ:PB6 +// gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6); +// gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6); - //PB7ģʽ - gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7); - gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7); +// //PB7ģʽ +// gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7); +// gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7); - MB85RC64_SCL_HIGH; - MB85RC64_SDA_HIGH; -} +// MB85RC64_SCL_HIGH; +// MB85RC64_SDA_HIGH; +// } /* @ brief SDAΪģʽ diff --git a/drivers/mb85rc64/mb85rc64.h b/drivers/mb85rc64/mb85rc64.h index ea29f04..dd3ad7b 100644 --- a/drivers/mb85rc64/mb85rc64.h +++ b/drivers/mb85rc64/mb85rc64.h @@ -15,6 +15,9 @@ #include "gd32f4xx_gpio.h" +#define MB85RC64_ADDRESS 0x50 + + #define MB85RC64_ADDRESS_WRITE 0xA0 #define MB85RC64_ADDRESS_READ 0xA1 //mb85rc64洢ַ @@ -29,10 +32,9 @@ #define MB85RC64_SDA_GET gpio_input_bit_get(GPIOB, GPIO_PIN_7) -void mb85rc64_gpio_init(); +// void mb85rc64_gpio_init(); char mb85rc64_page_write(unsigned short int addr, unsigned char* data, int data_len); char mb85rc64_add_read(unsigned short int addr, unsigned char *data, int data_len); -void mb85rc64_function_test(); void read_mb_id(uint8_t* buff); #endif \ No newline at end of file diff --git a/drivers/tmp75/tmp75.c b/drivers/tmp75/tmp75.c index b907cf6..4364c47 100644 --- a/drivers/tmp75/tmp75.c +++ b/drivers/tmp75/tmp75.c @@ -224,16 +224,16 @@ float tmp75_read_temp(void) // ʼź i2c_start(); // дtmp75ַ - tmp75_write_byte(TMP75_ADDRESS); + tmp75_write_byte(TMP75_ADDRESS);//д // tmp75ackϢ tmp75_ack(); // ȡݵĵַ - tmp75_write_byte(TEMP_REGISTER_ADDRESS); + tmp75_write_byte(TEMP_REGISTER_ADDRESS);// дַ 0x00 tmp75_ack(); i2c_start(); // дtmp75ַ - tmp75_write_byte(TMP75_ADDRESS + 1); // ַ + tmp75_write_byte(TMP75_ADDRESS + 1); // tmp75_ack(); tempH = tmp75_read_byte(); master_ack();