Forum: Mikrocontroller und Digitale Elektronik STM32 scanf-retarget


von Christoph A. (shadowrunner93)


Lesenswert?

Mahlzeit :)

Hier mein Beitrag aus dem STM32 Forum, hoffe ihr hier könnt mir helfen 
;)


While trying to retarget the printf() and scanf() functions, with printf 
everything worked well.
However, with scanf occured a weird problem.

First here are the overwrited low-level functions :
1
size_t __write(int handle, const unsigned char * buffer, size_t size)
2
{
3
  size_t nChars = 0;
4
5
  if (buffer == 0)
6
  {
7
    /*
8
     * This means that we should flush internal buffers.  Since we
9
     * don't we just return.  (Remember, "handle" == -1 means that all
10
     * handles should be flushed.)
11
     */
12
    return 0;
13
  }
14
15
  for (/* Empty */; size != 0; --size)
16
  {
17
    
18
    while(!(USART1->SR & USART_FLAG_TXE));
19
      USART1->DR = ((*buffer++) & 0x01FF);
20
21
    ++nChars;
22
  }
23
24
  return nChars;
25
}
26
27
size_t __read(int handle, unsigned char * buffer, size_t size)
28
{
29
  int nChars = 0;
30
31
  for (/* Empty */; size > 0; --size)
32
  {
33
    
34
    while(!(USART1->SR & USART_FLAG_RXNE));
35
    int c = USART1->DR & 0x01FF;
36
    if (c < 0)
37
      break;
38
39
    *buffer++ = c;
40
    ++nChars;
41
  }
42
43
  return nChars;
44
}
45
46
while (1)
47
{
48
  scanf("%i \n",&iNumb);
49
  printf("%i\n",iNumb);
50
}

I set a breakpoint into the __read-method() and I noticed something,I 
think that's the point:

Sending '1', the while-loop becomes true and the integer 'c' equals '1'. 
(Everythings fine)
The failure is, that the program jumps a second time into the function 
and get stuck in the while-loop because there's no second character. The 
result is : sending a second character, the first one is displayed and 
so on...

For better understanding what my problem is, here's the Terminal output:

1
2
Echo from STM32: 1
3
Echo from STM32: 2
4
Echo from STM32: 3
5
Echo from STM32: 4
6
Echo from STM32: 5
7
Echo from STM32: 6
8
Echo from STM32: 7
10
Echo from STM32: 8
11
12
Echo from STM32: 1011

The behavior with two-digit numbers is a little different...

Hope you can help me with this one! :)

Thank you in advance!
Chris
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.