Skip to content

STM32F1 merge: Nucleo F103RB #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jul 11, 2017
Merged
14 changes: 14 additions & 0 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ Nucleo_64.menu.board_part_num.NUCLEO_F091RC.build.product_line=STM32F091xC
Nucleo_64.menu.board_part_num.NUCLEO_F091RC.build.variant=NUCLEO_F091RC
Nucleo_64.menu.board_part_num.NUCLEO_F091RC.build.cmsis_lib_gcc=arm_cortexM0l_math

# NUCLEO_F103RB board
# Support: Serial1 (USART1 on PA10, PA9) and Serial2 (USART3 on PC11, PC10)
Nucleo_64.menu.board_part_num.NUCLEO_F103RB=Nucleo F103RB
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.node="NODE_F103RB,NUCLEO"
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.upload.maximum_size=131072
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.upload.maximum_data_size=20480
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.mcu=cortex-m3
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.f_cpu=72000000L
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.board=NUCLEO_F103RB
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.series=STM32F1xx
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.product_line=STM32F103xB
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.variant=NUCLEO_F103RB
Nucleo_64.menu.board_part_num.NUCLEO_F103RB.build.cmsis_lib_gcc=arm_cortexM3l_math

# NUCLEO_F303RE board
# Support: Serial1 (USART1 on PA10, PA9) and Serial2 (USART2 on PA1, PA0)
Nucleo_64.menu.board_part_num.NUCLEO_F303RE=Nucleo F303RE
Expand Down
7 changes: 7 additions & 0 deletions cores/arduino/WInterrupts.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
extern "C" {
#endif

#include "PinAF_STM32F1.h"

void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
{
uint32_t it_mode;
Expand All @@ -46,6 +48,11 @@ void attachInterrupt(uint32_t pin, void (*callback)(void), uint32_t mode)
it_mode = GPIO_MODE_IT_RISING;
break;
}

#ifdef STM32F1xx
pinF1_DisconnectDebug(p);
#endif /* STM32F1xx */

stm32_interrupt_enable(port, STM_GPIO_PIN(p), callback, it_mode);
}

Expand Down
125 changes: 125 additions & 0 deletions cores/arduino/stm32/PinAF_STM32F1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
*******************************************************************************
* Copyright (c) 2017, STMicroelectronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************
*/

#ifndef _PINAF_STM32F1_H
#define _PINAF_STM32F1_H

#ifdef STM32F1xx

#include "Arduino.h"

#ifdef __cplusplus
extern "C" {
#endif

static inline void pinF1_DisconnectDebug(PinName pin)
{
/** Enable this flag gives the possibility to use debug pins without any risk
* to lose traces
*/
#ifndef STM32F1_LOCK_DEBUG
// Enable AFIO clock
__HAL_RCC_AFIO_CLK_ENABLE();

// Disconnect JTAG-DP + SW-DP signals.
// Warning: Need to reconnect under reset
if ((pin == PA13) || (pin == PA14)) {
__HAL_AFIO_REMAP_SWJ_DISABLE(); // JTAG-DP Disabled and SW-DP Disabled
}
if ((pin == PA15) || (pin == PB3) || (pin == PB4)) {
__HAL_AFIO_REMAP_SWJ_NOJTAG(); // JTAG-DP Disabled and SW-DP enabled
}
#endif /* STM32F1_FORCE_DEBUG */
}

static inline void pin_SetF1AFPin(uint32_t afnum)
{
// Enable AFIO clock
__HAL_RCC_AFIO_CLK_ENABLE();

if (afnum > 0) {
switch (afnum) {
case 1: // Remap SPI1
__HAL_AFIO_REMAP_SPI1_ENABLE();
break;
case 2: // Remap I2C1
__HAL_AFIO_REMAP_I2C1_ENABLE();
break;
case 3: // Remap USART1
__HAL_AFIO_REMAP_USART1_ENABLE();
break;
case 4: // Remap USART2
__HAL_AFIO_REMAP_USART2_ENABLE();
break;
case 5: // Partial Remap USART3
__HAL_AFIO_REMAP_USART3_PARTIAL();
break;
case 6: // Partial Remap TIM1
__HAL_AFIO_REMAP_TIM1_PARTIAL();
break;
case 7: // Partial Remap TIM3
__HAL_AFIO_REMAP_TIM3_PARTIAL();
break;
case 8: // Full Remap TIM2
__HAL_AFIO_REMAP_TIM2_ENABLE();
break;
case 9: // Full Remap TIM3
__HAL_AFIO_REMAP_TIM3_ENABLE();
break;
#if defined(AFIO_MAPR_CAN_REMAP_REMAP1)
case 10: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
__HAL_AFIO_REMAP_CAN1_2();
break;
case 11: // CAN_RX mapped to PB8, CAN_TX mapped to PB9
__HAL_AFIO_REMAP_CAN1_3();
break;
#endif
case 12: // Full Remap USART3
__HAL_AFIO_REMAP_USART3_ENABLE();
break;
case 13: // Full Remap TIM1
__HAL_AFIO_REMAP_TIM1_ENABLE();
break;
case 14: // Full Remap TIM4
__HAL_AFIO_REMAP_TIM4_ENABLE();
break;
default:
break;
}
}
}

#ifdef __cplusplus
}
#endif

