Skip to content

Commit 6a994ae

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 3a733f4 commit 6a994ae

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
@@ -43,6 +43,14 @@
4343
#if defined(DRAG_N_DROP_SUPPORT) && !defined(DRAG_N_DROP_DISABLE)
4444
#include "file_stream.h"
4545
#include "flash_manager.h"
46+
#include "util.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
//**************************************************************************************************
@@ -152,20 +160,49 @@ uint32_t DAP_ProcessVendorCommand(const uint8_t *request, uint8_t *response) {
152160
// open mass storage device stream
153161
*response = stream_open((stream_type_t)(*request));
154162
num += (1 << 16) | 1;
163+
file_stream_buffer_pos = 0;
155164
break;
156165
}
157166
case ID_DAP_MSD_Close: {
167+
// write the remaining data in the buffer
168+
if (file_stream_buffer_pos) {
169+
*response = stream_write(file_stream_buffer, file_stream_buffer_pos);
170+
file_stream_buffer_pos = 0;
171+
if (ERROR_SUCCESS != *response &&
172+
ERROR_SUCCESS_DONE != *response &&
173+
ERROR_SUCCESS_DONE_OR_CONTINUE != *response) {
174+
num += 1;
175+
break;
176+
}
177+
}
158178
// close mass storage device stream
159179
*response = stream_close();
160180
num += 1;
161181
break;
162182
}
163183
case ID_DAP_MSD_Write: {
164-
// write to mass storage device
184+
// write to mass storage device in blocks of length == vfs sector size
165185
uint32_t write_len = *request;
166186
request++;
167187
main_blink_msc_led(MAIN_LED_FLASH);
168-
*response = stream_write((uint8_t *)request, write_len);
188+
if (file_stream_buffer_pos || (write_len % VFS_SECTOR_SIZE)) {
189+
uint32_t write_len_left = write_len;
190+
while (write_len_left > 0) {
191+
uint16_t copy_len = MIN(VFS_SECTOR_SIZE - file_stream_buffer_pos, write_len_left);
192+
memcpy(file_stream_buffer + file_stream_buffer_pos, request, copy_len);
193+
file_stream_buffer_pos += copy_len;
194+
write_len_left -= copy_len;
195+
request += copy_len;
196+
if (file_stream_buffer_pos >= VFS_SECTOR_SIZE) {
197+
*response = stream_write(file_stream_buffer, VFS_SECTOR_SIZE);
198+
file_stream_buffer_pos = 0;
199+
} else {
200+
*response = ERROR_SUCCESS;
201+
}
202+
}
203+
} else {
204+
*response = stream_write((uint8_t *)request, write_len);
205+
}
169206
num += ((write_len + 1) << 16) | 1;
170207
break;
171208
}

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

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

131-
static uint32_t usb_buffer[VFS_SECTOR_SIZE / sizeof(uint32_t)];
131+
uint32_t usb_buffer[VFS_SECTOR_SIZE / sizeof(uint32_t)];
132132
static error_t fail_reason = ERROR_SUCCESS;
133133
static file_transfer_state_t file_transfer_state;
134134

0 commit comments

Comments
 (0)