element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Achievement Levels
    • Benefits of Membership
    • Feedback and Support
    • Members Area
    • Personal Blogs
    • What's New on element14
  • Learn
    Learn
    • eBooks
    • Learning Center
    • Learning Groups
    • STEM Academy
    • Webinars, Training and Events
  • Technologies
    Technologies
    • 3D Printing
    • Experts & Guidance
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Arduino Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Project Groups
    • Raspberry Pi Projects
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Or choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
Avnet Boards Forums
  • Products
  • Dev Tools
  • Avnet Boards Community
  • Avnet Boards Forums
  • More
  • Cancel
Avnet Boards Forums
Software Application Development kbhit() function for bare metal
  • Forum
  • Documents
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Avnet Boards Forums requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • State Not Answered
  • Replies 4 replies
  • Subscribers 176 subscribers
  • Views 375 views
  • Users 0 members are here
Related

kbhit() function for bare metal

chipslinger
chipslinger over 10 years ago

I am looking for a kbhit() function. I'm starting from the helloworld application, using a UART. Can anyone let me know where this is located?

(There doesn't seem to be a conio.h include file anywhere).


If not, which file contains the software uart routines? Perhaps I can add a kbhit() into this file.


fyi: kbhit() is a non-blocking version of getchar() that returns 1 when a key is hit. It allows you to see if a character has been hit without waiting for it (e.g. for "escaping" out of a loop).


Thanks.

  • Sign in to reply
  • Cancel
  • zedhed
    0 zedhed over 10 years ago

    Hi chipslinger,

    For the LX9 MicroBoard, which used MicroBlaze, we wrote our own line handler function.  Keep in mind, that hardware design used a UARTLite device instead of the PS UART so there is a call to XUartLite_Recv() to get the contents of the UART receive buffer which was non-blocking.  In looking through the Zynq Standalone BSP, it looks like there is a similar function for the PS UART code file xuartps.c named XUartPs_Recv() which is also non-blocking.

    // ---------------------------------------------------------------------------
    int my_get_line_poll(char * line, int maxlen)
    {
        int buffer_index;
        char character_copy = 0;
        u8 DataBuffer[MAX_LINE_LENGTH];
        unsigned int received_char_count = 0;
        static unsigned int total_received_char_count = 0;
       
        // Make a call to UartLite instance to check for new characters
        // coming in.
        received_char_count = XUartLite_Recv(&UartLite, DataBuffer, (MAX_LINE_LENGTH - received_char_count));
       
        // Check to see if there were any characters received, otherwise return.
        if (received_char_count > 0)
        {
            for (buffer_index = 0; buffer_index < received_char_count; buffer_index++)
            {
                // Make sure that the line length has not been reached.
                if (total_received_char_count == MAX_LINE_LENGTH)
                {
                    // Force a line feed character, this is the end of the
                    // line that is being polled for.
                    outbyte('r');
                    outbyte('
    ');
                    // Null terminate the string so that it is still a
                    // valid string.
                    line[total_received_char_count] = 0;
                    // Reset the total character count in preparation for the
                    // next line yet to be received.
                    total_received_char_count = 0;
                    // Return the total count of characters in the current line.
                    return MAX_LINE_LENGTH;
                }
                // Get the next character that was received from the Uart Lite
                // device.
                character_copy = (char) DataBuffer[buffer_index];
               
                // Determine what action to take with the next received
                // character.
                if (character_copy == '
    ')
                {
                    // Ignore it.
                    ;
                }
                else if (character_copy == 'r')
                {
                    // A line feed character has been encountered, this is the
                    // end of the line that is being polled for.
                    outbyte('r');
                    outbyte('
    ');
                    // Null terminate the string so that it is still a
                    // valid string.
                    line[total_received_char_count] = 0;
                    // Determine the number of characters that are in the line.
                    received_char_count = total_received_char_count;
                    // Reset the total character count in preparation for the
                    // next line yet to be received.
                    total_received_char_count = 0;
                    // Return the total count of characters in the current line.
                    return received_char_count;
                }
                // Check for backspace or delete key.
                else if ((character_copy == 'b') || (character_copy == 0x7F))
                {
                    if (total_received_char_count > 0)
                    {
                        outbyte('b'); // Write backspace
                        outbyte(' ');  // Write space
                        outbyte('b'); // Write backspace
                        total_received_char_count--;
                        line[total_received_char_count] = 0;
                    }
                }
                // Check for escape key or control-U.
                else if ((character_copy == 0x1b) || (character_copy == 0x15))
                {
                    while (total_received_char_count > 0)
                    {
                        outbyte('b'); // Write backspace
                        outbyte(' ');  // Write space
                        outbyte('b'); // Write backspace
                        total_received_char_count--;
                        line[total_received_char_count] = 0;
                    }
                }
                else
                {
                    // Echo character back to the user.
                    outbyte(character_copy);
                    line[total_received_char_count] = character_copy;
                    total_received_char_count++;
                }
            }
        }
       
        return -1;
    }

    YIKES! Sorry about the way this forum mangles this C code, hopefully it is still somewhat readable on the other end.

    The intent of this function was to poll until a complete line with carriage return character was entered.  However, maybe doing something similar but limited to a single character would help you with what you need?

    Regards,

    -Kevin

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • chipslinger
    0 chipslinger over 10 years ago

    Yes, I think this function would work. Unfortunately, I haven't had any luck figuring out at what level in the code the structures/variables that this routine requires are maintained. I've grepped the code tree for the routines in this source file, but nothing comes up.

    Any ideas?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • zedhed
    0 zedhed over 10 years ago

    Hi chipslinger,

    Which routine are we talking about here XUartLite_Recv() or XUartPs_Recv()?

    If you are looking into using XUartPs_Recv(), I see the source code listed in project_14_2project_14_2.sdkSDKSDK_Exportstandalone_bsp_0ps7_cortexa9_0libsrcuartps_v1_01_asrcxuartps.c as I am using a 14.2 standalone example project.

    Looks like that function is going to need the XUartPS struct which is in the xuartps.h file:

    /**
    * The XUartPs driver instance data structure. A pointer to an instance data
    * structure is passed around by functions to refer to a specific driver
    * instance.
    */
    typedef struct {
    tXUartPs_Config Config;t/* Configuration data structure */
    tu32 InputClockHz;t/* Input clock frequency */
    tu32 IsReady;tt/* Device is initialized and ready */
    tu32 BaudRate;tt/* Current baud rate */

    tXUartPsBuffer SendBuffer;
    tXUartPsBuffer ReceiveBuffer;

    tXUartPs_Handler Handler;
    tvoid *CallBackRef;t/* Callback reference for event handler */
    } XUartPs;

    Regards,

    -Kevin

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
  • chipslinger
    0 chipslinger over 10 years ago

    I have a more basic problem.... Using the default Hello world program, getchar() doesn't work !!!

    I checked to make sure the txd/rxd pins are connected in the assignment chart, and I looked on the USB-Serial converter. When I hit a character, it isn't recognized...

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Verify Answer
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2023 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube