circuitpython/ports/stm/common-hal/digitalio/DigitalInOut.c

173 lines
6.3 KiB
C
Raw Normal View History

2020-03-11 18:13:06 -04:00
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
*
* 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.
*/
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/microcontroller/Pin.h"
2020-03-11 18:13:06 -04:00
2020-05-20 12:23:42 -04:00
// The HAL is sparse on obtaining register information, so we use the LLs here.
2020-04-06 19:13:55 -04:00
#if (CPY_STM32H7)
#include "stm32h7xx_ll_gpio.h"
2020-04-06 19:13:55 -04:00
#elif (CPY_STM32F7)
2020-04-02 11:47:16 -04:00
#include "stm32f7xx_ll_gpio.h"
2020-04-06 19:13:55 -04:00
#elif (CPY_STM32F4)
2020-03-11 18:13:06 -04:00
#include "stm32f4xx_ll_gpio.h"
feat: add Blues Swan R5 support complete pin mapping for Feather pins stubbed out files needed for complilation. still to be modified 0 out all CPY modules in mpconfigboard.mk until we get the build running add csv for pin generation for STM32L4R5 add F4R5 references in peripherals files refactored out board files BECAUSE I AM AN IDIOT; add L4 series system clocks file from CubeMX took a guess at the number of USB endpoint pairs to get the build done guess was close, but wrong. It is 8 clean up peripheral DEFs Fixes build error: ``` In file included from ../../py/mpstate.h:33, from ../../py/mpstate.c:27: ../../py/misc.h: In function 'vstr_str': ../../py/misc.h:196:1: sorry, unimplemented: Thumb-1 hard-float VFP ABI static inline char *vstr_str(vstr_t *vstr) { ^~~~~~ ``` Sleuthing steps: * verify that the feather_stm32f4_express board builds correctly * put a `#error` at the bottom of the `mpstate.c` file. * build for the feather and swan boards, with V=2 to capture the build command for that file. * use a differencing tool to inspect the differences between the two invocations * inspecting the differences, I saw a missing `-mcpu=cortex-m4` I tested by adding that to the Swan build command. The file built fine (stopping at the hard error, but no other warnings.) A grep through the sources revealed where this flag was being set for the stm ports. With this commit, the build gets further, but does not complete. The next exciting episode in this unfolding coding saga is just a commit away! working build with minimal set of modules for the Blues Swan r5 chore:change header copyright name to Blues Wireless Contributors USB operational. Fixed up clocks to be hardwired for LSE no HSE case. (Trying to combine HSE in there made the code much more complex, and I don't have a board to test it out on.) USART working adds support for `ENABLE_3V3` and `DISCHARGE_3V3` pins. I am surprised that pin definitions are quite low-level and don't include default direction and state, so the code currently has to initialize `ENABLE_3V3` pin as output. The LED takes over a second to discharge, so I wonder if the board startup code is not having the desired affect. short circuit implementation of backup memory for the STM32L4 all the ports remove company name from board name to be consistent with the Arduino board definition. add default pins for I2C, SPI and UART, so that `board.I2C` et al. works as expected. Confirmed I2C timing. fix board name fix incorrect pin definition. add test to allow manual check of each output pin analog IO code changes for WebUSB. Doesn't appear to work, will revisit later. ensure that `sys.platform` is available checkin missing file feat: make room for a larger filesystem so the sensor tutorial will fit on the device. fix:(stm32l4r5zi.csv): merged AF0-7 and AF8-15 into single lines and removed extraneous headers mixed in with the data. fix(parse_af_csv.py): pin index in the csv is 0 not 1, and AF index made 1 larger chore(Swan R5): update peripherals pins from `parse_af_csv.py` output optimize flash sector access
2021-07-29 18:06:31 -04:00
#elif (CPY_STM32L4)
#include "stm32l4xx_ll_gpio.h"
#else
#error unknown MCU for DigitalInOut
#endif
2020-03-11 18:13:06 -04:00
void common_hal_digitalio_digitalinout_never_reset(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self) {
2020-03-11 18:13:06 -04:00
never_reset_pin_number(self->pin->port, self->pin->number);
}
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin) {
2020-03-11 18:13:06 -04:00
common_hal_mcu_pin_claim(pin);
2020-03-11 18:13:06 -04:00
self->pin = pin;
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(self->pin->number);
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
return DIGITALINOUT_OK;
}
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self) {
return self->pin == NULL;
}
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self) {
if (common_hal_digitalio_digitalinout_deinited(self)) {
return;
}
reset_pin_number(self->pin->port, self->pin->number);
self->pin = NULL;
}
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_input(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
2020-03-11 18:13:06 -04:00
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(self->pin->number);
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
common_hal_digitalio_digitalinout_set_pull(self, pull);
return DIGITALINOUT_OK;
2020-03-11 18:13:06 -04:00
}
2020-05-20 12:23:42 -04:00
digitalinout_result_t common_hal_digitalio_digitalinout_switch_to_output(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self, bool value,
digitalio_drive_mode_t drive_mode) {
2020-03-11 18:13:06 -04:00
common_hal_digitalio_digitalinout_set_drive_mode(self, drive_mode);
common_hal_digitalio_digitalinout_set_value(self, value);
2020-05-20 12:23:42 -04:00
return DIGITALINOUT_OK;
2020-03-11 18:13:06 -04:00
}
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self) {
2020-03-11 18:13:06 -04:00
return (LL_GPIO_GetPinMode(pin_port(self->pin->port), pin_mask(self->pin->number))
== LL_GPIO_MODE_INPUT) ? DIRECTION_INPUT : DIRECTION_OUTPUT;
}
void common_hal_digitalio_digitalinout_set_value(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self, bool value) {
2020-03-11 18:13:06 -04:00
HAL_GPIO_WritePin(pin_port(self->pin->port), pin_mask(self->pin->number), value);
}
bool common_hal_digitalio_digitalinout_get_value(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self) {
2020-03-11 18:13:06 -04:00
return (LL_GPIO_GetPinMode(pin_port(self->pin->port), pin_mask(self->pin->number)) == LL_GPIO_MODE_INPUT)
? HAL_GPIO_ReadPin(pin_port(self->pin->port), pin_mask(self->pin->number))
: LL_GPIO_IsOutputPinSet(pin_port(self->pin->port), pin_mask(self->pin->number));
}
2020-05-20 12:23:42 -04:00
digitalinout_result_t common_hal_digitalio_digitalinout_set_drive_mode(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self,
digitalio_drive_mode_t drive_mode) {
2020-03-11 18:13:06 -04:00
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = pin_mask(self->pin->number);
GPIO_InitStruct.Mode = (drive_mode == DRIVE_MODE_OPEN_DRAIN ?
GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP);
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
2020-05-20 12:23:42 -04:00
return DIGITALINOUT_OK;
2020-03-11 18:13:06 -04:00
}
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self) {
2020-03-11 18:13:06 -04:00
return LL_GPIO_GetPinOutputType(pin_port(self->pin->port), pin_mask(self->pin->number))
2021-03-15 09:57:36 -04:00
== LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL;
2020-03-11 18:13:06 -04:00
}
digitalinout_result_t common_hal_digitalio_digitalinout_set_pull(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self, digitalio_pull_t pull) {
2020-03-11 18:13:06 -04:00
switch (pull) {
case PULL_UP:
2023-08-18 13:30:45 -04:00
LL_GPIO_SetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number), LL_GPIO_PULL_UP);
2020-03-11 18:13:06 -04:00
break;
case PULL_DOWN:
2023-08-18 13:30:45 -04:00
LL_GPIO_SetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number), LL_GPIO_PULL_DOWN);
2020-03-11 18:13:06 -04:00
break;
case PULL_NONE:
2023-08-18 13:30:45 -04:00
LL_GPIO_SetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number), LL_GPIO_PULL_NO);
2020-03-11 18:13:06 -04:00
break;
default:
break;
}
return DIGITALINOUT_OK;
2020-03-11 18:13:06 -04:00
}
digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
2021-03-15 09:57:36 -04:00
digitalio_digitalinout_obj_t *self) {
2020-03-11 18:13:06 -04:00
switch (LL_GPIO_GetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number))) {
case LL_GPIO_PULL_UP:
return PULL_UP;
case LL_GPIO_PULL_DOWN:
return PULL_DOWN;
case LL_GPIO_PULL_NO:
return PULL_NONE;
default:
return PULL_NONE;
}
}