Working fork to test F0 application

Dependents:   ppCANOpen_Example

Fork of CANnucleo by Zoltan Hudak

Committer:
ptpaterson
Date:
Thu Jan 07 04:23:51 2016 +0000
Revision:
18:cdab1fd4ff26
Parent:
16:f4c8f45bded9
Corrected interrupts for F0 chip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hudakz 16:f4c8f45bded9 1 /**
hudakz 16:f4c8f45bded9 2 ******************************************************************************
hudakz 16:f4c8f45bded9 3 * @file stm32f3xx_hal_msp.c
hudakz 16:f4c8f45bded9 4 * @author MCD Application Team
hudakz 16:f4c8f45bded9 5 * @version V1.0.0
hudakz 16:f4c8f45bded9 6 * @date 17-December-2014
hudakz 16:f4c8f45bded9 7 * @brief HAL MSP module.
hudakz 16:f4c8f45bded9 8 ******************************************************************************
hudakz 16:f4c8f45bded9 9 * @attention
hudakz 16:f4c8f45bded9 10 *
hudakz 16:f4c8f45bded9 11 * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
hudakz 16:f4c8f45bded9 12 *
hudakz 16:f4c8f45bded9 13 * Redistribution and use in source and binary forms, with or without modification,
hudakz 16:f4c8f45bded9 14 * are permitted provided that the following conditions are met:
hudakz 16:f4c8f45bded9 15 * 1. Redistributions of source code must retain the above copyright notice,
hudakz 16:f4c8f45bded9 16 * this list of conditions and the following disclaimer.
hudakz 16:f4c8f45bded9 17 * 2. Redistributions in binary form must reproduce the above copyright notice,
hudakz 16:f4c8f45bded9 18 * this list of conditions and the following disclaimer in the documentation
hudakz 16:f4c8f45bded9 19 * and/or other materials provided with the distribution.
hudakz 16:f4c8f45bded9 20 * 3. Neither the name of STMicroelectronics nor the names of its contributors
hudakz 16:f4c8f45bded9 21 * may be used to endorse or promote products derived from this software
hudakz 16:f4c8f45bded9 22 * without specific prior written permission.
hudakz 16:f4c8f45bded9 23 *
hudakz 16:f4c8f45bded9 24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
hudakz 16:f4c8f45bded9 25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
hudakz 16:f4c8f45bded9 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
hudakz 16:f4c8f45bded9 27 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
hudakz 16:f4c8f45bded9 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
hudakz 16:f4c8f45bded9 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
hudakz 16:f4c8f45bded9 30 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
hudakz 16:f4c8f45bded9 31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
hudakz 16:f4c8f45bded9 32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
hudakz 16:f4c8f45bded9 33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
hudakz 16:f4c8f45bded9 34 *
hudakz 16:f4c8f45bded9 35 ******************************************************************************
hudakz 16:f4c8f45bded9 36 *
hudakz 16:f4c8f45bded9 37 * Modified by Zoltan Hudak <hudakz@inbox.com>
hudakz 16:f4c8f45bded9 38 *
hudakz 16:f4c8f45bded9 39 ******************************************************************************
hudakz 16:f4c8f45bded9 40 */
hudakz 16:f4c8f45bded9 41 #if defined(TARGET_NUCLEO_F072RB) || \
hudakz 16:f4c8f45bded9 42 defined(TARGET_NUCLEO_F091RC)
hudakz 16:f4c8f45bded9 43
hudakz 16:f4c8f45bded9 44 #include "can_api.h"
hudakz 16:f4c8f45bded9 45 #include "pinmap.h"
hudakz 16:f4c8f45bded9 46
hudakz 16:f4c8f45bded9 47 CAN_HandleTypeDef _canHandle;
hudakz 16:f4c8f45bded9 48 CanRxMsgTypeDef _canRxMsg;
hudakz 16:f4c8f45bded9 49 CanTxMsgTypeDef _canTxMsg;
hudakz 16:f4c8f45bded9 50 PinName _rxPin;
hudakz 16:f4c8f45bded9 51 PinName _txPin;
hudakz 16:f4c8f45bded9 52
hudakz 16:f4c8f45bded9 53 void (*rxCompleteCallback) (void);
hudakz 16:f4c8f45bded9 54
hudakz 16:f4c8f45bded9 55 /**
hudakz 16:f4c8f45bded9 56 * @brief CAN initialization.
hudakz 16:f4c8f45bded9 57 * @param obj: can_t object
hudakz 16:f4c8f45bded9 58 * @param rxPin: RX pin name
hudakz 16:f4c8f45bded9 59 * @param txPin: TX pin name
hudakz 16:f4c8f45bded9 60 * @param abom: Automatic recovery from bus-off state
hudakz 16:f4c8f45bded9 61 * @retval None
hudakz 16:f4c8f45bded9 62 */
hudakz 16:f4c8f45bded9 63 void initCAN(can_t* obj, PinName rxPin, PinName txPin, FunctionalState abom) {
hudakz 16:f4c8f45bded9 64 _rxPin = rxPin;
hudakz 16:f4c8f45bded9 65 _txPin = txPin;
hudakz 16:f4c8f45bded9 66
hudakz 16:f4c8f45bded9 67 _canHandle.Instance = ((CAN_TypeDef*)CAN_BASE);
hudakz 16:f4c8f45bded9 68 _canHandle.pTxMsg = &_canTxMsg;
hudakz 16:f4c8f45bded9 69 _canHandle.pRxMsg = &_canRxMsg;
hudakz 16:f4c8f45bded9 70
hudakz 16:f4c8f45bded9 71 _canHandle.Init.TTCM = DISABLE;
hudakz 16:f4c8f45bded9 72 _canHandle.Init.ABOM = abom;
hudakz 16:f4c8f45bded9 73 _canHandle.Init.AWUM = DISABLE;
hudakz 16:f4c8f45bded9 74 _canHandle.Init.NART = DISABLE;
hudakz 16:f4c8f45bded9 75 _canHandle.Init.RFLM = DISABLE;
hudakz 16:f4c8f45bded9 76 _canHandle.Init.TXFP = DISABLE;
hudakz 16:f4c8f45bded9 77 _canHandle.Init.Mode = CAN_MODE_NORMAL;
hudakz 16:f4c8f45bded9 78
hudakz 16:f4c8f45bded9 79 // 125kbps bit rate (default)
hudakz 16:f4c8f45bded9 80 // APB1 peripheral clock = 48000000Hz
hudakz 16:f4c8f45bded9 81 _canHandle.Init.Prescaler = 24; // number of time quanta = 48000000/24/125000 = 16
hudakz 16:f4c8f45bded9 82 _canHandle.Init.SJW = CAN_SJW_1TQ;
hudakz 16:f4c8f45bded9 83 _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
hudakz 16:f4c8f45bded9 84 _canHandle.Init.BS2 = CAN_BS2_4TQ;
hudakz 16:f4c8f45bded9 85
hudakz 16:f4c8f45bded9 86 HAL_CAN_Init(&_canHandle);
hudakz 16:f4c8f45bded9 87 }
hudakz 16:f4c8f45bded9 88
hudakz 16:f4c8f45bded9 89 /**
hudakz 16:f4c8f45bded9 90 * @brief CAN MSP Initialization
hudakz 16:f4c8f45bded9 91 * @param hcan: CAN handle pointer
hudakz 16:f4c8f45bded9 92 * @retval None
hudakz 16:f4c8f45bded9 93 */
hudakz 16:f4c8f45bded9 94 void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) {
hudakz 16:f4c8f45bded9 95 GPIO_InitTypeDef GPIO_InitStruct;
hudakz 16:f4c8f45bded9 96
hudakz 16:f4c8f45bded9 97 if((_rxPin == PA_11) && (_txPin == PA_12)) {
hudakz 16:f4c8f45bded9 98
hudakz 16:f4c8f45bded9 99 /* CAN1 Periph clock enable */
hudakz 16:f4c8f45bded9 100 __CAN_CLK_ENABLE();
hudakz 16:f4c8f45bded9 101
hudakz 16:f4c8f45bded9 102 /* Enable GPIO clock */
hudakz 16:f4c8f45bded9 103 __GPIOA_CLK_ENABLE();
hudakz 16:f4c8f45bded9 104
hudakz 16:f4c8f45bded9 105 /* CAN1 RX GPIO pin configuration */
hudakz 16:f4c8f45bded9 106 GPIO_InitStruct.Pin = GPIO_PIN_11;
hudakz 16:f4c8f45bded9 107 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
hudakz 16:f4c8f45bded9 108 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
hudakz 16:f4c8f45bded9 109 GPIO_InitStruct.Pull = GPIO_NOPULL;
hudakz 16:f4c8f45bded9 110 GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
hudakz 16:f4c8f45bded9 111 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hudakz 16:f4c8f45bded9 112
hudakz 16:f4c8f45bded9 113 /* CAN1 TX GPIO pin configuration */
hudakz 16:f4c8f45bded9 114 GPIO_InitStruct.Pin = GPIO_PIN_12;
hudakz 16:f4c8f45bded9 115 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
hudakz 16:f4c8f45bded9 116 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
hudakz 16:f4c8f45bded9 117 GPIO_InitStruct.Pull = GPIO_NOPULL;
hudakz 16:f4c8f45bded9 118 GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
hudakz 16:f4c8f45bded9 119 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
hudakz 16:f4c8f45bded9 120 }
hudakz 16:f4c8f45bded9 121 else
hudakz 16:f4c8f45bded9 122 if((_rxPin == PB_8) && (_txPin == PB_9)) {
hudakz 16:f4c8f45bded9 123 /* CAN1 Periph clock enable */
hudakz 16:f4c8f45bded9 124 __CAN_CLK_ENABLE();
hudakz 16:f4c8f45bded9 125
hudakz 16:f4c8f45bded9 126 /* Enable GPIO clock */
hudakz 16:f4c8f45bded9 127 __GPIOB_CLK_ENABLE();
hudakz 16:f4c8f45bded9 128
hudakz 16:f4c8f45bded9 129 /* CAN1 RX GPIO pin configuration */
hudakz 16:f4c8f45bded9 130 GPIO_InitStruct.Pin = GPIO_PIN_8;
hudakz 16:f4c8f45bded9 131 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
hudakz 16:f4c8f45bded9 132 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
hudakz 16:f4c8f45bded9 133 GPIO_InitStruct.Pull = GPIO_NOPULL;
hudakz 16:f4c8f45bded9 134 GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
hudakz 16:f4c8f45bded9 135 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
hudakz 16:f4c8f45bded9 136
hudakz 16:f4c8f45bded9 137 /* CAN1 TX GPIO pin configuration */
hudakz 16:f4c8f45bded9 138 GPIO_InitStruct.Pin = GPIO_PIN_9;
hudakz 16:f4c8f45bded9 139 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
hudakz 16:f4c8f45bded9 140 GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
hudakz 16:f4c8f45bded9 141 GPIO_InitStruct.Pull = GPIO_NOPULL;
hudakz 16:f4c8f45bded9 142 GPIO_InitStruct.Alternate = GPIO_AF4_CAN;
hudakz 16:f4c8f45bded9 143 HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
hudakz 16:f4c8f45bded9 144 }
hudakz 16:f4c8f45bded9 145 else
hudakz 16:f4c8f45bded9 146 return;
hudakz 16:f4c8f45bded9 147 /* NVIC configuration for CAN1 Reception complete interrupt */
ptpaterson 18:cdab1fd4ff26 148 HAL_NVIC_SetPriority(CEC_CAN_IRQn, 1, 0);
ptpaterson 18:cdab1fd4ff26 149 HAL_NVIC_EnableIRQ(CEC_CAN_IRQn);
hudakz 16:f4c8f45bded9 150 }
hudakz 16:f4c8f45bded9 151
hudakz 16:f4c8f45bded9 152 /**
hudakz 16:f4c8f45bded9 153 * @brief CAN MSP De-Initialization
hudakz 16:f4c8f45bded9 154 * This function frees the hardware resources used:
hudakz 16:f4c8f45bded9 155 * - Disable the Peripheral's clock
hudakz 16:f4c8f45bded9 156 * - Revert GPIO to their default state
hudakz 16:f4c8f45bded9 157 * @param hcan: CAN handle pointer
hudakz 16:f4c8f45bded9 158 * @retval None
hudakz 16:f4c8f45bded9 159 */
hudakz 16:f4c8f45bded9 160 void HAL_CAN_MspDeInit(CAN_HandleTypeDef* hcan) {
hudakz 16:f4c8f45bded9 161
hudakz 16:f4c8f45bded9 162 /* Reset peripherals */
hudakz 16:f4c8f45bded9 163
hudakz 16:f4c8f45bded9 164 __CAN_FORCE_RESET();
hudakz 16:f4c8f45bded9 165 __CAN_RELEASE_RESET();
hudakz 16:f4c8f45bded9 166
hudakz 16:f4c8f45bded9 167 /* Disable peripherals and GPIO Clocks */
hudakz 16:f4c8f45bded9 168 if((_rxPin == PA_11) && (_txPin == PA_12)) {
hudakz 16:f4c8f45bded9 169 /* De-initialize the CAN1 RX GPIO pin */
hudakz 16:f4c8f45bded9 170 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_11);
hudakz 16:f4c8f45bded9 171
hudakz 16:f4c8f45bded9 172 /* De-initialize the CAN1 TX GPIO pin */
hudakz 16:f4c8f45bded9 173 HAL_GPIO_DeInit(GPIOA, GPIO_PIN_12);
hudakz 16:f4c8f45bded9 174 }
hudakz 16:f4c8f45bded9 175 else {
hudakz 16:f4c8f45bded9 176
hudakz 16:f4c8f45bded9 177 /* De-initialize the CAN1 RX GPIO pin */
hudakz 16:f4c8f45bded9 178 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_8);
hudakz 16:f4c8f45bded9 179
hudakz 16:f4c8f45bded9 180 /* De-initialize the CAN1 TX GPIO pin */
hudakz 16:f4c8f45bded9 181 HAL_GPIO_DeInit(GPIOB, GPIO_PIN_9);
hudakz 16:f4c8f45bded9 182 }
hudakz 16:f4c8f45bded9 183
hudakz 16:f4c8f45bded9 184
hudakz 16:f4c8f45bded9 185 /* Disable the NVIC for CAN reception */
ptpaterson 18:cdab1fd4ff26 186 HAL_NVIC_DisableIRQ(CEC_CAN_IRQn);
hudakz 16:f4c8f45bded9 187 }
hudakz 16:f4c8f45bded9 188
hudakz 16:f4c8f45bded9 189 /**
hudakz 16:f4c8f45bded9 190 * @brief Handles CAN RX0 interrupt request.
hudakz 16:f4c8f45bded9 191 * @param None
ptpaterson 18:cdab1fd4ff26 192 * @note STM32F0 uses different interrupts than F4
hudakz 16:f4c8f45bded9 193 * @retval None
hudakz 16:f4c8f45bded9 194 */
ptpaterson 18:cdab1fd4ff26 195 void CEC_CAN_IRQHandler(void)
ptpaterson 18:cdab1fd4ff26 196 {
ptpaterson 18:cdab1fd4ff26 197 HAL_CAN_IRQHandler(&_canHandle);
hudakz 16:f4c8f45bded9 198 }
hudakz 16:f4c8f45bded9 199
ptpaterson 18:cdab1fd4ff26 200
ptpaterson 18:cdab1fd4ff26 201
hudakz 16:f4c8f45bded9 202 /**
hudakz 16:f4c8f45bded9 203 * @brief Reception complete callback in non blocking mode
hudakz 16:f4c8f45bded9 204 * @param _canHandle: pointer to a CAN_HandleTypeDef structure that contains
hudakz 16:f4c8f45bded9 205 * the configuration information for the specified CAN.
hudakz 16:f4c8f45bded9 206 * @retval None
hudakz 16:f4c8f45bded9 207 */
hudakz 16:f4c8f45bded9 208 void HAL_CAN_RxCpltCallback(CAN_HandleTypeDef* _canHandle) {
hudakz 16:f4c8f45bded9 209 // if(HAL_CAN_Receive_IT(_canHandle, CAN_FIFO0) == HAL_OK) {
hudakz 16:f4c8f45bded9 210 // if(rxCompleteCallback != NULL)
hudakz 16:f4c8f45bded9 211 // rxCompleteCallback();
hudakz 16:f4c8f45bded9 212 // }
hudakz 16:f4c8f45bded9 213 // else {
hudakz 16:f4c8f45bded9 214 // error_handler(error);
hudakz 16:f4c8f45bded9 215 // }
hudakz 16:f4c8f45bded9 216
hudakz 16:f4c8f45bded9 217 // BUG: CAN race condition if HAL_CAN_Receive_IT() is used.
hudakz 16:f4c8f45bded9 218 // See https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Java/Flat.aspx?RootFolder=%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Java%2FBUG%20CAN%20race%20condition%20if%20HAL%5FCAN%5FReceive%5FIT%20is%20used
hudakz 16:f4c8f45bded9 219 //
hudakz 16:f4c8f45bded9 220 // Fixed by Mark Burton:
hudakz 16:f4c8f45bded9 221 // ideally, we should be able to call HAL_CAN_Receive_IT() here to set up for another
hudakz 16:f4c8f45bded9 222 // receive but the API is flawed because that function will fail if HAL_CAN_Transmit()
hudakz 16:f4c8f45bded9 223 // had already locked the handle when the receive interrupt occurred - so we do what
hudakz 16:f4c8f45bded9 224 // HAL_CAN_Receive_IT() would do
hudakz 16:f4c8f45bded9 225
hudakz 16:f4c8f45bded9 226 if (rxCompleteCallback != 0)
hudakz 16:f4c8f45bded9 227 rxCompleteCallback();
hudakz 16:f4c8f45bded9 228
hudakz 16:f4c8f45bded9 229 if (_canHandle->State == HAL_CAN_STATE_BUSY_TX)
hudakz 16:f4c8f45bded9 230 _canHandle->State = HAL_CAN_STATE_BUSY_TX_RX;
hudakz 16:f4c8f45bded9 231 else {
hudakz 16:f4c8f45bded9 232 _canHandle->State = HAL_CAN_STATE_BUSY_RX;
hudakz 16:f4c8f45bded9 233
hudakz 16:f4c8f45bded9 234 /* Set CAN error code to none */
hudakz 16:f4c8f45bded9 235 _canHandle->ErrorCode = HAL_CAN_ERROR_NONE;
hudakz 16:f4c8f45bded9 236
hudakz 16:f4c8f45bded9 237 /* Enable Error warning Interrupt */
hudakz 16:f4c8f45bded9 238 __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_EWG);
hudakz 16:f4c8f45bded9 239
hudakz 16:f4c8f45bded9 240 /* Enable Error passive Interrupt */
hudakz 16:f4c8f45bded9 241 __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_EPV);
hudakz 16:f4c8f45bded9 242
hudakz 16:f4c8f45bded9 243 /* Enable Bus-off Interrupt */
hudakz 16:f4c8f45bded9 244 __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_BOF);
hudakz 16:f4c8f45bded9 245
hudakz 16:f4c8f45bded9 246 /* Enable Last error code Interrupt */
hudakz 16:f4c8f45bded9 247 __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_LEC);
hudakz 16:f4c8f45bded9 248
hudakz 16:f4c8f45bded9 249 /* Enable Error Interrupt */
hudakz 16:f4c8f45bded9 250 __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_ERR);
hudakz 16:f4c8f45bded9 251 }
hudakz 16:f4c8f45bded9 252
hudakz 16:f4c8f45bded9 253 // Enable FIFO 0 message pending Interrupt
hudakz 16:f4c8f45bded9 254 __HAL_CAN_ENABLE_IT(_canHandle, CAN_IT_FMP0);
hudakz 16:f4c8f45bded9 255 }
hudakz 16:f4c8f45bded9 256 #endif