Working fork to test F0 application

Dependents:   ppCANOpen_Example

Fork of CANnucleo by Zoltan Hudak

Revision:
5:b53e5ee15315
Parent:
3:0fae6b54a2ee
Child:
6:c5a40d5fd9f1
diff -r 38403b42718b -r b53e5ee15315 can_api.c
--- a/can_api.c	Thu Jul 23 13:10:34 2015 +0000
+++ b/can_api.c	Tue Aug 04 11:58:09 2015 +0000
@@ -1,16 +1,16 @@
 /*
- can_api.c for STMicroelectronics mbed boards equipped with Controller Area Network interface:
-
- NUCLEO-F072RB
- NUCLEO-F091RC
- NUCLEO-F103RB
- NUCLEO-F302R8
- NUCLEO-F303RE
- NUCLEO-F334R8
- DISCO-F334C8
-
- Copyright (c) 2015 Zoltan Hudak <hudakz@inbox.com>
- All rights reserved.
+  ******************************************************************************
+  * @file    can_api.c
+  * @author  Zoltan Hudak
+  * @version 
+  * @date    04-August-2015
+  * @brief   CAN api for NUCLEO-F103RB platform
+  ******************************************************************************
+  * @attention
+  *
+  * <h2><center>&copy; COPYRIGHT(c) 2015 Zoltan Hudak <hudakz@inbox.com>
+  *
+  * All rights reserved.
 
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
@@ -63,12 +63,7 @@
 int can_frequency(can_t* obj, int hz) {
     HAL_NVIC_DisableIRQ(USB_LP_CAN1_RX0_IRQn);
 
-#if defined(TARGET_NUCLEO_F103RB) || \
-    defined(TARGET_NUCLEO_F302R8) || \
-    defined(TARGET_NUCLEO_F303RE) || \
-    defined(TARGET_NUCLEO_F334R8) || \
-    defined(TARGET_DISCO_F334C8)
-    // APB1 pheripheral clock = 36000000Hz
+    // APB1 peripheral clock = 36000000Hz
 
     switch(hz) {
     case 1000000:
@@ -105,63 +100,20 @@
 
     default:
         // 125kbps (default)
+#if DEBUG
         printf("Unknown frequency specified!\r\n");
         printf("Using default 125kbps\r\n");
+#endif
         _canHandle.Init.Prescaler = 18;     // number of time quanta = 36000000/18/125000 = 16
         _canHandle.Init.SJW = CAN_SJW_1TQ;
         _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
         _canHandle.Init.BS2 = CAN_BS2_4TQ;
     }
 
-#elif defined(TARGET_NUCLEO_F072RB) || \ 
-      defined(TARGET_NUCLEO_F091RC)
-    // APB1 pheripheral clock = 48000000Hz
-
-    switch(hz) {
-    case 1000000:
-        // 1000kbps bit rate
-        _canHandle.Init.Prescaler = 4;      // number of time quanta = 48000000/4/1000000 = 12
-        _canHandle.Init.SJW = CAN_SJW_1TQ;
-        _canHandle.Init.BS1 = CAN_BS1_8TQ;  // sample point at: (1 + 8) / 12 * 100 = 75%
-        _canHandle.Init.BS2 = CAN_BS2_3TQ;
-        break;
-
-    case 500000:
-        // 500kbps bit rate
-        _canHandle.Init.Prescaler = 8;      // number of time quanta = 48000000/8/500000 = 12
-        _canHandle.Init.SJW = CAN_SJW_1TQ;
-        _canHandle.Init.BS1 = CAN_BS1_8TQ;  // sample point at: (1 + 8) / 12 * 100 = 75%
-        _canHandle.Init.BS2 = CAN_BS2_3TQ;
-        break;
-
-    case 250000:
-        // 250kbps
-        _canHandle.Init.Prescaler = 12;     // number of time quanta = 48000000/12/250000 = 16
-        _canHandle.Init.SJW = CAN_SJW_1TQ;
-        _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
-        _canHandle.Init.BS2 = CAN_BS2_4TQ;
-        break;
-
-    case 125000:
-        // 125kbps
-        _canHandle.Init.Prescaler = 24;     // number of time quanta = 48000000/24/125000 = 16
-        _canHandle.Init.SJW = CAN_SJW_1TQ;
-        _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
-        _canHandle.Init.BS2 = CAN_BS2_4TQ;
-        break;
-
-    default:
-        // 125kbps (default)
-        printf("Unknown frequency specified!\r\n");
-        printf("Using default 125kbps\r\n");
-        _canHandle.Init.Prescaler = 24;     // number of time quanta = 48000000/24/125000 = 16
-        _canHandle.Init.SJW = CAN_SJW_1TQ;
-        _canHandle.Init.BS1 = CAN_BS1_11TQ; // sample point at: (1 + 11) / 16 * 100 = 75%
-        _canHandle.Init.BS2 = CAN_BS2_4TQ;
-    }
-#endif
     HAL_CAN_Init(&_canHandle);
     HAL_NVIC_EnableIRQ(USB_LP_CAN1_RX0_IRQn);
+    
+    return 1;
 }
 
 /**
@@ -172,7 +124,9 @@
  */
 void can_irq_init(can_t* obj, can_irq_handler handler, uint32_t id) {
     if(HAL_CAN_Receive_IT(&_canHandle, CAN_FIFO0) != HAL_OK) {
+#ifdef DEBUG
         printf("CAN reception initialization error\r\n");
+#endif
     }
 }
 
@@ -222,8 +176,13 @@
         _canHandle.pTxMsg->Data[i] = msg.data[i];
 
     if(HAL_CAN_Transmit(&_canHandle, 10) != HAL_OK) {
+#ifdef DEBUG
         printf("Transmission error\r\n");
+#endif
+        return 0;
     }
+    else
+        return 1;
 }
 
 /**
@@ -233,14 +192,14 @@
  * @retval
  */
 int can_read(can_t* obj, CAN_Message* msg, int handle) {
-    int i = 0;
-
     msg->id = _canHandle.pRxMsg->IDE == CAN_ID_STD ? _canHandle.pRxMsg->StdId : _canHandle.pRxMsg->ExtId;
     msg->type = _canHandle.pRxMsg->RTR == CAN_RTR_DATA ? CANData : CANRemote;
     msg->format = _canHandle.pRxMsg->IDE == CAN_ID_STD ? CANStandard : CANExtended;
     msg->len = _canHandle.pRxMsg->DLC;
-    for(i = 0; i < msg->len; i++)
+    for(int i = 0; i < msg->len; i++)
         msg->data[i] = _canHandle.pRxMsg->Data[i];
+        
+    return msg->len;
 }
 
 /**
@@ -298,6 +257,8 @@
     sFilterConfig.FilterActivation = ENABLE;
     sFilterConfig.BankNumber = 14;
     HAL_CAN_ConfigFilter(&_canHandle, &sFilterConfig);
+    
+    return 1;
 }
 
 /**
@@ -341,3 +302,4 @@
     // not implemented
 }
 
+