test

Fork of CANnucleo by Zoltan Hudak

Revision:
16:f4c8f45bded9
Parent:
15:5123ead7b002
Child:
17:1fd35431ee8e
--- a/can_api.c	Sun Dec 27 09:36:47 2015 +0000
+++ b/can_api.c	Wed Dec 30 08:23:30 2015 +0000
@@ -61,7 +61,59 @@
  */
 int can_frequency(can_t* obj, int hz) {
     HAL_NVIC_DisableIRQ(CAN_IRQ);
+    
+#if defined(TARGET_NUCLEO_F072RB) || \
+    defined(TARGET_NUCLEO_F091RC)
+    
+        // APB1 peripheral 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)
+        _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;
+    }
 
+#elif defined(TARGET_NUCLEO_F103RB) || \
+    defined(TARGET_NUCLEO_F303RE) || \
+    defined(TARGET_NUCLEO_F303K8) || \
+    defined(TARGET_NUCLEO_F334R8) || \
+    defined(TARGET_DISCO_F334C8)
+    
     // APB1 peripheral clock = 36000000Hz
 
     switch(hz) {
@@ -103,11 +155,62 @@
         printf("Unknown frequency specified!\r\n");
         printf("Using default 125kbps\r\n");
 #endif
+        _canHandle.Init.Prescaler = 30;     // number of time quanta = 45000000/30/125000 = 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_4TQ;
+    }
+    
+#elif defined(TARGET_NUCLEO_F446RE)
+    
+    // APB1 peripheral clock = 45000000Hz
+
+    switch(hz) {
+    case 1000000:
+        // 1000kbps bit rate
+        _canHandle.Init.Prescaler = 5;      // number of time quanta = 45000000/5/1000000 = 9
+        _canHandle.Init.SJW = CAN_SJW_1TQ;
+        _canHandle.Init.BS1 = CAN_BS1_6TQ;  // sample point at: (1 + 6) / 9 * 100 = 77.78%
+        _canHandle.Init.BS2 = CAN_BS2_2TQ;
+        break;
+
+    case 500000:
+        // 500kbps bit rate
+        _canHandle.Init.Prescaler = 10;      // number of time quanta = 45000000/10/500000 = 9
+        _canHandle.Init.SJW = CAN_SJW_1TQ;
+        _canHandle.Init.BS1 = CAN_BS1_6TQ;  // sample point at: (1 + 6) / 9 * 100 = 77.78%
+        _canHandle.Init.BS2 = CAN_BS2_2TQ;
+        break;
+
+    case 250000:
+        // 250kbps
+        _canHandle.Init.Prescaler = 15;      // number of time quanta = 45000000/15/250000 = 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 125000:
+        // 125kbps
+        _canHandle.Init.Prescaler = 30;     // number of time quanta = 45000000/30/125000 = 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;
+
+    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;
     }
+    
+#endif
 
     HAL_CAN_Init(&_canHandle);
     HAL_NVIC_EnableIRQ(CAN_IRQ);