From 81106742dd1215a25995a356c3d137c5f9c683bf Mon Sep 17 00:00:00 2001 From: Guriy Date: Fri, 3 Jul 2026 18:05:11 +0300 Subject: [PATCH] Refactor split main.c --- .gitignore | 2 +- Inc/app_state.h | 34 ++ Inc/laser_hw.h | 13 + Inc/modulation.h | 10 + Inc/protocol.h | 13 + Inc/storage.h | 10 + Inc/temp_control.h | 9 + Makefile | 5 + Src/laser_hw.c | 261 ++++++++++++ Src/main.c | 994 +-------------------------------------------- Src/modulation.c | 298 ++++++++++++++ Src/protocol.c | 256 ++++++++++++ Src/storage.c | 99 +++++ Src/temp_control.c | 100 +++++ 14 files changed, 1117 insertions(+), 987 deletions(-) create mode 100644 Inc/app_state.h create mode 100644 Inc/laser_hw.h create mode 100644 Inc/modulation.h create mode 100644 Inc/protocol.h create mode 100644 Inc/storage.h create mode 100644 Inc/temp_control.h create mode 100644 Src/laser_hw.c create mode 100644 Src/modulation.c create mode 100644 Src/protocol.c create mode 100644 Src/storage.c create mode 100644 Src/temp_control.c diff --git a/.gitignore b/.gitignore index 8b13789..378eac2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ - +build diff --git a/Inc/app_state.h b/Inc/app_state.h new file mode 100644 index 0000000..9dfc800 --- /dev/null +++ b/Inc/app_state.h @@ -0,0 +1,34 @@ +#ifndef __APP_STATE_H +#define __APP_STATE_H + +#include "main.h" +#include "fatfs.h" /*для типа FRESULT*/ + +/* HAL-хендлы (определены в main.c) */ +extern ADC_HandleTypeDef hadc1; +extern ADC_HandleTypeDef hadc3; +extern SD_HandleTypeDef hsd1; +extern TIM_HandleTypeDef htim4; +extern TIM_HandleTypeDef htim8; +extern TIM_HandleTypeDef htim10; +extern TIM_HandleTypeDef htim11; +extern UART_HandleTypeDef huart8; + +/* Разделяемое состояние приложения (определено в main.c, секция USER CODE PV) */ +extern uint32_t TO6, TO6_before, TO6_stop, TO6_uart, SD_SEEK, SD_SLIDE, temp32, + TO7, TO7_before, TO7_PID, TO10, TO10_counter, TIM10_period; +extern uint8_t uart_buf, CPU_state, CPU_state_old, UART_transmission_request, + State_Data[2], UART_DATA[DL_8], flg_tmt, u_tx_flg, u_rx_flg, TIM10_coflag; +extern uint16_t UART_rec_incr, UART_header, CS_result, temp16, + Long_Data[DL_16], COMMAND[CL_16]; +extern FRESULT fresult; +extern int test; +extern unsigned long fgoto, sizeoffile; + +extern LDx_SetupTypeDef LD1_curr_setup, LD2_curr_setup, LD1_def_setup, LD2_def_setup; +extern Work_SetupTypeDef Curr_setup, Def_setup; +extern LDx_ParamTypeDef LD1_param, LD2_param; +extern LD_Blinker_StateTypeDef LD_blinker; +extern task_t task; + +#endif /* __APP_STATE_H */ \ No newline at end of file diff --git a/Inc/laser_hw.h b/Inc/laser_hw.h new file mode 100644 index 0000000..25741ca --- /dev/null +++ b/Inc/laser_hw.h @@ -0,0 +1,13 @@ +#ifndef __LASER_HW_H +#define __LASER_HW_H + +#include "main.h" + +/* Set_LTEC() объявлена в main.h (USER CODE EFP) — оставляем там, + чтобы её видели и main.c, и it.c, и protocol.c без правок. */ + +uint16_t MPhD_T(uint8_t num); +uint16_t Get_ADC(uint8_t num); +void OUT_trigger(uint8_t out_n); + +#endif /* __LASER_HW_H */ diff --git a/Inc/modulation.h b/Inc/modulation.h new file mode 100644 index 0000000..1bbcb4e --- /dev/null +++ b/Inc/modulation.h @@ -0,0 +1,10 @@ +#ifndef __MODULATION_H +#define __MODULATION_H + +#include "main.h" + +void Stop_TIM10(void); +void Sweep_RunCurrent1(void); /* тело case TT_CHANGE_CURR_1 */ +void Blink_RunCurrent2(void); /* тело case TT_CHANGE_CURR_2 */ + +#endif /* __MODULATION_H */ diff --git a/Inc/protocol.h b/Inc/protocol.h new file mode 100644 index 0000000..86fc4e1 --- /dev/null +++ b/Inc/protocol.h @@ -0,0 +1,13 @@ +#ifndef __PROTOCOL_H +#define __PROTOCOL_H + +#include "main.h" + +void Decode_uart(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup); +void Decode_task(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup); +uint8_t CheckChecksum(uint16_t *pbuff); +uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len); +void USART_TX(uint8_t* dt, uint16_t sz); +void USART_TX_DMA(uint16_t sz); + +#endif /* __PROTOCOL_H */ diff --git a/Inc/storage.h b/Inc/storage.h new file mode 100644 index 0000000..c7658c7 --- /dev/null +++ b/Inc/storage.h @@ -0,0 +1,10 @@ +#ifndef __STORAGE_H +#define __STORAGE_H + +#include "main.h" + +int SD_SAVE(uint16_t *pbuff); +int SD_READ(uint16_t *pbuff); +int SD_REMOVE(void); + +#endif /* __STORAGE_H */ \ No newline at end of file diff --git a/Inc/temp_control.h b/Inc/temp_control.h new file mode 100644 index 0000000..beac6d6 --- /dev/null +++ b/Inc/temp_control.h @@ -0,0 +1,9 @@ +#ifndef __TEMP_CONTROL_H +#define __TEMP_CONTROL_H + +#include "main.h" + +uint16_t PID_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num); +uint16_t Advanced_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num); + +#endif /* __TEMP_CONTROL_H */ diff --git a/Makefile b/Makefile index d287b5a..9453bc8 100644 --- a/Makefile +++ b/Makefile @@ -73,6 +73,11 @@ Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_ll_tim.c \ Drivers/STM32F7xx_HAL_Driver/Src/stm32f7xx_ll_usart.c \ Src/system_stm32f7xx.c \ Src/File_Handling.c \ +Src/storage.c \ +Src/protocol.c \ +Src/temp_control.c \ +Src/laser_hw.c \ +Src/modulation.c \ Middlewares/Third_Party/FatFs/src/diskio.c \ Middlewares/Third_Party/FatFs/src/ff.c \ Middlewares/Third_Party/FatFs/src/ff_gen_drv.c \ diff --git a/Src/laser_hw.c b/Src/laser_hw.c new file mode 100644 index 0000000..58e79fa --- /dev/null +++ b/Src/laser_hw.c @@ -0,0 +1,261 @@ +#include "laser_hw.h" +#include "app_state.h" /* hadc1, hadc3 — нужны для Get_ADC */ + +/* --- Set_LTEC, MPhD_T, Get_ADC, OUT_trigger из main.c --- */ +void OUT_trigger(uint8_t out_n) +{ + switch (out_n) + { + case 0: + HAL_GPIO_WritePin(OUT_0_GPIO_Port, OUT_0_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_0_GPIO_Port, OUT_0_Pin, GPIO_PIN_RESET); + break; + + case 1: + HAL_GPIO_WritePin(OUT_1_GPIO_Port, OUT_1_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_1_GPIO_Port, OUT_1_Pin, GPIO_PIN_RESET); + break; + + case 2: + HAL_GPIO_WritePin(OUT_2_GPIO_Port, OUT_2_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_2_GPIO_Port, OUT_2_Pin, GPIO_PIN_RESET); + break; + + case 3: + HAL_GPIO_WritePin(OUT_3_GPIO_Port, OUT_3_Pin, GPIO_PIN_SET); + + HAL_GPIO_WritePin(OUT_3_GPIO_Port, OUT_3_Pin, GPIO_PIN_RESET); + break; + + case 4: + HAL_GPIO_WritePin(OUT_4_GPIO_Port, OUT_4_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_4_GPIO_Port, OUT_4_Pin, GPIO_PIN_RESET); + break; + + case 5: + HAL_GPIO_WritePin(OUT_5_GPIO_Port, OUT_5_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_5_GPIO_Port, OUT_5_Pin, GPIO_PIN_RESET); + break; + + case 6: + HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_RESET); + break; + + case 7: + HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_RESET); + break; + + case 8: + HAL_GPIO_WritePin(OUT_8_GPIO_Port, OUT_8_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_8_GPIO_Port, OUT_8_Pin, GPIO_PIN_RESET); + break; + + case 9: + HAL_GPIO_WritePin(OUT_9_GPIO_Port, OUT_9_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(OUT_9_GPIO_Port, OUT_9_Pin, GPIO_PIN_RESET); + break; + } +} + +void Set_LTEC(uint8_t num, uint16_t DATA) +{ + uint32_t tmp32; + + switch (num) + { + case 1: + HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_RESET);//Start operation with LDAC1 + //tmp32=0; + //while(tmp32<500){tmp32++;} + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_TXE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + LL_SPI_TransmitData16(SPI2, DATA);//Transmit word to Laser1 DAC + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_RXNE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + (void) SPI2->DR; + break; + case 2: + //HAL_GPIO_TogglePin(OUT_11_GPIO_Port, OUT_11_Pin); //for debug purposes + HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_RESET);//Start operation with LDAC2 + //tmp32=0; + //while(tmp32<500){tmp32++;} + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_TXE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + LL_SPI_TransmitData16(SPI6, DATA);//Transmit word to Laser1 DAC + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_RXNE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + (void) SPI6->DR; + break; + case 3: + HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_RESET);//Start operation with TECDAC1 + //tmp32=0; + //while(tmp32<500){tmp32++;} + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_TXE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + LL_SPI_TransmitData16(SPI2, DATA);//Transmit word to Laser1 DAC + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_RXNE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + (void) SPI2->DR; + break; + case 4: + HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_RESET);//Start operation with TECDAC2 + //tmp32=0; + //while(tmp32<500){tmp32++;} + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_TXE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + LL_SPI_TransmitData16(SPI6, DATA);//Transmit word to Laser1 DAC + tmp32 = 0; + while((!LL_SPI_IsActiveFlag_RXNE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. + (void) SPI6->DR; + break; + } + HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_SET);//End operation with LDAC1 + HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_SET);//End operation with LDAC2 + HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_SET);//End operation with TEC1 + HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_SET);//End operation with TEC2 +} +uint16_t MPhD_T(uint8_t num) +{ + uint16_t P; + uint32_t tmp32; + HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_RESET);//Prepare conversion + HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_RESET);//Prepare conversion + tmp32=0; + while(tmp32<500){tmp32++;} + HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_SET);//Stop acqusition & start conversion + HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_SET);//Stop acqusition & start conversion + tmp32=0; + while(tmp32<500){tmp32++;} + if (num==1)//MPD1 + { + HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_RESET); + tmp32=0; + while(tmp32<500){tmp32++;} + //LL_SPI_TransmitData16(SPI4, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... + LL_SPI_Enable(SPI4);//Enable SPI for MPhD1 ADC + tmp32 = 0; + while(((!LL_SPI_IsActiveFlag_RXNE(SPI4))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. + LL_SPI_Disable(SPI4);//Enable SPI for MPhD1 ADC + while(tmp32<500){tmp32++;} + //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); + HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET); + P = LL_SPI_ReceiveData16(SPI4); + } + else if (num==2)//MPD2 + { + HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_RESET); + tmp32=0; + while(tmp32<500){tmp32++;} + //LL_SPI_TransmitData16(SPI5, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... + LL_SPI_Enable(SPI5);//Enable SPI for MPhD2 ADC + tmp32 = 0; + while(((!LL_SPI_IsActiveFlag_RXNE(SPI5))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. + LL_SPI_Disable(SPI5);//Enable SPI for MPhD2 ADC + while(tmp32<500){tmp32++;} + //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); + HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET); + P = LL_SPI_ReceiveData16(SPI5); + } + else if (num==3)//ThrLD1 + { + HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_RESET); + tmp32=0; + while(tmp32<500){tmp32++;} + //LL_SPI_TransmitData16(SPI4, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... + LL_SPI_Enable(SPI4);//Enable SPI for ThrLD1 ADC + tmp32 = 0; + while(((!LL_SPI_IsActiveFlag_RXNE(SPI4))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. + LL_SPI_Disable(SPI4);//Enable SPI for ThrLD1 ADC + while(tmp32<500){tmp32++;} + //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); + HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET); + P = LL_SPI_ReceiveData16(SPI4); + } + else if (num==4)//ThrLD2 + { + HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_RESET); + tmp32=0; + while(tmp32<500){tmp32++;} + //LL_SPI_TransmitData16(SPI5, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... + LL_SPI_Enable(SPI5);//Enable SPI for ThrLD2 ADC + tmp32 = 0; + while(((!LL_SPI_IsActiveFlag_RXNE(SPI5))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. + LL_SPI_Disable(SPI5);//Enable SPI for ThrLD2 ADC + while(tmp32<500){tmp32++;} + //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); + HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET); + P = LL_SPI_ReceiveData16(SPI5); + } + /*float I_LD, Ith, I0m, T0m, Inorm, Tnorm1, Tnorm2, P, T_C, A, Pnorm; + + Inorm = (float) (65535) / (float) (100); + Tnorm1 = (float) (65535) / (float) (50); + Tnorm2 = 4; + Pnorm = (float)(65535) / (float)(20); + I0m = 8.1568;//@4 C - lowest temperature of system + T0m = 48.6282; + T_C = (float) (T_LD) / Tnorm1 + Tnorm2; + + Ith = I0m * expf(T_C/T0m); + I_LD = (float) (C_LD) / Inorm; + + if (I_LD > Ith) + { + A = (float) (2.24276128270098e-07) * T_C * T_C * T_C - (float) (4.73392579025590e-05) * T_C * T_C + (float) (0.00157250618257057) * T_C + (float) (0.228565407377466); + P = A * (I_LD - Ith) * Pnorm; + } + else + { + P = 0; + } */ + return P; +} +/*static uint16_t Temp_LD(uint16_t T_LD_before, uint16_t T_LD, uint32_t Timer_before, uint32_t Timer) +{ + uint16_t Result; +// uint8_t randf; + + randf = 0; + for (uint8_t i = 0; i < 32; i++) + { + randf = ((Timer>>i)&0x0001)^randf; + } + + Result = ((float)(T_LD - T_LD_before))*((float)(1-expf(((float)(Timer_before)-(float)(Timer))/((float)(100))))) + T_LD_before + (float)(randf); + + return (uint16_t)(Result); +}*/ +uint16_t Get_ADC(uint8_t num) +{ + uint16_t OUT; + switch (num) + { + case 0: + HAL_ADC_Start(&hadc1); // Power on + break; + case 1: + HAL_ADC_PollForConversion(&hadc1, 100); // Waiting for conversion + OUT = HAL_ADC_GetValue(&hadc1); // Get value adc + break; + case 2: + HAL_ADC_Stop(&hadc1); // Power off + break; + case 3: + HAL_ADC_Start(&hadc3); // Power on + break; + case 4: + HAL_ADC_PollForConversion(&hadc3, 100); // Waiting for conversion + OUT = HAL_ADC_GetValue(&hadc3); // Get value adc + break; + case 5: + HAL_ADC_Stop(&hadc3); // Power off + break; + } + return OUT; +} diff --git a/Src/main.c b/Src/main.c index 4bc97a2..2890ad0 100644 --- a/Src/main.c +++ b/Src/main.c @@ -25,6 +25,12 @@ // #include "math.h" #include "File_Handling.h" #include + #include "app_state.h" + #include "storage.h" + #include "protocol.h" + #include "temp_control.h" + #include "laser_hw.h" + #include "modulation.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -97,23 +103,8 @@ static void MX_TIM11_Init(void); static void MX_TIM4_Init(void); /* USER CODE BEGIN PFP */ static void Init_params(void); -static void Decode_uart(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup); -static void Decode_task(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup); -void Set_LTEC(uint8_t num, uint16_t DATA); -static uint16_t MPhD_T(uint8_t num); -static uint16_t Get_ADC(uint8_t num); -static uint16_t PID_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num); -uint8_t CheckChecksum(uint16_t *pbuff); -uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len); //int SD_Init(void); -int SD_SAVE(uint16_t *pbuff); //uint32_t Get_Length(void); -int SD_READ(uint16_t *pbuff); -int SD_REMOVE(void); -void USART_TX (uint8_t* dt, uint16_t sz); -void USART_TX_DMA (uint16_t sz); -static void Stop_TIM10(); -static void OUT_trigger(uint8_t); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ @@ -129,7 +120,6 @@ int main(void) { /* USER CODE BEGIN 1 */ - HAL_StatusTypeDef st; /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ @@ -408,282 +398,10 @@ int main(void) switch (task.task_type) { case TT_CHANGE_CURR_1: - - - //calculating timer periods for ADC clock and Mach-Zander modulator - //ADC clock - //TIM4 -> ARR = 60; // for 1.5 MHz - //TIM4 -> ARR = 91; // for 1 MHz - //TIM4 -> ARR = 45; // for 2 MHz - - //online calculation for debug purposes: - //manually varying TIM4 -> ARR by debugger while running - //TIM4 -> CCR3 = (TIM4 -> ARR +1)/2 - 1; - - - //Mach-Zander clock (should be half of ADC clock freq) - //TIM11 -> ARR = (TIM4 -> ARR +1)*2 - 1; - //TIM11 -> CCR1 = (TIM11 -> ARR +1)/2 - 1; - - - //for stm_ADC - //TIM4 -- ADC clock. Rising edge -- for SYNC_DETector_ON - //TIM4 -- ADC clock. Falling edge -- for SYNC_DETector_OFF - - //TIM4 freq is equal to TIM11 - //TIM4 phase should be adjustable to get correct sampling moment - - - - Set_LTEC(TT_CHANGE_CURR_2, task.curr); - (void) MPhD_T(TT_CHANGE_TEMP_1); - LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); - (void) MPhD_T(TT_CHANGE_TEMP_2); - LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); - temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); - Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 - temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); - Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 - - // Toggle pin for oscilloscope - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); //start of the whole frequency sweep procedure (start trigger pulse) - //for (uint32_t i = 100000; i; --i){;} - - - st = HAL_TIM_Base_Start_IT(&htim10); - if (st != HAL_OK) - while(1); - - uint16_t step_counter = 0; - uint16_t trigger_counter = 3; - //uint16_t trigger_step = (uint8_t )((task.max_param - task.current_param)/task.delta_param * 10); - uint16_t trigger_step = 50; - - uint16_t task_sheduler = 0; - - - - HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator - HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); //start ADC clock - TIM11 -> CR1 &= ~(1 << 3); //disables one-pulse mode - TIM4 -> CR1 &= ~(1 << 3); //disables one-pulse mode - - - - TIM11 -> CNT = 0; - TIM4 -> CNT = 0; - - HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator - HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); //start ADC clock - //TIM4 -> CNT = 0; - - TIM4 -> CNT = TIM4 -> ARR - 20; // not zero to make phase shift that will be robust to big delay in RF subsystem (up to ~400 ns) - TIM11 -> CNT = 0; - - uint32_t curr_step_N = 0; - uint32_t postAmpl_1_en_curr_step_N = ((task.max_param - task.current_param)/3.0 * 1.0) / task.delta_param; - uint32_t postAmpl_2_en_curr_step_N = ((task.max_param - task.current_param)/3.0 * 2.0) / task.delta_param; - - - while (task.current_param < task.max_param) - { - if (TIM10_coflag) - { - Set_LTEC(TT_CHANGE_CURR_1, task.current_param); - HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_9); //toggle PG9. Change indicates that current step started. - - //TIM11 -> CNT = 0; // to link modulator phase - //TIM4 -> CNT = 0; // to link ADC clock phase - task.current_param += task.delta_param; - TO10 = 0; - TIM10_coflag = 0; - - if (curr_step_N == postAmpl_1_en_curr_step_N){ - HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_SET); - } - if (curr_step_N == postAmpl_2_en_curr_step_N){ - HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_SET);; - } - - - - //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_SET); // set the current step laser current trigger - //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET); -//* - if (step_counter % trigger_step == 0){ //trigger at every 60 step - //OUT_trigger(trigger_counter); - ++trigger_counter; - } - ++step_counter; - - //HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); //stop trigger pulse -//*/ -/* - ++task_sheduler; - if (task_sheduler >= 10){ - task_sheduler = 0; - } - //maintain stable temperature of laser 2 - if (task_sheduler == 0){ - (void) MPhD_T(TT_CHANGE_TEMP_2); - LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); - temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); - Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 - } - //maintain stable temperature of laser 1 - //* - if (task_sheduler == 5){ - (void) MPhD_T(TT_CHANGE_TEMP_1); - LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); - temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); - Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 - } -//*/ - } - } - TIM11 -> DIER |= 1; //enable update interrupt. In this IRQ handler we will set both tims to one-pulse mode. - //TIM11 -> CR1 |= 1 << 3; //sets timer to one-pulse mode. So it will turn off at the next UpdateEvent - //TIM4 -> CR1 |= 1 << 3; //sets timer to one-pulse mode. So it will turn off at the next UpdateEvent - //but one-pulse mode should be disabled - - //HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator - //HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); //start ADC clock - - - - Stop_TIM10(); - HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_RESET); - HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_RESET); - - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); //stop trigger pulse - - - task.current_param = task.min_param; - Set_LTEC(TT_CHANGE_CURR_1, task.current_param); - if (task.tau > 3) - { - TIM10_period = htim10.Init.Period; - htim10.Init.Period = 9999; - TO10_counter = (task.tau - 1) * 100; - } - HAL_TIM_Base_Start_IT(&htim10); + Sweep_RunCurrent1(); break; case TT_CHANGE_CURR_2: - //Blink laser 2 -//* - Set_LTEC(TT_CHANGE_CURR_1, task.curr); - (void) MPhD_T(TT_CHANGE_TEMP_1); - LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); - (void) MPhD_T(TT_CHANGE_TEMP_2); - LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); - temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); - Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 - temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); - Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 - - LD_blinker.task_type = 2; - LD_blinker.state = 0; // 0 -- disabled (do nothing); 1 -- update LD current; 2 -- blinking, LD ON now; 3 -- blinking, LD OFF now - //LD_blinker.param = task.current_param; - LD_blinker.param = 0; - LD_blinker.param = 1000; // LD2 current (in unspecified units) - LD_blinker.signal_port = OUT_9_GPIO_Port; - LD_blinker.signal_pin = OUT_9_Pin; - - TIM8->ARR = 10000; //zero to LD_blinker.param change frequency (also in unspecified units). - //When it is too low -- Desktop app crashes (there is not so much compute sources on MCU to anwser to desktop`s questions) - st = HAL_TIM_Base_Start_IT(&htim8); - if (st != HAL_OK) - while(1); -// */ - - // Toggle pin for oscilloscope - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); - uint32_t i = 10000; while (--i){} - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); - LD_blinker.state = 2; - - st = HAL_TIM_Base_Start_IT(&htim10); - if (st != HAL_OK) - while(1); - while (task.current_param < task.max_param) - { - if (TIM10_coflag) - { - //Set_LTEC(TT_CHANGE_CURR_2, task.current_param); - //LD_blinker.param = task.current_param; - //++LD_blinker.param; - task.current_param += task.delta_param; - TO10 = 0; - TIM10_coflag = 0; - - - } - } - HAL_TIM_Base_Stop(&htim10); - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); - - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); - - HAL_TIM_Base_Stop_IT(&htim8); - TIM8->CNT = 0; - - Stop_TIM10(); - task.current_param = task.min_param; - Set_LTEC(TT_CHANGE_CURR_2, task.current_param); - if (task.tau > 3) - { - TIM10_period = htim10.Init.Period; - htim10.Init.Period = 9999; - TO10_counter = (task.tau - 1) * 100; - } - HAL_TIM_Base_Start_IT(&htim10); - - -//*/ - - /* // Backup - Set_LTEC(TT_CHANGE_CURR_1, task.curr); - (void) MPhD_T(TT_CHANGE_TEMP_1); - LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); - (void) MPhD_T(TT_CHANGE_TEMP_2); - LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); - temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); - Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 - temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); - Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 - - // Toggle pin for oscilloscope - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); - HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); - - st = HAL_TIM_Base_Start_IT(&htim10); - if (st != HAL_OK) - while(1); - while (task.current_param < task.max_param) - { - if (TIM10_coflag) - { - Set_LTEC(TT_CHANGE_CURR_2, task.current_param); - task.current_param += task.delta_param; - TO10 = 0; - TIM10_coflag = 0; - - - } - } - Stop_TIM10(); - task.current_param = task.min_param; - Set_LTEC(TT_CHANGE_CURR_2, task.current_param); - if (task.tau > 3) - { - TIM10_period = htim10.Init.Period; - htim10.Init.Period = 9999; - TO10_counter = (task.tau - 1) * 100; - } - HAL_TIM_Base_Start_IT(&htim10); - */ - - + Blink_RunCurrent2(); break; case TT_CHANGE_TEMP_1: // isn't implemented @@ -2068,590 +1786,11 @@ static void Init_params(void) // test = 12; // } } -static void Decode_uart(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup) -{ -// uint8_t *temp1; - uint16_t *temp2; - - //--------------------------------------------------------------------------------------------------------------------------------------------------------------------- - - - test=0; - if ((HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin) == GPIO_PIN_RESET)&& - (HAL_GPIO_ReadPin(USB_FLAG_GPIO_Port, USB_FLAG_Pin) == GPIO_PIN_SET))//if exist sd && connect usb write comand to c - { - test = Mount_SD("/"); - if (test == 0) //0 - suc - { - //Format_SD(); - test = Remove_File ("COMMAND.TXT"); - test = Create_File("COMMAND.TXT"); // 0 -succ - test = Write_File_byte("COMMAND.TXT", (uint8_t *)Command, CL_8); - test = Update_File_byte("COMMAND.TXT", (uint8_t *)Command, CL_8); - test = Unmount_SD("/"); // 0 - succ - } - } - - temp2 = (uint16_t *)Command; - Curr_setup->WORK_EN = ((uint8_t)((*temp2)>>0))&0x01; - Curr_setup->U5V1_EN = ((uint8_t)((*temp2)>>1))&0x01; - Curr_setup->U5V2_EN = ((uint8_t)((*temp2)>>2))&0x01; - Curr_setup->LD1_EN = ((uint8_t)((*temp2)>>3))&0x01; - Curr_setup->LD2_EN = ((uint8_t)((*temp2)>>4))&0x01; - Curr_setup->REF1_EN = ((uint8_t)((*temp2)>>5))&0x01; - Curr_setup->REF2_EN = ((uint8_t)((*temp2)>>6))&0x01; - Curr_setup->TEC1_EN = ((uint8_t)((*temp2)>>7))&0x01; - Curr_setup->TEC2_EN = ((uint8_t)((*temp2)>>8))&0x01; - Curr_setup->TS1_EN = ((uint8_t)((*temp2)>>9))&0x01; - Curr_setup->TS2_EN = ((uint8_t)((*temp2)>>10))&0x01; - Curr_setup->SD_EN = ((uint8_t)((*temp2)>>11))&0x01; - Curr_setup->PI1_RD = ((uint8_t)((*temp2)>>12))&0x01; - Curr_setup->PI2_RD = ((uint8_t)((*temp2)>>13))&0x01; - - temp2++; - LD1_curr_setup->LD_TEMP = (uint16_t)(*temp2); - temp2++; - LD2_curr_setup->LD_TEMP = (uint16_t)(*temp2); - temp2++; - temp2++; - temp2++; - Curr_setup->AVERAGES = (uint16_t)(*temp2); - temp2++; - LD1_curr_setup->P_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); - temp2++; - LD1_curr_setup->I_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); - temp2++; - LD2_curr_setup->P_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); - temp2++; - LD2_curr_setup->I_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); - temp2++; - Long_Data[13] = (uint16_t)(*temp2);//Message ID - temp2++; - LD1_curr_setup->CURRENT = (uint16_t)(*temp2); - temp2++; - LD2_curr_setup->CURRENT = (uint16_t)(*temp2); - temp2++; - - if (Curr_setup->U5V1_EN) - { - HAL_GPIO_WritePin(EN_5V1_GPIO_Port, EN_5V1_Pin, GPIO_PIN_SET); - } - else - { - HAL_GPIO_WritePin(EN_5V1_GPIO_Port, EN_5V1_Pin, GPIO_PIN_RESET); - } - - if (Curr_setup->U5V2_EN) - { - HAL_GPIO_WritePin(EN_5V2_GPIO_Port, EN_5V2_Pin, GPIO_PIN_SET); - } - else - { - HAL_GPIO_WritePin(EN_5V2_GPIO_Port, EN_5V2_Pin, GPIO_PIN_RESET); - } - - if (Curr_setup->LD1_EN) - { - HAL_GPIO_WritePin(LD1_EN_GPIO_Port, LD1_EN_Pin, GPIO_PIN_SET); - //LL_SPI_Enable(SPI2);//Enable SPI for Laser1 DAC - } - else - { - HAL_GPIO_WritePin(LD1_EN_GPIO_Port, LD1_EN_Pin, GPIO_PIN_RESET); - //LL_SPI_Disable(SPI2);//Disable SPI for Laser1 DAC - } - - if (Curr_setup->LD2_EN) - { - HAL_GPIO_WritePin(LD2_EN_GPIO_Port, LD2_EN_Pin, GPIO_PIN_SET); - //LL_SPI_Enable(SPI6);//Enable SPI for Laser2 DAC - } - else - { - HAL_GPIO_WritePin(LD2_EN_GPIO_Port, LD2_EN_Pin, GPIO_PIN_RESET); - //LL_SPI_Disable(SPI6);//Disable SPI for Laser2 DAC - } - - if (Curr_setup->REF1_EN) - { - HAL_GPIO_WritePin(REF0_EN_GPIO_Port, REF0_EN_Pin, GPIO_PIN_SET); - } - else - { - HAL_GPIO_WritePin(REF0_EN_GPIO_Port, REF0_EN_Pin, GPIO_PIN_RESET); - } - - if (Curr_setup->REF2_EN) - { - HAL_GPIO_WritePin(REF2_ON_GPIO_Port, REF2_ON_Pin, GPIO_PIN_SET); - } - else - { - HAL_GPIO_WritePin(REF2_ON_GPIO_Port, REF2_ON_Pin, GPIO_PIN_RESET); - } - - if ((Curr_setup->TS1_EN)&&(Curr_setup->TEC1_EN)) - { - Set_LTEC(3,32767); - Set_LTEC(3,32767); - HAL_GPIO_WritePin(TEC1_PD_GPIO_Port, TEC1_PD_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(TECEN1_GPIO_Port, TECEN1_Pin, GPIO_PIN_SET); - } - else - { - HAL_GPIO_WritePin(TECEN1_GPIO_Port, TECEN1_Pin, GPIO_PIN_RESET); - HAL_GPIO_WritePin(TEC1_PD_GPIO_Port, TEC1_PD_Pin, GPIO_PIN_RESET); - } - - if ((Curr_setup->TS2_EN)&&(Curr_setup->TEC2_EN)) - { - Set_LTEC(4,32767); - Set_LTEC(4,32767); - HAL_GPIO_WritePin(TEC2_PD_GPIO_Port, TEC2_PD_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(TECEN2_GPIO_Port, TECEN2_Pin, GPIO_PIN_SET); - } - else - { - HAL_GPIO_WritePin(TECEN2_GPIO_Port, TECEN2_Pin, GPIO_PIN_RESET); - HAL_GPIO_WritePin(TEC2_PD_GPIO_Port, TEC2_PD_Pin, GPIO_PIN_RESET); - } - - if (Curr_setup->PI1_RD==0) - { - LD1_curr_setup->P_coef_temp = 10; - LD1_curr_setup->I_coef_temp = 0.01; - } - - if (Curr_setup->PI2_RD==0) - { - LD2_curr_setup->P_coef_temp = 10; - LD2_curr_setup->I_coef_temp = 0.01; - } -} - -static void Decode_task(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup) -{ - uint16_t *temp2; - - temp2 = (uint16_t *)Command; - Curr_setup->WORK_EN = ((uint8_t)((*temp2)>>0))&0x01; - Curr_setup->U5V1_EN = ((uint8_t)((*temp2)>>1))&0x01; - Curr_setup->U5V2_EN = ((uint8_t)((*temp2)>>2))&0x01; - Curr_setup->LD1_EN = ((uint8_t)((*temp2)>>3))&0x01; - Curr_setup->LD2_EN = ((uint8_t)((*temp2)>>4))&0x01; - Curr_setup->REF1_EN = ((uint8_t)((*temp2)>>5))&0x01; - Curr_setup->REF2_EN = ((uint8_t)((*temp2)>>6))&0x01; - Curr_setup->TEC1_EN = ((uint8_t)((*temp2)>>7))&0x01; - Curr_setup->TEC2_EN = ((uint8_t)((*temp2)>>8))&0x01; - Curr_setup->TS1_EN = ((uint8_t)((*temp2)>>9))&0x01; - Curr_setup->TS2_EN = ((uint8_t)((*temp2)>>10))&0x01; - Curr_setup->SD_EN = ((uint8_t)((*temp2)>>11))&0x01; - Curr_setup->PI1_RD = ((uint8_t)((*temp2)>>12))&0x01; - Curr_setup->PI2_RD = ((uint8_t)((*temp2)>>13))&0x01; - - temp2++; - task.task_type = (uint8_t)(*temp2); temp2++; - task.min_param = (float)(*temp2); temp2++; - task.max_param = (float)(*temp2); temp2++; - task.delta_param = (float)(*temp2); temp2++; - task.dt = (float)(*temp2) / 100.0; temp2++; - task.sec_param = (float)(*temp2); temp2++; - task.curr = (float)(*temp2); temp2++; - task.temp = (float)(*temp2); temp2++; - task.tau = (float)(*temp2); temp2++; - task.p_coef_1 = (float)(*temp2) * 256.0; temp2++; - task.i_coef_1 = (float)(*temp2) * 256.0; temp2++; - task.p_coef_2 = (float)(*temp2) * 256.0; temp2++; - task.i_coef_2 = (float)(*temp2) * 256.0; temp2++; - - TO10_counter = task.dt / 10; -} - -void OUT_trigger(uint8_t out_n) -{ - switch (out_n) - { - case 0: - HAL_GPIO_WritePin(OUT_0_GPIO_Port, OUT_0_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_0_GPIO_Port, OUT_0_Pin, GPIO_PIN_RESET); - break; - - case 1: - HAL_GPIO_WritePin(OUT_1_GPIO_Port, OUT_1_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_1_GPIO_Port, OUT_1_Pin, GPIO_PIN_RESET); - break; - - case 2: - HAL_GPIO_WritePin(OUT_2_GPIO_Port, OUT_2_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_2_GPIO_Port, OUT_2_Pin, GPIO_PIN_RESET); - break; - - case 3: - HAL_GPIO_WritePin(OUT_3_GPIO_Port, OUT_3_Pin, GPIO_PIN_SET); - - HAL_GPIO_WritePin(OUT_3_GPIO_Port, OUT_3_Pin, GPIO_PIN_RESET); - break; - - case 4: - HAL_GPIO_WritePin(OUT_4_GPIO_Port, OUT_4_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_4_GPIO_Port, OUT_4_Pin, GPIO_PIN_RESET); - break; - - case 5: - HAL_GPIO_WritePin(OUT_5_GPIO_Port, OUT_5_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_5_GPIO_Port, OUT_5_Pin, GPIO_PIN_RESET); - break; - - case 6: - HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_RESET); - break; - - case 7: - HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_RESET); - break; - - case 8: - HAL_GPIO_WritePin(OUT_8_GPIO_Port, OUT_8_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_8_GPIO_Port, OUT_8_Pin, GPIO_PIN_RESET); - break; - - case 9: - HAL_GPIO_WritePin(OUT_9_GPIO_Port, OUT_9_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(OUT_9_GPIO_Port, OUT_9_Pin, GPIO_PIN_RESET); - break; - } -} - -void Set_LTEC(uint8_t num, uint16_t DATA) -{ - uint32_t tmp32; - - switch (num) - { - case 1: - HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_RESET);//Start operation with LDAC1 - //tmp32=0; - //while(tmp32<500){tmp32++;} - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_TXE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - LL_SPI_TransmitData16(SPI2, DATA);//Transmit word to Laser1 DAC - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_RXNE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - (void) SPI2->DR; - break; - case 2: - //HAL_GPIO_TogglePin(OUT_11_GPIO_Port, OUT_11_Pin); //for debug purposes - HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_RESET);//Start operation with LDAC2 - //tmp32=0; - //while(tmp32<500){tmp32++;} - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_TXE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - LL_SPI_TransmitData16(SPI6, DATA);//Transmit word to Laser1 DAC - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_RXNE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - (void) SPI6->DR; - break; - case 3: - HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_RESET);//Start operation with TECDAC1 - //tmp32=0; - //while(tmp32<500){tmp32++;} - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_TXE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - LL_SPI_TransmitData16(SPI2, DATA);//Transmit word to Laser1 DAC - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_RXNE(SPI2))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - (void) SPI2->DR; - break; - case 4: - HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_RESET);//Start operation with TECDAC2 - //tmp32=0; - //while(tmp32<500){tmp32++;} - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_TXE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - LL_SPI_TransmitData16(SPI6, DATA);//Transmit word to Laser1 DAC - tmp32 = 0; - while((!LL_SPI_IsActiveFlag_RXNE(SPI6))&&(tmp32<=500)) {tmp32++;}//When trans. last data cycle will be end. - (void) SPI6->DR; - break; - } - HAL_GPIO_WritePin(DAC_LD1_CS_GPIO_Port, DAC_LD1_CS_Pin, GPIO_PIN_SET);//End operation with LDAC1 - HAL_GPIO_WritePin(DAC_LD2_CS_GPIO_Port, DAC_LD2_CS_Pin, GPIO_PIN_SET);//End operation with LDAC2 - HAL_GPIO_WritePin(DAC_TEC1_CS_GPIO_Port, DAC_TEC1_CS_Pin, GPIO_PIN_SET);//End operation with TEC1 - HAL_GPIO_WritePin(DAC_TEC2_CS_GPIO_Port, DAC_TEC2_CS_Pin, GPIO_PIN_SET);//End operation with TEC2 -} -static uint16_t MPhD_T(uint8_t num) -{ - uint16_t P; - uint32_t tmp32; - HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_RESET);//Prepare conversion - HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_RESET);//Prepare conversion - tmp32=0; - while(tmp32<500){tmp32++;} - HAL_GPIO_WritePin(SPI4_CNV_GPIO_Port, SPI4_CNV_Pin, GPIO_PIN_SET);//Stop acqusition & start conversion - HAL_GPIO_WritePin(SPI5_CNV_GPIO_Port, SPI5_CNV_Pin, GPIO_PIN_SET);//Stop acqusition & start conversion - tmp32=0; - while(tmp32<500){tmp32++;} - if (num==1)//MPD1 - { - HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_RESET); - tmp32=0; - while(tmp32<500){tmp32++;} - //LL_SPI_TransmitData16(SPI4, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... - LL_SPI_Enable(SPI4);//Enable SPI for MPhD1 ADC - tmp32 = 0; - while(((!LL_SPI_IsActiveFlag_RXNE(SPI4))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. - LL_SPI_Disable(SPI4);//Enable SPI for MPhD1 ADC - while(tmp32<500){tmp32++;} - //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); - HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET); - P = LL_SPI_ReceiveData16(SPI4); - } - else if (num==2)//MPD2 - { - HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_RESET); - tmp32=0; - while(tmp32<500){tmp32++;} - //LL_SPI_TransmitData16(SPI5, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... - LL_SPI_Enable(SPI5);//Enable SPI for MPhD2 ADC - tmp32 = 0; - while(((!LL_SPI_IsActiveFlag_RXNE(SPI5))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. - LL_SPI_Disable(SPI5);//Enable SPI for MPhD2 ADC - while(tmp32<500){tmp32++;} - //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); - HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET); - P = LL_SPI_ReceiveData16(SPI5); - } - else if (num==3)//ThrLD1 - { - HAL_GPIO_WritePin(ADC_MPD1_CS_GPIO_Port, ADC_MPD1_CS_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_RESET); - tmp32=0; - while(tmp32<500){tmp32++;} - //LL_SPI_TransmitData16(SPI4, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... - LL_SPI_Enable(SPI4);//Enable SPI for ThrLD1 ADC - tmp32 = 0; - while(((!LL_SPI_IsActiveFlag_RXNE(SPI4))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. - LL_SPI_Disable(SPI4);//Enable SPI for ThrLD1 ADC - while(tmp32<500){tmp32++;} - //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); - HAL_GPIO_WritePin(ADC_ThrLD1_CS_GPIO_Port, ADC_ThrLD1_CS_Pin, GPIO_PIN_SET); - P = LL_SPI_ReceiveData16(SPI4); - } - else if (num==4)//ThrLD2 - { - HAL_GPIO_WritePin(ADC_MPD2_CS_GPIO_Port, ADC_MPD2_CS_Pin, GPIO_PIN_SET); - HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_RESET); - tmp32=0; - while(tmp32<500){tmp32++;} - //LL_SPI_TransmitData16(SPI5, 0xFFFF);//We must to clock the CLK output for collect RX data. We can do that only by transmitting data... - LL_SPI_Enable(SPI5);//Enable SPI for ThrLD2 ADC - tmp32 = 0; - while(((!LL_SPI_IsActiveFlag_RXNE(SPI5))&&(tmp32<=1000))) {tmp32++;}//When rec. last data cycle will be end. - LL_SPI_Disable(SPI5);//Enable SPI for ThrLD2 ADC - while(tmp32<500){tmp32++;} - //HAL_SPI_Receive(&hspi4, &P[0], 1, 100); - HAL_GPIO_WritePin(ADC_ThrLD2_CS_GPIO_Port, ADC_ThrLD2_CS_Pin, GPIO_PIN_SET); - P = LL_SPI_ReceiveData16(SPI5); - } - /*float I_LD, Ith, I0m, T0m, Inorm, Tnorm1, Tnorm2, P, T_C, A, Pnorm; - - Inorm = (float) (65535) / (float) (100); - Tnorm1 = (float) (65535) / (float) (50); - Tnorm2 = 4; - Pnorm = (float)(65535) / (float)(20); - I0m = 8.1568;//@4 C - lowest temperature of system - T0m = 48.6282; - T_C = (float) (T_LD) / Tnorm1 + Tnorm2; - - Ith = I0m * expf(T_C/T0m); - I_LD = (float) (C_LD) / Inorm; - - if (I_LD > Ith) - { - A = (float) (2.24276128270098e-07) * T_C * T_C * T_C - (float) (4.73392579025590e-05) * T_C * T_C + (float) (0.00157250618257057) * T_C + (float) (0.228565407377466); - P = A * (I_LD - Ith) * Pnorm; - } - else - { - P = 0; - } */ - return P; -} -/*static uint16_t Temp_LD(uint16_t T_LD_before, uint16_t T_LD, uint32_t Timer_before, uint32_t Timer) -{ - uint16_t Result; -// uint8_t randf; - - randf = 0; - for (uint8_t i = 0; i < 32; i++) - { - randf = ((Timer>>i)&0x0001)^randf; - } - - Result = ((float)(T_LD - T_LD_before))*((float)(1-expf(((float)(Timer_before)-(float)(Timer))/((float)(100))))) + T_LD_before + (float)(randf); - - return (uint16_t)(Result); -}*/ -static uint16_t Get_ADC(uint8_t num) -{ - uint16_t OUT; - switch (num) - { - case 0: - HAL_ADC_Start(&hadc1); // Power on - break; - case 1: - HAL_ADC_PollForConversion(&hadc1, 100); // Waiting for conversion - OUT = HAL_ADC_GetValue(&hadc1); // Get value adc - break; - case 2: - HAL_ADC_Stop(&hadc1); // Power off - break; - case 3: - HAL_ADC_Start(&hadc3); // Power on - break; - case 4: - HAL_ADC_PollForConversion(&hadc3, 100); // Waiting for conversion - OUT = HAL_ADC_GetValue(&hadc3); // Get value adc - break; - case 5: - HAL_ADC_Stop(&hadc3); // Power off - break; - } - return OUT; -} - -uint16_t Advanced_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num) -{ - // Main idea: - // I is responsible to maintaining constant temperature difference between laser and room temperature. - // As room temperature can be approximated as constant at current-varying time -- I should be kept constant too. - // As current through laser diode heats it -- we can estimate excessive power on laser diode and trim peltier current according to it. - // So, equation should be look like this: - // x_output = x_output_original + I(laser)*(a + (t - b)c) - // t -- cycle phase - // a,b,c -- constants - // - // How can we control laser diode temperature? - // -- We can set laser to fixed current at the time we need to measure. - // Then we should measure wavelength. - // Calibration sequence: - // 1) n - int e_pid; - float P_coef_current;//, I_coef_current; - float e_integral; - int x_output; - - e_pid = (int) LDx_results->LD_CURR_TEMP - (int) LDx_curr_setup->LD_TEMP; - - e_integral = LDx_results->e_integral; - - if((e_pid < 3000) && (e_pid > - 3000)){ - e_integral += LDx_curr_setup->I_coef_temp * (float)(e_pid) * (float)(TO7 - TO7_PID) / (float) 100;//100 - timer is too fast - } - P_coef_current = LDx_curr_setup->P_coef_temp; - - if (e_integral > 32000){ - e_integral = 32000; - } - else if (e_integral < - 32000){ - e_integral = -32000; - } - LDx_results->e_integral = e_integral; - - x_output = 32768 + P_coef_current * e_pid + (int)e_integral;//32768 - P_coef_current * e_pid - (int)e_integral;// - - if(x_output < 1000){ - x_output = 8800; - } - else if(x_output > 56800){ - x_output = 56800; - } - - if (num==2) - TO7_PID = TO7;//Save current time only on 2nd laser - - return (uint16_t)x_output; -} -uint16_t PID_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num) -{ - int e_pid; - float P_coef_current;//, I_coef_current; - float e_integral; - int x_output; - - e_pid = (int) LDx_results->LD_CURR_TEMP - (int) LDx_curr_setup->LD_TEMP; - - e_integral = LDx_results->e_integral; - - if((e_pid < 3000) && (e_pid > - 3000)){ - e_integral += LDx_curr_setup->I_coef_temp * (float)(e_pid) * (float)(TO7 - TO7_PID) / (float) 100;//100 - timer is too fast - } - P_coef_current = LDx_curr_setup->P_coef_temp; - - if (e_integral > 32000){ - e_integral = 32000; - } - else if (e_integral < - 32000){ - e_integral = -32000; - } - LDx_results->e_integral = e_integral; - - x_output = 32768 + P_coef_current * e_pid + (int)e_integral;//32768 - P_coef_current * e_pid - (int)e_integral;// - - if(x_output < 1000){ - x_output = 8800; - } - else if(x_output > 56800){ - x_output = 56800; - } - - if (num==2) - TO7_PID = TO7;//Save current time only on 2nd laser - - return (uint16_t)x_output; -} -uint8_t CheckChecksum(uint16_t *pbuff) -{ - uint16_t cl_ind; - - switch (UART_header) - { - case 0x7777: - cl_ind = TSK_16 - 2; - break; - case 0x1111: - cl_ind = CL_16 - 2; - break; - default: - return 0; - break; - } - - CS_result = CalculateChecksum(pbuff, cl_ind); - - return ((CS_result == COMMAND[cl_ind]) ? 1 : 0); -} -uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len) -{ - short i; - uint16_t cs = *pbuff; - - for(i = 1; i < len; i++) - { - cs ^= *(pbuff+i); - } - return cs; -} /*int SD_Init(void) { @@ -2679,128 +1818,11 @@ uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len) } }*/ -int SD_SAVE(uint16_t *pbuff) -{ - int test=0; - if (HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin)==GPIO_PIN_RESET) - { - test = Mount_SD("/"); - if (test == 0) //0 - suc - { - //Format_SD(); - test = Update_File_byte("FILE1.TXT", (uint8_t *)pbuff, DL_8); - test = Unmount_SD("/"); // 0 - succ - return test; - } - else - { - return 1; - } - } - else - { - return 1; - } -} - -//uint32_t Get_Length(void) -//{ -// return SD_matr[0][0] + ((uint32_t) (SD_matr[0][1])<<16); -//} - -int SD_READ(uint16_t *pbuff) -{ - int test=0; - if (HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin)==GPIO_PIN_RESET) - { - test = Mount_SD("/"); - if (test == 0) //0 - suc - { - //Format_SD(); - test = Seek_Read_File ("FILE1.TXT", (uint8_t *)pbuff, DL_8, fgoto);//Read next 246 bytes - fgoto+=DL_8; - test = Unmount_SD("/"); // 0 - succ - return test; - } - else - { - return 1; - } - } - else - { - return 1; - } - -/* for (uint16_t j = 0; j < DL_16; j++) - { - *(pbuff+j) = SD_matr[SD_SLIDE][j]; - } - if (SD_SLIDE ARR = 60; // for 1.5 MHz + //TIM4 -> ARR = 91; // for 1 MHz + //TIM4 -> ARR = 45; // for 2 MHz + + //online calculation for debug purposes: + //manually varying TIM4 -> ARR by debugger while running + //TIM4 -> CCR3 = (TIM4 -> ARR +1)/2 - 1; + + + //Mach-Zander clock (should be half of ADC clock freq) + //TIM11 -> ARR = (TIM4 -> ARR +1)*2 - 1; + //TIM11 -> CCR1 = (TIM11 -> ARR +1)/2 - 1; + + + //for stm_ADC + //TIM4 -- ADC clock. Rising edge -- for SYNC_DETector_ON + //TIM4 -- ADC clock. Falling edge -- for SYNC_DETector_OFF + + //TIM4 freq is equal to TIM11 + //TIM4 phase should be adjustable to get correct sampling moment + + + + Set_LTEC(TT_CHANGE_CURR_2, task.curr); + (void) MPhD_T(TT_CHANGE_TEMP_1); + LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); + (void) MPhD_T(TT_CHANGE_TEMP_2); + LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); + temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); + Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 + temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); + Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 + + // Toggle pin for oscilloscope + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); //start of the whole frequency sweep procedure (start trigger pulse) + //for (uint32_t i = 100000; i; --i){;} + + + st = HAL_TIM_Base_Start_IT(&htim10); + if (st != HAL_OK) + while(1); + + uint16_t step_counter = 0; + uint16_t trigger_counter = 3; + //uint16_t trigger_step = (uint8_t )((task.max_param - task.current_param)/task.delta_param * 10); + uint16_t trigger_step = 50; + + uint16_t task_sheduler = 0; + + + + HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator + HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); //start ADC clock + TIM11 -> CR1 &= ~(1 << 3); //disables one-pulse mode + TIM4 -> CR1 &= ~(1 << 3); //disables one-pulse mode + + + + TIM11 -> CNT = 0; + TIM4 -> CNT = 0; + + HAL_TIM_PWM_Start(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator + HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3); //start ADC clock + //TIM4 -> CNT = 0; + + TIM4 -> CNT = TIM4 -> ARR - 20; // not zero to make phase shift that will be robust to big delay in RF subsystem (up to ~400 ns) + TIM11 -> CNT = 0; + + uint32_t curr_step_N = 0; + uint32_t postAmpl_1_en_curr_step_N = ((task.max_param - task.current_param)/3.0 * 1.0) / task.delta_param; + uint32_t postAmpl_2_en_curr_step_N = ((task.max_param - task.current_param)/3.0 * 2.0) / task.delta_param; + + + while (task.current_param < task.max_param) + { + if (TIM10_coflag) + { + Set_LTEC(TT_CHANGE_CURR_1, task.current_param); + HAL_GPIO_TogglePin(GPIOG, GPIO_PIN_9); //toggle PG9. Change indicates that current step started. + + //TIM11 -> CNT = 0; // to link modulator phase + //TIM4 -> CNT = 0; // to link ADC clock phase + task.current_param += task.delta_param; + TO10 = 0; + TIM10_coflag = 0; + + if (curr_step_N == postAmpl_1_en_curr_step_N){ + HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_SET); + } + if (curr_step_N == postAmpl_2_en_curr_step_N){ + HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_SET);; + } + + + + //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_SET); // set the current step laser current trigger + //HAL_GPIO_WritePin(GPIOG, GPIO_PIN_9, GPIO_PIN_RESET); +//* + if (step_counter % trigger_step == 0){ //trigger at every 60 step + //OUT_trigger(trigger_counter); + ++trigger_counter; + } + ++step_counter; + + //HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); //stop trigger pulse +//*/ +/* + ++task_sheduler; + if (task_sheduler >= 10){ + task_sheduler = 0; + } + //maintain stable temperature of laser 2 + if (task_sheduler == 0){ + (void) MPhD_T(TT_CHANGE_TEMP_2); + LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); + temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); + Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 + } + //maintain stable temperature of laser 1 + //* + if (task_sheduler == 5){ + (void) MPhD_T(TT_CHANGE_TEMP_1); + LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); + temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); + Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 + } +//*/ + } + } + TIM11 -> DIER |= 1; //enable update interrupt. In this IRQ handler we will set both tims to one-pulse mode. + //TIM11 -> CR1 |= 1 << 3; //sets timer to one-pulse mode. So it will turn off at the next UpdateEvent + //TIM4 -> CR1 |= 1 << 3; //sets timer to one-pulse mode. So it will turn off at the next UpdateEvent + //but one-pulse mode should be disabled + + //HAL_TIM_PWM_Stop(&htim11, TIM_CHANNEL_1); //start modulating by Mach-Zander modulator + //HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3); //start ADC clock + + + + Stop_TIM10(); + HAL_GPIO_WritePin(OUT_6_GPIO_Port, OUT_6_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(OUT_7_GPIO_Port, OUT_7_Pin, GPIO_PIN_RESET); + + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); //stop trigger pulse + + + task.current_param = task.min_param; + Set_LTEC(TT_CHANGE_CURR_1, task.current_param); + if (task.tau > 3) + { + TIM10_period = htim10.Init.Period; + htim10.Init.Period = 9999; + TO10_counter = (task.tau - 1) * 100; + } + HAL_TIM_Base_Start_IT(&htim10); +} + +void Blink_RunCurrent2(void) +{ + HAL_StatusTypeDef st; + /* --- тело case TT_CHANGE_CURR_2 (без break;) --- */ + //Blink laser 2 +//* + Set_LTEC(TT_CHANGE_CURR_1, task.curr); + (void) MPhD_T(TT_CHANGE_TEMP_1); + LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); + (void) MPhD_T(TT_CHANGE_TEMP_2); + LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); + temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); + Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 + temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); + Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 + + LD_blinker.task_type = 2; + LD_blinker.state = 0; // 0 -- disabled (do nothing); 1 -- update LD current; 2 -- blinking, LD ON now; 3 -- blinking, LD OFF now + //LD_blinker.param = task.current_param; + LD_blinker.param = 0; + LD_blinker.param = 1000; // LD2 current (in unspecified units) + LD_blinker.signal_port = OUT_9_GPIO_Port; + LD_blinker.signal_pin = OUT_9_Pin; + + TIM8->ARR = 10000; //zero to LD_blinker.param change frequency (also in unspecified units). + //When it is too low -- Desktop app crashes (there is not so much compute sources on MCU to anwser to desktop`s questions) + st = HAL_TIM_Base_Start_IT(&htim8); + if (st != HAL_OK) + while(1); +// */ + + // Toggle pin for oscilloscope + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); + uint32_t i = 10000; while (--i){} + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); + LD_blinker.state = 2; + + st = HAL_TIM_Base_Start_IT(&htim10); + if (st != HAL_OK) + while(1); + while (task.current_param < task.max_param) + { + if (TIM10_coflag) + { + //Set_LTEC(TT_CHANGE_CURR_2, task.current_param); + //LD_blinker.param = task.current_param; + //++LD_blinker.param; + task.current_param += task.delta_param; + TO10 = 0; + TIM10_coflag = 0; + + + } + } + HAL_TIM_Base_Stop(&htim10); + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); + + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); + + HAL_TIM_Base_Stop_IT(&htim8); + TIM8->CNT = 0; + + Stop_TIM10(); + task.current_param = task.min_param; + Set_LTEC(TT_CHANGE_CURR_2, task.current_param); + if (task.tau > 3) + { + TIM10_period = htim10.Init.Period; + htim10.Init.Period = 9999; + TO10_counter = (task.tau - 1) * 100; + } + HAL_TIM_Base_Start_IT(&htim10); + + +//*/ + + /* // Backup + Set_LTEC(TT_CHANGE_CURR_1, task.curr); + (void) MPhD_T(TT_CHANGE_TEMP_1); + LD1_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_1); + (void) MPhD_T(TT_CHANGE_TEMP_2); + LD2_param.LD_CURR_TEMP = MPhD_T(TT_CHANGE_TEMP_2); + temp16=PID_Controller_Temp(&LD1_curr_setup, &LD1_param, 1); + Set_LTEC(TT_CHANGE_TEMP_1, temp16);//Drive Laser TEC 1 + temp16=PID_Controller_Temp(&LD2_curr_setup, &LD2_param, 2); + Set_LTEC(TT_CHANGE_TEMP_2, temp16);//Drive Laser TEC 2 + + // Toggle pin for oscilloscope + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_SET); + HAL_GPIO_WritePin(GPIOD, GPIO_PIN_7, GPIO_PIN_RESET); + + st = HAL_TIM_Base_Start_IT(&htim10); + if (st != HAL_OK) + while(1); + while (task.current_param < task.max_param) + { + if (TIM10_coflag) + { + Set_LTEC(TT_CHANGE_CURR_2, task.current_param); + task.current_param += task.delta_param; + TO10 = 0; + TIM10_coflag = 0; + + + } + } + Stop_TIM10(); + task.current_param = task.min_param; + Set_LTEC(TT_CHANGE_CURR_2, task.current_param); + if (task.tau > 3) + { + TIM10_period = htim10.Init.Period; + htim10.Init.Period = 9999; + TO10_counter = (task.tau - 1) * 100; + } + HAL_TIM_Base_Start_IT(&htim10); + */ + +} diff --git a/Src/protocol.c b/Src/protocol.c new file mode 100644 index 0000000..4502641 --- /dev/null +++ b/Src/protocol.c @@ -0,0 +1,256 @@ +#include "protocol.h" +#include "app_state.h" /* test, Long_Data, COMMAND, CS_result, UART_header, u_tx_flg, task, TO10_counter */ +#include "File_Handling.h" /* Mount_SD, Create_File, Write_File_byte, Update_File_byte, Remove_File, Unmount_SD */ + +/* --- 6 функций из main.c --- */ +void Decode_uart(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup) +{ +// uint8_t *temp1; + uint16_t *temp2; + + //--------------------------------------------------------------------------------------------------------------------------------------------------------------------- + + + test=0; + if ((HAL_GPIO_ReadPin(SDMMC1_EN_GPIO_Port, SDMMC1_EN_Pin) == GPIO_PIN_RESET)&& + (HAL_GPIO_ReadPin(USB_FLAG_GPIO_Port, USB_FLAG_Pin) == GPIO_PIN_SET))//if exist sd && connect usb write comand to c + { + test = Mount_SD("/"); + if (test == 0) //0 - suc + { + //Format_SD(); + test = Remove_File ("COMMAND.TXT"); + test = Create_File("COMMAND.TXT"); // 0 -succ + test = Write_File_byte("COMMAND.TXT", (uint8_t *)Command, CL_8); + test = Update_File_byte("COMMAND.TXT", (uint8_t *)Command, CL_8); + test = Unmount_SD("/"); // 0 - succ + } + } + + temp2 = (uint16_t *)Command; + Curr_setup->WORK_EN = ((uint8_t)((*temp2)>>0))&0x01; + Curr_setup->U5V1_EN = ((uint8_t)((*temp2)>>1))&0x01; + Curr_setup->U5V2_EN = ((uint8_t)((*temp2)>>2))&0x01; + Curr_setup->LD1_EN = ((uint8_t)((*temp2)>>3))&0x01; + Curr_setup->LD2_EN = ((uint8_t)((*temp2)>>4))&0x01; + Curr_setup->REF1_EN = ((uint8_t)((*temp2)>>5))&0x01; + Curr_setup->REF2_EN = ((uint8_t)((*temp2)>>6))&0x01; + Curr_setup->TEC1_EN = ((uint8_t)((*temp2)>>7))&0x01; + Curr_setup->TEC2_EN = ((uint8_t)((*temp2)>>8))&0x01; + Curr_setup->TS1_EN = ((uint8_t)((*temp2)>>9))&0x01; + Curr_setup->TS2_EN = ((uint8_t)((*temp2)>>10))&0x01; + Curr_setup->SD_EN = ((uint8_t)((*temp2)>>11))&0x01; + Curr_setup->PI1_RD = ((uint8_t)((*temp2)>>12))&0x01; + Curr_setup->PI2_RD = ((uint8_t)((*temp2)>>13))&0x01; + + temp2++; + LD1_curr_setup->LD_TEMP = (uint16_t)(*temp2); + temp2++; + LD2_curr_setup->LD_TEMP = (uint16_t)(*temp2); + temp2++; + temp2++; + temp2++; + Curr_setup->AVERAGES = (uint16_t)(*temp2); + temp2++; + LD1_curr_setup->P_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); + temp2++; + LD1_curr_setup->I_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); + temp2++; + LD2_curr_setup->P_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); + temp2++; + LD2_curr_setup->I_coef_temp = (float)((uint16_t)(*temp2))/((float)(256));//(float)(1/(float)((uint16_t)(*temp2))*((float)(10))); + temp2++; + Long_Data[13] = (uint16_t)(*temp2);//Message ID + temp2++; + LD1_curr_setup->CURRENT = (uint16_t)(*temp2); + temp2++; + LD2_curr_setup->CURRENT = (uint16_t)(*temp2); + temp2++; + + if (Curr_setup->U5V1_EN) + { + HAL_GPIO_WritePin(EN_5V1_GPIO_Port, EN_5V1_Pin, GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(EN_5V1_GPIO_Port, EN_5V1_Pin, GPIO_PIN_RESET); + } + + if (Curr_setup->U5V2_EN) + { + HAL_GPIO_WritePin(EN_5V2_GPIO_Port, EN_5V2_Pin, GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(EN_5V2_GPIO_Port, EN_5V2_Pin, GPIO_PIN_RESET); + } + + if (Curr_setup->LD1_EN) + { + HAL_GPIO_WritePin(LD1_EN_GPIO_Port, LD1_EN_Pin, GPIO_PIN_SET); + //LL_SPI_Enable(SPI2);//Enable SPI for Laser1 DAC + } + else + { + HAL_GPIO_WritePin(LD1_EN_GPIO_Port, LD1_EN_Pin, GPIO_PIN_RESET); + //LL_SPI_Disable(SPI2);//Disable SPI for Laser1 DAC + } + + if (Curr_setup->LD2_EN) + { + HAL_GPIO_WritePin(LD2_EN_GPIO_Port, LD2_EN_Pin, GPIO_PIN_SET); + //LL_SPI_Enable(SPI6);//Enable SPI for Laser2 DAC + } + else + { + HAL_GPIO_WritePin(LD2_EN_GPIO_Port, LD2_EN_Pin, GPIO_PIN_RESET); + //LL_SPI_Disable(SPI6);//Disable SPI for Laser2 DAC + } + + if (Curr_setup->REF1_EN) + { + HAL_GPIO_WritePin(REF0_EN_GPIO_Port, REF0_EN_Pin, GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(REF0_EN_GPIO_Port, REF0_EN_Pin, GPIO_PIN_RESET); + } + + if (Curr_setup->REF2_EN) + { + HAL_GPIO_WritePin(REF2_ON_GPIO_Port, REF2_ON_Pin, GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(REF2_ON_GPIO_Port, REF2_ON_Pin, GPIO_PIN_RESET); + } + + if ((Curr_setup->TS1_EN)&&(Curr_setup->TEC1_EN)) + { + Set_LTEC(3,32767); + Set_LTEC(3,32767); + HAL_GPIO_WritePin(TEC1_PD_GPIO_Port, TEC1_PD_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(TECEN1_GPIO_Port, TECEN1_Pin, GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(TECEN1_GPIO_Port, TECEN1_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(TEC1_PD_GPIO_Port, TEC1_PD_Pin, GPIO_PIN_RESET); + } + + if ((Curr_setup->TS2_EN)&&(Curr_setup->TEC2_EN)) + { + Set_LTEC(4,32767); + Set_LTEC(4,32767); + HAL_GPIO_WritePin(TEC2_PD_GPIO_Port, TEC2_PD_Pin, GPIO_PIN_SET); + HAL_GPIO_WritePin(TECEN2_GPIO_Port, TECEN2_Pin, GPIO_PIN_SET); + } + else + { + HAL_GPIO_WritePin(TECEN2_GPIO_Port, TECEN2_Pin, GPIO_PIN_RESET); + HAL_GPIO_WritePin(TEC2_PD_GPIO_Port, TEC2_PD_Pin, GPIO_PIN_RESET); + } + + if (Curr_setup->PI1_RD==0) + { + LD1_curr_setup->P_coef_temp = 10; + LD1_curr_setup->I_coef_temp = 0.01; + } + + if (Curr_setup->PI2_RD==0) + { + LD2_curr_setup->P_coef_temp = 10; + LD2_curr_setup->I_coef_temp = 0.01; + } +} + +void Decode_task(uint16_t *Command, LDx_SetupTypeDef *LD1_curr_setup, LDx_SetupTypeDef *LD2_curr_setup, Work_SetupTypeDef *Curr_setup) +{ + uint16_t *temp2; + + temp2 = (uint16_t *)Command; + Curr_setup->WORK_EN = ((uint8_t)((*temp2)>>0))&0x01; + Curr_setup->U5V1_EN = ((uint8_t)((*temp2)>>1))&0x01; + Curr_setup->U5V2_EN = ((uint8_t)((*temp2)>>2))&0x01; + Curr_setup->LD1_EN = ((uint8_t)((*temp2)>>3))&0x01; + Curr_setup->LD2_EN = ((uint8_t)((*temp2)>>4))&0x01; + Curr_setup->REF1_EN = ((uint8_t)((*temp2)>>5))&0x01; + Curr_setup->REF2_EN = ((uint8_t)((*temp2)>>6))&0x01; + Curr_setup->TEC1_EN = ((uint8_t)((*temp2)>>7))&0x01; + Curr_setup->TEC2_EN = ((uint8_t)((*temp2)>>8))&0x01; + Curr_setup->TS1_EN = ((uint8_t)((*temp2)>>9))&0x01; + Curr_setup->TS2_EN = ((uint8_t)((*temp2)>>10))&0x01; + Curr_setup->SD_EN = ((uint8_t)((*temp2)>>11))&0x01; + Curr_setup->PI1_RD = ((uint8_t)((*temp2)>>12))&0x01; + Curr_setup->PI2_RD = ((uint8_t)((*temp2)>>13))&0x01; + + temp2++; + task.task_type = (uint8_t)(*temp2); temp2++; + task.min_param = (float)(*temp2); temp2++; + task.max_param = (float)(*temp2); temp2++; + task.delta_param = (float)(*temp2); temp2++; + task.dt = (float)(*temp2) / 100.0; temp2++; + task.sec_param = (float)(*temp2); temp2++; + task.curr = (float)(*temp2); temp2++; + task.temp = (float)(*temp2); temp2++; + task.tau = (float)(*temp2); temp2++; + task.p_coef_1 = (float)(*temp2) * 256.0; temp2++; + task.i_coef_1 = (float)(*temp2) * 256.0; temp2++; + task.p_coef_2 = (float)(*temp2) * 256.0; temp2++; + task.i_coef_2 = (float)(*temp2) * 256.0; temp2++; + + TO10_counter = task.dt / 10; +} + +uint8_t CheckChecksum(uint16_t *pbuff) +{ + uint16_t cl_ind; + + switch (UART_header) + { + case 0x7777: + cl_ind = TSK_16 - 2; + break; + case 0x1111: + cl_ind = CL_16 - 2; + break; + default: + return 0; + break; + } + + CS_result = CalculateChecksum(pbuff, cl_ind); + + return ((CS_result == COMMAND[cl_ind]) ? 1 : 0); +} +uint16_t CalculateChecksum(uint16_t *pbuff, uint16_t len) +{ + short i; + uint16_t cs = *pbuff; + + for(i = 1; i < len; i++) + { + cs ^= *(pbuff+i); + } + return cs; +} + +void USART_TX (uint8_t* dt, uint16_t sz) +{ + uint16_t ind = 0; + while (indLD_CURR_TEMP - (int) LDx_curr_setup->LD_TEMP; + + e_integral = LDx_results->e_integral; + + if((e_pid < 3000) && (e_pid > - 3000)){ + e_integral += LDx_curr_setup->I_coef_temp * (float)(e_pid) * (float)(TO7 - TO7_PID) / (float) 100;//100 - timer is too fast + } + P_coef_current = LDx_curr_setup->P_coef_temp; + + if (e_integral > 32000){ + e_integral = 32000; + } + else if (e_integral < - 32000){ + e_integral = -32000; + } + LDx_results->e_integral = e_integral; + + x_output = 32768 + P_coef_current * e_pid + (int)e_integral;//32768 - P_coef_current * e_pid - (int)e_integral;// + + if(x_output < 1000){ + x_output = 8800; + } + else if(x_output > 56800){ + x_output = 56800; + } + + if (num==2) + TO7_PID = TO7;//Save current time only on 2nd laser + + return (uint16_t)x_output; +} + + +uint16_t PID_Controller_Temp(LDx_SetupTypeDef * LDx_curr_setup, LDx_ParamTypeDef * LDx_results, uint8_t num) +{ + int e_pid; + float P_coef_current;//, I_coef_current; + float e_integral; + int x_output; + + e_pid = (int) LDx_results->LD_CURR_TEMP - (int) LDx_curr_setup->LD_TEMP; + + e_integral = LDx_results->e_integral; + + if((e_pid < 3000) && (e_pid > - 3000)){ + e_integral += LDx_curr_setup->I_coef_temp * (float)(e_pid) * (float)(TO7 - TO7_PID) / (float) 100;//100 - timer is too fast + } + P_coef_current = LDx_curr_setup->P_coef_temp; + + if (e_integral > 32000){ + e_integral = 32000; + } + else if (e_integral < - 32000){ + e_integral = -32000; + } + LDx_results->e_integral = e_integral; + + x_output = 32768 + P_coef_current * e_pid + (int)e_integral;//32768 - P_coef_current * e_pid - (int)e_integral;// + + if(x_output < 1000){ + x_output = 8800; + } + else if(x_output > 56800){ + x_output = 56800; + } + + if (num==2) + TO7_PID = TO7;//Save current time only on 2nd laser + + return (uint16_t)x_output; +}