Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Zephyr #51

Open
waseemR02 opened this issue Aug 24, 2023 · 4 comments
Open

Support for Zephyr #51

waseemR02 opened this issue Aug 24, 2023 · 4 comments

Comments

@waseemR02
Copy link

I am working on programming blackpill_f401cc (WeAct) as a daughter board for Pixhawk. I was using mcp2515 for can interface and Zephyr as the RTOS. How do I go about implementing libcanard there?

@adolfogc
Copy link
Contributor

adolfogc commented Aug 27, 2023

Hi,

How did you connect the controller to the blackpill? It seems like SPI1 is the only available device for this board [1].

Some steps you probably need to accomplish:

  • Define a shield overlay [2] that uses the available SPI device. There are some examples in the Zephyr source tree that you can use as a base [3].
  • Enable the existing Zephyr driver for the MCP2515 [4][5].
  • Implement the "TxRX step" in which you update your Canard instance by using that Zephyr driver's interface; see, for example, how the SocketCAN driver in this repo is used [6].

Pointers:

  1. https://docs.zephyrproject.org/latest/boards/arm/blackpill_f401cc/doc/index.html
  2. https://docs.zephyrproject.org/latest/hardware/porting/shields.html
  3. https://github.com/zephyrproject-rtos/zephyr/tree/main/boards/shields/mcp2515
  4. https://docs.zephyrproject.org/latest/kernel/drivers/index.html
  5. https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/can
  6. static void processTxRxOnce(SocketCANInstance *socketcan, int32_t timeout_msec)

Hope this helps.

@waseemR02
Copy link
Author

I defined the mcp2515 under spi1 in device tree and used the existing shield example to write a c program for loopback. At this point, I didn't really test whether it works because i shifted my focus to dronecan as that's what I intend to use at the end to interact with pixhawk. This is where I don't know how to move forward. I have asked in the zephyr community and someone suggested to just write a driver for it. Keeping in mind i have never written any driver, though I m not against writing if I can write it. But then for this I would need to adapt the mcp2515 driver and write it for libcanard.

@waseemR02
Copy link
Author

waseemR02 commented Aug 31, 2023

As far as i understand I would need to read some can frame using the existing driver for mcp2515 and push it to the rx queue of libcanard and then pop a tx frame from tx queue and send it to the device. All this would still need the library.

@adolfogc
Copy link
Contributor

adolfogc commented Aug 31, 2023

Roughly, yes. Note that as per libcanard's internal documentation [1]:

... Libcanard does not interact with the underlying platform drivers directly, but does so via the application. Therefore, there is no need in a dedicated porting guide. ...

So you don't need a driver specifically coded for libcanard. Instead, in your application you use both the CAN driver's interface and libcanard's interface. Also, it isn't required to implement it as a TxRx function, you should code it as it best fits your own design. For example, you could implement the transmit and receive logic in separate functions.

I suggest that you first try to run the sample provided by Zephyr [2], and confirm that your current setup works. Also read Zephyr's CAN documentation, which provides some examples [3].

Finally, I can also point you to an example I had written a while ago, and that uses libcanard and the STM32 driver bundled with libcanard, in hopes that it may give you further ideas [4][5][6]. The code is designed to work with a different kernel+framework (qpc), however.

--

  1. https://github.com/dronecan/libcanard/tree/master/drivers
  2. https://github.com/zephyrproject-rtos/zephyr/blob/main/samples/drivers/can/counter/src/main.c
  3. https://docs.zephyrproject.org/latest/hardware/peripherals/canbus/controller.html
  4. https://github.com/adolfogc/stg-8nn-scaffold/blob/main/bsp/stg/src/bsp_can.c
  5. https://github.com/adolfogc/stg-8nn-scaffold-example/blob/master/blinky/uavcan_override.c
  6. https://github.com/adolfogc/stg-8nn-scaffold/blob/main/app/uavcan_node/uavcan_node_helpers.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants