/alps/ipecamera

To get this branch, use:
bzr branch http://suren.me/webbzr/alps/ipecamera

« back to all changes in this revision

Viewing changes to cli.c

  • Committer: Suren A. Chilingaryan
  • Date: 2011-12-12 04:45:35 UTC
  • Revision ID: csa@dside.dyndns.org-20111212044535-6no1q7g230i8uvlv
multithread preprocessing of ipecamera frames and code reorganization

Show diffs side-by-side

added added

removed removed

Lines of Context:
345
345
                case PCILIB_DMA_TYPE_PACKET:
346
346
                    printf("Packet");
347
347
                break;
 
348
                default:
 
349
                    printf("Unknown");
348
350
            }
349
351
            
350
352
            printf(", Address Width: %02lu bits\n", engine->addr_bits);
371
373
    else registers = model_info->registers;
372
374
    
373
375
    if (registers) {
374
 
        pcilib_register_bank_addr_t bank_addr;
 
376
        pcilib_register_bank_addr_t bank_addr = 0;
375
377
        if (bank) {
376
378
            pcilib_register_bank_t bank_id = pcilib_find_bank(handle, bank);
377
379
            pcilib_register_bank_description_t *b = model_info->banks + bank_id;
458
460
    int err;
459
461
    int i, j, errors;
460
462
    void *data, *buf, *check;
461
 
    void *fifo;
 
463
    void *fifo = NULL;
462
464
    struct timeval start, end;
463
465
    unsigned long time;
464
466
    size_t size, min_size, max_size;
483
485
            err = pcilib_wait_irq(handle, 0, 0, &irqs);
484
486
            if (err) irqs = 0;
485
487
            
486
 
            printf("%8i KB - ", size / 1024);
 
488
            printf("%8zu KB - ", size / 1024);
487
489
            
488
490
            printf("RW: ");
489
491
            if (mbs < 0) printf("failed ...   ");
571
573
        gettimeofday(&end,NULL);
572
574
 
573
575
        time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
574
 
        printf("%8i bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
 
576
        printf("%8zu bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
575
577
        
576
578
        fflush(0);
577
579
 
611
613
        gettimeofday(&end,NULL);
612
614
 
613
615
        time = (end.tv_sec - start.tv_sec)*1000000 + (end.tv_usec - start.tv_usec);
614
 
        printf("%8i bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
 
616
        printf("%8zu bytes - read: %8.2lf MB/s", size, 1000000. * size * BENCHMARK_ITERATIONS / (time * 1024. * 1024.));
615
617
        
616
618
        fflush(0);
617
619
 
651
653
 
652
654
    free(check);
653
655
    free(buf);
 
656
    
 
657
    return 0;
654
658
}
655
659
 
656
660
#define pci2host16(endianess, value) endianess?
793
797
    const char *format;
794
798
 
795
799
    pcilib_register_bank_t bank_id;
796
 
    pcilib_register_bank_addr_t bank_addr;
 
800
    pcilib_register_bank_addr_t bank_addr = 0;
797
801
 
798
802
    pcilib_register_value_t value;
799
803
    
871
875
    }
872
876
 
873
877
    int access = banks[bank_id].access / 8;
874
 
    int size = n * abs(access);
 
878
//    int size = n * abs(access);
875
879
    int block_width, blocks_per_line;
876
880
    int numbers_per_block, numbers_per_line; 
877
881
    
918
922
int WriteData(pcilib_t *handle, ACCESS_MODE mode, pcilib_dma_engine_addr_t dma, pcilib_bar_t bar, uintptr_t addr, size_t n, access_t access, int endianess, char ** data) {
919
923
    int read_back = 0;
920
924
    void *buf, *check;
921
 
    int res, i, err;
 
925
    int res = 0, i, err;
922
926
    int size = n * abs(access);
923
927
    size_t ret;
924
928
    pcilib_dma_engine_t dmaid;
933
937
            case 2: res = sscanf(data[i], "%hx", ((uint16_t*)buf)+i); break;
934
938
            case 4: res = sscanf(data[i], "%x", ((uint32_t*)buf)+i); break;
935
939
            case 8: res = sscanf(data[i], "%lx", ((uint64_t*)buf)+i); break;
 
940
            default: Error("Unexpected data size (%lu)", access);
936
941
        }
937
942
        if ((res != 1)||(!isxnumber(data[i]))) Error("Can't parse data value at poition %i, (%s) is not valid hex number", i, data[i]);
938
943
    }
1010
1015
 
1011
1016
int WriteRegister(pcilib_t *handle, pcilib_model_description_t *model_info, const char *bank, const char *reg, char ** data) {
1012
1017
    int err;
1013
 
    int i;
1014
1018
 
1015
1019
    unsigned long val;
1016
1020
    pcilib_register_value_t value;
1017
1021
 
1018
 
    const char *format;
 
1022
    const char *format = NULL;
1019
1023
 
1020
1024
    pcilib_register_t regid = pcilib_find_register(handle, bank, reg);
1021
1025
    if (regid == PCILIB_REGISTER_INVALID) Error("Can't find register (%s) from bank (%s)", reg, bank?bank:"autodetected");
1118
1122
    
1119
1123
    if (info->flags&PCILIB_EVENT_INFO_FLAG_BROKEN) {
1120
1124
        ctx->broken_count++;
1121
 
        return 0;
 
1125
        return PCILIB_STREAMING_CONTINUE;
1122
1126
    }
1123
1127
 
1124
1128
    data = pcilib_get_data(handle, event_id, ctx->data, &size);
1125
1129
    if (!data) {
1126
1130
        ctx->broken_count++;
1127
 
        return 0;
 
1131
        return PCILIB_STREAMING_CONTINUE;
1128
1132
    }
1129
1133
    
1130
1134
    
1136
1140
        else Error("Write failed");
1137
1141
    }
1138
1142
 
1139
 
    pcilib_return_data(handle, event_id, data);
 
1143
    pcilib_return_data(handle, event_id, ctx->data, data);
1140
1144
    
1141
1145
//    printf("%lu %lu\n", info->seqnum, info->offset);
1142
1146
 
1161
1165
*/
1162
1166
 
1163
1167
//    printf("data callback: %lu\n", event_id);    
1164
 
    return 0;
 
1168
    return PCILIB_STREAMING_CONTINUE;
1165
1169
}
1166
1170
 
1167
1171
int raw_data(pcilib_event_id_t event_id, pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user) {
1168
1172
//    printf("%i\n", event_id);
 
1173
    return PCILIB_STREAMING_CONTINUE;
1169
1174
}
1170
1175
 
1171
1176
 
1204
1209
void GrabStats(GRABContext *ctx, struct timeval *end_time) {
1205
1210
    pcilib_timeout_t duration, fps_duration;
1206
1211
    struct timeval cur;
1207
 
    double fps;
 
1212
    double fps = 0;
1208
1213
 
1209
1214
    if (!end_time) {
1210
1215
        gettimeofday(&cur, NULL);
1532
1537
    
1533
1538
    while ((entry = readdir(dir)) != NULL) {
1534
1539
        FILE *f;
1535
 
        unsigned long use;
1536
 
        unsigned long size;
1537
 
        unsigned long refs;
1538
 
        unsigned long mode;
1539
 
        unsigned long hwref;
 
1540
        unsigned long use = 0;
 
1541
        unsigned long size = 0;
 
1542
        unsigned long refs = 0;
 
1543
        unsigned long mode = 0;
 
1544
        unsigned long hwref = 0;
1540
1545
        
1541
1546
        if (strncmp(entry->d_name, "kbuf", 4)) continue;
1542
1547
        if (!isnumber(entry->d_name+4)) continue;
1581
1586
        
1582
1587
        printf("%08lx  ", uses[i].use);
1583
1588
        if (!i) printf("All Others         ");
1584
 
        else if ((uses[i].use >> 16) == PCILIB_KMEM_USE_DMA_RING) printf("DMA%u %s Ring      ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
1585
 
        else if ((uses[i].use >> 16) == PCILIB_KMEM_USE_DMA_PAGES) printf("DMA%u %s Pages     ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
1586
 
        else printf ("                   ", uses[i].use);
 
1589
        else if ((uses[i].use >> 16) == PCILIB_KMEM_USE_DMA_RING) printf("DMA%lu %s Ring      ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
 
1590
        else if ((uses[i].use >> 16) == PCILIB_KMEM_USE_DMA_PAGES) printf("DMA%lu %s Pages     ", uses[i].use&0x7F, ((uses[i].use&0x80)?"S2C":"C2S"));
 
1591
        else printf ("                   ");
1587
1592
        printf("  ");
1588
1593
        printf("% 6lu", uses[i].count);
1589
1594
        printf("     ");
1611
1616
    void *data;
1612
1617
    size_t size;
1613
1618
    pcilib_kmem_handle_t *kbuf;
1614
 
    
 
1619
 
1615
1620
    kbuf = pcilib_alloc_kernel_memory(handle, 0, block + 1, 0, 0, use, PCILIB_KMEM_FLAG_REUSE|PCILIB_KMEM_FLAG_TRY);
1616
1621
    if (!kbuf) {
1617
1622
        printf("The specified kernel buffer is not allocated\n");
1618
1623
        return 0;
1619
1624
    }
1620
 
    
 
1625
 
1621
1626
    data = pcilib_kmem_get_block_ua(handle, kbuf, block);
1622
1627
    if (data) {
1623
1628
        size = pcilib_kmem_get_block_size(handle, kbuf, block);
1626
1631
    } else {
1627
1632
        printf("The specified block is not existing\n");
1628
1633
    }
1629
 
    
 
1634
 
1630
1635
    pcilib_free_kernel_memory(handle, kbuf, KMEM_FLAG_REUSE);
 
1636
 
 
1637
    return 0;
1631
1638
}
1632
1639
 
1633
1640
int FreeKMEM(pcilib_t *handle, const char *device, const char *use, int force) {
1634
1641
    int err;
1635
1642
    int i;
1636
 
    
 
1643
 
1637
1644
    unsigned long useid;
1638
 
    
 
1645
 
1639
1646
    pcilib_kmem_flags_t flags = PCILIB_KMEM_FLAG_HARDWARE|PCILIB_KMEM_FLAG_PERSISTENT|PCILIB_KMEM_FLAG_EXCLUSIVE; 
1640
1647
    if (force) flags |= PCILIB_KMEM_FLAG_FORCE; // this will ignore mmap locks as well.
1641
1648
 
1653
1660
        
1654
1661
        return 0;
1655
1662
    }
1656
 
    
 
1663
 
1657
1664
    if ((!isxnumber(use))||(sscanf(use, "%lx", &useid) != 1)) Error("Invalid use (%s) is specified", use);
1658
 
    
 
1665
 
1659
1666
    err = pcilib_clean_kernel_memory(handle, useid, flags);
1660
1667
    if (err) Error("Error cleaning kernel buffers for use (0x%lx)", useid);
1661
 
    
 
1668
 
1662
1669
    return 0;
1663
1670
}
1664
1671
 
1689
1696
    printf("--------------------------------------------------------------------------------\n");
1690
1697
    while ((entry = readdir(dir)) != NULL) {
1691
1698
        FILE *f;
1692
 
        unsigned long use;
1693
 
        unsigned long size;
1694
 
        unsigned long refs;
1695
 
        unsigned long mode;
1696
 
        unsigned long hwref;
 
1699
        unsigned long use = 0;
 
1700
        unsigned long size = 0;
 
1701
        unsigned long refs = 0;
 
1702
        unsigned long mode = 0;
 
1703
        unsigned long hwref = 0;
1697
1704
        
1698
1705
        if (strncmp(entry->d_name, "kbuf", 4)) continue;
1699
1706
        if (!isnumber(entry->d_name+4)) continue;
1724
1731
        if (dmaid == PCILIB_DMA_ENGINE_INVALID) continue;
1725
1732
        
1726
1733
        
1727
 
        printf("DMA%u %s         ", use&0x7F, (use&0x80)?"S2C":"C2S");
 
1734
        printf("DMA%lu %s         ", use&0x7F, (use&0x80)?"S2C":"C2S");
1728
1735
        err = pcilib_start_dma(handle, dmaid, 0);
1729
1736
        if (err) {
1730
1737
            printf("-- Wrong state, start is failed\n");
1773
1780
 
1774
1781
    dmaid = pcilib_find_dma_by_addr(handle, dma_direction, dma);
1775
1782
    if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("The specified DMA engine is not found");
1776
 
    
 
1783
 
1777
1784
    err = pcilib_start_dma(handle, dmaid, 0);
1778
1785
    if (err) Error("Error starting the specified DMA engine");
1779
 
    
 
1786
 
1780
1787
    err = pcilib_get_dma_status(handle, dmaid, &status, 0, NULL);
1781
1788
    if (err) Error("Failed to obtain status of the specified DMA engine");
1782
 
    
 
1789
 
1783
1790
    buffer = (pcilib_dma_buffer_status_t*)malloc(status.ring_size*sizeof(pcilib_dma_buffer_status_t));
1784
1791
    if (!buffer) Error("Failed to allocate memory for status buffer");
1785
 
    
 
1792
 
1786
1793
    err = pcilib_get_dma_status(handle, dmaid, &status, status.ring_size, buffer);
1787
1794
    if (err) Error("Failed to obtain extended status of the specified DMA engine");
1788
 
    
1789
 
    
 
1795
 
 
1796
 
1790
1797
    printf("Buffer      Status      Total Size         \n");
1791
1798
    printf("--------------------------------------------------------------------------------\n");
1792
 
    
 
1799
 
1793
1800
    for (i = 0; i < status.ring_size; i++) {
1794
1801
        printf("%8zu    ", i);
1795
1802
        printf("%c%c %c%c ", buffer[i].used?'U':' ',  buffer[i].error?'E':' ', buffer[i].first?'F':' ', buffer[i].last?'L':' ');
1796
1803
        printf("% 10s", PrintSize(stmp, buffer[i].size));
1797
1804
        printf("\n");
1798
1805
    }
1799
 
    
 
1806
 
1800
1807
    printf("--------------------------------------------------------------------------------\n");
1801
1808
    printf("U - Used, E - Error, F - First block, L - Last Block\n");
1802
 
    
 
1809
 
1803
1810
    free(buffer);
1804
1811
 
1805
1812
    pcilib_stop_dma(handle, dmaid, 0);
1806
1813
 
 
1814
    return 0;
1807
1815
}
1808
1816
 
1809
1817
int ReadBuffer(pcilib_t *handle, const char *device, pcilib_model_description_t *model_info, pcilib_dma_engine_addr_t dma, pcilib_dma_direction_t dma_direction, size_t block, FILE *o) {
1810
1818
    int err;
1811
 
    size_t i;
1812
1819
    pcilib_dma_engine_t dmaid;
1813
1820
    pcilib_dma_engine_status_t status;
1814
1821
    pcilib_dma_buffer_status_t *buffer;
1815
1822
    size_t size;
1816
 
    char stmp[256];
1817
1823
 
1818
1824
    dmaid = pcilib_find_dma_by_addr(handle, dma_direction, dma);
1819
1825
    if (dmaid == PCILIB_DMA_ENGINE_INVALID) Error("The specified DMA engine is not found");
1902
1908
    const char *data_type = NULL;
1903
1909
    const char *dma_channel = NULL;
1904
1910
    const char *use = NULL;
1905
 
    pcilib_kmem_use_t use_id;
 
1911
    pcilib_kmem_use_t use_id = 0;
1906
1912
    size_t block = 0;
1907
 
    pcilib_irq_hw_source_t irq_source;
 
1913
    pcilib_irq_hw_source_t irq_source =  PCILIB_IRQ_SOURCE_DEFAULT;
1908
1914
    pcilib_dma_direction_t dma_direction = PCILIB_DMA_BIDIRECTIONAL;
1909
1915
    
1910
1916
    pcilib_dma_engine_addr_t dma = PCILIB_DMA_ENGINE_ADDR_INVALID;
1912
1918
    uintptr_t start = -1;
1913
1919
    size_t size = 1;
1914
1920
    access_t access = 4;
1915
 
    int skip = 0;
 
1921
//    int skip = 0;
1916
1922
    int endianess = 0;
1917
1923
    size_t timeout = 0;
1918
1924
    const char *output = NULL;
1920
1926
    size_t iterations = BENCHMARK_ITERATIONS;
1921
1927
 
1922
1928
    pcilib_t *handle;
1923
 
    
 
1929
 
1924
1930
    int size_set = 0;
1925
1931
    int timeout_set = 0;
1926
1932
    int run_time_set = 0;
2046
2052
                mode = MODE_WAIT_IRQ;
2047
2053
                if (optarg) num_offset = optarg;
2048
2054
                else if ((optind < argc)&&(argv[optind][0] != '-'))  num_offset = argv[optind++];
 
2055
                else num_offset = NULL;
2049
2056
                
2050
 
                if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
2051
 
                    Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
 
2057
                if (num_offset) {
 
2058
                    if ((!isnumber(num_offset))||(sscanf(num_offset, "%li", &itmp) != 1))
 
2059
                        Usage(argc, argv, "Invalid IRQ source is specified (%s)", num_offset);
2052
2060
 
2053
 
                irq_source = itmp;
 
2061
                    irq_source = itmp;
 
2062
                }
2054
2063
            break;
2055
2064
            case OPT_LIST_KMEM:
2056
2065
                if (mode != MODE_INVALID) Usage(argc, argv, "Multiple operations are not supported");
2132
2141
                }
2133
2142
            break;
2134
2143
            case OPT_SIZE:
2135
 
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &size) != 1))
 
2144
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &size) != 1)) {
2136
2145
                    if (strcasecmp(optarg, "unlimited"))
2137
2146
                        Usage(argc, argv, "Invalid size is specified (%s)", optarg);
2138
2147
                    else
2139
2148
                        size = 0;//(size_t)-1;
 
2149
                }
