diff --git a/firmware/func_gen_stm32f303re_nucleo/Core/Inc/main.h b/firmware/func_gen_stm32f303re_nucleo/Core/Inc/main.h index 1e29fd3..6187c0f 100644 --- a/firmware/func_gen_stm32f303re_nucleo/Core/Inc/main.h +++ b/firmware/func_gen_stm32f303re_nucleo/Core/Inc/main.h @@ -88,6 +88,10 @@ void Error_Handler(void); #define BTN_CH1_GPIO_Port GPIOA #define LED_CH1_Pin GPIO_PIN_10 #define LED_CH1_GPIO_Port GPIOA +#define BTN_CH2_Pin GPIO_PIN_11 +#define BTN_CH2_GPIO_Port GPIOA +#define LED_CH2_Pin GPIO_PIN_12 +#define LED_CH2_GPIO_Port GPIOA #define TMS_Pin GPIO_PIN_13 #define TMS_GPIO_Port GPIOA #define TCK_Pin GPIO_PIN_14 diff --git a/firmware/func_gen_stm32f303re_nucleo/Core/Src/gpio.c b/firmware/func_gen_stm32f303re_nucleo/Core/Src/gpio.c index 7c48cff..c84e100 100644 --- a/firmware/func_gen_stm32f303re_nucleo/Core/Src/gpio.c +++ b/firmware/func_gen_stm32f303re_nucleo/Core/Src/gpio.c @@ -57,7 +57,7 @@ void MX_GPIO_Init(void) HAL_GPIO_WritePin(GPIOC, ST7565_RST_Pin|ST7565_A0_Pin|ST7565_CS_Pin, GPIO_PIN_RESET); /*Configure GPIO pin Output Level */ - HAL_GPIO_WritePin(LED_CH1_GPIO_Port, LED_CH1_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(GPIOA, LED_CH1_Pin|LED_CH2_Pin, GPIO_PIN_RESET); /*Configure GPIO pin : PtPin */ GPIO_InitStruct.Pin = B1_Pin; @@ -66,9 +66,9 @@ void MX_GPIO_Init(void) HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct); /*Configure GPIO pins : PAPin PAPin PAPin PAPin - PAPin PAPin */ + PAPin PAPin PAPin */ GPIO_InitStruct.Pin = BTN1_Pin|BTN2_Pin|BTN3_Pin|BTN4_Pin - |BTN5_Pin|BTN_CH1_Pin; + |BTN5_Pin|BTN_CH1_Pin|BTN_CH2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -87,12 +87,12 @@ void MX_GPIO_Init(void) GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); - /*Configure GPIO pin : PtPin */ - GPIO_InitStruct.Pin = LED_CH1_Pin; + /*Configure GPIO pins : PAPin PAPin */ + GPIO_InitStruct.Pin = LED_CH1_Pin|LED_CH2_Pin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; - HAL_GPIO_Init(LED_CH1_GPIO_Port, &GPIO_InitStruct); + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } diff --git a/firmware/func_gen_stm32f303re_nucleo/Makefile b/firmware/func_gen_stm32f303re_nucleo/Makefile index ced76c6..a0b9a88 100644 --- a/firmware/func_gen_stm32f303re_nucleo/Makefile +++ b/firmware/func_gen_stm32f303re_nucleo/Makefile @@ -1,5 +1,5 @@ ########################################################################################################################## -# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Mon Jul 31 14:03:39 CEST 2023] +# File automatically-generated by tool: [projectgenerator] version: [3.17.1] date: [Mon Jul 31 16:49:59 CEST 2023] ########################################################################################################################## # ------------------------------------------------ diff --git a/firmware/func_gen_stm32f303re_nucleo/func_gen_stm32f303re_nucleo.ioc b/firmware/func_gen_stm32f303re_nucleo/func_gen_stm32f303re_nucleo.ioc index c0f7b4a..5b57eb2 100644 --- a/firmware/func_gen_stm32f303re_nucleo/func_gen_stm32f303re_nucleo.ioc +++ b/firmware/func_gen_stm32f303re_nucleo/func_gen_stm32f303re_nucleo.ioc @@ -36,25 +36,27 @@ Mcu.Pin20=PC9 Mcu.Pin21=PA8 Mcu.Pin22=PA9 Mcu.Pin23=PA10 -Mcu.Pin24=PA13 -Mcu.Pin25=PA14 -Mcu.Pin26=PC10 -Mcu.Pin27=PC11 -Mcu.Pin28=PC12 -Mcu.Pin29=PB3 +Mcu.Pin24=PA11 +Mcu.Pin25=PA12 +Mcu.Pin26=PA13 +Mcu.Pin27=PA14 +Mcu.Pin28=PC10 +Mcu.Pin29=PC11 Mcu.Pin3=PF0-OSC_IN -Mcu.Pin30=PB6 -Mcu.Pin31=PB7 -Mcu.Pin32=PB9 -Mcu.Pin33=VP_SYS_VS_Systick -Mcu.Pin34=VP_TIM2_VS_ClockSourceINT +Mcu.Pin30=PC12 +Mcu.Pin31=PB3 +Mcu.Pin32=PB6 +Mcu.Pin33=PB7 +Mcu.Pin34=PB9 +Mcu.Pin35=VP_SYS_VS_Systick +Mcu.Pin36=VP_TIM2_VS_ClockSourceINT Mcu.Pin4=PF1-OSC_OUT Mcu.Pin5=PA0 Mcu.Pin6=PA1 Mcu.Pin7=PA2 Mcu.Pin8=PA3 Mcu.Pin9=PA4 -Mcu.PinsNb=35 +Mcu.PinsNb=37 Mcu.ThirdPartyNb=0 Mcu.UserConstants= Mcu.UserName=STM32F303RETx @@ -83,6 +85,14 @@ PA10.GPIOParameters=GPIO_Label PA10.GPIO_Label=LED_CH1 PA10.Locked=true PA10.Signal=GPIO_Output +PA11.GPIOParameters=GPIO_Label +PA11.GPIO_Label=BTN_CH2 +PA11.Locked=true +PA11.Signal=GPIO_Input +PA12.GPIOParameters=GPIO_Label +PA12.GPIO_Label=LED_CH2 +PA12.Locked=true +PA12.Signal=GPIO_Output PA13.GPIOParameters=GPIO_Label PA13.GPIO_Label=TMS PA13.Locked=true diff --git a/firmware/shared_libs/controllers/ctrl_app.c b/firmware/shared_libs/controllers/ctrl_app.c index bcb8ca2..7717a91 100644 --- a/firmware/shared_libs/controllers/ctrl_app.c +++ b/firmware/shared_libs/controllers/ctrl_app.c @@ -35,6 +35,51 @@ static GEN_sig_t signal_gen[CHANNEL_MAX] = { {.type = GEN_PWM_TYPE, .gen = &pwm_gen[2]}, }; +void _signalGenDefaultValues(void) +{ + for (GEN_channel_t channel = 0; channel < CHANNEL_MAX; channel++) + { + switch (signal_gen[channel].type) + { + case GEN_FG_TYPE: + { + GEN_fg_t *gen = signal_gen[channel].gen; + gen->frequency = 1000; + gen->amplitude = 100; + gen->offset = 165; + gen->phase = 0; + gen->wave = GEN_SIN; + gen->enabled = FALSE; + gen->link = 0; + break; + } + case GEN_PWM_TYPE: + { + GEN_pwm_t *gen = signal_gen[channel].gen; + gen->frequency = 1000; + gen->amplitude = 0; + gen->offset = 0; + gen->phase = 0; + gen->duty = 50; + gen->enabled = FALSE; + break; + } + default: + break; + } + } +} + +void _setGenInitailState(GEN_channel_t channel) +{ + setFreq(&signal_gen[channel], channel); + setWave(&signal_gen[channel], channel); + setPhase(&signal_gen[channel], channel); + setAmplitude(&signal_gen[channel], channel); + setEnabled(&signal_gen[channel], channel); + // setOfsset(&signal_gen[channel], channel); +} + void APP_init(APP_data_t *app_data) { _app_data = app_data; @@ -57,9 +102,11 @@ void APP_init(APP_data_t *app_data) _app_data->isButtonChange = 1; _app_data->isButtonBlink = 1; + _signalGenDefaultValues(); CTRL_buttonsInit(); - HAL_GPIO_WritePin(LED_CH1_GPIO_Port, LED_CH1_Pin, GPIO_PIN_SET); gen_init(); + _setGenInitailState(CHANNEL1); + _setGenInitailState(CHANNEL2); } void CTRL_buttonsInit(void) @@ -143,7 +190,7 @@ static void _changeValueFunGen(int8_t dir) return; } gen->frequency = new_freq; - setFreq(&signal_gen[CHANNEL1], CHANNEL1); + setFreq(&signal_gen[_app_data->curr_channel], _app_data->curr_channel); break; } case LAY_AMPL: @@ -155,7 +202,7 @@ static void _changeValueFunGen(int8_t dir) return; } gen->amplitude = new_ampl; - setAmplitude(&signal_gen[CHANNEL1], CHANNEL1); + setAmplitude(&signal_gen[_app_data->curr_channel], _app_data->curr_channel); break; } case LAY_OFFS: @@ -167,7 +214,7 @@ static void _changeValueFunGen(int8_t dir) return; } gen->offset = new_offs; - setOfsset(&signal_gen[CHANNEL1], CHANNEL1); + setOfsset(&signal_gen[_app_data->curr_channel], _app_data->curr_channel); break; } case LAY_PHAS: @@ -179,6 +226,7 @@ static void _changeValueFunGen(int8_t dir) return; } gen->phase = new_phas; + setPhase(&signal_gen[_app_data->curr_channel], _app_data->curr_channel); break; } default: diff --git a/firmware/shared_libs/controllers/ctrl_channel_button.c b/firmware/shared_libs/controllers/ctrl_channel_button.c index d31d446..2f910ec 100644 --- a/firmware/shared_libs/controllers/ctrl_channel_button.c +++ b/firmware/shared_libs/controllers/ctrl_channel_button.c @@ -18,8 +18,9 @@ void CTRL_channelButtonInit(void) channel_buttons[BTN_CH_2].instance = BTN_CH_2; channel_buttons[BTN_CH_2].buttonPressed = CTRL_pushedChanBtnEvent; - channel_buttons[BTN_CH_2].gpio_port = DUMMY_GPIO_Port; - channel_buttons[BTN_CH_2].gpio_pin = DUMMY_GPIO_Pin; + channel_buttons[BTN_CH_2].buttonLongPressed = CTRL_longPushedChanBtnEvent; + channel_buttons[BTN_CH_2].gpio_port = BTN_CH2_GPIO_Port; + channel_buttons[BTN_CH_2].gpio_pin = BTN_CH2_Pin; channel_buttons[BTN_CH_2].pushed_state = GPIO_PIN_RESET; channel_buttons[BTN_CH_3].instance = BTN_CH_3; diff --git a/firmware/shared_libs/controllers/ctrl_generator.c b/firmware/shared_libs/controllers/ctrl_generator.c index a74c82a..451ee49 100644 --- a/firmware/shared_libs/controllers/ctrl_generator.c +++ b/firmware/shared_libs/controllers/ctrl_generator.c @@ -90,7 +90,6 @@ void led_off(GEN_led_t *hled) void gen_init() { ad9833_init(&dds_gen[FG_CHAN1].hdds, &hspi2, DDS1_CS_GPIO_Port, DDS1_CS_Pin); - ad9833_setMode(&dds_gen[FG_CHAN1].hdds, MODE_OFF); ad9833_init(&dds_gen[FG_CHAN2].hdds, &hspi2, DDS2_CS_GPIO_Port, DDS2_CS_Pin); // ad9833_init(&dds_gen[FG_CHAN3].hdds, &hspi2, DDS3_CS_PORT, DDS3_CS_PIN); @@ -99,11 +98,11 @@ void gen_init() // ltc2631_init(&dds_gen[FG_CHAN3].hoffs, &hi2c1, 0x02, LTC2631_8BIT, LTC_REF_2V5); mcp41x_init(&dds_gen[FG_CHAN1].hampl, &hspi2, AMP1_CS_GPIO_Port, AMP1_CS_Pin, MCP41X_10K); - mcp41x_setValue(&dds_gen[FG_CHAN1].hampl, 0); - mcp41x_init(&dds_gen[FG_CHAN1].hampl, &hspi2, AMP2_CS_GPIO_Port, AMP2_CS_Pin, MCP41X_10K); - // mcp41x_init(&dds_gen[FG_CHAN1].hampl, &hspi2, AMP3_CS_PORT, AMP3_CS_PIN, MCP41X_10K); + mcp41x_init(&dds_gen[FG_CHAN2].hampl, &hspi2, AMP2_CS_GPIO_Port, AMP2_CS_Pin, MCP41X_10K); + // mcp41x_init(&dds_gen[FG_CHAN3].hampl, &hspi2, AMP3_CS_PORT, AMP3_CS_PIN, MCP41X_10K); led_init(&dds_gen[FG_CHAN1].hled, LED_CH1_GPIO_Port, LED_CH1_Pin); + led_init(&dds_gen[FG_CHAN2].hled, LED_CH2_GPIO_Port, LED_CH2_Pin); } static void _setAmpliude(mcp41x_handle_t *hampl, uint16_t ampl_x100) @@ -116,25 +115,25 @@ static void _setAmpliude(mcp41x_handle_t *hampl, uint16_t ampl_x100) static void _setOffset(ltc2631_handle_t *hoffs, int16_t offs_x100) { uint32_t value = (offs_x100 + MAX_VOLT_POS) * LTC_REF_2V5; - ULOG_DEBUG("(%d:_setAmplitude) offs_x100: %d, value: %d", __LINE__, offs_x100, value); + ULOG_DEBUG("(%d:_setOffset) offs_x100: %d, value: %d", __LINE__, offs_x100, value); ltc2631_setOutputVoltage_u(hoffs, value); } static void _setFreqDdsGen(ad9833_handle_t *hdds, uint32_t freq) { - ULOG_DEBUG("(%d:_setAmplitude) freq: %d", __LINE__, freq); + ULOG_DEBUG("(%d:_setFreqDdsGen) freq: %d", __LINE__, freq); ad9833_setFrequency(hdds, CHAN_0, freq); } static void _setPhaseDdsGen(ad9833_handle_t *hdds, uint32_t phas) { - ULOG_DEBUG("(%d:_setAmplitude) phase: %d", __LINE__, phas); + ULOG_DEBUG("(%d:_setPhaseDdsGen) phase: %d", __LINE__, phas); ad9833_setPhase(hdds, CHAN_0, phas); } static void _setWaveDdsGen(ad9833_handle_t *hdds, GEN_wave_t wave) { - ULOG_DEBUG("(%d:_setAmplitude) wave: %d", __LINE__, wave); + ULOG_DEBUG("(%d:_setWaveDdsGen) wave: %d", __LINE__, wave); switch (wave) { case GEN_SIN: @@ -155,7 +154,7 @@ static void _setWaveDdsGen(ad9833_handle_t *hdds, GEN_wave_t wave) static void _setEnabledDdsGen(FG_handle_t *hfg, GEN_fg_t *gen) { - ULOG_DEBUG("(%d:_setAmplitude) phase: %d", __LINE__, gen->enabled); + ULOG_DEBUG("(%d:_setEnabledDdsGen) enabled: %d", __LINE__, gen->enabled); switch (gen->enabled) { case FALSE: diff --git a/firmware/shared_libs/drivers/ad9833/ad9833.c b/firmware/shared_libs/drivers/ad9833/ad9833.c index 249f8f7..7ba73bd 100644 --- a/firmware/shared_libs/drivers/ad9833/ad9833.c +++ b/firmware/shared_libs/drivers/ad9833/ad9833.c @@ -4,8 +4,8 @@ #include "ad9833.h" // Convenience calculations -static uint32_t ad9833_calcFreq(float f); // Calculate AD9833 frequency register from a frequency -static uint16_t ad9833_calcPhase(float a); // Calculate AD9833 phase register from phase +// static uint32_t ad9833_calcFreq(float f); // Calculate AD9833 frequency register from a frequency +// static uint16_t ad9833_calcPhase(float a); // Calculate AD9833 phase register from phase static void ad9833_transmit16(ad9833_handle_t *hfg, uint16_t data) { @@ -51,11 +51,10 @@ void ad9833_init(ad9833_handle_t *hfg, SPI_HandleTypeDef *hspi, GPIO_TypeDef *cs ad9833_setFrequency(hfg, CHAN_1, AD_DEFAULT_FREQ); ad9833_setPhase(hfg, CHAN_0, AD_DEFAULT_PHASE); ad9833_setPhase(hfg, CHAN_1, AD_DEFAULT_PHASE); - ad9833_reset(hfg, 0); // full transition - - ad9833_setMode(hfg, MODE_OFF); - ad9833_setActiveChannelFreq(hfg, CHAN_0); ad9833_setActiveChannelPhase(hfg, CHAN_0); + ad9833_setActiveChannelFreq(hfg, CHAN_0); + ad9833_setMode(hfg, MODE_OFF); + ad9833_reset(hfg, 0); // full transition } void ad9833_setActiveChannelFreq(ad9833_handle_t *hfg, AD_channel_t chan) @@ -146,26 +145,35 @@ void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode) ad9833_transmit16(hfg, hfg->_regCtl); } -static uint32_t ad9833_calcFreq(float f) -// Calculate register value for AD9833 frequency register from a frequency +// 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.5f); +// } + +static uint32_t ad9833_calcFreq_uint(uint32_t f) { - return (uint32_t)((f * AD_2POW28 / AD_MCLK) + 0.5f); + return ((f * AD_2POW28 + AD_MCLK_DIV2) / AD_MCLK); // ((n + d/2)/d) } -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.0f * (a / 10) / 45) + 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.0f * (a / 10) / 45) + 0.5f); +// } -void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, float freq) +static uint16_t ad9833_calcPhase_uint(uint16_t p) +{ + return ((p * 4096U + 180) / 360); +} +void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, uint32_t freq) { // PRINT("\nsetFreq CHAN_", chan); uint16_t freq_channel = 0; hfg->_freq[chan] = freq; - hfg->_regFreq[chan] = ad9833_calcFreq(freq); + hfg->_regFreq[chan] = ad9833_calcFreq_uint(freq); // select the address mask @@ -198,7 +206,7 @@ void ad9833_setPhase(ad9833_handle_t *hfg, AD_channel_t chan, uint16_t phase) uint16_t phase_channel = 0; hfg->_phase[chan] = phase; - hfg->_regPhase[chan] = ad9833_calcPhase(phase); + hfg->_regPhase[chan] = ad9833_calcPhase_uint(phase); // select the address mask switch (chan) diff --git a/firmware/shared_libs/drivers/ad9833/ad9833.h b/firmware/shared_libs/drivers/ad9833/ad9833.h index fb465a3..106a0c6 100644 --- a/firmware/shared_libs/drivers/ad9833/ad9833.h +++ b/firmware/shared_libs/drivers/ad9833/ad9833.h @@ -1,8 +1,9 @@ #pragma once -#define AD_DEFAULT_FREQ 1000U ///< Default initialisation frequency (Hz) -#define AD_DEFAULT_PHASE 0 ///< Default initialisation phase angle (degrees) -#define AD_MCLK 25000000UL ///< Clock speed of the AD9833 reference clock in Hz +#define AD_DEFAULT_FREQ 1000U ///< Default initialisation frequency (Hz) +#define AD_DEFAULT_PHASE 0 ///< Default initialisation phase angle (degrees) +#define AD_MCLK 25000000U ///< Clock speed of the AD9833 reference clock in Hz +#define AD_MCLK_DIV2 12500000U ///< Clock speed of the AD9833 reference clock in Hz /** * Channel enumerated type. * @@ -52,5 +53,5 @@ AD_channel_t ad9833_getActiveChannelFreq(ad9833_handle_t *hfg); void ad9833_setActiveChannelPhase(ad9833_handle_t *hfg, AD_channel_t chan); AD_channel_t ad9833_getActiveChannelPhase(ad9833_handle_t *hfg); void ad9833_setMode(ad9833_handle_t *hfg, AD_mode_t mode); -void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, float freq); +void ad9833_setFrequency(ad9833_handle_t *hfg, AD_channel_t chan, uint32_t freq); void ad9833_setPhase(ad9833_handle_t *hfg, AD_channel_t chan, uint16_t phase); \ No newline at end of file diff --git a/firmware/shared_libs/drivers/ad9833/ad9833_def.h b/firmware/shared_libs/drivers/ad9833/ad9833_def.h index 18f722f..85dc202 100644 --- a/firmware/shared_libs/drivers/ad9833/ad9833_def.h +++ b/firmware/shared_libs/drivers/ad9833/ad9833_def.h @@ -41,4 +41,4 @@ #define SEL_PHASE1 (1 << AD_FREQ0 | 1 << AD_FREQ1 | 1 << AD_PHASE) // AD9833 frequency and phase calculation macros -#define AD_2POW28 (1UL << 28) ///< Used when calculating output frequency +#define AD_2POW28 (1ULL << 28) ///< Used when calculating output frequency