| 1 |
/* |
| 2 |
* This file is part of the MicroPython project, http://micropython.org/ |
| 3 |
* |
| 4 |
* The MIT License (MIT) |
| 5 |
* |
| 6 |
* Copyright (c) 2021 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 <stdio.h> |
| 28 |
|
| 29 |
#include "py/runtime.h" |
| 30 |
#include "py/mphal.h" |
| 31 |
#include "timer.h" |
| 32 |
#include "extmod/mpbthci.h" |
| 33 |
|
| 34 |
#if !BUILDING_MBOOT |
| 35 |
|
| 36 |
#define CC2564_PIN_CTS (pyb_pin_BT_CTS) |
| 37 |
#define CC2564_PIN_RTS (pyb_pin_BT_RTS) |
| 38 |
#define CC2564_PIN_BT_SLOWCLK (pyb_pin_BT_SLOWCLK) |
| 39 |
#define CC2564_PIN_BT_ENABLE (pyb_pin_BT_ENABLE) |
| 40 |
|
| 41 |
// slight difference between LEGO_HUB_NO6 and LEGO_HUB_NO7 |
| 42 |
#ifndef CC2564_TIMER_BT_SLOWCLOCK_TIM |
| 43 |
#define CC2564_TIMER_BT_SLOWCLOCK_TIM 8 |
| 44 |
#define CC2564_TIMER_BT_SLOWCLOCK_TIM_CH 4 |
| 45 |
#endif |
| 46 |
|
| 47 |
static void cc2564_wait_cts_low(mp_hal_pin_obj_t cts, uint32_t timeout_ms) { |
| 48 |
for (int i = 0; i < timeout_ms; ++i) { |
| 49 |
if (mp_hal_pin_read(cts) == 0) { |
| 50 |
break; |
| 51 |
} |
| 52 |
mp_hal_delay_ms(1); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
int mp_bluetooth_hci_controller_init(void) { |
| 57 |
// Pull BTNSHUTD low to disable chip. |
| 58 |
mp_hal_pin_output(CC2564_PIN_BT_ENABLE); |
| 59 |
mp_hal_pin_low(CC2564_PIN_BT_ENABLE); |
| 60 |
|
| 61 |
// Output a 32768Hz signal on BTSLOWCLK. |
| 62 |
// tim = pyb.Timer(TIM, freq=32768) |
| 63 |
// tim_ch = tim.channel(TIM_CH, pyb.Timer.PWM, pin=btclk) |
| 64 |
// tim_ch.pulse_width_percent(50) |
| 65 |
mp_obj_t args[6] = { MP_OBJ_NEW_SMALL_INT(CC2564_TIMER_BT_SLOWCLOCK_TIM), MP_OBJ_NEW_QSTR(MP_QSTR_freq), MP_OBJ_NEW_SMALL_INT(32768), MP_OBJ_NULL }; |
| 66 |
mp_obj_t tim = MP_OBJ_TYPE_GET_SLOT(&pyb_timer_type, make_new)(&pyb_timer_type, 1, 1, args); |
| 67 |
mp_load_method(tim, MP_QSTR_channel, args); |
| 68 |
args[2] = MP_OBJ_NEW_SMALL_INT(CC2564_TIMER_BT_SLOWCLOCK_TIM_CH); |
| 69 |
args[3] = MP_OBJ_NEW_SMALL_INT(0); // CHANNEL_MODE_PWM_NORMAL |
| 70 |
args[4] = MP_OBJ_NEW_QSTR(MP_QSTR_pin); |
| 71 |
args[5] = (mp_obj_t)CC2564_PIN_BT_SLOWCLK; |
| 72 |
mp_obj_t tim_ch = mp_call_method_n_kw(2, 1, args); |
| 73 |
mp_load_method(tim_ch, MP_QSTR_pulse_width_percent, args); |
| 74 |
args[2] = MP_OBJ_NEW_SMALL_INT(50); |
| 75 |
mp_call_method_n_kw(1, 0, args); |
| 76 |
|
| 77 |
// Pull BTNSHUTD high to enable chip and wait for CTS to go low to indicate ready. |
| 78 |
mp_hal_pin_high(CC2564_PIN_BT_ENABLE); |
| 79 |
cc2564_wait_cts_low(CC2564_PIN_CTS, 500); |
| 80 |
|
| 81 |
return 0; |
| 82 |
} |
| 83 |
|
| 84 |
int mp_bluetooth_hci_controller_deinit(void) { |
| 85 |
// Pull BTNSHUTD low to disable chip. |
| 86 |
mp_hal_pin_low(CC2564_PIN_BT_ENABLE); |
| 87 |
|
| 88 |
return 0; |
| 89 |
} |
| 90 |
|
| 91 |
#endif |