0
votes

I use GSM module Telit GL865 in UART communication with STM32F7 MCU and softwares STM32CubeMX, Keil.

When I send AT commands over UART to GSM module as below, I get an IP address by the internal stack of the GSM module and ping it successfully:

AT
AT&K0
AT#SCFG=3,1,300,600,300,10
AT+CGDCONT=1,"IP","mgbs","0.0.0.0",0,0
AT+CGSN
AT#ICMP=2
AT#GPPPCFG="000.000.000.000",25,1
AT#GPPPCFGEXT=0
ATDT*99#
CONNECT

However, when I start a PPPoS session, I cannot ping the IP which was successfully established over PPP.

The IP, gateway and netmask addresses that are established over PPP stack are as follows:

IP      = 5.26.61.173
Gateway = 192.168.202.0
Netmask = 255.255.255.255

The first problematic thing I viewed is the Netmask address; however, LwIP PPPoS establishes this.

Secondly, after the establishment above, MCU and GSM module starts to send periodic data to each other as "~y " with length 49 and 53 respectively.

That means my debugging of printf( ) cannot show me all of the periodic data.

Thirdly, the most important, when I ping the IP of GSM above, the periodic data "~y " with length 68, is sent much more in time between MCU and GSM, means that pinging triggers the communication between GSM and MCU but no response back.

I use interrupt based UART transceiving which starts to fill the received buffer and put it into pppos_input_tcpip(ppp, recBuffer, recIndex) unless new data comes in 10ms.

The main thread, and the timer interrupt is as follows:

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "cmsis_os.h"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include "string.h"
#include "main.h"
#include "stm32f7xx_hal.h"
#include "cmsis_os.h"
#include "lwip.h"
#include "usart.h"
#include "gpio.h"
#include <stdio.h>
#include "string.h"
#include <time.h>
#include "lwip/opt.h"
#include "lwip/sys.h"
#include "lwip/timeouts.h"
#include "lwip/debug.h"
#include "lwip/stats.h"
#include "lwip/init.h"
#include "lwip/tcpip.h"
#include "lwip/netif.h"
#include "lwip/api.h"
#include "lwip/tcp.h"
#include "lwip/udp.h"
#include "lwip/dns.h"
#include "lwip/dhcp.h"
#include "lwip/autoip.h"
#include "lwip/etharp.h"
#include "netif/ethernet.h"
#include "lwip/apps/netbiosns.h"
#include "lwip/apps/httpd.h"
#include "lwip/sio.h"
//#include "ppp/ppp.h"
//#include "netif/ppp/pppapi.h"
//#include "netif/ppp/pppos.h"
#include "pppapi.h"
#include <lwip/sockets.h> 
#include "usart.h"
#include "test.h"

#define RX_SIZE 4096
/* USER CODE END Includes */

/* Variables -----------------------------------------------------------------*/
osThreadId defaultTaskHandle;

/* USER CODE BEGIN Variables */
uint8_t lcpBuffer[RX_SIZE];
uint8_t lcpBufferIndex, quantityOfPackages = 0;
uint8_t pppBuffer[4096];
uint32_t sysTickCounter = 0;
uint8_t sendData[50] = "                    \n";
uint8_t recData, recDataOlder, recDataOlder2,recDataOlder3,recDataOlder4,recDataOlder5,recDataOlder6;
uint8_t receivedValue,connected,called = 0;
uint32_t recIndex = 0;
uint8_t recBuffer[RX_SIZE];
uint8_t sent, send, connect, connectionStatus, endOfLcpNegotiation, NCPNegotiation = 0;
ppp_pcb *ppp;
struct netif ppp_netif;
u8_t sio_idx = 0;
sio_fd_t ppp_sio;
uint8_t ATconnected,lcpCame,length = 0;
uint8_t lcpStartIndex, lcpStopIndex, lcpStartOld, lcpStart, lcpStop, dataReceived, timeOut, fillThePPPBuffer, gsmInitEnd, sendLCP, sendNCP, socketOpened, socketBind, socketOpen = 0;
uint32_t pppBufferIndex = 0;
ip4_addr_t addr;
int socketStatus;
int socket_fd,accept_fd;
int addr_size,sent_data; char data_buffer[80]; 
struct sockaddr_in sa,ra,isa;
uint32_t runTimeCnt = 0;

