Example #1
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(buttonIT_process, ev, data)
{
  PROCESS_BEGIN();

  BUTTON_IRQ_EDGE_SELECTD();
  BUTTON_SELECT();
  BUTTON_MAKE_INPUT();
  BUTTON_SET_HANDLER();
  BUTTON_ENABLE_IRQ();
  while (1)
  {
    PROCESS_PAUSE();
    leds_on(LEDS_BLUE);
    delay();
    leds_off(LEDS_BLUE);
    delay();
  }

  PROCESS_END();
}
Example #2
0
/*---------------------------------------------------------------------------*/
static void tcpip_handler(void) {
  memset(buf, 0, MAX_PAYLOAD_LEN);
  if(uip_newdata()) {
    leds_on(LEDS_RED);
    len = uip_datalen();
    memcpy(buf, uip_appdata, len);
    PRINTF("%u bytes from [", len);
    PRINT6ADDR(&UIP_IP_BUF->srcipaddr);
    PRINTF("]:%u\n", UIP_HTONS(UIP_UDP_BUF->srcport));
    uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
    server_conn->rport = UIP_UDP_BUF->srcport;

    uip_udp_packet_send(server_conn, buf, len);
    /* Restore server connection to allow data from any node */
    uip_create_unspecified(&server_conn->ripaddr);
    server_conn->rport = 0;
  }
  leds_off(LEDS_RED);
  return;
}
Example #3
0
PROCESS_THREAD(sensys_tx, ev, data)
{
 PROCESS_EXITHANDLER()
 PROCESS_BEGIN();
 
 // Initial configurations  on CC2420 and resetting the timer
 leds_off(LEDS_ALL);
 cc2420_set_txpower(POWER);
 cc2420_set_channel(CHANNEL_SENDER);
 unicast_open(&uc, RIME_SENDER, &unicast_callbacks); 
 ctimer_stop(&timer1);
  
 // Ready to send...
 leds_on(LEDS_BLUE); 
 
 while(1) {
	PROCESS_WAIT_EVENT();		
 }        
 PROCESS_END();
}
Example #4
0
// Timer timeout callback
static void ctimer1_callback(void *ptr)
{ 	
  leds_on(LEDS_GREEN);
  struct message msg;
  msg.seqno = count++;	
  packetbuf_clear();
  packetbuf_copyfrom(&msg, sizeof(struct message));
  rimeaddr_t addr;
  addr.u8[0] = RECEIVER;
  addr.u8[1] = 0;
  unicast_send(&uc, &addr);	
  printf("Sending message %ld to %d.0\n",msg.seqno,RECEIVER);
  if(count >= total_to_be_sent){
	 count = 0;
  }
  else{
	ctimer_set(&timer1, PACKETS_TIME, ctimer1_callback, NULL);
  }
  leds_off(LEDS_GREEN);
}
Example #5
0
int main(void) {
    
    WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
    eint();

    clock_dco_set(8); // DCO 8MHz
    clock_mclk_set(CLOCK_SOURCE_DCO, 1); // MCLK 8MHz
    clock_smclk_set(CLOCK_SOURCE_DCO, 8); // SMCLK 1MHz
    clock_aclk_set(1);

    leds_init();
    leds_off(LEDS_ALL);

    mac_init();
    mac_set_tx_cb(sent);
    mac_set_rx_cb(rx);

    timer_start(TIMER_SOURCE_ACLK, 1);
    timer_register_cb(TIMER_ALARM_0, send);
    timer_set_alarm(TIMER_ALARM_0, 12000, 12000, TIMER_MODE_FROM_NOW, 0);

    leds_on(LEDS_ALL);


    while (1) {
        event = 0;
        LPM0;
        switch (event) {
        case EVENT_SEND:
            mac_send(msg, strlen(msg), MAC_BROADCAST);
            break;
        case EVENT_SENT:
            leds_toggle(LED_GREEN);
            break;
        case EVENT_RX:
            leds_toggle(LED_RED);
            break;
        }
    }
    return 0;
}
Example #6
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(broadcast_example_process, ev, data)
{
  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t addr;
  //uip_ip6addr(&addr, 0xfe80, 0, 0, 0, 0, 0, 0, 1);
  //uip_ip6addr(&addr, 0xfe01, 0, 0, 0, 0, 0, 0, 1);

  PROCESS_BEGIN();

#if PLATFORM_HAS_LEDS
  leds_off(LEDS_ALL);
#endif

  simple_udp_register(&broadcast_connection, UDP_PORT,
                      NULL, UDP_PORT,
                      receiver);

  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {
    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));
    uip_create_linklocal_allnodes_mcast(&addr);
#if PLATFORM_HAS_LEDS
  leds_on(LEDS_ALL);
#endif
    /*printf("Sending broadcast from: %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x on port %d\n", 
	addr.u16[0],addr.u16[1],addr.u16[2],addr.u16[3],addr.u16[4],addr.u16[5],addr.u16[6],addr.u16[7], 
	UDP_PORT);*/
    printf("Sending broadcast from: %02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x on port %d\n", addr.u8[0],addr.u8[1],addr.u8[2],addr.u8[3],addr.u8[4],addr.u8[5],addr.u8[6],addr.u8[7], addr.u8[8],addr.u8[9],addr.u8[10],addr.u8[11],addr.u8[12],addr.u8[13],addr.u8[14],addr.u8[15], UDP_PORT);
    simple_udp_sendto(&broadcast_connection, "Test", 4, &addr);
