Punctual Atomic Embedded Linux Driver Development–Linux 4G Communication

I have learned previously how to use a wired network or WIFI in Linux, but there are many restrictions on using a wired network or WIFI because wiring is required. Even WIFI must be wired first and then connected to a wireless router. There are many occasions where wiring is inconvenient. This is when 4G comes into play. Products can be directly connected to the network through 4G to achieve unattended operation. In this chapter, let’s learn how to use 4G to implement networking functions in the STM32MP157 development board.

Introduction to 4G network connection

4G network connection. For embedded Linux, 4G network connection is actually not difficult! You can look at other embedded Linux or Android development boards. 4G modules are all MiniPCIE interfaces, including many 4G modules are MiniPCIE interface. But after a little in-depth research, you will find that although these 4G modules use MiniPCIE interfaces, the actual communication interfaces are USB, so the 4G module drivers are converted to USB drivers. These 4G module manufacturers have provided detailed documents explaining how to use 4G modules under Linux and how to modify the Linux kernel to add 4G module drivers. Jiandian Atomic’s STM32MP157 development board also has a 4G module interface in the form of MiniPCIE. Although the appearance is MiniPCIE, it is actually USB. The 4G module schematic diagram of the STM32MP157 development board is shown below:

4g module schematic

U22 in the picture above is the MiniPCIE interface, and the MiniPCIE interface is connected to DP7 and DM7 of the FE2.1 HUB chip. U26 is the Nano SIM interface, and the STM32MP157 development board uses Nano SIM cards, so you can directly use your own mobile phone card for testing. The location of the 4G interface of the STM32MP157 development board is shown in the figure below:

ALPHA development board 4G interface

Before use, you need to insert the 4G module into the MiniPCIE interface shown above, then tighten the screws on both sides, and insert the Nano SIM card into the Nano SIM holder shown above strong>, pay attention to the direction of the Nano SIM card!

The metal contacts of the Nano SIM card are facing down!

Theoretically, all 4G modules with MiniPCIE interface can be connected to Zhengdian Atom’s STM32MP157 development board, because these 4G modules follow the same interface standard, but you still need to be careful when using it. Take a look at the interface pin description of the 4G module. Different 4G modules have different drive forms. This chapter explains the use of two 4G modules on the development board. One is the EC20 of Shanghai Quectel and the other is the ME3630 of Gosuncn IoT. Both 4G modules have MiniPCIE Interfaces, these two 4G modules are as shown in the figure below:

4G module

In the picture above, the left side is the ME3630-W 4G module of Gosuncn IoT, and the right side is the EC20 4G module of Shanghai Quectel. This chapter will explain how to use the two 4G modules EC20 and ME3630 on the development board.

The 4G module requires an antenna to work, so when purchasing a 4G module, you must remember to purchase an antenna, otherwise the test cannot be carried out. Generally, the antenna interface left by the 4G module with the MiniPCIE interface is an IPEX socket, so when purchasing an antenna, you should also choose an IPEX interface, or use an IPEX to SMA cable to transfer it.

Gosuncn ME3630 4G module experiment

ME3630 4G module introduction

ME3630 is an LTE Cat.4 seven-mode full network 4G module. In LTE mode, it can provide 50Mbps uplink rate and 150Mbps downlink rate, and supports fallback to 3G or 2G network. This modulesupports diversity reception. Diversity reception is a wireless connection technology in which terminal products support dual antennas to improve communication quality and communication reliability. ME3630 supports multiple network protocols, such as PAP, CHAP, PPP, etc., and has multiple functions, such as GNSS, Remote wakeup, SMS, supports FoTA over-the-air upgrade, etc. The ME3630 4G module is widely used in M2M fields such as smart meter reading, security information collection, industrial routers, vehicle communications and monitoring. ME3630 4G module features are as follows:

  1. One USB2.0 interface.
  2. One UART interface.
  3. The SIM card interface supports 1.8/3.0V.
  4. Built-in protocols such as TCP, UDP, FTP and HTTP.
  5. Support RAS/ECM/NDIS.
  6. Support AT commands.

