Archive for the ‘Coding’ Category

The source of the number 13

Wednesday, November 12th, 2008

Thank you Clark for the push – I had another look around and discovered that both 0×0D (13) and 0×0A (10) were being received by the AVR.  Linux was adding the extra 0×0D in front of my 0×0A, causing all the data in the command buffer to be shifted down one, which resulted in the changed numbers, and the strange text output.  After a bit of searching, I found that the stty utility can be used to turn this feature off:

stty -F /dev/ttyS0 -onlcr

So that’s that – now I can get back to working on a utility to encode some colour bitmaps to store in the remaining 10K on the ATMEGA168 I’m using :)

What does Atmel have against the number 10?

Monday, November 10th, 2008

Or maybe avr-gcc is the culprit?  Anyway, here’s the story:  I’ve been working on building an embedded computer for my motorcycle for some time now.  Recently I’ve been prototyping a display module for it.  I’ve got a couple of cell phone screens from SparkFun, and some code to drive them from an AVR.  So far, so good.  I’ve since added a MAX3232 to the mix to allow the actual computer to communicate with the screens.  I devised a simple serial protocol that allows me to have some fonts and simple drawing commands stored on the AVR so that I don’t need to send each pixel over the serial line.

Why won\'t it display a 10?

It works great – except – that whenever I send a command containing the byte 0×0A, the command gets misinterpreted somehow.  If I ask for a large 3 digit display showing the number 10, it displays the number 13.  Every time.  Every other number from 0×00 to 0xFF is unaffected.  If I send a command asking for some text at 10 pixels across, n pixels down, the text appears only as blank spaces, and in a different position.

I’ve confirmed that the byte is being received by the AVR correctly.  I store it in a buffer, waiting for the whole command to complete, and I’ve confirmed that it is still stored in the buffer when I go to execute the selected command.  The problem appears when I actually unpack the buffer into the arguments needed for the display function calls.  Inside the function body, the value that should have been 10 is changed.  This happens even if I just pass in the whole command buffer as a uint8_t *.

This makes no sense to me.  Especially since I can hard-code a 10 and have everything work without issue.  The only thing I can think of is that this is somehow related to the call being made from an interrupt handler, although this hardly explains why only calls involving the number 10 are affected…

I’m going to try moving the main switch into the main loop and see if that changes anything.

Ubuntu, apache2 and mod_auth_mysql

Monday, December 24th, 2007

I’ve been moving a site I built for a client quite a while ago over to a new server. The old server had been using versions of apache 1.3.x and mod_auth_mysql compiled from source, while the new server is running apache 2.x installed from Ubuntu’s packages. Most of the migration has gone pretty smoothly thus far, considering how different the versions of software are on the new server.

One difference that had me stuck for a while was getting mod_auth_mysql to authenticate users against the database on the new server. I was able to install it as a package instead of compiling from source, and the new version seemed to use the same configuration directives as the old one, but I was getting 500 errors whenever I accessed a password protected page. The web server log was reporting

Internal error: pcfg_openfile() called with NULL filename
[client 10.1.1.99] (9)Bad file descriptor: Could not open password file: (null)

Additionally, the database server wasn’t logging any connections from the web server. I finally came across this thread which explains the issue. Apparently, apache2 wants to use the htpasswd file facilities to authenticate users, unless you explicitly tell it not to. Adding

AuthBasicAuthoritative Off

to the .htaccess files allows mod_auth_mysql to perform authentications. That was a step in the right direction, however apache was still recording errors whenever a password protected page was requested. This seems to be another side effect of apache2 wanting to use htpasswd files. If you add

AuthUserFile /dev/null

to the .htaccess file as well, it stops complaining.

My ongoing battle with LaTeX

Sunday, November 11th, 2007

I’ve started using LaTeX to prepare assignments and other documents, and so far it’s working out pretty well. Formatting math is easily done, especially since I’m coming from OpenOffice, which uses a simplified version of the same markup. Importing and formatting code files with the listings package is great too. My big issue has been with positioning of figures.

It’s easy enough to add a figure to a document, and give it a caption, or reference it from elsewhere in the text. Getting it to flow into the text where you want it to is another story altogether. LaTeX likes to keep pages balanced, so as long as there’s text to be output, it doesn’t want to take up too much space on a page with figures. LaTeX likes to re-flow your document automatically to make sure it remains visually pleasing when you make changes to it. The problem is that with most assignments what’s needed is not so much a visually pleasing document as a document that will be easy for a TA or professor to mark. In order to achieve this, you generally want, say, the text and figures for question 6 to all appear, in order, before any of the text for question 7.

I’ve found a couple of the things that will make LaTeX float figures to later pages are settings for the limit as to how many figures it will allow on one page and how much text it will push to the next page to make room for figures. These commands will change those limits:

\renewcommand{\textfraction}{x%}
\setcounter{totalnumber}{x}

The first one sets the minimum fraction of text that will be displayed on any page, and the second sets the maximum number of figures to appear on any page. These two settings, in combination with the ! and h positioning flags to the figure environment, get close to letting you force the positioning of figures.

I’m still looking for something better though…

Stack pointers are busy little things

Wednesday, November 7th, 2007

I’m currently working with Angela Demke-Brown and Karen Reid on an extension to the sys161 operating system simulator to allow visualization of the current memory usage in the simulated system.  The intent is to help students in a 3rd year operating systems course with their understanding of virtual memory.  The visualizer now displays the used and unused pages, colour-coded according to the type of allocation.  The next step is to somehow visualize the contents of pages used for kernel stacks.

The current idea is to get the simulator to report whenever the stack pointer register changes value, and infer what happened from that.  Given that there is only one processor, the updated stack must be the one for the currently running thread (or an exception handler, but I’ll get to that later). I know which thread this is because a modification to the os161 kernel starter code causes the simulator to report whenever there is a context switch.  Given this, and a dump of the symbol table for the running kernel, it should be possible to get the information I need.

So, I coded up a simple test that just sent a message from the simulator with the new value of the stack pointer every time it changed.  I figured it would be updated often, but I had no idea how often – the resulting system sends so many messages to the visualizer program that it can’t keep up.  I’ve determined that most of these messages aren’t really interesting, being the result of the timer interrupt.  I’m hoping that I can find a way to exclude these, and that the remaining messages will be more manageable.  If not, I’ll have to work out a way to turn this feature off when it isn’t needed.