#if PLATFORM_HAS_LEDS
  leds_off(LEDS_ALL);
#endif
  }

  PROCESS_END();
}
Example #7
0
int main()
{
    uint8_t part_num, version_num;

    // Initialize the platform
    platform_init();

    printf("Testing RF2XX\n");

    // Set initial values
    leds_on(LED_0);
    leds_off(LED_1);

    printf("Initializing RF212...");
    // Init. the radio
    rf2xx_init(rf212);
    rf2xx_wakeup(rf212);
    printf("OK\n");

    printf("Reading RF212 PART_NUM (should be 7): ");
    part_num = rf2xx_reg_read(rf212, RF2XX_REG__PART_NUM);
    printf("%x\n", part_num);

    printf("Reading RF212 VERSION_NUM (should be 1): ");
    version_num = rf2xx_reg_read(rf212, RF2XX_REG__VERSION_NUM);
    printf("%x\n", version_num);

    while (1)
    {
        int i;

        for (i = 0; i < 0x10000; i++)
        {
            __asm__("nop");
        }

        leds_toggle(LED_0 + LED_1);
    }

    return 0;
}
PROCESS_THREAD(shell_sleepy_trilat_start_process, ev, data)
{
	PROCESS_BEGIN();

	open_runicast();
	my_node = rimeaddr_node_addr.u8[0];

	if (my_node != SINK_NODE)
	{	
		static struct etimer etimer0;
		static char message[4];
		static int i = 0;
		
		etimer_set(&etimer0, CLOCK_SECOND/16);
		sensor_init();
//		leds_on(LEDS_ALL);
		PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&etimer0));
		for (i = 1; i <= 100; i++)
		{
			etimer_set(&etimer0, CLOCK_SECOND/50);
			PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&etimer0));			
			my_noise = sensor_read() + my_noise;
		}
		sensor_uinit();
		my_noise = my_noise / 100; 
		
		etimer_set(&etimer0, CLOCK_SECOND * (my_node - FIRST_NODE + 1));
		leds_on(LEDS_ALL);
		PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&etimer0));
		leds_off(LEDS_ALL);	

		itoa(my_noise, message, 10);
		strcat(message, "!\0");

		transmit_runicast(message, SINK_NODE);

