Exemplo n.º 1
0
int main(int argc, char *argv[]) {
    char        *word, *pEnd;
    int         word_count, i, nread;
    uint32_t    cmd_addr, flag;
    char        *gap = " \n";
    char        buff[BUFFSIZE];
    ENTRY       definition, *wp;
    pthread_t   receive_t;

    if (setupIO(argc, argv)) {
         return EXIT_FAILURE;
    }

    // if not given, use default filename
    if (namelen == 0) {
        strcpy(filename, "prufh");
        namelen = 5;
    }
    strcat(filename, ".defs");

    // open list of forth word addresses
    FILE *file = fopen(filename, "r");
    if (file == NULL) {
        fprintf(stderr, "Unable to open definitions file, %s\n", filename);
        return EXIT_FAILURE;
    }

    // get number of definitions from begining of file
    fgets (buff, BUFFSIZE, file );
    word_count = atoi(buff);

    // create hash for definition table
    hcreate(word_count);

    // fill hash with name-address pairs
    for (i=0; i<word_count;) {
        fgets(buff, BUFFSIZE, file);

        word = strtok(buff, gap);
        if (word != NULL) {
            definition.key = strdup(word);
            definition.data = (void*)strtol(strtok(NULL, gap), NULL, 0);

            wp = hsearch(definition, ENTER);

            if (wp == NULL) {
                fprintf(stderr, "Dictionary entry failed on \"%s\"\n", word);
                return EXIT_FAILURE;
            } else {
                if (!quiet_mode) 
                    printf("Saved %9.9s  as   %p\n", wp->key, wp->data); 
            }	    
            i++;
        }
    }
    fclose(file);

    if (pruInit(pru_num) == EXIT_SUCCESS) { 

        // start seperate thread to handle forth output
        if (pthread_create(&receive_t, NULL, receive, (void*) &outpipe)) {
        }

        // main loop, relay input to pru
        for(;;) {
            nread = read(0, buff, BUFFSIZE);  // wait here for input on stdin
            if (nread > 1) {
                word = strtok(buff, gap);

                // exit program on "bye" command
                if (strncmp(word, "bye", 3) == 0) break;

                // find address of word corresponding to input
                definition.key = word;
                wp = hsearch(definition, FIND);

                if (wp == NULL) {
                    // if input not defined, see if it is a number
                    cmd_addr = strtoul(word, &pEnd, 0);
                    if (pEnd == word) {
                        fprintf(stderr, "Unknown word, \"%s\"\n", word);
                        continue;
                    } else {
                        // if a number was input, signal forth to push it
                        flag = LIT_FLAG;
                        if (!quiet_mode) 
                            printf("Pushing %d\n", cmd_addr);
                    }
                } else {
                    // signal that a command address is being sent
                    flag = CMD_FLAG;
                    // retrieve the address
                    cmd_addr = (uint16_t)(intptr_t)(wp->data);
                    if (!quiet_mode) 
                        printf("Executing %9.9s %x\n", word, cmd_addr);
                }

                // send message to pruss
                if (exec(cmd_addr, flag)) {
                    fprintf(stderr, "Unable to issue command, \"%s\"\n", word);
                }
            }
        }
    }
    hdestroy();

    pthread_cancel(receive_t);
    pthread_join(receive_t, NULL);

    /* shutdown pru */
    prussdrv_pru_disable(pru_num);
    prussdrv_exit();

    if (outpipe != 1) {
        close(outpipe);
    }
    if (inpipe != 0) {
        close(inpipe);
    }

    return EXIT_SUCCESS;
}
Exemplo n.º 2
0
PRU::PRU()
{
  pthread_t thread;
  pruInit();
  pthread_create(&thread, NULL, resetWatchdogPRU, (void*)1000);
}