2015年6月27日 星期六

Installing STM32F4 Development Environment for Linux and Mac OS


1.、Download Toolchain
Visit GNU Tools for ARM Embedded Processors or just use one of the links below to download it

2、Install automake

Mac OS:
$ brew install automake
Linux:
$ sudo apt get install automake

3、Make a New Directory and decompress the tar.bz2 file you download

Mac OS:
$ cd {Your workspace}
$ mkdir toolchain
$ mv {Your download} toolchain
$ cd toolchain
$ tar -jxvf gcc-arm-none-eabi-4_9-2015q2-20150609-mac.tar.bz2

Linux:
$ cd {Your workspace}
$ mkdir toolchain
$ mv {Your download} toolchain
$ cd toolchain
$ tar -jxvf gcc-arm-none-eabi-4_9-2015q2-20150609-linux.tar.bz2

4、Set environment variable
*If you close the terminal , the environment variable set by export command will disappear . 
You need to do those things again when you open a new terminal .    
( If you want to permanently export a variable --> google it  )
$ cd {Your workspace}
$ cd toolchain/gcc-arm-none-eabi-4_9-2015q2/bin
$ export PATH=$PATH:`pwd`

5、Get st-util tool (stlink tool)

$ cd {Your workspace}
$ mkdir sttool
$ cd sttool
$ git init
$ git pull https://github.com/texane/stlink
$ ./autogen.sh
$ ./configure
$ make
$ sudo make install

6、Download stm32f4 blink example
$ cd {Your workspace}
$ mkdir blink
$ cd blink
$ git init 

$ git pull https://github.com/SAM33/STM32F4Blink.git

7、Connect your stm32f4 to computer , and open a new terminal to run st-util

Mac OS:
$ st-util

Linux:
$ sudo st-util
You will get some message like this :
Remember the port which gdbserver running .

8、Go back to last terminal and make stm32f4 blink example
$ cd {Your workspace}
$ cd blink
$ make


9、Upload elf file to stm32f4
$ cd {Your workspace}
$ cd blink
$ arm-none-eabi-gdb
(gdb) tar ext localhost:4242
(gdb) load build/blink.elf
Loading section ..............
Loading section ..............
Loading section ..............
(gdb) run
The program being debugged has been started already.
Start it from beginning? (y or n) y
//now you will see stm32f4 board blinking !!
//stop : input ctrl^c twice
ctrl^c ctrl^c
//close gdb
(gdb) ctrl^d
A debugging session is active ....
Quit anyway? (y or n) y

This command : (gdb) tar ext localhost:????   ---->  ???? is the port you remember
Enable gdb remote debuging using st-util gdb server . You will get some log in your st-util windows ,
 

2015年1月23日 星期五

研究所推甄心得 -- 中央大學軟工所

------------------------------------------------------------------------------------------------------------------

個人背景:
1.喜歡實作各種專案
2.有大專生國科會計畫
3.CPE解題三題~四題水準
4.後段國立大學,班排(系排)前三
5.畢業專題為感測網路多媒體應用相關
6.無任何證照

------------------------------------------------------------------------------------------------------------------

推甄結果:
初試(資料審查) : 88.5 (68.50可進複試)
複試(上機考) : 21.88
總和(資料審查*1.0+上機考*1.5) : 121.32 (84.38以上備取)
結果 : 正取第四名 (總共收十名正取,十四名備取)
P.S. 今年備取備到第六名

------------------------------------------------------------------------------------------------------------------

準備:
中央軟工所初試不太在意校內成績,而且好像不太會刷人(想讓大家上機分高下?),
並且要上傳(也可以不用,只要不怕低分的話...)自己做過的程式專案一份,沒有限
制任何類型,我想審查分數的關鍵應該是專案分數以及CPE成績,另外,資料採
網路上傳方式。

------------------------------------------------------------------------------------------------------------------

