Skip to content

Commit

Permalink
Integrated adc128d818 driver
Browse files Browse the repository at this point in the history
  • Loading branch information
vagrant committed Jan 31, 2025
1 parent e13914b commit 61ac873
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 1 deletion.
52 changes: 52 additions & 0 deletions libraries/ms-drivers/inc/adc128d818.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// adc128d818.h
#pragma once
#include <stdint.h>
#include <stdbool.h>
#include "status.h"
#include "gpio.h"
#include "i2c.h"

// Enum for ADC128D818 status codes
typedef enum {
ADC128D818_STATUS_CODE_OK = 0,
ADC128D818_STATUS_CODE_ERROR,
NUM_ADC128D818_STATUS_CODES,
} Adc128d818StatusCode;

// Callback type for error handling
typedef void (*Adc128d818ErrorHandlerCb)(Adc128d818StatusCode code, void *context);

// Struct for ADC128D818 settings
typedef struct {
I2CPort i2c_port;
uint8_t i2c_address;
GpioAddress alert_pin;
Adc128d818ErrorHandlerCb error_handler;
void *error_context;
} Adc128d818Settings;

// Struct for ADC128D818 storage
typedef struct {
I2CPort i2c_port;
uint8_t i2c_address;
Adc128d818ErrorHandlerCb error_handler;
void *error_context;
} Adc128d818Storage;

// Initializes the ADC128D818 with the given settings
StatusCode adc128d818_init(Adc128d818Storage *storage, const Adc128d818Settings *settings);

// Reads a channel value from the ADC128D818
StatusCode adc128d818_read_channel(Adc128d818Storage *storage, uint8_t channel, uint16_t *reading);

// Sets the configuration register of the ADC128D818
StatusCode adc128d818_set_config(Adc128d818Storage *storage, uint8_t config);

// Sets the conversion rate of the ADC128D818
StatusCode adc128d818_set_conversion_rate(Adc128d818Storage *storage, uint8_t rate);

// Enables or disables a channel on the ADC128D818
StatusCode adc128d818_set_channel_enable(Adc128d818Storage *storage, uint8_t channel, bool enable);

// Start a single-shot conversion
StatusCode adc128d818_start_one_shot(Adc128d818Storage *storage);
40 changes: 40 additions & 0 deletions libraries/ms-drivers/inc/adc128d818_def.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

// I2C Base Address as described in the datasheet (Section 8.5.1.1)
// Pins 7 and 8 are unconnected in schematics, treated as LOW
#define ADC128D818_I2C_BASE_ADDRESS 0x1D

// Register Addresses from Table 3 in Section 8.6
#define ADC128D818_REG_CONFIG 0x00
#define ADC128D818_REG_INT_STATUS 0x01
#define ADC128D818_REG_INT_MASK 0x03
#define ADC128D818_REG_CONV_RATE 0x07
#define ADC128D818_REG_CHANNEL_DISABLE 0x08
#define ADC128D818_REG_ONE_SHOT 0x09
#define ADC128D818_REG_DEEP_SHUTDOWN 0x0A
#define ADC128D818_REG_ADV_CONFIG 0x0B
#define ADC128D818_REG_BUSY_STATUS 0x0C
#define ADC128D818_REG_CHANNEL_READINGS_START 0x20
#define ADC128D818_REG_LIMIT_START 0x2A
#define ADC128D818_REG_MANUFACTURER_ID 0x3E
#define ADC128D818_REG_REVISION_ID 0x3F

// Configuration Register Bits (Table 4)
#define ADC128D818_CONFIG_START 0x01
#define ADC128D818_CONFIG_INT_ENABLE 0x02
#define ADC128D818_CONFIG_INT_CLEAR 0x08
#define ADC128D818_CONFIG_INIT 0x80

// Advanced Configuration Register Bits (Section 8.3.3.1)
#define ADC128D818_ADV_CONFIG_INTERNAL_VREF 0x00
#define ADC128D818_ADV_CONFIG_EXTERNAL_VREF 0x10
#define ADC128D818_ADV_CONFIG_MODE_SINGLE_ENDED 0x00
#define ADC128D818_ADV_CONFIG_MODE_PSEUDO_DIFF 0x02

// Conversion Rate Values (Section 8.6.5)
#define ADC128D818_CONV_RATE_LOW_POWER 0x00
#define ADC128D818_CONV_RATE_CONTINUOUS 0x01

