Pages

Monday, March 11, 2013

Guzunty Pi gz_test program studying notes


So I looked into the Guzunty/Pi/ directory and checked out the gz.test program.  I first found the Makefile which I know is sort of MSDOS3.3 batch file to compile the source program gz_test.c into binary executable file gz_test.  I think I can change the following 2 lines for programs other that gz_test.c.


SRC = gz_test.c
BIN = gz_test


Then I checked the gz_test.c program which found very short, and the main thing is to enable the GPCLK0 and disable it when the user exit the program.

gz_clock_ena(GZ_CLK_5MHz, 0x07f); // Turn on a slow clock
printf("\nPress any key to stop test.");
scanf("%c", &aChar);
gz_clock_dis();


The clock enable function is a bit complicated.  I noticed that there is a couple stars which I remember is the scary and confusing pointer thing.  I think I better ignore them and perhaps start porting to Python. 


gz_clock_ena(int speed, int divisor) 


...

bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_ALT0);
*(bcm2835_clk + 0x1C) = 0x5A000000 | speed_id;    // GPCLK0 off
while (*(bcm2835_clk + 0x1C) & GZ_CLK_BUSY) {}    // Wait for BUSY low
*(bcm2835_clk + 0x1D) = 0x5A002000 | (divisor << 12); // set DIVI
*(bcm2835_clk + 0x1C) = 0x5A000010 | speed_id;    // GPCLK0 on
return 0;


.END

#


# Makefile:
#       gz_test - Guzunty test program
#
# Copyright (c) 2013 campbellsan
########################################################################
#
#    Guzunty is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    Guzunty is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public
#    License along with the Guzunty distribution.  If not, please see
#    <http://www.gnu.org/licenses/>.
########################################################################

DEBUG = -g -O0
CC = gcc
INCLUDE = -I/usr/local/include
CFLAGS = $(DEBUG) -Wall $(INCLUDE) -Winline -pipe -fPIC -DDEBUG_MODE
LIBS    = -lgz -lbcm2835 -lrt

SRC = gz_test.c

BIN = gz_test

all:
$(CC) $(CFLAGS) $(SRC) -o $(BIN) $(LIBS) 
    
clean:
rm -f *~ $(BIN)


/*
 * gz_test.c
 * 
 * Copyright 2013  guzunty
 * 
 * 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.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */


#include <stdio.h>
#include <gz_clk.h>

int main(int argc, char* argv[])
{
  char aChar;
  
    gz_clock_ena(GZ_CLK_5MHz, 0x07f); // Turn on a slow clock
    printf("\nPress any key to stop test.");
    scanf("%c", &aChar);
    gz_clock_dis();
    return 0;
}

.END

/*
 * gz_clk.c
 * 
 * Copyright 2012  campbellsan
 * 
 * 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.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 */
#include<gz_clk.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define GZ_CLK_BUSY (1 << 7)

int gz_clock_ena(int speed, int divisor) {
  int speed_id = 6;
  if (speed < GZ_CLK_5MHz || speed > GZ_CLK_125MHz) {
    printf("gz_clock_ena: Unsupported clock speed selected.\n");
    printf("Supported speeds: GZ_CLK_5MHz (0) and GZ_CLK_125MHz (1).");
    exit(-1);
  }
  if (speed == 0) {
    speed_id = 1;
  }
  if (divisor < 2) {
    printf("gz_clock_ena: Minimum divisor value is 2.");
    exit(-1);
  }
  if (divisor > 0xfff) {
    printf("gz_clock_ena: Maximum divisor value is %d.", 0xfff);
    exit(-1);
  }
  if (bcm2835_init() !=1) {
    printf("gz_clock_ena: Failed to initialize I/O\n");
    exit(-1);
  }
  usleep(5);
  bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_ALT0);
  *(bcm2835_clk + 0x1C) = 0x5A000000 | speed_id;    // GPCLK0 off
  while (*(bcm2835_clk + 0x1C) & GZ_CLK_BUSY) {}    // Wait for BUSY low
  *(bcm2835_clk + 0x1D) = 0x5A002000 | (divisor << 12); // set DIVI
  *(bcm2835_clk + 0x1C) = 0x5A000010 | speed_id;    // GPCLK0 on
  return 0;
  
}

int gz_clock_dis() {
  if (bcm2835_init() !=1) {
    printf("gz_clock_dis: Failed to initialize I/O\n");
    exit(-1);
  }
  bcm2835_gpio_fsel(RPI_GPIO_P1_07, BCM2835_GPIO_FSEL_INPT);
  return 0;
}

.END

No comments:

Post a Comment