初試:
由於畢業專題屬於嵌入式、感測網路,我想不太適合作為軟工所上傳用的專案,
所以我將之前人工智慧相關課程的期末專題改寫來使用,此專題為一象棋對戰
平台程式+AI開發環境平台程式+2隻DEMO用的AI程式,此對戰平台程式能夠讓
兩個玩家(任一玩家都可以是AI程式或人)進行暗棋PK,平台本身用JAVA來撰寫
(圖形化+Socket),AI程式是使用C語言(Socket),平台提供特定協議來讓AI連線、
操控,AI開發環境平台程式使用C#來撰寫,讓使用者能使用此環境來設計適合
對戰平台的AI演算法,自動編譯並產生AI程式,2隻DEMO用AI程式一為DFS
演算法,另一為基因演算法...(離題了,之後有空再介紹)。

書面文件部分則是十分普通,自傳+研究計畫+獎狀,差不多就沒了。

------------------------------------------------------------------------------------------------------------------

複試:
中央軟工所的複試為純上機考,可攜帶任何書面文件,但禁止連上網際網路、
禁止隨身碟、手機等電子產品,考試時間約3個多小時,題目7題左右,全英文
命題,Online judge系統(通過了還要人工審閱斟酌扣分,沒通過直接0分,不額
外人工斟酌給分),可用C、C++、JAVA、VC++語言,但部分題目限制用C或
C++。考前會給時間安裝順手的環境,作業系統為Windows,提供CodeBlock、
visual studio 2012 (.NET4.0) 、Eclipse (剩下的忘了...),個人覺得題目有點難,
屬於較活的應用題,不像CPE有些數學題、演算法題那麼枯燥,印象中的題目
(不照題號,因為題號都忘記了...) : A、一個用C寫的遊戲,叫你改成C++架構
(用多型、繼承...),B、使用system指令外部執行程式、傳參數,並取得回傳結
果(基本上是送分題),C、大數運算(忘記有沒有限用C、C++)。

最後Online judge我只有通過2題,大數運算以及system指令,不過大家的分數
普遍不高,若在此部分表現突出,有非常高的機率能錄取。

------------------------------------------------------------------------------------------------------------------

結語:
我覺得中央軟工所十分重視程式實務能力,若對自己的實作能力有信心,認為
可以在上機考拿到高分,那應該是滿有希望的!:

------------------------------------------------------------------------------------------------------------------

Android NDK -- 環境安裝

好久沒使用JNI來撰寫Android的程式了,今日突然想來複習一下,目標為重新編譯之前的舊JNI APP專案,以此篇文章用來記錄安裝過程,方便之後溫習。

1.將專案Import到Android Studio,重新編譯 : 發生錯誤,找不到NDK


2.下載Android NDK


3.執行自動解壓縮檔案,把NDK放到想要的路徑去,在此為C槽 : 路徑C:\android-ndk-r10d

4.找到專案中的檔案local.properties


5.加入ndk.dir=<NDK路徑>  *注意跳脫字元

6.重新編譯 : 發生錯誤 (記得之前用Eclipse沒這問題)

7.爬了一下文章,貌似是個BUG,只要在jni資料夾下新增一個空的util.c就能解決@@


8.過了,傳到裝置

9.完成

2015年1月4日 星期日

Prolific USB-to-Serial Comm Port (win8 driver安裝)

換了win8之後拿來開發嵌入式系統的神器突然不能用了,就算安裝了驅動程式還是會出現黃色的驚嘆號(裝置無法啟動:代碼10...),經過幾個小時的處理,使用下列方法解決,雖然不一定每台電腦都可用,但是各位可以做個參考:
1.把驅動程式移除(裝置管理員->Prolific USB-to-Serial...->{右鍵}解除安裝驅動程式)
2.控制台->裝置與印表機->(你的電腦那圖示)->{右鍵}裝置安裝設定->否->絕不安裝...
3.手動下載驅動程式(PL2303 Prolific Driverinstaller v110)
我的Google雲端硬碟備份(怕來源不明就自己去載吧,我這也是載來的,來源不確定)
https://drive.google.com/file/d/0BycfqXTMVQ6ARlNNUHNRX0pndkk/view?usp=sharing
4.管理員權限安裝PL2303 Driver (記得拔掉您的裝置)
5.控制台->裝置與印表機->(你的電腦那圖示)->{右鍵}裝置安裝設定->是  (把自動下載復原)
6.裝置裝上去會發現Prolific USB-to-Serial Comm Port裝置的黃色驚嘆號消失了

