Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "uhi_msc.h"
00048 #include "uhi_msc_mem.h"
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 static uint8_t uhi_msc_mem_lun;
00065
00066
00067 static volatile bool uhi_msc_mem_command_ongoing;
00068 static volatile bool uhi_msc_mem_command_status;
00069
00070
00071
00072
00073
00074
00075
00076 static void uhi_msc_mem_stop_pooling(bool b_success);
00077 static Ctrl_status uhi_msc_mem_translate_status(lun_status_t status);
00078
00079
00080
00081
00082
00083
00084
00085
00086 uint8_t uhi_msc_mem_get_lun(void)
00087 {
00088 while (!uhi_msc_is_available());
00089 return uhi_msc_get_lun();
00090 }
00091
00092 Ctrl_status uhi_msc_mem_test_unit_ready(uint8_t lun)
00093 {
00094 uhi_msc_lun_t *lun_desc;
00095
00096 while (!uhi_msc_is_available());
00097
00098 uhi_msc_mem_command_ongoing = true;
00099 uhi_msc_mem_lun = lun;
00100 if (!uhi_msc_scsi_test_unit_ready(uhi_msc_mem_lun, uhi_msc_mem_stop_pooling)) {
00101 return CTRL_FAIL;
00102 }
00103 while (uhi_msc_mem_command_ongoing);
00104 if (!uhi_msc_mem_command_status) {
00105 return CTRL_FAIL;
00106 }
00107 lun_desc = uhi_msc_get_lun_desc(uhi_msc_mem_lun);
00108 return uhi_msc_mem_translate_status(lun_desc->status);
00109 }
00110
00111 Ctrl_status uhi_msc_mem_read_capacity(uint8_t lun, uint32_t * u32_nb_sector)
00112 {
00113 uhi_msc_lun_t *lun_desc;
00114 uint32_t *pBlockLen ;
00115
00116 while (!uhi_msc_is_available());
00117 uhi_msc_mem_lun = lun;
00118 lun_desc = uhi_msc_get_lun_desc(uhi_msc_mem_lun);
00119 pBlockLen = (uint32_t *)&lun_desc->capacity.pLogicalBlockLength[0];
00120
00121 if (lun_desc == NULL) {
00122 return CTRL_FAIL;
00123 }
00124 if (*pBlockLen != 512) {
00125
00126
00127
00128 return CTRL_FAIL;
00129 }
00130 *u32_nb_sector = (uint32_t)&lun_desc->capacity.pLogicalBlockAddress[0];
00131 return uhi_msc_mem_translate_status(lun_desc->status);
00132 }
00133
00134 uint8_t uhi_msc_mem_read_sector_size(uint8_t lun)
00135 {
00136 uhi_msc_lun_t *lun_desc;
00137 uint32_t *pBlockLen ;
00138 while (!uhi_msc_is_available());
00139 uhi_msc_mem_lun = lun;
00140 lun_desc = uhi_msc_get_lun_desc(uhi_msc_mem_lun);
00141 pBlockLen = (uint32_t *)&lun_desc->capacity.pLogicalBlockLength[0];
00142
00143 if (lun_desc == NULL) {
00144 return 0;
00145 }
00146 return ((*pBlockLen) / 512);
00147 }
00148
00149 bool uhi_msc_mem_wr_protect(uint8_t lun)
00150 {
00151 uhi_msc_lun_t *lun_desc;
00152 uint32_t *pBlockLen ;
00153
00154 while (!uhi_msc_is_available());
00155 uhi_msc_mem_lun = lun;
00156 lun_desc = uhi_msc_get_lun_desc(uhi_msc_mem_lun);
00157 pBlockLen = (uint32_t *)&lun_desc->capacity.pLogicalBlockLength[0];
00158
00159 if (lun_desc == NULL) {
00160 return true;
00161 }
00162 if (*pBlockLen != 512) {
00163 return true;
00164 }
00165 return lun_desc->b_write_protected;
00166 }
00167
00168 bool uhi_msc_mem_removal(void)
00169 {
00170 return true;
00171 }
00172
00173 Ctrl_status uhi_msc_mem_read_10_ram(uint32_t addr, void *ram)
00174 {
00175 uhi_msc_lun_t *lun_desc;
00176 uint32_t *pBlockLen ;
00177
00178 while (!uhi_msc_is_available());
00179
00180 lun_desc = uhi_msc_get_lun_desc(uhi_msc_mem_lun);
00181 pBlockLen = (uint32_t *)&lun_desc->capacity.pLogicalBlockLength[0];
00182
00183 if (lun_desc == NULL) {
00184 return CTRL_FAIL;
00185 }
00186 if (uhi_msc_mem_translate_status(lun_desc->status) != CTRL_GOOD) {
00187 return uhi_msc_mem_translate_status(lun_desc->status);
00188 }
00189 if (*pBlockLen != 512) {
00190 return CTRL_FAIL;
00191 }
00192 uhi_msc_mem_command_ongoing = true;
00193 uhi_msc_scsi_read_10(uhi_msc_mem_lun, addr, ram, 1,
00194 uhi_msc_mem_stop_pooling);
00195 while (uhi_msc_mem_command_ongoing);
00196 if (!uhi_msc_mem_command_status) {
00197 return CTRL_FAIL;
00198 }
00199 return uhi_msc_mem_translate_status(lun_desc->status);
00200
00201 }
00202
00203 Ctrl_status uhi_msc_mem_write_10_ram(uint32_t addr, const void *ram)
00204 {
00205 uhi_msc_lun_t *lun_desc;
00206 uint32_t *pBlockLen;
00207
00208 while (!uhi_msc_is_available());
00209
00210 lun_desc = uhi_msc_get_lun_desc(uhi_msc_mem_lun);
00211 pBlockLen = (uint32_t *)&lun_desc->capacity.pLogicalBlockLength[0];
00212
00213 if (lun_desc == NULL) {
00214 return CTRL_FAIL;
00215 }
00216 if (uhi_msc_mem_translate_status(lun_desc->status) != CTRL_GOOD) {
00217 return uhi_msc_mem_translate_status(lun_desc->status);
00218 }
00219 if (*pBlockLen != 512) {
00220 return CTRL_FAIL;
00221 }
00222 uhi_msc_mem_command_ongoing = true;
00223 uhi_msc_scsi_write_10(uhi_msc_mem_lun, addr, ram, 1,
00224 uhi_msc_mem_stop_pooling);
00225 while (uhi_msc_mem_command_ongoing);
00226 if (!uhi_msc_mem_command_status) {
00227 return CTRL_FAIL;
00228 }
00229 return uhi_msc_mem_translate_status(lun_desc->status);
00230 }
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 static void uhi_msc_mem_stop_pooling(bool b_success)
00245 {
00246 uhi_msc_mem_command_ongoing = false;
00247 uhi_msc_mem_command_status = b_success;
00248 }
00249
00250
00251
00252
00253
00254
00255
00256
00257 static Ctrl_status uhi_msc_mem_translate_status(lun_status_t status)
00258 {
00259 switch (status) {
00260 case LUN_GOOD:
00261 return CTRL_GOOD;
00262 case LUN_NOT_PRESENT:
00263 return CTRL_NO_PRESENT;
00264 case LUN_BUSY:
00265 return CTRL_BUSY;
00266 case LUN_FAIL:
00267 default:
00268 return CTRL_FAIL;
00269 }
00270 }
00271
00272
00273
00274