Beispiel #1
0
int main(void)
{
  uint32_t ticks = 0;
  uint32_t timerstate;
  or1k_timer_init(100);

  gpio_init();

  or1k_timer_enable();
  while (1) {
    while (ticks == or1k_timer_get_ticks()) { }
    ticks++;
    timerstate = or1k_timer_disable();
    // do something atomar
    or1k_timer_restore(timerstate);
    if (ticks == 100) {
      printf("A second elapsed\n");
      // Increment the GPIO counter
      gpio_increment();
      or1k_timer_reset_ticks();
      ticks = 0;
    }
  }
  
  return 0;
}
Beispiel #2
0
int main(void)
{

  printf("\r\n");
  printf("Timer + UART demo\r\n");
  printf("Press any key to increment the LED counter, except backspace which will\r\n");
  printf("cause it to decrement\r\n");

  /* Set the GPIO to all out */
  char dir = 0xff;
  *(gpio_base+1) = dir;
  /* Initialise with the first data value */
  *(gpio_base+0) = dat;

  /* Initialise the counter at 1Hz, it won't start yet */
  or1k_timer_init(1);

  /* Install a custom timer handler */
  or1k_exception_handler_add(0x5,or1k_timer_interrupt_handler_new);
  
  /* OK, start the timer now */
  or1k_timer_enable();

  /* Now loop and check for UART input */
  while (1)
    {
      char c = __uart_getc();

      /* If it was backspace, decrement the LEDs */
      if (c==0x7f)
	dat-=2;

      /* Update the GPIO LED output on any keypress, just like the timer had
	 gone off */
      or1k_timer_user_handler();
      
    }

  return 0;
}
int main(int argc, char *argv[]) {
    clock_t start, stop;
    double ct, cmin = DBL_MAX, cmax = 0;
    int i, cminix, cmaxix;
    long j, n, seed;
    int iterations;
    unsigned int ticks;
    static int (* CDECL pBitCntFunc[FUNCS])(long) = {
        bit_count,
        bitcount,
        ntbl_bitcnt,
        ntbl_bitcount,
        /*            btbl_bitcnt, DOESNT WORK*/
        BW_btbl_bitcount,
        AR_btbl_bitcount,
        bit_shifter
    };
    static char *text[FUNCS] = {
        "Optimized 1 bit/loop counter",
        "Ratko's mystery algorithm",
        "Recursive bit count by nybbles",
        "Non-recursive bit count by nybbles",
        /*            "Recursive bit count by bytes",*/
        "Non-recursive bit count by bytes (BW)",
        "Non-recursive bit count by bytes (AR)",
        "Shift and count bits"
    };
    if (argc<2) {
        printf("Defaulting to %d iterations\n", ITERATIONS);
        iterations = ITERATIONS;
    } else {
        iterations=atoi(argv[1]);
    }

    puts("Bit counter algorithm benchmark\n");

    or1k_timer_init(TIMER_HZ);
    or1k_timer_enable();

    for (i = 0; i < FUNCS; i++) {
        start = clock();

        for (j = n = 0, seed = rand(); j < iterations; j++, seed += 13)
            n += pBitCntFunc[i](seed);

        stop = clock();
        ct = (stop - start) / (double)CLOCKS_PER_SEC;
        if (ct < cmin) {
            cmin = ct;
            cminix = i;
        }
        if (ct > cmax) {
            cmax = ct;
            cmaxix = i;
        }

        printf("%-38s> Time: %7.3f sec.; Bits: %ld\n", text[i], ct, n);
    }
    printf("\nBest  > %s\n", text[cminix]);
    printf("Worst > %s\n", text[cmaxix]);

    ticks = or1k_timer_get_ticks();
    printf("Elapsed: %d ticks at %d Hz\n", ticks, TIMER_HZ);

    return 0;
}
int main(void) {
    double  a1 = 1.0, b1 = -10.5, c1 = 32.0, d1 = -30.0;
    double  x[3];
    double X;
    int     solutions;
    int i;
    unsigned long l = 0x3fed0169L;
    struct int_sqrt q;
    long n = 0;
    unsigned int ticks;

    or1k_timer_init(TIMER_HZ);
    or1k_timer_enable();

#if 1
    /* solve soem cubic functions */
    printf("********* CUBIC FUNCTIONS ***********\n");
    /* should get 3 solutions: 2, 6 & 2.5   */
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = 1.0;
    b1 = -4.5;
    c1 = 17.0;
    d1 = -30.0;
    /* should get 1 solution: 2.5           */
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = 1.0;
    b1 = -3.5;
    c1 = 22.0;
    d1 = -31.0;
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = 1.0;
    b1 = -13.7;
    c1 = 1.0;
    d1 = -35.0;
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = 3.0;
    b1 = 12.34;
    c1 = 5.0;
    d1 = 12.0;
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = -8.0;
    b1 = -67.89;
    c1 = 6.0;
    d1 = -23.6;
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = 45.0;
    b1 = 8.67;
    c1 = 7.5;
    d1 = 34.0;
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    a1 = -12.0;
    b1 = -1.7;
    c1 = 5.3;
    d1 = 16.0;
    SolveCubic(a1, b1, c1, d1, &solutions, x);
    printf("Solutions:");
    for(i=0; i<solutions; i++)
        printf(" %f",x[i]);
    printf("\n");

    /* Now solve some random equations */
    for(a1=1; a1<10; a1+=1) {
        for(b1=10; b1>0; b1-=.25) {
            for(c1=5; c1<15; c1+=0.61) {
                for(d1=-1; d1>-5; d1-=.451) {
                    SolveCubic(a1, b1, c1, d1, &solutions, x);
                    printf("Solutions:");
                    for(i=0; i<solutions; i++)
                        printf(" %f",x[i]);
                    printf("\n");
                }
            }
        }
    }
#endif

#if 1
    printf("********* INTEGER SQR ROOTS ***********\n");
    /* perform some integer square roots */
    for (i = 0; i < 100000; i+=2) {
        usqrt(i, &q);
        // remainder differs on some machines
        // printf("sqrt(%3d) = %2d, remainder = %2d\n",
        printf("sqrt(%3d) = %2d\n",
               i, q.sqrt);
    }
    printf("\n");
    for (l = 0x3fed0169L; l < 0x3fed4169L; l++) {
        usqrt(l, &q);
        //printf("\nsqrt(%lX) = %X, remainder = %X\n", l, q.sqrt, q.frac);
        printf("sqrt(%lX) = %X\n", l, q.sqrt);
    }
#endif

#if 1
    printf("********* ANGLE CONVERSION ***********\n");
    /* convert some rads to degrees */
    /* for (X = 0.0; X <= 360.0; X += 1.0) */
    for (X = 0.0; X <= 360.0; X += .001)
        printf("%3.0f degrees = %.12f radians\n", X, deg2rad(X));
    puts("");
    /* for (X = 0.0; X <= (2 * PI + 1e-6); X += (PI / 180)) */
    for (X = 0.0; X <= (2 * PI + 1e-6); X += (PI / 5760))
        printf("%.12f radians = %3.0f degrees\n", X, rad2deg(X));

    ticks = or1k_timer_get_ticks();
    printf("********* ANGLE CONVERSION ***********\n");
#endif
    printf("Elapsed: %d ticks at %d Hz\n", ticks, TIMER_HZ);

    return 0;
}