0

I am trying to connect my TM4C123GH6PM Microcontroller from Texas Instruments with my Smartphone and use it to control an alarm clock and LED Lights. (the LEDs are controlled over a Transistor, which is controlled over an GPIO Pin). I have some experience with coding in C++ and the TM4C123GH6PM, but I am still learning a lot. So please excuse some foolish mistakes I might have made.

I want to connect the ESP8266 with the Microcontroller using UART and the TivaWare Framework. I have written some code and my UART works correctly (I tested it by sending chars from UART 4 to 3).

According to the AT commands of ESP8266 It should respond to "AT" with "OK". But whenever I send something to the ESP it responds with exactly what I sent to it. I checked the wiring, and that's not The Issue. Or at least I think so. Please correct me, if the wiring is wrong.

ESP -> TM4C123GH6PM:
GND -> GND
VCC -> 3.3V
Tx -> Rx (UART3 / PC6)
Rx -> Tx (UART4 / PC5)
CH_PD -> 3.3V

I also checked for the power consumption of the ESP. Everything is powered by the USB-port of my laptop, since that helps keep the cable mess down. I monitor the power consumption with (https://www.amazon.de/gp/product/B07C8CM5TG/ref=ppx_yo_dt_b_asin_title_o08_s00?ie=UTF8&psc=1). The ESP is drawing about 150mA from the computer, but the port can provide a lot more. I checked with some LEDs and 400mA is not a problem.

Can anyone help me? I am working on this now for over two days and can't find a Solution. What is the Problem with the ESP not responding correctly to the AT command? The blue light is one, when the code is running.

PS: The attached code contains also code for the alarm clock control and LEDs. I attached it, since it could be part of the problem, but some of it is commented out and most of it is not used.

#include<stdint.h>

#include<stdbool.h>

#include"inc/hw_ints.h"

#include"inc/hw_memmap.h"

#include"inc/hw_types.h"

#include"driverlib/gpio.h"

#include"driverlib/sysctl.h"

#include"driverlib/timer.h"

#include"driverlib/interrupt.h"

#include"driverlib/uart.h"

#include"driverlib/pin_map.h"

#include "driverlib/rom.h"

// stores the time since system start in ms
uint32_t systemTime_ms;

//bools or controling the alarm clock and LEDS
bool an_aus = false;
bool alarm_clock = false;

void InterruptHandlerTimer0A (void)
{
    // Clear the timer interrupt flag to avoid calling it up again directly
    TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    // increase the ms counter by 1 ms
    systemTime_ms++;
}

void clockSetup(void)
{
    uint32_t timerPeriod;
    //configure clock
    SysCtlClockSet(SYSCTL_SYSDIV_5|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ| SYSCTL_OSC_MAIN);
    //activate peripherals for the timer
    SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
    // configure timers as 32 bit timers in periodic mode
    TimerConfigure(TIMER0_BASE, TIMER_CFG_PERIODIC);
    // set the variable timerPeriod to the number of periods to generate a timeout every ms
    timerPeriod = (SysCtlClockGet()/1000);
    // pass the variable timerPeriod to the TIMER-0-A
    TimerLoadSet(TIMER0_BASE, TIMER_A, timerPeriod-1);
    // register the InterruptHandlerTimer0A function as an interrupt service routine
    TimerIntRegister(TIMER0_BASE, TIMER_A, &(InterruptHandlerTimer0A));
    // activate the interrupt on TIMER-0-A
    IntEnable(INT_TIMER0A);
    // generate an interrupt when TIMER-0-A generates a timeout
    TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
    // all interrupts are activated
    IntMasterEnable();
    // start the timer
    TimerEnable(TIMER0_BASE, TIMER_A);
}

void UART (void)
{
       //configure UART 4:
       SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC);
       SysCtlPeripheralEnable(SYSCTL_PERIPH_UART4);
       while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UART4));

       //GPIO pins for transmitting and receiving
       GPIOPinConfigure(GPIO_PC4_U4RX);
       GPIOPinConfigure(GPIO_PC5_U4TX);
       GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_4 | GPIO_PIN_5);

       //configure UART 8Bit, no parity, baudrat 38400
       UARTConfigSetExpClk(UART4_BASE, SysCtlClockGet(), 38400, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));

       //configure UART 3:
       SysCtlPeripheralEnable(SYSCTL_PERIPH_UART3);
       while(!SysCtlPeripheralReady(SYSCTL_PERIPH_UART3));
       GPIOPinConfigure(GPIO_PC6_U3RX);
       GPIOPinConfigure(GPIO_PC7_U3TX);
       GPIOPinTypeUART(GPIO_PORTC_BASE, GPIO_PIN_6 | GPIO_PIN_7);
       UARTConfigSetExpClk(UART3_BASE, SysCtlClockGet(), 38400, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE));
}

void delay_ms(uint32_t waitTime)
{
    // Saves the current system time in ms
    uint32_t aktuell = systemTime_ms;
    // Wait until the current system time corresponds to the sum of the time at the start of the delay and the waiting time
    while(aktuell + waitTime > systemTime_ms);
}

