Andrew Boyson / lpc1768

Dependents:   test-lpc1768 oldheating gps motorhome ... more

Files at this revision

API Documentation at this revision

Comitter:
andrewboyson
Date:
Fri Jun 03 11:51:13 2022 +0000
Parent:
70:3ae450c74c5e
Commit message:
Added can module

Changed in this revision

can/can.c Show annotated file Show diff for this revision Revisions of this file
can/can.h Show annotated file Show diff for this revision Revisions of this file
i2c/i2c1.c Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/can/can.c	Fri Jun 03 11:51:13 2022 +0000
@@ -0,0 +1,64 @@
+
+#include "log.h"
+#include "mstimer.h"
+
+#define AFMR      *((volatile unsigned *)0x4003C000) // Acceptance Filter Mode Register
+
+#define CAN2MOD   *((volatile unsigned *)0x40048000) // CAN Mode register
+#define CAN2CMR   *((volatile unsigned *)0x40048004) // CAN Command register
+#define CAN2GSR   *((volatile unsigned *)0x40048008) // CAN Global Status register
+#define CAN2ICR   *((volatile unsigned *)0x4004800C) // CAN Interrupt and Capture Register
+#define CAN2IER   *((volatile unsigned *)0x40048010) // CAN Interrupt Enable Register
+#define CAN2BTR   *((volatile unsigned *)0x40048014) // CAN Bus Timing Register
+#define CAN2TFI1  *((volatile unsigned *)0x40048030) // CAN Transmit Frame Information register 
+#define CAN2TID1  *((volatile unsigned *)0x40048034) // CAN Transmit Identifier Register
+#define CAN2TDA1  *((volatile unsigned *)0x40048038) // CAN Transmit Data Register A
+#define CAN2TDB1  *((volatile unsigned *)0x4004803C) // CAN Transmit Data Register B
+#define CAN2RFS   *((volatile unsigned *)0x40048020) // CAN Receive Frame Status register
+#define CAN2RID   *((volatile unsigned *)0x40048024) // CAN Receive Identifier register
+#define CAN2RDA   *((volatile unsigned *)0x40048028) // CAN Receive Data register A
+#define CAN2RDB   *((volatile unsigned *)0x4004802C) // CAN Receive Data register B
+
+void (*CanReceive)(uint16_t id, int length, uint32_t dataA, uint32_t dataB);
+void CanInit()
+{
+    LogTime("Can init started\r\n");
+    /*
+    00:   BRP = 95 : Clock is 96 / 4 = 24MHz and we want 1.000MHz so prescale by BRP+1 = 24: BRP = 23.
+    14:   SJW =  3 : 4 can clocks
+    16: TESG1 =  4 : 5 can clocks
+    20: TESG2 =  3 : 4 can clocks
+    */
+    CAN2BTR  = 0;
+    CAN2BTR |= 23 << 00;
+    CAN2BTR |=  3 << 14;
+    CAN2BTR |=  4 << 16;
+    CAN2BTR |=  3 << 20;
+    
+    AFMR |= 1 << 1; //Accept all messages
+    
+    CAN2MOD = 0; //Controller to operating mode
+}
+void CanSend(uint16_t id, int length, uint32_t dataA, uint32_t dataB)
+{
+    if (length > 8) length = 8;
+    CAN2CMR |= 1 << 5; //STB1 Select Tx Buffer 1
+    CAN2TFI1 = 0;
+    CAN2TFI1 |= length << 16; //DLC Data Length Code
+    CAN2TFI1 |= 0 << 30; //RTR Remote TRansmission
+    CAN2TFI1 |= 0 << 31; // FF Extended frame (0 = 11 bit id; 1 = 29 bit id)
+    CAN2TID1 = id;
+    CAN2TDA1 = dataA;
+    CAN2TDB1 = dataB;
+    CAN2CMR |= 1 << 0; //TR Transmission Request
+}
+void CanMain()
+{
+    if (CAN2GSR & 0x01) //RBS Receive Buffer Status - At least one complete message is  available in CANxRFS CANxRID CANxRDA CANxRDB
+    {
+        uint16_t id = CAN2RID & 0x3FF;
+        int      length = (CAN2RFS >> 16) & 0xF;
+        CanReceive(id, length, CAN2RDA, CAN2RDB);
+        CAN2CMR |= 1 << 2; //RRB Release Receive Buffer
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/can/can.h	Fri Jun 03 11:51:13 2022 +0000
@@ -0,0 +1,5 @@
+#include <stdint.h>
+extern void (*CanReceive)(uint16_t id, int length, uint32_t dataA, uint32_t dataB);
+extern void CanSend      (uint16_t id, int length, uint32_t dataA, uint32_t dataB);
+extern void CanInit(void);
+extern void CanMain(void);
\ No newline at end of file
--- a/i2c/i2c1.c	Tue May 18 16:25:19 2021 +0000
+++ b/i2c/i2c1.c	Fri Jun 03 11:51:13 2022 +0000
@@ -59,7 +59,7 @@
     
     if (I2C1Busy && MsTimerRelative(_msTimeOut, TIMEOUT_MS))
     {
-        LogTime("I2C1 -> Timeout\r\n");
+        if (I2C1Trace) LogTime("I2C1 -> Timeout\r\n");
         *_pResult  = I2C1_RESULT_TIMED_OUT;
         I2C1Busy   = false;
         I2C1CONSET = 0x10;          //Set STO bit
@@ -74,19 +74,19 @@
         {
             case 0x08: //Received START
             case 0x10: //Received a repeated START
-                if (I2C1Trace) { LogTimeF("I2C1 -> addr %02x + W\r\n", _addr); }
+                if (I2C1Trace) LogTimeF("I2C1 -> addr %02x + W\r\n", _addr);
                 I2C1DAT = (_addr << 1) | 0; //Send the address + W
                 I2C1CONCLR = 0x08;          //Clear S1
                 index = 0;
                 break;
             case 0x18: //received ACK after address
-                if (I2C1Trace) { LogTimeF("I2C1 -> data%d %02x\r\n", index, _pData[index]); }
+                if (I2C1Trace) LogTimeF("I2C1 -> data%d %02x\r\n", index, _pData[index]);
                 I2C1DAT = _pData[index];    //Load the data to be sent
                 I2C1CONCLR = 0x20;          //Reset the start bit
                 I2C1CONCLR = 0x08;          //Clear S1
                 break;
             case 0x20: //received NAK after address
-                if (I2C1Trace) { LogTime("I2C1 -> Stop\r\n"); }
+                if (I2C1Trace) LogTime("I2C1 -> Stop\r\n");
                 *_pResult  = I2C1_RESULT_FAILED;
                 I2C1Busy   = false;
                 I2C1CONSET = 0x10;          //Set STO bit
@@ -96,27 +96,27 @@
                 index++;
                 if (index >= _length)
                 {
-                    if (I2C1Trace) { LogTime("I2C1 -> Stop\r\n"); }
+                    if (I2C1Trace) LogTime("I2C1 -> Stop\r\n");
                     *_pResult  = I2C1_RESULT_OK;
                      I2C1Busy  = false;
                     I2C1CONSET = 0x10;      //Set STO bit
                 }
                 else
                 {
-                    if (I2C1Trace) { LogTimeF("I2C1 -> data%d %02x\r\n", index, _pData[index]); }
+                    if (I2C1Trace) LogTimeF("I2C1 -> data%d %02x\r\n", index, _pData[index]);
                     I2C1DAT = _pData[index];//Load the data to be sent
                 }
                 I2C1CONCLR = 0x08;          //Clear S1
                 break;
             case 0x30: //received NAK after data
-                if (I2C1Trace) { LogTime("I2C1 -> Stop\r\n"); }
+                if (I2C1Trace) LogTime("I2C1 -> Stop\r\n");
                 *_pResult  = I2C1_RESULT_FAILED;
                 I2C1Busy   = false;
                 I2C1CONSET = 0x10;          //Set STO bit
                 I2C1CONCLR = 0x08;          //Clear S1
                 break;
             case 0x38: //Arbitration lost
-                if (I2C1Trace) { LogTime("I2C1 -> Start\r\n"); }
+                if (I2C1Trace) LogTime("I2C1 -> Start\r\n");
                 *_pResult  = I2C1_RESULT_FAILED;
                 I2C1Busy   = false;
                 I2C1CONSET = 0x20;          //Set STA bit