volcanol的工控博客

  1 #include "stm32f10x.h"
  2 #include "serial.h"
  3 #include "rtthread.h"
  4 #include <rtdevice.h>
  5 /***********************************************************************************************************
  6 @   pin config  USART1_REMAP = 0                                                
  7 @____________________________________________________________________________*/
  8 
  9 #define UART1_GPIO_TX                           GPIO_Pin_9
 10 #define UART1_GPIO_RX                           GPIO_Pin_10
 11 #define UART1_GPIO                              GPIOA
 12 #define RCC_APBPeriph_UART1                 RCC_APB2Periph_USART1
 13 #define UART1_TX_DMA                            DMA1_Channel4
 14 #define UART1_RX_DMA                            DMA1_Channel5
 15 
 16 /***********************************************************************************************************
 17 @   Struct Definition   
 18 @____________________________________________________________________________*/
 19 struct serial_ringbuffer serial_int_rx_buffer;
 20 struct serial_ringbuffer serial_int_tx_buffer;
 21 struct rt_serial_device serialx_device;
 22 struct serial_user_data
 23 {
 24     USART_TypeDef* uart_device;
 25     const char name[RT_NAME_MAX];
 26 };
 27 
 28 
 29 
 30 /***********************************************************************************************************
 31 @   Hardware clock configuration
 32 @____________________________________________________________________________*/
 33 static void RCC_Configuration(void)
 34 {
 35     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
 36 
 37     /* Enable USART1 and GPIOA clocks */
 38     RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
 39 
 40 }
 41 
 42 static void GPIO_Configuration(void)
 43 {
 44     GPIO_InitTypeDef GPIO_InitStructure;
 45 
 46     /* Configure USART1 Rx (PA.10) as input floating */
 47     GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
 48     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 49     GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
 50 
 51     /* Configure USART1 Tx (PA.09) as alternate function push-pull */
 52     GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
 53     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 54     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 55     GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
 56 
 57 }
 58 
 59 static void NVIC_Configuration(void)
 60 {
 61     NVIC_InitTypeDef NVIC_InitStructure;
 62 
 63     /* Enable the USART1 Interrupt */
 64     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
 65     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
 66     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
 67     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
 68     NVIC_Init(&NVIC_InitStructure);
 69 
 70 
 71 }
 72 
 73 
 74 
 75 
 76 
 77 /***********************************************************************************************************
 78 @   model driven architecture interface
 79 @____________________________________________________________________________*/
 80  int serial_put_char(struct rt_serial_device *serial, char c)
 81  {
 82     USART_ClearFlag(USART1,USART_FLAG_TC);
 83     USART_SendData(USART1, (u8) c);
 84      while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
 85      return c;
 86  }
 87  
 88 int serial_get_char(struct rt_serial_device *serial)
 89  {  
 90     int  ch = -1;
 91     struct serial_user_data* user = (struct serial_user_data *)(serial->parent.user_data);
 92  
 93 
 94     if(USART_GetITStatus(user->uart_device, USART_IT_RXNE) != RESET)
 95     {
 96         /* interrupt mode receive */
 97         RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
 98 
 99         ch = USART_ReceiveData(user->uart_device);
100         
101         /* clear interrupt */
102         USART_ClearITPendingBit(user->uart_device, USART_IT_RXNE);
103     }
104     return ch;
105  }
106 
107  rt_err_t serial_control(struct rt_serial_device *serial, int cmd, void *arg)
108  {
109     u16     int_flag;                               //interrupt flag
110     FunctionalState     NewState;
111     struct serial_user_data *user = (struct serial_user_data *)serial->parent.user_data;
112 
113     switch(*(rt_uint32_t *)arg)
114     {
115         case RT_SERIAL_RX_INT:
116         {
117             int_flag = USART_IT_RXNE;
118             break;
119         }
120         case RT_SERIAL_TX_INT:
121         {
122             int_flag = USART_IT_TC;
123             break;
124         }
125         default :
126         {
127             break;
128         }
129     }
130     
131     switch(cmd)
132     {
133         case RT_DEVICE_CTRL_SET_INT:
134         {
135             NewState = ENABLE;
136             break;
137         }
138         case RT_DEVICE_CTRL_CLR_INT:
139         {
140             NewState = DISABLE;
141             break;
142         }
143         default:
144         {
145             break;
146         }
147     }
148     USART_ITConfig(user->uart_device, int_flag, NewState);
149  }
150 
151  rt_size_t serial_dma_transmit(struct rt_serial_device *serial, const char *buf, rt_size_t size)
152  {
153 
154  }
155 /***********************************************************************************************************
156 @Struct declaration 
157 @____________________________________________________________________________*/
158 struct serial_configure   serial_config = 
159 {
160     115200,
161     8,
162     1,
163     0,
164     0,
165     0,
166     0
167 };
168 struct serial_user_data serial_user_struct= 
169 {
170     USART1,
171     "usart1"
172 };
173 rt_err_t stm32_serial_config(struct rt_serial_device *serial, struct serial_configure *cfg);
174 struct rt_uart_ops  serial_ops = 
175 {
176     stm32_serial_config,
177     serial_control,
178     serial_put_char,
179     serial_get_char,
180     serial_dma_transmit 
181 };
182 
183 
184 rt_err_t stm32_serial_config(struct rt_serial_device *serial, struct serial_configure *cfg)
185 {
186     USART_InitTypeDef       USART_InitStructure;
187     USART_ClockInitTypeDef  USART_ClockInitStructure;
188     struct serial_user_data* user = (struct serial_user_data *)serial->parent.user_data;
189     
190 
191     RCC_Configuration();
192 
193     GPIO_Configuration();
194 
195     NVIC_Configuration();
196 /*
197     serial->config = serial_config;
198     serial->int_rx = &serial_int_rx_buffer;
199     serial->int_tx = &serial_int_tx_buffer;
200     serial->ops = &serial_ops;
201 */
202     USART_InitStructure.USART_BaudRate      = 9600;
203     USART_InitStructure.USART_WordLength    = USART_WordLength_8b;
204     USART_InitStructure.USART_StopBits          = USART_StopBits_1;
205     USART_InitStructure.USART_Parity                = USART_Parity_No;
206     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
207     USART_InitStructure.USART_Mode              = USART_Mode_Rx | USART_Mode_Tx;
208     USART_ClockInitStructure.USART_Clock        = USART_Clock_Disable;
209     USART_ClockInitStructure.USART_CPOL         = USART_CPOL_Low;
210     USART_ClockInitStructure.USART_CPHA         = USART_CPHA_2Edge;
211     USART_ClockInitStructure.USART_LastBit  = USART_LastBit_Disable;
212 
213     USART_Init(user->uart_device, &USART_InitStructure);
214     USART_ClockInit(user->uart_device, &USART_ClockInitStructure);  
215          
216 
217 //  rt_hw_serial_register(serial, user->name,
218 //  RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
219 //  user);
220 
221     /* enable interrupt */
222     USART_ITConfig(user->uart_device, USART_IT_RXNE, ENABLE);
223     
224     return RT_EOK;
225 }
226 
227 
228 
229 
230 
231 void rt_hw_usart2_init(void)
232 {
233  /* USART_InitTypeDef       USART_InitStructure;
234     USART_ClockInitTypeDef  USART_ClockInitStructure;
235     
236     RCC_Configuration();
237 
238     GPIO_Configuration();
239 
240     NVIC_Configuration();
241 */
242     serialx_device.config = serial_config;
243     serialx_device.int_rx = &serial_int_rx_buffer;
244     serialx_device.int_tx = &serial_int_tx_buffer;
245     serialx_device.ops = &serial_ops;
246 /*
247     USART_InitStructure.USART_BaudRate = 115200;
248     USART_InitStructure.USART_WordLength = USART_WordLength_8b;
249     USART_InitStructure.USART_StopBits = USART_StopBits_1;
250     USART_InitStructure.USART_Parity = USART_Parity_No;
251     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
252     USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
253     USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
254     USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
255     USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
256     USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
257 
258     USART_Init(serial_user_struct.uart_device, &USART_InitStructure);
259     USART_ClockInit(serial_user_struct.uart_device, &USART_ClockInitStructure); 
260      
261 */
262     rt_hw_serial_register(&serialx_device, serial_user_struct.name,
263     RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
264     &serial_user_struct);
265 
266     /* enable interrupt */
267     //USART_ITConfig(serial_user_struct.uart_device, USART_IT_RXNE, ENABLE);
268     
269     //stm32_serial_config(&serialx_device,&serial_config);
270 }
271  

