lab 5: interrupts, timing, and frequency analysis of pwm signals€¦ · using interrupts •run...

Post on 17-Jun-2020

3 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

1

Lab 5: Interrupts, Timing, andFrequency Analysis of PWM Signals

Fall 2019

2

Lab 5: Interrupts and Timing• Thus far, we have not worried about time in

our real-time code– Sampling an input signal or generating a periodic

output requires that your code run periodically

• We will use interrupts to create code that executes at a specified periodic rate– Application code stops what it�s doing and control

transfers to an interrupt service routine (ISR); ISR may sample a signal (using the ADC, for example) or generate a signal (using the FlexTimer, for example)

3

Interrupt Service

IRQ

Save processor stateand jump to ISR

Last ISR instruction is �return from interrupt.�Restore processor state

and jump back

Program Instruction

Program Instruction

Program Instruction

Program Instruction

Interrupt Service Routine

ISR Instruction

ISR Instruction

ISR Instruction

rfi

Note: Interrupts are (usually) disabled during execution of the ISR. What could happen if the ISR takes too long?

4

Lab 5: Basic Idea (see Lecture 6)• Generate a sine wave which

will be periodically sampled– Signal generator– �C� sine function– Look-up table

• Create a PWM signal with duty cycle equivalent to the sampled value (0-100% in our example)

• Appropriately filter the modulated PWM signal to recover the sine wave

• What�s the point?– Learn to use the Low PowerInterrupt Timer (LPIT)– RT S/W overhead issues– Demonstrate response of

lowpass HW filter to PWM input

SignalGenerator S32K144

Sine Wave

ModulatedPWM

5

Lab 5: Basic Idea (see Lecture 6)• Suppose we sample 0.1

Hz sine at 0.1 second intervals and generate a 10 Hz PWM (we�ll use much higher frequency and faster sampling in the lab)

• Frequency spectrum of PWM signal has components at +/- 0.1 Hz, and multiples of the 10 Hz switching frequency (after removing the DC component from the signal)

6

Lab 5: Basic Idea (see Lecture 6)

• Now all we have to do is low-pass filter the high frequency components of our signal to reconstruct the original sine wave

• Filter with unity gain at 0.1 Hz; very small gain at 10 Hz

S32K144 Interrupt Controller• Nested Vector Interrupt Controller (NVIC)

– Section 7.2 of the S32K144 Reference Manual.– Chapter 6 of ARM® Cortex®-M4 Processor Reference Manual.

• Supports up to 240 vectored interrupts with 16 programmable priority levels.– Table 2-1 of S32K144 Reference Manual.– S32K1xx_DMA_Interrupt_mapping.xlsx– A lower number means higher priority.

• We will use the S32K144 Low Power Interrupt Timer (LPIT) to generate periodic interrupt requests (IRQ).– Chapter 46 of the S32K144 Reference Manual.– Use to complete LPIT_template.c.

7

8

Low Power Interrupt Timer (LPIT)• The S32K144 has one LPIT module, LPIT0, with 4

channels (channels 0 – 3)• The spreadsheet S32K1xx_DMA_Interrupt_mapping.xlsxmatches LPIT channels 0 – 3 to Interrupt IDs.

• Each channel has a 32 bit timer that counts down from its initial value.

• When it reaches zero, an interrupt flag is set and the timer reloads its initial value and resumes counting

9

LPIT Frequency and Initial Value

• The frequency of the periodic interrupt is determined by the LPIT clock frequency and the counter initial value.

• The LPIT clock frequency is 40 MHz.

• Interrupt period must be an integer multiple of the LPIT clock period; call the number “counts”– interrupt period = counts*system clock

– counts = interrupt period/system clock

= system clock frequency/interrupt frequency

• Note: integer divide truncates, “counts” is an integer

• Careful! Timer counts down to zero, hence– timer initial value = counts -1

10

Lab 5: Software

• Use I/O software you developed in labs 3 and 4– adc.h and adc.c

• Read the input sine wave

– pwm.h and pwm.c• Generate the PWM signal

• Code required to initialize the LPIT and set up interrupt service routines– LPIT.h and LPIT.c (complete LPIT_template.c)

• Three ISRs required– Read duty cycle from signal generator

– Calculate duty cycle using C function

– Calculate duty cycle using look-up table

LPIT Initialization

• Several registers used with the LPIT module, including:– Peripheral Clock Controller (PCC)– Module Control Register (MCR)– Module Status Register (MSR)– Module Interrupt Enable Register (MIER)– Set Timer Enable Register (SETTEN)– Timer Value Register (TVAL0)– Timer Control Register (TCTRL0)

• Complete 3 functions in LPIT_template.c– enableLPIT()– initLPIT()– clearFlagLPIT()

