Skip to content

Commit 9b1a96d

Browse files
committed
optimize can_init(): call can_init_freq() with default freq
1 parent 6a3e343 commit 9b1a96d

File tree

2 files changed

+296
-31
lines changed

2 files changed

+296
-31
lines changed

main.cpp

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
/* Copyright (c) 2018 Arm Limited
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#include "mbed.h"
18+
#include "static_pinmap.h"
19+
20+
#ifndef RUN_EXP
21+
#define RUN_EXP 0
22+
#endif
23+
24+
#if DEVICE_SPI
25+
static void test_spi()
26+
{
27+
#if !RUN_EXP
28+
/* Regular use (master) */
29+
SPI spi(D1, D2, D3, D4);
30+
#else
31+
/* Explicit pinmap */
32+
//constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(PTD2, PTD3, PTD1, PTD0); // K64F
33+
//constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(PA_7, PA_6, PA_5, PA_4); // NUCLEO_F429ZI
34+
//constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(PA_7, PA_6, PA_5, PA_4); // NUCLEO_F411RE
35+
//constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(P0_3, P0_2, P0_6, P0_4); // LPC
36+
//constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(PA_7, PA_6, PA_5, PA_4); // DISCO
37+
//constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(PA_7, PA_6, PA_5, PA_4); // NUCLEO_F303RE
38+
constexpr spi_pinmap_t explicit_spi_pinmap = get_spi_pinmap(PA_7, PA_6, PA_5, PA_4); // NUCLEO_L073RZ
39+
40+
SPI spi(explicit_spi_pinmap);
41+
//SPI spi(get_spi_pinmap(PA_7, PA_6, PA_5, PA_4));
42+
#endif
43+
spi.format(8,0);
44+
}
45+
#endif
46+
47+
#if DEVICE_PWMOUT
48+
static void test_pwm()
49+
{
50+
#if !RUN_EXP
51+
PwmOut led(LED1);
52+
#else
53+
//constexpr PinMap explicit_pinmap = get_pwm_pinmap(PTA1); // K64F
54+
//constexpr PinMap explicit_pinmap = get_pwm_pinmap(PA_0); // NUCLEO_F429ZI
55+
//constexpr PinMap explicit_pinmap = get_pwm_pinmap(PA_0); // NUCLEO_F411RE
56+
//constexpr PinMap explicit_pinmap = get_pwm_pinmap(PA_0); // NUCLEO_F303RE
57+
constexpr PinMap explicit_pinmap = get_pwm_pinmap(PA_0); // NUCLEO_L073RZ
58+
59+
PwmOut led(explicit_pinmap);
60+
//PwmOut led(get_pwm_pinmap(PA_0));
61+
#endif
62+
led.period(4.0f); // 4 second period
63+
led.write(0.50f); // 50% duty cycle, relative to period
64+
}
65+
#endif
66+
67+
#if DEVICE_ANALOGIN
68+
static void test_analogin()
69+
{
70+
#if !RUN_EXP
71+
AnalogIn ain(ADC_VREF);
72+
#else
73+
//constexpr PinMap explicit_pinmap = get_analogin_pinmap(PTB2); // K64F
74+
//constexpr PinMap explicit_pinmap = get_analogin_pinmap(PA_0); // NUCLEO_F429ZI
75+
//constexpr PinMap explicit_pinmap = get_analogin_pinmap(PA_0); // NUCLEO_F411RE
76+
//constexpr PinMap explicit_pinmap = get_analogin_pinmap(p2); // NORDIC
77+
//constexpr PinMap explicit_pinmap = get_analogin_pinmap(PA_0); // DISCO
78+
//constexpr PinMap explicit_pinmap = get_analogin_pinmap(P0_23); // LPC
79+
constexpr PinMap explicit_pinmap = get_analogin_pinmap(PA_0); // NUCLEO_L073RZ
80+
81+
AnalogIn ain(explicit_pinmap);
82+
//AnalogIn ain(get_analogin_pinmap(PA_0));
83+
#endif
84+
if(ain > 0.3f) {
85+
while(1);
86+
}
87+
}
88+
#endif
89+
90+
#if DEVICE_ANALOGOUT
91+
static void test_analogout()
92+
{
93+
#if !RUN_EXP
94+
AnalogOut aout(D1);
95+
#else
96+
//constexpr PinMap explicit_pinmap = get_analogout_pinmap(DAC0_OUT); // K64F
97+
//constexpr PinMap explicit_pinmap = get_analogout_pinmap(PA_4); // NUCLEO_F429ZI
98+
//constexpr PinMap explicit_pinmap = get_analogout_pinmap(PA_4); // DISCO
99+
//constexpr PinMap explicit_pinmap = get_analogout_pinmap(PA_4); // NUCLEO_F303RE
100+
constexpr PinMap explicit_pinmap = get_analogout_pinmap(PA_4); // NUCLEO_L073RZ
101+
102+
AnalogOut aout(explicit_pinmap);
103+
//AnalogOut aout(get_analogout_pinmap(PA_4));
104+
#endif
105+
aout = 0.1;
106+
}
107+
#endif
108+
109+
#if DEVICE_I2C
110+
static void test_i2c()
111+
{
112+
#if !RUN_EXP
113+
I2C i2c(D1, D2);
114+
#else
115+
//constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(PTB1, PTB0); // K64F
116+
//constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(PB_7, PA_8); // NUCLEO_F429ZI
117+
//constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(PB_3, PB_10); // NUCLEO_F411RE
118+
//constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(PB_9, PB_8); // DISCO
119+
//constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(PA_10, PA_9); // NUCLEO_F303RE
120+
//constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(P0_13, P0_14); // LPC
121+
constexpr i2c_pinmap_t explicit_pinmap = get_i2c_pinmap(PA_10, PA_9); // NUCLEO_L073RZ
122+
123+
I2C i2c(explicit_pinmap);
124+
//I2C i2c(get_i2c_pinmap(PA_10, PA_9));
125+
#endif
126+
i2c.frequency(1000000);
127+
}
128+
#endif
129+
130+
#if DEVICE_SERIAL
131+
static void test_serial()
132+
{
133+
#if !RUN_EXP
134+
Serial serial(D1, D2);
135+
serial.set_flow_control(Serial::RTSCTS, D1, D2);
136+
#else
137+
//constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(PA_0, PA_1); // NUCLEO_F429ZI
138+
//constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(P0_30, P0_29); // NORDIC
139+
//constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(PA_0, PA_1); // DISCO
140+
//constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(PA_2, PA_3); // NUCLEO_F411RE
141+
//constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(PA_2, PA_3); // NUCLEO_F303RE
142+
//constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(P0_30, P0_29); // LPC
143+
constexpr serial_pinmap_t explicit_pinmap = get_uart_pinmap(PA_0, PA_1); // NUCLEO_L073RZ
144+
145+
146+
//constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(PA_0, PA_1); // NUCLEO_F429ZI
147+
//constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(P1_8, P1_7); // NORDIC
148+
//constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(PA_0, PA_1); // DISCO
149+
//constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(PA_0, PA_1); // NUCLEO_F411RE
150+
//constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(PA_0, PA_1); // NUCLEO_F303RE
151+
//constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(P1_7, P1_8); // LPC
152+
constexpr serial_fc_pinmap_t explicit_pinmap_fc = get_uart_fc_pinmap(PA_0, PA_1); // NUCLEO_L073RZ
153+
154+
Serial serial(explicit_pinmap);
155+
//Serial serial(get_uart_fc_pinmap(PA_0, PA_1));
156+
157+
serial.set_flow_control(Serial::RTSCTS, explicit_pinmap_fc);
158+
#endif
159+
if (serial.readable()) {
160+
while(1);
161+
}
162+
}
163+
#endif
164+
165+
#if DEVICE_QSPI
166+
static void test_qspi()
167+
{
168+
#if !RUN_EXP
169+
QSPI qspi_device(PB_1, PB_0, PA_7, PA_6, PB_10, PB_11);
170+
#else
171+
constexpr qspi_pinmap_t explicit_pinmap = get_qspi_pinmap(PB_1, PB_0, PA_7, PA_6, PB_10, PB_11); // DISCO
172+
QSPI qspi_device(explicit_pinmap);
173+
//QSPI qspi_device(get_qspi_pinmap(PB_1, PB_0, PA_7, PA_6, PB_10, PB_11));
174+
qspi_device.configure_format(QSPI_CFG_BUS_SINGLE, QSPI_CFG_BUS_SINGLE,
175+
QSPI_CFG_ADDR_SIZE_24, QSPI_CFG_BUS_SINGLE,
176+
QSPI_CFG_ALT_SIZE_8, QSPI_CFG_BUS_SINGLE, 0);
177+
#endif
178+
179+
}
180+
#endif
181+
182+
#if DEVICE_CAN
183+
static void test_can()
184+
{
185+
char counter;
186+
187+
#if !RUN_EXP
188+
CAN can(PA_11, PA_12, 500000);
189+
#else
190+
191+
//constexpr can_pinmap_t explicit_pinmap = get_can_pinmap(PA_11, PA_12); // DISCO
192+
constexpr can_pinmap_t explicit_pinmap = get_can_pinmap(PA_11, PA_12); // NUCLEO_F429ZI
193+
CAN can(explicit_pinmap);
194+
//CAN can(get_can_pinmap(PA_11, PA_12), 10000);
195+
#endif
196+
can.write(CANMessage(1337, &counter, 1));
197+
}
198+
#endif
199+
200+
static void test_sizes()
201+
{
202+
uint32_t PINMAP_ANALOGIN_SIZE = 0;
203+
uint32_t PINMAP_ANALOGOUT_SIZE = 0;
204+
uint32_t PINMAP_I2C_SIZE = 0;
205+
uint32_t PINMAP_UART_SIZE = 0;
206+
uint32_t PINMAP_SPI_SIZE = 0;
207+
uint32_t PINMAP_PWM_SIZE = 0;
208+
uint32_t PINMAP_QSPI_SIZE = 0;
209+
uint32_t PINMAP_CAN_SIZE = 0;
210+
211+
212+
#ifdef PINMAP_ANALOGIN
213+
PINMAP_ANALOGIN_SIZE = sizeof(PINMAP_ANALOGIN);
214+
#endif
215+
216+
#ifdef PINMAP_ANALOGIN_INTERNAL
217+
PINMAP_ANALOGIN_SIZE += sizeof(PINMAP_ANALOGIN_INTERNAL);
218+
#endif
219+
220+
#ifdef PINMAP_ANALOGOUT
221+
PINMAP_ANALOGOUT_SIZE = sizeof(PINMAP_ANALOGOUT);
222+
#endif
223+
224+
#ifdef PINMAP_I2C_SDA
225+
PINMAP_I2C_SIZE = sizeof(PINMAP_I2C_SDA) + sizeof(PINMAP_I2C_SCL);
226+
#endif
227+
228+
#ifdef PINMAP_UART_TX
229+
PINMAP_UART_SIZE = sizeof(PINMAP_UART_TX) + sizeof(PINMAP_UART_RX);
230+
#endif
231+
232+
#ifdef PINMAP_UART_CTS
233+
PINMAP_UART_SIZE += sizeof(PinMap_UART_CTS) + sizeof(PinMap_UART_RTS);
234+
#endif
235+
236+
#ifdef PINMAP_SPI_SCLK
237+
PINMAP_SPI_SIZE = sizeof(PINMAP_SPI_SCLK) + sizeof(PINMAP_SPI_MOSI) + sizeof(PINMAP_SPI_MISO) + sizeof(PINMAP_SPI_SSEL);
238+
#endif
239+
240+
#ifdef PINMAP_PWM
241+
PINMAP_PWM_SIZE = sizeof(PINMAP_PWM);
242+
#endif
243+
244+
#ifdef PINMAP_QSPI_DATA0
245+
PINMAP_QSPI_SIZE = sizeof(PINMAP_QSPI_DATA0) + sizeof(PINMAP_QSPI_DATA1) + sizeof(PINMAP_QSPI_DATA2) + sizeof(PINMAP_QSPI_DATA3) + sizeof(PinMap_QSPI_SCLK) + sizeof(PinMap_QSPI_SSEL);
246+
#endif
247+
248+
#ifdef PINMAP_CAN_RD
249+
PINMAP_CAN_SIZE = sizeof(PINMAP_CAN_RD) + sizeof(PINMAP_CAN_TD);
250+
#endif
251+
252+
printf("PINMAP_ANALOGIN_SIZE: %u \r\n", PINMAP_ANALOGIN_SIZE);
253+
printf("PINMAP_ANALOGOUT_SIZE: %u \r\n", PINMAP_ANALOGOUT_SIZE);
254+
printf("PINMAP_I2C_SIZE: %u \r\n", PINMAP_I2C_SIZE);
255+
printf("PINMAP_UART_SIZE: %u \r\n", PINMAP_UART_SIZE);
256+
printf("PINMAP_SPI_SIZE: %u \r\n", PINMAP_SPI_SIZE);
257+
printf("PINMAP_PWM_SIZE: %u \r\n", PINMAP_PWM_SIZE);
258+
printf("PINMAP_QSPI_SIZE: %u \r\n", PINMAP_QSPI_SIZE);
259+
printf("PINMAP_CAN_SIZE: %u \r\n", PINMAP_CAN_SIZE);
260+
261+
}
262+
263+
264+
265+
int main()
266+
{
267+
//test_pwm();
268+
269+
//test_analogin();
270+
271+
//test_analogout();
272+
273+
//test_spi();
274+
275+
//test_i2c();
276+
277+
//test_serial();
278+
279+
//test_qspi();
280+
281+
test_can();
282+
283+
//test_sizes();
284+
285+
while(1);
286+
287+
return 0;
288+
}