//		process_start(&node_timeout_process, NULL);
	}

	PROCESS_END();
}
Example #9
0
PROCESS_THREAD(shell_round_robin_blink_process, ev, data)
{
	PROCESS_BEGIN();

	static struct etimer etimer;
	static uint8_t my_node;
	my_node = rimeaddr_node_addr.u8[0];
	static uint8_t next_node;
	next_node = my_node + 1;
	
	if (my_node == LAST_NODE)
		next_node = FIRST_NODE;

	etimer_set(&etimer, CLOCK_SECOND/FREQUENCY);
	leds_on(LEDS_ALL);
	PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&etimer));
	transmit_unicast("Hello", next_node);
	leds_off(LEDS_ALL);
	
	PROCESS_END();
}
Example #10
0
File: main.c Project: dbohn/openlab
int main()
{
// Initialize the platform
platform_init();

/* compatibility with HiKoB */
if (uart_external == NULL)
    uart_external = uart_print;
else
    uart_enable(uart_external, 500000);

// Start the soft timer
soft_timer_init();

// Start the serial lib
iotlab_serial_start(500000);

// Start the application libs
cn_control_start();

cn_consumption_start();
cn_radio_start();
/* map i2c start stop to dc start/stop */
cn_control_config(cn_i2c_stop, cn_i2c_start);
cn_autotest_start();
cn_logger_reset();

//set the open node power to off and charge the battery
fiteco_lib_gwt_opennode_power_select(FITECO_GWT_OPENNODE_POWER__OFF);
fiteco_lib_gwt_battery_charge_enable();

//initialize the led, red off, green on
leds_off(LEDS_MASK);
leds_on(GREEN_LED);

// Run
platform_run();
return 0;
}
Example #11
0
static int
write_state(lwm2m_context_t *ctx, const uint8_t *inbuf, size_t insize,
            uint8_t *outbuf, size_t outsize)
{
  int value;
  size_t len;

  len = ctx->reader->read_boolean(ctx, inbuf, insize, &value);
  printf("Leds control value: %d\n", value);

  if(len > 0) {
    if (value == 0 && is_on == 1) {
      is_on = 0;
      leds_off(led_value); 
    }
    if (value == 1 && is_on == 0) {
      is_on = 1;
      leds_on(led_value);
    } 
  }
  return len;
}
Example #12
0
/*---------------------------------------------------------------------------*/
static void
pub_handler(const char *topic, uint16_t topic_len, const uint8_t *chunk,
            uint16_t chunk_len)
{
    DBG("Pub Handler: topic='%s' (len=%u), chunk_len=%u\n", topic, topic_len,
        chunk_len);

    /* If we don't like the length, ignore */

    if(/*topic_len != 23 ||*/ chunk_len != 1) {
        printf("Incorrect topic or chunk len. Ignored\n");
        return;
    }

    /* If the format != json, ignore
    if(strncmp(&topic[topic_len - 4], "json", 4) != 0) {
      printf("Incorrect format\n");
    }*/

    if(strstr(topic, "/cmd/leds") != NULL) {
        if(chunk[0] == '1') {
            leds_on(LEDS_RED);
        } else if(chunk[0] == '0') {
            leds_off(LEDS_RED);
        }
        return;
    }

#if BOARD_SENSORTAG
    if(strstr(topic, "/cmd/buzz") != NULL) {
        if(chunk[0] == '1') {
            buzzer_start(1000);
        } else if(chunk[0] == '0') {
            buzzer_stop();
        }
        return;
    }
#endif
}
Example #13
0
int main()
{
    int i;

    setup_io();

    while(1)
    {
	leds_on();

	printf("LED ON....\n");
	
	wait_rtc();
	
	leds_off();

	printf("LED OFF...\n");

	wait_rtc();
    }

}
Example #14
0
/*---------------------------------------------------------------------------*/
static void
request_recv(struct runicast_conn *c, rimeaddr_t *from, uint8_t seqno)
{
  const char *filename;
  uint8_t seq;

  if(packetbuf_datalen() < 2) {
    /* Bad filename, ignore request */
    printf("download: bad filename request (null)\n");
    return;
  }

  seq = ((uint8_t *)packetbuf_dataptr())[0];
  if(seq == req_last_seq) {
    PRINTF("download: ignoring duplicate request\n");
    return;
  }
  req_last_seq = seq;
  filename = ((char *)packetbuf_dataptr()) + 1;

  PRINTF("file requested: '%s'\n", filename);

  /* Initiate file transfer */
  leds_on(LEDS_GREEN);
  if(fd >= 0) {
    cfs_close(fd);
  }
  fd = cfs_open(filename, CFS_READ);
  if(fd < 0) {
    printf("download: bad filename request (no read access): %s\n", filename);
  } else {
    PRINTF("download: sending file: %s\n", filename);
  }

  rucb_close(&rucb);
  rucb_open(&rucb, RUCB_CHANNEL, &rucb_call);
  rucb_send(&rucb, from);
}
Example #15
0
static void pkt_tick(handler_arg_t arg)
{
    // unused
    (void) arg;

    enum tdma_result res;

    /* ensure we're connected */
    if (mac_tdma_is_connected())
    {
        leds_on(LED_1);
    }
    else
    {
        leds_off(LED_1);
        return;
    }

    /* get a packet */
    packet_t *packet = packet_alloc(0);
    if (!packet)
    {
        log_error("Can't allocate a packet");
        return;
    }

    /* fill it */
    log_printf("Send packet %u\n", index);
    *(packet->data) = index++;
    packet->length = 1;

    /* send the packet to the coordinator */
    if ((res = mac_tdma_send(packet, 0, pkt_sent, packet)) != TDMA_OK)
    {
        packet_free(packet);
        log_printf("Packet sending failed %d\n", res);
    }
}
Example #16
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(http_example_process, ev, data)
{
  static struct etimer et;
  uip_ip4addr_t ip4addr;
  uip_ip6addr_t ip6addr;

  PROCESS_BEGIN();

  uip_ipaddr(&ip4addr, 8, 8, 8, 8);
  ip64_addr_4to6(&ip4addr, &ip6addr);
  uip_nameserver_update(&ip6addr, UIP_NAMESERVER_INFINITE_LIFETIME);

  etimer_set(&et, CLOCK_SECOND * 20);
  PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

  memset(url_buffer, 0, HTTP_CLIENT_BUFFER_LEN);
  snprintf(url_buffer, HTTP_CLIENT_BUFFER_LEN,
           "http://maker.ifttt.com/trigger/%s/with/key/%s",
           IFTTT_EVENT, IFTTT_KEY);

  http_socket_init(&s);

  restarts = 0;

  while(1) {
    PROCESS_YIELD();
    if((ev == sensors_event) && (data == &button_sensor)) {
      if(button_sensor.value(BUTTON_SENSOR_VALUE_TYPE_LEVEL) ==
        BUTTON_SENSOR_PRESSED_LEVEL) {
        leds_on(LEDS_GREEN);
        printf("Button pressed! sending a POST to IFTTT\n");
        http_socket_post(&s, url_buffer, NULL, 0, NULL, callback, NULL);
      }
    }
  }

  PROCESS_END();
}
Example #17
0
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
{
    char *appdata;

    if(uip_newdata()) {

        leds_on(LEDS_RED);

        appdata = (char *)uip_appdata;
        appdata[uip_datalen()] = 0;
        PRINTF("Report RX '%s' from ", appdata);
        PRINTF("%d",
               UIP_IP_BUF->srcipaddr.u8[sizeof(UIP_IP_BUF->srcipaddr.u8) - 1]);
        PRINTF("\n");
#if SERVER_REPLY
        PRINTF("DATA sending reply\n");
        uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
        uip_udp_packet_send(server_conn, "Reply", sizeof("Reply"));
        uip_create_unspecified(&server_conn->ripaddr);
#endif
    }
}
Example #18
0
int main(void) {
    WDTCTL = WDTPW + WDTHOLD;                 // Stop watchdog timer
    eint();
    
    clock_dco_set(1); // DCO 1MHz
    clock_mclk_set(CLOCK_SOURCE_DCO, 1); // MCLK 1MHz
    clock_smclk_set(CLOCK_SOURCE_DCO, 8); // SMCLK 125kHz
    clock_aclk_set(1);

    leds_init();
    leds_on(LEDS_ALL);
    
    // init the rtc
    rtc_init();
    
    exec_alarm();
    
    while (1) {
        LPM0;
    }
    
    return 0;
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(handle_command(struct httpd_state *s))
{
  PSOCK_BEGIN(&s->sout);

  SEND_STRING(&s->sout, TOP);

  if(s->filename[1] == '0') {
    /* Turn off leds */
    leds_off(LEDS_ALL);
    SEND_STRING(&s->sout, "Turned off leds!");
  } else if(s->filename[1] == '1') {
    /* Turn on leds */
    leds_on(LEDS_ALL);
    SEND_STRING(&s->sout, "Turned on leds!");
  } else {
    SEND_STRING(&s->sout, "Unknown command");
  }

  SEND_STRING(&s->sout, BOTTOM);

  PSOCK_END(&s->sout);
}
PROCESS_THREAD(test_process, ev, data)
{
  static struct etimer etimer;

  PROCESS_EXITHANDLER(goto exit);
  PROCESS_BEGIN();

  printf("test_process starting\n");

  while(1) {
    leds_on(LEDS_RED);
    etimer_set(&etimer, CLOCK_SECOND);
    PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
    leds_off(LEDS_RED);
    etimer_set(&etimer, CLOCK_SECOND);
    PROCESS_WAIT_UNTIL(etimer_expired(&etimer));
  }

 exit:
  printf("test_process exiting\n");
  leds_off(LEDS_RED);
  PROCESS_END();
}
Example #21
0
int main()
{
    signed portBASE_TYPE ret;

    // Initialize the platform
    platform_init();

    log_info("USB HID test\r\n================\r\n");

    // Initialize USB
    usb_init(USB_HID);

    leds_on(LED_1);

    ret = xTaskCreate(vMouseTask, (signed char *)"Mouse", configMINIMAL_STACK_SIZE, NULL, 1, &vMouseTaskHandle);

    switch (ret)
    {
        case errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY:
            log_error("Could not allocate required memory");

            while (1)
            {
                ;
            }

            return 0;

        default:
            log_debug("Mouse task created successfully");
    }

    // Start the scheduler
    platform_run();

    return 0;
}
Example #22
0
static void panic(unsigned int type) {
	puts("\r\n****PANIC = ");
	putunsigned(type);
	puts("****\r\n");
	
	//spegni tutti i led
	leds_off(ALL_LED);
	
	//maschera dei led da accendere
	u32 ledmask = 0;
	
	u32 mask = 0x8;
	while(mask != 0) {
		switch(type & mask) {
			case 0x8:
				ledmask |= LED4;
				break;
			case 0x4:
				ledmask |= LED3;
				break;
			case 0x2:
				ledmask |= LED2;
				break;
			case 0x1:
				ledmask |= LED1;
				break;
		}		
		mask >>= 1;
	}
	
	leds_on(ledmask);
	
	for(;;) {
		loop_delay(3000000u);	//buona frequenza di lampeggiamento
		leds_toggle(ledmask);
	}
}
Example #23
0
void wdt_irq_handler(void)
{
	static int cnt = 0;
	
	/* Clear WDT interrupt */
	WTCLRINT = 1;

	printf("%d\r\n", ++cnt);

	leds_on(1);
	delay();
	leds_off(1);
	delay();

	clearVecAddress();

	if (cnt == 10) {
		printf("Resetting cpu...\r\n");
		//wdt_ops(RESET_EN, INTERRUPT_EN, CLOCK_DIV_FACTOR, WDT_EN, PRESCALER_VAL, WTDAT_VAL, WTCNT_VAL);
		WTDAT = WTDAT_VAL;
		WTCNT = WTCNT_VAL;
		WTCON = (PRESCALER_VAL<<8 | WDT_EN<<5 | CLOCK_DIV_FACTOR<<3 | INTERRUPT_EN<<2 | RESET_EN<<0);
	}
}
PROCESS_THREAD(cc26xx_demo_process, ev, data)
{
  PROCESS_EXITHANDLER(broadcast_close(&bc))

  PROCESS_BEGIN();

  gpio_relay_init();
  relay_all_clear();
  
  
  counter = 0;
  broadcast_open(&bc, BROADCAST_CHANNEL, &bc_rx);

  etimer_set(&et, CLOCK_SECOND);
  
  while(1) {

    PROCESS_YIELD();

    if(ev == PROCESS_EVENT_TIMER) {
      leds_on(LEDS_PERIODIC);
     
      etimer_set(&et, CLOCK_SECOND*5);
      rtimer_set(&rt, RTIMER_NOW() + LEDS_OFF_HYSTERISIS, 1,
                 rt_callback, NULL);
      counter = Get_ADC_reading();
      packetbuf_copyfrom(&counter, sizeof(counter));
      broadcast_send(&bc);
     // printf("adc data value : %d \r\n",counter);
        
    } 
     watchdog_periodic();
  }

  PROCESS_END();
}
Example #25
0
File: xmac.c Project: aiss83/nucbit
/*---------------------------------------------------------------------------*/
static int
detect_ack(void)
{
#define INTER_PACKET_INTERVAL              RTIMER_ARCH_SECOND / 5000
#define ACK_LEN 3
#define AFTER_ACK_DETECTECT_WAIT_TIME      RTIMER_ARCH_SECOND / 1000
    rtimer_clock_t wt;
    uint8_t ack_received = 0;

    wt = RTIMER_NOW();
    leds_on(LEDS_GREEN);
    while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + INTER_PACKET_INTERVAL)) { }
    leds_off(LEDS_GREEN);
    /* Check for incoming ACK. */
    if((NETSTACK_RADIO.receiving_packet() ||
            NETSTACK_RADIO.pending_packet() ||
            NETSTACK_RADIO.channel_clear() == 0))
    {
        int len;
        uint8_t ackbuf[ACK_LEN + 2];

        wt = RTIMER_NOW();
        while(RTIMER_CLOCK_LT(RTIMER_NOW(), wt + AFTER_ACK_DETECTECT_WAIT_TIME)) { }

        len = NETSTACK_RADIO.read(ackbuf, ACK_LEN);
        if(len == ACK_LEN)
        {
            ack_received = 1;
        }
    }
    if(ack_received)
    {
        leds_toggle(LEDS_RED);
    }
    return ack_received;
}
Example #26
0
/**
 * \brief A process that handles adding/removing
 *        BLE IPSP interfaces.
 */
