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

stm32/wb55: Add usb/bluetooth transparent mode to stm32wb55. #7703

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

andrewleech
Copy link
Sponsor Contributor

This PR allows the usage of a stm32wb55 board (eg. nucleo or dongle) as a USB/UART Bluetooth HCI Dongle. This allows it to be used to provide bluetooth functionality with the unix micropython port.

This functionality was written by @jimmo, I've sinply debugged to resolve some glitches and submitted it here.

This will disable normal BLE Stack usage and allow forwarding all HCI from stdin to BLE code.

To enable, build with the following define.
#define MICROPY_HW_STM32WB_TRANSPARENT_MODE (1)

Alternatively, it can be built with

cd micropython/ports/stm32
make CFLAGS_EXTRA="-DMICROPY_HW_STM32WB_TRANSPARENT_MODE=1" BOARD=USBDONGLE_WB55'",

When running on the board, it can be started with:

import stm
stm.rfcore_transparent()

or this can be added to main.py to make it start automatically.

#define MICROPY_HW_STM32WB_TRANSPARENT_MODE (1)

This will disable normal BLE Stack usage and allow forwarding all HCI from stdin to BLE code.
@codecov-commenter
Copy link

Codecov Report

Merging #7703 (63d67ca) into master (ed42002) will increase coverage by 0.00%.
The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #7703   +/-   ##
=======================================
  Coverage   98.25%   98.26%           
=======================================
  Files         154      154           
  Lines       20071    20072    +1     
=======================================
+ Hits        19721    19723    +2     
+ Misses        350      349    -1     
Impacted Files Coverage Δ
py/objtype.c 100.00% <0.00%> (ø)
py/obj.c 97.61% <0.00%> (+0.39%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ed42002...63d67ca. Read the comment docs.

@andrewleech andrewleech force-pushed the wb55-transparent-mode branch 2 times, most recently from 49ccc27 to 12055c7 Compare August 25, 2021 05:02
Copy link
Member

@jimmo jimmo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @andrewleech for putting this together and sorting out the remaining issues!

@dpgeorge I'm not sure if MICROPY_HW_STM32WB_TRANSPARENT_MODE is the right naming for this config option..wdyt?

Note: Without supporting the vendor HCI payloads it won't work with STM CubeMonitorRF, but that's something we can add in a future PR.

@@ -261,9 +264,7 @@ void ipcc_init(uint32_t irq_pri) {
// Enable receive IRQ on the BLE channel.
LL_C1_IPCC_EnableIT_RXO(IPCC);
LL_C1_IPCC_DisableReceiveChannel(IPCC, LL_IPCC_CHANNEL_1 | LL_IPCC_CHANNEL_2 | LL_IPCC_CHANNEL_3 | LL_IPCC_CHANNEL_4 | LL_IPCC_CHANNEL_5 | LL_IPCC_CHANNEL_6);
#if !MICROPY_HW_STM32WB_TRANSPARENT_MODE
LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be only enabled if NimBLE is being used. I think probably the simplest way to do this is to move this call to the start of rfcore_ble_init (at which point you'll be able to check the state of mp_bluetooth_nimble_ble_state).

I think down the track I'd probably make it so the caller of rfcore_ble_init (whcih is currently either transparent mode or hci uart init) pass a use_irqs bool or something, but what you have seems fine for now.

It would be worth adding a check to stm.rfcore_transparent() to early-fail if NimBLE is started too.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was originally going to add an arg to rfcore_ble_init for this however it'd still need a (new?) global variable to know whether to allow/block the irq re-enable in:

STATIC void tl_check_msg(volatile tl_list_node_t *head, unsigned int ch, parse_hci_info_t *parse) {
        ...
        if (ch == IPCC_CH_BLE && (mp_bluetooth_nimble_ble_state != MP_BLUETOOTH_NIMBLE_BLE_STATE_OFF)) {
            // Renable IRQs for BLE now that we've cleared the flag.
            LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE);
        }
}

as such re-using mp_bluetooth_nimble_ble_state is an efficient one to use, rather than creating something new.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've moved LL_C1_IPCC_EnableReceiveChannel(IPCC, IPCC_CH_BLE); as suggested in latest push

@@ -589,6 +588,7 @@ static const struct {
void rfcore_ble_init(void) {
DEBUG_printf("rfcore_ble_init\n");


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line added.

Copy link
Sponsor Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@andrewleech
Copy link
Sponsor Contributor Author

Todo: implement extra interfaces (custom hci commands) to enable compatibility with stm32cubemonitor https://github.com/STMicroelectronics/STM32CubeWB/blob/master/Projects/P-NUCLEO-WB55.Nucleo/Applications/BLE/BLE_TransparentMode/STM32_WPAN/App/tm.c

@dpgeorge
Copy link
Member

Closing in favour of #9046.

@projectgus
Copy link
Contributor

This is an automated heads-up that we've just merged a Pull Request
that removes the STATIC macro from MicroPython's C API.

See #13763

A search suggests this PR might apply the STATIC macro to some C code. If it
does, then next time you rebase the PR (or merge from master) then you should
please replace all the STATIC keywords with static.

Although this is an automated message, feel free to @-reply to me directly if
you have any questions about this.

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

Successfully merging this pull request may close these issues.

None yet

6 participants