The ME3630 4G module has a variety of configurations, such as pure data version, integrated GNSS version, full Netcom version, etc. This tutorial mainly uses the data communication function of ME3630, so it is recommended that you buy the full Netcom data version. If you want For the positioning function, buy the full Netcom data + GNSS version. As for other versions, you can choose according to your actual needs. Before officially using the ME3630 4G module, please insert it into the MiniPCIE socket of the development board, tighten the screws, insert the Nano SIM card, and connect the antenna, as shown in the figure below:

Development board connected to ME3630 module

After everything is ready, you can start driving the ME3630 4G module.

ME3630 4G module driver modification

Add USB device information

You need to add the USB device information of ME3630 in the Linux kernel first, because as mentioned earlier, the USB interface used by the ME3630 4G module. Open the drivers/usb/serial/option.c file of the Linux source code, find the option_ids array, and then add the PID and VID of ME3630 in it. The content to be added is as follows:

Sample code 54.2.2.1 ME3630 PID and VID information
1 {<!-- --> USB_DEVICE(0x19d2, 0x0117) }, /* ME3630*/
2 {<!-- --> USB_DEVICE(0x19d2, 0x0199) },
3 {<!-- --> USB_DEVICE(0x19d2, 0x1476) },

The completed option_ids array is as shown below:

option_ids array after adding PID and VID

Add ECM support program

ME3630 supports the ECM interface and can be easily connected to the Internet through the ECM interface. If you want to use the ECM interface, you need to modify the option_probe function in the drivers/usb/serial/option.c file. Find this function and enter the following content in it:

Sample code 54.2.2.2 What needs to be added to the option_probe function
1 /* GOSUNCN 4G modems */
2 printk("idVendor=%x, idProduct=%x, bInterfaceNumber =%d\r\
",
3 serial->dev->descriptor.idVendor,
4 serial->dev->descriptor.idProduct,
5 serial->interface->cur_altsetting->desc. bInterfaceNumber);
6
7 if (serial->dev->descriptor.idVendor == 0x19d2 & amp; & amp;
8 serial->dev->descriptor.idProduct == 0x1476 & amp; & amp;
9 serial->interface->cur_altsetting->desc. bInterfaceNumber == 3)
10 return -ENODEV;
11
12 if (serial->dev->descriptor.idVendor == 0x19d2 & amp; & amp;
13 serial->dev->descriptor.idProduct == 0x1476 & amp; & amp;
14 serial->interface->cur_altsetting->desc. bInterfaceNumber == 4)
15 return -ENODEV;
16
17 if (serial->dev->descriptor.idVendor == 0x19d2 & amp; & amp;
18 serial->dev->descriptor.idProduct == 0x1509 & amp; & amp;
19 serial->interface->cur_altsetting->desc. bInterfaceNumber == 4)
20 return -ENODEV;
twenty one
22 if (serial->dev->descriptor.idVendor == 0x19d2 & amp; & amp;
23 serial->dev->descriptor.idProduct == 0x1509 & amp; & amp;
24 serial->interface->cur_altsetting->desc. bInterfaceNumber == 5)
25 return -ENODEV;

The option_probe function after the addition is completed is shown in the figure below:

Content added to option_probe

Configuring the Linux kernel

The Linux kernel needs to be configured. First enable the USBNET function. The path is as follows:

-> Device Drivers
-> [*]Network device support
-> USB Network Adapters
-> <*> Multi-purpose USB Networking Framework

The configuration is shown in the figure below:

Enable USB network

Nextyou also need to enable the USB serial port GSM and CDMA drivers. The configuration path is as follows:

-> Device Drivers
-> [*] USB support
-> <*> USB Serial Converter support //selected
-> <*> USB driver for GSM and CDMA modems //selected

The configuration is shown in the figure below:

USB GSM and CDMA

Continue to configure the Linux kernel, enable USB CDC ACM mode, the configuration path is as follows:

