Psoc printf

Forum: 

La funzione printf() su piattaforma PSoC non è immediatamente disponibile, del resto non esiste uno stdout occorre simularlo per utilizzare la UART e quindi indirizzare i risultati di printf() su un emulatore di terminale, principalmente per operazioni di debug.

Il codice suggerito è questo:

debug.c

  1. /* ========================================
  2.  *
  3.  * Copyright YOUR COMPANY, THE YEAR
  4.  * All Rights Reserved
  5.  * UNPUBLISHED, LICENSED SOFTWARE.
  6.  *
  7.  * CONFIDENTIAL AND PROPRIETARY INFORMATION
  8.  * WHICH IS THE PROPERTY OF your company.
  9.  *
  10.  * ========================================
  11. */
  12.  
  13. #include "debug.h"
  14.  
  15. // Ricroda che devi aumentare Heap Size a 0x200 !!!
  16. #if defined(__ARMCC_VERSION)
  17.  
  18. /* For MDK/RVDS compiler revise fputc function for printf functionality */
  19. struct __FILE
  20. {
  21.     int handle;
  22. };
  23.  
  24. enum
  25. {
  26.     STDIN_HANDLE,
  27.     STDOUT_HANDLE,
  28.     STDERR_HANDLE
  29. };
  30.  
  31. FILE __stdin = {STDIN_HANDLE};
  32. FILE __stdout = {STDOUT_HANDLE};
  33. FILE __stderr = {STDERR_HANDLE};
  34.  
  35. int fputc(int ch, FILE *file)
  36. {
  37.     int ret = EOF;
  38.  
  39.     switch( file->handle )
  40.     {
  41.         case STDOUT_HANDLE:
  42.             UART_DEB_UartPutChar(ch);
  43.             ret = ch ;
  44.             break ;
  45.  
  46.         case STDERR_HANDLE:
  47.             ret = ch ;
  48.             break ;
  49.  
  50.         default:
  51.             file = file;
  52.             break ;
  53.     }
  54.     return ret ;
  55. }
  56.  
  57. #elif defined (__ICCARM__)      /* IAR */
  58.  
  59. /* For IAR compiler revise __write() function for printf functionality */
  60. size_t __write(int handle, const unsigned char * buffer, size_t size)
  61. {
  62.     size_t nChars = 0;
  63.  
  64.     if (buffer == 0)
  65.     {
  66.         /*
  67.          * This means that we should flush internal buffers.  Since we
  68.          * don't we just return.  (Remember, "handle" == -1 means that all
  69.          * handles should be flushed.)
  70.          */
  71.         return (0);
  72.     }
  73.  
  74.     for (/* Empty */; size != 0; --size)
  75.     {
  76.         UART_DEB_UartPutChar(*buffer++);
  77.         ++nChars;
  78.     }
  79.  
  80.     return (nChars);
  81. }
  82. #else  /* (__GNUC__)  GCC */
  83.  
  84. /* For GCC compiler revise _write() function for printf functionality */
  85.  
  86. int _write(int file, char *ptr, int len)
  87. {
  88.     int i;
  89.     file = file;
  90.  
  91.     for (i = 0; i < len; i++)
  92.     {
  93.         UART_UartPutChar(*ptr++);
  94.     }
  95.     return len;
  96. }
  97.  
  98. #endif  /* (__ARMCC_VERSION) */  

debug.h

  1. #ifndef _DEBUG_H_
  2.  
  3.     #define _DEBUG_H_
  4.     #include "stdio.h"
  5.  
  6. #endif

 

Occorre, inoltre ampliare l'Heap size a 0x200 o meglio 0x400 se non ci sono problemi di disponibilità di memoria.

Naturalmente, visto che si fa uso della UART, occorre avviare il relativo componente

UART_Start();