MCU單晶片韌體設計

2016年8月31日 星期三

Pi 新手教學(二) ---如何使用UART Console 登入Raspberry Pi




紅色Raspberry Pi Shield上內建  USB-to-Serial (PL2303)晶片, 故可以用電腦透過USB界面連接到Raspberry Pi。

硬體安裝


Raspberry Pi 紅色子卡插在Raspberry Pi 上,如下圖所示。





下方是給Pi的電源, 上方紅色子板是作 Console 用 



注意:
  1. Serial-to-USB 的連接器是 microUSB,即手機常用的那種接頭.
  2. Serial-to-USB 線不用時要拔掉不要插在板子上, 以免不小心壓到使連接器折斷或把板子上的PCB pad(銲接點)連根拔起而無法修復.

2016年8月7日 星期日

ARM mbed client Code


What is mbed client?

ARM mbed Client is a library that provides the means to connect constrained embedded devices to mbed Device Connector Service, mbed Device Server and to mbed-enabled cloud services from our partners.


  • Manage devices on mbed Device Server.
  • Securely communicate with internet services over the industry standard TLS/DTLS.
  • Fully control the endpoint and application logic.
class MbedClient: public M2MInterfaceObserver {
public:
    MbedClient(){
        _security = NULL;
        _interface = NULL;    // 產生M2M Interface : M2MInterfaceFactory::create_interface
        _device = NULL;     // 產生Device Object : M2MInterfaceFactory::create_device();
        _object = NULL;    // 產生Resource Object : M2MInterfaceFactory::create_object("Monitor");
_object_gpio = NULL;
        _error = false;
        _registered = false;   //,一旦 M2MObjectList object_registered 成功後, 將 _registered設為true
        _unregistered = false;
        _registration_updated = false;

    }

 ....

}


M2MInterface  --> register_object()          ----callback--> object_register()
                               unregistered_object()  ----callback--> object_unregister()
                               update_registration()    ----callback--> registration_updated()



M2MInterfaceObserver 類別上的5個Callback函式列表

virtual void bootstrap_done (M2MSecurity *server_object)=0
A callback indicating that the bootstap has been performed successfully. More...
virtual void object_registered (M2MSecurity *security_object, const M2MServer &server_object)=0
A callback indicating that the device object has been registered successfully to the LWM2M server. More...
virtual void object_unregistered (M2MSecurity *server_object)=0
A callback indicating that the device object has been successfully unregistered from the LWM2M server. More...
virtual void registration_updated (M2MSecurity *security_object, const M2MServer &server_object)=0
A callback indicating that the device object registration has been successfully updated on the LWM2M server. More...
virtual void error (M2MInterface::Error error)=0
A callback indicating that there was an error during the operation. More...
virtual void value_updated (M2MBase *base, M2MBase::BaseType type)=0
A callback indicating that the value of the resource object is updated by the server.Whenever there is a valid PUT operation for any of the resources, the application will receive a callback:


void error(M2MInterface::Error error){
        _error = true;
        String error_code;
switch(error) {
        case M2MInterface::ErrorNone:
             error_code += "M2MInterface::ErrorNone";
             break;
        case M2MInterface::AlreadyExists:
             error_code += "M2MInterface::AlreadyExists";
             break;

  .....


           }
        printf("\nError occured  : %s\n", error_code.c_str());
    }



There are two types of resources you can create
Dynamic: 值會改變, 當server 請求時要負責傳回值
Static: 值不改變
 configure different types of Objects, Object Instances and Resources 



  • mbed client Document

  1. ARM mbed Client Guide
  2. ARM mbed Client API





2016年8月4日 星期四

關於感測電路應用設計


元件

感應元件通常是利用半導體材料特性,對自然界的物理訊號 , 如光、聲音、壓力、溫度轉換成電氣訊號,而電氣訊號可能是電流、電阻、電壓的訊號。

感測元件例如硫化鎘(CdS)或光敏電阻器(LDR),其阻抗隨光落在它表面的總量而有不同; 焦電式紅外線感知器為因為吸收紅外線而產生AC電壓變化。能將位移、壓力、速度等機械量轉換成為電子訊號的元件稱為機電轉換器(electromechanical transducer),如陀螺儀(Gyroscope)與加速度計(Accelerometer)、E-compass 電子邏盤。透過光的入射與感測材料相互作用來進行工作稱為光感測器。量測人體訊號的稱為生醫感測器

微機電(MEMS)技術的快速發展,使得感測器的尺寸與功耗持續縮小,並讓各式各樣的感測器廣泛的應用在日常生活。

感應元件的特性:
  • 轉換後的電氣訊號都是很微弱的, 如電壓只會有mV (毫伏)
  • 輸出的電氣訊號含有一定的雜訊

感測電路應用設計即利用感測器所進行的電路設計


感測電路設計即利用感測元件的電氣訊號變化來設計"適當"的電路,以輸出如電壓、頻率、派衝等訊號,並滿足訊息傳輸、處理、記錄、顯示、控制的要求。其中, 由於感應元件通常產生的電氣訊號(不論是電壓或電流)都很微小,所以大多數感測電路都會用到OP Amp, 將訊號加以放大。

不論感應元件是屬電流、電壓或電阻的元件,最終經由我們所設計的感測電路都必須是電壓輸出,因為MCU/ SoC 嵌入式系統的輸入端都是看電壓 (為何只看電壓, 而不看電流或電阻, 理由何在?? )。MCU會針對電壓的類比訊號,經由ADC元件轉成數值,即可進行後續運算/演算法處理。





感測電路設計的一個例子:

目標:設計一個能自動點灯的照明設備

