[uCsimm] uClinux - two utilities

From: Stu (doktor1@earthlink.net)
Date: Sun Apr 02 2000 - 17:15:43 EDT


These two utilities are trivial, but somewhat useful. I wrote them a long
time ago but never bothered to release them. The first program displays the
system uptime. The second (somewhat unfinished but functional) turns pin 22
on and off at a certain interval (useful for an LED or whatever). I like to
put it in my /etc/rc file so that the LED blinks while the system is
running. Have fun.

------------------------------
/* uptime.c
 * by Stuart Lauderdale <doktor1@earthlink.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdio.h>
#include "/opt/uClinux/m68k-pic-coff/include/stdlib.h"
#include <string.h>

int main(void) {
        FILE *fd; /* File descriptor */
        char buffer[255]; /* Buffer uptime output will go to */
        int i; /* Counter */
        long int seconds; /* Number of seconds since boot */
        int dd; /* Final number of days */
        int hh; /* Final number of hours */
        int mm; /* Final number of minutes */

        /* Read the uptime string into buffer */
        fd = fopen("/proc/uptime", "r");
        fgets(buffer,254,fd);
        fclose(fd);

        /* The uptime string is actually 2 numbers, so truncate it to 1 */
        for(i=0; i<255; i++) {
                if(buffer[i]==32) buffer[i]=0; /* Truncate if space */
        }

        /* Now convert it to a long integer */
        seconds=atoi(buffer);

        /* Calculate seconds for each time interval (days, hours, minutes).
         * In the second column, we remove the proper number of seconds from
         * the total number of seconds - after all, we want to know the number
         * of days, hours, minutes, and seconds, not the total number of days,
         * the total numbers of hours, the total number of minutes, and the
         * total number of seconds.
         */

        dd=seconds/86400; seconds=seconds-(dd*86400);
        hh=seconds/3600; seconds=seconds-(hh*3600);
        mm=seconds/60; seconds=seconds-(mm*60);

        /* BUG: printf doesn't add the leading zero if n<9 under uClinux, so
         * you get something like "Uptime: 0 days, 1:4:44." instead of
         * "Uptime: 0 days, 01:04:44." It DOES add the leading zero if
         * you compile it with regular gcc under x86 linux.
         */

        printf("Uptime: %d days, %.2d:%.2d:%.2d.\n",dd,hh,mm,seconds);
        sleep(1);
        return 0;
}
------------------------------

------------------------------
/* ledblink.c
 * Derived from: Application Note from uCsimm manual
 * Tweaked by Stuart Lauderdale <doktor1@earthlink.net>, as follows:
 *
 * - TBD: Allow it to take arguments (interval in uSec, [p]ulse, or [h]elp)
 * - Changed sleep() to usleep(), so we can have microseconds!
 *
 * Notes
 *
 * - The ledblink program doesn't seem to take up much in the way of
 * system resources.
 * - If the system is heavily loaded (e.g. you're ping-flooding it), the
 * LED will freeze in whatever state it's in until the load goes back
 * to normal (or the uCsimm gets reset).
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <unistd.h>
#include <stdlib.h>
#include <asm/MC68EZ328.h>

void printHelp(void);

int main(int argc, char **argv) {
        long int uSec; /* uSec delay between LED status toggle */

        uSec = 500000;

        PDSEL |= 0x01; /* Select the I/O function for PD0 */
        PDDIR |= 0X01; /* Set PD0 to output */

        printf("Starting LED blinker at %d uSec for LED on pin 22...\n",uSec);
        while(1) {
                PDDATA ^=0x01; /* Toggle PD0 */
                usleep(uSec);
        }
}

void printHelp(void) {
        puts("Usage: ledblink [-u mSec | -h ]");
        puts(" mSec Delay between toggling LED on or off (in microseconds)");
        puts(" h Print this help screen");
}
------------------------------

This message resent by the ucsimm@uclinux.com list server http://www.uClinux.com/



This archive was generated by hypermail 2b30 : Sun Apr 07 2002 - 00:01:35 EST