使用 PIO 串口(还未测试)

This commit is contained in:
2024-03-31 18:04:35 +08:00
parent d329c022b5
commit 7f9fc40757
7 changed files with 443 additions and 14 deletions

78
main.c
View File

@@ -1,5 +1,64 @@
#include "main.h"
int IM1253B_INIT(void)
{
/*
// 初始化UART
uart_init(UART0, BAUD_RATE);
gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART0, false, false);
uart_set_format(UART0, DATA_BITS, STOP_BITS, PARITY);
sleep_ms(10);
*/
uint tx_offset = pio_add_program(IM1253B_PIO, &uart_tx_program);
uart_tx_program_init(IM1253B_PIO, IM1253B_PIO_SM_TX, tx_offset, IM1253B_PIO_TX_PIN, IM1253B_PIO_SERIAL_BAUD);
uint rx_offset = pio_add_program(IM1253B_PIO, &uart_rx_program);
uart_rx_program_init(IM1253B_PIO, IM1253B_PIO_SM_RX, rx_offset, IM1253B_PIO_RX_PIN, IM1253B_PIO_SERIAL_BAUD);
return 0;
}
int IM1253B_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) {
for (int i = 0; i < DATA_LEN; i++) {
uart_tx_program_putc(pio, sm, DATA[i]);
sleep_ms(1);
}
return 0;
}
int IM1253B_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t *DATA, int DATA_LEN) {
char c = '\0';
int received_count = 0;
int timeout_ms = 100; // 设置较长的超时时间
while (received_count < DATA_LEN && timeout_ms > 0) {
if (uart_rx_program_available(pio, sm)) {
c = uart_rx_program_getc(pio, sm);
DATA[received_count++] = c;
printf("0x%X ", c);
if (c == '\n') {
// 接收到换行符,停止接收数据
break;
}
} else {
// 没有接收到数据,继续等待
sleep_ms(1);
timeout_ms--;
}
}
if (received_count == 0) {
// 没有接收到有效数据,可以进行相应的处理
return -1;
}
return received_count;
}
unsigned int calccrc(unsigned char crcbuf, unsigned int crc)
{
unsigned char i;
@@ -54,14 +113,16 @@ void read_data(void)
Tx_Buffer[7] = crcnow.byte[0];
// 发送数据
uart_write_blocking(UART0, Tx_Buffer, 8);
//uart_write_blocking(UART0, Tx_Buffer, 8);
IM1253B_PIO_UART_TX_DATA(IM1253B_PIO, IM1253B_PIO_SM_TX, Tx_Buffer, 8);
sleep_ms(10);
// 接收数据
uint8_t _DATA[37] = { 0 };
uart_read_blocking(UART0, _DATA, 37);
//uart_read_blocking(UART0, _DATA, 37);
IM1253B_PIO_UART_RX_DATA(IM1253B_PIO, IM1253B_PIO_SM_RX, _DATA, 37);
for (i = 0; i <= 37 - 1; i++) {
//printf("0x%X ", _DATA[i]);
printf("0x%X ", _DATA[i]);
Rx_Buffer[i] = _DATA[i];
}
sleep_ms(10);
@@ -152,14 +213,7 @@ static uint16_t IM1253B(void)
{
static int i = 0;
// 初始化UART
uart_init(UART0, BAUD_RATE);
gpio_set_function(UART0_TX_PIN, GPIO_FUNC_UART);
gpio_set_function(UART0_RX_PIN, GPIO_FUNC_UART);
uart_set_hw_flow(UART0, false, false);
uart_set_format(UART0, DATA_BITS, STOP_BITS, PARITY);
sleep_ms(10);
/*
// 官方软件内部按钮发送的数据
// 电能清零 TX[13]:01 10 00 4B 00 02 04 00 00 00 00 B6 2C
@@ -211,6 +265,8 @@ int main(void)
sec = 0;
printf("IM1253B Module\n");
IM1253B_INIT();
while (1) {
watchdog_update(); // 喂狗