// Default Initialization Values
#define ADC128D818_DEFAULT_CONFIG (ADC128D818_CONFIG_START | ADC128D818_CONFIG_INT_ENABLE)
#define ADC128D818_DEFAULT_ADV_CONFIG (ADC128D818_ADV_CONFIG_EXTERNAL_VREF | ADC128D818_ADV_CONFIG_MODE_SINGLE_ENDED)
86 changes: 86 additions & 0 deletions libraries/ms-drivers/src/adc128d818.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "adc128d818.h"
#include "adc128d818_def.h"
#include "gpio.h"
#include "i2c.h"
#include "status.h"
#include <string.h>

// Helper function to write to a register
static StatusCode prv_write_register(Adc128d818Storage *storage, uint8_t reg, uint8_t value) {
uint8_t data[2] = { reg, value };
return i2c_write(storage->i2c_port, storage->i2c_address, data, sizeof(data));
}

// Helper function to read from a register
static StatusCode prv_read_register(Adc128d818Storage *storage, uint8_t reg, uint8_t *value) {
status_ok_or_return(i2c_write(storage->i2c_port, storage->i2c_address, &reg, 1));
return i2c_read(storage->i2c_port, storage->i2c_address, value, 1);
}

// Initializes the ADC128D818 with the given settings
StatusCode adc128d818_init(Adc128d818Storage *storage, const Adc128d818Settings *settings) {
if (storage == NULL || settings == NULL) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

memset(storage, 0, sizeof(Adc128d818Storage));
storage->i2c_port = settings->i2c_port;
storage->i2c_address = settings->i2c_address;
storage->error_handler = settings->error_handler;
storage->error_context = settings->error_context;

// Set default configuration
status_ok_or_return(prv_write_register(storage, ADC128D818_REG_CONFIG, ADC128D818_DEFAULT_CONFIG));
status_ok_or_return(prv_write_register(storage, ADC128D818_REG_ADV_CONFIG, ADC128D818_DEFAULT_ADV_CONFIG));

return STATUS_CODE_OK;
}

// Reads a channel value from the ADC128D818
StatusCode adc128d818_read_channel(Adc128d818Storage *storage, uint8_t channel, uint16_t *reading) {
if (channel >= 8 || storage == NULL || reading == NULL) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

uint8_t reg = ADC128D818_REG_CHANNEL_READINGS_START + channel;
uint8_t data[2] = { 0 };

status_ok_or_return(i2c_write(storage->i2c_port, storage->i2c_address, &reg, 1));
status_ok_or_return(i2c_read(storage->i2c_port, storage->i2c_address, data, sizeof(data)));

*reading = ((uint16_t)data[0] << 8) | data[1];
return STATUS_CODE_OK;
}

// Sets the configuration register of the ADC128D818
StatusCode adc128d818_set_config(Adc128d818Storage *storage, uint8_t config) {
return prv_write_register(storage, ADC128D818_REG_CONFIG, config);
}

// Sets the conversion rate of the ADC128D818
StatusCode adc128d818_set_conversion_rate(Adc128d818Storage *storage, uint8_t rate) {
return prv_write_register(storage, ADC128D818_REG_CONV_RATE, rate);
}

// Enables or disables a channel on the ADC128D818
StatusCode adc128d818_set_channel_enable(Adc128d818Storage *storage, uint8_t channel, bool enable) {
if (channel >= 8) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

uint8_t reg_value = 0;
status_ok_or_return(prv_read_register(storage, ADC128D818_REG_CHANNEL_DISABLE, &reg_value));

if (enable) {
reg_value &= ~(1 << channel);
} else {
reg_value |= (1 << channel);
}

return prv_write_register(storage, ADC128D818_REG_CHANNEL_DISABLE, reg_value);
}

// Start a single-shot conversion
StatusCode adc128d818_start_one_shot(Adc128d818Storage *storage) {
return prv_write_register(storage, ADC128D818_REG_ONE_SHOT, 1);
}
2 changes: 1 addition & 1 deletion platform/arm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
Import ('FLASH_TYPE', 'BUILD_TYPE')
import ('FLASH_TYPE', 'BUILD_TYPE')

PLATFORM_DIR = os.getcwd()

Expand Down

0 comments on commit 61ac873

Please sign in to comment.