@@ -508,6 +508,8 @@ int jtagInit(void)
508
508
outpin_init (TDI );
509
509
outpin_init (TCK );
510
510
511
+ mbPinSet ();
512
+
511
513
port_pin_set_output_level (TMS , 1 );
512
514
port_pin_set_output_level (TDI , 1 );
513
515
port_pin_set_output_level (TCK , 0 );
@@ -575,7 +577,7 @@ int jtagWriteBuffer(unsigned int address, const uint8_t *data, size_t len)
575
577
Js_Shiftdr ();
576
578
address = (address << 2 ) | 0x00000003 ;
577
579
ReadTDOBuf (32 , & address , 0 , 0 );
578
- ReadTDOBuf (32 * len , data , 0 , 0 );
580
+ ReadTDOBuf (32 * len + 2 , data , 0 , 0 );
579
581
return len ;
580
582
}
581
583
@@ -610,7 +612,72 @@ int jtagReadBuffer(unsigned int address, uint8_t *data, size_t len)
610
612
return len ;
611
613
}
612
614
613
- #define MB_BASE 0x00000000
615
+ #define MB_BASE 0x00000000
616
+ #define MB_TIMEOUT 5000
617
+
618
+ /**
619
+ */
620
+ int mbPinSet (void )
621
+ {
622
+ #ifdef MB_INT
623
+ uint32_t rpc [1 ];
624
+ rpc [0 ] = 0 ;
625
+ jtagWriteBuffer (MB_BASE , (const uint8_t * )rpc , 1 );
626
+ outpin_init (MB_INT );
627
+ outpin_off (MB_INT );
628
+ #endif
629
+ }
630
+
631
+ /**
632
+ * Sends len words (32 bit) via messagebox
633
+ */
634
+ int mbCmdSend (uint32_t * data , int len )
635
+ {
636
+ int ret ;
637
+ #ifdef MB_INT
638
+ ret = jtagWriteBuffer (MB_BASE , (const uint8_t * )data , len );
639
+ if (ret != len ) {
640
+ return -10 ;
641
+ }
642
+ outpin_on (MB_INT );
643
+ outpin_off (MB_INT );
644
+ #else
645
+ jtagWriteBuffer (MB_BASE + 1 , (const uint8_t * )(& data [1 ]), len - 1 );
646
+ jtagWriteBuffer (MB_BASE , (const uint8_t * )data , 1 );
647
+ #endif
648
+
649
+ do {
650
+ jtagReadBuffer (MB_BASE , (uint8_t * )& ret , 1 );
651
+ } while (ret );
652
+
653
+ jtagReadBuffer (MB_BASE + 1 , (uint8_t * )& ret , 1 );
654
+
655
+ return ret ;
656
+ }
657
+
658
+ /**
659
+ * Writes len words (32 bit) via messagebox at a specified address
660
+ */
661
+ int mbWrite (uint32_t address , void * data , int len )
662
+ {
663
+ jtagWriteBuffer (MB_BASE + address , (const uint8_t * )data , len );
664
+ return 0 ;
665
+ }
666
+
667
+ /**
668
+ * Reads len words (32 bit) using messagebox from a specified address
669
+ */
670
+ int mbRead (uint32_t address , void * data , int len )
671
+ {
672
+ uint32_t * p = (uint32_t * )data ;
673
+ int i ;
674
+
675
+ for (i = 0 ; i < len ; i ++ ) {
676
+ jtagReadBuffer (MB_BASE + address + i , (uint8_t * )& p [i ], 1 );
677
+ }
678
+ return 0 ;
679
+ }
680
+
614
681
#define MB_DEV_FLASH 0x01000000
615
682
616
683
void jtagFlashEraseBlock (uint32_t offset )
@@ -620,12 +687,7 @@ void jtagFlashEraseBlock(uint32_t offset)
620
687
rpc [1 ] = 2 ;
621
688
rpc [2 ] = offset ;
622
689
623
- jtagWriteBuffer (MB_BASE + 1 , (const uint8_t * )(& rpc [1 ]), 2 );
624
- jtagWriteBuffer (MB_BASE , (const uint8_t * )rpc , 1 );
625
-
626
- do {
627
- jtagReadBuffer (MB_BASE , (uint8_t * )rpc , 1 );
628
- } while (rpc [0 ]);
690
+ mbCmdSend (rpc , 3 );
629
691
}
630
692
631
693
void jtagFlashWriteBlock (uint32_t offset , size_t len , uint32_t * data )
@@ -635,23 +697,7 @@ void jtagFlashWriteBlock(uint32_t offset, size_t len, uint32_t* data)
635
697
rpc [1 ] = offset ;
636
698
rpc [2 ] = len ;
637
699
memcpy (& rpc [3 ], data , len );
638
-
639
- jtagWriteBuffer (MB_BASE + 1 , (const uint8_t * )(& rpc [1 ]), 2 + (len + 3 ) / 4 );
640
- jtagWriteBuffer (MB_BASE , (const uint8_t * )rpc , 1 );
641
-
642
- do {
643
- jtagReadBuffer (MB_BASE , (uint8_t * )rpc , 1 );
644
- } while (rpc [0 ]);
645
- }
646
-
647
- static unsigned char lookup [16 ] = {
648
- 0x0 , 0x8 , 0x4 , 0xc , 0x2 , 0xa , 0x6 , 0xe ,
649
- 0x1 , 0x9 , 0x5 , 0xd , 0x3 , 0xb , 0x7 , 0xf ,
650
- };
651
-
652
- static uint8_t reverse (uint8_t n ) {
653
- // Reverse the top and bottom nibble then swap them.
654
- return (lookup [n & 0b1111 ] << 4 ) | lookup [n >> 4 ];
700
+ mbCmdSend (rpc , 3 + ((len + 3 )/4 ));
655
701
}
656
702
657
703
void jtagFlashReadBlock (uint32_t offset , size_t len , uint8_t * buf )
@@ -661,17 +707,8 @@ void jtagFlashReadBlock(uint32_t offset, size_t len, uint8_t* buf)
661
707
rpc [1 ] = offset ;
662
708
rpc [2 ] = len ;
663
709
664
- jtagWriteBuffer (MB_BASE + 1 , (const uint8_t * )(& rpc [1 ]), 2 );
665
- jtagWriteBuffer (MB_BASE , (const uint8_t * )rpc , 1 );
666
-
667
- do {
668
- jtagReadBuffer (MB_BASE , (uint8_t * )rpc , 1 );
669
- } while (rpc [0 ]);
670
-
671
- size_t i ;
672
- for (i = 0 ; i < 1 + (len + 3 ) / 4 ; i ++ ) {
673
- jtagReadBuffer (MB_BASE + 2 + i , (uint8_t * )& rpc [2 + i ], 1 );
674
- }
710
+ mbCmdSend (rpc , 3 );
711
+ mbRead (2 , & rpc [2 ], (len + 3 ) / 4 + 1 );
675
712
676
713
uint8_t * newbuf = (uint8_t * )& rpc [3 ];
677
714
for (int i = 0 ; i < len ; i ++ ) {
0 commit comments