2022-11-10 01:38 AM
Below I am mentioned the code kindly go through it:
if(PushButtonFlag == 1)
{
HAL_UART_Transmit_IT(&huart1, PushButtonPrint0, sizeof(PushButtonPrint0));
//Open file for writing (Create)
if(f_open(&SDFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
if((byteswritten == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
}
else
{
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
f_close(&SDFile);
HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
}
// f_unlink("STM32.TXT");
}
if(f_open(&SDFile, "Jeev.TXT", FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
if((byteswritten == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
}
else
{
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
f_close(&SDFile);
HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
}
//f_unlink("Jeev.TXT");
}
if(f_open(&SDFile, "Jeev2.TXT", FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
if((byteswritten == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
}
else
{
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
f_close(&SDFile);
HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
}
// f_unlink("Jeev2.TXT");
}
if(f_open(&SDFile, "Jeev3.TXT", FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
if((byteswritten == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
}
else
{
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
f_close(&SDFile);
HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
}
// f_unlink("Jeev3.TXT");
}
if(f_open(&SDFile, "Jeev4.TXT", FA_CREATE_ALWAYS | FA_WRITE ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
//Write to the text file
res = f_write(&SDFile, wtext, strlen((char *)wtext), (void *)&byteswritten);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
if((byteswritten == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
}
else
{
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
f_close(&SDFile);
HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
}
// f_unlink("Jeev4.TXT");
}
if(f_open(&SDFile, "Jeev.TXT", FA_READ ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
//Write to the text file
res = f_read(&SDFile, rtext, sizeof(rtext), (void *)&bytesread);
//HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
osDelay(10);
if((bytesread == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
}
else
{
HAL_UART_Transmit (&huart1, rtext, sizeof(rtext), 10);
osDelay(10);
f_close(&SDFile);
//HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
//break;
// f_unlink("Jeev.TXT");
}
}
osDelay(10);
if(f_open(&SDFile, "STM32.TXT", FA_READ ) != FR_OK)
{
HAL_UART_Transmit_IT(&huart1, Print3, sizeof(Print3));
Error_Handler();
}
else
{
HAL_UART_Transmit_IT(&huart1, Print4, sizeof(Print4));
// //Read the text file
// // memset(rtext,0,sizeof(rtext));
res = f_read(&SDFile, rtext, sizeof(rtext), (void *)&bytesread);
HAL_Delay(10);
if((bytesread == 0) || (res != FR_OK))
{
HAL_UART_Transmit_IT(&huart1, Print5, sizeof(Print5));
Error_Handler();
//break;
}
else
{
HAL_UART_Transmit (&huart1, rtext, sizeof(rtext), 10);
HAL_Delay(10);
f_close(&SDFile);
//HAL_UART_Transmit_IT(&huart1, Print6, sizeof(Print6));
break;
}
}
// osDelay(20);
HAL_UART_Transmit_IT(&huart1, Print10, sizeof(Print10));
PushButtonFlag = 0;
}