PrintOut and sprintf strange behaviour

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
AndyAus
Posts: 1
Joined: 28 Sep 2021 15:38

PrintOut and sprintf strange behaviour

#1 Post by AndyAus » 28 Sep 2021 16:13

Hello,

I have observed very strange behaviour with the PrintOut and sprintf functions when trying to output debug information over UART for a project that I am working on.
I am using MicroC Pro compiler for PIC32, version 4.0.0.
My target chip is PIC32MX795F512L on a chipKit MAX32 development board, with chipKIT Network shield attached.
My project was making use of USB peripheral. I had been using PrintOut and sprintf functions to show debug information when receiving USB packets.
Once I noticed the odd behaviour, I created a stand-alone project based on the PrintOut.c example code included in the Examples folder.
I then added relevant code to try and replicate the issue.

The code is:

Code: Select all

volatile unsigned short USB_BD0_In_Buffer[64] = {0};

union {
    struct {
        unsigned short bLength;
        unsigned short bDescriptorType;
        unsigned int bcdUSB;
        unsigned short bDeviceClass;
        unsigned short bDeviceSubClass;
        unsigned short bDeviceProtocol;
        unsigned short bMaxPacketSize;
        unsigned int idVendor;
        unsigned int idProduct;
        unsigned int bcdDevice;
        unsigned short iManufacturer;
        unsigned short iProduct;
        unsigned short iSerialNumber;
        unsigned short bNumConfigurations;
    };
    unsigned short whole;
} USB_Device_Descriptor;

char  text_out_buffer[15];

void PrintHandler(char c) {
  UART1_Write(c);
}

