|
| 1 | +/* |
| 2 | + * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 hathach for Adafruit Industries |
| 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 "nrfx.h" |
| 28 | +#include "nrfx_power.h" |
| 29 | +#include "tick.h" |
| 30 | +#include "usb.h" |
| 31 | + |
| 32 | +#include "lib/mp-readline/readline.h" |
| 33 | +#include "lib/utils/interrupt_char.h" |
| 34 | + |
| 35 | +#ifdef NRF52840_XXAA |
| 36 | + |
| 37 | +void usb_init(void) { |
| 38 | + |
| 39 | +#ifdef SOFTDEVICE_PRESENT |
| 40 | + // TODO support Softdevice config |
| 41 | +#else |
| 42 | + // Softdevice is not present, init power module and register tusb power event function |
| 43 | + // for vusb detect, ready, removed |
| 44 | + extern void tusb_hal_nrf_power_event(uint32_t event); |
| 45 | + |
| 46 | + // Power module init |
| 47 | + const nrfx_power_config_t pwr_cfg = { 0 }; |
| 48 | + nrfx_power_init(&pwr_cfg); |
| 49 | + |
| 50 | + // USB Power detection |
| 51 | + const nrfx_power_usbevt_config_t config = { .handler = (nrfx_power_usb_event_handler_t) tusb_hal_nrf_power_event }; |
| 52 | + nrfx_power_usbevt_init(&config); |
| 53 | + |
| 54 | + nrfx_power_usbevt_enable(); |
| 55 | +#endif |
| 56 | + |
| 57 | + tusb_init(); |
| 58 | + |
| 59 | +#if MICROPY_KBD_EXCEPTION |
| 60 | + // set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() fired when Ctrl+C is received |
| 61 | + // TODO should be called in mp_hal_set_interrupt_char() |
| 62 | + tud_cdc_set_wanted_char(CHAR_CTRL_C); |
| 63 | +#endif |
| 64 | +} |
| 65 | + |
| 66 | +//--------------------------------------------------------------------+ |
| 67 | +// tinyusb callbacks |
| 68 | +//--------------------------------------------------------------------+ |
| 69 | +void tud_mount_cb(void) { |
| 70 | +} |
| 71 | + |
| 72 | +void tud_umount_cb(void) { |
| 73 | +} |
| 74 | + |
| 75 | +uint32_t tusb_hal_millis(void) { |
| 76 | + uint64_t ms; |
| 77 | + uint32_t us; |
| 78 | + current_tick(&ms, &us); |
| 79 | + return (uint32_t) ms; |
| 80 | +} |
| 81 | + |
| 82 | +#if MICROPY_KBD_EXCEPTION |
| 83 | + |
| 84 | +/** |
| 85 | + * Callback invoked when received an "wanted" char. |
| 86 | + * @param itf Interface index (for multiple cdc interfaces) |
| 87 | + * @param wanted_char The wanted char (set previously) |
| 88 | + */ |
| 89 | +void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char) |
| 90 | +{ |
| 91 | + (void) itf; // not used |
| 92 | + |
| 93 | + if (mp_interrupt_char == wanted_char) { |
| 94 | + tud_cdc_read_flush(); // flush read fifo |
| 95 | + mp_keyboard_interrupt(); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#endif |
| 100 | + |
| 101 | +#endif |
| 102 | + |
0 commit comments