Example #1
0
int main(int argc, char **argv)
{
  #define MAX_BITS (49) // 49-bit word.
  #define MAX_DATA (1*1024) // 1 KB data buffer of 49-bit data words - ~70 seconds @ 1 kHz.
  char data[MAX_DATA*MAX_BITS], word[MAX_BITS];
  //int (*data_ptr)[MAX_DATA][MAX_BITS] = &data, (*bit_ptr)[MAX_BITS] = data;
  int i = 0, j = 0, flag = 0, bit_cnt = 0, data_cnt = 0, zones, cmd;
  int data0,data1,data2,data3,data4,data5,data6;
  char year3[2],year4[2],month[2],day[2],hour[2],minute[2];
  FILE *out_file;
  struct sched_param param;
  struct timespec t, tmark;
  char msg[100] = "", oldMsg[100] = "";
  struct fifo dataFifo;

  /* Declare ourself as a real time task */
  param.sched_priority = MY_PRIORITY;
  if(sched_setscheduler(0, SCHED_FIFO, &param) == -1) {
    perror("sched_setscheduler failed");
    exit(-1);
  }

  /* Lock memory */
  if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
    perror("mlockall failed");
    exit(-2);
  }

  /* Pre-fault our stack */
  stack_prefault();

  for(i = 0; i < MAX_DATA*MAX_BITS; i++)
    data[i] = '0';

  for(i = 0; i < MAX_BITS; i++)
    word[i] = '0';

  // Set up gpio pointer for direct register access
  setup_io();

  // Set up FIFO
  fifo_init(&dataFifo, data, MAX_DATA*MAX_BITS);

  // Set pin direction
  INP_GPIO(PI_DATA_OUT); // must use INP_GPIO before we can use OUT_GPIO
  OUT_GPIO(PI_DATA_OUT);
  INP_GPIO(PI_DATA_IN);
  INP_GPIO(PI_CLOCK_IN);

  // Set PI_DATA_OUT pin low.
  GPIO_CLR = 1<<PI_DATA_OUT;

  clock_gettime(CLOCK_MONOTONIC, &t);
  tmark = t;
  while (1) {
//printf("pi_data:%i,data[%i][%i]:%i\n",GET_GPIO(PI_DATA_IN),data_cnt,bit_cnt,data[data_cnt][bit_cnt]);
    t.tv_nsec += INTERVAL;
    tnorm(&t);
    clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL);
    if (GET_GPIO(PI_CLOCK_IN) == PI_CLOCK_HI) flag = 1;
    else if ((GET_GPIO(PI_CLOCK_IN) == PI_CLOCK_LO) && (flag == 1)) {
      if (ts_diff(&t, &tmark) > 1100000) { // new word
        bit_cnt = 0; // start new word
        fifo_write(&dataFifo, word, MAX_BITS); // write current word to FIFO
/*printf("%04i: wrote ", data_cnt);
for(i=0; i<MAX_BITS; i++) printf("%c", word[i]);
printf(" to dataFifo\n");*/
        data_cnt++;
      }
      tmark = t;
      flag = 0;
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      /*t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);
      t.tv_nsec += INTERVAL;
      tnorm(&t);*/
      clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &t, NULL); // wait 50 uS for valid data
      word[bit_cnt++] = (GET_GPIO(PI_DATA_IN) == PI_DATA_HI) ? '0' : '1';
    }
    if (data_cnt == MAX_DATA)
      break;
  }

  // decode and write to file
  if ( (out_file = fopen ("data", "w")) == NULL )
    printf ("*** data could not be opened. \n" );
  else
    for ( i = 0; i < MAX_DATA; i++ ) {
      strcpy(msg, "");
      fifo_read(&dataFifo, word, MAX_BITS);
/*printf("%04i: read  ", i);
for(j=0; j<MAX_BITS; j++) printf("%c", word[j]);
printf(" from dataFifo\n");*/
      cmd = getBinaryData(word,0,8);
      if (cmd == 0x05) {
        strcpy(msg, "LED Status: ");
        if (getBinaryData(word,12,1)) strcat(msg, "Error ");
        if (getBinaryData(word,13,1)) strcat(msg, "Bypass ");
        if (getBinaryData(word,14,1)) strcat(msg, "Memory ");
        if (getBinaryData(word,15,1)) strcat(msg, "Armed ");
        if (getBinaryData(word,16,1)) strcat(msg, "Ready ");
      }
      else if (cmd == 0xa5) {
        sprintf(year3, "%d", getBinaryData(word,9,4));
        sprintf(year4, "%d", getBinaryData(word,13,4));
        sprintf(month, "%d", getBinaryData(word,19,4));
        sprintf(day, "%d", getBinaryData(word,23,5));
        sprintf(hour, "%d", getBinaryData(word,28,5));
        sprintf(minute, "%d", getBinaryData(word,33,6));
        strcpy(msg, "Date: 20");
        strcat(msg, year3);
        strcat(msg, year4);
        strcat(msg, "-");
        strcat(msg, month);
        strcat(msg, "-");
        strcat(msg, day);
        strcat(msg, " ");
        strcat(msg, hour);
        strcat(msg, ":");
        strcat(msg, minute);
      }
      else if (cmd == 0x27) {
        strcpy(msg, "Zone1: ");
        zones = getBinaryData(word,41,8);
        if (zones & 1) strcat(msg, "1 ");
        if (zones & 2) strcat(msg, "2 ");
        if (zones & 4) strcat(msg, "3 ");
        if (zones & 8) strcat(msg, "4 ");
        if (zones & 16) strcat(msg, "5 ");
        if (zones & 32) strcat(msg, "6 ");
        if (zones & 64) strcat(msg, "7 ");
        if (zones & 128) strcat(msg, "8 ");
      }
      else if (cmd == 0x2d) {
        strcpy(msg, "Zone2: ");
        zones = getBinaryData(word,41,8);
        if (zones & 1) strcat(msg, "9 ");
        if (zones & 2) strcat(msg, "10 ");
        if (zones & 4) strcat(msg, "11 ");
        if (zones & 8) strcat(msg, "12 ");
        if (zones & 16) strcat(msg, "13 ");
        if (zones & 32) strcat(msg, "14 ");
        if (zones & 64) strcat(msg, "15 ");
        if (zones & 128) strcat(msg, "16 ");
      }
      else if (cmd == 0x34) {
        strcpy(msg, "Zone3: ");
        zones = getBinaryData(word,41,8);
        if (zones & 1) strcat(msg, "9 ");
        if (zones & 2) strcat(msg, "10 ");
        if (zones & 4) strcat(msg, "11 ");
        if (zones & 8) strcat(msg, "12 ");
        if (zones & 16) strcat(msg, "13 ");
        if (zones & 32) strcat(msg, "14 ");
        if (zones & 64) strcat(msg, "15 ");
        if (zones & 128) strcat(msg, "16 ");
      }
      else if (cmd == 0x3e) {
        strcpy(msg, "Zone4: ");
        zones = getBinaryData(word,41,8);
        if (zones & 1) strcat(msg, "9 ");
        if (zones & 2) strcat(msg, "10 ");
        if (zones & 4) strcat(msg, "11 ");
        if (zones & 8) strcat(msg, "12 ");
        if (zones & 16) strcat(msg, "13 ");
        if (zones & 32) strcat(msg, "14 ");
        if (zones & 64) strcat(msg, "15 ");
        if (zones & 128) strcat(msg, "16 ");
      }
      else
        strcpy(msg, "Unknown command.");
      data0 = getBinaryData(word,0,8);  data1 = getBinaryData(word,8,8);
      data2 = getBinaryData(word,16,8); data3 = getBinaryData(word,24,8);
      data4 = getBinaryData(word,32,8); data5 = getBinaryData(word,40,8);
      data6 = getBinaryData(word,48,2);
      if (strcmp(msg, oldMsg) != 0) {
        fprintf (out_file, "data[%i],cmd:0x%02x,%s\n", i, cmd, msg);
        fprintf (out_file, "***data-all: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n",data0,data1,data2,data3,data4,data5,data6);
        strcpy(oldMsg, msg);
      }
    }
  fclose (out_file);

  /* Unlock memory */
  if(munlockall() == -1) {
    perror("munlockall failed");
    exit(-2);
  }

  return 0;
} // main
Example #2
0
void setup_gpio()
{
  INP_GPIO(7);  OUT_GPIO(7);
  INP_GPIO(8);  OUT_GPIO(8);
  INP_GPIO(9);  OUT_GPIO(9);
  INP_GPIO(10);  OUT_GPIO(10);
  INP_GPIO(11);  OUT_GPIO(11);
  // 14 and 15 are already set to UART mode
  // by Linux. Best if we don't touch them
  INP_GPIO(17);  OUT_GPIO(17);
  INP_GPIO(18);  OUT_GPIO(18);
  INP_GPIO(27);  OUT_GPIO(27);
  INP_GPIO(22);  OUT_GPIO(22);
  INP_GPIO(23);  OUT_GPIO(23);
  INP_GPIO(24);  OUT_GPIO(24);
  INP_GPIO(25);  OUT_GPIO(25);
} // setup_gpio
Example #3
0
void enable_gpio_read(int port) {
    INP_GPIO(port);
}
Example #4
0
int clock_dis() {
  INP_GPIO(4);
  return 0;
}
Example #5
0
int main(int argc, char** argv)
{
	int g, rep;

	setup_io();
	signal(SIGINT, intHandler);

	debug("setting up io pins");
	INP_GPIO(17);
	OUT_GPIO(17);

	//init_pwm();

	int glow = 0;
	int increment = 4;
	
	while (0) {
		glow += increment;
		increment++;
		if (glow > 1024 || glow < 0) {
			glow -= increment;
			increment = -increment;
		}
		//if (GPIO_LEVEL(8)) {
			GPIO_SET = 1 << 17;
			PWM_DATA = glow;
		//} else {
		//	GPIO_CLR = 1 << 7;
		//}

		usleep(30000);
	}

	debug("initializing lcd");
	init_lcd();
	debug("writing to lcd");
	lcd_print("Raspberry Pi :)", 1);

	time_t t = time(NULL);
	struct tm cur_time = *localtime(&t);
	char time_str[19];

	while (1) {
		t = time(NULL);
		cur_time = *localtime(&t);
		strftime(time_str, 18, "%a %I:%M:%S %p", &cur_time);
		lcd_print(time_str, 2);
		sleep(1);
	}

	return 0;

	for (rep=0; rep<10; rep++) {
		for (g=7; g<=11; g++) {
			GPIO_SET = 1<<g;
			sleep(1);
		}
		//for (g=7; g<=11; g++) {
		//	GPIO_CLR = 1<<g;
		//	sleep(1);
		//}
	}

	return 0;
}
Example #6
0
/********************************************************************
 * open_weatherstation, Windows version
 *
 * Input:   devicename (COM1, COM2 etc)
 * 
 * Returns: Handle to the weatherstation (type WEATHERSTATION)
 *
 ********************************************************************/
