_spi_main.c
上传用户:ujshua
上传日期:2018-02-13
资源大小:1100k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

C/C++

  1. /*
  2.  * $QNXLicenseC: 
  3.  * Copyright 2007, QNX Software Systems.  
  4.  *  
  5.  * Licensed under the Apache License, Version 2.0 (the "License"). You  
  6.  * may not reproduce, modify or distribute this software except in  
  7.  * compliance with the License. You may obtain a copy of the License  
  8.  * at: http://www.apache.org/licenses/LICENSE-2.0  
  9.  *  
  10.  * Unless required by applicable law or agreed to in writing, software  
  11.  * distributed under the License is distributed on an "AS IS" basis,  
  12.  * WITHOUT WARRANTIES OF ANY KIND, either express or implied. 
  13.  * 
  14.  * This file may contain contributions from others, either as  
  15.  * contributors under the License or as licensors under other terms.   
  16.  * Please review this entire file for other proprietary rights or license  
  17.  * notices, as well as the QNX Development Suite License Guide at  
  18.  * http://licensing.qnx.com/license-guide/ for other information. 
  19.  * $
  20.  */
  21.  
  22. #ifdef __USAGE
  23. %C - SPI driver
  24. Syntax:
  25. %C [-u unit]
  26. Options:
  27. -u unit    Set spi unit number (default: 0).
  28. -d         driver module name
  29. #endif
  30. #include <sys/procmgr.h>
  31. #include <dlfcn.h>
  32. #include "proto.h"
  33. int main(int argc, char *argv[])
  34. {
  35. spi_dev_t *head = NULL, *tail = NULL, *dev;
  36. void *drventry, *dlhdl;
  37. siginfo_t info;
  38. sigset_t set;
  39. int i, c, devnum = 0;
  40. if (ThreadCtl(_NTO_TCTL_IO, 0) == -1) {
  41. perror("ThreadCtl");
  42. return (!EOK);
  43. }
  44. _spi_init_iofunc();
  45. while ((c = getopt(argc, argv, "u:d:")) != -1) {
  46. switch (c) {
  47. case 'u':
  48. devnum = strtol(optarg, NULL, 0);
  49. break;
  50. case 'd':
  51. if ((drventry = _spi_dlload(&dlhdl, optarg)) == NULL) {
  52. perror("spi_load_driver() failed");
  53. return (-1);
  54. }
  55. do {
  56. if ((dev = calloc(1, sizeof(spi_dev_t))) == NULL)
  57. goto cleanup;
  58. if (argv[optind] == NULL || *argv[optind] == '-')
  59. dev->opts = NULL;
  60. else
  61. dev->opts = strdup(argv[optind]);
  62. ++optind;
  63. dev->funcs  = (spi_funcs_t *)drventry;
  64. dev->devnum = devnum++;
  65. dev->dlhdl  = dlhdl;
  66. i = _spi_create_instance(dev);
  67. if (dev->opts)
  68. free(dev->opts);
  69. if (i != EOK) {
  70. perror("spi_create_instance() failed");
  71. free(dev);
  72. goto cleanup;
  73. }
  74. if (head) {
  75. tail->next = dev;
  76. tail = dev;
  77. }
  78. else
  79. head = tail = dev;
  80. } while (optind < argc && *(optarg = argv[optind]) != '-'); 
  81. /* 
  82.  * Now we only support one dll
  83.  */
  84. goto start_spi;
  85. break;
  86. }
  87. }
  88. start_spi:
  89. if (head) {
  90. /* background the process */
  91. procmgr_daemon(0, PROCMGR_DAEMON_NOCLOSE | PROCMGR_DAEMON_NODEVNULL);
  92. sigemptyset(&set);
  93. sigaddset(&set, SIGTERM);
  94. for (;;) {
  95. if (SignalWaitinfo(&set, &info) == -1)
  96. continue;
  97. if (info.si_signo == SIGTERM)
  98. break;
  99. }
  100. }
  101. cleanup:
  102. while (dev = head) {
  103. dispatch_unblock(dev->ctp);
  104. dispatch_destroy(dev->dpp);
  105. resmgr_detach(dev->dpp, dev->id, _RESMGR_DETACH_ALL);
  106. /*
  107.  * Disable hardware
  108.  */
  109. dev->funcs->fini(dev->drvhdl);
  110. head = dev->next;
  111. free(dev);
  112. }
  113. dlclose(dlhdl);
  114. return (EOK);
  115. }