PROCESS_THREAD(ble_iface_observer, ev, data)
{
  static struct etimer led_timer;

  PROCESS_BEGIN();

  etimer_set(&led_timer, CLOCK_SECOND/2);

  while(1) {
    PROCESS_WAIT_EVENT();
    if(ev == ble_event_interface_added) {
      etimer_stop(&led_timer);
      leds_off(LEDS_1);
      leds_on(LEDS_2);
    } else if(ev == ble_event_interface_deleted) {
      etimer_set(&led_timer, CLOCK_SECOND/2);
      leds_off(LEDS_2);
    } else if(ev == PROCESS_EVENT_TIMER && etimer_expired(&led_timer)) {
      etimer_reset(&led_timer);
      leds_toggle(LEDS_1);
    }
  }
  PROCESS_END();
}
Example #27
0
/*
 * keypad_scan: perform a single scan of keyboard. Returns keycode of
 * key that was pressed (or -1 if nothing).
 */
int keypad_scan( void )
{
    int row, col;
    int pressed = -1;

    leds_off( );		// turn off LEDs during scan
    keypad_write_cols( ~1 );	// single zero at column 0
    data_h( );			// shift in ones
    for( col = 0; col < MAX_COLS; col++ )
    {
        keypad_state[col] = get_rows( );
        clk_h( );
        clk_l( );
    }
    keypad_write_cols( ~leds );
    leds_on( );

    // keyboard has been scanned, now look for pressed keys
    for( col = 0; col < MAX_COLS; col++ )
    {
        uint8_t diff = keypad_state[col] ^ keypad_prev[col];

        if( diff )
        {
            for( row = 0; row < MAX_ROWS; row++ )
            {
                uint8_t mask = 1 << row;

                if( diff & mask & keypad_state[col] )
                    pressed = row * 16 + col;
            }
        }
        keypad_prev[col] = keypad_state[col];
    }
    return pressed;
}
Example #28
0
/* this function has been defined to be called when a unicast is being received */
static void recv_uc(struct unicast_conn *c, const rimeaddr_t *from)
{
  // Round-trip time, will be decremented later
  clock_time_t rtt = clock_time();

  printf("unicast message received from %d.%d\n", from->u8[0], from->u8[1]);
  /* turn on blue led */
  leds_on(LEDS_BLUE);
  /* set the timer "leds_off_timer" to 1/8 second */
  ctimer_set(&leds_off_timer_send, CLOCK_SECOND / 8, timerCallback_turnOffLeds, NULL);

  /* from the packet we have just received, read the data and write it into the
   * struct tmReceived we have declared and instantiated above (line 16)
   */
  packetbuf_copyto(&tmReceived);

  /* print the contents of the received packet */
  printf("time received = %d clock ticks", (uint16_t)tmReceived.time);
  printf(" = %d secs ", (uint16_t)tmReceived.time / CLOCK_SECOND);
  printf("%d millis ", (1000L * ((uint16_t)tmReceived.time  % CLOCK_SECOND)) / CLOCK_SECOND);
  printf("originator = %d\n", tmReceived.originator);

  // If the packet received is not ours, send it back to the originator
  if(tmReceived.originator != node_id) {
    packetbuf_copyfrom(&tmReceived, sizeof(tmSent));

    if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr)) {
      /* when calling unicast_send, we have to specify the address as the second argument (a pointer to the defined rimeaddr_t struct) */
      unicast_send(&uc, &addr);
    }
    printf("sending packet to %u\n", addr.u8[0]);
  } else { // Our packet has completed a round-trip
    rtt -= tmReceived.time;
    printf("RTT = %d ms\n", (1000L * ((uint16_t)rtt  % CLOCK_SECOND)) / CLOCK_SECOND);
  }
}
Example #29
0
/*---------------------------------------------------------------------------*/
int
main(int argc, char **argv)
{
  /*
   * Initalize hardware.
   */

  msp430_cpu_init();
  clock_init();
  leds_init();

  leds_on(LEDS_RED);

  clock_wait(2);

  uart1_init(115200); /* Must come before first printf */

#if WITH_UIP
  slip_arch_init(115200);
#endif /* WITH_UIP */

  clock_wait(1);

  leds_on(LEDS_GREEN);
  //ds2411_init();

  /* XXX hack: Fix it so that the 802.15.4 MAC address is compatible
     with an Ethernet MAC address - byte 0 (byte 2 in the DS ID)
     cannot be odd. */
  //ds2411_id[2] &= 0xfe;

  leds_on(LEDS_BLUE);
  //xmem_init();

  leds_off(LEDS_RED);
  rtimer_init();
  /*
   * Hardware initialization done!
   */

  node_id = NODE_ID;

  /* Restore node id if such has been stored in external mem */
  //node_id_restore();

  /* for setting "hardcoded" IEEE 802.15.4 MAC addresses */
#ifdef IEEE_802154_MAC_ADDRESS
  {
    uint8_t ieee[] = IEEE_802154_MAC_ADDRESS;
    //memcpy(ds2411_id, ieee, sizeof(uip_lladdr.addr));
    //ds2411_id[7] = node_id & 0xff;
  }
#endif

  //random_init(ds2411_id[0] + node_id);

  leds_off(LEDS_BLUE);
  /*
   * Initialize Contiki and our processes.
   */
  process_init();
  process_start(&etimer_process, NULL);

  ctimer_init();

  init_platform();

  set_rime_addr();

  cc2520_init();
  {
    uint8_t longaddr[8];
    uint16_t shortaddr;

    shortaddr = (rimeaddr_node_addr.u8[0] << 8) +
      rimeaddr_node_addr.u8[1];
    memset(longaddr, 0, sizeof(longaddr));
    rimeaddr_copy((rimeaddr_t *)&longaddr, &rimeaddr_node_addr);

    printf("MAC %02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x ",
           longaddr[0], longaddr[1], longaddr[2], longaddr[3],
           longaddr[4], longaddr[5], longaddr[6], longaddr[7]);

    cc2520_set_pan_addr(IEEE802154_PANID, shortaddr, longaddr);
  }
  cc2520_set_channel(RF_CHANNEL);

  printf(CONTIKI_VERSION_STRING " started. ");
  if(node_id > 0) {
    printf("Node id is set to %u.\n", node_id);
  } else {
    printf("Node id is not set.\n");
  }

#if WITH_UIP6
  /* memcpy(&uip_lladdr.addr, ds2411_id, sizeof(uip_lladdr.addr)); */
  memcpy(&uip_lladdr.addr, rimeaddr_node_addr.u8,
         UIP_LLADDR_LEN > RIMEADDR_SIZE ? RIMEADDR_SIZE : UIP_LLADDR_LEN);

  /* Setup nullmac-like MAC for 802.15.4 */
/*   sicslowpan_init(sicslowmac_init(&cc2520_driver)); */
/*   printf(" %s channel %u\n", sicslowmac_driver.name, RF_CHANNEL); */

  /* Setup X-MAC for 802.15.4 */
  queuebuf_init();
  NETSTACK_RDC.init();
  NETSTACK_MAC.init();
  NETSTACK_NETWORK.init();

  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
         NETSTACK_MAC.name, NETSTACK_RDC.name,
         CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0 ? 1:
                         NETSTACK_RDC.channel_check_interval()),
         RF_CHANNEL);

  process_start(&tcpip_process, NULL);

  printf("Tentative link-local IPv6 address ");
  {
    uip_ds6_addr_t *lladdr;
    int i;
    lladdr = uip_ds6_get_link_local(-1);
    for(i = 0; i < 7; ++i) {
      printf("%02x%02x:", lladdr->ipaddr.u8[i * 2],
             lladdr->ipaddr.u8[i * 2 + 1]);
    }
    printf("%02x%02x\n", lladdr->ipaddr.u8[14], lladdr->ipaddr.u8[15]);
  }

  if(!UIP_CONF_IPV6_RPL) {
    uip_ipaddr_t ipaddr;
    int i;
    uip_ip6addr(&ipaddr, 0xaaaa, 0, 0, 0, 0, 0, 0, 0);
    uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
    uip_ds6_addr_add(&ipaddr, 0, ADDR_TENTATIVE);
    printf("Tentative global IPv6 address ");
    for(i = 0; i < 7; ++i) {
      printf("%02x%02x:",
             ipaddr.u8[i * 2], ipaddr.u8[i * 2 + 1]);
    }
    printf("%02x%02x\n",
           ipaddr.u8[7 * 2], ipaddr.u8[7 * 2 + 1]);
  }

