Skip to content

Commit 9093154

Browse files
committed
Call send_error only once
1 parent e009194 commit 9093154

File tree

1 file changed

+42
-59
lines changed

1 file changed

+42
-59
lines changed

firmware/nand_programmer.c

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ enum
3535

3636
enum
3737
{
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,
5051
};
5152

5253
typedef struct __attribute__((__packed__))
@@ -276,22 +277,23 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
276277

277278
if (addr & (prog->chip_info->block_size - 1))
278279
{
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;
281283
}
282284

283285
if (len & (prog->chip_info->block_size - 1))
284286
{
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;
287290
}
288291

289292
if (addr + len > prog->chip_info->size)
290293
{
291294
ERROR_PRINT("Erase address exceded 0x%lx+0x%lx is more then chip size "
292295
"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;
295297
}
296298

297299
page = addr / prog->chip_info->page_size;
@@ -300,10 +302,7 @@ static int _np_cmd_nand_erase(np_prog_t *prog)
300302
while (len)
301303
{
302304
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;
307306

308307
if (len >= prog->chip_info->block_size)
309308
len -= prog->chip_info->block_size;
@@ -347,8 +346,7 @@ static int np_cmd_nand_write_start(np_prog_t *prog)
347346
{
348347
ERROR_PRINT("Write address 0x%lx is more then chip size 0x%lx\r\n",
349348
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;
352350
}
353351

354352
prog->addr = write_start_cmd->addr;
@@ -429,15 +427,13 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
429427
NP_PACKET_BUF_SIZE)
430428
{
431429
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;
434431
}
435432

436433
if (!prog->addr_is_valid)
437434
{
438435
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;
441437
}
442438

443439
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)
451447
if (prog->page.offset == prog->chip_info->page_size)
452448
{
453449
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;
458451

459452
prog->addr += prog->chip_info->page_size;
460453
if (prog->addr >= prog->chip_info->size)
@@ -492,17 +485,13 @@ static int np_cmd_nand_write_end(np_prog_t *prog)
492485
if (!prog->addr_is_valid)
493486
{
494487
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;
497489
}
498490

499491
prog->addr_is_valid = 0;
500492

501493
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;
506495

507496
Exit:
508497
return np_send_ok_status();
@@ -577,8 +566,7 @@ static int _np_cmd_nand_read(np_prog_t *prog)
577566
{
578567
ERROR_PRINT("Read address 0x%lx is more then chip size 0x%lx\r\n",
579568
read_cmd->addr, prog->chip_info->size);
580-
np_send_error(NP_ERR_ADDR_EXCEEDED);
581-
return -1;
569+
return NP_ERR_ADDR_EXCEEDED;
582570
}
583571

584572
addr = read_cmd->addr;
@@ -590,10 +578,7 @@ static int _np_cmd_nand_read(np_prog_t *prog)
590578
while (read_cmd->len)
591579
{
592580
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;
597582

598583
while (page.offset < prog->chip_info->page_size && read_cmd->len)
599584
{
@@ -627,8 +612,7 @@ static int _np_cmd_nand_read(np_prog_t *prog)
627612
{
628613
ERROR_PRINT("Read address 0x%lx is more then chip size 0x%lx",
629614
addr, prog->chip_info->page_size);
630-
np_send_error(NP_ERR_ADDR_EXCEEDED);
631-
return -1;
615+
return NP_ERR_ADDR_EXCEEDED;
632616
}
633617
page.page++;
634618
page.offset = 0;
@@ -665,8 +649,7 @@ static int np_cmd_nand_select(np_prog_t *prog)
665649
prog->chip_info = NULL;
666650

667651
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;
670653
}
671654

672655
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,
686669
break;
687670
case NAND_ERROR:
688671
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;
691673
case NAND_TIMEOUT_ERROR:
692674
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;
695676
default:
696677
ERROR_PRINT("Unknown NAND status\r\n");
697-
np_send_error(NP_ERR_NAND_RD);
698-
return -1;
678+
return NP_ERR_NAND_RD;
699679
}
700680

701681
if (bad_block_data != NP_NAND_GOOD_BLOCK_MARK)
@@ -775,15 +755,13 @@ static int np_cmd_handler(np_prog_t *prog)
775755
if (!prog->chip_info && cmd->code != NP_CMD_NAND_SELECT)
776756
{
777757
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;
780759
}
781760

782761
if (!np_cmd_is_valid(cmd->code))
783762
{
784763
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;
787765
}
788766

789767
if (cmd_handler[cmd->code].exec(prog))
@@ -794,16 +772,21 @@ static int np_cmd_handler(np_prog_t *prog)
794772

795773
static void np_packet_handler(np_prog_t *prog)
796774
{
775+
int ret;
776+
797777
do
798778
{
799779
np_comm_cb->peek(&prog->rx_buf);
800780

801781
if (!prog->rx_buf)
802782
break;
803783

804-
np_cmd_handler(prog);
784+
ret = np_cmd_handler(prog);
805785

806786
np_comm_cb->consume();
787+
788+
if (ret < 0)
789+
np_send_error(-ret);
807790
}
808791
while (1);
809792
}

0 commit comments

Comments
 (0)