2016年9月9日 星期五

[Raspberry] 在Raspberry Pi 2 使用 UART

確認Pi 沒有主控台控制權,如果有,先關閉

pi@raspberrypi:~ $ dmesg | grep tty
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1024 bcm2708_fb.fbheight=768 bcm2709.boardrev=0xa21041 bcm2709.serial=0xf1ba6d8 smsc95xx.macaddr=B8:27:EB:1B:A6:D8 bcm2708_fb.fbswap=1 bcm2709.uart_clock=48000000 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
[    0.001532] console [tty1] enabled
[    0.093340] 3f201000.uart: ttyAMA0 at MMIO 0x3f201000 (irq = 87, base_baud = 0) is a PL011 rev2
[    0.596998] console [ttyAMA0] enabled
[    4.446432] systemd[1]: Expecting device dev-ttyAMA0.device...
[    5.297264] systemd[1]: Starting system-serial\x2dgetty.slice.
[    5.319043] systemd[1]: Created slice system-serial\x2dgetty.slice.
pi@raspberrypi:~ $

關閉方式:
Sudo raspi-config
9 Advanced Options
A7 Serial  Enable/Disable shell and kernel messages on the serial connection ,關閉他 

Reboot, dmesg | grep tty

pi@raspberrypi:~ $ dmesg | grep tty
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=1024 bcm2708_fb.fbheight=768 bcm2709.boardrev=0xa21041 bcm2709.serial=0xf1ba6d8 smsc95xx.macaddr=B8:27:EB:1B:A6:D8 bcm2708_fb.fbswap=1 bcm2709.uart_clock=48000000 bcm2709.disk_led_gpio=47 bcm2709.disk_led_active_low=0 vc_mem.mem_base=0x3dc00000 vc_mem.mem_size=0x3f000000  dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
[    0.001516] console [tty1] enabled
pi@raspberrypi:~ $

關閉了

去查看 /dev/ttyAMA0 不見了(網路上的範例都是用這個PORT !!!)
參考資料,第一二篇有寫了一大推解決方法……..太難了,看無


進入視窗,左上角 Menu -> Preferences -> Raspberry Pi Configuration ->Interfaces -> Serial --> Enable

Sudo apt-get install minicom  安裝軟體
sudo minicom –b 9600 –o –D /dev/ttyAMA0
切換至新視窗後,按鍵盤的Key,確認在示波器有看到輸出信號

gpio readall -- >check state of all pin
gpio mode 15 ALTO -->8pin改成 ALTO state



程式:

Sudo chmod a+rw /dev/ttyAMA0  -->如果沒權限,就無法open port

#include <cstdlib>

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>

using namespace std;

int main(int argc, char** argv) {
    //setup UART
    int uart_filestream=-1;
    uart_filestream=open("/dev/ttyAMA0",O_RDWR | O_NOCTTY | O_NDELAY);
    if(uart_filestream==-1)
    {
        printf("Unable to Open UART \r\n ");
        return 0;
    }
   
    //Configure UART
    struct termios options;
    tcgetattr(uart_filestream,&options);
    options.c_cflag=B9600 | CS8 | CLOCAL | CREAD;
    options.c_iflag=IGNPAR;
    options.c_oflag=0;
    options.c_lflag=0;
    tcflush(uart_filestream,TCIFLUSH);
    tcsetattr(uart_filestream,TCSANOW,&options);
   
    //Transmitting
    unsigned char tx_buf[10];
    unsigned char *p_buffer;  
    p_buffer=&tx_buf[0];
    *p_buffer++='O';
    *p_buffer++='I';
    *p_buffer++='A';
    if(uart_filestream !=-1)
    {
        int count=write(uart_filestream,&tx_buf[0],(p_buffer-&tx_buf[0]));
        if(count<0)
            printf("TX error \r\n");
    }
    
    close(uart_filestream);
    printf("finish\r\n");
    return 0;
}




參考資料:

Raspberry Raspbian turning off the UART functioning as a serial console


Serial(UART) communication on Raspberry Pi 2

Raspberry Pi 釋放主控台序列埠(GPIO14,GPIO15)

Using the UART (C語言)



[Raspberry] 在Raspberry Pi 2 使用 I2C

