2015年3月31日 星期二

USB Power Delivery資料收集



USB PD主要目的在於訂定規範,利用USB Port 提供 Power 供Device 執行快速充電(統一目前所有Mobile Device充電方式),解決現有Device 充電方式及規格不一的窘境,基於環保及避免資源浪費.......

  • Battery Charging Specification 1.2:[22] Released in December 2010.
    Several changes and increasing limits including allowing 1.5 A on charging ports for unconfigured devices, allowing High Speed communication while having a current up to 1.5 A and allowing a maximum current of 5 A.
  • Battery Charging Specification 1.1: Released in March 2007 and updated on 15 April 2009.
    Adds support for dedicated chargers (power supplies with USB connectors), host chargers (USB hosts that can act as chargers) and the No Dead Battery provision, which allows devices to temporarily draw 100 mA current after they have been attached. If a USB device is connected to dedicated charger, maximum current drawn by the device may be as high as 1.8 A. (Note that this document is not distributed with USB 2.0 specification package only USB 3.0 and USB On-The-Go.)

Consumer端:
iPhone 5 : 5V/1A
iPhone 6: 5V/1.5A
iPAD Air : 5V/2.1A
BC 1.2 : 5V/1.5A

Provider端:
Apple Charge Adapter
Apple Mac
PC laptop
PC desttop
Dedicated Charge Adapter


Usb 2.0 5v/500ma      2.5w
Usb 3.0 5v/900m.       4.5w
Bc 1.2  5v/1500m.      7.5w

Usb power deliver        ~100w

節錄自:

USB 3.1增加USB Power Delivery電力供應

USB 3.1也帶來新的電力供應規範 - USB Power Delivery(USB PD),設計上相容現有的USB 2.0和USB 3.0線材和連接器,新的USB 3.1 Type-C連接器也適用。USB PD支援更高的電壓和電流,以滿足不同的應用裝置,同時也相容現有的USB Battery Charging 1.2充電規格。USB PD為埠對埠的架構,USB和電力溝通訊號分開,電力的供應是透過主機端和裝置端的VBus通訊協定來溝通,如果裝置支援,則可依組態(Profiles)的電壓和電流,提供更高的瓦數供應。USB PD依裝置不同分成5個組態(Profiles),皆需要使用新的可偵測線材,才能提供大於1.5A或5V的電力。組態1針對手機,為5V/2A(10W),基本的電力輸出,已比USB 3.0的5V/0.9A(4.5W)還多; 組態2為5V/2A或12V/1.5A(最大18W),可為平板和筆電充電;組態3為5V/2A或12V/3A(可提供較大的筆電最大36W的電力);組態4為最大為20V/3A(60W),但限制Micro-A/B連接器,組態5為最大為20V/5A(100W)的電力供應,但限TYPE-A/B連接器。PCDIY!認為USB 3.1供電的提升,以及支援充電的應用是非常重要的,這將會讓USB 3.1有望可以大一統手機、平板、電腦傳輸介面。

/webroot/data/media/c651dbfdc62361145f6d49fcbdf719b6_800.jpg

USB 3.1規格裡面,USB PD依裝置不同分成5個組態(Profiles),最大可提供100瓦的電力(圖片來源:Intel)

USB Power Delivery技術與架構詳解

USB Power Delivery (USB PD)在充電/供電規格上,制定了最基本的Profile 1 (提供5V@2A, 10W供電,適用於手機、數位相機等可攜裝置)、Profile 2 (提供5V@2A、12V@1.5A, 10~18W供電,適用於平板電腦、外接儲存裝置)、Profile 3 (提供5V@2A、12V@3A, 10~36W供電,適用於Ultrabook、變形平板等裝置)、Profile 4 (提供5V@2A、12/20V@3A, 10~60W供電,這是microUSB能支援的最大供電規格,適用於一般高效能筆電、AIO電腦),以及最高級別Profile 5 (提供5V@2A、12/20V@5A, 10~100W供電,用於標準A/B與Type-C連接頭,可接液晶顯示器、平面電視)。

DIGITIMES中文網 原文網址: USB Power Delivery將促使3C與行動裝置充電標準趨於一統 http://www.digitimes.com.tw/tw/iot/shwnws.asp?CnlID=15&Cat=10&id=395752#ixzz3btvsQMhd



