Monthly Archives: July 2014

Parameters in Altium Designer

I use Altium Designer a lot for schematic capture and PCB layout: drawing electronic circuits, in other words. It has a handy feature called ‘Parameters’ which should make it easy to add a piece of data, for example a version number or the author’s name, to a project and have it appear on all of the documents which form part of that project. I’ve always had trouble getting it to work, but I’ve finally cracked it and I’m writing down my findings here so that I don’t forget them. Other people may find it useful too.


1. It’s possible to set parameters on a project and then refer to them in documents, but if there is a parameter in the document with the same name, it will hide the project parameter.

For example, all schematic documents have a ‘Revision’ parameter by default. If I set a ‘Revision’ parameter for the project, it won’t appear in any of the documents because the document ‘Revision’ parameter hides it. If I choose a new name which isn’t already used in the documents, like ‘ProjectRevision’, it does appear in the document.

2. In schematic documents, parameters are referred to using the equal sign:


In PCB documents, they’re referred to using a dot:


It’s bonkers, but it’s true.

Here’s my recipe for getting a version number to appear on both schematic documents and the PCB layout.

  1. Set your version number on the project using Project -> Project Options -> Parameters. Call it something which isn’t one of the default document parameters. I use ProjectRevision.
  2. In schematic sheets, use =ProjectRevision in text strings to call up the version number.
  3. In PCB sheets, use .ProjectRevision in text strings to show the version number.
  4. Make sure ‘Convert Special Strings’ is switched on in both schematic and PCB editors otherwise you won’t see the version number.

For the schematic editor, select Tools -> Preferences -> Schematic -> Graphical Editing and make sure ‘Convert Special Strings’ is ticked.

For the PCB editor, select Design -> Board Layers and Colours -> View Options and make sure ‘Convert Special Strings’ is ticked.

That’s it. Using parameters has made managing my Altium projects quicker and easier.

SD memory card access from an Atmel AVR microcontroller using sd-reader

I’ve recently had a need to access SD memory cards from an Atmel AVR microcontroller. There are plenty of libraries out there which can do it, which saved me a lot of time, but choosing the right one and getting it to work wasn’t entirely straightforward.


I looked at sdfatlib, which is intended for use with the Arduino. The Arduino also uses an AVR microcontroller but the code is mostly written in C++. My project (using Atmel Studio) was set up to build a lot of complex code in C, and I didn’t fancy trying to convince it to build C++ and link successfully. The compiler balked at the first mention of the word ‘class’, so I decided not to pursue it.

Next in the candidate list there was FatFs, which is written in C but targeted at all sorts of microcontrollers including the AVR. It looked more generic than I needed, and I didn’t want to spend too much time dealing with the low-level SPI stuff to talk to an SD card, so I moved on. I would certainly consider it for future projects though.

The final choice was sd-reader, written and released by Roland Riegel. This was the answer to my wishes: all written in C and well tested, specifically targeted at SD cards on AVR microcontrollers. It compiled as part of my project without a problem. However, there were some issues to deal with.

Firstly, it’s set up assuming that it has sole control of the SPI interface. My project already used SPI for another peripheral, so I had to do some trickery in sd_raw_config.h and my existing code to dynamically reconfigure the SPI port for each peripheral each time it was selected. I replaced the macro:

#define select_card() PORTB &= ~(1 << PORTB0)


#define select_card() ({SPCR=0x50; nSD_CS_PORT &= ~nSD_CS_MASK;})

which forced SPCR, the SPI control register, to the right value for the SD card. I had to do something similar in my other peripheral code. This all worked fine but a small change to sd_raw_init() in sd_raw.c was needed because the initialisation of the card takes place with a very slow clock speed, so that accesses SPCR as well.

I wanted to enable access time stamping in my application, so I set


in fat_config.h. The documentation indicates that functions fat_set_file_modification_date() and fat_set_file_documentation_time() are available, but in fact they’re not. They’re declared static and used internally by the sd-reader code. The way to get time stamps to work is to define a function:

void get_datetime(uint16_t* year, uint8_t* month, uint8_t* day, uint8_t* hour, uint8_t* min, uint8_t* sec)

which gets called by the sd-reader code. You write this function so that your timekeeping code fills in the values. It worked fine once I’d discovered this.

The final wrinkle was that the function to create a directory:

uint8_t fat_create_dir(struct fat_dir_struct* parent, const char* dir, struct fat_dir_entry_struct* dir_entry)

is claimed by the documentation to leave dir_entry filled in with the directory’s details if the directory already exists. It does, but returns 0 indicating an error, so there’s no way to tell if it was impossible to get the directory or it just already existed. I worked round this by attempting to open the directory first, and only if that failed, attempting to create it.

Caveats: I may not have been using the most up-to-date version of the software, or I may have misinterpreted the documentation, or these things might have changed since I wrote this text.