]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/Driver/src/USART_Demo.c
CMSIS-Driver WiFi documentation typo correction
[cmsis] / CMSIS / DoxyGen / Driver / src / USART_Demo.c
1 #include "Driver_USART.h"
2 #include "cmsis_os.h"                   /* ARM::CMSIS:RTOS:Keil RTX */
3 #include <stdio.h>
4 #include <string.h>
5  
6 void myUART_Thread(void const *argument);
7 osThreadId tid_myUART_Thread;
8  
9 /* USART Driver */
10 extern ARM_DRIVER_USART Driver_USART3;
11  
12  
13 void myUSART_callback(uint32_t event)
14 {
15   uint32_t mask;
16
17   mask = ARM_USART_EVENT_RECEIVE_COMPLETE  |
18          ARM_USART_EVENT_TRANSFER_COMPLETE |
19          ARM_USART_EVENT_SEND_COMPLETE     |
20          ARM_USART_EVENT_TX_COMPLETE       ;
21
22   if (event & mask) {
23     /* Success: Wakeup Thread */
24     osSignalSet(tid_myUART_Thread, 0x01);
25   }
26
27   if (event & ARM_USART_EVENT_RX_TIMEOUT) {
28     __breakpoint(0);  /* Error: Call debugger or replace with custom error handling */
29   }
30
31   if (event & (ARM_USART_EVENT_RX_OVERFLOW | ARM_USART_EVENT_TX_UNDERFLOW)) {
32     __breakpoint(0);  /* Error: Call debugger or replace with custom error handling */
33   }
34 }
35
36  
37 /* CMSIS-RTOS Thread - UART command thread */
38 void myUART_Thread(const void* args)
39 {
40     static ARM_DRIVER_USART * USARTdrv = &Driver_USART3;
41     ARM_DRIVER_VERSION     version;
42     ARM_USART_CAPABILITIES drv_capabilities;
43     char                   cmd;
44  
45   #ifdef DEBUG
46     version = USARTdrv->GetVersion();
47     if (version.api < 0x200)   /* requires at minimum API version 2.00 or higher */
48     {                          /* error handling */
49         return;
50     }
51     drv_capabilities = USARTdrv->GetCapabilities();
52     if (drv_capabilities.event_tx_complete == 0)
53     {                          /* error handling */
54         return;
55     }
56   #endif
57  
58     /*Initialize the USART driver */
59     USARTdrv->Initialize(myUSART_callback);
60     /*Power up the USART peripheral */
61     USARTdrv->PowerControl(ARM_POWER_FULL);
62     /*Configure the USART to 4800 Bits/sec */
63     USARTdrv->Control(ARM_USART_MODE_ASYNCHRONOUS |
64                       ARM_USART_DATA_BITS_8 |
65                       ARM_USART_PARITY_NONE |
66                       ARM_USART_STOP_BITS_1 |
67                       ARM_USART_FLOW_CONTROL_NONE, 4800);
68      
69     /* Enable Receiver and Transmitter lines */
70     USARTdrv->Control (ARM_USART_CONTROL_TX, 1);
71     USARTdrv->Control (ARM_USART_CONTROL_RX, 1);
72  
73     USARTdrv->Send("\nPress Enter to receive a message", 34);
74     osSignalWait(0x01, osWaitForever);
75      
76     while (1)
77     {
78         USARTdrv->Receive(&cmd, 1);          /* Get byte from UART */
79         osSignalWait(0x01, osWaitForever);
80         if (cmd == 13)                       /* CR, send greeting  */
81         {
82           USARTdrv->Send("\nHello World!", 12);
83           osSignalWait(0x01, osWaitForever);
84         }
85  
86     }
87 }