Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: canutils.cpp
- Revision:
- 5:1775b4b13232
- Parent:
- 4:682d96ff6d79
--- a/canutils.cpp Wed Sep 11 11:55:51 2013 +0000
+++ b/canutils.cpp Sat Apr 25 17:07:08 2015 +0000
@@ -26,17 +26,20 @@
//LPC_CANx->MOD |= (1 << 1); // Put into listen only mode
//LPC_CANx->MOD &= ~(1); // Re-enable CAN controller
-void can_disable(uint8_t chan) {
+void can_disable(uint8_t chan)
+{
// Put a CAN controller into disabled condition
chan == 1 ? LPC_CAN1->MOD |= 1 : LPC_CAN2->MOD |= 1;
}
-void can_enable(uint8_t chan) {
+void can_enable(uint8_t chan)
+{
// Put a CAN controller in operating mode
chan == 1 ? LPC_CAN1->MOD &= ~(1) : LPC_CAN2->MOD &= ~(1);
}
-void can_configure(uint8_t chan, uint32_t baud, bool listen) {
+void can_configure(uint8_t chan, uint32_t baud, bool listen)
+{
LPC_CAN_TypeDef *pCANx = (chan == 1) ? LPC_CAN1 : LPC_CAN2;
@@ -92,7 +95,8 @@
}
-void can_reset_filters() {
+void can_reset_filters()
+{
// Initialise the Acceptance Filters
LPC_CANAF->AFMR = 0x01; // Put Acceptance Filter into reset/configuration mode
for (uint16_t i = 0; i < 512; i++)
@@ -105,7 +109,8 @@
LPC_CANAF->AFMR = 0x00; // Enable Acceptance Filter all messages should be rejected
}
-void can_use_filters(bool active) {
+void can_use_filters(bool active)
+{
active ? LPC_CANAF->AFMR = 0 : LPC_CANAF->AFMR = 2;
}
@@ -114,7 +119,8 @@
original http://www.dragonwake.com/download/LPC1768/Example/CAN/CAN.c
simplified for CAN2 interface and std id (11 bit) only
*--------------------------------------------*/
-void can_add_filter(uint8_t chan, uint32_t id) {
+void can_add_filter(uint8_t chan, uint32_t id)
+{
static int CAN_std_cnt = 0;
uint32_t buf0, buf1;
@@ -197,29 +203,34 @@
} // CAN2_wrFilter
-void can_open() {
+void can_open()
+{
// activate external can transceiver
can.reset();
can_rs_pin = 0;
}
-void can_close() {
+void can_close()
+{
// disable external can transceiver
can_rs_pin = 1;
can.reset();
}
-void can_monitor() {
+void can_monitor()
+{
// Put CAN into silent monitoring mode
can.monitor(1);
}
-void can_active() {
+void can_active()
+{
// Take CAN out of silent monitoring mode
can.monitor(0);
}
-uint8_t can_set_speed(uint32_t speed) {
+uint8_t can_set_speed(uint32_t speed)
+{
// 600kbit/s first - basically sets up CAN interface, but to wrong speed - not sure what else it does
// can.frequency(600000);
// 615kbit/s direct write of 615 kbit/s speed setting
@@ -235,7 +246,8 @@
// inputs: none
// return: bool TRUE if there was a message, FALSE if no message.
//
-extern void show_can_message() {
+extern void show_can_message()
+{
CANMessage can_MsgRx;
if (can.read(can_MsgRx)) {
CANRXLEDON;
@@ -256,7 +268,8 @@
// inputs: none
// return: bool TRUE if there was a message, FALSE if no message.
//
-extern void show_T5can_message() {
+extern void show_T5can_message()
+{
CANMessage can_MsgRx;
if (can.read(can_MsgRx)) {
CANRXLEDON;
@@ -282,7 +295,8 @@
// inputs: none
// return: bool TRUE if there was a message, FALSE if no message.
//
-extern void show_T7can_message() {
+extern void show_T7can_message()
+{
CANMessage can_MsgRx;
if (can.read(can_MsgRx)) {
CANRXLEDON;
@@ -324,7 +338,8 @@
// inputs: none
// return: bool TRUE if there was a message, FALSE if no message.
//
-extern void show_T8can_message() {
+extern void show_T8can_message()
+{
CANMessage can_MsgRx;
if (can.read(can_MsgRx)) {
CANRXLEDON;
@@ -354,7 +369,8 @@
// inputs: none
// return: bool TRUE if there was a message, FALSE if no message.
//
-extern void silent_can_message() {
+extern void silent_can_message()
+{
CANMessage can_MsgRx;
if (can.read(can_MsgRx)) {
CANRXLEDON;
@@ -369,9 +385,17 @@
// return: TRUE if the CAN message was sent before the 'timeout' expires
// FALSE if 'timeout' expires or the message length is wrong
//
-extern bool can_send_timeout (uint32_t id, char *frame, uint8_t len, uint16_t timeout) {
+extern bool can_send_timeout (uint32_t id, char *frame, uint8_t len, uint16_t timeout)
+{
CANTimer.reset();
CANTimer.start();
+#ifdef DEBUG
+ printf("ID:%03x Len:%03x", id, len);
+ for (char i=0; i<len; i++) {
+ printf(" %02x", frame[i]);
+ }
+ printf("\n\r");
+#endif
while (CANTimer.read_ms() < timeout) {
if (can.write(CANMessage(id, frame, len))) {
CANTimer.stop();
@@ -396,24 +420,25 @@
// return: TRUE if a qualifying message was received
// FALSE if 'timeout' expires or the message length is wrong
//
-extern bool can_wait_timeout (uint32_t id, char *frame, uint8_t len, uint16_t timeout) {
+extern bool can_wait_timeout (uint32_t id, char *frame, uint8_t len, uint16_t timeout)
+{
CANMessage CANMsgRx;
CANTimer.reset();
CANTimer.start();
while (CANTimer.read_ms() < timeout) {
if (can.read(CANMsgRx)) {
- /*
- printf("w%03x8", CANMsgRx.id);
- for (char i=0; i<len; i++) {
- printf("%02x", CANMsgRx.data[i]);
- }
- printf("\n\r");
- // */
+#ifdef DEBUG
+ printf("ID:%03x Len:%03x", CANMsgRx.id, CANMsgRx.len);
+ for (char i=0; i<len; i++) {
+ printf(" %02x", CANMsgRx.data[i]);
+ }
+ printf("\n\r");
+#endif
CANRXLEDON;
// led2 = 1;
if (CANMsgRx.id == id || id ==0) {
CANTimer.stop();
-// if (T5MsgRx.len != len)
+// if (CANMsgRx.len != len)
// return FALSE;
for (int i=0; i<len; i++)
frame[i] = CANMsgRx.data[i];