示例#1
0
文件: acc-sl.c 项目: aoboy/acc-slight
/**
 * @brief transmit_epidemic
 */
static void transmit_epidemic(){

    packetbuf_clear();
    
    data_packet_t *dpkt = (data_packet_t*)packetbuf_dataptr();
    
    if(isAnchorFlag){
        dpkt->type = ANCHOR_PKT;
    }else{
        dpkt->type = PROBE_PKT;
    }
    //add sender offset.
    dpkt->offset   = probe_offset;
    dpkt->period   = get_node_period();
    dpkt->src_id   = rimeaddr_node_addr.u8[0];
    
    uint8_t pldSize = 0;
    if(radio_read_flag == 0){
        if(list_access_flag == 0){
            list_access_flag = 1;
            pldSize = neighs_add2payload(&dpkt->data[0], isAnchorFlag, probe_offset);
            list_access_flag = 0;
        }

        //if(pldSize){

            rtimer_clock_t t0;
            uint8_t i = 0, pkt_seen = 0, ccaCounter= 0;

            //ccaCounter= randomint_between(CCA_COUNT_MAX, CCA_COUNT_MAX_TX);

	    ccaCounter = /*CCA_COUNT_MAX +*/ random_int(CCA_COUNT_MAX_TX);
	    //watchdog
	    watchdog_periodic();
	     
            for( i = 0; i < ccaCounter && beacon_2_flag; i++){

		//watchdog
		watchdog_periodic();	
		
		
                t0 = RTIMER_NOW();

                while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + CCA_CHECK_TIME)){
                    //do nothing.. KUNANGA
                }

                if(NETSTACK_RADIO.channel_clear() == 0){
                    pkt_seen = 1;
                    break; // maybe return here .. collision is expected
                }

            }

            if((pkt_seen == 0) && (radio_read_flag == 0)){
                NETSTACK_RADIO.send((void*)packetbuf_dataptr(),DATAPKT_HDR_LEN + pldSize);
            }
        //} //end of pldSize
    }
}
示例#2
0
/*---------------------------------------------------------------------------*/
void
uart1_writeb(unsigned char c)
{
  watchdog_periodic();
#if TX_WITH_INTERRUPT

  /* Put the outgoing byte on the transmission buffer. If the buffer
     is full, we just keep on trying to put the byte into the buffer
     until it is possible to put it there. */
  while(ringbuf_put(&txbuf, c) == 0);

  /* If there is no transmission going, we need to start it by putting
     the first byte into the UART. */
  if(transmitting == 0) {
    transmitting = 1;
    UCA0TXBUF = ringbuf_get(&txbuf);
  }

#else /* TX_WITH_INTERRUPT */

 /* Loop until the transmission buffer is available. */
  while(!(IFG2 & UCA0TXIFG));    // Wait for TX buffer ready to receive new byte
        UCA0TXBUF = c; //c;            // Output character
#endif /* TX_WITH_INTERRUPT */
}
示例#3
0
void
AppColdStart(void)
{
  /* default startup */
  init_hardware();
  process_init();
  procinit_init();
  init_net();
  jts_init();

  /* application startup */
  autostart_start(autostart_processes);

  /* enable watchdog on JN5148, there is none on JN5139 */
#ifdef __BA2__
  watchdog_start();
#endif

  /* default main loop */
  while(1)
  {
    process_run();
    etimer_request_poll();

#ifdef __BA2__
    watchdog_periodic();
#endif
  }
}
/**
 * \brief This function is called by the 6lowpan code to send out a
 * packet.
 * \param dest the link layer destination address of the packet
 */
