• AVR Freaks

AnsweredHot!Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions

Author
FabFalchion
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/07/24 13:45:42
  • Location: 0
  • Status: offline
2019/08/07 13:42:18 (permalink)
0

Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions

Hi everyone, so I'm currently using some MLA code from v2018_11_26 located in:
 
C:\microchip\mla\v2018_11_26\apps\usb\device\cdc_basic\firmware\exp16_pic24fj64gb004_pim.x
 
I'm trying to get a serial output. but no matter what I do, it seems it skips a line of whatever i'm trying to print. I'm currently working inside the app_device_cdc_basic.c file.
-----------
 
Ex: I try to print...
1) "Hello World"
2) "Foo"
3) "Bar"
 
but, in the serial terminal, i only see "Hello World" and "Bar"
 
 
 
Details
I have a test array of data just to try outputting values from an array, as well as a buffer to store that data in after I use utoa() to convert it from an unsigned 8 bit integer to a char so that I can use one of the available functions to print it: putUSBUSART(), putsUSBUSART(), putrsUSBUSART(). 
 
// a test array of data 
uint8_t data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                           0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                           0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                           0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                           0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
                           0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A };


// buffer for utoa()
char data_convert[50];

 
My code looks like this inside the app_device_cdc_basic.c file.
 
/*******************************************************************************
Copyright 2016 Microchip Technology Inc. (www.microchip.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

To request to license the code under the MLA license (www.microchip.com/mla_license),
please contact mla_licensing@microchip.com
*******************************************************************************/

/** INCLUDES *******************************************************/
#include "system.h"

    #define FCY 32000000UL
    #include <libpic30.h> // __delayXXX() functions macros defined here


#include <stdint.h>
#include <string.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>


#include "usb.h"
#include "app_led_usb_status.h"
#include "app_device_cdc_basic.h"
#include "usb_config.h"

/** VARIABLES ******************************************************/

static bool buttonPressed;
//static char buttonMessage[] = "HELLO \r\n";

// a test array of data
uint8_t data[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                  0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                  0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                  0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                  0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
                  0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A};

char data_convert[50]; // // the converted data to a buffer

int i=0;

static uint8_t readBuffer[CDC_DATA_OUT_EP_SIZE];
static uint8_t writeBuffer[CDC_DATA_IN_EP_SIZE];


/*********************************************************************
* Function: void APP_DeviceCDCBasicDemoInitialize(void);
*
* Overview: Initializes the demo code
*
* PreCondition: None
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCDCBasicDemoInitialize()
{
    line_coding.bCharFormat = 0;
    line_coding.bDataBits = 8;
    line_coding.bParityType = 0;
    line_coding.dwDTERate = 9600;

    buttonPressed = false;
}

/*********************************************************************
* Function: void APP_DeviceCDCBasicDemoTasks(void);
*
* Overview: Keeps the demo running.
*
* PreCondition: The demo should have been initialized and started via
* the APP_DeviceCDCBasicDemoInitialize() and APP_DeviceCDCBasicDemoStart() demos
* respectively.
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_DeviceCDCBasicDemoTasks()
{

    /* If the USB device isn't configured yet, we can't really do anything
     * else since we don't have a host to talk to. So jump back to the
     * top of the while loop. */
    if( USBGetDeviceState() < CONFIGURED_STATE )
    {
        return;
    }

    /* If we are currently suspended, then we need to see if we need to
     * issue a remote wakeup. In either case, we shouldn't process any
     * keyboard commands since we aren't currently communicating to the host
     * thus just continue back to the start of the while loop. */
    if( USBIsDeviceSuspended()== true )
    {
        return;
    }
        
    /* If the user has pressed the button associated with this demo, then we
     * are going to send a "Button Pressed" message to the terminal.
     */
    if(BUTTON_IsPressed(BUTTON_DEVICE_CDC_BASIC_DEMO) == true)
    {
        /* Make sure that we only send the message once per button press and
         * not continuously as the button is held.
         */
        if(buttonPressed == false)
        {
            /* Make sure that the CDC driver is ready for a transmission.
             */
            if(mUSBUSARTIsTxTrfReady() == true)
            {
                // 10
                putrsUSBUSART("10. Current: ");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                utoa(data_convert, data[10], 10);
                putsUSBUSART(data_convert);
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                putrsUSBUSART(" \r\n");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                
                // 11
                putrsUSBUSART("11. Average Current: ");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                utoa(data_convert, data[11], 10);
                putsUSBUSART(data_convert);
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                putrsUSBUSART(" \r\n");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                // 12
                putrsUSBUSART("12. Max Error: ");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);

                utoa(data_convert, data[12], 10);
                putsUSBUSART(data_convert);
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);

                putrsUSBUSART(" \r\n");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                // 13
                putrsUSBUSART("13. Relative State Of Charge: ");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                utoa(data_convert, data[13], 10);
                putsUSBUSART(data_convert);
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                putrsUSBUSART(" \r\n");
                CDCTxService();
                USBDeviceTasks();
                __delay_us(200);
                
                
                
                buttonPressed = true;
            }
        }
    }
    else
    {
        /* If the button is released, we can then allow a new message to be
         * sent the next time the button is pressed.
         */
        buttonPressed = false;
    }

    /* Check to see if there is a transmission in progress, if there isn't, then
     * we can see about performing an echo response to data received.
     */
    if( USBUSARTIsTxTrfReady() == true)
    {
        uint8_t i;
        uint8_t numBytesRead;

        numBytesRead = getsUSBUSART(readBuffer, sizeof(readBuffer));

        /* For every byte that was read... */
        for(i=0; i<numBytesRead; i++)
        {
            switch(readBuffer[i])
            {
                /* If we receive new line or line feed commands, just echo
                 * them direct.
                 */
                case 0x0A:
                case 0x0D:
                    writeBuffer[i] = readBuffer[i];
                    break;
                /* If we receive something else, then echo it plus one
                 * so that if we receive 'a', we echo 'b' so that the
                 * user knows that it isn't the echo enabled on their
                 * terminal program.
                 */
                default:
                    //writeBuffer[i] = readBuffer[i] +1;
                    writeBuffer[i] = readBuffer[i];
                    break;
            }
        }

        if(numBytesRead > 0)
        {
            /* After processing all of the received data, we need to send out
             * the "echo" data now.
             */
            putUSBUSART(writeBuffer,numBytesRead);
        }
    }

    CDCTxService();
}

 

