34
34
*/
35
35
/**************************************************************************/
36
36
37
- #ifdef NRF52840_XXAA
38
-
39
37
#include "tusb.h"
40
38
#include "internal_flash.h"
41
39
55
53
56
54
#define FL_PAGE_SZ 4096
57
55
58
- /*------------------------------------------------------------------*/
59
- /* VARIABLES
60
- *------------------------------------------------------------------*/
61
56
static scsi_sense_fixed_data_t mscd_sense_data =
62
57
{
63
58
.response_code = 0x70 ,
@@ -73,114 +68,109 @@ static scsi_mode_parameters_t const msc_dev_mode_para =
73
68
.block_descriptor_length = 0
74
69
};
75
70
76
- /*------------------------------------------------------------------*/
77
- /*
78
- *------------------------------------------------------------------*/
79
-
80
71
//--------------------------------------------------------------------+
81
72
// tinyusb callbacks
82
73
//--------------------------------------------------------------------+
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 ] )
129
84
{
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 ;
131
108
}
132
- }
133
109
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
+ }
141
131
142
- return len ;
132
+ return len ;
143
133
}
144
134
145
135
/*------------------------------------------------------------------*/
146
136
/* Tinyusb Flash READ10 & WRITE10
147
137
*------------------------------------------------------------------*/
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 ;
151
142
152
- uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
143
+ uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
153
144
154
145
internal_flash_read_blocks (buffer , lba , block_count );
155
146
156
- return block_count * MSC_FLASH_BLOCK_SIZE ;
147
+ return block_count * MSC_FLASH_BLOCK_SIZE ;
157
148
}
158
149
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 ;
162
154
163
- uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
155
+ uint32_t const block_count = bufsize / MSC_FLASH_BLOCK_SIZE ;
164
156
165
157
// bufsize <= CFG_TUD_MSC_BUFSIZE (4096)
166
158
internal_flash_write_blocks (buffer , lba , block_count );
167
159
168
160
// update fatfs's cache if address matches
169
161
fs_user_mount_t * vfs = MP_STATE_VM (vfs_mount_table )-> obj ;
170
162
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 );
173
165
}
174
166
175
- return block_count * MSC_FLASH_BLOCK_SIZE ;
167
+ return block_count * MSC_FLASH_BLOCK_SIZE ;
176
168
}
177
169
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 ;
181
173
182
174
// flush pending cache when write10 is complete
183
175
internal_flash_flush ();
184
176
}
185
-
186
- #endif
0 commit comments