static void
send_packet(struct net_buf *buf, linkaddr_t *dest, bool last_fragment, void *ptr)
{
  /* Set the link layer destination address for the packet as a
   * packetbuf attribute. The MAC layer can access the destination
   * address with the function packetbuf_addr(PACKETBUF_ADDR_RECEIVER).
   */
  packetbuf_set_addr(buf, PACKETBUF_ADDR_RECEIVER, dest);

#if NETSTACK_CONF_BRIDGE_MODE
  /* This needs to be explicitly set here for bridge mode to work */
  packetbuf_set_addr(buf, PACKETBUF_ADDR_SENDER,(void*)&uip_lladdr);
#endif

  /* Force acknowledge from sender (test hardware autoacks) */
#if SICSLOWPAN_CONF_ACK_ALL
  packetbuf_set_attr(buf, PACKETBUF_ATTR_RELIABLE, 1);
#endif

  /* Provide a callback function to receive the result of
     a packet transmission. */
  NETSTACK_LLSEC.send(buf, &packet_sent, last_fragment, ptr);

  /* If we are sending multiple packets in a row, we need to let the
     watchdog know that we are still alive. */
  watchdog_periodic();
}
/*---------------------------------------------------------------------------*/
static cfs_offset_t
file_end(coffee_page_t start)
{
    struct file_header hdr;
    unsigned char buf[COFFEE_PAGE_SIZE];
    coffee_page_t page;
    int i;

    read_header(&hdr, start);

    /*
     * Move from the end of the range towards the beginning and look for
     * a byte that has been modified.
     *
     * An important implication of this is that if the last written bytes
     * are zeroes, then these are skipped from the calculation.
     */

    for(page = hdr.max_pages - 1; page >= 0; page--) {
        watchdog_periodic();
        COFFEE_READ(buf, sizeof(buf), (start + page) * COFFEE_PAGE_SIZE);
        for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
            if(buf[i] != 0) {
                if(page == 0 && i < sizeof(hdr)) {
                    return 0;
                }
                return 1 + i + (page * COFFEE_PAGE_SIZE) - sizeof(hdr);
            }
        }
    }

    /* All bytes are writable. */
    return 0;
}
示例#6
0
/*---------------------------------Main Routine----------------------------*/
int
main(void)
{
  /* GCC depends on register r1 set to 0 (?) */
  asm volatile ("clr r1");
  
  /* Initialize in a subroutine to maximize stack space */
  initialize();

#if DEBUG
{struct process *p;
 for(p = PROCESS_LIST();p != NULL; p = ((struct process *)p->next)) {
  PRINTA("Process=%p Thread=%p  Name=\"%s\" \n",p,p->thread,PROCESS_NAME_STRING(p));
 }
}
#endif

  while(1) {
    process_run();

    watchdog_periodic();

  }
  return 0;
}
示例#7
0
/*---------------------------------------------------------------------------*/
static int
erase(unsigned long from, unsigned long to)
{
  enum status_code ret;
  unsigned long addr;

  if (!opened) {
    return -1;
  }

  from = (from / SECTOR_SIZE) * SECTOR_SIZE;
  to   = (to   / SECTOR_SIZE) * SECTOR_SIZE;

  for(addr = from; addr < to; addr += SECTOR_SIZE) {
    TRACE("addr: %ld", addr);
    if ((ret = nvm_erase_row(addr)) != STATUS_OK) {
      WARN("nvm_erase_row error: %d", ret);
      return -1;
    }
    watchdog_periodic();
  }

  while (!nvm_is_ready());

  return 1;
}
示例#8
0
/*---------------------------------------------------------------------------*/
static int32_t
find_file(const char *name)
{
  struct file_header hdr;
  int32_t page;

  page = dir_cache_find(name);
  if(page >= 0) {
    return page;
  }
  
  page = 0;
  do {
    READ_HEADER(&hdr, page);
    if(COFFEE_PAGE_ACTIVE(hdr)) {
      if(strcmp(name, hdr.name) == 0) {
	dir_cache_add(name[0], page);
	return page;
      }
      page += hdr.max_pages;
    } else if(COFFEE_PAGE_ISOLATED(hdr)) {
      ++page;
    } else if(COFFEE_PAGE_OBSOLETE(hdr)) {
      page += hdr.max_pages;
    } else {
      /* It follows from the properties of the page allocation algorithm 
	 that if a free page is encountered, then the rest of the sector
	 is also free. */
      page = (page + COFFEE_PAGES_PER_SECTOR) & ~(COFFEE_PAGES_PER_SECTOR - 1);
    }
    watchdog_periodic();
  } while(page < COFFEE_PAGE_COUNT);
  
  return -1;
}
示例#9
0
/*---------------------------------------------------------------------------*/
void
uart1_writeb(unsigned char c)
{
  watchdog_periodic();
#if TX_WITH_INTERRUPT

  /* Put the outgoing byte on the transmission buffer. If the buffer
     is full, we just keep on trying to put the byte into the buffer
     until it is possible to put it there. */
  while(ringbuf_put(&txbuf, c) == 0);

  /* If there is no transmission going, we need to start it by putting
     the first byte into the UART. */
  if(transmitting == 0) {
    transmitting = 1;

    /* Loop until the transmission buffer is available. */
    /*while((IFG2 & UTXIFG1) == 0);*/
    TXBUF1 = ringbuf_get(&txbuf);
  }

#else /* TX_WITH_INTERRUPT */

  /* Loop until the transmission buffer is available. */
  while((IFG2 & UTXIFG1) == 0);

  /* Transmit the data. */
  TXBUF1 = c;
#endif /* TX_WITH_INTERRUPT */
}
示例#10
0
文件: bootloader.c 项目: 1uk3/contiki
void
Jump_To_Bootloader(void)
{
  /* Disable all interrupts */
  cli();

#ifdef UDCON
  /* If USB is used, detach from the bus */
  Usb_detach();

  uint8_t i;

  /* Wait two seconds for the USB detachment to register on the host */
  for(i = 0; i < 200; i++) {
    _delay_ms(10);
    watchdog_periodic();
  }
#endif

  /* Set the bootloader key to the magic value and force a reset */
  Boot_Key = MAGIC_BOOT_KEY;

  eeprom_write_byte(EEPROM_MAGIC_BYTE_ADDR, 0xFF);

  /* Enable interrupts */
  sei();

  watchdog_reboot();
}
示例#11
0
文件: uart1.c 项目: 1uk3/contiki
/*---------------------------------------------------------------------------*/
void
uart1_writeb(unsigned char c)
{
  watchdog_periodic();
#if TX_WITH_INTERRUPT
  /*
   * Put the outgoing byte on the transmission buffer. If the buffer
   * is full, we just keep on trying to put the byte into the buffer
   * until it is possible to put it there.
   */
  while(ringbuf_put(&txbuf, c) == 0);

  /*
   * If there is no transmission going, we need to start it by putting
   * the first byte into the UART.
   */
  if(transmitting == 0) {
    transmitting = 1;
    SC1_DATA = ringbuf_get(&txbuf);
    INT_SC1FLAG = INT_SCTXFREE;
    INT_SC1CFG |= INT_SCTXFREE;
  }
#else /* TX_WITH_INTERRUPT */

  /* Loop until the transmission buffer is available. */
  while((INT_SC1FLAG & INT_SCTXFREE) == 0);

  /* Transmit the data. */
  SC1_DATA = c;

  INT_SC1FLAG = INT_SCTXFREE;
#endif /* TX_WITH_INTERRUPT */
}
示例#12
0
/*---------------------------------------------------------------------------*/
void
sci3_writeb(unsigned char c)
{
  watchdog_periodic();
#if 1

  /* Put the outgoing byte on the transmission buffer. If the buffer
     is full, we just keep on trying to put the byte into the buffer
     until it is possible to put it there. */
  while(ringbuf_put(&txbuf, c) == 0);

  /* If there is no transmission going, we need to start it by putting
     the first byte into the UART. */
  if(transmitting == 0) {
    transmitting = 1;

    /* Loop until the transmission buffer is available. */
    /*while((IFG2 & UTXIFG1) == 0);*/
    //TXBUF1 = ringbuf_get(&txbuf);

    SCI3.SCR3.BYTE |= 0x04; /*  */
    SCI3.SSR.BYTE &= ~0x04; /* reset TxEI request */
  }

#else /* TX_WITH_INTERRUPT */

  /* Loop until the transmission buffer is available. */
  while (!(SCI3.SSR.BIT.TDRE));

  /* Transmit the data. */
  SCI3.TDR = c;
  SCI3.SSR.BIT.TDRE = 0;
#endif /* TX_WITH_INTERRUPT */
}
示例#13
0
/*---------------------------------------------------------------------------*/
static void
send_broadcast(mac_callback_t sent, void *ptr)
{
    struct queuebuf *qb;

    qb = queuebuf_new_from_packetbuf();
    if(!qb || (NETSTACK_FRAMER.create() < 0)) {
        PRINTF("coresec-strategy: Did not send broadcast\n");
        if(qb) {
            queuebuf_free(qb);
        }
        sent(ptr, MAC_TX_ERR, 0);
        return;
    }

    prepare_announce();
    adaptivesec_send_command_frame();
    watchdog_periodic();

    queuebuf_to_packetbuf(qb);
    queuebuf_free(qb);
#if WITH_BROADCAST_ENCRYPTION
    {
        uint8_t ignore[ADAPTIVESEC_BROADCAST_MIC_LEN];

        if(packetbuf_attr(PACKETBUF_ATTR_SECURITY_LEVEL) & (1 << 2)) {
            adaptivesec_aead(adaptivesec_group_key, 1, ignore, 1);
        }
    }
#endif /* WITH_BROADCAST_ENCRYPTION */
    NETSTACK_MAC.send(sent, ptr);
}
示例#14
0
void AppColdStart(void)
{
  /* default startup */
  init_hardware();

  buzzer_init();
  rgb_leds_init();
  sseg_init();

  process_init();
  init_net();

  /* start the main processes */
  procinit_init();
  autostart_start(autostart_processes);
  jts_init();

  /* enable watchdog on JN5148, there is none on JN5139 */
  watchdog_start();

  /* default main loop */
  while(1)
  {
    process_run();
    etimer_request_poll();
    watchdog_periodic();
  }
}
示例#15
0
uint16_t aes_encrypt(uint8_t * output, const uint8_t * data, uint16_t size) {
	static uint16_t i;
	static uint8_t *current_iv;
	watchdog_periodic();
	current_iv = output+(size+AES_BLOCK_SIZE-(size-1)%AES_BLOCK_SIZE-1);

#if AES_ALWAYS_SET_KEY
	ENCCS = AES_DEFAULT_MODE | AES_CMD_LOAD_KEY | AES_START;
	AES_DOWNLOAD(current_key, AES_KEY_SIZE);
	AES_WAIT();
#endif

#if AES_USE_MAC
	// load MAC with IV 0
	ENCCS = AES_CBCMAC_MODE | AES_CMD_LOAD_IV | AES_START;
	AES_DOWNLOAD(zero, AES_BLOCK_SIZE);
	AES_WAIT();
	// load data
	ENCCS = AES_CBCMAC_MODE | AES_CMD_LOAD_IV;
	for( i = 0 ; i < size-AES_BLOCK_SIZE ; i += AES_BLOCK_SIZE ) {
		ENCCS |= AES_START;
		AES_DOWNLOAD(data+i,AES_BLOCK_SIZE);
		AES_WAIT();
	}
	ENCCS = AES_CBC_MODE | AES_CMD_LOAD_IV | AES_START;
	AES_DOWNLOAD(data+i,size-i);
	AES_DOWNLOAD(zero,AES_BLOCK_SIZE-(size-i));
	AES_WAIT();
	AES_UPLOAD(current_iv+AES_BLOCK_SIZE,AES_BLOCK_SIZE);
#endif

	// first byte of IV stores how many bytes is necessary to complete the last block
	current_iv[0] = (current_iv-output)-size;
	for( i = 1 ; i < AES_BLOCK_SIZE ; ++i ) current_iv[i] = random_rand();

	ENCCS = AES_DEFAULT_MODE | AES_CMD_LOAD_IV | AES_START;
	AES_DOWNLOAD(current_iv, AES_BLOCK_SIZE);
	AES_WAIT();
	// load data
	ENCCS = AES_DEFAULT_MODE | AES_CMD_ENCRYPT;
	for( i = 0 ; i < size-AES_BLOCK_SIZE ; i += AES_BLOCK_SIZE ) {
		ENCCS |= AES_START;
		AES_DOWNLOAD(data+i,AES_BLOCK_SIZE);
		AES_WAIT();
		AES_UPLOAD(output+i,AES_BLOCK_SIZE);
	}
	// last block
	ENCCS |= AES_START;
	AES_DOWNLOAD(data+i,size-i);
	AES_DOWNLOAD(zero,AES_BLOCK_SIZE-(size-i));
	AES_WAIT();
	AES_UPLOAD(output+i,AES_BLOCK_SIZE);

	size += current_iv[0] + AES_BLOCK_SIZE;
#if AES_USE_MAC
	size += AES_BLOCK_SIZE;
#endif
	return size;
}
示例#16
0
uint16_t aes_decrypt(uint8_t * output, const uint8_t * data, uint16_t size) {
	static uint16_t i;
	static uint8_t *current_iv;
	watchdog_periodic();

#if AES_ALWAYS_SET_KEY
	ENCCS = AES_DEFAULT_MODE | AES_CMD_LOAD_KEY | AES_START;
	AES_DOWNLOAD(current_key, AES_KEY_SIZE);
	AES_WAIT();
#endif

#if AES_USE_MAC
	size -= 2*AES_BLOCK_SIZE;
#else
	size -= AES_BLOCK_SIZE;
#endif

	current_iv = data+size;
	ENCCS = AES_DEFAULT_MODE | AES_CMD_LOAD_IV | AES_START;
	AES_DOWNLOAD(current_iv, AES_BLOCK_SIZE);
	AES_WAIT();

	ENCCS = AES_DEFAULT_MODE | AES_CMD_DECRYPT;
	for( i = 0 ; i < size-AES_BLOCK_SIZE ; i += AES_BLOCK_SIZE ) {
		ENCCS |= AES_START;
		AES_DOWNLOAD(data+i,AES_BLOCK_SIZE);
		AES_WAIT();
		AES_UPLOAD(output+i,AES_BLOCK_SIZE);
	}
	// last block
	ENCCS |= AES_START;
	AES_DOWNLOAD(data+i,AES_BLOCK_SIZE);
	AES_WAIT();
	AES_UPLOAD(output+i,size-i);

#if AES_USE_MAC
	// load MAC with IV 0
	ENCCS = AES_CBCMAC_MODE | AES_CMD_LOAD_IV | AES_START;
	AES_DOWNLOAD(zero, AES_BLOCK_SIZE);
	AES_WAIT();
	// load data
	ENCCS = AES_CBCMAC_MODE | AES_CMD_LOAD_IV;
	for( i = 0 ; i < size-AES_BLOCK_SIZE ; i += AES_BLOCK_SIZE ) {
		ENCCS |= AES_START;
		AES_DOWNLOAD(output+i,AES_BLOCK_SIZE);
		AES_WAIT();
	}
	ENCCS = AES_CBC_MODE | AES_CMD_LOAD_IV | AES_START;
	AES_DOWNLOAD(output+i,size-i);
	AES_DOWNLOAD(zero,AES_BLOCK_SIZE-(size-i));
	AES_WAIT();
	for( i = 0 ; i < AES_BLOCK_SIZE ; ++i ) {
		uint8_t tmp = ENCDO;
		if( (current_iv+AES_BLOCK_SIZE)[i] != tmp ) return -1; // 0xffff
	}
#endif

	return size;
}
示例#17
0
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(myTimer_process, ev, data)
{
  static rtimer_clock_t t0;

  PROCESS_BEGIN();

  printf("I start the timer (1 s.)\n");
  while(1) {
    watchdog_periodic();
    t0 = RTIMER_NOW();
    while(RTIMER_CLOCK_LT(RTIMER_NOW(), t0 + RTIMER_SECOND));
    watchdog_periodic();
    timeout_handler();
  }

  PROCESS_END();
}
示例#18
0
/*---------------------------------------------------------------------------*/
void
cfs_coffee_arch_erase(uint16_t sector)
{
  watchdog_periodic();
  INTERRUPTS_DISABLE();
  rom_util_page_erase(COFFEE_START + sector * COFFEE_SECTOR_SIZE,
                      COFFEE_SECTOR_SIZE);
  INTERRUPTS_ENABLE();
}
示例#19
0
/*---------------------------------------------------------------------------*/
void
cfs_coffee_arch_write(const void *buf, unsigned int size, cfs_offset_t offset)
{
  const uint32_t *src = buf;
  uint32_t flash_addr = COFFEE_START + offset;
  unsigned int align;
  uint32_t word, len;
  uint32_t page_buf[COFFEE_PAGE_SIZE / FLASH_WORD_SIZE];
  unsigned int i;

  if(size && (align = flash_addr & (FLASH_WORD_SIZE - 1))) {
    len = MIN(FLASH_WORD_SIZE - align, size);
    word = ~((*src & ((1 << (len << 3)) - 1)) << (align << 3));
    watchdog_periodic();
    INTERRUPTS_DISABLE();
    rom_util_program_flash(&word, flash_addr & ~(FLASH_WORD_SIZE - 1),
                           FLASH_WORD_SIZE);
    INTERRUPTS_ENABLE();
    *(const uint8_t **)&src += len;
    size -= len;
    flash_addr += len;
  }

  while(size >= FLASH_WORD_SIZE) {
    len = MIN(size & ~(FLASH_WORD_SIZE - 1), COFFEE_PAGE_SIZE);
    for(i = 0; i < len / FLASH_WORD_SIZE; i++) {
      page_buf[i] = ~*src++;
    }
    watchdog_periodic();
    INTERRUPTS_DISABLE();
    rom_util_program_flash(page_buf, flash_addr, len);
    INTERRUPTS_ENABLE();
    size -= len;
    flash_addr += len;
  }

  if(size) {
    word = ~(*src & ((1 << (size << 3)) - 1));
    watchdog_periodic();
    INTERRUPTS_DISABLE();
    rom_util_program_flash(&word, flash_addr, FLASH_WORD_SIZE);
    INTERRUPTS_ENABLE();
  }
}
示例#20
0
文件: uart1x.c 项目: 1uk3/contiki
/*---------------------------------------------------------------------------*/
void
uart1_writeb(unsigned char c)
{
  watchdog_periodic();
  /* Loop until the transmission buffer is available. */
  while((UCA1STAT & UCBUSY));

  /* Transmit the data. */
  UCA1TXBUF = c;
}
示例#21
0
/*
 * Wait for a write/erase operation to finish.
 */
