miniFOC 1.0.3
This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.
uart.c
Go to the documentation of this file.
1/**************************************************************************/
11#include "uart.h"
12#include "config.h"
13#include "gd32f1x0.h"
14#include "system.h"
15
22volatile static unsigned char mdtp_receive_status = 0;
26volatile static unsigned char mdtp_receive_number_counter = 0;
30static unsigned char mdtp_receive_data_buffer[10] = {0};
31
36void mdtp_receive_handler(unsigned char data) {
37 /* data receiving finite state machine */
38 switch (mdtp_receive_status) {
39 case 0:
40 /* judge whether the packet header is received */
41 if (data == 0xff) {
42 /* enter the receive state */
44
45 /* clear receive array counter and buffer */
48 }
49 break;
50
51 case 1:
52 /* judge whether the end of the packet is mistakenly recognized as the header */
53 if (data == 0xff && mdtp_receive_number_counter != 0) {
54 /* an unexpected data had been received */
55 /* reset to receive start of package state */
57
58 /* clear receive array counter and buffer */
61 } else if (data != 0xff) {
62 /* judge whether the reception is completed or the error data is received */
64 /* receive the data into the array in turn */
69 }
70 }
71 break;
72
73 case 2:
74 if (data == 0xff) {
76
77 /* verify whether the pid byte is correct*/
78 if ((mdtp_receive_data_buffer[0] >> 4) == (~mdtp_receive_data_buffer[0] & 0x0f)) {
79 unsigned char tmp_rcv_buffer[8], counter = 0;
80 /* judge whether the package content is all 0xff */
81 if (mdtp_receive_data_buffer[1] == 0xa5 && mdtp_receive_data_buffer[9] == 0xa5)
82 user_memset(tmp_rcv_buffer, 0xff, sizeof(tmp_rcv_buffer));
83 else {
84 /* traverse the data byte to be adjusted */
85 for (; counter < 8; ++counter)
86 if (((mdtp_receive_data_buffer[9] >> counter) & 0x01) == 0x01)
87 tmp_rcv_buffer[counter] = 0xff;
88 else
89 tmp_rcv_buffer[counter] = mdtp_receive_data_buffer[counter + 1];
90 }
91 /* call user callback function to complete the next step */
92 mdtp_callback_handler(mdtp_receive_data_buffer[0] >> 4, tmp_rcv_buffer);
93 }
94 } else {
95 /* an unexpected data had been received */
96 /* reset to receive start of package state */
98
99 /* clear receive array counter and buffer */
102 }
103 break;
104 default:break;
105 }
106}
107
113void mdtp_data_transmit(unsigned char pid, const unsigned char *buffer) {
114 unsigned char temp_buf[12] = {0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
115 0x00, 0x00, 0x00, 0x00, 0x00, 0xff};
116 unsigned char mdtp_pack_counter;
117
118 /* traverse the array to determine whether there are bytes to be adjusted */
119 for (mdtp_pack_counter = 0; mdtp_pack_counter < 8; mdtp_pack_counter++) {
120 if (buffer[mdtp_pack_counter] == 0xff) {
121 temp_buf[2 + mdtp_pack_counter] = 0x00;
122 temp_buf[10] = (temp_buf[10] | (1 << mdtp_pack_counter));
123 } else
124 temp_buf[2 + mdtp_pack_counter] = buffer[mdtp_pack_counter];
125 }
126
127 /* judge whether the package is all 0xff */
128 if (temp_buf[10] == 0xff)
129 temp_buf[10] = temp_buf[2] = 0xa5;
130
131 /* load self checking packet id byte */
132 temp_buf[1] = pid << 4 | ((~pid) & 0x0f);
133
134 /* traverse the buffer array and send all bytes through UART0 */
135 for (mdtp_pack_counter = 0; mdtp_pack_counter < 12;
136 mdtp_pack_counter++) { uart_sendbyte(temp_buf[mdtp_pack_counter]);
137 }
138}
139
143void uart_config(void) {
144 /* UART interrupt configuration */
145 nvic_irq_enable(USART0_IRQn, UART_PRIORITY, 0);
146
147 /* enable GPIO clock and UART clock*/
148 rcu_periph_clock_enable(RCU_GPIOA);
149 rcu_periph_clock_enable(RCU_USART0);
150
151 /* connect port to UARTx */
152 gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
153 gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
154
155 /* configure UART Tx as alternate function push-pull */
156 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
157 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
158
159 /* configure UART Rx as alternate function push-pull */
160 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
161 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
162
163 /* UART configure */
164 usart_deinit(USART0);
165 usart_baudrate_set(USART0, UART_BAUDRATE);
166 usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
167 usart_receive_config(USART0, USART_RECEIVE_ENABLE);
168 usart_enable(USART0);
169
170 /* enable UART RBNE interrupt */
171 usart_interrupt_enable(USART0, USART_INT_RBNE);
172}
used to place important parameter configurations for users
#define UART_BAUDRATE
set UART baud rate to 512000
Definition: config.h:24
#define UART_PRIORITY
UART preemption priority set to 1.
Definition: config.h:37
void user_memset(void *buf, unsigned char data, unsigned char num)
format buffer array to fixed value, replace memset function
Definition: system.c:49
system basic function header file
static volatile unsigned char mdtp_receive_number_counter
medium capacity transport protocol receive character counter
Definition: uart.c:26
static unsigned char mdtp_receive_data_buffer[10]
medium capacity transport protocol receive buffer array
Definition: uart.c:30
void mdtp_receive_handler(unsigned char data)
medium capacity data transmission protocol unpacking handler
Definition: uart.c:36
void mdtp_data_transmit(unsigned char pid, const unsigned char *buffer)
medium capacity data transmission protocol packing function
Definition: uart.c:113
static volatile unsigned char mdtp_receive_status
medium capacity transport protocol receive state variable 0 idle state and waiting for start of packa...
Definition: uart.c:22
void uart_config(void)
configure uart0 periph and its gpios
Definition: uart.c:143
this is the header file of uart.c.
void mdtp_callback_handler(unsigned char pid, const unsigned char *data)
user callback function for unpacking completion of medium capacity transport protocol
Definition: main.c:28
#define uart_sendbyte(x)
UART send single byte macro.
Definition: uart.h:16