int main(void)
{
    timeCount = 0;

    hardwareInit();
    uart0_init(BAUD_SETTING);
/* Initialise timer to divide by 1025, giving 32ms time tick */
    timer0Init(0,5);

    rfm12_init();
    wdtInit();
    sei();

    indx = 0;

    for(;;)
    {
        wdt_reset();

        uint8_t sendMessage = false;
        uint16_t character = uart0_getc();
/* Wait for a serial incoming message to be built. */
        if (character != UART_NO_DATA)
        {
            inBuf[indx++] = (uint8_t)(character & 0xFF);
/* Signal to transmit if a CR was received, or string too long. */ 
            sendMessage = ((indx > MAX_MESSAGE) || (character == 0x0D));
        }

/* Send a transmission if message is ready to send, or something was
received and time waited is too long. */ 
        if (sendMessage || (timeCount++ > TIMEOUT))
        {
/* Wait for the transmit buffer to be freed and message loaded. */
            if ((indx > 0) && (rfm12_tx(indx, 0, inBuf) != RFM12_TX_OCCUPIED))
                indx = 0;
            timeCount = 0;
        }
        rfm12_tick();

/* If an RF incoming message has been received, take it from the buffer one
character at a time and transmit via the serial port. */
        if (rfm12_rx_status() == STATUS_COMPLETE)
        {
            uint8_t *bufferContents = rfm12_rx_buffer();
            uint8_t messageLength = rfm12_rx_len();
            uint8_t i;
            for (i=0;i<messageLength;i++)
            {
	            uart0_putc(bufferContents[i]);
            }
/* Clear the "in use" status of the receive buffer to be available for
rfm12lib. */
            rfm12_rx_clear();
        }
    }
}
Ejemplo n.º 2
0
static UINT32
sysInit(
	void
)
{
	/* board dependent modules */
	rtcInit();
	wdtInit();

	inputInit();

	audioInit();
	fmInit();
	lineBufferInit();
	graphicInit();
	scalarInit();
	if (strcmp(SYSCONFIG_PRODUCT, "gplus.microwindowsUI__gplus.evm32900b") != 0) {
		aesInit();
	}
	sdmaInit();
#ifdef SYSCONFIG_ARCH_SPMP8050
	rotatorInit();
#endif
#ifdef SYSCONFIG_ARCH_SPMP8050
	cevaInit();
#endif
	if( gp_ver.major == MACH_GPL32900 )
	{
		cevaInit();
	}
	
	storageInit();

	if (strcmp(SYSCONFIG_MAINSTORAGE, "gp_usb_disk")) {
		usbInit();
	}

	usbWifiInit();
    touchpanelInit();
#ifdef SYSCONFIG_SDIO
	system("modprobe cfg80211");
	system("modprobe mac80211");
	system("modprobe sunrpc");
#endif
	sensorInit();
	ppuInit();
	//tvInit();

	powerInit();
	batteryInit();
	gsensorInit();
	asensorInit();
#ifdef SYSCONFIG_GP_FAST_BOOT
	gpFastBootInit();
#endif /* SYSCONFIG_GP_FAST_BOOT */
	if( gp_ver.major == MACH_GPL32900B )
	{
		On2Init();
		LBPInit();
	}

	return SP_OK;
}
int main(void) {

  uint8_t numSensors = 0, i;
  // unsigned long nextflash = 0;

  // Configure all pins as inputs with pullups initially
  DDRA = 0x00;
  PORTA = 0xff;
  DDRB = 0x00;
  PORTB = 0xff;

  // Serial output line
  DDRB |= _BV(PINB0);
  PORTB |= _BV(PINB0);

  // LED
  // DDRA |= _BV(PINA0);

  // Radio power is PA1
  PORTA &= ~_BV(PINA1);
  DDRA |= _BV(PINA1);

  // Onewire power is PA2
  PORTA &= ~_BV(PINA2);
  DDRA |= _BV(PINA2);

  myPutStr("Hello world\r\n");

  // Various power-saving things

  // Disable BOD while sleeping. I hope.
  MCUCR |= (_BV(BODS) | _BV(BODSE));
  MCUCR &= ~_BV(BODSE);
  MCUCR |= (_BV(BODS));
 
  // Disable the ADC
  ADCSRA &= ~_BV(ADEN);

  // Disable the Analog Comparator
  ACSR |= _BV(ACD);

  // Disable clocking of timer1 and ADC
  PRR |= (_BV(PRTIM1)|_BV(PRADC));

  timerInit();
  wdtInit();
  initInterrupts();


  // Power up the Onewire bus
  PORTA |= _BV(PINA2);
  while(numSensors == 0) {
    myPutStr("Scanning for sensors\r\n");
    numSensors = search_sensors();
    myPutStr("Found ");
    myPutUint8(numSensors);
    myPutStr(" sensors\r\n");

    for (i=0;i<numSensors;i++) {
      uint8_t j;
      myPutStr("Sensor ");
      myPutUint8(i);
      myPutStr(" address ");
      for (j=0;j<OW_ROMCODE_SIZE;j++) {
	myPutUint8(gSensorIDs[i][j]);
      }
      if (gSensorIDs[i][0] == DS18S20_FAMILY_CODE ) {
	myPutStr(" DS18S20/DS1820");
      } else if ( gSensorIDs[i][0] == DS1822_FAMILY_CODE ) {
	myPutStr(" DS1822");
      } else {
	myPutStr(" DS18B20");
      }
      if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == DS18X20_POWER_PARASITE ) {
	myPutStr(" parasite\r\n");
      } else {
	myPutStr(" external\r\n");
      }

      // Enable 12 bit mode (won't do anything on DS18S20)
      DS18X20_write_scratchpad(&gSensorIDs[i][0], 0, 0, DS18B20_12_BIT);
      DS18X20_scratchpad_to_eeprom(DS18X20_get_power_status( &gSensorIDs[i][0] ),&gSensorIDs[i][0]);
	
    }
      

  }
  while(1) {
    unsigned long wakepoint;
    myRadioBuf_t radiobuf;
    //char debugbuf[10];

    // if ((signed long)now - (signed long)nextflash >= 0) {
    //   debugLedToggle(0);
    //   nextflash = now + 1000;
    // }

    // Power up the Onewire bus
    PORTA |= _BV(PINA2);
    // Let is stabilize for a few ms
    wakepoint = getMillis() + 15;
    numSensors = search_sensors();
    while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) {
      set_sleep_mode(SLEEP_MODE_IDLE);
      sleep_mode();
    }
    if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
      wakepoint = getMillis() + DS18B20_TCONV_12BIT;
      while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) {
	set_sleep_mode(SLEEP_MODE_IDLE);
	sleep_mode();
      }
      // Power the radio up
      PORTA |= _BV(PINA1);
      // Wakepoint set to now +100ms to allow radio to wake
      wakepoint = getMillis() + 100;

      for ( i = 0; i < numSensors; i++ ) {
        radiobuf.tenthousandths = -9999999L;
	if (DS18X20_read_maxres( &gSensorIDs[i][0], &(radiobuf.tenthousandths) ) != DS18X20_OK) {
	  radiobuf.tenthousandths = -9999999L;
	}

	//myPutStr("Sensor ");
	uint8_t j;
	for (j=0;j<OW_ROMCODE_SIZE;j++) {
            radiobuf.sensid[j] = gSensorIDs[i][j];
	    //myPutUint8(gSensorIDs[i][j]);
        }
	//myPutStr(" = ");
	//sprintf(debugbuf, "%d.%d\r\n", (int)(radiobuf.tenthousandths/10000), (int)(radiobuf.tenthousandths % 10000));
	//myPutStr(debugbuf);
        if (0 == i) {
	  // First time around, radio not initialized
	  while (! ((signed long)getMillis() - (signed long)wakepoint >= 0)) {
            set_sleep_mode(SLEEP_MODE_IDLE);
            sleep_mode();
          }
	  radioInit();
	  // No auto ack
	  radioSetAutoAck(0);
	  radioOpenWritingPipe(pipe);
        }
	radiobuf.tstamp = getMillis();
	//myPutStr("about to radioWrite, i=");
	//myPutUint8(i);
	//myPutStr("...");
	radioWrite(&radiobuf,sizeof(radiobuf));
	//myPutStr("Done\r\n");
      }
      // Power the radio down
      PORTA &= ~_BV(PINA1);
      // Plus the CE and CSN pins
      PORTA &= ~_BV(PINA7);
      PORTB &= ~_BV(PINB2);
      // And the onewire bus
      PORTA &= ~_BV(PINA2);
    } else {
      myPutStr("Error measuring sensors\n");
    }
    //tmp = getMillis();
    //radioWrite(&tmp , sizeof(unsigned long) );
    // Sleep hard until WDT fires
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);
    sleep_mode();
  }
}