diff --git a/Src/#stm32f0xx_hal_msp.c# b/Src/#stm32f0xx_hal_msp.c# new file mode 100644 index 0000000..91938df --- /dev/null +++ b/Src/#stm32f0xx_hal_msp.c# @@ -0,0 +1,117 @@ +/** + ****************************************************************************** + * File Name : stm32f0xx_hal_msp.c + * Date : 05/12/2014 20:22:27 + * Description : This file provides code for the MSP Initialization + * and de-Initialization codes. + ****************************************************************************** + * + * COPYRIGHT(c) 2014 STMicroelectronics + * + * 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 of STMicroelectronics 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 HOLDER 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. + * + ****************************************************************************** + */ +/* Includes ------------------------------------------------------------------*/ +#include "stm32f0xx_hal.h" + +/* USER CODE BEGIN 0 */ + +/* USER CODE END 0 */ + +/** + * Initializes the Global MSP. + */ +void HAL_MspInit(void) +{ + /* USER CODE BEGIN MspInit 0 */ + + /* USER CODE END MspInit 0 */ + + /* System interrupt init*/ +/* SysTick_IRQn interrupt configuration */ + HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0); + + /* USER CODE BEGIN MspInit 1 */ + + /* USER CODE END MspInit 1 */ +} + +void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) +{ + + GPIO_InitTypeDef GPIO_InitStruct; + if(hcan->Instance==CAN) + { + /* USER CODE BEGIN CAN_MspInit 0 */ + + /* USER CODE END CAN_MspInit 0 */ + /* Peripheral clock enable */ + __CAN_CLK_ENABLE(); + + /**CAN GPIO Configuration + PB8 ------> CAN_RX + PB9 ------> CAN_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_LOW; + GPIO_InitStruct.Alternate = GPIO_AF4_CAN; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + + /* USER CODE BEGIN CAN_MspInit 1 */ + + /* USER CODE END CAN_MspInit 1 */ + } + +} + +void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan) +{ + + if(hcan->Instance==CAN) + { + /* USER CODE BEGIN CAN_MspDeInit 0 */ + + /* USER CODE END CAN_MspDeInit 0 */ + /* Peripheral clock disable */ + __CAN_CLK_DISABLE(); + + /**CAN GPIO Configuration + PB8 ------> CAN_RX + PB9 ------> CAN_TX + */ + HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8|GPIO_PIN_9); + + /* USER CODE BEGIN CAN_MspDeInit 1 */ + + /* USER CODE END CAN_MspDeInit 1 */ + } + +} +/* USER CODE BEGIN 1 */ + +/* USER CODE END 1 */ + +/** * @} */ /** * @} */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ diff --git a/Src/.#stm32f0xx_hal_msp.c b/Src/.#stm32f0xx_hal_msp.c new file mode 120000 index 0000000..63e9ec0 --- /dev/null +++ b/Src/.#stm32f0xx_hal_msp.c @@ -0,0 +1 @@ +eric@Erics-MBP-2.lan.380 \ No newline at end of file diff --git a/Src/can.c b/Src/can.c index 1af981d..d26879e 100644 --- a/Src/can.c +++ b/Src/can.c @@ -39,39 +39,40 @@ void can_init(void) { void can_enable(void) { uint32_t status; if (bus_state == OFF_BUS) { - hcan.Init.Mode = CAN_MODE_NORMAL; - hcan.pTxMsg = NULL; - status = HAL_CAN_Init(&hcan); - status = HAL_CAN_ConfigFilter(&hcan, &filter); - bus_state = ON_BUS; + hcan.Init.Mode = CAN_MODE_NORMAL; + hcan.pTxMsg = NULL; + status = HAL_CAN_Init(&hcan); + status = HAL_CAN_ConfigFilter(&hcan, &filter); + bus_state = ON_BUS; } } void can_disable(void) { uint32_t status; if (bus_state == ON_BUS) { - // do a bxCAN reset (set RESET bit to 1) - hcan.Instance->MCR |= CAN_MCR_RESET; - bus_state = OFF_BUS; + // do a bxCAN reset (set RESET bit to 1) + hcan.Instance->MCR |= CAN_MCR_RESET; + bus_state = OFF_BUS; } + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); } void can_set_bitrate(enum can_bitrate bitrate) { if (bus_state == ON_BUS) { - // cannot set bitrate while on bus - return; + // cannot set bitrate while on bus + return; } switch (bitrate) { - case CAN_BITRATE_500K: - hcan.Init.Prescaler = 12; - break; - case CAN_BITRATE_250K: - hcan.Init.Prescaler = 24; - break; - case CAN_BITRATE_125K: - hcan.Init.Prescaler = 48; - break; + case CAN_BITRATE_500K: + hcan.Init.Prescaler = 12; + break; + case CAN_BITRATE_250K: + hcan.Init.Prescaler = 24; + break; + case CAN_BITRATE_125K: + hcan.Init.Prescaler = 48; + break; } } @@ -82,6 +83,7 @@ uint32_t can_tx(CanTxMsgTypeDef *tx_msg, uint32_t timeout) { hcan.pTxMsg = tx_msg; status = HAL_CAN_Transmit(&hcan, timeout); + HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1); return status; } @@ -92,12 +94,13 @@ uint32_t can_rx(CanRxMsgTypeDef *rx_msg, uint32_t timeout) { status = HAL_CAN_Receive(&hcan, CAN_FIFO0, timeout); + HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_1); return status; } uint8_t is_can_msg_pending(uint8_t fifo) { if (bus_state == OFF_BUS) { - return 0; + return 0; } return (__HAL_CAN_MSG_PENDING(&hcan, fifo) > 0); } diff --git a/Src/main.c b/Src/main.c index 277d713..45de8a3 100644 --- a/Src/main.c +++ b/Src/main.c @@ -52,6 +52,7 @@ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); +static void led_init(void); /* USER CODE BEGIN PFP */ @@ -80,21 +81,28 @@ int main(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); can_init(); + led_init(); MX_USB_DEVICE_Init(); /* USER CODE END 2 */ /* USER CODE BEGIN 3 */ - uint32_t count; - // start up delay to allow USB to initialize? - for(count=0; count<32000000; count++); + // turn on green LED + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET); + + // blink red LED for test + uint32_t count; + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET); + for (count=0; count < 200000; count++) { __asm("nop");} + HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET); // loop forever CanRxMsgTypeDef rx_msg; uint32_t status; uint8_t msg_buf[SLCAN_MTU]; + for (;;) { while (!is_can_msg_pending()); status = can_rx(&rx_msg, 3); @@ -102,11 +110,6 @@ int main(void) status = slcan_parse_frame(&msg_buf, &rx_msg); CDC_Transmit_FS(msg_buf, status); } - - #if 0 - for(count=0; count < 16000000; count++); - CDC_Transmit_FS(str, sizeof(str)); - #endif } /* USER CODE END 3 */ @@ -166,7 +169,15 @@ void MX_GPIO_Init(void) } /* USER CODE BEGIN 4 */ - +static void led_init() { + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; + GPIO_InitStruct.Alternate = 0; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); +} /* USER CODE END 4 */ #ifdef USE_FULL_ASSERT diff --git a/Src/stm32f0xx_hal_msp.c b/Src/stm32f0xx_hal_msp.c index 0ab31d2..db1355d 100644 --- a/Src/stm32f0xx_hal_msp.c +++ b/Src/stm32f0xx_hal_msp.c @@ -81,6 +81,12 @@ void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE BEGIN CAN_MspInit 1 */ + GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM; + GPIO_InitStruct.Alternate = 0; + HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* USER CODE END CAN_MspInit 1 */ }