AnsweredAssumed Answered

how to override printf to USART transfer with DMA

Question asked by vaugh.Huang on Aug 8, 2015
Latest reply on Aug 8, 2015 by vaugh.Huang
Hello,
I want to use USART2 of STM32 as debug output of application, so i overload printf as below:

01.#pragma import(__use_no_semihosting)
02.struct __FILE {
03.    int handle;
04.} __stdout;
05. 
06.void _sys_exit(int x)  { x = x; }
07. 
08.int fputc( int ch, FILE *f )    //  overload fputc
09.{
10.    while( (USART2->SR&0X40) == 0 );
11.    USART2->DR = (u8)ch;
12.    return ch;
13.}
14. 
15.int printf( const char* format, ... )
16.{
17.    int rv;
18.    va_list arg;
19. 
20.    while ( flag_tx_usart2_dma_busy );      // wait for DMA1_Stream6  IRQ finish if not
21.    flag_tx_usart2_dma_busy = 1;
22. 
23.    va_start(arg, format);
24.    rv = vsnprintf((char*)usart2_dma_tx_buf, USART2_DMA_TX_BUF_SIZE, format, arg);
25.    va_end(arg);
26. 
27.    DMA_SetCurrDataCounter(DMA1_Stream6, rv);   // set data number to send
28.    DMA_Cmd(DMA1_Stream6, ENABLE);          // start DMA -> USART -> PC
29.    return rv;
30.}

The IRQ of DMA TX:
1.void DMA1_Stream6_IRQHandler(void)
2.{
3.    if ( DMA_GetITStatus(DMA1_Stream6, DMA_IT_TCIF6) ) {
4.        DMA_Cmd(DMA1_Stream6, DISABLE); // disable DMA
5.        DMA_ClearITPendingBit(DMA1_Stream6, DMA_IT_TCIF6);  // Clear DMA Stream Transfer Complete interrupt pending bit
6.        flag_tx_usart2_dma_busy = 0;    // clear busy flag
7.    }
8.}

at last i call printf in USART1 IRQ, and found it not called. that's to say, the overload not functional. I do not know why?

Outcomes