下面是第二份代码:

  1 #include "stm32f10x.h"
  2 #include "serial.h"
  3 #include "rtthread.h"
  4 #include <rtdevice.h>
  5 /***********************************************************************************************************
  6 @   pin config  USART1_REMAP = 0                                                
  7 @____________________________________________________________________________*/
  8 
  9 /* USART1_REMAP = 0 */
 10 #define UART1_GPIO_TX                   GPIO_Pin_9
 11 #define UART1_GPIO_RX                   GPIO_Pin_10
 12 #define UART1_GPIO                          GPIOA
 13 #define RCC_APBPeriph_UART1         RCC_APB2Periph_USART1
 14 #define USART1_TX_DMA                   DMA1_Channel4
 15 #define USART1_RX_DMA                   DMA1_Channel5
 16 
 17 #if defined(STM32F10X_LD) || defined(STM32F10X_MD) || defined(STM32F10X_CL)
 18 #define UART2_GPIO_TX                   GPIO_Pin_5
 19 #define UART2_GPIO_RX                   GPIO_Pin_6
 20 #define UART2_GPIO                      GPIOD
 21 #define RCC_APBPeriph_UART2         RCC_APB1Periph_USART2
 22 #else /* for STM32F10X_HD */
 23 /* USART2_REMAP = 0 */
 24 #define UART2_GPIO_TX                   GPIO_Pin_2
 25 #define UART2_GPIO_RX                   GPIO_Pin_3
 26 #define UART2_GPIO                          GPIOA
 27 #define RCC_APBPeriph_UART2     RCC_APB1Periph_USART2
 28 #define UART2_TX_DMA                        DMA1_Channel7
 29 #define UART2_RX_DMA                        DMA1_Channel6
 30 #endif
 31 
 32 /* USART3_REMAP[1:0] = 00 */
 33 #define UART3_GPIO_RX                   GPIO_Pin_11
 34 #define UART3_GPIO_TX                   GPIO_Pin_10
 35 #define UART3_GPIO                          GPIOB
 36 #define RCC_APBPeriph_UART3     RCC_APB1Periph_USART3
 37 #define UART3_TX_DMA                        DMA1_Channel2
 38 #define UART3_RX_DMA                        DMA1_Channel3
 39 
 40 /***********************************************************************************************************
 41 @   Struct Definition   
 42 @____________________________________________________________________________*/
 43 struct serial_user_data
 44 {
 45     USART_TypeDef* uart_device;
 46     const char name[RT_NAME_MAX];
 47 };
 48 
 49 
 50 
 51 /***********************************************************************************************************
 52 @   Hardware clock configuration
 53 @____________________________________________________________________________*/
 54 static void RCC_Configuration(void)
 55 {
 56     RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
 57 
 58     /* Enable USART1 and GPIOA clocks */
 59     RCC_APB2PeriphClockCmd(RCC_APBPeriph_UART1 | RCC_APB2Periph_GPIOA, ENABLE);
 60 
 61     /* Enable AFIO and GPIOD clock */
 62     RCC_APB1PeriphClockCmd(RCC_APBPeriph_UART2, ENABLE);
 63 
 64 }
 65 
 66 static void GPIO_Configuration(void)
 67 {
 68     GPIO_InitTypeDef GPIO_InitStructure;
 69 
 70     /* Configure USART1 Rx (PA.10) as input floating */
 71     GPIO_InitStructure.GPIO_Pin = UART1_GPIO_RX;
 72     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 73     GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
 74 
 75     /* Configure USART1 Tx (PA.09) as alternate function push-pull */
 76     GPIO_InitStructure.GPIO_Pin = UART1_GPIO_TX;
 77     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 78     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 79     GPIO_Init(UART1_GPIO, &GPIO_InitStructure);
 80 
 81     /* Configure USART2 Rx as input floating */
 82     GPIO_InitStructure.GPIO_Pin = UART2_GPIO_RX;
 83     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
 84     GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
 85 
 86     /* Configure USART2 Tx as alternate function push-pull */
 87     GPIO_InitStructure.GPIO_Pin = UART2_GPIO_TX;
 88     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
 89     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 90     GPIO_Init(UART2_GPIO, &GPIO_InitStructure);
 91 
 92 }
 93 
 94 static void NVIC_Configuration(void)
 95 {
 96     NVIC_InitTypeDef NVIC_InitStructure;
 97 
 98     /* Enable the USART1 Interrupt */
 99     NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
100     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
101     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
102     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
103     NVIC_Init(&NVIC_InitStructure);
104 
105     /* Enable the USART2 Interrupt */
106     NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
107     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
108     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
109     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
110     NVIC_Init(&NVIC_InitStructure);
111 
112 
113 }
114 
115 static void DMA_Configuration(void)
116 {
117 #if defined (RT_USING_UART3)
118     DMA_InitTypeDef DMA_InitStructure;
119 
120     /* fill init structure */
121     DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
122     DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
123     DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
124     DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
125     DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
126     DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
127     DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
128 
129     /* DMA1 Channel5 (triggered by USART3 Tx event) Config */
130     DMA_DeInit(UART3_TX_DMA);
131     DMA_InitStructure.DMA_PeripheralBaseAddr = USART3_DR_Base;
132     DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
133     /* As we will set them before DMA actually enabled, the DMA_MemoryBaseAddr
134      * and DMA_BufferSize are meaningless. So just set them to proper values
135      * which could make DMA_Init happy.
136      */
137     DMA_InitStructure.DMA_MemoryBaseAddr = (u32)0;
138     DMA_InitStructure.DMA_BufferSize = 1;
139     DMA_Init(UART3_TX_DMA, &DMA_InitStructure);
140     DMA_ITConfig(UART3_TX_DMA, DMA_IT_TC | DMA_IT_TE, ENABLE);
141     DMA_ClearFlag(DMA1_FLAG_TC2);
142 #endif
143 }
144 
145 
146 
147 
148 /***********************************************************************************************************
149 @   model driven architecture interface
150 @____________________________________________________________________________*/
151 rt_err_t stm32_serial_config(struct rt_serial_device *serial, struct serial_configure *cfg);
152 
153  int serial_put_char(struct rt_serial_device *serial, char c)
154  {
155     struct serial_user_data* user = (struct serial_user_data *)(serial->parent.user_data);
156     
157     USART_ClearFlag(user->uart_device,USART_FLAG_TC);
158     USART_SendData(user->uart_device,  c);
159      while (USART_GetFlagStatus(user->uart_device, USART_FLAG_TC) == RESET);
160      
161      return c;
162  }
163  
164 int serial_get_char(struct rt_serial_device *serial)
165  {  
166     int  ch = -1;
167     struct serial_user_data* user = (struct serial_user_data *)(serial->parent.user_data);
168  
169 
170     if(USART_GetITStatus(user->uart_device, USART_IT_RXNE) != RESET)
171     {
172         /* interrupt mode receive */
173         RT_ASSERT(serial->parent.flag & RT_DEVICE_FLAG_INT_RX);
174 
175         ch = USART_ReceiveData(user->uart_device);
176         
177         /* clear interrupt */
178         USART_ClearITPendingBit(user->uart_device, USART_IT_RXNE);
179     }
180     return ch;
181  }
182 
183  rt_err_t serial_control(struct rt_serial_device *serial, int cmd, void *arg)
184  {
185     FunctionalState     NewState;
186      
187     struct serial_user_data *user = (struct serial_user_data *)serial->parent.user_data;
188 
189     switch(cmd)
190     {
191         case RT_DEVICE_CTRL_SET_INT:
192         {
193             NewState = ENABLE;
194             break;
195         }
196         case RT_DEVICE_CTRL_CLR_INT:
197         {
198             NewState = DISABLE;
199             break;
200         }
201         default:
202         {
203             break;
204         }
205     }
206     
207     switch(*(rt_uint32_t *)arg)
208     {
209         case RT_SERIAL_RX_INT:
210         {
211             USART_ITConfig(user->uart_device, USART_IT_RXNE, NewState);
212             break;
213         }
214         case RT_SERIAL_TX_INT:
215         {
216             USART_ITConfig(user->uart_device, USART_IT_TC, NewState);
217             break;
218         }
219         default :
220         {
221             break;
222         }
223     }
224     
225     return RT_EOK;
226  }
227 
228  rt_size_t serial_dma_transmit(struct rt_serial_device *serial, const char *buf, rt_size_t size)
229  {
230         return size;
231  }
232 
233 rt_err_t stm32_serial_config(struct rt_serial_device *serial, struct serial_configure *cfg)
234 {
235     USART_InitTypeDef               USART_InitStructure;
236     USART_ClockInitTypeDef      USART_ClockInitStructure;
237     struct serial_user_data*        user = (struct serial_user_data *)serial->parent.user_data;
238 
239     
240     RCC_Configuration();
241     GPIO_Configuration();
242     NVIC_Configuration();
243     DMA_Configuration();
244 
245     USART_InitStructure.USART_BaudRate      = cfg->baud_rate;
246     
247     switch(cfg->data_bits)
248     {
249         case 8:
250         {
251             USART_InitStructure.USART_WordLength    = USART_WordLength_8b;
252             break;
253         }
254         case 9:
255         {
256             USART_InitStructure.USART_WordLength    = USART_WordLength_9b;
257             break;
258         }
259         default :
260         {
261 #ifndef RT_USING_FINSH
262             rt_kprintf("data bit set error
");
263 #endif
264             break;
265         }
266     }
267 
268     switch(cfg->stop_bits)
269     {
270         case 1:
271         {
272             USART_InitStructure.USART_StopBits  = USART_StopBits_1;
273             break;
274         }
275         case 2:
276         {
277             USART_InitStructure.USART_StopBits  = USART_StopBits_2;
278             break;
279         }
280         case 3:
281         {
282             USART_InitStructure.USART_StopBits  = USART_StopBits_0_5;
283             break;
284         }
285         case 4:
286         {
287             USART_InitStructure.USART_StopBits  = USART_StopBits_1_5;
288             break;
289         }
290         default :
291         {
292 #ifndef RT_USING_FINSH
293             rt_kprintf("stopbits bit set error
");
294 #endif
295             break;
296         }
297     }
298 
299     switch(cfg->parity)
300     {
301         case 0:
302         {
303             USART_InitStructure.USART_Parity    = USART_Parity_No;
304             break;
305         }
306         case 1:
307         {
308             USART_InitStructure.USART_Parity    = USART_Parity_Odd;
309             break;
310         }
311         case 2:
312         {
313             USART_InitStructure.USART_Parity    = USART_Parity_Even;
314             break;
315         }
316         default :
317         {
318 #ifndef RT_USING_FINSH
319             rt_kprintf("data bit set error
");
320 #endif
321             break;
322         }
323     }
324     
325     USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
326     USART_InitStructure.USART_Mode              = USART_Mode_Rx | USART_Mode_Tx;
327     USART_ClockInitStructure.USART_Clock        = USART_Clock_Disable;
328     USART_ClockInitStructure.USART_CPOL         = USART_CPOL_Low;
329     USART_ClockInitStructure.USART_CPHA         = USART_CPHA_2Edge;
330     USART_ClockInitStructure.USART_LastBit  = USART_LastBit_Disable;
331 
332     USART_Init(user->uart_device, &USART_InitStructure);
333     USART_ClockInit(user->uart_device, &USART_ClockInitStructure);  
334 
335     /* enable interrupt */
336     USART_ITConfig(user->uart_device, USART_IT_RXNE, ENABLE);
337 
338     return RT_EOK;
339 }
340 
341 
342 /***********************************************************************************************************
343 @serial private function
344 @____________________________________________________________________________*/
345 struct rt_uart_ops  serial_ops = 
346 {
347     stm32_serial_config,
348     serial_control,
349     serial_put_char,
350     serial_get_char,
351     serial_dma_transmit 
352 };
353 /***********************************************************************************************************
354 @Struct declaration 
355 @____________________________________________________________________________*/
356 struct serial_configure   serial_config = 
357 {
358     115200,             //USART_BaudRate
359     8,                          //USART_WordLength
360     1,                          //USART_StopBits
361     0,                          //USART_Parity
362     0,                          //
363     0,                          //
364     0                           //
365 };
366 struct serial_user_data serial_user_struct = 
367 {
368     USART2,             //hardware device
369     "uart2"             //device name
370 };
371 struct serial_ringbuffer serial_int_rx_buffer;
372 struct serial_ringbuffer serial_int_tx_buffer;
373 struct rt_serial_device serialx_device;
374 
375 void rt_hw_serialx_register(void)
376 {
377     serialx_device.config = serial_config;
378     serialx_device.int_rx = &serial_int_rx_buffer;
379     serialx_device.int_tx = &serial_int_tx_buffer;
380     serialx_device.ops = &serial_ops;
381 
382     rt_hw_serial_register(&serialx_device, serial_user_struct.name,
383     RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
384     &serial_user_struct);
385 }
386 
387 
388 
389 
390 
391 
392 
393 
394 struct serial_user_data serial_user_struct1 = 
395 {
396     USART1,             //hardware device
397     "uart1"                 //device name
398 };
399 struct rt_serial_device serialx_device1;
400 struct serial_ringbuffer serial_int_rx_buffer1;
401 struct serial_ringbuffer serial_int_tx_buffer1;
402 void rt_hw_serialx_register1(void)
403 {
404     serialx_device1.config = serial_config;
405     serialx_device1.int_rx = &serial_int_rx_buffer1;
406     serialx_device1.int_tx = &serial_int_tx_buffer1;
407     serialx_device1.ops = &serial_ops;
408 
409     rt_hw_serial_register(&serialx_device1, serial_user_struct1.name,
410     RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM,
411     &serial_user_struct1);
412 }
413 
414 
415 
416 #ifdef RT_USING_FINSH
417 #include <finsh.h>
418 static rt_uint8_t led_inited = 0;
419 void usarts2(char *str)
420 {
421     rt_device_t usart;
422 
423     usart = rt_device_find("uart2");
424     rt_device_write(usart,0,str,20);
425 }
426 FINSH_FUNCTION_EXPORT(usarts2,str)
427 #endif
428 
429 
430  
原文地址:https://www.cnblogs.com/volcanol/p/6962221.html