fl { MAX3241E driver PropForth 4.6 01/09/2011 22:15:08 MAX3241E Propeller SS ----- P16 MOSI ----- P17 RES ----- P18 GPX ----- P19 CLK ----- P20 MISO ----- P21 } hex 10 wconstant _ss 11 wconstant _mosi 12 wconstant _res 13 wconstant _gpx 14 wconstant _clk 15 wconstant _miso wvariable Set_Address_to_7 00 c, 05 c, 07 c, 00 c, 00 c, 00 c, 00 c, 00 c, wvariable Get_Descriptor_Device 80 c, 06 c, 00 c, 01 c, 00 c, 00 c, 00 c, 00 c, wvariable Get_Descriptor_Config 80 c, 06 c, 00 c, 02 c, 00 c, 00 c, 00 c, 00 c, wvariable str 80 c, 06 c, 00 c, 03 c, 00 c, 00 c, 00 c, 00 c, : _ss_out_l _ss pinlo ; : _ss_out_h _ss pinhi ; : _clk_out_l _clk pinlo ; : _clk_out_h _clk pinhi ; : _mosi_out_l _mosi pinlo ; : _mosi_out_h _mosi pinhi ; : _res_out_l _res pinlo ; : _res_out_h _res pinhi ; : _gpx_out_l _gpx pinlo ; : _gpx_out_h _gpx pinhi ; 1 _miso lshift constant _misom wvariable maxPacketSize wvariable status wvariable nak_count wvariable nak_limit wvariable retry_count wvariable IN_nak_count wvariable xfrsize wvariable xfrlen wvariable last_transfer_size wvariable VID wvariable PID wvariable iMFG wvariable iPROD wvariable iSERIAL wvariable iCONFIG wvariable XfrData 100 allot \ data array \ shift out command-byte (1byte most siginficant bit first) \ shift in status (1byte most siginficant bit first) \ ( n1 -- ) n1:register-number : send_command 0 status W! \ initial data 80 8 0 do 2dup and if _mosi_out_h \ ." 1" else _mosi_out_l \ ." 0" then _clk_out_h 1 rshift status W@ _misom _maskin if 1 or then 1 lshift status W! \ read status _clk_out_l loop 2drop ; \ write n1 to register \ ( n1 -- ) n1:register data : byte_write 80 8 0 do 2dup and if _mosi_out_h \ ." 1" else _mosi_out_l \ ." 0" then _clk_out_h 1 rshift _clk_out_l loop 2drop ; \ read data from register \ ( -- n1) n1:register-data : byte_read 0 8 0 do 1 lshift _clk_out_h _misom _maskin if 1 or then _clk_out_l loop ; \ send command-byte to register \ ( n1 n2 -- ) n1:register-data n2:register number : write_reg 3 lshift 2+ send_command byte_write ; \ read data from MAX3421E-register \ ( n1 -- n2 ) n1:register number n2:read-data : read_reg 3 lshift send_command byte_read ; \ write data to MAX3421E-register \ ( n1 n2 -- ) n1:register-data n2:register-number : Hwreg _ss_out_l write_reg _ss_out_h ; \ write datas to FIFO \ (n1 n2 n3 -- ) n1:start-address for datas n2:write-number n3:register-number : Hwritebytes _ss_out_l 3 lshift 2+ \ shift n3 to left 3bit and plus write-bit(0x2) send_command 0 do C@++ byte_write loop drop _ss_out_h ; \ read data from MAX3421E-register \ ( n1 -- n2 ) n1:register-number n2:read-data : Hrreg _ss_out_l read_reg _ss_out_h ; : init_host f _ss lshift outa COG@ or outa COG! _ss 5 0 do dup pinout 1+ loop drop _miso pinin \ reset max3421e _res_out_l _res_out_h \ register PINCTR(R17) FDUPSPI 10 11 Hwreg \ register MODE(R27) HOST 1 1b Hwreg \ f 14 Hwreg ; \ check chip-revision : revision 12 Hrreg . ; \ test GPIO : GPIO_test init_host begin 14 Hrreg 4 rshift 15 Hrreg f0 and or invert 1 8 0 do 2dup and if 1 i 4 u/mod drop lshift i 4 < if 0 15 Hwreg 14 else 0 14 Hwreg 15 then Hwreg then 1 lshift loop 2drop 0 until ; : wait_disconnect ." Waiting for device disconnect" cr \ register HIRQ(R25) CONDETIRQ 20 19 Hwreg \ wait until CONDETIRQ is "0" begin 19 Hrreg 20 and 0= until \ register MODE(R27) DPPULLDN DMPULLDN HOST c1 1b Hwreg ." Device disconnected" cr ; : detect_device \ register MODE(R27) DPPULLDN DMPULLDN HOST c1 1b Hwreg \ register HIRQ(R25) CONDETIRQ 20 19 Hwreg ." Waiting for device connect" cr begin \ register HCTL(R29) SAMPLEBUS 4 1d Hwreg \ register HRSL(R31) 1f Hrreg c0 and dup if 1 else drop 0 then until 80 = if \ register MODE(R27) DPPULLDN DMPULLDN HOST SOFKAENAB c9 1b Hwreg ." Full-Speed Device Detected" cr else \ register MODE(R27) DPPULLDN DMPULLDN HOST LOWSPEED SOFKAENAB cb 1b Hwreg ." Low-Speed Device Detected" cr then ; : waitframe \ register HIRQ(R25) FRAMEIRQ 40 19 Hwreg 0 do \ register HIRQ(R25) FRAMEIRQ wait until FRAMEIRQ is cleared begin 19 Hrreg 40 and if 1 else 0 then until 40 19 Hwreg loop ; \ send USB_packet \ ( n1 n2 -- n3 ) n1:token n2:endpoint number n3:resultcode HRSL(R31) \ token SETUP :0x10 \ BULK-IN :0x00 + ep \ BULK-OUT:0x20 + ep \ HS-IN :0x80 + ep \ HS-OUT :0xA0 + ep \ ISO-IN :0x40 + ep \ ISO-OUT :0x60 + ep : Send_Packet 0 nak_count W! 0 retry_count W! begin \ register HXFR(R30) or 1e Hwreg \ ." Send_Packet1" st? \ register HIRQ(R25) HXFRDNIRQ begin 19 Hrreg 80 and if 1 else 0 then until \ ." Send_Packet-1" st? \ wait until HXFRDNIRQ is set \ clear HXFRDNIRQ 80 19 Hwreg \ read register HRSL(R31)and get transfer^result 1f Hrreg f and dup \ ." Send_Packet2" st? \ check if peripheral reply hrNAK(0x04) 4 = if \ check if nak_count reach 200 1 nak_count W+! nak_count W@ c8 = \ ." nak_count" nak_count W@ . if 1 else drop 0 then else \ ." Send_Packet3" st? \ check if peripheral return hrTIMEOUT(0x0E) dup e = if \ check if retry_count reach 3 1 retry_count W+! retry_count W@ 3 = if 1 else drop 0 then \ ." retry_count" retry_count W@ . else 1 then then until \ ." Send_Packet4" st? ; \ \ ( n1 n2 -- n3) n1:endpoint n2:inbytes n3:resultcode HRSL(R31) : IN_Transfer \ ." IN_transfer0" st? xfrsize W! 0 xfrlen W! \ ." IN_transfer1" st? begin \ BULK-IN packet to EP* register HXFR dup 0 swap Send_Packet dup 0= \ ." IN_transfer2" st? \ it shoud be 0 when success if drop \ register RCVBC(R6) 6 Hrreg dup \ ." IN_transfer3" st? \ get packet size 0 do \ register RCVFIFO(R1) 1 Hrreg \ dup . \ add packet's data to XfrData array XfrData i + xfrlen W@ + C! loop \ register HIRQ(R25) RCVDAVIRQ 4 19 Hwreg \ st? \ ." IN_transfer4" st? dup xfrlen W+! maxPacketSize W@ < xfrlen W@ xfrsize W@ >= or \ ." IN_transfer5" st? if xfrlen W@ last_transfer_size W! drop 0 1 \ return resultcode 0 else 0 then else nip 1 \ return resultcode is not 0 then until \ ." IN_transfer6" st? ; \ send CONTROL \ (n1 -- n2 ) n1:Descriptor_Type's address n2:0(success) not zero(error code) : CTL_Read \ write Get_Descriptor_Device's datas to SUDFIFO(R4) 8 4 Hwritebytes \ ." CTL_Read0" st? \ setup packet to EP0 register HXFR SETUP 10 0 Send_Packet dup 0= \ ." CTL_Read1" st? \ it shoud be 0 if success if \ st? drop \ register HCTL(R29) RCVTOG1 20 1d Hwreg \ ." CTL_Read2" st? \ byte-length for data stage 0 Get_Descriptor_Device 2+ dup 6 + C@ swap 7 + C@ ff u* + IN_Transfer dup 0= \ ." CTL_Read3" st? \ it shoud be 0 when success if drop nak_count W@ IN_nak_count W! \ HS-OUT packet to EP0 register HXGR HS OUTMIN a0 0 Send_Packet thens \ ." CTL_Read4" st? ; \ ( n1 -- n2 ) n1:data's address n2:resultcode HRSL(R31) : CTL_Write_ND \ register SUDFIFO(R4) 8 4 Hwritebytes 10 0 Send_Packet dup 0= if drop 80 0 Send_Packet then ; \ display error-message \ ( n1 -- ) ni:error code : print_error ." >>>>> Error >>>>>" cr 1 over = if ." MAX3421E SIE is busy" else 2 over = if ." Bad value in HXFR register" else 4 over = if ." Exceeded NAK limit" else c over = if ." LS Timeout" else d over = if ." FS Timeout" else e over = if ." Device did not respond in time" else f over = if ." Device babbled (sent too long)" else ." Programing error " thens drop cr ; decimal