void main() {
  AD1PCFG = 0xFFFF;         // Configure AN pins as digital I/O
  DDPCON.JTAGEN=0;
  
  UART1_Init(115200);        // Initialize Uart at 115200 bps
  Delay_ms(100);

  PrintOut(PrintHandler,"\r\nSTART\r\n\r\n");

  USB_BD0_In_Buffer[0] = 18;
  USB_BD0_In_Buffer[1] = 1;
  USB_BD0_In_Buffer[2] = 0;
  USB_BD0_In_Buffer[3] = 2;
  USB_BD0_In_Buffer[4] = 0;
  USB_BD0_In_Buffer[5] = 0;
  USB_BD0_In_Buffer[6] = 0;
  USB_BD0_In_Buffer[7] = 8;

  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[0] = 18\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[1] = 1\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[2] = 0\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[3] = 2\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[4] = 0\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[5] = 0\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[6] = 0\r\n");
  PrintOut(PrintHandler,"True value of USB_BDO_In_Buffer[7] = 8\r\n");
  
  PrintOut(PrintHandler,"\r\n");

  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[0] = %u\r\n",USB_BD0_In_Buffer[0]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[1] = %u\r\n",USB_BD0_In_Buffer[1]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[2] = %u\r\n",USB_BD0_In_Buffer[2]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[3] = %u\r\n",USB_BD0_In_Buffer[3]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[4] = %u\r\n",USB_BD0_In_Buffer[4]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[5] = %u\r\n",USB_BD0_In_Buffer[5]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[6] = %u\r\n",USB_BD0_In_Buffer[6]);
  PrintOut(PrintHandler,"PrintOut function on USB_BD0_In_Buffer[7] = %u\r\n",USB_BD0_In_Buffer[7]);
  
  PrintOut(PrintHandler,"\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[0]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[0] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[1]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[1] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[2]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[2] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[3]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[3] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[4]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[4] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[5]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[5] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[6]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[6] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  sprintf(text_out_buffer, "%u", USB_BD0_In_Buffer[7]);
  UART1_Write_Text("sprintf conversion of USB_BD0_In_Buffer[7] = ");
  UART1_Write_Text(text_out_buffer);
  UART1_Write_Text("\r\n");
  
  UART1_Write_Text("\r\n");

  USB_Device_Descriptor.bLength = USB_BD0_In_Buffer[0];
  USB_Device_Descriptor.bDescriptorType = USB_BD0_In_Buffer[1];
  USB_Device_Descriptor.bcdUSB = (USB_BD0_In_Buffer[3] << 8) + USB_BD0_In_Buffer[2];
  USB_Device_Descriptor.bDeviceClass = USB_BD0_In_Buffer[4];
  USB_Device_Descriptor.bDeviceSubClass = USB_BD0_In_Buffer[5];
  USB_Device_Descriptor.bDeviceProtocol = USB_BD0_In_Buffer[6];
  USB_Device_Descriptor.bMaxPacketSize = USB_BD0_In_Buffer[7];

  UART1_Write_Text("True value of bLength = 18\r\n");
  UART1_Write_Text("True value of bDescriptorType = 1\r\n");
  UART1_Write_Text("True value of bcdUSB = 512\r\n");
  UART1_Write_Text("True value of bDeviceClass = 0\r\n");
  UART1_Write_Text("True value of bDeviceSubClass = 0\r\n");
  UART1_Write_Text("True value of bDeviceProtocol = 0\r\n");
  UART1_Write_Text("True value of bMaxPacketSize = 8\r\n");
 
  
  UART1_Write_Text("\r\n");


  PrintOut(PrintHandler,"PrintOut of bLength = %u\r\n",USB_Device_Descriptor.bLength);
  PrintOut(PrintHandler,"PrintOut of bDescriptorType = %u\r\n",USB_Device_Descriptor.bDescriptorType);
  PrintOut(PrintHandler,"PrintOut of bcdUSB = %u\r\n",USB_Device_Descriptor.bcdUSB);
  PrintOut(PrintHandler,"PrintOut of bDeviceClass = %u\r\n",USB_Device_Descriptor.bDeviceClass);
  PrintOut(PrintHandler,"PrintOut of bDeviceSubClass = %u\r\n",USB_Device_Descriptor.bDeviceSubClass);
  PrintOut(PrintHandler,"PrintOut of bDeviceProtocol = %u\r\n",USB_Device_Descriptor.bDeviceProtocol);
  PrintOut(PrintHandler,"PrintOut of bMaxPacketSize = %u\r\n",USB_Device_Descriptor.bMaxPacketSize);
}
The code compiles and runs successfully. The output of the UART stream is as follows:

Code: Select all

START

True value of USB_BDO_In_Buffer[0] = 18
True value of USB_BDO_In_Buffer[1] = 1
True value of USB_BDO_In_Buffer[2] = 0
True value of USB_BDO_In_Buffer[3] = 2
True value of USB_BDO_In_Buffer[4] = 0
True value of USB_BDO_In_Buffer[5] = 0
True value of USB_BDO_In_Buffer[6] = 0
True value of USB_BDO_In_Buffer[7] = 8

PrintOut function on USB_BD0_In_Buffer[0] = 26642
PrintOut function on USB_BD0_In_Buffer[1] = 26625
PrintOut function on USB_BD0_In_Buffer[2] = 26624
PrintOut function on USB_BD0_In_Buffer[3] = 26626
PrintOut function on USB_BD0_In_Buffer[4] = 26624
PrintOut function on USB_BD0_In_Buffer[5] = 26624
PrintOut function on USB_BD0_In_Buffer[6] = 26624
PrintOut function on USB_BD0_In_Buffer[7] = 26632

sprintf conversion of USB_BD0_In_Buffer[0] = 26642
sprintf conversion of USB_BD0_In_Buffer[1] = 257
sprintf conversion of USB_BD0_In_Buffer[2] = 256
sprintf conversion of USB_BD0_In_Buffer[3] = 258
sprintf conversion of USB_BD0_In_Buffer[4] = 256
sprintf conversion of USB_BD0_In_Buffer[5] = 512
sprintf conversion of USB_BD0_In_Buffer[6] = 512
sprintf conversion of USB_BD0_In_Buffer[7] = 520

True value of bLength = 18
True value of bDescriptorType = 1
True value of bcdUSB = 512
True value of bDeviceClass = 0
True value of bDeviceSubClass = 0
True value of bDeviceProtocol = 0
True value of bMaxPacketSize = 8

PrintOut of bLength = 18
PrintOut of bDescriptorType = 1
PrintOut of bcdUSB = 512
PrintOut of bDeviceClass = 512
PrintOut of bDeviceSubClass = 512
PrintOut of bDeviceProtocol = 512
PrintOut of bMaxPacketSize = 520
Can someone please help me to understand why the PrintOut function appears to show values close to "26642" for the first few function calls?
I've been struggling with this issue for a long time and I can't figure out what might be going wrong.
Sometimes the function behaves itself for a while, but I add more function calls and it no longer displays correctly.

One observation is with the very last function calls (see last bits of UART printout):
The PrintOut function seems to work for "unsigned short" data type, then successfully displays "unsigned int" data type, but then fails when switching back to "unsigned short" type.
I.e. it works for bLength, bDescriptorType and bcdUSB, but then all subsequent values (bDeviceClass, bDeviceSubClass, bDeviceProtocol and bMaxPacketSize) appear shifted by value of 512.

Would greatly appreciate any help that could be offered to find out what is happening in this instance.
I can provide ZIP file of project files if anyone is curious and wishes to try and replicate.


Regards,
-Andrew

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: PrintOut and sprintf strange behaviour

#2 Post by filip » 19 Oct 2021 09:14

Hi,

Can you please attach a minimal project that demonstrates this issue ?

Regards,
Filip.

Post Reply

Return to “mikroC PRO for PIC32 General”