/webroot/data/media/06a2c1094d3b8cc646a076daf53e1772_800.jpg

USB 3.1規格裡面,USB PD電力和USB訊號獨立傳送,USB PD為埠對埠的架構(圖片來源:Intel)


/webroot/data/media/d37be8317f2dcaf07b3c143d0d7bda14_800.jpg
USB 3.1規格裡面,USB PD主機端和裝置端以VBus通訊協定來溝通(圖片來源:Intel)


以下節錄自:

除“Battery Charging Revision 1.2”外,USB的規格每更新一次,供給電力都在增加。2012年7月亮相的“USB PD”實現了100W供電。預計支持產品將在2013年上半年亮相。




USB PD相應於供給電力分5級規格。以各規格確定電壓和電流供電。100W供電以20V電壓和5A電流實現(a)。USB PD的拓扑將供電側稱為“Provider”,受電側稱為“Consumer”(b)。(圖由《日經電子》根據USB PD的技術標准書制作)

“USB PD”首先辨別是否為支持線纜。接下來通過VBUS通信確定實際傳輸電力的電壓和電流。然后供電。這一連串動作由“Device Policy Manager”等管理部負責。(圖由《日經電子》根據USB PD的技術標准書制作)


USB PD與以往的USB規格不同,需要專用控制IC和專用線纜。控制IC被稱為“PD芯片”。專用線纜還可連接以往的USB連接器。(圖由《日經電子》根據USB PD的技術標准書制作)


註: Standard-A 母頭位於Host端, Device端若為PD Consumer,則會利用連接PD Provider端的PD detect端子偵測Host端是否為PD provider capability,因此具備PD Consumer能力的Device線材不同以往.......

USB PD Standard-A連接器為了辨別產品是否支持USB PD,在接收側的“插座”裡面新追加了“PD DETECT”端子。稍微加長了USB PD Standard-A插頭的長度,以接觸到PD DETECT端子(圖5)。通過檢測是否有接觸,來判斷是否為支持線纜。
 “USB PD”的“Standard-A”連接器在接收側的插座中追加了用來檢測是否為USB PD支持線纜的“PDDETECT”端子(a、b)。該端子配置在插座裡面,僅長度較長的USB PD的Standard-A連接器能接觸到。(圖(a)由《日經電子》根據USB PD的技術標准書制作)

 “USB PD”的“Standard-B”連接器追加了ID端子,用來判斷是否為支持線纜(a,b)。ID端子還用來確定流過線纜的電流值。根據電容器的安裝位置來判斷是3A用線纜還是5A用線纜(c)。(圖(a)由《日經電子》根據USB PD的技術標准書制作)

USB PD的Micro規格連接器方面,在原規格中設置了ID端子,因此沒有太大改變。不過,由於最大流過60W電力,連接器部和線纜部等容易發熱。Standard-A和Standard-B規格也同樣如此,USB PD的規格必須將溫度上升范圍控制在+30℃以內。因此,必須採取不同於以往USB的熱對策。
追加PD芯片
關於USB PD新導入的確定電壓值和電流值的流程,通過在供電側和受電側雙方的產品中追加交換所需數據的IC、即“PD芯片”予以支持。通過在這兩個PD芯片間交換數據來確定發送的電壓和電流值。利用VBUS端子收發載流子頻率為23.2MHz、數據傳輸速度為300kbit/秒的“FSK(frequency shift keying)”信號。



美國英特爾公司的USB-IF人士介紹,PD芯片集成到USB控制器中的可能性比較低。這位人士認為因為“最合理又省錢的方法是,USB控制器利用現有的產品,USB PD隻在需要時追加PD芯片”。












USB Type-C Cable Detect (CD) and Power Delivery (PD)


NodeMCU固件-GPIO/ADC/PWM控制



參考連結: NodeMCU API指令

包括有下列Module:
node
file
wifi
wifi.sta
wifi.ap
timer
gpio
pwm
net
net server
net socket
i2c
adc
uart
1-wire
bit
spi
mqtt
mqtt client



NodeMCU 的 GPIO Pin 對應 ESP8266 GPIO Pin看連結說明.


esp12-3.jpg


NodeMCU 的 GPIO Module定義了12個GPIO Pin,對應如下:


IO indexESP8266 pinIO indexESP8266 pin
0 [*]GPIO167GPIO13 (Color Blue)
1GPIO58GPIO15 (Color Red)
2GPIO49GPIO3
3GPIO010GPIO1
4GPIO211GPIO9
5GPIO1412GPIO10
6GPIO12(Color Green)
** [] D0(GPIO16) can only be used as gpio read/write. no interrupt supported. no pwm/i2c/ow supported. *

GPIO16=0
GPIO5=1
GPIO4=2
GPIO0=3
GPIO2=4
GPIO14=5
GPIO12=6
GPIO13=7
GPIO15=8
GPIO3=9
GPIO1=10
GPIO9=11
GPIO10=12


//亮Red ,暗Green,Blue
gpio.mode(GPIO15, gpio.OUTPUT)  gpio.write(GPIO15,1) -- Red on
gpio.mode(GPIO12, gpio.OUTPUT)  gpio.write(GPIO12,0) -- Green off
gpio.mode(GPIO13, gpio.OUTPUT)  gpio.write(GPIO13,0) -- Blue off



關於GPIO Module說明

範例: (Pin index=0~12)

>gpio.mode(4, gpio.OUTPUT)
>gpio.write(4, gpio.LOW)   --點亮GPIO2 LED
>print(gpio.read(4))

0
>gpio.write(4,gpio.HIGH)  --關掉GPIO2 LED
>print(gpio.read(4))

1
>

關於ADC Module說明


測試板ADC是接光敏電阻,越暗,阻值大,電壓越大,所讀ADC數值就會越大

範例: 

>print(adc.read(0)) --暗

256
>print(adc.read(0)) --亮

0

讀Vdd33電壓
>print(adc.readvdd33()/1000) --read vdd voltage

2.944





關於PWM Module說明
Only 3 pins can be set to PWM mode at the most.

pin: 1~12, IO index
duty: 0~1023, pwm duty cycle, max 1023(10bit).
範例: 

    -- D6 is connected to green led (ESP8266 GPIO15)
    -- D7 is connected to blue led (ESP8266 GPIO12)
    -- D8 is connected to red led (ESSP8266 GPIO13)
    pwm.setup(6,500,512)
    pwm.setup(7,500,512)
    pwm.setup(8,500,512)
    pwm.start(6)
    pwm.start(7)
    pwm.start(8)
    function led(r,g,b)
      pwm.setduty(6,g)
      pwm.setduty(7,b)
      pwm.setduty(8,r)
    end
    led(512,0,0) --  set led to red
    led(0,0,512) -- set led to blue.






燒錄NodeMCU固件-安信可ESP-12測試板




有關 NodeMCU 的相關連結如下:


官網: http://nidemcu.com/



Popular repositories



最新NodeMCU最新固件下載位址:
https://github.com/nodemcu/nodemcu-firmware/releases

20150901補充 : 有float及 integer兩種版本binary... (keyword : LUA_NUMBER_INTEGRAL)
integer vs float version : integer version only support integer as lua number (less memory usage)


Numbers - Lua



my1.jpg

針對Windows用戶的NodeMCU Firmware燒錄步驟:


1.將GPIO0 拉LOW(將短路帽接上) ,執行NodeMCU Flasher ( Win32 , Win64)程式,接著設定正確的Com Port ,並按下"Flash" Button.

Begin program

2.等候片刻

Programming

3.顯示現在的Firmware Mac Address 設定

Program success

4.此時,可設定你想燒錄的檔案.

Setting

20150908補充:燒錄時請儘量連接至電腦的USB Port,就是不經過其他Hub,若使用由外部USB Hub接出的Port,會有無法燒錄或燒錄失敗的可能....



燒錄完成後記得GPIO0回復為High(Normal mode),
Baud Rate設成9600 , Reset and reboot 後,會看到如下輸出......



安信可ESP-12測試板入門




注意事項: 此模塊出廠Firmware是搭配App IOT.apk 設計, 所以沒有設計AT Command輸入功能,主要是Demo如何用手機來控制ESP8266 Module ,使用者可考慮利用SDK開發自己的Firmware應用,或可燒錄其他現成的Firmware, 例如 NodeMCP..........



安信可ESP-12測試板出廠Firmware,預設Boot 後貌似為115200 , 8 N 1......


