|
| 1 | +/* |
| 2 | + * This file is part of the OpenMV project, https://openmv.io. |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io> |
| 7 | + * Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io> |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | + * of this software and associated documentation files (the "Software"), to deal |
| 11 | + * in the Software without restriction, including without limitation the rights |
| 12 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | + * copies of the Software, and to permit persons to whom the Software is |
| 14 | + * furnished to do so, subject to the following conditions: |
| 15 | + * |
| 16 | + * The above copyright notice and this permission notice shall be included in |
| 17 | + * all copies or substantial portions of the Software. |
| 18 | + * |
| 19 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | + * THE SOFTWARE. |
| 26 | + * |
| 27 | + * NINA-W10 Bluetooth HCI driver. |
| 28 | + */ |
| 29 | + |
| 30 | +#include <stdio.h> |
| 31 | +#include <string.h> |
| 32 | + |
| 33 | +#include "py/runtime.h" |
| 34 | +#include "py/mphal.h" |
| 35 | +#include "extmod/mpbthci.h" |
| 36 | + |
| 37 | +#if MICROPY_PY_BLUETOOTH && MICROPY_PY_NETWORK_NINAW10 |
| 38 | + |
| 39 | +#define HCI_COMMAND_PACKET (0x01) |
| 40 | +#define HCI_ACLDATA_PACKET (0x02) |
| 41 | +#define HCI_EVENT_PACKET (0x04) |
| 42 | + |
| 43 | +#define HCI_COMMAND_COMPLETE (0x0e) |
| 44 | +#define HCI_COMMAND_TIMEOUT (3000) |
| 45 | + |
| 46 | +#define OGF_LINK_CTL (0x01) |
| 47 | +#define OGF_HOST_CTL (0x03) |
| 48 | + |
| 49 | +#define OCF_SET_EVENT_MASK (0x0001) |
| 50 | +#define OCF_RESET (0x0003) |
| 51 | + |
| 52 | +#define error_printf(...) mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) |
| 53 | +#define debug_printf(...) //mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) |
| 54 | + |
| 55 | +// Provided by the port, and also possibly shared with the stack. |
| 56 | +extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; |
| 57 | + |
| 58 | +static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param_buf) { |
| 59 | + uint8_t *buf = mp_bluetooth_hci_cmd_buf; |
| 60 | + |
| 61 | + buf[0] = HCI_COMMAND_PACKET; |
| 62 | + buf[1] = ocf; |
| 63 | + buf[2] = ogf << 2 | ocf >> 8; |
| 64 | + buf[3] = param_len; |
| 65 | + |
| 66 | + if (param_len) { |
| 67 | + memcpy(buf + 4, param_buf, param_len); |
| 68 | + } |
| 69 | + |
| 70 | + debug_printf("HCI Command: %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]); |
| 71 | + |
| 72 | + mp_bluetooth_hci_uart_write(buf, 4 + param_len); |
| 73 | + |
| 74 | + // Receive HCI event packet, initially reading 3 bytes (HCI Event, Event code, Plen). |
| 75 | + for (mp_uint_t start = mp_hal_ticks_ms(), size = 3, i = 0; i < size;) { |
| 76 | + while (!mp_bluetooth_hci_uart_any()) { |
| 77 | + MICROPY_EVENT_POLL_HOOK |
| 78 | + // Timeout. |
| 79 | + if ((mp_hal_ticks_ms() - start) > HCI_COMMAND_TIMEOUT) { |
| 80 | + error_printf("timeout waiting for HCI packet\n"); |
| 81 | + return -1; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + buf[i] = mp_bluetooth_hci_uart_readchar(); |
| 86 | + |
| 87 | + // There seems to be a sync issue with this fw/module. |
| 88 | + if (i == 0 && buf[0] == 0xFF) { |
| 89 | + continue; |
| 90 | + } |
| 91 | + |
| 92 | + // Check for packet type. |
| 93 | + if (i == 0 && buf[0] != HCI_EVENT_PACKET) { |
| 94 | + error_printf("unexpected HCI packet: %02x\n", buf[0]); |
| 95 | + return -1; |
| 96 | + } |
| 97 | + |
| 98 | + // Sanity check the packet parameters length. |
| 99 | + if (i == 2 && ((size += buf[2]) > sizeof(mp_bluetooth_hci_cmd_buf))) { |
| 100 | + error_printf("unexpected event packet length: %d\n", size); |
| 101 | + return -1; |
| 102 | + } |
| 103 | + |
| 104 | + i++; |
| 105 | + } |
| 106 | + |
| 107 | + // We're only looking for command complete events. |
| 108 | + if (buf[1] != HCI_COMMAND_COMPLETE || buf[4] != ocf || buf[5] != (ogf << 2 | ocf >> 8)) { |
| 109 | + error_printf("response mismatch: %02x %02x\n", buf[4], buf[5]); |
| 110 | + return -1; |
| 111 | + } |
| 112 | + |
| 113 | + // Log event. |
| 114 | + debug_printf("HCI Event packet: %02x %02x %02x %02x %02x %02x %02x\n", |
| 115 | + buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); |
| 116 | + |
| 117 | + // Status code. |
| 118 | + return buf[6]; |
| 119 | +} |
| 120 | + |
| 121 | +int mp_bluetooth_hci_controller_init(void) { |
| 122 | + // This is called immediately after the UART is initialised during stack initialisation. |
| 123 | + mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); |
| 124 | + mp_hal_pin_output(MICROPY_HW_NINA_RESET); |
| 125 | + |
| 126 | + mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 0); |
| 127 | + mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); |
| 128 | + mp_hal_delay_ms(100); |
| 129 | + |
| 130 | + mp_hal_pin_write(MICROPY_HW_NINA_RESET, 1); |
| 131 | + mp_hal_delay_ms(750); |
| 132 | + |
| 133 | + // The UART must be re-initialize here because the GPIO1/RX pin is used initially |
| 134 | + // to reset the module in Bluetooth mode. This will change back the pin to UART RX. |
| 135 | + mp_bluetooth_hci_uart_init(0, 0); |
| 136 | + |
| 137 | + // Send reset command |
| 138 | + return nina_hci_cmd(OGF_HOST_CTL, OCF_RESET, 0, NULL); |
| 139 | + // It seems that nothing else is needed for now. |
| 140 | +} |
| 141 | + |
| 142 | +int mp_bluetooth_hci_controller_deinit(void) { |
| 143 | + // Reset module |
| 144 | + mp_hal_pin_output(MICROPY_HW_NINA_RESET); |
| 145 | + mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); |
| 146 | + return 0; |
| 147 | +} |
| 148 | +#endif |
0 commit comments