0

It is sort of trivial issue, but it is still taking my tame for the last several days. I am trying to get the user input and wait until the user enters the value, but my program doesn't do it. I would be grateful, if someone could help me out please.

The idea is that the program should print the message that the user should enter the value with keyboard and wait until the keyboard input is entered. And according to the input, certain port pins data register should be set high in order to call port interrupt handler in the end.

/**
* main.c
*/
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "tm4c1294ncpdt.h"
#include "int_handler.h"
#include "inc/hw_memmap.h"

#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/rom_map.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"

//states[3]=[0,0,0]; //S0,S1,Error
/**
 * main.c
 * 2 out 3,3V
 * 2 in 3,3V
 */
int main(void)
{
    // Port Clock Gating Control Port A
    SYSCTL_RCGCGPIO_R |= 0x00000001;
    while((SYSCTL_PRGPIO_R & 0x00000001) == 0);
    // Set INPUT direction I/O pin PA6
    GPIO_PORTA_AHB_DIR_R &= 0xBF;
    // Digital I/O pins PA6 enable
    GPIO_PORTA_AHB_DEN_R |= 0x40;
    GPIO_PORTA_AHB_DATA_R &= 0x00;
    //GPIO_PORTA_AHB_AFSEL_R &= ~0x40; // disable alt funct on PA6 (default setting)

    //GPIO_PORTA_AHB_PCTL_R &= ~0x0F000000;// configure PA6 as GPIO (default setting)
    //GPIO_PORTA_AHB_AMSEL_R &= ~0x40;// disable analog functionality on PA6 (default setting)
    GPIO_PORTA_AHB_IS_R &= ~0x40;   // PA6 is edge-sensitive (default setting)
    GPIO_PORTA_AHB_IBE_R &= ~0x40;  // PA6 is not both edges (default setting)
    GPIO_PORTA_AHB_IEV_R |= 0x40;   // PA6 rising edge event
    GPIO_PORTA_AHB_ICR_R = 0x40;    // clear flag6
    GPIO_PORTA_AHB_IM_R |= 0x40;    // enable interrupt on PA6
                                // GPIO PortC=priority 2
    NVIC_EN0_R |= (1<<0); // enable PortA interrupt (Int#0/Vec#16) in NVIC


    // Port Clock Gating Control Port B
    SYSCTL_RCGCGPIO_R |= 0x00000002;
    while((SYSCTL_PRGPIO_R & 0x00000002) == 0);
    // Set output direction I/O pins PB0 to PB7
    GPIO_PORTB_AHB_DIR_R |= 0xFF;
    // Digital I/O pins PB2 to PB3 enable
    GPIO_PORTB_AHB_DEN_R |= 0x0C;

    // Port Clock Gating Control Port C
    SYSCTL_RCGCGPIO_R |= 0x00000004;
    while((SYSCTL_PRGPIO_R & 0x00000004) == 0);
    // Set output direction I/O pins PC0 to PC7
    GPIO_PORTC_AHB_DIR_R |= 0xFF;
    // Digital I/O pins PC4 to PC7 enable
    GPIO_PORTC_AHB_DEN_R |= 0xF0;


    // Port Clock Gating Control Port D
    SYSCTL_RCGCGPIO_R |= 0x00000008;
    while((SYSCTL_PRGPIO_R & 0x00000008) == 0);
    // Set output direction I/O pins PD3
    GPIO_PORTD_AHB_DIR_R |= 0x0F;
    GPIO_PORTD_AHB_DIR_R &= 0x0F;     // Set INPUT direction I/O pins PD7
    // Digital I/O pins PD3 AND PD7 enable
    GPIO_PORTD_AHB_DEN_R |= 0x88;

    GPIO_PORTD_AHB_IS_R &= ~0x80;   // PD7 is edge-sensitive (default setting)
    GPIO_PORTD_AHB_IBE_R &= ~0x80;  // PD7 is not both edges (default setting)
    GPIO_PORTD_AHB_IEV_R |= 0x80;   // PD7 rising edge event
    GPIO_PORTD_AHB_ICR_R = 0x80;    // clear flag6
    GPIO_PORTD_AHB_IM_R |= 0x80;    // enable interrupt on PA6

    NVIC_EN0_R |= (1<<3); // enable PortD interrupt (Int#3/Vec#19) in NVIC



    // Port Clock Gating Control Port E
    SYSCTL_RCGCGPIO_R |= (1 << 4);
    while((SYSCTL_PRGPIO_R & (1 << 4)) == 0);
    // Set INPUT direction I/O pins PE0 to PE3
    GPIO_PORTE_AHB_DIR_R &= 0xF0;
    GPIO_PORTE_AHB_DIR_R |= 0xF0;// Set OUTPUT direction I/O pins PE5
    // Digital I/O pins PE0 to PE3 AND PE5 enable
    GPIO_PORTE_AHB_DEN_R |= 0x2F;

    GPIO_PORTE_AHB_IS_R &= ~0x0F;   // PE0 to PE3 is edge-sensitive (default setting)
    GPIO_PORTE_AHB_IBE_R &= ~0x0F;  // PA6 is not both edges (default setting)
    GPIO_PORTE_AHB_IEV_R |= 0x0F;   // PA6 rising edge event
    GPIO_PORTE_AHB_ICR_R = 0x0F;    // clear flag6
    GPIO_PORTE_AHB_IM_R |= 0x0F;    // enable interrupt on PA6
                                // GPIO PortC=priority 2
    NVIC_EN0_R |= (1<<4); // enable PortE interrupt (Int#4/Vec#20) in NVIC


    // Port Clock Gating Control Port M
    SYSCTL_RCGCGPIO_R |= (1 << 11);
    while((SYSCTL_PRGPIO_R & (1 << 11)) == 0);
    // Set input direction I/O pins PM4 and PM5
    GPIO_PORTM_DIR_R &= ~(0x30);
    // Digital I/O pins PM4 and PM5 enable
    GPIO_PORTM_DEN_R |= 0x30;

    GPIO_PORTM_IS_R &= ~0x30;   // PE0 to PE3 is edge-sensitive (default setting)
    GPIO_PORTM_IBE_R &= ~0x30;  // PA6 is not both edges (default setting)
    GPIO_PORTM_IEV_R |= 0x30;   // PA6 rising edge event
    GPIO_PORTM_ICR_R = 0x30;    // clear flag6
    GPIO_PORTM_IM_R |= 0x30;    // enable interrupt on PA6
                                  // GPIO PortC=priority 2
    NVIC_EN2_R |= (1<<8); // enable PortM interrupt (Int#72/Vec#88) in NVIC

    while(1){
        //DEMO
        //Placing instructions with keyboard
        int i;
        char name='m';
        //fflush(stdout);
        printf("Please enter l or r");
        fflush(stdout);


        //gets(name);
        //scanf(" %c", &name);
        //fgets(name,5,stdin);
        //getchar();

        while(name != 'l' && name != 'r'){
            scanf("%c", &name);
        }

        printf("U entered %c", name);
        fflush(stdout);
        if(name=='l')//PE0 is high
        {
            GPIO_PORTE_AHB_DATA_R |= 0x01; // PE0 high
            for(i=0;i<500000;i++);
            GPIO_PORTE_AHB_DATA_R &= 0x00; // PE0 LOW
        }
        else if(name=='r'){
            GPIO_PORTE_AHB_DATA_R |= 0x02; // PE1 high
            for(i=0;i<500000;i++);
            GPIO_PORTE_AHB_DATA_R &= 0x00; // PE1 LOW
        }
        else{
            printf("Pls enter l or r");
            fflush (stdout);

        }

    }
}
Sean
  • 59
  • 7
  • So which microcontroller are you targeting? How does the user "enter the value"? How is the keyboard connected? What keyboard? – KamilCuk Dec 22 '19 at 13:47
  • @KamilCuk I am using tm4c1294ncpdt mC from TI, user enters the value using normal computer keyboard, which comes with the laptop. – Sean Dec 22 '19 at 14:31
  • @user3121023 thanks for the comment, the problem is that after I enter the value with keyboard and hit enter, nothing different is happening, it is still expecting the value. – Sean Dec 22 '19 at 14:38
  • Are you optimizing? Is this line supposed to replace `sleep`? `for(i=0;i<500000;i++);` – Dinu Dec 22 '19 at 14:40
  • Is the keyboard connected to tm4c1294ncpdt? – KamilCuk Dec 22 '19 at 14:41
  • @Dinu Yes, it sets the certain pin high for certain period and afterwards sets it low – Sean Dec 22 '19 at 14:42
  • @KamilCuk no, basically, my setup is very basic, as follows, I have my laptop, which has monitor, keyboard, trackpad and I have mC which is connected to laptop via USB cable. That's all. – Sean Dec 22 '19 at 14:43
  • @Sean does any of the libraries you use provide a `sleep` function? – Dinu Dec 22 '19 at 14:45
  • So the key press on your keybord does not "travel" to your microcontroller. The keybord is not connected to your microcontroller! Why would you think it would work? – KamilCuk Dec 22 '19 at 14:47
  • @Dinu, maybe, but I didn't search for sleep function, because for loop is doing fine in my opinion or you think it is not good idea? – Sean Dec 22 '19 at 14:48
  • @KamilCuk if I had any problem with the connection to mC, I would see it, but everything is fine with the connections – Sean Dec 22 '19 at 14:50
  • Is there no way to use a debugger on a microcontroller? – Dinu Dec 22 '19 at 14:51
  • @Dinu I am using debugger, but when it comes to while(name != 'l' && name != 'r') the debugger's step function becomes not available. I suppose the problem is with scanf function, but there are no error messages displayed – Sean Dec 22 '19 at 14:55
  • How about adding `printf("a");` and `printf("b");` before and after the `scanf();`. What is the result? – Dinu Dec 22 '19 at 15:10
  • And can you use `strace` or something similar to find out what `syscall`s your mC performs? – Dinu Dec 22 '19 at 15:14
  • @Dinu I placed the print statement right after the scanf and it didn't get printed, which means that scanf is breaking. During debug as well, because after scanf line the step function of debugger disappears. – Sean Dec 22 '19 at 18:20
  • Is there any way you can monitor `syscall`s ? It might give you a hint to what is actually happening, and even what is it waiting for. – Dinu Dec 22 '19 at 19:11
  • @Dinu No, as I found out only debugger is available to track the calls – Sean Dec 22 '19 at 19:35

