introduction to embedded software -...
TRANSCRIPT
Introduction to Embedded Software
Practice #5 Communicate with the Nunchuk over I2C
Dongkun Shin Embedded Software Laboratory
Sungkyunkwan University http://nyx.skku.ac.kr/
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Objective
• Add the WiiNunchuk device to the device tree.
• Write a kernel module for the WiiNunchuk.
- init( ), exit( )
- probe( ), remove( )
2
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Exercise
• WiiNunchuk module
- Register your I2C device driver
- Poll data from the WiiNunchuk every 50ms
- Print the polled data (dmesg)
✓ Joystick X, Y
✓ Accelerometer X, Y, Z
✓ C-button, Z-button
3
Review…
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Register WiiNunchuk to the Device Tree
• Override the i2c1 node for the WiiNunchuk
5
$ cd linux/arch/arm/boot/dts/ $ vim bcm2710-rpi-3-b.dts
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Rebuild the Device Tree
6
$ make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs -j16
$ scp arch/arm/boot/dts/*.dtb [email protected]:/home/pi/
$ scp arch/arm/boot/dts/overlays/*.dtb* [email protected]:/home/pi
$ sudo mv *.dtb /boot/
$ sudo mv *.dtb* /boot/overlays/ $ sudo reboot
Host PC Raspberry Pi
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Check the Device Tree
• Check that the WiiNunchuk device is registered
7
$ find /sys/firmware/devicetree/ -name “*nunchuk*”
WiiNunchuk Device Driver
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Exercise
• Complete the WiiNunchuk device driver
- Device initialization
- Input polling
- Device registration
• Due ~ 5/12 (Sun) 23:59:59
- Submit to i-Campus
- File to submit: nunchuk.c
9
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
WiiNunchuk Device Driver• Device initialization
1. Send two bytes to the device (0xf0, 0x55)
2. Send two bytes to the device (0xfb, 0x00)
• Input polling - read device registers
- The nunchuk exhibits a rather weird behavior: it seems that it updates the state of its internal registers only when they have been read.→ WE WILL NEED TO READ THE REGISTERS TWICE
1. Write 0x00 to the bus. This will allow us to read the device registers
2. Read 6 bytes from the device
10
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Functions to Use• i2c_add_driver( ), i2c_del_driver( )
• i2c_transfer( )
• input_event( )
- input_report_key( ), input_report_abs( )
- input_sync( )
11
linux/include/linux/i2c.h linux/include/linux/input.h
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
WiiNunchuk Output
12
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Output
13
$ sudo modprobe input-polldev $ sudo insmod nunchuk.ko
Introduction to Embedded Software SWE3027-41
Practice #5 2019-05-07
Output
14