commit 81873aa1ec86b6acf36200fe575478b53faeea8b Author: Bottersnike Date: Sat Jun 29 21:17:34 2024 +0100 Initial commit diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b200d38 --- /dev/null +++ b/.clang-format @@ -0,0 +1,6 @@ +BasedOnStyle: Google +UseTab: Never +BreakBeforeBraces: Custom +IndentWidth: 4 +ColumnLimit: 100 +Cpp11BracedListStyle: false diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..63ffbd5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +# Picolibc +picolibc/* +# Misc testing files +*.py +# Build artifacts +obj/* +host_aprom.bin diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..bd31aea --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,11 @@ +{ + "configurations": [ + { + "name": "GCC", + "includePath": ["C:/Program Files (x86)/Arm GNU Toolchain arm-none-eabi/13.2 Rel1/arm-none-eabi/include", "NUC123/inc", "NUC123/StdDriver/inc"], + "defines": [], + "intelliSenseMode": "gcc-arm" + } + ], + "version": 4 +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..8236676 --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,7 @@ +{ + "recommendations": [ + "aaron-bond.better-comments", + "marus25.cortex-debug", + "llvm-vs-code-extensions.vscode-clangd" + ] +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..3ba1095 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Host APROM Debug", + "preLaunchTask": "Build Host APROM", + "cwd": "${workspaceFolder}", + "executable": "./obj/host_aprom.elf", + "request": "launch", + "type": "cortex-debug", + "runToEntryPoint": "_start", + "servertype": "openocd", + "serverpath": "../../openocd-0.12.0-3/bin/openocd", + "configFiles": ["interface/stlink-v2.cfg", "../../nucxxx.cfg"], + "showDevDebugOutput": "raw", + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f580eeb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,16 @@ +{ + "clangd.arguments": ["--query-driver=*arm-none-eabi-*"], + "clangd.fallbackFlags": [ + "-IC:/Program Files (x86)/GNU Arm Embedded Toolchain/13.2 Rel1/arm-none-eabi/include", + "-I${workspaceFolder}/NUC123/inc", + "-I${workspaceFolder}/NUC123/StdDriver/inc", + "-D__GNUC__", + "-Wno-pointer-to-int-cast", + "-Wno-int-to-pointer-cast", + ], + "cortex-debug.variableUseNaturalFormat": true, + "cortex-debug.liveWatchRefreshRate": 1000, + "cortex-debug.enableTelemetry": false, + "files.trimFinalNewlines": true, + "editor.defaultFormatter": "llvm-vs-code-extensions.vscode-clangd", +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..6ab7336 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,19 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Build Host APROM", + "command": "make", + "args": [ + "-j", + "all" + ], + "type": "shell", + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [] + } + ] +} diff --git a/Development.md b/Development.md new file mode 100644 index 0000000..d9b5e60 --- /dev/null +++ b/Development.md @@ -0,0 +1,27 @@ +# Development +## Requirements +- Windows (Strongly recommend. Compiling on anything else is not officially supported.) +- [Make for Windows](https://gnuwin32.sourceforge.net/packages/make.htm) + - On the path, please +- [Arm GNU Toolchain](https://developer.arm.com/downloads/-/arm-gnu-toolchain-downloads) (Currently using 13.2 Rel1) + - To make your life easier, ensure this is located at `C:\Program Files (x86)\GNU Arm Embedded Toolchain\13.2 Rel1` +- [picolibc 1.8.6](https://github.com/picolibc/picolibc/releases/download/1.8.6/picolibc-1.8.6-13.2.Rel1.zip) + - Place the contents of this zip file in `picolibc` + +VSCode is recommended for development, and all recommended extensions should be installed. + +## Compiling and Uploading +Run `make -j` to compile the firmware. + +For rapid development, the HID bootloader is quite annoying. `flash.cmd` is provided which reprograms the chip using an ST-Link V2. This requires an OpenOCD installation in the root project folder, along with an existing compiled host bootloader binary. + +For debugging in VSCode, install the recommended extensions then hit F5. This requires an OpenOCD installation in the root project folder, and an ST-Link V2. + +## NUC123 BSP +The `NUC123` folder is a heavily reduced form of the complete BSP provided by Nuvoton. Modifications should not be made to any files within the `inc` or `StdDriver` folder. + +`startup_NUC123.s` is the program entrypoint, and may require modification. + +`NUC123.ld` is a modified version of the GCC linker script provided by Nuvoton, and may require modification. + +To pull in additional BSP library modules, if required, the `Makefile` should be modified. diff --git a/GCC.mk b/GCC.mk new file mode 100644 index 0000000..0dcb3b8 --- /dev/null +++ b/GCC.mk @@ -0,0 +1,21 @@ +PREFIX = arm-none-eabi- +CC = $(PREFIX)gcc +CXX = $(PREFIX)g++ +AS = $(PREFIX)gcc # Rather than AS, so we get preprocessing +AR = $(PREFIX)ar +LD = $(PREFIX)gcc # To pull in stdlib as needed +OBJCOPY = $(PREFIX)objcopy +OBJDUMP = $(PREFIX)objdump +SIZE = $(PREFIX)size + +SPECS := -specs=./picolibc/lib/gcc/arm-none-eabi/13.2.1/picolibc.specs + +INCLUDES += -I./picolibc/arm-none-eabi/picolibc/arm-none-eabi/include + +OPTIM ?= -flto -Os +GCCFLAGS := $(SPECS) -nolibc -nostdlib -nostartfiles -nodefaultlibs -mcpu=cortex-m0 -mthumb -Wl,--gc-sections $(OPTIM) -g + +CFLAGS := $(GCCFLAGS) -c -ffunction-sections -fdata-sections -fsigned-char \ + -fmessage-length=0 -ffreestanding +ASFLAGS := $(GCCFLAGS) -c -x assembler-with-cpp +LDFLAGS := $(GCCFLAGS) -Lpicolibc/arm-none-eabi/picolibc/arm-none-eabi/lib/thumb/v6-m/nofp diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..77a4bdd --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +SRC_DIR := src +OBJ_DIR := obj +BINARY_NAME := host_aprom + +OPTIM := -O0 +# OPTIM := -Ofast -flto +# OPTIM := -Os -flto +include GCC.mk +LIBRARY_MODULES := clk uart timer i2c +include NUC123/NUC123.mk + +CFLAGS += -Wno-gnu-variable-sized-type-not-at-end + +include generic.mk diff --git a/NUC123/NUC123.ld b/NUC123/NUC123.ld new file mode 100644 index 0000000..5c3ca27 --- /dev/null +++ b/NUC123/NUC123.ld @@ -0,0 +1,165 @@ +/* Linker script to configure memory regions. */ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 0x10000 /* 64k */ + RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 0x5000 /* 20k */ +} + +/* Library configurations */ +GROUP(libgcc.a libc.a libm.a libnosys.a) + +/* Linker script to place sections and symbol values. Should be used together + * with other linker script that defines memory regions FLASH and RAM. + * It references following symbols, which must be defined in code: + * Reset_Handler : Entry of reset handler + * + * It defines following symbols, which code can use without definition: + * __exidx_start + * __exidx_end + * __copy_table_start__ + * __copy_table_end__ + * __zero_table_start__ + * __zero_table_end__ + * __etext + * __data_start__ + * __preinit_array_start + * __preinit_array_end + * __init_array_start + * __init_array_end + * __fini_array_start + * __fini_array_end + * __data_end__ + * __bss_start__ + * __bss_end__ + * __end__ + * end + * __HeapLimit + * __StackLimit + * __StackTop + * __stack + * __Vectors_End + * __Vectors_Size + */ +ENTRY(Reset_Handler) + +SECTIONS +{ + .text : + { + KEEP(*(.vectors)) + __Vectors_End = .; + __Vectors_Size = __Vectors_End - __Vectors; + __end__ = .; + + . = . + 16; + + *(.text*) + + KEEP(*(.init)) + KEEP(*(.fini)) + + /* .ctors */ + *crtbegin.o(.ctors) + *crtbegin?.o(.ctors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .ctors) + *(SORT(.ctors.*)) + *(.ctors) + + /* .dtors */ + *crtbegin.o(.dtors) + *crtbegin?.o(.dtors) + *(EXCLUDE_FILE(*crtend?.o *crtend.o) .dtors) + *(SORT(.dtors.*)) + *(.dtors) + + *(.rodata*) + + KEEP(*(.eh_frame*)) + } > FLASH + + .ARM.extab : + { + *(.ARM.extab* .gnu.linkonce.armextab.*) + } > FLASH + + __exidx_start = .; + .ARM.exidx : + { + *(.ARM.exidx* .gnu.linkonce.armexidx.*) + } > FLASH + __exidx_end = .; + + . = ALIGN(4); + __etext = .; + + .data : AT (__etext) + { + . = ALIGN(4); + __data_start__ = .; + *(vtable) + *(.data*) + + . = ALIGN(4); + /* preinit data */ + PROVIDE_HIDDEN (__preinit_array_start = .); + KEEP(*(.preinit_array)) + PROVIDE_HIDDEN (__preinit_array_end = .); + + . = ALIGN(4); + /* init data */ + PROVIDE_HIDDEN (__init_array_start = .); + KEEP(*(SORT(.init_array.*))) + KEEP(*(.init_array)) + PROVIDE_HIDDEN (__init_array_end = .); + + + . = ALIGN(4); + /* finit data */ + PROVIDE_HIDDEN (__fini_array_start = .); + KEEP(*(SORT(.fini_array.*))) + KEEP(*(.fini_array)) + PROVIDE_HIDDEN (__fini_array_end = .); + + KEEP(*(.jcr*)) + . = ALIGN(4); + /* All data end */ + __data_end__ = .; + + } > RAM + + .bss : + { + . = ALIGN(4); + __bss_start__ = .; + *(.bss*) + *(COMMON) + . = ALIGN(4); + __bss_end__ = .; + } > RAM + + .heap (COPY): + { + __HeapBase = .; + __end__ = .; + end = __end__; + KEEP(*(.heap*)) + __HeapLimit = .; + } > RAM + + /* .stack_dummy section doesn't contains any symbols. It is only + * used for linker to calculate size of stack sections, and assign + * values to stack symbols later */ + .stack_dummy (COPY): + { + KEEP(*(.stack*)) + } > RAM + + /* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ + __StackTop = ORIGIN(RAM) + LENGTH(RAM); + __StackLimit = __StackTop - SIZEOF(.stack_dummy); + PROVIDE(__stack = __StackTop); + + /* Check if data + heap + stack exceeds RAM limit */ + ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") +} diff --git a/NUC123/NUC123.mk b/NUC123/NUC123.mk new file mode 100644 index 0000000..0d3f091 --- /dev/null +++ b/NUC123/NUC123.mk @@ -0,0 +1,32 @@ +SELF_DIR := $(dir $(lastword $(MAKEFILE_LIST))) + +INCLUDES += -I${SELF_DIR}inc +INCLUDES += -I${SELF_DIR}StdDriver/inc + +LDFLAGS += -T ${SELF_DIR}NUC123.ld + +$(OBJ_DIR)/startup_NUC123.o: $(SELF_DIR)startup_NUC123.s + @echo Compiling $< + @$(AS) $(ASFLAGS) -o $@ $< + +$(OBJ_DIR)/system_NUC123.o: $(SELF_DIR)system_NUC123.c + @echo Compiling $< + @$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +$(OBJ_DIR)/_syscalls.o: $(SELF_DIR)_syscalls.c + @echo Compiling $< + @$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +.PRECIOUS: $(OBJ_DIR)/NUC123_%.o +$(OBJ_DIR)/NUC123_%.o: $(SELF_DIR)StdDriver/src/%.c + @echo Compiling $< + @$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +DEVICE_OBJECTS := +DEVICE_OBJECTS += $(OBJ_DIR)/system_NUC123.o +ifneq ($(USE_CUSTOM_STARTUP_ASSEMBLY), 1) + DEVICE_OBJECTS += $(OBJ_DIR)/startup_NUC123.o +endif +DEVICE_OBJECTS += $(OBJ_DIR)/_syscalls.o + +$(foreach module,$(LIBRARY_MODULES),$(eval DEVICE_OBJECTS += $(OBJ_DIR)/NUC123_$(module).o)) diff --git a/NUC123/NUC123.svd b/NUC123/NUC123.svd new file mode 100644 index 0000000..a915618 --- /dev/null +++ b/NUC123/NUC123.svd @@ -0,0 +1,786 @@ + + + + + + + + ARM Ltd. + ARM + ARM_Example + ARMCM3 + 1.2 + ARM 32-bit Cortex-M3 Microcontroller based device, CPU clock up to 80MHz, etc. + + ARM Limited (ARM) is supplying this software for use with Cortex-M\n + processor based microcontroller, but can be equally used for other\n + suitable processor architectures. This file can be freely distributed.\n + Modifications to this file shall be clearly marked.\n + \n + THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED\n + OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF\n + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.\n + ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR\n + CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. + + + CM3 + r1p0 + little + true + false + 3 + false + + 8 + 32 + + 32 + read-write + 0x00000000 + 0xFFFFFFFF + + + + + TIMER0 + 1.0 + 32 Timer / Counter, counting up or down from different sources + TIMER + 0x40010000 + 32 + read-write + + + 0 + 0x100 + registers + + + + TIMER0 + 0 + + + + + + CR + Control Register + 0x00 + 32 + read-write + 0x00000000 + 0x1337F7F + + + + + EN + Enable + [0:0] + read-write + + + Disable + Timer is disabled and does not operate + 0 + + + Enable + Timer is enabled and can operate + 1 + + + + + + + RST + Reset Timer + [1:1] + write-only + + + No_Action + Write as ZERO if necessary + 0 + + + Reset_Timer + Reset the Timer + 1 + + + + + + + CNT + Counting direction + [3:2] + read-write + + + Count_UP + Timer Counts UO and wraps, if no STOP condition is set + 0 + + + Count_DOWN + Timer Counts DOWN and wraps, if no STOP condition is set + 1 + + + Toggle + Timer Counts up to MAX, then DOWN to ZERO, if no STOP condition is set + 2 + + + + + + + MODE + Operation Mode + [6:4] + read-write + + + Continous + Timer runs continously + 0 + + + Single_ZERO_MAX + Timer counts to 0x00 or 0xFFFFFFFF (depending on CNT) and stops + 1 + + + Single_MATCH + Timer counts to the Value of MATCH Register and stops + 2 + + + Reload_ZERO_MAX + Timer counts to 0x00 or 0xFFFFFFFF (depending on CNT), loads the RELOAD Value and continues + 3 + + + Reload_MATCH + Timer counts to the Value of MATCH Register, loads the RELOAD Value and continues + 4 + + + + + + + PSC + Use Prescaler + [7:7] + read-write + + + Disabled + Prescaler is not used + 0 + + + Enabled + Prescaler is used as divider + 1 + + + + + + + CNTSRC + Timer / Counter Source Divider + [11:8] + read-write + + + CAP_SRC + Capture Source is used directly + 0 + + + CAP_SRC_div2 + Capture Source is divided by 2 + 1 + + + CAP_SRC_div4 + Capture Source is divided by 4 + 2 + + + CAP_SRC_div8 + Capture Source is divided by 8 + 3 + + + CAP_SRC_div16 + Capture Source is divided by 16 + 4 + + + CAP_SRC_div32 + Capture Source is divided by 32 + 5 + + + CAP_SRC_div64 + Capture Source is divided by 64 + 6 + + + CAP_SRC_div128 + Capture Source is divided by 128 + 7 + + + CAP_SRC_div256 + Capture Source is divided by 256 + 8 + + + + + + + CAPSRC + Timer / Counter Capture Source + [15:12] + read-write + + + CClk + Core Clock + 0 + + + GPIOA_0 + GPIO A, PIN 0 + 1 + + + GPIOA_1 + GPIO A, PIN 1 + 2 + + + GPIOA_2 + GPIO A, PIN 2 + 3 + + + GPIOA_3 + GPIO A, PIN 3 + 4 + + + GPIOA_4 + GPIO A, PIN 4 + 5 + + + GPIOA_5 + GPIO A, PIN 5 + 6 + + + GPIOA_6 + GPIO A, PIN 6 + 7 + + + GPIOA_7 + GPIO A, PIN 7 + 8 + + + GPIOB_0 + GPIO B, PIN 0 + 9 + + + GPIOB_1 + GPIO B, PIN 1 + 10 + + + GPIOB_2 + GPIO B, PIN 2 + 11 + + + GPIOB_3 + GPIO B, PIN 3 + 12 + + + GPIOC_0 + GPIO C, PIN 0 + 13 + + + GPIOC_5 + GPIO C, PIN 1 + 14 + + + GPIOC_6 + GPIO C, PIN 2 + 15 + + + + + + + CAPEDGE + Capture Edge, select which Edge should result in a counter increment or decrement + [17:16] + read-write + + + RISING + Only rising edges result in a counter increment or decrement + 0 + + + FALLING + Only falling edges result in a counter increment or decrement + 1 + + + BOTH + Rising and falling edges result in a counter increment or decrement + 2 + + + + + + + TRGEXT + Triggers an other Peripheral + [21:20] + read-write + + + NONE + No Trigger is emitted + 0 + + + DMA1 + DMA Controller 1 is triggered, dependant on MODE + 1 + + + DMA2 + DMA Controller 2 is triggered, dependant on MODE + 2 + + + UART + UART is triggered, dependant on MODE + 3 + + + + + + + RELOAD + Select RELOAD Register n to reload Timer on condition + [25:24] + read-write + + + RELOAD0 + Selects Reload Register number 0 + 0 + + + RELOAD1 + Selects Reload Register number 1 + 1 + + + RELOAD2 + Selects Reload Register number 2 + 2 + + + RELOAD3 + Selects Reload Register number 3 + 3 + + + + + + + IDR + Selects, if Reload Register number is incremented, decremented or not modified + [27:26] + read-write + + + KEEP + Reload Register number does not change automatically + 0 + + + INCREMENT + Reload Register number is incremented on each match + 1 + + + DECREMENT + Reload Register number is decremented on each match + 2 + + + + + + + S + Starts and Stops the Timer / Counter + [31:31] + read-write + + + STOP + Timer / Counter is stopped + 0 + + + START + Timer / Counter is started + 1 + + + + + + + + + SR + Status Register + 0x04 + 16 + read-write + 0x00000000 + 0xD701 + + + + + RUN + Shows if Timer is running or not + [0:0] + read-only + + + Stopped + Timer is not running + 0 + + + Running + Timer is running + 1 + + + + + + + MATCH + Shows if the MATCH was hit + [8:8] + read-write + + + No_Match + The MATCH condition was not hit + 0 + + + Match_Hit + The MATCH condition was hit + 1 + + + + + + + UN + Shows if an underflow occured. This flag is sticky + [9:9] + read-write + + + No_Underflow + No underflow occured since last clear + 0 + + + Underflow + A minimum of one underflow occured since last clear + 1 + + + + + + + OV + Shows if an overflow occured. This flag is sticky + [10:10] + read-write + + + No_Overflow + No overflow occured since last clear + 0 + + + Overflow_occured + A minimum of one overflow occured since last clear + 1 + + + + + + + RST + Shows if Timer is in RESET state + [12:12] + read-only + + + Ready + Timer is not in RESET state and can operate + 0 + + + In_Reset + Timer is in RESET state and can not operate + 1 + + + + + + + RELOAD + Shows the currently active RELOAD Register + [15:14] + read-only + + + RELOAD0 + Reload Register number 0 is active + 0 + + + RELOAD1 + Reload Register number 1 is active + 1 + + + RELOAD2 + Reload Register number 2 is active + 2 + + + RELOAD3 + Reload Register number 3 is active + 3 + + + + + + + + + INT + Interrupt Register + 0x10 + 16 + read-write + 0x00000000 + 0x0771 + + + + + EN + Interrupt Enable + [0:0] + read-write + + + Disabled + Timer does not generate Interrupts + 0 + + + Enable + Timer triggers the TIMERn Interrupt + 1 + + + + + + + MODE + Interrupt Mode, selects on which condition the Timer should generate an Interrupt + [6:4] + read-write + + + Match + Timer generates an Interrupt when the MATCH condition is hit + 0 + + + Underflow + Timer generates an Interrupt when it underflows + 1 + + + Overflow + Timer generates an Interrupt when it overflows + 2 + + + + + + + + + COUNT + The Counter Register reflects the actual Value of the Timer/Counter + 0x20 + 32 + read-write + 0x00000000 + 0xFFFFFFFF + + + + + MATCH + The Match Register stores the compare Value for the MATCH condition + 0x24 + 32 + read-write + 0x00000000 + 0xFFFFFFFF + + + + + PRESCALE_RD + The Prescale Register stores the Value for the prescaler. The cont event gets divided by this value + 0x28 + 32 + read-only + 0x00000000 + 0xFFFFFFFF + + + + + PRESCALE_WR + The Prescale Register stores the Value for the prescaler. The cont event gets divided by this value + 0x28 + 32 + write-only + 0x00000000 + 0xFFFFFFFF + + + + + + 4 + 4 + 0,1,2,3 + RELOAD[%s] + The Reload Register stores the Value the COUNT Register gets reloaded on a when a condition was met. + 0x50 + 32 + read-write + 0x00000000 + 0xFFFFFFFF + + + + + + + TIMER1 + 0x40010100 + + TIMER1 + 4 + + + + + + TIMER2 + 0x40010200 + + TIMER2 + 6 + + + + diff --git a/NUC123/StdDriver/inc/adc.h b/NUC123/StdDriver/inc/adc.h new file mode 100644 index 0000000..5782cc0 --- /dev/null +++ b/NUC123/StdDriver/inc/adc.h @@ -0,0 +1,343 @@ +/**************************************************************************//** + * @file adc.h + * @version V3.00 + * $Revision: 7 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series ADC Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __ADC_H__ +#define __ADC_H__ + +#include "NUC123.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup ADC_Driver ADC Driver + @{ +*/ + +/** @addtogroup ADC_EXPORTED_CONSTANTS ADC Exported Constants + @{ +*/ +/*---------------------------------------------------------------------------------------------------------*/ +/* ADCR Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define ADC_ADCR_ADEN_CONVERTER_DISABLE (0UL<ADCHER = ((adc)->ADCHER & ~ADC_ADCHER_PRESEL_Msk) | (u32Source)) + +/** + * @brief Enable PDMA transfer. + * @param[in] adc The pointer of the specified ADC module + * @return None + * @details Enable PDMA to transfer the conversion data. + * @note While enable PDMA transfer, software must set ADIE = 0 to disable interrupt. + */ +#define ADC_ENABLE_PDMA(adc) ((adc)->ADCR |= ADC_ADCR_PTEN_Msk) + +/** + * @brief Disable PDMA transfer. + * @param[in] adc The pointer of the specified ADC module + * @return None + * @details Disable PDMA to transfer the conversion data. + */ +#define ADC_DISABLE_PDMA(adc) ((adc)->ADCR &= ~ADC_ADCR_PTEN_Msk) + +/** + * @brief Get conversion data of specified channel. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32ChNum ADC Channel, valid value are from 0 to 7. + * @return 16-bit data. + * @details Read RSLT bit field to get conversion data. + */ +#define ADC_GET_CONVERSION_DATA(adc, u32ChNum) ((adc)->ADDR[(u32ChNum)] & ADC_ADDR_RSLT_Msk) + +/** + * @brief Return the user-specified interrupt flags. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status. + * Valid values are: + * - \ref ADC_ADF_INT :Convert complete interrupt flag. + * - \ref ADC_CMP0_INT :Comparator 0 interrupt flag. + * - \ref ADC_CMP1_INT :Comparator 1 interrupt flag. + * @return User specified interrupt flags. + * @details Get the status of the ADC interrupt flag. + */ +#define ADC_GET_INT_FLAG(adc, u32Mask) ((adc)->ADSR & (u32Mask)) + +/** + * @brief This macro clear the selected interrupt status bits. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32Mask The combination of following interrupt status bits. Each bit corresponds to a interrupt status. + * Valid values are: + * - \ref ADC_ADF_INT :Convert complete interrupt flag. + * - \ref ADC_CMP0_INT :Comparator 0 interrupt flag. + * - \ref ADC_CMP1_INT :Comparator 1 interrupt flag. + * @return None + * @details ADF (ADSR[0])/CMPF0 (ADSR[1])/CMPF0 (ADSR[2]) can be cleared by writing 1 to itself. + */ +#define ADC_CLR_INT_FLAG(adc, u32Mask) ((adc)->ADSR = (u32Mask)) + +/** + * @brief Get the busy state of ADC. + * @param[in] adc The pointer of the specified ADC module. + * @retval 0 ADC is not busy. + * @retval 1 ADC is busy. + * @details BUSY(ADSR[3])is mirror of as ADST bit (ADCR[11]). + */ +#define ADC_IS_BUSY(adc) ((adc)->ADSR & ADC_ADSR_BUSY_Msk ? 1 : 0) + +/** + * @brief Check if the ADC conversion data is over written or not. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32ChNum ADC Channel, valid value are from 0 to 7. + * @retval 0 ADC data is not overrun. + * @retval 1 ADC data is overrun. + * @details OVERRUN (ADSR[23:16]) is a mirror to OVERRUN (ADDR0~7[16]). + */ +#define ADC_IS_DATA_OVERRUN(adc, u32ChNum) ((adc)->ADSR & (0x1 << (ADC_ADSR_OVERRUN_Pos + (u32ChNum))) ? 1 : 0) + +/** + * @brief Check if the ADC conversion data is valid or not. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32ChNum ADC Channel, valid value are from 0 to 7. + * @retval 0 ADC data is not valid. + * @retval 1 ADC data is valid. + * @details VALID (ADDR0~7[17]) is set to 1 when corresponding channel analog input conversion is completed and cleared by hardware after ADDR register is read. + */ +#define ADC_IS_DATA_VALID(adc, u32ChNum) ((adc)->ADSR & (0x1 << (ADC_ADSR_VALID_Pos + (u32ChNum))) ? 1 : 0) + +/** + * @brief Power down ADC module. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details Disable A/D converter analog circuit for saving power consumption. + * @note None + */ +#define ADC_POWER_DOWN(adc) ((adc)->ADCR &= ~ADC_ADCR_ADEN_Msk) + +/** + * @brief Power on ADC module. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details Before starting A/D conversion function, ADEN bit (ADCR[0]) should be set to 1. + */ +#define ADC_POWER_ON(adc) ((adc)->ADCR |= ADC_ADCR_ADEN_Msk) + +/** + * @brief Configure the comparator 0 and enable it. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32ChNum Specifies the source channel, valid value are from 0 to 7. + * @param[in] u32Condition Specifies the compare condition. Valid values are: + * - \ref ADC_ADCMPR_CMPCOND_LESS_THAN :The compare condition is "less than the compare value". + * - \ref ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL :The compare condition is "greater than or equal to the compare value. + * @param[in] u32Data Specifies the compare value, valid value are between 0 ~ 0x3FF. + * @param[in] u32MatchCount Specifies the match count setting, valid values are between 1~16. + * @return None + * @details For example, ADC_ENABLE_CMP0(ADC, 5, ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL, 0x200, 10); + * Means ADC will assert comparator 0 flag if channel 5 conversion result is greater or + * equal to 0x200 for 10 times continuously. + * \hideinitializer + */ +#define ADC_ENABLE_CMP0(adc, \ + u32ChNum, \ + u32Condition, \ + u32Data, \ + u32MatchCount) ((adc)->ADCMPR[0] = ((u32ChNum) << ADC_ADCMPR_CMPCH_Pos) | \ + (u32Condition) | \ + ((u32Data) << ADC_ADCMPR_CMPD_Pos) | \ + (((u32MatchCount) - 1) << ADC_ADCMPR_CMPMATCNT_Pos) |\ + ADC_ADCMPR_CMPEN_Msk) + +/** + * @brief Disable comparator 0. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details Set CMPEN (ADCMPR0[0]) to 0 to disable ADC controller to compare CMPD (ADCMPR0[25:16]). + */ +#define ADC_DISABLE_CMP0(adc) ((adc)->ADCMPR[0] = 0) + +/** + * @brief Configure the comparator 1 and enable it. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32ChNum Specifies the source channel, valid value are from 0 to 7. + * @param[in] u32Condition Specifies the compare condition. Valid values are: + * - \ref ADC_ADCMPR_CMPCOND_LESS_THAN :The compare condition is "less than the compare value". + * - \ref ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL :The compare condition is "greater than or equal to the compare value. + * @param[in] u32Data Specifies the compare value, valid value are between 0 ~ 0x3FF. + * @param[in] u32MatchCount Specifies the match count setting, valid values are between 1~16. + * @return None + * @details For example, ADC_ENABLE_CMP1(ADC, 5, ADC_ADCMPR_CMPCOND_GREATER_OR_EQUAL, 0x200, 10); + * Means ADC will assert comparator 1 flag if channel 5 conversion result is greater or + * equal to 0x200 for 10 times continuously. + * \hideinitializer + */ +#define ADC_ENABLE_CMP1(adc, \ + u32ChNum, \ + u32Condition, \ + u32Data, \ + u32MatchCount) ((adc)->ADCMPR[1] = ((u32ChNum) << ADC_ADCMPR_CMPCH_Pos) | \ + (u32Condition) | \ + ((u32Data) << ADC_ADCMPR_CMPD_Pos) | \ + (((u32MatchCount) - 1) << ADC_ADCMPR_CMPMATCNT_Pos) |\ + ADC_ADCMPR_CMPEN_Msk) + +/** + * @brief Disable comparator 1. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details Set CMPEN (ADCMPR1[0]) to 0 to disable ADC controller to compare CMPD (ADCMPR1[25:16]). + */ +#define ADC_DISABLE_CMP1(adc) ((adc)->ADCMPR[1] = 0) + +/** + * @brief Set ADC input channel. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32Mask Channel enable bit. Each bit corresponds to a input channel. Bit 0 is channel 0, bit 1 is channel 1..., bit 7 is channel 7. + * @return None + * @details Enabled channel will be converted while ADC starts. + * @note NUC123 series MCU ADC can only convert 1 channel at a time. If more than 1 channels are enabled, only channel + * with smallest number will be convert. + */ +#define ADC_SET_INPUT_CHANNEL(adc, u32Mask) ((adc)->ADCHER = ((adc)->ADCHER & ~ADC_ADCHER_CHEN_Msk) | (u32Mask)) + +/** + * @brief Start the A/D conversion. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details ADST (ADCR[11]) can be set to 1 from three sources: software, PWM Center-aligned trigger and external pin STADC. + */ +#define ADC_START_CONV(adc) ((adc)->ADCR |= ADC_ADCR_ADST_Msk) + +/** + * @brief Stop the A/D conversion. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details ADST (ADCR[11]) will be cleared to 0 by hardware automatically at the ends of single mode and single-cycle scan mode. + * In continuous scan mode, A/D conversion is continuously performed until software writes 0 to this bit or chip reset. + */ +#define ADC_STOP_CONV(adc) ((adc)->ADCR &= ~ADC_ADCR_ADST_Msk) + +void ADC_Open(ADC_T *adc, + uint32_t u32InputMode, + uint32_t u32OpMode, + uint32_t u32ChMask); +void ADC_Close(ADC_T *adc); +void ADC_EnableHWTrigger(ADC_T *adc, + uint32_t u32Source, + uint32_t u32Param); +void ADC_DisableHWTrigger(ADC_T *adc); +void ADC_EnableInt(ADC_T *adc, uint32_t u32Mask); +void ADC_DisableInt(ADC_T *adc, uint32_t u32Mask); + + + +/*@}*/ /* end of group ADC_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group ADC_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__ADC_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/clk.h b/NUC123/StdDriver/inc/clk.h new file mode 100644 index 0000000..c215f8a --- /dev/null +++ b/NUC123/StdDriver/inc/clk.h @@ -0,0 +1,453 @@ +/**************************************************************************//** + * @file clk.h + * @version V3.0 + * $Revision: 16 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series Clock Control Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __CLK_H__ +#define __CLK_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup CLK_Driver CLK Driver + @{ +*/ + +/** @addtogroup CLK_EXPORTED_CONSTANTS CLK Exported Constants + @{ +*/ + +#define FREQ_25MHZ 25000000 +#define FREQ_50MHZ 50000000 +#define FREQ_72MHZ 72000000 +#define FREQ_100MHZ 100000000 +#define FREQ_200MHZ 200000000 + + +/*---------------------------------------------------------------------------------------------------------*/ +/* CLKSEL0 constant definitions. */ +/*---------------------------------------------------------------------------------------------------------*/ +#define CLK_CLKSEL0_HCLK_S_HXT (0x0UL<>30) & 0x3) /*!< Calculate APBCLK offset on MODULE index, 0x0:AHBCLK, 0x1:APBCLK */ +#define MODULE_CLKSEL(x) (((x) >>28) & 0x3) /*!< Calculate CLKSEL offset on MODULE index, 0x0:CLKSEL0, 0x1:CLKSEL1, 0x2:CLKSEL2 */ +#define MODULE_CLKSEL_Msk(x) (((x) >>25) & 0x7) /*!< Calculate CLKSEL mask offset on MODULE index */ +#define MODULE_CLKSEL_Pos(x) (((x) >>20) & 0x1f) /*!< Calculate CLKSEL position offset on MODULE index */ +#define MODULE_CLKDIV(x) (((x) >>18) & 0x3) /*!< Calculate APBCLK CLKDIV on MODULE index, 0x0:CLKDIV */ +#define MODULE_CLKDIV_Msk(x) (((x) >>10) & 0xff) /*!< Calculate CLKDIV mask offset on MODULE index */ +#define MODULE_CLKDIV_Pos(x) (((x) >>5 ) & 0x1f) /*!< Calculate CLKDIV position offset on MODULE index */ +#define MODULE_IP_EN_Pos(x) (((x) >>0 ) & 0x1f) /*!< Calculate APBCLK offset on MODULE index */ +#define MODULE_NoMsk 0x0 /*!< Not mask on MODULE index */ +#define NA MODULE_NoMsk /*!< Not Available */ + +#define MODULE_APBCLK_ENC(x) (((x) & 0x03) << 30) /*!< MODULE index, 0x0:AHBCLK, 0x1:APBCLK */ +#define MODULE_CLKSEL_ENC(x) (((x) & 0x03) << 28) /*!< CLKSEL offset on MODULE index, 0x0:CLKSEL0, 0x1:CLKSEL1, 0x2:CLKSEL2 */ +#define MODULE_CLKSEL_Msk_ENC(x) (((x) & 0x07) << 25) /*!< CLKSEL mask offset on MODULE index */ +#define MODULE_CLKSEL_Pos_ENC(x) (((x) & 0x1f) << 20) /*!< CLKSEL position offset on MODULE index */ +#define MODULE_CLKDIV_ENC(x) (((x) & 0x03) << 18) /*!< APBCLK CLKDIV on MODULE index, 0x0:CLKDIV */ +#define MODULE_CLKDIV_Msk_ENC(x) (((x) & 0xff) << 10) /*!< CLKDIV mask offset on MODULE index */ +#define MODULE_CLKDIV_Pos_ENC(x) (((x) & 0x1f) << 5) /*!< CLKDIV position offset on MODULE index */ +#define MODULE_IP_EN_Pos_ENC(x) (((x) & 0x1f) << 0) /*!< APBCLK offset on MODULE index */ + + +#define PDMA_MODULE (MODULE_APBCLK_ENC( 0)|MODULE_IP_EN_Pos_ENC(CLK_AHBCLK_PDMA_EN_Pos) |\ + MODULE_CLKSEL_ENC(NA)|MODULE_CLKSEL_Msk_ENC(NA)|MODULE_CLKSEL_Pos_ENC(NA)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< PDMA Module */ +#define ISP_MODULE (MODULE_APBCLK_ENC( 0)|MODULE_IP_EN_Pos_ENC(CLK_AHBCLK_ISP_EN_Pos) |\ + MODULE_CLKSEL_ENC(NA)|MODULE_CLKSEL_Msk_ENC(NA)|MODULE_CLKSEL_Pos_ENC(NA)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< ISP Module */ + +#define WDT_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_WDT_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC( 0)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< WDT Module */ +#define TMR0_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_TMR0_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 7)|MODULE_CLKSEL_Pos_ENC( 8)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< TMR0 Module */ +#define TMR1_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_TMR1_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 7)|MODULE_CLKSEL_Pos_ENC(12)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< TMR1 Module */ +#define TMR2_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_TMR2_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 7)|MODULE_CLKSEL_Pos_ENC(16)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< TMR2 Module */ +#define TMR3_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_TMR3_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 7)|MODULE_CLKSEL_Pos_ENC(20)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< TMR3 Module */ +#define FDIV_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_FDIV_EN_Pos) |\ + MODULE_CLKSEL_ENC( 2)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC( 2)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< FDIV Module */ + +#define I2C0_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_I2C0_EN_Pos) |\ + MODULE_CLKSEL_ENC(NA)|MODULE_CLKSEL_Msk_ENC(NA)|MODULE_CLKSEL_Pos_ENC(NA)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< I2C0 Module */ +#define I2C1_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_I2C1_EN_Pos) |\ + MODULE_CLKSEL_ENC(NA)|MODULE_CLKSEL_Msk_ENC(NA)|MODULE_CLKSEL_Pos_ENC(NA)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< I2C1 Module */ +#define SPI0_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_SPI0_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 1)|MODULE_CLKSEL_Pos_ENC( 4)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< SPI0 Module */ +#define SPI1_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_SPI1_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 1)|MODULE_CLKSEL_Pos_ENC( 5)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< SPI1 Module */ +#define SPI2_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_SPI2_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 1)|MODULE_CLKSEL_Pos_ENC( 6)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< SPI2 Module */ + +#define UART0_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_UART0_EN_Pos)|\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC(24)|\ + MODULE_CLKDIV_ENC( 0)|MODULE_CLKDIV_Msk_ENC(0x0F)|MODULE_CLKDIV_Pos_ENC( 8)) /*!< UART0 Module */ +#define UART1_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_UART1_EN_Pos)|\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC(24)|\ + MODULE_CLKDIV_ENC( 0)|MODULE_CLKDIV_Msk_ENC(0x0F)|MODULE_CLKDIV_Pos_ENC( 8)) /*!< UART1 Module */ + +#define PWM01_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_PWM01_EN_Pos)|\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC(28)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< PWM01 Module */ +#define PWM23_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_PWM23_EN_Pos)|\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC(30)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< PWM23 Module */ +#define USBD_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_USBD_EN_Pos) |\ + MODULE_CLKSEL_ENC(NA)|MODULE_CLKSEL_Msk_ENC(NA)|MODULE_CLKSEL_Pos_ENC(NA)|\ + MODULE_CLKDIV_ENC( 0)|MODULE_CLKDIV_Msk_ENC(0x0F)|MODULE_CLKDIV_Pos_ENC(4)) /*!< USBD Module */ +#define ADC_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_ADC_EN_Pos) |\ + MODULE_CLKSEL_ENC( 1)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC( 2)|\ + MODULE_CLKDIV_ENC( 0)|MODULE_CLKDIV_Msk_ENC(0xFF)|MODULE_CLKDIV_Pos_ENC(16)) /*!< ADC Module */ +#define I2S_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_I2S_EN_Pos) |\ + MODULE_CLKSEL_ENC( 2)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC( 0)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< I2S Module */ +#define PS2_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_PS2_EN_Pos) |\ + MODULE_CLKSEL_ENC(NA)|MODULE_CLKSEL_Msk_ENC(NA)|MODULE_CLKSEL_Pos_ENC(NA)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< PS2 Module */ + +#define WWDT_MODULE (MODULE_APBCLK_ENC( 1)|MODULE_IP_EN_Pos_ENC(CLK_APBCLK_WDT_EN_Pos) |\ + MODULE_CLKSEL_ENC( 2)|MODULE_CLKSEL_Msk_ENC( 3)|MODULE_CLKSEL_Pos_ENC(16)|\ + MODULE_CLKDIV_ENC(NA)|MODULE_CLKDIV_Msk_ENC(NA)|MODULE_CLKDIV_Pos_ENC(NA)) /*!< WWDT Module */ + +/*@}*/ /* end of group CLK_EXPORTED_CONSTANTS */ + + +/** @addtogroup CLK_EXPORTED_FUNCTIONS CLK Exported Functions + @{ +*/ + + +/** + * @brief Get PLL clock frequency + * @param None + * @return PLL frequency + * @details This function get PLL frequency. The frequency unit is Hz. + */ +__STATIC_INLINE uint32_t CLK_GetPLLClockFreq(void) +{ + uint32_t u32PllFreq = 0, u32PllReg; + uint32_t u32FIN, u32NF, u32NR, u32NO; + uint8_t au8NoTbl[4] = {1, 2, 2, 4}; + + u32PllReg = CLK->PLLCON; + + if(u32PllReg & (CLK_PLLCON_PD_Msk | CLK_PLLCON_OE_Msk)) + return 0; /* PLL is in power down mode or fix low */ + + if(u32PllReg & CLK_PLLCON_PLL_SRC_HIRC) + u32FIN = __HIRC; /* PLL source clock from HIRC */ + else + u32FIN = __HXT; /* PLL source clock from HXT */ + + if(u32PllReg & CLK_PLLCON_BP_Msk) + return u32FIN; /* PLL is in bypass mode */ + + /* PLL is output enabled in normal work mode */ + u32NO = au8NoTbl[((u32PllReg & CLK_PLLCON_OUT_DV_Msk) >> CLK_PLLCON_OUT_DV_Pos)]; + u32NF = ((u32PllReg & CLK_PLLCON_FB_DV_Msk) >> CLK_PLLCON_FB_DV_Pos) + 2; + u32NR = ((u32PllReg & CLK_PLLCON_IN_DV_Msk) >> CLK_PLLCON_IN_DV_Pos) + 2; + + /* u32FIN is shifted 2 bits to avoid overflow */ + u32PllFreq = (((u32FIN >> 2) * u32NF) / (u32NR * u32NO) << 2); + + return u32PllFreq; +} + +/** + * @brief This function execute delay function. + * @param[in] us Delay time. The Max value is 2^24 / CPU Clock(MHz). Ex: + * 72MHz => 233016us, 50MHz => 335544us, + 48MHz => 349525us, 28MHz => 699050us ... + * @return None + * @details Use the SysTick to generate the delay time and the UNIT is in us. + * The SysTick clock source is from HCLK, i.e the same as system core clock. + * User can use SystemCoreClockUpdate() to calculate CyclesPerUs automatically before using this function. + */ +__STATIC_INLINE void CLK_SysTickDelay(uint32_t us) +{ + SysTick->LOAD = us * CyclesPerUs; + SysTick->VAL = (0x00); + SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; + + /* Waiting for down-count to zero */ + while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0); + + /* Disable SysTick counter */ + SysTick->CTRL = 0; +} + +/** + * @brief This function execute long delay function. + * @param[in] us Delay time. + * @return None + * @details Use the SysTick to generate the long delay time and the UNIT is in us. + * The SysTick clock source is from HCLK, i.e the same as system core clock. + * User can use SystemCoreClockUpdate() to calculate CyclesPerUs automatically before using this function. + */ +__STATIC_INLINE void CLK_SysTickLongDelay(uint32_t us) +{ + uint32_t delay; + + /* It should <= 233016us for each delay loop */ + delay = 233016L; + + do + { + if(us > delay) + { + us -= delay; + } + else + { + delay = us; + us = 0UL; + } + + SysTick->LOAD = delay * CyclesPerUs; + SysTick->VAL = (0x0UL); + SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk; + + /* Waiting for down-count to zero */ + while((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0UL); + + /* Disable SysTick counter */ + SysTick->CTRL = 0UL; + + }while(us > 0UL); + +} + + +void CLK_DisableCKO(void); +void CLK_EnableCKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv, uint32_t u32ClkDivBy1En); +void CLK_PowerDown(void); +void CLK_Idle(void); +uint32_t CLK_GetHXTFreq(void); +uint32_t CLK_GetHCLKFreq(void); +uint32_t CLK_GetPCLKFreq(void); +uint32_t CLK_GetCPUFreq(void); +uint32_t CLK_SetCoreClock(uint32_t u32Hclk); +void CLK_SetHCLK(uint32_t u32ClkSrc, uint32_t u32ClkDiv); +void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv); +void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc); +void CLK_EnableXtalRC(uint32_t u32ClkMask); +void CLK_DisableXtalRC(uint32_t u32ClkMask); +void CLK_EnableModuleClock(uint32_t u32ModuleIdx); +void CLK_DisableModuleClock(uint32_t u32ModuleIdx); +uint32_t CLK_EnablePLL(uint32_t u32PllClkSrc, uint32_t u32PllFreq); +void CLK_DisablePLL(void); +uint32_t CLK_WaitClockReady(uint32_t u32ClkMask); +void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count); +void CLK_DisableSysTick(void); + + +/*@}*/ /* end of group CLK_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group CLK_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__CLK_H__ + + + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/crc.h b/NUC123/StdDriver/inc/crc.h new file mode 100644 index 0000000..5a4e8af --- /dev/null +++ b/NUC123/StdDriver/inc/crc.h @@ -0,0 +1,166 @@ +/**************************************************************************//** + * @file crc.h + * @version V3.00 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series CRC driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ +#ifndef __CRC_H__ +#define __CRC_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup CRC_Driver CRC Driver + @{ +*/ + +/** @addtogroup CRC_EXPORTED_CONSTANTS CRC Exported Constants + @{ +*/ +/*---------------------------------------------------------------------------------------------------------*/ +/* CRC Polynomial Mode Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define CRC_CCITT 0x00000000UL /*!DMAIER |= (u32Mask)) + +/** + * @brief Disable CRC Interrupt + * + * @param[in] u32Mask Interrupt mask. Valid values are: + * - \ref CRC_DMAIER_CRC_BLKD_IE_Msk + * - \ref CRC_DMAIER_CRC_TABORT_IE_Msk + * + * @return None + * + * @details This macro disable the specify CRC interrupt function by u32Mask setting. + */ +#define CRC_DISABLE_INT(u32Mask) (CRC->DMAIER &= ~(u32Mask)) + +/** + * @brief Get CRC Interrupt Flag + * + * @param None + * + * @return Interrupt Flag Status + * + * @details This macro gets the CRC interrupt flags. + */ +#define CRC_GET_INT_FLAG() ((uint32_t)(CRC->DMAISR)) + +/** + * @brief Clear CRC Interrupt Flag + * + * @param[in] u32Mask Interrupt mask. Valid values are: + * - \ref CRC_DMAISR_CRC_BLKD_IF_Msk + * - \ref CRC_DMAISR_CRC_TABORT_IF_Msk + * + * @return None + * + * @details This macro clear the specify CRC interrupt flag by u32Mask setting. + */ +#define CRC_CLR_INT_FLAG(u32Mask) (CRC->DMAISR = (u32Mask)) + +/** + * @brief Set CRC seed value + * + * @param[in] u32Seed Seed value + * + * @return None + * + * @details This macro set CRC seed value. + * + * @note User must to setting CRC_RST (CRC_CTL[1] CRC Engine Reset) to reload the new seed value + * to CRC controller. + */ +#define CRC_SET_SEED(u32Seed) { CRC->SEED = (u32Seed); CRC->CTL |= CRC_CTL_CRC_RST_Msk; } + +/** + * @brief Get CRC Seed value + * + * @param None + * + * @return Seed Value + * + * @details This macro gets the current CRC seed value. + */ +#define CRC_GET_SEED() ((uint32_t)(CRC->SEED)) + +/** + * @brief CRC write data + * + * @param[in] u32Data write data + * + * @return None + * + * @details User can write data directly by this macro to perform CRC operation. + */ +#define CRC_WRITE_DATA(u32Data) (CRC->WDATA = (u32Data)) + + +/*********************************************************************/ +void CRC_Open(uint32_t u32Mode, uint32_t u32Attribute, uint32_t u32Seed, uint32_t u32DataLen); +void CRC_StartDMATransfer(uint32_t u32SrcAddr, uint32_t u32ByteCount); +uint32_t CRC_GetChecksum(void); + +/*@}*/ /* end of group CRC_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group CRC_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__CRC_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/fmc.h b/NUC123/StdDriver/inc/fmc.h new file mode 100644 index 0000000..1cfbe37 --- /dev/null +++ b/NUC123/StdDriver/inc/fmc.h @@ -0,0 +1,481 @@ +/**************************************************************************//** + * @file FMC.h + * @version V3.0 + * $Revision: 10 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series Flash Memory Controller Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __FMC_H__ +#define __FMC_H__ + +#include "NUC123.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup FMC_Driver FMC Driver + @{ +*/ + +/** @addtogroup FMC_EXPORTED_CONSTANTS FMC Exported Constants + @{ +*/ + + +/*---------------------------------------------------------------------------------------------------------*/ +/* Define Base Address */ +/*---------------------------------------------------------------------------------------------------------*/ +#define FMC_APROM_BASE 0x00000000UL /*!< APROM Base Address */ +#define FMC_LDROM_BASE 0x00100000UL /*!< LDROM Base Address */ +#define FMC_CONFIG_BASE 0x00300000UL /*!< CONFIG Base Address */ + +#define FMC_FLASH_PAGE_SIZE 0x200 /*!< Flash Page Size (512 Bytes) */ +#define FMC_LDROM_SIZE 0x1000 /*!< LDROM Size (4K Bytes) */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* ISPCON constant definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define FMC_ISPCON_BS_LDROM 0x2 /*!< ISPCON setting to select to boot from LDROM */ +#define FMC_ISPCON_BS_APROM 0x0 /*!< ISPCON setting to select to boot from APROM */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* ISPCMD constant definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define FMC_ISPCMD_READ 0x00 /*!< ISP Command: Read Flash */ +#define FMC_ISPCMD_PROGRAM 0x21 /*!< ISP Command: Program Flash */ +#define FMC_ISPCMD_PAGE_ERASE 0x22 /*!< ISP Command: Page Erase Flash */ +#define FMC_ISPCMD_VECMAP 0x2e /*!< ISP Command: Set VECMAP */ +#define FMC_ISPCMD_READ_UID 0x04 /*!< ISP Command: Read Unique ID */ +#define FMC_ISPCMD_READ_CID 0x0B /*!< ISP Command: Read Company ID */ +#define FMC_ISPCMD_READ_DID 0x0C /*!< ISP Command: Read Device ID */ + + +/*@}*/ /* end of group FMC_EXPORTED_CONSTANTS */ + +/** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions + @{ +*/ + +/*---------------------------------------------------------------------------------------------------------*/ +/* FMC Macro Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +/** + * @brief Enable ISP Function + * + * @param None + * + * @return None + * + * @details This function will set ISPEN bit of ISPCON control register to enable ISP function. + * + */ +#define FMC_ENABLE_ISP() (FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk) + + +/** + * @brief Disable ISP Function + * + * @param None + * + * @return None + * + * @details This function will clear ISPEN bit of ISPCON control register to disable ISP function. + * + */ +#define FMC_DISABLE_ISP() (FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk) + + +/** + * @brief Enable LDROM Update Function + * + * @param None + * + * @return None + * + * @details This function will set LDUEN bit of ISPCON control register to enable LDROM update function. + * User needs to set LDUEN bit before they can update LDROM. + * + */ +#define FMC_ENABLE_LD_UPDATE() (FMC->ISPCON |= FMC_ISPCON_LDUEN_Msk) + + + +/** + * @brief Disable LDROM Update Function + * + * @param None + * + * @return None + * + * @details This function will set ISPEN bit of ISPCON control register to disable LDROM update function. + * + */ +#define FMC_DISABLE_LD_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_LDUEN_Msk) /*!< Disable LDROM Update Function */ + + + +/** + * @brief Enable User Configuration Update Function + * + * @param None + * + * @return None + * + * @details This function will set CFGUEN bit of ISPCON control register to enable User Configuration update function. + * User needs to set CFGUEN bit before they can update User Configuration area. + * + */ +#define FMC_ENABLE_CFG_UPDATE() (FMC->ISPCON |= FMC_ISPCON_CFGUEN_Msk) + +/** + * @brief Disable User Configuration Update Function + * + * @param None + * + * @return None + * + * @details This function will clear CFGUEN bit of ISPCON control register to disable User Configuration update function. + * + */ +#define FMC_DISABLE_CFG_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_CFGUEN_Msk) /*!< Disable CONFIG Update Function */ + + +/** + * @brief Enable APROM Update Function + * + * @param None + * + * @return None + * + * @details This function will set APUEN bit of ISPCON control register to enable APROM update function. + * User needs to set APUEN bit before they can update APROM in APROM boot mode. + * + */ +#define FMC_ENABLE_AP_UPDATE() (FMC->ISPCON |= FMC_ISPCON_APUEN_Msk) + + +/** + * @brief Disable APROM Update Function + * + * @param None + * + * @return None + * + * @details This function will clear APUEN bit of ISPCON control register to disable APROM update function. + * + */ +#define FMC_DISABLE_AP_UPDATE() (FMC->ISPCON &= ~FMC_ISPCON_APUEN_Msk) /*!< Disable APROM Update Function */ + +/** + * @brief Get ISP fail flag + * + * @param None + * + * @retval 0 Previous ISP command execution result is successful + * @retval 1 Previous ISP command execution result is fail + * + * @details ISPFF flag of ISPCON is used to indicate ISP command success or fail. + * This function will return the ISPFF flag to identify ISP command OK or fail. + * + */ +#define FMC_GET_FAIL_FLAG() ((FMC->ISPCON & FMC_ISPCON_ISPFF_Msk) ? 1 : 0) + + +/** + * @brief Select booting from APROM + * + * @param None + * + * @return None + * + * @details If MCU is working without IAP, user need to set BS bit of ISPCON and reset CPU to execute the code of LDROM/APROM. + * This function is used to set BS bit of ISPCON to boot to APROM. + * + * @note To valid new BS bit setting, user also need to trigger CPU reset or System Reset Request after setting BS bit. + * + */ +#define FMC_SET_APROM_BOOT() (FMC->ISPCON &= ~FMC_ISPCON_BS_Msk) + +/** + * @brief Select booting from APROM + * + * @param None + * + * @return None + * + * @details If MCU is working without IAP, user need to set/clear BS bit of ISPCON and reset CPU to execute the code of APROM/LDROM. + * This function is used to clear BS bit of ISPCON to boot to LDROM. + * + * @note To valid new BS bit setting, user also need to trigger CPU reset or System Reset Request after clear BS bit. + * + */ +#define FMC_SET_LDROM_BOOT() (FMC->ISPCON |= FMC_ISPCON_BS_Msk) + + +/*---------------------------------------------------------------------------------------------------------*/ +/* inline functions */ +/*---------------------------------------------------------------------------------------------------------*/ +/** + * @brief Program 32-bit data into specified address of flash + * + * @param[in] u32addr Flash address include APROM, LDROM, Data Flash, and CONFIG + * @param[in] u32data 32-bit Data to program + * + * @return None + * + * @details To program word data into Flash include APROM, LDROM, Data Flash, and CONFIG. + * The corresponding functions in CONFIG are listed in FMC section of Technical Reference Manual. + * + */ +static __INLINE void FMC_Write(uint32_t u32addr, uint32_t u32data) +{ + FMC->ISPCMD = FMC_ISPCMD_PROGRAM; /* Set ISP Command Code */ + FMC->ISPADR = u32addr; /* Set Target ROM Address. The address must be word alignment. */ + FMC->ISPDAT = u32data; /* Set Data to Program */ + FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG); /* Waiting for ISP Done */ +} + +/** + * @brief Read 32-bit Data from specified address of flash + * + * @param[in] u32addr Flash address include APROM, LDROM, Data Flash, and CONFIG + * + * @return The data of specified address + * + * @details To read word data from Flash include APROM, LDROM, Data Flash, and CONFIG. + * + */ +static __INLINE uint32_t FMC_Read(uint32_t u32addr) +{ + FMC->ISPCMD = FMC_ISPCMD_READ; /* Set ISP Command Code */ + FMC->ISPADR = u32addr; /* Set Target ROM Address. The address must be word alignment. */ + FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG); /* Waiting for ISP Done */ + + return FMC->ISPDAT; +} + + +/** + * @brief Flash page erase + * + * @param[in] u32addr Flash address including APROM, LDROM, Data Flash, and CONFIG + * + * @details To do flash page erase. The target address could be APROM, LDROM, Data Flash, or CONFIG. + * The page size is 512 bytes. + * + * @retval 0 Success + * @retval -1 Erase failed + * + */ +static __INLINE int32_t FMC_Erase(uint32_t u32addr) +{ + FMC->ISPCMD = FMC_ISPCMD_PAGE_ERASE; /* Set ISP Command Code */ + FMC->ISPADR = u32addr; /* Set Target ROM Address. The address must be page alignment. */ + FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG); /* Waiting for ISP Done */ + + /* Check ISPFF flag to know whether erase OK or fail. */ + if(FMC->ISPCON & FMC_ISPCON_ISPFF_Msk) + { + FMC->ISPCON |= FMC_ISPCON_ISPFF_Msk; + return -1; + } + return 0; +} + +/** + * @brief Read Unique ID + * + * @param[in] u8index UID index. 0 = UID[31:0], 1 = UID[63:32], 2 = UID[95:64] + * + * @return The 32-bit unique ID data of specified UID index. + * + * @details To read out 96-bit Unique ID. + * + */ +static __INLINE uint32_t FMC_ReadUID(uint8_t u8index) +{ + FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */ + FMC->ISPADR = (u8index << 2); /* Set UID Address. It must be word alignment. */ + FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG); /* Waiting for ISP Done */ + + return FMC->ISPDAT; +} + + +/** + * @brief Read company ID + * + * @param None + * + * @return The company ID (32-bit) + * + * @details The company ID of Nuvoton is fixed to be 0xDA + * + */ +static __INLINE uint32_t FMC_ReadCID(void) +{ + FMC->ISPCMD = FMC_ISPCMD_READ_CID; /* Set ISP Command Code */ + FMC->ISPADR = 0x0; /* Must keep 0x0 when read CID */ + FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk) ; /* Waiting for ISP Done */ + + return FMC->ISPDAT; +} + + +/** + * @brief Read device ID + * + * @param None + * + * @return The device ID (32-bit) + * + * @details This function is used to read device ID. + * + */ +static __INLINE uint32_t FMC_ReadDID(void) +{ + FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */ + FMC->ISPADR = 0; /* Must keep 0x0 when read DID */ + FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk); /* Waiting for ISP Done */ + + return FMC->ISPDAT; +} + + + +/** + * @brief Read product ID + * + * @param None + * + * @return The product ID (32-bit) + * + * @details This function is used to read product ID. + * + */ +static __INLINE uint32_t FMC_ReadPID(void) +{ + FMC->ISPCMD = FMC_ISPCMD_READ_DID; /* Set ISP Command Code */ + FMC->ISPADR = 0x04; /* Must keep 0x4 when read PID */ + FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk); /* Waiting for ISP Done */ + + return FMC->ISPDAT; +} + +/** + * @brief To read UCID + * + * @param[in] u32Index Index of the UCID to read. u32Index must be 0, 1, 2, or 3. + * + * @return The UCID of specified index + * + * @details This function is used to read unique chip ID (UCID). + * + */ +static __INLINE uint32_t FMC_ReadUCID(uint32_t u32Index) +{ + FMC->ISPCMD = FMC_ISPCMD_READ_UID; /* Set ISP Command Code */ + FMC->ISPADR = (0x04 * u32Index) + 0x10; /* The UCID is at offset 0x10 with word alignment. */ + FMC->ISPTRG = FMC_ISPTRG_ISPGO_Msk; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG & FMC_ISPTRG_ISPGO_Msk); /* Waiting for ISP Done */ + + return FMC->ISPDAT; +} + + + +/** + * @brief Set vector mapping address + * + * @param[in] u32PageAddr The page address to remap to address 0x0. The address must be page alignment. + * + * @return None + * + * @details This function is used to set VECMAP to map specified page to vector page (0x0). + * + * @note + * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b) + * + */ +static __INLINE void FMC_SetVectorPageAddr(uint32_t u32PageAddr) +{ + FMC->ISPCMD = FMC_ISPCMD_VECMAP; /* Set ISP Command Code */ + FMC->ISPADR = u32PageAddr; /* The address of specified page which will be map to address 0x0. It must be page alignment. */ + FMC->ISPTRG = 0x1; /* Trigger to start ISP procedure */ + __ISB(); /* To make sure ISP/CPU be Synchronized */ + while(FMC->ISPTRG); /* Waiting for ISP Done */ +} + + +/** + * @brief Get current vector mapping address. + * + * @param None + * + * @return The current vector mapping address. + * + * @details To get VECMAP value which is the page address for remapping to vector page (0x0). + * + * @note + * VECMAP only valid when new IAP function is enabled. (CBS = 10'b or 00'b) + * + */ +static __INLINE uint32_t FMC_GetVECMAP(void) +{ + return (FMC->ISPSTA & FMC_ISPSTA_VECMAP_Msk); +} + +extern void FMC_Open(void); +extern void FMC_Close(void); +extern void FMC_EnableAPUpdate(void); +extern void FMC_DisableAPUpdate(void); +extern void FMC_EnableConfigUpdate(void); +extern void FMC_DisableConfigUpdate(void); +extern void FMC_EnableLDUpdate(void); +extern void FMC_DisableLDUpdate(void); +extern int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count); +extern int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count); +extern void FMC_SetBootSource(int32_t i32BootSrc); +extern int32_t FMC_GetBootSource(void); +extern uint32_t FMC_ReadDataFlashBaseAddr(void); + +/*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group FMC_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + + +#endif + diff --git a/NUC123/StdDriver/inc/gpio.h b/NUC123/StdDriver/inc/gpio.h new file mode 100644 index 0000000..d2ad757 --- /dev/null +++ b/NUC123/StdDriver/inc/gpio.h @@ -0,0 +1,458 @@ +/**************************************************************************//** + * @file GPIO.h + * @version V3.00 + * $Revision: 13 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series General Purpose I/O Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __GPIO_H__ +#define __GPIO_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup GPIO_Driver GPIO Driver + @{ +*/ + +/** @addtogroup GPIO_EXPORTED_CONSTANTS GPIO Exported Constants + @{ +*/ +#define GPIO_PIN_MAX 16 /*!< Specify Maximum Pins of Each GPIO Port */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* PMD Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define GPIO_PMD_INPUT 0x0UL /*!< Input Mode */ +#define GPIO_PMD_OUTPUT 0x1UL /*!< Output Mode */ +#define GPIO_PMD_OPEN_DRAIN 0x2UL /*!< Open-Drain Mode */ +#define GPIO_PMD_QUASI 0x3UL /*!< Quasi-bidirectional Mode */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* GPIO Interrupt Type Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define GPIO_INT_RISING 0x00010000UL /*!< Interrupt enable by Input Rising Edge */ +#define GPIO_INT_FALLING 0x00000001UL /*!< Interrupt enable by Input Falling Edge */ +#define GPIO_INT_BOTH_EDGE 0x00010001UL /*!< Interrupt enable by both Rising Edge and Falling Edge */ +#define GPIO_INT_HIGH 0x01010000UL /*!< Interrupt enable by Level-High */ +#define GPIO_INT_LOW 0x01000001UL /*!< Interrupt enable by Level-Level */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* IMD Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define GPIO_IMD_EDGE 0UL /*!< IMD Setting for Edge Trigger Mode */ +#define GPIO_IMD_LEVEL 1UL /*!< IMD Setting for Edge Level Mode */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* DBNCECON Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define GPIO_INT_CLK_ON 0x00000020UL /*!< DBNCECON setting for all IO pins edge detection circuit is always active after reset */ +#define GPIO_INT_CLK_OFF 0x00000000UL /*!< DBNCECON setting for edge detection circuit is active only if IO pin corresponding GPIOx_IEN bit is set to 1 */ + +#define GPIO_DBCLKSRC_LIRC 0x00000010UL /*!< DBNCECON setting for de-bounce counter clock source is the internal 10 kHz */ +#define GPIO_DBCLKSRC_HCLK 0x00000000UL /*!< DBNCECON setting for de-bounce counter clock source is the HCLK */ + +#define GPIO_DBCLKSEL_1 0x00000000UL /*!< DBNCECON setting for sampling cycle = 1 clocks */ +#define GPIO_DBCLKSEL_2 0x00000001UL /*!< DBNCECON setting for sampling cycle = 2 clocks */ +#define GPIO_DBCLKSEL_4 0x00000002UL /*!< DBNCECON setting for sampling cycle = 4 clocks */ +#define GPIO_DBCLKSEL_8 0x00000003UL /*!< DBNCECON setting for sampling cycle = 8 clocks */ +#define GPIO_DBCLKSEL_16 0x00000004UL /*!< DBNCECON setting for sampling cycle = 16 clocks */ +#define GPIO_DBCLKSEL_32 0x00000005UL /*!< DBNCECON setting for sampling cycle = 32 clocks */ +#define GPIO_DBCLKSEL_64 0x00000006UL /*!< DBNCECON setting for sampling cycle = 64 clocks */ +#define GPIO_DBCLKSEL_128 0x00000007UL /*!< DBNCECON setting for sampling cycle = 128 clocks */ +#define GPIO_DBCLKSEL_256 0x00000008UL /*!< DBNCECON setting for sampling cycle = 256 clocks */ +#define GPIO_DBCLKSEL_512 0x00000009UL /*!< DBNCECON setting for sampling cycle = 512 clocks */ +#define GPIO_DBCLKSEL_1024 0x0000000AUL /*!< DBNCECON setting for sampling cycle = 1024 clocks */ +#define GPIO_DBCLKSEL_2048 0x0000000BUL /*!< DBNCECON setting for sampling cycle = 2048 clocks */ +#define GPIO_DBCLKSEL_4096 0x0000000CUL /*!< DBNCECON setting for sampling cycle = 4096 clocks */ +#define GPIO_DBCLKSEL_8192 0x0000000DUL /*!< DBNCECON setting for sampling cycle = 8192 clocks */ +#define GPIO_DBCLKSEL_16384 0x0000000EUL /*!< DBNCECON setting for sampling cycle = 16384 clocks */ +#define GPIO_DBCLKSEL_32768 0x0000000FUL /*!< DBNCECON setting for sampling cycle = 32768 clocks */ + + +/* Define GPIO Pin Data Input/Output. It could be used to control each I/O pin by pin address mapping. + Example 1: + + PA10 = 1; + + It is used to set GPIO PA.10 to high; + + Example 2: + + if (PA10) + PA10 = 0; + + If GPIO PA.10 pin status is high, then set GPIO PA.10 data output to low. + */ +#define GPIO_PIN_DATA(port, pin) (*((volatile uint32_t *)((GPIO_PIN_DATA_BASE+(0x40*(port))) + ((pin)<<2)))) +#define PA10 GPIO_PIN_DATA(0, 10) /*!< Specify PA.10 Pin Data Input/Output */ +#define PA11 GPIO_PIN_DATA(0, 11) /*!< Specify PA.11 Pin Data Input/Output */ +#define PA12 GPIO_PIN_DATA(0, 12) /*!< Specify PA.12 Pin Data Input/Output */ +#define PA13 GPIO_PIN_DATA(0, 13) /*!< Specify PA.13 Pin Data Input/Output */ +#define PA14 GPIO_PIN_DATA(0, 14) /*!< Specify PA.14 Pin Data Input/Output */ +#define PA15 GPIO_PIN_DATA(0, 15) /*!< Specify PA.15 Pin Data Input/Output */ +#define PB0 GPIO_PIN_DATA(1, 0 ) /*!< Specify PB.0 Pin Data Input/Output */ +#define PB1 GPIO_PIN_DATA(1, 1 ) /*!< Specify PB.1 Pin Data Input/Output */ +#define PB2 GPIO_PIN_DATA(1, 2 ) /*!< Specify PB.2 Pin Data Input/Output */ +#define PB3 GPIO_PIN_DATA(1, 3 ) /*!< Specify PB.3 Pin Data Input/Output */ +#define PB4 GPIO_PIN_DATA(1, 4 ) /*!< Specify PB.4 Pin Data Input/Output */ +#define PB5 GPIO_PIN_DATA(1, 5 ) /*!< Specify PB.5 Pin Data Input/Output */ +#define PB6 GPIO_PIN_DATA(1, 6 ) /*!< Specify PB.6 Pin Data Input/Output */ +#define PB7 GPIO_PIN_DATA(1, 7 ) /*!< Specify PB.7 Pin Data Input/Output */ +#define PB8 GPIO_PIN_DATA(1, 8 ) /*!< Specify PB.8 Pin Data Input/Output */ +#define PB9 GPIO_PIN_DATA(1, 9 ) /*!< Specify PB.9 Pin Data Input/Output */ +#define PB10 GPIO_PIN_DATA(1, 10) /*!< Specify PB.10 Pin Data Input/Output */ +#define PB12 GPIO_PIN_DATA(1, 12) /*!< Specify PB.12 Pin Data Input/Output */ +#define PB13 GPIO_PIN_DATA(1, 13) /*!< Specify PB.13 Pin Data Input/Output */ +#define PB14 GPIO_PIN_DATA(1, 14) /*!< Specify PB.14 Pin Data Input/Output */ +#define PB15 GPIO_PIN_DATA(1, 15) /*!< Specify PB.15 Pin Data Input/Output */ +#define PC0 GPIO_PIN_DATA(2, 0 ) /*!< Specify PC.0 Pin Data Input/Output */ +#define PC1 GPIO_PIN_DATA(2, 1 ) /*!< Specify PC.1 Pin Data Input/Output */ +#define PC2 GPIO_PIN_DATA(2, 2 ) /*!< Specify PC.2 Pin Data Input/Output */ +#define PC3 GPIO_PIN_DATA(2, 3 ) /*!< Specify PC.3 Pin Data Input/Output */ +#define PC4 GPIO_PIN_DATA(2, 4 ) /*!< Specify PC.4 Pin Data Input/Output */ +#define PC5 GPIO_PIN_DATA(2, 5 ) /*!< Specify PC.5 Pin Data Input/Output */ +#define PC8 GPIO_PIN_DATA(2, 8 ) /*!< Specify PC.8 Pin Data Input/Output */ +#define PC9 GPIO_PIN_DATA(2, 9 ) /*!< Specify PC.9 Pin Data Input/Output */ +#define PC10 GPIO_PIN_DATA(2, 10) /*!< Specify PC.10 Pin Data Input/Output */ +#define PC11 GPIO_PIN_DATA(2, 11) /*!< Specify PC.11 Pin Data Input/Output */ +#define PC12 GPIO_PIN_DATA(2, 12) /*!< Specify PC.12 Pin Data Input/Output */ +#define PC13 GPIO_PIN_DATA(2, 13) /*!< Specify PC.13 Pin Data Input/Output */ +#define PD0 GPIO_PIN_DATA(3, 0 ) /*!< Specify PD.0 Pin Data Input/Output */ +#define PD1 GPIO_PIN_DATA(3, 1 ) /*!< Specify PD.1 Pin Data Input/Output */ +#define PD2 GPIO_PIN_DATA(3, 2 ) /*!< Specify PD.2 Pin Data Input/Output */ +#define PD3 GPIO_PIN_DATA(3, 3 ) /*!< Specify PD.3 Pin Data Input/Output */ +#define PD4 GPIO_PIN_DATA(3, 4 ) /*!< Specify PD.4 Pin Data Input/Output */ +#define PD5 GPIO_PIN_DATA(3, 5 ) /*!< Specify PD.5 Pin Data Input/Output */ +#define PD8 GPIO_PIN_DATA(3, 8 ) /*!< Specify PD.8 Pin Data Input/Output */ +#define PD9 GPIO_PIN_DATA(3, 9 ) /*!< Specify PD.9 Pin Data Input/Output */ +#define PD10 GPIO_PIN_DATA(3, 10) /*!< Specify PD.10 Pin Data Input/Output */ +#define PD11 GPIO_PIN_DATA(3, 11) /*!< Specify PD.11 Pin Data Input/Output */ +#define PF0 GPIO_PIN_DATA(5, 0 ) /*!< Specify PF.0 Pin Data Input/Output */ +#define PF1 GPIO_PIN_DATA(5, 1 ) /*!< Specify PF.1 Pin Data Input/Output */ +#define PF2 GPIO_PIN_DATA(5, 2 ) /*!< Specify PF.2 Pin Data Input/Output */ +#define PF3 GPIO_PIN_DATA(5, 3 ) /*!< Specify PF.3 Pin Data Input/Output */ + + +/*@}*/ /* end of group GPIO_EXPORTED_CONSTANTS */ + + +/** @addtogroup GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions + @{ +*/ + +/** + * @brief Clear GPIO Pin Interrupt Flag + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Clear the interrupt status of specified GPIO pin. + */ +#define GPIO_CLR_INT_FLAG(port, u32PinMask) ((port)->ISRC = (u32PinMask)) + +/** + * @brief Disable Pin De-bounce Function + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Disable the interrupt de-bounce function of specified GPIO pin. + */ +#define GPIO_DISABLE_DEBOUNCE(port, u32PinMask) ((port)->DBEN &= ~(u32PinMask)) + +/** + * @brief Enable Pin De-bounce Function + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Enable the interrupt de-bounce function of specified GPIO pin. + */ +#define GPIO_ENABLE_DEBOUNCE(port, u32PinMask) ((port)->DBEN |= (u32PinMask)) + +/** + * @brief Disable I/O Digital Input Path + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Disable I/O digital input path of specified GPIO pin. + */ +#define GPIO_DISABLE_DIGITAL_PATH(port, u32PinMask) ((port)->OFFD |= ((u32PinMask)<<16)) + +/** + * @brief Enable I/O Digital Input Path + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port \n. + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Enable I/O digital input path of specified GPIO pin. + */ +#define GPIO_ENABLE_DIGITAL_PATH(port, u32PinMask) ((port)->OFFD &= ~((u32PinMask)<<16)) + +/** + * @brief Disable I/O DOUT mask + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Disable I/O DOUT mask of specified GPIO pin. + */ +#define GPIO_DISABLE_DOUT_MASK(port, u32PinMask) ((port)->DMASK &= ~(u32PinMask)) + +/** + * @brief Enable I/O DOUT mask + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @return None + * + * @details Enable I/O DOUT mask of specified GPIO pin. + */ +#define GPIO_ENABLE_DOUT_MASK(port, u32PinMask) ((port)->DMASK |= (u32PinMask)) + +/** + * @brief Get GPIO Pin Interrupt Flag + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * + * @retval 0 No interrupt at specified GPIO pin + * @retval 1 The specified GPIO pin generate an interrupt + * + * @details Get the interrupt status of specified GPIO pin. + */ +#define GPIO_GET_INT_FLAG(port, u32PinMask) ((port)->ISRC & (u32PinMask)) + +/** + * @brief Set De-bounce Sampling Cycle Time + * + * @param[in] u32ClkSrc The de-bounce counter clock source. It could be : + * - \ref GPIO_DBCLKSRC_HCLK + * - \ref GPIO_DBCLKSRC_LIRC + * @param[in] u32ClkSel The de-bounce sampling cycle selection. It could be : + * - \ref GPIO_DBCLKSEL_1 + * - \ref GPIO_DBCLKSEL_2 + * - \ref GPIO_DBCLKSEL_4 + * - \ref GPIO_DBCLKSEL_8 + * - \ref GPIO_DBCLKSEL_16 + * - \ref GPIO_DBCLKSEL_32 + * - \ref GPIO_DBCLKSEL_64 + * - \ref GPIO_DBCLKSEL_128 + * - \ref GPIO_DBCLKSEL_256 + * - \ref GPIO_DBCLKSEL_512 + * - \ref GPIO_DBCLKSEL_1024 + * - \ref GPIO_DBCLKSEL_2048 + * - \ref GPIO_DBCLKSEL_4096 + * - \ref GPIO_DBCLKSEL_8192 + * - \ref GPIO_DBCLKSEL_16384 + * - \ref GPIO_DBCLKSEL_32768 + * + * @return None + * + * @details Set the interrupt de-bounce sampling cycle time based on the debounce counter clock source. \n + * Example: GPIO_SET_DEBOUNCE_TIME(GPIO_DBNCECON_DBCLKSRC_LIRC, GPIO_DBCLKSEL_4). \n + * It's meaning the De-debounce counter clock source is internal 10 KHz and sampling cycle selection is 4. \n + * Then the target de-bounce sampling cycle time is (4)*(1/(10*1000)) s = 4*0.0001 s = 400 us, + * and system will sampling interrupt input once per 400 us. + */ +#define GPIO_SET_DEBOUNCE_TIME(u32ClkSrc, u32ClkSel) (GPIO->DBNCECON = (GPIO_DBNCECON_ICLK_ON_Msk | (u32ClkSrc) | (u32ClkSel))) + +/** + * @brief Get GPIO Port IN Data + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * + * @return The specified port data + * + * @details Get the PIN register of specified GPIO port. + */ +#define GPIO_GET_IN_DATA(port) ((port)->PIN) + +/** + * @brief Set GPIO Port OUT Data + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Data GPIO port data. + * + * @return None + * + * @details Set the Data into specified GPIO port. + */ +#define GPIO_SET_OUT_DATA(port, u32Data) ((port)->DOUT = (u32Data)) + +/** + * @brief Toggle Specified GPIO pin + * + * @param[in] u32Pin Pxy + * + * @return None + * + * @details Toggle the specified GPIO pint. + */ +#define GPIO_TOGGLE(u32Pin) ((u32Pin) ^= 1) + +/** + * @brief Enable External GPIO Interrupt 0 + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Pin The pin of specified GPIO port. \n + * It could be 10 ~ 15 for PA GPIO port. \n + * It could be 0 ~ 10 and 12 ~ 15 for PB GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 13 for PC GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 11 for PD GPIO port. \n + * It could be 0 ~ 3 for PF GPIO port. + * @param[in] u32IntAttribs The interrupt attribute of specified GPIO pin. It could be \n + * - \ref GPIO_INT_RISING + * - \ref GPIO_INT_FALLING + * - \ref GPIO_INT_BOTH_EDGE + * - \ref GPIO_INT_HIGH + * - \ref GPIO_INT_LOW + * + * @return None + * + * @details This function is used to enable specified GPIO pin interrupt. + */ +#define GPIO_EnableEINT0 GPIO_EnableInt + + +/** + * @brief Disable External GPIO Interrupt 0 + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Pin The pin of specified GPIO port. \n + * It could be 10 ~ 15 for PA GPIO port. \n + * It could be 0 ~ 10 and 12 ~ 15 for PB GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 13 for PC GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 11 for PD GPIO port. \n + * It could be 0 ~ 3 for PF GPIO port. + * + * @return None + * + * @details This function is used to disable specified GPIO pin interrupt. + */ +#define GPIO_DisableEINT0 GPIO_DisableInt + + +/** + * @brief Enable External GPIO Interrupt 1 + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Pin The pin of specified GPIO port. \n + * It could be 10 ~ 15 for PA GPIO port. \n + * It could be 0 ~ 10 and 12 ~ 15 for PB GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 13 for PC GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 11 for PD GPIO port. \n + * It could be 0 ~ 3 for PF GPIO port. + * @param[in] u32IntAttribs The interrupt attribute of specified GPIO pin. It could be \n + * GPIO_INT_RISING, GPIO_INT_FALLING, GPIO_INT_BOTH_EDGE, GPIO_INT_HIGH, GPIO_INT_LOW. + * + * @return None + * + * @details This function is used to enable specified GPIO pin interrupt. + */ +#define GPIO_EnableEINT1 GPIO_EnableInt + + +/** + * @brief Disable External GPIO Interrupt 1 + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Pin The pin of specified GPIO port. \n + * It could be 10 ~ 15 for PA GPIO port. \n + * It could be 0 ~ 10 and 12 ~ 15 for PB GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 13 for PC GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 11 for PD GPIO port. \n + * It could be 0 ~ 3 for PF GPIO port. + * + * @return None + * + * @details This function is used to disable specified GPIO pin interrupt. + */ +#define GPIO_DisableEINT1 GPIO_DisableInt + + +void GPIO_SetMode(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode); +void GPIO_EnableInt(GPIO_T *port, uint32_t u32Pin, uint32_t u32IntAttribs); +void GPIO_DisableInt(GPIO_T *port, uint32_t u32Pin); + + +/*@}*/ /* end of group GPIO_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group GPIO_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__GPIO_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/i2c.h b/NUC123/StdDriver/inc/i2c.h new file mode 100644 index 0000000..8ee6975 --- /dev/null +++ b/NUC123/StdDriver/inc/i2c.h @@ -0,0 +1,212 @@ +/**************************************************************************//** + * @file I2C.h + * @version V3.0 + * $Revision: 10 $ + * $Date: 16/06/22 11:46a $ + * @brief NUC123 Series I2C Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __I2C_H__ +#define __I2C_H__ + +#include "NUC123.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup I2C_Driver I2C Driver + @{ +*/ + +/** @addtogroup I2C_EXPORTED_CONSTANTS I2C Exported Constants + @{ +*/ + +/*---------------------------------------------------------------------------------------------------------*/ +/* I2CON constant definitions. */ +/*---------------------------------------------------------------------------------------------------------*/ +#define I2C_I2CON_STA_STO_SI 0x38UL /*!< I2CON setting for I2C control bits. It would set STA, STO and SI bits */ +#define I2C_I2CON_STA_STO_SI_AA 0x3CUL /*!< I2CON setting for I2C control bits. It would set STA, STO, SI and AA bits */ +#define I2C_I2CON_STA_SI 0x28UL /*!< I2CON setting for I2C control bits. It would set STA and SI bits */ +#define I2C_I2CON_STA_SI_AA 0x2CUL /*!< I2CON setting for I2C control bits. It would set STA, SI and AA bits */ +#define I2C_I2CON_STO_SI 0x18UL /*!< I2CON setting for I2C control bits. It would set STO and SI bits */ +#define I2C_I2CON_STO_SI_AA 0x1CUL /*!< I2CON setting for I2C control bits. It would set STO, SI and AA bits */ +#define I2C_I2CON_SI 0x08UL /*!< I2CON setting for I2C control bits. It would set SI bit */ +#define I2C_I2CON_SI_AA 0x0CUL /*!< I2CON setting for I2C control bits. It would set SI and AA bits */ +#define I2C_I2CON_STA 0x20UL /*!< I2CON setting for I2C control bits. It would set STA bit */ +#define I2C_I2CON_STO 0x10UL /*!< I2CON setting for I2C control bits. It would set STO bit */ +#define I2C_I2CON_AA 0x04UL /*!< I2CON setting for I2C control bits. It would set AA bit */ + +#define I2C_GCMODE_ENABLE 1 /*!< Enable I2C GC Mode */ +#define I2C_GCMODE_DISABLE 0 /*!< Disable I2C GC Mode */ + +/*@}*/ /* end of group I2C_EXPORTED_CONSTANTS */ + +/** @addtogroup I2C_EXPORTED_FUNCTIONS I2C Exported Functions + @{ +*/ +/** + * @brief The macro is used to set I2C bus condition at One Time + * + * @param[in] i2c Specify I2C port + * @param[in] u8Ctrl A byte writes to I2C control register + * + * @return None + * + * @details Set I2CON register to control I2C bus conditions of START, STOP, SI, ACK. + */ +#define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ((i2c)->I2CON = ((i2c)->I2CON & ~0x3c) | (u8Ctrl)) + +/** + * @brief The macro is used to set START condition of I2C Bus + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details Set the I2C bus START condition in I2CON register. + */ +#define I2C_START(i2c) ((i2c)->I2CON = ((i2c)->I2CON | I2C_I2CON_SI_Msk) | I2C_I2CON_STA_Msk) + +/** + * @brief The macro is used to set STOP condition of I2C Bus + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details Set the I2C bus STOP condition in I2CON register. + */ +#define I2C_STOP(i2c) ((i2c)->I2CON = ((i2c)->I2CON | I2C_I2CON_SI_Msk) | I2C_I2CON_STO_Msk) + +/** + * @brief The macro is used to wait I2C bus status get ready + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details When a new status is presented of I2C bus, the SI flag will be set in I2CON register. + */ +#define I2C_WAIT_READY(i2c) while(!((i2c)->I2CON & I2C_I2CON_SI_Msk)) + +/** + * @brief The macro is used to Read I2C Bus Data Register + * + * @param[in] i2c Specify I2C port + * + * @return A byte of I2C data register + * + * @details I2C controller read data from bus and save it in I2CDAT register. + */ +#define I2C_GET_DATA(i2c) ((i2c)->I2CDAT) + +/** + * @brief Write a Data to I2C Data Register + * + * @param[in] i2c Specify I2C port + * @param[in] u8Data A byte that writes to data register + * + * @return None + * + * @details When write a data to I2CDAT register, the I2C controller will shift it to I2C bus. + */ +#define I2C_SET_DATA(i2c, u8Data) ((i2c)->I2CDAT = (u8Data)) + +/** + * @brief Get I2C Bus status code + * + * @param[in] i2c Specify I2C port + * + * @return I2C status code + * + * @details To get this status code to monitor I2C bus event. + */ +#define I2C_GET_STATUS(i2c) ((i2c)->I2CSTATUS) + +/** + * @brief Get Time-out flag from I2C Bus + * + * @param[in] i2c Specify I2C port + * + * @retval 0 I2C Bus time-out is not happened + * @retval 1 I2C Bus time-out is happened + * + * @details When I2C bus occurs time-out event, the time-out flag will be set. + */ +#define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->I2CTOC & I2C_I2CTOC_TIF_Msk) == I2C_I2CTOC_TIF_Msk ? 1:0 ) + +/** + * @brief To get wake-up flag from I2C Bus + * + * @param[in] i2c Specify I2C port + * + * @retval 0 Chip is not woken-up from power-down mode + * @retval 1 Chip is woken-up from power-down mode + * + * @details I2C bus occurs wake-up event, wake-up flag will be set. + */ +#define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->I2CWKUPSTS & I2C_I2CWKUPSTS_WKUPIF_Msk) == I2C_I2CWKUPSTS_WKUPIF_Msk ? 1:0 ) + +/** + * @brief To clear wake-up flag + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details If wake-up flag is set, use this macro to clear it. + */ +#define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->I2CWKUPSTS |= I2C_I2CWKUPSTS_WKUPIF_Msk) + +void I2C_ClearTimeoutFlag(I2C_T *i2c); +void I2C_Close(I2C_T *i2c); +void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack); +void I2C_DisableInt(I2C_T *i2c); +void I2C_EnableInt(I2C_T *i2c); +uint32_t I2C_GetBusClockFreq(I2C_T *i2c); +uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock); +uint32_t I2C_GetIntFlag(I2C_T *i2c); +uint32_t I2C_GetStatus(I2C_T *i2c); +uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock); +uint8_t I2C_GetData(I2C_T *i2c); +void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode); +void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask); +void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout); +void I2C_DisableTimeout(I2C_T *i2c); +void I2C_EnableWakeup(I2C_T *i2c); +void I2C_DisableWakeup(I2C_T *i2c); +void I2C_SetData(I2C_T *i2c, uint8_t u8Data); +uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, const uint8_t data); +uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, const uint8_t *data, uint32_t u32wLen); +uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, const uint8_t data); +uint32_t I2C_WriteMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, const uint8_t *data, uint32_t u32wLen); +uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, const uint8_t data); +uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, const uint8_t *data, uint32_t u32wLen); +uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr); +uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t *rdata, uint32_t u32rLen); +uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr); +uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *rdata, uint32_t u32rLen); +uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr); +uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *rdata, uint32_t u32rLen); +/*@}*/ /* end of group I2C_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group I2C_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif +#endif //__I2C_H__ diff --git a/NUC123/StdDriver/inc/i2s.h b/NUC123/StdDriver/inc/i2s.h new file mode 100644 index 0000000..353cf69 --- /dev/null +++ b/NUC123/StdDriver/inc/i2s.h @@ -0,0 +1,324 @@ +/**************************************************************************//** + * @file i2s.h + * @version V3.0 + * $Revision: 6 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series I2S driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + ******************************************************************************/ +#ifndef __I2S_H__ +#define __I2S_H__ + +#include "NUC123.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup I2S_Driver I2S Driver + @{ +*/ + +/** @addtogroup I2S_EXPORTED_CONSTANTS I2S Exported Constants + @{ +*/ +#define I2S_DATABIT_8 (0 << I2S_CON_WORDWIDTH_Pos) /*!< I2S data width is 8-bit */ +#define I2S_DATABIT_16 (1 << I2S_CON_WORDWIDTH_Pos) /*!< I2S data width is 16-bit */ +#define I2S_DATABIT_24 (2 << I2S_CON_WORDWIDTH_Pos) /*!< I2S data width is 24-bit */ +#define I2S_DATABIT_32 (3 << I2S_CON_WORDWIDTH_Pos) /*!< I2S data width is 32-bit */ + +/* Audio Format */ +#define I2S_MONO I2S_CON_MONO_Msk /*!< Mono channel */ +#define I2S_STEREO 0 /*!< Stereo channel */ + +/* I2S Data Format */ +#define I2S_FORMAT_MSB I2S_CON_FORMAT_Msk /*!< MSB data format */ +#define I2S_FORMAT_I2S 0 /*!< I2S data format */ +#define I2S_FORMAT_PCM_A I2S_CON_PCM_Msk /*!< PCM mode A data format */ +#define I2S_FORMAT_PCM_B (I2S_CON_PCM_Msk|I2S_CON_FORMAT_Msk) /*!< PCM mode B data format */ + +/* I2S Operation mode */ +#define I2S_MODE_SLAVE I2S_CON_SLAVE_Msk /*!< As slave mode */ +#define I2S_MODE_MASTER 0 /*!< As master mode */ + +/* I2S FIFO Threshold */ +#define I2S_FIFO_TX_LEVEL_WORD_0 0 /*!< TX threshold is 0 word */ +#define I2S_FIFO_TX_LEVEL_WORD_1 (1 << I2S_CON_TXTH_Pos) /*!< TX threshold is 1 word */ +#define I2S_FIFO_TX_LEVEL_WORD_2 (2 << I2S_CON_TXTH_Pos) /*!< TX threshold is 2 words */ +#define I2S_FIFO_TX_LEVEL_WORD_3 (3 << I2S_CON_TXTH_Pos) /*!< TX threshold is 3 words */ +#define I2S_FIFO_TX_LEVEL_WORD_4 (4 << I2S_CON_TXTH_Pos) /*!< TX threshold is 4 words */ +#define I2S_FIFO_TX_LEVEL_WORD_5 (5 << I2S_CON_TXTH_Pos) /*!< TX threshold is 5 words */ +#define I2S_FIFO_TX_LEVEL_WORD_6 (6 << I2S_CON_TXTH_Pos) /*!< TX threshold is 6 words */ +#define I2S_FIFO_TX_LEVEL_WORD_7 (7 << I2S_CON_TXTH_Pos) /*!< TX threshold is 7 words */ + +#define I2S_FIFO_RX_LEVEL_WORD_1 0 /*!< RX threshold is 1 word */ +#define I2S_FIFO_RX_LEVEL_WORD_2 (1 << I2S_CON_RXTH_Pos) /*!< RX threshold is 2 words */ +#define I2S_FIFO_RX_LEVEL_WORD_3 (2 << I2S_CON_RXTH_Pos) /*!< RX threshold is 3 words */ +#define I2S_FIFO_RX_LEVEL_WORD_4 (3 << I2S_CON_RXTH_Pos) /*!< RX threshold is 4 words */ +#define I2S_FIFO_RX_LEVEL_WORD_5 (4 << I2S_CON_RXTH_Pos) /*!< RX threshold is 5 words */ +#define I2S_FIFO_RX_LEVEL_WORD_6 (5 << I2S_CON_RXTH_Pos) /*!< RX threshold is 6 words */ +#define I2S_FIFO_RX_LEVEL_WORD_7 (6 << I2S_CON_RXTH_Pos) /*!< RX threshold is 7 words */ +#define I2S_FIFO_RX_LEVEL_WORD_8 (7 << I2S_CON_RXTH_Pos) /*!< RX threshold is 8 words */ + +/* I2S Record Channel */ +#define I2S_MONO_RIGHT 0 /*!< Record mono right channel */ +#define I2S_MONO_LEFT I2S_CON_RXLCH_Msk /*!< Record mono left channel */ + +/* I2S Channel */ +#define I2S_RIGHT 0 /*!< Select right channel */ +#define I2S_LEFT 1 /*!< Select left channel */ + +/*@}*/ /* end of group I2S_EXPORTED_CONSTANTS */ + +/** @addtogroup I2S_EXPORTED_FUNCTIONS I2S Exported Functions + @{ +*/ +/*---------------------------------------------------------------------------------------------------------*/ +/* inline functions */ +/*---------------------------------------------------------------------------------------------------------*/ +/** + * @brief Enable zero cross detection function. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32ChMask The mask for left or right channel. Valid values are: + * - \ref I2S_RIGHT + * - \ref I2S_LEFT + * @return None + * @details This function will set RCHZCEN or LCHZCEN bit of I2SCON register to enable zero cross detection function. + */ +static __INLINE void I2S_ENABLE_TX_ZCD(I2S_T *i2s, uint32_t u32ChMask) +{ + if(u32ChMask == I2S_RIGHT) + i2s->CON |= I2S_CON_RCHZCEN_Msk; + else + i2s->CON |= I2S_CON_LCHZCEN_Msk; +} + +/** + * @brief Disable zero cross detection function. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32ChMask The mask for left or right channel. Valid values are: + * - \ref I2S_RIGHT + * - \ref I2S_LEFT + * @return None + * @details This function will clear RCHZCEN or LCHZCEN bit of I2SCON register to disable zero cross detection function. + */ +static __INLINE void I2S_DISABLE_TX_ZCD(I2S_T *i2s, uint32_t u32ChMask) +{ + if(u32ChMask == I2S_RIGHT) + i2s->CON &= ~I2S_CON_RCHZCEN_Msk; + else + i2s->CON &= ~I2S_CON_LCHZCEN_Msk; +} + +/** + * @brief Enable I2S TX DMA function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will set TXDMA bit of I2SCON register to transmit data with PDMA. + */ +#define I2S_ENABLE_TXDMA(i2s) ( (i2s)->CON |= I2S_CON_TXDMA_Msk ) + +/** + * @brief Disable I2S TX DMA function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear TXDMA bit of I2SCON register to disable TX DMA function. + */ +#define I2S_DISABLE_TXDMA(i2s) ( (i2s)->CON &= ~I2S_CON_TXDMA_Msk ) + +/** + * @brief Enable I2S RX DMA function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will set RXDMA bit of I2SCON register to receive data with PDMA. + */ +#define I2S_ENABLE_RXDMA(i2s) ( (i2s)->CON |= I2S_CON_RXDMA_Msk ) + +/** + * @brief Disable I2S RX DMA function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear RXDMA bit of I2SCON register to disable RX DMA function. + */ +#define I2S_DISABLE_RXDMA(i2s) ( (i2s)->CON &= ~I2S_CON_RXDMA_Msk ) + +/** + * @brief Enable I2S TX function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will set TXEN bit of I2SCON register to enable I2S TX function. + */ +#define I2S_ENABLE_TX(i2s) ( (i2s)->CON |= I2S_CON_TXEN_Msk ) + +/** + * @brief Disable I2S TX function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear TXEN bit of I2SCON register to disable I2S TX function. + */ +#define I2S_DISABLE_TX(i2s) ( (i2s)->CON &= ~I2S_CON_TXEN_Msk ) + +/** + * @brief Enable I2S RX function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will set RXEN bit of I2SCON register to enable I2S RX function. + */ +#define I2S_ENABLE_RX(i2s) ( (i2s)->CON |= I2S_CON_RXEN_Msk ) + +/** + * @brief Disable I2S RX function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear RXEN bit of I2SCON register to disable I2S RX function. + */ +#define I2S_DISABLE_RX(i2s) ( (i2s)->CON &= ~I2S_CON_RXEN_Msk ) + +/** + * @brief Enable TX Mute function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will set MUTE bit of I2SCON register to enable I2S TX mute function. + */ +#define I2S_ENABLE_TX_MUTE(i2s) ( (i2s)->CON |= I2S_CON_MUTE_Msk ) + +/** + * @brief Disable TX Mute function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear MUTE bit of I2SCON register to disable I2S TX mute function. + */ +#define I2S_DISABLE_TX_MUTE(i2s) ( (i2s)->CON &= ~I2S_CON_MUTE_Msk ) + +/** + * @brief Clear TX FIFO. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear TX FIFO. The internal TX FIFO pointer will be reset to FIFO start point. + */ +#define I2S_CLR_TX_FIFO(i2s) ( (i2s)->CON |= I2S_CON_CLR_TXFIFO_Msk ) + +/** + * @brief Clear RX FIFO. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details This macro will clear RX FIFO. The internal RX FIFO pointer will be reset to FIFO start point. + */ +#define I2S_CLR_RX_FIFO(i2s) ( (i2s)->CON |= I2S_CON_CLR_RXFIFO_Msk ) + +/** + * @brief This function sets the recording source channel when mono mode is used. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32Ch Left or right channel. Valid values are: + * - \ref I2S_MONO_LEFT + * - \ref I2S_MONO_RIGHT + * @return None + * @details This function selects the recording source channel of monaural mode. + */ +static __INLINE void I2S_SET_MONO_RX_CHANNEL(I2S_T *i2s, uint32_t u32Ch) +{ + u32Ch == I2S_MONO_LEFT ? + (i2s->CON |= I2S_CON_RXLCH_Msk) : + (i2s->CON &= ~I2S_CON_RXLCH_Msk); +} + +/** + * @brief Write data to I2S TX FIFO. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32Data The value written to TX FIFO. + * @return None + * @details This macro will write a value to TX FIFO. + */ +#define I2S_WRITE_TX_FIFO(i2s, u32Data) ( (i2s)->TXFIFO = (u32Data) ) + +/** + * @brief Read RX FIFO. + * @param[in] i2s The pointer of the specified I2S module. + * @return The value read from RX FIFO. + * @details This function will return a value read from RX FIFO. + */ +#define I2S_READ_RX_FIFO(i2s) ( (i2s)->RXFIFO ) + +/** + * @brief Get the interrupt flag. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32Mask The mask value for all interrupt flags. Valid values are: + * - \ref I2S_STATUS_LZCF_Msk + * - \ref I2S_STATUS_RZCF_Msk + * - \ref I2S_STATUS_TXTHF_Msk + * - \ref I2S_STATUS_TXOVF_Msk + * - \ref I2S_STATUS_TXUDF_Msk + * - \ref I2S_STATUS_RXTHF_Msk + * - \ref I2S_STATUS_RXOVF_Msk + * - \ref I2S_STATUS_RXUDF_Msk + * - \ref I2S_STATUS_I2STXINT_Msk + * - \ref I2S_STATUS_I2SRXINT_Msk + * - \ref I2S_STATUS_I2SINT_Msk + * @return The interrupt flags specified by the u32mask parameter. + * @details This macro will return the combination flags of I2SSTATUS register. The flags are specified by the u32mask parameter. + */ +#define I2S_GET_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS & (u32Mask) ) + +/** + * @brief Clear the interrupt flag. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32Mask The mask value for all interrupt flags. Valid values are: + * - \ref I2S_STATUS_LZCF_Msk + * - \ref I2S_STATUS_RZCF_Msk + * - \ref I2S_STATUS_TXOVF_Msk + * - \ref I2S_STATUS_TXUDF_Msk + * - \ref I2S_STATUS_RXOVF_Msk + * - \ref I2S_STATUS_RXUDF_Msk + * @return None + * @details This macro will clear the interrupt flags specified by the u32mask parameter. + */ +#define I2S_CLR_INT_FLAG(i2s, u32Mask) ( (i2s)->STATUS = (u32Mask) ) + +/** + * @brief Get transmit FIFO level + * @param[in] i2s The pointer of the specified I2S module. + * @return TX FIFO level + * @details This macro will return the number of available words in TX FIFO. + */ +#define I2S_GET_TX_FIFO_LEVEL(i2s) ( (((i2s)->STATUS & I2S_STATUS_TX_LEVEL_Msk) >> I2S_STATUS_TX_LEVEL_Pos) & 0xF ) + +/** + * @brief Get receive FIFO level + * @param[in] i2s The pointer of the specified I2S module. + * @return RX FIFO level + * @details This macro will return the number of available words in RX FIFO. + */ +#define I2S_GET_RX_FIFO_LEVEL(i2s) ( (((i2s)->STATUS & I2S_STATUS_RX_LEVEL_Msk) >> I2S_STATUS_RX_LEVEL_Pos) & 0xF ) + + +/* Function prototype declaration */ +uint32_t I2S_Open(I2S_T *i2s, uint32_t u32MasterSlave, uint32_t u32SampleRate, uint32_t u32WordWidth, uint32_t u32Channels, uint32_t u32DataFormat); +void I2S_Close(I2S_T *i2s); +void I2S_EnableInt(I2S_T *i2s, uint32_t u32Mask); +void I2S_DisableInt(I2S_T *i2s, uint32_t u32Mask); +uint32_t I2S_EnableMCLK(I2S_T *i2s, uint32_t u32BusClock); +void I2S_DisableMCLK(I2S_T *i2s); + +/*@}*/ /* end of group I2S_EXPORTED_FUNCTIONS */ + + +/*@}*/ /* end of group I2S_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ + diff --git a/NUC123/StdDriver/inc/pdma.h b/NUC123/StdDriver/inc/pdma.h new file mode 100644 index 0000000..5bf7931 --- /dev/null +++ b/NUC123/StdDriver/inc/pdma.h @@ -0,0 +1,199 @@ +/**************************************************************************//** + * @file PDMA.h + * @version V1.00 + * $Revision: 6 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series PDMA Controller Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __PDMA_H__ +#define __PDMA_H__ + +#include "NUC123.h" + + +/** @addtogroup Standard_Driver Standard Driver + * @{ + */ + +/** @addtogroup PDMA_Driver PDMA Driver + * @{ + */ + +/** @addtogroup PDMA_EXPORTED_CONSTANTS PDMA Exported Constants + @{ +*/ + +/*---------------------------------------------------------------------------------------------------------*/ +/* Data Width Constant Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define PDMA_WIDTH_8 0x00080000UL /*!GCRISR)) + +/** + * @brief Get PDMA Channel Interrupt Status + * + * @param[in] u32Ch Selected DMA channel + * + * @return Interrupt Status + * + * @details This macro gets the channel interrupt status. + */ +#define PDMA_GET_CH_INT_STS(u32Ch) (*((__IO uint32_t *)((uint32_t)&PDMA0->ISR + (uint32_t)((u32Ch)*0x100)))) + +/** + * @brief Clear PDMA Channel Interrupt Flag + * + * @param[in] u32Ch Selected DMA channel + * @param[in] u32Mask Interrupt Mask + * + * @return None + * + * @details This macro clear the channel interrupt flag. + */ +#define PDMA_CLR_CH_INT_FLAG(u32Ch, u32Mask) (*((__IO uint32_t *)((uint32_t)&PDMA0->ISR + (uint32_t)((u32Ch)*0x100))) = (u32Mask)) + +/** + * @brief Check Channel Status + * + * @param[in] u32Ch The selected channel + * + * @retval 0 The selected channel is idle + * @retval 1 The selected channel is busy + * + * @details Check the selected channel is busy or not. + */ +#define PDMA_IS_CH_BUSY(u32Ch) ((*((__IO uint32_t *)((uint32_t)&PDMA0->CSR +(uint32_t)((u32Ch)*0x100)))&PDMA_CSR_TRIG_EN_Msk)? 1 : 0) + +/** + * @brief Set Source Address + * + * @param[in] u32Ch The selected channel + * @param[in] u32Addr The selected address + * + * @return None + * + * @details This macro set the selected channel source address. + */ +#define PDMA_SET_SRC_ADDR(u32Ch, u32Addr) (*((__IO uint32_t *)((uint32_t)&PDMA0->SAR + (uint32_t)((u32Ch)*0x100))) = (u32Addr)) + +/** + * @brief Set Destination Address + * + * @param[in] u32Ch The selected channel + * @param[in] u32Addr The selected address + * + * @return None + * + * @details This macro set the selected channel destination address. + */ +#define PDMA_SET_DST_ADDR(u32Ch, u32Addr) (*((__IO uint32_t *)((uint32_t)&PDMA0->DAR + (uint32_t)((u32Ch)*0x100))) = (u32Addr)) + +/** + * @brief Set Transfer Count + * + * @param[in] u32Ch The selected channel + * @param[in] u32Count Transfer Count + * + * @return None + * + * @details This macro set the selected channel transfer count. + * \hideinitializer + */ +#define PDMA_SET_TRANS_CNT(u32Ch, u32Count) { \ + if (((uint32_t)*((__IO uint32_t *)((uint32_t)&PDMA0->CSR + (uint32_t)((u32Ch)*0x100))) & PDMA_CSR_APB_TWS_Msk) == PDMA_WIDTH_32) \ + *((__IO uint32_t *)((uint32_t)&PDMA0->BCR + (uint32_t)((u32Ch)*0x100))) = ((u32Count) << 2); \ + else if (((uint32_t)*((__IO uint32_t *)((uint32_t)&PDMA0->CSR + (uint32_t)((u32Ch)*0x100))) & PDMA_CSR_APB_TWS_Msk) == PDMA_WIDTH_8) \ + *((__IO uint32_t *)((uint32_t)&PDMA0->BCR + (uint32_t)((u32Ch)*0x100))) = (u32Count); \ + else if (((uint32_t)*((__IO uint32_t *)((uint32_t)&PDMA0->CSR + (uint32_t)((u32Ch)*0x100))) & PDMA_CSR_APB_TWS_Msk) == PDMA_WIDTH_16) \ + *((__IO uint32_t *)((uint32_t)&PDMA0->BCR + (uint32_t)((u32Ch)*0x100))) = ((u32Count) << 1); \ +} + +/** + * @brief Stop the channel + * + * @param[in] u32Ch The selected channel + * + * @return None + * + * @details This macro stop the selected channel. + */ +#define PDMA_STOP(u32Ch) (*((__IO uint32_t *)((uint32_t)&PDMA0->CSR + (uint32_t)((u32Ch)*0x100))) &= ~PDMA_CSR_PDMACEN_Msk) + +void PDMA_Open(uint32_t u32Mask); +void PDMA_Close(void); +void PDMA_SetTransferCnt(uint32_t u32Ch, uint32_t u32Width, uint32_t u32TransCount); +void PDMA_SetTransferAddr(uint32_t u32Ch, uint32_t u32SrcAddr, uint32_t u32SrcCtrl, uint32_t u32DstAddr, uint32_t u32DstCtrl); +void PDMA_SetTransferMode(uint32_t u32Ch, uint32_t u32Periphral, uint32_t u32ScatterEn, uint32_t u32DescAddr); +void PDMA_Trigger(uint32_t u32Ch); +void PDMA_EnableInt(uint32_t u32Ch, uint32_t u32Mask); +void PDMA_DisableInt(uint32_t u32Ch, uint32_t u32Mask); + + +/** + * @} End of PDMA Device Function Interface + */ + +/** + * @} End of Function Interface + */ + +/** + * @} End of PDMA_Driver + */ + + +#endif // __PDMA_H__ diff --git a/NUC123/StdDriver/inc/ps2.h b/NUC123/StdDriver/inc/ps2.h new file mode 100644 index 0000000..2eb4a30 --- /dev/null +++ b/NUC123/StdDriver/inc/ps2.h @@ -0,0 +1,255 @@ +/**************************************************************************//** + * @file PS2.h + * @version V3.00 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series PS/2 Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + ******************************************************************************/ +#ifndef __PS2_H__ +#define __PS2_H__ + +/*---------------------------------------------------------------------------------------------------------*/ +/* Include related headers */ +/*---------------------------------------------------------------------------------------------------------*/ +#include "NUC123.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup PS2_Driver PS2 Driver + @{ +*/ + + +/** @addtogroup PS2_EXPORTED_FUNCTIONS PS2 Exported Functions + @{ +*/ + +/*---------------------------------------------------------------------------------------------------------*/ +/* Define Macros and functions */ +/*---------------------------------------------------------------------------------------------------------*/ + +/** + * @brief To Set PS/2 Tx FIFO length + * + * @param[in] u32Count Tx FIFO length + * + * @return None + * + * @details Before PS/2 data transmit, program needs to set the FIFO depth. + * \hideinitializer + */ +#define PS2_SET_TX_BYTE_CNT(u32Count) (PS2->PS2CON = (PS2->PS2CON & ~PS2_PS2CON_TXFIFO_DEPTH_Msk) \ + | (((u32Count)-1) << PS2_PS2CON_TXFIFO_DEPTH_Pos)) + +/** + * @brief This function use to Get PS/2 Status + * + * @param None + * + * @return PS/2 bus status + * + * @details To get PS/2 bus status which are about Byte index, Tx/Rx status, Error status and PS/2 line status. + */ +#define PS2_GET_STATUS() (PS2->PS2STATUS) + +/** + * @brief This function is used to Clear PS/2 Status + * + * @param[in] u32Mask Clear the specified status of PS/2 module: + * 1. PS2D_PS2STATUS_FRAMERR_Msk 2. PS2D_PS2STATUS_RXOVF_Msk + * + * @return None + * + * @details To clear PS/2 bus status which are about Byte index, TX/RX status, Error status, PS/2 line status. + */ +#define PS2_CLR_STATUS(u32Mask) (PS2->PS2STATUS = (u32Mask)) + +/** + * @brief This function is used to Clear PS/2 Tx FIFO + * + * @param None + * + * @return None + * + * @details Write 1 to terminate PS/2 device to host transmission. + * + * @note Write 1 is always clear Tx FIFO, and need write 0 to STOP the clear action. + */ +__STATIC_INLINE void PS2_CLEAR_TX_FIFO(void) +{ + PS2->PS2CON |= PS2_PS2CON_CLRFIFO_Msk; + PS2->PS2CON &= ~PS2_PS2CON_CLRFIFO_Msk; +} + +/** + * @brief This function is used to Clear PS2 Rx interrupt + * + * @param None + * + * @return None + * + * @details To disable PS/2 receive interrupt occurs. + */ +#define PS2_CLR_RX_INT_FLAG() (PS2->PS2INTID = PS2_PS2INTID_RXINT_Msk) + +/** + * @brief This function is used to Clear PS/2 Tx Interrupt + * + * @param None + * + * @return None + * + * @details To disable PS/2 transmit interrupt occurs. + */ +#define PS2_CLR_TX_INT_FLAG() (PS2->PS2INTID = PS2_PS2INTID_TXINT_Msk) + +/** + * @brief This function is used to Get PS/2 Interrupt + * + * @param[in] u32IntFlag Interrupt flag of PS2D_PS2INTID_TXINT_Msk, PS2D_PS2INTID_RXINT_Msk + * + * @retval 1 Interrupt occurs + * @retval 0 Interrupt not occurs + * + * @details To check PS/2 bus interrupt occur from TX or RX + */ +#define PS2_GET_INT_FLAG(u32IntFlag) ((PS2->PS2INTID & (u32IntFlag))?1:0) + +/** + * @brief Disable PS2CLK and PS2DATA pins override. + * + * @param None + * + * @return None + * + * @details To disable the override control of PS2CLK and PS2DATA pins. + */ +#define PS2_DISABLE_OVERRIDE() (PS2->PS2CON &= ~PS2_PS2CON_OVERRIDE_Msk) + +/** + * @brief Enable PS2CLK and PS2DATA pins Override. + * + * @param None + * + * @return None + * + * @details TO enable the override control of PS2CLK and PS2DATA pins. + */ +#define PS2_ENABLE_OVERRIDE() (PS2->PS2CON |= PS2_PS2CON_OVERRIDE_Msk) + +/** + * @brief This function is used to Get Indicates which data byte in transmit data shift register + * + * @param None + * + * @return The indicates which data byte in transmit data shift register. + * + * @details To get a indication which a data byte in the data shift register. + */ +#define PS2_GET_TX_BYTE_INDEX() ((PS2->PS2STATUS & PS2_PS2STATUS_BYTEIDX_Msk) >> PS2_PS2STATUS_BYTEIDX_Pos) + +/** + * @brief This function is used to set PS2DATA Pin low. + * + * @param None + * + * @return None + * + * @details To control the PS2DATA pin state to low. + */ +#define PS2_SET_DATA_LOW() (PS2->PS2CON &= ~PS2_PS2CON_FPS2DAT_Msk) + +/** + * @brief This function is used to set PS2DATA Pin high + * + * @param None + * + * @return None + * + * @details To control the PS2DATA pin state to high. + */ +#define PS2_SET_DATA_HIGH() (PS2->PS2CON |= PS2_PS2CON_FPS2DAT_Msk) + +/** + * @brief This function is used to set PS2CLK Pin low. + * + * @param None + * + * @return None + * + * @details To control the PS2CLK pin state to low. + */ +#define PS2_SET_CLK_LOW() (PS2->PS2CON &= ~PS2_PS2CON_FPS2CLK_Msk) + +/** + * @brief This function is used to set PS2CLK Pin high. + * + * @param None + * + * @return None + * + * @details To control the PS2CLK pin state to high. + */ +#define PS2_SET_CLK_HIGH() (PS2->PS2CON |= PS2_PS2CON_FPS2CLK_Msk) + +/** + * @brief Disable always sends acknowledge + * + * @param None + * + * @return None + * + * @details If parity error or Stop bit is not received correctly, acknowledge will not be sent to host at 12th clock. + */ +#define PS2_DISABLE_ACK_ALWAYS() (PS2->PS2CON |= PS2_PS2CON_ACK_Msk) + +/** + * @brief Always sends acknowledge + * + * @param None + * + * @return None + * + * @details Always send acknowledge to host at 12th clock for host to device communication. + */ +#define PS2_ENABLE_ACK_ALWAYS() (PS2->PS2CON &= ~PS2_PS2CON_ACK_Msk) + + +/*---------------------------------------------------------------------------------------------------------*/ +/* Define Function Prototypes */ +/*---------------------------------------------------------------------------------------------------------*/ + +void PS2_Open(void); +void PS2_Close(void); +uint8_t PS2_Read(void); +int32_t PS2_Write(uint32_t *pu32Buf, uint32_t u32ByteCount); +void PS2_EnableInt(uint32_t u32Mask); +void PS2_DisableInt(uint32_t u32Mask); + + +/*@}*/ /* end of group PS2_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group PS2_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__PS2_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ + diff --git a/NUC123/StdDriver/inc/pwm.h b/NUC123/StdDriver/inc/pwm.h new file mode 100644 index 0000000..2cc5002 --- /dev/null +++ b/NUC123/StdDriver/inc/pwm.h @@ -0,0 +1,231 @@ +/**************************************************************************//** + * @file pwm.h + * @version V3.00 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series PWM driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ +#ifndef __PWM_H__ +#define __PWM_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup PWM_Driver PWM Driver + @{ +*/ + +/** @addtogroup PWM_EXPORTED_CONSTANTS PWM Exported Constants + @{ +*/ +#define PWM_CHANNEL_NUM (4) /*!< PWM channel number */ +#define PWM_CLK_DIV_1 (4UL) /*!< PWM clock divide by 1 */ +#define PWM_CLK_DIV_2 (0UL) /*!< PWM clock divide by 2 */ +#define PWM_CLK_DIV_4 (1UL) /*!< PWM clock divide by 4 */ +#define PWM_CLK_DIV_8 (2UL) /*!< PWM clock divide by 8 */ +#define PWM_CLK_DIV_16 (3UL) /*!< PWM clock divide by 16 */ +#define PWM_EDGE_ALIGNED (0UL) /*!< PWM working in edge aligned type */ +#define PWM_CENTER_ALIGNED (1UL) /*!< PWM working in center aligned type */ +#define PWM_PERIOD_INT_UNDERFLOW (0) /*!< PWM period interrupt triggered if counter underflow */ +#define PWM_PERIOD_INT_MATCH_CNR (PWM_PIER_INT01TYPE_Msk) /*!< PWM period interrupt triggered if counter match CNR */ +#define PWM_CAPTURE_INT_RISING_LATCH (PWM_CCR0_CRL_IE0_Msk) /*!< PWM capture interrupt if channel has rising transition */ +#define PWM_CAPTURE_INT_FALLING_LATCH (PWM_CCR0_CFL_IE0_Msk) /*!< PWM capture interrupt if channel has falling transition */ +/*---------------------------------------------------------------------------------------------------------*/ +/* PWM Group channel number constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define PWM_CH0 0x0 /*!< PWM Group A channel 0 */ +#define PWM_CH1 0x1 /*!< PWM Group A channel 1 */ +#define PWM_CH2 0x2 /*!< PWM Group A channel 2 */ +#define PWM_CH3 0x3 /*!< PWM Group A channel 3 */ +#define PWM_CCR_MASK 0x000F000F /*!< PWM CCR0/CCR2 bit0~3 and bit16~19 mask */ + +/*@}*/ /* end of group PWM_EXPORTED_CONSTANTS */ + + +/** @addtogroup PWM_EXPORTED_FUNCTIONS PWM Exported Functions + @{ +*/ + +/** + * @brief Enable output inverter of specified channel(s) + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel + * Bit 0 represents channel 0, bit 1 represents channel 1... + * @return None + * @details This macro is used to enable capture input inverter for specified channel(s). + * \hideinitializer + */ +#define PWM_ENABLE_OUTPUT_INVERTER(pwm, u32ChannelMask) \ + do{ \ + int i;\ + (pwm)->PCR &= ~(PWM_PCR_CH0INV_Msk|PWM_PCR_CH1INV_Msk|PWM_PCR_CH2INV_Msk|PWM_PCR_CH3INV_Msk);\ + for(i = 0; i < 4; i++) { \ + if((u32ChannelMask) & (1 << i)) \ + (pwm)->PCR |= (PWM_PCR_CH0INV_Msk << (PWM_PCR_CH0INV_Pos * (i * 4))); \ + } \ + }while(0) + +/** + * @brief Get captured rising data of specified channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return The timer counter, 0~0xFFFF + * @details This macro is used to get captured rising data for specified channel. + */ +#define PWM_GET_CAPTURE_RISING_DATA(pwm, u32ChannelNum) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CRLR0)) + (u32ChannelNum) * 8)))) + +/** + * @brief Get captured falling data of specified channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return The timer counter, 0~0xFFFF + * @details This macro is used to get captured falling data for specified channel. + */ +#define PWM_GET_CAPTURE_FALLING_DATA(pwm, u32ChannelNum) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CFLR0)) + (u32ChannelNum) * 8)))) + +/** + * @brief Set the prescaler of the selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Prescaler Clock prescaler of specified channel. Valid values are between 1 ~ 0xFF + * @return None + * @details This macro is used to set timer pre-scale for specified channel. + * @note If u32Prescaler = 0, corresponding PWM-timer will be stopped. + * @note If u32Prescaler = x (x not equal to 0), it means Clock input is divided by (x + 1) before it is fed to the corresponding PWM counter. + */ +#define PWM_SET_PRESCALER(pwm, u32ChannelNum, u32Prescaler) \ + ((pwm)->PPR = ((pwm)->PPR & ~(PWM_PPR_CP01_Msk << (((u32ChannelNum) >> 1) * 8))) | ((u32Prescaler) << (((u32ChannelNum) >> 1) * 8))) + +/** + * @brief Set the divider of the selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Divider Clock divider of specified channel. Valid values are + * - \ref PWM_CLK_DIV_1 + * - \ref PWM_CLK_DIV_2 + * - \ref PWM_CLK_DIV_4 + * - \ref PWM_CLK_DIV_8 + * - \ref PWM_CLK_DIV_16 + * @return None + * @details This macro is used to set Timer clock source divider selection for specified channel. + */ +#define PWM_SET_DIVIDER(pwm, u32ChannelNum, u32Divider) \ + ((pwm)->CSR = ((pwm)->CSR & ~(PWM_CSR_CSR0_Msk << ((u32ChannelNum) * 4))) | ((u32Divider) << ((u32ChannelNum) * 4))) + +/** + * @brief Set the duty of the selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32CMR Duty of specified channel. Valid values are between 0~0xFFFF + * @return None + * @details This macro is used to set PWM Comparator value for specified channel. + * @note This new setting will take effect on next PWM period. + */ +#define PWM_SET_CMR(pwm, u32ChannelNum, u32CMR) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CMR0)) + (u32ChannelNum) * 12))) = (u32CMR)) + +/** + * @brief Set the period of the selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32CNR Period of specified channel. Valid values are between 0~0xFFFF + * @return None + * @details This macro is used to set timer loaded value(CNR) for specified channel.\n + * Loaded value determines the PWM period. + * @note This new setting will take effect on next PWM period. + * @note PWM counter will stop if period length set to 0. + */ +#define PWM_SET_CNR(pwm, u32ChannelNum, u32CNR) (*((__IO uint32_t *) ((((uint32_t)&((pwm)->CNR0)) + (u32ChannelNum) * 12))) = (u32CNR)) + +/** + * @brief Set the PWM aligned type + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel + * Bit 0 represents channel 0, bit 1 represents channel 1... + * @param[in] u32AlignedType PWM aligned type, valid values are: + * - \ref PWM_EDGE_ALIGNED + * - \ref PWM_CENTER_ALIGNED + * @return None + * @details This macro is used to set the PWM aligned type. + * @note PWM trigger ADC function is only supported when PWM operating at Center-aligned type. + * \hideinitializer + */ +#define PWM_SET_ALIGNED_TYPE(pwm, u32ChannelMask, u32AlignedType) \ + do{ \ + int i; \ + for(i = 0; i < 4; i++) { \ + if((u32ChannelMask) & (1 << i)) \ + (pwm)->PCR = ((pwm)->PCR & ~(PWM_PCR_PWM01TYPE_Msk << (i >> 1))) | ((u32AlignedType) << (PWM_PCR_PWM01TYPE_Pos + (i >> 1))); \ + } \ + }while(0) + + +uint32_t PWM_ConfigCaptureChannel(PWM_T *pwm, + uint32_t u32ChannelNum, + uint32_t u32UnitTimeNsec, + uint32_t u32CaptureEdge); +uint32_t PWM_ConfigOutputChannel(PWM_T *pwm, + uint32_t u32ChannelNum, + uint32_t u32Frequncy, + uint32_t u32DutyCycle); +void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition); +void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition); +uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_EnablePDMA(PWM_T *pwm, uint32_t u32ChannelMask, uint32_t u32RisingFirst); +void PWM_DisablePDMA(PWM_T *pwm, uint32_t u32ChannelMask); +void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration); +void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge); +void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge); +void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge); +uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_EnableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType); +void PWM_DisableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum); +uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType); +void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum); +void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum); +uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum); + + + +/*@}*/ /* end of group PWM_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group PWM_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__PWM_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/spi.h b/NUC123/StdDriver/inc/spi.h new file mode 100644 index 0000000..649fae9 --- /dev/null +++ b/NUC123/StdDriver/inc/spi.h @@ -0,0 +1,393 @@ +/**************************************************************************//** + * @file spi.h + * @version V3.0 + * $Revision: 11 $ + * $Date: 15/07/02 3:18p $ + * @brief NUC123 Series SPI Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __SPI_H__ +#define __SPI_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup SPI_Driver SPI Driver + @{ +*/ + +/** @addtogroup SPI_EXPORTED_CONSTANTS SPI Exported Constants + @{ +*/ + +#define SPI_MODE_0 (SPI_CNTRL_TX_NEG_Msk) /*!< CLKP=0; RX_NEG=0; TX_NEG=1 */ +#define SPI_MODE_1 (SPI_CNTRL_RX_NEG_Msk) /*!< CLKP=0; RX_NEG=1; TX_NEG=0 */ +#define SPI_MODE_2 (SPI_CNTRL_CLKP_Msk | SPI_CNTRL_RX_NEG_Msk) /*!< CLKP=1; RX_NEG=1; TX_NEG=0 */ +#define SPI_MODE_3 (SPI_CNTRL_CLKP_Msk | SPI_CNTRL_TX_NEG_Msk) /*!< CLKP=1; RX_NEG=0; TX_NEG=1 */ + +#define SPI_SLAVE (SPI_CNTRL_SLAVE_Msk) /*!< Set as slave */ +#define SPI_MASTER (0x0) /*!< Set as master */ + +#define SPI_SS0 (1<CNTRL2 |= SPI_CNTRL2_SLV_ABORT_Msk) + +/** + * @brief Clear the Slave 3-wire mode start interrupt flag. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Write 1 to SLV_START_INTSTS bit of SPI_STATUS register to clear the Slave 3-wire mode start interrupt flag. + */ +#define SPI_CLR_3WIRE_START_INT_FLAG(spi) ((spi)->STATUS = SPI_STATUS_SLV_START_INTSTS_Msk) + +/** + * @brief Clear the unit transfer interrupt flag. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Write 1 to IF bit of SPI_STATUS register to clear the unit transfer interrupt flag. + */ +#define SPI_CLR_UNIT_TRANS_INT_FLAG(spi) ((spi)->STATUS = SPI_STATUS_IF_Msk) + +/** + * @brief Disable 2-bit Transfer mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Clear TWOB bit of SPI_CNTRL register to disable 2-bit Transfer mode. + */ +#define SPI_DISABLE_2BIT_MODE(spi) ((spi)->CNTRL &= ~SPI_CNTRL_TWOB_Msk) + +/** + * @brief Disable Slave 3-wire mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Clear NOSLVSEL bit of SPI_CNTRL2 register to disable Slave 3-wire mode. + */ +#define SPI_DISABLE_3WIRE_MODE(spi) ((spi)->CNTRL2 &= ~SPI_CNTRL2_NOSLVSEL_Msk) + +/** + * @brief Disable Dual I/O mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Clear DUAL_IO_EN bit of SPI_CNTRL2 register to disable Dual I/O mode. + */ +#define SPI_DISABLE_DUAL_MODE(spi) ((spi)->CNTRL2 &= ~SPI_CNTRL2_DUAL_IO_EN_Msk) + +/** + * @brief Enable 2-bit Transfer mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set TWOB bit of SPI_CNTRL register to enable 2-bit Transfer mode. + */ +#define SPI_ENABLE_2BIT_MODE(spi) ((spi)->CNTRL |= SPI_CNTRL_TWOB_Msk) + +/** + * @brief Enable Slave 3-wire mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set NOSLVSEL bit of SPI_CNTRL2 register to enable Slave 3-wire mode. + * Only available in Slave mode. + */ +#define SPI_ENABLE_3WIRE_MODE(spi) ((spi)->CNTRL2 |= SPI_CNTRL2_NOSLVSEL_Msk) + +/** + * @brief Enable Dual input mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Clear DUAL_IO_DIR bit and set DUAL_IO_EN bit of SPI_CNTRL2 register to enable Dual input mode. + */ +#define SPI_ENABLE_DUAL_INPUT_MODE(spi) ((spi)->CNTRL2 = ((spi)->CNTRL2 & (~SPI_CNTRL2_DUAL_IO_DIR_Msk)) | SPI_CNTRL2_DUAL_IO_EN_Msk) + +/** + * @brief Enable Dual output mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set DUAL_IO_DIR bit and DUAL_IO_EN bit of SPI_CNTRL2 register to enable Dual output mode. + */ +#define SPI_ENABLE_DUAL_OUTPUT_MODE(spi) ((spi)->CNTRL2 |= (SPI_CNTRL2_DUAL_IO_EN_Msk | SPI_CNTRL2_DUAL_IO_DIR_Msk)) + +/** + * @brief Trigger RX PDMA function. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set RX_DMA_GO bit of SPI_DMA register to enable RX PDMA transfer function. + */ +#define SPI_TRIGGER_RX_PDMA(spi) ((spi)->DMA |= SPI_DMA_RX_DMA_GO_Msk) + +/** + * @brief Trigger TX PDMA function. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set TX_DMA_GO bit of SPI_DMA register to enable TX PDMA transfer function. + */ +#define SPI_TRIGGER_TX_PDMA(spi) ((spi)->DMA |= SPI_DMA_TX_DMA_GO_Msk) + +/** + * @brief Trigger TX and RX PDMA function. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set TX_DMA_GO bit and RX_DMA_GO bit of SPI_DMA register to enable TX and RX PDMA transfer function. + */ +#define SPI_TRIGGER_TX_RX_PDMA(spi) ((spi)->DMA |= (SPI_DMA_TX_DMA_GO_Msk | SPI_DMA_RX_DMA_GO_Msk)) + +/** + * @brief Get the count of available data in RX FIFO. + * @param[in] spi The pointer of the specified SPI module. + * @return The count of available data in RX FIFO. + * @details Read RX_FIFO_COUNT (SPI_STATUS[15:12]) to get the count of available data in RX FIFO. + */ +#define SPI_GET_RX_FIFO_COUNT(spi) (((spi)->STATUS & SPI_STATUS_RX_FIFO_COUNT_Msk) >> SPI_STATUS_RX_FIFO_COUNT_Pos) + +/** + * @brief Get the RX FIFO empty flag. + * @param[in] spi The pointer of the specified SPI module. + * @retval 0 RX FIFO is not empty. + * @retval 1 RX FIFO is empty. + * @details Read RX_EMPTY bit of SPI_STATUS register to get the RX FIFO empty flag. + */ +#define SPI_GET_RX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_RX_EMPTY_Msk)>>SPI_STATUS_RX_EMPTY_Pos) + +/** + * @brief Get the TX FIFO empty flag. + * @param[in] spi The pointer of the specified SPI module. + * @retval 0 TX FIFO is not empty. + * @retval 1 TX FIFO is empty. + * @details Read TX_EMPTY bit of SPI_STATUS register to get the TX FIFO empty flag. + */ +#define SPI_GET_TX_FIFO_EMPTY_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TX_EMPTY_Msk)>>SPI_STATUS_TX_EMPTY_Pos) + +/** + * @brief Get the TX FIFO full flag. + * @param[in] spi The pointer of the specified SPI module. + * @retval 0 TX FIFO is not full. + * @retval 1 TX FIFO is full. + * @details Read TX_FULL bit of SPI_STATUS register to get the TX FIFO full flag. + */ +#define SPI_GET_TX_FIFO_FULL_FLAG(spi) (((spi)->STATUS & SPI_STATUS_TX_FULL_Msk)>>SPI_STATUS_TX_FULL_Pos) + +/** + * @brief Get the datum read from RX0 register. + * @param[in] spi The pointer of the specified SPI module. + * @return Data in RX0 register. + * @details Read SPI_RX0 register to get the received datum. + */ +#define SPI_READ_RX0(spi) ((spi)->RX[0]) + +/** + * @brief Get the datum read from RX1 register. + * @param[in] spi The pointer of the specified SPI module. + * @return Data in RX1 register. + * @details Read SPI_RX1 register to get the received datum. + */ +#define SPI_READ_RX1(spi) ((spi)->RX[1]) + +/** + * @brief Write datum to TX0 register. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32TxData The datum which user attempt to transfer through SPI bus. + * @return None. + * @details Write u32TxData to TX0 register. + */ +#define SPI_WRITE_TX0(spi, u32TxData) ((spi)->TX[0] = (u32TxData)) + +/** + * @brief Write datum to TX1 register. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32TxData The datum which user attempt to transfer through SPI bus. + * @return None. + * @details Write u32TxData to TX1 register. + */ +#define SPI_WRITE_TX1(spi, u32TxData) ((spi)->TX[1] = (u32TxData)) + +/** + * @brief Set SPIn_SS0, SPIn_SS1 pin to high or low state. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] ss0 0 = Set SPIn_SS0 to low. 1 = Set SPIn_SS0 to high. + * @param[in] ss1 0 = Set SPIn_SS1 to low. 1 = Set SPIn_SS1 to high. + * @return None. + * @details Disable automatic slave selection function and set SPIn_SS0/SPIn_SS1 pin to specified high/low state. + * Only available in Master mode. + */ +#define SPI_SET_SS_LEVEL(spi, ss0, ss1) ((spi)->SSR = ((spi)->SSR & ~(SPI_SSR_AUTOSS_Msk|SPI_SSR_SS_LVL_Msk|SPI_SSR_SSR_Msk)) | (((ss1)^1) << 1) | ((ss0)^1)) + +/** + * @brief Set SPIn_SS0 pin to high state. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Disable automatic slave selection function and set SPIn_SS0 pin to high state. Only available in Master mode. + */ +#define SPI_SET_SS0_HIGH(spi) ((spi)->SSR = ((spi)->SSR & ~(SPI_SSR_AUTOSS_Msk|SPI_SSR_SS_LVL_Msk|SPI_SS0))) + +/** + * @brief Set SPIn_SS1 pin to high state. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Disable automatic slave selection function and set SPIn_SS1 pin to high state. Only available in Master mode. + */ +#define SPI_SET_SS1_HIGH(spi) ((spi)->SSR = ((spi)->SSR & ~(SPI_SSR_AUTOSS_Msk|SPI_SSR_SS_LVL_Msk|SPI_SS1))) + +/** + * @brief Set SPIn_SS0 pin to low state. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Disable automatic slave selection function and set SPIn_SS0 pin to low state. Only available in Master mode. + */ +#define SPI_SET_SS0_LOW(spi) ((spi)->SSR = ((spi)->SSR & ~(SPI_SSR_AUTOSS_Msk|SPI_SSR_SS_LVL_Msk|SPI_SS0)) | SPI_SS0) + +/** + * @brief Set SPIn_SS1 pin to low state. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Disable automatic slave selection function and set SPIn_SS1 pin to low state. Only available in Master mode. + */ +#define SPI_SET_SS1_LOW(spi) ((spi)->SSR = ((spi)->SSR & ~(SPI_SSR_AUTOSS_Msk|SPI_SSR_SS_LVL_Msk|SPI_SS1)) | SPI_SS1) + +/** + * @brief Enable Byte Reorder function. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set REORDER bit of SPI_CNTRL register to enable Byte Reorder function. + */ +#define SPI_ENABLE_BYTE_REORDER(spi) ((spi)->CNTRL |= SPI_CNTRL_REORDER_Msk) + +/** + * @brief Disable Byte Reorder function. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Clear REORDER bit of SPI_CNTRL register to disable Byte Reorder function. + */ +#define SPI_DISABLE_BYTE_REORDER(spi) ((spi)->CNTRL &= ~SPI_CNTRL_REORDER_Msk) + +/** + * @brief Set the length of suspend interval. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32SuspCycle Decides the length of suspend interval. It could be 0 ~ 15. + * @return None. + * @details Set the length of suspend interval according to u32SuspCycle. + * The length of suspend interval is ((u32SuspCycle + 0.5) * the length of one SPI bus clock cycle). + * Only available in Master mode. + */ +#define SPI_SET_SUSPEND_CYCLE(spi, u32SuspCycle) ((spi)->CNTRL = ((spi)->CNTRL & ~SPI_CNTRL_SP_CYCLE_Msk) | ((u32SuspCycle) << SPI_CNTRL_SP_CYCLE_Pos)) + +/** + * @brief Set the SPI transfer sequence with LSB first. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Set LSB bit of SPI_CNTRL register to set the SPI transfer sequence with LSB first. + */ +#define SPI_SET_LSB_FIRST(spi) ((spi)->CNTRL |= SPI_CNTRL_LSB_Msk) + +/** + * @brief Set the SPI transfer sequence with MSB first. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details Clear LSB bit of SPI_CNTRL register to set the SPI transfer sequence with MSB first. + */ +#define SPI_SET_MSB_FIRST(spi) ((spi)->CNTRL &= ~SPI_CNTRL_LSB_Msk) + +/** + * @brief Set the data width of a SPI transaction. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32Width The bit width of transfer data. + * @return None. + * @details The data width can be 8 ~ 32 bits. + */ +#define SPI_SET_DATA_WIDTH(spi, u32Width) ((spi)->CNTRL = ((spi)->CNTRL & ~SPI_CNTRL_TX_BIT_LEN_Msk) | (((u32Width)&0x1F) << SPI_CNTRL_TX_BIT_LEN_Pos)) + +/** + * @brief Get the SPI busy state. + * @param[in] spi The pointer of the specified SPI module. + * @retval 0 SPI controller is not busy. + * @retval 1 SPI controller is busy. + * @details This macro will return the busy state of SPI controller. + */ +#define SPI_IS_BUSY(spi) ( ((spi)->CNTRL & SPI_CNTRL_GO_BUSY_Msk)>>SPI_CNTRL_GO_BUSY_Pos ) + +/** + * @brief Set the GO_BUSY bit to trigger SPI transfer. + * @param[in] spi The pointer of the specified SPI module. + * @return None. + * @details If FIFO mode is disabled, user can use this macro to trigger the data transfer after all configuration is ready. + * If FIFO mode is enabled, user should not use this macro to trigger the data transfer. SPI controller will trigger the data transfer + * automatically after user write to SPI_TX0/1 register. + */ +#define SPI_TRIGGER(spi) ((spi)->CNTRL |= SPI_CNTRL_GO_BUSY_Msk) + + + +/* Function prototype declaration */ +uint32_t SPI_Open(SPI_T *spi, uint32_t u32MasterSlave, uint32_t u32SPIMode, uint32_t u32DataWidth, uint32_t u32BusClock); +void SPI_Close(SPI_T *spi); +void SPI_ClearRxFIFO(SPI_T *spi); +void SPI_ClearTxFIFO(SPI_T *spi); +void SPI_DisableAutoSS(SPI_T *spi); +void SPI_EnableAutoSS(SPI_T *spi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel); +uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock); +void SPI_EnableFIFO(SPI_T *spi, uint32_t u32TxThreshold, uint32_t u32RxThreshold); +void SPI_DisableFIFO(SPI_T *spi); +uint32_t SPI_GetBusClock(SPI_T *spi); +void SPI_EnableInt(SPI_T *spi, uint32_t u32Mask); +void SPI_DisableInt(SPI_T *spi, uint32_t u32Mask); +uint32_t SPI_GetIntFlag(SPI_T *spi, uint32_t u32Mask); +void SPI_ClearIntFlag(SPI_T *spi, uint32_t u32Mask); +uint32_t SPI_GetStatus(SPI_T *spi, uint32_t u32Mask); + + + +/** + * @} End of SPI Device Function Interface + */ + +/** + * @} End of NUC123 Function Interface + */ + +/** + * @} End of Standard_Driver + */ + +#ifdef __cplusplus +} +#endif + +#endif + diff --git a/NUC123/StdDriver/inc/sys.h b/NUC123/StdDriver/inc/sys.h new file mode 100644 index 0000000..6cc04ed --- /dev/null +++ b/NUC123/StdDriver/inc/sys.h @@ -0,0 +1,1369 @@ +/**************************************************************************//** + * @file sys.h + * @version V3.00 + * $Revision: 41 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series SYS Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __SYS_H__ +#define __SYS_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup SYS_Driver SYS Driver + @{ +*/ + +/** @addtogroup SYS_EXPORTED_CONSTANTS SYS Exported Constants + @{ +*/ + + +/*---------------------------------------------------------------------------------------------------------*/ +/* Module Reset Control Resister constant definitions. */ +/*---------------------------------------------------------------------------------------------------------*/ +#define PDMA_RST ((0x0<<24) | SYS_IPRSTC1_PDMA_RST_Pos ) /*!< PDMA reset is one of the SYS_ResetModule parameter */ +#define GPIO_RST ((0x4<<24) | SYS_IPRSTC2_GPIO_RST_Pos ) /*!< GPIO reset is one of the SYS_ResetModule parameter */ +#define TMR0_RST ((0x4<<24) | SYS_IPRSTC2_TMR0_RST_Pos ) /*!< TMR0 reset is one of the SYS_ResetModule parameter */ +#define TMR1_RST ((0x4<<24) | SYS_IPRSTC2_TMR1_RST_Pos ) /*!< TMR1 reset is one of the SYS_ResetModule parameter */ +#define TMR2_RST ((0x4<<24) | SYS_IPRSTC2_TMR2_RST_Pos ) /*!< TMR2 reset is one of the SYS_ResetModule parameter */ +#define TMR3_RST ((0x4<<24) | SYS_IPRSTC2_TMR3_RST_Pos ) /*!< TMR3 reset is one of the SYS_ResetModule parameter */ +#define I2C0_RST ((0x4<<24) | SYS_IPRSTC2_I2C0_RST_Pos ) /*!< I2C0 reset is one of the SYS_ResetModule parameter */ +#define I2C1_RST ((0x4<<24) | SYS_IPRSTC2_I2C1_RST_Pos ) /*!< I2C1 reset is one of the SYS_ResetModule parameter */ +#define SPI0_RST ((0x4<<24) | SYS_IPRSTC2_SPI0_RST_Pos ) /*!< SPI0 reset is one of the SYS_ResetModule parameter */ +#define SPI1_RST ((0x4<<24) | SYS_IPRSTC2_SPI1_RST_Pos ) /*!< SPI1 reset is one of the SYS_ResetModule parameter */ +#define SPI2_RST ((0x4<<24) | SYS_IPRSTC2_SPI2_RST_Pos ) /*!< SPI2 reset is one of the SYS_ResetModule parameter */ +#define UART0_RST ((0x4<<24) | SYS_IPRSTC2_UART0_RST_Pos ) /*!< UART0 reset is one of the SYS_ResetModule parameter */ +#define UART1_RST ((0x4<<24) | SYS_IPRSTC2_UART1_RST_Pos ) /*!< UART1 reset is one of the SYS_ResetModule parameter */ +#define PWM03_RST ((0x4<<24) | SYS_IPRSTC2_PWM03_RST_Pos ) /*!< PWM03 reset is one of the SYS_ResetModule parameter */ +#define PS2_RST ((0x4<<24) | SYS_IPRSTC2_PS2_RST_Pos ) /*!< PS2 reset is one of the SYS_ResetModule parameter */ +#define USBD_RST ((0x4<<24) | SYS_IPRSTC2_USBD_RST_Pos ) /*!< USBD reset is one of the SYS_ResetModule parameter */ +#define ADC_RST ((0x4<<24) | SYS_IPRSTC2_ADC_RST_Pos ) /*!< ADC reset is one of the SYS_ResetModule parameter */ +#define I2S_RST ((0x4<<24) | SYS_IPRSTC2_I2S_RST_Pos ) /*!< I2S reset is one of the SYS_ResetModule parameter */ + + +/*---------------------------------------------------------------------------------------------------------*/ +/* Brown Out Detector Threshold Voltage Selection constant definitions. */ +/*---------------------------------------------------------------------------------------------------------*/ +#define SYS_BODCR_BOD_RST_EN (1UL<GPA_MFP &= ~(SYS_GPA_MFP_PA10_Msk | SYS_GPA_MFP_PA11_Msk); + SYS->ALT_MFP &= ~(SYS_ALT_MFP_PA10_Msk | SYS_ALT_MFP_PA11_Msk); + + SYS->GPA_MFP |= (SYS_GPA_MFP_PA10_I2C1_SDA | SYS_GPA_MFP_PA11_I2C1_SCL); + SYS->ALT_MFP |= (SYS_ALT_MFP_PA10_I2C1_SDA | SYS_ALT_MFP_PA11_I2C1_SCL); +*/ + +//PA.10 +#define SYS_GPA_MFP_PA10_GPIO 0x00000000UL /*!< GPA_MFP PA.10 setting for GPIO */ +#define SYS_ALT_MFP_PA10_GPIO 0x00000000UL /*!< ALT_MFP PA.10 setting for GPIO */ +#define SYS_ALT_MFP1_PA10_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.10 */ + +#define SYS_GPA_MFP_PA10_I2C1_SDA (1UL<<10) /*!< GPA_MFP PA.10 setting for I2C1_SDA */ +#define SYS_ALT_MFP_PA10_I2C1_SDA 0x00000000UL /*!< ALT_MFP PA.10 setting for I2C1_SDA */ +#define SYS_ALT_MFP1_PA10_I2C1_SDA (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.10 */ + +#define SYS_GPA_MFP_PA10_SPI1_MISO0 0x00000000UL /*!< GPA_MFP PA.10 setting for SPI1_MISO0 */ +#define SYS_ALT_MFP_PA10_SPI1_MISO0 (1UL<<12) /*!< ALT_MFP PA.10 setting for SPI1_MISO0 */ +#define SYS_ALT_MFP1_PA10_SPI1_MISO0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.10 */ + +#define SYS_GPA_MFP_PA10_SPI2_MISO0 (1UL<<10) /*!< GPA_MFP PA.10 setting for SPI2_MISO0 */ +#define SYS_ALT_MFP_PA10_SPI2_MISO0 (1UL<<12) /*!< ALT_MFP PA.10 setting for SPI2_MISO0 */ +#define SYS_ALT_MFP1_PA10_SPI2_MISO0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.10 */ + +#define SYS_GPA_MFP_PA10_Msk (1UL<<10) /*!< GPA_MFP PA.10 mask */ +#define SYS_ALT_MFP_PA10_Msk (1UL<<12) /*!< ALT_MFP PA.10 mask */ +#define SYS_ALT_MFP1_PA10_Msk (uint32_t)NULL /*!< No ALT_MFP1 PA.10 mask */ + +//PA.11 +#define SYS_GPA_MFP_PA11_GPIO 0x00000000UL /*!< GPA_MFP PA.11 setting for GPIO */ +#define SYS_ALT_MFP_PA11_GPIO 0x00000000UL /*!< ALT_MFP PA.11 setting for GPIO */ +#define SYS_ALT_MFP1_PA11_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.11 */ + +#define SYS_GPA_MFP_PA11_I2C1_SCL (1UL<<11) /*!< GPA_MFP PA.11 setting for I2C1_SCL */ +#define SYS_ALT_MFP_PA11_I2C1_SCL 0x00000000UL /*!< ALT_MFP PA.11 setting for I2C1_SCL */ +#define SYS_ALT_MFP1_PA11_I2C1_SCL (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.11 */ + +#define SYS_GPA_MFP_PA11_SPI1_CLK 0x00000000UL /*!< GPA_MFP PA.11 setting for SPI1_CLK */ +#define SYS_ALT_MFP_PA11_SPI1_CLK (1UL<<11) /*!< ALT_MFP PA.11 setting for SPI1_CLK */ +#define SYS_ALT_MFP1_PA11_SPI1_CLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.11 */ + +#define SYS_GPA_MFP_PA11_SPI2_MOSI0 (1UL<<11) /*!< GPA_MFP PA.11 setting for MOSI20 */ +#define SYS_ALT_MFP_PA11_SPI2_MOSI0 (1UL<<11) /*!< ALT_MFP PA.11 setting for MOSI20 */ +#define SYS_ALT_MFP1_PA11_SPI2_MOSI0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.11 */ + +#define SYS_GPA_MFP_PA11_Msk (1UL<<11) /*!< GPA_MFP PA.11 mask */ +#define SYS_ALT_MFP_PA11_Msk (1UL<<11) /*!< ALT_MFP PA.11 mask */ +#define SYS_ALT_MFP1_PA11_Msk (uint32_t)NULL /*!< No ALT_MFP1 PA.11 mask */ + +//PA.12 +#define SYS_GPA_MFP_PA12_GPIO 0x00000000UL /*!< GPA_MFP PA.12 setting for GPIO */ +#define SYS_ALT_MFP_PA12_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PA.12 */ +#define SYS_ALT_MFP1_PA12_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.12 */ + +#define SYS_GPA_MFP_PA12_PWM0 (1UL<<12) /*!< GPA_MFP PA.12 setting for PWM0 */ +#define SYS_ALT_MFP_PA12_PWM0 (uint32_t)NULL /*!< No ALT_MFP setting for PA.12 */ +#define SYS_ALT_MFP1_PA12_PWM0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.12 */ + +#define SYS_GPA_MFP_PA12_Msk (1UL<<12) /*!< GPA_MFP PA.12 mask */ +#define SYS_ALT_MFP_PA12_Msk (uint32_t)NULL /*!< No ALT_MFP PA.12 mask */ +#define SYS_ALT_MFP1_PA12_Msk (uint32_t)NULL /*!< No ALT_MFP1 PA.12 mask */ + +//PA.13 +#define SYS_GPA_MFP_PA13_GPIO 0x00000000UL /*!< GPA_MFP PA.13 setting for GPIO */ +#define SYS_ALT_MFP_PA13_GPIO (uint32_t)NULL /*!< no ALT_MFP setting for PA.13 */ +#define SYS_ALT_MFP1_PA13_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.13 */ + +#define SYS_GPA_MFP_PA13_PWM1 (1UL<<13) /*!< GPA_MFP PA.13 setting for PWM1 */ +#define SYS_ALT_MFP_PA13_PWM1 (uint32_t)NULL /*!< no ALT_MFP setting for PA.13 */ +#define SYS_ALT_MFP1_PA13_PWM1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.13 */ + +#define SYS_GPA_MFP_PA13_Msk (1UL<<13) /*!< GPA_MFP PA.13 mask */ +#define SYS_ALT_MFP_PA13_Msk (uint32_t)NULL /*!< no ALT_MFP PA.13 mask */ +#define SYS_ALT_MFP1_PA13_Msk (uint32_t)NULL /*!< No ALT_MFP1 PA.13 mask */ + +//PA.14 +#define SYS_GPA_MFP_PA14_GPIO 0x00000000UL /*!< GPA_MFP PA.14 setting for GPIO */ +#define SYS_ALT_MFP_PA14_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PA.14 */ +#define SYS_ALT_MFP1_PA14_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.14 */ + +#define SYS_GPA_MFP_PA14_PWM2 (1UL<<14) /*!< GPA_MFP PA.14 setting for PWM2 */ +#define SYS_ALT_MFP_PA14_PWM2 (uint32_t)NULL /*!< No ALT_MFP setting for PA.14 */ +#define SYS_ALT_MFP1_PA14_PWM2 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.14 */ + +#define SYS_GPA_MFP_PA14_Msk (1UL<<14) /*!< GPA_MFP PA.14 mask */ +#define SYS_ALT_MFP_PA14_Msk (uint32_t)NULL /*!< No ALT_MFP PA.14 mask */ +#define SYS_ALT_MFP1_PA14_Msk (uint32_t)NULL /*!< No ALT_MFP1 PA.14 mask */ + +//PA.15 +#define SYS_GPA_MFP_PA15_GPIO 0x00000000UL /*!< GPA_MFP PA.15 setting for GPIO */ +#define SYS_ALT_MFP_PA15_GPIO 0x00000000UL /*!< ALT_MFP PA.15 setting for GPIO */ +#define SYS_ALT_MFP1_PA15_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.15 */ + +#define SYS_GPA_MFP_PA15_PWM3 (1UL<<15) /*!< GPA_MFP PA.15 setting for PWM3 */ +#define SYS_ALT_MFP_PA15_PWM3 0x00000000UL /*!< ALT_MFP PA.15 setting for PWM3 */ +#define SYS_ALT_MFP1_PA15_PWM3 (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.15 */ + +#define SYS_GPA_MFP_PA15_CLKO 0x00000000UL /*!< GPA_MFP PA.15 setting for CLKO */ +#define SYS_ALT_MFP_PA15_CLKO (1UL<<9) /*!< ALT_MFP PA.15 setting for CLKO */ +#define SYS_ALT_MFP1_PA15_CLKO (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.15 */ + +#define SYS_GPA_MFP_PA15_I2S_MCLK (1UL<<15) /*!< GPA_MFP PA.15 setting for I2S_MCLK */ +#define SYS_ALT_MFP_PA15_I2S_MCLK (1UL<<9) /*!< ALT_MFP PA.15 setting for I2S_MCLK */ +#define SYS_ALT_MFP1_PA15_I2S_MCLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PA.15 */ + +#define SYS_GPA_MFP_PA15_Msk (1UL<<15) /*!< GPA_MFP PA.15 mask */ +#define SYS_ALT_MFP_PA15_Msk (1UL<<9) /*!< ALT_MFP PA.15 mask */ +#define SYS_ALT_MFP1_PA15_Msk (uint32_t)NULL /*!< No ALT_MFP1 PA.15 mask */ + +//PB.0 +#define SYS_GPB_MFP_PB0_GPIO 0x00000000UL /*!< GPB_MFP PB.0 setting for GPIO */ +#define SYS_ALT_MFP_PB0_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PB.0 */ +#define SYS_ALT_MFP1_PB0_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.0 */ + +#define SYS_GPB_MFP_PB0_UART0_RXD (1UL<<0) /*!< GPB_MFP PB.0 setting for UART0_RXD */ +#define SYS_ALT_MFP_PB0_UART0_RXD (uint32_t)NULL /*!< No ALT_MFP setting for PB.0 */ +#define SYS_ALT_MFP1_PB0_UART0_RXD (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.0 */ + +#define SYS_GPB_MFP_PB0_Msk (1UL<<0) /*!< GPB_MFP PB.0 mask */ +#define SYS_ALT_MFP_PB0_Msk (uint32_t)NULL /*!< No ALT_MFP PB.0 mask */ +#define SYS_ALT_MFP1_PB0_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.0 mask */ + +//PB.1 +#define SYS_GPB_MFP_PB1_GPIO 0x00000000UL /*!< GPB_MFP PB.1 setting for GPIO */ +#define SYS_ALT_MFP_PB1_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PB.1 */ +#define SYS_ALT_MFP1_PB1_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.1 */ + +#define SYS_GPB_MFP_PB1_UART0_TXD (1UL<<1) /*!< GPB_MFP PB.1 setting for UART0_TXD */ +#define SYS_ALT_MFP_PB1_UART0_TXD (uint32_t)NULL /*!< No ALT_MFP setting for PB.1 */ +#define SYS_ALT_MFP1_PB1_UART0_TXD (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.1 */ + +#define SYS_GPB_MFP_PB1_Msk (1UL<<1) /*!< GPB_MFP PB.1 mask */ +#define SYS_ALT_MFP_PB1_Msk (uint32_t)NULL /*!< No ALT_MFP PB.1 mask */ +#define SYS_ALT_MFP1_PB1_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.1 mask */ + +//PB.2 +#define SYS_GPB_MFP_PB2_GPIO 0x00000000UL /*!< GPB_MFP PB.2 setting for GPIO */ +#define SYS_ALT_MFP_PB2_GPIO 0x00000000UL /*!< ALT_MFP PB.2 setting for GPIO */ +#define SYS_ALT_MFP1_PB2_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.2 */ + +#define SYS_GPB_MFP_PB2_UART0_nRTS (1UL<<2) /*!< GPB_MFP PB.2 setting for UART0_nRTS */ +#define SYS_ALT_MFP_PB2_UART0_nRTS 0x00000000UL /*!< ALT_MFP PB.2 setting for UART0_nRTS */ +#define SYS_ALT_MFP1_PB2_UART0_nRTS (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.2 */ + +#define SYS_GPB_MFP_PB2_TM2_EXT (1UL<<2) /*!< GPB_MFP PB.2 setting for TM2_EXT */ +#define SYS_ALT_MFP_PB2_TM2_EXT (1UL<<26) /*!< ALT_MFP PB.2 setting for TM2_EXT */ +#define SYS_ALT_MFP1_PB2_TM2_EXT (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.2 */ + +#define SYS_GPB_MFP_PB2_Msk (1UL<<2) /*!< GPB_MFP PB.2 mask */ +#define SYS_ALT_MFP_PB2_Msk (1UL<<26) /*!< ALT_MFP PB.2 mask */ +#define SYS_ALT_MFP1_PB2_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.2 mask */ + +//PB.3 +#define SYS_GPB_MFP_PB3_GPIO 0x00000000UL /*!< GPB_MFP PB.3 setting for GPIO */ +#define SYS_ALT_MFP_PB3_GPIO 0x00000000UL /*!< ALT_MFP PB.3 setting for GPIO */ +#define SYS_ALT_MFP1_PB3_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.3 */ + +#define SYS_GPB_MFP_PB3_UART0_nCTS (1UL<<3) /*!< GPB_MFP PB.3 setting for UART0_nCTS */ +#define SYS_ALT_MFP_PB3_UART0_nCTS 0x00000000UL /*!< ALT_MFP PB.3 setting for UART0_nCTS */ +#define SYS_ALT_MFP1_PB3_UART0_nCTS (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.3 */ + +#define SYS_GPB_MFP_PB3_TM3_EXT (1UL<<3) /*!< GPB_MFP PB.3 setting for TM3_EXT */ +#define SYS_ALT_MFP_PB3_TM3_EXT (1UL<<27) /*!< ALT_MFP PB.3 setting for TM3_EXT */ +#define SYS_ALT_MFP1_PB3_TM3_EXT (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.3 */ + +#define SYS_GPB_MFP_PB3_Msk (1UL<<3) /*!< GPB_MFP PB.3 mask */ +#define SYS_ALT_MFP_PB3_Msk (1UL<<27) /*!< ALT_MFP PB.3 mask */ +#define SYS_ALT_MFP1_PB3_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.3 mask */ + +//PB.4 +#define SYS_GPB_MFP_PB4_GPIO 0x00000000UL /*!< GPB_MFP PB.4 setting for GPIO */ +#define SYS_ALT_MFP_PB4_GPIO 0x00000000UL /*!< ALT_MFP PB.4 setting for GPIO */ +#define SYS_ALT_MFP1_PB4_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.4 */ + +#define SYS_GPB_MFP_PB4_UART1_RXD (1UL<<4) /*!< GPB_MFP PB.4 setting for UART1_RXD */ +#define SYS_ALT_MFP_PB4_UART1_RXD 0x00000000UL /*!< ALT_MFP PB.4 setting for UART1_RXD */ +#define SYS_ALT_MFP1_PB4_UART1_RXD (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.4 */ + +#define SYS_GPB_MFP_PB4_SPI2_SS0 0x00000000UL /*!< GPB_MFP PB.4 setting for SPI2_SS0 */ +#define SYS_ALT_MFP_PB4_SPI2_SS0 (1UL<<15) /*!< ALT_MFP PB.4 setting for SPI2_SS0 */ +#define SYS_ALT_MFP1_PB4_SPI2_SS0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.4 */ + +#define SYS_GPB_MFP_PB4_SPI1_SS1 (1UL<<4) /*!< GPB_MFP PB.4 setting for SPI1_SS1 */ +#define SYS_ALT_MFP_PB4_SPI1_SS1 (1UL<<15) /*!< ALT_MFP PB.4 setting for SPI1_SS1 */ +#define SYS_ALT_MFP1_PB4_SPI1_SS1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.4 */ + +#define SYS_GPB_MFP_PB4_Msk (1UL<<4) /*!< GPB_MFP PB.4 mask */ +#define SYS_ALT_MFP_PB4_Msk (1UL<<15) /*!< ALT_MFP PB.4 mask */ +#define SYS_ALT_MFP1_PB4_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.4 mask */ + +//PB.5 +#define SYS_GPB_MFP_PB5_GPIO 0x00000000UL /*!< GPB_MFP PB.5 setting for GPIO */ +#define SYS_ALT_MFP_PB5_GPIO 0x00000000UL /*!< ALT_MFP PB.5 setting for GPIO */ +#define SYS_ALT_MFP1_PB5_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.5 */ + +#define SYS_GPB_MFP_PB5_UART1_TXD (1UL<<5) /*!< GPB_MFP PB.5 setting for UART1_TXD */ +#define SYS_ALT_MFP_PB5_UART1_TXD 0x00000000UL /*!< ALT_MFP PB.5 setting for UART1_TXD */ +#define SYS_ALT_MFP1_PB5_UART1_TXD (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.5 */ + +#define SYS_GPB_MFP_PB5_SPI2_CLK (1UL<<5) /*!< GPB_MFP PB.5 setting for SPI2_CLK */ +#define SYS_ALT_MFP_PB5_SPI2_CLK (1UL<<18) /*!< ALT_MFP PB.5 setting for SPI2_CLK */ +#define SYS_ALT_MFP1_PB5_SPI2_CLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.5 */ + +#define SYS_GPB_MFP_PB5_Msk (1UL<<5) /*!< GPB_MFP PB.5 mask */ +#define SYS_ALT_MFP_PB5_Msk (1UL<<18) /*!< ALT_MFP PB.5 mask */ +#define SYS_ALT_MFP1_PB5_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.5 mask */ + +//PB.6 +#define SYS_GPB_MFP_PB6_GPIO 0x00000000UL /*!< GPB_MFP PB.6 setting for GPIO */ +#define SYS_ALT_MFP_PB6_GPIO 0x00000000UL /*!< ALT_MFP PB.6 setting for GPIO */ +#define SYS_ALT_MFP1_PB6_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.6 */ + +#define SYS_GPB_MFP_PB6_UART1_nRTS (1UL<<6) /*!< GPB_MFP PB.6 setting for UART1_nRTS */ +#define SYS_ALT_MFP_PB6_UART1_nRTS 0x00000000UL /*!< ALT_MFP PB.6 setting for UART1_nRTS */ +#define SYS_ALT_MFP1_PB6_UART1_nRTS (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.6 */ + +#define SYS_GPB_MFP_PB6_SPI2_MOSI0 (1UL<<6) /*!< GPB_MFP PB.6 setting for SPI2_MOSI0 */ +#define SYS_ALT_MFP_PB6_SPI2_MOSI0 (1UL<<17) /*!< ALT_MFP PB.6 setting for SPI2_MOSI0 */ +#define SYS_ALT_MFP1_PB6_SPI2_MOSI0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.6 */ + +#define SYS_GPB_MFP_PB6_Msk (1UL<<6) /*!< GPB_MFP PB.6 mask */ +#define SYS_ALT_MFP_PB6_Msk (1UL<<17) /*!< ALT_MFP PB.6 mask */ +#define SYS_ALT_MFP1_PB6_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.6 mask */ + +//PB.7 +#define SYS_GPB_MFP_PB7_GPIO 0x00000000UL /*!< GPB_MFP PB.7 setting for GPIO */ +#define SYS_ALT_MFP_PB7_GPIO 0x00000000UL /*!< ALT_MFP PB.7 setting for GPIO */ +#define SYS_ALT_MFP1_PB7_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.7 */ + +#define SYS_GPB_MFP_PB7_UART1_nCTS (1UL<<7) /*!< GPB_MFP PB.7 setting for UART1_nCTS */ +#define SYS_ALT_MFP_PB7_UART1_nCTS 0x00000000UL /*!< ALT_MFP PB.7 setting for UART1_nCTS */ +#define SYS_ALT_MFP1_PB7_UART1_nCTS (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.7 */ + +#define SYS_GPB_MFP_PB7_SPI2_MISO0 (1UL<<7) /*!< GPB_MFP PB.7 setting for SPI2_MISO0 */ +#define SYS_ALT_MFP_PB7_SPI2_MISO0 (1UL<<16) /*!< ALT_MFP PB.7 setting for SPI2_MISO0 */ +#define SYS_ALT_MFP1_PB7_SPI2_MISO0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.7 */ + +#define SYS_GPB_MFP_PB7_Msk (1UL<<7) /*!< GPB_MFP PB.7 mask */ +#define SYS_ALT_MFP_PB7_Msk (1UL<<16) /*!< ALT_MFP PB.7 mask */ +#define SYS_ALT_MFP1_PB7_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.7 mask */ + +//PB.8 +#define SYS_GPB_MFP_PB8_GPIO 0x00000000UL /*!< GPB_MFP PB.8 setting for GPIO */ +#define SYS_ALT_MFP_PB8_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PB.8 */ +#define SYS_ALT_MFP1_PB8_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.8 */ + +#define SYS_GPB_MFP_PB8_TM0 (1UL<<8) /*!< GPB_MFP PB.8 setting for TM0 */ +#define SYS_ALT_MFP_PB8_TM0 (uint32_t)NULL /*!< No ALT_MFP setting for PB.8 */ +#define SYS_ALT_MFP1_PB8_TM0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.8 */ + +#define SYS_GPB_MFP_PB8_Msk (1UL<<8) /*!< GPB_MFP PB.8 mask */ +#define SYS_ALT_MFP_PB8_Msk (uint32_t)NULL /*!< No ALT_MFP PB.8 mask */ +#define SYS_ALT_MFP1_PB8_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.8 mask */ + +//PB.9 +#define SYS_GPB_MFP_PB9_GPIO 0x00000000UL /*!< GPB_MFP PB.9 setting for GPIO */ +#define SYS_ALT_MFP_PB9_GPIO 0x00000000UL /*!< ALT_MFP PB.9 setting for GPIO */ +#define SYS_ALT_MFP1_PB9_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.9 */ + +#define SYS_GPB_MFP_PB9_TM1 (1UL<<9) /*!< GPB_MFP PB.9 setting for TM1 */ +#define SYS_ALT_MFP_PB9_TM1 0x00000000UL /*!< ALT_MFP PB.9 setting for TM1 */ +#define SYS_ALT_MFP1_PB9_TM1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.9 */ + +#define SYS_GPB_MFP_PB9_SPI1_SS1 (1UL<<9) /*!< GPB_MFP PB.9 setting for SPI1_SS1 */ +#define SYS_ALT_MFP_PB9_SPI1_SS1 (1UL<<1) /*!< ALT_MFP PB.9 setting for SPI1_SS1 */ +#define SYS_ALT_MFP1_PB9_SPI1_SS1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.9 */ + +#define SYS_GPB_MFP_PB9_PWM1 0x00000000UL /*!< GPB_MFP PB.9 setting for PWM1 (NUC123xxxAEx only) */ +#define SYS_ALT_MFP_PB9_PWM1 (1UL<<1) /*!< ALT_MFP PB.9 setting for PWM1 (NUC123xxxAEx only) */ +#define SYS_ALT_MFP1_PB9_PWM1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.9 */ + +#define SYS_GPB_MFP_PB9_Msk (1UL<<9) /*!< GPB_MFP PB.9 mask */ +#define SYS_ALT_MFP_PB9_Msk (1UL<<1) /*!< ALT_MFP PB.9 mask */ +#define SYS_ALT_MFP1_PB9_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.9 mask */ + +//PB.10 +#define SYS_GPB_MFP_PB10_GPIO 0x00000000UL /*!< GPB_MFP PB.10 setting for GPIO */ +#define SYS_ALT_MFP_PB10_GPIO 0x00000000UL /*!< ALT_MFP PB.10 setting for GPIO */ +#define SYS_ALT_MFP1_PB10_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.10 */ + +#define SYS_GPB_MFP_PB10_TM2 (1UL<<10) /*!< GPB_MFP PB.10 setting for TM2 */ +#define SYS_ALT_MFP_PB10_TM2 0x00000000UL /*!< ALT_MFP PB.10 setting for TM2 */ +#define SYS_ALT_MFP1_PB10_TM2 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.10 */ + +#define SYS_GPB_MFP_PB10_SPI0_SS1 (1UL<<10) /*!< GPB_MFP PB.10 setting for SPI0_SS1 */ +#define SYS_ALT_MFP_PB10_SPI0_SS1 (1UL<<0) /*!< ALT_MFP PB.10 setting for SPI0_SS1 */ +#define SYS_ALT_MFP1_PB10_SPI0_SS1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.10 */ + +#define SYS_GPB_MFP_PB10_Msk (1UL<<10) /*!< GPB_MFP PB.10 mask */ +#define SYS_ALT_MFP_PB10_Msk (1UL<<0) /*!< ALT_MFP PB.10 mask */ +#define SYS_ALT_MFP1_PB10_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.10 mask */ + +//PB.12 +#define SYS_GPB_MFP_PB12_GPIO 0x00000000UL /*!< GPB_MFP PB.12 setting for GPIO */ +#define SYS_ALT_MFP_PB12_GPIO 0x00000000UL /*!< ALT_MFP PB.12 setting for GPIO */ +#define SYS_ALT_MFP1_PB12_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.12 */ + +#define SYS_GPB_MFP_PB12_SPI1_SS0 (1UL<<12) /*!< GPB_MFP PB.12 setting for SPI1_SS0 */ +#define SYS_ALT_MFP_PB12_SPI1_SS0 0x00000000UL /*!< ALT_MFP PB.12 setting for SPI1_SS0 */ +#define SYS_ALT_MFP1_PB12_SPI1_SS0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.12 */ + +#define SYS_GPB_MFP_PB12_CLKO (1UL<<12) /*!< GPB_MFP PB.12 setting for CLKO */ +#define SYS_ALT_MFP_PB12_CLKO (1UL<<10) /*!< ALT_MFP PB.12 setting for CLKO */ +#define SYS_ALT_MFP1_PB12_CLKO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.12 */ + +#define SYS_GPB_MFP_PB12_Msk (1UL<<12) /*!< GPB_MFP PB.12 mask */ +#define SYS_ALT_MFP_PB12_Msk (1UL<<10) /*!< ALT_MFP PB.12 mask */ +#define SYS_ALT_MFP1_PB12_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.12 mask */ + +//PB.13 +#define SYS_GPB_MFP_PB13_GPIO 0x00000000UL /*!< GPB_MFP PB.13 setting for GPIO */ +#define SYS_ALT_MFP_PB13_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PB.13 */ +#define SYS_ALT_MFP1_PB13_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.13 */ + +#define SYS_GPB_MFP_PB13_Msk (1UL<<13) /*!< GPB_MFP PB.13 mask */ +#define SYS_ALT_MFP_PB13_Msk (uint32_t)NULL /*!< No ALT_MFP PB.13 mask */ +#define SYS_ALT_MFP1_PB13_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.13 mask */ + +//PB.14 +#define SYS_GPB_MFP_PB14_GPIO 0x00000000UL /*!< GPB_MFP PB.14 setting for GPIO */ +#define SYS_ALT_MFP_PB14_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PB.14 */ +#define SYS_ALT_MFP1_PB14_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.14 */ + +#define SYS_GPB_MFP_PB14_INT0 (1UL<<14) /*!< GPB_MFP PB.14 setting for INT0 */ +#define SYS_ALT_MFP_PB14_INT0 (uint32_t)NULL /*!< No ALT_MFP setting for PB.14 */ +#define SYS_ALT_MFP1_PB14_INT0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.14 */ + +#define SYS_GPB_MFP_PB14_Msk (1UL<<14) /*!< GPB_MFP PB.14 mask */ +#define SYS_ALT_MFP_PB14_Msk (uint32_t)NULL /*!< No ALT_MFP PB.14 mask */ +#define SYS_ALT_MFP1_PB14_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.14 mask */ + +//PB.15 +#define SYS_GPB_MFP_PB15_GPIO 0x00000000UL /*!< GPB_MFP PB.15 setting for GPIO */ +#define SYS_ALT_MFP_PB15_GPIO 0x00000000UL /*!< ALT_MFP PB.15 setting for GPIO */ +#define SYS_ALT_MFP1_PB15_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.15 */ + +#define SYS_GPB_MFP_PB15_INT1 (1UL<<15) /*!< GPB_MFP PB.15 setting for INT1 */ +#define SYS_ALT_MFP_PB15_INT1 0x00000000UL /*!< ALT_MFP PB.15 setting for INT1 */ +#define SYS_ALT_MFP1_PB15_INT1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.15 */ + +#define SYS_GPB_MFP_PB15_TM0_EXT (1UL<<15) /*!< GPB_MFP PB.15 setting for TM0_EXT */ +#define SYS_ALT_MFP_PB15_TM0_EXT (1UL<<24) /*!< ALT_MFP PB.15 setting for TM0_EXT */ +#define SYS_ALT_MFP1_PB15_TM0_EXT (uint32_t)NULL /*!< No ALT_MFP1 setting for PB.15 */ + +#define SYS_GPB_MFP_PB15_Msk (1UL<<15) /*!< GPB_MFP PB.15 mask */ +#define SYS_ALT_MFP_PB15_Msk (1UL<<24) /*!< ALT_MFP PB.15 mask */ +#define SYS_ALT_MFP1_PB15_Msk (uint32_t)NULL /*!< No ALT_MFP1 PB.15 mask */ + +//PC.0 +#define SYS_GPC_MFP_PC0_GPIO 0x00000000UL /*!< GPB_MFP PC.0 setting for GPIO */ +#define SYS_ALT_MFP_PC0_GPIO 0x00000000UL /*!< ALT_MFP PC.0 setting for GPIO */ +#define SYS_ALT_MFP1_PC0_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.0 */ + +#define SYS_GPC_MFP_PC0_SPI0_SS0 (1UL<<0) /*!< GPB_MFP PC.0 setting for SPI0_SS0 */ +#define SYS_ALT_MFP_PC0_SPI0_SS0 0x00000000UL /*!< ALT_MFP PC.0 setting for SPI0_SS0 */ +#define SYS_ALT_MFP1_PC0_SPI0_SS0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.0 */ + +#define SYS_GPC_MFP_PC0_I2S_LRCLK (1UL<<0) /*!< GPB_MFP PC.0 setting for I2S_LRCLK */ +#define SYS_ALT_MFP_PC0_I2S_LRCLK (1UL<<5) /*!< ALT_MFP PC.0 setting for I2S_LRCLK */ +#define SYS_ALT_MFP1_PC0_I2S_LRCLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.0 */ + +#define SYS_GPC_MFP_PC0_Msk (1UL<<0) /*!< GPC_MFP PC.0 mask */ +#define SYS_ALT_MFP_PC0_Msk (1UL<<5) /*!< ALT_MFP PC.0 mask */ +#define SYS_ALT_MFP1_PC0_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.0 mask */ + +//PC.1 +#define SYS_GPC_MFP_PC1_GPIO 0x00000000UL /*!< GPC_MFP PC.1 setting for GPIO */ +#define SYS_ALT_MFP_PC1_GPIO 0x00000000UL /*!< ALT_MFP PC.1 setting for GPIO */ +#define SYS_ALT_MFP1_PC1_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.1 */ + +#define SYS_GPC_MFP_PC1_SPI0_CLK (1UL<<1) /*!< GPC_MFP PC.1 setting for SPI0_CLK */ +#define SYS_ALT_MFP_PC1_SPI0_CLK 0x00000000UL /*!< ALT_MFP PC.1 setting for SPI0_CLK */ +#define SYS_ALT_MFP1_PC1_SPI0_CLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.1 */ + +#define SYS_GPC_MFP_PC1_I2S_BCLK (1UL<<1) /*!< GPC_MFP PC.1 setting for I2S_BCLK */ +#define SYS_ALT_MFP_PC1_I2S_BCLK (1UL<<6) /*!< ALT_MFP PC.1 setting for I2S_BCLK */ +#define SYS_ALT_MFP1_PC1_I2S_BCLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.1 */ + +#define SYS_GPC_MFP_PC1_Msk (1UL<<1) /*!< GPC_MFP PC.1 mask */ +#define SYS_ALT_MFP_PC1_Msk (1UL<<6) /*!< ALT_MFP PC.1 mask */ +#define SYS_ALT_MFP1_PC1_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.1 mask */ + +//PC.2 +#define SYS_GPC_MFP_PC2_GPIO 0x00000000UL /*!< GPC_MFP PC.2 setting for GPIO */ +#define SYS_ALT_MFP_PC2_GPIO 0x00000000UL /*!< ALT_MFP PC.2 setting for GPIO */ +#define SYS_ALT_MFP1_PC2_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.2 */ + +#define SYS_GPC_MFP_PC2_SPI0_MISO0 (1UL<<2) /*!< GPC_MFP PC.2 setting for SPI0_MISO0 */ +#define SYS_ALT_MFP_PC2_SPI0_MISO0 0x00000000UL /*!< ALT_MFP PC.2 setting for SPI0_MISO0 */ +#define SYS_ALT_MFP1_PC2_SPI0_MISO0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.2 */ + +#define SYS_GPC_MFP_PC2_I2S_DI (1UL<<2) /*!< GPC_MFP PC.2 setting for I2S_DI */ +#define SYS_ALT_MFP_PC2_I2S_DI (1UL<<7) /*!< ALT_MFP PC.2 setting for I2S_DI */ +#define SYS_ALT_MFP1_PC2_I2S_DI (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.2 */ + +#define SYS_GPC_MFP_PC2_Msk (1UL<<2) /*!< GPC_MFP PC.2 mask */ +#define SYS_ALT_MFP_PC2_Msk (1UL<<7) /*!< ALT_MFP PC.2 mask */ +#define SYS_ALT_MFP1_PC2_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.2 mask */ + +//PC.3 +#define SYS_GPC_MFP_PC3_GPIO 0x00000000UL /*!< GPC_MFP PC.3 setting for GPIO */ +#define SYS_ALT_MFP_PC3_GPIO 0x00000000UL /*!< ALT_MFP PC.3 setting for GPIO */ +#define SYS_ALT_MFP1_PC3_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.3 */ + +#define SYS_GPC_MFP_PC3_SPI0_MOSI0 (1UL<<3) /*!< GPC_MFP PC.3 setting for SPI0_MOSI0 */ +#define SYS_ALT_MFP_PC3_SPI0_MOSI0 0x00000000UL /*!< ALT_MFP PC.3 setting for SPI0_MOSI0 */ +#define SYS_ALT_MFP1_PC3_SPI0_MOSI0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.3 */ + +#define SYS_GPC_MFP_PC3_I2S_DO (1UL<<3) /*!< GPC_MFP PC.3 setting for I2S_DO */ +#define SYS_ALT_MFP_PC3_I2S_DO (1UL<<8) /*!< ALT_MFP PC.3 setting for I2S_DO */ +#define SYS_ALT_MFP1_PC3_I2S_DO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.3 */ + +#define SYS_GPC_MFP_PC3_Msk (1UL<<3) /*!< GPC_MFP PC.3 mask */ +#define SYS_ALT_MFP_PC3_Msk (1UL<<8) /*!< ALT_MFP PC.3 mask */ +#define SYS_ALT_MFP1_PC3_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.3 mask */ + +//PC.4 +#define SYS_GPC_MFP_PC4_GPIO 0x00000000UL /*!< GPC_MFP PC.4 setting for GPIO */ +#define SYS_ALT_MFP_PC4_GPIO 0x00000000UL /*!< ALT_MFP PC.4 setting for GPIO */ +#define SYS_ALT_MFP1_PC4_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.4 */ + +#define SYS_GPC_MFP_PC4_SPI0_MISO1 (1UL<<4) /*!< GPC_MFP PC.4 setting for SPI0_MISO1 */ +#define SYS_ALT_MFP_PC4_SPI0_MISO1 0x00000000UL /*!< ALT_MFP PC.4 setting for SPI0_MISO1 */ +#define SYS_ALT_MFP1_PC4_SPI0_MISO1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.4 */ + +#define SYS_GPC_MFP_PC4_UART0_RXD (1UL<<4) /*!< GPC_MFP PC.4 setting for UART0_RXD */ +#define SYS_ALT_MFP_PC4_UART0_RXD (1UL<<29) /*!< ALT_MFP PC.4 setting for UART0_RXD */ +#define SYS_ALT_MFP1_PC4_UART0_RXD (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.4 */ + +#define SYS_GPC_MFP_PC4_Msk (1UL<<4) /*!< GPC_MFP PC.4 mask */ +#define SYS_ALT_MFP_PC4_Msk (1UL<<29) /*!< ALT_MFP PC.4 mask */ +#define SYS_ALT_MFP1_PC4_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.4 mask */ + +//PC.5 +#define SYS_GPC_MFP_PC5_GPIO 0x00000000UL /*!< GPC_MFP PC.5 setting for GPIO */ +#define SYS_ALT_MFP_PC5_GPIO 0x00000000UL /*!< ALT_MFP PC.5 setting for GPIO */ +#define SYS_ALT_MFP1_PC5_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.5 */ + +#define SYS_GPC_MFP_PC5_SPI0_MOSI1 (1UL<<5) /*!< GPC_MFP PC.5 setting for SPI0_MOSI1 */ +#define SYS_ALT_MFP_PC5_SPI0_MOSI1 0x00000000UL /*!< ALT_MFP PC.5 setting for SPI0_MOSI1 */ +#define SYS_ALT_MFP1_PC5_SPI0_MOSI1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.5 */ + +#define SYS_GPC_MFP_PC5_UART0_TXD (1UL<<5) /*!< GPC_MFP PC.5 setting for UART0_TXD */ +#define SYS_ALT_MFP_PC5_UART0_TXD (1UL<<30) /*!< ALT_MFP PC.5 setting for UART0_TXD */ +#define SYS_ALT_MFP1_PC5_UART0_TXD (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.5 */ + +#define SYS_GPC_MFP_PC5_Msk (1UL<<5) /*!< GPC_MFP PC.5 mask */ +#define SYS_ALT_MFP_PC5_Msk (1UL<<30) /*!< ALT_MFP PC.5 mask */ +#define SYS_ALT_MFP1_PC5_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.5 mask */ + +//PC.8 +#define SYS_GPC_MFP_PC8_GPIO 0x00000000UL /*!< GPC_MFP PC.8 setting for GPIO */ +#define SYS_ALT_MFP_PC8_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PC.8 */ +#define SYS_ALT_MFP1_PC8_GPIO 0x00000000UL /*!< ALT_MFP1 PC.8 setting for GPIO */ + +#define SYS_GPC_MFP_PC8_SPI1_SS0 (1UL<<8) /*!< GPC_MFP PC.8 setting for SPI1_SS0 */ +#define SYS_ALT_MFP_PC8_SPI1_SS0 (uint32_t)NULL /*!< No ALT_MFP setting for PC.8 */ +#define SYS_ALT_MFP1_PC8_SPI1_SS0 0x00000000UL /*!< ALT_MFP1 PC.8 setting for SPI1_SS0 */ + +#define SYS_GPC_MFP_PC8_PWM0 (1UL<<8) /*!< GPC_MFP PC.8 setting for PWM0 (NUC123xxxAEx only) */ +#define SYS_ALT_MFP_PC8_PWM0 (uint32_t)NULL /*!< No ALT_MFP setting for PC.8 */ +#define SYS_ALT_MFP1_PC8_PWM0 (1UL<<23) /*!< ALT_MFP1 PC.8 setting for PWM0 (NUC123xxxAEx only) */ + +#define SYS_GPC_MFP_PC8_Msk (1UL<<8) /*!< GPC_MFP PC.8 mask */ +#define SYS_ALT_MFP_PC8_Msk (uint32_t)NULL /*!< No ALT_MFP PC.8 mask */ +#define SYS_ALT_MFP1_PC8_Msk (1UL<<23) /*!< ALT_MFP1 PC.8 mask */ + +//PC.9 +#define SYS_GPC_MFP_PC9_GPIO 0x00000000UL /*!< GPC_MFP PC.9 setting for GPIO */ +#define SYS_ALT_MFP_PC9_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PC.9 */ +#define SYS_ALT_MFP1_PC9_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.9 */ + +#define SYS_GPC_MFP_PC9_SPI1_CLK (1UL<<9) /*!< GPC_MFP PC.9 setting for SPI1_CLK */ +#define SYS_ALT_MFP_PC9_SPI1_CLK (uint32_t)NULL /*!< No ALT_MFP setting for PC.9 */ +#define SYS_ALT_MFP1_PC9_SPI1_CLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.9 */ + +#define SYS_GPC_MFP_PC9_Msk (1UL<<9) /*!< GPC_MFP PC.9 mask */ +#define SYS_ALT_MFP_PC9_Msk (uint32_t)NULL /*!< No ALT_MFP PC.9 mask */ +#define SYS_ALT_MFP1_PC9_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.9 mask */ + +//PC.10 +#define SYS_GPC_MFP_PC10_GPIO 0x00000000UL /*!< GPC_MFP PC.10 setting for GPIO */ +#define SYS_ALT_MFP_PC10_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PC.10 */ +#define SYS_ALT_MFP1_PC10_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.10 */ + +#define SYS_GPC_MFP_PC10_SPI1_MISO0 (1UL<<10) /*!< GPC_MFP PC.10 setting for SPI1_MISO0 */ +#define SYS_ALT_MFP_PC10_SPI1_MISO0 (uint32_t)NULL /*!< No ALT_MFP setting for PC.10 */ +#define SYS_ALT_MFP1_PC10_SPI1_MISO0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.10 */ + +#define SYS_GPC_MFP_PC10_Msk (1UL<<10) /*!< GPC_MFP PC.10 mask */ +#define SYS_ALT_MFP_PC10_Msk (uint32_t)NULL /*!< No ALT_MFP PC.10 mask */ +#define SYS_ALT_MFP1_PC10_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.10 mask */ + +//PC.11 +#define SYS_GPC_MFP_PC11_GPIO 0x00000000UL /*!< GPC_MFP PC.11 setting for GPIO */ +#define SYS_ALT_MFP_PC11_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PC.11 */ +#define SYS_ALT_MFP1_PC11_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.11 */ + +#define SYS_GPC_MFP_PC11_SPI1_MOSI0 (1UL<<11) /*!< GPC_MFP PC.11 setting for SPI1_MOSI0 */ +#define SYS_ALT_MFP_PC11_SPI1_MOSI0 (uint32_t)NULL /*!< No ALT_MFP setting for PC.11 */ +#define SYS_ALT_MFP1_PC11_SPI1_MOSI0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.11 */ + +#define SYS_GPC_MFP_PC11_Msk (1UL<<11) /*!< GPC_MFP PC.11 mask */ +#define SYS_ALT_MFP_PC11_Msk (uint32_t)NULL /*!< No ALT_MFP PC.11 mask */ +#define SYS_ALT_MFP1_PC11_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.11 mask */ + +//PC.12 +#define SYS_GPC_MFP_PC12_GPIO 0x00000000UL /*!< GPC_MFP PC.12 setting for GPIO */ +#define SYS_ALT_MFP_PC12_GPIO 0x00000000UL /*!< ALT_MFP PC.12 setting for GPIO */ +#define SYS_ALT_MFP1_PC12_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.12 */ + +#define SYS_GPC_MFP_PC12_SPI1_MISO1 (1UL<<12) /*!< GPC_MFP PC.12 setting for SPI1_MISO1 */ +#define SYS_ALT_MFP_PC12_SPI1_MISO1 0x00000000UL /*!< ALT_MFP PC.12 setting for SPI1_MISO1 */ +#define SYS_ALT_MFP1_PC12_SPI1_MISO1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.12 */ + +#define SYS_GPC_MFP_PC12_I2S_MCLK 0x00000000UL /*!< GPC_MFP PC.12 setting for I2S_MCLK */ +#define SYS_ALT_MFP_PC12_I2S_MCLK (1UL<<20) /*!< ALT_MFP PC.12 setting for I2S_MCLK */ +#define SYS_ALT_MFP1_PC12_I2S_MCLK (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.12 */ + +#define SYS_GPC_MFP_PC12_PWM2 (1UL<<12) /*!< GPC_MFP PC.12 setting for PWM2 */ +#define SYS_ALT_MFP_PC12_PWM2 (1UL<<20) /*!< ALT_MFP PC.12 setting for PWM2 */ +#define SYS_ALT_MFP1_PC12_PWM2 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.12 */ + +#define SYS_GPC_MFP_PC12_Msk (1UL<<12) /*!< GPC_MFP PC.12 mask */ +#define SYS_ALT_MFP_PC12_Msk (1UL<<20) /*!< ALT_MFP PC.12 mask */ +#define SYS_ALT_MFP1_PC12_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.12 mask */ + +//PC.13 +#define SYS_GPC_MFP_PC13_GPIO 0x00000000UL /*!< GPC_MFP PC.13 setting for GPIO */ +#define SYS_ALT_MFP_PC13_GPIO 0x00000000UL /*!< ALT_MFP PC.13 setting for GPIO */ +#define SYS_ALT_MFP1_PC13_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.13 */ + +#define SYS_GPC_MFP_PC13_SPI1_MOSI1 (1UL<<13) /*!< GPC_MFP PC.13 setting for SPI1_MOSI1 */ +#define SYS_ALT_MFP_PC13_SPI1_MOSI1 0x00000000UL /*!< ALT_MFP PC.13 setting for SPI1_MOSI1 */ +#define SYS_ALT_MFP1_PC13_SPI1_MOSI1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.13 */ + +#define SYS_GPC_MFP_PC13_CLKO 0x00000000UL /*!< GPC_MFP PC.13 setting for CLKO */ +#define SYS_ALT_MFP_PC13_CLKO (1UL<<21) /*!< ALT_MFP PC.13 setting for CLKO */ +#define SYS_ALT_MFP1_PC13_CLKO (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.13 */ + +#define SYS_GPC_MFP_PC13_PWM3 (1UL<<13) /*!< GPC_MFP PC.13 setting for PWM3 */ +#define SYS_ALT_MFP_PC13_PWM3 (1UL<<21) /*!< ALT_MFP PC.13 setting for PWM3 */ +#define SYS_ALT_MFP1_PC13_PWM3 (uint32_t)NULL /*!< No ALT_MFP1 setting for PC.13 */ + +#define SYS_GPC_MFP_PC13_Msk (1UL<<13) /*!< GPC_MFP PC.13 mask */ +#define SYS_ALT_MFP_PC13_Msk (1UL<<21) /*!< ALT_MFP PC.13 mask */ +#define SYS_ALT_MFP1_PC13_Msk (uint32_t)NULL /*!< No ALT_MFP1 PC.13 mask */ + +//PD.0 +#define SYS_GPD_MFP_PD0_GPIO 0x00000000UL /*!< GPD_MFP PD.0 setting for GPIO */ +#define SYS_ALT_MFP_PD0_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.0 */ +#define SYS_ALT_MFP1_PD0_GPIO 0x00000000UL /*!< ALT_MFP1 PD.0 setting for GPIO */ + +#define SYS_GPD_MFP_PD0_SPI2_SS0 0x00000000UL /*!< GPD_MFP PD.0 setting for SPI2_SS0 */ +#define SYS_ALT_MFP_PD0_SPI2_SS0 (uint32_t)NULL /*!< No ALT_MFP setting for PD.0 */ +#define SYS_ALT_MFP1_PD0_SPI2_SS0 (1UL<<16) /*!< ALT_MFP1 PD.0 setting for SPI2_SS0 */ + +#define SYS_GPD_MFP_PD0_ADC0 (1UL<<0) /*!< GPD_MFP PD.0 setting for ADC0 */ +#define SYS_ALT_MFP_PD0_ADC0 (uint32_t)NULL /*!< No ALT_MFP setting for PD.0 */ +#define SYS_ALT_MFP1_PD0_ADC0 (1UL<<16) /*!< ALT_MFP1 PD.0 setting for ADC0 */ + +#define SYS_GPD_MFP_PD0_Msk (1UL<<0) /*!< GPD_MFP PD.0 mask */ +#define SYS_ALT_MFP_PD0_Msk (uint32_t)NULL /*!< No ALT_MFP PD.0 mask */ +#define SYS_ALT_MFP1_PD0_Msk (1UL<<16) /*!< ALT_MFP1 PD.0 mask */ + +//PD.1 +#define SYS_GPD_MFP_PD1_GPIO 0x00000000UL /*!< GPD_MFP PD.1 setting for GPIO */ +#define SYS_ALT_MFP_PD1_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.1 */ +#define SYS_ALT_MFP1_PD1_GPIO 0x00000000UL /*!< ALT_MFP1 PD.1 setting for GPIO */ + +#define SYS_GPD_MFP_PD1_SPI0_SS1 (1UL<<1) /*!< GPD_MFP PD.1 setting for SPI0_SS1 */ +#define SYS_ALT_MFP_PD1_SPI0_SS1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.1 */ +#define SYS_ALT_MFP1_PD1_SPI0_SS1 0x00000000UL /*!< ALT_MFP1 PD.1 setting for SPI0_SS1 */ + +#define SYS_GPD_MFP_PD1_SPI2_CLK 0x00000000UL /*!< GPD_MFP PD.1 setting for SPI2_CLK */ +#define SYS_ALT_MFP_PD1_SPI2_CLK (uint32_t)NULL /*!< No ALT_MFP setting for PD.1 */ +#define SYS_ALT_MFP1_PD1_SPI2_CLK (1UL<<17) /*!< ALT_MFP1 PD.1 setting for SPI2_CLK */ + +#define SYS_GPD_MFP_PD1_ADC1 (1UL<<1) /*!< GPD_MFP PD.1 setting for ADC1 */ +#define SYS_ALT_MFP_PD1_ADC1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.1 */ +#define SYS_ALT_MFP1_PD1_ADC1 (1UL<<17) /*!< ALT_MFP1 PD.1 setting for ADC1 */ + +#define SYS_GPD_MFP_PD1_Msk (1UL<<1) /*!< GPD_MFP PD.1 mask */ +#define SYS_ALT_MFP_PD1_Msk (uint32_t)NULL /*!< No ALT_MFP PD.1 mask */ +#define SYS_ALT_MFP1_PD1_Msk (1UL<<17) /*!< ALT_MFP1 PD.1 mask */ + +//PD.2 +#define SYS_GPD_MFP_PD2_GPIO 0x00000000UL /*!< GPD_MFP PD.2 setting for GPIO */ +#define SYS_ALT_MFP_PD2_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.2 */ +#define SYS_ALT_MFP1_PD2_GPIO 0x00000000UL /*!< ALT_MFP1 PD.2 setting for GPIO */ + +#define SYS_GPD_MFP_PD2_SPI0_MISO1 (1UL<<2) /*!< GPD_MFP PD.2 setting for SPI0_MISO1 */ +#define SYS_ALT_MFP_PD2_SPI0_MISO1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.2 */ +#define SYS_ALT_MFP1_PD2_SPI0_MISO1 0x00000000UL /*!< ALT_MFP1 PD.2 setting for SPI0_MISO1 */ + +#define SYS_GPD_MFP_PD2_SPI2_MISO0 0x00000000UL /*!< GPD_MFP PD.2 setting for SPI2_MISO0 */ +#define SYS_ALT_MFP_PD2_SPI2_MISO0 (uint32_t)NULL /*!< No ALT_MFP setting for PD.2 */ +#define SYS_ALT_MFP1_PD2_SPI2_MISO0 (1UL<<18) /*!< ALT_MFP1 PD.2 setting for SPI2_MISO0 */ + +#define SYS_GPD_MFP_PD2_ADC2 (1UL<<2) /*!< GPD_MFP PD.2 setting for ADC2 */ +#define SYS_ALT_MFP_PD2_ADC2 (uint32_t)NULL /*!< No ALT_MFP setting for PD.2 */ +#define SYS_ALT_MFP1_PD2_ADC2 (1UL<<18) /*!< ALT_MFP1 PD.2 setting for ADC2 */ + +#define SYS_GPD_MFP_PD2_Msk (1UL<<2) /*!< GPD_MFP PD.2 mask */ +#define SYS_ALT_MFP_PD2_Msk (uint32_t)NULL /*!< No ALT_MFP PD.2 mask */ +#define SYS_ALT_MFP1_PD2_Msk (1UL<<18) /*!< ALT_MFP1 PD.2 mask */ + +//PD.3 +#define SYS_GPD_MFP_PD3_GPIO 0x00000000UL /*!< GPD_MFP PD.3 setting for GPIO */ +#define SYS_ALT_MFP_PD3_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.3 */ +#define SYS_ALT_MFP1_PD3_GPIO 0x00000000UL /*!< ALT_MFP1 PD.3 setting for GPIO */ + +#define SYS_GPD_MFP_PD3_SPI0_MOSI1 (1UL<<3) /*!< GPD_MFP PD.3 setting for SPI0_MOSI1 */ +#define SYS_ALT_MFP_PD3_SPI0_MOSI1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.3 */ +#define SYS_ALT_MFP1_PD3_SPI0_MOSI1 0x00000000UL /*!< ALT_MFP1 PD.3 setting for SPI0_MOSI1 */ + +#define SYS_GPD_MFP_PD3_SPI2_MOSI0 0x00000000UL /*!< GPD_MFP PD.3 setting for SPI2_MOSI0 */ +#define SYS_ALT_MFP_PD3_SPI2_MOSI0 (uint32_t)NULL /*!< No ALT_MFP setting for PD.3 */ +#define SYS_ALT_MFP1_PD3_SPI2_MOSI0 (1UL<<19) /*!< ALT_MFP1 PD.3 setting for SPI2_MOSI0 */ + +#define SYS_GPD_MFP_PD3_ADC3 (1UL<<3) /*!< GPD_MFP PD.3 setting for ADC3 */ +#define SYS_ALT_MFP_PD3_ADC3 (uint32_t)NULL /*!< No ALT_MFP setting for PD.3 */ +#define SYS_ALT_MFP1_PD3_ADC3 (1UL<<19) /*!< ALT_MFP1 PD.3 setting for ADC3 */ + +#define SYS_GPD_MFP_PD3_Msk (1UL<<3) /*!< GPD_MFP PD.3 mask */ +#define SYS_ALT_MFP_PD3_Msk (uint32_t)NULL /*!< No ALT_MFP PD.3 mask */ +#define SYS_ALT_MFP1_PD3_Msk (1UL<<19) /*!< ALT_MFP1 PD.3 mask */ + +//PD.4 +#define SYS_GPD_MFP_PD4_GPIO 0x00000000UL /*!< GPD_MFP PD.4 setting for GPIO */ +#define SYS_ALT_MFP_PD4_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.4 */ +#define SYS_ALT_MFP1_PD4_GPIO 0x00000000UL /*!< ALT_MFP1 PD.4 setting for GPIO */ + +#define SYS_GPD_MFP_PD4_SPI2_MISO1 0x00000000UL /*!< GPD_MFP PD.4 setting for SPI2_MISO1 */ +#define SYS_ALT_MFP_PD4_SPI2_MISO1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.4 */ +#define SYS_ALT_MFP1_PD4_SPI2_MISO1 (1UL<<20) /*!< ALT_MFP1 PD.4 setting for SPI2_MISO1 */ + +#define SYS_GPD_MFP_PD4_ADC4 (1UL<<4) /*!< GPD_MFP PD.4 setting for ADC4 */ +#define SYS_ALT_MFP_PD4_ADC4 (uint32_t)NULL /*!< No ALT_MFP setting for PD.4 */ +#define SYS_ALT_MFP1_PD4_ADC4 (1UL<<20) /*!< ALT_MFP1 PD.4 setting for ADC4 */ + +#define SYS_GPD_MFP_PD4_Msk (1UL<<4) /*!< GPD_MFP PD.4 mask */ +#define SYS_ALT_MFP_PD4_Msk (uint32_t)NULL /*!< No ALT_MFP PD.4 mask */ +#define SYS_ALT_MFP1_PD4_Msk (1UL<<20) /*!< ALT_MFP1 PD.4 mask */ + +//PD.5 +#define SYS_GPD_MFP_PD5_GPIO 0x00000000UL /*!< GPD_MFP PD.5 setting for GPIO */ +#define SYS_ALT_MFP_PD5_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.5 */ +#define SYS_ALT_MFP1_PD5_GPIO 0x00000000UL /*!< ALT_MFP1 PD.5 setting for GPIO */ + +#define SYS_GPD_MFP_PD5_SPI2_MOSI1 0x00000000UL /*!< GPD_MFP PD.5 setting for SPI2_MOSI1 */ +#define SYS_ALT_MFP_PD5_SPI2_MOSI1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.5 */ +#define SYS_ALT_MFP1_PD5_SPI2_MOSI1 (1UL<<21) /*!< ALT_MFP1 PD.5 setting for SPI2_MOSI1 */ + +#define SYS_GPD_MFP_PD5_ADC5 (1UL<<5) /*!< GPD_MFP PD.5 setting for ADC5 */ +#define SYS_ALT_MFP_PD5_ADC5 (uint32_t)NULL /*!< No ALT_MFP setting for PD.5 */ +#define SYS_ALT_MFP1_PD5_ADC5 (1UL<<21) /*!< ALT_MFP1 PD.5 setting for ADC5 */ + +#define SYS_GPD_MFP_PD5_Msk (1UL<<5) /*!< GPD_MFP PD.5 mask */ +#define SYS_ALT_MFP_PD5_Msk (uint32_t)NULL /*!< No ALT_MFP PD.5 mask */ +#define SYS_ALT_MFP1_PD5_Msk (1UL<<21) /*!< ALT_MFP1 PD.5 mask */ + +//PD.8 +#define SYS_GPD_MFP_PD8_GPIO 0x00000000UL /*!< GPD_MFP PD.8 setting for GPIO */ +#define SYS_ALT_MFP_PD8_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.8 */ +#define SYS_ALT_MFP1_PD8_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.8 */ + +#define SYS_GPD_MFP_PD8_SPI1_MOSI0 (1UL<<8) /*!< GPD_MFP PD.8 setting for SPI1_MOSI0 */ +#define SYS_ALT_MFP_PD8_SPI1_MOSI0 (uint32_t)NULL /*!< No ALT_MFP setting for PD.8 */ +#define SYS_ALT_MFP1_PD8_SPI1_MOSI0 (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.8 */ + +#define SYS_GPD_MFP_PD8_Msk (1UL<<8) /*!< GPD_MFP PD.8 mask */ +#define SYS_ALT_MFP_PD8_Msk (uint32_t)NULL /*!< No ALT_MFP PD.8 mask */ +#define SYS_ALT_MFP1_PD8_Msk (uint32_t)NULL /*!< No ALT_MFP1 PD.8 mask */ + +//PD.9 +#define SYS_GPD_MFP_PD9_GPIO 0x00000000UL /*!< GPD_MFP PD.9 setting for GPIO */ +#define SYS_ALT_MFP_PD9_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.9 */ +#define SYS_ALT_MFP1_PD9_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.9 */ + +#define SYS_GPD_MFP_PD9_Msk (1UL<<9) /*!< GPD_MFP PD.9 mask */ +#define SYS_ALT_MFP_PD9_Msk (uint32_t)NULL /*!< No ALT_MFP PD.9 mask */ +#define SYS_ALT_MFP1_PD9_Msk (uint32_t)NULL /*!< No ALT_MFP1 PD.9 mask */ + +//PD.10 +#define SYS_GPD_MFP_PD10_GPIO 0x00000000UL /*!< GPD_MFP PD.10 setting for GPIO */ +#define SYS_ALT_MFP_PD10_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.10 */ +#define SYS_ALT_MFP1_PD10_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.10 */ + +#define SYS_GPD_MFP_PD10_CLKO (1UL<<10) /*!< GPD_MFP PD.10 setting for CLKO */ +#define SYS_ALT_MFP_PD10_CLKO (uint32_t)NULL /*!< No ALT_MFP setting for PD.10 */ +#define SYS_ALT_MFP1_PD10_CLKO (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.10 */ + +#define SYS_GPD_MFP_PD10_Msk (1UL<<10) /*!< GPD_MFP PD.10 mask */ +#define SYS_ALT_MFP_PD10_Msk (uint32_t)NULL /*!< No ALT_MFP PD.10 mask */ +#define SYS_ALT_MFP1_PD10_Msk (uint32_t)NULL /*!< No ALT_MFP1 PD.10 mask */ + +//PD.11 +#define SYS_GPD_MFP_PD11_GPIO 0x00000000UL /*!< GPD_MFP PD.11 setting for GPIO */ +#define SYS_ALT_MFP_PD11_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PD.11 */ +#define SYS_ALT_MFP1_PD11_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.11 */ + +#define SYS_GPD_MFP_PD11_INT1 (1UL<<11) /*!< GPD_MFP PD.11 setting for INT1 */ +#define SYS_ALT_MFP_PD11_INT1 (uint32_t)NULL /*!< No ALT_MFP setting for PD.11 */ +#define SYS_ALT_MFP1_PD11_INT1 (uint32_t)NULL /*!< No ALT_MFP1 setting for PD.11 */ + +#define SYS_GPD_MFP_PD11_Msk (1UL<<11) /*!< GPD_MFP PD.11 mask */ +#define SYS_ALT_MFP_PD11_Msk (uint32_t)NULL /*!< No ALT_MFP PD.11 mask */ +#define SYS_ALT_MFP1_PD11_Msk (uint32_t)NULL /*!< No ALT_MFP1 PD.11 mask */ + +//PF.0 +#define SYS_GPF_MFP_PF0_GPIO 0x00000000UL /*!< GPF_MFP PF.0 setting for GPIO */ +#define SYS_ALT_MFP_PF0_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PF.0 */ +#define SYS_ALT_MFP1_PF0_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PF.0 */ + +#define SYS_GPF_MFP_PF0_XT1_OUT (1UL<<0) /*!< GPF_MFP PF.0 setting for XT1_OUT */ +#define SYS_ALT_MFP_PF0_XT1_OUT (uint32_t)NULL /*!< No ALT_MFP setting for PF.0 */ +#define SYS_ALT_MFP1_PF0_XT1_OUT (uint32_t)NULL /*!< No ALT_MFP1 setting for PF.0 */ + +#define SYS_GPF_MFP_PF0_Msk (1UL<<0) /*!< GPF_MFP PF.0 mask */ +#define SYS_ALT_MFP_PF0_Msk (uint32_t)NULL /*!< No ALT_MFP PF.0 mask */ +#define SYS_ALT_MFP1_PF0_Msk (uint32_t)NULL /*!< No ALT_MFP1 PF.0 mask */ + +//PF.1 +#define SYS_GPF_MFP_PF1_GPIO 0x00000000UL /*!< GPF_MFP PF.1 setting for GPIO */ +#define SYS_ALT_MFP_PF1_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PF.1 */ +#define SYS_ALT_MFP1_PF1_GPIO (uint32_t)NULL /*!< No ALT_MFP1 setting for PF.1 */ + +#define SYS_GPF_MFP_PF1_XT1_IN (1UL<<1) /*!< GPF_MFP PF.1 setting for XT1_IN */ +#define SYS_ALT_MFP_PF1_XT1_IN (uint32_t)NULL /*!< No ALT_MFP setting for PF.1 */ +#define SYS_ALT_MFP1_PF1_XT1_IN (uint32_t)NULL /*!< No ALT_MFP1 setting for PF.1 */ + +#define SYS_GPF_MFP_PF1_Msk (1UL<<1) /*!< GPF_MFP PF.1 mask */ +#define SYS_ALT_MFP_PF1_Msk (uint32_t)NULL /*!< No ALT_MFP PF.1 mask */ +#define SYS_ALT_MFP1_PF1_Msk (uint32_t)NULL /*!< No ALT_MFP1 PF.1 mask */ + +//PF.2 +#define SYS_GPF_MFP_PF2_GPIO 0x00000000UL /*!< GPF_MFP PF.2 setting for GPIO */ +#define SYS_ALT_MFP_PF2_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PF.2 */ +#define SYS_ALT_MFP1_PF2_GPIO 0x00000000UL /*!< ALT_MFP1 PF.2 setting for GPIO */ + +#define SYS_GPF_MFP_PF2_PS2_DAT (1UL<<2) /*!< GPF_MFP PF.2 setting for PS2_DAT */ +#define SYS_ALT_MFP_PF2_PS2_DAT (uint32_t)NULL /*!< No ALT_MFP setting for PF.2 */ +#define SYS_ALT_MFP1_PF2_PS2_DAT 0x00000000UL /*!< ALT_MFP1 PF.2 setting for PS2_DAT */ + +#define SYS_GPF_MFP_PF2_I2C0_SDA (1UL<<2) /*!< GPF_MFP PF.2 setting for I2C0_SDA */ +#define SYS_ALT_MFP_PF2_I2C0_SDA (uint32_t)NULL /*!< No ALT_MFP setting for PF.2 */ +#define SYS_ALT_MFP1_PF2_I2C0_SDA (0x2UL<<24) /*!< ALT_MFP1 PF.2 setting for I2C0_SDA */ + +#define SYS_GPF_MFP_PF2_ADC6 (1UL<<2) /*!< GPF_MFP PF.2 setting for ADC6 */ +#define SYS_ALT_MFP_PF2_ADC6 (uint32_t)NULL /*!< No ALT_MFP setting for PF.2 */ +#define SYS_ALT_MFP1_PF2_ADC6 (0x3UL<<24) /*!< ALT_MFP1 PF.2 setting for ADC6 */ + +#define SYS_GPF_MFP_PF2_Msk (1UL<<2) /*!< GPF_MFP PF.2 mask */ +#define SYS_ALT_MFP_PF2_Msk (uint32_t)NULL /*!< No ALT_MFP PF.2 mask */ +#define SYS_ALT_MFP1_PF2_Msk (0x3UL<<24) /*!< ALT_MFP1 PF.2 mask */ + +//PF.3 +#define SYS_GPF_MFP_PF3_GPIO 0x00000000UL /*!< GPF_MFP PF.3 setting for GPIO */ +#define SYS_ALT_MFP_PF3_GPIO (uint32_t)NULL /*!< No ALT_MFP setting for PF.3 */ +#define SYS_ALT_MFP1_PF3_GPIO 0x00000000UL /*!< ALT_MFP1 PF.3 setting for GPIO */ + +#define SYS_GPF_MFP_PF3_PS2_CLK (1UL<<3) /*!< GPF_MFP PF.3 setting for PS2_CLK */ +#define SYS_ALT_MFP_PF3_PS2_CLK (uint32_t)NULL /*!< No ALT_MFP setting for PF.3 */ +#define SYS_ALT_MFP1_PF3_PS2_CLK 0x00000000UL /*!< ALT_MFP1 PF.3 setting for PS2_CLK */ + +#define SYS_GPF_MFP_PF3_I2C0_SCL (1UL<<3) /*!< GPF_MFP PF.3 setting for I2C0_SCL */ +#define SYS_ALT_MFP_PF3_I2C0_SCL (uint32_t)NULL /*!< No ALT_MFP setting for PF.3 */ +#define SYS_ALT_MFP1_PF3_I2C0_SCL (0x2UL<<26) /*!< ALT_MFP1 PF.3 setting for I2C0_SCL */ + +#define SYS_GPF_MFP_PF3_ADC7 (1UL<<3) /*!< GPF_MFP PF.3 setting for ADC7 */ +#define SYS_ALT_MFP_PF3_ADC7 (uint32_t)NULL /*!< No ALT_MFP setting for PF.3 */ +#define SYS_ALT_MFP1_PF3_ADC7 (0x3UL<<26) /*!< ALT_MFP1 PF.3 setting for ADC7 */ + +#define SYS_GPF_MFP_PF3_Msk (1UL<<3) /*!< GPF_MFP PF.3 mask */ +#define SYS_ALT_MFP_PF3_Msk (uint32_t)NULL /*!< No ALT_MFP PF.3 mask */ +#define SYS_ALT_MFP1_PF3_Msk (0x3UL<<26) /*!< ALT_MFP1 PF.3 mask */ + + +/*---------------------------------------------------------------------------------------------------------*/ +/* New Multi-Function constant definitions. (NUC123xxxAEx only) */ +/*---------------------------------------------------------------------------------------------------------*/ + +/* How to use below #define? +Example: If user want to set PA.10 as I2C1_SDA and PA.11 as I2C1_SCL in initial function, + user can issue following command to achieve it. + + SYS->GPA_MFPH &= ~(SYS_GPA_MFPH_GPA10_MFP_Msk | SYS_GPA_MFPH_GPA11_MFP_Msk); + SYS->GPA_MFPH |= (SYS_GPA_MFPH_GPA10_MFP_I2C1_SDA | SYS_GPA_MFPH_GPA11_MFP_I2C1_SCL); +*/ + +//GPA10_MFP +#define SYS_GPA_MFPH_GPA10_MFP_GPIO (0x0UL<BODCR |= SYS_BODCR_BOD_INTF_Msk) + +/** + * @brief Set Brown-out detector function to normal mode + * @param None + * @return None + * @details This macro set Brown-out detector to normal mode. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_CLEAR_BOD_LPM() (SYS->BODCR &= ~SYS_BODCR_BOD_LPM_Msk) + +/** + * @brief Disable Brown-out detector function + * @param None + * @return None + * @details This macro disable Brown-out detector function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_DISABLE_BOD() (SYS->BODCR &= ~SYS_BODCR_BOD_EN_Msk) + +/** + * @brief Enable Brown-out detector function + * @param None + * @return None + * @details This macro enable Brown-out detector function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_ENABLE_BOD() (SYS->BODCR |= SYS_BODCR_BOD_EN_Msk) + +/** + * @brief Get Brown-out detector interrupt flag + * @param None + * @retval 0 Brown-out detect interrupt flag is not set. + * @retval >=1 Brown-out detect interrupt flag is set. + * @details This macro get Brown-out detector interrupt flag. + */ +#define SYS_GET_BOD_INT_FLAG() (SYS->BODCR & SYS_BODCR_BOD_INTF_Msk) + +/** + * @brief Get Brown-out detector status + * @param None + * @retval 0 System voltage is higher than BOD threshold voltage setting or BOD function is disabled. + * @retval >=1 System voltage is lower than BOD threshold voltage setting. + * @details This macro get Brown-out detector output status. + * If the BOD function is disabled, this function always return 0. + */ +#define SYS_GET_BOD_OUTPUT() (SYS->BODCR & SYS_BODCR_BOD_OUT_Msk) + +/** + * @brief Enable Brown-out detector interrupt function + * @param None + * @return None + * @details This macro enable Brown-out detector interrupt function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_DISABLE_BOD_RST() (SYS->BODCR &= ~SYS_BODCR_BOD_RSTEN_Msk) + +/** + * @brief Enable Brown-out detector reset function + * @param None + * @return None + * @details This macro enable Brown-out detect reset function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_ENABLE_BOD_RST() (SYS->BODCR |= SYS_BODCR_BOD_RSTEN_Msk) + +/** + * @brief Set Brown-out detector function low power mode + * @param None + * @return None + * @details This macro set Brown-out detector to low power mode. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_SET_BOD_LPM() (SYS->BODCR |= SYS_BODCR_BOD_LPM_Msk) + +/** + * @brief Set Brown-out detector voltage level + * @param[in] u32Level is Brown-out voltage level. Including : + * - \ref SYS_BODCR_BOD_VL_4_5V + * - \ref SYS_BODCR_BOD_VL_3_8V + * - \ref SYS_BODCR_BOD_VL_2_7V + * - \ref SYS_BODCR_BOD_VL_2_2V + * @return None + * @details This macro set Brown-out detector voltage level. + * The write-protection function should be disabled before using this macro. + */ +#define SYS_SET_BOD_LEVEL(u32Level) (SYS->BODCR = (SYS->BODCR & ~SYS_BODCR_BOD_VL_Msk) | (u32Level)) + +/** + * @brief Get reset source is from Brown-out detector reset + * @param None + * @retval 0 Previous reset source is not from Brown-out detector reset + * @retval >=1 Previous reset source is from Brown-out detector reset + * @details This macro get previous reset source is from Brown-out detect reset or not. + */ +#define SYS_IS_BOD_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_BOD_Msk) + +/** + * @brief Get reset source is from CPU reset + * @param None + * @retval 0 Previous reset source is not from CPU reset + * @retval >=1 Previous reset source is from CPU reset + * @details This macro get previous reset source is from CPU reset. + */ +#define SYS_IS_CPU_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_CPU_Msk) + +/** + * @brief Get reset source is from Low-Voltage-Reset + * @param None + * @retval 0 Previous reset source is not from Low-Voltage-Reset + * @retval >=1 Previous reset source is from Low-Voltage-Reset + * @details This macro get previous reset source is from Low-Voltage-Reset. + */ +#define SYS_IS_LVR_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_LVR_Msk) + +/** + * @brief Get reset source is from Power-on Reset + * @param None + * @retval 0 Previous reset source is not from Power-on Reset + * @retval >=1 Previous reset source is from Power-on Reset + * @details This macro get previous reset source is from Power-on Reset. + */ +#define SYS_IS_POR_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_POR_Msk) + +/** + * @brief Get reset source is from reset pin reset + * @param None + * @retval 0 Previous reset source is not from reset pin reset + * @retval >=1 Previous reset source is from reset pin reset + * @details This macro get previous reset source is from reset pin reset. + */ +#define SYS_IS_RSTPIN_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_RESET_Msk) + +/** + * @brief Get reset source is from system reset + * @param None + * @retval 0 Previous reset source is not from system reset + * @retval >=1 Previous reset source is from system reset + * @details This macro get previous reset source is from system reset. + */ +#define SYS_IS_SYSTEM_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_SYS_Msk) + +/** + * @brief Get reset source is from window watch dog reset + * @param None + * @retval 0 Previous reset source is not from window watch dog reset + * @retval >=1 Previous reset source is from window watch dog reset + * @details This macro get previous reset source is from window watch dog reset. + */ +#define SYS_IS_WDT_RST() (SYS->RSTSRC & SYS_RSTSRC_RSTS_WDT_Msk) + +/** + * @brief Disable Low-Voltage-Reset function + * @param None + * @return None + * @details This macro disable Low-Voltage-Reset function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_DISABLE_LVR() (SYS->BODCR &= ~SYS_BODCR_LVR_EN_Msk) + +/** + * @brief Enable Low-Voltage-Reset function + * @param None + * @return None + * @details This macro enable Low-Voltage-Reset function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_ENABLE_LVR() (SYS->BODCR |= SYS_BODCR_LVR_EN_Msk) + +/** + * @brief Disable Power-on Reset function + * @param None + * @return None + * @details This macro disable Power-on Reset function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_DISABLE_POR() (SYS->PORCR = 0x5AA5) + +/** + * @brief Enable Power-on Reset function + * @param None + * @return None + * @details This macro enable Power-on Reset function. + * The register write-protection function should be disabled before using this macro. + */ +#define SYS_ENABLE_POR() (SYS->PORCR = 0) + +/** + * @brief Clear reset source flag + * @param[in] u32RstSrc is reset source. Including: + * - \ref SYS_RSTSRC_RSTS_CPU_Msk + * - \ref SYS_RSTSRC_RSTS_SYS_Msk + * - \ref SYS_RSTSRC_RSTS_BOD_Msk + * - \ref SYS_RSTSRC_RSTS_LVR_Msk + * - \ref SYS_RSTSRC_RSTS_WDT_Msk + * - \ref SYS_RSTSRC_RSTS_RESET_Msk + * - \ref SYS_RSTSRC_RSTS_POR_Msk + * @return None + * @details This macro clear reset source flag. + */ +#define SYS_CLEAR_RST_SOURCE(u32RstSrc) (SYS->RSTSRC = (u32RstSrc) ) + + +/** + * @brief Enable register write-protection function + * @param None + * @return None + * @details This function enable register write-protection function. + * To lock the protected register to forbid write access. + */ +static __INLINE void SYS_LockReg(void) +{ + SYS->REGWRPROT = 0; +} + +/** + * @brief Disable register write-protection function + * @param None + * @return None + * @details This function disable register write-protection function. + * To unlock the protected register to allow write access. + * + */ +static __INLINE void SYS_UnlockReg(void) +{ + while(SYS->REGWRPROT != SYS_REGWRPROT_REGPROTDIS_Msk) + { + SYS->REGWRPROT = 0x59; + SYS->REGWRPROT = 0x16; + SYS->REGWRPROT = 0x88; + } +} + + +void SYS_ClearResetSrc(uint32_t u32Src); +uint32_t SYS_GetBODStatus(void); +uint32_t SYS_GetResetSrc(void); +uint32_t SYS_IsRegLocked(void); +uint32_t SYS_ReadPDID(void); +void SYS_ResetChip(void); +void SYS_ResetCPU(void); +void SYS_ResetModule(uint32_t u32ModuleIndex); +void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel); +void SYS_DisableBOD(void); + + +/*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group SYS_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + + +#ifdef __cplusplus +} +#endif + +#endif //__SYS_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/timer.h b/NUC123/StdDriver/inc/timer.h new file mode 100644 index 0000000..ebfd3ca --- /dev/null +++ b/NUC123/StdDriver/inc/timer.h @@ -0,0 +1,394 @@ +/**************************************************************************//** + * @file timer.h + * @version V3.00 + * $Revision: 7 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series Timer driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ +#ifndef __TIMER_H__ +#define __TIMER_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup TIMER_Driver TIMER Driver + @{ +*/ + +/** @addtogroup TIMER_EXPORTED_CONSTANTS TIMER Exported Constants + @{ +*/ + +#define TIMER_ONESHOT_MODE (0UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in one-shot mode */ +#define TIMER_PERIODIC_MODE (1UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in periodic mode */ +#define TIMER_TOGGLE_MODE (2UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in toggle-output mode */ +#define TIMER_CONTINUOUS_MODE (3UL << TIMER_TCSR_MODE_Pos) /*!< Timer working in continuous counting mode */ +#define TIMER_CAPTURE_FREE_COUNTING_MODE (0UL << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< Timer capture event to get timer counter value */ +#define TIMER_CAPTURE_COUNTER_RESET_MODE (1UL << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< Timer capture event to reset timer counter */ +#define TIMER_CAPTURE_FALLING_EDGE (0UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Falling edge trigger timer capture */ +#define TIMER_CAPTURE_RISING_EDGE (1UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Rising edge trigger timer capture */ +#define TIMER_CAPTURE_FALLING_AND_RISING_EDGE (2UL << TIMER_TEXCON_TEX_EDGE_Pos) /*!< Both falling and rising edge trigger timer capture */ +#define TIMER_COUNTER_FALLING_EDGE (0UL << TIMER_TEXCON_TX_PHASE_Pos) /*!< Counter increase on falling edge */ +#define TIMER_COUNTER_RISING_EDGE (1UL << TIMER_TEXCON_TX_PHASE_Pos) /*!< Counter increase on rising edge */ + +/*@}*/ /* end of group TIMER_EXPORTED_CONSTANTS */ + + +/** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions + @{ +*/ + +/** + * @brief Set Timer Compare Value + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * @param[in] u32Value Timer compare value. Valid values are between 2 to 0xFFFFFF. + * + * @return None + * + * @details This macro is used to set new Timer compared value. + */ +#define TIMER_SET_CMP_VALUE(timer, u32Value) ((timer)->TCMPR = (u32Value)) + +/** + * @brief Set Timer Prescale Value + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * @param[in] u32Value Timer prescale value. Valid values are between 0 to 0xFF. + * + * @return None + * + * @details This macro is used to set new Timer prescale value. + * @note Clock input is divided by (prescale + 1) before it is fed into timer. + */ +#define TIMER_SET_PRESCALE_VALUE(timer, u32Value) ((timer)->TCSR = ((timer)->TCSR & ~TIMER_TCSR_PRESCALE_Msk) | (u32Value)) + +/** + * @brief Check specify Timer Status + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @retval 0 Timer 24-bit up counter is inactive + * @retval 1 Timer 24-bit up counter is active + * + * @details This macro is used to check if specify Timer channel is inactive or active. + */ +#define TIMER_IS_ACTIVE(timer) ((timer)->TCSR & TIMER_TCSR_CACT_Msk ? 1 : 0) + +/** + * @brief Start Timer Counting + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to start Timer counting. + */ +static __INLINE void TIMER_Start(TIMER_T *timer) +{ + timer->TCSR |= TIMER_TCSR_CEN_Msk; +} + +/** + * @brief Stop Timer Counting + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to stop/suspend Timer counting. + */ +static __INLINE void TIMER_Stop(TIMER_T *timer) +{ + timer->TCSR &= ~TIMER_TCSR_CEN_Msk; +} + +/** + * @brief Enable Timer Interrupt Wakeup Function + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to enable the Timer interrupt wake-up function. + * @note To wake the system from Power-down mode, timer clock source must be ether LXT or LIRC. + */ +static __INLINE void TIMER_EnableWakeup(TIMER_T *timer) +{ + timer->TCSR |= TIMER_TCSR_WAKE_EN_Msk; +} + +/** + * @brief Disable Timer Wakeup Function + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to disable the Timer interrupt wake-up function. + */ +static __INLINE void TIMER_DisableWakeup(TIMER_T *timer) +{ + timer->TCSR &= ~TIMER_TCSR_WAKE_EN_Msk; +} + +/** + * @brief Enable Capture Pin De-bounce + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to enable the capture pin detection de-bounce function. + */ +static __INLINE void TIMER_EnableCaptureDebounce(TIMER_T *timer) +{ + timer->TEXCON |= TIMER_TEXCON_TEXDB_Msk; +} + +/** + * @brief Disable Capture Pin De-bounce + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to disable the capture pin detection de-bounce function. + */ +static __INLINE void TIMER_DisableCaptureDebounce(TIMER_T *timer) +{ + timer->TEXCON &= ~TIMER_TEXCON_TEXDB_Msk; +} + +/** + * @brief Enable Counter Pin De-bounce + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to enable the counter pin detection de-bounce function. + */ +static __INLINE void TIMER_EnableEventCounterDebounce(TIMER_T *timer) +{ + timer->TEXCON |= TIMER_TEXCON_TCDB_Msk; +} + +/** + * @brief Disable Counter Pin De-bounce + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to disable the counter pin detection de-bounce function. + */ +static __INLINE void TIMER_DisableEventCounterDebounce(TIMER_T *timer) +{ + timer->TEXCON &= ~TIMER_TEXCON_TCDB_Msk; +} + +/** + * @brief Enable Timer Time-out Interrupt + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to enable the Timer time-out interrupt function. + */ +static __INLINE void TIMER_EnableInt(TIMER_T *timer) +{ + timer->TCSR |= TIMER_TCSR_IE_Msk; +} + +/** + * @brief Disable Timer Time-out Interrupt + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to disable the Timer time-out interrupt function. + */ +static __INLINE void TIMER_DisableInt(TIMER_T *timer) +{ + timer->TCSR &= ~TIMER_TCSR_IE_Msk; +} + +/** + * @brief Enable Capture Interrupt + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to enable the Timer capture trigger interrupt function. + */ +static __INLINE void TIMER_EnableCaptureInt(TIMER_T *timer) +{ + timer->TEXCON |= TIMER_TEXCON_TEXIEN_Msk; +} + +/** + * @brief Disable Capture Interrupt + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function is used to disable the Timer capture trigger interrupt function. + */ +static __INLINE void TIMER_DisableCaptureInt(TIMER_T *timer) +{ + timer->TEXCON &= ~TIMER_TEXCON_TEXIEN_Msk; +} + +/** + * @brief Get Timer Time-out Interrupt Flag + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @retval 0 Timer time-out interrupt did not occur + * @retval 1 Timer time-out interrupt occurred + * + * @details This function indicates Timer time-out interrupt occurred or not. + */ +static __INLINE uint32_t TIMER_GetIntFlag(TIMER_T *timer) +{ + return (timer->TISR & TIMER_TISR_TIF_Msk ? 1 : 0); +} + +/** + * @brief Clear Timer time-out Interrupt Flag + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function clears Timer time-out interrupt flag. + */ +static __INLINE void TIMER_ClearIntFlag(TIMER_T *timer) +{ + timer->TISR = TIMER_TISR_TIF_Msk; +} + +/** + * @brief Get Timer Capture Interrupt Flag + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @retval 0 Timer capture interrupt did not occur + * @retval 1 Timer capture interrupt occurred + * + * @details This function indicates Timer capture interrupt occurred or not. + */ +static __INLINE uint32_t TIMER_GetCaptureIntFlag(TIMER_T *timer) +{ + return timer->TEXISR; +} + +/** + * @brief Clear Timer capture Interrupt Flag + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function clears Timer capture interrupt flag. + */ +static __INLINE void TIMER_ClearCaptureIntFlag(TIMER_T *timer) +{ + timer->TEXISR = TIMER_TEXISR_TEXIF_Msk; +} + +/** + * @brief Get Timer Wakeup Flag + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @retval 0 Timer did not wake up system + * @retval 1 Timer Timer wake up system + * + * @details This function indicates Timer has waked up system or not. + */ +static __INLINE uint32_t TIMER_GetWakeupFlag(TIMER_T *timer) +{ + return (timer->TISR & TIMER_TISR_TWF_Msk ? 1 : 0); +} + +/** + * @brief Clear Timer Wake-up Flag + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This function clears the Timer wake-up system flag. + */ +static __INLINE void TIMER_ClearWakeupFlag(TIMER_T *timer) +{ + timer->TISR = TIMER_TISR_TWF_Msk; +} + +/** + * @brief Get Capture value + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return Capture Value + * + * @details This function reports the current timer capture data value. + */ +static __INLINE uint32_t TIMER_GetCaptureData(TIMER_T *timer) +{ + return timer->TCAP; +} + +/** + * @brief Get Counter value + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return Counter Value + * + * @details This function reports the current 24-bit timer counter value. + */ +static __INLINE uint32_t TIMER_GetCounter(TIMER_T *timer) +{ + return timer->TDR; +} + +uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq); +void TIMER_Close(TIMER_T *timer); +void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec); +void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge); +void TIMER_DisableCapture(TIMER_T *timer); +void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge); +void TIMER_DisableEventCounter(TIMER_T *timer); +uint32_t TIMER_GetModuleClock(TIMER_T *timer); + +/*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group TIMER_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__TIMER_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/uart.h b/NUC123/StdDriver/inc/uart.h new file mode 100644 index 0000000..ecff409 --- /dev/null +++ b/NUC123/StdDriver/inc/uart.h @@ -0,0 +1,428 @@ +/**************************************************************************//** + * @file UART.h + * @version V3.0 + * $Revision: 15 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series UART Interface Controller Driver Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __UART_H__ +#define __UART_H__ + + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup UART_Driver UART Driver + @{ +*/ + +/** @addtogroup UART_EXPORTED_CONSTANTS UART Exported Constants + @{ +*/ + +/*---------------------------------------------------------------------------------------------------------*/ +/* UART FIFO size constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ + +#define UART0_FIFO_SIZE 16 /*!< UART0 supports separated receive/transmit 16/16 bytes entry FIFO */ +#define UART1_FIFO_SIZE 16 /*!< UART1 supports separated receive/transmit 16/16 bytes entry FIFO */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* UA_FCR constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ + +#define UART_FCR_RFITL_1BYTE (0x0 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 1 byte */ +#define UART_FCR_RFITL_4BYTES (0x1 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 4 bytes */ +#define UART_FCR_RFITL_8BYTES (0x2 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 8 bytes */ +#define UART_FCR_RFITL_14BYTES (0x3 << UART_FCR_RFITL_Pos) /*!< UA_FCR setting to set RX FIFO Trigger Level to 14 bytes */ + +#define UART_FCR_RTS_TRI_LEV_1BYTE (0x0 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 1 byte */ +#define UART_FCR_RTS_TRI_LEV_4BYTES (0x1 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 4 bytes */ +#define UART_FCR_RTS_TRI_LEV_8BYTES (0x2 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 8 bytes */ +#define UART_FCR_RTS_TRI_LEV_14BYTES (0x3 << UART_FCR_RTS_TRI_LEV_Pos) /*!< UA_FCR setting to set RTS Trigger Level to 14 bytes */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* UA_LCR constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define UART_WORD_LEN_5 (0) /*!< UA_LCR setting to set UART word length to 5 bits */ +#define UART_WORD_LEN_6 (1) /*!< UA_LCR setting to set UART word length to 6 bits */ +#define UART_WORD_LEN_7 (2) /*!< UA_LCR setting to set UART word length to 7 bits */ +#define UART_WORD_LEN_8 (3) /*!< UA_LCR setting to set UART word length to 8 bits */ + +#define UART_PARITY_NONE (0x0 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to set UART as no parity */ +#define UART_PARITY_ODD (0x1 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to set UART as odd parity */ +#define UART_PARITY_EVEN (0x3 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to set UART as even parity */ +#define UART_PARITY_MARK (0x5 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to keep parity bit as '1' */ +#define UART_PARITY_SPACE (0x7 << UART_LCR_PBE_Pos) /*!< UA_LCR setting to keep parity bit as '0' */ + +#define UART_STOP_BIT_1 (0x0 << UART_LCR_NSB_Pos) /*!< UA_LCR setting for one stop bit */ +#define UART_STOP_BIT_1_5 (0x1 << UART_LCR_NSB_Pos) /*!< UA_LCR setting for 1.5 stop bit when 5-bit word length */ +#define UART_STOP_BIT_2 (0x1 << UART_LCR_NSB_Pos) /*!< UA_LCR setting for two stop bit when 6, 7, 8-bit word length */ + + +/*---------------------------------------------------------------------------------------------------------*/ +/* UART RTS LEVEL TRIGGER constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define UART_RTS_IS_LOW_LEV_ACTIVE (0x1 << UART_MCR_LEV_RTS_Pos) /*!< Set RTS is Low Level Active */ +#define UART_RTS_IS_HIGH_LEV_ACTIVE (0x0 << UART_MCR_LEV_RTS_Pos) /*!< Set RTS is High Level Active */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* UA_IRCR constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define UART_IRCR_TX_SELECT (0x1 << UART_IRCR_TX_SELECT_Pos) /*!< Set IrDA function Tx mode */ +#define UART_IRCR_RX_SELECT (0x0 << UART_IRCR_TX_SELECT_Pos) /*!< Set IrDA function Rx mode */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* UA_FUNC_SEL constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define UART_FUNC_SEL_UART (0x0 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set UART Function (Default) */ +#define UART_FUNC_SEL_IrDA (0x2 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set IrDA Function */ +#define UART_FUNC_SEL_RS485 (0x3 << UART_FUN_SEL_FUN_SEL_Pos) /*!< UA_FUNC_SEL setting to set RS485 Function */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* UART BAUDRATE MODE constants definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define UART_BAUD_MODE0 (0) /*!< Set UART Baudrate Mode is Mode0 */ +#define UART_BAUD_MODE2 (UART_BAUD_DIV_X_EN_Msk | UART_BAUD_DIV_X_ONE_Msk) /*!< Set UART Baudrate Mode is Mode2 */ + + +/*@}*/ /* end of group UART_EXPORTED_CONSTANTS */ + + +/** @addtogroup UART_EXPORTED_FUNCTIONS UART Exported Functions + @{ +*/ + + +/** + * @brief Calculate UART baudrate mode0 divider + * + * @param[in] u32SrcFreq UART clock frequency + * @param[in] u32BaudRate Baudrate of UART module + * + * @return UART baudrate mode0 divider + * + * @details This macro calculate UART baudrate mode0 divider. + */ +#define UART_BAUD_MODE0_DIVIDER(u32SrcFreq, u32BaudRate) ((((u32SrcFreq) + ((u32BaudRate)*8)) / (u32BaudRate) >> 4)-2) + +/** + * @brief Calculate UART baudrate mode2 divider + * + * @param[in] u32SrcFreq UART clock frequency + * @param[in] u32BaudRate Baudrate of UART module + * + * @return UART baudrate mode2 divider + * + * @details This macro calculate UART baudrate mode2 divider. + */ +#define UART_BAUD_MODE2_DIVIDER(u32SrcFreq, u32BaudRate) ((((u32SrcFreq) + ((u32BaudRate)/2)) / (u32BaudRate))-2) + + +/** + * @brief Write data + * + * @param[in] uart The pointer of the specified UART module + * @param[in] u8Data Data byte to transmit + * + * @return None + * + * @details This macro write Data to Tx data register. + */ +#define UART_WRITE(uart, u8Data) ((uart)->THR = (u8Data)) + +/** + * @brief Read data + * + * @param[in] uart The pointer of the specified UART module + * + * @return The oldest data byte in RX FIFO + * + * @details This macro read Rx data register. + */ +#define UART_READ(uart) ((uart)->RBR) + + +/** + * @brief Get Tx empty + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 Tx FIFO is not empty + * @retval >=1 Tx FIFO is empty + * + * @details This macro get Tx empty register value. + */ +#define UART_GET_TX_EMPTY(uart) ((uart)->FSR & UART_FSR_TX_EMPTY_Msk) + + +/** + * @brief Get Rx empty + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 Rx FIFO is not empty + * @retval >=1 Rx FIFO is empty + * + * @details This macro get Rx empty register value. + */ +#define UART_GET_RX_EMPTY(uart) ((uart)->FSR & UART_FSR_RX_EMPTY_Msk) + +/** + * @brief Check specified uart port transmission is over. + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 Transmission is not over. + * @retval 1 Transmission is over. + * + * @details This macro return if Tx FIFO is empty and specified uart port transmission is over nor not. + */ +#define UART_IS_TX_EMPTY(uart) (((uart)->FSR & UART_FSR_TE_FLAG_Msk) >> UART_FSR_TE_FLAG_Pos) + + +/** + * @brief Wait specified uart port transmission is over + * + * @param[in] uart The pointer of the specified UART module + * + * @return None + * + * @details This macro wait specified uart port transmission is over. + */ +#define UART_WAIT_TX_EMPTY(uart) while(!((((uart)->FSR) & UART_FSR_TE_FLAG_Msk) >> UART_FSR_TE_FLAG_Pos)) + +/** + * @brief Check RX is ready or not + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 The number of bytes in the RX FIFO is less than the RFITL + * @retval 1 The number of bytes in the RX FIFO equals or larger than RFITL + * + * @details This macro check receive data available interrupt flag is set or not. + */ +#define UART_IS_RX_READY(uart) (((uart)->ISR & UART_ISR_RDA_IF_Msk)>>UART_ISR_RDA_IF_Pos) + + +/** + * @brief Check TX FIFO is full or not + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 1 TX FIFO is full + * @retval 0 TX FIFO is not full + * + * @details This macro check TX FIFO is full or not. + */ +#define UART_IS_TX_FULL(uart) (((uart)->FSR & UART_FSR_TX_FULL_Msk)>>UART_FSR_TX_FULL_Pos) + +/** + * @brief Check RX FIFO is full or not + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 1 RX FIFO is full + * @retval 0 RX FIFO is not full + * + * @details This macro check RX FIFO is full or not. + */ +#define UART_IS_RX_FULL(uart) (((uart)->FSR & UART_FSR_RX_FULL_Msk)>>UART_FSR_RX_FULL_Pos) + + +/** + * @brief Get Tx full register value + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 Tx FIFO is not full. + * @retval >=1 Tx FIFO is full. + * + * @details This macro get Tx full register value. + */ +#define UART_GET_TX_FULL(uart) ((uart)->FSR & UART_FSR_TX_FULL_Msk) + + +/** + * @brief Get Rx full register value + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 Rx FIFO is not full. + * @retval >=1 Rx FIFO is full. + * + * @details This macro get Rx full register value. + */ +#define UART_GET_RX_FULL(uart) ((uart)->FSR & UART_FSR_RX_FULL_Msk) + + +/** + * @brief Enable specified UART interrupt + * + * @param[in] uart The pointer of the specified UART module + * @param[in] u32eIntSel Interrupt type select + * - UART_IER_WAKE_EN_Msk : Wakeup interrupt + * - UART_IER_BUF_ERR_IEN_Msk : Buffer Error interrupt + * - UART_IER_TOUT_IEN_Msk : Rx time-out interrupt + * - UART_IER_MODEM_IEN_Msk : Modem interrupt + * - UART_IER_RLS_IEN_Msk : Rx Line status interrupt + * - UART_IER_THRE_IEN_Msk : Tx empty interrupt + * - UART_IER_RDA_IEN_Msk : Rx ready interrupt + * + * @return None + * + * @details This macro enable specified UART interrupt. + */ +#define UART_ENABLE_INT(uart, u32eIntSel) ((uart)->IER |= (u32eIntSel)) + + +/** + * @brief Disable specified UART interrupt + * + * @param[in] uart The pointer of the specified UART module + * @param[in] u32eIntSel Interrupt type select + * - UART_IER_WAKE_EN_Msk : Wakeup interrupt + * - UART_IER_BUF_ERR_IEN_Msk : Buffer Error interrupt + * - UART_IER_TOUT_IEN_Msk : Rx time-out interrupt + * - UART_IER_MODEM_IEN_Msk : Modem interrupt + * - UART_IER_RLS_IEN_Msk : Rx Line status interrupt + * - UART_IER_THRE_IEN_Msk : Tx empty interrupt + * - UART_IER_RDA_IEN_Msk : Rx ready interrupt + * @return None + * + * @details This macro disable specified UART interrupt. + */ +#define UART_DISABLE_INT(uart, u32eIntSel) ((uart)->IER &= ~ (u32eIntSel)) + + +/** + * @brief Get specified interrupt indicator status + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32eIntTypeFlag Interrupt Type Flag, should be + * - UART_ISR_HW_BUF_ERR_INT_Msk : In DMA Mode, Buffer Error Interrupt Indicator + * - UART_ISR_HW_TOUT_INT_Msk : In DMA Mode, Rx Time-out Interrupt Indicator + * - UART_ISR_HW_MODEM_INT_Msk : In DMA Mode, MODEM Status Interrupt Indicator + * - UART_ISR_HW_RLS_INT_Msk : In DMA Mode, Rx Line Status Interrupt Indicator + * - UART_ISR_HW_BUF_ERR_IF_Msk : In DMA Mode, Buffer Error Interrupt Flag + * - UART_ISR_HW_TOUT_IF_Msk : In DMA Mode, Rx Time-out Interrupt Flag + * - UART_ISR_HW_MODEM_IF_Msk : In DMA Mode, MODEM Status Interrupt Flag + * - UART_ISR_HW_RLS_IF_Msk : In DMA Mode, Rx Line Status Interrupt Flag + * - UART_ISR_LIN_INT_Msk : LIN Bus Interrupt Indicator + * - UART_ISR_BUF_ERR_INT_Msk : Buffer Error Interrupt Indicator + * - UART_ISR_TOUT_INT_Msk : Rx Time-out Interrupt Indicator + * - UART_ISR_MODEM_INT_Msk : MODEM Status Interrupt Indicator + * - UART_ISR_RLS_INT_Msk : Rx Line Status Interrupt Indicator + * - UART_ISR_THRE_INT_Msk : Tx Empty Interrupt Indicator + * - UART_ISR_RDA_INT_Msk : Rx Ready Interrupt Indicator + * - UART_ISR_LIN_IF_Msk : LIN Bus Interrupt Flag + * - UART_ISR_BUF_ERR_IF_Msk : Buffer Error Interrupt Flag + * - UART_ISR_TOUT_IF_Msk : Rx Time-out Interrupt Flag + * - UART_ISR_MODEM_IF_Msk : MODEM Status Interrupt Flag + * - UART_ISR_RLS_IF_Msk : Rx Line Status Interrupt Flag + * - UART_ISR_THRE_IF_Msk : Tx Empty Interrupt Flag + * - UART_ISR_RDA_IF_Msk : Rx Ready Interrupt Flag + * + * @retval 0 The specified interrupt is not happened. + * @retval 1 The specified interrupt is happened. + * + * @details This macro get specified interrupt flag or interrupt indicator status. + */ +#define UART_GET_INT_FLAG(uart,u32eIntTypeFlag) (((uart)->ISR & (u32eIntTypeFlag))?1:0) + + +/** + * @brief Set RTS pin to low + * + * @param[in] uart The pointer of the specified UART module + * + * @return None + * + * @details This macro set RTS pin to low. + */ +__STATIC_INLINE void UART_CLEAR_RTS(UART_T* uart) +{ + (uart)->MCR |= UART_MCR_LEV_RTS_Msk; + (uart)->MCR &= ~UART_MCR_RTS_Msk; +} + +/** + * @brief Set RTS pin to high + * + * @param[in] uart The pointer of the specified UART module + * @return None + * + * @details This macro set RTS pin to high. + */ +__STATIC_INLINE void UART_SET_RTS(UART_T* uart) +{ + (uart)->MCR |= UART_MCR_LEV_RTS_Msk | UART_MCR_RTS_Msk; +} + + +/** + * @brief Clear RS-485 Address Byte Detection Flag + * + * @param[in] uart The pointer of the specified UART module + * + * @return None + * + * @details This macro clear RS-485 address byte detection flag. + */ +#define UART_RS485_CLEAR_ADDR_FLAG(uart) ((uart)->FSR = UART_FSR_RS485_ADD_DETF_Msk) + + +/** + * @brief Get RS-485 Address Byte Detection Flag + * + * @param[in] uart The pointer of the specified UART module + * + * @retval 0 Receiver detects a data that is not an address bit. + * @retval 1 Receiver detects a data that is an address bit. + * + * @details This macro get RS-485 address byte detection flag. + */ +#define UART_RS485_GET_ADDR_FLAG(uart) (((uart)->FSR & UART_FSR_RS485_ADD_DETF_Msk) >> UART_FSR_RS485_ADD_DETF_Pos) + + +void UART_ClearIntFlag(UART_T* uart , uint32_t u32InterruptFlag); +void UART_Close(UART_T* uart); +void UART_DisableFlowCtrl(UART_T* uart); +void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag); +void UART_EnableFlowCtrl(UART_T* uart); +void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag); +void UART_Open(UART_T* uart, uint32_t u32baudrate); +uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes); +void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits); +void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC); +void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction); +void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr); +uint32_t UART_Write(UART_T* uart, uint8_t *pu8TxBuf, uint32_t u32WriteBytes); + + +/*@}*/ /* end of group UART_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group UART_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__UART_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ + diff --git a/NUC123/StdDriver/inc/usbd.h b/NUC123/StdDriver/inc/usbd.h new file mode 100644 index 0000000..bac1e96 --- /dev/null +++ b/NUC123/StdDriver/inc/usbd.h @@ -0,0 +1,663 @@ +/**************************************************************************//** + * @file usbd.h + * @version V3.0 + * $Revision: 18 $ + * $Date: 15/09/03 11:15a $ + * @brief NUC123 series USB driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + ******************************************************************************/ + +#ifndef __USBD_H__ +#define __USBD_H__ + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup USBD_Driver USBD Driver + @{ +*/ + +/** @addtogroup USBD_EXPORTED_STRUCTS USBD Exported Structs + @{ +*/ + + +typedef struct s_usbd_info +{ + const uint8_t *gu8DevDesc; /*!< Pointer for USB Device Descriptor */ + const uint8_t *gu8ConfigDesc; /*!< Pointer for USB Configuration Descriptor */ + const uint8_t **gu8StringDesc; /*!< Pointer for USB String Descriptor pointers */ + const uint8_t **gu8HidReportDesc; /*!< Pointer for USB HID Report Descriptor */ + const uint32_t *gu32HidReportSize; /*!< Pointer for HID Report descriptor Size */ + const uint32_t *gu32ConfigHidDescIdx; /*!< Pointer for HID Descriptor start index */ + +} S_USBD_INFO_T; + +extern const S_USBD_INFO_T gsInfo; + +/*@}*/ /* end of group USBD_EXPORTED_STRUCTS */ + + + +/** @addtogroup USBD_EXPORTED_CONSTANTS USBD Exported Constants + @{ +*/ + +#define USBD_BUF_BASE (USBD_BASE+0x100) + + + +#define USBD_MAX_EP 8 + +#define EP0 0 /*!< Endpoint 0 */ +#define EP1 1 /*!< Endpoint 1 */ +#define EP2 2 /*!< Endpoint 2 */ +#define EP3 3 /*!< Endpoint 3 */ +#define EP4 4 /*!< Endpoint 4 */ +#define EP5 5 /*!< Endpoint 5 */ +#define EP6 6 /*!< Endpoint 6 */ +#define EP7 7 /*!< Endpoint 7 */ + + +/*! b, then return a. Otherwise, return b. + */ +#define Maximum(a,b) ((a)>(b) ? (a) : (b)) + + +/** + * @brief Compare two input numbers and return minimum one + * + * @param[in] a First number to be compared + * @param[in] b Second number to be compared + * + * @return Minimum value between a and b + * + * @details If a < b, then return a. Otherwise, return b. + */ +#define Minimum(a,b) ((a)<(b) ? (a) : (b)) + + +/** + * @brief Enable USB + * + * @param None + * + * @return None + * + * @details To set USB ATTR control register to enable USB and PHY. + * + */ +#define USBD_ENABLE_USB() ((uint32_t)(USBD->ATTR |= (USBD_USB_EN|USBD_PHY_EN))) + +/** + * @brief Disable USB + * + * @param None + * + * @return None + * + * @details To set USB ATTR control register to disable USB. + * + */ +#define USBD_DISABLE_USB() ((uint32_t)(USBD->ATTR &= ~USBD_USB_EN)) + +/** + * @brief Enable USB PHY + * + * @param None + * + * @return None + * + * @details To set USB ATTR control register to enable USB PHY. + * + */ +#define USBD_ENABLE_PHY() ((uint32_t)(USBD->ATTR |= USBD_PHY_EN)) + +/** + * @brief Disable USB PHY + * + * @param None + * + * @return None + * + * @details To set USB ATTR control register to disable USB PHY. + * + */ +#define USBD_DISABLE_PHY() ((uint32_t)(USBD->ATTR &= ~USBD_PHY_EN)) + +/** + * @brief Enable SE0. Force USB PHY transceiver to drive SE0. + * + * @param None + * + * @return None + * + * @details Set DRVSE0 bit of USB_DRVSE0 register to enable software-disconnect function. Force USB PHY transceiver to drive SE0 to bus. + * + */ +#define USBD_SET_SE0() ((uint32_t)(USBD->DRVSE0 |= USBD_DRVSE0)) + +/** + * @brief Disable SE0 + * + * @param None + * + * @return None + * + * @details Clear DRVSE0 bit of USB_DRVSE0 register to disable software-disconnect function. + * + */ +#define USBD_CLR_SE0() ((uint32_t)(USBD->DRVSE0 &= ~USBD_DRVSE0)) + +/** + * @brief Set USB device address + * + * @param[in] addr The USB device address. + * + * @return None + * + * @details Write USB device address to USB_FADDR register. + * + */ +#define USBD_SET_ADDR(addr) (USBD->FADDR = (addr)) + +/** + * @brief Get USB device address + * + * @param None + * + * @return USB device address + * + * @details Read USB_FADDR register to get USB device address. + * + */ +#define USBD_GET_ADDR() ((uint32_t)(USBD->FADDR)) + +/** + * @brief Enable USB interrupt function + * + * @param[in] intr The combination of the specified interrupt enable bits. + * Each bit corresponds to a interrupt enable bit. + * This parameter decides which interrupts will be enabled. + * (USBD_INT_WAKEUP, USBD_INT_FLDET, USBD_INT_USB, USBD_INT_BUS) + * + * @return None + * + * @details Enable USB related interrupt functions specified by intr parameter. + * + */ +#define USBD_ENABLE_INT(intr) (USBD->INTEN |= (intr)) + +/** + * @brief Get interrupt status + * + * @param None + * + * @return The value of USB_INTSTS register + * + * @details Return all interrupt flags of USB_INTSTS register. + * + */ +#define USBD_GET_INT_FLAG() ((uint32_t)(USBD->INTSTS)) + +/** + * @brief Clear USB interrupt flag + * + * @param[in] flag The combination of the specified interrupt flags. + * Each bit corresponds to a interrupt source. + * This parameter decides which interrupt flags will be cleared. + * (USBD_INTSTS_WAKEUP, USBD_INTSTS_FLDET, USBD_INTSTS_BUS, USBD_INTSTS_USB) + * + * @return None + * + * @details Clear USB related interrupt flags specified by flag parameter. + * + */ +#define USBD_CLR_INT_FLAG(flag) (USBD->INTSTS = (flag)) + +/** + * @brief Get endpoint status + * + * @param None + * + * @return The value of USB_EPSTS register. + * + * @details Return all endpoint status. + * + */ +#define USBD_GET_EP_FLAG() ((uint32_t)(USBD->EPSTS)) + +/** + * @brief Get USB bus state + * + * @param None + * + * @return The value of USB_ATTR[3:0]. + * Bit 0 indicates USB bus reset status. + * Bit 1 indicates USB bus suspend status. + * Bit 2 indicates USB bus resume status. + * Bit 3 indicates USB bus time-out status. + * + * @details Return USB_ATTR[3:0] for USB bus events. + * + */ +#define USBD_GET_BUS_STATE() ((uint32_t)(USBD->ATTR & 0xf)) + +/** + * @brief Check cable connection state + * + * @param None + * + * @retval 0 USB cable is not attached. + * @retval 1 USB cable is attached. + * + * @details Check the connection state by FLDET bit of USB_FLDET register. + * + */ +#define USBD_IS_ATTACHED() ((uint32_t)(USBD->FLDET & USBD_FLDET_FLDET_Msk)) + +/** + * @brief Stop USB transaction of the specified endpoint ID + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return None + * + * @details Write 1 to CLRRDY bit of USB_CFGPx register to stop USB transaction of the specified endpoint ID. + * + */ +#define USBD_STOP_TRANSACTION(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_CLRRDY_Msk) + +/** + * @brief Set USB DATA1 PID for the specified endpoint ID + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return None + * + * @details Set DSQ_SYNC bit of USB_CFGx register to specify the DATA1 PID for the following IN token transaction. + * Base on this setting, hardware will toggle PID between DATA0 and DATA1 automatically for IN token transactions. + * + */ +#define USBD_SET_DATA1(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) |= USBD_CFG_DSQ_SYNC_Msk) + +/** + * @brief Set USB DATA0 PID for the specified endpoint ID + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return None + * + * @details Clear DSQ_SYNC bit of USB_CFGx register to specify the DATA0 PID for the following IN token transaction. + * Base on this setting, hardware will toggle PID between DATA0 and DATA1 automatically for IN token transactions. + * + */ +#define USBD_SET_DATA0(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) &= (~USBD_CFG_DSQ_SYNC_Msk)) + +/** + * @brief Set USB payload size (IN data) + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @param[in] size The transfer length. + * + * @return None + * + * @details This macro will write the transfer length to USB_MXPLDx register for IN data transaction. + * + */ +#define USBD_SET_PAYLOAD_LEN(ep, size) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4))) = (size)) + +/** + * @brief Get USB payload size (OUT data) + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return The value of USB_MXPLDx register. + * + * @details Get the data length of OUT data transaction by reading USB_MXPLDx register. + * + */ +#define USBD_GET_PAYLOAD_LEN(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].MXPLD + (uint32_t)((ep) << 4)))) + +/** + * @brief Configure endpoint + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @param[in] config The USB configuration. + * + * @return None + * + * @details This macro will write config parameter to USB_CFGx register of specified endpoint ID. + * + */ +#define USBD_CONFIG_EP(ep, config) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFG + (uint32_t)((ep) << 4))) = (config)) + +/** + * @brief Set USB endpoint buffer + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @param[in] offset The SRAM offset. + * + * @return None + * + * @details This macro will set the SRAM offset for the specified endpoint ID. + * + */ +#define USBD_SET_EP_BUF_ADDR(ep, offset) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4))) = (offset)) + +/** + * @brief Get the offset of the specified USB endpoint buffer + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return The offset of the specified endpoint buffer. + * + * @details This macro will return the SRAM offset of the specified endpoint ID. + * + */ +#define USBD_GET_EP_BUF_ADDR(ep) ((uint32_t)*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].BUFSEG + (uint32_t)((ep) << 4)))) + +/** + * @brief Set USB endpoint stall state + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return None + * + * @details Set USB endpoint stall state for the specified endpoint ID. Endpoint will respond STALL token automatically. + * + */ +#define USBD_SET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) |= USBD_CFGP_SSTALL_Msk) + +/** + * @brief Clear USB endpoint stall state + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @return None + * + * @details Clear USB endpoint stall state for the specified endpoint ID. Endpoint will respond ACK/NAK token. + */ +#define USBD_CLR_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) &= ~USBD_CFGP_SSTALL_Msk) + +/** + * @brief Get USB endpoint stall state + * + * @param[in] ep The USB endpoint ID. NUC123 supports 8 hardware endpoint ID. This parameter could be 0 ~ 7. + * + * @retval 0 USB endpoint is not stalled. + * @retval Others USB endpoint is stalled. + * + * @details Get USB endpoint stall state of the specified endpoint ID. + * + */ +#define USBD_GET_EP_STALL(ep) (*((__IO uint32_t *) ((uint32_t)&USBD->EP[0].CFGP + (uint32_t)((ep) << 4))) & USBD_CFGP_SSTALL_Msk) + +/** + * @brief To support byte access between USB SRAM and system SRAM + * + * @param[in] dest Destination pointer. + * + * @param[in] src Source pointer. + * + * @param[in] size Byte count. + * + * @return None + * + * @details This function will copy the number of data specified by size and src parameters to the address specified by dest parameter. + * + */ +static __INLINE void USBD_MemCopy(uint8_t *dest, uint8_t *src, int32_t size) +{ + while(size--) *dest++ = *src++; +} + + +/** + * @brief Set USB endpoint stall state + * + * @param[in] epnum USB endpoint number + * + * @return None + * + * @details Set USB endpoint stall state. Endpoint will respond STALL token automatically. + * + */ +static __INLINE void USBD_SetStall(uint8_t epnum) +{ + uint32_t u32CfgAddr; + uint32_t u32Cfg; + int i; + + for(i = 0; i < USBD_MAX_EP; i++) + { + u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */ + u32Cfg = *((__IO uint32_t *)(u32CfgAddr)); + + if((u32Cfg & 0xf) == epnum) + { + u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */ + u32Cfg = *((__IO uint32_t *)(u32CfgAddr)); + + *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg | USBD_CFGP_SSTALL); + break; + } + } +} + +/** + * @brief Clear USB endpoint stall state + * + * @param[in] epnum USB endpoint number + * + * @return None + * + * @details Clear USB endpoint stall state. Endpoint will respond ACK/NAK token. + */ +static __INLINE void USBD_ClearStall(uint8_t epnum) +{ + uint32_t u32CfgAddr; + uint32_t u32Cfg; + int i; + + for(i = 0; i < USBD_MAX_EP; i++) + { + u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */ + u32Cfg = *((__IO uint32_t *)(u32CfgAddr)); + + if((u32Cfg & 0xf) == epnum) + { + u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */ + u32Cfg = *((__IO uint32_t *)(u32CfgAddr)); + + *((__IO uint32_t *)(u32CfgAddr)) = (u32Cfg & ~USBD_CFGP_SSTALL); + break; + } + } +} + +/** + * @brief Get USB endpoint stall state + * + * @param[in] epnum USB endpoint number + * + * @retval 0 USB endpoint is not stalled. + * @retval Others USB endpoint is stalled. + * + * @details Get USB endpoint stall state. + * + */ +static __INLINE uint32_t USBD_GetStall(uint8_t epnum) +{ + uint32_t u32CfgAddr; + uint32_t u32Cfg; + int i; + + for(i = 0; i < USBD_MAX_EP; i++) + { + u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFG; /* USBD_CFG0 */ + u32Cfg = *((__IO uint32_t *)(u32CfgAddr)); + + if((u32Cfg & 0xf) == epnum) + { + u32CfgAddr = (uint32_t)(i << 4) + (uint32_t)&USBD->EP[0].CFGP; /* USBD_CFGP0 */ + break; + } + } + + return ((*((__IO uint32_t *)(u32CfgAddr))) & USBD_CFGP_SSTALL); +} + + +extern volatile uint8_t g_usbd_RemoteWakeupEn; + +typedef void (*VENDOR_REQ)(void); /*!< Functional pointer type declaration for Vendor class */ +typedef void (*CLASS_REQ)(void); /*!< Functional pointer type declaration for USB class request callback handler */ +typedef void (*SET_INTERFACE_REQ)(void); /*!< Functional pointer type declaration for USB set interface request callback handler */ +typedef void (*SET_CONFIG_CB)(void); /*!< Functional pointer type declaration for USB set configuration request callback handler */ + +/*--------------------------------------------------------------------*/ +void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface); +void USBD_Start(void); +void USBD_GetSetupPacket(uint8_t *buf); +void USBD_ProcessSetupPacket(void); +void USBD_StandardRequest(void); +void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size); +void USBD_CtrlIn(void); +void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size); +void USBD_CtrlOut(void); +void USBD_SwReset(void); +void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq); +void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback); +void USBD_LockEpStall(uint32_t u32EpBitmap); + +/*@}*/ /* end of group USBD_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group USBD_Driver */ + +/*@}*/ /* end of group Device_Driver */ + + +#endif //__USBD_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/wdt.h b/NUC123/StdDriver/inc/wdt.h new file mode 100644 index 0000000..8825a38 --- /dev/null +++ b/NUC123/StdDriver/inc/wdt.h @@ -0,0 +1,201 @@ +/**************************************************************************//** + * @file wdt.h + * @version V3.00 + * $Revision: 3 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series WDT driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ +#ifndef __WDT_H__ +#define __WDT_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup WDT_Driver WDT Driver + @{ +*/ + +/** @addtogroup WDT_EXPORTED_CONSTANTS WDT Exported Constants + @{ +*/ +/*---------------------------------------------------------------------------------------------------------*/ +/* WTCR Constants Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define WDT_TIMEOUT_2POW4 (0UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^4 * WDT clocks */ +#define WDT_TIMEOUT_2POW6 (1UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^6 * WDT clocks */ +#define WDT_TIMEOUT_2POW8 (2UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^8 * WDT clocks */ +#define WDT_TIMEOUT_2POW10 (3UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^10 * WDT clocks */ +#define WDT_TIMEOUT_2POW12 (4UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^12 * WDT clocks */ +#define WDT_TIMEOUT_2POW14 (5UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^14 * WDT clocks */ +#define WDT_TIMEOUT_2POW16 (6UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^16 * WDT clocks */ +#define WDT_TIMEOUT_2POW18 (7UL << WDT_WTCR_WTIS_Pos) /*!< Setting WDT time-out interval to 2^18 * WDT clocks */ + +/*---------------------------------------------------------------------------------------------------------*/ +/* WTCRALT Constants Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define WDT_RESET_DELAY_1026CLK (0UL << WDT_WTCRALT_WTRDSEL_Pos) /*!< Setting WDT reset delay period to 1026 * WDT clocks */ +#define WDT_RESET_DELAY_130CLK (1UL << WDT_WTCRALT_WTRDSEL_Pos) /*!< Setting WDT reset delay period to 130 * WDT clocks */ +#define WDT_RESET_DELAY_18CLK (2UL << WDT_WTCRALT_WTRDSEL_Pos) /*!< Setting WDT reset delay period to 18 * WDT clocks */ +#define WDT_RESET_DELAY_3CLK (3UL << WDT_WTCRALT_WTRDSEL_Pos) /*!< Setting WDT reset delay period to 3 * WDT clocks */ + +/*@}*/ /* end of group WDT_EXPORTED_CONSTANTS */ + + +/** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions + @{ +*/ + +/** + * @brief Clear WDT Reset System Flag + * + * @param None + * + * @return None + * + * @details This macro clear WDT time-out reset system flag. + */ +#define WDT_CLEAR_RESET_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk)) | WDT_WTCR_WTRF_Msk) + +/** + * @brief Clear WDT Time-out Interrupt Flag + * + * @param None + * + * @return None + * + * @details This macro clear WDT time-out interrupt flag. + */ +#define WDT_CLEAR_TIMEOUT_INT_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTRF_Msk | WDT_WTCR_WTWKF_Msk)) | WDT_WTCR_WTIF_Msk) + +/** + * @brief Clear WDT Wake-up Flag + * + * @param None + * + * @return None + * + * @details This macro clear WDT time-out wake-up system flag. + */ +#define WDT_CLEAR_TIMEOUT_WAKEUP_FLAG() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTRF_Msk | WDT_WTCR_WTIF_Msk)) | WDT_WTCR_WTWKF_Msk) + +/** + * @brief Get WDT Time-out Reset Flag + * + * @param None + * + * @retval 0 WDT did not cause system reset + * @retval 1 WDT caused system reset + * + * @details This macro indicate WDT time-out to reset system or not. + */ +#define WDT_GET_RESET_FLAG() ((WDT->WTCR & WDT_WTCR_WTRF_Msk)? 1 : 0) + +/** + * @brief Get WDT Time-out Interrupt Flag + * + * @param None + * + * @retval 0 WDT time-out interrupt did not occur + * @retval 1 WDT time-out interrupt occurred + * + * @details This macro indicate WDT time-out interrupt occurred or not. + */ +#define WDT_GET_TIMEOUT_INT_FLAG() ((WDT->WTCR & WDT_WTCR_WTIF_Msk)? 1 : 0) + +/** + * @brief Get WDT Time-out Wake-up Flag + * + * @param None + * + * @retval 0 WDT did not wake up system + * @retval 1 WDT waked up system + * + * @details This macro indicate WDT time-out waked system up or not + */ +#define WDT_GET_TIMEOUT_WAKEUP_FLAG() ((WDT->WTCR & WDT_WTCR_WTWKF_Msk)? 1 : 0) + +/** + * @brief Reset WDT Counter + * + * @param None + * + * @return None + * + * @details This macro is used to reset 18-bit WDT counter. + * @note If WDT is activated and enabled to reset system, user must reset WDT counter \n + * before WDT time-out plus reset delay reached. Or WDT generate a reset signal. + */ +#define WDT_RESET_COUNTER() (WDT->WTCR = (WDT->WTCR & ~(WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk | WDT_WTCR_WTRF_Msk)) | WDT_WTCR_WTR_Msk) + +/** + * @brief Stop WDT Counting + * + * @param None + * + * @return None + * + * @details This function stops WDT counting and disable WDT module. + */ +static __INLINE void WDT_Close(void) +{ + WDT->WTCR = 0; + return; +} + +/** + * @brief Enable WDT Time-out Interrupt + * + * @param None + * + * @return None + * + * @details This function enable the WDT time-out interrupt. + */ +static __INLINE void WDT_EnableInt(void) +{ + WDT->WTCR |= WDT_WTCR_WTIE_Msk; + return; +} + +/** + * @brief Disable WDT Time-out Interrupt + * + * @param None + * + * @return None + * + * @details This function disables the WDT time-out interrupt. + */ +static __INLINE void WDT_DisableInt(void) +{ + // Do not touch write 1 clear bits + WDT->WTCR &= ~(WDT_WTCR_WTIE_Msk | WDT_WTCR_WTRF_Msk | WDT_WTCR_WTIF_Msk | WDT_WTCR_WTWKF_Msk); + return; +} + +void WDT_Open(uint32_t u32TimeoutInterval, uint32_t u32ResetDelay, uint32_t u32EnableReset, uint32_t u32EnableWakeup); + +/*@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group WDT_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__WDT_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/inc/wwdt.h b/NUC123/StdDriver/inc/wwdt.h new file mode 100644 index 0000000..48c991d --- /dev/null +++ b/NUC123/StdDriver/inc/wwdt.h @@ -0,0 +1,145 @@ +/**************************************************************************//** + * @file wwdt.h + * @version V3.00 + * $Revision: 3 $ + * $Date: 15/07/02 11:21a $ + * @brief WWDT driver header file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ +#ifndef __WWDT_H__ +#define __WWDT_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup WWDT_Driver WWDT Driver + @{ +*/ + +/** @addtogroup WWDT_EXPORTED_CONSTANTS WWDT Exported Constants + @{ +*/ +/*---------------------------------------------------------------------------------------------------------*/ +/* WWDTCR Constants Definitions */ +/*---------------------------------------------------------------------------------------------------------*/ +#define WWDT_PRESCALER_1 (0 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 1 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_2 (1 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 2 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_4 (2 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 4 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_8 (3 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 8 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_16 (4 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 16 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_32 (5 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 32 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_64 (6 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 64 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_128 (7 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 128 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_192 (8 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 192 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_256 (9 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 256 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_384 (10 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 384 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_512 (11 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 512 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_768 (12 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 768 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_1024 (13 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 1024 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_1536 (14 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 1536 * (64*WWDT_CLK) */ +#define WWDT_PRESCALER_2048 (15 << WWDT_WWDTCR_PERIODSEL_Pos) /*!< Select max time-out period to 2048 * (64*WWDT_CLK) */ + +#define WWDT_RELOAD_WORD (0x00005AA5) /*!< Fill this value to WWDTRLD register to reload WWDT counter */ + +/*@}*/ /* end of group WWDT_EXPORTED_CONSTANTS */ + + +/** @addtogroup WWDT_EXPORTED_FUNCTIONS WWDT Exported Functions + @{ +*/ + +/** + * @brief Clear WWDT Reset System Flag + * + * @param None + * + * @return None + * + * @details This macro is used to clear WWDT counter time-out reset system flag. + */ +#define WWDT_CLEAR_RESET_FLAG() (WWDT->WWDTSR = WWDT_WWDTSR_WWDTRF_Msk) + +/** + * @brief Clear WWDT Compared Match Interrupt Flag + * + * @param None + * + * @return None + * + * @details This macro is used to clear WWDT counter compare match interrupt flag. + */ +#define WWDT_CLEAR_INT_FLAG() (WWDT->WWDTSR = WWDT_WWDTSR_WWDTIF_Msk) + +/** + * @brief Get WWDT Reset Flag + * + * @param None + * + * @retval 0 WWDT did not cause system reset + * @retval 1 WWDT counter time-out caused system reset + * + * @details This macro is used to indicate WWDT counter time-out reset system flag. + */ +#define WWDT_GET_RESET_FLAG() ((WWDT->WWDTSR & WWDT_WWDTSR_WWDTRF_Msk)? 1:0) + +/** + * @brief Get WWDT Compared Match Interrupt Flag + * + * @param None + * + * @retval 0 WWDT counter compare match interrupt did not occur + * @retval 1 WWDT counter compare match interrupt occurred + * + * @details This macro is used to indicate WWDT counter compare match interrupt occurred or not. + */ +#define WWDT_GET_INT_FLAG() ((WWDT->WWDTSR & WWDT_WWDTSR_WWDTIF_Msk)? 1:0) + +/** + * @brief Get WWDT Counter value + * + * @param None + * + * @return WWDT Counter Value + * + * @details This macro to reflects the current WWDT counter value. + */ +#define WWDT_GET_COUNTER() (WWDT->WWDTCVR) + +/** + * @brief Reload WWDT Counter + * + * @param None + * + * @return None + * + * @details This macro is used to reload the WWDT counter value to 0x3F. + * @note After WWDT enabled, user must reload WWDT counter while current counter is less than compare value \n + * and larger than 0, otherwise WWDT will cause system reset immediately. + */ +#define WWDT_RELOAD_COUNTER() (WWDT->WWDTRLD = WWDT_RELOAD_WORD) + +void WWDT_Open(uint32_t u32PreScale, uint32_t u32CmpValue, uint32_t u32EnableInt); + +/*@}*/ /* end of group WWDT_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group WWDT_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +#endif //__WWDT_H__ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/adc.c b/NUC123/StdDriver/src/adc.c new file mode 100644 index 0000000..c22b16c --- /dev/null +++ b/NUC123/StdDriver/src/adc.c @@ -0,0 +1,152 @@ +/**************************************************************************//** + * @file adc.c + * @version V3.00 + * $Revision: 7 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series ADC driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup ADC_Driver ADC Driver + @{ +*/ + +/** @addtogroup ADC_EXPORTED_FUNCTIONS ADC Exported Functions + @{ +*/ + +/** + * @brief This function configures ADC module to be ready for convert the input from selected channel. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32InputMode Decides the ADC analog input mode. This parameter is not used. + * @param[in] u32OpMode Decides the ADC operation mode. Valid values are: + * - \ref ADC_ADCR_ADMD_SINGLE :Single mode. + * - \ref ADC_ADCR_ADMD_SINGLE_CYCLE :Single cycle scan mode. + * - \ref ADC_ADCR_ADMD_CONTINUOUS :Continuous scan mode. + * @param[in] u32ChMask Channel enable bit. Each bit corresponds to a input channel. Bit 0 is channel 0, bit 1 is channel 1..., bit 7 is channel 7. + * @return None + * @details Before starting A/D conversion function, ADEN(ADCR[0]) should be set to 1. + * @note NUC123 series MCU ADC can only convert 1 channel at a time. If more than 1 channels are enabled, only channel + * with smallest number will be convert. + * @note This function does not turn on ADC power nor does trigger ADC conversion. + */ +void ADC_Open(ADC_T *adc, + uint32_t u32InputMode, + uint32_t u32OpMode, + uint32_t u32ChMask) +{ + + (adc)->ADCR = ((adc)->ADCR & (~ADC_ADCR_ADMD_Msk)) | (u32OpMode); + + (adc)->ADCHER = ((adc)->ADCHER & ~ADC_ADCHER_CHEN_Msk) | (u32ChMask); +} + +/** + * @brief Disable ADC module. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details Disable A/D converter analog circuit for saving power consumption. + */ +void ADC_Close(ADC_T *adc) +{ + (adc)->ADCR &= (~ADC_ADCR_ADEN_Msk); +} + +/** + * @brief Configure the hardware trigger condition and enable hardware trigger. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32Source Decides the hardware trigger source. Valid values are: + * - \ref ADC_ADCR_TRGS_STADC :A/D conversion is started by external STADC pin. + * - \ref ADC_ADCR_TRGS_PWM :A/D conversion is started by PWM. + * @param[in] u32Param ADC trigger by external pin, this parameter is used to set trigger condition. Valid values are: + * - \ref ADC_ADCR_TRGCOND_LOW_LEVEL :STADC Low level active. + * - \ref ADC_ADCR_TRGCOND_HIGH_LEVEL :STADC High level active. + * - \ref ADC_ADCR_TRGCOND_FALLING_EDGE :STADC Falling edge active. + * - \ref ADC_ADCR_TRGCOND_RISING_EDGE :STADC Rising edge active. + * @return None + * @details Software should disable TRGEN (ADCR[8]) and ADST (ADCR[11]) before change TRGS(ADCR[5:4]). + */ +void ADC_EnableHWTrigger(ADC_T *adc, + uint32_t u32Source, + uint32_t u32Param) +{ + (adc)->ADCR &= ~(ADC_ADCR_TRGS_Msk | ADC_ADCR_TRGCOND_Msk | ADC_ADCR_TRGEN_Msk); + + (adc)->ADCR |= (u32Source) | (u32Param) | ADC_ADCR_TRGEN_Msk; +} + +/** + * @brief Disable hardware trigger ADC function. + * @param[in] adc The pointer of the specified ADC module. + * @return None + * @details Disable triggering of A/D conversion by hardware (external STADC pin or PWM Center-aligned trigger). + */ +void ADC_DisableHWTrigger(ADC_T *adc) +{ + (adc)->ADCR &= ~(ADC_ADCR_TRGS_Msk | ADC_ADCR_TRGCOND_Msk | ADC_ADCR_TRGEN_Msk); +} + +/** + * @brief Enable the interrupt(s) selected by u32Mask parameter. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32Mask The combination of interrupt status bits listed below. Each bit + * corresponds to a interrupt status. This parameter decides which + * interrupts will be enabled. + * - \ref ADC_ADF_INT :ADC convert complete interrupt. + * - \ref ADC_CMP0_INT :ADC comparator 0 interrupt. + * - \ref ADC_CMP1_INT :ADC comparator 1 interrupt. + * @return None + * @details A/D conversion end interrupt request is generated if ADIE bit (ADCR[1]) is set to 1. + * If the compare function is enabled and the compare condition matches the setting of CMPCOND (ADCMPR0/1[2]) + * and CMPMATCNT (ADCMPR0/1[11:8]), CMPF0/1 bit (ADSR[1]/[2]) will be asserted, in the meanwhile, + * if CMPIE (ADCMPR0/1[1]) is set to 1, a compare interrupt request is generated. + */ +void ADC_EnableInt(ADC_T *adc, uint32_t u32Mask) +{ + if((u32Mask) & ADC_ADF_INT) + (adc)->ADCR |= ADC_ADCR_ADIE_Msk; + if((u32Mask) & ADC_CMP0_INT) + (adc)->ADCMPR[0] |= ADC_ADCMPR_CMPIE_Msk; + if((u32Mask) & ADC_CMP1_INT) + (adc)->ADCMPR[1] |= ADC_ADCMPR_CMPIE_Msk; +} + +/** + * @brief Disable the interrupt(s) selected by u32Mask parameter. + * @param[in] adc The pointer of the specified ADC module. + * @param[in] u32Mask The combination of interrupt status bits listed below. Each bit + * corresponds to a interrupt status. This parameter decides which + * interrupts will be disabled. + * - \ref ADC_ADF_INT :ADC convert complete interrupt. + * - \ref ADC_CMP0_INT :ADC comparator 0 interrupt. + * - \ref ADC_CMP1_INT :ADC comparator 1 interrupt. + * @return None + * @details The function is used to disable convert complete interrupt, comparator 0 interrupt or comparator 1 interrupt. + */ +void ADC_DisableInt(ADC_T *adc, uint32_t u32Mask) +{ + if((u32Mask) & ADC_ADF_INT) + (adc)->ADCR &= ~ADC_ADCR_ADIE_Msk; + if((u32Mask) & ADC_CMP0_INT) + (adc)->ADCMPR[0] &= ~ADC_ADCMPR_CMPIE_Msk; + if((u32Mask) & ADC_CMP1_INT) + (adc)->ADCMPR[1] &= ~ADC_ADCMPR_CMPIE_Msk; +} + + + +/*@}*/ /* end of group ADC_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group ADC_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/clk.c b/NUC123/StdDriver/src/clk.c new file mode 100644 index 0000000..b3270b4 --- /dev/null +++ b/NUC123/StdDriver/src/clk.c @@ -0,0 +1,679 @@ +/**************************************************************************//** + * @file clk.c + * @version V3.00 + * $Revision: 26 $ + * $Date: 15/10/30 8:44a $ + * @brief NUC123 series CLK driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ + +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup CLK_Driver CLK Driver + @{ +*/ + +/** @addtogroup CLK_EXPORTED_FUNCTIONS CLK Exported Functions + @{ +*/ + +/** + * @brief Disable frequency output function + * @param None + * @return None + * @details This function disable frequency output function. + */ +void CLK_DisableCKO(void) +{ + /* Disable CKO clock source */ + CLK_DisableModuleClock(FDIV_MODULE); +} + +/** + * @brief This function enable frequency divider module clock. + * enable frequency divider clock function and configure frequency divider. + * @param[in] u32ClkSrc is frequency divider function clock source. Including : + * - \ref CLK_CLKSEL2_FRQDIV_S_HXT + * - \ref CLK_CLKSEL2_FRQDIV_S_HCLK + * - \ref CLK_CLKSEL2_FRQDIV_S_HIRC + * @param[in] u32ClkDiv is divider output frequency selection. + * @param[in] u32ClkDivBy1En is not supported. + * @return None + * + * @details Output selected clock to CKO. The output clock frequency is divided by u32ClkDiv. + * The formula is: + * CKO frequency = (Clock source frequency) / 2^(u32ClkDiv + 1) + * This function is just used to set CKO clock. + * User must enable I/O for CKO clock output pin by themselves. + */ +void CLK_EnableCKO(uint32_t u32ClkSrc, uint32_t u32ClkDiv, uint32_t u32ClkDivBy1En) +{ + /* CKO = clock source / 2^(u32ClkDiv + 1) */ + CLK->FRQDIV = (CLK_FRQDIV_DIVIDER_EN_Msk | u32ClkDiv) ; + + /* Enable CKO clock source */ + CLK_EnableModuleClock(FDIV_MODULE); + + /* Select CKO clock source */ + CLK_SetModuleClock(FDIV_MODULE, u32ClkSrc, 0); +} + +/** + * @brief Enter to Power-down mode + * @param None + * @return None + * @details This function is used to let system enter to Power-down mode. + * The register write-protection function should be disabled before using this function. + */ +void CLK_PowerDown(void) +{ + /* Set the processor uses deep sleep as its low power mode */ + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + + /* Set system Power-down enabled and Power-down entry condition */ + CLK->PWRCON |= (CLK_PWRCON_PWR_DOWN_EN_Msk | CLK_PWRCON_PD_WAIT_CPU_Msk); + + /* Chip enter Power-down mode after CPU run WFI instruction */ + __WFI(); +} + +/** + * @brief Enter to Idle mode + * @param None + * @return None + * @details This function let system enter to Idle mode. + * The register write-protection function should be disabled before using this function. + */ +void CLK_Idle(void) +{ + /* Set the processor uses sleep as its low power mode */ + SCB->SCR &= ~SCB_SCR_SLEEPDEEP_Msk; + + /* Set chip in idle mode because of WFI command */ + CLK->PWRCON &= ~CLK_PWRCON_PWR_DOWN_EN_Msk; + + /* Chip enter idle mode after CPU run WFI instruction */ + __WFI(); +} + +/** + * @brief Get external high speed crystal clock frequency + * @param None + * @return External high frequency crystal frequency + * @details This function get external high frequency crystal frequency. The frequency unit is Hz. + */ +uint32_t CLK_GetHXTFreq(void) +{ + if(CLK->PWRCON & CLK_PWRCON_XTL12M_EN_Msk) + return __HXT; + else + return 0; +} + +/** + * @brief Get HCLK frequency + * @param None + * @return HCLK frequency + * @details This function get HCLK frequency. The frequency unit is Hz. + */ +uint32_t CLK_GetHCLKFreq(void) +{ + SystemCoreClockUpdate(); + return SystemCoreClock; +} + +/** + * @brief Get PCLK frequency + * @param None + * @return PCLK frequency + * @details This function get PCLK frequency. The frequency unit is Hz. + */ +uint32_t CLK_GetPCLKFreq(void) +{ + SystemCoreClockUpdate(); + if(CLK->APBDIV & CLK_APBDIV_APBDIV_Msk) + return SystemCoreClock / 2; + else + return SystemCoreClock; +} + +/** + * @brief Get CPU frequency + * @param None + * @return CPU frequency + * @details This function get CPU frequency. The frequency unit is Hz. + */ +uint32_t CLK_GetCPUFreq(void) +{ + SystemCoreClockUpdate(); + return SystemCoreClock; +} + +/** + * @brief Set HCLK frequency + * @param[in] u32Hclk is HCLK frequency. The range of u32Hclk is 25 MHz ~ 72 MHz. + * @return HCLK frequency + * @details This function is used to set HCLK frequency. The frequency unit is Hz. + * It would configure PLL frequency to 50MHz ~ 144MHz, + * set HCLK clock divider as 2 and switch HCLK clock source to PLL. + * The register write-protection function should be disabled before using this function. + */ +uint32_t CLK_SetCoreClock(uint32_t u32Hclk) +{ + uint32_t u32HIRCSTB; + + /* Read HIRC clock source stable flag */ + u32HIRCSTB = CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk; + + /* The range of u32Hclk is 25 MHz ~ 72 MHz */ + if(u32Hclk > FREQ_72MHZ) + u32Hclk = FREQ_72MHZ; + if(u32Hclk < FREQ_25MHZ) + u32Hclk = FREQ_25MHZ; + + /* Switch HCLK clock source to HIRC clock for safe */ + CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk; + CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); + CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_Msk; + CLK->CLKDIV &= (~CLK_CLKDIV_HCLK_N_Msk); + + /* Configure PLL setting if HXT clock is stable */ + if(CLK->CLKSTATUS & CLK_CLKSTATUS_XTL12M_STB_Msk) + u32Hclk = CLK_EnablePLL(CLK_PLLCON_PLL_SRC_HXT, (u32Hclk << 1)); + + /* Configure PLL setting if HXT clock is not stable */ + else + { + u32Hclk = CLK_EnablePLL(CLK_PLLCON_PLL_SRC_HIRC, (u32Hclk << 1)); + + /* Read HIRC clock source stable flag */ + u32HIRCSTB = CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk; + } + + /* Select HCLK clock source to PLL, + Select HCLK clock source divider as 2 + and update system core clock + */ + CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(2)); + + /* Disable HIRC if HIRC is disabled before setting core clock */ + if( u32HIRCSTB == 0 ) + CLK->PWRCON &= ~CLK_PWRCON_OSC22M_EN_Msk; + + /* Return actually HCLK frequency is PLL frequency divide 2 */ + return u32Hclk >> 1; +} + +/** + * @brief Set HCLK clock source and HCLK clock divider + * @param[in] u32ClkSrc is HCLK clock source. Including : + * - \ref CLK_CLKSEL0_HCLK_S_HXT + * - \ref CLK_CLKSEL0_HCLK_S_PLL_DIV2 + * - \ref CLK_CLKSEL0_HCLK_S_PLL + * - \ref CLK_CLKSEL0_HCLK_S_LIRC + * - \ref CLK_CLKSEL0_HCLK_S_HIRC + * @param[in] u32ClkDiv is HCLK clock divider. Including : + * - \ref CLK_CLKDIV_HCLK(x) + * @return None + * @details This function set HCLK clock source and HCLK clock divider. + * The register write-protection function should be disabled before using this function. + */ +void CLK_SetHCLK(uint32_t u32ClkSrc, uint32_t u32ClkDiv) +{ + uint32_t u32HIRCSTB; + + /* Read HIRC clock source stable flag */ + u32HIRCSTB = CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk; + + /* Switch to HIRC for Safe. Avoid HCLK too high when applying new divider. */ + CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk; + CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); + CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLK_S_Msk)) | CLK_CLKSEL0_HCLK_S_HIRC; + + /* Apply new Divider */ + CLK->CLKDIV = (CLK->CLKDIV & (~CLK_CLKDIV_HCLK_N_Msk)) | u32ClkDiv; + + /* Switch HCLK to new HCLK source */ + CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLK_S_Msk)) | u32ClkSrc; + + /* Update System Core Clock */ + SystemCoreClockUpdate(); + + /* Disable HIRC if HIRC is disabled before switching HCLK source */ + if( u32HIRCSTB == 0 ) + CLK->PWRCON &= ~CLK_PWRCON_OSC22M_EN_Msk; +} + +/** + * @brief This function set selected module clock source and module clock divider + * @param[in] u32ModuleIdx is module index. + * @param[in] u32ClkSrc is module clock source. + * @param[in] u32ClkDiv is module clock divider. + * @return None + * @details Valid parameter combinations listed in following table: + * + * |Module index |Clock source |Divider | + * | :---------------- | :------------------------------------| :--------------------- | + * |\ref WDT_MODULE |\ref CLK_CLKSEL1_WDT_S_LIRC | x | + * |\ref WDT_MODULE |\ref CLK_CLKSEL1_WDT_S_HCLK_DIV2048 | x | + * |\ref ADC_MODULE |\ref CLK_CLKSEL1_ADC_S_HXT |\ref CLK_CLKDIV_ADC(x) | + * |\ref ADC_MODULE |\ref CLK_CLKSEL1_ADC_S_PLL |\ref CLK_CLKDIV_ADC(x) | + * |\ref ADC_MODULE |\ref CLK_CLKSEL1_ADC_S_HCLK |\ref CLK_CLKDIV_ADC(x) | + * |\ref ADC_MODULE |\ref CLK_CLKSEL1_ADC_S_HIRC |\ref CLK_CLKDIV_ADC(x) | + * |\ref SPI0_MODULE |\ref CLK_CLKSEL1_SPI0_S_HCLK | x | + * |\ref SPI0_MODULE |\ref CLK_CLKSEL1_SPI0_S_PLL | x | + * |\ref SPI1_MODULE |\ref CLK_CLKSEL1_SPI1_S_HCLK | x | + * |\ref SPI1_MODULE |\ref CLK_CLKSEL1_SPI1_S_PLL | x | + * |\ref SPI2_MODULE |\ref CLK_CLKSEL1_SPI2_S_HCLK | x | + * |\ref SPI2_MODULE |\ref CLK_CLKSEL1_SPI2_S_PLL | x | + * |\ref TMR0_MODULE |\ref CLK_CLKSEL1_TMR0_S_HXT | x | + * |\ref TMR0_MODULE |\ref CLK_CLKSEL1_TMR0_S_HCLK | x | + * |\ref TMR0_MODULE |\ref CLK_CLKSEL1_TMR0_S_EXT_TRG | x | + * |\ref TMR0_MODULE |\ref CLK_CLKSEL1_TMR0_S_LIRC | x | + * |\ref TMR0_MODULE |\ref CLK_CLKSEL1_TMR0_S_HIRC | x | + * |\ref TMR1_MODULE |\ref CLK_CLKSEL1_TMR1_S_HXT | x | + * |\ref TMR1_MODULE |\ref CLK_CLKSEL1_TMR1_S_HCLK | x | + * |\ref TMR1_MODULE |\ref CLK_CLKSEL1_TMR1_S_EXT_TRG | x | + * |\ref TMR1_MODULE |\ref CLK_CLKSEL1_TMR1_S_LIRC | x | + * |\ref TMR1_MODULE |\ref CLK_CLKSEL1_TMR1_S_HIRC | x | + * |\ref TMR2_MODULE |\ref CLK_CLKSEL1_TMR2_S_HXT | x | + * |\ref TMR2_MODULE |\ref CLK_CLKSEL1_TMR2_S_HCLK | x | + * |\ref TMR2_MODULE |\ref CLK_CLKSEL1_TMR2_S_EXT_TRG | x | + * |\ref TMR2_MODULE |\ref CLK_CLKSEL1_TMR2_S_LIRC | x | + * |\ref TMR2_MODULE |\ref CLK_CLKSEL1_TMR2_S_HIRC | x | + * |\ref TMR3_MODULE |\ref CLK_CLKSEL1_TMR3_S_HXT | x | + * |\ref TMR3_MODULE |\ref CLK_CLKSEL1_TMR3_S_HCLK | x | + * |\ref TMR3_MODULE |\ref CLK_CLKSEL1_TMR3_S_EXT_TRG | x | + * |\ref TMR3_MODULE |\ref CLK_CLKSEL1_TMR3_S_LIRC | x | + * |\ref TMR3_MODULE |\ref CLK_CLKSEL1_TMR3_S_HIRC | x | + * |\ref UART0_MODULE |\ref CLK_CLKSEL1_UART_S_HXT |\ref CLK_CLKDIV_UART(x) | + * |\ref UART0_MODULE |\ref CLK_CLKSEL1_UART_S_PLL |\ref CLK_CLKDIV_UART(x) | + * |\ref UART0_MODULE |\ref CLK_CLKSEL1_UART_S_HIRC |\ref CLK_CLKDIV_UART(x) | + * |\ref UART1_MODULE |\ref CLK_CLKSEL1_UART_S_HXT |\ref CLK_CLKDIV_UART(x) | + * |\ref UART1_MODULE |\ref CLK_CLKSEL1_UART_S_PLL |\ref CLK_CLKDIV_UART(x) | + * |\ref UART1_MODULE |\ref CLK_CLKSEL1_UART_S_HIRC |\ref CLK_CLKDIV_UART(x) | + * |\ref PWM01_MODULE |\ref CLK_CLKSEL12_PWM01_S_HXT | x | + * |\ref PWM01_MODULE |\ref CLK_CLKSEL12_PWM01_S_HCLK | x | + * |\ref PWM01_MODULE |\ref CLK_CLKSEL12_PWM01_S_HIRC | x | + * |\ref PWM01_MODULE |\ref CLK_CLKSEL12_PWM01_S_LIRC | x | + * |\ref PWM23_MODULE |\ref CLK_CLKSEL12_PWM23_S_HXT | x | + * |\ref PWM23_MODULE |\ref CLK_CLKSEL12_PWM23_S_HCLK | x | + * |\ref PWM23_MODULE |\ref CLK_CLKSEL12_PWM23_S_HIRC | x | + * |\ref PWM23_MODULE |\ref CLK_CLKSEL12_PWM23_S_LIRC | x | + * |\ref I2S_MODULE |\ref CLK_CLKSEL2_I2S_S_HXT | x | + * |\ref I2S_MODULE |\ref CLK_CLKSEL2_I2S_S_PLL | x | + * |\ref I2S_MODULE |\ref CLK_CLKSEL2_I2S_S_HCLK | x | + * |\ref I2S_MODULE |\ref CLK_CLKSEL2_I2S_S_HIRC | x | + * |\ref FDIV_MODULE |\ref CLK_CLKSEL2_FRQDIV_S_HXT | x | + * |\ref FDIV_MODULE |\ref CLK_CLKSEL2_FRQDIV_S_HCLK | x | + * |\ref FDIV_MODULE |\ref CLK_CLKSEL2_FRQDIV_S_HIRC | x | + * |\ref WWDT_MODULE |\ref CLK_CLKSEL2_WWDT_S_HCLK_DIV2048 | x | + * |\ref WWDT_MODULE |\ref CLK_CLKSEL2_WWDT_S_LIRC | x | + * |\ref USBD_MODULE | x |\ref CLK_CLKDIV_USB(x) | + */ + +void CLK_SetModuleClock(uint32_t u32ModuleIdx, uint32_t u32ClkSrc, uint32_t u32ClkDiv) +{ + uint32_t u32sel = 0, u32div = 0; + uint32_t u32SelTbl[3] = {0x0, 0x4, 0xC}; + + if(MODULE_CLKSEL_Msk(u32ModuleIdx) != MODULE_NoMsk) + { + /* Get clock select control register address */ + u32sel = (uint32_t)&CLK->CLKSEL0 + (u32SelTbl[MODULE_CLKSEL(u32ModuleIdx)]); + /* Set new clock selection setting */ + M32(u32sel) = (M32(u32sel) & (~(MODULE_CLKSEL_Msk(u32ModuleIdx) << MODULE_CLKSEL_Pos(u32ModuleIdx)))) | u32ClkSrc; + + /* We need to set CLKSEL2 ext control bit for PWM */ + if(u32ModuleIdx == PWM01_MODULE) + CLK->CLKSEL2 = (CLK->CLKSEL2 & (~CLK_CLKSEL2_PWM01_S_E_Msk)) | (u32ClkSrc & CLK_CLKSEL2_PWM01_S_E_Msk); + else if(u32ModuleIdx == PWM23_MODULE) + CLK->CLKSEL2 = (CLK->CLKSEL2 & (~CLK_CLKSEL2_PWM23_S_E_Msk)) | (u32ClkSrc & CLK_CLKSEL2_PWM23_S_E_Msk); + } + + if(MODULE_CLKDIV_Msk(u32ModuleIdx) != MODULE_NoMsk) + { + /* Get clock divider control register address */ + u32div = (uint32_t)&CLK->CLKDIV + ((MODULE_CLKDIV(u32ModuleIdx)) * 4); + /* Apply new divider */ + M32(u32div) = (M32(u32div) & (~(MODULE_CLKDIV_Msk(u32ModuleIdx) << MODULE_CLKDIV_Pos(u32ModuleIdx)))) | u32ClkDiv; + } +} + +/** + * @brief Set SysTick clock source + * @param[in] u32ClkSrc is module clock source. Including: + * - \ref CLK_CLKSEL0_STCLK_S_HXT + * - \ref CLK_CLKSEL0_STCLK_S_HXT_DIV2 + * - \ref CLK_CLKSEL0_STCLK_S_HCLK_DIV2 + * - \ref CLK_CLKSEL0_STCLK_S_HIRC_DIV2 + * @return None + * @details This function set SysTick clock source. + * The register write-protection function should be disabled before using this function. + */ +void CLK_SetSysTickClockSrc(uint32_t u32ClkSrc) +{ + CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLK_S_Msk) | u32ClkSrc; +} + +/** + * @brief Enable clock source + * @param[in] u32ClkMask is clock source mask. Including : + * - \ref CLK_PWRCON_XTL12M_EN_Msk + * - \ref CLK_PWRCON_OSC22M_EN_Msk + * - \ref CLK_PWRCON_OSC10K_EN_Msk + * @return None + * @details This function enable clock source. + * The register write-protection function should be disabled before using this function. + */ +void CLK_EnableXtalRC(uint32_t u32ClkMask) +{ + CLK->PWRCON |= u32ClkMask; +} + +/** + * @brief Disable clock source + * @param[in] u32ClkMask is clock source mask. Including : + * - \ref CLK_PWRCON_XTL12M_EN_Msk + * - \ref CLK_PWRCON_OSC22M_EN_Msk + * - \ref CLK_PWRCON_OSC10K_EN_Msk + * @return None + * @details This function disable clock source. + * The register write-protection function should be disabled before using this function. + */ +void CLK_DisableXtalRC(uint32_t u32ClkMask) +{ + CLK->PWRCON &= ~u32ClkMask; +} + +/** + * @brief Enable module clock + * @param[in] u32ModuleIdx is module index. Including : + * - \ref PDMA_MODULE + * - \ref ISP_MODULE + * - \ref WDT_MODULE + * - \ref TMR0_MODULE + * - \ref TMR1_MODULE + * - \ref TMR2_MODULE + * - \ref TMR3_MODULE + * - \ref FDIV_MODULE + * - \ref I2C0_MODULE + * - \ref I2C1_MODULE + * - \ref SPI0_MODULE + * - \ref SPI1_MODULE + * - \ref SPI2_MODULE + * - \ref UART0_MODULE + * - \ref UART1_MODULE + * - \ref PWM01_MODULE + * - \ref PWM23_MODULE + * - \ref USBD_MODULE + * - \ref ADC_MODULE + * - \ref I2S_MODULE + * - \ref PS2_MODULE + * @return None + * @details This function enable module clock. + */ +void CLK_EnableModuleClock(uint32_t u32ModuleIdx) +{ + *(volatile uint32_t *)((uint32_t)&CLK->AHBCLK + ((MODULE_APBCLK(u32ModuleIdx)) * 4)) |= 1 << MODULE_IP_EN_Pos(u32ModuleIdx); +} + +/** + * @brief Disable module clock + * @param[in] u32ModuleIdx is module index. Including : + * - \ref PDMA_MODULE + * - \ref ISP_MODULE + * - \ref WDT_MODULE + * - \ref TMR0_MODULE + * - \ref TMR1_MODULE + * - \ref TMR2_MODULE + * - \ref TMR3_MODULE + * - \ref FDIV_MODULE + * - \ref I2C0_MODULE + * - \ref I2C1_MODULE + * - \ref SPI0_MODULE + * - \ref SPI1_MODULE + * - \ref SPI2_MODULE + * - \ref UART0_MODULE + * - \ref UART1_MODULE + * - \ref PWM01_MODULE + * - \ref PWM23_MODULE + * - \ref USBD_MODULE + * - \ref ADC_MODULE + * - \ref I2S_MODULE + * - \ref PS2_MODULE + * @return None + * @details This function disable module clock. + */ +void CLK_DisableModuleClock(uint32_t u32ModuleIdx) +{ + *(volatile uint32_t *)((uint32_t)&CLK->AHBCLK + ((MODULE_APBCLK(u32ModuleIdx)) * 4)) &= ~(1 << MODULE_IP_EN_Pos(u32ModuleIdx)); +} + + +/** + * @brief Set PLL frequency + * @param[in] u32PllClkSrc is PLL clock source. Including : + * - \ref CLK_PLLCON_PLL_SRC_HXT + * - \ref CLK_PLLCON_PLL_SRC_HIRC + * @param[in] u32PllFreq is PLL frequency + * @return PLL frequency + * @details This function is used to configure PLLCON register to set specified PLL frequency. + * The register write-protection function should be disabled before using this function. + */ +uint32_t CLK_EnablePLL(uint32_t u32PllClkSrc, uint32_t u32PllFreq) +{ + uint32_t u32PllSrcClk, u32NR, u32NF, u32NO, u32CLK_SRC; + uint32_t u32Tmp, u32Tmp2, u32Tmp3, u32Min, u32MinNF, u32MinNR; + + /* Disable PLL first to avoid unstable when setting PLL. */ + CLK->PLLCON = CLK_PLLCON_PD_Msk; + + /* PLL source clock is from HXT */ + if(u32PllClkSrc == CLK_PLLCON_PLL_SRC_HXT) + { + /* Enable HXT clock */ + CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk; + + /* Wait for HXT clock ready */ + CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); + + /* Select PLL source clock from HXT */ + u32CLK_SRC = CLK_PLLCON_PLL_SRC_HXT; + u32PllSrcClk = __HXT; + + /* u32NR start from 2 */ + u32NR = 2; + } + + /* PLL source clock is from HIRC */ + else + { + /* Enable HIRC clock */ + CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk; + + /* Wait for HIRC clock ready */ + CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); + + /* Select PLL source clock from HIRC */ + u32CLK_SRC = CLK_PLLCON_PLL_SRC_HIRC; + u32PllSrcClk = __HIRC; + + /* u32NR start from 4 when FIN = 22.1184MHz to avoid calculation overflow */ + u32NR = 4; + } + + /* Select "NO" according to request frequency */ + if((u32PllFreq <= FREQ_200MHZ) && (u32PllFreq > FREQ_100MHZ)) + { + u32NO = 0; + } + else if((u32PllFreq <= FREQ_100MHZ) && (u32PllFreq > FREQ_50MHZ)) + { + u32NO = 1; + u32PllFreq = u32PllFreq << 1; + } + else if((u32PllFreq <= FREQ_50MHZ) && (u32PllFreq >= FREQ_25MHZ)) + { + u32NO = 3; + u32PllFreq = u32PllFreq << 2; + } + else + { + /* Wrong frequency request. Just return default setting. */ + goto lexit; + } + + /* Find best solution */ + u32Min = (uint32_t) - 1; + u32MinNR = 0; + u32MinNF = 0; + for(; u32NR <= 33; u32NR++) + { + u32Tmp = u32PllSrcClk / u32NR; + if((u32Tmp > 1600000) && (u32Tmp < 16000000)) + { + for(u32NF = 2; u32NF <= 513; u32NF++) + { + u32Tmp2 = u32Tmp * u32NF; + if((u32Tmp2 >= 100000000) && (u32Tmp2 <= 200000000)) + { + u32Tmp3 = (u32Tmp2 > u32PllFreq) ? u32Tmp2 - u32PllFreq : u32PllFreq - u32Tmp2; + if(u32Tmp3 < u32Min) + { + u32Min = u32Tmp3; + u32MinNR = u32NR; + u32MinNF = u32NF; + + /* Break when get good results */ + if(u32Min == 0) + break; + } + } + } + } + } + + /* Enable and apply new PLL setting. */ + CLK->PLLCON = u32CLK_SRC | (u32NO << 14) | ((u32MinNR - 2) << 9) | (u32MinNF - 2); + + /* Waiting for PLL clock stable */ + CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk); + + /* Return actual PLL output clock frequency */ + return u32PllSrcClk / ((u32NO + 1) * u32MinNR) * u32MinNF; + +lexit: + + /* Apply default PLL setting and return */ + if(u32PllClkSrc == CLK_PLLCON_PLL_SRC_HXT) + CLK->PLLCON = 0xC22E; /* 48MHz */ + else + CLK->PLLCON = 0x8D66F; /* 48.06498462MHz */ + + CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk); + return CLK_GetPLLClockFreq(); + + +} + + +/** + * @brief Disable PLL + * @param None + * @return None + * @details This function disable PLL. + */ +void CLK_DisablePLL(void) +{ + CLK->PLLCON |= CLK_PLLCON_PD_Msk; +} + +/** + * @brief This function check selected clock source status + * @param[in] u32ClkMask is selected clock source. Including : + * - \ref CLK_CLKSTATUS_XTL12M_STB_Msk + * - \ref CLK_CLKSTATUS_OSC22M_STB_Msk + * - \ref CLK_CLKSTATUS_OSC10K_STB_Msk + * - \ref CLK_CLKSTATUS_PLL_STB_Msk + * + * @retval 0 clock is not stable + * @retval 1 clock is stable + * + * @details To wait for clock ready by specified CLKSTATUS bit or timeout (~300ms) + */ +uint32_t CLK_WaitClockReady(uint32_t u32ClkMask) +{ + int32_t i32TimeOutCnt = 2160000; + + while((CLK->CLKSTATUS & u32ClkMask) != u32ClkMask) + { + if(i32TimeOutCnt-- <= 0) + return 0; + } + + return 1; +} + +/** + * @brief Enable System Tick counter + * @param[in] u32ClkSrc is System Tick clock source. Including: + * - \ref CLK_CLKSEL0_STCLK_S_HXT + * - \ref CLK_CLKSEL0_STCLK_S_HXT_DIV2 + * - \ref CLK_CLKSEL0_STCLK_S_HCLK_DIV2 + * - \ref CLK_CLKSEL0_STCLK_S_HIRC_DIV2 + * - \ref CLK_CLKSEL0_STCLK_S_HCLK + * @param[in] u32Count is System Tick reload value. It could be 0~0xFFFFFF. + * @return None + * @details This function set System Tick clock source, reload value, enable System Tick counter and interrupt. + * The register write-protection function should be disabled before using this function. + */ +void CLK_EnableSysTick(uint32_t u32ClkSrc, uint32_t u32Count) +{ + /* Set System Tick counter disabled */ + SysTick->CTRL = 0; + + /* Set System Tick clock source */ + if( u32ClkSrc == CLK_CLKSEL0_STCLK_S_HCLK ) + SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk; + else + CLK->CLKSEL0 = (CLK->CLKSEL0 & ~CLK_CLKSEL0_STCLK_S_Msk) | u32ClkSrc; + + /* Set System Tick reload value */ + SysTick->LOAD = u32Count; + + /* Clear System Tick current value and counter flag */ + SysTick->VAL = 0; + + /* Set System Tick interrupt enabled and counter enabled */ + SysTick->CTRL |= SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk; +} + +/** + * @brief Disable System Tick counter + * @param None + * @return None + * @details This function disable System Tick counter. + */ +void CLK_DisableSysTick(void) +{ + /* Set System Tick counter disabled */ + SysTick->CTRL = 0; +} + + + +/*@}*/ /* end of group CLK_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group CLK_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/crc.c b/NUC123/StdDriver/src/crc.c new file mode 100644 index 0000000..deac8cd --- /dev/null +++ b/NUC123/StdDriver/src/crc.c @@ -0,0 +1,113 @@ +/**************************************************************************//** + * @file crc.c + * @version V3.00 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series CRC driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup CRC_Driver CRC Driver + @{ +*/ + +/** @addtogroup CRC_EXPORTED_FUNCTIONS CRC Exported Functions + @{ +*/ + +/** + * @brief CRC Open + * + * @param[in] u32Mode CRC operation polynomial mode. Valid values are: + * - \ref CRC_CCITT + * - \ref CRC_8 + * - \ref CRC_16 + * - \ref CRC_32 + * @param[in] u32Attribute CRC operation data attribute. Valid values are combined with: + * - \ref CRC_CHECKSUM_COM + * - \ref CRC_CHECKSUM_RVS + * - \ref CRC_WDATA_COM + * - \ref CRC_WDATA_RVS + * @param[in] u32Seed Seed value. + * @param[in] u32DataLen CPU Write Data Length. Valid values are: + * - \ref CRC_CPU_WDATA_8 + * - \ref CRC_CPU_WDATA_16 + * - \ref CRC_CPU_WDATA_32 + * + * @return None + * + * @details This function enable the CRC channel by specify CRC polynomial mode, data attribute, initial seed and write data length. + */ +void CRC_Open(uint32_t u32Mode, uint32_t u32Attribute, uint32_t u32Seed, uint32_t u32DataLen) +{ + /* Enable CRC channel clock */ + PDMA_GCR->GCRCSR |= PDMA_GCRCSR_CRC_CLK_EN_Msk; + + CRC->SEED = u32Seed; + CRC->CTL = u32Mode | u32Attribute | u32DataLen | CRC_CTL_CRCCEN_Msk; + + /* Setting RST bit will reload the initial seed value (CRC_SEED register) */ + CRC->CTL |= CRC_CTL_CRC_RST_Msk; +} + +/** + * @brief CRC Start DMA transfer + * + * @param[in] u32SrcAddr Starting source address of CRC DMA transfer. + * @param[in] u32ByteCount Calculate byte counts of CRC DMA transfer. + * + * @return None + * + * @details This function start CRC DMA transfer from specify source address and byte counts. + */ +void CRC_StartDMATransfer(uint32_t u32SrcAddr, uint32_t u32ByteCount) +{ + CRC->DMASAR = u32SrcAddr; + CRC->DMABCR = u32ByteCount; + CRC->CTL |= CRC_CTL_TRIG_EN_Msk; +} + +/** + * @brief Get CRC Checksum + * + * @param None + * + * @return Checksum Value + * + * @details This macro get the CRC checksum result by current CRC polynomial mode. + */ +uint32_t CRC_GetChecksum(void) +{ + switch(CRC->CTL & CRC_CTL_CRC_MODE_Msk) + { + case CRC_CCITT: + case CRC_16: + return (CRC->CHECKSUM & 0xFFFF); + + case CRC_32: + return (CRC->CHECKSUM); + + case CRC_8: + return (CRC->CHECKSUM & 0xFF); + + default: + return 0; + } +} + +/*@}*/ /* end of group CRC_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group CRC_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/fmc.c b/NUC123/StdDriver/src/fmc.c new file mode 100644 index 0000000..37a9323 --- /dev/null +++ b/NUC123/StdDriver/src/fmc.c @@ -0,0 +1,282 @@ +/**************************************************************************//** + * @file fmc.c + * @version V3.00 + * $Revision: 4 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series FMC driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ + +//* Includes ------------------------------------------------------------------*/ +#include +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup FMC_Driver FMC Driver + @{ +*/ + + +/** @addtogroup FMC_EXPORTED_FUNCTIONS FMC Exported Functions + @{ +*/ + + +/** + * @brief Set boot source from LDROM or APROM after next software reset + * + * @param[in] i32BootSrc + * 1: Boot from LDROM, + * 0: Boot from APROM + * + * @return None + * + * @details This function is used to switch APROM boot or LDROM boot. User need to call + * FMC_SetBootSource to select boot source first, then use CPU reset or + * System Reset Request to reset system. + * + */ +void FMC_SetBootSource(int32_t i32BootSrc) +{ + if(i32BootSrc) + FMC->ISPCON |= FMC_ISPCON_BS_Msk; /* Boot from LDROM */ + else + FMC->ISPCON &= ~FMC_ISPCON_BS_Msk;/* Boot from APROM */ +} + + +/** + * @brief Disable ISP Functions + * + * @param None + * + * @return None + * + * @details This function will clear ISPEN bit of ISPCON to disable ISP function + * + */ +void FMC_Close(void) +{ + FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk; +} + + +/** + * @brief Disable APROM update function + * + * @param None + * + * @return None + * + * @details Disable APROM update function will forbid APROM programming when boot form APROM. + * APROM update is default to be disable. + * + */ +void FMC_DisableAPUpdate(void) +{ + FMC->ISPCON &= ~FMC_ISPCON_APUEN_Msk; +} + + +/** + * @brief Disable User Configuration update function + * + * @param None + * + * @return None + * + * @details Disable User Configuration update function will forbid User Configuration programming. + * User Configuration update is default to be disable. + */ +void FMC_DisableConfigUpdate(void) +{ + FMC->ISPCON &= ~FMC_ISPCON_CFGUEN_Msk; +} + + +/** + * @brief Disable LDROM update function + * + * @param None + * + * @return None + + * @details Disable LDROM update function will forbid LDROM programming. + * LDROM update is default to be disable. + */ +void FMC_DisableLDUpdate(void) +{ + FMC->ISPCON &= ~FMC_ISPCON_LDUEN_Msk; +} + + +/** + * @brief Enable APROM update function + * + * @param None + * + * @return None + * + * @details Enable APROM to be able to program when boot from APROM. + * + */ +void FMC_EnableAPUpdate(void) +{ + FMC->ISPCON |= FMC_ISPCON_APUEN_Msk; +} + + +/** + * @brief Enable User Configuration update function + * + * @param None + * + * @return None + * + * @details Enable User Configuration to be able to program. + * + */ +void FMC_EnableConfigUpdate(void) +{ + FMC->ISPCON |= FMC_ISPCON_CFGUEN_Msk; +} + + +/** + * @brief Enable LDROM update function + * + * @param None + * + * @return None + * + * @details Enable LDROM to be able to program. + * + */ +void FMC_EnableLDUpdate(void) +{ + FMC->ISPCON |= FMC_ISPCON_LDUEN_Msk; +} + + +/** + * @brief Get the current boot source + * + * @param None + * + * @retval 0 This chip is currently booting from APROM + * @retval 1 This chip is currently booting from LDROM + * + * @note This function only show the boot source. + * User need to read ISPSTA register to know if IAP mode supported or not in relative boot. + */ +int32_t FMC_GetBootSource(void) +{ + if(FMC->ISPCON & FMC_ISPCON_BS_Msk) + return 1; + else + return 0; +} + + +/** + * @brief Enable FMC ISP function + * + * @param None + * + * @return None + * + * @details ISPEN bit of ISPCON must be set before we can use ISP commands. + * Therefore, To use all FMC function APIs, user needs to call FMC_Open() first to enable ISP functions. + * + * @note ISP functions are write-protected. user also needs to unlock it by calling SYS_UnlockReg() before using all ISP functions. + * + */ +void FMC_Open(void) +{ + FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk; +} + +/** + * @brief Get the base address of Data Flash if enabled. + * + * @param None + * + * @return The base address of Data Flash + * + * @details This function is used to return the base address of Data Flash. + * + */ +uint32_t FMC_ReadDataFlashBaseAddr(void) +{ + return FMC->DFBADR; +} + + +/** + * @brief Read the User Configuration words. + * + * @param[out] u32Config The word buffer to store the User Configuration data. + * @param[in] u32Count The word count to be read. + * + * @retval 0 Success + * @retval -1 Failed + * + * @details This function is used to read the settings of user configuration. + * if u32Count = 1, Only CONFIG0 will be returned to the buffer specified by u32Config. + * if u32Count = 2, Both CONFIG0 and CONFIG1 will be returned. + */ +int32_t FMC_ReadConfig(uint32_t *u32Config, uint32_t u32Count) +{ + int32_t i; + + for(i = 0; i < u32Count; i++) + u32Config[i] = FMC_Read(FMC_CONFIG_BASE + i * 4); + + return 0; +} + + +/** + * @brief Write User Configuration + * + * @param[in] u32Config The word buffer to store the User Configuration data. + * @param[in] u32Count The word count to program to User Configuration. + * + * @retval 0 Success + * @retval -1 Failed + * + * @details User must enable User Configuration update before writing it. + * User must erase User Configuration before writing it. + * User Configuration is also be page erase. User needs to backup necessary data + * before erase User Configuration. + */ +int32_t FMC_WriteConfig(uint32_t *u32Config, uint32_t u32Count) +{ + int32_t i; + + for(i = 0; i < u32Count; i++) + { + FMC_Write(FMC_CONFIG_BASE + i * 4, u32Config[i]); + if(FMC_Read(FMC_CONFIG_BASE + i * 4) != u32Config[i]) + return -1; + } + + return 0; +} + + +/*@}*/ /* end of group FMC_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group FMC_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ + + diff --git a/NUC123/StdDriver/src/gpio.c b/NUC123/StdDriver/src/gpio.c new file mode 100644 index 0000000..465ac27 --- /dev/null +++ b/NUC123/StdDriver/src/gpio.c @@ -0,0 +1,122 @@ +/**************************************************************************//** + * @file gpio.c + * @version V3.00 + * $Revision: 8 $ + * $Date: 16/06/08 9:58a $ + * @brief NUC123 series GPIO driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ + +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup GPIO_Driver GPIO Driver + @{ +*/ + +/** @addtogroup GPIO_EXPORTED_FUNCTIONS GPIO Exported Functions + @{ +*/ + +/** + * @brief Set GPIO operation mode + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32PinMask The single or multiple pins of specified GPIO port. \n + * It could be BIT10 ~ BIT15 for PA GPIO port. \n + * It could be BIT0 ~ BIT10 and BIT12 ~ BIT15 for PB GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT13 for PC GPIO port. \n + * It could be BIT0 ~ BIT5 and BIT8 ~ BIT11 for PD GPIO port. \n + * It could be BIT0 ~ BIT3 for PF GPIO port. + * @param[in] u32Mode Operation mode. It could be : + * - \ref GPIO_PMD_INPUT, + * - \ref GPIO_PMD_OUTPUT, + * - \ref GPIO_PMD_OPEN_DRAIN, + * - \ref GPIO_PMD_QUASI + * + * @return None + * + * @details This function is used to set specified GPIO operation mode. + */ +void GPIO_SetMode(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode) +{ + uint32_t i; + + for(i = 0; i < GPIO_PIN_MAX; i++) + { + if(u32PinMask & (1 << i)) + { + port->PMD = (port->PMD & ~(0x3 << (i << 1))) | (u32Mode << (i << 1)); + } + } +} + +/** + * @brief Enable GPIO interrupt + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Pin The pin of specified GPIO port. \n + * It could be 10 ~ 15 for PA GPIO port. \n + * It could be 0 ~ 10 and 12 ~ 15 for PB GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 13 for PC GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 11 for PD GPIO port. \n + * It could be 0 ~ 3 for PF GPIO port. + * @param[in] u32IntAttribs The interrupt attribute of specified GPIO pin. It could be : + * - \ref GPIO_INT_RISING + * - \ref GPIO_INT_FALLING + * - \ref GPIO_INT_BOTH_EDGE + * - \ref GPIO_INT_HIGH + * - \ref GPIO_INT_LOW + * + * @return None + * + * @details This function is used to enable specified GPIO pin interrupt. + */ +void GPIO_EnableInt(GPIO_T *port, uint32_t u32Pin, uint32_t u32IntAttribs) +{ + /* Configure interrupt mode of specified pin */ + port->IMD = (port->IMD & ~(1ul << u32Pin)) | (((u32IntAttribs >> 24) & 0xFFUL) << u32Pin); + + /* Enable interrupt function of specified pin */ + port->IEN = (port->IEN & ~(0x00010001ul << u32Pin)) | ((u32IntAttribs & 0xFFFFFFUL) << u32Pin); +} + + +/** + * @brief Disable GPIO interrupt + * + * @param[in] port GPIO port. It could be PA, PB, PC, PD or PF. + * @param[in] u32Pin The pin of specified GPIO port. \n + * It could be 10 ~ 15 for PA GPIO port. \n + * It could be 0 ~ 10 and 12 ~ 15 for PB GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 13 for PC GPIO port. \n + * It could be 0 ~ 5 and 8 ~ 11 for PD GPIO port. \n + * It could be 0 ~ 3 for PF GPIO port. + * + * @return None + * + * @details This function is used to disable specified GPIO pin interrupt. + */ +void GPIO_DisableInt(GPIO_T *port, uint32_t u32Pin) +{ + /* Configure interrupt mode of specified pin */ + port->IMD &= ~(1UL << u32Pin); + + /* Disable interrupt function of specified pin */ + port->IEN &= ~((0x00010001UL) << u32Pin); +} + + +/*@}*/ /* end of group GPIO_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group GPIO_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/i2c.c b/NUC123/StdDriver/src/i2c.c new file mode 100644 index 0000000..6ef24a2 --- /dev/null +++ b/NUC123/StdDriver/src/i2c.c @@ -0,0 +1,1159 @@ +/**************************************************************************//** + * @file i2c.c + * @version V3.00 + * $Revision: 12 $ + * $Date: 17/06/27 2:39p $ + * @brief NUC123 series I2C driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + *****************************************************************************/ +#include +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup I2C_Driver I2C Driver + @{ +*/ + + +/** @addtogroup I2C_EXPORTED_FUNCTIONS I2C Exported Functions + @{ +*/ + +/** + * @brief Enable specify I2C Controller and set Clock Divider + * + * @param[in] i2c Specify I2C port + * @param[in] u32BusClock The target I2C Bus clock in Hz + * + * @return Actual I2C Bus clock frequency + * + * @details The function enable the specify I2C controller and set proper Clock Divider + * in I2C CLOCK DIVIDED REGISTER (I2CLK) according to the target I2C Bus clock. + * I2C Bus clock = PCLK / (4*(divider+1). + * + */ +uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock) +{ + uint32_t u32Div; + + u32Div = (uint32_t)(((SystemCoreClock * 10) / (u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */ + i2c->I2CLK = u32Div; + + /* Enable I2C */ + i2c->I2CON |= I2C_I2CON_ENS1_Msk; + + return (SystemCoreClock / ((u32Div + 1) << 2)); +} + +/** + * @brief Disable specify I2C Controller + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details Reset I2C Controller and disable specify I2C port. + * + */ + +void I2C_Close(I2C_T *i2c) +{ + /* Reset I2C Controller */ + if((uint32_t)i2c == I2C0_BASE) + { + SYS->IPRSTC2 |= SYS_IPRSTC2_I2C0_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_I2C0_RST_Msk; + } + else if((uint32_t)i2c == I2C1_BASE) + { + SYS->IPRSTC2 |= SYS_IPRSTC2_I2C1_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_I2C1_RST_Msk; + } + + /* Disable I2C */ + i2c->I2CON &= ~I2C_I2CON_ENS1_Msk; +} + +/** + * @brief Clear Time-out Counter flag + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details When Time-out flag will be set, use this function to clear I2C bus Time-out counter flag . + * + */ +void I2C_ClearTimeoutFlag(I2C_T *i2c) +{ + i2c->I2CTOC |= I2C_I2CTOC_TIF_Msk; +} + +/** + * @brief Set control bit of I2C Controller + * + * @param[in] i2c Specify I2C port + * @param[in] u8Start Set I2C START condition + * @param[in] u8Stop Set I2C STOP condition + * @param[in] u8Si Clear SI flag + * @param[in] u8Ack Set I2C ACK bit + * + * @return None + * + * @details The function set I2C control bit of I2C bus protocol. + * + */ +void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack) +{ + uint32_t u32Reg = 0; + + if(u8Start) + u32Reg |= I2C_I2CON_STA; + if(u8Stop) + u32Reg |= I2C_I2CON_STO; + if(u8Si) + u32Reg |= I2C_I2CON_SI; + if(u8Ack) + u32Reg |= I2C_I2CON_AA; + + i2c->I2CON = (i2c->I2CON & ~0x3C) | u32Reg; +} + +/** + * @brief Disable Interrupt of I2C Controller + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details The function is used for disable I2C interrupt + * + */ +void I2C_DisableInt(I2C_T *i2c) +{ + i2c->I2CON &= ~I2C_I2CON_EI_Msk; +} + +/** + * @brief Enable Interrupt of I2C Controller + * + * @param[in] i2c Specify I2C port + * + * @return None + * + * @details The function is used for enable I2C interrupt + * + */ +void I2C_EnableInt(I2C_T *i2c) +{ + i2c->I2CON |= I2C_I2CON_EI_Msk; +} + +/** + * @brief Get I2C Bus clock + * + * @param[in] i2c Specify I2C port + * + * @return The actual I2C Bus clock in Hz + * + * @details To get the actual I2C Bus Clock frequency. + */ +uint32_t I2C_GetBusClockFreq(I2C_T *i2c) +{ + uint32_t u32Divider = i2c->I2CLK; + + return (SystemCoreClock / ((u32Divider + 1) << 2)); +} + +/** + * @brief Set I2C Bus clock + * + * @param[in] i2c Specify I2C port + * @param[in] u32BusClock The target I2C Bus clock in Hz + * + * @return The actual I2C Bus clock in Hz + * + * @details To set the actual I2C Bus clock frequency. + */ +uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock) +{ + uint32_t u32Div; + + u32Div = (uint32_t)(((SystemCoreClock * 10) / (u32BusClock * 4) + 5) / 10 - 1); /* Compute proper divider for I2C clock */ + i2c->I2CLK = u32Div; + + return (SystemCoreClock / ((u32Div + 1) << 2)); +} + +/** + * @brief Get Interrupt Flag + * + * @param[in] i2c Specify I2C port + * + * @return I2C interrupt flag status + * + * @details To get I2C Bus interrupt flag. + */ +uint32_t I2C_GetIntFlag(I2C_T *i2c) +{ + return ((i2c->I2CON & I2C_I2CON_SI_Msk) == I2C_I2CON_SI_Msk ? 1 : 0); +} + +/** + * @brief Get I2C bus Status Code + * + * @param[in] i2c Specify I2C port + * + * @return I2C Status Code + * + * @details To get I2C bus Status Code. + */ +uint32_t I2C_GetStatus(I2C_T *i2c) +{ + return (i2c->I2CSTATUS); +} + +/** + * @brief Read a byte from I2C Bus + * + * @param[in] i2c Specify I2C port + * + * @return I2C Data + * + * @details To read a bytes data from specify I2C port. + */ +uint8_t I2C_GetData(I2C_T *i2c) +{ + return (i2c->I2CDAT); +} + +/** + * @brief Send a byte to I2C bus + * + * @param[in] i2c I2C port + * @param[in] u8Data The data to send to I2C bus + * + * @return None + * + * @details This function is used to write a byte to specified I2C port + */ +void I2C_SetData(I2C_T *i2c, uint8_t u8Data) +{ + i2c->I2CDAT = u8Data; +} + +/** + * @brief Set 7-bit Slave Address and GC Mode + * + * @param[in] i2c I2C port + * @param[in] u8SlaveNo Set the number of I2C address register (0~3) + * @param[in] u8SlaveAddr 7-bit slave address + * @param[in] u8GCMode Enable/Disable GC Mode (I2C_GCMODE_ENABLE / I2C_GCMODE_DISABLE) + * + * @return None + * + * @details This function is used to set 7-bit slave addresses in I2C SLAVE ADDRESS REGISTER (I2CADDR0~3) + * and enable GC Mode. + * + */ +void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode) +{ + switch(u8SlaveNo) + { + case 1: + i2c->I2CADDR1 = (u8SlaveAddr << 1) | u8GCMode; + break; + case 2: + i2c->I2CADDR2 = (u8SlaveAddr << 1) | u8GCMode; + break; + case 3: + i2c->I2CADDR3 = (u8SlaveAddr << 1) | u8GCMode; + break; + case 0: + default: + i2c->I2CADDR0 = (u8SlaveAddr << 1) | u8GCMode; + break; + } +} + +/** + * @brief Configure the mask bits of 7-bit Slave Address + * + * @param[in] i2c I2C port + * @param[in] u8SlaveNo Set the number of I2C address mask register (0~3) + * @param[in] u8SlaveAddrMask A byte for slave address mask + * + * @return None + * + * @details This function is used to set 7-bit slave addresses. + * + */ +void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask) +{ + switch(u8SlaveNo) + { + case 1: + i2c->I2CADM1 = u8SlaveAddrMask << 1; + break; + case 2: + i2c->I2CADM2 = u8SlaveAddrMask << 1; + break; + case 3: + i2c->I2CADM3 = u8SlaveAddrMask << 1; + break; + case 0: + default: + i2c->I2CADM0 = u8SlaveAddrMask << 1; + break; + } +} + +/** + * @brief Enable Time-out Counter Function and support Long Time-out + * + * @param[in] i2c I2C port + * @param[in] u8LongTimeout Configure DIV4 to enable Long Time-out (0/1) + * + * @return None + * + * @details This function enable Time-out Counter function and configure DIV4 to support Long + * Time-out. + * + */ +void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout) +{ + if(u8LongTimeout) + i2c->I2CTOC |= I2C_I2CTOC_DIV4_Msk; + else + i2c->I2CTOC &= ~I2C_I2CTOC_DIV4_Msk; + + i2c->I2CTOC |= I2C_I2CTOC_ENTI_Msk; +} + +/** + * @brief Disable Time-out Counter Function + * + * @param[in] i2c I2C port + * + * @return None + * + * @details To disable Time-out Counter function in I2CTOC register. + * + */ +void I2C_DisableTimeout(I2C_T *i2c) +{ + i2c->I2CTOC &= ~I2C_I2CTOC_ENTI_Msk; +} + +/** + * @brief Enable I2C Wake-up Function + * + * @param[in] i2c I2C port + * + * @return None + * + * @details To enable Wake-up function of I2C Wake-up control register. + * + */ +void I2C_EnableWakeup(I2C_T *i2c) +{ + i2c->I2CWKUPCON |= I2C_I2CWKUPCON_WKUPEN_Msk; +} + +/** + * @brief Disable I2C Wake-up Function + * + * @param[in] i2c I2C port + * + * @return None + * + * @details To disable Wake-up function of I2C Wake-up control register. + * + */ +void I2C_DisableWakeup(I2C_T *i2c) +{ + i2c->I2CWKUPCON &= ~I2C_I2CWKUPCON_WKUPEN_Msk; +} + +/** + * @brief Write a byte to Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] data Write a byte data to Slave + * + * @retval 0 Write data success + * @retval 1 Write data fail, or bus occurs error events + * + * @details The function is used for I2C Master write a byte data to Slave. + * + */ + +uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, const uint8_t data) +{ + uint8_t u8Xfering = 1, u8Err = 0, u8Ctrl = 0; + + I2C_START(i2c); + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + I2C_SET_DATA(i2c, data); /* Write data to I2CDAT */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + return (u8Err | u8Xfering); /* return (Success)/(Fail) status */ +} + +/** + * @brief Write multi bytes to Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] *data Pointer to array to write data to Slave + * @param[in] u32wLen How many bytes need to write to Slave + * + * @return A length of how many bytes have been transmitted. + * + * @details The function is used for I2C Master write multi bytes data to Slave. + * + */ + +uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, const uint8_t *data, uint32_t u32wLen) +{ + uint8_t u8Xfering = 1, u8Err = 0, u8Ctrl = 0; + uint32_t u32txLen = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + case 0x28: + if(u32txLen> 8)); /* Write Hi byte address of register */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + if(u8Addr) + { + I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF)); /* Write Lo byte address of register */ + u8Addr = 0; + } + else if((u32txLen < 1) && (u8Addr == 0)) + { + I2C_SET_DATA(i2c, data); + u32txLen++; + } + else + { + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + } + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + return (u8Err | u8Xfering); /* return (Success)/(Fail) status */ +} + + +/** + * @brief Specify two bytes register address and write multi bytes to Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] u16DataAddr Specify a address (2 bytes) of data write to + * @param[in] *data Pointer to array to write data to Slave + * @param[in] u32wLen How many bytes need to write to Slave + * + * @return A length of how many bytes have been transmitted. + * + * @details The function is used for I2C Master specify a byte address that multi data write to in Slave. + * + */ + +uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, const uint8_t *data, uint32_t u32wLen) +{ + uint8_t u8Xfering = 1, u8Err = 0, u8Addr = 1, u8Ctrl = 0; + uint32_t u32txLen = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00) >> 8)); /* Write Hi byte address of register */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + if(u8Addr) + { + I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF)); /* Write Lo byte address of register */ + u8Addr = 0; + } + else if((u32txLen < u32wLen) && (u8Addr == 0)) + I2C_SET_DATA(i2c, data[u32txLen++]); /* Write data to Register I2CDAT*/ + else + { + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + } + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + return u32txLen; /* Return bytes length that have been transmitted */ +} + +/** + * @brief Read a byte from Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * + * @return Read a byte data from Slave + * + * @details The function is used for I2C Master to read a byte data from Slave. + * + */ +uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr) +{ + uint8_t u8Xfering = 1, u8Err = 0, rdata = 0, u8Ctrl = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x40: /* Slave Address ACK */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x48: /* Slave Address NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x58: + rdata = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + if(u8Err) + rdata = 0; /* If occurs error, return 0 */ + return rdata; /* Return read data */ +} + + +/** + * @brief Read multi bytes from Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[out] *rdata Point to array to store data from Slave + * @param[in] u32rLen How many bytes need to read from Slave + * + * @return A length of how many bytes have been received + * + * @details The function is used for I2C Master to read multi data bytes from Slave. + * + * + */ +uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t *rdata, uint32_t u32rLen) +{ + uint8_t u8Xfering = 1, u8Err = 0, u8Ctrl = 0; + uint32_t u32rxLen = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x40: /* Slave Address ACK */ + u8Ctrl = I2C_I2CON_SI_AA; /* Clear SI and set ACK */ + break; + case 0x48: /* Slave Address NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x50: + rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */ + if(u32rxLen<(u32rLen-1)) + { + u8Ctrl = I2C_I2CON_SI_AA; /* Clear SI and set ACK */ + } + else + { + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + } + break; + case 0x58: + rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + return u32rxLen; /* Return bytes length that have been received */ +} + + +/** + * @brief Specify a byte register address and read a byte from Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] u8DataAddr Specify a address(1 byte) of data read from + * + * @return Read a byte data from Slave + * + * @details The function is used for I2C Master specify a byte address that a data byte read from Slave. + * + * + */ +uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr) +{ + uint8_t u8Xfering = 1, u8Err = 0, rdata = 0, u8Ctrl = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + I2C_SET_DATA(i2c, u8DataAddr); /* Write Lo byte address of register */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + u8Ctrl = I2C_I2CON_STA_SI; /* Send repeat START */ + break; + case 0x10: + I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x40: /* Slave Address ACK */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x48: /* Slave Address NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x58: + rdata = (uint8_t) I2C_GET_DATA(i2c); /* Receive Data */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + if(u8Err) + rdata = 0; /* If occurs error, return 0 */ + return rdata; /* Return read data */ +} + +/** + * @brief Specify a byte register address and read multi bytes from Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] u8DataAddr Specify a address (1 bytes) of data read from + * @param[out] *rdata Point to array to store data from Slave + * @param[in] u32rLen How many bytes need to read from Slave + * + * @return A length of how many bytes have been received + * + * @details The function is used for I2C Master specify a byte address that multi data bytes read from Slave. + * + * + */ +uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t *rdata, uint32_t u32rLen) +{ + uint8_t u8Xfering = 1, u8Err = 0, u8Ctrl = 0; + uint32_t u32rxLen = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + I2C_SET_DATA(i2c, u8DataAddr); /* Write Lo byte address of register */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + u8Ctrl = I2C_I2CON_STA_SI; /* Send repeat START */ + break; + case 0x10: + I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x40: /* Slave Address ACK */ + u8Ctrl = I2C_I2CON_SI_AA; /* Clear SI and set ACK */ + break; + case 0x48: /* Slave Address NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x50: + rdata[u32rxLen++] = (uint8_t) I2C_GET_DATA(i2c); /* Receive Data */ + if(u32rxLen<(u32rLen-1)) + u8Ctrl = I2C_I2CON_SI_AA; /* Clear SI and set ACK */ + else + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x58: + rdata[u32rxLen++] = (uint8_t) I2C_GET_DATA(i2c); /* Receive Data */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + return u32rxLen; /* Return bytes length that have been received */ +} + +/** + * @brief Specify two bytes register address and read a byte from Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] u16DataAddr Specify a address(2 byte) of data read from + * + * @return Read a byte data from Slave + * + * @details The function is used for I2C Master specify two bytes address that a data byte read from Slave. + * + * + */ +uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr) +{ + uint8_t u8Xfering = 1, u8Err = 0, rdata = 0, u8Addr = 1, u8Ctrl = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00) >> 8)); /* Write Hi byte address of register */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + if(u8Addr) + { + I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF)); /* Write Lo byte address of register */ + u8Addr = 0; + } + else + u8Ctrl = I2C_I2CON_STA_SI; /* Clear SI and send repeat START */ + break; + case 0x10: + I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x40: /* Slave Address ACK */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x48: /* Slave Address NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x58: + rdata = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + if(u8Err) + rdata = 0; /* If occurs error, return 0 */ + return rdata; /* Return read data */ +} + +/** + * @brief Specify two bytes register address and read multi bytes from Slave + * + * @param[in] *i2c Point to I2C peripheral + * @param[in] u8SlaveAddr Access Slave address(7-bit) + * @param[in] u16DataAddr Specify a address (2 bytes) of data read from + * @param[out] *rdata Point to array to store data from Slave + * @param[in] u32rLen How many bytes need to read from Slave + * + * @return A length of how many bytes have been received + * + * @details The function is used for I2C Master specify two bytes address that multi data bytes read from Slave. + * + * + */ +uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t *rdata, uint32_t u32rLen) +{ + uint8_t u8Xfering = 1, u8Err = 0, u8Addr = 1, u8Ctrl = 0; + uint32_t u32rxLen = 0; + + I2C_START(i2c); /* Send START */ + while(u8Xfering && (u8Err == 0)) + { + I2C_WAIT_READY(i2c); + switch(I2C_GET_STATUS(i2c)) + { + case 0x08: + I2C_SET_DATA(i2c, (u8SlaveAddr << 1 | 0x00)); /* Write SLA+W to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x18: /* Slave Address ACK */ + I2C_SET_DATA(i2c, (uint8_t)((u16DataAddr & 0xFF00) >> 8)); /* Write Hi byte address of register */ + break; + case 0x20: /* Slave Address NACK */ + case 0x30: /* Master transmit data NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x28: + if(u8Addr) + { + I2C_SET_DATA(i2c, (uint8_t)(u16DataAddr & 0xFF)); /* Write Lo byte address of register */ + u8Addr = 0; + } + else + u8Ctrl = I2C_I2CON_STA_SI; /* Clear SI and send repeat START */ + break; + case 0x10: + I2C_SET_DATA(i2c, ((u8SlaveAddr << 1) | 0x01)); /* Write SLA+R to Register I2CDAT */ + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x40: /* Slave Address ACK */ + u8Ctrl = I2C_I2CON_SI_AA; /* Clear SI and set ACK */ + break; + case 0x48: /* Slave Address NACK */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + case 0x50: + rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */ + if(u32rxLen<(u32rLen-1)) + u8Ctrl = I2C_I2CON_SI_AA; /* Clear SI and set ACK */ + else + u8Ctrl = I2C_I2CON_SI; /* Clear SI */ + break; + case 0x58: + rdata[u32rxLen++] = (unsigned char) I2C_GET_DATA(i2c); /* Receive Data */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Xfering = 0; + break; + case 0x38: /* Arbitration Lost */ + default: /* Unknow status */ + u8Ctrl = I2C_I2CON_STO_SI; /* Clear SI and send STOP */ + u8Err = 1; + break; + } + I2C_SET_CONTROL_REG(i2c, u8Ctrl); /* Write controlbit to I2C_CTL register */ + } + return u32rxLen; /* Return bytes length that have been received */ +} + + +/*@}*/ /* end of group I2C_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group I2C_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/i2s.c b/NUC123/StdDriver/src/i2s.c new file mode 100644 index 0000000..8f699b0 --- /dev/null +++ b/NUC123/StdDriver/src/i2s.c @@ -0,0 +1,222 @@ +/**************************************************************************//** + * @file i2s.c + * @version V3.0 + * $Revision: 7 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series I2S driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ + +#include +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup I2S_Driver I2S Driver + @{ +*/ + +/** @addtogroup I2S_EXPORTED_FUNCTIONS I2S Exported Functions + @{ +*/ + +/** + * @brief This function is used to get I2S source clock frequency. + * @param[in] i2s The pointer of the specified I2S module. + * @return I2S source clock frequency (Hz). + * @details Return the source clock frequency according to the setting of I2S_S (CLKSEL2[1:0]). + */ +static uint32_t I2S_GetSourceClockFreq(I2S_T *i2s) +{ + uint32_t u32Freq, u32ClkSrcSel; + + u32ClkSrcSel = CLK->CLKSEL2 & CLK_CLKSEL2_I2S_S_Msk; + + switch(u32ClkSrcSel) + { + case CLK_CLKSEL2_I2S_S_HXT: + u32Freq = __HXT; + break; + + case CLK_CLKSEL2_I2S_S_PLL: + u32Freq = CLK_GetPLLClockFreq(); + break; + + case CLK_CLKSEL2_I2S_S_HIRC: + u32Freq = __HIRC; + break; + + case CLK_CLKSEL2_I2S_S_HCLK: + u32Freq = SystemCoreClock; + break; + + default: + u32Freq = __HIRC; + break; + } + + return u32Freq; +} + +/** + * @brief This function configures some parameters of I2S interface for general purpose use. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32MasterSlave I2S operation mode. Valid values are: + * - \ref I2S_MODE_MASTER + * - \ref I2S_MODE_SLAVE + * @param[in] u32SampleRate Sample rate + * @param[in] u32WordWidth Data length. Valid values are: + * - \ref I2S_DATABIT_8 + * - \ref I2S_DATABIT_16 + * - \ref I2S_DATABIT_24 + * - \ref I2S_DATABIT_32 + * @param[in] u32Channels Audio format. Valid values are: + * - \ref I2S_MONO + * - \ref I2S_STEREO + * @param[in] u32DataFormat Data format. Valid values are: + * - \ref I2S_FORMAT_I2S + * - \ref I2S_FORMAT_MSB + * - \ref I2S_FORMAT_PCM_A + * - \ref I2S_FORMAT_PCM_B + * @return Real sample rate. + * @details This function will configure I2S controller according to the input parameters. Set TX and RX FIFO threshold to middle value. + * The actual sample rate may be different from the target sample rate. The real sample rate will be returned for reference. + * @note Both the TX and RX functions will be enabled. + */ +uint32_t I2S_Open(I2S_T *i2s, uint32_t u32MasterSlave, uint32_t u32SampleRate, uint32_t u32WordWidth, uint32_t u32Channels, uint32_t u32DataFormat) +{ + uint32_t u32Divider; + uint32_t u32BitRate, u32SrcClk; + + /* Reset I2S */ + SYS->IPRSTC2 |= SYS_IPRSTC2_I2S_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_I2S_RST_Msk; + + /* Configure I2S controller according to input parameters. */ + i2s->CON = u32MasterSlave | u32WordWidth | u32Channels | u32DataFormat | I2S_FIFO_TX_LEVEL_WORD_4 | I2S_FIFO_RX_LEVEL_WORD_4; + + /* Get I2S source clock frequency */ + u32SrcClk = I2S_GetSourceClockFreq(i2s); + + /* Calculate bit clock rate */ + u32BitRate = u32SampleRate * (((u32WordWidth >> 4) & 0x3) + 1) * 16; + u32Divider = ((u32SrcClk / u32BitRate) >> 1) - 1; + i2s->CLKDIV = (i2s->CLKDIV & ~I2S_CLKDIV_BCLK_DIV_Msk) | (u32Divider << 8); + + /* Calculate real sample rate */ + u32BitRate = u32SrcClk / ((u32Divider + 1) * 2); + u32SampleRate = u32BitRate / ((((u32WordWidth >> 4) & 0x3) + 1) * 16); + + /* Enable TX, RX and I2S controller */ + i2s->CON |= (I2S_CON_RXEN_Msk | I2S_CON_TXEN_Msk | I2S_CON_I2SEN_Msk); + + return u32SampleRate; +} + +/** + * @brief Disable I2S function. + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details Disable I2S function. + */ +void I2S_Close(I2S_T *i2s) +{ + i2s->CON &= ~I2S_CON_I2SEN_Msk; +} + +/** + * @brief Enable interrupt function. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32Mask The combination of all related interrupt enable bits. + * Each bit corresponds to a interrupt bit. Valid values are: + * - \ref I2S_IE_LZCIE_Msk + * - \ref I2S_IE_RZCIE_Msk + * - \ref I2S_IE_TXTHIE_Msk + * - \ref I2S_IE_TXOVFIE_Msk + * - \ref I2S_IE_TXUDFIE_Msk + * - \ref I2S_IE_RXTHIE_Msk + * - \ref I2S_IE_RXOVFIE_Msk + * - \ref I2S_IE_RXUDFIE_Msk + * @return None + * @details This function enables the interrupt according to the mask parameter. + */ +void I2S_EnableInt(I2S_T *i2s, uint32_t u32Mask) +{ + i2s->IE |= u32Mask; +} + +/** + * @brief Disable interrupt function. + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32Mask The combination of all related interrupt enable bits. + * Each bit corresponds to a interrupt bit. Valid values are: + * - \ref I2S_IE_LZCIE_Msk + * - \ref I2S_IE_RZCIE_Msk + * - \ref I2S_IE_TXTHIE_Msk + * - \ref I2S_IE_TXOVFIE_Msk + * - \ref I2S_IE_TXUDFIE_Msk + * - \ref I2S_IE_RXTHIE_Msk + * - \ref I2S_IE_RXOVFIE_Msk + * - \ref I2S_IE_RXUDFIE_Msk + * @return None + * @details This function disables the interrupt according to the mask parameter. + */ +void I2S_DisableInt(I2S_T *i2s, uint32_t u32Mask) +{ + i2s->IE &= ~u32Mask; +} + +/** + * @brief Enable master clock (MCLK). + * @param[in] i2s The pointer of the specified I2S module. + * @param[in] u32BusClock The target MCLK clock. + * @return Actual MCLK clock + * @details Set the master clock rate according to u32BusClock parameter and enable master clock output. + * The actual master clock rate may be different from the target master clock rate. The real master clock rate will be returned for reference. + */ +uint32_t I2S_EnableMCLK(I2S_T *i2s, uint32_t u32BusClock) +{ + uint32_t u32Divider; + uint32_t u32SrcClk, u32Reg; + + u32SrcClk = I2S_GetSourceClockFreq(i2s); + if(u32BusClock == u32SrcClk) + u32Divider = 0; + else + u32Divider = (u32SrcClk / u32BusClock) >> 1; + + i2s->CLKDIV = (i2s->CLKDIV & ~I2S_CLKDIV_MCLK_DIV_Msk) | u32Divider; + + i2s->CON |= I2S_CON_MCLKEN_Msk; + + u32Reg = i2s->CLKDIV & I2S_CLKDIV_MCLK_DIV_Msk; + + if(u32Reg == 0) + return u32SrcClk; + else + return ((u32SrcClk >> 1) / u32Reg); +} + +/** + * @brief Disable master clock (MCLK). + * @param[in] i2s The pointer of the specified I2S module. + * @return None + * @details Disable master clock output. + */ +void I2S_DisableMCLK(I2S_T *i2s) +{ + i2s->CON &= ~I2S_CON_MCLKEN_Msk; +} + +/*@}*/ /* end of group I2S_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group I2S_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/pdma.c b/NUC123/StdDriver/src/pdma.c new file mode 100644 index 0000000..4f98a53 --- /dev/null +++ b/NUC123/StdDriver/src/pdma.c @@ -0,0 +1,292 @@ +/**************************************************************************//** + * @file pdma.c + * @version V3.00 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series PDMA driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup PDMA_Driver PDMA Driver + @{ +*/ + + +/** @addtogroup PDMA_EXPORTED_FUNCTIONS PDMA Exported Functions + @{ +*/ + +/** + * @brief PDMA Open + * + * @param[in] u32Mask Channel enable bits. + * + * @return None + * + * @details This function enable the PDMA channels. + */ +void PDMA_Open(uint32_t u32Mask) +{ + PDMA_GCR->GCRCSR |= (u32Mask << 8); +} + +/** + * @brief PDMA Close + * + * @param None + * + * @return None + * + * @details This function disable all PDMA channels. + */ +void PDMA_Close(void) +{ + PDMA_GCR->GCRCSR = 0; +} + +/** + * @brief Set PDMA Transfer Count + * + * @param[in] u32Ch The selected channel + * @param[in] u32Width Data width. Valid values are + * - \ref PDMA_WIDTH_8 + * - \ref PDMA_WIDTH_16 + * - \ref PDMA_WIDTH_32 + * @param[in] u32TransCount Transfer count + * + * @return None + * + * @details This function set the selected channel data width and transfer count. + */ +void PDMA_SetTransferCnt(uint32_t u32Ch, uint32_t u32Width, uint32_t u32TransCount) +{ + PDMA_T *pdma; + pdma = (PDMA_T *)((uint32_t) PDMA0_BASE + (0x100 * u32Ch)); + pdma->CSR = (pdma->CSR & ~PDMA_CSR_APB_TWS_Msk) | u32Width; + switch(u32Width) + { + case PDMA_WIDTH_32: + pdma->BCR = (u32TransCount << 2); + break; + + case PDMA_WIDTH_8: + pdma->BCR = u32TransCount; + break; + + case PDMA_WIDTH_16: + pdma->BCR = (u32TransCount << 1); + break; + + default: + ; + } +} + +/** + * @brief Set PDMA Transfer Address + * + * @param[in] u32Ch The selected channel + * @param[in] u32SrcAddr Source address + * @param[in] u32SrcCtrl Source control attribute. Valid values are + * - \ref PDMA_SAR_INC + * - \ref PDMA_SAR_FIX + * @param[in] u32DstAddr destination address + * @param[in] u32DstCtrl destination control attribute. Valid values are + * - \ref PDMA_DAR_INC + * - \ref PDMA_DAR_FIX + * + * @return None + * + * @details This function set the selected channel source/destination address and attribute. + */ +void PDMA_SetTransferAddr(uint32_t u32Ch, uint32_t u32SrcAddr, uint32_t u32SrcCtrl, uint32_t u32DstAddr, uint32_t u32DstCtrl) +{ + PDMA_T *pdma; + pdma = (PDMA_T *)((uint32_t) PDMA0_BASE + (0x100 * u32Ch)); + + pdma->SAR = u32SrcAddr; + pdma->DAR = u32DstAddr; + pdma->CSR = (pdma->CSR & ~(PDMA_CSR_SAD_SEL_Msk | PDMA_CSR_DAD_SEL_Msk)) | (u32SrcCtrl | u32DstCtrl); +} + +/** + * @brief Set PDMA Transfer Mode + * + * @param[in] u32Ch The selected channel + * @param[in] u32Peripheral The selected peripheral. Valid values are + * - \ref PDMA_SPI0_TX + * - \ref PDMA_SPI1_TX + * - \ref PDMA_SPI2_TX + * - \ref PDMA_UART0_TX + * - \ref PDMA_UART1_TX + * - \ref PDMA_I2S_TX + * - \ref PDMA_SPI0_RX + * - \ref PDMA_SPI1_RX + * - \ref PDMA_SPI2_RX + * - \ref PDMA_UART0_RX + * - \ref PDMA_UART1_RX + * - \ref PDMA_I2S_RX + * - \ref PDMA_ADC + * - \ref PDMA_PWM0_RX + * - \ref PDMA_PWM1_RX + * - \ref PDMA_PWM2_RX + * - \ref PDMA_PWM3_RX + * - \ref PDMA_MEM + * @param[in] u32ScatterEn Scatter-gather mode enable + * @param[in] u32DescAddr Scatter-gather descriptor address + * + * @return None + * + * @details This function set the selected channel transfer mode. Include peripheral setting. + */ +void PDMA_SetTransferMode(uint32_t u32Ch, uint32_t u32Peripheral, uint32_t u32ScatterEn, uint32_t u32DescAddr) +{ + uint32_t u32Index = 0; + PDMA_T *pdma; + pdma = (PDMA_T *)((uint32_t) PDMA0_BASE + (0x100 * u32Ch)); + + if(u32Peripheral > PDMA_PWM3_RX) /* Memory-to-Memory */ + pdma->CSR = (pdma->CSR & ~(PDMA_CSR_MODE_SEL_Msk)); + else if(u32Peripheral > PDMA_I2S_TX) /* Peripheral-to-Memory */ + pdma->CSR = (pdma->CSR & ~(PDMA_CSR_MODE_SEL_Msk) | (0x1 << PDMA_CSR_MODE_SEL_Pos)); + else /* Memory-to-Peripheral */ + pdma->CSR = (pdma->CSR & ~(PDMA_CSR_MODE_SEL_Msk) | (0x2 << PDMA_CSR_MODE_SEL_Pos)); + + switch(u32Peripheral) + { + case 0: + PDMA_GCR->PDSSR0 = (PDMA_GCR->PDSSR0 & ~PDMA_PDSSR0_SPI0_TXSEL_Msk) | (u32Ch << PDMA_PDSSR0_SPI0_TXSEL_Pos); + break; + case 1: + PDMA_GCR->PDSSR0 = (PDMA_GCR->PDSSR0 & ~PDMA_PDSSR0_SPI1_TXSEL_Msk) | (u32Ch << PDMA_PDSSR0_SPI1_TXSEL_Pos); + break; + case 2: + PDMA_GCR->PDSSR0 = (PDMA_GCR->PDSSR0 & ~PDMA_PDSSR0_SPI2_TXSEL_Msk) | (u32Ch << PDMA_PDSSR0_SPI2_TXSEL_Pos); + break; + case 3: + PDMA_GCR->PDSSR1 = (PDMA_GCR->PDSSR1 & ~PDMA_PDSSR1_UART0_TXSEL_Msk) | (u32Ch << PDMA_PDSSR1_UART0_TXSEL_Pos); + break; + case 4: + PDMA_GCR->PDSSR1 = (PDMA_GCR->PDSSR1 & ~PDMA_PDSSR1_UART1_TXSEL_Msk) | (u32Ch << PDMA_PDSSR1_UART1_TXSEL_Pos); + break; + case 5: + PDMA_GCR->PDSSR2 = (PDMA_GCR->PDSSR2 & ~PDMA_PDSSR2_I2S_TXSEL_Msk) | (u32Ch << PDMA_PDSSR2_I2S_TXSEL_Pos); + break; + case 6: + PDMA_GCR->PDSSR0 = (PDMA_GCR->PDSSR0 & ~PDMA_PDSSR0_SPI0_RXSEL_Msk) | (u32Ch << PDMA_PDSSR0_SPI0_RXSEL_Pos); + break; + case 7: + PDMA_GCR->PDSSR0 = (PDMA_GCR->PDSSR0 & ~PDMA_PDSSR0_SPI1_RXSEL_Msk) | (u32Ch << PDMA_PDSSR0_SPI1_RXSEL_Pos); + break; + case 8: + PDMA_GCR->PDSSR0 = (PDMA_GCR->PDSSR0 & ~PDMA_PDSSR0_SPI2_RXSEL_Msk) | (u32Ch << PDMA_PDSSR0_SPI2_RXSEL_Pos); + break; + case 9: + PDMA_GCR->PDSSR1 = (PDMA_GCR->PDSSR1 & ~PDMA_PDSSR1_UART0_RXSEL_Msk) | (u32Ch << PDMA_PDSSR1_UART0_RXSEL_Pos); + break; + case 10: + PDMA_GCR->PDSSR1 = (PDMA_GCR->PDSSR1 & ~PDMA_PDSSR1_UART1_RXSEL_Msk) | (u32Ch << PDMA_PDSSR1_UART1_RXSEL_Pos); + break; + case 11: + PDMA_GCR->PDSSR2 = (PDMA_GCR->PDSSR2 & ~PDMA_PDSSR2_I2S_RXSEL_Msk) | (u32Ch << PDMA_PDSSR2_I2S_RXSEL_Pos); + break; + case 12: + PDMA_GCR->PDSSR1 = (PDMA_GCR->PDSSR1 & ~PDMA_PDSSR1_ADC_RXSEL_Msk) | (u32Ch << PDMA_PDSSR1_ADC_RXSEL_Pos); + break; + case 13: + PDMA_GCR->PDSSR2 = (PDMA_GCR->PDSSR2 & ~PDMA_PDSSR2_PWM0_RXSEL_Msk) | (u32Ch << PDMA_PDSSR2_PWM0_RXSEL_Pos); + break; + case 14: + PDMA_GCR->PDSSR2 = (PDMA_GCR->PDSSR2 & ~PDMA_PDSSR2_PWM1_RXSEL_Msk) | (u32Ch << PDMA_PDSSR2_PWM1_RXSEL_Pos); + break; + case 15: + PDMA_GCR->PDSSR2 = (PDMA_GCR->PDSSR2 & ~PDMA_PDSSR2_PWM2_RXSEL_Msk) | (u32Ch << PDMA_PDSSR2_PWM2_RXSEL_Pos); + break; + case 16: + PDMA_GCR->PDSSR2 = (PDMA_GCR->PDSSR2 & ~PDMA_PDSSR2_PWM3_RXSEL_Msk) | (u32Ch << PDMA_PDSSR2_PWM3_RXSEL_Pos); + break; + + default:/* select PDMA channel as memory to memory */ + for(u32Index = 0; u32Index < 8; u32Index++) + { + if((PDMA_GCR->PDSSR0 & (0xF << (u32Index * 4))) == (u32Ch << (u32Index * 4))) + PDMA_GCR->PDSSR0 |= 0xF << (u32Index * 4); + if((PDMA_GCR->PDSSR1 & (0xF << (u32Index * 4))) == (u32Ch << (u32Index * 4))) + PDMA_GCR->PDSSR1 |= 0xF << (u32Index * 4); + if((PDMA_GCR->PDSSR2 & (0xF << (u32Index * 4))) == (u32Ch << (u32Index * 4))) + PDMA_GCR->PDSSR2 |= 0xF << (u32Index * 4); + } + } +} + +/** + * @brief Trigger PDMA + * + * @param[in] u32Ch The selected channel + * + * @return None + * + * @details This function trigger the selected channel. + */ +void PDMA_Trigger(uint32_t u32Ch) +{ + PDMA_T *pdma; + pdma = (PDMA_T *)((uint32_t) PDMA0_BASE + (0x100 * u32Ch)); + + pdma->CSR |= (PDMA_CSR_TRIG_EN_Msk | PDMA_CSR_PDMACEN_Msk); +} + +/** + * @brief Enable Interrupt + * + * @param[in] u32Ch The selected channel + * @param[in] u32Mask The Interrupt Type + * + * @return None + * + * @details This function enable the selected channel interrupt. + */ +void PDMA_EnableInt(uint32_t u32Ch, uint32_t u32Mask) +{ + PDMA_T *pdma; + pdma = (PDMA_T *)((uint32_t) PDMA0_BASE + (0x100 * u32Ch)); + + pdma->IER |= u32Mask; +} + +/** + * @brief Disable Interrupt + * + * @param[in] u32Ch The selected channel + * @param[in] u32Mask The Interrupt Type + * + * @return None + * + * @details This function disable the selected channel interrupt. + */ +void PDMA_DisableInt(uint32_t u32Ch, uint32_t u32Mask) +{ + PDMA_T *pdma; + pdma = (PDMA_T *)((uint32_t) PDMA0_BASE + (0x100 * u32Ch)); + + pdma->IER &= ~u32Mask; +} + + +/*@}*/ /* end of group PDMA_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group PDMA_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/ps2.c b/NUC123/StdDriver/src/ps2.c new file mode 100644 index 0000000..ad0a491 --- /dev/null +++ b/NUC123/StdDriver/src/ps2.c @@ -0,0 +1,201 @@ +/**************************************************************************//** + * @file ps2.c + * @version V3.00 + * $Revision: 7 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series PS/2 driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ + +#include +#include "NUC123.h" + +/*---------------------------------------------------------------------------------------------------------*/ +/* Includes of local headers */ +/*---------------------------------------------------------------------------------------------------------*/ +#include "ps2.h" + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup PS2_Driver PS2 Driver + @{ +*/ + + +/** @addtogroup PS2_EXPORTED_FUNCTIONS PS2 Exported Functions + @{ +*/ + + +/** + * @brief Enable PS/2 Interrupt + * + * @param[in] u32Mask The specified interrupt of PS/2 module: + * - PS2D_PS2CON_TXINTEN_Msk: PS/2 Tx interrupt + * - PS2D_PS2CON_RXINTEN_Msk: PS/2 Rx interrupt + * + * @return None + * + * @details The function is used to enable PS/2 specified Tx or Rx interrupt. + */ +void PS2_EnableInt(uint32_t u32Mask) +{ + PS2->PS2CON |= u32Mask; +} + +/** + * @brief Disable PS/2 Interrupt. + * + * @param[in] u32Mask The specified interrupt of PS2 module: + * - PS2D_PS2CON_TXINTEN_Msk: PS2 Tx interrupt + * - PS2D_PS2CON_RXINTEN_Msk: PS2 Rx interrupt + * + * @return None + * + * @details The function is used to disable PS/2 specified Tx or Rx interrupt. + */ +void PS2_DisableInt(uint32_t u32Mask) +{ + PS2->PS2CON &= ~u32Mask; +} + +/** + * @brief Enable PS/2 function and Set Parameter + * + * @param None + * + * @return None + * + * @details This function is used to enable PS/2 function and set one byte per transfer. + */ +void PS2_Open(void) +{ + /* Reset PS2 device */ + SYS->IPRSTC2 |= SYS_IPRSTC2_PS2_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_PS2_RST_Msk; + + /* Enable PS2 module */ + PS2->PS2CON |= PS2_PS2CON_PS2EN_Msk; + + /* Set One byte per transfer */ + PS2->PS2CON &= ~PS2_PS2CON_TXFIFO_DEPTH_Msk; + + /* Clear Tx FIFO */ + PS2->PS2CON |= PS2_PS2CON_CLRFIFO_Msk; + PS2->PS2CON &= (~PS2_PS2CON_CLRFIFO_Msk); +} + +/** + * @brief Disable PS/2 function + * + * @param None + * + * @return None + * + * @details This function use to disable PS/2 function. + */ +void PS2_Close(void) +{ + /* Enable PS2 module */ + PS2->PS2CON &= ~PS2_PS2CON_PS2EN_Msk; +} + +/** + * @brief This function use to read PS/2 Rx data. + * + * @param None + * + * @return Rx data + * + * @details To get PS/2 receive 8 bits data from PS2RXDATA register. + */ +uint8_t PS2_Read(void) +{ + return (uint8_t)(PS2->PS2RXDATA & PS2_PS2RXDATA_RXDATA_Msk); +} + +/** + * @brief This function use to transmit PS/2 data. + * + * @param[in] pu32Buf The buffer to send the data to PS/2 transmission FIFO. + * @param[in] u32ByteCount The byte number of data. + * + * @retval 0 transmit data time-out + * @retval 1 transmit data successful + * + * @details Write data to PS/2 transmit FIFO and set the depth of Tx transmit bytes, then check every data transmission success or time-out. + */ +int32_t PS2_Write(uint32_t *pu32Buf, uint32_t u32ByteCount) +{ + uint32_t u32TxFIFO_Depth = 16; + uint32_t u32delayno, txcnt, remainder; + uint8_t i = 0; + + txcnt = u32ByteCount / u32TxFIFO_Depth; + + remainder = u32ByteCount % u32TxFIFO_Depth; + if(remainder) txcnt++; + + u32delayno = 0; + while(!(PS2->PS2STATUS & PS2_PS2STATUS_TXEMPTY_Msk)) + { + u32delayno++; + if(u32delayno >= 0xF00000) + return FALSE; // Time Out + } + + if(u32ByteCount >= u32TxFIFO_Depth)//Tx FIFO is 16 bytes + PS2_SET_TX_BYTE_CNT(u32TxFIFO_Depth); + + do + { + u32delayno = 0; + while(!(PS2->PS2STATUS & PS2_PS2STATUS_TXEMPTY_Msk)) + { + u32delayno++; + if(u32delayno >= 0xF00000) + return FALSE; // Time Out + } + + if((txcnt == 1) && (remainder != 0)) + PS2_SET_TX_BYTE_CNT(u32ByteCount); + + PS2->PS2TXDATA0 = pu32Buf[i]; + PS2->PS2TXDATA1 = pu32Buf[i + 1]; + PS2->PS2TXDATA2 = pu32Buf[i + 2]; + PS2->PS2TXDATA3 = pu32Buf[i + 3]; + + i = i + 4; + + } + while(--txcnt); + + u32delayno = 0; + while(!(PS2->PS2STATUS & PS2_PS2STATUS_TXEMPTY_Msk)) + { + u32delayno++; + if(u32delayno >= 0xF00000) + return FALSE; // Time Out + } + + return TRUE; + +} + + +/*@}*/ /* end of group PS2_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group PS2_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ + + + diff --git a/NUC123/StdDriver/src/pwm.c b/NUC123/StdDriver/src/pwm.c new file mode 100644 index 0000000..139aa1c --- /dev/null +++ b/NUC123/StdDriver/src/pwm.c @@ -0,0 +1,678 @@ +/**************************************************************************//** + * @file pwm.c + * @version V3.00 + * $Revision: 9 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series PWM driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup PWM_Driver PWM Driver + @{ +*/ + + +/** @addtogroup PWM_EXPORTED_FUNCTIONS PWM Exported Functions + @{ +*/ + +/** + * @brief Configure PWM capture and get the nearest unit time. + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32UnitTimeNsec The unit time of counter + * @param[in] u32CaptureEdge The condition to latch the counter. This parameter is not used + * @return The nearest unit time in nano second. + * @details This function is used to configure PWM capture and get the nearest unit time. + */ +uint32_t PWM_ConfigCaptureChannel(PWM_T *pwm, + uint32_t u32ChannelNum, + uint32_t u32UnitTimeNsec, + uint32_t u32CaptureEdge) +{ + uint32_t u32Src; + uint32_t u32PWMClockSrc; + uint32_t u32PWMClkTbl[8] = {__HXT, NULL, NULL, __HIRC, NULL, NULL, NULL, __LIRC}; + uint32_t u32NearestUnitTimeNsec; + uint8_t u8Divider = 1; + /* this table is mapping divider value to register configuration */ + uint32_t u32PWMDividerToRegTbl[17] = {NULL, 4, 0, NULL, 1, NULL, NULL, NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 3}; + uint16_t u16Prescale = 2; + uint16_t u16CNR = 0xFFFF; + + if(u32ChannelNum < 2)/* channel 0 and channel 1 */ + u32Src = ((CLK->CLKSEL2 & (CLK_CLKSEL2_PWM01_S_EXT_Msk)) >> (CLK_CLKSEL2_PWM01_S_EXT_Pos - 2)) | (CLK->CLKSEL1 & (CLK_CLKSEL1_PWM01_S_Msk)) >> (CLK_CLKSEL1_PWM01_S_Pos); + else /* channel 2 and channel 3 */ + u32Src = ((CLK->CLKSEL2 & (CLK_CLKSEL2_PWM23_S_EXT_Msk)) >> (CLK_CLKSEL2_PWM23_S_EXT_Pos - 2)) | (CLK->CLKSEL1 & (CLK_CLKSEL1_PWM23_S_Msk)) >> (CLK_CLKSEL1_PWM23_S_Pos); + + if(u32Src == 2) + { + SystemCoreClockUpdate(); + u32PWMClockSrc = SystemCoreClock; + } + else + { + u32PWMClockSrc = u32PWMClkTbl[u32Src]; + } + + u32PWMClockSrc /= 1000; + for(; u16Prescale <= 0x100; u16Prescale++) + { + u32NearestUnitTimeNsec = (1000000 * u16Prescale * u8Divider) / u32PWMClockSrc; + if(u32NearestUnitTimeNsec < (u32UnitTimeNsec)) + { + if((u16Prescale == 0x100) && (u8Divider == 16)) //limit to the maximum unit time(nano second) + break; + if(u16Prescale == 0x100) + { + u16Prescale = 2; + u8Divider <<= 1; // clk divider could only be 1, 2, 4, 8, 16 + continue; + } + if(!((1000000 * ((u16Prescale * u8Divider) + 1)) > (u32NearestUnitTimeNsec * u32PWMClockSrc))) + break; + continue; + } + break; + } + + // Store return value here 'cos we're gonna change u8Divider & u16Prescale & u16CNR to the real value to fill into register + u16Prescale -= 1; + + // convert to real register value + u8Divider = u32PWMDividerToRegTbl[u8Divider]; + + // every two channels share a prescaler + (pwm)->PPR = ((pwm)->PPR & ~(PWM_PPR_CP01_Msk << ((u32ChannelNum >> 1) * 8))) | (u16Prescale << ((u32ChannelNum >> 1) * 8)); + (pwm)->CSR = ((pwm)->CSR & ~(PWM_CSR_CSR0_Msk << (4 * u32ChannelNum))) | (u8Divider << (4 * u32ChannelNum)); + // set PWM to edge aligned type + (pwm)->PCR &= ~(PWM_PCR_PWM01TYPE_Msk << (u32ChannelNum >> 1)); + (pwm)->PCR |= PWM_PCR_CH0MOD_Msk << (8 * u32ChannelNum); + *((__IO uint32_t *)((((uint32_t) & ((pwm)->CNR0)) + u32ChannelNum * 12))) = u16CNR; + + return (u32NearestUnitTimeNsec); +} + +/** + * @brief Configure PWM generator and get the nearest frequency in edge aligned auto-reload mode + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Frequency Target generator frequency + * @param[in] u32DutyCycle Target generator duty cycle percentage. Valid range are between 0 ~ 100. 10 means 10%, 20 means 20%... + * @return Nearest frequency clock in nano second + * @details This function is used to configure PWM generator and get the nearest frequency in edge aligned auto-reload mode. + * @note Since every two channels, (0 & 1), (2 & 3), shares a prescaler. Call this API to configure PWM frequency may affect + * existing frequency of other channel. + */ +uint32_t PWM_ConfigOutputChannel(PWM_T *pwm, + uint32_t u32ChannelNum, + uint32_t u32Frequency, + uint32_t u32DutyCycle) +{ + uint32_t u32Src; + uint32_t u32PWMClockSrc; + uint32_t u32PWMClkTbl[8] = {__HXT, NULL, NULL, __HIRC, NULL, NULL, NULL, __LIRC}; + uint32_t i; + uint8_t u8Divider = 1, u8Prescale = 0xFF; + /* this table is mapping divider value to register configuration */ + uint32_t u32PWMDividerToRegTbl[17] = {NULL, 4, 0, NULL, 1, NULL, NULL, NULL, 2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 3}; + uint16_t u16CNR = 0xFFFF; + + if(u32ChannelNum < 2)/* channel 0 and channel 1 */ + u32Src = ((CLK->CLKSEL2 & (CLK_CLKSEL2_PWM01_S_EXT_Msk)) >> (CLK_CLKSEL2_PWM01_S_EXT_Pos - 2)) | (CLK->CLKSEL1 & (CLK_CLKSEL1_PWM01_S_Msk)) >> (CLK_CLKSEL1_PWM01_S_Pos); + else /* channel 2 and channel 3 */ + u32Src = ((CLK->CLKSEL2 & (CLK_CLKSEL2_PWM23_S_EXT_Msk)) >> (CLK_CLKSEL2_PWM23_S_EXT_Pos - 2)) | (CLK->CLKSEL1 & (CLK_CLKSEL1_PWM23_S_Msk)) >> (CLK_CLKSEL1_PWM23_S_Pos); + + if(u32Src == 2) + { + SystemCoreClockUpdate(); + u32PWMClockSrc = SystemCoreClock; + } + else + { + u32PWMClockSrc = u32PWMClkTbl[u32Src]; + } + + for(; u8Divider < 17; u8Divider <<= 1) // clk divider could only be 1, 2, 4, 8, 16 + { + i = (u32PWMClockSrc / (u32Frequency)) / u8Divider; + // If target value is larger than CNR * prescale, need to use a larger divider + if(i > (0x10000 * 0x100)) + continue; + + // CNR = 0xFFFF + 1, get a prescaler that CNR value is below 0xFFFF + u8Prescale = (i + 0xFFFF) / 0x10000; + + // u8Prescale must at least be 2, otherwise the output stop + if(u8Prescale < 3) + u8Prescale = 2; + + i /= u8Prescale; + + if(i <= 0x10000) + { + if(i == 1) + u16CNR = 1; // Too fast, and PWM cannot generate expected frequency... + else + u16CNR = i; + break; + } + } + // Store return value here 'cos we're gonna change u8Divider & u8Prescale & u16CNR to the real value to fill into register + i = u32PWMClockSrc / (u8Prescale * u8Divider * u16CNR); + + u8Prescale -= 1; + u16CNR -= 1; + // convert to real register value + u8Divider = u32PWMDividerToRegTbl[u8Divider]; + + // every two channels share a prescaler + (pwm)->PPR = ((pwm)->PPR & ~(PWM_PPR_CP01_Msk << ((u32ChannelNum >> 1) * 8))) | (u8Prescale << ((u32ChannelNum >> 1) * 8)); + (pwm)->CSR = ((pwm)->CSR & ~(PWM_CSR_CSR0_Msk << (4 * u32ChannelNum))) | (u8Divider << (4 * u32ChannelNum)); + // set PWM to edge aligned type + (pwm)->PCR &= ~(PWM_PCR_PWM01TYPE_Msk << (u32ChannelNum >> 1)); + (pwm)->PCR |= PWM_PCR_CH0MOD_Msk << (8 * u32ChannelNum); + + if(u32DutyCycle) + { + *((__IO uint32_t *)((((uint32_t) & ((pwm)->CMR0)) + u32ChannelNum * 12))) = u32DutyCycle * (u16CNR + 1) / 100 - 1; + } + else + { + *((__IO uint32_t *)((((uint32_t) & ((pwm)->CMR0)) + u32ChannelNum * 12))) = 0; + } + *((__IO uint32_t *)((((uint32_t) & ((pwm)->CNR0)) + u32ChannelNum * 12))) = u16CNR; + + return(i); +} + + +/** + * @brief Start PWM module + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Bit 0 is channel 0, bit 1 is channel 1... + * @return None + * @details This function is used to start PWM module. + */ +void PWM_Start(PWM_T *pwm, uint32_t u32ChannelMask) +{ + uint32_t u32Mask = 0, i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + u32Mask |= (PWM_PCR_CH0EN_Msk << (i * 8)); + } + } + (pwm)->PCR |= u32Mask; +} + +/** + * @brief Stop PWM module + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Bit 0 is channel 0, bit 1 is channel 1... + * @return None + * @details This function is used to stop PWM module. + */ +void PWM_Stop(PWM_T *pwm, uint32_t u32ChannelMask) +{ + uint32_t i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + *((__IO uint32_t *)((((uint32_t) & ((pwm)->CNR0)) + i * 12))) = 0; + } + } +} + +/** + * @brief Stop PWM generation immediately by clear channel enable bit + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Bit 0 is channel 0, bit 1 is channel 1... + * @return None + * @details This function is used to stop PWM generation immediately by clear channel enable bit. + */ +void PWM_ForceStop(PWM_T *pwm, uint32_t u32ChannelMask) +{ + uint32_t u32Mask = 0, i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + u32Mask |= (PWM_PCR_CH0EN_Msk << (i * 8)); + } + } + (pwm)->PCR &= ~u32Mask; +} + +/** + * @brief Enable selected channel to trigger ADC + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Condition This parameter is not used + * @return None + * @details This function is used to enable selected channel to trigger ADC. + * @note This function is only supported when PWM operating at Center-aligned type. + */ +void PWM_EnableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition) +{ + (pwm)->TCON |= (PWM_TCON_PWM0TEN_Msk << u32ChannelNum); +} + +/** + * @brief Disable selected channel to trigger ADC + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return None + * @details This function is used to disable selected channel to trigger ADC. + */ +void PWM_DisableADCTrigger(PWM_T *pwm, uint32_t u32ChannelNum) +{ + (pwm)->TCON = ((pwm)->TCON & ~(PWM_TCON_PWM0TEN_Msk << u32ChannelNum)); +} + +/** + * @brief Clear selected channel trigger ADC flag + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Condition This parameter is not used + * @return None + * @details This function is used to clear selected channel trigger ADC flag. + */ +void PWM_ClearADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Condition) +{ + (pwm)->TSTATUS = (PWM_TSTATUS_PWM0TF_Msk << u32ChannelNum); +} + +/** + * @brief Get selected channel trigger ADC flag + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @retval 0 The specified channel trigger ADC to start of conversion flag is not set + * @retval 1 The specified channel trigger ADC to start of conversion flag is set + * @details This function is used to get PWM trigger ADC to start of conversion flag for specified channel. + */ +uint32_t PWM_GetADCTriggerFlag(PWM_T *pwm, uint32_t u32ChannelNum) +{ + return (((pwm)->TSTATUS & (PWM_TSTATUS_PWM0TF_Msk << (u32ChannelNum))) ? 1 : 0); +} + +/** + * @brief Enable capture of selected channel(s) + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Bit 0 is channel 0, bit 1 is channel 1... + * @return None + * @details This function is used to enable capture of selected channel(s). + */ +void PWM_EnableCapture(PWM_T *pwm, uint32_t u32ChannelMask) +{ + uint32_t i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + if(i < 2) + { + (pwm)->CCR0 |= PWM_CCR0_CAPCH0EN_Msk << (i * 16); + } + else + { + (pwm)->CCR2 |= PWM_CCR2_CAPCH2EN_Msk << ((i - 2) * 16); + } + } + } + (pwm)->CAPENR |= u32ChannelMask; +} + +/** + * @brief Disable capture of selected channel(s) + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Bit 0 is channel 0, bit 1 is channel 1... + * @return None + * @details This function is used to disable capture of selected channel(s). + */ +void PWM_DisableCapture(PWM_T *pwm, uint32_t u32ChannelMask) +{ + uint32_t i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + if(i < 2) + { + (pwm)->CCR0 &= ~(PWM_CCR0_CAPCH0EN_Msk << (i * 16)); + } + else + { + (pwm)->CCR2 &= ~(PWM_CCR2_CAPCH2EN_Msk << ((i - 2) * 16)); + } + } + } + (pwm)->CAPENR &= ~u32ChannelMask; +} + +/** + * @brief Enables PWM output generation of selected channel(s) + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Set bit 0 to 1 enables channel 0 output, set bit 1 to 1 enables channel 1 output... + * @return None + * @details This function is used to enables PWM output generation of selected channel(s). + */ +void PWM_EnableOutput(PWM_T *pwm, uint32_t u32ChannelMask) +{ + (pwm)->POE |= u32ChannelMask; +} + +/** + * @brief Disables PWM output generation of selected channel(s) + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel + * Set bit 0 to 1 disables channel 0 output, set bit 1 to 1 disables channel 1 output... + * @return None + * @details This function is used to disables PWM output generation of selected channel(s). + */ +void PWM_DisableOutput(PWM_T *pwm, uint32_t u32ChannelMask) +{ + (pwm)->POE &= ~u32ChannelMask; +} + +/** + * @brief Enables PDMA transfer of selected channel(s) for PWM capture + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Set bit 0 to 1 enables channel 0 output, set bit 1 to 1 enables channel 1 output... + * @param[in] u32RisingFirst The capture order is rising, falling first. Every two channels share the same setting. + * @return None + * @details This function is used to enables PDMA transfer of selected channel(s) for PWM capture + */ +void PWM_EnablePDMA(PWM_T *pwm, uint32_t u32ChannelMask, uint32_t u32RisingFirst) +{ + uint32_t i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + (pwm)->CAPPDMACTL = ((pwm)->CAPPDMACTL & ~(PWM_CAPPDMACTL_CAP0RFORDER_Msk << (i * 8))) | \ + (((u32RisingFirst << PWM_CAPPDMACTL_CAP0RFORDER_Pos) | \ + PWM_CAPPDMACTL_CAP0PDMAMOD_Msk | PWM_CAPPDMACTL_CAP0PDMAEN_Msk) << (i * 8)); + } + } +} + +/** + * @brief Disables PDMA transfer of selected channel(s) for PWM capture + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelMask Combination of enabled channels. Each bit corresponds to a channel. + * Set bit 0 to 1 enables channel 0 output, set bit 1 to 1 enables channel 1 output... + * @return None + * @details This function is used to enables PDMA transfer of selected channel(s) for PWM capture + */ +void PWM_DisablePDMA(PWM_T *pwm, uint32_t u32ChannelMask) +{ + uint32_t i; + for(i = 0; i < PWM_CHANNEL_NUM; i ++) + { + if(u32ChannelMask & (1 << i)) + { + (pwm)->CAPPDMACTL &= ~(PWM_CAPPDMACTL_CAP0PDMAEN_Msk << (i * 8)); + } + } +} + +/** + * @brief Enable Dead zone of selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Duration Dead Zone length in PWM clock count, valid values are between 0~0xFF, but 0 means there is no + * dead zone. + * @return None + * @details This function is used to enable Dead zone of selected channel. + */ +void PWM_EnableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Duration) +{ + // every two channels shares the same setting + u32ChannelNum >>= 1; + // set duration + (pwm)->PPR = ((pwm)->PPR & ~(PWM_PPR_DZI01_Msk << (8 * u32ChannelNum))) | (u32Duration << (PWM_PPR_DZI01_Pos + 8 * u32ChannelNum)); + // enable dead zone + (pwm)->PCR |= (PWM_PCR_DZEN01_Msk << u32ChannelNum); +} + +/** + * @brief Disable Dead zone of selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return None + * @details This function is used to disable Dead zone of selected channel. + */ +void PWM_DisableDeadZone(PWM_T *pwm, uint32_t u32ChannelNum) +{ + // every two channels shares the same setting + u32ChannelNum >>= 1; + // enable dead zone + (pwm)->PCR &= ~(PWM_PCR_DZEN01_Msk << u32ChannelNum); +} + +/** + * @brief Enable capture interrupt of selected channel. + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Edge Rising or falling edge to latch counter. + * - \ref PWM_CAPTURE_INT_RISING_LATCH + * - \ref PWM_CAPTURE_INT_FALLING_LATCH + * @return None + * @details This function is used to enable capture interrupt of selected channel. + */ +void PWM_EnableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge) +{ + if(u32ChannelNum < 2) + (pwm)->CCR0 |= u32Edge << (u32ChannelNum * 16); + else + (pwm)->CCR2 |= u32Edge << ((u32ChannelNum - 2) * 16); + +} + +/** + * @brief Disable capture interrupt of selected channel. + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Edge Rising or falling edge to latch counter. + * - \ref PWM_CAPTURE_INT_RISING_LATCH + * - \ref PWM_CAPTURE_INT_FALLING_LATCH + * @return None + * @details This function is used to disable capture interrupt of selected channel. + */ +void PWM_DisableCaptureInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge) +{ + if(u32ChannelNum < 2) + (pwm)->CCR0 &= ~(u32Edge << (u32ChannelNum * 16)); + else + (pwm)->CCR2 &= ~(u32Edge << ((u32ChannelNum - 2) * 16)); +} + +/** + * @brief Clear capture interrupt of selected channel. + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32Edge Rising or falling edge to latch counter. + * - \ref PWM_CAPTURE_INT_RISING_LATCH + * - \ref PWM_CAPTURE_INT_FALLING_LATCH + * @return None + * @details This function is used to clear capture interrupt of selected channel. + */ +void PWM_ClearCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32Edge) +{ + //clear capture interrupt flag, and clear CRLR or CFLR latched indicator + if(u32ChannelNum < 2) + (pwm)->CCR0 = ((pwm)->CCR0 & PWM_CCR_MASK) | (PWM_CCR0_CAPIF0_Msk << (u32ChannelNum * 16)) | (u32Edge << (u32ChannelNum * 16 + 5)); + else + (pwm)->CCR2 = ((pwm)->CCR2 & PWM_CCR_MASK) | (PWM_CCR2_CAPIF2_Msk << ((u32ChannelNum - 2) * 16)) | (u32Edge << ((u32ChannelNum - 2) * 16 + 5)); +} + +/** + * @brief Get capture interrupt of selected channel. + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @retval 0 No capture interrupt + * @retval 1 Rising edge latch interrupt + * @retval 2 Falling edge latch interrupt + * @retval 3 Rising and falling latch interrupt + * @details This function is used to get capture interrupt of selected channel. + */ +uint32_t PWM_GetCaptureIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) +{ + if(u32ChannelNum < 2) + { + return (((pwm)->CCR0 & ((PWM_CCR0_CRLRI0_Msk | PWM_CCR0_CFLRI0_Msk) << (u32ChannelNum * 16))) >> (PWM_CCR0_CRLRI0_Pos + u32ChannelNum * 16)); + } + else + { + return (((pwm)->CCR2 & ((PWM_CCR2_CRLRI2_Msk | PWM_CCR2_CFLRI2_Msk) << ((u32ChannelNum - 2) * 16))) >> (PWM_CCR2_CRLRI2_Pos + (u32ChannelNum - 2) * 16)); + } + +} +/** + * @brief Enable duty interrupt of selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32IntDutyType This parameter is not used + * @return None + * @details This function is used to enable duty interrupt of selected channel. + */ +void PWM_EnableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntDutyType) +{ + (pwm)->PIER |= (PWM_PIER_PWMDIE0_Msk << u32ChannelNum); +} + +/** + * @brief Disable duty interrupt of selected channel + * @param[in] pwm The pointer of the specified PWM module + * - PWMA : PWM Group A + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return None + * @details This function is used to disable duty interrupt of selected channel. + */ +void PWM_DisableDutyInt(PWM_T *pwm, uint32_t u32ChannelNum) +{ + (pwm)->PIER &= ~(PWM_PIER_PWMDIE0_Msk << u32ChannelNum); +} + +/** + * @brief Clear duty interrupt flag of selected channel + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return None + * @details This function is used to clear duty interrupt flag of selected channel. + */ +void PWM_ClearDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) +{ + (pwm)->PIIR = PWM_PIIR_PWMDIF0_Msk << u32ChannelNum; +} + +/** + * @brief Get duty interrupt flag of selected channel + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @retval 0 Duty interrupt did not occur + * @retval 1 Duty interrupt occurred + * @details This function is used to get duty interrupt flag of selected channel. + */ +uint32_t PWM_GetDutyIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) +{ + return (((pwm)->PIIR & (PWM_PIIR_PWMDIF0_Msk << u32ChannelNum)) ? 1 : 0); +} + +/** + * @brief Enable period interrupt of selected channel + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @param[in] u32IntPeriodType Period interrupt type, could be either + * - \ref PWM_PERIOD_INT_UNDERFLOW + * - \ref PWM_PERIOD_INT_MATCH_CNR + * @return None + * @details This function is used to enable period interrupt of selected channel. + * Every two channels, (0 & 1), (2 & 3), shares the period interrupt type setting. + */ +void PWM_EnablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum, uint32_t u32IntPeriodType) +{ + (pwm)->PIER = ((pwm)->PIER & ~(PWM_PIER_INT01TYPE_Msk << (u32ChannelNum >> 1))) | \ + (PWM_PIER_PWMIE0_Msk << u32ChannelNum) | (u32IntPeriodType << (u32ChannelNum >> 1)); +} + +/** + * @brief Disable period interrupt of selected channel + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return None + * @details This function is used to disable period interrupt of selected channel. + */ +void PWM_DisablePeriodInt(PWM_T *pwm, uint32_t u32ChannelNum) +{ + (pwm)->PIER &= ~(PWM_PIER_PWMIE0_Msk << u32ChannelNum); +} + +/** + * @brief Clear period interrupt of selected channel + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @return None + * @details This function is used to clear period interrupt of selected channel. + */ +void PWM_ClearPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) +{ + (pwm)->PIIR = (PWM_PIIR_PWMIF0_Msk << u32ChannelNum); +} + +/** + * @brief Get period interrupt of selected channel + * @param[in] pwm The pointer of the specified PWM module + * @param[in] u32ChannelNum PWM channel number. Valid values are between 0~3 + * @retval 0 Period interrupt did not occur + * @retval 1 Period interrupt occurred + * @details This function is used to get period interrupt of selected channel. + */ +uint32_t PWM_GetPeriodIntFlag(PWM_T *pwm, uint32_t u32ChannelNum) +{ + return (((pwm)->PIIR & (PWM_PIIR_PWMIF0_Msk << (u32ChannelNum))) ? 1 : 0); +} + + + +/*@}*/ /* end of group PWM_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group PWM_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/retarget.c b/NUC123/StdDriver/src/retarget.c new file mode 100644 index 0000000..f49cc5c --- /dev/null +++ b/NUC123/StdDriver/src/retarget.c @@ -0,0 +1,761 @@ +/**************************************************************************//** + * @file retarget.c + * @version V3.00 + * $Revision: 13 $ + * $Date: 16/06/13 7:50p $ + * @brief NUC123 Series Debug Port and Semihost Setting Source File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ + + +#include +#include "NUC123.h" + +#if defined ( __CC_ARM ) +#if (__ARMCC_VERSION < 400000) +#else +/* Insist on keeping widthprec, to avoid X propagation by benign code in C-lib */ +#pragma import _printf_widthprec +#endif +#endif + +/*---------------------------------------------------------------------------------------------------------*/ +/* Global variables */ +/*---------------------------------------------------------------------------------------------------------*/ +#if !(defined(__ICCARM__) && (__VER__ >= 6010000)) || (defined(__ICCARM__) && (__VER__ >= 8000000)) +struct __FILE +{ + int handle; /* Add whatever you need here */ +}; +#endif +FILE __stdout; +FILE __stdin; + +enum { r0, r1, r2, r3, r12, lr, pc, psr}; + +/** + * @brief Helper function to dump register while hard fault occurred + * @param[in] stack pointer points to the dumped registers in SRAM + * @return None + * @details This function is implement to print r0, r1, r2, r3, r12, lr, pc, psr + */ +static void stackDump(uint32_t stack[]) +{ + printf("r0 = 0x%x\n", stack[r0]); + printf("r1 = 0x%x\n", stack[r1]); + printf("r2 = 0x%x\n", stack[r2]); + printf("r3 = 0x%x\n", stack[r3]); + printf("r12 = 0x%x\n", stack[r12]); + printf("lr = 0x%x\n", stack[lr]); + printf("pc = 0x%x\n", stack[pc]); + printf("psr = 0x%x\n", stack[psr]); +} + +/** + * @brief Hard fault handler + * @param[in] stack pointer points to the dumped registers in SRAM + * @return None + * @details Replace while(1) at the end of this function with chip reset if WDT is not enabled for end product + */ +void Hard_Fault_Handler(uint32_t stack[]) +{ + printf("In Hard Fault Handler\n"); + + stackDump(stack); + + // Replace while(1) with chip reset if WDT is not enabled for end product + while(1); + //SYS->IPRSTC1 = SYS_IPRSTC1_CHIP_RST_Msk; +} + +/*---------------------------------------------------------------------------------------------------------*/ +/* Routine to write a char */ +/*---------------------------------------------------------------------------------------------------------*/ + +#if defined(DEBUG_ENABLE_SEMIHOST) +/* The static buffer is used to speed up the semihost */ +static char g_buf[16]; +static char g_buf_len = 0; + +/* Make sure won't goes here only because --gnu is defined , so + add !__CC_ARM and !__ICCARM__ checking */ +# if defined ( __GNUC__ ) && !(__CC_ARM) && !(__ICCARM__) + +# elif defined(__ICCARM__) + + +void SH_End(void) +{ + asm("MOVS R0,#1 \n" //; Set return value to 1 + "BX lr \n" //; Return + ); +} + +void SH_ICE(void) +{ + asm("CMP R2,#0 \n" + "BEQ SH_End \n" + "STR R0,[R2] \n" //; Save the return value to *pn32Out_R0 + ); +} + +/** + * + * @brief The function to process semihosted command + * @param[in] n32In_R0 : semihost register 0 + * @param[in] n32In_R1 : semihost register 1 + * @param[out] pn32Out_R0: semihost register 0 + * @retval 0: No ICE debug + * @retval 1: ICE debug + * + */ +int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0) +{ + asm("BKPT 0xAB \n" //; This instruction will cause ICE trap or system HardFault + "B SH_ICE \n" + "SH_HardFault: \n" //; Captured by HardFault + "MOVS R0,#0 \n" //; Set return value to 0 + "BX lr \n" //; Return + ); + + return 1; //; Return 1 when it is trap by ICE +} + +/** + * @brief Get LR value and branch to Hard_Fault_Handler function + * @param None + * @return None + * @details This function is use to get LR value and branch to Hard_Fault_Handler function. + */ +void Get_LR_and_Branch(void) +{ + asm("MOV R1, LR \n" //; LR current value + "B Hard_Fault_Handler \n" + ); +} + +/** + * @brief Get MSP value and branch to Get_LR_and_Branch function + * @param None + * @return None + * @details This function is use to get stack pointer value and branch to Get_LR_and_Branch function. + */ +void Stack_Use_MSP(void) +{ + asm("MRS R0, MSP \n" //; read MSP + "B Get_LR_and_Branch \n" + ); +} + +/** + * @brief Get stack pointer value and branch to Get_LR_and_Branch function + * @param None + * @return None + * @details This function is use to get stack pointer value and branch to Get_LR_and_Branch function. + */ +void HardFault_Handler_Ret(void) +{ + asm("MOVS r0, #4 \n" + "MOV r1, LR \n" + "TST r0, r1 \n" //; check LR bit 2 + "BEQ Stack_Use_MSP \n" //; stack use MSP + "MRS R0, PSP \n" //; stack use PSP, read PSP + "B Get_LR_and_Branch \n" + ); +} + +/** + * @brief This function is implemented to support semihost + * @param None + * @returns None + * @details This function is implement to support semihost message print. + * + */ +void SP_Read_Ready(void) +{ + asm("LDR R1, [R0, #24] \n" //; Get previous PC + "LDRH R3, [R1] \n" //; Get instruction + "LDR R2, [pc, #8] \n" //; The special BKPT instruction + "CMP R3, R2 \n" //; Test if the instruction at previous PC is BKPT + "BNE HardFault_Handler_Ret \n" //; Not BKPT + "ADDS R1, #4 \n" //; Skip BKPT and next line + "STR R1, [R0, #24] \n" //; Save previous PC + "BX lr \n" //; Return + "DCD 0xBEAB \n" //; BKPT instruction code + "B HardFault_Handler_Ret \n" + ); +} + +/** + * @brief Get stack pointer value and branch to Get_LR_and_Branch function + * @param None + * @return None + * @details This function is use to get stack pointer value and branch to Get_LR_and_Branch function. + */ +void SP_is_PSP(void) +{ + asm( + "MRS R0, PSP \n" //; stack use PSP, read PSP + "B Get_LR_and_Branch \n" + + ); +} + +/** + * @brief This HardFault handler is implemented to support semihost + * + * @param None + * + * @returns None + * + * @details This function is implement to support semihost message print. + * + */ +void HardFault_Handler (void) +{ + asm("MOV R0, lr \n" + "LSLS R0, #29 \n" //; Check bit 2 + "BMI SP_is_PSP \n" //; previous stack is PSP + "MRS R0, MSP \n" //; previous stack is MSP, read MSP + "B SP_Read_Ready \n" + ); + + while(1); +} + + +# else + +/** + * @brief This HardFault handler is implemented to support semihost + * @param None + * @returns None + * @details This function is implement to support semihost message print. + * + */ +__asm int32_t HardFault_Handler(void) +{ + MOV R0, LR + LSLS R0, #29 //; Check bit 2 + BMI SP_is_PSP //; previous stack is PSP + MRS R0, MSP //; previous stack is MSP, read MSP + B SP_Read_Ready +SP_is_PSP + MRS R0, PSP //; Read PSP + +SP_Read_Ready + LDR R1, [R0, #24] //; Get previous PC + LDRH R3, [R1] //; Get instruction + LDR R2, =0xBEAB //; The special BKPT instruction + CMP R3, R2 //; Test if the instruction at previous PC is BKPT + BNE HardFault_Handler_Ret //; Not BKPT + + ADDS R1, #4 //; Skip BKPT and next line + STR R1, [R0, #24] //; Save previous PC + + BX LR //; Return +HardFault_Handler_Ret + + /* TODO: Implement your own hard fault handler here. */ + MOVS r0, #4 + MOV r1, LR + TST r0, r1 //; check LR bit 2 + BEQ Stack_Use_MSP //; stack use MSP + MRS R0, PSP ;stack use PSP //; stack use PSP, read PSP + B Get_LR_and_Branch +Stack_Use_MSP + MRS R0, MSP ; stack use MSP //; read MSP +Get_LR_and_Branch + MOV R1, LR ; LR current value //; LR current value + LDR R2,=__cpp(Hard_Fault_Handler) //; branch to Hard_Fault_Handler + BX R2 + + B . + + ALIGN +} + +/** + * + * @brief The function to process semihosted command + * @param[in] n32In_R0 : semihost register 0 + * @param[in] n32In_R1 : semihost register 1 + * @param[out] pn32Out_R0: semihost register 0 + * @retval 0: No ICE debug + * @retval 1: ICE debug + * + */ +__asm int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0) +{ + BKPT 0xAB //; Wait ICE or HardFault + //; ICE will step over BKPT directly + //; HardFault will step BKPT and the next line + B SH_ICE + +SH_HardFault //; Captured by HardFault + MOVS R0, #0 //; Set return value to 0 + BX lr //; Return + +SH_ICE //; Captured by ICE + //; Save return value + CMP R2, #0 + BEQ SH_End + STR R0, [R2] //; Save the return value to *pn32Out_R0 + +SH_End + MOVS R0, #1 //; Set return value to 1 + BX lr //; Return +} +#endif + + +#else + +/* Make sure won't goes here only because --gnu is defined , so + add !__CC_ARM and !__ICCARM__ checking */ +# if defined ( __GNUC__ ) && !(__CC_ARM) && !(__ICCARM__) + +/** + * @brief This HardFault handler is implemented to show r0, r1, r2, r3, r12, lr, pc, psr + * + * @param None + * + * @returns None + * + * @details This function is implement to print r0, r1, r2, r3, r12, lr, pc, psr. + * + */ +void HardFault_Handler(void) +{ + asm("MOVS r0, #4 \n" + "MOV r1, LR \n" + "TST r0, r1 \n" /*; check LR bit 2 */ + "BEQ 1f \n" /*; stack use MSP */ + "MRS R0, PSP \n" /*; stack use PSP, read PSP */ + "MOV R1, LR \n" /*; LR current value */ + "B Hard_Fault_Handler \n" + "1: \n" + "MRS R0, MSP \n" /*; LR current value */ + "B Hard_Fault_Handler \n" + ::[Hard_Fault_Handler] "r" (Hard_Fault_Handler) // input + ); + while(1); +} + +# elif defined(__ICCARM__) + + +void Get_LR_and_Branch(void) +{ + asm("MOV R1, LR \n" //; LR current value + "B Hard_Fault_Handler \n" + ); +} + +void Stack_Use_MSP(void) +{ + asm("MRS R0, MSP \n" //; read MSP + "B Get_LR_and_Branch \n" + ); +} + +/** + * @brief This HardFault handler is implemented to show r0, r1, r2, r3, r12, lr, pc, psr + * + * @param None + * + * @returns None + * + * @details This function is implement to print r0, r1, r2, r3, r12, lr, pc, psr. + * + */ +void HardFault_Handler(void) +{ + asm("MOVS r0, #4 \n" + "MOV r1, LR \n" + "TST r0, r1 \n" //; check LR bit 2 + "BEQ Stack_Use_MSP \n" //; stack use MSP + "MRS R0, PSP \n" //; stack use PSP, read PSP + "B Get_LR_and_Branch \n" + ); + + while(1); +} + +# else + +/** + * @brief This HardFault handler is implemented to show r0, r1, r2, r3, r12, lr, pc, psr + * + * @param None + * + * @return None + * + * @details The function extracts the location of stack frame and passes it to Hard_Fault_Handler function as a pointer + * + */ +__asm int32_t HardFault_Handler(void) +{ + MOVS r0, #4 + MOV r1, LR + TST r0, r1 //; check LR bit 2 + BEQ Stack_Use_MSP //; stack use MSP + MRS R0, PSP //; stack use PSP, read PSP + B Get_LR_and_Branch +Stack_Use_MSP + MRS R0, MSP //; read MSP +Get_LR_and_Branch + MOV R1, LR //; LR current value + LDR R2,=__cpp(Hard_Fault_Handler) //; branch to Hard_Fault_Handler + BX R2 +} + +#endif + +#endif + + + +/** + * @brief Routine to send a char + * + * @param[in] ch Character to send to debug port. + * + * @returns Send value from UART debug port + * + * @details Send a target char to UART debug port . + */ + +#ifndef NONBLOCK_PRINTF + +void SendChar_ToUART(int ch) +{ + + while(DEBUG_PORT->FSR & UART_FSR_TX_FULL_Msk); + if(ch == '\n') + { + DEBUG_PORT->DATA = '\r'; + while(DEBUG_PORT->FSR & UART_FSR_TX_FULL_Msk); + } + + DEBUG_PORT->DATA = ch; +} + +#else +/* Non-block implement of send char */ +#define BUF_SIZE 2048 +void SendChar_ToUART(int ch) +{ + static uint8_t u8Buf[BUF_SIZE] = {0}; + static int32_t i32Head = 0; + static int32_t i32Tail = 0; + int32_t i32Tmp; + + /* Only flush the data in buffer to UART when ch == 0 */ + if(ch) + { + // Push char + if(ch == '\n') + { + i32Tmp = i32Head + 1; + if(i32Tmp >= BUF_SIZE) i32Tmp = 0; + if(i32Tmp != i32Tail) + { + u8Buf[i32Head] = '\r'; + i32Head = i32Tmp; + } + } + + i32Tmp = i32Head + 1; + if(i32Tmp >= BUF_SIZE) i32Tmp = 0; + if(i32Tmp != i32Tail) + { + u8Buf[i32Head] = ch; + i32Head = i32Tmp; + } + } + else + { + if(i32Tail == i32Head) + return; + } + + // pop char + do + { + i32Tmp = i32Tail + 1; + if(i32Tmp > BUF_SIZE) i32Tmp = 0; + + if((DEBUG_PORT->FSR & UART_FSR_TX_FULL_Msk) == 0) + { + DEBUG_PORT->THR = u8Buf[i32Tail]; + i32Tail = i32Tmp; + } + else + break; // FIFO full + } + while(i32Tail != i32Head); +} +#endif + + + +/** + * @brief Routine to send a char + * + * @param[in] ch Character to send to debug port. + * + * @returns Send value from UART debug port or semihost + * + * @details Send a target char to UART debug port or semihost. + */ +void SendChar(int ch) +{ +#if defined(DEBUG_ENABLE_SEMIHOST) + g_buf[g_buf_len++] = ch; + g_buf[g_buf_len] = '\0'; + if(g_buf_len + 1 >= sizeof(g_buf) || ch == '\n' || ch == '\0') + { + /* Send the char */ + if(SH_DoCommand(0x04, (int)g_buf, NULL) != 0) + { + g_buf_len = 0; + return; + } + else + { + g_buf_len = 0; + } + } +#else + SendChar_ToUART(ch); +#endif +} + +/** + * @brief Routine to get a char + * + * @param None + * + * @returns Get value from UART debug port or semihost + * + * @details Wait UART debug port or semihost to input a char. + */ +char GetChar(void) +{ +#ifdef DEBUG_ENABLE_SEMIHOST +# if defined ( __CC_ARM ) + int nRet; + while(SH_DoCommand(0x101, 0, &nRet) != 0) + { + if(nRet != 0) + { + SH_DoCommand(0x07, 0, &nRet); + return (char)nRet; + } + } +# else + int nRet; + while(SH_DoCommand(0x7, 0, &nRet) != 0) + { + if(nRet != 0) + return (char)nRet; + } +# endif + return (0); +#else + + while(1) + { + if((DEBUG_PORT->FSR & UART_FSR_RX_EMPTY_Msk) == 0) + { + return (DEBUG_PORT->DATA); + } + } + +#endif +} + +/** + * @brief Check any char input from UART + * + * @param None + * + * @retval 1: No any char input + * @retval 0: Have some char input + * + * @details Check UART RSR RX EMPTY or not to determine if any char input from UART + */ + +int kbhit(void) +{ + return !((DEBUG_PORT->FSR & UART_FSR_RX_EMPTY_Msk) == 0); +} +/** + * @brief Check if debug message finished + * + * @param None + * + * @retval 1: Message is finished + * @retval 0: Message is transmitting. + * + * @details Check if message finished (FIFO empty of debug port) + */ + +int IsDebugFifoEmpty(void) +{ + return ((DEBUG_PORT->FSR & UART_FSR_TE_FLAG_Msk) != 0); +} + +/** + * @brief C library retargetting + * + * @param[in] ch Character to send to debug port. + * + * @returns None + * + * @details Check if message finished (FIFO empty of debug port) + */ + +void _ttywrch(int ch) +{ + SendChar(ch); + return; +} + + +/** + * @brief Write character to stream + * + * @param[in] ch Character to be written. The character is passed as its int promotion. + * @param[in] stream Pointer to a FILE object that identifies the stream where the character is to be written. + * + * @returns If there are no errors, the same character that has been written is returned. + * If an error occurs, EOF is returned and the error indicator is set (see ferror). + * + * @details Writes a character to the stream and advances the position indicator.\n + * The character is written at the current position of the stream as indicated \n + * by the internal position indicator, which is then advanced one character. + * + * @note The above descriptions are copied from http://www.cplusplus.com/reference/clibrary/cstdio/fputc/. + * + * + */ + +int fputc(int ch, FILE *stream) +{ + SendChar(ch); + return ch; +} + +#if defined ( __GNUC__ ) + +#if !defined (OS_USE_SEMIHOSTING) +int _write (int fd, char *ptr, int len) +{ + int i = len; + + while(i--) + { + while(DEBUG_PORT->FSR & UART_FSR_TX_FULL_Msk); + + if(*ptr == '\n') + { + DEBUG_PORT->DATA = '\r'; + while(DEBUG_PORT->FSR & UART_FSR_TX_FULL_Msk); + } + + DEBUG_PORT->DATA = *ptr++; + } + return len; +} + +int _read (int fd, char *ptr, int len) +{ + + while((DEBUG_PORT->FSR & UART_FSR_RX_EMPTY_Msk) != 0); + *ptr = DEBUG_PORT->DATA; + return 1; + + +} +#endif + +#else + +/** + * @brief Get character from UART debug port or semihosting input + * + * @param[in] stream Pointer to a FILE object that identifies the stream on which the operation is to be performed. + * + * @returns The character read from UART debug port or semihosting + * + * @details For get message from debug port or semihosting. + * + */ + +int fgetc(FILE *stream) +{ + return (GetChar()); +} + +/** + * @brief Check error indicator + * + * @param[in] stream Pointer to a FILE object that identifies the stream. + * + * @returns If the error indicator associated with the stream was set, the function returns a nonzero value. + * Otherwise, it returns a zero value. + * + * @details Checks if the error indicator associated with stream is set, returning a value different + * from zero if it is. This indicator is generally set by a previous operation on the stream that failed. + * + * @note The above descriptions are copied from http://www.cplusplus.com/reference/clibrary/cstdio/ferror/. + * + */ + +int ferror(FILE *stream) +{ + return EOF; +} + +#endif + +#ifdef DEBUG_ENABLE_SEMIHOST +# ifdef __ICCARM__ +void __exit(int return_code) +{ + + /* Check if link with ICE */ + if(SH_DoCommand(0x18, 0x20026, NULL) == 0) + { + /* Make sure all message is print out */ + while(IsDebugFifoEmpty() == 0); + } +label: + goto label; /* endless loop */ +} +# else +void _sys_exit(int return_code) +{ + + /* Check if link with ICE */ + if(SH_DoCommand(0x18, 0x20026, NULL) == 0) + { + /* Make sure all message is print out */ + while(IsDebugFifoEmpty() == 0); + } +label: + goto label; /* endless loop */ +} +# endif +#endif + + + + + diff --git a/NUC123/StdDriver/src/spi.c b/NUC123/StdDriver/src/spi.c new file mode 100644 index 0000000..d3bbcfb --- /dev/null +++ b/NUC123/StdDriver/src/spi.c @@ -0,0 +1,603 @@ +/**************************************************************************//** + * @file spi.c + * @version V3.00 + * $Revision: 8 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series SPI driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup SPI_Driver SPI Driver + @{ +*/ + + +/** @addtogroup SPI_EXPORTED_FUNCTIONS SPI Exported Functions + @{ +*/ + +/** + * @brief This function make SPI module be ready to transfer. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32MasterSlave Decides the SPI module is operating in master mode or in Slave mode. (SPI_SLAVE, SPI_MASTER) + * @param[in] u32SPIMode Decides the transfer timing. (SPI_MODE_0, SPI_MODE_1, SPI_MODE_2, SPI_MODE_3) + * @param[in] u32DataWidth Decides the data width of a SPI transaction. + * @param[in] u32BusClock The expected frequency of SPI bus clock in Hz. + * @return Actual frequency of SPI peripheral clock. + * @details By default, the SPI transfer sequence is MSB first and the automatic slave selection function is disabled. + * In Slave mode, the u32BusClock parameter is useless and the SPI clock divider setting will be 0. + * The actual clock rate may be different from the target SPI clock rate. + * For example, if the SPI source clock rate is 12 MHz and the target SPI bus clock rate is 7 MHz, the + * actual SPI clock rate will be 6MHz. + * @note If u32BusClock = 0, DIVIDER setting will be set to the maximum value. + * @note If u32BusClock >= system clock frequency, SPI peripheral clock source will be set to HCLK and DIVIDER will be set to 0. + * @note In Slave mode, the SPI peripheral clock rate will be set to equal the system clock rate, and slave selection signal is low level active. + */ +uint32_t SPI_Open(SPI_T *spi, + uint32_t u32MasterSlave, + uint32_t u32SPIMode, + uint32_t u32DataWidth, + uint32_t u32BusClock) +{ + uint32_t u32ClkSrc = 0, u32Div, u32HCLKFreq; + + if(u32DataWidth == 32) + u32DataWidth = 0; + + /* Default setting: MSB first, disable unit transfer interrupt, SP_CYCLE = 0. */ + spi->CNTRL = u32MasterSlave | (u32DataWidth << SPI_CNTRL_TX_BIT_LEN_Pos) | (u32SPIMode); + + /* Set BCn = 1: f_spi = f_spi_clk_src / (DIVIDER + 1) */ + spi->CNTRL2 |= SPI_CNTRL2_BCn_Msk; + /* Get system clock frequency */ + u32HCLKFreq = CLK_GetHCLKFreq(); + + if(u32MasterSlave == SPI_MASTER) + { + /* Default setting: slave select signal is active low; disable automatic slave select function. */ + spi->SSR = SPI_SS_ACTIVE_LOW; + + /* Check clock source of SPI */ + if(spi == SPI0) + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI0_S_Msk) == CLK_CLKSEL1_SPI0_S_HCLK) + u32ClkSrc = u32HCLKFreq; + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + else if(spi == SPI1) + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI1_S_Msk) == CLK_CLKSEL1_SPI1_S_HCLK) + u32ClkSrc = u32HCLKFreq; + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + else + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI2_S_Msk) == CLK_CLKSEL1_SPI2_S_HCLK) + u32ClkSrc = u32HCLKFreq; + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + + if(u32BusClock >= u32HCLKFreq) + { + /* Select HCLK as the clock source of SPI */ + if(spi == SPI0) + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI0_S_Msk)) | CLK_CLKSEL1_SPI0_S_HCLK; + else if(spi == SPI1) + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI1_S_Msk)) | CLK_CLKSEL1_SPI1_S_HCLK; + else + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI2_S_Msk)) | CLK_CLKSEL1_SPI2_S_HCLK; + + /* Set DIVIDER = 0 */ + spi->DIVIDER = 0; + /* Return slave peripheral clock rate */ + return u32HCLKFreq; + } + else if(u32BusClock >= u32ClkSrc) + { + /* Set DIVIDER = 0 */ + spi->DIVIDER = 0; + /* Return master peripheral clock rate */ + return u32ClkSrc; + } + else if(u32BusClock == 0) + { + /* Set BCn = 0: f_spi = f_spi_clk_src / ((DIVIDER + 1) * 2) */ + spi->CNTRL2 &= (~SPI_CNTRL2_BCn_Msk); + /* Set DIVIDER to the maximum value 0xFF */ + spi->DIVIDER = (spi->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | (0xFF << SPI_DIVIDER_DIVIDER_Pos); + /* Return master peripheral clock rate */ + return (u32ClkSrc / ((0xFF + 1) * 2)); + } + else + { + u32Div = (((u32ClkSrc * 10) / u32BusClock + 5) / 10) - 1; /* Round to the nearest integer */ + if(u32Div > 0xFF) + { + /* Set BCn = 0: f_spi = f_spi_clk_src / ((DIVIDER + 1) * 2) */ + spi->CNTRL2 &= (~SPI_CNTRL2_BCn_Msk); + u32Div = (((u32ClkSrc * 10) / (u32BusClock * 2) + 5) / 10) - 1; /* Round to the nearest integer */ + if(u32Div > 0xFF) + u32Div = 0xFF; + spi->DIVIDER = (spi->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | (u32Div << SPI_DIVIDER_DIVIDER_Pos); + /* Return master peripheral clock rate */ + return (u32ClkSrc / ((u32Div + 1) * 2)); + } + else + { + spi->DIVIDER = (spi->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | (u32Div << SPI_DIVIDER_DIVIDER_Pos); + /* Return master peripheral clock rate */ + return (u32ClkSrc / (u32Div + 1)); + } + } + + } + else /* For Slave mode, force the SPI peripheral clock rate to equal the system clock rate. */ + { + /* Default setting: slave selection signal is low level active. */ + spi->SSR = SPI_SSR_SS_LTRIG_Msk; + + /* Select HCLK as the clock source of SPI */ + if(spi == SPI0) + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI0_S_Msk)) | CLK_CLKSEL1_SPI0_S_HCLK; + else if(spi == SPI1) + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI1_S_Msk)) | CLK_CLKSEL1_SPI1_S_HCLK; + else + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI2_S_Msk)) | CLK_CLKSEL1_SPI2_S_HCLK; + + /* Set DIVIDER = 0 */ + spi->DIVIDER = 0; + /* Return slave peripheral clock rate */ + return u32HCLKFreq; + } + +} + +/** + * @brief Disable SPI controller. + * @param[in] spi The pointer of the specified SPI module. + * @return None + * @details This function will reset SPI controller. + */ +void SPI_Close(SPI_T *spi) +{ + if(spi == SPI0) + { + /* Reset SPI */ + SYS->IPRSTC2 |= SYS_IPRSTC2_SPI0_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_SPI0_RST_Msk; + } + else if(spi == SPI1) + { + /* Reset SPI */ + SYS->IPRSTC2 |= SYS_IPRSTC2_SPI1_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_SPI1_RST_Msk; + } + else + { + /* Reset SPI */ + SYS->IPRSTC2 |= SYS_IPRSTC2_SPI2_RST_Msk; + SYS->IPRSTC2 &= ~SYS_IPRSTC2_SPI2_RST_Msk; + } +} + +/** + * @brief Clear RX FIFO buffer. + * @param[in] spi The pointer of the specified SPI module. + * @return None + * @details This function will clear SPI RX FIFO buffer. + */ +void SPI_ClearRxFIFO(SPI_T *spi) +{ + spi->FIFO_CTL |= SPI_FIFO_CTL_RX_CLR_Msk; +} + +/** + * @brief Clear TX FIFO buffer. + * @param[in] spi The pointer of the specified SPI module. + * @return None + * @details This function will clear SPI TX FIFO buffer. + */ +void SPI_ClearTxFIFO(SPI_T *spi) +{ + spi->FIFO_CTL |= SPI_FIFO_CTL_TX_CLR_Msk; +} + +/** + * @brief Disable the automatic slave selection function. + * @param[in] spi The pointer of the specified SPI module. + * @return None + * @details This function will disable the automatic slave selection function and set slave selection signal to inactive state. + */ +void SPI_DisableAutoSS(SPI_T *spi) +{ + spi->SSR &= ~(SPI_SSR_AUTOSS_Msk | SPI_SSR_SSR_Msk); +} + +/** + * @brief Enable the automatic slave selection function. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32SSPinMask Specifies slave selection pins. (SPI_SS0, SPI_SS1) + * @param[in] u32ActiveLevel Specifies the active level of slave selection signal. (SPI_SS_ACTIVE_HIGH, SPI_SS_ACTIVE_LOW) + * @return None + * @details This function will enable the automatic slave selection function. Only available in Master mode. + * The slave selection pin and the active level will be set in this function. + */ +void SPI_EnableAutoSS(SPI_T *spi, uint32_t u32SSPinMask, uint32_t u32ActiveLevel) +{ + spi->SSR = (spi->SSR & (~(SPI_SSR_AUTOSS_Msk | SPI_SSR_SS_LVL_Msk | SPI_SSR_SSR_Msk))) | (u32SSPinMask | u32ActiveLevel | SPI_SSR_AUTOSS_Msk); +} + +/** + * @brief Set the SPI bus clock. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32BusClock The expected frequency of SPI bus clock in Hz. + * @return Actual frequency of SPI bus clock. + * @details This function is only available in Master mode. The actual clock rate may be different from the target SPI bus clock rate. + * For example, if the SPI source clock rate is 12MHz and the target SPI bus clock rate is 7MHz, the actual SPI bus clock + * rate will be 6MHz. + * @note If u32BusClock = 0, DIVIDER setting will be set to the maximum value. + * @note If u32BusClock >= system clock frequency, SPI peripheral clock source will be set to HCLK and DIVIDER will be set to 0. + */ +uint32_t SPI_SetBusClock(SPI_T *spi, uint32_t u32BusClock) +{ + uint32_t u32ClkSrc, u32HCLKFreq; + uint32_t u32Div; + + /* Set BCn = 1: f_spi = f_spi_clk_src / (DIVIDER + 1) */ + spi->CNTRL2 |= SPI_CNTRL2_BCn_Msk; + /* Get system clock frequency */ + u32HCLKFreq = CLK_GetHCLKFreq(); + + /* Check clock source of SPI */ + if(spi == SPI0) + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI0_S_Msk) == CLK_CLKSEL1_SPI0_S_HCLK) + u32ClkSrc = u32HCLKFreq; + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + else if(spi == SPI1) + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI1_S_Msk) == CLK_CLKSEL1_SPI1_S_HCLK) + u32ClkSrc = u32HCLKFreq; + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + else + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI2_S_Msk) == CLK_CLKSEL1_SPI2_S_HCLK) + u32ClkSrc = u32HCLKFreq; + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + + if(u32BusClock >= u32HCLKFreq) + { + /* Select HCLK as the clock source of SPI */ + if(spi == SPI0) + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI0_S_Msk)) | CLK_CLKSEL1_SPI0_S_HCLK; + else if(spi == SPI1) + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI1_S_Msk)) | CLK_CLKSEL1_SPI1_S_HCLK; + else + CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_SPI2_S_Msk)) | CLK_CLKSEL1_SPI2_S_HCLK; + + /* Set DIVIDER = 0 */ + spi->DIVIDER = 0; + /* Return slave peripheral clock rate */ + return u32HCLKFreq; + } + else if(u32BusClock >= u32ClkSrc) + { + /* Set DIVIDER = 0 */ + spi->DIVIDER = 0; + /* Return master peripheral clock rate */ + return u32ClkSrc; + } + else if(u32BusClock == 0) + { + /* Set BCn = 0: f_spi = f_spi_clk_src / ((DIVIDER + 1) * 2) */ + spi->CNTRL2 &= (~SPI_CNTRL2_BCn_Msk); + /* Set DIVIDER to the maximum value 0xFF */ + spi->DIVIDER = (spi->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | (0xFF << SPI_DIVIDER_DIVIDER_Pos); + /* Return master peripheral clock rate */ + return (u32ClkSrc / ((0xFF + 1) * 2)); + } + else + { + u32Div = (((u32ClkSrc * 10) / u32BusClock + 5) / 10) - 1; /* Round to the nearest integer */ + if(u32Div > 0xFF) + { + /* Set BCn = 0: f_spi = f_spi_clk_src / ((DIVIDER + 1) * 2) */ + spi->CNTRL2 &= (~SPI_CNTRL2_BCn_Msk); + u32Div = (((u32ClkSrc * 10) / (u32BusClock * 2) + 5) / 10) - 1; /* Round to the nearest integer */ + if(u32Div > 0xFF) + u32Div = 0xFF; + spi->DIVIDER = (spi->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | (u32Div << SPI_DIVIDER_DIVIDER_Pos); + /* Return master peripheral clock rate */ + return (u32ClkSrc / ((u32Div + 1) * 2)); + } + else + { + spi->DIVIDER = (spi->DIVIDER & (~SPI_DIVIDER_DIVIDER_Msk)) | (u32Div << SPI_DIVIDER_DIVIDER_Pos); + /* Return master peripheral clock rate */ + return (u32ClkSrc / (u32Div + 1)); + } + } +} + +/** + * @brief Enable FIFO mode. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32TxThreshold Decides the TX FIFO threshold. + * @param[in] u32RxThreshold Decides the RX FIFO threshold. + * @return None + * @details Enable FIFO mode with user-specified TX FIFO threshold and RX FIFO threshold configurations. + */ +void SPI_EnableFIFO(SPI_T *spi, uint32_t u32TxThreshold, uint32_t u32RxThreshold) +{ + spi->FIFO_CTL = (spi->FIFO_CTL & ~(SPI_FIFO_CTL_TX_THRESHOLD_Msk | SPI_FIFO_CTL_RX_THRESHOLD_Msk) | + (u32TxThreshold << SPI_FIFO_CTL_TX_THRESHOLD_Pos) | + (u32RxThreshold << SPI_FIFO_CTL_RX_THRESHOLD_Pos)); + + spi->CNTRL |= SPI_CNTRL_FIFO_Msk; +} + +/** + * @brief Disable FIFO mode. + * @param[in] spi The pointer of the specified SPI module. + * @return None + * @details Clear FIFO bit of SPI_CNTRL register to disable FIFO mode. + */ +void SPI_DisableFIFO(SPI_T *spi) +{ + spi->CNTRL &= ~SPI_CNTRL_FIFO_Msk; +} + +/** + * @brief Get the actual frequency of SPI bus clock. Only available in Master mode. + * @param[in] spi The pointer of the specified SPI module. + * @return Actual SPI bus clock frequency. + * @details This function will calculate the actual SPI bus clock rate according to the settings of SPIn_S, BCn and DIVIDER. Only available in Master mode. + */ +uint32_t SPI_GetBusClock(SPI_T *spi) +{ + uint32_t u32Div; + uint32_t u32ClkSrc; + + /* Get DIVIDER setting */ + u32Div = (spi->DIVIDER & SPI_DIVIDER_DIVIDER_Msk) >> SPI_DIVIDER_DIVIDER_Pos; + + /* Check clock source of SPI */ + if(spi == SPI0) + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI0_S_Msk) == CLK_CLKSEL1_SPI0_S_HCLK) + u32ClkSrc = CLK_GetHCLKFreq(); + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + else if(spi == SPI1) + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI1_S_Msk) == CLK_CLKSEL1_SPI1_S_HCLK) + u32ClkSrc = CLK_GetHCLKFreq(); + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + else + { + if((CLK->CLKSEL1 & CLK_CLKSEL1_SPI2_S_Msk) == CLK_CLKSEL1_SPI2_S_HCLK) + u32ClkSrc = CLK_GetHCLKFreq(); + else + u32ClkSrc = CLK_GetPLLClockFreq(); + } + + if(spi->CNTRL2 & SPI_CNTRL2_BCn_Msk) /* BCn = 1: f_spi = f_spi_clk_src / (DIVIDER + 1) */ + { + /* Return SPI bus clock rate */ + return (u32ClkSrc / (u32Div + 1)); + } + else /* BCn = 0: f_spi = f_spi_clk_src / ((DIVIDER + 1) * 2) */ + { + /* Return SPI bus clock rate */ + return (u32ClkSrc / ((u32Div + 1) * 2)); + } +} + +/** + * @brief Enable interrupt function. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32Mask The combination of all related interrupt enable bits. + * Each bit corresponds to a interrupt bit. + * This parameter decides which interrupts will be enabled. + * (SPI_UNIT_INT_MASK, SPI_SSTA_INT_MASK, SPI_FIFO_TX_INT_MASK, + * SPI_FIFO_RX_INT_MASK, SPI_FIFO_RXOV_INT_MASK, SPI_FIFO_TIMEOUT_INT_MASK) + * @return None + * @details Enable SPI related interrupts specified by u32Mask parameter. + */ +void SPI_EnableInt(SPI_T *spi, uint32_t u32Mask) +{ + /* Enable unit transfer interrupt flag */ + if((u32Mask & SPI_UNIT_INT_MASK) == SPI_UNIT_INT_MASK) + spi->CNTRL |= SPI_CNTRL_IE_Msk; + + /* Enable slave 3-wire mode start interrupt flag */ + if((u32Mask & SPI_SSTA_INT_MASK) == SPI_SSTA_INT_MASK) + spi->CNTRL2 |= SPI_CNTRL2_SSTA_INTEN_Msk; + + /* Enable TX threshold interrupt flag */ + if((u32Mask & SPI_FIFO_TX_INT_MASK) == SPI_FIFO_TX_INT_MASK) + spi->FIFO_CTL |= SPI_FIFO_CTL_TX_INTEN_Msk; + + /* Enable RX threshold interrupt flag */ + if((u32Mask & SPI_FIFO_RX_INT_MASK) == SPI_FIFO_RX_INT_MASK) + spi->FIFO_CTL |= SPI_FIFO_CTL_RX_INTEN_Msk; + + /* Enable RX overrun interrupt flag */ + if((u32Mask & SPI_FIFO_RXOV_INT_MASK) == SPI_FIFO_RXOV_INT_MASK) + spi->FIFO_CTL |= SPI_FIFO_CTL_RXOV_INTEN_Msk; + + /* Enable RX time-out interrupt flag */ + if((u32Mask & SPI_FIFO_TIMEOUT_INT_MASK) == SPI_FIFO_TIMEOUT_INT_MASK) + spi->FIFO_CTL |= SPI_FIFO_CTL_TIMEOUT_INTEN_Msk; +} + +/** + * @brief Disable interrupt function. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32Mask The combination of all related interrupt enable bits. + * Each bit corresponds to a interrupt bit. + * This parameter decides which interrupts will be disabled. + * (SPI_UNIT_INT_MASK, SPI_SSTA_INT_MASK, SPI_FIFO_TX_INT_MASK, + * SPI_FIFO_RX_INT_MASK, SPI_FIFO_RXOV_INT_MASK, SPI_FIFO_TIMEOUT_INT_MASK) + * @return None + * @details Disable SPI related interrupts specified by u32Mask parameter. + */ +void SPI_DisableInt(SPI_T *spi, uint32_t u32Mask) +{ + /* Disable unit transfer interrupt flag */ + if((u32Mask & SPI_UNIT_INT_MASK) == SPI_UNIT_INT_MASK) + spi->CNTRL &= ~SPI_CNTRL_IE_Msk; + + /* Disable slave 3-wire mode start interrupt flag */ + if((u32Mask & SPI_SSTA_INT_MASK) == SPI_SSTA_INT_MASK) + spi->CNTRL2 &= ~SPI_CNTRL2_SSTA_INTEN_Msk; + + /* Disable TX threshold interrupt flag */ + if((u32Mask & SPI_FIFO_TX_INT_MASK) == SPI_FIFO_TX_INT_MASK) + spi->FIFO_CTL &= ~SPI_FIFO_CTL_TX_INTEN_Msk; + + /* Disable RX threshold interrupt flag */ + if((u32Mask & SPI_FIFO_RX_INT_MASK) == SPI_FIFO_RX_INT_MASK) + spi->FIFO_CTL &= ~SPI_FIFO_CTL_RX_INTEN_Msk; + + /* Disable RX overrun interrupt flag */ + if((u32Mask & SPI_FIFO_RXOV_INT_MASK) == SPI_FIFO_RXOV_INT_MASK) + spi->FIFO_CTL &= ~SPI_FIFO_CTL_RXOV_INTEN_Msk; + + /* Disable RX time-out interrupt flag */ + if((u32Mask & SPI_FIFO_TIMEOUT_INT_MASK) == SPI_FIFO_TIMEOUT_INT_MASK) + spi->FIFO_CTL &= ~SPI_FIFO_CTL_TIMEOUT_INTEN_Msk; +} + +/** + * @brief Get interrupt flag. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32Mask The combination of all related interrupt sources. + * Each bit corresponds to a interrupt source. + * This parameter decides which interrupt flags will be read. + * (SPI_UNIT_INT_MASK, SPI_SSTA_INT_MASK, SPI_FIFO_TX_INT_MASK, + * SPI_FIFO_RX_INT_MASK, SPI_FIFO_RXOV_INT_MASK, SPI_FIFO_TIMEOUT_INT_MASK) + * @return Interrupt flags of selected sources. + * @details Get SPI related interrupt flags specified by u32Mask parameter. + */ +uint32_t SPI_GetIntFlag(SPI_T *spi, uint32_t u32Mask) +{ + uint32_t u32IntFlag = 0; + + /* Check unit transfer interrupt flag */ + if((u32Mask & SPI_UNIT_INT_MASK) && (spi->CNTRL & SPI_CNTRL_IF_Msk)) + u32IntFlag |= SPI_UNIT_INT_MASK; + + /* Check slave 3-wire mode start interrupt flag */ + if((u32Mask & SPI_SSTA_INT_MASK) && (spi->CNTRL2 & SPI_CNTRL2_SLV_START_INTSTS_Msk)) + u32IntFlag |= SPI_SSTA_INT_MASK; + + /* Check TX threshold interrupt flag */ + if((u32Mask & SPI_FIFO_TX_INT_MASK) && (spi->STATUS & SPI_STATUS_TX_INTSTS_Msk)) + u32IntFlag |= SPI_FIFO_TX_INT_MASK; + + /* Check RX threshold interrupt flag */ + if((u32Mask & SPI_FIFO_RX_INT_MASK) && (spi->STATUS & SPI_STATUS_RX_INTSTS_Msk)) + u32IntFlag |= SPI_FIFO_RX_INT_MASK; + + /* Check RX overrun interrupt flag */ + if((u32Mask & SPI_FIFO_RXOV_INT_MASK) && (spi->STATUS & SPI_STATUS_RX_OVERRUN_Msk)) + u32IntFlag |= SPI_FIFO_RXOV_INT_MASK; + + /* Check RX time-out interrupt flag */ + if((u32Mask & SPI_FIFO_TIMEOUT_INT_MASK) && (spi->STATUS & SPI_STATUS_TIMEOUT_Msk)) + u32IntFlag |= SPI_FIFO_TIMEOUT_INT_MASK; + + return u32IntFlag; +} + +/** + * @brief Clear interrupt flag. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32Mask The combination of all related interrupt sources. + * Each bit corresponds to a interrupt source. + * This parameter decides which interrupt flags will be cleared. + * (SPI_UNIT_INT_MASK, SPI_SSTA_INT_MASK, + * SPI_FIFO_RXOV_INT_MASK, SPI_FIFO_TIMEOUT_INT_MASK) + * @return None + * @details Clear SPI related interrupt flags specified by u32Mask parameter. + */ +void SPI_ClearIntFlag(SPI_T *spi, uint32_t u32Mask) +{ + if(u32Mask & SPI_UNIT_INT_MASK) + spi->CNTRL |= SPI_CNTRL_IF_Msk; /* Clear unit transfer interrupt flag */ + + if(u32Mask & SPI_SSTA_INT_MASK) + spi->CNTRL2 |= SPI_CNTRL2_SLV_START_INTSTS_Msk; /* Clear slave 3-wire mode start interrupt flag */ + + if(u32Mask & SPI_FIFO_RXOV_INT_MASK) + spi->STATUS = SPI_STATUS_RX_OVERRUN_Msk; /* Clear RX overrun interrupt flag */ + + if(u32Mask & SPI_FIFO_TIMEOUT_INT_MASK) + spi->STATUS = SPI_STATUS_TIMEOUT_Msk; /* Clear RX time-out interrupt flag */ +} + +/** + * @brief Get SPI status. + * @param[in] spi The pointer of the specified SPI module. + * @param[in] u32Mask The combination of all related sources. + * Each bit corresponds to a source. + * This parameter decides which flags will be read. + * (SPI_BUSY_MASK, SPI_RX_EMPTY_MASK, SPI_RX_FULL_MASK, + * SPI_TX_EMPTY_MASK, SPI_TX_FULL_MASK) + * @return Flags of selected sources. + * @details Get SPI related status specified by u32Mask parameter. + */ +uint32_t SPI_GetStatus(SPI_T *spi, uint32_t u32Mask) +{ + uint32_t u32Flag = 0; + + /* Check busy status */ + if((u32Mask & SPI_BUSY_MASK) && (spi->CNTRL & SPI_CNTRL_GO_BUSY_Msk)) + u32Flag |= SPI_BUSY_MASK; + + /* Check RX empty flag */ + if((u32Mask & SPI_RX_EMPTY_MASK) && (spi->CNTRL & SPI_CNTRL_RX_EMPTY_Msk)) + u32Flag |= SPI_RX_EMPTY_MASK; + + /* Check RX full flag */ + if((u32Mask & SPI_RX_FULL_MASK) && (spi->CNTRL & SPI_CNTRL_RX_FULL_Msk)) + u32Flag |= SPI_RX_FULL_MASK; + + /* Check TX empty flag */ + if((u32Mask & SPI_TX_EMPTY_MASK) && (spi->CNTRL & SPI_CNTRL_TX_EMPTY_Msk)) + u32Flag |= SPI_TX_EMPTY_MASK; + + /* Check TX full flag */ + if((u32Mask & SPI_TX_FULL_MASK) && (spi->CNTRL & SPI_CNTRL_TX_FULL_Msk)) + u32Flag |= SPI_TX_FULL_MASK; + + return u32Flag; +} + +/*@}*/ /* end of group SPI_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group SPI_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/sys.c b/NUC123/StdDriver/src/sys.c new file mode 100644 index 0000000..e7817fa --- /dev/null +++ b/NUC123/StdDriver/src/sys.c @@ -0,0 +1,205 @@ +/**************************************************************************//** + * @file sys.c + * @version V3.00 + * $Revision: 7 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series SYS driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ + +#include "NUC123.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup SYS_Driver SYS Driver + @{ +*/ + + +/** @addtogroup SYS_EXPORTED_FUNCTIONS SYS Exported Functions + @{ +*/ + +/** + * @brief Clear reset source + * @param[in] u32Src is system reset source. Including : + * - \ref SYS_RSTSRC_RSTS_CPU_Msk + * - \ref SYS_RSTSRC_RSTS_SYS_Msk + * - \ref SYS_RSTSRC_RSTS_BOD_Msk + * - \ref SYS_RSTSRC_RSTS_LVR_Msk + * - \ref SYS_RSTSRC_RSTS_WDT_Msk + * - \ref SYS_RSTSRC_RSTS_RESET_Msk + * - \ref SYS_RSTSRC_RSTS_POR_Msk + * @return None + * @details This function clear the selected system reset source. + */ +void SYS_ClearResetSrc(uint32_t u32Src) +{ + SYS->RSTSRC |= u32Src; +} + +/** + * @brief Get Brown-out detector output status + * @param None + * @retval 0 System voltage is higher than BOD_VL setting or BOD_EN is 0. + * @retval 1 System voltage is lower than BOD_VL setting. + * @details This function get Brown-out detector output status. + */ +uint32_t SYS_GetBODStatus(void) +{ + return (SYS->BODCR & SYS_BODCR_BOD_OUT_Msk) ? 1 : 0; +} + +/** + * @brief Get reset status register value + * @param None + * @return Reset source + * @details This function get the system reset status register value. + */ +uint32_t SYS_GetResetSrc(void) +{ + return (SYS->RSTSRC); +} + +/** + * @brief Check if register lock is set + * @param None + * @retval 0 Write-protection function is disabled. + * @retval 1 Write-protection function is enabled. + * @details This function check register write-protection bit setting. + */ +uint32_t SYS_IsRegLocked(void) +{ + return !(SYS->REGWRPROT & SYS_REGWRPROT_REGPROTDIS_Msk); +} + +/** + * @brief Get product ID + * @param None + * @return Product ID + * @details This function get product ID. + */ +uint32_t SYS_ReadPDID(void) +{ + return SYS->PDID; +} + +/** + * @brief Reset chip with chip reset + * @param None + * @return None + * @details This function reset chip with chip reset. + * The register write-protection function should be disabled before using this function. + */ +void SYS_ResetChip(void) +{ + SYS->IPRSTC1 |= SYS_IPRSTC1_CHIP_RST_Msk; +} + +/** + * @brief Reset chip with CPU reset + * @param None + * @return None + * @details This function reset CPU with CPU reset. + * The register write-protection function should be disabled before using this function. + */ +void SYS_ResetCPU(void) +{ + SYS->IPRSTC1 |= SYS_IPRSTC1_CPU_RST_Msk; +} + +/** + * @brief Reset Module + * @param[in] u32ModuleIndex is module index. Including : + * - \ref PDMA_RST + * - \ref GPIO_RST + * - \ref TMR0_RST + * - \ref TMR1_RST + * - \ref TMR2_RST + * - \ref TMR3_RST + * - \ref I2C0_RST + * - \ref I2C1_RST + * - \ref SPI0_RST + * - \ref SPI1_RST + * - \ref SPI2_RST + * - \ref UART0_RST + * - \ref UART1_RST + * - \ref PWM03_RST + * - \ref PS2_RST + * - \ref USBD_RST + * - \ref ADC_RST + * - \ref I2S_RST + * @return None + * @details This function reset selected modules. + */ +void SYS_ResetModule(uint32_t u32ModuleIndex) +{ + /* Generate reset signal to the corresponding module */ + *(volatile uint32_t *)((uint32_t)&SYS->IPRSTC1 + (u32ModuleIndex >> 24)) |= 1 << (u32ModuleIndex & 0x00ffffff); + + /* Release corresponding module from reset state */ + *(volatile uint32_t *)((uint32_t)&SYS->IPRSTC1 + (u32ModuleIndex >> 24)) &= ~(1 << (u32ModuleIndex & 0x00ffffff)); +} + +/** + * @brief Enable and set Brown-out detector function + * @param[in] i32Mode is reset or interrupt mode. Including : + * - \ref SYS_BODCR_BOD_RST_EN + * - \ref SYS_BODCR_BOD_INTERRUPT_EN + * @param[in] u32BODLevel is Brown-out voltage level. Including : + * - \ref SYS_BODCR_BOD_VL_4_5V + * - \ref SYS_BODCR_BOD_VL_3_8V + * - \ref SYS_BODCR_BOD_VL_2_7V + * - \ref SYS_BODCR_BOD_VL_2_2V + * @return None + * @details This function configure Brown-out detector reset or interrupt mode, enable Brown-out function and set Brown-out voltage level. + * The register write-protection function should be disabled before using this function. + * + */ +void SYS_EnableBOD(int32_t i32Mode, uint32_t u32BODLevel) +{ + /* Enable Brown-out Detector function */ + SYS->BODCR |= SYS_BODCR_BOD_EN_Msk; + + /* Enable Brown-out interrupt or reset function */ + SYS->BODCR = (SYS->BODCR & ~SYS_BODCR_BOD_RSTEN_Msk) | i32Mode; + + /* Select Brown-out Detector threshold voltage */ + SYS->BODCR = (SYS->BODCR & ~SYS_BODCR_BOD_VL_Msk) | u32BODLevel; +} + +/** + * @brief Disable Brown-out detector function + * @param None + * @return None + * @details This function disable Brown-out detector function. + * The register write-protection function should be disabled before using this function. + */ +void SYS_DisableBOD(void) +{ + SYS->BODCR &= ~SYS_BODCR_BOD_EN_Msk; +} + + + +/*@}*/ /* end of group SYS_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group SYS_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +#ifdef __cplusplus +} +#endif + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/timer.c b/NUC123/StdDriver/src/timer.c new file mode 100644 index 0000000..67821d8 --- /dev/null +++ b/NUC123/StdDriver/src/timer.c @@ -0,0 +1,269 @@ +/**************************************************************************//** + * @file timer.c + * @version V3.00 + * $Revision: 6 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series Timer driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup TIMER_Driver TIMER Driver + @{ +*/ + +/** @addtogroup TIMER_EXPORTED_FUNCTIONS TIMER Exported Functions + @{ +*/ + +/** + * @brief Open Timer in specified mode and frequency + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * @param[in] u32Mode Operation mode. Possible options are + * - \ref TIMER_ONESHOT_MODE + * - \ref TIMER_PERIODIC_MODE + * - \ref TIMER_TOGGLE_MODE + * - \ref TIMER_CONTINUOUS_MODE + * @param[in] u32Freq Target working frequency + * + * @return Real Timer working frequency + * + * @details This API is used to configure timer to operate in specified mode and frequency. + * If timer cannot work in target frequency, a closest frequency will be chose and returned. + * @note After calling this API, Timer is \b NOT running yet. But could start timer running be calling + * \ref TIMER_Start macro or program registers directly. + */ +uint32_t TIMER_Open(TIMER_T *timer, uint32_t u32Mode, uint32_t u32Freq) +{ + uint32_t u32Clk = TIMER_GetModuleClock(timer); + uint32_t u32Cmpr = 0UL, u32Prescale = 0UL; + + /* Fastest possible timer working freq is (u32Clk / 2). While cmpr = 2, prescaler = 0. */ + if(u32Freq > (u32Clk / 2UL)) + { + u32Cmpr = 2UL; + } + else + { + u32Cmpr = u32Clk / u32Freq; + u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */ + if (u32Prescale > 0UL) + u32Cmpr = u32Cmpr / (u32Prescale + 1UL); + } + + timer->TCSR = u32Mode | u32Prescale; + timer->TCMPR = u32Cmpr; + + return(u32Clk / (u32Cmpr * (u32Prescale + 1))); +} + +/** + * @brief Stop Timer Counting + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This API stops Timer counting and disable all Timer interrupt function. + */ +void TIMER_Close(TIMER_T *timer) +{ + timer->TCSR = 0; + timer->TEXCON = 0; +} + +/** + * @brief Create a specify delay time + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * @param[in] u32Usec Delay period in micro seconds. Valid values are between 100~1000000 (100 micro second ~ 1 second). + * + * @return None + * + * @details This API is used to create a delay loop for u32usec micro seconds. + * @note This API overwrites the register setting of the timer used to count the delay time. + * @note This API use polling mode. So there is no need to enable interrupt for the timer module used to generate delay. + */ +void TIMER_Delay(TIMER_T *timer, uint32_t u32Usec) +{ + uint32_t u32Clk = TIMER_GetModuleClock(timer); + uint32_t u32Prescale = 0UL, u32Delay = (SystemCoreClock / u32Clk) + 1UL; + uint32_t u32Cmpr, u32NsecPerTick; + + /* Clear current timer configuration */ + timer->TCSR = 0UL; + timer->TEXCON = 0UL; + + if(u32Clk <= 1000000UL) /* min delay is 1000 us if timer clock source is <= 1 MHz */ + { + if(u32Usec < 1000UL) + { + u32Usec = 1000UL; + } + if(u32Usec > 1000000UL) + { + u32Usec = 1000000UL; + } + } + else + { + if(u32Usec < 100UL) + { + u32Usec = 100UL; + } + if(u32Usec > 1000000UL) + { + u32Usec = 1000000UL; + } + } + + if(u32Clk <= 1000000UL) + { + u32Prescale = 0UL; + u32NsecPerTick = 1000000000UL / u32Clk; + u32Cmpr = (u32Usec * 1000UL) / u32NsecPerTick; + } + else + { + u32Cmpr = u32Usec * (u32Clk / 1000000UL); + u32Prescale = (u32Cmpr >> 24); /* for 24 bits CMPDAT */ + if (u32Prescale > 0UL) + u32Cmpr = u32Cmpr / (u32Prescale + 1UL); + } + + timer->TCMPR = u32Cmpr; + timer->TCSR = TIMER_TCSR_CEN_Msk | TIMER_ONESHOT_MODE | u32Prescale; + + /* + When system clock is faster than timer clock, it is possible timer active bit cannot set in time while we check it. + And the while loop below return immediately, so put a tiny delay here allowing timer start counting and raise active flag. + */ + for(; u32Delay > 0; u32Delay--) + { + __NOP(); + } + + while(timer->TCSR & TIMER_TCSR_CACT_Msk); +} + +/** + * @brief Enable Timer Capture Function + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * @param[in] u32CapMode Timer capture mode. Could be + * - \ref TIMER_CAPTURE_FREE_COUNTING_MODE + * - \ref TIMER_CAPTURE_COUNTER_RESET_MODE + * @param[in] u32Edge Timer capture edge. Possible values are + * - \ref TIMER_CAPTURE_FALLING_EDGE + * - \ref TIMER_CAPTURE_RISING_EDGE + * - \ref TIMER_CAPTURE_FALLING_AND_RISING_EDGE + * + * @return None + * + * @details This API is used to enable timer capture function with specified mode and capture edge. + * @note Timer frequency should be configured separately by using \ref TIMER_Open API, or program registers directly. + */ +void TIMER_EnableCapture(TIMER_T *timer, uint32_t u32CapMode, uint32_t u32Edge) +{ + + timer->TEXCON = (timer->TEXCON & ~(TIMER_TEXCON_RSTCAPSEL_Msk | + TIMER_TEXCON_TEX_EDGE_Msk)) | + u32CapMode | u32Edge | TIMER_TEXCON_TEXEN_Msk; +} + +/** + * @brief Disable Timer Capture Function + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This API is used to disable the Timer capture function. + */ +void TIMER_DisableCapture(TIMER_T *timer) +{ + timer->TEXCON &= ~TIMER_TEXCON_TEXEN_Msk; +} + +/** + * @brief Enable Timer Counter Function + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * @param[in] u32Edge Detection edge of counter pin. Could be ether + * - \ref TIMER_COUNTER_FALLING_EDGE, or + * - \ref TIMER_COUNTER_RISING_EDGE + * + * @return None + * + * @details This function is used to enable the Timer counter function with specify detection edge. + * @note Timer compare value should be configured separately by using \ref TIMER_SET_CMP_VALUE macro or program registers directly. + * @note While using event counter function, \ref TIMER_TOGGLE_MODE cannot set as timer operation mode. + */ +void TIMER_EnableEventCounter(TIMER_T *timer, uint32_t u32Edge) +{ + timer->TEXCON = (timer->TEXCON & ~TIMER_TEXCON_TX_PHASE_Msk) | u32Edge; + timer->TCSR |= TIMER_TCSR_CTB_Msk; +} + +/** + * @brief Disable Timer Counter Function + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return None + * + * @details This API is used to disable the Timer event counter function. + */ +void TIMER_DisableEventCounter(TIMER_T *timer) +{ + timer->TCSR &= ~TIMER_TCSR_CTB_Msk; +} + +/** + * @brief Get Timer Clock Frequency + * + * @param[in] timer The pointer of the specified Timer module. It could be TIMER0, TIMER1, TIMER2, TIMER3. + * + * @return Timer clock frequency + * + * @details This API is used to get the clock frequency of Timer. + * @note This API cannot return correct clock rate if timer source is external clock input. + */ +uint32_t TIMER_GetModuleClock(TIMER_T *timer) +{ + uint32_t u32Src; + const uint32_t au32Clk[] = {__HXT, 0, 0, 0, 0, __LIRC, 0, __HIRC}; + + if(timer == TIMER0) + u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR0_S_Msk) >> CLK_CLKSEL1_TMR0_S_Pos; + else if(timer == TIMER1) + u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR1_S_Msk) >> CLK_CLKSEL1_TMR1_S_Pos; + else if(timer == TIMER2) + u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR2_S_Msk) >> CLK_CLKSEL1_TMR2_S_Pos; + else // Timer 3 + u32Src = (CLK->CLKSEL1 & CLK_CLKSEL1_TMR3_S_Msk) >> CLK_CLKSEL1_TMR3_S_Pos; + + if(u32Src == 2) + { + return(SystemCoreClock); + } + + return(au32Clk[u32Src]); +} + +/*@}*/ /* end of group TIMER_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group TIMER_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/uart.c b/NUC123/StdDriver/src/uart.c new file mode 100644 index 0000000..e1d18d7 --- /dev/null +++ b/NUC123/StdDriver/src/uart.c @@ -0,0 +1,453 @@ +/**************************************************************************//** + * @file uart.c + * @version V3.00 + * $Revision: 11 $ + * $Date: 16/03/04 9:22a $ + * @brief NUC123 series UART driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ + +#include +#include "NUC123.h" + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup UART_Driver UART Driver + @{ +*/ + +/** @addtogroup UART_EXPORTED_FUNCTIONS UART Exported Functions + @{ +*/ + +/** + * @brief Clear UART specified interrupt flag + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32InterruptFlag The specified interrupt of UART module. + * - UART_ISR_BUF_ERR_INT_Msk : Buffer Error interrupt + * - UART_ISR_MODEM_INT_Msk : Modem interrupt + * - UART_ISR_RLS_INT_Msk : Rx Line status interrupt + * + * @return None + * + * @details The function is used to clear UART specified interrupt flag. + */ +void UART_ClearIntFlag(UART_T* uart , uint32_t u32InterruptFlag) +{ + + if(u32InterruptFlag & UART_ISR_RLS_INT_Msk) /* clear Receive Line Status Interrupt */ + { + uart->FSR = UART_FSR_BIF_Msk | UART_FSR_FEF_Msk | UART_FSR_PEF_Msk; + uart->FSR = UART_FSR_RS485_ADD_DETF_Msk; + } + + if(u32InterruptFlag & UART_ISR_MODEM_INT_Msk) /* clear Modem Interrupt */ + uart->MSR |= UART_MSR_DCTSF_Msk; + + if(u32InterruptFlag & UART_ISR_BUF_ERR_INT_Msk) /* clear Buffer Error Interrupt */ + { + uart->FSR = UART_FSR_RX_OVER_IF_Msk | UART_FSR_TX_OVER_IF_Msk; + } + +} + +/** + * @brief Disable UART interrupt + * + * @param[in] uart The pointer of the specified UART module. + * + * @return None + * + * @details The function is used to disable UART interrupt. + */ +void UART_Close(UART_T* uart) +{ + uart->IER = 0; +} + + +/** + * @brief Disable UART auto flow control function + * + * @param[in] uart The pointer of the specified UART module. + * + * @return None + * + * @details The function is used to disable UART auto flow control. + */ +void UART_DisableFlowCtrl(UART_T* uart) +{ + uart->IER &= ~(UART_IER_AUTO_RTS_EN_Msk | UART_IER_AUTO_CTS_EN_Msk); +} + + +/** + * @brief Disable UART specified interrupt + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32InterruptFlag The specified interrupt of UART module. + * - UART_IER_WAKE_EN_Msk : Wakeup interrupt + * - UART_IER_BUF_ERR_IEN_Msk : Buffer Error interrupt + * - UART_IER_RTO_IEN_Msk : Rx time-out interrupt + * - UART_IER_MODEM_IEN_Msk : Modem interrupt + * - UART_IER_RLS_IEN_Msk : Rx Line status interrupt + * - UART_IER_THRE_IEN_Msk : Tx empty interrupt + * - UART_IER_RDA_IEN_Msk : Rx ready interrupt + * + * @return None + * + * @details The function is used to disable UART specified interrupt and disable NVIC UART IRQ. + */ +void UART_DisableInt(UART_T* uart, uint32_t u32InterruptFlag) +{ + /* Disable UART specified interrupt */ + UART_DISABLE_INT(uart, u32InterruptFlag); + + /* Disable NVIC UART IRQ */ + if(uart == UART0) + NVIC_DisableIRQ(UART0_IRQn); + else + NVIC_DisableIRQ(UART1_IRQn); +} + + +/** + * @brief Enable UART auto flow control function + * + * @param[in] uart The pointer of the specified UART module. + * + * @return None + * + * @details The function is used to enable UART auto flow control. + */ +void UART_EnableFlowCtrl(UART_T* uart) +{ + /* Set RTS pin output is low level active */ + uart->MCR |= UART_MCR_LEV_RTS_Msk; + + /* Set CTS pin input is low level active */ + uart->MSR |= UART_MSR_LEV_CTS_Msk; + + /* Set RTS and CTS auto flow control enable */ + uart->IER |= UART_IER_AUTO_RTS_EN_Msk | UART_IER_AUTO_CTS_EN_Msk; +} + + +/** + * @brief Enable UART specified interrupt + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32InterruptFlag The specified interrupt of UART module: + * - UART_IER_WAKE_EN_Msk : Wakeup interrupt + * - UART_IER_BUF_ERR_IEN_Msk : Buffer Error interrupt + * - UART_IER_RTO_IEN_Msk : Rx time-out interrupt + * - UART_IER_MODEM_IEN_Msk : Modem interrupt + * - UART_IER_RLS_IEN_Msk : Rx Line status interrupt + * - UART_IER_THRE_IEN_Msk : Tx empty interrupt + * - UART_IER_RDA_IEN_Msk : Rx ready interrupt + * + * @return None + * + * @details The function is used to enable UART specified interrupt and enable NVIC UART IRQ. + */ +void UART_EnableInt(UART_T* uart, uint32_t u32InterruptFlag) +{ + /* Enable UART specified interrupt */ + UART_ENABLE_INT(uart, u32InterruptFlag); + + /* Enable NVIC UART IRQ */ + if(uart == UART0) + NVIC_EnableIRQ(UART0_IRQn); + else + NVIC_EnableIRQ(UART1_IRQn); +} + + +/** + * @brief Open and set UART function + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32baudrate The baudrate of UART module. + * + * @return None + * + * @details This function use to enable UART function and set baud-rate. + */ +void UART_Open(UART_T* uart, uint32_t u32baudrate) +{ + uint8_t u8UartClkSrcSel, u8UartClkDivNum; + uint32_t u32ClkTbl[4] = {__HXT, 0, 0, __HIRC}; + uint32_t u32Baud_Div = 0; + + /* Get UART clock source selection */ + u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos; + + /* Get UART clock divider number */ + u8UartClkDivNum = (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos; + + /* Select UART function */ + uart->FUN_SEL = UART_FUNC_SEL_UART; + + /* Set UART line configuration */ + uart->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1; + + /* Set UART Rx and RTS trigger level */ + uart->FCR &= ~(UART_FCR_RFITL_Msk | UART_FCR_RTS_TRI_LEV_Msk); + + /* Get PLL clock frequency if UART clock source selection is PLL */ + if(u8UartClkSrcSel == 1) + u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq(); + + /* Set UART baud rate */ + if(u32baudrate != 0) + { + u32Baud_Div = UART_BAUD_MODE2_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate); + + if(u32Baud_Div > 0xFFFF) + uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate)); + else + uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); + } +} + + +/** + * @brief Read UART data + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] pu8RxBuf The buffer to receive the data of receive FIFO. + * @param[in] u32ReadBytes The read bytes number of data. + * + * @return u32Count Receive byte count + * + * @details The function is used to read Rx data from RX FIFO and the data will be stored in pu8RxBuf. + */ +uint32_t UART_Read(UART_T* uart, uint8_t *pu8RxBuf, uint32_t u32ReadBytes) +{ + uint32_t u32Count, u32delayno; + + for(u32Count = 0; u32Count < u32ReadBytes; u32Count++) + { + u32delayno = 0; + + while(uart->FSR & UART_FSR_RX_EMPTY_Msk) /* Check RX empty => failed */ + { + u32delayno++; + if(u32delayno >= 0x40000000) + return u32Count; + } + pu8RxBuf[u32Count] = uart->RBR; /* Get Data from UART RX */ + } + + return u32Count; + +} + + +/** + * @brief Set UART line configuration + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32baudrate The baudrate of UART module. + * If u32baudrate = 0, UART baudrate will not change. + * @param[in] u32data_width The data length of UART module. + * - UART_WORD_LEN_5 + * - UART_WORD_LEN_6 + * - UART_WORD_LEN_7 + * - UART_WORD_LEN_8 + * @param[in] u32parity The parity setting (none/odd/even/mark/space) of UART module. + * - UART_PARITY_NONE + * - UART_PARITY_ODD + * - UART_PARITY_EVEN + * - UART_PARITY_MARK + * - UART_PARITY_SPACE + * @param[in] u32stop_bits The stop bit length (1/1.5/2 bit) of UART module. + * - UART_STOP_BIT_1 + * - UART_STOP_BIT_1_5 + * - UART_STOP_BIT_2 + * + * @return None + * + * @details This function use to config UART line setting. + + */ +void UART_SetLine_Config(UART_T* uart, uint32_t u32baudrate, uint32_t u32data_width, uint32_t u32parity, uint32_t u32stop_bits) +{ + uint8_t u8UartClkSrcSel, u8UartClkDivNum; + uint32_t u32ClkTbl[4] = {__HXT, 0, 0, __HIRC}; + uint32_t u32Baud_Div = 0; + + /* Get UART clock source selection */ + u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos; + + /* Get UART clock divider number */ + u8UartClkDivNum = (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos; + + /* Get PLL clock frequency if UART clock source selection is PLL */ + if(u8UartClkSrcSel == 1) + u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq(); + + /* Set UART baud rate */ + if(u32baudrate != 0) + { + u32Baud_Div = UART_BAUD_MODE2_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate); + + if(u32Baud_Div > 0xFFFF) + uart->BAUD = (UART_BAUD_MODE0 | UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32baudrate)); + else + uart->BAUD = (UART_BAUD_MODE2 | u32Baud_Div); + } + + /* Set UART line configuration */ + uart->LCR = u32data_width | u32parity | u32stop_bits; +} + + +/** + * @brief Set Rx timeout count + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32TOC Rx timeout counter. + * + * @return None + * + * @details This function use to set Rx timeout count. + */ +void UART_SetTimeoutCnt(UART_T* uart, uint32_t u32TOC) +{ + /* Set time-out interrupt comparator */ + uart->TOR = (uart->TOR & ~UART_TOR_TOIC_Msk) | (u32TOC); + + /* Set time-out counter enable */ + uart->IER |= UART_IER_TIME_OUT_EN_Msk; +} + + +/** + * @brief Select and configure IrDA function + * + * @param[in] uart The pointer of the specified UART module + * @param[in] u32Buadrate The baudrate of UART module. + * @param[in] u32Direction The direction of UART module in IrDA mode: + * - UART_IRCR_TX_SELECT + * - UART_IRCR_RX_SELECT + * + * @return None + * + * @details The function is used to configure IrDA relative settings. It consists of TX or RX mode and baudrate. + */ +void UART_SelectIrDAMode(UART_T* uart, uint32_t u32Buadrate, uint32_t u32Direction) +{ + uint8_t u8UartClkSrcSel, u8UartClkDivNum; + uint32_t u32ClkTbl[4] = {__HXT, 0, 0, __HIRC}; + uint32_t u32Baud_Div; + + /* Select IrDA function mode */ + uart->FUN_SEL = UART_FUNC_SEL_IrDA; + + /* Get UART clock source selection */ + u8UartClkSrcSel = (CLK->CLKSEL1 & CLK_CLKSEL1_UART_S_Msk) >> CLK_CLKSEL1_UART_S_Pos; + + /* Get UART clock divider number */ + u8UartClkDivNum = (CLK->CLKDIV & CLK_CLKDIV_UART_N_Msk) >> CLK_CLKDIV_UART_N_Pos; + + /* Get PLL clock frequency if UART clock source selection is PLL */ + if(u8UartClkSrcSel == 1) + u32ClkTbl[u8UartClkSrcSel] = CLK_GetPLLClockFreq(); + + /* Set UART IrDA baud rate in mode 0 */ + if(u32Buadrate != 0) + { + u32Baud_Div = UART_BAUD_MODE0_DIVIDER((u32ClkTbl[u8UartClkSrcSel]) / (u8UartClkDivNum + 1), u32Buadrate); + + if(u32Baud_Div < 0xFFFF) + uart->BAUD = (UART_BAUD_MODE0 | u32Baud_Div); + } + + /* Configure IrDA relative settings */ + if(u32Direction == UART_IRCR_RX_SELECT) + { + uart->IRCR |= UART_IRCR_INV_RX_Msk; //Rx signal is inverse + uart->IRCR &= ~UART_IRCR_TX_SELECT_Msk; + } + else + { + uart->IRCR &= ~UART_IRCR_INV_TX_Msk; //Tx signal is not inverse + uart->IRCR |= UART_IRCR_TX_SELECT_Msk; + } +} + + +/** + * @brief Select and configure RS485 function + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] u32Mode The operation mode(NMM/AUD/AAD). + * - UART_ALT_CSR_RS485_NMM_Msk + * - UART_ALT_CSR_RS485_AUD_Msk + * - UART_ALT_CSR_RS485_AAD_Msk + * @param[in] u32Addr The RS485 address. + * + * @return None + * + * @details The function is used to set RS485 relative setting. + */ +void UART_SelectRS485Mode(UART_T* uart, uint32_t u32Mode, uint32_t u32Addr) +{ + /* Select UART RS485 function mode */ + uart->FUN_SEL = UART_FUNC_SEL_RS485; + + /* Set RS485 configuration */ + uart->ALT_CSR &= ~(UART_ALT_CSR_RS485_NMM_Msk | UART_ALT_CSR_RS485_AUD_Msk | UART_ALT_CSR_RS485_AAD_Msk | UART_ALT_CSR_ADDR_MATCH_Msk); + uart->ALT_CSR |= (u32Mode | (u32Addr << UART_ALT_CSR_ADDR_MATCH_Pos)); +} + + +/** + * @brief Write UART data + * + * @param[in] uart The pointer of the specified UART module. + * @param[in] pu8TxBuf The buffer to send the data to UART transmission FIFO. + * @param[out] u32WriteBytes The byte number of data. + * + * @return u32Count transfer byte count + * + * @details The function is to write data into TX buffer to transmit data by UART. + */ +uint32_t UART_Write(UART_T* uart, uint8_t *pu8TxBuf, uint32_t u32WriteBytes) +{ + uint32_t u32Count, u32delayno; + + for(u32Count = 0; u32Count != u32WriteBytes; u32Count++) + { + u32delayno = 0; + while(uart->FSR & UART_FSR_TX_FULL_Msk) /* Wait Tx not full or Time-out manner */ + { + u32delayno++; + if(u32delayno >= 0x40000000) + return u32Count; + } + uart->THR = pu8TxBuf[u32Count]; /* Send UART Data from buffer */ + } + + return u32Count; + +} + + +/*@}*/ /* end of group UART_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group UART_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ + + + diff --git a/NUC123/StdDriver/src/usbd.c b/NUC123/StdDriver/src/usbd.c new file mode 100644 index 0000000..10b631e --- /dev/null +++ b/NUC123/StdDriver/src/usbd.c @@ -0,0 +1,717 @@ +/**************************************************************************//** + * @file usbd.c + * @version V3.00 + * $Revision: 22 $ + * $Date: 15/09/01 3:15p $ + * @brief NUC123 series USBD driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include +#include +#include "NUC123.h" + +#if 0 +#define DBG_PRINTF printf +#else +#define DBG_PRINTF(...) +#endif +#ifdef __cplusplus +extern "C" +{ +#endif + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup USBD_Driver USBD Driver + @{ +*/ + + +/** @addtogroup USBD_EXPORTED_FUNCTIONS USBD Exported Functions + @{ +*/ + +/* Global variables for Control Pipe */ +uint8_t g_usbd_SetupPacket[8] = {0}; /*!< Setup packet buffer */ +volatile uint8_t g_usbd_RemoteWakeupEn = 0; /*!< Remote wake up function enable flag */ + +/** + * @cond HIDDEN_SYMBOLS + */ +static volatile uint8_t *g_usbd_CtrlInPointer = 0; +static volatile uint32_t g_usbd_CtrlInSize = 0; +static volatile uint8_t *g_usbd_CtrlOutPointer = 0; +static volatile uint32_t g_usbd_CtrlOutSize = 0; +static volatile uint32_t g_usbd_CtrlOutSizeLimit = 0; +static volatile uint32_t g_usbd_UsbAddr = 0; +static volatile uint32_t g_usbd_UsbConfig = 0; +static volatile uint32_t g_usbd_CtrlMaxPktSize = 8; +static volatile uint32_t g_usbd_UsbAltInterface = 0; +static volatile uint32_t g_usbd_CtrlOutToggle = 0; +static volatile uint8_t g_usbd_CtrlInZeroFlag = 0; +/** + * @endcond + */ + +const S_USBD_INFO_T *g_usbd_sInfo; /*!< A pointer for USB information structure */ + +VENDOR_REQ g_usbd_pfnVendorRequest = NULL; /*!< USB Vendor Request Functional Pointer */ +CLASS_REQ g_usbd_pfnClassRequest = NULL; /*!< USB Class Request Functional Pointer */ +SET_INTERFACE_REQ g_usbd_pfnSetInterface = NULL; /*!< USB Set Interface Functional Pointer */ +SET_CONFIG_CB g_usbd_pfnSetConfigCallback = NULL; /*!< USB Set configuration callback function pointer */ +uint32_t g_u32EpStallLock = 0; /*!< Bit map flag to lock specified EP when SET_FEATURE */ + +/** + * @brief This function makes USBD module to be ready to use + * + * @param[in] param The structure of USBD information. + * @param[in] pfnClassReq USB Class request callback function. + * @param[in] pfnSetInterface USB Set Interface request callback function. + * + * @return None + * + * @details This function will enable USB controller, USB PHY transceiver and pull-up resistor of USB_D+ pin. USB PHY will drive SE0 to bus. + */ +void USBD_Open(const S_USBD_INFO_T *param, CLASS_REQ pfnClassReq, SET_INTERFACE_REQ pfnSetInterface) +{ + g_usbd_sInfo = param; + g_usbd_pfnClassRequest = pfnClassReq; + g_usbd_pfnSetInterface = pfnSetInterface; + + /* get EP0 maximum packet size */ + g_usbd_CtrlMaxPktSize = g_usbd_sInfo->gu8DevDesc[7]; + + /* Initial USB engine */ + USBD->ATTR = 0x7D0; + /* Force SE0 */ + USBD_SET_SE0(); +} + +/** + * @brief This function makes USB host to recognize the device + * + * @param None + * + * @return None + * + * @details Enable WAKEUP, FLDET, USB and BUS interrupts. Disable software-disconnect function after 100ms delay with SysTick timer. + */ +void USBD_Start(void) +{ + CLK_SysTickDelay(100000); + /* Disable software-disconnect function */ + USBD_CLR_SE0(); + + /* Clear USB-related interrupts before enable interrupt */ + USBD_CLR_INT_FLAG(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP); + + /* Enable USB-related interrupts. */ + USBD_ENABLE_INT(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP); +} + +/** + * @brief Get the received SETUP packet + * + * @param[in] buf A buffer pointer used to store 8-byte SETUP packet. + * + * @return None + * + * @details Store SETUP packet to a user-specified buffer. + * + */ +void USBD_GetSetupPacket(uint8_t *buf) +{ + USBD_MemCopy(buf, g_usbd_SetupPacket, 8); +} + +/** + * @brief Process SETUP packet + * + * @param None + * + * @return None + * + * @details Parse SETUP packet and perform the corresponding action. + * + */ +void USBD_ProcessSetupPacket(void) +{ + g_usbd_CtrlOutToggle = 0; + /* Get SETUP packet from USB buffer */ + USBD_MemCopy(g_usbd_SetupPacket, (uint8_t *)USBD_BUF_BASE, 8); + + /* Check the request type */ + switch(g_usbd_SetupPacket[0] & 0x60) + { + case REQ_STANDARD: // Standard + { + USBD_StandardRequest(); + break; + } + case REQ_CLASS: // Class + { + if(g_usbd_pfnClassRequest != NULL) + { + g_usbd_pfnClassRequest(); + } + break; + } + case REQ_VENDOR: // Vendor + { + if(g_usbd_pfnVendorRequest != NULL) + { + g_usbd_pfnVendorRequest(); + } + break; + } + default: // reserved + { + /* Setup error, stall the device */ + USBD_SET_EP_STALL(EP0); + USBD_SET_EP_STALL(EP1); + break; + } + } +} + +/** + * @brief Process GetDescriptor request + * + * @param None + * + * @return None + * + * @details Parse GetDescriptor request and perform the corresponding action. + * + */ +void USBD_GetDescriptor(void) +{ + uint32_t u32Len; + + g_usbd_CtrlInZeroFlag = (uint8_t)0; + u32Len = 0; + u32Len = g_usbd_SetupPacket[7]; + u32Len <<= 8; + u32Len += g_usbd_SetupPacket[6]; + + switch(g_usbd_SetupPacket[3]) + { + // Get Device Descriptor + case DESC_DEVICE: + { + u32Len = Minimum(u32Len, LEN_DEVICE); + DBG_PRINTF("Get device desc, %d\n", u32Len); + USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8DevDesc, u32Len); + USBD_PrepareCtrlOut(0, 0); + break; + } + // Get Configuration Descriptor + case DESC_CONFIG: + { + uint32_t u32TotalLen; + + u32TotalLen = g_usbd_sInfo->gu8ConfigDesc[3]; + u32TotalLen = g_usbd_sInfo->gu8ConfigDesc[2] + (u32TotalLen << 8); + + if(u32Len > u32TotalLen) + { + u32Len = u32TotalLen; + if((u32Len % g_usbd_CtrlMaxPktSize) == 0) + { + g_usbd_CtrlInZeroFlag = (uint8_t)1; + } + } + USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8ConfigDesc, u32Len); + USBD_PrepareCtrlOut(0, 0); + break; + } + // Get HID Descriptor + case DESC_HID: + { + /* CV3.0 HID Class Descriptor Test, + Need to indicate index of the HID Descriptor within gu8ConfigDescriptor, specifically HID Composite device. */ + uint32_t u32ConfigDescOffset; // u32ConfigDescOffset is configuration descriptor offset (HID descriptor start index) + u32Len = Minimum(u32Len, LEN_HID); + DBG_PRINTF("Get HID desc, %d\n", u32Len); + + u32ConfigDescOffset = g_usbd_sInfo->gu32ConfigHidDescIdx[g_usbd_SetupPacket[4]]; + USBD_PrepareCtrlIn((uint8_t *)&g_usbd_sInfo->gu8ConfigDesc[u32ConfigDescOffset], u32Len); + + USBD_PrepareCtrlOut(0, 0); + break; + } + // Get Report Descriptor + case DESC_HID_RPT: + { + if(u32Len > g_usbd_sInfo->gu32HidReportSize[g_usbd_SetupPacket[4]]) + { + u32Len = g_usbd_sInfo->gu32HidReportSize[g_usbd_SetupPacket[4]]; + if((u32Len % g_usbd_CtrlMaxPktSize) == 0) + { + g_usbd_CtrlInZeroFlag = (uint8_t)1; + } + } + USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8HidReportDesc[g_usbd_SetupPacket[4]], u32Len); + USBD_PrepareCtrlOut(0, 0); + break; + } + // Get String Descriptor + case DESC_STRING: + { + // Get String Descriptor + if(g_usbd_SetupPacket[2] < 4) + { + if(u32Len > g_usbd_sInfo->gu8StringDesc[g_usbd_SetupPacket[2]][0]) + { + u32Len = g_usbd_sInfo->gu8StringDesc[g_usbd_SetupPacket[2]][0]; + if((u32Len % g_usbd_CtrlMaxPktSize) == 0) + { + g_usbd_CtrlInZeroFlag = (uint8_t)1; + } + } + USBD_PrepareCtrlIn((uint8_t *)g_usbd_sInfo->gu8StringDesc[g_usbd_SetupPacket[2]], u32Len); + USBD_PrepareCtrlOut(0, 0); + break; + } + else + { + // Not support. Reply STALL. + USBD_SET_EP_STALL(EP0); + USBD_SET_EP_STALL(EP1); + DBG_PRINTF("Unsupported string desc (%d). Stall ctrl pipe.\n", g_usbd_SetupPacket[2]); + break; + } + } + default: + // Not support. Reply STALL. + USBD_SET_EP_STALL(EP0); + USBD_SET_EP_STALL(EP1); + DBG_PRINTF("Unsupported get desc type. stall ctrl pipe\n"); + break; + } +} + +/** + * @brief Process standard request + * + * @param None + * + * @return None + * + * @details Parse standard request and perform the corresponding action. + * + */ +void USBD_StandardRequest(void) +{ + /* clear global variables for new request */ + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + + if(g_usbd_SetupPacket[0] & 0x80) /* request data transfer direction */ + { + // Device to host + switch(g_usbd_SetupPacket[1]) + { + case GET_CONFIGURATION: + { + // Return current configuration setting + /* Data stage */ + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)) = g_usbd_UsbConfig; + USBD_SET_DATA1(EP1); + USBD_SET_PAYLOAD_LEN(EP1, 0); + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 1); + /* Status stage */ + USBD_PrepareCtrlOut(0, 0); + DBG_PRINTF("Get configuration\n"); + break; + } + case GET_DESCRIPTOR: + { + USBD_GetDescriptor(); + /* Status stage */ + USBD_PrepareCtrlOut(0, 0); + DBG_PRINTF("Get descriptor\n"); + break; + } + case GET_INTERFACE: + { + // Return current interface setting + /* Data stage */ + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)) = g_usbd_UsbAltInterface; + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 1); + /* Status stage */ + USBD_PrepareCtrlOut(0, 0); + DBG_PRINTF("Get interface\n"); + break; + } + case GET_STATUS: + { + // Device + if(g_usbd_SetupPacket[0] == 0x80) + { + uint8_t u8Tmp; + + u8Tmp = 0; + if(g_usbd_sInfo->gu8ConfigDesc[7] & 0x40) u8Tmp |= 1; // Self-Powered/Bus-Powered. + if(g_usbd_sInfo->gu8ConfigDesc[7] & 0x20) u8Tmp |= (g_usbd_RemoteWakeupEn << 1); // Remote wake up + + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)) = u8Tmp; + } + // Interface + else if(g_usbd_SetupPacket[0] == 0x81) + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)) = 0; + // Endpoint + else if(g_usbd_SetupPacket[0] == 0x82) + { + uint8_t ep = g_usbd_SetupPacket[4] & 0xF; + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0)) = USBD_GetStall(ep) ? 1 : 0; + } + + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0) + 1) = 0; + /* Data stage */ + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 2); + /* Status stage */ + USBD_PrepareCtrlOut(0, 0); + DBG_PRINTF("Get status\n"); + break; + } + default: + { + /* Setup error, stall the device */ + USBD_SET_EP_STALL(EP0); + USBD_SET_EP_STALL(EP1); + DBG_PRINTF("Unknown request. stall ctrl pipe.\n"); + break; + } + } + } + else + { + // Host to device + switch(g_usbd_SetupPacket[1]) + { + case CLEAR_FEATURE: + { + if(g_usbd_SetupPacket[2] == FEATURE_ENDPOINT_HALT) + { + int32_t epNum, i; + + /* EP number stall is not allow to be clear in MSC class "Error Recovery Test". + a flag: g_u32EpStallLock is added to support it */ + epNum = g_usbd_SetupPacket[4] & 0xF; + for(i = 0; i < USBD_MAX_EP; i++) + { + if(((USBD->EP[i].CFG & 0xF) == epNum) && ((g_u32EpStallLock & (1 << i)) == 0)) + { + USBD->EP[i].CFGP &= ~USBD_CFGP_SSTALL_Msk; + USBD->EP[i].CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + DBG_PRINTF("Clr stall ep%d %x\n", i, USBD->EP[i].CFGP); + } + } + } + else if(g_usbd_SetupPacket[2] == FEATURE_DEVICE_REMOTE_WAKEUP) + g_usbd_RemoteWakeupEn = 0; + /* Status stage */ + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 0); + DBG_PRINTF("Clear feature op %d\n", g_usbd_SetupPacket[2]); + break; + } + case SET_ADDRESS: + { + g_usbd_UsbAddr = g_usbd_SetupPacket[2]; + DBG_PRINTF("Set addr to %d\n", g_usbd_UsbAddr); + + // DATA IN for end of setup + /* Status Stage */ + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 0); + break; + } + case SET_CONFIGURATION: + { + g_usbd_UsbConfig = g_usbd_SetupPacket[2]; + + if(g_usbd_pfnSetConfigCallback) + g_usbd_pfnSetConfigCallback(); + // DATA IN for end of setup + /* Status stage */ + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 0); + DBG_PRINTF("Set config to %d\n", g_usbd_UsbConfig); + break; + } + case SET_FEATURE: + { + if(g_usbd_SetupPacket[2] == FEATURE_ENDPOINT_HALT) + { + USBD_SetStall(g_usbd_SetupPacket[4] & 0xF); + DBG_PRINTF("Set feature. stall ep %d\n", g_usbd_SetupPacket[4] & 0xF); + } + else if(g_usbd_SetupPacket[2] == FEATURE_DEVICE_REMOTE_WAKEUP) + { + g_usbd_RemoteWakeupEn = 1; + DBG_PRINTF("Set feature. enable remote wakeup\n"); + } + /* Status stage */ + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 0); + break; + } + case SET_INTERFACE: + { + g_usbd_UsbAltInterface = g_usbd_SetupPacket[2]; + if(g_usbd_pfnSetInterface != NULL) + g_usbd_pfnSetInterface(); + /* Status stage */ + USBD_SET_DATA1(EP0); + USBD_SET_PAYLOAD_LEN(EP0, 0); + DBG_PRINTF("Set interface to %d\n", g_usbd_UsbAltInterface); + break; + } + default: + { + /* Setup error, stall the device */ + USBD_SET_EP_STALL(EP0); + USBD_SET_EP_STALL(EP1); + DBG_PRINTF("Unsupported request. stall ctrl pipe.\n"); + break; + } + } + } +} + +/** + * @brief Prepare the first Control IN pipe + * + * @param[in] pu8Buf The pointer of data sent to USB host. + * @param[in] u32Size The IN transfer size. + * + * @return None + * + * @details Prepare data for Control IN transfer. + * + */ +void USBD_PrepareCtrlIn(uint8_t *pu8Buf, uint32_t u32Size) +{ + DBG_PRINTF("Prepare Ctrl In %d\n", u32Size); + if(u32Size > g_usbd_CtrlMaxPktSize) + { + // Data size > MXPLD + g_usbd_CtrlInPointer = pu8Buf + g_usbd_CtrlMaxPktSize; + g_usbd_CtrlInSize = u32Size - g_usbd_CtrlMaxPktSize; + USBD_SET_DATA1(EP0); + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), pu8Buf, g_usbd_CtrlMaxPktSize); + USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlMaxPktSize); + } + else + { + // Data size <= MXPLD + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + USBD_SET_DATA1(EP0); + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), pu8Buf, u32Size); + USBD_SET_PAYLOAD_LEN(EP0, u32Size); + } +} + +/** + * @brief Repeat Control IN pipe + * + * @param None + * + * @return None + * + * @details This function processes the remained data of Control IN transfer. + * + */ +void USBD_CtrlIn(void) +{ + if(g_usbd_CtrlInSize) + { + // Process remained data + if(g_usbd_CtrlInSize > g_usbd_CtrlMaxPktSize) + { + // Data size > MXPLD + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlMaxPktSize); + USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlMaxPktSize); + g_usbd_CtrlInPointer += g_usbd_CtrlMaxPktSize; + g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize; + } + else + { + // Data size <= MXPLD + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP0), (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlInSize); + USBD_SET_PAYLOAD_LEN(EP0, g_usbd_CtrlInSize); + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + } + } + else // No more data for IN token + { + // In ACK for Set address + if((g_usbd_SetupPacket[0] == REQ_STANDARD) && (g_usbd_SetupPacket[1] == SET_ADDRESS)) + { + if((USBD_GET_ADDR() != g_usbd_UsbAddr) && (USBD_GET_ADDR() == 0)) + { + USBD_SET_ADDR(g_usbd_UsbAddr); + } + } + + /* For the case of data size is integral times maximum packet size */ + if(g_usbd_CtrlInZeroFlag) + { + USBD_SET_PAYLOAD_LEN(EP0, 0); + g_usbd_CtrlInZeroFlag = 0; + } + DBG_PRINTF("Ctrl In done.\n"); + } +} + +/** + * @brief Prepare the first Control OUT pipe + * + * @param[in] pu8Buf The pointer of data received from USB host. + * @param[in] u32Size The OUT transfer size. + * + * @return None + * + * @details This function is used to prepare the first Control OUT transfer. + * + */ +void USBD_PrepareCtrlOut(uint8_t *pu8Buf, uint32_t u32Size) +{ + g_usbd_CtrlOutPointer = pu8Buf; + g_usbd_CtrlOutSize = 0; + g_usbd_CtrlOutSizeLimit = u32Size; + USBD_SET_PAYLOAD_LEN(EP1, g_usbd_CtrlMaxPktSize); +} + +/** + * @brief Repeat Control OUT pipe + * + * @param None + * + * @return None + * + * @details This function processes the successive Control OUT transfer. + * + */ +void USBD_CtrlOut(void) +{ + uint32_t u32Size; + + DBG_PRINTF("Ctrl Out Ack %d\n", g_usbd_CtrlOutSize); + if(g_usbd_CtrlOutToggle != (USBD->EPSTS & USBD_EPSTS_EPSTS1_Msk)) + { + g_usbd_CtrlOutToggle = USBD->EPSTS & USBD_EPSTS_EPSTS1_Msk; + if(g_usbd_CtrlOutSize < g_usbd_CtrlOutSizeLimit) + { + u32Size = USBD_GET_PAYLOAD_LEN(EP1); + USBD_MemCopy((uint8_t *)g_usbd_CtrlOutPointer, (uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP1), u32Size); + g_usbd_CtrlOutPointer += u32Size; + g_usbd_CtrlOutSize += u32Size; + + if(g_usbd_CtrlOutSize < g_usbd_CtrlOutSizeLimit) + USBD_SET_PAYLOAD_LEN(EP1, g_usbd_CtrlMaxPktSize); + } + } + else + { + USBD_SET_PAYLOAD_LEN(EP1, g_usbd_CtrlMaxPktSize); + } +} + +/** + * @brief Reset software flags + * + * @param None + * + * @return None + * + * @details This function resets all variables for protocol and resets USB device address to 0. + * + */ +void USBD_SwReset(void) +{ + int i; + + // Reset all variables for protocol + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + g_usbd_CtrlOutPointer = 0; + g_usbd_CtrlOutSize = 0; + g_usbd_CtrlOutSizeLimit = 0; + g_u32EpStallLock = 0; + memset(g_usbd_SetupPacket, 0, 8); + + /* Reset PID DATA0 */ + for(i=0; iEP[i].CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + + // Reset USB device address + USBD_SET_ADDR(0); +} + +/** + * @brief USBD Set Vendor Request + * + * @param[in] pfnVendorReq Vendor Request Callback Function + * + * @return None + * + * @details This function is used to set USBD vendor request callback function + */ +void USBD_SetVendorRequest(VENDOR_REQ pfnVendorReq) +{ + g_usbd_pfnVendorRequest = pfnVendorReq; +} + +/** + * @brief The callback function which called when get SET CONFIGURATION request + * + * @param[in] pfnSetConfigCallback Callback function pointer for SET CONFIGURATION request + * + * @return None + * + * @details This function is used to set the callback function which will be called at SET CONFIGURATION request. + */ +void USBD_SetConfigCallback(SET_CONFIG_CB pfnSetConfigCallback) +{ + g_usbd_pfnSetConfigCallback = pfnSetConfigCallback; +} + + +/** + * @brief EP stall lock function to avoid stall clear by USB SET FEATURE request. + * + * @param[in] u32EpBitmap Use bitmap to select which endpoints will be locked + * + * @return None + * + * @details This function is used to lock relative endpoint to avoid stall clear by SET FEATURE requst. + * If ep stall locked, user needs to reset USB device or re-configure device to clear it. + */ +void USBD_LockEpStall(uint32_t u32EpBitmap) +{ + g_u32EpStallLock = u32EpBitmap; +} +/*@}*/ /* end of group USBD_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group USBD_Driver */ + +/*@}*/ /* end of group Device_Driver */ + +#ifdef __cplusplus +} +#endif + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/wdt.c b/NUC123/StdDriver/src/wdt.c new file mode 100644 index 0000000..81a9241 --- /dev/null +++ b/NUC123/StdDriver/src/wdt.c @@ -0,0 +1,71 @@ +/**************************************************************************//** + * @file wdt.c + * @version V3.00 + * $Revision: 4 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series WDT driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup WDT_Driver WDT Driver + @{ +*/ + +/** @addtogroup WDT_EXPORTED_FUNCTIONS WDT Exported Functions + @{ +*/ + +/** + * @brief Initialize WDT counter and start counting + * + * @param[in] u32TimeoutInterval Time-out interval period of WDT module. Valid values are: + * - \ref WDT_TIMEOUT_2POW4 + * - \ref WDT_TIMEOUT_2POW6 + * - \ref WDT_TIMEOUT_2POW8 + * - \ref WDT_TIMEOUT_2POW10 + * - \ref WDT_TIMEOUT_2POW12 + * - \ref WDT_TIMEOUT_2POW14 + * - \ref WDT_TIMEOUT_2POW16 + * - \ref WDT_TIMEOUT_2POW18 + * @param[in] u32ResetDelay Configure reset delay period while WDT time-out happened. Valid values are: + * - \ref WDT_RESET_DELAY_1026CLK + * - \ref WDT_RESET_DELAY_130CLK + * - \ref WDT_RESET_DELAY_18CLK + * - \ref WDT_RESET_DELAY_3CLK + * @param[in] u32EnableReset Enable WDT reset system function. Valid values are TRUE and FALSE. + * @param[in] u32EnableWakeup Enable WDT wake-up system function. Valid values are TRUE and FALSE. + * + * @return None + * + * @details This function make WDT module start counting with different time-out interval and reset delay period. + * @note Please make sure that Register Write-Protection Function has been disabled before using this function. + */ +void WDT_Open(uint32_t u32TimeoutInterval, + uint32_t u32ResetDelay, + uint32_t u32EnableReset, + uint32_t u32EnableWakeup) +{ + WDT->WTCRALT = u32ResetDelay; + + WDT->WTCR = u32TimeoutInterval | WDT_WTCR_WTE_Msk | + (u32EnableReset << WDT_WTCR_WTRE_Pos) | + (u32EnableWakeup << WDT_WTCR_WTWKE_Pos); + return; +} + +/*@}*/ /* end of group WDT_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group WDT_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/StdDriver/src/wwdt.c b/NUC123/StdDriver/src/wwdt.c new file mode 100644 index 0000000..bf9df40 --- /dev/null +++ b/NUC123/StdDriver/src/wwdt.c @@ -0,0 +1,72 @@ +/**************************************************************************//** + * @file wwdt.c + * @version V3.00 + * $Revision: 3 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 series WWDT driver source file + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. +*****************************************************************************/ +#include "NUC123.h" + + +/** @addtogroup Standard_Driver Standard Driver + @{ +*/ + +/** @addtogroup WWDT_Driver WWDT Driver + @{ +*/ + +/** @addtogroup WWDT_EXPORTED_FUNCTIONS WWDT Exported Functions + @{ +*/ + +/** + * @brief Open WWDT function to start counting + * + * @param[in] u32PreScale Prescale period for the WWDT counter period. Valid values are: + * - \ref WWDT_PRESCALER_1 + * - \ref WWDT_PRESCALER_2 + * - \ref WWDT_PRESCALER_4 + * - \ref WWDT_PRESCALER_8 + * - \ref WWDT_PRESCALER_16 + * - \ref WWDT_PRESCALER_32 + * - \ref WWDT_PRESCALER_64 + * - \ref WWDT_PRESCALER_128 + * - \ref WWDT_PRESCALER_192 + * - \ref WWDT_PRESCALER_256 + * - \ref WWDT_PRESCALER_384 + * - \ref WWDT_PRESCALER_512 + * - \ref WWDT_PRESCALER_768 + * - \ref WWDT_PRESCALER_1024 + * - \ref WWDT_PRESCALER_1536 + * - \ref WWDT_PRESCALER_2048 + * @param[in] u32CmpValue Setting the window compared value. Valid values are between 0x0 to 0x3F. + * @param[in] u32EnableInt Enable WWDT interrupt function. Valid values are TRUE and FALSE. + * + * @return None + * + * @details This function make WWDT module start counting with different counter period and compared window value. + * @note Application can call this function valid only once after boot up. + */ +void WWDT_Open(uint32_t u32PreScale, + uint32_t u32CmpValue, + uint32_t u32EnableInt) +{ + WWDT->WWDTCR = u32PreScale | + (u32CmpValue << WWDT_WWDTCR_WINCMP_Pos) | + ((u32EnableInt == TRUE) ? WWDT_WWDTCR_WWDTIE_Msk : 0) | + WWDT_WWDTCR_WWDTEN_Msk; + return; +} + +/*@}*/ /* end of group WWDT_EXPORTED_FUNCTIONS */ + +/*@}*/ /* end of group WWDT_Driver */ + +/*@}*/ /* end of group Standard_Driver */ + +/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/ diff --git a/NUC123/_syscalls.c b/NUC123/_syscalls.c new file mode 100644 index 0000000..a5f6166 --- /dev/null +++ b/NUC123/_syscalls.c @@ -0,0 +1,1168 @@ +// +// This file is part of the uOS++ III distribution +// Parts of this file are from the newlib sources, issued under GPL. +// Copyright (c) 2014 Liviu Ionescu +// + +// ---------------------------------------------------------------------------- + +int errno; +void *__dso_handle __attribute__ ((weak)); + +// ---------------------------------------------------------------------------- + +#if !defined(OS_USE_SEMIHOSTING) + +#include <_ansi.h> +#include <_syslist.h> +#include +//#include +#include +#include +#include +#include +#include + +void +__initialize_args(int* p_argc, char*** p_argv); + +// This is the standard default implementation for the routine to +// process args. It returns a single empty arg. +// For semihosting applications, this is redefined to get the real +// args from the debugger. You can also use it if you decide to keep +// some args in a non-volatile memory. + +void __attribute__((weak)) +__initialize_args(int* p_argc, char*** p_argv) +{ + // By the time we reach this, the data and bss should have been initialised. + + // The strings pointed to by the argv array shall be modifiable by the + // program, and retain their last-stored values between program startup + // and program termination. (static, no const) + static char name[] = ""; + + // The string pointed to by argv[0] represents the program name; + // argv[0][0] shall be the null character if the program name is not + // available from the host environment. argv[argc] shall be a null pointer. + // (static, no const) + static char* argv[2] = + { name, NULL }; + + *p_argc = 1; + *p_argv = &argv[0]; + return; +} + +// These functions are defined here to avoid linker errors in freestanding +// applications. They might be called in some error cases from library +// code. +// +// If you detect other functions to be needed, just let us know +// and we'll add them. + +int +raise(int sig __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int +kill(pid_t pid, int sig); + +int +kill(pid_t pid __attribute__((unused)), int sig __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +#endif // !defined(OS_USE_SEMIHOSTING) + +// ---------------------------------------------------------------------------- + +// If you need the empty definitions, remove the -ffreestanding option. + +#if __STDC_HOSTED__ == 1 + +char* __env[1] = +{ 0 }; +char** environ = __env; + +#if !defined(OS_USE_SEMIHOSTING) + +// Forward declarations + +int +_chown(const char* path, uid_t owner, gid_t group); + +int +_close(int fildes); + +int +_execve(char* name, char** argv, char** env); + +int +_fork(void); + +int +_fstat(int fildes, struct stat* st); + +int +_getpid(void); + +int +_gettimeofday(struct timeval* ptimeval, void* ptimezone); + +int +_isatty(int file); + +int +_kill(int pid, int sig); + +int +_link(char* existing, char* _new); + +int +_lseek(int file, int ptr, int dir); + +int +_open(char* file, int flags, int mode); + +int +_read(int file, char* ptr, int len); + +int +_readlink(const char* path, char* buf, size_t bufsize); + +int +_stat(const char* file, struct stat* st); + +int +_symlink(const char* path1, const char* path2); + +clock_t +_times(struct tms* buf); + +int +_unlink(char* name); + +int +_wait(int* status); + +int +_write(int file, char* ptr, int len); + +// Definitions + +int __attribute__((weak)) +_chown(const char* path __attribute__((unused)), + uid_t owner __attribute__((unused)), gid_t group __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_close(int fildes __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_execve(char* name __attribute__((unused)), char** argv __attribute__((unused)), + char** env __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_fork(void) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_fstat(int fildes __attribute__((unused)), + struct stat* st __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_getpid(void) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_gettimeofday(struct timeval* ptimeval __attribute__((unused)), + void* ptimezone __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_isatty(int file __attribute__((unused))) +{ + errno = ENOSYS; + return 0; +} + +int __attribute__((weak)) +_kill(int pid __attribute__((unused)), int sig __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_link(char* existing __attribute__((unused)), + char* _new __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_lseek(int file __attribute__((unused)), int ptr __attribute__((unused)), + int dir __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_open(char* file __attribute__((unused)), int flags __attribute__((unused)), + int mode __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_read(int file __attribute__((unused)), char* ptr __attribute__((unused)), + int len __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_readlink(const char* path __attribute__((unused)), + char* buf __attribute__((unused)), size_t bufsize __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_stat(const char* file __attribute__((unused)), + struct stat* st __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_symlink(const char* path1 __attribute__((unused)), + const char* path2 __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +clock_t __attribute__((weak)) +_times(struct tms* buf __attribute__((unused))) +{ + errno = ENOSYS; + return ((clock_t) -1); +} + +int __attribute__((weak)) +_unlink(char* name __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_wait(int* status __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +int __attribute__((weak)) +_write(int file __attribute__((unused)), char* ptr __attribute__((unused)), + int len __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +// ---------------------------------------------------------------------------- + +#else // defined(OS_USE_SEMIHOSTING) + +// ---------------------------------------------------------------------------- + +/* Support files for GNU libc. Files in the system namespace go here. + Files in the C namespace (ie those that do not start with an + underscore) go in .c. */ + +#include <_ansi.h> +#include +//#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "semihosting.h" + +int _kill (int pid, int sig); + +void __attribute__((noreturn)) _exit (int status); + +// Forward declarations. +int _system (const char*); +int _rename (const char*, const char*); +int _isatty (int); +clock_t _times (struct tms*); +int _gettimeofday (struct timeval *, void*); +int _unlink (const char*); +int _link (void); + +int _stat (const char*, struct stat*); + +int _fstat (int, struct stat*); +int _swistat (int fd, struct stat* st); +int _getpid (int); +int _close (int); +clock_t _clock (void); +int _swiclose (int); +int _open (const char*, int, ...); +int _swiopen (const char*, int); +int _write (int, char*, int); +int _swiwrite (int, char*, int); +int _lseek (int, int, int); +int _swilseek (int, int, int); +int _read (int, char*, int); +int _swiread (int, char*, int); + +void initialise_monitor_handles (void); + +void __initialize_args (int* p_argc, char*** p_argv); + +static int +checkerror (int); +static int +error (int); +static int +get_errno (void); + +// ---------------------------------------------------------------------------- + +#define ARGS_BUF_ARRAY_SIZE 80 +#define ARGV_BUF_ARRAY_SIZE 10 + +typedef struct +{ + char* pCommandLine; + int size; +} CommandLineBlock; + +void __initialize_args (int* p_argc, char*** p_argv) +{ + // Array of chars to receive the command line from the host + static char args_buf[ARGS_BUF_ARRAY_SIZE]; + + // Array of pointers to store the final argv pointers (pointing + // in the above array). + static char* argv_buf[ARGV_BUF_ARRAY_SIZE]; + + int argc = 0; + int isInArgument = 0; + + CommandLineBlock cmdBlock; + cmdBlock.pCommandLine = args_buf; + cmdBlock.size = sizeof(args_buf) - 1; + + int ret = call_host (SEMIHOSTING_SYS_GET_CMDLINE, &cmdBlock); + if (ret == 0) + { + + // In case the host send more than we can chew, limit the + // string to our buffer. + args_buf[ARGS_BUF_ARRAY_SIZE - 1] = '\0'; + + // The command line is a null terminated string + char* p = cmdBlock.pCommandLine; + + int delim = '\0'; + int ch; + + while ((ch = *p) != '\0') + { + if (isInArgument == 0) + { + if (!isblank(ch)) + { + if (argc >= (int) ((sizeof(argv_buf) / sizeof(argv_buf[0])) - 1)) + break; + + if (ch == '"' || ch == '\'') + { + // Remember the delimiter to search for the + // corresponding terminator + delim = ch; + ++p; // skip the delimiter + ch = *p; + } + // Remember the arg beginning address + argv_buf[argc++] = p; + isInArgument = 1; + } + } + else if (delim != '\0') + { + if ((ch == delim)) + { + delim = '\0'; + *p = '\0'; + isInArgument = 0; + } + } + else if (isblank(ch)) + { + delim = '\0'; + *p = '\0'; + isInArgument = 0; + } + ++p; + } + } + + if (argc == 0) + { + // No args found in string, return a single empty name. + args_buf[0] = '\0'; + argv_buf[0] = &args_buf[0]; + ++argc; + } + + // Must end the array with a null pointer. + argv_buf[argc] = NULL; + + *p_argc = argc; + *p_argv = &argv_buf[0]; + + // temporary here + initialise_monitor_handles (); + + return; +} + +// ---------------------------------------------------------------------------- + +void _exit (int status) +{ + /* There is only one SWI for both _exit and _kill. For _exit, call + the SWI with the second argument set to -1, an invalid value for + signum, so that the SWI handler can distinguish the two calls. + Note: The RDI implementation of _kill throws away both its + arguments. */ + report_exception (status == 0 ? ADP_Stopped_ApplicationExit : ADP_Stopped_RunTimeError); +} + +// ---------------------------------------------------------------------------- + +int __attribute__((weak)) +_kill (int pid __attribute__((unused)), int sig __attribute__((unused))) +{ + errno = ENOSYS; + return -1; +} + +// ---------------------------------------------------------------------------- + +/* Struct used to keep track of the file position, just so we + can implement fseek(fh,x,SEEK_CUR). */ +struct fdent +{ + int handle; + int pos; +}; + +#define MAX_OPEN_FILES 20 + +/* User file descriptors (fd) are integer indexes into + the openfiles[] array. Error checking is done by using + findslot(). + + This openfiles array is manipulated directly by only + these 5 functions: + + findslot() - Translate entry. + newslot() - Find empty entry. + initilise_monitor_handles() - Initialize entries. + _swiopen() - Initialize entry. + _close() - Handle stdout == stderr case. + + Every other function must use findslot(). */ + +static struct fdent openfiles[MAX_OPEN_FILES]; + +static struct fdent* findslot (int); +static int newslot (void); + +/* Register name faking - works in collusion with the linker. */ +register char* stack_ptr asm ("sp"); + +/* following is copied from libc/stdio/local.h to check std streams */ +extern void __sinit(struct _reent*); +#define CHECK_INIT(ptr) \ + do \ + { \ + if ((ptr) && !(ptr)->__sdidinit) \ + __sinit (ptr); \ + } \ + while (0) + +static int monitor_stdin; +static int monitor_stdout; +static int monitor_stderr; + +/* Return a pointer to the structure associated with + the user file descriptor fd. */ +static struct fdent* +findslot (int fd) +{ + CHECK_INIT(_REENT); + + /* User file descriptor is out of range. */ + if ((unsigned int) fd >= MAX_OPEN_FILES) + { + return NULL; + } + + /* User file descriptor is open? */ + if (openfiles[fd].handle == -1) + { + return NULL; + } + + /* Valid. */ + return &openfiles[fd]; +} + +/* Return the next lowest numbered free file + structure, or -1 if we can't find one. */ +static int +newslot (void) +{ + int i; + + for (i = 0; i < MAX_OPEN_FILES; i++) + { + if (openfiles[i].handle == -1) + { + break; + } + } + + if (i == MAX_OPEN_FILES) + { + return -1; + } + + return i; +} + +void +initialise_monitor_handles (void) +{ + int i; + + /* Open the standard file descriptors by opening the special + * teletype device, ":tt", read-only to obtain a descriptor for + * standard input and write-only to obtain a descriptor for standard + * output. Finally, open ":tt" in append mode to obtain a descriptor + * for standard error. Since this is a write mode, most kernels will + * probably return the same value as for standard output, but the + * kernel can differentiate the two using the mode flag and return a + * different descriptor for standard error. + */ + + int volatile block[3]; + + block[0] = (int) ":tt"; + block[2] = 3; /* length of filename */ + block[1] = 0; /* mode "r" */ + monitor_stdin = call_host (SEMIHOSTING_SYS_OPEN, (void*) block); + + block[0] = (int) ":tt"; + block[2] = 3; /* length of filename */ + block[1] = 4; /* mode "w" */ + monitor_stdout = call_host (SEMIHOSTING_SYS_OPEN, (void*) block); + + block[0] = (int) ":tt"; + block[2] = 3; /* length of filename */ + block[1] = 8; /* mode "a" */ + monitor_stderr = call_host (SEMIHOSTING_SYS_OPEN, (void*) block); + + /* If we failed to open stderr, redirect to stdout. */ + if (monitor_stderr == -1) + { + monitor_stderr = monitor_stdout; + } + + for (i = 0; i < MAX_OPEN_FILES; i++) + { + openfiles[i].handle = -1; + } + + openfiles[0].handle = monitor_stdin; + openfiles[0].pos = 0; + openfiles[1].handle = monitor_stdout; + openfiles[1].pos = 0; + openfiles[2].handle = monitor_stderr; + openfiles[2].pos = 0; +} + +static int +get_errno (void) +{ + return call_host (SEMIHOSTING_SYS_ERRNO, NULL); +} + +/* Set errno and return result. */ +static int +error (int result) +{ + errno = get_errno (); + return result; +} + +/* Check the return and set errno appropriately. */ +static int +checkerror (int result) +{ + if (result == -1) + { + return error (-1); + } + + return result; +} + +/* fh, is a valid internal file handle. + ptr, is a null terminated string. + len, is the length in bytes to read. + Returns the number of bytes *not* written. */ +int +_swiread (int fh, char* ptr, int len) +{ + int block[3]; + + block[0] = fh; + block[1] = (int) ptr; + block[2] = len; + + return checkerror (call_host (SEMIHOSTING_SYS_READ, block)); +} + +/* fd, is a valid user file handle. + Translates the return of _swiread into + bytes read. */ +int +_read (int fd, char* ptr, int len) +{ + int res; + struct fdent *pfd; + + pfd = findslot (fd); + if (pfd == NULL) + { + errno = EBADF; + return -1; + } + + res = _swiread (pfd->handle, ptr, len); + + if (res == -1) + { + return res; + } + + pfd->pos += len - res; + + /* res == len is not an error, + at least if we want feof() to work. */ + return len - res; +} + +/* fd, is a user file descriptor. */ +int _swilseek (int fd, int ptr, int dir) +{ + int res; + struct fdent *pfd; + + /* Valid file descriptor? */ + pfd = findslot (fd); + if (pfd == NULL) + { + errno = EBADF; + return -1; + } + + /* Valid whence? */ + if ((dir != SEEK_CUR) && (dir != SEEK_SET) && (dir != SEEK_END)) + { + errno = EINVAL; + return -1; + } + + /* Convert SEEK_CUR to SEEK_SET */ + if (dir == SEEK_CUR) + { + ptr = pfd->pos + ptr; + /* The resulting file offset would be negative. */ + if (ptr < 0) + { + errno = EINVAL; + if ((pfd->pos > 0) && (ptr > 0)) + { + errno = EOVERFLOW; + } + return -1; + } + dir = SEEK_SET; + } + + int block[2]; + if (dir == SEEK_END) + { + block[0] = pfd->handle; + res = checkerror (call_host (SEMIHOSTING_SYS_FLEN, block)); + if (res == -1) + { + return -1; + } + ptr += res; + } + + /* This code only does absolute seeks. */ + block[0] = pfd->handle; + block[1] = ptr; + res = checkerror (call_host (SEMIHOSTING_SYS_SEEK, block)); + + /* At this point ptr is the current file position. */ + if (res >= 0) + { + pfd->pos = ptr; + return ptr; + } + else + { + return -1; + } +} + +int _lseek (int fd, int ptr, int dir) +{ + return _swilseek (fd, ptr, dir); +} + +/* fh, is a valid internal file handle. + Returns the number of bytes *not* written. */ +int _swiwrite (int fh, char* ptr, int len) +{ + int block[3]; + + block[0] = fh; + block[1] = (int) ptr; + block[2] = len; + + return checkerror (call_host (SEMIHOSTING_SYS_WRITE, block)); +} + +/* fd, is a user file descriptor. */ +int _write (int fd, char* ptr, int len) +{ + int res; + struct fdent *pfd; + + pfd = findslot (fd); + if (pfd == NULL) + { + errno = EBADF; + return -1; + } + + res = _swiwrite (pfd->handle, ptr, len); + + /* Clearly an error. */ + if (res < 0) + { + return -1; + } + + pfd->pos += len - res; + + /* We wrote 0 bytes? + Retrieve errno just in case. */ + if ((len - res) == 0) + { + return error (0); + } + + return (len - res); +} + +int _swiopen (const char* path, int flags) +{ + int aflags = 0, fh; + uint32_t block[3]; + + int fd = newslot (); + + if (fd == -1) + { + errno = EMFILE; + return -1; + } + + /* It is an error to open a file that already exists. */ + if ((flags & O_CREAT) && (flags & O_EXCL)) + { + struct stat st; + int res; + res = _stat (path, &st); + if (res != -1) + { + errno = EEXIST; + return -1; + } + } + + /* The flags are Unix-style, so we need to convert them. */ +#ifdef O_BINARY + if (flags & O_BINARY) + { + aflags |= 1; + } +#endif + + /* In O_RDONLY we expect aflags == 0. */ + + if (flags & O_RDWR) + { + aflags |= 2; + } + + if ((flags & O_CREAT) || (flags & O_TRUNC) || (flags & O_WRONLY)) + { + aflags |= 4; + } + + if (flags & O_APPEND) + { + /* Can't ask for w AND a; means just 'a'. */ + aflags &= ~4; + aflags |= 8; + } + + block[0] = (uint32_t) path; + block[2] = strlen (path); + block[1] = (uint32_t) aflags; + + fh = call_host (SEMIHOSTING_SYS_OPEN, block); + + /* Return a user file descriptor or an error. */ + if (fh >= 0) + { + openfiles[fd].handle = fh; + openfiles[fd].pos = 0; + return fd; + } + else + { + return error (fh); + } +} + +int _open (const char* path, int flags, ...) +{ + return _swiopen (path, flags); +} + +/* fh, is a valid internal file handle. */ +int _swiclose (int fh) +{ + return checkerror (call_host (SEMIHOSTING_SYS_CLOSE, &fh)); +} + +/* fd, is a user file descriptor. */ +int _close (int fd) +{ + int res; + struct fdent *pfd; + + pfd = findslot (fd); + if (pfd == NULL) + { + errno = EBADF; + return -1; + } + + /* Handle stderr == stdout. */ + if ((fd == 1 || fd == 2) && (openfiles[1].handle == openfiles[2].handle)) + { + pfd->handle = -1; + return 0; + } + + /* Attempt to close the handle. */ + res = _swiclose (pfd->handle); + + /* Reclaim handle? */ + if (res == 0) + { + pfd->handle = -1; + } + + return res; +} + +int __attribute__((weak)) +_getpid (int n __attribute__ ((unused))) +{ + return 1; +} + +int +_swistat (int fd, struct stat* st) +{ + struct fdent *pfd; + int res; + + pfd = findslot (fd); + if (pfd == NULL) + { + errno = EBADF; + return -1; + } + + /* Always assume a character device, + with 1024 byte blocks. */ + st->st_mode |= S_IFCHR; + st->st_blksize = 1024; + res = checkerror (call_host (SEMIHOSTING_SYS_FLEN, &pfd->handle)); + if (res == -1) + { + return -1; + } + + /* Return the file size. */ + st->st_size = res; + return 0; +} + +int __attribute__((weak)) +_fstat (int fd, struct stat* st) +{ + memset (st, 0, sizeof(*st)); + return _swistat (fd, st); +} + +int __attribute__((weak)) +_stat (const char*fname, struct stat *st) +{ + int fd, res; + memset (st, 0, sizeof(*st)); + /* The best we can do is try to open the file readonly. + If it exists, then we can guess a few things about it. */ + if ((fd = _open (fname, O_RDONLY)) == -1) + { + return -1; + } + st->st_mode |= S_IFREG | S_IREAD; + res = _swistat (fd, st); + /* Not interested in the error. */ + _close (fd); + return res; +} + +int __attribute__((weak)) +_link (void) +{ + errno = ENOSYS; + return -1; +} + +int _unlink (const char* path) +{ + int res; + uint32_t block[2]; + block[0] = (uint32_t) path; + block[1] = strlen (path); + res = call_host (SEMIHOSTING_SYS_REMOVE, block); + + if (res == -1) + { + return error (res); + } + return 0; +} + +int _gettimeofday (struct timeval* tp, void* tzvp) +{ + struct timezone* tzp = tzvp; + if (tp) + { + /* Ask the host for the seconds since the Unix epoch. */ + tp->tv_sec = call_host (SEMIHOSTING_SYS_TIME, NULL); + tp->tv_usec = 0; + } + + /* Return fixed data for the timezone. */ + if (tzp) + { + tzp->tz_minuteswest = 0; + tzp->tz_dsttime = 0; + } + + return 0; +} + +/* Return a clock that ticks at 100Hz. */ +clock_t _clock (void) +{ + clock_t timeval; + + timeval = (clock_t) call_host (SEMIHOSTING_SYS_CLOCK, NULL); + return timeval; +} + +/* Return a clock that ticks at 100Hz. */ +clock_t +_times (struct tms* tp) +{ + clock_t timeval = _clock (); + + if (tp) + { + tp->tms_utime = timeval; /* user time */ + tp->tms_stime = 0; /* system time */ + tp->tms_cutime = 0; /* user time, children */ + tp->tms_cstime = 0; /* system time, children */ + } + + return timeval; +} + +int _isatty (int fd) +{ + struct fdent *pfd; + int tty; + + pfd = findslot (fd); + if (pfd == NULL) + { + errno = EBADF; + return 0; + } + + tty = call_host (SEMIHOSTING_SYS_ISTTY, &pfd->handle); + + if (tty == 1) + { + return 1; + } + + errno = get_errno (); + return 0; +} + +int _system (const char* s) +{ + uint32_t block[2]; + int e; + + /* Hmmm. The ARM debug interface specification doesn't say whether + SYS_SYSTEM does the right thing with a null argument, or assign any + meaning to its return value. Try to do something reasonable.... */ + if (!s) + { + return 1; /* maybe there is a shell available? we can hope. :-P */ + } + block[0] = (uint32_t) s; + block[1] = strlen (s); + e = checkerror (call_host (SEMIHOSTING_SYS_SYSTEM, block)); + if ((e >= 0) && (e < 256)) + { + /* We have to convert e, an exit status to the encoded status of + the command. To avoid hard coding the exit status, we simply + loop until we find the right position. */ + int exit_code; + + for (exit_code = e; e && WEXITSTATUS (e) != exit_code; e <<= 1) + { + continue; + } + } + return e; +} + +int _rename (const char* oldpath, const char* newpath) +{ + uint32_t block[4]; + block[0] = (uint32_t) oldpath; + block[1] = strlen (oldpath); + block[2] = (uint32_t) newpath; + block[3] = strlen (newpath); + return checkerror (call_host (SEMIHOSTING_SYS_RENAME, block)) ? -1 : 0; +} + +// ---------------------------------------------------------------------------- +// Required by Google Tests + +int mkdir (const char *path __attribute__((unused)), mode_t mode __attribute__((unused))) +{ +#if 0 + // always return true + return 0; +#else + errno = ENOSYS; + return -1; +#endif +} + +char *getcwd (char *buf, size_t size) +{ + // no cwd available via semihosting, so we use the temporary folder + strncpy (buf, "/tmp", size); + return buf; +} + +#endif // defined OS_USE_SEMIHOSTING + +#endif // __STDC_HOSTED__ == 1 diff --git a/NUC123/inc/NUC123.h b/NUC123/inc/NUC123.h new file mode 100644 index 0000000..24277e1 --- /dev/null +++ b/NUC123/inc/NUC123.h @@ -0,0 +1,8763 @@ +/**************************************************************************//** + * @file NUC123.h + * @version V3.0 + * $Revision: 85 $ + * $Date: 17/05/26 10:49a $ + * @brief NUC123 Series Peripheral Access Layer Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2016 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ + +/** + \mainpage Introduction + * + * + * This user manual describes the usage of NUC123 Series MCU device driver + * + * Disclaimer + * + * The Software is furnished "AS IS", without warranty as to performance or results, and + * the entire risk as to performance or results is assumed by YOU. Nuvoton disclaims all + * warranties, express, implied or otherwise, with regard to the Software, its use, or + * operation, including without limitation any and all warranties of merchantability, fitness + * for a particular purpose, and non-infringement of intellectual property rights. + * + * Copyright Notice + * + * Copyright (C) 2014~2016 Nuvoton Technology Corp. All rights reserved. + */ + + +#ifndef __NUC123_H__ +#define __NUC123_H__ + +/* + * ========================================================================== + * ---------- Interrupt Number Definition ----------------------------------- + * ========================================================================== +*/ + +/** @addtogroup MCU_CMSIS Device Definitions for CMSIS + Interrupt Number Definition and Configurations for CMSIS + @{ +*/ + +/** + * @details Interrupt Number Definition. The maximum of 32 Specific Interrupts are possible. + */ + +typedef enum IRQn +{ + /****** Cortex-M0 Processor Exceptions Numbers ***************************************************/ + NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */ + HardFault_IRQn = -13, /*!< 3 Cortex-M0 Hard Fault Interrupt */ + SVCall_IRQn = -5, /*!< 11 Cortex-M0 SV Call Interrupt */ + PendSV_IRQn = -2, /*!< 14 Cortex-M0 Pend SV Interrupt */ + SysTick_IRQn = -1, /*!< 15 Cortex-M0 System Tick Interrupt */ + + /****** ARMIKMCU Swift specific Interrupt Numbers ************************************************/ + BOD_IRQn = 0, /*!< Brown-Out Low Voltage Detected Interrupt */ + WDT_IRQn = 1, /*!< Watch Dog Timer Interrupt */ + EINT0_IRQn = 2, /*!< EINT0 Interrupt */ + EINT1_IRQn = 3, /*!< EINT1 Interrupt */ + GPAB_IRQn = 4, /*!< GPIO_PA/PB Interrupt */ + GPCDF_IRQn = 5, /*!< GPIO_PC/PD/PF Interrupt */ + PWMA_IRQn = 6, /*!< PWMA Interrupt */ + TMR0_IRQn = 8, /*!< TIMER0 Interrupt */ + TMR1_IRQn = 9, /*!< TIMER1 Interrupt */ + TMR2_IRQn = 10, /*!< TIMER2 Interrupt */ + TMR3_IRQn = 11, /*!< TIMER3 Interrupt */ + UART0_IRQn = 12, /*!< UART0 Interrupt */ + UART1_IRQn = 13, /*!< UART1 Interrupt */ + SPI0_IRQn = 14, /*!< SPI0 Interrupt */ + SPI1_IRQn = 15, /*!< SPI1 Interrupt */ + SPI2_IRQn = 16, /*!< SPI2 Interrupt */ + I2C0_IRQn = 18, /*!< I2C0 Interrupt */ + I2C1_IRQn = 19, /*!< I2C1 Interrupt */ + CAN0_IRQn = 20, /*!< CAN0 Interrupt */ + CAN1_IRQn = 21, /*!< CAN1 Interrupt */ + USBD_IRQn = 23, /*!< USB device Interrupt */ + PS2_IRQn = 24, /*!< PS/2 device Interrupt */ + PDMA_IRQn = 26, /*!< PDMA Interrupt */ + I2S_IRQn = 27, /*!< I2S Interrupt */ + PWRWU_IRQn = 28, /*!< Power Down Wake Up Interrupt */ + ADC_IRQn = 29, /*!< ADC Interrupt */ + IRC_IRQn = 30, /*!< IRC TRIM Interrupt */ +} IRQn_Type; + + +/* + * ========================================================================== + * ----------- Processor and Core Peripheral Section ------------------------ + * ========================================================================== + */ + +/* Configuration of the Cortex-M0 Processor and Core Peripherals */ +#define __MPU_PRESENT 0 /*!< armikcmu does not provide a MPU present or not */ +#define __NVIC_PRIO_BITS 2 /*!< armikcmu Supports 2 Bits for the Priority Levels */ +#define __Vendor_SysTickConfig 0 /*!< Set to 1 if different SysTick Config is used */ + + +/*@}*/ /* end of group MCU_CMSIS */ + + +#include "core_cm0.h" /* Cortex-M0 processor and core peripherals */ +#include "system_NUC123.h" /* NUC123 System */ + +#if defined ( __CC_ARM ) +#pragma anon_unions +#endif + + +/*-------------------------------- Device Specific Peripheral registers structures ---------------------*/ +/** @addtogroup REGISTER Control Register + Peripheral Control Registers + @{ + */ + +/*----------------------------- ADC Controller -------------------------------*/ +/** @addtogroup REG_ADC Analog to Digital Converter (ADC) + Memory Mapped Structure for ADC Controller + @{ + */ + +typedef struct +{ + + + +/** + * @var ADC_T::ADDR + * Offset: 0x00-0x1C ADC Data Register x + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[9:0] |RSLT |A/D Conversion Result + * | | |This field contains conversion result of ADC. + * |[16] |OVERRUN |Overrun Flag (Read Only) + * | | |If converted data in RSLT has not been read before new conversion result is loaded to this register, OVERRUN is set to 1 and previous conversion result is gone. + * | | |It is cleared by hardware after ADDR register is read. + * | | |0 = Data in RSLT (ADDRx[9:0], x=0~7) is recent conversion result. + * | | |1 = Data in RSLT (ADDRx[9:0], x=0~7) is overwritten. + * |[17] |VALID |Valid Flag (Read Only) + * | | |This bit is set to 1 when corresponding channel analog input conversion is completed and cleared by hardware after ADDR register is read. + * | | |0 = Data in RSLT bits (ADDRx[9:0], x=0~7) is not valid. + * | | |1 = Data in RSLT bits (ADDRx[9:0], x=0~7) is valid. + * @var ADC_T::ADCR + * Offset: 0x20 ADC Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |ADEN |A/D Converter Enable Bit + * | | |Before starting A/D conversion function, this bit should be set to 1. + * | | |Clear it to 0 to disable A/D converter analog circuit for saving power consumption. + * | | |0 = A/D converter Disabled. + * | | |1 = A/D converter Enabled. + * |[1] |ADIE |A/D Interrupt Enable Bit + * | | |A/D conversion end interrupt request is generated if ADIE bit is set to 1. + * | | |0 = A/D interrupt function Disabled. + * | | |1 = A/D interrupt function Enabled. + * |[3:2] |ADMD |A/D Converter Operation Mode + * | | |00 = Single conversion. + * | | |01 = Reserved. + * | | |10 = Single-cycle scan. + * | | |11 = Continuous scan. + * | | |When changing the operation mode, software should disable ADST bit (ADCR[11]) firstly. + * |[5:4] |TRGS |Hardware Trigger Source + * | | |00 = A/D conversion is started by external STADC pin. + * | | |11 = A/D conversion is started by PWM Center-aligned trigger. + * | | |Others = Reserved. + * | | |Note: TRGEN (ADCR[8]) and ADST (ADCR[11]) shall be cleared to 0 before changing TRGS. + * |[7:6] |TRGCOND |External Trigger Condition + * | | |These two bits decide external pin STADC trigger event is level or edge. + * | | |The signal must be kept at stable state at least 8 PCLKs for level trigger and 4 PCLKs at high and low state for edge trigger. + * | | |00 = Low level. + * | | |01 = High level. + * | | |10 = Falling edge. + * | | |11 = Rising edge. + * |[8] |TRGEN |Hardware Trigger Enable Bit + * | | |Enable or disable triggering of A/D conversion by external STADC pin or by PWM trigger. + * | | |0 = External trigger Disabled. + * | | |1 = External trigger Enabled. + * | | |Note: ADC hardware trigger function is only supported in single-cycle scan mode. + * | | |If hardware trigger is enabled, the ADST bit can be set to 1 by the selected hardware trigger source. + * |[9] |PTEN |PDMA Transfer Enable Bit + * | | |When A/D conversion is completed, the converted data is loaded into ADDR 0~7, software can enable this bit to generate a PDMA data transfer request. + * | | |When PTEN=1, software must set ADIE=0 (ADCR[1]) to disable interrupt. + * | | |0 = PDMA data transfer Disabled. + * | | |1 = PDMA data transfer in ADDR 0~7 Enabled. + * |[11] |ADST |A/D Conversion Start + * | | |ADST bit can be set to 1 from three sources: software, STADC pin and PWM output. + * | | |ADST will be cleared to 0 by hardware automatically at the ends of single mode and single-cycle scan mode. + * | | |In continuous scan mode, A/D conversion is continuously performed until software writes 0 to this bit or chip reset. + * | | |0 = Conversion stopped and A/D converter entering idle state. + * | | |1 = Conversion started. + * | | |Note: when ADST is clear to 0 by hardware automatically in single mode, user need to wait one ADC_CLK cycle for next A/D conversion. + * @var ADC_T::ADCHER + * Offset: 0x24 ADC Channel Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |CHEN |Analog Input Channel Enable Bit + * | | |Set CHEN[7:0] to enable the corresponding analog input channel 7 ~ 0. + * | | |0 = Channel Disabled. + * | | |1 = Channel Enabled. + * |[8] |PRESEL |Analog Input Channel 7 Select + * | | |0 = External analog input. + * | | |1 = Internal band-gap voltage. + * | | |Note: + * | | |When software selects the band-gap voltage as the analog input source of ADC channel 7, ADC clock rate needs to be limited to slower than 300 kHz. + * @var ADC_T::ADCMPR + * Offset: 0x28-0x2C ADC Compare Register x + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CMPEN |Compare Enable Bit + * | | |Set this bit to 1 to enable ADC controller to compare CMPD (ADCMPRx[25:16]) with specified channel conversion result when converted data is loaded into ADDRx register. + * | | |0 = Compare function Disabled. + * | | |1 = Compare function Enabled. + * |[1] |CMPIE |Compare Interrupt Enable Bit + * | | |If the compare function is enabled and the compare condition matches the setting of + * | | |CMPCOND (ADCMPRx[2]) and CMPMATCNT (ADCMPRx[11:8]), CMPFx bit (ADSR[2:1]) + * | | |will be set, in the meanwhile, if CMPIE (ADCMPRx[1]) is set to 1, a compare interrupt + * | | |request is generated. + * | | |0 = Compare function interrupt Disabled. + * | | |1 = Compare function interrupt Enabled. + * |[2] |CMPCOND |Compare Condition + * | | |0 = Set the compare condition as that when a 10-bit A/D conversion result is less than the 10-bit CMPD (ADCMPRx[25:16]), the internal match counter will increase one. + * | | |1 = Set the compare condition as that when a 10-bit A/D conversion result is greater or equal to the 10-bit CMPD (ADCMPRx[25:16]), the internal match counter will increase one. + * | | |Note: When the internal counter reaches the value to (CMPMATCNT+1), the CMPFx bit will be set. + * |[5:3] |CMPCH |Compare Channel Selection + * | | |000 = Channel 0 conversion result is selected to be compared. + * | | |001 = Channel 1 conversion result is selected to be compared. + * | | |010 = Channel 2 conversion result is selected to be compared. + * | | |011 = Channel 3 conversion result is selected to be compared. + * | | |100 = Channel 4 conversion result is selected to be compared. + * | | |101 = Channel 5 conversion result is selected to be compared. + * | | |110 = Channel 6 conversion result is selected to be compared. + * | | |111 = Channel 7 conversion result is selected to be compared. + * |[11:8] |CMPMATCNT |Compare Match Count + * | | |When the specified A/D channel analog conversion result matches the compare condition defined by CMPCOND (ADCMPRx[2]), the internal match counter will increase 1, + * | | |otherwise, the compare match counter will be cleared to 0. + * | | |When the internal counter reaches the value to (CMPMATCNT+1), CMPFx bit (ADSR[2:1]) will be set. + * |[25:16] |CMPD |Comparison Data + * | | |The 10-bit data is used to compare with conversion result of specified channel. + * @var ADC_T::ADSR + * Offset: 0x30 ADC Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |ADF |A/D Conversion End Flag + * | | |A status flag that indicates the end of A/D conversion. + * | | |ADF is set to 1 at these two conditions: + * | | |1. When A/D conversion ends in Single mode. + * | | |2. When A/D conversion ends on all specified channels in Scan mode. + * | | |This flag can be cleared by writing 1 to itself. + * |[1] |CMPF0 |Compare Flag + * | | |When the selected channel A/D conversion result meets setting condition in ADCMPR0 then this bit is set to 1. + * | | |And it is cleared by writing 1 to self. + * | | |0 = Conversion result in ADDR does not meet ADCMPR0 setting. + * | | |1 = Conversion result in ADDR meets ADCMPR0 setting. + * |[2] |CMPF1 |Compare Flag + * | | |When the selected channel A/D conversion result meets setting condition in ADCMPR1 then this bit is set to 1. + * | | |And it is cleared by writing 1 to self. + * | | |0 = Conversion result in ADDR does not meet ADCMPR1 setting. + * | | |1 = Conversion result in ADDR meets ADCMPR1 setting. + * |[3] |BUSY |BUSY/IDLE (Read Only) + * | | |0 = A/D converter is in idle state. + * | | |1 = A/D converter is busy at conversion. + * | | |This bit is a mirror of ADST bit in ADCR. + * |[6:4] |CHANNEL |Current Conversion Channel (Read Only) + * | | |This field reflects the current conversion channel when BUSY = 1 (ADSR[3]). + * | | |When BUSY(ADSR[3]) = 0, it shows the number of the next converted channel. + * |[15:8] |VALID |Data Valid Flag (Read Only) + * | | |VALID[7:0] is a mirror of the VALID bits in ADDR7[17] ~ ADDR0[17]. + * | | |It is read only. + * |[23:16] |OVERRUN |Overrun Flag (Read Only) + * | | |OVERRUN[7:0] is a mirror of the OVERRUN bits in ADDR7[16] ~ ADDR0[16]. + * @var ADC_T::ADPDMA + * Offset: 0x40 ADC PDMA Current Transfer Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[9:0] |AD_PDMA |ADC PDMA Current Transfer Data Register (Read Only) + * | | |When transferring A/D conversion result with PDMA, software can read this register to monitor current PDMA transfer data. + */ + + __I uint32_t ADDR[8]; /* Offset: 0x00-0x1C ADC Data Register x */ + __IO uint32_t ADCR; /* Offset: 0x20 ADC Control Register */ + __IO uint32_t ADCHER; /* Offset: 0x24 ADC Channel Enable Register */ + __IO uint32_t ADCMPR[2]; /* Offset: 0x28-0x2C ADC Compare Register x */ + __IO uint32_t ADSR; /* Offset: 0x30 ADC Status Register */ + __I uint32_t RESERVE0[3]; + __I uint32_t ADPDMA; /* Offset: 0x40 ADC PDMA Current Transfer Data Register */ + +} ADC_T; + + + +/** @addtogroup REG_ADC_BITMASK ADC Bit Mask + @{ + */ + +/* ADDR Bit Field Definitions */ +#define ADC_ADDR_VALID_Pos 17 /*!< ADC_T::ADDR: VALID Position */ +#define ADC_ADDR_VALID_Msk (1ul << ADC_ADDR_VALID_Pos) /*!< ADC_T::ADDR: VALID Mask */ + +#define ADC_ADDR_OVERRUN_Pos 16 /*!< ADC_T::ADDR: OVERRUN Position */ +#define ADC_ADDR_OVERRUN_Msk (1ul << ADC_ADDR_OVERRUN_Pos) /*!< ADC_T::ADDR: OVERRUN Mask */ + +#define ADC_ADDR_RSLT_Pos 0 /*!< ADC_T::ADDR: RSLT Position */ +#define ADC_ADDR_RSLT_Msk (0x3FFul << ADC_ADDR_RSLT_Pos) /*!< ADC_T::ADDR: RSLT Mask */ + +/* ADCR Bit Field Definitions */ +#define ADC_ADCR_ADST_Pos 11 /*!< ADC_T::ADCR: ADST Position */ +#define ADC_ADCR_ADST_Msk (1ul << ADC_ADCR_ADST_Pos) /*!< ADC_T::ADCR: ADST Mask */ + +#define ADC_ADCR_PTEN_Pos 9 /*!< ADC_T::ADCR: PTEN Position */ +#define ADC_ADCR_PTEN_Msk (1ul << ADC_ADCR_PTEN_Pos) /*!< ADC_T::ADCR: PTEN Mask */ + +#define ADC_ADCR_TRGEN_Pos 8 /*!< ADC_T::ADCR: TRGEN Position */ +#define ADC_ADCR_TRGEN_Msk (1ul << ADC_ADCR_TRGEN_Pos) /*!< ADC_T::ADCR: TRGEN Mask */ + +#define ADC_ADCR_TRGCOND_Pos 6 /*!< ADC_T::ADCR: TRGCOND Position */ +#define ADC_ADCR_TRGCOND_Msk (3ul << ADC_ADCR_TRGCOND_Pos) /*!< ADC_T::ADCR: TRGCOND Mask */ + +#define ADC_ADCR_TRGS_Pos 4 /*!< ADC_T::ADCR: TRGS Position */ +#define ADC_ADCR_TRGS_Msk (3ul << ADC_ADCR_TRGS_Pos) /*!< ADC_T::ADCR: TRGS Mask */ + +#define ADC_ADCR_ADMD_Pos 2 /*!< ADC_T::ADCR: ADMD Position */ +#define ADC_ADCR_ADMD_Msk (3ul << ADC_ADCR_ADMD_Pos) /*!< ADC_T::ADCR: ADMD Mask */ + +#define ADC_ADCR_ADIE_Pos 1 /*!< ADC_T::ADCR: ADIE Position */ +#define ADC_ADCR_ADIE_Msk (1ul << ADC_ADCR_ADIE_Pos) /*!< ADC_T::ADCR: ADIE Mask */ + +#define ADC_ADCR_ADEN_Pos 0 /*!< ADC_T::ADCR: ADEN Position */ +#define ADC_ADCR_ADEN_Msk (1ul << ADC_ADCR_ADEN_Pos) /*!< ADC_T::ADCR: ADEN Mask */ + +/* ADCHER Bit Field Definitions */ +#define ADC_ADCHER_PRESEL_Pos 8 /*!< ADC_T::ADCHER: PRESEL Position */ +#define ADC_ADCHER_PRESEL_Msk (1ul << ADC_ADCHER_PRESEL_Pos) /*!< ADC_T::ADCHER: PRESEL Mask */ + +#define ADC_ADCHER_CHEN_Pos 0 /*!< ADC_T::ADCHER: CHEN Position */ +#define ADC_ADCHER_CHEN_Msk (0xFFul << ADC_ADCHER_CHEN_Pos) /*!< ADC_T::ADCHER: CHEN Mask */ + +/* ADCMPR Bit Field Definitions */ +#define ADC_ADCMPR_CMPD_Pos 16 /*!< ADC_T::ADCMPR: CMPD Position */ +#define ADC_ADCMPR_CMPD_Msk (0x3FFul << ADC_ADCMPR_CMPD_Pos) /*!< ADC_T::ADCMPR: CMPD Mask */ + +#define ADC_ADCMPR_CMPMATCNT_Pos 8 /*!< ADC_T::ADCMPR: CMPMATCNT Position */ +#define ADC_ADCMPR_CMPMATCNT_Msk (0xFul << ADC_ADCMPR_CMPMATCNT_Pos) /*!< ADC_T::ADCMPR: CMPMATCNT Mask */ + +#define ADC_ADCMPR_CMPCH_Pos 3 /*!< ADC_T::ADCMPR: CMPCH Position */ +#define ADC_ADCMPR_CMPCH_Msk (7ul << ADC_ADCMPR_CMPCH_Pos) /*!< ADC_T::ADCMPR: CMPCH Mask */ + +#define ADC_ADCMPR_CMPCOND_Pos 2 /*!< ADC_T::ADCMPR: CMPCOND Position */ +#define ADC_ADCMPR_CMPCOND_Msk (1ul << ADC_ADCMPR_CMPCOND_Pos) /*!< ADC_T::ADCMPR: CMPCOND Mask */ + +#define ADC_ADCMPR_CMPIE_Pos 1 /*!< ADC_T::ADCMPR: CMPIE Position */ +#define ADC_ADCMPR_CMPIE_Msk (1ul << ADC_ADCMPR_CMPIE_Pos) /*!< ADC_T::ADCMPR: CMPIE Mask */ + +#define ADC_ADCMPR_CMPEN_Pos 0 /*!< ADC_T::ADCMPR: CMPEN Position */ +#define ADC_ADCMPR_CMPEN_Msk (1ul << ADC_ADCMPR_CMPEN_Pos) /*!< ADC_T::ADCMPR: CMPEN Mask */ + +/* ADSR Bit Field Definitions */ +#define ADC_ADSR_OVERRUN_Pos 16 /*!< ADC_T::ADSR: OVERRUN Position */ +#define ADC_ADSR_OVERRUN_Msk (0xFFul << ADC_ADSR_OVERRUN_Pos) /*!< ADC_T::ADSR: OVERRUN Mask */ + +#define ADC_ADSR_VALID_Pos 8 /*!< ADC_T::ADSR: VALID Position */ +#define ADC_ADSR_VALID_Msk (0xFFul << ADC_ADSR_VALID_Pos) /*!< ADC_T::ADSR: VALID Mask */ + +#define ADC_ADSR_CHANNEL_Pos 4 /*!< ADC_T::ADSR: CHANNEL Position */ +#define ADC_ADSR_CHANNEL_Msk (7ul << ADC_ADSR_CHANNEL_Pos) /*!< ADC_T::ADSR: CHANNEL Mask */ + +#define ADC_ADSR_BUSY_Pos 3 /*!< ADC_T::ADSR: BUSY Position */ +#define ADC_ADSR_BUSY_Msk (1ul << ADC_ADSR_BUSY_Pos) /*!< ADC_T::ADSR: BUSY Mask */ + +#define ADC_ADSR_CMPF1_Pos 2 /*!< ADC_T::ADSR: CMPF1 Position */ +#define ADC_ADSR_CMPF1_Msk (1ul << ADC_ADSR_CMPF1_Pos) /*!< ADC_T::ADSR: CMPF1 Mask */ + +#define ADC_ADSR_CMPF0_Pos 1 /*!< ADC_T::ADSR: CMPF0 Position */ +#define ADC_ADSR_CMPF0_Msk (1ul << ADC_ADSR_CMPF0_Pos) /*!< ADC_T::ADSR: CMPF0 Mask */ + +#define ADC_ADSR_ADF_Pos 0 /*!< ADC_T::ADSR: ADF Position */ +#define ADC_ADSR_ADF_Msk (1ul << ADC_ADSR_ADF_Pos) /*!< ADC_T::ADSR: ADF Mask */ + +/* ADPDMA Bit Field Definitions */ +#define ADC_ADPDMA_AD_PDMA_Pos 0 /*!< ADC_T::ADPDMA: AD_PDMA Position */ +#define ADC_ADPDMA_AD_PDMA_Msk (0x3FFul << ADC_ADPDMA_AD_PDMA_Pos) /*!< ADC_T::ADPDMA: AD_PDMA Mask */ +/*@}*/ /* end of group REG_ADC_BITMASK */ +/*@}*/ /* end of group REG_ADC */ + + +/*---------------------------- Clock Controller ------------------------------*/ +/** @addtogroup REG_CLK System Clock Controller (CLK) + Memory Mapped Structure for System Clock Controller + @{ + */ + +typedef struct +{ + + + +/** + * @var CLK_T::PWRCON + * Offset: 0x00 System Power-down Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |XTL12M_EN |External 4~24 MHz High Speed Crystal Enable (HXT) Control (Write Protect) + * | | |The bit default value is set by flash controller user configuration register CFOSC (Config0[26:24]). + * | | |When the default clock source is from external 4~24 MHz high speed crystal, this bit is set to 1 automatically. + * | | |0 = External 4~24 MHz high speed crystal oscillator (HXT) Disabled. + * | | |1 = External 4~24 MHz high speed crystal oscillator (HXT) Enabled. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * |[2] |OSC22M_EN |Internal 22.1184 MHz High Speed Oscillator (HIRC) Enable Control (Write Protect) + * | | |0 = Internal 22.1184 MHz high speed oscillator (HIRC) Disabled. + * | | |1 = Internal 22.1184 MHz high speed oscillator (HIRC) Enabled. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * |[3] |OSC10K_EN |Internal 10 KHz Low Speed Oscillator (LIRC) Enable Control (Write Protect) + * | | |0 = Internal 10 kHz low speed oscillator (LIRC) Disabled. + * | | |1 = Internal 10 kHz low speed oscillator (LIRC) Enabled. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * |[4] |PD_WU_DLY |Wake-up Delay Counter Enable Control (Write Protect) + * | | |When the chip wakes up from Power-down mode, the clock control will delay certain clock cycles to wait system clock stable. + * | | |The delayed clock cycle is 4096 clock cycles when chip work at external 4~24 MHz high speed crystal, and 256 clock cycles when chip work at internal 22.1184 MHz high speed oscillator. + * | | |0 = Clock cycles delay Disabled. + * | | |1 = Clock cycles delay Enabled. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * |[5] |PD_WU_INT_EN|Power-Down Mode Wake-Up Interrupt Enable Control (Write Protect) + * | | |0 = Power-down mode wake-up interrupt Disabled. + * | | |1 = Power-down mode wake-up interrupt Enabled. + * | | |Note1: The interrupt will occur when both PD_WU_STS and PD_WU_INT_EN are high. + * | | |Note2: This bit is write protected bit. Refer to the REGWRPROT register. + * |[6] |PD_WU_STS |Power-down Mode Wake-Up Interrupt Status + * | | |Set by "Power-down wake-up event", it indicates that resume from Power-down mode. + * | | |The flag is set if the GPIO, USB, UART, WDT, TIMER, I2C or BOD wake-up occurred. + * | | |This bit can be cleared to 0 by software writing "1". + * | | |Note: This bit is working only if PD_WU_INT_EN (PWRCON[5]) set to 1. + * |[7] |PWR_DOWN_EN|System Power-down Enable Bit (Write Protect) + * | | |When this bit is set to 1, Power-down mode is enabled and chip Power-down behavior will depends on the PD_WAIT_CPU bit + * | | |(a) If the PD_WAIT_CPU is 0, then the chip enters Power-down mode immediately after the PWR_DOWN_EN bit set. + * | | |(b) if the PD_WAIT_CPU is 1, then the chip keeps active till the CPU sleep mode is also active and then the chip enters Power-down mode (recommend) + * | | |When chip wakes up from Power-down mode, this bit is cleared by hardware. + * | | |User needs to set this bit again for next Power-down. + * | | |In Power-down mode, 4~24 MHz external high speed crystal oscillator (HXT) and the 22.1184 MHz internal high speed RC oscillator (HIRC) will be disabled in this mode, but the 10 kHz internal low speed RC oscillator (LIRC) is not controlled by Power-down mode. + * | | |In Power- down mode, the PLL and system clock are disabled, and ignored the clock source selection. The clocks of peripheral are not controlled by Power-down mode, if the peripheral clock source is from the 10 kHz internal low speed RC oscillator (LIRC). + * | | |The clocks of peripheral are not controlled by Power-down mode, if the peripheral clock source is from the internal 10 kHz low speed oscillator. + * | | |0 = Chip operating normally or chip in Idle mode because of WFI command. + * | | |1 = Chip enters Power-down mode instantly or waits CPU sleep command WFI. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * |[8] |PD_WAIT_CPU|This Bit Control The Power-Down Entry Condition (Write Protect) + * | | |0 = Chip enters Power-down mode when the PWR_DOWN_EN bit is set to 1. + * | | |1 = Chip enters Power-down mode when the both PD_WAIT_CPU and PWR_DOWN_EN bits are set to 1 and CPU run WFI instruction. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * @var CLK_T::AHBCLK + * Offset: 0x04 AHB Devices Clock Enable Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1] |PDMA_EN |PDMA Controller Clock Enable Control + * | | |0 = PDMA peripheral clock Disabled. + * | | |1 = PDMA peripheral clock Enabled. + * |[2] |ISP_EN |Flash ISP Controller Clock Enable Control + * | | |0 = Flash ISP peripheral clock Disabled. + * | | |1 = Flash ISP peripheral clock Enabled. + * @var CLK_T::APBCLK + * Offset: 0x08 APB Devices Clock Enable Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |WDT_EN |Watchdog Timer Clock Enable Control (Write Protect) + * | | |0 = Watchdog Timer clock Disabled. + * | | |1 = Watchdog Timer clock Enabled. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * |[2] |TMR0_EN |Timer0 Clock Enable Control + * | | |0 = Timer0 clock Disabled. + * | | |1 = Timer0 clock Enabled. + * |[3] |TMR1_EN |Timer1 Clock Enable Control + * | | |0 = Timer1 clock Disabled. + * | | |1 = Timer1 clock Enabled. + * |[4] |TMR2_EN |Timer2 Clock Enable Control + * | | |0 = Timer2 clock Disabled. + * | | |1 = Timer2 clock Enabled. + * |[5] |TMR3_EN |Timer3 Clock Enable Control + * | | |0 = Timer3 clock Disabled. + * | | |1 = Timer3 clock Enabled. + * |[6] |FDIV_EN |Frequency Divider Output Clock Enable Control + * | | |0 = FDIV clock Disabled. + * | | |1 = FDIV clock Enabled. + * |[8] |I2C0_EN |I2C0 Clock Enable Control + * | | |0 = I2C0 clock Disabled. + * | | |1 = I2C0 clock Enabled. + * |[9] |I2C1_EN |I2C1 Clock Enable Control + * | | |0 = I2C1 clock Disabled. + * | | |1 = I2C1 clock Enabled. + * |[12] |SPI0_EN |SPI0 Clock Enable Control + * | | |0 = SPI0 clock Disabled. + * | | |1 = SPI0 clock Enabled. + * |[13] |SPI1_EN |SPI1 Clock Enable Control + * | | |0 = SPI1 clock Disabled. + * | | |1 = SPI1 clock Enabled. + * |[14] |SPI2_EN |SPI2 Clock Enable Control + * | | |0 = SPI2 clock Disabled. + * | | |1 = SPI2 clock Enabled. + * |[16] |UART0_EN |UART0 Clock Enable Control + * | | |0 = UART0 clock Disabled. + * | | |1 = UART0 clock Enabled. + * |[17] |UART1_EN |UART1 Clock Enable Control + * | | |0 = UART1 clock Disabled. + * | | |1 = UART1 clock Enabled. + * |[20] |PWM01_EN |PWM_01 Clock Enable Control + * | | |0 = PWM01 clock Disabled. + * | | |1 = PWM01 clock Enabled. + * |[21] |PWM23_EN |PWM_23 Clock Enable Control + * | | |0 = PWM23 clock Disabled. + * | | |1 = PWM23 clock Enabled. + * |[27] |USBD_EN |USB 2.0 FS Device Controller Clock Enable Control + * | | |0 = USB clock Disabled. + * | | |1 = USB clock Enabled. + * |[28] |ADC_EN |Analog-Digital-Converter (ADC) Clock Enable Control + * | | |0 = ADC clock Disabled. + * | | |1 = ADC clock Enabled. + * |[29] |I2S_EN |I2S Clock Enable Control + * | | |0 = I2S clock Disabled. + * | | |1 = I2S clock Enabled. + * |[31] |PS2_EN |PS/2 Clock Enable Control + * | | |0 = PS/2 clock Disabled. + * | | |1 = PS/2 clock Enabled. + * @var CLK_T::CLKSTATUS + * Offset: 0x0C Clock status monitor Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |XTL12M_STB|External 4~24 MHz High Speed Crystal (HXT) Clock Source Stable Flag (Read Only) + * | | |0 = External 4~24 MHz high speed crystal clock (HXT) is not stable or disabled. + * | | |1 = External 4~24 MHz high speed crystal clock (HXT) is stable and enabled. + * |[2] |PLL_STB |Internal PLL Clock Source Stable Flag (Read Only) + * | | |0 = Internal PLL clock is not stable or disabled. + * | | |1 = Internal PLL clock is stable in normal mode. + * |[3] |OSC10K_STB|Internal 10 KHz Low Speed Oscillator (LIRC) Clock Source Stable Flag (Read Only) + * | | |0 = Internal 10 kHz low speed oscillator clock (LIRC) is not stable or disabled. + * | | |1 = Internal 10 kHz low speed oscillator clock (LIRC) is stable and enabled. + * |[4] |OSC22M_STB|Internal 22.1184 MHz High Speed Oscillator (HIRC) Clock Source Stable Flag (Read Only) + * | | |0 = Internal 22.1184 MHz high speed oscillator (HIRC) clock is not stable or disabled. + * | | |1 = Internal 22.1184 MHz high speed oscillator (HIRC) clock is stable and enabled. + * |[7] |CLK_SW_FAIL|Clock Switching Fail Flag + * | | |This bit is updated when software switches system clock source. + * | | |If switch target clock is stable, this bit will be set to 0. + * | | |If switch target clock is not stable, this bit will be set to 1. + * | | |0 = Clock switching success. + * | | |1 = Clock switching failure. + * | | |Note1: On NUC123xxxANx, this bit can be cleared to 0 by software writing "1". + * | | |Note2: On NUC123xxxAEx, this bit is read only. After selected clock source is stable, hardware will switch system clock to selected clock automatically, and CLK_SW_FAIL will be cleared automatically by hardware. + * @var CLK_T::CLKSEL0 + * Offset: 0x10 Clock Source Select Control Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |HCLK_S |HCLK Clock Source Select (Write Protect) + * | | |The 3-bit default value is reloaded from the value of CFOSC (CONFIG0[26:24]) in user configuration register of Flash controller by any reset. + * | | |Therefore the default value is either 000b or 111b. + * | | |000 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |001 = Clock source from PLL/2 clock. + * | | |010 = Clock source from PLL clock. + * | | |011 = Clock source from internal 10 kHz low speed oscillator clock. + * | | |111 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |Note1: Before clock switching, the related clock sources (both pre-select and new-select) must be turn on. + * | | |Note2: These bits are write protected bit. Refer to the REGWRPROT register. + * |[5:3] |STCLK_S |Cortex-M0 SysTick Clock Source Select (Write Protect) + * | | |If SYST_CSR[2] = 1, SysTick clock source is from HCLK. + * | | |If SYST_CSR[2] = 0, SysTick clock source is defined by STCLK_S(CLKSEL0[5:3]). + * | | |000 = Clock source from external 4~24 MHz high speed crystal clock. + * | | |010 = Clock source from external 4~24 MHz high speed crystal clock/2. + * | | |011 = Clock source from HCLK/2. + * | | |111 = Clock source from internal 22.1184 MHz high speed oscillator clock/2. + * | | |Note1: If SysTick clock source is not from HCLK (i.e. SYST_CSR[2] = 0), SysTick clock source must less than or equal to HCLK/2. + * | | |Note2: These bits are write protected bit. Refer to the REGWRPROT register. + * @var CLK_T::CLKSEL1 + * Offset: 0x14 Clock Source Select Control Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1:0] |WDT_S |Watchdog Timer Clock Source Select (Write Protect) + * | | |10 = Clock source from HCLK/2048 clock. + * | | |11 = Clock source from internal 10 kHz low speed oscillator clock. + * | | |Note: These bits are write protected bit. Refer to the REGWRPROT register. + * |[3:2] |ADC_S |ADC Clock Source Select + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |01 = Clock source from PLL clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * |[4] |SPI0_S |SPI0 Clock Source Selection + * | | |0 = Clock source from PLL clock. + * | | |1 = Clock source from HCLK. + * |[5] |SPI1_S |SPI1 Clock Source Selection + * | | |0 = Clock source from PLL clock. + * | | |1 = Clock source from HCLK. + * |[6] |SPI2_S |SPI2 Clock Source Selection + * | | |0 = Clock source from PLL clock. + * | | |1 = Clock source from HCLK. + * |[10:8] |TMR0_S |TIMER0 Clock Source Selection + * | | |000 = Clock source from external 4~24 MHz high speed crystal clock. + * | | |010 = Clock source from HCLK. + * | | |011 = Clock source from external trigger. + * | | |101 = Clock source from internal 10 kHz low speed oscillator clock. + * | | |111 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |Others = Reserved. + * |[14:12] |TMR1_S |TIMER1 Clock Source Selection + * | | |000 = Clock source from external 4~24 MHz high speed crystal clock. + * | | |010 = Clock source from HCLK. + * | | |011 = Clock source from external trigger. + * | | |101 = Clock source from internal 10 kHz low speed oscillator clock. + * | | |111 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |Others = Reserved. + * |[18:16] |TMR2_S |TIMER2 Clock Source Selection + * | | |000 = Clock source from external 4~24 MHz high speed crystal clock. + * | | |010 = Clock source from HCLK. + * | | |011 = Clock source from external trigger. + * | | |101 = Clock source from internal 10 kHz low speed oscillator clock. + * | | |111 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |Others = Reserved. + * |[22:20] |TMR3_S |TIMER3 Clock Source Selection + * | | |000 = Clock source from external 4~24 MHz high speed crystal clock. + * | | |010 = Clock source from HCLK. + * | | |011 = Reserved. + * | | |101 = Clock source from internal 10 kHz low speed oscillator clock. + * | | |111 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |Others = Reserved. + * |[25:24] |UART_S |UART Clock Source Selection + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |01 = Clock source from PLL clock. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * |[29:28] |PWM01_S |PWM0 and PWM1 Clock Source Selection + * | | |PWM0 and PWM1 used the same clock source; both of them used the same prescaler. + * | | |The clock source of PWM0 and PWM1 is defined by PWM01_S (CLKSEL1[29:28]) and PWM01_S_E (CLKSEL2[8]). + * | | |If PWM01_S_E = 0, the clock source of PWM0 and PWM1 defined by PWM01_S list below: + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |If PWM01_S_E = 1, the clock source of PWM0 and PWM1 defined by PWM01_S list below: + * | | |00 = Reserved. + * | | |01 = Reserved. + * | | |10 = Reserved. + * | | |11 = Clock source from internal 10 kHz low speed oscillator clock. + * |[31:30] |PWM23_S |PWM2 and PWM3 Clock Source Selection + * | | |PWM2 and PWM3 used the same clock source; both of them used the same prescaler. + * | | |The clock source of PWM2 and PWM3 is defined by PWM23_S (CLKSEL1[31:30]) and PWM23_S_E (CLKSEL2[9]). + * | | |If PWM23_S_E = 0, theclock source of PWM2 and PWM3 defined by PWM23_S list below: + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |If PWM23_S_E = 1, the clock source of PWM2 and PWM3 defined by PWM23_S list below: + * | | |00 = Reserved. + * | | |01 = Reserved. + * | | |10 = Reserved. + * | | |11 = Clock source from internal 10 kHz low speed oscillator clock. + * @var CLK_T::CLKDIV + * Offset: 0x18 Clock Divider Number Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |HCLK_N |HCLK Clock Divide Number From HCLK Clock Source + * | | |HCLK clock frequency = (HCLK clock source frequency) / (HCLK_N + 1). + * |[7:4] |USB_N |USB Clock Divide Number From PLL Clock + * | | |USB clock frequency = (PLL frequency) / (USB_N + 1). + * |[11:8] |UART_N |UART Clock Divide Number From UART Clock Source + * | | |UART clock frequency = (UART clock source frequency) / (UART_N + 1). + * |[23:16] |ADC_N |ADC Clock Divide Number From ADC Clock Source + * | | |ADC clock frequency = (ADC clock source frequency) / (ADC_N + 1). + * @var CLK_T::CLKSEL2 + * Offset: 0x1C Clock Source Select Control Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1:0] |I2S_S |I2S Clock Source Selection + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |01 = Clock source from PLL clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * |[3:2] |FRQDIV_S |Clock Divider Clock Source Selection + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * |[8] |PWM01_S_E |PWM0 and PWM1 Clock Source Selection Extend + * | | |PWM0 and PWM1 used the same clock source; both of them used the same prescaler. + * | | |The clock source of PWM0 and PWM1 is defined by PWM01_S (CLKSEL1[29:28]) and PWM01_S_E (CLKSEL2[8]). + * | | |If PWM01_S_E = 0, the clock source of PWM0 and PWM1 defined by PWM01_S list below: + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |If PWM01_S_E = 1, the clock source of PWM0 and PWM1 defined by PWM01_S list below: + * | | |00 = Reserved. + * | | |01 = Reserved. + * | | |10 = Reserved. + * | | |11 = Clock source from internal 10 kHz low speed oscillator clock. + * |[9] |PWM23_S_E |PWM2 and PWM3 Clock Source Selection Extend + * | | |PWM2 and PWM3 used the same clock source; both of them used the same prescaler. + * | | |The clock source of PWM2 and PWM3 is defined by PWM23_S (CLKSEL1[31:30]) and PWM23_S_E (CLKSEL2[9]). + * | | |If PWM23_S_E = 0, the clock source of PWM2 and PWM3 defined by PWM23_S list below: + * | | |00 = Clock source from external 4~24 MHz high speed crystal oscillator clock. + * | | |10 = Clock source from HCLK. + * | | |11 = Clock source from internal 22.1184 MHz high speed oscillator clock. + * | | |If PWM23_S_E = 1, the clock source of PWM2 and PWM3 defined by PWM23_S list below: + * | | |00 = Reserved. + * | | |01 = Reserved. + * | | |10 = Reserved. + * | | |11 = Clock source from internal 10 kHz low speed oscillator clock. + * |[17:16] |WWDT_S |Window Watchdog Timer Clock Source Selection + * | | |10 = Clock source from HCLK/2048 clock. + * | | |11 = Clock source from internal 10 kHz low speed oscillator clock. + * @var CLK_T::PLLCON + * Offset: 0x20 PLL Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8:0] |FB_DV |PLL Feedback Divider Control Bits + * | | |Refer to the PLL formulas. + * |[13:9] |IN_DV |PLL Input Divider Control Bits + * | | |Refer to the PLL formulas. + * |[15:14] |OUT_DV |PLL Output Divider Control Bits + * | | |Refer to the PLL formulas. + * |[16] |PD |Power-down Mode + * | | |If the PWR_DOWN_EN bit is set to 1 in PWRCON register, the PLL will enter Power-down mode too. + * | | |0 = PLL is in Normal mode. + * | | |1 = PLL is in Power-down mode (default). + * |[17] |BP |PLL Bypass Control + * | | |0 = PLL is in Normal mode (default). + * | | |1 = PLL clock output is same as PLL source clock input. + * |[18] |OE |PLL OE (FOUT Enable) Control + * | | |0 = PLL FOUT Enabled. + * | | |1 = PLL FOUT is fixed low. + * |[19] |PLL_SRC |PLL Source Clock Selection + * | | |0 = PLL source clock from external 4~24 MHz high speed crystal. + * | | |1 = PLL source clock from internal 22.1184 MHz high speed oscillator. + * @var CLK_T::FRQDIV + * Offset: 0x24 Frequency Divider Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |FSEL |Divider Output Frequency Selection Bits + * | | |The formula of output frequency is Fout = Fin/2(N+1). + * | | |Fin is the input clock frequency. + * | | |Fout is the frequency of divider output clock. + * | | |N is the 4-bit value of FSEL[3:0]. + * |[4] |DIVIDER_EN|Frequency Divider Enable Bit + * | | |0 = Frequency Divider function Disabled. + * | | |1 = Frequency Divider function Enabled. + * @var CLK_T::APBDIV + * Offset: 0x2C APB Divider Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |APBDIV |APB Divider Enable Bit + * | | |0 = PCLK is HCLK. + * | | |1 = PCLK is HCLK/2. + */ + + __IO uint32_t PWRCON; /* Offset: 0x00 System Power-down Control Register */ + __IO uint32_t AHBCLK; /* Offset: 0x04 AHB Devices Clock Enable Control Register */ + __IO uint32_t APBCLK; /* Offset: 0x08 APB Devices Clock Enable Control Register */ + __IO uint32_t CLKSTATUS; /* Offset: 0x0C Clock status monitor Register */ + __IO uint32_t CLKSEL0; /* Offset: 0x10 Clock Source Select Control Register 0 */ + __IO uint32_t CLKSEL1; /* Offset: 0x14 Clock Source Select Control Register 1 */ + __IO uint32_t CLKDIV; /* Offset: 0x18 Clock Divider Number Register */ + __IO uint32_t CLKSEL2; /* Offset: 0x1C Clock Source Select Control Register 2 */ + __IO uint32_t PLLCON; /* Offset: 0x20 PLL Control Register */ + __IO uint32_t FRQDIV; /* Offset: 0x24 Frequency Divider Control Register */ + __I uint32_t RESERVE0; + __IO uint32_t APBDIV; /* Offset: 0x2C APB Divider Control Register */ + +} CLK_T; + + + +/** @addtogroup REG_CLK_BITMASK CLK Bit Mask + @{ + */ + +/* CLK PWRCON Bit Field Definitions */ +#define CLK_PWRCON_PD_WAIT_CPU_Pos 8 /*!< CLK_T::PWRCON: PD_WAIT_CPU Position */ +#define CLK_PWRCON_PD_WAIT_CPU_Msk (1ul << CLK_PWRCON_PD_WAIT_CPU_Pos) /*!< CLK_T::PWRCON: PD_WAIT_CPU Mask */ + +#define CLK_PWRCON_PWR_DOWN_EN_Pos 7 /*!< CLK_T::PWRCON: PWR_DOWN_EN Position */ +#define CLK_PWRCON_PWR_DOWN_EN_Msk (1ul << CLK_PWRCON_PWR_DOWN_EN_Pos) /*!< CLK_T::PWRCON: PWR_DOWN_EN Mask */ + +#define CLK_PWRCON_PD_WU_STS_Pos 6 /*!< CLK_T::PWRCON: PD_WU_STS Position */ +#define CLK_PWRCON_PD_WU_STS_Msk (1ul << CLK_PWRCON_PD_WU_STS_Pos) /*!< CLK_T::PWRCON: PD_WU_STS Mask */ + +#define CLK_PWRCON_PD_WU_INT_EN_Pos 5 /*!< CLK_T::PWRCON: PD_WU_INT_EN Position */ +#define CLK_PWRCON_PD_WU_INT_EN_Msk (1ul << CLK_PWRCON_PD_WU_INT_EN_Pos) /*!< CLK_T::PWRCON: PD_WU_INT_EN Mask */ + +#define CLK_PWRCON_PD_WU_DLY_Pos 4 /*!< CLK_T::PWRCON: PD_WU_DLY Position */ +#define CLK_PWRCON_PD_WU_DLY_Msk (1ul << CLK_PWRCON_PD_WU_DLY_Pos) /*!< CLK_T::PWRCON: PD_WU_DLY Mask */ + +#define CLK_PWRCON_OSC10K_EN_Pos 3 /*!< CLK_T::PWRCON: OSC10K_EN Position */ +#define CLK_PWRCON_OSC10K_EN_Msk (1ul << CLK_PWRCON_OSC10K_EN_Pos) /*!< CLK_T::PWRCON: OSC10K_EN Mask */ +#define CLK_PWRCON_IRC10K_EN_Pos 3 /*!< CLK_T::PWRCON: IRC10K_EN Position */ +#define CLK_PWRCON_IRC10K_EN_Msk (1ul << CLK_PWRCON_IRC10K_EN_Pos) /*!< CLK_T::PWRCON: IRC10K_EN Mask */ + +#define CLK_PWRCON_OSC22M_EN_Pos 2 /*!< CLK_T::PWRCON: OSC22M_EN Position */ +#define CLK_PWRCON_OSC22M_EN_Msk (1ul << CLK_PWRCON_OSC22M_EN_Pos) /*!< CLK_T::PWRCON: OSC22M_EN Mask */ +#define CLK_PWRCON_IRC22M_EN_Pos 2 /*!< CLK_T::PWRCON: IRC22M_EN Position */ +#define CLK_PWRCON_IRC22M_EN_Msk (1ul << CLK_PWRCON_IRC22M_EN_Pos) /*!< CLK_T::PWRCON: IRC22M_EN Mask */ + +#define CLK_PWRCON_XTL12M_EN_Pos 0 /*!< CLK_T::PWRCON: XTL12M_EN Position */ +#define CLK_PWRCON_XTL12M_EN_Msk (1ul << CLK_PWRCON_XTL12M_EN_Pos) /*!< CLK_T::PWRCON: XTL12M_EN Mask */ + +/* CLK AHBCLK Bit Field Definitions */ +#define CLK_AHBCLK_ISP_EN_Pos 2 /*!< CLK_T::AHBCLK: ISP_EN Position */ +#define CLK_AHBCLK_ISP_EN_Msk (1ul << CLK_AHBCLK_ISP_EN_Pos) /*!< CLK_T::AHBCLK: ISP_EN Mask */ + +#define CLK_AHBCLK_PDMA_EN_Pos 1 /*!< CLK_T::AHBCLK: PDMA_EN Position */ +#define CLK_AHBCLK_PDMA_EN_Msk (1ul << CLK_AHBCLK_PDMA_EN_Pos) /*!< CLK_T::AHBCLK: PDMA_EN Mask */ + + +/* CLK APBCLK Bit Field Definitions */ +#define CLK_APBCLK_PS2_EN_Pos 31 /*!< CLK_T::APBCLK: PS2_EN Position */ +#define CLK_APBCLK_PS2_EN_Msk (1ul << CLK_APBCLK_PS2_EN_Pos) /*!< CLK_T::APBCLK: PS2_EN Mask */ + +#define CLK_APBCLK_I2S_EN_Pos 29 /*!< CLK_T::APBCLK: I2S_EN Position */ +#define CLK_APBCLK_I2S_EN_Msk (1ul << CLK_APBCLK_I2S_EN_Pos) /*!< CLK_T::APBCLK: I2S_EN Mask */ + +#define CLK_APBCLK_ADC_EN_Pos 28 /*!< CLK_T::APBCLK: ADC_EN Position */ +#define CLK_APBCLK_ADC_EN_Msk (1ul << CLK_APBCLK_ADC_EN_Pos) /*!< CLK_T::APBCLK: ADC_EN Mask */ + +#define CLK_APBCLK_USBD_EN_Pos 27 /*!< CLK_T::APBCLK: USBD_EN Position */ +#define CLK_APBCLK_USBD_EN_Msk (1ul << CLK_APBCLK_USBD_EN_Pos) /*!< CLK_T::APBCLK: USBD_EN Mask */ + +#define CLK_APBCLK_PWM23_EN_Pos 21 /*!< CLK_T::APBCLK: PWM23_EN Position */ +#define CLK_APBCLK_PWM23_EN_Msk (1ul << CLK_APBCLK_PWM23_EN_Pos) /*!< CLK_T::APBCLK: PWM23_EN Mask */ + +#define CLK_APBCLK_PWM01_EN_Pos 20 /*!< CLK_T::APBCLK: PWM01_EN Position */ +#define CLK_APBCLK_PWM01_EN_Msk (1ul << CLK_APBCLK_PWM01_EN_Pos) /*!< CLK_T::APBCLK: PWM01_EN Mask */ + +#define CLK_APBCLK_UART1_EN_Pos 17 /*!< CLK_T::APBCLK: UART1_EN Position */ +#define CLK_APBCLK_UART1_EN_Msk (1ul << CLK_APBCLK_UART1_EN_Pos) /*!< CLK_T::APBCLK: UART1_EN Mask */ + +#define CLK_APBCLK_UART0_EN_Pos 16 /*!< CLK_T::APBCLK: UART0_EN Position */ +#define CLK_APBCLK_UART0_EN_Msk (1ul << CLK_APBCLK_UART0_EN_Pos) /*!< CLK_T::APBCLK: UART0_EN Mask */ + +#define CLK_APBCLK_SPI2_EN_Pos 14 /*!< CLK_T::APBCLK: SPI2_EN Position */ +#define CLK_APBCLK_SPI2_EN_Msk (1ul << CLK_APBCLK_SPI2_EN_Pos) /*!< CLK_T::APBCLK: SPI2_EN Mask */ + +#define CLK_APBCLK_SPI1_EN_Pos 13 /*!< CLK_T::APBCLK: SPI1_EN Position */ +#define CLK_APBCLK_SPI1_EN_Msk (1ul << CLK_APBCLK_SPI1_EN_Pos) /*!< CLK_T::APBCLK: SPI1_EN Mask */ + +#define CLK_APBCLK_SPI0_EN_Pos 12 /*!< CLK_T::APBCLK: SPI0_EN Position */ +#define CLK_APBCLK_SPI0_EN_Msk (1ul << CLK_APBCLK_SPI0_EN_Pos) /*!< CLK_T::APBCLK: SPI0_EN Mask */ + +#define CLK_APBCLK_I2C1_EN_Pos 9 /*!< CLK_T::APBCLK: I2C1_EN Position */ +#define CLK_APBCLK_I2C1_EN_Msk (1ul << CLK_APBCLK_I2C1_EN_Pos) /*!< CLK_T::APBCLK: I2C1_EN Mask */ + +#define CLK_APBCLK_I2C0_EN_Pos 8 /*!< CLK_T::APBCLK: I2C0_EN_ Position */ +#define CLK_APBCLK_I2C0_EN_Msk (1ul << CLK_APBCLK_I2C0_EN_Pos) /*!< CLK_T::APBCLK: I2C0_EN_ Mask */ + +#define CLK_APBCLK_FDIV_EN_Pos 6 /*!< CLK_T::APBCLK: FDIV_EN Position */ +#define CLK_APBCLK_FDIV_EN_Msk (1ul << CLK_APBCLK_FDIV_EN_Pos) /*!< CLK_T::APBCLK: FDIV_EN Mask */ + +#define CLK_APBCLK_TMR3_EN_Pos 5 /*!< CLK_T::APBCLK: TMR3_EN Position */ +#define CLK_APBCLK_TMR3_EN_Msk (1ul << CLK_APBCLK_TMR3_EN_Pos) /*!< CLK_T::APBCLK: TMR3_EN Mask */ + +#define CLK_APBCLK_TMR2_EN_Pos 4 /*!< CLK_T::APBCLK: TMR2_EN Position */ +#define CLK_APBCLK_TMR2_EN_Msk (1ul << CLK_APBCLK_TMR2_EN_Pos) /*!< CLK_T::APBCLK: TMR2_EN Mask */ + +#define CLK_APBCLK_TMR1_EN_Pos 3 /*!< CLK_T::APBCLK: TMR1_EN Position */ +#define CLK_APBCLK_TMR1_EN_Msk (1ul << CLK_APBCLK_TMR1_EN_Pos) /*!< CLK_T::APBCLK: TMR1_EN Mask */ + +#define CLK_APBCLK_TMR0_EN_Pos 2 /*!< CLK_T::APBCLK: TMR0_EN Position */ +#define CLK_APBCLK_TMR0_EN_Msk (1ul << CLK_APBCLK_TMR0_EN_Pos) /*!< CLK_T::APBCLK: TMR0_EN Mask */ + +#define CLK_APBCLK_WDT_EN_Pos 0 /*!< CLK_T::APBCLK: WDT_EN Position */ +#define CLK_APBCLK_WDT_EN_Msk (1ul << CLK_APBCLK_WDT_EN_Pos) /*!< CLK_T::APBCLK: WDT_EN Mask */ + + +/* CLK CLKSTATUS Bit Field Definitions */ +#define CLK_CLKSTATUS_CLK_SW_FAIL_Pos 7 /*!< CLK_T::CLKSTATUS: CLK_SW_FAIL Position */ +#define CLK_CLKSTATUS_CLK_SW_FAIL_Msk (1ul << CLK_CLKSTATUS_CLK_SW_FAIL_Pos) /*!< CLK_T::CLKSTATUS: CLK_SW_FAIL Mask */ + +#define CLK_CLKSTATUS_OSC22M_STB_Pos 4 /*!< CLK_T::CLKSTATUS: OSC22M_STB Position */ +#define CLK_CLKSTATUS_OSC22M_STB_Msk (1ul << CLK_CLKSTATUS_OSC22M_STB_Pos) /*!< CLK_T::CLKSTATUS: OSC22M_STB Mask */ +#define CLK_CLKSTATUS_IRC22M_STB_Pos 4 /*!< CLK_T::CLKSTATUS: IRC22M_STB Position */ +#define CLK_CLKSTATUS_IRC22M_STB_Msk (1ul << CLK_CLKSTATUS_IRC22M_STB_Pos) /*!< CLK_T::CLKSTATUS: IRC22M_STB Mask */ + +#define CLK_CLKSTATUS_OSC10K_STB_Pos 3 /*!< CLK_T::CLKSTATUS: OSC10K_STB Position */ +#define CLK_CLKSTATUS_OSC10K_STB_Msk (1ul << CLK_CLKSTATUS_OSC10K_STB_Pos) /*!< CLK_T::CLKSTATUS: OSC10K_STB Mask */ +#define CLK_CLKSTATUS_IRC10K_STB_Pos 3 /*!< CLK_T::CLKSTATUS: IRC10K_STB Position */ +#define CLK_CLKSTATUS_IRC10K_STB_Msk (1ul << CLK_CLKSTATUS_IRC10K_STB_Pos) /*!< CLK_T::CLKSTATUS: IRC10K_STB Mask */ + +#define CLK_CLKSTATUS_PLL_STB_Pos 2 /*!< CLK_T::CLKSTATUS: PLL_STB Position */ +#define CLK_CLKSTATUS_PLL_STB_Msk (1ul << CLK_CLKSTATUS_PLL_STB_Pos) /*!< CLK_T::CLKSTATUS: PLL_STB Mask */ + +#define CLK_CLKSTATUS_XTL12M_STB_Pos 0 /*!< CLK_T::CLKSTATUS: XTL12M_STB Position */ +#define CLK_CLKSTATUS_XTL12M_STB_Msk (1ul << CLK_CLKSTATUS_XTL12M_STB_Pos) /*!< CLK_T::CLKSTATUS: XTL12M_STB Mask */ + +/* CLK CLKSEL0 Bit Field Definitions */ +#define CLK_CLKSEL0_STCLK_S_Pos 3 /*!< CLK_T::CLKSEL0: STCLK_S Position */ +#define CLK_CLKSEL0_STCLK_S_Msk (7ul << CLK_CLKSEL0_STCLK_S_Pos) /*!< CLK_T::CLKSEL0: STCLK_S Mask */ + +#define CLK_CLKSEL0_HCLK_S_Pos 0 /*!< CLK_T::CLKSEL0: HCLK_S Position */ +#define CLK_CLKSEL0_HCLK_S_Msk (7ul << CLK_CLKSEL0_HCLK_S_Pos) /*!< CLK_T::CLKSEL0: HCLK_S Mask */ + +/* CLK CLKSEL1 Bit Field Definitions */ +#define CLK_CLKSEL1_PWM23_S_Pos 30 /*!< CLK_T::CLKSEL1: PWM23_S Position */ +#define CLK_CLKSEL1_PWM23_S_Msk (3ul << CLK_CLKSEL1_PWM23_S_Pos) /*!< CLK_T::CLKSEL1: PWM23_S Mask */ + +#define CLK_CLKSEL1_PWM01_S_Pos 28 /*!< CLK_T::CLKSEL1: PWM01_S Position */ +#define CLK_CLKSEL1_PWM01_S_Msk (3ul << CLK_CLKSEL1_PWM01_S_Pos) /*!< CLK_T::CLKSEL1: PWM01_S Mask */ + +#define CLK_CLKSEL1_UART_S_Pos 24 /*!< CLK_T::CLKSEL1: UART_S Position */ +#define CLK_CLKSEL1_UART_S_Msk (3ul << CLK_CLKSEL1_UART_S_Pos) /*!< CLK_T::CLKSEL1: UART_S Mask */ + +#define CLK_CLKSEL1_TMR3_S_Pos 20 /*!< CLK_T::CLKSEL1: TMR3_S Position */ +#define CLK_CLKSEL1_TMR3_S_Msk (7ul << CLK_CLKSEL1_TMR3_S_Pos) /*!< CLK_T::CLKSEL1: TMR3_S Mask */ + +#define CLK_CLKSEL1_TMR2_S_Pos 16 /*!< CLK_T::CLKSEL1: TMR2_S Position */ +#define CLK_CLKSEL1_TMR2_S_Msk (7ul << CLK_CLKSEL1_TMR2_S_Pos) /*!< CLK_T::CLKSEL1: TMR2_S Mask */ + +#define CLK_CLKSEL1_TMR1_S_Pos 12 /*!< CLK_T::CLKSEL1: TMR1_S Position */ +#define CLK_CLKSEL1_TMR1_S_Msk (7ul << CLK_CLKSEL1_TMR1_S_Pos) /*!< CLK_T::CLKSEL1: TMR1_S Mask */ + +#define CLK_CLKSEL1_TMR0_S_Pos 8 /*!< CLK_T::CLKSEL1: TMR0_S Position */ +#define CLK_CLKSEL1_TMR0_S_Msk (7ul << CLK_CLKSEL1_TMR0_S_Pos) /*!< CLK_T::CLKSEL1: TMR0_S Mask */ + +#define CLK_CLKSEL1_SPI2_S_Pos 6 /*!< CLK_T::CLKSEL1: SPI2_S Position */ +#define CLK_CLKSEL1_SPI2_S_Msk (1ul << CLK_CLKSEL1_SPI2_S_Pos) /*!< CLK_T::CLKSEL1: SPI2_S Mask */ + +#define CLK_CLKSEL1_SPI1_S_Pos 5 /*!< CLK_T::CLKSEL1: SPI1_S Position */ +#define CLK_CLKSEL1_SPI1_S_Msk (1ul << CLK_CLKSEL1_SPI1_S_Pos) /*!< CLK_T::CLKSEL1: SPI1_S Mask */ + +#define CLK_CLKSEL1_SPI0_S_Pos 4 /*!< CLK_T::CLKSEL1: SPI0_S Position */ +#define CLK_CLKSEL1_SPI0_S_Msk (1ul << CLK_CLKSEL1_SPI0_S_Pos) /*!< CLK_T::CLKSEL1: SPI0_S Mask */ + +#define CLK_CLKSEL1_ADC_S_Pos 2 /*!< CLK_T::CLKSEL1: ADC_S Position */ +#define CLK_CLKSEL1_ADC_S_Msk (3ul << CLK_CLKSEL1_ADC_S_Pos) /*!< CLK_T::CLKSEL1: ADC_S Mask */ + +#define CLK_CLKSEL1_WDT_S_Pos 0 /*!< CLK_T::CLKSEL1: WDT_S Position */ +#define CLK_CLKSEL1_WDT_S_Msk (3ul << CLK_CLKSEL1_WDT_S_Pos) /*!< CLK_T::CLKSEL1: WDT_S Mask */ + +/* CLK CLKSEL2 Bit Field Definitions */ +#define CLK_CLKSEL2_WWDT_S_Pos 16 /*!< CLK_T::CLKSEL2: WWDT_S Position */ +#define CLK_CLKSEL2_WWDT_S_Msk (3ul << CLK_CLKSEL2_WWDT_S_Pos) /*!< CLK_T::CLKSEL2: WWDT_S Mask */ + +#define CLK_CLKSEL2_PWM23_S_E_Pos 9 /*!< CLK_T::CLKSEL2: PWM23_S_E Position */ +#define CLK_CLKSEL2_PWM23_S_E_Msk (1ul << CLK_CLKSEL2_PWM23_S_E_Pos) /*!< CLK_T::CLKSEL2: PWM23_S_E Mask */ +#define CLK_CLKSEL2_PWM23_S_EXT_Pos 9 /*!< CLK_T::CLKSEL2: PWM23_S_EXT Position */ +#define CLK_CLKSEL2_PWM23_S_EXT_Msk (1ul << CLK_CLKSEL2_PWM23_S_EXT_Pos) /*!< CLK_T::CLKSEL2: PWM23_S_EXT Mask */ + +#define CLK_CLKSEL2_PWM01_S_E_Pos 8 /*!< CLK_T::CLKSEL2: PWM01_S_E Position */ +#define CLK_CLKSEL2_PWM01_S_E_Msk (1ul << CLK_CLKSEL2_PWM01_S_E_Pos) /*!< CLK_T::CLKSEL2: PWM01_S_E Mask */ +#define CLK_CLKSEL2_PWM01_S_EXT_Pos 8 /*!< CLK_T::CLKSEL2: PWM01_S_EXT Position */ +#define CLK_CLKSEL2_PWM01_S_EXT_Msk (1ul << CLK_CLKSEL2_PWM01_S_EXT_Pos) /*!< CLK_T::CLKSEL2: PWM01_S_EXT Mask */ + +#define CLK_CLKSEL2_FRQDIV_S_Pos 2 /*!< CLK_T::CLKSEL2: FRQDIV_S Position */ +#define CLK_CLKSEL2_FRQDIV_S_Msk (3ul << CLK_CLKSEL2_FRQDIV_S_Pos) /*!< CLK_T::CLKSEL2: FRQDIV_S Mask */ + +#define CLK_CLKSEL2_I2S_S_Pos 0 /*!< CLK_T::CLKSEL2: I2S_S Position */ +#define CLK_CLKSEL2_I2S_S_Msk (3ul << CLK_CLKSEL2_I2S_S_Pos) /*!< CLK_T::CLKSEL2: I2S_S Mask */ + +/* CLK CLKDIV Bit Field Definitions */ +#define CLK_CLKDIV_ADC_N_Pos 16 /*!< CLK_T::CLKDIV: ADC_N Position */ +#define CLK_CLKDIV_ADC_N_Msk (0xFFul << CLK_CLKDIV_ADC_N_Pos) /*!< CLK_T::CLKDIV: ADC_N Mask */ + +#define CLK_CLKDIV_UART_N_Pos 8 /*!< CLK_T::CLKDIV: UART_N Position */ +#define CLK_CLKDIV_UART_N_Msk (0xFul << CLK_CLKDIV_UART_N_Pos) /*!< CLK_T::CLKDIV: UART_N Mask */ + +#define CLK_CLKDIV_USB_N_Pos 4 /*!< CLK_T::CLKDIV: USB_N Position */ +#define CLK_CLKDIV_USB_N_Msk (0xFul << CLK_CLKDIV_USB_N_Pos) /*!< CLK_T::CLKDIV: USB_N Mask */ + +#define CLK_CLKDIV_HCLK_N_Pos 0 /*!< CLK_T::CLKDIV: HCLK_N Position */ +#define CLK_CLKDIV_HCLK_N_Msk (0xFul << CLK_CLKDIV_HCLK_N_Pos) /*!< CLK_T::CLKDIV: HCLK_N Mask */ + +/* CLK PLLCON Bit Field Definitions */ +#define CLK_PLLCON_PLL_SRC_Pos 19 /*!< CLK_T::PLLCON: PLL_SRC Position */ +#define CLK_PLLCON_PLL_SRC_Msk (1ul << CLK_PLLCON_PLL_SRC_Pos) /*!< CLK_T::PLLCON: PLL_SRC Mask */ + +#define CLK_PLLCON_OE_Pos 18 /*!< CLK_T::PLLCON: PLL_SRC Position */ +#define CLK_PLLCON_OE_Msk (1ul << CLK_PLLCON_OE_Pos) /*!< CLK_T::PLLCON: PLL_SRC Mask */ + +#define CLK_PLLCON_BP_Pos 17 /*!< CLK_T::PLLCON: OE Position */ +#define CLK_PLLCON_BP_Msk (1ul << CLK_PLLCON_BP_Pos) /*!< CLK_T::PLLCON: OE Mask */ + +#define CLK_PLLCON_PD_Pos 16 /*!< CLK_T::PLLCON: PD Position */ +#define CLK_PLLCON_PD_Msk (1ul << CLK_PLLCON_PD_Pos) /*!< CLK_T::PLLCON: PD Mask */ + +#define CLK_PLLCON_OUT_DV_Pos 14 /*!< CLK_T::PLLCON: OUT_DV Position */ +#define CLK_PLLCON_OUT_DV_Msk (3ul << CLK_PLLCON_OUT_DV_Pos) /*!< CLK_T::PLLCON: OUT_DV Mask */ + +#define CLK_PLLCON_IN_DV_Pos 9 /*!< CLK_T::PLLCON: IN_DV Position */ +#define CLK_PLLCON_IN_DV_Msk (0x1Ful << CLK_PLLCON_IN_DV_Pos) /*!< CLK_T::PLLCON: IN_DV Mask */ + +#define CLK_PLLCON_FB_DV_Pos 0 /*!< CLK_T::PLLCON: FB_DV Position */ +#define CLK_PLLCON_FB_DV_Msk (0x1FFul << CLK_PLLCON_FB_DV_Pos) /*!< CLK_T::PLLCON: FB_DV Mask */ + +/* CLK FRQDIV Bit Field Definitions */ +#define CLK_FRQDIV_DIVIDER_EN_Pos 4 /*!< CLK_T::FRQDIV: DIVIDER_EN Position */ +#define CLK_FRQDIV_DIVIDER_EN_Msk (1ul << CLK_FRQDIV_DIVIDER_EN_Pos) /*!< CLK_T::FRQDIV: DIVIDER_EN Mask */ + +#define CLK_FRQDIV_FSEL_Pos 0 /*!< CLK_T::FRQDIV: FRQDIV_FSEL Position */ +#define CLK_FRQDIV_FSEL_Msk (0xFul << CLK_FRQDIV_FSEL_Pos) /*!< CLK_T::FRQDIV: FRQDIV_FSEL Mask */ + +/* CLK APBDIV Bit Field Definitions */ +#define CLK_APBDIV_APBDIV_Pos 0 /*!< CLK_T::APBDIV: APBDIV Position */ +#define CLK_APBDIV_APBDIV_Msk (1ul << CLK_APBDIV_APBDIV_Pos) /*!< CLK_T::APBDIV: APBDIV Mask */ +/*@}*/ /* end of group REG_CLK_BITMASK */ +/*@}*/ /* end of group REG_CLK */ + + +/*----------------------------- Cyclic Redundancy Check (CRC) Controller -----------------------------*/ +/** @addtogroup REG_CRC Cyclic Redundancy Check Controller (CRC) + Memory Mapped Structure for Cyclic Redundancy Check + @{ + */ + +typedef struct +{ + + +/** + * @var CRC_T::CTL + * Offset: 0x00 CRC Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CRCCEN |CRC Channel Enable + * | | |0 = No effect. + * | | |1 = CRC operation Enabled. + * | | |Note1: When operating in CRC DMA mode (TRIG_EN (CRC_CTL[23]) = 1), if user clears this bit, the DMA operation will be continuous until all CRC DMA operation is done, and the TRIG_EN (CRC_CTL[23]) bit will keep 1until all CRC DMA operation done. + * | | |But in this case, the CRC_BLKD_IF (CRC_DMAISR[1])flag will inactive, user can read CRC checksum result only if TRIG_EN (CRC_CTL[23]) clears to 0. + * | | |Note2: When operating in CRC DMA mode (TRIG_EN (CRC_CTL[23]) = 1), if user wants to stop the transfer immediately, user can write 1 to CRC_RST (CRC_CTL [1]) bit to stop the transmission. + * |[1] |CRC_RST |CRC Engine Reset + * | | |0 = No effect. + * | | |1 = Reset the internal CRC state machine and internal buffer. + * | | |The others contents of CRC_CTL register will not be cleared. + * | | |This bit will be cleared automatically. + * | | |Note: When operated in CPU PIO mode, setting this bit will reload the initial seed value (CRC_SEED register). + * |[23] |TRIG_EN |Trigger Enable + * | | |This bit is used to trigger the CRC DMA transfer. + * | | |0 = No effect. + * | | |1 = CRC DMA data read or write transfer Enabled. + * | | |Note1: If this bit asserts which indicates the CRC engine operation in CRC DMA mode, do not fill in any data in CRC_WDATA register. + * | | |Note2: When CRC DMA transfer completed, this bit will be cleared automatically. + * | | |Note3: If the bus error occurs when CRC DMA transfer data, all CRC DMA transfer will be stopped. + * | | |Software must reset all DMA channel before trigger DMA again. + * |[24] |WDATA_RVS |Write Data Order Reverse + * | | |This bit is used to enable the bit order reverse function for write data value in CRC_WDATA register. + * | | |0 = Bit order reverse for CRC write data in Disabled. + * | | |1 = Bit order reverse for CRC write data in Enabled (per byre). + * | | |Note: If the write data is 0xAABBCCDD, the bit order reverse for CRC write data in is 0x55DD33BB + * |[25] |CHECKSUM_RVS|Checksum Reverse + * | | |This bit is used to enable the bit order reverse function for write data value in CRC_CHECKSUM register. + * | | |0 = Bit order reverse for CRC checksum Disabled. + * | | |1 = Bit order reverse for CRC checksum Enabled. + * | | |Note: If the checksum result is 0XDD7B0F2E, the bit order reverse for CRC checksum is 0x74F0DEBB + * |[26] |WDATA_COM |Write Data 1's Complement + * | | |This bit is used to enable the 1's complement function for write data value in CRC_WDATA register. + * | | |0 = 1's complement for CRC write data in Disabled. + * | | |1 = 1's complement for CRC write data in Enabled. + * |[27] |CHECKSUM_COM|Checksum 1's Complement + * | | |This bit is used to enable the 1's complement function for checksum result in CRC_CHECKSUM register. + * | | |0 = 1's complement for CRC checksum Disabled. + * | | |1 = 1's complement for CRC checksum Enabled. + * |[29:28] |CPU_WDLEN |CPU Write Data Length + * | | |This field indicates the CPU write data length only when operating in CPU PIO mode. + * | | |00 = The write data length is 8-bit mode. + * | | |01 = The write data length is 16-bit mode. + * | | |10 = The write data length is 32-bit mode. + * | | |11 = Reserved. + * | | |Note1: This field is only valid when operating in CPU PIO mode. + * | | |Note2: When the write data length is 8-bit mode, the valid data in CRC_WDATA register is only CRC_WDATA [7:0] bits; if the write data length is 16-bit mode, the valid data in CRC_WDATA register is only CRC_WDATA [15:0]. + * |[31:30] |CRC_MODE |CRC Polynomial Mode + * | | |This field indicates the CRC operation polynomial mode. + * | | |00 = CRC-CCITT Polynomial Mode. + * | | |01 = CRC-8 Polynomial Mode. + * | | |10 = CRC-16 Polynomial Mode. + * | | |11 = CRC-32 Polynomial Mode. + * @var CRC_T::DMASAR + * Offset: 0x04 CRC DMA Source Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |CRC_DMASAR|CRC DMA Transfer Source Address Register + * | | |This field indicates a 32-bit source address of CRC DMA. + * | | |(CRC_DMASAR + CRC_DMABCR) = (CRC_DMACSAR + CRC_DMACBCR). + * | | |Note: The source address must be word alignment + * @var CRC_T::DMABCR + * Offset: 0x0C CRC DMA Transfer Byte Count Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CRC_DMABCR|CRC DMA Transfer Byte Count Register + * | | |This field indicates a 16-bit total transfer byte count number of CRC DMA + * | | |(CRC_DMASAR + CRC_DMABCR) = (CRC_DMACSAR + CRC_DMACBCR). + * @var CRC_T::DMACSAR + * Offset: 0x14 CRC DMA Current Source Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |CRC_DMACSAR|CRC DMA Current Source Address Register (Read Only) + * | | |This field indicates the current source address where the CRC DMA transfer just occurs. + * | | |(CRC_DMASAR + CRC_DMABCR) = (CRC_DMACSAR + CRC_DMACBCR). + * @var CRC_T::DMACBCR + * Offset: 0x1C CRC DMA Current Transfer Byte Count Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CRC_DMACBCR|CRC DMA Current Remained Byte Count Register (Read Only) + * | | |This field indicates the current remained byte count of CRC DMA. + * | | |(CRC_DMASAR + CRC_DMABCR) = (CRC_DMACSAR + CRC_DMACBCR). + * | | |Note: Setting CRC_RST (CRC_CTL[1]) bit to 1 will clear this register value. + * @var CRC_T::DMAIER + * Offset: 0x20 CRC DMA Interrupt Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CRC_TABORT_IE|CRC DMA Read/Write Target Abort Interrupt Enable + * | | |Enable this bit will generate the CRC DMA Target Abort interrupt signal while CRC_TARBOT_IF (CRC_DMAISR[0]) bit is set to 1. + * | | |0 = Target abort interrupt generation Disabled during CRC DMA transfer. + * | | |1 = Target abort interrupt generation Enabled during CRC DMA transfer. + * |[1] |CRC_BLKD_IE|CRC DMA Block Transfer Done Interrupt Enable + * | | |Enable this bit will generate the CRC DMA Transfer Done interrupt signal while CRC_BLKD_IF (CRC_DMAISR[1]) bit is set to 1. + * | | |0 = Interrupt generator Disabled when CRC DMA transfer done. + * | | |1 = Interrupt generator Enabled when CRC DMA transfer done. + * @var CRC_T::DMAISR + * Offset: 0x24 CRC DMA Interrupt Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CRC_TABORT_IF|CRC DMA Read/Write Target Abort Interrupt Flag + * | | |This bit indicates that CRC bus has error or not during CRC DMA transfer. + * | | |0 = No bus error response received during CRC DMA transfer. + * | | |1 = Bus error response received during CRC DMA transfer. + * | | |It is cleared by writing 1 to it through software. + * | | |Note: The bit filed indicate bus master received error response or not. + * | | |If bus master received error response, it means that CRC transfer target abort is happened. + * | | |DMA will stop transfer and respond this event to software then CRC state machine goes to IDLE state. + * | | |When target abort occurred, software must reset DMA before transfer those data again. + * |[1] |CRC_BLKD_IF|CRC DMA Block Transfer Done Interrupt Flag + * | | |This bit indicates that CRC DMA transfer has finished or not. + * | | |0 = Not finished if TRIG_EN (CRC_CTL[23]) bit has enabled. + * | | |1 = CRC transfer done if TRIG_EN (CRC_CTL[23]) bit has enabled. + * | | |It is cleared by writing 1 to it through software. + * | | |(When CRC DMA transfer done, TRIG_EN (CRC_CTL[23]) bit will be cleared automatically) + * @var CRC_T::WDATA + * Offset: 0x80 CRC Write Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |CRC_WDATA |CRC Write Data Register + * | | |When operating in CPU PIO mode, software can write data to this field to perform CRC operation. + * | | |When operating in DMA mode, this field indicates the DMA read data from memory and cannot be written. + * | | |Note: When the write data length is 8-bit mode, the valid data in CRC_WDATA register is only CRC_WDATA [7:0] bits; if the write data length is 16-bit mode, the valid data in CRC_WDATA register is only CRC_WDATA [15:0]. + * @var CRC_T::SEED + * Offset: 0x84 CRC Seed Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |CRC_SEED |CRC Seed Register + * | | |This field indicates the CRC seed value. + * @var CRC_T::CHECKSUM + * Offset: 0x88 CRC Checksum Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |CRC_CHECKSUM|CRC Checksum Register + * | | |This fields indicates the CRC checksum result + */ + + __IO uint32_t CTL; /* Offset: 0x00 CRC Control Register */ + __IO uint32_t DMASAR; /* Offset: 0x04 CRC DMA Source Address Register */ + __I uint32_t RESERVED0; + __IO uint32_t DMABCR; /* Offset: 0x0C CRC DMA Transfer Byte Count Register */ + __I uint32_t RESERVED1; + __I uint32_t DMACSAR; /* Offset: 0x14 CRC DMA Current Source Address Register */ + __I uint32_t RESERVED2; + __I uint32_t DMACBCR; /* Offset: 0x1C CRC DMA Current Transfer Byte Count Register */ + __IO uint32_t DMAIER; /* Offset: 0x20 CRC DMA Interrupt Enable Register */ + __IO uint32_t DMAISR; /* Offset: 0x24 CRC DMA Interrupt Status Register */ + __I uint32_t RESERVED3[22]; + __IO uint32_t WDATA; /* Offset: 0x80 CRC Write Data Register */ + __IO uint32_t SEED; /* Offset: 0x84 CRC Seed Register */ + __I uint32_t CHECKSUM; /* Offset: 0x88 CRC Checksum Register */ + +} CRC_T; + + + +/** @addtogroup REG_CRC_BITMASK CRC Bit Mask + @{ + */ + +/* CRC CTL Bit Field Definitions */ +#define CRC_CTL_CRC_MODE_Pos 30 /*!< CRC_T::CTL: CRC_MODE Position */ +#define CRC_CTL_CRC_MODE_Msk (0x3ul << CRC_CTL_CRC_MODE_Pos) /*!< CRC_T::CTL: CRC_MODE Mask */ + +#define CRC_CTL_CPU_WDLEN_Pos 28 /*!< CRC_T::CTL: CPU_WDLEN Position */ +#define CRC_CTL_CPU_WDLEN_Msk (0x3ul << CRC_CTL_CPU_WDLEN_Pos) /*!< CRC_T::CTL: CPU_WDLEN Mask */ + +#define CRC_CTL_CHECKSUM_COM_Pos 27 /*!< CRC_T::CTL: CHECKSUM_COM Position */ +#define CRC_CTL_CHECKSUM_COM_Msk (1ul << CRC_CTL_CHECKSUM_COM_Pos) /*!< CRC_T::CTL: CHECKSUM_COM Mask */ + +#define CRC_CTL_WDATA_COM_Pos 26 /*!< CRC_T::CTL: WDATA_COM Position */ +#define CRC_CTL_WDATA_COM_Msk (1ul << CRC_CTL_WDATA_COM_Pos) /*!< CRC_T::CTL: WDATA_COM Mask */ + +#define CRC_CTL_CHECKSUM_RVS_Pos 25 /*!< CRC_T::CTL: CHECKSUM_RVS Position */ +#define CRC_CTL_CHECKSUM_RVS_Msk (1ul << CRC_CTL_CHECKSUM_RVS_Pos) /*!< CRC_T::CTL: CHECKSUM_RVS Mask */ + +#define CRC_CTL_WDATA_RVS_Pos 24 /*!< CRC_T::CTL: WDATA_RVS Position */ +#define CRC_CTL_WDATA_RVS_Msk (1ul << CRC_CTL_WDATA_RVS_Pos) /*!< CRC_T::CTL: WDATA_RVS Mask */ + +#define CRC_CTL_TRIG_EN_Pos 23 /*!< CRC_T::CTL: TRIG_EN Position */ +#define CRC_CTL_TRIG_EN_Msk (1ul << CRC_CTL_TRIG_EN_Pos) /*!< CRC_T::CTL: TRIG_EN Mask */ + +#define CRC_CTL_CRC_RST_Pos 1 /*!< CRC_T::CTL: CRC_RST Position */ +#define CRC_CTL_CRC_RST_Msk (1ul << CRC_CTL_CRC_RST_Pos) /*!< CRC_T::CTL: CRC_RST Mask */ + +#define CRC_CTL_CRCCEN_Pos 0 /*!< CRC_T::CTL: CRCCEN Position */ +#define CRC_CTL_CRCCEN_Msk (1ul << CRC_CTL_CRCCEN_Pos) /*!< CRC_T::CTL: CRCCEN Mask */ + +/* CRC DMASAR Bit Field Definitions */ +#define CRC_DMASAR_CRC_DMASAR_Pos 0 /*!< CRC_T::DMASAR: CRC_DMASAR Position */ +#define CRC_DMASAR_CRC_DMASAR_Msk (0xFFFFFFFFul << CRC_DMASAR_CRC_DMASAR_Pos) /*!< CRC_T::DMASAR: CRC_DMASAR Mask */ + +/* CRC DMABCR Bit Field Definitions */ +#define CRC_DMABCR_CRC_DMABCR_Pos 0 /*!< CRC_T::DMABCR: CRC_DMABCR Position */ +#define CRC_DMABCR_CRC_DMABCR_Msk (0xFFFFul << CRC_DMABCR_CRC_DMABCR_Pos) /*!< CRC_T::DMABCR: CRC_DMABCR Mask */ + +/* CRC DMACSAR Bit Field Definitions */ +#define CRC_DMACSAR_CRC_DMACSAR_Pos 0 /*!< CRC_T::DMACSAR: CRC_DMACSAR Position */ +#define CRC_DMACSAR_CRC_DMACSAR_Msk (0xFFFFFFFFul << CRC_DMACSAR_CRC_DMACSAR_Pos) /*!< CRC_T::DMACSAR: CRC_DMACSAR Mask */ + +/* CRC DMACBCR Bit Field Definitions */ +#define CRC_DMACBCR_CRC_DMACBCR_Pos 0 /*!< CRC_T::DMACBCR: DMACBCR Position */ +#define CRC_DMACBCR_CRC_DMACBCR_Msk (0xFFFFul << CRC_DMACBCR_CRC_DMACBCR_Pos) /*!< CRC_T::DMACBCR: DMACBCR Mask */ + +/* CRC DMAIER Bit Field Definitions */ +#define CRC_DMAIER_CRC_BLKD_IE_Pos 1 /*!< CRC_T::DMAIER: CRC_BLKD_IE Position */ +#define CRC_DMAIER_CRC_BLKD_IE_Msk (1ul << CRC_DMAIER_CRC_BLKD_IE_Pos) /*!< CRC_T::DMAIER: CRC_BLKD_IE Mask */ + +#define CRC_DMAIER_CRC_TABORT_IE_Pos 0 /*!< CRC_T::DMAIER: CRC_TABORT_IE Position */ +#define CRC_DMAIER_CRC_TABORT_IE_Msk (1ul << CRC_DMAIER_CRC_TABORT_IE_Pos) /*!< CRC_T::DMAIER: CRC_TABORT_IE Mask */ + +/* CRC DMAISR Bit Field Definitions */ +#define CRC_DMAISR_CRC_BLKD_IF_Pos 1 /*!< CRC_T::DMAISR: CRC_BLKD_IF Position */ +#define CRC_DMAISR_CRC_BLKD_IF_Msk (1ul << CRC_DMAISR_CRC_BLKD_IF_Pos) /*!< CRC_T::DMAISR: CRC_BLKD_IF Mask */ + +#define CRC_DMAISR_CRC_TABORT_IF_Pos 0 /*!< CRC_T::DMAISR: CRC_TABORT_IF Position */ +#define CRC_DMAISR_CRC_TABORT_IF_Msk (1ul << CRC_DMAISR_CRC_TABORT_IF_Pos) /*!< CRC_T::DMAISR: CRC_TABORT_IF Mask */ + +/* CRC WDATA Bit Field Definitions */ +#define CRC_WDATA_CRC_WDATA_Pos 0 /*!< CRC_T::WDATA: CRC_WDATA Position */ +#define CRC_WDATA_CRC_WDATA_Msk (0xFFFFFFFFul << CRC_WDATA_CRC_WDATA_Pos) /*!< CRC_T::WDATA: CRC_WDATA Mask */ + +/* CRC SEED Bit Field Definitions */ +#define CRC_SEED_CRC_SEED_Pos 0 /*!< CRC_T::SEED: CRC_SEED Position */ +#define CRC_SEED_CRC_SEED_Msk (0xFFFFFFFFul << CRC_SEED_CRC_SEED_Pos) /*!< CRC_T::SEED: CRC_SEED Mask */ + +/* CRC CHECKSUM Bit Field Definitions */ +#define CRC_CHECKSUM_CRC_CHECKSUM_Pos 0 /*!< CRC_T::CHECKSUM: CRC_CHECKSUM Position */ +#define CRC_CHECKSUM_CRC_CHECKSUM_Msk (0xFFFFFFFFul << CRC_CHECKSUM_CRC_CHECKSUM_Pos) /*!< CRC_T::CHECKSUM: CRC_CHECKSUM Mask */ +/*@}*/ /* end of group REG_CRC_BITMASK */ +/*@}*/ /* end of group REG_CRC */ + +/*-------------------------- FLASH Memory Controller -------------------------*/ +/** @addtogroup REG_FMC Flash Memory Controller (FMC) + Memory Mapped Structure for Flash Memory Controller + @{ + */ + +typedef struct +{ + + +/** + * @var FMC_T::ISPCON + * Offset: 0x00 ISP Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |ISPEN |ISP Enable + * | | |This bit is protected bit. ISP function enable bit. Set this bit to enable ISP function. + * | | |1 = Enable ISP function + * | | |0 = Disable ISP function + * |[1] |BS |Boot Select + * | | |This bit is protected bit. Set/clear this bit to select next booting from LDROM/APROM, + * | | |respectively. This bit also functions as MCU booting status flag, which can be used to check where + * | | |MCU booted from. This bit is initiated with the inverted value of CBS in Config0 after power- + * | | |on reset; It keeps the same value at other reset. + * | | |1 = boot from LDROM + * | | |0 = boot from APROM + * |[4] |CFGUEN |Config Update Enable + * | | |Writing this bit to 1 enables s/w to update Config value by ISP procedure regardless of program + * | | |code is running in APROM or LDROM. + * | | |1 = Config update enable + * | | |0 = Config update disable + * |[5] |LDUEN |LDROM Update Enable + * | | |LDROM update enable bit. + * | | |1 = LDROM can be updated when the MCU runs in APROM. + * | | |0 = LDROM cannot be updated + * |[6] |ISPFF |ISP Fail Flag + * | | |This bit is set by hardware when a triggered ISP meets any of the following conditions: + * | | |(1) APROM writes to itself. + * | | |(2) LDROM writes to itself. + * | | |(3) Destination address is illegal, such as over an available range. + * | | |Write 1 to clear. + * |[7] |SWRST |Software Reset + * | | |Writing 1 to this bit to start software reset. + * | | |It is cleared by hardware after reset is finished. + * @var FMC_T::ISPADR + * Offset: 0x04 ISP Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |ISPADR |ISP Address + * | | |it supports word program only. ISPARD[1:0] must be kept 2'b00 for ISP operation. + * @var FMC_T::ISPDAT + * Offset: 0x08 ISP Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |ISPDAT |ISP Data + * | | |Write data to this register before ISP program operation + * | | |Read data from this register after ISP read operation + * @var FMC_T::ISPCMD + * Offset: 0x0C ISP Command Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[5:0] |ISPCMD |ISP Command + * | | |ISP command table is shown below: + * | | |0x00 = Read. + * | | |0x21 = Program. + * | | |0x22 = Page Erase. + * @var FMC_T::ISPTRG + * Offset: 0x10 ISP Trigger Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |ISPGO |ISP start trigger + * | | |Write 1 to start ISP operation and this bit will be cleared to 0 by hardware automatically when ISP + * | | |operation is finish. + * | | |1 = ISP is on going + * | | |0 = ISP done + * @var FMC_T::DFBADR + * Offset: 0x14 Data Flash Base Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |DFBA |Data Flash Base Address + * | | |This register indicates data flash start address. + * | | |It is a read only register. + * | | |For 8/16/32/64kB flash memory device, the data flash size is 4kB and it start address is fixed at + * | | |0x01F000 by hardware internally. + * @var FMC_T::FATCON + * Offset: 0x18 Flash Access Time Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |FPSEN |Flash Power Save Enable + * | | |If CPU clock is slower than 24 MHz, then s/w can enable flash power saving function. + * | | |1 = Enable flash power saving + * | | |0 = Disable flash power saving + * |[4] |L_SPEED |Flash Low Speed Mode Enable + * | | |1 = Flash access always no wait state (zero wait state) + * | | |0 = Insert wait state while Flash access discontinued address. + * | | |Note: Set this bit only when HCLK <= 25MHz. If HCLK > 25MHz, CPU will fetch wrong + * | | |code and cause fail result. + * @var FMC_T::ISPSTA + * Offset: 0x40 ISP Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |ISPGO |ISP Start Trigger (Read Only) + * | | |Write 1 to start ISP operation and this bit will be cleared to 0 by hardware + * | | |automatically when ISP operation is finished. + * | | |0 = ISP operation finished. + * | | |1 = ISP operation progressed. + * | | |Note: This bit is the same as ISPTRG bit0 + * |[2:1] |CBS |Chip Boot Selection (Read Only) + * | | |This is a mirror of CBS in Config0. + * |[6] |ISPFF |ISP Fail Flag (Write-protection Bit) + * | | |This bit is set by hardware when a triggered ISP meets any of the following conditions: + * | | |(1) APROM writes to itself. + * | | |(2) LDROM writes to itself. + * | | |(3) CONFIG is erased/programmed when CFGUEN is set to 0 + * | | |(4) Destination address is illegal, such as over an available range. + * |[20:9] |VECMAP |Vector Page Mapping Address (Read Only) + * | | |The current flash address space 0x0000_0000~0x0000_01FF is mapping to the address + * | | |{VECMAP[11:0], 9'h000} ~ {VECMAP[11:0], 9'h1FF} + */ + + __IO uint32_t ISPCON; /* Offset: 0x00 ISP Control Register */ + __IO uint32_t ISPADR; /* Offset: 0x04 ISP Address Register */ + __IO uint32_t ISPDAT; /* Offset: 0x08 ISP Data Register */ + __IO uint32_t ISPCMD; /* Offset: 0x0C ISP Command Register */ + __IO uint32_t ISPTRG; /* Offset: 0x10 ISP Trigger Control Register */ + __I uint32_t DFBADR; /* Offset: 0x14 Data Flash Base Address Register */ + __IO uint32_t FATCON; /* Offset: 0x18 Flash Access Time Control Register */ + __I uint32_t RESERVED[9]; + __IO uint32_t ISPSTA; /* Offset: 0x40 ISP Status Register */ + +} FMC_T; + + + +/** @addtogroup REG_FMC_BITMASK FMC Bit Mask + @{ + */ + +/* FMC ISPCON Bit Field Definitions */ +#define FMC_ISPCON_ET_Pos 12 /*!< FMC_T::ISPCON: ET Position */ +#define FMC_ISPCON_ET_Msk (7ul << FMC_ISPCON_ET_Pos) /*!< FMC_T::ISPCON: ET Mask */ + +#define FMC_ISPCON_PT_Pos 8 /*!< FMC_T::ISPCON: PT Position */ +#define FMC_ISPCON_PT_Msk (7ul << FMC_ISPCON_PT_Pos) /*!< FMC_T::ISPCON: PT Mask */ + +#define FMC_ISPCON_ISPFF_Pos 6 /*!< FMC_T::ISPCON: ISPFF Position */ +#define FMC_ISPCON_ISPFF_Msk (1ul << FMC_ISPCON_ISPFF_Pos) /*!< FMC_T::ISPCON: ISPFF Mask */ + +#define FMC_ISPCON_LDUEN_Pos 5 /*!< FMC_T::ISPCON: LDUEN Position */ +#define FMC_ISPCON_LDUEN_Msk (1ul << FMC_ISPCON_LDUEN_Pos) /*!< FMC_T::ISPCON: LDUEN Mask */ + +#define FMC_ISPCON_CFGUEN_Pos 4 /*!< FMC_T::ISPCON: CFGUEN Position */ +#define FMC_ISPCON_CFGUEN_Msk (1ul << FMC_ISPCON_CFGUEN_Pos) /*!< FMC_T::ISPCON: CFGUEN Mask */ + +#define FMC_ISPCON_APUEN_Pos 3 /*!< FMC_T::ISPCON: APUEN Position */ +#define FMC_ISPCON_APUEN_Msk (1ul << FMC_ISPCON_APUEN_Pos) /*!< FMC_T::ISPCON: APUEN Mask */ + +#define FMC_ISPCON_BS_Pos 1 /*!< FMC_T::ISPCON: BS Position */ +#define FMC_ISPCON_BS_Msk (0x1ul << FMC_ISPCON_BS_Pos) /*!< FMC_T::ISPCON: BS Mask */ + +#define FMC_ISPCON_ISPEN_Pos 0 /*!< FMC_T::ISPCON: ISPEN Position */ +#define FMC_ISPCON_ISPEN_Msk (1ul << FMC_ISPCON_ISPEN_Pos) /*!< FMC_T::ISPCON: ISPEN Mask */ + +/* FMC ISPADR Bit Field Definitions */ +#define FMC_ISPADR_ISPADR_Pos 0 /*!< FMC_T::ISPADR: ISPADR Position */ +#define FMC_ISPADR_ISPADR_Msk (0xFFFFFFFFul << FMC_ISPADR_ISPADR_Pos) /*!< FMC_T::ISPADR: ISPADR Mask */ + +/* FMC ISPADR Bit Field Definitions */ +#define FMC_ISPDAT_ISPDAT_Pos 0 /*!< FMC_T::ISPDAT: ISPDAT Position */ +#define FMC_ISPDAT_ISPDAT_Msk (0xFFFFFFFFul << FMC_ISPDAT_ISPDAT_Pos) /*!< FMC_T::ISPDAT: ISPDAT Mask */ + +/* FMC ISPCMD Bit Field Definitions */ +#define FMC_ISPCMD_FOEN_Pos 5 /*!< FMC_T::ISPCMD: FOEN Position */ +#define FMC_ISPCMD_FOEN_Msk (1ul << FMC_ISPCMD_FOEN_Pos) /*!< FMC_T::ISPCMD: FOEN Mask */ + +#define FMC_ISPCMD_FCEN_Pos 4 /*!< FMC_T::ISPCMD: FCEN Position */ +#define FMC_ISPCMD_FCEN_Msk (1ul << FMC_ISPCMD_FCEN_Pos) /*!< FMC_T::ISPCMD: FCEN Mask */ + +#define FMC_ISPCMD_FCTRL_Pos 0 /*!< FMC_T::ISPCMD: FCTRL Position */ +#define FMC_ISPCMD_FCTRL_Msk (0xFul << FMC_ISPCMD_FCTRL_Pos) /*!< FMC_T::ISPCMD: FCTRL Mask */ + +/* FMC ISPTRG Bit Field Definitions */ +#define FMC_ISPTRG_ISPGO_Pos 0 /*!< FMC_T::ISPTRG: ISPGO Position */ +#define FMC_ISPTRG_ISPGO_Msk (1ul << FMC_ISPTRG_ISPGO_Pos) /*!< FMC_T::ISPTRG: ISPGO Mask */ + +/* FMC DFBADR Bit Field Definitions */ +#define FMC_DFBADR_DFBA_Pos 0 /*!< FMC_T::DFBADR: DFBA Position */ +#define FMC_DFBADR_DFBA_Msk (0xFFFFFFFFul << FMC_DFBADR_DFBA_Pos) /*!< FMC_T::DFBADR: DFBA Mask */ + +/* FMC FATCON Bit Field Definitions */ +#define FMC_FATCON_FOMSEL1_Pos 6 /*!< FMC_T::FATCON: FOMSEL1 Position */ +#define FMC_FATCON_FOMSEL1_Msk (1ul << FMC_FATCON_FOMSEL1_Pos) /*!< FMC_T::FATCON: FOMSEL1 Mask */ + +#define FMC_FATCON_FOMSEL0_Pos 4 /*!< FMC_T::FATCON: FOMSEL0 Position */ +#define FMC_FATCON_FOMSEL0_Msk (1ul << FMC_FATCON_FOMSEL0_Pos) /*!< FMC_T::FATCON: FOMSEL0 Mask */ + +#define FMC_FATCON_FATS_Pos 1 /*!< FMC_T::FATCON: FATS Position */ +#define FMC_FATCON_FATS_Msk (7ul << FMC_FATCON_FATS_Pos) /*!< FMC_T::FATCON: FATS Mask */ + +#define FMC_FATCON_FPSEN_Pos 0 /*!< FMC_T::FATCON: FPSEN Position */ +#define FMC_FATCON_FPSEN_Msk (1ul << FMC_FATCON_FPSEN_Pos) /*!< FMC_T::FATCON: FPSEN Mask */ + + +#define FMC_ISPSTA_ISPGO_Pos 0 /*!< FMC_T::ISPSTA: ISPGO Position */ +#define FMC_ISPSTA_ISPGO_Msk (1ul << FMC_ISPSTA_ISPGO_Pos) /*!< FMC_T::ISPSTA: ISPGO Mask */ + +#define FMC_ISPSTA_CBS_Pos 1 /*!< FMC_T::ISPSTA: CBS Position */ +#define FMC_ISPSTA_CBS_Msk (0x3ul << FMC_ISPSTA_CBS_Pos) /*!< FMC_T::ISPSTA: CBS Mask */ + +#define FMC_ISPSTA_ISPFF_Pos 6 /*!< FMC_T::ISPSTA: ISPFF Position */ +#define FMC_ISPSTA_ISPFF_Msk (0x3ul << FMC_ISPSTA_ISPFF_Pos) /*!< FMC_T::ISPSTA: ISPFF Mask */ + +#define FMC_ISPSTA_VECMAP_Pos 9 /*!< FMC_T::ISPSTA: VECMAP Position */ +#define FMC_ISPSTA_VECMAP_Msk (0xFFFul << FMC_ISPSTA_VECMAP_Pos) /*!< FMC_T::ISPSTA: VECMAP Mask */ + +/*@}*/ /* end of group REG_FMC_BITMASK */ +/*@}*/ /* end of group REG_FMC */ + + + +/*--------------------- General Purpose I/O (GPIO) ---------------------*/ +/** @addtogroup REG_GPIO General Purpose Input/Output Controller (GPIO) + Memory Mapped Structure for General Purpose I/O + @{ + */ + +typedef struct +{ + + +/** + * @var GPIO_T::PMD + * Offset: 0x00/0x40/0x80/0xC0/0x140 GPIO Port [A/B/C/D/F] Pin I/O Mode Control + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2n+1:2n]|PMDn |GPIOx I/O Pin[n] Mode Control + * | | |Determine each I/O mode of GPIOx pins. + * | | |00 = GPIO port [n] pin is in Input mode. + * | | |01 = GPIO port [n] pin is in Push-pull Output mode. + * | | |10 = GPIO port [n] pin is in Open-drain Output mode. + * | | |11 = GPIO port [n] pin is in Quasi-bidirectional mode. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::OFFD + * Offset: 0x04/0x44/0x84/0xC4/0x144 GPIO Port [A/B/C/D/F] Pin Digital Input Path Disable Control + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[[n+16]]|OFFDn |GPIOx Pin[n] Digital Input Path Disable Control + * | | |Each of these bits is used to control if the digital input path of corresponding GPIO pin is disabled. + * | | |If input is analog signal, users can disable GPIO digital input path to avoid current leakage. + * | | |0 = I/O digital input path Enabled. + * | | |1 = I/O digital input path Disabled (digital input tied to low). + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::DOUT + * Offset: 0x08/0x48/0x88/0xC8/0x148 GPIO Port [A/B/C/D/F] Data Output Value + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |DOUTn |GPIOx Pin[n] Output Value + * | | |Each of these bits controls the status of a Px.n pin when the Px.n is configured as Push-pull output, Open-drain output or Quasi-bidirectional mode. + * | | |0 = Px.n will drive Low if the Px.n pin is configured as Push-pull output, Open-drain output or Quasi-bidirectional mode. + * | | |1 = Px.n will drive High if the Px.n pin is configured as Push-pull output or Quasi-bidirectional mode. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::DMASK + * Offset: 0x0C/0x4C/0x8C/0xCC/0x14C GPIO Port [A/B/C/D/F] Data Output Write Mask + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |DMASKn |GPIOx Pin[n] Data Output Write Mask + * | | |These bits are used to protect the corresponding DOUT (GPIOx_DOUT[n]) bit. + * | | |When the DATMSK (GPIOx _DATMSK[n]) bit is set to 1, the corresponding DOUT (GPIOx _DOUT[n]) bit is protected. + * | | |If the write signal is masked, writing data to the protect bit is ignored. + * | | |0 = Corresponding DOUT (GPIOx_DOUT[n]) bit can be updated. + * | | |1 = Corresponding DOUT (GPIOx_DOUT[n]) bit protected. + * | | |Note1: This function only protect corresponding DOUT (GPIOx_DOUT[n]) bit, and will not protect corresponding bit control register GPIOxn_DOUT. + * | | |Note2: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::PIN + * Offset: 0x10/0x50/0x90/0xD0/0x150 GPIO Port [A/B/C/D/F] Pin Value + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |PINn |GPIOx Pin[n] Pin Values + * | | |Each bit of the register reflects the actual status of the respective GPIO pin. + * | | |If the bit is 1, it indicates the corresponding pin status is high, else the pin status is low. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::DBEN + * Offset: 0x14/0x54/0x94/0xD4/0x154 GPIO Port [A/B/C/D/F] De-bounce Enable + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |DBENn |GPIOx Pin[n] Input Signal De-Bounce Enable + * | | |The DBEN[n] bit is used to enable the de-bounce function for each corresponding bit. + * | | |If the input signal pulse width cannot be sampled by continuous two de-bounce sample cycle, the input signal transition is seen as the signal bounce and will not trigger the interrupt. + * | | |The de-bounce clock source is controlled by DBCLKSRC (DBNCECON [4]), one de-bounce sample cycle period is controlled by DBCLKSEL (DBNCECON [3:0]). + * | | |0 = Px.n de-bounce function Disabled. + * | | |1 = Px.n de-bounce function Enabled. + * | | |The de-bounce function is valid only for edge triggered interrupt. If the interrupt mode is level triggered, the de-bounce enable bit is ignored. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::IMD + * Offset: 0x18/0x58/0x98/0xD8/0x158 GPIO Port [A/B/C/D/F] Interrupt Mode Control + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |IMDn |GPIOx Pin[n] Edge Or Level Detection Interrupt Control + * | | |IMD[n] is used to control the interrupt is by level trigger or by edge trigger. + * | | |If the interrupt is by edge trigger, the trigger source can be controlled by de-bounce. + * | | |If the interrupt is by level trigger, the input source is sampled by one HCLK clock and generates the interrupt. + * | | |0 = Edge trigger interrupt. + * | | |1 = Level trigger interrupt. + * | | |If the pin is set as the level trigger interrupt, only one level can be set on the registers GPIOx_IEN. + * | | |If both levels to trigger interrupt are set, the setting is ignored and no interrupt will occur. + * | | |The de-bounce function is valid only for edge triggered interrupt. If the interrupt mode is level triggered, the de-bounce enable bit is ignored. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::IEN + * Offset: 0x1C/0x5C/0x9C/0xDC/0x15C GPIO Port [A/B/C/D/F] Interrupt Enable + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |IF_ENn |GPIOx Pin[n] Falling Edge or Low Level Interrupt Trigger Type Enable Bit + * | | |The IF_EN (GPIOx_IEN[n]) bit is used to enable the interrupt for each of the corresponding input Px.n pin. Set bit to 1 also enable the pin wake-up function. + * | | |When setting the IF_EN (Px_IEN[n]) bit to 1 : + * | | |If the interrupt is level trigger (IMD (GPIOx_IMD[n]) bit is set to 1), the input Px.n pin will generate the interrupt while this pin state is at low level. + * | | |If the interrupt is edge trigger(IMD (GPIOx_IMD[n]) bit is set to 0), the input Px.n pin will generate the interrupt while this pin state changed from high to low. + * | | |0 = Px.n level low or high to low interrupt Disabled. + * | | |1 = Px.n level low or high to low interrupt Enabled. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * |[n+16] |IR_ENn |GPIOx Pin[n] Rising Edge or High Level Interrupt Trigger Type Enable Bit + * | | |The IR_EN (GPIOx_IEN[n+16]) bit is used to enable the interrupt for each of the corresponding input Px.n pin. Set bit to 1 also enable the pin wake-up function. + * | | |When setting the IR_EN (GPIOx_IEN[n+16]) bit to 1 : + * | | |If the interrupt is level trigger (IMD (GPIOx_IMD[n]) bit is set to 1), the input Px.n pin will generate the interrupt while this pin state is at high level. + * | | |If the interrupt is edge trigger (IMD (Px_IMD[n]) bit is set to 0), the input Px.n pin will generate the interrupt while this pin state changed from low to high. + * | | |0 = Px.n level high or low to high interrupt Disabled. + * | | |1 = Px.n level high or low to high interrupt Enabled. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + * @var GPIO_T::ISRC + * Offset: 0x20/0x60/0xA0/0xE0/0x160 GPIO Port [A/B/C/D/F] Interrupt Source Flag + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[n] |ISRCn |GPIOx Pin[n] Interrupt Source Flag + * | | |Read : + * | | |0 = No interrupt at Px.n. + * | | |1 = Px.n generates an interrupt. + * | | |Write : + * | | |0= No action. + * | | |1= Clear the corresponding pending interrupt. + * | | |Note: + * | | |n = 10~15 for port A. Others are reserved. + * | | |n = 0~10, 12~15 for port B. Others are reserved. + * | | |n = 0~5, 8~13 for port C. Others are reserved. + * | | |n = 0~5, 8~11 for port D. Others are reserved. + * | | |n = 0~3 for port F. Others are reserved. + */ + + __IO uint32_t PMD; /* Offset: 0x00/0x40/0x80/0xC0/0x140 GPIO Port [A/B/C/D/F] Pin I/O Mode Control */ + __IO uint32_t OFFD; /* Offset: 0x04/0x44/0x84/0xC4/0x144 GPIO Port [A/B/C/D/F] Pin Digital Input Path Disable Control */ + __IO uint32_t DOUT; /* Offset: 0x08/0x48/0x88/0xC8/0x148 GPIO Port [A/B/C/D/F] Data Output Value */ + __IO uint32_t DMASK; /* Offset: 0x0C/0x4C/0x8C/0xCC/0x14C GPIO Port [A/B/C/D/F] Data Output Write Mask */ + __I uint32_t PIN; /* Offset: 0x10/0x50/0x90/0xD0/0x150 GPIO Port [A/B/C/D/F] Pin Value */ + __IO uint32_t DBEN; /* Offset: 0x14/0x54/0x94/0xD4/0x154 GPIO Port [A/B/C/D/F] De-bounce Enable */ + __IO uint32_t IMD; /* Offset: 0x18/0x58/0x98/0xD8/0x158 GPIO Port [A/B/C/D/F] Interrupt Mode Control */ + __IO uint32_t IEN; /* Offset: 0x1C/0x5C/0x9C/0xDC/0x15C GPIO Port [A/B/C/D/F] Interrupt Enable */ + __IO uint32_t ISRC; /* Offset: 0x20/0x60/0xA0/0xE0/0x160 GPIO Port [A/B/C/D/F] Interrupt Source Flag */ + +} GPIO_T; + + + + +typedef struct +{ + + +/** + * @var GPIO_DBNCECON_T::DBNCECON + * Offset: 0x180 External Interrupt De-bounce Control + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |DBCLKSEL |De-Bounce Sampling Cycle Selection + * | | |0000 = Sample interrupt input once per 1 clocks. + * | | |0001 = Sample interrupt input once per 2 clocks. + * | | |0010 = Sample interrupt input once per 4 clocks. + * | | |0011 = Sample interrupt input once per 8 clocks. + * | | |0100 = Sample interrupt input once per 16 clocks. + * | | |0101 = Sample interrupt input once per 32 clocks. + * | | |0110 = Sample interrupt input once per 64 clocks. + * | | |0111 = Sample interrupt input once per 128 clocks. + * | | |1000 = Sample interrupt input once per 256 clocks. + * | | |1001 = Sample interrupt input once per 2*256 clocks. + * | | |1010 = Sample interrupt input once per 4*256clocks. + * | | |1011 = Sample interrupt input once per 8*256 clocks. + * | | |1100 = Sample interrupt input once per 16*256 clocks. + * | | |1101 = Sample interrupt input once per 32*256 clocks. + * | | |1110 = Sample interrupt input once per 64*256 clocks. + * | | |1111 = Sample interrupt input once per 128*256 clocks. + * |[4] |DBCLKSRC |De-Bounce Counter Clock Source Selection + * | | |0 = De-bounce counter clock source is the HCLK. + * | | |1 = De-bounce counter clock source is the internal 10 kHz low speed oscillator. + * |[5] |ICLK_ON |Interrupt Clock On Mode + * | | |0 = Edge detection circuit is active only if I/O pin corresponding GPIOx_IEN bit is set to 1. + * | | |1 = All I/O pins edge detection circuit is always active after reset. + * | | |It is recommended to turn off this bit to save system power if no special application concern. + */ + + __IO uint32_t DBNCECON; /* Offset: 0x180 External Interrupt De-bounce Control */ + +} GPIO_DBNCECON_T; + + + +/** @addtogroup REG_GPIO_BITMASK GPIO Bit Mask + @{ + */ + +/* GPIO PMD Bit Field Definitions */ +#define GPIO_PMD_PMD15_Pos 30 /*!< GPIO_T::PMD: PMD15 Position */ +#define GPIO_PMD_PMD15_Msk (0x3ul << GPIO_PMD_PMD15_Pos) /*!< GPIO_T::PMD: PMD15 Mask */ + +#define GPIO_PMD_PMD14_Pos 28 /*!< GPIO_T::PMD: PMD14 Position */ +#define GPIO_PMD_PMD14_Msk (0x3ul << GPIO_PMD_PMD14_Pos) /*!< GPIO_T::PMD: PMD14 Mask */ + +#define GPIO_PMD_PMD13_Pos 26 /*!< GPIO_T::PMD: PMD13 Position */ +#define GPIO_PMD_PMD13_Msk (0x3ul << GPIO_PMD_PMD13_Pos) /*!< GPIO_T::PMD: PMD13 Mask */ + +#define GPIO_PMD_PMD12_Pos 24 /*!< GPIO_T::PMD: PMD12 Position */ +#define GPIO_PMD_PMD12_Msk (0x3ul << GPIO_PMD_PMD12_Pos) /*!< GPIO_T::PMD: PMD12 Mask */ + +#define GPIO_PMD_PMD11_Pos 22 /*!< GPIO_T::PMD: PMD11 Position */ +#define GPIO_PMD_PMD11_Msk (0x3ul << GPIO_PMD_PMD11_Pos) /*!< GPIO_T::PMD: PMD11 Mask */ + +#define GPIO_PMD_PMD10_Pos 20 /*!< GPIO_T::PMD: PMD10 Position */ +#define GPIO_PMD_PMD10_Msk (0x3ul << GPIO_PMD_PMD10_Pos) /*!< GPIO_T::PMD: PMD10 Mask */ + +#define GPIO_PMD_PMD9_Pos 18 /*!< GPIO_T::PMD: PMD9 Position */ +#define GPIO_PMD_PMD9_Msk (0x3ul << GPIO_PMD_PMD9_Pos) /*!< GPIO_T::PMD: PMD9 Mask */ + +#define GPIO_PMD_PMD8_Pos 16 /*!< GPIO_T::PMD: PMD8 Position */ +#define GPIO_PMD_PMD8_Msk (0x3ul << GPIO_PMD_PMD8_Pos) /*!< GPIO_T::PMD: PMD8 Mask */ + +#define GPIO_PMD_PMD7_Pos 14 /*!< GPIO_T::PMD: PMD7 Position */ +#define GPIO_PMD_PMD7_Msk (0x3ul << GPIO_PMD_PMD7_Pos) /*!< GPIO_T::PMD: PMD7 Mask */ + +#define GPIO_PMD_PMD6_Pos 12 /*!< GPIO_T::PMD: PMD6 Position */ +#define GPIO_PMD_PMD6_Msk (0x3ul << GPIO_PMD_PMD6_Pos) /*!< GPIO_T::PMD: PMD6 Mask */ + +#define GPIO_PMD_PMD5_Pos 10 /*!< GPIO_T::PMD: PMD5 Position */ +#define GPIO_PMD_PMD5_Msk (0x3ul << GPIO_PMD_PMD5_Pos) /*!< GPIO_T::PMD: PMD5 Mask */ + +#define GPIO_PMD_PMD4_Pos 8 /*!< GPIO_T::PMD: PMD4 Position */ +#define GPIO_PMD_PMD4_Msk (0x3ul << GPIO_PMD_PMD4_Pos) /*!< GPIO_T::PMD: PMD4 Mask */ + +#define GPIO_PMD_PMD3_Pos 6 /*!< GPIO_T::PMD: PMD3 Position */ +#define GPIO_PMD_PMD3_Msk (0x3ul << GPIO_PMD_PMD3_Pos) /*!< GPIO_T::PMD: PMD3 Mask */ + +#define GPIO_PMD_PMD2_Pos 4 /*!< GPIO_T::PMD: PMD2 Position */ +#define GPIO_PMD_PMD2_Msk (0x3ul << GPIO_PMD_PMD2_Pos) /*!< GPIO_T::PMD: PMD2 Mask */ + +#define GPIO_PMD_PMD1_Pos 2 /*!< GPIO_T::PMD: PMD1 Position */ +#define GPIO_PMD_PMD1_Msk (0x3ul << GPIO_PMD_PMD1_Pos) /*!< GPIO_T::PMD: PMD1 Mask */ + +#define GPIO_PMD_PMD0_Pos 0 /*!< GPIO_T::PMD: PMD0 Position */ +#define GPIO_PMD_PMD0_Msk (0x3ul << GPIO_PMD_PMD0_Pos) /*!< GPIO_T::PMD: PMD0 Mask */ + +/* GPIO OFFD Bit Field Definitions */ +#define GPIO_OFFD_OFFD_Pos 16 /*!< GPIO_T::OFFD: OFFD Position */ +#define GPIO_OFFD_OFFD_Msk (0xFFFFul << GPIO_OFFD_OFFD_Pos) /*!< GPIO_T::OFFD: OFFD Mask */ + +/* GPIO DOUT Bit Field Definitions */ +#define GPIO_DOUT_DOUT_Pos 0 /*!< GPIO_T::DOUT: DOUT Position */ +#define GPIO_DOUT_DOUT_Msk (0xFFFFul << GPIO_DOUT_DOUT_Pos) /*!< GPIO_T::DOUT: DOUT Mask */ + +/* GPIO DMASK Bit Field Definitions */ +#define GPIO_DMASK_DMASK_Pos 0 /*!< GPIO_T::DMASK: DMASK Position */ +#define GPIO_DMASK_DMASK_Msk (0xFFFFul << GPIO_DMASK_DMASK_Pos) /*!< GPIO_T::DMASK: DMASK Mask */ + +/* GPIO PIN Bit Field Definitions */ +#define GPIO_PIN_PIN_Pos 0 /*!< GPIO_T::PIN: PIN Position */ +#define GPIO_PIN_PIN_Msk (0xFFFFul << GPIO_PIN_PIN_Pos) /*!< GPIO_T::PIN: PIN Mask */ + +/* GPIO DBEN Bit Field Definitions */ +#define GPIO_DBEN_DBEN_Pos 0 /*!< GPIO_T::DBEN: DBEN Position */ +#define GPIO_DBEN_DBEN_Msk (0xFFFFul << GPIO_DBEN_DBEN_Pos) /*!< GPIO_T::DBEN: DBEN Mask */ + +/* GPIO IMD Bit Field Definitions */ +#define GPIO_IMD_IMD_Pos 0 /*!< GPIO_T::IMD: IMD Position */ +#define GPIO_IMD_IMD_Msk (0xFFFFul << GPIO_IMD_IMD_Pos) /*!< GPIO_T::IMD: IMD Mask */ + +/* GPIO IEN Bit Field Definitions */ +#define GPIO_IEN_IR_EN_Pos 16 /*!< GPIO_T::IEN: IR_EN Position */ +#define GPIO_IEN_IR_EN_Msk (0xFFFFul << GPIO_IEN_IR_EN_Pos) /*!< GPIO_T::IEN: IR_EN Mask */ + +#define GPIO_IEN_IF_EN_Pos 0 /*!< GPIO_T::IEN: IF_EN Position */ +#define GPIO_IEN_IF_EN_Msk (0xFFFFul << GPIO_IEN_IF_EN_Pos) /*!< GPIO_T::IEN: IF_EN Mask */ + +/* GPIO ISRC Bit Field Definitions */ +#define GPIO_ISRC_ISRC_Pos 0 /*!< GPIO_T::ISRC: ISRC Position */ +#define GPIO_ISRC_ISRC_Msk (0xFFFFul << GPIO_ISRC_ISRC_Pos) /*!< GPIO_T::ISRC: ISRC Mask */ + +/* GPIO DBNCECON Bit Field Definitions */ +#define GPIO_DBNCECON_ICLK_ON_Pos 5 /*!< GPIO_DBNCECON_T::DBNCECON: ICLK_ON Position */ +#define GPIO_DBNCECON_ICLK_ON_Msk (1ul << GPIO_DBNCECON_ICLK_ON_Pos) /*!< GPIO_DBNCECON_T::DBNCECON: ICLK_ON Mask */ + +#define GPIO_DBNCECON_DBCLKSRC_Pos 4 /*!< GPIO_DBNCECON_T::DBNCECON: DBCLKSRC Position */ +#define GPIO_DBNCECON_DBCLKSRC_Msk (1ul << GPIO_DBNCECON_DBCLKSRC_Pos) /*!< GPIO_DBNCECON_T::DBNCECON: DBCLKSRC Mask */ + +#define GPIO_DBNCECON_DBCLKSEL_Pos 0 /*!< GPIO_DBNCECON_T::DBNCECON: DBCLKSEL Position */ +#define GPIO_DBNCECON_DBCLKSEL_Msk (0xFul << GPIO_DBNCECON_DBCLKSEL_Pos) /*!< GPIO_DBNCECON_T::DBNCECON: DBCLKSEL Mask */ +/*@}*/ /* end of group REG_GPIO_BITMASK */ +/*@}*/ /* end of group REG_GPIO */ + +/*------------------------------ I2C Controller ------------------------------*/ +/** @addtogroup REG_I2C Inter-IC Bus Controller (I2C) + Memory Mapped Structure for I2C Serial Interface Controller + @{ + */ + +typedef struct +{ + + +/** + * @var I2C_T::I2CON + * Offset: 0x00 I2C Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2] |AA |Assert Acknowledge Control + * | | |When AA =1 prior to address or data received, an acknowledged (low level to I2Cn_SDA) will be returned during the acknowledge clock pulse on the I2Cn_SCL line when 1.) A slave is acknowledging the address sent from master, 2.) The receiver devices are acknowledging the data sent by transmitter. + * | | |When AA=0 prior to address or data received, a Not acknowledged (high level to I2Cn_SDA) will be returned during the acknowledge clock pulse on the I2Cn_SCL line. + * |[3] |SI |I2C Interrupt Flag + * | | |When a new I2C state is present in the I2CSTATUS register, the SI flag is set by hardware, and if bit EI (I2CON [7]) is set, the I2C interrupt is requested. + * | | |SI must be cleared by software. + * | | |Clear SI by writing 1 to this bit. + * |[4] |STO |I2C STOP Control + * | | |In Master mode, setting STO to transmit a STOP condition to bus then I2C hardware will check the bus condition if a STOP condition is detected this bit will be cleared by hardware automatically. + * | | |In a slave mode, setting STO resets I2C hardware to the defined "not addressed" slave mode. + * | | |This means it is NO LONGER in the slave receiver mode to receive data from the master transmit device. + * |[5] |STA |I2C START Control + * | | |Setting STA to logic 1 to enter Master mode, the I2C hardware sends a START or repeat START condition to bus when the bus is free. + * |[6] |ENS1 |I2C Controller Enable + * | | |0 = Disabled. + * | | |1 = Enabled. + * | | |Set to enable I2C serial function controller. + * | | |When ENS1=1 the I2C serial function enables. + * | | |The multi-function pin function of I2Cn_SDA and I2Cn_SCL must set to I2C function first. + * |[7] |EI |Enable Interrupt + * | | |0 = I2C interrupt Disabled. + * | | |1 = I2C interrupt Enabled. + * @var I2C_T::I2CADDR0 + * Offset: 0x04 I2C Slave Address Register0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GC |General Call Function + * | | |0 = General Call Function Disabled. + * | | |1 = General Call Function Enabled. + * |[7:1] |I2CADDR |I2C Address Register + * | | |The content of this register is irrelevant when I2C is in Master mode. + * | | |In the slave mode, the seven most significant bits must be loaded with the chip's own address. + * | | |The I2C hardware will react if either of the address is matched. + * @var I2C_T::I2CDAT + * Offset: 0x08 I2C Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |I2CDAT |I2C Data Register + * | | |Bit [7:0] is located with the 8-bit transferred data of I2C serial port. + * @var I2C_T::I2CSTATUS + * Offset: 0x0C I2C Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |I2CSTATUS |I2C Status Register + * | | |The status register of I2C: + * | | |The three least significant bits are always 0. + * | | |The five most significant bits contain the status code. + * | | |There are 26 possible status codes. + * | | |When I2CSTATUS contains F8H, no serial interrupt is requested. + * | | |All other I2CSTATUS values correspond to defined I2C states. + * | | |When each of these states is entered, a status interrupt is requested (SI = 1). + * | | |A valid status code is present in I2CSTATUS one cycle after SI is set by hardware and is still present one cycle after SI has been reset by software. + * | | |In addition, states 00H stands for a Bus Error. + * | | |A Bus Error occurs when a START or STOP condition is present at an illegal position in the formation frame. + * | | |Example of illegal position are during the serial transfer of an address byte, a data byte or an acknowledge bit. + * @var I2C_T::I2CLK + * Offset: 0x10 I2C Clock Divided Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |I2CLK |I2C clock divided Register + * | | |The I2C clock rate bits: Data Baud Rate of I2C = (system clock) / (4x (I2CLK+1)). + * | | |Note: The minimum value of I2CLK is 4. + * @var I2C_T::I2CTOC + * Offset: 0x14 I2C Time-out Counter Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TIF |Time-out Flag + * | | |This bit is set by H/W when I2C time-out happened and it can interrupt CPU if I2C interrupt enable bit (EI) is set to 1. + * | | |Note: Write 1 to clear this bit. + * |[1] |DIV4 |Time-out Counter Input Clock Divided by 4 + * | | |0 = Disabled. + * | | |1 = Enabled. + * | | |When Enabled, The time-out period is extend 4 times. + * |[2] |ENTI |Time-out Counter Enable/Disable + * | | |0 = Disabled. + * | | |1 = Enabled. + * | | |When Enabled, the 14-bit time-out counter will start counting when SI is clear. + * | | |Setting flag SI to high will reset counter and re-start up counting after SI is cleared. + * @var I2C_T::I2CADDR1 + * Offset: 0x18 I2C Slave Address Register1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GC |General Call Function + * | | |0 = General Call Function Disabled. + * | | |1 = General Call Function Enabled. + * |[7:1] |I2CADDR |I2C Address Register + * | | |The content of this register is irrelevant when I2C is in Master mode. + * | | |In the slave mode, the seven most significant bits must be loaded with the chip's own address. + * | | |The I2C hardware will react if either of the address is matched. + * @var I2C_T::I2CADDR2 + * Offset: 0x1C I2C Slave Address Register2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GC |General Call Function + * | | |0 = General Call Function Disabled. + * | | |1 = General Call Function Enabled. + * |[7:1] |I2CADDR |I2C Address Register + * | | |The content of this register is irrelevant when I2C is in Master mode. + * | | |In the slave mode, the seven most significant bits must be loaded with the chip's own address. + * | | |The I2C hardware will react if either of the address is matched. + * @var I2C_T::I2CADDR3 + * Offset: 0x20 I2C Slave Address Register3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GC |General Call Function + * | | |0 = General Call Function Disabled. + * | | |1 = General Call Function Enabled. + * |[7:1] |I2CADDR |I2C Address Register + * | | |The content of this register is irrelevant when I2C is in Master mode. + * | | |In the slave mode, the seven most significant bits must be loaded with the chip's own address. + * | | |The I2C hardware will react if either of the address is matched. + * @var I2C_T::I2CADM0 + * Offset: 0x24 I2C Slave Address Mask Register0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:1] |I2CADM |I2C Address Mask Register + * | | |0 = Mask Disabled (the received corresponding register bit should be exact the same as address register.). + * | | |1 = Mask Enabled (the received corresponding address bit is don't care.). + * | | |I2C bus controllers support multiple address recognition with four address mask register. + * | | |When the bit in the address mask register is set to one, it means the received corresponding address bit is don't-care. + * | | |If the bit is set to zero, that means the received corresponding register bit should be exact the same as address register. + * @var I2C_T::I2CADM1 + * Offset: 0x28 I2C Slave Address Mask Register1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:1] |I2CADM |I2C Address Mask Register + * | | |0 = Mask Disabled (the received corresponding register bit should be exact the same as address register.). + * | | |1 = Mask Enabled (the received corresponding address bit is don't care.). + * | | |I2C bus controllers support multiple address recognition with four address mask register. + * | | |When the bit in the address mask register is set to one, it means the received corresponding address bit is don't-care. + * | | |If the bit is set to zero, that means the received corresponding register bit should be exact the same as address register. + * @var I2C_T::I2CADM2 + * Offset: 0x2C I2C Slave Address Mask Register2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:1] |I2CADM |I2C Address Mask Register + * | | |0 = Mask Disabled (the received corresponding register bit should be exact the same as address register.). + * | | |1 = Mask Enabled (the received corresponding address bit is don't care.). + * | | |I2C bus controllers support multiple address recognition with four address mask register. + * | | |When the bit in the address mask register is set to one, it means the received corresponding address bit is don't-care. + * | | |If the bit is set to zero, that means the received corresponding register bit should be exact the same as address register. + * @var I2C_T::I2CADM3 + * Offset: 0x30 I2C Slave Address Mask Register3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:1] |I2CADM |I2C Address Mask Register + * | | |0 = Mask Disabled (the received corresponding register bit should be exact the same as address register.). + * | | |1 = Mask Enabled (the received corresponding address bit is don't care.). + * | | |I2C bus controllers support multiple address recognition with four address mask register. + * | | |When the bit in the address mask register is set to one, it means the received corresponding address bit is don't-care. + * | | |If the bit is set to zero, that means the received corresponding register bit should be exact the same as address register. + * @var I2C_T::I2CWKUPCON + * Offset: 0x3C I2C Wake-up Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |WKUPEN |I2C Wake-up Enable + * | | |0 = I2C wake-up function Disabled. + * | | |1= I2C wake-up function Enabled. + * @var I2C_T::I2CWKUPSTS + * Offset: 0x40 I2C Wake-up Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |WKUPIF |I2C Wake-up Flag + * | | |When chip is woken up from Power-down mode by I2C, this bit is set to 1. + * | | |Software can write 1 to clear this bit. + */ + + __IO uint32_t I2CON; /* Offset: 0x00 I2C Control Register */ + __IO uint32_t I2CADDR0; /* Offset: 0x04 I2C Slave Address Register0 */ + __IO uint32_t I2CDAT; /* Offset: 0x08 I2C Data Register */ + __I uint32_t I2CSTATUS; /* Offset: 0x0C I2C Status Register */ + __IO uint32_t I2CLK; /* Offset: 0x10 I2C Clock Divided Register */ + __IO uint32_t I2CTOC; /* Offset: 0x14 I2C Time-out Counter Register */ + __IO uint32_t I2CADDR1; /* Offset: 0x18 I2C Slave Address Register1 */ + __IO uint32_t I2CADDR2; /* Offset: 0x1C I2C Slave Address Register2 */ + __IO uint32_t I2CADDR3; /* Offset: 0x20 I2C Slave Address Register3 */ + __IO uint32_t I2CADM0; /* Offset: 0x24 I2C Slave Address Mask Register0 */ + __IO uint32_t I2CADM1; /* Offset: 0x28 I2C Slave Address Mask Register1 */ + __IO uint32_t I2CADM2; /* Offset: 0x2C I2C Slave Address Mask Register2 */ + __IO uint32_t I2CADM3; /* Offset: 0x30 I2C Slave Address Mask Register3 */ + __I uint32_t RESERVED[2]; + __IO uint32_t I2CWKUPCON; /* Offset: 0x3C I2C Wake-up Control Register */ + __IO uint32_t I2CWKUPSTS; /* Offset: 0x40 I2C Wake-up Status Register */ + +} I2C_T; + + + +/** @addtogroup REG_I2C_BITMASK I2C Bit Mask + @{ + */ + +/* I2C I2CON Bit Field Definitions */ +#define I2C_I2CON_EI_Pos 7 /*!< I2C_T::I2CON: EI Position */ +#define I2C_I2CON_EI_Msk (1ul << I2C_I2CON_EI_Pos) /*!< I2C_T::I2CON: EI Mask */ + +#define I2C_I2CON_ENS1_Pos 6 /*!< I2C_T::I2CON: ENS1 Position */ +#define I2C_I2CON_ENS1_Msk (1ul << I2C_I2CON_ENS1_Pos) /*!< I2C_T::I2CON: ENS1 Mask */ + +#define I2C_I2CON_STA_Pos 5 /*!< I2C_T::I2CON: STA Position */ +#define I2C_I2CON_STA_Msk (1ul << I2C_I2CON_STA_Pos) /*!< I2C_T::I2CON: STA Mask */ + +#define I2C_I2CON_STO_Pos 4 /*!< I2C_T::I2CON: STO Position */ +#define I2C_I2CON_STO_Msk (1ul << I2C_I2CON_STO_Pos) /*!< I2C_T::I2CON: STO Mask */ + +#define I2C_I2CON_SI_Pos 3 /*!< I2C_T::I2CON: SI Position */ +#define I2C_I2CON_SI_Msk (1ul << I2C_I2CON_SI_Pos) /*!< I2C_T::I2CON: SI Mask */ + +#define I2C_I2CON_AA_Pos 2 /*!< I2C_T::I2CON: AA Position */ +#define I2C_I2CON_AA_Msk (1ul << I2C_I2CON_AA_Pos) /*!< I2C_T::I2CON: AA Mask */ + +/* I2C I2CADDR Bit Field Definitions */ +#define I2C_I2CADDR_I2CADDR_Pos 1 /*!< I2C_T::I2CADDR1: I2CADDR Position */ +#define I2C_I2CADDR_I2CADDR_Msk (0x7Ful << I2C_I2CADDR_I2CADDR_Pos) /*!< I2C_T::I2CADDR1: I2CADDR Mask */ + +#define I2C_I2CADDR_GC_Pos 0 /*!< I2C_T::I2CADDR1: GC Position */ +#define I2C_I2CADDR_GC_Msk (1ul << I2C_I2CADDR_GC_Pos) /*!< I2C_T::I2CADDR1: GC Mask */ + +/* I2C I2CDAT Bit Field Definitions */ +#define I2C_I2CDAT_I2CDAT_Pos 0 /*!< I2C_T::I2CDAT: I2CDAT Position */ +#define I2C_I2CDAT_I2CDAT_Msk (0xFFul << I2C_I2CDAT_I2CDAT_Pos) /*!< I2C_T::I2CDAT: I2CDAT Mask */ + +/* I2C I2CSTATUS Bit Field Definitions */ +#define I2C_I2CSTATUS_I2CSTATUS_Pos 0 /*!< I2C_T::I2CSTATUS: I2CSTATUS Position */ +#define I2C_I2CSTATUS_I2CSTATUS_Msk (0xFFul << I2C_I2CSTATUS_I2CSTATUS_Pos) /*!< I2C_T::I2CSTATUS: I2CSTATUS Mask */ + +/* I2C I2CLK Bit Field Definitions */ +#define I2C_I2CLK_I2CLK_Pos 0 /*!< I2C_T::I2CLK: I2CLK Position */ +#define I2C_I2CLK_I2CLK_Msk (0xFFul << I2C_I2CLK_I2CLK_Pos) /*!< I2C_T::I2CLK: I2CLK Mask */ + +/* I2C I2CTOC Bit Field Definitions */ +#define I2C_I2CTOC_ENTI_Pos 2 /*!< I2C_T::I2CTOC: ENTI Position */ +#define I2C_I2CTOC_ENTI_Msk (1ul << I2C_I2CTOC_ENTI_Pos) /*!< I2C_T::I2CTOC: ENTI Mask */ + +#define I2C_I2CTOC_DIV4_Pos 1 /*!< I2C_T::I2CTOC: DIV4 Position */ +#define I2C_I2CTOC_DIV4_Msk (1ul << I2C_I2CTOC_DIV4_Pos) /*!< I2C_T::I2CTOC: DIV4 Mask */ + +#define I2C_I2CTOC_TIF_Pos 0 /*!< I2C_T::I2CTOC: TIF Position */ +#define I2C_I2CTOC_TIF_Msk (1ul << I2C_I2CTOC_TIF_Pos) /*!< I2C_T::I2CTOC: TIF Mask */ + +/* I2C I2CADM Bit Field Definitions */ +#define I2C_I2CADM_I2CADM_Pos 1 /*!< I2C_T::I2CADM0: I2CADM Position */ +#define I2C_I2CADM_I2CADM_Msk (0x7Ful << I2C_I2CADM_I2CADM_Pos) /*!< I2C_T::I2CADM0: I2CADM Mask */ + +/* I2C I2CWKUPCON Bit Field Definitions */ +#define I2C_I2CWKUPCON_WKUPEN_Pos 0 /*!< I2C_T::I2CWKUPCON: WKUPEN Position */ +#define I2C_I2CWKUPCON_WKUPEN_Msk (1ul << I2C_I2CWKUPCON_WKUPEN_Pos) /*!< I2C_T::I2CWKUPCON: WKUPEN Mask */ + +/* I2C I2CWKUPSTS Bit Field Definitions */ +#define I2C_I2CWKUPSTS_WKUPIF_Pos 0 /*!< I2C_T::I2CWKUPSTS: WKUPIF Position */ +#define I2C_I2CWKUPSTS_WKUPIF_Msk (1ul << I2C_I2CWKUPSTS_WKUPIF_Pos) /*!< I2C_T::I2CWKUPSTS: WKUPIF Mask */ +/*@}*/ /* end of group REG_I2C_BITMASK */ +/*@}*/ /* end of group REG_I2C */ + +/*----------------------------- I2S Controller -------------------------------*/ +/** @addtogroup REG_I2S Integrated Inter-chip Sound(I2S) + Memory Mapped Structure for I2S Interface Controller + @{ + */ + +typedef struct +{ + + +/** + * @var I2S_T::CON + * Offset: 0x00 I2S Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |I2SEN |I2S Controller Enable + * | | |0 = Disabled. + * | | |1 = Enabled. + * |[1] |TXEN |Transmit Enable + * | | |0 = Data transmit Disabled. + * | | |1 = Data transmit Enabled. + * |[2] |RXEN |Receive Enable + * | | |0 = Data receiving Disabled. + * | | |1 = Data receiving Enabled. + * |[3] |MUTE |Transmit Mute Enable + * | | |0 = Transmit data is shifted from buffer. + * | | |1 = Send zero on transmit channel. + * |[5:4] |WORDWIDTH |Word Width + * | | |00 = data is 8-bit word. + * | | |01 = data is 16-bit word. + * | | |10 = data is 24-bit word. + * | | |11 = data is 32-bit word. + * |[6] |MONO |Monaural Data + * | | |0 = Data is stereo format. + * | | |1 = Data is monaural format. + * |[7] |FORMAT |Data Format + * | | |0 = I2S data format. + * | | |1 = MSB justified data format. + * |[8] |SLAVE |Slave Mode + * | | |I2S can operate as master or slave. + * | | |For Master mode, I2SBCLK and I2SLRCLK pins are output mode and send bit clock from NuMicro NUC123 series to Audio CODEC chip. + * | | |In Slave mode, I2SBCLK and I2SLRCLK pins are input mode and I2SBCLK and I2SLRCLK signals are received from outer Audio CODEC chip. + * | | |0 = Master mode. + * | | |1 = Slave mode. + * |[11:9] |TXTH |Transmit FIFO Threshold Level + * | | |If the count of remaining data word (32 bits) in transmit FIFO is equal to or less than threshold level then TXTHF (I2SSTATUS[18]) is set. + * | | |000 = 0 word data in transmit FIFO. + * | | |001 = 1 word data in transmit FIFO. + * | | |010 = 2 words data in transmit FIFO. + * | | |011 = 3 words data in transmit FIFO. + * | | |100 = 4 words data in transmit FIFO. + * | | |101 = 5 words data in transmit FIFO. + * | | |110 = 6 words data in transmit FIFO. + * | | |111 = 7 words data in transmit FIFO. + * |[14:12] |RXTH |Receive FIFO Threshold Level + * | | |When the count of received data word(s) in buffer is equal to or higher than threshold level, RXTHF (I2SSTATUS[10]) will be set. + * | | |000 = 1 word data in receive FIFO. + * | | |001 = 2 word data in receive FIFO. + * | | |010 = 3 word data in receive FIFO. + * | | |011 = 4 word data in receive FIFO. + * | | |100 = 5 word data in receive FIFO. + * | | |101 = 6 word data in receive FIFO. + * | | |110 = 7 word data in receive FIFO. + * | | |111 = 8 word data in receive FIFO. + * |[15] |MCLKEN |Master Clock Enable + * | | |If MCLKEN is set to 1, I2S controller will generate master clock on I2S_MCLK pin for external audio devices. + * | | |0 = Master clock Disabled. + * | | |1 = Master clock Enabled. + * |[16] |RCHZCEN |Right Channel Zero Cross Detection Enable + * | | |If this bit is set to 1, when right channel data sign bit change or next shift data bits are all 0 then RZCF flag in I2SSTATUS register is set to 1. + * | | |This function is only available in transmit operation. + * | | |0 = Right channel zero cross detection Disabled. + * | | |1 = Right channel zero cross detection Enabled. + * |[17] |LCHZCEN |Left Channel Zero Cross Detection Enable + * | | |If this bit is set to 1, when left channel data sign bit changes or next shift data bits are all 0 then LZCF flag in I2SSTATUS register is set to 1. + * | | |This function is only available in transmit operation. + * | | |0 = Left channel zero cross detection Disabled. + * | | |1 = Left channel zero cross detection Enabled. + * |[18] |CLR_TXFIFO|Clear Transmit FIFO + * | | |Write 1 to clear transmit FIFO, internal pointer is reset to FIFO start point, and TX_LEVEL[3:0] returns to 0 and + * | | |transmit FIFO becomes empty but data in transmit FIFO is not changed. + * | | |This bit is cleared by hardware automatically. Returns 0 on read. + * |[19] |CLR_RXFIFO|Clear Receive FIFO + * | | |Write 1 to clear receive FIFO, internal pointer is reset to FIFO start point, and RX_LEVEL[3:0] returns 0 and receive FIFO becomes empty. + * | | |This bit is cleared by hardware automatically. Returns 0 on read. + * |[20] |TXDMA |Enable Transmit DMA + * | | |When TX DMA is enabled, I2S request DMA to transfer data from SRAM to transmit FIFO if FIFO is not full. + * | | |0 = TX DMA Disabled. + * | | |1 = TX DMA Enabled. + * |[21] |RXDMA |Enable Receive DMA + * | | |When RX DMA is enabled, I2S requests DMA to transfer data from receive FIFO to SRAM if FIFO is not empty. + * | | |0 = RX DMA Disabled. + * | | |1 = RX DMA Enabled. + * |[23] |RXLCH |Receive Left Channel Enable + * | | |When monaural format is selected (MONO = 1), I2S controller will receive right channel data if RXLCH is set to 0, + * | | |and receive left channel data if RXLCH is set to 1. + * | | |0 = Receive right channel data in Mono mode. + * | | |1 = Receive left channel data in Mono mode. + * @var I2S_T::CLKDIV + * Offset: 0x04 I2S Clock Divider Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |MCLK_DIV |Master Clock Divider + * | | |If MCLKEN is set to 1, I2S controller will generate master clock for external audio devices. + * | | |The master clock rate, F_MCLK, is determined by the following expressions. + * | | |If MCLK_DIV >= 1, F_MCLK = F_I2SCLK/(2x(MCLK_DIV)). + * | | |If MCLK_DIV = 0, F_MCLK = F_I2SCLK. + * | | |F_I2SCLK is the frequency of I2S peripheral clock. + * | | |In general, the master clock rate is 256 times sampling clock rate. + * |[15:8] |BCLK_DIV |Bit Clock Divider + * | | |The I2S controller will generate bit clock in Master mode. + * | | |The bit clock rate, F_BCLK, is determined by the following expression. + * | | |F_BCLK = F_I2SCLK /(2x(BCLK_DIV + 1)) , where F_I2SCLK is the frequency of I2S peripheral clock. + * @var I2S_T::IE + * Offset: 0x08 I2S Interrupt Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RXUDFIE |Receive FIFO Underflow Interrupt Enable + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[1] |RXOVFIE |Receive FIFO Overflow Interrupt Enable + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[2] |RXTHIE |Receive FIFO Threshold Level Interrupt Enable + * | | |When the count of data words in receive FIFO is equal to or higher than RXTH (I2SCON[14:12]) and + * | | |this bit is set to 1, receive FIFO threshold level interrupt will be asserted. + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[8] |TXUDFIE |Transmit FIFO Underflow Interrupt Enable + * | | |Interrupt occurs if this bit is set to 1 and the transmit FIFO underflow flag is set to 1. + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[9] |TXOVFIE |Transmit FIFO Overflow Interrupt Enable + * | | |Interrupt occurs if this bit is set to 1 and the transmit FIFO overflow flag is set to 1 + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[10] |TXTHIE |Transmit FIFO Threshold Level Interrupt Enable + * | | |Interrupt occurs if this bit is set to 1 and the count of data words in transmit FIFO is less than TXTH (I2SCON[11:9]). + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[11] |RZCIE |Right Channel Zero-Cross Interrupt Enable + * | | |Interrupt occurs if this bit is set to 1 and right channel zero-cross event is detected. + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * |[12] |LZCIE |Left Channel Zero-Cross Interrupt Enable + * | | |Interrupt occurs if this bit is set to 1 and left channel zero-cross event is detected. + * | | |0 = Interrupt Disabled. + * | | |1 = Interrupt Enabled. + * @var I2S_T::STATUS + * Offset: 0x0C I2S Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |I2SINT |I2S Interrupt Flag + * | | |This bit is wire-OR of I2STXINT and I2SRXINT bits. + * | | |0 = No I2S interrupt. + * | | |1 = I2S interrupt. + * | | |Note: This bit is read only. + * |[1] |I2SRXINT |I2S Receive Interrupt + * | | |0 = No receive interrupt. + * | | |1 = Receive interrupt. + * | | |Note: This bit is read only. + * |[2] |I2STXINT |I2S Transmit Interrupt + * | | |0 = No transmit interrupt. + * | | |1 = Transmit interrupt. + * | | |Note: This bit is read only. + * |[3] |RIGHT |Right Channel + * | | |This bit indicates current transmit data is belong to which channel + * | | |0 = Left channel. + * | | |1 = Right channel. + * | | |Note: This bit is read only. + * |[8] |RXUDF |Receive FIFO Underflow Flag + * | | |Underflow event will occur if read the empty receive FIFO. + * | | |0 = No underflow event occurred. + * | | |1 = Underflow. + * | | |Note: Write 1 to clear this bit to 0. + * |[9] |RXOVF |Receive FIFO Overflow Flag + * | | |When receive FIFO is full and hardware attempt to write data to receive FIFO, this bit will be set to 1, data in 1st buffer will be overwrote. + * | | |0 = No overflow. + * | | |1 = Overflow. + * | | |Note: Write 1 to clear this bit to 0. + * |[10] |RXTHF |Receive FIFO Threshold Flag + * | | |When data word(s) in receive FIFO is equal to or larger than threshold value set in RXTH (I2SCON[14:12]). + * | | |The RXTHF bit becomes to 1. + * | | |It keeps at 1 till RX_LEVEL (I2SSTATUS[27:24]) is less than RXTH. + * | | |0 = Data word(s) in FIFO is less than threshold level. + * | | |1 = Data word(s) in FIFO is equal to or larger than threshold level. + * | | |Note: This bit is read only. + * |[11] |RXFULL |Receive FIFO Full + * | | |This bit reflects the count of data in receive FIFO is 8 + * | | |0 = Not full. + * | | |1 = Full. + * | | |Note: This bit is read only. + * |[12] |RXEMPTY |Receive FIFO Empty + * | | |This bit reflects the count of data in receive FIFO is 0 + * | | |0 = Not empty. + * | | |1 = Empty. + * | | |Note: This bit is read only. + * |[16] |TXUDF |Transmit FIFO Underflow Flag + * | | |If transmit FIFO is empty and hardware reads data from transmit FIFO. This bit will be set to 1. + * | | |0 = No underflow. + * | | |1 = Underflow. + * | | |Note: Software can write 1 to clear this bit to 0. + * |[17] |TXOVF |Transmit FIFO Overflow Flag + * | | |This bit will be set to 1 if writes data to transmit FIFO when transmit FIFO is full. + * | | |0 = No overflow. + * | | |1 = Overflow. + * | | |Note: Write 1 to clear this bit to 0. + * |[18] |TXTHF |Transmit FIFO Threshold Flag + * | | |When the count of data stored in transmit-FIFO is equal to or less than threshold value set in TXTH (I2SCON[11:9]). + * | | |The TXTHF bit becomes to 1. + * | | |It keeps at 1 till TX_LEVEL (I2SSTATUS[31:28]) is larger than TXTH. + * | | |0 = Data word(s) in FIFO is larger than threshold level. + * | | |1 = Data word(s) in FIFO is equal to or less than threshold level. + * | | |Note: This bit is read only. + * |[19] |TXFULL |Transmit FIFO Full + * | | |This bit reflects data word number in transmit FIFO is 8 + * | | |0 = Not full. + * | | |1 = Full. + * | | |Note: This bit is read only. + * |[20] |TXEMPTY |Transmit FIFO Empty + * | | |This bit reflects data word number in transmit FIFO is 0 + * | | |0 = Not empty. + * | | |1 = Empty. + * | | |Note: This bit is read only. + * |[21] |TXBUSY |Transmit Busy + * | | |This bit is cleared to 0 when all data in transmit FIFO and shift buffer is shifted out. + * | | |And set to 1 when 1st data is load to shift buffer. + * | | |0 = Transmit shift buffer is empty. + * | | |1 = Transmit shift buffer is not empty. + * | | |Note: This bit is read only. + * |[22] |RZCF |Right Channel Zero-Cross Flag + * | | |It indicates the sign bit of right channel sample data is changed or all data bits are 0. + * | | |0 = No zero-cross. + * | | |1 = Right channel zero-cross event is detected. + * | | |Note: Write 1 to clear this bit to 0. + * |[23] |LZCF |Left Channel Zero-Cross Flag + * | | |It indicates the sign bit of left channel sample data is changed or all data bits are 0. + * | | |0 = No zero-cross. + * | | |1 = Left channel zero-cross event is detected. + * | | |Note: Write 1 to clear this bit to 0. + * |[27:24] |RX_LEVEL |Receive FIFO Level + * | | |These bits indicate word number in receive FIFO + * | | |0000 = No data. + * | | |0001 = 1 word in receive FIFO. + * | | |.... + * | | |1000 = 8 words in receive FIFO. + * |[31:28] |TX_LEVEL |Transmit FIFO Level + * | | |These bits indicate word number in transmit FIFO + * | | |0000 = No data. + * | | |0001 = 1 word in transmit FIFO. + * | | |.... + * | | |1000 = 8 words in transmit FIFO. + * @var I2S_T::TXFIFO + * Offset: 0x10 I2S Transmit FIFO Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |TXFIFO |Transmit FIFO Register + * | | |I2S contains 8 words (8x32 bits) data buffer for data transmit. + * | | |Write data to this register to prepare data for transmission. + * | | |The remaining word number is indicated by TX_LEVEL (I2SSTATUS[31:28]). + * @var I2S_T::RXFIFO + * Offset: 0x14 I2S Receive FIFO Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |RXFIFO |Receive FIFO Register + * | | |I2S contains 8 words (8x32 bits) data buffer for data receive. + * | | |Read this register to get data of receive FIFO. + * | | |The remaining data word number is indicated by RX_LEVEL (I2SSTATUS[27:24]). + */ + + __IO uint32_t CON; /* Offset: 0x00 I2S Control Register */ + __IO uint32_t CLKDIV; /* Offset: 0x04 I2S Clock Divider Control Register */ + __IO uint32_t IE; /* Offset: 0x08 I2S Interrupt Enable Register */ + __IO uint32_t STATUS; /* Offset: 0x0C I2S Status Register */ + __O uint32_t TXFIFO; /* Offset: 0x10 I2S Transmit FIFO Register */ + __I uint32_t RXFIFO; /* Offset: 0x14 I2S Receive FIFO Register */ + +} I2S_T; + + + +/** @addtogroup REG_I2S_BITMASK I2S Bit Mask + @{ + */ + +/* I2S I2SCON Bit Field Definitions */ +#define I2S_CON_PCM_Pos 24 /*!< I2S_T::CON: PCM Position */ +#define I2S_CON_PCM_Msk (1ul << I2S_CON_PCM_Pos) /*!< I2S_T::CON: PCM Mask */ + +#define I2S_CON_RXLCH_Pos 23 /*!< I2S_T::CON: RXLCH Position */ +#define I2S_CON_RXLCH_Msk (1ul << I2S_CON_RXLCH_Pos) /*!< I2S_T::CON: RXLCH Mask */ + +#define I2S_CON_RXDMA_Pos 21 /*!< I2S_T::CON: RXDMA Position */ +#define I2S_CON_RXDMA_Msk (1ul << I2S_CON_RXDMA_Pos) /*!< I2S_T::CON: RXDMA Mask */ + +#define I2S_CON_TXDMA_Pos 20 /*!< I2S_T::CON: TXDMA Position */ +#define I2S_CON_TXDMA_Msk (1ul << I2S_CON_TXDMA_Pos) /*!< I2S_T::CON: TXDMA Mask */ + +#define I2S_CON_CLR_RXFIFO_Pos 19 /*!< I2S_T::CON: CLR_RXFIFO Position */ +#define I2S_CON_CLR_RXFIFO_Msk (1ul << I2S_CON_CLR_RXFIFO_Pos) /*!< I2S_T::CON: CLR_RXFIFO Mask */ + +#define I2S_CON_CLR_TXFIFO_Pos 18 /*!< I2S_T::CON: CLR_TXFIFO Position */ +#define I2S_CON_CLR_TXFIFO_Msk (1ul << I2S_CON_CLR_TXFIFO_Pos) /*!< I2S_T::CON: CLR_TXFIFO Mask */ + +#define I2S_CON_LCHZCEN_Pos 17 /*!< I2S_T::CON: LCHZCEN Position */ +#define I2S_CON_LCHZCEN_Msk (1ul << I2S_CON_LCHZCEN_Pos) /*!< I2S_T::CON: LCHZCEN Mask */ + +#define I2S_CON_RCHZCEN_Pos 16 /*!< I2S_T::CON: RCHZCEN Position */ +#define I2S_CON_RCHZCEN_Msk (1ul << I2S_CON_RCHZCEN_Pos) /*!< I2S_T::CON: RCHZCEN Mask */ + +#define I2S_CON_MCLKEN_Pos 15 /*!< I2S_T::CON: MCLKEN Position */ +#define I2S_CON_MCLKEN_Msk (1ul << I2S_CON_MCLKEN_Pos) /*!< I2S_T::CON: MCLKEN Mask */ + +#define I2S_CON_RXTH_Pos 12 /*!< I2S_T::CON: RXTH Position */ +#define I2S_CON_RXTH_Msk (7ul << I2S_CON_RXTH_Pos) /*!< I2S_T::CON: RXTH Mask */ + +#define I2S_CON_TXTH_Pos 9 /*!< I2S_T::CON: TXTH Position */ +#define I2S_CON_TXTH_Msk (7ul << I2S_CON_TXTH_Pos) /*!< I2S_T::CON: TXTH Mask */ + +#define I2S_CON_SLAVE_Pos 8 /*!< I2S_T::CON: SLAVE Position */ +#define I2S_CON_SLAVE_Msk (1ul << I2S_CON_SLAVE_Pos) /*!< I2S_T::CON: SLAVE Mask */ + +#define I2S_CON_FORMAT_Pos 7 /*!< I2S_T::CON: FORMAT Position */ +#define I2S_CON_FORMAT_Msk (1ul << I2S_CON_FORMAT_Pos) /*!< I2S_T::CON: FORMAT Mask */ + +#define I2S_CON_MONO_Pos 6 /*!< I2S_T::CON: MONO Position */ +#define I2S_CON_MONO_Msk (1ul << I2S_CON_MONO_Pos) /*!< I2S_T::CON: MONO Mask */ + +#define I2S_CON_WORDWIDTH_Pos 4 /*!< I2S_T::CON: WORDWIDTH Position */ +#define I2S_CON_WORDWIDTH_Msk (3ul << I2S_CON_WORDWIDTH_Pos) /*!< I2S_T::CON: WORDWIDTH Mask */ + +#define I2S_CON_MUTE_Pos 3 /*!< I2S_T::CON: MUTE Position */ +#define I2S_CON_MUTE_Msk (1ul << I2S_CON_MUTE_Pos) /*!< I2S_T::CON: MUTE Mask */ + +#define I2S_CON_RXEN_Pos 2 /*!< I2S_T::CON: RXEN Position */ +#define I2S_CON_RXEN_Msk (1ul << I2S_CON_RXEN_Pos) /*!< I2S_T::CON: RXEN Mask */ + +#define I2S_CON_TXEN_Pos 1 /*!< I2S_T::CON: TXEN Position */ +#define I2S_CON_TXEN_Msk (1ul << I2S_CON_TXEN_Pos) /*!< I2S_T::CON: TXEN Mask */ + +#define I2S_CON_I2SEN_Pos 0 /*!< I2S_T::CON: I2SEN Position */ +#define I2S_CON_I2SEN_Msk (1ul << I2S_CON_I2SEN_Pos) /*!< I2S_T::CON: I2SEN Mask */ + +/* I2S I2SCLKDIV Bit Field Definitions */ +#define I2S_CLKDIV_BCLK_DIV_Pos 8 /*!< I2S_T::CLKDIV: BCLK_DIV Position */ +#define I2S_CLKDIV_BCLK_DIV_Msk (0xFFul << I2S_CLKDIV_BCLK_DIV_Pos) /*!< I2S_T::CLKDIV: BCLK_DIV Mask */ + +#define I2S_CLKDIV_MCLK_DIV_Pos 0 /*!< I2S_T::CLKDIV: MCLK_DIV Position */ +#define I2S_CLKDIV_MCLK_DIV_Msk (7ul << I2S_CLKDIV_MCLK_DIV_Pos) /*!< I2S_T::CLKDIV: MCLK_DIV Mask */ + +/* I2S I2SIE Bit Field Definitions */ +#define I2S_IE_LZCIE_Pos 12 /*!< I2S_T::IE: LZCIE Position */ +#define I2S_IE_LZCIE_Msk (1ul << I2S_IE_LZCIE_Pos) /*!< I2S_T::IE: LZCIE Mask */ + +#define I2S_IE_RZCIE_Pos 11 /*!< I2S_T::IE: RZCIE Position */ +#define I2S_IE_RZCIE_Msk (1ul << I2S_IE_RZCIE_Pos) /*!< I2S_T::IE: RZCIE Mask */ + +#define I2S_IE_TXTHIE_Pos 10 /*!< I2S_T::IE: TXTHIE Position */ +#define I2S_IE_TXTHIE_Msk (1ul << I2S_IE_TXTHIE_Pos) /*!< I2S_T::IE: TXTHIE Mask */ + +#define I2S_IE_TXOVFIE_Pos 9 /*!< I2S_T::IE: TXOVFIE Position */ +#define I2S_IE_TXOVFIE_Msk (1ul << I2S_IE_TXOVFIE_Pos) /*!< I2S_T::IE: TXOVFIE Mask */ + +#define I2S_IE_TXUDFIE_Pos 8 /*!< I2S_T::IE: TXUDFIE Position */ +#define I2S_IE_TXUDFIE_Msk (1ul << I2S_IE_TXUDFIE_Pos) /*!< I2S_T::IE: TXUDFIE Mask */ + +#define I2S_IE_RXTHIE_Pos 2 /*!< I2S_T::IE: RXTHIE Position */ +#define I2S_IE_RXTHIE_Msk (1ul << I2S_IE_RXTHIE_Pos) /*!< I2S_T::IE: RXTHIE Mask */ + +#define I2S_IE_RXOVFIE_Pos 1 /*!< I2S_T::IE: RXOVFIE Position */ +#define I2S_IE_RXOVFIE_Msk (1ul << I2S_IE_RXOVFIE_Pos) /*!< I2S_T::IE: RXOVFIE Mask */ + +#define I2S_IE_RXUDFIE_Pos 0 /*!< I2S_T::IE: RXUDFIE Position */ +#define I2S_IE_RXUDFIE_Msk (1ul << I2S_IE_RXUDFIE_Pos) /*!< I2S_T::IE: RXUDFIE Mask */ + + +/* I2S I2SSTATUS Bit Field Definitions */ +#define I2S_STATUS_TX_LEVEL_Pos 28 /*!< I2S_T::STATUS: TX_LEVEL Position */ +#define I2S_STATUS_TX_LEVEL_Msk (0xFul << I2S_STATUS_TX_LEVEL_Pos) /*!< I2S_T::STATUS: TX_LEVEL Mask */ + +#define I2S_STATUS_RX_LEVEL_Pos 24 /*!< I2S_T::STATUS: RX_LEVEL Position */ +#define I2S_STATUS_RX_LEVEL_Msk (0xFul << I2S_STATUS_RX_LEVEL_Pos) /*!< I2S_T::STATUS: RX_LEVEL Mask */ + +#define I2S_STATUS_LZCF_Pos 23 /*!< I2S_T::STATUS: LZCF Position */ +#define I2S_STATUS_LZCF_Msk (1ul << I2S_STATUS_LZCF_Pos) /*!< I2S_T::STATUS: LZCF Mask */ + +#define I2S_STATUS_RZCF_Pos 22 /*!< I2S_T::STATUS: RZCF Position */ +#define I2S_STATUS_RZCF_Msk (1ul << I2S_STATUS_RZCF_Pos) /*!< I2S_T::STATUS: RZCF Mask */ + +#define I2S_STATUS_TXBUSY_Pos 21 /*!< I2S_T::STATUS: TXBUSY Position */ +#define I2S_STATUS_TXBUSY_Msk (1ul << I2S_STATUS_TXBUSY_Pos) /*!< I2S_T::STATUS: TXBUSY Mask */ + +#define I2S_STATUS_TXEMPTY_Pos 20 /*!< I2S_T::STATUS: TXEMPTY Position */ +#define I2S_STATUS_TXEMPTY_Msk (1ul << I2S_STATUS_TXEMPTY_Pos) /*!< I2S_T::STATUS: TXEMPTY Mask */ + +#define I2S_STATUS_TXFULL_Pos 19 /*!< I2S_T::STATUS: TXFULL Position */ +#define I2S_STATUS_TXFULL_Msk (1ul << I2S_STATUS_TXFULL_Pos) /*!< I2S_T::STATUS: TXFULL Mask */ + +#define I2S_STATUS_TXTHF_Pos 18 /*!< I2S_T::STATUS: TXTHF Position */ +#define I2S_STATUS_TXTHF_Msk (1ul << I2S_STATUS_TXTHF_Pos) /*!< I2S_T::STATUS: TXTHF Mask */ + +#define I2S_STATUS_TXOVF_Pos 17 /*!< I2S_T::STATUS: TXOVF Position */ +#define I2S_STATUS_TXOVF_Msk (1ul << I2S_STATUS_TXOVF_Pos) /*!< I2S_T::STATUS: TXOVF Mask */ + +#define I2S_STATUS_TXUDF_Pos 16 /*!< I2S_T::STATUS: TXUDF Position */ +#define I2S_STATUS_TXUDF_Msk (1ul << I2S_STATUS_TXUDF_Pos) /*!< I2S_T::STATUS: TXUDF Mask */ + +#define I2S_STATUS_RXEMPTY_Pos 12 /*!< I2S_T::STATUS: RXEMPTY Position */ +#define I2S_STATUS_RXEMPTY_Msk (1ul << I2S_STATUS_RXEMPTY_Pos) /*!< I2S_T::STATUS: RXEMPTY Mask */ + +#define I2S_STATUS_RXFULL_Pos 11 /*!< I2S_T::STATUS: RXFULL Position */ +#define I2S_STATUS_RXFULL_Msk (1ul << I2S_STATUS_RXFULL_Pos) /*!< I2S_T::STATUS: RXFULL Mask */ + +#define I2S_STATUS_RXTHF_Pos 10 /*!< I2S_T::STATUS: RXTHF Position */ +#define I2S_STATUS_RXTHF_Msk (1ul << I2S_STATUS_RXTHF_Pos) /*!< I2S_T::STATUS: RXTHF Mask */ + +#define I2S_STATUS_RXOVF_Pos 9 /*!< I2S_T::STATUS: RXOVF Position */ +#define I2S_STATUS_RXOVF_Msk (1ul << I2S_STATUS_RXOVF_Pos) /*!< I2S_T::STATUS: RXOVF Mask */ + +#define I2S_STATUS_RXUDF_Pos 8 /*!< I2S_T::STATUS: RXUDF Position */ +#define I2S_STATUS_RXUDF_Msk (1ul << I2S_STATUS_RXUDF_Pos) /*!< I2S_T::STATUS: RXUDF Mask */ + +#define I2S_STATUS_RIGHT_Pos 3 /*!< I2S_T::STATUS: RIGHT Position */ +#define I2S_STATUS_RIGHT_Msk (1ul << I2S_STATUS_RIGHT_Pos) /*!< I2S_T::STATUS: RIGHT Mask */ + +#define I2S_STATUS_I2STXINT_Pos 2 /*!< I2S_T::STATUS: I2STXINT Position */ +#define I2S_STATUS_I2STXINT_Msk (1ul << I2S_STATUS_I2STXINT_Pos) /*!< I2S_T::STATUS: I2STXINT Mask */ + +#define I2S_STATUS_I2SRXINT_Pos 1 /*!< I2S_T::STATUS: I2SRXINT Position */ +#define I2S_STATUS_I2SRXINT_Msk (1ul << I2S_STATUS_I2SRXINT_Pos) /*!< I2S_T::STATUS: I2SRXINT Mask */ + +#define I2S_STATUS_I2SINT_Pos 0 /*!< I2S_T::STATUS: I2SINT Position */ +#define I2S_STATUS_I2SINT_Msk (1ul << I2S_STATUS_I2SINT_Pos) /*!< I2S_T::STATUS: I2SINT Mask */ +/*@}*/ /* end of group REG_I2S_BITMASK */ +/*@}*/ /* end of group REG_I2S */ + +/*------------------------------ DMA Controller -----------------------------*/ +/** @addtogroup REG_PDMA Peripheral Direct Memory Access Controller (PDMA) + Memory Mapped Structure for PDMA Controller + @{ + */ + +typedef struct +{ + + +/** + * @var PDMA_T::CSR + * Offset: 0x00 PDMA Channel x Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PDMACEN |PDMA Channel Enable Bit + * | | |Setting this bit to 1 enables PDMA operation. + * | | |If this bit is cleared, PDMA will ignore all PDMA request and force Bus Master into IDLE state. + * |[1] |SW_RST |Software Engine Reset + * | | |0 = No effect. + * | | |1 = Reset the internal state machine, pointers and internal buffer. + * | | |The contents of control register will not be cleared. + * | | |This bit will be automatically cleared after one AHB clock cycle. + * |[3:2] |MODE_SEL |PDMA Mode Selection + * | | |00 = Memory to Memory mode (Memory-to-Memory). + * | | |01 = Peripheral to Memory mode (Peripheral-to-Memory). + * | | |10 = Memory to Peripheral mode (Memory-to-Peripheral). + * |[5:4] |SAD_SEL |Transfer Source Address Direction Selection + * | | |00 = Transfer source address is increasing successively. + * | | |01 = Reserved. + * | | |10 = Transfer source address is fixed (This feature can be used when data where transferred from a single source to multiple destinations). + * | | |11 = Reserved. + * |[7:6] |DAD_SEL |Transfer Destination Address Direction Selection + * | | |00 = Transfer destination address is increasing successively. + * | | |01 = Reserved. + * | | |10 = Transfer destination address is fixed. + * | | |(This feature can be used when data where transferred from multiple sources to a single destination). + * | | |11 = Reserved. + * |[20:19] |APB_TWS |Peripheral Transfer Width Selection + * | | |00 = One word (32-bit) is transferred for every PDMA operation. + * | | |01 = One byte (8-bit) is transferred for every PDMA operation. + * | | |10 = One half-word (16-bit) is transferred for every PDMA operation. + * | | |11 = Reserved. + * | | |Note: This field is meaningful only when MODE_SEL (PDMA_CSRx[3:2]) is Peripheral to Memory mode (Peripheral-to-Memory) or Memory to Peripheral mode (Memory-to-Peripheral). + * |[23] |TRIG_EN |Trigger Enable Bit + * | | |0 = No effect. + * | | |1 = PDMA data read or write transfer Enabled. + * | | |Note1: When PDMA transfer completed, this bit will be cleared automatically. + * | | |Note2: If the bus error occurs, all PDMA transfer will be stopped. + * | | |Software must reset all PDMA channel, and then trigger again. + * @var PDMA_T::SAR + * Offset: 0x04 PDMA Channel x Source Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PDMA_SAR |PDMA Transfer Source Address Register + * | | |This field indicates a 32-bit source address of PDMA. + * | | |Note: The source address must be word alignment. + * @var PDMA_T::DAR + * Offset: 0x08 PDMA Channel x Destination Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PDMA_DAR |PDMA Transfer Destination Address Register + * | | |This field indicates a 32-bit destination address of PDMA. + * | | |Note: The destination address must be word alignment + * @var PDMA_T::BCR + * Offset: 0x0C PDMA Channel x Transfer Byte Count Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |PDMA_BCR |PDMA Transfer Byte Count Register + * | | |This field indicates a 16-bit transfer byte count number of PDMA; it must be word alignment. + * @var PDMA_T::POINT + * Offset: 0x10 PDMA Channel x Internal buffer pointer Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |PDMA_POINT|PDMA Internal Buffer Pointer Register (Read Only) + * | | |This field indicates the internal buffer pointer. + * @var PDMA_T::CSAR + * Offset: 0x14 PDMA Channel x Current Source Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PDMA_CSAR |PDMA Current Source Address Register (Read Only) + * | | |This field indicates the source address where the PDMA transfer just occurred. + * @var PDMA_T::CDAR + * Offset: 0x18 PDMA Channel x Current Destination Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PDMA_CDAR |PDMA Current Destination Address Register (Read Only) + * | | |This field indicates the destination address where the PDMA transfer just occurred. + * @var PDMA_T::CBCR + * Offset: 0x1C PDMA Channel x Current Transfer Byte Count Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |PDMA_CBCR |PDMA Current Byte Count Register (Read Only) + * | | |This field indicates the current remained byte count of PDMA. + * | | |Note: SW_RST will clear this register value. + * @var PDMA_T::IER + * Offset: 0x20 PDMA Channel x Interrupt Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TABORT_IE |PDMA Read/Write Target Abort Interrupt Enable Bit + * | | |0 = Target abort interrupt generation Disabled during PDMA transfer. + * | | |1 = Target abort interrupt generation Enabled during PDMA transfer. + * |[1] |BLKD_IE |PDMA Block Transfer Done Interrupt Enable Bit + * | | |0 = Interrupt generator Disabled when PDMA transfer is done. + * | | |1 = Interrupt generator Enabled when PDMA transfer is done. + * @var PDMA_T::ISR + * Offset: 0x24 PDMA Channel x Interrupt Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TABORT_IF |PDMA Read/Write Target Abort Interrupt Flag + * | | |This bit can be cleared to 0 by software writing '1'. + * | | |0 = No bus ERROR response received. + * | | |1 = Bus ERROR response received. + * | | |Note: This bit filed indicates bus master received ERROR response or not. + * | | |If bus master received ERROR response, it means that target abort is happened. + * | | |PDMA controller will stop transfer and respond this event to software then goes to IDLE state. + * | | |When target abort occurred, software must reset PDMA, and then transfer those data again. + * |[1] |BLKD_IF |PDMA Block Transfer Done Interrupt Flag + * | | |This bit indicates that PDMA has finished all transfers. This bit can be cleared to 0 by software writing '1' + * | | |0 = Not finished. + * | | |1 = Done. + * @var PDMA_T::SBUF + * Offset: 0x80 PDMA Channel x Shared Buffer FIFO x Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PDMA_SBUF0|PDMA Shared Buffer FIFO 0 (Read Only) + * | | |Each channel has its own 1 word internal buffer. + */ + + __IO uint32_t CSR; /* Offset: 0x00 PDMA Channel x Control Register */ + __IO uint32_t SAR; /* Offset: 0x04 PDMA Channel x Source Address Register */ + __IO uint32_t DAR; /* Offset: 0x08 PDMA Channel x Destination Address Register */ + __IO uint32_t BCR; /* Offset: 0x0C PDMA Channel x Transfer Byte Count Register */ + __I uint32_t POINT; /* Offset: 0x10 PDMA Channel x Internal buffer pointer Register */ + __I uint32_t CSAR; /* Offset: 0x14 PDMA Channel x Current Source Address Register */ + __I uint32_t CDAR; /* Offset: 0x18 PDMA Channel x Current Destination Address Register */ + __I uint32_t CBCR; /* Offset: 0x1C PDMA Channel x Current Transfer Byte Count Register */ + __IO uint32_t IER; /* Offset: 0x20 PDMA Channel x Interrupt Enable Register */ + __IO uint32_t ISR; /* Offset: 0x24 PDMA Channel x Interrupt Status Register */ + __I uint32_t RESERVE[22]; + __I uint32_t SBUF; /* Offset: 0x80 PDMA Channel x Shared Buffer FIFO x Register */ + +} PDMA_T; + + + + +typedef struct +{ + + +/** + * @var PDMA_GCR_T::GCRCSR + * Offset: 0x00 PDMA Global Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8] |CLK0_EN |PDMA Controller Channel 0 Clock Enable Control + * | | |0 = CRC controller clock Disabled. + * | | |1 = CRC controller clock Enabled. + * |[9] |CLK1_EN |PDMA Controller Channel 1 Clock Enable Control + * | | |0 = PDMA channel 1 clock Disabled. + * | | |1 = PDMA channel 1 clock Enabled. + * |[10] |CLK2_EN |PDMA Controller Channel 2 Clock Enable Control + * | | |0 = PDMA channel 2 clock Disabled. + * | | |1 = PDMA channel 2 clock Enabled. + * |[11] |CLK3_EN |PDMA Controller Channel 3 Clock Enable Control + * | | |0 = PDMA channel 3 clock Disabled. + * | | |1 = PDMA channel 3 clock Enabled. + * |[12] |CLK4_EN |PDMA Controller Channel 4 Clock Enable Control + * | | |0 = PDMA channel 4 clock Disabled. + * | | |1 = PDMA channel 4 clock Enabled. + * |[13] |CLK5_EN |PDMA Controller Channel 5 Clock Enable Control + * | | |0 = PDMA channel 5 clock Disabled. + * | | |1 = PDMA channel 5 clock Enabled. + * |[24] |CRC_CLK_EN|CRC Controller Clock Enable Control + * | | |0 = CRC controller clock Disabled. + * | | |1 = CRC controller clock Enabled. + * @var PDMA_GCR_T::PDSSR0 + * Offset: 0x04 PDMA Service Selection Control Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |SPI0_RXSEL|PDMA SPI0 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral SPI0 RX. + * | | |Software can change the channel RX setting by SPI0_RXSEL. + * | | |0000: CH0 + * | | |0001: CH1 + * | | |0010: CH2 + * | | |0011: CH3 + * | | |0100: CH4 + * | | |0101: CH5 + * | | |Others : Reserved + * | | |Note: For example, SPI0_RXSEL = 0100, that means SPI0_RX is connected to PDMA_CH4. + * |[7:4] |SPI0_TXSEL|PDMA SPI0 TX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral SPI0 TX. + * | | |Software can configure the TX channel setting by SPI0_TXSEL. + * | | |The channel configuration is the same as SPI0_RXSEL field. + * | | |Please refer to the explanation of SPI0_RXSEL. + * |[11:8] |SPI1_RXSEL|PDMA SPI1 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral SPI1 RX. + * | | |Software can configure the RX channel setting by SPI1_RXSEL. + * | | |The channel configuration is the same as SPI0_RXSEL field. + * | | |Please refer to the explanation of SPI0_RXSEL. + * |[15:12] |SPI1_TXSEL|PDMA SPI1 TX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral SPI1 TX. + * | | |Software can configure the TX channel setting by SPI1_TXSEL. + * | | |The channel configuration is the same as SPI0_RXSEL field. + * | | |Please refer to the explanation of SPI0_RXSEL. + * |[19:16] |SPI2_RXSEL|PDMA SPI2 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral SPI2 RX. + * | | |Software can configure the RX channel setting by SPI2_RXSEL. + * | | |The channel configuration is the same as SPI0_RXSEL field. + * | | |Please refer to the explanation of SPI0_RXSEL. + * |[23:20] |SPI2_TXSEL|PDMA SPI2 TX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral SPI2 TX. + * | | |Software can configure the TX channel setting by SPI2_TXSEL. + * | | |The channel configuration is the same as SPI0_RXSEL field. + * | | |Please refer to the explanation of SPI0_RXSEL. + * @var PDMA_GCR_T::PDSSR1 + * Offset: 0x08 PDMA Service Selection Control Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |UART0_RXSEL|PDMA UART0 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral UART0 RX. + * | | |Software can change the channel RX setting by UART0_RXSEL. + * | | |0000: CH0 + * | | |0001: CH1 + * | | |0010: CH2 + * | | |0011: CH3 + * | | |0100: CH4 + * | | |0101: CH5 + * | | |Others : Reserved + * | | |Note: For example, UART0_RXSEL = 0100, which means UART0_RX is connected to PDMA_CH4. + * |[7:4] |UART0_TXSEL|PDMA UART0 TX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral UART0 TX. + * | | |Software can configure the TX channel setting by UART0_TXSEL. + * | | |The channel configuration is the same as UART0_RXSEL field. + * | | |Please refer to the explanation of UART0_RXSEL. + * |[11:8] |UART1_RXSEL|PDMA UART1 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral UART1 RX. + * | | |Software can configure the RX channel setting by UART1_RXSEL. + * | | |The channel configuration is the same as UART0_RXSEL field. + * | | |Please refer to the explanation of UART0_RXSEL. + * |[15:12] |UART1_TXSEL|PDMA UART1 TX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral UART1 TX. + * | | |Software can configure the TX channel setting by UART1_TXSEL. + * | | |The channel configuration is the same as UART0_RXSEL field. + * | | |Please refer to the explanation of UART0_RXSEL. + * |[27:24] |ADC_RXSEL |PDMA ADC RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral ADC RX. + * | | |Software can configure the RX channel setting by ADC_RXSEL. + * | | |The channel configuration is the same as UART0_RXSEL field. + * | | |Please refer to the explanation of UART0_RXSEL. + * @var PDMA_GCR_T::GCRISR + * Offset: 0x0C PDMA Global Interrupt Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |INTR0 |Interrupt Status Of Channel 0 + * | | |This bit is the interrupt status of PDMA channel 0. + * | | |Note: This bit is read only + * |[1] |INTR1 |Interrupt Status Of Channel 1 + * | | |This bit is the interrupt status of PDMA channel 1. + * | | |Note: This bit is read only + * |[2] |INTR2 |Interrupt Status Of Channel 2 + * | | |This bit is the interrupt status of PDMA channel 2. + * | | |Note: This bit is read only + * |[3] |INTR3 |Interrupt Status Of Channel 3 + * | | |This bit is the interrupt status of PDMA channel 3. + * | | |Note: This bit is read only + * |[4] |INTR4 |Interrupt Status Of Channel 4 + * | | |This bit is the interrupt status of PDMA channel 4. + * | | |Note: This bit is read only + * |[5] |INTR5 |Interrupt Status Of Channel 5 + * | | |This bit is the interrupt status of PDMA channel 5. + * | | |Note: This bit is read only + * |[16] |INTRCRC |Interrupt Status Of CRC Controller + * | | |This bit is the interrupt status of CRC controller + * | | |Note: This bit is read only + * |[31] |INTR |Interrupt Status + * | | |This bit is the interrupt status of PDMA controller. + * | | |Note: This bit is read only + * @var PDMA_GCR_T::PDSSR2 + * Offset: 0x10 PDMA Service Selection Control Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |I2S_RXSEL |PDMA I2S RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral I2S RX. + * | | |Software can change the channel RX setting by I2S_RXSEL. + * | | |0000: CH0 + * | | |0001: CH1 + * | | |0010: CH2 + * | | |0011: CH3 + * | | |0100: CH4 + * | | |0101: CH5 + * | | |Others : Reserved + * | | |Note: For example: I2S_RXSEL (PDMA_PDSSR2[3:0]) = 0100, that means I2S_RX is connected to PDMA_CH4. + * |[7:4] |I2S_TXSEL |PDMA I2S TX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral I2S TX. + * | | |Software can configure the TX channel setting by I2S_TXSEL. + * | | |The channel configuration is the same as I2S_RXSEL field. + * | | |Please refer to the explanation of I2S_RXSEL. + * |[11:8] |PWM0_RXSEL|PDMA PWM0 RX Selection + * | | |This filed defines which PDMA channel is connected to the on-chip peripheral PWM0 RX. + * | | |Software can configure the RX channel setting by PWM0_RXSEL. + * | | |The channel configuration is the same as I2S_RXSEL field. + * | | |Please refer to the explanation of I2S_RXSEL. + * |[15:12] |PWM1_RXSEL|PDMA PWM1 RX Selection + * | | |This filed defines which PDMA channel is connected to the on-chip peripheral PWM1 RX. + * | | |Software can configure the RX channel setting by PWM1_RXSEL. + * | | |The channel configuration is the same as I2S_RXSEL field. + * | | |Please refer to the explanation of I2S_RXSEL. + * |[19:16] |PWM2_RXSEL|PDMA PWM2 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral PWM2 RX. + * | | |Software can configure the RX channel setting by PWM2_RXSEL. + * | | |The channel configuration is the same as I2S_RXSEL field. + * | | |Please refer to the explanation of I2S_RXSEL. + * |[23:20] |PWM3_RXSEL|PDMA PWM3 RX Selection + * | | |This field defines which PDMA channel is connected to the on-chip peripheral PWM3 RX. + * | | |Software can configure the RX channel setting by PWM3_RXSEL. + * | | |The channel configuration is the same as I2S_RXSEL field. + * | | |Please refer to the explanation of I2S_RXSEL. + */ + + __IO uint32_t GCRCSR; /* Offset: 0x00 PDMA Global Control Register */ + __IO uint32_t PDSSR0; /* Offset: 0x04 PDMA Service Selection Control Register 0 */ + __IO uint32_t PDSSR1; /* Offset: 0x08 PDMA Service Selection Control Register 1 */ + __IO uint32_t GCRISR; /* Offset: 0x0C PDMA Global Interrupt Status Register */ + __IO uint32_t PDSSR2; /* Offset: 0x10 PDMA Service Selection Control Register 2 */ + +} PDMA_GCR_T; + + + + +/** @addtogroup REG_PDMA_BITMASK PDMA Bit Mask + @{ + */ + +/* PDMA CSR Bit Field Definitions */ +#define PDMA_CSR_TRIG_EN_Pos 23 /*!< PDMA_T::CSR: TRIG_EN Position */ +#define PDMA_CSR_TRIG_EN_Msk (1ul << PDMA_CSR_TRIG_EN_Pos) /*!< PDMA_T::CSR: TRIG_EN Mask */ + +#define PDMA_CSR_APB_TWS_Pos 19 /*!< PDMA_T::CSR: APB_TWS Position */ +#define PDMA_CSR_APB_TWS_Msk (3ul << PDMA_CSR_APB_TWS_Pos) /*!< PDMA_T::CSR: APB_TWS Mask */ + +#define PDMA_CSR_DAD_SEL_Pos 6 /*!< PDMA_T::CSR: DAD_SEL Position */ +#define PDMA_CSR_DAD_SEL_Msk (3ul << PDMA_CSR_DAD_SEL_Pos) /*!< PDMA_T::CSR: DAD_SEL Mask */ + +#define PDMA_CSR_SAD_SEL_Pos 4 /*!< PDMA_T::CSR: SAD_SEL Position */ +#define PDMA_CSR_SAD_SEL_Msk (3ul << PDMA_CSR_SAD_SEL_Pos) /*!< PDMA_T::CSR: SAD_SEL Mask */ + +#define PDMA_CSR_MODE_SEL_Pos 2 /*!< PDMA_T::CSR: MODE_SEL Position */ +#define PDMA_CSR_MODE_SEL_Msk (3ul << PDMA_CSR_MODE_SEL_Pos) /*!< PDMA_T::CSR: MODE_SEL Mask */ + +#define PDMA_CSR_SW_RST_Pos 1 /*!< PDMA_T::CSR: SW_RST Position */ +#define PDMA_CSR_SW_RST_Msk (1ul << PDMA_CSR_SW_RST_Pos) /*!< PDMA_T::CSR: SW_RST Mask */ + +#define PDMA_CSR_PDMACEN_Pos 0 /*!< PDMA_T::CSR: PDMACEN Position */ +#define PDMA_CSR_PDMACEN_Msk (1ul << PDMA_CSR_PDMACEN_Pos) /*!< PDMA_T::CSR: PDMACEN Mask */ + +/* PDMA BCR Bit Field Definitions */ +#define PDMA_BCR_BCR_Pos 0 /*!< PDMA_T::BCR: BCR Position */ +#define PDMA_BCR_BCR_Msk (0xFFFFul << PDMA_BCR_BCR_Pos) /*!< PDMA_T::BCR: BCR Mask */ + +/* PDMA POINT Bit Field Definitions */ +#define PDMA_POINT_POINT_Pos 0 /*!< PDMA_T::POINT: POINT Position */ +#define PDMA_POINT_POINT_Msk (0xFul << PDMA_POINT_POINT_Pos) /*!< PDMA_T::POINT: POINT Mask */ + +/* PDMA CBCR Bit Field Definitions */ +#define PDMA_CBCR_CBCR_Pos 0 /*!< PDMA_T::CBCR: CBCR Position */ +#define PDMA_CBCR_CBCR_Msk (0xFFFFul << PDMA_CBCR_CBCR_Pos) /*!< PDMA_T::CBCR: CBCR Mask */ + +/* PDMA IER Bit Field Definitions */ +#define PDMA_IER_BLKD_IE_Pos 1 /*!< PDMA_T::IER: BLKD_IE Position */ +#define PDMA_IER_BLKD_IE_Msk (1ul << PDMA_IER_BLKD_IE_Pos) /*!< PDMA_T::IER: BLKD_IE Mask */ + +#define PDMA_IER_TABORT_IE_Pos 0 /*!< PDMA_T::IER: TABORT_IE Position */ +#define PDMA_IER_TABORT_IE_Msk (1ul << PDMA_IER_TABORT_IE_Pos) /*!< PDMA_T::IER: TABORT_IE Mask */ + +/* PDMA ISR Bit Field Definitions */ +#define PDMA_ISR_BLKD_IF_Pos 1 /*!< PDMA_T::ISR: BLKD_IF Position */ +#define PDMA_ISR_BLKD_IF_Msk (1ul << PDMA_ISR_BLKD_IF_Pos) /*!< PDMA_T::ISR: BLKD_IF Mask */ + +#define PDMA_ISR_TABORT_IF_Pos 0 /*!< PDMA_T::ISR: TABORT_IF Position */ +#define PDMA_ISR_TABORT_IF_Msk (1ul << PDMA_ISR_TABORT_IF_Pos) /*!< PDMA_T::ISR: TABORT_IF Mask */ + +/* PDMA GCRCSR Bit Field Definitions */ +#define PDMA_GCRCSR_CRC_CLK_EN_Pos 24 /*!< PDMA_GCR_T::GCRCSR: CRC_CLK_EN Position */ +#define PDMA_GCRCSR_CRC_CLK_EN_Msk (1ul << PDMA_GCRCSR_CRC_CLK_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CRC_CLK_EN Mask */ + +#define PDMA_GCRCSR_CLK5_EN_Pos 13 /*!< PDMA_GCR_T::GCRCSR: CLK5_EN Position */ +#define PDMA_GCRCSR_CLK5_EN_Msk (1ul << PDMA_GCRCSR_CLK5_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CLK5_EN Mask */ + +#define PDMA_GCRCSR_CLK4_EN_Pos 12 /*!< PDMA_GCR_T::GCRCSR: CLK4_EN Position */ +#define PDMA_GCRCSR_CLK4_EN_Msk (1ul << PDMA_GCRCSR_CLK4_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CLK4_EN Mask */ + +#define PDMA_GCRCSR_CLK3_EN_Pos 11 /*!< PDMA_GCR_T::GCRCSR: CLK3_EN Position */ +#define PDMA_GCRCSR_CLK3_EN_Msk (1ul << PDMA_GCRCSR_CLK3_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CLK3_EN Mask */ + +#define PDMA_GCRCSR_CLK2_EN_Pos 10 /*!< PDMA_GCR_T::GCRCSR: CLK2_EN Position */ +#define PDMA_GCRCSR_CLK2_EN_Msk (1ul << PDMA_GCRCSR_CLK2_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CLK2_EN Mask */ + +#define PDMA_GCRCSR_CLK1_EN_Pos 9 /*!< PDMA_GCR_T::GCRCSR: CLK1_EN Position */ +#define PDMA_GCRCSR_CLK1_EN_Msk (1ul << PDMA_GCRCSR_CLK1_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CLK1_EN Mask */ + +#define PDMA_GCRCSR_CLK0_EN_Pos 8 /*!< PDMA_GCR_T::GCRCSR: CLK0_EN Position */ +#define PDMA_GCRCSR_CLK0_EN_Msk (1ul << PDMA_GCRCSR_CLK0_EN_Pos) /*!< PDMA_GCR_T::GCRCSR: CLK0_EN Mask */ + +/* PDMA PDSSR0 Bit Field Definitions */ +#define PDMA_PDSSR0_SPI2_TXSEL_Pos 20 /*!< PDMA_GCR_T::PDSSR0: SPI2_TXSEL Position */ +#define PDMA_PDSSR0_SPI2_TXSEL_Msk (0xFul << PDMA_PDSSR0_SPI2_TXSEL_Pos) /*!< PDMA_GCR_T::PDSSR0: SPI2_TXSEL Mask */ + +#define PDMA_PDSSR0_SPI2_RXSEL_Pos 16 /*!< PDMA_GCR_T::PDSSR0: SPI2_RXSEL Position */ +#define PDMA_PDSSR0_SPI2_RXSEL_Msk (0xFul << PDMA_PDSSR0_SPI2_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR0: SPI2_RXSEL Mask */ + +#define PDMA_PDSSR0_SPI1_TXSEL_Pos 12 /*!< PDMA_GCR_T::PDSSR0: SPI1_TXSEL Position */ +#define PDMA_PDSSR0_SPI1_TXSEL_Msk (0xFul << PDMA_PDSSR0_SPI1_TXSEL_Pos) /*!< PDMA_GCR_T::PDSSR0: SPI1_TXSEL Mask */ + +#define PDMA_PDSSR0_SPI1_RXSEL_Pos 8 /*!< PDMA_GCR_T::PDSSR0: SPI1_RXSEL Position */ +#define PDMA_PDSSR0_SPI1_RXSEL_Msk (0xFul << PDMA_PDSSR0_SPI1_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR0: SPI1_RXSEL Mask */ + +#define PDMA_PDSSR0_SPI0_TXSEL_Pos 4 /*!< PDMA_GCR_T::PDSSR0: SPI0_TXSEL Position */ +#define PDMA_PDSSR0_SPI0_TXSEL_Msk (0xFul << PDMA_PDSSR0_SPI0_TXSEL_Pos) /*!< PDMA_GCR_T::PDSSR0: SPI0_TXSEL Mask */ + +#define PDMA_PDSSR0_SPI0_RXSEL_Pos 0 /*!< PDMA_GCR_T::PDSSR0: SPI0_RXSEL Position */ +#define PDMA_PDSSR0_SPI0_RXSEL_Msk (0xFul << PDMA_PDSSR0_SPI0_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR0: SPI0_RXSEL Mask */ + +/* PDMA PDSSR1 Bit Field Definitions */ +#define PDMA_PDSSR1_ADC_RXSEL_Pos 24 /*!< PDMA_GCR_T::PDSSR1: ADC_RXSEL Position */ +#define PDMA_PDSSR1_ADC_RXSEL_Msk (0xFul << PDMA_PDSSR1_ADC_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR1: ADC_RXSEL Mask */ + +#define PDMA_PDSSR1_UART1_TXSEL_Pos 12 /*!< PDMA_GCR_T::PDSSR1: UART1_TXSEL Position */ +#define PDMA_PDSSR1_UART1_TXSEL_Msk (0xFul << PDMA_PDSSR1_UART1_TXSEL_Pos) /*!< PDMA_GCR_T::PDSSR1: UART1_TXSEL Mask */ + +#define PDMA_PDSSR1_UART1_RXSEL_Pos 8 /*!< PDMA_GCR_T::PDSSR1: UART1_RXSEL Position */ +#define PDMA_PDSSR1_UART1_RXSEL_Msk (0xFul << PDMA_PDSSR1_UART1_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR1: UART1_RXSEL Mask */ + +#define PDMA_PDSSR1_UART0_TXSEL_Pos 4 /*!< PDMA_GCR_T::PDSSR1: UART0_TXSEL Position */ +#define PDMA_PDSSR1_UART0_TXSEL_Msk (0xFul << PDMA_PDSSR1_UART0_TXSEL_Pos) /*!< PDMA_GCR_T::PDSSR1: UART0_TXSEL Mask */ + +#define PDMA_PDSSR1_UART0_RXSEL_Pos 0 /*!< PDMA_GCR_T::PDSSR1: UART0_RXSEL Position */ +#define PDMA_PDSSR1_UART0_RXSEL_Msk (0xFul << PDMA_PDSSR1_UART0_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR1: UART0_RXSEL Mask */ + +/* PDMA GCRISR Bit Field Definitions */ +#define PDMA_GCRISR_INTR_Pos 31 /*!< PDMA_GCR_T::GCRISR: INTR Position */ +#define PDMA_GCRISR_INTR_Msk (1ul << PDMA_GCRISR_INTR_Pos) /*!< PDMA_GCR_T::GCRISR: INTR Mask */ + +#define PDMA_GCRISR_INTRCRC_Pos 16 /*!< PDMA_GCR_T::GCRISR: INTRCRC Position */ +#define PDMA_GCRISR_INTRCRC_Msk (1ul << PDMA_GCRISR_INTRCRC_Pos) /*!< PDMA_GCR_T::GCRISR: INTRCRC Mask */ + +#define PDMA_GCRISR_INTR5_Pos 5 /*!< PDMA_GCR_T::GCRISR: INTR5 Position */ +#define PDMA_GCRISR_INTR5_Msk (1ul << PDMA_GCRISR_INTR5_Pos) /*!< PDMA_GCR_T::GCRISR: INTR5 Mask */ + +#define PDMA_GCRISR_INTR4_Pos 4 /*!< PDMA_GCR_T::GCRISR: INTR4 Position */ +#define PDMA_GCRISR_INTR4_Msk (1ul << PDMA_GCRISR_INTR4_Pos) /*!< PDMA_GCR_T::GCRISR: INTR4 Mask */ + +#define PDMA_GCRISR_INTR3_Pos 3 /*!< PDMA_GCR_T::GCRISR: INTR3 Position */ +#define PDMA_GCRISR_INTR3_Msk (1ul << PDMA_GCRISR_INTR3_Pos) /*!< PDMA_GCR_T::GCRISR: INTR3 Mask */ + +#define PDMA_GCRISR_INTR2_Pos 2 /*!< PDMA_GCR_T::GCRISR: INTR2 Position */ +#define PDMA_GCRISR_INTR2_Msk (1ul << PDMA_GCRISR_INTR2_Pos) /*!< PDMA_GCR_T::GCRISR: INTR2 Mask */ + +#define PDMA_GCRISR_INTR1_Pos 1 /*!< PDMA_GCR_T::GCRISR: INTR1 Position */ +#define PDMA_GCRISR_INTR1_Msk (1ul << PDMA_GCRISR_INTR1_Pos) /*!< PDMA_GCR_T::GCRISR: INTR1 Mask */ + +#define PDMA_GCRISR_INTR0_Pos 0 /*!< PDMA_GCR_T::GCRISR: INTR0 Position */ +#define PDMA_GCRISR_INTR0_Msk (1ul << PDMA_GCRISR_INTR0_Pos) /*!< PDMA_GCR_T::GCRISR: INTR0 Mask */ + +/* PDMA PDSSR2 Bit Field Definitions */ +#define PDMA_PDSSR2_PWM3_RXSEL_Pos 20 /*!< PDMA_GCR_T::PDSSR2: PWM3_RXSEL Position */ +#define PDMA_PDSSR2_PWM3_RXSEL_Msk (0xFul << PDMA_PDSSR2_PWM3_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR2: PWM3_RXSEL Mask */ + +#define PDMA_PDSSR2_PWM2_RXSEL_Pos 16 /*!< PDMA_GCR_T::PDSSR2: PWM2_RXSEL Position */ +#define PDMA_PDSSR2_PWM2_RXSEL_Msk (0xFul << PDMA_PDSSR2_PWM2_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR2: PWM2_RXSEL Mask */ + +#define PDMA_PDSSR2_PWM1_RXSEL_Pos 12 /*!< PDMA_GCR_T::PDSSR2: PWM1_RXSEL Position */ +#define PDMA_PDSSR2_PWM1_RXSEL_Msk (0xFul << PDMA_PDSSR2_PWM1_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR2: PWM1_RXSEL Mask */ + +#define PDMA_PDSSR2_PWM0_RXSEL_Pos 8 /*!< PDMA_GCR_T::PDSSR2: PWM0_RXSEL Position */ +#define PDMA_PDSSR2_PWM0_RXSEL_Msk (0xFul << PDMA_PDSSR2_PWM0_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR2: PWM0_RXSEL Mask */ + +#define PDMA_PDSSR2_I2S_TXSEL_Pos 4 /*!< PDMA_GCR_T::PDSSR2: I2S_TXSEL Position */ +#define PDMA_PDSSR2_I2S_TXSEL_Msk (0xFul << PDMA_PDSSR2_I2S_TXSEL_Pos) /*!< PDMA_GCR_T::PDSSR2: I2S_TXSEL Mask */ + +#define PDMA_PDSSR2_I2S_RXSEL_Pos 0 /*!< PDMA_GCR_T::PDSSR2: I2S_RXSEL Position */ +#define PDMA_PDSSR2_I2S_RXSEL_Msk (0xFul << PDMA_PDSSR2_I2S_RXSEL_Pos) /*!< PDMA_GCR_T::PDSSR2: I2S_RXSEL Mask */ +/*@}*/ /* end of group REG_PDMA_BITMASK */ +/*@}*/ /* end of group REG_PDMA */ + + +/*------------------------------ PS2 Controller ------------------------------*/ +/** @addtogroup REG_PS2 PS2 Serial Interface (PS2) + Memory Mapped Structure for PS2 Serial Interface Controller + @{ + */ + +typedef struct +{ + + +/** + * @var PS2_T::PS2CON + * Offset: 0x00 PS/2 Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PS2EN |Enable PS/2 Device + * | | |Enable PS/2 device controller + * | | |0 = Disabled. + * | | |1 = Enabled. + * |[1] |TXINTEN |Enable Transmit Interrupt + * | | |0 = Data transmit complete interrupt Disabled. + * | | |1 = Data transmit complete interrupt Enabled. + * |[2] |RXINTEN |Enable Receive Interrupt + * | | |0 = Data receive complete interrupt Disabled. + * | | |1 = Data receive complete interrupt Enabled. + * |[6:3] |TXFIFO_DEPTH|Transmit Data FIFO Depth + * | | |There are 16 bytes buffer for data transmit. + * | | |Software can define the FIFO depth from 1 to 16 bytes depends on application needs. + * | | |0 = 1 byte. + * | | |1 = 2 bytes. + * | | |... + * | | |14 = 15 bytes. + * | | |15 = 16 bytes. + * |[7] |ACK |Acknowledge Enable + * | | |0 = Always send acknowledge to host at 12th clock for host to device communication. + * | | |1 = If parity bit error or stop bit is not received correctly, acknowledge bit will not be sent to host at 12th clock. + * |[8] |CLRFIFO |Clear TX FIFO + * | | |Write 1 to this bit to terminate device to host transmission. + * | | |The TXEMPTY(PS2STATUS[7]) bit will be set to 1 and pointer BYTEIDEX(PS2STATUS[11:8]) is reset to 0 regardless there is residue data in buffer or not. + * | | |The buffer content is not been cleared. + * | | |0 = Not active. + * | | |1 = Clear FIFO. + * |[9] |OVERRIDE |Software Override PS/2 CLK/DATA Pin State + * | | |0 = PS2_CLK and PS2_DATA pins are controlled by internal state machine. + * | | |1 = PS2_CLK and PS2_DATA pins are controlled by software. + * |[10] |FPS2CLK |Force PS2CLK Line + * | | |It forces PS2_CLK line high or low regardless of the internal state of the device controller if OVERRIDE(PS2CON[9]) is set to 1. + * | | |0 = Force PS2_CLK line low. + * | | |1 = Force PS2_CLK line high. + * |[11] |FPS2DAT |Force PS2DATA Line + * | | |It forces PS2_DATA high or low regardless of the internal state of the device controller if OVERRIDE (PS2CON[9]) is set to 1. + * | | |0 = Force PS2_DATA low. + * | | |1 = Force PS2_DATA high. + * @var PS2_T::PS2TXDATA0 + * Offset: 0x04 PS/2 Transmit Data Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PS2TXDATAx|Transmit Data + * | | |Writing data to this register starts in device to host communication if bus is in IDLE state. + * | | |Software must enable PS2EN(PS2CON[0]) before writing data to TX buffer. + * @var PS2_T::PS2TXDATA1 + * Offset: 0x08 PS/2 Transmit Data Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PS2TXDATAx|Transmit Data + * | | |Writing data to this register starts in device to host communication if bus is in IDLE state. + * | | |Software must enable PS2EN(PS2CON[0]) before writing data to TX buffer. + * @var PS2_T::PS2TXDATA2 + * Offset: 0x0C PS/2 Transmit Data Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PS2TXDATAx|Transmit Data + * | | |Writing data to this register starts in device to host communication if bus is in IDLE state. + * | | |Software must enable PS2EN(PS2CON[0]) before writing data to TX buffer. + * @var PS2_T::PS2TXDATA3 + * Offset: 0x10 PS/2 Transmit Data Register 3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PS2TXDATAx|Transmit Data + * | | |Writing data to this register starts in device to host communication if bus is in IDLE state. + * | | |Software must enable PS2EN(PS2CON[0]) before writing data to TX buffer. + * @var PS2_T::PS2RXDATA + * Offset: 0x14 PS/2 Receive Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |RXDATA |Received Data + * | | |For host to device communication, after acknowledge bit is sent, the received data is copied from receive shift register to PS2RXDATA register. + * | | |CPU must read this register before next byte reception complete, otherwise the data will be overwritten and RXOVF(PS2STATUS[6]) bit will be set to 1. + * @var PS2_T::PS2STATUS + * Offset: 0x18 PS/2 Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PS2CLK |CLK Pin State + * | | |This bit reflects the status of the PS2_CLK line after synchronizing. + * |[1] |PS2DATA |DATA Pin State + * | | |This bit reflects the status of the PS2_DATA line after synchronizing and sampling. + * |[2] |FRAMERR |Frame Error + * | | |For host to device communication, this bit sets to 1 if STOP bit (logic 1) is not received. + * | | |If frame error occurs, the PS/2_DATA line may keep at low state after 12th clock. + * | | |At this moment, software overrides PS2_CLK to send clock till PS2_DATA release to high state. + * | | |After that, device sends a "Resend" command to host. + * | | |0 = No frame error. + * | | |1 = Frame error occur. + * | | |Write 1 to clear this bit. + * |[3] |RXPARITY |Received Parity + * | | |This bit reflects the parity bit for the last received data byte (odd parity). + * | | |This bit is read only. + * |[4] |RXBUSY |Receive Busy + * | | |This bit indicates that the PS/2 device is currently receiving data. + * | | |0 = Idle. + * | | |1 = Currently receiving data. + * | | |This bit is read only. + * |[5] |TXBUSY |Transmit Busy + * | | |This bit indicates that the PS/2 device is currently sending data. + * | | |0 = Idle. + * | | |1 = Currently sending data. + * | | |This bit is read only. + * |[6] |RXOVF |RX Buffer Overwrite + * | | |0 = No overwrite. + * | | |1 = Data in PS2RXDATA register is overwritten by new received data. + * | | |Write 1 to clear this bit. + * |[7] |TXEMPTY |TX FIFO Empty + * | | |When software writes data to PS2TXDATA0-3, the TXEMPTY bit is cleared to 0 immediately if PS2EN(PS2CON[0]) is enabled. + * | | |When transmitted data byte number is equal to FIFODEPTH(PS2CON[6:3]) then TXEMPTY bit is set to 1. + * | | |0 = There is data to be transmitted. + * | | |1 = FIFO is empty. + * | | |This bit is read only. + * |[11:8] |BYTEIDX |Byte Index + * | | |It indicates which data byte in transmit data shift register. + * | | |When all data in FIFO is transmitted and it will be cleared to 0. + * | | |This bit is read only. + * | | |BYTEIDX, DATA Transmit , BYTEIDX, DATA Transmit + * | | |0000 , PS2TXDATA0[ 7: 0], 1000 , PS2TXDATA2[ 7: 0], + * | | |0001 , PS2TXDATA0[15: 8], 1001 , PS2TXDATA2[15: 8], + * | | |0010 , PS2TXDATA0[23:16], 1010 , PS2TXDATA2[23:16], + * | | |0011 , PS2TXDATA0[31:24], 1011 , PS2TXDATA2[31:24], + * | | |0100 , PS2TXDATA1[ 7: 0], 1100 , PS2TXDATA3[ 7: 0], + * | | |0101 , PS2TXDATA1[15: 8], 1101 , PS2TXDATA3[15: 8], + * | | |0110 , PS2TXDATA1[23:16], 1110 , PS2TXDATA3[23:16], + * | | |0111 , PS2TXDATA1[31:24], 1111 , PS2TXDATA3[31:24], + * @var PS2_T::PS2INTID + * Offset: 0x1C PS/2 Interrupt Identification Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RXINT |Receive Interrupt + * | | |This bit is set to 1 when acknowledge bit is sent for Host to device communication. + * | | |Interrupt occurs if RXINTEN(PS2CON[2]) bit is set to 1. + * | | |0 = No interrupt. + * | | |1 = Receive interrupt occurs. + * | | |Write 1 to clear this bit to 0. + * |[1] |TXINT |Transmit Interrupt + * | | |This bit is set to 1 after STOP bit is transmitted. + * | | |Interrupt occur if TXINTEN(PS2CON[1]) bit is set to 1. + * | | |0 = No interrupt. + * | | |1 = Transmit interrupt occurs. + * | | |Write 1 to clear this bit to 0. + */ + + __IO uint32_t PS2CON; /* Offset: 0x00 PS/2 Control Register */ + __IO uint32_t PS2TXDATA0; /* Offset: 0x04 PS/2 Transmit Data Register 0 */ + __IO uint32_t PS2TXDATA1; /* Offset: 0x08 PS/2 Transmit Data Register 1 */ + __IO uint32_t PS2TXDATA2; /* Offset: 0x0C PS/2 Transmit Data Register 2 */ + __IO uint32_t PS2TXDATA3; /* Offset: 0x10 PS/2 Transmit Data Register 3 */ + __IO uint32_t PS2RXDATA; /* Offset: 0x14 PS/2 Receive Data Register */ + __IO uint32_t PS2STATUS; /* Offset: 0x18 PS/2 Status Register */ + __IO uint32_t PS2INTID; /* Offset: 0x1C PS/2 Interrupt Identification Register */ + +} PS2_T; + + + +/** @addtogroup REG_PS2_BITMASK PS2 Bit Mask + @{ + */ + +/* PS2 PS2CON Bit Field Definitions */ +#define PS2_PS2CON_PS2EN_Pos 0 /*!< PS2_T::PS2CON: PS2EN Position */ +#define PS2_PS2CON_PS2EN_Msk (1ul << PS2_PS2CON_PS2EN_Pos) /*!< PS2_T::PS2CON: PS2EN Mask */ + +#define PS2_PS2CON_TXINTEN_Pos 1 /*!< PS2_T::PS2CON: TXINTEN Position */ +#define PS2_PS2CON_TXINTEN_Msk (1ul << PS2_PS2CON_TXINTEN_Pos) /*!< PS2_T::PS2CON: TXINTEN Mask */ + +#define PS2_PS2CON_RXINTEN_Pos 2 /*!< PS2_T::PS2CON: RXINTEN Position */ +#define PS2_PS2CON_RXINTEN_Msk (1ul << PS2_PS2CON_RXINTEN_Pos) /*!< PS2_T::PS2CON: RXINTEN Mask */ + +#define PS2_PS2CON_TXFIFO_DEPTH_Pos 3 /*!< PS2_T::PS2CON: TXFIFO_DEPTH Position */ +#define PS2_PS2CON_TXFIFO_DEPTH_Msk (0xFul << PS2_PS2CON_TXFIFO_DEPTH_Pos) /*!< PS2_T::PS2CON: TXFIFO_DEPTH Mask */ + +#define PS2_PS2CON_ACK_Pos 7 /*!< PS2_T::PS2CON: ACK Position */ +#define PS2_PS2CON_ACK_Msk (1ul << PS2_PS2CON_ACK_Pos) /*!< PS2_T::PS2CON: ACK Mask */ + +#define PS2_PS2CON_CLRFIFO_Pos 8 /*!< PS2_T::PS2CON: CLRFIFO Position */ +#define PS2_PS2CON_CLRFIFO_Msk (1ul << PS2_PS2CON_CLRFIFO_Pos) /*!< PS2_T::PS2CON: CLRFIFO Mask */ + +#define PS2_PS2CON_OVERRIDE_Pos 9 /*!< PS2_T::PS2CON: OVERRIDE Position */ +#define PS2_PS2CON_OVERRIDE_Msk (1ul << PS2_PS2CON_OVERRIDE_Pos) /*!< PS2_T::PS2CON: OVERRIDE Mask */ + +#define PS2_PS2CON_FPS2CLK_Pos 10 /*!< PS2_T::PS2CON: FPS2CLK Position */ +#define PS2_PS2CON_FPS2CLK_Msk (1ul << PS2_PS2CON_FPS2CLK_Pos) /*!< PS2_T::PS2CON: FPS2CLK Mask */ + +#define PS2_PS2CON_FPS2DAT_Pos 11 /*!< PS2_T::PS2CON: FPS2DAT Position */ +#define PS2_PS2CON_FPS2DAT_Msk (1ul << PS2_PS2CON_FPS2DAT_Pos) /*!< PS2_T::PS2CON: FPS2DAT Mask */ + +/* PS/2 PS2RXDATA Bit Field Definitions */ +#define PS2_PS2RXDATA_RXDATA_Pos 0 /*!< PS2_T::PS2RXDATA: RXDATA Position */ +#define PS2_PS2RXDATA_RXDATA_Msk (0xFFul << PS2_PS2RXDATA_RXDATA_Pos) /*!< PS2_T::PS2RXDATA: RXDATA Mask */ + +/* PS/2 PS2STATUS Bit Field Definitions */ +#define PS2_PS2STATUS_PS2CLK_Pos 0 /*!< PS2_T::PS2STATUS: PS2CLK Position */ +#define PS2_PS2STATUS_PS2CLK_Msk (1ul << PS2_PS2STATUS_PS2CLK_Pos) /*!< PS2_T::PS2STATUS: PS2CLK Mask */ + +#define PS2_PS2STATUS_PS2DATA_Pos 1 /*!< PS2_T::PS2STATUS: PS2DATA Position */ +#define PS2_PS2STATUS_PS2DATA_Msk (1ul << PS2_PS2STATUS_PS2DATA_Pos) /*!< PS2_T::PS2STATUS: PS2DATA Mask */ + +#define PS2_PS2STATUS_FRAMERR_Pos 2 /*!< PS2_T::PS2STATUS: FRAMERR Position */ +#define PS2_PS2STATUS_FRAMERR_Msk (1ul << PS2_PS2STATUS_FRAMERR_Pos) /*!< PS2_T::PS2STATUS: FRAMERR Mask */ + +#define PS2_PS2STATUS_RXPARITY_Pos 3 /*!< PS2_T::PS2STATUS: RXPARITY Position */ +#define PS2_PS2STATUS_RXPARITY_Msk (1ul << PS2_PS2STATUS_RXPARITY_Pos) /*!< PS2_T::PS2STATUS: RXPARITY Mask */ + +#define PS2_PS2STATUS_RXBUSY_Pos 4 /*!< PS2_T::PS2STATUS: RXBUSY Position */ +#define PS2_PS2STATUS_RXBUSY_Msk (1ul << PS2_PS2STATUS_RXBUSY_Pos) /*!< PS2_T::PS2STATUS: RXBUSY Mask */ + +#define PS2_PS2STATUS_TXBUSY_Pos 5 /*!< PS2_T::PS2STATUS: TXBUSY Position */ +#define PS2_PS2STATUS_TXBUSY_Msk (1ul << PS2_PS2STATUS_TXBUSY_Pos) /*!< PS2_T::PS2STATUS: TXBUSY Mask */ + +#define PS2_PS2STATUS_RXOVF_Pos 6 /*!< PS2_T::PS2STATUS: RXOVF Position */ +#define PS2_PS2STATUS_RXOVF_Msk (1ul << PS2_PS2STATUS_RXOVF_Pos) /*!< PS2_T::PS2STATUS: RXOVF Mask */ + +#define PS2_PS2STATUS_TXEMPTY_Pos 7 /*!< PS2_T::PS2STATUS: TXEMPTY Position */ +#define PS2_PS2STATUS_TXEMPTY_Msk (1ul << PS2_PS2STATUS_TXEMPTY_Pos) /*!< PS2_T::PS2STATUS: TXEMPTY Mask */ + +#define PS2_PS2STATUS_BYTEIDX_Pos 8 /*!< PS2_T::PS2STATUS: BYTEIDX Position */ +#define PS2_PS2STATUS_BYTEIDX_Msk (0xFul << PS2_PS2STATUS_BYTEIDX_Pos) /*!< PS2_T::PS2STATUS: BYTEIDX Mask */ + +/* PS/2 PS2INTID Bit Field Definitions */ +#define PS2_PS2INTID_RXINT_Pos 0 /*!< PS2_T::PS2INTID: RXINT Position */ +#define PS2_PS2INTID_RXINT_Msk (1ul << PS2_PS2INTID_RXINT_Pos) /*!< PS2_T::PS2INTID: RXINT Mask */ + +#define PS2_PS2INTID_TXINT_Pos 1 /*!< PS2_T::PS2INTID: TXINT Position */ +#define PS2_PS2INTID_TXINT_Msk (1ul << PS2_PS2INTID_TXINT_Pos) /*!< PS2_T::PS2INTID: TXINT Mask */ +/*@}*/ /* end of group REG_PS2_BITMASK */ +/*@}*/ /* end of group REG_PS2 */ + +/*----------------------------- PWM Controller -------------------------------*/ +/** @addtogroup REG_PWM Pulse Width Modulation Controller (PWM) + Memory Mapped Structure for PWM Generator and Capture Timer + @{ + */ + +typedef struct +{ + + +/** + * @var PWM_T::PPR + * Offset: 0x00 PWM Prescaler Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |CP01 |Clock Prescaler 0 (PWM-Timer 0 / 1 For Group A) + * | | |Clock input is divided by (CP01 + 1) before it is fed to the corresponding PWM-timer + * | | |If CP01=0, then the clock prescaler 0 output clock will be stopped. + * | | |So corresponding PWM-timer will also be stopped. + * |[15:8] |CP23 |Clock Prescaler 2 (PWM-Timer2 / 3 For Group A) + * | | |Clock input is divided by (CP23 + 1) before it is fed to the corresponding PWM-timer + * | | |If CP23=0, then the clock prescaler 2 output clock will be stopped. + * | | |So corresponding PWM-timer will also be stopped. + * |[23:16] |DZI01 |Dead-Zone Interval For Pair Of Channel 0 And Channel 1 (PWM0 And PWM1 Pair For PWM Group A) + * | | |These 8-bit determine the Dead-zone length. + * | | |The unit time of dead-zone length is received from corresponding CSR bits. + * |[31:24] |DZI23 |Dead-Zone Interval For Pair Of Channel2 And Channel3 (PWM2 And PWM3 Pair For PWM Group A) + * | | |These 8-bit determine the Dead-zone length. + * | | |The unit time of dead-zone length is received from corresponding CSR bits. + * @var PWM_T::CSR + * Offset: 0x04 PWM Clock Source Divider Select Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |CSR0 |Timer 0 Clock Source Selection(PWM timer 0 for group A) + * | | |Select clock input for PWM timer, please refer to CSR3. + * |[6:4] |CSR1 |Timer 1 Clock Source Selection(PWM timer 1 for group A) + * | | |Select clock input for PWM timer, please refer to CSR3. + * |[10:8] |CSR2 |Timer 2 Clock Source Selection(PWM timer 2 for group A) + * | | |Select clock input for PWM timer, please refer to CSR3. + * |[14:12] |CSR3 |Timer 3 Clock Source Selection (PWM timer 3 for group A) + * | | |Select clock input for timer. + * | | |000 = Input clock divided by 2. + * | | |001 = Input clock divided by 4. + * | | |010 = Input clock divided by 8. + * | | |011 = Input clock divided by 16. + * | | |100 = Input clock divided by 1. + * @var PWM_T::PCR + * Offset: 0x08 PWM Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CH0EN |PWM-Timer 0 Enable Bit (PWM Timer 0 For Group A) + * | | |0 = Corresponding PWM-Timer running Stopped. + * | | |1 = Corresponding PWM-Timer start run Enabled. + * |[1] |CH0PINV |PWM-Timer 0 Output Polar Inverse Enable Bit (PWM Timer 0 For Group A) + * | | |0 = PWM0 output polar inverse Disabled. + * | | |1 = PWM0 output polar inverse Enabled. + * |[2] |CH0INV |PWM-Timer 0 Output Inverter Enable Bit (PWM Timer 0 For Group A) + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. + * |[3] |CH0MOD |PWM-Timer 0 Auto-Reload/One-Shot Mode (PWM Timer 0 For Group A) + * | | |0 = One-shot mode. + * | | |1 = Auto-reload mode. + * | | |Note: If there is a transition at this bit, it will cause CNR0 and CMR0 be cleared. + * |[4] |DZEN01 |Dead-Zone 0 Generator Enable Bit (PWM0 And PWM1 Pair For PWM Group A) + * | | |0 = Disabled. + * | | |1 = Enabled. + * | | |Note: When Dead-zone generator is enabled, the pair of PWM0 and PWM1 becomes a complementary pair for PWM group A. + * |[5] |DZEN23 |Dead-Zone 2 Generator Enable Bit (PWM2 And PWM3 Pair For PWM Group A) + * | | |0 = Dead-zone 2 generator Disabled. + * | | |1 = Dead-zone 2 generator Enabled. + * | | |Note: When Dead-zone generator is enabled, the pair of PWM2 and PWM3 becomes a complementary pair for PWM group A. + * |[8] |CH1EN |PWM-Timer 1 Enable Bit (PWM Timer 1 For Group A) + * | | |0 = Corresponding PWM-Timer running Stopped. + * | | |1 = Corresponding PWM-Timer start run Enabled. + * |[9] |CH1PINV |PWM-Timer 1 Output Polar Inverse Enable Bit (PWM Timer 1 For Group A) + * | | |0 = PWM1 output polar inverse Disabled. + * | | |1 = PWM1 output polar inverse Enabled. + * |[10] |CH1INV |PWM-Timer 1 Output Inverter Enable Bit (PWM Timer 1 For Group A) + * | | |0 = Inverter Disable. + * | | |1 = Inverter Enable. + * |[11] |CH1MOD |PWM-Timer 1 Auto-Reload/One-Shot Mode (PWM Timer 1 For Group A) + * | | |0 = One-shot mode. + * | | |1 = Auto-reload mode. + * | | |Note: If there is a transition at this bit, it will cause CNR1 and CMR1 be cleared. + * |[16] |CH2EN |PWM-Timer 2 Enable Bit (PWM Timer 2 For Group A) + * | | |0 = Corresponding PWM-Timer running Stopped. + * | | |1 = Corresponding PWM-Timer start run Enabled. + * |[17] |CH2PINV |PWM-Timer 2 Output Polar Inverse Enable Bit (PWM Timer 2 For Group A) + * | | |0 = PWM2 output polar inverse Disabled. + * | | |1 = PWM2 output polar inverse Enabled. + * |[18] |CH2INV |PWM-Timer 2 Output Inverter Enable Bit (PWM Timer 2 For Group A) + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. + * |[19] |CH2MOD |PWM-Timer 2 Auto-Reload/One-Shot Mode (PWM Timer 2 For Group A) + * | | |0 = One-shot mode. + * | | |1 = Auto-reload mode. + * | | |Note: If there is a transition at this bit, it will cause CNR2 and CMR2 be cleared. + * |[24] |CH3EN |PWM-Timer 3 Enable Bit (PWM Timer 3 For Group A) + * | | |0 = Corresponding PWM-Timer running Stopped. + * | | |1 = Corresponding PWM-Timer start run Enabled. + * |[25] |CH3PINV |PWM-Timer 3 Output Polar Inverse Enable (PWM Timer 3 For Group A) + * | | |0 = PWM3 output polar inverse Disable. + * | | |1 = PWM3 output polar inverse Enable. + * |[26] |CH3INV |PWM-Timer 3 Output Inverter Enable Bit (PWM Timer 3 For Group A) + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. + * |[27] |CH3MOD |PWM-Timer 3 Auto-Reload/One-Shot Mode (PWM Timer 3 For Group A) + * | | |0 = One-shot mode. + * | | |1 = Auto-reload mode. + * | | |Note: If there is a transition at this bit, it will cause CNR3 and CMR3 be cleared. + * |[30] |PWM01TYPE |PWM01 Aligned Type Selection Bit (PWM0 And PWM1 Pair For PWM Group A) + * | | |0 = Edge-aligned type. + * | | |1 = Center-aligned type. + * |[31] |PWM23TYPE |PWM23 Aligned Type Selection Bit (PWM2 And PWM3 Pair For PWM Group A) + * | | |0 = Edge-aligned type. + * | | |1 = Center-aligned type. + * @var PWM_T::CNR0 + * Offset: 0x0C PWM Counter Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CNRx |PWM Timer Loaded Value + * | | |CNR determines the PWM period. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CNR will take effect in next PWM cycle. + * | | |Note: When PWM operating at Center-aligned type, CNR value should be set between 0x0000 to 0xFFFE. + * | | |If CNR equal to 0xFFFF, the PWM will work unpredictable. + * | | |Note: When CNR value is set to 0, PWM output is always high. + * @var PWM_T::CMR0 + * Offset: 0x10 PWM Comparator Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CMRx |PWM Comparator Register + * | | |CMR determines the PWM duty. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CMR will take effect in next PWM cycle. + * @var PWM_T::PDR0 + * Offset: 0x14 PWM Data Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |PDRx |PWM Data Register + * | | |User can monitor PDR to know the current value in 16-bit counter. + * @var PWM_T::CNR1 + * Offset: 0x18 PWM Counter Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CNRx |PWM Timer Loaded Value + * | | |CNR determines the PWM period. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CNR will take effect in next PWM cycle. + * | | |Note: When PWM operating at Center-aligned type, CNR value should be set between 0x0000 to 0xFFFE. + * | | |If CNR equal to 0xFFFF, the PWM will work unpredictable. + * | | |Note: When CNR value is set to 0, PWM output is always high. + * @var PWM_T::CMR1 + * Offset: 0x1C PWM Comparator Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CMRx |PWM Comparator Register + * | | |CMR determines the PWM duty. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CMR will take effect in next PWM cycle. + * @var PWM_T::PDR1 + * Offset: 0x20 PWM Data Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |PDRx |PWM Data Register + * | | |User can monitor PDR to know the current value in 16-bit counter. + * @var PWM_T::CNR2 + * Offset: 0x24 PWM Counter Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CNRx |PWM Timer Loaded Value + * | | |CNR determines the PWM period. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CNR will take effect in next PWM cycle. + * | | |Note: When PWM operating at Center-aligned type, CNR value should be set between 0x0000 to 0xFFFE. + * | | |If CNR equal to 0xFFFF, the PWM will work unpredictable. + * | | |Note: When CNR value is set to 0, PWM output is always high. + * @var PWM_T::CMR2 + * Offset: 0x28 PWM Comparator Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CMRx |PWM Comparator Register + * | | |CMR determines the PWM duty. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CMR will take effect in next PWM cycle. + * @var PWM_T::PDR2 + * Offset: 0x2C PWM Data Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |PDRx |PWM Data Register + * | | |User can monitor PDR to know the current value in 16-bit counter. + * @var PWM_T::CNR3 + * Offset: 0x30 PWM Counter Register 3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CNRx |PWM Timer Loaded Value + * | | |CNR determines the PWM period. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CNR will take effect in next PWM cycle. + * | | |Note: When PWM operating at Center-aligned type, CNR value should be set between 0x0000 to 0xFFFE. + * | | |If CNR equal to 0xFFFF, the PWM will work unpredictable. + * | | |Note: When CNR value is set to 0, PWM output is always high. + * @var PWM_T::CMR3 + * Offset: 0x34 PWM Comparator Register 3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CMRx |PWM Comparator Register + * | | |CMR determines the PWM duty. + * | | |PWM frequency = PWMxy_CLK/[(prescale+1)*(clock divider)*(CNR+1)]; where xy, could be 01 or 23, depends on selected PWM channel. + * | | |For Edge-aligned type: + * | | | Duty ratio = (CMR+1)/(CNR+1). + * | | | CMR >= CNR: PWM output is always high. + * | | | CMR < CNR: PWM low width = (CNR-CMR) unit; PWM high width = (CMR+1) unit. + * | | | CMR = 0: PWM low width = (CNR) unit; PWM high width = 1 unit. + * | | |For Center-aligned type: + * | | | Duty ratio = [(2 x CMR) + 1]/[2 x (CNR+1)]. + * | | | CMR > CNR: PWM output is always high. + * | | | CMR <= CNR: PWM low width = 2 x (CNR-CMR) + 1 unit; PWM high width = (2 x CMR) + 1 unit. + * | | | CMR = 0: PWM low width = 2 x CNR + 1 unit; PWM high width = 1 unit. + * | | |(Unit = one PWM clock cycle). + * | | |Note: Any write to CMR will take effect in next PWM cycle. + * @var PWM_T::PDR3 + * Offset: 0x38 PWM Data Register 3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |PDRx |PWM Data Register + * | | |User can monitor PDR to know the current value in 16-bit counter. + * @var PWM_T::PIER + * Offset: 0x40 PWM Interrupt Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PWMIE0 |PWM Channel 0 Period Interrupt Enable Bit + * | | |0 = PWM channel 0 interrupt Disabled. + * | | |1 = PWM channel 0 interrupt Enabled. + * |[1] |PWMIE1 |PWM Channel 1 Period Interrupt Enable Bit + * | | |0 = PWM channel 1 interrupt Disabled. + * | | |1 = PWM channel 1 interrupt Enabled. + * |[2] |PWMIE2 |PWM Channel 2 Period Interrupt Enable Bit + * | | |0 = PWM channel 2 interrupt Disabled. + * | | |1 = PWM channel 2 interrupt Enabled. + * |[3] |PWMIE3 |PWM Channel 3 Period Interrupt Enable Bit + * | | |0 = PWM channel 3 interrupt Disabled. + * | | |1 = PWM channel 3 interrupt Enabled. + * |[8] |PWMDIE0 |PWM Channel 0 Duty Interrupt Enable Bit + * | | |0 = PWM channel 0 duty interrupt Disabled. + * | | |1 = PWM channel 0 duty interrupt Enabled. + * |[9] |PWMDIE1 |PWM Channel 1 Duty Interrupt Enable Bit + * | | |0 = PWM channel 1 duty interrupt Disabled. + * | | |1 = PWM channel 1 duty interrupt Enabled. + * |[10] |PWMDIE2 |PWM Channel 2 Duty Interrupt Enable Bit + * | | |0 = PWM channel 2 duty interrupt Disabled. + * | | |1 = PWM channel 2 duty interrupt Enabled. + * |[11] |PWMDIE3 |PWM Channel 3 Duty Interrupt Enable Bit + * | | |0 = PWM channel 3 duty interrupt Disabled. + * | | |1 = PWM channel 3 duty interrupt Enabled. + * |[16] |INT01TYPE |PWM01 Interrupt Period Type Selection Bit (PWM0 And PWM1 Pair For PWM Group A) + * | | |0 = PWMIFn will be set if PWM counter underflow. + * | | |1 = PWMIFn will be set if PWM counter matches CNRn register. + * | | |Note: This bit is effective when PWM in Center-aligned type only. + * |[17] |INT23TYPE |PWM23 Interrupt Period Type Selection Bit (PWM2 And PWM3 Pair For PWM Group A) + * | | |0 = PWMIFn will be set if PWM counter underflow. + * | | |1 = PWMIFn will be set if PWM counter matches CNRn register. + * | | |Note: This bit is effective when PWM in Center-aligned type only. + * @var PWM_T::PIIR + * Offset: 0x44 PWM Interrupt Indication Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PWMIF0 |PWM Channel 0 Period Interrupt Status + * | | |This bit is set by hardware when PWM0 counter reaches the requirement of interrupt (depend on INT01TYPE bit of PIER register) + * | | |if PWM0 interrupt enable bit (PWMIE0) is 1, This bit can be cleared to 0 by software writing '1'. + * |[1] |PWMIF1 |PWM Channel 1 Period Interrupt Status + * | | |This bit is set by hardware when PWM1 counter reaches the requirement of interrupt (depend on INT01TYPE bit of PIER register) + * | | |if PWM1 interrupt enable bit (PWMIE1) is 1, This bit can be cleared to 0 by software writing '1'. + * |[2] |PWMIF2 |PWM Channel 2 Period Interrupt Status + * | | |This bit is set by hardware when PWM2 counter reaches the requirement of interrupt (depend on INT23TYPE bit of PIER register) + * | | |if PWM2 interrupt enable bit (PWMIE2) is 1, This bit can be cleared to 0 by software writing '1'. + * |[3] |PWMIF3 |PWM Channel 3 Period Interrupt Status + * | | |This bit is set by hardware when PWM3 counter reaches the requirement of interrupt (depend on INT23TYPE bit of PIER register) + * | | |if PWM3 interrupt enable bit (PWMIE3) is 1, This bit can be cleared to 0 by software writing '1'. + * |[8] |PWMDIF0 |PWM Channel 0 Duty Interrupt Flag + * | | |Flag is set by hardware when channel 0 PWM counter down count and reaches CMR0, software can clear this bit by writing a one to it. + * | | |Note: If CMR equal to CNR, this flag is not working. + * |[9] |PWMDIF1 |PWM Channel 1 Duty Interrupt Flag + * | | |Flag is set by hardware when channel 1 PWM counter down count and reaches CMR1, software can clear this bit by writing a one to it. + * | | |Note: If CMR equal to CNR, this flag is not working. + * |[10] |PWMDIF2 |PWM Channel 2 Duty Interrupt Flag + * | | |Flag is set by hardware when channel 2 PWM counter down count and reaches CMR2, software can clear this bit by writing a one to it. + * | | |Note: If CMR equal to CNR, this flag is not working. + * |[11] |PWMDIF3 |PWM Channel 3 Duty Interrupt Flag + * | | |Flag is set by hardware when channel 3 PWM counter down count and reaches CMR3, software can clear this bit by writing a one to it. + * | | |Note: If CMR equal to CNR, this flag is not working. + * @var PWM_T::CCR0 + * Offset: 0x50 PWM Capture Control Register 0 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |INV0 |Channel 0 Inverter Enable Bit + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. Reverse the input signal from GPIO before fed to Capture timer + * |[1] |CRL_IE0 |Channel 0 Rising Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 0 has rising transition, Capture will issue an Interrupt. + * | | |0 = Rising latch interrupt Disabled. + * | | |1 = Rising latch interrupt Enabled. + * |[2] |CFL_IE0 |Channel 0 Falling Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 0 has falling transition, Capture will issue an Interrupt. + * | | |0 = Falling latch interrupt Disabled. + * | | |1 = Falling latch interrupt Enabled. + * |[3] |CAPCH0EN |Channel 0 Capture Function Enable Bit + * | | |When Enabled, Capture latched the PWM-counter value and saved to CRLR (Rising latch) and CFLR (Falling latch). + * | | |When Disabled, Capture does not update CRLR and CFLR, and disable PWM group channel 0 Interrupt. + * | | |0 = Capture function on PWM group channel 0 Disabled. + * | | |1 = Capture function on PWM group channel 0 Enabled. + * |[4] |CAPIF0 |Channel 0 Capture Interrupt Indication Flag + * | | |If PWM group channel 0 rising latch interrupt is enabled (CRL_IE0 = 1), a rising transition + * | | |occurs at PWM group channel 0 will result in CAPIF0 to high; Similarly, a falling transition + * | | |will cause CAPIF0 to be set high if PWM group channel 0 falling latch interrupt is enabled (CFL_IE0 = 1). + * | | |This bit can be cleared to 0 by software writing '1'. + * |[6] |CRLRI0 |CRLR0 Latched Indicator Bit + * | | |When PWM group input channel 0 has a rising transition, CRLR0 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[7] |CFLRI0 |CFLR0 Latched Indicator Bit + * | | |When PWM group input channel 0 has a falling transition, CFLR0 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[16] |INV1 |Channel 1 Inverter Enable Bit + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. Reverse the input signal from GPIO before fed to Capture timer + * |[17] |CRL_IE1 |Channel 1 Rising Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 1 has rising transition, Capture will issue an Interrupt. + * | | |0 = Rising latch interrupt Disabled. + * | | |1 = Rising latch interrupt Enabled. + * |[18] |CFL_IE1 |Channel 1 Falling Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 1 has falling transition, Capture will issue an Interrupt. + * | | |0 = Falling latch interrupt Disabled. + * | | |1 = Falling latch interrupt Enabled. + * |[19] |CAPCH1EN |Channel 1 Capture Function Enable Bit + * | | |When Enabled, Capture latched the PWM-counter and saved to CRLR (Rising latch) and CFLR (Falling latch). + * | | |When Disabled, Capture does not update CRLR and CFLR, and disable PWM group channel 1 Interrupt. + * | | |0 = Capture function on PWM group channel 1 Disabled. + * | | |1 = Capture function on PWM group channel 1 Enabled. + * |[20] |CAPIF1 |Channel 1 Capture Interrupt Indication Flag + * | | |If PWM group channel 1 rising latch interrupt is enabled (CRL_IE1 = 1), a rising transition + * | | |occurs at PWM group channel 1 will result in CAPIF1 to high; Similarly, a falling transition + * | | |will cause CAPIF1 to be set high if PWM group channel 1 falling latch interrupt is enabled (CFL_IE1 = 1). + * | | |This bit can be cleared to 0 by software writing '1'. + * |[22] |CRLRI1 |CRLR1 Latched Indicator Bit + * | | |When PWM group input channel 1 has a rising transition, CRLR1 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[23] |CFLRI1 |CFLR1 Latched Indicator Bit + * | | |When PWM group input channel 1 has a falling transition, CFLR1 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * @var PWM_T::CCR2 + * Offset: 0x54 PWM Capture Control Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |INV2 |Channel 2 Inverter Enable Bit + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. Reverse the input signal from GPIO before fed to Capture timer + * |[1] |CRL_IE2 |Channel 2 Rising Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 2 has rising transition, Capture will issue an Interrupt. + * | | |0 = Rising latch interrupt Disabled. + * | | |1 = Rising latch interrupt Enabled. + * |[2] |CFL_IE2 |Channel 2 Falling Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 2 has falling transition, Capture will issue an Interrupt. + * | | |0 = Falling latch interrupt Disabled. + * | | |1 = Falling latch interrupt Enabled. + * |[3] |CAPCH2EN |Channel 2 Capture Function Enable Bit + * | | |When Enabled, Capture latched the PWM-counter value and saved to CRLR (Rising latch) and CFLR (Falling latch). + * | | |When Disabled, Capture does not update CRLR and CFLR, and disable PWM group channel 2 Interrupt. + * | | |0 = Capture function on PWM group channel 2 Disabled. + * | | |1 = Capture function on PWM group channel 2 Enabled. + * |[4] |CAPIF2 |Channel 2 Capture Interrupt Indication Flag + * | | |If PWM group channel 2 rising latch interrupt is enabled (CRL_IE2=1), a rising transition + * | | |occurs at PWM group channel 2 will result in CAPIF2 to high; Similarly, a falling transition + * | | |will cause CAPIF2 to be set high if PWM group channel 2 falling latch interrupt is enabled (CFL_IE2=1). + * | | |This bit can be cleared to 0 by software writing '1'. + * |[6] |CRLRI2 |CRLR2 Latched Indicator Bit + * | | |When PWM group input channel 2 has a rising transition, CRLR2 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[7] |CFLRI2 |CFLR2 Latched Indicator Bit + * | | |When PWM group input channel 2 has a falling transition, CFLR2 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[16] |INV3 |Channel 3 Inverter Enable Bit + * | | |0 = Inverter Disabled. + * | | |1 = Inverter Enabled. Reverse the input signal from GPIO before fed to Capture timer + * |[17] |CRL_IE3 |Channel 3 Rising Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 3 has rising transition, Capture will issue an Interrupt. + * | | |0 = Rising latch interrupt Disabled. + * | | |1 = Rising latch interrupt Enabled. + * |[18] |CFL_IE3 |Channel 3 Falling Latch Interrupt Enable Bit + * | | |When Enabled, if Capture detects PWM group channel 3 has falling transition, Capture will issue an Interrupt. + * | | |0 = Falling latch interrupt Disabled. + * | | |1 = Falling latch interrupt Enabled. + * |[19] |CAPCH3EN |Channel 3 Capture Function Enable Bit + * | | |When Enabled, Capture latched the PWM-counter and saved to CRLR (Rising latch) and CFLR (Falling latch). + * | | |When Disabled, Capture does not update CRLR and CFLR, and disable PWM group channel 3 Interrupt. + * | | |0 = Capture function on PWM group channel 3 Disabled. + * | | |1 = Capture function on PWM group channel 3 Enabled. + * |[20] |CAPIF3 |Channel 3 Capture Interrupt Indication Flag + * | | |If PWM group channel 3 rising latch interrupt is enabled (CRL_IE3=1), a rising transition + * | | |occurs at PWM group channel 3 will result in CAPIF3 to high; Similarly, a falling transition + * | | |will cause CAPIF3 to be set high if PWM group channel 3 falling latch interrupt is enabled (CFL_IE3=1). + * | | |This bit can be cleared to 0 by software writing '1'. + * |[22] |CRLRI3 |CRLR3 Latched Indicator Bit + * | | |When PWM group input channel 3 has a rising transition, CRLR3 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[23] |CFLRI3 |CFLR3 Latched Indicator Bit + * | | |When PWM group input channel 3 has a falling transition, CFLR3 was latched with the value of PWM down-counter and this bit is set by hardware. + * | | |This bit can be cleared to 0 by software writing '1'. + * @var PWM_T::CRLR0 + * Offset: 0x58 PWM Capture Rising Latch Register (Channel 0) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CRLRx |Capture Rising Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has rising transition. + * @var PWM_T::CFLR0 + * Offset: 0x5C PWM Capture Falling Latch Register (Channel 0) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CFLRx |Capture Falling Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has falling transition. + * @var PWM_T::CRLR1 + * Offset: 0x60 PWM Capture Rising Latch Register (Channel 1) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CRLRx |Capture Rising Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has rising transition. + * @var PWM_T::CFLR1 + * Offset: 0x64 PWM Capture Falling Latch Register (Channel 1) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CFLRx |Capture Falling Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has falling transition. + * @var PWM_T::CRLR2 + * Offset: 0x68 PWM Capture Rising Latch Register (Channel 2) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CRLRx |Capture Rising Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has rising transition. + * @var PWM_T::CFLR2 + * Offset: 0x6C PWM Capture Falling Latch Register (Channel 2) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CFLRx |Capture Falling Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has falling transition. + * @var PWM_T::CRLR3 + * Offset: 0x70 PWM Capture Rising Latch Register (Channel 3) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CRLRx |Capture Rising Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has rising transition. + * @var PWM_T::CFLR3 + * Offset: 0x74 PWM Capture Falling Latch Register (Channel 3) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CFLRx |Capture Falling Latch Register + * | | |Latch the PWM counter when Channel 0/1/2/3 has falling transition. + * @var PWM_T::CAPENR + * Offset: 0x78 PWM Capture Input 0~3 Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |CAPENR |Capture Input Enable Register + * | | |There are four capture inputs from pad. Bit0~Bit3 are used to control each input enable or disable. + * | | |If enabled, PWMn multi-function pin input will affect its input capture function; + * | | |If Disabled, PWMn multi-function pin input does not affect input capture function. + * | | |xxx1 = Capture channel 0 is from pin PA.12. + * | | |xx1x = Capture channel 1 is from pin PA.13. + * | | |x1xx = Capture channel 2 is from pin PA.14. + * | | |1xxx = Capture channel 3 is from pin PA.15. + * @var PWM_T::POE + * Offset: 0x7C PWM Output Enable for Channel 0~3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PWM0 |Channel 0 Output Enable Register + * | | |0 = PWM channel 0 output to pin Disabled. + * | | |1 = PWM channel 0 output to pin Enabled. + * | | |Note: The corresponding GPIO pin must also be switched to PWM function + * |[1] |PWM1 |Channel 1 Output Enable Register + * | | |0 = PWM channel 1 output to pin Disabled. + * | | |1 = PWM channel 1 output to pin Enabled. + * | | |Note: The corresponding GPIO pin must also be switched to PWM function + * |[2] |PWM2 |Channel 2 Output Enable Register + * | | |0 = PWM channel 2 output to pin Disabled. + * | | |1 = PWM channel 2 output to pin Enabled. + * | | |Note: The corresponding GPIO pin must also be switched to PWM function + * |[3] |PWM3 |Channel 3 Output Enable Register + * | | |0 = Disable PWM channel 3 output to pin. + * | | |1 = Enable PWM channel 3 output to pin. + * | | |Note: The corresponding GPIO pin must also be switched to PWM function + * @var PWM_T::TCON + * Offset: 0x80 PWM Trigger Control for Channel 0~3 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PWM0TEN |Channel 0 Center-Aligned Trigger Enable Register + * | | |PWM can trigger ADC to start conversion when PWM counter up count to CNR if this bit is set to 1. + * | | |0 = PWM channel 0 trigger ADC function Disabled. + * | | |1 = PWM channel 0 trigger ADC function Enabled. + * | | |Note: This function is only supported when PWM operating at Center-aligned type. + * |[1] |PWM1TEN |Channel 1 Center-Aligned Trigger Enable Register + * | | |PWM can trigger ADC to start conversion when PWM counter up count to CNR if this bit is set to 1. + * | | |0 = PWM channel 1 trigger ADC function Disabled. + * | | |1 = PWM channel 1 trigger ADC function Enabled. + * | | |Note: This function is only supported when PWM operating at Center-aligned type. + * |[2] |PWM2TEN |Channel 2 Center-Aligned Trigger Enable Register + * | | |PWM can trigger ADC to start conversion when PWM counter up count to CNR if this bit is set to 1. + * | | |0 = PWM channel 2 trigger ADC function Disabled. + * | | |1 = PWM channel 2 trigger ADC function Enabled. + * | | |Note: This function is only supported when PWM operating at Center-aligned type. + * |[3] |PWM3TEN |Channel 3 Center-Aligned Trigger Enable Register + * | | |PWM can trigger ADC to start conversion when PWM counter up count to CNR if this bit is set to 1. + * | | |0 = PWM channel 3 trigger ADC function Disabled. + * | | |1 = PWM channel 3 trigger ADC function Enabled. + * | | |Note: This function is only supported when PWM operating at Center-aligned type. + * @var PWM_T::TSTATUS + * Offset: 0x84 PWM Trigger Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PWM0TF |Channel 0 Center-Aligned Trigger Flag + * | | |For Center-aligned Operating mode, this bit is set to 1 by hardware when PWM counter up counts to CNR if PWM0TEN bit is set to 1. + * | | |After this bit is set to 1, ADC will start conversion if ADC triggered source is selected by PWM. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[1] |PWM1TF |Channel 1 Center-Aligned Trigger Flag + * | | |For Center-aligned Operating mode, this bit is set to 1 by hardware when PWM counter up count to CNR if PWM1TEN bit is set to 1. + * | | |After this bit is set to 1, ADC will start conversion if ADC triggered source is selected by PWM. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[2] |PWM2TF |Channel 2 Center-Aligned Trigger Flag + * | | |For Center-aligned Operating mode, this bit is set to 1 by hardware when PWM counter up count to CNR if PWM2TEN bit is set to 1. + * | | |After this bit is set to 1, ADC will start conversion if ADC triggered source is selected by PWM. + * | | |This bit can be cleared to 0 by software writing '1'. + * |[3] |PWM3TF |Channel 3 Center-Aligned Trigger Flag + * | | |For Center-aligned Operating mode, this bit is set to 1 by hardware when PWM counter up count to CNR if PWM3TEN bit is set to 1. + * | | |After this bit is set to 1, ADC will start conversion if ADC triggered source is selected by PWM. + * | | |This bit can be cleared to 0 by software writing '1'. + * @var PWM_T::SYNCBUSY0 + * Offset: 0x88 PWM0 Synchronous Busy Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |S_BUSY |PWM Synchronous Busy + * | | |When software writes CNR0/CMR0/PPR or switches PWM0 operation mode (PCR[3]), + * | | |PWM will have a busy time to update these values completely because + * | | |PWM clock may be different from system clock domain. + * | | |Software needs to check this busy status before writing CNR0/CMR0/PPR or + * | | |switching PWM0 operation mode (PCR[3]) to make sure previous setting has been updated completely. + * | | |This bit will be set when software writes CNR0/CMR0/PPR or switches PWM0 operation mode (PCR[3]) + * | | |and will be cleared by hardware automatically when PWM update these value completely. + * @var PWM_T::SYNCBUSY1 + * Offset: 0x8C PWM1 Synchronous Busy Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |S_BUSY |PWM Synchronous Busy + * | | |When Software writes CNR1/CMR1/PPR or switches PWM1 operation mode (PCR[11]), + * | | |PWM will have a busy time to update these values completely because + * | | |PWM clock may be different from system clock domain. + * | | |Software needs to check this busy status before writing CNR1/CMR1/PPR or + * | | |switching PWM1 operation mode (PCR[11]) to make sure previous setting has been updated completely. + * | | |This bit will be set when software writes CNR1/CMR1/PPR or switches PWM1 operation mode (PCR[11]) + * | | |and will be cleared by hardware automatically when PWM update these value completely. + * @var PWM_T::SYNCBUSY2 + * Offset: 0x90 PWM2 Synchronous Busy Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |S_BUSY |PWM Synchronous Busy + * | | |When Software writes CNR2/CMR2/PPR or switch PWM2 operation mode (PCR[19]), + * | | |PWM will have a busy time to update these values completely because + * | | |PWM clock may be different from system clock domain. + * | | |Software needs to check this busy status before writing CNR2/CMR2/PPR or + * | | |switching PWM2 operation mode (PCR[19]) to make sure previous setting has been updated completely. + * | | |This bit will be set when software writes CNR2/CMR2/PPR or switch PWM2 operation mode (PCR[19]) + * | | |and will be cleared by hardware automatically when PWM update these value completely. + * @var PWM_T::SYNCBUSY3 + * Offset: 0x94 PWM3 Synchronous Busy Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |S_BUSY |PWM Synchronous Busy + * | | |When Software writes CNR3/CMR3/PPR or switch PWM3 operation mode (PCR[27]), + * | | |PWM will have a busy time to update these values completely because + * | | |PWM clock may be different from system clock domain. + * | | |Software need to check this busy status before writing CNR3/CMR3/PPR or + * | | |switching PWM3 operation mode (PCR[27]) to make sure previous setting has been updated completely. + * | | |This bit will be set when Software writes CNR3/CMR3/PPR or switch PWM3 operation mode (PCR[27]) + * | | |and will be cleared by hardware automatically when PWM update these value completely. + * @var PWM_T::CAPPDMACTL + * Offset: 0xC0 PWM Group A Trigger Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CAP0PDMAEN |Channel 0 PDMA Enable Bit + * | | |0 = Channel 0 PDMA function Disabled. + * | | |1 = Channel 0 PDMA function Enabled for the channel 0 captured data and transfer to memory. + * |[2:1] |CAP0PDMAMOD |Select CRLR0 or CFLR0 to Transfer PDMA + * | | |00 = Reserved + * | | |01 = CRLR0 + * | | |10 = CFLR0 + * | | |11 = Both CRLR0 and CFLR0 + * |[3] |CAP0RFORDER |Capture channel 0 Rising/Falling Order + * | | |Set this bit to determine whether the CRLR0 or CFLR0 is the first captured data transferred to memory through PDMA when CAP0PDMAMOD =11 + * | | |0 = CFLR0 is the first captured data to memory. + * | | |1 = CRLR0 is the first captured data to memory. + * |[8] |CAP1PDMAEN |Channel 1 PDMA Enable Bit + * | | |0 = Channel 1 PDMA function Disabled. + * | | |1 = Channel 1 PDMA function Enabled for the channel 1 captured data and transfer to memory. + * |[10:9] |CAP1PDMAMOD |Select CRLR1 or CFLR1 to Transfer PDMA + * | | |00 = Reserved + * | | |01 = CRLR1 + * | | |10 = CFLR1 + * | | |11 = Both CRLR1 and CFLR1 + * |[11] |CAP1RFORDER |Capture channel 1 Rising/Falling Order + * | | |Set this bit to determine whether the CRLR1 or CFLR1 is the first captured data transferred to memory through PDMA when CAP1PDMAMOD =11 + * | | |0 = CFLR1 is the first captured data to memory. + * | | |1 = CRLR1 is the first captured data to memory. + * |[16] |CAP2PDMAEN |Channel 2 PDMA Enable Bit + * | | |0 = Channel 2 PDMA function Disabled. + * | | |1 = Channel 2 PDMA function Enabled for the channel 2 captured data and transfer to memory. + * |[18:17] |CAP2PDMAMOD |Select CRLR2 or CFLR2 to Transfer PDMA + * | | |00 = Reserved + * | | |01 = CRLR2 + * | | |10 = CFLR2 + * | | |11 = Both CRLR2 and CFLR2 + * |[19] |CAP2RFORDER |Capture channel 2 Rising/Falling Order + * | | |Set this bit to determine whether the CRLR2 or CFLR2 is the first captured data transferred to memory through PDMA when CAP2PDMAMOD =11 + * | | |0 = CFLR2 is the first captured data to memory. + * | | |1 = CRLR2 is the first captured data to memory. + * |[24] |CAP3PDMAEN |Channel 3 PDMA Enable Bit + * | | |0 = Channel 3 PDMA function Disabled. + * | | |1 = Channel 3 PDMA function Enabled for the channel 3 captured data and transfer to memory. + * |[26:25] |CAP3PDMAMOD |Select CRLR3 or CFLR3 to Transfer PDMA + * | | |00 = Reserved + * | | |01 = CRLR3 + * | | |10 = CFLR3 + * | | |11 = Both CRLR3 and CFLR3 + * |[27] |CAP3RFORDER |Capture channel 3 Rising/Falling Order + * | | |Set this bit to determine whether the CRLR1 or CFLR3 is the first captured data transferred to memory through PDMA when CAP3PDMAMOD =11 + * | | |0 = CFLR3 is the first captured data to memory. + * | | |1 = CRLR3 is the first captured data to memory. + * @var PWM_T::CAP0PDMA + * Offset: 0xC4 PWM Group A PDMA channel 0 DATA Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CAP0RFPDMA|PDMA data register for channel 0 + * | | |it is the capturing value(CFLR0/CRLR0) for channel 0. + * @var PWM_T::CAP1PDMA + * Offset: 0xC8 PWM Group A PDMA channel 1 DATA Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CAP1RFPDMA|PDMA data register for channel 1 + * | | |it is the capturing value(CFLR1/CRLR1) for channel 1. + * @var PWM_T::CAP2PDMA + * Offset: 0xCC PWM Group A PDMA channel 2 DATA Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CAP2RFPDMA|PDMA data register for channel 2 + * | | |it is the capturing value(CFLR2/CRLR2) for channel 2. + * @var PWM_T::CAP3PDMA + * Offset: 0xD0 PWM Group A PDMA channel 3 DATA Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |CAP3RFPDMA|PDMA data register for channel 3 + * | | |it is the capturing value(CFLR3/CRLR3) for channel 3. + */ + + __IO uint32_t PPR; /* Offset: 0x00 PWM Prescaler Register */ + __IO uint32_t CSR; /* Offset: 0x04 PWM Clock Source Divider Select Register */ + __IO uint32_t PCR; /* Offset: 0x08 PWM Control Register */ + __IO uint32_t CNR0; /* Offset: 0x0C PWM Counter Register 0 */ + __IO uint32_t CMR0; /* Offset: 0x10 PWM Comparator Register 0 */ + __I uint32_t PDR0; /* Offset: 0x14 PWM Data Register 0 */ + __IO uint32_t CNR1; /* Offset: 0x18 PWM Counter Register 1 */ + __IO uint32_t CMR1; /* Offset: 0x1C PWM Comparator Register 1 */ + __I uint32_t PDR1; /* Offset: 0x20 PWM Data Register 1 */ + __IO uint32_t CNR2; /* Offset: 0x24 PWM Counter Register 2 */ + __IO uint32_t CMR2; /* Offset: 0x28 PWM Comparator Register 2 */ + __I uint32_t PDR2; /* Offset: 0x2C PWM Data Register 2 */ + __IO uint32_t CNR3; /* Offset: 0x30 PWM Counter Register 3 */ + __IO uint32_t CMR3; /* Offset: 0x34 PWM Comparator Register 3 */ + __I uint32_t PDR3; /* Offset: 0x38 PWM Data Register 3 */ + __I uint32_t RESERVED0[1]; + __IO uint32_t PIER; /* Offset: 0x40 PWM Interrupt Enable Register */ + __IO uint32_t PIIR; /* Offset: 0x44 PWM Interrupt Indication Register */ + __I uint32_t RESERVE1[2]; + __IO uint32_t CCR0; /* Offset: 0x50 PWM Capture Control Register 0 */ + __IO uint32_t CCR2; /* Offset: 0x54 PWM Capture Control Register 2 */ + __IO uint32_t CRLR0; /* Offset: 0x58 PWM Capture Rising Latch Register (Channel 0) */ + __IO uint32_t CFLR0; /* Offset: 0x5C PWM Capture Falling Latch Register (Channel 0) */ + __IO uint32_t CRLR1; /* Offset: 0x60 PWM Capture Rising Latch Register (Channel 1) */ + __IO uint32_t CFLR1; /* Offset: 0x64 PWM Capture Falling Latch Register (Channel 1) */ + __IO uint32_t CRLR2; /* Offset: 0x68 PWM Capture Rising Latch Register (Channel 2) */ + __IO uint32_t CFLR2; /* Offset: 0x6C PWM Capture Falling Latch Register (Channel 2) */ + __IO uint32_t CRLR3; /* Offset: 0x70 PWM Capture Rising Latch Register (Channel 3) */ + __IO uint32_t CFLR3; /* Offset: 0x74 PWM Capture Falling Latch Register (Channel 3) */ + __IO uint32_t CAPENR; /* Offset: 0x78 PWM Capture Input 0~3 Enable Register */ + __IO uint32_t POE; /* Offset: 0x7C PWM Output Enable for Channel 0~3 */ + __IO uint32_t TCON; /* Offset: 0x80 PWM Trigger Control for Channel 0~3 */ + __IO uint32_t TSTATUS; /* Offset: 0x84 PWM Trigger Status Register */ + __IO uint32_t SYNCBUSY0; /* Offset: 0x88 PWM0 Synchronous Busy Status Register */ + __IO uint32_t SYNCBUSY1; /* Offset: 0x8C PWM1 Synchronous Busy Status Register */ + __IO uint32_t SYNCBUSY2; /* Offset: 0x90 PWM2 Synchronous Busy Status Register */ + __IO uint32_t SYNCBUSY3; /* Offset: 0x94 PWM3 Synchronous Busy Status Register */ + __I uint32_t RESERVE2[10]; + __IO uint32_t CAPPDMACTL; /* Offset: 0xC0 PWM Group A Trigger Status Register */ + __IO uint32_t CAP0PDMA; /* Offset: 0xC4 PWM Group A PDMA channel 0 DATA Register */ + __IO uint32_t CAP1PDMA; /* Offset: 0xC8 PWM Group A PDMA channel 1 DATA Register */ + __IO uint32_t CAP2PDMA; /* Offset: 0xCC PWM Group A PDMA channel 2 DATA Register */ + __IO uint32_t CAP3PDMA; /* Offset: 0xD0 PWM Group A PDMA channel 3 DATA Register */ + +} PWM_T; + + + + +/** @addtogroup REG_PWM_BITMASK PWM Bit Mask + @{ + */ + +/* PWM PPR Bit Field Definitions */ +#define PWM_PPR_DZI23_Pos 24 /*!< PWM_T::PPR: DZI23 Position */ +#define PWM_PPR_DZI23_Msk (0xFFul << PWM_PPR_DZI23_Pos) /*!< PWM_T::PPR: DZI23 Mask */ + +#define PWM_PPR_DZI01_Pos 16 /*!< PWM_T::PPR: DZI01 Position */ +#define PWM_PPR_DZI01_Msk (0xFFul << PWM_PPR_DZI01_Pos) /*!< PWM_T::PPR: DZI01 Mask */ + +#define PWM_PPR_CP23_Pos 8 /*!< PWM_T::PPR: CP23 Position */ +#define PWM_PPR_CP23_Msk (0xFFul << PWM_PPR_CP23_Pos) /*!< PWM_T::PPR: CP23 Mask */ + +#define PWM_PPR_CP01_Pos 0 /*!< PWM_T::PPR: CP01 Position */ +#define PWM_PPR_CP01_Msk (0xFFul << PWM_PPR_CP01_Pos) /*!< PWM_T::PPR: CP01 Mask */ + +/* PWM CSR Bit Field Definitions */ +#define PWM_CSR_CSR3_Pos 12 /*!< PWM_T::CSR: CSR3 Position */ +#define PWM_CSR_CSR3_Msk (7ul << PWM_CSR_CSR3_Pos) /*!< PWM_T::CSR: CSR3 Mask */ + +#define PWM_CSR_CSR2_Pos 8 /*!< PWM_T::CSR: CSR2 Position */ +#define PWM_CSR_CSR2_Msk (7ul << PWM_CSR_CSR2_Pos) /*!< PWM_T::CSR: CSR2 Mask */ + +#define PWM_CSR_CSR1_Pos 4 /*!< PWM_T::CSR: CSR1 Position */ +#define PWM_CSR_CSR1_Msk (7ul << PWM_CSR_CSR1_Pos) /*!< PWM_T::CSR: CSR1 Mask */ + +#define PWM_CSR_CSR0_Pos 0 /*!< PWM_T::CSR: CSR0 Position */ +#define PWM_CSR_CSR0_Msk (7ul << PWM_CSR_CSR0_Pos) /*!< PWM_T::CSR: CSR0 Mask */ + +/* PWM PCR Bit Field Definitions */ +#define PWM_PCR_PWM23TYPE_Pos 31 /*!< PWM_T::PCR: PWM23TYPE Position */ +#define PWM_PCR_PWM23TYPE_Msk (1ul << PWM_PCR_PWM23TYPE_Pos) /*!< PWM_T::PCR: PWM23TYPE Mask */ + +#define PWM_PCR_PWM01TYPE_Pos 30 /*!< PWM_T::PCR: PWM01TYPE Position */ +#define PWM_PCR_PWM01TYPE_Msk (1ul << PWM_PCR_PWM01TYPE_Pos) /*!< PWM_T::PCR: PWM01TYPE Mask */ + +#define PWM_PCR_CH3MOD_Pos 27 /*!< PWM_T::PCR: CH3MOD Position */ +#define PWM_PCR_CH3MOD_Msk (1ul << PWM_PCR_CH3MOD_Pos) /*!< PWM_T::PCR: CH3MOD Mask */ + +#define PWM_PCR_CH3INV_Pos 26 /*!< PWM_T::PCR: CH3INV Position */ +#define PWM_PCR_CH3INV_Msk (1ul << PWM_PCR_CH3INV_Pos) /*!< PWM_T::PCR: CH3INV Mask */ + +#define PWM_PCR_CH3PINV_Pos 25 /*!< PWM_T::PCR: CH3PINV Position */ +#define PWM_PCR_CH3PINV_Msk (1ul << PWM_PCR_CH3PINV_Pos) /*!< PWM_T::PCR: CH3PINV Mask */ + +#define PWM_PCR_CH3EN_Pos 24 /*!< PWM_T::PCR: CH3EN Position */ +#define PWM_PCR_CH3EN_Msk (1ul << PWM_PCR_CH3EN_Pos) /*!< PWM_T::PCR: CH3EN Mask */ + +#define PWM_PCR_CH2MOD_Pos 19 /*!< PWM_T::PCR: CH2MOD Position */ +#define PWM_PCR_CH2MOD_Msk (1ul << PWM_PCR_CH2MOD_Pos) /*!< PWM_T::PCR: CH2MOD Mask */ + +#define PWM_PCR_CH2INV_Pos 18 /*!< PWM_T::PCR: CH2INV Position */ +#define PWM_PCR_CH2INV_Msk (1ul << PWM_PCR_CH2INV_Pos) /*!< PWM_T::PCR: CH2INV Mask */ + +#define PWM_PCR_CH2PINV_Pos 17 /*!< PWM_T::PCR: CH2PINV Position */ +#define PWM_PCR_CH2PINV_Msk (1ul << PWM_PCR_CH2PINV_Pos) /*!< PWM_T::PCR: CH2PINV Mask */ + +#define PWM_PCR_CH2EN_Pos 16 /*!< PWM_T::PCR: CH2EN Position */ +#define PWM_PCR_CH2EN_Msk (1ul << PWM_PCR_CH2EN_Pos) /*!< PWM_T::PCR: CH2EN Mask */ + +#define PWM_PCR_CH1MOD_Pos 11 /*!< PWM_T::PCR: CH1MOD Position */ +#define PWM_PCR_CH1MOD_Msk (1ul << PWM_PCR_CH1MOD_Pos) /*!< PWM_T::PCR: CH1MOD Mask */ + +#define PWM_PCR_CH1INV_Pos 10 /*!< PWM_T::PCR: CH1INV Position */ +#define PWM_PCR_CH1INV_Msk (1ul << PWM_PCR_CH1INV_Pos) /*!< PWM_T::PCR: CH1INV Mask */ + +#define PWM_PCR_CH1PINV_Pos 9 /*!< PWM_T::PCR: CH1PINV Position */ +#define PWM_PCR_CH1PINV_Msk (1ul << PWM_PCR_CH1PINV_Pos) /*!< PWM_T::PCR: CH1PINV Mask */ + +#define PWM_PCR_CH1EN_Pos 8 /*!< PWM_T::PCR: CH1EN Position */ +#define PWM_PCR_CH1EN_Msk (1ul << PWM_PCR_CH1EN_Pos) /*!< PWM_T::PCR: CH1EN Mask */ + +#define PWM_PCR_DZEN23_Pos 5 /*!< PWM_T::PCR: DZEN23 Position */ +#define PWM_PCR_DZEN23_Msk (1ul << PWM_PCR_DZEN23_Pos) /*!< PWM_T::PCR: DZEN23 Mask */ + +#define PWM_PCR_DZEN01_Pos 4 /*!< PWM_T::PCR: DZEN01 Position */ +#define PWM_PCR_DZEN01_Msk (1ul << PWM_PCR_DZEN01_Pos) /*!< PWM_T::PCR: DZEN01 Mask */ + +#define PWM_PCR_CH0MOD_Pos 3 /*!< PWM_T::PCR: CH0MOD Position */ +#define PWM_PCR_CH0MOD_Msk (1ul << PWM_PCR_CH0MOD_Pos) /*!< PWM_T::PCR: CH0MOD Mask */ + +#define PWM_PCR_CH0INV_Pos 2 /*!< PWM_T::PCR: CH0INV Position */ +#define PWM_PCR_CH0INV_Msk (1ul << PWM_PCR_CH0INV_Pos) /*!< PWM_T::PCR: CH0INV Mask */ + +#define PWM_PCR_CH0PINV_Pos 1 /*!< PWM_T::PCR: CH0PINV Position */ +#define PWM_PCR_CH0PINV_Msk (1ul << PWM_PCR_CH0PINV_Pos) /*!< PWM_T::PCR: CH0PINV Mask */ + +#define PWM_PCR_CH0EN_Pos 0 /*!< PWM_T::PCR: CH0EN Position */ +#define PWM_PCR_CH0EN_Msk (1ul << PWM_PCR_CH0EN_Pos) /*!< PWM_T::PCR: CH0EN Mask */ + +/* PWM CNR Bit Field Definitions */ +#define PWM_CNR_CNR_Pos 0 /*!< PWM_T::CNR0: CNR Position */ +#define PWM_CNR_CNR_Msk (0xFFFFul << PWM_CNR_CNR_Pos) /*!< PWM_T::CNR0: CNR Mask */ + +/* PWM CMR Bit Field Definitions */ +#define PWM_CMR_CMR_Pos 0 /*!< PWM_T::CMR0: CMR Position */ +#define PWM_CMR_CMR_Msk (0xFFFFul << PWM_CMR_CMR_Pos) /*!< PWM_T::CMR0: CMR Mask */ + +/* PWM PDR Bit Field Definitions */ +#define PWM_PDR_PDR_Pos 0 /*!< PWM_T::PDR0: PDR Position */ +#define PWM_PDR_PDR_Msk (0xFFFFul << PWM_PDR_PDR_Pos) /*!< PWM_T::PDR0: PDR Mask */ + +/* PWM PIER Bit Field Definitions */ + +#define PWM_PIER_INT23TYPE_Pos 17 /*!< PWM_T::PIER: INT23TYPE Position */ +#define PWM_PIER_INT23TYPE_Msk (1ul << PWM_PIER_INT23TYPE_Pos) /*!< PWM_T::PIER: INT23TYPE Mask */ + +#define PWM_PIER_INT01TYPE_Pos 16 /*!< PWM_T::PIER: INT01TYPE Position */ +#define PWM_PIER_INT01TYPE_Msk (1ul << PWM_PIER_INT01TYPE_Pos) /*!< PWM_T::PIER: INT01TYPE Mask */ + +#define PWM_PIER_PWMDIE3_Pos 11 /*!< PWM_T::PIER: PWMDIE3 Position */ +#define PWM_PIER_PWMDIE3_Msk (1ul << PWM_PIER_PWMDIE3_Pos) /*!< PWM_T::PIER: PWMDIE3 Mask */ + +#define PWM_PIER_PWMDIE2_Pos 10 /*!< PWM_T::PIER: PWMDIE2 Position */ +#define PWM_PIER_PWMDIE2_Msk (1ul << PWM_PIER_PWMDIE2_Pos) /*!< PWM_T::PIER: PWMDIE2 Mask */ + +#define PWM_PIER_PWMDIE1_Pos 9 /*!< PWM_T::PIER: PWMDIE1 Position */ +#define PWM_PIER_PWMDIE1_Msk (1ul << PWM_PIER_PWMDIE1_Pos) /*!< PWM_T::PIER: PWMDIE1 Mask */ + +#define PWM_PIER_PWMDIE0_Pos 8 /*!< PWM_T::PIER: PWMDIE0 Position */ +#define PWM_PIER_PWMDIE0_Msk (1ul << PWM_PIER_PWMDIE0_Pos) /*!< PWM_T::PIER: PWMDIE0 Mask */ + +#define PWM_PIER_PWMIE3_Pos 3 /*!< PWM_T::PIER: PWMIE3 Position */ +#define PWM_PIER_PWMIE3_Msk (1ul << PWM_PIER_PWMIE3_Pos) /*!< PWM_T::PIER: PWMIE3 Mask */ + +#define PWM_PIER_PWMIE2_Pos 2 /*!< PWM_T::PIER: PWMIE2 Position */ +#define PWM_PIER_PWMIE2_Msk (1ul << PWM_PIER_PWMIE2_Pos) /*!< PWM_T::PIER: PWMIE2 Mask */ + +#define PWM_PIER_PWMIE1_Pos 1 /*!< PWM_T::PIER: PWMIE1 Position */ +#define PWM_PIER_PWMIE1_Msk (1ul << PWM_PIER_PWMIE1_Pos) /*!< PWM_T::PIER: PWMIE1 Mask */ + +#define PWM_PIER_PWMIE0_Pos 0 /*!< PWM_T::PIER: PWMIE0 Position */ +#define PWM_PIER_PWMIE0_Msk (1ul << PWM_PIER_PWMIE0_Pos) /*!< PWM_T::PIER: PWMIE0 Mask */ + +/* PWM PIIR Bit Field Definitions */ +#define PWM_PIIR_PWMDIF3_Pos 11 /*!< PWM_T::PIIR: PWMDIF3 Position */ +#define PWM_PIIR_PWMDIF3_Msk (1ul << PWM_PIIR_PWMDIF3_Pos) /*!< PWM_T::PIIR: PWMDIF3 Mask */ + +#define PWM_PIIR_PWMDIF2_Pos 10 /*!< PWM_T::PIIR: PWMDIF2 Position */ +#define PWM_PIIR_PWMDIF2_Msk (1ul << PWM_PIIR_PWMDIF2_Pos) /*!< PWM_T::PIIR: PWMDIF2 Mask */ + +#define PWM_PIIR_PWMDIF1_Pos 9 /*!< PWM_T::PIIR: PWMDIF1 Position */ +#define PWM_PIIR_PWMDIF1_Msk (1ul << PWM_PIIR_PWMDIF1_Pos) /*!< PWM_T::PIIR: PWMDIF1 Mask */ + +#define PWM_PIIR_PWMDIF0_Pos 8 /*!< PWM_T::PIIR: PWMDIF0 Position */ +#define PWM_PIIR_PWMDIF0_Msk (1ul << PWM_PIIR_PWMDIF0_Pos) /*!< PWM_T::PIIR: PWMDIF0 Mask */ + +#define PWM_PIIR_PWMIF3_Pos 3 /*!< PWM_T::PIIR: PWMIF3 Position */ +#define PWM_PIIR_PWMIF3_Msk (1ul << PWM_PIIR_PWMIF3_Pos) /*!< PWM_T::PIIR: PWMIF3 Mask */ + +#define PWM_PIIR_PWMIF2_Pos 2 /*!< PWM_T::PIIR: PWMIF2 Position */ +#define PWM_PIIR_PWMIF2_Msk (1ul << PWM_PIIR_PWMIF2_Pos) /*!< PWM_T::PIIR: PWMIF2 Mask */ + +#define PWM_PIIR_PWMIF1_Pos 1 /*!< PWM_T::PIIR: PWMIF1 Position */ +#define PWM_PIIR_PWMIF1_Msk (1ul << PWM_PIIR_PWMIF1_Pos) /*!< PWM_T::PIIR: PWMIF1 Mask */ + +#define PWM_PIIR_PWMIF0_Pos 0 /*!< PWM_T::PIIR: PWMIF0 Position */ +#define PWM_PIIR_PWMIF0_Msk (1ul << PWM_PIIR_PWMIF0_Pos) /*!< PWM_T::PIIR: PWMIF0 Mask */ + +/* PWM CCR0 Bit Field Definitions */ +#define PWM_CCR0_CFLRI1_Pos 23 /*!< PWM_T::CCR0: CFLRI1 Position */ +#define PWM_CCR0_CFLRI1_Msk (1ul << PWM_CCR0_CFLRI1_Pos) /*!< PWM_T::CCR0: CFLRI1 Mask */ + +#define PWM_CCR0_CRLRI1_Pos 22 /*!< PWM_T::CCR0: CRLRI1 Position */ +#define PWM_CCR0_CRLRI1_Msk (1ul << PWM_CCR0_CRLRI1_Pos) /*!< PWM_T::CCR0: CRLRI1 Mask */ + +#define PWM_CCR0_CAPIF1_Pos 20 /*!< PWM_T::CCR0: CAPIF1 Position */ +#define PWM_CCR0_CAPIF1_Msk (1ul << PWM_CCR0_CAPIF1_Pos) /*!< PWM_T::CCR0: CAPIF1 Mask */ + +#define PWM_CCR0_CAPCH1EN_Pos 19 /*!< PWM_T::CCR0: CAPCH1EN Position */ +#define PWM_CCR0_CAPCH1EN_Msk (1ul << PWM_CCR0_CAPCH1EN_Pos) /*!< PWM_T::CCR0: CAPCH1EN Mask */ + +#define PWM_CCR0_CFL_IE1_Pos 18 /*!< PWM_T::CCR0: CFL_IE1 Position */ +#define PWM_CCR0_CFL_IE1_Msk (1ul << PWM_CCR0_CFL_IE1_Pos) /*!< PWM_T::CCR0: CFL_IE1 Mask */ + +#define PWM_CCR0_CRL_IE1_Pos 17 /*!< PWM_T::CCR0: CRL_IE1 Position */ +#define PWM_CCR0_CRL_IE1_Msk (1ul << PWM_CCR0_CRL_IE1_Pos) /*!< PWM_T::CCR0: CRL_IE1 Mask */ + +#define PWM_CCR0_INV1_Pos 16 /*!< PWM_T::CCR0: INV1 Position */ +#define PWM_CCR0_INV1_Msk (1ul << PWM_CCR0_INV1_Pos) /*!< PWM_T::CCR0: INV1 Mask */ + +#define PWM_CCR0_CFLRI0_Pos 7 /*!< PWM_T::CCR0: CFLRI0 Position */ +#define PWM_CCR0_CFLRI0_Msk (1ul << PWM_CCR0_CFLRI0_Pos) /*!< PWM_T::CCR0: CFLRI0 Mask */ + +#define PWM_CCR0_CRLRI0_Pos 6 /*!< PWM_T::CCR0: CRLRI0 Position */ +#define PWM_CCR0_CRLRI0_Msk (1ul << PWM_CCR0_CRLRI0_Pos) /*!< PWM_T::CCR0: CRLRI0 Mask */ + +#define PWM_CCR0_CAPIF0_Pos 4 /*!< PWM_T::CCR0: CAPIF0 Position */ +#define PWM_CCR0_CAPIF0_Msk (1ul << PWM_CCR0_CAPIF0_Pos) /*!< PWM_T::CCR0: CAPIF0 Mask */ + +#define PWM_CCR0_CAPCH0EN_Pos 3 /*!< PWM_T::CCR0: CAPCH0EN Position */ +#define PWM_CCR0_CAPCH0EN_Msk (1ul << PWM_CCR0_CAPCH0EN_Pos) /*!< PWM_T::CCR0: CAPCH0EN Mask */ + +#define PWM_CCR0_CFL_IE0_Pos 2 /*!< PWM_T::CCR0: CFL_IE0 Position */ +#define PWM_CCR0_CFL_IE0_Msk (1ul << PWM_CCR0_CFL_IE0_Pos) /*!< PWM_T::CCR0: CFL_IE0 Mask */ + +#define PWM_CCR0_CRL_IE0_Pos 1 /*!< PWM_T::CCR0: CRL_IE0 Position */ +#define PWM_CCR0_CRL_IE0_Msk (1ul << PWM_CCR0_CRL_IE0_Pos) /*!< PWM_T::CCR0: CRL_IE0 Mask */ + +#define PWM_CCR0_INV0_Pos 0 /*!< PWM_T::CCR0: INV0 Position */ +#define PWM_CCR0_INV0_Msk (1ul << PWM_CCR0_INV0_Pos) /*!< PWM_T::CCR0: INV0 Mask */ + +/* PWM CCR2 Bit Field Definitions */ +#define PWM_CCR2_CFLRI3_Pos 23 /*!< PWM_T::CCR2: CFLRI3 Position */ +#define PWM_CCR2_CFLRI3_Msk (1ul << PWM_CCR2_CFLRI3_Pos) /*!< PWM_T::CCR2: CFLRI3 Mask */ + +#define PWM_CCR2_CRLRI3_Pos 22 /*!< PWM_T::CCR2: CRLRI3 Position */ +#define PWM_CCR2_CRLRI3_Msk (1ul << PWM_CCR2_CRLRI3_Pos) /*!< PWM_T::CCR2: CRLRI3 Mask */ + +#define PWM_CCR2_CAPIF3_Pos 20 /*!< PWM_T::CCR2: CAPIF3 Position */ +#define PWM_CCR2_CAPIF3_Msk (1ul << PWM_CCR2_CAPIF3_Pos) /*!< PWM_T::CCR2: CAPIF3 Mask */ + +#define PWM_CCR2_CAPCH3EN_Pos 19 /*!< PWM_T::CCR2: CAPCH3EN Position */ +#define PWM_CCR2_CAPCH3EN_Msk (1ul << PWM_CCR2_CAPCH3EN_Pos) /*!< PWM_T::CCR2: CAPCH3EN Mask */ + +#define PWM_CCR2_CFL_IE3_Pos 18 /*!< PWM_T::CCR2: CFL_IE3 Position */ +#define PWM_CCR2_CFL_IE3_Msk (1ul << PWM_CCR2_CFL_IE3_Pos) /*!< PWM_T::CCR2: CFL_IE3 Mask */ + +#define PWM_CCR2_CRL_IE3_Pos 17 /*!< PWM_T::CCR2: CRL_IE3 Position */ +#define PWM_CCR2_CRL_IE3_Msk (1ul << PWM_CCR2_CRL_IE3_Pos) /*!< PWM_T::CCR2: CRL_IE3 Mask */ + +#define PWM_CCR2_INV3_Pos 16 /*!< PWM_T::CCR2: INV3 Position */ +#define PWM_CCR2_INV3_Msk (1ul << PWM_CCR2_INV3_Pos) /*!< PWM_T::CCR2: INV3 Mask */ + +#define PWM_CCR2_CFLRI2_Pos 7 /*!< PWM_T::CCR2: CFLRI2 Position */ +#define PWM_CCR2_CFLRI2_Msk (1ul << PWM_CCR2_CFLRI2_Pos) /*!< PWM_T::CCR2: CFLRI2 Mask */ + +#define PWM_CCR2_CRLRI2_Pos 6 /*!< PWM_T::CCR2: CRLRI2 Position */ +#define PWM_CCR2_CRLRI2_Msk (1ul << PWM_CCR2_CRLRI2_Pos) /*!< PWM_T::CCR2: CRLRI2 Mask */ + +#define PWM_CCR2_CAPIF2_Pos 4 /*!< PWM_T::CCR2: CAPIF2 Position */ +#define PWM_CCR2_CAPIF2_Msk (1ul << PWM_CCR2_CAPIF2_Pos) /*!< PWM_T::CCR2: CAPIF2 Mask */ + +#define PWM_CCR2_CAPCH2EN_Pos 3 /*!< PWM_T::CCR2: CAPCH2EN Position */ +#define PWM_CCR2_CAPCH2EN_Msk (1ul << PWM_CCR2_CAPCH2EN_Pos) /*!< PWM_T::CCR2: CAPCH2EN Mask */ + +#define PWM_CCR2_CFL_IE2_Pos 2 /*!< PWM_T::CCR2: CFL_IE2 Position */ +#define PWM_CCR2_CFL_IE2_Msk (1ul << PWM_CCR2_CFL_IE2_Pos) /*!< PWM_T::CCR2: CFL_IE2 Mask */ + +#define PWM_CCR2_CRL_IE2_Pos 1 /*!< PWM_T::CCR2: CRL_IE2 Position */ +#define PWM_CCR2_CRL_IE2_Msk (1ul << PWM_CCR2_CRL_IE2_Pos) /*!< PWM_T::CCR2: CRL_IE2 Mask */ + +#define PWM_CCR2_INV2_Pos 0 /*!< PWM_T::CCR2: INV2 Position */ +#define PWM_CCR2_INV2_Msk (1ul << PWM_CCR2_INV2_Pos) /*!< PWM_T::CCR2: INV2 Mask */ + +/* PWM CRLR Bit Field Definitions */ +#define PWM_CRLR_CRLR_Pos 0 /*!< PWM_T::CRLR0: CRLR Position */ +#define PWM_CRLR_CRLR_Msk (0xFFFFul << PWM_CRLR_CRLR_Pos) /*!< PWM_T::CRLR0: CRLR Mask */ + +/* PWM CFLR Bit Field Definitions */ +#define PWM_CFLR_CFLR_Pos 0 /*!< PWM_T::CFLR0: CFLR Position */ +#define PWM_CFLR_CFLR_Msk (0xFFFFul << PWM_CFLR_CFLR_Pos) /*!< PWM_T::CFLR0: CFLR Mask */ + +/* PWM CAPENR Bit Field Definitions */ +#define PWM_CAPENR_CAPENR_Pos 0 /*!< PWM_T::CAPENR: CAPENR Position */ +#define PWM_CAPENR_CAPENR_Msk (0xFul << PWM_CAPENR_CAPENR_Pos) /*!< PWM_T::CAPENR: CAPENR Mask */ + +/* PWM POE Bit Field Definitions */ +#define PWM_POE_PWM3_Pos 3 /*!< PWM_T::POE: PWM3 Position */ +#define PWM_POE_PWM3_Msk (1ul << PWM_POE_PWM3_Pos) /*!< PWM_T::POE: PWM3 Mask */ + +#define PWM_POE_PWM2_Pos 2 /*!< PWM_T::POE: PWM2 Position */ +#define PWM_POE_PWM2_Msk (1ul << PWM_POE_PWM2_Pos) /*!< PWM_T::POE: PWM2 Mask */ + +#define PWM_POE_PWM1_Pos 1 /*!< PWM_T::POE: PWM1 Position */ +#define PWM_POE_PWM1_Msk (1ul << PWM_POE_PWM1_Pos) /*!< PWM_T::POE: PWM1 Mask */ + +#define PWM_POE_PWM0_Pos 0 /*!< PWM_T::POE: PWM0 Position */ +#define PWM_POE_PWM0_Msk (1ul << PWM_POE_PWM0_Pos) /*!< PWM_T::POE: PWM0 Mask */ + +/* PWM TCON Bit Field Definitions */ + +#define PWM_TCON_PWM3TEN_Pos 3 /*!< PWM_T::TCON: PWM3TEN Position */ +#define PWM_TCON_PWM3TEN_Msk (1ul << PWM_TCON_PWM3TEN_Pos) /*!< PWM_T::TCON: PWM3TEN Mask */ + +#define PWM_TCON_PWM2TEN_Pos 2 /*!< PWM_T::TCON: PWM2TEN Position */ +#define PWM_TCON_PWM2TEN_Msk (1ul << PWM_TCON_PWM2TEN_Pos) /*!< PWM_T::TCON: PWM2TEN Mask */ + +#define PWM_TCON_PWM1TEN_Pos 1 /*!< PWM_T::TCON: PWM1TEN Position */ +#define PWM_TCON_PWM1TEN_Msk (1ul << PWM_TCON_PWM1TEN_Pos) /*!< PWM_T::TCON: PWM1TEN Mask */ + +#define PWM_TCON_PWM0TEN_Pos 0 /*!< PWM_T::TCON: PWM0TEN Position */ +#define PWM_TCON_PWM0TEN_Msk (1ul << PWM_TCON_PWM0TEN_Pos) /*!< PWM_T::TCON: PWM0TEN Mask */ + +/* PWM TSTATUS Bit Field Definitions */ + +#define PWM_TSTATUS_PWM3TF_Pos 3 /*!< PWM_T::TSTATUS: PWM3TF Position */ +#define PWM_TSTATUS_PWM3TF_Msk (1ul << PWM_TSTATUS_PWM3TF_Pos) /*!< PWM_T::TSTATUS: PWM3TF Mask */ + +#define PWM_TSTATUS_PWM2TF_Pos 2 /*!< PWM_T::TSTATUS: PWM2TF Position */ +#define PWM_TSTATUS_PWM2TF_Msk (1ul << PWM_TSTATUS_PWM2TF_Pos) /*!< PWM_T::TSTATUS: PWM2TF Mask */ + +#define PWM_TSTATUS_PWM1TF_Pos 1 /*!< PWM_T::TSTATUS: PWM1TF Position */ +#define PWM_TSTATUS_PWM1TF_Msk (1ul << PWM_TSTATUS_PWM1TF_Pos) /*!< PWM_T::TSTATUS: PWM1TF Mask */ + +#define PWM_TSTATUS_PWM0TF_Pos 0 /*!< PWM_T::TSTATUS: PWM0TF Position */ +#define PWM_TSTATUS_PWM0TF_Msk (1ul << PWM_TSTATUS_PWM0TF_Pos) /*!< PWM_T::TSTATUS: PWM0TF Mask */ + +/* PWM SYNCBUSY0 Bit Field Definitions */ +#define PWM_SYNCBUSY0_S_BUSY_Pos 0 /*!< PWM_T::SYNCBUSY0: S_BUSY Position */ +#define PWM_SYNCBUSY0_S_BUSY_Msk (1ul << PWM_SYNCBUSY0_S_BUSY_Pos) /*!< PWM_T::SYNCBUSY0: S_BUSY Mask */ + +/* PWM SYNCBUSY1 Bit Field Definitions */ +#define PWM_SYNCBUSY1_S_BUSY_Pos 0 /*!< PWM_T::SYNCBUSY1: S_BUSY Position */ +#define PWM_SYNCBUSY1_S_BUSY_Msk (1ul << PWM_SYNCBUSY1_S_BUSY_Pos) /*!< PWM_T::SYNCBUSY1: S_BUSY Mask */ + +/* PWM SYNCBUSY2 Bit Field Definitions */ +#define PWM_SYNCBUSY2_S_BUSY_Pos 0 /*!< PWM_T::SYNCBUSY2: S_BUSY Position */ +#define PWM_SYNCBUSY2_S_BUSY_Msk (1ul << PWM_SYNCBUSY2_S_BUSY_Pos) /*!< PWM_T::SYNCBUSY2: S_BUSY Mask */ + +/* PWM SYNCBUSY3 Bit Field Definitions */ +#define PWM_SYNCBUSY3_S_BUSY_Pos 0 /*!< PWM_T::SYNCBUSY3: S_BUSY Position */ +#define PWM_SYNCBUSY3_S_BUSY_Msk (1ul << PWM_SYNCBUSY3_S_BUSY_Pos) /*!< PWM_T::SYNCBUSY3: S_BUSY Mask */ + +/* PWM CAPPDMACTL Bit Field Definitions */ +#define PWM_CAPPDMACTL_CAP3RFORDER_Pos 27 /*!< PWM_T::CAPPDMACTL: CAP3RFORDER Position */ +#define PWM_CAPPDMACTL_CAP3RFORDER_Msk (1ul << PWM_CAPPDMACTL_CAP3RFORDER_Pos) /*!< PWM_T::CAPPDMACTL: CAP3RFORDER Mask */ + +#define PWM_CAPPDMACTL_CAP3PDMAMOD_Pos 25 /*!< PWM_T::CAPPDMACTL: CAP3PDMAMOD Position */ +#define PWM_CAPPDMACTL_CAP3PDMAMOD_Msk (3ul << PWM_CAPPDMACTL_CAP3PDMAMOD_Pos) /*!< PWM_T::CAPPDMACTL: CAP3PDMAMOD Mask */ + +#define PWM_CAPPDMACTL_CAP3PDMAEN_Pos 24 /*!< PWM_T::CAPPDMACTL: CAP3PDMAEN Position */ +#define PWM_CAPPDMACTL_CAP3PDMAEN_Msk (1ul << PWM_CAPPDMACTL_CAP3PDMAEN_Pos) /*!< PWM_T::CAPPDMACTL: CAP3PDMAEN Mask */ + +#define PWM_CAPPDMACTL_CAP2RFORDER_Pos 19 /*!< PWM_T::CAPPDMACTL: CAP2RFORDER Position */ +#define PWM_CAPPDMACTL_CAP2RFORDER_Msk (1ul << PWM_CAPPDMACTL_CAP2RFORDER_Pos) /*!< PWM_T::CAPPDMACTL: CAP2RFORDER Mask */ + +#define PWM_CAPPDMACTL_CAP2PDMAMOD_Pos 17 /*!< PWM_T::CAPPDMACTL: CAP2PDMAMOD Position */ +#define PWM_CAPPDMACTL_CAP2PDMAMOD_Msk (3ul << PWM_CAPPDMACTL_CAP2PDMAMOD_Pos) /*!< PWM_T::CAPPDMACTL: CAP2PDMAMOD Mask */ + +#define PWM_CAPPDMACTL_CAP2PDMAEN_Pos 16 /*!< PWM_T::CAPPDMACTL: CAP2PDMAEN Position */ +#define PWM_CAPPDMACTL_CAP2PDMAEN_Msk (1ul << PWM_CAPPDMACTL_CAP2PDMAEN_Pos) /*!< PWM_T::CAPPDMACTL: CAP2PDMAEN Mask */ + +#define PWM_CAPPDMACTL_CAP1RFORDER_Pos 11 /*!< PWM_T::CAPPDMACTL: CAP1RFORDER Position */ +#define PWM_CAPPDMACTL_CAP1RFORDER_Msk (1ul << PWM_CAPPDMACTL_CAP1RFORDER_Pos) /*!< PWM_T::CAPPDMACTL: CAP1RFORDER Mask */ + +#define PWM_CAPPDMACTL_CAP1PDMAMOD_Pos 9 /*!< PWM_T::CAPPDMACTL: CAP1PDMAMOD Position */ +#define PWM_CAPPDMACTL_CAP1PDMAMOD_Msk (3ul << PWM_CAPPDMACTL_CAP1PDMAMOD_Pos) /*!< PWM_T::CAPPDMACTL: CAP1PDMAMOD Mask */ + +#define PWM_CAPPDMACTL_CAP1PDMAEN_Pos 8 /*!< PWM_T::CAPPDMACTL: CAP1PDMAEN Position */ +#define PWM_CAPPDMACTL_CAP1PDMAEN_Msk (1ul << PWM_CAPPDMACTL_CAP1PDMAEN_Pos) /*!< PWM_T::CAPPDMACTL: CAP1PDMAEN Mask */ + +#define PWM_CAPPDMACTL_CAP0RFORDER_Pos 3 /*!< PWM_T::CAPPDMACTL: CAP0RFORDER Position */ +#define PWM_CAPPDMACTL_CAP0RFORDER_Msk (1ul << PWM_CAPPDMACTL_CAP0RFORDER_Pos) /*!< PWM_T::CAPPDMACTL: CAP0RFORDER Mask */ + +#define PWM_CAPPDMACTL_CAP0PDMAMOD_Pos 1 /*!< PWM_T::CAPPDMACTL: CAP0PDMAMOD Position */ +#define PWM_CAPPDMACTL_CAP0PDMAMOD_Msk (3ul << PWM_CAPPDMACTL_CAP0PDMAMOD_Pos) /*!< PWM_T::CAPPDMACTL: CAP0PDMAMOD Mask */ + +#define PWM_CAPPDMACTL_CAP0PDMAEN_Pos 0 /*!< PWM_T::CAPPDMACTL: CAP0PDMAEN Position */ +#define PWM_CAPPDMACTL_CAP0PDMAEN_Msk (1ul << PWM_CAPPDMACTL_CAP0PDMAEN_Pos) /*!< PWM_T::CAPPDMACTL: CAP0PDMAEN Mask */ + +/* PWM CAP0PDMA Bit Field Definitions */ +#define PWM_CAP0PDMA_CAP0RFPDMA_Pos 0 /*!< PWM_T::CAP0PDMA: CAP0RFPDMA Position */ +#define PWM_CAP0PDMA_CAP0RFPDMA_Msk (0xFFFFul << PWM_CAP0PDMA_CAP0RFPDMA_Pos) /*!< PWM_T::CAP0PDMA: CAP0RFPDMA Mask */ + +/* PWM CAP1PDMA Bit Field Definitions */ +#define PWM_CAP1PDMA_CAP1RFPDMA_Pos 0 /*!< PWM_T::CAP1PDMA: CAP1RFPDMA Position */ +#define PWM_CAP1PDMA_CAP1RFPDMA_Msk (0xFFFFul << PWM_CAP1PDMA_CAP1RFPDMA_Pos) /*!< PWM_T::CAP1PDMA: CAP1RFPDMA Mask */ + +/* PWM CAP2PDMA Bit Field Definitions */ +#define PWM_CAP2PDMA_CAP2RFPDMA_Pos 0 /*!< PWM_T::CAP2PDMA: CAP2RFPDMA Position */ +#define PWM_CAP2PDMA_CAP2RFPDMA_Msk (0xFFFFul << PWM_CAP2PDMA_CAP2RFPDMA_Pos) /*!< PWM_T::CAP2PDMA: CAP2RFPDMA Mask */ + +/* PWM CAP3PDMA Bit Field Definitions */ +#define PWM_CAP3PDMA_CAP3RFPDMA_Pos 0 /*!< PWM_T::CAP3PDMA: CAP3RFPDMA Position */ +#define PWM_CAP3PDMA_CAP3RFPDMA_Msk (0xFFFFul << PWM_CAP3PDMA_CAP3RFPDMA_Pos) /*!< PWM_T::CAP3PDMA: CAP3RFPDMA Mask */ +/*@}*/ /* end of group REG_PWM_BITMASK */ +/*@}*/ /* end of group REG_PWM */ + +/*------------------------- SPI Interface Controller -------------------------*/ +/** @addtogroup REG_SPI Peripheral Interface Controller (SPI) + Memory Mapped Structure for SPI Controller + @{ + */ + +typedef struct +{ + + +/** + * @var SPI_T::CNTRL + * Offset: 0x00 Control and Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GO_BUSY |SPI Transfer Control Bit And Busy Status + * | | |0 = Data transfer stopped. + * | | |1 = In Master mode, writing 1 to this bit to start the SPI data transfer; in Slave mode, + * | | | writing 1 to this bit indicates that the slave is ready to communicate with a master. + * | | |If FIFO mode is disabled, during the data transfer, this bit keeps the value of 1. + * | | |As the transfer is finished, this bit will be cleared automatically. + * | | |Software can read this bit to check if the SPI is in busy status. + * | | |In FIFO mode, this bit will be controlled by hardware. + * | | |Software should not modify this bit. + * | | |In Slave mode, this bit always returns 1 when this register is read by software. + * | | |In Master mode, this bit reflects the busy or idle status of SPI. + * | | |Note: + * | | |1. When FIFO mode is disabled, all configurations should be set before writing 1 to this GO_BUSY bit. + * | | |2. When FIFO mode is disabled and the software uses TX or RX PDMA function to transfer data, this bit + * | | | will be cleared after the PDMA finishes the data transfer. + * |[1] |RX_NEG |Receive On Negative Edge + * | | |0 = Received data input signal is latched on the rising edge of SPI bus clock. + * | | |1 = Received data input signal is latched on the falling edge of SPI bus clock. + * |[2] |TX_NEG |Transmit On Negative Edge + * | | |0 = Transmitted data output signal is changed on the rising edge of SPI bus clock. + * | | |1 = Transmitted data output signal is changed on the falling edge of SPI bus clock. + * |[7:3] |TX_BIT_LEN|Transmit Bit Length + * | | |This field specifies how many bits can be transmitted / received in one transaction. + * | | |The minimum bit length is 8 bits and can up to 32 bits. + * | | |TX_BIT_LEN = 0x08 ... 8 bits. + * | | |TX_BIT_LEN = 0x09 ... 9 bits. + * | | |...... + * | | |TX_BIT_LEN = 0x1F ... 31 bits. + * | | |TX_BIT_LEN = 0x00 ... 32 bits. + * |[10] |LSB |Send LSB First + * | | |0 = The MSB, which bit of transmit/receive register depends on the setting of TX_BIT_LEN, is transmitted/received first. + * | | |1 = The LSB, bit 0 of the SPI TX0/1 register, is sent first to the SPI data output pin, and the first bit received from + * | | | the SPI data input pin will be put in the LSB position of the RX register (bit 0 of SPI_RX0/1). + * |[11] |CLKP |Clock Polarity + * | | |0 = SPI bus clock is idle low. + * | | |1 = SPI bus clock is idle high. + * |[15:12] |SP_CYCLE |Suspend Interval (Master Only) + * | | |The four bits provide configurable suspend interval between two successive transmit/receive transaction in a transfer. + * | | |The definition of the suspend interval is the interval between the last clock edge of the preceding transaction word + * | | |and the first clock edge of the following transaction word. + * | | |The default value is 0x3. + * | | |The period of the suspend interval is obtained according to the following equation. + * | | |(SP_CYCLE[3:0] + 0.5) * period of SPI bus clock cycle + * | | |Example: + * | | |SP_CYCLE = 0x0 ... 0.5 SPI bus clock cycle. + * | | |SP_CYCLE = 0x1 ... 1.5 SPI bus clock cycle. + * | | |...... + * | | |SP_CYCLE = 0xE ... 14.5 SPI bus clock cycle. + * | | |SP_CYCLE = 0xF ... 15.5 SPI bus clock cycle. + * | | |If the variable clock function is enabled and the transmit FIFO buffer is not empty, the minimum period of suspend + * | | |interval between the successive transactions is (6.5 + SP_CYCLE) * SPI bus clock cycle. + * |[16] |IF |Unit Transfer Interrupt Flag + * | | |0 = No transaction has been finished since this bit was cleared to 0. + * | | |1 = SPI controller has finished one unit transfer. + * | | |Note: This bit will be cleared by writing 1 to itself. + * |[17] |IE |Unit Transfer Interrupt Enable + * | | |0 = SPI unit transfer interrupt Disabled. + * | | |1 = SPI unit transfer interrupt Enabled. + * |[18] |SLAVE |Slave Mode Enable + * | | |0 = Master mode. + * | | |1 = Slave mode. + * |[19] |REORDER |Byte Reorder Function Enable + * | | |0 = Byte Reorder function Disabled. + * | | |1 = Byte Reorder function Enabled. + * | | |A byte suspend interval will be inserted among each byte. + * | | |The period of the byte suspend interval depends on the setting of SP_CYCLE. + * | | |Note: + * | | |1. Byte Reorder function is only available if TX_BIT_LEN is defined as 16, 24, and 32 bits. + * | | |2. In Slave mode with level-trigger configuration, the slave select pin must be kept at active state during the + * | | | byte suspend interval. + * | | |3. The Byte Reorder function is not supported when the variable bus clock function or Dual I/O mode is enabled. + * |[21] |FIFO |FIFO Mode Enable + * | | |0 = FIFO mode Disabled. + * | | |1 = FIFO mode Enabled. + * | | |Note: + * | | |1. Before enabling FIFO mode, the other related settings should be set in advance. + * | | |2. In Master mode, if the FIFO mode is enabled, the GO_BUSY bit will be set to 1 automatically after writing data + * | | | to the transmit FIFO buffer; the GO_BUSY bit will be cleared to 0 automatically when the SPI controller is in idle. + * | | | If all data stored at transmit FIFO buffer are sent out, the TX_EMPTY bit will be set to 1 and the GO_BUSY bit will be cleared to 0. + * | | |3. After clearing this bit to 0, user must wait for at least 2 peripheral clock periods before setting this bit to 1 again. + * |[22] |TWOB |2-Bit Transfer Mode Enable + * | | |0 = 2-bit Transfer mode Disabled. + * | | |1 = 2-bit Transfer mode Enabled. + * | | |Note: When 2-bit Transfer mode is enabled, the serial transmitted 2-bit data are from SPI_TX1/0, and the received 2-bit data input are put in SPI_RX1/0. + * |[23] |VARCLK_EN |Variable Clock Enable (Master Only) + * | | |0 = SPI clock output frequency is fixed and decided only by the value of DIVIDER. + * | | |1 = SPI clock output frequency is variable. + * | | |The output frequency is decided by the value of VARCLK, DIVIDER, and DIVIDER2. + * | | |Note: When this VARCLK_EN bit is set to 1, the setting of TX_BIT_LEN must be programmed as 0x10 (16-bit mode). + * |[24] |RX_EMPTY |Receive FIFO Buffer Empty Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_CNTRL[24]. + * | | |0 = Receive FIFO buffer is not empty. + * | | |1 = Receive FIFO buffer is empty. + * |[25] |RX_FULL |Receive FIFO Buffer Full Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_STATUS[25]. + * | | |0 = Receive FIFO buffer is not full. + * | | |1 = Receive FIFO buffer is full. + * |[26] |TX_EMPTY |Transmit FIFO Buffer Empty Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_STATUS[26]. + * | | |0 = Transmit FIFO buffer is not empty. + * | | |1 = Transmit FIFO buffer is empty. + * |[27] |TX_FULL |Transmit FIFO Buffer Full Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_STATUS[27]. + * | | |0 = Transmit FIFO buffer is not full. + * | | |1 = Transmit FIFO buffer is full. + * @var SPI_T::DIVIDER + * Offset: 0x04 Clock Divider Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |DIVIDER |Clock Divider 1 Register + * | | |The value in this field is the frequency divider for generating the SPI peripheral clock and the SPI bus clock of SPI master. + * | | |The frequency is obtained according to the following equation. + * | | |If the bit of BCn, SPI_CNTRL2[31], is set to 0, + * | | | SPI peripheral clock frequency = system clock frequency / (DIVIDER + 1) / 2 + * | | |else if BCn is set to 1, + * | | | SPI peripheral clock frequency = SPI peripheral clock source frequency / (DIVIDER + 1) + * | | |The SPI peripheral clock source is defined in the CLKSEL1 register. + * |[23:16] |DIVIDER2 |Clock Divider 2 Register (Master Only) + * | | |The value in this field is the 2nd frequency divider for generating the second clock of the variable clock function. + * | | |The frequency is obtained according to the following equation: + * | | | f_clk2 = SPI peripheral clock frequency / (DIVIDER2 + 1) / 2 + * | | |If the VARCLK_EN bit is cleared to 0, this setting is unmeaning. + * @var SPI_T::SSR + * Offset: 0x08 Slave Select Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1:0] |SSR |Slave Select Control Bits (Master Only) + * | | |If AUTOSS bit is cleared, writing 1 to any bit of this field sets the proper SPIn_SPISS0/1 + * | | |line to an active state and writing 0 sets the line back to inactive state. + * | | |If the AUTOSS bit is set, writing 0 to any bit location of this field will keep the corresponding + * | | |SPIn_SPISS0/1 line at inactive state; writing 1 to any bit location of this field will select + * | | |appropriate SPIn_SPISS0/1 line to be automatically driven to active state for the duration of the + * | | |transmit/receive, and will be driven to inactive state for the rest of the time. + * | | |The active state of SPIn_SPISS0/1 is specified in SS_LVL. + * | | |Note: SPIn_SPISS0 is defined as the slave select input in Slave mode. + * |[2] |SS_LVL |Slave Select Active Level + * | | |This bit defines the active status of slave select signal (SPIn_SPISS0/1). + * | | |0 = The slave select signal SPIn_SPISS0/1 is active on low-level/falling-edge. + * | | |1 = The slave select signal SPIn_SPISS0/1 is active on high-level/rising-edge. + * |[3] |AUTOSS |Automatic Slave Select Function Enable (Master Only) + * | | |0 = If this bit is cleared, slave select signals will be asserted/de-asserted by setting /clearing + * | | | the corresponding bits of SPI_SSR[1:0]. + * | | |1 = If this bit is set, SPIn_SPISS0/1 signals will be generated automatically. + * | | | It means that device/slave select signal, which is set in SPI_SSR[1:0], will be asserted by the + * | | | SPI controller when transmit/receive is started, and will be de-asserted after each transmit/receive is finished. + * |[4] |SS_LTRIG |Slave Select Level Trigger Enable (Slave Only) + * | | |0 = Slave select signal is edge-trigger. + * | | | This is the default value. + * | | | The SS_LVL bit decides the signal is active after a falling-edge or rising-edge. + * | | |1 = Slave select signal is level-trigger. + * | | | The SS_LVL bit decides the signal is active low or active high. + * |[5] |LTRIG_FLAG|Level Trigger Accomplish Flag + * | | |In Slave mode, this bit indicates whether the received bit number meets the requirement or not after the current transaction done. + * | | |0 = Transferred bit length of one transaction does not meet the specified requirement. + * | | |1 = Transferred bit length meets the specified requirement which defined in TX_BIT_LEN. + * | | |Note: This bit is READ only. + * | | |As the GO_BUSY bit is set to 1 by software, the LTRIG_FLAG will be cleared to 0 after 4 SPI peripheral clock periods plus 1 system clock period. + * | | |In FIFO mode, this bit has no meaning. + * @var SPI_T::RX + * Offset: 0x10/0x14 Data Receive Register 0/1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |RX |Data Receive Register + * | | |The data receive register holds the datum received from SPI data input pin. + * | | |If FIFO mode is disabled, the last received data can be accessed through software by reading this register. + * | | |If the FIFO bit is set as 1 and the RX_EMPTY bit, SPI_CNTRL[24] or SPI_STATUS[24], is not set to 1, the receive + * | | |FIFO buffer can be accessed through software by reading this register. This is a read-only register. + * @var SPI_T::TX + * Offset: 0x20/0x24 Data Transmit Register 0/1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |TX |Data Transmit Register + * | | |The data transmit registers hold the data to be transmitted in the next transfer. + * | | |The number of valid bits depends on the setting of transmit bit length field of the SPI_CNTRL register. + * | | |For example, if TX_BIT_LEN is set to 0x08, the bits TX[7:0] will be transmitted in next transfer. + * | | |If TX_BIT_LEN is set to 0x00, the SPI controller will perform a 32-bit transfer. + * | | |Note 1: When the SPI controller is configured as a slave device and FIFO mode is disabled, if the SPI + * | | | controller attempts to transmit data to a master, the transmit data register should be updated + * | | | by software before setting the GO_BUSY bit to 1. + * | | |Note 2: In Master mode, SPI controller will start to transfer after 5 peripheral clock cycles after user writes to this register. + * @var SPI_T::VARCLK + * Offset: 0x34 Variable Clock Pattern Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |VARCLK |Variable Clock Pattern + * | | |This register defines the clock pattern of the SPI transfer. + * | | |If the variable clock function is disabled, this setting is unmeaning. + * @var SPI_T::DMA + * Offset: 0x38 SPI DMA Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TX_DMA_GO |Transmit DMA Start + * | | |Setting this bit to 1 will start the transmit PDMA process. + * | | |SPI controller will issue request to PDMA controller automatically. + * | | |Hardware will clear this bit to 0 automatically after PDMA transfer done. + * | | |If the SPI transmit PDMA function is used to transfer data, the GO_BUSY bit should not be set to 1 by software. + * | | |The PDMA control logic of SPI controller will set it automatically whenever necessary. + * | | |In Slave mode and when FIFO mode is disabled, the minimal suspend interval between two successive transactions + * | | |must be larger than (8 SPI clock periods + 14 APB clock periods) for edge-trigger mode or + * | | |(9.5 SPI clock periods + 14 APB clock periods) for level-trigger mode. + * | | |If the 2-bit Transfer mode is enabled, additional 18 APB clock periods for the above conditions is required. + * |[1] |RX_DMA_GO |Receive DMA Start + * | | |Setting this bit to 1 will start the receive PDMA process. + * | | |The SPI controller will issue request to PDMA controller automatically when the SPI receive buffer is not empty. + * | | |This bit will be cleared to 0 by hardware automatically after PDMA transfer is done. + * | | |If the software uses the receive PDMA function to access the received data of SPI and does not use the transmit + * | | |PDMA function, the GO_BUSY bit should be set by software. + * | | |Enabling FIFO mode is recommended if the software uses more than one PDMA channel to transfer data. + * | | |In Slave mode and when FIFO mode is disabled, if the software only uses one PDMA channel for SPI receive PDMA + * | | |function and the other PDMA channels are not in use, the minimal suspend interval between two successive + * | | |transactions must be larger than (9 SPI slave peripheral clock periods + 4 APB clock periods) for Edge-trigger + * | | |mode or (9.5 SPI slave peripheral clock periods + 4 APB clock periods) for Level-trigger mode. + * |[2] |PDMA_RST |PDMA Reset + * | | |0 = No effect. + * | | |1 = Reset the PDMA control logic of the SPI controller. This bit will be cleared to 0 automatically. + * @var SPI_T::CNTRL2 + * Offset: 0x3C Control and Status Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8] |NOSLVSEL |Slave 3-Wire Mode Enable + * | | |This is used to ignore the slave select signal in Slave mode. + * | | |The SPI controller can work with 3-wire interface including SPIn_CLK, SPIn_MISO, and SPIn_MOSI. + * | | |0 = 4-wire bi-direction interface. + * | | |1 = 3-wire bi-direction interface. + * | | |Note: In Slave 3-wire mode, the SS_LTRIG, SPI_SSR[4] will be set as 1 automatically. + * |[9] |SLV_ABORT |Slave 3-Wire Mode Abort Control + * | | |In normal operation, there is an interrupt event when the received data meet the required bits which defined in TX_BIT_LEN. + * | | |If the received bits are less than the requirement and there is no more SPI clock input over the one transfer time in + * | | |Slave 3-wire mode, the user can set this bit to force the current transfer done and then the user can get a transfer done interrupt event. + * | | |Note: This bit will be cleared to 0 automatically by hardware after it is set to 1 by software. + * |[10] |SSTA_INTEN|Slave 3-Wire Mode Start Interrupt Enable + * | | |Used to enable interrupt when the transfer has started in Slave 3-wire mode. + * | | |If there is no transfer done interrupt over the time period which is defined by user after the transfer start, + * | | |the user can set the SLV_ABORT bit to force the transfer done. + * | | |0 = Transaction start interrupt Disabled. + * | | |1 = Transaction start interrupt Enabled. + * | | |It will be cleared to 0 as the current transfer is done or the SLV_START_INTSTS bit is cleared. + * |[11] |SLV_START_INTSTS|Slave 3-Wire Mode Start Interrupt Status + * | | |This bit indicates if a transaction has started in Slave 3-wire mode. + * | | |It is a mutual mirror bit of SPI_STATUS[11]. + * | | |0 = Slave has not detected any SPI clock transition since the SSTA_INTEN bit was set to 1. + * | | |1 = A transaction has started in Slave 3-wire mode. + * | | |It will be cleared automatically when a transaction is done or by writing 1 to this bit. + * |[12] |DUAL_IO_DIR|Dual I/O Mode Direction Control + * | | |0 = Dual Input mode. + * | | |1 = Dual Output mode. + * |[13] |DUAL_IO_EN|Dual I/O Mode Enable + * | | |0 = Dual I/O mode Disabled. + * | | |1 = Dual I/O mode Enabled. + * |[16] |SS_INT_OPT|Slave Select Inactive Interrupt Option + * | | |This setting is only available if the SPI controller is configured as level trigger slave device. + * | | |0 = As the slave select signal goes to inactive level, the IF bit will NOT be set to 1. + * | | |1 = As the slave select signal goes to inactive level, the IF bit will be set to 1. + * |[31] |BCn |SPI Peripheral Clock Backward Compatible Option + * | | |0 = Backward compatible clock configuration. + * | | |1 = Clock configuration is not backward compatible. + * | | |Refer to the description of SPI_DIVIDER register for details. + * @var SPI_T::FIFO_CTL + * Offset: 0x40 SPI FIFO Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RX_CLR |Clear Receive FIFO Buffer + * | | |0 = No effect. + * | | |1 = Clear receive FIFO buffer. + * | | |The RX_FULL flag will be cleared to 0 and the RX_EMPTY flag will be set to 1. + * | | |This bit will be cleared to 0 by hardware after it is set to 1 by software. + * |[1] |TX_CLR |Clear Transmit FIFO Buffer + * | | |0 = No effect. + * | | |1 = Clear transmit FIFO buffer. + * | | |The TX_FULL flag will be cleared to 0 and the TX_EMPTY flag will be set to 1. + * | | |This bit will be cleared to 0 by hardware after it is set to 1 by software. + * |[2] |RX_INTEN |Receive Threshold Interrupt Enable + * | | |0 = RX threshold interrupt Disabled. + * | | |1 = RX threshold interrupt Enabled. + * |[3] |TX_INTEN |Transmit Threshold Interrupt Enable + * | | |0 = TX threshold interrupt Disabled. + * | | |1 = TX threshold interrupt Enabled. + * |[6] |RXOV_INTEN|Receive FIFO Overrun Interrupt Enable + * | | |0 = Receive FIFO overrun interrupt Disabled. + * | | |1 = Receive FIFO overrun interrupt Enabled. + * |[21] |TIMEOUT_INTEN|Receive FIFO Time-Out Interrupt Enable + * | | |0 = Time-out interrupt Disabled. + * | | |1 = Time-out interrupt Enabled. + * |[26:24] |RX_THRESHOLD|Receive FIFO Threshold + * | | |If the valid data count of the receive FIFO buffer is larger than the RX_THRESHOLD setting, + * | | |the RX_INTSTS bit will be set to 1, else the RX_INTSTS bit will be cleared to 0. + * |[30:28] |TX_THRESHOLD|Transmit FIFO Threshold + * | | |If the valid data count of the transmit FIFO buffer is less than or equal to the TX_THRESHOLD + * | | |setting, the TX_INTSTS bit will be set to 1, else the TX_INTSTS bit will be cleared to 0. + * @var SPI_T::STATUS + * Offset: 0x44 SPI Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RX_INTSTS |Receive FIFO Threshold Interrupt Status (Read Only) + * | | |0 = The valid data count within the Rx FIFO buffer is smaller than or equal to the setting value of RX_THRESHOLD. + * | | |1 = The valid data count within the receive FIFO buffer is larger than the setting value of RX_THRESHOLD. + * | | |Note: If RX_INTEN = 1 and RX_INTSTS = 1, the SPI controller will generate a SPI interrupt request. + * |[2] |RX_OVERRUN|Receive FIFO Overrun Status + * | | |When the receive FIFO buffer is full, the follow-up data will be dropped and this bit will be set to 1. + * | | |Note: This bit will be cleared by writing 1 to itself. + * |[4] |TX_INTSTS |Transmit FIFO Threshold Interrupt Status (Read Only) + * | | |0 = The valid data count within the transmit FIFO buffer is larger than the setting value of TX_THRESHOLD. + * | | |1 = The valid data count within the transmit FIFO buffer is less than or equal to the setting value of TX_THRESHOLD. + * | | |Note: If TX_INTEN = 1 and TX_INTSTS = 1, the SPI controller will generate a SPI interrupt request. + * |[11] |SLV_START_INTSTS|Slave Start Interrupt Status + * | | |It is used to dedicate if a transaction has started in Slave 3-wire mode. + * | | |It is a mutual mirror bit of SPI_CNTRL2[11]. + * | | |0 = Slave has not detected any SPI clock transition since the SSTA_INTEN bit was set to 1. + * | | |1 = A transaction has started in Slave 3-wire mode. + * | | |It will be cleared as a transaction is done or by writing 1 to this bit. + * |[15:12] |RX_FIFO_COUNT|Receive FIFO Data Count (Read Only) + * | | |This bit field indicates the valid data count of receive FIFO buffer. + * |[16] |IF |SPI Unit Transfer Interrupt Flag + * | | |It is a mutual mirror bit of SPI_CNTRL[16]. + * | | |0 = No transaction has been finished since this bit was cleared to 0. + * | | |1 = SPI controller has finished one unit transfer. + * | | |Note: This bit will be cleared by writing 1 to itself. + * |[20] |TIMEOUT |Time-Out Interrupt Flag + * | | |0 = No receive FIFO time-out event. + * | | |1 = Receive FIFO buffer is not empty and no read operation on receive FIFO buffer over 64 SPI clock + * | | |period in Master mode or over 576 SPI peripheral clock period in Slave mode. + * | | |When the received FIFO buffer is read by software, the time-out status will be cleared automatically. + * | | |Note: This bit will be cleared by writing 1 to itself. + * |[24] |RX_EMPTY |Receive FIFO Buffer Empty Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_CNTRL[24]. + * | | |0 = Receive FIFO buffer is not empty. + * | | |1 = Receive FIFO buffer is empty. + * |[25] |RX_FULL |Receive FIFO Buffer Empty Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_CNTRL[24]. + * | | |0 = Receive FIFO buffer is not empty. + * | | |1 = Receive FIFO buffer is empty. + * |[26] |TX_EMPTY |Transmit FIFO Buffer Empty Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_CNTRL[26]. + * | | |0 = Transmit FIFO buffer is not empty. + * | | |1 = Transmit FIFO buffer is empty. + * |[27] |TX_FULL |Transmit FIFO Buffer Full Indicator (Read Only) + * | | |It is a mutual mirror bit of SPI_CNTRL[27]. + * | | |0 = Transmit FIFO buffer is not full. + * | | |1 = Transmit FIFO buffer is full. + * |[31:28] |TX_FIFO_COUNT|Transmit FIFO Data Count (Read Only) + * | | |This bit field indicates the valid data count of transmit FIFO buffer. + */ + + __IO uint32_t CNTRL; /* Offset: 0x00 Control and Status Register */ + __IO uint32_t DIVIDER; /* Offset: 0x04 Clock Divider Register */ + __IO uint32_t SSR; /* Offset: 0x08 Slave Select Register */ + __I uint32_t RESERVE0; + __I uint32_t RX[2]; /* Offset: 0x10/0x14 Data Receive Register 0/1 */ + __I uint32_t RESERVE1; + __I uint32_t RESERVE2; + __O uint32_t TX[2]; /* Offset: 0x20/0x24 Data Transmit Register 0/1 */ + __I uint32_t RESERVE3; + __I uint32_t RESERVE4; + __I uint32_t RESERVE5; + __IO uint32_t VARCLK; /* Offset: 0x34 Variable Clock Pattern Register */ + __IO uint32_t DMA; /* Offset: 0x38 SPI DMA Control Register */ + __IO uint32_t CNTRL2; /* Offset: 0x3C Control and Status Register 2 */ + __IO uint32_t FIFO_CTL; /* Offset: 0x40 SPI FIFO Control Register */ + __IO uint32_t STATUS; /* Offset: 0x44 SPI Status Register */ + +} SPI_T; + + + +/** @addtogroup REG_SPI_BITMASK SPI Bit Mask + @{ + */ + +/* SPI_CNTRL Bit Field Definitions */ +#define SPI_CNTRL_TX_FULL_Pos 27 /*!< SPI_T::CNTRL: TX_FULL Position */ +#define SPI_CNTRL_TX_FULL_Msk (1ul << SPI_CNTRL_TX_FULL_Pos) /*!< SPI_T::CNTRL: TX_FULL Mask */ + +#define SPI_CNTRL_TX_EMPTY_Pos 26 /*!< SPI_T::CNTRL: TX_EMPTY Position */ +#define SPI_CNTRL_TX_EMPTY_Msk (1ul << SPI_CNTRL_TX_EMPTY_Pos) /*!< SPI_T::CNTRL: TX_EMPTY Mask */ + +#define SPI_CNTRL_RX_FULL_Pos 25 /*!< SPI_T::CNTRL: RX_FULL Position */ +#define SPI_CNTRL_RX_FULL_Msk (1ul << SPI_CNTRL_RX_FULL_Pos) /*!< SPI_T::CNTRL: RX_FULL Mask */ + +#define SPI_CNTRL_RX_EMPTY_Pos 24 /*!< SPI_T::CNTRL: RX_EMPTY Position */ +#define SPI_CNTRL_RX_EMPTY_Msk (1ul << SPI_CNTRL_RX_EMPTY_Pos) /*!< SPI_T::CNTRL: RX_EMPTY Mask */ + +#define SPI_CNTRL_VARCLK_EN_Pos 23 /*!< SPI_T::CNTRL: VARCLK_EN Position */ +#define SPI_CNTRL_VARCLK_EN_Msk (1ul << SPI_CNTRL_VARCLK_EN_Pos) /*!< SPI_T::CNTRL: VARCLK_EN Mask */ + +#define SPI_CNTRL_TWOB_Pos 22 /*!< SPI_T::CNTRL: TWOB Position */ +#define SPI_CNTRL_TWOB_Msk (1ul << SPI_CNTRL_TWOB_Pos) /*!< SPI_T::CNTRL: TWOB Mask */ + +#define SPI_CNTRL_FIFO_Pos 21 /*!< SPI_T::CNTRL: FIFO Position */ +#define SPI_CNTRL_FIFO_Msk (1ul << SPI_CNTRL_FIFO_Pos) /*!< SPI_T::CNTRL: FIFO Mask */ + +#define SPI_CNTRL_REORDER_Pos 19 /*!< SPI_T::CNTRL: REORDER Position */ +#define SPI_CNTRL_REORDER_Msk (1ul << SPI_CNTRL_REORDER_Pos) /*!< SPI_T::CNTRL: REORDER Mask */ + +#define SPI_CNTRL_SLAVE_Pos 18 /*!< SPI_T::CNTRL: SLAVE Position */ +#define SPI_CNTRL_SLAVE_Msk (1ul << SPI_CNTRL_SLAVE_Pos) /*!< SPI_T::CNTRL: SLAVE Mask */ + +#define SPI_CNTRL_IE_Pos 17 /*!< SPI_T::CNTRL: IE Position */ +#define SPI_CNTRL_IE_Msk (1ul << SPI_CNTRL_IE_Pos) /*!< SPI_T::CNTRL: IE Mask */ + +#define SPI_CNTRL_IF_Pos 16 /*!< SPI_T::CNTRL: IF Position */ +#define SPI_CNTRL_IF_Msk (1ul << SPI_CNTRL_IF_Pos) /*!< SPI_T::CNTRL: IF Mask */ + +#define SPI_CNTRL_SP_CYCLE_Pos 12 /*!< SPI_T::CNTRL: SP_CYCLE Position */ +#define SPI_CNTRL_SP_CYCLE_Msk (0xFul << SPI_CNTRL_SP_CYCLE_Pos) /*!< SPI_T::CNTRL: SP_CYCLE Mask */ + +#define SPI_CNTRL_CLKP_Pos 11 /*!< SPI_T::CNTRL: CLKP Position */ +#define SPI_CNTRL_CLKP_Msk (1ul << SPI_CNTRL_CLKP_Pos) /*!< SPI_T::CNTRL: CLKP Mask */ + +#define SPI_CNTRL_LSB_Pos 10 /*!< SPI_T::CNTRL: LSB Position */ +#define SPI_CNTRL_LSB_Msk (1ul << SPI_CNTRL_LSB_Pos) /*!< SPI_T::CNTRL: LSB Mask */ + +#define SPI_CNTRL_TX_BIT_LEN_Pos 3 /*!< SPI_T::CNTRL: TX_BIT_LEN Position */ +#define SPI_CNTRL_TX_BIT_LEN_Msk (0x1Ful << SPI_CNTRL_TX_BIT_LEN_Pos) /*!< SPI_T::CNTRL: TX_BIT_LEN Mask */ + +#define SPI_CNTRL_TX_NEG_Pos 2 /*!< SPI_T::CNTRL: TX_NEG Position */ +#define SPI_CNTRL_TX_NEG_Msk (1ul << SPI_CNTRL_TX_NEG_Pos) /*!< SPI_T::CNTRL: TX_NEG Mask */ + +#define SPI_CNTRL_RX_NEG_Pos 1 /*!< SPI_T::CNTRL: RX_NEG Position */ +#define SPI_CNTRL_RX_NEG_Msk (1ul << SPI_CNTRL_RX_NEG_Pos) /*!< SPI_T::CNTRL: RX_NEG Mask */ + +#define SPI_CNTRL_GO_BUSY_Pos 0 /*!< SPI_T::CNTRL: GO_BUSY Position */ +#define SPI_CNTRL_GO_BUSY_Msk (1ul << SPI_CNTRL_GO_BUSY_Pos) /*!< SPI_T::CNTRL: GO_BUSY Mask */ + +/* SPI_DIVIDER Bit Field Definitions */ +#define SPI_DIVIDER_DIVIDER2_Pos 16 /*!< SPI_T::DIVIDER: DIVIDER2 Position */ +#define SPI_DIVIDER_DIVIDER2_Msk (0xFFul << SPI_DIVIDER_DIVIDER2_Pos) /*!< SPI_T::DIVIDER: DIVIDER2 Mask */ + +#define SPI_DIVIDER_DIVIDER_Pos 0 /*!< SPI_T::DIVIDER: DIVIDER Position */ +#define SPI_DIVIDER_DIVIDER_Msk (0xFFul << SPI_DIVIDER_DIVIDER_Pos) /*!< SPI_T::DIVIDER: DIVIDER Mask */ + +/* SPI_SSR Bit Field Definitions */ +#define SPI_SSR_LTRIG_FLAG_Pos 5 /*!< SPI_T::SSR: LTRIG_FLAG Position */ +#define SPI_SSR_LTRIG_FLAG_Msk (1ul << SPI_SSR_LTRIG_FLAG_Pos) /*!< SPI_T::SSR: LTRIG_FLAG Mask */ + +#define SPI_SSR_SS_LTRIG_Pos 4 /*!< SPI_T::SSR: SS_LTRIG Position */ +#define SPI_SSR_SS_LTRIG_Msk (1ul << SPI_SSR_SS_LTRIG_Pos) /*!< SPI_T::SSR: SS_LTRIG Mask */ + +#define SPI_SSR_AUTOSS_Pos 3 /*!< SPI_T::SSR: AUTOSS Position */ +#define SPI_SSR_AUTOSS_Msk (1ul << SPI_SSR_AUTOSS_Pos) /*!< SPI_T::SSR: AUTOSS Mask */ + +#define SPI_SSR_SS_LVL_Pos 2 /*!< SPI_T::SSR: SS_LVL Position */ +#define SPI_SSR_SS_LVL_Msk (1ul << SPI_SSR_SS_LVL_Pos) /*!< SPI_T::SSR: SS_LVL Mask */ + +#define SPI_SSR_SSR_Pos 0 /*!< SPI_T::SSR: SSR Position */ +#define SPI_SSR_SSR_Msk (3ul << SPI_SSR_SSR_Pos) /*!< SPI_T::SSR: SSR Mask */ + +/* SPI_DMA Bit Field Definitions */ +#define SPI_DMA_PDMA_RST_Pos 2 /*!< SPI_T::DMA: PDMA_RST Position */ +#define SPI_DMA_PDMA_RST_Msk (1ul << SPI_DMA_PDMA_RST_Pos) /*!< SPI_T::DMA: PDMA_RST Mask */ + +#define SPI_DMA_RX_DMA_GO_Pos 1 /*!< SPI_T::DMA: RX_DMA_GO Position */ +#define SPI_DMA_RX_DMA_GO_Msk (1ul << SPI_DMA_RX_DMA_GO_Pos) /*!< SPI_T::DMA: RX_DMA_GO Mask */ + +#define SPI_DMA_TX_DMA_GO_Pos 0 /*!< SPI_T::DMA: TX_DMA_GO Position */ +#define SPI_DMA_TX_DMA_GO_Msk (1ul << SPI_DMA_TX_DMA_GO_Pos) /*!< SPI_T::DMA: TX_DMA_GO Mask */ + +/* SPI_CNTRL2 Bit Field Definitions */ +#define SPI_CNTRL2_BCn_Pos 31 /*!< SPI_T::CNTRL2: BCn Position */ +#define SPI_CNTRL2_BCn_Msk (1ul << SPI_CNTRL2_BCn_Pos) /*!< SPI_T::CNTRL2: BCn Mask */ + +#define SPI_CNTRL2_SS_INT_OPT_Pos 16 /*!< SPI_T::CNTRL2: SS_INT_OPT Position */ +#define SPI_CNTRL2_SS_INT_OPT_Msk (1ul << SPI_CNTRL2_SS_INT_OPT_Pos) /*!< SPI_T::CNTRL2: SS_INT_OPT Mask */ + +#define SPI_CNTRL2_DUAL_IO_EN_Pos 13 /*!< SPI_T::CNTRL2: DUAL_IO_EN Position */ +#define SPI_CNTRL2_DUAL_IO_EN_Msk (1ul << SPI_CNTRL2_DUAL_IO_EN_Pos) /*!< SPI_T::CNTRL2: DUAL_IO_EN Mask */ + +#define SPI_CNTRL2_DUAL_IO_DIR_Pos 12 /*!< SPI_T::CNTRL2: DUAL_IO_DIR Position */ +#define SPI_CNTRL2_DUAL_IO_DIR_Msk (1ul << SPI_CNTRL2_DUAL_IO_DIR_Pos) /*!< SPI_T::CNTRL2: DUAL_IO_DIR Mask */ + +#define SPI_CNTRL2_SLV_START_INTSTS_Pos 11 /*!< SPI_T::CNTRL2: SLV_START_INTSTS Position */ +#define SPI_CNTRL2_SLV_START_INTSTS_Msk (1ul << SPI_CNTRL2_SLV_START_INTSTS_Pos) /*!< SPI_T::CNTRL2: SLV_START_INTSTS Mask */ + +#define SPI_CNTRL2_SSTA_INTEN_Pos 10 /*!< SPI_T::CNTRL2: SSTA_INTEN Position */ +#define SPI_CNTRL2_SSTA_INTEN_Msk (1ul << SPI_CNTRL2_SSTA_INTEN_Pos) /*!< SPI_T::CNTRL2: SSTA_INTEN Mask */ + +#define SPI_CNTRL2_SLV_ABORT_Pos 9 /*!< SPI_T::CNTRL2: SLV_ABORT Position */ +#define SPI_CNTRL2_SLV_ABORT_Msk (1ul << SPI_CNTRL2_SLV_ABORT_Pos) /*!< SPI_T::CNTRL2: SLV_ABORT Mask */ + +#define SPI_CNTRL2_NOSLVSEL_Pos 8 /*!< SPI_T::CNTRL2: NOSLVSEL Position */ +#define SPI_CNTRL2_NOSLVSEL_Msk (1ul << SPI_CNTRL2_NOSLVSEL_Pos) /*!< SPI_T::CNTRL2: NOSLVSEL Mask */ + +/* SPI_FIFO_CTL Bit Field Definitions */ +#define SPI_FIFO_CTL_TX_THRESHOLD_Pos 28 /*!< SPI_T::FIFO_CTL: TX_THRESHOLD Position */ +#define SPI_FIFO_CTL_TX_THRESHOLD_Msk (7ul << SPI_FIFO_CTL_TX_THRESHOLD_Pos) /*!< SPI_T::FIFO_CTL: TX_THRESHOLD Mask */ + +#define SPI_FIFO_CTL_RX_THRESHOLD_Pos 24 /*!< SPI_T::FIFO_CTL: RX_THRESHOLD Position */ +#define SPI_FIFO_CTL_RX_THRESHOLD_Msk (7ul << SPI_FIFO_CTL_RX_THRESHOLD_Pos) /*!< SPI_T::FIFO_CTL: RX_THRESHOLD Mask */ + +#define SPI_FIFO_CTL_TIMEOUT_INTEN_Pos 21 /*!< SPI_T::FIFO_CTL: TIMEOUT_INTEN Position */ +#define SPI_FIFO_CTL_TIMEOUT_INTEN_Msk (1ul << SPI_FIFO_CTL_TIMEOUT_INTEN_Pos) /*!< SPI_T::FIFO_CTL: TIMEOUT_INTEN Mask */ + +#define SPI_FIFO_CTL_RXOV_INTEN_Pos 6 /*!< SPI_T::FIFO_CTL: RXOV_INTEN Position */ +#define SPI_FIFO_CTL_RXOV_INTEN_Msk (1ul << SPI_FIFO_CTL_RXOV_INTEN_Pos) /*!< SPI_T::FIFO_CTL: RXOV_INTEN Mask */ + +#define SPI_FIFO_CTL_TX_INTEN_Pos 3 /*!< SPI_T::FIFO_CTL: TX_INTEN Position */ +#define SPI_FIFO_CTL_TX_INTEN_Msk (1ul << SPI_FIFO_CTL_TX_INTEN_Pos) /*!< SPI_T::FIFO_CTL: TX_INTEN Mask */ + +#define SPI_FIFO_CTL_RX_INTEN_Pos 2 /*!< SPI_T::FIFO_CTL: RX_INTEN Position */ +#define SPI_FIFO_CTL_RX_INTEN_Msk (1ul << SPI_FIFO_CTL_RX_INTEN_Pos) /*!< SPI_T::FIFO_CTL: RX_INTEN Mask */ + +#define SPI_FIFO_CTL_TX_CLR_Pos 1 /*!< SPI_T::FIFO_CTL: TX_CLR Position */ +#define SPI_FIFO_CTL_TX_CLR_Msk (1ul << SPI_FIFO_CTL_TX_CLR_Pos) /*!< SPI_T::FIFO_CTL: TX_CLR Mask */ + +#define SPI_FIFO_CTL_RX_CLR_Pos 0 /*!< SPI_T::FIFO_CTL: RX_CLR Position */ +#define SPI_FIFO_CTL_RX_CLR_Msk (1ul << SPI_FIFO_CTL_RX_CLR_Pos) /*!< SPI_T::FIFO_CTL: RX_CLR Mask */ + +/* SPI_STATUS Bit Field Definitions */ +#define SPI_STATUS_TX_FIFO_COUNT_Pos 28 /*!< SPI_T::STATUS: TX_FIFO_COUNT Position */ +#define SPI_STATUS_TX_FIFO_COUNT_Msk (0xFul << SPI_STATUS_TX_FIFO_COUNT_Pos) /*!< SPI_T::STATUS: TX_FIFO_COUNT Mask */ + +#define SPI_STATUS_TX_FULL_Pos 27 /*!< SPI_T::STATUS: TX_FULL Position */ +#define SPI_STATUS_TX_FULL_Msk (1ul << SPI_STATUS_TX_FULL_Pos) /*!< SPI_T::STATUS: TX_FULL Mask */ + +#define SPI_STATUS_TX_EMPTY_Pos 26 /*!< SPI_T::STATUS: TX_EMPTY Position */ +#define SPI_STATUS_TX_EMPTY_Msk (1ul << SPI_STATUS_TX_EMPTY_Pos) /*!< SPI_T::STATUS: TX_EMPTY Mask */ + +#define SPI_STATUS_RX_FULL_Pos 25 /*!< SPI_T::STATUS: RX_FULL Position */ +#define SPI_STATUS_RX_FULL_Msk (1ul << SPI_STATUS_RX_FULL_Pos) /*!< SPI_T::STATUS: RX_FULL Mask */ + +#define SPI_STATUS_RX_EMPTY_Pos 24 /*!< SPI_T::STATUS: RX_EMPTY Position */ +#define SPI_STATUS_RX_EMPTY_Msk (1ul << SPI_STATUS_RX_EMPTY_Pos) /*!< SPI_T::STATUS: RX_EMPTY Mask */ + +#define SPI_STATUS_TIMEOUT_Pos 20 /*!< SPI_T::STATUS: TIMEOUT Position */ +#define SPI_STATUS_TIMEOUT_Msk (1ul << SPI_STATUS_TIMEOUT_Pos) /*!< SPI_T::STATUS: TIMEOUT Mask */ + +#define SPI_STATUS_IF_Pos 16 /*!< SPI_T::STATUS: IF Position */ +#define SPI_STATUS_IF_Msk (1ul << SPI_STATUS_IF_Pos) /*!< SPI_T::STATUS: IF Mask */ + +#define SPI_STATUS_RX_FIFO_COUNT_Pos 12 /*!< SPI_T::STATUS: RX_FIFO_COUNT Position */ +#define SPI_STATUS_RX_FIFO_COUNT_Msk (0xFul << SPI_STATUS_RX_FIFO_COUNT_Pos) /*!< SPI_T::STATUS: RX_FIFO_COUNT Mask */ + +#define SPI_STATUS_SLV_START_INTSTS_Pos 11 /*!< SPI_T::STATUS: SLV_START_INTSTS Position */ +#define SPI_STATUS_SLV_START_INTSTS_Msk (1ul << SPI_STATUS_SLV_START_INTSTS_Pos) /*!< SPI_T::STATUS: SLV_START_INTSTS Mask */ + +#define SPI_STATUS_TX_INTSTS_Pos 4 /*!< SPI_T::STATUS: TX_INTSTS Position */ +#define SPI_STATUS_TX_INTSTS_Msk (1ul << SPI_STATUS_TX_INTSTS_Pos) /*!< SPI_T::STATUS: TX_INTSTS Mask */ + +#define SPI_STATUS_RX_OVERRUN_Pos 2 /*!< SPI_T::STATUS: RX_OVERRUN Position */ +#define SPI_STATUS_RX_OVERRUN_Msk (1ul << SPI_STATUS_RX_OVERRUN_Pos) /*!< SPI_T::STATUS: RX_OVERRUN Mask */ + +#define SPI_STATUS_RX_INTSTS_Pos 0 /*!< SPI_T::STATUS: RX_INTSTS Position */ +#define SPI_STATUS_RX_INTSTS_Msk (1ul << SPI_STATUS_RX_INTSTS_Pos) /*!< SPI_T::STATUS: RX_INTSTS Mask */ +/*@}*/ /* end of group REG_SPI_BITMASK */ +/*@}*/ /* end of group REG_SPI */ + + +/*---------------------------- Global Controller -----------------------------*/ +/** @addtogroup REG_SYS System Manger Controller (SYS) + Memory Mapped Structure for System Controller + @{ + */ + + +typedef struct +{ + + + +/** + * @var GCR_T::PDID + * Offset: 0x00 Part Device Identification Number Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |PDID |Part Device Identification Number + * | | |This register reflects device part number code. + * | | |Software can read this register to identify which device is used. + * @var GCR_T::RSTSRC + * Offset: 0x04 System Reset Source Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RSTS_POR |Power-on Reset Flag + * | | |The RSTS_POR flag is set by the "Reset Signal" from the Power-On Reset (POR) controller or bit CHIP_RST (IPRSTC1[0]) to indicate the previous reset source. + * | | |0 = No reset from POR or CHIP_RST (IPRSTC1[0]). + * | | |1 = Power-on Reset (POR) or CHIP_RST (IPRSTC1[0]) had issued the reset signal to reset the system. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[1] |RSTS_RESET|Reset Pin Reset Flag + * | | |The RSTS_RESET flag is set by the "Reset Signal" from the nRESET Pin to indicate the previous reset source. + * | | |0 = No reset from nRESET pin. + * | | |1 = The Pin nRESET had issued the reset signal to reset the system. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[2] |RSTS_WDT |Watchdog Reset Flag + * | | |The RSTS_WDT flag is set by the "Reset Signal" from the Watchdog Timer to indicate the previous reset source + * | | |0 = No reset from watchdog timer. + * | | |1 = The watchdog timer had issued the reset signal to reset the system. + * | | |Note1: This bit can be cleared by writing "1" to it. + * | | |Note2: + * | | |Watchdog Timer register WTRF (WTCR[2]) bit is set if the system has been reset by WDT time-out reset. + * | | |Window Watchdog Timer register WWDTRF (WWDTSR[1]) bit is set if the system has been reset by WWDT time-out reset. + * |[3] |RSTS_LVR |Low Voltage Reset Flag + * | | |The RSTS_LVR flag is set by the "Reset Signal" from the Low-Voltage-Reset controller to indicate the previous reset source. + * | | |0 = No reset from LVR. + * | | |1 = The LVR controller had issued the reset signal to reset the system. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[4] |RSTS_BOD |Brown-out Detector Reset Flag + * | | |The RSTS_BOD flag is set by the "Reset Signal" from the Brown-out Detector to indicate the previous reset source. + * | | |0 = No reset from BOD. + * | | |1 = The BOD had issued the reset signal to reset the system. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[5] |RSTS_SYS |SYS Reset Flag + * | | |The RSTS_SYS flag is set by the "Reset Signal" from the Cortex-M0 kernel to indicate the previous reset source. + * | | |0 = No reset from Cortex-M0. + * | | |1 = The Cortex-M0 had issued the reset signal to reset the system by writing 1 to bit SYSRESETREQ (AIRCR[2], Application Interrupt and Reset Control Register, address = 0xE000ED0C) in system control registers of Cortex-M0 kernel. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[7] |RSTS_CPU |CPU Reset Flag + * | | |The RSTS_CPU flag is set by hardware if software writes CPU_RST (IPRSTC1[1]) 1 to reset Cortex-M0 CPU kernel and flash Memory Controller (FMC) + * | | |0 = No reset from CPU. + * | | |1 = Cortex-M0 CPU kernel and FMC are reset by software setting CPU_RST (IPRSTC1[1]) to 1. + * | | |Note: This bit can be cleared by writing "1" to it. + * @var GCR_T::IPRSTC1 + * Offset: 0x08 Peripheral Reset Control Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CHIP_RST |CHIP One-shot Reset (Write Protect) + * | | |Setting this bit will reset the whole chip, including CPU kernel and all peripherals, and this bit will automatically return to 0 after the 2 clock cycles. + * | | |The CHIP_RST is the same as the POR reset, all the chip controllers are reset and the chip setting from flash are also reload. + * | | |0 = CHIP normal operation. + * | | |1 = CHIP one-shot reset. + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * |[1] |CPU_RST |CPU Kernel One-shot Reset (Write Protect) + * | | |Setting this bit will only reset the CPU kernel and Flash Memory Controller(FMC), and this bit will automatically return 0 after the two clock cycles + * | | |0 = CPU normal operation. + * | | |1 = CPU one-shot reset. + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * |[2] |PDMA_RST |PDMA Controller Reset (Write Protect) + * | | |Setting this bit to 1 will generate a reset signal to the PDMA. User need to set this bit to 0 to release from reset state. + * | | |0 = PDMA controller normal operation. + * | | |1 = PDMA controller reset. + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * @var GCR_T::IPRSTC2 + * Offset: 0x0C Peripheral Reset Control Register 2 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1] |GPIO_RST |GPIO Controller Reset + * | | |0 = GPIO controller normal operation. + * | | |1 = GPIO controller reset. + * |[2] |TMR0_RST |Timer0 Controller Reset + * | | |0 = Timer0 controller normal operation. + * | | |1 = Timer0 controller reset. + * |[3] |TMR1_RST |Timer1 Controller Reset + * | | |0 = Timer1 controller normal operation. + * | | |1 = Timer1 controller reset. + * |[4] |TMR2_RST |Timer2 Controller Reset + * | | |0 = Timer2 controller normal operation. + * | | |1 = Timer2 controller reset. + * |[5] |TMR3_RST |Timer3 Controller Reset + * | | |0 = Timer3 controller normal operation. + * | | |1 = Timer3 controller reset. + * |[8] |I2C0_RST |I2C0 Controller Reset + * | | |0 = I2C0 controller normal operation. + * | | |1 = I2C0 controller reset. + * |[9] |I2C1_RST |I2C1 Controller Reset + * | | |0 = I2C1 controller normal operation. + * | | |1 = I2C1 controller reset. + * |[12] |SPI0_RST |SPI0 Controller Reset + * | | |0 = SPI0 controller normal operation. + * | | |1 = SPI0 controller reset. + * |[13] |SPI1_RST |SPI1 Controller Reset + * | | |0 = SPI1 controller normal operation. + * | | |1 = SPI1 controller reset. + * |[14] |SPI2_RST |SPI2 Controller Reset + * | | |0 = SPI2 controller normal operation. + * | | |1 = SPI2 controller reset. + * |[16] |UART0_RST |UART0 Controller Reset + * | | |0 = UART0 controller normal operation. + * | | |1 = UART0 controller reset. + * |[17] |UART1_RST |UART1 Controller Reset + * | | |0 = UART1 controller normal operation. + * | | |1 = UART1 controller reset. + * |[20] |PWM03_RST |PWM03 Controller Reset + * | | |0 = PWM03 controller normal operation. + * | | |1 = PWM03 controller reset. + * |[23] |PS2_RST |PS/2 Controller Reset + * | | |0 = PS/2 controller normal operation. + * | | |1 = PS/2 controller reset. + * |[27] |USBD_RST |USB Device Controller Reset + * | | |0 = USB device controller normal operation. + * | | |1 = USB device controller reset. + * |[28] |ADC_RST |ADC Controller Reset + * | | |0 = ADC controller normal operation. + * | | |1 = ADC controller reset. + * |[29] |I2S_RST |I2S Controller Reset + * | | |0 = I2S controller normal operation. + * | | |1 = I2S controller reset. + * @var GCR_T::BODCR + * Offset: 0x18 Brown-out Detector Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |BOD_EN |Brown-out Detector Enable Bit (Write Protect) + * | | |The default value is set by flash controller user configuration register CBODEN (Config0[23]) bit. + * | | |0 = Brown-out Detector function Disabled. + * | | |1 = Brown-out Detector function Enabled. + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * |[2:1] |BOD_VL |Brown-out Detector Threshold Voltage Selection (Write Protect) + * | | |The default value is set by flash memory controller user configuration register CBOV (Config0[22:21]) bits. + * | | |00 = Brown-out voltage is 2.2V. + * | | |01 = Brown-out voltage is 2.7V. + * | | |10 = Brown-out voltage is 3.7V. + * | | |11 = Brown-out voltage is 4.4V. + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * |[3] |BOD_RSTEN |Brown-out Reset Enable Bit (Write Protect) + * | | |0 = Brown-out "INTERRUPT" function Enabled. + * | | |1 = Brown-out "RESET" function Enabled. + * | | |While the Brown-out Detector function is enabled (BOD_EN high) and BOD reset function is enabled (BOD_RSTEN high), BOD will assert a signal to reset chip when the detected voltage is lower than the threshold (BOD_OUT high). + * | | |Note1: While the BOD function is enabled (BOD_EN high) and BOD interrupt function is enabled (BOD_RSTEN low), BOD will assert an interrupt if BOD_OUT is high. + * | | |BOD interrupt will keep till to the BOD_EN set to 0. + * | | |BOD interrupt can be blocked by disabling the NVIC BOD interrupt or disabling BOD function (set BOD_EN low). + * | | |Note2: The default value is set by flash controller user configuration register CBORST (Config0[20]) bit. + * | | |Note3: This bit is write protected. Refer to the REGWRPROT register. + * |[4] |BOD_INTF |Brown-out Detector Interrupt Flag + * | | |0 = Brown-out Detector does not detect any voltage draft at VDD down through or up through the voltage of BOD_VL setting. + * | | |1 = When Brown-out Detector detects the VDD is dropped down through the voltage of BOD_VL setting or the VDD is raised up through the voltage of BOD_VL setting, this bit is set to 1 and the Brown-out interrupt is requested if Brown-out interrupt is enabled. + * | | |Note: This bit can be cleared to 0 by software writing "1". + * |[5] |BOD_LPM |Brown-out Detector Low Power Mode (Write Protect) + * | | |0 = BOD operated in Normal mode (default). + * | | |1 = BOD Low Power mode Enabled. + * | | |Note1: The BOD consumes about 100 uA in Normal mode, and the low power mode can reduce the current to about 1/10 but slow the BOD response. + * | | |Note2: This bit is write protected. Refer to the REGWRPROT register. + * |[6] |BOD_OUT |Brown-out Detector Output Status + * | | |0 = Brown-out Detector output status is 0. It means the detected voltage is higher than BOD_VL setting or BOD_EN is 0. + * | | |1 = Brown-out Detector output status is 1. It means the detected voltage is lower than BOD_VL setting. If the BOD_EN is 0, BOD function disabled , this bit always responds to 0. + * |[7] |LVR_EN |Low Voltage Reset Enable Bit (Write Protect) + * | | |The LVR function reset the chip when the input power voltage is lower than LVR circuit setting. + * | | |LVR function is enabled by default. + * | | |0 = Low Voltage Reset function Disabled. + * | | |1 = Low Voltage Reset function Enabled - After enabling the bit, the LVR function will be active with 100us delay for LVR output stable (default). + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * @var GCR_T::PORCR + * Offset: 0x24 Power-on-Reset Controller Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |POR_DIS_CODE|Power-on-Reset Enable Bis (Write Protect) + * | | |When powered 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 internal POR circuit to avoid unpredictable noise to cause chip reset by writing 0x5AA5 to this field. + * | | |The POR function will be active again when this field is set to another value or chip is reset by other reset source, including: nRESET, Watchdog, LVR reset, BOD reset, ICE reset command and the software-chip reset function. + * | | |Note: This bit is write protected. Refer to the REGWRPROT register. + * @var GCR_T::GPA_MFP + * Offset: 0x30 GPIOA Multiple Function and Input Type Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[10] |GPA_MFP10 |PA.10 Pin Function Selection + * | | |Bits PA10_MFP1 (ALT_MFP[12]) and GPA_MFP[10] determine the PA.10 function. + * | | |(PA10_MFP1 (ALT_MFP[12]), GPA_MFP[10]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = I2C1_SDA function is selected. + * | | |(1, 0) = SPI1_MISO0 function is selected. + * | | |(1, 1) = SPI2_MISO0 function is selected. + * |[11] |GPA_MFP11 |PA.11 Pin Function Selection + * | | |Bits PA11_MFP1 (ALT_MFP[11]) and GPA_MFP[11] determine the PA.11 function. + * | | |(PA11_MFP1 (ALT_MFP[11]), GPA_MFP[11]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = I2C1_SCL function is selected. + * | | |(1, 0) = SPI1_CLK function is selected. + * | | |(1, 1) = SPI2_MOSI0 function is selected. + * |[12] |GPA_MFP12 |PA.12 Pin Function Selection + * | | |Bit GPA_MFP[12] determines the PA.12 function. + * | | |0 = GPIO function is selected. + * | | |1 = PWM0 function is selected. + * |[13] |GPA_MFP13 |PA.13 Pin Function Selection + * | | |Bit GPA_MFP[13] determines the PA.13 function. + * | | |0 = GPIO function is selected. + * | | |1 = PWM1 function is selected. + * |[14] |GPA_MFP14 |PA.14 Pin Function Selection + * | | |Bit GPA_MFP[14] determines the PA.14 function. + * | | |0 = GPIO function is selected. + * | | |1 = PWM2 function is selected. + * |[15] |GPA_MFP15 |PA.15 Pin Function Selection + * | | |Bits PA15_MFP1 (ALT_MFP[9]) and GPA_MFP[15] determine the PA.15 function. + * | | |(PA15_MFP1 (ALT_MFP[9]), GPA_MFP[15]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = PWM3 function is selected. + * | | |(1, 0) = CLKO function is selected. + * | | |(1, 1) = I2S_MCLK function is selected. + * |[31:16] |GPA_TYPEn |Schmitt Trigger Function Selection + * | | |0 = GPIOA[15:0] I/O input Schmitt Trigger function Disabled. + * | | |1 = GPIOA[15:0] I/O input Schmitt Trigger function Enabled. + * @var GCR_T::GPB_MFP + * Offset: 0x34 GPIOB Multiple Function and Input Type Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GPB_MFP0 |PB.0 Pin Function Selection + * | | |Bit GPB_MFP[0] determines the PB.0 function. + * | | |0 = GPIO function is selected. + * | | |1 = UART0_RXD function is selected. + * |[1] |GPB_MFP1 |PB.1 Pin Function Selection + * | | |Bit GPB_MFP[1] determines the PB.1 function. + * | | |0 = GPIO function is selected. + * | | |1 = UART0_TXD0 function is selected. + * |[2] |GPB_MFP2 |PB.2 Pin Function Selection + * | | |Bits PB2_MFP1(ALT_MFP[26]) and GPB_MFP[2] determine the PB.2 function. + * | | |(PB2_MFP1 (ALT_MFP[26]), GPB_MFP[2]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART0_nRTS function is selected. + * | | |(1, 1) = TM2_EXT function is selected. + * |[3] |GPB_MFP3 |PB.3 Pin Function Selection + * | | |Bits PB3_MFP1 (ALT_MFP[27]) and GPB_MFP[3] determine the PB.3 function. + * | | |(PB3_MFP1 (ALT_MFP[27]), GPB_MFP[3]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART0_nCTS function is selected. + * | | |(1, 1) = TM3_EXT function is selected. + * |[4] |GPB_MFP4 |PB.4 Pin Function Selection + * | | |Bits PB4_MFP1 (ALT_MFP[15]) and GPB_MFP[4] determine the PB.4 function. + * | | |(PB4_MFP1 (ALT_MFP[15]), GPB_MFP[4]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_RXD function is selected. + * | | |(1, 0) = SPI2_SS0 function is selected. + * | | |(1, 1) = SPI1_SS1 function is selected. + * |[5] |GPB_MFP5 |PB. 5 Pin Function Selection + * | | |Bits PB5_MFP1 (ALT_MFP[18]) and GPB_MFP[5] determine the PB.5 function. + * | | |(PB5_MFP1 (ALT_MFP[18]), GPB_MFP[5]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_TXD function is selected. + * | | |(1, 1) = SPI2_CLK function is selected. + * |[6] |GPB_MFP6 |PB.6 Pin Function Selection + * | | |Bits PB6_MFP1 (ALT_MFP[17]) and GPB_MFP[6] determine the PB.6 function. + * | | |(PB6_MFP1 (ALT_MFP[17]), GPB_MFP[6]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_nRTS function is selected. + * | | |(1, 1) = SPI2_MOSI0 function is selected. + * |[7] |GPB_MFP7 |PB.7 Pin Function Selection + * | | |Bits PB7_MFP1 (ALT_MFP[16]) and GPB_MFP[7] determine the PB.7 function. + * | | |(PB7_MFP1 (ALT_MFP[16]), GPB_MFP[7]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_nCTS function is selected. + * | | |(1, 1) = SPI2_MISO0 function is selected. + * |[8] |GPB_MFP8 |PB.8 Pin Function Selection + * | | |Bit GPB_MFP[8] determines the PB.8 function. + * | | |0 = GPIO function is selected. + * | | |1 = TM0 function is selected. + * |[9] |GPB_MFP9 |PB.9 Pin Function Selection + * | | |Bits PB9_MFP1 (ALT_MFP[1]) and GPB_MFP[9] determine the PB.9 function. + * | | |(PB9_MFP1 (ALT_MFP[1]), GPB_MFP[9]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = TM1 function is selected. + * | | |(1, 0) = PWM1 function is selected. (NUC123xxxAEx Only) + * | | |(1, 1) = SPI1_SS1 function is selected. + * |[10] |GPB_MFP10 |PB.10 Pin Function Selection + * | | |Bit GPB_MFP[10] determines the PB.10 function. + * | | |(PB10_MFP1 (ALT_MFP[0]), GPB_MFP[10]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = TM2 function is selected. + * | | |(1, 1) = SPI0_SS1 function is selected. + * |[12] |GPB_MFP12 |PB.12 Pin Function Selection + * | | |Bits PB12_MFP1 (ALT_MFP[10]) and GPB_MFP[12] determine the PB.12 function. + * | | |(PB12_MFP1 (ALT_MFP[10]), GPB_MFP[12]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_SS0 function is selected. + * | | |(1, 1) = CLKO function is selected. + * |[13] |GPB_MFP13 |PB.13 Pin Function Selection + * | | |Bit GPB_MFP[13] determines the PB.13 function. + * | | |0 = GPIO function is selected. + * |[14] |GPB_MFP14 |PB.14 Pin Function Selection + * | | |Bit GPB_MFP[14] determines PB.14 function. + * | | |0 = GPIO function is selected. + * | | |1 = INT0 function is selected. + * |[15] |GPB_MFP15 |PB.15 Pin Function Selection + * | | |Bits PB15_MFP1 (ALT_MFP[24]) and GPB_MFP[15] determine the PB.15 function. + * | | |(PB15_MFP1 (ALT_MFP[24]), GPB_MFP[15]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = INT1 function is selected. + * | | |(1, 1) = TM0_EXT function is selected. + * |[31:16] |GPB_TYPEn |Schmitt Trigger Function Selection + * | | |0 = GPIOB[15:0] I/O input Schmitt Trigger function Disabled. + * | | |1 = GPIOB[15:0] I/O input Schmitt Trigger function Enabled. + * @var GCR_T::GPC_MFP + * Offset: 0x38 GPIOC Multiple Function and Input Type Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GPC_MFP0 |PC.0 Pin Function Selection + * | | |Bits PC0_MFP1 (ALT_MFP[5]) and GPC_MFP[0] determine the PC.0 function. + * | | |(PC0_MFP1 (ALT_MFP[5]), GPC_MFP[0]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_SS0 function is selected. + * | | |(1, 1) = I2S_LRCLK function is selected. + * |[1] |GPC_MFP1 |PC.1 Pin Function Selection + * | | |Bits PC1_MFP1 (ALT_MFP[6]) and GPC_MFP[1] determine the PC.1 function. + * | | |(PC1_MFP1 (ALT_MFP[6]), GPC_MFP[1]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_CLK function is selected. + * | | |(1, 1) = I2S_BCLK function is selected. + * |[2] |GPC_MFP2 |PC.2 Pin Function Selection + * | | |Bits PC2_MFP1 (ALT_MFP[7]) and GPC_MFP[2] determine the PC.2 function. + * | | |(PC2_MFP1 (ALT_MFP[7]), GPC_MFP[2]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MISO0 function is selected. + * | | |(1, 1) = I2S_DI function is selected. + * |[3] |GPC_MFP3 |PC.3 Pin Function Selection + * | | |Bits PC3_MFP1 (ALT_MFP[8]) and GPC_MFP[3] determine the PC.3 function. + * | | |(PC3_MFP1 (ALT_MFP[8]), GPC_MFP[3]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MOSI0 function is selected. + * | | |(1, 1) = I2S_DO function is selected. + * |[4] |GPC_MFP4 |PC.4 Pin Function Selection + * | | |Bits PC4_MFP1 (ALT_MFP[29]) and GPC_MFP[4] determine the PC.4 function. + * | | |(PC4_MFP1 (ALT_MFP[29]), GPC_MFP[4]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MISO1 function is selected. + * | | |(1, 1) = UART0_RXD function is selected. + * |[5] |GPC_MFP5 |PC.5 Pin Function Selection + * | | |Bits PC5_MFP1 (ALT_MFP[30]) and GPC_MFP[5] determine the PC.5 function. + * | | |(PC5_MFP1 (ALT_MFP[30]), GPC_MFP[5]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MOSI1 function is selected. + * | | |(1, 1) = UART0_TXD function is selected. + * |[8] |GPC_MFP8 |PC.8 Pin Function Selection + * | | |Bits PC8_MFP1 (ALT_MFP1[23]) and GPC_MFP[8] determine the PC.8 function. + * | | |(PC8_MFP1 (ALT_MFP1[23]), GPC_MFP[8]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_SS0 function is selected. + * | | |(1, 1) = PWM0 function is selected. (NUC123xxxAEx Only) + * |[9] |GPC_MFP9 |PC.9 Pin Function Selection + * | | |Bit GPC_MFP[9] determines the PC.9 function. + * | | |0 = GPIO function is selected. + * | | |1 = SPI1_CLK function is selected. + * |[10] |GPC_MFP10 |PC.10 Pin Function Selection + * | | |Bit GPC_MFP[10] determines the PC.10 function. + * | | |0 = GPIO function is selected. + * | | |1 = SPI1_MISO0 function is selected. + * |[11] |GPC_MFP11 |PC.11 Pin Function Selection + * | | |Bit GPC_MFP[11] determines the PC.11 function. + * | | |0 = GPIO function is selected. + * | | |1 = SPI1_MOSI0 function is selected. + * |[12] |GPC_MFP12 |PC.12 Pin Function Selection + * | | |Bits PC12_MFP1 (ALT_MFP[20]) and GPC_MFP[12] determine the PC.12 function. + * | | |(PC12_MFP1 (ALT_MFP[20]), GPC_MFP[12]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_MISO1 function is selected. + * | | |(1, 0) = I2S_MCLK function is selected. + * | | |(1, 1) = PWM2 function is selected. + * |[13] |GPC_MFP13 |PC.13 Pin Function Selection + * | | |Bits PC13_MFP1 (ALT_MFP[21]) and GPC_MFP[13] determine the PC.13 function. + * | | |(PC13_MFP1 (ALT_MFP[21]), GPC_MFP[13]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_MOSI1 function is selected. + * | | |(1, 0) = CLKO function is selected. + * | | |(1, 1) = PWM3 function is selected. + * |[31:16] |GPC_TYPEn |Schmitt Trigger Function Selection + * | | |0 = GPIOC[15:0] I/O input Schmitt Trigger function Disabled. + * | | |1 = GPIOC[15:0] I/O input Schmitt Trigger function Enabled. + * @var GCR_T::GPD_MFP + * Offset: 0x3C GPIOD Multiple Function and Input Type Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GPD_MFP0 |PD.0 Pin Function Selection + * | | |Bits PD0_MFP1 (ALT_MFP1[16]) and GPD_MFP[0] determine the PD.0 function. + * | | |(PD0_MFP1 (ALT_MFP1[16]), GPD_MFP[0]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(1, 0) = SPI2_SS0 function is selected. + * | | |(1, 1) = ADC0 function is selected. + * |[1] |GPD_MFP1 |PD.1 Pin Function Selection + * | | |Bits PD1_MFP1 (ALT_MFP1[17]) and GPD_MFP[1] determine the PD.1 function. + * | | |(PD1_MFP1 (ALT_MFP1[17]), GPD_MFP[1]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_SS1 function is selected. + * | | |(1, 0) = SPI2_CLK function is selected. + * | | |(1, 1) = ADC1 function is selected. + * |[2] |GPD_MFP2 |PD.2 Pin Function Selection + * | | |Bits PD2_MFP1 (ALT_MFP1[18]) and GPD_MFP[2] determine the PD.2 function. + * | | |(PD2_MFP1 (ALT_MFP1[18]), GPD_MFP[2]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MISO1 function is selected. + * | | |(1, 0) = SPI2_MISO0 function is selected. + * | | |(1, 1) = ADC2 function is selected. + * |[3] |GPD_MFP3 |PD.3 Pin Function Selection + * | | |Bits PD3_MFP1 (ALT_MFP1[19]) and GPD_MFP[3] determine the PD.3 function. + * | | |(PD3_MPF1 (ALT_MFP1[19]), GPD_MFP[3]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MOSI1 function is selected. + * | | |(1, 0) = SPI2_MOSI0 function is selected. + * | | |(1, 1) = ADC3 function is selected. + * |[4] |GPD_MFP4 |PD.4 Pin Function Selection + * | | |Bits PD4_MFP1 (ALT_MFP1[20]) and GPD_MFP[4] determine the PD.4 function. + * | | |(PD4_MFP1 (ALT_MFP1[20]), GPD_MFP[4]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(1, 0) = SPI2_MISO1 function is selected. + * | | |(1, 1) = ADC4 function is selected. + * |[5] |GPD_MFP5 |PD.5 Pin Function Selection + * | | |Bits PD5_MFP1 (ALT_MFP1[21]) and GPD_MFP[5] determine the PD.5 function. + * | | |(PD5_MFP1 (ALT_MFP1[21]), GPD_MFP[5]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(1, 0) = SPI2_MOSI1 function is selected. + * | | |(1, 1) = ADC5 function is selected. + * |[8] |GPD_MFP8 |PD.8 Pin Function Selection + * | | |Bit GPD_MFP[8] determines the PD.8 function. + * | | |0 = GPIO function is selected. + * | | |1 = SPI1_MOSI0 function is selected. + * |[9] |GPD_MFP9 |PD.9 Pin Function Selection + * | | |it GPD_MFP[9] determines the PD.9 function. + * | | |0 = GPIO function is selected. + * |[10] |GPD_MFP10 |PD.10 Pin Function Selection + * | | |Bit GPD_MFP[10] determines the PD.10 function. + * | | |0 = GPIO function is selected. + * | | |1 = CLKO function is selected. + * |[11] |GPD_MFP11 |PD.11 Pin Function Selection + * | | |Bit GPD_MFP[11] determines the PD.11 function. + * | | |0 = GPIO function is selected. + * | | |1 = INT1 function is selected. + * |[31:16] |GPD_TYPEn |Schmitt Trigger Function Selection + * | | |0 = GPIOD[15:0] I/O input Schmitt Trigger function Disabled. + * | | |1 = GPIOD[15:0] I/O input Schmitt Trigger function Enabled. + * @var GCR_T::GPF_MFP + * Offset: 0x44 GPIOF Multiple Function and Input Type Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |GPF_MFP0 |PF.0 Pin Function Selection + * | | |Bit GPF_MFP[0] determines the PF.0 function. + * | | |0 = GPIO function is selected. + * | | |1 = XT1_OUT function is selected. + * | | |Note1: For NUC123xxxANx, the value of this bit is controlled by CGPFMFP (Config0[27]) when power-up, user can write this bit after chip power-up. + * | | |Note2: For NUC123xxxAEx, the value of this bit is controlled by CGPFMFP (Config0[27]). + * |[1] |GPF_MFP1 |PF.1 Pin Function Selection + * | | |Bit GPF_MFP[1] determines the PF.1 function. + * | | |0 = GPIO function is selected. + * | | |1 = XT1_IN function is selected. + * | | |Note1: For NUC123xxxANx, the value of this bit is controlled by CGPFMFP (Config0[27]) when power-up, user can write this bit after chip power-up. + * | | |Note2: For NUC123xxxAEx, the value of this bit is controlled by CGPFMFP (Config0[27]). + * |[2] |GPF_MFP2 |PF.2 Pin Function Selection + * | | |Bits PF2_MFP1 (ALT_MFP1[25:24]) and GPF_MFP[2] determine the PF.2 function. + * | | |(PF2_MFP1 (ALT_MFP1[25:24]), GPF_MFP[2]) value and function mapping are as following list. + * | | |(00, 0) = GPIO function is selected. + * | | |(00, 1) = PS2_DAT function is selected. + * | | |(10, 1) = I2C0_SDA function is selected. + * | | |(11, 1) = ADC6 function is selected. + * | | |The reset value of this bit is 1. + * |[3] |GPF_MFP3 |PF.3 Pin Function Selection + * | | |Bits PF3_MFP1 (ALT_MFP1[27:26]) and GPF_MFP[3] determine the PF.3 function. + * | | |(PF3_MFP1 (ALT_MFP1[27:26]), GPF_MFP[3]) value and function mapping are as following list. + * | | |(00, 0) = GPIO function is selected. + * | | |(00, 1) = PS2_CLK function is selected. + * | | |(10, 1) = I2C0_SCL function is selected. + * | | |(11, 1) = ADC7 function is selected. + * | | |The reset value of this bit is 1. + * |[19:16] |GPF_TYPEn |Schmitt Trigger Function Selection + * | | |0 = GPIOF[3:0] I/O input Schmitt Trigger function Disabled. + * | | |1 = GPIOF[3:0] I/O input Schmitt Trigger function Enabled. + * @var GCR_T::ALT_MFP + * Offset: 0x50 Alternative Multiple Function Pin Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |PB10_MFP1 |PB.10 Pin Alternate Function Selection + * | | |Bit GPB_MFP[10] determines the PB.10 function. + * | | |(PB10_MFP1 (ALT_MFP[0]), GPB_MFP[10]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = TM2 function is selected. + * | | |(1, 1) = SPI0_SS1 function is selected. + * |[1] |PB9_MFP1 |PB.9 Pin Alternate Function Selection + * | | |Bits PB9_MFP1 (ALT_MFP[1]) and GPB_MFP[9] determine the PB.9 function. + * | | |(PB9_MFP1 (ALT_MFP[1]), GPB_MFP[9]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = TM1 function is selected. + * | | |(1, 0) = PWM1 function is selected. (NUC123xxxAEx Only) + * | | |(1, 1) = SPI1_SS1 function is selected. + * |[5] |PC0_MFP1 |PC.0 Pin Alternate Function Selection + * | | |Bits PC0_MFP1 (ALT_MFP[5]) and GPC_MFP[0] determine the PC.0 function. + * | | |(PC0_MFP1 (ALT_MFP[5]), GPC_MFP[0]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_SS0 function is selected. + * | | |(1, 1) = I2S_LRCLK function is selected. + * |[6] |PC1_MFP1 |PC.1 Pin Alternate Function Selection + * | | |Bits PC1_MFP1 (ALT_MFP[6]) and GPC_MFP[1] determine the PC.1 function. + * | | |(PC1_MFP1 (ALT_MFP[6]), GPC_MFP[1]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_CLK function is selected. + * | | |(1, 1) = I2S_BCLK function is selected. + * |[7] |PC2_MFP1 |PC.2 Pin Alternate Function Selection + * | | |Bits PC2_MFP1 (ALT_MFP[7]) and GPC_MFP[2] determine the PC.2 function. + * | | |(PC2_MFP1 (ALT_MFP[7]), GPC_MFP[2]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MISO0 function is selected. + * | | |(1, 1) = I2S_DI function is selected. + * |[8] |PC3_MFP1 |PC.3 Pin Alternate Function Selection + * | | |Bits PC3_MFP1 (ALT_MFP[8]) and GPC_MFP[3] determine the PC.3 function. + * | | |(PC3_MFP1 (ALT_MFP[8]), GPC_MFP[3]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MOSI0 function is selected. + * | | |(1, 1) = I2S_DO function is selected. + * |[9] |PA15_MFP1 |PA.15 Pin Alternate Function Selection + * | | |Bits PA15_MFP1 (ALT_MFP[9]) and GPA_MFP[15] determine the PA.15 function. + * | | |(PA15_MFP1 (ALT_MFP[9]), GPA_MFP[15]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = PWM3 function is selected. + * | | |(1, 0) = CLKO function is selected. + * | | |(1, 1) = I2S_MCLK function is selected. + * |[10] |PB12_MFP1 |PB.12 Pin Alternate Function Selection + * | | |Bits PB12_MFP1 (ALT_MFP[10]) and GPB_MFP[12] determine the PB.12 function. + * | | |(PB12_MFP1 (ALT_MFP[10]), GPB_MFP[12]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_SS0 function is selected. + * | | |(1, 1) = CLKO function is selected. + * |[11] |PA11_MFP1 |PA.11 Pin Alternate Function Selection + * | | |Bits PA11_MFP1 (ALT_MFP[11]) and GPA_MFP[11] determine the PA.11 function. + * | | |(PA11_MFP1 (ALT_MFP[11]), GPA_MFP[11]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = I2C1_SCL function is selected. + * | | |(1, 0) = SPI1_CLK function is selected. + * | | |(1, 1) = SPI2_MOSI0 function is selected. + * |[12] |PA10_MFP1 |PA.10 Pin Alternate Function Selection + * | | |Bits PA10_MFP1 (ALT_MFP[12]) and GPA_MFP[10] determine the PA.10 function. + * | | |(PA10_MFP1 (ALT_MFP[12]), GPA_MFP[10]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = I2C1_SDA function is selected. + * | | |(1, 0) = SPI1_MISO0 function is selected. + * | | |(1, 1) = SPI2_MISO0 function is selected. + * |[15] |PB4_MFP1 |PB.4 Pin Alternate Function Selection + * | | |Bits PB4_MFP1 (ALT_MFP[15]) and GPB_MFP[4] determine the PB.4 function. + * | | |(PB4_MFP1 (ALT_MFP[15]), GPB_MFP[4]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_RXD function is selected. + * | | |(1, 0) = SPI2_SS0 function is selected. + * | | |(1, 1) = SPI1_SS1 function is selected. + * |[16] |PB7_MFP1 |PB.7 Pin Alternate Function Selection + * | | |Bits PB7_MFP1 (ALT_MFP[16]) and GPB_MFP[7] determine the PB.7 function. + * | | |(PB7_MFP1 (ALT_MFP[16]), GPB_MFP[7]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_nCTS function is selected. + * | | |(1, 1) = SPI2_MISO0 function is selected. + * |[17] |PB6_MFP1 |PB.6 Pin Alternate Function Selection + * | | |Bits PB6_MFP1 (ALT_MFP[17]) and GPB_MFP[6] determine the PB.6 function. + * | | |(PB6_MFP1 (ALT_MFP[17]), GPB_MFP[6]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_nRTS function is selected. + * | | |(1, 1) = SPI2_MOSI0 function is selected. + * |[18] |PB5_MFP1 |PB. 5 Pin Alternate Function Selection + * | | |Bits PB5_MFP1 (ALT_MFP[18]) and GPB_MFP[5] determine the PB.5 function. + * | | |(PB5_MFP1 (ALT_MFP[18]), GPB_MFP[5]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART1_TXD function is selected. + * | | |(1, 1) = SPI2_CLK function is selected. + * |[20] |PC12_MFP1 |PC.12 Pin Alternate Function Selection + * | | |Bits PC12_MFP1 (ALT_MFP[20]) and GPC_MFP[12] determine the PC.12 function. + * | | |(PC12_MFP1 (ALT_MFP[20]), GPC_MFP[12]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_MISO1 function is selected. + * | | |(1, 0) = I2S_MCLK function is selected. + * | | |(1, 1) = PWM2 function is selected. + * |[21] |PC13_MFP1 |PC.13 Pin Alternate Function Selection + * | | |Bits PC13_MFP1 (ALT_MFP[21]) and GPC_MFP[13] determine the PC.13 function. + * | | |(PC13_MFP1 (ALT_MFP[21]), GPC_MFP[13]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_MOSI1 function is selected. + * | | |(1, 0) = CLKO function is selected. + * | | |(1, 1) = PWM3 function is selected. + * |[24] |PB15_MFP1 |PB.15 Pin Alternate Function Selection + * | | |Bits PB15_MFP1 (ALT_MFP[24]) and GPB_MFP[15] determine the PB.15 function. + * | | |(PB15_MFP1 (ALT_MFP[24]), GPB_MFP[15]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = INT1 function is selected. + * | | |(1, 1) = TM0_EXT function is selected. + * |[26] |PB2_MFP1 |PB.2 Pin Alternate Function Selection + * | | |Bits PB2_MFP1 (ALT_MFP[26]) and GPB_MFP[2] determine the PB.2 function. + * | | |(PB2_MFP1 (ALT_MFP[26]), GPB_MFP[2]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART0_nRTS function is selected. + * | | |(1, 1) = TM2_EXT function is selected. + * |[27] |PB3_MFP1 |PB.3 Pin Alternate Function Selection + * | | |Bits PB3_MFP1 (ALT_MFP[27]) and GPB_MFP[3] determine the PB.3 function. + * | | |(PB3_MFP1 (ALT_MFP[27]), GPB_MFP[3]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = UART0_nCTS function is selected. + * | | |(1, 1) = TM3_EXT function is selected. + * |[29] |PC4_MFP1 |PC.4 Pin Alternate Function Selection + * | | |Bits PC4_MFP1 (ALT_MFP[29]) and GPC_MFP[4] determine the PC.4 function. + * | | |(PC4_MFP1 (ALT_MFP[29]), GPC_MFP[4]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MISO1 function is selected. + * | | |(1, 1) = UART0_RXD function is selected. + * |[30] |PC5_MFP1 |PC.5 Pin Alternate Function Selection + * | | |Bits PC5_MFP1 (ALT_MFP[30]) and GPC_MFP[5] determine the PC.5 function. + * | | |(PC5_MFP1 (ALT_MFP[30]), GPC_MFP[5]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MOSI1 function is selected. + * | | |(1, 1) = UART0_TXD function is selected. + * @var GCR_T::ALT_MFP1 + * Offset: 0x54 Alternative Multiple Function Pin Control Register 1 + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[16] |PD0_MFP1 |PD.0 Pin Alternate Function Selection + * | | |Bits PD0_MFP1 (ALT_MFP1[16]) and GPD_MFP[0] determine the PD.0 function. + * | | |(PD0_MFP1 (ALT_MFP1[16]), GPD_MFP[0]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(1, 0) = SPI2_SS0 function is selected. + * | | |(1, 1) = ADC0 function is selected. + * |[17] |PD1_MFP1 |PD.1 Pin Alternate Function Selection + * | | |Bits PD1_MFP1 (ALT_MFP1[17]) and GPD_MFP[1] determine the PD.1 function. + * | | |(PD1_MFP1 (ALT_MFP1[17]), GPD_MFP[1]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_SS1 function is selected. + * | | |(1, 0) = SPI2_CLK function is selected. + * | | |(1, 1) = ADC1 function is selected. + * |[18] |PD2_MFP1 |PD.2 Pin Alternate Function Selection + * | | |Bits PD2_MFP1 (ALT_MFP1[18]) and GPD_MFP[2] determine the PD.2 function. + * | | |(PD2_MFP1 (ALT_MFP1[18]), GPD_MFP[2]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MISO1 function is selected. + * | | |(1, 0) = SPI2_MISO0 function is selected. + * | | |(1, 1) = ADC2 function is selected. + * |[19] |PD3_MFP1 |PD.3 Pin Alternate Function Selection + * | | |Bits PD3_MFP1 (ALT_MFP1[19]) and GPD_MFP[3] determine the PD.3 function. + * | | |(PD3_MPF1 (ALT_MFP1[19]), GPD_MFP[3]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI0_MOSI1 function is selected. + * | | |(1, 0) = SPI2_MOSI0 function is selected. + * | | |(1, 1) = ADC3 function is selected. + * |[20] |PD4_MFP1 |PD.4 Pin Alternate Function Selection + * | | |Bits PD4_MFP1 (ALT_MFP1[20]) and GPD_MFP[4] determine the PD.4 function. + * | | |(PD4_MFP1 (ALT_MFP1[20]), GPD_MFP[4]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(1, 0) = SPI2_MISO1 function is selected. + * | | |(1, 1) = ADC4 function is selected. + * |[21] |PD5_MFP1 |PD.5 Pin Alternate Function Selection + * | | |Bits PD5_MFP1 (ALT_MFP1[21]) and GPD_MFP[5] determine the PD.5 function. + * | | |(PD5_MFP1 (ALT_MFP1[21]), GPD_MFP[5]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(1, 0) = SPI2_MOSI1 function is selected. + * | | |(1, 1) = ADC5 function is selected. + * |[23] |PC8_MFP1 |PC.8 Pin Alternate Function Selection + * | | |Bits PC8_MFP1 (ALT_MFP1[23]) and GPC_MFP[8] determine the PC.8 function. + * | | |(PC8_MFP1 (ALT_MFP1[23]), GPC_MFP[8]) value and function mapping are as following list. + * | | |(0, 0) = GPIO function is selected. + * | | |(0, 1) = SPI1_SS0 function is selected. + * | | |(1, 1) = PWM0 function is selected. (NUC123xxxAEx Only) + * |[25:24] |PF2_MFP1 |PF.2 Pin Alternate Function Selection + * | | |Bits PF2_MFP1 (ALT_MFP1[25:24]) and GPF_MFP[2] determine the PF.2 function. + * | | |(PF2_MFP1(ALT_MFP1 [25:24]), GPF_MFP[2]) value and function mapping are as following list. + * | | |(00, 0) = GPIO function is selected. + * | | |(00, 1) = PS2_DAT function is selected. + * | | |(10, 1) = I2C0_SDA function is selected. + * | | |(11, 1) = ADC6 function is selected. + * |[27:26] |PF3_MFP1 |PF.3 Pin Alternate Function Selection + * | | |Bits PF3_MFP1 (ALT_MFP1[27:26]) and GPF_MFP[3] determine the PF.3 function. + * | | |(PF3_MFP1 (ALT_MFP1[27:26]), GPF_MFP[3]) value and function mapping are as following list. + * | | |(00, 0) = GPIO function is selected. + * | | |(00, 1) = PS2_CLK function is selected. + * | | |(10, 1) = I2C0_SCL function is selected. + * | | |(11, 1) = ADC7 function is selected. + * @var GCR_T::GPA_IOCR + * Offset: 0xC0 GPIOA IO Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[10] |GPA10_DS |PA.10 Pin Driving Strength Selection + * | | |0 = PA.10 strong driving strength mode Disabled. + * | | |1 = PA.10 strong driving strength mode Enabled. + * |[11] |GPA11_DS |PA.11 Pin Driving Strength Selection + * | | |0 = PA.11 strong driving strength mode Disabled. + * | | |1 = PA.11 strong driving strength mode Enabled. + * @var GCR_T::GPB_IOCR + * Offset: 0xC4 GPIOB IO Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[4] |GPB4_DS |PB.4 Pin Driving Strength Selection + * | | |0 = PB.4 strong driving strength mode Disabled. + * | | |1 = PB.4 strong driving strength mode Enabled. + * |[5] |GPB5_DS |PB.5 Pin Driving Strength Selection + * | | |0 = PB.5 strong driving strength mode Disabled. + * | | |1 = PB.5 strong driving strength mode Enabled. + * |[6] |GPB6_DS |PB.6 Pin Driving Strength Selection + * | | |0 = PB.6 strong driving strength mode Disabled. + * | | |1 = PB.6 strong driving strength mode Enabled. + * |[7] |GPB7_DS |PB.7 Pin Driving Strength Selection + * | | |0 = PB.7 strong driving strength mode Disabled. + * | | |1 = PB.7 strong driving strength mode Enabled. + * |[8] |GPB8_DS |PB.8 Pin Driving Strength Selection + * | | |0 = PB.8 strong driving strength mode Disabled. + * | | |1 = PB.8 strong driving strength mode Enabled. + * |[12] |GPB12_DS |PB.12 Pin Driving Strength Selection + * | | |0 = PB.12 strong driving strength mode Disabled. + * | | |1 = PB.12 strong driving strength mode Enabled. + * |[13] |GPB13_DS |PB.13 Pin Driving Strength Selection + * | | |0 = PB.13 strong driving strength mode Disabled. + * | | |1 = PB.13 strong driving strength mode Enabled. + * |[14] |GPB14_DS |PB.14 Pin Driving Strength Selection + * | | |0 = PB.14 strong driving strength mode Disabled. + * | | |1 = PB.14 strong driving strength mode Enabled. + * @var GCR_T::GPD_IOCR + * Offset: 0xCC GPIOD IO Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8] |GPD8_DS |PD.8 Pin Driving Strength Selection + * | | |0 = PD.8 strong driving strength mode Disabled. + * | | |1 = PD.8 strong driving strength mode Enabled. + * |[9] |GPD9_DS |PD.9 Pin Driving Strength Selection + * | | |0 = PD.9 strong driving strength mode Disabled. + * | | |1 = PD.9 strong driving strength mode Enabled. + * |[10] |GPD10_DS |PD.10 Pin Driving Strength Selection + * | | |0 = PD.10 strong driving strength mode Disabled. + * | | |1 = PD.10 strong driving strength mode Enabled. + * |[11] |GPD11_DS |PD.11 Pin Driving Strength Selection + * | | |0 = PD.11 strong driving strength mode Disabled. + * | | |1 = PD.11 strong driving strength mode Enabled. + * @var GCR_T::REGWRPROT + * Offset: 0x100 Register Write Protect register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |REGPROTDIS|Register Write-Protection Disable index (Read Only) + * | | |1 = Write-protection Disabled for writing protected registers. + * | | |0 = Write-protection Enabled for writing protected registers. + * | | |Any write to the protected register is ignored. + * | | |The Protected registers are: + * | | |IPRSTC1: address 0x5000_0008 + * | | |BODCR: address 0x5000_0018 + * | | |PORCR: address 0x5000_0024 + * | | |PWRCON: address 0x5000_0200 (bit[6] is not protected for power wake-up interrupt clear) + * | | |APBCLK bit[0]: address 0x5000_0208 (bit[0] is watchdog clock enabled) + * | | |CLKSEL0: address 0x5000_0210 (for HCLK and CPU STCLK clock source select) + * | | |CLKSEL1 bit[1:0]: address 0x5000_0214 (for watchdog clock source select) + * | | |ISPCON: address 0x5000_C000 (Flash ISP Control register) + * | | |WTCR: address 0x4000_4000 + * | | |FATCON: address 0x5000_C018 + * |[7:0] |REGWRPROT |Register Write-Protection Code (Write Only) + * | | |Some registers have write-protection function. Writing these registers has to disable the protected function by writing the sequence value "59h", "16h", "88h" to this field. + * | | |After this sequence is completed, the REGPROTDIS bit will be set to 1 and write-protection registers can be normal write. + * @var GCR_T::GPA_MFPH + * Offset: 0x134 GPIOA Multiple Function High Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[10:8] |GPA10_MFP |PA.10 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = I2C1_SDA function is selected. + * | | |010 = SPI1_MISO0 function is selected. + * | | |011 = SPI2_MISO0 function is selected. + * |[14:12] |GPA11_MFP |PA.11 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = I2C1_SCL function is selected. + * | | |010 = SPI1_CLK function is selected. + * | | |011 = SPI2_MOSI0 function is selected. + * |[18:16] |GPA12_MFP |PA.12 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = PWM0 function is selected. + * |[22:20] |GPA13_MFP |PA.13 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = PWM1 function is selected. + * |[26:24] |GPA14_MFP |PA.14 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = PWM2 function is selected. + * |[30:28] |GPA15_MFP |PA.15 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = PWM3 function is selected. + * | | |010 = CLKO function is selected. + * | | |011 = I2S_MCLK function is selected. + * @var GCR_T::GPB_MFPL + * Offset: 0x138 GPIOB Multiple Function Low Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPB0_MFP |PB.0 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART0_RXD function is selected. + * |[6:4] |GPB1_MFP |PB.1 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART0_TXD function is selected. + * |[10:8] |GPB2_MFP |PB.2 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART0_nRTS function is selected. + * | | |010 = TM2_EXT function is selected. + * |[14:12] |GPB3_MFP |PB.3 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART0_nCTS function is selected. + * | | |010 = TM3_EXT function is selected. + * |[18:16] |GPB4_MFP |PB.4 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART1_RXD function is selected. + * | | |010 = SPI2_SS0 function is selected. + * | | |010 = SPI1_SS1 function is selected. + * |[22:20] |GPB5_MFP |PB.5 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART1_TXD function is selected. + * | | |010 = SPI2_CLK function is selected. + * |[26:24] |GPB6_MFP |PB.6 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART1_nRTS function is selected. + * | | |010 = SPI2_MOSI0 function is selected. + * |[30:28] |GPB7_MFP |PB.7 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = UART1_nCTS function is selected. + * | | |010 = SPI2_MISO0 function is selected. + * @var GCR_T::GPB_MFPH + * Offset: 0x13C GPIOB Multiple Function High Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPB8_MFP |PB.8 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = TM0 function is selected. + * |[6:4] |GPB9_MFP |PB.9 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = TM1 function is selected. + * | | |010 = SPI1_SS1 function is selected. + * | | |011 = PWM1 function is selected. + * |[10:8] |GPB10_MFP |PB.10 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = TM2 function is selected. + * | | |010 = SPI0_SS1 function is selected. + * |[18:16] |GPB12_MFP |PB.12 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_SS0 function is selected. + * | | |010 = CLKO function is selected. + * |[22:20] |GPB13_MFP |PB.13 Pin Function Selection + * | | |000 = GPIO function is selected. + * |[26:24] |GPB14_MFP |PB.14 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = INT0 function is selected. + * |[30:28] |GPB15_MFP |PB.15 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = INT1 function is selected. + * | | |010 = TM0_EXT function is selected. + * @var GCR_T::GPC_MFPL + * Offset: 0x140 GPIOC Multiple Function Low Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPC0_MFP |PC.0 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_SS0 function is selected. + * | | |010 = I2S_LRCK function is selected. + * |[6:4] |GPC1_MFP |PC.1 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_CLK function is selected. + * | | |001 = I2S_BCLK function is selected. + * |[10:8] |GPC2_MFP |PC.2 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_MISO0 function is selected. + * | | |010 = I2S_DI function is selected. + * |[14:12] |GPC3_MFP |PC.3 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_MIOSI0 function is selected. + * | | |010 = I2S_DO function is selected. + * |[18:16] |GPC4_MFP |PC.4 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_MISO1 function is selected. + * | | |010 = UART0_RXD function is selected. + * |[22:20] |GPC5_MFP |PC.5 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_MOSI1 function is selected. + * | | |010 = UART0_TXD function is selected. + * @var GCR_T::GPC_MFPH + * Offset: 0x144 GPIOC Multiple Function High Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPC8_MFP |PC.8 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_SS0 function is selected. + * | | |010 = PWM0 function is selected. + * |[6:4] |GPC9_MFP |PC.9 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_CLK function is selected. + * |[10:8] |GPC10_MFP |PC.10 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_MISO0 function is selected. + * |[14:12] |GPC11_MFP |PC.11 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_MOSI0 function is selected. + * |[18:16] |GPC12_MFP |PC.12 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_MISO1 function is selected. + * | | |010 = I2S_MCLK function is selected. + * | | |011 = PWM2 function is selected. + * |[22:20] |GPC13_MFP |PC.13 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_MOSI1 function is selected. + * | | |010 = CLKO function is selected. + * | | |011 = PWM3 function is selected. + * @var GCR_T::GPD_MFPL + * Offset: 0x148 GPIOD Multiple Function Low Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPD0_MFP |PD.0 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI2_SS0 function is selected. + * | | |010 = ADC0 function is selected. + * |[6:4] |GPD1_MFP |PD.1 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_SS1 function is selected. + * | | |010 = SPI2_CLK function is selected. + * | | |011 = ADC1 function is selected. + * |[10:8] |GPD2_MFP |PD.2 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_MISO1 function is selected. + * | | |010 = SPI2_MISO0 function is selected. + * | | |010 = ADC2 function is selected. + * |[14:12] |GPD3_MFP |PD.3 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI0_MOSI1 function is selected. + * | | |010 = SPI2_MOSI0 function is selected. + * | | |011 = ADC3 function is selected. + * |[18:16] |GPD4_MFP |PD.4 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI2_MISO1 function is selected. + * | | |010 = ADC4 function is selected. + * |[22:20] |GPD5_MFP |PD.5 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI2_MOSI1 function is selected. + * | | |010 = ADC5 function is selected. + * @var GCR_T::GPD_MFPH + * Offset: 0x14C GPIOD Multiple Function High Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPD8_MFP |PD.8 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = SPI1_MOSI0 function is selected. + * |[6:4] |GPD9_MFP |PD.9 Pin Function Selection + * | | |000 = GPIO function is selected. + * |[10:8] |GPD10_MFP |PD.10 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = CLKO function is selected. + * |[14:12] |GPD11_MFP |PD.11 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = INT1 function is selected. + * @var GCR_T::GPF_MFPL + * Offset: 0x158 GPIOF Multiple Function Low Byte Control Register (NUC123xxxAEx Only) + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[2:0] |GPF0_MFP |PF.0 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = XT1_OUT function is selected. + * |[6:4] |GPF1_MFP |PF.1 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = XT1_IN function is selected. + * |[10:8] |GPF2_MFP |PF.2 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = PS2_DAT function is selected. + * | | |010 = I2C0_SDA function is selected. + * | | |010 = ADC6 function is selected. + * |[14:12] |GPF3_MFP |PF.3 Pin Function Selection + * | | |000 = GPIO function is selected. + * | | |001 = PS2_CLK function is selected. + * | | |010 = I2C0_SCL function is selected. + * | | |011 = ADC7 function is selected. + */ + + __I uint32_t PDID; /* Offset: 0x00 Part Device Identification Number Register */ + __IO uint32_t RSTSRC; /* Offset: 0x04 System Reset Source Register */ + __IO uint32_t IPRSTC1; /* Offset: 0x08 Peripheral Reset Control Register 1 */ + __IO uint32_t IPRSTC2; /* Offset: 0x0C Peripheral Reset Control Register 2 */ + __I uint32_t RESERVE0[2]; + __IO uint32_t BODCR; /* Offset: 0x18 Brown-out Detector Control Register */ + __I uint32_t RESERVE1[2]; + __IO uint32_t PORCR; /* Offset: 0x24 Power-on-Reset Controller Register */ + __I uint32_t RESERVE2[2]; + __IO uint32_t GPA_MFP; /* Offset: 0x30 GPIOA Multiple Function and Input Type Control Register */ + __IO uint32_t GPB_MFP; /* Offset: 0x34 GPIOB Multiple Function and Input Type Control Register */ + __IO uint32_t GPC_MFP; /* Offset: 0x38 GPIOC Multiple Function and Input Type Control Register */ + __IO uint32_t GPD_MFP; /* Offset: 0x3C GPIOD Multiple Function and Input Type Control Register */ + __I uint32_t RESERVE3; + __IO uint32_t GPF_MFP; /* Offset: 0x44 GPIOF Multiple Function and Input Type Control Register */ + __I uint32_t RESERVE4[2]; + __IO uint32_t ALT_MFP; /* Offset: 0x50 Alternative Multiple Function Pin Control Register */ + __IO uint32_t ALT_MFP1; /* Offset: 0x54 Alternative Multiple Function Pin Control Register 1 */ + __I uint32_t RESERVE5[26]; + __IO uint32_t GPA_IOCR; /* Offset: 0xC0 GPIOA IO Control Register */ + __IO uint32_t GPB_IOCR; /* Offset: 0xC4 GPIOB IO Control Register */ + __I uint32_t RESERVE6; + __IO uint32_t GPD_IOCR; /* Offset: 0xCC GPIOD IO Control Register */ + __I uint32_t RESERVE7[12]; + __IO uint32_t REGWRPROT; /* Offset: 0x100 Register Write Protect register */ + __I uint32_t RESERVE8[12]; + __IO uint32_t GPA_MFPH; /* Offset: 0x134 GPIOA Multiple Function High Byte Control Register (NUC123xxxAEx Only) */ + __IO uint32_t GPB_MFPL; /* Offset: 0x138 GPIOB Multiple Function Low Byte Control Register (NUC123xxxAEx Only) */ + __IO uint32_t GPB_MFPH; /* Offset: 0x13C GPIOB Multiple Function High Byte Control Register (NUC123xxxAEx Only) */ + __IO uint32_t GPC_MFPL; /* Offset: 0x140 GPIOC Multiple Function Low Byte Control Register (NUC123xxxAEx Only) */ + __IO uint32_t GPC_MFPH; /* Offset: 0x144 GPIOC Multiple Function High Byte Control Register (NUC123xxxAEx Only) */ + __IO uint32_t GPD_MFPL; /* Offset: 0x148 GPIOD Multiple Function Low Byte Control Register (NUC123xxxAEx Only) */ + __IO uint32_t GPD_MFPH; /* Offset: 0x14C GPIOD Multiple Function High Byte Control Register (NUC123xxxAEx Only) */ + __I uint32_t RESERVE9[2]; + __IO uint32_t GPF_MFPL; /* Offset: 0x158 GPIOF Multiple Function Low Byte Control Register (NUC123xxxAEx Only) */ + +} GCR_T; + + + + +/** @addtogroup REG_SYS_BITMASK SYS Bit Mask + @{ + */ + +/* GCR RSTSRC Bit Field Definitions */ +#define SYS_RSTSRC_RSTS_CPU_Pos 7 /*!< GCR_T::RSTSRC: RSTS_CPU Position */ +#define SYS_RSTSRC_RSTS_CPU_Msk (1ul << SYS_RSTSRC_RSTS_CPU_Pos) /*!< GCR_T::RSTSRC: RSTS_CPU Mask */ + +#define SYS_RSTSRC_RSTS_SYS_Pos 5 /*!< GCR_T::RSTSRC: RSTS_SYS Position */ +#define SYS_RSTSRC_RSTS_SYS_Msk (1ul << SYS_RSTSRC_RSTS_SYS_Pos) /*!< GCR_T::RSTSRC: RSTS_SYS Mask */ + +#define SYS_RSTSRC_RSTS_BOD_Pos 4 /*!< GCR_T::RSTSRC: RSTS_BOD Position */ +#define SYS_RSTSRC_RSTS_BOD_Msk (1ul << SYS_RSTSRC_RSTS_BOD_Pos) /*!< GCR_T::RSTSRC: RSTS_BOD Mask */ + +#define SYS_RSTSRC_RSTS_LVR_Pos 3 /*!< GCR_T::RSTSRC: RSTS_LVR Position */ +#define SYS_RSTSRC_RSTS_LVR_Msk (1ul << SYS_RSTSRC_RSTS_LVR_Pos) /*!< GCR_T::RSTSRC: RSTS_LVR Mask */ + +#define SYS_RSTSRC_RSTS_WDT_Pos 2 /*!< GCR_T::RSTSRC: RSTS_WDT Position */ +#define SYS_RSTSRC_RSTS_WDT_Msk (1ul << SYS_RSTSRC_RSTS_WDT_Pos) /*!< GCR_T::RSTSRC: RSTS_WDT Mask */ + +#define SYS_RSTSRC_RSTS_RESET_Pos 1 /*!< GCR_T::RSTSRC: RSTS_RESET Position */ +#define SYS_RSTSRC_RSTS_RESET_Msk (1ul << SYS_RSTSRC_RSTS_RESET_Pos) /*!< GCR_T::RSTSRC: RSTS_RESET Mask */ + +#define SYS_RSTSRC_RSTS_POR_Pos 0 /*!< GCR_T::RSTSRC: RSTS_POR Position */ +#define SYS_RSTSRC_RSTS_POR_Msk (1ul << SYS_RSTSRC_RSTS_POR_Pos) /*!< GCR_T::RSTSRC: RSTS_POR Mask */ + +/* GCR IPRSTC1 Bit Field Definitions */ +#define SYS_IPRSTC1_PDMA_RST_Pos 2 /*!< GCR_T::IPRSTC1: PDMA_RST Position */ +#define SYS_IPRSTC1_PDMA_RST_Msk (1ul << SYS_IPRSTC1_PDMA_RST_Pos) /*!< GCR_T::IPRSTC1: PDMA_RST Mask */ + +#define SYS_IPRSTC1_CPU_RST_Pos 1 /*!< GCR_T::IPRSTC1: CPU_RST Position */ +#define SYS_IPRSTC1_CPU_RST_Msk (1ul << SYS_IPRSTC1_CPU_RST_Pos) /*!< GCR_T::IPRSTC1: CPU_RST Mask */ + +#define SYS_IPRSTC1_CHIP_RST_Pos 0 /*!< GCR_T::IPRSTC1: CHIP_RST Position */ +#define SYS_IPRSTC1_CHIP_RST_Msk (1ul << SYS_IPRSTC1_CHIP_RST_Pos) /*!< GCR_T::IPRSTC1: CHIP_RST Mask */ + +/* GCR IPRSTC2 Bit Field Definitions */ +#define SYS_IPRSTC2_I2S_RST_Pos 29 /*!< GCR_T::IPRSTC2: I2S_RST Position */ +#define SYS_IPRSTC2_I2S_RST_Msk (1ul << SYS_IPRSTC2_I2S_RST_Pos) /*!< GCR_T::IPRSTC2: I2S_RST Mask */ + +#define SYS_IPRSTC2_ADC_RST_Pos 28 /*!< GCR_T::IPRSTC2: ADC_RST Position */ +#define SYS_IPRSTC2_ADC_RST_Msk (1ul << SYS_IPRSTC2_ADC_RST_Pos) /*!< GCR_T::IPRSTC2: ADC_RST Mask */ + +#define SYS_IPRSTC2_USBD_RST_Pos 27 /*!< GCR_T::IPRSTC2: USBD_RST Position */ +#define SYS_IPRSTC2_USBD_RST_Msk (1ul << SYS_IPRSTC2_USBD_RST_Pos) /*!< GCR_T::IPRSTC2: USBD_RST Mask */ + +#define SYS_IPRSTC2_PS2_RST_Pos 23 /*!< GCR_T::IPRSTC2: PS2_RST Position */ +#define SYS_IPRSTC2_PS2_RST_Msk (1ul << SYS_IPRSTC2_PS2_RST_Pos) /*!< GCR_T::IPRSTC2: PS2_RST Mask */ + +#define SYS_IPRSTC2_PWM03_RST_Pos 20 /*!< GCR_T::IPRSTC2: PWM03_RST Position */ +#define SYS_IPRSTC2_PWM03_RST_Msk (1ul << SYS_IPRSTC2_PWM03_RST_Pos) /*!< GCR_T::IPRSTC2: PWM03_RST Mask */ + +#define SYS_IPRSTC2_UART1_RST_Pos 17 /*!< GCR_T::IPRSTC2: UART1_RST Position */ +#define SYS_IPRSTC2_UART1_RST_Msk (1ul << SYS_IPRSTC2_UART1_RST_Pos) /*!< GCR_T::IPRSTC2: UART1_RST Mask */ + +#define SYS_IPRSTC2_UART0_RST_Pos 16 /*!< GCR_T::IPRSTC2: UART0_RST Position */ +#define SYS_IPRSTC2_UART0_RST_Msk (1ul << SYS_IPRSTC2_UART0_RST_Pos) /*!< GCR_T::IPRSTC2: UART0_RST Mask */ + +#define SYS_IPRSTC2_SPI2_RST_Pos 14 /*!< GCR_T::IPRSTC2: SPI2_RST Position */ +#define SYS_IPRSTC2_SPI2_RST_Msk (1ul << SYS_IPRSTC2_SPI2_RST_Pos) /*!< GCR_T::IPRSTC2: SPI2_RST Mask */ + +#define SYS_IPRSTC2_SPI1_RST_Pos 13 /*!< GCR_T::IPRSTC2: SPI1_RST Position */ +#define SYS_IPRSTC2_SPI1_RST_Msk (1ul << SYS_IPRSTC2_SPI1_RST_Pos) /*!< GCR_T::IPRSTC2: SPI1_RST Mask */ + +#define SYS_IPRSTC2_SPI0_RST_Pos 12 /*!< GCR_T::IPRSTC2: SPI0_RST Position */ +#define SYS_IPRSTC2_SPI0_RST_Msk (1ul << SYS_IPRSTC2_SPI0_RST_Pos) /*!< GCR_T::IPRSTC2: SPI0_RST Mask */ + +#define SYS_IPRSTC2_I2C1_RST_Pos 9 /*!< GCR_T::IPRSTC2: I2C1_RST Position */ +#define SYS_IPRSTC2_I2C1_RST_Msk (1ul << SYS_IPRSTC2_I2C1_RST_Pos) /*!< GCR_T::IPRSTC2: I2C1_RST Mask */ + +#define SYS_IPRSTC2_I2C0_RST_Pos 8 /*!< GCR_T::IPRSTC2: I2C0_RST Position */ +#define SYS_IPRSTC2_I2C0_RST_Msk (1ul << SYS_IPRSTC2_I2C0_RST_Pos) /*!< GCR_T::IPRSTC2: I2C0_RST Mask */ + +#define SYS_IPRSTC2_TMR3_RST_Pos 5 /*!< GCR_T::IPRSTC2: TMR3_RST Position */ +#define SYS_IPRSTC2_TMR3_RST_Msk (1ul << SYS_IPRSTC2_TMR3_RST_Pos) /*!< GCR_T::IPRSTC2: TMR3_RST Mask */ + +#define SYS_IPRSTC2_TMR2_RST_Pos 4 /*!< GCR_T::IPRSTC2: TMR2_RST Position */ +#define SYS_IPRSTC2_TMR2_RST_Msk (1ul << SYS_IPRSTC2_TMR2_RST_Pos) /*!< GCR_T::IPRSTC2: TMR2_RST Mask */ + +#define SYS_IPRSTC2_TMR1_RST_Pos 3 /*!< GCR_T::IPRSTC2: TMR1_RST Position */ +#define SYS_IPRSTC2_TMR1_RST_Msk (1ul << SYS_IPRSTC2_TMR1_RST_Pos) /*!< GCR_T::IPRSTC2: TMR1_RST Mask */ + +#define SYS_IPRSTC2_TMR0_RST_Pos 2 /*!< GCR_T::IPRSTC2: TMR0_RST Position */ +#define SYS_IPRSTC2_TMR0_RST_Msk (1ul << SYS_IPRSTC2_TMR0_RST_Pos) /*!< GCR_T::IPRSTC2: TMR0_RST Mask */ + +#define SYS_IPRSTC2_GPIO_RST_Pos 1 /*!< GCR_T::IPRSTC2: GPIO_RST Position */ +#define SYS_IPRSTC2_GPIO_RST_Msk (1ul << SYS_IPRSTC2_GPIO_RST_Pos) /*!< GCR_T::IPRSTC2: GPIO_RST Mask */ + +/* GCR BODCR Bit Field Definitions */ +#define SYS_BODCR_LVR_EN_Pos 7 /*!< GCR_T::BODCR: LVR_EN Position */ +#define SYS_BODCR_LVR_EN_Msk (1ul << SYS_BODCR_LVR_EN_Pos) /*!< GCR_T::BODCR: LVR_EN Mask */ + +#define SYS_BODCR_BOD_OUT_Pos 6 /*!< GCR_T::BODCR: BOD_OUT Position */ +#define SYS_BODCR_BOD_OUT_Msk (1ul << SYS_BODCR_BOD_OUT_Pos) /*!< GCR_T::BODCR: BOD_OUT Mask */ + +#define SYS_BODCR_BOD_LPM_Pos 5 /*!< GCR_T::BODCR: BOD_LPM Position */ +#define SYS_BODCR_BOD_LPM_Msk (1ul << SYS_BODCR_BOD_LPM_Pos) /*!< GCR_T::BODCR: BOD_LPM Mask */ + +#define SYS_BODCR_BOD_INTF_Pos 4 /*!< GCR_T::BODCR: BOD_INTF Position */ +#define SYS_BODCR_BOD_INTF_Msk (1ul << SYS_BODCR_BOD_INTF_Pos) /*!< GCR_T::BODCR: BOD_INTF Mask */ + +#define SYS_BODCR_BOD_RSTEN_Pos 3 /*!< GCR_T::BODCR: BOD_RSTEN Position */ +#define SYS_BODCR_BOD_RSTEN_Msk (1ul << SYS_BODCR_BOD_RSTEN_Pos) /*!< GCR_T::BODCR: BOD_RSTEN Mask */ + +#define SYS_BODCR_BOD_VL_Pos 1 /*!< GCR_T::BODCR: BOD_VL Position */ +#define SYS_BODCR_BOD_VL_Msk (3ul << SYS_BODCR_BOD_VL_Pos) /*!< GCR_T::BODCR: BOD_VL Mask */ + +#define SYS_BODCR_BOD_EN_Pos 0 /*!< GCR_T::BODCR: BOD_EN Position */ +#define SYS_BODCR_BOD_EN_Msk (1ul << SYS_BODCR_BOD_EN_Pos) /*!< GCR_T::BODCR: BOD_EN Mask */ + +/* GCR PORCR Bit Field Definitions */ +#define SYS_PORCR_POR_DIS_CODE_Pos 0 /*!< GCR_T::PORCR: POR_DIS_CODE Position */ +#define SYS_PORCR_POR_DIS_CODE_Msk (0xFFFFul << SYS_PORCR_POR_DIS_CODE_Pos) /*!< GCR_T::PORCR: POR_DIS_CODE Mask */ + +/* GCR GPAMFP Bit Field Definitions */ +#define SYS_GPA_MFP_GPA_TYPE_Pos 16 /*!< GCR_T::GPA_MFP: GPA_TYPE Position */ +#define SYS_GPA_MFP_GPA_TYPE_Msk (0xFFFFul << SYS_GPA_MFP_GPA_TYPE_Pos) /*!< GCR_T::GPA_MFP: GPA_TYPE Mask */ + +#define SYS_GPA_MFP_GPA_MFP_Pos 0 /*!< GCR_T::GPA_MFP: GPA_MFP Position */ +#define SYS_GPA_MFP_GPA_MFP_Msk (0xFFFFul << SYS_GPA_MFP_GPA_MFP_Pos) /*!< GCR_T::GPA_MFP: GPA_MFP Mask */ + + +/* GCR GPBMFP Bit Field Definitions */ +#define SYS_GPB_MFP_GPB_TYPE_Pos 16 /*!< GCR_T::GPB_MFP: GPB_TYPE Position */ +#define SYS_GPB_MFP_GPB_TYPE_Msk (0xFFFFul << SYS_GPB_MFP_GPB_TYPE_Pos) /*!< GCR_T::GPB_MFP: GPB_TYPE Mask */ + +#define SYS_GPB_MFP_GPB_MFP_Pos 0 /*!< GCR_T::GPB_MFP: GPB_MFP Position */ +#define SYS_GPB_MFP_GPB_MFP_Msk (0xFFFFul << SYS_GPB_MFP_GPB_MFP_Pos) /*!< GCR_T::GPB_MFP: GPB_MFP Mask */ + +/* GCR GPCMFP Bit Field Definitions */ +#define SYS_GPC_MFP_GPC_TYPE_Pos 16 /*!< GCR_T::GPC_MFP: GPC_TYPE Position */ +#define SYS_GPC_MFP_GPC_TYPE_Msk (0xFFFFul << SYS_GPC_MFP_GPC_TYPE_Pos) /*!< GCR_T::GPC_MFP: GPC_TYPE Mask */ + +#define SYS_GPC_MFP_GPC_MFP_Pos 0 /*!< GCR_T::GPC_MFP: GPC_MFP Position */ +#define SYS_GPC_MFP_GPC_MFP_Msk (0xFFFFul << SYS_GPC_MFP_GPC_MFP_Pos) /*!< GCR_T::GPC_MFP: GPC_MFP Mask */ + +/* GCR GPDMFP Bit Field Definitions */ +#define SYS_GPD_MFP_GPD_TYPE_Pos 16 /*!< GCR_T::GPD_MFP: GPD_TYPE Position */ +#define SYS_GPD_MFP_GPD_TYPE_Msk (0xFFFFul << SYS_GPD_MFP_GPD_TYPE_Pos) /*!< GCR_T::GPD_MFP: GPD_TYPE Mask */ + +#define SYS_GPD_MFP_GPD_MFP_Pos 0 /*!< GCR_T::GPD_MFP: GPD_MFP Position */ +#define SYS_GPD_MFP_GPD_MFP_Msk (0xFFFFul << SYS_GPD_MFP_GPD_MFP_Pos) /*!< GCR_T::GPD_MFP: GPD_MFP Mask */ + +/* GCR GPFMFP Bit Field Definitions */ +#define SYS_GPF_MFP_GPF_TYPE_Pos 16 /*!< GCR_T::GPF_MFP: GPF_TYPE Position */ +#define SYS_GPF_MFP_GPF_TYPE_Msk (0xFul << SYS_GPF_MFP_GPF_TYPE_Pos) /*!< GCR_T::GPF_MFP: GPF_TYPE Mask */ + +#define SYS_GPF_MFP_GPF_MFP3_Pos 3 /*!< GCR_T::GPF_MFP: GPF_MFP3 Position */ +#define SYS_GPF_MFP_GPF_MFP3_Msk (1ul << SYS_GPF_MFP_GPF_MFP3_Pos) /*!< GCR_T::GPF_MFP: GPF_MFP3 Mask */ + +#define SYS_GPF_MFP_GPF_MFP2_Pos 2 /*!< GCR_T::GPF_MFP: GPF_MFP2 Position */ +#define SYS_GPF_MFP_GPF_MFP2_Msk (1ul << SYS_GPF_MFP_GPF_MFP2_Pos) /*!< GCR_T::GPF_MFP: GPF_MFP2 Mask */ + +#define SYS_GPF_MFP_GPF_MFP1_Pos 1 /*!< GCR_T::GPF_MFP: GPF_MFP1 Position */ +#define SYS_GPF_MFP_GPF_MFP1_Msk (1ul << SYS_GPF_MFP_GPF_MFP1_Pos) /*!< GCR_T::GPF_MFP: GPF_MFP1 Mask */ + +#define SYS_GPF_MFP_GPF_MFP0_Pos 0 /*!< GCR_T::GPF_MFP: GPF_MFP0 Position */ +#define SYS_GPF_MFP_GPF_MFP0_Msk (1ul << SYS_GPF_MFP_GPF_MFP0_Pos) /*!< GCR_T::GPF_MFP: GPF_MFP0 Mask */ + +/* GCR ALTMFP Bit Field Definitions */ +#define SYS_ALT_MFP_PC5_MFP1_Pos 30 /*!< GCR_T::ALT_MFP: PC5_MFP1 Position */ +#define SYS_ALT_MFP_PC5_MFP1_Msk (1ul << SYS_ALT_MFP_PC5_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC5_MFP1 Mask */ + +#define SYS_ALT_MFP_PC4_MFP1_Pos 29 /*!< GCR_T::ALT_MFP: PC4_MFP1 Position */ +#define SYS_ALT_MFP_PC4_MFP1_Msk (1ul << SYS_ALT_MFP_PC4_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC4_MFP1 Mask */ + +#define SYS_ALT_MFP_PB3_MFP1_Pos 27 /*!< GCR_T::ALT_MFP: PB3_MFP1 Position */ +#define SYS_ALT_MFP_PB3_MFP1_Msk (1ul << SYS_ALT_MFP_PB3_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB3_MFP1 Mask */ + +#define SYS_ALT_MFP_PB2_MFP1_Pos 26 /*!< GCR_T::ALT_MFP: PB2_MFP1 Position */ +#define SYS_ALT_MFP_PB2_MFP1_Msk (1ul << SYS_ALT_MFP_PB2_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB2_MFP1 Mask */ + +#define SYS_ALT_MFP_PB15_MFP1_Pos 24 /*!< GCR_T::ALT_MFP: PB15_MFP1 Position */ +#define SYS_ALT_MFP_PB15_MFP1_Msk (1ul << SYS_ALT_MFP_PB15_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB15_MFP1 Mask */ + +#define SYS_ALT_MFP_PC13_MFP1_Pos 21 /*!< GCR_T::ALT_MFP: PC13_MFP1 Position */ +#define SYS_ALT_MFP_PC13_MFP1_Msk (1ul << SYS_ALT_MFP_PC13_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC13_MFP1 Mask */ + +#define SYS_ALT_MFP_PC12_MFP1_Pos 20 /*!< GCR_T::ALT_MFP: PC12_MFP1 Position */ +#define SYS_ALT_MFP_PC12_MFP1_Msk (1ul << SYS_ALT_MFP_PC12_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC12_MFP1 Mask */ + +#define SYS_ALT_MFP_PB5_MFP1_Pos 18 /*!< GCR_T::ALT_MFP: PB5_MFP1 Position */ +#define SYS_ALT_MFP_PB5_MFP1_Msk (1ul << SYS_ALT_MFP_PB5_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB5_MFP1 Mask */ + +#define SYS_ALT_MFP_PB6_MFP1_Pos 17 /*!< GCR_T::ALT_MFP: PB6_MFP1 Position */ +#define SYS_ALT_MFP_PB6_MFP1_Msk (1ul << SYS_ALT_MFP_PB6_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB6_MFP1 Mask */ + +#define SYS_ALT_MFP_PB7_MFP1_Pos 16 /*!< GCR_T::ALT_MFP: PB7_MFP1 Position */ +#define SYS_ALT_MFP_PB7_MFP1_Msk (1ul << SYS_ALT_MFP_PB7_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB7_MFP1 Mask */ + +#define SYS_ALT_MFP_PB4_MFP1_Pos 15 /*!< GCR_T::ALT_MFP: PB4_MFP1 Position */ +#define SYS_ALT_MFP_PB4_MFP1_Msk (1ul << SYS_ALT_MFP_PB4_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB4_MFP1 Mask */ + +#define SYS_ALT_MFP_PA10_MFP1_Pos 12 /*!< GCR_T::ALT_MFP: PA10_MFP1 Position */ +#define SYS_ALT_MFP_PA10_MFP1_Msk (1ul << SYS_ALT_MFP_PA10_MFP1_Pos) /*!< GCR_T::ALT_MFP: PA10_MFP1 Mask */ + +#define SYS_ALT_MFP_PA11_MFP1_Pos 11 /*!< GCR_T::ALT_MFP: PA11_MFP1 Position */ +#define SYS_ALT_MFP_PA11_MFP1_Msk (1ul << SYS_ALT_MFP_PA11_MFP1_Pos) /*!< GCR_T::ALT_MFP: PA11_MFP1 Mask */ + +#define SYS_ALT_MFP_PB12_MFP1_Pos 10 /*!< GCR_T::ALT_MFP: PB12_MFP1 Position */ +#define SYS_ALT_MFP_PB12_MFP1_Msk (1ul << SYS_ALT_MFP_PB12_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB12_MFP1 Mask */ + +#define SYS_ALT_MFP_PA15_MFP1_Pos 9 /*!< GCR_T::ALT_MFP: A15_MFP1 Position */ +#define SYS_ALT_MFP_PA15_MFP1_Msk (1ul << SYS_ALT_MFP_PA15_MFP1_Pos) /*!< GCR_T::ALT_MFP: A15_MFP1 Mask */ + +#define SYS_ALT_MFP_PC3_MFP1_Pos 8 /*!< GCR_T::ALT_MFP: PC3_MFP1 Position */ +#define SYS_ALT_MFP_PC3_MFP1_Msk (1ul << SYS_ALT_MFP_PC3_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC3_MFP1 Mask */ + +#define SYS_ALT_MFP_PC2_MFP1_Pos 7 /*!< GCR_T::ALT_MFP: PC2_MFP1 Position */ +#define SYS_ALT_MFP_PC2_MFP1_Msk (1ul << SYS_ALT_MFP_PC2_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC2_MFP1 Mask */ + +#define SYS_ALT_MFP_PC1_MFP1_Pos 6 /*!< GCR_T::ALT_MFP: PC1_MFP1 Position */ +#define SYS_ALT_MFP_PC1_MFP1_Msk (1ul << SYS_ALT_MFP_PC1_MFP1_Pos) /*!< GCR_T::ALT_MFP: PC1_MFP1 Mask */ + +#define SYS_ALT_MFP_PC0_MFP1_Pos 5 /*!< GCR_T::ALT_MFP: PC0_MFP1 Position */ +#define SYS_ALT_MFP_PC0_MFP1_Msk (1ul << SYS_ALT_MFP_PC0_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB0_MFP1 Mask */ + +#define SYS_ALT_MFP_PB9_MFP1_Pos 1 /*!< GCR_T::ALT_MFP: PB9_MFP1 Position */ +#define SYS_ALT_MFP_PB9_MFP1_Msk (1ul << SYS_ALT_MFP_PB9_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB9_MFP1 Mask */ + +#define SYS_ALT_MFP_PB10_MFP1_Pos 0 /*!< GCR_T::ALT_MFP: PB10_MFP1 Position */ +#define SYS_ALT_MFP_PB10_MFP1_Msk (1ul << SYS_ALT_MFP_PB10_MFP1_Pos) /*!< GCR_T::ALT_MFP: PB10_MFP1 Mask */ + +/* GCR ALTMFP1 Bit Field Definitions */ +#define SYS_ALT_MFP1_PF3_MFP1_Pos 26 /*!< GCR_T::ALT_MFP1: PF3_MFP1 Position */ +#define SYS_ALT_MFP1_PF3_MFP1_Msk (0x3ul << SYS_ALT_MFP1_PF3_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PF3_MFP1 Mask */ + +#define SYS_ALT_MFP1_PF2_MFP1_Pos 24 /*!< GCR_T::ALT_MFP1: PF2_MFP1 Position */ +#define SYS_ALT_MFP1_PF2_MFP1_Msk (0x3ul << SYS_ALT_MFP1_PF2_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PF2_MFP1 Mask */ + +#define SYS_ALT_MFP1_PC8_MFP1_Pos 23 /*!< GCR_T::ALT_MFP1: PC8_MFP1 Position */ +#define SYS_ALT_MFP1_PC8_MFP1_Msk (1ul << SYS_ALT_MFP1_PC8_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PC8_MFP1 Mask */ + +#define SYS_ALT_MFP1_PD5_MFP1_Pos 21 /*!< GCR_T::ALT_MFP1: PD5_MFP1 Position */ +#define SYS_ALT_MFP1_PD5_MFP1_Msk (1ul << SYS_ALT_MFP1_PD5_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PD5_MFP1 Mask */ + +#define SYS_ALT_MFP1_PD4_MFP1_Pos 20 /*!< GCR_T::ALT_MFP1: PD4_MFP1 Position */ +#define SYS_ALT_MFP1_PD4_MFP1_Msk (1ul << SYS_ALT_MFP1_PD4_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PD4_MFP1 Mask */ + +#define SYS_ALT_MFP1_PD3_MFP1_Pos 19 /*!< GCR_T::ALT_MFP1: PD3_MFP1 Position */ +#define SYS_ALT_MFP1_PD3_MFP1_Msk (1ul << SYS_ALT_MFP1_PD3_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PD3_MFP1 Mask */ + +#define SYS_ALT_MFP1_PD2_MFP1_Pos 18 /*!< GCR_T::ALT_MFP1: PD2_MFP1 Position */ +#define SYS_ALT_MFP1_PD2_MFP1_Msk (1ul << SYS_ALT_MFP1_PD2_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PD2_MFP1 Mask */ + +#define SYS_ALT_MFP1_PD1_MFP1_Pos 17 /*!< GCR_T::ALT_MFP1: PD1_MFP1 Position */ +#define SYS_ALT_MFP1_PD1_MFP1_Msk (1ul << SYS_ALT_MFP1_PD1_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PD1_MFP1 Mask */ + +#define SYS_ALT_MFP1_PD0_MFP1_Pos 16 /*!< GCR_T::ALT_MFP1: PD0_MFP1 Position */ +#define SYS_ALT_MFP1_PD0_MFP1_Msk (1ul << SYS_ALT_MFP1_PD0_MFP1_Pos) /*!< GCR_T::ALT_MFP1: PD0_MFP1 Mask */ + +/* GCR GPA_IOCR Bit Field Definitions */ +#define SYS_GPA_IOCR_GPA11_DS_Pos 11 /*!< GCR_T::GPA_IOCR: GPA11_DS Position */ +#define SYS_GPA_IOCR_GPA11_DS_Msk (0x3ul << SYS_GPA_IOCR_GPA11_DS_Pos) /*!< GCR_T::GPA_IOCR: GPA11_DS Mask */ + +#define SYS_GPA_IOCR_GPA10_DS_Pos 10 /*!< GCR_T::GPA_IOCR: GPA10_DS Position */ +#define SYS_GPA_IOCR_GPA10_DS_Msk (0x3ul << SYS_GPA_IOCR_GPA10_DS_Pos) /*!< GCR_T::GPA_IOCR: GPA10_DS Mask */ + +/* GCR GPB_IOCR Bit Field Definitions */ +#define SYS_GPB_IOCR_GPB14_DS_Pos 14 /*!< GCR_T::GPB_IOCR: GPB14_DS Position */ +#define SYS_GPB_IOCR_GPB14_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB14_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB14_DS Mask */ + +#define SYS_GPB_IOCR_GPB13_DS_Pos 13 /*!< GCR_T::GPB_IOCR: GPB13_DS Position */ +#define SYS_GPB_IOCR_GPB13_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB13_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB13_DS Mask */ + +#define SYS_GPB_IOCR_GPB12_DS_Pos 12 /*!< GCR_T::GPB_IOCR: GPB12_DS Position */ +#define SYS_GPB_IOCR_GPB12_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB12_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB12_DS Mask */ + +#define SYS_GPB_IOCR_GPB8_DS_Pos 8 /*!< GCR_T::GPB_IOCR: GPB8_DS Position */ +#define SYS_GPB_IOCR_GPB8_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB8_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB8_DS Mask */ + +#define SYS_GPB_IOCR_GPB7_DS_Pos 7 /*!< GCR_T::GPB_IOCR: GPB7_DS Position */ +#define SYS_GPB_IOCR_GPB7_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB7_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB7_DS Mask */ + +#define SYS_GPB_IOCR_GPB6_DS_Pos 6 /*!< GCR_T::GPB_IOCR: GPB6_DS Position */ +#define SYS_GPB_IOCR_GPB6_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB6_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB6_DS Mask */ + +#define SYS_GPB_IOCR_GPB5_DS_Pos 5 /*!< GCR_T::GPB_IOCR: GPB5_DS Position */ +#define SYS_GPB_IOCR_GPB5_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB5_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB5_DS Mask */ + +#define SYS_GPB_IOCR_GPB4_DS_Pos 4 /*!< GCR_T::GPB_IOCR: GPB4_DS Position */ +#define SYS_GPB_IOCR_GPB4_DS_Msk (0x3ul << SYS_GPB_IOCR_GPB4_DS_Pos) /*!< GCR_T::GPB_IOCR: GPB4_DS Mask */ + +/* GCR GPD_IOCR Bit Field Definitions */ +#define SYS_GPD_IOCR_GPD11_DS_Pos 11 /*!< GCR_T::GPD_IOCR: GPD11_DS Position */ +#define SYS_GPD_IOCR_GPD11_DS_Msk (0x3ul << SYS_GPD_IOCR_GPD11_DS_Pos) /*!< GCR_T::GPD_IOCR: GPD11_DS Mask */ + +#define SYS_GPD_IOCR_GPD10_DS_Pos 10 /*!< GCR_T::GPD_IOCR: GPD10_DS Position */ +#define SYS_GPD_IOCR_GPD10_DS_Msk (0x3ul << SYS_GPD_IOCR_GPD10_DS_Pos) /*!< GCR_T::GPD_IOCR: GPD10_DS Mask */ + +#define SYS_GPD_IOCR_GPD9_DS_Pos 9 /*!< GCR_T::GPD_IOCR: GPD9_DS Position */ +#define SYS_GPD_IOCR_GPD9_DS_Msk (0x3ul << SYS_GPD_IOCR_GPD9_DS_Pos) /*!< GCR_T::GPD_IOCR: GPD9_DS Mask */ + +#define SYS_GPD_IOCR_GPD8_DS_Pos 8 /*!< GCR_T::GPD_IOCR: GPD8_DS Position */ +#define SYS_GPD_IOCR_GPD8_DS_Msk (0x3ul << SYS_GPD_IOCR_GPD8_DS_Pos) /*!< GCR_T::GPD_IOCR: GPD8_DS Mask */ + +/* GCR REGWRPROT Bit Field Definitions */ +#define SYS_REGWRPROT_REGWRPROT_Pos 0 /*!< GCR_T::REGWRPROT: REGWRPROT Position */ +#define SYS_REGWRPROT_REGWRPROT_Msk (0xFFul << SYS_REGWRPROT_REGWRPROT_Pos) /*!< GCR_T::REGWRPROT: REGWRPROT Mask */ + +#define SYS_REGWRPROT_REGPROTDIS_Pos 0 /*!< GCR_T::REGWRPROT: REGPROTDIS Position */ +#define SYS_REGWRPROT_REGPROTDIS_Msk (1ul << SYS_REGWRPROT_REGPROTDIS_Pos) /*!< GCR_T::REGWRPROT: REGPROTDIS Mask */ + +/* GCR GPA_MFPH Bit Field Definitions */ +#define SYS_GPA_MFPH_GPA15_MFP_Pos 28 /*!< GCR_T::GPA_MFPH: GPA15_MFP Position */ +#define SYS_GPA_MFPH_GPA15_MFP_Msk (0x7ul << SYS_GPA_MFPH_GPA15_MFP_Pos) /*!< GCR_T::GPA_MFPH: GPA15_MFP Mask */ + +#define SYS_GPA_MFPH_GPA14_MFP_Pos 24 /*!< GCR_T::GPA_MFPH: GPA14_MFP Position */ +#define SYS_GPA_MFPH_GPA14_MFP_Msk (0x7ul << SYS_GPA_MFPH_GPA14_MFP_Pos) /*!< GCR_T::GPA_MFPH: GPA14_MFP Mask */ + +#define SYS_GPA_MFPH_GPA13_MFP_Pos 20 /*!< GCR_T::GPA_MFPH: GPA13_MFP Position */ +#define SYS_GPA_MFPH_GPA13_MFP_Msk (0x7ul << SYS_GPA_MFPH_GPA13_MFP_Pos) /*!< GCR_T::GPA_MFPH: GPA13_MFP Mask */ + +#define SYS_GPA_MFPH_GPA12_MFP_Pos 16 /*!< GCR_T::GPA_MFPH: GPA12_MFP Position */ +#define SYS_GPA_MFPH_GPA12_MFP_Msk (0x7ul << SYS_GPA_MFPH_GPA12_MFP_Pos) /*!< GCR_T::GPA_MFPH: GPA12_MFP Mask */ + +#define SYS_GPA_MFPH_GPA11_MFP_Pos 12 /*!< GCR_T::GPA_MFPH: GPA11_MFP Position */ +#define SYS_GPA_MFPH_GPA11_MFP_Msk (0x7ul << SYS_GPA_MFPH_GPA11_MFP_Pos) /*!< GCR_T::GPA_MFPH: GPA11_MFP Mask */ + +#define SYS_GPA_MFPH_GPA10_MFP_Pos 8 /*!< GCR_T::GPA_MFPH: GPA10_MFP Position */ +#define SYS_GPA_MFPH_GPA10_MFP_Msk (0x7ul << SYS_GPA_MFPH_GPA10_MFP_Pos) /*!< GCR_T::GPA_MFPH: GPA10_MFP Mask */ + +/* GCR GPB_MFPL Bit Field Definitions */ +#define SYS_GPB_MFPL_GPB7_MFP_Pos 28 /*!< GCR_T::GPB_MFPL: GPB7_MFP Position */ +#define SYS_GPB_MFPL_GPB7_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB7_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB7_MFP Mask */ + +#define SYS_GPB_MFPL_GPB6_MFP_Pos 24 /*!< GCR_T::GPB_MFPL: GPB6_MFP Position */ +#define SYS_GPB_MFPL_GPB6_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB6_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB6_MFP Mask */ + +#define SYS_GPB_MFPL_GPB5_MFP_Pos 20 /*!< GCR_T::GPB_MFPL: GPB5_MFP Position */ +#define SYS_GPB_MFPL_GPB5_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB5_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB5_MFP Mask */ + +#define SYS_GPB_MFPL_GPB4_MFP_Pos 16 /*!< GCR_T::GPB_MFPL: GPB4_MFP Position */ +#define SYS_GPB_MFPL_GPB4_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB4_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB4_MFP Mask */ + +#define SYS_GPB_MFPL_GPB3_MFP_Pos 12 /*!< GCR_T::GPB_MFPL: GPB3_MFP Position */ +#define SYS_GPB_MFPL_GPB3_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB3_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB3_MFP Mask */ + +#define SYS_GPB_MFPL_GPB2_MFP_Pos 8 /*!< GCR_T::GPB_MFPL: GPB2_MFP Position */ +#define SYS_GPB_MFPL_GPB2_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB2_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB2_MFP Mask */ + +#define SYS_GPB_MFPL_GPB1_MFP_Pos 4 /*!< GCR_T::GPB_MFPL: GPB1_MFP Position */ +#define SYS_GPB_MFPL_GPB1_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB1_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB1_MFP Mask */ + +#define SYS_GPB_MFPL_GPB0_MFP_Pos 0 /*!< GCR_T::GPB_MFPL: GPB0_MFP Position */ +#define SYS_GPB_MFPL_GPB0_MFP_Msk (0x7ul << SYS_GPB_MFPL_GPB0_MFP_Pos) /*!< GCR_T::GPB_MFPL: GPB0_MFP Mask */ + +/* GCR GPB_MFPH Bit Field Definitions */ +#define SYS_GPB_MFPH_GPB15_MFP_Pos 28 /*!< GCR_T::GPB_MFPH: GPB15_MFP Position */ +#define SYS_GPB_MFPH_GPB15_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB15_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB15_MFP Mask */ + +#define SYS_GPB_MFPH_GPB14_MFP_Pos 24 /*!< GCR_T::GPB_MFPH: GPB14_MFP Position */ +#define SYS_GPB_MFPH_GPB14_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB14_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB14_MFP Mask */ + +#define SYS_GPB_MFPH_GPB13_MFP_Pos 20 /*!< GCR_T::GPB_MFPH: GPB13_MFP Position */ +#define SYS_GPB_MFPH_GPB13_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB13_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB13_MFP Mask */ + +#define SYS_GPB_MFPH_GPB12_MFP_Pos 16 /*!< GCR_T::GPB_MFPH: GPB12_MFP Position */ +#define SYS_GPB_MFPH_GPB12_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB12_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB12_MFP Mask */ + +#define SYS_GPB_MFPH_GPB10_MFP_Pos 8 /*!< GCR_T::GPB_MFPH: GPB10_MFP Position */ +#define SYS_GPB_MFPH_GPB10_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB10_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB10_MFP Mask */ + +#define SYS_GPB_MFPH_GPB9_MFP_Pos 4 /*!< GCR_T::GPB_MFPH: GPB9_MFP Position */ +#define SYS_GPB_MFPH_GPB9_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB9_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB9_MFP Mask */ + +#define SYS_GPB_MFPH_GPB8_MFP_Pos 0 /*!< GCR_T::GPB_MFPH: GPB8_MFP Position */ +#define SYS_GPB_MFPH_GPB8_MFP_Msk (0x7ul << SYS_GPB_MFPH_GPB8_MFP_Pos) /*!< GCR_T::GPB_MFPH: GPB8_MFP Mask */ + +/* GCR GPC_MFPL Bit Field Definitions */ +#define SYS_GPC_MFPL_GPC5_MFP_Pos 20 /*!< GCR_T::GPC_MFPL: GPC5_MFP Position */ +#define SYS_GPC_MFPL_GPC5_MFP_Msk (0x7ul << SYS_GPC_MFPL_GPC5_MFP_Pos) /*!< GCR_T::GPC_MFPL: GPC5_MFP Mask */ + +#define SYS_GPC_MFPL_GPC4_MFP_Pos 16 /*!< GCR_T::GPC_MFPL: GPC4_MFP Position */ +#define SYS_GPC_MFPL_GPC4_MFP_Msk (0x7ul << SYS_GPC_MFPL_GPC4_MFP_Pos) /*!< GCR_T::GPC_MFPL: GPC4_MFP Mask */ + +#define SYS_GPC_MFPL_GPC3_MFP_Pos 12 /*!< GCR_T::GPC_MFPL: GPC3_MFP Position */ +#define SYS_GPC_MFPL_GPC3_MFP_Msk (0x7ul << SYS_GPC_MFPL_GPC3_MFP_Pos) /*!< GCR_T::GPC_MFPL: GPC3_MFP Mask */ + +#define SYS_GPC_MFPL_GPC2_MFP_Pos 8 /*!< GCR_T::GPC_MFPL: GPC2_MFP Position */ +#define SYS_GPC_MFPL_GPC2_MFP_Msk (0x7ul << SYS_GPC_MFPL_GPC2_MFP_Pos) /*!< GCR_T::GPC_MFPL: GPC2_MFP Mask */ + +#define SYS_GPC_MFPL_GPC1_MFP_Pos 4 /*!< GCR_T::GPC_MFPL: GPC1_MFP Position */ +#define SYS_GPC_MFPL_GPC1_MFP_Msk (0x7ul << SYS_GPC_MFPL_GPC1_MFP_Pos) /*!< GCR_T::GPC_MFPL: GPC1_MFP Mask */ + +#define SYS_GPC_MFPL_GPC0_MFP_Pos 0 /*!< GCR_T::GPC_MFPL: GPC0_MFP Position */ +#define SYS_GPC_MFPL_GPC0_MFP_Msk (0x7ul << SYS_GPC_MFPL_GPC0_MFP_Pos) /*!< GCR_T::GPC_MFPL: GPC0_MFP Mask */ + +/* GCR GPC_MFPH Bit Field Definitions */ +#define SYS_GPC_MFPH_GPC13_MFP_Pos 20 /*!< GCR_T::GPC_MFPH: GPC13_MFP Position */ +#define SYS_GPC_MFPH_GPC13_MFP_Msk (0x7ul << SYS_GPC_MFPH_GPC13_MFP_Pos) /*!< GCR_T::GPC_MFPH: GPC13_MFP Mask */ + +#define SYS_GPC_MFPH_GPC12_MFP_Pos 16 /*!< GCR_T::GPC_MFPH: GPC12_MFP Position */ +#define SYS_GPC_MFPH_GPC12_MFP_Msk (0x7ul << SYS_GPC_MFPH_GPC12_MFP_Pos) /*!< GCR_T::GPC_MFPH: GPC12_MFP Mask */ + +#define SYS_GPC_MFPH_GPC11_MFP_Pos 12 /*!< GCR_T::GPC_MFPH: GPC11_MFP Position */ +#define SYS_GPC_MFPH_GPC11_MFP_Msk (0x7ul << SYS_GPC_MFPH_GPC11_MFP_Pos) /*!< GCR_T::GPC_MFPH: GPC11_MFP Mask */ + +#define SYS_GPC_MFPH_GPC10_MFP_Pos 8 /*!< GCR_T::GPC_MFPH: GPC11_MFP Position */ +#define SYS_GPC_MFPH_GPC10_MFP_Msk (0x7ul << SYS_GPC_MFPH_GPC10_MFP_Pos) /*!< GCR_T::GPC_MFPH: GPC11_MFP Mask */ + +#define SYS_GPC_MFPH_GPC9_MFP_Pos 4 /*!< GCR_T::GPC_MFPH: GPC9_MFP Position */ +#define SYS_GPC_MFPH_GPC9_MFP_Msk (0x7ul << SYS_GPC_MFPH_GPC9_MFP_Pos) /*!< GCR_T::GPC_MFPH: GPC9_MFP Mask */ + +#define SYS_GPC_MFPH_GPC8_MFP_Pos 0 /*!< GCR_T::GPC_MFPH: GPC8_MFP Position */ +#define SYS_GPC_MFPH_GPC8_MFP_Msk (0x7ul << SYS_GPC_MFPH_GPC8_MFP_Pos) /*!< GCR_T::GPC_MFPH: GPC8_MFP Mask */ + +/* GCR GPD_MFPL Bit Field Definitions */ +#define SYS_GPD_MFPL_GPD5_MFP_Pos 20 /*!< GCR_T::GPD_MFPL: GPD5_MFP Position */ +#define SYS_GPD_MFPL_GPD5_MFP_Msk (0x7ul << SYS_GPD_MFPL_GPD5_MFP_Pos) /*!< GCR_T::GPD_MFPL: GPD5_MFP Mask */ + +#define SYS_GPD_MFPL_GPD4_MFP_Pos 16 /*!< GCR_T::GPD_MFPL: GPD4_MFP Position */ +#define SYS_GPD_MFPL_GPD4_MFP_Msk (0x7ul << SYS_GPD_MFPL_GPD4_MFP_Pos) /*!< GCR_T::GPD_MFPL: GPD4_MFP Mask */ + +#define SYS_GPD_MFPL_GPD3_MFP_Pos 12 /*!< GCR_T::GPD_MFPL: GPD3_MFP Position */ +#define SYS_GPD_MFPL_GPD3_MFP_Msk (0x7ul << SYS_GPD_MFPL_GPD3_MFP_Pos) /*!< GCR_T::GPD_MFPL: GPD3_MFP Mask */ + +#define SYS_GPD_MFPL_GPD2_MFP_Pos 8 /*!< GCR_T::GPD_MFPL: GPD2_MFP Position */ +#define SYS_GPD_MFPL_GPD2_MFP_Msk (0x7ul << SYS_GPD_MFPL_GPD2_MFP_Pos) /*!< GCR_T::GPD_MFPL: GPD2_MFP Mask */ + +#define SYS_GPD_MFPL_GPD1_MFP_Pos 4 /*!< GCR_T::GPD_MFPL: GPD1_MFP Position */ +#define SYS_GPD_MFPL_GPD1_MFP_Msk (0x7ul << SYS_GPD_MFPL_GPD1_MFP_Pos) /*!< GCR_T::GPD_MFPL: GPD1_MFP Mask */ + +#define SYS_GPD_MFPL_GPD0_MFP_Pos 0 /*!< GCR_T::GPD_MFPL: GPD0_MFP Position */ +#define SYS_GPD_MFPL_GPD0_MFP_Msk (0x7ul << SYS_GPD_MFPL_GPD0_MFP_Pos) /*!< GCR_T::GPD_MFPL: GPD0_MFP Mask */ + +/* GCR GPD_MFPH Bit Field Definitions */ +#define SYS_GPD_MFPH_GPD11_MFP_Pos 12 /*!< GCR_T::GPD_MFPH: GPD11_MFP Position */ +#define SYS_GPD_MFPH_GPD11_MFP_Msk (0x7ul << SYS_GPD_MFPH_GPD11_MFP_Pos) /*!< GCR_T::GPD_MFPH: GPD11_MFP Mask */ + +#define SYS_GPD_MFPH_GPD10_MFP_Pos 8 /*!< GCR_T::GPD_MFPH: GPD10_MFP Position */ +#define SYS_GPD_MFPH_GPD10_MFP_Msk (0x7ul << SYS_GPD_MFPH_GPD10_MFP_Pos) /*!< GCR_T::GPD_MFPH: GPD10_MFP Mask */ + +#define SYS_GPD_MFPH_GPD9_MFP_Pos 4 /*!< GCR_T::GPD_MFPH: GPD9_MFP Position */ +#define SYS_GPD_MFPH_GPD9_MFP_Msk (0x7ul << SYS_GPD_MFPH_GPD9_MFP_Pos) /*!< GCR_T::GPD_MFPH: GPD9_MFP Mask */ + +#define SYS_GPD_MFPH_GPD8_MFP_Pos 0 /*!< GCR_T::GPD_MFPH: GPD8_MFP Position */ +#define SYS_GPD_MFPH_GPD8_MFP_Msk (0x7ul << SYS_GPD_MFPH_GPD8_MFP_Pos) /*!< GCR_T::GPD_MFPH: GPD8_MFP Mask */ + +/* GCR GPF_MFPL Bit Field Definitions */ +#define SYS_GPF_MFPL_GPF3_MFP_Pos 12 /*!< GCR_T::GPF_MFPL: GPF3_MFP Position */ +#define SYS_GPF_MFPL_GPF3_MFP_Msk (0x7ul << SYS_GPF_MFPL_GPF3_MFP_Pos) /*!< GCR_T::GPF_MFPL: GPF3_MFP Mask */ + +#define SYS_GPF_MFPL_GPF2_MFP_Pos 8 /*!< GCR_T::GPF_MFPL: GPF2_MFP Position */ +#define SYS_GPF_MFPL_GPF2_MFP_Msk (0x7ul << SYS_GPF_MFPL_GPF2_MFP_Pos) /*!< GCR_T::GPF_MFPL: GPF2_MFP Mask */ + +#define SYS_GPF_MFPL_GPF1_MFP_Pos 4 /*!< GCR_T::GPF_MFPL: GPF1_MFP Position */ +#define SYS_GPF_MFPL_GPF1_MFP_Msk (0x7ul << SYS_GPF_MFPL_GPF1_MFP_Pos) /*!< GCR_T::GPF_MFPL: GPF1_MFP Mask */ + +#define SYS_GPF_MFPL_GPF0_MFP_Pos 0 /*!< GCR_T::GPF_MFPL: GPF0_MFP Position */ +#define SYS_GPF_MFPL_GPF0_MFP_Msk (0x7ul << SYS_GPF_MFPL_GPF0_MFP_Pos) /*!< GCR_T::GPF_MFPL: GPF0_MFP Mask */ + + +/*@}*/ /* end of group REG_SYS_BITMASK */ + + + +typedef struct +{ + + +/** + * @var GCR_INT_T::IRQSRC + * Offset: 0x00~0x7C IRQ0~IRQ31 Interrupt Source Identity + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |INT_SRC |Interrupt Source + * | | |Define the interrupt sources for interrupt event. + * @var GCR_INT_T::NMISEL + * Offset: 0x80 NMI Source Interrupt Select Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[4:0] |NMI_SEL |NMI Interrupt Source Selection + * | | |The NMI interrupt to Cortex-M0 can be selected from one of the peripheral interrupt by setting NMI_SEL with corresponding interrupt number. + * |[8] |NMI_EN |NMI Interrupt Enable Bit (Write Protect) + * | | |0 = NMI interrupt Disabled. + * | | |1 = NMI interrupt Enabled. + * | | |Note: This bit is write protected bit. Refer to the REGWRPROT register. + * @var GCR_INT_T::MCUIRQ + * Offset: 0x84 MCU Interrupt Request Source Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |MCU_IRQ |MCU IRQ Source Register + * | | |The MCU_IRQ collects all the interrupts from the peripherals and generates the synchronous interrupt to Cortex-M0. There are two modes to generate interrupt to Cortex-M0, Normal mode and Test mode. + * | | |The MCU_IRQ collects all interrupts from each peripheral and synchronizes them and then interrupts the Cortex-M0. + * | | |When the MCU_IRQ[n] is 0: Set MCU_IRQ[n] 1 will generate an interrupt to Cortex_M0 NVIC[n]. + * | | |When the MCU_IRQ[n] is 1 (mean an interrupt is assert), setting 1 to the MCU_IRQ[n] will clear the interrupt and setting MCU_IRQ[n] 0 has no effect. + */ + + __I uint32_t IRQSRC[32]; /* Offset: 0x00~0x7C IRQ0~IRQ31 Interrupt Source Identity */ + __IO uint32_t NMISEL; /* Offset: 0x80 NMI Source Interrupt Select Control Register */ + __IO uint32_t MCUIRQ; /* Offset: 0x84 MCU Interrupt Request Source Register */ + +} GCR_INT_T; + + + +/** @addtogroup REG_INT_BITMASK INT Bit Mask + @{ + */ + +/* INT IRQSRC Bit Field Definitions */ +#define INT_IRQSRC_INT_SRC_Pos 0 /*!< GCR_INT_T::IRQSRC: INT_SRC Position */ +#define INT_IRQSRC_INT_SRC_Msk (0xFul << INT_IRQSRC_INT_SRC_Pos) /*!< GCR_INT_T::IRQSRC: INT_SRC Mask */ + +/* INT NMI_SEL Bit Field Definitions */ +#define INT_NMI_SEL_NMI_EN_Pos 8 /*!< GCR_INT_T::NMISEL: NMI_EN Position */ +#define INT_NMI_SEL_NMI_EN_Msk (1ul << INT_NMI_SEL_NMI_EN_Pos) /*!< GCR_INT_T::NMISEL: NMI_EN Mask */ + +#define INT_NMI_SEL_NMI_SEL_Pos 0 /*!< GCR_INT_T::NMISEL: NMI_SEL Position */ +#define INT_NMI_SEL_NMI_SEL_Msk (0x1Ful << INT_NMI_SEL_NMI_SEL_Pos) /*!< GCR_INT_T::NMISEL: NMI_SEL Mask */ + +/* INT MCUIRQ Bit Field Definitions */ +#define INT_MCUIRQ_MCU_IRQ_Pos 0 /*!< GCR_INT_T::MCUIRQ: MCU_IRQ Position */ +#define INT_MCUIRQ_MCU_IRQ_Msk (0xFFFFFFFFul << INT_MCUIRQ_MCU_IRQ_Pos) /*!< GCR_INT_T::MCUIRQ: MCU_IRQ Mask */ + +/*@}*/ /* end of group REG_SYS_BITMASK */ +/*@}*/ /* end of group REG_SYS*/ + +/*----------------------------- Timer Controller (TMR) -----------------------------*/ +/** @addtogroup REG_TIMER Timer Controller (TIMER) + Memory Mapped Structure for Timer Controller + @{ + */ + +typedef struct +{ + + +/** + * @var TIMER_T::TCSR + * Offset: 0x00 Timer Control and Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |PRESCALE |Prescale Counter + * | | |Timer input clock source is divided by (PRESCALE+1) before it is fed to the Timer up counter. + * | | |If this field is 0 (PRESCALE = 0), then there is no scaling. + * |[16] |TDR_EN |Data Load Enable Control + * | | |When TDR_EN is set, TDR (Timer Data Register) will be updated continuously with the 24-bit up-timer value as the timer is counting. + * | | |0 = Timer Data Register update Disabled. + * | | |1 = Timer Data Register update Enabled while Timer counter is active. + * |[23] |WAKE_EN |Wake Up Function Enable Control + * | | |0 = Wake-up trigger event Disabled. + * | | |1 = Wake-up trigger event Enabled. + * |[24] |CTB |Counter Mode Enable Control + * | | |This bit is for external counting pin function enabled. + * | | |When timer is used as an event counter, this bit should be set to 1 and select HCLK as timer clock source. + * | | |0 = External counter mode Disabled. + * | | |1 = External counter mode Enabled. + * |[25] |CACT |Timer Active Status (Read Only) + * | | |This bit indicates the 24-bit up counter status. + * | | |0 = 24-bit up counter is not active. + * | | |1 = 24-bit up counter is active. + * |[26] |CRST |Timer Reset + * | | |0 = No effect. + * | | |1 = Reset 8-bit prescale counter, 24-bit up counter value and CEN bit if CACT is 1. + * |[28:27] |MODE |Timer Operating Mode + * | | |00 = The Timer controller is operated in One-shot mode. + * | | |01 = The Timer controller is operated in Periodic mode. + * | | |10 = The Timer controller is operated in Toggle-output mode. + * | | |11 = The Timer controller is operated in Continuous Counting mode. + * |[29] |IE |Interrupt Enable Control + * | | |0 = Timer Interrupt function Disabled. + * | | |1 = Timer Interrupt function Enabled. + * | | |If this bit is enabled, when the timer interrupt flag (TISR[0] TIF) is set to 1, the timer interrupt signal is generated and inform to CPU. + * |[30] |CEN |Timer Enable Control + * | | |0 = Stops/Suspends counting. + * | | |1 = Starts counting. + * | | |Note1: In stop status, and then set CEN to 1 will enable the 24-bit up counter to keep counting from the last stop counting value. + * | | |Note2: This bit is auto-cleared by hardware in one-shot mode (TCSR [28:27] = 00) when the timer interrupt flag (TISR[0] TIF) is generated. + * |[31] |DBGACK_TMR|ICE Debug Mode Acknowledge Disable (Write Protect) + * | | |0 = ICE debug mode acknowledgment effects TIMER counting. + * | | |TIMER counter will be held while CPU is held by ICE. + * | | |1 = ICE debug mode acknowledgment Disabled. + * | | |TIMER counter will keep going no matter CPU is held by ICE or not. + * @var TIMER_T::TCMPR + * Offset: 0x04 Timer Compare Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[23:0] |TCMP |Timer Compared Value + * | | |TCMP is a 24-bit compared value register. + * | | |When the internal 24-bit up counter value is equal to TCMP value, the TIF flag will set to 1. + * | | |Time-out period = (Period of Timer clock input) * (8-bit PRESCALE + 1) * (24-bit TCMP). + * | | |Note1: Never write 0x0 or 0x1 in TCMP field, or the core will run into unknown state. + * | | |Note2: When timer is operating at continuous counting mode, the 24-bit up counter will keep counting continuously even if user writes a new value into TCMP field. + * | | |But if timer is operating at other modes, the 24-bit up counter will restart counting and using newest TCMP value to be the timer compared value if user writes a new value into TCMP field. + * @var TIMER_T::TISR + * Offset: 0x08 Timer Interrupt Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TIF |Timer Interrupt Flag + * | | |This bit indicates the interrupt flag status of Timer while TDR value reaches to TCMP value. + * | | |0 = No effect. + * | | |1 = TDR value matches the TCMP value. + * | | |Note: This bit is cleared by writing 1 to it. + * |[1] |TWF |Timer Wake-Up Flag + * | | |This bit indicates the interrupt wake-up flag status of Timer. + * | | |0 = Timer does not cause CPU wake-up. + * | | |1 = CPU wake-up from Idle or Power-down mode if Timer time-out interrupt signal generated. + * | | |Note: This bit is cleared by writing 1 to it. + * @var TIMER_T::TDR + * Offset: 0x0C Timer Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[23:0] |TDR |Timer Data Register + * | | |If TDR_EN (TCSR[16]) is set to 1, TDR register will be updated continuously to monitor 24-bit up counter value. + * @var TIMER_T::TCAP + * Offset: 0x10 Timer Capture Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[23:0] |TCAP |Timer Capture Data Register + * | | |When TEXIF flag is set to 1, the current TDR value will be auto-loaded into this TCAP filed immediately. + * @var TIMER_T::TEXCON + * Offset: 0x14 Timer External Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TX_PHASE |Timer External Count Pin Phase Detect Selection + * | | |This bit indicates the detection phase of TMx pin. + * | | |0 = A falling edge of TMx pin will be counted. + * | | |1 = A rising edge of TMx pin will be counted. + * |[2:1] |TEX_EDGE |Timer External Capture Pin Edge Detect Selection + * | | |00 = A 1 to 0 transition on TMx_EXT pin will be detected. + * | | |01 = A 0 to 1 transition on TMx_EXT pin will be detected. + * | | |10 = Either 1 to 0 or 0 to 1 transition on TMx_EXT pin will be detected. + * | | |11 = Reserved. + * |[3] |TEXEN |Timer External Pin Function Enable + * | | |This bit enables the RSTCAPSEL function on the TxEX pin. + * | | |0 = RSTCAPSEL function of TxEX pin will be ignored. + * | | |1 = RSTCAPSEL function of TxEX pin is active. + * |[4] |RSTCAPSEL |Timer External Reset Counter / Timer External Capture Mode Selection + * | | |0 = Transition on TMx_EXT + * | | |pin is using to save the TDR value into TCAP value if TEXIF flag is set to 1. + * | | |1 = Transition on TMx_EXT pin is using to reset the 24-bit up counter. + * |[5] |TEXIEN |Timer External Capture Interrupt Enable Control + * | | |0 = TMx_EXT pin detection Interrupt Disabled. + * | | |1 = TMx_EXT pin detection Interrupt Enabled. + * | | |If TEXIEN enabled, Timer will raise an external capture interrupt signal and inform to CPU while TEXIF flag is set to 1. + * |[6] |TEXDB |Timer External Capture Input Pin De-Bounce Enable Control + * | | |0 = TMx_EXT pin de-bounce Disabled. + * | | |1 = TMx_EXT pin de-bounce Enabled. + * | | |If this bit is enabled, the edge detection of TMx_EXT pin is detected with de-bounce circuit. + * |[7] |TCDB |Timer External Counter Input Pin De-Bounce Enable Control + * | | |0 = TMx pin de-bounce Disabled. + * | | |1 = TMx pin de-bounce Enabled. + * | | |If this bit is enabled, the edge detection of TMx pin is detected with de-bounce circuit. + * @var TIMER_T::TEXISR + * Offset: 0x18 Timer External Interrupt Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |TEXIF |Timer External Capture Interrupt Flag + * | | |This bit indicates the external capture interrupt flag status. + * | | |When TEXEN enabled, TMx_EXT pin selected as external capture function, and a transition on TMx_EXT pin matched the TEX_EDGE setting, this flag will set to 1 by hardware. + * | | |1 = TMx_EXT + * | | |pin interrupt occurred. + * | | |0 = TMx_EXT + * | | |pin interrupt did not occur. + * | | |Note: This bit is cleared by writing 1 to it. + */ + + __IO uint32_t TCSR; /* Offset: 0x00 Timer Control and Status Register */ + __IO uint32_t TCMPR; /* Offset: 0x04 Timer Compare Register */ + __IO uint32_t TISR; /* Offset: 0x08 Timer Interrupt Status Register */ + __I uint32_t TDR; /* Offset: 0x0C Timer Data Register */ + __I uint32_t TCAP; /* Offset: 0x10 Timer Capture Data Register */ + __IO uint32_t TEXCON; /* Offset: 0x14 Timer External Control Register */ + __IO uint32_t TEXISR; /* Offset: 0x18 Timer External Interrupt Status Register */ + +} TIMER_T; + + + + +/** @addtogroup REG_TIMER_BITMASK TIMER Bit Mask + @{ + */ + + +/* TIMER TCSR Bit Field Definitions */ +#define TIMER_TCSR_DBGACK_TMR_Pos 31 /*!< TIMER_T::TCSR: DBGACK_TMR Position */ +#define TIMER_TCSR_DBGACK_TMR_Msk (1ul << TIMER_TCSR_DBGACK_TMR_Pos) /*!< TIMER_T::TCSR: DBGACK_TMR Mask */ + +#define TIMER_TCSR_CEN_Pos 30 /*!< TIMER_T::TCSR: CEN Position */ +#define TIMER_TCSR_CEN_Msk (1ul << TIMER_TCSR_CEN_Pos) /*!< TIMER_T::TCSR: CEN Mask */ + +#define TIMER_TCSR_IE_Pos 29 /*!< TIMER_T::TCSR: IE Position */ +#define TIMER_TCSR_IE_Msk (1ul << TIMER_TCSR_IE_Pos) /*!< TIMER_T::TCSR: IE Mask */ + +#define TIMER_TCSR_MODE_Pos 27 /*!< TIMER_T::TCSR: MODE Position */ +#define TIMER_TCSR_MODE_Msk (0x3ul << TIMER_TCSR_MODE_Pos) /*!< TIMER_T::TCSR: MODE Mask */ + +#define TIMER_TCSR_CRST_Pos 26 /*!< TIMER_T::TCSR: CRST Position */ +#define TIMER_TCSR_CRST_Msk (1ul << TIMER_TCSR_CRST_Pos) /*!< TIMER_T::TCSR: CRST Mask */ + +#define TIMER_TCSR_CACT_Pos 25 /*!< TIMER_T::TCSR: CACT Position */ +#define TIMER_TCSR_CACT_Msk (1ul << TIMER_TCSR_CACT_Pos) /*!< TIMER_T::TCSR: CACT Mask */ + +#define TIMER_TCSR_CTB_Pos 24 /*!< TIMER_T::TCSR: CTB Position */ +#define TIMER_TCSR_CTB_Msk (1ul << TIMER_TCSR_CTB_Pos) /*!< TIMER_T::TCSR: CTB Mask */ + +#define TIMER_TCSR_WAKE_EN_Pos 23 /*!< TIMER_T::TCSR: WAKE_EN Position */ +#define TIMER_TCSR_WAKE_EN_Msk (1ul << TIMER_TCSR_WAKE_EN_Pos) /*!< TIMER_T::TCSR: WAKE_EN Mask */ + +#define TIMER_TCSR_TDR_EN_Pos 16 /*!< TIMER_T::TCSR: TDR_EN Position */ +#define TIMER_TCSR_TDR_EN_Msk (1ul << TIMER_TCSR_TDR_EN_Pos) /*!< TIMER_T::TCSR: TDR_EN Mask */ + +#define TIMER_TCSR_PRESCALE_Pos 0 /*!< TIMER_T::TCSR: PRESCALE Position */ +#define TIMER_TCSR_PRESCALE_Msk (0xFFul << TIMER_TCSR_PRESCALE_Pos) /*!< TIMER_T::TCSR: PRESCALE Mask */ + +/* TIMER TCMPR Bit Field Definitions */ +#define TIMER_TCMP_TCMP_Pos 0 /*!< TIMER_T::TCMPR: TCMP Position */ +#define TIMER_TCMP_TCMP_Msk (0xFFFFFFul << TIMER_TCMP_TCMP_Pos) /*!< TIMER_T::TCMPR: TCMP Mask */ + +/* TIMER TISR Bit Field Definitions */ +#define TIMER_TISR_TWF_Pos 1 /*!< TIMER_T::TISR: TWF Position */ +#define TIMER_TISR_TWF_Msk (1ul << TIMER_TISR_TWF_Pos) /*!< TIMER_T::TISR: TWF Mask */ + +#define TIMER_TISR_TIF_Pos 0 /*!< TIMER_T::TISR: TIF Position */ +#define TIMER_TISR_TIF_Msk (1ul << TIMER_TISR_TIF_Pos) /*!< TIMER_T::TISR: TIF Mask */ + +/* TIMER TDR Bit Field Definitions */ +#define TIMER_TDR_TDR_Pos 0 /*!< TIMER_T::TDR: TDR Position */ +#define TIMER_TDR_TDR_Msk (0xFFFFFFul << TIMER_TDR_TDR_Pos) /*!< TIMER_T::TDR: TDR Mask */ + +/* TIMER TCAP Bit Field Definitions */ +#define TIMER_TCAP_TCAP_Pos 0 /*!< TIMER_T::TCAP: TCAP Position */ +#define TIMER_TCAP_TCAP_Msk (0xFFFFFFul << TIMER_TCAP_TCAP_Pos) /*!< TIMER_T::TCAP: TCAP Mask */ + +/* TIMER TEXCON Bit Field Definitions */ +#define TIMER_TEXCON_TCDB_Pos 7 /*!< TIMER_T::TEXCON: TCDB Position */ +#define TIMER_TEXCON_TCDB_Msk (1ul << TIMER_TEXCON_TCDB_Pos) /*!< TIMER_T::TEXCON: TCDB Mask */ + +#define TIMER_TEXCON_TEXDB_Pos 6 /*!< TIMER_T::TEXCON: TEXDB Position */ +#define TIMER_TEXCON_TEXDB_Msk (1ul << TIMER_TEXCON_TEXDB_Pos) /*!< TIMER_T::TEXCON: TEXDB Mask */ + +#define TIMER_TEXCON_TEXIEN_Pos 5 /*!< TIMER_T::TEXCON: TEXIEN Position */ +#define TIMER_TEXCON_TEXIEN_Msk (1ul << TIMER_TEXCON_TEXIEN_Pos) /*!< TIMER_T::TEXCON: TEXIEN Mask */ + +#define TIMER_TEXCON_RSTCAPSEL_Pos 4 /*!< TIMER_T::TEXCON: RSTCAPSEL Position */ +#define TIMER_TEXCON_RSTCAPSEL_Msk (1ul << TIMER_TEXCON_RSTCAPSEL_Pos) /*!< TIMER_T::TEXCON: RSTCAPSEL Mask */ + +#define TIMER_TEXCON_TEXEN_Pos 3 /*!< TIMER_T::TEXCON: TEXEN Position */ +#define TIMER_TEXCON_TEXEN_Msk (1ul << TIMER_TEXCON_TEXEN_Pos) /*!< TIMER_T::TEXCON: TEXEN Mask */ + +#define TIMER_TEXCON_TEX_EDGE_Pos 1 /*!< TIMER_T::TEXCON: TEX_EDGE Position */ +#define TIMER_TEXCON_TEX_EDGE_Msk (0x3ul << TIMER_TEXCON_TEX_EDGE_Pos) /*!< TIMER_T::TEXCON: TEX_EDGE Mask */ + +#define TIMER_TEXCON_TX_PHASE_Pos 0 /*!< TIMER_T::TEXCON: TX_PHASE Position */ +#define TIMER_TEXCON_TX_PHASE_Msk (1ul << TIMER_TEXCON_TX_PHASE_Pos) /*!< TIMER_T::TEXCON: TX_PHASE Mask */ + +/* TIMER TEXISR Bit Field Definitions */ +#define TIMER_TEXISR_TEXIF_Pos 0 /*!< TIMER_T::TEXISR: TEXIF Position */ +#define TIMER_TEXISR_TEXIF_Msk (1ul << TIMER_TEXISR_TEXIF_Pos) /*!< TIMER_T::TEXISR: TEXIF Mask */ +/*@}*/ /* end of group REG_TIMER_BITMASK */ +/*@}*/ /* end of group REG_TIMER */ + + +/*------------------------- UART Interface Controller ------------------------*/ + +/** @addtogroup REG_UART Universal Asynchronous Receiver/Transmitter Controller (UART) + Memory Mapped Structure for UART Serial Interface Controller + @{ + */ + +typedef struct +{ + + + /** + * @var UART_T::DATA + * Offset: 0x00 UART Data Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |DATA |Data Register + * | | |By writing to this register, the UART will send out an 8-bit data through the UART_TXD pin (LSB first). + * | | |By reading this register, the UART will return an 8-bit data received from UART_RXD pin (LSB first). + * @var UART_T::THR + * Offset: 0x00 UART Transmit Holding Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] | THR |Transmit Holding Register + * | | |By writing to this register, the UART will send out an 8-bit data through the UART_TXD pin (LSB first). + * @var UART_T::RBR + * Offset: 0x00 UART Receive Buffer Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |RBR |Receive Buffer Register (Read Only) + * | | |By reading this register, the UART will return the 8-bit data received from UART_RXD pin (LSB first). + * @var UART_T::IER + * Offset: 0x04 UART Interrupt Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RDA_IEN |Receive Data Available Interrupt Enable Bit + * | | |0 = Receive data available interrupt Disabled. + * | | |1 = Receive data available interrupt Enabled. + * |[1] |THRE_IEN |Transmit Holding Register Empty Interrupt Enable Bit + * | | |0 = Transmit holding register empty interrupt Disabled. + * | | |1 = Transmit holding register empt interrupt Enabled. + * |[2] |RLS_IEN |Receive Line Status Interrupt Enable Bit + * | | |0 = Receive Line Status interrupt Disabled. + * | | |1 = Receive Line Status interrupt Enabled. + * |[3] |MODEM_IEN |Modem Status Interrupt Enable Bit + * | | |0 = Modem status interrupt Disabled. + * | | |1 = Modem status interrupt Enabled. + * |[4] |RTO_IEN |RX Time-out Interrupt Enable Bit + * | | |0 = RX time-out interrupt Disabled. + * | | |1 = RX time-out interrupt Enabled. + * |[5] |BUF_ERR_IEN|Buffer Error Interrupt Enable Bit + * | | |0 = Buffer error interrupt Disabled. + * | | |1 = Buffer error interrupt Enabled. + * |[6] |WAKE_EN |UART Wake-up Function Enable Bit + * | | |0 = UART wake-up function Disabled. + * | | |1 = UART wake-up function Enabled, when chip is in Power-down mode, an external nCTS change will wake up chip from Power-down mode. + * |[11] |TIME_OUT_EN|Receive Buffer Time-out Counter Enable Bit + * | | |0 = Receive Buffer Time-out counter Disabled. + * | | |1 = Receive Buffer Time-out counter Enabled. + * |[12] |AUTO_RTS_EN|nRTS Auto Flow Control Enable Bit + * | | |0 = nRTS auto flow control Disabled. + * | | |1 = nRTS auto flow control Enabled. + * | | |Note: When nRTS auto-flow is enabled, if the number of bytes in the RX FIFO is equal to the RTS_TRI_LEV (UA_FCR [19:16]), the UART will de-assert nRTS signal. + * |[13] |AUTO_CTS_EN|nCTS Auto Flow Control Enable Bit + * | | |0 = nCTS auto flow control Disabled. + * | | |1 = nCTS auto flow control Enabled. + * | | |Note: When nCTS auto-flow is enabled, the UART will send data to external device when nCTS input assert (UART will not send data to device until nCTS is asserted). + * |[14] |DMA_TX_EN |TX DMA Enable Bit + * | | |This bit can enable or disable TX DMA service. + * | | |0 = TX DMA Disabled. + * | | |1 = TX DMA Enabled. + * | | |Note: If RLS_IEN (UA_IER[2]) is enabled and HW_RLS_INT(UA_ISR[26]) is set to 1, the RLS (Receive Line Status) Interrupt is caused. + * | | |If RLS interrupt is caused by Break Error Flag BIF(UA_FSR[6]), Frame Error Flag FEF(UA_FSR[5]) or Parity Error Flag PEF(UA_FSR[4]), UART PDMA transmit request operation is stop. + * | | |Clear Break Error Flag BIF or Frame Error Flag FEF or Parity Error Flag PEF by writing "1" to corresponding BIF, FEF and PEF to make UART PDMA transmit request operation continue. + * |[15] |DMA_RX_EN |RX DMA Enable Bit + * | | |This bit can enable or disable RX DMA service. + * | | |0 = RX DMA Disabled. + * | | |1 = RX DMA Enabled. + * | | |Note: If RLS_IEN (UA_IER[2]) is enabled and HW_RLS_INT(UA_ISR[26]) is set to 1, the RLS (Receive Line Status) Interrupt is caused. + * | | |If RLS interrupt is caused by Break Error Flag BIF(UA_FSR[6]), Frame Error Flag FEF(UA_FSR[5]) or Parity Error Flag PEF(UA_FSR[4]), UART PDMA receive request operation is stop. + * | | |Clear Break Error Flag BIF or Frame Error Flag FEF or Parity Error Flag PEF by writing "1" to corresponding BIF, FEF and PEF to make UART PDMA receive request operation continue. + * @var UART_T::FCR + * Offset: 0x08 UART FIFO Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1] |RFR |RX Field Software Reset + * | | |When RFR is set, all the byte in the receiver FIFO and RX internal state machine are cleared. + * | | |0 = No effect. + * | | |1 = Reset the RX internal state machine and pointers. + * | | |Note: This bit will automatically clear at least 3 UART peripheral clock cycles. + * |[2] |TFR |TX Field Software Reset + * | | |When TFR is set, all the byte in the transmit FIFO and TX internal state machine are cleared. + * | | |0 = No effect. + * | | |1 = Reset the TX internal state machine and pointers. + * | | |Note: This bit will automatically clear at least 3 UART peripheral clock cycles. + * |[7:4] |RFITL |RX FIFO Interrupt Trigger Level + * | | |When the number of bytes in the received FIFO is equal to the RFITL, the RDA_IF (UA_ISR[0]) will be set (if RDA_IEN(UA_IER [0]) enabled, an interrupt will be generated). + * | | |0000 = RX FIFO Interrupt Trigger Level is 1 byte. + * | | |0001 = RX FIFO Interrupt Trigger Level is 4 bytes. + * | | |0010 = RX FIFO Interrupt Trigger Level is 8 bytes. + * | | |0011 = RX FIFO Interrupt Trigger Level is 14 bytes. + * |[8] |RX_DIS |Receiver Disable + * | | |The receiver is disabled or not. + * | | |0 = Receiver Enabled. + * | | |1 = Receiver Disabled. + * | | |Note: This field is used for RS-485 Normal Multi-drop mode. It should be programmed before RS485_NMM (UA_ALT_CSR [8]) is programmed. + * |[19:16] |RTS_TRI_LEV|nRTS Trigger Level For Auto-flow Control Use + * | | |0000 = nRTS Trigger Level is 1 byte. + * | | |0001 = nRTS Trigger Level is 4 bytes. + * | | |0010 = nRTS Trigger Level is 8 bytes. + * | | |0011 = nRTS Trigger Level is 14 bytes. + * | | |Note: This field is used for nRTS auto-flow control. + * @var UART_T::LCR + * Offset: 0x0C UART Line Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1:0] |WLS |Word Length Selection + * | | |00 = Word length is 5-bit. + * | | |01 = Word length is 6-bit. + * | | |10 = Word length is 7-bit + * | | |11 = Word length is 8-bit + * |[2] |NSB |Number Of "STOP Bit" + * | | |0 = One " STOP bit" is generated in the transmitted data. + * | | |1 = When select 5-bit word length, 1.5 "STOP bit" is generated in the transmitted data. + * | | |When select 6-,7- and 8-bit word length, 2 "STOP bit" is generated in the transmitted data. + * |[3] |PBE |Parity Bit Enable Bit + * | | |0 = No parity bit. + * | | |1 = Parity bit is generated on each outgoing character and is checked on each incoming data. + * |[4] |EPE |Even Parity Enable Bit + * | | |0 = Odd number of logic 1's is transmitted and checked in each word. + * | | |1 = Even number of logic 1's is transmitted and checked in each word. + * | | |This bit has effect only when PBE (UA_LCR[3]) is set. + * |[5] |SPE |Stick Parity Enable Bit + * | | |0 = Stick parity Disabled. + * | | |1 = If PBE (UA_LCR[3]) and EBE (UA_LCR[4]) are logic 1, the parity bit is transmitted and checked as logic 0. + * | | |If PBE (UA_LCR[3]) is 1 and EBE (UA_LCR[4]) is 0 then the parity bit is transmitted and checked as 1. + * |[6] |BCB |Break Control Bit + * | | |When this bit is set to logic 1, the serial data output (TX) is forced to the Spacing State (logic 0). + * | | |This bit acts only on TX and has no effect on the transmitter logic. + * @var UART_T::MCR + * Offset: 0x10 UART Modem Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1] |RTS |nRTS (Request-To-Send) Signal Control + * | | |This bit is direct control internal nRTS signal active or not, and then drive the nRTS pin output with LEV_RTS bit configuration. + * | | |0 = nRTS signal is active. + * | | |1 = nRTS signal is inactive. + * | | |Note1: This nRTS signal control bit is not effective when RTS auto-flow control is enabled in UART function mode. + * | | |Note2: This nRTS signal control bit is not effective when RS-485 auto direction mode (AUD) is enabled in RS-485 function mode. + * |[9] |LEV_RTS |nRTS Pin Active Level + * | | |This bit defines the active level state of nRTS pin output. + * | | |0 = nRTS pin output is high level active. + * | | |1 = nRTS pin output is low level active. + * |[13] |RTS_ST |nRTS Pin State (Read Only) + * | | |This bit mirror from nRTS pin output of voltage logic status. + * | | |0 = nRTS pin output is low level voltage logic state. + * | | |1 = nRTS pin output is high level voltage logic state. + * @var UART_T::MSR + * Offset: 0x14 UART Modem Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |DCTSF |Detect nCTS State Change Flag + * | | |This bit is set whenever nCTS input has change state, and it will generate Modem interrupt to CPU when MODEM_IEN (UA_IER [3]) is set to 1. + * | | |0 = nCTS input has not change state. + * | | |1 = nCTS input has change state. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[4] |CTS_ST |nCTS Pin Status (Read Only) + * | | |This bit mirror from CTS pin input of voltage logic status. + * | | |0 = nCTS pin input is low level voltage logic state. + * | | |1 = nCTS pin input is high level voltage logic state. + * | | |Note: This bit echoes when UART Controller peripheral clock is enabled, and nCTS multi-function port is selected. + * |[8] |LEV_CTS |nCTS Pin Active Level + * | | |This bit defines the active level state of nCTS pin input. + * | | |0 = nCTS pin input is high level active. + * | | |1 = nCTS pin input is low level active. + * @var UART_T::FSR + * Offset: 0x18 UART FIFO Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RX_OVER_IF|RX Overflow Error Interrupt Flag + * | | |This bit is set when RX FIFO overflow. + * | | |If the number of bytes of received data is greater than RX_FIFO (UA_RBR) size, this bit will be set. + * | | |0 = RX FIFO is not overflow. + * | | |1 = RX FIFO is overflow. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[3] |RS485_ADD_DETF|RS-485 Address Byte Detection Flag + * | | |0 = Receiver detects a data that is not an address bit (bit 9 ='1'). + * | | |1 = Receiver detects a data that is an address bit (bit 9 ='1'). + * | | |Note1: This field is used for RS-485 function mode and RS485_ADD_EN (UA_ALT_CSR[15]) is set to 1 to enable Address detection mode . + * | | |Note2: This bit can be cleared by writing "1" to it. + * |[4] |PEF |Parity Error Flag + * | | |This bit is set to logic 1 whenever the received character does not have a valid "parity bit". + * | | |0 = No parity error is generated. + * | | |1 = Parity error is generated. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[5] |FEF |Framing Error Flag + * | | |This bit is set to logic 1 whenever the received character does not have a valid "stop bit" (that is, the stop bit following the last data bit or parity bit is detected as logic 0). + * | | |0 = No framing error is generated. + * | | |1 = Framing error is generated. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[6] |BIF |Break Interrupt Flag + * | | |This bit is set to logic 1 whenever the received data input(RX) is held in the "spacing state" (logic 0) for longer than a full word transmission time (that is, the total time of "start bit" + data bits + parity + stop bits). + * | | |0 = No Break interrupt is generated. + * | | |1 = Break interrupt is generated. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[13:8] |RX_POINTER|RX FIFO Pointer (Read Only) + * | | |This field indicates the RX FIFO Buffer Pointer. + * | | |When UART receives one byte from external device, RX_POINTER increases one. + * | | |When one byte of RX FIFO is read by CPU, RX_POINTER decreases one. + * | | |The Maximum value shown in RX_POINTER is 15. + * | | |When the using level of RX FIFO Buffer equal to 16, the RX_FULL bit is set to 1 and RX_POINTER will show 0. + * | | |As one byte of RX FIFO is read by CPU, the RX_FULL bit is cleared to 0 and RX_POINTER will show 15. + * |[14] |RX_EMPTY |Receiver FIFO Empty (Read Only) + * | | |This bit initiate RX FIFO empty or not. + * | | |0 = RX FIFO is not empty. + * | | |1 = RX FIFO is empty. + * | | |Note: When the last byte of RX FIFO has been read by CPU, hardware sets this bit high. It will be cleared when UART receives any new data. + * |[15] |RX_FULL |Receiver FIFO Full (Read Only) + * | | |This bit initiates RX FIFO is full or not. + * | | |0 = RX FIFO is not full. + * | | |1 = RX FIFO is full. + * | | |Note: This bit is set when the number of usage in RX FIFO Buffer is equal to 16, otherwise is cleared by hardware. + * |[21:16] |TX_POINTER|TX FIFO Pointer (Read Only) + * | | |This field indicates the TX FIFO Buffer Pointer. + * | | |When CPU writes one byte into UA_THR, TX_POINTER increases one. + * | | |When one byte of TX FIFO is transferred to Transmitter Shift Register, TX_POINTER decreases one. + * | | |The Maximum value shown in TX_POINTER is 15. + * | | |When the using level of TX FIFO Buffer equal to 16, the TX_FULL bit is set to 1 and TX_POINTER will show 0. + * | | |As one byte of TX FIFO is transferred to Transmitter Shift Register, the TX_FULL bit is cleared to 0 and TX_POINTER will show 15. + * |[22] |TX_EMPTY |Transmitter FIFO Empty (Read Only) + * | | |This bit indicates TX FIFO empty or not. + * | | |0 = TX FIFO is not empty. + * | | |1 = TX FIFO is empty. + * | | |Note: When the last byte of TX FIFO has been transferred to Transmitter Shift Register, hardware sets this bit high. + * | | |It will be cleared when writing data into THR (TX FIFO not empty). + * |[23] |TX_FULL |Transmitter FIFO Full (Read Only) + * | | |This bit indicates TX FIFO full or not. + * | | |0 = TX FIFO is not full. + * | | |1 = TX FIFO is full. + * | | |Note: This bit is set when the using level of TX FIFO Buffer equal to 16; otherwise, it is cleared by hardware. + * |[24] |TX_OVER_IF|TX Overflow Error Interrupt Flag + * | | |If TX FIFO (UA_THR) is full, an additional write to UA_THR will cause this bit to logic 1. + * | | |0 = TX FIFO is not overflow. + * | | |1 = TX FIFO is overflow. + * | | |Note: This bit can be cleared by writing "1" to it. + * |[28] |TE_FLAG |Transmitter Empty Flag (Read Only) + * | | |This bit is set by hardware when TX FIFO (UA_THR) is empty and the STOP bit of the last byte has been transmitted. + * | | |0 = TX FIFO is not empty or the STOP bit of the last byte has not been transmitted. + * | | |1 = TX FIFO is empty and the STOP bit of the last byte has been transmitted. + * | | |Note: This bit is cleared automatically when TX FIFO is not empty or the last byte transmission has not completed. + * @var UART_T::ISR + * Offset: 0x1C UART Interrupt Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |RDA_IF |Receive Data Available Interrupt Flag (Read Only) + * | | |When the number of bytes in the RX FIFO equals the RFITL (UA_FCR[7:4]) then the RDA_IF (UA_ISR[0]) will be set. + * | | |If RDA_IEN (UA_IER [0]) is enabled, the RDA interrupt will be generated. + * | | |0 = No RDA interrupt flag is generated. + * | | |1 = RDA interrupt flag is generated. + * | | |Note: This bit is read only and it will be cleared when the number of unread bytes of RX FIFO drops below the threshold level RFITL(UA_FCR[7:4]). + * |[1] |THRE_IF |Transmit Holding Register Empty Interrupt Flag (Read Only) + * | | |This bit is set when the last data of TX FIFO is transferred to Transmitter Shift Register. + * | | |If THRE_IEN (UA_IER[1]) is enabled, the THRE interrupt will be generated. + * | | |0 = No THRE interrupt flag is generated. + * | | |1 = THRE interrupt flag is generated. + * | | |Note: This bit is read only and it will be cleared when writing data into THR (TX FIFO not empty). + * |[2] |RLS_IF |Receive Line Interrupt Flag (Read Only) + * | | |This bit is set when the RX receive data have parity error, frame error or break error (at least one of 3 bits, BIF(UA_FSR[6]), FEF(UA_FSR[5]) and PEF(UA_FSR[4]), is set). + * | | |If RLS_IEN (UA_IER[2]) is enabled, the RLS interrupt will be generated. + * | | |0 = No RLS interrupt flag is generated. + * | | |1 = RLS interrupt flag is generated. + * | | |Note1: In RS-485 function mode, this field is set include receiver detect and received address byte character (bit9 = '1') bit. At the same time, the bit of RS485_ADD_DETF (UA_FSR[3]) is also set. + * | | |Note2: This bit is read only and reset to 0 when all bits of BIF (UA_FSR[6]), FEF (UA_FSR[5]) and PEF (UA_FSR[4]) are cleared. + * | | |Note3: In RS-485 function mode, this bit is read only and reset to 0 when all bits of BIF (UA_FSR[6]) , FEF (UA_FSR[5]) and PEF (UA_FSR[4]) and RS485_ADD_DETF (UA_FSR[3]) are cleared. + * |[3] |MODEM_IF |MODEM Interrupt Flag (Read Only) + * | | |This bit is set when the nCTS pin has state change (DCTSF (UA_MSR[0]) = 1). + * | | |If MODEM_IEN (UA_IER[3]) is enabled, the Modem interrupt will be generated. + * | | |0 = No Modem interrupt flag is generated. + * | | |1 = Modem interrupt flag is generated. + * | | |Note: This bit is read only and reset to 0 when bit DCTSF is cleared by a write 1 on DCTSF(UA_MSR[0]). + * |[4] |TOUT_IF |Receive Buffer Time-out Interrupt Flag (Read Only) + * | | |This bit is set when the RX FIFO is not empty and no activities occurred in the RX FIFO and the time-out counter equal to TOIC (UA_TOR[7:0]). + * | | |If TOUT_IEN (UA_IER[4]) is enabled, the Tout interrupt will be generated. + * | | |0 = No Receive Buffer Time-out interrupt flag is generated. + * | | |1 = Receive Buffer Time-out interrupt flag is generated. + * | | |Note: This bit is read only and user can read UA_RBR (RX is in active) to clear it. + * |[5] |BUF_ERR_IF|Buffer Error Interrupt Flag (Read Only) + * | | |This bit is set when the TX FIFO or RX FIFO overflows (TX_OVER_IF (UA_FSR[24]) or RX_OVER_IF (UA_FSR[0]) is set). + * | | |When BUF_ERR_IF (UA_ISR[5])is set, the transfer is not correct. + * | | |If BUF_ERR_IEN (UA_IER[8]) is enabled, the buffer error interrupt will be generated. + * | | |0 = No buffer error interrupt flag is generated. + * | | |1 = Buffer error interrupt flag is generated. + * | | |Note: Note: This bit is cleared if both of RX_OVER_IF (UA_FSR[0]) and TX_OVER_IF (UA_FSR[24]) are cleared to 0 by writing 1 to RX_OVER_IF (UA_FSR[0]) and TX_OVER_IF (UA_FSR[24]). + * |[8] |RDA_INT |Receive Data Available Interrupt Indicator (Read Only) + * | | |This bit is set if RDA_IEN (UA_IER[0]) and RDA_IF (UA_ISR[0]) are both set to 1. + * | | |0 = No RDA interrupt is generated. + * | | |1 = RDA interrupt is generated. + * |[9] |THRE_INT |Transmit Holding Register Empty Interrupt Indicator (Read Only) + * | | |This bit is set if THRE_IEN (UA_IER[1]) and THRE_IF (UA_SR[1]) are both set to 1. + * | | |0 = No THRE interrupt is generated. + * | | |1 = THRE interrupt is generated. + * |[10] |RLS_INT |Receive Line Status Interrupt Indicator (Read Only) + * | | |This bit is set if RLS_IEN (UA_IER[2]) and RLS_IF (UA_ISR[2]) are both set to 1. + * | | |0 = No RLS interrupt is generated. + * | | |1 = RLS interrupt is generated + * |[11] |MODEM_INT |MODEM Status Interrupt Indicator (Read Only) + * | | |This bit is set if MODEM_IEN (UA_IER[3]) and MODEM_IF (UA_ISR[4]) are both set to 1 + * | | |0 = No Modem interrupt is generated. + * | | |1 = Modem interrupt is generated. + * |[12] |TOUT_INT |Receive Buffer Time-out Interrupt Indicator (Read Only) + * | | |This bit is set if TOUT_IEN (UA_IER[4]) and TOUT_IF (UA_ISR[4]) are both set to 1. + * | | |0 = No Receive Buffer Time-out interrupt is generated. + * | | |1 = Receive Buffer Time-out interrupt is generated. + * |[13] |BUF_ERR_INT|Buffer Error Interrupt Indicator (Read Only) + * | | |This bit is set if BUF_ERR_IEN (UA_IER[5]) and BUF_ERR_IF (UA_ISR[5]) are both set to 1. + * | | |0 = No buffer error interrupt is generated . + * | | |1 = Buffer error interrupt is generated. + * |[18] |HW_RLS_IF |In DMA Mode, Receive Line Status Flag (Read Only) + * | | |This bit is set when the RX receive data have parity error, frame error or break error (at least one of 3 bits, BIF (UA_FSR[6]), FEF (UA_FSR[5]) and PEF (UA_FSR[4]) is set). + * | | |If RLS_IEN (UA_IER[2]) is enabled, the RLS interrupt will be generated. + * | | |0 = No RLS interrupt flag is generated in DMA mode. + * | | |1 = RLS interrupt flag is generated in DMA mode. + * | | |Note1: In RS-485 function mode, this field include receiver detect any address byte received address byte character (bit9 = '1') bit. + * | | |Note2: In UART function mode, this bit is read only and reset to 0 when all bits of BIF (UA_FSR[6]) , FEF (UA_FSR[5]) and PEF (UA_FSR[4]) are cleared. + * | | |Note3: In RS-485 function mode, this bit is read only and reset to 0 when all bits of BIF (UA_FSR[6]) , FEF (UA_FSR[5]) and PEF (UA_FSR[4]) and RS485_ADD_DETF (UA_FSR[3]) are cleared. + * |[19] |HW_MODEM_IF|In DMA Mode, MODEM Interrupt Flag (Read Only) + * | | |This bit is set when the CTS pin has state change (DCTSF (US_MSR[0] = 1)). + * | | |If MODEM_IEN (UA_IER[3]) is enabled, the Modem interrupt will be generated. + * | | |0 = No Modem interrupt flag is generated in DMA mode. + * | | |1 = Modem interrupt flag is generated in DMA mode. + * | | |Note: This bit is read only and reset to 0 when the bit DCTSF (US_MSR[0]) is cleared by writing 1 on DCTSF (US_MSR[0]). + * |[20] |HW_TOUT_IF|In DMA Mode, Receive Buffer Time-out Interrupt Flag (Read Only) + * | | |This bit is set when the RX FIFO is not empty and no activities occurred in the RX FIFO and the time-out counter equal to TOIC (UA_TOR[7:0]). + * | | |If TOUT_IEN (UA_IER[4]) is enabled, the Tout interrupt will be generated. + * | | |0 = No Receive Buffer Time-out interrupt flag is generated in DMA mode. + * | | |1 = Receive Buffer Time-out interrupt flag is generated in DMA mode. + * | | |Note: This bit is read only and user can read UA_RBR (RX is in active) to clear it. + * |[21] |HW_BUF_ERR_IF|In DMA Mode, Buffer Error Interrupt Flag (Read Only) + * | | |This bit is set when the TX or RX FIFO overflows (TX_OVER_IF (UA__FSR[24]) or RX_OVER_IF (UA_FSR[0]) is set). + * | | |When BUF_ERR_IF (UA_ISR[5]) is set, the transfer maybe is not correct. + * | | |If BUF_ERR_IEN (UA_IER[5]) is enabled, the buffer error interrupt will be generated. + * | | |0 = No buffer error interrupt flag is generated in DMA mode. + * | | |1 = Buffer error interrupt flag is generated in DMA mode. + * | | |Note: This bit is cleared when both TX_OVER_IF (UA_FSR[24]) and RX_OVER_IF (UA_FSR[0]) are cleared. + * |[26] |HW_RLS_INT|In DMA Mode, Receive Line Status Interrupt Indicator (Read Only) + * | | |This bit is set if RLS_IEN (UA_IER[2]) and HW_RLS_IF (UA_ISR[18]) are both set to 1. + * | | |0 = No RLS interrupt is generated in DMA mode. + * | | |1 = RLS interrupt is generated in DMA mode. + * |[27] |HW_MODEM_INT|In DMA Mode, MODEM Status Interrupt Indicator (Read Only) + * | | |This bit is set if MODEM_IEN (UA_IER[3]) and HW_MODEM_IF (UA_ISR[19]) are both set to 1. + * | | |0 = No Modem interrupt is generated in DMA mode. + * | | |1 = Modem interrupt is generated in DMA mode. + * |[28] |HW_TOUT_INT|In DMA Mode, Receive Buffer Time-out Interrupt Indicator (Read Only) + * | | |This bit is set if TOUT_IEN (UA_IER[4])and HW_TOUT_IF (UA_ISR[20]) are both set to 1. + * | | |0 = No Receive Buffer Time-out interrupt is generated in DMA mode. + * | | |1 = Receive Buffer Time-out interrupt is generated in DMA mode. + * |[29] |HW_BUF_ERR_INT|In DMA Mode, Buffer Error Interrupt Indicator (Read Only) + * | | |This bit is set if BUF_ERR_IEN (UA_IER[5]) and HW_BUF_ERR_IF (UA_ISR[21] )are both set to 1. + * | | |0 = No buffer error interrupt is generated in DMA mode. + * | | |1 = Buffer error interrupt is generated in DMA mode. + * @var UART_T::TOR + * Offset: 0x20 UART Time-out Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7:0] |TOIC |Time-out Interrupt Comparator + * | | |The time-out counter resets and starts counting (counting clock = baud rate) whenever the RX FIFO receives a new data word if time-out counter is enabled by setting TIME_OUT_EN(UA_IER [11]). + * | | |Once the content of time-out counter is equal to that of time-out interrupt comparator (TOIC (UA_TOR[7:0])), a receiver time-out interrupt (TOUT_INT (UA_ISR[12])) is generated if RTO_IEN (UA_IER[4]). + * | | |A new incoming data word or RX FIFO empty clears TOUT_IF (UA_ISR[4]). + * | | |In order to avoid receiver time-out interrupt generation immediately during one character is being received, TOIC value should be set between 40 and 255. + * | | |Thus, for example, if TOIC is set as 40, the time-out interrupt is generated after four characters are not received when 1 stop bit and no parity check is set for UART transfer. + * |[15:8] |DLY |TX Delay Time Value + * | | |This field is used to programming the transfer delay time between the last stop bit and next start bit. + * @var UART_T::BAUD + * Offset: 0x24 UART Baud Rate Divisor Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[15:0] |BRD |Baud Rate Divider + * | | |The field indicates the baud rate divider. + * |[27:24] |DIVIDER_X |Divider X + * | | |The baud rate divider M = X+1. + * |[28] |DIV_X_ONE |Divider X Equal to 1 + * | | |0 = Divider M is X +1. + * | | |1 = Divider M is 1. + * |[29] |DIV_X_EN |Divider X Enable + * | | |The BRD is Baud Rate Divider, and the baud rate equation is Baud Rate = Clock / [M * (BRD + 2)]; The default value of M is 16. + * | | |0 = Divider X Disabled (the equation of M = 16). + * | | |1 = Divider X Enabled (the equation of M = X+1, but DIVIDER_X [27:24] must >= 8). + * | | |Note: In IrDA mode, this bit must disable. + * @var UART_T::IRCR + * Offset: 0x28 UART IrDA Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1] |TX_SELECT |IrDA Receiver/Transmitter Selection Enable Bit + * | | |0 = IrDA Transmitter Disabled and Receiver Enabled. + * | | |1 = IrDA Transmitter Enabled and Receiver Disabled. + * |[5] |INV_TX |IrDA inverse Transmitting Output Signal Control + * | | |0 = None inverse transmitting signal. + * | | |1 = Inverse transmitting output signal. + * |[6] |INV_RX |IrDA inverse Receive Input Signal Control + * | | |0 = None inverse receiving input signal. + * | | |1 = Inverse receiving input signal. + * @var UART_T::ALT_CSR + * Offset: 0x2C UART Alternate Control/Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8] |RS485_NMM |RS-485 Normal Multi-Drop Operation Mode (NMM) + * | | |0 = RS-485 Normal Multi-drop Operation mode (NMM) Disabled. + * | | |1 = RS-485 Normal Multi-drop Operation mode (NMM) Enabled. + * | | |Note: It cannot be active with RS-485_AAD operation mode. + * |[9] |RS485_AAD |RS-485 Auto Address Detection Operation Mode (AAD) + * | | |0 = RS-485 Auto Address Detection Operation mode (AAD) Disabled. + * | | |1 = RS-485 Auto Address Detection Operation mode (AAD) Enabled. + * | | |Note: It cannot be active with RS-485_NMM operation mode. + * |[10] |RS485_AUD |RS-485 Auto Direction Mode (AUD) + * | | |0 = RS-485 Auto Direction Operation mode (AUO) Disabled. + * | | |1 = RS-485 Auto Direction Operation mode (AUO) Enabled. + * | | |Note: It can be active with RS-485_AAD or RS-485_NMM operation mode. + * |[15] |RS485_ADD_EN|RS-485 Address Detection Enable Bit + * | | |This bit is used to enable RS-485 Address Detection mode. + * | | |0 = Address detection mode Disabled. + * | | |1 = Address detection mode Enabled. + * | | |Note: This bit is used for RS-485 any operation mode. + * |[31:24] |ADDR_MATCH|Address Match Value + * | | |This field contains the RS-485 address match values. + * | | |Note: This field is used for RS-485 auto address detection mode. + * @var UART_T::FUN_SEL + * Offset: 0x30 UART Function Select Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1:0] |FUN_SEL |Function Select Enable + * | | |00 = UART function Enabled. + * | | |10 = IrDA function Enabled. + * | | |11 = RS-485 function Enabled. + */ + + union { + __IO uint32_t DATA; /* Offset: 0x00 UART Data Register */ + __IO uint32_t THR; /* Offset: 0x00 UART Transmit Holding Register */ + __IO uint32_t RBR; /* Offset: 0x00 UART Receive Buffer Register */ + }; + __IO uint32_t IER; /* Offset: 0x04 UART Interrupt Enable Register */ + __IO uint32_t FCR; /* Offset: 0x08 UART FIFO Control Register */ + __IO uint32_t LCR; /* Offset: 0x0C UART Line Control Register */ + __IO uint32_t MCR; /* Offset: 0x10 UART Modem Control Register */ + __IO uint32_t MSR; /* Offset: 0x14 UART Modem Status Register */ + __IO uint32_t FSR; /* Offset: 0x18 UART FIFO Status Register */ + __IO uint32_t ISR; /* Offset: 0x1C UART Interrupt Status Register */ + __IO uint32_t TOR; /* Offset: 0x20 UART Time-out Register */ + __IO uint32_t BAUD; /* Offset: 0x24 UART Baud Rate Divisor Register */ + __IO uint32_t IRCR; /* Offset: 0x28 UART IrDA Control Register */ + __IO uint32_t ALT_CSR; /* Offset: 0x2C UART Alternate Control/Status Register */ + __IO uint32_t FUN_SEL; /* Offset: 0x30 UART Function Select Register */ + +} UART_T; + + + + +/** @addtogroup REG_UART_BITMASK UART Bit Mask + @{ + */ + +/* UART THR Bit Field Definitions */ +#define UART_THR_THR_Pos 0 /*!< UART_T::THR: THR Position */ +#define UART_THR_THR_Msk (0xFul << UART_THR_THR_Pos) /*!< UART_T::THR: THR Mask */ + +/* UART RBR Bit Field Definitions */ +#define UART_RBR_RBR_Pos 0 /*!< UART_T::RBR: RBR Position */ +#define UART_RBR_RBR_Msk (0xFul << UART_RBR_RBR_Pos) /*!< UART_T::RBR: RBR Mask */ + +/* UART IER Bit Field Definitions */ +#define UART_IER_DMA_RX_EN_Pos 15 /*!< UART_T::IER: RX DMA Enable Position */ +#define UART_IER_DMA_RX_EN_Msk (1ul << UART_IER_DMA_RX_EN_Pos) /*!< UART_T::IER: RX DMA Enable Mask */ + +#define UART_IER_DMA_TX_EN_Pos 14 /*!< UART_T::IER: TX DMA Enable Position */ +#define UART_IER_DMA_TX_EN_Msk (1ul << UART_IER_DMA_TX_EN_Pos) /*!< UART_T::IER: TX DMA Enable Mask */ + +#define UART_IER_AUTO_CTS_EN_Pos 13 /*!< UART_T::IER: AUTO_CTS_EN Position */ +#define UART_IER_AUTO_CTS_EN_Msk (1ul << UART_IER_AUTO_CTS_EN_Pos) /*!< UART_T::IER: AUTO_CTS_EN Mask */ + +#define UART_IER_AUTO_RTS_EN_Pos 12 /*!< UART_T::IER: AUTO_RTS_EN Position */ +#define UART_IER_AUTO_RTS_EN_Msk (1ul << UART_IER_AUTO_RTS_EN_Pos) /*!< UART_T::IER: AUTO_RTS_EN Mask */ + +#define UART_IER_TIME_OUT_EN_Pos 11 /*!< UART_T::IER: TIME_OUT_EN Position */ +#define UART_IER_TIME_OUT_EN_Msk (1ul << UART_IER_TIME_OUT_EN_Pos) /*!< UART_T::IER: TIME_OUT_EN Mask */ + +#define UART_IER_WAKE_EN_Pos 6 /*!< UART_T::IER: WAKE_EN Position */ +#define UART_IER_WAKE_EN_Msk (1ul << UART_IER_WAKE_EN_Pos) /*!< UART_T::IER: WAKE_EN Mask */ + +#define UART_IER_BUF_ERR_IEN_Pos 5 /*!< UART_T::IER: BUF_ERR_IEN Position */ +#define UART_IER_BUF_ERR_IEN_Msk (1ul << UART_IER_BUF_ERR_IEN_Pos) /*!< UART_T::IER: BUF_ERR_IEN Mask */ + +#define UART_IER_RTO_IEN_Pos 4 /*!< UART_T::IER: RTO_IEN Position */ +#define UART_IER_RTO_IEN_Msk (1ul << UART_IER_RTO_IEN_Pos) /*!< UART_T::IER: RTO_IEN Mask */ + +#define UART_IER_MODEM_IEN_Pos 3 /*!< UART_T::IER: MODEM_IEN Position */ +#define UART_IER_MODEM_IEN_Msk (1ul << UART_IER_MODEM_IEN_Pos) /*!< UART_T::IER: MODEM_IEN Mask */ + +#define UART_IER_RLS_IEN_Pos 2 /*!< UART_T::IER: RLS_IEN Position */ +#define UART_IER_RLS_IEN_Msk (1ul << UART_IER_RLS_IEN_Pos) /*!< UART_T::IER: RLS_IEN Mask */ + +#define UART_IER_THRE_IEN_Pos 1 /*!< UART_T::IER: THRE_IEN Position */ +#define UART_IER_THRE_IEN_Msk (1ul << UART_IER_THRE_IEN_Pos) /*!< UART_T::IER: THRE_IEN Mask */ + +#define UART_IER_RDA_IEN_Pos 0 /*!< UART_T::IER: RDA_IEN Position */ +#define UART_IER_RDA_IEN_Msk (1ul << UART_IER_RDA_IEN_Pos) /*!< UART_T::IER: RDA_IEN Mask */ + +/* UART FCR Bit Field Definitions */ +#define UART_FCR_RTS_TRI_LEV_Pos 16 /*!< UART_T::FCR: RTS_TRI_LEV Position */ +#define UART_FCR_RTS_TRI_LEV_Msk (0xFul << UART_FCR_RTS_TRI_LEV_Pos) /*!< UART_T::FCR: RTS_TRI_LEV Mask */ + +#define UART_FCR_RX_DIS_Pos 8 /*!< UART_T::FCR: RX_DIS Position */ +#define UART_FCR_RX_DIS_Msk (1ul << UART_FCR_RX_DIS_Pos) /*!< UART_T::FCR: RX_DIS Mask */ + +#define UART_FCR_RFITL_Pos 4 /*!< UART_T::FCR: RFITL Position */ +#define UART_FCR_RFITL_Msk (0xFul << UART_FCR_RFITL_Pos) /*!< UART_T::FCR: RFITL Mask */ + +#define UART_FCR_TFR_Pos 2 /*!< UART_T::FCR: TFR Position */ +#define UART_FCR_TFR_Msk (1ul << UART_FCR_TFR_Pos) /*!< UART_T::FCR: TFR Mask */ + +#define UART_FCR_RFR_Pos 1 /*!< UART_T::FCR: RFR Position */ +#define UART_FCR_RFR_Msk (1ul << UART_FCR_RFR_Pos) /*!< UART_T::FCR: RFR Mask */ + +/* UART LCR Bit Field Definitions */ +#define UART_LCR_BCB_Pos 6 /*!< UART_T::LCR: BCB Position */ +#define UART_LCR_BCB_Msk (1ul << UART_LCR_BCB_Pos) /*!< UART_T::LCR: BCB Mask */ + +#define UART_LCR_SPE_Pos 5 /*!< UART_T::LCR: SPE Position */ +#define UART_LCR_SPE_Msk (1ul << UART_LCR_SPE_Pos) /*!< UART_T::LCR: SPE Mask */ + +#define UART_LCR_EPE_Pos 4 /*!< UART_T::LCR: EPE Position */ +#define UART_LCR_EPE_Msk (1ul << UART_LCR_EPE_Pos) /*!< UART_T::LCR: EPE Mask */ + +#define UART_LCR_PBE_Pos 3 /*!< UART_T::LCR: PBE Position */ +#define UART_LCR_PBE_Msk (1ul << UART_LCR_PBE_Pos) /*!< UART_T::LCR: PBE Mask */ + +#define UART_LCR_NSB_Pos 2 /*!< UART_T::LCR: NSB Position */ +#define UART_LCR_NSB_Msk (1ul << UART_LCR_NSB_Pos) /*!< UART_T::LCR: NSB Mask */ + +#define UART_LCR_WLS_Pos 0 /*!< UART_T::LCR: WLS Position */ +#define UART_LCR_WLS_Msk (0x3ul << UART_LCR_WLS_Pos) /*!< UART_T::LCR: WLS Mask */ + +/* UART MCR Bit Field Definitions */ +#define UART_MCR_RTS_ST_Pos 13 /*!< UART_T::MCR: RTS_ST Position */ +#define UART_MCR_RTS_ST_Msk (1ul << UART_MCR_RTS_ST_Pos) /*!< UART_T::MCR: RTS_ST Mask */ + +#define UART_MCR_LEV_RTS_Pos 9 /*!< UART_T::MCR: LEV_RTS Position */ +#define UART_MCR_LEV_RTS_Msk (1ul << UART_MCR_LEV_RTS_Pos) /*!< UART_T::MCR: LEV_RTS Mask */ + +#define UART_MCR_RTS_Pos 1 /*!< UART_T::MCR: RTS Position */ +#define UART_MCR_RTS_Msk (1ul << UART_MCR_RTS_Pos) /*!< UART_T::MCR: RTS Mask */ + +/* UART MSR Bit Field Definitions */ +#define UART_MSR_LEV_CTS_Pos 8 /*!< UART_T::MSR: LEV_CTS Position */ +#define UART_MSR_LEV_CTS_Msk (1ul << UART_MSR_LEV_CTS_Pos) /*!< UART_T::MSR: LEV_CTS Mask */ + +#define UART_MSR_CTS_ST_Pos 4 /*!< UART_T::MSR: CTS_ST Position */ +#define UART_MSR_CTS_ST_Msk (1ul << UART_MSR_CTS_ST_Pos) /*!< UART_T::MSR: CTS_ST Mask */ + +#define UART_MSR_DCTSF_Pos 0 /*!< UART_T::MSR: DCTST Position */ +#define UART_MSR_DCTSF_Msk (1ul << UART_MSR_DCTSF_Pos) /*!< UART_T::MSR: DCTST Mask */ + + +/* UART FSR Bit Field Definitions */ +#define UART_FSR_TE_FLAG_Pos 28 /*!< UART_T::FSR: TE_FLAG Position */ +#define UART_FSR_TE_FLAG_Msk (1ul << UART_FSR_TE_FLAG_Pos) /*!< UART_T::FSR: TE_FLAG Mask */ + +#define UART_FSR_TX_OVER_IF_Pos 24 /*!< UART_T::FSR: TX_OVER_IF Position */ +#define UART_FSR_TX_OVER_IF_Msk (1ul << UART_FSR_TX_OVER_IF_Pos) /*!< UART_T::FSR: TX_OVER_IF Mask */ + +#define UART_FSR_TX_FULL_Pos 23 /*!< UART_T::FSR: TX_FULL Position */ +#define UART_FSR_TX_FULL_Msk (1ul << UART_FSR_TX_FULL_Pos) /*!< UART_T::FSR: TX_FULL Mask */ + +#define UART_FSR_TX_EMPTY_Pos 22 /*!< UART_T::FSR: TX_EMPTY Position */ +#define UART_FSR_TX_EMPTY_Msk (1ul << UART_FSR_TX_EMPTY_Pos) /*!< UART_T::FSR: TX_EMPTY Mask */ + +#define UART_FSR_TX_POINTER_Pos 16 /*!< UART_T::FSR: TX_POINTER Position */ +#define UART_FSR_TX_POINTER_Msk (0x3Ful << UART_FSR_TX_POINTER_Pos) /*!< UART_T::FSR: TX_POINTER Mask */ + +#define UART_FSR_RX_FULL_Pos 15 /*!< UART_T::FSR: RX_FULL Position */ +#define UART_FSR_RX_FULL_Msk (1ul << UART_FSR_RX_FULL_Pos) /*!< UART_T::FSR: RX_FULL Mask */ + +#define UART_FSR_RX_EMPTY_Pos 14 /*!< UART_T::FSR: RX_EMPTY Position */ +#define UART_FSR_RX_EMPTY_Msk (1ul << UART_FSR_RX_EMPTY_Pos) /*!< UART_T::FSR: RX_EMPTY Mask */ + +#define UART_FSR_RX_POINTER_Pos 8 /*!< UART_T::FSR: RX_POINTERS Position */ +#define UART_FSR_RX_POINTER_Msk (0x3Ful << UART_FSR_RX_POINTER_Pos) /*!< UART_T::FSR: RX_POINTER Mask */ + +#define UART_FSR_BIF_Pos 6 /*!< UART_T::FSR: BIF Position */ +#define UART_FSR_BIF_Msk (1ul << UART_FSR_BIF_Pos) /*!< UART_T::FSR: BIF Mask */ + +#define UART_FSR_FEF_Pos 5 /*!< UART_T::FSR: FEF Position */ +#define UART_FSR_FEF_Msk (1ul << UART_FSR_FEF_Pos) /*!< UART_T::FSR: FEF Mask */ + +#define UART_FSR_PEF_Pos 4 /*!< UART_T::FSR: PEF Position */ +#define UART_FSR_PEF_Msk (1ul << UART_FSR_PEF_Pos) /*!< UART_T::FSR: PEF Mask */ + +#define UART_FSR_RS485_ADD_DETF_Pos 3 /*!< UART_T::FSR: RS485_ADD_DETF Position */ +#define UART_FSR_RS485_ADD_DETF_Msk (1ul << UART_FSR_RS485_ADD_DETF_Pos) /*!< UART_T::FSR: RS485_ADD_DETF Mask */ + +#define UART_FSR_RX_OVER_IF_Pos 0 /*!< UART_T::FSR: RX_OVER_IF Position */ +#define UART_FSR_RX_OVER_IF_Msk (1ul << UART_FSR_RX_OVER_IF_Pos) /*!< UART_T::FSR: RX_OVER_IF Mask */ + +/* UART ISR Bit Field Definitions */ +#define UART_ISR_HW_BUF_ERR_INT_Pos 29 /*!< UART_T::ISR: HW BUF_ERR_INT Position */ +#define UART_ISR_HW_BUF_ERR_INT_Msk (1ul << UART_ISR_HW_BUF_ERR_INT_Pos) /*!< UART_T::ISR: HW BUF_ERR_INT Mask */ + +#define UART_ISR_HW_TOUT_INT_Pos 28 /*!< UART_T::ISR: HW TOUT_INT Position */ +#define UART_ISR_HW_TOUT_INT_Msk (1ul << UART_ISR_HW_TOUT_INT_Pos) /*!< UART_T::ISR: HW TOUT_INT Mask */ + +#define UART_ISR_HW_MODEM_INT_Pos 27 /*!< UART_T::ISR: HW MODEM_INT Position */ +#define UART_ISR_HW_MODEM_INT_Msk (1ul << UART_ISR_HW_MODEM_INT_Pos) /*!< UART_T::ISR: HW MODEM_INT Mask */ + +#define UART_ISR_HW_RLS_INT_Pos 26 /*!< UART_T::ISR: HW RLS_INT Position */ +#define UART_ISR_HW_RLS_INT_Msk (1ul << UART_ISR_HW_RLS_INT_Pos) /*!< UART_T::ISR: HW RLS_INT Position */ + +#define UART_ISR_HW_BUF_ERR_IF_Pos 21 /*!< UART_T::ISR: HW BUF_ERR_IF Position */ +#define UART_ISR_HW_BUF_ERR_IF_Msk (1ul << UART_ISR_HW_BUF_ERR_IF_Pos) /*!< UART_T::ISR: HW BUF_ERR_IF Mask */ + +#define UART_ISR_HW_TOUT_IF_Pos 20 /*!< UART_T::ISR: HW TOUT_IF Position */ +#define UART_ISR_HW_TOUT_IF_Msk (1ul << UART_ISR_HW_TOUT_IF_Pos) /*!< UART_T::ISR: HW TOUT_IF Mask */ + +#define UART_ISR_HW_MODEM_IF_Pos 19 /*!< UART_T::ISR: HW MODEM_IF Position */ +#define UART_ISR_HW_MODEM_IF_Msk (1ul << UART_ISR_HW_MODEM_IF_Pos) /*!< UART_T::ISR: HW MODEM_IF Mask */ + +#define UART_ISR_HW_RLS_IF_Pos 18 /*!< UART_T::ISR: HW RLS_IF Position */ +#define UART_ISR_HW_RLS_IF_Msk (1ul << UART_ISR_HW_RLS_IF_Pos) /*!< UART_T::ISR: HW RLS_IF Mark */ + +#define UART_ISR_BUF_ERR_INT_Pos 13 /*!< UART_T::ISR: BUF_ERR_INT Position */ +#define UART_ISR_BUF_ERR_INT_Msk (1ul << UART_ISR_BUF_ERR_INT_Pos) /*!< UART_T::ISR: BUF_ERR_INT Mask */ + +#define UART_ISR_TOUT_INT_Pos 12 /*!< UART_T::ISR: TOUT_INT Position */ +#define UART_ISR_TOUT_INT_Msk (1ul << UART_ISR_TOUT_INT_Pos) /*!< UART_T::ISR: TOUT_INT Mask */ + +#define UART_ISR_MODEM_INT_Pos 11 /*!< UART_T::ISR: MODEM_INT Position */ +#define UART_ISR_MODEM_INT_Msk (1ul << UART_ISR_MODEM_INT_Pos) /*!< UART_T::ISR: MODEM_INT Mask */ + +#define UART_ISR_RLS_INT_Pos 10 /*!< UART_T::ISR: RLS_INT Position */ +#define UART_ISR_RLS_INT_Msk (1ul << UART_ISR_RLS_INT_Pos) /*!< UART_T::ISR: RLS_INT Mask */ + +#define UART_ISR_THRE_INT_Pos 9 /*!< UART_T::ISR: THRE_INT Position */ +#define UART_ISR_THRE_INT_Msk (1ul << UART_ISR_THRE_INT_Pos) /*!< UART_T::ISR: THRE_INT Mask */ + +#define UART_ISR_RDA_INT_Pos 8 /*!< UART_T::ISR: RDA_INT Position */ +#define UART_ISR_RDA_INT_Msk (1ul << UART_ISR_RDA_INT_Pos) /*!< UART_T::ISR: RDA_INT Mask */ + +#define UART_ISR_BUF_ERR_IF_Pos 5 /*!< UART_T::ISR: BUF_ERR_IF Position */ +#define UART_ISR_BUF_ERR_IF_Msk (1ul << UART_ISR_BUF_ERR_IF_Pos) /*!< UART_T::ISR: BUF_ERR_IF Mask */ + +#define UART_ISR_TOUT_IF_Pos 4 /*!< UART_T::ISR: TOUT_IF Position */ +#define UART_ISR_TOUT_IF_Msk (1ul << UART_ISR_TOUT_IF_Pos) /*!< UART_T::ISR: TOUT_IF Mask */ + +#define UART_ISR_MODEM_IF_Pos 3 /*!< UART_T::ISR: MODEM_IF Position */ +#define UART_ISR_MODEM_IF_Msk (1ul << UART_ISR_MODEM_IF_Pos) /*!< UART_T::ISR: MODEM_IF Mask */ + +#define UART_ISR_RLS_IF_Pos 2 /*!< UART_T::ISR: RLS_IF Position */ +#define UART_ISR_RLS_IF_Msk (1ul << UART_ISR_RLS_IF_Pos) /*!< UART_T::ISR: RLS_IF Mask */ + +#define UART_ISR_THRE_IF_Pos 1 /*!< UART_T::ISR: THRE_IF Position */ +#define UART_ISR_THRE_IF_Msk (1ul << UART_ISR_THRE_IF_Pos) /*!< UART_T::ISR: THRE_IF Mask */ + +#define UART_ISR_RDA_IF_Pos 0 /*!< UART_T::ISR: RDA_IF Position */ +#define UART_ISR_RDA_IF_Msk (1ul << UART_ISR_RDA_IF_Pos) /*!< UART_T::ISR: RDA_IF Mask */ + + +/* UART TOR Bit Field Definitions */ +#define UART_TOR_DLY_Pos 8 /*!< UART_T::TOR: DLY Position */ +#define UART_TOR_DLY_Msk (0xFFul << UART_TOR_DLY_Pos) /*!< UART_T::TOR: DLY Mask */ + +#define UART_TOR_TOIC_Pos 0 /*!< UART_T::TOR: TOIC Position */ +#define UART_TOR_TOIC_Msk (0xFFul << UART_TOR_TOIC_Pos) + +/* UART BAUD Bit Field Definitions */ +#define UART_BAUD_DIV_X_EN_Pos 29 /*!< UART_T::BAUD: DIV_X_EN Position */ +#define UART_BAUD_DIV_X_EN_Msk (1ul << UART_BAUD_DIV_X_EN_Pos) /*!< UART_T::BAUD: DIV_X_EN Mask */ + +#define UART_BAUD_DIV_X_ONE_Pos 28 /*!< UART_T::BAUD: DIV_X_ONE Position */ +#define UART_BAUD_DIV_X_ONE_Msk (1ul << UART_BAUD_DIV_X_ONE_Pos) /*!< UART_T::BAUD: DIV_X_ONE Mask */ + +#define UART_BAUD_DIVIDER_X_Pos 24 /*!< UART_T::BAUD: DIVIDER_X Position */ +#define UART_BAUD_DIVIDER_X_Msk (0xFul << UART_BAUD_DIVIDER_X_Pos) /*!< UART_T::BAUD: DIVIDER_X Mask */ + +#define UART_BAUD_BRD_Pos 0 /*!< UART_T::BAUD: BRD Position */ +#define UART_BAUD_BRD_Msk (0xFFFFul << UART_BAUD_BRD_Pos) /*!< UART_T::BAUD: BRD Mask */ + +/* UART IRCR Bit Field Definitions */ +#define UART_IRCR_INV_RX_Pos 6 /*!< UART_T::IRCR: INV_RX Position */ +#define UART_IRCR_INV_RX_Msk (1ul << UART_IRCR_INV_RX_Pos) /*!< UART_T::IRCR: INV_RX Mask */ + +#define UART_IRCR_INV_TX_Pos 5 /*!< UART_T::IRCR: INV_TX Position */ +#define UART_IRCR_INV_TX_Msk (1ul << UART_IRCR_INV_TX_Pos) /*!< UART_T::IRCR: INV_TX Mask */ + +#define UART_IRCR_TX_SELECT_Pos 1 /*!< UART_T::IRCR: TX_SELECT Position */ +#define UART_IRCR_TX_SELECT_Msk (1ul << UART_IRCR_TX_SELECT_Pos) /*!< UART_T::IRCR: TX_SELECT Mask */ + +/* UART ALT_CSR Bit Field Definitions */ +#define UART_ALT_CSR_ADDR_MATCH_Pos 24 /*!< UART_T::ALT_CSR: ADDR_MATCH Position */ +#define UART_ALT_CSR_ADDR_MATCH_Msk (0xFFul << UART_ALT_CSR_ADDR_MATCH_Pos) /*!< UART_T::ALT_CSR: ADDR_MATCH Mask */ + +#define UART_ALT_CSR_RS485_ADD_EN_Pos 15 /*!< UART_T::ALT_CSR: RS485_ADD_EN Position */ +#define UART_ALT_CSR_RS485_ADD_EN_Msk (1ul << UART_ALT_CSR_RS485_ADD_EN_Pos) /*!< UART_T::ALT_CSR: RS485_ADD_EN Mask */ + +#define UART_ALT_CSR_RS485_AUD_Pos 10 /*!< UART_T::ALT_CSR: RS485_AUD Position */ +#define UART_ALT_CSR_RS485_AUD_Msk (1ul << UART_ALT_CSR_RS485_AUD_Pos) /*!< UART_T::ALT_CSR: RS485_AUD Mask */ + +#define UART_ALT_CSR_RS485_AAD_Pos 9 /*!< UART_T::ALT_CSR: RS485_AAD Position */ +#define UART_ALT_CSR_RS485_AAD_Msk (1ul << UART_ALT_CSR_RS485_AAD_Pos) /*!< UART_T::ALT_CSR: RS485_AAD Mask */ + +#define UART_ALT_CSR_RS485_NMM_Pos 8 /*!< UART_T::ALT_CSR: RS485_NMM Position */ +#define UART_ALT_CSR_RS485_NMM_Msk (1ul << UART_ALT_CSR_RS485_NMM_Pos) /*!< UART_T::ALT_CSR: RS485_NMM Mask */ + +/* UART FUN_SEL Bit Field Definitions */ +#define UART_FUN_SEL_FUN_SEL_Pos 0 /*!< UART_T::FUN_SEL: FUN_SEL Position */ +#define UART_FUN_SEL_FUN_SEL_Msk (0x3ul << UART_FUN_SEL_FUN_SEL_Pos) /*!< UART_T::FUN_SEL: FUN_SEL Mask */ + +/*@}*/ /* end of group REG_UART_BITMASK */ +/*@}*/ /* end of group REG_UART */ + +/*--------------------------- USB Device Controller --------------------------*/ +/** @addtogroup REG_USBD Universal Serial Bus Device Controller (USBD) + Memory Mapped Structure for USB Device Controller + @{ + */ + + +typedef struct +{ + + +/** + * @var USBD_EP_T::BUFSEG + * Offset: 0x500/0x510/0x520/0x530/0x540/0x550/0x560/0x570 Endpoint 0~7 Buffer Segmentation Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8:3] |BUFSEG |Endpoint Buffer Segmentation + * | | |It is used to indicate the offset address for each endpoint with the USB SRAM starting address The effective starting address of the endpoint is + * | | |USB_SRAM address + { BUFSEG[8:3], 3'b000} + * | | |Where the USB_SRAM address = USBD_BA+0x100h. + * @var USBD_EP_T::MXPLD + * Offset: 0x504/0x514/0x524/0x534/0x544/0x554/0x564/0x574 Endpoint 0~7 Maximal Payload Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8:0] |MXPLD |Maximal Payload + * | | |Define the data length which is transmitted to host (IN token) or the actual data length which is received from the host (OUT token). + * | | |It also used to indicate that the endpoint is ready to be transmitted in IN token or received in OUT token. + * | | |(1) When the register is written by CPU, + * | | |For IN token, the value of MXPLD is used to define the data length to be transmitted and indicate the data buffer is ready. + * | | |For OUT token, it means that the controller is ready to receive data from the host and the value of MXPLD is the maximal data length comes from host. + * | | |(2) When the register is read by CPU, + * | | |For IN token, the value of MXPLD is indicated by the data length be transmitted to host + * | | |For OUT token, the value of MXPLD is indicated the actual data length receiving from host. + * | | |Note: Once MXPLD is written, the data packets will be transmitted/received immediately after IN/OUT token arrived. + * @var USBD_EP_T::CFG + * Offset: 0x508/0x518/0x528/0x538/0x548/0x558/0x568/0x578 Endpoint 0~7 Configuration Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[3:0] |EP_NUM |Endpoint Number + * | | |These bits are used to define the endpoint number of the current endpoint. + * |[4] |ISOCH |Isochronous Endpoint + * | | |This bit is used to set the endpoint as Isochronous endpoint, no handshake. + * | | |0 = No Isochronous endpoint. + * | | |1 = Isochronous endpoint. + * |[6:5] |STATE |Endpoint STATE + * | | |00 = Endpoint is Disabled. + * | | |01 = Out endpoint. + * | | |10 = IN endpoint. + * | | |11 = Undefined. + * |[7] |DSQ_SYNC |Data Sequence Synchronization + * | | |0 = DATA0 PID. + * | | |1 = DATA1 PID. + * | | |Note: It is used to specify the DATA0 or DATA1 PID in the following IN token transaction. + * | | |Hardware will toggle automatically in IN token base on the bit. + * |[9] |CSTALL |Clear STALL Response + * | | |0 = Disable the device to clear the STALL handshake in setup stage. + * | | |1 = Clear the device to response STALL handshake in setup stage. + * @var USBD_EP_T::CFGP + * Offset: 0x50C/0x51C/0x52C/0x53C/0x54C/0x55C/0x56C/0x57C Endpoint 0~7 Set Stall and Clear In/Out Ready Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |CLRRDY |Clear Ready + * | | |When the USB_MXPLD register is set by user, it means that the endpoint is ready to transmit or receive data. + * | | |If the user wants to turn off this transaction before the transaction start, users can set this bit to 1 to turn it off and it will be cleared to 0 automatically. + * | | |For IN token, write '1' to clear the IN token had ready to transmit the data to USB. + * | | |For OUT token, write '1' to clear the OUT token had ready to receive the data from USB. + * | | |This bit is write 1 only and is always 0 when it is read back. + * |[1] |SSTALL |Set STALL + * | | |0 = Disable the device to response STALL. + * | | |1 = Set the device to respond STALL automatically. + */ + + __IO uint32_t BUFSEG; /* Offset: 0x500/0x510/0x520/0x530/0x540/0x550/0x560/0x570 Endpoint 0~7 Buffer Segmentation Register */ + __IO uint32_t MXPLD; /* Offset: 0x504/0x514/0x524/0x534/0x544/0x554/0x564/0x574 Endpoint 0~7 Maximal Payload Register */ + __IO uint32_t CFG; /* Offset: 0x508/0x518/0x528/0x538/0x548/0x558/0x568/0x578 Endpoint 0~7 Configuration Register */ + __IO uint32_t CFGP; /* Offset: 0x50C/0x51C/0x52C/0x53C/0x54C/0x55C/0x56C/0x57C Endpoint 0~7 Set Stall and Clear In/Out Ready Control Register */ + +} USBD_EP_T; + + + + + +typedef struct +{ + + +/** + * @var USBD_T::INTEN + * Offset: 0x00 USB Interrupt Enable Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |BUS_IE |Bus Event Interrupt Enable + * | | |0 = BUS event interrupt Disabled. + * | | |1 = BUS event interrupt Enabled. + * |[1] |USB_IE |USB Event Interrupt Enable + * | | |0 = USB event interrupt Disabled. + * | | |1 = USB event interrupt Enabled. + * |[2] |FLDET_IE |Floating Detection Interrupt Enable + * | | |0 = Floating detection Interrupt Disabled. + * | | |1 = Floating detection Interrupt Enabled. + * |[3] |WAKEUP_IE |USB Wake-Up Interrupt Enable + * | | |0 = Wake-up Interrupt Disabled. + * | | |1 = Wake-up Interrupt Enabled. + * |[8] |WAKEUP_EN |Wake-Up Function Enable + * | | |0 = USB wake-up function Disabled. + * | | |1 = USB wake-up function Enabled. + * |[15] |INNAK_EN |Active NAK Function And Its Status In IN Token + * | | |0 = When device responds NAK after receiving IN token, IN NAK status will not be + * | | | updated to USBD_EPSTS register, so that the USB interrupt event will not be asserted. + * | | |1 = IN NAK status will be updated to USBD_EPSTS register and the USB interrupt event + * | | | will be asserted, when the device responds NAK after receiving IN token. + * @var USBD_T::INTSTS + * Offset: 0x04 USB Interrupt Event Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |BUS_STS |BUS Interrupt Status + * | | |The BUS event means that there is one of the suspense or the resume function in the bus. + * | | |0 = No BUS event occurred. + * | | |1 = Bus event occurred; check USB_ATTR[3:0] to know which kind of bus event was occurred, cleared by write 1 to USB_INTSTS[0]. + * |[1] |USB_STS |USB Event Interrupt Status + * | | |The USB event includes the SETUP Token, IN Token, OUT ACK, ISO IN, or ISO OUT events in the bus. + * | | |0 = No USB event occurred. + * | | |1 = USB event occurred, check EPSTS0~7 to know which kind of USB event occurred. + * | | |Cleared by write 1 to USB_INTSTS[1] or EPEVT0~7 and SETUP (USB_INTSTS[31]). + * |[2] |FLDET_STS |Floating Detection Interrupt Status + * | | |0 = There is not attached/detached event in the USB. + * | | |1 = There is attached/detached event in the USB bus and it is cleared by write 1 to USB_INTSTS[2]. + * |[3] |WAKEUP_STS|Wake-Up Interrupt Status + * | | |0 = No Wake-up event occurred. + * | | |1 = Wake-up event occurred, cleared by write 1 to USB_INTSTS[3]. + * |[16] |EPEVT0 |Endpoint 0's USB Event Status + * | | |0 = No event occurred on endpoint 0. + * | | |1 = USB event occurred on Endpoint 0, check USB_EPSTS[10:8] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[16] or USB_INTSTS[1]. + * |[17] |EPEVT1 |Endpoint 1's USB Event Status + * | | |0 = No event occurred on endpoint 1. + * | | |1 = USB event occurred on Endpoint 1, check USB_EPSTS[13:11] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[17] or USB_INTSTS[1]. + * |[18] |EPEVT2 |Endpoint 2's USB Event Status + * | | |0 = No event occurred on endpoint 2. + * | | |1 = USB event occurred on Endpoint 2, check USB_EPSTS[16:14] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[18] or USB_INTSTS[1]. + * |[19] |EPEVT3 |Endpoint 3's USB Event Status + * | | |0 = No event occurred on endpoint 3. + * | | |1 = USB event occurred on Endpoint 3, check USB_EPSTS[19:17] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[19] or USB_INTSTS[1]. + * |[20] |EPEVT4 |Endpoint 4's USB Event Status + * | | |0 = No event occurred on endpoint 4. + * | | |1 = USB event occurred on Endpoint 4, check USB_EPSTS[22:20] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[20] or USB_INTSTS[1]. + * |[21] |EPEVT5 |Endpoint 5's USB Event Status + * | | |0 = No event occurred on endpoint 5. + * | | |1 = USB event occurred on Endpoint 5, check USB_EPSTS[25:23] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[21] or USB_INTSTS[1]. + * |[22] |EPEVT6 |Endpoint 6's USB Event Status + * | | |0 = No event occurred on endpoint 6. + * | | |1 = USB event occurred on Endpoint 6, check USB_EPSTS[28:26] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[22] or USB_INTSTS[1]. + * |[23] |EPEVT7 |Endpoint 7's USB Event Status + * | | |0 = No event occurred on endpoint 7. + * | | |1 = USB event occurred on Endpoint 7, check USB_EPSTS[31:29] to know which kind of USB event was occurred, cleared by write 1 to USB_INTSTS[23] or USB_INTSTS[1]. + * |[31] |SETUP |Setup Event Status + * | | |0 = No Setup event. + * | | |1 = SETUP event occurred, cleared by write 1 to USB_INTSTS[31]. + * @var USBD_T::FADDR + * Offset: 0x08 USB Device Function Address Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[6:0] |FADDR |USB Device Function Address + * @var USBD_T::EPSTS + * Offset: 0x0C USB Endpoint Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[7] |OVERRUN |Overrun + * | | |It indicates that the received data is over the maximum payload number or not. + * | | |0 = No overrun. + * | | |1 = Out Data is more than the Max Payload in MXPLD register or the Setup Data is more than 8 Bytes. + * |[10:8] |EPSTS0 |Endpoint 0 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[13:11] |EPSTS1 |Endpoint 1 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[16:14] |EPSTS2 |Endpoint 2 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[19:17] |EPSTS3 |Endpoint 3 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[22:20] |EPSTS4 |Endpoint 4 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[25:23] |EPSTS5 |Endpoint 5 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[28:26] |EPSTS6 |Endpoint 6 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * |[31:29] |EPSTS7 |Endpoint 7 Bus Status + * | | |These bits are used to indicate the current status of this endpoint + * | | |000 = In ACK. + * | | |001 = In NAK. + * | | |010 = Out Packet Data0 ACK. + * | | |110 = Out Packet Data1 ACK. + * | | |011 = Setup ACK. + * | | |111 = Isochronous transfer end. + * @var USBD_T::ATTR + * Offset: 0x10 USB Bus Status and Attribution Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |USBRST |USB Reset Status + * | | |0 = Bus no reset. + * | | |1 = Bus reset when SE0 (single-ended 0) is presented more than 2.5us. + * | | |Note: This bit is read only. + * |[1] |SUSPEND |Suspend Status + * | | |0 = Bus no suspend. + * | | |1 = Bus idle more than 3ms, either cable is plugged off or host is sleeping. + * | | |Note: This bit is read only. + * |[2] |RESUME |Resume Status + * | | |0 = No bus resume. + * | | |1 = Resume from suspend. + * | | |Note: This bit is read only. + * |[3] |TIMEOUT |Time-Out Status + * | | |0 = No time-out. + * | | |1 = No Bus response more than 18 bits time. + * | | |Note: This bit is read only. + * |[4] |PHY_EN |PHY Transceiver Function Enable + * | | |0 = PHY transceiver function Disabled. + * | | |1 = PHY transceiver function Enabled. + * |[5] |RWAKEUP |Remote Wake-Up + * | | |0 = Release the USB bus from K state. + * | | |1 = Force USB bus to K (USB_D+ low, USB_D- high) state, used for remote wake-up. + * |[7] |USB_EN |USB Controller Enable + * | | |0 = USB Controller Disabled. + * | | |1 = USB Controller Enabled. + * |[8] |DPPU_EN |Pull-Up Resistor On USB_D+ Enable + * | | |0 = Pull-up resistor in USB_D+ pin Disabled. + * | | |1 = Pull-up resistor in USB_D+ pin Enabled. + * |[9] |PWRDN |Power-Down PHY Transceiver, Low Active + * | | |0 = Power-down related circuit of PHY transceiver. + * | | |1 = Turn-on related circuit of PHY transceiver. + * |[10] |BYTEM |CPU Access USB SRAM Size Mode Selection + * | | |0 = Word mode: The size of the transfer from CPU to USB SRAM can be Word only. + * | | |1 = Byte mode: The size of the transfer from CPU to USB SRAM can be Byte only. + * @var USBD_T::FLDET + * Offset: 0x14 USB Floating Detection Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |FLDET |Device Floating Detected + * | | |0 = Controller is not attached into the USB host. + * | | |1 =Controller is attached into the BUS. + * @var USBD_T::STBUFSEG + * Offset: 0x18 Setup Token Buffer Segmentation Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[8:3] |STBUFSEG |Setup Token Buffer Segmentation + * | | |It is used to indicate the offset address for the SETUP token with the USB Device SRAM starting address The effective starting address is + * | | |USB_SRAM address + {STBUFSEG[8:3], 3'b000} + * | | |Where the USB_SRAM address = USBD_BA+0x100h. + * | | |Note: It is used for SETUP token only. + * @var USBD_T::DRVSE0 + * Offset: 0x90 USB Drive SE0 Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |DRVSE0 |Drive Single Ended Zero In USB Bus + * | | |The Single Ended Zero (SE0) is when both lines (USB_D+ and USB_D-) are being pulled low. + * | | |0 = None. + * | | |1 = Force USB PHY transceiver to drive SE0. + */ + + __IO uint32_t INTEN; /* Offset: 0x00 USB Interrupt Enable Register */ + __IO uint32_t INTSTS; /* Offset: 0x04 USB Interrupt Event Status Register */ + __IO uint32_t FADDR; /* Offset: 0x08 USB Device Function Address Register */ + __I uint32_t EPSTS; /* Offset: 0x0C USB Endpoint Status Register */ + __IO uint32_t ATTR; /* Offset: 0x10 USB Bus Status and Attribution Register */ + __I uint32_t FLDET; /* Offset: 0x14 USB Floating Detection Register */ + __IO uint32_t STBUFSEG; /* Offset: 0x18 Setup Token Buffer Segmentation Register */ + __I uint32_t RESERVE0[29]; + __IO uint32_t DRVSE0; /* Offset: 0x90 USB Drive SE0 Control Register */ + __I uint32_t RESERVE2[283]; + USBD_EP_T EP[8]; /* Offset: 0x500 Endpoint 0~7 Control Registers */ + +} USBD_T; + + + + +/** @addtogroup REG_USBD_BITMASK USBD Bit Mask + @{ + */ + +/* USBD INTEN Bit Field Definitions */ +#define USBD_INTEN_INNAK_EN_Pos 15 /*!< USBD_T::INTEN: INNAK_EN Position */ +#define USBD_INTEN_INNAK_EN_Msk (1ul << USBD_INTEN_INNAK_EN_Pos) /*!< USBD_T::INTEN: INNAK_EN Mask */ + +#define USBD_INTEN_WAKEUP_EN_Pos 8 /*!< USBD_T::INTEN: RWAKEUP Position */ +#define USBD_INTEN_WAKEUP_EN_Msk (1ul << USBD_INTEN_WAKEUP_EN_Pos) /*!< USBD_T::INTEN: RWAKEUP Mask */ + +#define USBD_INTEN_WAKEUP_IE_Pos 3 /*!< USBD_T::INTEN: WAKEUP_IE Position */ +#define USBD_INTEN_WAKEUP_IE_Msk (1ul << USBD_INTEN_WAKEUP_IE_Pos) /*!< USBD_T::INTEN: WAKEUP_IE Mask */ + +#define USBD_INTEN_FLDET_IE_Pos 2 /*!< USBD_T::INTEN: FLDET_IE Position */ +#define USBD_INTEN_FLDET_IE_Msk (1ul << USBD_INTEN_FLDET_IE_Pos) /*!< USBD_T::INTEN: FLDET_IE Mask */ + +#define USBD_INTEN_USB_IE_Pos 1 /*!< USBD_T::INTEN: USB_IE Position */ +#define USBD_INTEN_USB_IE_Msk (1ul << USBD_INTEN_USB_IE_Pos) /*!< USBD_T::INTEN: USB_IE Mask */ + +#define USBD_INTEN_BUS_IE_Pos 0 /*!< USBD_T::INTEN: BUS_IE Position */ +#define USBD_INTEN_BUS_IE_Msk (1ul << USBD_INTEN_BUS_IE_Pos) /*!< USBD_T::INTEN: BUS_IE Mask */ + +/* USBD INTSTS Bit Field Definitions */ +#define USBD_INTSTS_SETUP_Pos 31 /*!< USBD_T::INTSTS: SETUP Position */ +#define USBD_INTSTS_SETUP_Msk (1ul << USBD_INTSTS_SETUP_Pos) /*!< USBD_T::INTSTS: SETUP Mask */ + +#define USBD_INTSTS_EPEVT7_Pos 23 /*!< USBD_T::INTSTS: EPEVT7 Position */ +#define USBD_INTSTS_EPEVT7_Msk (0x1ul << USBD_INTSTS_EPEVT7_Pos) /*!< USBD_T::INTSTS: EPEVT7 Mask */ + +#define USBD_INTSTS_EPEVT6_Pos 22 /*!< USBD_T::INTSTS: EPEVT6 Position */ +#define USBD_INTSTS_EPEVT6_Msk (0x1ul << USBD_INTSTS_EPEVT6_Pos) /*!< USBD_T::INTSTS: EPEVT6 Mask */ + +#define USBD_INTSTS_EPEVT5_Pos 21 /*!< USBD_T::INTSTS: EPEVT5 Position */ +#define USBD_INTSTS_EPEVT5_Msk (0x1ul << USBD_INTSTS_EPEVT5_Pos) /*!< USBD_T::INTSTS: EPEVT5 Mask */ + +#define USBD_INTSTS_EPEVT4_Pos 20 /*!< USBD_T::INTSTS: EPEVT4 Position */ +#define USBD_INTSTS_EPEVT4_Msk (0x1ul << USBD_INTSTS_EPEVT4_Pos) /*!< USBD_T::INTSTS: EPEVT4 Mask */ + +#define USBD_INTSTS_EPEVT3_Pos 19 /*!< USBD_T::INTSTS: EPEVT3 Position */ +#define USBD_INTSTS_EPEVT3_Msk (0x1ul << USBD_INTSTS_EPEVT3_Pos) /*!< USBD_T::INTSTS: EPEVT3 Mask */ + +#define USBD_INTSTS_EPEVT2_Pos 18 /*!< USBD_T::INTSTS: EPEVT2 Position */ +#define USBD_INTSTS_EPEVT2_Msk (0x1ul << USBD_INTSTS_EPEVT2_Pos) /*!< USBD_T::INTSTS: EPEVT2 Mask */ + +#define USBD_INTSTS_EPEVT1_Pos 17 /*!< USBD_T::INTSTS: EPEVT1 Position */ +#define USBD_INTSTS_EPEVT1_Msk (0x1ul << USBD_INTSTS_EPEVT1_Pos) /*!< USBD_T::INTSTS: EPEVT1 Mask */ + +#define USBD_INTSTS_EPEVT0_Pos 16 /*!< USBD_T::INTSTS: EPEVT0 Position */ +#define USBD_INTSTS_EPEVT0_Msk (0x1ul << USBD_INTSTS_EPEVT0_Pos) /*!< USBD_T::INTSTS: EPEVT0 Mask */ + +#define USBD_INTSTS_WAKEUP_STS_Pos 3 /*!< USBD_T::INTSTS: WAKEUP_STS Position */ +#define USBD_INTSTS_WAKEUP_STS_Msk (1ul << USBD_INTSTS_WAKEUP_STS_Pos) /*!< USBD_T::INTSTS: WAKEUP_STS Mask */ + +#define USBD_INTSTS_FLDET_STS_Pos 2 /*!< USBD_T::INTSTS: FLDET_STS Position */ +#define USBD_INTSTS_FLDET_STS_Msk (1ul << USBD_INTSTS_FLDET_STS_Pos) /*!< USBD_T::INTSTS: FLDET_STS Mask */ + +#define USBD_INTSTS_USB_STS_Pos 1 /*!< USBD_T::INTSTS: USB_STS Position */ +#define USBD_INTSTS_USB_STS_Msk (1ul << USBD_INTSTS_USB_STS_Pos) /*!< USBD_T::INTSTS: USB_STS Mask */ + +#define USBD_INTSTS_BUS_STS_Pos 0 /*!< USBD_T::INTSTS: BUS_STS Position */ +#define USBD_INTSTS_BUS_STS_Msk (1ul << USBD_INTSTS_BUS_STS_Pos) /*!< USBD_T::INTSTS: BUS_STS Mask */ + +/* USBD FADDR Bit Field Definitions */ +#define USBD_FADDR_FADDR_Pos 0 /*!< USBD_T::FADDR: FADDR Position */ +#define USBD_FADDR_FADDR_Msk (0x7Ful << USBD_FADDR_FADDR_Pos) /*!< USBD_T::FADDR: FADDR Mask */ + +/* USBD EPSTS Bit Field Definitions */ +#define USBD_EPSTS_EPSTS7_Pos 29 /*!< USBD_T::EPSTS: EPSTS7 Position */ +#define USBD_EPSTS_EPSTS7_Msk (7ul << USBD_EPSTS_EPSTS7_Pos) /*!< USBD_T::EPSTS: EPSTS7 Mask */ + +#define USBD_EPSTS_EPSTS6_Pos 26 /*!< USBD_T::EPSTS: EPSTS6 Position */ +#define USBD_EPSTS_EPSTS6_Msk (7ul << USBD_EPSTS_EPSTS6_Pos) /*!< USBD_T::EPSTS: EPSTS6 Mask */ + +#define USBD_EPSTS_EPSTS5_Pos 23 /*!< USBD_T::EPSTS: EPSTS5 Position */ +#define USBD_EPSTS_EPSTS5_Msk (7ul << USBD_EPSTS_EPSTS5_Pos) /*!< USBD_T::EPSTS: EPSTS5 Mask */ + +#define USBD_EPSTS_EPSTS4_Pos 20 /*!< USBD_T::EPSTS: EPSTS4 Position */ +#define USBD_EPSTS_EPSTS4_Msk (7ul << USBD_EPSTS_EPSTS4_Pos) /*!< USBD_T::EPSTS: EPSTS4 Mask */ + +#define USBD_EPSTS_EPSTS3_Pos 17 /*!< USBD_T::EPSTS: EPSTS3 Position */ +#define USBD_EPSTS_EPSTS3_Msk (7ul << USBD_EPSTS_EPSTS3_Pos) /*!< USBD_T::EPSTS: EPSTS3 Mask */ + +#define USBD_EPSTS_EPSTS2_Pos 14 /*!< USBD_T::EPSTS: EPSTS2 Position */ +#define USBD_EPSTS_EPSTS2_Msk (7ul << USBD_EPSTS_EPSTS2_Pos) /*!< USBD_T::EPSTS: EPSTS2 Mask */ + +#define USBD_EPSTS_EPSTS1_Pos 11 /*!< USBD_T::EPSTS: EPSTS1 Position */ +#define USBD_EPSTS_EPSTS1_Msk (7ul << USBD_EPSTS_EPSTS1_Pos) /*!< USBD_T::EPSTS: EPSTS1 Mask */ + +#define USBD_EPSTS_EPSTS0_Pos 8 /*!< USBD_T::EPSTS: EPSTS0 Position */ +#define USBD_EPSTS_EPSTS0_Msk (7ul << USBD_EPSTS_EPSTS0_Pos) /*!< USBD_T::EPSTS: EPSTS0 Mask */ + +#define USBD_EPSTS_OVERRUN_Pos 7 /*!< USBD_T::EPSTS: OVERRUN Position */ +#define USBD_EPSTS_OVERRUN_Msk (1ul << USBD_EPSTS_OVERRUN_Pos) /*!< USBD_T::EPSTS: OVERRUN Mask */ + +/* USBD ATTR Bit Field Definitions */ +#define USBD_ATTR_BYTEM_Pos 10 /*!< USBD_T::ATTR: BYTEM Position */ +#define USBD_ATTR_BYTEM_Msk (1ul << USBD_ATTR_BYTEM_Pos) /*!< USBD_T::ATTR: BYTEM Mask */ + +#define USBD_ATTR_PWRDN_Pos 9 /*!< USBD_T::ATTR: PWRDN Position */ +#define USBD_ATTR_PWRDN_Msk (1ul << USBD_ATTR_PWRDN_Pos) /*!< USBD_T::ATTR: PWRDN Mask */ + +#define USBD_ATTR_DPPU_EN_Pos 8 /*!< USBD_T::ATTR: DPPU_EN Position */ +#define USBD_ATTR_DPPU_EN_Msk (1ul << USBD_ATTR_DPPU_EN_Pos) /*!< USBD_T::ATTR: DPPU_EN Mask */ + +#define USBD_ATTR_USB_EN_Pos 7 /*!< USBD_T::ATTR: USB_EN Position */ +#define USBD_ATTR_USB_EN_Msk (1ul << USBD_ATTR_USB_EN_Pos) /*!< USBD_T::ATTR: USB_EN Mask */ + +#define USBD_ATTR_RWAKEUP_Pos 5 /*!< USBD_T::ATTR: RWAKEUP Position */ +#define USBD_ATTR_RWAKEUP_Msk (1ul << USBD_ATTR_RWAKEUP_Pos) /*!< USBD_T::ATTR: RWAKEUP Mask */ + +#define USBD_ATTR_PHY_EN_Pos 4 /*!< USBD_T::ATTR: PHY_EN Position */ +#define USBD_ATTR_PHY_EN_Msk (1ul << USBD_ATTR_PHY_EN_Pos) /*!< USBD_T::ATTR: PHY_EN Mask */ + +#define USBD_ATTR_TIMEOUT_Pos 3 /*!< USBD_T::ATTR: TIMEOUT Position */ +#define USBD_ATTR_TIMEOUT_Msk (1ul << USBD_ATTR_TIMEOUT_Pos) /*!< USBD_T::ATTR: TIMEOUT Mask */ + +#define USBD_ATTR_RESUME_Pos 2 /*!< USBD_T::ATTR: RESUME Position */ +#define USBD_ATTR_RESUME_Msk (1ul << USBD_ATTR_RESUME_Pos) /*!< USBD_T::ATTR: RESUME Mask */ + +#define USBD_ATTR_SUSPEND_Pos 1 /*!< USBD_T::ATTR: SUSPEND Position */ +#define USBD_ATTR_SUSPEND_Msk (1ul << USBD_ATTR_SUSPEND_Pos) /*!< USBD_T::ATTR: SUSPEND Mask */ + +#define USBD_ATTR_USBRST_Pos 0 /*!< USBD_T::ATTR: USBRST Position */ +#define USBD_ATTR_USBRST_Msk (1ul << USBD_ATTR_USBRST_Pos) /*!< USBD_T::ATTR: USBRST Mask */ + +/* USBD FLDET Bit Field Definitions */ +#define USBD_FLDET_FLDET_Pos 0 /*!< USBD_T::FLDET: FLDET Position */ +#define USBD_FLDET_FLDET_Msk (1ul << USBD_FLDET_FLDET_Pos) /*!< USBD_T::FLDET: FLDET Mask */ + +/* USBD STBUFSEG Bit Field Definitions */ +#define USBD_STBUFSEG_STBUFSEG_Pos 3 /*!< USBD_T::STBUFSEG: STBUFSEG Position */ +#define USBD_STBUFSEG_STBUFSEG_Msk (0x3Ful << USBD_STBUFSEG_STBUFSEG_Pos) /*!< USBD_T::STBUFSEG: STBUFSEG Mask */ + +/* USBD BUFSEG Bit Field Definitions */ +#define USBD_BUFSEG_BUFSEG_Pos 3 /*!< USBD_EP_T::BUFSEG: BUFSEG Position */ +#define USBD_BUFSEG_BUFSEG_Msk (0x3Ful << USBD_BUFSEG_BUFSEG_Pos) /*!< USBD_EP_T::BUFSEG: BUFSEG Mask */ + +/* USBD MXPLD Bit Field Definitions */ +#define USBD_MXPLD_MXPLD_Pos 0 /*!< USBD_EP_T::MXPLD: MXPLD Position */ +#define USBD_MXPLD_MXPLD_Msk (0x1FFul << USBD_MXPLD_MXPLD_Pos) /*!< USBD_EP_T::MXPLD: MXPLD Mask */ + +/* USBD CFG Bit Field Definitions */ +#define USBD_CFG_CSTALL_Pos 9 /*!< USBD_EP_T::CFG: CSTALL Position */ +#define USBD_CFG_CSTALL_Msk (1ul << USBD_CFG_CSTALL_Pos) /*!< USBD_EP_T::CFG: CSTALL Mask */ + +#define USBD_CFG_DSQ_SYNC_Pos 7 /*!< USBD_EP_T::CFG: DSQ_SYNC Position */ +#define USBD_CFG_DSQ_SYNC_Msk (1ul << USBD_CFG_DSQ_SYNC_Pos) /*!< USBD_EP_T::CFG: DSQ_SYNC Mask */ + +#define USBD_CFG_STATE_Pos 5 /*!< USBD_EP_T::CFG: STATE Position */ +#define USBD_CFG_STATE_Msk (3ul << USBD_CFG_STATE_Pos) /*!< USBD_EP_T::CFG: STATE Mask */ + +#define USBD_CFG_ISOCH_Pos 4 /*!< USBD_EP_T::CFG: ISOCH Position */ +#define USBD_CFG_ISOCH_Msk (1ul << USBD_CFG_ISOCH_Pos) /*!< USBD_EP_T::CFG: ISOCH Mask */ + +#define USBD_CFG_EP_NUM_Pos 0 /*!< USBD_EP_T::CFG: EP_NUM Position */ +#define USBD_CFG_EP_NUM_Msk (0xFul << USBD_CFG_EP_NUM_Pos) /*!< USBD_EP_T::CFG: EP_NUM Mask */ + +/* USBD CFGP Bit Field Definitions */ +#define USBD_CFGP_SSTALL_Pos 1 /*!< USBD_EP_T::CFGP: SSTALL Position */ +#define USBD_CFGP_SSTALL_Msk (1ul << USBD_CFGP_SSTALL_Pos) /*!< USBD_EP_T::CFGP: SSTALL Mask */ + +#define USBD_CFGP_CLRRDY_Pos 0 /*!< USBD_EP_T::CFGP: CLRRDY Position */ +#define USBD_CFGP_CLRRDY_Msk (1ul << USBD_CFGP_CLRRDY_Pos) /*!< USBD_EP_T::CFGP: CLRRDY Mask */ + +/* USBD DRVSE0 Bit Field Definitions */ +#define USBD_DRVSE0_DRVSE0_Pos 0 /*!< USBD_T::DRVSE0: DRVSE0 Position */ +#define USBD_DRVSE0_DRVSE0_Msk (1ul << USBD_DRVSE0_DRVSE0_Pos) /*!< USBD_T::DRVSE0: DRVSE0 Mask */ +/*@}*/ /* end of group REG_USBD_BITMASK */ +/*@}*/ /* end of group REG_USBD */ + + +/*----------------------------- Watchdog Timer (WDT) -----------------------------*/ +/** @addtogroup REG_WDT Watch Dog Timer Controller (WDT) + Memory Mapped Structure for Watchdog Timer + @{ + */ + +typedef struct +{ + + +/** + * @var WDT_T::WTCR + * Offset: 0x00 Watchdog Timer Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |WTR |Reset Watchdog Timer Up Counter (Write Protect) + * | | |0 = No effect. + * | | |1 = Reset the internal 18-bit WDT up counter value. + * | | |Note: This bit will be automatically cleared by hardware. + * |[1] |WTRE |Watchdog Timer Reset Enable (Write Protect) + * | | |Setting this bit will enable the WDT time-out reset function if the WDT up counter value has not been cleared after the specific WDT reset delay period expires. + * | | |0 = WDT time-out reset function Disabled. + * | | |1 = WDT time-out reset function Enabled. + * |[2] |WTRF |Watchdog Timer Time-out Reset Flag + * | | |This bit indicates the system has been reset by WDT time-out reset or not. + * | | |0 = WDT time-out reset did not occur. + * | | |1 = WDT time-out reset occurred. + * | | |Note: This bit is cleared by writing 1 to it. + * |[3] |WTIF |Watchdog Timer Time-out Interrupt Flag + * | | |This bit will set to 1 while WDT up counter value reaches the selected WDT time-out interval. + * | | |0 = WDT time-out interrupt did not occur. + * | | |1 = WDT time-out interrupt occurred. + * | | |Note: This bit is cleared by writing 1 to it. + * |[4] |WTWKE |Watchdog Timer Time-out Wake-Up Function Control + * | | |(Write Protect) + * | | |If this bit is set to 1, while WTIF is generated to 1 and WTIE enabled, the WDT time-out interrupt signal will generate a wake-up trigger event to chip. + * | | |0 = Wake-up trigger event Disabled if WDT time-out interrupt signal generated. + * | | |1 = Wake-up trigger event Enabled if WDT time-out interrupt signal generated. + * | | |Note: Chip can be woken-up by WDT time-out interrupt signal generated only if WDT clock source is selected to 10 kHz oscillator. + * |[5] |WTWKF |Watchdog Timer Time-out Wake-Up Flag + * | | |This bit indicates the interrupt wake-up flag status of WDT. + * | | |0 = WDT does not cause chip wake-up. + * | | |1 = Chip wake-up from Idle or Power-down mode if WDT time-out interrupt signal generated. + * | | |Note: This bit is cleared by writing 1 to it. + * |[6] |WTIE |Watchdog Timer Time-out Interrupt Enable Control (Write Protect) + * | | |If this bit is enabled, the WDT time-out interrupt signal is generated and inform to CPU. + * | | |0 = WDT time-out interrupt Disabled. + * | | |1 = WDT time-out interrupt Enabled. + * |[7] |WTE |Watchdog Timer Enable Control (Write Protect) + * | | |0 = WDT Disabled. (This action will reset the internal up counter value.) + * | | |1 = WDT Enabled. + * | | |Note: If CWDTEN (CONFIG0[31] Watchdog Enable) bit is set to 0, this bit is forced as 1 and + * | | | user cannot change this bit to 0. + * |[10:8] |WTIS |Watchdog Timer Time-out Interval Selection (Write Protect) + * | | |These three bits select the time-out interval period for the WDT. + * | | |000 = 24 *TWDT. + * | | |001 = 26 * TWDT. + * | | |010 = 28 * TWDT. + * | | |011 = 210 * TWDT. + * | | |100 = 212 * TWDT. + * | | |101 = 214 * TWDT. + * | | |110 = 216 * TWDT. + * | | |111 = 218 * TWDT. + * |[31] |DBGACK_WDT|ICE Debug Mode Acknowledge Disable Control (Write Protect) + * | | |0 = ICE debug mode acknowledgment effects WDT counting. + * | | |WDT up counter will be held while CPU is held by ICE. + * | | |1 = ICE debug mode acknowledgment Disabled. + * | | |WDT up counter will keep going no matter CPU is held by ICE or not. + * @var WDT_T::WTCRALT + * Offset: 0x04 Watchdog Timer Alternative Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[1:0] |WTRDSEL |Watchdog Timer Reset Delay Selection (Write Protect) + * | | |When WDT time-out happened, user has a time named WDT Reset Delay Period to clear WDT counter to prevent WDT time-out reset happened. + * | | |User can select a suitable value of WDT Reset Delay Period for different WDT time-out period. + * | | |These bits are protected bit. + * | | |It means programming this bit needs to write "59h", "16h", "88h" to address 0x5000_0100 to disable register protection. + * | | |Reference the register REGWRPROT at address GCR_BA+0x100. + * | | |00 = Watchdog Timer Reset Delay Period is 1026 * WDT_CLK. + * | | |01 = Watchdog Timer Reset Delay Period is 130 * WDT_CLK. + * | | |10 = Watchdog Timer Reset Delay Period is 18 * WDT_CLK. + * | | |11 = Watchdog Timer Reset Delay Period is 3 * WDT_CLK. + * | | |Note: This register will be reset to 0 if WDT time-out reset happened. + */ + + __IO uint32_t WTCR; /* Offset: 0x00 Watchdog Timer Control Register */ + __IO uint32_t WTCRALT; /* Offset: 0x04 Watchdog Timer Alternative Control Register */ + +} WDT_T; + + + + +/** @addtogroup REG_WDT_BITMASK WDT Bit Mask + @{ + */ + +/* WDT WTCR Bit Field Definitions */ +#define WDT_WTCR_DBGACK_WDT_Pos 31 /*!< WDT_T::WTCR: DBGACK_WDT Position */ +#define WDT_WTCR_DBGACK_WDT_Msk (1ul << WDT_WTCR_DBGACK_WDT_Pos) /*!< WDT_T::WTCR: DBGACK_WDT Mask */ + +#define WDT_WTCR_WTIS_Pos 8 /*!< WDT_T::WTCR: WTIS Position */ +#define WDT_WTCR_WTIS_Msk (0x7ul << WDT_WTCR_WTIS_Pos) /*!< WDT_T::WTCR: WTIS Mask */ + +#define WDT_WTCR_WTE_Pos 7 /*!< WDT_T::WTCR: WTE Position */ +#define WDT_WTCR_WTE_Msk (1ul << WDT_WTCR_WTE_Pos) /*!< WDT_T::WTCR: WTE Mask */ + +#define WDT_WTCR_WTIE_Pos 6 /*!< WDT_T::WTCR: WTIE Position */ +#define WDT_WTCR_WTIE_Msk (1ul << WDT_WTCR_WTIE_Pos) /*!< WDT_T::WTCR: WTIE Mask */ + +#define WDT_WTCR_WTWKF_Pos 5 /*!< WDT_T::WTCR: WTWKF Position */ +#define WDT_WTCR_WTWKF_Msk (1ul << WDT_WTCR_WTWKF_Pos) /*!< WDT_T::WTCR: WTWKF Mask */ + +#define WDT_WTCR_WTWKE_Pos 4 /*!< WDT_T::WTCR: WTWKE Position */ +#define WDT_WTCR_WTWKE_Msk (1ul << WDT_WTCR_WTWKE_Pos) /*!< WDT_T::WTCR: WTWKE Mask */ + +#define WDT_WTCR_WTIF_Pos 3 /*!< WDT_T::WTCR: WTIF Position */ +#define WDT_WTCR_WTIF_Msk (1ul << WDT_WTCR_WTIF_Pos) /*!< WDT_T::WTCR: WTIF Mask */ + +#define WDT_WTCR_WTRF_Pos 2 /*!< WDT_T::WTCR: WTRF Position */ +#define WDT_WTCR_WTRF_Msk (1ul << WDT_WTCR_WTRF_Pos) /*!< WDT_T::WTCR: WTRF Mask */ + +#define WDT_WTCR_WTRE_Pos 1 /*!< WDT_T::WTCR: WTRE Position */ +#define WDT_WTCR_WTRE_Msk (1ul << WDT_WTCR_WTRE_Pos) /*!< WDT_T::WTCR: WTRE Mask */ + +#define WDT_WTCR_WTR_Pos 0 /*!< WDT_T::WTCR: WTR Position */ +#define WDT_WTCR_WTR_Msk (1ul << WDT_WTCR_WTR_Pos) /*!< WDT_T::WTCR: WTR Mask */ + +/* WDT WTCRALT Bit Field Definitions */ +#define WDT_WTCRALT_WTRDSEL_Pos 0 /*!< WDT_T::WTCRALT: WTRDSEL Position */ +#define WDT_WTCRALT_WTRDSEL_Msk (0x3ul << WDT_WTCRALT_WTRDSEL_Pos) /*!< WDT_T::WTCRALT: WTRDSEL Mask */ +/*@}*/ /* end of group REG_WDT_BITMASK */ +/*@}*/ /* end of group REG_WDT */ + + +/*----------------------------- Window Watchdog Timer (WWDT) -----------------------------*/ +/** @addtogroup REG_WWDT Window Watchdog Timer (WWDT) + Memory Mapped Structure for Window Watchdog Timer + @{ + */ + +typedef struct +{ + + +/** + * @var WWDT_T::WWDTRLD + * Offset: 0x00 Window Watchdog Timer Reload Counter Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[31:0] |WWDTRLD |WWDT Reload Counter Register + * | | |Writing 0x00005AA5 to this register will reload the WWDT counter value to 0x3F. + * | | |Note: User can only write WWDTRLD to reload WWDT counter value when current WWDT + * | | | counter value between 0 and WINCMP. If user writes WWDTRLD when current WWDT + * | | | counter value is larger than WINCMP, WWDT reset signal will generate immediately. + * @var WWDT_T::WWDTCR + * Offset: 0x04 Window Watchdog Timer Control Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |WWDTEN |WWDT Enable Control + * | | |0 = WWDT counter is stopped. + * | | |1 = WWDT counter is starting counting. + * |[1] |WWDTIE |WWDT Interrupt Enable Control + * | | |If this bit is enabled, the WWDT counter compare match interrupt signal is generated and inform to CPU. + * | | |0 = WWDT counter compare match interrupt Disabled. + * | | |1 = WWDT counter compare match interrupt Enabled. + * |[11:8] |PERIODSEL |WWDT Counter Prescale Period Selection + * | | |0000 = Pre-scale is 1; Max time-out period is 1 * 64 * TWWDT. + * | | |0001 = Pre-scale is 2; Max time-out period is 2 * 64 * TWWDT. + * | | |0010 = Pre-scale is 4; Max time-out period is 4 * 64 * TWWDT. + * | | |0011 = Pre-scale is 8; Max time-out period is 8 * 64 * TWWDT. + * | | |0100 = Pre-scale is 16; Max time-out period is 16 * 64 * TWWDT. + * | | |0101 = Pre-scale is 32; Max time-out period is 32 * 64 * TWWDT. + * | | |0110 = Pre-scale is 64; Max time-out period is 64 * 64 * TWWDT. + * | | |0111 = Pre-scale is 128; Max time-out period is 128 * 64 * TWWDT. + * | | |1000 = Pre-scale is 192; Max time-out period is 192 * 64 * TWWDT. + * | | |1001 = Pre-scale is 256; Max time-out period is 256 * 64 * TWWDT. + * | | |1010 = Pre-scale is 384; Max time-out period is 384 * 64 * TWWDT. + * | | |1011 = Pre-scale is 512; Max time-out period is 512 * 64 * TWWDT. + * | | |1100 = Pre-scale is 768; Max time-out period is 768 * 64 * TWWDT. + * | | |1101 = Pre-scale is 1024; Max time-out period is 1024 * 64 * TWWDT. + * | | |1110 = Pre-scale is 1536; Max time-out period is 1536 * 64 * TWWDT. + * | | |1111 = Pre-scale is 2048; Max time-out period is 2048 * 64 * TWWDT. + * |[21:16] |WINCMP |WWDT Window Compare Register + * | | |Set this register to adjust the valid reload window. + * | | |Note: User can only write WWDTRLD to reload WWDT counter value when current WWDT counter value between 0 and WINCMP. + * | | |If user writes WWDTRLD when current WWDT counter value larger than WINCMP, WWDT reset signal will generate immediately. + * |[31] |DBGACK_WWDT|ICE Debug Mode Acknowledge Disable Control + * | | |0 = ICE debug mode acknowledgment effects WWDT counting. + * | | |WWDT down counter will be held while CPU is held by ICE. + * | | |1 = ICE debug mode acknowledgment Disabled. + * | | |WWDT down counter will keep going no matter CPU is held by ICE or not. + * @var WWDT_T::WWDTSR + * Offset: 0x08 Window Watchdog Timer Status Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[0] |WWDTIF |WWDT Compare Match Interrupt Flag + * | | |This bit indicates the interrupt flag status of WWDT while WWDT counter value matches WINCMP value. + * | | |0 = No effect. + * | | |1 = WWDT counter value matches WINCMP value. + * | | |Note: This bit is cleared by writing 1 to it. + * |[1] |WWDTRF |WWDT Time-out Reset Flag + * | | |This bit indicates the system has been reset by WWDT time-out reset or not. + * | | |0 = WWDT time-out reset did not occur. + * | | |1 = WWDT time-out reset occurred. + * | | |Note: This bit is cleared by writing 1 to it. + * @var WWDT_T::WWDTCVR + * Offset: 0x0C Window Watchdog Timer Counter Value Register + * --------------------------------------------------------------------------------------------------- + * |Bits |Field |Descriptions + * | :----: | :----: | :---- | + * |[5:0] |WWDTCVAL |WWDT Counter Value + * | | |WWDTCVAL will be updated continuously to monitor 6-bit down counter value. + */ + + __IO uint32_t WWDTRLD; /* Offset: 0x00 Window Watchdog Timer Reload Counter Register */ + __IO uint32_t WWDTCR; /* Offset: 0x04 Window Watchdog Timer Control Register */ + __IO uint32_t WWDTSR; /* Offset: 0x08 Window Watchdog Timer Status Register */ + __I uint32_t WWDTCVR; /* Offset: 0x0C Window Watchdog Timer Counter Value Register */ + +} WWDT_T; + + + +/** @addtogroup REG_WWDT_BITMASK WWDT Bit Mask + @{ + */ + +/* WWDT WWDTRLD Bit Field Definitions */ +#define WWDT_WWDTRLD_WWDTRLD_Pos 0 /*!< WWDT_T::WWDTRLD: WWDTRLD Position */ +#define WWDT_WWDTRLD_WWDTRLD_Msk (0xFFFFFFFFul << WWDT_WWDTRLD_WWDTRLD_Pos) /*!< WWDT_T::WWDTRLD: WWDTRLD Mask */ + +/* WWDT WWDTCR Bit Field Definitions */ +#define WWDT_WWDTCR_DBGACK_WWDT_Pos 31 /*!< WWDT_T::WWDTCR: DBGACK_WWDT Position */ +#define WWDT_WWDTCR_DBGACK_WWDT_Msk (1ul << WWDT_WWDTCR_DBGACK_WWDT_Pos) /*!< WWDT_T::WWDTCR: DBGACK_WWDT Mask */ + +#define WWDT_WWDTCR_WINCMP_Pos 16 /*!< WWDT_T::WWDTCR: WINCMP Position */ +#define WWDT_WWDTCR_WINCMP_Msk (0x3Ful << WWDT_WWDTCR_WINCMP_Pos) /*!< WWDT_T::WWDTCR: WINCMP Mask */ + +#define WWDT_WWDTCR_PERIODSEL_Pos 8 /*!< WWDT_T::WWDTCR: PERIODSEL Position */ +#define WWDT_WWDTCR_PERIODSEL_Msk (0xFul << WWDT_WWDTCR_PERIODSEL_Pos) /*!< WWDT_T::WWDTCR: PERIODSEL Mask */ + +#define WWDT_WWDTCR_WWDTIE_Pos 1 /*!< WWDT_T::WWDTCR: WWDTIE Position */ +#define WWDT_WWDTCR_WWDTIE_Msk (1ul << WWDT_WWDTCR_WWDTIE_Pos) /*!< WWDT_T::WWDTCR: WWDTIE Mask */ + +#define WWDT_WWDTCR_WWDTEN_Pos 0 /*!< WWDT_T::WWDTCR: WWDTEN Position */ +#define WWDT_WWDTCR_WWDTEN_Msk (1ul << WWDT_WWDTCR_WWDTEN_Pos) /*!< WWDT_T::WWDTCR: WWDTEN Mask */ + +/* WWDT WWDTSR Bit Field Definitions */ +#define WWDT_WWDTSR_WWDTRF_Pos 1 /*!< WWDT_T::WWDTSR: WWDTRF Position */ +#define WWDT_WWDTSR_WWDTRF_Msk (1ul << WWDT_WWDTSR_WWDTRF_Pos) /*!< WWDT_T::WWDTSR: WWDTRF Mask */ + +#define WWDT_WWDTSR_WWDTIF_Pos 0 /*!< WWDT_T::WWDTSR: WWDTIF Position */ +#define WWDT_WWDTSR_WWDTIF_Msk (1ul << WWDT_WWDTSR_WWDTIF_Pos) /*!< WWDT_T::WWDTSR: WWDTIF Mask */ + +/* WWDT WWDTCVR Bit Field Definitions */ +#define WWDT_WWDTCVR_WWDTCVAL_Pos 0 /*!< WWDT_T::WWDTCVR: WWDTRF Position */ +#define WWDT_WWDTCVR_WWDTCVAL_Msk (0x3Ful << WWDT_WWDTCVR_WWDTCVAL_Pos) /*!< WWDT_T::WWDTCVR: WWDTRF Mask */ +/*@}*/ /* end of group REG_WWDT_BITMASK */ +/*@}*/ /* end of group REG_WWDT */ +/*@}*/ /* end of group REGISTER */ + + +/******************************************************************************/ +/* Peripheral memory map */ +/******************************************************************************/ +/** @addtogroup PERIPHERAL_MEM_MAP Peripheral Memory Map + Memory Mapped Structure for Series Peripheral + @{ + */ +/* Peripheral and SRAM base address */ +#define FLASH_BASE (( uint32_t)0x00000000) +#define SRAM_BASE (( uint32_t)0x20000000) +#define AHB_BASE (( uint32_t)0x50000000) +#define APB1_BASE (( uint32_t)0x40000000) +#define APB2_BASE (( uint32_t)0x40100000) + +/* Peripheral memory map */ +#define GPIO_BASE (AHB_BASE + 0x4000) /*!< GPIO Base Address */ +#define PA_BASE (GPIO_BASE ) /*!< GPIO PORTA Base Address */ +#define PB_BASE (GPIO_BASE + 0x0040) /*!< GPIO PORTB Base Address */ +#define PC_BASE (GPIO_BASE + 0x0080) /*!< GPIO PORTC Base Address */ +#define PD_BASE (GPIO_BASE + 0x00C0) /*!< GPIO PORTD Base Address */ +#define PE_BASE (GPIO_BASE + 0x0100) /*!< GPIO PORTE Base Address */ +#define PF_BASE (GPIO_BASE + 0x0140) /*!< GPIO PORTF Base Address */ +#define GPIO_DBNCECON_BASE (GPIO_BASE + 0x0180) /*!< GPIO De-bounce Cycle Control Base Address */ +#define GPIO_PIN_DATA_BASE (GPIO_BASE + 0x0200) /*!< GPIO Pin Data Input/Output Control Base Address */ + + +#define UART0_BASE (APB1_BASE + 0x50000) +#define UART1_BASE (APB2_BASE + 0x50000) +#define UART2_BASE (APB2_BASE + 0x54000) + + +#define TIMER0_BASE (APB1_BASE + 0x10000) /*!< Timer0 Base Address */ +#define TIMER1_BASE (APB1_BASE + 0x10020) /*!< Timer1 Base Address */ +#define TIMER2_BASE (APB2_BASE + 0x10000) /*!< Timer2 Base Address */ +#define TIMER3_BASE (APB2_BASE + 0x10020) /*!< Timer3 Base Address */ + +#define WDT_BASE (APB1_BASE + 0x4000) /*!< Watchdog Timer Base Address */ + +#define WWDT_BASE (APB1_BASE + 0x4100) /*!< Window Watchdog Timer Base Address */ + +#define SPI0_BASE (APB1_BASE + 0x30000) /*!< SPI0 Base Address */ +#define SPI1_BASE (APB1_BASE + 0x34000) /*!< SPI1 Base Address */ +#define SPI2_BASE (APB2_BASE + 0x30000) /*!< SPI2 Base Address */ + +#define I2C0_BASE (APB1_BASE + 0x20000) /*!< I2C0 Base Address */ +#define I2C1_BASE (APB2_BASE + 0x20000) /*!< I2C1 Base Address */ + +#define ADC_BASE (APB1_BASE + 0xE0000) /*!< ADC Base Address */ + +#define CLK_BASE (AHB_BASE + 0x00200) /*!< System Clock Controller Base Address */ + +#define GCR_BASE (AHB_BASE + 0x00000) /*!< System Global Controller Base Address */ + +#define INT_BASE (AHB_BASE + 0x00300) /*!< Interrupt Source Controller Base Address */ + +#define FMC_BASE (AHB_BASE + 0x0C000) + +#define PS2_BASE (APB2_BASE + 0x00000) /*!< PS/2 Base Address */ + +#define USBD_BASE (APB1_BASE + 0x60000) /*!< USBD Base Address */ + +#define PDMA0_BASE (AHB_BASE + 0x08000) /*!< PDMA0 Base Address */ +#define PDMA1_BASE (AHB_BASE + 0x08100) /*!< PDMA1 Base Address */ +#define PDMA2_BASE (AHB_BASE + 0x08200) /*!< PDMA2 Base Address */ +#define PDMA3_BASE (AHB_BASE + 0x08300) /*!< PDMA3 Base Address */ +#define PDMA4_BASE (AHB_BASE + 0x08400) /*!< PDMA4 Base Address */ +#define PDMA5_BASE (AHB_BASE + 0x08500) /*!< PDMA5 Base Address */ + +#define PDMA_GCR_BASE (AHB_BASE + 0x08F00) /*!< PDMA Global Base Address */ + +#define CRC_BASE (AHB_BASE + 0x08E00) /*!< CRC Base Address */ + +#define PWMA_BASE (APB1_BASE + 0x40000) /*!< PWMA Base Address */ + +#define I2S_BASE (APB2_BASE + 0xA0000) /*!< I2S Base Address */ + +/*@}*/ /* end of group PERIPHERAL_MEM_MAP */ + +/******************************************************************************/ +/* Peripheral Definitions */ +/******************************************************************************/ + +/** @addtogroup PERIPHERAL Peripheral Definitions + The Definitions of Peripheral + @{ + */ +#define PA ((GPIO_T *) PA_BASE) /*!< GPIO PORTA Configuration Struct */ +#define PB ((GPIO_T *) PB_BASE) /*!< GPIO PORTB Configuration Struct */ +#define PC ((GPIO_T *) PC_BASE) /*!< GPIO PORTC Configuration Struct */ +#define PD ((GPIO_T *) PD_BASE) /*!< GPIO PORTD Configuration Struct */ +#define PF ((GPIO_T *) PF_BASE) /*!< GPIO PORTF Configuration Struct */ +#define GPIO ((GPIO_DBNCECON_T *) GPIO_DBNCECON_BASE) /*!< Interrupt De-bounce Cycle Control Configuration Struct */ + +#define UART0 ((UART_T *) UART0_BASE) /*!< UART0 Configuration Struct */ +#define UART1 ((UART_T *) UART1_BASE) /*!< UART1 Configuration Struct */ + +#define TIMER0 ((TIMER_T *) TIMER0_BASE) /*!< Timer0 Configuration Struct */ +#define TIMER1 ((TIMER_T *) TIMER1_BASE) /*!< Timer1 Configuration Struct */ +#define TIMER2 ((TIMER_T *) TIMER2_BASE) /*!< Timer2 Configuration Struct */ +#define TIMER3 ((TIMER_T *) TIMER3_BASE) /*!< Timer3 Configuration Struct */ + +#define WDT ((WDT_T *) WDT_BASE) /*!< Watchdog Timer Configuration Struct */ + +#define WWDT ((WWDT_T *) WWDT_BASE) /*!< Window Watchdog Timer Configuration Struct */ + +#define SPI0 ((SPI_T *) SPI0_BASE) /*!< SPI0 Configuration Struct */ +#define SPI1 ((SPI_T *) SPI1_BASE) /*!< SPI1 Configuration Struct */ +#define SPI2 ((SPI_T *) SPI2_BASE) /*!< SPI2 Configuration Struct */ + +#define I2C0 ((I2C_T *) I2C0_BASE) /*!< I2C0 Configuration Struct */ +#define I2C1 ((I2C_T *) I2C1_BASE) /*!< I2C1 Configuration Struct */ + +#define I2S ((I2S_T *) I2S_BASE) /*!< I2S Configuration Struct */ + +#define ADC ((ADC_T *) ADC_BASE) /*!< ADC Configuration Struct */ + +#define CLK ((CLK_T *) CLK_BASE) /*!< System Clock Controller Configuration Struct */ + +#define SYS ((GCR_T *) GCR_BASE) /*!< System Global Controller Configuration Struct */ + +#define SYSINT ((GCR_INT_T *) INT_BASE) /*!< Interrupt Source Controller Configuration Struct */ + +#define FMC ((FMC_T *) FMC_BASE) + +#define PS2 ((PS2_T *) PS2_BASE) /*!< PS/2 Configuration Struct */ + +#define USBD ((USBD_T *) USBD_BASE) /*!< USBD Configuration Struct */ + +#define PDMA0 ((PDMA_T *) PDMA0_BASE) /*!< PDMA0 Configuration Struct */ +#define PDMA1 ((PDMA_T *) PDMA1_BASE) /*!< PDMA1 Configuration Struct */ +#define PDMA2 ((PDMA_T *) PDMA2_BASE) /*!< PDMA2 Configuration Struct */ +#define PDMA3 ((PDMA_T *) PDMA3_BASE) /*!< PDMA3 Configuration Struct */ +#define PDMA4 ((PDMA_T *) PDMA4_BASE) /*!< PDMA4 Configuration Struct */ +#define PDMA5 ((PDMA_T *) PDMA5_BASE) /*!< PDMA5 Configuration Struct */ + +#define PDMA_GCR ((PDMA_GCR_T *) PDMA_GCR_BASE) /*!< PDMA Global Configuration Struct */ + +#define CRC ((CRC_T *) CRC_BASE) /*!< CRC Configuration Struct */ + +#define PWMA ((PWM_T *) PWMA_BASE) /*!< PWMA Configuration Struct */ + +/*@}*/ /* end of group PERIPHERAL */ + +#define UNLOCKREG() do{*((__IO uint32_t *)(GCR_BASE + 0x100)) = 0x59;*((__IO uint32_t *)(GCR_BASE + 0x100)) = 0x16;*((__IO uint32_t *)(GCR_BASE + 0x100)) = 0x88;}while(*((__IO uint32_t *)(GCR_BASE + 0x100))==0) +#define LOCKREG() *((__IO uint32_t *)(GCR_BASE + 0x100)) = 0x00 + +#define REGCOPY(dest, src) *((uint32_t *)&(dest)) = *((uint32_t *)&(src)) +#define CLEAR(dest) *((uint32_t *)&(dest)) = 0 + +//============================================================================= +/** @addtogroup IO_ROUTINE I/O routines + The Declaration of I/O routines + @{ + */ + +typedef volatile unsigned char vu8; ///< Define 8-bit unsigned volatile data type +typedef volatile unsigned short vu16; ///< Define 16-bit unsigned volatile data type +typedef volatile unsigned long vu32; ///< Define 32-bit unsigned volatile data type + +/** + * @brief Get a 8-bit unsigned value from specified address + * @param[in] addr Address to get 8-bit data from + * @return 8-bit unsigned value stored in specified address + */ +#define M8(addr) (*((vu8 *) (addr))) + +/** + * @brief Get a 16-bit unsigned value from specified address + * @param[in] addr Address to get 16-bit data from + * @return 16-bit unsigned value stored in specified address + * @note The input address must be 16-bit aligned + */ +#define M16(addr) (*((vu16 *) (addr))) + +/** + * @brief Get a 32-bit unsigned value from specified address + * @param[in] addr Address to get 32-bit data from + * @return 32-bit unsigned value stored in specified address + * @note The input address must be 32-bit aligned + */ +#define M32(addr) (*((vu32 *) (addr))) + +/** + * @brief Set a 32-bit unsigned value to specified I/O port + * @param[in] port Port address to set 32-bit data + * @param[in] value Value to write to I/O port + * @return None + * @note The output port must be 32-bit aligned + */ +#define outpw(port,value) *((volatile unsigned int *)(port)) = (value) + +/** + * @brief Get a 32-bit unsigned value from specified I/O port + * @param[in] port Port address to get 32-bit data from + * @return 32-bit unsigned value stored in specified I/O port + * @note The input port must be 32-bit aligned + */ +#define inpw(port) (*((volatile unsigned int *)(port))) + +/** + * @brief Set a 16-bit unsigned value to specified I/O port + * @param[in] port Port address to set 16-bit data + * @param[in] value Value to write to I/O port + * @return None + * @note The output port must be 16-bit aligned + */ +#define outps(port,value) *((volatile unsigned short *)(port)) = (value) + +/** + * @brief Get a 16-bit unsigned value from specified I/O port + * @param[in] port Port address to get 16-bit data from + * @return 16-bit unsigned value stored in specified I/O port + * @note The input port must be 16-bit aligned + */ +#define inps(port) (*((volatile unsigned short *)(port))) + +/** + * @brief Set a 8-bit unsigned value to specified I/O port + * @param[in] port Port address to set 8-bit data + * @param[in] value Value to write to I/O port + * @return None + */ +#define outpb(port,value) *((volatile unsigned char *)(port)) = (value) + +/** + * @brief Get a 8-bit unsigned value from specified I/O port + * @param[in] port Port address to get 8-bit data from + * @return 8-bit unsigned value stored in specified I/O port + */ +#define inpb(port) (*((volatile unsigned char *)(port))) + +/** + * @brief Set a 32-bit unsigned value to specified I/O port + * @param[in] port Port address to set 32-bit data + * @param[in] value Value to write to I/O port + * @return None + * @note The output port must be 32-bit aligned + */ +#define outp32(port,value) *((volatile unsigned int *)(port)) = (value) + +/** + * @brief Get a 32-bit unsigned value from specified I/O port + * @param[in] port Port address to get 32-bit data from + * @return 32-bit unsigned value stored in specified I/O port + * @note The input port must be 32-bit aligned + */ +#define inp32(port) (*((volatile unsigned int *)(port))) + +/** + * @brief Set a 16-bit unsigned value to specified I/O port + * @param[in] port Port address to set 16-bit data + * @param[in] value Value to write to I/O port + * @return None + * @note The output port must be 16-bit aligned + */ +#define outp16(port,value) *((volatile unsigned short *)(port)) = (value) + +/** + * @brief Get a 16-bit unsigned value from specified I/O port + * @param[in] port Port address to get 16-bit data from + * @return 16-bit unsigned value stored in specified I/O port + * @note The input port must be 16-bit aligned + */ +#define inp16(port) (*((volatile unsigned short *)(port))) + +/** + * @brief Set a 8-bit unsigned value to specified I/O port + * @param[in] port Port address to set 8-bit data + * @param[in] value Value to write to I/O port + * @return None + */ +#define outp8(port,value) *((volatile unsigned char *)(port)) = (value) + +/** + * @brief Get a 8-bit unsigned value from specified I/O port + * @param[in] port Port address to get 8-bit data from + * @return 8-bit unsigned value stored in specified I/O port + */ +#define inp8(port) (*((volatile unsigned char *)(port))) + +/*@}*/ /* end of group IO_ROUTINE */ + + + + +/** @addtogroup legacy_Constants Legacy Constants + Legacy Constants + @{ +*/ + + +#define E_SUCCESS 0 +#ifndef NULL +#define NULL 0 +#endif + +#define TRUE 1 +#define FALSE 0 + +#define ENABLE 1 +#define DISABLE 0 + +/* Define one bit mask */ +#define BIT0 0x00000001 +#define BIT1 0x00000002 +#define BIT2 0x00000004 +#define BIT3 0x00000008 +#define BIT4 0x00000010 +#define BIT5 0x00000020 +#define BIT6 0x00000040 +#define BIT7 0x00000080 +#define BIT8 0x00000100 +#define BIT9 0x00000200 +#define BIT10 0x00000400 +#define BIT11 0x00000800 +#define BIT12 0x00001000 +#define BIT13 0x00002000 +#define BIT14 0x00004000 +#define BIT15 0x00008000 +#define BIT16 0x00010000 +#define BIT17 0x00020000 +#define BIT18 0x00040000 +#define BIT19 0x00080000 +#define BIT20 0x00100000 +#define BIT21 0x00200000 +#define BIT22 0x00400000 +#define BIT23 0x00800000 +#define BIT24 0x01000000 +#define BIT25 0x02000000 +#define BIT26 0x04000000 +#define BIT27 0x08000000 +#define BIT28 0x10000000 +#define BIT29 0x20000000 +#define BIT30 0x40000000 +#define BIT31 0x80000000 + +/* Byte Mask Definitions */ +#define BYTE0_Msk (0x000000FF) +#define BYTE1_Msk (0x0000FF00) +#define BYTE2_Msk (0x00FF0000) +#define BYTE3_Msk (0xFF000000) + +#define _GET_BYTE0(u32Param) (((u32Param) & BYTE0_Msk) ) /*!< Extract Byte 0 (Bit 0~ 7) from parameter u32Param */ +#define _GET_BYTE1(u32Param) (((u32Param) & BYTE1_Msk) >> 8) /*!< Extract Byte 1 (Bit 8~15) from parameter u32Param */ +#define _GET_BYTE2(u32Param) (((u32Param) & BYTE2_Msk) >> 16) /*!< Extract Byte 2 (Bit 16~23) from parameter u32Param */ +#define _GET_BYTE3(u32Param) (((u32Param) & BYTE3_Msk) >> 24) /*!< Extract Byte 3 (Bit 24~31) from parameter u32Param */ + +/*@}*/ /* end of group legacy_Constants */ + + +/******************************************************************************/ +/* Peripheral header files */ +/******************************************************************************/ +#include "sys.h" +#include "adc.h" +#include "fmc.h" +#include "gpio.h" +#include "i2c.h" +#include "pwm.h" +#include "spi.h" +#include "timer.h" +#include "wdt.h" +#include "wwdt.h" +#include "uart.h" +#include "i2s.h" +#include "usbd.h" +#include "pdma.h" +#include "ps2.h" +#include "clk.h" +#include "crc.h" +#endif + + + diff --git a/NUC123/inc/cmsis_gcc.h b/NUC123/inc/cmsis_gcc.h new file mode 100644 index 0000000..bb89fbb --- /dev/null +++ b/NUC123/inc/cmsis_gcc.h @@ -0,0 +1,1373 @@ +/**************************************************************************//** + * @file cmsis_gcc.h + * @brief CMSIS Cortex-M Core Function/Instruction Header File + * @version V4.30 + * @date 20. October 2015 + ******************************************************************************/ +/* Copyright (c) 2009 - 2015 ARM LIMITED + + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + - Neither the name of ARM nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + * + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + ---------------------------------------------------------------------------*/ + + +#ifndef __CMSIS_GCC_H +#define __CMSIS_GCC_H + +/* ignore some GCC warnings */ +#if defined ( __GNUC__ ) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wsign-conversion" +#pragma GCC diagnostic ignored "-Wconversion" +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + + +/* ########################### Core Function Access ########################### */ +/** \ingroup CMSIS_Core_FunctionInterface + \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions + @{ + */ + +/** + \brief Enable IRQ Interrupts + \details Enables IRQ interrupts by clearing the I-bit in the CPSR. + Can only be executed in Privileged modes. + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __enable_irq(void) +{ + __ASM volatile ("cpsie i" : : : "memory"); +} + + +/** + \brief Disable IRQ Interrupts + \details Disables IRQ interrupts by setting the I-bit in the CPSR. + Can only be executed in Privileged modes. + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __disable_irq(void) +{ + __ASM volatile ("cpsid i" : : : "memory"); +} + + +/** + \brief Get Control Register + \details Returns the content of the Control Register. + \return Control Register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_CONTROL(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, control" : "=r" (result) ); + return(result); +} + + +/** + \brief Set Control Register + \details Writes the given value to the Control Register. + \param [in] control Control Register value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_CONTROL(uint32_t control) +{ + __ASM volatile ("MSR control, %0" : : "r" (control) : "memory"); +} + + +/** + \brief Get IPSR Register + \details Returns the content of the IPSR Register. + \return IPSR Register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_IPSR(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, ipsr" : "=r" (result) ); + return(result); +} + + +/** + \brief Get APSR Register + \details Returns the content of the APSR Register. + \return APSR Register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_APSR(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, apsr" : "=r" (result) ); + return(result); +} + + +/** + \brief Get xPSR Register + \details Returns the content of the xPSR Register. + + \return xPSR Register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_xPSR(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, xpsr" : "=r" (result) ); + return(result); +} + + +/** + \brief Get Process Stack Pointer + \details Returns the current value of the Process Stack Pointer (PSP). + \return PSP Register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PSP(void) +{ + register uint32_t result; + + __ASM volatile ("MRS %0, psp\n" : "=r" (result) ); + return(result); +} + + +/** + \brief Set Process Stack Pointer + \details Assigns the given value to the Process Stack Pointer (PSP). + \param [in] topOfProcStack Process Stack Pointer value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_PSP(uint32_t topOfProcStack) +{ + __ASM volatile ("MSR psp, %0\n" : : "r" (topOfProcStack) : "sp"); +} + + +/** + \brief Get Main Stack Pointer + \details Returns the current value of the Main Stack Pointer (MSP). + \return MSP Register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_MSP(void) +{ + register uint32_t result; + + __ASM volatile ("MRS %0, msp\n" : "=r" (result) ); + return(result); +} + + +/** + \brief Set Main Stack Pointer + \details Assigns the given value to the Main Stack Pointer (MSP). + + \param [in] topOfMainStack Main Stack Pointer value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_MSP(uint32_t topOfMainStack) +{ + __ASM volatile ("MSR msp, %0\n" : : "r" (topOfMainStack) : "sp"); +} + + +/** + \brief Get Priority Mask + \details Returns the current state of the priority mask bit from the Priority Mask Register. + \return Priority Mask value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_PRIMASK(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, primask" : "=r" (result) ); + return(result); +} + + +/** + \brief Set Priority Mask + \details Assigns the given value to the Priority Mask Register. + \param [in] priMask Priority Mask + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_PRIMASK(uint32_t priMask) +{ + __ASM volatile ("MSR primask, %0" : : "r" (priMask) : "memory"); +} + + +#if (__CORTEX_M >= 0x03U) + +/** + \brief Enable FIQ + \details Enables FIQ interrupts by clearing the F-bit in the CPSR. + Can only be executed in Privileged modes. + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __enable_fault_irq(void) +{ + __ASM volatile ("cpsie f" : : : "memory"); +} + + +/** + \brief Disable FIQ + \details Disables FIQ interrupts by setting the F-bit in the CPSR. + Can only be executed in Privileged modes. + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __disable_fault_irq(void) +{ + __ASM volatile ("cpsid f" : : : "memory"); +} + + +/** + \brief Get Base Priority + \details Returns the current value of the Base Priority register. + \return Base Priority register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_BASEPRI(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, basepri" : "=r" (result) ); + return(result); +} + + +/** + \brief Set Base Priority + \details Assigns the given value to the Base Priority register. + \param [in] basePri Base Priority value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_BASEPRI(uint32_t value) +{ + __ASM volatile ("MSR basepri, %0" : : "r" (value) : "memory"); +} + + +/** + \brief Set Base Priority with condition + \details Assigns the given value to the Base Priority register only if BASEPRI masking is disabled, + or the new value increases the BASEPRI priority level. + \param [in] basePri Base Priority value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_BASEPRI_MAX(uint32_t value) +{ + __ASM volatile ("MSR basepri_max, %0" : : "r" (value) : "memory"); +} + + +/** + \brief Get Fault Mask + \details Returns the current value of the Fault Mask register. + \return Fault Mask register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_FAULTMASK(void) +{ + uint32_t result; + + __ASM volatile ("MRS %0, faultmask" : "=r" (result) ); + return(result); +} + + +/** + \brief Set Fault Mask + \details Assigns the given value to the Fault Mask register. + \param [in] faultMask Fault Mask value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_FAULTMASK(uint32_t faultMask) +{ + __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) : "memory"); +} + +#endif /* (__CORTEX_M >= 0x03U) */ + + +#if (__CORTEX_M == 0x04U) || (__CORTEX_M == 0x07U) + +/** + \brief Get FPSCR + \details Returns the current value of the Floating Point Status/Control register. + \return Floating Point Status/Control register value + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __get_FPSCR(void) +{ +#if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U) + uint32_t result; + + /* Empty asm statement works as a scheduling barrier */ + __ASM volatile (""); + __ASM volatile ("VMRS %0, fpscr" : "=r" (result) ); + __ASM volatile (""); + return(result); +#else + return(0); +#endif +} + + +/** + \brief Set FPSCR + \details Assigns the given value to the Floating Point Status/Control register. + \param [in] fpscr Floating Point Status/Control value to set + */ +__attribute__( ( always_inline ) ) __STATIC_INLINE void __set_FPSCR(uint32_t fpscr) +{ +#if (__FPU_PRESENT == 1U) && (__FPU_USED == 1U) + /* Empty asm statement works as a scheduling barrier */ + __ASM volatile (""); + __ASM volatile ("VMSR fpscr, %0" : : "r" (fpscr) : "vfpcc"); + __ASM volatile (""); +#endif +} + +#endif /* (__CORTEX_M == 0x04U) || (__CORTEX_M == 0x07U) */ + + + +/*@} end of CMSIS_Core_RegAccFunctions */ + + +/* ########################## Core Instruction Access ######################### */ +/** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface + Access to dedicated instructions + @{ +*/ + +/* Define macros for porting to both thumb1 and thumb2. + * For thumb1, use low register (r0-r7), specified by constraint "l" + * Otherwise, use general registers, specified by constraint "r" */ +#if defined (__thumb__) && !defined (__thumb2__) +#define __CMSIS_GCC_OUT_REG(r) "=l" (r) +#define __CMSIS_GCC_USE_REG(r) "l" (r) +#else +#define __CMSIS_GCC_OUT_REG(r) "=r" (r) +#define __CMSIS_GCC_USE_REG(r) "r" (r) +#endif + +/** + \brief No Operation + \details No Operation does nothing. This instruction can be used for code alignment purposes. + */ +__attribute__((always_inline)) __STATIC_INLINE void __NOP(void) +{ + __ASM volatile ("nop"); +} + + +/** + \brief Wait For Interrupt + \details Wait For Interrupt is a hint instruction that suspends execution until one of a number of events occurs. + */ +__attribute__((always_inline)) __STATIC_INLINE void __WFI(void) +{ + __ASM volatile ("wfi"); +} + + +/** + \brief Wait For Event + \details Wait For Event is a hint instruction that permits the processor to enter + a low-power state until one of a number of events occurs. + */ +__attribute__((always_inline)) __STATIC_INLINE void __WFE(void) +{ + __ASM volatile ("wfe"); +} + + +/** + \brief Send Event + \details Send Event is a hint instruction. It causes an event to be signaled to the CPU. + */ +__attribute__((always_inline)) __STATIC_INLINE void __SEV(void) +{ + __ASM volatile ("sev"); +} + + +/** + \brief Instruction Synchronization Barrier + \details Instruction Synchronization Barrier flushes the pipeline in the processor, + so that all instructions following the ISB are fetched from cache or memory, + after the instruction has been completed. + */ +__attribute__((always_inline)) __STATIC_INLINE void __ISB(void) +{ + __ASM volatile ("isb 0xF":::"memory"); +} + + +/** + \brief Data Synchronization Barrier + \details Acts as a special kind of Data Memory Barrier. + It completes when all explicit memory accesses before this instruction complete. + */ +__attribute__((always_inline)) __STATIC_INLINE void __DSB(void) +{ + __ASM volatile ("dsb 0xF":::"memory"); +} + + +/** + \brief Data Memory Barrier + \details Ensures the apparent order of the explicit memory operations before + and after the instruction, without ensuring their completion. + */ +__attribute__((always_inline)) __STATIC_INLINE void __DMB(void) +{ + __ASM volatile ("dmb 0xF":::"memory"); +} + + +/** + \brief Reverse byte order (32 bit) + \details Reverses the byte order in integer value. + \param [in] value Value to reverse + \return Reversed value + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __REV(uint32_t value) +{ +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) + return __builtin_bswap32(value); +#else + uint32_t result; + + __ASM volatile ("rev %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) ); + return(result); +#endif +} + + +/** + \brief Reverse byte order (16 bit) + \details Reverses the byte order in two unsigned short values. + \param [in] value Value to reverse + \return Reversed value + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __REV16(uint32_t value) +{ + uint32_t result; + + __ASM volatile ("rev16 %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) ); + return(result); +} + + +/** + \brief Reverse byte order in signed short value + \details Reverses the byte order in a signed short value with sign extension to integer. + \param [in] value Value to reverse + \return Reversed value + */ +__attribute__((always_inline)) __STATIC_INLINE int32_t __REVSH(int32_t value) +{ +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + return (short)__builtin_bswap16(value); +#else + int32_t result; + + __ASM volatile ("revsh %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) ); + return(result); +#endif +} + + +/** + \brief Rotate Right in unsigned value (32 bit) + \details Rotate Right (immediate) provides the value of the contents of a register rotated by a variable number of bits. + \param [in] value Value to rotate + \param [in] value Number of Bits to rotate + \return Rotated value + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __ROR(uint32_t op1, uint32_t op2) +{ + return (op1 >> op2) | (op1 << (32U - op2)); +} + + +/** + \brief Breakpoint + \details Causes the processor to enter Debug state. + Debug tools can use this to investigate system state when the instruction at a particular address is reached. + \param [in] value is ignored by the processor. + If required, a debugger can use it to store additional information about the breakpoint. + */ +#define __BKPT(value) __ASM volatile ("bkpt "#value) + + +/** + \brief Reverse bit order of value + \details Reverses the bit order of the given value. + \param [in] value Value to reverse + \return Reversed value + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __RBIT(uint32_t value) +{ + uint32_t result; + +#if (__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U) + __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) ); +#else + int32_t s = 4 /*sizeof(v)*/ * 8 - 1; /* extra shift needed at end */ + + result = value; /* r will be reversed bits of v; first get LSB of v */ + for (value >>= 1U; value; value >>= 1U) + { + result <<= 1U; + result |= value & 1U; + s--; + } + result <<= s; /* shift when v's highest bits are zero */ +#endif + return(result); +} + + +/** + \brief Count leading zeros + \details Counts the number of leading zeros of a data value. + \param [in] value Value to count the leading zeros + \return number of leading zeros in value + */ +#define __CLZ __builtin_clz + + +#if (__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U) + +/** + \brief LDR Exclusive (8 bit) + \details Executes a exclusive LDR instruction for 8 bit value. + \param [in] ptr Pointer to data + \return value of type uint8_t at (*ptr) + */ +__attribute__((always_inline)) __STATIC_INLINE uint8_t __LDREXB(volatile uint8_t *addr) +{ + uint32_t result; + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + __ASM volatile ("ldrexb %0, %1" : "=r" (result) : "Q" (*addr) ); +#else + /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not + accepted by assembler. So has to use following less efficient pattern. + */ + __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) : "memory" ); +#endif + return ((uint8_t) result); /* Add explicit type cast here */ +} + + +/** + \brief LDR Exclusive (16 bit) + \details Executes a exclusive LDR instruction for 16 bit values. + \param [in] ptr Pointer to data + \return value of type uint16_t at (*ptr) + */ +__attribute__((always_inline)) __STATIC_INLINE uint16_t __LDREXH(volatile uint16_t *addr) +{ + uint32_t result; + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + __ASM volatile ("ldrexh %0, %1" : "=r" (result) : "Q" (*addr) ); +#else + /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not + accepted by assembler. So has to use following less efficient pattern. + */ + __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) : "memory" ); +#endif + return ((uint16_t) result); /* Add explicit type cast here */ +} + + +/** + \brief LDR Exclusive (32 bit) + \details Executes a exclusive LDR instruction for 32 bit values. + \param [in] ptr Pointer to data + \return value of type uint32_t at (*ptr) + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __LDREXW(volatile uint32_t *addr) +{ + uint32_t result; + + __ASM volatile ("ldrex %0, %1" : "=r" (result) : "Q" (*addr) ); + return(result); +} + + +/** + \brief STR Exclusive (8 bit) + \details Executes a exclusive STR instruction for 8 bit values. + \param [in] value Value to store + \param [in] ptr Pointer to location + \return 0 Function succeeded + \return 1 Function failed + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __STREXB(uint8_t value, volatile uint8_t *addr) +{ + uint32_t result; + + __ASM volatile ("strexb %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) ); + return(result); +} + + +/** + \brief STR Exclusive (16 bit) + \details Executes a exclusive STR instruction for 16 bit values. + \param [in] value Value to store + \param [in] ptr Pointer to location + \return 0 Function succeeded + \return 1 Function failed + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __STREXH(uint16_t value, volatile uint16_t *addr) +{ + uint32_t result; + + __ASM volatile ("strexh %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" ((uint32_t)value) ); + return(result); +} + + +/** + \brief STR Exclusive (32 bit) + \details Executes a exclusive STR instruction for 32 bit values. + \param [in] value Value to store + \param [in] ptr Pointer to location + \return 0 Function succeeded + \return 1 Function failed + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __STREXW(uint32_t value, volatile uint32_t *addr) +{ + uint32_t result; + + __ASM volatile ("strex %0, %2, %1" : "=&r" (result), "=Q" (*addr) : "r" (value) ); + return(result); +} + + +/** + \brief Remove the exclusive lock + \details Removes the exclusive lock which is created by LDREX. + */ +__attribute__((always_inline)) __STATIC_INLINE void __CLREX(void) +{ + __ASM volatile ("clrex" ::: "memory"); +} + + +/** + \brief Signed Saturate + \details Saturates a signed value. + \param [in] value Value to be saturated + \param [in] sat Bit position to saturate to (1..32) + \return Saturated value + */ +#define __SSAT(ARG1,ARG2) \ +({ \ + uint32_t __RES, __ARG1 = (ARG1); \ + __ASM ("ssat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \ + __RES; \ + }) + + +/** + \brief Unsigned Saturate + \details Saturates an unsigned value. + \param [in] value Value to be saturated + \param [in] sat Bit position to saturate to (0..31) + \return Saturated value + */ +#define __USAT(ARG1,ARG2) \ +({ \ + uint32_t __RES, __ARG1 = (ARG1); \ + __ASM ("usat %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \ + __RES; \ + }) + + +/** + \brief Rotate Right with Extend (32 bit) + \details Moves each bit of a bitstring right by one bit. + The carry input is shifted in at the left end of the bitstring. + \param [in] value Value to rotate + \return Rotated value + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __RRX(uint32_t value) +{ + uint32_t result; + + __ASM volatile ("rrx %0, %1" : __CMSIS_GCC_OUT_REG (result) : __CMSIS_GCC_USE_REG (value) ); + return(result); +} + + +/** + \brief LDRT Unprivileged (8 bit) + \details Executes a Unprivileged LDRT instruction for 8 bit value. + \param [in] ptr Pointer to data + \return value of type uint8_t at (*ptr) + */ +__attribute__((always_inline)) __STATIC_INLINE uint8_t __LDRBT(volatile uint8_t *addr) +{ + uint32_t result; + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + __ASM volatile ("ldrbt %0, %1" : "=r" (result) : "Q" (*addr) ); +#else + /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not + accepted by assembler. So has to use following less efficient pattern. + */ + __ASM volatile ("ldrbt %0, [%1]" : "=r" (result) : "r" (addr) : "memory" ); +#endif + return ((uint8_t) result); /* Add explicit type cast here */ +} + + +/** + \brief LDRT Unprivileged (16 bit) + \details Executes a Unprivileged LDRT instruction for 16 bit values. + \param [in] ptr Pointer to data + \return value of type uint16_t at (*ptr) + */ +__attribute__((always_inline)) __STATIC_INLINE uint16_t __LDRHT(volatile uint16_t *addr) +{ + uint32_t result; + +#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 8) + __ASM volatile ("ldrht %0, %1" : "=r" (result) : "Q" (*addr) ); +#else + /* Prior to GCC 4.8, "Q" will be expanded to [rx, #0] which is not + accepted by assembler. So has to use following less efficient pattern. + */ + __ASM volatile ("ldrht %0, [%1]" : "=r" (result) : "r" (addr) : "memory" ); +#endif + return ((uint16_t) result); /* Add explicit type cast here */ +} + + +/** + \brief LDRT Unprivileged (32 bit) + \details Executes a Unprivileged LDRT instruction for 32 bit values. + \param [in] ptr Pointer to data + \return value of type uint32_t at (*ptr) + */ +__attribute__((always_inline)) __STATIC_INLINE uint32_t __LDRT(volatile uint32_t *addr) +{ + uint32_t result; + + __ASM volatile ("ldrt %0, %1" : "=r" (result) : "Q" (*addr) ); + return(result); +} + + +/** + \brief STRT Unprivileged (8 bit) + \details Executes a Unprivileged STRT instruction for 8 bit values. + \param [in] value Value to store + \param [in] ptr Pointer to location + */ +__attribute__((always_inline)) __STATIC_INLINE void __STRBT(uint8_t value, volatile uint8_t *addr) +{ + __ASM volatile ("strbt %1, %0" : "=Q" (*addr) : "r" ((uint32_t)value) ); +} + + +/** + \brief STRT Unprivileged (16 bit) + \details Executes a Unprivileged STRT instruction for 16 bit values. + \param [in] value Value to store + \param [in] ptr Pointer to location + */ +__attribute__((always_inline)) __STATIC_INLINE void __STRHT(uint16_t value, volatile uint16_t *addr) +{ + __ASM volatile ("strht %1, %0" : "=Q" (*addr) : "r" ((uint32_t)value) ); +} + + +/** + \brief STRT Unprivileged (32 bit) + \details Executes a Unprivileged STRT instruction for 32 bit values. + \param [in] value Value to store + \param [in] ptr Pointer to location + */ +__attribute__((always_inline)) __STATIC_INLINE void __STRT(uint32_t value, volatile uint32_t *addr) +{ + __ASM volatile ("strt %1, %0" : "=Q" (*addr) : "r" (value) ); +} + +#endif /* (__CORTEX_M >= 0x03U) || (__CORTEX_SC >= 300U) */ + +/*@}*/ /* end of group CMSIS_Core_InstructionInterface */ + + +/* ################### Compiler specific Intrinsics ########################### */ +/** \defgroup CMSIS_SIMD_intrinsics CMSIS SIMD Intrinsics + Access to dedicated SIMD instructions + @{ +*/ + +#if (__CORTEX_M >= 0x04U) /* only for Cortex-M4 and above */ + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SADD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("sadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __QADD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("qadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SHADD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("shadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UADD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UQADD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uqadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UHADD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uhadd8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SSUB8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("ssub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __QSUB8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("qsub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SHSUB8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("shsub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __USUB8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("usub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UQSUB8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uqsub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UHSUB8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uhsub8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SADD16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("sadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __QADD16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("qadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SHADD16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("shadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UADD16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UQADD16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uqadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UHADD16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uhadd16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SSUB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("ssub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __QSUB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("qsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SHSUB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("shsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __USUB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("usub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UQSUB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uqsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UHSUB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uhsub16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SASX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("sasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __QASX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("qasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SHASX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("shasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UASX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UQASX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uqasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UHASX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uhasx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SSAX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("ssax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __QSAX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("qsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SHSAX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("shsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __USAX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("usax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UQSAX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uqsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UHSAX(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uhsax %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __USAD8(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("usad8 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __USADA8(uint32_t op1, uint32_t op2, uint32_t op3) +{ + uint32_t result; + + __ASM volatile ("usada8 %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) ); + return(result); +} + +#define __SSAT16(ARG1,ARG2) \ +({ \ + int32_t __RES, __ARG1 = (ARG1); \ + __ASM ("ssat16 %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \ + __RES; \ + }) + +#define __USAT16(ARG1,ARG2) \ +({ \ + uint32_t __RES, __ARG1 = (ARG1); \ + __ASM ("usat16 %0, %1, %2" : "=r" (__RES) : "I" (ARG2), "r" (__ARG1) ); \ + __RES; \ + }) + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UXTB16(uint32_t op1) +{ + uint32_t result; + + __ASM volatile ("uxtb16 %0, %1" : "=r" (result) : "r" (op1)); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __UXTAB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("uxtab16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SXTB16(uint32_t op1) +{ + uint32_t result; + + __ASM volatile ("sxtb16 %0, %1" : "=r" (result) : "r" (op1)); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SXTAB16(uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("sxtab16 %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMUAD (uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("smuad %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMUADX (uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("smuadx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMLAD (uint32_t op1, uint32_t op2, uint32_t op3) +{ + uint32_t result; + + __ASM volatile ("smlad %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMLADX (uint32_t op1, uint32_t op2, uint32_t op3) +{ + uint32_t result; + + __ASM volatile ("smladx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint64_t __SMLALD (uint32_t op1, uint32_t op2, uint64_t acc) +{ + union llreg_u{ + uint32_t w32[2]; + uint64_t w64; + } llr; + llr.w64 = acc; + +#ifndef __ARMEB__ /* Little endian */ + __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) ); +#else /* Big endian */ + __ASM volatile ("smlald %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) ); +#endif + + return(llr.w64); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint64_t __SMLALDX (uint32_t op1, uint32_t op2, uint64_t acc) +{ + union llreg_u{ + uint32_t w32[2]; + uint64_t w64; + } llr; + llr.w64 = acc; + +#ifndef __ARMEB__ /* Little endian */ + __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) ); +#else /* Big endian */ + __ASM volatile ("smlaldx %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) ); +#endif + + return(llr.w64); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMUSD (uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("smusd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMUSDX (uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("smusdx %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMLSD (uint32_t op1, uint32_t op2, uint32_t op3) +{ + uint32_t result; + + __ASM volatile ("smlsd %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMLSDX (uint32_t op1, uint32_t op2, uint32_t op3) +{ + uint32_t result; + + __ASM volatile ("smlsdx %0, %1, %2, %3" : "=r" (result) : "r" (op1), "r" (op2), "r" (op3) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint64_t __SMLSLD (uint32_t op1, uint32_t op2, uint64_t acc) +{ + union llreg_u{ + uint32_t w32[2]; + uint64_t w64; + } llr; + llr.w64 = acc; + +#ifndef __ARMEB__ /* Little endian */ + __ASM volatile ("smlsld %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) ); +#else /* Big endian */ + __ASM volatile ("smlsld %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) ); +#endif + + return(llr.w64); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint64_t __SMLSLDX (uint32_t op1, uint32_t op2, uint64_t acc) +{ + union llreg_u{ + uint32_t w32[2]; + uint64_t w64; + } llr; + llr.w64 = acc; + +#ifndef __ARMEB__ /* Little endian */ + __ASM volatile ("smlsldx %0, %1, %2, %3" : "=r" (llr.w32[0]), "=r" (llr.w32[1]): "r" (op1), "r" (op2) , "0" (llr.w32[0]), "1" (llr.w32[1]) ); +#else /* Big endian */ + __ASM volatile ("smlsldx %0, %1, %2, %3" : "=r" (llr.w32[1]), "=r" (llr.w32[0]): "r" (op1), "r" (op2) , "0" (llr.w32[1]), "1" (llr.w32[0]) ); +#endif + + return(llr.w64); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SEL (uint32_t op1, uint32_t op2) +{ + uint32_t result; + + __ASM volatile ("sel %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE int32_t __QADD( int32_t op1, int32_t op2) +{ + int32_t result; + + __ASM volatile ("qadd %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +__attribute__( ( always_inline ) ) __STATIC_INLINE int32_t __QSUB( int32_t op1, int32_t op2) +{ + int32_t result; + + __ASM volatile ("qsub %0, %1, %2" : "=r" (result) : "r" (op1), "r" (op2) ); + return(result); +} + +#define __PKHBT(ARG1,ARG2,ARG3) \ +({ \ + uint32_t __RES, __ARG1 = (ARG1), __ARG2 = (ARG2); \ + __ASM ("pkhbt %0, %1, %2, lsl %3" : "=r" (__RES) : "r" (__ARG1), "r" (__ARG2), "I" (ARG3) ); \ + __RES; \ + }) + +#define __PKHTB(ARG1,ARG2,ARG3) \ +({ \ + uint32_t __RES, __ARG1 = (ARG1), __ARG2 = (ARG2); \ + if (ARG3 == 0) \ + __ASM ("pkhtb %0, %1, %2" : "=r" (__RES) : "r" (__ARG1), "r" (__ARG2) ); \ + else \ + __ASM ("pkhtb %0, %1, %2, asr %3" : "=r" (__RES) : "r" (__ARG1), "r" (__ARG2), "I" (ARG3) ); \ + __RES; \ + }) + +__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SMMLA (int32_t op1, int32_t op2, int32_t op3) +{ + int32_t result; + + __ASM volatile ("smmla %0, %1, %2, %3" : "=r" (result): "r" (op1), "r" (op2), "r" (op3) ); + return(result); +} + +#endif /* (__CORTEX_M >= 0x04) */ +/*@} end of group CMSIS_SIMD_intrinsics */ + + +#if defined ( __GNUC__ ) +#pragma GCC diagnostic pop +#endif + +#endif /* __CMSIS_GCC_H */ diff --git a/NUC123/inc/core_cm0.h b/NUC123/inc/core_cm0.h new file mode 100644 index 0000000..711dad5 --- /dev/null +++ b/NUC123/inc/core_cm0.h @@ -0,0 +1,798 @@ +/**************************************************************************//** + * @file core_cm0.h + * @brief CMSIS Cortex-M0 Core Peripheral Access Layer Header File + * @version V4.30 + * @date 20. October 2015 + ******************************************************************************/ +/* Copyright (c) 2009 - 2015 ARM LIMITED + + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + - Neither the name of ARM nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + * + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + ---------------------------------------------------------------------------*/ + + +#if defined ( __ICCARM__ ) + #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #pragma clang system_header /* treat file as system include file */ +#endif + +#ifndef __CORE_CM0_H_GENERIC +#define __CORE_CM0_H_GENERIC + +#include + +#ifdef __cplusplus + extern "C" { +#endif + +/** + \page CMSIS_MISRA_Exceptions MISRA-C:2004 Compliance Exceptions + CMSIS violates the following MISRA-C:2004 rules: + + \li Required Rule 8.5, object/function definition in header file.
+ Function definitions in header files are used to allow 'inlining'. + + \li Required Rule 18.4, declaration of union type or object of union type: '{...}'.
+ Unions are used for effective representation of core registers. + + \li Advisory Rule 19.7, Function-like macro defined.
+ Function-like macros are used to allow more efficient code. + */ + + +/******************************************************************************* + * CMSIS definitions + ******************************************************************************/ +/** + \ingroup Cortex_M0 + @{ + */ + +/* CMSIS CM0 definitions */ +#define __CM0_CMSIS_VERSION_MAIN (0x04U) /*!< [31:16] CMSIS HAL main version */ +#define __CM0_CMSIS_VERSION_SUB (0x1EU) /*!< [15:0] CMSIS HAL sub version */ +#define __CM0_CMSIS_VERSION ((__CM0_CMSIS_VERSION_MAIN << 16U) | \ + __CM0_CMSIS_VERSION_SUB ) /*!< CMSIS HAL version number */ + +#define __CORTEX_M (0x00U) /*!< Cortex-M Core */ + + +#if defined ( __CC_ARM ) + #define __ASM __asm /*!< asm keyword for ARM Compiler */ + #define __INLINE __inline /*!< inline keyword for ARM Compiler */ + #define __STATIC_INLINE static __inline + +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #define __ASM __asm /*!< asm keyword for ARM Compiler */ + #define __INLINE __inline /*!< inline keyword for ARM Compiler */ + #define __STATIC_INLINE static __inline + +#elif defined ( __GNUC__ ) + #define __ASM __asm /*!< asm keyword for GNU Compiler */ + #define __INLINE inline /*!< inline keyword for GNU Compiler */ + #define __STATIC_INLINE static inline + +#elif defined ( __ICCARM__ ) + #define __ASM __asm /*!< asm keyword for IAR Compiler */ + #define __INLINE inline /*!< inline keyword for IAR Compiler. Only available in High optimization mode! */ + #define __STATIC_INLINE static inline + +#elif defined ( __TMS470__ ) + #define __ASM __asm /*!< asm keyword for TI CCS Compiler */ + #define __STATIC_INLINE static inline + +#elif defined ( __TASKING__ ) + #define __ASM __asm /*!< asm keyword for TASKING Compiler */ + #define __INLINE inline /*!< inline keyword for TASKING Compiler */ + #define __STATIC_INLINE static inline + +#elif defined ( __CSMC__ ) + #define __packed + #define __ASM _asm /*!< asm keyword for COSMIC Compiler */ + #define __INLINE inline /*!< inline keyword for COSMIC Compiler. Use -pc99 on compile line */ + #define __STATIC_INLINE static inline + +#else + #error Unknown compiler +#endif + +/** __FPU_USED indicates whether an FPU is used or not. + This core does not support an FPU at all +*/ +#define __FPU_USED 0U + +#if defined ( __CC_ARM ) + #if defined __TARGET_FPU_VFP + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #if defined __ARM_PCS_VFP + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#elif defined ( __GNUC__ ) + #if defined (__VFP_FP__) && !defined(__SOFTFP__) + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#elif defined ( __ICCARM__ ) + #if defined __ARMVFP__ + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#elif defined ( __TMS470__ ) + #if defined __TI_VFP_SUPPORT__ + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#elif defined ( __TASKING__ ) + #if defined __FPU_VFP__ + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#elif defined ( __CSMC__ ) + #if ( __CSMC__ & 0x400U) + #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)" + #endif + +#endif + +#include "core_cmInstr.h" /* Core Instruction Access */ +#include "core_cmFunc.h" /* Core Function Access */ + +#ifdef __cplusplus +} +#endif + +#endif /* __CORE_CM0_H_GENERIC */ + +#ifndef __CMSIS_GENERIC + +#ifndef __CORE_CM0_H_DEPENDANT +#define __CORE_CM0_H_DEPENDANT + +#ifdef __cplusplus + extern "C" { +#endif + +/* check device defines and use defaults */ +#if defined __CHECK_DEVICE_DEFINES + #ifndef __CM0_REV + #define __CM0_REV 0x0000U + #warning "__CM0_REV not defined in device header file; using default!" + #endif + + #ifndef __NVIC_PRIO_BITS + #define __NVIC_PRIO_BITS 2U + #warning "__NVIC_PRIO_BITS not defined in device header file; using default!" + #endif + + #ifndef __Vendor_SysTickConfig + #define __Vendor_SysTickConfig 0U + #warning "__Vendor_SysTickConfig not defined in device header file; using default!" + #endif +#endif + +/* IO definitions (access restrictions to peripheral registers) */ +/** + \defgroup CMSIS_glob_defs CMSIS Global Defines + + IO Type Qualifiers are used + \li to specify the access to peripheral variables. + \li for automatic generation of peripheral register debug information. +*/ +#ifdef __cplusplus + #define __I volatile /*!< Defines 'read only' permissions */ +#else + #define __I volatile const /*!< Defines 'read only' permissions */ +#endif +#define __O volatile /*!< Defines 'write only' permissions */ +#define __IO volatile /*!< Defines 'read / write' permissions */ + +/* following defines should be used for structure members */ +#define __IM volatile const /*! Defines 'read only' structure member permissions */ +#define __OM volatile /*! Defines 'write only' structure member permissions */ +#define __IOM volatile /*! Defines 'read / write' structure member permissions */ + +/*@} end of group Cortex_M0 */ + + + +/******************************************************************************* + * Register Abstraction + Core Register contain: + - Core Register + - Core NVIC Register + - Core SCB Register + - Core SysTick Register + ******************************************************************************/ +/** + \defgroup CMSIS_core_register Defines and Type Definitions + \brief Type definitions and defines for Cortex-M processor based devices. +*/ + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_CORE Status and Control Registers + \brief Core Register type definitions. + @{ + */ + +/** + \brief Union type to access the Application Program Status Register (APSR). + */ +typedef union +{ + struct + { + uint32_t _reserved0:28; /*!< bit: 0..27 Reserved */ + uint32_t V:1; /*!< bit: 28 Overflow condition code flag */ + uint32_t C:1; /*!< bit: 29 Carry condition code flag */ + uint32_t Z:1; /*!< bit: 30 Zero condition code flag */ + uint32_t N:1; /*!< bit: 31 Negative condition code flag */ + } b; /*!< Structure used for bit access */ + uint32_t w; /*!< Type used for word access */ +} APSR_Type; + +/* APSR Register Definitions */ +#define APSR_N_Pos 31U /*!< APSR: N Position */ +#define APSR_N_Msk (1UL << APSR_N_Pos) /*!< APSR: N Mask */ + +#define APSR_Z_Pos 30U /*!< APSR: Z Position */ +#define APSR_Z_Msk (1UL << APSR_Z_Pos) /*!< APSR: Z Mask */ + +#define APSR_C_Pos 29U /*!< APSR: C Position */ +#define APSR_C_Msk (1UL << APSR_C_Pos) /*!< APSR: C Mask */ + +#define APSR_V_Pos 28U /*!< APSR: V Position */ +#define APSR_V_Msk (1UL << APSR_V_Pos) /*!< APSR: V Mask */ + + +/** + \brief Union type to access the Interrupt Program Status Register (IPSR). + */ +typedef union +{ + struct + { + uint32_t ISR:9; /*!< bit: 0.. 8 Exception number */ + uint32_t _reserved0:23; /*!< bit: 9..31 Reserved */ + } b; /*!< Structure used for bit access */ + uint32_t w; /*!< Type used for word access */ +} IPSR_Type; + +/* IPSR Register Definitions */ +#define IPSR_ISR_Pos 0U /*!< IPSR: ISR Position */ +#define IPSR_ISR_Msk (0x1FFUL /*<< IPSR_ISR_Pos*/) /*!< IPSR: ISR Mask */ + + +/** + \brief Union type to access the Special-Purpose Program Status Registers (xPSR). + */ +typedef union +{ + struct + { + uint32_t ISR:9; /*!< bit: 0.. 8 Exception number */ + uint32_t _reserved0:15; /*!< bit: 9..23 Reserved */ + uint32_t T:1; /*!< bit: 24 Thumb bit (read 0) */ + uint32_t _reserved1:3; /*!< bit: 25..27 Reserved */ + uint32_t V:1; /*!< bit: 28 Overflow condition code flag */ + uint32_t C:1; /*!< bit: 29 Carry condition code flag */ + uint32_t Z:1; /*!< bit: 30 Zero condition code flag */ + uint32_t N:1; /*!< bit: 31 Negative condition code flag */ + } b; /*!< Structure used for bit access */ + uint32_t w; /*!< Type used for word access */ +} xPSR_Type; + +/* xPSR Register Definitions */ +#define xPSR_N_Pos 31U /*!< xPSR: N Position */ +#define xPSR_N_Msk (1UL << xPSR_N_Pos) /*!< xPSR: N Mask */ + +#define xPSR_Z_Pos 30U /*!< xPSR: Z Position */ +#define xPSR_Z_Msk (1UL << xPSR_Z_Pos) /*!< xPSR: Z Mask */ + +#define xPSR_C_Pos 29U /*!< xPSR: C Position */ +#define xPSR_C_Msk (1UL << xPSR_C_Pos) /*!< xPSR: C Mask */ + +#define xPSR_V_Pos 28U /*!< xPSR: V Position */ +#define xPSR_V_Msk (1UL << xPSR_V_Pos) /*!< xPSR: V Mask */ + +#define xPSR_T_Pos 24U /*!< xPSR: T Position */ +#define xPSR_T_Msk (1UL << xPSR_T_Pos) /*!< xPSR: T Mask */ + +#define xPSR_ISR_Pos 0U /*!< xPSR: ISR Position */ +#define xPSR_ISR_Msk (0x1FFUL /*<< xPSR_ISR_Pos*/) /*!< xPSR: ISR Mask */ + + +/** + \brief Union type to access the Control Registers (CONTROL). + */ +typedef union +{ + struct + { + uint32_t _reserved0:1; /*!< bit: 0 Reserved */ + uint32_t SPSEL:1; /*!< bit: 1 Stack to be used */ + uint32_t _reserved1:30; /*!< bit: 2..31 Reserved */ + } b; /*!< Structure used for bit access */ + uint32_t w; /*!< Type used for word access */ +} CONTROL_Type; + +/* CONTROL Register Definitions */ +#define CONTROL_SPSEL_Pos 1U /*!< CONTROL: SPSEL Position */ +#define CONTROL_SPSEL_Msk (1UL << CONTROL_SPSEL_Pos) /*!< CONTROL: SPSEL Mask */ + +/*@} end of group CMSIS_CORE */ + + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_NVIC Nested Vectored Interrupt Controller (NVIC) + \brief Type definitions for the NVIC Registers + @{ + */ + +/** + \brief Structure type to access the Nested Vectored Interrupt Controller (NVIC). + */ +typedef struct +{ + __IOM uint32_t ISER[1U]; /*!< Offset: 0x000 (R/W) Interrupt Set Enable Register */ + uint32_t RESERVED0[31U]; + __IOM uint32_t ICER[1U]; /*!< Offset: 0x080 (R/W) Interrupt Clear Enable Register */ + uint32_t RSERVED1[31U]; + __IOM uint32_t ISPR[1U]; /*!< Offset: 0x100 (R/W) Interrupt Set Pending Register */ + uint32_t RESERVED2[31U]; + __IOM uint32_t ICPR[1U]; /*!< Offset: 0x180 (R/W) Interrupt Clear Pending Register */ + uint32_t RESERVED3[31U]; + uint32_t RESERVED4[64U]; + __IOM uint32_t IP[8U]; /*!< Offset: 0x300 (R/W) Interrupt Priority Register */ +} NVIC_Type; + +/*@} end of group CMSIS_NVIC */ + + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_SCB System Control Block (SCB) + \brief Type definitions for the System Control Block Registers + @{ + */ + +/** + \brief Structure type to access the System Control Block (SCB). + */ +typedef struct +{ + __IM uint32_t CPUID; /*!< Offset: 0x000 (R/ ) CPUID Base Register */ + __IOM uint32_t ICSR; /*!< Offset: 0x004 (R/W) Interrupt Control and State Register */ + uint32_t RESERVED0; + __IOM uint32_t AIRCR; /*!< Offset: 0x00C (R/W) Application Interrupt and Reset Control Register */ + __IOM uint32_t SCR; /*!< Offset: 0x010 (R/W) System Control Register */ + __IOM uint32_t CCR; /*!< Offset: 0x014 (R/W) Configuration Control Register */ + uint32_t RESERVED1; + __IOM uint32_t SHP[2U]; /*!< Offset: 0x01C (R/W) System Handlers Priority Registers. [0] is RESERVED */ + __IOM uint32_t SHCSR; /*!< Offset: 0x024 (R/W) System Handler Control and State Register */ +} SCB_Type; + +/* SCB CPUID Register Definitions */ +#define SCB_CPUID_IMPLEMENTER_Pos 24U /*!< SCB CPUID: IMPLEMENTER Position */ +#define SCB_CPUID_IMPLEMENTER_Msk (0xFFUL << SCB_CPUID_IMPLEMENTER_Pos) /*!< SCB CPUID: IMPLEMENTER Mask */ + +#define SCB_CPUID_VARIANT_Pos 20U /*!< SCB CPUID: VARIANT Position */ +#define SCB_CPUID_VARIANT_Msk (0xFUL << SCB_CPUID_VARIANT_Pos) /*!< SCB CPUID: VARIANT Mask */ + +#define SCB_CPUID_ARCHITECTURE_Pos 16U /*!< SCB CPUID: ARCHITECTURE Position */ +#define SCB_CPUID_ARCHITECTURE_Msk (0xFUL << SCB_CPUID_ARCHITECTURE_Pos) /*!< SCB CPUID: ARCHITECTURE Mask */ + +#define SCB_CPUID_PARTNO_Pos 4U /*!< SCB CPUID: PARTNO Position */ +#define SCB_CPUID_PARTNO_Msk (0xFFFUL << SCB_CPUID_PARTNO_Pos) /*!< SCB CPUID: PARTNO Mask */ + +#define SCB_CPUID_REVISION_Pos 0U /*!< SCB CPUID: REVISION Position */ +#define SCB_CPUID_REVISION_Msk (0xFUL /*<< SCB_CPUID_REVISION_Pos*/) /*!< SCB CPUID: REVISION Mask */ + +/* SCB Interrupt Control State Register Definitions */ +#define SCB_ICSR_NMIPENDSET_Pos 31U /*!< SCB ICSR: NMIPENDSET Position */ +#define SCB_ICSR_NMIPENDSET_Msk (1UL << SCB_ICSR_NMIPENDSET_Pos) /*!< SCB ICSR: NMIPENDSET Mask */ + +#define SCB_ICSR_PENDSVSET_Pos 28U /*!< SCB ICSR: PENDSVSET Position */ +#define SCB_ICSR_PENDSVSET_Msk (1UL << SCB_ICSR_PENDSVSET_Pos) /*!< SCB ICSR: PENDSVSET Mask */ + +#define SCB_ICSR_PENDSVCLR_Pos 27U /*!< SCB ICSR: PENDSVCLR Position */ +#define SCB_ICSR_PENDSVCLR_Msk (1UL << SCB_ICSR_PENDSVCLR_Pos) /*!< SCB ICSR: PENDSVCLR Mask */ + +#define SCB_ICSR_PENDSTSET_Pos 26U /*!< SCB ICSR: PENDSTSET Position */ +#define SCB_ICSR_PENDSTSET_Msk (1UL << SCB_ICSR_PENDSTSET_Pos) /*!< SCB ICSR: PENDSTSET Mask */ + +#define SCB_ICSR_PENDSTCLR_Pos 25U /*!< SCB ICSR: PENDSTCLR Position */ +#define SCB_ICSR_PENDSTCLR_Msk (1UL << SCB_ICSR_PENDSTCLR_Pos) /*!< SCB ICSR: PENDSTCLR Mask */ + +#define SCB_ICSR_ISRPREEMPT_Pos 23U /*!< SCB ICSR: ISRPREEMPT Position */ +#define SCB_ICSR_ISRPREEMPT_Msk (1UL << SCB_ICSR_ISRPREEMPT_Pos) /*!< SCB ICSR: ISRPREEMPT Mask */ + +#define SCB_ICSR_ISRPENDING_Pos 22U /*!< SCB ICSR: ISRPENDING Position */ +#define SCB_ICSR_ISRPENDING_Msk (1UL << SCB_ICSR_ISRPENDING_Pos) /*!< SCB ICSR: ISRPENDING Mask */ + +#define SCB_ICSR_VECTPENDING_Pos 12U /*!< SCB ICSR: VECTPENDING Position */ +#define SCB_ICSR_VECTPENDING_Msk (0x1FFUL << SCB_ICSR_VECTPENDING_Pos) /*!< SCB ICSR: VECTPENDING Mask */ + +#define SCB_ICSR_VECTACTIVE_Pos 0U /*!< SCB ICSR: VECTACTIVE Position */ +#define SCB_ICSR_VECTACTIVE_Msk (0x1FFUL /*<< SCB_ICSR_VECTACTIVE_Pos*/) /*!< SCB ICSR: VECTACTIVE Mask */ + +/* SCB Application Interrupt and Reset Control Register Definitions */ +#define SCB_AIRCR_VECTKEY_Pos 16U /*!< SCB AIRCR: VECTKEY Position */ +#define SCB_AIRCR_VECTKEY_Msk (0xFFFFUL << SCB_AIRCR_VECTKEY_Pos) /*!< SCB AIRCR: VECTKEY Mask */ + +#define SCB_AIRCR_VECTKEYSTAT_Pos 16U /*!< SCB AIRCR: VECTKEYSTAT Position */ +#define SCB_AIRCR_VECTKEYSTAT_Msk (0xFFFFUL << SCB_AIRCR_VECTKEYSTAT_Pos) /*!< SCB AIRCR: VECTKEYSTAT Mask */ + +#define SCB_AIRCR_ENDIANESS_Pos 15U /*!< SCB AIRCR: ENDIANESS Position */ +#define SCB_AIRCR_ENDIANESS_Msk (1UL << SCB_AIRCR_ENDIANESS_Pos) /*!< SCB AIRCR: ENDIANESS Mask */ + +#define SCB_AIRCR_SYSRESETREQ_Pos 2U /*!< SCB AIRCR: SYSRESETREQ Position */ +#define SCB_AIRCR_SYSRESETREQ_Msk (1UL << SCB_AIRCR_SYSRESETREQ_Pos) /*!< SCB AIRCR: SYSRESETREQ Mask */ + +#define SCB_AIRCR_VECTCLRACTIVE_Pos 1U /*!< SCB AIRCR: VECTCLRACTIVE Position */ +#define SCB_AIRCR_VECTCLRACTIVE_Msk (1UL << SCB_AIRCR_VECTCLRACTIVE_Pos) /*!< SCB AIRCR: VECTCLRACTIVE Mask */ + +/* SCB System Control Register Definitions */ +#define SCB_SCR_SEVONPEND_Pos 4U /*!< SCB SCR: SEVONPEND Position */ +#define SCB_SCR_SEVONPEND_Msk (1UL << SCB_SCR_SEVONPEND_Pos) /*!< SCB SCR: SEVONPEND Mask */ + +#define SCB_SCR_SLEEPDEEP_Pos 2U /*!< SCB SCR: SLEEPDEEP Position */ +#define SCB_SCR_SLEEPDEEP_Msk (1UL << SCB_SCR_SLEEPDEEP_Pos) /*!< SCB SCR: SLEEPDEEP Mask */ + +#define SCB_SCR_SLEEPONEXIT_Pos 1U /*!< SCB SCR: SLEEPONEXIT Position */ +#define SCB_SCR_SLEEPONEXIT_Msk (1UL << SCB_SCR_SLEEPONEXIT_Pos) /*!< SCB SCR: SLEEPONEXIT Mask */ + +/* SCB Configuration Control Register Definitions */ +#define SCB_CCR_STKALIGN_Pos 9U /*!< SCB CCR: STKALIGN Position */ +#define SCB_CCR_STKALIGN_Msk (1UL << SCB_CCR_STKALIGN_Pos) /*!< SCB CCR: STKALIGN Mask */ + +#define SCB_CCR_UNALIGN_TRP_Pos 3U /*!< SCB CCR: UNALIGN_TRP Position */ +#define SCB_CCR_UNALIGN_TRP_Msk (1UL << SCB_CCR_UNALIGN_TRP_Pos) /*!< SCB CCR: UNALIGN_TRP Mask */ + +/* SCB System Handler Control and State Register Definitions */ +#define SCB_SHCSR_SVCALLPENDED_Pos 15U /*!< SCB SHCSR: SVCALLPENDED Position */ +#define SCB_SHCSR_SVCALLPENDED_Msk (1UL << SCB_SHCSR_SVCALLPENDED_Pos) /*!< SCB SHCSR: SVCALLPENDED Mask */ + +/*@} end of group CMSIS_SCB */ + + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_SysTick System Tick Timer (SysTick) + \brief Type definitions for the System Timer Registers. + @{ + */ + +/** + \brief Structure type to access the System Timer (SysTick). + */ +typedef struct +{ + __IOM uint32_t CTRL; /*!< Offset: 0x000 (R/W) SysTick Control and Status Register */ + __IOM uint32_t LOAD; /*!< Offset: 0x004 (R/W) SysTick Reload Value Register */ + __IOM uint32_t VAL; /*!< Offset: 0x008 (R/W) SysTick Current Value Register */ + __IM uint32_t CALIB; /*!< Offset: 0x00C (R/ ) SysTick Calibration Register */ +} SysTick_Type; + +/* SysTick Control / Status Register Definitions */ +#define SysTick_CTRL_COUNTFLAG_Pos 16U /*!< SysTick CTRL: COUNTFLAG Position */ +#define SysTick_CTRL_COUNTFLAG_Msk (1UL << SysTick_CTRL_COUNTFLAG_Pos) /*!< SysTick CTRL: COUNTFLAG Mask */ + +#define SysTick_CTRL_CLKSOURCE_Pos 2U /*!< SysTick CTRL: CLKSOURCE Position */ +#define SysTick_CTRL_CLKSOURCE_Msk (1UL << SysTick_CTRL_CLKSOURCE_Pos) /*!< SysTick CTRL: CLKSOURCE Mask */ + +#define SysTick_CTRL_TICKINT_Pos 1U /*!< SysTick CTRL: TICKINT Position */ +#define SysTick_CTRL_TICKINT_Msk (1UL << SysTick_CTRL_TICKINT_Pos) /*!< SysTick CTRL: TICKINT Mask */ + +#define SysTick_CTRL_ENABLE_Pos 0U /*!< SysTick CTRL: ENABLE Position */ +#define SysTick_CTRL_ENABLE_Msk (1UL /*<< SysTick_CTRL_ENABLE_Pos*/) /*!< SysTick CTRL: ENABLE Mask */ + +/* SysTick Reload Register Definitions */ +#define SysTick_LOAD_RELOAD_Pos 0U /*!< SysTick LOAD: RELOAD Position */ +#define SysTick_LOAD_RELOAD_Msk (0xFFFFFFUL /*<< SysTick_LOAD_RELOAD_Pos*/) /*!< SysTick LOAD: RELOAD Mask */ + +/* SysTick Current Register Definitions */ +#define SysTick_VAL_CURRENT_Pos 0U /*!< SysTick VAL: CURRENT Position */ +#define SysTick_VAL_CURRENT_Msk (0xFFFFFFUL /*<< SysTick_VAL_CURRENT_Pos*/) /*!< SysTick VAL: CURRENT Mask */ + +/* SysTick Calibration Register Definitions */ +#define SysTick_CALIB_NOREF_Pos 31U /*!< SysTick CALIB: NOREF Position */ +#define SysTick_CALIB_NOREF_Msk (1UL << SysTick_CALIB_NOREF_Pos) /*!< SysTick CALIB: NOREF Mask */ + +#define SysTick_CALIB_SKEW_Pos 30U /*!< SysTick CALIB: SKEW Position */ +#define SysTick_CALIB_SKEW_Msk (1UL << SysTick_CALIB_SKEW_Pos) /*!< SysTick CALIB: SKEW Mask */ + +#define SysTick_CALIB_TENMS_Pos 0U /*!< SysTick CALIB: TENMS Position */ +#define SysTick_CALIB_TENMS_Msk (0xFFFFFFUL /*<< SysTick_CALIB_TENMS_Pos*/) /*!< SysTick CALIB: TENMS Mask */ + +/*@} end of group CMSIS_SysTick */ + + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_CoreDebug Core Debug Registers (CoreDebug) + \brief Cortex-M0 Core Debug Registers (DCB registers, SHCSR, and DFSR) are only accessible over DAP and not via processor. + Therefore they are not covered by the Cortex-M0 header file. + @{ + */ +/*@} end of group CMSIS_CoreDebug */ + + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_core_bitfield Core register bit field macros + \brief Macros for use with bit field definitions (xxx_Pos, xxx_Msk). + @{ + */ + +/** + \brief Mask and shift a bit field value for use in a register bit range. + \param[in] field Name of the register bit field. + \param[in] value Value of the bit field. + \return Masked and shifted value. +*/ +#define _VAL2FLD(field, value) ((value << field ## _Pos) & field ## _Msk) + +/** + \brief Mask and shift a register value to extract a bit filed value. + \param[in] field Name of the register bit field. + \param[in] value Value of register. + \return Masked and shifted bit field value. +*/ +#define _FLD2VAL(field, value) ((value & field ## _Msk) >> field ## _Pos) + +/*@} end of group CMSIS_core_bitfield */ + + +/** + \ingroup CMSIS_core_register + \defgroup CMSIS_core_base Core Definitions + \brief Definitions for base addresses, unions, and structures. + @{ + */ + +/* Memory mapping of Cortex-M0 Hardware */ +#define SCS_BASE (0xE000E000UL) /*!< System Control Space Base Address */ +#define SysTick_BASE (SCS_BASE + 0x0010UL) /*!< SysTick Base Address */ +#define NVIC_BASE (SCS_BASE + 0x0100UL) /*!< NVIC Base Address */ +#define SCB_BASE (SCS_BASE + 0x0D00UL) /*!< System Control Block Base Address */ + +#define SCB ((SCB_Type *) SCB_BASE ) /*!< SCB configuration struct */ +#define SysTick ((SysTick_Type *) SysTick_BASE ) /*!< SysTick configuration struct */ +#define NVIC ((NVIC_Type *) NVIC_BASE ) /*!< NVIC configuration struct */ + + +/*@} */ + + + +/******************************************************************************* + * Hardware Abstraction Layer + Core Function Interface contains: + - Core NVIC Functions + - Core SysTick Functions + - Core Register Access Functions + ******************************************************************************/ +/** + \defgroup CMSIS_Core_FunctionInterface Functions and Instructions Reference +*/ + + + +/* ########################## NVIC functions #################################### */ +/** + \ingroup CMSIS_Core_FunctionInterface + \defgroup CMSIS_Core_NVICFunctions NVIC Functions + \brief Functions that manage interrupts and exceptions via the NVIC. + @{ + */ + +/* Interrupt Priorities are WORD accessible only under ARMv6M */ +/* The following MACROS handle generation of the register offset and byte masks */ +#define _BIT_SHIFT(IRQn) ( ((((uint32_t)(int32_t)(IRQn)) ) & 0x03UL) * 8UL) +#define _SHP_IDX(IRQn) ( (((((uint32_t)(int32_t)(IRQn)) & 0x0FUL)-8UL) >> 2UL) ) +#define _IP_IDX(IRQn) ( (((uint32_t)(int32_t)(IRQn)) >> 2UL) ) + + +/** + \brief Enable External Interrupt + \details Enables a device-specific interrupt in the NVIC interrupt controller. + \param [in] IRQn External interrupt number. Value cannot be negative. + */ +__STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn) +{ + NVIC->ISER[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL)); +} + + +/** + \brief Disable External Interrupt + \details Disables a device-specific interrupt in the NVIC interrupt controller. + \param [in] IRQn External interrupt number. Value cannot be negative. + */ +__STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn) +{ + NVIC->ICER[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL)); +} + + +/** + \brief Get Pending Interrupt + \details Reads the pending register in the NVIC and returns the pending bit for the specified interrupt. + \param [in] IRQn Interrupt number. + \return 0 Interrupt status is not pending. + \return 1 Interrupt status is pending. + */ +__STATIC_INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn) +{ + return((uint32_t)(((NVIC->ISPR[0U] & (1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL))) != 0UL) ? 1UL : 0UL)); +} + + +/** + \brief Set Pending Interrupt + \details Sets the pending bit of an external interrupt. + \param [in] IRQn Interrupt number. Value cannot be negative. + */ +__STATIC_INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn) +{ + NVIC->ISPR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL)); +} + + +/** + \brief Clear Pending Interrupt + \details Clears the pending bit of an external interrupt. + \param [in] IRQn External interrupt number. Value cannot be negative. + */ +__STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn) +{ + NVIC->ICPR[0U] = (uint32_t)(1UL << (((uint32_t)(int32_t)IRQn) & 0x1FUL)); +} + + +/** + \brief Set Interrupt Priority + \details Sets the priority of an interrupt. + \note The priority cannot be set for every core interrupt. + \param [in] IRQn Interrupt number. + \param [in] priority Priority to set. + */ +__STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) +{ + if ((int32_t)(IRQn) < 0) + { + SCB->SHP[_SHP_IDX(IRQn)] = ((uint32_t)(SCB->SHP[_SHP_IDX(IRQn)] & ~(0xFFUL << _BIT_SHIFT(IRQn))) | + (((priority << (8U - __NVIC_PRIO_BITS)) & (uint32_t)0xFFUL) << _BIT_SHIFT(IRQn))); + } + else + { + NVIC->IP[_IP_IDX(IRQn)] = ((uint32_t)(NVIC->IP[_IP_IDX(IRQn)] & ~(0xFFUL << _BIT_SHIFT(IRQn))) | + (((priority << (8U - __NVIC_PRIO_BITS)) & (uint32_t)0xFFUL) << _BIT_SHIFT(IRQn))); + } +} + + +/** + \brief Get Interrupt Priority + \details Reads the priority of an interrupt. + The interrupt number can be positive to specify an external (device specific) interrupt, + or negative to specify an internal (core) interrupt. + \param [in] IRQn Interrupt number. + \return Interrupt Priority. + Value is aligned automatically to the implemented priority bits of the microcontroller. + */ +__STATIC_INLINE uint32_t NVIC_GetPriority(IRQn_Type IRQn) +{ + + if ((int32_t)(IRQn) < 0) + { + return((uint32_t)(((SCB->SHP[_SHP_IDX(IRQn)] >> _BIT_SHIFT(IRQn) ) & (uint32_t)0xFFUL) >> (8U - __NVIC_PRIO_BITS))); + } + else + { + return((uint32_t)(((NVIC->IP[ _IP_IDX(IRQn)] >> _BIT_SHIFT(IRQn) ) & (uint32_t)0xFFUL) >> (8U - __NVIC_PRIO_BITS))); + } +} + + +/** + \brief System Reset + \details Initiates a system reset request to reset the MCU. + */ +__STATIC_INLINE void NVIC_SystemReset(void) +{ + __DSB(); /* Ensure all outstanding memory accesses included + buffered write are completed before reset */ + SCB->AIRCR = ((0x5FAUL << SCB_AIRCR_VECTKEY_Pos) | + SCB_AIRCR_SYSRESETREQ_Msk); + __DSB(); /* Ensure completion of memory access */ + + for(;;) /* wait until reset */ + { + __NOP(); + } +} + +/*@} end of CMSIS_Core_NVICFunctions */ + + + +/* ################################## SysTick function ############################################ */ +/** + \ingroup CMSIS_Core_FunctionInterface + \defgroup CMSIS_Core_SysTickFunctions SysTick Functions + \brief Functions that configure the System. + @{ + */ + +#if (__Vendor_SysTickConfig == 0U) + +/** + \brief System Tick Configuration + \details Initializes the System Timer and its interrupt, and starts the System Tick Timer. + Counter is in free running mode to generate periodic interrupts. + \param [in] ticks Number of ticks between two interrupts. + \return 0 Function succeeded. + \return 1 Function failed. + \note When the variable __Vendor_SysTickConfig is set to 1, then the + function SysTick_Config is not included. In this case, the file device.h + must contain a vendor-specific implementation of this function. + */ +__STATIC_INLINE uint32_t SysTick_Config(uint32_t ticks) +{ + if ((ticks - 1UL) > SysTick_LOAD_RELOAD_Msk) + { + return (1UL); /* Reload value impossible */ + } + + SysTick->LOAD = (uint32_t)(ticks - 1UL); /* set reload register */ + NVIC_SetPriority (SysTick_IRQn, (1UL << __NVIC_PRIO_BITS) - 1UL); /* set Priority for Systick Interrupt */ + SysTick->VAL = 0UL; /* Load the SysTick Counter Value */ + SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | + SysTick_CTRL_TICKINT_Msk | + SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */ + return (0UL); /* Function successful */ +} + +#endif + +/*@} end of CMSIS_Core_SysTickFunctions */ + + + + +#ifdef __cplusplus +} +#endif + +#endif /* __CORE_CM0_H_DEPENDANT */ + +#endif /* __CMSIS_GENERIC */ diff --git a/NUC123/inc/core_cmFunc.h b/NUC123/inc/core_cmFunc.h new file mode 100644 index 0000000..652a48a --- /dev/null +++ b/NUC123/inc/core_cmFunc.h @@ -0,0 +1,87 @@ +/**************************************************************************//** + * @file core_cmFunc.h + * @brief CMSIS Cortex-M Core Function Access Header File + * @version V4.30 + * @date 20. October 2015 + ******************************************************************************/ +/* Copyright (c) 2009 - 2015 ARM LIMITED + + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + - Neither the name of ARM nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + * + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + ---------------------------------------------------------------------------*/ + + +#if defined ( __ICCARM__ ) + #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #pragma clang system_header /* treat file as system include file */ +#endif + +#ifndef __CORE_CMFUNC_H +#define __CORE_CMFUNC_H + + +/* ########################### Core Function Access ########################### */ +/** \ingroup CMSIS_Core_FunctionInterface + \defgroup CMSIS_Core_RegAccFunctions CMSIS Core Register Access Functions + @{ +*/ + +/*------------------ RealView Compiler -----------------*/ +#if defined ( __CC_ARM ) + #include "cmsis_armcc.h" + +/*------------------ ARM Compiler V6 -------------------*/ +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #include "cmsis_armcc_V6.h" + +/*------------------ GNU Compiler ----------------------*/ +#elif defined ( __GNUC__ ) + #include "cmsis_gcc.h" + +/*------------------ ICC Compiler ----------------------*/ +#elif defined ( __ICCARM__ ) + #include + +/*------------------ TI CCS Compiler -------------------*/ +#elif defined ( __TMS470__ ) + #include + +/*------------------ TASKING Compiler ------------------*/ +#elif defined ( __TASKING__ ) + /* + * The CMSIS functions have been implemented as intrinsics in the compiler. + * Please use "carm -?i" to get an up to date list of all intrinsics, + * Including the CMSIS ones. + */ + +/*------------------ COSMIC Compiler -------------------*/ +#elif defined ( __CSMC__ ) + #include + +#endif + +/*@} end of CMSIS_Core_RegAccFunctions */ + +#endif /* __CORE_CMFUNC_H */ diff --git a/NUC123/inc/core_cmInstr.h b/NUC123/inc/core_cmInstr.h new file mode 100644 index 0000000..f474b0e --- /dev/null +++ b/NUC123/inc/core_cmInstr.h @@ -0,0 +1,87 @@ +/**************************************************************************//** + * @file core_cmInstr.h + * @brief CMSIS Cortex-M Core Instruction Access Header File + * @version V4.30 + * @date 20. October 2015 + ******************************************************************************/ +/* Copyright (c) 2009 - 2015 ARM LIMITED + + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + - Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + - Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + - Neither the name of ARM nor the names of its contributors may be used + to endorse or promote products derived from this software without + specific prior written permission. + * + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + ---------------------------------------------------------------------------*/ + + +#if defined ( __ICCARM__ ) + #pragma system_include /* treat file as system include file for MISRA check */ +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #pragma clang system_header /* treat file as system include file */ +#endif + +#ifndef __CORE_CMINSTR_H +#define __CORE_CMINSTR_H + + +/* ########################## Core Instruction Access ######################### */ +/** \defgroup CMSIS_Core_InstructionInterface CMSIS Core Instruction Interface + Access to dedicated instructions + @{ +*/ + +/*------------------ RealView Compiler -----------------*/ +#if defined ( __CC_ARM ) + #include "cmsis_armcc.h" + +/*------------------ ARM Compiler V6 -------------------*/ +#elif defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) + #include "cmsis_armcc_V6.h" + +/*------------------ GNU Compiler ----------------------*/ +#elif defined ( __GNUC__ ) + #include "cmsis_gcc.h" + +/*------------------ ICC Compiler ----------------------*/ +#elif defined ( __ICCARM__ ) + #include + +/*------------------ TI CCS Compiler -------------------*/ +#elif defined ( __TMS470__ ) + #include + +/*------------------ TASKING Compiler ------------------*/ +#elif defined ( __TASKING__ ) + /* + * The CMSIS functions have been implemented as intrinsics in the compiler. + * Please use "carm -?i" to get an up to date list of all intrinsics, + * Including the CMSIS ones. + */ + +/*------------------ COSMIC Compiler -------------------*/ +#elif defined ( __CSMC__ ) + #include + +#endif + +/*@}*/ /* end of group CMSIS_Core_InstructionInterface */ + +#endif /* __CORE_CMINSTR_H */ diff --git a/NUC123/inc/system_NUC123.h b/NUC123/inc/system_NUC123.h new file mode 100644 index 0000000..0ac7fba --- /dev/null +++ b/NUC123/inc/system_NUC123.h @@ -0,0 +1,66 @@ +/**************************************************************************//** + * @file system_NUC123.h + * @version V3.0 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series CMSIS System Header File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#ifndef __SYSTEM_NUC123_H +#define __SYSTEM_NUC123_H + +#ifdef __cplusplus +extern "C" { +#endif +/*---------------------------------------------------------------------------------------------------------*/ +/* Macro Definition */ +/*---------------------------------------------------------------------------------------------------------*/ +#ifndef DEBUG_PORT +# define DEBUG_PORT UART0 /*!< Select Debug Port which is used for retarget.c to output debug message to UART */ +#endif + +/*---------------------------------------------------------------------------- + Define SYSCLK + *----------------------------------------------------------------------------*/ +#define __HXT (12000000UL) /*!< External Crystal Clock Frequency */ +#define __LXT (32768UL) /*!< External Crystal Clock Frequency 32.768KHz */ +#define __HIRC (22118400UL) /*!< Internal 22M RC Oscillator Frequency */ +#define __LIRC (10000UL) /*!< Internal 10K RC Oscillator Frequency */ +#define __HSI (50000000UL) /*!< PLL default output is 50MHz */ + +extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ +extern uint32_t CyclesPerUs; /*!< Cycles per micro second */ +extern uint32_t PllClock; /*!< PLL Output Clock Frequency */ + +/** + * Initialize the system + * + * @param none + * @return none + * + * @brief Setup the microcontroller system + * Initialize GPIO directions and values + */ +extern void SystemInit(void); + + +/** + * Update SystemCoreClock variable + * + * @param none + * @return none + * + * @brief Updates the SystemCoreClock with current core Clock + * retrieved from CPU registers. + */ +extern void SystemCoreClockUpdate(void); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/NUC123/semihosting.h b/NUC123/semihosting.h new file mode 100644 index 0000000..3370854 --- /dev/null +++ b/NUC123/semihosting.h @@ -0,0 +1,116 @@ + +#ifndef ARM_SEMIHOSTING_H_ +#define ARM_SEMIHOSTING_H_ + +// ---------------------------------------------------------------------------- + +// Semihosting operations. +enum OperationNumber +{ + // Regular operations + SEMIHOSTING_EnterSVC = 0x17, + SEMIHOSTING_ReportException = 0x18, + SEMIHOSTING_SYS_CLOSE = 0x02, + SEMIHOSTING_SYS_CLOCK = 0x10, + SEMIHOSTING_SYS_ELAPSED = 0x30, + SEMIHOSTING_SYS_ERRNO = 0x13, + SEMIHOSTING_SYS_FLEN = 0x0C, + SEMIHOSTING_SYS_GET_CMDLINE = 0x15, + SEMIHOSTING_SYS_HEAPINFO = 0x16, + SEMIHOSTING_SYS_ISERROR = 0x08, + SEMIHOSTING_SYS_ISTTY = 0x09, + SEMIHOSTING_SYS_OPEN = 0x01, + SEMIHOSTING_SYS_READ = 0x06, + SEMIHOSTING_SYS_READC = 0x07, + SEMIHOSTING_SYS_REMOVE = 0x0E, + SEMIHOSTING_SYS_RENAME = 0x0F, + SEMIHOSTING_SYS_SEEK = 0x0A, + SEMIHOSTING_SYS_SYSTEM = 0x12, + SEMIHOSTING_SYS_TICKFREQ = 0x31, + SEMIHOSTING_SYS_TIME = 0x11, + SEMIHOSTING_SYS_TMPNAM = 0x0D, + SEMIHOSTING_SYS_WRITE = 0x05, + SEMIHOSTING_SYS_WRITEC = 0x03, + SEMIHOSTING_SYS_WRITE0 = 0x04, + + // Codes returned by SEMIHOSTING_ReportException + ADP_Stopped_ApplicationExit = ((2 << 16) + 38), + ADP_Stopped_RunTimeError = ((2 << 16) + 35), + +}; + +// ---------------------------------------------------------------------------- + +// SWI numbers and reason codes for RDI (Angel) monitors. +#define AngelSWI_ARM 0x123456 +#ifdef __thumb__ +#define AngelSWI 0xAB +#else +#define AngelSWI AngelSWI_ARM +#endif +// For thumb only architectures use the BKPT instruction instead of SWI. +#if defined(__ARM_ARCH_7M__) \ + || defined(__ARM_ARCH_7EM__) \ + || defined(__ARM_ARCH_6M__) +#define AngelSWIInsn "bkpt" +#define AngelSWIAsm bkpt +#else +#define AngelSWIInsn "swi" +#define AngelSWIAsm swi +#endif + +#if defined(OS_DEBUG_SEMIHOSTING_FAULTS) +// Testing the local semihosting handler cannot use another BKPT, since this +// configuration cannot trigger HaedFault exceptions while the debugger is +// connected, so we use an illegal op code, that will trigger an +// UsageFault exception. +#define AngelSWITestFault "setend be" +#define AngelSWITestFaultOpCode (0xB658) +#endif + +static inline int +__attribute__ ((always_inline)) +call_host (int reason, void* arg) +{ + int value; + asm volatile ( + + " mov r0, %[rsn] \n" + " mov r1, %[arg] \n" +#if defined(OS_DEBUG_SEMIHOSTING_FAULTS) + " " AngelSWITestFault " \n" +#else + " " AngelSWIInsn " %[swi] \n" +#endif + " mov %[val], r0" + + : [val] "=r" (value) /* Outputs */ + : [rsn] "r" (reason), [arg] "r" (arg), [swi] "i" (AngelSWI) /* Inputs */ + : "r0", "r1", "r2", "r3", "ip", "lr", "memory", "cc" + // Clobbers r0 and r1, and lr if in supervisor mode + ); + + // Accordingly to page 13-77 of ARM DUI 0040D other registers + // can also be clobbered. Some memory positions may also be + // changed by a system call, so they should not be kept in + // registers. Note: we are assuming the manual is right and + // Angel is respecting the APCS. + return value; +} + +// ---------------------------------------------------------------------------- + +// Function used in _exit() to return the status code as Angel exception. +static inline void +__attribute__ ((always_inline,noreturn)) +report_exception (int reason) +{ + call_host (SEMIHOSTING_ReportException, (void*) reason); + + for (;;) + ; +} + +// ---------------------------------------------------------------------------- + +#endif // ARM_SEMIHOSTING_H_ diff --git a/NUC123/startup_NUC123.s b/NUC123/startup_NUC123.s new file mode 100644 index 0000000..99df924 --- /dev/null +++ b/NUC123/startup_NUC123.s @@ -0,0 +1,232 @@ + .syntax unified + .arch armv8 - m.base + + .section .stack + .align 3 +#ifndef Stack_Size + .equ Stack_Size, 0x00000400 +#endif + .global __StackTop + .global __StackLimit +__StackLimit: + .space Stack_Size + .size __StackLimit, . - __StackLimit +__StackTop: + .size __StackTop, . - __StackTop + + .section .heap + .align 3 +#ifndef Heap_Size + .equ Heap_Size, 0x00000100 +#endif + + .global __HeapBase + .global __HeapLimit +__HeapBase: + .if Heap_Size + .space Heap_Size + .endif + .size __HeapBase, . - __HeapBase +__HeapLimit: + .size __HeapLimit, . - __HeapLimit + + .section .vectors + .align 2 + .global __Vectors +__Vectors: + .long __StackTop /* Top of Stack */ + .long Reset_Handler /* Reset Handler */ + .long NMI_Handler /* NMI Handler */ + .long HardFault_Handler /* Hard Fault Handler */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long SVC_Handler /* SVCall Handler */ + .long 0 /* Reserved */ + .long 0 /* Reserved */ + .long PendSV_Handler /* PendSV Handler */ + .long SysTick_Handler /* SysTick Handler */ + + /* External interrupts */ + + .long BOD_IRQHandler + .long WDT_IRQHandler + .long EINT0_IRQHandler + .long EINT1_IRQHandler + .long GPAB_IRQHandler + .long GPCDF_IRQHandler + .long PWMA_IRQHandler + .long Default_Handler + .long TMR0_IRQHandler + .long TMR1_IRQHandler + .long TMR2_IRQHandler + .long TMR3_IRQHandler + .long UART0_IRQHandler + .long UART1_IRQHandler + .long SPI0_IRQHandler + .long SPI1_IRQHandler + .long SPI2_IRQHandler + .long SPI3_IRQHandler + .long I2C0_IRQHandler + .long I2C1_IRQHandler + .long CAN0_IRQHandler + .long CAN1_IRQHandler + .long SC012_IRQHandler + .long USBD_IRQHandler + .long PS2_IRQHandler + .long ACMP_IRQHandler + .long PDMA_IRQHandler + .long I2S_IRQHandler + .long PWRWU_IRQHandler + .long ADC_IRQHandler + .long Default_Handler + .long RTC_IRQHandler + + .size __Vectors, . - __Vectors + + + + .text + .thumb + .thumb_func + .align 2 + .global Reset_Handler + .type Reset_Handler, % function + + +Reset_Handler: + /* Single section scheme. + * + * The ranges of copy from/to are specified by following symbols + * __etext: LMA of start of the section to copy from. Usually end of text + * __data_start__: VMA of start of the section to copy to + * __data_end__: VMA of end of the section to copy to + * + * All addresses must be aligned to 4 bytes boundary. + */ + ldr r1, = __etext + ldr r2, = __data_start__ + ldr r3, = __data_end__ + + subs r3, r2 + ble .L_loop1_done +.L_loop1: + subs r3, #4 + ldr r0, [r1, r3] + str r0, [r2, r3] + bgt .L_loop1 +.L_loop1_done: + + /* Single BSS section scheme. + * + * The BSS section is specified by following symbols + * __bss_start__: start of the BSS section. + * __bss_end__: end of the BSS section. + * + * Both addresses must be aligned to 4 bytes boundary. + */ + ldr r1, = __bss_start__ + ldr r2, = __bss_end__ + + movs r0, 0 + + subs r2, r1 + ble .L_loop3_done +.L_loop3: + subs r2, #4 + str r0, [r1, r2] + bgt .L_loop3 +.L_loop3_done: + + /* There's no SystemInit for NUC123, so no point making a pointless call */ + // bl SystemInit + +#ifndef __ENTRY +#define __ENTRY _entry +#endif + bl __ENTRY + + .pool + .size Reset_Handler, . - Reset_Handler + + .align 1 + .thumb_func + .weak Default_Handler + .type Default_Handler, % function + +Default_Handler: + b . + .size Default_Handler, . - Default_Handler + + /* Macro to define default handlers. Default handler + * will be weak symbol and just dead loops. They can be + * overwritten by other handlers */ + + .macro def_irq_handler handler_name + .weak \handler_name + .set \handler_name, Default_Handler + .endm + + def_irq_handler NMI_Handler + def_irq_handler HardFault_Handler + def_irq_handler SVC_Handler + def_irq_handler PendSV_Handler + def_irq_handler SysTick_Handler + + + def_irq_handler BOD_IRQHandler + def_irq_handler WDT_IRQHandler + def_irq_handler EINT0_IRQHandler + def_irq_handler EINT1_IRQHandler + def_irq_handler GPAB_IRQHandler + def_irq_handler GPCDF_IRQHandler + def_irq_handler PWMA_IRQHandler + def_irq_handler TMR0_IRQHandler + def_irq_handler TMR1_IRQHandler + def_irq_handler TMR2_IRQHandler + def_irq_handler TMR3_IRQHandler + def_irq_handler UART0_IRQHandler + def_irq_handler UART1_IRQHandler + def_irq_handler SPI0_IRQHandler + def_irq_handler SPI1_IRQHandler + def_irq_handler SPI2_IRQHandler + def_irq_handler SPI3_IRQHandler + def_irq_handler I2C0_IRQHandler + def_irq_handler I2C1_IRQHandler + def_irq_handler CAN0_IRQHandler + def_irq_handler CAN1_IRQHandler + def_irq_handler SC012_IRQHandler + def_irq_handler USBD_IRQHandler + def_irq_handler PS2_IRQHandler + def_irq_handler ACMP_IRQHandler + def_irq_handler PDMA_IRQHandler + def_irq_handler I2S_IRQHandler + def_irq_handler PWRWU_IRQHandler + def_irq_handler ADC_IRQHandler + def_irq_handler RTC_IRQHandler + + + /* ;int32_t SH_DoCommand(int32_t n32In_R0, int32_t n32In_R1, int32_t *pn32Out_R0) */ + .align 2 + .thumb_func + .type SH_DoCommand, % function + +SH_DoCommand: + + BKPT 0xAB /* ; Wait ICE or HardFault */ + //LDR R3, = SH_Return + MOV R4, lr + BLX R3 /* ; Call SH_Return. The return value is in R0 */ + BX R4 /* ; Return value = R0 */ + .size SH_DoCommand, . - SH_DoCommand + + .align 2 + .thumb_func + .global __PC + .type __PC, % function + + .end diff --git a/NUC123/system_NUC123.c b/NUC123/system_NUC123.c new file mode 100644 index 0000000..7b47694 --- /dev/null +++ b/NUC123/system_NUC123.c @@ -0,0 +1,80 @@ +/**************************************************************************//** + * @file system_NUC123.c + * @version V3.0 + * $Revision: 5 $ + * $Date: 15/07/02 11:21a $ + * @brief NUC123 Series CMSIS System File + * + * @note + * SPDX-License-Identifier: Apache-2.0 + * Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved. + * + ******************************************************************************/ +#include +#include "NUC123.h" + + +/*---------------------------------------------------------------------------- + Clock Variable definitions + *----------------------------------------------------------------------------*/ +uint32_t SystemCoreClock = __HSI; /*!< System Clock Frequency (Core Clock) */ +uint32_t CyclesPerUs = (__HSI / 1000000); /* Cycles per micro second */ +uint32_t PllClock = __HSI; /*!< PLL Output Clock Frequency */ +uint32_t gau32ClkSrcTbl[] = {__HXT, NULL, __HSI, __LIRC, NULL, NULL, NULL, __HIRC}; + + +/*---------------------------------------------------------------------------- + Clock functions + This function is used to update the variable SystemCoreClock + and must be called whenever the core clock is changed. + *----------------------------------------------------------------------------*/ +void SystemCoreClockUpdate(void) /* Get Core Clock Frequency */ +{ + uint32_t u32Freq, u32ClkSrc; + uint32_t u32HclkDiv; + + /* Update PLL Clock */ + PllClock = CLK_GetPLLClockFreq(); + + u32ClkSrc = CLK->CLKSEL0 & CLK_CLKSEL0_HCLK_S_Msk; + + if(u32ClkSrc == CLK_CLKSEL0_HCLK_S_PLL) + { + /* Use PLL clock */ + u32Freq = PllClock; + } + else if(u32ClkSrc == CLK_CLKSEL0_HCLK_S_PLL_DIV2) + { + /* Use PLL/2 clock */ + u32Freq = PllClock >> 1; + } + else + { + /* Use the clock sources directly */ + u32Freq = gau32ClkSrcTbl[u32ClkSrc]; + } + + u32HclkDiv = (CLK->CLKDIV & CLK_CLKDIV_HCLK_N_Msk) + 1; + + /* Update System Core Clock */ + SystemCoreClock = u32Freq / u32HclkDiv; + + CyclesPerUs = (SystemCoreClock + 500000) / 1000000; +} + +/*---------------------------------------------------------------------------------------------------------*/ +/* Function: SystemInit */ +/* */ +/* Parameters: */ +/* None */ +/* */ +/* Returns: */ +/* None */ +/* */ +/* Description: */ +/* The necessary initialization of system. */ +/* */ +/*---------------------------------------------------------------------------------------------------------*/ +void SystemInit(void) +{ +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..3ec4cbd --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +# TASOLLER Custom Firmware (Host MCU - Application ROM) + +See [Development](./Development.md) for development information. + +Yes, the code is a mess. I just wanted to get something onto git y'know :). + +## Configuration +Hold FN2 for configuration. It is not the same as stock DAO. + +- Pad 1/2 (cell 0): Left wing colour +- Pad 3/4 (cell 1): Ground colour +- Pad 5/6 (cell 2): Ground colour when pressed, and separator colour +- Pad 7/8 (cell 3): Right wing colour +- Pad 9/10 (cell 4): No function +- Pad 11/12 (cell 5): Toggle rainbow effect on/off +- Pad 13/14 (cell 6): Increase/decrease ground brightness +- Pad 15/16 (cell 7): Increase/decrease wing brightness +- Pad 17/18 (cell 8): No function +- Pad 19/20 (cell 9): No function +- Pad 21/22 (cell 10): No function +- Pad 23/24 (cell 11): No function +- Pad 25/26 (cell 12): Increase/decrease sensitivity +- Pad 27/28 (cell 13): No function +- Pad 29/30 (cell 14): Toggle HID keyboard mode +- Pad 31/32 (cell 15): Toggle IO4 emulation mode + +## Calibration +Make sure no hands or objects are near the slider before starting calibration. + +Hold FN1 for two seconds; the slider will flash red for a few seconds, then fill up with a blue bar. + +The slider will then briefly flash green. After this, begin to rub your hands across the slider as much as possible! The cells will turn increasingly green; the greener you can get them the better. Once the dividers have turned green you can save your calibration by pressing FN2. + +**Note:** Calibration is a separate process to adjusting the sensor sensitivity. It is recommended to re-calibrate after adjustment of sensitivity. For the best performance, run a re-calibration before every play session to account for changes in your room's temperature and humidity. diff --git a/flash.cmd b/flash.cmd new file mode 100644 index 0000000..eaf1458 --- /dev/null +++ b/flash.cmd @@ -0,0 +1,17 @@ +@..\..\openocd-0.12.0-3\bin\openocd.exe -f interface/stlink-v2.cfg -f ..\..\nucxxx.cfg ^ + -c "SysReset halt" ^ + -c "flash read_bank 1 dataflash.bin" ^ + -c "ChipErase" ^ + -c "exit" + +@..\..\openocd-0.12.0-3\bin\openocd.exe -f interface/stlink-v2.cfg -f ..\..\nucxxx.cfg ^ + -c "SysReset halt" ^ + -c "WriteConfigRegs 0xFFFFFF7F 0xFFFFFFFF" ^ + -c "ReadConfigRegs" ^ + -c "program ../bootloader/host_bl.bin 0x100000" ^ + -c "program host_aprom.bin 0" ^ + -c "program dataflash.bin 0x1F000" ^ + -c "SysReset aprom run" ^ + -c "exit" + +@del dataflash.bin diff --git a/generic.mk b/generic.mk new file mode 100644 index 0000000..6bb33b5 --- /dev/null +++ b/generic.mk @@ -0,0 +1,40 @@ +C_SOURCES = $(wildcard $(SRC_DIR)/*.c) +ASM_SOURCES = $(wildcard $(SRC_DIR)/*.s) + +C_OBJECTS = $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(C_SOURCES)) +ASM_OBJECTS = $(patsubst $(SRC_DIR)/%.s,$(OBJ_DIR)/%.o,$(ASM_SOURCES)) + +OBJECTS = $(C_OBJECTS) $(ASM_OBJECTS) $(DEVICE_OBJECTS) + +all: $(BINARY_NAME).bin +.DEFAULT_GOAL=all + +# Speed up compiles by not deleting these +.PRECIOUS: $(OBJ_DIR)/%.o +.PRECIOUS: $(OBJ_DIR)/%.elf + +%.bin: $(OBJ_DIR)/%.elf + @echo Creating binary $@ + @$(OBJCOPY) -O binary $< $@ + +$(OBJ_DIR)/%.elf: $(OBJECTS) + @echo Linking $@ + @$(LD) $(LDFLAGS) -o $@ $^ + @echo ============================================================================== + @$(SIZE) $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + @echo Compiling $< + @$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $< + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.s + @echo Compiling $< + @$(AS) $(ASFLAGS) -o $@ $< + +clean: + -del $(BINARY_NAME).bin + -rmdir /Q /S $(OBJ_DIR) + +$(shell mkdir $(OBJ_DIR)) + +.PHONY: all clean diff --git a/picolibc/.gitkeep b/picolibc/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/run.cmd b/run.cmd new file mode 100644 index 0000000..fb19986 --- /dev/null +++ b/run.cmd @@ -0,0 +1,4 @@ +@..\..\openocd-0.12.0-3\bin\openocd.exe -f interface/stlink-v2.cfg -f ..\..\nucxxx.cfg ^ + -c "SysReset halt" ^ + -c "SysReset aprom run" ^ + -c "exit" diff --git a/src/delay.s b/src/delay.s new file mode 100644 index 0000000..08349a3 --- /dev/null +++ b/src/delay.s @@ -0,0 +1,6 @@ +.syntax unified + .global DelayCycles +DelayCycles: + subs r0, r0, #1 + bcs DelayCycles + bx lr diff --git a/src/descriptors.c b/src/descriptors.c new file mode 100644 index 0000000..378c0ac --- /dev/null +++ b/src/descriptors.c @@ -0,0 +1,466 @@ +#include + +#include "tasoller.h" + +static const uint8_t IO4_ReportDescriptor[] = { + // Analog input (28 bytes) + HID_USAGE_PAGE(GENERIC_DESKTOP), + HID_USAGE(JOYSTICK), + HID_COLLECTION(APPLICATION), + HID_REPORT_ID(HID_REPORT_ID_IO4), + HID_USAGE(POINTER), + HID_COLLECTION(PHYSICAL), + // 8 ADC channels + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + // 4 Rotary channels + HID_USAGE(RX), + HID_USAGE(RY), + HID_USAGE(RX), + HID_USAGE(RY), + // 2 Coin chutes + HID_USAGE(SLIDER), + HID_USAGE(SLIDER), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(4, 65534), + HID_PHYSICAL_MINIMUM(1, 0), + HID_PHYSICAL_MAXIMUM(4, 65534), + HID_REPORT_COUNT(14), + HID_REPORT_SIZE(16), + HID_INPUT(DATA, VARIABLE, ABSOLUTE, NO_WRAP, LINEAR, PREFERRED_STATE, NO_NULL_POSITION), + HID_END_COLLECTION(PHYSICAL), + + // Digital input (6 bytes = 48 bits) + // [ 0~15]: Player 1 buttons + // [16~31]: Player 2 buttons + // [32~39]: System status + // -> 01h: ? + // -> 02h: ? + // -> 04h: ? + // -> 08h: ? + // -> 10h: Comm timeout set + // -> 20h: Sampling count set + // [40~47]: USB status + // -> 01h: ? + // -> 02h: ? + // -> 04h: ? (is set on timeout) + HID_USAGE_PAGE(SIMULATION), + HID_USAGE_PAGE(BUTTONS), + HID_USAGE_MINIMUM(1, 1), + HID_USAGE_MAXIMUM(1, 48), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(1, 1), + HID_PHYSICAL_MAXIMUM(1, 1), + HID_REPORT_SIZE(1), + HID_REPORT_COUNT(48), + HID_INPUT(DATA, VARIABLE, ABSOLUTE, NO_WRAP, LINEAR, PREFERRED_STATE, NO_NULL_POSITION), + + // Reserved for future use. Pad with null. (29 bytes) + HID_USAGE(UNDEFINED), + HID_REPORT_SIZE(8), + HID_REPORT_COUNT(29), + HID_INPUT(CONSTANT, ARRAY, ABSOLUTE, NO_WRAP, LINEAR, PREFERRED_STATE, NO_NULL_POSITION), + HID_USAGE_PAGE2(2, 0xFFA0), // Vendor defined FF0A + HID_USAGE(UNDEFINED), + + // General-purpose commands to the board. First byte is the command, then 62 data byte + HID_REPORT_ID(HID_REPORT_ID_IO4_CMD), + HID_COLLECTION(APPLICATION), + HID_USAGE(UNDEFINED), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(1, 255), + HID_REPORT_SIZE(8), + HID_REPORT_COUNT(63), + HID_OUTPUT(DATA, VARIABLE, ABSOLUTE, NO_WRAP, LINEAR, PREFERRED_STATE, NO_NULL_POSITION, + NON_VOLATILE), + HID_END_COLLECTION(APPLICATION), + + HID_END_COLLECTION(APPLICATION), +}; + +static const uint8_t Keyboard_ReportDescriptor[] = { + // Keyboard input descriptor + HID_USAGE_PAGE(GENERIC_DESKTOP), + HID_USAGE(KEYBOARD), + HID_COLLECTION(APPLICATION), + HID_REPORT_ID(HID_REPORT_ID_KEYBOARD), + + HID_USAGE_PAGE(KEYBOARD), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(1, 231), + HID_USAGE_MINIMUM(1, 0), + HID_USAGE_MAXIMUM(1, 231), + HID_REPORT_SIZE(8), + HID_REPORT_COUNT(NUM_FN + NUM_AIR + NUM_GROUND), + HID_INPUT(DATA, ARRAY, ABSOLUTE), + + HID_END_COLLECTION(APPLICATION), + + // Debugging reports descriptors (they dump the raw PSoC data) + HID_USAGE_PAGE(GENERIC_DESKTOP), + HID_USAGE(JOYSTICK), + HID_COLLECTION(APPLICATION), + + HID_REPORT_ID(HID_REPORT_ID_DEBUG_A), + HID_USAGE(POINTER), + HID_COLLECTION(LOGICAL), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(4, 0xffff), + HID_PHYSICAL_MINIMUM(1, 0), + HID_PHYSICAL_MAXIMUM(4, 0xffff), + HID_REPORT_COUNT(16), + HID_REPORT_SIZE(16), + HID_INPUT(DATA, VARIABLE, ABSOLUTE), + HID_END_COLLECTION(LOGICAL), + + HID_REPORT_ID(HID_REPORT_ID_DEBUG_B), + HID_USAGE(POINTER), + HID_COLLECTION(LOGICAL), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(4, 0xffff), + HID_PHYSICAL_MINIMUM(1, 0), + HID_PHYSICAL_MAXIMUM(4, 0xffff), + HID_REPORT_SIZE(16), + HID_REPORT_COUNT(16), + HID_INPUT(DATA, VARIABLE, ABSOLUTE), + HID_END_COLLECTION(LOGICAL), + + HID_END_COLLECTION(APPLICATION), +}; + +static const uint8_t Debug_ReportDescriptor[] = { + HID_USAGE_PAGE(GENERIC_DESKTOP), + HID_USAGE(JOYSTICK), + HID_COLLECTION(APPLICATION), + + HID_REPORT_ID(HID_REPORT_ID_DEBUG_A), + HID_USAGE(POINTER), + HID_COLLECTION(LOGICAL), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(4, 0xffff), + HID_PHYSICAL_MINIMUM(1, 0), + HID_PHYSICAL_MAXIMUM(4, 0xffff), + HID_REPORT_COUNT(16), + HID_REPORT_SIZE(16), + HID_INPUT(DATA, VARIABLE, ABSOLUTE), + HID_END_COLLECTION(LOGICAL), + + HID_REPORT_ID(HID_REPORT_ID_DEBUG_B), + HID_USAGE(POINTER), + HID_COLLECTION(LOGICAL), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_USAGE(X), + HID_USAGE(Y), + HID_LOGICAL_MINIMUM(1, 0), + HID_LOGICAL_MAXIMUM(4, 0xffff), + HID_PHYSICAL_MINIMUM(1, 0), + HID_PHYSICAL_MAXIMUM(4, 0xffff), + HID_REPORT_SIZE(16), + HID_REPORT_COUNT(16), + HID_INPUT(DATA, VARIABLE, ABSOLUTE), + HID_END_COLLECTION(LOGICAL), + + HID_END_COLLECTION(APPLICATION), +}; + +usb_device_descr_t gIO4DeviceDescriptor = { + sizeof(usb_device_descr_t), + DESC_DEVICE, + 0x0200, + USB_CLASS_UNSPECIFIED, + 0, + 0, + EP0_MAX_PKT_SIZE, + // Unfortunately we're forced to always use this VID if we want IO4 support to work + IO4_VID, + IO4_PID, + 0x0100, + USB_STRING_VENDOR, + USB_STRING_PRODUCT, + USB_STRING_SERIAL, + 1, +}; + +// We have a unified descriptor that covers +typedef struct __attribute__((packed)) { + const usb_desc_config_t Config; + + const usb_desc_iad_t CDC_IAD; + + const usb_desc_interface_t CDC_CMD_Interface; + const usb_desc_cdc_header_t CDC_Header; + const usb_desc_cdc_call_t CDC_Call; + const usb_desc_cdc_acm_t CDC_ACM; + const usb_desc_cdc_union_t CDC_Union; + const usb_desc_endpoint_t CDC_CMD_Endpoint; + + const usb_desc_interface_t CDC_Interface; + const usb_desc_endpoint_t CDC_IN_Endpoint; + const usb_desc_endpoint_t CDC_OUT_Endpoint; + + const usb_desc_interface_t HID_IO4_Interface; + const usb_desc_hid_t HID_IO4; + const usb_desc_endpoint_t HID_IO4_EndpointIn; + /** + * We're meant to have an OUT endpoint for IO4, but IO4 uses the Win32 WriteFile API, which will + * happily fall back to SET_REPORT calls on the control endpoints if there's no OUT endpoint. + * + * Chunithm never uses the output capability of IO4, so we have no high-frequency data that + * might choke our control endpoints--we just need to support the initial configuration packets. + */ + // const usb_desc_endpoint_t HID_EndpointOut; + + const usb_desc_interface_t HID_Misc_Interface; + const usb_desc_hid_t HID_Misc; + const usb_desc_endpoint_t HID_Misc_EndpointIn; + const usb_desc_endpoint_t HID_Misc_EndpointOut; +} config_desc_t; +static const config_desc_t gConfigDescriptor = { + // Config + { + sizeof(usb_desc_config_t), + DESC_CONFIG, + sizeof gConfigDescriptor, + _USBD_ITF_MAX, + 0x01, + 0x00, + 0x80 | (USBD_SELF_POWERED << 6) | (USBD_REMOTE_WAKEUP << 5), + USBD_MAX_POWER, + }, + + // CDC IAD + { + sizeof(usb_desc_iad_t), + DESC_IAD, + USBD_ITF_CDC_CMD, + 2, + USB_CLASS_CDC, + CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, + CDC_COMM_PROTOCOL_NONE, + USB_STRING_CDC, + }, + + // CDC Control + { + sizeof(usb_desc_interface_t), + DESC_INTERFACE, + USBD_ITF_CDC_CMD, + 0x00, + 0x01, + USB_CLASS_CDC, + CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL, + CDC_COMM_PROTOCOL_NONE, + 0, + }, + { + sizeof(usb_desc_cdc_header_t), + DESC_CS_INTERFACE, + CDC_FUNC_DESC_HEADER, + 0x0110, + }, + { + sizeof(usb_desc_cdc_call_t), + DESC_CS_INTERFACE, + CDC_FUNC_DESC_CALL_MANAGEMENT, + 0, + USBD_ITF_CDC_DAT, + }, + { + sizeof(usb_desc_cdc_acm_t), + DESC_CS_INTERFACE, + CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, + // Supports set line coding + 2, + }, + { + sizeof(usb_desc_cdc_union_t), + DESC_CS_INTERFACE, + CDC_FUNC_DESC_UNION, + USBD_ITF_CDC_CMD, + USBD_ITF_CDC_DAT, + }, + { + sizeof(usb_desc_endpoint_t), + DESC_ENDPOINT, + USBD_CDC_EP_CMD, + EP_INT, + USBD_CDC_CMD_MAX_SIZE, + 1, + }, + // CDC Data + { + sizeof(usb_desc_interface_t), + DESC_INTERFACE, + USBD_ITF_CDC_DAT, + 0, + 2, + USB_CLASS_CDC_DATA, + 0, + 0, + 0, + }, + { + sizeof(usb_desc_endpoint_t), + DESC_ENDPOINT, + USBD_CDC_EP_IN, + EP_BULK, + USBD_CDC_IN_MAX_SIZE, + 0, + }, + { + sizeof(usb_desc_endpoint_t), + DESC_ENDPOINT, + USBD_CDC_EP_OUT, + EP_BULK, + USBD_CDC_OUT_MAX_SIZE, + 0, + }, + + // IO4 HID + { + sizeof(usb_desc_interface_t), + DESC_INTERFACE, + USBD_ITF_HID_IO4, + 0x00, + 0x01, + USB_CLASS_HID, + 0, + HID_KEYBOARD, + USB_STRING_HID_IO4, + }, + { + sizeof(usb_desc_hid_t), + DESC_HID, + 0x0110, + 0x00, + 0x01, + DESC_HID_RPT, + sizeof IO4_ReportDescriptor, + }, + { + sizeof(usb_desc_endpoint_t), + DESC_ENDPOINT, + USBD_HID_IO4_EP_IN, + EP_INT, + USBD_HID_BUF_LEN, + HID_IO4_INT_IN_INTERVAL, + }, + + // Misc HID + { + sizeof(usb_desc_interface_t), + DESC_INTERFACE, + USBD_ITF_HID_MISC, + 0x00, + 0x01, + USB_CLASS_HID, + 0, + HID_KEYBOARD, + USB_STRING_HID_MISC, + }, + { + sizeof(usb_desc_hid_t), + DESC_HID, + 0x0110, + 0x00, + 0x01, + DESC_HID_RPT, + sizeof Keyboard_ReportDescriptor, + }, + { + sizeof(usb_desc_endpoint_t), + DESC_ENDPOINT, + USBD_HID_MISC_EP_IN, + EP_INT, + USBD_HID_BUF_LEN, + HID_DEFAULT_INT_IN_INTERVAL, + }, + { + sizeof(usb_desc_endpoint_t), + DESC_ENDPOINT, + USBD_HID_MISC_EP_OUT, + EP_INT, + USBD_HID_BUF_LEN, + HID_DEFAULT_INT_IN_INTERVAL, + }, +}; + +const char* gszVendorInitial = "Bottersnike"; +const char* gszVendor = IO4_VENDOR; +const char* gszProduct = "TASOLLER"; + +const usb_device_descr_t *gpDeviceDescriptor = &gIO4DeviceDescriptor; +const usb_desc_config_t *gpConfigDescriptor = &gConfigDescriptor.Config; +const uint32_t gu32HidDescIO4Offset = (offsetof(config_desc_t, HID_IO4)); +const uint32_t gu32HidDescMiscOffset = (offsetof(config_desc_t, HID_Misc)); +const uint32_t gu32UsbHidIO4ReportLen = sizeof IO4_ReportDescriptor; +const uint32_t gu32UsbHidMiscReportLen = sizeof Keyboard_ReportDescriptor; +const uint8_t* gpu8UsbHidIO4Report = (uint8_t*)IO4_ReportDescriptor; +const uint8_t* gpu8UsbHidMiscReport = (uint8_t*)Keyboard_ReportDescriptor; diff --git a/src/fmc.c b/src/fmc.c new file mode 100644 index 0000000..222fbe4 --- /dev/null +++ b/src/fmc.c @@ -0,0 +1,99 @@ +#include "tasoller.h" + +void FMC_Open(void) { FMC->ISPCON |= FMC_ISPCON_ISPEN_Msk; } +void FMC_Close(void) { FMC->ISPCON &= ~FMC_ISPCON_ISPEN_Msk; } + +int FMC_Proc(uint32_t u32Cmd, uint32_t addr_start, uint32_t addr_end, uint32_t *pu32Data) { + uint32_t u32Addr, Reg; + + for (u32Addr = addr_start; u32Addr < addr_end; pu32Data++) { + FMC->ISPCMD = u32Cmd; + FMC->ISPADR = u32Addr; + + if (u32Cmd == FMC_ISPCMD_PROGRAM) FMC->ISPDAT = *pu32Data; + + FMC->ISPTRG = 0x1; + __ISB(); + while (FMC->ISPTRG & 0x1) + ; // Wait for ISP command done. + + Reg = FMC->ISPCON; + if (Reg & FMC_ISPCON_ISPFF_Msk) { + FMC->ISPCON = Reg; + return -1; + } + + if (u32Cmd == FMC_ISPCMD_READ) *pu32Data = FMC->ISPDAT; + + if (u32Cmd == FMC_ISPCMD_PAGE_ERASE) { + u32Addr += FMC_FLASH_PAGE_SIZE; + } else { + u32Addr += 4; + } + } + + return 0; +} + +#define DATAFLASH_BASE (FMC->DFBADR) // 1F000 +// #define DATAFLASH_BASE (0x1F000) +// Dao has his data based at FE0. We're going to base ourself at 000 instead +#define DATAFLASH_EEPROM_BASE (DATAFLASH_BASE + 0x000) +#define DATAFLASH_VERSION (0x02) // Gets merged into the magic number +#define DATAFLASH_MAGIC (0x54617300 | DATAFLASH_VERSION) + +flash_t gConfig; + +void FMC_EEPROM_Load(void) { + FMC_Open(); + + FMC_ReadData(DATAFLASH_EEPROM_BASE, DATAFLASH_EEPROM_BASE + sizeof gConfig, (void *)&gConfig); + if (gConfig.u32Magic != DATAFLASH_MAGIC) { + // Zeroing flags first means GCC knows we don't care about the other bits + gConfig.u8Flags = 0; + gConfig.bEnableIO4 = 1; + gConfig.bEnableKeyboard = 0; + gConfig.bEnableRainbow = 1; + + gConfig.u8Sens = 8; + + gConfig.u16HueWingLeft = 330; + gConfig.u16HueWingRight = 180; + gConfig.u16HueGround = 45; + gConfig.u16HueGroundActive = 330; + + gConfig.u8LedGroundBrightness = 255; + gConfig.u8LedWingBrightness = 255; + + for (uint8_t i = 0; i < 32; i++) { + gConfig.u16PSoCScaleMin[i] = 0; + gConfig.u16PSoCScaleMax[i] = 2000; + } + + bConfigDirty = 1; + } + + FMC_Close(); +} +uint8_t bConfigDirty = 0; +void FMC_EEPROM_Store(void) { + if (!bConfigDirty) return; + bConfigDirty = 0; + + FMC_Open(); + FMC->ISPCON |= FMC_ISPCON_LDUEN_Msk; + FMC->ISPCON |= FMC_ISPCON_APUEN_Msk; + FMC_Erase(DATAFLASH_EEPROM_BASE); + + gConfig.u32Magic = DATAFLASH_MAGIC; + FMC_WriteData(DATAFLASH_EEPROM_BASE, DATAFLASH_EEPROM_BASE + sizeof gConfig, (void *)&gConfig); + + // We don't actually have anything to do if we fail, so there's no point checking for now ig + // // Check if our write succeeded or not + // if (FMC_Read(DATAFLASH_EEPROM_BASE) != DATAFLASH_MAGIC) + // ; + + FMC->ISPCON &= ~FMC_ISPCON_APUEN_Msk; + FMC->ISPCON &= ~FMC_ISPCON_LDUEN_Msk; + FMC_Close(); +} diff --git a/src/fmc_user.h b/src/fmc_user.h new file mode 100644 index 0000000..1fa7195 --- /dev/null +++ b/src/fmc_user.h @@ -0,0 +1,60 @@ +#pragma once + +#include + +#define Config0 FMC_CONFIG_BASE +#define Config1 FMC_CONFIG_BASE + 4 + +#define ISPGO 0x01 +#define _FMC_ENABLE_CFG_UPDATE() \ + (FMC->ISPCTL |= FMC_ISPCTL_CFGUEN_Msk) // Enable CONFIG Update Function +#define _FMC_DISABLE_CFG_UPDATE() \ + (FMC->ISPCTL &= ~FMC_ISPCTL_CFGUEN_Msk) // Disable CONFIG Update Function + +int FMC_Proc(uint32_t u32Cmd, uint32_t addr_start, uint32_t addr_end, uint32_t *data); + +#define FMC_EraseAP(u32Start, u32Size) \ + FMC_Proc(FMC_ISPCMD_PAGE_ERASE, (u32Start), (u32Start) + (u32Size), NULL) +#define FMC_ReadData(u32Start, u32End, pu32Data) \ + FMC_Proc(FMC_ISPCMD_READ, (u32Start), (u32End), (pu32Data)) +#define FMC_WriteData(u32Start, u32End, pu32Data) \ + FMC_Proc(FMC_ISPCMD_PROGRAM, (u32Start), (u32End), (pu32Data)) +#define FMC_ReadU32(u32Addr, pData) FMC_ReadData((u32Addr), (u32Addr) + 4, (pData)) +#define FMC_Erase_User(u32Addr) FMC_Proc(FMC_ISPCMD_PAGE_ERASE, (u32Addr), (u32Addr) + 4, 0) + +void FMC_Open(void); +void FMC_Close(void); +void FMC_EEPROM_Load(void); +void FMC_EEPROM_Store(void); + +#define FMC_EEPROM_VERSION 0x01 +typedef struct __attribute__((aligned(4), packed)) { + uint32_t u32Magic; + + // Flags + union { + struct __attribute__((packed)) { + uint8_t bEnableIO4 : 1; + uint8_t bEnableKeyboard : 1; + uint8_t bEnableRainbow : 1; + }; + uint8_t u8Flags; + }; + + // Only needs 4 bits but we aren't short atm + uint8_t u8Sens; // [1~16], Higher = more sensitive + + uint16_t u16HueWingLeft; + uint16_t u16HueWingRight; + uint16_t u16HueGround; + uint16_t u16HueGroundActive; + + uint8_t u8LedGroundBrightness; + uint8_t u8LedWingBrightness; + + // Calibration data + uint16_t u16PSoCScaleMin[32]; + uint16_t u16PSoCScaleMax[32]; +} flash_t; +extern flash_t gConfig; +extern uint8_t bConfigDirty; diff --git a/src/gpio.c b/src/gpio.c new file mode 100644 index 0000000..bd58a9b --- /dev/null +++ b/src/gpio.c @@ -0,0 +1,11 @@ +#include "tasoller.h" + +void GPIO_SetMode(GPIO_T *port, uint32_t u32PinMask, uint32_t u32Mode) { + uint32_t i; + + for (i = 0; i < GPIO_PIN_MAX; i++) { + if (u32PinMask & (1 << i)) { + port->PMD = (port->PMD & ~(0x3 << (i << 1))) | (u32Mode << (i << 1)); + } + } +} diff --git a/src/hid.c b/src/hid.c new file mode 100644 index 0000000..9c2a0c4 --- /dev/null +++ b/src/hid.c @@ -0,0 +1,198 @@ +#include "tasoller.h" + +#define HID_IO4_BUF ((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_HID_IO4_IN))) +#define HID_MISC_BUF ((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_HID_MISC_IN))) + +static const uint8_t u8GroundThreshold = 20; +static const uint8_t u8GroundKeymap[32] = { + KEY_I, KEY_COMMA, // + KEY_8, KEY_K, // + KEY_U, KEY_M, // + KEY_7, KEY_J, // + KEY_Y, KEY_N, // + KEY_6, KEY_H, // + KEY_T, KEY_B, // + KEY_5, KEY_G, // + KEY_R, KEY_V, // + KEY_4, KEY_F, // + KEY_E, KEY_C, // + KEY_3, KEY_D, // + KEY_W, KEY_X, // + KEY_2, KEY_S, // + KEY_Q, KEY_Z, // + KEY_1, KEY_A, // +}; +static const uint8_t u8AirKeymap[6] = { + HID_KEYBOARD_SLASH_AND_QUESTION_MARK, // VK_OEM_2 + HID_KEYBOARD_PERIOD_AND_GREATER_THAN, // VK_OEM_PERIOD + HID_KEYBOARD_QUOTE_AND_DOUBLEQUOTE, // VK_OEM_7 + HID_KEYBOARD_SEMICOLON_AND_COLON, // VK_OEM_1 + HID_KEYBOARD_RIGHT_BRACKET_AND_RIGHT_CURLY_BRACE, // VK_OEM_6 + HID_KEYBOARD_LEFT_BRACKET_AND_LEFT_CURLY_BRACE, // VK_OEM_4 +}; +static inline void _HID_Keyboard_Tick(uint8_t bReal) { + hid_report_t *buf = (hid_report_t *)HID_MISC_BUF; + + memset(buf, 0, sizeof *buf); + buf->bReportId = HID_REPORT_ID_KEYBOARD; + uint8_t kI = 0; + + if (bReal) { + if (gu8DigitalButtons & 0x01) buf->bKeyboard[kI++] = HID_KEYBOARD_F1; + if (gu8DigitalButtons & 0x02) buf->bKeyboard[kI++] = HID_KEYBOARD_F2; + if (gu8DigitalButtons & 0x04) buf->bKeyboard[kI++] = u8AirKeymap[0]; + if (gu8DigitalButtons & 0x08) buf->bKeyboard[kI++] = u8AirKeymap[1]; + if (gu8DigitalButtons & 0x10) buf->bKeyboard[kI++] = u8AirKeymap[2]; + if (gu8DigitalButtons & 0x20) buf->bKeyboard[kI++] = u8AirKeymap[3]; + if (gu8DigitalButtons & 0x40) buf->bKeyboard[kI++] = u8AirKeymap[4]; + if (gu8DigitalButtons & 0x80) buf->bKeyboard[kI++] = u8AirKeymap[5]; + + for (int i = 0; i < 32; i++) { + if (gu8GroundData[i] > u8GroundThreshold) buf->bKeyboard[kI++] = u8GroundKeymap[i]; + } + } + + USBD_SET_PAYLOAD_LEN(EP_HID_MISC_IN, sizeof *buf); +} + +#define IO4_BUTTON_TEST (1 << 9) +#define IO4_BUTTON_SERVICE (1 << 6) + +#define IO4_CMD_SET_COMM_TIMEOUT 0x01 +#define IO4_CMD_SET_SAMPLING_COUNT 0x02 +#define IO4_CMD_CLEAR_BOARD_STATUS 0x03 +#define IO4_CMD_SET_GENERAL_OUTPUT 0x04 +#define IO4_CMD_SET_PWM_OUTPUT 0x05 +#define IO4_CMD_SET_UNIQUE_OUTPUT 0x41 +#define IO4_CMD_UPDATE_FIRMWARE 0x85 + +volatile uint8_t u8IO4SystemStatus = 0; +volatile uint8_t u8IO4USBStatus = 0; +volatile uint16_t u16IO4CommTimeout = 0; +volatile uint8_t u8IO4SamplingCount = 0; + +static void _HID_IO4_Prepare(volatile uint8_t *pu8EpBuf) { + io4_hid_in_t *buf = (io4_hid_in_t *)pu8EpBuf; + + memset(buf, 0, sizeof *buf); + buf->bReportId = HID_REPORT_ID_IO4; + + // System buttons + if (gu8DigitalButtons & 0x01) buf->wButtons[0] |= IO4_BUTTON_TEST; + if (gu8DigitalButtons & 0x02) buf->wButtons[0] |= IO4_BUTTON_SERVICE; + // Airs + if (!(gu8DigitalButtons & 0x04)) buf->wButtons[0] |= 1 << 13; + if (!(gu8DigitalButtons & 0x08)) buf->wButtons[1] |= 1 << 13; + if (!(gu8DigitalButtons & 0x10)) buf->wButtons[0] |= 1 << 12; + if (!(gu8DigitalButtons & 0x20)) buf->wButtons[1] |= 1 << 12; + if (!(gu8DigitalButtons & 0x40)) buf->wButtons[0] |= 1 << 11; + if (!(gu8DigitalButtons & 0x80)) buf->wButtons[1] |= 1 << 11; + + buf->bUsbStatus = u8IO4USBStatus; + buf->bSystemStatus = u8IO4SystemStatus; +} +static void _HID_IO4_Tick() { + _HID_IO4_Prepare(HID_IO4_BUF); + + // We must send data every 8ms! None of that "only sending changed keys" stuff + // Trigger a write + gu8HIDIO4Ready = 0; + USBD_SET_PAYLOAD_LEN(EP_HID_IO4_IN, sizeof(io4_hid_in_t)); +} +static void _HID_Debug_Tick() { + debug_hid_report_t *buf = (debug_hid_report_t *)HID_MISC_BUF; + memset(buf, 0, sizeof *buf); + + static uint8_t bWhich = 0; + if ((bWhich++) & 1) { + buf->bReportId = HID_REPORT_ID_DEBUG_A; + for (uint8_t i = 0; i < 32; i += 2) buf->wData[i / 2] = gu8GroundData[i]; + } else { + buf->bReportId = HID_REPORT_ID_DEBUG_B; + for (uint8_t i = 1; i < 32; i += 2) buf->wData[i / 2] = gu8GroundData[i]; + } + USBD_SET_PAYLOAD_LEN(EP_HID_MISC_IN, sizeof *buf); +} + +static void _HID_Misc_Tick() { + static uint8_t sbLastEnableKeyboard = 0; + if (gConfig.bEnableKeyboard) { + sbLastEnableKeyboard = 1; + _HID_Keyboard_Tick(1); + } else if (sbLastEnableKeyboard) { + // If we've just disabled the keyboard, make sure to send a packet with all keys released! + sbLastEnableKeyboard = 0; + _HID_Keyboard_Tick(0); + } + // TODO: gbEnableDebug (we'll need to use a toggle to alternate) + + gu8HIDMiscReady = 0; +} + +void USBD_HID_PrepareReport() { + if (gu8HIDIO4Ready) _HID_IO4_Tick(); + if (gu8HIDMiscReady) _HID_Misc_Tick(); +} +static uint8_t sIO4InBuffer[sizeof(io4_hid_in_t)] = { 0 }; +uint8_t *USBD_HID_GetReport(uint8_t u8ReportId, uint32_t *pu32Size) { + switch (u8ReportId) { + case HID_REPORT_ID_IO4: + if (!gConfig.bEnableIO4) return NULL; + _HID_IO4_Prepare(sIO4InBuffer); + *pu32Size = sizeof sIO4InBuffer; + return sIO4InBuffer; + default: + return NULL; + } +} +void USBD_HID_SetReport(volatile uint8_t *pu8EpBuf, uint32_t u32Size) { + // TODO: is pu8EpBuf[0] the report ID? + // We need to switch on that report ID so we know what we're doing! + if (!gConfig.bEnableIO4) return; + + if (u32Size < 2) return; + switch (pu8EpBuf[1]) { + case IO4_CMD_SET_COMM_TIMEOUT: + if (u32Size >= 2 + 1) { + u16IO4CommTimeout = (uint16_t)pu8EpBuf[2] * 200; + u8IO4SystemStatus |= 0x10; + + gu8HIDIO4Ready = 1; + _HID_IO4_Tick(); + } + break; + case IO4_CMD_SET_SAMPLING_COUNT: + if (u32Size >= 2 + 1) { + u8IO4SamplingCount = pu8EpBuf[2]; + u8IO4SystemStatus |= 0x20; + + gu8HIDIO4Ready = 1; + _HID_IO4_Tick(); + } + break; + case IO4_CMD_CLEAR_BOARD_STATUS: + u8IO4SystemStatus &= 0x0F; + u8IO4USBStatus &= 0x04; + + gu8HIDIO4Ready = 1; + _HID_IO4_Tick(); + break; + case IO4_CMD_SET_GENERAL_OUTPUT: + if (u32Size >= 2 + 3) { + // 20 bits of data for GPO (+4 of padding) + } + break; + case IO4_CMD_SET_PWM_OUTPUT: + if (u32Size >= 2 + 0) { + // 0 bytes of data for PWM duty cycles (IO4 has no PWM!) + } + break; + case IO4_CMD_SET_UNIQUE_OUTPUT: + if (u32Size >= 2 + 62) { + // 62 bytes of unique output data + } + break; + case IO4_CMD_UPDATE_FIRMWARE: + break; + } +} diff --git a/src/led.c b/src/led.c new file mode 100644 index 0000000..7e119a0 --- /dev/null +++ b/src/led.c @@ -0,0 +1,412 @@ +#include "tasoller.h" + +hsv_t gaControlledIntLedData[LED_NUM_GROUND] = { 0 }; +uint8_t gbLedDataIsControlledInt = 0; +uint8_t gu8aControlledExtLedData[32 * 3]; +uint8_t gbLedDataIsControlledExt = 0; + +volatile uint8_t gu8LEDTx[LED_Tx_BUFFER]; +static void (*s_I2C1HandlerFn)(uint32_t u32Status) = NULL; + +volatile static uint8_t su8LedTxDataLock = 0; + +// Helper definitions +#define SLAVE_RX_ADDR_ACK 0x60 +#define SLAVE_RX_ACK 0x80 +#define I2C_SLAVE_RX_NACK 0x88 +#define I2C_SLAVE_TX_REPEAT_START_STOP 0xA0 +#define SLAVE_TX_ACK 0xA8 +#define I2C_SLAVE_TX_NACK 0xC0 + +void I2C1_IRQHandler(void) { + if (I2C_GET_TIMEOUT_FLAG(I2C1)) { + I2C_ClearTimeoutFlag(I2C1); + } else { + if (s_I2C1HandlerFn != NULL) (s_I2C1HandlerFn)(I2C1->I2CSTATUS); + } +} + +void I2C1_SlaveTx(uint32_t u32Status) { + static uint8_t su8I2CReadAddr = 0; + + switch (u32Status) { + case SLAVE_RX_ACK: + su8I2CReadAddr = I2C1->I2CDAT; + su8LedTxDataLock = 1; + I2C_SET_CONTROL_REG(I2C1, I2C_I2CON_SI_AA); + break; + case SLAVE_TX_ACK: + I2C_SET_DATA(I2C1, gu8LEDTx[su8I2CReadAddr]); + I2C_SET_CONTROL_REG(I2C1, I2C_I2CON_SI_AA); + break; + + case SLAVE_RX_ADDR_ACK: + case I2C_SLAVE_TX_NACK: + case I2C_SLAVE_RX_NACK: + case I2C_SLAVE_TX_REPEAT_START_STOP: + I2C_SET_CONTROL_REG(I2C1, I2C_I2CON_SI_AA); + break; + + default: + // Hmm? + I2C_SET_CONTROL_REG(I2C1, I2C_I2CON_SI_AA); + break; + } +} + +void LED_I2C1_Init(void) { + I2C_Open(I2C1, 100000); + I2C_SetSlaveAddr(I2C1, 0, 0x18, 0); + I2C_SetSlaveAddr(I2C1, 1, 0x30, 0); + I2C_SetSlaveAddr(I2C1, 2, 0x55, 0); + I2C_SetSlaveAddr(I2C1, 3, 0x18, 0); + I2C_SetSlaveAddrMask(I2C1, 0, 1); + I2C_SetSlaveAddrMask(I2C1, 1, 4); + I2C_SetSlaveAddrMask(I2C1, 2, 1); + I2C_SetSlaveAddrMask(I2C1, 3, 4); + I2C_EnableInt(I2C1); + NVIC_EnableIRQ(I2C1_IRQn); + + // I2C1 enter no address SLV mode + I2C_SET_CONTROL_REG(I2C1, I2C_I2CON_SI_AA); + + s_I2C1HandlerFn = I2C1_SlaveTx; +} + +// static inline void LEDTxLock(void) { +// gu8LEDTx[0] = 0; +// } +// static inline void LEDTxCommit(uint8_t u8Command, uint8_t u8NExpected) { +// gu8LEDTx[0] = u8Command; +// } + +/** + * @brief Convert from RGB to HSV + * + * @param pu8aRGB Destination 3-tuple to receive RGB values + * @param u16H Hue, ranging 0~LED_HUE_MAX + * @param u8S Saturation, ranging 0~255 + * @param u8V Value, ranging 0~255 + */ +void HsvToRgb(uint8_t* pu8aRGB, uint16_t u16H, uint8_t u8S, uint8_t u8V) { + if (u8S == 0) { + pu8aRGB[0] = u8V; + pu8aRGB[1] = u8V; + pu8aRGB[2] = u8V; + return; + } + + uint8_t region = u16H / (LED_HUE_MAX / 6); + uint8_t remainder = (u16H - (region * (LED_HUE_MAX / 6))) * (255 / (LED_HUE_MAX / 6)); + + uint8_t p = (u8V * (255 - u8S)) >> 8; + uint8_t q = (u8V * (255 - ((u8S * remainder) >> 8))) >> 8; + uint8_t t = (u8V * (255 - ((u8S * (255 - remainder)) >> 8))) >> 8; + + switch (region) { + case 0: + pu8aRGB[0] = u8V; + pu8aRGB[1] = t; + pu8aRGB[2] = p; + break; + case 1: + pu8aRGB[0] = q; + pu8aRGB[1] = u8V; + pu8aRGB[2] = p; + break; + case 2: + pu8aRGB[0] = p; + pu8aRGB[1] = u8V; + pu8aRGB[2] = t; + break; + case 3: + pu8aRGB[0] = p; + pu8aRGB[1] = q; + pu8aRGB[2] = u8V; + break; + case 4: + pu8aRGB[0] = t; + pu8aRGB[1] = p; + pu8aRGB[2] = u8V; + break; + default: + pu8aRGB[0] = u8V; + pu8aRGB[1] = p; + pu8aRGB[2] = q; + break; + } + + return; +} + +// 0x00: Normal operation (all other values ignore ground data) +// 0x01: [Stock] Uses colours. Bar 1 full (from right) +// 0x02: [Stock] Uses colours. Bar 2 full (from right) +// 0x03: [Stock] Uses colours. Bar 3 full (from right) +// 0x04: [Stock] Uses colours. Bar 4 full (from right) +// 0x1X: [Stock] All white with black gaps. X bars black (from right) +// 0x1X: [CFW] Rainbow with black gaps. X bars black (from right) +// 0x20: [CFW] Flashes three times +// 0x80: [Stock] Flashes three times +static uint8_t su8LedSpecial = 0x05; + +// 0: Separator bar every 4 (4 sections) +// 1: Separator bar every 2 (8 sections) +// 2: Separator bar every 1 (16 sections) +// 3: No separator bars +// 4~7: LEDs off +// For some reason this value can be |8, even though firmware suggests otherwise +static uint8_t su8LedSeparators = 1; + +// 0: Invalid, but functions as 1 +// 1: 32-key mode +// 2: 16-key mode (same as 32key mode) +// 3: 8-key mode (bars light in pairs) +// 4: 4-key mode (bars light in quads) +static uint8_t su8LedNKey = 1; + +// 0x40: Turns off ground LEDs +// 0x80: Turns off wing LEDs +static uint8_t su8LedOff = 0; + +// 0x8X: Separator 1~(X+1) lit (ie X ranges from 0~E; F is the same as E) +static uint8_t su8LedCfwRainbow = 0x8F; + +void LED_WriteBasicGrounds(void) { + gu8LEDTx[0] = LED_CMD_BASIC; + + // 32 bits of grounds + // (01,02)=key1, (04,08)=key2 (10,20)=key3, (40,80)=key4 + gu8LEDTx[1] = 0; + gu8LEDTx[2] = 0; + gu8LEDTx[3] = 0; + gu8LEDTx[4] = 0; + for (uint8_t i = 0; i < 4; i++) { + for (uint8_t j = 0; j < 8; j++) { + if (gu8GroundData[i * 8 + j] > PSoC_INTERNAL_DIGITAL_TH) gu8LEDTx[i + 1] |= (1 << j); + } + } + +#ifdef LED_FIRMWARE_CFW + gu8LEDTx[5] = 0; // Wing fill + for (uint8_t i = 0; i < 6; i++) + if (gu8DigitalButtons & (1 << (i + 2))) gu8LEDTx[5] |= 1 << i; + gu8LEDTx[6] = su8LedCfwRainbow; + gu8LEDTx[7] = 0; // Unused +#else + // Wings + gu8LEDTx[5] = 0; // Wing fill + for (uint8_t i = 0; i < 6; i++) + if (gu8DigitalButtons & (1 << (i + 2))) gu8LEDTx[5] |= 1 << i; + gu8LEDTx[6] = gConfig.u16HueWingLeft / LED_HUE_SCALE; // Hue left (default 330) + gu8LEDTx[7] = gConfig.u16HueWingRight / LED_HUE_SCALE; // Hue right (default 180) +#endif + + // Unused in CFW + gu8LEDTx[8] = gConfig.u16HueGround / LED_HUE_SCALE; // Hue ground inactive (default 45) + gu8LEDTx[9] = gConfig.u16HueGroundActive / LED_HUE_SCALE; // Hue ground active (default 330) + + // In CFW only su8LedOff is respected + gu8LEDTx[10] = (su8LedSeparators << 4) | su8LedOff | su8LedNKey; + gu8LEDTx[11] = su8LedSpecial; +} + +static const uint8_t su8aWingSensors[LED_NUM_LEFT] = { + DIGITAL_AIR1_Msk, DIGITAL_AIR1_Msk, DIGITAL_AIR1_Msk, DIGITAL_AIR1_Msk, // + DIGITAL_AIR2_Msk, DIGITAL_AIR2_Msk, DIGITAL_AIR2_Msk, DIGITAL_AIR2_Msk, // + DIGITAL_AIR3_Msk, DIGITAL_AIR3_Msk, DIGITAL_AIR3_Msk, DIGITAL_AIR3_Msk, // + DIGITAL_AIR4_Msk, DIGITAL_AIR4_Msk, DIGITAL_AIR4_Msk, DIGITAL_AIR4_Msk, // + DIGITAL_AIR5_Msk, DIGITAL_AIR5_Msk, DIGITAL_AIR5_Msk, DIGITAL_AIR5_Msk, // + DIGITAL_AIR6_Msk, DIGITAL_AIR6_Msk, DIGITAL_AIR6_Msk, DIGITAL_AIR6_Msk, // +}; + +#define SATURATION_ACTIVE 255 +#define SATURATION_INACTIVE 240 +#define VALUE_ACTIVE (gConfig.u8LedWingBrightness) +#define VALUE_INACTIVE (gConfig.u8LedWingBrightness / 2) + +static void LED_OffGround(void) { + memset((uint8_t*)&gu8LEDTx[LED_DATA_OFFSET], 0, LED_NUM_GROUND * 3); +} +static void LED_OffWings(void) { + memset((uint8_t*)&gu8LEDTx[LED_DATA_OFFSET + LED_NUM_GROUND * 3], 0, + (LED_NUM_LEFT + LED_NUM_RIGHT) * 3); +} +static void LED_AirWings(void) { + uint8_t u8aRgbActive[3]; + uint8_t u8aRgbInactive[3]; + + uint8_t j, i = LED_NUM_GROUND; + + // Left wing + HsvToRgb(u8aRgbActive, gConfig.u16HueWingLeft, SATURATION_ACTIVE, VALUE_ACTIVE); + HsvToRgb(u8aRgbInactive, gConfig.u16HueWingLeft, SATURATION_INACTIVE, VALUE_INACTIVE); + for (j = 0; i < LED_NUM_GROUND + LED_NUM_LEFT; i++, j++) { + // GRB + if (gu8DigitalButtons & su8aWingSensors[LED_NUM_LEFT - j - 1]) { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aRgbActive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aRgbActive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aRgbActive[2]; + } else { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aRgbInactive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aRgbInactive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aRgbInactive[2]; + } + } + + // Right wing + HsvToRgb(u8aRgbActive, gConfig.u16HueWingRight, SATURATION_ACTIVE, VALUE_ACTIVE); + HsvToRgb(u8aRgbInactive, gConfig.u16HueWingRight, SATURATION_INACTIVE, VALUE_INACTIVE); + for (j = 0; i < LED_NUM_GROUND + LED_NUM_LEFT + LED_NUM_RIGHT; i++, j++) { + // GRB + if (gu8DigitalButtons & su8aWingSensors[j]) { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aRgbActive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aRgbActive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aRgbActive[2]; + } else { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aRgbInactive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aRgbInactive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aRgbInactive[2]; + } + } +} + +static uint8_t LED_ScaleU8(uint8_t u8V, uint8_t u8Scale) { + return ((uint16_t)u8V * (uint16_t)u8Scale) / 255; +} + +static void LED_GroundRainbow(void) { + uint8_t u8aRGB[3]; + + // 5 ticks * 360 hue = 1800 calls for one cycle (1.8s) + static uint16_t u16Hue = 0; + static uint8_t u8Ticker = 0; + if (++u8Ticker == 5) { + u8Ticker = 0; + u16Hue++; + if (u16Hue == LED_HUE_MAX) u16Hue = 0; + } + + /** + * There are 48 LEDs for ground, but we only send 31 values + * They're mapped to the LEDs as follows: + * 00a11b22c33d44f55g66h77i... + * + * That is, we can't split-colour a key :P + */ + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + uint8_t v = 190; + uint8_t h = 0; + uint8_t nCell = i >> 1; + if (i % 2 == 0) { + if (gu16PSoCDigital & (1 << nCell)) { + v = 255; + h = LED_HUE_MAX / 2; + } + } else if (nCell % 4 == 3) { + h = LED_HUE_MAX / 2; + } + + // GRB + HsvToRgb(u8aRGB, (u16Hue + h + (i * (LED_HUE_MAX / LED_NUM_GROUND))) % LED_HUE_MAX, v, + LED_ScaleU8(v - 63, gConfig.u8LedGroundBrightness)); + + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aRGB[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aRGB[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aRGB[2]; + } +} +static void LED_GroundStatic(void) { + uint8_t u8aGround[3]; + uint8_t u8aGroundActive[3]; + + HsvToRgb(u8aGround, gConfig.u16HueGround, 255, gConfig.u8LedGroundBrightness); + HsvToRgb(u8aGroundActive, gConfig.u16HueGroundActive, 255, gConfig.u8LedGroundBrightness); + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + const uint8_t nCell = i >> 1; + if (i % 2 == 0) { + // This is a cell. Light it according to the touch input + if (gu16PSoCDigital & (1 << nCell)) { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aGroundActive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aGroundActive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aGroundActive[2]; + } else { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aGround[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aGround[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aGround[2]; + } + } else if (nCell % 4 == 3) { + // This is a separating divider. Light it with the active colour + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aGroundActive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aGroundActive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aGroundActive[2]; + } else { + // This is a non-separating divider. Light it based on the two cells either side + if (gu16PSoCDigital & (1 << nCell) && gu16PSoCDigital & (1 << (nCell + 1))) { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aGroundActive[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aGroundActive[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aGroundActive[2]; + } else { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aGround[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aGround[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aGround[2]; + } + } + } +} + +void LED_WriteRGB(void) { + gu8LEDTx[0] = LED_CMD_RGB_FULL; + +#ifdef LED_FIRMWARE_CFW + // "CFW" added this byte + gu8LEDTx[1] = su8LedSpecial | su8LedOff; +#endif + + // Even when grounds are disabled, internal control overrides that + if (gbLedDataIsControlledInt) { + PIN_LED_GROUND_PWR = 1; + + uint8_t u8aRGB[3]; + // Convert from HSV to GRB + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + HsvToRgb(u8aRGB, gaControlledIntLedData[LED_NUM_GROUND - i - 1].u16H, + gaControlledIntLedData[LED_NUM_GROUND - i - 1].u8S, + gaControlledIntLedData[LED_NUM_GROUND - i - 1].u8V); + + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = u8aRGB[1]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = u8aRGB[0]; + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = u8aRGB[2]; + } + } else if (gConfig.u8LedGroundBrightness) { + PIN_LED_GROUND_PWR = 1; + + if (gbLedDataIsControlledExt) { + // Swap from BRG to GRB + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 0] = + LED_ScaleU8(gu8aControlledExtLedData[i * 3 + 2], gConfig.u8LedGroundBrightness); + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 1] = + LED_ScaleU8(gu8aControlledExtLedData[i * 3 + 1], gConfig.u8LedGroundBrightness); + gu8LEDTx[LED_DATA_OFFSET + i * 3 + 2] = + LED_ScaleU8(gu8aControlledExtLedData[i * 3 + 0], gConfig.u8LedGroundBrightness); + } + } else if (gConfig.bEnableRainbow) { + LED_GroundRainbow(); + } else { + LED_GroundStatic(); + } + } else { + PIN_LED_GROUND_PWR = 0; + LED_OffGround(); + } + + if (gConfig.u8LedWingBrightness) { + PIN_LED_WING_PWR = 1; + // TODO: Get data from game when gbLedDataIsControlledExt (HID, probably) + LED_AirWings(); + } else { + PIN_LED_WING_PWR = 0; + LED_OffWings(); + } +} diff --git a/src/led.h b/src/led.h new file mode 100644 index 0000000..63cdd6e --- /dev/null +++ b/src/led.h @@ -0,0 +1,73 @@ +#pragma once + +#include + +#define LED_FIRMWARE_CFW +#ifndef LED_FIRMWARE_CFW +#define LED_DATA_OFFSET 1 +#else +#define LED_DATA_OFFSET 2 +#endif + +// LED count definitions +#define LED_NUM_GROUND 31 +#define LED_NUM_LEFT 24 +#define LED_NUM_RIGHT 24 + +// HSV definitions +#define LED_HUE_SCALE 5 // For transmission to LED board in basic mode +#define LED_HUE_MAX 360 + +// Host->LED MCU commands +#define LED_CMD_BASIC 0xA5 +#define LED_CMD_RGB_FULL 0x5A + +// LED index names +#define LED_CELL_0 0 +#define LED_CELL_1 2 +#define LED_CELL_2 4 +#define LED_CELL_3 6 +#define LED_CELL_4 8 +#define LED_CELL_5 10 +#define LED_CELL_6 12 +#define LED_CELL_7 14 +#define LED_CELL_8 16 +#define LED_CELL_9 18 +#define LED_CELL_10 20 +#define LED_CELL_11 22 +#define LED_CELL_12 24 +#define LED_CELL_13 26 +#define LED_CELL_14 28 +#define LED_CELL_15 30 + +#define LED_DIVIDER_0_1 1 +#define LED_DIVIDER_1_2 3 +#define LED_DIVIDER_2_3 5 +#define LED_DIVIDER_3_4 7 +#define LED_DIVIDER_4_5 9 +#define LED_DIVIDER_5_6 11 +#define LED_DIVIDER_6_7 13 +#define LED_DIVIDER_7_8 15 +#define LED_DIVIDER_8_9 17 +#define LED_DIVIDER_9_10 19 +#define LED_DIVIDER_10_11 21 +#define LED_DIVIDER_11_12 23 +#define LED_DIVIDER_12_13 25 +#define LED_DIVIDER_13_14 27 +#define LED_DIVIDER_14_15 29 + +typedef struct { + uint16_t u16H; + uint8_t u8S; + uint8_t u8V; +} hsv_t; +// Internal LED control, from the settings UI (gets priority) +extern hsv_t gaControlledIntLedData[LED_NUM_GROUND]; +extern uint8_t gbLedDataIsControlledInt; +// External LED control, from the game +extern uint8_t gu8aControlledExtLedData[32 * 3]; +extern uint8_t gbLedDataIsControlledExt; + +void LED_I2C1_Init(void); +void LED_WriteBasicGrounds(void); +void LED_WriteRGB(void); diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..e529768 --- /dev/null +++ b/src/main.c @@ -0,0 +1,138 @@ +#include "tasoller.h" + +uint8_t volatile gu8HIDIO4Ready = 0; +uint8_t volatile gu8HIDMiscReady = 0; + +#define DEBOUNCE_COUNT 8 +uint8_t TickDebouncer(uint8_t u8Buttons) { + static uint8_t u8Counters[8] = { 0 }; + uint8_t debounced = 0; + for (uint8_t i = 0; i < 8; i++) { + if (u8Buttons & (1 << i)) + u8Counters[i] = 0; + else if (u8Counters[i] < DEBOUNCE_COUNT) + u8Counters[i]++; + + if (u8Counters[i] < DEBOUNCE_COUNT) debounced |= (1 << i); + } + return debounced; +} + +uint8_t gu8DigitalButtons; +#define DELAY_FN 2 +#define DELAY_AIR 2 +void Digital_TickInputs() { + uint8_t u8Buttons = 0; + + GPIO_SetMode(_PIN_FN1, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_FN); + GPIO_SetMode(_PIN_FN1, GPIO_PMD_QUASI); + u8Buttons |= PIN_FN1 ? 0 : DIGITAL_FN1_Msk; + + GPIO_SetMode(_PIN_FN2, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_FN); + GPIO_SetMode(_PIN_FN2, GPIO_PMD_QUASI); + u8Buttons |= PIN_FN2 ? 0 : DIGITAL_FN2_Msk; + + GPIO_SetMode(_PIN_AIR1, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_AIR); + GPIO_SetMode(_PIN_AIR1, GPIO_PMD_QUASI); + u8Buttons |= PIN_AIR1 ? DIGITAL_AIR1_Msk : 0; + + GPIO_SetMode(_PIN_AIR2, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_AIR); + GPIO_SetMode(_PIN_AIR2, GPIO_PMD_QUASI); + u8Buttons |= PIN_AIR2 ? DIGITAL_AIR2_Msk : 0; + + GPIO_SetMode(_PIN_AIR3, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_AIR); + GPIO_SetMode(_PIN_AIR3, GPIO_PMD_QUASI); + u8Buttons |= PIN_AIR3 ? DIGITAL_AIR3_Msk : 0; + + GPIO_SetMode(_PIN_AIR4, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_AIR); + GPIO_SetMode(_PIN_AIR4, GPIO_PMD_QUASI); + u8Buttons |= PIN_AIR4 ? DIGITAL_AIR4_Msk : 0; + + GPIO_SetMode(_PIN_AIR5, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_AIR); + GPIO_SetMode(_PIN_AIR5, GPIO_PMD_QUASI); + u8Buttons |= PIN_AIR5 ? DIGITAL_AIR5_Msk : 0; + + GPIO_SetMode(_PIN_AIR6, GPIO_PMD_INPUT); + DelayCycles_Small(DELAY_AIR); + GPIO_SetMode(_PIN_AIR6, GPIO_PMD_QUASI); + u8Buttons |= PIN_AIR6 ? DIGITAL_AIR6_Msk : 0; + + gu8DigitalButtons = TickDebouncer(u8Buttons); +} + +int _entry(void) { + SYS_UnlockReg(); + SYS_Init(); +#ifdef ENABLE_BOOTLOADER_CHECK + SYS_Bootloader_Check(); +#endif + SYS_ModuleInit(); + + FMC_EEPROM_Load(); + + gu8VcomReady = 1; + gu8HIDIO4Ready = 1; + gu8HIDMiscReady = 1; + + /** + * Unfortunately, everything related to this seems to be a bit broken at the moment + * For some reason, this code gets into infinite loops on cold boots, even with 5 seconds of + * delay added first. + * To make matters worse, if we ever actually call PSoCSetFingerCapacitance it ends up with + * huge asymmetry between the two PSoCs, which is just unworkable. + */ + // uint16_t u16InitialFingerCap = PSoCGetFingerCapacitance(); + // if (u16InitialFingerCap != gu16PSoCFingerCap) { + // PSoCSetFingerCapacitance(gu16PSoCFingerCap); + // } + + static uint32_t su32NowMs = 0; + su32NowMs++; + while (1) { + USB_VCOM_Tick(); + + if (bPSoCDirty) { + bPSoCDirty = 0; + PSoC_PostProcessing(); + } + + Slider_TickSerial(); + USBD_HID_PrepareReport(); + + // Limit this to 250us so our debouncer has a chance to function + // 250us is the resolution of our timer currently, though we could increase it if needed + if (gu8Do250usTick) { + Digital_TickInputs(); + } + + if (gu8Do1msTick) { + su32NowMs++; + gu8Do1msTick = 0; + + Slider_Tick1ms(); + + PSoC_DigitalCalc(); + UI_Tick(); + + // LED_WriteBasicGrounds(); + LED_WriteRGB(); + } /* else { + DelayCycles(45); + }*/ + + // Wait until we know both PSoC are awake and talking before we attempt configure them + if (gu8PSoCSeenData == 0b011) { + PSoC_SetFingerCapacitanceFromConfig(); + gu8PSoCSeenData |= 0b100; + } + + FMC_EEPROM_Store(); + } +} diff --git a/src/pins.h b/src/pins.h new file mode 100644 index 0000000..b4d4b6a --- /dev/null +++ b/src/pins.h @@ -0,0 +1,64 @@ +#define PIN_SDA PA10 +#define PIN_SCL PA11 +#define PIN_USB_MUX_EN PB1 +#define PIN_USB_MUX_SEL PB0 +#define PIN_FN1 PB7 +#define PIN_FN2 PB2 +#define PIN_EC1 PB3 +#define PIN_EC2 PC5 +#define PIN_EC3 PC4 +#define PIN_RX2 PD9 +#define PIN_RX3 PD10 +#define PIN_RX4 PD11 +#define PIN_LED_WING_PWR PB13 +#define PIN_LED_GROUND_PWR PC3 + +#define _PIN_SDA PA, BIT10 +#define _PIN_SCL PA, BIT11 +#define _PIN_USB_MUX_EN PB, BIT1 +#define _PIN_USB_MUX_SEL PB, BIT0 +#define _PIN_FN1 PB, BIT7 +#define _PIN_FN2 PB, BIT2 +#define _PIN_EC1 PB, BIT3 +#define _PIN_EC2 PC, BIT5 +#define _PIN_EC3 PC, BIT4 +#define _PIN_RX2 PD, BIT9 +#define _PIN_RX3 PD, BIT10 +#define _PIN_RX4 PD, BIT11 +#define _PIN_LED_WING_PWR PB, BIT13 +#define _PIN_LED_GROUND_PWR PC, BIT3 + +#define PIN_AIR1 PIN_RX2 +#define PIN_AIR2 PIN_EC1 +#define PIN_AIR3 PIN_RX3 +#define PIN_AIR4 PIN_EC2 +#define PIN_AIR5 PIN_RX4 +#define PIN_AIR6 PIN_EC3 +#define _PIN_AIR1 _PIN_RX2 +#define _PIN_AIR2 _PIN_EC1 +#define _PIN_AIR3 _PIN_RX3 +#define _PIN_AIR4 _PIN_EC2 +#define _PIN_AIR5 _PIN_RX4 +#define _PIN_AIR6 _PIN_EC3 + +#define USB_MUX_ENABLE 0 +#define USB_MUX_DISABLE 1 +#define USB_MUX_HOST 0 +#define USB_MUX_LEDS 1 + +#define DIGITAL_FN1_Pos 0 +#define DIGITAL_FN2_Pos 1 +#define DIGITAL_AIR1_Pos 2 +#define DIGITAL_AIR2_Pos 3 +#define DIGITAL_AIR3_Pos 4 +#define DIGITAL_AIR4_Pos 5 +#define DIGITAL_AIR5_Pos 6 +#define DIGITAL_AIR6_Pos 7 +#define DIGITAL_FN1_Msk (1 << DIGITAL_FN1_Pos) +#define DIGITAL_FN2_Msk (1 << DIGITAL_FN2_Pos) +#define DIGITAL_AIR1_Msk (1 << DIGITAL_AIR1_Pos) +#define DIGITAL_AIR2_Msk (1 << DIGITAL_AIR2_Pos) +#define DIGITAL_AIR3_Msk (1 << DIGITAL_AIR3_Pos) +#define DIGITAL_AIR4_Msk (1 << DIGITAL_AIR4_Pos) +#define DIGITAL_AIR5_Msk (1 << DIGITAL_AIR5_Pos) +#define DIGITAL_AIR6_Msk (1 << DIGITAL_AIR6_Pos) diff --git a/src/psoc.c b/src/psoc.c new file mode 100644 index 0000000..6f94266 --- /dev/null +++ b/src/psoc.c @@ -0,0 +1,292 @@ +#include "tasoller.h" + +uint16_t gu16PSoCDiff[32] = { 0 }; + +uint32_t gu32PSoCDigital = 0; +uint32_t gu32PSoCDigitalPos = 0; +uint32_t gu32PSoCDigitalNeg = 0; +uint32_t gu32PSoCDigitalTrig = 0; +uint16_t gu16PSoCDigital = 0; +uint16_t gu16PSoCDigitalPos = 0; +uint16_t gu16PSoCDigitalNeg = 0; +uint16_t gu16PSoCDigitalTrig = 0; + +uint8_t gu8PSoCSeenData = 0; + +volatile uint8_t bPSoCDirty = 0; + +static void* pu8PsocRxDestination = NULL; +static uint8_t pu8PsocRxDestinationLen = 0; +static volatile uint8_t* pu8PsocGotData = NULL; +static void PSoC_HandleRx(PSoC_CMD_RX eCmd, uint8_t u8Len, uint8_t* u8Data) { + if (eCmd != PSoC_CMD_RX_SLAVE_DIFF && eCmd != PSoC_CMD_RX_MASTER_DIFF && + eCmd != PSoC_CMD_RX_CS_START) { + static uint8_t debug = 0; + debug++; + } + + uint8_t u8PsocDataOffset = 0; + switch (eCmd) { + case PSoC_CMD_RX_REQUEST_FINGER_CAP: + PSoC_SetFingerCapacitanceFromConfig(); + return; + + // We don't care about these + case PSoC_CMD_RX_CS_START: + case PSoC_CMD_RX_CS_END: + return; + + case PSoC_CMD_RX_SLAVE_DIFF: + u8PsocDataOffset = 16; + gu8PSoCSeenData |= 1; + // Falls through + case PSoC_CMD_RX_MASTER_DIFF: + if (eCmd == PSoC_CMD_RX_MASTER_DIFF) gu8PSoCSeenData |= 2; + if (u8Len != 32) return; + for (uint8_t i = 0; i < 16; i++) { + gu16PSoCDiff[u8PsocDataOffset + i] = u8Data[(i * 2)] << 8; + gu16PSoCDiff[u8PsocDataOffset + i] |= u8Data[(i * 2) + 1]; + } + bPSoCDirty = 1; + return; + + // Arbitrary data reception + case PSoC_CMD_RX_MASTER_TOUCH_TH: + case PSoC_CMD_RX_SLAVE_TOUCH_TH: + case PSoC_CMD_RX_MASTER_FINGER_TH: + case PSoC_CMD_RX_MASTER_HYSTERESIS: + case PSoC_CMD_RX_SLAVE_FINGER_TH: + case PSoC_CMD_RX_SLAVE_HYSTERESIS: + case PSoC_CMD_RX_GET_FINGER_CAP: + case PSoC_CMD_RX_SET_FINGER_CAP: + if (pu8PsocRxDestination) { + if (u8Len > pu8PsocRxDestinationLen) u8Len = pu8PsocRxDestinationLen; + memcpy(pu8PsocRxDestination, u8Data, u8Len); + // Make sure we don't go clobbering stuff later! + pu8PsocRxDestination = NULL; + } + if (pu8PsocGotData) { + *pu8PsocGotData = 1; + // Make sure we don't go clobbering stuff later! + pu8PsocGotData = NULL; + } + return; + } +} +void UART1_IRQHandler(void) { + static uint8_t su8RxData[32] = { 0 }; + static uint8_t su8State = 0; + static PSoC_CMD_RX eCmd = 0; + static uint8_t su8Len = 0; + static uint8_t su8Index = 0; + static uint8_t su8Sum = 0; + + // We only care about data interrupts + if (!(UART1->ISR & (UART_ISR_RDA_INT_Msk | UART_ISR_TOUT_INT_Msk))) return; + + uint8_t u8Data = UART_READ(UART1); + + switch (su8State) { + case 0: + eCmd = u8Data; // For debugging + switch (u8Data) { + // Just go away + case PSoC_CMD_RX_CS_START: + case PSoC_CMD_RX_CS_END: + return; + // Commands with no payload + case PSoC_CMD_RX_SET_FINGER_CAP: + PSoC_HandleRx(u8Data, 0, NULL); + return; + + // Commands that have a payload we need to receive + case PSoC_CMD_RX_REQUEST_FINGER_CAP: + case PSoC_CMD_RX_MASTER_DIFF: + case PSoC_CMD_RX_SLAVE_DIFF: + case PSoC_CMD_RX_MASTER_TOUCH_TH: + case PSoC_CMD_RX_SLAVE_TOUCH_TH: + case PSoC_CMD_RX_MASTER_FINGER_TH: + case PSoC_CMD_RX_MASTER_HYSTERESIS: + case PSoC_CMD_RX_SLAVE_FINGER_TH: + case PSoC_CMD_RX_SLAVE_HYSTERESIS: + case PSoC_CMD_RX_GET_FINGER_CAP: + eCmd = u8Data; + su8Sum = u8Data; + su8State++; + return; + } + return; + case 1: + su8Len = u8Data; + su8Index = 0; + su8Sum += u8Data; + su8State++; + // If there's no payload to receive, skip that step + if (!su8Len) su8State++; + // Packets too large for reception need dropped + if (su8Len > sizeof su8RxData) su8State = 0; + return; + case 2: + su8Sum += u8Data; + su8RxData[su8Index++] = u8Data; + if (su8Index == su8Len) su8State++; + return; + case 3: + // Only process packets with a valid checksum + if (su8Sum == u8Data) PSoC_HandleRx(eCmd, su8Len, su8RxData); + su8State = 0; + return; + + // If we somehow land in an invalid state (should be impossible), make sure we can recover + default: + su8State = 0; + return; + } +} + +static inline void PSoC_Cmd(PSoC_CMD_TX eCmd, uint8_t u8D0, uint8_t u8D1, uint8_t u8Blocking) { + static uint8_t su8Ready = 0; + if (u8Blocking) { + su8Ready = 0; + pu8PsocGotData = &su8Ready; + } + + static uint8_t u8Packet[5]; + u8Packet[0] = eCmd; + u8Packet[1] = 2; + u8Packet[2] = u8D0; + u8Packet[3] = u8D1; + u8Packet[4] = eCmd + 2 + u8D0 + u8D1; + for (uint8_t i = 0; i < 5; i++) { + while (UART_IS_TX_FULL(UART1)) + ; + UART_WRITE(UART1, u8Packet[i]); + } + + if (u8Blocking) { + while (!su8Ready) + ; + } +} + +uint16_t PSoC_GetFingerCapacitance(void) { + uint16_t u16FingerCap; + + pu8PsocRxDestination = &u16FingerCap; + pu8PsocRxDestinationLen = sizeof u16FingerCap; + PSoC_Cmd(PSoC_CMD_TX_GET_FINGER_CAP, 0, 0, 1); + + return u16FingerCap; + // Swap endian + // return (u16FingerCap >> 8) | ((u16FingerCap & 0xff) << 8); +} +void PSoC_GetDebug(PSoC_CMD_DEBUG u8Cmd, uint8_t* pu8Data, volatile uint8_t* pu8Ready) { + pu8PsocGotData = pu8Ready; + pu8PsocRxDestination = pu8Data; + pu8PsocRxDestinationLen = 32; + PSoC_Cmd(PSoC_CMD_TX_GET_DEBUG, 0, u8Cmd, 0); +} +void PSoC_SetFingerCapacitance(uint16_t u16FingerCap) { + if (u16FingerCap > 1023) u16FingerCap = 1023; + // PSoC_Cmd(PSoC_CMD_TX_SET_FINGER_CAP, u16FingerCap >> 8, u16FingerCap & 0xff, 1); + PSoC_Cmd(PSoC_CMD_TX_SET_FINGER_CAP, u16FingerCap >> 8, u16FingerCap & 0xff, 1); +} +void PSoC_SetFingerCapacitanceFromConfig(void) { + PSoC_SetFingerCapacitance(PSoC_FINGER_CAP_MIN + (16 - gConfig.u8Sens) * PSoC_FINGER_CAP_STEP); +} +void PSoC_EnableDebug(uint8_t u8D1, uint8_t u8D2) { + // TODO: Finish figuring out what values these two bytes need + PSoC_Cmd(PSoC_CMD_TX_ENABLE_DEBUG, u8D1, u8D2, 1); +} + +uint8_t gu8GroundData[32]; + +// These are used to uniformly narrow the min/max gap +// #define SCALE_OFFSET_MIN 150 +// #define SCALE_OFFSET_MIN 100 +// #define SCALE_OFFSET_MAX 450 + +void PSoC_PostProcessing(void) { + // Process the raw PSoC data to compute our external 0-255 values + for (uint8_t i = 0; i < 32; i++) { + uint8_t j = (15 - (i / 2)) * 2 + (i & 1); + + const uint16_t u16Pad = gu16PSoCDiff[i]; + + const uint16_t u16Min = gConfig.u16PSoCScaleMin[i]; // + SCALE_OFFSET_MIN; + const uint16_t u16Max = gConfig.u16PSoCScaleMax[i]; // - SCALE_OFFSET_MAX; + + if (u16Pad < u16Min) { + gu8GroundData[j] = 0; + } else if (u16Pad > u16Max) { + gu8GroundData[j] = 255; + } else { + // Multiply by 255 first to retain precision + // This can overflow u16, hence the u32 cast pre-mult + gu8GroundData[j] = ((uint32_t)(u16Pad - u16Min) * 255) / (u16Max - u16Min); + } + } +} +/** + * @brief Calculate digital input data from PSoC values + * + * Must be called at exactly 1ms intervals + * + * Trigger will activate once, a 200ms delay, then again every 5ms. + */ +void PSoC_DigitalCalc(void) { + // Calculate digital data for 32 pads + uint32_t u32Previous = gu32PSoCDigital; + gu32PSoCDigital = 0; + for (uint8_t i = 0; i < 32; i++) { + if (gu8GroundData[i] > PSoC_INTERNAL_DIGITAL_TH) { + gu32PSoCDigital |= (1 << i); + } + } + gu32PSoCDigitalPos = gu32PSoCDigital & ~u32Previous; + gu32PSoCDigitalNeg = u32Previous & ~gu32PSoCDigital; + gu32PSoCDigitalTrig = gu32PSoCDigitalPos; + + // Calculate digital data for 16 cells + uint16_t u16Previous = gu16PSoCDigital; + gu16PSoCDigital = 0; + for (uint8_t i = 0; i < 16; i++) { + if (gu8GroundData[i * 2] > PSoC_INTERNAL_DIGITAL_TH || + gu8GroundData[i * 2 + 1] > PSoC_INTERNAL_DIGITAL_TH) { + gu16PSoCDigital |= (1 << i); + } + } + gu16PSoCDigitalPos = gu16PSoCDigital & ~u16Previous; + gu16PSoCDigitalNeg = u16Previous & ~gu16PSoCDigital; + gu16PSoCDigitalTrig = gu16PSoCDigitalPos; + + static uint8_t u8TriggerCounter = 0; + if (++u8TriggerCounter != 5) return; + u8TriggerCounter = 0; + + static uint8_t u8a16TriggerCounter[16] = { 0 }; + for (uint8_t i = 0; i < 16; i++) { + if (!(gu16PSoCDigital & (1 << i))) { + u8a16TriggerCounter[i] = 0; + } else if (u8a16TriggerCounter[i] == 200 / 5) { + // High-speed repeat + gu16PSoCDigitalTrig |= (1 << i); + } else { + // Counter up to high-speed + u8a16TriggerCounter[i]++; + } + } + + static uint8_t u8a32TriggerCounter[32] = { 0 }; + for (uint8_t i = 0; i < 32; i++) { + if (!(gu32PSoCDigital & (1 << i))) { + u8a32TriggerCounter[i] = 0; + } else if (u8a32TriggerCounter[i] == 200 / 5) { + // High-speed repeat + gu32PSoCDigitalTrig |= (1 << i); + } else { + // Counter up to high-speed + u8a32TriggerCounter[i]++; + } + } +} diff --git a/src/psoc.h b/src/psoc.h new file mode 100644 index 0000000..d40ac06 --- /dev/null +++ b/src/psoc.h @@ -0,0 +1,110 @@ +#pragma once + +#include + +#define PSoC_DATA_SIZE 32 +#define PSoC_PACKET_SIZE (2 + PSoC_DATA_SIZE) + +// Used for calculation of PSoC_FINGER_CAP_MIN +// The difference between CAP and MIN must be divisible by 8 +#define PSoC_FINGER_CAP_MIN 20 // 0.2pF +#define PSoC_DEFAULT_CAP 340 // 3.2pF +// Maximum is CAP*2-MIN*2 ie 6.0pF + +#define PSoC_FINGER_CAP_STEP ((PSoC_DEFAULT_CAP - PSoC_FINGER_CAP_MIN) / 8) + +typedef enum { + // Host -> PSoC + PSoC_CMD_TX_GET_FINGER_CAP = 0xB5, + PSoC_CMD_TX_GET_DEBUG = 0xB6, + PSoC_CMD_TX_SET_FINGER_CAP = 0xBD, + PSoC_CMD_TX_ENABLE_DEBUG = 0xDE, +} PSoC_CMD_TX; +typedef enum { + // PSoC -> Host + PSoC_CMD_RX_MASTER_DIFF = 0xAD, + PSoC_CMD_RX_SLAVE_DIFF = 0xAF, + PSoC_CMD_RX_REQUEST_FINGER_CAP = 0xC1, + + // PSoC -> Host (Debug) + PSoC_CMD_RX_MASTER_TOUCH_TH = 0xAA, + PSoC_CMD_RX_SLAVE_TOUCH_TH = 0xAB, + PSoC_CMD_RX_MASTER_FINGER_TH = 0xA0, + PSoC_CMD_RX_MASTER_HYSTERESIS = 0xA1, + PSoC_CMD_RX_SLAVE_FINGER_TH = 0xA2, + PSoC_CMD_RX_SLAVE_HYSTERESIS = 0xA3, + PSoC_CMD_RX_CS_START = 0x00, + PSoC_CMD_RX_CS_END = 0xFF, + + // Packets that get a direct response + PSoC_CMD_RX_GET_FINGER_CAP = PSoC_CMD_TX_GET_FINGER_CAP, + PSoC_CMD_RX_SET_FINGER_CAP = PSoC_CMD_TX_SET_FINGER_CAP, +} PSoC_CMD_RX; +typedef enum { + PSoC_CMD_DEBUG_MASTER_TOUCH_TH = 0, + PSoC_CMD_DEBUG_SLAVE_TOUCH_TH, + PSoC_CMD_DEBUG_MASTER_FINGER_TH, + PSoC_CMD_DEBUG_SLAVE_FINGER_TH, + PSoC_CMD_DEBUG_MASTER_HYSTERESIS, + PSoC_CMD_DEBUG_SLAVE_HYSTERESIS, +} PSoC_CMD_DEBUG; + +// Difference between the raw count value and the baseline, from SmartSense +extern uint16_t gu16PSoCDiff[32]; + +// Has the difference data changed? +extern volatile uint8_t bPSoCDirty; + +// Used for producing gu32PSoCDigital and gu16PSoCDigital +#define PSoC_INTERNAL_DIGITAL_TH 0 + +// PSoC data, re-interpreted as digital inputs +extern uint32_t gu32PSoCDigital; +extern uint32_t gu32PSoCDigitalPos; +extern uint32_t gu32PSoCDigitalNeg; +extern uint32_t gu32PSoCDigitalTrig; +extern uint16_t gu16PSoCDigital; +extern uint16_t gu16PSoCDigitalPos; +extern uint16_t gu16PSoCDigitalNeg; +extern uint16_t gu16PSoCDigitalTrig; + +extern uint8_t gu8PSoCSeenData; + +#define PSoC_RAW_T0 34 +#define PSoC_RAW_T1 91 +#define PSoC_RAW_T2 121 +#define PSoC_RAW_MIN 199 +#define PSoC_SCALE 5 // ie to scale from [0~4ff] to [0~ff] + +#define PSoC_OUT_MIN 20 +#define PSoC_OUT_MAX 255 + +#define PSoC_SCALE_MIN (0 + 128) +#define PSoC_SCALE_MAX (0x4ff - 128 - 256) +#define PSoC_SCALE_RANGE (3) // ie to scale from [min~max] to [0~ff] + +void PSoC_PostProcessing(void); +void PSoC_DigitalCalc(void); + +// General usage PSoC commands; return instantly +void PSoC_SetFingerCapacitance(uint16_t u16FingerCap); +void PSoC_SetFingerCapacitanceFromConfig(void); +/** + * @brief Get the finger capacitance value currently configured on the Master PSoC + * + * Warning! This function is BLOCKING. Calls are likely to take at least 15ms + */ +uint16_t PSoC_GetFingerCapacitance(void); +/** + * @brief Request a debug array from the PSoC pair + * + * This call is asynchronous. Use pu8Ready to ensure pu8Data is ready. + * Only one call can be in-flight at once. + * **DO NOT** call PSoCGetFingerCapacitance while waiting on this function. + * + * @param u8Cmd The specific debug array to request + * @param pu8Data Pointer to array that will receive the data. Must be 32 bytes. + * @param pu8Ready Optional uint8_t to signal the data is ready + */ +void PSoC_GetDebug(PSoC_CMD_DEBUG u8Cmd, uint8_t* pu8Data, volatile uint8_t* pu8Ready); +void PSoC_EnableDebug(uint8_t u8D1, uint8_t u8D2); diff --git a/src/slider.c b/src/slider.c new file mode 100644 index 0000000..6532493 --- /dev/null +++ b/src/slider.c @@ -0,0 +1,177 @@ +#include "tasoller.h" + +#define SLIDER_SYNC 0xFF +#define SLIDER_MARK 0xFD + +static uint8_t su8AutoEnabled = 0; +static uint8_t su8GotLedData = 0; +static uint32_t su32SinceLastControlled = 0; + +typedef enum { + SLIDER_PARSE_SYNC_WAIT = 0, + SLIDER_PARSE_CMD, + SLIDER_PARSE_NDATA, + SLIDER_PARSE_DATA, + SLIDER_PARSE_CHECKSUM, +} slider_parse_state; + +typedef enum { + SLIDER_CMD_AUTO = 0x01, + SLIDER_CMD_SET_LED = 0x02, + SLIDER_CMD_AUTO_START = 0x03, + SLIDER_CMD_AUTO_STOP = 0x04, + SLIDER_CMD_RESET = 0x10, + SLIDER_CMD_GET_BOARD_INFO = 0xF0, +} slider_cmd; + +static const uint8_t su8SliderVersion[32] = { + '1', '5', '3', '3', '0', ' ', ' ', ' ', 0xA0, + + '0', '6', '7', '1', '2', 0xFF, + + 0x90, +}; + +static inline void Slider_Write(uint8_t u8Byte) { + if (u8Byte == SLIDER_SYNC || u8Byte == SLIDER_MARK) { + USB_VCOM_Write(SLIDER_MARK); + u8Byte--; + } + USB_VCOM_Write(u8Byte); +} +static void Slider_Respond(slider_cmd u8SliderCmd, const uint8_t* pu8Packet, uint8_t u8NPacket) { + uint8_t u8Sum = SLIDER_SYNC + u8SliderCmd + u8NPacket; + USB_VCOM_Write(SLIDER_SYNC); // We don't want to escape sync! + Slider_Write(u8SliderCmd); + Slider_Write(u8NPacket); + + for (uint16_t i = 0; i < u8NPacket; i++) { + u8Sum += pu8Packet[i]; + Slider_Write(pu8Packet[i]); + } + + Slider_Write(-u8Sum); +} +static void Slider_Process(slider_cmd u8SliderCmd, uint8_t* pu8Packet, uint8_t u8NPacket) { + switch (u8SliderCmd) { + case SLIDER_CMD_RESET: + // Hmm? Do we not need to here? + Slider_Respond(SLIDER_CMD_RESET, NULL, 0); + return; + case SLIDER_CMD_GET_BOARD_INFO: + Slider_Respond(SLIDER_CMD_GET_BOARD_INFO, su8SliderVersion, sizeof su8SliderVersion); + return; + + case SLIDER_CMD_SET_LED: + // TODO: What is the first byte of data? (00h and 28h observed) + // Why are there 32 triples? + if (u8NPacket == 1 + 0x60) { + gbLedDataIsControlledExt = 1; + su32SinceLastControlled = 0; + memcpy(gu8aControlledExtLedData, &pu8Packet[1], 3 * 32); + } + su8GotLedData = 1; + // No response + return; + + case SLIDER_CMD_AUTO_START: + su8AutoEnabled = 1; + su8GotLedData = 1; + // No response + return; + case SLIDER_CMD_AUTO_STOP: + // Purge any Tx buffer from the auto sending + if (su8AutoEnabled) USB_VCOM_PurgeTx(); + su8AutoEnabled = 0; + Slider_Respond(SLIDER_CMD_AUTO_STOP, NULL, 0); + return; + + // This is an outbound-only command, so we should never see it here! + case SLIDER_CMD_AUTO: + default: + return; + } +} + +void Slider_TickSerial(void) { + /** + * Byte 0: FF + * Byte 1: Command + * Byte 2: Length (n) + * Byte [3~2+n]: Data + * Byte [3+n]: Checksum + * + * Byte FD is escape; the next byte should +1'd + */ + + static slider_parse_state su8State = SLIDER_PARSE_SYNC_WAIT; + static uint8_t u8Mark = 0; + static uint8_t u8NPacket = 0; + static uint8_t u8NRead = 0; + static uint8_t u8Sum = 0; + static uint8_t u8SliderCmd = 0; + + static uint8_t u8Packet[0x61]; // The largest inbound packet expected is to set LEDs + + // Make sure we flush the buffer! + while (USB_VCOM_Available()) { + uint8_t u8Byte = USB_VCOM_Read(); + if (u8Byte == SLIDER_MARK) { + u8Mark = 1; + continue; + } else if (u8Mark) { + u8Mark = 0; + // TODO: If u8Byte is 0xFD we should technically give up here + u8Byte++; + } + + u8Sum += u8Byte; + switch (su8State) { + case SLIDER_PARSE_SYNC_WAIT: + u8Sum = 0xff; + if (u8Byte == SLIDER_SYNC) su8State = SLIDER_PARSE_CMD; + break; + case SLIDER_PARSE_CMD: + u8SliderCmd = u8Byte; + su8State = SLIDER_PARSE_NDATA; + break; + case SLIDER_PARSE_NDATA: + u8NPacket = u8Byte; + u8NRead = 0; + su8State = SLIDER_PARSE_DATA; + + // If this is more data than we could handle, just give up + if (u8NPacket > sizeof u8Packet) su8State = SLIDER_PARSE_SYNC_WAIT; + // If there's nothing to do.. do nothing! + if (u8NPacket == 0) su8State = SLIDER_PARSE_CHECKSUM; + break; + case SLIDER_PARSE_DATA: + u8Packet[u8NRead++] = u8Byte; + if (u8NRead == u8NPacket) su8State = SLIDER_PARSE_CHECKSUM; + break; + case SLIDER_PARSE_CHECKSUM: + // Only handle the packet if the sum equaled out + if (u8Sum == 0) Slider_Process(u8SliderCmd, u8Packet, u8NPacket); + + su8State = SLIDER_PARSE_SYNC_WAIT; + break; + } + } +} + +void Slider_Tick1ms() { + if (gbLedDataIsControlledExt) { + // If we haven't had an LED packet in 5 seconds, call it quits + if (++su32SinceLastControlled == 5 * 1000) gbLedDataIsControlledExt = 0; + } + + if (su8AutoEnabled) { + static uint8_t u8Counter = 0; + // Only actually send an update every 8ms + if (++u8Counter != 8) return; + + u8Counter = 0; + + Slider_Respond(SLIDER_CMD_AUTO, gu8GroundData, sizeof gu8GroundData); + } +} diff --git a/src/slider.h b/src/slider.h new file mode 100644 index 0000000..f9bddfc --- /dev/null +++ b/src/slider.h @@ -0,0 +1,53 @@ +// 16 cells (0-15) +#define CELL_0_Msk BIT15 +#define CELL_1_Msk BIT14 +#define CELL_2_Msk BIT13 +#define CELL_3_Msk BIT12 +#define CELL_4_Msk BIT11 +#define CELL_5_Msk BIT10 +#define CELL_6_Msk BIT9 +#define CELL_7_Msk BIT8 +#define CELL_8_Msk BIT7 +#define CELL_9_Msk BIT6 +#define CELL_10_Msk BIT5 +#define CELL_11_Msk BIT4 +#define CELL_12_Msk BIT3 +#define CELL_13_Msk BIT2 +#define CELL_14_Msk BIT1 +#define CELL_15_Msk BIT0 +// 32 pads (1-32) +#define PAD_1_Msk BIT30 +#define PAD_2_Msk BIT31 +#define PAD_3_Msk BIT28 +#define PAD_4_Msk BIT29 +#define PAD_5_Msk BIT26 +#define PAD_6_Msk BIT27 +#define PAD_7_Msk BIT24 +#define PAD_8_Msk BIT25 +#define PAD_9_Msk BIT22 +#define PAD_10_Msk BIT23 +#define PAD_11_Msk BIT20 +#define PAD_12_Msk BIT21 +#define PAD_13_Msk BIT18 +#define PAD_14_Msk BIT19 +#define PAD_15_Msk BIT16 +#define PAD_16_Msk BIT17 +#define PAD_17_Msk BIT14 +#define PAD_18_Msk BIT15 +#define PAD_19_Msk BIT12 +#define PAD_20_Msk BIT13 +#define PAD_21_Msk BIT10 +#define PAD_22_Msk BIT11 +#define PAD_23_Msk BIT8 +#define PAD_24_Msk BIT9 +#define PAD_25_Msk BIT6 +#define PAD_26_Msk BIT7 +#define PAD_27_Msk BIT4 +#define PAD_28_Msk BIT5 +#define PAD_29_Msk BIT2 +#define PAD_30_Msk BIT3 +#define PAD_31_Msk BIT0 +#define PAD_32_Msk BIT1 + +void Slider_TickSerial(void); +void Slider_Tick1ms(void); diff --git a/src/sys.c b/src/sys.c new file mode 100644 index 0000000..2b0bff1 --- /dev/null +++ b/src/sys.c @@ -0,0 +1,158 @@ +#include "tasoller.h" + +#ifdef stdout +// Unnecessary for compilation, but clangd can get a bit confused +#undef stdout +#endif +// For picolibc +FILE *const stdout = NULL; + +void SYS_ResetModule(uint32_t u32ModuleIndex) { + // Generate reset signal to the corresponding module + *(volatile uint32_t *)((uint32_t)&SYS->IPRSTC1 + (u32ModuleIndex >> 24)) |= + 1 << (u32ModuleIndex & 0x00ffffff); + + // Release corresponding module from reset state + *(volatile uint32_t *)((uint32_t)&SYS->IPRSTC1 + (u32ModuleIndex >> 24)) &= + ~(1 << (u32ModuleIndex & 0x00ffffff)); +} + +void SYS_Init(void) { + // Enable XT1_OUT (PF.0) and XT1_IN (PF.1) + SYS->GPF_MFP &= ~(SYS_GPF_MFP_PF0_Msk | SYS_GPF_MFP_PF1_Msk); + SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN; + + // Enable Internal RC 22.1184 MHz clock + CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk); + CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk); + + // Switch HCLK clock source to Internal RC and HCLK source divide 1 + CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1)); + + // Enable external XTAL 12 MHz clock + CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk); + CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk); + + // Set core clock + CLK_SetCoreClock(72000000); + + // Enable module clocks + CLK_EnableModuleClock(UART1_MODULE); + CLK_EnableModuleClock(USBD_MODULE); + CLK_EnableModuleClock(TMR0_MODULE); + CLK_EnableModuleClock(I2C1_MODULE); + // Select module clock sources + CLK_SetModuleClock(UART1_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1)); + CLK_SetModuleClock(USBD_MODULE, 0, CLK_CLKDIV_USB(3)); + CLK_SetModuleClock(TMR0_MODULE, 0, 0); + CLK_SetModuleClock(I2C1_MODULE, 0, 0); + + // Set GPB multi-function pins for UART1 RXD(PB4) and TXD(PB5) + SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB4_Msk | SYS_GPB_MFP_PB5_Msk); + SYS->GPB_MFP |= SYS_GPB_MFP_PB4_UART1_RXD | SYS_GPB_MFP_PB5_UART1_TXD; + + GPIO_SetMode(_PIN_SDA, GPIO_PMD_OUTPUT); + GPIO_SetMode(_PIN_SCL, GPIO_PMD_OUTPUT); + PIN_SDA = 1; + PIN_SCL = 1; + + // Configure our GPIO pins + GPIO_SetMode(_PIN_EC1, GPIO_PMD_INPUT); + GPIO_SetMode(_PIN_RX2, GPIO_PMD_INPUT); + GPIO_SetMode(_PIN_RX3, GPIO_PMD_INPUT); + GPIO_SetMode(_PIN_RX4, GPIO_PMD_INPUT); + GPIO_SetMode(_PIN_EC2, GPIO_PMD_INPUT); + GPIO_SetMode(_PIN_EC3, GPIO_PMD_INPUT); + GPIO_SetMode(_PIN_USB_MUX_SEL, GPIO_PMD_OUTPUT); + GPIO_SetMode(_PIN_USB_MUX_EN, GPIO_PMD_OUTPUT); + GPIO_SetMode(_PIN_LED_WING_PWR, GPIO_PMD_OUTPUT); + GPIO_SetMode(_PIN_LED_GROUND_PWR, GPIO_PMD_OUTPUT); + + PIN_LED_WING_PWR = 1; + PIN_LED_GROUND_PWR = 1; + + GPIO_SetMode(_PIN_FN2, GPIO_PMD_INPUT); + // TODO: + if (PIN_FN2 == 0) { + PIN_USB_MUX_SEL = USB_MUX_LEDS; + PIN_USB_MUX_EN = USB_MUX_ENABLE; + GPIO_SetMode(_PIN_SDA, GPIO_PMD_OUTPUT); + GPIO_SetMode(_PIN_SCL, GPIO_PMD_OUTPUT); + PIN_SDA = 0; + PIN_SCL = 0; + return; + } + + // Set GPA multi-function pins for I2C1 SDA and SCL + SYS->GPA_MFP &= ~(SYS_GPA_MFP_PA10_Msk | SYS_GPA_MFP_PA11_Msk); + SYS->GPA_MFP |= (SYS_GPA_MFP_PA10_I2C1_SDA | SYS_GPA_MFP_PA11_I2C1_SCL); + SYS->ALT_MFP &= ~(SYS_ALT_MFP_PA10_Msk | SYS_ALT_MFP_PA11_Msk); + SYS->ALT_MFP |= (SYS_ALT_MFP_PA10_I2C1_SDA | SYS_ALT_MFP_PA11_I2C1_SCL); + + PIN_USB_MUX_EN = USB_MUX_ENABLE; + PIN_USB_MUX_SEL = USB_MUX_HOST; +} + +void SYS_ModuleInit(void) { + // Setup UART1 + SYS_ResetModule(UART1_RST); + UART_Open(UART1, 460800); + /* Enable interrupts for: + * - Receive data available + * - Receive line status + * - RX time-out + */ + UART1->IER = (UART_IER_RDA_IEN_Msk | UART_IER_RLS_IEN_Msk | UART_IER_RTO_IEN_Msk); + NVIC_EnableIRQ(UART1_IRQn); + + // Set NVIC priorities + NVIC_SetPriority(USBD_IRQn, 1); + NVIC_SetPriority(TMR0_IRQn, 2); + NVIC_SetPriority(I2S_IRQn, 3); + NVIC_SetPriority(I2C0_IRQn, 4); + + CLK_SysTickDelay(10 ms); + + // For communication with the LED MCU + LED_I2C1_Init(); + + // Timer 0: Used for HID, PSoC processing and slider outbound + // This timer runs at 4kHz, but only sets the 1ms flag every 4 calls + TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 4 kHz); + TIMER_EnableInt(TIMER0); + NVIC_EnableIRQ(TMR0_IRQn); + TIMER_Start(TIMER0); + + // Setup our USB stack + Tas_USBD_Open(); + Tas_USBD_Init(); + Tas_USBD_Start(); + NVIC_EnableIRQ(USBD_IRQn); +} + +// Define a dedicated symbol for this, so we can easily trap it with a debugger! +void HardFault_Handler() { + while (1) + ; +} + +void SYS_Bootloader_Check() { + FMC_Open(); + while (FMC_Read(BOOTLOADER_MAGIC_ADDR) != BOOTLOADER_MAGIC) + ; + FMC_Close(); +} + +volatile uint8_t gu8Do250usTick = 0; +volatile uint8_t gu8Do1msTick = 0; +void TMR0_IRQHandler(void) { + static uint32_t su32Counter; + if (TIMER_GetIntFlag(TIMER0)) { + TIMER_ClearIntFlag(TIMER0); + gu8Do250usTick = 1; + if (++su32Counter == 4) { + su32Counter = 0; + gu8Do1msTick = 1; + } + } +} diff --git a/src/tasoller.h b/src/tasoller.h new file mode 100644 index 0000000..471e94e --- /dev/null +++ b/src/tasoller.h @@ -0,0 +1,275 @@ +#pragma once +#include +#include +#include + +#define ms *1000 +#define kHz *1000 + +/* === LEDs === */ +#define LED_Tx_BUFFER 254 +// Defined as volatile due to the I2C interrupt reading at random times! +extern volatile uint8_t gu8LEDTx[LED_Tx_BUFFER]; + +/* === DAO-DRM === */ +// * For now the bootloader check is being left enabled. +// * It'll help catch if I break that in my bootloader :P +#define ENABLE_BOOTLOADER_CHECK +#define BOOTLOADER_MAGIC 0x5555A320 +#define BOOTLOADER_MAGIC_ADDR 0x100FF8 + +/* === USB definitions === */ +#include "usb_inc/hid.h" +#include "usb_inc/keymap.h" +#include "usb_inc/usb.h" + +/* === Local files === */ +#include "fmc_user.h" +#include "led.h" +#include "pins.h" +#include "psoc.h" +#include "slider.h" + +#define IO4_VENDOR "SEGA INTERACTIVE" +// Product description +#define _IO4_PRODUCT \ + "I/O CONTROL BD;" /* Board type */ \ + "15257 ;" /* Board number */ \ + "01;" /* Mode */ \ + "90;" /* Firmware revision */ \ + "1831;" /* Firmware checksum */ \ + "6679A;" /* Chip number */ \ + "00;" /* Config */ +// Functional description +#define _IO4_FUNCTION \ + "GOUT=14_" /* General purpose output (20) */ \ + "ADIN=8,E_" /* ADC input (8) */ \ + "ROTIN=4_" /* Rotary input (4) */ \ + "COININ=2_" /* Coin input (2) */ \ + "SWIN=2,E_" /* Switch inputs (2) */ \ + "UQ1=41,6;" /* Unique function 1 (?) */ +#define IO4_PRODUCT _IO4_PRODUCT _IO4_FUNCTION +#define IO4_VID 0x0CA3 +#define IO4_PID 0x0021 + +#define INCR(x, y) ((x) = (x) < (y) ? (x) + 1 : (y)) +#define DECR(x, y) ((x) = (x) > (y) ? (x) - 1 : (y)) +#define MOD_INCR(x, y) ((x) = (x) == ((y)-1) ? 0 : ((x) + 1)) +#define MOD_DECR(x, y) ((x) = (x) == 0 ? ((y)-1) : ((x)-1)) +#define INV(x) ((x) = 1 - (x)) + +extern volatile uint8_t gu8VcomReady; +/** + * WARNING: This is both used internally and sent verbatim over serial + * It **MUST** be 32 bytes of ground data + */ +extern uint8_t gu8GroundData[32]; + +/* === For HID === */ +enum { + HID_REPORT_ID_IO4 = 1, + HID_REPORT_ID_KEYBOARD, + HID_REPORT_ID_DEBUG_A, + HID_REPORT_ID_DEBUG_B, + HID_REPORT_ID_IO4_CMD = 16, +}; + +#define NUM_FN 2 +#define NUM_AIR 6 +#define NUM_GROUND 32 + +typedef struct __attribute__((packed)) { + uint8_t bReportId; + uint8_t bKeyboard[NUM_FN + NUM_AIR + NUM_GROUND]; +} hid_report_t; +typedef struct __attribute__((packed)) { + uint8_t bReportId; + uint16_t wADC[8]; + uint16_t wRotary[4]; + uint16_t wCoin[2]; + uint16_t wButtons[2]; + uint8_t bSystemStatus; + uint8_t bUsbStatus; + uint8_t bUnique[29]; +} io4_hid_in_t; +typedef struct __attribute__((packed)) { + uint8_t bReportId; + uint8_t bCmd; + uint8_t bData[62]; +} io4_hid_out_t; +typedef struct __attribute__((packed)) { + uint8_t bReportId; + uint16_t wData[16]; +} debug_hid_report_t; + +extern uint8_t gu8DigitalButtons; + +// void HID_Tick(); +void USBD_HID_PrepareReport(); +uint8_t *USBD_HID_GetReport(uint8_t u8ReportId, uint32_t *pu32Size); +void USBD_HID_SetReport(volatile uint8_t *pu8EpBuf, uint32_t u32Size); + +// For CDC +typedef struct __attribute__((packed)) { + uint32_t u32DTERate; // Baud rate + uint8_t u8CharFormat; // Stop bit + uint8_t u8ParityType; // Parity + uint8_t u8DataBits; // Data bits +} STR_VCOM_LINE_CODING; +extern volatile int8_t gi8BulkOutReady; +extern STR_VCOM_LINE_CODING gLineCoding; +extern uint16_t gCtrlSignal; +extern volatile uint16_t comRbytes; +extern volatile uint16_t comRhead; +extern volatile uint16_t comRtail; +extern volatile uint16_t comTbytes; +extern volatile uint16_t comThead; +extern volatile uint16_t comTtail; +extern volatile uint8_t *gpu8RxBuf; +extern volatile uint32_t gu32RxSize; +extern volatile uint32_t gu32TxSize; +// For HID +extern uint8_t volatile gu8HIDIO4Ready; +extern uint8_t volatile gu8HIDMiscReady; +// General USB control +extern uint8_t volatile g_u8Suspend; +extern uint8_t g_u8Idle; +extern uint8_t g_u8Protocol; + +extern const char *gszVendorInitial; +extern const char *gszVendor; +extern const char *gszProduct; +extern const usb_device_descr_t *gpDeviceDescriptor; +extern const usb_desc_config_t *gpConfigDescriptor; +extern const uint32_t gu32HidDescIO4Offset; +extern const uint32_t gu32HidDescMiscOffset; +extern const uint32_t gu32UsbHidIO4ReportLen; +extern const uint32_t gu32UsbHidMiscReportLen; +extern const uint8_t *gpu8UsbHidIO4Report; +extern const uint8_t *gpu8UsbHidMiscReport; + +/*-------------------------------------------------------------*/ + +// Interfaces +enum { + USBD_ITF_CDC_CMD, + USBD_ITF_CDC_DAT, + USBD_ITF_HID_IO4, + USBD_ITF_HID_MISC, + _USBD_ITF_MAX, +}; + +// Endpoint number mapping +#define EP_CTRL_IN EP0 +#define EP_CTRL_OUT EP1 +#define EP_CDC_IN EP2 +#define EP_CDC_OUT EP3 +#define EP_CDC_CMD EP4 +#define EP_HID_IO4_IN EP5 +#define EP_HID_MISC_IN EP6 +#define EP_HID_MISC_OUT EP7 + +#define _USBD_INTSTS(x) USBD_INTSTS_EP##x +#define USBD_INTSTS(x) _USBD_INTSTS(x) + +// Must match the above!! +#define USBD_INTSTS_CTRL_IN USBD_INTSTS(EP_CTRL_IN) +#define USBD_INTSTS_CTRL_OUT USBD_INTSTS(EP_CTRL_OUT) +#define USBD_INTSTS_CDC_IN USBD_INTSTS(EP_CDC_IN) +#define USBD_INTSTS_CDC_OUT USBD_INTSTS(EP_CDC_OUT) +#define USBD_INTSTS_CDC_CMD USBD_INTSTS(EP_CDC_CMD) +#define USBD_INTSTS_HID_IO4_IN USBD_INTSTS(EP_HID_IO4_IN) +#define USBD_INTSTS_HID_MISC_IN USBD_INTSTS(EP_HID_MISC_IN) +#define USBD_INTSTS_HID_MISC_OUT USBD_INTSTS(EP_HID_MISC_OUT) + +#define USBD_CDC_EP_IN (1 | EP_INPUT) +#define USBD_CDC_EP_OUT (2 | EP_OUTPUT) +#define USBD_CDC_EP_CMD (3 | EP_INPUT) +#define USBD_HID_IO4_EP_IN (4 | EP_INPUT) +#define USBD_HID_MISC_EP_IN (5 | EP_INPUT) +#define USBD_HID_MISC_EP_OUT (6 | EP_OUTPUT) + +#define USBD_SETUP_BUF_LEN (8) +#define USBD_CDC_CMD_MAX_SIZE (16) +#define USBD_CDC_IN_MAX_SIZE (64) // Device -> Host +#define USBD_CDC_OUT_MAX_SIZE (64) // Host -> Device +#define USBD_HID_BUF_LEN (64) + +// Endpoint packet max size (cannot total more than 512!) +#define EP0_MAX_PKT_SIZE 64 +#define EP1_MAX_PKT_SIZE 64 +#define EP2_MAX_PKT_SIZE USBD_CDC_IN_MAX_SIZE +#define EP3_MAX_PKT_SIZE USBD_CDC_OUT_MAX_SIZE +#define EP4_MAX_PKT_SIZE USBD_CDC_CMD_MAX_SIZE +#define EP5_MAX_PKT_SIZE USBD_HID_BUF_LEN +#define EP6_MAX_PKT_SIZE USBD_HID_BUF_LEN +#define EP7_MAX_PKT_SIZE USBD_HID_BUF_LEN + +#define SETUP_BUF_BASE 0 +#define SETUP_BUF_LEN 8 + +#if (SETUP_BUF_LEN + EP0_MAX_PKT_SIZE + EP1_MAX_PKT_SIZE + EP2_MAX_PKT_SIZE + EP3_MAX_PKT_SIZE + \ + EP4_MAX_PKT_SIZE + EP5_MAX_PKT_SIZE + EP6_MAX_PKT_SIZE + EP7_MAX_PKT_SIZE) > 512 +#error USB endpoint packet sizes exceeds 512-byte maximum +#endif + +#define EP0_BUF_BASE (SETUP_BUF_BASE + SETUP_BUF_LEN) +#define EP1_BUF_BASE (EP0_BUF_BASE + EP0_MAX_PKT_SIZE) +#define EP2_BUF_BASE (EP1_BUF_BASE + EP1_MAX_PKT_SIZE) +#define EP3_BUF_BASE (EP2_BUF_BASE + EP2_MAX_PKT_SIZE) +#define EP4_BUF_BASE (EP3_BUF_BASE + EP3_MAX_PKT_SIZE) +#define EP5_BUF_BASE (EP4_BUF_BASE + EP4_MAX_PKT_SIZE) +#define EP6_BUF_BASE (EP5_BUF_BASE + EP5_MAX_PKT_SIZE) +#define EP7_BUF_BASE (EP5_BUF_BASE + EP6_MAX_PKT_SIZE) + +// Define Descriptor information +#define HID_IO4_INT_IN_INTERVAL 8 +#define HID_DEFAULT_INT_IN_INTERVAL 1 +#define HID_DEFAULT_INT_OUT_INTERVAL 1 +#define USBD_SELF_POWERED 0 +#define USBD_REMOTE_WAKEUP 0 +#define USBD_MAX_POWER (500 / 2) + +/*-------------------------------------------------------------*/ +#define HEX_NIBBLE(x) ("0123456789ABCDEF"[(x)&0xf]) + +void SYS_Init(void); +void SYS_Bootloader_Check(void); +void SYS_ModuleInit(void); +extern volatile uint8_t gu8Do250usTick; +extern volatile uint8_t gu8Do1msTick; + +void EP_CDC_CMD_Handler(void); +void EP_CDC_OUT_Handler(void); +void EP_CDC_IN_Handler(void); +void EP_HID_IO4_IN_Handler(void); +void EP_HID_MISC_IN_Handler(void); +void EP_HID_MISC_OUT_Handler(void); + +void USB_VCOM_Write(uint8_t u8Char); +uint16_t USB_VCOM_Available(void); +uint8_t USB_VCOM_Read(void); +void USB_VCOM_Tick(void); +void USB_VCOM_PurgeTx(void); + +void UI_Tick(void); + +void DelayCycles(uint32_t u32Cycles); +#define DelayCycles_Small(x) \ + for (int i = 0; i < (x); i++) __asm__ volatile("" : "+g"(i) : :); + +// Custom USBD implementation +void Tas_USBD_Open(void); +void Tas_USBD_Init(void); +void Tas_USBD_Start(void); +void Tas_USBD_ClassRequest(void); +void Tas_USBD_GetSetupPacket(usb_setup_t *buf); +void Tas_USBD_ProcessSetupPacket(void); +void Tas_USBD_PrepareCtrlIn(void *pu8Buf, uint32_t u32Size); +void Tas_USBD_CtrlIn(void); +void Tas_USBD_PrepareCtrlOut(void *pu8Buf, uint32_t u32Size, + void (*pCallback)(volatile uint8_t *, uint32_t)); +void Tas_USBD_CtrlOut(void); +void Tas_USBD_SwReset(void); +extern volatile uint8_t *g_usbd_CtrlInPointer; +extern volatile uint32_t g_usbd_CtrlInSize; diff --git a/src/ui.c b/src/ui.c new file mode 100644 index 0000000..53fb765 --- /dev/null +++ b/src/ui.c @@ -0,0 +1,249 @@ +#include "tasoller.h" + +#define FN2_HOLD_TIME 250 // ms of FN2 to enter config +#define FN1_HOLD_TIME 2000 // ms of FN1 to enter calibration + +// All three of these are scaled based on gConfig.u8Sens +#define CALIB_HANDS_MIN 100 // If we read less than this, ignore it +#define CALIB_HANDS_REQ 700 // All values are required to be greater than this +#define CALIB_HANDS_MAX 1300 // Scale from orange to green between [req] and [max] + +#define RED 0 +#define GREEN 120 +#define BLUE 240 + +static void UI_TickSensitivity(void) { + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + gaControlledIntLedData[i].u16H = 0; + gaControlledIntLedData[i].u8S = (gConfig.u8Sens - 1) * 16; + gaControlledIntLedData[i].u8V = 0; + } + + for (uint8_t i = 0; i < gConfig.u8Sens; i++) gaControlledIntLedData[i * 2].u8V = 255; + + // Preferentially allow decreasing of sensitivity, in case it's been taken up too high + if (gu32PSoCDigitalPos & PAD_26_Msk && gConfig.u8Sens > 1) { + gConfig.u8Sens--; + } else if (gu32PSoCDigitalPos & PAD_25_Msk && gConfig.u8Sens < 16) { + gConfig.u8Sens++; + } +} + +static void UI_TickSettings(void) { + // If either of the sensitivity settings are being changed, just render that + static uint8_t su8SensTimeout = 0; + if (gu16PSoCDigital & CELL_12_Msk) { + su8SensTimeout = 150; + UI_TickSensitivity(); + return; + } else if (su8SensTimeout) { + su8SensTimeout--; + UI_TickSensitivity(); + + // Save the sensitivity on exit + if (su8SensTimeout == 0) PSoC_SetFingerCapacitanceFromConfig(); + return; + } + + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + gaControlledIntLedData[i].u16H = 0; + gaControlledIntLedData[i].u8S = 255; + gaControlledIntLedData[i].u8V = 0; + } + + { // LED colour control + gaControlledIntLedData[LED_CELL_0].u16H = gConfig.u16HueWingLeft; + gaControlledIntLedData[LED_CELL_0].u8V = 255; + gaControlledIntLedData[LED_CELL_1].u16H = gConfig.u16HueGround; + gaControlledIntLedData[LED_CELL_1].u8V = 255; + gaControlledIntLedData[LED_DIVIDER_1_2].u16H = gConfig.u16HueGroundActive; + gaControlledIntLedData[LED_DIVIDER_1_2].u8V = 255; + gaControlledIntLedData[LED_CELL_2].u16H = gConfig.u16HueGround; + gaControlledIntLedData[LED_CELL_2].u8V = 255; + gaControlledIntLedData[LED_CELL_3].u16H = gConfig.u16HueWingRight; + gaControlledIntLedData[LED_CELL_3].u8V = 255; + + if (gu32PSoCDigitalTrig & PAD_1_Msk) MOD_INCR(gConfig.u16HueWingLeft, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_2_Msk) MOD_DECR(gConfig.u16HueWingLeft, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_3_Msk) MOD_INCR(gConfig.u16HueGround, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_4_Msk) MOD_DECR(gConfig.u16HueGround, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_5_Msk) MOD_INCR(gConfig.u16HueGroundActive, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_6_Msk) MOD_DECR(gConfig.u16HueGroundActive, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_7_Msk) MOD_INCR(gConfig.u16HueWingRight, LED_HUE_MAX); + if (gu32PSoCDigitalTrig & PAD_8_Msk) MOD_DECR(gConfig.u16HueWingRight, LED_HUE_MAX); + } + // [Cell 4 no function] + { // Lighting toggles + static uint16_t su16Hue = 0; + MOD_INCR(su16Hue, LED_HUE_MAX * 5); + if (gConfig.bEnableRainbow) { + gaControlledIntLedData[LED_CELL_5].u16H = su16Hue / 5; + gaControlledIntLedData[LED_CELL_5].u8V = 255; + } else { + gaControlledIntLedData[LED_CELL_5].u8S = 0; + gaControlledIntLedData[LED_CELL_5].u8V = 255; + } + + if (gu16PSoCDigitalPos & CELL_5_Msk) INV(gConfig.bEnableRainbow); + } + { // Brightness + if (gConfig.u8LedGroundBrightness) { + gaControlledIntLedData[LED_CELL_6].u8S = 0; + gaControlledIntLedData[LED_CELL_6].u8V = gConfig.u8LedGroundBrightness; + } else { + gaControlledIntLedData[LED_CELL_6].u8V = 255; + } + if (gConfig.u8LedWingBrightness) { + gaControlledIntLedData[LED_CELL_7].u8S = 0; + gaControlledIntLedData[LED_CELL_7].u8V = gConfig.u8LedWingBrightness; + } else { + gaControlledIntLedData[LED_CELL_7].u8V = 255; + } + + if (gu32PSoCDigitalTrig & PAD_13_Msk) INCR(gConfig.u8LedGroundBrightness, 255); + if (gu32PSoCDigitalTrig & PAD_14_Msk) DECR(gConfig.u8LedGroundBrightness, 0); + if (gu32PSoCDigitalTrig & PAD_15_Msk) INCR(gConfig.u8LedWingBrightness, 255); + if (gu32PSoCDigitalTrig & PAD_16_Msk) DECR(gConfig.u8LedWingBrightness, 0); + } + // [Cell 8,9,10,11 no function] + { // Sensitivity control (handled in dedicated function) + gaControlledIntLedData[LED_CELL_12].u8S = (gConfig.u8Sens - 1) * 16; + gaControlledIntLedData[LED_CELL_12].u8V = 255; + } + // [Cell 13 no function] + { // Mode switching + gaControlledIntLedData[LED_CELL_14].u16H = gConfig.bEnableKeyboard ? GREEN : RED; + gaControlledIntLedData[LED_CELL_14].u8V = 255; + gaControlledIntLedData[LED_CELL_15].u16H = gConfig.bEnableIO4 ? GREEN : RED; + gaControlledIntLedData[LED_CELL_15].u8V = 255; + + if (gu16PSoCDigitalPos & CELL_14_Msk) INV(gConfig.bEnableKeyboard); + if (gu16PSoCDigitalPos & CELL_15_Msk) INV(gConfig.bEnableIO4); + } +} + +static inline void _FillControlled(uint16_t u16H, uint8_t u8Fill) { + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + gaControlledIntLedData[i].u16H = u16H; + gaControlledIntLedData[i].u8S = 255; + gaControlledIntLedData[i].u8V = i < u8Fill ? 255 : 0; + } +} + +static uint8_t bCalibrationActive = 0; +static void UI_TickCalibration(void) { + static uint16_t u16Timer = 0; + static uint16_t su16MaxNoHands[32] = { 0 }; + static uint16_t su16MaxHands[32] = { 0 }; + + if (!bCalibrationActive) { + // Calibration start + u16Timer = 0; + bCalibrationActive = 1; + memset(su16MaxNoHands, 0, sizeof su16MaxNoHands); + memset(su16MaxHands, 0, sizeof su16MaxHands); + } + // Only tick the timer when we're using it, to avoid overflow + if (u16Timer < 5000) u16Timer++; + + if (u16Timer < 3000) { + // Flash red for the first 3 seconds + _FillControlled(RED, ((u16Timer / 250) & 1) ? 0 : LED_NUM_GROUND); + } else if (u16Timer < 4000) { + // Fill up the cells with blue + _FillControlled(BLUE, (u16Timer - 3000) / (1000 / LED_NUM_GROUND)); + for (uint8_t i; i < 32; i++) { + su16MaxNoHands[i] = Maximum(su16MaxNoHands[i], gu16PSoCDiff[i]); + } + } else if (u16Timer < 5000) { + // Flash green for the next second + _FillControlled(GREEN, ((u16Timer / 250) & 1) ? 0 : LED_NUM_GROUND); + } else { + for (uint8_t i = 0; i < 32; i++) { + // As well as the raw minimum, force a small constant threshold minimum too + su16MaxHands[i] = Maximum(su16MaxHands[i], gu16PSoCDiff[i] + (gConfig.u8Sens * 5)); + } + + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + gaControlledIntLedData[i].u8S = 255; + gaControlledIntLedData[i].u8V = 255; + } + + uint16_t u16CalibMin = CALIB_HANDS_MIN + (gConfig.u8Sens * 50); + uint16_t u16CalibReq = CALIB_HANDS_REQ + (gConfig.u8Sens * 50); + uint16_t u16CalibMax = CALIB_HANDS_MAX + (gConfig.u8Sens * 50); + + uint8_t bOk = 1; + // Iterate over the cells + for (uint8_t i = 0; i < 16; i++) { + if (su16MaxHands[i * 2] < u16CalibMin || su16MaxHands[i * 2 + 1] < u16CalibMin || + su16MaxHands[i * 2] < su16MaxNoHands[i * 2] || + su16MaxHands[i * 2 + 1] < su16MaxNoHands[i * 2 + 1]) { + // Not enough data + gaControlledIntLedData[i * 2].u8V = 0; + bOk = 0; + } else if (su16MaxHands[i * 2] < u16CalibReq || su16MaxHands[i * 2 + 1] < u16CalibReq) { + // Data is too low + gaControlledIntLedData[i * 2].u16H = RED; + bOk = 0; + } else if (su16MaxHands[i * 2] < u16CalibMax || su16MaxHands[i * 2 + 1] < u16CalibMax) { + // We've got enough, but it could be better + uint16_t u16Min = Minimum(su16MaxHands[i * 2], su16MaxHands[i * 2 + 1]); + gaControlledIntLedData[i * 2].u16H = + ((u16Min - u16CalibReq) * GREEN) / (u16CalibMax - u16CalibReq); + } else { + // More than enough data + gaControlledIntLedData[i * 2].u16H = GREEN; + } + } + + for (uint8_t i = 0; i < LED_NUM_GROUND; i++) { + if (i % 2 == 1) { + gaControlledIntLedData[i].u16H = bOk ? GREEN : RED; + } + } + + // Calibration complete + if (gu8DigitalButtons & DIGITAL_FN1_Msk) { + bCalibrationActive = 0; + + if (bOk) { + memcpy(gConfig.u16PSoCScaleMin, su16MaxNoHands, sizeof su16MaxNoHands); + memcpy(gConfig.u16PSoCScaleMax, su16MaxHands, sizeof su16MaxHands); + bConfigDirty = 1; + } + } + } +} + +void UI_Tick(void) { + static uint8_t u8Fn2Held = 0; + if (gu8DigitalButtons & DIGITAL_FN2_Msk) { + if (u8Fn2Held < FN2_HOLD_TIME) u8Fn2Held++; + } else { + // We released the button after holding it for long enough to be in the configuration UI, so + // assume something changed + if (u8Fn2Held >= FN2_HOLD_TIME) { + bConfigDirty = 1; + } + + u8Fn2Held = 0; + } + + static uint16_t u16Fn1Held = 0; + if (gu8DigitalButtons & DIGITAL_FN1_Msk) { + if (u16Fn1Held < FN1_HOLD_TIME) u16Fn1Held++; + } else { + u16Fn1Held = 0; + } + + if (bCalibrationActive || u16Fn1Held >= FN1_HOLD_TIME) { + gbLedDataIsControlledInt = 1; + UI_TickCalibration(); + } else if (u8Fn2Held >= FN2_HOLD_TIME) { + gbLedDataIsControlledInt = 1; + UI_TickSettings(); + } else { + gbLedDataIsControlledInt = 0; + } +} diff --git a/src/usb_inc/hid.h b/src/usb_inc/hid.h new file mode 100644 index 0000000..c8bed22 --- /dev/null +++ b/src/usb_inc/hid.h @@ -0,0 +1,1654 @@ +// https://github.com/katyo/hid_def +#pragma once + +#include "./hid_macro.h" + +#define HID_REQ_GET_REPORT 0x01 +#define HID_REQ_GET_IDLE 0x02 +#define HID_REQ_GET_PROTOCOL 0x03 +#define HID_REQ_SET_REPORT 0x09 +#define HID_REQ_SET_IDLE 0x0a +#define HID_REQ_SET_PROTOCOL 0x0b + +#define HID_REPORT_INPUT 0x01 +#define HID_REPORT_OUTPUT 0x02 +#define HID_REPORT_FEATURE 0x03 + +/* + * type: USB_DT_REPORT or USB_DT_HID + */ +#define HID_REQ_VALUE(dt, id) (((dt) << 8) | (id)) + +/* + * Element prefix + */ +#define HID_TYPE_MAIN (0<<2) +#define HID_TYPE_GLOBAL (1<<2) +#define HID_TYPE_LOCAL (2<<2) +#define HID_HEAD(tag, type, size) (((tag)<<4) | _CAT2(HID_TYPE_, type) | (((size) < 4 ? (size) : 3) & 0x3)) + +#define HID_DATA0(x) +#define HID_DATA1(x) , (x) + +#define HID_ENDIAN_LITTLE 1 +#define HID_ENDIAN_BIG 2 +#define HID_ENDIAN_PDP 3 + +#ifndef HID_ENDIAN +# ifdef __GNUC__ +# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ +# define HID_ENDIAN HID_ENDIAN_LITTLE +# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ +# define HID_ENDIAN HID_ENDIAN_BIG +# elif __BYTE_ORDER__ == __ORDER_PDP_ENDIAN__ +# define HID_ENDIAN HID_ENDIAN_PDP +# else +# error "Unsupported endian! Possibly you should define HID_ENDIAN macro." +# endif +# else +# ifdef _MSC_VER +# ifdef _WIN32 +# define HID_ENDIAN HID_ENDIAN_LITTLE +# endif +# endif +# endif +#endif + +#ifndef HID_ENDIAN +# error "Unable to determine endianness! Possibly you should define HID_ENDIAN macro." +#endif + +#if HID_ENDIAN == HID_ENDIAN_LITTLE +# define HID_DATA2(x) , (((x) >> 0) & 0xff), (((x) >> 8) & 0xff) +# define HID_DATA4(x) , (((x) >> 0) & 0xff), (((x) >> 8) & 0xff), (((x) >> 16) & 0xff), (((x) >> 24) & 0xff) +#else /* HID_ENDIAN == HID_ENDIAN_BIG || HID_ENDIAN == HID_ENDIAN_PDP */ +# define HID_DATA2(x) , (((x) >> 8) & 0xff), (((x) >> 0) & 0xff) +# if HID_ENDIAN == HID_ENDIAN_PDP +# define HID_DATA4(x) , (((x) >> 16) & 0xff), (((x) >> 24) & 0xff), (((x) >> 0) & 0xff), (((x) >> 8) & 0xff) +# else /* HID_ENDIAN == HID_ENDIAN_BIG */ +# define HID_DATA4(x) , (((x) >> 24) & 0xff), (((x) >> 16) & 0xff), (((x) >> 8) & 0xff), (((x) >> 0) & 0xff) +# endif +#endif + +#define HID_DATA(size, data) _CAT2(HID_DATA, size)(data) + +#define HID_ELEM(tag, type, size, ...) HID_HEAD(tag, type, size) HID_DATA(size, __VA_ARGS__) + +/* + * Main elements + */ + +/* type */ +#define HID_FLAG_DATA (0<<0) +#define HID_FLAG_CONSTANT (1<<0) +/* kind */ +#define HID_FLAG_ARRAY (0<<1) +#define HID_FLAG_VARIABLE (1<<1) +/* rel */ +#define HID_FLAG_ABSOLUTE (0<<2) +#define HID_FLAG_RELATIVE (1<<2) +/* wrap */ +#define HID_FLAG_NO_WRAP (0<<3) +#define HID_FLAG_WRAP (1<<3) +/* lin */ +#define HID_FLAG_LINEAR (0<<4) +#define HID_FLAG_NON_LINEAR (1<<4) +/* pref */ +#define HID_FLAG_PREFERRED_STATE (0<<5) +#define HID_FLAG_NO_PREFERRED (1<<5) +/* null */ +#define HID_FLAG_NO_NULL_POSITION (0<<6) +#define HID_FLAG_NULL_STATE (1<<6) +/* vol (only for feature and output) */ +#define HID_FLAG_NON_VOLATILE (0<<7) +#define HID_FLAG_VOLATILE (1<<7) +/* data */ +#define HID_FLAG_BIT_FIELD (0<<7) +#define HID_FLAG_BUFFERED_BYTES (1<<7) + +#define HID_APPLY_FLAG(x) _CAT2(HID_FLAG_, x) +#define HID_UNION_FLAG(...) (0 _MAP(| HID_APPLY_FLAG, ##__VA_ARGS__)) + +#define HID_INPUT(...) HID_ELEM(0x8, MAIN, 1, HID_UNION_FLAG(__VA_ARGS__)) +#define HID_OUTPUT(...) HID_ELEM(0x9, MAIN, 1, HID_UNION_FLAG(__VA_ARGS__)) +#define HID_FEATURE(...) HID_ELEM(0xb, MAIN, 1, HID_UNION_FLAG(__VA_ARGS__)) + +#define HID_COLLECTION_PHYSICAL 0x00 /* group of axes */ +#define HID_COLLECTION_APPLICATION 0x01 /* mouse, keyboard */ +#define HID_COLLECTION_LOGICAL 0x02 /* interrelated data */ +#define HID_COLLECTION_REPORT 0x03 +#define HID_COLLECTION_NAMED_ARRAY 0x04 +#define HID_COLLECTION_USAGE_SWITCH 0x05 +#define HID_COLLECTION_USAGE_MODIFIER 0x06 +#define HID_COLLECTION_VENDOR_DEFINED 0x80 +#define HID_COLLECTION(x) HID_ELEM(0xa, MAIN, 1, _CAT2(HID_COLLECTION_, x)) +#define HID_END_COLLECTION(...) HID_ELEM(0xc, MAIN, 0) + +/* + * Global elements + */ + +/* Usage Pages (see Hut1_12v2.pdf) */ +#define HID_USAGE_PAGE_UNDEFINED 1, 0x00 +#define HID_USAGE_PAGE_GENERIC_DESKTOP 1, 0x01 +#define HID_USAGE_PAGE_DESKTOP 1, 0x01 +#define HID_USAGE_PAGE_SIMULATION 1, 0x02 +#define HID_USAGE_PAGE_VIRTUAL_REALITY 1, 0x03 +#define HID_USAGE_PAGE_SPORT 1, 0x04 +#define HID_USAGE_PAGE_GAME 1, 0x05 +#define HID_USAGE_PAGE_GENERIC_DEVICE 1, 0x06 +#define HID_USAGE_PAGE_KEYBOARD 1, 0x07 +#define HID_USAGE_PAGE_KEYPAD 1, 0x07 +#define HID_USAGE_PAGE_LED 1, 0x08 +#define HID_USAGE_PAGE_LEDS 1, 0x08 +#define HID_USAGE_PAGE_BUTTON 1, 0x09 +#define HID_USAGE_PAGE_BUTTONS 1, 0x09 +#define HID_USAGE_PAGE_ORDINAL 1, 0x0a +#define HID_USAGE_PAGE_TELEPHONY 1, 0x0b +#define HID_USAGE_PAGE_CONSUMER 1, 0x0c +#define HID_USAGE_PAGE_DIGITIZER 1, 0x0d +#define HID_USAGE_PAGE_PHYSICAL_DEVICE 1, 0x0f +#define HID_USAGE_PAGE_UNICODE 1, 0x10 +#define HID_USAGE_PAGE_ALPHA_NUMERIC_DISPLAY 1, 0x14 +#define HID_USAGE_PAGE_MEDICAL_INSTRUMENTS 1, 0x40 +#define HID_USAGE_PAGE_MONITOR_DEVICE 1, 0x80 +#define HID_USAGE_PAGE_POWER_DEVICE 1, 0x84 +#define HID_USAGE_PAGE_BATTERY_SYSTEM 1, 0x85 +#define HID_USAGE_PAGE_BAR_CODE_SCANNER 1, 0x8c +#define HID_USAGE_PAGE_SCALE 1, 0x8d +#define HID_USAGE_PAGE_CAMERA_CONTROL 1, 0x90 +#define HID_USAGE_PAGE_ARCADE 1, 0x91 +/* End of Usage Pages */ + +#define HID_USAGE_PAGE2(n, x) HID_ELEM(0x0, GLOBAL, n, x) +#define HID_USAGE_PAGE(x) _CALL(HID_USAGE_PAGE2, _CAT2(HID_USAGE_PAGE_, x)) + +#define HID_LOGICAL_MINIMUM(n, x) HID_ELEM(0x1, GLOBAL, n, x) +#define HID_LOGICAL_MAXIMUM(n, x) HID_ELEM(0x2, GLOBAL, n, x) + +#define HID_PHYSICAL_MINIMUM(n, x) HID_ELEM(0x3, GLOBAL, n, x) +#define HID_PHYSICAL_MAXIMUM(n, x) HID_ELEM(0x4, GLOBAL, n, x) + +/** + * @brief HID unit exponent + * + * @param e The unit exponent + * + * unit x 10 ^ e + */ +#define HID_UNIT_EXPONENT(e) HID_ELEM(0x5, GLOBAL, 1, e) + +#define HID_UNIT_NONE_SYSTEM 0x0 +#define HID_UNIT_SI_LINEAR 0x1 +#define HID_UNIT_SI_LIN 0x1 +#define HID_UNIT_SI_ROTATION 0x2 +#define HID_UNIT_SI_ROT 0x2 +#define HID_UNIT_EN_LINEAR 0x3 +#define HID_UNIT_EN_LIN 0x3 +#define HID_UNIT_EN_ROTATION 0x4 +#define HID_UNIT_EN_ROT 0x4 +#define HID_UNIT_VENDOR_DEFINED 0xf + +#define HID_UNIT_SI(k) HID_UNIT_SI_##k +#define HID_UNIT_EN(k) HID_UNIT_EN_##k + +#define HID_UNIT_EXP(n) (!_ASSERT(-8 <= (n) && (n) <= 7) ? (0) : ((n) < 0) ? (0x10 + (n)) : (n)) +#define HID_UNIT_NIB(n, x) ((x) << (4 * (n))) + +#define HID_UNIT_LENGTH(n) HID_UNIT_NIB(1, HID_UNIT_EXP(n)) +#define HID_UNIT_MASS(n) HID_UNIT_NIB(2, HID_UNIT_EXP(n)) +#define HID_UNIT_TIME(n) HID_UNIT_NIB(3, HID_UNIT_EXP(n)) +#define HID_UNIT_TEMPERATURE(n) HID_UNIT_NIB(4, HID_UNIT_EXP(n)) +#define HID_UNIT_CURRENT(n) HID_UNIT_NIB(5, HID_UNIT_EXP(n)) +#define HID_UNIT_LUMINOSITY(n) HID_UNIT_NIB(6, HID_UNIT_EXP(n)) + +#define HID_UNIT_NONE 0x0 +#define HID_UNIT_CENTIMETER(n) HID_UNIT_LENGTH(n) +#define HID_UNIT_GRAM(n) HID_UNIT_MASS(n) +#define HID_UNIT_SECOND(n) HID_UNIT_TIME(n) +#define HID_UNIT_KELVIN(n) HID_UNIT_TEMPERATURE(n) +#define HID_UNIT_AMPERE(n) HID_UNIT_CURRENT(n) +#define HID_UNIT_CANDELLA(n) HID_UNIT_LUMINOSITY(n) +#define HID_UNIT_RADIANS(n) HID_UNIT_LENGTH(n) +#define HID_UNIT_INCH(n) HID_UNIT_LENGTH(n) +#define HID_UNIT_SLUG(n) HID_UNIT_MASS(n) +#define HID_UNIT_FAHRENHEIT(n) HID_UNIT_TEMPERATURE(n) +#define HID_UNIT_DEGREES(n) HID_UNIT_LENGTH(n) + +#define HID_UNIT_CM(n) HID_UNIT_CENTIMETER(n) +#define HID_UNIT_G(n) HID_UNIT_GRAM(n) +#define HID_UNIT_SEC(n) HID_UNIT_SECOND(n) +#define HID_UNIT_S(n) HID_UNIT_SECOND(n) +#define HID_UNIT_K(n) HID_UNIT_KELVIN(n) +#define HID_UNIT_AMP(n) HID_UNIT_AMPERE(n) +#define HID_UNIT_A(n) HID_UNIT_AMPERE(n) +#define HID_UNIT_CD(n) HID_UNIT_CANDELLA(n) +#define HID_UNIT_RAD(n) HID_UNIT_RADIANS(n) +#define HID_UNIT_IN(n) HID_UNIT_INCH(n) +#define HID_UNIT_F(n) HID_UNIT_FAHRENHEIT(n) +#define HID_UNIT_DEG(n) HID_UNIT_DEGREES(n) + +#define HID_APPLY_UNIT(x) _CAT2(HID_UNIT_, x) +#define HID_UNION_UNIT(...) (0 _MAP(| HID_APPLY_UNIT, ##__VA_ARGS__)) + +/** + * @brief HID units + * + * @param ... The set of basic units with exponent + * + * The measurement systems: + * @li SI(k) - Si units + * @li EN(k) - English units + * where @p k is LINEAR (LIN) or ROTATION (ROT). + * + * The basic units: + * @li CENTIMETER(e) or CM(e) - Centimeter ^ e + * @li GRAM(e) or G(e) - Gram ^ e + * @li SECOND(e) or S(e) - Second ^ e + * @li KELVIN(e) or K(e) - Kelvin ^ e + * @li AMPERE(e) or A(e) - Ampere ^ e + * @li CANDELLA(e) or CD(e) - Candella ^ e + * @li RADIANS(e) or RAD(e) - Radians ^ e + * @li INCH(e) or IN(e) - Inch ^ e + * @li SLUG(e) - Slug ^ e + * @li FAHRENHEIT(n) or F(e) - Fahrenheit ^ e + * @li DEGREES(e) or DEG(e) - Degrees ^ e + */ +#define HID_UNIT(...) HID_ELEM(0x6, GLOBAL, 4, HID_UNION_UNIT(__VA_ARGS__)) + +/* +#define HID_UNITS_NM HID_UNIT(CM(1)), HID_UNIT_EXPONENT(-7) +#define HID_UNITS_UM HID_UNIT(CM(1)), HID_UNIT_EXPONENT(-4) +#define HID_UNITS_MM HID_UNIT(CM(1)), HID_UNIT_EXPONENT(-1) +#define HID_UNITS_CM _EVAL(HID_UNIT(CM(1)), HID_UNIT_EXPONENT(0)) +#define HID_UNITS_DM HID_UNIT(CM(1)), HID_UNIT_EXPONENT(1) +#define HID_UNITS_M HID_UNIT(CM(1)), HID_UNIT_EXPONENT(2) +#define HID_UNITS_KM HID_UNIT(CM(1)), HID_UNIT_EXPONENT(5) +*/ + +#define HID_UNITS(x) _CAT2(HID_UNITS_, x) + +#define HID_REPORT_SIZE(x) HID_ELEM(0x7, GLOBAL, 1, x) +#define HID_REPORT_ID(x) HID_ELEM(0x8, GLOBAL, 1, x) +#define HID_REPORT_COUNT(x) HID_ELEM(0x9, GLOBAL, 1, x) + +#define HID_PUSH HID_ELEM(0xa, GLOBAL, 0) +#define HID_POP HID_ELEM(0xb, GLOBAL, 0) + +/* + * Local elements + */ + +#define HID_USAGE_UNDEFINED 1, 0x00 + +/* Generic Desktop Usages */ +#define HID_USAGE_POINTER 1, 0x01 +#define HID_USAGE_MOUSE 1, 0x02 +#define HID_USAGE_JOYSTICK 1, 0x04 +#define HID_USAGE_GAME_PAD 1, 0x05 +#define HID_USAGE_KEYBOARD 1, 0x06 +#define HID_USAGE_KEYPAD 1, 0x07 +#define HID_USAGE_MULTI_AXIS 1, 0x08 +#define HID_USAGE_TABLET 1, 0x09 + +#define HID_USAGE_X 1, 0x30 +#define HID_USAGE_Y 1, 0x31 +#define HID_USAGE_Z 1, 0x32 +#define HID_USAGE_RX 1, 0x33 +#define HID_USAGE_RY 1, 0x34 +#define HID_USAGE_RZ 1, 0x35 +#define HID_USAGE_SLIDER 1, 0x36 +#define HID_USAGE_DIAL 1, 0x37 +#define HID_USAGE_WHEEL 1, 0x38 +#define HID_USAGE_HAT_SWITCH 1, 0x39 +#define HID_USAGE_COUNTED_BUFFER 1, 0x3a +#define HID_USAGE_BYTE_COUNT 1, 0x3b +#define HID_USAGE_MOTION_WAKEUP 1, 0x3c +#define HID_USAGE_START 1, 0x3d +#define HID_USAGE_SELECT 1, 0x3e + +#define HID_USAGE_VX 1, 0x40 +#define HID_USAGE_VY 1, 0x41 +#define HID_USAGE_VZ 1, 0x42 +#define HID_USAGE_VBRX 1, 0x43 +#define HID_USAGE_VBRY 1, 0x44 +#define HID_USAGE_VBRZ 1, 0x45 +#define HID_USAGE_VNO 1, 0x46 +#define HID_USAGE_FEATURE_NOTIFICATION 1, 0x47 +#define HID_USAGE_RESOLUTION_MULTIPLIER 1, 0x48 + +#define HID_USAGE_SYSTEM_CONTROL 1, 0x80 +#define HID_USAGE_SYSTEM_POWER_DOWN 1, 0x81 +#define HID_USAGE_SYSTEM_SLEEP 1, 0x82 +#define HID_USAGE_SYSTEM_WAKE_UP 1, 0x83 +#define HID_USAGE_SYSTEM_CONTEXT_MENU 1, 0x84 +#define HID_USAGE_SYSTEM_MAIN_MENU 1, 0x85 +#define HID_USAGE_SYSTEM_APP_MENU 1, 0x86 +#define HID_USAGE_SYSTEM_MENU_HELP 1, 0x87 +#define HID_USAGE_SYSTEM_MENU_EXIT 1, 0x88 +#define HID_USAGE_SYSTEM_MENU_SELECT 1, 0x89 +#define HID_USAGE_SYSTEM_MENU_RIGHT 1, 0x8a +#define HID_USAGE_SYSTEM_MENU_LEFT 1, 0x8b +#define HID_USAGE_SYSTEM_MENU_UP 1, 0x8c +#define HID_USAGE_SYSTEM_MENU_DOWN 1, 0x8d +#define HID_USAGE_SYSTEM_COLD_RESTART 1, 0x8e +#define HID_USAGE_SYSTEM_WARM_RESTART 1, 0x8f + +#define HID_USAGE_D_PAD_UP 1, 0x90 +#define HID_USAGE_D_PAD_DOWN 1, 0x91 +#define HID_USAGE_D_PAD_RIGHT 1, 0x92 +#define HID_USAGE_D_PAD_LEFT 1, 0x93 + +#define HID_USAGE_SYSTEM_DOCK 1, 0xa0 +#define HID_USAGE_SYSTEM_UNDOCK 1, 0xa1 +#define HID_USAGE_SYSTEM_SETUP 1, 0xa2 +#define HID_USAGE_SYSTEM_BREAK 1, 0xa3 +#define HID_USAGE_SYSTEM_DEBUGGER_BREAK 1, 0xa4 +#define HID_USAGE_APPLICATION_BREAK 1, 0xa5 +#define HID_USAGE_APPLICATION_DEBUGGER_BREAK 1, 0xa6 +#define HID_USAGE_SYSTEM_SPEAKER_MUTE 1, 0xa7 +#define HID_USAGE_SYSTEM_HIBERNATE 1, 0xa8 + +#define HID_USAGE_SYSTEM_DISPLAY_INVERT 1, 0xb0 +#define HID_USAGE_SYSTEM_DISPLAY_INTERNAL 1, 0xb1 +#define HID_USAGE_SYSTEM_DISPLAY_EXTERNAL 1, 0xb2 +#define HID_USAGE_SYSTEM_DISPLAY_BOTH 1, 0xb3 +#define HID_USAGE_SYSTEM_DISPLAY_DUAL 1, 0xb4 +#define HID_USAGE_SYSTEM_DISPLAY_TOGGLE 1, 0xb5 +#define HID_USAGE_SYSTEM_DISPLAY_SWAP 1, 0xb6 +#define HID_USAGE_SYSTEM_DISPLAY_AUTOSCALE 1, 0xb7 + +/* Simulation Usages */ +#define HID_USAGE_FLIGHT 1, 0x01 +#define HID_USAGE_AUTOMOBILE 1, 0x02 +#define HID_USAGE_TANK 1, 0x03 +#define HID_USAGE_SPACESHIP 1, 0x04 +#define HID_USAGE_SUBMARINE 1, 0x05 +#define HID_USAGE_SAILING 1, 0x06 +#define HID_USAGE_MOTOCYCLE 1, 0x07 +#define HID_USAGE_SPORTS 1, 0x08 +#define HID_USAGE_AIRPLANE 1, 0x09 +#define HID_USAGE_HELICOPTER 1, 0x0a +#define HID_USAGE_MAGIC_CARPET 1, 0x0b +#define HID_USAGE_BICYCLE 1, 0x0c + +#define HID_USAGE_FLIGHT_CONTROL_STICK 1, 0x20 +#define HID_USAGE_FLIGHT_STICK 1, 0x21 +#define HID_USAGE_CYCLE_CONTROL 1, 0x22 +#define HID_USAGE_CYCLE_TRIM 1, 0x23 +#define HID_USAGE_FLIGHT_YOKE 1, 0x24 +#define HID_USAGE_TRACK_CONTROL 1, 0x25 + +#define HID_USAGE_AILERON 1, 0xb0 +#define HID_USAGE_AILERON_TRIM 1, 0xb1 +#define HID_USAGE_ANTI_TORQUE_CONTROL 1, 0xb2 +#define HID_USAGE_AUTOPILOT_ENABLE 1, 0xb3 +#define HID_USAGE_CHAFF_RELEASE 1, 0xb4 +#define HID_USAGE_COLLECTIVE_CONTROL 1, 0xb5 +#define HID_USAGE_DRIVE_BRAKE 1, 0xb6 +#define HID_USAGE_ELECRONIC_CONTERMEASURES 1, 0xb7 +#define HID_USAGE_ELEVATOR 1, 0xb8 +#define HID_USAGE_ELEVATOR_TRIM 1, 0xb9 +#define HID_USAGE_RUDDER 1, 0xba +#define HID_USAGE_THROTTLE 1, 0xbb +#define HID_USAGE_FLIGHT_COMMUNICATIONS 1, 0xbc +#define HID_USAGE_FLARE_RELEASE 1, 0xbd +#define HID_USAGE_LANDING_GEAR 1, 0xbe +#define HID_USAGE_TOE_BRAKE 1, 0xbf + +#define HID_USAGE_TRIGGER 1, 0xc0 +#define HID_USAGE_WEAPON_ARM 1, 0xc1 +#define HID_USAGE_WEAPON_SELECT 1, 0xc2 +#define HID_USAGE_WING_FLAPS 1, 0xc3 +#define HID_USAGE_ACCELERATOR 1, 0xc4 +#define HID_USAGE_BRAKE 1, 0xc5 +#define HID_USAGE_CLUTCH 1, 0xc6 +#define HID_USAGE_SHIFTER 1, 0xc7 +#define HID_USAGE_STEERING 1, 0xc8 +#define HID_USAGE_TURRET_DIRECTION 1, 0xc9 +#define HID_USAGE_BARREL_ELEVATION 1, 0xca +#define HID_USAGE_DIVE_PLANE 1, 0xcb +#define HID_USAGE_BALLAST 1, 0xcc +#define HID_USAGE_BICYCLE_CRANK 1, 0xcd +#define HID_USAGE_HANDLE_BARS 1, 0xce +#define HID_USAGE_FRONT_BRAKE 1, 0xcf +#define HID_USAGE_REAR_BRAKE 1, 0xd0 + +/* Virtual Reality Usages */ +#define HID_USAGE_BELT 1, 0x01 +#define HID_USAGE_BODY_SUIT 1, 0x02 +#define HID_USAGE_FLEXOR 1, 0x03 +#define HID_USAGE_GLOVE 1, 0x04 +#define HID_USAGE_HEAD_TRACKER 1, 0x05 +#define HID_USAGE_HEAD_DISPLAY 1, 0x06 +#define HID_USAGE_HAND_TRACKER 1, 0x07 +#define HID_USAGE_OCULOMETER 1, 0x08 +#define HID_USAGE_VEST 1, 0x09 +#define HID_USAGE_ANIMATRONIC 1, 0x0a + +#define HID_USAGE_STEREO_ENABLE 1, 0x20 +#define HID_USAGE_DISPLAY_ENABLE 1, 0x21 + +/* Sport Controls Usages */ +#define HID_USAGE_BASEBALL_BAT 1, 0x01 +#define HID_USAGE_GOLF_CLUB 1, 0x02 +#define HID_USAGE_ROWING_MACHINE 1, 0x03 +#define HID_USAGE_TREADMILL 1, 0x04 + +#define HID_USAGE_OAR 1, 0x30 +#define HID_USAGE_SLOPE 1, 0x31 +#define HID_USAGE_RATE 1, 0x32 +#define HID_USAGE_STICK_SPEED 1, 0x33 +#define HID_USAGE_STICK_FACE_ANGLE 1, 0x34 +#define HID_USAGE_STICK_HEEL_TOE 1, 0x35 +#define HID_USAGE_STICK_FLOW_THROUGH 1, 0x36 +#define HID_USAGE_STICK_TEMPO 1, 0x37 +#define HID_USAGE_STICK_TYPE 1, 0x38 +#define HID_USAGE_STICK_HEIGHT 1, 0x39 + +#define HID_USAGE_PUTTER 1, 0x50 +#define HID_USAGE_IRON(n) 1, (0x50 + (_ASSERT(1 <= (n) && (n) <= 11) ? (n) : (0))) /* n: [1..11] */ +#define HID_USAGE_SAND_WEDGE 1, 0x5c +#define HID_USAGE_LOFT_WEDGE 1, 0x5d +#define HID_USAGE_POWER_WEDGE 1, 0x5e +#define HID_USAGE_WOOD(n) 1, (0x5e + (_ASSERT(1 <= (n) && (n) <= 11) ? (n) : (0))) /* n: [1..9] */ + +/* Game Controls Usages */ +#define HID_USAGE_GAME_3D 1, 0x01 +#define HID_USAGE_PINBALL 1, 0x02 +#define HID_USAGE_GUN 1, 0x03 + +#define HID_USAGE_POINT_OF_VIEW 1, 0x20 +#define HID_USAGE_TURN_RIGHT_LEFT 1, 0x21 +#define HID_USAGE_PITCH_FORWARD_BACKWARD 1, 0x22 +#define HID_USAGE_ROLL_RIGHT_LEFT 1, 0x23 +#define HID_USAGE_MOVE_RIGHT_LEFT 1, 0x24 +#define HID_USAGE_MOVE_FORWARD_BACKWARD 1, 0x25 +#define HID_USAGE_MOVE_UP_DOWN 1, 0x26 +#define HID_USAGE_LEAN_RIGHT_LEFT 1, 0x27 +#define HID_USAGE_LEAN_FORWARD_BACKWARD 1, 0x28 +#define HID_USAGE_HEIGHT_OF_POV 1, 0x29 +#define HID_USAGE_FLIPPER 1, 0x2a +#define HID_USAGE_SECONDARY_FLIPPER 1, 0x2b +#define HID_USAGE_BUMP 1, 0x2c +#define HID_USAGE_NEW_GAME 1, 0x2d +#define HID_USAGE_SHOOT_BALL 1, 0x2e +#define HID_USAGE_PLAYER 1, 0x2f + +#define HID_USAGE_GUN_BOLT 1, 0x30 +#define HID_USAGE_GUN_CLIP 1, 0x31 +#define HID_USAGE_GUN_SELECTOR 1, 0x32 +#define HID_USAGE_GUN_SINGLE_SHOOT 1, 0x33 +#define HID_USAGE_GUN_BURST 1, 0x34 +#define HID_USAGE_GUN_AUTOMATIC 1, 0x35 +#define HID_USAGE_GUN_SAFETY 1, 0x36 +#define HID_USAGE_GAMEPAD_FIRE_JUMP 1, 0x37 +#define HID_USAGE_GAMEPAD_TRIGGER 1, 0x39 + +/* Generic Device Usages */ +#define HID_USAGE_BATTERY_STRENGTH 1, 0x20 +#define HID_USAGE_WIRELESS_CHANNEL 1, 0x21 +#define HID_USAGE_WIRELESS_ID 1, 0x22 +#define HID_USAGE_DISCOVER_WIRELESS_CONTROL 1, 0x23 +#define HID_USAGE_SECURITY_CODE_CHAR_ENTERED 1, 0x24 +#define HID_USAGE_SECURITY_CODE_CHAR_ERASED 1, 0x25 +#define HID_USAGE_SECURITY_CODE_CLEARED 1, 0x26 + +/* Keyboard/Keypad Usages */ +#define HID_USAGE_ERROR_ROLL_OVER 1, 0x01 +#define HID_USAGE_POST_FAIL 1, 0x02 +#define HID_USAGE_ERROR_UNDEFINED 1, 0x03 +#define HID_USAGE_KEY_A 1, 0x04 +#define HID_USAGE_KEY_B 1, 0x05 +#define HID_USAGE_KEY_C 1, 0x06 +#define HID_USAGE_KEY_D 1, 0x07 +#define HID_USAGE_KEY_E 1, 0x08 +#define HID_USAGE_KEY_F 1, 0x09 +#define HID_USAGE_KEY_G 1, 0x0a +#define HID_USAGE_KEY_H 1, 0x0b +#define HID_USAGE_KEY_I 1, 0x0c +#define HID_USAGE_KEY_J 1, 0x0d +#define HID_USAGE_KEY_K 1, 0x0e +#define HID_USAGE_KEY_L 1, 0x0f +#define HID_USAGE_KEY_M 1, 0x10 +#define HID_USAGE_KEY_N 1, 0x11 +#define HID_USAGE_KEY_O 1, 0x12 +#define HID_USAGE_KEY_P 1, 0x13 +#define HID_USAGE_KEY_Q 1, 0x14 +#define HID_USAGE_KEY_R 1, 0x15 +#define HID_USAGE_KEY_S 1, 0x16 +#define HID_USAGE_KEY_T 1, 0x17 +#define HID_USAGE_KEY_U 1, 0x18 +#define HID_USAGE_KEY_V 1, 0x19 +#define HID_USAGE_KEY_W 1, 0x1a +#define HID_USAGE_KEY_X 1, 0x1b +#define HID_USAGE_KEY_Y 1, 0x1c +#define HID_USAGE_KEY_Z 1, 0x1d +#define HID_USAGE_KEY_1 1, 0x1e +#define HID_USAGE_KEY_2 1, 0x1f +#define HID_USAGE_KEY_3 1, 0x20 +#define HID_USAGE_KEY_4 1, 0x21 +#define HID_USAGE_KEY_5 1, 0x22 +#define HID_USAGE_KEY_6 1, 0x23 +#define HID_USAGE_KEY_7 1, 0x24 +#define HID_USAGE_KEY_8 1, 0x25 +#define HID_USAGE_KEY_9 1, 0x26 +#define HID_USAGE_KEY_0 1, 0x27 +#define HID_USAGE_KEY_ENTER 1, 0x28 +#define HID_USAGE_KEY_ESCAPE 1, 0x29 +#define HID_USAGE_KEY_DELETE 1, 0x2a +#define HID_USAGE_KEY_BACKSPACE 1, 0x2a +#define HID_USAGE_KEY_TAB 1, 0x2b +#define HID_USAGE_KEY_SPACEBAR 1, 0x2c +#define HID_USAGE_KEY_SPACE 1, 0x2c +#define HID_USAGE_KEY_MINUS 1, 0x2d +#define HID_USAGE_KEY_UNDERSCORE 1, 0x2d +#define HID_USAGE_KEY_EQUALS 1, 0x2e +#define HID_USAGE_KEY_PLUS 1, 0x2e +#define HID_USAGE_KEY_SQUARE_OPEN 1, 0x2f +#define HID_USAGE_KEY_SQUARE_CLOSE 1, 0x30 +#define HID_USAGE_KEY_BACKSLASH 1, 0x31 +#define HID_USAGE_KEY_NON_US_SHARP 1, 0x32 +#define HID_USAGE_KEY_SEMICOLON 1, 0x33 +#define HID_USAGE_KEY_COLON 1, 0x33 +#define HID_USAGE_KEY_QUOTE 1, 0x34 +#define HID_USAGE_KEY_GRAVE_ACCENT 1, 0x35 +#define HID_USAGE_KEY_TILDE 1, 0x35 +#define HID_USAGE_KEY_COMMA 1, 0x36 +#define HID_USAGE_KEY_DOT 1, 0x37 +#define HID_USAGE_KEY_SLASH 1, 0x38 +#define HID_USAGE_KEY_CAPS_LOCK 1, 0x39 + +#define HID_USAGE_KEY_F1 1, 0x3a +#define HID_USAGE_KEY_F2 1, 0x3b +#define HID_USAGE_KEY_F3 1, 0x3c +#define HID_USAGE_KEY_F4 1, 0x3d +#define HID_USAGE_KEY_F5 1, 0x3e +#define HID_USAGE_KEY_F6 1, 0x3f +#define HID_USAGE_KEY_F7 1, 0x40 +#define HID_USAGE_KEY_F8 1, 0x41 +#define HID_USAGE_KEY_F9 1, 0x42 +#define HID_USAGE_KEY_F10 1, 0x43 +#define HID_USAGE_KEY_F11 1, 0x44 +#define HID_USAGE_KEY_F12 1, 0x45 + +#define HID_USAGE_KEY_PRINT_SCREEN 1, 0x46 +#define HID_USAGE_KEY_SCROLL_LOCK 1, 0x47 +#define HID_USAGE_KEY_PAUSE 1, 0x48 +#define HID_USAGE_KEY_INSERT 1, 0x49 +#define HID_USAGE_KEY_HOME 1, 0x4a +#define HID_USAGE_KEY_PAGE_UP 1, 0x4b +#define HID_USAGE_KEY_DELETE_FORWARD 1, 0x4c +#define HID_USAGE_KEY_END 1, 0x4d +#define HID_USAGE_KEY_PAGE_DOWN 1, 0x4e +#define HID_USAGE_KEY_RIGHT_ARROW 1, 0x4f +#define HID_USAGE_KEY_LEFT_ARROW 1, 0x50 +#define HID_USAGE_KEY_DOWN_ARROW 1, 0x51 +#define HID_USAGE_KEY_UP_ARROW 1, 0x52 + +#define HID_USAGE_KEY_PAD_NUM_LOCK 1, 0x53 +#define HID_USAGE_KEY_PAD_DIVIDE 1, 0x54 +#define HID_USAGE_KEY_PAD_MULTIPLY 1, 0x55 +#define HID_USAGE_KEY_PAD_MINUS 1, 0x56 +#define HID_USAGE_KEY_PAD_PLUS 1, 0x57 +#define HID_USAGE_KEY_PAD_ENTER 1, 0x58 +#define HID_USAGE_KEY_PAD_1 1, 0x59 +#define HID_USAGE_KEY_PAD_END 1, 0x59 +#define HID_USAGE_KEY_PAD_2 1, 0x5a +#define HID_USAGE_KEY_PAD_DOWN_ARROW 1, 0x5a +#define HID_USAGE_KEY_PAD_3 1, 0x5b +#define HID_USAGE_KEY_PAD_PAGE_DOWN 1, 0x5b +#define HID_USAGE_KEY_PAD_4 1, 0x5c +#define HID_USAGE_KEY_PAD_LEFT_ARROW 1, 0x5c +#define HID_USAGE_KEY_PAD_5 1, 0x5d +#define HID_USAGE_KEY_PAD_6 1, 0x5e +#define HID_USAGE_KEY_PAD_RIGHT_ARROW 1, 0x5e +#define HID_USAGE_KEY_PAD_7 1, 0x5f +#define HID_USAGE_KEY_PAD_HOME 1, 0x5f +#define HID_USAGE_KEY_PAD_8 1, 0x60 +#define HID_USAGE_KEY_PAD_UP_ARROW 1, 0x60 +#define HID_USAGE_KEY_PAD_9 1, 0x61 +#define HID_USAGE_KEY_PAD_PAGE_UP 1, 0x61 +#define HID_USAGE_KEY_PAD_0 1, 0x62 +#define HID_USAGE_KEY_PAD_INSERT 1, 0x62 +#define HID_USAGE_KEY_PAD_DOT 1, 0x63 +#define HID_USAGE_KEY_PAD_DELETE 1, 0x63 + +#define HID_USAGE_KEY_NON_US_BACKSLASH 1, 0x64 +#define HID_USAGE_KEY_APPLICATION 1, 0x65 +#define HID_USAGE_KEY_POWER 1, 0x66 + +#define HID_USAGE_KEY_PAD_EQUALS 1, 0x67 + +#define HID_USAGE_KEY_F13 1, 0x68 +#define HID_USAGE_KEY_F14 1, 0x69 +#define HID_USAGE_KEY_F15 1, 0x6a +#define HID_USAGE_KEY_F16 1, 0x6b +#define HID_USAGE_KEY_F17 1, 0x6c +#define HID_USAGE_KEY_F18 1, 0x6d +#define HID_USAGE_KEY_F19 1, 0x6e +#define HID_USAGE_KEY_F20 1, 0x6f +#define HID_USAGE_KEY_F21 1, 0x70 +#define HID_USAGE_KEY_F22 1, 0x71 +#define HID_USAGE_KEY_F23 1, 0x72 +#define HID_USAGE_KEY_F24 1, 0x73 + +#define HID_USAGE_KEY_EXECUTE 1, 0x74 +#define HID_USAGE_KEY_HELP 1, 0x75 +#define HID_USAGE_KEY_MENU 1, 0x76 +#define HID_USAGE_KEY_SELECT 1, 0x77 +#define HID_USAGE_KEY_STOP 1, 0x78 +#define HID_USAGE_KEY_AGAIN 1, 0x79 +#define HID_USAGE_KEY_UNDO 1, 0x7a +#define HID_USAGE_KEY_CUT 1, 0x7b +#define HID_USAGE_KEY_COPY 1, 0x7c +#define HID_USAGE_KEY_PASTE 1, 0x7d +#define HID_USAGE_KEY_FIND 1, 0x7e +#define HID_USAGE_KEY_MUTE 1, 0x7f +#define HID_USAGE_KEY_VOLUME_UP 1, 0x80 +#define HID_USAGE_KEY_VOLUME_DOWN 1, 0x81 + +#define HID_USAGE_KEY_LOCKING_CAPS_LOCK 1, 0x82 +#define HID_USAGE_KEY_LOCKING_NUM_LOCK 1, 0x83 +#define HID_USAGE_KEY_LOCKING_SCROLL_LOCK 1, 0x84 + +#define HID_USAGE_KEY_PAD_COMMA 1, 0x85 +#define HID_USAGE_KEY_PAD_EQUAL_SIGN 1, 0x86 + +#define HID_USAGE_KEY_INTERNATIONAL_1 1, 0x87 +#define HID_USAGE_KEY_INTERNATIONAL_2 1, 0x88 +#define HID_USAGE_KEY_INTERNATIONAL_3 1, 0x89 +#define HID_USAGE_KEY_INTERNATIONAL_4 1, 0x8a +#define HID_USAGE_KEY_INTERNATIONAL_5 1, 0x8b +#define HID_USAGE_KEY_INTERNATIONAL_6 1, 0x8c +#define HID_USAGE_KEY_INTERNATIONAL_7 1, 0x8d +#define HID_USAGE_KEY_INTERNATIONAL_8 1, 0x8e +#define HID_USAGE_KEY_INTERNATIONAL_9 1, 0x8f + +#define HID_USAGE_KEY_LANG_1 1, 0x90 +#define HID_USAGE_KEY_LANG_2 1, 0x91 +#define HID_USAGE_KEY_LANG_3 1, 0x92 +#define HID_USAGE_KEY_LANG_4 1, 0x93 +#define HID_USAGE_KEY_LANG_5 1, 0x94 +#define HID_USAGE_KEY_LANG_6 1, 0x95 +#define HID_USAGE_KEY_LANG_7 1, 0x96 +#define HID_USAGE_KEY_LANG_8 1, 0x97 +#define HID_USAGE_KEY_LANG_9 1, 0x98 + +#define HID_USAGE_KEY_ALTERNATE_ERASE 1, 0x99 +#define HID_USAGE_KEY_SYSTEM_REQUEST 1, 0x9a +#define HID_USAGE_KEY_ATTENTION 1, 0x9a +#define HID_USAGE_KEY_CANCEL 1, 0x9b +#define HID_USAGE_KEY_CLEAR 1, 0x9c +#define HID_USAGE_KEY_PRIOR 1, 0x9d +#define HID_USAGE_KEY_RETURN 1, 0x9e +#define HID_USAGE_KEY_SEPARATOR 1, 0x9f +#define HID_USAGE_KEY_OUT 1, 0xa0 +#define HID_USAGE_KEY_OPER 1, 0xa1 +#define HID_USAGE_KEY_CLEAR_AGAIN 1, 0xa2 +#define HID_USAGE_KEY_CR_SEL_PROPS 1, 0xa3 +#define HID_USAGE_KEY_EX_SEL 1, 0xa4 + +#define HID_USAGE_KEY_PAD_00 1, 0xb0 +#define HID_USAGE_KEY_PAD_000 1, 0xb1 +#define HID_USAGE_KEY_THOUSANDS_SEPARATOR 1, 0xb2 +#define HID_USAGE_KEY_DECIMAL_SEPARATOR 1, 0xb3 +#define HID_USAGE_KEY_CURRENCY_UNIT 1, 0xb4 +#define HID_USAGE_KEY_CURRENCY_SUB_UNIT 1, 0xb5 +#define HID_USAGE_KEY_PAD_PAREN_OPEN 1, 0xb6 +#define HID_USAGE_KEY_PAD_ROUND_OPEN 1, 0xb6 +#define HID_USAGE_KEY_PAD_PAREN_CLOSE 1, 0xb7 +#define HID_USAGE_KEY_PAD_ROUND_CLOSE 1, 0xb7 +#define HID_USAGE_KEY_PAD_BRACE_OPEN 1, 0xb8 +#define HID_USAGE_KEY_PAD_BRACE_CLOSE 1, 0xb9 +#define HID_USAGE_KEY_PAD_TAB 1, 0xba +#define HID_USAGE_KEY_PAD_BACKSPACE 1, 0xbb +#define HID_USAGE_KEY_PAD_A 1, 0xbc +#define HID_USAGE_KEY_PAD_B 1, 0xbd +#define HID_USAGE_KEY_PAD_C 1, 0xbe +#define HID_USAGE_KEY_PAD_D 1, 0xbf +#define HID_USAGE_KEY_PAD_E 1, 0xc0 +#define HID_USAGE_KEY_PAD_F 1, 0xc1 +#define HID_USAGE_KEY_PAD_XOR 1, 0xc2 +#define HID_USAGE_KEY_PAD_CARET 1, 0xc3 +#define HID_USAGE_KEY_PAD_BINARY_XOR 1, 0xc3 +#define HID_USAGE_KEY_PAD_PERCENT 1, 0xc4 +#define HID_USAGE_KEY_PAD_LESS_THAN 1, 0xc5 +#define HID_USAGE_KEY_PAD_LEFT_ANGLE 1, 0xc5 +#define HID_USAGE_KEY_PAD_GREAT_THAN 1, 0xc6 +#define HID_USAGE_KEY_PAD_RIGHT_ANGLE 1, 0xc6 +#define HID_USAGE_KEY_PAD_AMPERSAND 1, 0xc7 +#define HID_USAGE_KEY_PAD_BINARY_AND 1, 0xc7 +#define HID_USAGE_KEY_PAD_LOGIC_AND 1, 0xc8 +#define HID_USAGE_KEY_PAD_BINARY_OR 1, 0xc9 +#define HID_USAGE_KEY_PAD_LOGIC_OR 1, 0xca +#define HID_USAGE_KEY_PAD_COLON 1, 0xcb +#define HID_USAGE_KEY_PAD_SHARP 1, 0xcc +#define HID_USAGE_KEY_PAD_SPACE 1, 0xcd +#define HID_USAGE_KEY_PAD_AT 1, 0xce +#define HID_USAGE_KEY_PAD_LOGIC_NOT 1, 0xcf +#define HID_USAGE_KEY_PAD_EXCLAMATION 1, 0xcf +#define HID_USAGE_KEY_PAD_MEMORY_STORE 1, 0xd0 +#define HID_USAGE_KEY_PAD_MEMORY_RECALL 1, 0xd1 +#define HID_USAGE_KEY_PAD_MEMORY_CLEAR 1, 0xd2 +#define HID_USAGE_KEY_PAD_MEMORY_ADD 1, 0xd3 +#define HID_USAGE_KEY_PAD_MEMORY_SUBSTRACT 1, 0xd4 +#define HID_USAGE_KEY_PAD_MEMORY_MULTIPLY 1, 0xd5 +#define HID_USAGE_KEY_PAD_MEMORY_DIVIDE 1, 0xd6 +#define HID_USAGE_KEY_PAD_PLUS_MINUS 1, 0xd7 +#define HID_USAGE_KEY_PAD_CLEAR 1, 0xd8 +#define HID_USAGE_KEY_PAD_CLEAR_ENTRY 1, 0xd9 +#define HID_USAGE_KEY_PAD_BINARY 1, 0xda +#define HID_USAGE_KEY_PAD_OCTAL 1, 0xdb +#define HID_USAGE_KEY_PAD_DECIMAL 1, 0xdc +#define HID_USAGE_KEY_PAD_HEXADECIMAL 1, 0xdd + +#define HID_USAGE_KEY_LEFT_CONTROL 1, 0xe0 +#define HID_USAGE_KEY_LEFT_SHIFT 1, 0xe1 +#define HID_USAGE_KEY_LEFT_ALT 1, 0xe2 +#define HID_USAGE_KEY_LEFT_GUI 1, 0xe3 +#define HID_USAGE_KEY_RIGHT_CONTROL 1, 0xe4 +#define HID_USAGE_KEY_RIGHT_SHIFT 1, 0xe5 +#define HID_USAGE_KEY_RIGHT_ALT 1, 0xe6 +#define HID_USAGE_KEY_RIGHT_GUI 1, 0xe7 + +/* LED Usages */ +#define HID_USAGE_NUM_LOCK 1, 0x01 +#define HID_USAGE_CAPS_LOCK 1, 0x02 +#define HID_USAGE_SCROLL_LOCK 1, 0x03 +#define HID_USAGE_COMPOSE 1, 0x04 +#define HID_USAGE_KANA 1, 0x05 +#define HID_USAGE_POWER 1, 0x06 +#define HID_USAGE_SHIFT 1, 0x07 +#define HID_USAGE_DO_NOT_DISTURB 1, 0x08 +#define HID_USAGE_MUTE 1, 0x09 +#define HID_USAGE_TONE_ENABLE 1, 0x0a +#define HID_USAGE_HIGH_CUT_FILTER 1, 0x0b +#define HID_USAGE_LOW_CUT_FILTER 1, 0x0c +#define HID_USAGE_EQUALIZER_ENABLE 1, 0x0d +#define HID_USAGE_SOUND_FIELD_ON 1, 0x0e +#define HID_USAGE_SURROUND_ON 1, 0x0f +#define HID_USAGE_REPEAT 1, 0x10 +#define HID_USAGE_STEREO 1, 0x11 +#define HID_USAGE_SAMPLING_RATE_DETECT 1, 0x12 +#define HID_USAGE_SPINNING 1, 0x13 +#define HID_USAGE_CAV 1, 0x14 +#define HID_USAGE_CLV 1, 0x15 +#define HID_USAGE_RECORDING_FORMAT_DETECT 1, 0x16 +#define HID_USAGE_OFF_HOOK 1, 0x17 +#define HID_USAGE_RING 1, 0x18 +#define HID_USAGE_MESSAGE_WAITING 1, 0x19 +#define HID_USAGE_DATA_MODE 1, 0x1a +#define HID_USAGE_BATTERY_OPERATION 1, 0x1b +#define HID_USAGE_BATTERY_OK 1, 0x1c +#define HID_USAGE_BATTERY_LOW 1, 0x1d +#define HID_USAGE_SPEAKER 1, 0x1e +#define HID_USAGE_HEAD_SET 1, 0x1f +#define HID_USAGE_HOLD 1, 0x20 +#define HID_USAGE_MICROPHONE 1, 0x21 +#define HID_USAGE_COVERAGE 1, 0x22 +#define HID_USAGE_NIGHT_MODE 1, 0x23 +#define HID_USAGE_SEND_CALLS 1, 0x24 +#define HID_USAGE_CALL_PICKUP 1, 0x25 +#define HID_USAGE_CONFERENCE 1, 0x26 +#define HID_USAGE_STAND_BY 1, 0x27 +#define HID_USAGE_CAMERA_ON 1, 0x28 +#define HID_USAGE_CAMERA_OFF 1, 0x29 +#define HID_USAGE_ON_LINE 1, 0x2a +#define HID_USAGE_OFF_LINE 1, 0x2b +#define HID_USAGE_BUSY 1, 0x2c +#define HID_USAGE_READY 1, 0x2d +#define HID_USAGE_PAPER_OUT 1, 0x2e +#define HID_USAGE_PAPER_JAM 1, 0x2f +#define HID_USAGE_REMOTE 1, 0x30 +#define HID_USAGE_FORWARD 1, 0x31 +#define HID_USAGE_REVERSE 1, 0x32 +#define HID_USAGE_STOP 1, 0x33 +#define HID_USAGE_REWIND 1, 0x34 +#define HID_USAGE_FAST_FORWARD 1, 0x35 +#define HID_USAGE_PLAY 1, 0x36 +#define HID_USAGE_PAUSE 1, 0x37 +#define HID_USAGE_RECORD 1, 0x38 +#define HID_USAGE_ERROR 1, 0x39 +#define HID_USAGE_USAGE_SELECTED_INDICATOR 1, 0x3a +#define HID_USAGE_USAGE_IN_USE_INDICATOR 1, 0x3b +#define HID_USAGE_USAGE_MULTI_MODE_INDICATOR 1, 0x3c +#define HID_USAGE_INDICATOR_ON 1, 0x3d +#define HID_USAGE_INDICATOR_FLASH 1, 0x3e +#define HID_USAGE_INDICATOR_SLOW_BLINK 1, 0x3f +#define HID_USAGE_INDICATOR_FAST_BLINK 1, 0x40 +#define HID_USAGE_INDICATOR_OFF 1, 0x41 +#define HID_USAGE_FLASH_ON_TIME 1, 0x42 +#define HID_USAGE_SLOW_BLINK_ON_TIME 1, 0x43 +#define HID_USAGE_SLOW_BLINK_OFF_TIME 1, 0x44 +#define HID_USAGE_FAST_BLINK_ON_TIME 1, 0x45 +#define HID_USAGE_FAST_BLINK_OFF_TIME 1, 0x46 +#define HID_USAGE_USAGE_INDICATOR_COLOR 1, 0x47 +#define HID_USAGE_INDICATOR_RED 1, 0x48 +#define HID_USAGE_INDICATOR_GREEN 1, 0x49 +#define HID_USAGE_INDICATOR_AMBER 1, 0x4a +#define HID_USAGE_GENERIC_INDICATOR 1, 0x4b +#define HID_USAGE_SYSTEM_SUSPEND 1, 0x4c +#define HID_USAGE_EXTERNAL_POWER_CONNECTED 1, 0x4d + +/* Button Usages */ +#define HID_USAGE_BUTTON_PRIMARY 1, 0x01 +#define HID_USAGE_BUTTON_TRIGGER 1, 0x01 +#define HID_USAGE_BUTTON_SECONDARY 1, 0x02 +#define HID_USAGE_BUTTON_TERTIARY 1, 0x03 +#define HID_USAGE_BUTTON(n, x) n, (0x00 + (_ASSERT(1 <= (n) && (n) <= 2 && 1 <= (x) && (x) <= ((n) == 1 ? 0xff : 0xffff)) ? (x) : (0))) /* x: [1..255] (n:1) or [1..65535] (n:2) */ + +/* Ordinal Usages */ +#define HID_USAGE_INSTANCE(n, x) n, (0x00 + (_ASSERT(1 <= (n) && (n) <= 2 && 1 <= (x) && (x) <= ((n) == 1 ? 0xff : 0xffff)) ? (x) : (0))) /* x: [1..255] (n:1) or [1..65535] (n:2) */ + +/* Telephony Usages */ +#define HID_USAGE_PHONE 1, 0x01 +#define HID_USAGE_ANSWERING_MACHINE 1, 0x02 +#define HID_USAGE_MESSAGE_CONTROLS 1, 0x03 +#define HID_USAGE_HANDSET 1, 0x04 +#define HID_USAGE_HEADSET 1, 0x05 +#define HID_USAGE_TELEPHONY_KEYPAD 1, 0x06 +#define HID_USAGE_PROGRAMMABLE_BUTTON 1, 0x07 + +#define HID_USAGE_HOOK_SWITCH 1, 0x20 +#define HID_USAGE_FLASH 1, 0x21 +#define HID_USAGE_FEATURE 1, 0x22 +#define HID_USAGE_TELEPHONY_HOLD 1, 0x23 +#define HID_USAGE_REDIAL 1, 0x24 +#define HID_USAGE_TRANSFER 1, 0x25 +#define HID_USAGE_DROP 1, 0x26 +#define HID_USAGE_PARK 1, 0x27 +#define HID_USAGE_FORWARD_CALLS 1, 0x28 +#define HID_USAGE_ALTERNATE_FUNCTION 1, 0x29 +#define HID_USAGE_LINE 1, 0x2a +#define HID_USAGE_SPEAKER_PHONE 1, 0x2b +#define HID_USAGE_TELEPHONY_CONFERENCE 1, 0x2c +#define HID_USAGE_RING_ENABLE 1, 0x2d +#define HID_USAGE_RING_SELECT 1, 0x2e +#define HID_USAGE_PHONE_MUTE 1, 0x2f + +#define HID_USAGE_CALLER_ID 1, 0x30 +#define HID_USAGE_SEND 1, 0x31 + +#define HID_USAGE_SPEED_DIAL 1, 0x50 +#define HID_USAGE_STORE_NUMBER 1, 0x51 +#define HID_USAGE_RECALL_NUMBER 1, 0x52 +#define HID_USAGE_PHONE_DIRECTORY 1, 0x53 + +#define HID_USAGE_VOICE_MAIL 1, 0x70 +#define HID_USAGE_SCREEN_CALLS 1, 0x71 +#define HID_USAGE_TELEPHONY_DO_NOT_DISTURB 1, 0x72 +#define HID_USAGE_MESSAGE 1, 0x73 +#define HID_USAGE_ANSWER_ON_OFF 1, 0x74 + +#define HID_USAGE_INSIDE_DIAL_TONE 1, 0x90 +#define HID_USAGE_OUTSIDE_DIAL_TONE 1, 0x91 +#define HID_USAGE_INSIDE_RING_TONE 1, 0x92 +#define HID_USAGE_OUTSIDE_RING_TONE 1, 0x93 +#define HID_USAGE_PRIORITY_RING_TONE 1, 0x94 +#define HID_USAGE_INSIDE_RINGBACK 1, 0x95 +#define HID_USAGE_PRIORITY_RINGBACK 1, 0x96 +#define HID_USAGE_LINE_BUSY_TONE 1, 0x97 +#define HID_USAGE_REORDER_TONE 1, 0x98 +#define HID_USAGE_CALL_WAITING_TONE 1, 0x99 +#define HID_USAGE_CONFIRMATION_TONE_1 1, 0x9a +#define HID_USAGE_CONFIRMATION_TONE_2 1, 0x9b +#define HID_USAGE_TONES_OFF 1, 0x9c +#define HID_USAGE_OUTSIDE_RINGBACK 1, 0x9d +#define HID_USAGE_RINGER 1, 0x9e + +#define HID_USAGE_PHONE_KEY_0 1, 0xb0 +#define HID_USAGE_PHONE_KEY_1 1, 0xb1 +#define HID_USAGE_PHONE_KEY_2 1, 0xb2 +#define HID_USAGE_PHONE_KEY_3 1, 0xb3 +#define HID_USAGE_PHONE_KEY_4 1, 0xb4 +#define HID_USAGE_PHONE_KEY_5 1, 0xb5 +#define HID_USAGE_PHONE_KEY_6 1, 0xb6 +#define HID_USAGE_PHONE_KEY_7 1, 0xb7 +#define HID_USAGE_PHONE_KEY_8 1, 0xb8 +#define HID_USAGE_PHONE_KEY_9 1, 0xb9 +#define HID_USAGE_PHONE_KEY_STAR 1, 0xba +#define HID_USAGE_PHONE_KEY_POUND 1, 0xbb +#define HID_USAGE_PHONE_KEY_A 1, 0xbc +#define HID_USAGE_PHONE_KEY_B 1, 0xbd +#define HID_USAGE_PHONE_KEY_C 1, 0xbe +#define HID_USAGE_PHONE_KEY_D 1, 0xbf + +/* Consumer Usages */ +#define HID_USAGE_CONSUMER_CONTROL 1, 0x01 +#define HID_USAGE_NUMERIC_KEY_PAD 1, 0x02 +#define HID_USAGE_PROGRAMMABLE_BUTTONS 1, 0x03 +#define HID_USAGE_CUSTOMER_MICROPHONE 1, 0x04 +#define HID_USAGE_CUSTOMER_HEADPHONE 1, 0x05 +#define HID_USAGE_GRAPHIC_EQUALIZER 1, 0x06 + +#define HID_USAGE_PLUS_10 1, 0x20 +#define HID_USAGE_PLUS_100 1, 0x21 +#define HID_USAGE_AM_PM 1, 0x22 + +#define HID_USAGE_CUSTOMER_POWER 1, 0x30 +#define HID_USAGE_CUSTOMER_RESET 1, 0x31 +#define HID_USAGE_CUSTOMER_SLEEP 1, 0x32 +#define HID_USAGE_SLEEP_AFTER 1, 0x33 +#define HID_USAGE_SLEEP_MODE 1, 0x34 +#define HID_USAGE_ILLUMINATION 1, 0x35 +#define HID_USAGE_FUNCTION_BUTTONS 1, 0x36 + +#define HID_USAGE_MENU 1, 0x40 +#define HID_USAGE_MENU_PICK 1, 0x41 +#define HID_USAGE_MENU_UP 1, 0x42 +#define HID_USAGE_MENU_DOWN 1, 0x43 +#define HID_USAGE_MENU_LEFT 1, 0x44 +#define HID_USAGE_MENU_RIGHT 1, 0x45 +#define HID_USAGE_MENU_ESCAPE 1, 0x46 +#define HID_USAGE_MENU_VALUE_INCREASE 1, 0x47 +#define HID_USAGE_MENU_VALUE_DECREASE 1, 0x48 + +#define HID_USAGE_DATA_ON_SCREEN 1, 0x60 +#define HID_USAGE_CLOSED_CAPTION 1, 0x61 +#define HID_USAGE_CLOSED_CAPTION_SELECT 1, 0x62 +#define HID_USAGE_VCR_TV 1, 0x63 +#define HID_USAGE_BROADCAST_MODE 1, 0x64 +#define HID_USAGE_SNAPSHOT 1, 0x65 +#define HID_USAGE_STILL 1, 0x66 + +#define HID_USAGE_SELECTION 1, 0x80 +#define HID_USAGE_ASSIGN_SELECTION 1, 0x81 +#define HID_USAGE_MODE_STEP 1, 0x82 +#define HID_USAGE_RECALL_LAST 1, 0x83 +#define HID_USAGE_ENTER_CHANNEL 1, 0x84 +#define HID_USAGE_ORDER_MOVIE 1, 0x85 +#define HID_USAGE_CHANNEL 1, 0x86 +#define HID_USAGE_MEDIA_SELECTION 1, 0x87 +#define HID_USAGE_MEDIA_SELECT_COMPUTER 1, 0x88 +#define HID_USAGE_MEDIA_SELECT_TV 1, 0x89 +#define HID_USAGE_MEDIA_SELECT_WWW 1, 0x8a +#define HID_USAGE_MEDIA_SELECT_DVD 1, 0x8b +#define HID_USAGE_MEDIA_SELECT_TELEPHONE 1, 0x8c +#define HID_USAGE_MEDIA_SELECT_PROGRAM_GUIDE 1, 0x8d +#define HID_USAGE_MEDIA_SELECT_VIDEO_PHONE 1, 0x8e +#define HID_USAGE_MEDIA_SELECT_GAMES 1, 0x8f +#define HID_USAGE_MEDIA_SELECT_MESSAGES 1, 0x90 +#define HID_USAGE_MEDIA_SELECT_CD 1, 0x91 +#define HID_USAGE_MEDIA_SELECT_VCR 1, 0x92 +#define HID_USAGE_MEDIA_SELECT_TUNER 1, 0x93 +#define HID_USAGE_QUIT 1, 0x94 +#define HID_USAGE_HELP 1, 0x95 +#define HID_USAGE_MEDIA_SELECT_TAPE 1, 0x96 +#define HID_USAGE_MEDIA_SELECT_CABLE 1, 0x97 +#define HID_USAGE_MEDIA_SELECT_SATELLITE 1, 0x98 +#define HID_USAGE_MEDIA_SELECT_SECURITY 1, 0x99 +#define HID_USAGE_MEDIA_SELECT_HOME 1, 0x9a +#define HID_USAGE_MEDIA_SELECT_CALL 1, 0x9b +#define HID_USAGE_CHANNEL_INCREMENT 1, 0x9c +#define HID_USAGE_CHANNEL_DECREMENT 1, 0x9d +#define HID_USAGE_MEDIA_SELECT_SAP 1, 0x9e + +#define HID_USAGE_VCR_PLUS 1, 0xa0 +#define HID_USAGE_ONCE 1, 0xa1 +#define HID_USAGE_DAILY 1, 0xa2 +#define HID_USAGE_WEEKLY 1, 0xa3 +#define HID_USAGE_MONTHLY 1, 0xa4 + +#define HID_USAGE_CUSTOMER_PLAY 1, 0xb0 +#define HID_USAGE_CUSTOMER_PAUSE 1, 0xb1 +#define HID_USAGE_CUSTOMER_RECORD 1, 0xb2 +#define HID_USAGE_CUSTOMER_FAST_FORWARD 1, 0xb3 +#define HID_USAGE_CUSTOMER_REWIND 1, 0xb4 +#define HID_USAGE_SCAN_NEXT_TRACK 1, 0xb5 +#define HID_USAGE_SCAN_PREVIOUS_TRACK 1, 0xb6 +#define HID_USAGE_CUSTOMER_STOP 1, 0xb7 +#define HID_USAGE_CUSTOMER_EJECT 1, 0xb8 +#define HID_USAGE_RANDOM_PLAY 1, 0xb9 +#define HID_USAGE_SELECT_DISC 1, 0xba +#define HID_USAGE_ENTER_DISC 1, 0xbb +#define HID_USAGE_CUSTOMER_REPEAT 1, 0xbc +#define HID_USAGE_TRACKING 1, 0xbd +#define HID_USAGE_TRACK_NORMAL 1, 0xbe +#define HID_USAGE_SLOW_TRACKING 1, 0xbf +#define HID_USAGE_FRAME_FORWARD 1, 0xc0 +#define HID_USAGE_FRAME_BACK 1, 0xc1 +#define HID_USAGE_MARK 1, 0xc2 +#define HID_USAGE_CLEAR_MARK 1, 0xc3 +#define HID_USAGE_REPEAT_FROM_MARK 1, 0xc4 +#define HID_USAGE_RETURN_TO_MARK 1, 0xc5 +#define HID_USAGE_SEARCH_MARK_FORWARD 1, 0xc6 +#define HID_USAGE_SEARCH_MARK_BACKWARDS 1, 0xc7 +#define HID_USAGE_COUNTER_RESET 1, 0xc8 +#define HID_USAGE_SHOW_COUNTER 1, 0xc9 +#define HID_USAGE_TRACKING_INCREMENT 1, 0xca +#define HID_USAGE_TRACKING_DECREMENT 1, 0xcb +#define HID_USAGE_STOP_EJECT 1, 0xcc +#define HID_USAGE_PLAY_PAUSE 1, 0xcd +#define HID_USAGE_PLAY_SKIP 1, 0xce + +#define HID_USAGE_VOLUME 1, 0xe0 +#define HID_USAGE_BALANCE 1, 0xe1 +#define HID_USAGE_CUSTOMER_MUTE 1, 0xe2 +#define HID_USAGE_BASS 1, 0xe3 +#define HID_USAGE_TREBLE 1, 0xe4 +#define HID_USAGE_BASS_BOOST 1, 0xe5 +#define HID_USAGE_SURROUND_MODE 1, 0xe6 +#define HID_USAGE_LOUDNESS 1, 0xe7 +#define HID_USAGE_MPX 1, 0xe8 +#define HID_USAGE_VOLUME_INCREMENT 1, 0xe9 +#define HID_USAGE_VOLUME_DECREMENT 1, 0xea + +#define HID_USAGE_SPEED_SELECT 1, 0xf0 +#define HID_USAGE_PLAYBACK_SPEED 1, 0xf1 +#define HID_USAGE_STANDARD_PLAY 1, 0xf2 +#define HID_USAGE_LONG_PLAY 1, 0xf3 +#define HID_USAGE_EXTENDED_PLAY 1, 0xf4 +#define HID_USAGE_SLOW 1, 0xf5 + +#define HID_USAGE_FAN_ENABLE 2, 0x100 +#define HID_USAGE_FAN_SPEED 2, 0x101 +#define HID_USAGE_LIGHT_ENABLE 2, 0x102 +#define HID_USAGE_LIGHT_ILLUMINATION_LEVEL 2, 0x103 +#define HID_USAGE_CLIMATE_CONTROL_ENABLE 2, 0x104 +#define HID_USAGE_ROOM_TEMPERATURE 2, 0x105 +#define HID_USAGE_SECURITY_ENABLE 2, 0x106 +#define HID_USAGE_FIRE_ALARM 2, 0x107 +#define HID_USAGE_POLICE_ALARM 2, 0x108 +#define HID_USAGE_PROXIMITY 2, 0x109 +#define HID_USAGE_MOTION 2, 0x10a +#define HID_USAGE_DURESS_ALARM 2, 0x10b +#define HID_USAGE_HOLDUP_ALARM 2, 0x10c +#define HID_USAGE_MEDICAL_ALARM 2, 0x10d + +#define HID_USAGE_BALANCE_RIGHT 2, 0x150 +#define HID_USAGE_BALANCE_LEFT 2, 0x151 +#define HID_USAGE_BASS_INCREMENT 2, 0x152 +#define HID_USAGE_BASS_DECREMENT 2, 0x153 +#define HID_USAGE_TREBLE_INCREMENT 2, 0x154 +#define HID_USAGE_TREBLE_DECREMENT 2, 0x155 + +#define HID_USAGE_SPEAKER_SYSTEM 2, 0x160 +#define HID_USAGE_CHANNEL_LEFT 2, 0x161 +#define HID_USAGE_CHANNEL_RIGHT 2, 0x162 +#define HID_USAGE_CHANNEL_CENTER 2, 0x163 +#define HID_USAGE_CHANNEL_FRONT 2, 0x164 +#define HID_USAGE_CHANNEL_CENTER_FRONT 2, 0x165 +#define HID_USAGE_CHANNEL_SIDE 2, 0x166 +#define HID_USAGE_CHANNEL_SURROUND 2, 0x167 +#define HID_USAGE_CHANNEL_LOW_FREQUENCY_ENHANCEMENT 2, 0x168 +#define HID_USAGE_CHANNEL_TOP 2, 0x169 +#define HID_USAGE_CHANNEL_UNKNOWN 2, 0x16a + +#define HID_USAGE_SUB_CHANNEL 2, 0x170 +#define HID_USAGE_SUB_CHANNEL_INCREMENT 2, 0x171 +#define HID_USAGE_SUB_CHANNEL_DECREMENT 2, 0x172 +#define HID_USAGE_ALTERNATE_AUDIO_INCREMENT 2, 0x173 +#define HID_USAGE_ALTERNATE_AUDIO_DECREMENT 2, 0x174 + +#define HID_USAGE_APPLICATION_LAUNCH_BUTTONS 2, 0x180 +#define HID_USAGE_AL_LAUNCH_BUTTON_CONFIGURATION_TOOL 2, 0x181 +#define HID_USAGE_AL_PROGRAMMABLE_BUTTON_CONFIGURATION 2, 0x182 +#define HID_USAGE_AL_CONSUMER_CONTROL_CONFIGURATION 2, 0x183 +#define HID_USAGE_AL_WORD_PROCESSOR 2, 0x184 +#define HID_USAGE_AL_TEXT_EDITOR 2, 0x185 +#define HID_USAGE_AL_SPREADSHEET 2, 0x186 +#define HID_USAGE_AL_GRAPHICS_EDITOR 2, 0x187 +#define HID_USAGE_AL_PRESENTATION_APP 2, 0x188 +#define HID_USAGE_AL_DATABASE_APP 2, 0x189 +#define HID_USAGE_AL_EMAIL_READER 2, 0x18a +#define HID_USAGE_AL_NEWS_READER 2, 0x18b +#define HID_USAGE_AL_VOICE_MAIL 2, 0x18c +#define HID_USAGE_AL_CONTACTS_ADDRESS_BOOK 2, 0x18d +#define HID_USAGE_AL_CALENDAR_SCHEDULE 2, 0x18e +#define HID_USAGE_AL_TASK_PROJECT_MANAGER 2, 0x18f +#define HID_USAGE_AL_LOG_JOURNAL_TIMECARD 2, 0x190 +#define HID_USAGE_AL_CHECKBOOK_FINANCE 2, 0x191 +#define HID_USAGE_AL_CALCULATOR 2, 0x192 +#define HID_USAGE_AL_AV_CAPTURE_PLAYBACK 2, 0x193 +#define HID_USAGE_AL_LOCAL_MACHINE_BROWSER 2, 0x194 +#define HID_USAGE_AL_LAN_WAN_BROWSER 2, 0x195 +#define HID_USAGE_AL_INTERNET_BROWSER 2, 0x196 +#define HID_USAGE_AL_REMOTE_NETWORKING_ISP_CONNECT 2, 0x197 +#define HID_USAGE_AL_NETWORK_CONFERENCE 2, 0x198 +#define HID_USAGE_AL_NETWORK_CHAT 2, 0x199 +#define HID_USAGE_AL_TELEPHONE_DIALER 2, 0x19a +#define HID_USAGE_AL_LOGON 2, 0x19b +#define HID_USAGE_AL_LOGOFF 2, 0x19c +#define HID_USAGE_AL_LOGON_LOGOFF 2, 0x19d +#define HID_USAGE_AL_TERMINAL_LOCK_SCREENSAVER 2, 0x19e +#define HID_USAGE_AL_CONTROL_PANEL 2, 0x19f +#define HID_USAGE_AL_COMMAND_LINE_PROCESSOR_RUN 2, 0x1a0 +#define HID_USAGE_AL_PROCESS_TASK_MANAGER 2, 0x1a1 +#define HID_USAGE_AL_SELECT_TASK_APPLICATION 2, 0x1a2 +#define HID_USAGE_AL_NEXT_TASK_APPLICATION 2, 0x1a3 +#define HID_USAGE_AL_PREVIOUS_TASK_APPLICATION 2, 0x1a4 +#define HID_USAGE_AL_PREEMPTIVE_HALT_TASK_APPLICATION 2, 0x1a5 +#define HID_USAGE_AL_INTEGRATED_HELP_CENTER 2, 0x1a6 +#define HID_USAGE_AL_DOCUMENTS 2, 0x1a7 +#define HID_USAGE_AL_THESAURUS 2, 0x1a8 +#define HID_USAGE_AL_DICTIONARY 2, 0x1a9 +#define HID_USAGE_AL_DESKTOP 2, 0x1aa +#define HID_USAGE_AL_SPELL_CHECK 2, 0x1ab +#define HID_USAGE_AL_GRAMMAR_CHECK 2, 0x1ac +#define HID_USAGE_AL_WIRELESS_STATUS 2, 0x1ad +#define HID_USAGE_AL_KEYBOARD_LAYOUT 2, 0x1ae +#define HID_USAGE_AL_VIRUS_PROTECTION 2, 0x1af +#define HID_USAGE_AL_ENCRYPTION 2, 0x1b0 +#define HID_USAGE_AL_SCREEN_SAVER 2, 0x1b1 +#define HID_USAGE_AL_ALARMS 2, 0x1b2 +#define HID_USAGE_AL_CLOCK 2, 0x1b3 +#define HID_USAGE_AL_FILE_BROWSER 2, 0x1b4 +#define HID_USAGE_AL_POWER_STATUS 2, 0x1b5 +#define HID_USAGE_AL_IMAGE_BROWSER 2, 0x1b6 +#define HID_USAGE_AL_AUDIO_BROWSER 2, 0x1b7 +#define HID_USAGE_AL_MOVIE_BROWSER 2, 0x1b8 +#define HID_USAGE_AL_DIGITAL_RIGHTS_MANAGER 2, 0x1b9 +#define HID_USAGE_AL_DIGITAL_WALLET 2, 0x1ba + +#define HID_USAGE_AL_INSTANT_MESSAGING 2, 0x1bc +#define HID_USAGE_AL_OEM_FEATURES_TIPS_TUTORIAL_BROWSER 2, 0x1bd +#define HID_USAGE_AL_OEM_HELP 2, 0x1be +#define HID_USAGE_AL_ONLINE_COMMUNITY 2, 0x1bf +#define HID_USAGE_AL_ENTERTAINMENT_CONTENT_BROWSER 2, 0x1c0 +#define HID_USAGE_AL_ONLINE_SHOPPING_BROWSER 2, 0x1c1 +#define HID_USAGE_AL_SMARTCARD_INFORMATION_HELP 2, 0x1c2 +#define HID_USAGE_AL_MARKET_MONITOR_FINANCE_BROWSER 2, 0x1c3 +#define HID_USAGE_AL_CUSTOMIZED_CORPORATE_NEWS_BROWSER 2, 0x1c4 +#define HID_USAGE_AL_ONLINE_ACTIVITY_BROWSER 2, 0x1c5 +#define HID_USAGE_AL_RESEARCH_SEARCH_BROWSER 2, 0x1c6 +#define HID_USAGE_AL_AUDIO_PLAYER 2, 0x1c7 + +#define HID_USAGE_GENERIC_GUI_APPLICATION_CONTROLS 2, 0x200 +#define HID_USAGE_AC_NEW 2, 0x201 +#define HID_USAGE_AC_OPEN 2, 0x202 +#define HID_USAGE_AC_CLOSE 2, 0x203 +#define HID_USAGE_AC_EXIT 2, 0x204 +#define HID_USAGE_AC_MAXIMIZE 2, 0x205 +#define HID_USAGE_AC_MINIMIZE 2, 0x206 +#define HID_USAGE_AC_SAVE 2, 0x207 +#define HID_USAGE_AC_PRINT 2, 0x208 +#define HID_USAGE_AC_PROPERTIES 2, 0x209 + +#define HID_USAGE_AC_UNDO 2, 0x21a +#define HID_USAGE_AC_COPY 2, 0x21b +#define HID_USAGE_AC_CUT 2, 0x21c +#define HID_USAGE_AC_PASTE 2, 0x21d +#define HID_USAGE_AC_SELECT_ALL 2, 0x21e +#define HID_USAGE_AC_FIND 2, 0x21f + +#define HID_USAGE_AC_FIND_AND_REPLACE 2, 0x220 +#define HID_USAGE_AC_SEARCH 2, 0x221 +#define HID_USAGE_AC_GO_TO 2, 0x222 +#define HID_USAGE_AC_HOME 2, 0x223 +#define HID_USAGE_AC_BACK 2, 0x224 +#define HID_USAGE_AC_FORWARD 2, 0x225 +#define HID_USAGE_AC_STOP 2, 0x226 +#define HID_USAGE_AC_REFRESH 2, 0x227 +#define HID_USAGE_AC_PREVIOUS_LINK 2, 0x228 +#define HID_USAGE_AC_NEXT_LINK 2, 0x229 +#define HID_USAGE_AC_BOOKMARKS 2, 0x22a +#define HID_USAGE_AC_HISTORY 2, 0x22b +#define HID_USAGE_AC_SUBSCRIPTIONS 2, 0x22c +#define HID_USAGE_AC_ZOOM_IN 2, 0x22d +#define HID_USAGE_AC_ZOOM_OUT 2, 0x22e +#define HID_USAGE_AC_ZOOM 2, 0x22f +#define HID_USAGE_AC_FULL_SCREEN_VIEW 2, 0x230 +#define HID_USAGE_AC_NORMAL_VIEW 2, 0x231 +#define HID_USAGE_AC_VIEW_TOGGLE 2, 0x232 +#define HID_USAGE_AC_SCROLL_UP 2, 0x233 +#define HID_USAGE_AC_SCROLL_DOWN 2, 0x234 +#define HID_USAGE_AC_SCROLL 2, 0x325 +#define HID_USAGE_AC_PAN_LEFT 2, 0x236 +#define HID_USAGE_AC_PAN_RIGHT 2, 0x237 +#define HID_USAGE_AC_PAN 2, 0x238 +#define HID_USAGE_AC_NEW_WINDOW 2, 0x239 +#define HID_USAGE_AC_TILE_HORIZONTALLY 2, 0x23a +#define HID_USAGE_AC_TILE_VERTICALLY 2, 0x23b +#define HID_USAGE_AC_FORMAT 2, 0x23c +#define HID_USAGE_AC_EDIT 2, 0x23d +#define HID_USAGE_AC_BOLD 2, 0x23e +#define HID_USAGE_AC_ITALICS 2, 0x23f +#define HID_USAGE_AC_UNDERLINE 2, 0x240 +#define HID_USAGE_AC_STRIKETHROUGH 2, 0x241 +#define HID_USAGE_AC_SUBSCRIPT 2, 0x242 +#define HID_USAGE_AC_SUPERSCRIPT 2, 0x243 +#define HID_USAGE_AC_ALL_CAPS 2, 0x244 +#define HID_USAGE_AC_ROATE 2, 0x245 +#define HID_USAGE_AC_RESIZE 2, 0x246 +#define HID_USAGE_AC_FLIP_HORIZONTAL 2, 0x247 +#define HID_USAGE_AC_FLIP_VERTICAL 2, 0x248 +#define HID_USAGE_AC_MIRROR_HORIZONTAL 2, 0x249 +#define HID_USAGE_AC_MIRROR_VERTICAL 2, 0x24a +#define HID_USAGE_AC_FONT_SELECT 2, 0x24b +#define HID_USAGE_AC_FONT_COLOR 2, 0x24c +#define HID_USAGE_AC_FONT_SIZE 2, 0x24d +#define HID_USAGE_AC_JUSTIFY_LEFT 2, 0x24e +#define HID_USAGE_AC_JUSTIFY_CENTER_H 2, 0x24f +#define HID_USAGE_AC_JUSTIFY_RIGHT 2, 0x250 +#define HID_USAGE_AC_JUSTIFY_BLOCK_H 2, 0x251 +#define HID_USAGE_AC_JUSTIFY_TOP 2, 0x252 +#define HID_USAGE_AC_JUSTIFY_CENTER_V 2, 0x253 +#define HID_USAGE_AC_JUSTIFY_BOTTOM 2, 0x254 +#define HID_USAGE_AC_JUSTIFY_BLOCK_V 2, 0x255 +#define HID_USAGE_AC_INDENT_DECREASE 2, 0x256 +#define HID_USAGE_AC_INDENT_INCREASE 2, 0x257 +#define HID_USAGE_AC_NUMBERED_LIST 2, 0x258 +#define HID_USAGE_AC_RESTART_NUMBERING 2, 0x259 +#define HID_USAGE_AC_BULLETED_LIST 2, 0x25a +#define HID_USAGE_AC_PROMOTE 2, 0x25b +#define HID_USAGE_AC_DEMOTE 2, 0x25c +#define HID_USAGE_AC_YES 2, 0x25d +#define HID_USAGE_AC_NO 2, 0x25e +#define HID_USAGE_AC_CANCEL 2, 0x25f +#define HID_USAGE_AC_CATALOG 2, 0x260 +#define HID_USAGE_AC_BUY_CHECKOUT 2, 0x261 +#define HID_USAGE_AC_ADD_TO_CART 2, 0x262 +#define HID_USAGE_AC_EXPAND 2, 0x263 +#define HID_USAGE_AC_EXPAND_ALL 2, 0x264 +#define HID_USAGE_AC_COLLAPSE 2, 0x265 +#define HID_USAGE_AC_COLLAPSE_ALL 2, 0x266 +#define HID_USAGE_AC_PRINT_PREVIEW 2, 0x267 +#define HID_USAGE_AC_PASTE_SPECIAL 2, 0x268 +#define HID_USAGE_AC_INSERT_MODE 2, 0x269 +#define HID_USAGE_AC_DELETE 2, 0x26a +#define HID_USAGE_AC_LOCK 2, 0x26b +#define HID_USAGE_AC_UNLOCK 2, 0x26c +#define HID_USAGE_AC_PROTECT 2, 0x26d +#define HID_USAGE_AC_UNPROTECT 2, 0x26e +#define HID_USAGE_AC_ATTACH_COMMENT 2, 0x26f +#define HID_USAGE_AC_DELETE_COMMENT 2, 0x270 +#define HID_USAGE_AC_VIEW_COMMENT 2, 0x271 +#define HID_USAGE_AC_SELECT_WORD 2, 0x272 +#define HID_USAGE_AC_SELECT_SENTENCE 2, 0x273 +#define HID_USAGE_AC_SELECT_PARAGRAPH 2, 0x274 +#define HID_USAGE_AC_SELECT_COLUMN 2, 0x275 +#define HID_USAGE_AC_SELECT_ROW 2, 0x276 +#define HID_USAGE_AC_SELECT_TABLE 2, 0x277 +#define HID_USAGE_AC_SELECT_OBJECT 2, 0x278 +#define HID_USAGE_AC_REDO_REPEAT 2, 0x279 +#define HID_USAGE_AC_SORT 2, 0x27a +#define HID_USAGE_AC_SORT_ASCENDING 2, 0x27b +#define HID_USAGE_AC_SORT_DESCENDING 2, 0x27c +#define HID_USAGE_AC_FILTER 2, 0x27d +#define HID_USAGE_AC_SET_CLOCK 2, 0x27e +#define HID_USAGE_AC_VIEW_CLOCK 2, 0x27f +#define HID_USAGE_AC_SELECT_TIME_ZONE 2, 0x280 +#define HID_USAGE_AC_EDIT_TIME_ZONES 2, 0x281 +#define HID_USAGE_AC_SET_ALARM 2, 0x282 +#define HID_USAGE_AC_CLEAR_ALARM 2, 0x283 +#define HID_USAGE_AC_SNOOZE_ALARM 2, 0x284 +#define HID_USAGE_AC_RESET_ALARM 2, 0x285 +#define HID_USAGE_AC_SYNCHRONIZE 2, 0x286 +#define HID_USAGE_AC_SEND_RECEIVE 2, 0x287 +#define HID_USAGE_AC_SEND_TO 2, 0x288 +#define HID_USAGE_AC_REPLY 2, 0x289 +#define HID_USAGE_AC_REPLY_ALL 2, 0x28a +#define HID_USAGE_AC_FORWARD_MESSAGE 2, 0x28b +#define HID_USAGE_AC_SEND 2, 0x28c +#define HID_USAGE_AC_ATTACH_FILE 2, 0x28d +#define HID_USAGE_AC_UPLOAD 2, 0x28e +#define HID_USAGE_AC_DOWNLOAD 2, 0x28f +#define HID_USAGE_AC_SET_BORDERS 2, 0x290 +#define HID_USAGE_AC_INSERT_ROW 2, 0x291 +#define HID_USAGE_AC_INSERT_COLUMN 2, 0x292 +#define HID_USAGE_AC_INSERT_FILE 2, 0x293 +#define HID_USAGE_AC_INSERT_PICTURE 2, 0x294 +#define HID_USAGE_AC_INSERT_OBJECT 2, 0x295 +#define HID_USAGE_AC_INSERT_SYMBOL 2, 0x296 +#define HID_USAGE_AC_SAVE_AND_CLOSE 2, 0x297 +#define HID_USAGE_AC_RENAME 2, 0x298 +#define HID_USAGE_AC_MERGE 2, 0x299 +#define HID_USAGE_AC_SPLIT 2, 0x29a + +#define HID_USAGE_AC_DISTRIBUTE_HORIZONTALLY 2, 0x29b +#define HID_USAGE_AC_DISTRIBUTE_VERTICALLY 2, 0x29c + +/* Digitizer Usages */ +#define HID_USAGE_DIGITIZER 1, 0x01 +#define HID_USAGE_PEN 1, 0x02 +#define HID_USAGE_LIGHT_PEN 1, 0x03 +#define HID_USAGE_TOUCH_SCREEN 1, 0x04 +#define HID_USAGE_TOUCH_PAD 1, 0x05 +#define HID_USAGE_WHITE_BOARD 1, 0x06 +#define HID_USAGE_COORDINATE_MEASURING_MACHINE 1, 0x07 +#define HID_USAGE_DIGITIZER_3D 1, 0x08 +#define HID_USAGE_STEREO_PLOTTER 1, 0x09 +#define HID_USAGE_ARTICULATED_ARM 1, 0x0a +#define HID_USAGE_ARMATURE 1, 0x0b +#define HID_USAGE_MULTIPLE_POINT_DIGITIZER 1, 0x0c +#define HID_USAGE_FREE_SPACE_WAND 1, 0x0d + +#define HID_USAGE_STYLUS 1, 0x20 +#define HID_USAGE_PUCK 1, 0x21 +#define HID_USAGE_FINGER 1, 0x22 + +#define HID_USAGE_TIP_PRESSURE 1, 0x30 +#define HID_USAGE_BARREL_PRESSURE 1, 0x31 +#define HID_USAGE_IN_RANGE 1, 0x32 +#define HID_USAGE_TOUCH 1, 0x33 +#define HID_USAGE_UNTOUCH 1, 0x34 +#define HID_USAGE_TAP 1, 0x35 +#define HID_USAGE_QUALITY 1, 0x36 +#define HID_USAGE_DATA_VALID 1, 0x37 +#define HID_USAGE_TRANSDUCER_INDEX 1, 0x38 +#define HID_USAGE_TABLET_FUNCTION_KEYS 1, 0x39 +#define HID_USAGE_PROGRAM_CHANGE_KEYS 1, 0x3a +#define HID_USAGE_DIG_BATTERY_STRENGTH 1, 0x3b +#define HID_USAGE_INVERT 1, 0x3c +#define HID_USAGE_X_TILT 1, 0x3d +#define HID_USAGE_Y_TILT 1, 0x3e +#define HID_USAGE_AZIMUTH 1, 0x3f +#define HID_USAGE_ALTITUDE 1, 0x40 +#define HID_USAGE_TWIST 1, 0x41 +#define HID_USAGE_TIP_SWITCH 1, 0x42 +#define HID_USAGE_SECONDARY_TIP_SWITCH 1, 0x43 +#define HID_USAGE_BARREL_SWITCH 1, 0x44 +#define HID_USAGE_ERASER 1, 0x45 +#define HID_USAGE_TABLET_PICK 1, 0x46 + +/* Alphanumeric Display Usages */ +#define HID_USAGE_ALPHANUMERIC_DISPLAY 1, 0x01 +#define HID_USAGE_BITMAPPED_DISPLAY 1, 0x02 + +#define HID_USAGE_DISPLAY_ATTRIBUTES_REPORT 1, 0x20 +#define HID_USAGE_ASCII_CHARACTER_SET 1, 0x21 +#define HID_USAGE_DATA_READ_BACK 1, 0x22 +#define HID_USAGE_FONT_READ_BACK 1, 0x23 +#define HID_USAGE_DISPLAY_CONTROL_REPORT 1, 0x24 +#define HID_USAGE_CLEAR_DISPLAY 1, 0x25 +#define HID_USAGE_ALNUM_DISPLAY_ENABLE 1, 0x26 +#define HID_USAGE_SCREEN_SAVER_DELAY 1, 0x27 +#define HID_USAGE_SCREEN_SAVER_ENABLE 1, 0x28 +#define HID_USAGE_VERTICAL_SCROLL 1, 0x29 +#define HID_USAGE_HORIZONTAL_SCROLL 1, 0x2a +#define HID_USAGE_CHARACTER_REPORT 1, 0x2b +#define HID_USAGE_DISPLAY_DATA 1, 0x2c +#define HID_USAGE_DISPLAY_STATUS 1, 0x2d +#define HID_USAGE_STAT_NOT_READY 1, 0x2e +#define HID_USAGE_STAT_READY 1, 0x2f +#define HID_USAGE_NOT_A_LOADABLE_CHARACTER 1, 0x30 +#define HID_USAGE_FONT_DATA_CANNOT_BE_READ 1, 0x31 +#define HID_USAGE_CURSOR_POSITION_REPORT 1, 0x32 +#define HID_USAGE_ROW 1, 0x33 +#define HID_USAGE_COLUMN 1, 0x34 +#define HID_USAGE_ROWS 1, 0x35 +#define HID_USAGE_COLUMNS 1, 0x36 +#define HID_USAGE_CURSOR_PIXEL_POSITION 1, 0x37 +#define HID_USAGE_CURSOR_MODE 1, 0x38 +#define HID_USAGE_CURSOR_ENABLE 1, 0x39 +#define HID_USAGE_CURSOR_BLINK 1, 0x3a +#define HID_USAGE_FONT_REPORT 1, 0x3b +#define HID_USAGE_FONT_DATA 1, 0x3c +#define HID_USAGE_CHARACTER_WIDTH 1, 0x3d +#define HID_USAGE_CHARACTER_HEIGHT 1, 0x3e +#define HID_USAGE_SPACING_HORIZONTAL 1, 0x3f +#define HID_USAGE_SPACING_VERTICAL 1, 0x40 +#define HID_USAGE_UNICODE_CHARACTER_SET 1, 0x41 +#define HID_USAGE_FONT_SEVEN_SEGMENT 1, 0x42 +#define HID_USAGE_SEVEN_SEGMENT_DIRECT_MAP 1, 0x43 +#define HID_USAGE_FONT_FOURTEEN_SEGMENT 1, 0x44 +#define HID_USAGE_FOURTEEN_SEGMENT_DIRECT_MAP 1, 0x45 +#define HID_USAGE_DISPLAY_BRIGHTNESS 1, 0x46 +#define HID_USAGE_DISPLAY_CONTRAST 1, 0x47 +#define HID_USAGE_CHARACTER_ATTRIBUTE 1, 0x48 +#define HID_USAGE_ATTRIBUTE_READBACK 1, 0x49 +#define HID_USAGE_ATTRIBUTE_DATA 1, 0x4a +#define HID_USAGE_CHAR_ATTR_ENHANCE 1, 0x4b +#define HID_USAGE_CHAR_ATTR_UNDERLINE 1, 0x4c +#define HID_USAGE_CHAR_ATTR_BLINK 1, 0x4d + +#define HID_USAGE_BITMAP_SIZE_X 1, 0x80 +#define HID_USAGE_BITMAP_SIZE_Y 1, 0x81 + +#define HID_USAGE_BIT_DEPTH_FORMAT 1, 0x83 +#define HID_USAGE_DISPLAY_ORIENTATION 1, 0x84 +#define HID_USAGE_PALETTE_REPORT 1, 0x85 +#define HID_USAGE_PALETTE_DATA_SIZE 1, 0x86 +#define HID_USAGE_PALETTE_DATA_OFFSET 1, 0x87 +#define HID_USAGE_PALETTE_DATA 1, 0x88 +#define HID_USAGE_BLIT_REPORT 1, 0x8a +#define HID_USAGE_BLIT_RECTANGLE_X1 1, 0x8b +#define HID_USAGE_BLIT_RECTANGLE_Y1 1, 0x8c +#define HID_USAGE_BLIT_RECTANGLE_X2 1, 0x8d +#define HID_USAGE_BLIT_RECTANGLE_Y2 1, 0x8e +#define HID_USAGE_BLIT_DATA 1, 0x8f +#define HID_USAGE_SOFT_BUTTON 1, 0x90 +#define HID_USAGE_SOFT_BUTTON_ID 1, 0x91 +#define HID_USAGE_SOFT_BUTTON_SIDE 1, 0x92 +#define HID_USAGE_SOFT_BUTTON_OFFSET_1 1, 0x93 +#define HID_USAGE_SOFT_BUTTON_OFFSET_2 1, 0x94 +#define HID_USAGE_SOFT_BUTTON_REPORT 1, 0x95 + +/* Medical Instrument Usages */ +#define HID_USAGE_MEDICAL_ULTRASOUND 1, 0x01 + +#define HID_USAGE_VCR_ACQUISITION 1, 0x20 +#define HID_USAGE_FREEZE_THAW 1, 0x21 +#define HID_USAGE_CLIP_STORE 1, 0x22 +#define HID_USAGE_UPDATE 1, 0x23 +#define HID_USAGE_NEXT 1, 0x24 +#define HID_USAGE_SAVE 1, 0x25 +#define HID_USAGE_PRINT 1, 0x26 +#define HID_USAGE_MICROPHONE_ENABLE 1, 0x27 + +#define HID_USAGE_CINE 1, 0x40 +#define HID_USAGE_TRANSMIT_POWER 1, 0x41 +#define HID_USAGE_SOUND_VOLUME 1, 0x42 +#define HID_USAGE_FOCUS 1, 0x43 +#define HID_USAGE_DEPTH 1, 0x44 + +#define HID_USAGE_SOFT_STEP_PRIMARY 1, 0x60 +#define HID_USAGE_SOFT_STEP_SECONDARY 1, 0x61 + +#define HID_USAGE_DEPTH_GAIN_COMPENSATION 1, 0x70 + +#define HID_USAGE_ZOOM_SELECT 1, 0x80 +#define HID_USAGE_ZOOM_ADJUST 1, 0x81 +#define HID_USAGE_SPECTRAL_DOPPLER_MODE_SELECT 1, 0x82 +#define HID_USAGE_SPECTRAL_DOPPLER_ADJUST 1, 0x83 +#define HID_USAGE_COLOR_DOPPLER_MODE_SELECT 1, 0x84 +#define HID_USAGE_COLOR_DOPPLER_ADJUST 1, 0x85 +#define HID_USAGE_MOTION_MODE_SELECT 1, 0x86 +#define HID_USAGE_MOTION_MODE_ADJUST 1, 0x87 +#define HID_USAGE_MODE_2D_SELECT 1, 0x88 +#define HID_USAGE_MODE_2D_ADJUST 1, 0x89 + +#define HID_USAGE_SOFT_CONTROL_SELECT 1, 0xa0 +#define HID_USAGE_SOFT_CONTROL_ADJUST 1, 0xa1 + +/* Power Device Usages */ +#define HID_USAGE_INAME 1, 0x01 +#define HID_USAGE_PRESENT_STATUS 1, 0x02 +#define HID_USAGE_CHANGED_STATUS 1, 0x03 +#define HID_USAGE_UPS 1, 0x04 +#define HID_USAGE_POWER_SUPPLY 1, 0x05 + +#define HID_USAGE_BATTERY_SYSTEM 1, 0x10 +#define HID_USAGE_BATTERY_SYSTEM_ID 1, 0x11 +#define HID_USAGE_BATTERY 1, 0x12 +#define HID_USAGE_BATTERY_ID 1, 0x13 +#define HID_USAGE_CHARGER 1, 0x14 +#define HID_USAGE_CHARGER_ID 1, 0x15 +#define HID_USAGE_POWER_CONVERTER 1, 0x16 +#define HID_USAGE_POWER_CONVERTER_ID 1, 0x17 +#define HID_USAGE_OUTLET_SYSTEM 1, 0x18 +#define HID_USAGE_OUTLET_SYSTEM_ID 1, 0x19 +#define HID_USAGE_INPUT 1, 0x1a +#define HID_USAGE_INPUT_ID 1, 0x1b +#define HID_USAGE_OUTPUT 1, 0x1c +#define HID_USAGE_OUTPUT_ID 1, 0x1d +#define HID_USAGE_FLOW 1, 0x1e +#define HID_USAGE_FLOW_ID 1, 0x1f +#define HID_USAGE_OUTLET 1, 0x20 +#define HID_USAGE_OUTLET_ID 1, 0x21 +#define HID_USAGE_GANG 1, 0x22 +#define HID_USAGE_GANG_ID 1, 0x23 +#define HID_USAGE_POWER_SUMMARY 1, 0x24 +#define HID_USAGE_POWER_SUMMARY_ID 1, 0x25 + +#define HID_USAGE_VOLTAGE 1, 0x30 +#define HID_USAGE_CURRENT 1, 0x31 +#define HID_USAGE_FREQUENCY 1, 0x32 +#define HID_USAGE_APPARENT_POWER 1, 0x33 +#define HID_USAGE_ACTIVE_POWER 1, 0x34 +#define HID_USAGE_PERCENT_LOAD 1, 0x35 +#define HID_USAGE_TEMPERATURE 1, 0x36 +#define HID_USAGE_HUMIDITY 1, 0x37 +#define HID_USAGE_BAD_COUNT 1, 0x38 + +#define HID_USAGE_CONFIG_VOLTAGE 1, 0x40 +#define HID_USAGE_CONFIG_CURRENT 1, 0x41 +#define HID_USAGE_CONFIG_FREQUENCY 1, 0x42 +#define HID_USAGE_CONFIG_APPARENT_POWER 1, 0x43 +#define HID_USAGE_CONFIG_ACTIVE_POWER 1, 0x44 +#define HID_USAGE_CONFIG_PERCENT_LOAD 1, 0x45 +#define HID_USAGE_CONFIG_TEMPERATURE 1, 0x46 +#define HID_USAGE_CONFIG_HUMIDITY 1, 0x47 + +#define HID_USAGE_SWITCH_ON_CONTROL 1, 0x50 +#define HID_USAGE_SWITCH_OFF_CONTROL 1, 0x51 +#define HID_USAGE_TOGGLE_CONTROL 1, 0x52 +#define HID_USAGE_LOW_VOLTAGE_TRANSFER 1, 0x53 +#define HID_USAGE_HIGH_VOLTAGE_TRANSFER 1, 0x54 +#define HID_USAGE_DELAY_BEFORE_REBOOT 1, 0x55 +#define HID_USAGE_DELAY_BEFORE_STARTUP 1, 0x56 +#define HID_USAGE_DELAY_BEFORE_SHUTDOWN 1, 0x57 +#define HID_USAGE_TEST 1, 0x58 +#define HID_USAGE_MODULE_RESET 1, 0x59 +#define HID_USAGE_AUDIBLE_ALARM_CONTROL 1, 0x5a + +#define HID_USAGE_PRESENT 1, 0x60 +#define HID_USAGE_GOOD 1, 0x61 +#define HID_USAGE_INTERNAL_FAILURE 1, 0x62 +#define HID_USAGE_VOLTAGE_OUT_OF_RANGE_ 1, 0x63 +#define HID_USAGE_FREQUENCY_OUT_OF_RANGE 1, 0x64 +#define HID_USAGE_OVER_LOAD 1, 0x65 +#define HID_USAGE_OVER_CHARGED 1, 0x66 +#define HID_USAGE_OVER_TEMPERATURE 1, 0x67 +#define HID_USAGE_SHUTDOWN_REQUESTED 1, 0x68 +#define HID_USAGE_SHUTDOWN_IMMINENT 1, 0x69 + +#define HID_USAGE_SWITCH_ON_OFF 1, 0x6b +#define HID_USAGE_SWITCHABLE 1, 0x6c +#define HID_USAGE_USED 1, 0x6d +#define HID_USAGE_BOOST 1, 0x6e +#define HID_USAGE_BUCK 1, 0x6f + +#define HID_USAGE_INITIALIZED 1, 0x70 +#define HID_USAGE_TESTED 1, 0x71 +#define HID_USAGE_AWAITING_POWER 1, 0x72 +#define HID_USAGE_COMMUNICATION_LOST 1, 0x73 + +#define HID_USAGE_IMANUFACTURER 1, 0xfd +#define HID_USAGE_IPRODUCT 1, 0xfe +#define HID_USAGE_ISERIAL_NUMBER 1, 0xff + +/* Battery System Usages */ +#define HID_USAGE_SMB_BATTERY_MODE 1, 0x01 +#define HID_USAGE_SMB_BATTERY_STATUS 1, 0x02 +#define HID_USAGE_SMB_ALARM_WARNING 1, 0x03 +#define HID_USAGE_SMB_CHARGER_MODE 1, 0x04 +#define HID_USAGE_SMB_CHARGER_STATUS 1, 0x05 +#define HID_USAGE_SMB_CHARGER_SPEC_INFO 1, 0x06 +#define HID_USAGE_SMB_SELECTOR_STATE 1, 0x07 +#define HID_USAGE_SMB_SELECTOR_PRESETS 1, 0x08 +#define HID_USAGE_SMB_SELECTOR_INFO 1, 0x09 + +#define HID_USAGE_OPTIONAL_MFG_FUNCTION(n) 1, (0x09 + (_ASSERT(1 <= (n) && (n) <= 5) ? (n) : (0))) /* n: [1..5] */ +#define HID_USAGE_CONNECTION_TO_SMBUS 1, 0x15 +#define HID_USAGE_OUTPUT_CONNECTION 1, 0x16 +#define HID_USAGE_CHARGER_CONNECTION 1, 0x17 +#define HID_USAGE_BATTERY_INSERTION 1, 0x18 +#define HID_USAGE_USE_NEXT 1, 0x19 +#define HID_USAGE_OK_TO_USE 1, 0x1a +#define HID_USAGE_BATTERY_SUPPORTED 1, 0x1b +#define HID_USAGE_SELECTOR_REVISION 1, 0x1c +#define HID_USAGE_CHARGING_INDICATOR 1, 0x1d + +#define HID_USAGE_MANUFACTURER_ACCESS 1, 0x28 +#define HID_USAGE_REMAINING_CAPACITY_LIMIT 1, 0x29 +#define HID_USAGE_REMAINING_TIME_LIMIT 1, 0x2a +#define HID_USAGE_AT_RATE 1, 0x2b +#define HID_USAGE_CAPACITY_MODE 1, 0x2c +#define HID_USAGE_BROADCAST_TO_CHARGER 1, 0x2d +#define HID_USAGE_PRIMARY_BATTERY 1, 0x2e +#define HID_USAGE_CHARGE_CONTROLLER 1, 0x2f + +#define HID_USAGE_TERMINATE_CHARGE 1, 0x40 +#define HID_USAGE_TERMINATE_DISCHARGE 1, 0x41 +#define HID_USAGE_BELOW_REMAINING_CAPACITY_LIMIT 1, 0x42 +#define HID_USAGE_REMAINING_TIME_LIMIT_EXPIRED 1, 0x43 +#define HID_USAGE_CHARGING 1, 0x44 +#define HID_USAGE_DISCHARGING 1, 0x45 +#define HID_USAGE_FULLY_CHARGED 1, 0x46 +#define HID_USAGE_FULLY_DISCHARGED 1, 0x47 +#define HID_USAGE_CONDITIONING_FLAG 1, 0x48 +#define HID_USAGE_AT_RATE_OK 1, 0x49 +#define HID_USAGE_SMB_ERROR_CODE 1, 0x4a +#define HID_USAGE_NEED_REPLACEMENT 1, 0x4b + +#define HID_USAGE_AT_RATE_TIME_TO_FULL 1, 0x60 +#define HID_USAGE_AT_RATE_TIME_TO_EMPTY 1, 0x61 +#define HID_USAGE_AVERAGE_CURRENT 1, 0x62 +#define HID_USAGE_MAX_ERROR 1, 0x63 +#define HID_USAGE_RELATIVE_STATE_OF_CHARGE 1, 0x64 +#define HID_USAGE_ABSOLUTE_STATE_OF_CHARGE 1, 0x65 +#define HID_USAGE_REMAINING_CAPACITY 1, 0x66 +#define HID_USAGE_FULL_CHARGE_CAPACITY 1, 0x67 +#define HID_USAGE_RUN_TIME_TO_EMPTY 1, 0x68 +#define HID_USAGE_AVERAGE_TIME_TO_EMPTY 1, 0x69 +#define HID_USAGE_AVERAGE_TIME_TO_FULL 1, 0x6a +#define HID_USAGE_CYCLE_COUNT 1, 0x6b + +#define HID_USAGE_BATTERY_PACK_MODEL_LEVEL 1, 0x80 +#define HID_USAGE_INTERNAL_CHARGE_CONTROLLER 1, 0x81 +#define HID_USAGE_PRIMARY_BATTERY_SUPPORT 1, 0x82 +#define HID_USAGE_DESIGN_CAPACITY 1, 0x83 +#define HID_USAGE_SPECIFICATION_INFO 1, 0x84 +#define HID_USAGE_MANUFACTURER_DATE 1, 0x85 +#define HID_USAGE_SERIAL_NUMBER 1, 0x86 +#define HID_USAGE_IMANUFACTURER_NAME 1, 0x87 +#define HID_USAGE_IDEVICE_NAME 1, 0x88 +#define HID_USAGE_IDEVICE_CHEMISTERY 1, 0x89 +#define HID_USAGE_MANUFACTURER_DATA 1, 0x8a +#define HID_USAGE_RECHARGABLE 1, 0x8b +#define HID_USAGE_WARNING_CAPACITY_LIMIT 1, 0x8c +#define HID_USAGE_CAPACITY_GRANULARITY(n) 1, (0x8c + (_ASSERT(1 <= (n) && (n) <= 2) ? (n) : (n))) /* n: [1..2] */ +#define HID_USAGE_IOEM_INFORMATION 1, 0x8f + +#define HID_USAGE_INHIBIT_CHARGE 1, 0xc0 +#define HID_USAGE_ENABLE_POLLING 1, 0xc1 +#define HID_USAGE_RESET_TO_ZERO 1, 0xc2 + +#define HID_USAGE_AC_PRESENT 1, 0xd0 +#define HID_USAGE_BATTERY_PRESENT 1, 0xd1 +#define HID_USAGE_POWER_FAIL 1, 0xd2 +#define HID_USAGE_ALARM_INHIBITED 1, 0xd3 +#define HID_USAGE_THERMISTOR_UNDER_RANGE 1, 0xd4 +#define HID_USAGE_THERMISTOR_HOT 1, 0xd5 +#define HID_USAGE_THERMISTOR_COLD 1, 0xd6 +#define HID_USAGE_THERMISTOR_OVER_RANGE 1, 0xd7 +#define HID_USAGE_VOLTAGE_OUT_OF_RANGE 1, 0xd8 +#define HID_USAGE_CURRENT_OUT_OF_RANGE 1, 0xd9 +#define HID_USAGE_CURRENT_NOT_REGULATED 1, 0xda +#define HID_USAGE_VOLTAGE_NOT_REGULATED 1, 0xdb +#define HID_USAGE_MASTER_MODE 1, 0xdc + +#define HID_USAGE_CHARGER_SELECTOR_SUPPORT 1, 0xf0 +#define HID_USAGE_CHARGER_SPEC 1, 0xf1 +#define HID_USAGE_LEVEL(n) 1, (0xf0 + (_ASSERT(2 <= (n) && (n) <= 3) ? (n) : (0))) /* n: [2..3] */ + +/* End of Usages */ + +#define HID_USAGE2(n, x) HID_ELEM(0x0, LOCAL, n, x) +#define HID_USAGE(x) _CALL(HID_USAGE2, _CAT2(HID_USAGE_, x)) + +#define HID_PAGED_USAGE(p, u) HID_ELEM(0x0, LOCAL, 4, (_NTH1(_CAT2(HID_USAGE_PAGE_, p)) << 16) | _NTH1(_CAT2(HID_USAGE_, u))) + +#define HID_USAGE_MINIMUM(n, x) HID_ELEM(0x1, LOCAL, n, x) +#define HID_USAGE_MAXIMUM(n, x) HID_ELEM(0x2, LOCAL, n, x) +#define HID_DESIGNATOR_INDEX(n, x) HID_ELEM(0x3, LOCAL, n, x) +#define HID_DESIGNATOR_MINIMUM(n, x) HID_ELEM(0x4, LOCAL, n, x) +#define HID_DESIGNATOR_MAXIMUM(n, x) HID_ELEM(0x5, LOCAL, n, x) +#define HID_STRING_INDEX(n, x) HID_ELEM(0x7, LOCAL, n, x) +#define HID_STRING_MINIMUM(n, x) HID_ELEM(0x8, LOCAL, n, x) +#define HID_STRING_MAXIMUM(n, x) HID_ELEM(0x9, LOCAL, n, x) +#define HID_DELIMITER(x) HID_ELEM(0xa, LOCAL, 1, x) +#define HID_DELIMITER_OPEN HID_DELIMITER(1) +#define HID_DELIMITER_CLOSE HID_DELIMITER(0) diff --git a/src/usb_inc/hid_macro.h b/src/usb_inc/hid_macro.h new file mode 100644 index 0000000..dbb656b --- /dev/null +++ b/src/usb_inc/hid_macro.h @@ -0,0 +1,69 @@ +// https://github.com/katyo/hid_def + +#pragma once + +#define _NONE(...) + +#define _UNWR_(...) __VA_ARGS__ +#define _UNWR(a) _UNWR_ a + +#define _CAT2_(a, b) a##b +#define _CAT2(a, b) _CAT2_(a, b) + +#define _CAT3_(a, b, c) a##b##c +#define _CAT3(a, b, c) _CAT3_(a, b, c) + +#define _CAT4_(a, b, c, d) a##b##c##d +#define _CAT4(a, b, c, d) _CAT4_(a, b, c, d) + +#define _CAT5_(a, b, c, d, e) a##b##c##d##e +#define _CAT5(a, b, c, d, e) _CAT5_(a, b, c, d, e) + +#define _NTH0_(a, ...) a +#define _NTH0(...) _NTH0_(__VA_ARGS__) + +#define _NTH1_(a, b, ...) b +#define _NTH1(...) _NTH1_(__VA_ARGS__) + +#define _NTH2_(a, b, c, ...) c +#define _NTH2(...) _NTH2_(__VA_ARGS__) + +#define _NTH3_(a, b, c, d, ...) d +#define _NTH3(...) _NTH3_(__VA_ARGS__) + +#define _NTH4_(a, b, c, d, e, ...) e +#define _NTH4(...) _NTH4_(__VA_ARGS__) + +#define _MAX2(a, b) ((a) > (b) ? (a) : (b)) +#define _MIN2(a, b) ((a) < (b) ? (a) : (b)) + +#define _CALL_(f, ...) f(__VA_ARGS__) +#define _CALL(f, ...) _CALL_(f, ##__VA_ARGS__) + +#define _EVAL0(...) __VA_ARGS__ +#define _EVAL1(...) _EVAL0(_EVAL0(_EVAL0(__VA_ARGS__))) +#define _EVAL2(...) _EVAL1(_EVAL1(_EVAL1(__VA_ARGS__))) +#define _EVAL3(...) _EVAL2(_EVAL2(_EVAL2(__VA_ARGS__))) +#define _EVAL4(...) _EVAL3(_EVAL3(_EVAL3(__VA_ARGS__))) +#define _EVAL(...) _EVAL4(_EVAL4(_EVAL4(__VA_ARGS__))) + +#define _MAP_END(...) +#define _MAP_OUT +#define _MAP_COMMA , + +#define _MAP_GET_END2() 0, _MAP_END +#define _MAP_GET_END1(...) _MAP_GET_END2 +#define _MAP_GET_END(...) _MAP_GET_END1 +#define _MAP_NEXT0(test, next, ...) next _MAP_OUT +#define _MAP_NEXT1(test, next) _MAP_NEXT0(test, next, 0) +#define _MAP_NEXT(test, next) _MAP_NEXT1(_MAP_GET_END test, next) + +#define _MAP0(f, x, peek, ...) f(x) _MAP_NEXT(peek, _MAP1)(f, peek, __VA_ARGS__) +#define _MAP1(f, x, peek, ...) f(x) _MAP_NEXT(peek, _MAP0)(f, peek, __VA_ARGS__) + +/** + * Applies the function macro `f` to each of the remaining parameters. + */ +#define _MAP(f, ...) _EVAL(_MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +#define _ASSERT(x) ((1/(!!(x))) ? (x) : (x)) diff --git a/src/usb_inc/keymap.h b/src/usb_inc/keymap.h new file mode 100644 index 0000000..6a1dd88 --- /dev/null +++ b/src/usb_inc/keymap.h @@ -0,0 +1,769 @@ +#pragma once + +#include + +// Keyboard Modifiers +enum KeyboardMods { + MOD_LEFT_CTRL = (1 << 8), + MOD_LEFT_SHIFT = (1 << 9), + MOD_LEFT_ALT = (1 << 10), + MOD_LEFT_GUI = (1 << 11), + MOD_RIGHT_CTRL = (1 << 12), + MOD_RIGHT_SHIFT = (1 << 13), + MOD_RIGHT_ALT = (1 << 14), + MOD_RIGHT_GUI = (uint16_t)(1 << 15), +}; + +// Keyboard Leds +enum KeyboardLeds { + LED_NUM_LOCK = (1 << 0), + LED_CAPS_LOCK = (1 << 1), + LED_SCROLL_LOCK = (1 << 2), + LED_COMPOSE = (1 << 3), + LED_KANA = (1 << 4), + LED_POWER = (1 << 5), + LED_SHIFT = (1 << 6), + LED_DO_NOT_DISTURB = (1 << 7), +}; + +enum KeyboardKeycode { + KEY_RESERVED = 0, + KEY_ERROR_ROLLOVER = 1, + KEY_POST_FAIL = 2, + KEY_ERROR_UNDEFINED = 3, + KEY_A = 4, + KEY_B = 5, + KEY_C = 6, + KEY_D = 7, + KEY_E = 8, + KEY_F = 9, + KEY_G = 10, + KEY_H = 11, + KEY_I = 12, + KEY_J = 13, + KEY_K = 14, + KEY_L = 15, + KEY_M = 16, + KEY_N = 17, + KEY_O = 18, + KEY_P = 19, + KEY_Q = 20, + KEY_R = 21, + KEY_S = 22, + KEY_T = 23, + KEY_U = 24, + KEY_V = 25, + KEY_W = 26, + KEY_X = 27, + KEY_Y = 28, + KEY_Z = 29, + KEY_1 = 30, + KEY_2 = 31, + KEY_3 = 32, + KEY_4 = 33, + KEY_5 = 34, + KEY_6 = 35, + KEY_7 = 36, + KEY_8 = 37, + KEY_9 = 38, + KEY_0 = 39, + KEY_ENTER = 40, + KEY_RETURN = 40, // Alias + KEY_ESC = 41, + KEY_BACKSPACE = 42, + KEY_TAB = 43, + KEY_SPACE = 44, + KEY_MINUS = 45, + KEY_EQUAL = 46, + KEY_LEFT_BRACE = 47, + KEY_RIGHT_BRACE = 48, + KEY_BACKSLASH = 49, + KEY_NON_US_NUM = 50, + KEY_SEMICOLON = 51, + KEY_QUOTE = 52, + KEY_TILDE = 53, + KEY_COMMA = 54, + KEY_PERIOD = 55, + KEY_SLASH = 56, + KEY_CAPS_LOCK = 0x39, + KEY_F1 = 0x3A, + KEY_F2 = 0x3B, + KEY_F3 = 0x3C, + KEY_F4 = 0x3D, + KEY_F5 = 0x3E, + KEY_F6 = 0x3F, + KEY_F7 = 0x40, + KEY_F8 = 0x41, + KEY_F9 = 0x42, + KEY_F10 = 0x43, + KEY_F11 = 0x44, + KEY_F12 = 0x45, + KEY_PRINT = 0x46, + KEY_PRINTSCREEN = 0x46, // Alias + KEY_SCROLL_LOCK = 0x47, + KEY_PAUSE = 0x48, + KEY_INSERT = 0x49, + KEY_HOME = 0x4A, + KEY_PAGE_UP = 0x4B, + KEY_DELETE = 0x4C, + KEY_END = 0x4D, + KEY_PAGE_DOWN = 0x4E, + KEY_RIGHT_ARROW = 0x4F, + KEY_LEFT_ARROW = 0x50, + KEY_DOWN_ARROW = 0x51, + KEY_UP_ARROW = 0x52, + KEY_RIGHT = 0x4F, // Alias + KEY_LEFT = 0x50, // Alias + KEY_DOWN = 0x51, // Alias + KEY_UP = 0x52, // Alias + KEY_NUM_LOCK = 0x53, + KEYPAD_DIVIDE = 0x54, + KEYPAD_MULTIPLY = 0x55, + KEYPAD_SUBTRACT = 0x56, + KEYPAD_ADD = 0x57, + KEYPAD_ENTER = 0x58, + KEYPAD_1 = 0x59, + KEYPAD_2 = 0x5A, + KEYPAD_3 = 0x5B, + KEYPAD_4 = 0x5C, + KEYPAD_5 = 0x5D, + KEYPAD_6 = 0x5E, + KEYPAD_7 = 0x5F, + KEYPAD_8 = 0x60, + KEYPAD_9 = 0x61, + KEYPAD_0 = 0x62, + KEYPAD_DOT = 0x63, + KEY_NON_US = 0x64, + KEY_APPLICATION = 0x65, // Context menu/right click + KEY_MENU = 0x65, // Alias + + // Most of the following keys will only work with Linux or not at all. + // F13+ keys are mostly used for laptop functions like ECO key. + KEY_POWER = 0x66, // PowerOff (Ubuntu) + KEY_PAD_EQUALS = 0x67, // Dont confuse with KEYPAD_EQUAL_SIGN + KEY_F13 = 0x68, // Tools (Ubunutu) + KEY_F14 = 0x69, // Launch5 (Ubuntu) + KEY_F15 = 0x6A, // Launch6 (Ubuntu) + KEY_F16 = 0x6B, // Launch7 (Ubuntu) + KEY_F17 = 0x6C, // Launch8 (Ubuntu) + KEY_F18 = 0x6D, // Launch9 (Ubuntu) + KEY_F19 = 0x6E, // Disabled (Ubuntu) + KEY_F20 = 0x6F, // AudioMicMute (Ubuntu) + KEY_F21 = 0x70, // Touchpad toggle (Ubuntu) + KEY_F22 = 0x71, // TouchpadOn (Ubuntu) + KEY_F23 = 0x72, // TouchpadOff Ubuntu) + KEY_F24 = 0x73, // Disabled (Ubuntu) + KEY_EXECUTE = 0x74, // Open (Ubuntu) + KEY_HELP = 0x75, // Help (Ubuntu) + KEY_MENU2 = 0x76, // Disabled (Ubuntu) + KEY_SELECT = 0x77, // Disabled (Ubuntu) + KEY_STOP = 0x78, // Cancel (Ubuntu) + KEY_AGAIN = 0x79, // Redo (Ubuntu) + KEY_UNDO = 0x7A, // Undo (Ubuntu) + KEY_CUT = 0x7B, // Cut (Ubuntu) + KEY_COPY = 0x7C, // Copy (Ubuntu) + KEY_PASTE = 0x7D, // Paste (Ubuntu) + KEY_FIND = 0x7E, // Find (Ubuntu) + KEY_MUTE = 0x7F, + KEY_VOLUME_MUTE = 0x7F, // Alias + KEY_VOLUME_UP = 0x80, + KEY_VOLUME_DOWN = 0x81, + KEY_LOCKING_CAPS_LOCK = 0x82, // Disabled (Ubuntu) + KEY_LOCKING_NUM_LOCK = 0x83, // Disabled (Ubuntu) + KEY_LOCKING_SCROLL_LOCK = 0x84, // Disabled (Ubuntu) + KEYPAD_COMMA = 0x85, // . + KEYPAD_EQUAL_SIGN = 0x86, // Disabled (Ubuntu), Dont confuse with KEYPAD_EQUAL + KEY_INTERNATIONAL1 = 0x87, // Disabled (Ubuntu) + KEY_INTERNATIONAL2 = 0x88, // Hiragana Katakana (Ubuntu) + KEY_INTERNATIONAL3 = 0x89, // Disabled (Ubuntu) + KEY_INTERNATIONAL4 = 0x8A, // Henkan (Ubuntu) + KEY_INTERNATIONAL5 = 0x8B, // Muhenkan (Ubuntu) + KEY_INTERNATIONAL6 = 0x8C, // Disabled (Ubuntu) + KEY_INTERNATIONAL7 = 0x8D, // Disabled (Ubuntu) + KEY_INTERNATIONAL8 = 0x8E, // Disabled (Ubuntu) + KEY_INTERNATIONAL9 = 0x8F, // Disabled (Ubuntu) + KEY_LANG1 = 0x90, // Disabled (Ubuntu) + KEY_LANG2 = 0x91, // Disabled (Ubuntu) + KEY_LANG3 = 0x92, // Katana (Ubuntu) + KEY_LANG4 = 0x93, // Hiragana (Ubuntu) + KEY_LANG5 = 0x94, // Disabled (Ubuntu) + KEY_LANG6 = 0x95, // Disabled (Ubuntu) + KEY_LANG7 = 0x96, // Disabled (Ubuntu) + KEY_LANG8 = 0x97, // Disabled (Ubuntu) + KEY_LANG9 = 0x98, // Disabled (Ubuntu) + KEY_ALTERNATE_ERASE = 0x99, // Disabled (Ubuntu) + KEY_SYSREQ_ATTENTION = 0x9A, // Disabled (Ubuntu) + KEY_CANCEL = 0x9B, // Disabled (Ubuntu) + KEY_CLEAR = 0x9C, // Delete (Ubuntu) + KEY_PRIOR = 0x9D, // Disabled (Ubuntu) + KEY_RETURN2 = 0x9E, // Disabled (Ubuntu), Do not confuse this with KEY_ENTER + KEY_SEPARATOR = 0x9F, // Disabled (Ubuntu) + KEY_OUT = 0xA0, // Disabled (Ubuntu) + KEY_OPER = 0xA1, // Disabled (Ubuntu) + KEY_CLEAR_AGAIN = 0xA2, // Disabled (Ubuntu) + KEY_CRSEL_PROPS = 0xA3, // Disabled (Ubuntu) + KEY_EXSEL = 0xA4, // Disabled (Ubuntu) + + KEY_PAD_00 = 0xB0, // Disabled (Ubuntu) + KEY_PAD_000 = 0xB1, // Disabled (Ubuntu) + KEY_THOUSANDS_SEPARATOR = 0xB2, // Disabled (Ubuntu) + KEY_DECIMAL_SEPARATOR = 0xB3, // Disabled (Ubuntu) + KEY_CURRENCY_UNIT = 0xB4, // Disabled (Ubuntu) + KEY_CURRENCY_SUB_UNIT = 0xB5, // Disabled (Ubuntu) + KEYPAD_LEFT_BRACE = 0xB6, // ( + KEYPAD_RIGHT_BRACE = 0xB7, // ) + KEYPAD_LEFT_CURLY_BRACE = 0xB8, // Disabled (Ubuntu) + KEYPAD_RIGHT_CURLY_BRACE = 0xB9, // Disabled (Ubuntu) + KEYPAD_TAB = 0xBA, // Disabled (Ubuntu) + KEYPAD_BACKSPACE = 0xBB, // Disabled (Ubuntu) + KEYPAD_A = 0xBC, // Disabled (Ubuntu) + KEYPAD_B = 0xBD, // Disabled (Ubuntu) + KEYPAD_C = 0xBE, // Disabled (Ubuntu) + KEYPAD_D = 0xBF, // Disabled (Ubuntu) + KEYPAD_E = 0xC0, // Disabled (Ubuntu) + KEYPAD_F = 0xC1, // Disabled (Ubuntu) + KEYPAD_XOR = 0xC2, // Disabled (Ubuntu) + KEYPAD_CARET = 0xC3, // Disabled (Ubuntu) + KEYPAD_PERCENT = 0xC4, // Disabled (Ubuntu) + KEYPAD_LESS_THAN = 0xC5, // Disabled (Ubuntu) + KEYPAD_GREATER_THAN = 0xC6, // Disabled (Ubuntu) + KEYPAD_AMPERSAND = 0xC7, // Disabled (Ubuntu) + KEYPAD_DOUBLEAMPERSAND = 0xC8, // Disabled (Ubuntu) + KEYPAD_PIPE = 0xC9, // Disabled (Ubuntu) + KEYPAD_DOUBLEPIPE = 0xCA, // Disabled (Ubuntu) + KEYPAD_COLON = 0xCB, // Disabled (Ubuntu) + KEYPAD_POUND_SIGN = 0xCC, // Disabled (Ubuntu) + KEYPAD_SPACE = 0xCD, // Disabled (Ubuntu) + KEYPAD_AT_SIGN = 0xCE, // Disabled (Ubuntu) + KEYPAD_EXCLAMATION_POINT = 0xCF, // Disabled (Ubuntu) + KEYPAD_MEMORY_STORE = 0xD0, // Disabled (Ubuntu) + KEYPAD_MEMORY_RECALL = 0xD1, // Disabled (Ubuntu) + KEYPAD_MEMORY_CLEAR = 0xD2, // Disabled (Ubuntu) + KEYPAD_MEMORY_ADD = 0xD3, // Disabled (Ubuntu) + KEYPAD_MEMORY_SUBTRACT = 0xD4, // Disabled (Ubuntu) + KEYPAD_MEMORY_MULTIPLY = 0xD5, // Disabled (Ubuntu) + KEYPAD_MEMORY_DIVIDE = 0xD6, // Disabled (Ubuntu) + KEYPAD_PLUS_MINUS = 0xD7, // Disabled (Ubuntu) + KEYPAD_CLEAR = 0xD8, // Delete (Ubuntu) + KEYPAD_CLEAR_ENTRY = 0xD9, // Disabled (Ubuntu) + KEYPAD_BINARY = 0xDA, // Disabled (Ubuntu) + KEYPAD_OCTAL = 0xDB, // Disabled (Ubuntu) + KEYPAD_DECIMAL = 0xDC, // Disabled (Ubuntu) + KEYPAD_HEXADECIMAL = 0xDD, // Disabled (Ubuntu) + + KEY_LEFT_CTRL = 0xE0, + KEY_LEFT_SHIFT = 0xE1, + KEY_LEFT_ALT = 0xE2, + KEY_LEFT_GUI = 0xE3, + KEY_LEFT_WINDOWS = 0xE3, // Alias + KEY_RIGHT_CTRL = 0xE4, + KEY_RIGHT_SHIFT = 0xE5, + KEY_RIGHT_ALT = 0xE6, + KEY_RIGHT_GUI = 0xE7, + KEY_RIGHT_WINDOWS = 0xE7, // Alias + + // Keyboard HID mappings + + // Reserved (no_event_indicated) + HID_KEYBOARD_ERROR_ROLLOVER = 0x01, + HID_KEYBOARD_POST_FAIL = 0x02, + HID_KEYBOARD_ERROR_UNDEFINED = 0x03, + HID_KEYBOARD_A_AND_A = 0x04, + HID_KEYBOARD_B_AND_B = 0x05, + HID_KEYBOARD_C_AND_C = 0x06, + HID_KEYBOARD_D_AND_D = 0x07, + HID_KEYBOARD_E_AND_E = 0x08, + HID_KEYBOARD_F_AND_F = 0x09, + HID_KEYBOARD_G_AND_G = 0x0A, + HID_KEYBOARD_H_AND_H = 0x0B, + HID_KEYBOARD_I_AND_I = 0x0C, + HID_KEYBOARD_J_AND_J = 0x0D, + HID_KEYBOARD_K_AND_K = 0x0E, + HID_KEYBOARD_L_AND_L = 0x0F, + HID_KEYBOARD_M_AND_M = 0x10, + HID_KEYBOARD_N_AND_N = 0x11, + HID_KEYBOARD_O_AND_O = 0x12, + HID_KEYBOARD_P_AND_P = 0x13, + HID_KEYBOARD_Q_AND_Q = 0x14, + HID_KEYBOARD_R_AND_R = 0x15, + HID_KEYBOARD_S_AND_S = 0x16, + HID_KEYBOARD_T_AND_T = 0x17, + HID_KEYBOARD_U_AND_U = 0x18, + HID_KEYBOARD_V_AND_V = 0x19, + HID_KEYBOARD_W_AND_W = 0x1A, + HID_KEYBOARD_X_AND_X = 0x1B, + HID_KEYBOARD_Y_AND_Y = 0x1C, + HID_KEYBOARD_Z_AND_Z = 0x1D, + HID_KEYBOARD_1_AND_EXCLAMATION_POINT = 0x1E, + HID_KEYBOARD_2_AND_AT = 0x1F, + HID_KEYBOARD_3_AND_POUND = 0x20, + HID_KEYBOARD_4_AND_DOLLAR = 0x21, + HID_KEYBOARD_5_AND_PERCENT = 0x22, + HID_KEYBOARD_6_AND_CARAT = 0x23, + HID_KEYBOARD_7_AND_AMPERSAND = 0x24, + HID_KEYBOARD_8_AND_ASTERISK = 0x25, + HID_KEYBOARD_9_AND_LEFT_PAREN = 0x26, + HID_KEYBOARD_0_AND_RIGHT_PAREN = 0x27, + HID_KEYBOARD_ENTER = 0x28, // (MARKED AS ENTER_SLASH_RETURN) + HID_KEYBOARD_ESCAPE = 0x29, + HID_KEYBOARD_DELETE = 0x2A, // (BACKSPACE) + HID_KEYBOARD_TAB = 0x2B, + HID_KEYBOARD_SPACEBAR = 0x2C, + HID_KEYBOARD_MINUS_AND_UNDERSCORE = 0x2D, // (UNDERSCORE) + HID_KEYBOARD_EQUALS_AND_PLUS = 0x2E, + HID_KEYBOARD_LEFT_BRACKET_AND_LEFT_CURLY_BRACE = 0x2F, + HID_KEYBOARD_RIGHT_BRACKET_AND_RIGHT_CURLY_BRACE = 0x30, + HID_KEYBOARD_BACKSLASH_AND_PIPE = 0x31, + HID_KEYBOARD_NON_US_POUND_AND_TILDE = 0x32, + HID_KEYBOARD_SEMICOLON_AND_COLON = 0x33, + HID_KEYBOARD_QUOTE_AND_DOUBLEQUOTE = 0x34, + HID_KEYBOARD_GRAVE_ACCENT_AND_TILDE = 0x35, + HID_KEYBOARD_COMMA_AND_LESS_THAN = 0x36, + HID_KEYBOARD_PERIOD_AND_GREATER_THAN = 0x37, + HID_KEYBOARD_SLASH_AND_QUESTION_MARK = 0x38, + HID_KEYBOARD_CAPS_LOCK = 0x39, + HID_KEYBOARD_F1 = 0x3A, + HID_KEYBOARD_F2 = 0x3B, + HID_KEYBOARD_F3 = 0x3C, + HID_KEYBOARD_F4 = 0x3D, + HID_KEYBOARD_F5 = 0x3E, + HID_KEYBOARD_F6 = 0x3F, + HID_KEYBOARD_F7 = 0x40, + HID_KEYBOARD_F8 = 0x41, + HID_KEYBOARD_F9 = 0x42, + HID_KEYBOARD_F10 = 0x43, + HID_KEYBOARD_F11 = 0x44, + HID_KEYBOARD_F12 = 0x45, + HID_KEYBOARD_PRINTSCREEN = 0x46, + HID_KEYBOARD_SCROLL_LOCK = 0x47, + HID_KEYBOARD_PAUSE = 0x48, + HID_KEYBOARD_INSERT = 0x49, + HID_KEYBOARD_HOME = 0x4A, + HID_KEYBOARD_PAGE_UP = 0x4B, + HID_KEYBOARD_DELETE_FORWARD = 0x4C, + HID_KEYBOARD_END = 0x4D, + HID_KEYBOARD_PAGE_DOWN = 0x4E, + HID_KEYBOARD_RIGHTARROW = 0x4F, + HID_KEYBOARD_LEFTARROW = 0x50, + HID_KEYBOARD_DOWNARROW = 0x51, + HID_KEYBOARD_UPARROW = 0x52, + HID_KEYPAD_NUM_LOCK_AND_CLEAR = 0x53, + HID_KEYPAD_DIVIDE = 0x54, + HID_KEYPAD_MULTIPLY = 0x55, + HID_KEYPAD_SUBTRACT = 0x56, + HID_KEYPAD_ADD = 0x57, + HID_KEYPAD_ENTER = 0x58, + HID_KEYPAD_1_AND_END = 0x59, + HID_KEYPAD_2_AND_DOWN_ARROW = 0x5A, + HID_KEYPAD_3_AND_PAGE_DOWN = 0x5B, + HID_KEYPAD_4_AND_LEFT_ARROW = 0x5C, + HID_KEYPAD_5 = 0x5D, + HID_KEYPAD_6_AND_RIGHT_ARROW = 0x5E, + HID_KEYPAD_7_AND_HOME = 0x5F, + HID_KEYPAD_8_AND_UP_ARROW = 0x60, + HID_KEYPAD_9_AND_PAGE_UP = 0x61, + HID_KEYPAD_0_AND_INSERT = 0x62, + HID_KEYPAD_PERIOD_AND_DELETE = 0x63, + HID_KEYBOARD_NON_US_BACKSLASH_AND_PIPE = 0x64, + HID_KEYBOARD_APPLICATION = 0x65, + HID_KEYBOARD_POWER = 0x66, + HID_KEYPAD_EQUALS = 0x67, + HID_KEYBOARD_F13 = 0x68, + HID_KEYBOARD_F14 = 0x69, + HID_KEYBOARD_F15 = 0x6A, + HID_KEYBOARD_F16 = 0x6B, + HID_KEYBOARD_F17 = 0x6C, + HID_KEYBOARD_F18 = 0x6D, + HID_KEYBOARD_F19 = 0x6E, + HID_KEYBOARD_F20 = 0x6F, + HID_KEYBOARD_F21 = 0x70, + HID_KEYBOARD_F22 = 0x71, + HID_KEYBOARD_F23 = 0x72, + HID_KEYBOARD_F24 = 0x73, + HID_KEYBOARD_EXECUTE = 0x74, + HID_KEYBOARD_HELP = 0x75, + HID_KEYBOARD_MENU = 0x76, + HID_KEYBOARD_SELECT = 0x77, + HID_KEYBOARD_STOP = 0x78, + HID_KEYBOARD_AGAIN = 0x79, + HID_KEYBOARD_UNDO = 0x7A, + HID_KEYBOARD_CUT = 0x7B, + HID_KEYBOARD_COPY = 0x7C, + HID_KEYBOARD_PASTE = 0x7D, + HID_KEYBOARD_FIND = 0x7E, + HID_KEYBOARD_MUTE = 0x7F, + HID_KEYBOARD_VOLUME_UP = 0x80, + HID_KEYBOARD_VOLUME_DOWN = 0x81, + HID_KEYBOARD_LOCKING_CAPS_LOCK = 0x82, + HID_KEYBOARD_LOCKING_NUM_LOCK = 0x83, + HID_KEYBOARD_LOCKING_SCROLL_LOCK = 0x84, + HID_KEYPAD_COMMA = 0x85, + HID_KEYPAD_EQUAL_SIGN = 0x86, + HID_KEYBOARD_INTERNATIONAL1 = 0x87, + HID_KEYBOARD_INTERNATIONAL2 = 0x88, + HID_KEYBOARD_INTERNATIONAL3 = 0x89, + HID_KEYBOARD_INTERNATIONAL4 = 0x8A, + HID_KEYBOARD_INTERNATIONAL5 = 0x8B, + HID_KEYBOARD_INTERNATIONAL6 = 0x8C, + HID_KEYBOARD_INTERNATIONAL7 = 0x8D, + HID_KEYBOARD_INTERNATIONAL8 = 0x8E, + HID_KEYBOARD_INTERNATIONAL9 = 0x8F, + HID_KEYBOARD_LANG1 = 0x90, + HID_KEYBOARD_LANG2 = 0x91, + HID_KEYBOARD_LANG3 = 0x92, + HID_KEYBOARD_LANG4 = 0x93, + HID_KEYBOARD_LANG5 = 0x94, + HID_KEYBOARD_LANG6 = 0x95, + HID_KEYBOARD_LANG7 = 0x96, + HID_KEYBOARD_LANG8 = 0x97, + HID_KEYBOARD_LANG9 = 0x98, + HID_KEYBOARD_ALTERNATE_ERASE = 0x99, + HID_KEYBOARD_SYSREQ_SLASH_ATTENTION = 0x9A, + HID_KEYBOARD_CANCEL = 0x9B, + HID_KEYBOARD_CLEAR = 0x9C, + HID_KEYBOARD_PRIOR = 0x9D, + HID_KEYBOARD_RETURN = 0x9E, + HID_KEYBOARD_SEPARATOR = 0x9F, + HID_KEYBOARD_OUT = 0xA0, + HID_KEYBOARD_OPER = 0xA1, + HID_KEYBOARD_CLEAR_SLASH_AGAIN = 0xA2, + HID_KEYBOARD_CRSEL_SLASH_PROPS = 0xA3, + HID_KEYBOARD_EXSEL = 0xA4, + // Reserved 0xA5-AF + HID_KEYPAD_00 = 0xB0, + HID_KEYPAD_000 = 0xB1, + HID_THOUSANDS_SEPARATOR = 0xB2, + HID_DECIMAL_SEPARATOR = 0xB3, + HID_CURRENCY_UNIT = 0xB4, + HID_CURRENCY_SUBUNIT = 0xB5, + HID_KEYPAD_LEFT_PAREN = 0xB6, + HID_KEYPAD_RIGHT_PAREN = 0xB7, + HID_KEYPAD_LEFT_CURLY_BRACE = 0xB8, + HID_KEYPAD_RIGHT_CURLY_BRACE = 0xB9, + HID_KEYPAD_TAB = 0xBA, + HID_KEYPAD_BACKSPACE = 0xBB, + HID_KEYPAD_A = 0xBC, + HID_KEYPAD_B = 0xBD, + HID_KEYPAD_C = 0xBE, + HID_KEYPAD_D = 0xBF, + HID_KEYPAD_E = 0xC0, + HID_KEYPAD_F = 0xC1, + HID_KEYPAD_XOR = 0xC2, + HID_KEYPAD_CARAT = 0xC3, + HID_KEYPAD_PERCENT = 0xC4, + HID_KEYPAD_LESS_THAN = 0xC5, + HID_KEYPAD_GREATER_THAN = 0xC6, + HID_KEYPAD_AMPERSAND = 0xC7, + HID_KEYPAD_DOUBLEAMPERSAND = 0xC8, + HID_KEYPAD_PIPE = 0xC9, + HID_KEYPAD_DOUBLEPIPE = 0xCA, + HID_KEYPAD_COLON = 0xCB, + HID_KEYPAD_POUND_SIGN = 0xCC, + HID_KEYPAD_SPACE = 0xCD, + HID_KEYPAD_AT_SIGN = 0xCE, + HID_KEYPAD_EXCLAMATION_POINT = 0xCF, + HID_KEYPAD_MEMORY_STORE = 0xD0, + HID_KEYPAD_MEMORY_RECALL = 0xD1, + HID_KEYPAD_MEMORY_CLEAR = 0xD2, + HID_KEYPAD_MEMORY_ADD = 0xD3, + HID_KEYPAD_MEMORY_SUBTRACT = 0xD4, + HID_KEYPAD_MEMORY_MULTIPLY = 0xD5, + HID_KEYPAD_MEMORY_DIVIDE = 0xD6, + HID_KEYPAD_PLUS_SLASH_MINUS = 0xD7, + HID_KEYPAD_CLEAR = 0xD8, + HID_KEYPAD_CLEAR_ENTRY = 0xD9, + HID_KEYPAD_BINARY = 0xDA, + HID_KEYPAD_OCTAL = 0xDB, + HID_KEYPAD_DECIMAL = 0xDC, + HID_KEYPAD_HEXADECIMAL = 0xDD, + + // 0xDE-0xDF - RESERVED + HID_KEYBOARD_LEFT_CONTROL = 0xE0, + HID_KEYBOARD_LEFT_SHIFT = 0xE1, + HID_KEYBOARD_LEFT_ALT = 0xE2, + HID_KEYBOARD_LEFT_GUI = 0xE3, + HID_KEYBOARD_RIGHT_CONTROL = 0xE4, + HID_KEYBOARD_RIGHT_SHIFT = 0xE5, + HID_KEYBOARD_RIGHT_ALT = 0xE6, + HID_KEYBOARD_RIGHT_GUI = 0xE7, +}; + +static const uint16_t _asciimap[] = { + KEY_RESERVED, // NUL + KEY_RESERVED, // SOH + KEY_RESERVED, // STX + KEY_RESERVED, // ETX + KEY_RESERVED, // EOT + KEY_RESERVED, // ENQ + KEY_RESERVED, // ACK + KEY_RESERVED, // BEL + KEY_BACKSPACE, // BS Backspace + KEY_TAB, // TAB Tab + KEY_ENTER, // LF Enter + KEY_RESERVED, // VT + KEY_RESERVED, // FF + KEY_RESERVED, // CR + KEY_RESERVED, // SO + KEY_RESERVED, // SI + KEY_RESERVED, // DEL + KEY_RESERVED, // DC1 + KEY_RESERVED, // DC2 + KEY_RESERVED, // DC3 + KEY_RESERVED, // DC4 + KEY_RESERVED, // NAK + KEY_RESERVED, // SYN + KEY_RESERVED, // ETB + KEY_RESERVED, // CAN + KEY_RESERVED, // EM + KEY_RESERVED, // SUB + KEY_RESERVED, // ESC + KEY_RESERVED, // FS + KEY_RESERVED, // GS + KEY_RESERVED, // RS + KEY_RESERVED, // US + + KEY_SPACE, // ' ' Space + KEY_1 | MOD_LEFT_SHIFT, // ! + KEY_QUOTE | MOD_LEFT_SHIFT, // " + KEY_3 | MOD_LEFT_SHIFT, // # + KEY_4 | MOD_LEFT_SHIFT, // $ + KEY_5 | MOD_LEFT_SHIFT, // % + KEY_7 | MOD_LEFT_SHIFT, // & + KEY_QUOTE, // ' + KEY_9 | MOD_LEFT_SHIFT, // ( + KEY_0 | MOD_LEFT_SHIFT, // ) + KEY_8 | MOD_LEFT_SHIFT, // * + KEY_EQUAL | MOD_LEFT_SHIFT, // + + KEY_COMMA, // , + KEY_MINUS, // - + KEY_PERIOD, // . + KEY_SLASH, // / + KEY_0, // 0 + KEY_1, // 1 + KEY_2, // 2 + KEY_3, // 3 + KEY_4, // 4 + KEY_5, // 5 + KEY_6, // 6 + KEY_7, // 7 + KEY_8, // 8 + KEY_9, // 9 + KEY_SEMICOLON | MOD_LEFT_SHIFT, // : + KEY_SEMICOLON, // ; + KEY_COMMA | MOD_LEFT_SHIFT, // < + KEY_EQUAL, // = + KEY_PERIOD | MOD_LEFT_SHIFT, // > + KEY_SLASH | MOD_LEFT_SHIFT, // ? + KEY_2 | MOD_LEFT_SHIFT, // @ + KEY_A | MOD_LEFT_SHIFT, // A + KEY_B | MOD_LEFT_SHIFT, // B + KEY_C | MOD_LEFT_SHIFT, // C + KEY_D | MOD_LEFT_SHIFT, // D + KEY_E | MOD_LEFT_SHIFT, // E + KEY_F | MOD_LEFT_SHIFT, // F + KEY_G | MOD_LEFT_SHIFT, // G + KEY_H | MOD_LEFT_SHIFT, // H + KEY_I | MOD_LEFT_SHIFT, // I + KEY_J | MOD_LEFT_SHIFT, // J + KEY_K | MOD_LEFT_SHIFT, // K + KEY_L | MOD_LEFT_SHIFT, // L + KEY_M | MOD_LEFT_SHIFT, // M + KEY_N | MOD_LEFT_SHIFT, // N + KEY_O | MOD_LEFT_SHIFT, // O + KEY_P | MOD_LEFT_SHIFT, // P + KEY_Q | MOD_LEFT_SHIFT, // Q + KEY_R | MOD_LEFT_SHIFT, // R + KEY_S | MOD_LEFT_SHIFT, // S + KEY_T | MOD_LEFT_SHIFT, // T + KEY_U | MOD_LEFT_SHIFT, // U + KEY_V | MOD_LEFT_SHIFT, // V + KEY_W | MOD_LEFT_SHIFT, // W + KEY_X | MOD_LEFT_SHIFT, // X + KEY_Y | MOD_LEFT_SHIFT, // Y + KEY_Z | MOD_LEFT_SHIFT, // Z + KEY_LEFT_BRACE, // [ + KEY_BACKSLASH, // bslash + KEY_RIGHT_BRACE, // ] + KEY_6 | MOD_LEFT_SHIFT, // ^ + KEY_MINUS | MOD_LEFT_SHIFT, // _ + KEY_TILDE, // ` + KEY_A, // a + KEY_B, // b + KEY_C, // c + KEY_D, // d + KEY_E, // e + KEY_F, // f + KEY_G, // g + KEY_H, // h + KEY_I, // i + KEY_J, // j + KEY_K, // k + KEY_L, // l + KEY_M, // m + KEY_N, // n + KEY_O, // o + KEY_P, // p + KEY_Q, // q + KEY_R, // r + KEY_S, // s + KEY_T, // t + KEY_U, // u + KEY_V, // v + KEY_W, // w + KEY_X, // x + KEY_Y, // y + KEY_Z, // z + KEY_LEFT_BRACE | MOD_LEFT_SHIFT, // { + KEY_BACKSLASH | MOD_LEFT_SHIFT, // | + KEY_RIGHT_BRACE | MOD_LEFT_SHIFT, // } + KEY_TILDE | MOD_LEFT_SHIFT, // ~ + KEY_RESERVED, // DEL + // 7-bit ASCII codes end here + + // The following characters belong to ISO-8859-15 + // ! The first 16 values here are used for the numpad + KEYPAD_0, + KEYPAD_1, + KEYPAD_2, + KEYPAD_3, + KEYPAD_4, + KEYPAD_5, + KEYPAD_6, + KEYPAD_7, + KEYPAD_8, + KEYPAD_9, + KEYPAD_ADD, + KEYPAD_SUBTRACT, + KEYPAD_MULTIPLY, + KEYPAD_DIVIDE, + KEYPAD_ENTER, + KEYPAD_DOT, + // KEY_RESERVED, // 128 - Unused + // KEY_RESERVED, // 129 - Unused + // KEY_RESERVED, // 130 - Unused + // KEY_RESERVED, // 131 - Unused + // KEY_RESERVED, // 132 - Unused + // KEY_RESERVED, // 133 - Unused + // KEY_RESERVED, // 134 - Unused + // KEY_RESERVED, // 135 - Unused + // KEY_RESERVED, // 136 - Unused + // KEY_RESERVED, // 137 - Unused + // KEY_RESERVED, // 138 - Unused + // KEY_RESERVED, // 139 - Unused + // KEY_RESERVED, // 140 - Unused + // KEY_RESERVED, // 141 - Unused + // KEY_RESERVED, // 142 - Unused + // KEY_RESERVED, // 143 - Unused + KEY_RESERVED, // 144 - Unused + KEY_RESERVED, // 145 - Unused + KEY_RESERVED, // 146 - Unused + KEY_RESERVED, // 147 - Unused + KEY_RESERVED, // 148 - Unused + KEY_RESERVED, // 149 - Unused + KEY_RESERVED, // 150 - Unused + KEY_RESERVED, // 151 - Unused + KEY_RESERVED, // 152 - Unused + KEY_RESERVED, // 153 - Unused + KEY_RESERVED, // 154 - Unused + KEY_RESERVED, // 155 - Unused + KEY_RESERVED, // 156 - Unused + KEY_RESERVED, // 157 - Unused + KEY_RESERVED, // 158 - Unused + KEY_RESERVED, // 159 - Unused + KEY_RESERVED, // 160 - Non-breaking Space + KEY_RESERVED, // 161 - Inverted Exclamation Mark + KEY_RESERVED, // 162 - Cent + KEY_RESERVED, // 163 - British Pound Sign + KEY_RESERVED, // 164 - Euro Sign + KEY_RESERVED, // 165 - Yen + KEY_RESERVED, // 166 - Capital 's' Inverted Circumflex + KEY_RESERVED, // 167 - Section Sign + KEY_RESERVED, // 168 - 's' Inverted Circumflex + KEY_RESERVED, // 169 - Copyright Sign + KEY_RESERVED, // 170 - Superscript 'a' + KEY_RESERVED, // 171 - Open Guillemet + KEY_RESERVED, // 172 - Logic Negation + KEY_RESERVED, // 173 - Soft Hypen + KEY_RESERVED, // 174 - Registered Trademark + KEY_RESERVED, // 175 - Macron + KEY_RESERVED, // 176 - Degree Symbol + KEY_RESERVED, // 177 - Plus-Minus + KEY_RESERVED, // 178 - Superscript '2' + KEY_RESERVED, // 179 - Superscript '3' + KEY_RESERVED, // 180 - Capital 'z' Inverted Circumflex + KEY_RESERVED, // 181 - Micro Symbol + KEY_RESERVED, // 182 - Paragraph Mark + KEY_RESERVED, // 183 - Interpunct + KEY_RESERVED, // 184 - 'z' Inverted Circumflex + KEY_RESERVED, // 185 - Superscript '1' + KEY_RESERVED, // 186 - Ordinal Indicator + KEY_RESERVED, // 187 - Closed Guillemet + KEY_RESERVED, // 188 - Capital 'oe' + KEY_RESERVED, // 189 - 'oe' + KEY_RESERVED, // 190 - Capital 'y' Umlaut + KEY_RESERVED, // 191 - Inverted Question Mark + KEY_RESERVED, // 192 - Capital 'a' Grave + KEY_RESERVED, // 193 - Capital 'a' Acute + KEY_RESERVED, // 194 - Capital 'a' Circumflex + KEY_RESERVED, // 195 - Capital 'a' Tilde + KEY_RESERVED, // 196 - Capital 'a' Umlaut + KEY_RESERVED, // 197 - Capital 'a' Circle + KEY_RESERVED, // 198 - Capital 'ae' + KEY_RESERVED, // 199 - Capital 'c' Cedilla + KEY_RESERVED, // 200 - Capital 'e' Grave + KEY_RESERVED, // 201 - Capital 'e' Acute + KEY_RESERVED, // 202 - Capital 'e' Circumflex + KEY_RESERVED, // 203 - Capital 'e' Umlaut + KEY_RESERVED, // 204 - Capital 'i' Grave + KEY_RESERVED, // 205 - Capital 'i' Acute + KEY_RESERVED, // 206 - Capital 'i' Circumflex + KEY_RESERVED, // 207 - Capital 'i' Umlaut + KEY_RESERVED, // 208 - Capital Eth + KEY_RESERVED, // 207 - Capital 'n' Tilde + KEY_RESERVED, // 210 - Capital 'o' Grave + KEY_RESERVED, // 211 - Capital 'o' Acute + KEY_RESERVED, // 212 - Capital 'o' Circumflex + KEY_RESERVED, // 213 - Capital 'o' Tilde + KEY_RESERVED, // 214 - Capital 'o' Umlaut + KEY_RESERVED, // 215 - Multiplication Sign + KEY_RESERVED, // 216 - Capital 'o' Barred + KEY_RESERVED, // 217 - Capital 'u' Grave + KEY_RESERVED, // 218 - Capital 'u' Acute + KEY_RESERVED, // 219 - Capital 'u' Circumflex + KEY_RESERVED, // 220 - Capital 'u' Umlaut + KEY_RESERVED, // 221 - Capital 'y' Acute + KEY_RESERVED, // 222 - Capital Thorn + KEY_RESERVED, // 223 - Eszett + KEY_RESERVED, // 224 - 'a' Grave + KEY_RESERVED, // 225 - 'a' Acute + KEY_RESERVED, // 226 - 'a' Circumflex + KEY_RESERVED, // 227 - 'a' Tilde + KEY_RESERVED, // 228 - 'a' Umlaut + KEY_RESERVED, // 229 - 'a' Circle + KEY_RESERVED, // 230 - 'ae' + KEY_RESERVED, // 231 - 'c' Cedilla + KEY_RESERVED, // 232 - 'e' Grave + KEY_RESERVED, // 233 - 'e' Acute + KEY_RESERVED, // 234 - 'e' Circumflex + KEY_RESERVED, // 235 - 'e' Umlaut + KEY_RESERVED, // 236 - 'i' Grave + KEY_RESERVED, // 237 - 'i' Acute + KEY_RESERVED, // 238 - 'i' Circumflex + KEY_RESERVED, // 239 - 'i' Umlaut + KEY_RESERVED, // 240 - Eth + KEY_RESERVED, // 241 - 'n' Tilde + KEY_RESERVED, // 242 - 'o' Grave + KEY_RESERVED, // 243 - 'o' Acute + KEY_RESERVED, // 244 - 'o' Circumflex + KEY_RESERVED, // 245 - 'o' Tilde + KEY_RESERVED, // 246 - 'o' Umlaut + KEY_RESERVED, // 247 - Multiplication Sign + KEY_RESERVED, // 248 - 'o' Barred + KEY_RESERVED, // 249 - 'u' Grave + KEY_RESERVED, // 250 - 'u' Acute + KEY_RESERVED, // 251 - 'u' Circumflex + KEY_RESERVED, // 252 - 'u' Umlaut + KEY_RESERVED, // 253 - 'y' Acute + KEY_RESERVED, // 254 - Thorn + KEY_RESERVED, // 255 - 'y' Umlaut +}; diff --git a/src/usb_inc/usb.h b/src/usb_inc/usb.h new file mode 100644 index 0000000..71e739b --- /dev/null +++ b/src/usb_inc/usb.h @@ -0,0 +1,316 @@ +#pragma once +#include + +// USB U16 helpers +#define U16(_high, _low) ((uint16_t)(((_high) << 8) | (_low))) +#define U16_HIGH(_u16) ((uint8_t)(((_u16) >> 8) & 0x00ff)) +#define U16_LOW(_u16) ((uint8_t)((_u16)&0x00ff)) +#define U16_TO_U8S_BE(_u16) U16_HIGH(_u16), U16_LOW(_u16) +#define U16_TO_U8S_LE(_u16) U16_LOW(_u16), U16_HIGH(_u16) +#define U16_TO_U8S_BE_A(_u16) \ + { U16_TO_U8S_BE(_u16) } +#define U16_TO_U8S_LE_A(_u16) \ + { U16_TO_U8S_LE(_u16) } + +// USB String helpers +enum { + USB_STRING_LANG = 0, + USB_STRING_VENDOR, + USB_STRING_PRODUCT, + USB_STRING_SERIAL, + USB_STRING_CDC, + USB_STRING_HID_IO4, + USB_STRING_HID_MISC, +}; + +// USB Spec definitions +#define DESC_IAD 0x0B +#define DESC_CS_DEVICE 0x21 +#define DESC_CS_CONFIGURATION 0x22 +#define DESC_CS_STRING 0x23 +#define DESC_CS_INTERFACE 0x24 +#define DESC_CS_ENDPOINT 0x25 + +#define USB_CLASS_UNSPECIFIED 0 +#define USB_CLASS_AUDIO 1 +#define USB_CLASS_CDC 2 +#define USB_CLASS_HID 3 +#define USB_CLASS_RESERVED_4 4 +#define USB_CLASS_PHYSICAL 5 +#define USB_CLASS_IMAGE 6 +#define USB_CLASS_PRINTER 7 +#define USB_CLASS_MSC 8 +#define USB_CLASS_HUB 9 +#define USB_CLASS_CDC_DATA 10 +#define USB_CLASS_SMART_CARD 11 +#define USB_CLASS_RESERVED_12 12 +#define USB_CLASS_CONTENT_SECURITY 13 +#define USB_CLASS_VIDEO 14 +#define USB_CLASS_PERSONAL_HEALTHCARE 15 +#define USB_CLASS_AUDIO_VIDEO 16 + +#define USB_CLASS_DIAGNOSTIC 0xDC +#define USB_CLASS_WIRELESS_CONTROLLER 0xE0 +#define USB_CLASS_MISC 0xEF +#define USB_CLASS_APPLICATION_SPECIFIC 0xFE +#define USB_CLASS_VENDOR_SPECIFIC 0xFF + +#define CDC_COMM_SUBCLASS_DIRECT_LINE_CONTROL_MODEL 0x01 +#define CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL 0x02 +#define CDC_COMM_SUBCLASS_TELEPHONE_CONTROL_MODEL 0x03 +#define CDC_COMM_SUBCLASS_MULTICHANNEL_CONTROL_MODEL 0x04 +#define CDC_COMM_SUBCLASS_CAPI_CONTROL_MODEL 0x05 +#define CDC_COMM_SUBCLASS_ETHERNET_CONTROL_MODEL 0x06 +#define CDC_COMM_SUBCLASS_ATM_NETWORKING_CONTROL_MODEL 0x07 +#define CDC_COMM_SUBCLASS_WIRELESS_HANDSET_CONTROL_MODEL0x08 +#define CDC_COMM_SUBCLASS_DEVICE_MANAGEMENT 0x09 +#define CDC_COMM_SUBCLASS_MOBILE_DIRECT_LINE_MODEL 0x0A +#define CDC_COMM_SUBCLASS_OBEX 0x0B +#define CDC_COMM_SUBCLASS_ETHERNET_EMULATION_MODEL 0x0C +#define CDC_COMM_SUBCLASS_NETWORK_CONTROL_MODEL 0x0D + +#define CDC_COMM_PROTOCOL_NONE 0x00 +#define CDC_COMM_PROTOCOL_ATCOMMAND 0x01 +#define CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101 0x02 +#define CDC_COMM_PROTOCOL_ATCOMMAND_PCCA_101_AND_ANNEXO0x03 +#define CDC_COMM_PROTOCOL_ATCOMMAND_GSM_707 0x04 +#define CDC_COMM_PROTOCOL_ATCOMMAND_3GPP_27007 0x05 +#define CDC_COMM_PROTOCOL_ATCOMMAND_CDMA 0x06 +#define CDC_COMM_PROTOCOL_ETHERNET_EMULATION_MODEL 0x07 + +#define CDC_FUNC_DESC_HEADER 0x00 +#define CDC_FUNC_DESC_CALL_MANAGEMENT 0x01 +#define CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT 0x02 +#define CDC_FUNC_DESC_DIRECT_LINE_MANAGEMENT 0x03 +#define CDC_FUNC_DESC_TELEPHONE_RINGER 0x04 +#define CDC_FUNC_DESC_TELEPHONE_CALL_AND_LINE_STATE_REPORTING_CAPACITY0x05 +#define CDC_FUNC_DESC_UNION 0x06 +#define CDC_FUNC_DESC_COUNTRY_SELECTION 0x07 +#define CDC_FUNC_DESC_TELEPHONE_OPERATIONAL_MODES 0x08 +#define CDC_FUNC_DESC_USB_TERMINAL 0x09 +#define CDC_FUNC_DESC_NETWORK_CHANNEL_TERMINAL 0x0A +#define CDC_FUNC_DESC_PROTOCOL_UNIT 0x0B +#define CDC_FUNC_DESC_EXTENSION_UNIT 0x0C +#define CDC_FUNC_DESC_MULTICHANEL_MANAGEMENT 0x0D +#define CDC_FUNC_DESC_CAPI_CONTROL_MANAGEMENT 0x0E +#define CDC_FUNC_DESC_ETHERNET_NETWORKING 0x0F +#define CDC_FUNC_DESC_ATM_NETWORKING 0x10 +#define CDC_FUNC_DESC_WIRELESS_HANDSET_CONTROL_MODEL 0x11 +#define CDC_FUNC_DESC_MOBILE_DIRECT_LINE_MODEL 0x12 +#define CDC_FUNC_DESC_MOBILE_DIRECT_LINE_MODEL_DETAIL 0x13 +#define CDC_FUNC_DESC_DEVICE_MANAGEMENT_MODEL 0x14 +#define CDC_FUNC_DESC_OBEX 0x15 +#define CDC_FUNC_DESC_COMMAND_SET 0x16 +#define CDC_FUNC_DESC_COMMAND_SET_DETAIL 0x17 +#define CDC_FUNC_DESC_TELEPHONE_CONTROL_MODEL 0x18 +#define CDC_FUNC_DESC_OBEX_SERVICE_IDENTIFIER 0x19 +#define CDC_FUNC_DESC_NCM 0x1A + +#define SET_LINE_CODING 0x20 +#define GET_LINE_CODING 0x21 +#define SET_CONTROL_LINE_STATE 0x22 + +typedef struct __attribute__((packed)) { + uint8_t bmRequestType; + uint8_t bRequest; + union { + uint8_t wBytes[6]; + struct __attribute__((packed)) { + uint16_t wValue; + uint16_t wIndex; + uint16_t wLength; + }; + + // Core setup packet types + struct __attribute__((packed)) { + uint8_t bIndex; + uint8_t bType; + uint16_t wLanguageId; + uint16_t wDescriptorLength; + } getDescriptor; + struct __attribute__((packed)) { + uint16_t wFeature; + uint16_t wEp; + } clearFeature; + struct __attribute__((packed)) { + uint16_t wAddress; + } setAddress; + struct __attribute__((packed)) { + uint16_t wConfiguration; + } setConfiguration; + struct __attribute__((packed)) { + uint16_t wFeature; + uint16_t wEp; + } setFeature; + struct __attribute__((packed)) { + uint16_t wAlternate; + uint16_t wInterface; + } setInterface; + struct __attribute__((packed)) { + uint16_t wValue; + uint16_t wInterface; + } getStatus; + + // USB HID + struct __attribute__((packed)) { + uint8_t bIndex; + uint8_t bType; + uint16_t wInterfaceNum; + uint16_t wDescriptorLength; + } hidGetDescriptor; + struct __attribute__((packed)) { + uint8_t bReportId; + uint8_t bReportType; + uint16_t wInterface; + uint16_t wLength; + } hidGetReport; + struct __attribute__((packed)) { + uint8_t bReportId; + uint8_t bReportType; + uint16_t wInterface; + uint16_t wLength; + } hidSetReport; + struct __attribute__((packed)) { + uint8_t bReportId; + uint8_t bPad; + uint16_t wInterface; + uint16_t wLength; + } hidGetIdle; + struct __attribute__((packed)) { + uint8_t bReportId; + uint8_t bDuration; + uint16_t wInterface; + uint16_t wLength; + } hidSetIdle; + struct __attribute__((packed)) { + uint16_t wPad; + uint16_t wInterface; + uint16_t wLength; + } hidGetProtocol; + struct __attribute__((packed)) { + uint16_t wProtocol; + uint16_t wInterface; + uint16_t wLength; + } hidSetProtocol; + + // USB CDC + struct __attribute__((packed)) { + uint16_t wValue; + uint16_t wInterface; + uint16_t wLength; + } getLineCoding; + struct __attribute__((packed)) { + uint16_t wValue; + uint16_t wInterface; + uint16_t wLength; + } setLineCoding; + }; +} usb_setup_t; + +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdUSB; + uint8_t bDeviceClass; + uint8_t bDeviceSubClass; + uint8_t bDeviceProtocol; + uint8_t bMaxPacketSize0; + uint16_t idVendor; + uint16_t idProduct; + uint16_t bcdDevice; + uint8_t iManufacture; + uint8_t iProduct; + uint8_t iSerialNumber; + uint8_t bNumConfigurations; +} usb_device_descr_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bFirstInterface; + uint8_t bInterfaceCount; + uint8_t bFunctionClass; + uint8_t bFunctionSubClass; + uint8_t bFunctionProtocol; + uint8_t iFunction; +} usb_desc_iad_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t wTotalLength; + uint8_t bNumInterfaces; + uint8_t bConfigurationValue; + uint8_t iConfiguration; + uint8_t bmAttributes; + uint8_t MaxPower; +} usb_desc_config_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bInterfaceNumber; + uint8_t bAlternateSetting; + uint8_t bNumEndpoints; + uint8_t bInterfaceClass; + uint8_t bInterfaceSubClass; + uint8_t bInterfaceProtocol; + uint8_t iInterface; +} usb_desc_interface_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint16_t bcdHID; + uint8_t bCountryCode; + uint8_t bNumDescriptors; + uint8_t bReportDescriptorType; + uint16_t wDescriptorLength; +} usb_desc_hid_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bEndpointAddress; + uint8_t bmAttributes; + uint16_t wMaxPacketSize; + uint8_t bInterval; +} usb_desc_endpoint_t; + +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint16_t bcdCDC; +} usb_desc_cdc_header_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bControlInterface; + uint8_t bSubordinateInterface0; +} usb_desc_cdc_union_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bmCapabilities; + uint8_t bDataInterface; +} usb_desc_cdc_call_t; +typedef struct __attribute__((packed)) { + uint8_t bLength; + uint8_t bDescriptorType; + uint8_t bDescriptorSubtype; + uint8_t bmCapabilities; +} usb_desc_cdc_acm_t; + +// HID definitions +#define GET_REPORT 0x01 +#define GET_IDLE 0x02 +#define GET_PROTOCOL 0x03 +#define SET_REPORT 0x09 +#define SET_IDLE 0x0A +#define SET_PROTOCOL 0x0B + +#define HID_NONE 0x00 +#define HID_KEYBOARD 0x01 +#define HID_MOUSE 0x02 + +#define HID_RPT_TYPE_INPUT 0x01 +#define HID_RPT_TYPE_OUTPUT 0x02 +#define HID_RPT_TYPE_FEATURE 0x03 diff --git a/src/usbd_driver.c b/src/usbd_driver.c new file mode 100644 index 0000000..453c47e --- /dev/null +++ b/src/usbd_driver.c @@ -0,0 +1,551 @@ +#include "tasoller.h" + +usb_setup_t g_usbd_SetupPacket; +// uint8_t g_usbd_SetupPacket[8] = { 0 }; +// static const usb_setup_t* p_usbd_SetupPacket = (usb_setup_t*)&g_usbd_SetupPacket; +volatile uint8_t g_usbd_RemoteWakeupEn = 0; + +volatile uint8_t *g_usbd_CtrlInPointer = 0; +volatile uint32_t g_usbd_CtrlInSize = 0; +static volatile uint8_t *g_usbd_CtrlOutPointer = 0; +static volatile uint32_t g_usbd_CtrlOutSize = 0; +static volatile uint32_t g_usbd_CtrlOutSizeLimit = 0; +static volatile uint32_t g_usbd_UsbAddr = 0; +static volatile uint32_t g_usbd_UsbConfig = 0; +static volatile uint32_t g_usbd_CtrlMaxPktSize = 8; +static volatile uint32_t g_usbd_UsbAltInterface = 0; +static volatile uint32_t g_usbd_CtrlOutToggle = 0; +static volatile uint8_t g_usbd_CtrlInZeroFlag = 0; +static void (*g_usbd_CtrlOutCallback)(volatile uint8_t *, uint32_t); + +uint32_t g_u32EpStallLock = 0; + +static inline void Tas_USBD_GetDescriptor(void); +static inline void Tas_USBD_StandardRequest(void); + +static uint8_t su8VendorCount = 0; + +void Tas_USBD_Open(void) { + g_usbd_CtrlMaxPktSize = gpDeviceDescriptor->bMaxPacketSize0; + USBD->ATTR = 0x650; // Disable D+ and USB controller + CLK_SysTickLongDelay(3000 ms); + USBD->ATTR = 0x7D0; + USBD_SET_SE0(); +} +void Tas_USBD_Init(void) { + // Buffer range for setup packet -> [0~7] + USBD->STBUFSEG = SETUP_BUF_BASE; + + // USB control IN/OUT on EP_CTRL_IN/EP_CTRL_OUT (addr 0) + USBD_CONFIG_EP(EP_CTRL_IN, USBD_CFG_CSTALL | USBD_CFG_EPMODE_IN | 0); + USBD_SET_EP_BUF_ADDR(EP_CTRL_IN, EP0_BUF_BASE); + USBD_CONFIG_EP(EP_CTRL_OUT, USBD_CFG_CSTALL | USBD_CFG_EPMODE_OUT | 0); + USBD_SET_EP_BUF_ADDR(EP_CTRL_OUT, EP1_BUF_BASE); + + // CDC data IN/OUT on EP2/EP3 + USBD_CONFIG_EP(EP_CDC_IN, USBD_CFG_EPMODE_IN | 1); + USBD_SET_EP_BUF_ADDR(EP_CDC_IN, EP2_BUF_BASE); + USBD_CONFIG_EP(EP_CDC_OUT, USBD_CFG_EPMODE_OUT | 2); + USBD_SET_EP_BUF_ADDR(EP_CDC_OUT, EP3_BUF_BASE); + USBD_SET_PAYLOAD_LEN(EP_CDC_OUT, USBD_CDC_OUT_MAX_SIZE); + + // CDC command IN on EP4 + USBD_CONFIG_EP(EP_CDC_CMD, USBD_CFG_EPMODE_IN | 3); + USBD_SET_EP_BUF_ADDR(EP_CDC_CMD, EP4_BUF_BASE); + + // IO4 HID IN on EP5 + USBD_CONFIG_EP(EP_HID_IO4_IN, USBD_CFG_EPMODE_IN | 4); + USBD_SET_EP_BUF_ADDR(EP_HID_IO4_IN, EP5_BUF_BASE); + + // Misc HID IN/OUT on EP6/EP6 + USBD_CONFIG_EP(EP_HID_MISC_IN, USBD_CFG_EPMODE_IN | 5); + USBD_SET_EP_BUF_ADDR(EP_HID_MISC_IN, EP6_BUF_BASE); + USBD_CONFIG_EP(EP_HID_MISC_OUT, USBD_CFG_EPMODE_OUT | 6); + USBD_SET_EP_BUF_ADDR(EP_HID_MISC_OUT, EP7_BUF_BASE); + USBD_SET_PAYLOAD_LEN(EP_HID_MISC_OUT, USBD_HID_BUF_LEN); +} +void Tas_USBD_Start(void) { + // 100ms delay required as part of spec + CLK_SysTickDelay(100 ms); + + USBD_CLR_SE0(); // Disable software-disconnect function + + // Clear USB-related interrupts before enable interrupt + USBD_CLR_INT_FLAG(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP); + // Enable USB-related interrupts. + USBD_ENABLE_INT(USBD_INT_BUS | USBD_INT_USB | USBD_INT_FLDET | USBD_INT_WAKEUP); +} + +void Tas_USBD_GetSetupPacket(usb_setup_t *buf) { + memcpy(buf, &g_usbd_SetupPacket, sizeof g_usbd_SetupPacket); +} + +// as unused for now +static inline void Tas_USBD_VendorRequest(void) { return; } + +void Tas_USBD_ProcessSetupPacket(void) { + g_usbd_CtrlOutToggle = 0; + // Get SETUP packet from USB buffer + USBD_MemCopy((uint8_t *)&g_usbd_SetupPacket, (uint8_t *)USBD_BUF_BASE, 8); + + // Check the request type + switch (g_usbd_SetupPacket.bmRequestType & 0x60) { + case REQ_STANDARD: + Tas_USBD_StandardRequest(); + break; + case REQ_CLASS: + Tas_USBD_ClassRequest(); + break; + case REQ_VENDOR: + Tas_USBD_VendorRequest(); + break; + + default: + // Setup error, stall the device + USBD_SET_EP_STALL(EP_CTRL_IN); + USBD_SET_EP_STALL(EP_CTRL_OUT); + break; + } +} + +static inline void Tas_USBD_GetDescriptor(void) { + uint16_t u16Len; + + g_usbd_CtrlInZeroFlag = 0; + u16Len = g_usbd_SetupPacket.wLength; + + switch (g_usbd_SetupPacket.getDescriptor.bType) { + // Get Device Descriptor + case DESC_DEVICE: + su8VendorCount = 0; + + u16Len = Minimum(u16Len, LEN_DEVICE); + Tas_USBD_PrepareCtrlIn((uint8_t *)gpDeviceDescriptor, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + // Get Configuration Descriptor + case DESC_CONFIG: { + uint32_t u32TotalLen = gpConfigDescriptor->wTotalLength; + if (u16Len > u32TotalLen) { + u16Len = u32TotalLen; + if ((u16Len % g_usbd_CtrlMaxPktSize) == 0) g_usbd_CtrlInZeroFlag = 1; + } + Tas_USBD_PrepareCtrlIn((uint8_t *)gpConfigDescriptor, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + } + // Get HID Descriptor + case DESC_HID: + /* CV3.0 HID Class Descriptor Test, + Need to indicate index of the HID Descriptor within gConfigDescriptor, specifically + HID Composite device. */ + uint32_t u32ConfigDescOffset = 0; // u32ConfigDescOffset is configuration descriptor + // offset (HID descriptor start index) + u16Len = Minimum(u16Len, LEN_HID); + + if (g_usbd_SetupPacket.hidGetDescriptor.wInterfaceNum == USBD_ITF_HID_IO4) + u32ConfigDescOffset = gu32HidDescIO4Offset; + else if (g_usbd_SetupPacket.hidGetDescriptor.wInterfaceNum == USBD_ITF_HID_MISC) + u32ConfigDescOffset = gu32HidDescMiscOffset; + Tas_USBD_PrepareCtrlIn(((uint8_t *)gpConfigDescriptor) + u32ConfigDescOffset, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + // Get Report Descriptor + case DESC_HID_RPT: + if (g_usbd_SetupPacket.hidGetDescriptor.wInterfaceNum == USBD_ITF_HID_IO4) { + if (u16Len > gu32UsbHidIO4ReportLen) { + u16Len = gu32UsbHidIO4ReportLen; + if ((u16Len % g_usbd_CtrlMaxPktSize) == 0) g_usbd_CtrlInZeroFlag = 1; + } + Tas_USBD_PrepareCtrlIn((uint8_t *)gpu8UsbHidIO4Report, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + } else if (g_usbd_SetupPacket.hidGetDescriptor.wInterfaceNum == USBD_ITF_HID_MISC) { + if (u16Len > gu32UsbHidMiscReportLen) { + u16Len = gu32UsbHidMiscReportLen; + if ((u16Len % g_usbd_CtrlMaxPktSize) == 0) g_usbd_CtrlInZeroFlag = 1; + } + Tas_USBD_PrepareCtrlIn((uint8_t *)gpu8UsbHidMiscReport, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + } else { + Tas_USBD_PrepareCtrlIn(NULL, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + } + + break; + + // Get String Descriptor + case DESC_STRING: { + uint8_t iString = g_usbd_SetupPacket.getDescriptor.bIndex; + static uint8_t u8Str[256]; // IO4 needs at least 2+(2*96)=194 + + memset(u8Str, 0, sizeof u8Str); + u8Str[1] = DESC_STRING; + + switch (iString) { + case USB_STRING_LANG: + u8Str[0] = 2 + (2 * 2); + u8Str[2] = 0x09; + u8Str[3] = 0x04; + break; + + case USB_STRING_VENDOR: { + const char *szVendor; + if (su8VendorCount < 2) { + szVendor = gszVendorInitial; + su8VendorCount++; + } else { + szVendor = gszVendor; + } + + uint8_t u8Len = strlen(szVendor); + u8Str[0] = 2 + u8Len * 2; + for (uint8_t i = 0; i < u8Len; i++) { + u8Str[2 + i * 2] = szVendor[i]; + u8Str[2 + i * 2 + 1] = 0; + } + break; + } + case USB_STRING_PRODUCT: { + uint8_t u8Len = strlen(gszProduct); + u8Str[0] = 2 + u8Len * 2; + for (uint8_t i = 0; i < u8Len; i++) { + u8Str[2 + i * 2] = gszProduct[i]; + u8Str[2 + i * 2 + 1] = 0; + } + break; + } + case USB_STRING_SERIAL: { + // TODO: I think it might be a u16 then two u8s? + // Need to check the TRM + uint32_t u32serial; + + FMC_Open(); + u8Str[0] = 2 + (2 * 24); + + u32serial = FMC_ReadUID(0); + for (int i = 0; i < 8; i++) + u8Str[2 + i * 2] = HEX_NIBBLE((u32serial >> (i * 4))); + u32serial = FMC_ReadUID(1); + for (int i = 0; i < 8; i++) + u8Str[2 + (8 * 2) + i * 2] = HEX_NIBBLE((u32serial >> (i * 4))); + u32serial = FMC_ReadUID(2); + for (int i = 0; i < 8; i++) + u8Str[2 + (16 * 2) + i * 2] = HEX_NIBBLE((u32serial >> (i * 4))); + + FMC_Close(); + break; + } + case USB_STRING_CDC: { + const char *szCdc = "TASOLLER Slider Serial (COM1)"; + uint8_t u8Len = strlen(szCdc); + u8Str[0] = 2 + u8Len * 2; + for (uint8_t i = 0; i < u8Len; i++) { + u8Str[2 + i * 2] = szCdc[i]; + u8Str[2 + i * 2 + 1] = 0; + } + break; + } + case USB_STRING_HID_IO4: { + const char *szCdc = IO4_PRODUCT; + uint8_t u8Len = strlen(szCdc); + u8Str[0] = 2 + u8Len * 2; + for (uint8_t i = 0; i < u8Len; i++) { + u8Str[2 + i * 2] = szCdc[i]; + u8Str[2 + i * 2 + 1] = 0; + } + break; + } + case USB_STRING_HID_MISC: { + const char *szCdc = "TASOLLER HID"; + uint8_t u8Len = strlen(szCdc); + u8Str[0] = 2 + u8Len * 2; + for (uint8_t i = 0; i < u8Len; i++) { + u8Str[2 + i * 2] = szCdc[i]; + u8Str[2 + i * 2 + 1] = 0; + } + break; + } + + default: + // Not support. Reply STALL. + USBD_SET_EP_STALL(EP_CTRL_IN); + USBD_SET_EP_STALL(EP_CTRL_OUT); + break; + } + + if (u8Str[0] != 0) { + if (u16Len > u8Str[0]) u16Len = u8Str[0]; + if ((u16Len % g_usbd_CtrlMaxPktSize) == 0) g_usbd_CtrlInZeroFlag = 1; + Tas_USBD_PrepareCtrlIn(u8Str, u16Len); + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + } + break; + } + + default: + // Not support. Reply STALL. + USBD_SET_EP_STALL(EP_CTRL_IN); + USBD_SET_EP_STALL(EP_CTRL_OUT); + break; + } +} + +static inline void Tas_USBD_StandardRequest(void) { + // Clear global variables for new request + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + + // Switch on request data transfer direction + if (g_usbd_SetupPacket.bmRequestType & 0x80) { + // Device to host + switch (g_usbd_SetupPacket.bRequest) { + case GET_CONFIGURATION: + // Return current configuration setting + /* Data stage */ + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN)) = g_usbd_UsbConfig; + USBD_SET_DATA1(EP_CTRL_OUT); + USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, 0); + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 1); + /* Status stage */ + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + case GET_DESCRIPTOR: + Tas_USBD_GetDescriptor(); + /* Status stage */ + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + case GET_INTERFACE: + // Return current interface setting + // Data stage + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN)) = g_usbd_UsbAltInterface; + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 1); + // Status stage + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + case GET_STATUS: + // Device + if (g_usbd_SetupPacket.bmRequestType == 0x80) { + uint8_t u8Tmp; + + u8Tmp = 0; + if (gpConfigDescriptor->bmAttributes & 0x40) + u8Tmp |= 1; // Self-Powered/Bus-Powered. + if (gpConfigDescriptor->bmAttributes & 0x20) + u8Tmp |= (g_usbd_RemoteWakeupEn << 1); // Remote wake up + + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN)) = u8Tmp; + } + // Interface + else if (g_usbd_SetupPacket.bmRequestType == 0x81) + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN)) = 0; + // Endpoint + else if (g_usbd_SetupPacket.bmRequestType == 0x82) { + uint8_t ep = g_usbd_SetupPacket.getStatus.wInterface & 0xF; + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN)) = + USBD_GetStall(ep) ? 1 : 0; + } + + M8(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN) + 1) = 0; + // Data stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 2); + // Status stage + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + default: + // Setup error, stall the device + USBD_SET_EP_STALL(EP_CTRL_IN); + USBD_SET_EP_STALL(EP_CTRL_OUT); + break; + } + } else { + // Host to device + switch (g_usbd_SetupPacket.bRequest) { + case CLEAR_FEATURE: + if (g_usbd_SetupPacket.clearFeature.wFeature == FEATURE_ENDPOINT_HALT) { + int32_t epNum, i; + + /* EP number stall is not allow to be clear in MSC class "Error Recovery Test". + a flag: g_u32EpStallLock is added to support it */ + epNum = g_usbd_SetupPacket.clearFeature.wEp & 0xF; + for (i = 0; i < USBD_MAX_EP; i++) { + if (((USBD->EP[i].CFG & 0xF) == epNum) && + ((g_u32EpStallLock & (1 << i)) == 0)) { + USBD->EP[i].CFGP &= ~USBD_CFGP_SSTALL_Msk; + USBD->EP[i].CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + } + } + } else if (g_usbd_SetupPacket.clearFeature.wFeature == FEATURE_DEVICE_REMOTE_WAKEUP) + g_usbd_RemoteWakeupEn = 0; + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_ADDRESS: + g_usbd_UsbAddr = g_usbd_SetupPacket.setAddress.wAddress; + + // DATA IN for end of setup + // Status Stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_CONFIGURATION: + g_usbd_UsbConfig = g_usbd_SetupPacket.setConfiguration.wConfiguration; + + // Callback would be here if we wanted to have one :) + + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_FEATURE: + if (g_usbd_SetupPacket.setFeature.wFeature == FEATURE_ENDPOINT_HALT) { + USBD_SetStall(g_usbd_SetupPacket.setFeature.wEp & 0xF); + } else if (g_usbd_SetupPacket.setFeature.wFeature == FEATURE_DEVICE_REMOTE_WAKEUP) { + g_usbd_RemoteWakeupEn = 1; + } + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_INTERFACE: + g_usbd_UsbAltInterface = g_usbd_SetupPacket.setInterface.wAlternate; + // Callback would be here if wanted to have one :) + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + default: + // Setup error, stall the device + USBD_SET_EP_STALL(EP_CTRL_IN); + USBD_SET_EP_STALL(EP_CTRL_OUT); + break; + } + } +} + +void Tas_USBD_PrepareCtrlIn(void *pu8Buf, uint32_t u32Size) { + if (u32Size > g_usbd_CtrlMaxPktSize) { + // Data size > MXPLD + g_usbd_CtrlInPointer = (uint8_t *)pu8Buf + g_usbd_CtrlMaxPktSize; + g_usbd_CtrlInSize = u32Size - g_usbd_CtrlMaxPktSize; + USBD_SET_DATA1(EP_CTRL_IN); + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN), (uint8_t *)pu8Buf, + g_usbd_CtrlMaxPktSize); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, g_usbd_CtrlMaxPktSize); + } else { + // Data size <= MXPLD + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + USBD_SET_DATA1(EP_CTRL_IN); + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN), (uint8_t *)pu8Buf, + u32Size); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, u32Size); + } +} + +/** + * @brief Repeat Control IN pipe + * @details This function processes the remained data of Control IN transfer. + */ +void Tas_USBD_CtrlIn(void) { + if (g_usbd_CtrlInSize) { + // Process remained data + if (g_usbd_CtrlInSize > g_usbd_CtrlMaxPktSize) { + // Data size > MXPLD + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN), + (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlMaxPktSize); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, g_usbd_CtrlMaxPktSize); + g_usbd_CtrlInPointer += g_usbd_CtrlMaxPktSize; + g_usbd_CtrlInSize -= g_usbd_CtrlMaxPktSize; + } else { + // Data size <= MXPLD + USBD_MemCopy((uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN), + (uint8_t *)g_usbd_CtrlInPointer, g_usbd_CtrlInSize); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, g_usbd_CtrlInSize); + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + } + } else { // No more data for IN token + // In ACK for Set address + if ((g_usbd_SetupPacket.bmRequestType == REQ_STANDARD) && + (g_usbd_SetupPacket.bRequest == SET_ADDRESS)) { + if ((USBD_GET_ADDR() != g_usbd_UsbAddr) && (USBD_GET_ADDR() == 0)) { + USBD_SET_ADDR(g_usbd_UsbAddr); + } + } + + /* For the case of data size is integral times maximum packet size */ + if (g_usbd_CtrlInZeroFlag) { + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + g_usbd_CtrlInZeroFlag = 0; + } + } +} + +void Tas_USBD_PrepareCtrlOut(void *pu8Buf, uint32_t u32Size, + void (*pCallback)(volatile uint8_t *, uint32_t)) { + g_usbd_CtrlOutPointer = pu8Buf; + g_usbd_CtrlOutSize = 0; + g_usbd_CtrlOutSizeLimit = u32Size; + g_usbd_CtrlOutCallback = pCallback; + USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, g_usbd_CtrlMaxPktSize); +} + +/** + * @brief Repeat Control OUT pipe + * @details This function processes the successive Control OUT transfer. + */ +void Tas_USBD_CtrlOut(void) { + uint32_t u32Size; + + if (g_usbd_CtrlOutToggle != (USBD->EPSTS & USBD_EPSTS_EPSTS1_Msk)) { + g_usbd_CtrlOutToggle = USBD->EPSTS & USBD_EPSTS_EPSTS1_Msk; + if (g_usbd_CtrlOutSize < g_usbd_CtrlOutSizeLimit) { + u32Size = USBD_GET_PAYLOAD_LEN(EP_CTRL_OUT); + USBD_MemCopy((uint8_t *)g_usbd_CtrlOutPointer, + (uint8_t *)USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_OUT), u32Size); + g_usbd_CtrlOutPointer += u32Size; + g_usbd_CtrlOutSize += u32Size; + + if (g_usbd_CtrlOutSize < g_usbd_CtrlOutSizeLimit) + USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, g_usbd_CtrlMaxPktSize); + } + + if (g_usbd_CtrlOutSize >= g_usbd_CtrlOutSizeLimit && g_usbd_CtrlOutCallback) { + g_usbd_CtrlOutCallback(g_usbd_CtrlOutPointer - g_usbd_CtrlOutSize, g_usbd_CtrlOutSize); + } + } else { + USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, g_usbd_CtrlMaxPktSize); + } +} + +void Tas_USBD_SwReset(void) { + // Reset all variables for protocol + g_usbd_CtrlInPointer = 0; + g_usbd_CtrlInSize = 0; + g_usbd_CtrlOutPointer = 0; + g_usbd_CtrlOutSize = 0; + g_usbd_CtrlOutSizeLimit = 0; + g_u32EpStallLock = 0; + memset(&g_usbd_SetupPacket, 0, 8); + + // Reset PID DATA0 + for (int i = 0; i < USBD_MAX_EP; i++) USBD->EP[i].CFG &= ~USBD_CFG_DSQ_SYNC_Msk; + + // Reset USB device address + USBD_SET_ADDR(0); +} diff --git a/src/usbd_user.c b/src/usbd_user.c new file mode 100644 index 0000000..febbd73 --- /dev/null +++ b/src/usbd_user.c @@ -0,0 +1,245 @@ +#include "tasoller.h" + +uint8_t volatile g_u8Suspend = 0; +uint8_t g_u8Idle = 0; +uint8_t g_u8Protocol = 0; + +uint8_t gHidSetReport[64]; + +STR_VCOM_LINE_CODING gLineCoding = { 0, 0, 0, 0 }; +uint16_t gCtrlSignal; + +uint32_t volatile g_u32OutToggle = 0; + +void USBD_IRQHandler(void) { + uint32_t u32IntSts = USBD_GET_INT_FLAG(); + uint32_t u32State = USBD_GET_BUS_STATE(); + + if (u32IntSts & USBD_INTSTS_FLDET) { + // Floating detect + USBD_CLR_INT_FLAG(USBD_INTSTS_FLDET); + + if (USBD_IS_ATTACHED()) { + USBD_ENABLE_USB(); + } else { + USBD_DISABLE_USB(); + } + } + + if (u32IntSts & USBD_INTSTS_WAKEUP) { + USBD_CLR_INT_FLAG(USBD_INTSTS_WAKEUP); + } + + if (u32IntSts & USBD_INTSTS_BUS) { + USBD_CLR_INT_FLAG(USBD_INTSTS_BUS); + + if (u32State & USBD_STATE_USBRST) { + // Bus reset + USBD_ENABLE_USB(); + Tas_USBD_SwReset(); + g_u32OutToggle = 0; + g_u8Suspend = 0; + } + if (u32State & USBD_STATE_SUSPEND) { + // Enter power down to wait USB attached; enable USB but disable PHY + g_u8Suspend = 1; + USBD_DISABLE_PHY(); + } + if (u32State & USBD_STATE_RESUME) { + // Enable USB and enable PHY + USBD_ENABLE_USB(); + g_u8Suspend = 0; + } + } + + if (u32IntSts & USBD_INTSTS_USB) { + // USB event + if (u32IntSts & USBD_INTSTS_SETUP) { // Setup packet + USBD_CLR_INT_FLAG(USBD_INTSTS_SETUP); + + // Clear the data IN/OUT ready flag of control end-points + USBD_STOP_TRANSACTION(EP_CTRL_IN); + USBD_STOP_TRANSACTION(EP_CTRL_OUT); + + Tas_USBD_ProcessSetupPacket(); + } + + // Control endpoints + if (u32IntSts & USBD_INTSTS_CTRL_IN) { + USBD_CLR_INT_FLAG(USBD_INTSTS_CTRL_IN); + Tas_USBD_CtrlIn(); + } + if (u32IntSts & USBD_INTSTS_CTRL_OUT) { + USBD_CLR_INT_FLAG(USBD_INTSTS_CTRL_OUT); + Tas_USBD_CtrlOut(); + } + + // CDC endpoints + if (u32IntSts & USBD_INTSTS_CDC_IN) { + USBD_CLR_INT_FLAG(USBD_INTSTS_CDC_IN); + EP_CDC_IN_Handler(); + } + if (u32IntSts & USBD_INTSTS_CDC_OUT) { + USBD_CLR_INT_FLAG(USBD_INTSTS_CDC_OUT); + EP_CDC_OUT_Handler(); + } + if (u32IntSts & USBD_INTSTS_CDC_CMD) { + USBD_CLR_INT_FLAG(USBD_INTSTS_CDC_CMD); + // TODO: ACM packets have connect/disconnect? + } + + // IO4 HID endpoints + if (u32IntSts & USBD_INTSTS_HID_IO4_IN) { + USBD_CLR_INT_FLAG(USBD_INTSTS_HID_IO4_IN); + EP_HID_IO4_IN_Handler(); + } + + // IO4 Misc endpoints + if (u32IntSts & USBD_INTSTS_HID_MISC_IN) { + USBD_CLR_INT_FLAG(USBD_INTSTS_HID_MISC_IN); + EP_HID_MISC_IN_Handler(); + } + if (u32IntSts & USBD_INTSTS_HID_MISC_OUT) { + USBD_CLR_INT_FLAG(USBD_INTSTS_HID_MISC_OUT); + EP_HID_MISC_OUT_Handler(); + } + } +} + +void EP_HID_IO4_IN_Handler(void) { gu8HIDIO4Ready = 1; } +void EP_HID_MISC_IN_Handler(void) { gu8HIDMiscReady = 1; } +void EP_HID_MISC_OUT_Handler(void) { + // TODO: Handle anything we need to here +} +void EP_CDC_OUT_Handler(void) { + // Bulk OUT + if (g_u32OutToggle == (USBD->EPSTS & USBD_EPSTS_EPSTS3_Msk)) { + USBD_SET_PAYLOAD_LEN(EP_CDC_OUT, USBD_CDC_OUT_MAX_SIZE); + } else { + gu32RxSize = USBD_GET_PAYLOAD_LEN(EP_CDC_OUT); + gpu8RxBuf = (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CDC_OUT)); + + g_u32OutToggle = USBD->EPSTS & USBD_EPSTS_EPSTS3_Msk; + // Set a flag to indicate bulk out ready + gi8BulkOutReady = 1; + } +} +void EP_CDC_IN_Handler(void) { gu32TxSize = 0; } + +void Tas_USBD_ClassRequest(void) { + usb_setup_t setup; + Tas_USBD_GetSetupPacket(&setup); + + if (setup.bmRequestType & 0x80) { + // Device to host + switch (setup.bRequest) { + case GET_LINE_CODING: + if (setup.getLineCoding.wInterface == USBD_ITF_CDC_CMD) + USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_IN)), + (uint8_t *)&gLineCoding, setup.getLineCoding.wLength); + + // Data stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, setup.getLineCoding.wLength); + // Status stage + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + case GET_REPORT: { + uint32_t u32Size = 0; + uint8_t *pu8Data = NULL; + pu8Data = USBD_HID_GetReport(setup.hidGetReport.bReportId, &u32Size); + + Tas_USBD_PrepareCtrlIn(pu8Data, u32Size); + // Status stage + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + } + + case GET_IDLE: + USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, setup.hidGetIdle.wLength); + // Data stage + Tas_USBD_PrepareCtrlIn(&g_u8Idle, setup.hidGetIdle.wLength); + // Status stage + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + case GET_PROTOCOL: + USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, setup.hidGetProtocol.wLength); + // Data stage + Tas_USBD_PrepareCtrlIn(&g_u8Protocol, setup.hidGetProtocol.wLength); + // Status stage + Tas_USBD_PrepareCtrlOut(NULL, 0, NULL); + break; + + default: + // Setup error, stall the device + USBD_SetStall(EP_CTRL_IN); + USBD_SetStall(EP_CTRL_OUT); + break; + } + } else { + // Host to device + switch (setup.bRequest) { + case SET_CONTROL_LINE_STATE: + // TODO: Use bit[0] (DTR) to identify connection state + // Is RTS worth using? + if (setup.wIndex == USBD_ITF_CDC_CMD) gCtrlSignal = setup.wValue; + + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_LINE_CODING: + if (setup.setLineCoding.wInterface == USBD_ITF_CDC_CMD) + Tas_USBD_PrepareCtrlOut(&gLineCoding, sizeof gLineCoding, NULL); + + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_REPORT: + // Report Type = Output + if (setup.hidSetReport.bReportType == 2) { + Tas_USBD_PrepareCtrlOut(&gHidSetReport, sizeof gHidSetReport, + USBD_HID_SetReport); + + // USBD_SET_DATA1(EP_CTRL_OUT); + // USBD_SET_PAYLOAD_LEN(EP_CTRL_OUT, setup.wLength); + + // USBD_HID_RecvData( + // setup.hidSetReport.bReportId, + // (uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CTRL_OUT)), + // setup.hidSetReport.wLength); + + // // Status stage + // Tas_USBD_PrepareCtrlIn(NULL, 0); + } + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_IDLE: + g_u8Idle = setup.hidSetIdle.bDuration; + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + case SET_PROTOCOL: + g_u8Protocol = setup.hidSetProtocol.wProtocol; + // Status stage + USBD_SET_DATA1(EP_CTRL_IN); + USBD_SET_PAYLOAD_LEN(EP_CTRL_IN, 0); + break; + + default: + // Setup error, stall the device + USBD_SetStall(EP_CTRL_IN); + USBD_SetStall(EP_CTRL_OUT); + break; + } + } +} diff --git a/src/vcom.c b/src/vcom.c new file mode 100644 index 0000000..1decb3a --- /dev/null +++ b/src/vcom.c @@ -0,0 +1,102 @@ +#include "tasoller.h" + +#define BUF_SIZE_RX 512 +#define BUF_SIZE_TX 512 + +volatile uint8_t gu8VcomReady = 0; + +static volatile uint8_t gau8ComRbuf[BUF_SIZE_RX]; +static volatile uint16_t gu16ComRbytes = 0; +static volatile uint16_t gu16ComRhead = 0; +static volatile uint16_t gu16ComRtail = 0; + +static volatile uint8_t gau8ComTbuf[BUF_SIZE_TX]; +static volatile uint16_t gu16ComTbytes = 0; +static volatile uint16_t gu16ComThead = 0; +static volatile uint16_t gu16ComTtail = 0; + +uint8_t gau8RxBuf[64] = { 0 }; +volatile uint8_t *gpu8RxBuf = 0; +volatile uint32_t gu32RxSize = 0; +volatile uint32_t gu32TxSize = 0; + +volatile int8_t gi8BulkOutReady = 0; + +void _USB_VCOM_Tick_Tx(void) { + uint32_t u32Len; + if (gu32TxSize != 0) return; + + // Check wether we have new COM Rx data to send to USB or not + if (!gu16ComRbytes) { + // Prepare a zero packet if previous packet size is USBD_CDC_IN_MAX_SIZE and + // no more data to send at this moment to note Host the transfer has been done + u32Len = USBD_GET_PAYLOAD_LEN(EP_CDC_IN); + if (u32Len == USBD_CDC_IN_MAX_SIZE) USBD_SET_PAYLOAD_LEN(EP_CDC_IN, 0); + return; + } + + u32Len = gu16ComRbytes; + if (u32Len > USBD_CDC_IN_MAX_SIZE) u32Len = USBD_CDC_IN_MAX_SIZE; + + for (uint32_t i = 0; i < u32Len; i++) { + if (gu16ComRhead >= BUF_SIZE_RX) gu16ComRhead = 0; + gau8RxBuf[i] = gau8ComRbuf[gu16ComRhead++]; + } + + // __set_PRIMASK(1); + gu16ComRbytes -= u32Len; + // __set_PRIMASK(0); + + gu32TxSize = u32Len; + USBD_MemCopy((uint8_t *)(USBD_BUF_BASE + USBD_GET_EP_BUF_ADDR(EP_CDC_IN)), (uint8_t *)gau8RxBuf, + u32Len); + USBD_SET_PAYLOAD_LEN(EP_CDC_IN, u32Len); +} +void _USB_VCOM_Tick_Rx(void) { + // Process the Bulk out data when bulk out data is ready. + if (!gi8BulkOutReady) return; + if (gu32RxSize > BUF_SIZE_TX - gu16ComTbytes) return; + + for (uint32_t i = 0; i < gu32RxSize; i++) { + gau8ComTbuf[gu16ComTtail++] = gpu8RxBuf[i]; + if (gu16ComTtail >= BUF_SIZE_TX) gu16ComTtail = 0; + } + + // __set_PRIMASK(1); + gu16ComTbytes += gu32RxSize; + // __set_PRIMASK(0); + + gu32RxSize = 0; + gi8BulkOutReady = 0; // Clear bulk out ready flag + + // Ready to get next BULK out + USBD_SET_PAYLOAD_LEN(EP_CDC_OUT, USBD_CDC_OUT_MAX_SIZE); +} +void USB_VCOM_Tick(void) { + _USB_VCOM_Tick_Tx(); + _USB_VCOM_Tick_Rx(); +} + +uint8_t USB_VCOM_Read() { + if (!gu16ComTbytes) return 0xff; + gu16ComTbytes--; + if (gu16ComThead >= BUF_SIZE_TX) gu16ComThead = 0; + return gau8ComTbuf[gu16ComThead++]; +} +void USB_VCOM_Write(uint8_t u8Data) { + if (gu16ComRbytes < BUF_SIZE_RX) { + // Enqueue the character + gau8ComRbuf[gu16ComRtail++] = u8Data; + if (gu16ComRtail >= BUF_SIZE_RX) gu16ComRtail = 0; + gu16ComRbytes++; + } else { + // FIFO over run + } +} + +uint16_t USB_VCOM_Available(void) { return gu16ComTbytes; } +void USB_VCOM_PurgeTx(void) { + gu16ComRbytes = 0; + gu16ComRhead = 0; + gu16ComRtail = 0; +}