2017-10-15 104 views
0

我試圖在同一個全局數組上發送2個不同的字符串,通過UART發送DMA作爲終端日誌。然而,發生的是它發送了第一個字符串,無論我嘗試了什麼(重新初始化整個DMA和UART,它只發送第一個字符串。 我希望它在每次寫入時傳輸一次任何字符串全球陣列DMA USART使用stm32f407xx作爲終端日誌的發送端

char result2[100]; 
char result[100]; 

void UART2INIT(void) 
{ 


    RCC->APB1ENR|=RCC_APB1ENR_USART2EN; 
    RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN; 
    GPIOA->MODER|=((2U<<4)|(2U<<6)); 
    GPIOA->AFR[0]=0x7777; 
    USART2->CR1|=(1U << 13) | (1U<<5); 
    // USART2->CR1|=USART_CR1_M; //IF YOU NEED TO TURN IT ON TO SELECT ANOTHER WORD LENGTH 
    USART2->CR2 &=~ (1U << 13); 
// CR2 IS MAINLY USED IN CASE IF YOU WANTED TO USE UART AS UNIVERSAL SYNCHRONOUS, THUS IT IS NOT WEIRD YOU CAN FIND POLARITY AND PHASE SETTINGS BY  WHICH YOU CAN SET IT UP 
/* 
    The baud rate is computed using the following formula: 
    - IntegerDivider = ((PCLKx)/(8 * (OVR8+1) * (USART_InitStruct->USART_BaudRate))) 
- FractionalDivider = ((IntegerDivider - ((u32) IntegerDivider)) * 8 * (OVR8+1)) + 0.5 
    Where OVR8 is the "oversampling by 8 mode" configuration bit in the CR1 register. */ 



    USART2->BRR=((42*1000000)/19200)+0.5; 
    USART2->CR1|=USART_CR1_TCIE|USART_CR1_RXNEIE; //enabling exceptions mask 
    USART2->CR3|=USART_CR3_DMAT; 
    USART2->CR3|=USART_CR3_DMAR; 
    USART2->CR1|=(1U<<2)|(1U<<3); 

    while(!(USART2->SR) & (USART_SR_TC)); 
} 






void DMA_UART_INIT(void) 
{ 

    RCC->AHB1ENR|=RCC_AHB1ENR_DMA1EN; 
    DMA1_Stream6->CR=0; 
    while(DMA1_Stream6->CR); 
    DMA1->LISR=0; 
    DMA1->HISR=0; 
    DMA1_Stream6->M0AR= (uint32_t) &result; 
    DMA1_Stream6->PAR=0x40004404; 
    DMA1_Stream6->NDTR=100; //x items 
    DMA1_Stream6->CR&=~((1U<<5)|(1U<<9)|(1U<<11)|(1U<<13)|(1U<<15)|(1u<<21)); 
    DMA1_Stream6->CR|=(1u<<4)| (1u<<6)|(1u<<10)|(2u<<16)|(4u<<25)|(1U<<1); 

    USART2->SR&=~(1U<<6); 
    DMA1_Stream6->CR&=~(1u<<0); 


} 

while (1) 
{ 
strcpy(result,"testing DMA"); 
DMA1_Stream6->CR|=(1u<<0); 
Delay(0XFFFFFf); 
DMA1_Stream6->CR &= ~(1u<<0); 
DMA1->HIFCR &=~ (DMA_HIFCR_CHTIF6 | DMA_HIFCR_CTCIF6 | DMA_HIFCR_CFEIF6); 
while(!(USART2->SR) & (USART_SR_TC)); 
strcpy(result2,"TESTING DMA"); 
DMA_UART_RESET(); 
UART2INIT(); 

DMA_UART_INIT(); 
DMA1_Stream6->M0AR= (uint32_t) &result2; 

DMA1_Stream6->NDTR=100; 
DMA1_Stream6->CR|=(1u<<0); 


Delay(0XFFFFFf); 
} 

請注意:我已經在函數寫這些不過我試圖使用一段時間(1),以解決那裏的問題又是我無法找到它 的代碼。有點凌亂,因爲我試圖拼命地觸發幾個位,希望它可以工作,所以請和我一起裸露。

回答

0

很難說沒有調試的問題在哪裏,但是:

  1. while(!(USART2->SR) & (USART_SR_TC))不正確。它首先在邏輯上反轉SR,然後才應用按位AND。
  2. 當使用帶有DMA的外設時,不要檢查外設的狀態寄存器,而是檢查DMA完成狀態。在DMA正在運行時,外設狀態可能會隨意更改。一旦DMA完成,您可以根據需要檢查外圍設備是否有錯誤等。
  3. 您在UART上啓用了用於發送和接收的DMA,但代碼僅初始化用於傳輸的DMA通道。首先嚐試調試,但不要設置DMAR
  4. 確實很難說出(1U<<5)|(1U<<9)|(1U<<11)|(1U<<13)|(1U<<15)|(1u<<21)的含義。