|
| 1 | +#include "bluetooth_btstack_control_ev3.h" |
| 2 | + |
| 3 | +#if PBDRV_CONFIG_BLUETOOTH_BTSTACK_EV3 |
| 4 | + |
| 5 | +#include <math.h> |
| 6 | + |
| 7 | +#include <btstack.h> |
| 8 | +#include <tiam1808/ecap.h> |
| 9 | +#include <tiam1808/hw/hw_syscfg0_AM1808.h> |
| 10 | +#include <tiam1808/hw/hw_syscfg1_AM1808.h> |
| 11 | +#include <tiam1808/hw/hw_types.h> |
| 12 | +#include <tiam1808/hw/soc_AM1808.h> |
| 13 | + |
| 14 | +#include <pbio/error.h> |
| 15 | +#include <pbio/os.h> |
| 16 | +#include "../gpio/gpio_ev3.h" |
| 17 | + |
| 18 | +static const pbdrv_gpio_t pin_bluetooth_enable = PBDRV_GPIO_EV3_PIN(9, 27, 24, 4, 9); |
| 19 | + |
| 20 | +static int ev3_control_off(); |
| 21 | + |
| 22 | +static void ev3_control_init(const void *config) { |
| 23 | + // From the ev3dev configuration: |
| 24 | + // |
| 25 | + // There is a PIC microcontroller for interfacing with an Apple MFi |
| 26 | + // chip. This interferes with normal Bluetooth operation, so we need |
| 27 | + // to make sure it is turned off. Note: The publicly available |
| 28 | + // schematics from LEGO don't show that these pins are connected to |
| 29 | + // anything, but they are present in the source code from LEGO. |
| 30 | + const pbdrv_gpio_t bt_pic_en = PBDRV_GPIO_EV3_PIN(8, 19, 16, 3, 3); |
| 31 | + pbdrv_gpio_alt(&bt_pic_en, SYSCFG_PINMUX8_PINMUX8_19_16_GPIO3_3); |
| 32 | + pbdrv_gpio_out_low(&bt_pic_en); |
| 33 | + // Hold RTS high (we're not ready to receive anything from the PIC). |
| 34 | + const pbdrv_gpio_t bt_pic_rts = PBDRV_GPIO_EV3_PIN(9, 7, 4, 4, 14); |
| 35 | + pbdrv_gpio_alt(&bt_pic_rts, SYSCFG_PINMUX9_PINMUX9_7_4_GPIO4_14); |
| 36 | + pbdrv_gpio_out_high(&bt_pic_rts); |
| 37 | + // CTS technically does not need to be configured, but for documentation |
| 38 | + // purposes we do. |
| 39 | + const pbdrv_gpio_t bt_pic_cts = PBDRV_GPIO_EV3_PIN(12, 3, 0, 5, 7); |
| 40 | + pbdrv_gpio_alt(&bt_pic_cts, SYSCFG_PINMUX12_PINMUX12_3_0_GPIO5_7); |
| 41 | + pbdrv_gpio_input(&bt_pic_cts); |
| 42 | + // Don't interfere with the BT clock's enable pin. |
| 43 | + const pbdrv_gpio_t bt_clock_en = PBDRV_GPIO_EV3_PIN(1, 11, 8, 0, 5); |
| 44 | + pbdrv_gpio_alt(&bt_clock_en, SYSCFG_PINMUX1_PINMUX1_11_8_GPIO0_5); |
| 45 | + pbdrv_gpio_input(&bt_clock_en); |
| 46 | + |
| 47 | + // Configure ECAP2 to emit the slow clock signal for the bluetooth module. |
| 48 | + ECAPOperatingModeSelect(SOC_ECAP_2_REGS, ECAP_APWM_MODE); |
| 49 | + // Calculate the number of clock ticks the APWM period should last. Note |
| 50 | + // that the following float operations are all constant and optimized away. |
| 51 | + // APWM is clocked by sysclk2 by default. |
| 52 | + // Target frequency is 32.767 kHz, see cc2560 datasheet. |
| 53 | + // Note that the APWM module wraps on the cycle after reaching the period |
| 54 | + // value, which means we need to subtract one from the desired period to get |
| 55 | + // a period length in cycles that matches the desired frequency. |
| 56 | + const int aprd = round(SOC_SYSCLK_2_FREQ / 32767.0) - 1; |
| 57 | + ECAPAPWMCaptureConfig(SOC_ECAP_2_REGS, aprd / 2, aprd); |
| 58 | + // Set the polarity to active high. It doesn't matter which it is but for |
| 59 | + // the sake of determinism we set it explicitly. |
| 60 | + ECAPAPWMPolarityConfig(SOC_ECAP_2_REGS, ECAP_APWM_ACTIVE_HIGH); |
| 61 | + // Disable input and output synchronization. |
| 62 | + ECAPSyncInOutSelect(SOC_ECAP_2_REGS, ECAP_SYNC_IN_DISABLE, ECAP_SYNC_OUT_DISABLE); |
| 63 | + // Start the counter running. |
| 64 | + ECAPCounterControl(SOC_ECAP_2_REGS, ECAP_COUNTER_FREE_RUNNING); |
| 65 | + // Set gp0[7] to output the ECAP2 APWM signal. |
| 66 | + const pbdrv_gpio_t bluetooth_slow_clock = PBDRV_GPIO_EV3_PIN(1, 3, 0, 0, 7); |
| 67 | + pbdrv_gpio_alt(&bluetooth_slow_clock, SYSCFG_PINMUX1_PINMUX1_3_0_ECAP2); |
| 68 | + |
| 69 | + pbdrv_gpio_alt(&pin_bluetooth_enable, SYSCFG_PINMUX9_PINMUX9_27_24_GPIO4_9); |
| 70 | + |
| 71 | + // Start the module in a defined (and disabled) state. |
| 72 | + ev3_control_off(); |
| 73 | +} |
| 74 | + |
| 75 | +static int ev3_control_on() { |
| 76 | + // Note: the module is not actually "on" yet, however, the way it signals |
| 77 | + // its on-ness is by unblocking our ability to send UART messages. We |
| 78 | + // use auto flow control on the UART, so we don't actually need to wait |
| 79 | + // for the module to come up here. |
| 80 | + pbdrv_gpio_out_high(&pin_bluetooth_enable); |
| 81 | + return 0; |
| 82 | +} |
| 83 | + |
| 84 | +static int ev3_control_off() { |
| 85 | + pbdrv_gpio_out_low(&pin_bluetooth_enable); |
| 86 | + return 0; |
| 87 | +} |
| 88 | + |
| 89 | +static const btstack_control_t ev3_control = { |
| 90 | + .init = &ev3_control_init, |
| 91 | + .on = &ev3_control_on, |
| 92 | + .off = &ev3_control_off, |
| 93 | + .sleep = NULL, |
| 94 | + .wake = NULL, |
| 95 | + .register_for_power_notifications = NULL, |
| 96 | +}; |
| 97 | + |
| 98 | +const btstack_control_t *pbdrv_bluetooth_control_ev3_instance(void) { |
| 99 | + return &ev3_control; |
| 100 | +} |
| 101 | + |
| 102 | +#endif |
0 commit comments