3434*/
3535/**************************************************************************/
3636
37- #ifdef NRF52840_XXAA
38-
3937#include "tusb.h"
4038#include "internal_flash.h"
4139
5553
5654#define FL_PAGE_SZ 4096
5755
58- /*------------------------------------------------------------------*/
59- /* VARIABLES
60- *------------------------------------------------------------------*/
6156static scsi_sense_fixed_data_t mscd_sense_data =
6257{
6358 .response_code = 0x70 ,
@@ -73,114 +68,109 @@ static scsi_mode_parameters_t const msc_dev_mode_para =
7368 .block_descriptor_length = 0
7469};
7570
76- /*------------------------------------------------------------------*/
77- /*
78- *------------------------------------------------------------------*/
79-
8071//--------------------------------------------------------------------+
8172// tinyusb callbacks
8273//--------------------------------------------------------------------+
83- int32_t tud_msc_scsi_cb (uint8_t rhport , uint8_t lun , uint8_t const scsi_cmd [16 ], void * buffer , uint16_t bufsize )
84- {
85- // read10 & write10 has their own callback and MUST not be handled here
86-
87- void const * ptr = NULL ;
88- uint16_t len = 0 ;
89-
90- // most scsi handled is input
91- bool in_xfer = true;
92-
93- switch (scsi_cmd [0 ])
94- {
95- case SCSI_CMD_REQUEST_SENSE :
96- ptr = & mscd_sense_data ;
97- len = sizeof (scsi_sense_fixed_data_t );
98- break ;
99-
100- case SCSI_CMD_MODE_SENSE_6 :
101- ptr = & msc_dev_mode_para ;
102- len = sizeof (msc_dev_mode_para );
103- break ;
104-
105- case SCSI_CMD_TEST_UNIT_READY :
106- ptr = NULL ;
107- len = 0 ;
108- break ;
109-
110- case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL :
111- ptr = NULL ;
112- len = 0 ;
113- break ;
114-
115- default :
116- // negative is error -> Data stage is STALL, status = failed
117- return -1 ;
118- }
119-
120- // return len must not larger than bufsize
121- TU_ASSERT ( bufsize >= len );
122-
123- if ( ptr && len )
124- {
125- if (in_xfer )
126- {
127- memcpy (buffer , ptr , len );
128- }else
74+ int32_t tud_msc_scsi_cb (uint8_t rhport , uint8_t lun , uint8_t const scsi_cmd [16 ], void * buffer , uint16_t bufsize ) {
75+ // read10 & write10 has their own callback and MUST not be handled here
76+
77+ void const * ptr = NULL ;
78+ uint16_t len = 0 ;
79+
80+ // most scsi handled is input
81+ bool in_xfer = true;
82+
83+ switch ( scsi_cmd [0 ] )
12984 {
130- // SCSI output
85+ case SCSI_CMD_REQUEST_SENSE :
86+ ptr = & mscd_sense_data ;
87+ len = sizeof (scsi_sense_fixed_data_t );
88+ break ;
89+
90+ case SCSI_CMD_MODE_SENSE_6 :
91+ ptr = & msc_dev_mode_para ;
92+ len = sizeof (msc_dev_mode_para );
93+ break ;
94+
95+ case SCSI_CMD_TEST_UNIT_READY :
96+ ptr = NULL ;
97+ len = 0 ;
98+ break ;
99+
100+ case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL :
101+ ptr = NULL ;
102+ len = 0 ;
103+ break ;
104+
105+ default :
106+ // negative is error -> Data stage is STALL, status = failed
107+ return -1 ;
131108 }
132- }
133109
134- //------------- clear sense data if it is not request sense command -------------//
135- if ( SCSI_CMD_REQUEST_SENSE != scsi_cmd [0 ] )
136- {
137- mscd_sense_data .sense_key = SCSI_SENSEKEY_NONE ;
138- mscd_sense_data .additional_sense_code = 0 ;
139- mscd_sense_data .additional_sense_qualifier = 0 ;
140- }
110+ // return len must not larger than bufsize
111+ TU_ASSERT (bufsize >= len );
112+
113+ if ( ptr && len )
114+ {
115+ if ( in_xfer )
116+ {
117+ memcpy (buffer , ptr , len );
118+ } else
119+ {
120+ // SCSI output
121+ }
122+ }
123+
124+ //------------- clear sense data if it is not request sense command -------------//
125+ if ( SCSI_CMD_REQUEST_SENSE != scsi_cmd [0 ] )
126+ {
127+ mscd_sense_data .sense_key = SCSI_SENSEKEY_NONE ;
128+ mscd_sense_data .additional_sense_code = 0 ;
129+ mscd_sense_data .additional_sense_qualifier = 0 ;
130+ }
141131
142- return len ;
132+ return len ;
143133}
144134
145135/*------------------------------------------------------------------*/
146136/* Tinyusb Flash READ10 & WRITE10
147137 *------------------------------------------------------------------*/
148- int32_t tud_msc_read10_cb (uint8_t rhport , uint8_t lun , uint32_t lba , uint32_t offset , void * buffer , uint32_t bufsize )
149- {
150- (void ) rhport ; (void ) lun ; (void ) offset ;
138+ int32_t tud_msc_read10_cb (uint8_t rhport , uint8_t lun , uint32_t lba , uint32_t offset , void * buffer , uint32_t bufsize ) {
139+ (void ) rhport ;
140+ (void ) lun ;
141+ (void ) offset ;
151142
152- uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
143+ uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
153144
154145 internal_flash_read_blocks (buffer , lba , block_count );
155146
156- return block_count * MSC_FLASH_BLOCK_SIZE ;
147+ return block_count * MSC_FLASH_BLOCK_SIZE ;
157148}
158149
159- int32_t tud_msc_write10_cb (uint8_t rhport , uint8_t lun , uint32_t lba , uint32_t offset , void * buffer , uint32_t bufsize )
160- {
161- (void ) rhport ; (void ) lun ; (void ) offset ;
150+ int32_t tud_msc_write10_cb (uint8_t rhport , uint8_t lun , uint32_t lba , uint32_t offset , void * buffer , uint32_t bufsize ) {
151+ (void ) rhport ;
152+ (void ) lun ;
153+ (void ) offset ;
162154
163- uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
155+ uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
164156
165157 // bufsize <= CFG_TUD_MSC_BUFSIZE (4096)
166158 internal_flash_write_blocks (buffer , lba , block_count );
167159
168160 // update fatfs's cache if address matches
169161 fs_user_mount_t * vfs = MP_STATE_VM (vfs_mount_table )-> obj ;
170162
171- if ( (lba <= vfs -> fatfs .winsect ) && (vfs -> fatfs .winsect <= (lba + bufsize / MSC_FLASH_BLOCK_SIZE )) ) {
172- memcpy (vfs -> fatfs .win , buffer + MSC_FLASH_BLOCK_SIZE * (vfs -> fatfs .winsect - lba ), MSC_FLASH_BLOCK_SIZE );
163+ if ( (lba <= vfs -> fatfs .winsect ) && (vfs -> fatfs .winsect <= (lba + bufsize / MSC_FLASH_BLOCK_SIZE )) ) {
164+ memcpy (vfs -> fatfs .win , buffer + MSC_FLASH_BLOCK_SIZE * (vfs -> fatfs .winsect - lba ), MSC_FLASH_BLOCK_SIZE );
173165 }
174166
175- return block_count * MSC_FLASH_BLOCK_SIZE ;
167+ return block_count * MSC_FLASH_BLOCK_SIZE ;
176168}
177169
178- void tud_msc_write10_complete_cb (uint8_t rhport , uint8_t lun )
179- {
180- (void ) rhport ; ( void ) lun ;
170+ void tud_msc_write10_complete_cb (uint8_t rhport , uint8_t lun ) {
171+ ( void ) rhport ;
172+ (void ) lun ;
181173
182174 // flush pending cache when write10 is complete
183175 internal_flash_flush ();
184176}
185-
186- #endif
0 commit comments