static int install_getstartpage(int startpage, int pagemargin, int desiredsize) { uint16_t page = 0, stpage = 0xffff; uint16_t pagesize = 0; int maxlen = -1; int maxlen_start = 0xffff; int status; for (status=0, page=0; status >= 0; page++) { status = up_progmem_ispageerased(page); pagesize = up_progmem_pagesize(page); /* Is this beginning of new free space section */ if (status == 0) { if (stpage == 0xffff) stpage = page; } else if (status != 0) { if (stpage != 0xffff) { if ((page - stpage) > maxlen) { if (maxlen==-1) { /* First time found sth? */ stpage += pagemargin; maxlen = 0; } if (stpage < startpage) { stpage = startpage; } if (page > stpage) { maxlen = page - stpage; maxlen_start = stpage; } if (maxlen*pagesize >= desiredsize) { /* printf("Found page at %d ... %d\n", stpage, page); */ return maxlen_start*pagesize; } } stpage = 0xffff; } } } /* Requested space is not available */ return -1; }
static void free_getprogmeminfo(struct mallinfo * mem) { uint16_t page = 0, stpage = 0xFFFF; uint16_t pagesize = 0; int status; mem->arena = 0; mem->fordblks = 0; mem->uordblks = 0; mem->mxordblk = 0; for (status=0, page=0; status >= 0; page++) { status = up_progmem_ispageerased(page); pagesize = up_progmem_pagesize(page); mem->arena += pagesize; /* Is this beginning of new free space section */ if (status == 0) { if (stpage == 0xFFFF) stpage = page; mem->fordblks += pagesize; } else if (status != 0) { mem->uordblks += pagesize; if (stpage != 0xFFFF && up_progmem_isuniform()) { stpage = page - stpage; if (stpage > mem->mxordblk) { mem->mxordblk = stpage; } stpage = 0xFFFF; } } } mem->mxordblk *= pagesize; }
int sif_main(int argc, char *argv[]) { if (argc >= 2) { if (!strcmp(argv[1], "init")) { return sif_init(); } else if (!strcmp(argv[1], "gpio") && argc == 4) { vsn_sif.gpio[0] = atoi(argv[2]); vsn_sif.gpio[1] = atoi(argv[3]); sif_gpio1_update(); sif_gpio2_update(); printf("GPIO States: %2x %2x\n", vsn_sif.gpio[0], vsn_sif.gpio[1]); return 0; } else if (!strcmp(argv[1], "pwr") && argc == 3) { int val = atoi(argv[2]); //STM32_TIM_SETCOMPARE(vsn_sif.tim8, GPIO_OUT_PWRPWM_TIM8_CH, val); STM32_TIM_SETCOMPARE(vsn_sif.tim3, GPIO_OUT_PWM_TIM3_CH, val); return 0; } else if (!strcmp(argv[1], "time") && argc == 3) { struct timespec t_set; t_set.tv_sec = atoi(argv[2]); clock_settime(CLOCK_REALTIME, &t_set); } else if (!strcmp(argv[1], "free")) { size_t page = 0, stpage = 0xFFFF; ssize_t status; do { status = up_progmem_ispageerased(page++); /* Is this beginning of new free space section */ if (status == 0) { if (stpage == 0xFFFF) stpage = page-1; } else if (status != 0) { if (stpage != 0xFFFF) { printf("Free Range:\t%lu\t-\t%lu\n", (unsigned long)stpage, (unsigned long)(page-2)); stpage = 0xFFFF; } } } while (status >= 0); return 0; } else if (!strcmp(argv[1], "erase") && argc == 3) { size_t page = atoi(argv[2]); printf("Erase result: %d\n", up_progmem_erasepage(page)); return 0; } else if (!strcmp(argv[1], "flash") && argc == 3) { size_t page = atoi(argv[2]); size_t addr = page * up_progmem_pagesize(page); printf("Write result: %d (writing to address %lxh)\n", up_progmem_write(addr, "Test", 4), (unsigned long)addr); return 0; } else if (!strcmp(argv[1], "i2c") && argc == 3) { int val = atoi(argv[2]); I2C_SETFREQUENCY(vsn_sif.i2c1, 100000); struct lis331dl_dev_s * lis = lis331dl_init(vsn_sif.i2c1, val); if (lis) { const struct lis331dl_vector_s * a; int i; uint32_t time_stamp = clock_systimer(); /* Set to 400 Hz : 3 = 133 Hz/axis */ lis331dl_setconversion(lis, false, true); /* Sample some values */ for (i=0; i<1000;) { if ((a = lis331dl_getreadings(lis))) { i++; printf("%d %d %d\n", a->x, a->y, a->z); } else if (errno != 11) { printf("Readings errno %d\n", errno); break; } } printf("Time diff = %d\n", clock_systimer() - time_stamp); lis331dl_deinit(lis); } else printf("Exit point: errno=%d\n", errno); return 0; } else if (!strcmp(argv[1], "pga")) { int gain = atoi(argv[2]); gain = vsn_muxbus_setpgagain(gain); printf("Gain changed: %d\n", gain); return 0; } else if (!strcmp(argv[1], "cc")) { struct cc1101_dev_s * cc; uint8_t buf[64]; int sta; cc = cc1101_init(vsn_sif.spi2, CC1101_PIN_GDO0, GPIO_CC1101_GDO0, &cc1101_rfsettings_ISM1_868MHzGFSK100kbps); if (cc) { /* Work-around: enable falling edge, event and interrupt */ stm32_gpiosetevent(GPIO_CC1101_GDO0, false, true, true, cc1101_eventcb); /* Enable clock to ARM PLL, allowing to speed-up to 72 MHz */ cc1101_setgdo(cc, CC1101_PIN_GDO2, CC1101_GDO_CLK_XOSC3); cc1101_setchannel(cc, 0); /* AV Test Hex, receive on that channel */ cc1101_receive(cc); /* Enter RX mode */ while (1) { fflush(stdout); sta = cc1101_read(cc, buf, 64); if (sta > 0) { printf("Received %d bytes: rssi=%d [dBm], LQI=%d (CRC %s)\n", sta, cc1101_calcRSSIdBm(buf[sta-2]), buf[sta-1]&0x7F, (buf[sta-1]&0x80)?"OK":"BAD"); cc1101_write(cc, buf, 61); cc1101_send(cc); printf("Packet send back\n"); cc1101_receive(cc); } } } } } fprintf(stderr, "%s:\tinit\n\tgpio\tA B\n\tpwr\tval\n", argv[0]); fprintf(stderr, "rtc time = %u, time / systick = %u / %u\n", up_rtc_time(), time(NULL), clock_systimer()); return -1; }
ssize_t up_progmem_eraseblock(size_t block) { uintptr_t base; size_t page_address; if (block >= STM32_FLASH_NPAGES) { return -EFAULT; } #if defined(STM32_FLASH_DUAL_BANK) /* Handle paged FLASH */ if (block >= STM32_FLASH_BANK0_NPAGES) { base = STM32_FLASH_BANK1_BASE; } else #endif { base = STM32_FLASH_BANK0_BASE; } sem_lock(); if ((getreg32(base + STM32_RCC_CR_OFFSET) & RCC_CR_HSION) == 0) { sem_unlock(); return -EPERM; } /* Get flash ready and begin erasing single page */ flash_unlock(base); modifyreg32(base + STM32_FLASH_CR_OFFSET, 0, FLASH_CR_PER); /* Must be valid - page index checked above */ page_address = up_progmem_getaddress(block); putreg32(page_address, base + STM32_FLASH_AR_OFFSET); modifyreg32(base + STM32_FLASH_CR_OFFSET, 0, FLASH_CR_STRT); while ((getreg32(base + STM32_FLASH_SR_OFFSET) & FLASH_SR_BSY) != 0) { up_waste(); } modifyreg32(base + STM32_FLASH_CR_OFFSET, FLASH_CR_PER, 0); sem_unlock(); /* Verify */ if (up_progmem_ispageerased(block) == 0) { return up_progmem_erasesize(block); /* success */ } else { return -EIO; /* failure */ } }
ssize_t __ramfunc__ up_progmem_erasepage(size_t page) { int ret = 0; int timeout; uint32_t regval; irqstate_t flags; if (page >= (EFM32_FLASH_NPAGES+EFM32_USERDATA_NPAGES)) { return -EFAULT; } efm32_flash_unlock(); flags = enter_critical_section(); /* enable writing to the flash */ bitband_set_peripheral(EFM32_MSC_WRITECTRL, _MSC_WRITECTRL_WREN_SHIFT, 1); /* Load address */ putreg32((uint32_t)up_progmem_getaddress(page), EFM32_MSC_ADDRB); putreg32(MSC_WRITECMD_LADDRIM, EFM32_MSC_WRITECMD); regval = getreg32(EFM32_MSC_STATUS); /* Check for invalid address */ if (regval & MSC_STATUS_INVADDR) { ret = -EINVAL; } /* Check for write protected page */ if ((ret == 0) && (regval & MSC_STATUS_LOCKED)) { ret = -EPERM; } /* Send erase page command */ if (ret == 0) { putreg32(MSC_WRITECMD_ERASEPAGE, EFM32_MSC_WRITECMD); /* Wait for the erase to complete */ timeout = MSC_PROGRAM_TIMEOUT; while ((getreg32(EFM32_MSC_STATUS) & MSC_STATUS_BUSY) && (timeout != 0)) { timeout--; } if (timeout == 0) { ret = -ETIMEDOUT; } } /* Disable writing to the MSC */ bitband_set_peripheral(EFM32_MSC_WRITECTRL, _MSC_WRITECTRL_WREN_SHIFT, 0); if (ret == 0) { /* Verify */ if (up_progmem_ispageerased(page) != 0) { ret = -EIO; } } leave_critical_section(flags); if (ret != 0) { return ret; } /* Success */ return up_progmem_pagesize(page); }