void StartDefaultTask(void const * argument);

extern void MX_LWIP_Init(void);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

//PPP function
static void status_cb(ppp_pcb *pcb, int err_code, void *ctx) {
    struct netif *pppif = ppp_netif(pcb);
    called = 1;

    switch(err_code) {
        case PPPERR_NONE: {
            connectionStatus = 1;
            printf("status_cb: Connected\n");
            printf("   our_ipaddr  = %s\r\n", ipaddr_ntoa(&pppif->ip_addr));
            printf("   gatewayaddr = %s\r\n", ipaddr_ntoa(&pppif->gw));
            printf("   netmask     = %s\r\n", ipaddr_ntoa(&pppif->netmask));
            break;
        }
        case PPPERR_PARAM: {
            connectionStatus = 2;
            printf("status_cb: Invalid parameter\r\n");
            break;
        }
        case PPPERR_OPEN: {
            connectionStatus = 3;
            printf("status_cb: Unable to open PPP session\r\n");
            break;
        }
        case PPPERR_DEVICE: {
            connectionStatus = 4;
            printf("status_cb: Invalid I/O device for PPP\r\n");
            break;
        }
        case PPPERR_ALLOC: {
            connectionStatus = 5;
            printf("status_cb: Unable to allocate resources\r\n");
            break;
        }
        case PPPERR_USER: {
            connectionStatus = 6;
            ppp_free(pcb);
            printf("status_cb: User interrupt\r\n");
            break;
        }
        case PPPERR_CONNECT: {
            connectionStatus = 7;
            printf("status_cb: Connection lost\r\n");
            break;
        }
        case PPPERR_AUTHFAIL: {
            connectionStatus = 8;
            printf("status_cb: Failed authentication challenge\r\n");
            break;
        }
        case PPPERR_PROTOCOL: {
            connectionStatus = 9;
            printf("status_cb: Failed to meet protocol\r\n");
            break;
        }
        case PPPERR_PEERDEAD: {
            connectionStatus = 10;
            printf("status_cb: Connection timeout\r\n");
            break;
        }
        case PPPERR_IDLETIMEOUT: {
            connectionStatus = 11;
            printf("status_cb: Idle Timeout\r\n");
            break;
        }
        case PPPERR_CONNECTTIME: {
            connectionStatus = 12;
            printf("status_cb: Max connect time reached\r\n");
            break;
        }
        case PPPERR_LOOPBACK: {
            connectionStatus = 13;
            printf("status_cb: Loopback detected\r\n");
            break;
        }
        default: {
            connectionStatus = 14;
            printf("status_cb: Unknown error code %d\r\n", err_code);
            break;
        }
    }
}
//PPP function
static u32_t output_cb(ppp_pcb *pcb, u8_t *data, u32_t len, void *ctx){
    printf("sent:     %s with length: %d\r\n",data,len);
    return HAL_UART_Transmit_IT(&huart1, data, len);
}

/* Init FreeRTOS */
void MX_FREERTOS_Init(void) {
  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 1024);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
}

