Become a leader in the IoT community!

New DevHeads get a 320-point leaderboard boost when joining the DevHeads IoT Integration Community. In addition to learning and advising, active community leaders are rewarded with community recognition and free tech stuff. Start your Legendary Collaboration now!

Step 1 of 5

CREATE YOUR PROFILE *Required

OR
Step 2 of 5

WHAT BRINGS YOU TO DEVHEADS? *Choose 1 or more

Collaboration & Work 🤝
Learn & Grow 📚
Contribute Experience & Expertise 🔧
Step 3 of 5

WHAT'S YOUR INTEREST OR EXPERTISE? *Choose 1 or more

Hardware & Design 💡
Embedded Software 💻
Edge Networking
Step 4 of 5

Personalize your profile

Step 5 of 5

Read & agree to our COMMUNITY RULES

  1. We want this server to be a welcoming space! Treat everyone with respect. Absolutely no harassment, witch hunting, sexism, racism, or hate speech will be tolerated.
  2. If you see something against the rules or something that makes you feel unsafe, let staff know by messaging @admin in the "support-tickets" tab in the Live DevChat menu.
  3. No age-restricted, obscene or NSFW content. This includes text, images, or links featuring nudity, sex, hard violence, or other graphically disturbing content.
  4. No spam. This includes DMing fellow members.
  5. You must be over the age of 18 years old to participate in our community.
  6. Our community uses Answer Overflow to index content on the web. By posting in this channel your messages will be indexed on the worldwide web to help others find answers.
  7. You agree to our Terms of Service (https://www.devheads.io/terms-of-service/) and Privacy Policy (https://www.devheads.io/privacy-policy)
By clicking "Finish", you have read and agreed to the our Terms of Service and Privacy Policy.

Zephyr and userspace

Hi guys, using zephyr rtos, someone know the best way to toggle a gpio pin, from a thdead that runs in user mode?
Thank you very much!

  1. melta101#0
  2. melta101#0
  3. melta101#0

    If you have any other question, i would love to help

  4. sbrok82#0

    Thank you very much!
    I know how to start threads, define gpios, blink them, etc… using supervisor mode threads (the default), but when I try with user mode thread I am facing an hardfault.

  5. melta101#0

    which HW are you using?

  6. sbrok82#0

    Stm32f401re

  7. melta101#0

    Hey, sorry for the ghost.
    Could you tell me the flow of your code if possible?

  8. sbrok82#0

    I think i can prepare something to share, but not today, sorry and thank you again for your interest

  9. sbrok82#0

    I found 1 minute to prepare a minimal try that goes in hardfault
    usermode_test_1.zip

  10. melta101#0

    is this a supported board?

  11. sbrok82#0

    Don’t know

  12. melta101#0

    how were you building it up until now?

  13. sbrok82#0

    Without config_usermode=y and without K_USER option to the thread

  14. melta101#0

    sorry i wasnt clear enough
    i mean to say
    which west cmd for building the project

  15. sbrok82#0

    Oh sorry

  16. melta101#0

    something like
    west build -p always -b .

  17. sbrok82#0

    Yup

  18. melta101#0

    i want the board snippit for that

  19. sbrok82#0

    -b nucleo_f401re

  20. sbrok82#0

    Sorry i need to go now, i can be on again in about 2h

  21. melta101#0

    Cool,
    lemme try it out
    till then

  22. sbrok82#0

    Hi melta, have you made any attempts?

  23. melta101#0

    dont have an stm with me right now, thought i had one in office. will try to get one in another 8-10 hr

    but from my guess, it feel like the hardfault occurs because it is accessing something with higher privileges

    if you have a debugger, could you check what are the value of the FSR?(atleast with that, we can find which area is the prob, whether it is memfault, hardfault etc )

  24. melta101#0

    also, have you raised this on the stm chat on zephyrproject?

  25. sbrok82#0

    I can.
    In my opinion i need to place the gpio descriptor in a memory section that have user access and/or give the user access to the memory area in which the gpio registers are mapped… But i am new in user space and searching for a guidance…

  26. sbrok82#0

    I wrote in memory protection channel…

  27. sbrok82#0

    Just forwarded in stm32 channel!

  28. melta101#0

    https://docs.zephyrproject.org/apidoc/latest/group__usermode__apis.html#ga6a2dae4c6dc6959d8785ff1924b1b424

    i think you are correct, we will need to grant it some privileges of any kernel object that it needs to use

  29. melta101#0

    maybe that’s why the hardfault happens because the user thread is being denied access to GPIO API

  30. sbrok82#0

    Anyway I read that in usermode threads can use syscalls… And gpio api should be syscalls… 😵‍💫

  31. sbrok82#0

    Compiling… Then I’ll attach the hardfault status

  32. sbrok82#0

    image.pngimage.png

  33. melta101#0

    yep,
    it’s a memfault

  34. melta101#0

    seems to be the GPIO

  35. sbrok82#0

    i am pretty sure that is the GPIO

  36. sbrok82#0

    unfortunately can`t find a sample for that

  37. melta101#0

    k_object_access_all_grant
    use this instead

  38. melta101#0

    the access grant should come from supervisor thread. and as we havnt declared a supervisor thread
    we cant directly declare it to access GPIO

  39. melta101#0

    if we want it to have access, we would need to statically give teh access

  40. melta101#0

    so instead of k_thread_create we need K_THREAD_DEFINE

  41. melta101#0

    and instead of k_object_access_grant, we need K_THREAD_ACCESS_GRANT

  42. sbrok82#0

    the main thread isn`t a supervisor thread?

  43. sbrok82#0

    same behaviour

  44. melta101#0

    @32bitwidesaviour could you give your insights here?

  45. sbrok82#0

    Here are my tries:
    https://github.com/silvio-vallorani-zephyr-portfolio/usermode_gpio_test/ -> 3 branches, one for each approach

  46. melta101#0

    i dont think dbg_pin1 is a kernel object, because the parameter accepts a void, it’s taking anything.
    leading to no error during compilation

  47. melta101#0

    https://github.com/zephyrproject-rtos/zephyr/blob/main/scripts/build/gen_kobject_list.py

    we can use this script to list all possible k_object present in the elf

  48. melta101#0
  49. sbrok82#0

    if I remove the k_object_access_grant(&dbg_pin1, usermode_thread); the program goes in

    static inline int z_vrfy_gpio_port_toggle_bits(const struct device *port, gpio_port_pins_t pins)
    {
    K_OOPS(K_SYSCALL_DRIVER_GPIO(port, port_toggle_bits)); <-- and finally in /* LCOV_EXCL_START */ FUNC_NORETURN __weak void arch_system_halt(unsigned int reason) { ARG_UNUSED(reason); /* TODO: What's the best way to totally halt the system if SMP * is enabled? */ (void)arch_irq_lock(); for (;;) { /* Spin endlessly */ } } with reason == 3

  50. melta101#0

    from the driver side
    i can see that when CONFIG_USERSPACE is present in prj.conf,
    it includes gpio_handlers.c
    which lets the user use the gpio

  51. melta101#0

    hmm

  52. melta101#0

    i think i found a hint

  53. sbrok82#0

    yup I follow his suggestion but I noticed that there is a permission error

  54. sbrok82#0

    solved. thank you very much for your time!

  55. melta101#0

    Could you share your finding?

  56. sbrok82#0

    https://github.com/silvio-vallorani-zephyr-portfolio/usermode_gpio_test/

    This is a functioning sample
    I need to add CONFIG_MPU=y to prj.conf and grant access for user thread to the gpio “port” object

    I tried to grant gpio.port before, but without CONFIG_MPU=y… resulting in hardfault

    Then i add CONFIG_MPU=y but grant access to gpio object… resulting in permission error

    Finally I tried with both and boom

    Anyway i need to use CONFIG_NO_OPTIMIZATIONS=y to debug step by step inside the syscalls…

  57. melta101#0

    great
    It was a learning to me too!

    Maybe i should also start using userspace style programming

CONTRIBUTE TO THIS THREAD

Browse other questions tagged 

Leaderboard

RANKED BY XP

All time
  • 1.
    Avatar
    @Nayel115
    1620 XP
  • 2.
    Avatar
    @UcGee
    650 XP
  • 3.
    Avatar
    @melta101
    600 XP
  • 4.
    Avatar
    @lifegochi
    250 XP
  • 5.
    Avatar
    @Youuce
    180 XP
  • 6.
    Avatar
    @hemalchevli
    170 XP