Shop OBEX P1 Docs P2 Docs Learn Events
Clean slate with Demoboard - Page 3 — Parallax Forums

Clean slate with Demoboard

13»

Comments

  • LeonLeon Posts: 7,620
    edited 2012-03-02 05:38
    printf takes *lots* of memory! Try lighting an LED instead.
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-03-02 06:39
    The program below compiles and runs, the LED 16 turns on/off when card is inserted, and removed. The part that does not work, 'int CSset'. The use of CSset does not cause an error, but it also does not light up LED 23 when it is used in main(). I guess their is something that I am missing with the workings of pthread, or the understanding of global variables.

    Ray

    /*
    * uSD1.c
    */
    
    #include <stdio.h>
    #include <propeller.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <cog.h>
    #include <sys/stat.h>
    #include <dirent.h>
    #include "misc.h"
    #include <pthread.h>
    
    
    extern _Driver _SimpleSerialDriver;
    extern _Driver _FileDriver;
    
    _Driver *_driverlist[] = {
        &_SimpleSerialDriver,
        &_FileDriver,
        NULL
    };
    
    int CSset;
    
    void CheckCS(void *argv)
    {
        pthread_set_affinity_thiscog_np();
    
        while(1)
        {
        if ((INA & (1<<4)) == 0)
        {
           /* printf("Card Detected.\n"); */
          /*  dfs_mount(); */
            high(16);
            CSset = 1;
          /*  printf("File System Mounted.\n"); */
        }
        else
           /* printf("No Card Detected.\n"); */
            low(16);
            CSset = 0;
        sleep(1);
        }
    }
    
    int main(int argc, char* argv[])
    {
    
        pthread_t thr;
    
        pthread_create(&thr, NULL, CheckCS, NULL);
    
        uint8_t buffer[80];
    
    #ifdef __PROPELLER_LMM__
    #if 1
        buffer[0] = 0;
        buffer[1] = 1;
        buffer[2] = 2;
        buffer[3] = 3;
        LoadSDDriver(buffer);
    #else
        LoadSDDriver(0);
    #endif
    #endif
    
    /*    sleep(1);
        CheckCS(); */
        if (CSset == 1)
            high(23);
    
        while(1)
        {
        }
        return 0;
    }
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-03-02 06:58
    You need to put the pin 23 logic in the while(1) loop. Don't you also want to set it low if CSset == 0? And you may need to declare CSset as volatile so the compiler doesn't optimize the test of CSset out of the loop. So your code should look something like this
    volatile int CSset;
    ...
    while (1)
    {
      if (CSset)
        high(23);
      else
        low(23);
    }
    
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-03-02 07:08
    I just tried the code, and LED 23 is not turning on. Their must be something else that is going wrong.

    Ray
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-03-02 07:36
    I just noticed something, I commented out <unistd.h>, and I am still using sleep() in the CheckCS(), but I am not getting any compiler complaints. So, where else does sleep() reside?

    Ray

    /*
    * uSD1.c
    */
    
    #include <stdio.h>
    #include <propeller.h>
    /*#include <stdlib.h>
    #include <unistd.h>
    #include <string.h>
    #include <cog.h>
    #include <sys/stat.h>
    #include <dirent.h> */
    #include "misc.h"
    #include <pthread.h>
    
    
    extern _Driver _SimpleSerialDriver;
    extern _Driver _FileDriver;
    
    _Driver *_driverlist[] = {
        &_SimpleSerialDriver,
        &_FileDriver,
        NULL
    };
    
    
    
    void *CheckCS(void *argv)
    {
        pthread_set_affinity_thiscog_np();
    
        while(1)
        {
        if ((INA & (1<<4)) == 0)
                high(16);
        else
            low(16);
    
        sleep(1);
        }
    }
    
    int main(int argc, char* argv[])
    {
    
        pthread_t thr;
    
        pthread_create(&thr, NULL, CheckCS, NULL);
    
        uint8_t buffer[80];
    
    #ifdef __PROPELLER_LMM__
    #if 1
        buffer[0] = 0;
        buffer[1] = 1;
        buffer[2] = 2;
        buffer[3] = 3;
        LoadSDDriver(buffer);
    #else
        LoadSDDriver(0);
    #endif
    #endif
    
    
    
    
        while(1)
        {
        }
        return 0;
    }
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-03-02 08:08
    You need braces after the "else" in the earlier version of the CheckCS routine. The "CSset = 0" statement is always being executed in the loop independent of the value of the CD signal. BTW, Shouldn't your routine be called CheckCD instead of checkCS, and CSset should be CDset. It's a bit confusing using CS instead of CD.


    The function prototype for sleep is in unistd.h, or more precisely, it is in sys/unistd.h, ;which is included by unistd.h. You won't get a warning if the function prototype is missing unless you specify the -Wall compiler flag when you compile.
Sign In or Register to comment.