1 | #include "stm32f10x_gpio.h"
|
2 | #include "stm32f10x_rcc.h"
|
3 | #include "stm32f10x_usart.h"
|
4 | #include "stm32f10x_tim.h"
|
5 | #include "misc.h"
|
6 |
|
7 | GPIO_InitTypeDef GPIO_InitStructure;
|
8 | TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
9 | USART_InitTypeDef USART_InitStructure;
|
10 |
|
11 | #define READ_BUFFER_SIZE 20
|
12 |
|
13 | volatile char read_buffer[READ_BUFFER_SIZE];
|
14 | volatile char read_buffer_single = 0;
|
15 | volatile char string_complete = 0;
|
16 | volatile int uart_str_count = 0;
|
17 |
|
18 | void Delay(__IO uint32_t nCount)
|
19 | {
|
20 | while(nCount--)
|
21 | {
|
22 | }
|
23 | }
|
24 |
|
25 | void nvic_initialisierung(uint16_t IRQ)
|
26 | {
|
27 | NVIC_InitTypeDef NVIC_InitStructure; //create NVIC structure
|
28 | NVIC_InitStructure.NVIC_IRQChannel = IRQ;
|
29 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
30 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
31 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
32 | NVIC_Init(&NVIC_InitStructure);
|
33 | }
|
34 |
|
35 | void UARTSend(const unsigned char *pucBuffer)
|
36 | {
|
37 |
|
38 | while(*pucBuffer)
|
39 | {
|
40 | USART_SendData(USART1, (uint16_t) *pucBuffer++);
|
41 | /* Loop until the end of transmission */
|
42 | while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
|
43 | {
|
44 | }
|
45 | }
|
46 | }
|
47 |
|
48 | void USART_Configuration(void)
|
49 | {
|
50 |
|
51 | // Enable USART1 and GPIOA clock */
|
52 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
|
53 |
|
54 | /* Configure USART1 Tx (PA.09) as alternate function push-pull */
|
55 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
56 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
57 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
58 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
59 |
|
60 | /* Configure USART1 Rx (PA.10) as input floating */
|
61 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
62 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
63 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
64 |
|
65 | /* USART1 configuration ------------------------------------------------------*/
|
66 | /* USART1 configured as follow:
|
67 | - BaudRate = 19200 baud
|
68 | - Word Length = 8 Bits
|
69 | - One Stop Bit
|
70 | - No parity
|
71 | - Hardware flow control disabled (RTS and CTS signals)
|
72 | - Receive and transmit enabled
|
73 | - USART Clock disabled
|
74 | - USART CPOL: Clock is active low
|
75 | - USART CPHA: Data is captured on the middle
|
76 | - USART LastBit: The clock pulse of the last data bit is not output to
|
77 | the SCLK pin
|
78 | */
|
79 | USART_InitStructure.USART_BaudRate = 19200;
|
80 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
81 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
82 | USART_InitStructure.USART_Parity = USART_Parity_No;
|
83 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
84 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
85 |
|
86 | USART_Init(USART1, &USART_InitStructure);
|
87 |
|
88 | /* Enable the USART1 Receive interrupt: this interrupt is generated when the
|
89 | USART1 receive data register is not empty */
|
90 | USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
|
91 |
|
92 | /* Enable USART1 */
|
93 | USART_Cmd(USART1, ENABLE);
|
94 | }
|
95 |
|
96 |
|
97 | void USART1_IRQHandler(void)
|
98 | {
|
99 | if (USART_GetITStatus(USART1,USART_IT_RXNE) == SET)
|
100 | {
|
101 | USART_ClearITPendingBit(USART1,USART_IT_RXNE);
|
102 |
|
103 | // Daten aus dem Puffer lesen
|
104 | read_buffer_single = (USART1->DR & (uint16_t)0x01FF);
|
105 |
|
106 | if ( string_complete==0 )
|
107 | {
|
108 | //Bedingungen für ein Ende des Strings
|
109 | if (read_buffer_single!='\n' && read_buffer_single!='\r' && uart_str_count<READ_BUFFER_SIZE-1)
|
110 | {
|
111 | read_buffer[uart_str_count]=read_buffer_single;
|
112 | uart_str_count++;
|
113 | }
|
114 | else
|
115 | {
|
116 | read_buffer[uart_str_count]='\0';
|
117 | uart_str_count=0;
|
118 | string_complete=1;
|
119 | }
|
120 | }
|
121 | }
|
122 | }
|
123 |
|
124 |
|
125 | int main(void)
|
126 | {
|
127 |
|
128 | SystemInit();
|
129 | nvic_initialisierung(USART1_IRQn);
|
130 | USART_Configuration();
|
131 |
|
132 | while(1){
|
133 | if (string_complete)
|
134 | {
|
135 | // Echo des gesendeten Strings erzeugen
|
136 | // Achtung der String muss mit '\n' oder '\r' abgeschlossen werden beim Sender
|
137 | UARTSend(read_buffer);
|
138 | UARTSend("\n\r");
|
139 | string_complete = 0;
|
140 | }
|
141 | }
|
142 | }
|