From 9ff5eb279d0184f03b71e130d0f056063b1adb23 Mon Sep 17 00:00:00 2001 From: patacongo Date: Thu, 22 Dec 2011 15:59:50 +0000 Subject: [PATCH] Add loopback support to STM32 CAN driver; Add apps/examples/can loopback test git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4213 42af7a65-404d-4744-a932-0658087f49c3 --- configs/README.txt | 3 + configs/hymini-stm32v/README.txt | 2 + configs/stm3210e-eval/README.txt | 2 + configs/stm3210e-eval/src/up_adc.c | 7 +- configs/stm3240g-eval/README.txt | 2 + configs/stm3240g-eval/dhcpd/defconfig | 7 +- configs/stm3240g-eval/nettest/defconfig | 7 +- configs/stm3240g-eval/nsh/appconfig | 3 + configs/stm3240g-eval/nsh/defconfig | 7 +- configs/stm3240g-eval/ostest/defconfig | 7 +- configs/stm3240g-eval/src/Makefile | 4 + configs/stm3240g-eval/src/stm3240g-internal.h | 32 +++- configs/stm3240g-eval/src/up_adc.c | 30 +++- configs/stm3240g-eval/src/up_can.c | 142 ++++++++++++++++++ 14 files changed, 238 insertions(+), 17 deletions(-) create mode 100644 configs/stm3240g-eval/src/up_can.c diff --git a/configs/README.txt b/configs/README.txt index 90302fff64..6bec1ca1c3 100644 --- a/configs/README.txt +++ b/configs/README.txt @@ -641,6 +641,9 @@ defconfig -- This is a configuration file similar to the Linux Default: 8 CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. Default: 4 + CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback + mode for testing. If the driver does support loopback mode, the setting + will enable it. (If the driver does not, this setting will have no effect). SPI driver diff --git a/configs/hymini-stm32v/README.txt b/configs/hymini-stm32v/README.txt index ccb3850e2e..8dce52d26a 100755 --- a/configs/hymini-stm32v/README.txt +++ b/configs/hymini-stm32v/README.txt @@ -495,6 +495,8 @@ HY-Mini specific Configuration Options Default: 8 CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. Default: 4 + CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback + mode for testing. The STM32 CAN driver does support loopback mode. CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. diff --git a/configs/stm3210e-eval/README.txt b/configs/stm3210e-eval/README.txt index e5f2541d8e..05face7f07 100755 --- a/configs/stm3210e-eval/README.txt +++ b/configs/stm3210e-eval/README.txt @@ -583,6 +583,8 @@ STM3210E-EVAL-specific Configuration Options Default: 8 CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. Default: 4 + CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback + mode for testing. The STM32 CAN driver does support loopback mode. CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. diff --git a/configs/stm3210e-eval/src/up_adc.c b/configs/stm3210e-eval/src/up_adc.c index f84a88d58c..fa05f547b5 100644 --- a/configs/stm3210e-eval/src/up_adc.c +++ b/configs/stm3210e-eval/src/up_adc.c @@ -124,6 +124,11 @@ int adc_devinit(void) /* Call stm32_adcinitialize() to get an instance of the ADC interface */ adc = stm32_adcinitialize(1, g_chanlist, ADC_NCHANNELS); + if (adc == NULL) + { + adbg("ERROR: Failed to get ADC interface\n"); + return; + } /* Register the ADC driver at "/dev/adc0" */ @@ -133,7 +138,7 @@ int adc_devinit(void) adbg("adc_register failed: %d\n", ret); } - return OK; + return ret; } #endif /* CONFIG_STM32_ADC || CONFIG_STM32_ADC2 || CONFIG_STM32_ADC3 */ diff --git a/configs/stm3240g-eval/README.txt b/configs/stm3240g-eval/README.txt index 5bed82cc85..78e70f4000 100755 --- a/configs/stm3240g-eval/README.txt +++ b/configs/stm3240g-eval/README.txt @@ -524,6 +524,8 @@ STM3240G-EVAL-specific Configuration Options Default: 8 CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. Default: 4 + CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback + mode for testing. The STM32 CAN driver does support loopback mode. CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. diff --git a/configs/stm3240g-eval/dhcpd/defconfig b/configs/stm3240g-eval/dhcpd/defconfig index a0ba27c0b9..17fa324d9b 100755 --- a/configs/stm3240g-eval/dhcpd/defconfig +++ b/configs/stm3240g-eval/dhcpd/defconfig @@ -265,14 +265,17 @@ CONFIG_SSI_POLLWAIT=y # Default: 8 # CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. # Default: 4 +# CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback +# mode for testing. The STM32 CAN driver does support loopback mode. # CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. # CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. # CONFIG_CAN=n #CONFIG_CAN_FIFOSIZE #CONFIG_CAN_NPENDINGRTR -CONFIG_CAN1_BAUD=115200 -CONFIG_CAN2_BAUD=115200 +CONFIG_CAN_LOOPBACK=n +CONFIG_CAN1_BAUD=700000 +CONFIG_CAN2_BAUD=700000 # # STM32F40xxx Ethernet device driver settings diff --git a/configs/stm3240g-eval/nettest/defconfig b/configs/stm3240g-eval/nettest/defconfig index afcd39512c..216cbee480 100755 --- a/configs/stm3240g-eval/nettest/defconfig +++ b/configs/stm3240g-eval/nettest/defconfig @@ -265,14 +265,17 @@ CONFIG_SSI_POLLWAIT=y # Default: 8 # CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. # Default: 4 +# CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback +# mode for testing. The STM32 CAN driver does support loopback mode. # CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. # CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. # CONFIG_CAN=n #CONFIG_CAN_FIFOSIZE #CONFIG_CAN_NPENDINGRTR -CONFIG_CAN1_BAUD=115200 -CONFIG_CAN2_BAUD=115200 +CONFIG_CAN_LOOPBACK=n +CONFIG_CAN1_BAUD=700000 +CONFIG_CAN2_BAUD=700000 # # STM32F40xxx Ethernet device driver settings diff --git a/configs/stm3240g-eval/nsh/appconfig b/configs/stm3240g-eval/nsh/appconfig index 6951578dac..6e9df6c9cd 100644 --- a/configs/stm3240g-eval/nsh/appconfig +++ b/configs/stm3240g-eval/nsh/appconfig @@ -54,4 +54,7 @@ ifeq ($(CONFIG_PWM),y) CONFIGURED_APPS += examples/pwm endif +ifeq ($(CONFIG_CAN),y) +CONFIGURED_APPS += examples/can +endif diff --git a/configs/stm3240g-eval/nsh/defconfig b/configs/stm3240g-eval/nsh/defconfig index 72ab549efa..efc95eccbe 100755 --- a/configs/stm3240g-eval/nsh/defconfig +++ b/configs/stm3240g-eval/nsh/defconfig @@ -265,14 +265,17 @@ CONFIG_SSI_POLLWAIT=y # Default: 8 # CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. # Default: 4 +# CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback +# mode for testing. The STM32 CAN driver does support loopback mode. # CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. # CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. # CONFIG_CAN=n #CONFIG_CAN_FIFOSIZE #CONFIG_CAN_NPENDINGRTR -CONFIG_CAN1_BAUD=115200 -CONFIG_CAN2_BAUD=115200 +CONFIG_CAN_LOOPBACK=n +CONFIG_CAN1_BAUD=700000 +CONFIG_CAN2_BAUD=700000 # # STM32F40xxx Ethernet device driver settings diff --git a/configs/stm3240g-eval/ostest/defconfig b/configs/stm3240g-eval/ostest/defconfig index e0b696dfee..4811aaf392 100755 --- a/configs/stm3240g-eval/ostest/defconfig +++ b/configs/stm3240g-eval/ostest/defconfig @@ -265,14 +265,17 @@ CONFIG_SSI_POLLWAIT=y # Default: 8 # CONFIG_CAN_NPENDINGRTR - The size of the list of pending RTR requests. # Default: 4 +# CONFIG_CAN_LOOPBACK - A CAN driver may or may not support a loopback +# mode for testing. The STM32 CAN driver does support loopback mode. # CONFIG_CAN1_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN1 is defined. # CONFIG_CAN2_BAUD - CAN1 BAUD rate. Required if CONFIG_STM32_CAN2 is defined. # CONFIG_CAN=n #CONFIG_CAN_FIFOSIZE #CONFIG_CAN_NPENDINGRTR -CONFIG_CAN1_BAUD=115200 -CONFIG_CAN2_BAUD=115200 +CONFIG_CAN_LOOPBACK=n +CONFIG_CAN1_BAUD=700000 +CONFIG_CAN2_BAUD=700000 # # STM32F40xxx Ethernet device driver settings diff --git a/configs/stm3240g-eval/src/Makefile b/configs/stm3240g-eval/src/Makefile index 7d09e145ae..6cda7250de 100644 --- a/configs/stm3240g-eval/src/Makefile +++ b/configs/stm3240g-eval/src/Makefile @@ -50,6 +50,10 @@ ifeq ($(CONFIG_PWM),y) CSRCS += up_pwm.c endif +ifeq ($(CONFIG_CAN),y) +CSRCS += up_can.c +endif + COBJS = $(CSRCS:.c=$(OBJEXT)) SRCS = $(ASRCS) $(CSRCS) diff --git a/configs/stm3240g-eval/src/stm3240g-internal.h b/configs/stm3240g-eval/src/stm3240g-internal.h index fb4ce466b8..32b9ff577c 100644 --- a/configs/stm3240g-eval/src/stm3240g-internal.h +++ b/configs/stm3240g-eval/src/stm3240g-internal.h @@ -48,7 +48,7 @@ /**************************************************************************************************** * Definitions ****************************************************************************************************/ - +/* Configuration ************************************************************************************/ /* How many SPI modules does this chip support? */ #if STM32_NSPI < 1 @@ -62,6 +62,36 @@ # undef CONFIG_STM32_SPI3 #endif +/* You can use either CAN1 or CAN2, but you can't use both because they share the same transceiver */ + +#if defined(CONFIG_STM32_CAN1) && defined(CONFIG_STM32_CAN2) +# warning "The STM3250G-EVAL will only support one of CAN1 and CAN2" +#endif + +/* You can't use CAN1 with FSMC: + * + * PD0 = FSMC_D2 & CAN1_RX + * PD1 = FSMC_D3 & CAN1_TX + */ + +#ifndef CONFIG_CAN_LOOPBACK +# if defined(CONFIG_STM32_CAN1) && defined(CONFIG_STM32_FSMC) +# warning "The STM3250G-EVAL will only support one of CAN1 and FSMC" +# endif +#endif + +/* The USB OTG HS ULPI bus is shared with CAN2 bus: + * + * PB13 = ULPI_D6 & CAN2_TX + * PB5 = ULPI_D7 & CAN2_RX + */ + +#ifndef CONFIG_CAN_LOOPBACK +# if defined(CONFIG_STM32_CAN2) && defined(CONFIG_STM32_OTGHS) +# warning "The STM3250G-EVAL will only support one of CAN2 and USB OTG HS" +# endif +#endif + /* STM3240G-EVAL GPIOs ******************************************************************************/ /* LEDs */ diff --git a/configs/stm3240g-eval/src/up_adc.c b/configs/stm3240g-eval/src/up_adc.c index c96c2b6063..c7db7a3c38 100644 --- a/configs/stm3240g-eval/src/up_adc.c +++ b/configs/stm3240g-eval/src/up_adc.c @@ -91,21 +91,37 @@ * ************************************************************************************/ -void adc_devinit(void) +int adc_devinit(void) { + static bool initialized = false; struct adc_dev_s *adc; int ret; - /* Call stm32_adcinitialize() to get an instance of the ADC interface */ + /* Check if we have already initialized */ + + if (!initialized) + { + /* Configure the pins as analog inputs for the selected channels */ #warning "Missing Logic" - /* Register the ADC driver at "/dev/adc0" */ + /* Call stm32_adcinitialize() to get an instance of the ADC interface */ +#warning "Missing Logic" - ret = adc_register("/dev/adc0", adc); - if (ret < 0) - { - adbg("adc_register failed: %d\n", ret); + /* Register the ADC driver at "/dev/adc0" */ + + ret = adc_register("/dev/adc0", adc); + if (ret < 0) + { + adbg("adc_register failed: %d\n", ret); + return ret; + } + + /* Now we are initialized */ + + initialized = true; } + + return OK; } #endif /* CONFIG_STM32_ADC || CONFIG_STM32_ADC2 || CONFIG_STM32_ADC3 */ diff --git a/configs/stm3240g-eval/src/up_can.c b/configs/stm3240g-eval/src/up_can.c new file mode 100644 index 0000000000..eafcec0bf5 --- /dev/null +++ b/configs/stm3240g-eval/src/up_can.c @@ -0,0 +1,142 @@ +/************************************************************************************ + * configs/stm3240g-eval/src/up_can.c + * arch/arm/src/board/up_can.c + * + * Copyright (C) 2011 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 NuttX 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 OWNER 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. + * + ************************************************************************************/ + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include + +#include +#include + +#include +#include + +#include "chip.h" +#include "up_arch.h" + +#include "stm32.h" +#include "stm32_can.h" +#include "stm3240g-internal.h" + +#if defined(CONFIG_CAN) && (defined(CONFIG_STM32_CAN1) || defined(CONFIG_STM32_CAN2)) + +/************************************************************************************ + * Pre-processor Definitions + ************************************************************************************/ +/* Configuration ********************************************************************/ + +#if defined(CONFIG_STM32_CAN1) && defined(CONFIG_STM32_CAN2) +# warning "Both CAN1 and CAN2 are enabled. Assuming only CAN1." +# undef CONFIG_STM32_CAN2 +#endif + +#ifdef CONFIG_STM32_CAN1 +# define CAN_PORT 1 +#else +# define CAN_PORT 2 +#endif + +/* Debug ***************************************************************************/ +/* Non-standard debug that may be enabled just for testing CAN */ + +#ifdef CONFIG_DEBUG_CAN +# define candbg dbg +# define canvdbg vdbg +# define canlldbg lldbg +# define canllvdbg llvdbg +#else +# define candbg(x...) +# define canvdbg(x...) +# define canlldbg(x...) +# define canllvdbg(x...) +#endif + +/************************************************************************************ + * Private Functions + ************************************************************************************/ + +/************************************************************************************ + * Public Functions + ************************************************************************************/ + +/************************************************************************************ + * Name: can_devinit + * + * Description: + * All STM32 architectures must provide the following interface to work with + * examples/can. + * + ************************************************************************************/ + +int can_devinit(void) +{ + static bool initialized = false; + struct can_dev_s *can; + int ret; + + /* Check if we have already initialized */ + + if (!initialized) + { + /* Call stm32_caninitialize() to get an instance of the CAN interface */ + + can = stm32_caninitialize(CAN_PORT); + if (can == NULL) + { + candbg("ERROR: Failed to get CAN interface\n"); + return -ENODEV; + } + + /* Register the CAN driver at "/dev/can0" */ + + ret = can_register("/dev/can0", can); + if (ret < 0) + { + candbg("ERROR: can_register failed: %d\n", ret); + return ret; + } + + /* Now we are initialized */ + + initialized = true; + } + + return OK; +} + +#endif /* CONFIG_STM32_CAN || CONFIG_STM32_CAN2 || CONFIG_STM32_CAN3 */