Skip to content

Commit ef08379

Browse files
author
Maksymilian Wojczuk
committed
Added function to write files
1 parent dfcace7 commit ef08379

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

Inc/fatfs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ void MX_FATFS_Init(void);
7070

7171
/* USER CODE BEGIN Prototypes */
7272
void usb_ls();
73+
int usb_write(const void *bytes, size_t size);
7374
/* USER CODE END Prototypes */
7475
#ifdef __cplusplus
7576
}

Src/fatfs.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,36 @@ void usb_ls() {
120120
printf("Error mounting USB\n");
121121
}
122122
}
123+
124+
#define CHECK_FRESULT(result, msg, er) \
125+
if ((result) != FR_OK) { \
126+
printf("%s, error code 0x%x\n", (msg), (result)); \
127+
return (er); \
128+
}
129+
130+
int usb_write(const void *bytes, size_t size) {
131+
FRESULT result;
132+
133+
134+
result = f_mount(&USBHFatFS, "", 1);
135+
if (result == FR_OK) {
136+
FIL fp;
137+
result = f_open(&fp, "0:/temp", FA_WRITE | FA_CREATE_ALWAYS);
138+
CHECK_FRESULT(result, "open failed", -1);
139+
140+
uint written_bytes;
141+
result = f_write(&fp, bytes, size, &written_bytes);
142+
CHECK_FRESULT(result, "write failed", -1);
143+
144+
result = f_close(&fp);
145+
CHECK_FRESULT(result, "close failed", -1);
146+
147+
f_mount(NULL, "", 0);
148+
} else {
149+
CHECK_FRESULT(result, "mount failed", -1);
150+
}
151+
152+
}
123153
/* USER CODE END Application */
124154

125155
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

Src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ void StartDefaultTask(void const * argument)
361361
HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_SET);
362362
usb_ls();
363363
osDelay(500);
364+
usb_write("asd", 3);
364365
HAL_GPIO_WritePin(LED_R_GPIO_Port, LED_R_Pin, GPIO_PIN_SET);
365366
}
366367
HAL_GPIO_WritePin(LED_G_GPIO_Port, LED_G_Pin, GPIO_PIN_SET);

0 commit comments

Comments
 (0)