/* StartDefaultTask function */
void StartDefaultTask(void const * argument)
{
  /* init code for LWIP */
  MX_LWIP_Init();

  /* USER CODE BEGIN StartDefaultTask */
    //SysTick_Config(SystemCoreClock/1000); 
    HAL_UART_Receive_IT(&huart1, &recData, 1);

    // Start of AT commands

    HAL_Delay(1000);    
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT\r\n", strlen("AT\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT&K0\r\n", strlen("AT&K0\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SCFG=3,1,300,600,300,10\r\n", strlen("AT#SCFG=3,1,300,600,300,10\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SLED=2,2,2\r\n", strlen("AT#SLED=2,2,2\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SLEDSAV\r\n", strlen("AT#SLEDSAV\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGDCONT=1,\"IP\",\"mgbs\",\"0.0.0.0\",0,0\r\n", strlen("AT+CGDCONT=1,\"IP\",\"mgbs\",\"0.0.0.0\",0,0\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CGSN\r\n", strlen("AT+CGSN\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#ICMP=2\r\n", strlen("AT#ICMP=2\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CSQ\r\n", strlen("AT+CSQ\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT+CCLK?\r\n", strlen("AT+CCLK?\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT?\r\n", strlen("AT#SGACT?\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT=1,0\r\n", strlen("AT#SGACT=1,0\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT=1,1\r\n", strlen("AT#SGACT=1,1\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#SGACT?\r\n", strlen("AT#SGACT?\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#FRWL=1,\"10.0.1.1\",\"255.255.0.0\"\r\n", strlen("AT#FRWL=1,\"10.0.1.1\",\"255.255.0.0\"\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFG=\"000.000.000.000\",25,1\r\n", strlen("AT#GPPPCFG=\"000.000.000.000\",25,1\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GPPPCFGEXT=0\r\n", strlen("AT#GPPPCFGEXT=0\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"AT#GAUTH=0\r\n", strlen("AT#GAUTH=0\r\n"));
    HAL_Delay(1000);
    memset(recBuffer, 0, sizeof(recBuffer));
    recIndex = 0;
    HAL_UART_Transmit_IT(&huart1, (uint8_t*)"ATDT*99***1#\r\n", strlen("ATDT*99***1#\r\n"));
    //End of AT commands

    //Start PPP
    ppp = pppapi_pppos_create(&ppp_netif, output_cb, status_cb, NULL);
    if (ppp == NULL) {
        printf("Error init pppos");
        return;
    }
    pppapi_set_default(ppp);
    ppp_set_auth(ppp, PPPAUTHTYPE_NONE, "", "");
    pppapi_listen(ppp);


    for(;;)
    {
        //if GSM is initialized, give data to ppp if stops coming after 10ms, 
        //send flag is set by systick(10ms) and gsmInitEnd is set after LCP session starts

        if(gsmInitEnd){
            if(send){
                send = 0;
                pppos_input_tcpip(ppp, recBuffer, recIndex);
                printf("received: %s with length: %d\r\n", recBuffer, recIndex);
                recIndex = 0;
                runTimeCnt++;
                memset(recBuffer, 0, sizeof(recBuffer));
            }
        }
    }
}

// UART rx callback, called over 1 data received
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
    if (UartHandle->Instance == USART1)
    {
        //callback reset
        HAL_UART_Receive_IT(&huart1, &recData, 1);

        //receive buffer overflow check
        if(recIndex >= RX_SIZE){
            recIndex = 0;
        }   

        //3 variables below are externed to send data if stops coming to
        //buffer after 10ms with Systick interrupt later than GSM initialization
        dataReceived = 1;    
        sysTickCounter = 0;
        send = 0;

        //LCP packets start with 0x7E, that means the end of GSM initialization.
        if(recData == 0x7E) 
        {
            if(gsmInitEnd == 0){
                recIndex = 0;
                memset(recBuffer, 0, sizeof(recBuffer));
            }
            gsmInitEnd = 1;
        }
        recBuffer[recIndex++] = recData;
    }
}

Systick Handler that checks the RX buffer and sets the send flag to send the data to ppp if data stops coming after 10ms:

void SysTick_Handler(void)
{
    if(dataReceived){
        sysTickCounter++;
        if (sysTickCounter >= 10)
        {
            send = 1;
            dataReceived = 0;
            sysTickCounter = 0;
        }
    }
  osSystickHandler();
}

Data communication between GSM and STM32 seems OK to me(!) and triggered with ping request; however, no ping response..

In my opinion, problem may be about the connection between ppp_netif and ppp_netif(ppp), netmask or ping implementation of the LwIP stack...

2

2 Answers

0
votes

The answer is activating outgoing and incoming ICMP checksum options of STM32CubeMX...So, activating LWIP_ICMP module is insufficient by itself to response ping requests..

0
votes

If you enable the software checksums for PPPOS, Ethernet breaks because STM32 is doing them in HW on Ethernet - GRRR