targets/TARGET_STM/can_api.c

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -167,23 +167,6 @@ static void _can_init_direct(can_t *obj, const can_pinmap_t *pinmap)
167167
CAN_INIT_FREQ_DIRECT(obj, pinmap, 100000);
168168
}
169169

170-
void can_init(can_t *obj, PinName rd, PinName td)
171-
{
172-
CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
173-
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
174-
int peripheral = (int) pinmap_merge(can_rd, can_td);
175-
176-
int function_rd = (int)pinmap_find_function(rd, PinMap_CAN_RD);
177-
int function_td = (int)pinmap_find_function(td, PinMap_CAN_TD);
178-
179-
const can_pinmap_t static_pinmap = {peripheral, rd, function_rd, td, function_td};
180-
181-
/* default frequency is 100 kHz */
182-
CAN_INIT_DIRECT(obj, &static_pinmap);
183-
}
184-
185-
186-
187170
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
188171
{
189172
CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
@@ -198,6 +181,10 @@ void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
198181
CAN_INIT_FREQ_DIRECT(obj, &static_pinmap, hz);
199182
}
200183

184+
void can_init(can_t *obj, PinName rd, PinName td)
185+
{
186+
can_init_freq(obj, rd, td, 100000);
187+
}
201188

202189
void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)
203190
{
@@ -666,7 +653,7 @@ static void _can_init_direct(can_t *obj, const can_pinmap_t *pinmap)
666653
CAN_INIT_FREQ_DIRECT(obj, pinmap, 100000);
667654
}
668655