static unsigned
wait_ready(void)
{
  unsigned u;
  do {
    u = read_status_register();
    watchdog_periodic();
  } while(u & 0x01);		/* WIP=1, write in progress */
  return u;
}
示例#22
0
/*---------------------------------------------------------------------------*/
void
cfs_coffee_arch_read(void *buf, unsigned int size, cfs_offset_t offset)
{
  const uint8_t *src;
  uint8_t *dst;

  watchdog_periodic();
  for(src = (const void *)(COFFEE_START + offset), dst = buf; size; size--) {
    *dst++ = ~*src++;
  }
}
/*---------------------------------------------------------------------------*/
int
cfs_read(int fd, void *buf, unsigned size)
{
    struct file_header hdr;
    struct file_desc *fdp;
    struct file *file;
    unsigned bytes_left;
    int r;
#if COFFEE_MICRO_LOGS
    struct log_param lp;
#endif

    if(!(FD_VALID(fd) && FD_READABLE(fd))) {
        return -1;
    }

    fdp = &coffee_fd_set[fd];
    file = fdp->file;
    if(fdp->offset + size > file->end) {
        size = file->end - fdp->offset;
    }

    bytes_left = size;
    if(FILE_MODIFIED(file)) {
        read_header(&hdr, file->page);
    }

    /*
     * Fill the buffer by copying from the log in first hand, or the
     * ordinary file if the page has no log record.
     */
    while(bytes_left) {
        watchdog_periodic();
        r = -1;
#if COFFEE_MICRO_LOGS
        if(FILE_MODIFIED(file)) {
            lp.offset = fdp->offset;
            lp.buf = buf;
            lp.size = bytes_left;
            r = read_log_page(&hdr, file->record_count, &lp);
        }
#endif /* COFFEE_MICRO_LOGS */
        /* Read from the original file if we cannot find the data in the log. */
        if(r < 0) {
            r = bytes_left;
            COFFEE_READ(buf, r, absolute_offset(file->page, fdp->offset));
        }
        bytes_left -= r;
        fdp->offset += r;
        buf += r;
    }
    return size;
}
示例#24
0
/*
 * Probe I2C bus for PMS5003 device 
 */
