1 | //-------------------------------------------STM32F4_USART-------------------------------------------------------
|
2 |
|
3 | /* Beschreibung:
|
4 | * Ganze Strings mit Endezeichen werden interruptgesteuert empfangen und dann zurückgesendet.
|
5 | */
|
6 | /*
|
7 |
|
8 | Hinweis zum Clock fuer 8MHz Quarz, damit er auf 168MHz läuft (betrifft das stm32F4Discovery):
|
9 |
|
10 | in der system_stm32f4xx.c muss eingetragen sein:
|
11 | PLL_M = 8
|
12 |
|
13 | in der stm32f4xx.h muss eingetragen sein:
|
14 | HSE_VALUE = 8000000
|
15 | */
|
16 |
|
17 | #include "stm32f4xx.h"
|
18 | #include "stm32f4xx_gpio.h"
|
19 | #include "stm32f4xx_rcc.h"
|
20 | #include "misc.h"
|
21 | #include "stm32f4xx_usart.h"
|
22 |
|
23 | #define READ_BUFFER_SIZE 20
|
24 |
|
25 | volatile char read_buffer[READ_BUFFER_SIZE];
|
26 | volatile char read_buffer_single = 0;
|
27 | volatile char string_complete = 0;
|
28 | volatile int uart_str_count = 0;
|
29 |
|
30 | void Delay(__IO uint32_t nCount)
|
31 | {
|
32 | while(nCount--)
|
33 | {
|
34 | }
|
35 | }
|
36 |
|
37 | void USART_INIT_EXAMPLE(void)
|
38 | {
|
39 | /* USARTx configured as follow:
|
40 | - BaudRate = 19200 baud
|
41 | - Word Length = 8 Bits
|
42 | - One Stop Bit
|
43 | - No parity
|
44 | - Hardware flow control disabled (RTS and CTS signals)
|
45 | - Receive and transmit enabled
|
46 | */
|
47 | GPIO_InitTypeDef GPIO_InitStructure;
|
48 | USART_InitTypeDef USART_InitStructure;
|
49 |
|
50 | /* Enable GPIO clock */
|
51 | RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
|
52 |
|
53 | /* Enable UART clock */
|
54 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
|
55 |
|
56 | /* Connect PXx to USARTx_Tx*/
|
57 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
|
58 |
|
59 | /* Connect PXx to USARTx_Rx*/
|
60 | GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
|
61 |
|
62 | /* Configure USART Tx as alternate function */
|
63 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
64 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
65 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
66 |
|
67 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
68 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
69 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
70 |
|
71 | /* Configure USART Rx as alternate function */
|
72 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
73 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
|
74 | GPIO_Init(GPIOC, &GPIO_InitStructure);
|
75 |
|
76 | USART_InitStructure.USART_BaudRate = 19200;
|
77 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
78 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
79 | USART_InitStructure.USART_Parity = USART_Parity_No;
|
80 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
81 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
82 |
|
83 | /* USART configuration */
|
84 | USART_Init(USART3, &USART_InitStructure);
|
85 |
|
86 | // Receive Interrupt enable
|
87 | USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
|
88 |
|
89 | /* Enable USART */
|
90 | USART_Cmd(USART3, ENABLE);
|
91 |
|
92 | }
|
93 |
|
94 | void UART_SEND_EXAMPLE(char byte)
|
95 | {
|
96 | USART_SendData(USART3,byte);
|
97 | /* Loop until the end of transmission */
|
98 | while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET){}
|
99 | }
|
100 |
|
101 | void UART_SEND_TEXT_EXAMPLE(char *buffer)
|
102 | {
|
103 | while (*buffer)
|
104 | {
|
105 | UART_SEND_EXAMPLE(*buffer);
|
106 | buffer++;
|
107 | }
|
108 | }
|
109 |
|
110 | void NVIC_INIT_EXAMPLE(uint16_t IRQ)
|
111 | {
|
112 | NVIC_InitTypeDef NVIC_InitStructure;
|
113 |
|
114 | NVIC_InitStructure.NVIC_IRQChannel = IRQ;
|
115 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ Channel wird enabled
|
116 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Die Priorität --> kleine Zahlen = hohe Priorität
|
117 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
118 | NVIC_Init(&NVIC_InitStructure);
|
119 | }
|
120 |
|
121 | void USART3_IRQHandler(void)
|
122 | {
|
123 | if (USART_GetITStatus(USART3,USART_IT_RXNE) == SET)
|
124 | {
|
125 | USART_ClearITPendingBit(USART3,USART_IT_RXNE);
|
126 |
|
127 | // Daten aus dem Puffer lesen
|
128 | read_buffer_single = (USART3->DR & (uint16_t)0x01FF);
|
129 |
|
130 | if ( string_complete==0 )
|
131 | {
|
132 | //Bedingungen für ein Ende des Strings
|
133 | if (read_buffer_single!='\n' && read_buffer_single!='\r' && uart_str_count<READ_BUFFER_SIZE-1)
|
134 | {
|
135 | read_buffer[uart_str_count]=read_buffer_single;
|
136 | uart_str_count++;
|
137 | }
|
138 | else
|
139 | {
|
140 | read_buffer[uart_str_count]='\0';
|
141 | uart_str_count=0;
|
142 | string_complete=1;
|
143 | }
|
144 | }
|
145 | }
|
146 | }
|
147 |
|
148 |
|
149 | int main(void)
|
150 | {
|
151 |
|
152 | // Systemclock auf 168MHz (stm32F4Discovery mit 8MHz Quarz)
|
153 | SystemInit();
|
154 |
|
155 | // Initialisieren der Schnittstelle
|
156 | USART_INIT_EXAMPLE();
|
157 |
|
158 | // Interruptcontroller für RX-Interrupt konfigurieren
|
159 | NVIC_INIT_EXAMPLE(USART3_IRQn);
|
160 |
|
161 |
|
162 | //nun Text auf der ser. Schnittstelle empfangen und zurücksenden.
|
163 | while(1)
|
164 | {
|
165 | if (string_complete)
|
166 | {
|
167 | // Echo des gesendeten Strings erzeugen
|
168 | // Achtung der String muss mit '\n' oder '\r' abgeschlossen werden beim Sender
|
169 | UART_SEND_TEXT_EXAMPLE((char*)read_buffer);
|
170 | string_complete = 0;
|
171 | }
|
172 | }
|
173 | }
|