1 Answers1

0

Since name is a char, you shouldn't compare with strcmp.

Use

while(name != 'l' && name != 'r'){
    scanf("%c", &name);
}
//name cannot have values other than 'l' and 'r' here

//...

if(name == 'l'){
//...
}else{
//...
}
Dinu
  • 129
  • 2
  • 6
  • thanks for the comment, the problem is that after I enter the value with keyboard and hit enter, nothing different is happening, it is still expecting the value – Sean Dec 22 '19 at 14:40
  • What is the output you get from this? `printf("U entered %c", name);` – Dinu Dec 22 '19 at 14:43
  • Nothing, in my console, I don't see this message – Sean Dec 22 '19 at 14:45
  • Do you see the prompt? – Dinu Dec 22 '19 at 14:46
  • This is what I see in my console after I enter the value and hit enter: Please enter l or rl – Sean Dec 22 '19 at 14:46
  • I think it may get SIGSEGV on this line: `printf("U entered %s", name);`. `%s` expects a pointer. Use `%c` here too – Dinu Dec 22 '19 at 14:48
  • yes, I have already fixed it, because compiler was giving a warning, but again it didn't solve the problem that the compiler is still expecting the value – Sean Dec 22 '19 at 14:52
  • Can you update your question with the code you currently have? – Dinu Dec 22 '19 at 14:55