#endif /* STM32F1xx */

#endif /* _PINAF_STM32F1_H */
22 changes: 16 additions & 6 deletions cores/arduino/stm32/analog.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include "hw_config.h"
#include "analog.h"
#include "timer.h"
#include "PinAF_STM32F1.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -83,6 +84,7 @@
#error "ADC SAMPLINGTIME could not be defined"
#endif

#ifndef STM32F1xx
#ifdef ADC_CLOCK_SYNC_PCLK_DIV2
#define ADC_CLOCK_DIV ADC_CLOCK_SYNC_PCLK_DIV2
#elif defined(ADC_CLOCK_ASYNC_DIV1)
Expand All @@ -92,6 +94,7 @@
#else
#error "ADC_CLOCK_DIV could not be defined"
#endif
#endif /* STM32F1xx */

#ifndef ADC_REGULAR_RANK_1
#define ADC_REGULAR_RANK_1 1
Expand Down Expand Up @@ -432,7 +435,8 @@ void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
#ifdef __HAL_RCC_ADC_CLK_ENABLE
__HAL_RCC_ADC_CLK_ENABLE();
#endif
#ifdef __HAL_RCC_ADC_CONFIG
/* For STM32F1xx, ADC prescaler is confgured in SystemClock_Config (variant.cpp) */
#if defined(__HAL_RCC_ADC_CONFIG) && !defined(STM32F1xx)
/* ADC Periph interface clock configuration */
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_SYSCLK);
#endif
Expand Down Expand Up @@ -557,16 +561,18 @@ uint16_t adc_read_value(PinName pin)

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

#ifndef STM32F1xx
AdcHandle.Init.ClockPrescaler = ADC_CLOCK_DIV; /* Asynchronous clock mode, input ADC clock divided */
AdcHandle.Init.Resolution = ADC_RESOLUTION_12B; /* 12-bit resolution for converted data */
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
AdcHandle.Init.DMAContinuousRequests = DISABLE; /* DMA one-shot mode selected (not applied to this example) */
#endif
AdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT; /* Right-alignment for converted data */
AdcHandle.Init.ScanConvMode = DISABLE; /* Sequencer disabled (ADC conversion on only 1 channel: channel set on rank 1) */
AdcHandle.Init.EOCSelection = ADC_EOC_SINGLE_CONV; /* EOC flag picked-up to indicate conversion end */
AdcHandle.Init.ContinuousConvMode = DISABLE; /* Continuous mode disabled to have only 1 conversion at each conversion trig */
AdcHandle.Init.DiscontinuousConvMode = DISABLE; /* Parameter discarded because sequencer is disabled */
AdcHandle.Init.ExternalTrigConv = ADC_SOFTWARE_START; /* Software start to trig the 1st conversion manually, without external event */
AdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE; /* Parameter discarded because software trigger chosen */
AdcHandle.Init.DMAContinuousRequests = DISABLE; /* DMA one-shot mode selected (not applied to this example) */
AdcHandle.State = HAL_ADC_STATE_RESET;
#if defined (STM32F0xx) || defined (STM32L0xx)
AdcHandle.Init.LowPowerAutoWait = DISABLE; /* Auto-delayed conversion feature disabled */
Expand Down Expand Up @@ -609,9 +615,9 @@ uint16_t adc_read_value(PinName pin)
return 0;
}

#if defined (STM32F0xx) || defined (STM32F3xx) || defined (STM32L4xx)
#if defined (STM32F0xx) || defined (STM32F1xx) || defined (STM32F3xx) || defined (STM32L4xx)
/*##-2.1- Calibrate ADC then Start the conversion process ####################*/
#if defined (STM32F0xx)
#if defined (STM32F0xx) || defined (STM32F1xx)
if (HAL_ADCEx_Calibration_Start(&AdcHandle) != HAL_OK)
#else
if (HAL_ADCEx_Calibration_Start(&AdcHandle, ADC_SINGLE_ENDED) != HAL_OK)
Expand Down Expand Up @@ -687,7 +693,11 @@ void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim)
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
#ifdef STM32F1xx
pin_SetF1AFPin(STM_PIN_AFNUM(function));
#else
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(function);
#endif /* STM32F1xx */
GPIO_InitStruct.Pin = STM_GPIO_PIN(g_current_pin);

HAL_GPIO_Init(port, &GPIO_InitStruct);
Expand Down
4 changes: 4 additions & 0 deletions cores/arduino/stm32/digital_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include "digital_io.h"
#include "stm32_def.h"
#include "hw_config.h"
#include "PinAF_STM32F1.h"

