Skip to content

Commit d976856

Browse files
fprfpistm
authored andcommitted
uart, analog(ADC, PWM), digital validated.
Disconnects debug pins if used but debug pins can be locked if needed. Signed-off-by: fpr <[email protected]>
1 parent 1ee9be7 commit d976856

File tree

6 files changed

+139
-81
lines changed

6 files changed

+139
-81
lines changed

cores/arduino/stm32/PinAF_STM32F1.h

Lines changed: 89 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -28,74 +28,98 @@
2828
*******************************************************************************
2929
*/
3030

31-
#ifndef _PINAF_STM32F1_H
32-
#define _PINAF_STM32F1_H
31+
#ifndef _PINAF_STM32F1_H
32+
#define _PINAF_STM32F1_H
3333

34-
#include "Arduino.h"
34+
#ifdef STM32F1xx
3535

36-
#ifdef __cplusplus
37-
extern "C" {
38-
#endif
36+
#include "Arduino.h"
3937

40-
static inline void pin_SetF1AFPin(uint32_t afnum)
41-
{
42-
// Enable AFIO clock
43-
__HAL_RCC_AFIO_CLK_ENABLE();
38+
#ifdef __cplusplus
39+
extern "C" {
40+
#endif
4441

45-
if (afnum > 0) {
46-
switch (afnum) {
47-
case 1: // Remap SPI1
48-
__HAL_AFIO_REMAP_SPI1_ENABLE();
49-
break;
50-
case 2: // Remap I2C1
51-
__HAL_AFIO_REMAP_I2C1_ENABLE();
52-
break;
53-
case 3: // Remap USART1
54-
__HAL_AFIO_REMAP_USART1_ENABLE();
55-
break;
56-
case 4: // Remap USART2
57-
__HAL_AFIO_REMAP_USART2_ENABLE();
58-
break;
59-
case 5: // Partial Remap USART3
60-
__HAL_AFIO_REMAP_USART3_PARTIAL();
61-
break;
62-
case 6: // Partial Remap TIM1
63-
__HAL_AFIO_REMAP_TIM1_PARTIAL();
64-
break;
65-
case 7: // Partial Remap TIM3
66-
__HAL_AFIO_REMAP_TIM3_PARTIAL();
67-
break;
68-
case 8: // Full Remap TIM2
69-
__HAL_AFIO_REMAP_TIM2_ENABLE();
70-
break;
71-
case 9: // Full Remap TIM3
72-
__HAL_AFIO_REMAP_TIM3_ENABLE();
73-
break;
74-
#if defined(AFIO_MAPR_CAN_REMAP_REMAP1)
75-
case 10: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
76-
__HAL_AFIO_REMAP_CAN1_2();
77-
break;
78-
case 11: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
79-
__HAL_AFIO_REMAP_CAN1_3();
80-
break;
81-
#endif
82-
case 12: // Full Remap USART3
83-
__HAL_AFIO_REMAP_USART3_ENABLE();
84-
break;
85-
case 13: // Full Remap TIM1
86-
__HAL_AFIO_REMAP_TIM1_ENABLE();
87-
break;
88-
case 14: // Full Remap TIM4
89-
__HAL_AFIO_REMAP_TIM4_ENABLE();
90-
break;
91-
default:
92-
break;
93-
}
94-
}
95-
}
42+
static inline void pinF1_DisconnectDebug(PinName pin)
43+
{
44+
/** Enable this flag gives the possibility to use debug pins without any risk
45+
* to lose traces
46+
*/
47+
#ifndef STM32F1_LOCK_DEBUG
48+
// Enable AFIO clock
49+
__HAL_RCC_AFIO_CLK_ENABLE();
9650

97-
#ifdef __cplusplus
98-
}
99-
#endif
51+
// Disconnect JTAG-DP + SW-DP signals.
52+
// Warning: Need to reconnect under reset
53+
if ((pin == PA13) || (pin == PA14)) {
54+
__HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
55+
}
56+
if ((pin == PA15) || (pin == PB3) || (pin == PB4)) {
57+
__HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
58+
}
59+
#endif /* STM32F1_FORCE_DEBUG */
60+
}
10061

101-
#endif /* _PINAF_STM32F1_H */
62+
static inline void pin_SetF1AFPin(uint32_t afnum)
63+
{
64+
// Enable AFIO clock
65+
__HAL_RCC_AFIO_CLK_ENABLE();
66+
67+
if (afnum > 0) {
68+
switch (afnum) {
69+
case 1: // Remap SPI1
70+
__HAL_AFIO_REMAP_SPI1_ENABLE();
71+
break;
72+
case 2: // Remap I2C1
73+
__HAL_AFIO_REMAP_I2C1_ENABLE();
74+
break;
75+
case 3: // Remap USART1
76+
__HAL_AFIO_REMAP_USART1_ENABLE();
77+
break;
78+
case 4: // Remap USART2
79+
__HAL_AFIO_REMAP_USART2_ENABLE();
80+
break;
81+
case 5: // Partial Remap USART3
82+
__HAL_AFIO_REMAP_USART3_PARTIAL();
83+
break;
84+
case 6: // Partial Remap TIM1
85+
__HAL_AFIO_REMAP_TIM1_PARTIAL();
86+
break;
87+
case 7: // Partial Remap TIM3
88+
__HAL_AFIO_REMAP_TIM3_PARTIAL();
89+
break;
90+
case 8: // Full Remap TIM2
91+
__HAL_AFIO_REMAP_TIM2_ENABLE();
92+
break;
93+
case 9: // Full Remap TIM3
94+
__HAL_AFIO_REMAP_TIM3_ENABLE();
95+
break;
96+
#if defined(AFIO_MAPR_CAN_REMAP_REMAP1)
97+
case 10: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
98+
__HAL_AFIO_REMAP_CAN1_2();
99+
break;
100+
case 11: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
101+
__HAL_AFIO_REMAP_CAN1_3();
102+
break;
103+
#endif
104+
case 12: // Full Remap USART3
105+
__HAL_AFIO_REMAP_USART3_ENABLE();
106+
break;
107+
case 13: // Full Remap TIM1
108+
__HAL_AFIO_REMAP_TIM1_ENABLE();
109+
break;
110+
case 14: // Full Remap TIM4
111+
__HAL_AFIO_REMAP_TIM4_ENABLE();
112+
break;
113+
default:
114+
break;
115+
}
116+
}
117+
}
118+
119+
#ifdef __cplusplus
120+
}
121+
#endif
122+
123+
#endif /* STM32F1xx */
124+
125+
#endif /* _PINAF_STM32F1_H */

cores/arduino/stm32/analog.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "hw_config.h"
5151
#include "analog.h"
5252
#include "timer.h"
53+
#include "PinAF_STM32F1.h"
5354

5455
#ifdef __cplusplus
5556
extern "C" {
@@ -83,6 +84,7 @@
8384
#error "ADC SAMPLINGTIME could not be defined"
8485
#endif
8586

87+
#ifndef STM32F1xx
8688
#ifdef ADC_CLOCK_SYNC_PCLK_DIV2
8789
#define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV2
8890
#elif defined(ADC_CLOCK_ASYNC_DIV1)
@@ -92,6 +94,7 @@
9294
#else
9395
#error "ADC_CLOCK_DIV could not be defined"
9496
#endif
97+
#endif /* STM32F1xx */
9598

9699
#ifndef ADC_REGULAR_RANK_1
97100
#define ADC_REGULAR_RANK_1 1
@@ -432,7 +435,8 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
432435
#ifdef __HAL_RCC_ADC_CLK_ENABLE
433436
__HAL_RCC_ADC_CLK_ENABLE();
434437
#endif
435-
#ifdef __HAL_RCC_ADC_CONFIG
438+
/* For STM32F1xx, ADC prescaler is confgured in SystemClock_Config (variant.cpp) */
439+
#if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx)
436440
/* ADC Periph interface clock configuration */
437441
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
438442
#endif
@@ -557,16 +561,22 @@ uint16_t adc_read_value(PinName pin)
557561

558562
if (AdcHandle.Instance == NP) return 0;
559563

564+
#ifndef STM32F1xx
560565
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV; /* Asynchronous clock mode, input ADC clock divided */
561566
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit resolution for converted data */
567+
#endif
562568
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment for converted data */
563569
AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
570+
#ifndef STM32F1xx
564571
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */
572+
#endif
565573
AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */
566574
AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
567575
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */
576+
#ifndef STM32F1xx
568577
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
569578
AdcHandle.Init.DMAContinuousRequests = DISABLE; /* DMA one-shot mode selected (not applied to this example) */
579+
#endif
570580
AdcHandle.State = HAL_ADC_STATE_RESET;
571581
#if defined (STM32F0xx) || defined (STM32L0xx)
572582
AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */
@@ -687,7 +697,11 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
687697
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
688698
GPIO_InitStruct.Pull = GPIO_NOPULL;
689699
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
700+
#ifdef STM32F1xx
701+
pin_SetF1AFPin(STM_PIN_AFNUM(function));
702+
#else
690703
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function);
704+
#endif /* STM32F1xx */
691705
GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin);
692706

