Пример #1
0
void Task1()
{
nrk_time_t t;
uint16_t cnt;
cnt=0;
nrk_kprintf( PSTR("Nano-RK Version ") );
printf( "%d\r\n",NRK_VERSION );


setup_uart1(UART_BAUDRATE_19K2);
DDRE=0x2;
printf( "My node's address is %u\r\n",NODE_ADDR );
  
printf( "Task1 PID=%u\r\n",nrk_get_pid());
t.secs=5;
t.nano_secs=0;

// setup a software watch dog timer
nrk_sw_wdt_init(0, &t, NULL);
nrk_sw_wdt_start(0);


	putc1('~'); putc1('A'); putc1(' '); putc1('8');
	nrk_wait_until_next_period();
	putc1('~'); putc1('A'); putc1(' '); putc1('8');
  while(1) {
	// Update watchdog timer
	nrk_sw_wdt_update(0);
	nrk_led_toggle(ORANGE_LED);
	nrk_gpio_toggle(NRK_DEBUG_0);
	printf( "Task1 cnt=%u\r\n",cnt );
	nrk_wait_until_next_period();
        // Uncomment this line to cause a stack overflow
	// if(cnt>20) kill_stack(10);

	// At time 50, the OS will halt and print statistics
	// This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
	// if(cnt==50)  {
	//	nrk_stats_display_all();
	//	nrk_halt();
	//	}


	cnt++;
	}
}
Пример #2
0
void Task1()
{
  nrk_time_t t;
  uint16_t cnt;
  uint8_t val;
  cnt=0;
  nrk_kprintf( PSTR("Nano-RK Version ") );
  printf("b");
  printf( "%d\r\n",NRK_VERSION );
  
  printf( "My node's address is %u\r\n",NODE_ADDR );
  
  printf( "Task1 PID=%u\r\n",nrk_get_pid());
  t.secs=30;
  t.nano_secs=0;
  
  // setup a software watch dog timer
 nrk_sw_wdt_init(0, &t, NULL);
 nrk_sw_wdt_start(0);
 
 nrk_gpio_direction(BUTTON, NRK_PIN_INPUT);
 
 while(1) {
   // Update watchdog timer
   nrk_sw_wdt_update(0);
   nrk_led_toggle(ORANGE_LED);
   val=nrk_gpio_get(BUTTON);
   
   // Button logic is inverter 0 means pressed, 1 not pressed
   printf( "Task1 cnt=%u button state=%u\r\n",cnt,val );
   nrk_wait_until_next_period();
   // Uncomment this line to cause a stack overflow
   // if(cnt>20) kill_stack(10);
   
   // At time 50, the OS will halt and print statistics
   // This requires the NRK_STATS_TRACKER #define in nrk_cfg.h
   if(cnt==50)  {
     nrk_stats_display_all();
     nrk_halt();
   }
   
   
   cnt++;
 }
}
Пример #3
0
void rx_task ()
{
    nrk_time_t t;
    uint16_t cnt;
    int8_t v;
    uint8_t len, i;
    uint8_t chan;


    cnt = 0;
    nrk_kprintf (PSTR ("Nano-RK Version "));
    printf ("%d\r\n", NRK_VERSION);


    nrk_kprintf( PSTR( "RX Task PID=" ));
    printf ("%u\r\n", nrk_get_pid ());
    t.secs = 5;
    t.nano_secs = 0;

    while (cal_done==0)
        nrk_wait_until_next_period ();


    chan = RADIO_CHANNEL;
    if (SET_MAC == 0x00) {

        v = read_eeprom_mac_address (&mac_address);
        if (v == NRK_OK) {
            v = read_eeprom_channel (&chan);
            v = read_eeprom_aes_key(aes_key);
        }
        else {
            while (1) {
                nrk_kprintf (PSTR
                             ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
                nrk_wait_until_next_period ();
            }
        }
    }
    else
        mac_address = SET_MAC;

    printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
    printf ("chan = %d\r\n", chan);
    len=0;
    for(i=0; i<16; i++ ) {
        len+=aes_key[i];
    }
    printf ("AES checksum = %d\r\n", len);



    tdma_init (TDMA_CLIENT, chan, (mac_address));

    tdma_aes_setkey(aes_key);
    tdma_aes_enable();

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    v = tdma_tx_slot_add (mac_address&0xff);

    if (v != NRK_OK)
        nrk_kprintf (PSTR ("Could not add slot!\r\n"));

    // setup a software watch dog timer
    t.secs=30;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);
    while (1) {
        // Update watchdog timer
        nrk_sw_wdt_update(0);
        v = tdma_recv (&rx_tdma_fd, &rx_buf, &len, TDMA_BLOCKING);
        if (v == NRK_OK) {
            // printf ("src: %u\r\nrssi: %d\r\n", rx_tdma_fd.src, rx_tdma_fd.rssi);
            // printf ("slot: %u\r\n", rx_tdma_fd.slot);
            // printf ("cycle len: %u\r\n", rx_tdma_fd.cycle_size);
            v=buf_to_pkt(&rx_buf, &rx_pkt);
            if(v==NRK_OK)
            {
                if(((rx_pkt.dst_mac&0xff) == (mac_address&0xff)) || ((rx_pkt.dst_mac&0xff)==0xff))
                {
                    if(rx_pkt.type==PING)
                    {
                        send_ack=1;
                        nrk_led_clr(0);
                        nrk_led_clr(1);
                        if(rx_pkt.payload[0]==PING_1)
                        {
                            nrk_led_set(0);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(0);
                        }
                        if(rx_pkt.payload[0]==PING_2)
                        {
                            nrk_led_set(1);
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_wait_until_next_period();
                            nrk_led_clr(1);
                        }
                        if(rx_pkt.payload[0]==PING_PERSIST)
                        {
                            nrk_led_set(0);
                        }


                    }

                    if(rx_pkt.type==APP)
                    {
                        // payload 1: Key
                        if(rx_pkt.payload[1]==2)
                        {
                            send_ack=1;
                            // payload 2: Outlet Number
                            // payload 3: On/Off
                            if(rx_pkt.payload[3]==0) {
                                power_socket_disable(rx_pkt.payload[2]);
                                plug_led_green_clr();
                                //printf( "Disable %d\r\n", rx_pkt.payload[2] );
                            }
                            if(rx_pkt.payload[3]==1) {
                                power_socket_enable(rx_pkt.payload[2]);
                                //printf( "Enable %d\r\n", rx_pkt.payload[2] );
                                plug_led_green_set();
                            }
                        }

                        // payload 1: Key
                        if(rx_pkt.payload[1]==3)
                        {
                            send_ack=1;
                            true_power_thresh=((uint32_t)rx_pkt.payload[3])<<16 | ((uint32_t)rx_pkt.payload[4])<<8 | (uint32_t)rx_pkt.payload[5];
                            set_power_thresh(true_power_thresh);
                        }




                    }

                }

            }
            /*      printf ("len: %u\r\npayload: ", len);
            for (i = 0; i < len; i++)
              printf ("%d ", rx_buf[i]);
            printf ("\r\n");

            if(rx_buf[0]==(mac_address&0xff))
            {
            if(rx_buf[2]==0) {
            	power_socket_disable(rx_buf[1]);
            	printf( "Disable %d\r\n", rx_buf[1] );
            }
            if(rx_buf[2]==1) {
            	power_socket_enable(rx_buf[1]);
            	printf( "Enable %d\r\n", rx_buf[1] );
            }
                 }
                 */

        }

        tdma_rx_pkt_release();
        //  nrk_wait_until_next_period();
    }

}
Пример #4
0
void tx_task ()
{
    uint8_t j, i, val, cnt;
    int8_t len;
    int8_t v,fd;
    uint8_t buf[2];
    nrk_sig_mask_t ret;
    nrk_time_t t;



    // Set Port D.0 as input
    // On the FireFly nodes we use this for motion or syntonistor input
    // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway
//  DDRD &= ~(0x1);



    printf ("tx_task PID=%d\r\n", nrk_get_pid ());

    // setup a software watch dog timer
    t.secs=10;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // set port G as input for gpio
    DDRG=0;


    while (1) {

        nrk_led_clr(RED_LED);


        tx_pkt.payload[0] = 1;  // ELEMENTS
        tx_pkt.payload[1] = 3;  // Key

        fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ);
        if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));


        val=nrk_set_status(fd,SENSOR_SELECT,MOTION);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[28]=buf[1];
        tx_pkt.payload[29]=buf[0];



        val=nrk_set_status(fd,SENSOR_SELECT,BAT);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[2]=buf[1];
        tx_pkt.payload[3]=buf[0];
        //printf( "Task bat=%d",buf);

