Program TMS320LF2407A

download Program  TMS320LF2407A

of 107

Transcript of Program TMS320LF2407A

  • 8/18/2019 Program TMS320LF2407A

    1/107

    See discussions, stats, and author profiles for this publication at: https://www.researchgate.net/publication/259344057

    Introduction to C programming theTMS320LF2407A DSP Controller

    BOOK · FEBRUARY 2011

    READS

    1,063

    1 AUTHOR:

    George Terzakis

    University of Portsmouth

    10 PUBLICATIONS  0 CITATIONS 

    SEE PROFILE

    Available from: George Terzakis

    Retrieved on: 12 March 2016

    https://www.researchgate.net/profile/George_Terzakis?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_4https://www.researchgate.net/profile/George_Terzakis?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_5https://www.researchgate.net/?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_1https://www.researchgate.net/profile/George_Terzakis?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_7https://www.researchgate.net/institution/University_of_Portsmouth?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_6https://www.researchgate.net/profile/George_Terzakis?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_5https://www.researchgate.net/profile/George_Terzakis?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_4https://www.researchgate.net/?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_1https://www.researchgate.net/publication/259344057_Introduction_to_C_programming_the_TMS320LF2407A_DSP_Controller?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_3https://www.researchgate.net/publication/259344057_Introduction_to_C_programming_the_TMS320LF2407A_DSP_Controller?enrichId=rgreq-cfe98ae7-51ba-40b2-a978-166efc8a6b01&enrichSource=Y292ZXJQYWdlOzI1OTM0NDA1NztBUzo5OTc5MzM4MDU3NzI4NUAxNDAwODA0MDA0Mjg0&el=1_x_2

  • 8/18/2019 Program TMS320LF2407A

    2/107

     

    Introduction to C Programming

    with the TMS320LF2407A ™  

    DSP Controller

    George Terzakis

  • 8/18/2019 Program TMS320LF2407A

    3/107

     

    ii

  • 8/18/2019 Program TMS320LF2407A

    4/107

     

    iii

    To my dad; we miss you.

    G.T.

  • 8/18/2019 Program TMS320LF2407A

    5/107

     

    iv

  • 8/18/2019 Program TMS320LF2407A

    6/107

     

    v

    About the author

    George Terzakis received the B.S. degree in Informatics from the University of Piraeus,

    Greece, in 1999. He received the M.S in Electronics & Computer Technology from Indiana

    State University, Terre Haute, IN, in 2002 and the M.S. in Robotics from the University of the

    West of England, Bristol, in 2010. He joined the Greek army in 2003 for a 12-month period of

    national service and during the period of 2005-2010, his main occupation involved teaching

    the fundamentals concepts in programming and informatics as a junior/senior high-school

    teacher. Among several other jobs, he has worked part-time for Indiana State University and

    the University of Patras, Greece, in topics concerning robotics and automation. His research

    interests are focused on robotics and relative disciplines.

  • 8/18/2019 Program TMS320LF2407A

    7/107

  • 8/18/2019 Program TMS320LF2407A

    8/107

     

    vii

    Contents

    Preface .............................................................................................................................................................. xvi 

    Acknowledgements  ...................................................................................................................................... xix 

    1. 

    INTRODUCTION ..................................................................................................................... 1 

    1.1 The TMS320LF/LC240xA™ series of DSP Controllers by Texas Instruments .....................................................1 1.1.1  The TMS320LF2407A™ DSP Controller made by TI   ...............................................................................1 

    1.1.2  Peripherals of the LF2407A Controller .................................... ..................................... .........................3 

    1.2  Programming the LF2407A in C ................................................................................................................4 

    1.3  What you need in order to read this book ...............................................................................................5 

    2. 

    WRITING AND EXECUTING PROGRAM “HELLO” ......................................................... 7 

    2.1 Creating a first project with Code Composer Studio™  .....................................................................................7 

    2.1.1 Creating the C file(s) ..................................................................................................................................8 2.1.2 The missing details ....................................................................................................................................8 

    2.1.3 Putting it all together ..............................................................................................................................10 

    2.1.4 The moment of truth ...............................................................................................................................11 

    2.1.5 What do we expect to see? .....................................................................................................................13 

    2.2 How did we do it? ..........................................................................................................................................13 

    2.2.1 Initialization of the System Control and Status Registers 1, 2 (SCSR1 and SCSR2)................................. ....14 

    2.2.2 The Watchdog .........................................................................................................................................16 

    2.2.3 I/O Pin configuration ...............................................................................................................................18 

    2.3 Using C structures to access register bits .......................................................................................................20 

    2.4 The Linker Command File ...............................................................................................................................21 

    2.4.1 Sections generated by the C compiler .....................................................................................................21 

    2.4.2 The MEMORY  clause in the Linker Command file.....................................................................................24 

    2.4.3 The SECTIONS clause in the Linker Command file ....................................................................................25 

    3.  REFINING PROGRAM “HELLO” ........................................................................................28 

  • 8/18/2019 Program TMS320LF2407A

    9/107

  • 8/18/2019 Program TMS320LF2407A

    10/107

     

    ix

    5.1.4 I/O Pin Configuration ...............................................................................................................................69 

    5.1.5 The main() function  .................................................................................................................................69 

    5.1.6 Observing the Pulse Width Modulated signal on the T1PWM/T1CMP pin ............................... ................70

    5.2 Using the Compare Units for Pulse Width Modulation in a new project........................................................72 

    5.2.1 Configuring Compare Unit 1 in EVA .........................................................................................................73  

    5.2.2 The Compare Control Register A (COMCONA) ...................................................... ...................................75 

    5.2.3 The Compare Registers (CMPR1-6) ................................... .................................... ...................................77 

    5.2.4 The Compare Action Control Registers (ACTRA and ACTRB) .................................. ...................................77 

    5.2.5 The Dead-band Timer Control Register A (DBTCONA) ................................ ................................... ...........78 

    5.2.6 PWM Initialization Routine ................................... .................................... .................................... ...........80 

    5.2.7 I/O Initialization .......................................................................................................................................83 

    5.2.8 Interrupt Service Routines .......................................................................................................................84 

    5.2.9 Compiling and executing .........................................................................................................................85 

    5.3 Using the EVA Capture Units ..........................................................................................................................88 

    5.3.1 Using the Capture Units for Rising/Falling edge detection in a new project ......................................... ....90 

    5.3.2 Defining Structures for the Capture Unit Registers ................................................ ..................................91 

    5.3.3 EVA Interrupt Mask and Flag Registers B and C ....................... .................................... ............................98 

    5.3.4 Capture Control Register A and Capture FIFO Status Register A ............................................................. 100 

    5.3.5 Capture FIFO Registers ............................................... ..................................... .................................... .. 102 

    5.3.6 Initializing Capture Units 1 and 2 ...................................... ..................................... ................................ 103 

    5.3.7 Core INT4 and Capture 1 and 2 Interrupt Service Routines ............................... ................................... .. 105 

    5.3.8 Configuring Capture I/O Pins 1 and 2 ............................................. .................................... .................... 110 

    5.3.9 Executing the Program ..................................................... ..................................... ................................ 111 

    5.4 Conclusions/Suggestions .............................................................................................................................. 113 

    6. 

    THE ANALOG TO DIGITAL CONVERTER .................................................................... 115 

    6.1 Overview of the Analog to Digital Converter ............................................................................................... 115  

    6.1.1 The Sequencers .................................. ..................................... .................................... .......................... 115 

    6.2 Using the ADC in Cascaded Sequencer Mode ............................................................................................... 117  

    6.2.1 Creating header and C files for the ADC Registers ................................ ................................... ............... 118 

    6.2.2 The ADC Control Register 1 (ADCTRL1) ................................................ ..................................... ............. 121

    6.2.3 The ADC Control Register 2 (ADCTRL2) .......................................... .................................... .................... 123 

    6.2.4 Autosequence Status Register (AUTO_SEQ_SR) .................................. .................................... ............... 125 

    6.2.5 The Maximum Conversion Channels Register (MAXCONV) ................................... ................................. 126 6.2.6 The ADC Input Channel Select Sequencing Control Registers (CHSELSEQ n) ................................... ......... 127 

    6.2.7 ADC Conversion Result Buffer Registers (RESULTn) .............................. ................................... ............... 128 

    6.2.8 Initializing the ADC in Cascaded Sequencer Mode ................... .................................... .......................... 128 

    6.2.9 The ADC High Priority Interrupt Service Routine ................................................................ .................... 131 

    6.2.10 I/O initialization ................................ ..................................... .................................... .......................... 133 

    6.2.11 The main() function ................................ ................................... ..................................... ..................... 133 

    6.2.12 Executing the program ................................................... .................................... ................................. 134 

  • 8/18/2019 Program TMS320LF2407A

    11/107

     

    x

    6.2. 13 Verifying expected time lapse between consecutive conversion sessions ........................................... 137 

    6.3 Using the ADC in Dual Sequencer Mode ...................................................................................................... 138 

    6.3.1 A new project for ADC operation in Dual Sequencer Mode........................ .................................... ........ 139 

    6.3.2 Configuring the ADC for Dual Sequencer Operation Mode ................................... ................................. 139 

    6.3.3 The ADC Interrupt Service Routine in Dual Sequencer Mode ................................................................ . 140 

    6.3.4 The main() function ................................... ................................... .................................... ..................... 142 

    6.3.5 Executing the program ..................................................... ..................................... ................................ 143 

    6.4 Summary/Conclusions ................................................................................................................................. 144 

    7. 

    THE SERIAL COMMUNICATIONS INTERFACE .......................................................... 146 

    7.1 Overview of the Serial Communications Interface ....................................................................................... 146 

    7.2 A Program to Transmit and Receive Characters with the SCI ....................................................................... 147 

    7.2.1 Creating a new header file for SCI register structures ................................ ................................... ......... 1477.2.2 The SCI Communication Control Register (SCICCR) .................. .................................... .......................... 150 

    7.2.3 The SCI Control Register 1 .......................................... ..................................... .................................... .. 151 

    7.2.4 The SCI Control Register 2 .......................................... ..................................... .................................... .. 153 

    7.2.5 The SCI Receiver Status Register ................................. ..................................... .................................... .. 153 

    7.2.6 The SCI Priority Register .......................................................... .................................... .......................... 154 

    7.2.7 Baud-Select Registers (SCIHBAUD – SCILBAUD) ............................. ................................... ..................... 155 

    7.2.8 Receiver Buffer Register and Transmitter Buffer Register .................................................. .................... 157 

    7.2.9 The main() function of project “sci”  .................................. ..................................... ................................ 157 

    7.2.10 Initialization of Serial Communications (file “DSP24_Uart.c” ) ................................... ........................... 158 

    7.2.11 I/O Configuration............................... .................................... .................................... .......................... 162 

    7.2.12 Interrupt Service Routines .............................................. .................................... ................................. 162 7.2.13 Program Execution ......................................................... .................................... ................................. 164 

    7.3 Summary ...................................................................................................................................................... 166 

    8. 

    THE SERIAL PERIPHERAL INTERFACE ...................................................................... 168 

    8.1 Overview of the Serial Peripheral Interface ................................................................................................. 168 

    8.2 A Program to Transmit characters with the SPI using the DSP as Master .................................................... 169 

    8.2.1 Creating a new header file for the SPI registers ............................. ................................... ..................... 169 

    8.2.2 The SPI Configuration Control Register (SPICCR) .......................................................... .......................... 1728.2.3 The SPI Operation Control Register (SPICTL) ................................................................ .......................... 173 

    8.2.4 The SPI Status Register (SPISTS) .................................. .................................... ..................................... .. 176 

    8.2.5 The SPI Baud Rate Register (SPIBRR) ................................. ..................................... ................................ 177 

    8.2.6 The SPICLK signal in terms of the CLKOUT signal and the value in SPIBRR .............................................. 177 

    8.2.7 The SPI Priority Register (SPIPRI) ................................ ..................................... .................................... .. 179 

    8.2.8 The SPI Receive and Transmit Buffer Registers (SPIRXBUF and SPITXBUF) ......................... .................... 179 

    8.2.9 The SPI Emulation Buffer Register (SPIRXEMU) ................................... .................................... ............... 179 

  • 8/18/2019 Program TMS320LF2407A

    12/107

     

    xi

    8.2.10 Initialization of the SPI ........................................................... .................................... .......................... 180 

    8.2.11 Sending a character ............................................................... ................................... ........................... 182 

    8.2.12 I/O Configuration............................... .................................... .................................... .......................... 182 

    8.2.13 The main() function ................................ ................................... ..................................... ..................... 183 

    8.2.14 SPI Interrupt Service Routines ........................................ .................................... ................................. 184 

    8.2.15 Building and Executing ............... .................................... .................................... ................................. 185 

    8.3 Summary/Conclusions ................................................................................................................................. 187 

    9. 

    THE CONTROLLER AREA NETWORK (CAN) MODULE .......................................... 189 

    9.1 Overview of the CAN Bus Protocol ............................................................................................................... 189 

    9.1.2 The CAN Physical Layer ................................... .................................... .................................... ............... 189 

    9.1.3 The CAN lines (CANH and CANL) .................................................... .................................... .................... 190 

    9.1.3 Exchanging messages through the CAN bus using Data and Remote Request Frames ........................... 191 

    9.1.4 Placing frames on the CAN bus ...................................................... .................................... .................... 194 

    9.1.5 The Bit Stuffing Rule ................................................... .................................... ..................................... .. 196 9.1.6 Error and Overload Frames....................... ..................................... .................................... .................... 196

    9.1.7 States of a CAN node .................................................. .................................... ..................................... .. 196 

    9.1.8 Bit Timing and Synchronization ............................. ................................... .................................... ......... 199 

    9.2 Overview of the CAN Controller ................................................................................................................... 200 

    9.2.1 CAN Mailboxes ................................................................. ..................................... ................................ 201 

    9.3 Using the CAN module to Transmit and Receive Data frames ..................................................................... 202  

    9.3.1 Creating a new header file for the CAN registers ................................ .................................... ............... 203 

    9.3.2 Message Identifier for High-Word Mailboxes 0-5 (MSGIDnH) ................................ ................................ 213 

    9.3.3 Message Identifier for Low-Word Mailboxes 0-5 (MSGIDnL) ............... ..................................... ............. 214 9.3.4 Message Control Field (MSGCTRLn) .................................. ..................................... ................................ 214 

    9.3.5 The Local Acceptance Mask Register 0 and 1 High Word (LAM0_H and LAM1_H).............. .................... 214 

    9.3.6 The Local Acceptance Mask Register 0 and 1 Low Word (LAM0_L and LAM1_L) .................................. .. 215 

    9.3.7 Mailbox Buffers .................................. ..................................... .................................... .......................... 215 

    9.3.8 The Mailbox Direction/Enable Register (MDER) ........................................................... .......................... 216 

    9.3.9 The Transmit Control Register (TCR) .............................................. .................................... .................... 217 

    9.3.10 The Receive Control Register ....................................................... .................................... .................... 218 

    9.3.11 The Master Control Register (MCR) ..................... .................................... .................................... ........ 219 

    9.3.12 The Bit Configuration Register 2 (BCR2) ................................ .................................... ........................... 221 

    9.3.13 The Bit Configuration Register 1 (BCR1) ................................ .................................... ........................... 221 

    9.3.14 The Error Status Register (ESR) ................................................................ ................................... ......... 223 9.3.15 The Global Status Register (GSR) ......................... .................................... ................................... ......... 225 

    9.3.16 The CAN Error Counter Register (CEC) ......................................... .................................... .................... 226 

    9.3.17 The CAN Interrupt Flag Register (CAN_IFR) ................................. .................................... ..................... 226 

    9.3.18 The CAN Interrupt Mask Register (CAN_IMR) ............................................................ .......................... 227 

    9.3.19 Initialization of the CAN module ................................ ................................... ..................................... .. 228 

    9.3.20 A Routine for Transmission ................................. .................................... .................................... ......... 234 

    9.3.21 I/O Configuration............................... .................................... .................................... .......................... 235 

  • 8/18/2019 Program TMS320LF2407A

    13/107

     

    xii

    9.3.22 CAN Interrupt Service Routines ............................................. .................................... .......................... 235 

    9.3.23 The main() function ................................ ................................... ..................................... ..................... 237 

    9.3.24 Building and Executing ............... .................................... .................................... ................................. 238 

    9.3.25 Tracking the data frame on the transmission line .................. .................................... .......................... 239 

    9.4 Summary/Conclusions ................................................................................................................................. 242 

    10. THE DISCRETE FOURIER TRANSFORM ..................................................................... 244 

    10.1 Overview on Fourier Analysis ..................................................................................................................... 244 

    10.1.1 Complex Numbers as a Representation of Phase and Magnitude .............................. .......................... 244 

    10.1.2 Fourier Analysis ................................ ..................................... .................................... .......................... 245 

    10.1.3 The Fourier Series .................................... .................................... ................................... ..................... 247 

    10.1.4 The Time-Continuous Fourier Transform .......................................... .................................... ............... 249 

    10.1.5 The Fourier series for Discrete-Time Signals (Discrete Fourier Transform) .................................. ......... 250 

    10.1.5 The DFT algorithm ............................................................ ................................... ................................ 251

    10.2 A Program to perform signal Analysis using the simple DFT ...................................................................... 252  

    10.2.1 Modifying the Linker Command File .............. .................................... .................................... .............. 252 

    10.2.2 Initialization of the ADC .................................................. .................................... ................................. 254 

    10.2.3 Initialization of Timer 1 for Pulse Width Modulation ............................... ................................... ......... 255 

    10.2.4 Initialization of the SPI ........................................................... .................................... .......................... 259 

    10.2.5 Global Data and DFT Routines ............................. ................................... .................................... ......... 261 

    10.2.6 The ADC Interrupt Service Routine ........................................ .................................... .......................... 263 

    10.2.7 I/O initialization ................................ ..................................... .................................... .......................... 265 

    10.2.8 The main() function ................................ ................................... ..................................... ..................... 265 

    10.2.9 Program Execution ......................................................... .................................... ................................. 266 

    10.3 The Fast Fourier Transform (FFT) ............................................................................................................... 267 

    10.3.1 How the FFT works ............................................. .................................... .................................... ......... 268 

    10.3.2 Forward/Inverse FFT................... .................................... .................................... ................................. 272 

    10.3.3 Adding FFT routines in the application.... ..................................... .................................... .................... 274 

    10.3.4 Signal Transposition Routine .................................... .................................... ..................................... .. 275 

    10.3.5 Routines for Forward/Inverse FFT Computations .................................... .................................... ......... 278 

    10.3.6 Modifying the main() function .................................. .................................... ..................................... .. 285 

    10.3.7 Program Execution ......................................................... .................................... ................................. 286 

    10.4 Conclusions/Suggestions ............................................................................................................................ 286 

    11. 

    DIGITAL FILTERS ............................................................................................................. 289 

    11.1 Overview on Digital Filtering ...................................................................................................................... 289 

    11.1.1 Convolution ................................ .................................... .................................... ................................. 289 

    11.1.2 Impulse, Frequency and Step Response of LTI Systems ................................. ..................................... .. 291 

    11.1.3 Implementation of Digital Filters ............................... ................................... ..................................... .. 293 

    11.1.4 Calculating the Output of a FIR filter ................................ ................................... ................................. 294 

  • 8/18/2019 Program TMS320LF2407A

    14/107

     

    xiii

    11.1.5 Calculating the Output of an IIR filter ..... ..................................... .................................... .................... 295

    11.2 Implementing FIR filters ............................................................................................................................. 300 

    11.2.1 Moving Average Filters ................................................................ ................................... ..................... 301 

    11.2.2 Windowed-Sinc Low-Pass Filters..................................... .................................... ................................. 302 

    11.2.3 Windowed-Sinc High-Pass Filters .................................... .................................... ................................. 306 

    11.2.4 Windowed-Sinc Band-Pass Filters ...................................................... .................................... .............. 307 

    11.2.5 Peripheral Initialization..................... ..................................... .................................... .......................... 309 

    11.2.6 Input shifting and Convolution ..................... ..................................... ..................................... ............. 312 

    11.2.7 Putting it all together ................................... .................................... .................................... ............... 313 

    11.2.8 Interrupt Service Routines .............................................. .................................... ................................. 316 

    11.2.9 The main() function ................................ ................................... ..................................... ..................... 317 

    11.2.10 Program execution ....................................................... .................................... ................................. 319 

    11.3 Implementing IIR filters .............................................................................................................................. 326 

    11.3.1 Transforming Continuous Filters into Discrete Filters ................................... ..................................... .. 327 

    11.3.2 Designing a 2nd

     order Low-Pass Butterworth Filter ................................. .................................... ......... 330 

    11.3.3 Designing a 2nd

     order Butterworth High-Pass Filter ................................. .................................... ......... 332 

    11.3.4 Calculating the Output of the filter .......................................................... ..................................... ....... 333 

    11.3.5 Putting it altogether ........................................... .................................... .................................... ......... 335 

    11.3.6 The main() function ................................ ................................... ..................................... ..................... 338 

    11.3.7 Program Execution ......................................................... .................................... ................................. 339 

    11.4 Conclusions/Suggestions ............................................................................................................................ 343 

    12. 

    TOPICS OF SPECIAL INTEREST .................................................................................... 345 

    12.1 Creating C-callable Assembly functions ...................................................................................................... 345 12.1.1 The Components of the C2xx CPU ......................................... .................................... .......................... 345 

    12.1.2 Assembly Language of the LF2407A ................................ .................................... ................................. 348 

    12.1.3 Creating C-callable Assembly function Templates with TI’s Code Composer Studio™ .......................... 351 

    12.1.4 A few Tips for your C-callable Assembly Template .................................. .................................... ......... 367 

    12.2 More about the Watchdog Timer ............................................................................................................... 367 

    12.2.1 Overview of the Watchdog Timer ................................... .................................... ................................. 368 

    12.2.2 Using the Watchdog in a Program ................ ..................................... ..................................... ............. 369 

    Bibliography .................................... .................................... .................................... ..................................... .. 374 

    Sources ............................................................................................................................................................ 375 

    Appendix A: Header and C files for EVA, EVB .................................................................. ..................... 378 

    Structure Definitions (“DSP24_Ev.h” ) ........................................................... .................................... .............. 378 

    Register Declarations (“DSP24_Ev.c” ) .................................. .................................... ..................................... .. 388 

    Appendix B: Header and C files for the LF2407A ISRs .............................................. .......................... 392 

    Declarations of Interrupt Service Routines (“DSP24_DefaultISR.h” ) ............ .................................... ............... 392 

    Implementations of Interrupt Service Routines (“DSP24_DefaultISR.c” ) ....................................................... .. 394 

  • 8/18/2019 Program TMS320LF2407A

    15/107

     

    xiv

  • 8/18/2019 Program TMS320LF2407A

    16/107

     

    xv

  • 8/18/2019 Program TMS320LF2407A

    17/107

     

    xvi

    Preface

    My first experience with the C2x generation of DSP controllers by Texas Instruments

    was with the TMS320F2812™ in the context of a graduate course related to digital signal

    processing. The first impression, for someone who has an average amount of experience in

    embedded programming, is that this product can be looked upon in two ways: Either as a hi-fi

    microcontroller or as a very specialized CPU in terms of certain mathematical operations

    commonly employed in DSP algorithms. My problem was to select a proper course of action

    towards these two directions, in order to broaden my overall knowledge on the DSP

    controller.

    I realized that the best way to make a “smooth” start into understanding the F2812

    was to initially face it as a typical microcontroller and program it in a high-level language (i.e.,

    C) to perform simple or relatively elaborate tasks involving I/O operations. I downloaded the

    tidcs set of C/C++ header file libraries by TI and used several available examples to “break” my

    way through the preliminary steps. During that period, I came across the F2812’s 16-bit fixed-

    point counterpart by Texas Instruments, the TMS320LF2407A™. The two controllers have an

    almost identical set of peripherals and, to program them in C, was essentially a similar task.

    Since the LF2407A did not have a corresponding set of C/C++ libraries, I used the tidcs header

    files as templates to create a set of structures for its memory-mapped registers. During thisprocess, several architectural characteristics of the C24x CPU would gradually emerge and the

    information found in application notes and technical manuals that initially seemed to be

    scattered pieces of a puzzle would eventually start to fall into place.

    This book is a compilation of methods and examples placed in an order that could take

    a student without any prior knowledge on the TMS320™ series by Texas Instruments through

    a fast introduction to C programming with the controller, while several other concepts and

    issues will be gradually addressed in the context of examples. Additionally, the text “points” to

    certain technical manuals and application notes that provide the full extent of the information

    required to comprehend the topic under examination. Assembly language is generally avoided

    until Chapter 12. It is my opinion that one should be already acquainted with the essentials of

    the controller before moving on to optimizing code by making use of the specialized

    instruction set of the C24x CPU core.

    The examples related to DSP (fast Fourier transform, digital filters) in Chapters 10 and

    11 are all implemented in C. These examples merely intend to demonstrate the abilities of the

  • 8/18/2019 Program TMS320LF2407A

    18/107

     

    xvii

    peripherals introduced in the previous chapters, as well as the possibility of optimizing their

    performance by considering the advantages offered by the instruction set of the C24x core.

    To conclude, this book is intended for students who wish to have a fast introduction

    with the TMS320™ series by Texas Instruments, using the TMS320LF2407A™ DSP controller. It

    is a compilation of solutions to problems that usually appear during the first stages of one’s

    “acquaintance” with the DSP controller and its peripherals. Moreover, the code examples

    presented throughout the text are intended to illustrate the basics and provide the incentive

    for further investigation on the issue at hand, rather than to give a “clean-cut” solution to a

    problem with specified parameters. In overall, this book is more-less the introductory course I

    would have chosen for myself in order to understand the essentials of a DSP embedded

    platform and I am hoping that it will serve as an equally useful guide to students and aspiring

    engineers.

    George Terzakis

    [email protected] 

    Bristol, United Kingdom

  • 8/18/2019 Program TMS320LF2407A

    19/107

     

    xviii

  • 8/18/2019 Program TMS320LF2407A

    20/107

     

    xix

    Acknowledgements

    I would like to thank Julie Van Haren, Director of Communications – Embedded

    Processing, Texas Instruments, for her useful advises regarding the proper use of Texas

    Instruments trademarks in literature.

    I would also like to thank Manus and Jo McParland for devoting some of their time in

    making sure that the text is devoid of unnecessary pretentious language and that it is

    articulate and intelligible to the reader.

  • 8/18/2019 Program TMS320LF2407A

    21/107

     

    xx

  • 8/18/2019 Program TMS320LF2407A

    22/107

    INTRODUCTION

    1

    Introduction

    1.1 The TMS320LF/LC240xA™ series of DSP Controllers by Texas Instruments

    The Texas Instruments TMS320LF/LC240xA™ digital signal processor controller is a “nicely

    wrapped” package that includes a high-speed CPU core suitable for digital signal processing

    applications, combined with the typical set of microcontroller peripherals in a single-chip

    package (Figure 1-1). Performances of up to 40 million instructions per second (40 MIPS), is

    one of the greatest assets of the TMS320LF/LC240xA when compared to traditional 16-bit

    microcontrollers. The 240xA core uses 16-bit fixed point arithmetic with a wide variety of

    instructions specifically suited to accommodate the necessary calculations required in digital

    processing applications.

    1.1.1 

    The TMS320LF2407A™ DSP Controller

    TI’s TMS320LF2407A™, henceforth referred to as LF2407A or 2407A throughout this

    book, is the classic representative of the 240xA series. The LF2407A is part of the C24x

    generation and member of the C2000™ platform of fixed point DSPs; the architectural

    similarities of these processors make software highly portable between them, while controller

    peripherals are practically identical in several aspects. Understanding the LF2407A is, in fact,

    the key to “unlocking the secrets” of all other members of the C2000 platform and possibly,

    many other DSP products by Texas Instruments. The broader TMS320™ family includes the

    C1x, C2x, C20x, C24x, C5x, C54x, and C6x generations of fixed-point DSPs; additionally, the C3x

    and C4x floating-point DSPs; finally, the C8x multiprocessor DSPs. Devices within a generation

    are identical in terms of CPU architecture, while they present slight variations in the

    corresponding configurations of memory and peripherals.

    The naming conventions applied in the C code examples throughout this book are

    suited to follow the standards of C libraries of other DSP models in the C2000 platform (i.e.,the TMS320F2812™). You may be surprised to see how easily the code introduced in the next

    chapters will apply to other DSP controllers (such as the TMS320F2812™) with minor

    differences, regardless of CPU architectural incompatibilities (e.g., 16-bit as opposed to 32-

    bit).

    1

  • 8/18/2019 Program TMS320LF2407A

    23/107

    INTRODUCTION

    2

    Figure 1-1. 240xA CPU Core and Peripherals single-chip package1.

    1 TMS320LF/LC240xA DSP Controllers Reference Guide-System and Peripherals (Texas Instruments)

  • 8/18/2019 Program TMS320LF2407A

    24/107

    INTRODUCTION

    3

    1.1.2 

    Peripherals of the LF2407A Controller

    The LF2407A combines the high-performance CPU core with a set of peripherals acting

    as the “heavy artillery” to meet interfacing requirements for the most demanding of problems

    in terms of digital signal processing, communications and general purpose I/O operations. The

    set of peripherals on the LF2407A includes:

    The Event Managers, incorporating Timers and PWM generators.

    The Controller Area Network (CAN) Module.

    The Analog to Digital Converter.

    The Serial Peripheral Interface (SPI) for synchronous serial communications.

    The Serial Communications Interface (SCI) - asynchronous serial port (universal

    asynchronous receiver and transmitter − UART). 

    The Watchdog timer.

    General 

    bi-directional digital I/O (GPIO) pins.

    In the next chapters, each of the above peripherals will be introduced with simple examples

    and several configurations will be discussed. In terms of pure DSP applications, the peripherals

    commonly employed are the event managers and the ADC.

    The event managers. These peripherals include a set of modules to facilitate creation

    of pulse width modulated signals and capture rising/falling edges in pulses. In the “heart” of

    the event managers, lie the general purpose timers, providing clocking to the modules of the

    device; they may also be used to synchronize the occurrence of events in our programs. The

    event managers are highly configurable to the last detail, with an extensive list ofconfiguration/control registers.

    The CAN module. The controller area network module implements the multi-master

    CAN bus communications protocol interface with a set of six mailboxes.

    The Analog to Digital Converter. The ADC is used to sample analog signals and

    produce the corresponding digital value stored in a 10-bit integer result. Arguably, it is one of

    the most significant peripherals on the LF2407A. It utilizes two sequencers as finite state

    machines that synchronize the sampling process.

    The Serial Peripheral Interface. The SPI is used for synchronous master-slave high

    speed serial communications. Typically, the SPI is used for communications with external

    devices such as LCDs and Digital to Analog Converters.

    The Serial Communications Interface. The SCI implements typical asynchronous serial

    communications (UART). Typical applications of the SCI include communications with other

  • 8/18/2019 Program TMS320LF2407A

    25/107

    INTRODUCTION

    4

    controllers, or a PC. The designated lines for reception (RX) and transmission (TX) are not

    level-shifted on the DSP board (i.e., they operate at 0-3.3V).

    The Watchdog Timer. The watchdog is essentially a timer, acting as a safety precaution

    against possible program locks in endless loops. When enabled, the watchdog increases an

    internal 8-bit counter using a clocking signal running at a sub-multiple frequency of the CPU

    clock signal. The program should be able to reset the counter before an overflow occurs; if, for

    any reason (which may possibly be an execution “hung”), the program fails to reset the

    watchdog in time, the counter will overflow and a system reset will be asserted.

    The General bi-directional I/O pins. The LF2407A has a set of general I/O pins

    organized in ports A, B, C, D, E and F. Most of the I/O pins on the LF2407A are multiplexed

    with other devices (e.g., general I/O pin A6 is multiplexed with the PWM1 pin) and must be

    configured prior to use, either for their primary (non - general I/O) or secondary (general I/O)

    function. Moreover, general I/O pins can be configured either as input or output.

    1.2 

    Programming the LF2407A in C

    Traditionally, the 2407A does not come with C header files regarding its set of

    peripherals. Most of the existing code examples are developed in assembly. There are a great

    number of reasons why a DSP should be programmed in assembly and all of them have to do

    with performance in terms of execution time. However, using a high level language to initialize

    the required controller devices in a program should not interfere with performance, but

    rather only with the program size, which should not be a major issue these days.

    The following chapters take the reader throughout all peripherals on the 2407A with a

    few simple examples developed in C regarding each case. Since there are no libraries to work

    with, the text will guide the reader through the process of creating a set of header files

    involving the registers of the corresponding peripherals. It may be extra work, but it is

    advantageous; the process of creating these files can be a nice exercise for memorizing and/or

    highlighting the names of registers related to the initialization of every device on the DSP.

    Moreover, the naming conventions for typical header files, registers and functions (interrupt

    service routines and system initialization functions) are chosen so that they match the ones

    used in the tidcs library provided by Texas Instruments for the TMS320F2812. Given that theDSP controllers of the C2000 platform (and, possibly, ones that belong to other generations

    and platforms) share most of their peripherals, you will find that your code can be easily

    portable to other controllers with very few modifications. As mentioned earlier, the LF2407A

    is the gateway to understanding a broader set of DSP products by Texas Instruments. The

    overall gain is that in the end of the day, the reader should have a very good notion of what

    kind of problems one may be faced with, regarding initialization and use of peripherals, not

  • 8/18/2019 Program TMS320LF2407A

    26/107

    INTRODUCTION

    5

    only on the LF2407A, but on other DSPs, while building a useful set of code templates for

    his/her programs.

    The demonstration examples (fast Fourier transform, FIR and IIR filters) are all purely

    implemented in C; they are intended to demonstrate merely a fraction of the issues that an

    engineer/student may be faced with, when trying to implement the relative algorithms for

    commercial use. Different approaches in the implementation are recommended and

    discussed; including the use of C-callable assembly code to improve performance by taking

    advantage of the LF2407A specialized instruction set in terms of typical DSP computations.

    Regardless of whether the programs presented in this book are ideally suitable for solutions to

    many problems in the field, they merely serve as an introduction to the relative topics. The

    main goal is to suggest and discuss solutions in a comprehensive and coherent manner for the

    reader, without expecting prior extensive knowledge, either on embedded programming or

    signal processing in general.

    1.3 

    What you need in order to read this book

    Reading through the first nine chapters does not require extended programming and/or

    digital signal processing skills. However, a minimum acquaintance with C language and

    computer architecture will be the prerequisites for the early chapters at least. Every new

    concept comes with an example, following a short discussion on the issues presented in that

    example. Readers who may have a certain experience in embedded programming will

    probably have a head-start; the concepts introduced here, are somehow “deviant” from the

    typical programming scheme in which, for example, a computer science student may be

    accustomed to and it may take a while until he/she becomes fully adjusted. Finally, a certain

    background on mathematics and control theory will be required for Chapters 10 and 11. It is

    advisable to consult and cross-check every new concept with the technical manuals provided

    by Texas Instruments and with the literature presented in the bibliography and sources

    sections of this book.

  • 8/18/2019 Program TMS320LF2407A

    27/107

    INTRODUCTION

    6

  • 8/18/2019 Program TMS320LF2407A

    28/107

    WRITING AND EXECUTING PROGRAM “HELLO”  

    7

    Writing and Executing Program “hello” 

    2.1 Creating a first project with Code Composer Studio™ 

    We better quickly jump-start your interest with the 2407A with the old-fashioned

    “hello” program. In fact, the programming approach involves the all times classic code

    structure; we have a main() function, a few variables and a main loop. Sounds familiar? Start

    Code Composer Studio™ IDE by Texas Instruments and create a new project.

    Figure 2-1. Creating a new project.

    In the first step you will be asked to name the project, choose a suitable folder and a target.Type a name (i.e. “hello”) and create a folder in windows explorer to store your project.

    Preferably, create your folder without any spaces or special characters and make sure the

    path does not include extended names (e.g., “Documents and Settings” ); the linker version

    may have an issue with these kinds of names and it may produce errors without any apparent

    reason. Choose “TMS320C24XX” as target.

    Figure 2-2. One step away from project “hello”. 

    2

  • 8/18/2019 Program TMS320LF2407A

    29/107

    WRITING AND EXECUTING PROGRAM “HELLO”  

    8

    2.1.1 Creating the C file(s)

    Like in any other C/C++ IDE, you will have to create your .c or .h files and thereafter,

    add them to the project. Let us create a file named “hmain.c”  from File New Source File. If

    all went nicely, you should be able to work on a new editor window for the file you just

    created. Type the following code:

    #define MCRA (volatile unsigned  int *)0x7090#define PADATDIR (volatile unsigned  int *)0x7098#define WDCR (volatile unsigned  int *)0x7029#define SCSR2 (volatile unsigned  int *)0x7019

    void  main() {

    unsigned  int i, temp, temp1;

    *SCSR2 = (*SCSR2 | 0x000B) & 0x000F;

    *WDCR = 0x00E8; // disable the watchdog

    *MCRA &= 0xFFFC; // set Port A General Pins 0 and 1// to General I/O function

    *PADATDIR|=0x0303; // Port A0, A1 bits output and 'high'/* main loop */

     while (1) {for(i=0;i

  • 8/18/2019 Program TMS320LF2407A

    30/107

    WRITING AND EXECUTING PROGRAM “HELLO”  

    9

    defined inside a linker command file (extension, .cmd ). The structure of a command file is

    simple, but choosing the locations to store your code and variable sections can be very

    tricky. Since no one has the spare time to go through technical manuals every time he/she

    needs to develop a program, a good policy would be to save your first .cmd  file and then

    either reuse it verbatim, or make slight changes to fit your new project’s requirements. Here’sa command file that will be used throughout many examples in this text.

     MEMORY{PAGE 0: /* Program Memory */ VECS: org=00000h, len=00040h /* internal FLASH */FLASH: org=00044h, len=07FBCh /* internal FLASH */PSARAM: org=08000h, len=00800h /* internal SARAM */EXTPROG: org=08800h, len=07600h /* external SARAM */

    PAGE 1: /* Data Memory */B2: org=00060h, len=00020h /* internal DARAM */

    B0: org=00200h, len=00100h /* internal DARAM */B1: org=00300h, len=00100h /* internal DARAM */DSARAM: org=00800h, len=00800h /* internal SARAM */EXTDATA: org=08000h, len=08000h /* external SARAM */

    }

    SECTIONS{/* Sections generated by the C compiler */

    .text: > EXTPROG PAGE 0 /* initialized */.cinit: > EXTPROG PAGE 0 /* initialized */.const: > B1 PAGE 1 /* initialized */.switch: > EXTPROG PAGE 0 /* initialized */

    .bss: > EXTDATA PAGE 1 /* uninitialized */

    .stack: > DSARAM PAGE 1 /* uninitialized */.sysmem: > B1 PAGE 1 /* uninitialized */

    /* Sections declared by the user */

    /* vectors: > VECS PAGE 0  */ /* initialized */

    In File New Source File create a file named “ 2407  _cmd.cmd”  and save it in folder “hello” .

    Notice highlighted section vectors mapped to VECS in PAGE 0; the allocation is commented-

    out. As stated in the comments, this section is NOT created by the C compiler, still it should be

    included in most applications, since it contains the core interrupt vector table of the 2407A;

    for now, keep it cased in comments. We will be going through the details of this command file

    shortly after we build and execute our project.

    Library file “ rts2xx.lib” . Although upon the creation of the project we specifically

    stated a target (i.e., TMS320CXX ), this is not enough for the compiler to produce target-

    specific object files. File “rts2xx.lib”  must always be added to the project when C language is

  • 8/18/2019 Program TMS320LF2407A

    31/107

    WRITING AND EXECUTING PROGRAM “HELLO”  

    10

    used, otherwise the compiler will not know how to generate assembly instructions, allocate

    variables, emulate floating point arithmetic, perform data and program addressing, etc. You

    should be able to locate “rts2xx.lib”  in “./C2400/cgtools/lib”  starting from the Code Composer

    Studio™ root directory (usually, “ C:\CCStudiocx.x ”).

    2.1.3 Putting it all together

    So, now we have a C file, a linker command file and a library file to add to the project.

    Just go Project Add Files to Project , or right-click on the project’s name in the project browser

    window floating on the left side of the IDE and choose Add Files to Project .

    Figure 2-3. Adding files to the project.

    Make sure to select the appropriate extension every time. The IDE will distribute .cmd  and .c 

    (as well as .asm, if any) under the Source tree node, whereas the .lib can be found under

    Libraries. If all went okay, your project browser window should look like the one in Figure 2-4.

    Do not forget to save your project using menu Project Save.

    Figure 2-4. Project “hello” browser window; .cmd , .c , .lib files added.

  • 8/18/2019 Program TMS320LF2407A

    32/107

    WRITING AND EXECUTING PROGRAM “HELLO”  

    11

  • 8/18/2019 Program TMS320LF2407A

    33/107

    REFINING PROGRAM “HELLO” 

    28

    Refining program “hello” 

    3.1 Beginning of execution

    Let us load our program again to the DSP. Following download, the instruction pointer

    moves right at the beginning of the cinit  section in program memory as shown in the

    disassembly window in Figure 3-1.

    Figure 3-1. Disassembly window after downloading “hello.out ” .

    Now, let us try something new. Go Debug Reset CPU and check the disassemblywindow again (Figure 3-2).

    Figure 3-2. The Disassembly Window after a CPU Reset.

    Something very strange happened! Although your program is still in memory, the DSP

    chose to ignore the memory location of the cinit  section; instead, the instruction pointer now

    3

  • 8/18/2019 Program TMS320LF2407A

    34/107

    REFINING PROGRAM “HELLO” 

    29

    moved to address 0x0000 in program memory containing an ADD instruction. Try Debug Go

    Main now. Oops! No main()? You can obviously tell that the program does not suspend

    execution at the beginning of the main() function as you may have expected. In fact, the DSP is

    still running! You can verify this by checking the Debug menu. All execution menu items are

    inactive, except for Halt. Hit Halt  and let us go through what just happened.

    3.1.1 The Reset interrupt Vector

    If no boot ROM is present, then, following a reset, the DSP loads address 0x0000

    (program memory) to the instruction pointer. If you take a good look at Figure 2-15, this is the

    first memory location corresponding to the core interrupt vectors. Address 0x0000 is the

    location for the reset  vector and it should contain a branching instruction (jump) to

    whatever you want the DSP to do immediately after reset. I think you guessed right. It

    should jump to the cinit  section. Remember the line cased in comments in the SECTIONS 

    clause in “2407_cmd.cmd ” ? By using the comments, we have excluded the vectors section

    from being loaded to program memory; hence, when the DSP resets, it looks for a branching

    instruction at 0x0000, but instead, it finds a random data residue interpreted as an instruction

    (an ADD in this case); consequently, execution “hangs” since the DSP is executing random

    instructions in memory.

    Go File New Source File and create a new file. Save it as “cvectors1.asm” . Do not

    worry, this is as much as you will be dealing with assembly. Unfortunately, this is the only way

    to add a section to your C program; so type the following:

    .ref _c_int0

    .sect  "vectors"rset: B _c_int0 ;00h reset 

    Make sure you apply indentation as you see it above; the assembler may have problems if you

    do not. The “.” prior to the “ref ”  and “sect ”  keywords, implies that these strings are assembler

    directives, not instructions. The .ref  directive is used to imply that symbol  _c_int0  is

    externally defined. Actually, this is the symbol created by the C compiler for the cinit

    section. Keep always in mind that when referencing symbols created in C from an assembly

    file, the “_” character should be added prior to the symbol. The .sect directive creates a newsection. Notice now the first line in section “vectors”. It contains an assembly instruction for

    an unconditional branch to the _c_int0 code (B _c_int0). That is exactly what we need! So

    now, after a reset, the DSP will be executing a “jump” to the program’s cinit  section and things

    can start all over again without any unwanted “hangs”. 

  • 8/18/2019 Program TMS320LF2407A

    35/107

    REFINING PROGRAM “HELLO” 

    30

    Add “cvectors1.asm”  file to the project. Then, open the command file and remove the

    comments from the line loading the vectors section in memory as shown below:

    vectors: > VECS PAGE 0 /* initialized */ 

    Your project browser window should look like the one shown in Figure 3-3. Save the updated

    command file and build your project.

    Figure 3-3. Project browser window following addition of “cvectors1.asm”.

    Load the program into the DSP and do a Go Main. Now, do a Reset CPU and observe what

    happens. The debugger takes you to file “cvectors1.asm”  and suspends execution right beforethe jump to _c_int0 as shown in Figure 3-4.

    Figure 3-4. Execution suspension (break) following a reset.

  • 8/18/2019 Program TMS320LF2407A

    36/107

    REFINING PROGRAM “HELLO” 

    31

    If you now do a Go Main, you can rest assured that execution will be suspended right at the

    beginning of the main() function. Do a few resets just to raise the spirit seeing how nicely it

    works!

    3.2 Completing the “cvectors.asm” file

    So far, we have ensured that the program will restart “smoothly” following a reset. But

    this is not the only reason why we need to have a “cvectors.asm” file. In fact, “cvectors.asm”  

    should include the entire interrupt vector table with unconditional branches to all interrupt

    service routines.

    3.2.1 The LF2407A Core Interrupts

    The 2407A is capable of six (6) maskable interrupts and several software (TRAPS) and

    non-maskable interrupts (NMI). Practically, your code will be dealing with the six maskable

    core interrupts (INT1-6), with the extreme exception of the occasional use of software

    interrupts. The first 6 interrupts (except INT0 which is the reset  interrupt vector) correspond

    to the peripherals of the 2407A through a peripheral interrupt expansion controller  (will be

    discussed later), and it is important that the branching instructions in “cvectors.asm”  are

    pointing to the appropriate interrupt service routines (ISR) implemented in the C code.

    Depending on the program specifications, you may need to implement less than six ISRs, but it

    is a good policy to keep a set of 6 routine definitions in your code, so that you will not have to

    modify the “cvectors.asm”  every once in a while. Let us first conclude the “cvectors1.asm”  file.

    Open the editor and type the following:

    .ref _c_int0

    .ref _INT1_GISR

    .ref _INT2_GISR

    .ref _INT3_GISR

    .ref _INT4_GISR

    .ref _INT5_GISR

    .ref _INT6_GISR

    .sect  "vectors"rset: B _c_int0 ;00h resetint1: B _INT1_GISR ;02h INT1int2: B _INT2_GISR ;04h INT2int3: B _INT3_GISR ;06h INT3int4: B _INT4_GISR ;08h INT4int5: B _INT5_GISR ;0Ah INT5int6: B _INT6_GISR ;0Ch INT6int7: B int7 ;0Eh reservedint8: B int8 ;10h INT8 (software)int9: B int9 ;12h INT9 (software)int10: B int10 ;14h INT10 (software)int11: B int11 ;16h INT11 (software)int12: B int12 ;18h INT12 (software)

  • 8/18/2019 Program TMS320LF2407A

    37/107

    REFINING PROGRAM “HELLO” 

    32

    int13: B int13 ;1Ah INT13 (software)int14: B int14 ;1Ch INT14 (software)int15: B int15 ;1Eh INT15 (software)int16: B int16 ;20h INT16 (software)int17: B int17 ;22h TRAPint18: B int18 ;24h NMIint19: B int19 ;26h reservedint20: B int20 ;28h INT20 (software)int21: B int21 ;2Ah INT21 (software)int22: B int22 ;2Ch INT22 (software)int23: B int23 ;2Eh INT23 (software)int24: B int24 ;30h INT24 (software)int25: B int25 ;32h INT25 (software)int26: B int26 ;34h INT26 (software)int27: B int27 ;36h INT27 (software)int28: B int28 ;38h INT28 (software)int29: B int29 ;3Ah INT29 (software)int30: B int30 ;3Ch INT30 (software)int31: B int31 ;3Eh INT31 (software)

    Save the file now as “cvectors.asm” . Notice the first seven .ref directives. They inform thecompiler that symbols _c_int0, _INT1_GISR, _INT2_GISR, _INT3_GISR, _INT4_GISR, _INT5_GISR

    and _INT6_GISR will be externally defined (meaning somewhere in your C code). Keep in mind

    that if you refer to a symbol defined in C inside an assembly file, you should always add a

    “_” prefix to the reference (i.e., _c_int0 refers to c_int0, _INT1_GISR refers to INT1_GISR,

    etc.). As mentioned earlier, the _c_int0 corresponds to a function named “c_int0”

    automatically generated by the C compiler. The rest are yet to be implemented, but should be

    defined and declared somewhere in the project code, otherwise the linker will not be able to

    resolve them and eventually fill your output window with dreadful errors!

    3.2.2. Adding Declarations for Core Interrupt Service Routines 1-6 in C

    Create a new C header file named “ DSP24_DefaultISR.h”  and type the following:

    #ifndef DSP24_DEFAULT_ISR_H#define DSP24_DEFAULT_ISR_H

    interrupt void  INT1_GISR(void );

    interrupt void  INT2_GISR(void );

    interrupt void  INT3_GISR(void );

    interrupt void  INT4_GISR(void );

    interrupt void  INT5_GISR(void );

    interrupt void  INT6_GISR(void );

    #endif 

  • 8/18/2019 Program TMS320LF2407A

    38/107

    REFINING PROGRAM “HELLO” 

    33

    Save the changes you made. The compiler can resolve the symbols directly from the .c files;

    therefore, the header file is not really required. However, it is best that you keep it, since it

    could be used for constant definition and additional include directives. The interrupt keyword

    informs the compiler that the function that follows is an Interrupt service function; thus, the

    compiler adds context saving and retrieving specific code (storing/retrieving flags, registervalues, etc.) at the beginning and the end of the function respectively. The C compiler makes

    sure that you will not have to worry much about “context saving” , if you only remember to

    use the interrupt keyword. Notice the names of the routines (INT1_GISR,…,INT6_GISR). These

    are the symbols referred to in the “cvectors.asm”  file that we modified a while ago; now the

    “_” prefix is omitted since we are typing C code. Don’t worry about the implementation of

    these functions yet. As long as the linker can locate these symbols referred to in

    “cvectors.asm” , your project building will be successful. Now, create a new file named

    “DSP24_DefaultISR.c”  for the declaration of the interrupt service routines referred in

    “cvectors.asm” . Type the following:

    #include "DSP24_DefaultISR.h"

    interrupt void  INT1_GISR(void ) {}

    interrupt void  INT2_GISR(void ) {}

    interrupt void  INT3_GISR(void ) {}

    interrupt void  INT4_GISR(void ) {

    }

    interrupt void  INT5_GISR(void ) {}

    interrupt void  INT6_GISR(void ) {}

    Although it may seem unimportant for the time being, try to adopt the naming

    conventions used in this text for your files. For example, “DefaultISR”  is a name used

    commonly in projects for other DSP models (such as TI’s TMS320C2812/F2812™). Most

    developing principles and methods mentioned in this text may well apply with slight

    modifications to other DSP products by Texas Instruments. Moreover, it is important that

    you recognize the role of a file in a project simply by its name. You may find that most

    programs are portable from one DSP model to another with much less effort than you

    imagined.

  • 8/18/2019 Program TMS320LF2407A

    39/107

    USING INTERRUPTS AND THE GENERAL PURPOSE TIMERS

    46

    Using Interrupts and the General Purpose Timers

    4.1 A brief introduction to Interrupts of the 2407A

    For those of you acquainted with event driven programming in Unix or other operating

    systems, you surely realize the importance of handling asynchronous events in a program.

    Think of interrupts as special events, normally triggered by external sources involving

    peripherals. Depending on the case, you may choose to ignore these signals; however, in

    other cases, you may choose to “interrupt” the sequential flow of execution and branch code

    execution to a special function that handles the event that triggered the interrupt. The special

    routines that handle interrupt signals are usually called interrupt handlers, but you will findthe term interrupt service routines (ISR) rather more commonly used. The 2407A is able to 

    “sense” numerous interrupt sources, mainly related to its peripherals.

    4.1.1 The Peripheral Interrupt Expansion Controller (PIE)

    The 2407A acknowledges interrupts in two levels. The core itself provides six maskable

    interrupts (INT1-6). Technically, each of those interrupts may correspond to one specific

    source. When programming interrupts for a PC, we know that there is a one-to-one mapping

    from a peripheral interrupt source to a core interrupt in the CPU (i.e., the Intel x86 has 256

    core interrupt vector table entries). Unfortunately, we do not have the luxury of using apowerful Pentium™ on our DSP board. On the other hand, the DSP should be able to serve up

    to 49 interrupt sources, most of which correspond to its peripherals. To overcome the

    problem of having a great number of hardware interrupts (as opposed to the six available

    maskable core interrupts) to be served by the CPU, these interrupts are organized in groups 

    or levels, each one corresponding to one of the six core maskable interrupts (INT1-6). This is

    actually where the peripheral interrupt expansion controller (PIE) kicks-in. The PIE  

    “intercepts” interrupt signals from the various peripherals and consequently triggers the

    appropriate core interrupt. It is as simple as that: Every maskable core interrupt in the 2407A,

    is “responsible” for several interrupt sources. How is this happening? Better see this throughan example and fill in the details as we go.

    4

  • 8/18/2019 Program TMS320LF2407A

    40/107

    USING INTERRUPTS AND THE GENERAL PURPOSE TIMERS

    47

    Figure 4-1. The Peripheral Interrupt Expansion scheme of the 2407A1.

    4.2 Using a General Purpose Timer

    The LF2407A is equipped with four general purpose timers (timer 1-4), all identical in

    every aspect. For those already acquainted with embedded programming, it is common

    knowledge that if you want your program to “do business”, you must always have at least one

    1 TMS320LF/LC2407A DSP Controllers Reference Guide –  System and Peripherals (Texas Instruments)

  • 8/18/2019 Program TMS320LF2407A

    41/107

    USING INTERRUPTS AND THE GENERAL PURPOSE TIMERS

    48

    timing source to synchronize events. “Time awareness” and “periodic execution” are

    important factors to consider when controlling a motor, polling sensors or filtering signals.

    4.2.1 Project “Timer1”  

    Create a new project folder named “ Timer1” . Copy files “cvectors.asm” ,“2407_cmd.cmd” , “DSP24_Sys.c” , “DSP24_DefaultISR.h”  and “DSP24_DefaultISR.c”  to the new

    folder and create a new project named “Timer1”. Now, create a new file named “T1main.c”  

    and type-in the following code:

    extern void  initSystem(void );extern void  initTimer1(void );

    void  main() {

    initSystem();initTimer1();

    asm(" CLRC INTM"); // Enable Global Interrupts 

     while (1) {}

    }

    This is an even smaller main() function than the one in program “hello” ! Notice

    externally implemented function initTimer1() called right after system initialization. This

    function will start the timer and enable the desired interrupts.

    Notice the inline assembly instruction asm(“ CLRC INTM”). If you are using interrupts

    in your program, you must use this instruction to enable them. By default, following a reset,

    interrupts are disabled. The DSP has a global interrupt switch (INTM) that disables/enables

    interrupts. You will realize that it comes very handy in many occasions in our programs. You

    may disable interrupts using inline assembly instruction asm(“ SETC INTM”).

  • 8/18/2019 Program TMS320LF2407A

    42/107

    THE EVENT MANAGERS

    64

    The Event Managers

    5.1 Overview on the Event Managers

    As mentioned earlier, the 2407A is equipped with event managers A and B (EVA and

    EVB). EVA and EVB are identical in every aspect; for simplicity reasons, we will be going

    through the characteristics of EVA. EVA includes two general purpose timers (timer 1 and

    timer 2), three compare units, three capture units and one quadrature encoder pulse (QEP) 

    circuit .

    Figure 5-1. Block diagram of Event Manager A1.

    1 TMS320LF/LC240xA DSP Controllers Reference Guide –  System and Peripherals (Texas Instruments)

    5

  • 8/18/2019 Program TMS320LF2407A

    43/107

    THE EVENT MANAGERS

    65

    The general purpose timers. We have already made our first acquaintance with the

    general purpose timers. So far, we have only used them to produce periodic events (period

    match, compare and underflow interrupts). In the context of the event manager, timers may

    be additionally configured to produce pulse width modulated signals on pins TxCMP/TxPWM 

    (x=1,2,3,4). Throughout the present chapter, we will be using timer 1 to produce a PWMsignal. Moreover, you should think of timers as the “life force” of the event managers,

    providing clocking to other units, such as the compare units (Figure 5-1).

    The compare units. The three compare units (per event manager) can be specifically

    used for PWM signal production on pins PWM1, PWM2, PWM3, PWM4, PWM5 and PWM6 in

    the case of EVA and PWM7, PWM8, PWM9, PWM10, PWM11, PWM12 for EVB. They are

    clocked by timer 1 (EVA) and timer 3 (EVB) and they can be configured to produce pulses in

    pairs of pins (e.g., PWM1 and PWM2). The compare units can use a special dead-band unit as

    an accessory to produce artificial “dead-bands” for “active high”-“active low” PWM pin pairs. 

    The capture units. Capture units may come-in very handy when trying to “capture”

    pulses of external origin. Such an example could be the bounce-back pulse from a sonar

    transducer, or a series of pulses from a rotary motor encoder. In fact, this is probably what the

    designers of the event managers had in mind when they were designing capture units.

    Actually, to accommodate our needs in terms of capturing motor encoder pulses, we may

    configure the quadrature encoder pulse circuit for quadrature motor encoder pulse capturing

    to determine both rotation speed and direction.

    Concluding, event managers comprise several interconnected units to accommodate

    the numerous specific needs that may arise when controlling a motor and/or when receiving

    feedback in the form of pulses. EVA and EVB are highly configurable to the last detail;

    therefore, they involve a large set of configuration and control registers. Along with the CAN

    controller, they are the most complicated peripherals on the 2407A. The large number of

    registers to deal with may give you a headache, but it is something that can be dealt with,

    mainly by properly organizing your header files and functions.

    5.1 Using Timer 1 for Pulse Width Modulation

    Let us create a new project folder named “t1pwm” . Copy all source files from “Timer1”  folder into the new one, including the linker command file “2407_cmd.cmd”  and assembly file

    “cvectors.asm” . Now, go to folder “hello” and copy files “DSP24_Gpio.h”  and “DSP24_Gpio.c”  

    to your new folder (since we will be using an I/O pin, we should make sure that this particular

    pin is configured for PWM operation with the appropriate I/O mux register defined in

    “DSP24_Gpio.h” ). Now, create a new project “t1pwm” . Add all files to your new project, and

    don’t forget to add “rts2xx.lib”  after you finished.

  • 8/18/2019 Program TMS320LF2407A

    44/107

    THE EVENT MANAGERS

    66

    5.1.1 Expanding “DSP24_Ev.h”  

    Open “DSP24_Ev.h” . In order to use timer 1 to produce a PWM signal, we will have to

    configure additional registers. Add the following structure definitions:

    // General Purpose Timer CONtrol register A (GPTCONA)struct GPTCONA_BITS {

    unsigned  int T1PIN:2; // 0-1 Polarity of GP timer 1 compareunsigned  int T2PIN:2; // 2-3 Polarity of GP timer 2 compareunsigned  int rsvd1:2; // 4-5 Reservedunsigned  int TCOMPOE:1; // 6 CoMPare Output Enableunsigned  int T1TOADC:2; // 7-8 Start ADC with Timer 3unsigned  int T2TOADC:2; // 9-10 Start ADC with Timer 4unsigned  int rsvd2:2; // 11-12 Reservedunsigned  int T1STAT:1; // 13 Timer 3 STATusunsigned  int T2STAT:1; // 14 Timer 3 STATusunsigned  int rsvd3:1; // 15 reserved

    };

    union GPTCONA_REG {unsigned  int  all;struct  GPTCONA_BITS bit;

    };extern volatile union GPTCONA_REG *GPTCONAbits; 

    The event managers offer the “luxury” of configuring certain actions upon timer-related

    events. Actually, we may use a GP Timer to produce PWM on the T1PWM/T1CMP pin, or even

    start the analog to digital converter  (ADC). Configuration of these actions (and several others)

    can be done through the appropriate general purpose timer control register  (i.e., GPTCONA).

    Obviously, there are two general purpose timer control registers, GPTCONA (EVA) andGPTCONB (EVB).

    Now, open “DSP24_Ev.h”  and add the declaration for GPTCONA as follows:

    #include "DSP24_Ev.h"

    volatile unsigned  int *T1CNT=(void *)0x7401; /* GP timer 1 counter reg */volatile unsigned  int *T1CMPR=(void *)0x7402; /* GP timer 1 compare reg */volatile unsigned  int *T1PR=(void *)0x7403; /* GP timer 1 period reg */

    // GPTCONAvolatile union GPTCONA_REG *GPTCONAbits=(void *)0x7400;

    //T1CONvolatile union T1CON_REG *T1CONbits=(void *)0x7404;

    // EVAIMRAvolatile union EVAIMRA_REG *EVAIMRAbits=(void *)0x742C;

    // EVAIFRAvolatile union EVAIFRA_REG *EVAIFRAbits=(void *)0x742F; 

    GPTCONA declaration 

  • 8/18/2019 Program TMS320LF2407A

    45/107

  • 8/18/2019 Program TMS320LF2407A

    46/107

    THE EVENT MANAGERS

    68

    bit definitions for all four timers in EVA and EVB. Although in project “Timer1” , we did not

    configure the general purpose timer register, it should be a solid policy to configure it, even if

    you do not intend to use pulse width modulation on the T1PWM/T1CMP pin.

    5.1.3 General Purpose Timer Control Register A

    The general purpose timer control register   A (GPTCONA) bitwise outline is shown in

    Figure 5-2.

    Figure 5-2. Register GPTCONA bits2.

    The T1PIN0-1 bits. Bits 0 and 1 in GPTCONA configure the polarity of the timer 1

    compare pin. We choose active high (“10”). Check the manual for more options.

    The T2PIN0-1 bits. Bits 2 and 3 in GPTCONA configure the polarity of the timer 2

    compare pin. It is configured as f orced l ow”  (“00”), since timer 2 does not concern this

    example.

    The TCMPOE bit . Bit 6 in GPTCONA enables timer 1 and timer 2 compare outputs.

    Obviously, you should set this bit to “1” if you want to get a pulse out of the compare pins. 

    The T1TOADC0-1 bits. Bits 7-8 in GPTCONA define the actions of timer 1 events upon

    the analog to digital converter . As mentioned earlier, the ADC may be forced to start its

    conversion operation by a GP Timer. Since we are not using the ADC, these bits are set “00” 

    (no event triggers ADC). Check your manual for more options.

    The T2TOADC0-1 bits. Bits 9-10 in GPTCONA define the actions of timer 1 events upon

    the analog to digital converter . The ADC conversion sequence may be triggered by events

    related to timer 2. Again, since we are not using the ADC, these bits are set “00” (no event

    triggers ADC).

    2 TMS320LF/LC240xA DSP Controllers Reference Guide –  System and Peripherals (Texas Instruments)

  • 8/18/2019 Program TMS320LF2407A

    47/107

    THE EVENT MANAGERS

    69

    The T1STAT bit . Bit 13 in GPTCONA is a read -only bit providing information on the

    timer counter status (“0” – counting downward, “1” – counting upward).

    The T2STAT bit . Same as bit 14 for timer 2.

    5.1.4 I/O Pin Configuration

    Never forget that most I/O pins on the DSP are multiplexed. Specifically, the

    T1PWM/T1CMP pin can either be T1CMP, or just another general I/O pin (i.e., B4). It is

    important to configure your I/O before using it . Normally, I/O should be configured after the

    system initialization sequence. The best way is to create a function initIO(). Let us append file

    “DSP24_Sys.c” . Open the file, and add the following function:

    void  initIO(void ) {

     MCRAbits->bit.T1PWMT1CMP_GPIOB4=1; // T1CMP pin set to PWM

    A “1” is written to the appropriate bit-field of structure MCRAbits in order to configure pin

    T1PWM/T1CMP/GPIOB4 as a timer 1 output (primary function). This is the first time we are

    accessing MCRAbits from “DSP24_Sys.c”, therefore you should add an include statement right

    at the beginning of the file:

    #include "DSP24_Gpio.h" 

    Save the file. Now it is time to make a few changes in “T1main.c”  and we will be ready to go!

    5.1.5 The main() function 

    The modifications required for main() are the addition of a call to initIO(), as well as the

    external declaration of the function. Modify the code as follows:

    extern void  initSystem(void );extern void  initTimer1(void );

    extern void  initIO(void );

    void  main() {

    initSystem();

    initIO();

    initTimer1();

    asm(" CLRC INTM"); // enable Interrupts while (1) {

    External declaration

    of initIO()

    Call to initIO() right after

    System Initialization

  • 8/18/2019 Program TMS320LF2407A

    48/107

    THE EVENT MANAGERS

    70

    }} 

    Save all changes to your files and to the project. The project browser window should now look

    like the one in Figure 5-3.

    Figure 5-3. Project browser window for “t1pwm”. 

    Do a Rebuild all  and download the program to the DSP. Our next task is to verify the PWM

    signal from the T1PWM/T1CMP pin. So, get your oscilloscope ready.

    5.1.6 Observing the Pulse Width Modulated signal on the T1PWM/T1CMP pin

    Before we run the program, we will have to physically locate the T1PWM/T1CMP pin

    on the DSP board. Figure 5-4 shows what you can also discover in the eZdsp™ LF2407A–  

    Technical Reference manual by Spectrum Digital Incorporated.

    Figure 5-4. P2/P8 Header connections (T1PWM is 15 and GND is 40)3.

    3 eZdsp™ LF2407A–  Technical Reference (Spectrum Digital)

  • 8/18/2019 Program TMS320LF2407A

    49/107

    THE EVENT MANAGERS

    71

    You may now connect your oscilloscope probe to pin 15. Reference ground is located on pin

    40 (you may alternatively use pin 39) in the header.

    If your program is already loaded in the DSP, do a CPU Reset and a Go Main

    afterwards. R