tom@zzz:~/staapl/staapl/pickit2$ darcs record -m 'first pk2 comm working' hunk ./doc/staapl-blog.txt 1677 +Entry: USB +Date: Tue Aug 26 15:28:15 CEST 2008 + +I'm writing some support code for the ``Ubiquitous'' Serial Bus. The +problem with usb is its (too) complex setup procedure. It is quite +general, and has a lot of overhead when you want to do simple things +with little meta-data overhead. It seems that this is the main reason +most people stick to the HID subsystem, moreover because this does not +require device driver developent. + +I'm looking at this from two perspectives: + + * Need to support USB PICkit2 through libusb/FFI + * Client code for the PIC18 + +The host code necessary to drive the PICkit2 is interesting to figure +out how things work, while the client code for the PIC18 is an +interesting excercise for code generation from a higher level +description. I've attempted this before without much success. + +The eventual state of a USB connection is data packets moving back and +forth between virtual pipes called ENDPOINTs. [_$_] + Shall I record this change? (1/16) [ynWsfvpxdaqjk], or ? for help: a Finished recording patch 'first pk2 comm working' tom@zzz:~/staapl/staapl/pickit2$ push zwizwa --- brood-5 Reading pristine 44/295 Tue Aug 26 19:31:53 CEST 2008 tom@zwizwa.be * first pk2 comm working Tue Aug 26 18:05:23 CEST 2008 tom@zwizwa.be * pickit2 Finished applying... --- sweb --- pool --- snot --- pr --- papers --- packetforth tom@zzz:~/staapl/staapl/pickit2$ darcs record -m blog hunk ./doc/staapl-blog.txt 1680 -I'm writing some support code for the ``Ubiquitous'' Serial Bus. The +I'm writing some support code for the Univeral Serial Bus. The Shall I record this change? (1/4) [ynWsfvpxdaqjk], or ? for help: a Finished recording patch 'blog' tom@zzz:~/staapl/staapl/pickit2$ darcs wh No changes! tom@zzz:~/staapl/staapl/pickit2$ darcs amend-record Tue Aug 26 19:37:40 CEST 2008 tom@zwizwa.be * blog Shall I amend this patch? [yNvpxq], or ? for help: a Invalid response, try again! Shall I amend this patch? [yNvpxq], or ? for help: y hunk ./doc/staapl-ramblings.txt 36201 -TueEntry: PK2 replies +Entry: PK2 replies Shall I add this change? (1/1) [ynWsfvpxdaqjk], or ? for help: a Finished amending patch: Tue Aug 26 19:39:30 CEST 2008 tom@zwizwa.be * blog tom@zzz:~/staapl/staapl/pickit2$ push giebrok --- brood-5 Reading pristine 31/251 Writing pristine 72/79 Tue Aug 26 19:39:30 CEST 2008 tom@zwizwa.be * blog Tue Aug 26 19:31:53 CEST 2008 tom@zwizwa.be * first pk2 comm working Tue Aug 26 18:05:23 CEST 2008 tom@zwizwa.be * pickit2 Tue Aug 26 15:03:53 CEST 2008 tom@zwizwa.be * fixes Tue Aug 26 14:49:12 CEST 2008 tom@zwizwa.be * libusb backup Finished applying... --- sweb --- pool --- snot --- pr --- papers --- packetforth tom@zzz:~/staapl/staapl/pickit2$ pk2cmd -I -ppic18f1220 Device ID = 0000 Operation Succeeded tom@zzz:~/staapl/staapl/pickit2$ pk2cmd -I -ppic18f1220 Device ID = 0000 VDD Error detected. Check target for proper connectivity. tom@zzz:~/staapl/staapl/pickit2$ pk2cmd -I -ppic18f1220 Device ID = 0000 Operation Succeeded tom@zzz:~/staapl/staapl/pickit2$ touch pk2script.ss tom@zzz:~/staapl/staapl/pickit2$ darcs add pk2script.ss tom@zzz:~/staapl/staapl/pickit2$ darcs record -m scriptstuff . Recording changes in "staapl/pickit2": hunk ./staapl/pickit2/pk2const.ss 30 + +;; Can it run Forth? + + +(define (make-pk2-opcode opcode nargs name) + (lambda args + (define (check condition) + (unless condition (error 'pk2-invalid-argument "~a" (cons name args)))) + (case nargs + ((255) (list* opcode (length args) args)) ;; variable, first arg is count + ((254) (list* (car args) (length args) (cdr args))) ;; script #, then count + (else + (check (= nargs (length args))) + (cons opcode args))))) + +(define-syntax-rule (pk2-instruction-set (opcode nargs name) ...) + (make-immutable-hash + (list (cons 'name (make-pk2-opcode opcode nargs 'name)) ...))) + +(define pk2-ns + (pk2-instruction-set + (#xff 0 VDD_ON) + (#xfe 0 VDD_OFF) + (#xfd 0 VDD_GND_ON) + (#xfc 0 VDD_GND_OFF) + (#xfb 0 VPP_ON) + (#xfa 0 VPP_OFF) + (#xf9 0 VPP_PWM_ON) + (#xf8 0 VPP_PWM_OFF) + (#xf7 0 MCLR_GND_ON) + (#xf6 0 MCLR_GND_OFF) + (#xf5 0 BUSY_LED_ON) + (#xf4 0 BUSY_LED_OFF) + (#xf3 1 SET_ICSP_PINS) + (#xf2 1 WRITE_BYTE_LITERAL) + (#xf1 0 WRITE_BYTE_BUFFER) + (#xf0 0 READ_BYTE_BUFFER) + (#xef 0 READ_BYTE) + (#xee 2 WRITE_BITS_LITERAL) + (#xed 1 WRITE_BITS_BUFFER) + (#xec 1 READ_BITS_BUFFER) + (#xeb 1 READ_BITS) + (#xea 1 SET_ICSP_SPEED) + (#xe9 2 LOOP) + (#xe8 1 DELAY_LONG) + (#xe7 1 DELAY_SHORT) + (#xe6 2 IF_EQ_GOTO) + (#xe5 2 IF_GT_GOTO) + (#xe4 1 GOTO_INDEX) + (#xe3 0 EXIT_SCRIPT) + (#xe2 1 PEEK_SFR) + (#xe1 2 POKE_SFR) + (#xe0 0 ICDSLAVE_RX) + (#xdf 1 ICDSLAVE_TX_LIT) + (#xde 0 ICDSLAVE_TX_BUF) + (#xdd 1 LOOPBUFFER) + (#xdc 0 ICSP_STATES_BUFFER) + (#xdb 0 POP_DOWNLOAD) + (#xda 2 COREINST18) + (#xd9 3 COREINST24) + (#xd8 0 NOP24) + (#xd7 0 VISI24) + (#xd6 0 RD2_BYTE_BUFFER) + (#xd5 1 RD2_BITS_BUFFER) + (#xd4 1 WRITE_BUFWORD_W) + (#xd3 1 WRITE_BUFBYTE_W) + (#xd2 1 CONST_WRITE_DL) + (#xaf 0 SCRIPT_BUFFER_CHKSM) + (#xae 0 RESET) + (#xad 0 END_OF_BUFFER) + (#xac 0 UPLOAD_DATA_NOLEN) + (#xab 0 CLR_SCRIPT_BUFFER) + (#xaa 0 UPLOAD_DATA) + (#xa9 0 CLR_UPLOAD_BUFFER) + (#xa8 255 DOWNLOAD_DATA) + (#xa7 0 CLR_DOWNLOAD_BUFFER) + (#xa6 255 EXECUTE_SCRIPT) + (#xa5 2 RUN_SCRIPT) + (#xa4 254 DOWNLOAD_SCRIPT) + (#xa3 0 READ_VOLTAGES) + (#xa2 0 READ_STATUS) + (#xa1 3 SETVPP) + (#xa0 3 SETVDD) + (0 0 end-marker))) + Shall I record this change? (1/3) [ynWsfvpxdaqjk], or ? for help: a Finished recording patch 'scriptstuff' tom@zzz:~/staapl/staapl/pickit2$ darcs unrecord Tue Aug 26 20:29:53 CEST 2008 tom@zwizwa.be * scriptstuff Shall I unrecord this patch? (1/19) [ynWvpxdaqjk], or ? for help: y Tue Aug 26 19:39:30 CEST 2008 tom@zwizwa.be * blog Shall I unrecord this patch? (2/19) [ynWvpxdaqjk], or ? for help: d Finished unrecording. tom@zzz:~/staapl/staapl/pickit2$ darcs wh hunk ./doc/staapl-ramblings.txt 36210 - - hunk ./staapl/pickit2/pk2const.ss 30 + + addfile ./staapl/pickit2/pk2script.ss hunk ./staapl/pickit2/pk2script.ss 1 - +#lang scheme/base +;; Can it run Forth? + + +(define (make-pk2-opcode opcode nargs name) + (lambda args + (define (check condition) + (unless condition (error 'pk2-invalid-argument "~a" (cons name args)))) + (case nargs + ((255) (list* opcode (length args) args)) ;; variable, first arg is count + ((254) (list* (car args) (length args) (cdr args))) ;; script #, then count + (else + (check (= nargs (length args))) + (cons opcode args))))) + +(define-syntax-rule (pk2-instruction-set (opcode nargs name) ...) + (make-immutable-hash + (list (cons 'name (make-pk2-opcode opcode nargs 'name)) ...))) + +(define pk2-ns + (pk2-instruction-set + (#xff 0 VDD_ON) + (#xfe 0 VDD_OFF) + (#xfd 0 VDD_GND_ON) + (#xfc 0 VDD_GND_OFF) + (#xfb 0 VPP_ON) + (#xfa 0 VPP_OFF) + (#xf9 0 VPP_PWM_ON) + (#xf8 0 VPP_PWM_OFF) + (#xf7 0 MCLR_GND_ON) + (#xf6 0 MCLR_GND_OFF) + (#xf5 0 BUSY_LED_ON) + (#xf4 0 BUSY_LED_OFF) + (#xf3 1 SET_ICSP_PINS) + (#xf2 1 WRITE_BYTE_LITERAL) + (#xf1 0 WRITE_BYTE_BUFFER) + (#xf0 0 READ_BYTE_BUFFER) + (#xef 0 READ_BYTE) + (#xee 2 WRITE_BITS_LITERAL) + (#xed 1 WRITE_BITS_BUFFER) + (#xec 1 READ_BITS_BUFFER) + (#xeb 1 READ_BITS) + (#xea 1 SET_ICSP_SPEED) + (#xe9 2 LOOP) + (#xe8 1 DELAY_LONG) + (#xe7 1 DELAY_SHORT) + (#xe6 2 IF_EQ_GOTO) + (#xe5 2 IF_GT_GOTO) + (#xe4 1 GOTO_INDEX) + (#xe3 0 EXIT_SCRIPT) + (#xe2 1 PEEK_SFR) + (#xe1 2 POKE_SFR) + (#xe0 0 ICDSLAVE_RX) + (#xdf 1 ICDSLAVE_TX_LIT) + (#xde 0 ICDSLAVE_TX_BUF) + (#xdd 1 LOOPBUFFER) + (#xdc 0 ICSP_STATES_BUFFER) + (#xdb 0 POP_DOWNLOAD) + (#xda 2 COREINST18) + (#xd9 3 COREINST24) + (#xd8 0 NOP24) + (#xd7 0 VISI24) + (#xd6 0 RD2_BYTE_BUFFER) + (#xd5 1 RD2_BITS_BUFFER) + (#xd4 1 WRITE_BUFWORD_W) + (#xd3 1 WRITE_BUFBYTE_W) + (#xd2 1 CONST_WRITE_DL) + (#xaf 0 SCRIPT_BUFFER_CHKSM) + (#xae 0 RESET) + (#xad 0 END_OF_BUFFER) + (#xac 0 UPLOAD_DATA_NOLEN) + (#xab 0 CLR_SCRIPT_BUFFER) + (#xaa 0 UPLOAD_DATA) + (#xa9 0 CLR_UPLOAD_BUFFER) + (#xa8 255 DOWNLOAD_DATA) + (#xa7 0 CLR_DOWNLOAD_BUFFER) + (#xa6 255 EXECUTE_SCRIPT) + (#xa5 2 RUN_SCRIPT) + (#xa4 254 DOWNLOAD_SCRIPT) + (#xa3 0 READ_VOLTAGES) + (#xa2 0 READ_STATUS) + (#xa1 3 SETVPP) + (#xa0 3 SETVDD) + (0 0 end-marker))) + +(define-syntax-rule (pk2-begin (rator . rands) ...) + (append ((hash-ref pk2-ns 'rator) . rands) ...)) tom@zzz:~/staapl/staapl/pickit2$ darcs add pk2script.ss The following file is already in the repository; note that to ensure portability we don't allow files that differ only in case. Use --case-ok to override this: staapl/pickit2/pk2script.ss darcs failed: No files were added tom@zzz:~/staapl/staapl/pickit2$ darcs record -m scriptstuff hunk ./doc/staapl-ramblings.txt 36210 - - Shall I record this change? (1/4) [ynWsfvpxdaqjk], or ? for help: a Finished recording patch 'scriptstuff' tom@zzz:~/staapl/staapl/pickit2$ push zwizwa --- brood-5 Writing inventory 1/3 : Tue Aug 26 20:30:42 CEST 2008 tom@zwizwa.be * scriptstuff Tue Aug 26 19:39:30 CEST 2008 tom@zwizwa.be * blog Finished applying... --- sweb --- pool --- snot --- pr --- papers --- packetforth tom@zzz:~/staapl/staapl/pickit2$