Merge pull request #2143 from hierophect/stm32-analogio
STM32: AnalogIO
This commit is contained in:
commit
013aea3bb9
@ -145,6 +145,8 @@ CFLAGS += -DHSE_VALUE=8000000 -DCFG_TUSB_MCU=OPT_MCU_STM32F4 -DCFG_TUD_CDC_RX_BU
|
||||
SRC_STM32 = \
|
||||
boards/$(BOARD)/stm32f4xx_hal_msp.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_gpio.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_adc.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_adc.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pcd_ex.c \
|
||||
stm32f4/STM32F4xx_HAL_Driver/Src/stm32f4xx_ll_usb.c \
|
||||
|
@ -80,11 +80,24 @@ STATIC const mp_rom_map_elem_t board_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR_PC08), MP_ROM_PTR(&pin_PC08) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_PC07), MP_ROM_PTR(&pin_PC07) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_PC06), MP_ROM_PTR(&pin_PC06) },
|
||||
//Names
|
||||
//ST LED names
|
||||
{ MP_ROM_QSTR(MP_QSTR_LD3), MP_ROM_PTR(&pin_PD13) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LD4), MP_ROM_PTR(&pin_PD12) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LD5), MP_ROM_PTR(&pin_PD14) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LD6), MP_ROM_PTR(&pin_PD15) },
|
||||
//more useful LED names
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED1), MP_ROM_PTR(&pin_PD13) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED2), MP_ROM_PTR(&pin_PD12) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED3), MP_ROM_PTR(&pin_PD14) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED4), MP_ROM_PTR(&pin_PD15) },
|
||||
//AnalogIO names
|
||||
{ MP_ROM_QSTR(MP_QSTR_A0), MP_ROM_PTR(&pin_PA00) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_A1), MP_ROM_PTR(&pin_PA01) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_A2), MP_ROM_PTR(&pin_PA02) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_A3), MP_ROM_PTR(&pin_PA03) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_A4), MP_ROM_PTR(&pin_PA04) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_A5), MP_ROM_PTR(&pin_PA05) },
|
||||
//actual LED names
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED_ORANGE), MP_ROM_PTR(&pin_PD13) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED_GREEN), MP_ROM_PTR(&pin_PD12) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_LED_RED), MP_ROM_PTR(&pin_PD14) },
|
||||
|
@ -36,7 +36,7 @@
|
||||
*/
|
||||
#define HAL_MODULE_ENABLED
|
||||
|
||||
/* #define HAL_ADC_MODULE_ENABLED */
|
||||
#define HAL_ADC_MODULE_ENABLED
|
||||
/* #define HAL_CRYP_MODULE_ENABLED */
|
||||
/* #define HAL_CAN_MODULE_ENABLED */
|
||||
/* #define HAL_CRC_MODULE_ENABLED */
|
||||
|
@ -36,7 +36,7 @@
|
||||
*/
|
||||
#define HAL_MODULE_ENABLED
|
||||
|
||||
/* #define HAL_ADC_MODULE_ENABLED */
|
||||
#define HAL_ADC_MODULE_ENABLED
|
||||
/* #define HAL_CRYP_MODULE_ENABLED */
|
||||
/* #define HAL_CAN_MODULE_ENABLED */
|
||||
/* #define HAL_CRC_MODULE_ENABLED */
|
||||
|
129
ports/stm32f4/common-hal/analogio/AnalogIn.c
Normal file
129
ports/stm32f4/common-hal/analogio/AnalogIn.c
Normal file
@ -0,0 +1,129 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 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 "common-hal/analogio/AnalogIn.h"
|
||||
#include "py/runtime.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_ll_gpio.h"
|
||||
#include "stm32f4xx_ll_adc.h"
|
||||
#include "stm32f4xx_ll_bus.h"
|
||||
|
||||
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
|
||||
const mcu_pin_obj_t *pin) {
|
||||
|
||||
// No ADC function on pin
|
||||
if (pin->adc_unit == 0x00) {
|
||||
mp_raise_ValueError(translate("Pin does not have ADC capabilities"));
|
||||
}
|
||||
// TODO: add ADC traits to structure?
|
||||
|
||||
// Note that ADC2 is always bundled pin-to-pin with ADC1 if it exists, and used only
|
||||
// for dual conversion. For this basic application it is never used.
|
||||
LL_GPIO_SetPinMode(pin_port(pin->port), (uint32_t)pin_mask(pin->number), LL_GPIO_MODE_ANALOG);
|
||||
if (pin->adc_unit & 0x01) {
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC1);
|
||||
} else if (pin->adc_unit == 0x04) {
|
||||
#ifdef LL_APB2_GRP1_PERIPH_ADC3
|
||||
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_ADC3);
|
||||
#endif
|
||||
} else {
|
||||
mp_raise_ValueError(translate("Invalid ADC Unit value"));
|
||||
}
|
||||
claim_pin(pin);
|
||||
self->pin = pin;
|
||||
}
|
||||
|
||||
bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t *self) {
|
||||
return self->pin == mp_const_none;
|
||||
}
|
||||
|
||||
void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t *self) {
|
||||
if (common_hal_analogio_analogin_deinited(self)) {
|
||||
return;
|
||||
}
|
||||
reset_pin_number(self->pin->port,self->pin->number);
|
||||
self->pin = mp_const_none;
|
||||
}
|
||||
|
||||
uint16_t common_hal_analogio_analogin_get_value(analogio_analogin_obj_t *self) {
|
||||
// Something else might have used the ADC in a different way,
|
||||
// so we completely re-initialize it.
|
||||
ADC_TypeDef * ADCx;
|
||||
|
||||
if(self->pin->adc_unit & 0x01) {
|
||||
ADCx = ADC1;
|
||||
} else if (self->pin->adc_unit == 0x04) {
|
||||
#ifdef ADC3
|
||||
ADCx = ADC3;
|
||||
#endif
|
||||
} else {
|
||||
mp_raise_ValueError(translate("Invalid ADC Unit value"));
|
||||
}
|
||||
|
||||
LL_GPIO_SetPinMode(pin_port(self->pin->port), (uint32_t)pin_mask(self->pin->number), LL_GPIO_MODE_ANALOG);
|
||||
//LL_GPIO_PIN_0
|
||||
|
||||
//HAL Implementation
|
||||
ADC_HandleTypeDef AdcHandle;
|
||||
ADC_ChannelConfTypeDef sConfig;
|
||||
|
||||
AdcHandle.Instance = ADCx;
|
||||
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
||||
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
|
||||
AdcHandle.Init.ScanConvMode = DISABLE;
|
||||
AdcHandle.Init.ContinuousConvMode = DISABLE;
|
||||
AdcHandle.Init.DiscontinuousConvMode = DISABLE;
|
||||
AdcHandle.Init.NbrOfDiscConversion = 0;
|
||||
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
|
||||
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
||||
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
||||
AdcHandle.Init.NbrOfConversion = 1;
|
||||
AdcHandle.Init.DMAContinuousRequests = DISABLE;
|
||||
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
||||
HAL_ADC_Init(&AdcHandle);
|
||||
|
||||
sConfig.Channel = (uint32_t)self->pin->adc_channel; //ADC_CHANNEL_0 <-normal iteration, not mask
|
||||
sConfig.Rank = 1;
|
||||
sConfig.SamplingTime = ADC_SAMPLETIME_15CYCLES; //Taken from micropython
|
||||
HAL_ADC_ConfigChannel(&AdcHandle, &sConfig);
|
||||
|
||||
HAL_ADC_Start(&AdcHandle);
|
||||
HAL_ADC_PollForConversion(&AdcHandle,1);
|
||||
uint16_t value = (uint16_t)HAL_ADC_GetValue(&AdcHandle);
|
||||
HAL_ADC_Stop(&AdcHandle);
|
||||
|
||||
// // Shift the value to be 16 bit.
|
||||
return value << 4;
|
||||
}
|
||||
|
||||
float common_hal_analogio_analogin_get_reference_voltage(analogio_analogin_obj_t *self) {
|
||||
return 3.3f;
|
||||
}
|
48
ports/stm32f4/common-hal/analogio/AnalogIn.h
Normal file
48
ports/stm32f4/common-hal/analogio/AnalogIn.h
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGIN_H
|
||||
#define MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGIN_H
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
const mcu_pin_obj_t * pin;
|
||||
} analogio_analogin_obj_t;
|
||||
|
||||
static inline uint8_t stm32_adc_units(uint8_t adc_packed) {
|
||||
return adc_packed >> 5;
|
||||
}
|
||||
|
||||
static inline uint8_t stm32_adc_channel(uint8_t adc_packed) {
|
||||
return adc_packed & 0x1f;
|
||||
}
|
||||
|
||||
#endif // MICROPY_INCLUDED_STM32F4_COMMON_HAL_ANALOGIO_ANALOGIN_H
|
57
ports/stm32f4/common-hal/analogio/AnalogOut.c
Normal file
57
ports/stm32f4/common-hal/analogio/AnalogOut.c
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2013, 2014 Damien P. George
|
||||
*
|
||||
* 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 <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "py/mperrno.h"
|
||||
#include "py/runtime.h"
|
||||
|
||||
#include "shared-bindings/analogio/AnalogOut.h"
|
||||
#include "shared-bindings/microcontroller/Pin.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
void common_hal_analogio_analogout_construct(analogio_analogout_obj_t* self,
|
||||
const mcu_pin_obj_t *pin) {
|
||||
mp_raise_ValueError(translate("DAC not supported"));
|
||||
}
|
||||
|
||||
bool common_hal_analogio_analogout_deinited(analogio_analogout_obj_t *self) {
|
||||
return self->deinited;
|
||||
}
|
||||
|
||||
void common_hal_analogio_analogout_deinit(analogio_analogout_obj_t *self) {
|
||||
|
||||
}
|
||||
|
||||
void common_hal_analogio_analogout_set_value(analogio_analogout_obj_t *self,
|
||||
uint16_t value) {
|
||||
}
|
||||
|
||||
void analogout_reset(void) {
|
||||
// audioout_reset also resets the DAC, and does a smooth ramp down to avoid clicks
|
||||
// if it was enabled, so do that instead if AudioOut is enabled.
|
||||
}
|
42
ports/stm32f4/common-hal/analogio/AnalogOut.h
Normal file
42
ports/stm32f4/common-hal/analogio/AnalogOut.h
Normal file
@ -0,0 +1,42 @@
|
||||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2016 Scott Shawcroft
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
|
||||
#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "py/obj.h"
|
||||
|
||||
typedef struct {
|
||||
mp_obj_base_t base;
|
||||
uint8_t channel;
|
||||
bool deinited;
|
||||
} analogio_analogout_obj_t;
|
||||
|
||||
void analogout_reset(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_ANALOGIO_ANALOGOUT_H
|
1
ports/stm32f4/common-hal/analogio/__init__.c
Normal file
1
ports/stm32f4/common-hal/analogio/__init__.c
Normal file
@ -0,0 +1 @@
|
||||
// No analogio module functions.
|
@ -28,6 +28,7 @@
|
||||
#include "shared-bindings/digitalio/DigitalInOut.h"
|
||||
#include "py/runtime.h"
|
||||
#include "supervisor/shared/translate.h"
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4xx_ll_gpio.h"
|
||||
|
||||
@ -47,7 +48,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct(
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
|
||||
|
||||
return DIGITALINOUT_OK;
|
||||
}
|
||||
@ -73,7 +74,7 @@ void common_hal_digitalio_digitalinout_switch_to_input(
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
|
||||
|
||||
common_hal_digitalio_digitalinout_set_pull(self, pull);
|
||||
}
|
||||
@ -89,20 +90,20 @@ void common_hal_digitalio_digitalinout_switch_to_output(
|
||||
digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
|
||||
digitalio_digitalinout_obj_t *self) {
|
||||
|
||||
return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number))
|
||||
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(
|
||||
digitalio_digitalinout_obj_t *self, bool value) {
|
||||
HAL_GPIO_WritePin(pin_port(self->pin->number), pin_mask(self->pin->number), value);
|
||||
HAL_GPIO_WritePin(pin_port(self->pin->port), pin_mask(self->pin->number), value);
|
||||
}
|
||||
|
||||
bool common_hal_digitalio_digitalinout_get_value(
|
||||
digitalio_digitalinout_obj_t *self) {
|
||||
return (LL_GPIO_GetPinMode(pin_port(self->pin->number), pin_mask(self->pin->number)) == LL_GPIO_MODE_INPUT)
|
||||
? HAL_GPIO_ReadPin(pin_port(self->pin->number), pin_mask(self->pin->number))
|
||||
: LL_GPIO_IsOutputPinSet(pin_port(self->pin->number), pin_mask(self->pin->number));
|
||||
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));
|
||||
}
|
||||
|
||||
void common_hal_digitalio_digitalinout_set_drive_mode(
|
||||
@ -114,13 +115,13 @@ void common_hal_digitalio_digitalinout_set_drive_mode(
|
||||
GPIO_MODE_OUTPUT_OD : GPIO_MODE_OUTPUT_PP);
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
||||
HAL_GPIO_Init(pin_port(self->pin->number), &GPIO_InitStruct);
|
||||
HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
digitalio_drive_mode_t common_hal_digitalio_digitalinout_get_drive_mode(
|
||||
digitalio_digitalinout_obj_t *self) {
|
||||
|
||||
return LL_GPIO_GetPinOutputType(pin_port(self->pin->number), pin_mask(self->pin->number))
|
||||
return LL_GPIO_GetPinOutputType(pin_port(self->pin->port), pin_mask(self->pin->number))
|
||||
== LL_GPIO_OUTPUT_OPENDRAIN ? DRIVE_MODE_OPEN_DRAIN : DRIVE_MODE_PUSH_PULL;
|
||||
}
|
||||
|
||||
@ -129,13 +130,13 @@ void common_hal_digitalio_digitalinout_set_pull(
|
||||
|
||||
switch (pull) {
|
||||
case PULL_UP:
|
||||
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_UP);
|
||||
LL_GPIO_SetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number),LL_GPIO_PULL_UP);
|
||||
break;
|
||||
case PULL_DOWN:
|
||||
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN);
|
||||
LL_GPIO_SetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number),LL_GPIO_PULL_DOWN);
|
||||
break;
|
||||
case PULL_NONE:
|
||||
LL_GPIO_SetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number),LL_GPIO_PULL_NO);
|
||||
LL_GPIO_SetPinPull(pin_port(self->pin->port), pin_mask(self->pin->number),LL_GPIO_PULL_NO);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -146,7 +147,7 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
|
||||
digitalio_digitalinout_obj_t *self) {
|
||||
|
||||
|
||||
switch (LL_GPIO_GetPinPull(pin_port(self->pin->number), pin_mask(self->pin->number))) {
|
||||
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:
|
||||
|
@ -17,6 +17,7 @@ CIRCUITPY_MINIMAL_BUILD = 1
|
||||
|
||||
CIRCUITPY_BOARD = 1
|
||||
CIRCUITPY_DIGITALIO = 1
|
||||
CIRCUITPY_ANALOGIO = 1
|
||||
CIRCUITPY_MICROCONTROLLER = 1
|
||||
CIRCUITPY_BUSIO = 1
|
||||
CIRCUITPY_TIME = 1
|
||||
|
@ -44,9 +44,10 @@ typedef struct {
|
||||
uint8_t adc_channel:5;
|
||||
} mcu_pin_obj_t;
|
||||
|
||||
//Standard stm32 adc unit combinations
|
||||
#define ADC_1 1
|
||||
#define ADC_123 7
|
||||
#define ADC_12 3
|
||||
#define ADC_123 7
|
||||
#define ADC_3 4
|
||||
|
||||
//STM32 ADC pins can have a combination of 1, 2 or all 3 ADCs on a single pin,
|
||||
|
@ -106,6 +106,7 @@
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4/gpio.h"
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
void stm32f4_peripherals_gpio_init(void) {
|
||||
//Enable all GPIO for now
|
||||
@ -133,6 +134,20 @@ void stm32f4_peripherals_gpio_init(void) {
|
||||
stm32f4_peripherals_status_led(1,0);
|
||||
stm32f4_peripherals_status_led(2,0);
|
||||
stm32f4_peripherals_status_led(3,0);
|
||||
|
||||
//Never reset pins
|
||||
never_reset_pin_number(2,13); //PC13 anti tamp
|
||||
never_reset_pin_number(2,14); //PC14 OSC32_IN
|
||||
never_reset_pin_number(2,15); //PC15 OSC32_OUT
|
||||
never_reset_pin_number(0,13); //PA13 SWDIO
|
||||
never_reset_pin_number(0,14); //PA14 SWCLK
|
||||
never_reset_pin_number(0,15); //PA15 JTDI
|
||||
never_reset_pin_number(1,3); //PB3 JTDO
|
||||
never_reset_pin_number(1,4); //PB4 JTRST
|
||||
|
||||
// Port H is not included in GPIO port array
|
||||
// never_reset_pin_number(5,0); //PH0 JTDO
|
||||
// never_reset_pin_number(5,1); //PH1 JTRST
|
||||
}
|
||||
|
||||
//LEDs are inverted on F411 DISCO
|
||||
|
@ -94,9 +94,9 @@ const mcu_pin_obj_t pin_PA09 = PIN(0, 9, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA10 = PIN(0, 10, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA11 = PIN(0, 11, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA12 = PIN(0, 12, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA13 = PIN(0, 13, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA14 = PIN(0, 14, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA15 = PIN(0, 15, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PA13 = PIN(0, 13, NO_ADC); //SWDIO
|
||||
const mcu_pin_obj_t pin_PA14 = PIN(0, 14, NO_ADC); //SWCLK
|
||||
const mcu_pin_obj_t pin_PA15 = PIN(0, 15, NO_ADC); //JTDI
|
||||
|
||||
const mcu_pin_obj_t pin_PC10 = PIN(2, 10, NO_ADC);
|
||||
const mcu_pin_obj_t pin_PC11 = PIN(2, 11, NO_ADC);
|
||||
|
@ -179,6 +179,7 @@
|
||||
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "stm32f4/gpio.h"
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
void stm32f4_peripherals_gpio_init(void) {
|
||||
//Enable all GPIO for now
|
||||
@ -207,6 +208,20 @@ void stm32f4_peripherals_gpio_init(void) {
|
||||
stm32f4_peripherals_status_led(1,0);
|
||||
stm32f4_peripherals_status_led(2,0);
|
||||
stm32f4_peripherals_status_led(3,0);
|
||||
|
||||
//Never reset pins
|
||||
never_reset_pin_number(2,13); //PC13 anti tamp
|
||||
never_reset_pin_number(2,14); //PC14 OSC32_IN
|
||||
never_reset_pin_number(2,15); //PC15 OSC32_OUT
|
||||
never_reset_pin_number(0,13); //PA13 SWDIO
|
||||
never_reset_pin_number(0,14); //PA14 SWCLK
|
||||
never_reset_pin_number(0,15); //PA15 JTDI
|
||||
never_reset_pin_number(1,3); //PB3 JTDO
|
||||
never_reset_pin_number(1,4); //PB4 JTRST
|
||||
|
||||
// Port H is not included in GPIO port array
|
||||
// never_reset_pin_number(5,0); //PH0 JTDO
|
||||
// never_reset_pin_number(5,1); //PH1 JTRST
|
||||
}
|
||||
|
||||
//LEDs are inverted on F411 DISCO
|
||||
|
@ -30,6 +30,8 @@
|
||||
#include "boards/board.h"
|
||||
#include "tick.h"
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
#include "stm32f4/clocks.h"
|
||||
#include "stm32f4/gpio.h"
|
||||
|
||||
@ -48,7 +50,7 @@ safe_mode_t port_init(void) {
|
||||
}
|
||||
|
||||
void reset_port(void) {
|
||||
|
||||
reset_all_pins();
|
||||
}
|
||||
|
||||
void reset_to_bootloader(void) {
|
||||
@ -56,7 +58,7 @@ void reset_to_bootloader(void) {
|
||||
}
|
||||
|
||||
void reset_cpu(void) {
|
||||
|
||||
NVIC_SystemReset();
|
||||
}
|
||||
|
||||
extern uint32_t _ebss;
|
||||
@ -70,5 +72,8 @@ uint32_t port_get_saved_word(void) {
|
||||
}
|
||||
|
||||
void HardFault_Handler(void) {
|
||||
while(1) {}
|
||||
reset_into_safe_mode(HARD_CRASH);
|
||||
while (true) {
|
||||
asm("nop;");
|
||||
}
|
||||
}
|
||||
|
@ -32,6 +32,8 @@
|
||||
#include "lib/mp-readline/readline.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#include "common-hal/microcontroller/Pin.h"
|
||||
|
||||
void init_usb_hardware(void) {
|
||||
//TODO: if future chips overload this with options, move to peripherals management.
|
||||
|
||||
@ -50,12 +52,15 @@ void init_usb_hardware(void) {
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
never_reset_pin_number(0, 11);
|
||||
never_reset_pin_number(0, 12);
|
||||
|
||||
/* Configure VBUS Pin */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_9;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
never_reset_pin_number(0, 9);
|
||||
|
||||
/* This for ID line debug */
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_10;
|
||||
@ -64,6 +69,7 @@ void init_usb_hardware(void) {
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF10_OTG_FS;
|
||||
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
||||
never_reset_pin_number(0, 10);
|
||||
|
||||
#ifdef STM32F412Zx
|
||||
/* Configure POWER_SWITCH IO pin (F412 ONLY)*/
|
||||
@ -71,6 +77,7 @@ void init_usb_hardware(void) {
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
|
||||
never_reset_pin_number(0, 8);
|
||||
#endif
|
||||
|
||||
/* Peripheral clock enable */
|
||||
|
@ -54,6 +54,11 @@ void SysTick_Handler(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t HAL_GetTick(void) //override ST HAL
|
||||
{
|
||||
return (uint32_t)ticks_ms;
|
||||
}
|
||||
|
||||
void tick_init() {
|
||||
uint32_t ticks_per_ms = SystemCoreClock/ 1000;
|
||||
SysTick_Config(ticks_per_ms); // interrupt is enabled
|
||||
|
Loading…
Reference in New Issue
Block a user