#include"max22200.hpp"// 1. Implement the SPI interfaceclassMySpi:publicmax22200::SpiInterface<MySpi>{public:voidtransfer(constuint8_t*tx,uint8_t*rx,size_tlen){// Your SPI transfer implementation// Assert CS, transfer data, deassert CS}};// 2. Create instancesMySpispi;max22200::MAX22200driver(spi);// 3. Initializeif(driver.Initialize()==max22200::DriverStatus::OK){// 4. Configure channel 0 (CDR, low-side; user units: mA and ms)max22200::ChannelConfigconfig;config.drive_mode=max22200::DriveMode::CDR;config.side_mode=max22200::SideMode::LOW_SIDE;config.hit_setpoint=500.0f;// 500 mAconfig.hold_setpoint=200.0f;// 200 mAconfig.hit_time_ms=10.0f;// 10 ms// IFS from SetBoardConfig; driver uses STATUS FREQM for hit timeconfig.chop_freq=max22200::ChopFreq::FMAIN_DIV2;config.hit_current_check_enabled=true;// Enable HIT current checkdriver.ConfigureChannel(0,config);driver.EnableChannel(0);}
Step-by-Step Explanation
Step 1: Include the Header
1
#include"max22200.hpp"
This includes the main driver class and all necessary types.
Step 2: Implement the SPI Interface
You need to implement the SpiInterface for your platform. See Platform Integration for detailed examples.
The constructor takes a reference to your SPI interface implementation.
Step 4: Initialize
1
2
3
if(driver.Initialize()==max22200::DriverStatus::OK){// Driver is ready}
Step 5: Configure Channel
1
2
3
4
5
6
7
8
9
10
11
max22200::ChannelConfigconfig;config.drive_mode=max22200::DriveMode::CDR;config.side_mode=max22200::SideMode::LOW_SIDE;config.hit_setpoint=500.0f;// 500 mA (CDR)config.hold_setpoint=200.0f;// 200 mAconfig.hit_time_ms=10.0f;// 10 ms// IFS from SetBoardConfig; driver uses cached STATUS for hit timeconfig.chop_freq=max22200::ChopFreq::FMAIN_DIV2;config.hit_current_check_enabled=true;driver.ConfigureChannel(0,config);
Alternatively, set board config first then use convenience APIs:
1
2
3
4
max22200::BoardConfigboard(30.0f,false);// IFS from RREFdriver.SetBoardConfig(board);driver.SetHitCurrentMa(0,500);// Channel 0, 500 mAdriver.SetHoldCurrentMa(0,200);
#include"max22200.hpp"classMySpi:publicmax22200::SpiInterface<MySpi>{// ... SPI implementation};voidapp_main(){MySpispi;max22200::MAX22200driver(spi);// Initializemax22200::DriverStatusstatus=driver.Initialize();if(status!=max22200::DriverStatus::OK){printf("Initialization failed\n");return;}// Configure channel (user units: mA and ms)max22200::ChannelConfigconfig;config.drive_mode=max22200::DriveMode::CDR;config.side_mode=max22200::SideMode::LOW_SIDE;config.hit_setpoint=500.0f;config.hold_setpoint=200.0f;config.hit_time_ms=10.0f;config.chop_freq=max22200::ChopFreq::FMAIN_DIV2;status=driver.ConfigureChannel(0,config);if(status!=max22200::DriverStatus::OK){printf("Channel configuration failed\n");return;}// Enable channeldriver.EnableChannel(0);// Read configured current in mA (requires SetBoardConfig with IFS)uint32_tcurrent_ma=0;if(driver.GetHitCurrentMa(0,current_ma)==max22200::DriverStatus::OK){printf("Channel 0 hit current: %"PRIu32" mA\n",current_ma);}}
Expected Output
When running this example (with SetBoardConfig set for your IFS), you should see the configured current in mA, for example:
1
Channel 0 hit current: 500 mA
Troubleshooting
If you encounter issues:
Compilation errors: Check that youβve implemented the transfer() method in your SPI interface
Initialization fails: Verify SPI connections and hardware setup
Channel not working: Check channel configuration and enable state