AnsweredAssumed Answered

keil uVision4 problem with C++ code

Question asked by Fabio_IT on Oct 14, 2013
Latest reply on Oct 18, 2013 by Fabio_IT
Hi guys,

I'm trying to use printf function with C++ code. The code is working but only first 11 characters are successfully written. any ideas? that's the code:

001./* Includes ------------------------------------------------------------------*/
002.#include "stm32f0xx.h"
003.#include <stdio.h>
004.#include <stddef.h>
005.#include <string.h>
006. 
007./* Private typedef -----------------------------------------------------------*/
008./* Private define ------------------------------------------------------------*/
009./* Private macro -------------------------------------------------------------*/
010./* Private variables ---------------------------------------------------------*/
011.volatile uint32_t msTicks;
012. 
013./* Function prototypes -----------------------------------------------*/
014.void SysTick_Handler(void);
015.void Delay (uint32_t dlyTicks);
016. 
017.void USART1_Init(void);
018.void USART1_IRQHandler(void);
019./* Functions ---------------------------------------------------------*/
020. 
021./*
022.  SysTick handler
023. */
024.void SysTick_Handler(void) {
025.  msTicks++;
026.}
027. 
028./*
029.  Function to provide delay (in mSec)
030. */
031.void Delay (uint32_t delayTicks) {                                             
032.  uint32_t currentTicks;
033. 
034.  currentTicks = msTicks;
035.  while ((msTicks - currentTicks) < delayTicks);
036.}
037. 
038./*
039.  USART1 Tx PA.9, Rx PA.10 gpio clock interrupt configuration
040. */
041.void USART1_Init(void) {
042.  USART_InitTypeDef USART_InitStructure;
043.  GPIO_InitTypeDef GPIO_InitStructure;
044.  NVIC_InitTypeDef NVIC_InitStructure;
045.   
046.  // configuring clock sources for USART1
047.  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
048.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
049. 
050.  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
051.  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
052.    
053.  /* Configure USART1 pins:  Rx and Tx ----------------------------*/
054.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
055.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
056.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
057.  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
058.  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
059.  GPIO_Init(GPIOA, &GPIO_InitStructure);
060.  
061.  /* Enable USART1 IRQ */
062.  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
063.  NVIC_InitStructure.NVIC_IRQChannelPriority = 3;
064.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
065.  NVIC_Init(&NVIC_InitStructure);
066.    
067.  USART_InitStructure.USART_BaudRate = 115200;
068.  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
069.  USART_InitStructure.USART_StopBits = USART_StopBits_1;
070.  USART_InitStructure.USART_Parity = USART_Parity_No;
071.  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
072.  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
073.  USART_Init(USART1, &USART_InitStructure);
074.   
075.  // enabling USART1
076.  USART_Cmd(USART1,ENABLE);
077. 
078.  // enable incoming data interrupt
079.  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
080.}
081. 
082./*
083.  USART1 interrupt handler
084. */
085.void USART1_IRQHandler(void) {
086.  volatile uint8_t data = 0x00;
087. 
088.  if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {
089.    data = USART_ReceiveData(USART1);
090.    
091.    printf("Received character: %c\n", data);
092.  }
093.}
094./**
095.  * @brief  Main program.
096.  * @param  None
097.  * @retval None
098.  */
099.int main(void) {
100.  SystemCoreClockUpdate();                      /* Get Core Clock Frequency   */
101.  if (SysTick_Config(SystemCoreClock / 1000)) { /* SysTick 1 msec interrupts  */
102.    while (1);                                  /* Capture error              */
103.  }
104.     
105.    USART1_Init();
106. 
107.    printf("System clock: %u MHz\n", SystemCoreClock / 1000000);
108. 
109.  /*
110.    ENDLESS LOOP BEGIN
111.   */
112.  while (1) {
113.    printf("I'm alive...\n");
114.    Delay(1000);
115.  }
116. 
117.  return(0);
118.}
119. 
120.#ifdef  USE_FULL_ASSERT
121. 
122./**
123.  * @brief  Reports the name of the source file and the source line number
124.  *         where the assert_param error has occurred.
125.  * @param  file: pointer to the source file name
126.  * @param  line: assert_param error line source number
127.  * @retval None
128.  */
129.void assert_failed(uint8_t* file, uint32_t line)
130.{
131.  /* User can add his own implementation to report the file name and line number,
132.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
133. 
134.  /* Infinite loop */
135.  while (1)
136.  {
137.  }
138.}
139.#endif

and this is the modified retarget.c
01./******************************************************************************/
02./* RETARGET.C: 'Retarget' layer for target-dependent low level functions      */
03./******************************************************************************/
04./* This file is part of the uVision/ARM development tools.                    */
05./* Copyright (c) 2005 Keil Software. All rights reserved.                     */
06./* This software may only be used under the terms of a valid, current,        */
07./* end user licence from KEIL for a compatible version of KEIL software       */
08./* development tools. Nothing else gives you the right to use this software.  */
09./******************************************************************************/
10. 
11.#include "stm32f0xx.h"
12.#include <stdio.h>
13.#include <time.h>
14.#include <rt_misc.h>
15. 
16.#pragma import(__use_no_semihosting_swi)
17. 
18.struct __FILE { int handle; /* Add whatever you need here */ };
19.FILE __stdout;
20.FILE __stdin;
21. 
22. 
23.int fputc(int ch, FILE *f) {
24.    /* Transmit the character using USART1 */
25.    USART_SendData(USART1, (uint8_t) ch);
26. 
27.    /* Wait until transmit finishes */
28.    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {}
29.             
30.    return ch;
31.}
32. 
33.int fgetc(FILE *f) {
34.    uint8_t data = 0x00;
35. 
36.    if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
37.        data = USART_ReceiveData(USART1);
38. 
39.    return data;
40.}
41. 
42. 
43.int ferror(FILE *f) {
44.  /* Your implementation of ferror */
45.  return EOF;
46.}
47. 
48. 
49.void _ttywrch(int ch) {
50.        /* Transmit the character using USART1 */
51.    USART_SendData(USART1, (uint8_t) ch);
52. 
53.    /* Wait until transmit finishes */
54.    while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) {}
55.}
56. 
57. 
58.void _sys_exit(int return_code) {
59.  while (1);    /* endless loop */
60.}




Outcomes