gms0012
Posts: 6
Joined: Sat Jun 14, 2014 6:14 am

c libs

Sun Jun 15, 2014 7:34 pm

hi friends..

are there c libs available für the bare metal pi?

like the arduino libs?


thanks

User avatar
JRV
Posts: 270
Joined: Mon Apr 02, 2012 1:39 pm
Location: Minneapolis, MN

Re: c libs

Mon Jun 16, 2014 12:37 pm

Is this what you are looking for: http://wiringpi.com/

gms0012
Posts: 6
Joined: Sat Jun 14, 2014 6:14 am

Re: c libs

Tue Jun 17, 2014 7:58 am

hi .

i am not sure..

that looks for me that there must be running a linux kernel.. or did i understand it wrong?

User avatar
rpdom
Posts: 17719
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: c libs

Tue Jun 17, 2014 8:06 am

I'm fairly certain those libraries are only for accessing the hardware from programs written to run under Linux, not with a bare metal system.

Accessing the majority of GPIO functions is fairly easy in bare metal, either from C or Assembler (my preferred method).

hldswrth
Posts: 108
Joined: Mon Sep 10, 2012 4:14 pm

Re: c libs

Tue Jun 17, 2014 10:29 am

The thing about bare metal is that its entirely up to you. Any C library is going to be based on a load of low-level functions that are specific to whatever bare-metal kernel they are written for (e.g. write to device, allocate memory, etc.). So a bare-metal C library is meaningless on its own. You need a kernel to go with it, or else have the source code and port the C library to use your own low-level kernel functions.
There is at least one open source C library out there I believe, called newlib, which you could try to port to your kernel. I've not looked at it myself.
Alternatively, perhaps the question is really, has anyone got a bare metal kernel + C library?

gms0012
Posts: 6
Joined: Sat Jun 14, 2014 6:14 am

Re: c libs

Tue Jun 17, 2014 11:18 am

yes.. the last thing would be great..

like this http://www.ti.com/tool/starterware-sitara for the BeageBoard..

is there not such a thing for the RI?

User avatar
joan
Posts: 15093
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: c libs

Tue Jun 17, 2014 11:35 am

All the Linux C libraries use /dev/mem to access the gpios. It should be simple to extract the low level routines (read/write/PUD up/down/off/read mode/set mode) and use them on bare metal.

For instance here is part of the source for the pigpio C library.

Code: Select all

/* ----------------------------------------------------------------------- */

int gpioSetMode(unsigned gpio, unsigned mode)
{
   int reg, shift;

   DBG(DBG_USER, "gpio=%d mode=%d", gpio, mode);

   CHECK_INITED;

   if (gpio > PI_MAX_GPIO)
      SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);

   if (mode > PI_ALT3)
      SOFT_ERROR(PI_BAD_MODE, "gpio %d, bad mode (%d)", gpio, mode);

   reg   =  gpio/10;
   shift = (gpio%10) * 3;

   if (gpio <= PI_MAX_USER_GPIO)
   {
      if (mode != PI_OUTPUT)
      {
         switch (gpioInfo[gpio].is)
         {
            case GPIO_SERVO:
               /* switch servo off */
               myGpioSetServo(gpio,
                  gpioInfo[gpio].width/gpioCfg.clockMicros, 0);
               break;

            case GPIO_PWM:
               /* switch pwm off */
               myGpioSetPwm(gpio, gpioInfo[gpio].width, 0);
               break;
         }

         gpioInfo[gpio].is = GPIO_UNDEFINED;
      }
   }

   gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);

   return 0;
}


/* ----------------------------------------------------------------------- */

int gpioGetMode(unsigned gpio)
{
   int reg, shift;

   DBG(DBG_USER, "gpio=%d", gpio);

   CHECK_INITED;

   if (gpio > PI_MAX_GPIO)
      SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);

   reg   =  gpio/10;
   shift = (gpio%10) * 3;

   return (*(gpioReg + reg) >> shift) & 7;
}


/* ----------------------------------------------------------------------- */

int gpioSetPullUpDown(unsigned gpio, unsigned pud)
{
   DBG(DBG_USER, "gpio=%d pud=%d", gpio, pud);

   CHECK_INITED;

   if (gpio > PI_MAX_GPIO)
      SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);

   if (pud > PI_PUD_UP)
      SOFT_ERROR(PI_BAD_PUD, "gpio %d, bad pud (%d)", gpio, pud);

   *(gpioReg + GPPUD) = pud;

   myGpioDelay(20);

   *(gpioReg + GPPUDCLK0 + BANK) = BIT;

   myGpioDelay(20);
  
   *(gpioReg + GPPUD) = 0;

   *(gpioReg + GPPUDCLK0 + BANK) = 0;

   return 0;
}


/* ----------------------------------------------------------------------- */

int gpioRead(unsigned gpio)
{
   DBG(DBG_USER, "gpio=%d", gpio);

   CHECK_INITED;

   if (gpio > PI_MAX_GPIO)
      SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);

   if ((*(gpioReg + GPLEV0 + BANK) & BIT) != 0) return PI_ON;
   else                                         return PI_OFF;
}


/* ----------------------------------------------------------------------- */

int gpioWrite(unsigned gpio, unsigned level)
{
   DBG(DBG_USER, "gpio=%d level=%d", gpio, level);

   CHECK_INITED;

   if (gpio > PI_MAX_GPIO)
      SOFT_ERROR(PI_BAD_GPIO, "bad gpio (%d)", gpio);

   if (level > PI_ON)
      SOFT_ERROR(PI_BAD_LEVEL, "gpio %d, bad level (%d)", gpio, level);

   if (gpio <= PI_MAX_USER_GPIO)
   {
      if (gpioInfo[gpio].is != GPIO_WRITE)
      {
         if (gpioInfo[gpio].is == GPIO_UNDEFINED)
         {
            if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
            else                 *(gpioReg + GPSET0 + BANK) = BIT;
            gpioSetMode(gpio, PI_OUTPUT);
         }
         else if (gpioInfo[gpio].is == GPIO_PWM)
         {
            /* switch pwm off */
            myGpioSetPwm(gpio, gpioInfo[gpio].width, 0);
         }
         else if (gpioInfo[gpio].is == GPIO_SERVO)
         {
            /* switch servo off */
            myGpioSetServo(
               gpio, gpioInfo[gpio].width/gpioCfg.clockMicros, 0);
         }

         gpioInfo[gpio].is=GPIO_WRITE;
         gpioInfo[gpio].width=0;
      }
   }

   if (level == PI_OFF) *(gpioReg + GPCLR0 + BANK) = BIT;
   else                 *(gpioReg + GPSET0 + BANK) = BIT;

   return 0;
}
If you remove all the error checking and debugging paraphernalia it ends up as bare metal routines.

valtonia
Posts: 26
Joined: Wed Jul 04, 2012 9:09 pm

Re: c libs

Thu Jun 19, 2014 9:28 pm

Here's a really ancient all-c library that gives you most of the basic functions. It doesn't include printf as that's quite platform specific. I don't remember where this code came from now - it might even have been from a book!

Anyway, it might be enough to get you started.

http://pastebin.com/C8yhHi4E

Return to “Bare metal, Assembly language”