TM4C123-Peripheral Driver Library

本文主要是對TI的TivaWare™ Peripheral Driver Library USER’S GUIDE(spmu298d.pdf)文件的閱讀摘錄,是外設庫的簡介及對寄存器、庫函數兩種編程方式的認識。
本文重點是對支持寄存器訪問方式的頭文件中寄存器命名方式的理解。

一、外設庫的簡介

該庫是一個訪問外設的驅動集。
While they are not drivers in the pure operating system sense (that is, they do not have a common interface and do not connect into a global device driver infrastructure), they do provide a mechanism that makes it easy to use the device’s peripherals
雖然它們不是純操作系統意義上的驅動,即它們沒有統一的接口,不連接到全局設備驅動基礎結構。 但它們的確使訪問外設更加方便

Where possible, computations that can be performed at compile time are done there instead
of at run time.
在編譯時能完成的計算將在編譯時完成

The drivers do not support the full capabilities of the hardware
the existing code can be used as a reference upon which to add support for the additional
capabilities.
該驅動不支持外設的全部功能,可以參考現有代碼來增加功能

The APIs have a means of removing all error checking code. Because the error checking is
usually only useful during initial program development, it can be removed to improve code size
and speed.
這些API有刪除錯誤檢查代碼的方法。 因爲錯誤檢查只在開發階段有用。不進行錯誤檢查可以減小代碼量並提高速度

二、外設庫的結構

driverlib/ 這個文件夾包含外設的驅動源碼
.inc/ 這個文件夾爲定義的用於直接寄存器訪問編程的頭文件
該文件夾中文件爲:
hw_*.h Header files, one per peripheral, that describe all the registers and the bit fields within those registers for each peripheral. These header files are used by the drivers to directly access a peripheral, and can be used by application code to bypass the peripheral driver library API.
這些文件用於直接寄存器訪問,編程時也可以用這些頭文件配置外設以替代API函數
makedefs 這是make files. 用的一些定義集

三、外設庫的使用

The peripheral driver library provides support for two programming models: the direct register access model and the software driver model. Each model can be used independently or combined, based on the needs of the application or the programming environment desired by the developer.
有寄存器版本和庫函數版本,可以單獨使用或混合使用
1.direct register access直接寄存器訪問
In the direct register access model, the peripherals are programmed by the application by writing values directly into the peripheral’s registers.
這種方式直接給外設寄存器賦值。

inc文件夾中定義了一系列的宏定義以方便編程。
對寄存器的宏定義在與單片機型號對應的頭文件中
(for example, the header file for the TM4C123GH6PZ
microcontroller is inc/ tm4c123gh6pz .h).

編程時將相應的頭文件包含就可以使用其中定義的宏定義。
以 tm4c123gh6pz.h 文件爲例 講解宏定義的命名規則。

  • Values that end in _R are used to access the value of a register. For example, SSI0_CR0_R is used to access the CR0 register in the SSI0 module.
    以 _R 結尾的定義都定義了寄存器地址,它用來訪問相應的寄存器。

    延伸:在inc中的 hw_memmap.h 中宏定義了每個外設在內存中的基地址,driverlib 中每個外設的頭文件又定義了相應寄存器的偏移量。然後在庫函數版本中經常有HWREG(base +offset )用來訪問外設寄存器。 庫函數版本就是爲直接寄存器訪問加了個封裝,函數調用會降低速度。
    對HWREG及寄存器地址參見文章:外設寄存器地址說明

  • Values that end in _M represent the mask for a multi-bit field in a register. If the value placed in the multi-bit field is a number, there is a macro with the same base name but ending with _S (for example, SSI_CR0_SCR_M and SSI_CR0_SCR_S). If the value placed into the multi-bit field is an enumeration, then there are a set of macros with the same base name but ending with identifiers for the various enumeration values (for example, the SSI_CR0_FRF_M macro defines the bit field, and the SSI_CR0_FRF_NMW, SSI_CR0_FRF_TI, and SSI_CR0_FRF_MOTO macros provide the enumerations for the bit field).

  • Values that end in _S represent the number of bits to shift a value in order to align it with a multi-bit field. These values match the macro with the same base name but ending with _M.

以_M結尾的是多位域,如果這個多位域的配置值是一個數字,那麼會有一個對應的以_s結尾的宏定義,該定義爲該多位域在寄存器中的偏移量。
如果在這個多位域中的值是枚舉量,將有一系列的宏定義,該定義的結尾表明枚舉值意義。

  • All register name macros start with the module name and instance number (for example, SSI0 for the first SSI module) and are followed by the name of the register as it appears in the datasheet (for example, the CR0 register in the data sheet results in SSI0_CR0_R).
    寄存器名以外設模塊名+外設序號開頭,緊跟着是寄存器名,結尾爲_R
  • All register bit fields start with the module name, followed by the register name, and then followed by the bit field name as it appears in the data sheet. For example, the SCR bit field in the CR0 register in the SSI module is identified by SSI_CR0_SCR
    寄存器的位以模塊名開頭,緊跟着是寄存器名,以手冊中定義的位名結尾