11

Using Interrupts

• Run your ISR code by calling

12

initLPIT(LPIT0_CHANNEL, frequency, &IsrName, 0xC);

- LPIT0_channel is the LPIT module channel (= 0)

- frequency is the desired ISR frequency

- IsrName is the name of the ISR

- 0xC is the interrupt priority (set arbitrarily to 12)

•Note: at the end of every interrupt routine you will

write in lab5.c, you must clear the timer interrupt

flag by calling

- clearFlagLPIT(LPIT0_CHANNEL)

Configure the PCC LPIT Register

• Section 29.6.15

– Clear the CGC bit to disable the clock– Choose the PCS bitfield to select clock option 6– Set the CGC bit to enable the clock

13

Peripheral Clocking

• Clock option 6– SPLLDIV2_CLK (40 MHz – see eecs461.h)

14

LPIT Configuration in S32K144.h15

/** LPIT - Register Layout Typedef */typedef struct {

__I uint32_t VERID; __I uint32_t PARAM; __IO uint32_t MCR; __IO uint32_t MSR; __IO uint32_t MIER; __IO uint32_t SETTEN; __IO uint32_t CLRTEN;

uint8_t RESERVED_0[4];struct {

__IO uint32_t TVAL; __I uint32_t CVAL; __IO uint32_t TCTRL;

uint8_t RESERVED_0[4];} TMR[LPIT_TMR_COUNT];

} LPIT_Type, *LPIT_MemMapPtr;

• LPIT may be configured using struct and #define items in NXP supplied header file

Module Control Register (MCR)16

• M_CEN bitfield enables the clock to the LPIT. Must be set in order toaccess other configuration registers.

Module Status Register (MSR)17

• TIFn: channel n timer interrupt flag. - Set to 0 while timer is counting down and to 1 when it reaches 0. - To clear the TIFn bit, write a 1 to it.- A new interrupt on channel n cannot be generated until the TIFn flag is cleared.

Module Interrupt Enable Register (MIER)18

• Interrupt generation is enabled when the TIEn bitfield is set and the TIFnflag is cleared.

Set Timer Enable Register (SETTEN)19

• Channel n timer is enabled by setting the SET_T_EN_n bitfield to 1. - has the same effect as setting the T_EN bitfield of the TCTRLn register.

Timer Value Register (TVAL0)20

• TMR_VAL is the 32-bit initial value for timer channel 0. The timer countsdown until it reaches 0, then generates an interrupt and loads theinitial value and resumes counting down again.

Timer Control Register (TCTRL0)21

• Timer LPT0 is either controlled by an external trigger (which we do not use)or a change in the T_EN bit.

• T_EN: enables (1) or disables (0) the channel timer.• MODE: configures channel timer mode – 32-bit periodic counter.• TSOT: if set to 0, channel timer starts on a rising edge of the T_EN bit.• TSOI: if set to 0, channel timer does not stop after timeout.• TROT: if set to 0, ignores external trigger (which we do not use).

22

isrA: Read Duty Cycle from Signal Generator

• See lab assignment for details• ISR frequency: 20 kHz• Sine wave: 1 kHz, 1 to 4 volts, external input• PWM:

– 20 kHz and 60 kHz frequency (DIP selectable)– Duty cycle proportional to voltage input

• Procedure:– Turn on LED 0.– Read AN0 analog input– Calculate duty cycle– Set the PWM duty cycle– Turn off LED 0.– Clear LPIT Interrupt Flag

23

isrB: Calculate Duty Cycle from sin()

• ISR frequency: 1 kHz

• Sine wave: 100 Hz, calculated by sin() function

• PWM: 60 kHz, 10% to 90% duty cycle

• Procedure:

– Turn on LED 0.

– Calculate sin( 2*pi * i / 10 ), i.e., 10 times per period,

hence i is incremented by 1 each invocation.

– Set the PWM duty cycle

– Turn off LED 0.

• Compare doubles (64 bit) and floats (32 bit). Type

conversions: p. 198 Kernighan and Ritchie.

Note: sinf() vs. sin() & 12.34f vs 12.34

24

isrC: Calculate Duty Cycle Table Look-up

• See lab assignment for details• Essentially the same as isrB, except

pre-calculate sin() and store as a look-up table

• What’s the advantage?

25

C Trig Function Libraries

• Compiler dependent• Tradeoff between execution speed and

accuracy• Several ways to compute sin(x), including:

– if x is small, sin(x) ≈ x– Taylor series, sin(x) = x - x3/3! + x5/5!-… (slow

convergence)– polynomial approximation– interpolate from look-up table– combination– http://stackoverflow.com/questions/2284860– http://www.ganssle.com/approx.htm

top related