#else /* WITH_UIP6 */

  NETSTACK_RDC.init();
  NETSTACK_MAC.init();
  NETSTACK_NETWORK.init();

  printf("%s %s, channel check rate %lu Hz, radio channel %u\n",
         NETSTACK_MAC.name, NETSTACK_RDC.name,
         CLOCK_SECOND / (NETSTACK_RDC.channel_check_interval() == 0? 1:
                         NETSTACK_RDC.channel_check_interval()),
         RF_CHANNEL);
#endif /* WITH_UIP6 */

#if !WITH_UIP && !WITH_UIP6
  uart1_set_input(serial_line_input_byte);
  serial_line_init();
#endif

  leds_off(LEDS_GREEN);

#if TIMESYNCH_CONF_ENABLED
  timesynch_init();
  timesynch_set_authority_level((rimeaddr_node_addr.u8[0] << 4) + 16);
#endif /* TIMESYNCH_CONF_ENABLED */

#if WITH_UIP
  process_start(&tcpip_process, NULL);
  process_start(&uip_fw_process, NULL);	/* Start IP output */
  process_start(&slip_process, NULL);

  slip_set_input_callback(set_gateway);

  {
    uip_ipaddr_t hostaddr, netmask;

    uip_init();

    uip_ipaddr(&hostaddr, 172,16,
	       rimeaddr_node_addr.u8[0],rimeaddr_node_addr.u8[1]);
    uip_ipaddr(&netmask, 255,255,0,0);
    uip_ipaddr_copy(&meshif.ipaddr, &hostaddr);

    uip_sethostaddr(&hostaddr);
    uip_setnetmask(&netmask);
    uip_over_mesh_set_net(&hostaddr, &netmask);
    /*    uip_fw_register(&slipif);*/
    uip_over_mesh_set_gateway_netif(&slipif);
    uip_fw_default(&meshif);
    uip_over_mesh_init(UIP_OVER_MESH_CHANNEL);
    printf("uIP started with IP address %d.%d.%d.%d\n",
           uip_ipaddr_to_quad(&hostaddr));
  }
