This commit is contained in:
parent
521b2be597
commit
6d3f46b8cd
|
@ -77,6 +77,7 @@ int main(void)
|
|||
// gd32_usart_init();
|
||||
i2c_init();
|
||||
adc_init();
|
||||
usart_init();
|
||||
|
||||
|
||||
|
||||
|
@ -98,7 +99,8 @@ int main(void)
|
|||
}
|
||||
/* 写ttest到MB85TEST_ADD地址 */
|
||||
mb85rc64_write_read(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest), PAGE_WRITE);
|
||||
|
||||
uint8_t rx_data = 0;
|
||||
uint8_t tx_data = 0x61;
|
||||
// 进入主循环或启动其他任务
|
||||
while (1)
|
||||
{
|
||||
|
@ -116,6 +118,36 @@ int main(void)
|
|||
adc_software_trigger_enable(ADCX, SEQUENCE_CHANNEL);// 软件触发使能 后续接读取
|
||||
#endif
|
||||
|
||||
// while (usart_flag_get(USART_PERIPH, USART_FLAG_RBNE) == RESET);
|
||||
// usart_flag_clear(USART_PERIPH, USART_FLAG_RBNE);
|
||||
// rx_data = usart_data_receive(USART_PERIPH);
|
||||
// if(rx_data == 'a')
|
||||
// {
|
||||
// beep_enable();
|
||||
// delay_ms(250);
|
||||
// beep_disable();
|
||||
// }
|
||||
// else if(rx_data == 'b')
|
||||
// {
|
||||
// beep_enable();
|
||||
// delay_ms(500);
|
||||
// beep_disable();
|
||||
// }
|
||||
|
||||
while(RESET == usart_flag_get(USART_PERIPH, USART_FLAG_TBE))
|
||||
{
|
||||
|
||||
}
|
||||
//写寄存器
|
||||
usart_data_transmit(USART_PERIPH, tx_data);
|
||||
//等待发送完成
|
||||
while(RESET == usart_flag_get(USART_PERIPH, USART_FLAG_TC))
|
||||
{
|
||||
|
||||
}
|
||||
usart_flag_clear(USART_PERIPH, USART_FLAG_TC);
|
||||
|
||||
|
||||
// memset(&ttest, 0, sizeof(ttest));
|
||||
mb85rc64_write_read(MB85TEST_ADD, (unsigned char *)&ttest, sizeof(ttest), ADDR_READ);
|
||||
ttest.tmperature = ptz_temperature_collect_tmp75_task();
|
||||
|
|
|
@ -16,9 +16,6 @@
|
|||
#include "gd32f4xx.h"
|
||||
#include "delay.h"
|
||||
|
||||
#include "beep.h"
|
||||
|
||||
|
||||
|
||||
|
||||
// #define SOFTWARE_I2C
|
||||
|
|
|
@ -1,93 +1,40 @@
|
|||
#include "drv_usart.h"
|
||||
#include "gd32f4xx.h"
|
||||
|
||||
// 串口配置结构体
|
||||
static struct gd32_usart_config usart_config[] = {
|
||||
void usart_init(void)
|
||||
{
|
||||
.name = "test",
|
||||
.usart_periph = USART0,
|
||||
.rcu_periph_clock = RCU_USART0,
|
||||
.gpio_port = GPIOA,
|
||||
.rcu_gpio_clock = RCU_GPIOA,
|
||||
.tx_pin = GPIO_PIN_9,
|
||||
.rx_pin = GPIO_PIN_10,
|
||||
.baud_rate = 9600,
|
||||
.irq_type = USART0_IRQn
|
||||
},
|
||||
// 可以添加更多串口配置
|
||||
};
|
||||
usart_colok_config();
|
||||
|
||||
// 初始化所有配置的串口
|
||||
void gd32_usart_init(void)
|
||||
{
|
||||
for (int i = 0; i < sizeof(usart_config) / sizeof(usart_config[0]); i++)
|
||||
{
|
||||
gd32_usart_configure(&usart_config[i]);
|
||||
}
|
||||
usart_gpio_config();
|
||||
}
|
||||
|
||||
// 配置单个串口
|
||||
void gd32_usart_configure(struct gd32_usart_config *config)
|
||||
void usart_colok_config(void)
|
||||
{
|
||||
// 启用时钟
|
||||
rcu_periph_clock_enable(config->rcu_periph_clock);
|
||||
rcu_periph_clock_enable(config->rcu_gpio_clock);
|
||||
rcu_periph_clock_enable(USART_GPIO_CLK);//使能引脚时钟
|
||||
rcu_periph_clock_enable(USART_PERIPH_CLK);//使能外设时钟
|
||||
}
|
||||
|
||||
void usart_gpio_config(void)
|
||||
{
|
||||
// 配置GPIO
|
||||
gpio_af_set(config->gpio_port, GPIO_AF_7, config->tx_pin);
|
||||
gpio_af_set(config->gpio_port, GPIO_AF_7, config->rx_pin);
|
||||
gpio_mode_set(config->gpio_port, GPIO_MODE_AF, GPIO_PUPD_NONE, config->tx_pin);
|
||||
gpio_mode_set(config->gpio_port, GPIO_MODE_AF, GPIO_PUPD_NONE, config->rx_pin);
|
||||
gpio_output_options_set(config->gpio_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, config->tx_pin);
|
||||
gpio_output_options_set(config->gpio_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, config->rx_pin);
|
||||
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_TX_PIN);
|
||||
gpio_af_set(USART_GPIO_PORT, USART_GPIO_AF, USART_RX_PIN);
|
||||
|
||||
// gpio_mode_set(USART_GPIO_PORT, USART_GPIO_AF, GPIO_PUPD_PULLUP, USART_TX_PIN | USART_RX_PIN);
|
||||
gpio_mode_set(USART_GPIO_PORT, USART_GPIO_AF, GPIO_PUPD_NONE, USART_TX_PIN | USART_RX_PIN);
|
||||
gpio_output_options_set(USART_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, USART_TX_PIN | USART_RX_PIN);
|
||||
// 配置USART
|
||||
usart_deinit(config->usart_periph);
|
||||
usart_baudrate_set(config->usart_periph, config->baud_rate);
|
||||
usart_word_length_set(config->usart_periph, USART_WL_8BIT);
|
||||
usart_stop_bit_set(config->usart_periph, USART_STB_1BIT);
|
||||
usart_parity_config(config->usart_periph, USART_PM_NONE);
|
||||
// usart_hardware_flow_rts_config(config->usart_periph, USART_RTS_DISABLE);
|
||||
// usart_hardware_flow_cts_config(config->usart_periph, USART_CTS_DISABLE);
|
||||
usart_receive_config(config->usart_periph, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(config->usart_periph, USART_TRANSMIT_ENABLE);
|
||||
usart_deinit(USART_PERIPH);
|
||||
usart_baudrate_set(USART_PERIPH, USART_BAUD_RATE);
|
||||
|
||||
usart_receive_config(USART_PERIPH, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART_PERIPH, USART_TRANSMIT_ENABLE);
|
||||
|
||||
// 启用USART
|
||||
usart_enable(config->usart_periph);
|
||||
usart_enable(USART_PERIPH);
|
||||
|
||||
// 配置中断(如果需要)
|
||||
// nvic_irq_enable(config->irq_type, 0, 0);
|
||||
// usart_interrupt_enable(config->usart_periph, USART_INT_RBNE);
|
||||
}
|
||||
|
||||
// 发送字符
|
||||
void gd32_usart_putchar(struct gd32_usart_config *config, char ch)
|
||||
{
|
||||
usart_data_transmit(config->usart_periph, (uint16_t)ch);
|
||||
while (usart_flag_get(config->usart_periph, USART_FLAG_TBE) == RESET);
|
||||
}
|
||||
|
||||
// 发送字符串
|
||||
void gd32_usart_puts(struct gd32_usart_config *config, const char *str)
|
||||
{
|
||||
while (*str) {
|
||||
gd32_usart_putchar(config, *str++);
|
||||
}
|
||||
}
|
||||
|
||||
void usart_puts(const char *str)
|
||||
{
|
||||
gd32_usart_puts(&usart_config[0], str);
|
||||
|
||||
}
|
||||
|
||||
// 获取串口配置 by name
|
||||
struct gd32_usart_config *gd32_usart_get_config(const char *name)
|
||||
{
|
||||
for (int i = 0; i < sizeof(usart_config) / sizeof(usart_config[0]); i++) {
|
||||
if (rt_strcmp(usart_config[i].name, name) == 0) {
|
||||
return &usart_config[i];
|
||||
}
|
||||
}
|
||||
return RT_NULL;
|
||||
}
|
|
@ -1,30 +1,28 @@
|
|||
#ifndef __DRV_USART_H__
|
||||
#define __DRV_USART_H__
|
||||
#ifndef DRV_USART_H
|
||||
#define DRV_USART_H
|
||||
|
||||
#include <rtthread.h>
|
||||
#include <rtservice.h>
|
||||
#include "gd32f4xx.h"
|
||||
#include "gd32f4xx_usart.h"
|
||||
|
||||
// 串口配置结构体
|
||||
struct gd32_usart_config {
|
||||
const char *name;
|
||||
uint32_t usart_periph;
|
||||
uint32_t rcu_periph_clock;
|
||||
uint32_t gpio_port;
|
||||
uint32_t rcu_gpio_clock;
|
||||
uint32_t tx_pin;
|
||||
uint32_t rx_pin;
|
||||
uint32_t baud_rate;
|
||||
uint8_t irq_type;
|
||||
};
|
||||
#define USART_GPIO_PORT GPIOA
|
||||
#define USART_GPIO_CLK RCU_GPIOA
|
||||
#define USART_PERIPH USART0
|
||||
#define USART_PERIPH_CLK RCU_USART0
|
||||
|
||||
#define USART_TX_PIN GPIO_PIN_9
|
||||
#define USART_RX_PIN GPIO_PIN_10
|
||||
|
||||
#define USART_GPIO_AF GPIO_AF_7
|
||||
#define USART_IRQn USART0_IRQn
|
||||
// #define USART_IRQHandler USART0_IRQHandler
|
||||
// #define USART_IRQHandler_NAME "USART0_IRQHandler"
|
||||
#define USART_BAUD_RATE 9600
|
||||
|
||||
void usart_init(void);
|
||||
void usart_colok_config(void);
|
||||
void usart_gpio_config(void);
|
||||
// void usart_putc(struct rt_serial_device *serial, char ch);
|
||||
// void usart_getc(struct rt_serial_device *serial, char *ch);
|
||||
// void usart_enable(struct rt_serial_device *serial, rt_bool_t enable);
|
||||
|
||||
|
||||
// 函数声明
|
||||
void gd32_usart_init(void);
|
||||
void gd32_usart_configure(struct gd32_usart_config *config);
|
||||
void gd32_usart_putchar(struct gd32_usart_config *config, char ch);
|
||||
void gd32_usart_puts(struct gd32_usart_config *config, const char *str);
|
||||
struct gd32_usart_config *gd32_usart_get_config(const char *name);
|
||||
|
||||
#endif /* __DRV_USART_H__ */
|
||||
#endif
|
Loading…
Reference in New Issue