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

about scanf retarget in Cortexm3 of SmartFuson of Actel

Hi,

I have one question about Cortexm3 from smartfusion device from actel. Could you pls help me?
I add follwing codes into retarget.c and then add retarget.c into source code group and find :
printf function works.however scanf function doesn't. anybody knows why?

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

int fgetc(FILE *f) {
int i=0;
i=ITM_ReceiveChar();
if((-1)!=i)
        return ((int)ITM_SendChar((uint32_t)i));
else
        return (0);
}
  printf("hello world\n\r"); // when executed, string displayed in debug(printf)window
  scanf("%s",str);                // when executed, hard fault occurs

Parents
  • Hello Fisher Cheng,

    µVision provides for a CM3/CM4 debug support via ITM (Instrumented Trace Macrocell).
    In a debug session the Debug (printf) Viewer window is used to display the debug data.

    Direction: Microcontroller -> µVision:
    * Characters received via ITM communication channel 0 are written in a printf style to Debug (printf) Viewer window.

    Direction: µVision -> Microcontroller:
    * variable ITM_RxBuffer is used for data transfer.
    * Check if ITM_RxBuffer variable is available (only performed once).
    * Read character from Debug (printf) Viewer window.
    * If ITM_RxBuffer empty write character to ITM_RxBuffer.

    Note: Current solution does not use a buffer mechanism for transmitting the characters.

    For my tests (only simulation) I used following code in retarget.c

    #include <stdio.h>
    #include <rt_misc.h>
    #include "a2fxxxm3.h"
    
    #pragma import(__use_no_semihosting_swi)
    
    volatile int ITM_RxBuffer = ITM_RXBUFFER_EMPTY;       /* used for Debug Input */
    
    struct __FILE { int handle; };
    FILE __stdout;
    FILE __stdin;
    
    
    int fputc(int ch, FILE *f) {
      return (ITM_SendChar(ch));
    }
    
    int fgetc(FILE *f) {                /* blocking */
      while (ITM_CheckChar() != 1);
      return (ITM_ReceiveChar());
    }
    
    int ferror(FILE *f) {
      /* Your implementation of ferror */
      return EOF;
    }
    
    void _ttywrch(int ch) {
      ITM_SendChar(ch);
    }
    
    void _sys_exit(int return_code) {
    label:  goto label;  /* endless loop */
    }
    

    Do not use MicroLib if you are using scanf(..)

    Best Regards,
    Martin Günther

Reply
  • Hello Fisher Cheng,

    µVision provides for a CM3/CM4 debug support via ITM (Instrumented Trace Macrocell).
    In a debug session the Debug (printf) Viewer window is used to display the debug data.

    Direction: Microcontroller -> µVision:
    * Characters received via ITM communication channel 0 are written in a printf style to Debug (printf) Viewer window.

    Direction: µVision -> Microcontroller:
    * variable ITM_RxBuffer is used for data transfer.
    * Check if ITM_RxBuffer variable is available (only performed once).
    * Read character from Debug (printf) Viewer window.
    * If ITM_RxBuffer empty write character to ITM_RxBuffer.

    Note: Current solution does not use a buffer mechanism for transmitting the characters.

    For my tests (only simulation) I used following code in retarget.c

    #include <stdio.h>
    #include <rt_misc.h>
    #include "a2fxxxm3.h"
    
    #pragma import(__use_no_semihosting_swi)
    
    volatile int ITM_RxBuffer = ITM_RXBUFFER_EMPTY;       /* used for Debug Input */
    
    struct __FILE { int handle; };
    FILE __stdout;
    FILE __stdin;
    
    
    int fputc(int ch, FILE *f) {
      return (ITM_SendChar(ch));
    }
    
    int fgetc(FILE *f) {                /* blocking */
      while (ITM_CheckChar() != 1);
      return (ITM_ReceiveChar());
    }
    
    int ferror(FILE *f) {
      /* Your implementation of ferror */
      return EOF;
    }
    
    void _ttywrch(int ch) {
      ITM_SendChar(ch);
    }
    
    void _sys_exit(int return_code) {
    label:  goto label;  /* endless loop */
    }
    

    Do not use MicroLib if you are using scanf(..)

    Best Regards,
    Martin Günther

Children