Page 1 of 1

How to properly reference threads in NIL?

Posted: Sun Aug 02, 2015 3:56 am
by ulikoehler
Hi,
I'm currently porting some old ChibiOS 2.x code to 3.x with NIL to to uC flash size restrictions.

In nil.c, the standard event functions, most notably chEvtSignalI are defined for the NIL kernel.
However, there is no chThdCreateStatic() that returns a thread_t* in NIL as threads are handled via the thread table.

Therefore, my question is how to properly use chEventSignalI in NIL. My current code is:

Code: Select all

void uartTXend2Callback(UARTDriver *uartp) {
  (void)uartp;
  palClearPad(GPIOA, GPIOA_RS485_DE);
  osalSysLockFromISR();
  chEvtSignalI(&nil.threads[0], EVENT_UART_TX_FINISHED);
  chSchRescheduleS(); //Maybe unneccessary
  osalSysUnlockFromISR();
}


Before using uartSend in the thread code, I use

Code: Select all

chEvtWaitAnyTimeout(EVENT_UART_TX_FINISHED, TIME_INFINITE)


Using this method, the thread never wakes up. I am not sure if this is related to some other part of my source code, but directly using the thread table (I'm using only one thread besides main) via &nil.threads[0] seems somewhat strange to me.

Best regards, Uli

Re: How to properly reference threads in NIL?

Posted: Sun Aug 02, 2015 7:20 am
by Giovanni
Hi,

Accessing the table is fine, probably having a macro for that would be useful.

Note that you MUST NOT call S-class functions from ISRs, so that call is not just unnecessary, it is forbidden.

Giovanni

Re: How to properly reference threads in NIL?

Posted: Tue Aug 11, 2015 1:40 am
by mikenick42
I don't know if it counts as the right way to do it, but I have it working with a global thread pointer and then assigning the result of chThdGetSelfX() to that in the listener.

Re: How to properly reference threads in NIL?

Posted: Tue Aug 11, 2015 1:46 am
by ulikoehler
@mikenick42: Nice idea ;-)

Besides being a bit ugly, &nil.threads[0] works well. Maybe we should define a function for that (code has not been checked, might contain errors)?

Code: Select all

static inline thread_t* nilGetThreadByIndex(int n) {
    osalDbgCheck(n < NIL_CFG_NUM_THREADS && n > 0);
    return &nil.threads[n];
}