Adding GPIO to Microcontroller with CPLD

  • Thread starter j777
  • Start date
In summary, you will need an input register with a Tri-statable output register, each output flop will have an individual Tri-state enable, and that will be driven by the pin direction register.
  • #1
j777
148
0
Hi,

I would like to use a CPLD to add additional GPIO to a microcontroller. I've never used a CPLD before so I'm in search of a good book that will get me up to speed. Does anybody have any recommendations on books or other applicable material?


Thanks
 
Engineering news on Phys.org
  • #2
Usually additional GPIO just involves address decoding.
I might think a small PLD would be sufficient for that.
What are you trying to do?
 
  • #3
As NoTime says, you might be able to use a smaller PAL instead of a CPLD, but even the 32-cell CPLDs are pretty simple to use. I like the Vantis Mach-3 series, because they have nice wide minterm capability. Here is the Application Note page at Lattice Semiconductor's website:

http://www.latticesemi.com/dynamic/index.cfm?fuseaction=view_category&document_type=31&jsessionid=ba30f8f9aba5x$3F$3F$
 
  • #4
Thanks for the suggesstions but I'm looking at using an Altera MAXII CPLD because I would like to add 2 8bit input ports (Port A and B), 2 8bit output ports (Port C and D, 2 8bit GPIO ports (Port E and F), address decoding for these ports, and possibly an interface to a serial ADC.

This is my first time using a CPLD (or any PLD for that matter) so I have a nice little adventure ahead of me.

The CPLD is attached to an 8bit multiplexed bus with ALE, RD, and WR strobes. So far I have the CPLD latch the address from the bus and use tri-state buffers to read ports A and B and write to ports C and D. A latch is also used on ports C and D to latch the output values from the buffer. If I want to be able to read ports C and D is it as simple as using an additional tri-state buffer per port to put the latched output values onto the data bus? Or is there a better way to do this?

The 2 GPIO ports are to include an 8bit data direction register per port that allows for each port pin to be configured as either an input or an output. A write to the port should only change the values of pins configured as outputs and all pins should be able to be read.
Does anybody have any ideas on the best way to implement this? Thanks
 
  • #5
Everything you mention is very do-able. You will want to have some number of address lines go into the part, and use those to select among the various registers and functions you describe. Some of the registers will be the control registers (like for the pin direction configuration you mention), and others will be the input and output ports themselves.

This technique is called memory-mapped IO, and is common in microcontroller (uC) circuits. You decode a range of addresses, and use those addresses to select which register or function you are working with in the CPLD.
 
  • #6
I understand the concept of memory mapped IO but what I really need is some guidance on how to implement the logic inside the CPLD to perform what I described above. What I'm really stuck on is how to implement the data direction registers. Actually maintaining the values for one of these registers is not a problem for me (decoding the address and doing read/write to correct register) instead its the association between a data direction register (DDR) and its port that I'm struggling with. For example if a pin is changed from an input to an output in the DDR the corresponding port pin should be changed to a default output value. Can somebody give me some help with the logic involved in this type of thing?
 
  • #7
What language are you coding the CPLD in? Even if you're just using ABEL-HDL, there are constructs that you can use like if-then statements. If you use VHDL or Verilog, you get even more abstract statements that you can use to intuitively code up mulplexers and such.
 
  • #8
I'm using Verilog.

I must let you know that my skill level in combinational logic is -beginner-. My real struggle here is the logic. Can you give me a little guidance?
 
  • #9
What reference do you have for learning CPLD design with Verilog? Does it cover instantiating registers and implementing MUXes? For the thing you want to do with switchable IO pin directions, you will have an input register with a Tri-statable output register connected to the same pins. Each output flop will have an individual Tri-state enable, and that will be driven by the pin direction register. So even if it is configured as an output, you can still read back that output value via the input register.
 
  • #10
Ok, thank you...thats exactly the type of response I was looking for. Now I just have to try to wrap my head around it... :)

Can you explain the last sentence in more detail?
 
  • #11
j777 said:
Can you explain the last sentence in more detail?

berkeman said:
Each output flop will have an individual Tri-state enable, and that will be driven by the pin direction register. So even if it is configured as an output, you can still read back that output value via the input register.
Let's say register 0 is the direction control register, register 1 is the input register, and register 2 is the output register. You will need to decide how you want to handle the input register -- whether you just read the inputs directly and transparently, or whether you want to store the external inputs on each CPLD clock, and even if you want to synchronize them (which you should with asynchronous inputs to avoid metastability problems). Let's say you just decide to read the inputs transparently for now.

So that means register 0 and 2 each consist of 8 flip-flops apiece, which are written by your uC to your CPLD when the correct addresses are decoded on write cycles. When register 1 is decoded on a read cycle, the 8 external inputs are driven onto the uC data bus.

