顯示具有 OpenWRT 標籤的文章。 顯示所有文章
顯示具有 OpenWRT 標籤的文章。 顯示所有文章

2019年7月2日 星期二

[OpenWRT] date 初始化

執行OpenWRT的裝置,主要像Router這一類或是一些MPU等級的開發板,

例如MTK7688,若裝置斷電重開後,要獲得正確的時間,可以透過NTP或是

額外加裝的RTC(Real-time Clock) Chip。

若是處於未連網及不具有RTC的情況下,系統會找尋/etc下最新一筆

檔案的變更時間,做為目前的時間。這段程式碼在/etc/init.d/sysfixtime


START=00
STOP=90
boot() {
 local maxtime="$(maxtime)"
 local curtime="$(date +%s)"
 [ $curtime -lt $maxtime ] && date -s @$maxtime
}

maxtime() {
 local file newest

 for file in $( find /etc -type f ) ; do
  [ -z "$newest" -o "$newest" -ot "$file" ] && newest=$file
 done
 [ "$newest" ] && date -r "$newest" +%s
}

2018年10月14日 星期日

[OpenWRT] 透過Direct Acess存取AR9331 GPIO

在OpenWRT上要控制GPIO有二種方式

1.透過修改/sys/class/gpio/gpioxx/Value來變更,此種方法好處是可以在多種程式語言,

單純透過讀寫檔案的方式就可以改變GPIO,亦不需要知道實際GPIO所對應的記憶體位置。

缺點則是速度較慢。

Ex:切換系統LED

echo "27" > /sys/class/gpio/export

echo "out" > /sys/class/gpio/gpio27/direction
echo "1" > /sys/class/gpio/gpio27/value
or
echo "0" > /sys/class/gpio/gpio27/value
2. Direct Access GPIO Register,這種方式需要知道GPIO所對應的暫存器位置 ,適合以C語言開發,並對切換GPIO腳位速度有需求的使用者,例如RS 485應用。


以AR9331為例,參考它的DataSheet ,可以得知GPIO的位置為18040000。


讓GPIO 27 閃爍的範例程碼


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <string.h>
#include <time.h>


#define GPIO_ADDR 0x18040000 // base address
#define GPIO_BLOCK 48     // memory block size

volatile unsigned long *gpioAddress;
//Read GPIO Status
//return 0 -->Low
//return >0 -->High
int gpioRead(int gpio)
{
    unsigned long value = *(gpioAddress + 1);
    return (value & (1 << gpio));
}

// "1" (output) or "0" (input) 
void gpioDirection(int gpio, int direction)
{
    // obtain current GPIO settings
    unsigned long value = *(gpioAddress + 0); 
    if (direction == 1)
    {
        value |= (1 << gpio); // set bit to 1 (output)
    }
    else
    {
        value &= ~(1 << gpio); // clear bit (input)
    }
    *(gpioAddress + 0) = value;
}
//Set GPIO
void gpioSet(int gpio, int data)
{
    // obtain current GPIO settings
    unsigned long value = *(gpioAddress + 2); 
    if (data == 1)
    {
        value |= (1 << gpio); // set bit to 1 (High)
    }
    else
    {
        value &= ~(1 << gpio); // clear bit  (Low)
    }
    *(gpioAddress + 2) = value;
}
//Init GPIO Address, if /dev/mem not foud, you need to rebuild Image and add this module
int gpioSetup()
{
    int  m_mfd;
    if ((m_mfd = open("/dev/mem", O_RDWR)) < 0)
    {
        return -1;
    }    
    gpioAddress = (unsigned long*)mmap(NULL, GPIO_BLOCK, PROT_READ|PROT_WRITE, MAP_SHARED, m_mfd, GPIO_ADDR);
    close(m_mfd);

    if (gpioAddress == MAP_FAILED)
    {
        return -2;
    }
    return 0;
}

int main(int argc, char **argv)
{
    int pin=27;
    int ms=500;
    
    gpioSetup();
        
 sscanf(argv[1], "%d", &pin);

    gpioDirection(pin,1);//set pin output    

    for (int i=0;i<100;i++)  //blink system led
    {
        gpioSet(pin,1);  //set syteme led high
        usleep(1000*ms); //sleep 500ms
        gpioSet(pin,0);  //set system led low
        usleep(1000*ms); //sleep 500ms
    }  
}

參考來源:

Working with GPIOs (C/C++)

AR9331 Datasheet

2017年2月1日 星期三

[OpenWRT] OpenWRT in VirtualBox -- Mac OS

1.首先安裝VirtualBox

可以到以下網址下載安裝,不要錢

https://www.virtualbox.org/


2.下載OpenWRT Image

https://downloads.openwrt.org/chaos_calmer/15.05/x86/generic/openwrt-15.05-x86-generic-combined-ext4.img.gz


3.解壓縮後會出現一個檔案

openwrt-15.05-x86-generic-combined-ext4.img


4.將檔案格式轉換成VDI (透過VBoxManage)


Applications/VirtualBox.app/Contents/MacOS/VBoxManage convertfromraw --format VDI  openwrt-15.05-x86-generic-combined-ext4.img openwrt.vdi


轉換成功會有以下訊息

Converting from raw image file="/Users/boywhychen/Downloads/openwrt-15.05-x86-generic-combined-ext4.img" to file="openwrt.vdi"... Creating dynamic image with size 55050240 bytes (53MB)..

5.接下來是開啟VirtualBox,新增一個虛擬機,並使用剛才轉換好的openwrt.vdi


























比較特別的一點是,官方安裝說明說,其實你只需要256MB或是更小的RAM

(Optionally decrease RAM size from 256 MB to something smaller)

不過我想試試多給它一點RAM,我先給它512RAM。



6.啟動虛擬機,可以直接按下Enter,也可以等一下它自己會進入預設的系統

























7.如果你跟我一樣卡在這個畫面,請按下Enter


8.可以開啟用囉

























9.來設定一下網路,不然不能連網

先關掉虛擬機,開啟虛擬機網路設定

(1)設定介面卡1為NAT
































(2)設定介面卡2 為橋接介面卡,例如我是用Wi-Fi上網則選擇en1:Wi-Fi,如果是用網路

線,就選擇乙太網路。































10.重新啟動虛擬機,就可以正常連網了。






系統環境:

Mac OS 10.11.5


VirtualBox Version 5.0.20


參考資料:


OpenWrt in VirtualBox