uint8_t
pms5003_i2c_probe(void) {
  watchdog_periodic();
  if(!i2c_start(I2C_PMS5003_ADDR)) {
    i2c_stop();
    i2c_probed |= I2C_PMS5003;
    return 1;

  }
  i2c_probed &= ~I2C_PMS5003;
  return 0;
}
示例#25
0
/*---------------------------------------------------------------------------*/
static int32_t
find_offset_in_file(int first_page)
{
  struct file_header hdr;
  unsigned char buf[COFFEE_PAGE_SIZE];
  int page;
  int i;
  int search_limit;
  uint32_t range_start, range_end, part_size;

  READ_HEADER(&hdr, first_page);
  for(search_limit = i = 0; i < sizeof(hdr.eof_hint) * CHAR_BIT; i++) {
    if(hdr.eof_hint >> i) {
      search_limit = i + 1;
    }
  }
  part_size = hdr.max_pages / sizeof(hdr.eof_hint) / CHAR_BIT;
  if(part_size == 0) {
    part_size = 1;
  }
  range_start = part_size * search_limit;
  range_end = range_start + part_size;

  if(range_end > hdr.max_pages) {
    range_end = hdr.max_pages;
  }

  /*
   * Move from the end of the range towards the beginning and look for
   * a byte that has been modified.
   *
   * An important implication of this is that if the last written bytes
   * are zeroes, then these are skipped from the calculation.
   */
  
  for(page = first_page + range_end; page >= first_page + range_start; page--) {
    watchdog_periodic();
    COFFEE_READ(buf, sizeof(buf), page * COFFEE_PAGE_SIZE);
    for(i = COFFEE_PAGE_SIZE - 1; i >= 0; i--) {
      if(buf[i] != 0) {
	if(page == first_page) {
	  return i < sizeof(hdr) ? 0 : 1 + i - sizeof(hdr);
	} else {
	  return 1 + i + (page - first_page) * COFFEE_PAGE_SIZE - sizeof(hdr);
	}
      }
    }
  }
  
  /* All bytes are writable. */
  return 0;
}
示例#26
0
文件: i2c.c 项目: 1847123212/contiki
uint16_t
i2c_probe(void)
{
  int p = 0;
  const char *del = ",";
  uint16_t probed = 0;
  watchdog_periodic();
  if(!i2c_start(I2C_AT24MAC_ADDR)) {
    i2c_stop();
    probed |= I2C_AT24MAC;
    print_delim(p++, "AT24MAC", del);
  }
  watchdog_periodic();
  if(!i2c_start(I2C_SHT2X_ADDR)) {
    i2c_stop();
    probed |= I2C_SHT2X;
    print_delim(p++, "SHT2X", del);
  }
  watchdog_periodic();
  if(!i2c_start(I2C_CO2SA_ADDR)) {
    i2c_stop();
    probed |= I2C_CO2SA;
    print_delim(p++, "CO2SA", del);
  }
  watchdog_periodic();
  if(!i2c_start(I2C_BME280_ADDR)) {
    i2c_stop();
    probed |= I2C_BME280;
    print_delim(p++, "BME280", del);
  }
  watchdog_periodic();
  if(!i2c_start(I2C_PMS5003_ADDR)) {
    i2c_stop();
    probed |= I2C_PMS5003;
    print_delim(p++, "PMS5003", del);
  }
  return probed;
}
示例#27
0
/*
 * Notify all modules that we're back on and rely on them to restore clocks
 * and power domains as required.
 */
