Skip to content

Instantly share code, notes, and snippets.

@brianhsu
Created March 29, 2016 03:38
Show Gist options
  • Select an option

  • Save brianhsu/a0e8f011498e4719c05b to your computer and use it in GitHub Desktop.

Select an option

Save brianhsu/a0e8f011498e4719c05b to your computer and use it in GitHub Desktop.
Installation
=========================
1. git clone https://github.com/devttys0/libmpsse.git
2. cd cd libmpsse/src/
3 ./configure --prefix=/usr/
4. make
gcc -Wall -fPIC -fno-strict-aliasing -g -O2 -lftdi1 -DLIBFTDI1=1 -c support.c
gcc -Wall -fPIC -fno-strict-aliasing -g -O2 -lftdi1 -DLIBFTDI1=1 -c mpsse.c
gcc -Wall -fPIC -fno-strict-aliasing -g -O2 -lftdi1 -c fast.c
In file included from fast.c:10:0:
mpsse.h:9:18: 嚴重錯誤:ftdi.h:沒有此一檔案或目錄
#include <ftdi.h>
^
編譯插斷。
Makefile:32: recipe for target 'fast.o' failed
make: *** [fast.o] Error 1
5. 如果出現上述錯誤,修改 mpsse.h 這個檔案,把下面的那段直接改成 #include <libftdi1/ftdi.h>
#if LIBFTDI1 == 1
#include <libftdi1/ftdi.h>
#else
#include <ftdi.h>
#endif
6. sudo make install
Usage
===============================
1. 確認 Kernel 的 ftdi_sio / usbserial 模組沒有被載入,先用 root 執行下面的指令來卸載
modprobe -r -q ftdi_sio
modprobe -r -q usbserial
2. 範例程式碼:
#include <stdio.h>
#include <stdlib.h>
#include <mpsse.h>
#include <unistd.h>
int main(int argc, char * argv[])
{
struct mpsse_context *io = NULL;
int i = 0, retval = EXIT_FAILURE;
int PIN = GPIOL3;
// 開啟 FT232H Device
io = MPSSE(GPIO, 0, 0);
// 檢查是否正確開啟
if(io && io->open) {
for(i=0; i<100; i++) {
printf("Set HIGH...\n");
// 設定 PIN 腳為 HIGH,拉到約 3V
PinHigh(io, PIN);
int state1 = PinState(io, PIN, -1); // 讀 PIN 腳,最後一個參數固定為 -1,若返回 1 為 HIGH,0 為 LOW
printf("State 1: %d\n", state1);
sleep(1);
// 設定 PIN 腳為 LOW,拉到約 0V
printf("Set LOW...\n");
PinLow(io, PIN);
int state2 = PinState(io, PIN, -1); // 讀 PIN 腳,最後一個參數固定為 -1,若返回 1 為 HIGH,0 為 LOW
printf("State 2: %d\n", state2);
sleep(1);
}
retval = EXIT_SUCCESS;
}
else {
printf("Failed to open MPSSE: %s\n", ErrorString(io));
}
Close(io);
return retval;
}
3. 編譯:
gcc gpio.c -lmpsse
4. 執行上述的程式,若出現 Device not found 錯誤的話,試試看用 root 執行。
GPIO PIN Define
=========================
// 只能做 Output
GPIOH7 = C7
GPIOH6 = C6
GPIOH5 = C5
GPIOH4 = C4
GPIOH3 = C3
GPIOH2 = C2
GPIOH1 = C1
GPIOH0 = C0
// 可以做 Input / Output
GPIOL3 = D7
GPIOL2 = D6
GPIOL1 = D5
GPIOL0 = D4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment