Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions License.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ This files includes licensing information for parts of this repository.
* `cores/arduino/*.*` mainly contains modified files from Arduino, except the
files listed under [BSD 3-Clause License](#bsd-3-clause-license) and
[MIT License](#mit-license)
* `cores/arduino/stm32`
* `cores/arduino/stm32` - except the files listed under
[BSD 3-Clause License](#bsd-3-clause-license) and [MIT License](#mit-license)
* `cores/arduino/api` (own copy of the license in `cores/arduino/api/LICENSE`),
except the files listed under [MIT License](#mit-license)
* `libraries/EEPROM`, `libraries/IWatchdog`, `libraries/Keyboard`,
Expand All @@ -31,6 +32,8 @@ This files includes licensing information for parts of this repository.
* `libraries/I3C`, `libraries/RGB_LED_TLC59731` and `libraries/SubGhz`
* `libraries/SrcWrapper` - files with a BSD 3-Clause header, mainly
`libraries/SrcWrapper/src/stm32` and `libraries/SrcWrapper/inc`
* `cores/arduino/stm32/analog.cpp`, `cores/arduino/stm32/interrupt.cpp` and
`cores/arduino/stm32/uart.c`
* `variants` - files with a BSD 3-Clause header, such as `PeripheralPins.c`,
`variant_generic.*` and `generic_clock.c`

Expand All @@ -47,7 +50,7 @@ This files includes licensing information for parts of this repository.

[MIT License](#mit-license) is used for:
* `libraries/VirtIO/*/virtio*` and `libraries/VirtIO/*/VirtIOSerial.*`
* `libraries/SrcWrapper/*/HardwareTimer.*`
* `libraries/SrcWrapper/inc/HardwareTimer.h` and `cores/arduino/stm32/HardwareTimer.cpp`
* `libraries/SrcWrapper/src/new.cpp`
* `cores/arduino/api/Udp.h` and `cores/arduino/api/deprecated-avr-comp/avr/pgmspace.h`

Expand Down
4 changes: 4 additions & 0 deletions cores/arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,12 @@ add_library(core_bin STATIC EXCLUDE_FROM_ALL
main.cpp
pins_arduino.c
Serial.cpp
stm32/analog.cpp
stm32/HardwareTimer.cpp
stm32/interrupt.cpp
stm32/startup_stm32yyxx.c
stm32/startup_stm32yyxx.S
stm32/uart.c
Tone.cpp
WInterrupts.cpp
wiring_analog.c
Expand Down
20 changes: 20 additions & 0 deletions cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

#define ARDUINO_MAIN
#include <unistd.h>
#include "Arduino.h"

/**
Expand Down Expand Up @@ -54,6 +55,25 @@ __attribute__((constructor(101))) void premain()
init();
}

extern "C" {
/**
* @brief Halt on exit().
*
* Replaces the C library exit(), which pulls in the stdio cleanup. That
* links _write() and, through it, the whole UART driver in every sketch,
* even when main() never returns. Weak, so a sketch can provide its own.
*
* @note The atexit() handlers and static destructors are not run and stdio
* buffers are not flushed.
*
* @param status Exit status, unused.
*/
WEAK __attribute__((noreturn)) void exit(int status)
{
_exit(status);
}
}

/**
* @brief Main entry point of the Arduino application.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,44 @@ timerObj_t *HardwareTimer_Handle[TIMER_NUM] = {NULL};
#endif
#endif

#if !defined(USE_HALV2_DRIVER)
/**
* @brief Timer MSP initialization: enable the timer clock, then set priority
* of and enable the update interrupt and, if different, the capture
* compare interrupt.
* Called before HAL_TIM_Base_Init() as the registers are accessed by the HAL.
* @param obj timer object
*/
static void timer_msp_init(timerObj_t *obj)
{
TIM_TypeDef *instance = obj->handle.Instance;
enableTimerClock(instance);

// configure Update interrupt
HAL_NVIC_SetPriority(getTimerUpIrq(instance), obj->preemptPriority, obj->subPriority);
HAL_NVIC_EnableIRQ(getTimerUpIrq(instance));

if (getTimerCCIrq(instance) != getTimerUpIrq(instance)) {
// configure Capture Compare interrupt
HAL_NVIC_SetPriority(getTimerCCIrq(instance), obj->preemptPriority, obj->subPriority);
HAL_NVIC_EnableIRQ(getTimerCCIrq(instance));
}
}

/**
* @brief Timer MSP de-initialization: disable the timer clock and interrupts.
* Called after HAL_TIM_Base_DeInit().
* @param obj timer object
*/
static void timer_msp_deinit(timerObj_t *obj)
{
TIM_TypeDef *instance = obj->handle.Instance;
disableTimerClock(instance);
HAL_NVIC_DisableIRQ(getTimerUpIrq(instance));
HAL_NVIC_DisableIRQ(getTimerCCIrq(instance));
}
#endif /* !USE_HALV2_DRIVER */

HardwareTimer::HardwareTimer()
{
_timerObj.instance = nullptr;
Expand All @@ -76,8 +114,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance)
_timerObj.preemptPriority = TIM_IRQ_PRIO;
_timerObj.subPriority = TIM_IRQ_SUBPRIO;

/* Enable timer clock. Even if it is also done in HAL_TIM_Base_MspInit(),
it is done there so that it is possible to write registers right now */
/* Enable timer clock so that it is possible to write registers right now */
enableTimerClock(instance);

// Initialize NULL callbacks
Expand Down Expand Up @@ -106,7 +143,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance)
// HAL_TIM_SetUpdateSource(&_timerObj.handle, HAL_TIM_UPDATE_REGULAR);
// HAL_TIM_EnableUpdateGeneration(&_timerObj.handle);

// configure Update interrupt done in HAL_TIM_Base_MspInit for V1
// configure Update interrupt done in timer_msp_init for V1
HAL_CORTEX_NVIC_SetPriority(getTimerUpIrq(_timerObj.instance),
(hal_cortex_nvic_preemp_priority_t)_timerObj.preemptPriority,
(hal_cortex_nvic_sub_priority_t)_timerObj.subPriority);
Expand Down Expand Up @@ -140,6 +177,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance)
_timerObj.handle.Init.RepetitionCounter = 0;
#endif
_timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
timer_msp_init(&_timerObj);
HAL_TIM_Base_Init(&(_timerObj.handle));
#endif
}
Expand Down Expand Up @@ -1591,6 +1629,7 @@ void HardwareTimer::timerHandleDeinit()
#else
HAL_TIM_Base_Stop_IT(&(_timerObj.handle));
HAL_TIM_Base_DeInit(&(_timerObj.handle));
timer_msp_deinit(&_timerObj);
#endif
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ uint32_t get_adc_internal_channel(PinName pin)
* the GPIO pins.
* @param hadc ADC handle pointer.
*/
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
static void adc_msp_init(ADC_HandleTypeDef *hadc)
{
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* ADC Periph clock enable */
Expand Down Expand Up @@ -634,7 +634,7 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
* @brief ADC MSP de-initialization: disable the peripheral clock.
* @param hadc ADC handle pointer.
*/
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
static void adc_msp_deinit(ADC_HandleTypeDef *hadc)
{
#ifdef __HAL_RCC_ADC_FORCE_RESET
__HAL_RCC_ADC_FORCE_RESET();
Expand Down Expand Up @@ -978,8 +978,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
AdcHandle.Lock = HAL_UNLOCKED;
/* Some other ADC_HandleTypeDef fields exists but not required */

g_current_pin = pin; /* Needed for HAL_ADC_MspInit*/
g_current_pin = pin; /* Needed for adc_msp_init */

adc_msp_init(&AdcHandle);
if (HAL_ADC_Init(&AdcHandle) != HAL_OK) {
return 0;
}
Expand Down Expand Up @@ -1115,7 +1116,9 @@ uint16_t adc_read_value(PinName pin, uint32_t resolution)
return 0;
}

if (HAL_ADC_DeInit(&AdcHandle) != HAL_OK) {
HAL_StatusTypeDef deinit_status = HAL_ADC_DeInit(&AdcHandle);
adc_msp_deinit(&AdcHandle);
if (deinit_status != HAL_OK) {
return 0;
}

Expand Down Expand Up @@ -1293,7 +1296,7 @@ uint32_t get_dac_channel(PinName pin)
* the GPIO pins.
* @param hdac DAC handle pointer.
*/
void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
static void dac_msp_init(DAC_HandleTypeDef *hdac)
{
/* DAC Periph clock enable */
if (hdac->Instance == DAC1) {
Expand Down Expand Up @@ -1358,6 +1361,7 @@ void dac_write_value(PinName pin, uint32_t value, bool do_init)
if (do_init) {
/*##-1- Configure the DAC peripheral #######################################*/
g_current_pin = pin;
dac_msp_init(&DacHandle);
if (HAL_DAC_Init(&DacHandle) != HAL_OK) {
/* Initialization Error */
return;
Expand Down Expand Up @@ -1415,7 +1419,7 @@ void dac_write_value(PinName pin, uint32_t value, bool do_init)
* @brief DAC MSP de-initialization: disable the peripheral clock.
* @param hdac DAC handle pointer.
*/
void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
static void dac_msp_deinit(DAC_HandleTypeDef *hdac)
{
/* DAC Periph clock disable */
if (hdac->Instance == DAC1) {
Expand Down Expand Up @@ -1517,7 +1521,9 @@ void dac_stop(PinName pin)

HAL_DAC_Stop(&DacHandle, dacChannel);

if (HAL_DAC_DeInit(&DacHandle) != HAL_OK) {
HAL_StatusTypeDef deinit_status = HAL_DAC_DeInit(&DacHandle);
dac_msp_deinit(&DacHandle);
if (deinit_status != HAL_OK) {
/* DeInitialization Error */
return;
}
Expand Down
File renamed without changes.
24 changes: 22 additions & 2 deletions cores/arduino/wiring_analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,29 @@ extern "C" {
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\
(!defined(HAL_TIM_MODULE_ONLY) &&\
(defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1))))
//This is the list of the IOs configured
uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0};
/**
* @brief Stop the analog output (PWM or DAC) of a pin. Called by pinMode().
* @param pin Pin name.
*/
void analog_output_stop(PinName pin)
{
#if !defined(HAL_DAC_MODULE_ONLY) &&\
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1)))
if (pin_in_pinmap(pin, PinMap_DAC)) {
dac_stop(pin);
} else
#endif
{
#if !defined(HAL_TIM_MODULE_ONLY) &&\
(defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1)))
if (pin_in_pinmap(pin, PinMap_TIM)) {
pwm_stop(pin);
}
#endif /* !HAL_TIM_MODULE_ONLY && (HAL_TIM_MODULE_ENABLED || (USE_HAL_TIM_MODULE && (USE_HAL_TIM_MODULE == 1))) */
}
}
#endif

#define MAX_PWM_RESOLUTION 16
static int _writeResolution = PWM_RESOLUTION;
static int _internalWriteResolution =
Expand Down
28 changes: 14 additions & 14 deletions cores/arduino/wiring_digital.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ extern "C" {
#endif


extern pin_size_t g_anOutputPinConfigured[MAX_NB_PORT];
#if !defined(HAL_DAC_MODULE_ONLY) &&\
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\
(!defined(HAL_TIM_MODULE_ONLY) &&\
(defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1))))
//This is the list of the IOs configured as analog output
uint32_t g_anOutputPinConfigured[MAX_NB_PORT] = {0};
#endif

// Weak: only defined (wiring_analog.c) when the sketch uses analogWrite()
void analog_output_stop(PinName pin) __attribute__((weak));

/**
* @brief Set the mode of a pin.
Expand All @@ -48,20 +57,11 @@ void pinMode(pin_size_t pinNumber, PinMode pinMode)
// If the pin that support PWM or DAC output, we need to turn it off
#if !defined(HAL_DAC_MODULE_ONLY) &&\
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1))) ||\
(defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY))
(!defined(HAL_TIM_MODULE_ONLY) &&\
(defined(HAL_TIM_MODULE_ENABLED) || (defined(USE_HAL_TIM_MODULE) && (USE_HAL_TIM_MODULE == 1))))
if (is_pin_configured(p, g_anOutputPinConfigured)) {
#if !defined(HAL_DAC_MODULE_ONLY) &&\
(defined(HAL_DAC_MODULE_ENABLED) || (defined(USE_HAL_DAC_MODULE) && (USE_HAL_DAC_MODULE == 1)))
if (pin_in_pinmap(p, PinMap_DAC)) {
dac_stop(p);
} else
#endif
{
#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
if (pin_in_pinmap(p, PinMap_TIM)) {
pwm_stop(p);
}
#endif //HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY
if (analog_output_stop != NULL) {
analog_output_stop(p);
}
/* Unconditionally reset the pin configuration */
reset_pin_configured(p, g_anOutputPinConfigured);
Expand Down
2 changes: 1 addition & 1 deletion docs/customization/HAL-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ The following HAL system configuration values can be redefined:
* `USE_SD_TRANSCEIVER`: Enable the microSD transceiver

## Use HAL modules without Arduino ownership
STM32 peripherals have many powerful features. Some of them are used by default by the Arduino API: I2C, SPI, TIM, U(S)ART, ... and take over IRQ Handlers (ex: `TIMx_IRQHandler`) and other HAL weaked functions (ex: `HAL_XXX_MspInit()`).
STM32 peripherals have many powerful features. Some of them are used by default by the Arduino API: I2C, SPI, TIM, U(S)ART, ... and take over IRQ Handlers (ex: `TIMx_IRQHandler`) and other HAL weaked functions (ex: `HAL_UART_RxCpltCallback()`).
For advanced user applications, it could be useful to take control of those IRQ handlers.

Since core version 1.8.0, it is possible to disable the use of selected HAL modules by the Arduino API.
Expand Down
4 changes: 0 additions & 4 deletions libraries/SrcWrapper/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ add_library(SrcWrapper_bin OBJECT EXCLUDE_FROM_ALL
src/HAL/stm32yyxx_utils_fdcan.c
src/HAL/stm32yyxx_utils_i2c.c
src/HAL/stm32yyxx_utils_i3c.c
src/HardwareTimer.cpp
src/LL/stm32yyxx_ll_adc.c
src/LL/stm32yyxx_ll_bdma.c
src/LL/stm32yyxx_ll_comp.c
Expand Down Expand Up @@ -200,20 +199,17 @@ add_library(SrcWrapper_bin OBJECT EXCLUDE_FROM_ALL
src/LL/stm32yyxx_ll_usb.c
src/LL/stm32yyxx_ll_utils.c
src/new.cpp
src/stm32/analog.cpp
src/stm32/bootloader.c
src/stm32/clock.c
src/stm32/core_callback.c
src/stm32/dwt.c
src/stm32/hw_config.c
src/stm32/interrupt.cpp
src/stm32/otp.c
src/stm32/pinmap.c
src/stm32/PortNames.c
src/stm32/stm32_def.c
src/stm32/system_stm32yyxx.c
src/stm32/timer.c
src/stm32/uart.c
src/syscalls.c
)
target_link_libraries(SrcWrapper_bin PUBLIC SrcWrapper_usage)
Expand Down
17 changes: 17 additions & 0 deletions libraries/SrcWrapper/inc/PinConfigured.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,23 @@ extern "C" {
#define reset_pin_configured(pin, map) \
(map[PINCONF_INDEX(pin)] = map[PINCONF_INDEX(pin)] & (~PINCONF_BIT(pin)))

/**
* @brief Map of the pins configured as analog output (PWM or DAC) by analogWrite().
*
* Defined in wiring_digital.c so pinMode() does not depend on wiring_analog.c.
*/
extern uint32_t g_anOutputPinConfigured[];

/**
* @brief Stop the analog output (PWM or DAC) of a pin.
*
* Defined in wiring_analog.c and referenced weakly by pinMode(): the PWM and
* DAC code is only linked when the sketch uses analogWrite().
*
* @param pin Pin name.
*/
void analog_output_stop(PinName pin);

#ifdef __cplusplus
}
#endif
Expand Down
Loading
Loading