Skip to content

Commit 2310b4c

Browse files
committed
[NUCLEO_F103RB] Code cleanup
1 parent a129b8f commit 2310b4c

File tree

5 files changed

+37
-117
lines changed

5 files changed

+37
-117
lines changed

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/PeripheralNames.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ typedef enum {
3232
UART_2 = (int)USART2_BASE
3333
} UARTName;
3434

35-
#define STDIO_UART_TX UART_TX
36-
#define STDIO_UART_RX UART_RX
35+
#define STDIO_UART_TX PA_2
36+
#define STDIO_UART_RX PA_3
3737
#define STDIO_UART UART_2
3838

3939
typedef enum {

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/PinNames.h

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -88,45 +88,10 @@ typedef enum {
8888
PC_14 = 0x2E,
8989
PC_15 = 0x2F,
9090

91-
PD_0 = 0x30,
92-
PD_1 = 0x31,
9391
PD_2 = 0x32,
94-
PD_3 = 0x33,
95-
PD_4 = 0x34,
96-
PD_5 = 0x35,
97-
PD_6 = 0x36,
98-
PD_7 = 0x37,
9992
PD_8 = 0x38,
100-
PD_9 = 0x39,
101-
PD_10 = 0x3A,
102-
PD_11 = 0x3B,
103-
PD_12 = 0x3C,
104-
PD_13 = 0x3D,
105-
PD_14 = 0x3E,
106-
PD_15 = 0x3F,
10793

108-
PE_0 = 0x40,
109-
PE_1 = 0x41,
110-
PE_2 = 0x42,
111-
PE_3 = 0x43,
112-
PE_4 = 0x44,
113-
PE_5 = 0x45,
114-
PE_6 = 0x46,
115-
PE_7 = 0x47,
116-
PE_8 = 0x48,
117-
PE_9 = 0x49,
118-
PE_10 = 0x4A,
119-
PE_11 = 0x4B,
120-
PE_12 = 0x4C,
121-
PE_13 = 0x4D,
122-
PE_14 = 0x4E,
123-
PE_15 = 0x4F,
124-
125-
//--------------------
126-
// NUCLEO_F103RB board
127-
//--------------------
128-
129-
// Arduino connectors
94+
// Arduino connector namings
13095
A0 = PA_0,
13196
A1 = PA_1,
13297
A2 = PA_4,
@@ -150,7 +115,11 @@ typedef enum {
150115
D14 = PB_9,
151116
D15 = PB_8,
152117

153-
// Generic signals
118+
// Generic signals namings
119+
LED1 = PA_5,
120+
LED2 = PA_5,
121+
LED3 = PA_5,
122+
LED4 = PA_5,
154123
USER_BUTTON = PC_13,
155124
SERIAL_TX = PA_2,
156125
SERIAL_RX = PA_3,
@@ -159,14 +128,9 @@ typedef enum {
159128
SPI_MOSI = PA_7,
160129
SPI_MISO = PA_6,
161130
SPI_SCK = PA_5,
131+
SPI_CS = PB_6,
162132
PWM_OUT = PB_3,
163133

164-
// mbed mandatory namings
165-
LED1 = PA_5,
166-
LED2 = PA_5,
167-
LED3 = PA_5,
168-
LED4 = PA_5,
169-
170134
// Not connected
171135
NC = (int)0xFFFFFFFF
172136
} PinName;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
//==============================================================================
17-
// STM32F103
18-
//==============================================================================
1916
#include "analogin_api.h"
2017
#include "wait_api.h"
2118

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/serial_api.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,12 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
//==============================================================================
17-
// STM32F103
18-
//==============================================================================
1916
#include "serial_api.h"
2017
#include "cmsis.h"
2118
#include "pinmap.h"
2219
#include "error.h"
2320
#include <string.h>
2421

25-
/******************************************************************************
26-
* INITIALIZATION
27-
******************************************************************************/
28-
2922
static const PinMap PinMap_UART_TX[] = {
3023
{PA_9, UART_1, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},
3124
{PA_2, UART_2, STM_PIN_DATA(GPIO_Mode_AF_PP, 0)},

libraries/mbed/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/spi_api.c

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
//==============================================================================
17-
// STM32F103
18-
//==============================================================================
1916
#include "spi_api.h"
2017

2118
#if DEVICE_SPI
@@ -45,16 +42,33 @@ static const PinMap PinMap_SPI_SCLK[] = {
4542

4643
// Only used in Slave mode
4744
static const PinMap PinMap_SPI_SSEL[] = {
48-
{PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
49-
{PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 1)}, // Remap
45+
{PB_6, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)}, // Generic IO, not real H/W NSS pin
46+
//{PA_4, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 0)},
47+
//{PA_15, SPI_1, STM_PIN_DATA(GPIO_Mode_IN_FLOATING, 1)}, // Remap
5048
{NC, NC, 0}
5149
};
5250

53-
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
54-
55-
SPI_TypeDef *spi;
51+
static void init_spi(spi_t *obj) {
52+
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
5653
SPI_InitTypeDef SPI_InitStructure;
57-
54+
55+
SPI_Cmd(spi, DISABLE);
56+
57+
SPI_InitStructure.SPI_Mode = obj->mode;
58+
SPI_InitStructure.SPI_NSS = obj->nss;
59+
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
60+
SPI_InitStructure.SPI_DataSize = obj->bits;
61+
SPI_InitStructure.SPI_CPOL = obj->cpol;
62+
SPI_InitStructure.SPI_CPHA = obj->cpha;
63+
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
64+
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
65+
SPI_InitStructure.SPI_CRCPolynomial = 7;
66+
SPI_Init(spi, &SPI_InitStructure);
67+
68+
SPI_Cmd(spi, ENABLE);
69+
}
70+
71+
void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel) {
5872
// Determine the SPI to use
5973
SPIName spi_mosi = (SPIName)pinmap_peripheral(mosi, PinMap_SPI_MOSI);
6074
SPIName spi_miso = (SPIName)pinmap_peripheral(miso, PinMap_SPI_MISO);
@@ -69,9 +83,6 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
6983
if (obj->spi == (SPIName)NC) {
7084
error("SPI pinout mapping failed");
7185
}
72-
73-
// Get SPI registers structure address
74-
spi = (SPI_TypeDef *)(obj->spi);
7586

7687
// Enable SPI clock
7788
if (obj->spi == SPI_1) {
@@ -99,35 +110,19 @@ void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel
99110
else { // Slave
100111
pinmap_pinout(ssel, PinMap_SPI_SSEL);
101112
obj->mode = SPI_Mode_Slave;
102-
obj->nss = SPI_NSS_Hard;
113+
obj->nss = SPI_NSS_Soft;
103114
}
104115

105-
// SPI configuration
106-
SPI_InitStructure.SPI_Mode = obj->mode;
107-
SPI_InitStructure.SPI_NSS = obj->nss;
108-
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
109-
SPI_InitStructure.SPI_DataSize = obj->bits;
110-
SPI_InitStructure.SPI_CPOL = obj->cpol;
111-
SPI_InitStructure.SPI_CPHA = obj->cpha;
112-
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
113-
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
114-
SPI_InitStructure.SPI_CRCPolynomial = 7;
115-
SPI_Init(spi, &SPI_InitStructure);
116-
117-
SPI_Cmd(spi, ENABLE);
116+
init_spi(obj);
118117
}
119118

120119
void spi_free(spi_t *obj) {
121120
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
122121
SPI_I2S_DeInit(spi);
123122
}
124123

125-
void spi_format(spi_t *obj, int bits, int mode, int slave) {
126-
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
127-
SPI_InitTypeDef SPI_InitStructure;
128-
124+
void spi_format(spi_t *obj, int bits, int mode, int slave) {
129125
// Save new values
130-
131126
if (bits == 8) {
132127
obj->bits = SPI_DataSize_8b;
133128
}
@@ -163,26 +158,10 @@ void spi_format(spi_t *obj, int bits, int mode, int slave) {
163158
obj->nss = SPI_NSS_Hard;
164159
}
165160

166-
SPI_Cmd(spi, DISABLE);
167-
168-
SPI_InitStructure.SPI_Mode = obj->mode;
169-
SPI_InitStructure.SPI_NSS = obj->nss;
170-
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
171-
SPI_InitStructure.SPI_DataSize = obj->bits;
172-
SPI_InitStructure.SPI_CPOL = obj->cpol;
173-
SPI_InitStructure.SPI_CPHA = obj->cpha;
174-
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
175-
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
176-
SPI_InitStructure.SPI_CRCPolynomial = 7;
177-
SPI_Init(spi, &SPI_InitStructure);
178-
179-
SPI_Cmd(spi, ENABLE);
161+
init_spi(obj);
180162
}
181163

182164
void spi_frequency(spi_t *obj, int hz) {
183-
SPI_TypeDef *spi = (SPI_TypeDef *)(obj->spi);
184-
SPI_InitTypeDef SPI_InitStructure;
185-
186165
// Get SPI clock frequency
187166
uint32_t PCLK = SystemCoreClock >> 1;
188167

@@ -203,20 +182,7 @@ void spi_frequency(spi_t *obj, int hz) {
203182
// Save new value
204183
obj->br_presc = ((baud_rate > 7) ? (7 << 3) : (baud_rate << 3));
205184

206-
SPI_Cmd(spi, DISABLE);
207-
208-
SPI_InitStructure.SPI_Mode = obj->mode;
209-
SPI_InitStructure.SPI_NSS = obj->nss;
210-
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
211-
SPI_InitStructure.SPI_DataSize = obj->bits;
212-
SPI_InitStructure.SPI_CPOL = obj->cpol;
213-
SPI_InitStructure.SPI_CPHA = obj->cpha;
214-
SPI_InitStructure.SPI_BaudRatePrescaler = obj->br_presc;
215-
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
216-
SPI_InitStructure.SPI_CRCPolynomial = 7;
217-
SPI_Init(spi, &SPI_InitStructure);
218-
219-
SPI_Cmd(spi, ENABLE);
185+
init_spi(obj);
220186
}
221187

222188
static inline int ssp_readable(spi_t *obj) {

0 commit comments

Comments
 (0)