How to write a driver in C for an IC

Thread Starter

nikolakisant03

Joined Oct 21, 2012
21
Hi everyone,

I want to try to write a driver for this battery fuel gauge. 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
 

402DF855

Joined Feb 9, 2013
271
Also, determine which functions of the sensor are important to you. I'd probably start by getting code to read temperature and voltage, and then be able to write the control word.
 

Thread Starter

nikolakisant03

Joined Oct 21, 2012
21
Yes, I am using the nRF52840 dev, board for the prototyping

Do you intend to run Linux on the board? What features of the sensor are you interested in?
No, I don't
At the moment I want to measure the basics: battery voltage, remaining capacity, average current & power, state of charge and state of health

What is a driver?
A piece of code that will include all definitions and functions in order to read and write information from the peripheral (bq27421 in my case). Maybe the word "driver" is wrong? Maybe it's better the word "library"? In any case, you understood what I want to achieve :)
 

BobTPH

Joined Jun 5, 2013
8,654
you understood what I want to achieve
No he didn’t, nor did I. A driver on Windows or linux is a very specific thing and quite different from what you are apparently talking about.

I believe what you are asking is how to communicate with a particular chip on a particular microcontroller.

Bob
 

Thread Starter

nikolakisant03

Joined Oct 21, 2012
21
No he didn’t, nor did I. A driver on Windows or linux is a very specific thing and quite different from what you are apparently talking about.

I believe what you are asking is how to communicate with a particular chip on a particular microcontroller.

Bob
Hi Bob, Yes this is exactly what I want to do. Sorry for the wrong terminology
 

BobTPH

Joined Jun 5, 2013
8,654
Okay, now that I understand what you are trying to do, I will try to make a guess at what you are asking.

I will assume that you know that you must read and understand the datasheet for the IC, then come up with a list of what queries you want to support. I assume also that you know that you must learn how to use the I2C module on the micro.

So then you would define a software interface to the device, probably as a set of functions, like:

Initialization
Setting operational modes
Reading various data
Sending data if that is necessary
Whatever else it might do

I would then create prototypes for these and put them in a .h file. Naming the functions is important, you should use a prefix or suffix for all of them, like BFG_ for “battery fuel gauge.” Do this in whatever style you prefer.

And finally implement the functions in a .c file with the same name as the .h file.

Is this what you are asking?

Bob
 
Last edited:

geekoftheweek

Joined Oct 6, 2013
1,180
Something you may want to look in to... VGA, DVI, and HDMI all have built in I2C that you can hack a cord and use. II have not tried it with Windows, but with Linux it is just like reading and writing to a normal file.
 

Thread Starter

nikolakisant03

Joined Oct 21, 2012
21
Is this what you are asking?

This is exactly what I was asking for!!
I've read the datasheet and I have found the register section (is called gauge commands in my case)
I know how to use the I2C on MCU.

So based on your comments
1. Create a header file and define registers, declare functions, and create structures
2. Create a .c file and define the functions

Tips: Use prefix or suffix for registers/ functions and structures

Is there anything else I need to take care of?
 
Last edited:

nsaspook

Joined Aug 27, 2009
12,746
In general.

One of the first things you need to know or to define is the application API. Is it very a general need for X total hardware device access or narrow, needing access to only a few bytes of vital data. What data, in what format and/or functions does the application need from the device for the application functionality. The application functionality might only be one Battery Low indicator from a gpio pin or it could be a full suite of battery operational parameters with real-time logging. This defines what the driver/library does at a some level of detail. Does your 'driver' need access to every possible command and data combination possible in a memory buffer/function set or does it only need to implement a minimal command set and data access service to decide if one LED is on or not.

What's the needed refresh rate (1ms, 100ms, 1s) of data? This will greatly affect how you write X driver. Will it be synchronous so that every request for data needs to send a specific hardware device sequence just for that command or do you refresh data asynchronously from a background process updating a buffer of device data isolated from each specific application command? The physical I2C connections/initialization to the hardware device won't change but the data structures and functions needed to support X API will, depending on the exact needs.
 
Last edited:

Picbuster

Joined Dec 2, 2013
1,047
Hi everyone,

I want to try to write a driver for this battery fuel gauge. 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
Before you start to write a driver find an answer to the next questions;
low level: how to wire up
level one: signal levels
level3: protocol used

What type of ic do I want to connect to read, write only or both.
What data do you want from the chip and what do you want to send to the chip.
What do you have to send to the chip to make to do what you want.

Pay attention to error conditions when occur ( all levels) and report when needed.

When finished with above you can start connecting and programming.

that's all.
Picbuster
 

MrSoftware

Joined Oct 29, 2013
2,167
The Nordic boards have lots of driver support. The Keil uVision compiler is easier to use than Eclipse, but it's expensive if your binary gets larger than their "free" cutoff. Are you going to be using one of their softdevices, or run 100% your own code? Start by looking through their library of drivers. I believe they have source posted for both drivers that use their softdevice, and for drivers without. Look at what they did as that will be a great guide to get started. You can then write one on your own to test how well you understand. Do a lot of reading, their softdevices often use DMA and other great features, but it can be tricky to follow if you haven't read up on how it works. If you're going to use the built in radios at all, I highly recommend using a softdevice. Writing the your own code to low level control the radio is no minor task.
 
Last edited:
Top