From bb90fdc5fc75d83bdf88ee7ac6a780c56c2d528a Mon Sep 17 00:00:00 2001 From: Bernd Herzog Date: Sat, 1 Apr 2023 17:39:08 +0200 Subject: [PATCH] Implemented write --- firmware/baseband/sd_over_usb/diskio.c | 4 ++ firmware/baseband/sd_over_usb/diskio.h | 1 + .../baseband/sd_over_usb/proc_sd_over_usb.cpp | 12 +---- firmware/baseband/sd_over_usb/scsi.c | 46 ++++++++----------- 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/firmware/baseband/sd_over_usb/diskio.c b/firmware/baseband/sd_over_usb/diskio.c index 2fcbcfd9..2417e4a2 100644 --- a/firmware/baseband/sd_over_usb/diskio.c +++ b/firmware/baseband/sd_over_usb/diskio.c @@ -10,3 +10,7 @@ uint32_t get_capacity(void) { bool_t read_block(uint32_t startblk, uint8_t *buf, uint32_t n) { return sdcRead(&SDCD1, startblk, buf, n); } + +bool_t write_block(uint32_t startblk, uint8_t *buf, uint32_t n) { + return sdcWrite(&SDCD1, startblk, buf, n); +} diff --git a/firmware/baseband/sd_over_usb/diskio.h b/firmware/baseband/sd_over_usb/diskio.h index 5bfe9b30..7b0cd064 100644 --- a/firmware/baseband/sd_over_usb/diskio.h +++ b/firmware/baseband/sd_over_usb/diskio.h @@ -6,5 +6,6 @@ uint32_t get_capacity(void); bool_t read_block(uint32_t startblk, uint8_t *buf, uint32_t n); +bool_t write_block(uint32_t startblk, uint8_t *buf, uint32_t n); #endif /* __DISKIO_H__ */ \ No newline at end of file diff --git a/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp b/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp index 04fb8ee0..fa0c3c31 100644 --- a/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp +++ b/firmware/baseband/sd_over_usb/proc_sd_over_usb.cpp @@ -39,26 +39,18 @@ CH_IRQ_HANDLER(Vector60) { } } -//uint8_t buf[512]; - int main() { sdcStart(&SDCD1, nullptr); if (sdcConnect(&SDCD1) == CH_FAILED) chDbgPanic("no sd card #1"); - // memset(&buf[0], 0, 512); - // if (sdcRead(&SDCD1, 0, &buf[0], 1) == CH_FAILED) chDbgPanic("no sd card #2"); - - start_usb(); - // memset(&buf[0], 0, 512); - // if (sdcRead(&SDCD1, 0, &buf[0], 1) == CH_FAILED) chDbgPanic("no sd card #3"); - - //event_dispatcher.run(); while (true) { usb_transfer(); } + stop_usb(); + return 0; } diff --git a/firmware/baseband/sd_over_usb/scsi.c b/firmware/baseband/sd_over_usb/scsi.c index 1150bda2..b9c90760 100644 --- a/firmware/baseband/sd_over_usb/scsi.c +++ b/firmware/baseband/sd_over_usb/scsi.c @@ -6,17 +6,17 @@ __asm__ __volatile__("bkpt 1") -volatile bool usb_bulk_block_send = false; +volatile bool usb_bulk_block_done = false; void usb_bulk_block_cb(void* user_data, unsigned int bytes_transferred) { - usb_bulk_block_send = true; + usb_bulk_block_done = true; (void)user_data; (void)bytes_transferred; } void usb_send_bulk(void* const data, const uint32_t maximum_length) { - usb_bulk_block_send = false; + usb_bulk_block_done = false; usb_transfer_schedule_block( &usb_endpoint_bulk_in, @@ -25,7 +25,20 @@ void usb_send_bulk(void* const data, const uint32_t maximum_length) { usb_bulk_block_cb, NULL); - while (!usb_bulk_block_send); + while (!usb_bulk_block_done); +} + +void usb_receive_bulk(void* const data, const uint32_t maximum_length) { + usb_bulk_block_done = false; + + usb_transfer_schedule_block( + &usb_endpoint_bulk_out, + data, + maximum_length, + usb_bulk_block_cb, + NULL); + + while (!usb_bulk_block_done); } void usb_send_csw(msd_cbw_t *msd_cbw_data, uint8_t status) { @@ -135,7 +148,7 @@ uint8_t request_sense(msd_cbw_t *msd_cbw_data) { return 0; } -uint8_t mode_sense6 (msd_cbw_t *msd_cbw_data) { +uint8_t mode_sense6(msd_cbw_t *msd_cbw_data) { (void)msd_cbw_data; scsi_mode_sense6_response_t ret = { @@ -178,31 +191,12 @@ uint8_t data_read10(msd_cbw_t *msd_cbw_data) { return 0; } -volatile uint32_t write10_blocks_send = 0; -void write10_cb(void* user_data, unsigned int bytes_transferred) -{ - write10_blocks_send++; - - (void)user_data; - (void)bytes_transferred; -} - uint8_t data_write10(msd_cbw_t *msd_cbw_data) { - write10_blocks_send = 0; - data_request_t req = decode_data_request(msd_cbw_data->cmd_data); for (size_t block_index = 0; block_index < req.blk_cnt; block_index++) { - usb_transfer_schedule_block( - &usb_endpoint_bulk_out, - &usb_bulk_buffer[0], - 512, - write10_cb, - msd_cbw_data); - - while (write10_blocks_send <= block_index); - - //TODO: write to SD + usb_receive_bulk(&usb_bulk_buffer[0], 512); + write_block(req.first_lba + block_index, &usb_bulk_buffer[0], 1 /* n blocks */); } return 0;