Skip to content

Commit 887fc11

Browse files
committed
Merge pull request #151 from bcostm/master
[NUCLEO_F030R8] Fix vectors remap in RAM issue and more...
2 parents a31ec9c + 6866978 commit 887fc11

File tree

10 files changed

+321
-115
lines changed

10 files changed

+321
-115
lines changed

libraries/mbed/targets/cmsis/TARGET_STM/TARGET_NUCLEO_F030R8/cmsis_nvic.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ static unsigned char vtor_remap = 0; // To keep track that the vectors remap is
3838
void NVIC_SetVector(IRQn_Type IRQn, uint32_t vector) {
3939
int i;
4040
// Space for dynamic vectors, initialised to allocate in R/W
41-
static volatile uint32_t* vectors = (uint32_t *)NVIC_RAM_VECTOR_ADDRESS;
41+
static volatile uint32_t *vectors = (uint32_t *)NVIC_RAM_VECTOR_ADDRESS;
4242

4343
// Copy and switch to dynamic vectors if first time called
4444
if (vtor_remap == 0) {
4545
uint32_t *old_vectors = (uint32_t *)NVIC_FLASH_VECTOR_ADDRESS;
4646
for (i = 0; i < NVIC_NUM_VECTORS; i++) {
4747
vectors[i] = old_vectors[i];
4848
}
49+
SYSCFG->CFGR1 |= 0x03; // Embedded SRAM mapped at 0x00000000
4950
vtor_remap = 1; // The vectors remap is done
5051
}
5152

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/analogin_api.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ static inline uint16_t adc_read(analogin_t *obj) {
122122
}
123123

124124
while(!ADC_GetFlagStatus(adc, ADC_FLAG_ADRDY)); // Wait ADC ready
125-
125+
126126
ADC_StartOfConversion(adc); // Start conversion
127127

128128
while(ADC_GetFlagStatus(adc, ADC_FLAG_EOC) == RESET); // Wait end of conversion

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@
4949

5050
#define DEVICE_RTC 1
5151

52-
#define DEVICE_PWMOUT 0
52+
#define DEVICE_PWMOUT 1
5353

54-
#define DEVICE_SLEEP 0
54+
#define DEVICE_SLEEP 1
5555

5656
//=======================================
5757

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/gpio_api.c

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
#include "pinmap.h"
3232
#include "error.h"
3333

34+
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
35+
3436
uint32_t gpio_set(PinName pin) {
3537
if (pin == NC) return 0;
3638

@@ -39,35 +41,17 @@ uint32_t gpio_set(PinName pin) {
3941
return (uint32_t)(1 << ((uint32_t)pin & 0xF)); // Return the pin mask
4042
}
4143

42-
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
44+
void gpio_init(gpio_t *obj, PinName pin, PinDirection direction) {
4345
GPIO_TypeDef *gpio;
44-
46+
4547
if (pin == NC) return;
4648

4749
uint32_t port_index = STM_PORT(pin);
48-
49-
// Get GPIO structure base address
50-
switch (port_index) {
51-
case PortA:
52-
gpio = (GPIO_TypeDef *)GPIOA_BASE;
53-
break;
54-
case PortB:
55-
gpio = (GPIO_TypeDef *)GPIOB_BASE;
56-
break;
57-
case PortC:
58-
gpio = (GPIO_TypeDef *)GPIOC_BASE;
59-
break;
60-
case PortD:
61-
gpio = (GPIO_TypeDef *)GPIOD_BASE;
62-
break;
63-
case PortF:
64-
gpio = (GPIO_TypeDef *)GPIOF_BASE;
65-
break;
66-
default:
67-
error("GPIO port number is not correct.");
68-
break;
69-
}
7050

51+
// Enable GPIO clock
52+
uint32_t gpio_add = Set_GPIO_Clock(port_index);
53+
gpio = (GPIO_TypeDef *)gpio_add;
54+
7155
// Fill GPIO object structure for future use
7256
obj->pin = pin;
7357
obj->mask = gpio_set(pin);
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/* mbed Microcontroller Library
2+
*******************************************************************************
3+
* Copyright (c) 2014, STMicroelectronics
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
* 3. Neither the name of STMicroelectronics nor the names of its contributors
15+
* may be used to endorse or promote products derived from this software
16+
* without specific prior written permission.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*******************************************************************************
29+
*/
30+
#include <stddef.h>
31+
#include "cmsis.h"
32+
33+
#include "gpio_irq_api.h"
34+
#include "pinmap.h"
35+
#include "error.h"
36+
37+
#define EDGE_NONE (0)
38+
#define EDGE_RISE (1)
39+
#define EDGE_FALL (2)
40+
#define EDGE_BOTH (3)
41+
42+
#define CHANNEL_NUM (3)
43+
44+
static uint32_t channel_ids[CHANNEL_NUM] = {0, 0, 0};
45+
static uint32_t channel_gpio[CHANNEL_NUM] = {0, 0, 0};
46+
static uint32_t channel_pin[CHANNEL_NUM] = {0, 0, 0};
47+
48+
static gpio_irq_handler irq_handler;
49+
50+
static void handle_interrupt_in(uint32_t irq_index) {
51+
52+
// Retrieve the gpio and pin that generate the irq
53+
GPIO_TypeDef *gpio = (GPIO_TypeDef *)(channel_gpio[irq_index]);
54+
uint32_t pin = (uint32_t)(1 << channel_pin[irq_index]);
55+
56+
// Clear interrupt flag
57+
if (EXTI_GetITStatus(pin) != RESET)
58+
{
59+
EXTI_ClearITPendingBit(pin);
60+
}
61+
62+
if (channel_ids[irq_index] == 0) return;
63+
64+
// Check which edge has generated the irq
65+
if ((gpio->IDR & pin) == 0) {
66+
irq_handler(channel_ids[irq_index], IRQ_FALL);
67+
}
68+
else {
69+
irq_handler(channel_ids[irq_index], IRQ_RISE);
70+
}
71+
}
72+
73+
// The irq_index is passed to the function
74+
static void gpio_irq0(void) {handle_interrupt_in(0);}
75+
static void gpio_irq1(void) {handle_interrupt_in(1);}
76+
static void gpio_irq2(void) {handle_interrupt_in(2);}
77+
78+
extern uint32_t Set_GPIO_Clock(uint32_t port_idx);
79+
80+
int gpio_irq_init(gpio_irq_t *obj, PinName pin, gpio_irq_handler handler, uint32_t id) {
81+
IRQn_Type irq_n = (IRQn_Type)0;
82+
uint32_t vector = 0;
83+
uint32_t irq_index;
84+
85+
if (pin == NC) return -1;
86+
87+
uint32_t port_index = STM_PORT(pin);
88+
uint32_t pin_index = STM_PIN(pin);
89+
90+
// Select irq number and interrupt routine
91+
switch (pin) {
92+
case PC_13: // User button
93+
irq_n = EXTI4_15_IRQn;
94+
vector = (uint32_t)&gpio_irq0;
95+
irq_index = 0;
96+
break;
97+
case PA_0:
98+
irq_n = EXTI0_1_IRQn;
99+
vector = (uint32_t)&gpio_irq1;
100+
irq_index = 1;
101+
break;
102+
case PB_3:
103+
irq_n = EXTI2_3_IRQn;
104+
vector = (uint32_t)&gpio_irq2;
105+
irq_index = 2;
106+
break;
107+
default:
108+
error("This pin is not supported\n");
109+
return -1;
110+
}
111+
112+
// Enable GPIO clock
113+
uint32_t gpio_add = Set_GPIO_Clock(port_index);
114+
115+
// Enable SYSCFG clock
116+
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
117+
118+
// Connect EXTI line to pin
119+
SYSCFG_EXTILineConfig(port_index, pin_index);
120+
121+
// Configure EXTI line
122+
EXTI_InitTypeDef EXTI_InitStructure;
123+
EXTI_InitStructure.EXTI_Line = (uint32_t)(1 << pin_index);
124+
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
125+
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
126+
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
127+
EXTI_Init(&EXTI_InitStructure);
128+
129+
// Enable and set EXTI interrupt to the lowest priority
130+
NVIC_InitTypeDef NVIC_InitStructure;
131+
NVIC_InitStructure.NVIC_IRQChannel = irq_n;
132+
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
133+
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
134+
NVIC_Init(&NVIC_InitStructure);
135+
136+
NVIC_SetVector(irq_n, vector);
137+
NVIC_EnableIRQ(irq_n);
138+
139+
// Save informations for future use
140+
obj->irq_n = irq_n;
141+
obj->irq_index = irq_index;
142+
obj->event = EDGE_NONE;
143+
channel_ids[irq_index] = id;
144+
channel_gpio[irq_index] = gpio_add;
145+
channel_pin[irq_index] = pin_index;
146+
147+
irq_handler = handler;
148+
149+
return 0;
150+
}
151+
152+
void gpio_irq_free(gpio_irq_t *obj) {
153+
channel_ids[obj->irq_index] = 0;
154+
channel_gpio[obj->irq_index] = 0;
155+
channel_pin[obj->irq_index] = 0;
156+
// Disable EXTI line
157+
EXTI_InitTypeDef EXTI_InitStructure;
158+
EXTI_StructInit(&EXTI_InitStructure);
159+
EXTI_Init(&EXTI_InitStructure);
160+
obj->event = EDGE_NONE;
161+
}
162+
163+
void gpio_irq_set(gpio_irq_t *obj, gpio_irq_event event, uint32_t enable) {
164+
EXTI_InitTypeDef EXTI_InitStructure;
165+
166+
uint32_t pin_index = channel_pin[obj->irq_index];
167+
168+
EXTI_InitStructure.EXTI_Line = (uint32_t)(1 << pin_index);
169+
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
170+
171+
if (event == IRQ_RISE) {
172+
if ((obj->event == EDGE_FALL) || (obj->event == EDGE_BOTH)) {
173+
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
174+
obj->event = EDGE_BOTH;
175+
}
176+
else { // NONE or RISE
177+
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
178+
obj->event = EDGE_RISE;
179+
}
180+
}
181+
182+
if (event == IRQ_FALL) {
183+
if ((obj->event == EDGE_RISE) || (obj->event == EDGE_BOTH)) {
184+
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
185+
obj->event = EDGE_BOTH;
186+
}
187+
else { // NONE or FALL
188+
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
189+
obj->event = EDGE_FALL;
190+
}
191+
}
192+
193+
if (enable) {
194+
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
195+
}
196+
else {
197+
EXTI_InitStructure.EXTI_LineCmd = DISABLE;
198+
}
199+
200+
EXTI_Init(&EXTI_InitStructure);
201+
}
202+
203+
void gpio_irq_enable(gpio_irq_t *obj) {
204+
NVIC_EnableIRQ(obj->irq_n);
205+
}
206+
207+
void gpio_irq_disable(gpio_irq_t *obj) {
208+
NVIC_DisableIRQ(obj->irq_n);
209+
obj->event = EDGE_NONE;
210+
}

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F030R8/objects.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ extern "C" {
4040
#endif
4141

4242
struct gpio_irq_s {
43-
uint32_t ch;
4443
IRQn_Type irq_n;
45-
uint32_t event; // 0=none, 1=rise, 2=fall, 3=rise+fall
44+
uint32_t irq_index;
45+
uint32_t event;
4646
};
4747

4848
struct port_s {

0 commit comments

Comments
 (0)