Jump to content
  • 0

How to solve XGpio_DiscreteRead() blocked by gets()?


billgas

Question

hello,

     I practice to use vivado 2018.2 to create two AXI GPIOs like the first picture. I could use C program to pass the ON/OFF from switchs to leds;however, the XGpio_DiscreteRead() is always blocked by gets() when i want to use gets() to get cmd,and write data to leds according cmd. The following  is my C code.Please introduce me to solve the problem. Thank you a lot.

#include "xparameters.h"
#include "xgpio.h"
#include <stdio.h>
#include <stdlib.h>
#include "sleep.h"
int main(){
        XGpio output,input;              /* SW &LED Instance */
        int button_data=0x00;
        int switch_data=0;
        int led_value = 0;  
        int i=0;
        char cmd[4];

        XGpio_Initialize(&output, XPAR_AXI_GPIO_1_DEVICE_ID);
        XGpio_SetDataDirection(&output, 1, 0x0);

        XGpio_Initialize(&input, XPAR_AXI_GPIO_0_DEVICE_ID);
        XGpio_SetDataDirection(&input, 1, 0xF);
       
        while(1) {
            i++;
            printf("This the %d time run the while loop\n\r",i);
            printf("Enter Command: \n\r");
            gets(cmd);

            switch_data = XGpio_DiscreteRead(&input, 1);
            printf("switch_data = %d\n\r",switch_data);

            led_value = switch_data+1;

            XGpio_DiscreteWrite(&output, 1, led_value);
            printf("data wrtie over");
            printf("==========================================");

            usleep(20000);
         }
   return 0;

}

image.png

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

Hello @billgas,

I have created my own block design and I have tested your code. For me it works fine. Every time I enter a new character into my serial console the loop gets reiterated  and the "switch_data" + 1 variable gets passed to the led's.  When you said that it gets stuck at gets(), it's probably because this function keeps on reading until it sees a newline character. So, if you are not entering a newline character the while(1) loop simply stops at gets(). Using gets() it's not the best idea because it's suffer from buffer overflow. You can use fgets() instead, or you can create your own commands enumeration. Something like this  typedef enum {command_a, command_b....}  my_commands;

Best Regards,

Bogdan Vanca 

Link to comment
Share on other sites

Hello @billgas,

I used termite not tera-term. The transmitted text, in my case was converted into Append LF. This was the default format for serial communication on termite, and I personally didn't encountered any problems with it. This was also the format used by me for other several projects that involved serial own commands communication. I advise you to use the same format. I look forward from hearing from you.

 

PS : You have reported by mistake my previous message :). 

Cheers,

Bogdan Vanca

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...