Skip to main content

Posts

2014


msp430 - basics

·6 mins

It’s interesting that msp430 has low power mode (LPM) which enables the MCU to get into sleep mode and stay operating. There are 6 profiles for this: 5 low-power modes, and one active mode.

These modes related to which components get disabled, thus lowering power consumption. The components are: CPU, MCLK (Master Clock), SMCLK (Sub-System Master Clock), ACLK (Auxiliary Clock), DCO (Digitally Controlled Oscillator), and internal crystal oscillator. By default, after power-up, DCO is used as the source oscillator for MCLK and SMCLK, while ACLK is sourced from a low-frequency (~32kHz) additional watch-crystal (LFXT1), All the clocks is software selectable, and can use either DCO, additional crystal (LFXT1), or internal low-power oscillator (VLO), but ACLK is the only clock that can not be configured to use DCO as its source oscillator.

Now back to the operating modes, here is the list of profile for msp430

  • Active mode is the normal operation profile, all components is enabled and running.
  • LPM 0 has CPU and MCLK disabled
  • LPM 1 has CPU, MCLK, and DCO disabled. DCO only disabled if it’s not used in active mode.
  • LPM 2 has CPU, MCLK, and SMCLK disabled.
  • LPM 3 has CPU, MCLK, SMCLK, and DCO disabled.
  • LPM 4 has CPU, MCLK, SMCLK, ACLK, DCO, and internal crytal oscillator disabled.

So yes, LPM 4 is the deep sleep mode.

The low-power mode can be set to be interruptible. That is, by setting the interrupt-enable flag together with the LPM profile. We’ll see how to do that in a moment.

The usual first program for microcontroller is the grand LED-blinking code. And to do that naively is to XOR-ing the bit to the pin where the led is mounted, and add some delay/sleep for some cycles so it’s alternating between high and low currents.

#include <msp430.h> 

int main(void) {
    WDTCTL = WDTPW | WDTHOLD;   // Stop watchdog timer
    /* Set P1.0 direction to output */
    P1DIR |= BIT0;

    /* Set P1.0 output high */
    P1OUT |= BIT0;

    while (1) {
        /* Wait for 200000 cycles */
        __delay_cycles(200000);
        /* Toggle P1.0 output */
        P1OUT ^= BIT0;
    }

    return 0;
}

/* Adapted from http://simplyembedded.wordpress.com/tutorials/lesson3/
 */

Now you see the code above would require the MCU to run in active mode all the time, and for MSP430 to living up to its title, we can optimize the code so it can run with less power consumption (shame on me because I haven’t measure the actual power consumption yet). First let’s take a look at the first line inside our main block above. The default watchdog timer was set to do a h/w reset in every 32ms (or the default timer set, since it’s probably different), so we have to hold it, or more like killing it, since we wont have anything to do with the timer.

But hey, it’s a timer. Could we possibly use that as the timer for our blinking led, sure we could. The watchdog can be configured to interrupt the normal execution and move the PC (Program Counter) to the ISR (Interrupt Service Routine), and execute the code over there. This combined with the low-power mode can be used to eliminate the infinite loop above, just like this:

#include <msp430.h> 

int i;

int main(void) {
    /* Set Watchdog timer to interrupt in interval 32ms,
     * clocked from SMCLK. */
    WDTCTL = WDT_MDLY_32;
    IE1 |= WDTIE;

    /* Set P1.0 direction to output */
    P1DIR |= BIT0;

    /* Set P1.0 output high */
    P1OUT |= BIT0;

    i = 0;

    __bis_SR_register(LPM1_bits | GIE);
}


/* Watchdog Timer interrupt service routine */
#if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
#pragma vector=WDT_VECTOR
__interrupt void watchdog_timer(void)
#elif defined(__GNUC__)
void __attribute__ ((interrupt(WDT_VECTOR))) watchdog_timer (void)
#else
#error Compiler not supported!
#endif
{
    i++;
    if (i >= 25) {
        P1OUT ^= BIT0;                            /* Toggle P1.0 using exclusive-OR */
        i = 0;
    }
}

For now I wont mind with the extra flag checking over there, but in case you curious, it’s checking for the compiler version and will use the pragma and __interrupt void watchdog_timer(void) for TI or IAR C compiler, and will use a bit longer line void __attribute__ ((interrupt(WDT_VECTOR))) watchdog_timer (void) for gcc. Other than those compiler, throw error.

Now back to our topic. I said I will show you how to set an interruptible low-power mode, and that it is. With some addition, actually, we should add the Watchdog interrupt-enable flag to the register, then proceed to the LPM1 mode while standby for any interrupts registered.

We defined the ISR as watchdog_timer and since 32ms is a bit too fast, we multiply the counter by 25 so it will blinking with interval at roughly 800ms.

got the launchpad

·2 mins

Here’s the new toy. An evaluation board of the legendary MSP430 series. It comes with 2 MCUs the MSP430G2553, a spare MSP4302452, and an 32kHz crystal for external oscillator. I’ll be working with MSP430G2553 mainly because it has twice the memory of the latter, another reason is since MSP430G2553 has its own h/w UART support, but since the board could do soft emulation for the UART (need jumper switching and some emulation code though), I think I will tinkering with the latter too, that is after I comfortable with the UART setup.

