Exemple #1
0
void discover_task()
{
	uint8_t len;
	uint8_t connection_l;
	while (!bmac_started ())
    nrk_wait_until_next_period ();		

	if(log_g) printf("log:discover_task PID=%d\r\n",nrk_get_pid());


	while(1) {
			nrk_sem_pend(conn_sem);
			connection_l = connection_g;
			nrk_sem_post(conn_sem);
			

			if(connection_l == 0 && vertsk_active_g == 0 && version_g[MAC_ADDR] > 0) {
				nrk_led_toggle(BLUE_LED);	
				if(log_g) printf("log:Sending discover pkt\r\n");
				nrk_sem_pend(tx_sem);
				sprintf(tx_buf,"0:%d:S:%d",MAC_ADDR,version_g[MAC_ADDR]);
				bmac_tx_pkt(tx_buf, strlen(tx_buf));
				nrk_sem_post(tx_sem);
				memset(tx_buf,0,strlen(tx_buf));
			}
    	nrk_wait_until_next_period ();
	
	}

}
Exemple #2
0
// atomic_update_network_joined
void inline atomic_update_network_joined(uint8_t update) {
  nrk_sem_pend(g_network_joined_mux); 
  { 
    g_network_joined = update;
  }
  nrk_sem_post(g_network_joined_mux);  
}
Exemple #3
0
// atomic_update_outlet_state - atomically update outlet state
void inline atomic_update_outlet_state(uint8_t update) {
  nrk_sem_pend(g_global_outlet_state_mux); 
  {
    g_global_outlet_state = update;
  }
  nrk_sem_post(g_global_outlet_state_mux);
}
Exemple #4
0
void rx_task ()
{
    uint8_t i,len;
    int8_t rssi, v;
    nrk_status_t ret;
    uint8_t *local_rx_buf;

//    printf( "rx_task: PID=%d\r\n",nrk_get_pid());

    bmac_rx_pkt_set_buffer(rx_buf,RF_MAX_PAYLOAD_SIZE);

    while(!bmac_started()) nrk_wait_until_next_period();

    while(1)
    {
        if( bmac_rx_pkt_ready()==0)
            bmac_wait_until_rx_pkt();  

        v = nrk_sem_pend(lock);
        if( startCnt == 0 )
            startCnt = 1;
        v = nrk_sem_post(lock);

        nrk_led_toggle(GREEN_LED); 
        local_rx_buf = bmac_rx_pkt_get(&len,&rssi);

        printf( "rx_task: rssi=%d data=", rssi);
        for( i=0; i<len; i++ ) {
            printf( "%c", local_rx_buf[i]);
        }
        nrk_kprintf( PSTR("\r\n") );

        bmac_rx_pkt_release();
    }
}
Exemple #5
0
// atomic_pop - pop onto the queue atomically
void inline atomic_pop(packet_queue *pq, packet *p, nrk_sem_t *mux) {
  nrk_sem_pend(mux); 
  {
    pop(pq, p);
  }
  nrk_sem_post(mux);
}
Exemple #6
0
void tx_task()
{
  uint8_t cnt = 0;
  uint8_t val;
  int8_t v;
  
//  printf( "tx_task: PID=%d\r\n",nrk_get_pid());
  
  bmac_init(25);
  while(!bmac_started()) nrk_wait_until_next_period();

  while(1)
  {
        if (cnt > 25) {
            nrk_led_set(BLUE_LED);
            nrk_terminate_task();
        }

        nrk_led_set(RED_LED); 
        sprintf( tx_buf, "%d", cnt );
        val = bmac_tx_pkt(tx_buf, strlen(tx_buf));
        nrk_led_clr(RED_LED); 
//        nrk_kprintf( PSTR("TX task sent data!\r\n") );
        nrk_wait_until_next_period();

        v = nrk_sem_pend(lock);
        if ( startCnt )
            cnt++;
        v = nrk_sem_post(lock);
  }
}
Exemple #7
0
void Task1()
{
uint16_t cnt;
int8_t v;

printf( "My node's address is %d\r\n",NODE_ADDR );

  printf( "Task1 PID=%d\r\n",nrk_get_pid());
  cnt=0;
  while(1) {
	nrk_led_toggle(ORANGE_LED);
	printf( "Task1 cnt=%d\r\n",cnt );
	nrk_kprintf( PSTR("Task1 accessing semaphore\r\n"));
	v = nrk_sem_pend(my_semaphore);
	if(v==NRK_ERROR) nrk_kprintf( PSTR("T1 error pend\r\n"));
	nrk_kprintf( PSTR("Task1 holding semaphore\r\n"));
	// wait some time inside semaphore to show the effect
	nrk_wait_until_next_period();
	v = nrk_sem_post(my_semaphore);
	if(v==NRK_ERROR) nrk_kprintf( PSTR("T1 error post\r\n"));
	nrk_kprintf( PSTR("Task1 released semaphore\r\n"));
	nrk_wait_until_next_period();
	cnt++;
	}
}
Exemple #8
0
void Task2() {
	uint8_t cnt;
	int8_t v;

	cnt = 0;
	while (1) {
		nrk_led_toggle(BLUE_LED);
//		printf("Task2 cnt=%d\r\n", cnt);
//		nrk_kprintf(PSTR("Task2 accessing semaphore\r\n"));
//		printf("T2_next_period = %u \n", nrk_cur_task_TCB->next_period);

		v = nrk_sem_pend(sem1);

		if (v == NRK_ERROR)
			nrk_kprintf(PSTR("T2 error pend\r\n"));
//		nrk_kprintf(PSTR("Task2 holding semaphore\r\n"));
//		printf("In T2 sys_ceiling = %u \n", system_ceiling);
		printf("In T2 Q --- ");
		nrk_print_readyQ();
		printf("\n");
		// to something within the critical section
		nrk_wait_until_next_period();
		v = nrk_sem_post(sem1);
		if (v == NRK_ERROR)
			nrk_kprintf(PSTR("T2 error post\r\n"));
//		nrk_kprintf(PSTR("Task2 released semaphore\r\n"));
		nrk_wait_until_next_period();

		cnt++;
	}

}
Exemple #9
0
void Task1() {
	uint16_t cnt;
	int8_t v;

	cnt = 0;
	while (1) {
		nrk_led_toggle(ORANGE_LED);
//		printf("Task1 cnt=%d\r\n", cnt);
//		nrk_kprintf(PSTR("Task1 accessing semaphore\r\n"));
//		printf("T1_next_period = %u \n", nrk_cur_task_TCB->next_period);
		v = nrk_sem_pend(sem1);
		if (v == NRK_ERROR)
			nrk_kprintf(PSTR("T1 error pend\r\n"));
//		nrk_kprintf(PSTR("Task1 holding semaphore\r\n"));
//		printf("In T1 sys_ceiling = %u \n", system_ceiling);
		printf("In T1 Q --- ");
		nrk_print_readyQ();
		printf("\n");
		// to something within the critical section
		nrk_wait_until_next_period();
		v = nrk_sem_post(sem1);
		if (v == NRK_ERROR)
			nrk_kprintf(PSTR("T1 error post\r\n"));
//		nrk_kprintf(PSTR("Task1 released semaphore\r\n"));
		nrk_wait_until_next_period();

		if (cnt == 3)
			break;
		cnt++;
	}
	nrk_stats_display_all();
}
Exemple #10
0
// atomic_update_button_pressed - atomically update button_pressed flag
void inline atomic_update_button_pressed(uint8_t update) {
  nrk_sem_pend(g_button_pressed_mux); 
  {
    g_button_pressed = update;
  }
  nrk_sem_post(g_button_pressed_mux);  
}
Exemple #11
0
// creates Task4 and uses semaphore1
void Task4()
{
  uint8_t counter;
  uint8_t waitCount=3;

  printf( "Task4 PID=%d\r\n",nrk_get_pid());
  counter=0;
   while(1) 
  {
        nrk_led_toggle(GREEN_LED);
        printf( "Task4 counter=%d\r\n",counter );
		if(0==waitCount)
		{
			nrk_kprintf( PSTR("Task4 accessing semaphore1\r\n"));
			nrk_sem_pend(semaphore1);
			nrk_kprintf( PSTR("Task4 holding semaphore1\r\n"));
		}
	   	 waitCount++;
	   if(3==waitCount)
	   {
			nrk_sem_post(semaphore1);
			nrk_kprintf( PSTR("Task4 released semaphore1\r\n"));
			waitCount=0;
		}
        nrk_wait_until_next_period();
        counter++;
    }

}
Exemple #12
0
// atomic_kick_watchdog - atomically kick the watchdog timer
uint8_t inline atomic_kick_watchdog() {
  nrk_sem_pend(g_net_watchdog_mux); 
  {
    g_net_watchdog = HEART_FACTOR;
  }
  nrk_sem_post(g_net_watchdog_mux);  
  return HEART_FACTOR;  
}
Exemple #13
0
void Task2()
{
  int16_t cnt;
  int8_t val;
uint32_t sector = 0;
nrk_sem_t *radio_sem;


while(1) nrk_wait_until_next_period();
radio_sem= rf_get_sem();
if(radio_sem==NULL) nrk_kprintf( PSTR("radio sem failed!\r\n" ));
  printf( "Task2 PID=%u\r\n",nrk_get_pid());
  cnt=0;


  val=mmc_init();
  if(val!=0 ) {
	printf( "val=%d\r\n",val );
        nrk_kprintf( PSTR("card init failed\r\n") );
        while(1);
        }

  while(1) {
	nrk_sem_pend (radio_sem);
        printf("\nsector %lu\n\r",sector);                // show sector number
        val=mmc_readsector(sector,sectorbuffer);    // read a data sector
        printf( "readsector returned %d\n",val );
        for(cnt=0; cnt<32; cnt++ )
                printf( "%d ",sectorbuffer[cnt] );
        printf( "\n\r" );

        val=sectorbuffer[0];
        val++;
        for(cnt=0; cnt<512; cnt++ )
        {
        sectorbuffer[cnt]=val;
        }

        nrk_kprintf( PSTR("Writting\r\n") );
        val=mmc_writesector(sector,sectorbuffer);    // read a data sector
        printf( "writesector returned %d\n",val );
        printf( "After write:\r\n" );
        val=mmc_readsector(sector,sectorbuffer);    // read a data sector
	nrk_sem_post(radio_sem);
        printf( "readsector returned %d\n",val );
        if(val==0)
        {
         for(cnt=0; cnt<32; cnt++ )
                printf( "%d ",sectorbuffer[cnt] );
        nrk_kprintf( PSTR("\n\r") );
        }
sector++;
//	nrk_led_toggle(RED_LED);
//	printf( "Task2 signed cnt=%d\r\n",cnt );
	nrk_wait_until_next_period();
	cnt--;
	}
}
Exemple #14
0
// atomic_network_joined - atomically return the network status
uint8_t inline atomic_network_joined() {
  uint8_t returnVal;
  nrk_sem_pend(g_network_joined_mux); 
  { 
    returnVal = g_network_joined;
  }
  nrk_sem_post(g_network_joined_mux); 
  return returnVal; 
}
Exemple #15
0
// atomic_outlet_state - atomically return outlet state
uint8_t inline atomic_outlet_state() {
  uint8_t outlet_state = OFF;
  nrk_sem_pend(g_global_outlet_state_mux); 
  {
    outlet_state = g_global_outlet_state;
  }
  nrk_sem_post(g_global_outlet_state_mux);
  return outlet_state;
}
Exemple #16
0
/***** HELPER FUNCTIONS *****/
uint8_t inline atomic_size(packet_queue *pq, nrk_sem_t *mux) {
  uint8_t toReturn;
  nrk_sem_pend(mux); 
  {
    toReturn = pq->size;
  }
  nrk_sem_post(mux);
  return toReturn;
}
Exemple #17
0
// atomic_button_pressed - atomically return the button_pressed flag
uint8_t inline atomic_button_pressed() {
  uint8_t returnVal;
  nrk_sem_pend(g_button_pressed_mux); 
  {
    returnVal = g_button_pressed;
  }
  nrk_sem_post(g_button_pressed_mux);
  return returnVal;  
}
/**********************************************************
 * stop sending a carrier pulse; set the radio to idle state
 */
