pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


Quantron fix rtprio patch
Posted by Anonymous on Tue 22nd Oct 2019 16:41
raw | new post

  1. diff --git a/ptxdist/local_src/ecu01-codesys/io_driver/ecu01.cpp b/ptxdist/local_src/ecu01-codesys/io_driver/ecu01.cpp
  2. index 4148859..bf56618 100644
  3. --- a/ptxdist/local_src/ecu01-codesys/io_driver/ecu01.cpp
  4. +++ b/ptxdist/local_src/ecu01-codesys/io_driver/ecu01.cpp
  5. @@ -144,7 +144,7 @@ void EcuDriverInit(char *pcDeviceName, int iInputCnt, int iOutputCnt)
  6.        ready = false;
  7.     }
  8.  
  9. -   threadHdlCC = CAL_SysTaskCreate( (char*)threadHdlCCName, threadCC, NULL, TASKPRIO_SYSTEM_BASE+42, 0, 0, NULL, &rtsResult);
  10. +   threadHdlCC = CAL_SysTaskCreate( (char*)threadHdlCCName, threadCC, NULL, TASKPRIO_SYSTEM_BASE+6, 0, 0, NULL, &rtsResult);
  11.     if ((threadHdlCC == RTS_INVALID_HANDLE) || (rtsResult != ERR_OK)) {
  12.        /* error */
  13.        ready = false;
  14. diff --git a/ptxdist/local_src/ecu01-codesys/io_driver/ecu01softcompcap.cpp b/ptxdist/local_src/ecu01-codesys/io_driver/ecu01softcompcap.cpp
  15. index 8dea629..7085534 100644
  16. --- a/ptxdist/local_src/ecu01-codesys/io_driver/ecu01softcompcap.cpp
  17. +++ b/ptxdist/local_src/ecu01-codesys/io_driver/ecu01softcompcap.cpp
  18. @@ -31,6 +31,7 @@
  19.  #include "ecu01softcompcap.h"
  20.  #include <time.h>
  21.  #include <limits.h>
  22. +#include <stdlib.h>
  23.  #include <stdio.h>
  24.  #include <string.h>
  25.  
  26. @@ -255,11 +256,15 @@ Ecu01SoftCompCap::Ecu01SoftCompCap():
  27.     _callback120(120),
  28.     _statusFlag(1)
  29.  {
  30. -   int prio = sched_get_priority_max(SCHED_FIFO);
  31. +   /*
  32. +    * Note: MAX_USER_RT_PRIO is 100, while sched_get_priority_max(SCHED_FIFO)
  33. +    * in userland returns 99
  34. +    */
  35. +   int softcc_prio = sched_get_priority_max(SCHED_FIFO)-6;
  36.     struct sched_param param;
  37.  
  38.     (void)memset(&param, 0, sizeof(param));
  39. -   param.sched_priority = prio;
  40. +   param.sched_priority = softcc_prio;
  41.    
  42.     if( _gpio120.isOpen() ){
  43.        if( _gpio120.guardGpioChange( &_callback120, 1000 ) ){
  44. @@ -269,6 +274,19 @@ Ecu01SoftCompCap::Ecu01SoftCompCap():
  45.           }
  46.        }
  47.     }
  48. +
  49. +   /*
  50. +    * Bump priority of the gpiolib irq handling thread.
  51. +    *
  52. +    * It seems there is no way to force individual gpios their own
  53. +    * threads with a different realtime priority, so we have
  54. +    * to do this here, globally for all gpios.
  55. +    *
  56. +    * WARNING: This is very likely causing trouble elsewhere,
  57. +    * because now ALL gpiolib activity runs with a glibgpio
  58. +    * irq handling thread with priority 97!!
  59. +    */
  60. +   (void)system("x=$(/usr/bin/pgrep 'irq/.+-gpiolib') && /usr/bin/chrt -f -p 97 \"$x\"");
  61.  }
  62.  
  63.  // ****************************************************************************
  64. diff --git a/ptxdist/local_src/ecu01-comp-cap/main.c b/ptxdist/local_src/ecu01-comp-cap/main.c
  65. index 1fe9651..787565b 100644
  66. --- a/ptxdist/local_src/ecu01-comp-cap/main.c
  67. +++ b/ptxdist/local_src/ecu01-comp-cap/main.c
  68. @@ -47,7 +47,11 @@
  69.  #include <linux/seq_file.h>
  70.  #include <linux/cdev.h>
  71.  #include <linux/spinlock.h>
  72. -
  73. +#include <linux/irq.h>
  74. +#include <linux/irqdesc.h>
  75. +#include <linux/interrupt.h>
  76. +#include <linux/sched.h>
  77. +#include <linux/sched/rt.h>
  78.  //#include <asm/system.h>         /* cli(), *_flags */
  79.  #include <asm/uaccess.h>        /* copy_*_user */
  80.  
  81. @@ -177,6 +181,85 @@ int comp_cap_release(struct inode *inode, struct file *filp)
  82.     return 0;
  83.  }
  84.  
  85. +static
  86. +struct task_struct *irq_desc_to_irq_thread_task_struct(struct irq_desc *irq_desc)
  87. +{
  88. +       if (irq_desc && irq_desc->action)
  89. +       {
  90. +               /*
  91. +                * Note: |irq_desc->action| can be chained if the irq
  92. +                * is a shared one. Maybe we should place a fatal assert
  93. +                * for that case since we do not implement this here.
  94. +                */
  95. +               return irq_desc->action->thread;
  96. +       }
  97. +
  98. +       return NULL;
  99. +}
  100. +
  101. +/*
  102. + * 1. Note: MAX_USER_RT_PRIO is 100, while sched_get_priority_max(SCHED_FIFO)
  103. + *   in userland returns 99
  104. + * 2. The kernel thread "posixcputmr/0" must run with rtprio 99, and we
  105. + *   MUST NOT interfere with it!!
  106. + */
  107. +#define ECU01_CC_IRQ_THREAD_RTPRIO (MAX_USER_RT_PRIO-5)
  108. +
  109. +/*
  110. + * set scheduler paramters of the compare capture interrupt threads
  111. + *
  112. + * Note: Realtime priorities of the compare capture interrupt
  113. + * threads can be monitored with
  114. + * $ ps -eT -o s,tid,pid,cls,pri,rtprio,comm,time | egrep 'irq/.+CC' #
  115. + *
  116. + * ToDo: Failure to set the parameters should be fatal for the
  117. + * |open()| syscall which calls this function!
  118. + */
  119. +static
  120. +void set_cc_irq_thread_scheduler_parameters(struct comp_cap_dev_priv *priv_dev)
  121. +{
  122. +       struct irq_desc *irq_desc;
  123. +       bool set_sched_success = false;
  124. +       struct task_struct *ts;
  125. +       struct sched_param param;
  126. +       (void)memset(&param, 0, sizeof(param));
  127. +       param.sched_priority = ECU01_CC_IRQ_THREAD_RTPRIO;
  128. +
  129. +       irq_desc = irq_to_desc(priv_dev->IRQ);
  130. +       if (!irq_desc)
  131. +       {
  132. +               /* This can fail with certain kernel CONFIG_* settings */
  133. +               (void)printk(KERN_EMERG "%s:%d: %s\n",
  134. +                       __func__, __LINE__, "irq_to_desc() failed.");
  135. +               return;
  136. +       }
  137. +
  138. +       ts = irq_desc_to_irq_thread_task_struct(irq_desc);
  139. +       if (ts)
  140. +       {
  141. +               if (sched_setscheduler(ts, SCHED_FIFO, &param) == 0)
  142. +                       set_sched_success = true;
  143. +       }
  144. +
  145. +       /*
  146. +        * Be very verbose&loud about setting the scheduler class
  147. +        * and priority, since the application will not work correctly
  148. +        * if we fail to do this.
  149. +        *
  150. +        * ToDo: Propagate an error to the caller, so that a device
  151. +        * |open()| FAILS.
  152. +        */
  153. +       (void)printk(KERN_EMERG "ecu01-compcap: "
  154. +               "irq=%ld/%ld, %s interrupt thread pid=%ld to '%s'/rtprio=%ld\n",
  155. +               (long)irq_desc->irq_data.irq,
  156. +               (long)priv_dev->IRQ,
  157. +               (set_sched_success?"changed":"failed to change"),
  158. +               (long)(ts?ts->pid:-1),
  159. +               "SCHED_FIFO",
  160. +               (long)param.sched_priority);
  161. +}
  162. +
  163. +
  164.  int start_comp_cap( struct comp_cap_dev *dev, int enable )
  165.  {
  166.     int res = -1;
  167. @@ -188,6 +271,8 @@ int start_comp_cap( struct comp_cap_dev *dev, int enable )
  168.  
  169.     res = request_irq(priv_dev->IRQ, cc_isr, IRQF_DISABLED, dev->NAME , priv_dev );
  170.  
  171. +   set_cc_irq_thread_scheduler_parameters(priv_dev);
  172. +
  173.     cc_restart(priv_dev);
  174.  
  175.     priv_dev->clk = clk_get_sys( priv_dev->CLK_PROVIDER, priv_dev->CLK_PARENT);

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}





All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at