|
| 1 | +// This file is part of the CircuitPython project: https://circuitpython.org |
| 2 | +// |
| 3 | +// SPDX-FileCopyrightText: Copyright (c) 2017 Scott Shawcroft for Adafruit Industries |
| 4 | +// |
| 5 | +// SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +#include "supervisor/board.h" |
| 8 | +#include "mpconfigboard.h" |
| 9 | + |
| 10 | +#include "stm32l4xx.h" |
| 11 | +#include "stm32l433xx.h" |
| 12 | + |
| 13 | +#include "shared-bindings/microcontroller/Pin.h" |
| 14 | +#include "shared-bindings/digitalio/DigitalInOut.h" |
| 15 | +#include "shared-bindings/digitalio/Direction.h" |
| 16 | +#include "shared-bindings/digitalio/DriveMode.h" |
| 17 | +#include "board.h" |
| 18 | + |
| 19 | +digitalio_digitalinout_obj_t power_pin = { .base.type = &digitalio_digitalinout_type }; |
| 20 | +digitalio_digitalinout_obj_t discharge_pin = { .base.type = &digitalio_digitalinout_type }; |
| 21 | + |
| 22 | +void initialize_discharge_pin(void) { |
| 23 | + /* Initialize the 3V3 discharge to be OFF and the output power to be ON */ |
| 24 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 25 | + __HAL_RCC_GPIOH_CLK_ENABLE(); |
| 26 | + __HAL_RCC_GPIOB_CLK_ENABLE(); |
| 27 | + |
| 28 | + common_hal_digitalio_digitalinout_construct(&power_pin, &pin_PH00); |
| 29 | + common_hal_digitalio_digitalinout_construct(&discharge_pin, &pin_PH01); |
| 30 | + common_hal_digitalio_digitalinout_never_reset(&power_pin); |
| 31 | + common_hal_digitalio_digitalinout_never_reset(&discharge_pin); |
| 32 | + |
| 33 | + GPIO_InitTypeDef GPIO_InitStruct; |
| 34 | + |
| 35 | + /* Set DISCHARGE_3V3 as well as the pins we're not initially using to FLOAT */ |
| 36 | + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; |
| 37 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 38 | + GPIO_InitStruct.Pin = GPIO_PIN_1; |
| 39 | + HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); /* PH1 DISCHARGE_3V3 */ |
| 40 | + GPIO_InitStruct.Pin = GPIO_PIN_3; |
| 41 | + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* PB3 is USB_DETECT */ |
| 42 | + GPIO_InitStruct.Pin = GPIO_PIN_15; |
| 43 | + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* PA15 is CHARGE_DETECT */ |
| 44 | + GPIO_InitStruct.Pin = GPIO_PIN_4; |
| 45 | + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* PA4 is BAT_VOLTAGE */ |
| 46 | + |
| 47 | + /* Turn on the 3V3 regulator */ |
| 48 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 49 | + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
| 50 | + GPIO_InitStruct.Pin = GPIO_PIN_0; |
| 51 | + HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); |
| 52 | + HAL_GPIO_WritePin(GPIOH, GPIO_InitStruct.Pin, GPIO_PIN_SET); |
| 53 | +} |
| 54 | + |
| 55 | +void board_init(void) { |
| 56 | + // enable the debugger while sleeping. Todo move somewhere more central (kind of generally useful in a debug build) |
| 57 | + SET_BIT(DBGMCU->CR, DBGMCU_CR_DBG_SLEEP); |
| 58 | + |
| 59 | + // Set tick interrupt priority, default HAL value is intentionally invalid |
| 60 | + // Without this, USB does not function. |
| 61 | + HAL_InitTick((1UL << __NVIC_PRIO_BITS) - 1UL); |
| 62 | + |
| 63 | + __HAL_RCC_GPIOA_CLK_ENABLE(); |
| 64 | + GPIO_InitTypeDef GPIO_InitStruct; |
| 65 | + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; |
| 66 | + GPIO_InitStruct.Pull = GPIO_NOPULL; |
| 67 | + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; |
| 68 | + GPIO_InitStruct.Pin = GPIO_PIN_8; |
| 69 | + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
| 70 | + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET); |
| 71 | + HAL_Delay(50); |
| 72 | + HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); |
| 73 | +} |
| 74 | + |
| 75 | +void reset_board(void) { |
| 76 | + initialize_discharge_pin(); |
| 77 | +} |
| 78 | + |
| 79 | +// Use the MP_WEAK supervisor/shared/board.c versions of routines not defined here. |
0 commit comments