-> Device Drivers
-> [*] USB support
-> <*> Support for Host-side USB
-> <*> USB Modem (CDC ACM) support

The configuration is shown in the figure below:

Enable USB CDC ACM function

That’s it for the configuration of the Linux kernel. Compile the Linux kernel, and then use the new uImage to start the development board. If ME3630 has been plugged in, the system will output information as shown below after startup:

ME3630 Virtual USB Information

As can be seen from the picture above, ME3630 has virtualized three USB devices, namely ttyUSB0-ttyUSB2. For 4G modules that support ECM interface, such as ZM5330/ZM8620/ME3620/ME3630. If the module works in ECM mode, you can check the corresponding network card by running the “ifconfig -a” command. The name of the network card may be usbX/ecmX/ethX, etc. X is a specific number. If it exists, it indicates the ECM interface. Driver loaded successfully. Enter the “ifconfig -a” command, and you will find an additional network card named “usb0”, as shown in the following figure:

USB0 network card corresponding to ME3630

ME3630 4G module ppp networking test

Enable Linux kernel ppp function

-> Device Drivers
-> [*] Network device support
-> <*> PPP (point-to-point protocol) support
-> <*> PPP BSD-Compress compression
-> <*> PPP Deflate compression
-> [*] PPP filtering
-> <*> PPP MPPE compression (encryption)
-> [*] PPP multilink support
-> <*> PPP over Ethernet
-> <*> PPP support for async serial ports
-> <*> PPP support for sync tty ports

After the configuration is completed, it is as shown in the figure below:

Linux kernel PPP enablement

After the configuration is completed, recompile the Linux kernel to obtain a new uImage image file, and then use the new uImage image file to start the development board.

Port pppd software

You need to use pppd software to implement ppp dial-up Internet access. You can enable it directly in buildroot. The configuration path is as follows:

-> Target packages
-> Networking applications
-> pppd
-> [*] filtering
-> [*] radius
-> [*] overwrite /etc/resolv.conf (NEW)

The configuration is shown in the figure below:

pppd tool

After the configuration is completed, recompile the file system. After the compilation is completed, enter the output/images directory and run the following command to replace the file system:

cd output/images/ //Enter the output/images directory
sudo tar -axvf rootfs.tar -C /home/zuozhongkai/linux/nfs/rootfs //Extract to the nfsroot directory

The above command will decompress the compressed package output/images/rootfs.tar in buildroot to the directory /home/zuozhongkai/linux/nfs/rootfs. This directory is the nfsroot directory in the tutorial. Extract it to the corresponding directory file according to your actual situation. middle.

Restart the development board. After restarting, enter “pppd -v” to check the pppd version number. If the pppd version number is displayed normally, it means that the pppd transplant is successful, as shown in the following figure:

pppd version number information

ppp Internet test

Before using pppd for dial-up Internet access, you need to create 4 files. These 4 files must be placed in the same directory. Create the /etc/ppp/gosuncn directory under the root file system of the development board, enter the /etc/ppp/gosuncn directory just created, and then create a new shell script file named “ppp-on”, enter the following content in the ppp-on file:

Sample code 54.2.3.1 ppp-on file content
1 #!/bin/sh
2 clear
3 OPTION_FILE="gosuncn_options"
4 DIALER_SCRIPT=$(pwd)/gosuncn_ppp_dialer
5 exec pppd file $OPTION_FILE connect "chat -v -f ${DIALER_SCRIPT}"

Create a new file named “gosuncn_options” and enter the following content in the file:

Sample code 54.2.3.2 gosuncn_options file content
1/dev/ttyUSB2
2 115200
3 crtscts
4 modems
5 persist
6 locks
7 noauth
8noipdefault
9 debug
10 nodetach
11 user Anyname
12 password Anypassword
13 ipcp-accept-local
14ipcp-accept-remote
15 defaultroute
16 usepeerdns
17 noccp
18nobsdcomp
19 novj
20 dumps

Line 1, if it is a China Unicom or China Mobile card, use ttyUSB2.

Create a new file named “gosuncn_ppp_dialer” and enter the following content:

