Skip to content

Commit 19ff31d

Browse files
committed
add mutex to log utils
1 parent e333e07 commit 19ff31d

File tree

4 files changed

+72
-37
lines changed

4 files changed

+72
-37
lines changed

Boardfiles/nucleol552zeq/Core/Src/main.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
extern "C" {
4343
#include "app_fatfs.h"
44+
#include "log_util.h"
4445
}
4546
/* USER CODE END Includes */
4647

@@ -79,7 +80,6 @@ extern "C" {
7980
void SMTask(void *pvParameters) {
8081
SystemManager SM;
8182
SM.flyManually();
82-
8383
}
8484
/* USER CODE END 0 */
8585

@@ -136,14 +136,11 @@ int main(void)
136136
Error_Handler();
137137
}
138138
/* USER CODE BEGIN 2 */
139-
140-
141-
139+
logInit();
142140

143141
TaskHandle_t hSM = NULL;
144142
xTaskCreate(SMTask, "SM", 500U, NULL, osPriorityNormal, &hSM);
145143

146-
147144
/* USER CODE END 2 */
148145

149146
/* Init scheduler */

Boardfiles/nucleol552zeq/Core/Src/usart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void MX_LPUART1_UART_Init(void)
4444

4545
/* USER CODE END LPUART1_Init 1 */
4646
hlpuart1.Instance = LPUART1;
47-
hlpuart1.Init.BaudRate = 230400;
47+
hlpuart1.Init.BaudRate = 209700;
4848
hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
4949
hlpuart1.Init.StopBits = UART_STOPBITS_1;
5050
hlpuart1.Init.Parity = UART_PARITY_NONE;
Lines changed: 66 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
#include "log_util.h"
22

3+
static SemaphoreHandle_t log_mutex = NULL;
4+
5+
/**
6+
* @brief Create mutex for logging utilities
7+
* @retval FRESULT: result status
8+
*/
9+
FRESULT logInit() {
10+
if (log_mutex == NULL) {
11+
log_mutex = xSemaphoreCreateMutex();
12+
}
13+
return FR_OK;
14+
}
15+
316
/**
417
* @brief Write/append bytes into a file, creates file if does not exist
518
* @param fileName: file name as a string
@@ -12,22 +25,33 @@ FRESULT logWrite(char* fileName, void* buff, UINT buffSize, UINT* bytesWritten)
1225
FATFS fs;
1326
FIL fil;
1427
FRESULT res;
15-
16-
if (res = f_mount(&fs, "", 0) != FR_OK)
17-
return res;
18-
19-
if (res = f_open(&fil, fileName, FA_WRITE | FA_OPEN_APPEND) != FR_OK)
20-
return res;
21-
22-
if (res = f_write(&fil, buff, buffSize, bytesWritten) != FR_OK)
23-
return res;
24-
25-
if (res = f_close(&fil) != FR_OK)
26-
return res;
27-
28-
if (res = f_mount(0, "", 0) != FR_OK)
29-
return res;
30-
28+
if (xSemaphoreTake(log_mutex, (TickType_t) portMAX_DELAY) == pdPASS) {
29+
if (res = f_mount(&fs, "", 0) != FR_OK) {
30+
xSemaphoreGive(log_mutex);
31+
return res;
32+
}
33+
34+
if (res = f_open(&fil, fileName, FA_WRITE | FA_OPEN_APPEND) != FR_OK) {
35+
xSemaphoreGive(log_mutex);
36+
return res;
37+
}
38+
39+
if (res = f_write(&fil, buff, buffSize, bytesWritten) != FR_OK) {
40+
xSemaphoreGive(log_mutex);
41+
return res;
42+
}
43+
44+
if (res = f_close(&fil) != FR_OK) {
45+
xSemaphoreGive(log_mutex);
46+
return res;
47+
}
48+
49+
if (res = f_mount(0, "", 0) != FR_OK) {
50+
xSemaphoreGive(log_mutex);
51+
return res;
52+
}
53+
xSemaphoreGive(log_mutex);
54+
}
3155
return FR_OK;
3256
}
3357

@@ -43,21 +67,32 @@ FRESULT logRead(char* fileName, void* buff, UINT buffSize, UINT* bytesRead) {
4367
FATFS fs;
4468
FIL fil;
4569
FRESULT res;
70+
if (xSemaphoreTake(log_mutex, (TickType_t) portMAX_DELAY) == pdPASS) {
71+
if (res = f_mount(&fs, "", 0) != FR_OK) {
72+
xSemaphoreGive(log_mutex);
73+
return res;
74+
}
75+
76+
if (res = f_open(&fil, fileName, FA_READ) != FR_OK) {
77+
xSemaphoreGive(log_mutex);
78+
return res;
79+
}
80+
81+
if (res = f_read(&fil, buff, buffSize, bytesRead) != FR_OK) {
82+
xSemaphoreGive(log_mutex);
83+
return res;
84+
}
85+
86+
if (res = f_close(&fil) != FR_OK) {
87+
xSemaphoreGive(log_mutex);
88+
return res;
89+
}
4690

47-
if (res = f_mount(&fs, "", 0) != FR_OK)
48-
return res;
49-
50-
if (res = f_open(&fil, fileName, FA_READ) != FR_OK)
51-
return res;
52-
53-
if (res = f_read(&fil, buff, buffSize, bytesRead) != FR_OK)
54-
return res;
55-
56-
if (res = f_close(&fil) != FR_OK)
57-
return res;
58-
59-
if (res = f_mount(0, "", 0) != FR_OK)
60-
return res;
61-
91+
if (res = f_mount(0, "", 0) != FR_OK) {
92+
xSemaphoreGive(log_mutex);
93+
return res;
94+
}
95+
xSemaphoreGive(log_mutex);
96+
}
6297
return FR_OK;
6398
}

Boardfiles/nucleol552zeq/FATFS/Target/log_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
#include "stddef.h"
55
#include "ff.h"
6+
#include "FreeRTOS.h"
7+
#include "semphr.h"
68

9+
FRESULT logInit();
710
FRESULT logWrite(char*, void*, UINT, UINT*);
811
FRESULT logRead(char*, void*, UINT, UINT*);
912

0 commit comments

Comments
 (0)