Thread: How to write a driver in C for an IC

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    97

    How to write a driver in C for an IC

    Hi everyone,


    I want to try to write a driver for this battery fuel gauge http://www.ti.com/lit/ds/symlink/bq2...=1588097914776.

    This is my first attempt of writing a driver and it seems like a great opportunity to learn many things through the procedure.

    As I mentioned, I've never written a driver before and I want to know If there are any general rules or guides or steps (from where to start and where to finish) that I should follow. There are some general information that I should gather before starting? What I know so far, is that the communication protocol will be the I2C and I will write in C


    Thanks in advance
    Nick

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Is there an operating system between you and the hardware?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Hi Salem and thank you for you reply,

    Thank you for your reply. I am running a windows machine (if this is what you mean) and I will use the nRF52840 SoC which integrates Arm Cortex-M4F 32-bit processor
    nRF52840 Multi-Protocol 2.4GHz SoC - Nordic | Mouser

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I am running a windows machine (if this is what you mean)
    This is your development host.

    > I will use the nRF52840 SoC which integrates Arm Cortex-M4F 32-bit processor
    Yes, the other software (maybe an operating system) also running on here at the same time as your driver.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Quote Originally Posted by Salem View Post
    >

    > I will use the nRF52840 SoC which integrates Arm Cortex-M4F 32-bit processor
    Yes, the other software (maybe an operating system) also running on here at the same time as your driver.
    No there is not any OS.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, so you're reading from the chip using your driver, and sending the information to where?

    * Software stacks available as downloads
    * Application development independent of protocol stack
    Are there any useful code samples you can download to take care of the low level bit-bashing to talk to say the I2C bus?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    I will read from the chip using the driver and then I will send the data through BLE to the central
    - I wrote the code to send the data over BLE

    I believe that I used wrong terminology.. I want to write a library and not a driver.. Actually I want to communicate with this particular chip and send the data over BLE
    This is a library example I found
    GitHub - svcguy/lib-BQ27421: A library based on STM32 CubeMX HAL drivers for interfacing with the Texas Instruments BQ27421 Fuel Gauge IC

    I want to write something similar with that from scratch, that's why I'm asking if there are any general rules/guidelines I sould follow.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just general programming guidelines IMO.

    But if you've never done low level bit bashing this close to hardware before, then read everything you can on the chip in question, everything you can about I2C.

    Create lots of little functions
    - send 1 byte to I2C
    - send n bytes to I2C
    - receive 1 byte from I2C
    - receive n bytes from I2C

    You need to test each of these thoroughly before you implement some higher level function like 'read_battery_level', which will involve several I2C transactions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    97
    Thank you Salem,

    So far this is the backbone I am thinking to follow:

    1. Create a header file and define registers, declare functions, and create structures
    2. Create a .c file and define the functions

    Tips:
    - Use prefixes or suffixes for registers/ functions and structures (e.g.
    BFG_ for “battery fuel gauge")
    - Test the I2C read and write functions and confirm that you can read and write (single and multiple bytes) with success

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Lot of WRITE calls in parallel in C WRITE test code
    By Bobby1995 in forum C Programming
    Replies: 2
    Last Post: 04-28-2020, 08:47 AM
  2. Replies: 9
    Last Post: 09-09-2014, 08:23 AM
  3. hi how to check my serial port is intrupt driver or poll driver?
    By nkrao123@gmail. in forum Networking/Device Communication
    Replies: 5
    Last Post: 09-19-2011, 11:20 PM
  4. [C++] USB gadget driver, unbound kernel driver?
    By Jelte in forum C++ Programming
    Replies: 0
    Last Post: 01-13-2010, 08:53 AM
  5. problem with fout.write: cannot write 0x0A (10 dec)
    By yvesdefrenne in forum C++ Programming
    Replies: 7
    Last Post: 05-23-2005, 12:53 PM

Tags for this Thread