2140
2150
                        
2141
2151
                size_set = 1;
2142
2152
            break;
2151
2161
                
2152
2162
            break;
2153
2163
            case OPT_TIMEOUT:
2154
 
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &timeout) != 1))
 
2164
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &timeout) != 1)) {
2155
2165
                    if (strcasecmp(optarg, "unlimited"))
2156
2166
                        Usage(argc, argv, "Invalid timeout is specified (%s)", optarg);
2157
2167
                    else
2158
2168
                        timeout = PCILIB_TIMEOUT_INFINITE;
 
2169
                }
2159
2170
                timeout_set = 1;
2160
2171
            break;
2161
2172
            case OPT_OUTPUT:
2172
2183
                data_type = optarg;
2173
2184
            break;
2174
2185
            case OPT_RUN_TIME:
2175
 
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &run_time) != 1))
 
2186
                if ((!isnumber(optarg))||(sscanf(optarg, "%zu", &run_time) != 1)) {
2176
2187
                    if (strcasecmp(optarg, "unlimited"))
2177
2188
                        Usage(argc, argv, "Invalid run-time is specified (%s)", optarg);
2178
2189
                    else
2179
2190
                        run_time = 0;
 
2191
                }
2180
2192
                run_time_set = 1;
2181
2193
            break;
2182
2194
            case OPT_TRIGGER_TIME:
2461
2473
     case MODE_FREE_KMEM:
2462
2474
        FreeKMEM(handle, fpga_device, use, force);
2463
2475
     break;
 
2476
     case MODE_INVALID:
 
2477
        break;
2464
2478
    }
2465
2479
 
2466
2480
    if (ofile) fclose(ofile);