0

I am trying to use the DE1-SoC board to run this program. It is supposed to allow the user to input a character, and return that letter in binary on red LEDs on the board. It uses two functions that take in user input and displays the execution to the terminal. When I run the program, random characters (like å) get output and not regular characters.

Here's my code.

#include "JTAG_UART.h"
#include "address_map_arm.h"

int main(void) {
    /* Declare volatile pointers to I/O registers (volatile means that IO load
       and store instructions will be used to access these pointer locations,
       instead of regular memory loads and stores) */
    volatile int * JTAG_UART_ptr = (int *)JTAG_UART_BASE; // JTAG UART address
volatile int * LED_ptr = (int*)LED_BASE;
    char  text_string[] = "\nJTAG UART example code\n> \0";
    char *str, * c;
  //  char *c_ptr=c;

    /* print a text string */
    for (str = text_string; *str != 0; ++str)
        put_jtag(JTAG_UART_ptr, *str);

    /* read and echo characters */
    while (1) {
        c = get_jtag(JTAG_UART_ptr);
         if (c != 0 && c<123 && c>96){
         *LED_ptr = *c ;
         put_jtag(JTAG_UART_ptr, *c);
        }
           // put_jtag(JTAG_UART_ptr, c);
    }
}

This is the code for the functions I referenced.

#include "JTAG_UART.h"

/*******************************************************************************
 * Subroutine to send a character to the JTAG UART
 ******************************************************************************/
void put_jtag(volatile int * JTAG_UART_ptr, char c) {
    int control;
    control = *(JTAG_UART_ptr + 1); // read the JTAG_UART control register
    if (control & 0xFFFF0000)       // if space, echo character, else ignore
        *(JTAG_UART_ptr) = c;
}

/*******************************************************************************
 * Subroutine to read a character from the JTAG UART
 * Returns \0 if no character, otherwise returns the character
 ******************************************************************************/
char get_jtag(volatile int * JTAG_UART_ptr) {
    int data;
    data = *(JTAG_UART_ptr); // read the JTAG_UART data register
    if (data & 0x00008000)   // check RVALID to see if there is new data
        return ((char)data & 0xFF);
    else
        return ('\0');
}

Inputting a character like 'a' which is the decimal number 97 in ASCII. Should display itself as 01100001 with each '1' representing itself lit up on the board. As I stated I'm having a logic error, while input is being read 'a' would appear as 00010000

4
  • 1
    To begin with, you should drop the "sloppy typing". You definitely never want to use signed int type for 32 bit registers, replace that with uint32_t. Only use char for actual characters, never for raw data - use uint8_t for that. Regarding your hex literals, you should end them all with ul to ensure that they are always of the same type. Once you've gotten rid of the subtle signed number bug potential, you'll be in a much better position to trouble-shoot the problem.
    – Lundin
    May 18, 2020 at 9:52
  • Are you sure your compiler did not complain about the assignment of a char to a char* here: c = get_jtag(JTAG_UART_ptr); !?
    – Clifford
    May 18, 2020 at 19:48
  • My answer does not address @Lundin's criticisms, because they do not address the specific issue you are asking about - but do not ignore them, they address other issues.
    – Clifford
    May 18, 2020 at 20:08
  • Don't vandalize your questions. If you want to mark it solved, click the checkmark under the vote arrows on an answer. May 22, 2020 at 2:47

1 Answer 1

1

You have defined c as a char* when it should clearly be a char.

char c ;

Then loose the *c de-references:

*LED_ptr = c ;
put_jtag(JTAG_UART_ptr, c);

The line:

 c = get_jtag(JTAG_UART_ptr);

should have issued a warning; GCC for example outputs:

warning: initialization makes pointer from integer without a cast [-Wint-conversion]

Do not ignore (or disable) warnings; at least don't ignore them then ask a question here without mentioning the warning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.