#endif /* WITH_UIP */

  energest_init();
  ENERGEST_ON(ENERGEST_TYPE_CPU);

  watchdog_start();
  /* Stop the watchdog */
  watchdog_stop();

#if !PROCESS_CONF_NO_PROCESS_NAMES
  print_processes(autostart_processes);
#else /* !PROCESS_CONF_NO_PROCESS_NAMES */
  putchar('\n'); /* include putchar() */
#endif /* !PROCESS_CONF_NO_PROCESS_NAMES */
  autostart_start(autostart_processes);

  /*
   * This is the scheduler loop.
   */
  while(1) {

    int r;
    do {
      /* Reset watchdog. */
      watchdog_periodic();
      r = process_run();
    } while(r > 0);

    /*
     * Idle processing.
     */
    int s = splhigh();		/* Disable interrupts. */
    /* uart1_active is for avoiding LPM3 when still sending or receiving */
    if(process_nevents() != 0 || uart1_active()) {
      splx(s);                  /* Re-enable interrupts. */
    } else {
      static unsigned long irq_energest = 0;

      /* Re-enable interrupts and go to sleep atomically. */
      ENERGEST_OFF(ENERGEST_TYPE_CPU);
      ENERGEST_ON(ENERGEST_TYPE_LPM);
      /* We only want to measure the processing done in IRQs when we
	 are asleep, so we discard the processing time done when we
	 were awake. */
      energest_type_set(ENERGEST_TYPE_IRQ, irq_energest);
      watchdog_stop();
      _BIS_SR(GIE | SCG0 | SCG1 | CPUOFF); /* LPM3 sleep. This
                                              statement will block
                                              until the CPU is
                                              woken up by an
                                              interrupt that sets
                                              the wake up flag. */

      /* We get the current processing time for interrupts that was
         done during the LPM and store it for next time around.  */
      dint();
      irq_energest = energest_type_time(ENERGEST_TYPE_IRQ);
      eint();
      watchdog_start();
      ENERGEST_OFF(ENERGEST_TYPE_LPM);
      ENERGEST_ON(ENERGEST_TYPE_CPU);
    }
  }
}
Example #30
0
char glossy_scheduler(struct rtimer *t, void *ptr) {
	PT_BEGIN(&pt);

	if (IS_INITIATOR()) {	// Glossy initiator.
		while (1) {
			printf("[SCHEDULER]: Get Data from queue\n");
			// Increment sequence number.
			glossy_data.seq_no++;
			// Glossy phase.
			leds_on(LEDS_GREEN);
			rtimer_clock_t t_stop = RTIMER_TIME(t) + GLOSSY_DURATION;
			// Start Glossy.
			glossy_start((uint8_t *)&glossy_data, DATA_LEN, GLOSSY_INITIATOR, GLOSSY_SYNC, N_TX,
					APPLICATION_HEADER, t_stop, (rtimer_callback_t)glossy_scheduler, t, ptr);
			// Store time at which Glossy has started.
			t_start = RTIMER_TIME(t);
			// Yield the protothread. It will be resumed when Glossy terminates.
			PT_YIELD(&pt);

			// Off phase.
			leds_off(LEDS_GREEN);
			// Stop Glossy.
			glossy_stop();
			if (!GLOSSY_IS_BOOTSTRAPPING()) {
				// Glossy has already successfully bootstrapped.
				if (!GLOSSY_IS_SYNCED()) {
					// The reference time was not updated: increment reference time by GLOSSY_PERIOD.
					set_t_ref_l(GLOSSY_REFERENCE_TIME + GLOSSY_PERIOD);
					set_t_ref_l_updated(1);
				}
			}
			// Schedule begin of next Glossy phase based on GLOSSY_PERIOD.
			rtimer_set(t, t_start + GLOSSY_PERIOD, 1, (rtimer_callback_t)glossy_scheduler, ptr);
			// Estimate the clock skew over the last period.
			estimate_period_skew();
			// Poll the process that prints statistics (will be activated later by Contiki).
			process_poll(&glossy_print_stats_process);
			// Yield the protothread.
			PT_YIELD(&pt);
		}
	} else {	// Glossy receiver.
		while (1) {
			// Glossy phase.
			leds_on(LEDS_GREEN);
			rtimer_clock_t t_stop;
			if (GLOSSY_IS_BOOTSTRAPPING()) {
				// Glossy is still bootstrapping:
				// Schedule end of Glossy phase based on GLOSSY_INIT_DURATION.
				t_stop = RTIMER_TIME(t) + GLOSSY_INIT_DURATION;
			} else {
				// Glossy has already successfully bootstrapped:
				// Schedule end of Glossy phase based on GLOSSY_DURATION.
				t_stop = RTIMER_TIME(t) + GLOSSY_DURATION;
			}
			// Start Glossy.
			glossy_start((uint8_t *)&glossy_data, DATA_LEN, GLOSSY_RECEIVER, GLOSSY_SYNC, N_TX,
					APPLICATION_HEADER, t_stop, (rtimer_callback_t)glossy_scheduler, t, ptr);
			// Yield the protothread. It will be resumed when Glossy terminates.
			PT_YIELD(&pt);

			// Off phase.
			leds_off(LEDS_GREEN);
			// Stop Glossy.
			glossy_stop();
			if (GLOSSY_IS_BOOTSTRAPPING()) {
				// Glossy is still bootstrapping.
				if (!GLOSSY_IS_SYNCED()) {
					// The reference time was not updated: reset skew_estimated to zero.
					skew_estimated = 0;
				}
			} else {
				// Glossy has already successfully bootstrapped.
				if (!GLOSSY_IS_SYNCED()) {
					// The reference time was not updated:
					// increment reference time by GLOSSY_PERIOD + period_skew.
					set_t_ref_l(GLOSSY_REFERENCE_TIME + GLOSSY_PERIOD + period_skew);
					set_t_ref_l_updated(1);
					// Increment sync_missed.
					sync_missed++;
				} else {
					// The reference time was not updated: reset sync_missed to zero.
					sync_missed = 0;
				}
			}
			// Estimate the clock skew over the last period.
			estimate_period_skew();
			if (GLOSSY_IS_BOOTSTRAPPING()) {
				// Glossy is still bootstrapping.
				if (skew_estimated == 0) {
					// The reference time was not updated:
					// Schedule begin of next Glossy phase based on last begin and GLOSSY_INIT_PERIOD.
					rtimer_set(t, RTIMER_TIME(t) + GLOSSY_INIT_PERIOD, 1,
							(rtimer_callback_t)glossy_scheduler, ptr);
				} else {
					// The reference time was updated:
					// Schedule begin of next Glossy phase based on reference time and GLOSSY_INIT_PERIOD.
					rtimer_set(t, GLOSSY_REFERENCE_TIME + GLOSSY_PERIOD - GLOSSY_INIT_GUARD_TIME, 1,
							(rtimer_callback_t)glossy_scheduler, ptr);
				}
			} else {
				// Glossy has already successfully bootstrapped:
				// Schedule begin of next Glossy phase based on reference time and GLOSSY_PERIOD.
				rtimer_set(t, GLOSSY_REFERENCE_TIME + GLOSSY_PERIOD +
						period_skew - GLOSSY_GUARD_TIME * (1 + sync_missed), 1,
						(rtimer_callback_t)glossy_scheduler, ptr);
			}
			// Poll the process that prints statistics (will be activated later by Contiki).
			process_poll(&glossy_print_stats_process);
			// Yield the protothread.
			PT_YIELD(&pt);
		}
	}

	PT_END(&pt);
}