Complete Android CAN Bus Program Development
I recently studied a TMDEVM-AM3358 Development Board of TI Company, which is an industrial evaluation board with abundant hardware resources and excellent performance. I need to use Nic, serial port and CAN bus on the board. As we all know, in a mobile operating system like android that is popular all over the world, Nic support has been well developed along with the development of this system. However, the line of mouth has gradually faded out of our field of view, but it is easy to use and still comes in handy in many times. The most rare one is the CAN bus, so many problems have been encountered in debugging the CAN module. Unfortunately, it seems that even Google is hard to find an introduction to the can development of the AM335x ARM Development Board, here we will summarize the work I have done and problems on the CAN during this period. This makes it easy for people who need to take a detour.
For the introduction of CAN, the official website provides a document and click to download it. After several times of reading, I had some knowledge about his basic steps and intentions, but I was confused that all the steps were completed on the PC, however, the last step is on the ARM. I didn't see any operations on the ARM board. How can I achieve this? To understand this problem, first take a look at the procedure of the document.
First, we need to start the CAN module on the ARM board, adjust SW8, and set profile to 1. SW8 is at the bottom of the display cable, as shown in:
Run the linux system on the ARM Development Board without modifying the canutil tool source code.
If your ARM is a running linux system, congratulations. TI's linux source code has integrated the CANUTIL tool and can be used directly. However, the CAN driver does not appear to be enabled by default. For example, in an ARM terminal
root@am335x-evm:~# canconfig can0 bitrate 50000 Cannot find device can0 failed to set bitrate of can0 to 50000
The system prompts that the can0 device cannot be found, indicating that the CAN driver is not added to the kernel. We need to configure the driver as follows:
Input in the kernel root directory
gedit Makefile
Find the following snippet:
Export KBUILD_BUILDHOST: = (SUBARCH) ARCH? = (SUBARCH)
CROSS_COMPILE? = $ (CONFIG_CROSS_COMPILE: "%" = %)
To:
Export KBUILD_BUILDHOST: =$ (SUBARCH)
ARCH? = Arm
CROSS_COMPILE? = Arm-eabi-
This step aims to make the current kernel compilation mode an ARM platform.
Next, enter:
make menuconfig
Follow the steps shown in the figure to select one:
1. [*] Networking support->
2. <*> CAN bus subsystem support->
3. CAN Device Drivers->
4. <*> Bosch D_CAN devices->
Note that the D_CAN here is asterisks.
5. Generic Platform Bus based D_CAN driver
Also, this item is marked with an asterisk.
Okay. After the driver is configured, run the Make command. Then, Make the generated kernel image to the SD card and restart the Development Board. Then the CAN0 device can be used.
You can configure and send and receive data using the following commands:
1. Set the can0 baud rate to 50000 and enable the three sampling mode. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: java;"> canconfig can0 bitrate 50000 ctrlmode triple-sampling onEnable the can0 Device
canconfig can0 start
Send data
cansend can0 -i 0x10 0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x88
Receive data
candump can0
Run the android system on the ARM Development Board or re-compile the canutil tool.
If you are using android or you want to modify and recompile the CAN tool, you need to continue with the following steps.
Configure the CAN driver in the kernel and set Profile Selection to 1.
Then download the source code. In the official instructions, you need to download three source codes (click to download them ):
-Iproute2
-Libsocket
-Canutil
But here I only need to use the last canutil.
After the download is complete, configure the environment variables as follows:
export GNUEABI=arm-arago-linux-gnueabiexport CC=$GNUEABI-gccexport LD=$GNUEABI-ldexport NM=$GNUEABI-nmexport AR=$GNUEABI-arexport RANLIB=$GNUEABI-ranlibexport CXX=$GNUEABI-c++export PREFIX=$FILESYS_PATH/usrexport CROSS_COMPILE_PREFIX=$PREFIXexport PATH=$TOOL_CHAIN_PATH/bin:$PATHexport LIBSOCKETCAN_INSTALL_DIR=$LIBSOCKETCAN_PATH/installexport PKG_CONFIG_PATH=$LIBSOCKETCAN_PATH/configexport LD_LIBRARY_PATH=${LIBDIR}:${LD_LIBRARY_PATH}export LD_RAN_PATH=${LIBDIR}:${LD_RAN_PATH}export LDFLAGS=-Wl,--rpath -Wl,$LIBSOCKETCAN_INSTALL_DIR/libexport INCLUDES=-I$LIBSOCKETCAN_INSTALL_DIR/include
The PREFIX is the target directory, which can be set by yourself.
Then go to the canutils-4.0.6 folder and enter the following command:
./configure --host=arm-arago-linux --prefix=$PREFIX --enable-debug
The Makefile is generated based on the target platform. Run the following commands in sequence:
make
make install
After completion, the following four files are found under PREFIX/usr/bin:
Cansend, candump, cansequence, canecho
In PREFIX/usr/sbin, find the following file:
Canconfig
Copy these files to the/system/bin directory of the android system (root permission required ). In this way, we can execute these five commands.
However, if you execute the command directly, the following error may occur:
root@android:/ # cansend/system/bin/sh: cansend: No such file or directory
This indicates that the cansend application is dynamically compiled and the required library file is missing locally. You can run the readelf command on the PC to view the required library files.
root@MC:~/android/can/usr/bin# readelf -l cansendElf file type is EXEC (Executable file)Entry point 0x8679There are 8 program headers, starting at offset 52Program Headers: Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align EXIDX 0x001074 0x00009074 0x00009074 0x00008 0x00008 R 0x4 PHDR 0x000034 0x00008034 0x00008034 0x00100 0x00100 R E 0x4 INTERP 0x000134 0x00008134 0x00008134 0x00019 0x00019 R 0x1 [Requesting program interpreter: /lib/ld-linux-armhf.so.3] LOAD 0x000000 0x00008000 0x00008000 0x01080 0x01080 R E 0x8000 LOAD 0x001080 0x00011080 0x00011080 0x00158 0x00170 RW 0x8000 DYNAMIC 0x00108c 0x0001108c 0x0001108c 0x000e8 0x000e8 RW 0x4 NOTE 0x000150 0x00008150 0x00008150 0x00044 0x00044 R 0x4 GNU_STACK 0x000000 0x00000000 0x00000000 0x00000 0x00000 RW 0x4
You can see a sentence in the middle:
[Requesting program interpreter:/lib/ld-linux-armhf.so.3]
Here the ld-linux-armhf.so.3 library file is required to use the cansend command. This is also a difference between android and linux in the file system. android cannot directly run applications dynamically compiled by the cross compiler. For more information, see my other blog --
How to run a dynamically compiled program in the Android system. Copy the library files in the ARM-linux system to the/lib directory of the android system.
In this case, you can use canconfig to configure the can device, send data through cansend and cansequence, and receive data through candump. For specific instruction sets, see the official documentation.
The above is the debugging process for the can module of the AM335x Development Board since this time, and it has been a lot of effort, mainly because there are too few materials, except for the documentation, I did not find an introduction to the android CAN of this development board. If you have any questions, leave a message to discuss them.