boards/stm32f103-minimum: Add CAN bring-up, char/socket drivers, and defconfig
Build Documentation / build-html (push) Has been cancelled

Add board support for on-chip bxCAN: stm32_can.c registers /dev/can0 via stm32_caninitialize() when CONFIG_STM32_CAN_CHARDRIVER is set; stm32_cansock.c calls stm32_cansockinitialize() for the socket-CAN path when CONFIG_STM32_CAN_SOCKET is set.

Signed-off-by: Alexey Matveev <tippet@yandex.ru>
This commit is contained in:
Alexey Matveev
2026-04-19 09:01:57 +03:00
committed by Xiang Xiao
parent 87bebead38
commit e9de74d3a9
8 changed files with 317 additions and 0 deletions
@@ -599,6 +599,44 @@ firmware in the second board. Now you can start the both boards, run the
"can" command in the Write-only board and then run the "can" command in the
Read-only board. You should see the data coming.
CAN support
===========
The STM32F103C8 has a on-chip bxCAN controller, then we can use it for
CAN bus communication.
It has limitations:
- USB is not available together with on-chip CAN, because they share the SRAM
buffer (see the MCP2515 External Module section);
- you need to use transceivers (SN65HVD230, ...) to connect the CAN bus;
- you need a second device on the CAN bus to test the communication.
If you want only to check the CAN bus, you can use mode 'loopback' in the CAN device.
CAN PINS
--------
..
CAN1 (default pin mapping; ``CONFIG_STM32_CAN1_REMAP1`` is not set)
RX PA11
TX PA12
CAN1 (alternate mapping; enable ``CONFIG_STM32_CAN1_REMAP1``)
RX PB8
TX PB9
On STM32F103Cx in LQFP48, CAN1_REMAP2 is not available.
Transceivers
------------
The MCU CAN peripheral only provides single-ended TTL-level ``CAN_RX`` / ``CAN_TX``
lines. A CAN transceiver interfaces those to the differential **CANH** / **CANL**
bus (dominant/recessive signaling), drives the correct line impedance, and
typically adds short-circuit and ESD protection so the chip can safely connect
to a real vehicle or bench network. Common parts include SN65HVD230, MCP2551,
and TJA1050.
STM32F103 Minimum - specific Configuration Options
==================================================
@@ -885,3 +923,14 @@ veml6070
This is a config example to use the Vishay VEML6070 UV-A sensor. To use this
sensor you need to connect PB6 (I2C1 CLK) to SCL; PB7 (I2C1 SDA) to SDA of
sensor module. I used a GY-VEML6070 module to test this driver.
can
---
This is a config example to use on-chip CAN1 and the CAN example application.
Wire PA11 (CAN_RX) and PA12 (CAN_TX) to a CAN transceiver;
connect the transceiver's CANH and CANL to the bus.
See the 'CAN support' section above for pins, transceivers, and limitations.
A UART adapter on USART1 (PA9/PA10) is required for the NSH console on a
host PC.
@@ -0,0 +1,74 @@
#
# This file is autogenerated: PLEASE DO NOT EDIT IT.
#
# You can use "make menuconfig" to make any modifications to the installed .config file.
# You can then do "make savedefconfig" to generate a new defconfig file that includes your
# modifications.
#
# CONFIG_DISABLE_OS_API is not set
# CONFIG_NSH_DISABLESCRIPT is not set
# CONFIG_NSH_DISABLE_CAT is not set
# CONFIG_NSH_DISABLE_CD is not set
# CONFIG_NSH_DISABLE_CP is not set
# CONFIG_NSH_DISABLE_ECHO is not set
# CONFIG_NSH_DISABLE_EXEC is not set
# CONFIG_NSH_DISABLE_EXIT is not set
# CONFIG_NSH_DISABLE_FREE is not set
# CONFIG_NSH_DISABLE_GET is not set
# CONFIG_NSH_DISABLE_HEXDUMP is not set
# CONFIG_NSH_DISABLE_LS is not set
# CONFIG_NSH_DISABLE_MKDIR is not set
# CONFIG_NSH_DISABLE_MOUNT is not set
# CONFIG_NSH_DISABLE_PS is not set
# CONFIG_NSH_DISABLE_PUT is not set
# CONFIG_NSH_DISABLE_PWD is not set
# CONFIG_NSH_DISABLE_RM is not set
# CONFIG_NSH_DISABLE_SLEEP is not set
# CONFIG_NSH_DISABLE_TEST is not set
# CONFIG_NSH_DISABLE_UMOUNT is not set
# CONFIG_NSH_DISABLE_UNAME is not set
# CONFIG_NSH_DISABLE_WGET is not set
# CONFIG_NSH_DISABLE_XD is not set
CONFIG_ARCH="arm"
CONFIG_ARCH_BOARD="stm32f103-minimum"
CONFIG_ARCH_BOARD_COMMON=y
CONFIG_ARCH_BOARD_STM32F103_MINIMUM=y
CONFIG_ARCH_CHIP="stm32"
CONFIG_ARCH_CHIP_STM32=y
CONFIG_ARCH_CHIP_STM32F103C8=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARD_LOOPSPERMSEC=5483
CONFIG_BUILTIN=y
CONFIG_CAN_ERRORS=y
CONFIG_CAN_EXTID=y
CONFIG_DEFAULT_SMALL=y
CONFIG_EXAMPLES_CAN=y
CONFIG_EXAMPLES_CAN_NMSGS=4
CONFIG_EXAMPLES_CAN_WRITE=y
CONFIG_HOST_MACOS=y
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_LINE_MAX=80
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=1024
CONFIG_RAM_SIZE=20480
CONFIG_RAM_START=0x20000000
CONFIG_RAW_BINARY=y
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HPWORK=y
CONFIG_SCHED_HPWORKPRIORITY=192
CONFIG_SCHED_WAITPID=y
CONFIG_SERIAL_TERMIOS=y
CONFIG_START_DAY=5
CONFIG_START_MONTH=7
CONFIG_START_YEAR=2011
CONFIG_STM32_CAN1=y
CONFIG_STM32_CAN_TSEG1=13
CONFIG_STM32_CAN_TSEG2=2
CONFIG_STM32_DISABLE_IDLE_SLEEP_DURING_DEBUG=y
CONFIG_STM32_JTAG_FULL_ENABLE=y
CONFIG_STM32_USART1=y
CONFIG_SYMTAB_ORDEREDBYNAME=y
CONFIG_SYSTEM_NSH=y
CONFIG_TASK_NAME_SIZE=0
CONFIG_USART1_SERIAL_CONSOLE=y
@@ -109,6 +109,15 @@ if(CONFIG_USBMSC)
list(APPEND SRCS stm32_usbmsc.c)
endif()
if(CONFIG_STM32_CAN)
if(CONFIG_STM32_CAN_CHARDRIVER)
list(APPEND SRCS stm32_can.c)
endif()
if(CONFIG_STM32_CAN_SOCKET)
list(APPEND SRCS stm32_cansock.c)
endif()
endif()
target_sources(board PRIVATE ${SRCS})
set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/ld.script")
@@ -111,6 +111,15 @@ ifeq ($(CONFIG_USBMSC),y)
CSRCS += stm32_usbmsc.c
endif
ifeq ($(CONFIG_STM32_CAN),y)
ifeq ($(CONFIG_STM32_CAN_CHARDRIVER),y)
CSRCS += stm32_can.c
endif
ifeq ($(CONFIG_STM32_CAN_SOCKET),y)
CSRCS += stm32_cansock.c
endif
endif
DEPPATH += --dep-path board
VPATH += :board
CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board
@@ -578,5 +578,29 @@ int stm32_bringup(void)
usbdev_adb_initialize();
#endif
#ifdef CONFIG_STM32_CAN_CHARDRIVER
/* Initialize CAN and register the CAN driver. */
ret = stm32_can_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_can_setup failed: %d\n", ret);
}
#endif
#ifdef CONFIG_STM32_CAN_SOCKET
/* Initialize CAN socket interface */
/* STM32F103C8 may not have enough Flash for SocketCAN; use a part with
* more Flash (e.g. STM32F103CB).
*/
ret = stm32_cansock_setup();
if (ret < 0)
{
syslog(LOG_ERR, "ERROR: stm32_cansock_setup failed: %d\n", ret);
}
#endif
return ret;
}
@@ -0,0 +1,69 @@
/****************************************************************************
* boards/arm/stm32/stm32f103-minimum/src/stm32_can.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <errno.h>
#include <nuttx/debug.h>
#include <nuttx/can/can.h>
#include "stm32.h"
#include "stm32_can.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_can_setup
*
* Description:
* Initialize CAN and register the CAN device
*
****************************************************************************/
int stm32_can_setup(void)
{
struct can_dev_s *can;
int ret;
can = stm32_caninitialize(1);
if (can == NULL)
{
canerr("ERROR: Failed to get CAN interface\n");
return -ENODEV;
}
ret = can_register("/dev/can0", can);
if (ret < 0)
{
canerr("ERROR: can_register failed: %d\n", ret);
return ret;
}
return OK;
}
@@ -0,0 +1,59 @@
/****************************************************************************
* boards/arm/stm32/stm32f103-minimum/src/stm32_cansock.c
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/debug.h>
#include "stm32_can.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: stm32_cansock_setup
*
* Description:
* Initialize CAN socket interface
*
****************************************************************************/
int stm32_cansock_setup(void)
{
int ret;
/* Call stm32_cansockinitialize() to register the CAN network device */
ret = stm32_cansockinitialize(1);
if (ret < 0)
{
canerr("ERROR: Failed to get CAN interface %d\n", ret);
return ret;
}
return OK;
}
@@ -374,5 +374,29 @@ int stm32_hyt271initialize(int devno);
int stm32_ds18b20initialize(int devno);
#endif
/****************************************************************************
* Name: stm32_can_setup
*
* Description:
* Initialize CAN and register the CAN device
*
****************************************************************************/
#ifdef CONFIG_STM32_CAN_CHARDRIVER
int stm32_can_setup(void);
#endif
/****************************************************************************
* Name: stm32_cansock_setup
*
* Description:
* Initialize CAN socket interface
*
****************************************************************************/
#ifdef CONFIG_STM32_CAN_SOCKET
int stm32_cansock_setup(void);
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_ARM_STM32_STM32F103_MINIMUM_SRC_STM32F103_MINIMUM_H */