vt340

jay

Dec 26th, 2020 (edited)
60
246 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. __xdata struct { uint8_t r [ 252 ], t [ 124 ]; } jay_buf;
  2.         struct { uint8_t r,         t;         } jay_ind, jay_len;
  3. uint16_t jay_numbits;
  4.  
  5. const uint8_t jay_caps     [ 4 ] = { 3, 8, 2 };         /* hw_ver free_mem sel_if */
  6. const uint8_t jay_free_mem [ 4 ] = { 0xFC };            /* 252 */
  7. const uint8_t jay_hw_stat  [ 8 ] = { 0x5, 0xD };        /* 3333 */
  8. const uint8_t jay_hw_ver   [ 4 ] = { 0xD1, 0x2F, 1 };   /* 77777 */
  9. const uint8_t jay_sel_if   [ 4 ] = { 2 };               /* mask */
  10. const uint8_t jay_ver   [ 0x22 ] = "\x20\x00Jay CH55x";
  11.  
  12. void jay_init ( void ) {
  13.     GPIO_SWCLOCK( 0 );
  14.     GPIO_SWCLOCK_MODE();
  15.     GPIO_SWDIN_MODE();
  16.     jay_ind.r = jay_len.r = 0;
  17.     jay_ind.t = jay_len.t = 0;
  18. }
  19.  
  20. void jay_reply ( uint8_t *buf, uint8_t len ) {
  21.     jay_ind.t = jay_len.t = 0;
  22.     for( uint8_t i = 0; i < len; i++ ) jay_buf.t[ jay_len.t++ ] = buf[ i ];
  23. }
  24.  
  25. uint8_t jay_protocol ( void ) {
  26.     switch( jay_buf.r[ 0 ] ) {
  27.         case 0x01 : jay_reply( jay_ver, sizeof( jay_ver ));
  28.                     break;
  29.         case 0x07 : jay_reply( jay_hw_stat, sizeof( jay_hw_stat ));
  30.                     break;
  31.         case 0xC7 : jay_reply( jay_sel_if, sizeof( jay_sel_if ));
  32.                     if( jay_buf.r[ 1 ] != 0xFF ) jay_buf.t[ 0 ] = 1;
  33.                     break;
  34.         case 0xCF : jay_numbits = *(( uint16_t* ) &jay_buf.r[ 2 ] );
  35.                     return 4 + 2 * (( jay_numbits + 7 ) >> 3 );
  36.         case 0xD4 : jay_reply( jay_free_mem, sizeof( jay_free_mem ));
  37.                     break;
  38.         case 0xDC : GPIO_RESET( 0 );
  39.                     break;
  40.         case 0xDD : GPIO_RESET( 1 );
  41.                     break;
  42.         case 0xE8 : jay_reply( jay_caps, sizeof( jay_caps ));
  43.                     break;
  44.         case 0xF0 : jay_reply( jay_hw_ver, sizeof( jay_hw_ver ));
  45.     }
  46.     return 0;
  47. }
  48.  
  49. void jay_swd ( void ) {
  50.     uint8_t dir, in, mask, out;
  51.     dir = in = mask = out = 0;
  52.     jay_ind.r = 4;
  53.     jay_len.r = 4 + (( jay_numbits + 7 ) >> 3 );
  54.     jay_ind.t = jay_len.t = 0;
  55.     while( jay_numbits-- ) {
  56.         if( mask == 0 ) {
  57.             mask = 1;
  58.             dir = jay_buf.r[ jay_ind.r++ ];
  59.             out = jay_buf.r[ jay_len.r++ ];
  60.             in = 0;
  61.         }
  62.         if( dir & mask ) {
  63.             GPIO_SWDOUT_MODE();
  64.             GPIO_SWDOUT( out & mask );
  65.         } else {
  66.             GPIO_SWDIN_MODE();
  67.         }
  68.         GPIO_SWCLOCK( 0 ); GPIO_DELAY();
  69.         GPIO_SWCLOCK( 1 ); GPIO_DELAY();
  70.         in |= GPIO_SWDIN() ? mask : 0;
  71.         mask <<= 1;
  72.         if( mask == 0 ) jay_buf.t[ jay_len.t++ ] = in;
  73.     }
  74.     if( mask ) jay_buf.t[ jay_len.t++ ] = in;
  75.     jay_buf.t[ jay_len.t++ ] = 0;
  76.     GPIO_SWCLOCK( 0 );
  77. }
  78.  
  79. void jay_poll ( uint8_t *rbuf, uint8_t *rlen, uint8_t *tbuf, uint8_t *tlen ) {
  80.     uint8_t i, len;
  81.     if( jay_len.t ) {
  82.         if( *tlen == 0 ) {
  83.             len = jay_len.t - jay_ind.t;
  84.             if( len > 64 ) len = 64; else if( len > 1 ) len--;
  85.             for( i = 0; i < len; i++ ) tbuf[ i ] = jay_buf.t[ jay_ind.t++ ];
  86.             if( jay_ind.t >= jay_len.t ) jay_ind.t = jay_len.t = 0;
  87.             *tlen = len;
  88.         }
  89.     } else if( *rlen ) {
  90.         len = *rlen;
  91.         for( i = 0; i < len; i++ ) jay_buf.r[ jay_ind.r++ ] = rbuf[ i ];
  92.         if( jay_len.r == 0 ) jay_len.r = jay_protocol();
  93.         if( jay_ind.r >= jay_len.r ) {
  94.             if( jay_buf.r[ 0 ] == 0xCF ) jay_swd();
  95.             jay_ind.r = jay_len.r = 0;
  96.         }
  97.         *rlen = 0;
  98.     }
  99. }
  100.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×