1 | const int bufSize = 255;
|
2 | uint8_t buffer [bufSize], bufFill, txPtr;
|
3 |
|
4 | void myUsartInit () {
|
5 | // Clock des USART einschalten:
|
6 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
|
7 | RCC_APB2PeriphClockLPModeCmd(RCC_APB2Periph_USART1, ENABLE);
|
8 | RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOBEN, ENABLE);
|
9 |
|
10 | // USART Pins einschalten
|
11 | GPIO_InitTypeDef GPIO_InitStructure;
|
12 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
|
13 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
14 | GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
|
15 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
16 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
17 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
18 | GPIO_PinAFConfig (GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
|
19 |
|
20 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
|
21 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
22 | GPIO_PinAFConfig (GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
|
23 |
|
24 | // USART konfigurieren & einschalten
|
25 | USART_InitTypeDef uInit;
|
26 | USART_StructInit (&uInit);
|
27 | uInit.USART_BaudRate = 57600;
|
28 | uInit.USART_WordLength = USART_WordLength_8b;
|
29 | uInit.USART_StopBits = USART_StopBits_1;
|
30 | uInit.USART_Parity = USART_Parity_No;
|
31 | uInit.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
32 | uInit.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
33 | USART_Init (USART1, &uInit);
|
34 |
|
35 | USART_ClockInitTypeDef ucInit;
|
36 | USART_ClockStructInit (&ucInit);
|
37 | ucInit.USART_Clock = USART_Clock_Disable;
|
38 | USART_ClockInit (USART1, &ucInit);
|
39 |
|
40 | USART_Cmd (USART1, ENABLE);
|
41 |
|
42 | // Interrupt channel einschalten
|
43 | NVIC_InitTypeDef NVIC_InitStructure;
|
44 | NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
|
45 | NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
46 | NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
|
47 | NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
|
48 | NVIC_Init(&NVIC_InitStructure);
|
49 |
|
50 | // Interrupts einschalten
|
51 | USART_ITConfig (USART1, USART_IT_TXE, ENABLE); // TX register Empty - wir können was senden
|
52 | USART_ITConfig (USART1, USART_IT_RXNE, ENABLE); // RX register Not Empty - es wurde was empfangen
|
53 |
|
54 | bufFill = 0; txPtr = 0;
|
55 | }
|
56 |
|
57 | void USART1_IRQHandler (void) {
|
58 | // Können wir was senden?
|
59 | if (USART_GetFlagStatus (USART1, USART_FLAG_TXE) == SET) {
|
60 | // Gibts was zu senden?
|
61 | if (txPtr < bufFill) {
|
62 | // absenden
|
63 | USART_SendData (USART1, buffer [txPtr]);
|
64 | txPtr++;
|
65 | } else {
|
66 | // Wenn nicht - Interrupt abschalten, wir wollen nicht in Endlosschleife in diesem Interrupt landen
|
67 | USART_ITConfig (USART1, USART_IT_TXE, DISABLE);
|
68 | }
|
69 | }
|
70 | // Haben wir was empfangen?
|
71 | if (USART_GetFlagStatus (USART1, USART_FLAG_RXNE) == SET) {
|
72 | // Auslesen
|
73 | uint8_t rec = USART_ReceiveData (USART1);
|
74 | // Noch Platz im Puffer?
|
75 | if(bufFill < bufSize) {
|
76 | // Speichern
|
77 | buffer [bufFill] = rec;
|
78 | bufFill++;
|
79 |
|
80 | // TXE-Interrupt ein
|
81 | USART_ITConfig (USART1, USART_IT_TXE, ENABLE);
|
82 | // Können wir das direkt absenden?
|
83 | if(USART_GetFlagStatus (USART1, USART_FLAG_TXE) == SET) {
|
84 | // Absenden
|
85 | USART_SendData (USART1, buffer [txPtr]);
|
86 | txPtr++;
|
87 | } // Falls nicht, wird demnächst ein TXE-Interrupt eintreffen und wir können dann senden
|
88 | }
|
89 | }
|
90 | }
|