void rf_carrier_off()
{
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif
        cc2420Strobe(CC2420_SRFOFF); // stop radio
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
Exemple #19
0
void rf_set_channel( uint8_t channel )
{
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_pend (radio_sem);
#endif
    halRfSetChannel(channel);
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_post(radio_sem);
#endif
}
/**********************************************************
 * start sending a carrier pulse
 * assumes wdrf_radio_test_mode() was called before doing this
 */
void rf_carrier_on()
{
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif
        cc2420Strobe(CC2420_STXON); // tell radio to start sending
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
Exemple #21
0
// atomic_increment_seq_num - increment sequence number atomically and return
uint16_t inline atomic_increment_seq_num() {
  uint16_t returnVal;
  nrk_sem_pend(g_seq_num_mux); 
  {
    g_seq_num++;
    returnVal = g_seq_num;
  }
  nrk_sem_post(g_seq_num_mux);
  return returnVal;
}
Exemple #22
0
// atomic_decrement_watchdog - atomically decrement watchdog timer
uint8_t inline atomic_decrement_watchdog() {
  uint8_t returnVal;
  nrk_sem_pend(g_net_watchdog_mux); 
  {
    g_net_watchdog--;
    returnVal = g_net_watchdog;
  }
  nrk_sem_post(g_net_watchdog_mux);  
  return returnVal;
}
/**********************************************************
 * set the radio into "normal" mode (buffered TXFIFO) and go into (data) receive */
void rf_data_mode() {
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif
        cc2420Strobe(CC2420_SRFOFF); //stop radio
        cc2420WriteReg(CC2420_MDMCTRL1, 0x0500); // default MDMCTRL1 value
        cc2420WriteReg(CC2420_DACTST, 0); // default value
        rf_flush_rx_fifo();
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
void rf_polling_rx_on(void) {
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_pend (radio_sem);
#endif
    	rfSettings.receiveOn = TRUE;
	cc2420Strobe(CC2420_SFLUSHRX);
	cc2420Strobe(CC2420_SRXON);
	
	rx_ready=0;
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_post(radio_sem);
#endif
} // rf_rx_on() 
Exemple #25
0
void retrans_task() {

  if(log_g) printf ("log:retrans_task PID=%d\r\n", nrk_get_pid ());
	uint8_t cnt = 0;
	uint8_t connection_l;
	uint8_t data_cnt;
	uint8_t prev_data_seq = 0;  
//	while (!bmac_started ())
    nrk_wait_until_next_period ();

	while(1) {
		nrk_sem_pend(conn_sem);
		connection_l = connection_g;
		nrk_sem_post(conn_sem);	

		if(pending_retransmit_g == 1)  {
				if(log_g) printf("log:Re-transmitting packet\r\n");	
				bmac_tx_pkt(uart_rx_buf, strlen(uart_rx_buf));
				retransmit_count_g++;
				if(retransmit_count_g==5)
				terminate_connection();
			} else {
				// this checks if there is a connection and one is receivng data then 
				// if the sender shuts down then the connection is terminated after a while	
				if(connection_l == 1 && data_index_g < 0) {
					if(recv_data_seq_g == prev_data_seq) 
							data_cnt++;
					if(recv_data_seq_g > prev_data_seq)
					{
							data_cnt = 0; 	
							prev_data_seq = recv_data_seq_g;
					}
					if(data_cnt > 5) {
						if(log_g)nrk_kprintf(PSTR("log:Recv cn term\n"));
						terminate_connection();
						data_cnt = 0; 
					}
				}

				cnt++;
				//if(log_g) printf("log:ver activ con:%d cnt:%d\n",connection_l,cnt);
				if(cnt>0 && connection_l == 0) {
				cnt = 0;
				vertsk_active_g = 1; 
				}
			}

		nrk_wait_until_next_period();

	}
}
Exemple #26
0
void rf_tx_power(uint8_t pwr)
{
    uint16_t tmp;
    //tmp=0x5070;
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_pend (radio_sem);
#endif
    tmp=0xA0E0;
    tmp=tmp | (pwr&0x1F);
    FASTSPI_SETREG(CC2420_TXCTRL, tmp);   // Set the FIFOP threshold to maximum
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_post(radio_sem);
#endif
}
//-------------------------------------------------------------------------------------------------------
//  void rf_rx_off(void)
//
//  DESCRIPTION:
//      Disables the CC2420 receiver and the FIFOP interrupt.
//-------------------------------------------------------------------------------------------------------
void rf_rx_off(void) {
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_pend (radio_sem);
#endif
	// XXX
    	//SET_VREG_INACTIVE();	
	rfSettings.receiveOn = FALSE;
	cc2420Strobe(CC2420_SRFOFF);
	rx_ready=0;
#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_post(radio_sem);
#endif
    //	DISABLE_FIFOP_INT();
} // rf_rx_off() 
Exemple #28
0
/**********************************************************
 * set the radio into "normal" mode (buffered TXFIFO) and go into (data) receive */
void rf_data_mode() {
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_pend(radio_sem);
#endif


        FASTSPI_STROBE(CC2420_SRFOFF); //stop radio
        FASTSPI_SETREG(CC2420_MDMCTRL1, 0x0500); // default MDMCTRL1 value
        FASTSPI_SETREG(CC2420_DACTST, 0); // default value
        rf_flush_rx_fifo();
#ifdef RADIO_PRIORITY_CEILING
nrk_sem_post(radio_sem);
#endif
}
Exemple #29
0
int8_t slip_tx (uint8_t * buf, uint8_t size)
{
  uint8_t i;
  int8_t v;
  uint8_t checksum;

// Make sure size is less than 128 so it doesn't act as a control
// message
  if (size > 128) {
    _nrk_errno_set (3);
    return NRK_ERROR;
  }

  v = nrk_sem_pend (slip_tx_sem);
  if (v == NRK_ERROR) {
    nrk_kprintf (PSTR ("SLIP TX ERROR:  Access to semaphore failed\r\n"));
    _nrk_errno_set (1);
    return NRK_ERROR;
  }

// Send the start byte
  put_byte (START);
  put_byte (size);

  checksum = 0;

// Send payload and stuff bytes as needed
  for (i = 0; i < size; i++) {
    if (buf[i] == END || buf[i] == ESC)
      put_byte (ESC);
    put_byte (buf[i]);
    checksum += buf[i];
  }

// Make sure checksum is less than 128 so it doesn't act as a control
// message
  checksum &= 0x7F;
  // Send the end byte
  put_byte (checksum);
  put_byte (END);
  v = nrk_sem_post (slip_tx_sem);
  if (v == NRK_ERROR) {
    nrk_kprintf (PSTR ("SLIP TX ERROR:  Release of semaphore failed\r\n"));
    _nrk_errno_set (2);
    return NRK_ERROR;
  }
  return NRK_OK;
}
Exemple #30
0
void rf_set_rx(RF_RX_INFO *pRRI, uint8_t channel )
{

#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_pend (radio_sem);
#endif

    FASTSPI_STROBE(CC2420_SFLUSHRX);
    FASTSPI_STROBE(CC2420_SFLUSHRX);
    halRfSetChannel(channel);
    rfSettings.pRxInfo = pRRI;

#ifdef RADIO_PRIORITY_CEILING
    nrk_sem_post(radio_sem);
#endif
}