8000 stm32/nimble: Add nimble bindings. · andrewleech/micropython@57d08b0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 57d08b0

Browse files
committed
stm32/nimble: Add nimble bindings.
1 parent cba39ec commit 57d08b0

File tree

9 files changed

+750
-0
lines changed

9 files changed

+750
-0
lines changed

ports/stm32/nimble/bsp/bsp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

ports/stm32/nimble/hal/hal_gpio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty

ports/stm32/nimble/hal/hal_uart.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef MICROPY_INCLUDED_STM32_NIMBLE_HAL_HAL_UART_H
2+
#define MICROPY_INCLUDED_STM32_NIMBLE_HAL_HAL_UART_H
3+
4+
#include <stdint.h>
5+
6+
#define SYSINIT_PANIC_ASSERT_MSG(cond, msg)
7+
8+
#define HAL_UART_PARITY_NONE (0)
9+
10+
typedef int (*hal_uart_tx_cb_t)(void *arg);
11+
typedef int (*hal_uart_rx_cb_t)(void *arg, uint8_t data);
12+
13+
int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_uart_rx_cb_t rx_cb, void *rx_arg);
14+
int hal_uart_config(uint32_t port, uint32_t baud, uint32_t bits, uint32_t stop, uint32_t parity, uint32_t flow);
15+
void hal_uart_start_tx(uint32_t port);
16+
int hal_uart_close(uint32_t port);
17+
18+
#endif // MICROPY_INCLUDED_STM32_NIMBLE_HAL_HAL_UART_H

ports/stm32/nimble/hci_uart.c

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "pin_static_af.h"
30+
#include "uart.h"
31+
#include "nimble/ble.h"
32+
#include "hal/hal_uart.h"
33+
#include "cywbt.h"
34+
35+
#if MICROPY_BLUETOOTH_NIMBLE
36+
37+
/******************************************************************************/
38+
// Bindings CYWBT to Nimble
39+
40+
static hal_uart_tx_cb_t hal_uart_tx_cb;
41+
static void *hal_uart_tx_arg;
42+
static hal_uart_rx_cb_t hal_uart_rx_cb;
43+
static void *hal_uart_rx_arg;
44+
45+
static uint32_t bt_sleep_ticks;
46+
47+
int hal_uart_init_cbs(uint32_t port, hal_uart_tx_cb_t tx_cb, void *tx_arg, hal_uart_rx_cb_t rx_cb, void *rx_arg) {
48+
hal_uart_tx_cb = tx_cb;
49+
hal_uart_tx_arg = tx_arg;
50+
hal_uart_rx_cb = rx_cb;
51+
hal_uart_rx_arg = rx_arg;
52+
return 0; // success
53+
}
54+
55+
int hal_uart_config(uint32_t port, uint32_t baud, uint32_t bits, uint32_t stop, uint32_t parity, uint32_t flow) {
56+
cywbt_init();
57+
cywbt_activate();
58+
return 0; // success
59+
}
60+
61+
void hal_uart_start_tx(uint32_t port) {
62+
size_t len = 0;
63+
for (;;) {
64+
int data = hal_uart_tx_cb(hal_uart_tx_arg);
65+
if (data == -1) {
66+
break;
67+
}
68+
cywbt_hci_cmd_buf[len++] = data;
69+
}
70+
71+
#if 0
72+
printf("[% 8d] BTUTX: %02x", mp_hal_ticks_ms(), hci_cmd_buf[0]);
73+
for (int i = 1; i < len; ++i) {
74+
printf(":%02x", hci_cmd_buf[i]);
75+
}
76+
printf("\n");
77+
#endif
78+
79+
bt_sleep_ticks = mp_hal_ticks_ms();
80+
if (mp_hal_pin_read(pyb_pin_BT_DEV_WAKE) == 1) {
81+
//printf("BT WAKE for TX\n");
82+
mp_hal_pin_low(pyb_pin_BT_DEV_WAKE); // wake up
83+
mp_hal_delay_ms(5); // can't go lower than this
84+
}
85+
86+
uart_tx_strn(&cywbt_hci_uart_obj, (void*)cywbt_hci_cmd_buf, len);
87+
}
88+
89+
int hal_uart_close(uint32_t port) {
90+
return 0; // success
91+
}
92+
93+
void nimble_uart_process(void) {
94+
int host_wake = mp_hal_pin_read(pyb_pin_BT_HOST_WAKE);
95+
/*
96+
// this is just for info/tracing purposes
97+
static int last_host_wake = 0;
98+
if (host_wake != last_host_wake) {
99+
printf("HOST_WAKE change %d -> %d\n", last_host_wake, host_wake);
100+
last_host_wake = host_wake;
101+
}
102+
*/
103+
while (uart_rx_any(&cywbt_hci_uart_obj)) {
104+
uint8_t data = uart_rx_char(&cywbt_hci_uart_obj);
105+
//printf("UART RX: %02x\n", data);
106+
hal_uart_rx_cb(hal_uart_rx_arg, data);
107+
}
108+
if (host_wake == 1 && mp_hal_pin_read(pyb_pin_BT_DEV_WAKE) == 0) {
109+
if (mp_hal_ticks_ms() - bt_sleep_ticks > 500) {
110+
//printf("BT SLEEP\n");
111+
mp_hal_pin_high(pyb_pin_BT_DEV_WAKE); // let sleep
112+
}
113+
}
114+
}
115+
116+
#endif // MICROPY_BLUETOOTH_NIMBLE

