_spi_create_instance.c
上传用户:lkkete893
上传日期:2020-06-19
资源大小:1536k
文件大小:3k
开发平台:

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. #include "proto.h"
  22. static int _spi_register_interface(void *data)
  23. {
  24. spi_dev_t *dev = data;
  25. SPIDEV *drvhdl;
  26. resmgr_attr_t rattr;
  27. char devname[PATH_MAX + 1];
  28. if ((drvhdl = dev->funcs->init(dev, dev->opts)) == NULL) {
  29. free(dev->opts);
  30. return (!EOK);
  31. }
  32. dev->drvhdl = drvhdl;
  33. /* set up i/o handler functions */
  34. memset(&rattr, 0, sizeof(rattr));
  35. rattr.nparts_max   = SPI_RESMGR_NPARTS_MIN;
  36. rattr.msg_max_size = SPI_RESMGR_MSGSIZE_MIN;
  37. iofunc_attr_init(&drvhdl->attr, S_IFCHR | 0666, NULL, NULL);
  38. drvhdl->attr.mount = &_spi_mount;
  39. /* register device name */
  40. snprintf(devname, PATH_MAX, "/dev/spi%d", dev->devnum);
  41. if (-1 == (dev->id = resmgr_attach(dev->dpp, &rattr, devname, _FTYPE_ANY, 0,
  42. &_spi_connect_funcs, &_spi_io_funcs, (void *)drvhdl))) {
  43. perror("resmgr_attach() failed");
  44. goto failed1;
  45. }
  46. resmgr_devino(dev->id, &drvhdl->attr.mount->dev, &drvhdl->attr.inode);
  47. if ((dev->ctp = dispatch_context_alloc(dev->dpp)) != NULL)
  48. return (EOK);
  49. perror("dispatch_context_alloc() failed");
  50. resmgr_detach(dev->dpp, dev->id, _RESMGR_DETACH_ALL);
  51. failed1:
  52. dev->funcs->fini(drvhdl);
  53. return (!EOK);
  54. }
  55. static void* _spi_driver_thread(void *data)
  56. {
  57. spi_dev_t *dev = data;
  58. if (_spi_register_interface(data) != EOK)
  59. return NULL;
  60. while (1) {
  61. if ((dev->ctp = dispatch_block(dev->ctp)) != NULL)
  62. dispatch_handler(dev->ctp);
  63. else
  64. break;
  65. }
  66. return NULL;
  67. }
  68. int _spi_create_instance(spi_dev_t *dev)
  69. {
  70. pthread_attr_t pattr;
  71. struct sched_param param;
  72. if (NULL == (dev->dpp = dispatch_create())) {
  73. perror("dispatch_create() failed");
  74. goto failed0;
  75. }
  76. pthread_attr_init(&pattr);
  77. pthread_attr_setschedpolicy(&pattr, SCHED_RR);
  78. param.sched_priority = 21;
  79. pthread_attr_setschedparam(&pattr, &param);
  80. pthread_attr_setinheritsched(&pattr, PTHREAD_EXPLICIT_SCHED);
  81. // Create thread for this interface
  82. if (pthread_create(NULL, &pattr, (void *)_spi_driver_thread, dev) != EOK) {
  83. perror("pthread_create() failed");
  84. goto failed1;
  85. }
  86. return (EOK);
  87. failed1:
  88. dispatch_destroy(dev->dpp);
  89. failed0:
  90. return (-1);
  91. }