void Task1() { uint16_t cnt; int8_t fd; nrk_kprintf( PSTR("Nano-RK Version ") ); printf( "%d\r\n",NRK_VERSION ); //while(1) nrk_wait_until_next_period(); nrk_gpio_direction(NRK_MMC_9,NRK_PIN_INPUT); nrk_gpio_direction(NRK_MMC_10,NRK_PIN_INPUT); nrk_gpio_direction(NRK_MMC_11,NRK_PIN_INPUT); bpm_index=0; nrk_gpio_pullups(1); nrk_ext_int_configure(NRK_PC_INT_5, NULL, &button_press_int); nrk_ext_int_configure(NRK_PC_INT_6, NULL, &button_press_int); nrk_ext_int_configure(NRK_PC_INT_7, NULL, &button_press_int); nrk_ext_int_enable(NRK_PC_INT_5); nrk_ext_int_enable(NRK_PC_INT_6); nrk_ext_int_enable(NRK_PC_INT_7); nrk_ext_int_configure(NRK_EXT_INT_0, NRK_RISING_EDGE, &heart_rate_int); nrk_ext_int_enable(NRK_EXT_INT_0); cnt=0; printf( "My node's address is %u\r\n",NODE_ADDR ); fd=nrk_open(FIREFLY_SENSOR_BASIC,READ); if(fd==NRK_ERROR) nrk_kprintf(PSTR("Failed to open sensor driver\r\n")); printf( "Task1 PID=%u\r\n",nrk_get_pid()); while(1) { //printf( "Task1 cnt=%u\r\n",cnt ); nrk_wait_until_next_period(); printf( "9=%d ",nrk_gpio_get(NRK_MMC_9)); printf( "10=%d ",nrk_gpio_get(NRK_MMC_10)); printf( "11=%d ",nrk_gpio_get(NRK_MMC_11)); printf( "%d %d\r\n",cnt,hrm_get_value() ); nrk_led_toggle(GREEN_LED); // Uncomment this line to cause a stack overflow cnt++; } }
// This function will put the node into a low-duty checking mode to save power void tdma_snooze() { int8_t v; uint8_t i; // stop the software watchdog timer so it doesn't interrupt nrk_sw_wdt_stop(0); // This flag is cleared only by the button interrupt snoozing=1; // Setup the button interrupt nrk_ext_int_configure( NRK_EXT_INT_1,NRK_LEVEL_TRIGGER, &wakeup_func); // Clear it so it doesn't fire instantly EIFR=0xff; nrk_ext_int_enable( NRK_EXT_INT_1); // Now loop and periodically check for packets while(1) { nrk_led_clr(RED_LED); nrk_led_clr(GREEN_LED); rf_power_up(); rf_rx_on (); // check return from button interrupt on next cycle if(snoozing==0 ) return; tmp_time.secs=0; tmp_time.nano_secs=10*NANOS_PER_MS; nrk_led_set(RED_LED); // Leave radio on for two loops of 10ms for a total of 20ms every 10 seconds for(i=0; i<2; i++ ) { v = _tdma_rx (); if(v==NRK_OK) { nrk_led_set(RED_LED); nrk_ext_int_disable( NRK_EXT_INT_1); nrk_sw_wdt_update(0); nrk_sw_wdt_start(0); return NRK_OK; } nrk_wait(tmp_time); } nrk_led_clr(RED_LED); rf_power_down(); tmp_time.secs=10; tmp_time.nano_secs=0; nrk_sw_wdt_stop(0); nrk_wait(tmp_time); } }
void tx_task () { uint8_t j, i, val, cnt; int8_t len; int8_t v; nrk_sig_t tx_done_signal; nrk_sig_mask_t ret; send_ack=0; cal_done=0; printf ("tx_task PID=%d\r\n", nrk_get_pid ()); // Wait until the tx_task starts up bmac // This should be called by all tasks using bmac that power_init (); #ifndef DISABLE_BUTTON nrk_gpio_direction(NRK_BUTTON,NRK_PIN_INPUT ); nrk_ext_int_configure(NRK_EXT_INT_0, NRK_FALLING_EDGE, &button_handler ); nrk_ext_int_enable(NRK_EXT_INT_0); #endif v_center=((uint16_t)nrk_eeprom_read_byte(EEPROM_CAL_V_MSB_ADDR))<<8 | (uint16_t)nrk_eeprom_read_byte(EEPROM_CAL_V_LSB_ADDR); c_center=((uint16_t)nrk_eeprom_read_byte(EEPROM_CAL_C1_MSB_ADDR))<<8 | (uint16_t)nrk_eeprom_read_byte(EEPROM_CAL_C1_LSB_ADDR); c2_center=((uint16_t)nrk_eeprom_read_byte(EEPROM_CAL_C2_MSB_ADDR))<<8 | (uint16_t)nrk_eeprom_read_byte(EEPROM_CAL_C2_LSB_ADDR); if(((PIND & 0x1) == 0) || (v_center==0xffff) || (v_center==0x0) ) { // Get v_center and c_centers enough to grab calibration values v_center=512; c_center=512; c2_center=512; power_socket_enable(0); socket_0_disable(); plug_led_green_clr(); plug_led_red_clr(); for(i=0; i<3; i++ ) { plug_led_green_set(); nrk_wait_until_next_period(); plug_led_green_clr(); nrk_wait_until_next_period(); } plug_led_green_clr(); plug_led_red_clr(); for(i=0; i<5; i++ ) nrk_wait_until_next_period(); v_center=(v_p2p_high+v_p2p_low)/2; c_center=(c_p2p_high+c_p2p_low)/2; c2_center=(c_p2p_high2+c_p2p_low2)/2; nrk_eeprom_write_byte(EEPROM_CAL_V_MSB_ADDR, (uint8_t)(v_center>>8)); nrk_eeprom_write_byte(EEPROM_CAL_V_LSB_ADDR, (uint8_t)v_center&0xff); nrk_eeprom_write_byte(EEPROM_CAL_C1_MSB_ADDR, (uint8_t)(c_center>>8)); nrk_eeprom_write_byte(EEPROM_CAL_C1_LSB_ADDR, (uint8_t)c_center&0xff); nrk_eeprom_write_byte(EEPROM_CAL_C2_MSB_ADDR, (uint8_t)(c2_center>>8)); nrk_eeprom_write_byte(EEPROM_CAL_C2_LSB_ADDR, (uint8_t)c2_center&0xff); nrk_eeprom_write_byte(EEPROM_ENERGY1_0_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_1_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_2_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_3_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_4_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_5_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_6_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY1_7_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_0_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_1_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_2_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_3_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_4_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_5_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_6_ADDR, 0); nrk_eeprom_write_byte(EEPROM_ENERGY2_7_ADDR, 0); //plug_led_green_set(); //socket_0_enable(); //power_socket_enable(0); // Set default power threshold set_power_thresh(DEFAULT_POWER_THRESH); power_socket_disable(0); socket_0_disable(); }
void deep_sleep_button() { int i,cnt; nrk_int_disable(); nrk_eeprom_write_byte(EEPROM_SLEEP_STATE_ADDR,1); nrk_led_set(0); nrk_spin_wait_us(50000); nrk_led_clr(0); nrk_led_clr(1); nrk_led_clr(2); nrk_led_clr(3); nrk_watchdog_disable(); nrk_int_disable(); _nrk_os_timer_stop(); nrk_gpio_direction(NRK_BUTTON, NRK_PIN_INPUT); rf_power_down(); // Enable Pullup for button //PORTD = 0xff; nrk_gpio_set(NRK_BUTTON); nrk_ext_int_configure( NRK_EXT_INT_1,NRK_LEVEL_TRIGGER, &wakeup_func); EIFR=0xff; nrk_ext_int_enable( NRK_EXT_INT_1); nrk_int_enable(); DDRA=0x0; DDRB=0x0; DDRC=0x0; ASSR=0; TRXPR = 1 << SLPTR; set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); // reboot while(1) { //printf( "awake!\r\n" ); nrk_led_clr(0); nrk_led_clr(1); nrk_led_set(1); cnt=0; for(i=0; i<100; i++ ) { //if((PIND & 0x2)==0x2)cnt++; if(nrk_gpio_get(NRK_BUTTON)==0)cnt++; nrk_spin_wait_us(10000); } printf( "cnt=%d\n",cnt ); if(cnt>=50 ) { // reboot nrk_led_clr(1); nrk_led_set(0); nrk_eeprom_write_byte(EEPROM_SLEEP_STATE_ADDR,0); nrk_spin_wait_us(50000); nrk_spin_wait_us(50000); nrk_watchdog_enable(); while(1); } nrk_led_clr(0); nrk_led_clr(1); TRXPR = 1 << SLPTR; set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); } }