I welcome you to my next blog as part of Experimenting with Current Sense Amplifiers Design Challenge. In my previous blog post #1, #2, #3 and #4 I described my experiment plans as part of contest, and shown basic usage of MAX40080 Current Sense Amplifier sensor. In blog #5 I briefly announced my Libraries. In this Blog post I will show my C Library which I have created for interfacing MAX40080 sensor using Microcontrollers. While there are Libraries provided by MikroE and Maxim I still decided to make my own library. I created library for learning all details about all features of MAX40080 chip. I will use this library in some of my future experiments.
Github
My Library is available at GitHub. Github contains library itself, some examples and description of the library. There are prepared packages at Release page for multiple platforms containing only required files for deploying.
Supported Platforms
Library is designed as multiplatform. Library itself is implemented in MAX40080.c and MAX40080.h files which does not directly depends on any platform. Next to these files there are PlatformSpecific files. Main PlatformSpecific file is plain template, and you use it when porting library to new platform. It contains 6 functions related to underlaying I2C driver which is used for communicating with MAX40080 sensor. Except template there are bundled prepared platform specific files for following platforms:
- Maxim Integrated MAX32625 MCU and SDK
- Renesas RA (tested on RA2L1 and RA6M4)
- Userspace Linux
MAX32625 is Microcontroller from Maxim which I use for my main experiments. Testing setup was described and shown in Blog #4. Support for Renesas RA family I added because I want to test it with some different MCU, and I am considering using Renesas EK-RA6M4 board in one of my experiments. Finally, there are support for something that I refer “Userspace Linux”. It is designed for use in standard computer C application running on Linux and it utilize I2C driver in operating system for controlling I2C bus. It of course targets applications running on Raspberry Pi and I plan to develop command line utility for controlling sensor without any programming needed based on this Library port.
Getting started with library
If you want to use this library. You need to follow following 6 steps. The same instructions are at Github main page.
- Go to Release page and download ZIP file targeting your Platform or ZIP targeting Generic platform if your platform is not supported.
- Unzip downloaded file and copy all files to folder of your project or IDE
- Add #include "MAX40080.h" at the beginning of your source code file.
- Call MAX40080_Init() function at the beginning of your program.
- If you use different setup than 10mOhm shunt resistor (10mOhm is used at MikroE Click board, MAX40080EVKIT use 50mOhm) and 100kOhm address resistor (100kOhm is used by both MikroE Click Board and MAX40080EVKIT) then open `MAX40080.h` file and edit `MAX40080_I2C_7BIT_ADDRESS` and `MAX40080_SHUNT_RESISOTR_VALUE` constants.
- Use functions starting with `MAX40080_` as needed.
Library basics
Library is written in C and can be used in C and C++ projects. This means that library is fully available via functions and there are no classes. Library is designed for supporting all features of the MAX40080 CSA chip including its advanced features. If you are missing something, just write comment or create issue at Github. Library is designed for very low memory consumption making it suitable for use with tiny microcontrollers. At runtime it uses only few static global variables for storing sensor configuration. In opposition because library is comprehensive it can occupy lot of program memory. It also uses 256 byte large CRC table for fast CRC computations. For reducing program memory occupation you can delete or comment out unused functions from MAX40080.c. Removing CRC table requires some effort but it is also possible. Library is designed to correctly provide and distinguish errors. Every function returns integer value. Zero return value means that everything is ok and non-zero status code means that some error occurred. Error codes provided by library are defined in enum:
typedef enum { MAX40080_Status_Ok = 0, MAX40080_Status_I2CNack = -10, MAX40080_Status_I2CError = -11, MAX40080_Status_I2CTimeout = -12, MAX40080_Status_PacketErrorCheckFailed = -13, MAX40080_Status_NotImplemented = -20, MAX40080_Status_BadArg = -21, MAX40080_Status_InvalidOperation = -22, MAX40080_Status_FifoIsEmpty = -23, } MAX40080_Status;
This approach of reporting allows you to implement very robust application. You can for example implement retry logic when error like MAX40080_Status_PacketErrorCheckFailed occur making very reliable applications.
Every library function, constant, variable, and so on is prefixed MAX40080_ . Similarly functions implementing control of underlaying I2C driver are prefixed MAX40080_PlatformSpecifc_ . You should not call PlatformSpecific functions directly. They are called from the library. Library manage internal state of sensor and bypassing library may tend to inconsistent state of library and sensor. As a consequence this can lead to further errors, so use only Library functions without PlatformSpecific in name.
Hello Current Sensing World Example
Following code contains simple project reading current from the sensor:
#include "MAX40080.h" int main(void) { MAX40080_Status status; status = MAX40080_Init(); // handle error if status is non-zero MAX40080_Configuration config; MAX40080_GetDefaultConfiguration(&config); config.operatingMode = MAX40080_OperationMode_Active; status = MAX40080_SetConfiguration(&config); // handle error if status is non-zero while (1) { float current; status = MAX40080_ReadCurrent(¤t); if (status && status != MAX40080_Status_FifoIsEmpty) { // handle error continue; } if (status == MAX40080_Status_FifoIsEmpty) { continue; } // print current over UART } }
This example is referred as 01_read_current_continous on the github. For MAX32625 MCU is fully implemented (including comments in code above) in this file and for Renesas EK-RA6M4 Kit it is implemented here.
Github repository contains several other examples. All of them are linked in the table at homepage including links to descriptions and folders with source codes.
Summary and future plans
In this blog I described my C Library for MAX40080 sensor. In the next blog I will similarly describe my Python library. Half of the experimenting period is over. For my future projects with MAX40080 sensor I ordered PCBs at OSHPark. They were shipped and they are on the way. I expect them in about week. In the meantime, I want to improve Libraries, experiment with some other features of MAX40080 CSA sensor and make more examples. I will describe some of these experiments in future blog posts. The I hope I will receive PCBs and I will be able to do my main experiments described in Blog #1.
Next blog: Blog #7: MAX40080 Python Library