Sample code 54.2.3.3 gosuncn_ppp_dialer file content
1 ABORT "NO CARRIER"
2 ABORT "ERROR"
3 TIMEOUT 120
4 "" ATE
5 SAY "ATE"
6 ECHO ON
7 OK ATH
8 OK ATP
9 OK AT + CGDCONT=1,"IP","3GNET"
10 OKATD*99#
11 CONNECT

Lines 9 and 10 are the network APN codes. If it is a China Unicom card, the contents of lines 9 and 10 are as follows:

OK AT + CGDCONT=1,"IP","3GNET"
OK ATD*99#

If it is a telecommunications card, the contents of lines 9 and 10 are as follows:

OK AT + CGDCONT=1,"IP","CTNET"
OK ATD*99#

If it is a mobile card, the contents of lines 9 and 10 are as follows:

OK AT + CGDCONT=1,"IP","CMNET"
OK ATD*99#

Finally, create a new shell script named “disconnect” and enter the following content:

Sample code 54.2.3.4 disconnect file content
1 #!/bin/sh
2 killall pppd

After writing these four files, give the ppp-on and disconnect files executable permissions, the command is as follows
Down:

chmod 777 ppp-on disconnect

After completion, enter the following command to connect to the 4G network:

./ppp-on & amp;

If ME3630 successfully connects to the 4G network, it will automatically apply for an IP address and print out the corresponding address information, as shown in the figure below:

4G connection process

As can be seen from the picture above, the 4G network connection is successful and the IP address is 10.40.133.153. After ppp dial-up is successful, a network card named “ppp0” will be generated, as shown in the figure above:

ppp0 network card information

The 4G network test needs to turn off other network cards, otherwise there may be problems with the network test, but now the system is started through the network, and the root file system is mounted through NFS, so other network cards cannot be turned off. For example, eth0. In order to solve this problem, uboot, Linux kernel, .dtb device tree and root file system can only be burned into the EMMC of the board, and then the system on the EMMC can be started directly, so that other network cards are not required to work. There.

After programming is completed, restart the development board and make sure that the current development board only has one ppp0 network card corresponding to ME3630. Finally, you can directly ping Baidu official website. The result is as shown in the figure below:

ME3630 4G module ping Baidu official website

ME3630 4G module ECM networking test

For modules that support the ECM interface, you can access the Internet directly through the ECM. The ME3630 module supports the ECM interface. Restart the development board. Enter the “ifconfig -a” command to see that there is a port named “usb0” Network cardas shown below:

usb0 network card

This usb0 network card is the network card corresponding to the ECM interface. Create a shell script named “ecm_on” in the /etc/ppp/gosuncn (created in the previous section) directory of the development board root file system. The content is as follows:

Sample code 54.2.4.1 ecm_on file content
1 #!/bin/sh
2 clear
3 OPTION_FILE="gosuncn_options"
4 DIALER_SCRIPT=$(pwd)/gosuncn_ecm_dialer
5 exec pppd file $OPTION_FILE connect "chat -v -f ${DIALER_SCRIPT}"

Finally create a file named “gosuncn_ecm_dialer” with the following contents:

Sample code 54.2.4.2 gosuncn_ecm_dialer file content
1 ABORT "NO CARRIER"
2 ABORT "ERROR"
3 TIMEOUT 120
4 "" ATE
5 SAY "ATE"
6 ECHO ON
7 OK ATH
8 OK ATP
9 OK AT + ZSWITCH=L
10 OK AT + ZECMCALL=1
11 OK AT + CGDCONT=1,"IP","3GNET"
12 OK ATD*99#
13 CONNECT

Lines 11 and 12 are the network APN codes. If it is a China Unicom card, the contents of lines 11 and 12 are as follows:

OK AT + CGDCONT=1,"IP","3GNET"
OK ATD*99#

If it is a telecommunications card, the contents of lines 11 and 12 are as follows:

OK AT + CGDCONT=1,"IP","CTNET"
OK ATD*99#