void ex_int_handler(void)
{
    // press the button to start timer for alarm clock
    alarm_clock = true;
    GPIOIntClear(GPIO_PORTF_BASE,GPIO_PIN_4);
}

int main(void)
{
    //Peripherals for LED and GPIO
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //UART
    UART();

    //Timer
    clockSetup();

    // button
    GPIOPinTypeGPIOInput(GPIO_PORTF_BASE,GPIO_PIN_4);
    GPIOPadConfigSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_STRENGTH_2MA,GPIO_PIN_TYPE_STD_WPU);

    //OnboardLED
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_1);
    GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE,GPIO_PIN_3);

    //Interrupt Timer
    GPIOIntDisable(GPIO_PORTF_BASE,GPIO_PIN_4);
    GPIOIntClear(GPIO_PORTF_BASE,GPIO_PIN_4);
    GPIOIntTypeSet(GPIO_PORTF_BASE,GPIO_PIN_4,GPIO_FALLING_EDGE);
    GPIOIntRegister(GPIO_PORTF_BASE,ex_int_handler);
    GPIOIntEnable(GPIO_PORTF_BASE,GPIO_PIN_4);

    //Transistor Gate
    GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_0);
    //GPIOPadConfigSet(GPIO_PORTB_BASE,GPIO_PIN_0,GPIO_STRENGTH_6MA,GPIO_PIN_TYPE_STD_WPU);

    //debugging only: save all the received data from the ESP in an array to look at while debugging
    int32_t data[20] = {0};
    int32_t j = 0;

    //Code for debugging the UART and ESP8266
while(1){

    //Checks for Data in the FIFO
    while(!UARTCharsAvail(UART4_BASE));

    //send AT-command to ESP8266
    UARTCharPut(UART4_BASE, 'A');
    while(UARTBusy(UART4_BASE));
    UARTCharPut(UART4_BASE, 'T');
    while(UARTBusy(UART4_BASE));

    if(UARTCharsAvail(UART3_BASE))
    {
        while(UARTCharsAvail(UART3_BASE))
        {
            //Read data from the FIFO in UART3 -> received from ESP8266
            data[j] = UARTCharGet(UART3_BASE);
            j++;
        }
    }

    //clear array when its full
    if (j >= 20)
    {
        j = 0;
        for(int32_t a = 0; a <21; a++)
        {
            data[a] = 0;
        }
    }
}

//code to run the alarm clock and leds
    /*
    while(1)
    {
        if (alarm_clock)
        {
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_3,GPIO_PIN_3);
            //Wait
            delay_ms(30600000);
            GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0,GPIO_PIN_0);
            alarm_clock = false;
            GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_3,0x00);
            //Start Red LED blinking when it is finished
             while(1)
            {
                GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,GPIO_PIN_1);
                delay_ms(1000);
                GPIOPinWrite(GPIO_PORTF_BASE,GPIO_PIN_1,0x00);
                delay_ms(1000);

            }
        }
    }
    */
}
CL4PTP
  • 3
  • 2
  • Have you flashed the ESP8266 properly with the AT Firmware? – PatrickOfThings Apr 07 '20 at 17:13
  • you must terminate the at commands with \r\n – Juraj Apr 07 '20 at 17:16
  • @PatrickOfThings I had trouble flashing the ESP8266 with my TM4C123GH6PM and I have no FTDI adapter available. I ordered one but it could take a while until it arrives. Shouldn't the stock Firmware support AT-Commands? According to the vendor it was shipped with Firmware Version 2.0. – CL4PTP Apr 07 '20 at 18:25
  • @Juraj But how should i send this? When I use UARTCharPut(UART4_BASE, '\r\n'); It responds with AT202. Or 65 84 202 to be exact. And sending the chars on at a time is not possible. Code Composer Studio picks '\' up as some kind of comment. – CL4PTP Apr 07 '20 at 18:34
  • Look through the available UART functions. You should have the abillity to place a whole string into the UART and send it. Try sending "AT\r\n", "AT", and "AT\n" and see if any get a different response. Putting one character into your UART send is not going to work well. – PatrickOfThings Apr 07 '20 at 18:46
  • `UARTCharPut(UART4_BASE, '\r');` `\r` is a character – Juraj Apr 07 '20 at 18:47

1 Answers1

0

According to the AT commands of ESP8266 It should respond to "AT" with "OK". But whenever I send something to the ESP it responds with exactly what I sent to it

Modems with AT Commands commonly ship with the echo mode turned on, so that when you are interacting with it manually through serial port, it will echo the characters you sent first, and then send the reply.

So, when you are automating the process, you first send the characters, then wait for the reply until you reach a '\r'. Well, you are reaching a '\r', but its the one from the echo. You might have some other characters next. You send AT, you should receive AT first, then you have the OK.

To solve this problem, you should turn echo mode off. The command to turn off echo is ATE0.

lcarvao
  • 1
  • 1