#ifdef __cplusplus
extern "C" {
Expand All @@ -59,6 +60,9 @@ void digital_io_init(PinName pin, uint32_t mode, uint32_t pull)
GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStructure.Mode = mode;
GPIO_InitStructure.Pull = pull;
#ifdef STM32F1xx
pinF1_DisconnectDebug(pin);
#endif /* STM32F1xx */
HAL_GPIO_Init(port, &GPIO_InitStructure);
}

Expand Down
36 changes: 34 additions & 2 deletions cores/arduino/stm32/interrupt.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,23 +170,55 @@ uint8_t get_pin_id(uint16_t pin)
void stm32_interrupt_enable(GPIO_TypeDef *port, uint16_t pin, void (*callback)(void), uint32_t mode)
{
GPIO_InitTypeDef GPIO_InitStruct;
uint32_t pull;
uint8_t id = get_pin_id(pin);

#ifdef STM32F1xx
uint8_t position;
uint32_t CRxRegOffset = 0;
uint32_t ODRRegOffset = 0;
volatile uint32_t *CRxRegister;
const uint32_t ConfigMask = 0x00000008; //MODE0 == 0x0 && CNF0 == 0x2
#else
uint32_t pull;
#endif /* STM32F1xx */

// GPIO pin configuration
GPIO_InitStruct.Pin = pin;
GPIO_InitStruct.Mode = mode;

//read the pull mode directly in the register as no function exists to get it.
//Do it in case the user already defines the IO through the digital io
//interface
#ifndef STM32F1xx
pull = port->PUPDR;
#ifdef GPIO_PUPDR_PUPD0
pull &=(GPIO_PUPDR_PUPD0<<(id*2));
GPIO_InitStruct.Pull = (GPIO_PUPDR_PUPD0 & (pull>>(id*2)));
#else
pull &=(GPIO_PUPDR_PUPDR0<<(id*2));
GPIO_InitStruct.Pull = (GPIO_PUPDR_PUPDR0 & (pull>>(id*2)));
#endif
#endif /* GPIO_PUPDR_PUPD0 */
#else
CRxRegister = (pin < GPIO_PIN_8) ? &port->CRL : &port->CRH;

for (position = 0; position < 16; position++) {
if(pin == (0x0001 << position)) {
CRxRegOffset = (pin < GPIO_PIN_8) ? (position << 2) : ((position - 8) << 2);
ODRRegOffset = position;
}
}

if((*CRxRegister & ((GPIO_CRL_MODE0 | GPIO_CRL_CNF0) << CRxRegOffset)) == (ConfigMask << CRxRegOffset)) {
if((port->ODR & (GPIO_ODR_ODR0 << ODRRegOffset)) == (GPIO_ODR_ODR0 << ODRRegOffset)) {
GPIO_InitStruct.Pull = GPIO_PULLUP;
} else {
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
}
} else {
GPIO_InitStruct.Pull = GPIO_NOPULL;
}
#endif /* STM32F1xx */

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

HAL_GPIO_Init(port, &GPIO_InitStruct);
Expand Down
17 changes: 17 additions & 0 deletions cores/arduino/stm32/spi_com.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "stm32_def.h"
#include "hw_config.h"
#include "spi_com.h"
#include "PinAF_STM32F1.h"

#ifdef __cplusplus
extern "C" {
Expand Down Expand Up @@ -275,7 +276,11 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
#ifdef STM32F1xx
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI)));
#else
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_mosi,PinMap_SPI_MOSI));
#endif /* STM32F1xx */
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

Expand All @@ -285,7 +290,11 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
#ifdef STM32F1xx
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_miso,PinMap_SPI_MISO)));
#else
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_miso,PinMap_SPI_MISO));
#endif /* STM32F1xx */
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

Expand All @@ -303,7 +312,11 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
GPIO_InitStruct.Pull = GPIO_PULLUP;
}
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
#ifdef STM32F1xx
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK)));
#else
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_sclk,PinMap_SPI_SCLK));
#endif /* STM32F1xx */
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

Expand All @@ -313,7 +326,11 @@ void spi_init(spi_t *obj, uint32_t speed, spi_mode_e mode, uint8_t msb)
GPIO_InitStruct.Mode = STM_PIN_MODE(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
GPIO_InitStruct.Pull = STM_PIN_PUPD(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
#ifdef STM32F1xx
pin_SetF1AFPin(STM_PIN_AFNUM(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL)));
#else
GPIO_InitStruct.Alternate = STM_PIN_AFNUM(pinmap_function(obj->pin_ssel,PinMap_SPI_SSEL));
#endif /* STM32F1xx */
HAL_GPIO_Init(port, &GPIO_InitStruct);
}

Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/stm32/stm32_def_build.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
#define CMSIS_STARTUP_FILE "startup_stm32l476xx.s"
#elif defined(STM32L432xx)
#define CMSIS_STARTUP_FILE "startup_stm32l432xx.s"
#elif defined(STM32F103xB)
#define CMSIS_STARTUP_FILE "startup_stm32f103xb.s"
#else
#error UNKNOWN CHIP
#endif
Expand Down
Loading