//	tx_pkt.payload[2]=0;
//	tx_pkt.payload[3]=0;

        val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[4]=buf[1];
        tx_pkt.payload[5]=buf[0];
        //printf( " light=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[6]=buf[1];
        tx_pkt.payload[7]=buf[0];
        //printf( " temp=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[8]=buf[1];
        tx_pkt.payload[9]=buf[0];
        //printf( " acc_x=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[10]=buf[1];
        tx_pkt.payload[11]=buf[0];
        //printf( " acc_y=%d",buf);
        val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[12]=buf[1];
        tx_pkt.payload[13]=buf[0];
        //printf( " acc_z=%d",buf);


        val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[16]=buf[3];
        tx_pkt.payload[17]=buf[2];
        tx_pkt.payload[18]=buf[1];
        tx_pkt.payload[19]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,TEMP2);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[20]=buf[3];
        tx_pkt.payload[21]=buf[2];
        tx_pkt.payload[22]=buf[1];
        tx_pkt.payload[23]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,PRESS);
        val=nrk_read(fd,&buf,4);
        tx_pkt.payload[24]=buf[3];
        tx_pkt.payload[25]=buf[2];
        tx_pkt.payload[26]=buf[1];
        tx_pkt.payload[27]=buf[0];


        val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
        val=nrk_read(fd,&buf,2);
        tx_pkt.payload[14]=buf[1];
        tx_pkt.payload[15]=buf[0];


        // GPIO data