ports/stm32/nimble/misc.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018-2019 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
29+
#if MICROPY_BLUETOOTH_NIMBLE
30+
31+
/******************************************************************************/
32+
// Misc functions needed by Nimble
33+
34+
#include <stdarg.h>
35+
36+
int sprintf(char *str, const char *fmt, ...) {
37+
va_list ap;
38+
va_start(ap, fmt);
39+
int ret = vsnprintf(str, 65535, fmt, ap);
40+
va_end(ap);
41+
return ret;
42+
}
43+
44+
// TODO deal with root pointers
45+
46+
void *malloc(size_t size) {
47+
//printf("NIMBLE malloc(%u)\n", (uint)size);
48+
return m_malloc(size);
49+
}
50+
51+
void free(void *ptr) {
52+
//printf("NIMBLE free(%p)\n", ptr);
53+
return m_free(ptr);
54+
}
55+
56+
void *realloc(void *ptr, size_t size) {
57+
//printf("NIMBLE realloc(%p, %u)\n", ptr, (uint)size);
58+
return m_realloc(ptr, size);
59+
}
60+
61+
#endif

ports/stm32/nimble/nimble.mk

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# Makefile directives for Apache mynewt nimble BLE component
2+
3+
ifeq ($(MICROPY_BLUETOOTH_NIMBLE),1)
4+
5+
NIMBLE_DIR = lib/mynewt-nimble
6+
7+
SRC_NIMBLE += $(addprefix $(NIMBLE_DIR)/, \
8+
$(addprefix ext/tinycrypt/src/, \
9+
aes_encrypt.c \
10+
cmac_mode.c \
11+
ecc.c \
12+
ecc_dh.c \
13+
utils.c \
14+
) \
15+
nimble/host/services/gap/src/ble_svc_gap.c \
16+
nimble/host/services/gatt/src/ble_svc_gatt.c \
17+
$(addprefix nimble/host/src/, \
18+
ble_att.c \
19+
ble_att_clt.c \
20+
ble_att_cmd.c \
21+
ble_att_svr.c \
22+
ble_eddystone.c \
23+
ble_gap.c \
24+
ble_gattc.c \
25+
ble_gatts.c \
26+
ble_hs_adv.c \
27+
ble_hs_atomic.c \
28+
ble_hs.c \
29+
ble_hs_cfg.c \
30+
ble_hs_conn.c \
31+
ble_hs_dbg.c \
32+
ble_hs_flow.c \
33+
ble_hs_hci.c \
34+
ble_hs_hci_cmd.c \
35+
ble_hs_hci_evt.c \
36+
ble_hs_hci_util.c \
37+
ble_hs_id.c \
38+
ble_hs_log.c \
39+
ble_hs_mbuf.c \
40+
ble_hs_misc.c \
41+
ble_hs_mqueue.c \
42+
ble_hs_pvcy.c \
43+
ble_hs_startup.c \
44+
ble_hs_stop.c \
45+
ble_ibeacon.c \
46+
ble_l2cap.c \
47+
ble_l2cap_coc.c \
48+
ble_l2cap_sig.c \
49+
ble_l2cap_sig_cmd.c \
50+
ble_monitor.c \
51+
ble_sm_alg.c \
52+
ble_sm.c \
53+
ble_sm_cmd.c \
54+
ble_sm_lgcy.c \
55+
ble_sm_sc.c \
56+
ble_store.c \
57+
ble_store_util.c \
58+
ble_uuid.c \
59+
) \
60+
nimble/host/store/ram/src/ble_store_ram.c \
61+
nimble/host/util/src/addr.c \
62+
nimble/transport/uart/src/ble_hci_uart.c \
63+
$(addprefix porting/nimble/src/, \
64+
endian.c \
65+
mem.c \
66+
nimble_port.c \
67+
os_mbuf.c \
68+
os_mempool.c \
69+
os_msys_init.c \
70+
) \
71+
)
72+
73+
SRC_NIMBLE += \
74+
nimble/misc.c \
75+
nimble/npl_os.c \
76+
nimble/hci_uart.c \
77+
78+
CFLAGS_MOD += -DMICROPY_BLUETOOTH_NIMBLE=1
79+
80+
INC += -Inimble
81+
INC += -I$(TOP)/$(NIMBLE_DIR)
82+
INC += -I$(TOP)/$(NIMBLE_DIR)/ext/tinycrypt/include
83+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/host/include
84+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/host/services/gap/include
85+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/host/services/gatt/include
86+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/host/store/ram/include
87+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/host/util/include
88+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/include
89+
INC += -I$(TOP)/$(NIMBLE_DIR)/nimble/transport/uart/include
90+
INC += -I$(TOP)/$(NIMBLE_DIR)/porting/nimble/include
91+
92+
$(BUILD)/$(NIMBLE_DIR)/%.o: CFLAGS += -Wno-maybe-uninitialized -Wno-pointer-arith -Wno-unused-but-set-variable -Wno-format
93+
94+
OBJ += $(addprefix $(BUILD)/, $(SRC_NIMBLE:.c=.o))
95+
96+
endif

