/ * @brief * Execute a command * * @details * * @note * * @param[in] command * SPI Flash instruction * * @param[in] address * Memory address * * @param[in] buffer * Poniter to the read/write buffer * * @param[in] size * Buffer size in byte * * @return * Number of read/written bytes *********************************************************************/
| 261 | * Number of read/written bytes |
| 262 | *********************************************************************/ |
| 263 | rt_uint32_t efm_spiFlash_cmd( |
| 264 | enum sflash_inst_type_t command, |
| 265 | rt_uint32_t address, |
| 266 | rt_uint8_t *buffer, |
| 267 | rt_uint32_t size) |
| 268 | { |
| 269 | RT_ASSERT(sFlash != RT_NULL); |
| 270 | |
| 271 | sflash_instruction *inst; |
| 272 | rt_uint8_t *inst_buf; |
| 273 | rt_uint8_t inst_len, head_len; |
| 274 | rt_uint32_t data_len; |
| 275 | |
| 276 | sflash_debug("SFLASH: Inst %x\n", sflash_inst_code_tbl[command]); |
| 277 | if (sflash_data_len_tbl[command] && !size) |
| 278 | { |
| 279 | sflash_debug("SFLASH: No data!\n"); |
| 280 | return 0x00; |
| 281 | } |
| 282 | |
| 283 | data_len = (sflash_data_len_tbl[command] < size)? \ |
| 284 | sflash_data_len_tbl[command] : size; |
| 285 | if (data_len && (buffer == RT_NULL)) |
| 286 | { |
| 287 | sflash_debug("SFLASH: No buffer specified!\n"); |
| 288 | return 0x00; |
| 289 | } |
| 290 | |
| 291 | /* Allocate memory for write buffer */ |
| 292 | if (sflash_read_inst_tbl[command]) |
| 293 | { |
| 294 | inst_buf = rt_malloc(6 + 4); |
| 295 | inst = (sflash_instruction *)(inst_buf + 1); |
| 296 | head_len = 1; |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | inst_buf = rt_malloc(5 + data_len); |
| 301 | inst = (sflash_instruction *)inst_buf; |
| 302 | head_len = 0; |
| 303 | } |
| 304 | |
| 305 | /* Fill in instruction */ |
| 306 | inst->code = sflash_inst_code_tbl[command]; |
| 307 | if (command >= sflash_inst_wrlr) |
| 308 | { |
| 309 | /* MSB first */ |
| 310 | inst->address = ((address & 0x000000FF) << 16) | \ |
| 311 | (address & 0x0000FF00) | \ |
| 312 | ((address & 0x00FF0000) >> 16); |
| 313 | if (command >= sflash_inst_read_f) |
| 314 | { |
| 315 | inst->dummy = 0x00; |
| 316 | inst_len = 5; |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | inst_len = 4; |