1

I am just trying to toggle on Board LED and listen to incoming Data from the terminal window at the same time, but the whole Loop function hangs when it reachs the fgets(), until I send something the the terminal window then it continues to loop. There must be something like "if Serial available , then start to read ?? Here is my Code, I'm new in Atmel studio

 #include <asf.h>
    #include <string.h>
   

 char incomingData [256];  

   
    // /**
    //  *  \brief Configure UART for debug message output.
    //  */
    static void configure_console(void)
    {
        const usart_serial_options_t uart_serial_options = {
            .baudrate = CONF_UART_BAUDRATE,
            #ifdef CONF_UART_CHAR_LENGTH
            .charlength = CONF_UART_CHAR_LENGTH,
            #endif
            .paritytype = CONF_UART_PARITY,
            #ifdef CONF_UART_STOP_BITS
            .stopbits = CONF_UART_STOP_BITS,
            #endif
        };
    
        //  /* Configure console UART. */
        sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
        stdio_serial_init(CONF_UART, &uart_serial_options);
    }
    int main (void)
 {  
        /* Insert system clock initialization code here (sysclk_init()). */
    
        board_init();
        sysclk_init();
        configure_console();
        /* Insert application code here, after the board has been initialized. */
         
    gpio_set_pin_low(LED0_GPIO);
    while (1)
       
    {
        printf("Hello\r\n");
        gpio_toggle_pin(LED0_GPIO);
        fgets(incomingData,sizeof(incomingData),stdin);
        printf("%s\r\n",incomingData);
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
a.raad
  • 21
  • 4
  • https://stackoverflow.com/questions/45938222 – Robert Harvey Jun 10 '21 at 11:47
  • Hi Robert, sorry but this was not helpful, i tried while(fgets(incomingData,sizeof(incomingData),stdin)!=NULL) , still got stucked – a.raad Jun 10 '21 at 12:32
  • If you are waiting for inconstant input rather use an interrupt routine and keep the LED toggling in the infinite loop. Or check if the `stdin` is [empty](https://stackoverflow.com/questions/26948723/checking-the-stdin-buffer-if-its-empty). – Olupo Jun 10 '21 at 12:35
  • @a.raad: It sounds like you either didn't read the post I linked, or you didn't understand it. – Robert Harvey Jun 10 '21 at 12:49
  • What is `incomingData`? Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT Jun 10 '21 at 12:59
  • when i check the stdin, it is never empty, doenst matter if i send something through the terminal or not. @RobertHarvey i read it but it seems i didnt understand it, can u edit my example so i would understand easier ? – a.raad Jun 10 '21 at 12:59
  • Well, maybe it's time to revisit your assumptions. There is [plenty of prior art](https://www.google.com/search?q=atmel+read+from+serial+port) on reading from a serial port using an Atmel board; I suggest you start there. – Robert Harvey Jun 10 '21 at 13:01
  • @RobertHarvey do you think i didnt try to search ? or i didnt spend time to solve the Problem ? if you dont want to help ,then u dont need to comment anything , you could have showed by now how to fix it, it is only a couple lines but U NEED to make yourself important – a.raad Jun 10 '21 at 13:05
  • No, I think you're using the wrong approach. Carefully study one of the articles that explains how to do serial I/O with Atmel, and follow that approach. – Robert Harvey Jun 10 '21 at 13:06
  • The stdio.h functions aren't suitable for concurrent programming. If you need several things happening "at once" (or appearing to), you need to use lower level functions. You can just dig your way down into the ASF lib and grab or rewrite parts from the UART driver. Be aware that this lib is very badly written, so digging through its internals isn't easy. Might be easier to roll out your own UART driver from scratch by reading the manual. – Lundin Jun 10 '21 at 13:46
  • Alternatively in this specific you could just update the LED from a cyclic timer interrupt. – Lundin Jun 10 '21 at 13:47

0 Answers0