35
35
36
36
enum
37
37
{
38
- NP_ERR_ADDR_EXCEEDED = 0x00 ,
39
- NP_ERR_ADDR_INVALID = 0x01 ,
40
- NP_ERR_ADDR_NOT_ALIGN = 0x02 ,
41
- NP_ERR_NAND_WR = 0x03 ,
42
- NP_ERR_NAND_RD = 0x04 ,
43
- NP_ERR_NAND_ERASE = 0x05 ,
44
- NP_ERR_CHIP_NOT_SEL = 0x06 ,
45
- NP_ERR_CHIP_NOT_FOUND = 0x07 ,
46
- NP_ERR_CMD_DATA_SIZE = 0x08 ,
47
- NP_ERR_CMD_INVALID = 0x09 ,
48
- NP_ERR_BUF_OVERFLOW = 0x0a ,
49
- NP_ERR_LEN_NOT_ALIGN = 0x0b ,
38
+ NP_ERR_INTERNAL = -1 ,
39
+ NP_ERR_ADDR_EXCEEDED = -100 ,
40
+ NP_ERR_ADDR_INVALID = -101 ,
41
+ NP_ERR_ADDR_NOT_ALIGN = -102 ,
42
+ NP_ERR_NAND_WR = -103 ,
43
+ NP_ERR_NAND_RD = -104 ,
44
+ NP_ERR_NAND_ERASE = -105 ,
45
+ NP_ERR_CHIP_NOT_SEL = -106 ,
46
+ NP_ERR_CHIP_NOT_FOUND = -107 ,
47
+ NP_ERR_CMD_DATA_SIZE = -108 ,
48
+ NP_ERR_CMD_INVALID = -109 ,
49
+ NP_ERR_BUF_OVERFLOW = -110 ,
50
+ NP_ERR_LEN_NOT_ALIGN = -111 ,
50
51
};
51
52
52
53
typedef struct __attribute__((__packed__ ))
@@ -276,22 +277,23 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
276
277
277
278
if (addr & (prog -> chip_info -> block_size - 1 ))
278
279
{
279
- np_send_error (NP_ERR_ADDR_NOT_ALIGN );
280
- return -1 ;
280
+ ERROR_PRINT ("Address 0x%lx is not aligned to block size 0x%lx\r\n" ,
281
+ addr , prog -> chip_info -> block_size );
282
+ return NP_ERR_ADDR_NOT_ALIGN ;
281
283
}
282
284
283
285
if (len & (prog -> chip_info -> block_size - 1 ))
284
286
{
285
- np_send_error (NP_ERR_LEN_NOT_ALIGN );
286
- return -1 ;
287
+ ERROR_PRINT ("Length 0x%lx is not aligned to block size 0x%lx\r\n" ,
288
+ len , prog -> chip_info -> block_size );
289
+ return NP_ERR_LEN_NOT_ALIGN ;
287
290
}
288
291
289
292
if (addr + len > prog -> chip_info -> size )
290
293
{
291
294
ERROR_PRINT ("Erase address exceded 0x%lx+0x%lx is more then chip size "
292
295
"0x%lx\r\n" , addr , len , prog -> chip_info -> size );
293
- np_send_error (NP_ERR_ADDR_EXCEEDED );
294
- return -1 ;
296
+ return NP_ERR_ADDR_EXCEEDED ;
295
297
}
296
298
297
299
page = addr / prog -> chip_info -> page_size ;
@@ -300,10 +302,7 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
300
302
while (len )
301
303
{
302
304
if (np_nand_erase (prog , page ))
303
- {
304
- np_send_error (NP_ERR_NAND_ERASE );
305
- return -1 ;
306
- }
305
+ return NP_ERR_NAND_ERASE ;
307
306
308
307
if (len >= prog -> chip_info -> block_size )
309
308
len -= prog -> chip_info -> block_size ;
@@ -347,8 +346,7 @@ static int np_cmd_nand_write_start(np_prog_t *prog)
347
346
{
348
347
ERROR_PRINT ("Write address 0x%lx is more then chip size 0x%lx\r\n" ,
349
348
write_start_cmd -> addr , prog -> chip_info -> size );
350
- np_send_error (NP_ERR_ADDR_EXCEEDED );
351
- return -1 ;
349
+ return NP_ERR_ADDR_EXCEEDED ;
352
350
}
353
351
354
352
prog -> addr = write_start_cmd -> addr ;
@@ -429,15 +427,13 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
429
427
NP_PACKET_BUF_SIZE )
430
428
{
431
429
ERROR_PRINT ("Data size is wrong %d\r\n" , write_data_cmd -> len );
432
- np_send_error (NP_ERR_CMD_DATA_SIZE );
433
- return -1 ;
430
+ return NP_ERR_CMD_DATA_SIZE ;
434
431
}
435
432
436
433
if (!prog -> addr_is_valid )
437
434
{
438
435
ERROR_PRINT ("Write address is not set\r\n" );
439
- np_send_error (NP_ERR_ADDR_INVALID );
440
- return -1 ;
436
+ return NP_ERR_ADDR_INVALID ;
441
437
}
442
438
443
439
if (prog -> page .offset + write_data_cmd -> len > prog -> chip_info -> page_size )
@@ -451,10 +447,7 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
451
447
if (prog -> page .offset == prog -> chip_info -> page_size )
452
448
{
453
449
if (np_nand_write (prog , prog -> chip_info ))
454
- {
455
- np_send_error (NP_ERR_NAND_WR );
456
- return -1 ;
457
- }
450
+ return NP_ERR_NAND_WR ;
458
451
459
452
prog -> addr += prog -> chip_info -> page_size ;
460
453
if (prog -> addr >= prog -> chip_info -> size )
@@ -492,17 +485,13 @@ static int np_cmd_nand_write_end(np_prog_t *prog)
492
485
if (!prog -> addr_is_valid )
493
486
{
494
487
ERROR_PRINT ("Write address is not set\r\n" );
495
- np_send_error (NP_ERR_ADDR_INVALID );
496
- return -1 ;
488
+ return NP_ERR_ADDR_INVALID ;
497
489
}
498
490
499
491
prog -> addr_is_valid = 0 ;
500
492
501
493
if (np_nand_write (prog , prog -> chip_info ))
502
- {
503
- np_send_error (NP_ERR_NAND_WR );
504
- return -1 ;
505
- }
494
+ return NP_ERR_NAND_WR ;
506
495
507
496
Exit :
508
497
return np_send_ok_status ();
@@ -577,8 +566,7 @@ static int _np_cmd_nand_read(np_prog_t *prog)
577
566
{
578
567
ERROR_PRINT ("Read address 0x%lx is more then chip size 0x%lx\r\n" ,
579
568
read_cmd -> addr , prog -> chip_info -> size );
580
- np_send_error (NP_ERR_ADDR_EXCEEDED );
581
- return -1 ;
569
+ return NP_ERR_ADDR_EXCEEDED ;
582
570
}
583
571
584
572
addr = read_cmd -> addr ;
@@ -590,10 +578,7 @@ static int _np_cmd_nand_read(np_prog_t *prog)
590
578
while (read_cmd -> len )
591
579
{
592
580
if (np_nand_read (addr , & page , prog -> chip_info ))
593
- {
594
- np_send_error (NP_ERR_NAND_RD );
595
- return -1 ;
596
- }
581
+ return NP_ERR_NAND_RD ;
597
582
598
583
while (page .offset < prog -> chip_info -> page_size && read_cmd -> len )
599
584
{
@@ -627,8 +612,7 @@ static int _np_cmd_nand_read(np_prog_t *prog)
627
612
{
628
613
ERROR_PRINT ("Read address 0x%lx is more then chip size 0x%lx" ,
629
614
addr , prog -> chip_info -> page_size );
630
- np_send_error (NP_ERR_ADDR_EXCEEDED );
631
- return -1 ;
615
+ return NP_ERR_ADDR_EXCEEDED ;
632
616
}
633
617
page .page ++ ;
634
618
page .offset = 0 ;
@@ -665,8 +649,7 @@ static int np_cmd_nand_select(np_prog_t *prog)
665
649
prog -> chip_info = NULL ;
666
650
667
651
ERROR_PRINT ("Chip ID %lu not found\r\n" , select_cmd -> chip_num );
668
- np_send_error (NP_ERR_CHIP_NOT_FOUND );
669
- return -1 ;
652
+ return NP_ERR_CHIP_NOT_FOUND ;
670
653
}
671
654
672
655
return np_send_ok_status ();
@@ -686,16 +669,13 @@ static int np_read_bad_block_info_from_page(np_prog_t *prog, uint32_t block,
686
669
break ;
687
670
case NAND_ERROR :
688
671
ERROR_PRINT ("NAND read bad block info error at 0x%lx\r\n" , addr );
689
- np_send_error (NP_ERR_NAND_RD );
690
- return -1 ;
672
+ return NP_ERR_NAND_RD ;
691
673
case NAND_TIMEOUT_ERROR :
692
674
ERROR_PRINT ("NAND read timeout at 0x%lx\r\n" , addr );
693
- np_send_error (NP_ERR_NAND_RD );
694
- return -1 ;
675
+ return NP_ERR_NAND_RD ;
695
676
default :
696
677
ERROR_PRINT ("Unknown NAND status\r\n" );
697
- np_send_error (NP_ERR_NAND_RD );
698
- return -1 ;
678
+ return NP_ERR_NAND_RD ;
699
679
}
700
680
701
681
if (bad_block_data != NP_NAND_GOOD_BLOCK_MARK )
@@ -775,15 +755,13 @@ static int np_cmd_handler(np_prog_t *prog)
775
755
if (!prog -> chip_info && cmd -> code != NP_CMD_NAND_SELECT )
776
756
{
777
757
ERROR_PRINT ("Chip is not selected\r\n" );
778
- np_send_error (NP_ERR_CHIP_NOT_SEL );
779
- return -1 ;
758
+ return NP_ERR_CHIP_NOT_SEL ;
780
759
}
781
760
782
761
if (!np_cmd_is_valid (cmd -> code ))
783
762
{
784
763
ERROR_PRINT ("Invalid cmd code %d\r\n" , cmd -> code );
785
- np_send_error (NP_ERR_CMD_INVALID );
786
- return -1 ;
764
+ return NP_ERR_CMD_INVALID ;
787
765
}
788
766
789
767
if (cmd_handler [cmd -> code ].exec (prog ))
@@ -794,16 +772,21 @@ static int np_cmd_handler(np_prog_t *prog)
794
772
795
773
static void np_packet_handler (np_prog_t * prog )
796
774
{
775
+ int ret ;
776
+
797
777
do
798
778
{
799
779
np_comm_cb -> peek (& prog -> rx_buf );
800
780
801
781
if (!prog -> rx_buf )
802
782
break ;
803
783
804
- np_cmd_handler (prog );
784
+ ret = np_cmd_handler (prog );
805
785
806
786
np_comm_cb -> consume ();
787
+
788
+ if (ret < 0 )
789
+ np_send_error (- ret );
807
790
}
808
791
while (1 );
809
792
}
0 commit comments