DMA UART mit Parity bit

Gast #4936895
Lesenswert?

Hallo Forum,

zu allererst: Ich verwende einen STM32F303!
Ich möchte 8-bit-Werte, welche im Array "send_buffer" vorhanden sind 
über UART und DMA schicken. Wenn ich in der UART-Konfiguration folgende 
Einstellungen durchführe: Wort-Länge = 8 und kein Parity-Bit soll 
gesendet werden, dann werden alle 8 Elemente im Array korrekt gesendet.

Ich möchte allerdings ein Parity Bit dabei haben (Even). Das heißt, ich 
stelle in der Einstellung die Wort-Länge auf 9 bzw. Parity-Bit = Even 
ein.
Nun habe ich das Problem, dass jede zweite Element vom Array NICHT 
gesendet wird und die letzten 4 gesendete Werte sind "irgendwelche" 
Werte. (mit Oszi angesehen)

Weiß jemand, welche Einstellung fehlt?

Der Code sieht wie folgt aus:

1
#include <stm32f30x.h>
2
#include <stm32f30x_gpio.h>
3
#include <stm32f30x_rcc.h>
4
#include <stm32f30x_usart.h>
5
#include <stm32f30x_misc.h>     //for NVIC
6
#include <core_cm4.h>
7
#include <stm32f30x_dma.h>
8
#include <stm32f30x_debug.h>
9

10

11
#define TXnumber 8             USART gesendet werden sollen
12
#define RXnumber 16            // Anzahl der 8bit Werte die über USART empfangen werden sollen
13

14

15
//###### global variables ########
16

17
uint8_t send_buffer[TXnumber] = {0x55, 0x0, 0x55, 0x0, 0x55, 0x0, 0x55, 0x0};    
18

19

20
// prototypes
21
void RCC_Configuration(void);
22
void USART3_Configuration();
23
void DMA_UART3_transmit_Configuration();
24
void send_with_DMA_UART3(uint8_t buffer[], uint8_t size);
25

26

27
void RCC_Configuration(void)
28
{   
29
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);     // Enable peripheral clock for GPIOB for USART3
30
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);  // Enable peripheral clock for USART3
31
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);      // Enable peripheral clock for DMA1
32
}
33

34

35

36
void USART3_Configuration()
37
{ 
38
    gpioConfig.GPIO_Mode = GPIO_Mode_AF;            // Alternate function mode  page: 237
39
    gpioConfig.GPIO_OType = GPIO_OType_PP;          // Output Push-Pull         page: 237
40
    gpioConfig.GPIO_Speed = GPIO_Speed_10MHz;       // Medium speed             page: 238
41
    gpioConfig.GPIO_PuPd = GPIO_PuPd_NOPULL;        // No pull-up, pull down    page: 239
42
    gpioConfig.GPIO_Pin = (uint32_t)(GPIO_Pin_10 | GPIO_Pin_11);
43
    GPIO_Init(GPIOB, &gpioConfig);                  // pin initialization
44
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_7);    
45
    GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_7);    
46

47
    USART_InitTypeDef usartConfig;
48

49
    RCC->CFGR3   |= RCC_CFGR3_USART3SW_0;         // UART3 clock nun SYSCLK (72MHz)
50

51
    usartConfig.USART_BaudRate = 57600;           
52
    usartConfig.USART_WordLength = USART_WordLength_9b;         // 9th bit = parity bit
53
    usartConfig.USART_StopBits = USART_StopBits_2;
54
    usartConfig.USART_Parity = USART_Parity_Even;
55
    usartConfig.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
56
    usartConfig.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
57
    USART_Init(USART3, &usartConfig);
58

59
    USART_ClearITPendingBit(USART3, USART_IT_RXNE);                 // Clear interrupt flag
60
    USART_ClearITPendingBit(USART3, USART_IT_TXE);
61

62
    USART_ITConfig(USART3, USART_IT_TXE, DISABLE);                  // disable Transmit Data Register empty interrupt
63
    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);                  // Enable the USART RX Interrupt (without enabling it, no interrupt will occur when receiving)
64

65
    NVIC_EnableIRQ(USART3_IRQn);                                    // Enable USART3 global interrupt
66
    USART_Cmd(USART3, ENABLE);      // UART3 enable             
67
}
68

69

70

71

72
void DMA_UART3_transmit_Configuration()
73
{
74

75
    // DMA1 Channel 2 - UART3 TX
76
    DMA_Cmd(DMA1_Channel2, DISABLE);                                    
77

78
    DMA_InitTypeDef DMA_InitStruct;
79
    //DMA_StructInit(&DMA_InitStruct);
80
    //DMA_InitStruct.DMA_Channel = DMA1_Channel2;
81
    DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&(USART3->TDR);   
82
    DMA_InitStruct.DMA_MemoryBaseAddr = (uint32_t)&send_buffer[0];        // 32-Bit Basis Adresse im Speicher (Adresse der Speicherzelle, wo das erste Byte vorhanden ist).
83
    DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralDST;                     
84
    DMA_InitStruct.DMA_M2M = DMA_M2M_Disable;                           
85
    DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;        
86
    DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;                
87
    DMA_InitStruct.DMA_Mode = DMA_Mode_Normal;                          
88
    DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;       
89
    DMA_InitStruct.DMA_Priority = DMA_Priority_Low;
90
    DMA_InitStruct.DMA_BufferSize = 0;                                  
91
    DMA_Init(DMA1_Channel2, &DMA_InitStruct);
92
    DMA_ClearFlag(DMA1_FLAG_TE2);
93
    DMA_ClearFlag(DMA1_FLAG_TC2);
94
    DMA_ClearFlag(DMA1_FLAG_HT2);
95
    //DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);                     // Interrupt will occur when Transmission complete (TC)
96
    //NVIC_EnableIRQ(DMA1_Channel2_IRQn);                               // Enable DMA1 Chan2 global interrupt
97

98

99
    USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE);                      // Enable USARTy DMA TX request
100

101
    DMA_Cmd(DMA1_Channel2, ENABLE);
102
}
103

104

105

106

107
void send_with_DMA_UART3(uint8_t buffer[], uint8_t size)
108
{
109
    // buffer = Array, das gesendet werden soll (ein Element hat 16bit)
110
    // size = Anzahl der Elemente, die gesendet werden (Größe des Arrays)
111

112
    DMA_Cmd(DMA1_Channel2, DISABLE);
113
    DMA_SetCurrDataCounter(DMA1_Channel2, size);          // Anzahl der Bytes die gesendet werden sollen
114
    DMA1_Channel2->CMAR = (uint32_t)&(send_buffer[0]);  // Pointer zeigt auf Adresse des ersten Bytes, das gesendet werden soll
115
    DMA_Cmd(DMA1_Channel2, ENABLE);
116
} // void send_with_DMA
117

118

119

120

121

122
int main ()
123
{
124
    RCC_Configuration();
125
    USART3_Configuration();
126
    DMA_UART3_transmit_Configuration();
127

128
    RXcounter = 2;
129

130
    while(1)
131
        {
132
        send_with_DMA_UART3(send_buffer,8);      // 1st parameter: array which should be sent; 2nd parameter: number of the arrays that should be sent
133
        } //while(1)
134
} //main
Gast #4936904
Lesenswert?

Hallo

Das Parity Bit zählt nicht zur Wortlänge.

Also so:
1
usartConfig.USART_WordLength = USART_WordLength_8b; //8 data bits
2
usartConfig.USART_StopBits = USART_StopBits_2;
3
usartConfig.USART_Parity = USART_Parity_Even;

da1l6

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren