Implemented write

This commit is contained in:
Bernd Herzog 2023-04-01 17:39:08 +02:00
parent 50859171f4
commit bb90fdc5fc
4 changed files with 27 additions and 36 deletions

View File

@ -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);
}

View File

@ -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__ */

View File

@ -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;
}

View File

@ -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;