This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

STM32, printf wiindow and USART2 to printf

Hello,

I got PCB where a Bluetooth modul is SMD mounted at USART2 and a STM32F103ZE T6 (High Density). I'm using Keil + cmsis + stlib3.5.0

I'm trying to get the BT Modul working.
For that I need to see what the USART2 is echoing from my BT-Module and I have to write some AT-Commands to the USART2 from a debug window in keil (is that possible?)

Can anyone help me?

I started to do my retarget.c, it looks like this but i dont know if its correct or not:

#include <stdio.h>
#include <rt_misc.h>

#pragma import(__use_no_semihosting_swi)

extern int  SendChar (int ch);                    /* In Serial.c                        */

struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;

int fputc (int ch, FILE *f) { return (SendChar(ch)); }

int ferror (FILE *f)
{
/* Your implementation of ferror */
    return EOF;
}


void _ttywrch (int ch) { SendChar(ch); }

void _sys_exit (int return_code) { for (;;); }

and my stmdbg.ini for the ULINK2:

FUNC void DebugSetup (void) {

  _WDWORD(0xE0042004, 0x00000027);  // DBGMCU_CR
  _WDWORD(0x40044064, 0x00000003);  // Enable SWO on P0.9
}


DebugSetup();                       // Debugger Setup

and my serial.c

/******************************************************************************/
/* SERIAL.C: Low Level Serial Routines                                        */
/******************************************************************************/
/* This file is part of the uVision/ARM development tools.                    */
/* Copyright (c) 2005-2007 Keil Software. All rights reserved.                */
/* This software may only be used under the terms of a valid, current,        */
/* end user licence from KEIL for a compatible version of KEIL software       */
/* development tools. Nothing else gives you the right to use this software.  */
/******************************************************************************/

#define ITM_Port8(n)    (*((volatile unsigned char *)(0xE0000000+4*n)))
#define ITM_Port16(n)   (*((volatile unsigned short*)(0xE0000000+4*n)))
#define ITM_Port32(n)   (*((volatile unsigned long *)(0xE0000000+4*n)))

#define DEMCR           (*((volatile unsigned long *)(0xE000EDFC)))
#define TRCENA          0x01000000

#include <stm32f10x_lib.h>                        /* STM32F10x Library Definitions      */


/* Implementation of putchar (also used by printf function to output data)    */
int SendChar (int ch)                             /* Write character to Serial Port     */
{

#ifdef DBG_ITM
    if (DEMCR & TRCENA)
    {
        while (ITM_Port32(0) == 0);
        ITM_Port8(0) = ch;
    }
#else
    USART_SendData(USART2, (unsigned char) ch);
    while (!(USART2->SR & USART_FLAG_TXE));
#endif
    return (ch);
}


int GetKey (void)                                 /* Read character from Serial Port    */
{

    while (!(USART2->SR & USART_FLAG_RXNE));
    return (USART_ReceiveData(USART2));
}

///////////////////////////////////////////////////////////////////////////////////
If anyone has something finshed please share or give some good adivce :)

with regards
Sven

  • and my USART2 setup in setup.c

    void SetupBT (void)
    {
            /* USART2 Clock */
            RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
    
            /*Init USART2 USART2: CTS PA0, RTS PA1, TX PA2, RX PA3 */
    
            /* Configure USART2 RTS and USART2 Tx as alternate function push-pull */
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
            GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
            GPIO_Init(GPIOA, &GPIO_InitStructure);
    
            /* Configure USART2 CTS and USART2 Rx as input floating */
            GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_3;
            GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
            GPIO_Init(GPIOA, &GPIO_InitStructure);
    
            USART_InitStructure.USART_BaudRate = 38400;
            USART_InitStructure.USART_WordLength = USART_WordLength_8b;
            USART_InitStructure.USART_StopBits = USART_StopBits_1;
            USART_InitStructure.USART_Parity = USART_Parity_No;
            USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
            USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
            USART_Init(USART2, &USART_InitStructure);
            USART_Cmd(USART2, ENABLE);
    
            /* Init BT */
            /*here i need the printf and "cout" function, to check the echo replying from the BT over USART2 */
            IMU_Led1_On();
            Delay(100);
            send_string("STWMOD=0\r\n");
        send_string("STBD=38400\r\n");
            Delay(200);
            send_string("STNA=IMU\r\n");
    
            send_string("STAUTO=0\r\n");
            send_string("STOAUT=1\r\n");
            send_string("STPIN=0000\r\n");
            Delay(200);
            send_string("INQ=0\r\n");
            Delay(200);
    
    }
    

  • Hi,

    please have a look at the CMSIS (core_cm3.h) function ITM_SendChar(). You should use this. In the Keil STM32 Examples you'll also find a simpler example for setting up the UARTs :-)

    .
    BR,
    /th.