以上...希望能幫助到您。

2014年11月18日 星期二

JPEG Camera 攝影模組

前幾天入手了這個相機模組(LinkSprite JPEG Color Camera Serial UART Interface )

這個相機模組價格算是很高,但是由於使用Serial Communication,並且是直接傳輸給你JPEG邊碼後的格式,基本上使用起來十分容易,並且能給一些低速的晶片(如大家最常用的Arduino來使用,不一定需要ARM之類的晶片),只需要了解此相機的相關協議,基本上就能使用,在此附上datasheet給各位讀者參考。
LinkSprite JPEG Color Camera Serial UART Interface

此款相機模組和小弟上次購買的ov7670相機模組還差蠻多的,ov7670使用標準的SCCB介面,相容I2C介面,主要是ARM 系統在用的比較多,或是要搭配 tft LCD 做顯示,若要搭配Arduino使用可能會不太合適,所以在此就先不討論。


由於手邊的Arduino只有主板,並沒有搭配sd卡模組之類的,就算接了LinkSprite JPEG Color Camera Serial UART Interface也沒辦反儲存本次的拍攝,所以這次直接使用相機模組+USB to UART晶片連接電腦,使用VC++來實作,連接方法如下圖(看不清楚請另存圖片):



對於VC不熟的,或是對於.NET的SerialPort物件不熟的,也沒關係,主要命令拍攝的程式碼:
























若您是使用Arduino,則自行把SerialWriteChar()和serialport->readChar()或serialport->readByte(),修改為SoftwareSerial.write()以及SoftwareSerial.read()就可以,比較麻煩的是
fopen()、fwrite()、fclose()等等,由於本人沒研究Arduino的sd卡操作,所以此部分要自行改寫。

最後附上完整的VC專案檔案
點我下載(Google雲端)



2014年9月14日 星期日

C# 跨執行緒作業無效: 存取控制項時所使用的執行緒與建立控制項的執行緒不同

最近寫程式時遇到一個問題,如題,但造成的原因是什麼呢?

以下是我寫的遇到問題的程式的程式碼片段Form1.cs的內容
完整的遇到問題之專案請到這裡下載


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        MyLabelController Controller = new  MyLabelController();
        public Form1()
        {
            InitializeComponent();
        }
        public void label1_Hello()
        {
            label1.Text = "Hello";
        }
        public void label1_World()
        {
            label1.Text = "World";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Controller.HelloWorld(this);
        }
    }

    public class MyLabelController
    {
        Form1 Parent;
        public void HelloWorld(Form1 target)
        {
            Parent=target;
            Parent.label1_Hello();
            System.Timers.Timer t = new System.Timers.Timer(3000);
            t.Elapsed += this.Next;
            t.Enabled = true;
        }
        private void Next(Object obj, ElapsedEventArgs e)
        {
            Parent.label1_World();
        }
    }
}


這隻程式主要是透過按下按鈕,然後把label的值改成Hello,並在三秒後把值改成World,但是在這裡並不是由Form1對label的值進行修改,而是由Form1的成員Controller來對label的值進行修改,並且啟動Controller中的Timer來計時,並觸發二度改寫文字的事件,成員Controller的類別是屬於寫在同一個Namespace WindowsFormsApplication1的類別MyLabelController,至於為何我要這樣用? 只是單純想測試可不可以寫一個控制用成員,負責管理其他UI類的成員,只需要操控此控制用成員,此控制用成員能幫你去管理你的所有UI類成員,也就是當有數百個label類的UI成員的時候,成員Controller能夠依照你對它的操控,幫你去管理其他的label類的UI成員。

程式執行畫面大概像這樣:

而按下按鈕,之後會順利的顯示Hello,但是過了三秒後再跳出World的時候,就發生錯誤了

以上就是遇到的問題,或許大家會覺得我這種寫法脫褲子放屁,遇到問題活該,不過由於我剛學C#沒有很久,之前都是學Gnu的C與C++,所以目前還是在學習階段,所以我當遇到問題是學習,既然都遇到問題了,那至少要先把這個問題解決,再以別的方法實踐相同功能,作為C#的學習。