1  Android手机下載安装IOT.APK   IOS客户端稍后发布
2  短路帽(如下圖左側邊上的2Pin Jumper)若接上上电进入烧写模式,若拿掉則进入正常运行状态
3  装上电池后,手机搜索到网络“AI-THINKER” 的路由器
4  加入这个路由器(模块形成的路由器)密码是:ai-thinker

5  加入成功后就可以用手机直接控制模块了。

(注:此模块默认固件 AI-THINKER-IOT-2014-10-17 165528.bin (508 KB, 下载次数: 1009) ,默认已刷入,想用手机直接控制的话请勿刷写其他固件!)




若要接Usb-Serial 接法如下,

下圖為 PL2303 + ESP8266測試板

my1.jpg
請注意上面TXD及RXD所接位置,不要接錯了.......



2015年3月26日 星期四

Lua on UEFI



EDK2 下Lua 源碼位置 :Lua

Readme.txt

Embedding Lua
-------------
The Lua library instance, LuaLib, is defined by StdLib.inc.  Since, currently, all applications which
embed Lua are also StdLib applications, StdLib.inc will be included by your package's .DSC file.

The header files required to use LuaLib are in the standard include path at StdLib\Include\Lua.
They may be referenced as:
  #include  <Lua/lua.h>
  #include  <Lua/lualib.h>
  #include  <Lua/lauxlib>
  #include  <Lua/luaconf.h>

Lua/luaconf.h is the Lua configuration file.  If you wish to build Lua with custom characteristics,
this is the file to modify.  Modify the file in StdLib\Include\Lua since the file in the Lua
source tree is just a stub which references the file in StdLib.


Installation on UEFI
--------------------
Install the Lua.efi file into \Efi\Tools.   This is the standalone Lua interpreter.
Create a directory, \Efi\StdLib\lib\Lua.    This is the default location for Lua scripts.

If desired, copy the files from AppPkg\Applications\Lua\scripts, in the source tree, into
\Efi\StdLib\lib\Lua.

Bugs and Other Issues
---------------------
EOF characters, ^D or ^Z, are not properly recognized by the console and can't be used to
terminate an application.  Use os.exit() to exit Lua.


使用操作如下:



fs0>lua
>require("myprint")
>viewtable(package.loaded)
>print(package.path)
>print(package.cpath)




Download : efi.zip




只有lua才能编译出小于200K的代码吗?NO! Python 也可以 ...


有空來試試Python on UEFI







C Library(Module) Loader for UEFI Lua



require("mylib")    // load mylib.efi or mylib.so or mylib.dll

本篇學習如何建立在Uefi 下可供Lua載入的library

試圖讓library能由Lua C Loader 載入

關鍵檔案在src/loadlib.c


LUA_USE_DLOPEN :for linux *.so


LUA_DL_DLL :for windows *.dll




/*

119 ** {========================================================================

120 ** This is an implementation of loadlib based on the dlfcn interface.

121 ** The dlfcn interface is available in Linux, SunOS, Solaris, IRIX, FreeBSD,

122 ** NetBSD, AIX 4.2, HPUX 11, and  probably most other Unix flavors, at least

123 ** as an emulation layer on top of native functions.

124 ** =========================================================================

125 */
126 
127 #include <dlfcn.h>
128 
129 static void ll_unloadlib (void *lib) {
130   dlclose(lib);
131 }
132 
133 
134 static void *ll_load (lua_State *L, const char *path, int seeglb) {
135   void *lib = dlopen(path, RTLD_NOW | (seeglb ? RTLD_GLOBAL : RTLD_LOCAL));
136   if (lib == NULL) lua_pushstring(L, dlerror());
137   return lib;
138 }
139 
140 
141 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
142   lua_CFunction f = (lua_CFunction)dlsym(lib, sym);
143   if (f == NULL) lua_pushstring(L, dlerror());
144   return f;
145 }
146 
147 /* }====================================================== */
148 
149 


222 #undef LIB_FAIL
223 #define LIB_FAIL    "absent"
224 
225 
226 #define DLMSG   "dynamic libraries not enabled; check your Lua installation"
227 
228 
229 static void ll_unloadlib (void *lib) {
230   (void)(lib);  /* not used */
231 }
232 
233 
234 static void *ll_load (lua_State *L, const char *path, int seeglb) {
235   (void)(path); (void)(seeglb);  /* not used */
236   lua_pushliteral(L, DLMSG);
237   return NULL;
238 }
239 
240 
241 static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym) {
242   (void)(lib); (void)(sym);  /* not used */
243   lua_pushliteral(L, DLMSG);
244   return NULL;
245 }


