PWM on the USBee with custom firmware
In this article I described how to use the signal generator firmware to send out PWM signals generated on the PC. However, there is a better way. The USBee SX is based on a Cypress FX2lp, a microcontroller for which the firmware must be sent every time the device is connected. This gives an excellent oppertunity to experiment with custom firmwares that do fun stuff, such as driving led effects independently from the host...
The 8051 compatible instruction set can be programmed directly using open source tools (SDCC) with a header file describing the Special Function Registers (SFRs) specific to the platform (fx2regs.h). Warning: some of the fx2regs.h files floating around produce blatantly wrong code when used with SDCC, so be careful. The one that I used is written espcially for SDCC (by Christer Weinigel), and licensed under a permissive (X11/MIT) license.
Another thing that is important to know for the USBee SX is that PORTB is connected to the output pins, bit 0 is connected to pin 0 and so on. All pins can be configured either as input or as output.
In my bitbucket reposity I put a Python script to upload and launch arbitrary firmware: exec.py. The firmware doesn't need to do anything with USB, it can simply go into a loop driving the outputs. I've also included an example firmware that blinks a led slowly using PWM plus a timer interrupt to set the level.
The core of the PWM generation code is a simple infinite loop that outputs a 1 if the counter is smaller than the defined level, and a 0 otherwise. The counter wraps at 255, making sure that level defines the part of the time that a 1 is output. Note that it is impossible to entirely disable the led here. Even if level=0, the led will be on 1/256th of the time.
Of course, keeping the level the same all the time is boring. If we want to change the value in time, a timer interrupt comes in handy:
When the timer interrupt triggers, which happens when the timer counter overflows after
0xFFFF, this code restores the value of the timer counter word, and increases the brightness level. In this way, we can determine the blinking speed with [timer_h, timer_l]-- the closer it is to
0xFFFF, the faster the level will change. Before the timer interrupt will happen, the timer needs to be enabled:
For an explanation of the bits please refer to the data sheet.