This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Feb 14th, 2013  |  syntax: C  |  size: 3.64 KB  |  views: 5  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. static uint32_t microSD_stack[DEFAULT_STACK_SIZE / sizeof(uint32_t)];
  2. static struct sigaction signalHandler;
  3. static struct semaphore event_trigger;
  4. //static enum {
  5. //    STATE_NOT_PRESENT,
  6. //    STATE_PRESENT,
  7. //    STATE_UNKNOWN,
  8. //    
  9. //}
  10. static bool microSD_state = false;
  11.  
  12. bool mount_microSD(void) {
  13.     int ret = mount("/dev/mmcblk0p1", "/mnt/mmc", "vfat", MS_MGC_VAL | MS_SYNCHRONOUS | MS_RELATIME, "iocharset=utf8");
  14.     if (ret < 0) {
  15.         return false;
  16.     }
  17.     return true;
  18. }
  19.  
  20. bool umount_microSD(void) {
  21.     int ret = umount2("/mnt/mmc", MNT_FORCE);
  22.     if (ret < 0) {
  23.         return false;
  24.     }
  25.     return true;
  26. }
  27.  
  28. bool microSD_inserted(void) {
  29.     /* Active LOW */
  30.     return (!gpio_control(DEV_CTRL_GPIO_IS_HIGH, GPIO_SD_SENSE, 0, 0));
  31. }
  32.  
  33. static void microSD_check(int signal)
  34. {
  35. //        printf("signal received!");
  36. //    if (signal == SIGUSR1) {
  37.     semaphore_release(&event_trigger);
  38. //        printf("wait unlocked");
  39. //    }
  40. }
  41.  
  42. static void NORETURN_ATTR microSD_thread(void)
  43. {
  44.     while (true) {
  45.         /* Indefinitely wait for an event */
  46.         printf("Released! Checking...\n");
  47.         if (microSD_state != microSD_inserted()) {
  48.             /* Something has changed */
  49.             microSD_state = microSD_inserted();
  50.             /* Wait for the kernel to load microSD device */
  51.             if (microSD_state) {
  52.                 printf("microsd inserted\n");
  53.                 /* Loading microSD takes some time */
  54.                 sleep(HZ);
  55.                 mount_microSD();
  56.                 printf("microSD mounted\n");
  57.                 queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
  58.             }
  59.             else {
  60.             queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
  61.                 printf("microSD removed\n");
  62.                 umount_microSD();
  63.                 printf("microSD umounted\n");
  64.                
  65.             }
  66.  
  67.             queue_broadcast(SYS_FS_CHANGED, 0);
  68.         }
  69.        
  70.         semaphore_wait(&event_trigger, TIMEOUT_BLOCK);
  71.        
  72.     }
  73. }
  74.  
  75.    
  76. /*    
  77.     bool microsd_init = false;
  78.     if (microSD_inserted()) {
  79.         if (!microSD_state) {
  80.             queue_broadcast(SYS_HOTSWAP_INSERTED, 0);
  81.             backlight_on();
  82.  
  83.             microsd_init = mount_microSD();
  84.             microSD_state = true;
  85.         }
  86.     }
  87.     else {
  88.         if (microSD_state) {
  89.             queue_broadcast(SYS_HOTSWAP_EXTRACTED, 0);
  90.             backlight_on();
  91.             microsd_init = umount_microSD();
  92.             microSD_state = false;
  93.         }
  94.     }
  95.     if (microsd_init)
  96.         queue_broadcast(SYS_FS_CHANGED, 0);
  97.     sleep(HZ);
  98. }
  99. */
  100.  
  101. void sdsense_init(void) {
  102.     /* Setup GPIO pin for microSD sense */
  103.     gpio_control(DEV_CTRL_GPIO_SET_MUX, GPIO_SD_SENSE, CONFIG_DEFAULT, 0);
  104.     gpio_control(DEV_CTRL_GPIO_SET_INPUT, GPIO_SD_SENSE, CONFIG_DEFAULT, 0);
  105.     microSD_state = microSD_inserted();
  106.    
  107.     /* Set up the handler, may be invoked also without a hotplug event
  108.      * (very unlikely on a "single-process" environment)
  109.      */
  110.    
  111.     semaphore_init(&event_trigger, 1, 0);
  112.        
  113.     signalHandler.sa_handler = microSD_check;
  114.     signalHandler.sa_flags = 0;
  115.     sigemptyset(&signalHandler.sa_mask);
  116.  
  117.     create_thread(microSD_thread, microSD_stack, sizeof(microSD_stack), 0, "microSD"
  118.                   IF_PRIO(, PRIORITY_BACKGROUND) IF_COP(, CPU));
  119.    
  120.     /* Now that we are ready, set the in-kernel call */
  121.     char temp[100];
  122.     sprintf(temp, "echo kill -s SIGUSR1 %i > /proc/sys/kernel/hotplug", getpid());
  123.     system(temp);
  124.    
  125.     /**/
  126.     int err = sigaction(SIGUSR1, &signalHandler, NULL);
  127.     printf("Setup done! %i\n", err);
  128. }
clone this paste RAW Paste Data