ports/stm32/nimble/nimble_npl_os.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#ifndef MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H
2+
#define MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H
3+
4+
#include <stdint.h>
5+
6+
#define BLE_NPL_OS_ALIGNMENT (4)
7+
#define BLE_NPL_TIME_FOREVER (0xffffffff)
8+
9+
typedef uint32_t ble_npl_time_t;
10+
typedef int32_t ble_npl_stime_t;
11+
12+
struct ble_npl_event {
13+
ble_npl_event_fn *fn;
14+
void *arg;
15+
struct ble_npl_event *prev;
16+
struct ble_npl_event *next;
17+
};
18+
19+
struct ble_npl_eventq {
20+
struct ble_npl_event *head;
21+
struct ble_npl_eventq *nextq;
22+
};
23+
24+
struct ble_npl_callout {
25+
bool active;
26+
uint32_t ticks;
27+
struct ble_npl_eventq *evq;
28+
struct ble_npl_event ev;
29+
struct ble_npl_callout *nextc;
30+
};
31+
32+
struct ble_npl_mutex {
33+
volatile uint8_t locked;
34+
};
35+
36+
struct ble_npl_sem {
37+
volatile uint16_t count;
38+
};
39+
40+
#endif // MICROPY_INCLUDED_STM32_NIMBLE_NIMBLE_NPL_OS_H

0 commit comments

Comments
 (0)
0