Drv8301.c quality

Hi,

I’m going through the driver code in drv8301.c and I’m noticing rather serious bugs. I can’t help but thinking this code was developed in India: Copious amount of unnecessary use of the article ‘the’ in comments, useless comments explaining the C statement below but not the intend, and unawareness of when and when not to escape line endings:

drvDataNew = Spi_8301_Vars->Ctrl_Reg_2.OCTW_SET |
Spi_8301_Vars->Ctrl_Reg_2.GAIN |
Spi_8301_Vars->Ctrl_Reg_2.DC_CAL_CH1p2 |
Spi_8301_Vars->Ctrl_Reg_2.OC_TOFF;

But what is far worse, is that it is largely untested code. This function cannot work:
DRV8301_VdsLevel_e DRV8301_getOcLevel(DRV8301_Handle handle)
{
uint16_t data;

// read data
data = DRV8301_readSpi(handle,DRV8301_RegName_Control_1);

// clear the bits
data &= (~DRV8301_CTRL1_OC_ADJ_SET_BITS);

return((DRV8301_VdsLevel_e)data);
} // end of DRV8301_getOcLevel() function

It clears the bits requested, and it even says so in the comments! Its clearly a copy-paste error from the setter functions and obviously never tested.

These functions show the same defect:
DRV8301_OcMode_e DRV8301_getOcMode(DRV8301_Handle handle)
DRV8301_OcOffTimeMode_e DRV8301_getOcOffTimeMode(DRV8301_Handle handle)
DRV8301_OcTwMode_e DRV8301_getOcTwMode(DRV8301_Handle handle)
DRV8301_PeakCurrent_e DRV8301_getPeakCurrent(DRV8301_Handle handle)
DRV8301_PwmMode_e DRV8301_getPwmMode(DRV8301_Handle handle)
DRV8301_ShuntAmpGain_e DRV8301_getShuntAmpGain(DRV8301_Handle handle)

Does anybody know where this driver originates from? And are there any other externally borrowed parts I should be aware of?

Cheers!

Can you elaborate what exactly is wrong with stated lines above?

The functions mentioned are defective; They clear the bitfield requested instead of the complementary bits.

And the drvDataNew assigning statement has unnecessary trailing backslashes, which this forum platform seems to be omitting.

As far as I know, it’s a TI official library. But it’s certainly possible it’s borked. Please submit a pull request with tested changes to https://github.com/madcowswe/ODrive/

Other external portions included ARM CMSIS, FreeRTOS, ST CubeMX generated code, and ST HAL.

Also, you can tell the forum that what you’re posting is “Preformatted code” by selecting the block and then pressing the “Preformatted text (Ctrl+Shift+C)” button.