read.shreds

·2 mins

I’ve been putting up some courage to actually write about this thing. Started working on this roughly 1 and a half years ago, I believe it was not long after Google initially announced that they’ll closing Google Reader.

As I realized that I never build any barely-usable so-called-product, also for the fact that my related skills on this were sound more like a joke, I kept the repository private for some months. It was really hard when feedbin announced, since we both built based on the same stack (We both based on ruby on rails, and using feedzira (later feedjira) as our core feed fetcher). I think I was supposed to drop the project and use feedbin instead, considering it’s waaay more superior in addition of its massive community.

rusty quine

·1 min
This is an old entry actually: fn main() { let text = r##" println!("fn main() {}\n let text = {}{}{}{}{}{}{}{};\n{};\n{}", "{", "r", "#", "#", '"', text, '"', "#", "#", text, "}")"##; println!("fn main() {}\n let text = {}{}{}{}{}{}{}{};\n{};\n{}", "{", "r", "#", "#", '"', text, '"', "#", "#", text, "}"); } That is the first quine and the first rust code I have ever written. Really want to actually invest some time on this language.

on OSX

·2 mins

So, I’ve been using an mba for the last 4 months, and it’s quite nice. The case is shiny and all, it is lightweight, and the desktop is beautiful for a BSD-ish OS. I was a bit worried the experience will turned out like Brendan Gregg’s osx-kernel-crash, good thing it’s not happened (probably yet). Though yes, I’ve experienced a lot of rendering glitches on Mavericks (too bad no snapshot on this), which is quite embarrassing, it makes you feel like using win98 all over again.

Overall, it’s fast, quite contrary to the most complain I heard about OSX being slow. While I think it’s mostly thanks to the SSD. I kind of suspecting people say OSX being slow since they closing apps via the close button or Cmd-W (which is just closing/hiding the window), and not using Cmd-Q to actually closing the app.

While I said the desktop is beautiful, it’s just beautiful on a glance. Things aren’t look any better after a while. And yep, this is also applies to Yosemite. I spend the day mostly on iTerm in fullscreen mode, and only back to the desktop for browser-related stuff.

does this thing still on?

·1 min
I think some time along the way when I planned to do this writeup-series about wrk etc… I got a bit overwhelmed by the lack of the C knowledge I have. And the fact that wrk is pretty much a fast-moving target. As of currently, it even already has Lua scripting support backed by LuaJIT. So while half part of myself really want to continue with the writeup, the other half wants to write about another things.

2013


Code Tidbits: wrk pt.1

·9 mins

I have this plan to do some write up series related to programming and such, mostly as another form of practicing C systems programming. Just when I wondered about what topic I should write, I found this gem written by Will Glozer. So I think I can study the source code and write some little explanation here and there about the code, hence the tidbits. Feel free to critic and correct things if you find mistakes on this. It would be my honour.

I kinda like the source code for its simplicity and learn a lot from them. The library used from redis including zmalloc and ae. I will going into zmalloc, before that, lets see this aprintf first.

Ookami Kodomo no Ame to Yuki

·2 mins

This one will sure off under my radar if [優]Yuu-sensei didn’t tweet about it, since I knew the movie thanks to her manga adaptation.

Ookami Kodomo no Ame to Yuki is Mamoru Hosoda’s latest movie featuring Miyazaki Aoi as Hana, mother of two werewolves Yuki and Ame. Her wolf husband died not long after she gave birth to Ame.

I’m not sure what I got from the movie. I’m not, in particular, a fan of Mamoru Hosoda, albeit I watched both Toki wo Kakeru Shoujo and Summer Wars, and some Digimon movies which I can only vaguely remember. But I can assure that this movie is sure delivers.

I’m biased, partly because I watched this in low quality format, also might because I read the manga first and set my expectation bar a bit too high. Some animation looked bland, frame-wasting scenes, subpar voice acting, yada yada.

But hey, maybe that’s just me. I will say again that I’m biased, so while I’m neutral about the movie, I will recommend the manga instead. I’m a fan of Yuu-sensei since the first time I found her comic in Gelatin’s 2010 Autumn issue. It’s hard to find her artwork on internet other than her site or her pixiv page. Her adaptation of Ookami kodomo is splendid. I love how she can put warm feeling on all of her drawings. The story itself not changed that much. It still taken from Yuki’s point of view.

VividRed Operation #3

·1 min
VividGreen is the best! period. Also the ending is written and arranged by DECO*27. Which is awesome

The Hook

·1 min
Referring to previous post, this is the post-receive script I used for the blog. Actually I forgot to attach this to the previous post, so I decide to just create another post for this. {% include_code post-receive lang:sh post-receive %} The locale entries is needed by jekyll, since it’s not set at git/www user environment Also don’t forget to add this entry to sudoers git ALL=(www) NOPASSWD: ALL Basically, it tells sudo to allow git to act as www without any password.