Raleigh NC, USA
support@misfittech.net
lab1_dark
Vendor’s Drivers
Home » Uncategorized  »  Vendor’s Drivers

I often get a lot of flack from vendors and other engineers about not using vendor provided drivers. Specifically I view vendor supplied drivers as references on how the peripheral should work. As such I use it as reference to write my own drivers.

So most likely you are in the camp of that is stupid, as the vendor supplied driver are well tested and there is no way your driver can be better. Or your in the camp of amend brother you got that right. Either way read on to understand why I do this.

Requirements and Goals

First step in understand the firmware is to understand the requirements. For myself I view my projects as being like the brakes on your car. That is you would really have a bad day if the anti lock brake controller decided to watchdog reset as you are doing an emergency stop. Therefore my perspective on drivers and firmware is write everything as though it is for the brakes on your car. Sure there are simple projects like blinking the lights on the christmas tree that does not need this level of code, but if you have that level of driver for the tree lights it will not hurt.

Power On Reset State

This next part I learned the painful way. Back in the day I was using an AVR processor, which were great in their time. We used the vendor's drivers and had the application working. We then added a bootloader onto the device and now our code did not run. As it turns out the boot loader used some peripherals (UART in this case) so when our application started the vendor's drivers assumed the UART peripheral registers were in the power on reset state. Since they were not it was not configured properly. This was a great lesson to always reset registers to their power on reset state in drivers. Note that most peripherals now have a reset bit for this very reason.

Note although most peripherals on chips now have the reset bit. Some things like the interrupt system may not be reset when going from bootloader to application.

Data Cache

Another example more recent was where I was using the gigabit MAC ethernet driver from Microchip. I was getting some really weird results, sometimes it would work and others it would not. I reviewed the driver code and realized they were using DMA without flushing and invalidating the data cache. I contacted Microchip and their tech support was great in that they told me "None of our drivers are written to be used with data cache enabled on the chip."

Time outs and Error checking

Still are not convinced consider this snippet of code from a Microchip I2C driver:

 // Wait for control byte to be transferred before initiating repeat start for read              
while((TWIHS0_REGS->TWIHS_SR & (TWIHS_SR_TXCOMP_Msk | TWIHS_SR_TXRDY_Msk)) != 0);
while((TWIHS0_REGS->TWIHS_SR & (TWIHS_SR_TXRDY_Msk)) == 0);

As you can see the while loop have no time outs. As such your code has a possibility of watchdog resetting while waiting for the condition. If you timed out you could record error to help future you.

Interrupts and Other Resources

Another thing you will find in vendor code is that some use interrupts and some don't. The same with DMA. It is a bad day when you call a driver that needs interrupts enabled, when you have interrupts off. Or when you are using a DMA channel and find that the vendor's driver code is also using that DMA channel.

Error Checking

Other things in vendors code you will find is that they do not check for all the error conditions. For example most vendor UART drivers never check or handle framing errors. That is the vendor's drivers most of the time are written to prove that peripheral works, not handle when it does not work. One of the reasons the vendors do not check errors is that they have no framework to handle errors so if you can not do anything with error, why bother checking for it.

This writing code to prove something works is what I call "happy path" code. That is code that works as long as you stay on the happy path. If you get a bit off that path then everything blows up. From my experience most developers only write code for the happy path and hence they are fine with vendor drivers. However I am not a happy path developer.

All the Bugs are Mine

I tell people is that every bug in the product is my bug. This means that every bug in the hardware is my bug, and every bug in the firmware is my bug. If I used a vendor's driver and it has a bug, then that bug is now my bug to fix. Your microprocessor vendor is not going to be on the phone with your customer telling them how they will fix the problem, it is your problem now. As such I would rather take the time to understand the vendor's driver and rewrite so it has time outs, error checking, at the start. In my experience if I do not do it at the start, I will have to do it later when the driver bugs appear.

On the plus side if you stick with a processor for a few years you get library of very very stable and well proven drivers that can be reused on all future projects.

Leave a Reply

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