初步判斷整個過程是利用dlopen 及 LoadLibraryEx的加載過程.......

疑問: Uefi 有Dynamic Load Library架構嗎?好像是沒有,載入Library有重定位問題,UEFI 可由載入的Application or Driver 中,對應出所要的Function ?要將Library以UEFI Driver 常駐形式存在,且初始化時,要建立FunctionName及FunctionAddress 對應關係


static void ll_unloadlib (void *lib)

static void *ll_load (lua_State *L, const char *path, int seeglb)

static lua_CFunction ll_sym (lua_State *L, void *lib, const char *sym)


在UEFI 下實現構想如下:

1.建立 EFI_LUA_DLL_PROTOCOL,用於Function name查找
2.於ll_load時載入 Lua Module Uefi Driver
3.於ll_unloadlib時卸載 Lua Module Uefi Driver
4.於ll_sym時call method 取得Function Address


 static int ll_loadfunc (lua_State *L, const char *path, const char *sym) {
287   void *reg = ll_checkclib(L, path);  /* check loaded C libraries */
288   if (reg == NULL) {  /* must load library? */
289     reg = ll_load(L, path, *sym == '*');
290     if (reg == NULL) return ERRLIB;  /* unable to load library */
291     ll_addtoclib(L, path, reg);
292   }
293   if (*sym == '*') {  /* loading only library (no function)? */
294     lua_pushboolean(L, 1);  /* return 'true' */
295     return 0;  /* no errors */
296   }
297   else {
298     lua_CFunction f = ll_sym(L, reg, sym);
299     if (f == NULL)
300       return ERRFUNC;  /* unable to find function */
301     lua_pushcfunction(L, f);  /* else create new function */
302     return 0;  /* no errors */
303   }
304 }


print(package.path)
require("hello")

(收工)

2015年3月24日 星期二

Lua 認識與語法學習



http://www.lua.org/


Lua是一種輕量語言,它的官方版本只包括一個精簡的核心和最基本的庫。這使得Lua體積小、啟動速度快。它用標準C語言編寫並以原始碼形式開放,編譯後僅僅一百餘K,可以很方便的嵌入別的程式裡。和許多「大而全」的語言不一樣,網路通訊、圖形介面等都沒有預設提供。但是Lua可以很容易地被擴充功能:由宿主語言(通常是C或C++)提供這些功能,Lua可以使用它們,就像是本來就內建的功能一樣。事實上,現在已經有很多成熟的擴充功能模組可供選用。
Lua是一種多重編程範式的程式設計語言:它只提供了很小的一個特性集合來滿足不同編程範式的需要,而不是為某種特定的編程範式提供繁雜的特性支援。例如,Lua並不提供繼承這個特性,但是你可以用元表來模擬它。諸如命名空間這些概念都沒有在語言基本特性中實作,但是我們可以用表結構(Lua唯一提供的複雜資料結構)輕易模擬。Lua可以在執行時隨時構造出一個函式,並把它看作一個物件(正是所謂的first class function),這個特性可以很好的滿足函數語言程式設計的需要。這是提供了這些基本的元特性,我們可以任意的對語言進行自需的改造。
Lua原生支援的資料類型非常之少,它只提供了數字(預設是雙精度浮點數,可配置)、布爾量、字串、表、子程式協程(coroutine)以及用戶自訂資料這幾種。但是其處理表和字串的效率非常之高,加上元表的支援,開發者可以高效的模擬出需要的複雜資料類型(比如集合、陣列等)。
Lua是一個動態弱型別語言,支援增量式垃圾收集策略。有內建的,與作業系統無關的協作式多執行緒(coroutine)支援。
有8種基本類型:nil、布爾值(boolean)、數字型(number)、字串型(string)、用戶自訂類型(userdata)、函式(function)、執行緒(thread)和表(table)


關於Lua語法,可看這本書.



相關參考資料:

Lua - 維基百科,自由的百科全書 - Wikipedia

[PDF] Programming in Lua - CentOS中文站

Lua 5.1 参考手册 - 云风工作室

edartuz/lua-serial · GitHub

http://gamedevgeek.com/tutorials/getting-started-with-lua/