於是後來查了資料,此種問題,是因為我在class MyLabelController中宣告的Timers事件,實際上已經與Form1始於不同的執行續, 但是在Form1呼叫Controller的方法HelloWorld時,是以Form1的執行續互叫來執行的,所以顯示Hello時沒遇到錯誤,直到Controller的Timers事件Elapsed時,此時由於是由MyLabelController的事件處理觸發的事件,故執行續與Form1的執行續不相同,而為了安全起見,兩個不同執行續A、B,A無法控制位於B中的UI物件(控制項),B也無法無法控制位於A中的UI物件(控制項),所以發生了System.InvalidOperationException,造成錯誤。至於要如何修改呢?  在這裡我們必須用到委派(dalegate)的方式來處理

委派如同字面意思,在這裡就是讓Controller委託 Form1去做label1_World()這件事情,由於 Form1本身就是label1UI物件(控制項)得擁有者,由Controller委託 Form1來做,自然就不會遇到A無法控制位於B中的UI物件(控制項)的問題,至於要怎麼改呢?  程式碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Timers;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        MyLabelController Controller = new  MyLabelController();
        public delegate void mydalegate();
        public mydalegate md;

        public Form1()
        {
            InitializeComponent();
            md = new mydalegate(label1_World);
        }
        public void label1_Hello()
        {
            label1.Text = "Hello";
        }
        public void label1_World()
        {
            label1.Text = "World";
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Controller.HelloWorld(this);
        }
    }

    public class MyLabelController
    {
        Form1 Parent;
        public void HelloWorld(Form1 target)
        {
            Parent=target;
            Parent.label1_Hello();
            System.Timers.Timer t = new System.Timers.Timer(3000);
            t.Elapsed += this.Next;
            t.Enabled = true;
        }
        private void Next(Object obj, ElapsedEventArgs e)
        {
            //Parent.label1_World();
            Parent.Invoke(Parent.md);
        }
    }
}
解釋一下下列的程式碼:
public delegate void mydalegate(); 由於我們要委託的對象方法為
public void label1_World(); 是屬於void,並且沒有參數傳入,所以這裡要把 mydalegate 定義成這樣:public delegate void mydalegate();。
接下來,建立委派
public mydalegate md; 並且在Form1的建構子,明確指定此委派的對象為方法為 label1_World;

        public Form1()
        {
            InitializeComponent();
            md = new mydalegate(label1_World);
        }
最後,只要把Next中的程式,修改為

        private void Next(Object obj, ElapsedEventArgs e)
        {
            //Parent.label1_World();
            Parent.Invoke(Parent.md);    //委派Form1去執行md這個委派

        }
就能順利執行了,以下為程式執行結果


改寫後的程式碼,請點這裡下載

2014年3月16日 星期日

作業系統(恐龍本) 第一章整理(1)

整理1.1~1.3

-------------------------------------------------------------------------------------
1 Introduction

01.作業系統的目標(Operating system goals):
     -執行使用者的程式、解決使用者的問題
     -讓電腦系統方便的被使用
     -有效率的管理電腦硬體

-------------------------------------------------------------------------------------
1.1 What Operating Systems Do

02.電腦系統可被分為四個構成要素(Computer system can be divided into four components):
     -硬體
     -作業系統
     -應用程式
     -使用者(包含人、機器或其他電腦)

-------------------------------------------------------------------------------------
1.1.1 User View
1.1.2 System View

03.作業系統做什麼(What Operating Systems Do)
     -Depends on the point of view
           +User View
                 -Users want convenience, ease of use,Don’t care about resource utilization
                 -Handheld computers are resource poor, optimized for usability and
                   battery life
                 -......
           +System View
                 -作業系統是一個資源分配者(OS is a resource allocator)
                       +管理所有的資源
                       +當需求互相衝突時,決定資源要配給何者
                 -作業系統是一個控制程式(OS is a control program)
                       +管理使用者程式的執行,避免錯誤與不合適(errors and improper)的使用電腦