If it is a mobile card, the contents of lines 11 and 12 are as follows:

OK AT + CGDCONT=1,"IP","CMNET"
OK ATD*99#

After completion, give the ecm-on file executable permission, the command is as follows
Down:

chmod 777 ecm-on

After completion, enter the following command to connect to the 4G network:

./ecm-on & amp;

If ME3630 successfully connects to the 4G network, the message shown below will appear:

ECM network connection successful

After the connection is successful, open the usb0 network card. The command is as follows:

ifconfig usb0 up //Open usb0 network card

After the usb0 network card is opened, enter the following command to obtain the IP address:

udhcpc -i usb0 //Get IP address

The process of obtaining an IP address is shown in the figure below:

The process of obtaining an IP address for the usb0 network card

As can be seen from the picture above, the IP address obtained by the usb0 network card is 10.35.243.53, and then ping the Baidu official website. If the ping can be successful, it means that the ECM interface of ME3630 is successfully connected.

At this point, the network connection of the ME3630 4G module has been fully tested, and you can use 4G to access the Internet on the STM32MP157 development board of Punctual Atom.

ME3630 4G module GNSS positioning test

Attention! The GNSS of ME3630-C3C requires a passive antenna. Active antennas cannot be used, otherwise positioning cannot be performed!

Some models of ME3630 have GNSS function, which is GPS positioning. When I transplanted the ME3630 driver before, I knew that there would eventually be three ttyUSB devices, namely ttyUSB0-ttyUSB2, of which ttyUSB1 is the GNSS interface. If you have used other GPS modules before, you should know that the GPS module outputs through the serial port, and the ME3630 is the same, except that the ME3630 is a USB-to-serial port, and ttyUSB1 is the GNSS serial port converted from the ME3630. So you can use minicom directly to view the GNSS information output by ttyUSB1. The GNSS positioning of ME3630 uses the ttyUSB1 interface, and the baud rate is 115200. Therefore, the minicom configuration in this section is as shown below:

minicom configuration

Use AT commands to configure ME3630

The GNSS positioning output function of ME3630 is turned off by default and must be configured using AT commands first. The AT command configuration process is as follows, where the dark color is the AT command, and “OK” is the return value of ME3630:

ME3630-C3C AT command to configure GPS

After the AT command configuration is completed, ME3630 will start searching for stars. Note that the GPS antenna must be placed outdoors! After the ME3630 star search is completed, the positioning information in NEMA format will be output, as shown in the figure below:

GNSS positioning information

In actual application development, NEMA format data is directly parsed, and then specific longitude and latitude, speed, altitude, UTC time and other information are obtained.

EC20 4G module experiment

In this part, I bought a 4G module from Atom, so it was from the previous experiment, so I haven’t looked at it here.

Quectel GobiNET driver transplantation

This is for the EC20 4G module, I haven’t looked at it yet.

Summary

I looked online and found that Wildfire has the EC20 4G module, so if you are using that module, you can also take a look at the punctual atomic tutorials corresponding to this chapter.

The same as the 4G module, you don’t need to write the driver yourself, you just need to learn to use it.

First, you need to add the PID and VID corresponding to the ME3630 in the option_ids array in drivers/usb/serial/option.c, and then add the ECM support program in the option_probe function.

Afterwards, you need to configure the Linux kernel, enable USBNET, enable USB serial port GSM and CDMA drivers, and enable USB CDC ACM mode; at the same time, in order to test the 4G module networking, you need to enable the ppp function of the LInux kernel and install the pppd software in the buildroot. .

To test the ppp Internet access, you need to create the /etc/ppp/gosuncn directory by yourself, and create the corresponding 4 files. Give execution permissions and then test. Here, you need to burn to EMMC and test. If you still use nfs to mount, problems may occur. .

For ECM networking testing, you need to add 2 files to /etc/ppp/gosuncn and grant execution permissions.

For the GNSS positioning test of this module, you need to configure the minicom to read the baud rate of 115200, and then configure the ME3630 through the corresponding AT command to read the data in NEMA format.