Using resumable functions with RTOS (ThreadX) #1185
-
Hello, I'm currently looking into device drivers (specifically I2C EEPROM) and I see that you are using Protothreads. Basically what happens is the I2C Master processes a transaction through ISR (which is a nice implementation), but in parallel the protothread basically busy waits for the transaction to finish (because my other threads are ThreadX threads and not protothreads). ThreadX supports time slicing, so my code works, but if I'd be able to actually use ThreadX primitives to wait for the transaction to finish (event flags), ThreadX would not need to do time slicing but instead knows that the IO thread is still blocked. I'm not sure how hard it is to implement this and how to go about it. I'd appreciate some input! Thank you so much for this great project! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
My current solution is like this (but this way I cannot use I2cDevice and therefore also not the modm-provided device drivers), but it at least works well: I wrap the I2cTransaction and provide a blocking wait function: namespace modm {
template <class Transaction> requires std::derived_from<Transaction, I2cTransaction>
class TxI2cTransaction : public Transaction {
public:
explicit TxI2cTransaction(uint8_t address) : Transaction(address) {
tx_semaphore_create(&sema, "i2c_sema", sizeof(sema));
}
virtual ~TxI2cTransaction() { tx_semaphore_delete(&sema); }
void detaching(I2cTransaction::DetachCause cause) override {
Transaction::detaching(cause);
tx_semaphore_put(&sema);
}
bool isBusy() {
blockingWait();
return Transaction::isBusy();
}
void blockingWait() { tx_semaphore_get(&sema, TX_WAIT_FOREVER); }
private:
TX_SEMAPHORE sema{};
};
} // namespace modm Then I use the modm::TxI2cTransaction<modm::I2cWriteReadTransaction> tx{EEPROM_ADDRESS};
u_int8_t address = MEMORY_ADDRESS;
tx.configureWriteRead(&address, 1, buf, sizeof(buf));
Board::id::I2C::start(&tx);
tx.blockingWait();
// Here I have my result in the buffer and can check the success using the tx. I feel like there is a better solution though |
Beta Was this translation helpful? Give feedback.
I'm afraid modm doesn't support yield hinting in any way, it's purely based on polling. modm was never meant to be used in an RTOS, since most RTOS have their own HAL. You can only hack the I2C implementation to add the semaphore into the driver.
Does ThreadX not have it's own HAL for STM32H7? modm doesn't support a lot of the advanced peripherals on the H7.
It's something I want to change with the stackful fiber scheduler, but progress is slow.
For FreeRTOS, there recently was a PR that added FreeRTOS specialization for UART buffers, something like this would be needed for other RTOS as well.