-------------------------------------------------------------------------------------
1.1.3 Defining Operating Systems

04.定義作業系統(Operating System Definition)
     -In general, we have no completely adequate definition of an operating system.
           +In addition,wehave no universally accepted definition of what is part of
              the operating system.
           +operating system is the one program running at all times on the computer
              called the kernel

-------------------------------------------------------------------------------------
1.2 Computer-System Organization

-------------------------------------------------------------------------------------
1.2.1 Computer-System Operation

05.電腦啟動
     -電腦啟動時bootstrap program會被載入到記憶體中
     -bootstrap program通常放在ROM或EPROM,作為韌體(firmware)存在
     -初始化系統的各個層面
           +初始化CPU的暫存器(CPU registers)
           +初始化裝置控制器(device controllers)
           +初始化記憶體的內容(memory contents)
     -載入並執行作業系統的核心(kernel)

06.中斷(interrupt) (p8)
     -軟體發出中斷
           +executing a special operation called a system call
     -硬體發出中斷
           +sending a signal to the CPU, usually by way of the system bus
     -中斷向量(interruptvector)

-------------------------------------------------------------------------------------
1.2.2 Storage Structure

07.Memory的種類
     -DRAM(dynamic random-access memory)
     -ROM(read-only memory)
           +因為ROM不能被修改,只有static的程式,像是開機程式被存在那
     -EEPROM(electrically erasable programmable read-only memory)
           +EEPROM無法被頻繁的修改,通常放著不易改變的程式,以智慧型手機來說,
            EEPROM存著原廠安裝的程式

08.Storage-device hierarchy
     -根據速度價錢以及volatile/nonvolatile,可由高到低排成這樣的層級
           +Register
           +Cache
           +Main memory(DRAM)
           +SSD(solid-state disk)
           +Magnetic disk
           +Optical disk
           +Magnitic tapes
                 -

09.volatile storage 與 nonvolatile storage
     -volatile storage 在停止供電後,會失去所有儲存的資料
     -nonvolatile storage 在停止供電後,不會失去儲存的資料

10.其他特殊設備
     -NVRAM(nonvolatile random-access memory)
           +透過DRAM+電池,擁有DRAM的速度,並能在斷電後保有資料(看電池能用多久)
   
-------------------------------------------------------------------------------------
1.2.3 I/O Structure

11.device controller 與 device driver (p12)
     -每個device driver都是特定用於某個device controller上的
     -device controller位於硬體上,擁有自己的Buffer與Register
     -device driver會去適當的讀取device controller的Register,判斷要做甚麼
      (such as “read a character from the keyboard”
     -作業系統針對不同的device controller擁有不同的device driver

12.DMA(direct memory access)
     -device controller不透CPU,直接把自己整個Block的Buffer傳輸到Main memory的某部分,
      每傳完一個Block丟出一個interupt,通知device driver操作已經完成

-------------------------------------------------------------------------------------
1.3 Computer-System Architecture

-------------------------------------------------------------------------------------
1.3.1 Single-Processor Systems

13.Single-Processor Systems
     -僅具有一個用來處理Instruction set的主CPU
     -幾乎所有的Single-Processor Systems架構仍具有其他.special-purpose processors,
      不過並不會因為有這些special-purpose processors,讓架構變成Multiprocessor systems

14.special-purpose processors
     -They may come in the form of device-specific processors
           +disk controllers
                 -a disk-controller microprocessor receives a sequence of requests
 fromthe main CPU and implements its own disk queue and scheduling algorithm
           +keyboard controllers
           +graphics controllers

-------------------------------------------------------------------------------------
1.3.2 Multiprocessor Systems

15.Multiprocessor systems have three main advantages (p14)
     -提高吞吐量(Increased throughput)
     -更經濟(Economy of scale)
     -增加可靠性(Increased reliability)

16.The multiple-processor systems in use today are of two types.
     -不對稱多處理(Asymmetric multiprocessing)
           +A boss–worker relationship. The boss processor schedules
            and allocates work to the worker processors.
     -對稱多處理(Symmetric multiprocessing , SMP)
           +Each processor performs all tasks within the operating system