Let's say that the bits in register 0 are active high to enable the tri-state outputs of register 2. So if you write reg0 <-- 0x0F, that enables the low 4 bits of reg2 as outputs, and leaves the high 4 bits of reg2 tri-stated. The outputs of reg2 are connected to your 8 bi-directional IO lines, so any lines that are enabled as outputs (by the data in reg0) will have those outputs driven by the data in reg2. Any other bits that are tri-stated can be used as inputs. And when you read reg1 to read the bidirectional IO byte, you see your own data on any lines that you are driving, and the external data on any lines that you are not driving.

Makes sense?


EDIT -- fixed some reg2/3 mixups in numbering.
 
Last edited:
  • #12
Yes, that makes perfect sense. Thank you so much for your help.
 
  • #13
Ok, I've implemented what you have described with the inputs being read directly. Lastnight I was thinking about the inputs and the possible metastability problems and I remembered you had mentioned something about it. What is the difference between storing the inputs on each CPLD clock and synchronizing them? Wouldn't storing them on each CPLD clock synchronize them?
 
  • #14
Excellent question -- good to see that you are thinking about this. In the real world, you cannot just bring an asynchronous signal into a synchronous clocked circuit, because the setup and hold requirements of the input flipflop may not be met. If the input changes right as the clock to the FF is changing (violating the setup requirement typically), then the FF can enter a metastable state, and the output can be anything (high, low, or wandering around). This metastable state persists for some amount of time, depending on the physical logic devices used, but it is usually less than a clock period for the system.

So, to avoid using this metastable output directly, you want to use two FFs in series to "synchronize" an asynchronous input with your clocked logic. That way, even if the first FF goes metastable because of a timing violation, the output of the first FF will be stable by the next clock, at which point the timing of the second FF is met, and the output of the second FF can be used by the rest of the synchronous circuitry.

So in your OP circuit, for each of the asynchronous inputs, you would have two FFs in series, being clocked by the system/CPLD clock. That makes two 8-bit input registers, with the outputs of the first register going into the inputs of the 2nd register, and the outputs of the 2nd register are what you would read from your uC.

I don't have a good online pointer for the concept of synchronizing asynchronous digital signals, but it should be in your textbook, or you can probably find more info via google. BTW, this is one of the fundamental interview questions that we ask all candidates for positions in our digital design group, and we even ask our analog candidates this question. It's fundamental to designing circuits that work in the real world.
 

Related to Adding GPIO to Microcontroller with CPLD

1. What is a CPLD and how does it work?

A CPLD, or Complex Programmable Logic Device, is a type of programmable logic chip that contains a large number of logic gates and programmable interconnects. It works by taking inputs from external devices, such as a microcontroller, and using its programmable logic to manipulate and route those signals to other devices.

2. Why would I need to add GPIO to a microcontroller with a CPLD?

Adding GPIO, or General Purpose Input/Output, to a microcontroller with a CPLD can provide additional functionality and flexibility to the system. It allows for more complex logic to be implemented and can reduce the number of pins needed on the microcontroller, freeing up space and reducing costs.

3. What are the benefits of using a CPLD for GPIO expansion?

Using a CPLD for GPIO expansion offers several benefits. It allows for a larger number of I/O pins to be added to a microcontroller without having to use additional chips. It also provides the ability to configure and reconfigure the logic as needed, making it more flexible and adaptable to changing requirements.

4. How do I program a CPLD to add GPIO to a microcontroller?

To program a CPLD for GPIO expansion, you will need to use a Hardware Description Language (HDL) such as Verilog or VHDL. These languages allow you to describe the desired logic and interconnections within the CPLD. You will also need a CPLD programming tool, which typically connects to a computer via USB or parallel port.

5. Are there any limitations to using a CPLD for GPIO expansion?

While CPLDs offer many benefits for adding GPIO to a microcontroller, there are some limitations to consider. The number of I/O pins available on a CPLD is finite, so if a large number of GPIOs are needed, a different solution may be necessary. Also, the use of a CPLD may add complexity to the system and require additional design and programming time.

Similar threads

Replies
9
Views
1K
  • Electrical Engineering
Replies
2
Views
1K
  • Electrical Engineering
Replies
3
Views
2K
  • Electrical Engineering
Replies
18
Views
8K
  • Electrical Engineering
Replies
7
Views
2K
  • Computing and Technology
Replies
13
Views
445
  • Electrical Engineering
Replies
2
Views
1K
  • Electrical Engineering
Replies
4
Views
1K
  • Electrical Engineering
Replies
5
Views
3K
  • Electrical Engineering
Replies
7
Views
3K
Back
Top