功能需求:

  • 可以調整感應距離 (即靈敏度) 
  • 每次觸發的延時時間
  • 感應距離最遠 7 公尺
  • 範圍可達 110 度角
  • 偵測到有人活動時,輸出電壓值 3.3V~5V; 否則輸出 0V
工程師要開始思考, 如何做到這些功能?

1) 有什麼樣感測器以偵測到人?

 PIR (Passive Infrared Sensor )紅外線人體感測器,它主要的感測方法為利用溫度之變化,來感應人之移動。PIR內部焦電元件是強導電體。紅外線照射到強導電體元件上,元件會吸收紅外線產生熱及溫度變化。因溫升會使元件表面產生電荷 (此為焦電效應),後續再經電路進行感測處理後就能產生On/Off信號。
在自然界,任何高於絕對溫度(-273℃)時物體都將產生紅外光譜,不同溫度的物體,其釋放的紅外能量的波長是不一樣的,因此紅外波長與溫度的高低是相關的。人體體溫約在 37 度,所以會發出特定波長 10 微米左右的紅外線。


焦電型紅外線感測器

2) 如何對找到的這個感測器進行功能設計(即感測電路設計)?


http://www.ittraining.com.tw/ittraining/index.php/course/hardware/ec





另外,可以增加一些功能,

  • 如具有調整觸發模式的Jumper :單次觸發,中間偵測有人在移動, 不會延長timer。重複觸發,若偵測仍有人在移動, 會延長timer時間。
  • 使用兩個可變電阻, 一個用來調整感應距離 (即靈敏度) ,另一個用來調整每次觸發的延時時間。

3) PIR模組 ? 

一般常見市售的PIR模組

PIR 模組正面有一個 PE (聚乙烯) 做的平凸透鏡外罩, 用來聚焦紅外線,以增加感測範圍
中間為焦電型紅外線感應器 




PIR 模組的背面,兩個可變電阻可以調整參數
                              
                           

參考資料: 

  1. 焦電型紅外線感應器 規格書下載
  2. 生醫感測器
  3. 更多資訊:: http://www.ittraining.com.tw/ittraining/index.php/course/hardware/ec





2016年8月2日 星期二

Raspberry Pi SSH免密碼登入


1) 下載 putty key-generator
2)在 windows 上 ,用 PuTTYgen: puttygen.exe 產生一對public/private key pair




2) 在SSH server  (Raspberry Pi):


將public key 的內容, 全部貼在該使用者 HOME目錄的~/.ssh/authorized_keys這個檔案內

mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys


ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEArL7fz7+mbNQGTeE5MkcRn57o+mADnC8XA1CPUwTcSGOichlt3KSHyuMJbt2OQOv1lsI44jMnBbA6D5sEff1CWE7FDRizqZeG9Hrc5EioAs26y4/Tv1Mcj7bN182SrUlHEn5UJNGvjGEq3QVtE7C57EDbCZeiWIMuOQo3mizUsRxmCeMrwveK2pYPqmzsKHelFY82ZC/APJtkscSrmYm5NTG4Lxe18mXdT52QaP6gGMVq5O9R5201mfSuuxP5SEqlUO4UGjxAygaRBDV4qYL6cP3k1qcm4AeXGA4dEyy9CbjX/PzxOFYcV35W9phypFIPZW99a7X5HgC/BQ7mOsWXPw== rsa-key-20160911

== 後面的rsa-key-20160911是一個自訂字串, 當你貼很多個public key讓你可以知道這個所貼上的public key的來源或產生日期等資訊

3) SSH client (Windows)

在 putty 內設定,在Auth SSH, 指定剛儲存下來的private key檔案 (Save private key), 再次登入使用該user 去Login , 就不用再輸入密碼了





用putty登入時, 就只要輸入linux 登入帳號,不必再輸入密碼了




3) 若有多個帳號想要使用同一組public/private key, 可以複製一組成功的到其他帳號home目錄下

例如, 複製student到 root

cp -a /home/student/.ssh /root 
chown -R root.root /root/.ssh   #.shh底下檔案的擁用者要全部改成登入者

下載

提醒: 

使用SSH登登入, 只須輸入帳號,不用再輸入密碼了, 登入的帳號,必須是系統有的帳立的帳號(add user)且被允許使用SSH連入系統

例如, 原本就有設定 禁止 root 的登入, 那要修改 /etc/ssh/sshd_config, 使得 root 以遠端連線的方式登入, 找到 PermitRootLogin , 並修改如下

PermitRootLogin yes



----------------------------------------------------------------------------
Note:

停用密碼認證的登入方式,只允許金鑰認證,可以修改 /etc/ssh/sshd_config ,

PasswordAuthentication no 
PubkeyAuthentication yes

# 更改後須再重新啓動SSH server
sudo service ssh restart


........................







2016年8月1日 星期一

brk, sbrk - change data segment size


brk, sbrk - change data segment size


brk and sbrk are basic memory management system calls used in Unix and Unix-like operating systems to control the amount of memory allocated to the data segment of the process.[1] 
These functions are typically called from a higher-level memory management library function such as malloc.
In the original Unix system, brk and sbrk were the only ways in which applications could acquire additional data space; later versions allowed this to also be done using the mmap call.


Function signatures and behavior



#include <unistd.h>

int brk(void *addr);

void *sbrk(intptr_t increment);

brk() and sbrk() change the location of the program break, which defines the end of the process's data segment (i.e., the program break is the first location after the end of the uninitialized data segment). Increasing the program break has the effect of allocating memory to the process; decreasing the break deallocates memory.

brk() sets the end of the data segment to the value specified by addr, when that value is reasonable, the system has enough memory, and the process does not exceed its maximum data size (see setrlimit(2)).

sbrk() increments the program's data space byincrement bytes. Calling sbrk() with an increment of 0 can be used to find the current location of the program break.