driver: ltc2631

This commit is contained in:
2023-03-26 00:35:58 +01:00
parent 7d30ec0355
commit d387c26596
11 changed files with 362 additions and 31 deletions

View File

@@ -147,13 +147,13 @@ void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode)
static uint32_t ad9833_calcFreq(float f)
// Calculate register value for AD9833 frequency register from a frequency
{
return (uint32_t)((f * AD_2POW28 / AD_MCLK) + 0.5);
return (uint32_t)((f * AD_2POW28 / AD_MCLK) + 0.5f);
}
static uint16_t ad9833_calcPhase(float a)
// Calculate the value for AD9833 phase register from given phase in tenths of a degree
{
return (uint16_t)((512.0 * (a / 10) / 45) + 0.5);
return (uint16_t)((512.0f * (a / 10) / 45) + 0.5f);
}
void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, float freq)

View File

@@ -1,8 +1,6 @@
// Author: https://github.com/MajicDesigns/MD_AD9833
#pragma once
#include "main.h"
/** @}*/
// AD9833 Control Register bit definitions

View File

@@ -0,0 +1,90 @@
#include "main.h"
#include "ltc2631.h"
void ltc2631_transmit(ltc2631_handle_t *hdac, uint8_t *data)
{
HAL_I2C_Master_Transmit(hdac->hi2c, hdac->addr, data, 3, 1);
}
void ltc2631_init(ltc2631_handle_t *hdac, I2C_HandleTypeDef *hi2c, uint8_t addr, ltc2631_res_t res, float ref)
{
hdac->hi2c = hi2c;
hdac->addr = addr;
hdac->resolution = res;
hdac->ref_voltage_f = ref;
hdac->ref_voltage_u = ref * 1000U;
}
void ltc2631_setOutputVoltage_f(ltc2631_handle_t *hdac, float volt)
{
uint16_t value = 0;
uint8_t data[3] = {0};
if (volt > hdac->ref_voltage_f)
{
volt = hdac->ref_voltage_f;
}
if (volt < 0)
{
volt = 0;
}
value = (volt / hdac->ref_voltage_f + 0.005f) * (hdac->resolution - 1);
value = value << (16 - hdac->resolution);
data[0] = LTC_WRTIEUPDATE;
data[1] = (value >> 8);
data[2] = value;
ltc2631_transmit(hdac, data);
}
void ltc2631_setOutputVoltage_u(ltc2631_handle_t *hdac, uint32_t volt_x1000)
{
uint16_t value = 0;
uint8_t data[3] = {0};
if (volt_x1000 > hdac->ref_voltage_u)
{
volt_x1000 = hdac->ref_voltage_u;
}
if (volt_x1000 < 0)
{
volt_x1000 = 0;
}
value = (volt_x1000 * (hdac->resolution - 1)) / hdac->ref_voltage_u;
value = value << (16 - hdac->resolution);
data[0] = LTC_WRTIEUPDATE;
data[1] = (value >> 8);
data[2] = value;
ltc2631_transmit(hdac, data);
}
void ltc2631_setOutputValue(ltc2631_handle_t *hdac, uint16_t value)
{
uint8_t data[3] = {0};
if (value >= (1 << hdac->resolution))
{
value = (1 << hdac->resolution) - 1;
}
value = value << (16 - hdac->resolution);
data[0] = LTC_WRTIEUPDATE;
data[1] = (value >> 8);
data[2] = value;
ltc2631_transmit(hdac, data);
}
void ltc2631_sleep(ltc2631_handle_t *hdac)
{
uint8_t data[3] = {0};
data[0] = LTC_SLEEP;
ltc2631_transmit(hdac, data);
}

View File

@@ -0,0 +1,34 @@
#pragma once
// C3 C2 C1 C0
#define LTC_WRITE 0b00000000 // 0 0 0 0 Write to Input Register
#define LTC_UPDATE 0b00010000 // 0 0 0 1 Update (Power Up) DAC Register
#define LTC_WRTIEUPDATE 0b00110000 // 0 0 1 1 Write to and Update (Power Up) DAC Register
#define LTC_SLEEP 0b01000000 // 0 1 0 0 Power Down
#define LTC_INTERNALREF 0b01100000 // 0 1 1 0 Select Internal Reference
#define LTC_EXTERNALREF 0b01110000 // 0 1 1 1 Select External Reference
#define LTC_REF_2V5 2.5
#define LTC_REF_4V096 4.096
typedef enum
{
LTC2631_8BIT = 8,
LTC2631_10BIT = 10,
LTC2631_12BIT = 12
} ltc2631_res_t;
typedef struct
{
I2C_HandleTypeDef *hi2c;
uint8_t addr;
ltc2631_res_t resolution;
float ref_voltage_f;
uint32_t ref_voltage_u;
} ltc2631_handle_t;
void ltc2631_init(ltc2631_handle_t *hdac, I2C_HandleTypeDef *hi2c, uint8_t addr, ltc2631_res_t res, float ref);
void ltc2631_setOutputVoltage_f(ltc2631_handle_t *hdac, float volt);
void ltc2631_setOutputVoltage_u(ltc2631_handle_t *hdac, uint32_t volt_x1000);
void ltc2631_setOutputValue(ltc2631_handle_t *hdac, uint16_t value);
void ltc2631_sleep(ltc2631_handle_t *hdac);