2024-11-07 08:34 AM
Hi,
https://community.st.com/t5/stm32-mcus-products/configuring-ppp-on-stm32f407disc-with-lte-modem-via-usart-using/m-p/728673#M262741
This continues the previous thread discussing the PPPoS implementation for the STM32F407. I successfully established a PPP connection and obtained an IP address. However, my ICMP ping to Google did not receive a successful response. Please check the code snippet below for any necessary changes.
void PPPosClientThread()
{
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
pppos_task_started = 1;
osSemaphoreRelease(pppos_mutex);
int gsmCmdIter = 0;
int nfail = 0;
printf("GSM: Starting Modem thread\r\n");
char* data = (char*) malloc(BUF_SIZE);
char PPP_ApnATReq[sizeof(CONFIG_GSM_APN) + 24];
sprintf(PPP_ApnATReq, "AT+CGDCONT=1,\"IP\",\"%s\"\r\n", CONFIG_GSM_APN);
cmd_APN.cmd = PPP_ApnATReq;
cmd_APN.cmdSize = strlen(PPP_ApnATReq);
//_disconnect(1); // Disconnect if connected // FOR testing
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
pppos_tx_count = 0;
pppos_rx_count = 0;
gsm_status = GSM_STATE_FIRSTINIT;
osSemaphoreRelease(pppos_mutex);
// GSM reset pin functionality required
enableAllInitCmd();
while (1)
{
while (gsmCmdIter < GSM_InitCmdsSize)
{
if (GSM_Init[gsmCmdIter]->skip)
{
#if GSM_DEBUG
infoCommand(GSM_Init[gsmCmdIter]->cmd, GSM_Init[gsmCmdIter]->cmdSize, "Skip command:");
#endif
gsmCmdIter++;
continue;
}
if (atCmd_waitResponse(GSM_Init[gsmCmdIter]->cmd,
GSM_Init[gsmCmdIter]->cmdResponseOnOk, NULL,
GSM_Init[gsmCmdIter]->cmdSize,
GSM_Init[gsmCmdIter]->timeoutMs, NULL, 0) == 0)
{
// * No response or not as expected, start from first initialization command
#if GSM_DEBUG
printf("GSM: Wrong response, restarting...\r\n");
#endif
nfail++;
if (nfail > 20)
goto exit;
osDelay(3000);
gsmCmdIter = 0;
continue;
}
if (GSM_Init[gsmCmdIter]->delayMs > 0)
osDelay(GSM_Init[gsmCmdIter]->delayMs);
GSM_Init[gsmCmdIter]->skip = 1;
if (GSM_Init[gsmCmdIter] == &cmd_Reg)
GSM_Init[gsmCmdIter]->delayMs = 0;
// Next command
gsmCmdIter++;
}
#if GSM_DEBUG
printf("GSM: GSM initialized.\r\n");
#endif
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
if(gsm_status == GSM_STATE_FIRSTINIT)
{
osSemaphoreRelease(pppos_mutex);
// ** After first successful initialization create PPP control block
ppp = pppos_create(&ppp_netif, ppp_output_callback, ppp_status_cb, NULL);
if (ppp == NULL)
{
#if GSM_DEBUG
printf("GSM: Error initializing PPPoS\r\n");
#endif
break; // end task
}
}
else
osSemaphoreRelease(pppos_mutex);
//ppp_set_default(ppp);
netif_set_default(&ppp_netif);
//ppp_set_auth(ppp, PPPAUTHTYPE_ANY, "", "");
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
gsm_status = GSM_STATE_IDLE;
osSemaphoreRelease(pppos_mutex);
ppp_connect(ppp,0);
while(1)
{
// === Check if disconnect requested ===
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
if (do_pppos_connect <= 0)
{
int end_task = do_pppos_connect;
do_pppos_connect = 1;
osSemaphoreRelease(pppos_mutex);
#if GSM_DEBUG
printf("\r\n");
printf("GSM: Disconnect requested.\r\n");
#endif
ppp_close(ppp, 0);
int gstat = 1;
while (gsm_status != GSM_STATE_DISCONNECTED)
{
// Handle data received from GSM
memset(data, 0, BUF_SIZE);
int len = UART_Read(data, BUF_SIZE, 30);
if (len > 0)
{
pppos_input_tcpip(ppp, (u8_t*)data, len);
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
pppos_tx_count += len;
osSemaphoreRelease(pppos_mutex);
}
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
gstat = gsm_status;
osSemaphoreRelease(pppos_mutex);
}
osDelay(1000);
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
uint8_t rfoff = gsm_rfOff;
osSemaphoreRelease(pppos_mutex);
_disconnect(rfoff); // Disconnect GSM if still connected
#if GSM_DEBUG
printf("GSM: Disconnected.\r\n");
#endif
gsmCmdIter = 0;
enableAllInitCmd();
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
gsm_status = GSM_STATE_IDLE;
do_pppos_connect = 0;
osSemaphoreRelease(pppos_mutex);
if (end_task < 0) goto exit;
// === Wait for reconnect request ===
gstat = 0;
while (gstat == 0)
{
osDelay(100);
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
gstat = do_pppos_connect;
osSemaphoreRelease(pppos_mutex);
}
#if GSM_DEBUG
printf("\r\n");
printf("GSM: Reconnect requested.\r\n");
#endif
break;
}
// === Check if disconnected ===
if (gsm_status == GSM_STATE_DISCONNECTED)
{
osSemaphoreRelease(pppos_mutex);
#if GSM_DEBUG
printf("\r\n");
printf("GSM: Disconnected, trying again...\r\n");
#endif
ppp_close(ppp, 0);
_disconnect(1);
enableAllInitCmd();
gsmCmdIter = 0;
gsm_status = GSM_STATE_IDLE;
osDelay(10000);
break;
}
else
osSemaphoreRelease(pppos_mutex);
// === Handle data received from GSM ===
memset(data, 0, BUF_SIZE);
int len = UART_Read(data, BUF_SIZE, 50);
if (len > 0)
{
//printf("RX: %d\r\n", len);
//printf("data :%s\r\n",data);
pppos_input_tcpip(ppp, (u8_t*)data, len);
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
pppos_tx_count += len;
osSemaphoreRelease(pppos_mutex);
}
} // Handle GSM modem responses & disconnects loop
} // main task loop
exit:
if (data) free(data); // free data buffer
if (ppp) ppp_free(ppp);
osSemaphoreWait(pppos_mutex, PPPOSMUTEX_TIMEOUT);
pppos_task_started = 0;
gsm_status = GSM_STATE_FIRSTINIT;
osSemaphoreRelease(pppos_mutex);
#if GSM_DEBUG
printf("GSM: PPPoS TASK TERMINATED\r\n");
#endif
//osThreadTerminate(mythread);
}
Ping code snippet:
void start_ping_action() {
//get gateway IP from global net interface
ip_addr_t gw_addr = ppp_netif.ip_addr;
uint8_t gw_ip_part_1 = ip4_addr1(&gw_addr);
//check if DHCP already succeeded by checking against non 0 ip part
int ret = 0;
if(gw_ip_part_1 != 0) {
ip_addr_t target_ping_ip;
//select target:
//gateway
//target_ping_ip = gw_addr;
//static IPs
//IP_ADDR4(&target_ping_ip, 192,168,1,180);
IP_ADDR4(&target_ping_ip, 216,58,213,195); //google.com
//IP_ADDR4(&target_ping_ip, 8, 8, 8, 8); // Google's DNS
printf("Starting to ping IP: %d.%d.%d.%d.\r\n", (int)ip4_addr1(&target_ping_ip),
(int)ip4_addr2(&target_ping_ip), (int)ip4_addr3(&target_ping_ip),
(int)ip4_addr4(&target_ping_ip));
if((ret = ping_ip(target_ping_ip)) != PING_ERR_OK) {
printf("Error while sending ping: %d\r\n", ret);
}
}
//every 4 seconds, start a new ping attempt
sys_timeout(4000, start_ping_action, NULL);
}
void check_ping_result() {
ping_result_t res;
memset(&res, 0, sizeof(res));
int retcode = 0;
if((retcode = ping_ip_result(&res)) == PING_ERR_OK) {
if (res.result_code == PING_RES_ECHO_REPLY) {
printf("Good ping from %s %u ms\r\n", ipaddr_ntoa(&res.response_ip),
(unsigned) res.response_time_ms);
} else {
printf("Bad ping err %d\r\n", res.result_code);
}
} else {
//printf("No ping result available yet: %d\n", retcode);
}
sys_timeout(100, check_ping_result, NULL);
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
Output logs:
UART6 init done
GSM: Starting Modem thread
GSM: AT COMMAND: [AT..]
GSM: AT RESPONSE: [..OK..]
GSM: AT COMMAND: [ATD*99#..]
GSM: AT RESPONSE: [..CONNECT 115200..]
GSM: GSM initialized.
GSM: ppp_status_cb: 0
GSM: status_cb: Connected
GSM: ipaddr = 10.220.50.XXX
GSM: gateway = 10.64.64.XX
GSM: netmask = 255.255.255.255
GSM: LTE Init done
Starting StartDefaultTask
IP:10.220.50.142
Starting to ping IP: 216.58.213.XXX.
Ping timed out
Bad ping err 0
Starting to ping IP: 216.58.213.XXX.
Ping timed out
Bad ping err 0
Starting to ping IP: 216.58.213.XXX.
Ping timed out
Bad ping err 0
Starting to ping IP: 216.58.213.XXX.