TMS320F2812 - Event Manager
TMS320F2812 - Event Manager
Introduction
Now it is time to discuss one of the most powerful hardware modules of the C28x, called the Event Manager (EV). An EV is a unit that is able to deal with different types of time-based procedures. The core of this manager is a little bit similar to the DSPs core timer units Timer 0, 1 and 2. Although the Event Manager Timer units are also called "Timer 1, 2, 3 and 4", these timers are totally independent of the three core timers. So please do not mix them up! From now, when we speak of a timer unit, we have to clarify if it is a Core timer or an Event Manager timer! The Event Manager Timer unit is a 16-bit counter/timer unit, whereas a Core Timer is a 32-bit register. The most important difference between the Event Manager and the Core timers is its input/output system. An EV is able to produce hardware signals directly from an internal time event. Thus this unit is most often used to generate time based digital hardware signals. This signal is a digital pulse with binary amplitude (0, 1). With the help of the EV-logic we can modify the frequency and/or the pulse width of these output signals. When we apply an internal control scheme to modify the shape of the signals during run time, we call this Pulse Width Modulation (PWM). PWM is used for two main purposes: Digital Motor Control (DMC) Analogue Voltage Generator
We will discuss these two main areas a little bit later. The C28x is able to generate up to 16 PWM output signals. The Event Manager is also able to perform time measurements based on hardware signals. With the help of 6 edge detectors, called Capture Units we can measure the time difference between two hardware signals to determine the speed of a rotating shaft in rotations per minute. The third part of the Event Manager is called Quadrature Encoder Pulse unit (QEP). This is a unit that is used to derive the speed and direction information of a rotating shaft directly from hardware signals from incremental encoders or resolvers. The C28x is equipped with two Event Managers, called EVA and EVB. These are two identical hardware units; two 16-bit timers within each of these EVs generate the time base for all internal operations. In case of EVA the timers are called General Purpose Timer T1 and T2, in case of EVB they are called T3 and T4. This module includes also two lab-exercises Lab5 and Lab5A based on the eZdsp and the Zwickau Adapter board. To perform Lab5A you will need a simple analogue oscilloscope.
5-1
Module Topics
Module Topics
C28x Event Manager..................................................................................................................................5-1 Introduction .............................................................................................................................................5-1 Module Topics..........................................................................................................................................5-2 Event Manager Block Diagram ...............................................................................................................5-3 General Purpose Timer............................................................................................................................5-4 Timer Operating Modes ...........................................................................................................................5-5 Interrupt Sources .....................................................................................................................................5-6 GP Timer Registers..................................................................................................................................5-7 GP Timer Interrupts...............................................................................................................................5-12 Lab 5: Lets play a tune! ........................................................................................................................5-14 Event Manager Compare Units .............................................................................................................5-20 Capture Units.........................................................................................................................................5-31 Quadrature Encoder Pulse Unit (QEP) .................................................................................................5-36 Lab 5A: Generate a PWM sine wave .....................................................................................................5-39 Optional Exercise...................................................................................................................................5-50
5-2
EV Control Registers / Logic GP Timer 1 Compare GP Timer 1 Data Bus Compare Unit 1 Compare Unit 2 Compare Unit 3 GP Timer 2 Compare GP Timer 2 MUX Capture Units
Output Logic
PWM Circuits Output Logic PWM Circuits Output Logic PWM Circuits Output Logic Output Logic
CLK DIR
QEP Circuit
CAP1/QEP1 CAP2/QEP2 CAP3/QEPI1
5-2
5-3
Clock Prescaler
M U X
Compare Register
GPTCONA
Output Logic
TxPWM_ TxCMP
Note: x = 1 or 2
5-4
Another unique feature of the C28x is its shadow functionality of operating registers, in the case of GP Timers 1 and 2 available for compare register and period register. For some applications it is necessary to modify the values inside a compare or period register every period. The advantage of the background registers is that we can prepare the values for the next period in the previous one. Without a background function we would have to wait for the end of the current period, and then trigger a high priorized interrupt. Sometimes this principle will miss its deadline
5-4
This example: TxCON.3-2 = 00 (reload TxCMPR on underflow) TxCON.3TxPR = 3 Seamless counting continues TxCMPR = 1 (initially) Up count period is TxPR+1 Prescale = 1
CPU writes a 2 to compare reg. buffer anytime here TxCMPR=2 is loaded here TxCMPR=2 3 2 1 1 0 0 2 1 0 3 2 3
5-5
The slides give two examples of the two most used operating modes. Note: There are two more modes see data sheet.
This example: TxCON.3-2 = 01 (reload TxCMPR on underflow or period match) TxCON.3TxPR = 3 Seamless up/down repetition TxCMPR = 1 (initially) Up/down count period is 2*TxPR 2*TxPR Prescale = 1
TxCMPR loads with a 1 3 2 1 2 1 0 1 2 TxCMPR loads with a 2 3 2 1 0 TxCMPR loads with a 1
5-6
5-5
Interrupt Sources
Interrupt Sources
Each of the two timers of Event Manager A (EVA) is able to generate four types of interrupt requests: timer underflow (counter equals zero), timer compare (counter equals compare register), timer period (counter equals period register) and timer overflow (counter equals 0xFFFF not shown on slide). The slide also shows two options for the physical shape of the output signal (TxPWM) -active high and active low. The two options not shown are forced low and forced high. All four options are controlled by register GPTCONA.
Comp2 Comp1
The example on the slides assumes Counting up/down Mode and that the timer starts with value Comp1 loaded into TxCMPR and period #1 in TXPR. At some point in period 2 our code changes the value in TXCMPR from Comp1 to Comp2. Thanks to the compare register background (or shadow) function this value is taken into foreground with the next reload condition. This leads to the new shape of the output signal for period 3. Somewhere in period 3 our code modifies register TxPR preparing the shape of period 4.The answer is the PIE (Peripheral Interrupt Expansion)-unit. Please note that we have two points for Compare Interrupts within each period. Question: How do we distinguish between them? When we go through all timer control registers on the next pages please remember this question. There must be a way to specify whether we are in the first or second half of a period.
5-6
GP Timer Registers
GP Timer Registers
To set up an Event Manager Timer we have to configure five registers per timer. If wed like to use one or more timer interrupt sources, then we have to set up a few more registers. The next slides are going through the registers step by step, at the end a lab exercise is waiting for you!
Register
T1CNT T1CMPR T1PR
GP Timer Registers
Address Description
0x007400 0x007401 0x007402 0x007403 0x007404 0x007405 0x007406 0x007407 0x007408 0x007500 0x007501 0x007502 0x007503 0x007504 0x007505 0x007506 0x007507 0x007508 General Purpose Timer Control Register A Timer 1 Counter Register Timer 1 Compare Register Buffer Timer 1 Period Register Buffer Timer 1 Control Register Timer 2 Counter Register Timer 2 Compare Register Buffer Timer 2 Period Register Buffer Timer 2 Control Register General Purpose Timer Control Register B Timer 3 Counter Register Timer 3 Compare Register Buffer Timer 3 Period Register Buffer Timer 3 Control Register Timer 4 Counter Register Timer 4 Compare Register Buffer Timer 4 Period Register Buffer Timer 4 Control Register
5-8
GPTCONA
EVA
EVB
Upper Byte:
Timer 2 Compare Trip Enable T2CTRIPE (if EXTCONA[0]=1)
0 = disable 1 = enable
15
reserved
14
T2STAT
13
T1STAT
12
RESERVED
11
RESERVED
10-9
T2TOADC
8-7
T1TOADC
5-7
GP Timer Registers
5
RESERVED
4
RESERVED
3-2
T2PIN
1-0
T1PIN
5 - 10
5-8
GP Timer Registers
Upper Byte:
15 FREE 14 SOFT 13 reserved 12 11 10 TPS2 9 TPS1 8 TPS0
TMODE1 TMODE0
Emulation Halt Behavior 00 = stop immediately 01 = stop at end of period 1x = free run (do not stop)
Bits 12-11 select the operation mode. Weve discussed the two most important modes before; the two remaining modes are the Directional Up/Down mode and the Stop/Hold mode. The first mode uses an external input (TDIRA) to specify the counting direction; the latter just halts the timer in its current status, no re-initialization needed to resume afterwards. Bits 10 to 8 are the input clock prescaler to define another clock division factor. Recall that the counting frequency is derived from:
The external oscillator (30MHz) The internal PLL-status (PLLCR: multiply by 10/2 = 150 MHz) The High speed clock prescaler (HSPCP = divide by 2: 75 MHz) and The Timer Clock Prescale factor (1 to 128)
This gives us the option to specify the desired period for a timer. For example, to setup a timer period of 100 milliseconds, we can use this calculation: Timer input pulse = (1/ext_clock_freq) * 1/PLL * HISPCP * Timer TPS 1,7067 s = (1/ 30 MHz) 100 ms / 1,7067 s = 58593. Load TxPR with 58593 to set the timer period to 100 milliseconds. * 1/5 * 2 * 128
5-9
GP Timer Registers
Bit 6 enables the timer operation. At the end of an initialization procedure we will have to set bit 6 to 1 to start the timer. Bits 5 and 4 select the timer clock source; Bits 3 and 2 define the point of time to reload the value out of the background register into the foreground compare register. Bit 1 is used to enable the compare operation. Sometimes youll need an internal period generator only, for these applications you can switch off the compare operation that is the generation of switch patterns for the output lines. Bits 7 and 0 are timer 2 specific bit fields. They are dont cares for register T1CON. With the help of bit 7 we can force a start of timer 1 and 2 simultaneously. In this case, both timers start if bit 6 (TENABLE) of T1CON is set. Bit 0 forces timer 2 to use period register of timer 1 as base to generate a synchronized period for timer 2 and timer 1.
Lower Byte:
Timer Enable
0 = timer disable 1 = timer enable
Timer Compare Operation Enable Timer Clock Source 00 = internal (HSPCLK) 01 = external TCLKIN pin 10 = reserved 11 = QEP
6 5 4
TCLKS0
Period Register Select 0 = use own per. reg. 1 = use Timer 1 per. reg (bit reserved in T1CON)
3
TCLD1
2
TCLD0
TECMPR SELT1PR
Start with Timer 1 0 = use own TENABLE 1 = use Timer 1 TENABLE (bit reserved in T1CON)
Compare Register Reload Condition 00 = when counter equals zero (underflow) 01 = when counter equals zero or period reg 10 = immediately 11 = reserved
5 - 12
5 - 10
GP Timer Registers
Now lets make a calculation. Assume that your task is to setup a PWM signal with a period of 50 kHz and a pulse width of 25%:
Solution :
= = = = = =
5 - 11
GP Timer Interrupts
GP Timer Interrupts
To enable one of the interrupt sources of Event Manager A we have to set a bit inside EVAIMRA, B or C.
EVAIMRA Register
@ 0x742C
12
-
15
-
14
-
13
-
11
-
10
8
T1CINT
T1OFINT T1UFINT
7
T1PINT
6
-
5
-
4
-
Bit 10: 9: 8: 7: 3: 2: 1: 0:
Event Timer 1 Overflow Timer 1 Underflow Timer 1 Compare match Timer 1 Period match Compare Unit 3, Compare match Compare Unit 2, Compare match Compare Unit 1, Compare match Power Drive Protect input, EVA
5 - 16
EVAIMRB Register
@ 0x742D
12
-
15
-
14
-
13
-
11
-
10
-
9
-
8
-
7
-
6
-
5
-
4
-
1
T2CINT
0
T2PINT
T2OFINT T2UFINT
Bit 3: 2: 1: 0:
Event Timer 2 Overflow Timer 2 Underflow Timer 2 Compare match Timer 2 Period match
5 - 17
5 - 12
GP Timer Interrupts
EVAIMRC Register
@ 0x742E
12
-
15
-
14
-
13
-
11
-
10
-
9
-
8
-
7
-
6
-
5
-
4
-
3
-
Bit 2: 1: 0:
Event Capture Unit 3 input Capture Unit 2 input Capture Unit 1 input
5 - 18
EVAIFRx Register
15 14
-
13
-
12
-
11
-
10
8
T1CINT
EVAIFRA @ 0x742F
Read: 0 = no event 1 = flag set
T1OFINT T1UFINT
7
T1PINT
6
-
5
-
4
-
15
14
-
13
-
12
-
11
-
10
-
9
-
8
-
EVAIFRB @ 0x7430
Write: 0 = no effect 1 = reset flag
7
-
6
-
5
-
4
-
1
T2CINT
0
T2PINT
T2OFINT T2UFINT
15
14
-
13
-
12
-
11
-
10
-
9
-
8
-
EVAIFRA @ 0x7431
7
-
6
-
5
-
4
-
3
-
5 - 13
5 - 20
The result of Lab4 is a good starting point for Lab5. Recall that we initialized the core timer 0 to request an interrupt service every 50 milliseconds. Now we can use this interrupt service routine to load the next note into the period and compare register of T1. A time of 50 milliseconds is a little bit too fast, but we have a 50ms variable CpuTimer0.InterruptCount. If we wait until the value of this variable is 10 we know that an interval of 500ms is over. After this period we can play the next note starting with c1 and go to c2 in an endless loop. Or, try to play the notes alternately as an ascending and descending sequence (or: recall a nursery rhyme).
5 - 14
5 - 21
Lab5.c
DSP281x_GlobalVariableDefs.c
F2812_EzDSP_RAM_lnk.cmd
F2812_Headers_nonBIOS.cmd
5 - 15
rts2800_ml.lib
Select the Compiler tab. In the preprocessor Category, find the Include Search Path (-i) box and enter:
C:\tidcs\C28\dsp281x\v100\DSP281x_headers\include;..\include
6. Setup the stack size: Inside Build Options select the Linker tab and enter in the Stack Size (-stack) box:
400
Close the Build Options Menu by Clicking <OK>.
and watch the tools run in the build window. If you get errors or warnings debug as necessary. So far we just generated a new project Lab5 with the old code from Lab4. If you run the code now you should see the Knight Rider of Lab4. Now we can start to modify our code in Lab5.c.
5 - 16
The TMODE-field to counting up mode; Field TPS to divide by 128; Bit TENABLE to disable timer (we will enable it later) Field TCLKS to internal clock Field TCLD to reload on underflow and Bit TECMPR to enable compare operation
12. Last question is: how do we initialize T1PR? Well, obviously we need 8 different values for our 8 basic notes. So lets define a new integer array frequency [8] as a local variable in main! 13. How do we initialize array frequency [8]? We can initialize the array together with the definition inside main: int frequency [8] = {?,?,?,?,?,?,?,?}; A basic octave is a fixed series of 8 frequencies. AND: there is a relationship between the basic note c1 (264 Hz) and the next notes: 264 Hz (c1) 297Hz (d) = 9/8 * c1 330Hz (e) = 5/4 * c1 352Hz (f) = 4/3 * c1
DSP28 - Event Manager
396Hz (g) = 3/2 * c1 440Hz (a) = 5/3 * c1 495Hz (b) = 15/8 * c1 528 Hz (c2) = 2 * c1
5 - 17
What is the relationship between these frequencies and T1PWM? Answer: We have to setup T1PR to generate a PWM period according to this list. The equation is: T1_PWM_Freq = 150MHz / (HISPCP * TPS * T1PR) For c1 = 264 Hz we get: T1PR = 150MHz / (2 * 128 * 264 Hz) = 2219. For d = 297 Hz we use: T1PR = 150MHz / (2 * 128 * 297 Hz) = 1973. Calculate the 8 initial numbers and complete the initial part for array frequency!
14. Next step: Modify the endless while(1) loop of main! Recall: i. The Core Timer T0 requests an interrupt every 50 milliseconds.
ii. The Watchdog Timer is alive! It will trigger a reset after 33ns*512*256*WDPS. If WDPS was initialized to 64 this reads as 280ms. iii. Timer T0 Interrupt Service Routine increment variable CpuTimer0.InterruptCount every 50 milliseconds We have to reset the watchdog every 200 milliseconds and we should play the next note after 500 milliseconds. Two tasks within this while(1) loop. Later we will learn that this type of multi tasking is much better solved with the help of DSP/BIOS Texas Instruments Real Time Operating System. For now we have to do it by our self. How can we find out, if a period of 200ms is over? We just have to test if CpuTimer0.InterruptCount is a multiple of 4. In language C this could be done by modulo division with 4 reminder is zero: if ((CpuTimer0.InterruptCount%4)==0) If it is TRUE then we have to perform the second half of the watchdog re-trigger sequence: EALLOW; SysCtrlRegs.WDKEY = 0xAA; EDIS; In a similar technique we can wait for 10 times 50 ms = 500 ms before we apply the next note into T1PR and T1CMPR. In Lab4 we did a reset of variable CpuTimer0.InterruptCount every time a period was over. Doing so, we limited the values for this variable between 0 and 3, which was fine for this single task exercise. When we have to take care of more activities with different periods, it is not a good recommendation to reset this variable. A better approach is to build a time interval out of two read operations of CpuTimer0.InterruptCount. With the first access we gather the actual time and store this value in a local unsigned long variable
5 - 18 DSP28 - Event Manager
time_stamp. With a second access to CpuTimer0.InterruptCount we can read the new time information and the difference between this value and the value of time_stamp is the elapsed time in multiples of 50ms. A wait instruction for 500ms could now look like this: if ( (CpuTimer0.InterruptCount time_stamp ) > 10) If TRUE, then:
Load time_stamp with CpuTimer0.InterruptCount Load the next note into EvaRegs.T1PR Load EvaRegs.T1CMPR = EvaRegs.T1PR/2 Enable T1PWM, set EvaRegs.T1CON.bit.TENABLE = 1 Implement and handle a status counter (variable i) to loop through array
frequency[8]
and watch the tools run in the build window. If you get errors or warnings debug as necessary. 16. Load the output file down to the DSP Click: File Load Program and choose the desired output file.
Test
17. Reset the DSP by clicking on: Debug Debug Reset CPU Restart followed by
18. Run the program until the first line of your C-code by clicking: Debug Go main.
5 - 19
EV Control Registers / Logic GP Timer 1 Compare GP Timer 1 Data Bus Compare Unit 1 Compare Unit 2 Compare Unit 3 GP Timer 2 Compare GP Timer 2 MUX Capture Units
Output Logic
PWM Circuits Output Logic PWM Circuits Output Logic PWM Circuits Output Logic Output Logic
CLK DIR
QEP Circuit
CAP1/QEP1 CAP2/QEP2 CAP3/QEPI1
5 - 22
A compare unit is a peripheral that is designed to generate pulse width modulated (PWM) output signals. What is a PWM signal and what is it used for?
5 - 20
With a PWM signal we can represent any analogue output signal as a series of digital pulses! All we need to do with this pulse series is to integrate it (with a simple low pass filter) to imitate the desired signal. This way we can build a sine wave shaped output signal. The more pulses we use for one period of the desired signal, the more precisely we can imitate it. We speak very often of two different frequencies, the PWM-frequency (or sometimes carrier frequency) and the desired signal frequency.
Original Signal
PWM representation
PAM representation
5 - 24
A lot of practical applications have an internal integrator, for example the windings of an electrical motor are perfectly suited to behave as a low-pass filter. One of the most used applications of PWM is digital motor control. Why is that? Answer: The overall goal is to control electrical drives by imprinting harmonic voltages and currents into the windings of the motor. This is done to avoid electromagnetic distortions of the environment and to achieve a high power factor. To induce a sine wave shaped signal into the windings of a motor we would have to use an amplifier to achieve high currents. The simplest amplifier is a standard NPN or PNP transistor that proportionally amplifies the base current into the collector current. Problem is, for high currents we cant force the transistor into its linear area; this would generate a lot of thermal losses and for sure exceed its maximal power dissipation. The solution is to use this transistor in its static switch states only (On: Ice = Icesat, Off: Ice = 0). In this states a transistor has its smallest power dissipation. AND: by adapting the switch pattern of a PWM (recall: amplitude is 1 or 0 only) we can induce a sine wave shaped current!
5 - 21
PWM
We have two different options to generate a PWM-signal, asymmetric and symmetric PWM.
Period Compare
Counter
5 - 26
5 - 22
5 - 27
NOTE: The value in T1PR defines the length of a period TPWM in asymmetric operating mode. For symmetric mode the value of TxPR defines only half of the length of a period TPWM. The Compare Unit consists of 6 output signals PWM1 to PWM6. The time base is derived from Event Manager Timer1, e.g. register T1PR together with the setup for T1 (Register T1CON) defines the length of a PWM-period for all six output signals. Register T1CNT is used as the common counter register. With 3 new registers CMPR1,CMPR2and CMPR3 we can specify 3 different switch pattern based on T1PR. Obviously this leads to a 3-phase control pattern for 3 phase electrical motors. Each Compare Unit is able to drive a pair of two output signals. With the help of its own output logic we usually define the two lines to be opposite or 180-degree out of phase to each other - a typical pattern for digital motor control. The next slide shows a typical layout for a three-phase power-switching application.
5 - 23
DC bus capacitor
GP Timer 1 Counter
DBTCONA . 11 - 2
PWMy, y+1
Note: x = 1, 2, 3; y = 1, 3, 5
5 - 29
5 - 24
The central block of the Compare Unit is a compare logic that compares the value of Event Manager Timer 1 counter register T1CNT against Compare Register CMPRx. If there is a first match, a rising edge signal goes into the next block called Dead Band Unit. With the second match between T1CNT and CMPRx in symmetric PWM mode a falling edge signal is generated. We will discuss this Dead Band Unit a little bit later. We do have three Compare Units available. The output logic is controlled by means of a register, called Action Control Register ACTRA and register COMCONA. With the help of this register set we can adjust the shape of the physical PWM output signal to our needs. We can specify four types for all 6 output lines:
Active High: o First CMPRx match switches PWM output from 0 to 1. After second CMPRx match the signal is set back to 0.
Active Low: o First CMPRx match switches PWM output from 1 to 0. After second CMPRx match the signal is setback to 1.
Address
Description
COMCONA 0x007411 Compare Control Register A 0x007413 Compare Action Control Register A 0x007417 Compare Register 1 0x007418 Compare Register 2 0x007419 Compare Register 3 0x007513 Compare Action Control Register B 0x007515 Dead-Band Timer Control Register B 0x007517 Compare Register 4 0x007518 Compare Register 5 0x007519 Compare Register 6 DBTCONA 0x007415 Dead-Band Timer Control Register A CMPR1 CMPR2 CMPR3 ACTRB
EVA
EVB
5 - 25
The next two slides explain the set up for the individual bit fields of COMCONA. Most of the bits are reserved in basic operation mode (EXTCONA [0] = 0).
COMCONA @ 0x007411
15
CENABLE
14
CLD1
13
CLD0
12
PDPINT Status
0 = low 1 = high
5 - 31
COMCONA @ 0x007411
FCMP2OE
C2TRIPE
(if EXTCONA[0]=1) (if EXTCONA[0]=1) (if EXTCONA[0]=1) 0 = disable 0 = disable 0 = disable 1 = enable 1 = enable 1 = enable
FCMP3OE
FCMP1OE
C3TRIPE
C1TRIPE
5 - 26
COMCONA [15] is the enable bit for the three phase compare units. With COMCONA [14:13] and COMCONA [11:10] we specify the point in time when the compare registers and action control registers are reloaded (shadow register content into foreground). As we have seen with the timers we can prepare the next period in the current running period. COMCONA [8] shows the status of the power drive protection flag. If it is a 1, the DSP has seen an interrupt request from its over-current input PDPINT. With EXTCONA [0] =1 all three pairs of compare output lines can be enabled independently of each other. If EXTCONA [0] =0 then all six lines are enabled with COMCONA [9] =1. If EXTCONA [0] =1 we can use three more individual over current inputs signals. To use these over current signals we can enable or disable this feature using bits COMCONA [2:0]. COMCONA [12] = 1 enables a special switch pattern for digital motor control, called Space Vector Modulation (SVM). This feature is built-in hardware support for one specific theoretical control algorithm. For details see literature or your lectures about power electronics.
15-4 reserved
3 EVSOCE
2 QEPIE
1 QEPIQUAL
0 INDCOE
5 - 33
5 - 27
14
D2
13
D1
12
D0
11
10
Pin Action on Compare: CMPyACT1-0 00 01 10 11 force low active low active high forced high
5 - 34
SV Rotation Direction
can write as 0 when SV not in use
ACTRA [11:0] define the shapes of the six PWM output signals as discussed before. ACTRA [15:12] are used to support Space Vector Modulation. Bit 15 defines the rotation direction of the resulting electromagnetic vector as clockwise or anti clockwise. ACTRA [14:12] declare the Basic Space Vector for the next PWM periods. A Basic Space Vector is a 60-degree section of the unit circle. This gives 6 vectors per rotation plus two virtual vectors with no current imprint. If SVM is not used, one can initialize bits 15:12 to zero.
5 - 28
to motor phase
Transistor gates turn on faster than they shut off Short circuit if both gates are on at same time!
5 - 35
Two basic approaches exist for controlling shoot-through: modify the transistors, or modify the PWM gate signals controlling the transistors. In the first case, the switch-on time of the transistor gate must be increased so that it (slightly) exceeds the switch-off time. The hard way to accomplish this is by adding a cluster of passive components such as resistors and diodes in series with the transistor gate to act as low-pass filter to implement the delay. The second approach to shoot-through control separates transitions on complimentary PWM signals with a fixed period of time. This is called dead-band. While it is possible to perform software implementation of dead-band, the C28x offers on-chip hardware for this purpose that requires no additional CPU overhead. Compared to the passive approach, dead-band offers more precise control of gate timing requirements.
5 - 29
HSPCLK
Prescaler
DBTCONA . 4 - 2
PHx
edge detect
ENA
reset
4-bit Counter
comparator
DTPHx DTPHx_
5 - 36
Each compare unit has a dead-band timer, but shares the clock prescaler unit and the dead-band period with the other compare units. Dead-band can be individually enabled for each unit.
DB Timer Enable
0 = disable 1 = enable
DB Timer Prescaler
000 = 1 001 = 2 010 = 4 011 = 8 100 = 16 101 = 32 110 = 32 111 = 32
5 - 37
5 - 30
Capture Units
Capture Units
EV Control Registers / Logic GP Timer 1 Compare GP Timer 1 Data Bus Compare Unit 1 Compare Unit 2 Compare Unit 3 GP Timer 2 Compare GP Timer 2 MUX Capture Units
Output Logic
PWM Circuits Output Logic PWM Circuits Output Logic PWM Circuits Output Logic Output Logic
CLK DIR
QEP Circuit
CAP1/QEP1 CAP2/QEP2 CAP3/QEPI1
5 - 38
The capture units allow time-based logging of external logic level signal transitions on the capture input pins. Event Manager A has three capture units, and each is associated with a capture input pin. The time base is selectable to be either GP timer 1 or 2. The timer value is captured and stored in the corresponding 2-level-deep FIFO stack when a specified transition is detected on a capture input pin. Capture Unit 3 can be configured to trigger an A/D conversion that is synchronized with an external signal of a capture event.
5 - 31
Capture Units
Capture Units
Timer
Trigger
Timestamp Values
Capture units timestamp transitions on capture input pins Three capture units (per event manager) each associated with a capture input pin
5 - 39
Three potential uses for the Capture Units are: Measurement of the width of a pulse or a digital signal Automatic start of the AD Converter by a Capture Event from CAP3 Low speed estimation of a rotating shaft. A potential advantage for low speed estimation is given when we use time capture (16Bit resolution) instead of position pulse counting (poor resolution in slow mode).
5 - 32
Capture Units
x
5 - 40
GP Timer 1 Counter
GP Timer 2 Counter
CAPCONA . 14 -12
CAPCONA . 10 - 9
MUX
Enable
CAPRESET CAPCONA . 15
5 - 33
Capture Units
Address
0x007420 0x007422 0x007423 0x007424 0x007425 0x007427 0x007428 0x007429 0x007520 0x007522 0x007523 0x007524 0x007525 0x007527 0x007528 0x007529
Description
Capture Control Register A Capture FIFO Status Register A Two-Level Deep FIFO 1 Stack Two-Level Deep FIFO 2 Stack Two-Level Deep FIFO 3 Stack Bottom Register of FIFO 1 Bottom Register of FIFO 2 Bottom Register of FIFO 3 Capture Control Register B Capture FIFO Status Register B Two-Level Deep FIFO 4 Stack Two-Level Deep FIFO 5 Stack Two-Level Deep FIFO 6 Stack Bottom Register of FIFO 4 Bottom Register of FIFO 5 Bottom Register of FIFO 6
5 - 42
EVA
EVB
ADC Start
0 = no action 1 = CAP3INT flag 11 10 9 8
14-13
CAPQEPN
reserved
Timer Select
0 = GP Timer 2 1 = GP Timer 1 1-0 reserved
CAP3EDGE
5 - 34
Capture Units
CAPCONA [15] is a reset bit for the Capture state machine and the status of the FIFO. It should be used in a single instruction to reset the Capture units during initialization. Note: to execute reset you will have to apply a zero! With CAPCONA [14-12] the Capture Units are enabled. Please note that CAP1 and CAP2 are enabled jointly, whereas CAP3 has its own enable bit. CAPCONA [10-9] are used to select the clock base for the capture units. Again, for CAP1 and CAP2 we have to select the same GP timer. CAPCONA [8] allows CAP3 to start an AD conversion. Of course, before we use this option, we have to initialize the ADC. This will be explained in the next chapter. CAPCONA [7-2] specify if the capture units are triggered with a rising or falling edge or with both edges.
15-14 reserved
13-12
11-10
9-8
7-0 reserved
CAP3FIFO
CAP2FIFO
CAP1FIFO
FIFOx Status:
00 = empty 01 = one entry 10 = two entries 11 = three entries attempted, 1st entry lost
5 - 44
Register CAPFIFOA reflects the filling status of the three result register FIFOs. In case of an overflow the oldest entry will be lost. This principle ensures that a capture unit stores the two latest measurement results. If our program performs a read access to one of the FIFO result registers the status value in the corresponding CAPFIFOA bit field is decremented.
5 - 35
5 - 45
A QEP unit is normally used to derive direction and speed information from an incremental encoder circuit mounted on a rotating shaft. As shown on the previous slide two sensor signals are used to generate two digital pulse streams Channel A and Channel B.
EV Control Registers / Logic GP Timer 1 Compare GP Timer 1 Data Bus Compare Unit 1 Compare Unit 2 Compare Unit 3 GP Timer 2 Compare GP Timer 2 MUX Capture Units
Output Logic
PWM Circuits Output Logic PWM Circuits Output Logic PWM Circuits Output Logic Output Logic
CLK DIR
QEP Circuit
CAP1/QEP1 CAP2/QEP2 CAP3/QEPI1
5 - 46
5 - 36
The time relationship between A and B lead to a state machine with four states. Depending on the sequence of states and the speed of alternation, the QEP unit timer is decremented or incremented. By reading and comparing this timer counter information at fixed intervals, we can obtain speed and/or position information.
increment counter
10
decrement counter
Ch. A Ch. B
5 - 47
.
QEPIQUAL
CAP1/QEP1 CAP2/QEP2
Ch. B
Index
CAP3/QEPI
GP Timer 2 selected as pulse counter Timer Prescaler bypassed (i.e. Prescale always 1)
QEPIE
GP Timer 2
5 - 48
5 - 37
15-4 reserved
3 EVSOCE
2 QEPIE
1 QEPIQUAL
0 INDCOE
5 - 49
The third capture input pin QEPI1can be used as an absolute position information signal for a zero degree crankshaft position. This signal is then used to reset the QEP timer to its initial state. To enable the QEPI1 index function we have set EXTCONA [2] to 1. Then we have two more options, selected with EXTCONA [1]: Use index pulse QEPI1 independent from the state of QEP1 and QEP2 Use index pulse QEPI1 as a valid trigger pulse only if this event is qualified by the state of QEP1 = 1 AND QEP2 = 1.
5 - 38
Lab5A.c
DSP281x_GlobalVariableDefs.c
F2812_EzDSP_RAM_lnk.cmd
F2812_Headers_nonBIOS.cmd
rts2800_ml.lib
Select the Compiler tab. In the preprocessor Category, find the Include Search Path (-i) box and enter:
400
Close the Build Options Menu by Clicking <OK>.
5 - 40
11. Next we have to initialize the Event Manager Timer 1 to produce a PWM signal. This involves the registers GPTCONA, T1CON, T1CMPR and T1PR. For register GPTCONA it is recommended to use the bit-member of this predefined union to set bit TCMPOE to 1 and bit field T1PIN to active low. For register T1CON set
The TMODE-field to counting up mode; Field TPS to divide by 1; Bit TENABLE to disable timer; Field TCLKS to internal clock Field TCLD to reload on underflow Bit TECMPR to enable compare operation
CpuTimer0Regs.TCR.bit.TSS = 0; i = 0; time_stamp = 0;
and add 4 new lines to initialise T1PR, T1CMPR, to enable GP Timer1 Compare interrupt and to start GP Timer 1: EvaRegs.T1PR = 1500; EvaRegs.T1CMPR = EvaRegs.T1PR/2; EvaRegs.EVAIMRA.bit.T1CINT = 1; EvaRegs.T1CON.bit.TENABLE = 1;
What is this number 1500 for? Well, it defines the length of a PWM period:
f PWM =
with TPST1=1, HISCP = 2, fCPU = 150MHz and a desired fPWM = 50kHz we derive: T1PR = 1500! T1CMPR is preloaded with half of T1PR. Whys that? Well, in general T1CMPR defines the width of the PWM-pulse. Our start-up value obviously defines a pulse width of 50%.
5 - 41
Original Signal
PWM representation
PAM representation
5 - 24
A duty cycle of 50% represents a sine angle of 0 degrees! And, it makes sense to initialize the PWM unit for this angle. From the bottom left of the slide we can derive: Degree 0 90 180 270 360 Sin 0 1 0 -1 0 Duty Cycle 50% 100% 50% 0% 50%
5 - 42
13. Modify the endless while(1) loop of main! We will perform all activities using GP Timer 1 Compare Interrupt Service. Therefore we can delete almost all lines of this main background loop, we only have to keep the watchdog service: while(1) { EALLOW; SysCtrlRegs.WDKEY = 0xAA; EDIS; } 14. Rename the interrupt service routine cpu_timer0_isr into T1_Compare_isr. Remove the line CpuTimer0.InterruptCount++; and replace the last line of this routine by: PieCtrlRegs.PIEACK.all = PIEACK_GROUP2; Before this line add another one to acknowledge the GP Timer 1 Compare Interrupt Service is done. Remember how? The Event Manager has 3 interrupt flag registers EVAIFRA,EVAIFRB and EVAIFRC. We have to clear the T1CINT bit (done by setting of the bit): EvaRegs.EVAIFRA.bit.T1CINT = 1;
and watch the tools run in the build window. If you get syntax errors or warnings debug as necessary. 16. Load the output file down to the DSP Click: File Load Program and choose the desired output file.
Test
17. Reset the DSP by clicking on: Debug Debug Debug Reset CPU Restart Go main. followed by and
5 - 43
18. When you now run the code the DSP should generate a 50 kHz PWM signal with a duty cycle of 50% on T1PWM. If you have an oscilloscope you can use jumper JP7 (in front of the loudspeaker) of the Zwickau Adapter board to measure the signal. If your laboratory cant provide a scope, you can set a breakpoint into the interrupt service routine of T1 Compare at line PieCtrlRegs.PIEACK.all = PIEACK_GROUP2; Verify that your breakpoint is hit periodically, that register T1PR holds 1500 and register T1CMPR is initialized with 750. Use the watch window to do so. Do not continue with the next steps until this point is reached successfully! Instead go back and try to find out, what went wrong during the modification of your source code.
5 - 44
Question is: how do we calculate the sine-values? Use the sin(x) function
This function is part of the C compilers math.lib. All we would need to do is to add the header file math.h to our project. Problem: sin(x) is a floatingpoint function; our DSP is a fixed point processor. That means the compiler has to generate quite a lot of assembler instructions to calculate the sine values. This will cost us a quite a lot of CPU time, just to calculate the same series of sine values over and over. Feasible, but not recommended
Next Question:
How do we generate a lookup table? Well, use a calculator, note all results and type them into an array! How many values? Well, the more values we have the better we can approximate the analogue sine wave shape! Recall, we need sine values from 0 to 360. Sounds like a lot of boring work, doesnt it? Answer: Texas Instruments has already done the work for you. The C28x DSP comes with a BOOT-ROM (see memory map module 1). A part of this memory area is a sine wave table! From Address 0x3F F000 to 0x3F F3FF we find 512 values for sin(x). The numbers are stored as 32 Bit numbers in Q30-notation. With 512 entries we have an angle step of 0.703 (360/512) for a unit circle.
5 - 45
Bit 31 (-1)*21
Bit 30 1*20
Bit 29 1*2-1
Bit 28 1*2-2
Bit 27 1*2-3
Bit 26 1*2-4
Bit 25 1*2-5
The decimal range of a Q30 number is 2...+1.9999: Most negative number: Decimal minus 1: Smallest negative number Zero Smallest positive number Decimal plus 1: Most positive number: 0x8000 0000 0xC000 0000 0xFFFF FFFF 0x0000 0000 0x0000 0001 0x4000 0000 0x7FFF FFFF: -2 -1 -9.31322e-10 0 +9.31322e-10 +1 +1.999999999
IQ-Math Library
Texas Instruments has built a whole library of fixed-point math operations based on this Q-format. This library called IQ-Math is widely used in closed control applications like digital motor control, FAST FOURIER TRANSFORM (FFT) or digital filters (FIR, IIR). The library is free, no royalties and can be downloaded from TIs web. The appendix of this CD contains the current version of IQ-Math. We will discuss and use this library in a specific module in Part 2 of this DSP course.
5 - 46
Address 0x3F F000 0x3F F002 0x3F F004 0x3F F006 ... 0x3F F100 .... 0x3F F200 .... 0x3F F300 .... 0x3F F3FE
0x0000
0x4000
90
1.0
0x0000
0x0000
180
0.0
0x0000
0xC000
270
-1.0
0xF170
0xFF36
359,3
-0.01227153838
5 - 47
The #pragma statement declares a specific data memory area, called IQmathTables. This area will be linked in the next procedure step to the address range of the Boot ROM sine table. The global variable sine_table[512] is an array of this new data type IQ30. 20. Add an additional Linker Command file to your project. From E:\C281x\Labs\Lab5A add: Lab5A.cmd Open and inspect this file. You will see that we add just one entry for the physical memory location (ROM) in data page 1 and that we connect the memory area IQmathTables to address ROM. The attribute NOLOAD assures that the debugger will not try to download this area into the DSP when we load the program because it is already there, it is ROM read only memory. 21. Modify T1_Compare_isr( ) This interrupt service routine is a good point to modify the pulse width of the PWM signal. Recall, we do have now a global array sine_table[512] that holds all the sine values we need for our calculation. Now we have to do a little bit of maths. What is the relationship between this sine value and the value of T1CMPR? Answer: (1) We know that the difference between T1PR and T1CMPR defines the pulse width of the current PWM period. So the goal is to calculate a new value for T1CMPR. (2) Next, we have to take into account that the sine table delivers signed values between +1 and 1. Therefore we have to add an offset of +1.0 to this value. (3) This shifted sine value has to be multiplied with T1PR/2. Summary:
5 - 48
To avoid fixed-point overflows we can embed the calculation into a saturation function _IQsat(x,max,min) to limit the result between T1PR and 0. This leads to our final instruction: EvaRegs.T1CMPR = EvaRegs.T1PR _IQsat( _IQmpy( sine_table[index] + _IQ30(0.9999), EvaRegs.T1PR/2), EvaRegs.T1PR,0) ; Add this line just after the EDIS instruction that follows the service of the Watchdog timer. 22. Setup the sine wave frequency. Recall that the Boot ROM sine table consists of 512 entries for a unit circle. The frequency of the sine wave is given by:
f SIN =
For example, if we use all 512 entries of the Boot ROM table we get:
f SIN =
f SIN =
Lets use this last set up. It means we have to increment the index by 4 to make the next access to the lookup table. Add the next line after the update line for T1CMPR: index += 4; Do not forget to: (1) Reset variable index to 0 if it is increased above 511. (2) Declare the integer variable index to be static.
5 - 49
Optional Exercise
and watch the tools run in the build window. If you get syntax errors or warnings debug as necessary. 24. Load and test the final version of the output file as youve done before. With the help of the oscilloscope we should see now a change of the pulse width of the PWM signal on the fly. 25. Optional: Low pass filter: The low pass filter capacity of the tiny loudspeaker is not strong enough to integrate the pulse sequence to a sine wave shaped signal. We can improve this by adding a simple low-pass filter between the two connectors of jumper JP7-2 (DSP-T1PWM) and JP7-1 (Loudspeaker). Build a passive low pass filter of first order with a frequency of 25 KHz:
f Filter =
1 = 25 KHz 2 R C
Optional Exercise
How about other frequencies?
In the previous exercise we generated a modulated sine wave of 390 Hz. We used the 512-point look-up table and stepped through it using an increment of 4. How do we generate other frequencies? Answer: when we change the step size for variable index we can generate more (or less PWM-periods per 360. More means we slow down the sine frequency, less means we increase the sine wave frequency. The following table shows the different sine wave frequencies for a PWM carrier frequency of 50 KHz and a lookup table of 512 points per 360:
f SIN =
We cant increase the step size much above 50 because this gives us only 10 points per 360 to synthesize the sine wave.
5 - 50
Optional Exercise
What about frequencies that we do not match with any of these incremental steps? Recall our Lab Exercise 5 with the range of 8 notes; it started with a note of 264Hz. How do we generate a sine wave of 264 Hz? The answer is: We have to modify the PWM frequency itself. So far we did all experiments with a fixed PWM signal of 50 KHz. Lets fix now the number of points taken out of the look up table to 128 (that is an index increment by 4). To get a sine wave of 264Hz we calculate:
f SIN =
f PWM =
T 1PR =
T1PR has to be loaded with an integer value, so we have to round the result to 2219. Test:
f PWM =
f CPU 150 MHz = = 33.799 KHz T 1PR TPST 1 HISCP 2219 *1* 2
f PWM 33.799 KHz = = 264.05 Hz 128 128
f SIN =
Thats a reasonable result; the intended frequency of 264Hz is missed by an error of 0.02%.
26. Try to setup your code to generate a sine wave of 264Hz! 27. If you have additional time in your laboratory try to improve Lab5 to generate all 8 notes with sine wave modulated PWMs!
End of Lab 5A
5 - 51
Optional Exercise
5 - 52