Skip to content

Commit 8bdba05

Browse files
microbit-carlosmathias-arm
authored andcommitted
dap_vendor: Buffer MSD_Write packets into 512-byte blocks.
To send to the file streamer blocks of size matching the VFS sector size, which is expected for some stream formats like Universal Hex Blocks.
1 parent 56058d5 commit 8bdba05

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

source/daplink/cmsis-dap/DAP_vendor.c

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,19 @@
3838
#include "settings.h"
3939
#include "target_family.h"
4040
#include "flash_manager.h"
41+
#include "util.h"
4142
#include <string.h>
4243
#include "daplink_vendor_commands.h"
4344

4445
#ifdef DRAG_N_DROP_SUPPORT
4546
#include "file_stream.h"
47+
48+
// Reusing the MSC sector buffer from vfs_manager.c to save memory
49+
// as using both at the same time will break anyway
50+
extern uint32_t usb_buffer[VFS_SECTOR_SIZE / sizeof(uint32_t)];
51+
static uint8_t *file_stream_buffer = (uint8_t *)usb_buffer;
52+
static const uint32_t file_stream_buffer_size = sizeof(usb_buffer);
53+
static uint16_t file_stream_buffer_pos = 0;
4654
#endif
4755

4856
//**************************************************************************************************
@@ -150,20 +158,49 @@ uint32_t DAP_ProcessVendorCommand(const uint8_t *request, uint8_t *response) {
150158
// open mass storage device stream
151159
*response = stream_open((stream_type_t)(*request));
152160
num += (1 << 16) | 1;
161+
file_stream_buffer_pos = 0;
153162
break;
154163
}
155164
case ID_DAP_MSD_Close: {
165+
// write the remaining data in the buffer
166+
if (file_stream_buffer_pos) {
167+
*response = stream_write(file_stream_buffer, file_stream_buffer_pos);
168+
file_stream_buffer_pos = 0;
169+
if (ERROR_SUCCESS != *response &&
170+
ERROR_SUCCESS_DONE != *response &&
171+
ERROR_SUCCESS_DONE_OR_CONTINUE != *response) {
172+
num += 1;
173+
break;
174+
}
175+
}
156176
// close mass storage device stream
157177
*response = stream_close();
158178
num += 1;
159179
break;
160180
}
161181
case ID_DAP_MSD_Write: {
162-
// write to mass storage device
182+
// write to mass storage device in blocks of length == vfs sector size
163183
uint32_t write_len = *request;
164184
request++;
165185
main_blink_msc_led(MAIN_LED_FLASH);
166-
*response = stream_write((uint8_t *)request, write_len);
186+
if (file_stream_buffer_pos || (write_len % VFS_SECTOR_SIZE)) {
187+
uint32_t write_len_left = write_len;
188+
while (write_len_left > 0) {
189+
uint16_t copy_len = MIN(VFS_SECTOR_SIZE - file_stream_buffer_pos, write_len_left);
190+
memcpy(file_stream_buffer + file_stream_buffer_pos, request, copy_len);
191+
file_stream_buffer_pos += copy_len;
192+
write_len_left -= copy_len;
193+
request += copy_len;
194+
if (file_stream_buffer_pos >= VFS_SECTOR_SIZE) {
195+
*response = stream_write(file_stream_buffer, VFS_SECTOR_SIZE);
196+
file_stream_buffer_pos = 0;
197+
} else {
198+
*response = ERROR_SUCCESS;
199+
}
200+
}
201+
} else {
202+
*response = stream_write((uint8_t *)request, write_len);
203+
}
167204
num += ((write_len + 1) << 16) | 1;
168205
break;
169206
}

source/daplink/drag-n-drop/vfs_manager.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ U32 USBD_MSC_BlockCount;
126126
U8 *USBD_MSC_BlockBuf;
127127
#endif
128128

129-
static uint32_t usb_buffer[VFS_SECTOR_SIZE / sizeof(uint32_t)];
129+
uint32_t usb_buffer[VFS_SECTOR_SIZE / sizeof(uint32_t)];
130130
static error_t fail_reason = ERROR_SUCCESS;
131131
static file_transfer_state_t file_transfer_state;
132132

0 commit comments

Comments
 (0)