cancel
Showing results for 
Search instead for 
Did you mean: 

How to printf in Keil ( Nucleo L073RZ)

waiyang93
Associate II
Posted on May 26, 2016 at 18:16

Hi,

Currently I am using ST nucleo L073RZ with Keil testing with stm32

cubeL0 example code. Is there any way to do a printf in Keil say print out the value in the debug printf window.

#printf #keil #nucleo #stm32l0
6 REPLIES 6
Posted on May 26, 2016 at 19:44

The Cortex-M0 doesn't not provide for the SWV (Serial Wire Viewer) model used on the M3/M4 designs.

You would need to use the serial port (VCP of ST-LINK) and add Retargeting code for that into your Keil project, and then use a terminal app like RealTerm, ClearTerm or TeraTerm, etc to interact with the board.

http://www.keil.com/support/man/docs/gsac/gsac_retargetcortex.htm

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
waiyang93
Associate II
Posted on May 27, 2016 at 04:25

Hi clive 1 thanks for the reply , I added the retarget.c file into my project folder . However, how do I make use of the fputc to print out the value? For example I need to print out this buffer value which send out by Tx

/* Buffer used for transmission */

uint8_t aTxBuffer[] = '' ****UART_TwoBoards_ComIT**** ****UART_TwoBoards_ComIT****  ****UART_TwoBoards_ComIT**** '';

Posted on May 28, 2016 at 18:39

puts(aTxBuffer); //?? via STDIO

outstring(aTxBuffer); // simple character at a time string processing

void outstring(char *s)
{
while(*s)
{
while(!(USART1->ISR & USART_ISR_TXE));
USART1->TDR = *s++;
}
}
void outchar(char c)
{
while(!(USART1->ISR & USART_ISR_TXE));
USART1->TDR = c;
}
int SendChar(int c)
{
while(!(USART1->ISR & USART_ISR_TXE));
USART1->TDR = c;
return(c);
}
//****************************************************************************
#include <
rt_misc.h
>
#pragma import(__use_no_semihosting_swi)
struct __FILE { int handle; /* Add whatever you need here */ };
FILE __stdout;
FILE __stdin;
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)
{
label: goto label; /* endless loop */
}
//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
waiyang93
Associate II
Posted on May 30, 2016 at 08:37

I have edited the retarget.c however all the usart terms are underline. Do I need to define something or include some library?

//code///////

/*----------------------------------------------------------------------------

 * Name:    Retarget.c

 * Purpose: 'Retarget' layer for target-dependent low level functions

 * Note(s):

 *----------------------------------------------------------------------------

 * This file is part of the µVision/ARM development tools.

 * 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.

 *

 * This software is supplied ''AS IS'' without warranties of any kind.

 *

 * Copyright (c) 2009 Keil - An ARM Company. All rights reserved.

 *----------------------------------------------------------------------------*/

#include <stdio.h>

#include <rt_misc.h>

#pragma import(__use_no_semihosting_swi)

extern int sendchar (int c);

extern int getkey   (void);

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

FILE __stdout;

FILE __stdin;

int fputc(int c, FILE *f) {

  return (sendchar(c));

}

int fgetc(FILE *f) {

  return (getkey());

}

int ferror(FILE *f) {

  /* Your implementation of ferror */

  return EOF;

}

void _ttywrch(int c) {

  sendchar(c);

}

void _sys_exit(int return_code) {

label:  goto label;  /* endless loop */

}

void outstring(char *s)

{

   while(*s)

  {

    while(!(USART1->ISR & USART_ISR_TXE));

    USART1->TDR = *s++;

  }

}

 

void outchar(char c)

{

  while(!(USART1->ISR & USART_ISR_TXE));

  USART1->TDR = c;

}

 

int SendChar(int c)

{

  while(!(USART1->ISR & USART_ISR_TXE));

  USART1->TDR = c;

  return(c);

}

  

Posted on January 27, 2018 at 03:20

The Cortex-M0 can use the 'Event Recorder'

http://www.keil.com/pack/doc/compiler/RetargetIO/html/_retarget__examples_er.html