WEATHERSTATION open_weatherstation (char *device) {
  WEATHERSTATION ws;
  struct termios adtio;
  unsigned char buffer[BUFFER_SIZE];
  long i;
  print_log(1,"open_weatherstation");

  //calibrate nanodelay function
  microdelay_init(1);

  // Set up gpi pointer for direct register access
  setup_io();

  // Switch GPIO 7..11 to output mode

 /************************************************************************\
  * You are about to change the GPIO settings of your computer.          *
  * Mess this up and it will stop working!                               *
  * It might be a good idea to 'sync' before running this program        *
  * so at least you still have your code changes written to the SD-card! *
 \************************************************************************/

  // Set GPIO pins 7-11 to output
  for (g=7; g<=11; g++)
  {
    INP_GPIO(g); // must use INP_GPIO before we can use OUT_GPIO
    OUT_GPIO(g);
  }

	

	
	
  //Setup serial port
  if ((ws = open(device, O_RDWR | O_NOCTTY)) < 0)
  {
    printf("\nUnable to open serial device %s\n", device);
    exit(EXIT_FAILURE);
  }

  if ( flock(ws, LOCK_EX) < 0 ) {
    perror("\nSerial device is locked by other program\n");
    exit(EXIT_FAILURE);
  }
  //We want full control of what is set and simply reset the entire adtio struct
  memset(&adtio, 0, sizeof(adtio));
  // Serial control options
  adtio.c_cflag &= ~PARENB;      // No parity
  adtio.c_cflag &= ~CSTOPB;      // One stop bit
  adtio.c_cflag &= ~CSIZE;       // Character size mask
  adtio.c_cflag |= CS8;          // Character size 8 bits
  adtio.c_cflag |= CREAD;        // Enable Receiver
  //adtio.c_cflag &= ~CREAD;        // Disable Receiver
  adtio.c_cflag &= ~HUPCL;       // No "hangup"
  adtio.c_cflag &= ~CRTSCTS;     // No flowcontrol
  adtio.c_cflag |= CLOCAL;       // Ignore modem control lines

  // Baudrate, for newer systems
  cfsetispeed(&adtio, BAUDRATE);
  cfsetospeed(&adtio, BAUDRATE);	

  // Serial local options: adtio.c_lflag
  // Raw input = clear ICANON, ECHO, ECHOE, and ISIG
  // Disable misc other local features = clear FLUSHO, NOFLSH, TOSTOP, PENDIN, and IEXTEN
  // So we actually clear all flags in adtio.c_lflag
  adtio.c_lflag = 0;

  // Serial input options: adtio.c_iflag
  // Disable parity check = clear INPCK, PARMRK, and ISTRIP 
  // Disable software flow control = clear IXON, IXOFF, and IXANY
  // Disable any translation of CR and LF = clear INLCR, IGNCR, and ICRNL	
  // Ignore break condition on input = set IGNBRK
  // Ignore parity errors just in case = set IGNPAR;
  // So we can clear all flags except IGNBRK and IGNPAR
  adtio.c_iflag = IGNBRK|IGNPAR;

  // Serial output options
  // Raw output should disable all other output options
  adtio.c_oflag &= ~OPOST;

  adtio.c_cc[VTIME] = 10;		// timer 1s
  adtio.c_cc[VMIN] = 0;		// blocking read until 1 char

  if (tcsetattr(ws, TCSANOW, &adtio) < 0)
  {
	  printf("Unable to initialize serial device");
	  exit(0);
  }
  tcflush(ws, TCIOFLUSH);
  
  for (i = 0; i < 448; i++) {
    buffer[i] = 'U';
  }
  write(ws, buffer, 448);

  set_DTR(ws,0);
  set_RTS(ws,0);
  i = 0;
  do {
    sleep_short(10);
    i++;
  } while (i < INIT_WAIT && !get_DSR(ws));

  if (i == INIT_WAIT)
  {
    print_log(2,"Connection timeout 1");
    printf ("Connection timeout\n");
    close_weatherstation(ws);
    exit(0);
  }
  i = 0;
  do {
    sleep_short(10);
    i++;
  } while (i < INIT_WAIT && get_DSR(ws));

  if (i != INIT_WAIT) {
    set_RTS(ws,1);
    set_DTR(ws,1);
  } else {
    print_log(2,"Connection timeout 2");
    printf ("Connection timeout\n");
    close_weatherstation(ws);
    exit(0);
  }
  write(ws, buffer, 448);
  return ws;
}