What´s your hardware? I have for fun a FTDI UM232H and a LED-strip WS2801. This is code for
and a "running light", 64-bit-version (no problem to convert in 32, comments in german):
Code: Select all
OutputBuffer.q
OutputBuffer1.q ;für "schwarz"
InputBuffer.q
ftHandle.l
NumBytesToSend.l
NumBytesRead.l
NumInputBuffer.l
ftStatus.l
numDevs.l
BytesToWrite.l
BytesWritten.l
ClockDivisor.l = 29 ;60/((1+29)*2) (MHz) = 1Mhz
AnzLED.l = 32 ;anpassen!
R.a
G.a
B.a
DLLOK = OpenLibrary(0, "ftd2xx64.dll") ;64-Bit-Version laden
If DLLOK
Prototype.i ProtoFT_1(V1l.l) ;für die 1-Parameter-Funktionen
CreateDeviceInfoList_FT.ProtoFT_1 = GetFunction(0, "FT_CreateDeviceInfoList")
Close_FT.ProtoFT_1 = GetFunction(0, "FT_Close")
ResetDevice_FT.ProtoFT_1 = GetFunction(0, "FT_ResetDevice")
Prototype.i ProtoFT_2(V1l.l, V2l.l) ;für die 2-Parameter-Funktionen
Open_FT.ProtoFT_2 = GetFunction(0, "FT_Open")
GetQueueStatus_FT.ProtoFT_2 = GetFunction(0, "FT_GetQueueStatus")
SetLatencyTimer_FT.ProtoFT_2 = GetFunction(0, "FT_SetLatencyTimer")
Prototype.i ProtoFT_3(V1l.l, V2l.l, V3l.l) ;für die 3-Parameter-Funktionen
SetUSBParameters_FT.ProtoFT_3 = GetFunction(0, "FT_SetUSBParameters")
SetTimeouts_FT.ProtoFT_3 = GetFunction(0, "FT_SetTimeouts")
SetBitMode_FT.ProtoFT_3 = GetFunction(0, "FT_SetBitMode")
Prototype.i ProtoFT_4(V1l.l, V2l.l, V3l.l, V4l.l) ;für die 4-Parameter-Funktionen
Write_FT.ProtoFT_4 = GetFunction(0, "FT_Write")
Read_FT.ProtoFT_4 = GetFunction(0, "FT_Read")
Prototype.i ProtoFT_5(V1l.l, V2l.l, V3l.l, V4l.l, V5l.l) ;für die 5-Parameter-Funktionen
SetChars_FT.ProtoFT_5 = GetFunction(0, "FT_SetChars")
ftStatus = CreateDeviceInfoList_FT(@numDevs); : Debug ftStatus : Debug numDevs
CDIL$ = "CreateDeviceInfoList: "
If ftStatus = 0 And numDevs > 0
Ergebnis$ = CDIL$ + "O.K." + #LFCR$ + Str(numDevs) + " Device(s) gefunden!" + #LFCR$
MessageRequester("Ergebnisse FT232H", Ergebnis$)
Else
Ergebnis$ = CDIL$ + "Kein Device gefunden! Ende!"
CloseLibrary(0)
MessageRequester("Ergebnisse FT232H", Ergebnis$)
End
EndIf
OutputBuffer = AllocateMemory(1024)
OutputBuffer1 = AllocateMemory(1024) ;für LEDs "aus" (schwarz)
InputBuffer = AllocateMemory(1024)
ftStatus = Open_FT(0, @ftHandle) : Debug ftStatus : Debug ftHandle ;0=Device-Nr.1
Ergebnis$ + "Handle: " + Str(ftHandle) + #LFCR$
;Configure FTDI Port For MPSSE Use
ftStatus = ResetDevice_FT(ftHandle) : Debug "ResetDevice = " + Str(ftStatus)
ftStatus = GetQueueStatus_FT(ftHandle, @NumInputBuffer) : Debug ftStatus : Debug "Im Buffer2: " + Str(NumInputBuffer)
ftStatus = SetUSBParameters_FT(ftHandle, $FFFF, $FFFF) : Debug ftStatus
ftStatus = SetChars_FT(ftHandle, 0, 0, 0, 0) : Debug ftStatus
ftStatus = SetTimeouts_FT(ftHandle, 0, 3000) : Debug ftStatus
ftStatus = SetLatencyTimer_FT(ftHandle, 1) : Debug ftStatus
ftStatus = SetBitMode_FT(ftHandle, 0, 0) : Debug ftStatus ;Reset Controller
ftStatus = SetBitMode_FT(ftHandle, 0, 2) : Debug ftStatus ;Enable MPSSE mode
Delay(50)
;Enable internal loop-back
PokeA(OutputBuffer, $84)
BytesToWrite = 1;NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben loop-back: " + Str(BytesWritten)
;Ergebnis$ + "Geschriebe Bytes für SPI_Enable : " + Str(BytesWritten) + #LFCR$
;Synchronize the MPSSE by sending a bogus opcode (0xAB),
;The MPSSE will respond with "Bad Command" (0xFA) followed by the bogus opcode itself.
PokeA(OutputBuffer, $AB)
BytesToWrite = 1;NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben Bad Command: " + Str(BytesWritten)
;Delay(100)
NumBytesRead = 0
While NumBytesRead <> 2
ftStatus = GetQueueStatus_FT(ftHandle, @NumInputBuffer) : Debug ftStatus : Debug "Im Buffer3: " + Str(NumInputBuffer)
ftStatus = Read_FT(ftHandle, InputBuffer, NumInputBuffer, @NumBytesRead)
ftStatus = GetQueueStatus_FT(ftHandle, @NumInputBuffer) : Debug ftStatus : Debug "Im Buffer : " + Str(NumInputBuffer)
Wend
Debug Hex(PeekA(InputBuffer)) ;$FA
Debug Hex(PeekA(InputBuffer + 1)) ;$AB
;Disable internal loop-back
PokeA(OutputBuffer, $85)
BytesToWrite = 1;NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben loop-back: " + Str(BytesWritten)
;MPSSE Setup
;Configure the MPSSE settings For JTAG
;Multple commands can be sent To the MPSSE With one FT_Write
NumBytesToSend = 0
PokeA(OutputBuffer + NumBytesToSend, $8A) ;Use 60MHz master clock (disable divide by 5)
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $97) ;Turn off adaptive clocking (may be needed for ARM)
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $8D) ;Disable three-phase clocking
NumBytesToSend + 1
BytesToWrite = NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben Configure the MPSSE: " + Str(BytesWritten)
;Set TCK frequency
NumBytesToSend = 0
PokeA(OutputBuffer + NumBytesToSend, $86) ;Command to set clock divisor
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, ClockDivisor & $FF) ;Set 0xValueL of clock divisor
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, (ClockDivisor >> 8) & $FF) ;Set 0xValueH of clock divisor
NumBytesToSend + 1
BytesToWrite = NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben TCK frequency: " + Str(BytesWritten)
;// Set initial states of the MPSSE Interface
;// - low byte, both pin directions And output values
;// Pin name Signal Direction Config Initial State Config
;// ADBUS0 TCK/SK output 1 high 1 Bit0
;// ADBUS1 TDI/DO output 1 low 0
;// ADBUS2 TDO/DI input 0 0
;// ADBUS3 TMS/CS output 1 high 1
;// ADBUS4 GPIOL0 output 1 low 0
;// ADBUS5 GPIOL1 output 1 low 0
;// ADBUS6 GPIOL2 output 1 high 1
;// ADBUS7 GPIOL3 output 1 high 1 Bit7
; C9 Wert des Bytes
;Set initial states of the MPSSE interface
NumBytesToSend = 0
PokeA(OutputBuffer + NumBytesToSend, $80) ;Configure data bits low-byte of MPSSE port
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $09) ;$C9 Initial state config above
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $0B) ;$FB Direction config above
NumBytesToSend + 1
BytesToWrite = NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben initial states1: " + Str(BytesWritten)
;SPI_Enable
; NumBytesToSend = 0
; For i = 0 To 4 ;one 0x80 command can keep 0.2us, do 5 times to stay in this situation for 1µs
; PokeA(OutputBuffer + NumBytesToSend, $80) ;GPIO command for ADBUS
; NumBytesToSend + 1
; PokeA(OutputBuffer + NumBytesToSend, $08) ;set CS high, MOSI and SCL low
; NumBytesToSend + 1
; PokeA(OutputBuffer + NumBytesToSend, $0B) ;bit3:CS, bit2:MISO, bit1:MOSI, bit0:SCK
; NumBytesToSend + 1
; Next
;;Debug NumBytesToSend
; BytesToWrite = NumBytesToSend
; ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten); : Debug ftStatus : Debug "Geschrieben1: " + Str(BytesWritten)
; Ergebnis$ + "Geschriebe Bytes für SPI_Enable : " + Str(BytesWritten) + #LFCR$
;// Set initial states of the MPSSE Interface
;// - high byte, both pin directions And output values
;// Pin name Signal Direction Config Initial State Config
;// ACBUS0 GPIOH0 input 0 0
;// ACBUS1 GPIOH1 input 0 0
;// ACBUS2 GPIOH2 input 0 0
;// ACBUS3 GPIOH3 input 0 0
;// ACBUS4 GPIOH4 input 0 0
;// ACBUS5 GPIOH5 input 0 0
;// ACBUS6 GPIOH6 input 0 0
;// ACBUS7 GPIOH7 input 0 0
NumBytesToSend = 0
PokeA(OutputBuffer + NumBytesToSend, $82) ;Configure data bits low-byte of MPSSE port
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, 0) ;
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $0) ;
NumBytesToSend + 1
BytesToWrite = NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben initial states2: " + Str(BytesWritten)
NumBytesToSend = 0
PokeA(OutputBuffer + NumBytesToSend, $10) ;Output on rising clock, no input, MSB first, clock a number of bytes out
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, (AnzLED * 3) - 1) ;Length L der Daten! -1
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, 0) ;Length H
NumBytesToSend + 1
For j = 0 To (AnzLED >> 2) - 1
PokeA(OutputBuffer + NumBytesToSend, $ff) ;rot
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $00) ;grün
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $00) ;blau
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $00) ;rot
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $ff) ;grün
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $00) ;blau
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $00) ;rot
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $00) ;grün
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $ff) ;blau
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $ff) ;rot
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $ff) ;grün
NumBytesToSend + 1
PokeA(OutputBuffer + NumBytesToSend, $ff) ;blau
NumBytesToSend + 1
Next
BytesToWrite = NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben: " + Str(BytesWritten)
OpenWindow(0, 0, 0, 220, 70, "Test LED-Kette mit FT232_SPI", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(0, 10, 10, 200, 20, "LED-Lauf-Licht")
ButtonGadget(1, 10, 40, 200, 20, "Ende, LEDs aus", #PB_Button_Default)
;alle LEDs aus, OutputBuffer1 ist mit 0 initialisiert!
NumBytesToSend = 0
PokeA(OutputBuffer1 + NumBytesToSend, $10) ;Output on rising clock, no input, MSB first, clock a number of bytes out
NumBytesToSend + 1
PokeA(OutputBuffer1 + NumBytesToSend, (AnzLED * 3) - 1) ;Length L der Daten! -1
NumBytesToSend + 1
PokeA(OutputBuffer1 + NumBytesToSend, 0) ;Length H
NumBytesToSend + 1
;NumBytesToSend = 3
;For j = 0 To (AnzLED * 3) - 1
; PokeA(OutputBuffer1 + NumBytesToSend, $00) ;schwarz
; NumBytesToSend + 1
;Next
BytesToWrite = (AnzLED * 3) + 3
ftStatus = Write_FT(ftHandle, OutputBuffer1, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben!!!: " + Str(BytesWritten)
Repeat
EventID = WaitWindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 0
Repeat
EventID = WindowEvent()
If EventID = #PB_Event_Gadget
Select EventGadget()
Case 1
Break 2
EndSelect
EndIf
PokeA(@B, PeekA(OutputBuffer + (AnzLED * 3) + 2))
PokeA(@G, PeekA(OutputBuffer + (AnzLED * 3) + 1))
PokeA(@R, PeekA(OutputBuffer + AnzLED * 3))
For i = 0 To AnzLED - 2
PokeA(OutputBuffer + (AnzLED * 3) + 2 - (i * 3), PeekA(OutputBuffer + (AnzLED * 3) - 1 - (i * 3)))
PokeA(OutputBuffer + (AnzLED * 3) + 1 - (i * 3), PeekA(OutputBuffer + (AnzLED * 3) - 2 - (i * 3)))
PokeA(OutputBuffer + AnzLED * 3 - (i * 3), PeekA(OutputBuffer + (AnzLED * 3) - 3 - (i * 3)))
Next
PokeA(OutputBuffer + 5, B)
PokeA(OutputBuffer + 4, G)
PokeA(OutputBuffer + 3, R)
BytesToWrite = (AnzLED * 3) + 3;NumBytesToSend
ftStatus = Write_FT(ftHandle, OutputBuffer, BytesToWrite, @BytesWritten); : Debug ftStatus
Delay(1000)
ForEver
Case 1
Break
EndSelect
ElseIf EventID = #PB_Event_CloseWindow
Break
EndIf
ForEver
;alle LEDs aus, s.oben
BytesToWrite = (AnzLED * 3) + 3
ftStatus = Write_FT(ftHandle, OutputBuffer1, BytesToWrite, @BytesWritten) : Debug ftStatus : Debug "Geschrieben: " + Str(BytesWritten)
FreeMemory(OutputBuffer)
FreeMemory(OutputBuffer1)
FreeMemory(InputBuffer)
ftStatus = Close_FT(ftHandle) : Debug ftStatus
CloseLibrary(0)
Else
MessageRequester("Fehler!", "ftd2xx64.dll nicht gefunden!")
End
EndIf
;MessageRequester("Ergebnisse FT232H", Ergebnis$)
End
;FT_STATUS (DWORD):
;FT_OK = 0
;FT_INVALID_HANDLE = 1
;FT_DEVICE_NOT_FOUND = 2
;FT_DEVICE_NOT_OPENED = 3
;FT_IO_ERROR = 4
;FT_INSUFFICIENT_RESOURCES = 5
;FT_INVALID_PARAMETER = 6
;FT_INVALID_BAUD_RATE = 7
;FT_DEVICE_NOT_OPENED_FOR_ERASE = 8
;FT_DEVICE_NOT_OPENED_FOR_WRITE = 9
;FT_FAILED_TO_WRITE_DEVICE = 10
;FT_EEPROM_READ_FAILED = 11
;FT_EEPROM_WRITE_FAILED = 12
;FT_EEPROM_ERASE_FAILED = 13
;FT_EEPROM_NOT_PRESENT = 14
;FT_EEPROM_NOT_PROGRAMMED = 15
;FT_INVALID_ARGS = 16
;FT_NOT_SUPPORTED = 17
;FT_OTHER_ERROR = 18