Wednesday, March 16, 2011

SystemVerilog struct assignment

You may have occasion to initialize a structure in SystemVerilog:

typedef struct {
int f1;
int f2;
int f3;
} set_of_values_T;

set_of_values_T set_of_values = {1, 2, -3};


This might seem fine, but the above assignment is actually a concatenation. The simulator will take the set of values and pack them into one big bit vector.

In my experience, Modelsim was okay with positive values, or a mix of positive and negative values. In compilation, it would note that it was promoting an assignment to a concatenation. But if all the values were negative, it would give an error that a packed value was being assigned to an unpacked value.

A quick Google search turned up a book preview that showed an example of how to initialize a structure. Very simple, really. Just add a tick (') before the assignment:


set_of_values_T set_of_values = '{1, 2, 3};


This does the trick, and the notes and errors go away. SystemVerilog uses the '{} construct to differentiate a list of values from a concatenation, {}.

EDIT: Another example, this time with a dynamic array or queue:


bit [0:2] values[$]= '{3,5,6};

Tuesday, February 22, 2011

Engineering Economics

I'm going to deviate from technical discussion and get into money a little. Engineers are often under pressure to reduce costs. Someone asked for advice this week on removing a couple of parts that are not needed for a particular version of a product that we are shipping. Removing those parts has some implications, which include testing to verify that there are no "gotchas", possibly releasing a new program image to account for the parts not being there, further testing of that new image, and so on.

It's good for engineers to have at least some grasp of the tradeoffs involved in their decisions. In this case, we are talking about 14 cents of savings per board. At 5,000 units per year, this comes out to $700. (Network infrastructure does not have the volumes of consumer products.) At 10,000 we are at $1400. Is it worth the time?

How much is engineering time worth? You could try to figure the hourly cost. Divide the salary by 2,000 hours per year. Don't forget to add the cost to the employer of benefits, employer's share of taxes, worker's comp, office space, supplies, all that expensive depreciating equipment in the lab. $100/hour is probably low. What about $500? Maybe.

What else could an R&D engineer be spending his time on? What about new product development?

How much does an engineer bring to the bottom line in new product development? How much of the profits should be allocated to engineering? We grudgingly have to give some credit to marketing, legal, human resources, and other groups that keep the company going. Say engineers make up a third of the overhead. Then maybe it's fair to take credit for a third of the net profit.

How much profit for a new product? We can look at net margins for the business and figure what percentage of overall business profits is due to the new product.

The higher the volumes, the more it makes sense to pay someone to work on the problem, whether it's taking 14 cents of parts off a board, or developing a new product. We engineers are often isolated from these kinds of numbers, especially in a large company. We rely on upper management to set our priorities. But we can at least throw around some rough numbers and figure out if we are pulling our weight.

Friday, February 11, 2011

Arbitrary voltage source in LTSpice

Here is a post I found when searching for a way to use some data as an input to LTSpice to test a filter.

http://ltspicelabs.blogspot.com/2006/10/using-wav-files-for-io-and-transient.html

This talks about taking a .wav file as input. Which is fine if you are doing audio. But what if you want to generate a .wav file based on, say, a mathematical model?

Python has a nice wave module. So you could write a short script like this:

# Send data to output .wave file
# filename = string
# fsys = integer system clock rate
# data = list of integers
def writewave(filename, fsys, data):
oversample_factor = 8
offset_factor = 16384
scale_factor = 60 # 8 bit in to ~15 bit out

w = wave.open(filename, 'wb')
w.setnchannels(1)
w.setsampwidth(2)
w.setframerate(fsys * oversample_factor) # need to oversample to generate the steppy DAC output
w.setnframes(len(data))
d = '' # string to hold byte values in little-endian order
for i in data:
ii = scale_factor * i + offset_factor
rl = int(ii) & 255
rh = (int(ii) >> 8) & 255
for j in xrange(oversample_factor):
d = d + chr(rl) + chr(rh) # little-endian
w.writeframes(d)
w.close()

This will write out your data to a .wav file that can then be used in LTSpice. The example here has a sample width of 2, which would be two bytes, or signed 16 bits. I was modeling a signed 8-bit DAC output, so I scaled it up and offset it to get what I wanted. Also I put in an oversampling factor to get a more step-wise response instead of a linear ramp between sample points.

Don't forget "import wave" in the Python script!

Wednesday, November 17, 2010

Interesting reset problem

I happened across this description of a board that failed to boot up properly, just as we were dealing with a similar issue at work. You never know where debugging is going to lead you.

We are ramping up for production of a certain controller board, and have built many prototypes and pre-production boards. Suddenly the factory reported a batch of boards that sometimes get stuck in reset, with different frequencies of occurrence depending on seemingly random factors--for example, if a certain place on the board is probed, or a certain daughtercard is present. We have not recently changed the design and have not seen this problem before.

The first thing we checked is that the board's power rails are coming up in the expected sequence and are stable by the time we start releasing reset. No problems there.

Next, we looked at the glue logic that controls the reset. After some probing and trying different things, we ruled out problems in the glue logic. Could this be some new, undiscovered errata or a bad lot of microcontrollers? The problem only occurs once after power-up. If we can get out of reset, any following resets have no problems whatsoever.

The next step was to think about what the controller is doing right at startup, before it releases reset. We have it set to look in the boot ROM at address 0 to retrieve a configuration word (CW) that further configures the controller before it's up and running. Normally it will find a valid preamble and read a bunch of words from the ROM. But in the failing cases, we only saw it read two words and then stop. Is it reading bad data?

Time to break out the logic analyzer and look at the bus. Interestingly, the probes seem to load the bus in such a way that we can no longer produce the failure! Now which analyzer pods can we take away to get the failure again? It turns out that the pod with the address lines makes the board pass when present, and sometimes fail when removed.

With the analyzer on the bus, we could see that the first word out of the ROM is correct--part of the preamble--in the passing case, but some other value in the failing case. But the address and control signals all look correct. We checked the reset pulse for the ROM and did some adjustments. After all, if the ROM is somehow coming up in a funky state, a good result pulse should take care of it, right? But no improvement there.

Finally, we contacted the ROM vendor and found out a newly discovered errata. Sometimes it really is someone else's fault! There is a known issue where the first read access after power-up, if it is to address 0, outputs the manufacturer code instead of the data. We happened to build with some parts that were susceptible to this problem.

Now we can work around this, and should be good to go. We changed the glue logic to force another reset if the controller is stuck, which is a good robustness improvement anyway. Now we just need to get the revised silicon from the vendor.

Wednesday, July 28, 2010

Tuesday, June 29, 2010

$4 Development Kit

I was tipped off by someone from work that TI has this little development kit available for the low, low price of $4.30. But so many people have jumped on this, that it's backordered.

It would be nice to be able to get my hands on this for tinkering at home. You can't beat the price, and the development kit is included. I'm assuming it's powered right off the USB, so no external power supply is needed.

Story is here:
http://ee.cleversoul.com/news/tis-amazing-430-launchpad.html/

Friday, May 21, 2010

Digital Feedback Control

Linear feedback systems are well known. In the continuous domain, the Laplace transform can be used to characterize the system and find an appropriate compensator to give the desired response. But in a digital world, this can break down. If the output of the system is sampled at discrete time intervals, the time lag can cause system instability that might not be predicted with the Laplace transform.

With some simple continuous feedback systems, the gain of the feedback control can be increased to improve the response time. There might not be any theoretical limit to how high the gain can go. But in a discrete time system, the time lag can cause oscillations and instability if the gain is raised beyond some limit.

As a simple example, suppose you have an integral feedback controller. This type of feedback accumulates error and uses this to drive the thing being controlled. It can be a little slower, but it is simpler than a full-fledged PID controller. It is very stable -- it tends to asymptotically approach the set point for all gains, and it tends towards zero steady-state error, which might be important in some systems. A write-up of that kind of system is here [pdf].

In the continuous domain, the open loop gain is just the integrator, 1/s, times the feedback gain K. In closed-loop form, assuming unity feedback, the transfer function comes out to K / (K + s). As you increase K, you increase the speed of the response.

In the discrete domain, you can simply do this in a tabular fashion, as if you had a digital controller that samples the output, compares against the set point, and accumulates the error. For a small value of K, say 0.2, you get a nice exponential decay towards the set point.

Click to enlarge.

If K gets larger, you will start to see ringing but the system is still stable.


Finally, increasing K by too much will make the system unstable.


There are analytical ways of figuring the response of a discrete time system. That is for another post.