My experience with writing embedded C is pretty limited so there will probably be a simple solution to this question.
I am using a Microblaze softcore on a basys 3 FPGA development board and will have a state machine that will print the user some options from a main menu, and prompt the user to press some keys which will then advance the state machine to the correct place. However my question, is how should I compare to check which key the user has actually pressed?
I had firstly read the UART receive buffer using the built-in Xilinx functions which allowed me to see which characters were pressed, but then I realised I could maybe simplify this using the gets() C function. I have tried making the text 1 character long and comparing it with another character or string value in an if statement, but it never seems to evaluate to true (I have LEDs on the board connected to the outputs of the Microblaze so a specific character is pressed, I just set some LED pattern. Here the LED never comes on. Any advice?
EDIT
I have a feeling this if statement never evaluates true because I think I am comparing the address in memory where text is stored, and not the value of text possibly?
Here is the C code below:
#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"
#include "xgpio.h"
#include "xuartlite.h"
#define GPIO_EXAMPLE_DEVICE_ID XPAR_GPIO_0_DEVICE_ID
#define UARTLITE_DEVICE_ID XPAR_UARTLITE_0_DEVICE_ID
#define DATA 0b10101010
u8 RecvBuffer[1]; /* Buffer for Receiving Data */
int UartLiteSelfTestExample(u16 DeviceId);
XGpio Gpio; /* The Instance of the GPIO Driver */
XUartLite UartLite; /* Instance of the UartLite device */
int main()
{
init_platform();
int UART_Status;
int GPIO_Status;
UART_Status = UartLiteSelfTestExample(UARTLITE_DEVICE_ID);
if (UART_Status != XST_SUCCESS) {
xil_printf("Uartlite Setup Failed!\r\n");
}
else{
xil_printf("Uartlite Setup Successful!\r\n");
xil_printf("\n");
}
/* Initialise the GPIO driver */
GPIO_Status = XGpio_Initialize(&Gpio, GPIO_EXAMPLE_DEVICE_ID);
if (GPIO_Status != XST_SUCCESS) {
xil_printf("GPIO Initialisation Failed!\r\n");
xil_printf("\n");
}
else {
xil_printf("GPIO Initialisation Successful!\r\n");
xil_printf("\n");
}
/* Set the direction for all signals as outputs */
XGpio_SetDataDirection(&Gpio, 1, 0x00);
xil_printf("--------------------------------\n\r");
xil_printf(" Main Menu\n\r");
xil_printf("--------------------------------\n\r");
xil_printf("\n");
xil_printf("Options: 1 - Setup\r");
xil_printf(" 2 - Peek\r");
xil_printf(" 3 - Status\r");
xil_printf(" 4 - Run\r");
xil_printf("\n");
//MY QUESTION IS INSIDE THIS WHILE LOOP!
while(1){
char text[1];
gets(text);
xil_printf("%s", text);
if (text == "1"){
XGpio_DiscreteWrite(&Gpio, 1, 1);
}
}
cleanup_platform();
return 0;
}
/// function body ///
int UartLiteSelfTestExample(u16 DeviceId)
{
int Status;
/*
* Initialise the UartLite driver so that it is ready to use.
*/
Status = XUartLite_Initialize(&UartLite, DeviceId);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Perform a self-test to ensure that the hardware was built correctly.
*/
Status = XUartLite_SelfTest(&UartLite);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
return XST_SUCCESS;
}
```
getsfunction has been flagged as problematic for a very long time, which has mainly to do with potential for buffer overruns on a hosted system. But it's also a similar problem on embedded systems, in case you receive different UART input than you expected, for whatever reason. Consequentlygetswas flagged as obsolescent back in the 1990s and finally permanently removed from the C language in year 2011. Therefore, if your compiler allowed you use it still, your compiler is outdated and non-conforming. You might want to look into that. \$\endgroup\$