Linux Kernel Module for the TSIC 306

I’ve made my first foray into the kernel, by writing a kernel module for the TSIC 306 temperature sensor. After overcoming the difficulties in setting up the tools, the actual driver development was much easier than expected. The source code is up on Google Code and is basically working, but needs a little more development.

This maps the sensor to the file-system, so you can load the module and read the temperature like this:

$ sudo insmod tsic.ko
$ cat /sys/kernel/tsic/temp
14680

In the example above, the temperature is 14.68°C and all the values are scaled by 1000 to avoid floating point maths in the module, and to match the behaviour of existing sensors such as:

$ cat /sys/class/thermal/thermal_zone0/temp 
40084

Writing the module turned out to be easier than expected, and there are some great libraries such as gpiolib and kobject which simplify development. Documentation is a bit scarce, but there are some useful resources:

Eventually I plan to add support for multiple sensors on configurable GPIO pins,  make it more robust and clean up a few issues such as the lack of time-outs in the I/O.

7 thoughts on “Linux Kernel Module for the TSIC 306”

    1. Not yet, but I might implement support for multiple sensors at some point. I’ll post an update on this blog, or you can check progress on the google code site. Out of interest, what’s your project?

  1. Dear James,

    thank you for sharing the tsic kernel code!
    I am very interested in using the tsic kernel module for my own RPi project using an TSIC206 temp sensor in an HVAC scenario.

    I managed to compile the module on my Pi, but I’m struggeling to get the modul loaded and running. Using
    `sudo insmod tsic.ko`
    gives me the command line error:
    `insmod: ERROR: could not insert module /home/pi/tsic/tsic-kernel/trunk/tsic.ko: Cannot allocate memory`

    dmesg tells me
    `[178869.424057] WARNING: CPU: 1 PID: 29027 at fs/sysfs/dir.c:31 sysfs_warn_dup+0x78/0x88
    [178869.431914] sysfs: cannot create duplicate filename ‘/kernel/tsic’

    [178869.599368] kobject_add_internal failed for tsic with -EEXIST, don’t try to register things with the same name in the same directory.
    [178869.611487] kobject_create_and_add: kobject_add error: -17`

    The directory `/sys/kernel/tsic`indeed exists but reading the file`temp`in it only outputs lots of entries such as
    `Message from syslogd@pi at Jan 11 23:15:14 …
    kernel:[179568.667631] 7fa0: 80108000 8028c0e8 00020000 7fffe000 00000003 76c40000 00020000 00027184`

    I would appreciate if you have any advice for me!

    Regards
    Sebastian

    1. Hi Sebastian
      It’s a few years since I worked with the Kernel module, but this old forum thread might help you to get it working (note that the Google Code links may be dead, as I had to move all the code to Github when Google shut down Google code):
      https://www.raspberrypi.org/forums/viewtopic.php?t=30325
      As I remember, I had to cross-compile the module on Ubuntu (didn’t compile on the Pi).
      Eventually, I rewrote this to run from user mode (without the Kernel module) by using the PIGPIO library. If you’re developing in C++, that might also be a better option for you:
      https://github.com/jam3sward/tsic-pigpio
      Let me know if you need any further help, and I’ll try to take a look for you.
      Best regards
      James

  2. Hi James,
    I could compile the `tsic-pigpio` project. But to make it work, I had to add the folowing line:
    #include <unistd.h>
    at the beginning of `main.cpp`. Otherwise the compiler did not know about e.g. `opterr`.
    Then it worked like a charm. I can now read my TSIC206 temperature. Thank you again for the good work!
    Eventually I will try to cross-compile the tsic-kernel module. I’m still not successfull on this side.

    Regards, Sebastian

  3. That’s great! I’m glad you got it working.
    It took me a while to figure out how, but I fixed the angle brackets in your post above 😉
    I’ll add the additional include on Github (it worked without originally, but I guess the headers must have changed since and I was “getting away with it” before).
    Regards
    James

Leave a Reply

Your email address will not be published. Required fields are marked *