int8_t power_socket_enable (uint8_t socket) { if (socket == 0) { socket_0_enable (); socket_0_active = 1; nrk_int_disable(); nrk_eeprom_write_byte(EEPROM_STATE_ADDR, socket_0_active); nrk_int_enable(); } nrk_timer_int_start (NRK_APP_TIMER_0); return 1; }
void Task1() { uint16_t cnt; uint8_t i,t; uint32_t ut; nrk_kprintf( PSTR("Nano-RK Version ") ); printf( "%d\r\n",NRK_VERSION ); for(i=0; i<255; i++ ) nrk_eeprom_write_byte(NRK_ERROR_EEPROM_INDEX+(uint16_t)i, 0xff); nrk_kprintf(PSTR("Error log eeprom erased...")); while(1) { nrk_led_toggle(GREEN_LED); nrk_wait_until_next_period(); } }
int8_t power_socket_disable (uint8_t socket) { if (socket == 0) { socket_0_disable (); socket_0_active = 0; nrk_int_disable(); nrk_eeprom_write_byte(EEPROM_STATE_ADDR, socket_0_active); nrk_int_enable(); } /* if (socket_1_active == 0 && socket_0_active == 0) { // Turn interrupt off to save power nrk_timer_int_stop (NRK_APP_TIMER_0); // Shutdown monitor circuit to save power power_mon_disable (); } */ return 1; }
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(); } }
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; 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 (); 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); }
void event_detector_task() { update_energy_sig=nrk_signal_create(); if(update_energy_sig==NRK_ERROR) nrk_kprintf(PSTR("Error creating update_energy_sig signal!\r\n")); nrk_signal_register(update_energy_sig); if(v==NRK_ERROR) nrk_kprintf( PSTR( "nrk_signal_register failed\r\n" )); //ev_timeout.secs=10; //ev_timeout.nano_secs=0; while(1) { // Wait on signal //nrk_set_next_wakeup(ev_timeout); my_sigs=nrk_event_wait( SIG(update_energy_sig) /*| SIG(nrk_wakeup_signal)*/ ); if(my_sigs!=0 && ticks_last>900) { tmp_d=current_total_last / ticks_last; if(tmp_d<0) tmp_d=0; tmp_d=sqrt(tmp_d); rms_current=(uint16_t)tmp_d; tmp_d=current_total2_last / ticks_last; if(tmp_d<0) tmp_d=0; tmp_d=sqrt(tmp_d); rms_current2=(uint16_t)tmp_d; tmp_d=voltage_total_last / ticks_last; if(tmp_d<0) tmp_d=0; tmp_d=sqrt(tmp_d); rms_voltage=(uint16_t)tmp_d; if(energy_total_last<0) energy_total_last=0; true_power=energy_total_last / ticks_last; if(true_power>TRUE_POWER_ON_THRESH) cummulative_energy.total+=true_power; if(energy_total2_last<0) energy_total2_last=0; true_power2=energy_total2_last / ticks_last; if(true_power2>TRUE_POWER_ON_THRESH) cummulative_energy2.total+=true_power2; total_secs++; // Divide by seconds per hour to give Watts per hour // If this is too slow, make a power of 2 shit instead... tmp_energy.total=cummulative_energy.total/3600; tmp_energy2.total=cummulative_energy2.total/3600; // Only care about lower 6 bytes of 8 byte long long // At max power this will last 239 years // Divide by P-scaler to get Watt*hrs if(total_secs%400==0 ) { nrk_int_disable(); nrk_eeprom_write_byte(EEPROM_ENERGY1_0_ADDR, cummulative_energy.byte[0]); nrk_eeprom_write_byte(EEPROM_ENERGY1_1_ADDR, cummulative_energy.byte[1]); nrk_eeprom_write_byte(EEPROM_ENERGY1_2_ADDR, cummulative_energy.byte[2]); nrk_eeprom_write_byte(EEPROM_ENERGY1_3_ADDR, cummulative_energy.byte[3]); nrk_eeprom_write_byte(EEPROM_ENERGY1_4_ADDR, cummulative_energy.byte[4]); nrk_eeprom_write_byte(EEPROM_ENERGY1_5_ADDR, cummulative_energy.byte[5]); nrk_eeprom_write_byte(EEPROM_ENERGY1_6_ADDR, cummulative_energy.byte[6]); nrk_eeprom_write_byte(EEPROM_ENERGY1_7_ADDR, cummulative_energy.byte[7]); nrk_eeprom_write_byte(EEPROM_ENERGY2_0_ADDR, cummulative_energy2.byte[0]); nrk_eeprom_write_byte(EEPROM_ENERGY2_1_ADDR, cummulative_energy2.byte[1]); nrk_eeprom_write_byte(EEPROM_ENERGY2_2_ADDR, cummulative_energy2.byte[2]); nrk_eeprom_write_byte(EEPROM_ENERGY2_3_ADDR, cummulative_energy2.byte[3]); nrk_eeprom_write_byte(EEPROM_ENERGY2_4_ADDR, cummulative_energy2.byte[4]); nrk_eeprom_write_byte(EEPROM_ENERGY2_5_ADDR, cummulative_energy2.byte[5]); nrk_eeprom_write_byte(EEPROM_ENERGY2_6_ADDR, cummulative_energy2.byte[6]); nrk_eeprom_write_byte(EEPROM_ENERGY2_7_ADDR, cummulative_energy2.byte[7]); nrk_int_enable(); } /* // EVENT DETECTOR tran_pkt.msgs_payload=async_buf; tran_pkt.num_msgs=0; //if(rms_current>current_last) delta=rms_current-current_last; //else delta=current_last-rms_current; if(true_power>true_power_last) delta=true_power-true_power_last; else delta=true_power_last-true_power; if(true_power2>true_power_last2) delta2=true_power2-true_power_last2; else delta=true_power_last2-true_power2; //if(rms_current2>current_last2) delta2=rms_current2-current_last2; //else delta2=current_last2-rms_current2; if((socket_0_push_enabled && (delta>socket_0_push_threshold*100)) || (socket_1_push_enabled && (delta2>socket_1_push_threshold*100))) { pd_pkt.type=DEBUG_PKT; pd_pkt.rms_voltage=rms_voltage; pd_pkt.rms_current=rms_current; pd_pkt.rms_current2=rms_current2; pd_pkt.true_power=true_power; pd_pkt.true_power2=true_power2; pd_pkt.freq=freq; pd_pkt.socket0_state= socket_0_active; pd_pkt.socket1_state= socket_1_active; for(len=0; len<6; len++ ) { pd_pkt.energy[len]=tmp_energy.byte[len]; pd_pkt.energy2[len]=tmp_energy2.byte[len]; } pd_pkt.current_p2p_high=l_c_p2p_high; pd_pkt.current_p2p_low=l_c_p2p_low; pd_pkt.current_p2p_high2=l_c_p2p_high2; pd_pkt.current_p2p_low2=l_c_p2p_low2; pd_pkt.voltage_p2p_high=l_v_p2p_high; pd_pkt.voltage_p2p_low=l_v_p2p_low; pd_pkt.total_secs=total_secs; tran_pkt.num_msgs=0; tran_pkt.checksum=0; tran_pkt.msgs_payload=&(async_buf[TRANSDUCER_PKT_HEADER_SIZE]); tran_msg.payload=&(async_buf[TRANSDUCER_PKT_HEADER_SIZE+TRANSDUCER_MSG_HEADER_SIZE]); tran_msg.type=TRAN_POWER_PKT; tran_msg.len=ff_power_debug_pack(tran_msg.payload, &pd_pkt); tran_msg.mac_addr=my_mac; len=transducer_msg_add( &tran_pkt, &tran_msg); len = transducer_pkt_pack(&tran_pkt, async_buf); val=push_p2p_pkt( async_buf,len,TRANSDUCER_PKT, 0x0 ); } */ true_power_last=true_power; true_power_last2=true_power2; } // toggle RED led if freq not above 50Hz //if(freq==0) nrk_led_toggle(RED_LED); } }