網路上找到的,Raspberry Pi2 Model B 40 Pin GPIO接腳圖,其中:GPIO3 I2C SDA1,GPIO5 I2C SCL1,GPIO9 GND

找到的資料有提及:
電流輸出的限制:每個接腳輸出最大為16mA,全部接腳同時最大輸出為50mA
GPIO3.3V準位




pi@raspberrypi:~ $ lsmod   (檢查系統所載入的module)
Module                  Size  Used by
.
i2c_bcm2708             4834     0
i2c_dev                 5859     0


pi@raspberrypi:~ $ sudo  i2cdetect  (列舉出系統上的I2C裝置)
i2c-1   i2c          3f804000.i2c   I2C adapter


pi@raspberrypi:~ $ sudo  i2cdetect  -y  1  (列出可用的slave address)
    0 1 2 3 4 5 6 7 8 9 a b c d e f
00:      -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- 14 -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- 55 -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --                        


 pi@raspberrypi:~ $ sudo i2cget -y 1 0x55 0x2c   (讀取SOC low bte)
0x31
pi@raspberrypi:~ $ sudo i2cget -y 1 0x55 0x2d   (讀取SOC high bte)
0x00


pi@raspberrypi:~ $ sudo i2cdump -y 1 0x55
No size specified (using byte-data access)
        0 1   2   3   4  5  6  7  8    9 a    b   c  d    e  f
00: 00 00 00 00 ff 7f a6 0b 2a 0f c0 01 34 05 9f 0a    .....???*???4???
10: 34 05 9f 0a 00 00 ff ff 00 00 00 00 ff ff 00 00    4???............
20: ff ff 79 04 00 00 ff ff a6 0b 00 00 31 00 64 00    ..y?....??..1.d.
30: 00 00 34 05 00 00 00 00 00 00 00 00 be 0a 78 94    ..4?........??x?
40: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
60: 00 00 00 00 00 00 00 00 20 00 00 00 00 00 00 00    ........ .......
70: 00 00 c3 0b 00 00 55 00 00 00 00 00 00 00 00 00    ..??..U.........
80: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
90: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
a0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
b0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
c0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
d0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
e0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX
f0: XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX XX    XXXXXXXXXXXXXXXX


安裝 libi2c-dev (sudo apt-get install libi2c-dev)
有安裝就可使用 i2c_smbus_read_word_data 等眾多function,不然只有簡單 read/write 可用
如果有用 python,安裝 python-smbus


C的程式如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>

using namespace std;

/*
*
*/
int main(int argc, char** argv) {
int file_i2c;
int length;
unsigned char buffer[100]={0};
char *filename=(char *)"/dev/i2c-1";    

//open the bus
if((file_i2c=open(filename,O_RDWR))<0)
{
    printf("Failed to openthei2c bus\n\r");
    return 0;
}
else
    printf("open /dev/i2c-1 ok!\r\n");

//setup slave address
int addr=0x55;
if(ioctl(file_i2c,I2C_SLAVE,addr)<0)
{
    printf("Failed to acquire bus acess and/or talk toslave");
    return 0;
}
else
    printf("setup slaveaddress to 0x55 ok!\r\n");


length=2;
if(read(file_i2c,buffer,length)!=length)  //not useful
{
    printf("Failed to read  from i2c bus\r\n");
    return 0;
}
else
    printf("Data read:%x %x\r\n",buffer[0],buffer[1]);


int dat;
dat=i2c_smbus_read_word_data(file_i2c,0x3e);
 if(dat<0)
 {
     printf("read failed\r\n");
 }
 else
 {
     printf("Data read:%lx \r\n",dat);
 }

printf("finish\n\r");  

close(file_i2c);

return 0;
}





不錯的參考資料
(1)   安裝編譯C/C++
[How To Raspberry Pi] 安裝 GCC
Raspberry Pi:Pi 2 Raspbian 上安裝NetBeansIDE,撰寫JavaC++ 程式

(2)安裝/啟動 I2C driver
I2C Installation for Raspberry Pi – Step by Step Guide
讓你的Raspberry Pi透過I2C讀取EEPROM

