Sunday, January 31, 2010

DIY Oscilloscope

A friend of mine convinced me to buy a kit to build a simple oscilloscope. At first, I thought it would not be very useful because the analog bandwidth is not more than 1 MHz. On the other hand, most of the times I have needed an oscilloscope, it has been to debug simple and slow data lines, such as SPI and RS232, so I decided to give it a chance. It was a real challenge to solder, because there were a lot of really small surface mount components (mostly capacitors and resistors) to solder. After a few evenings of soldering I was ready to try it out.

Here are a few pictures. First of all I measured the oscilloscope's built-in 500 Hz test signal:



Second, I measured the 9600 bps serial output from my HP48SX calculator. You can see that the output is not proper RS232 signal levels. I guess this is a way to save battery power on the calculator:



After a little testing, I am really impressed by the oscilloscope, especially as it was priced just over 500 swedish kronor (less than $80). It was also fun and interesting to solder all the small parts!

Sunday, January 17, 2010

Serial port insanity.

After oceans of time I am back again. The kernel now implements the following system calls:

  • rtos_yield (yield to another process)
  • rtos_allocate (allocate a 64 byte message buffer from the buffer collection)
  • rtos_transmit (hand over a message to another process)
  • rtos_accept (accept a message from another process)
  • rtos_dispose (dispose of a buffer)
  • rtos_delay (delay a specified number of system ticks)
The final names are yet to be decided. I also implemented support for the Cortex-M3 SysTick timer (which is mandatory in all Cortex-M3 based chips). It's exception handler increases the system tick counter and releases processes that have blocked themselves using rtos_delay.

I also started implementing support for USART2 in the STM32. That is because this USART is connected to the RS232 DSUB connector on the STM32-P103 dev board I use. To start with, I hooked the port up to my laptop using a RS232-USB adapter, trying a number of different serial port sniffer/terminal programs to see the dev board output. With little success! After a few hours, I grabbed my old HP 48SX calculator, which has an RS232 port on it. A few simple commands allow me to configure the serial port settings and then everything worked perfectly fine!

To set up USART2 for just using the transmitter, you have to do the following:


/* Enable PCLK1 (USART2 is on APB1). */
*STM32_RCC_APB1ENR |= (0x1 << 17); /* USART2EN - Enable USART2 clock. */

/* Enable PCLK2 (GPIOA is used by USART2). */
*STM32_RCC_APB2ENR |= (1 << 2); /* IOPAEN - Enable GPIOA clock. */

/* Set up the PA2 pin for alternate function push/pull. This is the
TX pin for USART2. */
*STM32_GPIOA_CRL =
(2 << 10) | /* CNF2 - Alternate function output push/pull. */
(1 << 8); /* MODE2 - Output mode, max 10 MHz. */

/* Set up the Baud Rate Register for 9600 bps. See RM0008, chapter
24.3.4. The value to be programmed in the BRR should be 234.375.
DIV_Mantissa=234 DIV_Fraction=6. */
*STM32_USART2_BRR = (234 << 4) | 6; /* 9600 bps */

/* After-reset default values for other registers are suitable for an
8-N-1 setup without enabled interrupts and DMA. */

/* Enable the USART and the transmitter. */
*STM32_USART2_CR1 = (1 << 13) | (1 << 3);


As I mentioned, I did not set up the receiver as this code is just a simple test to output characters over a serial line. Most of the lines are pretty self-explanatory I think, but there are a few thing worth noting. First, you have to enable clocking not only of the USART, but also for the corresponding GPIO port (in this case GPIOA). Second, when setting up the baud rate register, the fraction value is 16 times the fraction (0.375) i.e. 6. This is the value to be used when the clock frequency used for APB1 is 36 MHz (which is the maximum allowed frequency for APB1, and half the system clock). Do not forget to set up the APB1 clock prescaler!

Thursday, September 10, 2009

Some things have happened.

I have to apologize for the very bad update ratio on the blog. Hopefully someone will return to it anyway!

Since my last post, some 3 months ago, the kernel now implements 5 different system calls. These are:
  • yield
  • allocate a buffer
  • transmit a buffer to another process
  • receive a buffer from some other process
  • dispose of a buffer
The kernel is now a very simple message passing based one. It also handles different priorities, so that at every moment, the highest-prioritized ready process will run. Processes can now be written in C, as system call library functions are provided.

A couple of months ago, someone asked about a description of the entire process from booting up to scheduling. I will attempt to describe a few important steps:

First of all, the kernel and applications only run from RAM. Some basic support for running from ROM is implemented in the kernel and in the BSP (startup routines). There should not really need to be a lot of differences between these two cases and support for running from ROM will be there in time.

The way I run the system right now is using OpenOCD and GDB to download an elf file to the board. The elf file contains the monolith consisting of the kernel, the BSP and the application, everything linked to run from RAM. After downloading, I reset the board and let it run.

The first thing that happens is that the CPU is reset, which means that it will use the default exception vector location (which is at address 0x00000000, i.e. in flash) to find the reset routine. I have (once and for all) put a small assembly routine in flash, which is pointed out by the reset vector. This routine reads the desired reset vector from RAM (at 0x20000004), the initial main stack pointer (at 0x20000000) and simply jumps to the boot routine pointed out by the address in 0x20000004, which is the BSP startup. This bootstrapping assembly routine is not part of the OS, nor the BSP.

