MCU單晶片韌體設計

2017年3月26日 星期日

USB Host 端如何區別 Low Speed, Full Speed , High Speed ?



  • USB 1.0 Low Speed: 1.5Mbps : D- 接一個上拉電阻
  • USB 1.1 Full Speed : 12Mbps : D+ 接一個上拉電阻
  • USB 2.0 High Speed: 480Mbps : D+ 接一個上拉電阻

USB Electrical

其中, R2, R3 接15K (下拉)電阻到GND(在晶片內控制)。


Full Speed 為例, 如下圖 , 當USB Device 插入USB Host 端時, USB_D+ 可以查覺到High (1.5K和15K分壓的結果), Host 便知道有USB Device 插入了, 並且知道這是Full Speed的狀置。若是USB_D- 接上拉電阻, 則知這是插入了Low Speed設備。
USB 1.1 Full Speed : D+ 接一個上拉電阻

如何知道是Full Speed 或  High Speed ?

要知道一個設備是否支援High Speed,需使用兩個特殊的信號狀態。


USB 2.0 devices use a special protocol during reset, called chirping, to negotiate the high bandwidth mode with the host/hub. 
  • A device that is USB 2.0 High Speed capable first connects as an Full Speed device (D+ pulled high), but upon receiving a USB RESET (both D+ and D− driven LOW by host for 10 to 20 ms) it pulls the D− line high, known as chirp K. This indicates to the host that the device is high bandwidth. 
  • If the host/hub is also HS capable, it chirps (returns alternating J and K states on D− and D+ lines) letting the device know that the hub operates at high bandwidth. The device has to receive at least three sets of KJ chirps before it changes to high bandwidth terminations and begins high bandwidth signaling. 
  • Because USB 3.0 uses wiring separate and additional to that used by USB 2.0 and USB 1.x, such bandwidth negotiation is not required.

在Reset期間,支援High Speed的設備會送出一個 Chirp K。支援High Speed的Hub檢測到該 Chirp 後,會送出交替的 Chirp K 與 J。當設備檢測到 KJKJKJ 的樣式後,它會移除它的Full Speed上升電阻,然後在High Speed執行所有的通信。如果集線器沒有對設備的 Chirp K 做出相應,設備知道它必須 繼續使用Full Speed通信。

Signalling 

USB 1.1 full-speed

  • D+ high :is J;  and K is inverse of J state
  • Sync --> Line Transitions: KJKJKJKK
  • End of Packet (EOP): Line Transitions: SE0 + SE0 (D+, D+ are low) + J


Line transition pattern


Example of a Negative Acknowledge packet transmitted by USB 1.1 full-speed device when there is no more data to read. It consists of the following fields: clock synchronization byte, type of packet and end of packet. Data packets would have more information between the type of packet and end of packet.

USB Packet Format


 參考


  1. https://en.wikipedia.org/wiki/USB
  2.  USB-IF
  3. http://www.ittraining.com.tw/ittraining/course/firmware/usb




2017年3月16日 星期四

多重接取(Multiple Access)技術概念整理



多重存取(Multiple Access) 談的是讓很多人共同來使用的資訊通道,而彼此不互相干擾,因此必須以多工技術(multiplex)架構來提供。依方法可分為FDMA、TDMA、CDMA三種。

頻寬可以想像是一個房間空間,有N個人要用

  1. FDMA: 在相同的空間隔出很多的小間,所以N個人可以在同一時段使用 
  2. TDMA: 只要時間錯開, 所以不用隔間。由1個人使用全部空間,用完再換另一個人
  3. CDMA: 不隔間且時間也不必錯開,只要這N個人講不同的語言(spreading code),而互不影響彼此的對話,各自仍然可以分辨出各自不同的語言,不會造成解讀的困擾。CDMA,將不同使用者的資料分別與特定密碼(正交展頻碼)運算以後,再傳送到資料通道,接收端以不同的密碼來分辨要接收的訊號。這種方法必須將數位訊號與密碼進行運算,故只能使用在數位通訊系統中。



「CDMA」的圖片搜尋結果


我們說話的時候,不可能只有一種頻率,而會有高音(高頻)與低音(低頻)的變化;也就是說,我們其實是發出某一個頻率範圍的聲音(聲波)。因此,當我們以高頻電磁波來傳送語音訊號時,不會只用到一個「頻率」,必須使用某一個「頻率範圍」來通話才行。這個可以傳遞訊號的頻率範圍就稱為頻寬(bandwidth),單位和頻率相同為赫茲(Hz)。

通訊時,每一對通訊使用者必須使用「不同的頻率範圍」來通話,因為手機並不會分辨到底是誰和誰在說話,而是接收「某一個頻率範圍」


在通訊設備中,「調變」是指發射端將低頻訊號(聲音)處理成高頻訊號(電磁波)以後,再傳送出去;「解調」則是指接收端將高頻訊號(電磁波)接收以後,還原成低頻訊號(聲音)。

科學家發現聲音傳輸時損耗比較大,容易受到干擾,所以無法傳遞很遠;而電磁波傳輸時損耗較小,不易受到干擾,所以能夠傳遞很遠。例如:人類的聲音頻率約300~3400 Hz,僅能傳遞數百公尺;而FM 收音機的頻率約88~108 MHz,便可傳遞數百公里之遠。因此,聰明的科學家就想到要以高頻的電磁波「載著」低頻的聲音,如此一來就可以傳遞數百公里了,我們稱之為高頻載波技術。  


http://scimonth.blogspot.tw/2014/09/blog-post_3.html


2017年3月9日 星期四

Python 入門學習快速筆記 (五)

類別

class Demo:
   
        i=0   # 類別變數
        def __init__(self, name):
                self.name=name    # self.name 實體變數
        def showvalue(self):
                print("hello",self.i)

        def showname(self):
                print("hello",self.name)


private variable 或 private_method 在名稱前面加上 __
__variable
__function

Private value

Traceback (most recent call last): File "C:/Python34/aaa.py", line 33, in <module> print(a.__y) # private attribute AttributeError: 'Demo' object has no attribute '__y'



200 Traceback (most recent call last): File "C:/Python34/aaa.py", line 25, in <module> print(a.i) AttributeError: 'Demo' object has no attribute 'i'


Traceback (most recent call last): File "C:/Python34/aaa.py", line 25, in <module> print(a.__i) AttributeError: 'Demo' object has no attribute '__i'

==========================================


封裝 


public data, private datapublic function, private function


==========================================


類別多重繼承

子類別(subclass)能夠繼承(inherit) 多個父類別,使子類別可以有多種特性。
  • 當子類別繼承(inheritance)超過一個來源的時候,會以寫在最左邊的父類別優先繼承,這是說,多個父類別如果有相同名稱的屬性(attribute)與方法(method),例如init()、str__()等,就會以最左邊的父類別優先。


==========================================

多型(polymorphism) 與抽象方法( Abstract method)


class Animal: def __init__(self, name): # Constructor of the class self.name = name def talk(self): # Abstract method, defined by convention only raise NotImplementedError("Subclass must implement abstract method") class Cat(Animal): def talk(self): return 'Meow!' class Dog(Animal): def talk(self): return 'Woof! Woof!' class Fish(Animal): pass animals = [Cat('Missy'), Cat('Garfield'), Dog('Lassie'),Fish('Gold')] for animal in animals: print(animal.name + ': ' + animal.talk())


Traceback (most recent call last): File "D:\Python_lecturer_NTU\python code\python code\EX05_08.py", line 20, in <module> print(animal.name + ': ' + animal.talk()) File "D:\Python_lecturer_NTU\python code\python code\EX05_08.py", line 5, in talk raise NotImplementedError("Subclass must implement abstract method") NotImplementedError: Subclass must implement abstract method







MPU6050量測物體姿態的MEMS感測器



MPU6050: 一個可以用來量測物體姿態的MEMS感測器


3-axis Accelerometer


MPU6050 可以設定量測在16bit 在 +/- 2g,+/- 4g,+/- 8g,+/- 16g的G力範圍, 但為了獲得較好的靈敏度,故設定量測在16bit 在 +/- 2g的G力  (16bit表示正負, 所以範圍是-32,768~32,767)

故靜止平放時, Z軸有一個 1個G力 (就是地心引力) 往下, 所以Z軸會有一個數值約在+/-16,000, 看你是放那正放或反放。
而其實, X,Y,Z 都有1個G力, 故平放不移動, 看那一個軸讀出來的值比較大(+/-16,000, ), 就可以知道Sensor擺放姿態。(平放、站立、側躺)


加速度計功能: 瞬間往某方向快速移動時,可以量出Sensor瞬間移動的方向量, 看是X 左右,Y是前後,Z是上下。


3-axis Gyroscope 

A gyroscope is a device for measuring or maintaining orientation, based on the principles of angular momentum, it is to measure the rate of changes of the angles (deg/s)

角速度計功能: 用來量出Sensor瞬間轉動量, 看是繞X旋轉,繞Y旋轉,繞X旋轉,單位是角速度量。一樣有正負,繞著順時針旋轉還是逆時針旋轉。








2017年3月4日 星期六

電燈分段的電子開關接法


物聯網專題: 電燈分段的電子開關接法


有三段開關: 
                 

第一段 黃、橙線過電=>亮2燈
第二段 橙線過電=>亮1燈
第三段 白線過電=>亮1燈





改變成第一動灯全開的方法: 
  1. 讓橙線接到兩個燈具, 當第二段橙線過電時==> 亮2燈
  2. 把黃線接到白線, 讓黃白線短路, 使第一段橙、黃線過電時==> 亮3燈
第一段 橙、黃線過電=>亮3燈
第二段 橙線過電=>亮2燈
第三段 白線過電=>亮1燈




參考: http://bbs.pigoo.com/thread-61832-1-1.html

2017年3月3日 星期五

TDMA 中的兩種機制



Aloha


Aloha : 沒有coordinate , 想送就送,撞了再重傳
Slotted Aloha: 加入time slot, 要大家同步在同一個time slot 傳送, 但仍然想送就送, 撞了再重傳
Aloha: G exp(-2G)  (G:  mean  arrival rate) if G=1, 1/(e^2)=0.168
Slotted Aloha: G exp(-G)  , if G=1, 1/e=0.36

Slotted Aloha 約是 Aloha 兩倍throughput

CSMA (Carrier Sense Multiple Access) 

"Listen before talk"
CSMA 可以被分類為三種:

  1. non-persistent CSMA: channel busy, 等一段random backoff time, 再偵測channel idle 就送
  2. 1-persistent CSMA: channel busy 不等一段random backoff time, 仍持續偵測channel , idle 就送
  3.  p-Persistent CSMA: channel busy 不等一段random backoff time, 仍持續偵測channel , channle ide , 擲一骰子(機率p) 決定是否要傳送。若是落在'(1-p) 不送,