I've tried to do lots, such as changing baud rate, introducing delays, etc, but to no avail. I have a feeling that my strings are simply being fed in too fast, but i'm not sure if thats a part of the given puts functions, or something I can control from this c file. Any help is appreciated!
post edited by FabFalchion - 2019/08/07 13:55:50
#1
Antipodean
Super Member
  • Total Posts : 1732
  • Reward points : 0
  • Joined: 2008/12/09 10:19:08
  • Location: Didcot, United Kingdom
  • Status: offline
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/07 14:03:05 (permalink)
0
FabFalchion
...
Details
I have a test array of data just to try outputting values from an array, as well as a buffer to store that data in after I use utoa() to convert it from an unsigned 8 bit integer to a char so that I can use one of the available functions to print it: putUSBUSART(), putsUSBUSART(), putrsUSBUSART(). 
 
// a test array of data 
uint8_t data[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                           0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                           0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                           0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                           0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23,
                           0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A };

}

 

I've tried to do lots, such as changing baud rate, introducing delays, etc, but to no avail. I have a feeling that my strings are simply being fed in too fast, but i'm not sure if thats a part of the given puts functions, or something I can control from this c file. Any help is appreciated!




If that is your real data what are you really getting at the receive end. You have mostly control characters in there and the CDC driver is not necessarily transparent to them.
 
 
 
 

Do not use my alias in your message body when replying, your message will disappear ...

Alan
#2
Jim Nickerson
User 452
  • Total Posts : 6185
  • Reward points : 0
  • Joined: 2003/11/07 12:35:10
  • Location: San Diego, CA
  • Status: online
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/16 09:46:48 (permalink)
0
missing post
 
#3
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11285
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/16 09:49:07 (permalink)
0
control characters in there and the CDC driver is not necessarily transparent to them.

 
CDC is a completely transparent data pipe.
#4
jtemples
عُضْوٌ جَدِيد
  • Total Posts : 11285
  • Reward points : 0
  • Joined: 2004/02/13 12:31:19
  • Location: Southern California
  • Status: offline
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/16 09:51:54 (permalink)
0
 
                __delay_us(200);

 
I would start by getting rid of all these delays.
#5
newfound
Super Member
  • Total Posts : 1837
  • Reward points : 0
  • Joined: 2003/11/07 12:35:49
  • Status: offline
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/16 10:07:10 (permalink) ☼ Best Answerby FabFalchion 2019/08/17 10:58:05
0
 
CDCTxService() runs a state machine. To progress through the states to get back to the "ready" state you need to call CDCTxService() at least TWICE for every transaction. Not just that, between some states, you need to have the calls to CDCTxService() spaced far enough apart in time that the packet would have been sent between calls to it. That means at least a time gap of 1ms minimum between some states. 
 
I would suggest, as a more efficient scheme, calls to CDCTxService() be put in a polling loop at immediately before trying to send more payload data.
 

do {
CDCTxService();
} while (cdc_trf_state != CDC_TX_READY)

 
IMHO it is an absolutely terrible API, poorly documented and one I never use as it can be very wasteful under some circumstances.  
 
Please note that what I am suggesting is general in nature and may not cover every "gotcha" as I do not use this API for my USB work. However, it is a good starting point for you to understand how the API works and what is wrong with your code. And... It may even work too.
 
 
 
 
#6
ric
Super Member
  • Total Posts : 23546
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/18 00:25:24 (permalink)
0
There's a ghost post at the end of this thread.
It's probably one quoting the post by anti-podean.
As his signature says, that will cause your post to disappear, due to no fault of his.

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#7
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3233
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: Skipping lines in Serial Output with MLA USB_Device_CDC_Basic functions 2019/08/18 08:35:58 (permalink)
0
1. Mask control code, keep CR etc..
2. Replace ascii char < 32 or > 126 with a period before sending to a console.
 
In the data[] table, ctrl code 7 will cause the console to make a sound. (0x07 BELL).

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#8
Jump to:
© 2019 APG vNext Commercial Version 4.5