669-
void can_init(can_t *obj, PinName rd, PinName td)
656+
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
670657
{
671658
CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
672659
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
@@ -677,22 +664,12 @@ void can_init(can_t *obj, PinName rd, PinName td)
677664

678665
const can_pinmap_t static_pinmap = {peripheral, rd, function_rd, td, function_td};
679666

680-
/* default frequency is 100 kHz */
681-
CAN_INIT_DIRECT(obj, &static_pinmap);
667+
CAN_INIT_FREQ_DIRECT(obj, &static_pinmap, hz);
682668
}
683669

684-
void can_init_freq(can_t *obj, PinName rd, PinName td, int hz)
670+
void can_init(can_t *obj, PinName rd, PinName td)
685671
{
686-
CANName can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
687-
CANName can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
688-
int peripheral = (int) pinmap_merge(can_rd, can_td);
689-
690-
int function_rd = (int)pinmap_find_function(rd, PinMap_CAN_RD);
691-
int function_td = (int)pinmap_find_function(td, PinMap_CAN_TD);
692-
693-
const can_pinmap_t static_pinmap = {peripheral, rd, function_rd, td, function_td};
694-
695-
CAN_INIT_FREQ_DIRECT(obj, &static_pinmap, hz);
672+
can_init_freq(obj, rd, td, 100000);
696673
}
697674

698675
void can_irq_init(can_t *obj, can_irq_handler handler, uint32_t id)

0 commit comments

Comments
 (0)