Beispiel #1
0
uint8_t cc2420_txrx(uint8_t data)
{
    /* Wait for SPI to be ready for transmission */
    long count = 0;
    do {
        count++;
        if (count >= MAX_SPI_WAIT) {
            core_panic(0x2420, "cc2420_txrx(): SPI never ready for TX!");
        }
    } while (!(IFG2 & UCB0TXIFG));
    /* Transmit data byte to CC2420, and wait for end of transmission */
    IFG2 &= ~UCB0RXIFG;
    UCB0TXBUF = data;
    count = 0;
    do {
        count++;
        if (count >= MAX_SPI_WAIT) {
            core_panic(0x2420, "cc2420_txrx(): couldn't send byte!");
        }
    } while (UCB0STAT & UCBUSY);
    /* Read the byte that CC2420 has (normally, during TX) returned */
    count = 0;
    do {
        count++;
        if (count >= MAX_SPI_WAIT) {
            core_panic(0x2420, "cc2420_txrx(): couldn't receive byte!");
        }
    } while (!(IFG2 & UCB0RXIFG));
    /* Return received byte */
    return UCB0RXBUF;
}
Beispiel #2
0
static inline void irq_handler(i2c_t dev)
{
    assert(dev < I2C_NUMOF);

    I2C_TypeDef *i2c = i2c_config[dev].dev;

    unsigned state = i2c->ISR;
    DEBUG("\n\n### I2C ERROR OCCURED ###\n");
    DEBUG("status: %08x\n", state);
    if (state & I2C_ISR_OVR) {
        DEBUG("OVR\n");
    }
    if (state & I2C_ISR_NACKF) {
        DEBUG("AF\n");
    }
    if (state & I2C_ISR_ARLO) {
        DEBUG("ARLO\n");
    }
    if (state & I2C_ISR_BERR) {
        DEBUG("BERR\n");
    }
    if (state & I2C_ISR_PECERR) {
        DEBUG("PECERR\n");
    }
    if (state & I2C_ISR_TIMEOUT) {
        DEBUG("TIMEOUT\n");
    }
    if (state & I2C_ISR_ALERT) {
        DEBUG("SMBALERT\n");
    }
    core_panic(PANIC_GENERAL_ERROR, "I2C FAULT");
}
Beispiel #3
0
int
mon_rtc(int argc, char **argv){
  int time_buf_size =  MD_BUF_SIZE;
  char *time_buf;
  uint8_t yr, dt, mo, dw, hr, mn, sc;
  if(argc<2){
    printf("wrong number of args to set time\n");
    return -1;
  }
  if(strcmp(argv[1],"get")==0){
    time_buf = membag_alloc(time_buf_size);
    if(time_buf==NULL)
      core_panic();
    rtc_get_time_str(time_buf,time_buf_size);
    printf("Time: %s\n",time_buf);
    membag_free(time_buf);
  }
  else if(strcmp(argv[1],"set")==0){
    if(argc!=9){
      printf("please specify yr dt mo dw hr mn sc\n");
      return -1;
    }
    yr = atoi(argv[2]);
    mo = atoi(argv[3]);
    dt = atoi(argv[4]);
    dw = atoi(argv[5]);
    hr = atoi(argv[6]);
    mn = atoi(argv[7]);
    sc = atoi(argv[8]);
    if(i2c_rtc_set_time(sc,mn,hr,dw,dt,mo,yr)!=0)
      printf("error setting RTC\n");
    else{
      time_buf = membag_alloc(time_buf_size);
      if(time_buf==NULL)
	core_panic();
      rtc_get_time_str(time_buf,time_buf_size);
      printf("Set time to: %s\n",time_buf);
      membag_free(time_buf);
    }
  }
  else{
    printf("bad arguments to rtc\n");
    return -1;
  }
  return 0;
}
Beispiel #4
0
//Core memory functions
void*
core_malloc(int size){
  void* ptr;
  ptr = membag_alloc(size);
  if(ptr==NULL)
    core_panic();
  memset(ptr,0x0,size);
  return ptr;
}
Beispiel #5
0
void kw2xrf_set_out_clk(kw2xrf_t *dev)
{
    /* TODO: add clock select */
    /* check modem's crystal oscillator, CLK_OUT shall be 4MHz */
    uint8_t tmp = kw2xrf_read_dreg(dev, MKW2XDM_CLK_OUT_CTRL);
    if (tmp != 0x8Bu) {
        core_panic(PANIC_GENERAL_ERROR, "Could not start MKW2XD radio transceiver");
    }
}
Beispiel #6
0
uint8_t cc2420_get_cca(void)
{
    uint8_t status;
    long count = 0;
    do {
        unsigned int sr = disableIRQ();
        cc2420_spi_select();
        status = cc2420_txrx(NOBYTE);
        cc2420_spi_unselect();
        restoreIRQ(sr);
        count++;
        if (count >= MAX_RSSI_WAIT) {
            core_panic(0x2420, "cc2420_get_cca(): RSSI never valid!");
        }
    } while (!(status & CC2420_STATUS_RSSI_VALID));
    return CC2420_GIO1;
}
Beispiel #7
0
void isr_unhandled(void)
{
    core_panic(PANIC_UNHANDLED_ISR, "UNHANDLED ISR");
}
Beispiel #8
0
void isr_debug_mon(void)
{
    core_panic(PANIC_DEBUG_MON, "DEBUG MON HANDLER");
}
Beispiel #9
0
void isr_usage_fault(void)
{
    core_panic(PANIC_USAGE_FAULT, "ISR USAGE FAULT");
}
Beispiel #10
0
void isr_bus_fault(void)
{
    core_panic(PANIC_BUS_FAULT, "BUS FAULT");
}
Beispiel #11
0
void isr_usage_fault(void)
{
    core_panic(PANIC_USAGE_FAULT, "USAGE FAULT");
    while (1) {asm ("nop");}
}
Beispiel #12
0
void HardFault_Handler(void)
{
    core_panic(HARD_FAULT, "HARD FAULT");

    while (1);
}
Beispiel #13
0
void monitor(void){
  uint32_t prev_tick=0;

  //allocate memory for buffers and flush them
  cmd_buf = membag_alloc(CMD_BUF_SIZE);
  if(!cmd_buf)
    core_panic();
  memset(cmd_buf,0x0,CMD_BUF_SIZE);
  //initialize the power packet buffers
  tx_pkt = &power_pkts[0];
  cur_pkt = &power_pkts[1];
  //both are empty
  tx_pkt->status = POWER_PKT_EMPTY;
  cur_pkt->status = POWER_PKT_EMPTY;

  //initialize runtime configs
  wemo_config.echo = false;
  wemo_config.debug_level = DEBUG_ERROR;
  wemo_config.collect_data = true; //collect power data

  //check if we are on USB
  if(gpio_pin_is_high(VBUS_PIN)){
    rgb_led_set(LED_LT_BLUE,0); 
    //don't start wifi because we are configuring
    b_wifi_enabled=false;
    //don't collect power data
    wemo_config.collect_data = false;
  }
  //check if the plug is in calibrate mode
  if(wemo_config.calibrate){
    //start the calibration PWM
    pwm_channel_enable_interrupt(PWM,CAL_PWM_CHANNEL,CAL_PWM_CHANNEL);
    pwm_channel_enable(PWM,CAL_PWM_CHANNEL);
    //don't start wifi because we are in calibration mode
    b_wifi_enabled=false;
    wemo_config.standalone = true;
    wemo_config.collect_data = false;
    //indicate cal mode with a purple LED
    rgb_led_set(LED_PURPLE,0);
  }
  //check if reset is pressed
  if(gpio_pin_is_low(BUTTON_PIN)){
    //erase the configs
    memset(wemo_config.nilm_id,0x0,MAX_CONFIG_LEN);
    memset(wemo_config.nilm_ip_addr,0x0,MAX_CONFIG_LEN);
    memset(wemo_config.wifi_ssid,0x0,MAX_CONFIG_LEN);
    memset(wemo_config.wifi_pwd,0x0,MAX_CONFIG_LEN);
    //save the erased config
    fs_write_config();
    core_log("erased config");
    //erase the stored data 
    
    //spin until button is released
    rgb_led_set(LED_ORANGE,500);
    while(gpio_pin_is_low(BUTTON_PIN));
    rgb_led_set(LED_ORANGE,0); //disable blink
  }
  //setup WIFI
  if(b_wifi_enabled){
    if(wifi_init()!=0){
      rgb_led_set(LED_PURPLE,0);
    }
    else{
      //good to go! turn light green
      rgb_led_set(LED_LT_GREEN,0);
    }
  }
  //initialize the wifi_rx buffer and flag
  wifi_rx_buf_full = false;
  memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE);
  while (1) {
    //***** SYS TICK ACTIONS ******
    if(sys_tick!=prev_tick){
      //check if there is a valid wemo sample
      if(wemo_sample.valid==true && wemo_config.collect_data){
	core_log_power_data(&wemo_sample);
      }
      wemo_read_power();
      wdt_restart(WDT);
      prev_tick = sys_tick;
    }
    //check for pending data from the Internet
    if(wifi_rx_buf_full){
      core_process_wifi_data();
      wifi_rx_buf_full=false;
    }
    //see if we have any commands to run
    if(cmd_buf_full){
      runcmd(cmd_buf); //  run it
      //clear the buffer
      cmd_buf_idx = 0;
      memset(cmd_buf,0x0,CMD_BUF_SIZE);
      if(wemo_config.echo)
	printf("\r> "); //print the prompt
      cmd_buf_full=false;
    }
  }
    
}
Beispiel #14
0
void core_process_wifi_data(void){
  int BUF_SIZE=XL_BUF_SIZE;
  char *buf;
  int chan_num, data_size, r;
  unsigned int red,green,blue, blink;
  unsigned int yr,mo,dt,dw,hr,mn,sc;
  int time_buf_size =  MD_BUF_SIZE;
  char *time_buf;

  //match against the data
  if(strlen(wifi_rx_buf)>BUF_SIZE){
    printf("can't process rx'd packet, too large\n");
    core_log("can't process rx'd packet, too large");
    return;
  }
  //allocate memory
  buf = core_malloc(BUF_SIZE);
  r=sscanf(wifi_rx_buf,"\r\n+IPD,%d,%d:%s", &chan_num, &data_size, buf);
  if(r!=3){
    printf("rx'd corrupt data, ignoring\n");
    core_log("rx'd corrupt data, ignoring\n");
    //free memory
    core_free(buf);
    return;
  }

  if(wemo_config.debug_level>DEBUG_INFO)
    printf("Got [%d] bytes on channel [%d]\n",
	   data_size, chan_num);
  //discard responses from the NILM to power logging packets, but keep the response
  //if another core function has requested some data, this is done with the callback 
  //function. The requesting core function registers a callback and this function calls
  //it and then resets the callback to NULL
  if(chan_num==4){
    //close the socket
    wifi_send_cmd("AT+CIPCLOSE=4","Unlink",buf,BUF_SIZE,1);
    //execute the callback
    if(tx_callback!=NULL){
      (*tx_callback)(wifi_rx_buf);
      tx_callback=NULL;
    }
    //clear the server buffer
    memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE);
    //free memory
    core_free(buf);
    return;
  }
  ///////////////////
  //this data must be inbound to the server port, process the command
  //
  //     RELAY ON
  if(strcmp(buf,"relay_on")==0){
    gpio_set_pin_high(RELAY_PIN);
    printf("relay ON\n");
    //return "OK" to indicate success
    wifi_send_txt(0,"OK");
  }
  //     RELAY OFF
  else if(strcmp(buf,"relay_off")==0){
    gpio_set_pin_low(RELAY_PIN);
    printf("relay OFF\n");
    //return "OK" to indicate success
    wifi_send_txt(0,"OK");
  }
  //     LED SET
  else if(strstr(buf,"set_led")==buf){
    if(sscanf(buf,"set_led_%u_%u_%u_%u.",&red,&green,&blue,&blink)!=4){
      core_log("corrupt led_set request");
    } else {
      rgb_led_set(red,green,blue,blink);
      if(wemo_config.echo)
	printf("set led: [%u, %u, %u, %u]\n",red,green,blue,blink);
      wifi_send_txt(0,"OK");
    }
  }
  //     RTC SET
  else if(strstr(buf,"set_rtc")==buf){
    if(sscanf(buf,"set_rtc_%u_%u_%u_%u_%u_%u_%u.",
	      &yr,&mo,&dt,&dw,&hr,&mn,&sc)!=7){
      core_log("corrupt rtc_set request");
    } else {
      if(i2c_rtc_set_time(sc,mn,hr,dw,dt,mo,yr)!=0)
	printf("error setting RTC\n");
      else{
	time_buf = membag_alloc(time_buf_size);
	if(time_buf==NULL)
	  core_panic();
	rtc_get_time_str(time_buf,time_buf_size);
	if(wemo_config.echo)
	  printf("wifi set rtc to: %s\n",time_buf);
	core_log("wifi set rtc");
	membag_free(time_buf);
	wifi_send_txt(0,"OK");
      }
    }
  }
  //     SEND DATA
  else if(strcmp(buf,"send_data")==0){
    if(tx_pkt->status!=POWER_PKT_READY){
      r = wifi_send_txt(chan_num,"error: no data");
      if(r==TX_ERR_MODULE_RESET)
	while(wifi_init()!=0); //fail!!! anger!!!! reset the module
    } else {
      //send the data
      r=wifi_send_raw(chan_num,(uint8_t*)tx_pkt,sizeof(*tx_pkt));
      if(r==TX_ERR_MODULE_RESET){
	while(wifi_init()!=0); //fail!! anger!!! reset the module
      } else {
	//clear out the packet so we can start again
	memset(tx_pkt,0,sizeof(*tx_pkt));
	tx_pkt->status=POWER_PKT_EMPTY;
	if(wemo_config.debug_level>=DEBUG_INFO)
	  printf("sent data\n");
      }
    }
  }
  else{
    printf("unknown command: %s\n",buf);
    wifi_send_txt(chan_num,"error: unknown command");
    //free memory
    core_free(buf);
    return;
  }
  //clear the server buffer
  memset(wifi_rx_buf,0x0,WIFI_RX_BUF_SIZE);
  //free the memory
  core_free(buf);
  return;
}
Beispiel #15
0
void isr_hard_fault(void)
{
    core_panic(PANIC_HARD_FAULT, "HARD FAULT");
    while (1) {asm ("nop");}
}
Beispiel #16
0
void isr_bus_fault(void)
{
    core_panic(PANIC_BUS_FAULT, "BUS FAULT");
    while (1) {asm ("nop");}
}
Beispiel #17
0
void isr_nmi(void)
{
    core_panic(PANIC_NMI_HANDLER, "NMI HANDLER");
}
Beispiel #18
0
/**
 * @brief Default handler is called in case no interrupt handler was defined
 */
void dummy_handler(void)
{
    core_panic(PANIC_DUMMY_HANDLER, "DUMMY HANDLER");
}
Beispiel #19
0
void isr_hard_fault(void)
{
    core_panic(PANIC_HARD_FAULT, "HARD FAULT");
}
Beispiel #20
0
void isr_mem_manage(void)
{
    core_panic(PANIC_MEM_MANAGE, "MEM MANAGE HANDLER");
}
Beispiel #21
0
void isr_usage_fault(void)
{
    core_panic(USAGE_FAULT, "USAGE FAULT");
}