From a9599bae5ea9a47302e55439519b99a3879bb58a Mon Sep 17 00:00:00 2001 From: yelvlab Date: Tue, 1 Apr 2025 23:19:51 +0800 Subject: [PATCH] RS485 test --- src/led.c | 2 +- src/main.c | 7 +++++++ src/rs485.c | 20 +++++++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/led.c b/src/led.c index 31df075..df1704f 100644 --- a/src/led.c +++ b/src/led.c @@ -28,5 +28,5 @@ void led_config(void) { timer_enable(LED_BLINK_TIMER); - nvic_irq_enable(LED_BLINK_IRQ, 2); + nvic_irq_enable(LED_BLINK_IRQ, 3U); } diff --git a/src/main.c b/src/main.c index 85babea..6cf02e9 100644 --- a/src/main.c +++ b/src/main.c @@ -38,6 +38,13 @@ int main(void) { /* Initialize watchdog */ watchdog_init(); + // usart_rs485_driver_enable() + + // gpio_bit_set(RS485_GPIO_PORT, RS485_EN_PIN); + delay_ms(5); + printf("RS485 EN PIN RESET\n"); + // gpio_bit_set(RS485_GPIO_PORT, RS485_EN_PIN); + while (1) { delay_ms(99); fwdgt_counter_reload(); diff --git a/src/rs485.c b/src/rs485.c index 368f267..24da43b 100644 --- a/src/rs485.c +++ b/src/rs485.c @@ -11,24 +11,34 @@ void rs485_config(void) { rcu_periph_clock_enable(RS485_GPIO_RCU); rcu_periph_clock_enable(RS485_RCU); - gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, GPIO_PIN_2 | GPIO_PIN_3); + gpio_af_set(RS485_GPIO_PORT, GPIO_AF_1, RS485_TX_PIN | RS485_RX_PIN | RS485_EN_PIN); /* configure USART Tx&Rx as alternate function push-pull */ gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_TX_PIN | RS485_RX_PIN); gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_TX_PIN | RS485_RX_PIN); /* configure RS485 EN Pin */ - gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_EN_PIN); + gpio_mode_set(RS485_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, RS485_EN_PIN); gpio_output_options_set(RS485_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_EN_PIN); gpio_bit_write(RS485_GPIO_PORT, RS485_EN_PIN, SET); /* USART configure */ usart_deinit(RS485_PHY); usart_baudrate_set(RS485_PHY, RS485_BAUDRATE); - usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE); - usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE); + // usart_parity_config(RS485_PHY, USART_PM_NONE); + // usart_word_length_set(RS485_PHY, USART_WL_8BIT); + // usart_stop_bit_set(RS485_PHY, USART_STB_1BIT); + // usart_receive_config(RS485_PHY, USART_RECEIVE_ENABLE); + // usart_transmit_config(RS485_PHY, USART_TRANSMIT_ENABLE); - usart_enable(RS485_PHY); + usart_driver_assertime_config(RS485_PHY, 0x01); + usart_driver_deassertime_config(RS485_PHY, 0x01); + + // usart_depolarity_config(USART0, USART_DEP_HIGH); + // usart_depolarity_config(USART0, USART_DEP_LOW); + + // usart_enable(RS485_PHY); + usart_rs485_driver_enable(RS485_PHY); nvic_irq_enable(USART0_IRQn, 0); usart_interrupt_enable(RS485_PHY, USART_INT_RBNE);