(3)參考文章:
Raspberry Pi:如何更新Raspbian
Raspberry Pi:Raspbian套件列表
Raspberry Pi安裝中文環境
http://yehnan.blogspot.tw/2012/08/raspberry-pi.html 
黑傑克的筆記
I2C Drivers, Part I
Interfacing an I2C GPIO expander(MCP23017) to the Raspberry Pu using C++(i2Cdev)
Using the I2C interface












2016年8月24日 星期三

ESP8266 wifi 模組測試

這一篇有講到,把 ESP8266當 server,然後連到 AP, 另外再把一台PC當 client,也連到AP
然後PC就可與ESP8266連線,秀字出來喔!!

http://lets-make-games.blogspot.tw/2015/05/wifi-esp8266-arduino.html

2016年8月23日 星期二

ESP8266 WIFI 模組初體驗

(1)ESP8266 端(PC COM PORT 監控端)
AT
OK


SP8266Wifi連線有三種模式:
Station模式:扮演連線到現有Wifi的裝置。
AP模式:扮演無線基地台,提供Wifi環境供連線。
Station+AP混合模式:允許同時使用以上兩種模式的特有功能。

AT+CWMODE=2      將模式設定為第2(AP模式)
OK


AT+CIFSR           顯示ESP8266自己目前的 IP位址
192.168.4.1
OK


AT+CIPMUX? 查詢目前的設定值
AT+CIPMUX=1 允許一對多的多重連線。

AT+CIPMUX=0 關閉多重連線,只能一對一連線。此時無法進入伺服器模式。

AT+CIPMUX=1
OK


AT+CIPSERVER= <mode>[,<port>]開啟(或關閉)伺服器,等候客戶端透過指定的連接埠(Port)連線。
<mode>0關閉伺服器,1開啟。

<port>:網路連接埠號碼。範圍從 0~65535皆可。連線到伺服器的裝置必須知道連接埠(Port)號碼才能進行連線。一般根據不同的用途,會有
慣用的連接埠號碼,不過並沒有強制性

AT+CIPSERVER=1,8888
OK


查詢AP的設定,此指令只有當 ESP8266 工作於 AP/BOTH 模式時才有用, STA 模式下此指令會得到 ERROR 回應.
AT+CWSAP?
+CWSAP:"ESP_174537","",1,0    預設 ssid, pwd, ch, ecn
(2)PC端
網路連線到ESP_174537
開啟終端機,輸入:

則 ESP8266端會出現 Link,關閉終端機,ESP8266端會出現Unlink


(3)ESP8266

AT+CIPSEND=<length>只適用於 CIPMUX=0 的情況。
AT+CIPSEND=<id>,<length>只適用於 CIPMUX=1 的情況。

<id>:在多重連線的情況下,需要輸入連線的代號,ESP8266才會知道要把訊息發送給哪一條連線。

<length>要發送的訊息長度。等到此指令發送出去之後,會出現「>」符號在螢幕,這時就可以輸入符合訊息長度的訊息。

AT+CIPSEND=0,5
>HELLO
SEND OK
在終端機會出現HELLO

(4)開瀏覽器,打入 http://192.168.4.1:888, ESP8266 出現如下訊息
Link
+IPD,4,270:GET / HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-TW
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; ASU2JS)
Accept-Encoding: gzip, deflate
Host: 192.168.4.1:8888
DNT: 1
Connection: Keep-Alive
OK
開瀏覽器,打入 http://192.168.4.1:888/?PIN=A,在 ESP8266 出現如下訊息
Link
+IPD,1,276:GET /?PIN=A HTTP/1.1
Accept: text/html, application/xhtml+xml, */*
Accept-Language: zh-TW
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0; ASU2JS)
Accept-Encoding: gzip, deflate
Host: 192.168.4.1:8888
DNT: 1
Connection: Keep-Alive
OK



AT+CIPCLOSE=<id>中斷指定的連線,適用於 CIPMUX=1的情況
AT+CIPCLOSE 中斷連線,適用於 CIPMUX=0 的情況

<id>:要中斷的連線代號

2016年8月16日 星期二

[Raspberry Pi]HDMI to vGA

網路上找地的做法,在 SD 找到Config.txt,編輯他,設定如下:
hdmi_group=2
hdmi_mode=16
hdmi_drive=2

如果失敗....

hdmi_force_hotplug=1
config_hdmi_boost=4

在失敗....

hdmi_safe=1

無招了.....