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

6
firmware/.vscode/settings.json vendored Normal file
View File

@@ -0,0 +1,6 @@
{
"files.associations": {
"ad9833_def.h": "c",
"ad9833.h": "c"
}
}

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file i2c.h
* @brief This file contains all the function prototypes for
* the i2c.c file
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __I2C_H__
#define __I2C_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern I2C_HandleTypeDef hi2c1;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_I2C1_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __I2C_H__ */

View File

@@ -0,0 +1,130 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file i2c.c
* @brief This file provides code for the configuration
* of the I2C instances.
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "i2c.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
I2C_HandleTypeDef hi2c1;
/* I2C1 init function */
void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x0000020B;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
void HAL_I2C_MspInit(I2C_HandleTypeDef* i2cHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspInit 0 */
/* USER CODE END I2C1_MspInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_I2C1;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* I2C1 clock enable */
__HAL_RCC_I2C1_CLK_ENABLE();
/* USER CODE BEGIN I2C1_MspInit 1 */
/* USER CODE END I2C1_MspInit 1 */
}
}
void HAL_I2C_MspDeInit(I2C_HandleTypeDef* i2cHandle)
{
if(i2cHandle->Instance==I2C1)
{
/* USER CODE BEGIN I2C1_MspDeInit 0 */
/* USER CODE END I2C1_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_I2C1_CLK_DISABLE();
/**I2C1 GPIO Configuration
PB6 ------> I2C1_SCL
PB7 ------> I2C1_SDA
*/
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
HAL_GPIO_DeInit(GPIOB, GPIO_PIN_7);
/* USER CODE BEGIN I2C1_MspDeInit 1 */
/* USER CODE END I2C1_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */

View File

@@ -18,6 +18,7 @@
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "spi.h"
#include "usart.h"
#include "gpio.h"
@@ -88,6 +89,7 @@ int main(void)
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SPI2_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
@@ -141,8 +143,9 @@ void SystemClock_Config(void)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_I2C1;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_HSI;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();

View File

@@ -1,5 +1,5 @@
##########################################################################################################################
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Mar 25 18:27:37 CET 2023]
# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Sat Mar 25 21:51:05 CET 2023]
##########################################################################################################################
# ------------------------------------------------
@@ -61,13 +61,16 @@ Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_exti.c \
Core/Src/system_stm32f3xx.c \
Core/Src/spi.c \
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi.c \
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c
Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_spi_ex.c \
Core/Src/i2c.c
######################################
# shared libs source
######################################
# ad9833
C_SOURCES += ../shared_libs/drivers/ad9833/ad9833.c
# ltc2631
C_SOURCES += ../shared_libs/drivers/ltc2631/ltc2631.c
# ASM sources
ASM_SOURCES = \
@@ -132,11 +135,13 @@ C_INCLUDES = \
# ad9833 includes
C_INCLUDES += -I../shared_libs/drivers/ad9833
# ltc2631 includes
C_INCLUDES += -I../shared_libs/drivers/ltc2631
# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections
CFLAGS += $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -Wdouble-promotion
ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2

View File

@@ -1,15 +1,19 @@
#MicroXplorer Configuration settings - do not modify
File.Version=6
GPIO.groupedBy=Group By Peripherals
I2C1.I2C_Speed_Mode=I2C_Fast
I2C1.IPParameters=I2C_Speed_Mode,Timing
I2C1.Timing=0x0000020B
KeepUserPlacement=false
Mcu.CPN=STM32F303RET6
Mcu.Family=STM32F3
Mcu.IP0=NVIC
Mcu.IP1=RCC
Mcu.IP2=SPI2
Mcu.IP3=SYS
Mcu.IP4=USART2
Mcu.IPNb=5
Mcu.IP0=I2C1
Mcu.IP1=NVIC
Mcu.IP2=RCC
Mcu.IP3=SPI2
Mcu.IP4=SYS
Mcu.IP5=USART2
Mcu.IPNb=6
Mcu.Name=STM32F303R(D-E)Tx
Mcu.Package=LQFP64
Mcu.Pin0=PC13
@@ -18,7 +22,9 @@ Mcu.Pin10=PB15
Mcu.Pin11=PA13
Mcu.Pin12=PA14
Mcu.Pin13=PB3
Mcu.Pin14=VP_SYS_VS_Systick
Mcu.Pin14=PB6
Mcu.Pin15=PB7
Mcu.Pin16=VP_SYS_VS_Systick
Mcu.Pin2=PC15-OSC32_OUT
Mcu.Pin3=PF0-OSC_IN
Mcu.Pin4=PF1-OSC_OUT
@@ -27,7 +33,7 @@ Mcu.Pin6=PA3
Mcu.Pin7=PA5
Mcu.Pin8=PB13
Mcu.Pin9=PB14
Mcu.PinsNb=15
Mcu.PinsNb=17
Mcu.ThirdPartyNb=0
Mcu.UserConstants=
Mcu.UserName=STM32F303RETx
@@ -87,6 +93,11 @@ PB3.GPIOParameters=GPIO_Label
PB3.GPIO_Label=SWO
PB3.Locked=true
PB3.Signal=SYS_JTDO-TRACESWO
PB6.Locked=true
PB6.Mode=I2C
PB6.Signal=I2C1_SCL
PB7.Mode=I2C
PB7.Signal=I2C1_SDA
PC13.GPIOParameters=GPIO_Label,GPIO_ModeDefaultEXTI
PC13.GPIO_Label=B1 [Blue PushButton]
PC13.GPIO_ModeDefaultEXTI=GPIO_MODE_IT_FALLING
@@ -131,7 +142,7 @@ ProjectManager.StackSize=0x400
ProjectManager.TargetToolchain=Makefile
ProjectManager.ToolChainLocation=
ProjectManager.UnderRoot=false
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART2_UART_Init-USART2-false-HAL-true
ProjectManager.functionlistsort=1-SystemClock_Config-RCC-false-HAL-false,2-MX_GPIO_Init-GPIO-false-HAL-true,3-MX_USART2_UART_Init-USART2-false-HAL-true,4-MX_SPI2_Init-SPI2-false-HAL-true
RCC.ADC12outputFreq_Value=72000000
RCC.ADC34outputFreq_Value=72000000
RCC.AHBFreq_Value=72000000

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);