KinetisV - Peripheral Module Reference
KinetisV - Peripheral Module Reference
KinetisV - Peripheral Module Reference
Quick Reference
Contents
Section number
Title
Page
Chapter 1
General System Setup (Software Considerations)
1.1
Software considerations..................................................................................................................................................9
1.1.1
Overview............................................................................................................................................................9
1.1.2
Code execution...................................................................................................................................................9
1.1.3
1.1.4
1.1.3.1
1.1.3.2
1.1.4.2
Startup routines..................................................................................................................................11
1.1.4.2.1 Disable watchdog..............................................................................................................11
1.1.4.2.2 Initialize RAM..................................................................................................................11
1.1.4.2.3 Enable port clocks.............................................................................................................11
1.1.4.2.4 Ramp system clock to selected frequency........................................................................12
1.1.4.2.5 Enable UART for terminal communication......................................................................12
1.1.4.2.6 Jump to start of main function for application..................................................................12
Chapter 2
General System Setup (Hardware Considerations)
2.1
Hardware considerations.................................................................................................................................................13
2.1.1
Overview............................................................................................................................................................13
2.1.2
Floorplan............................................................................................................................................................13
2.1.2.1
2.1.3
Connectors.........................................................................................................................................14
Section number
Title
Page
2.1.3.2
2.1.3.3
Oscillators..........................................................................................................................................15
2.1.3.3.1 MCG oscillator..................................................................................................................15
2.1.3.4
General filtering.................................................................................................................................18
2.1.3.4.1 RESET_b and NMI_b.......................................................................................................18
2.1.3.4.2 General purpose I/O..........................................................................................................19
2.1.3.4.3 Analog inputs....................................................................................................................19
2.1.4
2.1.5
Debug interface..................................................................................................................................22
Chapter 3
Nested Vector Interrupt Controller (NVIC)
3.1
NVIC...............................................................................................................................................................................25
3.1.1
3.1.2
Overview............................................................................................................................................................25
3.1.1.1
Introduction .......................................................................................................................................25
3.1.1.2
Features .............................................................................................................................................25
Configuration examples.....................................................................................................................................26
3.1.2.1
3.1.2.2
Chapter 4
Clocking System
4.1
Clocking..........................................................................................................................................................................29
4.1.1
Overview............................................................................................................................................................29
4.1.2
Features..............................................................................................................................................................29
4.1.3
Configuration examples.....................................................................................................................................31
4.1.4
4.1.5
Section number
4.1.6
Title
Page
References..........................................................................................................................................................34
Chapter 5
Power Management Control (PMC/SMC/LLWU/RCM)
5.1
Introduction.....................................................................................................................................................................37
5.2
5.3
5.2.1
Overview............................................................................................................................................................37
5.2.2
5.2.2.2
Configuration examples.....................................................................................................................38
5.2.2.3
5.2.2.4
Hardware implementation..................................................................................................................40
5.3.2
Overview............................................................................................................................................................42
5.3.1.1
Introduction........................................................................................................................................42
5.3.1.2
Configuration examples.....................................................................................................................................43
5.3.2.1
5.4
5.3.2.2
5.3.2.3
5.4.2
Overview............................................................................................................................................................46
5.4.1.1
5.4.1.2
5.4.2.2
Module wake-up................................................................................................................................47
5.4.2.3
Pin wake-up........................................................................................................................................47
5.4.2.4
Section number
5.4.2.5
Title
Page
Wake-up sequence.............................................................................................................................48
5.5
5.6
5.7
Chapter 6
enhanced Direct Memory Access (eDMA) Controller
6.1
6.2
eDMA.............................................................................................................................................................................55
6.1.1
Overview............................................................................................................................................................55
6.1.2
Introduction........................................................................................................................................................55
eDMA trigger..................................................................................................................................................................57
6.2.1
DMA multiplexer...............................................................................................................................................57
6.2.2
Trigger mode......................................................................................................................................................58
6.2.3
6.3
Transfer process..............................................................................................................................................................59
6.4
6.5
Module configuration.........................................................................................................................................61
Chapter 7
Universal Asynchronous Receiver and Transmitter (UART) Module
7.1
Overview.........................................................................................................................................................................63
7.2
Features...........................................................................................................................................................................63
7.3
Configuration example....................................................................................................................................................64
7.4
7.3.1
7.3.2
7.3.3
7.3.4
Chapter 8
Memory-Mapped Divide and Square Root (MMDVSQ)
8.1
Introduction.....................................................................................................................................................................69
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
Section number
Title
Page
8.2
Features...........................................................................................................................................................................69
8.3
8.4
8.5
8.6
Execution time................................................................................................................................................................72
8.7
Tips of usage...................................................................................................................................................................73
8.7.1
8.7.2
Chapter 9
Analog-to-Digital Converter (ADC)
9.1
Overview.........................................................................................................................................................................75
9.2
Introduction.....................................................................................................................................................................75
9.3
Features...........................................................................................................................................................................76
9.4
ADC configuration..........................................................................................................................................................76
9.5
Chapter 10
Programmable Delay Block (PDB)
10.1 Overview.........................................................................................................................................................................79
10.2 Introduction.....................................................................................................................................................................79
10.3 Features...........................................................................................................................................................................80
10.4 PDB pre-trigger...............................................................................................................................................................80
10.5 Ping-Pong operation........................................................................................................................................................82
10.5.1 PDB pre-trigger sample mode............................................................................................................................82
10.5.2 Back-to-back sample mode................................................................................................................................82
10.5.2.1 2-channel ADC input back-to-back sample mode.............................................................................83
10.5.2.2 4-channel ADC input pre-trigger sample mode.................................................................................83
Chapter 11
Using FlexTimer (FTM) via Programmable Delay Block (PDB) to Schedule ADC Conversion
11.1 Overview.........................................................................................................................................................................85
11.2 Introduction.....................................................................................................................................................................85
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
Freescale Semiconductor, Inc.
Section number
Title
Page
11.3 Features...........................................................................................................................................................................86
11.4 Configuration code..........................................................................................................................................................87
11.4.1 FTM trigger 4-channel ADC ping-pong conversion via back-to-back mode of PDB.......................................87
11.4.2 ADC configuration.............................................................................................................................................87
11.4.3 PDB configuration.............................................................................................................................................88
11.4.4 FTM configuration.............................................................................................................................................88
11.4.5 ADC ISR............................................................................................................................................................89
Chapter 12
FlexTimer Module (FTM)
12.1 Overview.........................................................................................................................................................................91
12.2 Introduction.....................................................................................................................................................................91
12.3 Features...........................................................................................................................................................................91
12.3.1 FTM clock..........................................................................................................................................................92
12.3.2 Interrupts and DMA...........................................................................................................................................92
12.3.3 Modes of operation............................................................................................................................................92
12.3.4 Updating MOD and CnV...................................................................................................................................93
12.3.5 FTM period........................................................................................................................................................93
12.3.6 Additional features.............................................................................................................................................94
12.4 Configuration examples..................................................................................................................................................94
12.4.1 Example Edge Aligned PWM and Input Capture Mode.................................................................................94
Chapter 1
General System Setup (Software Considerations)
1.1 Software considerations
1.1.1 Overview
This chapter provides a quick look at some of the general characteristics of the Kinetis V
series of MCUs. This is a brief introduction of the operation of the devices and typical
software initialization.
For more information, see the device-specific reference manual and data sheet.
Software considerations
For more information, see the "Reset and Boot" chapter of the device-specific reference
manual.
1.1.4.1.1.1
CPSIE
r0,=0
r1,=0
r2,=0
r3,=0
r4,=0
r5,=0
r6,=0
r7,=0
1.1.4.1.1.2
import start
BL
; Unmask interrupts
Disable watchdog
For code development and debugging, it is best to disable the watchdog. The watchdog
can be disabled by first unlocking the watchdog followed by clearing the WDOGEN bit.
Users should avoid any breakpoints in between the following code lines during
debugging.
/* Disable the watchdog timer */
/* First unlock the watchdog so that we can write to registers */
DisableInterrupts;
WDOG>UNLOCK = 0xC520;
WDOG>UNLOCK = 0xD928; EnableInterrupts;
/* Clear the WDOG bit to disable the watchdog */
WDOG >STCTRLH & =~WDOG_STCTRLH_WDOGEN_MASK;
1.1.4.2.2
Initialize RAM
Depending on the application, the following steps may be required. First, copy the vector
table from flash to RAM, copy initialized data from flash to RAM, clear the zeroinitialized data section, and copy functions from flash to RAM.
1.1.4.2.3
To configure the I/O pin muxing options, the port clocks must first be enabled. This
allows the pin functions to later be changed to the desired function for the application.
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
Freescale Semiconductor, Inc.
11
Software considerations
SIM->SCGC5 |= (SIM_SCGC5_PORTA_MASK
| SIM_SCGC5_PORTB_MASK
| SIM_SCGC5_PORTC_MASK
| SIM_SCGC5_PORTD_MASK
| SIM_SCGC5_PORTE_MASK );
1.1.4.2.4
The Multipurpose clock generator (MCG) provides several options for clocking the
system. Configure the MCG mode, reference source, and selected frequency output based
on the needs of the system.
1.1.4.2.5
See the section describing UART in this document for more information.
1.1.4.2.6
Chapter 2
General System Setup (Hardware Considerations)
2.1 Hardware considerations
2.1.1 Overview
This chapter will outline the best practices for hardware design when using the Kinetis V
series MCUs. The designer must consider numerous aspects when creating the system so
that performance, cost, and quality meet the end-user expectations. Performance usually
implies high speed digital signalling, but it also applies to accurate sampling of analog
signals. Cost is influenced by component selection, of which the PCB may be the most
expensive element. Quality involves manufacturability, reliability, and conformance to
industry or governmental standards.
Evaluation boards are great for evaluating the operation and performance of the many
features of Freescale MCUs. However, evaluation systems are not ideal examples for
implementation of robust system design techniques. This document will mention some of
the hardware techniques found on the Freescale Tower Systems, and will give
recommendations that are more appropriate to conventional systems that are not required
to implement all of the feature options.
2.1.2 Floorplan
The organization of the printed circuit board (PCB) depends on many factors. Typically,
there are connectors, mechanical components, high speed signals, low speed signals,
switches, and power domains, among others, that need to be considered. While placement
of connectors and some mechanical components (switches, relays, and so on) is critical to
the end products form, there are some basic recommendations that can significantly
affect the electrical performance and electromagnetic compatibility (EMC) of the PCB
assembly.
13
Hardware considerations
2.1.2.1 Connectors
The PCB should be organized so that all of the connectors are along one edge of the
board and away from the MCU. The concept here is to prevent placing the MCU in
between connectors that can become effective radiators when cables are attached. This
also keeps the MCU from being in the path of high energy transients that can shoot across
the board from one connector to another. Connectors may be placed on adjacent edges of
the PCB if necessary, but only when the MCU is not in a direct path between the
connectors.
Connector locations should allow for placement of filter components. Noise must be
suppressed at the connector, before it can propagate onto the PCB. For more information
on this topic, see the input filtering section.
the proper power plane. Power traces from the planes should be kept as short as possible
as they are routed to circuitry, such as pullups, filters, other logic and drivers, on the top
and bottom layers. More information is given in the PCB layer stack-up section below.
2.1.3.3 Oscillators
The Kinetis MCU starts up with an internal digitally controlled oscillator (DCO) to
control the bus clocking, and then software can be used to enable an external oscillator if
desired. The external oscillator for the multipurpose clock generator (MCG) can range
from a 32.768 kHz crystal up to a 32 MHz crystal or ceramic resonator.
15
Hardware considerations
2.1.3.3.1
MCG oscillator
The high speed oscillator that can be used to source the MCG module is very versatile.
The component choices for this oscillator are detailed in the device-specific reference
manual. The placement of this crystal or resonator is described here.
The EXTAL and XTAL pins are located on the outside pad ring of the BGA package and
on corner pins of the LQFP/QFN package. This allows room for placement and routing of
the crystal or resonator on the top layer, close to the MCU. The feedback resistor and
load capacitors, if needed, can be placed on the top layer as well. See Figure 2-1, Figure
2-2, and Figure 2-3.
Note that the low power modes of this oscillator do not require a feedback resistor, and
may not require external load capacitors. See the device-specific reference manual for
details. This makes it as simple as possible because only one component has to be placed
and routed. Low power oscillators are more susceptible to interference by system
generated noise, therefore the guidelines for crystal routing are important.
The crystal or resonator must be located close to the MCU. No signals of any kind should
be routed on the layer directly below the crystal. The load capacitors and ground of the
crystal package must be connected to a single ground trace coming from the closest VSS
pin or the recommended ground under the MCU. An unbroken ground plane on the layer
directly below the crystal is recommended. A ground pour must be placed around the
crystal and its load components to protect it from crosstalk from adjacent signals on the
mounting layer.
R2
XTAL
R1
EXTAL
C1
Y1
C2
Ground under
MCU package
XTAL
EXTAL
VSS
VDD
C0
VDD
Ground pour
(not ground ring)
R1
VSS/GND
Y1
C1
C2
No signals or
components
under crystal
components!
17
Hardware considerations
Ground under
MCU package
XTAL
EXTAL
VSS
VDD
R2
VDD
C0
Ground pour
(not ground ring)
R1
VSS/GND
Y1
C1
No signals or
components
under crystal
components!
C2
The RESET_b pin, if enabled, should have a 100 nF capacitor close to the MCU for
transient protection. The NMI_b pin, if enabled, must not have any capacitance
connected to it. Each pin, when enabled as their default function, has a weak internal
pullup, but an external 4.7 k to 10 k pullup is recommended. As with power pin
filtering, it is recommended to minimize the ground loop for the capacitor and the VDD
loop for the pullup resistor for these pins.
The RESET_b pin also has a configurable digital filter to reject potential noise on this
input after power-up. The configuration bits are located in the RCM_RPFC register.
While use of this filter may negate the need for the pullup and capacitor mentioned
above, it is still recommended to use external filtering in electrically noisy environments.
2.1.3.4.2
General purpose inputs, such as low speed inputs, timer inputs, and signals from offboard should have low pass filters (series resistor and capacitor to ground) to prevent data
corruption due to crosstalk or transients. The filter capacitor should be placed close to the
MCU pin, while the resistor can be placed closer to the source.
Inputs that come from connectors should have low pass filtering at the connector to
prevent noise from propagating onto the PCB. This requires a robust ground structure
around the connector. Series resistors for signals that come from off-board should be
placed as close to the connector as possible. A filter cap closer to the MCU input pin may
be required if the signal trace length is very long and can pick up noise from other
circuits.
Output pins must not have any significant capacitance placed close to the MCU. These
signals can have capacitors at the load or connector to minimize radiated emissions if
necessary.
NOTE
Must ensure that VDD be powered prior to /RESET_B and all
others GPIO pins.
2.1.3.4.3
Analog inputs
Analog inputs should also have low pass filters. The challenge with analog inputs,
especially for high resolution analog-to-digital conversions, is that the filter design needs
to consider the source impedance and sample time rather than a simple cutoff frequency.
This topic cannot be discussed in detail here, but the general concept is that fast sample
times will require smaller capacitor values and source impedances than slow sample
times. Higher resolution inputs may require smaller capacitor values and source
impedances than lower resolution inputs.
In general, capacitor values can range from 10 pF for high speed conversions to 1 uF for
low speed conversions. Series resistors are added for RC filter and a typical value is 100
Ohms.
19
Hardware considerations
21
The debug signals are multiplexed with general purpose I/O pins, therefore some signals
will require proper biasing to select the operating mode. The SWD_CLK pin has an
internal pull down device and SWD_DIO has an internal pull up device. The connectors
23
for this interface are keyed dual row 0.050 centered headers. When implementing either
of these headers on a target system, pin 7 must be depopulated to use the 19-pin or 9-pin
adapters from the debug tool. The Samtec part numbers for these connectors are:
This interface is useful during the development phase of a project. The header may not
need to be populated in the production phase of the project, but the PCB pads should be
kept available for future debugging purposes.
Chapter 3
Nested Vector Interrupt Controller (NVIC)
3.1 NVIC
3.1.1 Overview
This chapter shows how the NVIC is integrated into the Kinetis MCUs and how to
configure it and set-up module interrupts. It also demonstrates the steps to set the
interrupts for the desired peripheral and how to locate the vector table from flash to
RAM.
3.1.1.1 Introduction
The NVIC is a standard module on the ARM Cortex-M series. This module is closely
integrated with the core and provides very low latency entering and exiting an interrupt
service routine (ISR). It takes 15 cycles to exit an ISR, unless the exit from the interrupt
is into another pending ISR. In this case, the MCU tail-chains and the exit and re-entry
takes 11 cycles.
The NVIC provides four different interrupt priorities which can be used to control the
order in which interrupts must be serviced. Priorities are 0-3, with 0 receiving the highest
priority. For example, in a motor-control application, if a timer interrupt and UART occur
simultaneously, the timer interrupt that moves the motor is more critical than the UART
interrupt receiving a character. The timer priority must be set higher than the UART.
25
NVIC
3.1.1.2 Features
On Kinetis V1x series MCUs the NVIC provides up to 48 interrupt sources including 16
that are core specific. It also implements up to four priority levels that are fully
programmable. The NVIC uses a vector table to manage the interrupts. This vector table
can be stored in either flash or RAM, depending on the application.
Table 3-1. Core exceptions
Address
Vector
IRQ
Source module
Source description
ARM core
ARM core
ARM core
ARM core
Hard fault
11
ARM core
SVCall
12
14
ARM core
15
ARM core
This example shows how to set up the NVIC for a specific module, using the LPTMR.
The steps to configure the NVIC for this module are:
1. Identify the vector number and the IRQ number of the module from the vector table
in the device-specific reference manual in the section Interrupt Channel
Assignments. For the LPTMR the vector is 44 and IRQ is 28
2. Identify which bit to set, perform a modulo operation dividing the IRQ number by
32. This number is used to enable the interrupt on NVIC->ISER[0] and to clear the
pending interrupts from NVIC->ICPR[0].
Example:
LPTMR BIT = 28 mod 32
LPTMR BIT = 28
3. At this point, the interrupt for the LPTMR can be configured:
NVIC->ICPR[0|=(1<<28);
NVIC->ISER[0|=(1<<28);
4. Next, set the interrupt priority level. This is application dependent. On Kinetis V1x
series MCUs there are four different priority levels. To set the priority, write to the
NVIC->IP[IRQn] register; the "IRQn" represents the IRQ number divided by 4. Note
the most significant nibble is used to set up the priority, the lower nibble is reserved
and reads as zero. The LPTMR example sets the priority to priority 3:
NVIC->IP[_IP_IDX(IRQn)] = (NVIC->IP[_IP_IDX(IRQn)] & ~(0xFF << _BIT_SHIFT(IRQn))) |
(((priority << (8 - __NVIC_PRIO_BITS)) & 0xFF) << _BIT_SHIFT(IRQn)); //Set Priority to
priority 3 to the LPTMR module
5. After the NVIC registers are set up, finish the peripheral configuration that must
enable the interrupt.
6. In the ISR, clear the peripheral interrupt flag and read back the status register to
avoid re-entrance. For this example:
void LPTMR_Isr (void)
{
LPTMR0->CSR|=LPTMR_CSR_TCF_MASK; //Clear LPTMR Compare flag
LPTMR0->CSR = ( LPTMR_CSR_TEN_MASK |
LPTMR_CSR_TIE_MASK |
LPTMR_CSR_TCF_MASK );
/*ISR code goes here*/
}
27
NVIC
The NVIC provides a simple way to reallocate the vector table. The user needs to set up
the Vector Table Offset Register (VTOR) with the address offset for the new position.
If you plan to store the vector table in RAM, you must first copy the table from the flash
to RAM. Also note that in some low power modes, a portion of the RAM will not be
powered, which can lead to a vector table corruption. In this case, locate the vector table
in the flash prior to entering a low power mode.
3.1.2.2.1 Code example and explanation
The CM0+ core adds support for a programmable Vector Table Offset Register (VTOR)
to relocate the exception vector table. This device supports booting from internal flash.
The vector table is initially in flash. If the vector table is needed in RAM, move it in this
manner:
1. Copy the entire vector table from flash to RAM. The linker command file labels are
useful in this step. Refer to the following sample code:
/*Address for VECTOR_TABLE and VECTOR_RAM come from the linker file*/
extern uint32 __VECTOR_TABLE[];
extern uint32 __VECTOR_RAM[];
/* Copy the vector table to RAM */
if (__VECTOR_RAM != __VECTOR_TABLE)
{
for (n = 0; n < 0x104; n++)
__VECTOR_RAM[n] = __VECTOR_TABLE[n];
}
2. After the table has been copied, set the proper offset for the VTOR register:
/* Point the VTOR to the new copy of the vector table */
write_vtor((uint32)__VECTOR_RAM);
It is important to follow these steps in order, to ensure that there is always a valid vector
table.
Chapter 4
Clocking System
4.1 Clocking
4.1.1 Overview
This chapter will discuss the clocking system and the multipurpose clock generator
(MCG) module. Examples will provide an overview of how to switch between the MCG
modes and specifically how to enable the on-chip FLL for high-speed operation. Clock
selection options will be discussed for the RTC.
4.1.2 Features
An example of the clocking system is summarized in the following figure. Not all clock
sources will be available on specific devices. Refer to the individual device reference
manual for full details of the available clock sources.
29
Clocking
MCG
4 MHz IRC
SIM
IRCS
FCRDIV
MCGIRCLK
CG
32 kHz IRC
FLL
Core clock,
platform clock,
and system clock
fast peripherals
OUTDIV1
CG
OUTDIV4
CG
Bus clock/
Flash clock
OUTDIV5
CG
Alt ADC
clock
CLKS
IREFS
MCGOUTCLK
FRDIV
EREFS0
System oscillator
EXTAL0
OSCCLK
XTAL0
OSC
logic
OSC32KCLK
MCGFLLCLK
CG
OSCERCLK
ERCLK32K
XTAL_CLK
CG Clock gate
Note: See subsequent sections for details on where these clocks are used.
The system level clocks are provided by the MCG. The MCG consists of:
Two individually trimmable internal reference clocks (IRC), a slow IRC with a
frequency of ~32 kHz and a fast IRC with a frequency of ~4 MHz, which can be
reduced by means of the FCRDIV divider
Frequency locked loop (FLL) using the slow IRC or an external source as the
reference clock
Auto trim machine (ATM) to allow both of the IRCs to be trimmed to a custom
frequency using an externally-generated reference clock
The clocks provided by the MCG are summarized as follows:
MCGOUTCLK The main system clock used to generate the core, bus, and memory
clocks. It can be generated from one of the on-chip reference oscillators, the on-chip
crystal/resonator oscillator, an externally generated square wave clock, or the FLL.
MCGFLLCLK The output of the FLL and is available any time the FLL is enabled.
MCGIRCLK The output of the selected IRC. The selected IRC will be enabled
whenever this clock is selected.
In addition to the clocks provided by the MCG, there are three other system level clock
sources available for use by various peripheral modules:
OSCERCLK The clock provided by the system oscillator and is the output of the
oscillator or the external square wave clock source.
ERCLK32K The output of the system oscillator if it is configured in low power
mode at 32 kHz, the external RTC_CLKIN path or the low power oscillator (LPO).
LPO The output of the low power oscillator. It is an on-chip, very low power
oscillator with an output of approximately 1 kHz that is available in all run and low
power modes except VLLS0.
31
Clocking
// check if in FEI mode
/*
if (!((((MCG -> S & MCG_S_CLKST_MASK) >> MCG_S_CLKST_SHIFT) == 0x0) && // check CLKS mux
has selcted FLL output
(MCG -> S & MCG_S_IREFST_MASK)))
// check PLLS mux
has selected FLL
{
*/
return 0x1;
33
Clocking
4.1.6 References
The following list of application notes associated with crystal oscillators are available on
the Freescale website at www.freescale.com. They discuss common oscillator
characteristics, potential problems, and troubleshooting guidelines:
35
Clocking
Chapter 5
Power Management Control (PMC/SMC/LLWU/RCM)
5.1 Introduction
This chapter is a brief description of the power management features of the Kinetis V
series 32-bit MCU.
There are four modules covered in this chapter:
37
in RUN, WAIT, and STOP modes. POR circuitry is on in all modes and can be optionally
disabled in VLLS0. The user has control over whether LVD is used and whether the POR
is enabled in the lowest power mode (VLLS0). When using the low voltage detect
features, the user has full control over the LVD and LVW trip voltages. The LVW is a
warning detect circuit and the LVD is reset detect circuit.
As voltage falls below the warning level, the LVW circuit flags the warning event and
can cause an interrupt. If the voltage continues to fall, the LVD circuit flags the detect
event and can either cause a reset or an interrupt. The user can choose what action to take
in the interrupt service routine. If a detect is selected to drive reset, the LVD circuit holds
the MCU in reset until the supply voltage rises above the detect threshold.
The POR circuit for the MCU will hold the MCU in reset based upon the VDD voltage.
Before entering the VLLS0 low power mode, the user can choose to disable the POR
circuit. Because the MCU is switched off in VLLS0, the POR protection is not really
needed and can be disabled. This saves a few hundred nano amps of power while the
MCU is in this mode.
If the POR circuit is disabled in VLLS0, the MCU will continue to hold the state of the
pins until the VDD levels are much lower than the POR trip voltage levels.
Exiting VLLS0 follows the reset mode. The POR circuit is reenabled protecting the MCU
operation during the recovery.
39
The following diagram shows the observed behavior of the reset pin during a ramp of
VDD. In both of the following diagrams RESET asserts initially as the POR circuit is
powered up. Next RESET is released when untrimmed LVD level is reached. Next
RESET is asserted when LVD trim register is loaded, followed by RESET being released
when trimmed LVD level is reached.
Figure 5-2. Observed RESET pin behavior during normal slow VDD rise
The following diagram shows the observed behavior of the reset pin during a fast ramp of
VDD.
Figure 5-3. Observed RESET pin behavior during normal fast VDD rise
41
5.3.1.1 Introduction
There are 10 power modes and some new clocking options. These modes and options are
described below.
1. Run Default Operation of the MCU out of Reset, On-chip voltage regulator is On,
full capability.
2. Very Low Power Run (VLPR) On-chip voltage regulator is in a mode that
supplies only enough power to run the MCU in a reduced frequency. Core clock up
to 4 MHz and Bus frequency up to 1 MHz.
3. Wait ARM core enters Sleep mode, NVIC remains sensitive to interrupts,
Peripherals Continue to be clocked.
4. Stop ARM core enters DeepSleep mode, NVIC is disabled, WIC is used to wake
up from interrupt, peripheral clocks are stopped.
5. Very Low Power Wait (VLPW) ARM core enters Sleep mode, NVIC remains
sensitive to interrupts (FCLK = ON), On-chip voltage regulator is in a mode that
supplies only enough power to run the MCU at a reduced frequency.
6. Very Low Power Stop (VLPS) ARM core enters DeepSleep mode, NVIC is
disabled (FCLK = OFF), WIC is used to wake up from interrupt, peripheral clocks
are stopped, On-chip voltage regulator is in a mode that supplies only enough power
to run the MCU at a reduced frequency, all SRAMs are operating (content retained
and I/O states held).
7. Very Low Leakage Stop3 (VLLS3) ARM core enters SleepDeep mode, NVIC is
disabled, LLWU is used to wake up, peripheral clocks are stopped, all SRAMs are
operating (content retained and I/O states held), and most modules are disabled.
8. Very Low Leakage Stop 1 (VLLS1) ARM core enters SleepDeep mode, NVIC is
disabled, LLWU is used to wake up, peripheral clocks are stopped, all SRAMs are
powered down, and I/O states held. Most modules are disabled.
9. Very Low Leakage Stop 0 (VLLS0) Lowest Power Mode ARM core enters
SleepDeep mode, NVIC is disabled, LLWU is used to wake up, peripheral clocks are
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
42
stopped, All SRAMs are powered down, and I/O states held. Most modules are
disabled, LPO shut down, optional POR brown-out detection.
The modules available in each of the power modes are described in a table. Please see
Module operation in low-power modes for the details of the module operations in each of
the low-power modes.
43
At this point, notice that the LPTMR, and optionally, the brown-out detection, are the
only modules that are fully functional in all of the lowest power modules. Notice also the
modules that allow wake-up in the low-power modes such as the GPIO, LLWU, or the
comparator.
In this example system, the MCU spends most of the time in one of the lower power
modes waking up periodically to check sensor signals, plus other house-keeping tasks.
The MCU also wakes up from a user input. This can be hitting a button, a touch of a
capacitive sensor, the rise or fall of an analog signal from a sensor feeding the
comparator. To enable these sources please refer to the LLWU section 3 in the devicespecific reference manual for configuration details.
The example drivers code for SMC are available from the Freescale Web site
www.freescale.com.
Please refer to AN4503 for power management ideas and explanations as well as mode
entry and exit drivers.
This write allows the MCU to enter WAIT, Normal STOP or VLPS only. It is then no
longer possible to enter any other low-power mode.
After the PMPROT register has been written, the write to PMCTRL and STOPCTRL
determines the mode that will be entered. For our example, entry into VLPS mode would
be enabled with this write sequence.
SMC -> PMCTRL &= ~SMC_PMCTRL_STOPM_MASK;
SMC -> PMCTRL |= SMC_PMCTRL_STOPM(0x02);
5.3.2.1.1
To ensure that the correct mode is entered there are some serialization considerations.
This means that the write to PMCTRL takes 6-7 cycles to complete and if the write to
PMCTRL is done immediately before the WFI instruction is executed, see the bad code
below, then the MCU may try to enter the mode that was defined before the write was
made.
//BAD CODE//
/* Set the SLEEPDEEP bit to enable deep sleep mode (STOP) */
SCB -> SCR |= SCB_SCR_SLEEPDEEP_MASK;
SMC -> PMCTRL |= SMC_PMCTRL_STOPM(0x2);
/* if PMCTRL register was previously a 0x00 before the write,
the low power mode entered with the execution of next
instruction WFI may be NORMAL STOP not VLPS */
asm("WFI");
If you need to change the mode control value of STOPM right before entering the lowpower mode, then it is best to do a read back of the PMCTRL register before executing
the WFI. This insures the write has completed before the core starts the low-power mode
entry. See the updated serialized code sequence below. To make sure the read does not
get optimized out by the compiler define as volatile.
// BETTER CODE //
volatile unsigned int dummyread;
/* Set the SLEEPDEEP bit to enable deep sleep mode (STOP) */
SCB -> SCR |= SCB_SCR_SLEEPDEEP_MASK;
SMC -> PMCTRL |= SMC_PMCTRL_STOPM(0x2);
dummyread = SMC -> PMCTRL;
asm("WFI");
45
Recovery from VLLSx is through the wake-up reset event. The MCU will wake from
VLLSx by means of reset, an enabled pin, or an enabled module. See Table 5-3 in the
LLWU configuration section for a list of the sources. The wake-up flow from VLLS0,
VLLS1, and VLLS3 is through reset. The wake-up bit in the SRS registers is set,
indicating that the MCU is recovering from a low power mode. Code execution begins
but the I/O are held in the pre-low-power mode entry state and the oscillator is disabled
even if EREFSTEN had been set before entering VLLSx. The user is required to clear
this hold by writing to PMC_REGSC[ACKISO].
Prior to releasing the hold the user must reinitialize the I/O to the pre-low-power mode
entry state, so that unwanted transitions on the I/O do not occur when the hold is released.
47
The code below configures a pin as a GPIO input pin. It can be a LLWU wake-up as long
as it is selected as a digital input pin.
/* Enable Port D6 to be a digital pin. */
SIM -> SCGC5 = SIM_SCGC5_PORTD_MASK;
PORTD -> PCR6 = (PORT_PCR_ISF_MASK |
// clear Flag if there
PORT_PCR_MUX(01) |
// GPIO
PORT_PCR_IRQC(0x0A) | // falling= A Rising = 9
PORT_PCR_PE_MASK |
// Pull enable
PORT_PCR_PS_MASK);
// pull up/down enable
GPIOD -> PDDR &= 0xFFFFFFBF;
// set Port D6 as input
/* Set the LLWU pin enable bits to enable the PORTD6 input
* to be a wakeup source.
* WUPE15 in the LLWU_PE4 register is used in this case
* since it is associated with PTD6. */
LLWU -> PE4 = LLWU_PE4_WUPE15(2); //falling edge detection
This needs to be done for each of the pins you want to enable as an interrupt and low
leakage mode wake-up source.
For example, to allow the processing of the pin PTE1, add the following initialization
code:
/* example code in the interrupt vectors initialization code */
NVIC_EnableIRQ(LLWU_IRQn);
// ready for this interrupt.
NVIC_EnableIRQ(PORTBCDE_IRQn); // ready for this interrupt.
Then, there is a need for interrupt service routines for the two enabled interrupt sources,
the LLWU interrupt, and the port BCDE interrupt.
For VLLS0, VLLS1, or VLLS3, the exit is always through the reset vector and then
through the interrupt vector of the LLWU immediately after the LLWU interrupt is
enabled in the NVIC with the "NVIC_EnableIRQ(LLWU_IRQn)" function call. There is
a WAKEUP bit in the RCM_SRS0 register that allows the user to tell if the reset was due
to an LLWU wake-up event.
An example of wake-up test code is shown below:
if (RCM -> SRS0 & RCM_SRS0_WAKEUP_MASK){
printf("\nWakeup bit set from low power mode ");
if (((SMC -> PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) &&
((SMC -> STOPCTRL & SMC_STOPCTRL_VLLSM_MASK)== 0))
printf("VLLS0 exit ") ;
if (((SMC -> PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) &&
((SMC -> STOPCTRL & SMC_STOPCTRL_VLLSM_MASK)== 1))
printf("VLLS1 exit ") ;
if (((SMC -> PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) &&
((SMC -> STOPCTRL & SMC_STOPCTRL_VLLSM_MASK)== 2))
printf("VLLS2 exit ") ;
if (((SMC -> PMCTRL & SMC_PMCTRL_STOPM_MASK)== 4) &&
((SMC -> STOPCTRL & SMC_STOPCTRL_VLLSM_MASK)== 3))
printf("VLLS3 exit ") ;
If the LPTMR is the wake-up source and the LLWU interrupt is enabled in sequence
before the LPTMR interrupts you must clear the source of the module interrupt or else
the code execution will never leave the LLWU interrupt service routine. An example is
given in the code snippet below:
if (LLWU -> F3 & LLWU_F3_MWUF0_MASK) {
SIM -> SCGC5 |= SIM_SCGC5_LPTMR_MASK;
LPTMR0 -> CSR |= LPTMR_CSR_TCF_MASK;
// write 1 to TCF to clear the LPT timer compare flag
LPTMR0 -> CSR = ( LPTMR_CSR_TEN_MASK
| LPTMR_CSR_TIE_MASK
| LPTMR_CSR_TCF_MASK );
// write one to clear the flag
LLWU -> F3 |= LLWU_F3_MWUF0_MASK;
}
The I/O states and the oscillator setup are held if the wake-up event is from VLLS0,
VLLS1, or VLLS3. The user is required to clear this hold by writing to the ACKISO bit
in the PMC_REGSC register. Prior to releasing the hold the user must reinitialize the I/O
to the pre-low-power mode entry state, so that unwanted transitions on the I/O do not
occur when the hold is released.
if (PMC -> REGSC & PMC_REGSC_ACKISO_MASK)
PMC -> REGSC |= PMC_REGSC_ACKISO_MASK;
49
Modules
VLPR
VLPW
Stop
VLPS
VLLSx
static
static
OFF
Core modules
NVIC
FF
FF
System modules
Mode Controller
FF
FF
FF
FF
FF
static
static
static
static
FF2
low power
low power
ON
low power
low power in
VLLS3, OFF in
VLLS0/1
disabled
disabled
ON
disabled
disabled
Brown-out
Detection
ON
ON
ON
ON
ON in VLLS1/3,
optionally disabled
in VLLS03
eDMA
FF
FF
Async operation
Async operation
OFF
FF
FF
FF
static
static
static
OFF
ON
ON in VLLS1/3,
OFF in VLLS0
LLWU1
Regulator
LVD
Async operation in
CPO
Watchdog
FF
static in CPO
EWM
FF
FF in PSTOP2
Clocks
1kHz LPO
System oscillator
(OSC)
ON
ON
ON
OSCERCLK
optional
OSCERCLK max of
limited to low
16 MHz crystal
range/low power in
VLLS1/3, OFF in
VLLS0
MCG
4 MHz IRC
4 MHz IRC
OFF
Core clock
4 MHz max
OFF
OFF
OFF
OFF
Platform clock
4 MHz max
4 MHz max
OFF
OFF
OFF
System clock
4 MHz max
4 MHz max
OFF
OFF
OFF
1 MHz max
OFF
OFF
OFF
OFF in CPO
Bus clock
1 MHz max
OFF in CPO
25 MHz max in
PSTOP2 from RUN
1 MHz max in
PSTOP2 from
VLPR
Memory and memory interfaces
Table continues on the next page...
VLPR
VLPW
Stop
VLPS
VLLSx
low power
low power
low power
OFF
low power
low power
low power
low power in
VLLS3, OFF in
VLLS0/1
No register access
in CPO
SRAM
low power
Communication interfaces
UART0
1 Mbit/s
1 Mbit/s
static
static
OFF
1 Mbit/s
Async operation
Async operation
OFF
static
static
OFF
static, address
match wakeup
static, address
match wakeup
OFF
Async operation in
CPO
UART1
1 Mbit/s
Async operation in
CPO
SPI0
FF in PSTOP2
50 kbit/s
50 kbit/s
static, address
match wakeup in
CPO
FF in PSTOP2
Timers
FTM0, FTM1,
FTM2
PDB
FF
FF
static
static
OFF
FF
static
static
OFF
FF
Async operation
Async operation
Async operation4
OFF
HS or LS compare
LS compare in
VLLS1/3, OFF in
VLLS0
static
static, OFF in
VLLS0
static
static
Async operation in
CPO
FF
Async operation in
CPO
LPTMR
FF
FF in PSTOP2
Analog
16-bit ADC
FF
FF
FF
FF in PSTOP2
FF
HS or LS compare
in CPO
6-bit DAC
FF
FF
HS or LS compare
FF in PSTOP2
FF
static in CPO
12-bit DAC
static
FF in PSTOP2
FF
static
51
VLPR
VLPW
static in CPO
Stop
VLPS
VLLSx
static output,
wakeup input
FF in PSTOP2
Human-machine interfaces
GPIO
FF
FF
static output,
wakeup input
FF in PSTOP2
1. Using the LLWU module, the external pins available for this chip do not require the associated peripheral function to be
enabled. It only requires the function controlling the pin (GPIO or peripheral) to be configured as an input to allow a
transition to occur to the LLWU.
2. Since LPO clock source is disabled, filters will be bypassed during VLLS0
3. The STOPCTRL[PORPO] bit in the SMC module controls this option.
4. LPO clock source is not available in VLLS0. To use system OSC in VLLS0 it must be configured for bypass (external
clock) operation. Pulse counting is available in all modes.
5. CMP in stop or VLPS supports high speed or low speed external pin-to-pin or external pin-to-DAC compares. CMP in
VLLSx only supports low speed external pin-to-pin or external pin-to-DAC compares. Windowed, sampled & filtered modes
of operation are not available while in stop, VLPS, or VLLSx modes.
From
To
RUN
WAIT
WAIT
RUN
Interrupt or Reset
RUN
STOP
STOP
RUN
Interrupt or Reset
RUN
VLPR
Trigger conditions
RUN
Set PMCTRL[RUNM]=00 or
Reset.
VLPR
VLPW
VLPW
VLPR
Interrupt
VLPW
RUN
Reset.
From
To
VLPR
VLPS
Trigger conditions
PMCTRL[STOPM]=000 If PMCTRL[STOPM]=000 and
STOPCTRL[PSTOPO]=00, then VLPS mode is entered
instead of STOP. If PMCTRL[STOPM]=000 and
STOPCTRL[PSTOPO]=01 or 10, then only a Partial Stop
mode is entered instead of VLPS or 010,
Sleep-now or sleep-on-exit modes entered with SLEEPDEEP
set, which is controlled in System Control Register in ARM
core.
VLPS
VLPR
Interrupt
NOTE: If VLPS was entered directly from RUN, hardware
will not allow this transition and will force exit back to
RUN
RUN
VLPS
PMPROT[AVLP]=1, PMCTRL[STOPM]=010,
Sleep-now or sleep-on-exit modes entered with SLEEPDEEP
set, which is controlled in System Control Register in ARM
core.
VLPS
RUN
RUN
VLLSx
VLLSx
RUN
VLPR
VLLSx
PMPROT[AVLLS]=1, PMCTRL[STOPM]=100,
STOPCTRL[VLLSM]=x (VLLSx), Sleep-now or sleep-on-exit
modes entered with SLEEPDEEP set, which is controlled in
System Control Register in ARM core.
Wake up from enabled LLWU LPTMR input source or RESET
pin
PMPROT[AVLLS]=1, PMCTRL[STOPM]=100,
STOPCTRL[VLLSM]=x (VLLSx), Sleep-now or sleep-on-exit
modes entered with SLEEPDEEP set, which is controlled in
System Control Register in ARM core.
LLWU pin
LLWU_P3
PTA4
LLWU_P5
PTB0
LLWU_P6
PTC1
LLWU_P7
PTC3
LLWU_P8
PTC4
LLWU_P9
PTC5
LLWU_P10
PTC6
LLWU_P12
PTD0
Table continues on the next page...
53
LLWU_P13
PTD2
LLWU_P14
PTD4
LLWU_P15
PTD6
LLWU_M0IF
LPTMR0
LLWU_M1IF
CMP0
LLWU_M2IF
CMP1
LLWU_M3IF
Reserved
LLWU_M4IF
Reserved
LLWU_M5IF
Reserved
LLWU_M6IF
Reserved
LLWU_M7IF
Reserved
Chapter 6
enhanced Direct Memory Access (eDMA) Controller
6.1 eDMA
6.1.1 Overview
This chapter is a compilation of code examples and quick reference materials created to
shorten the development time of your applications that use the eDMA controller of the
Kinetis V series. Consult the device-specific reference manual for specific part
information.
This chapter demonstrates how to configure and use the eDMA controller to create data
movement between different memory and peripheral spaces without CPU intervention.
6.1.2 Introduction
The eDMA controller provides the ability to move data from one memory mapped
location to another. After it is configured and initiated, the eDMA controller operates in
parallel to the core, performing data transfers that would otherwise have been handled by
the CPU. This results in reduced CPU loading and a corresponding increase in system
performance. Figure 6-1 illustrates the functionality provided by the eDMA controller.
55
eDMA
The eDMA controller contains a 16-bit data buffer as temporary storage. Because the
Kinetis V series is a crossbar based architecture, the CPU is the primary bus master
connected to the M0 master port. The eDMA is connected to the M2 master port of the
crossbar switch. Therefore, the CPU and eDMA can access different slave ports
simultaneously. With this multi-master architecture, the system can maximize the use of
the eDMA features. Figure 6-2 shows the basic architecture of the Kinetis V series. A
specialized device may have differencesrefer to the device-specific reference manual
for details.
GPIO
controller
Crossbar Switch
ARM core
unified bus
M0
S0
Flash
controller
M2
S1
SRAML
BME
SRAMU
S2
DMA
Master Modules
Peripheral
bridge 0
Peripherals
Slave Modules
The crossbar switch forms the heart of this multi-master architecture. It links each master
to the required slave device. Both fixed priority and round robin arbitration schemes are
available. If both masters attempt joint access to the same slave, an arbitration scheme
begins and eliminates bus contention.
57
eDMA trigger
59
Configuration steps
Source Data
Transferred
(bytes
- n) =4
DMA Request
Minor Loop
CITER
3
DMA Request
1
2
3
4
CITER
2
DMA Request
1
2
3
4
1
2
3
4
CITER
1
(channel activated)
Time
Major Loop
61
Using the above configurations, the required DMA functionality for this example has
been achieved.
Chapter 7
Universal Asynchronous Receiver and Transmitter
(UART) Module
7.1 Overview
The UART module on the Kinetis V series devices supports asynchronous, full-duplex
serial communications with peripheral devices or other CPUs. The UART module has
four main modes of operation -- UART, IrDA, and LIN mode.
The following sections will discuss the features and use of the UART in UART mode. In
particular the use of the UART as an RS-232 serial communication port will be
described. For full details on the UART module, including all of its features and modes
of operation, please see the device-specific reference manual.
7.2 Features
The feature set available on the UARTs vary from UART to UART. Basic UART
functionality is available on all UARTs, but the clock source for the module and the
transmit and receive FIFO sizes can vary. The UART intantiation table lists the UART
features that vary based on UART module instantiation.
Table 7-1. UART instantiations on Kinetis
UART instance
LIN supported?
FIFOs
Module clock
UART0
Yes
Core clock
UART1
Yes
Peripheral clock
NOTE
This table describes the UART instantiations on the Kinetis V
series devices available at the time of publication. As new
63
Configuration example
of the initial configuration of both the UART and eDMA modules and then any
processing of data that is required to prepare it for transmission or interpret it after
reception.
->
->
65
Configuration example
/* Save off the current value of the UARTx_C4 register except for the BRFA field */
u8Temp = pUART -> C4 & ~(UART_C4_BRFA(0x1F));
pUART -> C4 = u8Temp |
UART_C4_BRFA(u16Brfa);
Because this is a polled implementation, the function will wait until a character is
received. If no character is received, then the code will remain in the while loop
indefinitely. To avoid code getting stuck when no traffic is being received, include a
function to test whether a character is present. The uart_getchar_present function can be
called prior to calling the uart_getchar function in cases when UART receive traffic is not
guaranteed or required before moving on with program execution.
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
66
67
Using the DMA to move data can help to decrease the CPU loading even more than using
the UART interrupts. The UART's same RDRF and TDRE flags used for an interruptdriven software approach can be re-routed to the eDMA controller instead. This is done
by setting the UARTx_C5[TDMAS, RDMAS] bits. Each of these requests would be
routed to a different eDMA channel (the specific eDMA channels would be selected by
programming the DMA channel mux). One eDMA channel would be responsible for
handling receive traffic, so it would read one or more bytes from the UART for each
request. The second eDMA channel would be responsible for handling the transmit
traffic, so it would write one or more bytes to the UART for each request. When the
entire transmit or receive DMA movement is complete the eDMA can interrupt the core
to notify it of the completion. In this approach the CPU has no loading associated with
the actual data movement. All of the CPU loading is the result of the initial configuration
of both the UART and eDMA modules and then any processing of data that is required to
prepare it for transmission or interpret it after reception.
Chapter 8
Memory-Mapped Divide and Square Root (MMDVSQ)
8.1 Introduction
The ARM v6M instruction set architecture (ISA) implemented on the Cortex-M0+ core
does not support integer divide instructions or square root functions. However, these
arithmetic operations are widely used in motor control applications. To maximize such
system performance and minimize the device power dissipation, the co-processor
MMDVSQ is included in Kinetis V series to support execution of the integer divide and
unsigned integer square root operations. The supported integer divide operations include
32/32 signed (SDIV) and unsigned (UDIV) calculation.
8.2 Features
The supported features of the MMDVSQ are as follows:
Lightweight implementation of 32-bit integer divide and square root arithmetic
operations
Supports 32/32 signed and unsigned divide (or remainder) calculations
Supports 32-bit unsigned square root calculations
Simple programming model includes input data and result registers plus a control/
status register
Programming model interface optimized for activation from inline code or software
library call
Fast Start configuration minimizes the memory-mapped register write
overhead
Supports two methods to determine when result is valid, including software
polling
Configurable divide-by-zero response
69
Dbg
NVIC
AGU
Dec
Rn
LD/ST
SHFT
ALU
MUL
AHB Bus
IO Port
MTB Port
RGPIO
32
PRAM
RAM
Array
MTB
MCM
s1
m0
MMDVSQ
AXBS
-Lite
Alt-Master
DMA_4ch
32
m3
s2
m2
s0
BME
PBRIDGE
32
FMC
Slave
Peripherals
NVM
Array
All functionality associated with the MMDVSQ module resides in the core platforms
clock domain. The MMDVSQ is only clocked when responding to bus requests to its
programming model or is busy performing a calculation. This minimizes system power
dissipation.
MMDVSQ_DEND (dividend)
MMDVSQ_DSOR (divisor))
MMDVSQ_RCND (radicand))
MMDVSQ_RES (result))
MMDVSQ_CSR (control/status))
These registers can only be accessed via word-sized (32-bit) accessed. At any instant in
time, the MMDVSQ can perform either a divider or square root calculation.
For divide, the specific operation to be performed according to the following table:
Table 8-1. Determining Divide Specific Operations
MMDVSQ_CSR[REM]
MMDVSQ_CSR[USGN]
Signed divide
MMDVSQ_RES = quotient
(MMDVSQ_DEND/MMDVSQ_DSOR);
Unsigned divide
MMDVSQ_RES = quotient
(MMDVSQ_DEND/MMDVSQ_DSOR);
Signed divide
MMDVSQ_RES = remainder
(MMDVSQ_DEND%MMDVSQ_DSOR);
Unsigned divide
MMDVSQ_RES = remainder
(MMDVSQ_DEND%MMDVSQ_DSOR);
71
The following code shows how to do signed divide and get remainder in u32Result:
MMDVSQ -> CSR = (MMDVSQ CSR & ~(MMDVSQ_CSR_USGN_MASK)) | MMDVSQ_CSR_REM_MASK;
MMDVSQ -> DEND = i32Numerator;
MMDVSQ -> DSOR = i32Denominator;
i32Result = MMDVSQ -> RES;
The following code shows how to get the square root of u32Data in u16Result:
MMDVSQ -> RCND = u32Data;
u16Result = MMDVSQ -> RES;
73
Tips of usage
Chapter 9
Analog-to-Digital Converter (ADC)
9.1 Overview
Each sub-family of the Kinetis V series may have a different ADC module. This chapter
describes the ADC module used in KV1x sub-family. The ADC module is a SAR type
with up to 16-bit resolution. It has two independent ADCs, ADC0 and ADC1. The ADC
module supports up to 4 pairs of differential inputs and up to 24 single-ended inputs and
features a self-calibration mode, automatic compare, and hardware average. The ADCx
(x=0, 1) has two conversion result registers, ADCx_RA and ADCx_RB, and completes
4-channel samples using a trigger event.
9.2 Introduction
The ADC module can be configured to single-end mode or differential mode. The Output
is right-justified unsigned format for single-ended modes, and it is 2's complement 16-bit
sign extended for differential modes. The single mode has 16-bit, 12-bit, 10-bit and 8-bit.
The differential mode includes 16-bit, 13-bit, 11-bit and 9-bit. The MSB is symbol
placement, 1 is negative, and 0 is positive.
Each ADC has 12 external channels from ADCx_SE0 to ADC_SE11 in single-end mode,
and ADC0 can be configured 2 pairs differential mode ADC0_DP0 and ADC0_DM0
and ADC0_DP1 ADC0_DM1, and ADC1 can be configured 2 pairs differential mode
ADC1_DP1 and ADC1_DM1 and ADC1_DP2 ADC0_DM2. There is internal
temperature sensor, Bandgap, VREFH and VREFL channel, it is convenient to test
junction or ambient temperature using integral temperature sensor and it can remove
temperature drift error in reference via internal reference channel at any time.
ADC has self-calibration function, the user has to calibrate ADC at beginning of the
conversion. Calibration function can remove the offset effectively.
75
Features
ADC has hardware average function, it can be set to 4, 8,16 and 32 samples average.
Automatic compare function has less-than, greater-than or equal-to, within range, or outof-range, programmable value.
ADC has low power mode for lower noise application, and long sample selection is good
choice for low speed and high accuracy operation. ADC also can be configured to high
speed for fast conversion like PMSM motor control application.
ADC has hardware trigger and software trigger mode, ADC can be configured to
continuous conversion via only one trigger signal.
ADC supports DMA operations, the conversion flag can trigger DMA transfer when the
DMA function is enabled.
9.3 Features
3. Configure ADCx_CFG1 and ADCx_CFG2. Select input clock source, divider, ADC
conversion mode, long sample time configuration. Once configure ADCx_CFG1
register, the user has to update ADCx_CFG2 register according to ADCx_CFG1
configuration.
4. Configure ADCx_SC2 and ADCx_SC3. Select trig mode, compare function and,
reference voltage and average function, etc. if compare function is enable,
ADCx_CVn register should be correct value.
5. Configure ADCx_SC1n, set up differential or interrupt control bit.
6. Write channel number in ADCx_SC1n. If software trigger is enable, it will kick off
ADC conversion.
7. Check COCO conversion complete flag.
8. Read ADCx_Rn.
The hardware trigger is from PDB module, so the user has to configure PDB module if
hardware is enable. The ADC configuration should be prior to PDB and peripheral. For
example, if the users want to use FTM channel flag to trig ADC, the user needs to
configure ADC firstly, secondly configure PDB, FTM is configured at last. It is order to
do not neglect the trigger when ADC or PDB is not ready.
77
Use high quality RC components for the anti-aliasing filter. Place this RC filter as
close to the ADC input pins as possible where it can remove the most noise.
Provide very stable analog ground and voltage planes, both for analog power and
voltage references if full accuracy of the ADC is required.
Note
When building an external voltage divider on the ADC input
pin to sample high voltage, choose the voltage divider with the
lowest equivalent resistor value, because the ADC input
resistance is only 5K .
Chapter 10
Programmable Delay Block (PDB)
10.1 Overview
The Programmable Delay Block (PDB) is interconnected with the ADC, CMP, and
FTMs. The PDB has a pre-trigger function for ADC and provides an interval interrupt as
a 12-bit DAC hardware trigger. Individual PDB pulse-out signals are connected to each
CMP block and used for the sample window. The PDB channel 1 pre-trigger can also be
used as an FTM synchronous input.
10.2 Introduction
The PDB provides the programmable delay time for trigger input source, the PDB
interval trigger provides the hardware trigger for 12-bit DAC, and the PDB pulse out for
CMP module. The PDB has pre-trigger and back-to-back operations. When pre-trigger is
not enabled, the PDB works under bypass mode.
There are two channels in the PDB. Each channel is associated with one ADC, for
example, channel 0 is assigned to ADC0, and channel 1 is assigned to ADC1. The PDB
can be configured to pre-trigger mode and bypass mode. One PDB channel can be
configured as two pre-triggers, pre-trigger0 and pre-trigger1, when the input trigger event
is coming, the PDB starts to run, when the PDB counter (the value in PCBx_CNT)
matches the channel delay (the value in PDB_CHnDLY0 or PDB_CHnDLY1), the
channel flag will be set, and the trigger can be used as ADC hardware trigger. If the pretrigger is disabled, it works in bypass mode, the input trigger source of PDB can be
connected to ADC directly.
The PDB pre-trigger back-to-back mode configures the PDB pre-triggers as a single
chain or several chains. PDB back-to-back operation acknowledgment connections are
implemented as follows:
PDB channel 0 pre-trigger 0 acknowledgement input: ADC1SC1B_COCO
79
Features
10.3 Features
The programmable delay block features the following:
Input hardware trigger sources and one of software trigger source, the user can find
PDB input trigger source from chip configuration on KV10 reference manual.
KV10 has two configurable PDB channels for each ADC.
One PDB channel is associated with one ADC, one trigger output for ADC hardware
trigger and has two pre-trigger outputs for ADC trigger select per PDB channel.
16-bit delay register per pre-trigger output Trigger outputs, it can be enabled or
disabled independently, and it also support bypass of the delay registers of the pretrigger outputs, the input trigger event is served to ADC directly.
PDB can works under One-Shot or Continuous modes.
Optional back-to-back mode operation, which enables the ADC conversions
complete to trigger the next PDB channel, there are channel delay interrupt and
channel sequence error interrupt and its flag.
One 16-bit resolution interval trigger output per DAC, optional bypass of the delay
interval trigger registers, DAC also has external trigger from ADC0_SC1A[COCO].
2 channel pulse outputs for CMP0 and CMP1. Pulse-out's can be enabled or disabled
independently, and the pulse width is programmable via PDBx_POnDLY register.
DMA support.
81
Ping-Pong operation
For example, there are two channel input signals. You can choose between PDB channel
0 or channel 1. To begin, set the channel delay time, CHnDLY0, and then enable the
channel pre-trigger and its output. The ping-pong operation will be completed by the
PDB pre-trigger output source.
83
Ping-Pong operation
Chapter 11
Using FlexTimer (FTM) via Programmable Delay
Block (PDB) to Schedule ADC Conversion
11.1 Overview
This chapter demonstrates how to use the PDB module to schedule and perform ADC
conversions via FTM period trigger input, the ADCs works under 16-bit single-end
mode, ADC0 samples the voltage on the ADC0_SE1(PTE16), and channel
ADC0_SE5(PTE17), and ADC1 samples the voltage ADC1_SE1(PTE18) and
ADC1_SE7(PTE18) on TWR-KV10Z32 board. And print the buffered 256 set
conversion result via CDC virtual serial port of OpenSDA.
The code example shows how to:
11.2 Introduction
Timing of ADC conversions relative to system events is a key to applications, such as
motor control, and metering, requiring timing of ADC conversions for the best time to get
a noise-reduced reading. When the Kinetis V MCU is acting as a controller, it will output
control changes from time to time. Scheduling ADC conversions around these changes,
which may make transient disturbances in the system, is key. Scheduling the ADC
conversions at a time after the transient effects of the last control change has been made
can enable smooth operation of control loops. The PDB allows simple scheduling of one
or both of the ADC peripherals conversions. In this example, both ADCs will be
scheduled.
85
Features
11.3 Features
The ADC features demonstrated by the PWM_PDB_ADC_Lab example code include:
Simple calibration of the ADC:
A simple driver for the ADC, which facilitates using both ADCs and their calibration
with minimal software, is included in the adc_demo example code. Prior to taking
the first measurement, during the initialization of the demo project the ADC will be
calibrated. The use of the driver of the ADC will simplify this. While the ADC can
be used prior to calibration for conversions, the calibration of the ADC enables it to
meet its specifications.
Averaging by 1, 4, 8, 16, or 32:
The ADCs ability to average up to thirty-two conversion values prior to ending the
conversion process and generating a result will be demonstrated. This feature reduces
CPU load; it also reduces the effect of a noise spike on any readings. It is a simple
arithmetic averaging of thirty-two (or less if so configured) ADC conversions. These
conversions are taken upon the PDB triggering the ADC.
The ADCs interrupts:
The interrupt feature of the ADC is also used in the example. The ADC conversion
result is read from ADCx_Rn registers in the Interrupt Service of ADC0 and ADC1.
Hardware triggering of the ADC with the FTM via PDB:
The ADC works with the FTM to trigger the ADCs conversions. Determining which
ADC trigger to convert is based on your configuration choices. In this case the ADC
will be configured to be triggered only by the FTM channel flag via PDB. The pretrigger works under back-to-back mode.
16-bit resolution:
The conversion results in this example are 16 bit unsigned.
Differential or single-ended:
Single-ended mode is illustrated in this example.
Chapter 11 Using FlexTimer (FTM) via Programmable Delay Block (PDB) to Schedule ADC Conversion
87
Configuration code
/*!<write
/*!<write
/*!<write
/*!<write
ADC0
ADC0
ADC1
ADC1
channel
channel
channel
channel
1*/
5*/
1*/
7*/
Chapter 11 Using FlexTimer (FTM) via Programmable Delay Block (PDB) to Schedule ADC Conversion
mode*/
/* set MOD value */
FTM_SetModValue(FTM0, 4678); /*!<set FTM period is 16KHz, the modulo value is equal
(75000/16)-1 */
FTM_PWMDeadtimeSet(FTM0, 2, 0x13);/*!<set channln and channel n-1 dead time, 1us*/
/* set clock source and start the counter */
FTM_ClockSet(FTM0, FTM_CLOCK_SYSTEMCLOCK, FTM_CLOCK_PS_DIV1);
/* set the duty cycle, note: only fit for combine mode */
FTM_SetDutyCycleCombine(FTM0, FTM_CHANNEL_CHANNEL1, 50);
FTM_SetDutyCycleCombine(FTM0, FTM_CHANNEL_CHANNEL3, 50);
FTM_SetDutyCycleCombine(FTM0, FTM_CHANNEL_CHANNEL5, 50);
FTM0->EXTTRIG = 0x10;
/*!<trigger source is from FTM0 channel 0 trigger */
/* read ADC conversion u8Result */
while(u8CycleTimes<255);
/*!< wait for u8Result0A[i],u8Result0B[i],u8Result1A[i] and
u8Result1B[i] is full
*/
for(i=0;i<256;i++)
/*!< print adc conversion result. */
{
printf("%d, %d, %d, %d\n",u8Result0A[i],u8Result0B[i],u8Result1A[i],u8Result1B[i]);
}
printf("Please enter any character which will echo...\n");
/* echo chars received from terminal */
while(1)
{
u8Ch = UART_GetChar(TERM_PORT);
UART_PutChar(TERM_PORT, u8Ch);
}
}
/*****************************************************************************//*!
*
* @brief ADC1 module task
*
89
Configuration code
* @param none
*
* @return none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/
void ADC1_Task(void)
{
if((ADC1->SC1[0] & ADC_SC1_COCO_MASK)==ADC_SC1_COCO_MASK)
{
u8Result1A[u8CycleTimes] = ADC1->R[0];
}
if((ADC1->SC1[1] & ADC_SC1_COCO_MASK)==ADC_SC1_COCO_MASK)
{
u8Result1B[u8CycleTimes] = ADC1->R[1];
u8CycleTimes++;
}
}
Chapter 12
FlexTimer Module (FTM)
12.1 Overview
This chapter will demonstrate the features of the FlexTimer Module (FTM) of Kinetis V
series devices. It also presents examples of how to properly configure the module to
achieve its required operational mode. One of the examples included in this chapter
utilizes two different modes of FTM operation: input capture and PWM mode. Another
example mentions the PWM functionality of FTM working in very low power stop mode
(VLPS).
12.2 Introduction
The FTM is built on the base of TPM module well known from Freescale 8-bit
microcontrollers. The FTM extends the functionality to meet the demands of motor
control, digital lighting solutions, and power conversion, while providing low cost and
backwards compatibility with the TPM module.
12.3 Features
The features of FTM on all Kinetis V series devices can vary in the number of FTMs and
the channels included in each module. The FTM functionality can be summarized into six
basic modes of operation:
Input capture
Output compare
PWM
Deadtime insertion
Quadrature decoder
Fault control
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
91
Features
In an output compare mode, set, clear, or toggle of output on match can be achieved. This
mode also allows the generation of positive or negative pulses on the match event. This
event occurs on compare match of FTMx_CNT and FTMx_CnV.
The most useful feature of the FTM is pulse width modulation (PWM). Edge- (up
counting) and center- (up-down counting) aligned PWM are available in this timer
module via FTMx_SC[CPWMS]. Both modes of operation provide positive (high true) or
negative (low true) PWM pulse generation. The channel pulse width is a proportional part
of modulo value and is defined by FTMx_CnV. Combine mode is not available in this
module.
93
Configuration examples
out
in
out
in
FTM0
FTM1
Before configuring both FTM modules, SIM must configure all required clock options. In
this example, system clock is used as a source clock for FTM. Therefore, it is necessary
to set FTMx_SC.CLKS[1:0] to 1. Next, the clocks for the ports whose pins will be used
must also be enabled. In this example, pin PTC2 and PTC3 are used as channel outputs of
FTM0, and PTD6 and PTD7 are used as channel inputs of FTM1. Therefore, clocks for
PORTC must be enabled. In the end, the clock gates must be enabled for both of the
FTMs used. Follow the next few lines of code with required SIM module configuration.
/* Enable PORTC/D clocks */
SIM->SCGC5 |= SIM_SCGC5_PORTC_MASK | (SIM_SCGC5_PORTD_MASK;
/* Enable FTM0/1 clocks */
SIM->SCGC6 |= SIM_SCGC6_FTM0_MASK;
SIM->SCGC6 |= SIM_SCGC6_FTM1_MASK;
NOTE
If any additional clock settings are required in your application,
they must also be implemented in SIM configuration.
After SIM initialization, required port pins must be configured according to their use. It is
necessary to clear only interrupt status flag, select an alternative pin for the FTM channel,
and for outputs you can enable drive strength.
/* Enable the FTM0 Ch1/2 function on PTC2/3 */
PORTC->PCR[2] = PORT_PCR_MUX(0x4);
PORTC->PCR[3] = PORT_PCR_MUX(0x4);
/* Enable the FTM1 Ch0/1 function on PTD6/7*/
PORTD->PCR[6] = PORT_PCR_MUX(0x5);
PORTD->PCR[7] = PORT_PCR_MUX(0x5);
You can also redefine interrupt vector to your interrupt service routine. This must be
done in a different module, such as isr.h.
extern void FTM0_Isr(void);
#undef VECTOR_033
#define VECTOR_033
FTM0_Isr
95
Configuration examples
Then FTM0 module can be initialized. FTM0_CONF should stay in its default state. For
best performance, initialize the counter, that is, write to FTM0_CNT, before writing to
the modulo register. Modulo value MOD is set to 4800 to generate a PWM signal with
10 kHz,assuming that the system clock is set to 48 MHz. Then FTM0_SC will enable
timer overflow interrupt, set edge-aligned (up counting) mode, select clock mode to allow
the counter to increment on every clock, and set the prescaler divider to 1.
/* update MOD value */
FTM_SetModValue(FTM0, 4800);
/* set clock source, start counter */
FTM_ClockSet(FTM0, FTM_CLOCK_SYSTEMCLOCK, FTM_CLOCK_PS_DIV1);
Channels configuration must be done after general FTM0 module configuration. Channel
1 of FTM0 is configured as edge-aligned PWM with high-true pulses (positive PWM
pulses). Channel 2 is configured as edge-aligned PWM with low-true pulses (negative
pulses). Channel interrupt is disabled. Channels value must also be initialized.
pFTM->CONTROLS[1].CnSC
pFTM->CONTROLS[1].CnV
pFTM->CONTROLS[2].CnSC
pFTM->CONTROLS[2].CnV
=
=
=
=
FTM_CnSC_MSB_MASK | FTM_CnSC_ELSB_MASK;
0;
FTM_CnSC_MSB_MASK | FTM_CnSC_ELSA_MASK;
0;
NOTE
Both channels of the FTM are configured in inverse PWM to
demonstrate how one leg of the three phase converter used in
motor control application can be configured. Also note that
dead time control is available in the Kinetis V series FTM.
This module generates interrupt on timer overflow. It is convenient to clear the
corresponding flag in interrupt service routine. Value register is also set.
void FTM0_Isr(void)
{
/* clear the flag */
FTM_ClrOverFlowFlag(FTM0);
NOTE
Setting the compare value in the timer overflow interrupt
service routine of the same FTM is not a best practice. CnV
registers are updated by their buffer value immediately after
timer overflow. Therefore, one period of FTM can be lost in
such a case. Instead, set compare value immediately before
timer overflow.
FTM1_Isr
Because pulse width measurement uses FTM1, it is desirable to trigger start of FTM1
counting by FTP0 overflow. Therefore, FTM0_CONF sets input trigger to TMP0
overflow and enables counter start on trigger. For more information, see Figure 13-2.
Counter is set to stop on overflow and input trigger provides its restart. The rest of the
configuration is similar to FTM0, except modulo value, which can be set higher. In this
example, it was set to 11200, assuming FTM1 period is 4285 Hz.
FTM_ClockSet(FTM1, FTM_CLOCK_SYSTEMCLOCK, FTM_CLOCK_PS_DIV1);
FTM_EnableChannelInt(FTM1, 1);
FTM1 also generates interrupt on time overflow. In the interrupt service routine, the
timer overflow flag is cleared and pulse width is calculated from the rising and falling
edges values of channels. Channel flags must also be cleared in to be able to capture
edges in the next FTM1 period.
void FTM1_Isr(void)
{
FTM_ClrChannelFlag(FTM0, FTM_CHANNELPAIR0);
FTM_ClrChannelFlag(FTM0, FTM_CHANNELPAIR0+1);
u8IntMark = 1;
}
NOTE
When a different input signal will supply FTM1 inputs, then it
is recommended to increase modulo value to 65535 to reach the
highest possible resolution of pulse width measurement. Also
note that if the result of pulse width is negative, it should not be
taken into account. In this case, rising edge (FTM1_C0V) was
captured two periods before and falling edge (FTM1_C1V) was
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
Freescale Semiconductor, Inc.
97
Configuration examples
FTM0
FTM0
TOF
PM0
FTM0
ISR
FTM0
TOF
FTM0
TOF
FTM0
ISR
FTM0
ISR
FTM0
TOF
FTM0
ISR
FTM0
TOF
FTM0
TOF
FTM0
ISR
FTM0
TOF
FTM0
ISR
FTM0
ISR
FTM0
TOF
FTM0
ISR
CH1
out
CH2
out
CSOO
FTM1
CSOO
CSOO
FE
FE
RE
CSOT RE
RE
FE
CSOT
t
FTM1
TOF
FTM1
ISR
FTM1
TOF
FTM1
TOF
FTM1
ISR
FTM1
ISR
Figure 12-2. Functional description of example (RE rising edge, FE falling edge,
CSOO counter stop on overflow, CSOT counter start on trigger)
Appendix A
How to Load QRUG Examples
A.1 Overview
This chapter describes how to load and run the sample code described in other sections of
the Kinetis V series Quick Reference User Guide. It describes the procedures used to
ensure your Tower system or Freedom board is connected properly, and explains how to
load the example projects.
99
Terminal configuration
When you plug in the USB cable to your board, you will see LEDs on the board turn on.
Open IAR and go to File -> Open -> Workspace in the menu bar.
Open the Blinky_green workspace at kvxx-drv-lib\build\iar\kv10\Blinky_green\.
The workspace that opens contains a Blink_green project for the TWR-KV10Z.
There are two flash combinations available in the demo which this project supports;
FLASH_32KB and FLASH_16KB. Select FLASH_32KB for the TWR_KV10
board.
Kinetis V Series Peripheral Module Quick Reference, Rev. 0, 02/2014
100
101
9. Download the code to the board and start the debugger by pressing the Download
and Debug button.
10. The code will download into flash. The debugger screen will appear and pause at the
first instruction. Click the Go button to start running.
11. After you select Go, the software will print out information and indicate to input any
character which will echo, meanwhile the green LED (D1,D3,D5) begins to blink.
Running the Blinky_green project.
Please enter any character which will echo...