jeudi 21 juillet 2016

printf only prints characters up to stdout's buffer size

I'm trying to use newlib's printf function to print something on a UART device. Therefor I implemented

int _write(int file, char *ptr, int len) {
  HAL_StatusTypeDef status;

  switch (file) {
  case STDOUT_FILENO: /*stdout*/
  case STDERR_FILENO: /* stderr */
    while ((status = HAL_UART_Transmit_IT(&UART_Uart2Handle, ptr, len)) == HAL_BUSY) {}

    if (status != HAL_ERROR) {
      errno = EBADF;
      return -1;
    }
    break;
  default:
    errno = EBADF;
    return -1;
  }
  return len;
}

Printing works fine until the buffer of stdout is filled, e.g. if I try

setvbuf(stdout, NULL, _IOFBF, 3);
printf("Hello World!n");
fflush(stdout); // Just to be sure ;)

I only receive a 'Hel' on the device. A similar behaviour is observed with _IONBF or _IOLBF. When I set a breakpoint on _write in the debugger I gets called only once for the content of one buffer size.

Any ideas or hints why _write does not get called more often?

Aucun commentaire:

Enregistrer un commentaire