diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 5221d46..73c7dc3 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -8,6 +8,7 @@ "${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32F3xx/Include", "${workspaceFolder}/Drivers/STM32F3xx_HAL_Driver/Inc", "${workspaceFolder}/app/drivers/ad9833", + "${workspaceFolder}/app/drivers/ad5303", "${workspaceFolder}/app/drivers/hw_button", "${workspaceFolder}/app/drivers/led", "${workspaceFolder}/app/drivers/mcp41x", diff --git a/Makefile b/Makefile index 81b4027..a7385eb 100644 --- a/Makefile +++ b/Makefile @@ -68,6 +68,7 @@ Drivers/STM32F3xx_HAL_Driver/Src/stm32f3xx_hal_uart_ex.c \ Core/Src/system_stm32f3xx.c C_SOURCES += app/drivers/ad9833/ad9833.c +C_SOURCES += app/drivers/ad5303/ad5303.c C_SOURCES += app/drivers/hw_button/hw_button.c C_SOURCES += app/drivers/led/led.c C_SOURCES += app/drivers/mcp41x/mcp41x.c @@ -144,6 +145,7 @@ C_INCLUDES = \ -IDrivers/CMSIS/Include C_INCLUDES += -Iapp/drivers/ad9833 +C_INCLUDES += -Iapp/drivers/ad5303 C_INCLUDES += -Iapp/drivers/hw_button C_INCLUDES += -Iapp/drivers/led C_INCLUDES += -Iapp/drivers/mcp41x diff --git a/app/core/app.c b/app/core/app.c index fefbf11..a9dfdc7 100644 --- a/app/core/app.c +++ b/app/core/app.c @@ -9,6 +9,7 @@ #include "mcu_cs.h" #include "mcp41x.h" #include "ad9833.h" +#include "ad5303.h" #define MAX_VOLT 330 typedef enum @@ -32,11 +33,15 @@ typedef enum } wave_t; st7565_handle_t hst7565; +GFX_display_t disp; + mcp41x_handle_t hmcp41; MCU_cs_t hmcp41_cs; ad9833_handle_t had9833; MCU_cs_t had9833_cs; -GFX_display_t disp; +ad5303_handle_t hdac; +MCU_cs_t hdac_cs; + uint32_t last_tick; option_t option; @@ -70,6 +75,9 @@ void setup(void) mcu_cs_init(&had9833_cs, DDS1_CS_GPIO_Port, DDS1_CS_Pin, GPIO_PIN_SET); ad9833_init(&had9833, &hspi2, &had9833_cs.super); + mcu_cs_init(&hdac_cs, OFFS1_CS_GPIO_Port, OFFS1_CS_Pin, GPIO_PIN_SET); + ad5303_init(&hdac, &hspi2, &hdac_cs.super, AD_8BIT); + bottomButtonInit(); DISP_clearScreen(&disp); @@ -307,11 +315,13 @@ void changeValue(int8_t inc) case OFFS: { var[OFFS] = (var[OFFS] + inc) & UINT8_MAX; + ad5303_set_value(&hdac, var[OFFS], AD_DAC1); break; } case ZERO: { var[ZERO] = (var[ZERO] + inc) & UINT8_MAX; + ad5303_set_value(&hdac, var[ZERO], AD_DAC2); break; } default: diff --git a/app/drivers/ad5303/ad5303.c b/app/drivers/ad5303/ad5303.c new file mode 100644 index 0000000..a8330b1 --- /dev/null +++ b/app/drivers/ad5303/ad5303.c @@ -0,0 +1,81 @@ +#include "ad5303.h" + +void ad5303_init(ad5303_handle_t *hdac, SPI_HandleTypeDef *hspi, cs_handle_t *hcs, ad5303_res_t res) +{ + hdac->hspi = hspi; + hdac->hcs = hcs; + hdac->res = res; + + ad5303_gain(hdac, AD_GAIN_X1); + ad5303_mode(hdac, AD_NORAMAL); + ad5303_set_value(hdac, 0, AD_DAC1); + ad5303_set_value(hdac, 0, AD_DAC2); +} + +void ad5303_set_value(ad5303_handle_t *hdac, uint16_t value, ad5303_dac_t dac) +{ + uint16_t data; + uint8_t data8; + switch (dac) + { + case AD_DAC1: + hdac->_regCtl &= ~(1 << 15); + break; + case AD_DAC2: + hdac->_regCtl |= (1 << 15); + break; + + default: + break; + } + + data = hdac->_regCtl | (value << hdac->res); + + hdac->hcs->cs_on(hdac->hcs); + + data8 = (uint8_t)((data >> 8) & 0x00FF); + HAL_SPI_Transmit(hdac->hspi, &data8, 1, HAL_MAX_DELAY); + data8 = (uint8_t)(data & 0x00FF); + HAL_SPI_Transmit(hdac->hspi, &data8, 1, HAL_MAX_DELAY); + + hdac->hcs->cs_off(hdac->hcs); +} + +void ad5303_mode(ad5303_handle_t *hdac, ad5303_mode_t mode) +{ + switch (mode) + { + case AD_NORAMAL: + hdac->_regCtl &= ~(11 << 12); + break; + case AD_LOWZ: + hdac->_regCtl |= (1 << 12); + hdac->_regCtl &= ~(1 << 13); + break; + case AD_HIGHZ: + hdac->_regCtl &= ~(1 << 12); + hdac->_regCtl |= (1 << 13); + break; + case AD_OFF: + hdac->_regCtl |= (11 << 12); + break; + + default: + break; + } +} + +void ad5303_gain(ad5303_handle_t *hdac, ad5303_gain_t gain) +{ + switch (gain) + { + case AD_GAIN_X1: + hdac->_regCtl &= ~(1 << 14); + break; + case AD_GAIN_X2: + hdac->_regCtl |= (1 << 14); + break; + default: + break; + } +} diff --git a/app/drivers/ad5303/ad5303.h b/app/drivers/ad5303/ad5303.h new file mode 100644 index 0000000..8593d95 --- /dev/null +++ b/app/drivers/ad5303/ad5303.h @@ -0,0 +1,44 @@ +#pragma once +#include "inttypes.h" +#include "spi_cs_if.h" +#include "spi.h" + +typedef enum +{ + AD_8BIT = 4, + AD_10BIT = 2, + AD_12BIT = 0, +} ad5303_res_t; + +typedef enum +{ + AD_DAC1, + AD_DAC2, +} ad5303_dac_t; + +typedef enum +{ + AD_GAIN_X1, + AD_GAIN_X2, +} ad5303_gain_t; + +typedef enum +{ + AD_NORAMAL, + AD_LOWZ, + AD_HIGHZ, + AD_OFF +} ad5303_mode_t; + +typedef struct +{ + uint16_t _regCtl; + SPI_HandleTypeDef *hspi; + cs_handle_t *hcs; + ad5303_res_t res; +} ad5303_handle_t; + +void ad5303_init(ad5303_handle_t *hdac, SPI_HandleTypeDef *hspi, cs_handle_t *hcs, ad5303_res_t res); +void ad5303_set_value(ad5303_handle_t *hdac, uint16_t value, ad5303_dac_t dac); +void ad5303_mode(ad5303_handle_t *hdac, ad5303_mode_t mode); +void ad5303_gain(ad5303_handle_t *hdac, ad5303_gain_t gain);