693707
HAL_GPIO_Init(port, &GPIO_InitStruct);

cores/arduino/stm32/digital_io.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "digital_io.h"
3939
#include "stm32_def.h"
4040
#include "hw_config.h"
41+
#include "PinAF_STM32F1.h"
4142

4243
#ifdef __cplusplus
4344
extern "C" {
@@ -59,6 +60,9 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull)
5960
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
6061
GPIO_InitStructure.Mode = mode;
6162
GPIO_InitStructure.Pull = pull;
63+
#ifdef STM32F1xx
64+
pinF1_DisconnectDebug(pin);
65+
#endif /* STM32F1xx */
6266
HAL_GPIO_Init(port, &GPIO_InitStructure);
6367
}
6468

cores/arduino/stm32/uart.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#include "digital_io.h"
5252
#include "interrupt.h"
5353
#include "variant.h"
54+
#include "PinAF_STM32F1.h"
5455

5556
#ifdef __cplusplus
5657
extern "C" {
@@ -215,7 +216,11 @@ void uart_init(serial_t *obj)
215216
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_rx,PinMap_UART_RX));
216217
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
217218
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_rx,PinMap_UART_RX));
219+
#ifdef STM32F1xx
220+
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_rx,PinMap_UART_RX)));
221+
#else
218222
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_rx,PinMap_UART_RX));
223+
#endif /* STM32F1xx */
219224
HAL_GPIO_Init(port, &GPIO_InitStruct);
220225

221226
//TX
@@ -224,7 +229,11 @@ void uart_init(serial_t *obj)
224229
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_tx,PinMap_UART_TX));
225230
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
226231
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_tx,PinMap_UART_TX));
232+
#ifdef STM32F1xx
233+
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_tx,PinMap_UART_TX)));
234+
#else
227235
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_tx,PinMap_UART_TX));
236+
#endif /* STM32F1xx */
228237
HAL_GPIO_Init(port, &GPIO_InitStruct);
229238

230239
//Configure uart
@@ -519,7 +528,7 @@ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
519528
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
520529
{
521530
volatile uint32_t tmpval;
522-
#ifdef STM32F4xx
531+
#if defined(STM32F4xx) || defined(STM32F1xx)
523532
if (__HAL_UART_GET_FLAG(huart, UART_FLAG_PE) != RESET) {
524533
tmpval = huart->Instance->DR; // Clear PE flag
525534
} else if (__HAL_UART_GET_FLAG(huart, UART_FLAG_FE) != RESET) {
@@ -659,4 +668,5 @@ void UART8_IRQHandler(void)
659668
}
660669
#endif
661670

671+
662672
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

variants/NUCLEO_F103RB/variant.cpp

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,25 @@ const PinName digital_arduino[] = {
9191
* UART objects
9292
*/
9393

94-
// HardwareSerial Serial(PA3, PA2); // Connected to ST-Link
95-
// HardwareSerial Serial1(PA10, PA9);
96-
//
97-
// void serialEvent() __attribute__((weak));
98-
// void serialEvent() { }
99-
//
100-
// void serialEvent1() __attribute__((weak));
101-
// void serialEvent1() { }
102-
//
103-
// void serialEventRun(void)
104-
// {
105-
// if (Serial.available()) serialEvent();
106-
// if (Serial1.available()) serialEvent1();
107-
// }
94+
HardwareSerial Serial(PA3, PA2); // Connected to ST-Link
95+
HardwareSerial Serial1(PA10, PA9);
96+
HardwareSerial Serial2(PC11, PC10); //Morpho pins
97+
98+
void serialEvent() __attribute__((weak));
99+
void serialEvent() { }
100+
101+
void serialEvent1() __attribute__((weak));
102+
void serialEvent1() { }
103+
104+
void serialEvent2() __attribute__((weak));
105+
void serialEvent2() { }
106+
107+
void serialEventRun(void)
108+
{
109+
if (Serial.available()) serialEvent();
110+
if (Serial1.available()) serialEvent1();
111+
if (Serial2.available()) serialEvent2();
112+
}
108113

109114
// ----------------------------------------------------------------------------
110115

variants/NUCLEO_F103RB/variant.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ uint32_t pinNametoPinNumber(PinName p);
152152
#ifdef __cplusplus
153153
extern HardwareSerial Serial;
154154
extern HardwareSerial Serial1;
155+
extern HardwareSerial Serial2;
155156

156157
// These serial port names are intended to allow libraries and architecture-neutral
157158
// sketches to automatically default to the correct port name for a particular type

0 commit comments

Comments
 (0)