1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
#include "at32f403a_407_board.h"
#include "at32f403a_407_clock.h"
#define RS485_BAUDRATE 9600
#define RS485_BUFFER_SIZE 128
static void rs485_config(void)
{
gpio_init_type gpio_init_struct;
/* enable the uart2 and gpio clock */
crm_periph_clock_enable(CRM_USART2_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
/* configure the uart2 tx,rx,de pin */
gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
gpio_init_struct.gpio_pins = GPIO_PINS_2;
gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
gpio_init(GPIOA, &gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_3;
gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
gpio_init_struct.gpio_pull = GPIO_PULL_UP;
gpio_init(GPIOA, &gpio_init_struct);
gpio_init_struct.gpio_pins = GPIO_PINS_1;
gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
gpio_init(GPIOA, &gpio_init_struct);
gpio_bits_reset(GPIOA, GPIO_PINS_1);
/* configure uart2 param */
usart_init(USART2, RS485_BAUDRATE, USART_DATA_8BITS, USART_STOP_1_BIT);
usart_flag_clear(USART2, USART_RDBF_FLAG);
usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
usart_receiver_enable(USART2, TRUE);
usart_transmitter_enable(USART2, TRUE);
usart_enable(USART2, TRUE);
nvic_irq_enable(USART2_IRQn, 1, 0);
}
int main(void)
{
char str[]="start test..\r\n";
u8 len = 0;
system_clock_config();
at32_board_init();
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
rs485_config();
len = sizeof(str);
rs485_send_data((u8*)str, len);
while(1)
{
if(usart_flag_get(USART2, USART_IDLEF_FLAG) != RESET)
{
usart_data_receive(USART2);
usart_interrupt_enable(USART2, USART_RDBF_INT, FALSE);
rs485_send_data(rs485_buffer_rx, rs485_buffer_rx_cnt);
rs485_buffer_rx_cnt = 0;
usart_interrupt_enable(USART2, USART_RDBF_INT, TRUE);
}
}
}
|