RetroBSD
http://retrobsd.org/

udelay() msdelay() msecs() usecs() cpus()
http://retrobsd.org/viewtopic.php?f=5&t=37309
Page 1 of 1

Author:  Pito [ Fri Oct 09, 2015 5:45 pm ]
Post subject:  udelay() msdelay() msecs() usecs() cpus()

delay.c with inidelay()

delay.c
Code:
// udelay() msdelay() msecs() usecs() cpus()
// with precalculated coefs
// Serge and Pito 10/2015
// for SmallerC

#include <stdio.h>
#include <sys/sysctl.h>
#include <machine/cpu.h>

int coef1, coef2;

int cpu_khz()
{
    int mib[2], khz;
    size_t size;

    mib[0] = CTL_MACHDEP;
    mib[1] = CPU_FREQ_KHZ;
    size = sizeof(khz);
    if (sysctl(mib, 2, &khz, &size, NULL, 0) < 0) {
        perror("sysctl");
        return -1;
    }
    return khz;
}

int bus_khz()
{
    int mib[2], khz;
    size_t size;

    mib[0] = CTL_MACHDEP;
    mib[1] = CPU_BUS_KHZ;
    size = sizeof(khz);
    if (sysctl(mib, 2, &khz, &size, NULL, 0) < 0) {
        perror("sysctl");
        return -1;
    }
    return khz;
}

unsigned c0_count()
{
    asm ("rdhwr $2, $2");
}

int coef1, coef2;

void inidelay(void) {
   coef1 = cpu_khz() / 2000;
   coef2 = cpu_khz() / 2;
}

void udelay(unsigned usec)
{
    unsigned now = c0_count();
    unsigned final = now + (usec * coef1);
           
    for (;;) {
        now = c0_count();
        if ((int) (now - final) >= 0)
            break;
    }
}

void msdelay(unsigned mssec)
{
    unsigned now = c0_count();
    unsigned final = now + (mssec * coef2);
           
    for (;;) {
        now = c0_count();
        if ((int) (now - final) >= 0)
            break;
    }
}

unsigned cpus()
{
    unsigned now = c0_count();
    return (now * 2);
}

unsigned usecs()
{
    unsigned now = c0_count();
    return (now / coef1);
}

unsigned msecs()
{
    unsigned now = c0_count();
    return (now / coef2);
}

delay.h
Code:
// udelay() msdelay() msecs() usecs() cpus()
// with precalculated coefs
// Serge and Pito 10/2015
// for SmallerC

extern int cpu_khz();
extern int bus_khz();
extern unsigned c0_count();
extern void inidelay(void);
extern void udelay(unsigned usec);
extern void msdelay(unsigned mssec);
extern unsigned cpus();
extern unsigned usecs();
extern unsigned msecs();


testdel.c
Code:
#include "delay.h"

int main() {

unsigned dl, ms1, us1, ms, us;

inidelay();

dl = 1000000;

ms1 = msecs();
us1 = usecs();

udelay(dl);

us = usecs();
ms = msecs();

printf("us1= %d ms1= %d\r\n", us1, ms1);
printf("us= %d ms= %d\r\n", us, ms);

us = us - us1;
ms = ms - ms1;

printf("use= %d mse= %d\r\n", us, ms);

return 0;
}

Code:
# cc -o delay.o -c delay.c
# cc -o tst delay.o testdel.c
# ./tst
us1= 49475021 ms1= 49475
us= 50475036 ms= 50475
use= 1000015 mse= 1000
# ./tst
us1= 52532145 ms1= 52532
us= 53532161 ms= 53532
use= 1000016 mse= 1000
# ./tst
us1= 55578436 ms1= 55578
us= 56578452 ms= 56578
use= 1000016 mse= 1000
# ./tst
us1= 58434049 ms1= 58434
us= 59434066 ms= 59434
use= 1000017 mse= 1000
# ./tst
us1= 61944544 ms1= 61944
us= 62944560 ms= 62944
use= 1000016 mse= 1000
#

With dl=5000000
Code:
# ./tst
us1= 748332323 ms1= 748332
us= 753332338 ms= 753332
use= 5000015 mse= 5000
# ./tst
us1= 757297878 ms1= 757297
us= 762297895 ms= 762297
use= 5000017 mse= 5000
# ./tst
us1= 764543169 ms1= 764543
us= 769543185 ms= 769543
use= 5000016 mse= 5000
# ./tst
us1= 772752695 ms1= 772752
us= 777752711 ms= 777752
use= 5000016 mse= 5000
# ./tst
us1= 780234939 ms1= 780234
us= 785234953 ms= 785234
use= 5000014 mse= 5000
#

Author:  Pito [ Fri Oct 09, 2015 8:29 pm ]
Post subject:  Re: udelay() msdelay() msecs() usecs() cpus()

Code:
2.11 BSD Unix for PIC32, revision G403 build 9:
     Compiled 2015-10-09 by pito@UBU1404:
     /home/pito/Retronew/retrobsd/sys/pic32/baremetal
cpu: 795F512H 20 MHz, bus 20 MHz
oscillator: HS crystal, PLL div 1:3 mult x15

Code:
# ./tst
us1= 283640002 ms1= 283639
us= 288640009 ms= 288640
use= 5000007 mse= 5001
# ./tst
us1= 297513675 ms1= 297513
us= 302513682 ms= 302513
use= 5000007 mse= 5000
# ./tst
us1= 353656370 ms1= 353656
us= 358656376 ms= 358656
use= 5000006 mse= 5000
#

Author:  Pito [ Sun Oct 11, 2015 1:52 pm ]
Post subject:  Re: udelay() msdelay() msecs() usecs() cpus()

UPDATE - see above - split into delay.c and delay.h (i2c.h, i2c.c, pins.h, pins.c)
Example:
Code:
# cc -o delay.o -c delay.c
# cc -o i2c.o -c i2c.c
# cc -o pins.o -c pins.c
# cc -o i2cscan i2c.o pins.o delay.o i2cscan.c
# ./i2cscan
I2C dev found, addr(r/w): 00(01/00)  1E(3D/3C)  4C(99/98)  53(A7/A6)  57(AF/AE)  68(D1/D0)  69(D3/D2)  77(EF/EE)

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/