Refactor split main.c

This commit is contained in:
Guriy
2026-07-03 18:05:11 +03:00
parent 90abf51075
commit 81106742dd
14 changed files with 1117 additions and 987 deletions

256
Src/protocol.c Normal file
View File

@ -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 (ind<sz)
{
while (!LL_USART_IsActiveFlag_TXE(USART1)) {}
LL_USART_TransmitData8(USART1,*(uint8_t*)(dt+ind));
ind++;
}
}
void USART_TX_DMA (uint16_t sz)
{
while (u_tx_flg) {}//Wait until previous transfer not complete. u_tx_flg is resetting in DMA interupt function.
LL_DMA_DisableStream(DMA2, LL_DMA_STREAM_7);
LL_DMA_SetDataLength(DMA2, LL_DMA_STREAM_7, sz);
LL_DMA_EnableStream(DMA2, LL_DMA_STREAM_7);
u_tx_flg = 1;//indicate that transfer begin
}