在嵌入式領域中,幾乎所有的設備控制和各種協議控制都在同一個嵌入式CPU當中,非常有利於對CPU Core和設備進行抽象。如果能對CPU Core和設備的各種控制進行抽象,人們在移植OS或者開發驅動程序時就沒有必要對CPU進行非常深入的了解,不必要了解某個寄存器的某個位是控制什麼的,也沒有必要了解怎樣初始化某個控制寄存器等等。
在利用控制器編寫裸板應用程序時,編程人員只需要了解該控制器的初始化順序以及初始化的內容而不需要了解初始化的具體細節就能完成應用程序。顯然可以大大的提高工作效率,並且對於硬件的具體細節設置是在應用程序中最容易出錯的地方,而利用控制器則可以大大的減少出錯的可能性。
ARM是功能更加強大的單片機,在學習裸機程序設計的過程中,發現ARM比一般的單片機集成了更多的外設(一般的單片機只集成了串口,還有四組IO端口),包含更多的GPIO,有更多的寄存器,通過寄存器的設置和啟用來控制相關引腳,從而控制相關設備。S3C2440內部集成了硬件控制器,各種驅動協議用硬件控制器產生,我們只需配置對應硬件控制器的寄存器即可產生相應的驅動時序。 高端ARM學習以軟件編程為主(即理解為主),其資源豐富,很少需要需要擴展外設,學習重點在於如何配置寄存器以及如何編寫應用程序。
設置相應的控制寄存器,讀寫相應的數據寄存器就可以。www.linuxidc.com學習裸板程序開發的重點是理解各種接口協議的原理,這樣就可以很好的理解控制器的初始化順序以及初始化的內容。
下面以S3C2440的I2C控制器為例來講解。下面的程序是基於中斷模式,I2C協議的講解可以查看我以前的博客。I2C協議中規定:接收器接收到一個字節(地址或數據)後,要發送一個ACK,發送器接收到ACK後可以接著發送數據或結束數據傳輸。接收器(這裡我們是S3C2440的I2C控制器)收到ACK後會產生一個中斷,在中斷處理程序中可以繼續發送程序或者結束傳輸。理解了I2C協議的基本原理,編寫程序就水到渠成。
- //===================================================================
- // SMDK2440 IIC configuration
- // GPE15=IICSDA, GPE14=IICSCL
- // "Interrupt mode" for IIC block
- //===================================================================
-
- //******************[ Test_Iic ]**************************************
- void Test_Iic(void)
- {
- unsigned int i,j,save_E,save_PE;
- static U8 data[256];
-
- Uart_Printf("\nIIC Test(Interrupt) using AT24C08\n");
-
- save_E = rGPECON; //保存以前的值
- rGPECON |= 0xa00000; //GPE15:IICSDA , GPE14:IICSCL
-
- pISR_IIC = (unsigned)IicInt; //設置中斷處理函數
- rINTMSK &= ~(BIT_IIC); //打開IIC中斷
-
- //Enable ACK, Prescaler IICCLK=PCLK/16, Enable interrupt, Transmit clock value Tx clock=IICCLK/16
- // If PCLK 50.7MHz, IICCLK = 3.17MHz, Tx Clock = 0.198MHz
- rIICCON = (1<<7) | (0<<6) | (1<<5) | (0xf);//第四位為1,SCL線被拉低,傳輸被中斷
-
- rIICADD = 0x10; //2440 slave address = [7:1]
- rIICSTAT = 0x10; //IIC bus data output enable(Rx/Tx)使能接收/發送功能
- rIICLC = (1<<2)|(1); // Filter enable, 15 clocks SDA output delay added by junon
-
- Uart_Printf("Write test data into AT24C08\n");
-
- for(i=0;i<256;i++)
- Wr24C080(0xa0,(U8)i,i);
-
- for(i=0;i<256;i++)
- data[i] = 0;
-
- Uart_Printf("Read test data from AT24C08\n");
-
- for(i=0;i<256;i++)
- Rd24C080(0xa0,(U8)i,&(data[i]));
-
- //Line changed 0 ~ f
- for(i=0;i<16;i++)
- {
- for(j=0;j<16;j++)
- Uart_Printf("%2x ",data[i*16+j]);
- Uart_Printf("\n");
- }
- rINTMSK |= BIT_IIC;
- rGPECON = save_E;
- }
-
-
- //*************************[ Wr24C080 ]****************************
- void Wr24C080(U32 slvAddr,U32 addr,U8 data)
- {
- _iicMode = WRDATA;
- _iicPt = 0;
- _iicData[0] = (U8)addr;
- _iicData[1] = data;
- _iicDataCount = 2;
-
- rIICDS = slvAddr; //0xa0
- rIICSTAT = 0xf0; //MasTx,Start主機發送器 發出S信號
- //Clearing the pending bit isn't needed because the pending bit has been cleared.
-
- while(_iicDataCount!=-1);//等待數據數據發送完畢www.linuxidc.com
-
-
-
-
- //開始一次新的傳輸,不過只發送設備地址,等待應答,以證明上次傳輸完成。而下次Wr24C080的時候會重新開始,這次的傳輸並沒有寫入數據
- _iicMode = POLLACK;
-
- while(1)
- {
- rIICDS = slvAddr;
- _iicStatus = 0x100;
- rIICSTAT = 0xf0; //MasTx,Start
- rIICCON = 0xaf; //Resumes IIC operation.
-
- while(_iicStatus==0x100) ;//等待數據數據發送完畢,狀態寄存器改變了就可以了
-
- if(!(_iicStatus&0x1)) //接收到的最後一位為0 (接收到ACK信號)
- break; //When ACK is received 判斷最後一位的狀態
- }
- rIICSTAT = 0xd0; //Stop MasTx condition
- rIICCON = 0xaf; //Resumes IIC operation.
- Delay(1); //Wait until stop condtion is in effect.
- //Write is completed.
- }
-
- //**********************[ Rd24C080 ] ***********************************
- void Rd24C080(U32 slvAddr,U32 addr,U8 *data)
- {
- _iicMode = SETRDADDR;
- _iicPt = 0;
- _iicData[0] = (U8)addr;
- _iicDataCount = 1;
-
- rIICDS = slvAddr;
- rIICSTAT = 0xf0; //MasTx,Start
- //Clearing the pending bit isn't needed because the pending bit has been cleared.
- while(_iicDataCount!=-1);
-
- _iicMode = RDDATA;
- _iicPt = 0;
- _iicDataCount = 1;
-
- rIICDS = slvAddr;
- rIICSTAT = 0xb0; //MasRx,Start
- rIICCON = 0xaf; //Resumes IIC operation.
- while(_iicDataCount!=-1);
-
- *data = _iicData[1];
- }
-
-
- //-------------------------------------------------------------------------
- void __irq IicInt(void)
- {
- U32 iicSt,i;
-
- rSRCPND = BIT_IIC; //Clear pending bit
- rINTPND = BIT_IIC;
- iicSt = rIICSTAT;
-
- if(iicSt & 0x8){} //When bus arbitration is failed.
- if(iicSt & 0x4){} //When a slave address is matched with IICADD
- if(iicSt & 0x2){} //When a slave address is 0000000b
- if(iicSt & 0x1){} //When ACK isn't received
-
- switch(_iicMode)
- {
- case POLLACK:
- _iicStatus = iicSt;
- break;
-
- case RDDATA:
- if((_iicDataCount--)==0)
- {
- _iicData[_iicPt++] = rIICDS;
-
- rIICSTAT = 0x90; //Stop MasRx condition
- rIICCON = 0xaf; //Resumes IIC operation.
- Delay(1); //Wait until stop condtion is in effect.
- //Too long time...
- //The pending bit will not be set after issuing stop condition.
- break;
- }
- _iicData[_iicPt++] = rIICDS; //The last data has to be read with no ack.
-
- if((_iicDataCount)==0)
- rIICCON = 0x2f; //Resumes IIC operation with NOACK.
- else
- rIICCON = 0xaf; //Resumes IIC operation with ACK
- break;
-
- case WRDATA:
- if((_iicDataCount--)==0)
- {
- rIICSTAT = 0xd0; //Stop MasTx condition
- rIICCON = 0xaf; //Resumes IIC operation.
- Delay(1); //Wait until stop condtion is in effect.
- //The pending bit will not be set after issuing stop condition.
- break;
- }
- rIICDS = _iicData[_iicPt++]; //_iicData[0] has dummy.
- for(i=0;i<10;i++); //for setup time until rising edge of IICSCL
-
- rIICCON = 0xaf; //resumes IIC operation.
- break;
-
- case SETRDADDR:
- // Uart_Printf("[ S%d ]",_iicDataCount);
- if((_iicDataCount--)==0)
- break; //IIC operation is stopped because of IICCON[4]
- rIICDS = _iicData[_iicPt++];
- for(i=0;i<10;i++); //For setup time until rising edge of IICSCL
- rIICCON = 0xaf; //Resumes IIC operation.
- break;
-
- default:
- break;
- }
- }