The BSP startup routine first of all sets up the STM32 clocks, so that the CPU core runs at 72 MHz (SYSCLK) which is generated from an 8 MHz external crystal which is multiplied by 9 using a PLL. AHB and APB2 clocks are also set up properly.

After setting up all clocks, the BSS segment is cleared. The area to be cleared is obtained by getting the addresses of the _bss_start and _bss_end symbols. These symbols are defined in the linker command file which is supplied by the BSP.

Next, by writing to the NVIC_VTOR register, the exception table location is moved to RAM (where it already is loaded). The GPIO module is set up to allow flashing the status LED.

After that, the BSP is almost finished setting up the board. There is one more thing to consider before handing over to the kernel.

All kernel setup processes (kernel initialization, process creation) must run from handler mode. I'll get back to the reason for that later. The default CPU mode after reset is thread mode, and the only way to switch to handler mode is through an exception. Therefore, the BSP writes the address of the kernel startup routine,
to the SVC (system call) vector in RAM and triggers it by issuing an svc instruction. This results in a switch to handler mode and a jump to the kernel.

The kernel startup routine clears the kernel pool area, then calls a hook which the application is supposed to implement. This hook function should do a number of rtos_create_process calls. This call allows the application to specify a process entry point, a priority and a stack size. For each call, a PCB is created in the kernel pool area, as well as a stack. The PCB is sorted into a readylist. After all these calls, the kernel is ready to start the highest prioritized process.

I mentioned earlier that the kernel startup must run from handler mode. The reason for this is that when the first process is scheduled, it will be started in the same way as if it was resumed after preemption. Therefore, for each created process, the architecture specific code is called to prepare the process stack in a way such that returning to the process and resuming it actually starts the process from the first instruction and with an empty stack.

When an application issues a system call, this is done by putting the syscall id (an integer, ranging from 0 to 4) in register r12 (done by an inline assembly routine). The scratch registers r0-r3 are transparently sent from the function call to the svc exception handler. The exception handler uses r12 as an offset into a jump table. Then a jump is taken to the system call implementation.

No interrupt handling is implemented yet, i.e. I haven't had to deal with critical regions in the kernel yet. It is still to be decided for instance what exception priority levels will be used for the SVC call in relation to other exceptions (interrupts from peripherals).

There is yet some more to be written about for instance context switching using the PendSV exception. I'll return to that later!

Wednesday, June 10, 2009

Flash a LED!

Yesterday, I configured GPIO port C to be able to flash the status LED. I now have two processes yielding to each other. One of them turns on the LED, the other turns it off. Here is one of them. It writes to the GPIO C BSRR (bit set/reset register) to set port C bit 12 high, which turns the LED off.


.global CM3_proc0
.thumb_func

CM3_proc0:
ldr r1, =0x40011010 @ PORT C BSRR register.
mov r2, 0x00001000 @ Set bit 12.
str r2, [r1]
mov r0, 0x1 @ Yield system call.
svc @ Perform the system call.
mov r0, 0x800000 @ Delay loop.
loop0:
sub r0, 0x1
cmp r0, 0x0
bne loop0
b CM3_proc0


Tuesday, May 26, 2009

A Fablab opens up in Malmö!

A couple of days ago I read about the Fablab opening up soon in Malmö in the southern part of Sweden. I am really happy about this. At least a couple of my sparetime electronic projects have ended up as a PCB without a box or any mechanics at all. For instance, my MP3 player looks like this:


When I designed the PCB, I was planning to design and build a box afterwards, but I realize I'll never get down to actually doing that, drilling and sawing acrylic plastic is just too cumbersome. With a fablab, I will be able to design and mill my own boxes and mechanics without an enormous budget.

Be sure to check in http://www.fablab.se/.

Wednesday, May 13, 2009

First successful context switch!

A couple of days ago, I ran my first fully working context switches. I have two small test processes that actually just yield to each other:

/* Test processes. */
__attribute__ ((naked)) static void proc0()
{
for (;;)
{
asm("mov r0, 0x1\n\t"
"svc\n\t");
}
}

__attribute__ ((naked)) static void proc1()
{
for (;;)
{
asm("mov r0, 0x1\n\t"
"svc\n\t");
}
}



The code is really an ugly mix of C and assembly, and it seems like the __attribute__ ((naked)) thing completely fools gdb as well. If i put a breakpoint on one of these two functions gdb does not put it on the first instruction, as you would probably want it to. Instead, it puts it one instruction after the first instruction. My guess is that gdb believes the first instruction to be a preamble for setting up the stack frame. But we don't have any stack frames here, due to the special attribute used. The workaround is to put a breakpoint on the address of the first instruction.

The processes put 1 in r0, which means call the yield system call, which in turns causes the simple scheduler to switch to the other process.

Thursday, May 7, 2009

A few shots of the development board.

Yesterday, I took a few pictures of the development board and the OCD module I use.

This is the STM32-P103 from Olimex. It features an STM32F103RBT6 µC from ST Microelectronics. It is based on the Cortex M3 architecture from ARM. The 32-bit CPU runs at up to 72 MHz. Equipped with 128 KiB of flash and 20 KiB of SRAM, it should be enough for many fun projects:



A view from the bottom side, showing the SD/MMC card connector:



The ARM-USB-OCD adapter that I use to hook the board up to my Linux laptop: