Professional Documents
Culture Documents
Support Chips:
NuMicro
TM
NUC100 series
Support Platforms:
Nuvoton
V1.05.002
-2-
Table of Contents
1. Overview ................................................................................................................ 18
1.1. Organization ......................................................................................................................... 18
1.2. Relative Documents ............................................................................................................. 18
1.3. Abbreviations and Glossaries .............................................................................................. 18
1.4. Data Type Definition ........................................................................................................... 20
DrvSYS_SelectHCLKSource ........................................................................................................................... 38
DrvSYS_SelectSysTickSource ......................................................................................................................... 38
DrvSYS_SelectIPClockSource ......................................................................................................................... 39
DrvSYS_SetClockDivider ................................................................................................................................ 41
DrvSYS_SetOscCtrl ......................................................................................................................................... 41
DrvSYS_SetPowerDownWakeUpInt ............................................................................................................... 42
DrvSYS_EnterPowerDown .............................................................................................................................. 43
DrvSYS_SelectPLLSource ............................................................................................................................... 44
DrvSYS_SetPLLMode ..................................................................................................................................... 44
DrvSYS_GetExtClockFreq ............................................................................................................................... 45
DrvSYS_GetPLLContent ................................................................................................................................. 45
DrvSYS_SetPLLContent .................................................................................................................................. 46
DrvSYS_GetPLLClockFreq ............................................................................................................................. 46
DrvSYS_GetHCLKFreq ................................................................................................................................... 47
DrvSYS_Open .................................................................................................................................................. 47
DrvSYS_SetFreqDividerOutput ....................................................................................................................... 48
DrvSYS_EnableHighPerformanceMode .......................................................................................................... 49
DrvSYS_DisableHighPerformanceMode ......................................................................................................... 50
DrvSYS_Delay ................................................................................................................................................. 50
DrvSYS_GetChipClockSourceStatus ............................................................................................................... 51
DrvSYS_GetClockSwitchStatus ....................................................................................................................... 52
DrvSYS_ClearClockSwitchStatus .................................................................................................................... 52
DrvSYS_GetVersion ........................................................................................................................................ 53
3.5. Macros.................................................................................................................................. 57
_DRVUART_SENDBYTE .............................................................................................................................. 57
_DRVUART_RECEIVEBYTE ........................................................................................................................ 57
_DRVUART_SET_DIVIDER.......................................................................................................................... 58
_DRVUART_RECEIVEAVAILABLE ........................................................................................................... 58
_DRVUART_WAIT_TX_EMPTY .................................................................................................................. 58
DrvUART_DisableInt ....................................................................................................................................... 63
DrvUART_ClearIntFlag ................................................................................................................................... 63
DrvUART_GetIntStatus ................................................................................................................................... 64
DrvUART_GetCTSInfo.................................................................................................................................... 65
DrvUART_SetRTS ........................................................................................................................................... 66
DrvUART_Read ............................................................................................................................................... 67
DrvUART_Write .............................................................................................................................................. 68
DrvUART_EnablePDMA ................................................................................................................................. 69
DrvUART_DisablePDMA................................................................................................................................ 69
DrvUART_SetFnIRDA .................................................................................................................................... 70
DrvUART_SetFnRS485 ................................................................................................................................... 71
DrvUART_SetFnLIN ....................................................................................................................................... 72
DrvUART_GetVersion ..................................................................................................................................... 73
DrvTIMER_GetCaptureData ............................................................................................................................ 91
DrvTIMER_GetCaptureIntFlag ........................................................................................................................ 92
DrvTIMER_ClearCaptureIntFlag ..................................................................................................................... 92
DrvTIMER_EnableCaptureDebounce .............................................................................................................. 93
DrvTIMER_DisableCaptureDebounce ............................................................................................................. 94
DrvTIMER_EnableCounterDebounce .............................................................................................................. 94
DrvTIMER_DisableCounterDebounce ............................................................................................................. 95
DrvTIMER_SelectCounterDetectPhase ........................................................................................................... 96
DrvTIMER_GetVersion ................................................................................................................................... 96
DrvWDT_Open ................................................................................................................................................ 97
DrvWDT_Close ................................................................................................................................................ 97
DrvWDT_InstallISR ......................................................................................................................................... 98
DrvWDT_Ioctl .................................................................................................................................................. 98
V1.05.002
- 12 -
DrvI2S_EnableRxDMA.................................................................................................................................. 392
DrvI2S_DisableRxDMA ................................................................................................................................ 392
DrvI2S_EnableTx ........................................................................................................................................... 393
DrvI2S_DisableTx .......................................................................................................................................... 393
DrvI2S_EnableRx ........................................................................................................................................... 394
DrvI2S_DisableRx .......................................................................................................................................... 394
DrvI2S_EnableTxMute ................................................................................................................................... 395
DrvI2S_DisableTxMute.................................................................................................................................. 395
DrvI2S_EnableMCLK .................................................................................................................................... 395
DrvI2S_DisableMCLK ................................................................................................................................... 396
DrvI2S_ClearTxFIFO ..................................................................................................................................... 396
DrvI2S_ClearRxFIFO ..................................................................................................................................... 397
DrvI2S_SelectClockSource ............................................................................................................................ 397
DrvI2S_GetSourceClockFreq ......................................................................................................................... 398
DrvI2S_GetVersion ........................................................................................................................................ 398
V1.05.002
- 17 -
1. Overview
1.1. Organization
This document describes the NuMicro NUC100 series driver reference manual. System-level software
developers can use the NuMicro NUC100 series driver to do the fast application software development,
instead of using the register level programming, which can reduce the total development time significantly.
In this document, a description, usage and an illustrated example code are provided for each driver
application interface. The full driver samples and driver source codes can be found in the BSP (Board
Support Package) of the NuMicroTM NUC100 series.
This document is organized into several chapters. Chapter 1 is an overview. From Chapter 2 to Chapter 17
are the detailed driver descriptions including the followings: System Driver, UART Driver, Timer Driver,
GPIO Driver, ADC Driver, SPI Driver, I2C Driver, RTC Driver, CAN Driver, PWM Driver, PS2 Driver,
FMC Driver, USB Driver, PDMA Driver, I2S Driver and EBI Driver.
Finally, for the NuMicroTM NUC100 series selection guide and product identity list are described in
Appendix.
Analog-to-Digital Converter
AHB
AMBA
APB
BOD
BUF
Buffer
CAN
CFG
Configuration
DSQ
Data Sequence
V1.05.002
- 18 -
EBI
EP
End Point
FIFO
First-In-First-Out
FLD
Float-Detection
FMC
GPIO
I2C
I2S
LIN
LVR
PDID
PDMA
PHY
Physical layer
PLL
Phase-Locked Loop
POR
Power On Reset
PWM
Pulse-Width Modulation
PS/2
SPI
TOG
Toggle
TRIG
Trigger
TRM
UART
V1.05.002
- 19 -
Definition
singed char
signed short
signed int
unsigned char
unsigned short
unsigned int
Description
8 bits signed integer
16 bits signed integer
32 bits signed integer
8 bits unsigned integer
16 bits unsigned integer
32 bits unsigned integer
V1.05.002
- 20 -
2. SYS Driver
2.1. Introduction
The following functions are included in System Manager and Clock Controller section,
Product Device ID
System management registers for chip and module functional reset.
Brown-Out and chip miscellaneous control.
Clock generator
System clock and peripherals clock
Power down mode
V1.05.002
- 21 -
Value
1
2
3
4
5
8
9
12
13
14
15
16
17
18
20
21
22
23
24
27
28
29
32
33
Description
GPIO reset
Timer0 reset
Timer1 reset
Timer2 reset
Timer3 reset
I2C0 reset
I2C1 reset
SPI0 reset
SPI1 reset
SPI2 reset
SPI3 reset
UART0 reset
UART1 reset
UART2 reset
PWM0~3 reset
PWM4~7 reset
Analog Comparator reset
PS2 reset
CAN0 reset
USB device reset
ADC reset
I2S reset
PDMA reset
EBI reset
E_SYS_IP_CLK
Enumeration identifier
E_SYS_WDT_CLK
E_SYS_RTC_CLK
E_SYS_TMR0_CLK
E_SYS_TMR1_CLK
E_SYS_TMR2_CLK
E_SYS_TMR3_CLK
E_SYS_FDIV_CLK
E_SYS_I2C0_CLK
Value
0
1
2
3
4
5
6
8
Description
Watch Dog Timer clock enable control
RTC clock enable control
Timer0 clock enable control
Timer1 clock enable control
Timer2 clock enable control
Timer3 clock enable control
Clock Divider clock enable control
I2C0 clock enable control
E_SYS_I2C1_CLK
E_SYS_SPI0_CLK
E_SYS_SPI1_CLK
E_SYS_SPI2_CLK
E_SYS_SPI3_CLK
E_SYS_UART0_CLK
E_SYS_UART1_CLK
E_SYS_UART2_CLK
E_SYS_PWM01_CLK
E_SYS_PWM23_CLK
E_SYS_PWM45_CLK
E_SYS_PWM67_CLK
E_SYS_CAN0_CLK
E_SYS_USBD_CLK
E_SYS_ADC_CLK
E_SYS_I2S_CLK
E_SYS_ACMP_CLK
E_SYS_PS2_CLK
E_SYS_PDMA_CLK
E_SYS_ISP_CLK
E_SYS_EBI_CLK
9
12
13
14
15
16
17
18
20
21
22
23
24
27
28
29
30
31
33
34
35
E_SYS_PLL_CLKSRC
Enumeration identifier
E_SYS_EXTERNAL_12M
E_SYS_INTERNAL_22M
Value
0
1
Description
PLL source clock is from external 12MHz
PLL source clock is from internal 22MHz
E_SYS_IP_DIV
Enumeration identifier
E_SYS_ADC_DIV
E_SYS_UART_DIV
E_SYS_USB_DIV
E_SYS_HCLK_DIV
Value
0
1
2
3
Description
ADC source clock divider setting
UART source clock divider setting
USB source clock divider setting
HCLK source clock divider setting
E_SYS_IP_CLKSRC
Enumeration identifier
E_SYS_WDT_CLKSRC
E_SYS_ADC_CLKSRC
E_SYS_TMR0_CLKSRC
E_SYS_TMR1_CLKSRC
Value
0
1
2
3
Description
Watch Dog Timer clock source setting
ADC clock source setting
Timer0 clock source setting
Timer1 clock source setting
V1.05.002
- 24 -
E_SYS_TMR2_CLKSRC
E_SYS_TMR3_CLKSRC
E_SYS_UART_CLKSRC
E_SYS_PWM01_CLKSRC
E_SYS_PWM23_CLKSRC
E_SYS_I2S_CLKSRC
E_SYS_FRQDIV_CLKSRC
E_SYS_PWM45_CLKSRC
E_SYS_PWM67_CLKSRC
4
5
6
7
8
9
10
11
12
E_SYS_CHIP_CLKSRC
Enumeration identifier
E_SYS_XTL12M
E_SYS_XTL32K
E_SYS_OSC22M
E_SYS_OSC10K
E_SYS_PLL
Value
0
1
2
3
4
Description
Select External 12M Crystal
Select External 32K Crystal
Select Internal 22M Oscillator
Select Internal 10K Oscillator
Select PLL clock
E_SYS_PD_TYPE
Enumeration identifier
E_SYS_IMMEDIATE
E_SYS_WAIT_FOR_CPU
Value
0
1
Description
Enter power down immediately
Enter power down wait CPU sleep command
2.4. Functions
DrvSYS_ReadProductID
Prototype
uint32_t DrvSYS_ReadProductID (void);
Description
To read product device identity. The Product Device ID is depended on Chip part number.
Please refer to PDID Table of Appendix in details.
Parameter
None
Include
Driver/DrvSYS.h
V1.05.002
- 25 -
Return Value
Product Device ID
Example
uint32_t u32data;
u32data = DrvSYS_ReadProductID ( );
DrvSYS_GetResetSource
Prototype
uint32_t DrvSYS_GetResetSource (void);
Description
To identify reset source from last operation. The corresponding reset source bits are listed in
Register RSTSRC of TRM in details.
Bit Number
Bit 0
Bit 1
Bit 2
Bit 3
Bit 4
Bit 5
Bit 6
Bit 7
Description
Power On Reset
RESET Pin
Watch Dog Timer
Low Voltage Reset
Brown-Out Detector Reset
Cortex M0 Kernel Reset
Reserved
CPU Reset
Parameter
None
Include
Driver/DrvSYS.h
Return Value
The value in RSTSRC register.
Example
uint32_t u32data;
u32data = DrvSYS_GetResetSource ( );
DrvSYS_ClearResetSource
Prototype
uint32_t DrvSYS_ClearResetSource (uint32_t u32Src);
Description
V1.05.002
- 26 -
Succeed
Example
DrvSYS_ClearResetSource (1 << 3);
DrvSYS_ResetIP
Prototype
void
Description
To reset IP include GPIO, Timer0, Timer1, Timer2, Timer3, I2C0, I2C1, SPI0, SPI1, SPI2,
SPI3, UART0, UART1, UART2, PWM03, PWM47, ACMP, PS2, CAN0, USBD, ADC, I2S,
PDMA, and EBI.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API to reset PDMA or EBI. User can check the status of the Register Write-Protection
function with DrvSYS_IsProtectedRegLocked ( ).
Parameter
eIpRst
[in]
/* Reset I2C0 */
DrvSYS_ResetIP (E_SYS_SPI0_RST);
/* Reset SPI0 */
DrvSYS_ResetIP (E_SYS_UART0_RST);
/* Reset UART0 */
V1.05.002
- 27 -
DrvSYS_ResetCPU
Prototype
void
DrvSYS_ResetCPU (void);
Description
To reset CPU. Software will set CPU_RST (IPRSTC1 [1]) to reset Cortex-M0 CPU kernel
and Flash memory controller (FMC).
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_ResetCPU ( );
DrvSYS_ResetChip
Prototype
void
DrvSYS_ResetChip (void);
Description
To reset whole chip, including Cortex-M0 CPU kernel and all peripherals.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
Return Value
V1.05.002
- 28 -
None
Example
DrvSYS_ResetChip ( );
DrvSYS_SelectBODVolt
Prototype
void
Description
To select Brown-Out threshold voltage.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u8Volt [in]
3: 4.5V, 2: 3.8V, 1: 2.7V, 0: 2.2V
Include
Driver/DrvSYS.h
Return Value
None.
Example
DrvSYS_SelectBODVolt (0);
DrvSYS_SelectBODVolt (1);
DrvSYS_SelectBODVolt (2);
DrvSYS_SetBODFunction
Prototype
void
DrvSYS_SetBODFunction (int32_t i32Enable, int32_t i32Flag, BOD_CALLBACK
bodcallbackFn);
Description
To enable Brown-out detector and select Brown-out reset function or interrupt function. If
Brown-Out interrupt function is selected, this will install call back function for BOD interrupt
handler. When the voltage of AVDD Pin is lower than selected Brown-Out threshold voltage,
Brown-out detector will reset chip or assert an interrupt. User can use
DrvSYS_SelectBODVolt ( ) to select Brown-Out threshold voltage.
V1.05.002
- 29 -
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
i32Enable [in]
1: enable, 0: disable
i32Flag [in]
1: enable Brown-out reset function, 0: enable Brown-out interrupt function
bodcallbackFn [in]
Install Brown-Out call back function when interrupt function is enabled.
Include
Driver/DrvSYS.h
Return Value
None
Example
/* Enable Brown-Out Detector , select Brown-Out Interrupt function and install callback
function BOD_CallbackFn */
DrvSYS_SetBODFunction (1, 0, BOD_CallbackFn);
/* Enable Brown-Out Detector and select Brown-Out reset function */
DrvSYS_SetBODFunction (1, 1, NULL);
/* Disable Brown-Out Detector */
DrvSYS_SetBODFunction (0, 0, NULL);
DrvSYS_EnableBODLowPowerMode
Prototype
void
DrvSYS_EnableBODLowPowerMode (void);
Description
To enable Brown-out Detector low power mode. The Brown-Out Detector consumes about
100uA in normal mode, the low power mode can reduce the current to about 1/10 but slow
the Brown-Out Detector response.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
V1.05.002
- 30 -
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_EnableBODLowPowerMode ( );
DrvSYS_DisableBODLowPowerMode
Prototype
void
DrvSYS_DisableBODLowPowerMode (void);
Description
To disable Brown-out Detector low power mode.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_DisableBODLowPowerMode ( );
DrvSYS_EnableLowVoltReset
Prototype
void
DrvSYS_EnableLowVoltReset (void);
Description
To enable low voltage reset function reset the chip when input voltage is lower than LVR
circuit. The typical threshold is 2.0V. The characteristics of LVR threshold voltage is shown
in Electrical Characteristics Section of TRM.
V1.05.002
- 31 -
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_EnableLowVoltRst ( );
DrvSYS_DisableLowVoltReset
Prototype
void
DrvSYS_DisableLowVoltReset (void);
Description
To disable low voltage reset function.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_DisableLowVoltRst ( );
DrvSYS_GetBODState
Prototype
uint32_t DrvSYS_GetBODState (void);
V1.05.002
- 32 -
Description
To get Brown-out Detector state.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
1: the detected voltage is lower than BOD threshold voltage.
0: the detected voltage is higher than BOD threshold voltage.
Example
uint32_t u32flag;
/* Get Brown-out state if Brown-out detector function is enabled */
u32flag = DrvSYS_GetBODState ( );
DrvSYS_EnableTemperatureSensor
Prototype
void
DrvSYS_EnableTemperatureSensor (void);
Description
To enable temperature sensor function.
Parameters
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_EnableTemperatureSensor ( );
DrvSYS_DisableTemperatureSensor
Prototype
void
DrvSYS_DisableTemperatureSensor (void);
Description
V1.05.002
- 33 -
DrvSYS_UnlockProtectedReg
Prototype
int32_t
DrvSYS_UnlockProtectedReg (void);
Description
To unlock the protected registers. Some of the system control registers need to be protected to
avoid inadvertent write and disturb the chip operation. These system control registers are
locked after the power on reset. If user needs to modify these registers, user must UNLOCK
them. These protected registers are listed in Register REGWRPROT of System Manager
Section of TRM in details.
Parameters
None
Include
Driver/DrvSYS.h
Return Value
0
Succeed
<0
Failed
Example
int32_t i32ret;
/* Unlock protected registers */
i32ret = DrvSYS_UnlockProtectedReg ( );
DrvSYS_LockProtectedReg
Prototype
int32_t
DrvSYS_LockProtectedReg (void);
V1.05.002
- 34 -
Description
To re-lock the protected registers. Recommend user to re-lock the protected register after
modifying these registers
Parameters
None
Include
Driver/DrvSYS.h
Return Value
0
Succeed
<0
Failed
Example
int32_t i32ret;
/* Lock protected registers */
i32ret = DrvSYS_LockProtectedReg ( );
DrvSYS_IsProtectedRegLocked
Prototype
int32_t
DrvSYS_IsProtectedRegLocked (void);
Description
To check the protected registers are locked or not.
Parameters
None
Include
Driver/DrvSYS.h
Return Value
1: The Protected Registers are unlocked.
0: The Protected Registers are locked.
Example
int32_t i32flag;
/* Check the protected registers are unlocked or not */
i32flag = DrvSYS_IsProtectedRegLocked ( );
If (i32flag)
/* do something for unlock */
V1.05.002
- 35 -
else
/* do something for lock */
DrvSYS_EnablePOR
Prototype
void
DrvSYS_EnablePOR (void);
Description
To re-enable power-on-reset control.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameters
None
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_EnablePOR ( );
DrvSYS_DisablePOR
Prototype
void
DrvSYS_DisablePOR (void);
Description
To disable power-on-reset control. When power on, the POR circuit generates a reset signal
to reset the whole chip function, but noise on the power may cause the POR active again.
User can disable the POR control circuit for this condition.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameters
None
V1.05.002
- 36 -
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_DisablePOR ( );
DrvSYS_SetIPClock
Prototype
void
Description
To enable or disable IP clock include Watch Dog TImer, RTC, Timer0, Timer1, Timer2,
Timer3, I2C0, I2C1, SPI0, SPI1, SPI2, SPI3, UART0, UART1, UART2, PWM01, PWM23,
PWM45, PWM67, CAN0, USBD, ADC, I2S, ACMP, PS2, PDMA, EBI, Flash ISP controller
and Frequency Divider Output.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API to enable or disable the clock of Watch Dog Timer. User can check the status of the
Register Write-Protection function with DrvSYS_IsProtectedRegLocked ( ).
Parameter
eIpClk [in]
Enumeration for IP clock, reference the E_SYS_IP_CLK of Section 2.3.
i32Enable [in]
1: enable, 0: disable
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_SetIPClock (E_SYS_I2C0_CLK, 1);
- 37 -
DrvSYS_SelectHCLKSource
Prototype
int32_t
Description
To select HCLK clock source from external 12M crystal clock, external 32K crystal clock,
PLL clock, internal 10K oscillator clock, or internal 22M oscillator clock. Please refer to the
Clock Diagram for HCLK usage in details.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u8ClkSrcSel
[in]
Succeed
<0
Incorrect parameter
Example
DrvSYS_SelectHCLKSource (0);
DrvSYS_SelectHCLKSource (2);
DrvSYS_SelectSysTickSource
Prototype
int32_t
Description
V1.05.002
- 38 -
To select Cortex-M0 SysTick clock source from external 12M crystal clock, external 32K
crystal clock, external 12M crystal clock/2, HCLK/2, or internal 22M oscillator clock/2. The
SysTick timer is a standard timer included by Cortex-M0.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u8ClkSrcSel
[in]
Succeed
<0
Incorrect parameter
Example
DrvSYS_SelectSysTickSource (0);
DrvSYS_SelectSysTickSource (3);
DrvSYS_SelectIPClockSource
Prototype
int32_t
DrvSYS_SelectIPClockSource (E_SYS_IP_CLKSRC eIpClkSrc, uint8_t
u8ClkSrcSel);
Description
To select IP clock source include Watch Dog Timer, ADC, Timer 0~3, UART, PWM01,
PWM23, PWM45, PWM67, I2S and Frequency Divider Output. Please refer to the Clock
Diagram for IP clock source. The settings of IPs corresponding clock source are listed in
Registers CLKSEL1 and CLKSEL2 of TRM in details.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API to select the clock source of Watch Dog Timer. User can check the status of the
Register Write-Protection function with DrvSYS_IsProtectedRegLocked ( ).
V1.05.002
- 39 -
Parameter
eIpClkSrc
[in]
[in]
u8ClkSrcSel
Watch Dog
Timer
Reserved
HCLK/2048
Internal 10K
ADC
External 12M
PLL
Internal 22M
Timer
External 12M
External 32K
HCLK
Reserved
Internal 22M
UART
External 12M
PLL
Reserved
Internal 22M
PWM
External 12M
External 32K
HCLK
Internal 22M
Frequency
Divider Output
External 12M
External 32K
HCLK
Internal 22M
I2S
External 12M
PLL
HCLK
Internal 22M
Ext. 32K
(*)
HCLK
(*)
Note (*)
Only NuMicroTM NUC1x0xxxCx Series (Ex. NUC140VE3CN) support External 32
KHz Crystal as Watch Dog Timer clock source and HCLK as ADC clock source. Please
refer to NuMicroTM NUC100 Series Products Selection Guide of Appendix in details.
Include
Driver/DrvSYS.h
Return Value
0
Succeed
<0
Incorrect parameter
Example
/* Select ADC clock source from 12M */
DrvSYS_SelectIPClockSource (E_SYS_ADC_CLKSRC, 0x00);
/* Select TIMER0 clock source from HCLK */
DrvSYS_SelectIPClockSource (E_SYS_TMR0_CLKSRC, 0x02);
/* Select I2S clock source from HCLK */
DrvSYS_SelectIPClockSource (E_SYS_I2S_CLKSRC, 0x02);
V1.05.002
- 40 -
DrvSYS_SetClockDivider
Prototype
int32_t
Description
To set IP engine clock divide number from IP clock source.
The IP clock frequency is calculated by:
IP clock source frequency / (i32value + 1).
Parameter
eIpDiv
[in]
Succeed
<0
Incorrect parameter
Example
/* Set ADC clock divide number 0x01; ADC clock = ADC source clock / (1+1) */
DrvSYS_SetClockDivider (E_SYS_ADC_DIV, 0x01);
/* Set UART clock divide number 0x02; UART clock = UART source clock / (2+1) */
DrvSYS_SetClockDivider (E_SYS_UART_DIV, 0x02);
/* Set HCLK clock divide number 0x03; HCLK clock = HCLK source clock / (3+1) */
DrvSYS_SetIPClockSource (E_SYS_HCLK_DIV, 0x03);
DrvSYS_SetOscCtrl
Prototype
int32_t
Description
To enable or disable internal oscillator and external crystal include internal 10K and 22M
oscillator, or external 32K and 12M crystal.
V1.05.002
- 41 -
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
eOscCtrl
[in]
Succeed
<0
Incorrect parameter
Example
DrvSYS_SetOscCtrl (E_SYS_XTL12M, 1);
DrvSYS_SetPowerDownWakeUpInt
Prototype
void DrvSYS_SetPowerDownWakeUpInt (int32_t i32Enable, PWRWU_CALLBACK
pdwucallbackFn, int32_t i32enWUDelay);
Description
To enable or disable power down wake up interrupt function, and install its callback function
if power down wake up is enable, and enable clock cycles delay to wait the system clock
stable. The delayed clock cycle is 4096 clock cycles when chip work at external 4~24 MHz
crystal, or 256 clock cycles when chip work at internal 22.1184 MHz oscillator. The power
down wake up interrupt will occur when GPIO, USB, UART, WDT, CAN, ACMP, BOD or
RTC wakeup.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
i32Enable [in]
1: enable, 0: disable
pdwucallbackFn [in]
V1.05.002
- 42 -
Install power down wake up call back function when interrupt function is enabled.
i32enWUDelay [in]
1: enable clock cycles delay, 0: disable clock cycles delay
Include
Driver/DrvSYS.h
Return Value
None
Example
/* Enable Power down Wake up Interrupt function, install callback function
PWRWU_CallbackFn, and enable clock cycles delay */
DrvSYS_SetPowerDownWakeUpInt (1, PWRWU_CallbackFn, 1);
/* Disable Power down Wake up Interrupt function, and uninstall callback function */
DrvSYS_SetPowerDownWakeUpInt (0, NULL, 0);
DrvSYS_EnterPowerDown
Prototype
void
Description
To enter system power down mode immediately or after CPU enters sleep mode. When chip
enters power down mode, the LDO, 12M crystal, and 22M oscillator will be disabled. Please
refer to Application Note, AN_1007_EN_Power_Management, for application.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
ePDType
[in]
DrvSYS_SelectPLLSource
Prototype
void
Description
To select PLL clock source include 22M oscillator and 12M crystal.
Parameter
ePllSrc [in]
E_SYS_EXTERNAL_12M / E_SYS_INTERNAL_22M
Include
Driver/DrvSYS.h
Return Value
None
Example
/* Select PLL clock source from 12M */
DrvSYS_ SelectPLLSource (E_SYS_EXTERNAL_12M);
/* Select PLL clock source from 22M */
DrvSYS_ SelectPLLSource (E_SYS_INTERNAL_22M);
DrvSYS_SetPLLMode
Prototype
void
Description
To set PLL operate in power down mode or normal mode.
Parameter
i32Flag
[in]
Driver/DrvSYS.h
Return Value
None
Example
/* Enable PLL power down mode, PLL operates in power down mode */
DrvSYS_SetPLLMode (1);
/* Disable PLL power down mode, PLL operates in normal mode */
DrvSYS_SetPLLMode (0);
DrvSYS_GetExtClockFreq
Prototype
uint32_t DrvSYS_GetExtClockFreq (void);
Description
To get external crystal clock frequency. The unit is in Hz.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
The external crystal clock frequency
Example
uint32_t u32clock;
u32clock = DrvSYS_GetExtClockFreq ( ); /* Get external crystal clock frequency */
DrvSYS_GetPLLContent
Prototype
uint32_t
Description
To calculate the nearest PLL frequency to fit the target PLL frequency that is defined by
u32PllClk.
Parameter
ePllSrc
[in]
E_SYS_EXTERNAL_12M / E_SYS_INTERNAL_22M
V1.05.002
- 45 -
u32PllClk [in]
The target PLL clock frequency. The unit is in Hz. The range of u32PllClk is
25MHz~200MHz.
Include
Driver/DrvSYS.h
Return Value
The PLL control register setting.
Example
uint32_t u32PllCr;
/* Get PLL control register setting for target PLL clock 50MHz */
u32PllCr = DrvSYS_GetPLLContent (E_SYS_EXTERNAL_12M, 50000000);
DrvSYS_SetPLLContent
Prototype
void
Description
To set PLL settings. User can use DrvSYS_GetPLLContent ( ) to get proper PLL setting and
use DrvSYS_GetPLLClockFreq ( ) to get actual PLL clock frequency.
Parameter
u32PllContent [in]
The PLL register setting for the target PLL clock frequency.
Include
Driver/DrvSYS.h
Return Value
None
Example
uint32_t u32PllCr;
/* Get PLL control register setting for target PLL clock 50MHz */
u32PllCr = DrvSYS_GetPLLContent (E_DRVSYS_EXTERNAL_12M, 50000000);
/* Set PLL control register setting to get nearest PLL clock */
DrvSYS_SetPLLContent (u32PllCr);
DrvSYS_GetPLLClockFreq
Prototype
V1.05.002
- 46 -
uint32_t
DrvSYS_GetPLLClockFreq (void);
Description
To get PLL clock output frequency.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
The PLL clock output frequency in Hz
Example
uint32_t u32clock;
u32clock = DrvSYS_GetPLLClockFreq ( );
DrvSYS_GetHCLKFreq
Prototype
uint32_t
DrvSYS_GetHCLKFreq (void);
Description
To get HCLK clock frequency.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
The HCLK clock frequency in Hz
Example
uint32_t u32clock;
u32clock = DrvSYS_GetHCLKFreq ( );
DrvSYS_Open
Prototype
int32_t DrvSYS_Open (uint32_t u32Hclk);
Description
V1.05.002
- 47 -
To configure the PLL setting according to the PLL source clock and target HCLK clock. Due
to hardware limitation, the actual HCLK clock may be different to target HCLK clock.
The DrvSYS_GetPLLClockFreq ( ) could be used to get actual PLL clock.
The DrvSYS_GetHCLKFreq ( ) could be used to get actual HCLK clock.
The DrvSYS_SetClockDivider ( ) could be used to get lower HCLK clock.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u32Hclk
[in]
The target HCLK clock frequency. The unit is in Hz. The range of u32Hclk is
25MHz~50MHz.
Include
Driver/DrvSYS.h
Return Value
E_SUCCESS
Succeed
E_DRVSYS_ERR_OUT_OF_RANGE
E_DRVSYS_ERR_REG_PROTECTED
Example
/* Set PLL clock 50MHz, and switch HCLK source clock to PLL */
DrvSYS_Open (50000000);
DrvSYS_SetFreqDividerOutput
Prototype
int32_t
Description
NUC100 Series support to monitor clock source frequency by CLKO output pin. This
function is used to enable or disable frequency clock output and set its divider number. The
formula of output frequency is Fout
Fin
, where Fin is the input clock frequency,
2 N 1
V1.05.002
- 48 -
Parameter
i32Flag [in]
1: enable; 0: disable.
u8Divider [in]
The divider number of output frequency. The value is 0~15.
Include
Driver/DrvSYS.h
Return Value
0
Succeed
<0
Incorrect parameter
Example
/* Enable frequency clock output and set its divide number 2,
The output frequency = input clock / 2^(2+1) */
DrvSYS_SetFreqDividerOutput (1, 2);
/* Disable frequency clock output */
DrvSYS_SetFreqDividerOutput (0, 0);
DrvSYS_EnableHighPerformanceMode
Prototype
void
DrvSYS_EnableHighPerformanceMode (void);
Description
To enable chip high performance mode. When this function is enable, internal RAM and
GPIO access is working with zero wait state.
Note 1
Only Low Density series support this function. Please refer to NuMicroTM NUC100 Series
Products Selection Guide of Appendix in details.
Note 2
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
V1.05.002
- 49 -
Return Value
None
Example
/* Enable high performance mode */
DrvSYS_EnableHighPerformanceMode ( );
DrvSYS_DisableHighPerformanceMode
Prototype
void
DrvSYS_DisableHighPerformanceMode (void);
Description
To disable chip high performance mode.
Note 1
Only Low Density series support this function. Please refer to NuMicroTM NUC100 Series
Products Selection Guide of Appendix in details.
Note 2
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvSYS.h
Return Value
None
Example
/* Disable high performance mode */
DrvSYS_DisableHighPerformanceMode ( );
DrvSYS_Delay
Prototype
void
Description
Use the SysTick timer of Cortex-M0 to generate the delay time and the unit is in us. The
SysTick clock source is default to be from HCLK clock. If the SysTick clock source is
changed by user, the delay time may be not correct.
V1.05.002
- 50 -
Parameter
us
[in]
Delay time. The maximal delay time is 335000 us.
Include
Driver/DrvSYS.h
Return Value
None
Example
DrvSYS_Delay (5000);
/* Delay 5000us */
DrvSYS_GetChipClockSourceStatus
Prototype
int32_t
Description
To monitor if the chip clock source stable or not, include internal 10K, 22M oscillator,
external 32K, 12M crystal, or PLL clock.
Note
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) and Low Density series
support this function. Please refer to NuMicroTM NUC100 Series Products Selection Guide of
Appendix in details.
Parameter
eClkSrc
[in]
<0
Incorrect parameter
Example
/* Enable external 12M */
DrvSYS_SetOscCtrl (E_SYS_XTL12M, 1);
/* Waiting for 12M Crystal stable */
V1.05.002
- 51 -
DrvSYS_GetClockSwitchStatus
Prototype
uint32_t
DrvSYS_GetClockSwitchStatus (void);
Description
To get if switch target clock is successful or failed when software switches system clock
source.
Note
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) and Low Density series
support this function. Please refer to NuMicroTM NUC100 Series Products Selection Guide of
Appendix in details.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
0: Clock switch success
1: Clock switch fail
Example
uint32_t u32flag;
DrvSYS_SelectHCLKSource (2);
u32flag = DrvSYS_GetClockSwitchStatus ( );
If (u32flag)
/* do something for clock switch fail */
DrvSYS_ClearClockSwitchStatus
Prototype
void
DrvSYS_ClearClockSwitchStatus (void);
Description
To clear the Clock Switch Fail Flag.
V1.05.002
- 52 -
Note
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) and Low Density series
support this function. Please refer to NuMicroTM NUC100 Series Products Selection Guide of
Appendix in details.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
None
Example
uint32_t u32flag;
DrvSYS_SelectHCLKSource (0);
u32flag = DrvSYS_GetClockSwitchStatus ( );
if (u32flag)
DrvSYS_ClearClockSwitchStatus ( );
DrvSYS_GetVersion
Prototype
uint32_t DrvSYS_GetVersion (void);
Description
Get this version of DrvSYS driver.
Parameter
None
Include
Driver/DrvSYS.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
V1.05.002
- 53 -
3. UART Driver
3.1. UART Introduction
The Universal Asynchronous Receiver/Transmitter (UART) performs a serial-to-parallel conversion on
data characters received from the peripheral such as MODEM, and a parallel-to-serial conversion on data
characters received from the CPU.
Details please refer to the section in the target chip specification titled UART.
V1.05.002
- 54 -
Value
Description
MODE_TX
MODE_RX
1
2
Value
Description
UART_PORT0
UART_PORT1
UART_PORT2
0x000
UART port 0
0x100000 UART port 1
0x104000 UART port 2
E_INT_SOURCE
Enumeration identifier
Value
Description
DRVUART_RDAINT
0x1
DRVUART_THREINT
DRVUART_WAKEUPINT
DRVUART_RLSINT
DRVUART_MOSINT
DRVUART_TOUTINT
DRVUART_BUFERRINT
DRVUART_LININT
0x2
0x40
0x4
0x8
0x10
0x20
0x100
E_INT_FLAG
Enumeration identifier
Value
Description
DRVUART_RDAINT_FLAG
0x100
DRVUART_THREINT_FLAG
DRVUART_RLSINT_FLAG
DRVUART_MOSINT_FLAG
DRVUART_TOUTINT_FLAG
DRVUART_BUFERRINT_FLAG
DRVUART_LININT_FLAG
0x200
0x400
0x800
0x1000
0x2000
0x8000
V1.05.002
- 55 -
E_DATABITS_SETTINGS
Enumeration identifier
Value
Description
DRVUART_DATABITS_5
DRVUART_DATABITS_6
DRVUART_DATABITS_7
DRVUART_DATABITS_8
0x0
0x1
0x2
0x3
Enumeration identifier
Value
Description
DRVUART_PARITY_NONE
DRVUART_PARITY_ODD
DRVUART_PARITY_EVEN
DRVUART_PARITY_MARK
DRVUART_PARITY_SPACE
0x0
0x1
0x3
0x5
0x7
None parity
Odd parity enable
Even parity enable
Parity mask
Parity space
Enumeration identifier
Value
Description
DRVUART_STOPBITS_1
0x0
DRVUART_STOPBITS_1_5
0x1
DRVUART_STOPBITS_2
0x1
E_PARITY_SETTINGS
E_STOPBITS_SETTINGS
E_FIFO_SETTINGS
Enumeration identifier
Value
Description
DRVUART_FIFO_1BYTES
DRVUART_FIFO_4BYTES
DRVUART_FIFO_8BYTES
DRVUART_FIFO_14BYTES
DRVUART_FIFO_30BYTES
DRVUART_FIFO_46BYTES
DRVUART_FIFO_62BYTES
0x0
0x1
0x2
0x3
0x4
0x5
0x6
Enumeration identifier
Value
Description
FUN_UART
FUN_LIN
FUN_IRDA
FUN_RS485
0
1
2
3
E_UART_FUNC
V1.05.002
- 56 -
E_MODE_RS485
Enumeration identifier
Value
Description
MODE_RS485_NMM
MODE_RS485_AAD
MODE_RS485_AUD
1
2
4
3.5. Macros
_DRVUART_SENDBYTE
Prototype
void _DRVUART_SENDBYTE (u32Port, byData);
Description
Send 1 byte data from UART.
Include
Driver/DrvUART.h
Return Value
None.
Example
/* Using UART port0 to send one byte 0x55 */
_DRVUART_SENDBYTE (UART_PORT0, 0x55);
_DRVUART_RECEIVEBYTE
Prototype
uint8_t
_DRVUART_RECEIVEBYTE (u32Port);
Description
Receive 1 byte data from specified UART FIFO.
Include
Driver/DrvUART.h
Return Value
One byte data.
Example
V1.05.002
- 57 -
_DRVUART_SET_DIVIDER
Prototype
void
Description
To set the UART divider to control UART baud-rate
Include
Driver/DrvUART.h
Return Value
None.
Example
/* Set the divider of UART is 6 */
_DRVUART_SET_DIVIDER (UART_PORT0, 6);
_DRVUART_RECEIVEAVAILABLE
Prototype
int8_t
_DRVUART_RECEIVEAVAILABLE (u32Port);
Description
To get current Rx FIFO pointer
Include
Driver/DrvUART.h
Return Value
Rx FIFO pointer value.
Example
/* To get UART channel 0 current Rx FIFO pointer */
_DRVUART_RECEIVEAVAILABLE (UART_PORT0);
_DRVUART_WAIT_TX_EMPTY
Prototype
V1.05.002
- 58 -
void
_DRVUART_WAIT_TX_EMPTY (u32Port);
Description
Polling Tx empty flag to check Tx FIFO is empty.
Include
Driver/DrvUART.h
Return Value
None.
Example
/* Send 0x55 from UART0 and check TX FIFO is empty */
_DRVUART_SENDBYTE (UART_PORT0, 0x55);
_DRVUART_WAIT_TX_EMPTY (UART_PORT0);
3.6. Functions
DrvUART_Open
Prototype
int32_t
DrvUART_Open (
E_UART_PORT u32Port,
UART_T *sParam
);
Description
The function is used to initialize UART. It consists of baud-rate, parity, data-bits, stop-bits,
rx-trigger-level and timeout interval settings.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
sParam [in]
Specify the property of UART. It includes
u32BaudRate: Baud rate (Hz)
u8cParity: NONE/EVEN/ODD parity
It could be
V1.05.002
- 59 -
Include
Driver/DrvUART.h
Return Value
E_SUCCESS: Success.
E_DRVUART_ERR_PORT_INVALID: Wrong UART port configure
E_DRVUART_ERR_PARITY_INVALID: Wrong party setting
E_DRVUART_ERR_DATA_BITS_INVALID: Wrong Data bit setting
E_DRVUART_ERR_STOP_BITS_INVALID: Wrong Stop bit setting
E_DRVUART_ERR_TRIGGERLEVEL_INVALID: Wrong trigger level setting
V1.05.002
- 60 -
Example
/* Set UART0 under 115200bps, 8 data bits ,1 stop bit and none parity and 1 byte Rx trigger
level settings. */
STR_UART_T sParam;
sParam.u32BaudRate
= 115200;
sParam.u8cDataBits
= DRVUART_DATABITS_8;
sParam.u8cStopBits
= DRVUART_STOPBITS_1;
sParam.u8cParity
= DRVUART_PARITY_NONE;
sParam.u8cRxTriggerLevel
= DRVUART_FIFO_1BYTES;
DrvUART_Close
Prototype
void DrvUART_Close (
E_UART_PORT u32Port
);
Description
The function is used to disable UART clock, disable ISR and clear callback function pointer
after checking the TX empty.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
Include
Driver/DrvUART.h
Return Value
None
Example
/* Close UART channel 0 */
DrvUART_Close (UART_PORT0);
DrvUART_EnableInt
Prototype
V1.05.002
- 61 -
void
DrvUART_EnableInt (
E_UART_PORT u32Port,
uint32_t
u32InterruptFlag,
PFN_DRVUART_CALLBACK pfncallback
);
Description
The function is used to enable specified UART interrupt, install the callback function and
enable NVIC UART IRQ.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
u32InterruptFlag [in]
DRVUART_LININT : LIN RX Break Field Detected Interrupt Enable
DRVUART_BUFERRINT : Buffer Error Interrupt Enable
DRVUART_WAKEINT : Wakeup Enable.
DRVUART_MOSINT : MODEM Status Interrupt.
DRVUART_RLSNT : Receive Line Status Interrupt.
DRVUART_THREINT : Transmit Holding Register Empty Interrupt.
DRVUART_RDAINT : Receive Data Available Interrupt and Time-out Interrupt
DRVUART_TOUTINT : Time-out Interrupt.
pfncallback [in]
Call back function pointer
Include
Driver/DrvUART.h
Return Value
None
Note
Use | to connect the interrupt flags to enable multiple interrupts simultaneously.
If you call the function twice in a project, the settings is depend on the second setting.
Example
/* Enable UART channel 0 RDA and THRE interrupt. Finally, install UART_INT_HANDLE
function to be callback function. */
DrvUART_EnableInt(UART_PORT0, (DRVUART_RDAINT |
DRVUART_THREINT ),UART_INT_HANDLE);
V1.05.002
- 62 -
DrvUART_DisableInt
Prototype
void
DrvUART_DisableInt (
E_UART_PORT u32Port
uint32_t
u32InterruptFlag
);
Description
The function is used to disable UART specified interrupt, uninstall the call back function and
disable NVIC UART IRQ.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
u32InterruptFlag [in]
DRVUART_LININT : LIN RX Break Field Detected Interrupt Enable
DRVUART_BUFERRINT : Buffer Error Interrupt Enable
DRVUART_WAKEINT : Wakeup Enable.
DRVUART_MOSINT : MODEM Status Interrupt.
DRVUART_RLSNT : Receive Line Status Interrupt.
DRVUART_THREINT : Transmit Holding Register Empty Interrupt.
DRVUART_RDAINT : Receive Data Available Interrupt and Time-out Interrupt
DRVUART_TOUTINT : Time-out Interrupt.
Include
Driver/DrvUART.h
Return Value
None
Note
Use | to connect the interrupt flags to disable multiple interrupts simultaneously.
Example
/* To disable the THRE interrupt enable flag. */
DrvUART_DisableInt (UART_PORT0, DRVUART_THREINT);
DrvUART_ClearIntFlag
Prototype
uint32_t
V1.05.002
- 63 -
DrvUART_ClearIntFlag (
E_UART_PORT u32Port
uint32_t
u32InterruptFlag
);
Description
The function is used to clear UART specified interrupt flag.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
u32InterruptFlag [in]
DRVUART_LININT_FLAG: LIN RX Break Field Detected Interrupt Flag
DRVUART_BUFERRINT_FLAG : Buffer Error Interrupt Flag
DRVUART_MOSINT_FLAG : MODEM Status Interrupt Flag
DRVUART_RLSINT_FLAG: : Receive Line Status Interrupt Flag.
Include
Driver/DrvUART.h
Return Value
E_SUCESS
Success
Example
/* To clear UART0 LIN break interrupt flag */
DrvUART_ClearIntFlag (UART_PORT0, DRVUART_LININT_FLAG);
DrvUART_GetIntStatus
Prototype
int32_t
DrvUART_GetIntStatus (
E_UART_PORT u32Port
uint32_t
u32InterruptFlag
);
Description
The function is used to get the specified UART interrupt status.
V1.05.002
- 64 -
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
u32InterruptFlag [in]
DRVUART_LININT_FLAG : LIN RX Break Field Detected Interrupt Flag
DRVUART_BUFERRINT_FLAG: Buffer Error Interrupt Flag
DRVUART_MOSINT_FLAG : MODEM Status Interrupt Flag
DRVUART_RLSINT_FLAG : Receive Line Status Interrupt Flag
DRVUART_THREINT_FLAG: Transmit Holding Register Empty Interrupt Flag.
DRVUART_RDAINT_FLAG: Receive Data Available Interrupt Flag.
DRVUART_TOUTINT_FLAG : Time-out Interrupt Flag
Include
Driver/DrvUART.h
Return Value
0: The specified interrupt did not happen.
1: The specified interrupt happened.
E_DRVUART_ARGUMENT: Error Parameter.
Note
It is recommended to poll one interrupt at a time.
Example
/* To get the Time-out interrupt enable flag. */
If(DrvUART_GetIntStatus (UART_PORT0, DRVUART_TOUTINT_FLAG))
printf(Time Out Interrupt is happened!\n);
else
printf(Time Out Interrupt is not happened or error parameter\n);
DrvUART_GetCTSInfo
Prototype
void
DrvUART_GetCTSInfo(
E_UART_PORT
u32Port,
uint8_t
*pu8CTSValue,
uint8_t
*pu8CTSChangeState
V1.05.002
- 65 -
Description
The function is used to get CTS pin value and detect CTS change state
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1 (UART_PORT2 is no supported.)
pu8CTSValue [out]
Specify the buffer to receive the CTS value.Retrun current CTS pin state.
pu8CTSChangeState [out]
Specify the buffer to receive the CTS change state. Return CTS pin state is changed or
not. 1 means changed and 0 means not yet.
Include
Driver/DrvUART.h
Return Value
None
Example
/* To get CTS pin status and save to u8CTS_value. To get detect CTS change flag and save
to u8CTS_state. */
uint8_t u8CTS_value, u8CTS_state;
DrvUART_GetCTSInfo(UART_PORT1,& u8CTS_value,& u8CTS_state);
DrvUART_SetRTS
Prototype
void
DrvUART_SetRTS (
E_UART_PORT u32Port,
uint8_t
u8Value,
uint16_t
u16TriggerLevel
}
Description
The function is used to set RTS setting.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1 (UART_PORT2 is no supported.)
V1.05.002
- 66 -
u8Value [in]
Set 0: Drive RTS pin to logic 1 (If the LEV_RTS set to low level triggered).
Drive RTS pin to logic 0 (If the LEV_RTS set to high level triggered).
Set 1: Drive RTS pin to logic 0 (If the LEV_RTS set to low level triggered).
Drive RTS pin to logic 1 (If the LEV_RTS set to high level triggered).
Note. LEV_RTS is RTS Trigger Level. 0 is low level and 1 is high level.
u16TriggerLevel [in]
RTS Trigger Level :DRVUART_FIFO_1BYTES to DRVUART_FIFO_62BYTES
Include
Driver/DrvUART.h
Return Value
None
Example
/* Condition: Drive RTS to logic 1 in UART channel 1 and Set RTS trigger level is 1 bytes*/
DrvUART_SetRTS (UART_PORT1,1, DRVUART_FIFO_1BYTES);
DrvUART_Read
Prototype
int32_t
DrvUART_Read (
E_UART_PORT
u32Port
uint8_t
*pu8RxBuf,
uint32_t
u32ReadBytes
);
Description
The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
pu8RxBuf [out]
Specify the buffer to receive the data of receive FIFO.
u32ReadBytes [in]
Specify the read bytes number of data.
Include
Driver/DrvUART.h
V1.05.002
- 67 -
Return Value
E_SUCCESS: Success.
E_DRVUART_TIMEOUT: FIFO polling timeout.
Example
/* Condition: Read RX FIFO 1 byte and store in bInChar buffer.
*/
uint8_t bInChar[1];
DrvUART_Read(UART_PORT0,bInChar,1);
DrvUART_Write
Prototype
int32_t
DrvUART_Write(
E_UART_PORT u32Port
uint8_t
*pu8TxBuf,
uint32_t
u32WriteBytes
);
Description
The function is to write data into TX buffer to transmit data by UART
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
pu8TxBuf [in]
Specify the buffer to send the data to UART transmission FIFO.
u32WriteBytes [in]
Specify the byte number of data.
Include
Driver/DrvUART.h
Return Value
E_SUCCESS: Success
E_DRVUART_TIMEOUT: FIFO polling timeout
Example
/* Condition: Send 1 byte from bInChar buffer to TX FIFO. */
uint8_t bInChar[1] = 0x55;
DrvUART_Write(UART_PORT0,bInChar,1);
V1.05.002
- 68 -
DrvUART_EnablePDMA
Prototype
void
DrvUART_EnablePDMA (
E_UART_PORT u32Port
);
Description
The function is used to control enable PDMA transmit/receive channel
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1 (UART_PORT2 is no supported.)
Include
Driver/DrvUART.h
Return Value
None.
Example
/* Enable TX and RX PDMA in UART 1 */
DrvUART_EnablePDMA(UART_PORT1);
DrvUART_DisablePDMA
Prototype
void
DrvUART_DisablePDMA (
E_UART_PORT u32Port
);
Description
The function is used to control disable PDMA transmit/receive channel
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1 (UART_PORT2 is no supported.)
Include
V1.05.002
- 69 -
Driver/DrvUART.h
Return Value
None.
Example
/* Disable Tx and Rx PDMA in UART 1 */
DrvUART_DisablePDMA(UART_PORT1);
DrvUART_SetFnIRDA
Prototype
void
DrvUART_SetFnIRDA (
E_UART_PORT u32Port
STR_IRCR_T
str_IRCR
);
Description
The function is used to configure IRDA relative settings. It consists of TX or RX mode and
Inverse TX or RX signals.
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
str_IRCR [in]
The structure of IrDA
It includes of
u8cTXSelect : 1 : Enable IrDA transmit function. It becomes TX mode
0 : Disable IrDA transmit function. It becomes RX mode.
u8cInvTX : Invert Tx signal function TRUE or FASLE
u8cInvRX : Invert Rx signal function (Default value is TRUE) TRUE or FASLE
Include
Driver/DrvUART.h
Return Value
None
Note
V1.05.002
- 70 -
Before using the API, you should configure UART setting firstly. And make sure the
baud-rate setting is used mode 0 (UART divider is 16)in baud-rate configure.
Example
/* Change UART1 to IRDA function and Inverse the RX signals. */
STR_IRCR_T sIrda;
sIrda.u8cTXSelect
= ENABLE;
sIrda.u8cInvTX
= FALSE;
sIrda.u8cInvRX
= TRUE;
DrvUART_SetFnIRDA(UART_PORT1,&sIrda);
DrvUART_SetFnRS485
Prototype
void
DrvUART_OpenRS485 (
E_UART_PORT u32Port,
STR_RS485_T *str_RS485
);
Description
The function is used to set RS485 relative setting
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
str_RS485 [in]
The structure of RS485
It includes of
u8cModeSelect: Select operation mode
MODE_RS485_NMM: RS-485 Normal Multi-drop Mode
MODE_RS485_AAD: RS-485 Auto Address Detection Mode
MODE_RS485_AUD: RS-485 Auto Direction Mode
u8cAddrEnable: Enable or Disable RS-485 Address Detection
u8cAddrValue: Set Address match value
u8cDelayTime: Set transmit delay time value
u8cRxDisable: Enable or Disable receiver function.
V1.05.002
- 71 -
Include
Driver/DrvUART.h
Return Value
None
Note
None
Example
/* Condition: Change UART1 to RS485 function. Set relative setting as below.*/
STR_RS485_T sParam_RS485;
sParam_RS485.u8cAddrEnable
= ENABLE;
sParam_RS485.u8cAddrValue
= 0xC0;
sParam_RS485.u8cModeSelect
= MODE_RS485_AAD|MODE_RS485_AUD;
sParam_RS485.u8cDelayTime
= 0;
sParam_RS485.u8cRxDisable
= TRUE;
/* Address */
DrvUART_SetFnRS485(UART_PORT1,&sParam_RS485);
DrvUART_SetFnLIN
Prototype
void
DrvUART_SetFnLIN (
E_UART_PORT u32Port
uint16_t u16Mode,
uint16_t u16BreakLength
);
Description
The function is used to set LIN relative setting
Parameter
u32Port [in]
Specify UART_PORT0/UART_PORT1/UART_PORT2
u16Mode [in]
Specify LIN direction : MODE_TX and/or MODE_RX
u16BreakLength [in]
Specify break count value. It should be larger than 13 bit time according LIN protocol.
Include
V1.05.002
- 72 -
Driver/DrvUART.h
Return Value
None
Example
/* Change UART1 to LIN function and set to transmit the header information. */
DrvUART_SetFnLIN(uart_ch,MODE_TX | MODE_RX,13);
DrvUART_GetVersion
Prototype
int32_t
DrvUART_GetVersion (void);
Description
Return the current version number of driver.
Include
Driver/DrvUART.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
V1.05.002
- 73 -
4. TIMER/WDT Driver
4.1. TIMER/WDT Introduction
The timer module includes four channels, TIMER0~TIMER3, which allow you to easily implement
a counting scheme for use. The timer can perform functions like frequency measurement, event
counting, interval measurement, clock generation, delay timing, and so on. The timer can generate an
interrupt signal upon timeout, or provide the current value of count during operation. And for external
count and capture functions, only NUC1x0xxxBx and NUC1x0xxxCx series supported,
ex:NUC140RD2BN and NUC140VE3CN. Please refer to NuMicroTM NUC100 Series Products
Selection Guide of Appendix in details.
The purpose of Watchdog Timer (WDT) is to perform a system reset after the software running into a
problem. This prevents system from hanging for an infinite period of time.
4 sets of 32-bit timers with 24-bit up-timer and one 8-bit pre-scale counter.
Independent clock source for each timer.
Provides one-shot, periodic, toggle and continuous counting operation modes.
Time out period =
(Period of timer clock input) * (8-bit pre-scale counter + 1) * (24-bit TCMP).
Maximum counting cycle time = (1 / T MHz) * (2^8) * (2^24), T is the period of timer clock.
24-bit timer value is readable through TDR (Timer Data Register).
Support event counting function to count the event from external pin.
Support input capture function to capture or reset counter value.
18-bit free running counter to avoid CPU from Watchdog timer reset before the delay
time expires.
Selectable time-out interval (2^4 ~ 2^18) and the time out interval is 104 ms ~ 26.3168 s
(if WDT_CLK = 10 kHz).
Reset period = (1/10 kHz) * 63, if WDT_CLK = 10 kHz.
Value
0x0
Description
Specify the timer channel - 0
V1.05.002
- 74 -
E_TMR1
0x1
E_TMR2
0x2
E_TMR3
0x3
E_TIMER_OPMODE
Enumeration Identifier
Value
Description
E_ONESHOT_MODE
0x0
E_PERIODIC_MODE
0x1
E_TOGGLE_MODE
0x2
E_CONTINUOUS_MODE
0x3
E_TIMER_TX_PHASE
Enumeration Identifier
Value
Description
E_PHASE_FALLING
0x0
E_PHASE_RISING
0x1
E_TIMER_TEX_EDGE
Enumeration Identifier
Value
Description
E_EDGE_FALLING
0x0
E_EDGE_RISING
0x1
E_EDGE_BOTH
0x2
Value
Description
E_TIMER_RSTCAP_MODE
Enumeration Identifier
E_CAPTURE
0x0
E_RESET
0x1
E_WDT_CMD
Enumeration Identifier
Value
Description
E_WDT_IOC_START_TIMER
0x0
E_WDT_IOC_STOP_TIMER
0x1
V1.05.002
- 75 -
E_WDT_IOC_ENABLE_INT
0x2
E_WDT_IOC_DISABLE_INT
0x3
E_WDT_IOC_ENABLE_WAKEUP
0x4
E_WDT_IOC_DISABLE_WAKEUP
0x5
E_WDT_IOC_RESET_TIMER
0x6
E_WDT_IOC_ENABLE_RESET_FUNC
0x7
E_WDT_IOC_DISABLE_RESET_FUNC
0x8
E_WDT_IOC_SET_INTERVAL
0x9
E_WDT_INTERVAL
Enumeration Identifier
Value
Description
E_LEVEL0
0x0
E_LEVEL1
0x1
E_LEVEL2
0x2
E_LEVEL3
0x3
E_LEVEL4
0x4
E_LEVEL5
0x5
E_LEVEL6
0x6
E_LEVEL7
0x7
4.4. Functions
DrvTIMER_Init
Prototype
void DrvTIMER_Init (void)
Description
User must to call this function before any timer operations after system boot up.
Parameter
None
V1.05.002
- 76 -
Include
Driver/DrvTIMER.h
Return Value
None
Example:
/* Info the system can accept Timer APIs after calling DrvTIMER_Init() */
DrvTIMER_Init ();
DrvTIMER_Open
Prototype
int32_t DrvTIMER_Open (
E_TIMER_CHANNEL ch,
uint32_t
uTicksPerSecond,
E_TIMER_OPMODE op_mode
)
Description
Open the specified timer channel with specified operation mode.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
uTicksPerSecond [in]
This value means how many timer interrupt ticks in one second
op_moode [in]
E_TIMER_OPMODE, E_ONESHOT_MODE / E_PERIODIC_MODE /
E_TOGGLE_MODE / E_CONTINUOUS_MODE
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
E_DRVTIMER_CLOCK_RATE: Calculate initial value fail
Example
/* Using TIMER0 at PERIODIC_MODE, 2 ticks / sec */
DrvTIMER_Open (E_TMR0, 2, E_PERIODIC_MODE);
V1.05.002
- 77 -
DrvTIMER_Close
Prototype
int32_t DrvTIMER_Close (E_TIMER_CHANNEL ch)
Description
The function is used to close the timer channel.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Close the specified timer channel */
DrvTIMER_Close (E_TMR0);
DrvTIMER_SetTimerEvent
Prototype
int32_t DrvTIMER_SetTimerEvent (
E_TIMER_CHANNEL ch,
uint32_t
uInterruptTicks,
TIMER_CALLBACK pTimerCallback ,
uint32_t
parameter
)
Description
Install the interrupt callback function of the specified timer channel. And trigger timer
callback function when interrupt occur uInterruptTicks times.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
uInterruptTicks [in]
Number of timer interrupt occurred
pTimerCallback [in]
The function pointer of the interrupt callback function
V1.05.002
- 78 -
parameter [in]
A parameter of the callback function
Include
Driver/DrvTIMER.h
Return Value
uTimerEventNo:
E_DRVTIMER_EVENT_FULL:
Example
/* Install callback "TMR_Callback" and trigger callback
when timer interrupt happen twice */
uTimerEventNo = DrvTIMER_SetTimerEvent (E_TMR0, 2,
(TIMER_CALLBACK)TMR_Callback, 0);
DrvTIMER_ClearTimerEvent
Prototype
void DrvTIMER_ClearTimerEvent (
E_TIMER_CHANNEL ch,
uint32_t
uTimerEventNo
)
Description
Clear the timer event of the specified timer channel.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
uTimerEventNo [in]
The timer event number
Include
Driver/DrvTIMER.h
Return Value
None
Example
/* Close the specified timer event */
DrvTIMER_ClearTimerEvent (E_TMR0, uTimerEventNo);
V1.05.002
- 79 -
DrvTIMER_EnableInt
Prototype
int32_t DrvTIMER_EnableInt (E_TIMER_CHANNEL ch)
Description
This function is used to enable the specified timer interrupt.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Enable Timer-0 interrupt function */
DrvTIMER_EnableInt (E_TMR0);
DrvTIMER_DisableInt
Prototype
int32_t DrvTIMER_DisableInt (E_TIMER_CHANNEL ch)
Description
This function is used to disable the specified timer interrupt.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Disable Timer-0 interrupt function */
DrvTIMER_DisaleInt (E_TMR0);
V1.05.002
- 80 -
DrvTIMER_GetIntFlag
Prototype
int32_t DrvTIMER_GetIntFlag (E_TIMER_CHANNEL ch)
Description
Get the interrupt flag status from the specified timer channel.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
iIntStatus: 0 is No interrupt, 1is Interrupt occurred
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Get the interrupt flag status from Timer-0 */
u32TMR0IntFlag = DrvTIMER_GetIntFlag (E_TMR0);
DrvTIMER_ClearIntFlag
Prototype
int32_t DrvTIMER_ClearIntFlag (E_TIMER_CHANNEL ch)
Description
Clear the interrupt flag of the specified timer channel.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Clear Timer-0 interrupt flag */
DrvTIMER_ClearIntFlag (E_TMR0);
V1.05.002
- 81 -
DrvTIMER_Start
Prototype
int32_t DrvTIMER_Start (E_TIMER_CHANNEL ch)
Description
Start to count the specified timer channel.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Start to count the Timer-0 */
DrvTIMER_Start (E_TMR0);
DrvTIMER_GetIntTicks
Prototype
uin32_t DrvTIMER_GetIntTicks (E_TIMER_CHANNEL ch)
Description
This function is used to get the number of interrupt occurred after the timer interrupt
function is enabled.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
uTimerTick: Return the interrupt ticks
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
V1.05.002
- 82 -
DrvTIMER_ResetIntTicks
Prototype
int32_t DrvTIMER_ResetIntTicks (E_TIMER_CHANNEL ch)
Description
This function is used to clear interrupt ticks to 0.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Reset the interrupt ticks of Timer-1 to 0 */
DrvTIMER_ResetIntTicks (E_TMR1);
DrvTIMER_Delay
Prototype
void DrvTIMER_Delay (E_TIMER_CHANNEL ch, uint32_t uIntTicks)
Description
This function is used to add a delay loop by specified interrupt ticks of the timer channel.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
uIntTicks [in]
The delay ticks
Include
Driver/DrvTIMER.h
Return Value
V1.05.002
- 83 -
None
Example
/* Delay Timer-0 3000 ticks */
DrvTIMER_Delay (E_TMR0, 3000);
DrvTIMER_OpenCounter
Prototype
int32_t DrvTIMER_OpenCounter (
E_TIMER_CHANNEL ch,
uint32_t uCounterBoundary,
E_TIMER_OPMODE op_mode
);
Description
This function is used to open the timer channel with the specified operation mode.
And the counting source of timer is from the external event/counter.
The TIMER clock source should be set as HCLK.
Note
Only NUC1x0xxxBx and NUC1x0xxxCx series support this function, ex:NUC140RD2BN
and NUC140VE3CN. Please refer to NuMicroTM NUC100 Series Products Selection Guide
of Appendix in details.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
uCounterBoundary [in]
The parameter is used to determine how many counts occurred will toggle once timer
interrupt
op_mode [in]
E_TIMER_OPMODE, its included E_ONESHOT_MODE / E_PERIODIC_MODE /
E_CONTINUOUS_MODE
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
E_DRVTIMER_EIO: Timer has not been initialized
Example
V1.05.002
- 84 -
DrvTIMER_StartCounter
Prototype
in32_t DrvTIMER_StartCounter (E_TIMER_CHANNEL ch)
Description
Start counting of the specified timer channel.
Note
Only NUC1x0xxxBx and NUC1x0xxxCx series support this function, ex:NUC140RD2BN
and NUC140VE3CN. Please refer to NuMicroTM NUC100 Series Products Selection Guide
of Appendix in details.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Start to count the Timer-0 by external counter */
DrvTIMER_StartCounter (E_TMR0);
DrvTIMER_GetCounters
Prototype
uin32_t DrvTIMER_GetCounters (E_TIMER_CHANNEL ch)
Description
This function is used to get the current counters of the specified timer channel.
Only NUC1x0xxxBx and NUC1x0xxxCx series support this function, ex:NUC140RD2BN
and NUC140VE3CN. Please refer to NuMicroTM NUC100 Series Products Selection Guide
of Appendix in details.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
V1.05.002
- 85 -
Include
Driver/DrvTIMER.h
Return Value
u32Counters: Return current counters
E_DRVTIMER_CHANNEL: Invalid timer channel
Example:
/* Get the current counts of Timer-0 */
u32TMR0ExtTicks = DrvTIMER_GetCounters (E_TMR0);
DrvTIMER_OpenCapture
Prototype
int32_t DrvTIMER_OpenCapture (
E_TIMER_CHANNEL ch,
E_TIMER_RSTCAP_MODE mode
);
Description
This function is used to initial the external timer capture source and set to start catpure or
reset specified timer counter.
The TIMER clock source should be set as HCLK.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
mode [in]
E_TIMER_RSTCAP_MODE,
E_CAPTURE : Run capture function
E_RESET : Reset counter value of specified timer channel
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Open external Timer-0 capture function */
DrvTIMER_OpenCapture (E_TMR0, E_CAPTURE);
V1.05.002
- 86 -
DrvTIMER_CloseCapture
Prototype
int32_t DrvTIMER_CloseCapture (
E_TIMER_CHANNEL ch,
);
Description
This function is used to close the external timer capture source.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Close external Timer-0 capture function */
DrvTIMER_CloseCapture (E_TMR0);
DrvTIMER_SelectExternalMode
Prototype
int32_t DrvTIMER_SelectExternalMode (
E_TIMER_CHANNEL ch,
E_TIMER_RSTCAP_MODE mode
);
Description
This function is used to select to run capture function or reset the timer counter.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
mode [in]
E_TIMER_RSTCAP_MODE,
V1.05.002
- 87 -
DrvTIMER_SelectCaptureEdge
Prototype
int32_t DrvTIMER_OpenCapture (
E_TIMER_CHANNEL ch,
E_TIMER_TEX_EDGE edge
);
Description
This function is used to configure the detect edge of timer capture mode.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
edge [in]
E_TIMER_TEX_EDGE,
E_EDGE_FALLING : 1 to 0 transition on TEX will be detected.
E_EDGE_RISING : 0 to 1 transition on TEX will be detected.
E_EDGE_BOTH : either 0 to 1 or 1 to 0 transition on TEX will be detected.
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
V1.05.002
- 88 -
/* Configure timer-0 capture detect occurrd when 0 to 1 transition on external capture pin */
DrvTIMER_SelectCaptureEdge (E_TMR0, E_EDGE_RISING);
DrvTIMER_EnableCaptureInt
Prototype
int32_t DrvTIMER_EnableCaptureInt (
E_TIMER_CHANNEL ch,
);
Description
This function is used to enable the timer external interrupt function.
If any transition on TEX pin and matched with the E_TIMER_TEX_EDGE settings, system
will cause the external interrupt flag(TEXIF) to 1.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Enable external timer-0 capture detect interrupt function */
DrvTIMER_EnableCaptureInt (E_TMR0);
DrvTIMER_DisableCaptureInt
Prototype
int32_t DrvTIMER_DisableCaptureInt (
E_TIMER_CHANNEL ch,
);
Description
This function is used to disable the timer external interrupt function.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
V1.05.002
- 89 -
DrvTIMER_EnableCapture
Prototype
int32_t DrvTIMER_EnableCapture (
E_TIMER_CHANNEL ch,
);
Description
This function is used to enable the specified capture function.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Enable external timer-0 capture function */
DrvTIMER_EnableCapture (E_TMR0);
DrvTIMER_DisableCapture
Prototype
int32_t DrvTIMER_DisableCapture (
E_TIMER_CHANNEL ch,
V1.05.002
- 90 -
);
Description
This function is used to disable the specified capture function.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Disable external timer-0 capture function */
DrvTIMER_DisableCapture (E_TMR0);
DrvTIMER_GetCaptureData
Prototype
int32_t DrvTIMER_GetCaptureData (
E_TIMER_CHANNEL ch,
);
Description
This function is used to get the capture value of the specified timer channel.
And the return data is valid only if the capture interrupt flag set to 1 by H.W.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
Capture value: Return capture value
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
V1.05.002
- 91 -
DrvTIMER_GetCaptureIntFlag
Prototype
int32_t DrvTIMER_GetCaptureIntFlag (
E_TIMER_CHANNEL ch,
);
Description
Get the external interrupt flag status from the specified timer channel.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
External interrupt flag: 0:No interrupt / 1:Interrupt occurred
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Get the external timer-0 capture interrupt status */
uint32_t u32IntStatus, u32CurData;
u32IntStatus = DrvTIMER_GetCaptureIntFlag (E_TMR0);
if (u32IntStatus == 1)
{
/* Get the current capture data from timer-0 */
u32CurData = DrvTIMER_GetCaptureData (E_TMR0);
}
DrvTIMER_ClearCaptureIntFlag
Prototype
V1.05.002
- 92 -
int32_t DrvTIMER_GetCaptureIntFlag (
E_TIMER_CHANNEL ch,
);
Description
Clear the external interrupt flag of the specified timer channel.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Get the external timer-0 capture interrupt status */
uint32_t u32IntStatus, u32CurData;
u32IntStatus = DrvTIMER_GetCaptureIntFlag (E_TMR0);
if (u32IntStatus == 1)
{
/* Get the current capture data from timer-0 */
u32CurData = DrvTIMER_GetCaptureData (E_TMR0);
/* Clear capture interrupt status to receive the next valid capture value */
DrvTIMER_ClearCaptureIntFlag (E_TMR0);
}
DrvTIMER_EnableCaptureDebounce
Prototype
int32_t DrvTIMER_EnableCaptureDebounce (
E_TIMER_CHANNEL ch,
);
Description
Enable the debounce function of specified external capture input source.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
V1.05.002
- 93 -
DrvTIMER_DisableCaptureDebounce
Prototype
int32_t DrvTIMER_DisableCaptureDebounce (
E_TIMER_CHANNEL ch,
);
Description
Disable the debounce function of specified external capture input source.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Disable external timer-0 capture debounce function */
DrvTIMER_DisableCaptureDebounce (E_TMR0);
DrvTIMER_EnableCounterDebounce
Prototype
int32_t DrvTIMER_EnableCounterDebounce (
E_TIMER_CHANNEL ch,
V1.05.002
- 94 -
);
Description
Enable the debounce function of specified external counter input source.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Enable external timer-0 counter debounce function */
DrvTIMER_EnableCounterDebounce (E_TMR0);
DrvTIMER_DisableCounterDebounce
Prototype
int32_t DrvTIMER_DisableCounterDebounce (
E_TIMER_CHANNEL ch,
);
Description
Disable the debounce function of specified external counter input source.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
V1.05.002
- 95 -
DrvTIMER_SelectCounterDetectPhase
Prototype
int32_t DrvTIMER_SelectCounterDetectPhase (
E_TIMER_CHANNEL ch,
E_TIMER_TX_PHASE phase
);
Description
This function is used to configure the counter detect phase of specified source.
Only NUC1x0xxxCx series support this function, ex:NUC140VE3CN.
Parameter
ch [in]
E_TIMER_CHANNEL, it could be E_TMR0 / E_TMR1 / E_TMR2 / E_TMR3
edge [in]
E_TIMER_TX_PHASE,
E_PHASE_FALLING : A falling edge of external counter pin will be counted.
E_PHASE_RISING : A rising edge of external counter pin will be counted.
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVTIMER_CHANNEL: Invalid timer channel
Example
/* Configure timer-0 counter detect phase is from low to high */
DrvTIMER_SelectCounterDetectPhase (E_TMR0, E_PHASE_RISING);
DrvTIMER_GetVersion
Prototype
uint32_t DrvTIMER_GetVersion (void)
Description
Get the version number of Timer/WDT driver.
Include
Driver/DrvTIMER.h
V1.05.002
- 96 -
Return Value
Version number :
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* Get the current version of Timer Driver */
u32Version = DrvTIMER_GetVersion ();
DrvWDT_Open
Prototype
int32_t DrvWDT_Open (E_WDT_INTERVAL WDTlevel)
Description
Enable WDT engine clock and set WDT time-out interval.
All bits in WDT register are write-protected. User must to check the REGWRPROT bit is
enabled or disabled if write the specified WDT bit fail.
Parameter
WDTlevel [in]
E_WDT_INTERVAL, enumerate the WDT time-out interval. Refer to
WDT_INTERVAL enumeration for detail time-out value.
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_DRVWDT_OPEN: WDT open fail
Example
/* Set the WDT time-out interval is (2^16)*WDT_CLK */
DrvWDT_Open (E_WDT_LEVEL6);
DrvWDT_Close
Prototype
void DrvWDT_Close (void)
Description
The function is used to stop/disable WDT relative functions.
All bits in WDT register are write-protected. User must to check the REGWRPROT bit is
enabled or disabled if write the specified WDT bit fail.
V1.05.002
- 97 -
Parameter
None
Include
Driver/DrvTIMER.h
Return Value
None
Example
/* Close Watch Dog Timer */
DrvWDT_Close ();
DrvWDT_InstallISR
Prototype
void DrvWDT_InstallISR (WDT_CALLBACK pvWDTISR)
Description
The function is used to install WDT interrupt service routine.
All bits in WDT register are write-protected. User must to check the REGWRPROT bit is
enabled or disabled if write the specified WDT bit fail.
Parameter
pvWDTISR [in]
The function pointer of the interrupt service routine
Include
Driver/DrvTIMER.h
Return Value
None
Example
/* Install the WDT callback function */
DrvWDT_InstallISR ((WDT_CALLBACK)WDT_Callback);
DrvWDT_Ioctl
Prototype
int32_T DrvWDT_Ioctl (E_WDT_CMD uWDTCmd, uint32_t uArgument)
Description
The function is used to operate more WDT applications, it could be the
start/stop the WDT, enable/disable WDT interrupt function, enable/disable WDT time-out
wake up function, enable/disable system reset when WDT time-out and set the WDT time-out
V1.05.002
- 98 -
interval.
All bits in WDT register are write-protected. User must to check the REGWRPROT bit is
enabled or disabled if write the specified WDT bit fail.
Parameter
uWDTCmd [in]
E_WDT_CMD commands, it could be the one of the follow commands
E_WDT_IOC_START_TIMER
E_WDT_IOC_STOP_TIMER
E_WDT_IOC_ENABLE_INT
E_WDT_IOC_DISABLE_INT
E_WDT_IOC_ENABLE _WAKEUP
E_WDT_IOC_DISABLE_WAKEUP
E_WDT_IOC_RESET_TIMER
E_WDT_IOC_ENABLE_RESET_FUNC
E_WDT_IOC_DISABLE_RESET_FUNC
E_WDT_IOC_SET_INTERVAL
uArgument [in]
Set the argument for the specified WDT command
Include
Driver/DrvTIMER.h
Return Value
E_SUCCESS: Operation successful
E_ DRVWDT_CMD: Invalid WDT command
Example
/* Start to count WDT by calling WDT_IOC_START_TIMER command */
DrvWDT_Ioctl (E_WDT_IOC_START_TIMER, 0);
V1.05.002
- 99 -
5. GPIO Driver
5.1. GPIO introduction
NUC100 Medium Density Series has up to 80 General Purpose I/O pins can be shared with other
function pins; it depends on the chip configuration. These 80 pins are arranged in 5 ports named with
GPIOA, GPIOB, GPIOC, GPIOD and GPIOE. Each port equips maximum 16 pins.
NUC100 Low Density Series has up to 65 General Purpose I/O pins can be shared with other function
pins; it depends on the chip configuration and package. These 65 pins are arranged in 5 ports. GPIOA,
GPIOB, GPIOC and GPIOD with each port equips maximum 16 pins and GPIOE with 1 pin GPE[5].
Value
0
1
2
3
4
Description
Define GPIO Port A
Define GPIO Port B
Define GPIO Port C
Define GPIO Port D
Define GPIO Port E
E_DRVGPIO_IO
Enumeration Identifier
E_IO_INPIT
E_IO_OUTPUT
E_IO_OPENDRAIN
E_IO_QUASI
Value
0
1
2
3
Description
Set GPIO as Input mode
Set GPIO as Output mode
Set GPIO as Open-Drain mode
Set GPIO as Quasi-bidirectional mode
V1.05.002
- 100 -
E_DRVGPIO_INT_TYPE
Enumeration Identifier
Value
Description
E_IO_RISING
E_IO_FALLING
E_IO_BOTH_EDGE
E_DRVGPIO_INT_MODE
Enumeration Identifier
E_MODE_EDGE
E_MODE_LEVEL
Value
0
1
Description
Set interrupt mode is Edge trigger
Set interrupt mode is Level trigger
E_DRVGPIO_DBCLKSRC
Enumeration Identifier
E_DBCLKSRC_HCLK
E_DBCLKSRC_10K
Value
Description
0
1
E_DRVGPIO_FUNC
Enumeration Identifier
Pins assignment
Description
E_FUNC_GPIO
E_FUNC_CLKO
E_FUNC_I2C0 / E_FUNC_I2C1
E_FUNC_I2S
E_FUNC_CAN0
E_FUNC_ACMP0 /
E_FUNC_ACMP1
GPC.6~7 / GPC.14~15
E_FUNC_SPI0
GPC.0~3
E_FUNC_SPI0_SS1
E_FUNC_SPI0_2BIT_MODE
GPB.10
GPC.4 and GPC.5
E_FUNC_SPI1
GPC.8~11
E_FUNC_SPI1_SS1
E_FUNC_SPI1_2BIT_MODE
GPB.9
GPC.12 and GPC.13
E_FUNC_SPI2
GPD.0~3
E_FUNC_SPI2_SS1
E_FUNC_SPI2_2BIT_MODE
GPA.7
GPD.4 and GPC.5
E_FUNC_SPI3
GPD.8~11
E_FUNC_SPI3_SS1
E_FUNC_SPI3_2BIT_MODE
GPB.14
GPD.12 and GPD.13
E_FUNC_SPI0_QFN36PIN
GPC.0~3
- 101 -
E_FUNC_SPI0_SS1_QFN36PIN
E_FUNC_SPI0_2BIT_MODE_
QFN36PIN
E_FUNC_ADC0 / E_FUNC_ADC1 /
E_FUNC_ADC2 / E_FUNC_ADC3 /
E_FUNC_ADC4 / E_FUNC_ADC5 /
E_FUNC_ADC6 / E_FUNC_ADC7
E_FUNC_EXTINT0 /
E_FUNC_EXTINT1
E_FUNC_TMR0 / E_FUNC_TMR1 /
E_FUNC_TMR2 / E_FUNC_TMR3
E_FUNC_T0EX / E_FUNC_T1EX /
E_FUNC_T2EX / E_FUNC_T3EX
E_FUNC_UART0
E_FUNC_UART0_RX_TX
E_FUNC_UART0_RTS_CTS
E_FUNC_UART1
E_FUNC_UART1_RX_TX
E_FUNC_UART1_RTS_CTS
E_FUNC_UART2
E_FUNC_PWM01 /
E_FUNC_PWM23 /
E_FUNC_PWM45 /
E_FUNC_PWM67
E_FUNC_PWM0 / E_FUNC_PWM1 /
E_FUNC_PWM2 / E_FUNC_PWM3 /
E_FUNC_PWM4 / E_FUNC_PWM5 /
E_FUNC_PWM6 / E_FUNC_PWM7
E_FUNC_EBI_8B
E_FUNC_EBI_16B
GPD.1
GPD.2 and GPD.3
GPA.0~7
Enable ADC0/ADC1/ADC2/ADC3/
ADC4/ADC5/ADC6/ADC7 functions
GPB.14 / GPB.15
Enable TIMER0/TIMER1/TIMER2/
TIMER3 as Toggle/Counter mode
GPB.15, GPE.5, GPB.2 Enable TIMER0/TIMER1/TIMER2/
and GPB.3
TIMER3 as external Capture mode
GPB.0~3
Enable UART0 RX, TX, RTS and CTS
GPB.0~1
Enable UART0 RX, TX
GPB.2~3
Enable UART0 RTS, CTS
GPB.4~7
Enable UART1 RX, TX, RTS and CTS
GPB.4~5
Enable UART1 RX, TX
GPB.6~7
Enable UART1 RTS, CTS
GPD.14~15
Enable UART2 RX, TX
GPA.12~13 /
GPA.14~15 /
Enable PWM01/PWM23/PWM45/
GPB.11, GPE.5 /
PWM67 functions
GPE.0~1
GPA.12 / GPA.13 /
GPA.14 / GPA.15 /
Enable PWM0/PWM1/PWM2/PWM3/
GPB.11 / GPE.5 /
PWM4/PWM5/PWM6/PWM7 functions
GPE.0 / GPE.1
GPB.12~13,GPC.14~15
GPC.6~7,GPA.6~7,
Enable EBI with 8 bit address width
GPB.6~7,GPA10~11
GPB.12~13,GPC.14~15
GPC.6~7,GPA.6~7,
GPA.5~1,GPA.12~14,
Enable EBI with 16 bit address width
GPB.6~7,GPA10~11
GPB.2~3
GPB.8~11
5.4. Macros
_DRVGPIO_DOUT
Prototype
_DRVGPIO_DOUT (PortNum, PinNum)
Description
This macro is used to control I/O Bit Output/Input Control Register of the specified pin. User
can set output data value of the specified pin by calling _DRVGPIO_DOUT macro, if the
V1.05.002
- 102 -
GPIO pin is configured as output mode. Or get the input data value by calling
_DRVGPIO_DOUT directly, if the GPIO pin is configured as input mode.
Note
Only NUC1x0xxxBx and NUC1x0xxxCx series support this function, ex:NUC140RD2BN
and NUC140VE3CN. Please refer to NuMicroTM NUC100 Series Products Selection Guide
of Appendix in details.
Parameter
PortNum [in]
Specify the GPIO port. It could be 0~4 to correspond to the GPIO-A/B/C/D/E.
PinNum [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Example
/* Configure GPA-1 to output mode */
DrvGPIO_Open (E_GPA, 1, E_IO_OUTPUT);
/* Set GPA-1 to high */
_DRVGPIO_DOUT (E_GPA, 1) = 1;
/* ...... */
/* Configure GPB-3 to input mode */
uint8_t u8PinValue;
DrvGPIO_Open (E_GPB, 3, E_IO_INPUT);
/* Get GPB-3 pin value */
u8PinValue = _DRVGPIO_DOUT (E_GPB, 3);
V1.05.002
- 103 -
Include
Driver/DrvGPIO.h
Example
/* Configure GPA-1 to output mode */
DrvGPIO_Open (E_GPA, 1, E_IO_OUTPUT);
/* Set GPA-1 to high */
GPA_1 = 1;
/* ...... */
/* Configure GPB-3 to input mode */
uint8_t u8PinValue;
DrvGPIO_Open (E_GPB, 3, E_IO_INPUT);
/* Get GPB-3 pin value */
u8PinValue = GPB_3;
5.5. Functions
DrvGPIO_Open
Prototype
int32_t DrvGPIO_Open (
E_DRVGPIO_PORT
port,
int32_t
i32Bit,
E_DRVGPIO_IO
mode
)
Description
Set the specified GPIO pin to the specified GPIO operation mode.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
mode [in]
E_DRVGPIO_IO, set the specified GPIO pin to be E_IO_INPUT, E_IO_OUTPUT,
E_IO_OPENDRAIN or E_IO_QUASI mode.
Include
Driver/DrvGPIO.h
Return Value
V1.05.002
- 104 -
DrvGPIO_Close
Prototype
int32_t DrvGPIO_Close (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Close the specified GPIO pin function and set the pin to quasi-bidirectional mode.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
E_DRVGPIO_ARGUMENT: Incorrect argument
Example
/* Close GPA-0 function and set to default quasi-bidirectional mode */
DrvGPIO_Close (E_GPA, 0);
DrvGPIO_SetBit
Prototype
int32_t DrvGPIO_SetBit (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Set the specified GPIO pin to 1.
Parameter
port [in]
V1.05.002
- 105 -
DrvGPIO_GetBit
Prototype
int32_t DrvGPIO_GetBit (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Get the pin value from the specified input GPIO pin.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
The specified input pin value: 0 / 1
E_DRVGPIO_ARGUMENT: ncorrect argument
Example
int32_t i32BitValue;
/* Configure GPA-1 as GPIO input mode*/
DrvGPIO_Open (E_GPA, 1, E_IO_INPUT);
i32BitValue = DrvGPIO_GetBit (E_GPA, 1);
V1.05.002
- 106 -
if
{
(u32BitValue == 1)
printf(GPA-1 pin status is high.\n);
}
else
{
printf(GPA-1 pin status is low.\n);
}
DrvGPIO_ClrBit
Prototype
int32_t DrvGPIO_ClrBit (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Set the specified GPIO pin to 0.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
E_DRVGPIO_ARGUMENT: Incorrect arguments
Example
/* Configure GPA-0 as GPIO output mode*/
DrvGPIO_Open (E_GPA, 0, E_IO_OUTPUT);
/* Set GPA-0 to 0(low) */
DrvGPIO_ClrBit (E_GPA, 0);
DrvGPIO_SetPortBits
Prototype
int32_tDrvGPIO_SetPortBits (E_DRVGPIO_PORT port, int32_t i32Data)
Description
Set the output port value to the specified GPIO port.
Parameter
V1.05.002
- 107 -
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Data [in]
The data output value. It could be 0~0xFFFF.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
E_DRVGPIO_ARGUMENT: Incorrect argument
Example
/* Set the output value of GPA port to 0x1234 */
DrvGPIO_SetPortBits (E_GPA, 0x1234);
DrvGPIO_GetPortBits
Prototype
int32_t DrvGPIO_GetPortBits (E_DRVGPIO_PORT port)
Description
Get the input port value from the specified GPIO port.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
Include
Driver/DrvGPIO.h
Return Value
The specified input port value: 0 ~ 0xFFFF
E_DRVGPIO_ARGUMENT: Incorrect argument
Example
/* Get the GPA port input data value */
int32_t i32PortValue;
i32PortValue = DrvGPIO_GetPortBits (E_GPA);
DrvGPIO_GetDoutBit
Prototype
V1.05.002
- 108 -
DrvGPIO_GetPortDoutBits
Prototype
int32_t DrvGPIO_GetPortDoutBits (E_DRVGPIO_PORT port)
Description
Get the port value from the specified Data Output Value Register. If the corresponding bit of
the return port value is 1, it means the corresponding bit is output data to high. Otherwise, its
output data to low.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
Include
Driver/DrvGPIO.h
Return Value
The portt value of the specified register: 0 ~ 0xFFFF
V1.05.002
- 109 -
DrvGPIO_SetBitMask
Prototype
int32_t DrvGPIO_SetBitMask (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
This function is used to protect the write data function of the corresponding GPIO pin. When
set the bit mask, the write signal is masked and write data to the protect bit is ignored.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example
/* Protect GPA-0 write data function */
DrvGPIO_SetBitMask (E_GPA, 0);
DrvGPIO_GetBitMask
Prototype
int32_t DrvGPIO_GetBitMask (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Get the bit value from the specified Data Output Write Mask Register. If the bit value is 1, it
means the corresponding bit is protected. And write data to the bit is ignored.
Parameter
port [in]
V1.05.002
- 110 -
DrvGPIO_ClrBitMask
Prototype
int32_t DrvGPIO_ClrBitMask (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
This function is used to remove the write protect function of the the corresponding GPIOpin.
After remove the bit mask, write data to the corresponding bit is workable.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example
/* Remove the GPA-0 write protect function */
DrvGPIO_ClrBitMask (E_GPA, 0);
V1.05.002
- 111 -
DrvGPIO_SetPortMask
Prototype
int32_t DrvGPIO_SetPortMask (E_DRVGPIO_PORT port, int32_t i32MaskData)
Description
This function is used to protect the write data function of the corresponding GPIO
pins.When set the bits are masked, write data to the protect bits are ignored.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32MaskData [in]
Specify pins of the GPIO port. It could be 0~0xFFFF.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
E_DRVGPIO_ARGUMENT: Incorrect argument
Example
/* Protect GPA-0/4 write data function */
DrvGPIO_SetPortMask (E_GPA, 0x11);
DrvGPIO_GetPortMask
Prototype
int32_t DrvGPIO_GetPortMask (E_DRVGPIO_PORT port)
Description
Get the port value from the specified Data Output Write Mask Register. If the corresponding
bit of the return port value is 1, its meaning the bits are protected. And write data to the bits
are ignored.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
Include
Driver/DrvGPIO.h
V1.05.002
- 112 -
Return Value
The portt value of the specified register: 0 ~ 0xFFFF
Example
/* Get the port value from GPA Data Output Write Mask Resister */
int32_t i32MaskValue;
i32MaskValue = DrvGPIO_GetPortMask (E_GPA);
/* If (i32MaskValue = 0x11), its meaning GPA-0/4 are protected */
DrvGPIO_ClrPortMask
Prototype
int32_t DrvGPIO_ClrPortMask (E_DRVGPIO_PORT port, int32_t i32MaskData)
Description
This function is used to remove the write protect function of the corresponding GPIO pins.
After remove those bits mask, write data to the corresponding bits are workable.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32MaskData [in]
Specify pins of the GPIO port. It could be 0~0xFFFF.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example
/* Remove the GPA-0/4 write protect function */
DrvGPIO_ClrPortMask (E_GPA, 0x11);
DrvGPIO_EnableDigitalInputBit
Prototype
int32_t DrvGPIO_EnableDigitalInputBit (
E_DRVGPIO_PORT port,
E_DRVGPIO_PIN i32Bit
)
Description
Enable IO digital input path of the specified GPIO input pin.
V1.05.002
- 113 -
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port.
It could be E_GPA, E_GPB, E_GPC, E_GPD and E_GPE.
pin [in]
Specify pin of the GPIO port. It could be be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example:
/* Enable GPA.0 IO digital input path */
DrvGPIO_EnableDigitalInputBit (E_GPA, 0);
DrvGPIO_DisableDigitalInputBit
Prototype
int32_t DrvGPIO_DisableDigitalInputBit (
E_DRVGPIO_PORT port,
E_DRVGPIO_PIN i32Bit
)
Description
Disable IO digital input path of the specified GPIO input pin.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port.
It could be E_GPA, E_GPB, E_GPC, E_GPD and E_GPE.
pin [in]
Specify pin of the GPIO port. It could be be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example:
/* Disable GPA.0 IO digital input path */
DrvGPIO_DisableDigitalInputBit (E_GPA, 0);
V1.05.002
- 114 -
DrvGPIO_EnableDebounce
Prototype
int32_t DrvGPIO_EnableDebounce (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Enable the de-bounce function of the specified GPIO input pin.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example
/* Enable GPA-0 interrupt de-bounce function */
DrvGPIO_EnableDebounce (E_GPA, 0);
DrvGPIO_DisableDebounce
Prototype
int32_t DrvGPIO_DisableDebounce (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Disable the de-bounce function of the specified GPIO input pin.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15.
Include
Driver/DrvGPIO.h
Return Value
V1.05.002
- 115 -
DrvGPIO_SetDebounceTime
Prototype
int32_t DrvGPIO_SetDebounceTime (
uint32_t u32CycleSelection,
E_DRVGPIO_DBCLKSRC ClockSource
)
Description
Set the interrupt de-bounce sampling time based on the de-bounce counter clock source. If
the de-bounce clock source is from internal 10 KHz and sampling cycle selection is 4. The
target de-bounce time is (2^4)*(1/(10*1000)) s = 16*0.0001 s = 1600 us, and system will
sampling interrupt input once per 1600 us.
Parameter
u32CycleSelection [in]
The number of sampling cycle selection, the range of value is from 0 ~ 15. The target
de-bounce time is (2^(u32CycleSelection))*(ClockSource) second.
ClockSource [in]
E_DRVGPIO_DBCLKSRC, it could be DBCLKSRC_HCLK or DBCLKSRC_10K.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
E_DRVGPIO_ARGUMENT: Incorrect argument
Example
/* Set de-bounce sampling time to 1600 us. (2^4)*(10 KHz) */
DrvGPIO_SetDebounceTime (4, E_DBCLKSRC_10K);
DrvGPIO_GetDebounceSampleCycle
Prototype
int32_t DrvGPIO_GetDebounceSampleCycle (void)
Description
V1.05.002
- 116 -
This function is used to get the number of de-bounce sampling cycle selection.
Parameter
None
Include
Driver/DrvGPIO.h
Return Value
Number of the sampling cycle selection: 0 ~ 15
Example
int32_t i32CycleSelection;
i32CycleSelection = DrvGPIO_GetDebounceSampleCycle ();
/* If i32CycleSelection is 4 and clock source from 10 KHz.
/* Its meaning to sample interrupt input once per 16*100us. */
*/
DrvGPIO_EnableInt
Prototype
int32_t DrvGPIO_EnableInt (
E_DRVGPIO_PORT port,
int32_t i32Bit,
E_DRVGPIO_INT_TYPE TriggerType,
E_DRVGPIO_INT_MODE Mode
)
Description
Enable the interrupt function of the specified GPIO pin. Excpet for GPB.14 and GPB.15 pins.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15. But the GPB.14/15 is only used for
external interrupt 0/1.
TriggerType [in]
E_DRVGPIO_INT_TYPE, specify the interrupt trigger type. It could be
E_IO_RISING, E_IO_FALLING or E_IO_BOTH_EDGE and its meaning the
interrupt function enable by rising edge/high level, falling edge/low level or both rising
edge and falling edge. If the interrupt mode is E_MODE_LEVEL and interrupt type is
E_BOTH_EDGEthen calling this API is ignored.
Mode [in]
V1.05.002
- 117 -
DrvGPIO_DisableInt
Prototype
int32_t DrvGPIO_DisableInt (E_DRVGPIO_PORT port, int32_t i32Bit)
Description
Disable the interrupt function of the specified GPIO pin. Excpet for GPB.14 and GPB.15
pins.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
i32Bit [in]
Specify pin of the GPIO port. It could be 0~15. But the GPB.14/15 is only used for
external interrupt 0/1.
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
Example
/* Disable GPB-13 interrupt function. */
DrvGPIO_DisableInt (E_GPB, 13);
V1.05.002
- 118 -
DrvGPIO_SetIntCallback
Prototype
void DrvGPIO_SetIntCallback (
GPIO_GPAB_CALLBACK pfGPABCallback,
GPIO_GPCDE_CALLBACK pfGPCDECallback
)
Description
Install the interrupt callback function for GPA/GPB port and GPC/GPD/GPE port,
GPB.14 and GPB.15 pins.
except
Parameter
pfGPABCallback [in], the function pointer of GPA/GPB callback function.
pfGPCDECallback [in], the function pointer of GPC/GPD/GPE callback function.
Include
Driver/DrvGPIO.h
Return Value
None
Example
/* Set GPA/B and GPC/D/E interrupt callback functions */
DrvGPIO_SetIntCallback (GPABCallback, GPCDECallback);
DrvGPIO_EnableEINT0
Prototype
void DrvGPIO_EnableEINT0 (
E_DRVGPIO_INT_TYPE TriggerType,
E_DRVGPIO_INT_MODE Mode,
GPIO_EINT0_CALLBACK pfEINT0Callback
)
Description
Enable the interrupt function for external GPIO interrupt from /INT0(GPB.14) pin.
Parameter
TriggerType [in]
E_DRVGPIO_INT_TYPE, specify the interrupt trigger type. It could be E_IO_RISING,
E_IO_FALLING or E_IO_BOTH_EDGE and its meaning the interrupt function enable
by rising edge/high level, falling edge/low level or both rising edge and falling edge. If
V1.05.002
- 119 -
DrvGPIO_DisableEINT0
Prototype
void DrvGPIO_DisableEINT0 (void)
Description
Disable the interrupt function for external GPIO interrupt from /INT0 (GPB.14) pin.
Parameter
None
Include
Driver/DrvGPIO.h
Return Value
None
Example
/* Disable external INT0 interrupt function. */
DrvGPIO_DisableEINT0 ();
DrvGPIO_EnableEINT1
Prototype
void DrvGPIO_EnableEINT1 (
V1.05.002
- 120 -
E_DRVGPIO_INT_TYPE TriggerType,
E_DRVGPIO_INT_MODE Mode,
GPIO_EINT0_CALLBACK pfEINT0Callback
)
Description
Enable the interrupt function for external GPIO interrupt from /INT1(GPB.15) pin.
Parameter
TriggerType [in]
E_DRVGPIO_INT_TYPE, specify the interrupt trigger type. It could be E_IO_RISING,
E_IO_FALLING or E_IO_BOTH_EDGE and its meaning the interrupt function enable
by rising edge/high level, falling edge/low level or both rising edge and falling edge. If
the interrupt mode is E_MODE_LEVEL and interrupt type is E_BOTH_EDGEthen
calling this API is ignored.
Mode [in]
E_DRVGPIO_INT_MODE, specify the interrupt mode. It could be E_MODE_EDGE
or E_MODE_LEVEL L to control the interrupt is by edge trigger or by level trigger. If
the interrupt mode is E_MODE_LEVEL and interrupt type is E_BOTH_EDGEthen
calling this API is ignored
pfEINT1Callback [in]
Its the function pointer of the external INT1 callback function.
Include
Driver/DrvGPIO.h
Return Value
None
Example
/* Enable external INT1 interrupt as low level trigger. */
DrvGPIO_EnableEINT1 (E_IO_FALLING, E_MODE_LEVEL, EINT1Callback);
DrvGPIO_DisableEINT1
Prototype
void DrvGPIO_DisableEINT1 (void)
Description
Disable the interrupt function for external GPIO interrupt from /INT1(GPB.15) pin.
Parameter
None
Include
V1.05.002
- 121 -
Driver/DrvGPIO.h
Return Value
None
Example
/* Disable external INT1 interrupt function. */
DrvGPIO_DisableEINT1 ();
DrvGPIO_GetIntStatus
Prototype
uint32_t DrvGPIO_GetIntStatus (E_DRVGPIO_PORT port)
Description
Get the port value from the specified Interrupt Trigger Source Indicator Register.If the
corresponding bit of the return port value is 1, its meaning the interrupt occurred at the
corresponding bit. Otherwise, no interrupt occurred at that bit.
Parameter
port [in]
E_DRVGPIO_PORT, specify GPIO port. It could be E_GPA, E_GPB, E_GPC,
E_GPD and E_GPE.
Include
Driver/DrvGPIO.h
Return Value
The portt value of the specified register: 0 ~ 0xFFFF
Example
/* Get GPA interrupt status. */
int32_t i32INTStatus;
i32INTStatus = DrvGPIO_GetIntStatus (E_GPA);
DrvGPIO_InitFunction
Prototype
int32_t DrvGPIO_InitFunction (E_DRVGPIO_FUNC function)
Description
Initialize the specified function and configure the relative pins for specified function used.
Note
Not all the chips support these functions. Please refer to NuMicroTM NUC100 Series
Products Selection Guide of Appendix in details.
V1.05.002
- 122 -
Parameter
function [in]
DRVGPIO_FUNC, specified the relative GPIO pins as special function pins.
It could be:
E_FUNC_GPIO,
E_FUNC_CLKO,
E_FUNC_I2C0 / E_FUNC_I2C1,
E_FUNC_I2S,
E_FUNC_CAN0,
E_FUNC_ACMP0 / E_FUNC_ACMP1,
E_FUNC_SPI0 / E_FUNC_SPI0_SS1 / E_FUNC_SPI0_2BIT_MODE,
E_FUNC_SPI1 / E_FUNC_SPI1_SS1 / E_FUNC_SPI1_2BIT_MODE,
E_FUNC_SPI2 / E_FUNC_SPI2_SS1 / E_FUNC_SPI2_2BIT_MODE,
E_FUNC_SPI3 / E_FUNC_SPI3_SS1 / E_FUNC_SPI3_2BIT_MODE,
E_FUNC_SPI0_QFN36PIN / E_FUNC_SPI0_SS1_QFN36PIN /
E_FUNC_SPI0_2BIT_MODE_QFN36PIN,
E_FUNC_ADC0 / E_FUNC_ADC1 / E_FUNC_ADC2 / E_FUNC_ADC3 /
E_FUNC_ADC4 / E_FUNC_ADC5 / E_FUNC_ADC6 / FUNC_ADC7,
E_FUNC_EXTINT0 / E_FUNC_EXTINT1,
E_FUNC_TMR0 / E_FUNC_TMR1 / E_FUNC_TMR2 / E_FUNC_TMR3,
E_FUNC_T0EX / E_FUNC_T1EX / E_FUNC_T2EX / E_FUNC_T3EX,
E_FUNC_UART0 / E_FUNC_UART0_RX_TX / E_FUNC_UART0_RTS_CTS,
E_FUNC_UART1 / E_FUNC_UART1_RX_TX / E_FUNC_UART1_RTS_CTS,
E_FUNC_UART2 / E_FUNC_UART2_RX_TX / E_FUNC_UART2_RTS_CTS,
E_FUNC_PWM01 / E_FUNC_PWM23 / E_FUNC_PWM45 / E_FUNC_PWM67,
E_FUNC_PWM0 / E_FUNC_PWM1 / E_FUNC_PWM2 / E_FUNC_PWM3 /
E_FUNC_PWM4 / E_FUNC_PWM5 / E_FUNC_PWM6 / E_FUNC_PWM7,
E_FUNC_EBI_8B / E_FUNC_EBI_16B,
Include
Driver/DrvGPIO.h
Return Value
E_SUCCESS: Operation successful
E_DRVGPIO_ARGUMENT: Incorrect argument
Example
/* Init UART0 RX, TX, RTS and CTS function */
DrvGPIO_InitFunction (E_FUNC_UART0);
DrvGPIO_GetVersion
Prototype
uint32_t DrvGPIO_GetVersion (void)
Description
This function is used to return the version number of GPIO driver.
Include
V1.05.002
- 123 -
Driver/DrvGPIO.h
Return Value
The version number of GPIO driver:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* Get the current version of GPIO Driver */
int32_t i32GPIOVer;
i32GPIOVer = DrvGPIO_GetVersion ();
V1.05.002
- 124 -
6. ADC Driver
6.1. ADC Introduction
NuMicroTM NUC100 series contain one 12-bit successive approximation analog-to-digital converters (SAR
A/D converter) with 8 input channels. It takes about 27 ADC clock cycles to convert one sample, and the
maximum input clock to ADC is 16MHz at 5.0V.The A/D converter supports three operation modes: single,
single-cycle scan and continuous scan mode. The A/D converters can be started by software and external
STADC/PB.8 pin. In this document, we will introduce how to use the ADC driver.
Single mode
V1.05.002
- 125 -
Value
0
1
Description
ADC single end input
ADC differential input
Value
0
1
2
Description
Single operation mode
Single cycle scan mode
Continuous scan mode
Value
0
1
2
3
Description
External 12MHz clock
Internal PLL clock
System clock
Internal 22.1184MHz clock
Value
0
1
2
3
Description
Low level trigger
High level trigger
Falling edge trigger
Rising edge trigger
Value
0
1
2
Description
External input signal
Internal bandgap voltage
Internal temperature sensor
Value
0
1
Description
Less than compare data
Greater or equal to compare data
E_ADC_OPERATION_MODE
Enumeration Identifier
ADC_SINGLE_OP
ADC_SINGLE_CYCLE_OP
ADC_CONTINUOUS_OP
E_ADC_CLK_SRC
Enumeration Identifier
EXTERNAL_12MHZ
INTERNAL_PLL
INTERNAL_HCLK
INTERNAL_RC22MHZ
E_ADC_EXT_TRI_COND
Enumeration Identifier
LOW_LEVEL
HIGH_LEVEL
FALLING_EDGE
RISING_EDGE
E_ADC_CH7_SRC
Enumeration Identifier
EXTERNAL_INPUT_SIGNAL
INTERNAL_BANDGAP
INTERNAL_TEMPERATURE_SENSOR
E_ADC_CMP_CONDITION
Enumeration Identifier
LESS_THAN
GREATER_OR_EQUAL
E_ADC_DIFF_MODE_OUTPUT_FORMAT
Enumeration Identifier
UNSIGNED_OUTPUT
TWOS_COMPLEMENT
Value
0
1
Description
Unsigned format
2s complement format
V1.05.002
- 126 -
6.4. Macros
_DRVADC_CONV
Prototype
void _DRVADC_CONV (void);
Description
Inform ADC to start an A/D conversion.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Start an A/D conversion */
_DRVADC_CONV();
_DRVADC_GET_ADC_INT_FLAG
Prototype
uint32_t _DRVADC_GET_ADC_INT_FLAG (void);
Description
Get the status of ADC interrupt flag.
Include
Driver/DrvADC.h
Return Value
0: ADC interrupt does not occur.
1: ADC interrupt occurs.
Example
/* Get the status of ADC interrupt flag */
if(_DRVADC_GET_ADC_INT_FLAG())
printf(ADC interrupt occurs.\n);
V1.05.002
- 127 -
_DRVADC_GET_CMP0_INT_FLAG
Prototype
uint32_t _DRVADC_GET_CMP0_INT_FLAG (void);
Description
Get the status of ADC comparator 0 interrupt flag.
Include
Driver/DrvADC.h
Return Value
0: ADC comparator 0 interrupt does not occur.
1: ADC comparator 0 interrupt occurs.
Example
/* Get the status of ADC comparator 0 interrupt flag */
if(_DRVADC_GET_CMP0_INT_FLAG())
printf(ADC comparator 0 interrupt occurs.\n);
_DRVADC_GET_CMP1_INT_FLAG
Prototype
uint32_t _DRVADC_GET_CMP1_INT_FLAG (void);
Description
Get the status of ADC comparator 1 interrupt flag.
Include
Driver/DrvADC.h
Return Value
0: ADC comparator 1 interrupt does not occur.
1: ADC comparator 1 interrupt occurs.
Example
/* Get the status of ADC comparator 1 interrupt flag */
if(_DRVADC_GET_CMP1_INT_FLAG())
printf(ADC comparator 1 interrupt occurs.\n);
_DRVADC_CLEAR_ADC_INT_FLAG
Prototype
V1.05.002
- 128 -
_DRVADC_CLEAR_CMP0_INT_FLAG
Prototype
void _DRVADC_CLEAR_CMP0_INT_FLAG (void);
Description
Clear the ADC comparator 0 interrupt flag.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Clear the ADC comparator 0 interrupt flag */
_DRVADC_CLEAR_CMP0_INT_FLAG();
_DRVADC_CLEAR_CMP1_INT_FLAG
Prototype
void _DRVADC_CLEAR_CMP1_INT_FLAG (void);
Description
Clear the ADC comparator 1 interrupt flag.
Include
Driver/DrvADC.h
Return Value
V1.05.002
- 129 -
None.
Example
/* Clear the ADC comparator 1 interrupt flag */
_DRVADC_CLEAR_CMP1_INT_FLAG();
6.5. Functions
DrvADC_Open
Prototype
void DrvADC_Open (
E_ADC_INPUT_MODE InputMode,
E_ADC_OPERATION_MODE OpMode,
uint8_t u8ChannelSelBitwise,
E_ADC_CLK_SRC ClockSrc,
uint8_t u8AdcDivisor
);
Description
Enable the ADC function and configure the related settings.
Parameters
InputMode [in]
Specify the type of the analog input signal. It might be single-end or differential input.
ADC_SINGLE_END
ADC_DIFFERENTIAL
OpMode [in]
Specify the operation mode. It might be single, single cycle scan or continuous scan
mode.
ADC_SINGLE_OP
: single mode
ADC_SINGLE_CYCLE_OP
ADC_CONTINUOUS_OP
u8ChannelSelBitwise [in]
Specify the input channels. If software enables more than one channel in single mode,
only the channel with the lowest number will be converted and the other enabled channels
will be ignored. For example, if user enable channel 2, 3 and 4 in single mode, only
channel 2 will be converted. In differential input mode, only the even number of the two
corresponding channels needs to be enabled. The conversion result will be placed to the
corresponding data register of the selected channel. For example, in single-end input
V1.05.002
- 130 -
mode, 0x4 means the channel 2 is selected; in differential input mode, it means channel
pair 1 is selected.
ClockSrc [in]
Specify the clock source of ADC clock.
EXTERNAL_12MHZ
INTERNAL_PLL
INTERNAL_HCLK
: system clock
INTERNAL_RC22MHZ
u8AdcDivisor [in]
Determine the ADC clock frequency. The range of u8AdcDivisor is 0 ~ 0xFF.
ADC clock frequency = ADC clock source frequency / ( u8AdcDivisor + 1 )
Include
Driver/DrvADC.h
Return Value
None.
Example
/* single end input, single operation mode, channel 5 is selected, ADC clock frequency =
12MHz/(5+1) */
DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0x20, EXTERNAL_12MHZ, 5);
DrvADC_Close
Prototype
void DrvADC_Close (void);
Description
Close ADC functions. Disable ADC, ADC engine clock and ADC interrupt.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Close the ADC function */
DrvADC_Close();
V1.05.002
- 131 -
DrvADC_SetADCChannel
Prototype
void DrvADC_SetADCChannel (
uint8_t u8ChannelSelBitwise,
E_ADC_INPUT_MODE InputMode
);
Description
Select ADC input channels.
Parameters
u8ChannelSelBitwise [in]
Specify the input channels. If software enables more than one channel in single mode,
only the channel with the lowest number will be converted and the other enabled channels
will be ignored. For example, if user enable channel 2, 3 and 4 in single mode, only
channel 2 will be converted. In differential input mode, only the even number of the two
corresponding channels needs to be enabled. The conversion result will be placed to the
corresponding data register of the selected channel. For example, in single-end input
mode, 0x4 means the channel 2 is selected; in differential input mode, it means channel
pair 1 is selected.
InputMode [in]
Specify the type of the analog input signal. It might be single-end or differential input.
ADC_SINGLE_END
ADC_DIFFERENTIAL
Include
Driver/DrvADC.h
Return Value
None.
Example
/* In single-end input mode, this function select channel 0 and channel 2; In differential input
mode, it select channel pair 0 and channel pair 1. */
DrvADC_SetADCChannel (0x5);
DrvADC_ConfigADCChannel7
Prototype
void DrvADC_ConfigADCChannel7 (E_ADC_CH7_SRC Ch7Src);
Description
Select the input signal source of ADC channel 7.
V1.05.002
- 132 -
Parameters
Ch7Src [in]
Specify the analog input source.
EXTERNAL_INPUT_SIGNAL : external analog input
INTERNAL_BANDGAP : internal band gap voltage
INTERNAL_TEMPERATURE_SENSOR
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Select the external analog input as the source of channel 7 */
DrvADC_ConfigADCChannel7(EXTERNAL_INPUT_SIGNAL);
DrvADC_SetADCInputMode
Prototype
void DrvADC_SetADCInputMode (E_ADC_INPUT_MODE InputMode);
Description
Set the ADC input mode.
Parameters
InputMode [in]
Specify the input mode.
ADC_SINGLE_END
ADC_DIFFERENTIAL
Include
Driver/DrvADC.h
Return Value
None.
Example
/* The following statement indicates that the external analog input is a single-end input */
DrvADC_SetADCInputMode(ADC_SINGLE_END);
V1.05.002
- 133 -
DrvADC_SetADCOperationMode
Prototype
void DrvADC_SetADCOperationMode (E_ADC_OPERATION_MODE OpMode);
Description
Set the ADC operation mode.
Parameters
OpMode [in]
Specify the operation mode.
ADC_SINGLE_OP
: single mode
ADC_SINGLE_CYCLE_OP
ADC_CONTINUOUS_OP
Include
Driver/DrvADC.h
Return Value
None.
Example
/* The following statement configures the single mode as the operation mode */
DrvADC_SetADCOperationMode(ADC_SINGLE_OP);
DrvADC_SetADCClkSrc
Prototype
void DrvADC_SetADCClkSrc (E_ADC_CLK_SRC ClockSrc);
Description
Select the ADC clock source.
Parameters
ClockSrc [in]
Specify the ADC clock source.
EXTERNAL_12MHZ
INTERNAL_PLL
INTERNAL_HCLK
: system clock
INTERNAL_RC22MHZ
Include
Driver/DrvADC.h
V1.05.002
- 134 -
Return Value
None.
Example
/* Select the external 12MHz crystal as the clock source of ADC */
DrvADC_SetADCClkSrc (EXTERNAL_12MHZ);
DrvADC_SetADCDivisor
Prototype
void DrvADC_SetADCDivisor (uint8_t u8AdcDivisor);
Description
Set the divisor value of ADC clock to determine the ADC clock frequency.
ADC clock frequency = ADC clock source frequency / ( u8AdcDivisor + 1 )
Parameters
u8AdcDivisor [in]
Specify the divisor value. The range of u8AdcDivisor is 0 ~ 0xFF.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* The clock source of ADC is from external 12MHz crystal. The ADC clock frequency is
2MHz. */
DrvADC_SetADCClkSrc (EXTERNAL_12MHZ);
DrvADC_SetADCDivisor (5);
DrvADC_EnableADCInt
Prototype
void DrvADC_EnableADCInt (
DRVADC_ADC_CALLBACK Callback,
uint32_t u32UserData
);
Description
V1.05.002
- 135 -
Enable ADC interrupt and setup the callback function. As an ADC interrupt occurs, the
callback function will be executed. When the ADC interrupt function is enabled and one of
the following conditions happens, the ADC interrupt will be asserted.
The A/D conversion of the specified channel is completed in single mode.
The A/D conversions of all selected channels are completed in single cycle scan mode or
continuous scan mode.
Parameters
Callback [in]
The callback function of the ADC interrupt.
u32UserData [in]
The parameter of the callback function.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* ADC interrupt callback function */
void AdcIntCallback(uint32_t u32UserData)
{
gu8AdcIntFlag = 1;
}
/* Enable the ADC interrupt and setup the callback function. The parameter 0 will be passed
to the callback function. */
DrvADC_EnableADCInt(AdcIntCallback, 0);
DrvADC_DisableADCInt
Prototype
void DrvADC_DisableADCInt (void);
Description
Disable the ADC interrupt.
Parameters
None
Include
Driver/DrvADC.h
V1.05.002
- 136 -
Return Value
None.
Example
/* Disable the ADC interrupt */
DrvADC_DisableADCInt();
DrvADC_EnableADCCmp0Int
Prototype
void DrvADC_EnableADCCmp0Int (
DRVADC_ADCMP0_CALLBACK Callback,
uint32_t u32UserData
);
Description
Enable the ADC comparator 0 interrupt and setup callback function. If the conversion result
satisfies the compare conditions set in DrvADC_EnableADCCmp0(), a comparator 0
interrupt will be asserted and the callback function will be executed.
Parameters
Callback [in]
The callback function of the ADC comparator 0 interrupt.
u32UserData [in]
The parameter of the callback function.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* ADC comparator 0 interrupt callback function */
void Cmp0IntCallback(uint32_t u32UserData)
{
gu8AdcCmp0IntFlag = 1;
}
int32_t main()
{
V1.05.002
- 137 -
/* Enable the ADC comparator 0 interrupt and setup the callback function. The parameter
0 will be passed to the callback function. */
DrvADC_EnableADCCmp0Int(Cmp0IntCallback, 0);
}
DrvADC_DisableADCCmp0Int
Prototype
void DrvADC_DisableAdcmp0Int (void);
Description
Disable the ADC comparator 0 interrupt.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Disable the ADC comparator 0 interrupt */
DrvADC_DisableADCCmp0Int();
DrvADC_EnableADCCmp1Int
Prototype
void DrvADC_EnableADCCmp1Int (
DRVADC_ADCMP1_CALLBACK Callback,
uint32_t u32UserData
);
Description
Enable the ADC comparator 1 interrupt and setup callback function. If the conversion result
satisfies the compare conditions set in DrvADC_EnableADCCmp1(), a comparator 1
interrupt will be asserted and the callback function will be executed.
Parameters
Callback [in]
The callback function of the ADC comparator 1 interrupt.
u32UserData [in]
V1.05.002
- 138 -
/* Enable the ADC comparator 1 interrupt and setup the callback function. The parameter
0 will be passed to the callback function. */
DrvADC_EnableADCCmp1Int(Cmp1IntCallback, 0);
}
DrvADC_DisableADCCmp1Int
Prototype
void DrvADC_DisableADCCmp1Int (void);
Description
Disable the ADC comparator 1 interrupt.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Disable the ADC comparator 1 interrupt */
DrvADC_DisableADCCmp1Int();
V1.05.002
- 139 -
DrvADC_GetConversionRate
Prototype
uint32_t DrvADC_GetConversionRate (void);
Description
Get the A/D conversion rate. The ADC takes about 27 ADC clock cycles for converting one
sample.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
Return the conversion rate. The unit is sample/second.
Example
/* The clock source of ADC is from external 12MHz crystal. The ADC clock frequency is
2MHz. The conversion rate is about 74K sample/second */
DrvADC_SetADCClkSrc (EXTERNAL_12MHZ);
DrvADC_SetADCDivisor (5);
/* Get the conversion rate */
printf("Conversion rate: %d samples/second\n", DrvADC_GetConversionRate());
DrvADC_EnableExtTrigger
Prototype
void DrvADC_EnableExtTrigger (E_ADC_EXT_TRI_COND TriggerCondition);
Description
Allow the external trigger pin (PB8) to be the trigger source of ADC. The external trigger pin
must be configured as an input pin in advance.
Parameters
TriggerCondition [in]
Specify the trigger condition. The trigger condition could be low-level / high-level /
falling-edge / positive-edge.
LOW_LEVEL
: low level.
HIGH_LEVEL
: high level.
: rising edge.
V1.05.002
- 140 -
Include
Driver/DrvADC.h
Return Value
None
Example
/* Use PB8 pin as the external trigger pin. The trigger condition is low level trigger. */
DrvADC_EnableExtTrigger(LOW_LEVEL);
DrvADC_DisableExtTrigger
Prototype
void DrvADC_DisableExtTrigger (void);
Description
Prohibit the external ADC trigger.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Disable the ADC external trigger source */
DrvADC_DisableExtTrigger ();
DrvADC_StartConvert
Prototype
void DrvADC_StartConvert(void);
Description
Clear the ADC interrupt flag (ADF) and start A/D converting.
Parameters
None.
Include
Driver/DrvADC.h
V1.05.002
- 141 -
Return Value
None.
Example
/* Clear ADF bit and start converting */
DrvADC_StartConvert();
DrvADC_StopConvert
Prototype
void DrvADC_StopConvert(void);
Description
Stop A/D converting.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Stop converting */
DrvADC_StopConvert();
DrvADC_IsConversionDone
Prototype
uint32_t DrvADC_IsConversionDone (void);
Description
Check whether the conversion action is finished or not.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
TURE
Conversion finished
V1.05.002
- 142 -
FALSE
In converting
Example
/* If the ADC interrupt is not enabled, user can call this function to check the state of
conversion action */
/* Start A/D conversion */
DrvADC_StartConvert();
/* Wait conversion done */
while(!DrvADC_IsConversionDone());
DrvADC_GetConversionData
Prototype
int32_t DrvADC_GetConversionData (uint8_t u8ChannelNum);
Description
Get the conversion result of the specified ADC channel.
Parameters
u8ChannelNum [in]
Specify the ADC channel. The range of this value is 0~7.
Include
Driver/DrvADC.h
Return Value
A 32-bit conversion result. It is generated by extending the original 12 bits conversion result.
Example
/* Get the conversion result of ADC channel 3 */
printf(Conversion result of channel 3: %d\n, DrvADC_GetConversionData(3));
DrvADC_EnablePDMA
Prototype
void DrvADC_EnablePDMA (void);
Description
Enable PDMA transfer. User can transfer the A/D conversion result to user-specified memory
space by PDMA without CPU intervention. In single mode, only the conversion result of the
selected channel will be transferred. In single cycle scan mode or continuous scan mode, the
conversion results of all enabled channels will be transferred by PDMA.
Parameters
V1.05.002
- 143 -
None.
Include
Driver/DrvADC.h
Return Value
None
Example
/* Enable PDMA transfer */
DrvADC_EnablePDMA();
DrvADC_DisablePDMA
Prototype
void DrvADC_DisablePDMA (void);
Description
Disable PDMA transfer.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None
Example
/* Disable PDMA transfer */
DrvADC_DisablePDMA();
DrvADC_IsDataValid
Prototype
uint32_t DrvADC_IsDataValid (uint8_t u8ChannelNum);
Description
Check whether the conversion data is valid or not.
Parameters
u8ChannelNum [in]
Specify the ADC channel. The range of this value is 0~7.
V1.05.002
- 144 -
Include
Driver/DrvADC.h
Return Value
TURE: data is valid
FALSE: data is invalid
Example
/* Check if the data of channel 3 is valid. */
If( DrvADC_IsDataValid(3) )
u32ConversionData = DrvADC_GetConversionData(u8ChannelNum); /* Get the data */
DrvADC_IsDataOverrun
Prototype
uint32_t DrvADC_IsDataOverrun (uint8_t u8ChannelNum);
Description
Check whether the conversion data is overrun or not.
Parameters
u8ChannelNum [in]
Specify the ADC channel. The range of this value is 0~7.
Include
Driver/DrvADC.h
Return Value
TURE
overrun
FALSE
non-overrun
Example
/* Check if the data of channel 3 is overrun. */
If(DrvADC_IsDataOverrun(3) )
printf(The data has been overwritten.\n);
DrvADC_EnableADCCmp0
Prototype
int32_t DrvADC_EnableADCCmp0 (
uint8_t u8CmpChannelNum,
E_ADC_CMP_CONDITION CmpCondition,
V1.05.002
- 145 -
uint16_t u16CmpData,
uint8_t u8CmpMatchCount
);
Description
Enable the ADC comparator 0 and configure the necessary settings.
Parameters
u8CmpChannelNum [in]
Specify the channel number that wants to compare. The range of this value is 0~7.
CmpCondition [in]
Specify the compare condition.
LESS_THAN
DrvADC_DisableADCCmp0
Prototype
void DrvADC_DisableADCCmp0 (void);
Description
V1.05.002
- 146 -
DrvADC_EnableADCCmp1
Prototype
int32_t DrvADC_EnableADCCmp1 (
uint8_t u8CmpChannelNum,
E_ADC_CMP_CONDITION CmpCondition,
uint16_t u16CmpData,
uint8_t u8CmpMatchCount
);
Description
Enable the ADC comparator 1 and configure the necessary settings.
Parameters
u8CmpChannelNum [in]
Specify the channel number that wants to compare. The range of this value is 0~7.
CmpCondition [in]
Specify the compare condition.
LESS_THAN : less than the compare data.
GREATER_OR_EQUAL : greater or equal to the compare data.
u16CmpData [in]
Specify the compare data. The range is 0 ~ 0xFFF.
u8CmpMatchCount [in]
Specify the compare match count. The range is 0 ~ 15. When the specified A/D channel
analog conversion result matches the compare condition, the internal match counter will
increase 1. When the internal counter reaches the value to (u8CmpMatchCount +1), the
interrupt flag of comparator 1 will be set.
V1.05.002
- 147 -
Include
Driver/DrvADC.h
Return Value
E_SUCCESS: Success. The compare function is enabled.
E_DRVADC_ARGUMENT: One of the input arguments is out of the range
Example
u8CmpChannelNum = 0;
u8CmpMatchCount = 5;
/* Enable ADC comparator1. Compare condition: conversion result < 0x800. */
DrvADC_EnableADCCmp1(u8CmpChannelNum, LESS_THAN, 0x800,
u8CmpMatchCount);
DrvADC_DisableADCCmp1
Prototype
void DrvADC_DisableADCCmp1 (void);
Description
Disable the ADC comparator 1.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Disable the ADC comparator 1 */
DrvADC_DisableADCCmp1();
DrvADC_EnableSelfCalibration
Prototype
void DrvADC_EnableSelfCalibration (void);
Description
Enable the self calibration function for minimizing the A/D conversion error. When chip
power on or software switches the ADC input type between single-end mode and differential
mode, user needs to call this function to enable the self calibration. After call this function,
V1.05.002
- 148 -
user can call DrvADC_IsCalibrationDone() to check if the self calibration is done before any
A/D conversion.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
None.
Example
/* Enable the self calibration function */
DrvADC_EnableSelfCalibration();
DrvADC_IsCalibrationDone
Prototype
uint32_t DrvADC_IsCalibrationDone (void);
Description
Check whether the self calibration action is finished or not.
Parameters
None.
Include
Driver/DrvADC.h
Return Value
TURE: the self calibration action is finished.
FALSE: the self calibration action is in progress.
Example
if( DrvADC_IsCalibrationDone() )
printf(Self calibration done.\n);
DrvADC_DisableSelfCalibration
Prototype
void DrvADC_DisableSelfCalibration (void);
Description
V1.05.002
- 149 -
DrvADC_DiffModeOutputFormat
Prototype
void DrvADC_DiffModeOutputFormat (
E_ADC_DIFF_MODE_OUTPUT_FORMAT OutputFormat
);
Description
Select the output format of differential input mode. Only NUC101 and low density version of
NuMicroTM NUC100 series products support this function. Please refer to NuMicroTM
NUC100 Series Products Selection Guide of Appendix.
Parameters
OutputFormat [in]
Specify the output format. It could be unsigned format (UNSIGNED_OUTPUT) or 2s
complement format (TWOS_COMPLEMENT.)
Include
Driver/DrvADC.h
Return Value
None
Example
/* 2s complement format */
DrvADC_DiffModeOutputFormat(TWOS_COMPLEMENT);
DrvADC_GetVersion
Prototype
V1.05.002
- 150 -
23:16
15:8
7:0
00000000
MAJOR_NUM
MINOR_NUM
BUILD_NUM
Example
printf("Driver version: %x\n", DrvADC_GetVersion());
V1.05.002
- 151 -
7.
SPI Driver
7.1. SPI Introduction
The Serial Peripheral Interface (SPI) is a synchronous serial data communication protocol which operates in full
duplex mode. Devices communicate in master/slave mode with 4-wire bi-direction interface. NuMicroTM
NUC100 series contain four sets of SPI controller performing a serial-to-parallel conversion on data received
from a peripheral device, and a parallel-to-serial conversion on data transmitted to a peripheral device. Each SPI
set can drive up to 2 external peripherals. It also can be driven as the slave device when the SLAVE bit
(CNTRL[18]) is set.
Each controller can generate an individual interrupt signal when data transfer is finished and can be cleared by
writing 1 to the respective interrupt flag. The active level of device/slave select signal can be programmed to
low active or high active on SSR[SS_LVL] bit, which depends on the connected peripheral. Writing a divisor
into DIVIDER register can program the frequency of serial clock output when it is as the master. If the
VARCLK_EN bit in SPI_CNTRL[23] is enabled, the serial clock can be set as two programmable frequencies
which are defined in DIVIDER and DIVIDER2. The format of the variable frequency is defined in VARCLK.
Each SPI controller contains two 32-bit transmission buffers (TX0 and TX1) and two reception buffers (RX0
and RX1), and can provide burst mode operation. It also supports variable length transfer.
The controller also supports two bits transfer mode which is defined in the SPI_CNTL[22]. When the TWOB bit,
in SPI_CNTL[22], is enabled, it can transmit and receive two bit serial data via the transmission/reception
buffers. The 1st bit channel transmits the data from TX0 and receives the data into RX0. The 2 nd bit channel
transmits the data from TX1 and receives the data into RX1.
In this document, we will introduce how to use the SPI driver.
V1.05.002
- 152 -
Value
Description
eDRVSPI_PORT0
SPI port 0
eDRVSPI_PORT1
SPI port 1
eDRVSPI_PORT2
SPI port 2
eDRVSPI_PORT3
SPI port 3
Enumeration Identifier
Value
Description
eDRVSPI_MASTER
Master mode
eDRVSPI_SLAVE
Slave mode
Enumeration Identifier
Value
Description
eDRVSPI_TYPE0
eDRVSPI_TYPE1
eDRVSPI_TYPE2
eDRVSPI_TYPE3
eDRVSPI_TYPE4
eDRVSPI_TYPE5
eDRVSPI_TYPE6
eDRVSPI_TYPE7
Enumeration Identifier
Value
Description
eDRVSPI_LSB_FIRST
eDRVSPI_MSB_FIRST
Value
Description
E_DRVSPI_MODE
E_DRVSPI_TRANS_TYPE
E_DRVSPI_ENDIAN
E_DRVSPI_BYTE_REORDER
Enumeration Identifier
eDRVSPI_BYTE_REORDER_SUSPEND_DISABLE 0
eDRVSPI_BYTE_REORDER_SUSPEND
- 153 -
Enumeration Identifier
Value
Description
eDRVSPI_BYTE_REORDER
eDRVSPI_BYTE_SUSPEND
Enumeration Identifier
Value
Description
eDRVSPI_EDGE_TRIGGER
Edge trigger
eDRVSPI_LEVEL_TRIGGER
Level trigger
Enumeration Identifier
Value
Description
eDRVSPI_ACTIVE_LOW_FALLING
eDRVSPI_ACTIVE_HIGH_RISING
0
1
Low-level/Falling-edge active
High-level/Rising-edge active
Enumeration Identifier
Value
Description
eDRVSPI_NONE
eDRVSPI_SS0
eDRVSPI_SS1
Select the 2
eDRVSPI_SS0_SS1
Enumeration Identifier
Value
Description
eDRVSPI_TX_DMA
eDRVSPI_RX_DMA
0
1
Enable Tx DMA
Enable Rx DMA
E_DRVSPI_SSLTRIG
E_DRVSPI_SS_ACT_TYPE
E_DRVSPI_SLAVE_SEL
st
nd
E_DRVSPI_DMA_MODE
V1.05.002
- 154 -
7.4. Functions
DrvSPI_Open
Prototype
int32_t DrvSPI_Open(
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_MODE eMode,
E_DRVSPI_TRANS_TYPE eType,
int32_t i32BitLength
);
Description
This function is used to open SPI module. It decides the SPI to work in master or slave mode,
SPI bus timing and bit length per transfer. The automatic slave select function will be
enabled.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eMode [in]
To work in Master (eDRVSPI_MASTER) or Slave (eDRVSPI_SLAVE) mode
eType [in]
Transfer types, i.e. the bus timing. It could be eDRVSPI_TYPE0~eDRVSPI_TYPE7.
eDRVSPI_TYPE0: the clock idle state is low; drive data at rising-edge of serial clock;
latch data at rising-edge of serial clock. Drive data and latch data at the same edge. Not
recommend to use this transfer type.
eDRVSPI_TYPE1: the clock idle state is low; drive data at falling-edge of serial clock;
latch data at rising-edge of serial clock.
eDRVSPI_TYPE2: the clock idle state is low; drive data at rising-edge of serial clock;
latch data at falling-edge of serial clock.
eDRVSPI_TYPE3: the clock idle state is low; drive data at falling-edge of serial clock;
latch data at falling-edge of serial clock. Drive data and latch data at the same edge. Not
recommend to use this transfer type.
V1.05.002
- 155 -
eDRVSPI_TYPE4: the clock idle state is high; drive data at rising-edge of serial clock;
latch data at rising-edge of serial clock. Drive data and latch data at the same edge. Not
recommend to use this transfer type.
eDRVSPI_TYPE5: the clock idle state is high; drive data at falling-edge of serial clock;
latch data at rising-edge of serial clock.
eDRVSPI_TYPE6: the clock idle state is high; drive data at rising-edge of serial clock;
latch data at falling-edge of serial clock.
eDRVSPI_TYPE7: the clock idle state is high; drive data at falling-edge of serial clock;
latch data at falling-edge of serial clock. Drive data and latch data at the same edge. Not
recommend to use this transfer type.
i32BitLength [in]
Bit length per transaction. The range is 1 ~32.
Include
Driver/DrvSPI.h
Return Value
E_SUCCESS : Success.
E_DRVSPI_ERR_INIT : The specified SPI port has been opened before.
E_DRVSPI_ERR_BIT_LENGTH : The bit length is out of range.
E_DRVSPI_ERR_BUSY : The specified SPI port is in busy status.
Example
/* Configure SPI0 as a master, 32-bit transaction, not QFN 36-pin package */
DrvSPI_Open(eDRVSPI_PORT0, eDRVSPI_MASTER, eDRVSPI_TYPE1, 32);
DrvSPI_Close
Prototype
void DrvSPI_Close (
E_DRVSPI_PORT eSpiPort
);
Description
Close the specified SPI module and disable the SPI interrupt.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
V1.05.002
- 156 -
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Close SPI0 */
DrvSPI_Close(eDRVSPI_PORT0);
DrvSPI_Set2BitTransferMode
Prototype
void DrvSPI_Set2BitTransferMode (
E_DRVSPI_PORT eSpiPort,
uint8_t bEnable
);
Description
Set 2-bit transfer mode.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
bEnable [in]
Enable (TRUE) / Disable (FALSE)
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Enable 2-bit transfer mode of SPI0 */
DrvSPI_Set2BitTransferMode(eDRVSPI_PORT0, TRUE);
V1.05.002
- 157 -
DrvSPI_SetEndian
Prototype
void DrvSPI_SetEndian (
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_ENDIAN eEndian
);
Description
This function is used to configure the bit order of each transaction.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eEndian [in]
Specify LSB first (eDRVSPI_LSB_FIRST) or MSB first (eDRVSPI_MSB_FIRST.)
Include
Driver/DrvSPI.h
Return Value
None
Example
/* The transfer order of SPI0 is LSB first */
DrvSPI_SetEndian(eDRVSPI_PORT0, eDRVSPI_LSB_FIRST);
DrvSPI_SetBitLength
Prototype
int32_t DrvSPI_SetBitLength(
E_DRVSPI_PORT eSpiPort,
int32_t i32BitLength
);
Description
This function is used to configure the bit length of SPI transfer.
V1.05.002
- 158 -
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
i32BitLength [in]
Specify the bit length. The range is 1~32 bits.
Include
Driver/DrvSPI.h
Return Value
E_SUCCESS : Success.
E_DRVSPI_ERR_BIT_LENGTH : The bit length is out of range.
Example
/* The transfer bit length of SPI0 is 8-bit */
DrvSPI_SetBitLength(eDRVSPI_PORT0, 8);
DrvSPI_SetByteReorder
Prototype
int32_t DrvSPI_SetByteReorder (
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_BYTE_REORDER eOption
);
Description
This function is used to enable/disable Byte Reorder function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eOption [in]
V1.05.002
- 159 -
The options of Byte Reorder function and Byte Suspend function. The Byte Suspend
function is only available in 32-bit transaction.
eDRVSPI_BYTE_REORDER_SUSPEND_DISABLE:
Both Byte Reorder function and Byte Suspend function are disabled.
eDRVSPI_BYTE_REORDER_SUSPEND:
Both Byte Reorder function and Byte Suspend function are enabled.
eDRVSPI_BYTE_REORDER:
Only enable the Byte Reorder function.
eDRVSPI_BYTE_SUSPEND:
Only enable the Byte Suspend function.
Include
Driver/DrvSPI.h
Return Value
E_SUCCESS : Success.
E_DRVSPI_ERR_BIT_LENGTH : The bit length MUST be 8/16/24/32.
Example
/* The transfer bit length of SPI0 is 32-bit */
DrvSPI_SetBitLength(eDRVSPI_PORT0, 32);
/* Enable the Byte Reorder function of SPI0 */
DrvSPI_SetByteReorder(eDRVSPI_PORT0, eDRVSPI_BYTE_REORDER);
DrvSPI_SetSuspendCycle
Prototype
int32_t DrvSPI_SetSuspendCycle (
E_DRVSPI_PORT eSpiPort,
int32_t i32Interval
);
Description
Set the number of clock cycle of the suspend interval. In slave mode, executing this function
is unmeaningful.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
V1.05.002
- 160 -
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
i32Interval [in]
In burst transfer mode, this value specified the delay clock number between successive
transactions. If the Byte Suspend function is enabled, it specified the delay clock number
among each byte. Please refer to TRM for the calculation of the suspend interval.
Include
Driver/DrvSPI.h
Return Value
E_SUCCESS : Success.
E_DRVSPI_ERR_SUSPEND_INTERVAL : The suspend interval setting is out of range.
Example
/* The suspend interval is 10 SPI clock cycles */
DrvSPI_SetSuspendCycle (eDRVSPI_PORT0, 10);
DrvSPI_SetTriggerMode
Prototype
void DrvSPI_SetTriggerMode (
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_SSLTRIG eSSTriggerMode
);
Description
Set the trigger mode of slave select pin. In master mode, executing this function is
functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eSSTriggerMode [in]
Specify the trigger mode.
eDRVSPI_EDGE_TRIGGER: edge trigger.
eDRVSPI_LEVEL_TRIGGER: level trigger.
V1.05.002
- 161 -
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Level trigger */
DrvSPI_SetTriggerMode(eDRVSPI_PORT0, eDRVSPI_LEVEL_TRIGGER);
DrvSPI_SetSlaveSelectActiveLevel
Prototype
void DrvSPI_SetSlaveSelectActiveLevel (
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_SS_ACT_TYPE eSSActType
);
Description
Set the active level of slave select.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eSSActType [in]
Select the active type of slave select pin.
eDRVSPI_ACTIVE_LOW_FALLING:
Slave select pin is active low in level-trigger mode; or falling-edge trigger in
edge-trigger mode.
eDRVSPI_ACTIVE_HIGH_RISING:
Slave select pin is active high in level-trigger mode; or rising-edge trigger in
edge-trigger mode.
Include
Driver/DrvSPI.h
Return Value
V1.05.002
- 162 -
None
Example
/* Configure the active level of SPI0 slave select pin */
DrvSPI_SetSlaveSelectActiveLevel(eDRVSPI_PORT0,
eDRVSPI_ACTIVE_LOW_FALLING);
DrvSPI_GetLevelTriggerStatus
Prototype
uint8_t DrvSPI_GetLevelTriggerStatus (
E_DRVSPI_PORT eSpiPort
);
Description
This function is used to get the level-trigger transmission status of slave device.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
TRUE: The transaction number and the transferred bit length met the specified requirements.
FALSE: The transaction number or the transferred bit length of one transaction doesn't meet
the specified requirements.
Example
/* Level trigger */
DrvSPI_SetTriggerMode(eDRVSPI_PORT0, eDRVSPI_LEVEL_TRIGGER);
V1.05.002
- 163 -
DrvSPI_EnableAutoSS
Prototype
void DrvSPI_EnableAutoSS (
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_SLAVE_SEL eSlaveSel
);
Description
This function is used to enable the automatic slave select function and select the slave select
pins. The automatic slave select means the SPI will set the slave select pin to active state
when transferring data and set the slave select pin to inactive state when one transfer is
finished. For some devices, the slave select pin may need to be kept at active state for many
transfers. User should disable the automatic slave select function and control the slave select
pin manually for these devices. In slave mode, executing this function is functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eSlaveSel [in]
Select the slave select pins which will be used.
eDRVSPI_NONE
eDRVSPI_SS0
eDRVSPI_SS1
V1.05.002
- 164 -
DrvSPI_DisableAutoSS
Prototype
void DrvSPI_DisableAutoSS (
E_DRVSPI_PORT eSpiPort
);
Description
This function is used to disable the automatic slave selection function. If user wants to keep
the slave select signal at active state during multiple words data transfer, user can disable the
automatic slave selection function and control the slave select signal manually. In slave mode,
executing this function is functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the automatic slave select function of SPI0 */
DrvSPI_DisableAutoSS(eDRVSPI_PORT0);
DrvSPI_SetSS
Prototype
void DrvSPI_SetSS(
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_SLAVE_SEL eSlaveSel
);
Description
Configure the slave select pins. In slave mode, executing this function is functionless.
Parameters
V1.05.002
- 165 -
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0 : SPI0
eDRVSPI_PORT1 : SPI1
eDRVSPI_PORT2 : SPI2
eDRVSPI_PORT3 : SPI3
eSlaveSel [in]
In automatic slave select operation, use this parameter to select the slave select pins
which will be used.
In manual slave select operation, the specified slave select pins will be set to active state.
It could be eDRVSPI_NONE, eDRVSPI_SS0, eDRVSPI_SS1 or eDRVSPI_SS0_SS1.
eDRVSPI_NONE : no slave was selected.
eDRVSPI_SS0 : the SS0 was selected.
eDRVSPI_SS1 : the SS1 was selected.
eDRVSPI_SS0_SS1 : both SS0 and SS1 were selected.
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the automatic slave select function of SPI0 */
DrvSPI_DisableAutoSS(eDRVSPI_PORT0);
/* Set the SS0 pin to active state */
DrvSPI_SetSS(eDRVSPI_PORT0, eDRVSPI_SS0);
DrvSPI_ClrSS
Prototype
void DrvSPI_ClrSS(
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_SLAVE_SEL eSlaveSel
);
Description
Set the specified slave select pins to inactive state. In slave mode, executing this function is
functionless.
Parameters
V1.05.002
- 166 -
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eSlaveSel [in]
Specify slave select pins.
eDRVSPI_NONE : no slave was selected.
eDRVSPI_SS0 : the SS0 was selected.
eDRVSPI_SS1 : the SS1 was selected.
eDRVSPI_SS0_SS1 : both SS0 and SS1 were selected.
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the automatic slave select function of SPI0 */
DrvSPI_DisableAutoSS(eDRVSPI_PORT0);
/* Set the SS0 pin to inactive state */
DrvSPI_ClrSS(eDRVSPI_PORT0, eDRVSPI_SS0);
DrvSPI_IsBusy
Prototype
uint8_t DrvSPI_IsBusy(
E_DRVSPI_PORT eSpiPort
);
Description
Check the busy status of the specified SPI port.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
V1.05.002
- 167 -
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
TURE: The SPI port is in busy.
FALSE: The SPI port is not in busy.
Example
/* set the GO_BUSY bit of SPI0 */
DrvSPI_SetGo(eDRVSPI_PORT0);
/* Check the busy status of SPI0 */
while( DrvSPI_IsBusy(eDRVSPI_PORT0) );
DrvSPI_BurstTransfer
Prototype
int32_t DrvSPI_BurstTransfer(
E_DRVSPI_PORT eSpiPort,
int32_t i32BurstCnt,
int32_t i32Interval
);
Description
Configure the burst transfer settings. If i32BurstCnt is set to 2, it performs burst transfer. SPI
controller will transfer two successive transactions. The suspend interval length between the
two transactions is determined by the value of i32Interval. In slave mode, the setting of
i32Interval is functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
i32BurstCnt [in]
Specify the transaction number in one transfer. It could be 1 or 2.
i32Interval [in]
V1.05.002
- 168 -
Suspend interval length. Specify the number of SPI clock cycle between successive
transactions. The range of this setting value is 2~17.
Include
Driver/DrvSPI.h
Return Value
E_SUCCESS : Success.
E_DRVSPI_ERR_BURST_CNT : The burst count is out of range.
E_DRVSPI_ERR_SUSPEND_INTERVAL : The interval is out of range.
Example
/* Configure the SPI0 burst transfer mode; two transactions in one transfer; 10 delay clocks
between the transactions. */
DrvSPI_BurstTransfer(eDRVSPI_PORT0, 2, 10);
DrvSPI_SetClockFreq
Prototype
uint32_t
DrvSPI_SetClockFreq(
E_DRVSPI_PORT eSpiPort,
uint32_t u32Clock1,
uint32_t u32Clock2
);
Description
Configure the frequency of SPI clock. In master mode, the output frequency of serial clock is
programmable. If the variable clock function is enabled, the output pattern of serial clock is
defined in VARCLK. If the bit pattern of VARCLK is 0, the output frequency of SPICLK
is equal to the frequency of variable clock 1. Otherwise, the output frequency is equal to the
frequency of variable clock 2. In slave mode, executing this function is functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
u32Clock1 [in]
V1.05.002
- 169 -
Specify the SPI clock rate in Hz. It's the clock rate of SPI engine clock and variable
clock 1.
u32Clock2 [in]
Specify the SPI clock rate in Hz. It's the clock rate of variable clock 2.
Include
Driver/DrvSPI.h
Driver/DrvSYS.h
Return Value
The actual clock rate of SPI engine clock is returned. The actual clock may different to the
target SPI clock due to hardware limitation.
Example
/* SPI0 clock rate of clock 1 is 2MHz; the clock rate of clock 2 is 1MHz */
DrvSPI_SetClockFreq(eDRVSPI_PORT0, 2000000, 1000000);
DrvSPI_GetClock1Freq
Prototype
uint32_t
DrvSPI_GetClock1Freq(
E_DRVSPI_PORT eSpiPort
);
Description
Get the SPI engine clock rate in Hz. In slave mode, executing this function is functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Driver/DrvSYS.h
Return Value
The frequency of SPI bus engine clock. The unit is Hz.
V1.05.002
- 170 -
Example
/* Get the engine clock rate of SPI0 */
printf("SPI clock rate: %d Hz\n", DrvSPI_GetClock1Freq(eDRVSPI_PORT0));
DrvSPI_GetClock2Freq
Prototype
uint32_t
DrvSPI_GetClock2Freq(
E_DRVSPI_PORT eSpiPort
);
Description
Get the clock rate of variable clock 2 in Hz. In slave mode, executing this function is
functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Driver/DrvSYS.h
Return Value
The frequency of variable clock 2. The unit is Hz.
Example
/* Get the clock rate of SPI0 variable clock 2 */
printf("SPI clock rate of variable clock 2: %d Hz\n",
DrvSPI_GetClock2Freq(eDRVSPI_PORT0));
DrvSPI_SetVariableClockFunction
Prototype
void
DrvSPI_SetVariableClockFunction (
E_DRVSPI_PORT eSpiPort,
V1.05.002
- 171 -
uint8_t bEnable,
uint32_t u32Pattern
);
Description
Set the variable clock function. The output pattern of serial clock is defined in VARCLK
register. A two-bit combination in the VARCLK defines the pattern of one serial clock cycle.
The bit field VARCLK[31:30] defines the first clock cycle of SPICLK. The bit field
VARCLK[29:28] defines the second clock cycle of SPICLK and so on. The following figure
is the timing relationship among the serial clock (SPICLK), the VARCLK register and the
variable clock sources.
If the bit pattern of VARCLK is 0, the output frequency of SPICLK is equal to the
frequency of variable clock 1.
If the bit pattern of VARCLK is 1, the output frequency of SPICLK is equal to the
frequency of variable clock 2.
SPICLK
VARCLK
00000000011111111111111110000111
Variable clock 1
Variable clock 2
Note that when enable the variable clock function, the setting of transfer bit length must be
programmed as 0x10 (16 bits mode) only.
In slave mode, executing this function is functionless.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
bEnable [in]
Enable (TRUE) / Disable (FALSE)
u32Pattern [in]
Specify the variable clock pattern. If bEnable is set to 0, this setting is functionless.
Include
Driver/DrvSPI.h
V1.05.002
- 172 -
Return Value
None.
Example
/* Enable the SPI0 variable clock function and set the variable clock pattern */
DrvSPI_SetVariableClockFunction(eDRVSPI_PORT0, TRUE, 0x007FFF87);
DrvSPI_EnableInt
Prototype
void DrvSPI_EnableInt(
E_DRVSPI_PORT eSpiPort,
PFN_DRVSPI_CALLBACK pfnCallback,
uint32_t u32UserData
);
Description
Enable the SPI interrupt of the specified SPI port and install the callback function.
Parameters
u16Port [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pfnCallback [in]
The callback function of the corresponding SPI interrupt.
u32UserData [in]
The parameter which will be passed to the callback function.
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Enable the SPI0 interrupt and install the callback function. The parameter 0 will be passed
to the callback function. */
DrvSPI_EnableInt(eDRVSPI_PORT0, SPI0_Callback, 0);
V1.05.002
- 173 -
DrvSPI_DisableInt
Prototype
void DrvSPI_DisableInt(
E_DRVSPI_PORT eSpiPort
);
Description
Disable the SPI interrupt.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the SPI0 interrupt */
DrvSPI_DisableInt(eDRVSPI_PORT0);
DrvSPI_GetIntFlag
Prototype
uint32_t DrvSPI_GetIntFlag (
E_DRVSPI_PORT eSpiPort
);
Description
Get the SPI interrupt flag.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
V1.05.002
- 174 -
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
0: the SPI interrupt does not occur.
1: the SPI interrupt occurs.
Example
/* Get the SPI0 interrupt flag */
DrvSPI_GetIntFlag(eDRVSPI_PORT0);
DrvSPI_ClrIntFlag
Prototype
void DrvSPI_ClrIntFlag (
E_DRVSPI_PORT eSpiPort
);
Description
Clear the SPI interrupt flag.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None.
Example
/* Clear the SPI0 interrupt flag */
DrvSPI_ClrIntFlag(eDRVSPI_PORT0);
V1.05.002
- 175 -
DrvSPI_SingleRead
Prototype
uint8_t DrvSPI_SingleRead(
E_DRVSPI_PORT eSpiPort,
uint32_t *pu32Data
);
Description
Read data from SPI RX registers and trigger SPI for next transfer.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pu32Data [out]
A buffer pointer. This buffer is used for storing the data got from the SPI bus.
Include
Driver/DrvSPI.h
Return Value
TRUE: The data stored in pu32Data is valid.
FALSE: The data stored in pu32Data is invalid.
Example
/* Read the previous retrieved data and trigger next transfer. */
uint32_t u32DestinationData;
DrvSPI_SingleRead(eDRVSPI_PORT0, &u32DestinationData);
DrvSPI_SingleWrite
Prototype
uint8_t DrvSPI_SingleWrite (
E_DRVSPI_PORT eSpiPort,
uint32_t *pu32Data
);
Description
Write data to SPI TX0 register and trigger SPI to start transfer.
V1.05.002
- 176 -
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pu32Data [in]
A buffer pointer. The data stored in this buffer will be transmitted through the SPI bus.
Include
Driver/DrvSPI.h
Return Value
TRUE: The data stored in pu32Data has been transferred.
FALSE: The SPI is in busy. The data stored in pu32Data has not been transferred.
Example
/* Write the data stored in u32SourceData to TX buffer of SPI0 and trigger SPI to start
transfer. */
uint32_t u32SourceData;
DrvSPI_SingleWrite(eDRVSPI_PORT0, &u32SourceData);
DrvSPI_BurstRead
Prototype
uint8_t DrvSPI_BurstRead (
E_DRVSPI_PORT eSpiPort,
uint32_t *pu32Buf
);
Description
Read two words of data from SPI RX registers and then trigger SPI for next transfer.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pu32Buf [out]
V1.05.002
- 177 -
A buffer pointer. This buffer is used for storing the data got from the SPI bus.
Include
Driver/DrvSPI.h
Return Value
TRUE: The data stored in pu32Buf is valid.
FALSE: The data stored in pu32Buf is invalid.
Example
/* Read two words of data from SPI0 RX registers to au32DestinationData[u32DataCount]
and au32DestinationData[u32DataCount+1]. And then trigger SPI for next transfer. */
DrvSPI_BurstRead(eDRVSPI_PORT0, &au32DestinationData[u32DataCount]);
DrvSPI_BurstWrite
Prototype
uint8_t DrvSPI_BurstWrite (
E_DRVSPI_PORT eSpiPort,
uint32_t *pu32Buf
);
Description
Write two words of data to SPI TX registers and then trigger SPI to start a transfer.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pu32Buf [in]
A buffer pointer. The data stored in this buffer will be transmitted through the SPI bus.
Include
Driver/DrvSPI.h
Return Value
TRUE: The data stored in pu32Buf has been transferred.
FALSE: The SPI is in busy. The data stored in pu32Buf has not been transferred.
Example
V1.05.002
- 178 -
DrvSPI_DumpRxRegister
Prototype
uint32_t
DrvSPI_DumpRxRegister (
E_DRVSPI_PORT eSpiPort,
uint32_t *pu32Buf,
uint32_t u32DataCount
);
Description
Read data from RX registers. This function will not trigger a SPI data transfer.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pu32Buf [out]
A buffer pointer. This buffer is used for storing the data got from the SPI RX registers.
u32DataCount [in]
The count of data read from RX registers. The maximum number is 2.
Include
Driver/DrvSPI.h
Return Value
The count of data actually read from Rx registers.
Example
/* Read one word of data from SPI0 RX buffer and store to
au32DestinationData[u32DataCount] */
DrvSPI_DumpRxRegister(eDRVSPI_PORT0, &au32DestinationData[u32DataCount], 1);
DrvSPI_SetTxRegister
Prototype
V1.05.002
- 179 -
uint32_t
DrvSPI_SetTxRegister (
E_DRVSPI_PORT eSpiPort,
uint32_t *pu32Buf,
uint32_t u32DataCount
);
Description
Write data to TX registers. This function will not trigger a SPI data transfer.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pu32Buf [in]
A buffer stores the data which will be written to TX registers.
u32DataCount [in]
The count of data written to TX registers.
Include
Driver/DrvSPI.h
Return Value
The count of data actually written to SPI TX registers.
Example
/* Write one word of data stored in u32Buffer to SPI0 TX register. */
DrvSPI_SetTxRegister(eDRVSPI_PORT0, &u32Buffer, 1);
DrvSPI_SetGo
Prototype
void DrvSPI_SetGo (
E_DRVSPI_PORT eSpiPort
);
Description
In master mode, call this function can start a SPI data transfer. In slave mode, executing this
function means that the slave is ready to communicate with a master.
Parameters
V1.05.002
- 180 -
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Trigger a SPI data transfer */
DrvSPI_SetGo(eDRVSPI_PORT0);
DrvSPI_ClrGo
Prototype
void DrvSPI_ClrGo (
E_DRVSPI_PORT eSpiPort
);
Description
Stop a SPI data trasfer.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Stop a SPI data transfer */
V1.05.002
- 181 -
DrvSPI_ClrGo(eDRVSPI_PORT0);
DrvSPI_SetPDMA
Prototype
void DrvSPI_SetPDMA (
E_DRVSPI_PORT eSpiPort,
E_DRVSPI_DMA_MODE eDmaMode,
uint8_t bEnable
);
Description
Configure the DMA settings.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
eDmaMode [in]
Specify the DMA mode.
eDRVSPI_TX_DMA: DMA-Transmitting
eDRVSPI_RX_DMA: DMA-Receiving
eEnable [in]
True: Enable DMA.
False: Disable DMA.
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Enable the SPI0 DMA-Receiving function */
DrvSPI_SetPDMA(eDRVSPI_PORT0, eDRVSPI_RX_DMA, TRUE);
V1.05.002
- 182 -
DrvSPI_EnableDivOne
Prototype
void DrvSPI_EnableDivOne (
E_DRVSPI_PORT eSpiPort
);
Description
Enable the DIV_ONE feature. The SPI clock rate will be equal to system clock rate. Only the
chips with the part number NUC1x0xxxCx, ex: NUC140VE3CN, can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Enable the DIV_ONE feature.of SPI0 */
DrvSPI_EnableDivOne (eDRVSPI_PORT0);
DrvSPI_DisableDivOne
Prototype
void DrvSPI_DisableDivOne (
E_DRVSPI_PORT eSpiPort
);
Description
Disable the DIV_ONE feature. Only the chips with the part number NUC1x0xxxCx, ex:
NUC140VE3CN, can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
V1.05.002
- 183 -
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the DIV_ONE feature.of SPI0 */
DrvSPI_DisableDivOne (eDRVSPI_PORT0);
DrvSPI_Enable3Wire
Prototype
void DrvSPI_Enable3Wire (
E_DRVSPI_PORT eSpiPort
);
Description
Enable the SPI 3-wire function. In master mode, executing this function is unmeaningful.
Only the chips with the part number NUC1x0xxxCx, ex: NUC140VE3CN, can support this
function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Enable the 3-wire SPI function.of SPI0 */
DrvSPI_Enable3Wire (eDRVSPI_PORT0);
V1.05.002
- 184 -
DrvSPI_Disable3Wire
Prototype
void DrvSPI_Disable3Wire (
E_DRVSPI_PORT eSpiPort
);
Description
Disable the SPI 3-wire function. Only the chips with the part number NUC1x0xxxCx, ex:
NUC140VE3CN, can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the 3-wire SPI function.of SPI0 */
DrvSPI_Disable3Wire (eDRVSPI_PORT0);
DrvSPI_3WireAbort
Prototype
void DrvSPI_3WireAbort (
E_DRVSPI_PORT eSpiPort
);
Description
Abort transfer when using 3-wire SPI. If using 3-wire SPI as slave, when slave start interrupt
status is set but transfer done flag doesn't be set over a reasonable time, use this function to
abort this transfer. Only the chips with the part number NUC1x0xxxCx, ex: NUC140VE3CN,
can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
V1.05.002
- 185 -
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Abort current transfer.of SPI0 */
DrvSPI_ 3WireAbort (eDRVSPI_PORT0);
DrvSPI_Enable3WireStartInt
Prototype
void DrvSPI_Enable3WireStartInt (
E_DRVSPI_PORT eSpiPort,
PFN_DRVSPI_CALLBACK pfnCallback,
uint32_t u32UserData
);
Description
Enable the 3-wire SPI start interrupt of the specified SPI port and install the callback function.
Only the chips with the part number NUC1x0xxxCx, ex: NUC140VE3CN, can support this
function.
Parameters
u16Port [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
pfnCallback [in]
The callback function of the corresponding SPI interrupt.
u32UserData [in]
The parameter which will be passed to the callback function.
V1.05.002
- 186 -
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Enable the 3-wire SPI0 start interrupt and install the callback function. The parameter 0
will be passed to the callback function. */
DrvSPI_Enable3WireStartInt (eDRVSPI_PORT0, SPI0_Callback, 0);
DrvSPI_Disable3WireStartInt
Prototype
void DrvSPI_Disable3WireStartInt (
E_DRVSPI_PORT eSpiPort
);
Description
Disable the 3-wire SPI start interrupt. Only the chips with the part number NUC1x0xxxCx,
ex: NUC140VE3CN, can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None
Example
/* Disable the 3-wire SPI0 start interrupt */
DrvSPI_Disable3WireStartInt (eDRVSPI_PORT0);
DrvSPI_Get3WireStartIntFlag
Prototype
V1.05.002
- 187 -
uint32_t DrvSPI_Get3WireStartIntFlag (
E_DRVSPI_PORT eSpiPort
);
Description
Get the 3-wire SPI start interrupt status. Only the chips with the part number NUC1x0xxxCx,
ex: NUC140VE3CN, can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
0: the SPI start interrupt doesn't occur.
1: the SPI start interrupt occurs.
Example
/* Get the 3-wire SPI0 start interrupt flag */
DrvSPI_Get3WireStartIntFlag (eDRVSPI_PORT0);
DrvSPI_Clr3WireStartIntFlag
Prototype
void DrvSPI_Clr3WireStartIntFlag (
E_DRVSPI_PORT eSpiPort
);
Description
Clear the 3-wire SPI start interrupt status. Only the chips with the part number
NUC1x0xxxCx, ex: NUC140VE3CN, can support this function.
Parameters
eSpiPort [in]
Specify the SPI port.
eDRVSPI_PORT0
: SPI0
V1.05.002
- 188 -
eDRVSPI_PORT1
: SPI1
eDRVSPI_PORT2
: SPI2
eDRVSPI_PORT3
: SPI3
Include
Driver/DrvSPI.h
Return Value
None.
Example
/* Clear the 3-wire SPI0 start interrupt flag */
DrvSPI_Clr3WireStartIntFlag (eDRVSPI_PORT0);
DrvSPI_GetVersion
Prototype
uint32_t
DrvSPI_GetVersion (void);
Description
Get the version number of SPI driver.
Include
Driver/DrvSPI.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
printf("Driver version: %x\n", DrvSPI_GetVersion());
V1.05.002
- 189 -
8. I2C Driver
8.1. I2C Introduction
I2C is bi-directional serial bus with two wires that provides a simple and efficient method of data exchange
between devices. The I2C standard is a true multi-master bus including collision detection and arbitration
that prevents data corruption if two or more masters attempt to control the bus simultaneously. Serial, 8-bit
oriented bi-directional data transfers can be made up 1.0 Mbps.
For NuMicro NUC100 Series, I2C device could act as master or slave and I2C driver can help user to use
I2C functions easily.
Value
Description
I2C_PORT0
I2C_PORT1
0
1
I2C port 0
I2C port 1
E_I2C_CALLBACK_TYPE
Enumeration identifier
Value
Description
I2CFUNC
ARBITLOSS
BUSERROR
TIMEOUT
2
3
8.4. Functions
DrvI2C_Open
Prototype
int32_t
Description
To open the I2C hardware and configure the I2C bus clock. The maximum of I2C bus clock
is 1MHz.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
u32BusClock [in]
To configure I2C bus clock. The unit is Hz.
Include
Driver/DrvI2C.h
Return Value
0
Succeed
Example
/* Enable I2C0 and set I2C0 bus clock 100 KHz */
DrvI2C_Open (I2C_PORT0, 100000);
DrvI2C_Close
Prototype
int32_t
Description
To close the I2C hardware.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
V1.05.002
- 191 -
Driver/DrvI2C.h
Return Value
0
Succeed
Example
DrvI2C_Close (I2C_PORT0); /* Disable I2C0 */
DrvI2C_SetClockFreq
Prototype
int32_t
Description
To configure the I2C bus clock. I2C bus clock = I2C source clock / (4 x (I2CCLK_DIV+1)).
The maximum of I2C bus clock is 1MHz.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
u32BusClock [in]
To configure I2C bus clock. The unit is Hz.
Include
Driver/DrvI2C.h
Return Value
0
Succeed
Example
/* Set I2C0 bus clock 200 KHz */
DrvI2C_SetClockFreq (I2C_PORT0, 200000);
DrvI2C_GetClockFreq
Prototype
uint32_t
Description
To get the I2C bus clock. I2C bus clock = I2C source clock / (4 x (I2CCLK_DIV+1))
Parameter
port [in]
V1.05.002
- 192 -
u32clock;
DrvI2C_SetAddress
Prototype
int32_t
DrvI2C_SetAddress (E_I2C_PORT port, uint8_t slaveNo, uint8_t slave_addr,
uint8_t GC_Flag);
Description
To set 7-bit physical slave address to the specified I2C slave address. Four slave addresses
supported. The setting takes effect when I2C operates as slave mode.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
slaveNo [in]
To select slave address. The slaveNo is 0 ~ 3.
slave_addr [in]
To set 7-bit physical slave address for selected slave address.
GC_Flag [in]
To enable or disable general call function. (1: enable, 0: disable)
Include
Driver/DrvI2C.h
Return Value
0: Succeed
<0: Failed
Example
DrvI2C_SetAddress(I2C_PORT0, 0, 0x15, 0);
DrvI2C_SetAddress(I2C_PORT0, 1, 0x35, 0);
DrvI2C_SetAddress(I2C_PORT0, 2, 0x55, 0);
DrvI2C_SetAddress(I2C_PORT0, 3, 0x75, 0);
- 193 -
DrvI2C_SetAddressMask
Prototype
int32_t
DrvI2C_SetAddressMask (E_I2C_PORT port, uint8_t slaveNo, uint8_t
slaveAddrMask);
Description
To set 7-bit physical slave address mask to the specified I2C salve address mask. Four slave
address masks supported. The setting takes effect when I2C operates as slave mode.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
slaveNo [in]
To select slave address mask. The value is 0 ~ 3.
slaveAddrMask [in]
To set 7-bit physical slave address mask for selected slave address mask. The
corresponding address bit is Dont care.
Include
Driver/DrvI2C.h
Return Value
0: Succeed
<0: Failed
Example
DrvI2C_SetAddress (I2C_PORT0, 0, 0x15, 0);
/* Set I2C0 1st slave address mask 0x01, slave address 0x15 and 0x14 would be addressed */
DrvI2C_SetAddressMask (I2C_PORT0, 0, 0x01);
/* Set I2C0 2nd slave address mask 0x04, slave address 0x35 and 0x31 would be addressed
*/
DrvI2C_SetAddressMask (I2C_PORT0, 1, 0x04);
DrvI2C_GetStatus
Prototype
uint32_t DrvI2C_GetStatus (E_I2C_PORT port);
Description
To get the I2C status code. There are 26 status codes. Please refer to Data Transfer Flow in
I2C Section of TRM in details.
V1.05.002
- 194 -
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
Driver/DrvI2C.h
Return Value
I2C status code
Example
uint32_t
u32status;
DrvI2C_WriteData
Prototype
void
Description
To set a byte of data to be sent.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
u8data [in]
Byte data.
Include
Driver/DrvI2C.h
Return Value
None
Example
DrvI2C_WriteData (I2C_PORT0, 0x55); /* Set byte data 0x55 into I2C0 data register */
DrvI2C_ReadData
Prototype
uint8_t
DrvI2C_ReadData(E_I2C_PORT port);
Description
V1.05.002
- 195 -
u8data;
u8data = DrvI2C_ReadData (I2C_PORT0); /* Read out byte data from I2C0 data register */
DrvI2C_Ctrl
Prototype
void DrvI2C_Ctrl(E_I2C_PORT port, uint8_t start, uint8_t stop, uint8_t intFlag, uint8_t ack);
Description
To set I2C control bit include STA, STO, AA, SI in control register.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
start [in]
To set STA bit or not. (1: set, 0: dont set). If the STA bit is set, a START or repeat
START signal will be generated when I2C bus is free.
stop [in]
To set STO bit or not. (1: set, 0: dont set). If the STO bit is set, a STOP signal will be
generated. When a STOP condition is detected, this bit will be cleared by hardward
automatically.
intFlag [in]
To clear SI flag (I2C interrupt flag). (1: clear, 0: dont work)
ack [in]
To enable AA bit (Assert Acknowledge control bit) or not. (1: enable, 0: disable)
Include
Driver/DrvI2C.h
Return Value
V1.05.002
- 196 -
None
Example
DrvI2C_Ctrl (I2C_PORT0, 0, 0, 1, 0); /* Set I2C0 SI bit to clear SI flag */
DrvI2C_Ctrl (I2C_PORT0, 1, 0, 0, 0); /* Set I2C0 STA bit to send START signal */
DrvI2C_GetIntFlag
Prototype
uint8_t
DrvI2C_GetIntFlag(E_I2C_PORT port);
Description
To get I2C interrupt flag status.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
Driver/DrvI2C.h
Return Value
Interrupt status (1 or 0)
Example
uint8_t
u8flagStatus;
DrvI2C_ClearIntFlag
Prototype
void
Description
To clear I2C interrupt flag if the flag is set 1.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
Driver/DrvI2C.h
Return Value
V1.05.002
- 197 -
None
Example
DrvI2C_ClearIntFlag (I2C_PORT0); /* Clear I2C0 interrupt flag (SI) */
DrvI2C_EnableInt
Prototype
int32_t
Description
To enable I2C interrupt function.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
Driver/DrvI2C.h
Return Value
0
Succeed
Example
DrvI2C_EnableInt (I2C_PORT0); /* Enable I2C0 interrupt */
DrvI2C_DisableInt
Prototype
int32_t
Description
To disable I2C interrupt function.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
Driver/DrvI2C.h
Return Value
0
Succeed
Example
V1.05.002
- 198 -
DrvI2C_InstallCallBack
Prototype
int32_t
DrvI2C_InstallCallBack (E_I2C_PORT port, E_I2C_CALLBACK_TYPE Type,
I2C_CALLBACK callbackfn);
Description
To install I2C call back function in I2C interrupt handler.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Type [in]
There are four types for call back function. (I2CFUNC / ARBITLOSS / BUSERROR /
TIMEOUT)
I2CFUNC: For normal I2C condition
ARBITLOSS: For master mode when arbitration loss occurs. The status code is 0x38.
BUSERROR: For bus error condition. The status code is 0x00.
TIMEOUT: For 14-bit time-out counter overflow.
callbackfn [in]
Call back function name for specified interrupt event.
Include
Driver/DrvI2C.h
Return Value
0: Succeed
<0: Failed
Example
/* Install I2C0 call back function I2C0_Callback_Normal for I2C normal condition */
DrvI2C_InstallCallback (I2C_PORT0, I2CFUNC, I2C0_Callback_Normal);
/* Install I2C0 call back function I2C0_Callback_BusErr for Bus Error condition */
DrvI2C_InstallCallback (I2C_PORT0, BUSERROR, I2C0_Callback_BusErr);
DrvI2C_UninstallCallBack
Prototype
int32_t
Type);
V1.05.002
- 199 -
Description
To uninstall I2C call back function in I2C interrupt handler.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Type [in]
There are four types for call back function. (I2CFUNC / ARBITLOSS / BUSERROR /
TIMEOUT)
I2CFUNC: For normal I2C condition
ARBITLOSS: For master mode when arbitration loss occurs. The status code is 0x38.
BUSERROR: For bus error condition. The status code is 0x00.
TIMEOUT: For 14-bit time-out counter overflow.
Include
Driver/DrvI2C.h
Return Value
0: Succeed
<0: Failed
Example
/* Uninstall I2C0 call back function for I2C normal condition */
DrvI2C_UninstallCallBack (I2C_PORT0, I2CFUNC);
/* Uninstall I2C0 call back function for Bus Error condition */
DrvI2C_UninstallCallBack (I2C_PORT0, BUSERROR);
DrvI2C_SetTimeoutCounter
Prototype
int32_t
DrvI2C_SetTimeoutCounter (E_I2C_PORT port, int32_t i32enable, uint8_t
u8div4);
Description
To configure 14-bit time-out counter.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
i32enable [in]
To enable or disable 14-bit time-out counter. (1: enable, 0: disable)
V1.05.002
- 200 -
u8div4 [in]
1: Enable DIV4 function. The source clock of the time-out counter is equal to HCLK / 4
when the time-out counter is enabled.
0: Disable DIV4 function. The source clock of the time-out counter is from HCLK when
the time-out counter is enabled.
Include
Driver/DrvI2C.h
Return Value
0
Succeed
Example
/* Enable I2C0 14-bit timeout counter and disable its DIV4 function */
DrvI2C_EnableTimeoutCount (I2C_PORT0, 1, 0);
DrvI2C_ClearTimeoutFlag
Prototype
void
Description
To clear I2C TIF flag if the flag is set 1.
Parameter
port [in]
Specify I2C interface. (I2C_PORT0 / I2C_PORT1)
Include
Driver/DrvI2C.h
Return Value
None
Example
DrvI2C_ClearTimeoutFlag (I2C_PORT0); /* Clear I2C0 TIF flag */
DrvI2C_GetVersion
Prototype
uint32_t DrvI2C_GetVersion (void);
Description
Get this modules version.
V1.05.002
- 201 -
Parameter
None
Include
Driver/DrvI2C.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
V1.05.002
- 202 -
9. RTC Driver
9.1. RTC Introduction
Real Time Clock (RTC) unit provides user the real time and calendar message .The RTC uses a
32.768 KHz external crystal. A built in RTC is designed to generate the periodic interrupt signal. The
period can be 1/128, 1/64, 1/32, 1/16, 1/8, 1/4, 1/2 and 1 second. And the RTC controller supports
periodic Time Tick and Alarm Match interruptst.
There is a time counter (second, minute, hour) and calendar counter (day, month, year) for
user to check the time.
12-hour or 24-hour mode is selectable.
Leap year compensation automatically.
Day of week counter.
Frequency compensate register.
All time and calendar message is expressed in BCD code.
Support periodic time tick interrupt with 8 period options 1/128, 1/64, 1/32, 1/16, 1/8, 1/4, 1/2 and 1
second.
Support RTC Time Tick and Alarm Match interrupt
Support wake-up chip from power down mode by RTC Time Tick or Alarm Match interrupt.
V1.05.002
- 203 -
Constant Definition
Constant Name
Value
Description
DRVRTC_INIT_KEY
DRVRTC_WRITE_KEY
0xA965
DRVRTC_CLOCK_12
12-Hour mode
DRVRTC_CLOCK_24
24-Hour mode
DRVRTC_AM
a.m.
DRVRTC_PM
p.m.
DRVRTC_YEAR2000
2000
DRVRTC_FCR_REFERENCE
32761
Value
Description
DRVRTC_ALARM_INT
DRVRTC_TICK_INT
DRVRTC_ALL_INT
Enumeration identifier
Value
Description
DRVRTC_TICK_1_SEC
DRVRTC_TICK_1_2_SEC
DRVRTC_TICK_1_4_SEC
DRVRTC_TICK_1_8_SEC
DRVRTC_TICK_1_16_SEC
DRVRTC_TICK_1_32_SEC
DRVRTC_TICK_1_64_SEC
DRVRTC_TICK_1_128_SEC
Enumeration identifier
Value
Description
DRVRTC_CURRENT_TIME
E_DRVRTC_TICK
E_DRVRTC_TIME_SELECT
V1.05.002
- 204 -
DRVRTC_ALARM_TIME
Enumeration identifier
Value
Description
DRVRTC_SUNDAY
DRVRTC_MONDAY
DRVRTC_TUESDAY
DRVRTC_WEDNESDAY
DRVRTC_THURSDAY
DRVRTC_FRIDAY
DRVRTC_SATURDAY
E_DRVRTC_DWR_PARAMETER
9.4. Functions
DrvRTC_SetFrequencyCompensation
Prototype
int32_t
DrvRTC_SetFrequencyCompensation (
int32_t i32FrequencyX100
);
Description
Set Frequency Compensation Data
Parameter
i32FrequencyX100 [in]
Specify the RTC clock X100, ex: 3277365 means 32773.65.
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_FCR_VALUE: Wrong Compensation value
Example
/* If the measured RTC crystal frequency is 32773.65Hz.*/
V1.05.002
- 205 -
DrvRTC_SetFrequencyCompensation (3277365) ;
DrvRTC_IsLeapYear
Prototype
int32_t
DrvRTC_IsLeapYear (void);
Description
According to current time , return this year is leap year or not.
Parameter
None.
Include
Driver/DrvRTC.h
Return Value
1: This year is a leap year.
0: This year is not a leap year.
Example
If (DrvRTC_IsLeapYear())
printf(This is Leap year!);
else
printf(This is not Leap year!);
DrvRTC_GetIntTick
Prototype
int32_t DrvRTC_GetIntTick (void);
Description
The function is used to get current Software tick count after enable tick interrupt.
Parameter
None.
Include
Driver/DrvRTC.h
Return Value
Software Tick Count in tick interrupt
V1.05.002
- 206 -
Example
/* Polling the tick count to wait 3 sec.*/
DrvRTC_SetTickMode(DRVRTC_TICK_1_2_SEC) ;
DrvRTC_EnableInt(DRVRTC_TICK_INT, NULL);
While (DrvRTC_GetTick() < 6);
printf(Pass though 3 sec\n)
DrvRTC_ResetIntTick
Prototype
void DrvRTC_ResetTick (void);
Description
The function is used to reset the tick count counting in interrupt.
Parameter
None.
Include
Driver/DrvRTC.h
Return Value
None
Example
DrvRTC_ResetTick ();
DrvRTC_WriteEnable
Prototype
int32_t
DrvRTC_WriteEnable (void);
Description
Access Password to AER to make access other register enable
Parameter
None.
Include
Driver/DrvRTC.h
Return Value
V1.05.002
- 207 -
E_SUCCESS: Success
E_DRVRTC_ERR_FAILED : Failed.
Note
After write a password to AER register, FCR / TAR / CAR / TTR register can be written or
read. And after 512 RTC clocks(about 15ms), access enable wiil auto-clear.
Example
/* Before you want to set the value in FCR / TAR / CAR / TTR register, using the function to
open access account. */
DrvRTC_WriteEnable ();
DrvRTC_Init
Prototype
int32_t DrvRTC_Init (void);
Description
Initial RTC. It consists of clear callback function pointer, enable 32K clock and RTC clock
and write initial key to let RTC start count.
Parameter
None.
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_EIO : Initial RTC Failed.
Example
/*In the beginning, call the function to initial RTC */
DrvRTC_Init() ;
DrvRTC_SetTickMode
Prototype
int32_t DrvRTC_SetTickMode(uint8_t ucMode);
Description
The function is used to set time tick period for periodic time tick Interrupt.
V1.05.002
- 208 -
Parameter
ucMode [in]
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_EIO: Access Enable failed
E_DRVRTC_ERR_ENOTTY: Parameter is wrong
Example
/* Set Tick interrupt is 128 tick/sec */
DrvRTC_SetTickMode (DRVRTC_TICK_1_128_SEC) ;
DrvRTC_EnableInt
Prototype
int32_t DrvRTC_EnableInt (
DRVRTC_INT_SOURCE str_IntSrc,
PFN_DRVRTC_CALLBACK pfncallback);
Description
The function is used to enable specified interrupt and install callback function. .
Parameter
str_IntSrc [in]
pfncallback [in]
: Alarm interrupt
DRVRTC_TICK_INT
: Tick interrupt
DRVRTC_ALL_INT
V1.05.002
- 209 -
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_ENOTTY: Parameter is wrong
Example
/* Enable tick interrupt and install callback function RTC_TickCallBackfn.*/
DrvRTC_EnableInt(DRVRTC_TICK_INT, RTC_TickCallBackfn);
DrvRTC_DisableInt
Prototype
int32_t
DrvRTC_DisableInt (
DRVRTC_INT_SOURCE str_IntSrc);
Description
The function is used to disable specified interrupt and remove callback function.
Parameter
str_IntSrc [in]
: Alarm interrupt
DRVRTC_TICK_INT
: Tick interrupt
DRVRTC_ALL_INT
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_ENOTTY: Parameter is wrong
Example
/* Disable tick and alarm interrupt*/
DrvRTC_DisableInt(DRVRTC_ALL_INT);
V1.05.002
- 210 -
DrvRTC_Open
Prototype
int32_t
DrvRTC_Open (
S_DRVRTC_TIME_DATA_T *sPt
);
Description
Set Current time (Year/Month/Day, Hour/Minute/Sec and day of week)
Parameter
*sPt [in]
Specify the time property and current time. It includes
u8cClockDisplay
u8cAmPm
u32cSecond
u32cMinute
u32cHour
u32cDayOfWeek
u32cDay
u32cMonth
u32Year
u8IsEnableWakeUp
: DRVRTC_CLOCK_12 / DRVRTC_CLOCK_24
: DRVRTC_AM / DRVRTC_PM
: Second value
: Minute value
: Hour value
: Day of week
: Day value
: Month value
: Year value
: Enable or not Wakeup function when time alarm happen
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_EIO: Initial RTC Failed.
Example
/* Start RTC count from 2009.Jan.19, 13:20:00 . */
S_DRVRTC_TIME_DATA_T sInitTime;
sInitTime.u32Year
= 2009;
sInitTime.u32cMonth
= 1;
sInitTime.u32cDay
= 19;
sInitTime.u32cHour
= 13;
sInitTime.u32cMinute
= 20;
sInitTime.u32cSecond
= 0;
sInitTime.u32cDayOfWeek = DRVRTC_MONDAY;
sInitTime.u8cClockDisplay = DRVRTC_CLOCK_24;
if (DrvRTC_Open(&sInitTime) !=E_SUCCESS)
{
printf("RTC Open Fail!!\n");
}
V1.05.002
- 211 -
DrvRTC_Read
Prototype
int32_t
DrvRTC_Read (
E_DRVRTC_TIME_SELECT eTime,
S_DRVRTC_TIME_DATA_T *sPt
);
Description
Read current date/time or alarm date/time from RTC setting
Parameter
eTime [in]
Specify the current/alarm time to be read.
DRVRTC_CURRENT_TIME
: Current time
DRVRTC_ALARM_TIME
: Alarm time
*sPt [in]
Specify the buffer to store the data read from RTC. It includes:
u8cClockDisplay
u8cAmPm
u32cSecond
u32cMinute
u32cHour
u32cDayOfWeek
u32cDay
u32cMonth
u32Year
: DRVRTC_CLOCK_12 / DRVRTC_CLOCK_24
: DRVRTC_AM / DRVRTC_PM
: Second value
: Minute value
: Hour value
: Day of week
: Day value
: Month value
: Year value
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_EIO: Initial RTC Failed.
Example
/* Condition: You want to get current RTC calendar and time */
S_DRVRTC_TIME_DATA_T sCurTime;
DrvRTC_Read(DRVRTC_CURRENT_TIME, &sCurTime);
V1.05.002
- 212 -
printf("Current Time:%d/%02d/%02d
%02d:%02d:%02d\n",
sCurTime.u32Year,sCurTime.u32cMonth,sCurTime.u32cDay,sCurTime.u32cHour,sCurTi
me.u32cMinute,sCurTime.u32cSecond);
DrvRTC_Write
Prototype
int32_t
DrvRTC_Write (
E_DRVRTC_TIME_SELECT eTime,
S_DRVRTC_TIME_DATA_T *sPt
);
Description
Set current date/time or alarm date/time to RTC
Parameter
eTime [in]
Specify the current/alarm time to be written.
DRVRTC_CURRENT_TIME
: Current time
DRVRTC_ALARM_TIME
: Alarm time
*sPt [in]
Specify the data to write to RTC. It includes:
u8cClockDisplay
u8cAmPm
u32cSecond
u32cMinute
u32cHour
u32cDayOfWeek
u32cDay
u32cMonth
u32Year
DRVRTC_CLOCK_12 / DRVRTC_CLOCK_24
DRVRTC_AM / DRVRTC_PM
Second value
Minute value
Hour value
Day of week
Day value
Month value
Year value
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
E_DRVRTC_ERR_EIO: Initial RTC Failed.
Example
V1.05.002
- 213 -
DrvRTC_Close
Prototype
int32_t
DrvRTC_Close (void);
Description
Disable NVIC channel of RTC and both tick and alarm interrupt..
Include
Driver/DrvRTC.h
Return Value
E_SUCCESS: Success
Example
DrvRTC_Close();
DrvRTC_GetVersion
Prototype
int32_t
DrvRTC_GetVersion (void);
Description
Return the current version number of driver.
Include
Driver/DrvRTC.h
Return Value
Version number :
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
V1.05.002
- 214 -
Value
Description
CALLBACK_RXOK
CALLBACK_TXOK
CALLBACK_EWARN
V1.05.002
- 215 -
Name
Value
Description
CALLBACK_BOFF
CALLBACK_MSG
CALLBACK_WAKEUP
Value
Description
CAN_STD_ID
Standard ID (11-bits)
CAN_EXT_ID
Extended ID (29-bits)
Value
Description
REMOTE_FRAME
Remote Frame
DATA_FRAME
Data Frame
10.4. Functions
DrvCAN_Init
Prototype
void DrvCAN_Init(void);
Description
The function is used to reset and Initializes CAN IP
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Enable CAN IP clock */
DrvCAN_Init();
V1.05.002
- 216 -
DrvCAN_Close
Prototype
void DrvCAN_Close(void);
Description
Reset and clear all CAN control and disable CAN IP
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Disable CAN IP clock , clear callback function pointer and reset CAN IP*/
DrvCAN_Close();
DrvCAN_Open
Prototype
int32_t DrvCAN_Open(uint32_t u32kbps);
Description
The function is used to set bus timing parameter according current clock and target bit rate.
Parameter
u32kbps [in]
The target CAN kilo bit rate per second.
The range of u32kbps is 1~1000Kbps.
Include
Driver/DrvCAN.h
Return Value
E_DRVCAN_ERR_BITRATE
E_SUCCESS
Example
/* Set CAN bitrate is 500kbps */
DrvCAN_Open(500);
V1.05.002
- 217 -
DrvCAN_SetTiming
Prototype
void DrvCAN_SetTiming(uint8_t u8Tseg2, uint8_t u8Tseg1, uint8_t u8Sjw, uint32_t
u32Brp);
Description
Setups the CAN timing with specific parameters.
Parameter
u8Tseg1 [in]
specifies Time Segment before the sample point. This parameter must be a number
between 1 and 16.
u8Tseg2 [in]
Time Segment after the sample point. This parameter must be a number between 1 and
8.
u8Sjw [in]
Synchronisation Jump Width. This parameter must be a number between 1 and 4.
u32Brp [in]
Baud Rate Prescaler. This parameter must be a number between 1 and 1024
f APB_CLK
(u8Tseg1 + u8Tseg2 3) (u32Bpr 1)
V1.05.002
- 218 -
DrvCAN_LeaveInitMode();
If the system clock freq = 16MHz, so
CAN bit-rate =
16000000
= 1000kbps
(2 + 3 3) (1 1)
DrvCAN_ResetMsgObj
Prototype
void
Description
Configures the message object as default.
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: SUCCESS
E_DRVCAN_NO_USEFUL_INTERFACE: No useful interface
Example
/* Reset CAN Message Object No.5 information*/
DrvCAN_ResetMsgObj (5);
DrvCAN_ResetAllMsgObj
Prototype
void
DrvCAN_ResetAllMsgObj (void);
Description
Configures all the message objects as default.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
V1.05.002
- 219 -
None.
Example
/* Reset all CAN Message Object */
DrvCAN_ ResetAllMsgObj ();
DrvCAN_SetTxMsgObj
Prototype
int32_t DrvCAN_SetTxMsgObj(uint8_t u8MsgObj, STR_CANMSG_T* pCanMsg);
Description
The function is used to configure a transmit object.
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31
pCanMsg [in]
A structure about CAN message object
idType:
specifies the identifier type of the frames that will be transmitted. using this message
object. This parameter can be one of the following values:
CAN_STD_ID (standard ID, 11-bit)
CAN_EXT_ID (extended ID, 29-bit)
FrameType: DATA_FRAME or REMOTE_FRAME
Id: specifies the identifier used for acceptance filtering
Dlc: Desird data bytes you want to send. Maximun is 8.
Data[0]~ Data[7]: Data value
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: SUCCESS
E_DRVCAN_NO_USEFUL_INTERFACE: No useful interface
Example
/* Configure tMsg structure content into Message Object 0 */
STR_CANMSG_T tMsg;
/* Send a 11-bits message */
V1.05.002
- 220 -
tMsg.FrameType= DATA_FRAME;
tMsg.IdType
= CAN_STD_ID;
tMsg.Id
= 0x7FF;
tMsg.Dlc
= 0;
if(DrvCAN_SetTxMsgObj(MSG(0),&tMsg) < 0)
printf("Set Tx Msg Object failed\n");
DrvCAN_SetMsgObjMask
Prototype
int32_t DrvCAN_SetMsgObjMask(uint8_t u8MsgObj,STR_CANMASK_T* MaskMsg);
Description
Configures Mask as the message object .
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
MaskMsg [in]
specifies the mask structure as message object.
The structure is including of
u8Xtd ( Mask IDE bit)
u8Dir ( Mask Direction)
u32Id ( Mask ID bit)
u8IdType ( Mask ID Type)
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: SUCCESS
E_DRVCAN_NO_USEFUL_INTERFACE: No useful interface
Example
/* Set CAN Message Object No.0 Mask ID is 0x7FF */
STR_CANMASK_T tMsg;
tMsg. u32Id
= 0x7FF;
V1.05.002
- 221 -
DrvCAN_SetRxMsgObj
Prototype
int32_t DrvCAN_SetRxMsgObj(uint8_t u8msgobj, uint8_t u8idType, uint32_t u32id, uint8_t
u8singleOrFifoLast);
Description
The function is used to configure a receive message object..
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
idType [in]
specifies the identifier type of the frames that will be transmitted
using this message object. This parameter can be one of the following values:
- CAN_STD_ID (standard ID, 11-bit)
- CAN_EXT_ID (extended ID, 29-bit)
u32id [in]
specifies the identifier used for acceptance filtering
u8singleOrFifoLast [in]
specifies the end-of-buffer indicator.
This parameter can be one of the following values:
- TRUE: for a single receive object or a FIFO receive object that is the last one of the
FIFO.
- FALSE: for a FIFO receive object that is not the last one
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: SUCCESS
E_DRVCAN_NO_USEFUL_INTERFACE: No useful interface
Example
/* Configure CAN Message Object No.0 only receive ID 0x123 */
STR_CANMSG_T rMsg;
if(DrvCAN_SetRxMsgObj(MSG(0),CAN_STD_ID, 0x123,TRUE) < 0)
printf("Set Rx Msg Object failed\n");
V1.05.002
- 222 -
DrvCAN_ClrIntPnd
Prototype
int32_t DrvCAN_ClrIntPnd (uint8_t u8msgobj);
Description
The function is used to reset IntPnd and TXRQSTNEWDAT bit in a Message Object.
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: SUCCESS
E_DRVCAN_NO_USEFUL_INTERFACE: No useful interface
Example
/* Clear CAN Message Object 0 interrupt pending */
DrvCAN_ ClrIntPnd (0);
DrvCAN_SetTxRqst
Prototype
uint32_t DrvCAN_SetTxRqst (uint8_t u8MsgObj);
Description
The function is used to set transmit request bit in the target message object.
Parameters
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: SUCCESS
Example
/* After call DrvCAN_SetTxMsg () to set up your message content into target message
object , you can call this API and let Message Handler to send this message*/
V1.05.002
- 223 -
DrvCAN_ReadMsgObj
Prototype
int32_t DrvCAN_ReadMsgObj(uint8_t u8MsgObj, uint8_t release, STR_CANMSG_T*
pCanMsg);
Description
Gets the message, if received.
Parameters
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
u8Release [in]
specifies the message release indicator.
This parameter can be one of the following values:
- TRUE: the message object is released when getting the data.
- FALSE: the message object is not released.
pCanMsg [in]
pointer to the message structure where received data is copied.
Include
Driver/DrvCAN
Return Value
- E_SUCCESS: Success
- E_DRVCAN_NO_PENDING_MSG: No any message received
Example
/* Polling IIDR flag to wait specified message object status changed and receive information
is stored as rMsg structure.*/
while(CAN->u32IIDR ==0);
DrvCAN_WaitEndOfTx
Prototype
int32_t DrvCAN_WaitEndOfTx(void);
Description
Waiting until current transmission is finished
V1.05.002
- 224 -
Parameters
None
Include
Driver/DrvCAN.h
Return Value
- E_SUCCESS: Transmission ended
Example
/* Wait.Transmit OK*/
DrvCAN_WaitEndOfTx();
printf(Transmit successfully);
DrvCAN_BasicSendMsg
Prototype
int32_t DrvCAN_BasicSendMsg(STR_CANMSG_T* pCanMsg);
Description
The function is used to send CAN message in BASIC mode of test mode. Before call the API,
the user should be call DrvCAN_EnterTestMode(CAN_TESTR_BASIC) and let CAN
controller enter basic mode of test mode. Please notice IF1 Registers used as Tx Buffer in
basic mode
Parameter
pCanMsg [in]
Pointer to the message structure containing data to transmit..
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: Transmission OK
E_DRVCAN_ERR_TIMEOUT: Check busy flag of interface 0 is timeout
Example
/* Use basic mode to send message without using message ram*/
STR_CANMSG_T msg1;
msg1.FrameType= DATA_FRAME;
msg1.IdType
= CAN_STD_ID;
msg1.Id
= 0x555;
msg1.Dlc
= 0;
V1.05.002
- 225 -
DrvCAN_BasicSendMsg(&msg1);
DrvCAN_BasicReceiveMsg
Prototype
int32_t DrvCAN_BasicReceiveMsg(STR_CANMSG_T* pCanMsg);
Description
Get a message information in BASIC mode. This mode does not use the message RAM
Using IF2 to get receive message information
Parameter
pCanMsg [in]
pointer to the message structure where message is copied.
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS: Reception OK
E_DRVCAN_NO_PENDING_MSG: No any message received
Example
/* Wait data in and stored in rmsg structure*/
STR_CANMSG_T rMsg;
DrvCAN_WaitMsg();
DrvCAN_BasicReceiveMsg(&rMsg);
DrvCAN_EnterInitMode
Prototype
void DrvCAN_EnterInitMode(void);
Description
This function is used to set CAN to enter initialization mode and enable access bit timing
register. After bit timing configuration ready, user must call DrvCAN_LeaveInitMode()to
leave initialization mode and lock bit timing register to let new configuration take effect.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
V1.05.002
- 226 -
None
Example
/* Enter init mode and user can changed bus timing settings.*/
DrvCAN_EnterInitMode();
DrvCAN_LeaveInitMode
Prototype
void DrvCAN_LeaveInitMode(void);
Description
This function is used to set CAN to leave initialization mode to let bit timing configuration
take effect after configuration ready.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* leave init mode and to let the bit-timing configuration take effect.*/
DrvCAN_LeaveInitMode();
DrvCAN_EnterTestMode
Prototype
void DrvCAN_EnterTestMode(uint8_t u8TestMask);
Description
Switchs the CAN into test mode. There are four test mode (BASIC/SILENT/LOOPBACK/
LOOPBACK combined SILENT/CONTROL_TX_PIN)could be selected. After setting test
mode,user must call DrvCAN_LeaveInitMode() to let the setting take effect.
Parameter
u8TestMask [in]
specifies the configuration in test modes
It could be
CAN_TESTR_BASIC
CAN_TESTR_SILENT
CAN_TESTR_LBACK
CAN_TESTR_TX0
CAN_TESTR_TX1
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Enter basic mode of test mode*/
DrvCAN_ EnterTestMode (CAN_TEST_BASIC);
DrvCAN_LeaveTestMode
Prototype
void DrvCAN_LeaveTestMode(void);
Description
This function is used to Leaves the current test mode (switch into normal mode)..
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Leave test mode and then enter normal mode
*/
DrvCAN_IsNewDataReceived
Prototype
uint32_t DrvCAN_IsNewDataReceived (uint8_t u8MsgObj);
Description
This function is used to get the waiting status of a received message.
V1.05.002
- 228 -
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
A non-zero value if the corresponding message object has a new data bit is set, else 0.
Example
/* Check message object 0 is no received new message */
if(!DrvCAN_IsNewDataReceived (0);
return false;
DrvCAN_IsTxRqstPending
Prototype
uint32_t DrvCAN_IsTxRqstPending (uint8_t u8MsgObj);
Description
This function is used to get the request pending status of a transmitted message..
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
A non-zero value if the corresponding message has an tx request pending, else 0.
Example
/* Check message object 0 transmit request is sent or not */
if(!DrvCAN_IsTxRqstPending (0);
return false;
DrvCAN_IsIntPending
Prototype
uint32_t DrvCAN_IsIntPending(uint8_t u8MsgoObj);
V1.05.002
- 229 -
Description
This function is used to get the interrupt status of a message object.
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
A non-zero value if the corresponding message has an interrupt pending, else 0.
Example
/* Check message object 0 interrupt is pending or not */
if(!DrvCAN_ IsIntPending (0);
return false;
DrvCAN_IsObjectValid
Prototype
uint32_t DrvCAN_IsObjectValid(uint8_t u8MsgObj);
Description
This function is used to test the validity of a message object (ready to use)..
Parameter
u8MsgObj [in]
specifies the Message object number, from 0 to 31.
Include
Driver/DrvCAN.h
Return Value
A non-zero value if the corresponding message object is valid, else 0.
Example
/* Check message object 0 is valied or not */
if(!DrvCAN_ IsObjectValid (0);
return false;
V1.05.002
- 230 -
DrvCAN_ResetIF
Prototype
void DrvCAN_ResetIF(uint8_t u8IF_Num);
Description
This function is used to reset message interface parameters..
Parameter
u8IF_Num [in]
specifies the Message Control Interface, 0 or 1
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Reset interface 0 all setting register value */
DrvCAN_ResetIF(0);
DrvCAN_WaitMsg
Prototype
void DrvCAN_WaitMsg(void);
Description
This function is used to wait message into message buffer in basic mode. Please notice the
function is polling NEWDAT bit of MCON register by while loop and it is used in basic
mode.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Wait new message into message ram */
DrvCAN_WaitMsg ();
printf(New Data In\n);
V1.05.002
- 231 -
DrvCAN_EnableInt
Prototype
int32_t DrvCAN_EnableInt(uint16_t u16IntEnable);
Description
Enable CAN interrupt and NVIC corresponding to CAN.
Parameter
u16IntEnable [in]
Interrupt Enable (CAN_CON_IE or CAN_CON_SIE or CAN_CON_EIE).
It could be
CAN_CON_IE
DrvCAN_DisableInt
Prototype
int32_t DrvCAN_DisableInt(uint16_t u16IntEnable);
Description
Disable CAN interrupt and NVIC corresponding to CAN.
Parameter
u16IntEnable [in]
Interrupt Enable (CAN_CON_IE or CAN_CON_SIE or CAN_CON_EIE).
CAN_CON_IE : Module interrupt enable
CAN_CON_SIE : Status change interrupt enable
CAN_CON_EIE : Error interrupt enable
Include
Driver/DrvCAN.h
V1.05.002
- 232 -
Return Value
None.
Example
/* Interrupt Disable */
DrvCAN_DisableInt(CAN_CON_IE);
DrvCAN_InstallCallback
Prototype
int32_t DrvCAN_InstallCallback(E_CAN_CALLBACK_TYPE Type, CAN_CALLBACK
callbackfn);
Description
Install CAN call back function for CAN normal function
MSG,RXOK,TXOK,EWARN,BOFF,WAKEUP.
Parameter
Type [in]
E_CAN_CALLBACK_TYPE (CALLBACK_RXOK or CALLBACK_TXOK or
CALLBACK_EWARN or CALLBACK_BOFF or CALLBACK_MSG or
CALLBACK_WAKEUP). More detail please ref Table 10.1
callback [in]
callback function pointer
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS : Success
E_ E_DRVCAN_ERR_ARGUMENT: Failed
Example
/* Install Message callback function TestFnMsg */
DrvCAN_InstallCallback(CALLBACK_MSG, (CAN_CALLBACK)TestFnMsg);
DrvCAN_UninstallCallback
Prototype
int32_t DrvCAN_UninstallCallback(E_CAN_CALLBACK_TYPE Type);
Description
The function is used to uninstall exist callback function pointer.
Parameter
V1.05.002
- 233 -
Type [in]
E_CAN_CALLBACK_TYPE (CALLBACK_RXOK or CALLBACK_TXOK or
CALLBACK_EWARN or CALLBACK_BOFF or CALLBACK_MSG or
CALLBACK_WAKEUP). More detail please ref Table 10.1
Include
Driver/DrvCAN.h
Return Value
E_SUCCESS : Success
E_ E_DRVCAN_ERR_ARGUMENT: Failed
Example
/* Remove all message object callback function pointer */
DrvCAN_UninstallCallback(CALLBACK_MSG);
DrvCAN_EnableWakeUp
Prototype
void DrvCAN_EnableWakeUp(void);
Description
The function is used to enable wakeup function.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Enable wake-up function */
DrvCAN_EnableWakeUp();
DrvCAN_DisableWakeUp
Prototype
void DrvCAN_DisableWakeUp(void);
Description
The function is used to disable wakeup function.
V1.05.002
- 234 -
Parameter
None
Include
Driver/DrvCAN.h
Return Value
None
Example
/* Disable wake-up function */
DrvCAN_DisableWakeUp()
DrvCAN_GetCANBitRate
Prototype
int32_t DrvCAN_GetCANBitRate(void);
Description
Return current CAN bitrate according to user bit-timing parameter settings.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
Current Bit-Rate (kilo bit per second)
Example
/* Get current CAN bit rate */
int32 i32bitrate;
i32bitrate
= DrvCAN_GetCANBitRate ();
DrvCAN_GetTxErrCount
Prototype
uint32_t DrvCAN_GetTxErrCount(void);
Description
V1.05.002
- 235 -
DrvCAN_GetRxErrCount
Prototype
uint32_t DrvCAN_GetRxErrCount(void);
Description
The function is used to get current receive error counter (REC)
Parameter
None
Include
Driver/DrvCAN.h
Return Value
Current Receive Error Counter(REC)
Example
/* Get current receive error counter(REC) */
int32 i32RxErrCnt
i32RxErrCnt
DrvCAN_GetVersion
Prototype
uint32_t DrvCAN_GetVersion (void);
V1.05.002
- 236 -
Description
Get this modules version.
Parameter
None
Include
Driver/DrvCAN.h
Return Value
CAN driver current version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* Get CAN driver current version number */
int32_t i32CANVersionNum
i32CANVersionNum = DrvCAN_GetVersion();
V1.05.002
- 237 -
V1.05.002
- 238 -
Value
Description
DRVPWM_TIMER0
DRVPWM_TIMER1
DRVPWM_TIMER2
DRVPWM_TIMER3
DRVPWM_TIMER4
DRVPWM_TIMER5
DRVPWM_TIMER6
DRVPWM_TIMER7
DRVPWM_CAP0
DRVPWM_CAP1
DRVPWM_CAP2
DRVPWM_CAP3
DRVPWM_CAP4
DRVPWM_CAP5
DRVPWM_CAP6
DRVPWM_CAP7
DRVPWM_CAP_ALL_INT
DRVPWM_CAP_RISING_INT
DRVPWM_CAP_FALLING_INT
DRVPWM_CAP_RISING_FLAG
DRVPWM_CAP_FALLING_FLAG
DRVPWM_CLOCK_DIV_1
DRVPWM_CLOCK_DIV_2
DRVPWM_CLOCK_DIV_4
DRVPWM_CLOCK_DIV_8
DRVPWM_CLOCK_DIV_16
0x00
0x01
0x02
0x03
0x04
0x05
0x06
0x07
0x10
0x11
0x12
0x13
0x14
0x15
0x16
0x17
3
1
2
6
7
4
0
1
2
3
DRVPWM_EXT_12M
DRVPWM_EXT_32K
DRVPWM_HCLK
DRVPWM_INTERNAL_22M
DRVPWM_AUTO_RELOAD_MODE
DRVPWM_ONE_SHOT_MODE
`
1
0
PWM Timer 0
PWM Timer 1
PWM Timer 2
PWM Timer 3
PWM Timer 4
PWM Timer 5
PWM Timer 6
PWM Timer 7
PWM Capture 0
PWM Capture 1
PWM Capture 2
PWM Capture 3
PWM Capture 4
PWM Capture 5
PWM Capture 6
PWM Capture 7
PWM Capture Rising and Falling Interrupt
PWM Capture Rising Interrupt
PWM Capture Falling Interrupt
Capture rising interrupt flag
Capture falling interrupt flag
Input clock divided by 1
Input clock divided by 2
Input clock divided by 4
Input clock divided by 8
Input clock divided by 16
PWM clock source from external 12M
crystal
PWM clock source from external 32K
crystal
PWM clock source from HCLK
PWM clock source from internal 22MHz
oscillator
PWM Timer auto-reload mode
PWM Timer One-shot mode
V1.05.002
- 239 -
11.4. Functions
DrvPWM_IsTimerEnabled
Prototype
int32_t
DrvPWM_IsTimerEnabled(uint8_t u8Timer);
Description
This function is used to get PWM specified timer enable/disable state
Parameter
u8Timer [in]
Specify the timer.
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
Include
Driver/DrvPWM.h
Return Value
1: The specified timer is enabled.
0: The specified timer is disabled.
Example
int32_t
i32state ;
DrvPWM_SetTimerCounter
Prototype
void
V1.05.002
- 240 -
Description
This function is used to set the PWM specified timer counter.
Parameter
u8Timer [in]
Specify the timer.
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
u16Counter [in]
Specify the timer value. (0~65535). If the counter is set to 0, the timer will stop.
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Set 10000 to PWM timer 3 counter register. When the PWM timer 3 start to count down,
PWM timer 3 will count down from 10000 to 0. If PWM timer 3 is set to auto-reload mode,
the PWM timer 3 will reload 10000 to PWM timer 3 counter register after PWM timer 3
count down to 0 and PWM timer 3 will continue to count down from 10000 to 0 again. */
DrvPWM_SetTimerCounter(DRVPWM_TIMER3, 10000);
DrvPWM_GetTimerCounter
Prototype
uint32_t
DrvPWM_GetTimerCounter(uint8_t u8Timer);
Description
This function is used to get the PWM specified timer counter value
Parameter
u8Timer [in]
Specify the timer.
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
V1.05.002
- 241 -
DrvPWM_EnableInt
Prototype
void
DrvPWM_EnableInt(uint8_t u8Timer, uint8_t u8Int, PFN_DRVPWM_CALLBACK
pfncallback);
Description
This function is used to enable the PWM timer/capture interrupt and install the call back
function.
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
u8Int [in]
Specify the capture interrupt type (The parameter is valid only when capture function)
V1.05.002
- 242 -
DRVPWM_CAP_RISING_INT
DRVPWM_CAP_FALLING_INT
DRVPWM_CAP_ALL_INT
pfncallback [in]
The pointer of the callback function for specified timer / capture.
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Enable PWM capture 5 falling edge interrupt and install DRVPWM_CapIRQHandler() as
its interrupt callback function.*/
DrvPWM_EnableInt(DRVPWM_CAP5, DRVPWM_CAP_FALLING_INT,
DRVPWM_CapIRQHandler);
DrvPWM_DisableInt
Prototype
void
DrvPWM_DisableInt(uint8_t u8Timer);
Description
This function is used to disable the PWM timer/capture interrupt.
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
V1.05.002
- 243 -
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Disable PWM capture 5 interrupts including rising and falling interrupt source and also
uninstall PWM capture 5 rising and falling interrupt callback functions. */
DrvPWM_DisableInt(DRVPWM_CAP5);
/* Disable PWM timer 5 interrupt and uninstall PWM timer 5 callback function.*/
DrvPWM_DisableInt(DRVPWM_TIMER5);
DrvPWM_ClearInt
Prototype
void
DrvPWM_ClearInt(uint8_t u8Timer);
Description
This function is used to clear the PWM timer/capture interrupt flag.
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
Include
Driver/DrvPWM.h
Return Value
V1.05.002
- 244 -
None
Example
/* Clear PWM timer 1 interrupt flag.*/
DrvPWM_ClearInt(DRVPWM_TIMER1);
/* Clear PWM capture 0 interrupt flag. */
DrvPWM_ClearInt(DRVPWM_CAP0);
DrvPWM_GetIntFlag
Prototype
int32_t
DrvPWM_GetIntFlag(uint8_t u8Timer);
Description
This function is used to get the PWM timer/capture interrupt flag
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
Include
Driver/DrvPWM.h
Return Value
1: The specified interrupt occurs.
0: The specified interrupt doesnt occur.
Example
/* Get PWM timer 6 interrupt flag.*/
V1.05.002
- 245 -
if(DrvPWM_GetIntFlag(DRVPWM_TIMER6)==1)
printf(PWM timer 6 interrupt occurs!\n);
else if(DrvPWM_GetIntFlag(DRVPWM_TIMER6)==0)
printf(PWM timer 6 interrupt dosent occur!\n);
DrvPWM_GetRisingCounter
Prototype
uint16_t DrvPWM_GetRisingCounter(uint8_t u8Capture);
Description
This function is used to get value which latches the counter when theres a rising transition.
Parameter
u8Capture [in]
Specify the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
Include
Driver/DrvPWM.h
Return Value
The value was latched from PWM capture current counter when theres a rising transition.
Example
/* Get PWM capture 7 rising latch register value. */
uint16_t u16RetValTimer7RisingLatchValue;
u16RetValTimer7RisingLatchValue = DrvPWM_GetRisingCounter (DRVPWM_CAP7);
DrvPWM_GetFallingCounter
Prototype
uint16_t DrvPWM_GetFallingCounter(uint8_t u8Capture);
Description
This function is used to get value which latches the counter when theres a falling transition.
V1.05.002
- 246 -
Parameter
u8Capture [in]
Specify the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
Include
Driver/DrvPWM.h
Return Value
The value was latched from PWM capture current counter when theres a falling transition.
Example
/* Get PWM capture 7 falling latch register value.*/
uint16_t u16RetValTimer7FallingLatchValue;
u16RetValTimer7FallingLatchValue = DrvPWM_GetFallingCounter (DRVPWM_CAP7);
DrvPWM_GetCaptureIntStatus
Prototype
int32_t
Description
Check if theres a rising / falling transition
Parameter
u8Capture [in]
Specify the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
u8IntType [in]
Specify the Capture Latched Indicator.
V1.05.002
- 247 -
DRVPWM_CAP_RISING_FLAG
DRVPWM_CAP_FALLING_FLAG
Include
Driver/DrvPWM.h
Return Value
TRUE: The specified transition occurs.
FALSE: The specified transition doesnt occur.
Example
/* Get PWM capture 5 rising transition flag.*/
if(DrvPWM_GetCaptureIntStatus(DRVPWM_CAP5, DRVPWM_CAP_RISING_FLAG)==TRUE)
printf(PWM capture 5 rising transition occurs!\n);
else if(DrvPWM_GetCaptureIntStatus(DRVPWM_CAP5, DRVPWM_CAP_RISING_FLAG)==FALSE)
printf(PWM capture 5 rising transition doesnt occur!\n);
DrvPWM_ClearCaptureIntStatus
Prototype
void
Description
Clear the rising / falling transition indicator flag
Parameter
u8Capture [in]
Specify the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
u8IntType [in]
Specify the Capture Latched Indicator.
DRVPWM_CAP_RISING_FLAG
V1.05.002
- 248 -
Return Value
None
Example
/* Clear PWM capture 5 falling transition flag.*/
DrvPWM_ClearCaptureIntStatus(DRVPWM_CAP5, DRVPWM_CAP_FALLING_FLAG);
DrvPWM_Open
Prototype
void
DrvPWM_Open(void);
Description
Enable PWM engine clock and reset PWM.
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Enable PWM engine clock and reset PWM engine. */
DrvPWM_Open();
DrvPWM_Close
Prototype
void
DrvPWM_Close(void);
Description
Disable PWM engine clock and the Capture Input / PWM Output Enable function.
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Disable PWM timer 0~7 output, PWM capture 0~7 output and disable PWM engine clock.*/
DrvPWM_Close ( );
V1.05.002
- 249 -
DrvPWM_EnableDeadZone
Prototype
void
DrvPWM_EnableDeadZone(uint8_t u8Timer, uint8_t u8Length, int32_t
i32EnableDeadZone);
Description
This function is used to set the dead zone length and enable/disable Dead Zone function.
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0 or DRVPWM_TIMER1: PWM timer 0 & PWM timer 1.
DRVPWM_TIMER2 or DRVPWM_TIMER3: PWM timer 2 & PWM timer 3.
DRVPWM_TIMER4 or DRVPWM_TIMER5: PWM timer 4 & PWM timer 5.
DRVPWM_TIMER6 or DRVPWM_TIMER7: PWM timer 6 & PWM timer 7.
u8Length [in]
Specify Dead Zone Length: 0~255. The unit is one period of PWM clock.
i32EnableDeadZone [in]
Enable DeadZone (1) / Diasble DeadZone (0)
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Enable PWM timer 0 and time 1 Dead-Zone function. PWM timer 0 and PWM timer 1
became a complementary pair. Set Dead-Zone time length to 100 and the unit time of
Dead-Zone length which is the same as the unit of received PWM timer clock.*/
uint8_t u8DeadZoneLength = 100;
DrvPWM_EnableDeadZone (DRVPWM_TIMER0, u8DeadZoneLength, 1);
Sample code
/* Enable Timer0 and Timer1 Dead-Zone function and set Dead-Zone interval to 5us. Dead zone
interval = [1 / (PWM0 engine clock source / sPt.u8PreScale / sPt.u8ClockSelector)]*
u8DeadZoneLength = unit time * u8DeadZoneLength = [1/(12000000 / 6 / 1)] * 10 = 5us */
uint8_t u8DeadZoneLength = 10; // Set dead zone length to 10 unit time
/* PWM Timer property */
sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
sPt.u8HighPulseRatio = 30;
sPt.i32Inverter = 0;
V1.05.002
- 250 -
sPt.u32Duty = 1000;
sPt.u8ClockSelector = DRVPWM_CLOCK_DIV_1;
sPt.u8PreScale = 6;
u8Timer = DRVPWM_TIMER0;
/* Select PWM engine clock source */
DrvPWM_SelectClockSource(u8Timer, DRVPWM_EXT_12M);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(u8Timer, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(u8Timer, 1);
/* Enable Output for PWM Timer1 */
DrvPWM_SetTimerIO(DRVPWM_TIMER1, 1);
/* Enable Timer0 and Time1 dead zone function and Set dead zone length to 10 */
DrvPWM_EnableDeadZone(u8Timer, u8DeadZoneLength, 1);
/* Enable the PWM Timer 0 */
DrvPWM_Enable(u8Timer, 1);
DrvPWM_Enable
Prototype
void
Description
This function is used to enable PWM timer / capture function
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
V1.05.002
- 251 -
DrvPWM_SetTimerClk
Prototype
uint32_t
Description
This function is used to configure the frequency/pulse/mode/inverter function. The function
will set the frequency property automatically when user set a nonzero frequency value by
u32Frequency. When the setting of frequency value (u32Frequency) is not specified, i.e set
to 0, user needs to provide the setting of clock selector, prescale and duty to generate desired
frequency.
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
V1.05.002
- 252 -
u8ClockSelector
u8PreScale
u32Duty
Description
The timer/capture frequency (Hz)
High pulse ratio (1~100)
DRVPWM_ONE_SHOT_MODE /
DRVPWM_AUTO_RELOAD_MODE
Inverter Enable (1) / Inverter Disable (0)
Clock Selector
DRVPWM_CLOCK_DIV_1: PWM input clock is divided by 1
DRVPWM_CLOCK_DIV_2: PWM input clock is divided by 2
DRVPWM_CLOCK_DIV_4: PWM input clock is divided by 4
DRVPWM_CLOCK_DIV_8: PWM input clock is divided by 8
DRVPWM_CLOCK_DIV_16: PWM input clock is divided by 16
(The parameter takes effect when u32Frequency = 0)
Prescale (1 ~ 255). If the u8PreScale is set to 0, the timer will stop
The PWM input clock = PWM source clock / (u8PreScale + 1)
(The parameter takes effect when u32Frequency = 0)
Pulse duty (0x1 ~ 0x10000)
(The parameter takes effect when u32Frequency = 0 or u8Timer =
DRVPWM_CAP0/DRVPWM_CAP1/DRVPWM_CAP2/DRVPW
M_CAP3/DRVPWM_CAP4/DRVPWM_CAP5/DRVPWM_CAP6/
DRVPWM_CAP7)
Include
Driver/DrvPWM.h
Return Value
The actual specified PWM frequency (Hz).
Example
/* PWM timer 0 output 1KHz waveform and duty cycle of waveform is 20% */
Method 1:
Fill sPt.u32Frequency = 1000 to determine the waveform frequency and
DrvPWM_SetTimerClk() will set the frequency property automatically.
/* Select PWM engine clock and user must know the HCLK frequency*/
DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK);
/* Set PWM Timer0 Configuration */
DrvPWM_SetTimerClk(u8Timer, &sPt);
/* Enable Output for PWM Timer0 */
DrvPWM_SetTimerIO(u8Timer, 1);
/* Enable Interrupt Sources of PWM Timer0 and install call back function */
DrvPWM_EnableInt(u8Timer, 0, DRVPWM_PwmIRQHandler);
/* Enable the PWM Timer 0 */
DrvPWM_Enable(u8Timer, 1);
V1.05.002
- 254 -
DrvPWM_SetTimerIO
Prototype
void
Description
This function is used to enable/disable PWM timer/capture I/O function
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0: PWM timer 0.
DRVPWM_TIMER1: PWM timer 1.
DRVPWM_TIMER2: PWM timer 2.
DRVPWM_TIMER3: PWM timer 3.
DRVPWM_TIMER4: PWM timer 4.
DRVPWM_TIMER5: PWM timer 5.
DRVPWM_TIMER6: PWM timer 6.
DRVPWM_TIMER7: PWM timer 7.
or the capture.
DRVPWM_CAP0: PWM capture 0.
DRVPWM_CAP1: PWM capture 1.
DRVPWM_CAP2: PWM capture 2.
DRVPWM_CAP3: PWM capture 3.
DRVPWM_CAP4: PWM capture 4.
DRVPWM_CAP5: PWM capture 5.
DRVPWM_CAP6: PWM capture 6.
DRVPWM_CAP7: PWM capture 7.
i32Enable [in]
Enable (1) / Disable (0)
Include
Driver/DrvPWM.h
Return Value
None
Example
/* Enable PWM timer 0 output.*/
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1);
/* Disable PWM timer 0 output. */
DrvPWM_SetTimerIO(DRVPWM_TIMER0, 0);
/* Enable PWM capture 3 input. */
DrvPWM_SetTimerIO(DRVPWM_CAP3, 1);
/* Disable PWM capture timer 3 input
V1.05.002
- 255 -
DrvPWM_SetTimerIO(DRVPWM_CAP3, 0);
DrvPWM_SelectClockSource
Prototype
void DrvPWM_SelectClockSource(uint8_t u8Timer, uint8_t u8ClockSourceSelector);
Description
This function is used to select PWM0&PWM1, PWM2&PWM3, PWM4&PWM5 and
PWM6&PWM7 engine clock source. It means PWM0/1 use one clock source. PWM2/3 can
use another clock source and so on. In other words, if user change PWM timer 0 clock source
from external 12MHz to internal 22MHz, the clock source of PWM timer 1 will also be
changed from external 12MHz to internal 22MHz. Furthermore, it is possible to set the clock
source of PWM1 to be external 12MHz and set the clock source of PWM2 to be external
32.768Hz.
Parameter
u8Timer [in]
Specify the timer
DRVPWM_TIMER0 or DRVPWM_TIMER1: PWM timer 0 & PWM timer 1.
DRVPWM_TIMER2 or DRVPWM_TIMER3: PWM timer 2 & PWM timer 3.
DRVPWM_TIMER4 or DRVPWM_TIMER5: PWM timer 4 & PWM timer 5.
DRVPWM_TIMER6 or DRVPWM_TIMER7: PWM timer 6 & PWM timer 7.
u8ClockSourceSelector [in]
To set the clock source of specified PWM timer. it could be DRVPWM_EXT_12M /
DRVPWM_EXT_32K / DRVPWM_HCLK / DRVPWM_INTERNAL_22M. where
DRVPWM_EXT_12M is external crystal clock. DRVPWM_EXT_32K is external
32.768 Hz crystal clock. DRVPWM_HCLK is HCLK. DRVPWM_INTERNAL_22M is
internal 22.1184 MHz crystal clock
Include
Driver/DrvPWM.h
Return Value
None
Example
Select PWM timer 0 and PWM timer 1 engine clock source from HCLK.
DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_HCLK);
Select PWM timer 6 and PWM timer 7 engine clock source from external 12MHz.
DrvPWM_SelectClockSource(DRVPWM_TIMER7, DRVPWM_EXT_12M);
V1.05.002
- 256 -
DrvPWM_SelectClearLatchFlagOption
Prototype
int32_t DrvPWM_SelectClearLatchFlagOption (int32_t i32option);
Description
This function is used to select how to clear Capture rising & falling Latch Indicator.
Parameter
i32option [in]
0: Select option to clear the Capture Latch Indicators by writing a '0'.
1: Select option to clear the Capture Latch Indicators by writing a '1'.
Include
Driver/DrvPWM.h
Return Value
0
Succeed
<0
Note
Only NUC1x0xxxBx(Ex: NUC140RD2BN), NUC1x0xxxCx(Ex: NUC140VE3CN) and
NUC101 of NuMicroTM NUC100 series support this function.Please refer to NuMicroTM
NUC100 Series Products Selection Guide of Appendix in details.
DrvPWM_GetVersion
Prototype
uint32_t DrvPWM_GetVersion (void);
Description
Get this modules version.
Parameter
None
Include
Driver/DrvPWM.h
Return Value
PWM driver current version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
V1.05.002
- 257 -
V1.05.002
- 258 -
Value
Description
DRVPS2_RXINT
DRVPS2_TXINT
DRVPS2_TXFIFODEPTH
16
TX FIFO depth
V1.05.002
- 259 -
12.4. Macros
_DRVPS2_OVERRIDE
Prototype
void _DRVPS2_OVERRIDE(bool state);
Description
This macro is used to enable/disable software to control DATA/CLK line.
Parameter
state [in]
Specify software override or not. 1 means to enable software override PS/2 CLK/DATA
pin state, 0 means to disable it.
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Enable Software to control DATA/CLK pin */
_DRVPS2_OVERRIDE(1)
/* Disable Software to control DATA/CLK pin */
_DRVPS2_OVERRIDE(0)
_DRVPS2_PS2CLK
Prototype
void _DRVPS2_PS2CLK(bool state);
Description
This macro can force PS2CLK high or low regardless of the internal state of the device
controller if _DRVPS2_OVERRIDE called. 1 means high, 0 means low
Parameter
state [in]
Specify PS2CLK line high or low
Include
Driver/DrvPS2.h
V1.05.002
- 260 -
Return Value
None.
Note
The macro is meaningful only when DRVPS2_OVERRIDE has been called.
Example
/* Force PS2CLK pin high. */
_DRVPS2_PS2CLK(1);
/* Force PS2CLK pin low. */
_DRVPS2_PS2CLK(0);
_DRVPS2_PS2DATA
Prototype
void _DRVPS2_PS2DATA(bool state);
Description
This macro can force PS2DATA high or low regardless of the internal state of the device
controller if _DRVPS2_OVERRIDE called. 1 means high, 0 means low.
Parameter
state [in]
Specify PS2DATA line high or low
Include
Driver/DrvPS2.h
Return Value
None.
Note
The macro is meaningful only when _DRVPS2_OVERRIDE has been called.
Example
/* Force PS2DATApin high. */
_DRVPS2_PS2DATA (1);
/* Force PS2DATA pin low. */
_DRVPS2_PS2DATA (0);
_DRVPS2_CLRFIFO
Prototype
V1.05.002
- 261 -
void DRVPS2_CLRFIFO();
Description
The macro is used to clear TX FIFO.
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Clear TX FIFO. */
_DRVPS2_CLRFIFO();
_DRVPS2_ACKNOTALWAYS
Prototype
void _DRVPS2_ACKNOTALWAYS();
Description
The macro is used to enable ack not always.. If parity error or stop bit is not received
correctly, acknowledge bit will not be sent to host at 12 th clock.,
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Enable ackknowlwde NOT always. */
_DRVPS2_ACKNOTALWAYS()
_DRVPS2_ACKALWAYS
Prototype
void _DRVPS2_ACKALWAYS();
V1.05.002
- 262 -
Description
The macro is used to enable ack always.. If parity error or stop bit is not received correctly,
acknowledge bit will always send acknowledge to host at 12th clock for host to device
communication
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Enable ackknowlwde always. */
_DRVPS2_ACKALWAYS()
_DRVPS2_RXINTENABLE
Prototype
void _DRVPS2_RXINTENABLE();
Description
The macro is used to enable Rx interrupt. When acknowledge bit is sent from host to device,
RX interrupt will happen
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Enable RX interrupt. */
_DRVPS2_RXINTENABLE();
_DRVPS2_RXINTDISABLE
Prototype
void _DRVPS2_RXINTDISABLE();
V1.05.002
- 263 -
Description
The macro is used to disable Rx interrupt.
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Disable RX interrupt. */
_DRVPS2_RXINTDISABLE ();
_DRVPS2_TXINTENABLE
Prototype
void _DRVPS2_TXINTENABLE();
Description
The macro is used to enable TX interrupt. When STOP bit is transmitted, TX interrupt will
happen.
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Enable TX interrupt. */
_DRVPS2_TXINTENABLE();
_DRVPS2_TXINTDISABLE
Prototype
void _DRVPS2_TXINTDISABLE ();
Description
V1.05.002
- 264 -
_DRVPS2_PS2ENABLE
Prototype
void _RVPS2_PS2ENABLE ();
Description
The macro is used to enable PS/2 device controller.
Parameter
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Enable PS/2 device controller. */
_DRVPS2_PS2ENABLE ();
_DRVPS2_PS2DISABLE
Prototype
void _DRVPS2_PS2DISABLE ();
Description
The macro is used to disable PS/2 device controller.
Parameter
V1.05.002
- 265 -
None
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Disable PS/2 device controller. */
_DRVPS2_PS2DISABLE ();
_DRVPS2_TXFIFO
Prototype
void _DRVPS2_TXFIFO(depth);
Description
The macro is used to set TX FIFO depth. The range of TX FIFO is [1,16]
Parameter
data [in] : Specify TX FIFO depth(1~16).
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Set TX FIFO depth to 16 bytes. */
_DRVPS2_TXFIFO(16);
/* Set TX FIFO depth to 1 bytes. */
_DRVPS2_TXFIFO(1);
_DRVPS2_SWOVERRIDE
Prototype
void _DRVPS2_SWOVERRIDE(bool data,
bool clk);
Description
The macro is used to set PS2DATA and PS2CLK line by software override. Its equal to
these macos:
_DRVPS2_PS2DATA(data);
V1.05.002
- 266 -
_DRVPS2_PS2CLK(clk);
_DRVPS2_OVERRIDE(1);
Parameter
data [in]
Specify PS2DATA line high or low
clk [in]
Specify PS2CLK line high or low
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Set PS2DATA to high and set PS2CLK to low. */
_DRVPS2_SWOVERRIDE(1, 0);
/* Set PS2DATA to low and set PS2CLK to high. */
_DRVPS2_SWOVERRIDE(0, 1);
_DRVPS2_INTCLR
Prototype
void _DRVPS2_INTCLR(uint8_t intclr) ;
Description
The macro is used to clear interrup status.
Parameter
intclr [in]
Specify to clear TX or RX interrupt. Intclr=0x1 for clear RX interrupt; Intclr=0x2 for
clear TX interrupt; Intclr=0x3 for clear RX and TX interrupt
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Clear RX interrupt. */
_DRVPS2_INTCLR(1);
V1.05.002
- 267 -
/* Clear TX interrupt. */
_DRVPS2_INTCLR(2);
/* Clear TX and RX interrupt. */
_DRVPS2_INTCLR(3);
_DRVPS2_RXDATA
Prototype
uint8_t _DRVPS2_RXDATA();
Description
Reads 1 byte from the receive register.
Parameter
None
Include
Driver/DrvPS2.h
Return Value
One byte data received.
Example
/* Read one byte from PS/2 receive data register. */
uint8_t u8ReceiveData;
u8ReceiveData = _DRVPS2_RXDATA();
_DRVPS2_TXDATAWAIT
Prototype
void _DRVPS2_TXDATAWAIT(uint32_t data, uint32_t len);
Description
The macro is used to wait TX FIFO EMPTY, set TX FIFO depth(length-1) and fill TX
FIFO0-3(Register PS2TXDATA0). Data is sent immediately if bus is in IDLE state. The
range of length is from 1 to 16 bytes. If the transfer size is more than 4 bytes, user should call
DRVPS2_TXDATA1~3() after calling _DRVPS2_TXDATAWAIT() to transfer remind data.
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1
Parameter
data [in]
Specify the data sent
len [in]
V1.05.002
- 268 -
Specify the length of the data sent. Unit is byte. Range is [1, 16]
Include
Driver/DrvPS2.h
Return Value
None
Example
/* Wait TX FIFO empty and then write 16 bytes to TX FIFO. The sixteen bytes consist of 0x01 to
0x16. */
_DRVPS2_TXDATAWAIT(0x04030201, 16);
_DRVPS2_TXDATA1(0x08070605);
_DRVPS2_TXDATA2(0x0C0B0A09);
_DRVPS2_TXDATA3(0x100F0E0D);
/* Wait TX FIFO empty and then write 5 bytes to TX FIFO. The six bytes consist of 0x01 to
0x05. */
_DRVPS2_TXDATAWAIT(0x04030201, 5);
_DRVPS2_TXDATA1(0x05);
/* Wait TX FIFO empty and then write 3 bytes to TX FIFO. The three bytes consist of 0x01 to
0x03. */
_DRVPS2_TXDATAWAIT(0x030201, 3);
_DRVPS2_TXDATA
Prototype
void _DRVPS2_TXDATA(uint32_t data,
uint32_t len);
Description
The macro is used to set TX FIFO depth and fill TX FIFO0-3. But not wait TX FIFO
EMPTY. Data is sent if bus is in IDLE state immediately. The range of len is [1, 16]
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1.
Parameter
data [in]
Specify the data sent
len [in]
Specify the length of the data sent. Unit is byte. Range is [1, 16]
Include
Driver/DrvPS2.h
Return Value
V1.05.002
- 269 -
None
Note
If the transfer size is more than 4 bytes, user should issue _DRVPS2_TXDATA1~3() after
issuing _DRVPS2_TXDATA();
Example
/*Write 16 bytes to TX FIFO. The sixteen bytes consist of 0x01 to 0x16. */
_DRVPS2_TXDATA(0x04030201, 16);
_DRVPS2_TXDATA1(0x08070605);
_DRVPS2_TXDATA2(0x0C0B0A09);
_DRVPS2_TXDATA3(0x100F0E0D);
/* Write 5 bytes to TX FIFO. The six bytes consist of 0x01 to 0x05. */
_DRVPS2_TXDATA(0x04030201, 5);
_DRVPS2_TXDATA1(0x05);
/* Write 3 bytes to TX FIFO. The three bytes consist of 0x01 to 0x03. */
_DRVPS2_TXDATA(0x030201, 3);
_DRVPS2_TXDATA0
Prototype
void _DRVPS2_TXDATA0(uint32_t data);
Description
The macro is used to fill TX FIFO0-3. But not wait TX FIFO EMPTY and not set TX FIFO
depth. Data is sent if bus is in IDLE state immediately.
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1.
Parameter
data [in]
Specify the data that will be sent
Include
Driver/DrvPS2.h
Return Value
None.
Example
/* Write 16 bytes to TX FIFO. The sixteen bytes consist of 0x01 to 0x16. */
while(_DRVPS2_ISTXEMPTY()==0);
_DRVPS2_TXFIFO(16);
V1.05.002
- 270 -
_DRVPS2_TXDATA0(0x04030201);
_DRVPS2_TXDATA1(0x08070605);
_DRVPS2_TXDATA2(0x0C0B0A09);
_DRVPS2_TXDATA3(0x100F0E0D);
_DRVPS2_TXDATA1
Prototype
void _DRVPS2_TXDATA1(uint32_t data);
Description
The macro is used to fill TX FIFO4-7. But not wait TX FIFO EMPTY and not set TX FIFO
depth.
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1.
Parameter
data [in]
Specify the data that will be sent
Include
Driver/DrvPS2.h
Return Value
None
Example
Please refer to _DRVPS2_TXDATA0() example.
_DRVPS2_TXDATA2
Prototype
void _DRVPS2_TXDATA2(uint32_t data);
Description
The macro is used to fill TX FIFO8-11. But not wait TX FIFO EMPTY and not set TX FIFO
depth.
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1.
Parameter
data [in]
Specify the data that will be sent
Include
V1.05.002
- 271 -
Driver/DrvPS2.h
Return Value
None
Example
Please refer to _DRVPS2_TXDATA0() example.
_DRVPS2_TXDATA3
Prototype
void _DRVPS2_TXDATA3(uint32_t data);
Description
The macro is used to fill TX FIFO12-15. But not wait TX FIFO EMPTY and not set TX
FIFO depth.
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1.
Parameter
data [in]
Specify the data that will be sent.
Include
Driver/DrvPS2.h
Return Value
None
Example
Please refer to _DRVPS2_TXDATA0() example.
_DRVPS2_ISTXEMPTY
Prototype
uint8_t _DRVPS2_ISTXEMPTY();
Description
The macro is used to check TX FIFO whether or not empty
When transmitted data byte number is equal to FIFODEPTH then TXEMPTY bit is set to 1.
Parameter
None
Include
V1.05.002
- 272 -
Driver/ DrvPS2.h
Return Value
TX FIFO empty status.
0: TX FIFO is empty.
1: TX FIFO is not empty.
Example
Please refer to _DRVPS2_TXDATA0() example.
_DRVPS2_ISFRAMEERR
Prototype
uint8_t _DRVPS2_ISFRAMEERR();
Description
The macro is used to check whether or not frame error happen. For host to device
communication, if STOP bit is not received it is a frame error. If frame error occurs, DATA
line may keep at low state after 12th clock. At this moment, software override PS2CLK to
send clock till PS2DATA release to high state. After that, device sends a Resend command
to host
Parameter
None
Include
Driver/DrvPS2.h
Return Value
Frame error status.
0: Not frame error.
1: Frame error.
Example
/* Check Frame error and print the result. */
if(_DRVPS2_ISFRAMEERR()==1)
printf(Frame error happen!!\n);
else
printf(Frame error not happen!!\n);
_DRVPS2_ISRXBUSY
Prototype
uint8_t _DRVPS2_ISRXBUSY();
V1.05.002
- 273 -
Description
The macro is used to check whether or not Rx busy. If busy it indicates that PS/2 device is
currently receiving data
Parameter
None
Include
Driver/ DrvPS2.h
Return Value
RX busy flag.
0: RX is not busy,
1: RX is busy.
Example
/* Check RX is busy or not. */
if(_DRVPS2_ISRXBUSY()==1)
printf(RX is busy!\n);
else
printf(RX is not busy!\n);
12.5. Functions
DrvPS2_Open
Prototype
int32_t DrvPS2_Open();
Description
This function is used to init PS/2 IP. It includes enable PS2 clock, enable PS/2 controller,
clear FIFO, set TX FIFO depth to default value zero.
Parameter
None
Include
Driver/DrvPS2.h
Return Value
E_SUCCESS.
Example
V1.05.002
- 274 -
DrvPS2_Close
Prototype
void DrvPS2_Close();
Description
This function is used to disable PS2 controller, disable PS/2 clock and set TX FIFO depth to
default value zero
Parameter
None
Include
Driver/ DrvPS2.h
Return Value
None
Example
/* Close PS2 IP. */
DrvPS2_Close ();
DrvPS2_EnableInt
Prototype
int32_t DrvPS2_EnableInt (
uint32_t
u32InterruptFlag,
PFN_DRVPS2_CALLBACK pfncallback
);
Description
This function is used to enable TX/RX interrupt and install interrupt call back function.
Parameter
u32InterruptFlag [in]
Specify TX/RX interrupt flag that will be enable. It can be DRVPS2_TXINT or
DRVPS2_RXINT or DRVPS2_TXINT| DRVPS2_RXINT
pfncallback [in]
Specify the interrupt call back function. When PS2 interrupt happen, this function will be
called
V1.05.002
- 275 -
Include
Driver/ DrvPS2.h
Return Value
E_SUCCESS
Example
/* Enable TX/RX interrupt, install TX/RX call back function: PS2Mouse_IRQHandler(); */
DrvPS2_EnableInt(DRVPS2_TXINT| DRVPS2_RXINT, PS2Mouse_IRQHandler);
DrvPS2_DisableInt
Prototype
void DrvPS2_DisableInt(uint32_t
u32InterruptFlag);
Description
This function is used to disable Tx/Rx interrupt and uninstall interrupt call back function..
Parameter
u32InterruptFlag [in]
Specify TX/RX interrupt flag that will be disabled. It can be DRVPS2_TXINT or
DRVPS2_RXINT or DRVPS2_TXINT| DRVPS2_RXINT.
Include
Driver/ DrvPS2.h
Return Value
None
Example
/* Disable TX/RX interrupt and uninstall TX and RX call back function. */
DrvPS2_DisableInt(DRVPS2_TXINT| DRVPS2_RXINT);
DrvPS2_IsIntEnabled
Prototype
uint32_t DrvPS2_IsIntEnabled(uint32_t
u32InterruptFlag);
Description
This function is used to check whether or not interrupt be enabled.
Parameter
u32InterruptFlag [in]
V1.05.002
- 276 -
DrvPS2_ClearIn
Prototype
uint32_t DrvPS2_ClearInt(uint32_t
u32InterruptFlag);
Description
This function is used to clear interrupt status.
Parameter
U32InterruptFlag [in]
Specify Tx/Rx interrupt flag that will be cleared. It can be DRVPS2_TXINT or
DRVPS2_RXINT or DRVPS2_TXINT| DRVPS2_RXINT
Include
Driver/DrvPS2.h
Return Value
V1.05.002
- 277 -
E_SUCCESS: Success.
Example
/* Clear TX interrupt. */
DrvPS2_ClearInt(DRVPS2_TXINT);
/* Clear RX interrupt. */
DrvPS2_ClearInt(DRVPS2_RXINT);
/* Clear TX and RX interrupt. */
DrvPS2_ClearInt(DRVPS2_TXINT| DRVPS2_RXINT);
DrvPS2_GetIntStatus
Prototype
int8_t DrvPS2_GetIntStatus(uint32_t u32InterruptFlag);
Description
This function is used to check interrupt status. If interrupt that be checked happens it will
return TRUE
Parameter
U32InterruptFlag [in]
Specify TX/RX interrupt flag that will be checked. It can be DRVPS2_TXINT or
DRVPS2_RXINT
Include
Driver/DrvPS2.h
Return Value
TRUE: interrupt that be checked happens
FALSE: interrupt that be checked doesnt happen.
Example
/* Check TX interrupt status */
int8_t i8InterruptStatus;
i8InterruptStatus = DrvPS2_GetIntStatus(DRVPS2_TXINT);
if(i8InterruptStatus==TRUE)
printf(TX interrupt that be checked happens\n);
else
printf(TX interrupt doesnt happen\n);
V1.05.002
- 278 -
DrvPS2_SetTxFIFODepth
Prototype
void DrvPS2_SetTxFIFODepth( uint16_t
u16TxFIFODepth);
Description
This function is used to set TX FIFO depth. The function will call macro DRVPS2_TXFIFO
to set TX FIFO depth
Parameter
u16TxFIFODepth [in]
Specify TX FIFO depth. The range can be [1, 16]
Include
Driver/DrvPS2.h
Return Value
None
Example
/* Set TX FIFO depth to 16 bytes. */
DrvPS2_SetTxFIFODepth(16);
/* Set TX FIFO depth to 1 byte. */
DrvPS2_SetTxFIFODepth(1);
DrvPS2_Read
Prototype
int32_t DrvPS2_Read(uint8_t
*pu8RxBuf);
Description
The function is used to read one byte to the buffer of pu8RxBuf. The function will call macro
DRVPS2_RXDATA to receive data
Parameter
pu8RxBuf [out]
the buffer is used to contain byte received. The size of buffer needs one byte only
Include
Driver/DrvPS2.h
Return Value
E_SUCCESS: Success.
Example
V1.05.002
- 279 -
DrvPS2_Write
Prototype
int32_t
DrvPS2_Write(
uint32_t
*pu32TxBuf,
uint32_t
u32WriteBytes
);
Description
The function is used to write the buffer of pu32TxBuf and the length of u32WriteBytes to
host. If data count sent is less than 16 bytes, please use macro DRVPS2_TXDATAxxx for
speed
Parameter
pu32TxBuf [in]
the data that will be sent to host.
u32WriteBytes [in]
the length of data that will be sent to host.
Include
Driver/DrvPS2.h
Return Value
E_SUCCESS: Success.
Example
/* Write 64 bytes to TX buffer and TX buffer will send the 64 bytes out. */
uint32_t au32TXData[64];
DrvPS2_Write(au32TXData, 64);
DrvPS2_GetVersion
Prototype
int32_t DrvPS2_GetVersion(void);
Description
V1.05.002
- 280 -
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* Get PS/2 driver current version number */
int32_t i32Ps2VersionNum;
i32Ps2VersionNum = DrvPS2_GetVersion ();
V1.05.002
- 281 -
Size
32 KB
64 KB
AP ROM
128 KB
(128-0.5*N) KB
4 KB
4 KB
Data Flash
0 KB
(0.5*N) KB
LD ROM
4KB
User Configuration
2 words
Start Address
0x00000000
0x0001F000
0x0001F000
None
DFBADR
0x00100000
0x00300000
End Address
0x00007FFF
0x0000FFFF
0x0001FFFF if DFEN=0 for 128 KB
(DFBADR-1) if DFEN=1 for 128 KB
0x0001FFFF
0x0001FFFF
None if DFEN=0 for 128 KB
0x0001FFFF if DFEN=1 for 128 KB
0x00100FFF
0x00300004
V1.05.002
- 282 -
0x0001_FFFF
0x0001_FFFF
DataFlash
0.5*N1 k bytes
DFBADR[31:0]
Data Flash
4k bytes
Programmable start
address
0x0001_F000
Data Flash
4k bytes
0x0001_F000
Reserved
Reserved
Application Program
(128-0.5*N)K bytes
Note:
N is the number
of pages.
Application Program
64K bytes
Application Program
32K bytes
Value
Description
E_FMC_APROM
E_FMC_LDROM
0
1
13.4. Functions
DrvFMC_EnableISP
Prototype
void
DrvFMC_EnableISP (void);
Description
V1.05.002
- 283 -
To enable ISP function. This function will check if internal 22M oscillator is enabled or not.
If not, this function will enable 22M oscillator automatically. User can disable 22M oscillator
by using DrvSYS_SetOscCtrl ( ) if needed after ISP finished.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_EnableISP ( );
DrvFMC_DisableISP
Prototype
void
DrvFMC_DisableISP (void);
Description
To disable ISP function.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_DisableISP ( );
V1.05.002
- 284 -
DrvFMC_BootSelect
Prototype
void
DrvFMC_BootSelect(E_FMC_BOOTSELECT boot);
Description
To select next booting from APROM or LDROM.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
boot [in]
Specify E_FMC_APROM or E_FMC_LDROM.
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_BootSelect (E_FMC_LDROM);
DrvFMC_BootSelect (E_FMC_APROM);
DrvFMC_GetBootSelect
Prototype
E_FMC_BOOTSELECT
DrvFMC_GetBootSelect(void);
Description
To get current boot select setting.
Parameter
None.
Include
Driver/DrvFMC.h
Return Value
E_FMC_APROM
E_FMC_LDROM
V1.05.002
- 285 -
Example
E_FMC_BOOTSELECT e_bootSelect
/* Check this booting is from APROM or LDROM */
e_bootSelect = DrvFMC_GetBootSelect ( );
DrvFMC_EnableLDUpdate
Prototype
void
DrvFMC_EnableLDUpdate (void);
Description
To enable LDROM update function. LDROM can be updated if LDROM update function is
enabled when the MCU runs in APROM.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_EnableLDUpdate ( );
DrvFMC_DisableLDUpdate
Prototype
void
DrvFMC_DisableLDUpdate (void);
Description
To disable LDROM update function.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
V1.05.002
- 286 -
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_DisableLDUpdate ( );
DrvFMC_EnableConfigUpdate
Prototype
void
DrvFMC_EnableConfigUpdate (void);
Description
To enable Config update function. If Congif update function is enabled, the user
configuration can be update regardless of MCU is running in APROM or LDROM.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_EnableConfigUpdate ( );
DrvFMC_DisableConfigUpdate
Prototype
void
DrvFMC_DisableConfigUpdate (void);
Description
To disable Config update function.
V1.05.002
- 287 -
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_DisableConfigUpdate ( );
DrvFMC_EnableAPUpdate
Prototype
void
DrvFMC_EnableAPUpdate (void);
Description
To enable APROM update function. APROM can be updated if APROM update function is
enabled when the MCU runs in APROM.
Note 1
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) support this function. Please
refer to NuMicroTM NUC100 Series Products Selection Guide of Appendix in details.
Note 2
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_EnableAPUpdate ( );
- 288 -
DrvFMC_DisableAPUpdate
Prototype
void
DrvFMC_DisableAPUpdate (void);
Description
To disable APROM update function.
Note 1
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) support this function. Please
refer to NuMicroTM NUC100 Series Products Selection Guide of Appendix in details.
Note 2
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_DisableAPUpdate ( );
DrvFMC_EnablePowerSaving
Prototype
void
DrvFMC_EnablePowerSaving (void);
Description
To enable flash access power saving function. If CPU clock is slower than 24 MHz, user can
enable flash power saving function.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
V1.05.002
- 289 -
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_EnablePowerSaving ( );
DrvFMC_DisablePowerSaving
Prototype
void
DrvFMC_DisablePowerSaving (void);
Description
To disable flash access power saving function.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
DrvFMC_DisablePowerSaving ( );
DrvFMC_Write
Prototype
int32_t
Description
To write word data into APROM, LDROM, Data Flash or Config. The Memory Map of
APROM and Data Flash are depended on the product of NuMicroTM NUC100 series. Please
refer to NuMicroTM NUC100 Series Products Selection Guide of Appendix for Flash size.
The corresponding function in Config0 and Config1 are described in FMC Section of TRM in
details.
V1.05.002
- 290 -
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u32addr [in]
Word address of APROM, LDROM, Data Flash or Config.
u32data [in]
Word data to be programmed into APROM, LDROM, Data Flash or Config.
Include
Driver/DrvFMC.h
Return Value
0: Succeed
<0: Failed
Example
/* Program word data 0x12345678 into address 0x1F000 */
DrvFMC_Write (0x1F000, 0x12345678);
DrvFMC_Read
Prototype
int32_t
Description
To read data from APROM, LDROM, Data Flash or Config. The Memory Map of APROM
and Data Flash are depended on the product of NuMicroTM NUC100 series. Please refer to
NuMicroTM NUC100 Series Products Selection Guide of Appendix for Flash size.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u32addr [in]
Word address of APROM, LDROM, Data Flash or Config.
u32data [in]
The word data to store data from APROM, LDROM, Data Flash or Config.
Include
V1.05.002
- 291 -
Driver/DrvFMC.h
Return Value
0: Succeed
<0: Failed
Example
uint32_t u32Data;
/* Read word data from address 0x1F000, and read data is stored to u32Data */
DrvFMC_Read (0x1F000, &u32Data);
DrvFMC_Erase
Prototype
int32_t
Description
To page erase APROM, LDROM, Data Flash or Config. The flash page erase unit is 512
bytes. The Memory Map of APROM and Data Flash are depended on the product of
NuMicroTM NUC100 series. Please refer to NuMicroTM NUC100 Series Products Selection
Guide of Appendix for Flash size.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u32addr [in]
Flash page base address of APROM, LDROM and Data Flash, or Config0 addrsss.
Include
Driver/DrvFMC.h
Return Value
0: Succeed
<0: Failed
Example
/* Page Erase from 0x1F000 to 0x1F1FF */
DrvFMC_Erase (0x1F000);
DrvFMC_WriteConfig
Prototype
V1.05.002
- 292 -
int32_t
Description
To erase Config and write data into Config0 and Config1. The corresponding functions in
Config0 and Config1 are described in FMC Section of TRM in details.
Note
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
u32data0 [in]
Word data to be programmed into Config0.
u32data1 [in]
Word data to be programmed into Config1.
Include
Driver/DrvFMC.h
Return Value
0: Succeed
<0: Failed
Example
/* Program word data 0xFFFFFFFE into Config0 and word data 0x1E000 into Config1 */
DrvFMC_Config (0xFFFFFFFE, 0x1E000);
DrvFMC_ReadDataFlashBaseAddr
Prototype
uint32_t DrvFMC_ReadDataFlashBaseAddr (void);
Description
To read data flash base address. For 128k bytes flash device, the base address of data flash is
defined by user in Config1. For less 128k bytes flash device, the base address is fixed at
0x1F000.
Parameter
None
Include
Driver/DrvFMC.h
Return Value
V1.05.002
- 293 -
DrvFMC_EnableLowFreqOptMode
Prototype
void
DrvFMC_EnableLowFreqOptMode (void);
Description
To enable flash access low frequency optimization mode. It can improve flash access
performance when CPU runs at low frequency.
Note 1
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) and Low Density series
support this function. Please refer to NuMicroTM NUC100 Series Products Selection Guide of
Appendix in details. And, Set this bit only when HCLK 25MHz. If HCLK > 25MHz, CPU
will fetch wrong code and cause fail result.
Note 2
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
/* Enable flash access low frequency optimization mode */
DrvFMC_EnableLowFreqOptMode ( );
DrvFMC_DisableLowFreqOptMode
Prototype
void
DrvFMC_DisableLowFreqOptMode (void);
V1.05.002
- 294 -
Description
To disable flash access low frequency optimization mode.
Note 1
Only NuMicroTM NUC1x0xxxCx series (Ex. NUC140VE3CN) and Low Density series
support this function. Please refer to NuMicroTM NUC100 Series Products Selection Guide of
Appendix in details.
Note 2
Please make sure that the Register Write-Protection function has been unlocked before using
this API. User can check the status of the Register Write-Protection function with
DrvSYS_IsProtectedRegLocked ( ).
Parameter
None
Include
Driver/DrvFMC.h
Return Value
None
Example
/* Disable flash access low frequency optimization mode */
DrvFMC_DisableLowFreqOptMode ( );
DrvFMC_GetVersion
Prototype
uint32_t DrvFMC_GetVersion (void);
Description
Get this modules version.
Parameter
None
Include
Driver/DrvFMC.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
V1.05.002
- 295 -
14.2. Feature
V1.05.002
- 296 -
USB
Driver
USB
Descriptors
Endpoint
Configuration
Class Function
Handlers
USB
Initiator
Control Pipe
Handlers
USB Event
Handlers
USB
Controller
(un)Plug
Event
Wakeup
Event
Bus
Events
USB
Events
Endpoint
Events
V1.05.002
- 297 -
Value
Description
USBD_INTEN
0x40060000
USBD_INTSTS
0x40060004
USBD_FADDR
0x40060008
USBD_EPSTS
0x4006000C
USBD_ATTR
0x40060010
USBD_FLDETB
0x40060014
USBD_BUFSEG
0x40060018
USBD_BUFSEG0
0x40060020
USBD_MXPLD0
0x40060024
V1.05.002
- 298 -
Constant Name
Value
Description
USBD_CFG0
0x40060028
USBD_CFGP0
0x4006002C
USBD_BUFSEG1
0x40060030
USBD_MXPLD1
0x40060034
USBD_CFG1
0x40060038
USBD_CFGP1
0x4006003C
USBD_BUFSEG2
0x40060040
USBD_MXPLD2
0x40060044
USBD_CFG2
0x40060048
USBD_CFGP2
0x4006004C
USBD_BUFSEG3
0x40060050
USBD_MXPLD3
0x40060054
USBD_CFG3
0x40060058
USBD_CFGP3
0x4006005C
USBD_BUFSEG4
0x40060060
USBD_MXPLD4
0x40060064
USBD_CFG4
0x40060068
USBD_CFGP4
0x4006006C
USBD_BUFSEG5
0x40060070
USBD_MXPLD5
0x40060074
USBD_CFG5
0x40060078
USBD_CFGP5
0x4006007C
USBD_DRVSE0
0x40060090
USB_SRAM_BASE
0x40060100
Value
Description
INTEN_INNAK
0x00008000
Active NAK interrupt function and its status flag for IN token
INTEN_WAKEUP_EN 0x00000100
INTEN_WAKEUP_IE
0x00000008
INTEN_FLDET_IE
0x00000004
INTEN_USB_IE
0x00000002
Constant Name
Value
Description
INTEN_BUS_IE
0x00000001
Value
Description
INTSTS_SETUP
0x80000000
INTSTS_EPEVT5
0x00200000
INTSTS_EPEVT4
0x00100000
INTSTS_ EPEVT 3
0x00080000
INTSTS_ EPEVT 2
0x00040000
INTSTS_ EPEVT 1
0x00020000
INTSTS_ EPEVT 0
0x00010000
INTSTS_WAKEUP_STS 0x00000008
INTSTS_FLDET_STS
0x00000004
INTSTS_USB_STS
0x00000002
INTSTS_BUS_STS
0x00000001
Value
Description
ATTR_BYTEM
0x00000400
ATTR_PWRDN
0x00000200
ATTR_DPPU_EN
0x00000100
ATTR_USB_EN
0x00000080
ATTR_RWAKEUP
0x00000020
Remote Wake Up
ATTR_PHY_EN
0x00000010
ATTR_TIMEOUT
0x00000008
ATTR_RESUME
0x00000004
Resume Status
ATTR_SUSPEND
0x00000002
Suspend Status
ATTR_USBRST
0x00000001
Value
Description
CFG_CSTALL
0x00000200
V1.05.002
- 300 -
Constant Name
Value
Description
CFG_DSQ_SYNC
0x00000080
CFG_STATE
0x00000060
Endpoint STATE
CFG_EPT_IN
0x00000040
IN endpoint
CFG_EPT_OUT
0x00000020
Out endpoint
CFG_ISOCH
0x00000010
Isochronous Endpoint
CFG_EP_NUM
0x0000000F
Endpoint Number
Value
Description
CFGP_SSTALL
0x00000002
CFGP_CLRRDY
0x00000001
Clear Ready
14.6. Macro
_DRVUSB_ENABLE_MISC_INT
Prototype
void _DRVUSB_ENABLE_MISC_INT (
uint32_t
u32Flags
);
Description
Enable/Disable miscellaneous interrupts including USB event, Wakeup event, Float-detection
event and bus event.
Parameter
u32Flags [in]
USB interrupt events. It can be following flags.
IEF_WAKEUP: Wakeup interrupt flag.
IEF_FLD: Float-detection interrupts flag.
IEF_USB: USB event interrupt flag.
IEF_BUS: Bus event interrupt flag.
u32Flag = 0 will disable all USB interrupts.
Include
Driver/DrvUsb.h
V1.05.002
- 301 -
Return Value
None
Example
_DRVUSB_ENABLE_MISC_INT(0);
_DRVUSB_ENABLE_WAKEUP
Prototype
void _DRVUSB_ENABLE_WAKEUP (void);
Description
Enable USB wakeup function. If USB wakeup function is enabled, any activity of USB bus
could be used to wakeup CPU from power down.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_ENABLE_WAKEUP(); /* To enable the USB wakeup function */
_DRVUSB_DISABLE_WAKEUP
Prototype
void _DRVUSB_DISABLE_WAKEUP (void);
Description
Disable USB wakeup function. If USB wakeup function is disable, USB cant used to
wakeup up CPU from power down.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
V1.05.002
- 302 -
None
Example
_DRVUSB_DISABLE_WAKEUP(); /* To avoid wakeup CPU by USB */
_DRVUSB_ENABLE_WAKEUP_INT
Prototype
void _DRVUSB_ENABLE_WAKEUP_INT (void);
Description
Enable wakeup interrupt. USB will raise a wakeup event interrupt when wakeup interrupt is
enabled.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
DRVUSB_ENABLE_WAKEUP_INT() /* To enable wakeup event interrupt */
_DRVUSB_DISABLE_WAKEUP_INT
Prototype
void _DRVUSB_DISABLE_WAKEUP_INT (void);
Description
Disable wakeup interrupt to avoid USB raise an interrupt when wakeup from power down.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
V1.05.002
- 303 -
_DRVUSB_ENABLE_FLDET_INT
Prototype
void _DRVUSB_ENABLE_FLDET_INT (void);
Description
Enable float-detection interrupt to raise an interrupt when USB plug-in or un-plug.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_ENABLE_FLDET_INT() /* To enable float-detection interrupt */
_DRVUSB_DISABLE_FLDET_INT
Prototype
void _DRVUSB_DISABLE_FLDET_INT (void);
Description
Disable float-detection interrupt.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_DISABLE_FLDET_INT() /* To disable float-detection interrupt */
V1.05.002
- 304 -
_DRVUSB_ENABLE_USB_INT
Prototype
void _DRVUSB_ENABLE_USB_INT (void);
Description
Enable USB interrupt. It could be used to control USB interrupt only and
_DRVUSB_ENABLE_MISC_INT() can be used to control all USB related interrupts at the
same time.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_ENABLE_USB_INT () /* To enable USB interrupt */
_DRVUSB_DISABLE_USB_INT
Prototype
void _DRVUSB_DISABLE_USB_INT (void);
Description
Disable USB interrupt.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_ DISABLE _USB_INT () /* To disable USB interrupt */
V1.05.002
- 305 -
_DRVUSB_ENABLE_BUS_INT
Prototype
void _DRVUSB_ENABLE_BUS_INT (void);
Description
Enable USB bus interrupt.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_ENABLE_BUS_INT () /* To enable USB bus interrupt */
_DRVUSB_DISABLE_BUS_INT
Prototype
void _DRVUSB_DISABLE_BUS_INT (void);
Description
Disable bus interrupt.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_DISABLE_BUS_INT () /* To disable USB bus interrupt */
_DRVUSB_CLEAR_EP_READY_AND_TRIG_STALL
Prototype
V1.05.002
- 306 -
void _DRVUSB_CLEAR_EP_READY_AND_TRIG_STALL (
uint32_t
u32EPId
);
Description
Clear specified USB endpoint hardware In/Out Ready and respond STALL,
Parameter
u32EPId[in]
EP Identity (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_CLEAR_EP_READY_AND_TRIG_STALL(3)
endpoint identity 3 and let it to response STALL. */
Notes
Here, EP (endpoint) identity means number of USB device hardware, not USB endpoint
number defined by USB standard.
_DRVUSB_CLEAR_EP_READY
Prototype
void _DRVUSB_CLEAR_EP_READY (
uint32_t
u32EPId
);
Description
Clear EP In/Out Ready.
Parameter
u32EPId[in]
EP Identity (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
None
V1.05.002
- 307 -
Example
_DRVUSB_CLEAR_EP_READY(1)
_DRVUSB_SET_SETUP_BUF
Prototype
void _DRVUSB_SET_SETUP_BUF (
uint32_t
u32BufAddr
);
Description
Specify buffer address for Setup transaction. This buffer is used to store setup token data and
its size is fixed to be 8 bytes according to USB standard. Therefore, the buffer address must
be 8 bytes alignment.
Parameter
u32BufAddr [in]
Buffer address for setup token. It could be USB_BA+0x100 ~ USB_BA+0x2F8 where
USB_BA is 0x40060000.
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_SET_SETUP_BUF(0x400602F8)
0x400602F8 */
_DRVUSB_SET_EP_BUF
Prototype
void _DRVUSB_SET_EP_BUF (
uint32_t
u32EPId,
uint32_t
u32BufAddr
);
Description
Specify buffer address for specified hardware endpoint identity and it must be 8 bytes
alignment. This buffer would be used to buffer the data of IN/OUT USB transaction. The
buffer size used by IN/OUT USB transaction is dependent on maximum payload of related
endpoint identity.
Parameter
V1.05.002
- 308 -
u32EPId [in]
EP identity (valid value: 0 ~ 5).
u32BufAddr [in]
Used to set buffer address and valid address is from 0x40060100 ~ 0x400602F8.
Furthermore, buffer address + maximum payload size must less than 0x400602FF.
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_SET_EP_BUF(1, 0x40060100)
to 0x40060100 */
_DRVUSB_TRIG_EP
Prototype
void _DRVUSB_TRIG_EP (
uint32_t
u32EPId,
uint32_t
u32TrigSize
);
Description
Trigger next transaction for specified endpoint identity and the transaction size is also defined
at the same time.
Parameter
u32EPId [in]
EP identity (valid value: 0 ~ 5) for trigger Data In or Out transaction.
u32TrigSize [in]
For Data Out transaction, it means maximum data size transferred from Host; for Data
In transaction, it means how many data transferred to Host.
Include
Driver/DrvUsb.h
Return Value
None
Example
V1.05.002
- 309 -
/* Trigger the transaction of endpoint identity 1 and the transaction payload size is 64 bytes */
_DRVUSB_TRIG_EP (1, 64)
_DRVUSB_GET_EP_DATA_SIZE
Prototype
uint32_t
_DRVUSB_GET_EP_DATA_SIZE (
uint32_t
u32EPId
);
Description
Length of data transmitted to or received from Host for specified endpoint identity.
Parameter
u32EPId [in]
EP identity (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
For IN endpoint: length of data transmitting to host in bytes.
For OUT endpoint: Actual length of data receiving from host in bytes.
Example
/* To get the size of received data of endpoint identity 1. */
size = _DRVUSB_GET_EP_DATA_SIZE(1);
_DRVUSB_SET_EP_TOG_BIT
Prototype
void
_DRVUSB_SET_EP_TOG_BIT (
uint32_t
u32EPId,
int32_t
bData0
)
Description
Specify Data0 or Data1 for specified endpoint identity. This bit will toggle automatically
after Host ACK the IN token.
V1.05.002
- 310 -
Parameter
u32EPId [in]
EP identity (valid value: 0 ~ 5).
bData0 [in]
Specify DATA0 or DATA1 for IN transaction. TRUE is for DATA0, FALSE is for
DATA1.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To set the toggle bit as DATA0 for endpoint identity 1 */
_DRVUSB_SET_EP_TOG_BIT(1, TRUE);
_DRVUSB_SET_EVENT_FLAG
Prototype
void _DRVUSB_SET_EVENT_FLAG (
uint32_t
u32Data
);
Description
Set Interrupt Event Flag to clear them. The interrupt event flags are write one clear.
Parameter
u32Data [in]
Specify the event to be clear. It could be
Events
Value
Description
EVF_SETUP
0x80000000 Got a setup token event
EVF_EPTF5
0x00200000 Got USB event from endpoint identity 5
EVF_EPTF4
0x00100000 Got USB event from endpoint identity 4
EVF_EPTF3
0x00080000 Got USB event from endpoint identity 3
EVF_EPTF2
0x00040000 Got USB event from endpoint identity 2
EVF_EPTF1
0x00020000 Got USB event from endpoint identity 1
EVF_EPTF0
0x00010000 Got USB event from endpoint identity 0
EVF_WAKEUP 0x00000008 Got a wakeup event
EVF_FLD
0x00000004 Got float-detection event
EVF_USB
0x00000002 Got USB event include endpoint events or setup event
EVF_BUS
0x00000001 Got USB bus event
V1.05.002
- 311 -
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_SET_EVENT_FLAG(EVF_BUS); /* Clear USB bus event */
_DRVUSB_SET_EVENT_FLAG(EVF_BUS | EVF_FLD); /* Clear USB bus event and
float-detection event */
_DRVUSB_GET_EVENT_FLAG
Prototype
uint32_t
_DRVUSB_GET_EVENT_FLAG (void);
Description
Get Interrupt Event Flags
Parameter
None
Include
Driver/DrvUsb.h
Return Value
Return EVF register value. Please refer to _DRVUSB_SET_EVENT_FLAG() for detail
event information.
Example
u32Events = _DRVUSB_GET_EVF(); /* Get events */
_DRVUSB_CLEAR_EP_STALL
Prototype
void _DRVUSB_CLEAR_EP_STALL (
uint32_t
u32EPId
);
Description
Stop to force specified endpoint identity to respond STALL to host.
V1.05.002
- 312 -
Parameter
u32EPId [in]
EP identity (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_CLEAR_EP_STALL(1);/* Clear the STALL of endpoint identity 1 */
_DRVUSB_TRIG_EP_STALL
Prototype
void _DRVUSB_TRIG_EP_STALL (
uint32_t
u32EPId
);
Description
Force EPx (x = 0 ~ 5) to response STALL
Parameter
u32EPId[in]
EP identity (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
None
Example
_DRVUSB_TRIG_EP_STALL (1); /* Force to STALL endpoint identity 1 */
_DRVUSB_CLEAR_EP_DSQ_SYNC
Prototype
void _DRVUSB_CLEAR_EP_DSQ_SYNC (
uint32_t
u32EPId
V1.05.002
- 313 -
);
Description
Clear the endpoint toggle bit to DATA0,i.e force the toggle bit to be DATA0. This bit will
toggle automatically after IN token ack from host.
Parameter
u32EPId [in]
EP Identity (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Force the toggle bit of endpoint identity 2 to be DATA0 */
_DRVUSB_CLEAR_EP_DSQ_SYNC (2);
_DRVUSB_SET_CFG
Prototype
void _DRVUSB_SET_CFG (
uint32_t
u32CFGNum,
uint32_t
u32Data
);
Description
This macro is used to set USB CFG register.
Parameter
u32CFGNum [in]
CFG number (valid value: 0 ~ 5).
u32Data [in]
Specify the setting for CFG register.
Include
Driver/DrvUsb.h
Return Value
None
V1.05.002
- 314 -
Example
/* Set USB CFG2 control register as 0x3 */
_DRVUSB_SET_CFG (2, 0x3);
_DRVUSB_GET_CFG
Prototype
uint32_t
_DRVUSB_GET_CFG (
uint32_t
u32CFGNum
);
Description
Get current setting of USB CFG register.
Parameter
u32CFGNum [in]
CFG number (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
Return specified CFG register value
Example
/* Get the setting of USB CFG2 control register */
u32Cfg = _DRVUSB_GET_CFG (3);
_DRVUSB_SET_FADDR
Prototype
void _DRVUSB_SET_FADDR (
uint32_t
u32Addr
)
Description
To set USB device address. The valid address is from 0 ~ 127.
Parameter
V1.05.002
- 315 -
u32Addr [in]
The USB device address and it could be 0 ~ 127.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Set the USB devcie address as 3 */
_DRVUSB_SET_FADDR (3);
_DRVUSB_GET_FADDR
Prototype
uint32_t
_DRVUSB_GET_FADDR (void)
Description
To get USB device address.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
Return USB device address.
Example
/* Get USB devcie address */
u32Addr = _DRVUSB_GET_FADDR ();
_DRVUSB_GET_EPSTS
Prototype
uint32_t
_DRVUSB_GET_EPSTS (void)
V1.05.002
- 316 -
Description
Get USB endpoint states register (EPSTS) value. The states register could be used to idendity
the detail information of USB event. For detail information of EPSTS, please refere to
NuMicroTM Technical Reference Manual.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
Return STS register value
Example
/* Get USB STS register value */
u32Reg = _DRVUSB_GET_STS();
_DRVUSB_SET_CFGP
Prototype
void _DRVUSB_SET_CFGP(
uint8_t
u8CFGPNum,
uint32_t
u32Data
);
Description
To set extra configuration register (CFGP). The CFGP register could be used to STALL the
endpoint and clear endpoint ready flag.
CFGP[1]: STALL control bit. Set 1 to force the endpoint to response STALL to host.
CFGP[0]: Ready flag and it is write one clear.
Parameter
u8CFGPNum[in]
CFGP register number (valid value: 0 ~ 5).
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
V1.05.002
- 317 -
None
Example
/* To STALL the endpoint identity 1. */
_DRVUSB_SET_CFGP(1, 0x2);
_DRVUSB_GET_CFGP
Prototype
uint32_t
_DRVUSB_GET_CFGP (
uint32_t
u32CFGPNum
);
Description
Get the value of extra configuration register (CFGP)
Parameter
u32CFGPNum[in]
CFGP register number (valid value: 0 ~ 5).
Include
Driver/DrvUsb.h
Return Value
Return CFGP register value
Example
/* Get the register value of CFG1 */
_DRVUSB_GET_CFGP(1);
_DRVUSB_ENABLE_USB
Prototype
void _DRVUSB_ENABLE_USB (void)
Description
Enable USB, PHY and use remote wake-up
Parameter
V1.05.002
- 318 -
None
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Enable USB, PHY and remote wakeup. */
_DRVUSB_ENABLE_USB();
_DRVUSB_DISABLE_USB
Prototype
void _DRVUSB_DISABLE_USB (void)
Description
Disable USB, PHY but still enable remote wake-up
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Disable USB, PHY but still enable remote wakeup. */
_DRVUSB_DISABLE_USB();
_DRVUSB_DISABLE_PHY
Prototype
void _DRVUSB_DISABLE_PHY (void)
Description
Disable PHY and remote wake-up.
Parameter
V1.05.002
- 319 -
None
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Disable PHY and remote wakeup. */
_DRVUSB_DISABLE_PHY();
_DRVUSB_ENABLE_SE0
Prototype
void _DRVUSB_ENABLE_SE0 (void)
Description
Force USB to drive SE0 to bus. It can be used to simulate unplug event to let host re-connect
to device. For more information about SE0, please refer to USB standard.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Force bus to be SE0 state */
_DRVUSB_ENABLE_SE0();
_DRVUSB_DISABLE_SE0
Prototype
void _DRVUSB_DISABLE_SE0 (void)
Description
Stop to drive SE0 to USB bus.
V1.05.002
- 320 -
Parameter
None
Include
Driver/DrvUsb.h
Return Value
None
Example
/* Stop to drive SE0 state to USB bus */
_DRVUSB_DISABLE_SE0();
_DRVUSB_SET_CFG_EP0
Prototype
void _DRVUSB_SET_CFG_EP0 (
uint32_t
u32Data
)
Description
Stall control and clear In/out ready flag of endpoint identity 0. Please refer to
_DRVUSB_SET_CFGP() for the bit definition of CFGP register.
Parameter
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To STALL endpoint identity 0 */
_DRVUSB_SET_CFG_EP0(0x2);
_DRVUSB_SET_CFG_EP1
Prototype
V1.05.002
- 321 -
void _DRVUSB_SET_CFG_EP1 (
uint32_t
u32Data
)
Description
Stall control and clear In/out ready flag of endpoint identity 1. Please refer to
_DRVUSB_SET_CFGP() for the bit definition of CFGP register.
Parameter
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To STALL endpoint identity 1 */
_DRVUSB_SET_CFG_EP1(0x2);
_DRVUSB_SET_CFG_EP2
Prototype
void _DRVUSB_SET_CFG_EP2 (
uint32_t
u32Data
)
Description
Stall control and clear In/out ready flag of endpoint identity 2. Please refer to
_DRVUSB_SET_CFGP() for the bit definition of CFGP register.
Parameter
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
None
V1.05.002
- 322 -
Example
/* To STALL endpoint identity 2 */
_DRVUSB_SET_CFG_EP2(0x2);
_DRVUSB_SET_CFGP3
Prototype
void _DRVUSB_SET_CFG_EP3 (
uint32_t
u32Data
)
Description
Stall control and clear In/out ready flag of endpoint identity 3. Please refer to
_DRVUSB_SET_CFGP() for the bit definition of CFGP register.
Parameter
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To STALL endpoint identity 3 */
_DRVUSB_SET_CFG_EP3(0x2);
_DRVUSB_SET_CFGP4
Prototype
void _DRVUSB_SET_CFG_EP4 (
uint32_t
u32Data
)
Description
Stall control and clear In/out ready flag of endpoint identity 4. Please refer to
_DRVUSB_SET_CFGP() for the bit definition of CFGP register.
Parameter
V1.05.002
- 323 -
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To STALL endpoint identity 4 */
_DRVUSB_SET_CFG_EP4(0x2);
_DRVUSB_SET_CFGP5
Prototype
void _DRVUSB_SET_CFG_EP5 (
uint32_t
u32Data
)
Description
Stall control and clear In/out ready flag of endpoint identity 5. Please refer to
_DRVUSB_SET_CFGP() for the bit definition of CFGP register.
Parameter
u32Data [in]
Specify data in CFGP register to STALL the endpoint or clear ready flag.
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To STALL endpoint identity 5 */
_DRVUSB_SET_CFG_EP5(0x2);
V1.05.002
- 324 -
14.7. Functions
DrvUSB_GetVersion
Prototype
uint32_t
DrvUSB_GetVersion (void);
Description
Get this modules version.
Parameter
None
Include
Driver/DrvUsb.h
Return Value
Version number :
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* To get modules version */
u32Version = DrvUSB_GetVersion();
DrvUSB_Open
Prototype
int32_t
DrvUsb_Open (
void *
pVoid
)
Description
This function is used to reset USB controller, initial the USB endpoints, interrupts, and USB
driver structures. It also used to call the relative handler when the USB is attached before
USB driver opened. The user must provide the materials before they can call DrvUSB_Open,
including sEpDescription, g_sBusOps.
V1.05.002
- 325 -
sEpDescription:
The structure type of sEpDescription is as follows:
typedef struct
{
//bit7 is directory bit, 1: input; 0: output
uint32_t
u32EPAddr;
uint32_t
u32MaxPacketSize;
uint8_t * u8SramBuffer;
}S_DRVUSB_EP_CTRL;
This structure is used to set the endpoint number, maximum packet size, and buffer of
specified endpoint hardware. There are 6 endpoints hardware available in NUC100 series
USB controller.
g_sBusOps:
The structure type of g_sBusOps is as follows:
typedef struct
{
PFN_DRVUSB_CALLBACK
apfnCallback;
void *
apCallbackArgu;
}S_DRVUSB_EVENT_PROCESS
{NULL, NULL},
{DrvUSB_BusResetCallback, &g_HID_sDevice},
{NULL, NULL},
{NULL, NULL},
{DrvUSB_CtrlSetupAck, &g_HID_sDevice},
};
Parameter
pVoid
NULL
Callback function
None
If the pVoid is not NULL, it will be the callback function of USB
V1.05.002
- 326 -
DrvUSB_Close
Prototype
void
DrvUSB_Close (void);
Description
Close USB controller and disable USB interrupt.
Include
Driver/DrvUSB.h
Return Value
None
Example
/* To close USB device */
DrvUSB_Close();
DrvUSB_PreDispatchEvent
Prototype
void DrvUSB_PreDispatchEvent(void);
Description
Pre-dispatch event base on EVF register.
Parameter
None
V1.05.002
- 327 -
Include
Driver/DrvUsb.h
Return Value
None
Example
/* To pre dispatch USB device events at IRQ handler */
USBD_IRQHandler()
{
DrvUSB_PreDispatchEvent();
}
DrvUSB_DispatchEvent
Prototype
void DrvUSB_DispatchEvent(void)
Description
Dispatch misc and endpoint event. Misc event include attach/detach/bus reset/bus
suspend/bus resume and setup ACK, Misc event's handler is defined by g_sBusOps[]. The
user must provide g_sBusOps[] before using USB driver.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* To dispatch USB events to handle them by related callback funcitons. */
DrvUSB_DispatchEvent();
DrvUSB_IsData0
Prototype
int32_t DrvUSB_IsData0(uint32_t u32EpId)
Description
To check if the current DATA is DATA0. If it is false, then it should be DATA1.
V1.05.002
- 328 -
Parameter
u32EpId
Include
Driver/DrvUSB.h
Return Value
TRUE
FALSE
Example
/* Get toggle bit of endpoint identity 2 */
if(DrvUSB_IsData0(2) )
{
/* The toggle bit of endpoint identity 2 is DATA0 */
}
DrvUSB_GetUsbState
Prototype
E_DRVUSB_STATE DrvUSB_GetUsbState(void)
Description
Get current USB state E_DRVUSB_STATE. The status list as follows:
USB Status
eDRVUSB_DETACHED
eDRVUSB_ATTACHED
eDRVUSB_POWERED
eDRVUSB_DEFAULT
eDRVUSB_ADDRESS
eDRVUSB_CONFIGURED
eDRVUSB_SUSPENDED
Description
The USB has been detached.
The USB has been attached.
The USB is powered.
The USB is in normal state.
The USB is in ADDRESS state.
The USB is in CONFIGURATION state.
The USB is suspended.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
To return the current USB state.
Example
/* Get current USB state */
eUsbState = DrvUSB_GetUsbState();
V1.05.002
- 329 -
if (eUsbState == eDRVUSB_DETACHED)
{
/* USB unplug */
}
DrvUSB_SetUsbState
Prototype
void DrvUSB_SetUsbState(E_DRVUSB_STATE eUsbState)
Description
To change current USB state. Please refer to DrvUSB_GetUsbState for available states.
Parameter
eUsbState
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Set current USB state */
DrvUSB_SetUsbState(eDRVUSB_DETACHED);
DrvUSB_GetEpIdentity
Prototype
uint32_t DrvUSB_GetEpIdentity(uint32_t u32EpNum, uint32_t u32EpAttr)
Description
To get endpoint index base on endpoint number and direction. The endpoint id is used to
identify the hardware endpoint resource. The range of endpoint index could be 0 ~ 5. The
endpoint number is assigned by software and it could be 0 ~ 15 according to USB standard.
Host will access the device through relative endpoint number.
Parameter
u32EpNum
u32EpAttr
Include
Driver/DrvUSB.h
Return Value
V1.05.002
- 330 -
0~5
otherwise
Example
/* Get the hardware endpoint identity of USB OUT endpoint 3 */
u32EpId = DrvUSB_GetEpIdentity(3, EP_OUTPUT);
DrvUSB_GetEpId
Prototype
uint32_t DrvUSB_GetEpId(uint32_t u32EpNum)
Description
Get endpoint index base on endpoint address. This argument u32EpNum is different from
DrvUSB_GetEPIdentity's because its argument includes direction bit (bit 7). eg: 0x81. If the
bit 7 is high, it indicates this is EP_INPUT, otherwise it is EP_OUTPUT.
Parameter
u32EpNum
Include
Driver/DrvUSB.h
Return Value
0~5
otherwise
Example
/* Get the hardware endpoint identity of USB IN endpoint 4 */
u32EpId = DrvUSB_GetEpIdentity(0x84);
DrvUSB_DataOutTrigger
Prototype
int32_t DrvUSB_DataOutTrigger(uint32_t u32EpNum, uint32_t u32Size)
Description
Trigger data out ready flag by write MXPLD register. It indicates the relative endpoint buffer
is ready to receive data out packet.
Parameter
V1.05.002
- 331 -
u32EpNum
u32Size
Include
Driver/DrvUSB.h
Return Value
0
Succeed
<0
Example
/* Trigger endpoint number 2 to receive OUT packet of host and the maximum packet size is
64 bytes */
DrvUSB_DataOutTrigger(2, 64);
DrvUSB_GetOutData
Prototype
uint8_t * DrvUSB_GetOutData(uint32_t u32EpNum, uint32_t *u32Size)
Description
This function will return the buffer pointer of u32EpNum 's out USB SRAM buffer. User can
use this pointer to get the data payload of current data out packet.
Parameter
u32EpNum
u32Size
Include
Driver/DrvUSB.h
Return Value
To return USB SRAM address.
Example
/* Get the buffer address and size of received data of endpoint number 2 */
pu8EpBuf = DrvUSB_GetOutData(2, &u32Size);
DrvUSB_DataIn
Prototype
V1.05.002
- 332 -
u8Buffer
u32Size
Include
Driver/DrvUSB.h
Return Value
0
Successful
E_DRVUSB_SIZE_TOO_LONG
Example
/* Prepare 2 bytes data for endpoint number 0 IN transaction. */
DrvUSB_DataIn(0, au8Data, 2);
DrvUSB_BusResetCallback
Prototype
void DrvUSB_BusResetCallback(void * pVoid)
Description
Bus reset handler. After receiving bus reset event, this handler will be called. It will reset
USB address, accept SETUP packet and initial the endpoints.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
V1.05.002
- 333 -
DrvUSB_InstallClassDevice
Prototype
void * DrvUSB_InstallClassDevice(S_DRVUSB_CLASS *sUsbClass)
Description
Register USB class device to USB driver.
Parameter
sUsbClass
Include
Driver/DrvUSB.h
Return Value
Return USB driver pointer
Example
/* Register USB class device to USB driver. */
g_HID_sDevice.device = (void *)DrvUSB_InstallClassDevice(&sHidUsbClass);
DrvUSB_InstallCtrlHandler
Prototype
int32_t DrvUSB_InstallCtrlHandler(
void * *device,
S_DRVUSB_CTRL_CALLBACK_ENTRY
uint32_t u32RegCnt
*psCtrlCallbackEntry,
)
Description
V1.05.002
- 334 -
Register ctrl pipe handler including SETUP ACK , IN ACK, OUT ACK handle for
Standard/Vendor/Class command.
Parameter
device
psCtrlCallbackEntry
u32RegCnt
Include
Driver/DrvUSB.h
Return Value
0
Success
E_DRVUSB_NULL_POINTER
Example
/* Register ctrl pipe handler. */
i32Ret = DrvUSB_InstallCtrlHandler(g_HID_sDevice.device, g_asCtrlCallbackEntry,
sizeof(g_asCtrlCallbackEntry) / sizeof(g_asCtrlCallbackEntry[0]));
DrvUSB_CtrlSetupAck
Prototype
void DrvUSB_CtrlSetupAck(void * pArgu)
Description
When SETUP ack interrupt happen, this function will be called. It will call SETUP handler
that DrvUSB_InstallCtrlHandler registered base on command category and command.
Parameter
pArgu
Include
Driver/DrvUSB.h
Return Value
None
Example
/* bus event call back */
S_DRVUSB_EVENT_PROCESS g_sBusOps[6] =
{
{NULL, NULL},
{NULL, NULL},
- 335 -
{DrvUSB_BusResetCallback, &g_HID_sDevice},
{NULL, NULL},
{NULL, NULL},
{DrvUSB_CtrlSetupAck, &g_HID_sDevice},
};
DrvUSB_CtrlDataInAck
Prototype
void DrvUSB_CtrlDataInAck(void * pArgu)
Description
When IN ack interrupt happen, this function will be called. It will call IN ACK handler that
DrvUSB_InstallCtrlHandler registered base on command category and command.
Parameter
pArgu
Include
Driver/DrvUSB.h
Return Value
None
Example
/* USB event call back */
S_DRVUSB_EVENT_PROCESS g_sUsbOps[12] =
{
{DrvUSB_CtrlDataInAck, &g_HID_sDevice},/* ctrl pipe0 (EP address 0) In ACK callback */
{DrvUSB_CtrlDataOutAck, &g_HID_sDevice},/* ctrl pipe0 (EP address 0) Out ACK callback */
{HID_IntInCallback, &g_HID_sDevice},/* EP address 1 In ACK callback */
{NULL, NULL},
/* EP address 1 Out ACK callback */
{NULL, NULL},
/* EP address 2 In ACK callback */
{HID_IntOutCallback, &g_HID_sDevice},/* EP address 2 Out ACK callback */
{NULL, NULL},
/* EP address 3 In ACK callback */
{NULL, NULL},
/* EP address 3 Out ACK callback */
{NULL, NULL},
/* EP address 4 In ACK callback */
{NULL, NULL},
/* EP address 4 Out ACK callback */
{NULL, NULL},
/* EP address 5 In ACK callback */
{NULL, NULL},
/* EP address 5 Out ACK callback */
};
DrvUSB_CtrlDataOutAck
Prototype
void DrvUSB_CtrlDataOutAck(void * pArgu)
Description
When OUT ack interrupt happen, this function will be called. It will call OUT handler that
DrvUSB_RegisterCtrl registered base on command category and command.
V1.05.002
- 336 -
Parameter
pArgu
Include
Driver/DrvUSB.h
Return Value
None
Example
/* USB event call back */
S_DRVUSB_EVENT_PROCESS g_sUsbOps[12] =
{
{DrvUSB_CtrlDataInAck
, &g_HID_sDevice},/* ctrl pipe0 (EP address 0) In ACK callback */
{DrvUSB_CtrlDataOutAck , &g_HID_sDevice},/* ctrl pipe0 (EP address 0) Out ACK callback */
{HID_IntInCallback
, &g_HID_sDevice},/* EP address 1 In ACK callback */
{NULL, NULL},
/* EP address 1 Out ACK callback */
{NULL, NULL},
/* EP address 2 In ACK callback */
{HID_IntOutCallback
, &g_HID_sDevice},/* EP address 2 Out ACK callback */
{NULL, NULL},
/* EP address 3 In ACK callback */
{NULL, NULL},
/* EP address 3 Out ACK callback */
{NULL, NULL},
/* EP address 4 In ACK callback */
{NULL, NULL},
/* EP address 4 Out ACK callback */
{NULL, NULL},
/* EP address 5 In ACK callback */
{NULL, NULL},
/* EP address 5 Out ACK callback */
};
DrvUSB_CtrlDataInDefault
Prototype
void DrvUSB_CtrlDataInDefault(void * pVoid)
Description
IN ACK default handler. It is used to return ACK for next OUT token.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
/* If no control data IN callback installed, just use default one */
if (psEntry->pfnCtrlDataInCallback == NULL)
psEntry->pfnCtrlDataInCallback = DrvUSB_CtrlDataInDefault;
V1.05.002
- 337 -
DrvUSB_CtrlDataOutDefault
Prototype
void DrvUSB_CtrlDataOutDefault(void * pVoid)
Description
OUT ACK default handler. It is used to return zero data length packet when next IN token.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
/* If no control data OUT callback installed, just use default one */
if (psEntry->pfnCtrlDataOutCallback == NULL)
psEntry->pfnCtrlDataOutCallback = DrvUSB_CtrlDataOutDefault;
DrvUSB_Reset
Prototype
void DrvUSB_Reset(uint32_t u32EpNum)
Description
Restore the specified CFGx and CFGPx registers according the endpoint number.
Parameter
u32EpNum
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Reset endpoint number 2 */
DrvUSB_Reset(2);
V1.05.002
- 338 -
DrvUSB_ClrCtrlReady
Prototype
void DrvUSB_ClrCtrlReady(void)
Description
Clear ctrl pipe ready flag that was set by MXPLD.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Clear control endpoint ready flag */
DrvUSB_ClrCtrlReady();
DrvUSB_ClrCtrlReadyAndTrigStall
Prototype
void DrvUSB_ClrCtrlReadyAndTrigStall(void);
Description
Clear control pipe ready flag that was set by MXPLD and send STALL.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Clear control pipe ready flag that was set by MXPLD and send STALL. */
DrvUSB_ClrCtrlReadyAndTrigStall();
V1.05.002
- 339 -
DrvUSB_GetSetupBuffer
Prototype
uint32_t DrvUSB_GetSetupBuffer(void)
Description
Get setup buffer address of USB SRAM to read the received setup packet data.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
Setup buffer address
Example
/* Get setup buffer address of USB SRAM. */
SetupBuffer = (uint8_t *)DrvUSB_GetSetupBuffer();
DrvUSB_GetFreeSRAM
Prototype
uint32_t DrvUSB_GetFreeSRAM(void)
Description
Get free USB SRAM buffer address after EP assign base on
sEpDescription[i].u32MaxPacketSize in DrvUSB_Open. User can get this for dual buffer.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
Free USB SRAM address
Example
/* Get the base address of free USB SRAM */
u32BaseAddr = DrvUSB_GetFreeSRAM();
V1.05.002
- 340 -
DrvUSB_EnableSelfPower
Prototype
void DrvUSB_EnableSelfPower(void)
Description
Enable self-power attribution of USB device.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Set a flag to note the USB device is self-power */
DrvUSB_EnableSelfPower();
DrvUSB_DisableSelfPower
Prototype
void DrvUSB_DisableSelfPower(void)
Description
Disable self-power attribution of USB device.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Clear the flag to note the USB device is not self-power */
DrvUSB_ DisableSelfPower ();
V1.05.002
- 341 -
DrvUSB_IsSelfPowerEnabled
Prototype
int32_t DrvUSB_IsSelfPowerEnabled(void)
Description
Self-power is enable or disable.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
TRUE
FALSE
Example
/* Check if the USB device is self-power */
if(DrvUSB_IsSelfPowerEnabled())
{
/* The USB device is self-power */
}
DrvUSB_EnableRemoteWakeup
Prototype
void DrvUSB_EnableRemoteWakeup(void)
Description
Enable remote wakeup attribution of USB device.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Set the flag to note the USB device supports remote wakeup */
V1.05.002
- 342 -
DrvUSB_EnableRemoteWakeup();
DrvUSB_DisableRemoteWakeup
Prototype
void DrvUSB_DisableRemoteWakeup(void)
Description
Disable remote wakeup attribution.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Clear the flag to note the USB device doesnt support remote wakeup */
DrvUSB_DisableRemoteWakeup();
DrvUSB_IsRemoteWakeupEnabled
Prototype
int32_t DrvUSB_IsRemoteWakeupEnabled (int32_t * pbVoid)
Description
Return remote wakeup is enabling or disable.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
TRUE
FALSE
Example
/* Check if the USB device supports remote wakeup. */
V1.05.002
- 343 -
DrvUSB_SetMaxPower
Prototype
int32_t DrvUSB_SetMaxPower(uint32_t u32MaxPower)
Description
Configure max power. The unit is 2mA. Maximum MaxPower 0xFA (500mA), default is
0x32 (100mA)
Parameter
u32MaxPower
Include
Driver/DrvUSB.h
Return Value
0: Successful
<0: Wrong maximum value
Example
/* Set the maximum power is 150mA */
DrvUSB_SetMaxPower(75);
DrvUSB_GetMaxPower
Prototype
int32_t DrvUSB_GetMaxPower(void)
Description
Get current max power. The unit is in 2mA, i.e. 0x32 is 100mA.
Parameter
None
Include
Driver/DrvUSB.h
Return Value
Return the maximum power. (2mA unit)
V1.05.002
- 344 -
Example
/* Get the maximum power */
i32Power = DrvUSB_GetMaxPower();
DrvUSB_EnableUSB
Prototype
void DrvUSB_EnableUSB(S_DRVUSB_DEVICE *psDevice)
Description
Enable USB, PHY and remote wakeup.
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Enable USB, PHY and remote wakeup function. */
DrvUSB_EnableUSB(psDevice);
DrvUSB_DisableUSB
Prototype
void DrvUSB_DisableUSB(S_DRVUSB_DEVICE * psDevice)
Description
Disable USB, PHY but keep remote wakeup function on.
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
V1.05.002
- 345 -
Example
/* Enable USB, PHY and remote wakeup function. */
DrvUSB_DisableUSB(psDevice);
DrvUSB_PreDispatchWakeupEvent
Prototype
void DrvUSB_PreDispatchWakeupEvent(S_DRVUSB_DEVICE *psDevice)
Description
Pre-dispatch wakeup event. This function does nothing and reserves for further usage
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
N/A
DrvUSB_PreDispatchFDTEvent
Prototype
void DrvUSB_PreDispatchFDTEvent(S_DRVUSB_DEVICE * psDevice)
Description
Pre-dispatch plug-in and plug-out event
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
V1.05.002
- 346 -
DrvUSB_PreDispatchBusEvent
Prototype
void DrvUSB_PreDispatchBusEvent(S_DRVUSB_DEVICE *psDevice)
Description
Pre-dispatch BUS event
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Pre-dispatch bus event. */
DrvUSB_PreDispatchBusEvent(&gsUsbDevice);
DrvUSB_PreDispatchEPEvent
Prototype
void DrvUSB_PreDispatchEPEvent(S_DRVUSB_DEVICE * psDevice)
Description
Pre-dispatch EP event including IN ACK/IN NAK/OUT ACK/ISO end. This function is used
to recognize endpoint events and record them for further processing of
DrvUSB_DispatchEPEvent(). All EP event handlers are defined at g_sUsbOps[].
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
V1.05.002
- 347 -
/* Clear USB events individually instead of in total. Otherwise, incoming USB events may be
cleared mistakenly. Pre-dispatch USB event. */
DrvUSB_PreDispatchEPEvent(&gsUsbDevice);
DrvUSB_DispatchWakeupEvent
Prototype
void DrvUSB_DispatchWakeupEvent(S_DRVUSB_DEVICE *psDevice)
Description
Dispatch wakeup event. This function does nothing and reserves for further usage.
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
N/A
DrvUSB_DispatchMiscEvent
Prototype
void DrvUSB_DispatchMiscEvent(S_DRVUSB_DEVICE * psDevice)
Description
Dispatch Misc event. The event is set by attach/detach/bus reset/bus suspend/bus resume and
setup ACK. Misc event's handler is defined at g_sBusOps[].
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Parsing the MISC events and call relative handles */
V1.05.002
- 348 -
DrvUSB_DispatchMiscEvent(&gsUsbDevice);
DrvUSB_DispatchEPEvent
Prototype
void DrvUSB_DispatchEPEvent(S_DRVUSB_DEVICE * psDevice)
Description
Dispatch EP event, the event is set by DrvUSB_PreDispatchEPEvent() including IN ACK/IN
NAK/OUT ACK/ISO end. The EP event's handler is defined at g_sUsbOps[].
Parameter
psDevice
Include
Driver/DrvUSB.h
Return Value
None
Example
/* Parsing the endpoint events and call relative handlers */
DrvUSB_ DispatchEPEvent (&gsUsbDevice);
DrvUSB_CtrlSetupSetAddress
Prototype
void DrvUSB_CtrlSetupSetAddress(void * pVoid)
Description
Setup ACK handler for set address command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
/*ctrl pipe call back.*/
V1.05.002
- 349 -
DrvUSB_CtrlSetupClearSetFeature
Prototype
void DrvUSB_CtrlSetupClearSetFeature(void * pVoid)
Description
Setup ACK handler for Clear feature command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, CLEAR_FEATURE, DrvUSB_CtrlSetupClearSetFeature, 0, 0,
&g_HID_sDevice}
};
DrvUSB_CtrlSetupGetConfiguration
Prototype
void DrvUSB_CtrlSetupGetConfiguration(void * pVoid)
Description
Setup ACK handler for Get configuration command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
V1.05.002
- 350 -
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, GET_CONFIGURATION, DrvUSB_CtrlSetupGetConfiguration, 0, 0,
&g_HID_sDevice}
};
DrvUSB_CtrlSetupGetStatus
Prototype
void DrvUSB_CtrlSetupGetStatus(void * pVoid)
Description
Setup ACK handler for Get status command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, GET_STATUS, DrvUSB_CtrlSetupGetStatus, 0, 0, &g_HID_sDevice}
};
DrvUSB_CtrlSetupGetInterface
Prototype
void DrvUSB_CtrlSetupGetInterface(void * pVoid)
Description
Setup ACK handler for Get interface command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
V1.05.002
- 351 -
None
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, GET_INTERFACE, DrvUSB_CtrlSetupGetInterface, 0, 0, &g_HID_sDevice}
};
DrvUSB_CtrlSetupSetInterface
Prototype
void DrvUSB_CtrlSetupSetInterface(void * pVoid)
Description
Setup ACK handler for Set interface command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, SET_INTERFACE, DrvUSB_CtrlSetupSetInterface, 0, 0, &g_HID_sDevice}
};
DrvUSB_CtrlSetupSetConfiguration
Prototype
void DrvUSB_CtrlSetupSetConfiguration(void * pVoid)
Description
Setup ACK handler for Set configuration command.
Parameter
pVoid
Include
Driver/DrvUSB.h
V1.05.002
- 352 -
Return Value
None
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, SET_CONFIGURATION, DrvUSB_CtrlSetupSetConfiguration, 0, 0,
&g_HID_sDevice}
};
DrvUSB_CtrlDataInSetAddress
Prototype
void DrvUSB_CtrlDataInSetAddress(void * pVoid)
Description
Setup ACK handler for Set address command.
Parameter
pVoid
Include
Driver/DrvUSB.h
Return Value
None
Example
S_DRVUSB_CTRL_CALLBACK_ENTRY g_asCtrlCallbackEntry[] =
{ //request type,command,setup ack handler, in ack handler,out ack handler, parameter
{REQ_STANDARD, SET_ADDRESS, DrvUSB_CtrlSetupSetAddress,
DrvUSB_CtrlDataInSetAddress, 0, &g_HID_sDevice}
};
DrvUSB_memcpy
Prototype
void DrvUSB_memcpy(uint8_t *pi8Dest, uint8_t *pi8Src, uint32_t u32Size)
Description
The USB buffer is recommended to be byte access thus this function is implemented by byte
access.
Parameter
pi8Dest: Destination pointer
V1.05.002
- 353 -
V1.05.002
- 354 -
Value
Description
CHANNEL_OFFSET
0x100
Value
Description
eDRVPDMA_CHANNEL_0
PDMA channel 0
V1.05.002
- 355 -
eDRVPDMA_CHANNEL_1
eDRVPDMA_CHANNEL_2
eDRVPDMA_CHANNEL_3
eDRVPDMA_CHANNEL_4
eDRVPDMA_CHANNEL_5
eDRVPDMA_CHANNEL_6
eDRVPDMA_CHANNEL_7
eDRVPDMA_CHANNEL_8
1
2
3
4
5
6
7
8
PDMA channel 1
PDMA channel 2
PDMA channel 3
PDMA channel 4
PDMA channel 5
PDMA channel 6
PDMA channel 7
PDMA channel 8
E_DRVPDMA_DIRECTION_SELECT
Enumeration identifier
Value
eDRVPDMA_DIRECTION_INCREMENTED 0
eDRVPDMA_DIRECTION_FIXED
Description
Source/Destination Address Direction is
incremented.
Source/Destination Address Direction is fixed.
E_DRVPDMA_TRANSFER_WIDTH
Enumeration identifier
Value
eDRVPDMA_WIDTH_32BITS 0
eDRVPDMA_WIDTH_8BITS
eDRVPDMA_WIDTH_16BITS 2
Description
One word is transferred for every PDMA operation in
IP-to-Memory/Memory-to-IP mode.
One byte is transferred for every PDMA operation in
IP-to-Memory/Memory-to-IP mode.
Half word is transferred for every PDMA operation in
IP-to-Memory/Memory-to-IP mode.
E_DRVPDMA_INT_ENABLE
Enumeration identifier
Value
Description
eDRVPDMA_TABORT
eDRVPDMA_BLKD
1
2
E_DRVPDMA_APB_DEVICE
Enumeration identifier
Value
Description
eDRVPDMA_SPI0
eDRVPDMA_SPI1
eDRVPDMA_SPI2
eDRVPDMA_SPI3
eDRVPDMA_UART0
eDRVPDMA_UART1
eDRVPDMA_ADC
eDRVPDMA_I2S
0
1
2
3
4
5
7
8
V1.05.002
- 356 -
E_DRVPDMA_APB_RW
Enumeration identifier
Value
Description
eDRVPDMA_READ_APB
eDRVPDMA_WRITE_APB
0
1
E_DRVPDMA_MODE
Enumeration identifier
Value
Description
eDRVPDMA_MODE_MEM2MEM 0
eDRVPDMA_MODE_APB2MEM 1
eDRVPDMA_MODE_MEM2APB 2
15.5. Functions
DrvPDMA_Init
Prototype
void
DrvPDMA_Init (void);
Description
The function is used to enable AHB PDMA engine clock.
Parameter
None
Include
Driver/DrvPDMA.h
Return Value
None
Example
/* Enable AHB PDMA engine clock */
DrvPDMA_Init();
DrvPDMA_Close
Prototype
void DrvPDMA_Close (void);
V1.05.002
- 357 -
Description
The function is used to disable all PDMA channel clock and AHB PDMA clock
Parameter
None
Include
Driver/DrvPDMA.h
Return Value
None
Example
/* Disable all PDMA channel clock and AHB PDMA clock */
DrvPDMA_Close();
DrvPDMA_CHEnableTransfer
Prototype
int32_t
DrvPDMA_CHEnablelTransfer(
E_DRVPDMA_CHANNEL_INDEX eChannel
);
Description
The function is used to enable PDMA specified channel and enable specified channel data
read or write transfer
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success.
E_DRVPDMA_ERR_PORT_INVALID: Invalid port number
Example
/* Enable PDMA channel0 and enable channel0 data read/write transfer */
DrvPDMA_CHEnableTransfer(eDRVPDMA_CHANNEL_0);
V1.05.002
- 358 -
DrvPDMA_CHSoftwareReset
Prototype
int32_t
DrvPDMA_CHSoftwareReset(
E_DRVPDMA_CHANNEL_INDEX eChannel
);
Description
The function is used to do software reset specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success.
E_DRVPDMA_ERR_PORT_INVALID: Invalid port number
Note
The function will reset the specified channel internal state machine and pointers. The contents
of control register will not be cleared.
Example
/* Software reset PDMA channel0 and get returned value */
int32_t i32RetVal_CH0SoftwareReset;
i32RetVal_CH0SoftwareReset =
DrvPDMA_CH0SoftwareReset(eDRVPDMA_CHANNEL_0);
DrvPDMA_Open
Prototype
int32_t
DrvPDMA_Open(
E_DRVPDMA_CHANNEL_INDEX sChannel,
STR_PDMA_T *sParam
);
V1.05.002
- 359 -
Description
The function configures PDMA setting
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
sParam [in]
The struct parameter to configure PDMA,
It includes
sSrcCtrl.u32Addr : Source Address.(must be word alignment)
sSrcCtrl.eAddrDirection: Source Address Direction
eDRVPDMA_DIRECTION_INCREMENTED: Source address direction is incremented
eDRVPDMA_DIRECTION_FIXED: Source address direction is fixed
sDestCtrl.u32Addr: Destination Address.(must be word alignment)
sDestCtrl.eAddrDirection: Destination Address Direction. It could be
eDRVPDMA_DIRECTION_INCREMENTED / eDRVPDMA_DIRECTION_FIXED.
eDRVPDMA_DIRECTION_INCREMENTED: Destination address direction is incremented
eDRVPDMA_DIRECTION_FIXED: Destination address direction is fixed
u8TransWidth: Peripheral Transfer Width. This field is meaningful only when the operation
mode setting are APB to memory or memory to APB. It could be
eDRVPDMA_WIDTH_8BITS / eDRVPDMA_WIDTH_16BITS /
eDRVPDMA_WIDTH_32BITS.
eDRVPDMA_WIDTH_8BITS: One byte (8 bits) is transferred for every PDMA operation.
eDRVPDMA_WIDTH_16BITS: One half-word (16 bits) is transferred for every PDMA
operation.
eDRVPDMA_WIDTH_32BITS: One word (32 bits) is transferred for every PDMA operation.
u8Mode: Operation Mode
eDRVPDMA_MODE_MEM2MEM: Memory to memory mode.
eDRVPDMA_MODE_APB2MEM: APB to memory mode.
eDRVPDMA_MODE_MEM2APB: memory to APB mode.
i32ByteCnt: PDMA Transfer Byte Count
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
E_DRVPDMA_ERR_PORT_INVALID: Invalid port number
Example
/*---------------------------------------------------------------------------------------*/
V1.05.002
- 360 -
DrvPDMA_ClearIntFlag
Prototype
void
DrvPDMA_ClearIntFlag(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_INT_FLAG eIntFlag
);
Description
The function is used to clear interrupt status for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eIntFlag [in] Interrupt source:
eDRVPDMA_TABORT: Read/Write Target Abort
eDRVPDMA_BLKD: Block Transfer Done
Include
Driver/DrvPDMA.h
V1.05.002
- 361 -
Return Value
None
Example
/* Clear channel0 block transfer done interrupt flag. */
DrvPDMA_ClearIntFlag(eDRVPDMA_CHANNEL_0, eDRVPDMA_BLKD_FLAG);
/* Clear channel1 read/write target abort interrupt flag */
DrvPDMA_ClearIntFlag(eDRVPDMA_CHANNEL_1, eDRVPDMA_TABORT);
DrvPDMA_PollInt
Prototype
int32_t
DrvPDMA_PollInt(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_INT_FLAG eIntFlag
);
Description
The function is used to polling channel interrupt status
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eIntFlag [in] Intterrupt source:
eDRVPDMA_TABORT: Read/Write Target Abort
eDRVPDMA_BLKD: Block Transfer Done
Include
Driver/DrvPDMA.h
Return Value
True: Interrupt status is set.
False: Interrupt status is clear.
Example
/* Get Channel 5 transfer done interrupt status */
int32_t i32Channel5TransferDone;
/* Enable INT */
V1.05.002
- 362 -
DrvPDMA_EnableInt(eDRVPDMA_CHANNEL_5, eDRVPDMA_BLKD );
DrvPDMA_SetAPBTransferWidth
Prototype
int32_t
DrvPDMA_SetAPBTransferWidth(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_TRANSFER_WIDTH eTransferWidth
);
Description
The function is used to set APB transfer width for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eTransferWidth [in]
eDRVPDMA_WIDTH_32BITS: One word (32 bits) is transferred for every PDMA operation.
eDRVPDMA_WIDTH_8BITS: One byte (8 bits) is transferred for every PDMA operation.
eDRVPDMA_WIDTH_16BITS: One half-word (16 bits) is transferred for every PDMA operation.
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
E_DRVPDMA_ERR_PORT_INVALID: invalid port number
Note
This function is meaningful only when PDMA mode select is APB-to-Memory or
Memory-to-APB mode.
Example
V1.05.002
- 363 -
DrvPDMA_SetCHForAPBDevice
Prototype
int32_t
DrvPDMA_SetCHForAPBDevice(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_APB_DEVICE
eDevice,
E_DRVPDMA_APB_RW
eRWAPB
);
Description
The function is used to select PDMA channel for APB device
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eDevice [in]
Channel for APB device. It includes of
eDRVPDMA_SPI0~3,eDRVPDMA_UART0~1, eDRVPDMA_ADC,
eDRVPDMA_I2S
eRWAPB [in] : PDMA transfer data direction
eDRVPDMA_WRITE_APB: PDMA transfer data from memory to specified APB.
eDRVPDMA_READ_APB: PDMA transfer data from specified APB to memory.
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
E_DRVPDMA_ERR_PORT_INVALID: Invalid port
E_DRVPDMA_FALSE_INPUT: Invalid APB device
Example
/*Set PDMA channel1 to UART1 TX port*/
DrvPDMA_SetCHForAPBDevice(eDRVPDMA_CHANNEL_1,eDRVPDMA_UART1,eDR
VPDMA_WRITE_APB);
/*Set PDMA channel0 to SPI0 RX port*/
V1.05.002
- 364 -
DrvPDMA_SetCHForAPBDevice(eDRVPDMA_CHANNEL_0,eDRVPDMA_SPI0,eDRVP
DMA_READ_APB);
DrvPDMA_SetSourceAddress
Prototype
int32_t
DrvPDMA_SetSourceAddress(
E_DRVPDMA_CHANNEL_INDEX eChannel,
uint32_t u32SourceAddr
);
Description
The function is used to set source address for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
u32SourceAddress [in]
Source address
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
E_DRVPDMA_ERR_PORT_INVALID: Invalid port number
Example
/* Set channel 0 source address to specified address.*/
DrvPDMA_ SetSourceAddress (eDRVPDMA_CHANNEL_0, 0x20001000);
DrvPDMA_SetDestAddress
Prototype
int32_t
DrvPDMA_SetDestAddress(
E_DRVPDMA_CHANNEL_INDEX eChannel,
uint32_t u32DestAddr
);
Description
V1.05.002
- 365 -
DrvPDMA_DisableInt
Prototype
int32_t
DrvPDMA_DisableInt(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_INT_ENABLE eIntSource
);
Description
The function is used to disable interrupt for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eIntSource [in]: Interrupt source
eDRVPDMA_TABORT: Read/Write Target Abort
eDRVPDMA_BLKD: Block Transfer Done
Include
Driver/DrvPDMA.h
Return Value
V1.05.002
- 366 -
E_SUCCESS: Success
E_DRVPDMA_ERR_PORT_INVALID: invalid port number
Example
/*Disable channel3 read/write target abort interrupt*/
DrvPDMA_DisableInt(eDRVPDMA_CHANNEL_3, eDRVPDMA_TABORT);
DrvPDMA_EnableInt
Prototype
int32_t
DrvPDMA_EnableInt(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_INT_ENABLE eIntSource
);
Description
The function is used to enable Interrupt for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eIntSource [in]: Interrupt source
eDRVPDMA_TABORT: Read/Write Target Abort
eDRVPDMA_BLKD: Block Transfer Done
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
E_DRVPDMA_ERR_PORT_INVALID: invalid port number
Example
/*Enable channel0 block transfer done interrupt.*/
DrvPDMA_EnableInt(eDRVPDMA_CHANNEL_0, eDRVPDMA_BLKD);
DrvPDMA_GetAPBTransferWidth
Prototype
int32_t
DrvPDMA_GetAPBTransferWidth(
V1.05.002
- 367 -
E_DRVPDMA_CHANNEL_INDEX eChannel);
Description
The function is used to get peripheral transfer width from specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
0: One word (32 bits) is transferred for every PDMA operation.
1: One byte (8 bits) is transferred for every PDMA operation.
2: One half-word (16 bits) is transferred for every PDMA operation.
E_DRVPDMA_ERR_PORT_INVALID: invalid port number
Note
This function is meaningful only when PDMA mode selection is
APB-to-Memory/Memory-to-APB mode.
Example
/*Get peripheral transfer width from channel3*/
int32_t i32Channel3APBTransferWidth;
i32Channel3APBTransferWidth = DrvPDMA_GetAPBTransferWidth(eDRVPDMA_CHANNEL_3);
DrvPDMA_GetCHForAPBDevice
Prototype
Int32_t
DrvPDMA_GetCHForAPBDevice(
E_DRVPDMA_APB_DEVICE eDevice,
E_DRVPDMA_APB_RW eRWAPB
);
Description
The function is used to get PDMA channel for specified APB device
Parameter
eDevice [in]
Channel for APB device. It includes of
V1.05.002
- 368 -
eDRVPDMA_SPI0~3,eDRVPDMA_UART0~1, eDRVPDMA_ADC,
eDRVPDMA_I2S
eRWAPB [in] : Specify APB direction
eDRVPDMA_READ_APB: APB to memory
eDRVPDMA_WRITE_APB: memory to APB
Include
Driver/DrvPDMA.h
Return Value
0: channel 0
1: channel 1
2: channel 2
3: channel 3
4: channel 4
5: channel 5
6: channel 6
7: channel 7
8: channel 8
E_DRVPDMA_FALSE_INPUT:
Wrong parameter
Others: Reserved
Note
If APBDevice dont be assigned to any channel, the default return value will be 15(0xF).
Example
/* Get UART0 RX PDMA channel*/
int32_t i32GetChannel4APBDevice;
i32GetChannel4APBDevice = DrvPDMA_GetCHForAPBDevice(eDRVPDMA_UART0,
eDRVPDMA_READ_APB);
DrvPDMA_GetCurrentDestAddr
Prototype
uint32_t
DrvPDMA_GetCurrentDestAddr(
E_DRVPDMA_CHANNEL_INDEX eChannel
);
Description
The function is used to get current destination address from specified channel.
V1.05.002
- 369 -
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
Current destination address
Note
Current destination address indicates the destination address where the PDMA transfer is just
occurring.
Example
/*Get Channel5 current destination address;*/
uint32_t u32Channel5CurDestAddr;
u32Channel5CurDestAddr = DrvPDMA_GetCurrentDestAddr(eDRVPDMA_CHANNEL_5);
DrvPDMA_GetCurrentSourceAddr
Prototype
uint32_t
DrvPDMA_GetCurrentSourceAddr(
E_DRVPDMA_CHANNEL_INDEX eChannel
)
Description
The function is used to get current source address from specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
Current source address register indicates the source address where the PDMA transfer is just
occurring.
Example
/*Get channel7 current source address.*/
V1.05.002
- 370 -
uint32_t u32Channel7CurrentSourceAddress;
u32Channel7CurrentSourceAddress =
DrvPDMA_GetCurrentSourceAddr(eDRVPDMA_CHANNEL_7);
DrvPDMA_GetRemainTransferCount
Prototype
uint32_t
DrvPDMA_GetRemainTransferCount(
E_DRVPDMA_CHANNEL_INDEX eChannel
);
Description
The function is used to get current remained byte count of specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
Current remained byte count.
Note
If user set transfer byte to 64 bytes, the current byte count will be 64bytes in the beginning of
transfer. After PDMA transferred 4 bytes to memory, user can issue this API and will get
current remained byte count value which is 60 bytes.
Example
Get Channel0 Current remained byte count
uint32_t u32CurrentRemainedByteCount;
u32CurrentRemainedByteCount =
DrvPDMA_GetRemainTransferCount(eDRVPDMA_CHANNEL_0);
DrvPDMA_GetInternalBufPointer
Prototype
uint32_t
DrvPDMA_GetInternalBufPointer(
E_DRVPDMA_CHANNEL_INDEX eChannel
);
V1.05.002
- 371 -
Description
The function is used to get internal buffer pointer for specified channel
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
E_DRVPDMA_ERR_PORT_INVALID : invalid port
0x01 : internal pointer point to byte1(one byte remained in PDMA buffer)
0x03 : internal pointer point to byte2(two byte remained in PDMA buffer)
0x07: internal pointer point to byte3(three byte remained in PDMA buffer)
0x0F: internal pointer point to byte4(There is no more data remained in PDMA buffer)
Example
/*Get channel0 internal buffer data point to know how many bytes remained in PDMA shared
buffer and print the internal buffer values.*/
uint32_t u32PdmaInternalBufferPoint ;
uint32_t u32PdmaSharedBufferData;
uint8_t au8EffectiveSharedBufferData[4] ;
u32PdmaInternalBufferPoint = DrvPDMA_GetInternalBufPointer(eDRVPDMA_CHANNEL_0)
if(u32PdmaInternalBufferPoint==0x01)
{
printf(Because the Pdma Internal bufer point is 0x01 which indicates that there is only one
byte data remained in PDMA buffer!)
u32PdmaSharedBufferData = DrvPDMA_GetSharedBufData(eDRVPDMA_CHANNEL_0) ;
au8EffectiveSharedBufferData [0] = (uint8_t)(u32PdmaSharedBufferData&0x000000FF) ;
printf(PDMA Shared buffer data is %x\n, au8EffectiveSharedBufferData [0]) ;
}
else if(u32PdmaInternalBufferPoint==0x03)
{
printf(Because the Pdma Internal bufer point is 0x03 which indicates that there is two bytes
data remained in PDMA buffer!)
u32PdmaSharedBufferData = DrvPDMA_GetSharedBufData(eDRVPDMA_CHANNEL_0) ;
au8EffectiveSharedBufferData [0] = (uint8_t)(u32PdmaSharedBufferData&0x000000FF) ;
au8EffectiveSharedBufferData [1] = (uint8_t)(u32PdmaSharedBufferData&0x0000FF00) ;
V1.05.002
- 372 -
DrvPDMA_GetSharedBufData
Prototype
uint32_t
DrvPDMA_GetSharedBufData(
E_DRVPDMA_CHANNEL_INDEX eChannel,
)
Description
The function is used to get shared buffer content from specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
V1.05.002
- 373 -
DrvPDMA_GetTransferLength
Prototype
int32_t
DrvPDMA_GetTransferLength(
E_DRVPDMA_CHANNEL_INDEX eChannel,
uint32_t* pu32TransferLength
);
Description
The function is used to get channel transfer length setting. The unit of * pu32TransferLength
is byte.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
pu32TransferLength [in]
The data pointer to save transfer length
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
Example
/* Get the transfer byte count setting of channel0.*/
uint32_t u32GetTransferByteCountSetting;
DrvPDMA_GetTransferLength(eDRVPDMA_CHANNEL_0,
&u32GetTransferByteCountSetting);
DrvPDMA_GetSourceAddress
Prototype
uint32_t
V1.05.002
- 374 -
DrvPDMA_GetSourceAddress (
E_DRVPDMA_CHANNEL_INDEX eChannel,
)
Description
The function is used to get source address for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
Source address
Example
/* Get the source address of channel0 */
uint32_t u32GetSourceAddress;
u32GetSourceAddress = DrvPDMA_GetSourceAddress(eDRVPDMA_CHANNEL_0);
DrvPDMA_GetDestAddress
Prototype
uint32_t
DrvPDMA_GetDestAddress (
E_DRVPDMA_CHANNEL_INDEX eChannel,
)
Description
The function is used to get destination address for specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
V1.05.002
- 375 -
Destination address
Example
/* Get the destination address of channel0 */
uint32_t u32GetDestAddress;
u32GetDestAddress = DrvPDMA_GetDestAddress(eDRVPDMA_CHANNEL_0);
DrvPDMA_InstallCallBack
Prototype
int32_t
DrvPDMA_InstallCallBack(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_INT_ENABLE eIntSource,
PFN_DRVPDMA_CALLBACK pfncallback
);
Description
The function is used to install call back function for specified channel and interrupt source.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eIntSource [in]: Interrupt source
eDRVPDMA_TABORT: read/write target abort
eDRVPDMA_BLKD: block transfer done
pfncallback [in]
The callback function pointer
Include
Driver/DrvPDMA.h
Return Value
E_SUCCESS: Success
Example
Please refer to DrvPDMA_Open() sample code.
V1.05.002
- 376 -
DrvPDMA_IsCHBusy
Prototype
int32_t
DrvPDMA_IsCHBusy(
E_DRVPDMA_CHANNEL_INDEX eChannel
);
Description
The function is used to Get Channel Enable/Disable status
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
Include
Driver/DrvPDMA.h
Return Value
TRUE: The channel is busy
FALSE: The channel is un-used.
E_DRVPDMA_ERR_PORT_INVALID: invalid port number
Example
/* Get channel0 bus status.*/
int32_t i32Channel0BusStatus;
i32Channel0BusStatus = if(DrvPDMA_IsCHBusy(eDRVPDMA_CHANNEL_0);
if(i32Channel0BusStatus== TRUE)
printf(Channel0 bus is busy!!\n);
else if(i32Channel0BusStatus== FALSE)
printf(Channel0 bus is not busy!!\n);
else if(i32Channel0BusStatus== E_DRVPDMA_ERR_PORT_INVALID)
printf(invalid port!!\n);
DrvPDMA_IsIntEnabled
Prototype
int32_t
V1.05.002
- 377 -
DrvPDMA_IsIntEnabled(
E_DRVPDMA_CHANNEL_INDEX eChannel,
E_DRVPDMA_INT_ENABLE eIntSource
);
Description
The function is used to check if the specified interrupt source is enabled in specified channel.
Parameter
eChannel [in]
Specify eDRVPDMA_CHANNEL_0~8
eIntSource [in]
Interrupt source:
eDRVPDMA_TABORT/eDRVPDMA_BLKD
Include
Driver/DrvPDMA.h
Return Value
TRUE: The specified interrupt source of specified channel is enable.
FALSE: The specified interrupt source of specified channel is disable.
Include
Driver/DrvPDMA.h
Example
int32_t i32IsIntEnable;
i32IsIntEnable=DrvPDMA_IsIntEnabled(eDRVPDMA_CHANNEL_0, eDRVPDMA_BLKD)
if(i32IsIntEnable == TRUE )
printf(Channel0 Block transfer Done interrupt is enable!\n);
else if(i32IsIntEnable == FALSE )
printf(Channel0 Block transfer Done interrupt is disable!\n);
DrvPDMA_GetVersion
Prototype
int32_t
DrvPDMA_GetVersion (void);
Description
Return the current version number of driver.
V1.05.002
- 378 -
Include
Driver/DrvPDMA.h
Return Value
PDMA driver current version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* Get PDMA driver current version number */
int32_t i32PDMAVersionNum;
i32PDMAVersionNum = DrvPDMA_GetVersion();
V1.05.002
- 379 -
V1.05.002
- 380 -
Value
Description
DRVI2S_DATABIT_8
0x00
DRVI2S_DATABIT_16
0x01
DRVI2S_DATABIT_24
0x02
DRVI2S_DATABIT_32
0x03
DRVI2S_MONO
0x01
DRVI2S_STEREO
0x00
DRVI2S_FORMAT_MSB
0x01
DRVI2S_FORMAT_I2S
0x00
DRVI2S_MODE_SLAVE
0x01
DRVI2S_MODE_MASTER
0x00
DRVI2S_FIFO_LEVEL_WORD_0
0x00
DRVI2S_FIFO_LEVEL_WORD_1
0x01
DRVI2S_FIFO_LEVEL_WORD_2
0x02
DRVI2S_FIFO_LEVEL_WORD_3
0x03
DRVI2S_FIFO_LEVEL_WORD_4
0x04
DRVI2S_FIFO_LEVEL_WORD_5
0x05
DRVI2S_FIFO_LEVEL_WORD_6
0x06
DRVI2S_FIFO_LEVEL_WORD_7
0x07
DRVI2S_FIFO_LEVEL_WORD_8
0x08
DRVI2S_EXT_12M
DRVI2S_PLL
DRVI2S_HCLK
DRVI2S_INTERNAL_22M
V1.05.002
- 381 -
Value
Description
I2S_LEFT_CHANNEL
I2S_RIGHT_CHANNEL
0
1
E_I2S_CALLBACK_TYPE
Enumeration identifier
Value
Description
I2S_RX_UNDERFLOW
I2S_RX_OVERFLOW
I2S_RX_FIFO_THRESHOLD
I2S_TX_UNDERFLOW
I2S_TX_OVERFLOW
I2S_TX_FIFO_THRESHOLD
I2S_TX_RIGHT_ZERO_CROSS
I2S_TX_LEFT_ZERO_CROSS
0
1
2
8
9
10
11
12
u32Data
);
Description
Write word data to Tx FIFO.
Parameter
u32Data [in]
Word data to Tx FIFO.
Include
Driver/DrvI2S.h
V1.05.002
- 382 -
Return Value
None
Example
/* Write word data 0x12345678 into I2S Tx FIFO */
_DRVI2S_WRITE_TX_FIFO (0x12345678);
_DRVI2S_READ_RX_FIFO
Prototype
static __inline
uint32_t
_DRVI2S_READ_RX_FIFO (
void
);
Description
Read out word data from Rx FIFO.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
Word data from Rx FIFO.
Example
uint32_t u32data;
/* Read word data from I2S Rx FIFO */
u32data = _DRVI2S_READ_RX_FIFO ();
_DRVI2S_READ_TX_FIFO_LEVEL
Prototype
static __inline
uint32_t
_DRVI2S_READ_TX_FIFO_LEVEL (
void
);
V1.05.002
- 383 -
Description
Get word data number in Tx FIFO.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
0~8: word data in Tx FIFO
Example
uint32_t u32len;
/* Get word data number in Tx FIFO */
u32len = _DRVI2S_READ_TX_FIFO_LEVEL ();
_DRVI2S_READ_RX_FIFO_LEVEL
Prototype
static __inline
uint32_t
_DRVI2S_READ_RX_FIFO_LEVEL (
void
);
Description
Get word data number in Rx FIFO.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
0~8: word data in Rx FIFO
Example
uint32_t u32len;
/* Get word data number in Rx FIFO */
u32len = _DRVI2S_READ_RX_FIFO_LEVEL ( );
V1.05.002
- 384 -
16.6. Functions
DrvI2S_Open
Prototype
int32_t DrvI2S_Open (S_DRVI2S_DATA_T *sParam);
Description
This function is used to enable I2S clock and function, and configure the data length/data
format/FIFO threshold level/BCLK (Bit Clock). The data and audio formats are shown in I2S
Operation and FIFO Operation of I2S Section in TRM. For master mode, I2S_BCLK and
I2S_LRCLK pins are output mode; for slave mode, I2S_BCLK and I2S_LRCLK pins are input
mode. Also, the I2S signals (I2S_BCLK and I2S_LRCLK) are shown in I2S Block Diagram of
I2S Section in TRM.
Parameter
*sParam [in]
It includes the following parameter
u32SampleRate: Sampling rate. The setting takes effect when I2S operates as master
mode.
u8WordWidth: 8, 16, 24, or 32 bit data size - DRVI2S_DATABIT_8 /
DRVI2S_DATABIT_16 / DRVI2S_DATABIT_24 /
DRVI2S_DATABIT_32
u8AudioFormat: Support mono or stereo audio data - DRVI2S_MONO /
DRVI2S_STEREO
u8DataFormat: Support I2S and MSB justified data format DRVI2S_FORMAT_I2S / DRVI2S_FORMAT_MSB
u8Mode: Operate as master or slave mode - DRVI2S_MODE_MASTER /
DRVI2S_MODE_SLAVE
u8TxFIFOThreshold: Tx FIFO threshold level - DRVI2S_FIFO_LEVEL_WORD_0 /
DRVI2S_FIFO_LEVEL_WORD_1 /
DRVI2S_FIFO_LEVEL_WORD_2 /
DRVI2S_FIFO_LEVEL_WORD_3 /
DRVI2S_FIFO_LEVEL_WORD_4 /
DRVI2S_FIFO_LEVEL_WORD_5 /
DRVI2S_FIFO_LEVEL_WORD_6 /
DRVI2S_FIFO_LEVEL_WORD_7
u8RxFIFOThreshold: Rx FIFO threshold level - DRVI2S_FIFO_LEVEL_WORD_1 /
DRVI2S_FIFO_LEVEL_WORD_2 /
DRVI2S_FIFO_LEVEL_WORD_3 /
DRVI2S_FIFO_LEVEL_WORD_4 /
DRVI2S_FIFO_LEVEL_WORD_5 /
DRVI2S_FIFO_LEVEL_WORD_6 /
DRVI2S_FIFO_LEVEL_WORD_7 /
DRVI2S_FIFO_LEVEL_WORD_8
V1.05.002
- 385 -
Include
Driver/DrvI2S.h
Return Value
0
Success
Example
S_DRVI2S_DATA_T st;
DrvI2S_Close
Prototype
void
DrvI2S_Close (void);
Description
Close I2S controller and disable I2S clock.
Include
Driver/DrvI2S.h
Return Value
None
Example
DrvI2S_Close ( );
/* Disable I2S */
DrvI2S_EnableInt
Prototype
int32_t DrvI2S_EnableInt (E_I2S_CALLBACK_TYPE Type, I2S_CALLBACK callbackfn);
V1.05.002
- 386 -
Description
To enable I2S interrupt function and install relative call back function in I2S interrupt
handler.
Parameter
Type [in]
There are eight types for call back function.
I2S_RX_UNDERFLOW: Rx FIFO underflow
I2S_RX_OVERFLOW: Rx FIFO overflow.
I2S_RX_FIFO_THRESHOLD: Data word in Rx FIFO is higher than Rx threshold level.
I2S_TX_UNDERFLOW: Tx FIFO underflow.
I2S_TX_OVERFLOW: Tx FIFO overflow
I2S_TX_FIFO_THRESHOLD: Data word in Tx FIFO is less than Tx threshold level.
I2S_TX_RIGHT_ZERO_CROSS: Tx right channel zero cross.
I2S_TX_LEFT_ZERO_CROSS: Tx left channel zero cross.
callbackfn [in]
Call back function name for specified interrupt event.
Include
Driver/DrvI2S.h
Return Value
0: Succeed
<0: Failed
Example
/* Enable Rx threshold level interrupt and install its callback function */
DrvI2S_EnableInt (I2S_RX_FIFO_THRESHOLD, Rx_thresholdCallbackfn);
/* Enable Tx threshold level interrupt and install its callback function */
DrvI2S_EnableInt (I2S_TX_FIFO_THRESHOLD, Tx_thresholdCallbackfn);
DrvI2S_DisableInt
Prototype
int32_t DrvI2S_DisableInt (E_I2S_CALLBACK_TYPE Type);
Description
To disable I2S interrupt function and uninstall relative call back function in I2S interrupt
handler.
Parameter
V1.05.002
- 387 -
Type [in]
There are eight types for call back function.
I2S_RX_UNDERFLOW: Rx FIFO underflow
I2S_RX_OVERFLOW: Rx FIFO overflow
I2S_RX_FIFO_THRESHOLD: Data word in Rx FIFO is higher than Rx threshold level.
I2S_TX_UNDERFLOW: Tx FIFO underflow.
I2S_TX_OVERFLOW: Tx FIFO overflow
I2S_TX_FIFO_THRESHOLD: Data word in Tx FIFO is less than Tx threshold level.
I2S_TX_RIGHT_ZERO_CROSS: Tx right channel zero cross.
I2S_TX_LEFT_ZERO_CROSS: Tx left channel zero cross.
Include
Driver/DrvI2S.h
Return Value
0: Succeed
<0: Failed
Example
/* Disable Rx threshold level interrupt and uninstall its callback function */
DrvI2S_DisableInt (I2S_RX_FIFO_THRESHOLD);
/* Disable Tx threshold level interrupt and uninstall its callback function */
DrvI2S_DisableInt (I2S_TX_FIFO_THRESHOLD);
DrvI2S_GetBCLKFreq
Prototype
uint32_t DrvI2S_GetBCLKFreq (void);
Description
To get the I2S BCLK (Bit Clock) frequency.
BCLK = I2S source clock / (2 x (BCLK divider + 1))
Parameter
None
Include
Driver/DrvI2S.h
Return Value
I2S BCLK frequency. The unit is Hz.
V1.05.002
- 388 -
Example
uint32_t u32clock;
u32clock = DrvI2S_GetBCLKFreq ( );
DrvI2S_SetBCLKFreq
Prototype
void DrvI2S_SetBCLKFreq (uint32_t u32Bclk);
Description
To configure BCLK (Bit Clock) clock. The BCLK will work when I2S operates in master
mode. BCLK = I2S source clock / (2 x BCLK divider + 1))
Parameter
u32Bclk [in]
I2S BCLK frequency. The unit is Hz.
Include
Driver/DrvI2S.h
Return Value
None
Example
DrvI2S_SetBCLKFreq (512000);
DrvI2S_GetMCLKFreq
Prototype
uint32_t DrvI2S_GetMCLKFreq (void);
Description
To get the I2S MCLK (Master Clock) frequency.
MCLK = I2S source clock / (2 x MCLK divider))
Parameter
None
Include
Driver/DrvI2S.h
Return Value
I2S MCLK frequency. The unit is Hz.
V1.05.002
- 389 -
Example
uint32_t u32clock;
u32clock = DrvI2S_GetMCLKFreq ( );
DrvI2S_SetMCLKFreq
Prototype
void DrvI2S_SetMCLKFreq (uint32_t u32Mclk);
Description
To configure MCLK (Master Clock) clock.
MCLK = I2S source clock / (2 x (MCLK divider))
Parameter
u32Mclk [in]
I2S MCLK frequency. The unit is Hz.
Include
Driver/DrvI2S.h
Return Value
None
Example
DrvI2S_SetMCLKFreq (12000000);
DrvI2S_SetChannelZeroCrossDetect
Prototype
int32_t DrvI2S_SetChannelZeroCrossDetect (E_I2S_CHANNEL channel, int32_t i32flag);
Description
To enable or disable right/left channel zero cross detect function.
Parameter
channel [in]
I2S_LEFT_CHANNEL / I2S_RIGHT_CHANNEL
i32flag [in]
To enable or disable zero cross detect function. (1: enable 0: disable)
Include
Driver/DrvI2S.h
V1.05.002
- 390 -
Return Value
0: Succeed
<0: Failed
Example
/* Enable left channel zero cross detect */
DrvI2S_SetChannelZeroCrossDetect (I2S_LEFT_CHANNEL, 1);
/* Disable right channel zero cross detect */
DrvI2S_SetChannelZeroCrossDetect (I2S_RIGHT_CHANNEL, 0);
DrvI2S_EnableTxDMA
Prototype
void
DrvI2S_EnableTxDMA (void);
Description
To enable I2S Tx DMA function. I2S requests DMA to transfer data to Tx FIFO.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Enable I2S Tx DMA function */
DrvI2S_EnableTxDMA ( );
DrvI2S_DisableTxDMA
Prototype
void
DrvI2S_DisableTxDMA (void);
Description
To disable I2S Tx DMA function.
Parameter
None
Include
V1.05.002
- 391 -
Driver/DrvI2S.h
Return Value
None
Example
/* Disable I2S Tx DMA function */
DrvI2S_DisableTxDMA ( );
DrvI2S_EnableRxDMA
Prototype
void
DrvI2S_EnableRxDMA (void);
Description
To enable I2S Rx DMA function. I2S requests DMA to transfer data from Rx FIFO.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Enable I2S Rx DMA function */
DrvI2S_EnableRxDMA ( );
DrvI2S_DisableRxDMA
Prototype
void
DrvI2S_DisableRxDMA (void);
Description
To disable I2S Rx DMA function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
V1.05.002
- 392 -
None
Example
/* Disable I2S Rx DMA function */
DrvI2S_DisableRxDMA ( );
DrvI2S_EnableTx
Prototype
void
DrvI2S_EnableTx (void);
Description
To enable I2S Tx function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Enable I2S Tx function */
DrvI2S_EnableTx ( );
DrvI2S_DisableTx
Prototype
void
DrvI2S_DisableTx (void);
Description
To disable I2S Tx function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
V1.05.002
- 393 -
DrvI2S_EnableRx
Prototype
void
DrvI2S_EnableRx (void);
Description
To enable I2S Rx function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Enable I2S Rx function */
DrvI2S_EnableRx ( );
DrvI2S_DisableRx
Prototype
void
DrvI2S_DisableRx (void);
Description
To Disable I2S Rx function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Disable I2S Rx function */
DrvI2S_DisableRx ( );
V1.05.002
- 394 -
DrvI2S_EnableTxMute
Prototype
void
DrvI2S_EnableTxMute (void);
Description
To enable I2S Tx Mute function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Enable I2S Tx Mute function */
DrvI2S_EnableTxMute ( );
DrvI2S_DisableTxMute
Prototype
void
DrvI2S_DisableTxMute (void);
Description
To disable I2S Tx Mute function.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Disable I2S Tx Mute function */
DrvI2S_DisableTxMute ( );
DrvI2S_EnableMCLK
Prototype
V1.05.002
- 395 -
void
DrvI2S_EnableMCLK (void);
Description
To enable I2S MCLK output from GPIOA Pin15.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Enable MCLK output */
DrvI2S_EnableMCLK ( );
DrvI2S_DisableMCLK
Prototype
void
DrvI2S_DisableMCLK (void);
Description
To disable I2S MCLK output from GPIOA Pin15.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
/* Disable MCLK output */
DrvI2S_DisableMCLK ( );
DrvI2S_ClearTxFIFO
Prototype
void
DrvI2S_ClearTxFIFO (void);
Description
V1.05.002
- 396 -
/* Clear Tx FIFO */
DrvI2S_ClearRxFIFO
Prototype
void
DrvI2S_ClearRxFIFO (void);
Description
To clear Rx FIFO. The internal pointer of Rx FIFO is reset to start point.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
None
Example
DrvI2S_ClearRxFIFO ( );
/* Clear Rx FIFO */
DrvI2S_SelectClockSource
Prototype
void
Description
To select I2S clock source, including external 12M, PLL clock, HCLK and internal 22M.
Parameter
u8ClkSrcSel [in]
To select I2S clock source. There are four sources for I2S:
V1.05.002
- 397 -
DrvI2S_GetSourceClockFreq
Prototype
uint32_t DrvI2S_GetSourceClockFreq (void);
Description
To get I2S source clock frequency.
Parameter
None
Include
Driver/DrvI2S.h
Return Value
I2S clock source frequency. The unit is Hz.
Example
uint32_t u32clock;
u32clock = DrvI2S_GetSourceClock ( );
DrvI2S_GetVersion
Prototype
uint32_t DrvI2S_GetVersion (void);
Description
Get this modules version.
V1.05.002
- 398 -
Parameter
None
Include
Driver/DrvI2S.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
V1.05.002
- 399 -
External devices with max. 64K byte (8 bit data width)/128K byte (16 bit data width) supported.
Variable external bus base clock (MCLK) supported.
8 bit or 16 bit data width supported.
Variable data access time (tACC), address latch enable time (tALE) and address hold time (tAHD)
supported.
Address bus and data bus multiplex mode supported to save the address pins.
Configurable idle cycle supported for differene access condition: Write command finish (W2X),
Read-to-Read (R2R).
V1.05.002
- 400 -
tALE
tLHD
tA2D
tACC
tAHD
Idle cycle
tASU
tALE
MCLK
nCS
ALE
nRD
AD[15:0]
Address output[15:0]
RData
input
Address output[15:0]
nWR
AD[15:0]
Address output[15:0]
WData output[15:0]
XX
Address output[15:0]
Value
Description
E_DRVEBI_DATA_8BIT
0x0
E_DRVEBI_DATA_16BIT
0x1
E_DRVEBI_ADDR_WIDTH
Enumeration Identifier
Value
Description
E_DRVEBI_ADDR_8BIT
0x0
E_DRVEBI_ADDR_16BIT
0x1
E_DRVEBI_MCLKDIV
Enumeration Identifier
Value
Description
E_DRVEBI_MCLKDIV_1
0x0
E_DRVEBI_MCLKDIV_2
0x1
E_DRVEBI_MCLKDIV_4
0x2
E_DRVEBI_MCLKDIV_8
0x3
E_DRVEBI_MCLKDIV_16
0x4
E_DRVEBI_MCLKDIV_32
0x5
E_DRVEBI_MCLKDIV_DEFAULT
0x6
V1.05.002
- 402 -
Example
/* Open the EBI device with 16bit bus width. The start address of the device is at 0x60000000
and the storage size is 128KB */
DRVEBI_CONFIG_T sEBIConfig;
sEBIConfig.eDataWidth = eDRVEBI_DATA_16BIT;
sEBIConfig.eAddrWidth = eDRVEBI_ADDR_16BIT;
sEBIConfig.u32BaseAddress = 0x60000000;
sEBIConfig.u32Size = 0x20000;
DrvEBI_Open (sEBIConfig);
DrvEBI_Close
Prototype
void DrvEBI_Close (void)
Description
Disable EBI function and release the relative pins for GPIO used.
Parameter
None
Include
Driver/DrvEBI.h
Return Value
None
Example:
/* Close the EBI device */
DrvEBI_Close ();
DrvEBI_SetBusTiming
Prototype
void DrvEBI_SetBusTiming (DRVEBI_TIMING_T sEBITiming)
Description
Configure the relative EBI bus timing.
Parameter
sEBITiming [in]
DRVEBI_TIMING_T
eMCLKDIV:
E_DRVEBI_MCLKDIV, it could be E_DRVEBI_MCLKDIV_1,
E_DRVEBI_MCLKDIV_2, E_DRVEBI_MCLKDIV_4,
V1.05.002
- 403 -
E_DRVEBI_MCLKDIV_8, E_DRVEBI_MCLKDIV_16,
E_DRVEBI_MCLKDIV_32 or E_DRVEBI_MCLKDIV_DEFAULT.
u8ExttALE: Expand time of ALE 0~7, tALE = (u8ExttALE+1)*MCLK.
u8ExtIR2R: Idle cycle between Read-Read 0~15, idle cycle = u8ExtIR2R*MCLK
u8ExtIW2X: Idle cycle after Write 0~15, idle cycle = u8ExtIW2X*MCLK
u8ExttAHD: EBI address hold time 0~7, tAHD = (u8ExttAHD+1)*MCLK
u8ExttACC: EBI data access time 0~31, tAHD = (u8ExttACC+1)*MCLK
Include
Driver/DrvEBI.h
Return Value
None
Example:
/* Set the relative EBI bus timing */
DRVEBI_TIMING_T sEBITiming;
sEBITiming.eMCLKDIV = eDRVEBI_MCLKDIV_1;
sEBITiming.u8ExttALE = 0;
sEBITiming.u8ExtIR2R = 0;
sEBITiming.u8ExtIW2X = 0;
sEBITiming.u8ExttAHD = 0;
sEBITiming.u8ExttACC = 0;
DrvEBI_SetBusTiming (sEBITiming);
DrvEBI_GetBusTiming
Prototype
void DrvEBI_GetBusTiming (DRVEBI_TIMING_T *psEBITiming)
Description
Get the current bus timing of the EBI.
Parameter
psEBITiming [out]
DRVEBI_TIMING_T, refer to DrvEBI_SetBusTiming for detail information
Include
Driver/DrvEBI.h
Return Value
Data buffer pointer that stored the EBI bus timing settings
Example:
V1.05.002
- 404 -
DrvEBI_GetVersion
Prototype
uint32_t DrvEBI_GetVersion (void)
Description
Get the version number of EBI driver.
Include
Driver/DrvEBI.h
Return Value
Version number:
31:24
00000000
23:16
MAJOR_NUM
15:8
MINOR_NUM
7:0
BUILD_NUM
Example
/* Get the current version of EBI Driver */
u32Version = DrvEBI_GetVersion ();
V1.05.002
- 405 -
18. Appendix
18.1. NuMicroTM NUC100 Series Products Selection Guide
NUC100 Advance Line Selection Guide (low density)
Connectivity
Part number
Flash
SRAM
SPI
I2C
ADC
Timer
RTC EBI
ISP
ICP
PDMA
I/O
Package
NUC100LC1BN
32 KB
4 KB
8x12-Bit 4x32-bit
up to 35
LQFP48
NUC100LD1BN
64 KB
4 KB
8x12-Bit 4x32-bit
up to 35
LQFP48
NUC100LD2BN
64 KB
8 KB
8x12-bit 4x32-bit
up to 35
LQFP48
NUC100RC1BN
32 KB
4 KB
8x12-Bit 4x32-bit
up to 49
LQFP64
NUC100RD1BN
64 KB
4 KB
8x12-Bit 4x32-bit
up to 49
LQFP64
NUC100RD2BN
64 KB
8 KB
8x12-bit 4x32-bit
up to 49
LQFP64
ISP
ICP
PDMA
I/O
Package
Flash
(KB)
SRAM
NUC100LD3AN
64 KB
Part number
ADC
Timer
RTC EBI
UART
SPI
I2C
16 KB
8x12-bit 4x32-bit
up to 35
LQFP48
NUC100LE3AN
128 KB 16 KB
8x12-bit 4x32-bit
up to 35
LQFP48
NUC100RD3AN
64 KB
16 KB
8x12-bit 4x32-bit
up to 49
LQFP64
NUC100RE3AN
128 KB 16 KB
8x12-bit 4x32-bit
up to 49
LQFP64
NUC100VD2AN
64 KB
8 KB
8x12-bit 4x32-bit
up to 80
LQFP100
NUC100VD3AN
64 KB
16 KB
8x12-bit 4x32-bit
up to 80
LQFP100
NUC100VE3AN 128 KB 16 KB
8x12-bit 4x32-bit
up to 80
LQFP100
ISP
ICP
PDMA
I/O
Package
Flash
SRAM
ADC
Timer
RTC EBI
USB
NUC120LC1BN 32 KB
4 KB
8x12-Bit 4x32-bit
up to 31
LQFP48
NUC120LD1BN 64 KB
4 KB
8x12-Bit 4x32-bit
up to 31
LQFP48
NUC120LD2BN 64 KB
8 KB
8x12-bit 4x32-bit
up to 31
LQFP48
V1.05.002
- 406 -
NUC120RC1BN 32 KB
4 KB
8x12-Bit 4x32-bit
up to 45
LQFP64
NUC120RD1BN 64 KB
4 KB
8x12-Bit 4x32-bit
up to 45
LQFP64
NUC120RD2BN 64 KB
8 KB
8x12-bit 4x32-bit
up to 45
LQFP64
ISP
ICP
PDMA
I/O
Package
Flash
SRAM
NUC120LD3AN 64 KB
ADC
Timer
RTC EBI
USB
16 KB
8x12-bit 4x32-bit
up to 31
LQFP48
NUC120LE3AN 128 KB 16 KB
8x12-bit 4x32-bit
up to 31
LQFP48
NUC120RD3AN 64 KB
16 KB
8x12-bit 4x32-bit
up to 45
LQFP64
NUC120RE3AN 128 KB 16 KB
8x12-bit 4x32-bit
up to 45
LQFP64
NUC120VD2AN 64 KB
8 KB
8x12-bit 4x32-bit
up to 76
LQFP100
NUC120VD3AN 64 KB
16 KB
8x12-bit 4x32-bit
up to 76
LQFP100
NUC120VE3AN 128 KB 16 KB
8x12-bit 4x32-bit
up to 76
LQFP100
Flash
SRAM
ADC
ISP
PDMA
ICP
I/O
Package
NUC130LC1CN 32 KB
4 KB
8x12-bit 4x32-bit
up to 35
LQFP48
NUC130LD2CN 64 KB
8 KB
8x12-bit 4x32-bit
up to 35
LQFP48
NUC130LE3CN 128KB
16KB
8x12-bit 4x32-bit
up to 35
LQFP48
NUC130RC1CN 32 KB
4 KB
8x12-bit 4x32-bit
up to 49
LQFP64
NUC130RD2CN 64 KB
8 KB
8x12-bit 4x32-bit
up to 49
LQFP64
NUC130RE3CN 128KB
16KB
8x12-bit 4x32-bit
up to 49
LQFP64
NUC130VE3CN 128KB
16KB
8x12-bit 4x32-bit
up to 80
LQFP100
I/O
Package
Flash SRAM
ISP
PDMA
ICP
NUC140LC1CN 32 KB
4 KB
8x12-bit 4x32-bit
up to 31
LQFP48
NUC140LD2CN 64 KB
8 KB
8x12-bit 4x32-bit
up to 31
LQFP48
8x12-bit 4x32-bit
up to 31
LQFP48
NUC140RC1CN 32 KB
4 KB
8x12-bit 4x32-bit
up to 45
LQFP64
NUC140RD2CN 64 KB
8 KB
8x12-bit 4x32-bit
up to 45
LQFP64
8x12-bit 4x32-bit
up to 45
LQFP64
8x12-bit 4x32-bit
up to 76
LQFP100
V1.05.002
- 407 -
Flash
SRAM
SPI
ADC
Timer
RTC
ISP
ICP
I/O
Package
NUC101LC1BN
32 KB
4 KB
4x32-bit
up to 31
LQFP48
NUC101LD2BN
64 KB
8 KB
4x32-bit
up to 31
LQFP48
NUC101YC1BN
32 KB
4 KB
4x32-bit
up to 31
QFP36
NUC101YD2BN
64 KB
8 KB
4x32-bit
up to 31
QFP36
NUC120VD3AN
NUC120VE3AN
0x00012021
0x00012018
PDID
0x10010108
0x10010104
0x10010147
0x10010143
V1.05.002
- 409 -
Date
Description
V1.00.001
Jan. 8, 2009
V1.00.002
V1.03.001
Jan. 5, 2011
V1.04.001
V1.04.002
V1.04.003
V1.05.001
V1.05.002
Created
Fix errors
Add example of API
Fix errors
Fix clock diagram error
Update API description according to NUC100 Series BSP
v.1.003.001
Update CAN driver
Supports NUC130XXXCN and 140XXXCN
Update clock diagram to add EBI clock tree
V1.05.002
- 410 -
Important Notice
Nuvoton products are not designed, intended, authorized or warranted for use as components in
equipment or systems intended for surgical implantation, atomic energy control instruments,
aircraft or spacecraft instruments, transportation instruments, traffic signal instruments,
combustion control instruments, or for any other applications intended to support or sustain life.
Furthermore, Nuvoton products are not intended for applications whereby failure could result or
lead to personal injury, death or severe property or environmental damage.
Nuvoton customers using or selling these products for such applications do so at their own risk
and agree to fully indemnify Nuvoton for any damages resulting from their improper use or
sales.
V1.05.002
- 411 -