-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
RP2: Add ublox Nina-W10 WiFi/BT module driver. #7668
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* This file is part of the OpenMV project, https://openmv.io. | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io> | ||
* Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* NINA-W10 driver BSP. | ||
*/ | ||
#ifndef __NINA_BSP_H__ | ||
#define __NINA_BSP_H__ | ||
int nina_bsp_init(void); | ||
int nina_bsp_deinit(void); | ||
int nina_bsp_read_irq(void); | ||
int nina_bsp_spi_slave_select(uint32_t timeout); | ||
int nina_bsp_spi_slave_deselect(void); | ||
int nina_bsp_spi_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t size); | ||
#endif // define __NINA_BSP_H__ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* This file is part of the OpenMV project, https://openmv.io. | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io> | ||
* Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* NINA-W10 Bluetooth HCI driver. | ||
*/ | ||
#include "py/mphal.h" | ||
#if MICROPY_PY_BLUETOOTH && MICROPY_PY_NETWORK_NINAW10 | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "py/runtime.h" | ||
#include "extmod/mpbthci.h" | ||
|
||
#define HCI_COMMAND_PACKET (0x01) | ||
#define HCI_ACLDATA_PACKET (0x02) | ||
#define HCI_EVENT_PACKET (0x04) | ||
|
||
#define HCI_COMMAND_COMPLETE (0x0e) | ||
#define HCI_COMMAND_TIMEOUT (3000) | ||
|
||
#define OGF_LINK_CTL (0x01) | ||
#define OGF_HOST_CTL (0x03) | ||
|
||
#define OCF_SET_EVENT_MASK (0x0001) | ||
#define OCF_RESET (0x0003) | ||
|
||
#define error_printf(...) mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) | ||
#define debug_printf(...) // mp_printf(&mp_plat_print, "nina_bt_hci.c: " __VA_ARGS__) | ||
|
||
// Provided by the port, and also possibly shared with the stack. | ||
extern uint8_t mp_bluetooth_hci_cmd_buf[4 + 256]; | ||
|
||
static int nina_hci_cmd(int ogf, int ocf, size_t param_len, const uint8_t *param_buf) { | ||
uint8_t *buf = mp_bluetooth_hci_cmd_buf; | ||
|
||
buf[0] = HCI_COMMAND_PACKET; | ||
buf[1] = ocf; | ||
buf[2] = ogf << 2 | ocf >> 8; | ||
buf[3] = param_len; | ||
|
||
if (param_len) { | ||
memcpy(buf + 4, param_buf, param_len); | ||
} | ||
|
||
debug_printf("HCI Command: %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3]); | ||
|
||
mp_bluetooth_hci_uart_write(buf, 4 + param_len); | ||
|
||
// Receive HCI event packet, initially reading 3 bytes (HCI Event, Event code, Plen). | ||
for (mp_uint_t start = mp_hal_ticks_ms(), size = 3, i = 0; i < size;) { | ||
while (!mp_bluetooth_hci_uart_any()) { | ||
MICROPY_EVENT_POLL_HOOK | ||
// Timeout. | ||
if ((mp_hal_ticks_ms() - start) > HCI_COMMAND_TIMEOUT) { | ||
error_printf("timeout waiting for HCI packet\n"); | ||
return -1; | ||
} | ||
} | ||
|
||
buf[i] = mp_bluetooth_hci_uart_readchar(); | ||
|
||
// There seems to be a sync issue with this fw/module. | ||
if (i == 0 && buf[0] == 0xFF) { | ||
continue; | ||
} | ||
|
||
// Check for packet type. | ||
if (i == 0 && buf[0] != HCI_EVENT_PACKET) { | ||
error_printf("unexpected HCI packet: %02x\n", buf[0]); | ||
return -1; | ||
} | ||
|
||
// Sanity check the packet parameters length. | ||
if (i == 2 && ((size += buf[2]) > sizeof(mp_bluetooth_hci_cmd_buf))) { | ||
error_printf("unexpected event packet length: %d\n", size); | ||
return -1; | ||
} | ||
|
||
i++; | ||
} | ||
|
||
// We're only looking for command complete events. | ||
if (buf[1] != HCI_COMMAND_COMPLETE || buf[4] != ocf || buf[5] != (ogf << 2 | ocf >> 8)) { | ||
error_printf("response mismatch: %02x %02x\n", buf[4], buf[5]); | ||
return -1; | ||
} | ||
|
||
// Log event. | ||
debug_printf("HCI Event packet: %02x %02x %02x %02x %02x %02x %02x\n", | ||
buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); | ||
|
||
// Status code. | ||
return buf[6]; | ||
} | ||
|
||
int mp_bluetooth_hci_controller_init(void) { | ||
// This is called immediately after the UART is initialised during stack initialisation. | ||
mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); | ||
mp_hal_pin_output(MICROPY_HW_NINA_RESET); | ||
|
||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 0); | ||
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); | ||
mp_hal_delay_ms(100); | ||
|
||
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 1); | ||
mp_hal_delay_ms(750); | ||
|
||
// The UART must be re-initialize here because the GPIO1/RX pin is used initially | ||
// to reset the module in Bluetooth mode. This will change back the pin to UART RX. | ||
mp_bluetooth_hci_uart_init(0, 0); | ||
|
||
// Send reset command | ||
return nina_hci_cmd(OGF_HOST_CTL, OCF_RESET, 0, NULL); | ||
// It seems that nothing else is needed for now. | ||
} | ||
|
||
int mp_bluetooth_hci_controller_deinit(void) { | ||
// Reset module | ||
mp_hal_pin_output(MICROPY_HW_NINA_RESET); | ||
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); | ||
return 0; | ||
} | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
/* | ||
* This file is part of the OpenMV project, https://openmv.io. | ||
* | ||
* The MIT License (MIT) | ||
* | ||
* Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io> | ||
* Copyright (c) 2013-2021 Kwabena W. Agyeman <kwagyeman@openmv.io> | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
* | ||
* NINA-W10 driver BSP implementation. | ||
*/ | ||
#include "py/mphal.h" | ||
#if MICROPY_PY_NETWORK_NINAW10 | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
|
||
#include "py/runtime.h" | ||
#include "modmachine.h" | ||
#include "extmod/machine_spi.h" | ||
#include "mpconfigboard.h" | ||
|
||
#include "nina_bsp.h" | ||
#include "nina_wifi_drv.h" | ||
|
||
#if NINA_DEBUG | ||
#define debug_printf(...) mp_printf(&mp_plat_print, __VA_ARGS__) | ||
#else | ||
#define debug_printf(...) | ||
#endif | ||
|
||
int nina_bsp_init(void) { | ||
mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); | ||
mp_hal_pin_input(MICROPY_HW_NINA_ACK); | ||
mp_hal_pin_output(MICROPY_HW_NINA_RESET); | ||
mp_hal_pin_output(MICROPY_HW_NINA_GPIO0); | ||
|
||
// Reset module in WiFi mode | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 1); | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO0, 1); | ||
|
||
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); | ||
mp_hal_delay_ms(100); | ||
|
||
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 1); | ||
mp_hal_delay_ms(750); | ||
|
||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO0, 0); | ||
mp_hal_pin_input(MICROPY_HW_NINA_GPIO0); | ||
|
||
// Initialize SPI. | ||
mp_obj_t args[] = { | ||
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIFI_SPI_ID), | ||
MP_OBJ_NEW_SMALL_INT(MICROPY_HW_WIFI_SPI_BAUDRATE), | ||
}; | ||
|
||
MP_STATE_PORT(mp_wifi_spi) = machine_spi_type.make_new((mp_obj_t)&machine_spi_type, 2, 0, args); | ||
return 0; | ||
} | ||
|
||
int nina_bsp_deinit(void) { | ||
mp_hal_pin_output(MICROPY_HW_NINA_GPIO1); | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 1); | ||
|
||
mp_hal_pin_output(MICROPY_HW_NINA_RESET); | ||
mp_hal_pin_write(MICROPY_HW_NINA_RESET, 0); | ||
mp_hal_delay_ms(100); | ||
|
||
mp_hal_pin_output(MICROPY_HW_NINA_GPIO0); | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO0, 1); | ||
return 0; | ||
} | ||
|
||
int nina_bsp_read_irq(void) { | ||
return mp_hal_pin_read(MICROPY_HW_NINA_GPIO0); | ||
} | ||
|
||
int nina_bsp_spi_slave_select(uint32_t timeout) { | ||
// Wait for ACK to go low. | ||
for (mp_uint_t start = mp_hal_ticks_ms(); mp_hal_pin_read(MICROPY_HW_NINA_ACK) == 1; mp_hal_delay_ms(1)) { | ||
if ((mp_hal_ticks_ms() - start) >= timeout) { | ||
return -1; | ||
} | ||
} | ||
|
||
// Chip select. | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 0); | ||
|
||
< 1E0A /td> | // Wait for ACK to go high. | |
for (mp_uint_t start = mp_hal_ticks_ms(); mp_hal_pin_read(MICROPY_HW_NINA_ACK) == 0; mp_hal_delay_ms(1)) { | ||
if ((mp_hal_ticks_ms() - start) >= 100) { | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 1); | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
int nina_bsp_spi_slave_deselect(void) { | ||
mp_hal_pin_write(MICROPY_HW_NINA_GPIO1, 1); | ||
return 0; | ||
} | ||
|
||
int nina_bsp_spi_transfer(const uint8_t *tx_buf, uint8_t *rx_buf, uint32_t size) { | ||
mp_obj_t mp_wifi_spi = MP_STATE_PORT(mp_wifi_spi); | ||
((mp_machine_spi_p_t *)machine_spi_type.protocol)->transfer(mp_wifi_spi, size, tx_buf, rx_buf); | ||
#if NINA_DEBUG | ||
for (int i = 0; i < size; i++) { | ||
if (tx_buf) { | ||
debug_printf("0x%x ", tx_buf[i]); | ||
} else { | ||
debug_printf("0x%x ", rx_buf[i]); | ||
} | ||
} | ||
#endif | ||
return 0; | ||
} | ||
#endif // MICROPY_PY_NETWORK_NINAW10 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.