//	gpio_pin = !!(PINE & 0x4);
        gpio_pin = PING & 0x1;
        tx_pkt.payload[30]=gpio_pin;
        //printf( " audio=%d\r\n",buf);

        nrk_close(fd);

        tx_pkt.payload_len=31;
        tx_pkt.src_mac=mac_address;
        tx_pkt.dst_mac=0;
        tx_pkt.type=APP;

        len=pkt_to_buf(&tx_pkt,&tx_buf );

        if(len>0)
        {
            v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
            if (v == NRK_OK) {
            }
        } else {
            nrk_kprintf(PSTR("Pkt tx error\r\n"));
            nrk_wait_until_next_period();
        }


        nrk_sw_wdt_update(0);
    }



}
Пример #5
0
void tx_task ()
{
  uint8_t j, i, val, cnt;
  int8_t len;
  int8_t v,fd;
  uint8_t buf[2];
  nrk_sig_mask_t ret;
  nrk_time_t t;
  int8_t* success;
  int16_t acbuf[3];
  int gyrobuf[3];
  int magbuf[3];

	/* Setup application timer with:
	 * Prescaler = 5 
	 * Compare Match = 2500
	 * Sys Clock = 1.0 MHz according to Mega128RFA1 manual pg149
	 * Prescaler 5 means divide sys clock by 1024
	 * 1000000 / 1024 = 976.5625 Hz clock
	 * 1 / 976.5625 = 1.024 ms per tick
	 * 1.024 ms * 1000 = ~1024 ms / per interrupt callback
	 */
	
	val=nrk_timer_int_configure(NRK_APP_TIMER_0, 5, 1000, &my_timer_callback );
	if(val==NRK_OK) nrk_kprintf( PSTR("Callback timer setup\r\n"));
	else nrk_kprintf( PSTR("Error setting up timer callback\r\n"));
	
	// Zero the timer...
	nrk_timer_int_reset(NRK_APP_TIMER_0);
	// Start the timer...
	nrk_timer_int_start(NRK_APP_TIMER_0);

  // Set Port D.0 as input
  // On the FireFly nodes we use this for motion or syntonistor input
  // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway
  //  DDRD &= ~(0x1);
  	
   
   //nrk_kprintf( PSTR("Booting Up ADXL345. . .\r\n") );
  /**** Start Accelerometer and check connections 
   * Start up accelerometer
   * Ensure ADXL is connected. 
   * Enable continous measurement
   * INT_SCALER =  10000 
   * Set accelerometer range to maximum og 2G
   * Set sample rate to 1.6Khz
   ****/	
   set_up_ADXL345();
	
  /**** Start Gyro and check connections 
   * Start up gyro
   * Zero calibrate gyro.
   * INT_SCALER_GYRO =  100  
   * Leave gyro still for about 2 seconds to zero-calibrate
   * sets gyro sample rate to 8Khz with a 256Hz BW(bandwidth) LP (low pass) filter
   ****/
   set_up_ITG3200();	
	
  /**** Start magnetometer and check connections 
   * set scale to +/- 1.3 gauss
   * set measurement mode to continous	
   * INT_SCALER_MAG = 1000
   * sets sample rate to highest value of 75Hz
   * sets averaging value to 8 samples per reading givein at 75 Hz
   ****/
   success = set_up_HMC5883L();
   
   if (*success) nrk_kprintf( PSTR("HMC5883L(Magnetometer) boot success. Scale +/- 1.3 Ga. . .\r\n"));
   if ( *(success+1) ) nrk_kprintf( PSTR("HMC5883L measurement mode: continuous . .\r\n") );
	
	//printf( "My node's address is %d\r\n",NODE_ADDR );

  printf ("tx_task PID=%d\r\n", nrk_get_pid ());

   // setup a software watch dog timer
   t.secs=10;
   t.nano_secs=0;
   nrk_sw_wdt_init(0, &t, NULL);
   nrk_sw_wdt_start(0);

   while (!tdma_started ())
   nrk_wait_until_next_period ();

	

  while (1) {

	//nrk_led_toggle(GREEN_LED);


	tx_pkt.payload[0] = 1;  // ELEMENTS
	tx_pkt.payload[1] = 3;  // Key

   /* 	fd=nrk_open(FIREFLY_3_SENSOR_BASIC,READ);
        if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n"));
//	val=nrk_set_status(fd,SENSOR_SELECT,BAT);
//	val=nrk_read(fd,&buf,2);
//	tx_pkt.payload[2]=buf[1];
//	tx_pkt.payload[3]=buf[0];
	//printf( "Task bat=%d",buf);
	tx_pkt.payload[2]=0;
	tx_pkt.payload[3]=0;
	
	//val=nrk_set_status(fd,SENSOR_SELECT,LIGHT);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[4]=buf[1];
	tx_pkt.payload[5]=buf[0];
	//printf( " light=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,TEMP);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[6]=buf[1];
	tx_pkt.payload[7]=buf[0];
	//printf( " temp=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,ACC_X);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[8]=buf[1];
	tx_pkt.payload[9]=buf[0];
	//printf( " acc_x=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,ACC_Y);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[10]=buf[1];
	tx_pkt.payload[11]=buf[0];
	//printf( " acc_y=%d",buf);
	
	//val=nrk_set_status(fd,SENSOR_SELECT,ACC_Z);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[12]=buf[1];
	tx_pkt.payload[13]=buf[0];
	//printf( " acc_z=%d",buf);
	
	/*
	val=nrk_set_status(fd,SENSOR_SELECT,HUMIDITY);
	val=nrk_read(fd,&buf,4);
	tx_pkt.payload[16]=buf[3];
	tx_pkt.payload[17]=buf[2];
	tx_pkt.payload[18]=buf[1];
	tx_pkt.payload[19]=buf[0];


	val=nrk_set_status(fd,SENSOR_SELECT,TEMP2);
	val=nrk_read(fd,&buf,4);
	tx_pkt.payload[20]=buf[3];
	tx_pkt.payload[21]=buf[2];
	tx_pkt.payload[22]=buf[1];
	tx_pkt.payload[23]=buf[0];


	val=nrk_set_status(fd,SENSOR_SELECT,PRESS);
	val=nrk_read(fd,&buf,4);
	tx_pkt.payload[24]=buf[3];
	tx_pkt.payload[25]=buf[2];
	tx_pkt.payload[26]=buf[1];
	tx_pkt.payload[27]=buf[0];

	//val=nrk_set_status(fd,SENSOR_SELECT,MOTION);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[28]=buf[1];
	tx_pkt.payload[29]=buf[0];

	//val=nrk_set_status(fd,SENSOR_SELECT,AUDIO_P2P);
	//val=nrk_read(fd,&buf,2);
	tx_pkt.payload[14]=buf[1];
	tx_pkt.payload[15]=buf[0];


	// GPIO data
	//gpio_pin = !!(PINE & 0x4);
	//tx_pkt.payload[30]=gpio_pin;
	//printf( " audio=%d\r\n",buf);

    	nrk_close(fd);*/
	  tx_pkt.payload[30]=30;
	  tx_pkt.payload[29]=29;
	  tx_pkt.payload[28]=8;
	  tx_pkt.payload[27]=0;
	  tx_pkt.payload[26]=0;
	  tx_pkt.payload[25]=29;
	  tx_pkt.payload[24]=8;
	  tx_pkt.payload[23]=0;
	  tx_pkt.payload[22]=0;
	  tx_pkt.payload[21]=8;
	  tx_pkt.payload[20]=0;
	  tx_pkt.payload[19]=0;
	  tx_pkt.payload[18]=0;
	  tx_pkt.payload[17]=0;
	  tx_pkt.payload[16]=0;
      tx_pkt.payload[15]=60;
	  tx_pkt.payload[14]=0;
	  tx_pkt.payload[13]=0;
	  tx_pkt.payload[12]=0;
	  tx_pkt.payload[11]=0;
	  tx_pkt.payload[10]=0;
	  tx_pkt.payload[9]=0;
	  tx_pkt.payload[8]=0;
	  tx_pkt.payload[7]=155;
	  tx_pkt.payload[6]=45;
	  tx_pkt.payload[5]=8;
	  tx_pkt.payload[4]=5;
	  tx_pkt.payload[3]=3;
	  tx_pkt.payload[2]=2;

  	 tx_pkt.payload_len=31;
	 tx_pkt.src_mac=mac_address;
	 tx_pkt.dst_mac=0;
	 tx_pkt.type=APP;

    len=pkt_to_buf(&tx_pkt,&tx_buf );

 if(len>0){
    v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
    if (v == NRK_OK) {
 	 }
 }
 else { 
	nrk_kprintf(PSTR("Pkt tx error\r\n")); 
	nrk_wait_until_next_period(); 
 }
   

	nrk_sw_wdt_update(0);  
  }
}
Пример #6
0
int8_t bmac_init (uint8_t chan)
{
    bmac_running=0;
    tx_reserve=-1;
    cca_active=true;
    rx_failure_cnt=0;
	
#ifdef NRK_SW_WDT
#ifdef BMAC_SW_WDT_ID

    _bmac_check_period.secs=30;
    _bmac_check_period.nano_secs=0;
    nrk_sw_wdt_init(BMAC_SW_WDT_ID, &_bmac_check_period, NULL );
    nrk_sw_wdt_start(BMAC_SW_WDT_ID);
#endif
#endif



    _bmac_check_period.secs=0;
    _bmac_check_period.nano_secs=BMAC_DEFAULT_CHECK_RATE_MS*NANOS_PER_MS;
	
	// SIGNAL
/*  bmac_rx_pkt_signal=nrk_signal_create();
    if(bmac_rx_pkt_signal==NRK_ERROR)
    {
        printf("BMAC ERROR: creating rx signal failed\r\n");
       // nrk_kernel_error_add(NRK_SIGNAL_CREATE_ERROR,nrk_cur_task_TCB->task_ID); // commented out by madhur: error implementation has not been checked yet
        return NRK_ERROR;
    }
*/    bmac_tx_pkt_done_signal=nrk_signal_create();
    if(bmac_tx_pkt_done_signal==NRK_ERROR)
    {
        printf("BMAC ERROR: creating tx signal failed\r\n");
        //nrk_kernel_error_add(NRK_SIGNAL_CREATE_ERROR,nrk_cur_task_TCB->task_ID);
        return NRK_ERROR;
    }
    bmac_enable_signal=nrk_signal_create();
    if(bmac_enable_signal==NRK_ERROR)
    {
        printf("BMAC ERROR: creating enable signal failed\r\n");
       // nrk_kernel_error_add(NRK_SIGNAL_CREATE_ERROR,nrk_cur_task_TCB->task_ID);
        return NRK_ERROR;
    }
	

    tx_data_ready=0;
		
    // Set the one main rx buffer
    rx_buf_empty=0;
    bmac_rfRxInfo.pPayload = NULL;
    bmac_rfRxInfo.max_length = 0;

    // Setup the MRF24J40 chip
    rf_init (&bmac_rfRxInfo, chan, 0xffff, 0);
    g_chan=chan;

		/*
    FASTSPI_SETREG(CC2420_RSSI, 0xE580); // CCA THR=-25
    FASTSPI_SETREG(CC2420_TXCTRL, 0x80FF); // TX TURNAROUND = 128 us
    FASTSPI_SETREG(CC2420_RXCTRL1, 0x0A56);
    */
		
		// default cca thresh of -45
    //rf_set_cca_thresh(-45);
    rf_set_cca_thresh(-45);
		
		// Disable checking address field
		rf_addr_decode_disable();
		
    bmac_running=1;
    is_enabled=1;
    return NRK_OK;
}
Пример #7
0
void rx_task()
{
nrk_time_t t;
uint16_t cnt;
int8_t v;
uint8_t len,i,chan;


cnt=0;
nrk_kprintf( PSTR("Nano-RK Version ") );
printf( "%d\r\n",NRK_VERSION );

  
printf( "Gateway Task PID=%u\r\n",nrk_get_pid());
t.secs=10;
t.nano_secs=0;

// setup a software watch dog timer
nrk_sw_wdt_init(0, &t, NULL);
nrk_sw_wdt_start(0);

  chan = 16;
  if (SET_MAC == 0xffff) {

    v = read_eeprom_mac_address (&mac_address);
    if (v == NRK_OK) {
      v = read_eeprom_channel (&chan);
    }
    else {
      while (1) {
        nrk_kprintf (PSTR
                     ("* ERROR reading MAC address, run eeprom-set utility\r\n"));
  	nrk_led_toggle(RED_LED);
        nrk_wait_until_next_period ();
      }
    }
  }
  else
    mac_address = SET_MAC;

  printf ("MAC ADDR: %x\r\n", mac_address & 0xffff);
  printf ("chan = %d\r\n", chan);


tdma_init(TDMA_HOST, chan, mac_address);

// Change these parameters anytime you want...
tdma_set_slot_len_ms(10);
tdma_set_slots_per_cycle(12);

slip_init (stdin, stdout, 0, 0);

while(!tdma_started()) nrk_wait_until_next_period();
nrk_led_set(GREEN_LED);
  while(1) {
	v=tdma_recv(&rx_tdma_fd, &slip_tx_buf, &len, TDMA_BLOCKING );	
	nrk_led_set(ORANGE_LED);
	if(v==NRK_OK)
	{
		//for(i=0; i<len; i++ ) printf( "%c", rx_buf[i]);
		// Got a packet from the network so send it over SLIP
		slip_tx ( slip_tx_buf, len );
	}
	else tdma_rx_pkt_release();
	nrk_led_clr(ORANGE_LED);
	nrk_sw_wdt_update(0); 
  	}
}
Пример #8
0
void tx_task ()
{
    uint8_t j, i, val, cnt;
    int8_t len;
    int8_t v,fd;
    nrk_sig_mask_t ret;
    nrk_time_t t;



    // Set Port D.0 as input
    // On the FireFly nodes we use this for motion or syntonistor input
    // It can be interrupt driven, but we don't do this with TDMA since updates are so fast anyway



    printf ("tx_task PID=%d\r\n", nrk_get_pid ());

    // setup a software watch dog timer
    t.secs=10;
    t.nano_secs=0;
    nrk_sw_wdt_init(0, &t, NULL);
    nrk_sw_wdt_start(0);

    while (!tdma_started ())
        nrk_wait_until_next_period ();

    // set port G as input for gpio
    nrk_gpio_direction(NRK_PORTG_0,NRK_PIN_INPUT );

    send_ack=0;

    while (1) {
        nrk_led_clr(RED_LED);

        tx_pkt.payload[0] = 1;  // ELEMENTS
        tx_pkt.payload[1] = 4;  // Key
        tx_pkt.payload[2] = nrk_gpio_get(NRK_PORTG_0);  // Key
        tx_pkt.payload_len=3;
        tx_pkt.src_mac=mac_address;
        tx_pkt.dst_mac=0;
        tx_pkt.type=APP;

        if(send_ack==1)
        {
            tx_pkt.type=ACK;
            tx_pkt.payload_len=0;
            send_ack=0;
        }


        len=pkt_to_buf(&tx_pkt,&tx_buf );

        if(len>0)
        {
            v = tdma_send (&tx_tdma_fd, &tx_buf, len, TDMA_BLOCKING);
            if (v == NRK_OK) {
            }
        } else {
            nrk_kprintf(PSTR("Pkt tx error\r\n"));
            nrk_wait_until_next_period();
        }


        nrk_sw_wdt_update(0);
    }



}
Пример #9
0
int8_t bmac_init (uint8_t chan)
{
  bmac_running = 0;
  tx_reserve = -1;
  cca_active = true;
  rx_failure_cnt = 0;
#ifdef NRK_SW_WDT
#ifdef BMAC_SW_WDT_ID

  _bmac_check_period.secs = 30;
  _bmac_check_period.nano_secs = 0;
  nrk_sw_wdt_init (BMAC_SW_WDT_ID, &_bmac_check_period, NULL);
  nrk_sw_wdt_start (BMAC_SW_WDT_ID);
#endif
#endif



  _bmac_check_period.secs = 0;
  _bmac_check_period.nano_secs = BMAC_DEFAULT_CHECK_RATE_MS * NANOS_PER_MS;
  bmac_rx_pkt_signal = nrk_signal_create ();
  if (bmac_rx_pkt_signal == NRK_ERROR) {
    nrk_kprintf (PSTR ("BMAC ERROR: creating rx signal failed\r\n"));
    nrk_kernel_error_add (NRK_SIGNAL_CREATE_ERROR, nrk_cur_task_TCB->task_ID);
    return NRK_ERROR;
  }
  bmac_tx_pkt_done_signal = nrk_signal_create ();
  if (bmac_tx_pkt_done_signal == NRK_ERROR) {
    nrk_kprintf (PSTR ("BMAC ERROR: creating tx signal failed\r\n"));
    nrk_kernel_error_add (NRK_SIGNAL_CREATE_ERROR, nrk_cur_task_TCB->task_ID);
    return NRK_ERROR;
  }
  bmac_enable_signal = nrk_signal_create ();
  if (bmac_enable_signal == NRK_ERROR) {
    nrk_kprintf (PSTR ("BMAC ERROR: creating enable signal failed\r\n"));
    nrk_kernel_error_add (NRK_SIGNAL_CREATE_ERROR, nrk_cur_task_TCB->task_ID);
    return NRK_ERROR;
  }


  tx_data_ready = 0;
  // Set the one main rx buffer
  rx_buf_empty = 0;
  bmac_rfRxInfo.pPayload = NULL;
  bmac_rfRxInfo.max_length = 0;

  // Setup the cc2420 chip
  rf_power_up ();
  rf_init (&bmac_rfRxInfo, chan, 0xffff, 0);
  g_chan = chan;

//    FASTSPI_SETREG(CC2420_RSSI, 0xE580); // CCA THR=-25
//    FASTSPI_SETREG(CC2420_TXCTRL, 0x80FF); // TX TURNAROUND = 128 us
//    FASTSPI_SETREG(CC2420_RXCTRL1, 0x0A56); 
  // default cca thresh of -45
  //rf_set_cca_thresh(-45); 
  rf_set_cca_thresh (0x0);
  bmac_running = 1;
  is_enabled = 1;
  return NRK_OK;
}