static void
wake_up(void)
{
  lpm_registered_module_t *module;

  /* Remember IRQ energest for next pass */
  ENERGEST_IRQ_SAVE(irq_energest);
  ENERGEST_ON(ENERGEST_TYPE_CPU);
  ENERGEST_OFF(ENERGEST_TYPE_LPM);

  /* Sync so that we get the latest values before adjusting recharge settings */
  ti_lib_sys_ctrl_aon_sync();

  /* Adjust recharge settings */
  ti_lib_sys_ctrl_adjust_recharge_after_power_down();

  /*
   * Release the request to the uLDO
   * This is likely not required, since the switch to GLDO/DCDC is automatic
   * when coming back from deep sleep
   */
  ti_lib_prcm_mcu_uldo_configure(false);

  /* Turn on cache again */
  ti_lib_vims_mode_set(VIMS_BASE, VIMS_MODE_ENABLED);
  ti_lib_prcm_cache_retention_enable();

  ti_lib_aon_ioc_freeze_disable();
  ti_lib_sys_ctrl_aon_sync();

  /* Check operating conditions, optimally choose DCDC versus GLDO */
  ti_lib_sys_ctrl_dcdc_voltage_conditional_control();

  /*
   * We may or may not have been woken up by an AON RTC tick. If not, we need
   * to adjust our software tick counter
   */
  clock_update();

  watchdog_periodic();

  /* Notify all registered modules that we've just woken up */
  for(module = list_head(modules_list); module != NULL;
      module = module->next) {
    if(module->wakeup) {
      module->wakeup();
    }
  }
}
示例#28
0
/*-------------------------------------------------------------------------*/
int
main(void)
{
  initialize();

  while(1) {
    process_run();
    watchdog_periodic();




}
  return 0;
}
示例#29
0
also into the IRQ
///
void eeprom_write(eeprom_addr_t addr, unsigned char *buf, int size)
{
struct timer tmr;
timer_set(&tmr,1000); // deve essere almeno 1000 ms
while (1) {
	watchdog_periodic();
	if (timer_expired(&tmr)) {
		printf("eeprom_read error\r\n");
		printf("nvm_state_str.nvm_sts:%d\r\n",nvm_state_str.nvm_sts);
		printf("nvm_state_str.nvm_cmd:%d\r\n",nvm_state_str.nvm_cmd);
		return;
		}
	if (nvm_state_str.nvm_sts == nvms_WRITEDONE) {
		nvm_state_str.nvm_cmd = nvmc_NONE; 
		nvm_state_str.nvm_sts = nvms_NONE; 
		return;
		}
	if (IS_BUSY || (nvm_state_str.nvm_sts != nvms_NONE)) continue;
	nvm_state_str.pAddress = buf;
	nvm_state_str.count = size;
	nvm_state_str.Address.us = addr;
	switch(nvm_state_str.Address.uc[0]) {
		case 0: nvm_state_str.DevSel_W = WRITE_PAGE_0; break;
		case 1: nvm_state_str.DevSel_W = WRITE_PAGE_1; break;
		case 2: nvm_state_str.DevSel_W = WRITE_PAGE_2; break;
		case 3: nvm_state_str.DevSel_W = WRITE_PAGE_3; break;
		case 4: nvm_state_str.DevSel_W = WRITE_PAGE_4; break;
		case 5: nvm_state_str.DevSel_W = WRITE_PAGE_5; break;
		case 6: nvm_state_str.DevSel_W = WRITE_PAGE_6; break;
		case 7: nvm_state_str.DevSel_W = WRITE_PAGE_7; break;
		default: nvm_state_str.DevSel_W = WRITE_PAGE_0; break;
		}
	// set Master transfer mode
	IIC2.ICCR1.BIT.MST = 1;
	IIC2.ICCR1.BIT.TRS = 1;
	//
	_START_();
	//
	IIC2.ICSR.BIT.TEND = 0;
	IIC2.ICIER.BIT.TEIE = 1;
	nvm_state_str.iicStatus = iics_DEVSEL_W;
	nvm_state_str.nvm_cmd = nvmc_WRITE_ARRAY;
	nvm_state_str.nvm_sts = nvms_WRITEINPROGRESS;
	IIC2.ICDRT = nvm_state_str.DevSel_W;
	}
}
示例#30
0
static size_t memwrite_flash(void *dest, void *src, size_t len) {
#if USE_BLOCKWRITING
	return rom_pwrite(src, len, (off_t)(uintptr_t)dest);
	// The compiler will remove everything from this point on.
	// -> No #elsif needed
#endif /* USE_BLOCKWRITING */
	size_t written = 0;
	unsigned short *lcldest = dest;
	char *lclsrc = src;
	unsigned short ow;
	char *owptr = (char*) (&ow);

#if DEBUG
	if ((uintptr_t) dest & 1) {
		puts("Alignment FAIL");
	}
#endif
	//DPRINTF("Flash: %x\n", ow);
	//watchdog_periodic();

#if 1 != FBENCHMARK
	flash_setup();
#endif
	while ((len & ~0x1) > written) {
		owptr[0] = *lclsrc++;
		owptr[1] = *lclsrc++;
#if 1 !=FBENCHMARK
		flash_write(lcldest, ow);
#if DEBUG
		printf("WRT: %p, %d, %d\n", lcldest, ow, *lcldest);
		watchdog_periodic();
		if (ow != *lcldest) {
			printf("PANIC!");
			while (1)
				;
		}
#endif
		lcldest++;
#endif
		written += 2;
	}
#if 1 != FBENCHMARK
	flash_done();
	IFG1 |= UTXIFG0;
#endif
	return written;
}