通過tm4c123gh6pz數據手冊中SSI_CR0寄存器的示意圖及tm4c123gh6pz.h頭文件中的宏定義來直觀理解一下上面的話。

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

//*****************************************************************************
//
// The following are defines for the bit fields in the SSI_O_CR0 register.
//
//*****************************************************************************
#define SSI_CR0_SCR_M           0x0000FF00  // SSI Serial Clock Rate
#define SSI_CR0_SPH             0x00000080  // SSI Serial Clock Phase
#define SSI_CR0_SPO             0x00000040  // SSI Serial Clock Polarity
#define SSI_CR0_FRF_M           0x00000030  // SSI Frame Format Select
#define SSI_CR0_FRF_MOTO        0x00000000  // Freescale SPI Frame Format
#define SSI_CR0_FRF_TI          0x00000010  // Synchronous Serial Frame Format
#define SSI_CR0_FRF_NMW         0x00000020  // MICROWIRE Frame Format
#define SSI_CR0_DSS_M           0x0000000F  // SSI Data Size Select
#define SSI_CR0_DSS_4           0x00000003  // 4-bit data
#define SSI_CR0_DSS_5           0x00000004  // 5-bit data
#define SSI_CR0_DSS_6           0x00000005  // 6-bit data
#define SSI_CR0_DSS_7           0x00000006  // 7-bit data
#define SSI_CR0_DSS_8           0x00000007  // 8-bit data
#define SSI_CR0_DSS_9           0x00000008  // 9-bit data
#define SSI_CR0_DSS_10          0x00000009  // 10-bit data
#define SSI_CR0_DSS_11          0x0000000A  // 11-bit data
#define SSI_CR0_DSS_12          0x0000000B  // 12-bit data
#define SSI_CR0_DSS_13          0x0000000C  // 13-bit data
#define SSI_CR0_DSS_14          0x0000000D  // 14-bit data
#define SSI_CR0_DSS_15          0x0000000E  // 15-bit data
#define SSI_CR0_DSS_16          0x0000000F  // 16-bit data
#define SSI_CR0_SCR_S           8

//*****************************************************************************

結命圖示及上述代碼
SCR是一個多位域, 其中的值爲數字 ,起始位爲第8位。
所以會有
#define SSI_CR0_SCR_M 定義表明這個多位域
以及對應的:
#define SSI_CR0_SCR_S 8 表明偏移量爲8

DSS是一個多位域,有定義:
#define SSI_CR0_DSS_M 0x0000000F // SSI Data Size Select
但其中的值是枚舉值,所以緊跟着有一系列定義:
#define SSI_CR0_DSS_4 0x00000003 // 4-bit data
#define SSI_CR0_DSS_5 0x00000004 // 5-bit data
#define SSI_CR0_DSS_6 0x00000005 // 6-bit data
……

SPH是一個單位域,所以有唯一的定義:
#define SSI_CR0_SPH 0x00000080 // SSI Serial Clock Phase

直接訪問寄存器的編程例子如下所示:

SSI0_CR0_R = ((5 << SSI_CR0_SCR_S) | SSI_CR0_SPH | SSI_CR0_SPO |
SSI_CR0_FRF_MOTO | SSI_CR0_DSS_8);
SSI0_CR0_R = 0x000005c7;
ulValue = (SSI0_CR0_R & SSI_CR0_SCR_M) >> SSI0_CR0_SCR_S;
//本句代碼爲從CR0寄存器的SCR位域取出其中的值

2.Software Driver Model
In the software driver model, the API provided by the peripheral driver library is used by applications to control the peripherals.
以下代碼爲用API配置SSI的CR0寄存器

SSIConfigSetExpClk(SSI0_BASE, 50000000, SSI_FRF_MOTO_MODE_3,
SSI_MODE_MASTER, 1000000, 8);

3.兩種方式的優缺及混合使用
api方便易用但代碼效率會低,所以適用於對速度要求不嚴格的配置階段
直接寄存器方式高效,適用於操作階段。
For example, the software driver model can be used to configure the peripherals (because this is not performance critical) and the direct register access model can be used for operation of the peripheral (which may be more performance critical). Or, the software driver model can be used for peripherals that are not performance critical (such as a UART used for data logging) and the direct register access model for performance critical peripherals (such as the ADC module used to capture real-time analog data).

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章