CPLD Tutorial


Page 1 of 16 123411 ... LastLast
Results 1 to 20 of 305

Thread: CPLD Tutorial

  1. #1
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default CPLD Tutorial

    Quote Originally Posted by acondit View Post
    Thanks Mariss,

    It appears that there used to be a post on the xilinx site showing how to construct an oscillator but it appears that they have removed it. There are still links to such a post but the go to a 404 error.

    As usual you seem to be the man with the answers.

    Thanks again,
    Alan
    Alan,

    The way I use CPLDs is a little different than the way they are intended to be used. An XC2C32A CPLD can run with a 50MHz to 100MHz clock. It is intended to be a replacement for fast 'glue' logic and many if not most applications utilize this kind of speed.

    Designing a microstep drive or a servodrive doesn't require and cannot use anywhere near that kind of clock speed. 5MHz or less is plenty and clock frequency accuracy is of little consequence. We get +/-5% over temperature and supply voltage tolerances. That is why an RC Schmidt inverter oscillator is more than adequate.

    I chose the CoolRunner II for characteristics that are vital to me. Foremost is zero DC supply current. Its supply current is a linear function of clock frequency and at 5MHz it's not much (100uA or less). Other CPLDs draw 100mA or more and that is a deal-breaker in my applications. Second, the CoolRunner II has 'true' CMOS inputs. They can be set to 'float' and 'Schmidt' which makes them ideal for quasi-analog inputs. Also the device and its development system is intuitive to use and free of vices.

    Finally, it turns out 32 and 64 macrocells are sufficient (barely, but just sufficient) to squeeze out fairly sophisticated designs. The QFN package (QFN32 and QFN48) accommodates only the 32 and 64 macrocell parts. The 128, 256 and larger macrocell parts come in enormous VQFP-100 and VQFP-144 or BGA packages. I cannot use such large parts owing to product size constraints and I'm unwilling to use BGA because of the package expense and the inability to visually inspect solder connections.

    Mariss

    Similar Threads:


  2. #2
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default CPLD Tutorial

    The present Tutorial in this link.
    http://www.cnczone.com/forums/showthread.php?t=74497
    Will be moved to here for a new exposure.
    Mariss gave his blessing




    About the Verilog tutorial I promised:

    I'm now ready to take a crack at this. It will cover what I have learned as a non-programmer in the past 18 months or so going from zero to now with Verilog and CPLDs.

    I was afraid of Verilog and wasted about 2 years avoiding it until I had no choice. I thought it would be formidable to learn and learning always makes my head hurt. Verilog can be formidable because it is a powerful tool if it is used to its full potential. I found out it can be used easily if the applications are relatively simple such as mine are. Dirt-simple and painless actually for small CPLDs.

    I'm not a programmer and I do not like programming. Just so you know; for me code has always been a means to end and has never been something to be enjoyed for its own sake. I reserve analog design as "something to be enjoyed for its own sake".

    The assumption here (prerequisites?) is you already know how to design logic. You have to know how to generate timing charts, be comfortable with accounting for propagation delays, know Karnaugh Maps and generally be really conversant with efficient logic design. If you're not, I really can't help you there because the effort to bring you up to speed is beyond the scope of this exercise.

    Real programmers are going to blanch at what follows. Most will wonder where I got the nerve to do a Verilog tutorial when I know next to nothing about it. Trust me, it works though. It isn't meant as an "in your face" thing at all. I know and respect many top-notch programmers. Rather, it is how do you efficiently convert a logic diagram into a small CPLD and be assured it will work properly right away. It is a very narrow venue.

    There are only 6 things you have to learn in Verilog to make anything you want to work that will fit in a 32 or 64 macrocell CPLD:

    1) Declare inputs and outputs. They are the gozinto and gozoutof pins on your CPLD. They come directly after the top module name.
    (input INA, INB, INC, etc, output OUTX, OUTY, OUTZ, etc, inout SOMENAME);

    2) Write behavioral descriptions for your D-Flops and counters. These are subroutines. I put them after the top module.

    Here's one for a very simple D-Flop:

    module DF (input C, D, output Q);
    reg df = 0;
    always @(posedge C)
    df <= D;
    assign Q = df;
    endmodule

    Here's one for an Up-Down counter:

    module CB12BRE (input UD, CE, R, C, output [11:0] Q);
    reg [11:0] u_d = 12'h000;
    always @(posedge C)
    if (R)
    u_d <= 12'h000;
    else if (CE)
    if (UD) begin
    u_d <= u_d + 1;
    end
    else begin
    u_d <= u_d - 1;
    end
    assign Q = u_d;
    endmodule

    3) Anytime you have a counter, name it and declare a wire:

    wire [11:0] QB; means you have a 12-bit bus named 'QB' belonging to some counter named 'QB'.

    4) "Solder in" your D-Flops and counters:

    DF F1 (.C(CLK), .D(STP), .Q(Q1));

    Which means module named DF (a subroutine), here called 'F1' has its clock (C) connected to a global signal named 'CLK' which happens to be the oscillator. The flop's 'D' input here happens to connect to a signal named 'STP'. It's output 'Q' is named 'Q1' to distinguish itself from all the other D-flops in the circuit.

    Same goes for:

    CB12BRE M1 (.C(CLK), .CE(So), .UD(Do), .R(Q12), .Q(QB));

    Which is our 12-bit counter subroutine here named M1 whose C input goes to CLK, it's CE (clock enable) goes to a signal called 'So', its UD input (up-down) goes to something called 'Do' and its R (reset) input goes to some D-flop named 'Q12'. The 12-bit counter's outputs are named 'QB' and reference the named wire 'wire [11:0] QB'.

    5) Everything else is an 'assign' statement:

    assign G11 = G9 & and G10;

    Means "The output of gate 11 is the AND combination of gates 9 and 10". Only 4 operators here; & = AND, | = OR, ^ = XOR and ~ = invert.

    6) All designs are fully synchronous. That means if it has a CLK input (C), it goes to the system clock. You do not have access to any counter or D-flop clock. This seems like a terrible restriction but it's not. You have the CE (clock enable) inputs which more than makes up for the restriction as you will see later.

    That's it. Those are the 6 things you have to learn to do what you need.

    This is the CNCzone, electronics forum. What I propose as a learning project is to make a 7A, 80V half-step drive. Squeeze into a $1 CPLD the logic and make a practical and usable chopper-type (constant off-time) drive as a design exercise. We will go from a description of what's required, generate a logic schematic, minimize the logic and optimize it for a CPLD and translate it into Verilog code. Should some care to build it, it will work better than what's out there commercially.

    I think it will be a lot of fun. Everyone will learn something and a few will get hooked. I need to know if there is an interest because it will place a burden on my time to prepare the material.

    Mariss

    Last edited by Al_The_Man; 05-18-2009 at 11:27 AM.


  3. #3
    Registered apache405's Avatar
    Join Date
    Jan 2007
    Location
    USA
    Posts
    197
    Downloads
    0
    Uploads
    0

    Default

    Mariss,

    Do you use a dev board for the CoolRunners? If you do, which one do you use?

    -Jeff


  4. #4
    Registered
    Join Date
    Aug 2006
    Location
    usa
    Posts
    247
    Downloads
    0
    Uploads
    0

    Default

    I have a definate interest
    Amplexus
    Ps what do you think of the cypress PSoC? they have some interesting stepper ap notes, it's no gecko but does have potential. 12 analog blocks onboard 9 bit dacs, pwm, comparators op amps etc several a to d's and 16 digital blocks plus a fast microprossesor. Their reference design leaves a bit to be desired but it may be better than anything else out there ( Allegro etc) and they include all of the source code. they call it pwm but it looks like a chopper to me, non syncronous, no morphing and no midband compensation. it's no gecko at the moment and never will be if I code it.



  5. #5
    Member Bubba's Avatar
    Join Date
    Mar 2004
    Location
    lagrange
    Posts
    1804
    Downloads
    27
    Uploads
    0

    Default

    I have been watching the thread and afraid it was not going to happen. I have a very definite interest as I think it might just be the replacement logic for a project I have in mind.
    Please do it.

    Art
    AKA Country Bubba (Older Than Dirt)


  6. #6
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default

    Quote Originally Posted by apache405 View Post
    Mariss,

    Do you use a dev board for the CoolRunners? If you do, which one do you use?
    I designed my own "breadboard" double-sided printed circuit board. Surface mount components presents a dilemma for breadboarding. I used to use perf-board, vector pins, 28 gauge bus wire and teflon sleeving with thru-hole components for breadboarding. This isn't a suitable method when SMT parts are used.

    My solution was to have every general purpose surface mount part connect to thru-hole pads which are then wired from the back side using 28 gauge bus wire and teflon sleeving. While I was at it, I also included a G203V power section, voltage regulators, optoisolators and terminal blocks. Most of my work deals with motor controls so this comes in handy. The 'breadboard' contains:

    1) An XC2C32A and an XC2C64A concatenated to a JTAG programming head.
    2) Two full MOSFET bridges (7A @ 80VDC) suitable for steppers or servos.
    3) An 8-bit R-2R network for D to A and A to D conversion.
    4) Eight noise filtered CPLD input networks.
    5) Six indicator LEDs.
    6) Pads for 6 SOIC-8 devices (12 op-amps, comparators, etc).
    7) Pads for 12 SOT-23 (3, 4, 5 and 6 pin devices).
    8) Pads for 120 general purpose 0603 components.
    9) Pads for 4 trimpots, 4 tactile SMT switches, 15-pin jumper block.
    10) Onboard 12V, 5V, 3.3V and 1.8V regulators fed from an 18V to 80V input.

    I developed the upcoming G380 servodrive on this breadboard. It would have been nearly impossible without it.

    Mariss

    Attached Thumbnails Attached Thumbnails CPLD Tutorial-cpld_breadboard-gif  


  7. #7
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default

    What I'd like to know is which CDLP development board most of you have. I've got several of their Digilent XC2C256 boards that have a 4-digit LED display, 4 2X6 header sockets along the top, a mini-USB jack on the left, 2 slide switches and 2 tactile momentary switches along the bottom. The back of the board says "PB200-146 REV D", "copyright 2008, revision 2.0" and a bar code sticker that says "D163663"

    This board also has an 8-pin DIP socket for an external oscillator. I can come up with an RC clock oscillator hack for this board using the socket if this is the one most people have.

    Mariss



  8. #8
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default

    OK, let's start. Please see the attached gif.

    The schematic is for a half-step chopper drive scaled for 7A @ 80VDC maximum. The diagram shows the CPLD and the external circuit for winding A. The winding B circuit is identical so there is no point in repeating it. The trimpot adjusts the set current from 0 to 7A.

    The constant off-time chopper algorithm is:

    1) Apply voltage to the motor coil (LA = 1, RA = 0).
    2) Ignore the comparator signal (CMPA) for 5uS to allow transients to die away.
    3) Input CMPA goes low when current reaches reference level.
    4) When CMPA goes low, compliment LA and RA (LA = 0, RA = 1) for 20uS.
    5) Go back to (1).

    STP and DIR go to a 3-bit UD counter whose outputs are called QA. These outputs go to phase decoders A and B. The decoders generate the motor winding current direction (input to XOR gate), synchronize the 5uS and 20uS timers to the STP input (via RES) and modulate the winding's current amplitude (via REFA) to generate the necessary half-step waveform.

    You can think of a half-step drive as a 2 microstep drive. Here current is either 'set current' or zero. Higher resolution microstepping would have REFA generate other current levels between zero and 'set current'.

    If anything so far is unclear or needs better explanation, please ask.

    Next up: Designing the clock oscillator and step counter.

    Mariss

    Attached Thumbnails Attached Thumbnails CPLD Tutorial-example-drive-jpg  


  9. #9
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default

    1) CLOCK OSCILLATOR:

    The choice of the oscillator frequency must be made early on because it will impact the size of the finished design.

    The oscillator period determines the 'time granularity' of the design. Nothing can happen in a synchronous design between clock 'ticks'. It will not respond properly to inputs having a shorter time than the clock period and outputs can change state only on the clock ticks.

    Slower the clock rates generally result in fewer registers used. In this design, 5uS and 20uS timers must be constructed. If a 200kHz clock (5uS period) is used, a 2-bit counter (2 registers) will get you the needed 20uS timer.

    A 20MHz clock (50nS period) would require 400 clock ticks to count off 20uS which requires a 9-bit counter (and 9 registers).

    So what is the minimum CPLD clock frequency for this circuit? Well, it's a step motor drive so lets say 3,000 RPM is the maximum design motor speed. A half-step drive goes 3,000 RPM when it gets 20,000 STP pulses per second. That is a step pulse period of 50uS. Let's say we want to resolve the CPLD timing to 1&#37; of that to eliminate potential phase jitter and beat frequency problems. 1% of 50uS is 0.5uS so we need a 2MHz CPLD clock.

    A 2.4K resistor and a 100pF capacitor should give 2MHz when connected to a Schmidt trigger input inverter in the CPLD. See the schematic in post #104 for this inverter.

    Next: STP counter design.

    Mariss



  10. #10
    Registered apache405's Avatar
    Join Date
    Jan 2007
    Location
    USA
    Posts
    197
    Downloads
    0
    Uploads
    0

    Default

    Quote Originally Posted by Mariss Freimanis View Post
    What I'd like to know is which CDLP development board most of you have. I've got several of their Digilent XC2C256 boards that have a 4-digit LED display, 4 2X6 header sockets along the top, a mini-USB jack on the left, 2 slide switches and 2 tactile momentary switches along the bottom. The back of the board says "PB200-146 REV D", "copyright 2008, revision 2.0" and a bar code sticker that says "D163663"

    This board also has an 8-pin DIP socket for an external oscillator. I can come up with an RC clock oscillator hack for this board using the socket if this is the one most people have.

    Mariss
    I have a Digilent XCR Plus with a Xilinx XCR3064L in it. It has 4 buttons, 8 switches, 8 LEDs, a 2 digit 7 segment LED, a trim pot, a breakout for the XCR chip and a breadboard. It also has a JTAG header and I have a parallel programmer for it (I need to replace it with a USB one)

    -Jeff


  11. #11
    Member Bubba's Avatar
    Join Date
    Mar 2004
    Location
    lagrange
    Posts
    1804
    Downloads
    27
    Uploads
    0

    Default

    Quote Originally Posted by Mariss Freimanis View Post
    What I'd like to know is which CDLP development board most of you have. I've got several of their Digilent XC2C256 boards that have a 4-digit LED display, 4 2X6 header sockets along the top, a mini-USB jack on the left, 2 slide switches and 2 tactile momentary switches along the bottom. The back of the board says "PB200-146 REV D", "copyright 2008, revision 2.0" and a bar code sticker that says "D163663"

    This board also has an 8-pin DIP socket for an external oscillator. I can come up with an RC clock oscillator hack for this board using the socket if this is the one most people have.

    Mariss
    At the present time, I have none! I want to see where we go with this and I will get one at the appropriate time!

    Discussions as we go will also be a determining factor.

    Art
    AKA Country Bubba (Older Than Dirt)


  12. #12
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default

    Quote Originally Posted by apache405 View Post
    I have a Digilent XCR Plus with a Xilinx XCR3064L in it.
    Bummer. The XCR3064L doesn't have the kind of inputs that would make it suitable for what I'm going to do here. It must be a CoolRunner II.

    Mariss



  13. #13
    Gold Member acondit's Avatar
    Join Date
    Apr 2005
    Location
    USA
    Posts
    1778
    Downloads
    0
    Uploads
    0

    Default

    Quote Originally Posted by Mariss Freimanis View Post
    What I'd like to know is which CDLP development board most of you have. I've got several of their Digilent XC2C256 boards that have a 4-digit LED display, 4 2X6 header sockets along the top, a mini-USB jack on the left, 2 slide switches and 2 tactile momentary switches along the bottom. The back of the board says "PB200-146 REV D", "copyright 2008, revision 2.0" and a bar code sticker that says "D163663"

    This board also has an 8-pin DIP socket for an external oscillator. I can come up with an RC clock oscillator hack for this board using the socket if this is the one most people have.

    Mariss
    My board is the same except for the barcode sticker.

    Alan



  14. #14
    Registered
    Join Date
    Aug 2006
    Location
    usa
    Posts
    247
    Downloads
    0
    Uploads
    0

    Default board number

    I have the diligent D192943 and await your condensed verilog wisdom, even I can learn 6 instructions, verilog seems not too hard. Also been messinf with rhe cypress psoc which is a cool mix of analog and digital blocks wirh a microprocessor, it runs C and assembly and has a very easy to use express mode for rapid prototyping that actually works much better than I expected nice tool set.
    Amplexus



  15. #15
    Registered pminmo's Avatar
    Join Date
    Jun 2003
    Location
    St. Peters, Mo USA
    Posts
    3312
    Downloads
    0
    Uploads
    0

    Default

    I started with the Digilent C-mod ( http://www.digilentinc.com/Products/...=419&Prod=CMOD) and their parallel JTAG Programming Cable. Works pretty well with ISE although sometimes it can be a bit contrary to get the JTAG chain initialized, but I'm sure it's more of an ISE issue than Digilent. Once I progress to my own boards then the C-Mod sits..... But I would recommend it as a cheap way to get started with Coolrunner parts and CPLD's.

    Phil, Still too many interests, too many projects, and not enough time!!!!!!!!
    Vist my websites - http://pminmo.com & http://millpcbs.com


  16. #16
    Registered
    Join Date
    Jan 2006
    Location
    USA
    Posts
    121
    Downloads
    0
    Uploads
    0

    Default Links

    I thought links to these two threads would be appropriate, since one discusses which Coolrunner kit to buy.

    http://www.cnczone.com/forums/showthread.php?t=74497

    http://www.cnczone.com/forums/showthread.php?t=80541

    Mine should be here tomorrow.

    Jon



  17. #17
    Gold Member
    Join Date
    Mar 2003
    Location
    United States
    Posts
    2839
    Downloads
    0
    Uploads
    0

    Default

    I pounded out the first chapter and I'm half-way through chapter 2. Chapter 2 really gets down to business.:-) Let me know what you think.

    Mariss

    Attached Thumbnails Attached Thumbnails CPLD Tutorial-tutorial_ch1-pdf  


  18. #18
    Registered pminmo's Avatar
    Join Date
    Jun 2003
    Location
    St. Peters, Mo USA
    Posts
    3312
    Downloads
    0
    Uploads
    0

    Default

    I think your doing a great job! I wish I had this a year ago......

    Phil, Still too many interests, too many projects, and not enough time!!!!!!!!
    Vist my websites - http://pminmo.com & http://millpcbs.com


  19. #19
    Member Bubba's Avatar
    Join Date
    Mar 2004
    Location
    lagrange
    Posts
    1804
    Downloads
    27
    Uploads
    0

    Default

    Mariss,
    Well written and I went right though it (except for FAT finger typing) and was able to complete this chapter with little problem.

    I downloaded V11/1 before I saw your post to use 10.x, but so far there seems to be no real difference.

    I do have one question however and maybe this is a difference. You talk about assigning pins and in V11.1, I had to input the physical pin numbers. I followed yours, but did YOU assign the pin numbers or was that done by the software?

    Art
    AKA Country Bubba (Older Than Dirt)


  20. #20
    Registered pminmo's Avatar
    Join Date
    Jun 2003
    Location
    St. Peters, Mo USA
    Posts
    3312
    Downloads
    0
    Uploads
    0

    Default

    Heres some code that I have used below for quarterstep. I'm real interested in Mariss's pwm code as this seems to be inefficient:

    /// Quarter step translator /////
    module QTranslator (clk, step, reset, dir, q );

    input clk, reset, step, dir;
    output [17:0] q;
    reg [3:0] tmp;
    reg [17:0] q;

    always @(posedge step or posedge reset)
    begin
    if (reset)
    tmp = 4'b0000;
    else
    if (dir)
    tmp = tmp + 1;
    else
    tmp = tmp - 1;
    end
    // phases-pwma-pwmb
    // q abcd-xxxxxxx-xxxxxxx
    always @(posedge clk)
    begin
    case (tmp)
    4'h0: q <= 18'b001000000001111111; //0
    4'h1: q <= 18'b101001100011110101; //22.5
    4'h2: q <= 18'b101010110101011010; //45
    4'h3: q <= 18'b101011101010110001; //67.5
    4'h4: q <= 18'b100011111110000000; //90
    4'h5: q <= 18'b100111101010110001; //102.5
    4'h6: q <= 18'b100110110101011010; //135
    4'h7: q <= 18'b100101100011110101; //157.5
    4'h8: q <= 18'b000100000001111111; //180
    4'h9: q <= 18'b010101100011110101; //202.5
    4'hA: q <= 18'b010110110101011010; //225
    4'hB: q <= 18'b010111101010110001; //247.5
    4'hC: q <= 18'b010011111110000000; //270
    4'hD: q <= 18'b011011101010110001; //292.5
    4'hE: q <= 18'b011010110101011010; //315
    4'hF: q <= 18'b011001100011110101; //337.5
    endcase
    end

    endmodule

    /// PWM code ////

    module PWM (DC, CLK ,Q , RESET);
    input [6:0] DC; // duty cycle input
    input CLK;
    input RESET;
    output Q;
    reg [7:0] acc; // accumulator has one more bit than the duty cycle
    assign Q=acc[7]; // output is the 8th bit
    initial acc=0; // for simulation only
    always @(posedge CLK)
    begin
    if (RESET) acc=0;
    acc=acc[6:0]+DC; // only add with 8 bits.
    end
    endmodule

    Phil, Still too many interests, too many projects, and not enough time!!!!!!!!
    Vist my websites - http://pminmo.com & http://millpcbs.com


Page 1 of 16 123411 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


About CNCzone.com

    We are the largest and most active discussion forum for manufacturing industry. The site is 100% free to join and use, so join today!

Follow us on


Our Brands

CPLD Tutorial

CPLD Tutorial