Exemplo n.º 1
0
/**
 * \brief Receive the files through XMODEM protocol
 *
 * \param usart  Base address of the USART instance.
 * \param p_buffer  Pointer to receive buffer
 *
 * \return received file size
 */
uint32_t xmodem_receive_file(usart_if usart, int8_t *p_buffer)
{
	uint32_t ul_timeout;
	uint8_t c_char;
	int32_t l_done;
	uint8_t uc_sno = 0x01;
	uint32_t ul_size = 0;

	/* Wait and put 'C' till start XMODEM transfer */
	while (1) {
		usart_serial_putchar(usart, 'C');
		ul_timeout = (sysclk_get_peripheral_hz() / 10);
		while (!(usart_serial_is_rx_ready(usart)) && ul_timeout) {
			ul_timeout--;
		}
		if (usart_serial_is_rx_ready(usart)) {
			break;
		}
	}

	/* Begin to receive the data */
	l_done = 0;
	while (l_done == 0) {
		usart_serial_getchar(usart, &c_char);

		switch (c_char) {
		/* Start of transfer */
		case XMDM_SOH:
			l_done = xmodem_get_packet(usart, p_buffer+ul_size, uc_sno);
			if (l_done == 0) {
				uc_sno++;
				ul_size += PKTLEN_128;
			}
			usart_serial_putchar(usart, XMDM_ACK);
			break;

		/* End of transfer */
		case XMDM_EOT:
			usart_serial_putchar(usart, XMDM_ACK);
			l_done = ul_size;
			break;

		case XMDM_CAN:
		case XMDM_ESC:
		default:
			l_done = -1;
			break;
		}
	}
	return ul_size;
}
Exemplo n.º 2
0
int wifi_init(void){
  uint32_t BUF_SIZE = MD_BUF_SIZE;
  uint8_t tmp;  //dummy var for flushing UART
  int r; //return value from wifi_send_cmd (length of resp)
  char *buf;
  char *tx_buf;

  //allocate static buffers if they are not NULL
  if(resp_buf==NULL){
    resp_buf=core_malloc(RESP_BUF_SIZE);
  }
  if(resp_complete_buf==NULL){
    resp_complete_buf=core_malloc(RESP_COMPLETE_BUF_SIZE);
  }
  if(wifi_rx_buf==NULL){
    wifi_rx_buf = core_malloc(WIFI_RX_BUF_SIZE);
  }
  //if we are standalone, don't do anything
  if(wemo_config.standalone){
    printf("warning: wifi_init called in standalone mode\n");
    return 0; 
  }
  //initialize the memory
  buf = core_malloc(BUF_SIZE);
  tx_buf = core_malloc(BUF_SIZE);

  //set up the UART
  static usart_serial_options_t usart_options = {
    .baudrate = WIFI_UART_BAUDRATE,
    .charlength = WIFI_UART_CHAR_LENGTH,
    .paritytype = WIFI_UART_PARITY,
    .stopbits = WIFI_UART_STOP_BITS
  };
  gpio_configure_pin(PIO_PA9_IDX, (PIO_PERIPH_A | PIO_DEFAULT));
  gpio_configure_pin(PIO_PA10_IDX, (PIO_PERIPH_A | PIO_DEFAULT));
  pmc_enable_periph_clk(ID_WIFI_UART);
  sysclk_enable_peripheral_clock(ID_WIFI_UART);
  usart_serial_init(WIFI_UART,&usart_options);
  //flush any existing data
  while(usart_serial_is_rx_ready(WIFI_UART)){
    usart_serial_getchar(WIFI_UART,&tmp);
  }
  // Trigger from timer 0
  pmc_enable_periph_clk(ID_TC0);
  tc_init(TC0, 0,                        // channel 0
	  TC_CMR_TCCLKS_TIMER_CLOCK5     // source clock (CLOCK5 = Slow Clock)
	  | TC_CMR_CPCTRG                // up mode with automatic reset on RC match
	  | TC_CMR_WAVE                  // waveform mode
	  | TC_CMR_ACPA_CLEAR            // RA compare effect: clear
	  | TC_CMR_ACPC_SET              // RC compare effect: set
	  );
  TC0->TC_CHANNEL[0].TC_RA = 0;  // doesn't matter
  TC0->TC_CHANNEL[0].TC_RC = 64000;  // sets frequency: 32kHz/32000 = 1 Hz
  NVIC_ClearPendingIRQ(TC0_IRQn);
  NVIC_SetPriority(TC0_IRQn,1);//high priority
  NVIC_EnableIRQ(TC0_IRQn);
  tc_enable_interrupt(TC0, 0, TC_IER_CPCS);
  //reset the module
  if(wifi_send_cmd("AT+RST","ready",buf,BUF_SIZE,8)==0){
    printf("Error reseting ESP8266\n");
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //set to mode STA
  if(wifi_send_cmd("AT+CWMODE=1","OK",buf,BUF_SIZE,8)==0){
    printf("Error setting ESP8266 mode\n");
    core_free(buf);
    core_free(tx_buf);
    return 0;
  }
  //try to join the specified network  
  snprintf(tx_buf,BUF_SIZE,"AT+CWJAP=\"%s\",\"%s\"",
	   wemo_config.wifi_ssid,wemo_config.wifi_pwd);
  if((r=wifi_send_cmd(tx_buf,"OK",buf,BUF_SIZE,10))==0){
    printf("no response to CWJAP\n");
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //check for errors
  if( (r<1) || (strcmp(&buf[r-1],"OK")!=0)){
    snprintf(tx_buf,BUF_SIZE,"failed to join network [%s]: [%s]\n",wemo_config.wifi_ssid, buf);
    printf(tx_buf);
    core_log(tx_buf);
    //free memory
    core_free(buf);
    core_free(tx_buf);
    return -1;
  }
  //see if we have an IP address
  wifi_send_cmd("AT+CIFSR","OK",buf,BUF_SIZE,5);
  if(strstr(buf,"ERROR")==buf){
    printf("error getting IP address\n");
    //free the memory
    core_free(tx_buf);
    core_free(buf);
    return -1;
  }
  //try to parse the response into an IP address
  //expect 4 octets but *not* 0.0.0.0
  int a1,a2,a3,a4;
  if(!(sscanf(buf,"+CIFSR:STAIP,\"%d.%d.%d.%d\"",&a1,&a2,&a3,&a4)==4 && a1!=0)){
    printf("error, bad address: %s\n",buf);
    //free the memory
    core_free(tx_buf);
    core_free(buf);
    return -1;
  }
  //save the IP to our config
  snprintf(buf,BUF_SIZE,"%d.%d.%d.%d",a1,a2,a3,a4);
  memset(wemo_config.ip_addr,0x0,MAX_CONFIG_LEN);
  strcpy(wemo_config.ip_addr,buf);
  //set the mode to multiple connection
  wifi_send_cmd("AT+CIPMUX=1","OK",buf,BUF_SIZE,2);
  //start a server on port 1336
  wifi_send_cmd("AT+CIPSERVER=1,1336","OK",buf,BUF_SIZE,2);

  //if we know the NILM IP address, send it our IP
  if(strlen(wemo_config.nilm_ip_addr)!=0){
    if(wifi_send_ip()==TX_ERR_MODULE_RESET){
      return TX_ERR_MODULE_RESET;
    }
  } 
  else {
     //get the NILM IP address from the manager
     //once we know the NILM address we send it ours
     core_get_nilm_ip_addr();
  }

  //log the event
  snprintf(buf,BUF_SIZE,"Joined [%s] with IP [%s]",
	   wemo_config.wifi_ssid,wemo_config.ip_addr);
  printf("\n%s\n",buf);
  core_log(buf);
  //free the memory
  core_free(tx_buf);
  core_free(buf);
  return 0;
}
Exemplo n.º 3
0
/**
 * Check if there is console input
 * \return true if there is input
 */
bool dbg_usart_rx_ready(void)
{
	return usart_serial_is_rx_ready(DBG_USART_BASE);
}