Skip to content

Commit

Permalink
Baudrate
Browse files Browse the repository at this point in the history
Make CAN baudrate settable in the canInit function
  • Loading branch information
JamieBlakey committed Feb 21, 2018
1 parent 0396c14 commit 75ee6db
Show file tree
Hide file tree
Showing 8 changed files with 376 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ st_cmd_t Msg;
uint8_t Buffer[8] = {};

void setup() {
canInit(); // Initialise CAN port. must be before Serial.begin
canInit(500000); // Initialise CAN port. must be before Serial.begin
Serial.begin(1000000); // start serial port
Msg.pt_data = &Buffer[0]; // reference message data to buffer

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const uint8_t sendData[8] = {0,10,20,40,80,100,120,127};
uint8_t txBuffer[8] = {};

void setup() {
canInit(); // Initialise CAN port. must be before Serial.begin
canInit(500000); // Initialise CAN port. must be before Serial.begin
Serial.begin(1000000); // start serial port
txMsg.pt_data = &txBuffer[0]; // reference message data to transmit buffer
}
Expand Down
4 changes: 2 additions & 2 deletions Libraries/Arduino/AST_CAN/src/ASTCanLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

#include "ASTCanLib.h"

void canInit(void){
void canInit(long baud){
// Clock prescaler
CLKPR = ( 1 << CLKPCE ); // Set Clock Prescaler change enable
CLKPR = 0x00;
Expand All @@ -23,7 +23,7 @@ void canInit(void){
PORTB &= ~(1<<PORTB1); // RST Led
PORTB &= ~(1<<PORTB4); // Go into normal mode (CAN_STBY low)
_delay_ms(50);
can_init(0);
can_init(0,baud);
}

void clearBuffer(uint8_t *Buffer){
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Arduino/AST_CAN/src/ASTCanLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ extern "C"{
#include <can_lib.h>
}

void canInit(void);
void canInit(long baud);
void clearBuffer(uint8_t *Buffer);

#endif
416 changes: 208 additions & 208 deletions Libraries/Arduino/AST_CAN/src/can_drv.c

Large diffs are not rendered by default.

234 changes: 117 additions & 117 deletions Libraries/Arduino/AST_CAN/src/can_drv.h

Large diffs are not rendered by default.

50 changes: 45 additions & 5 deletions Libraries/Arduino/AST_CAN/src/can_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,52 @@
//! ==1: baudrate performed
//!
//------------------------------------------------------------------------------
U8 can_init(U8 mode)
// U8 can_init(U8 mode)
// {
// if ((Can_bit_timing(mode))==0) return (0); // c.f. macro in "can_drv.h"
// can_clear_all_mob(); // c.f. function in "can_drv.c"
// Can_enable(); // c.f. macro in "can_drv.h"
// return (1);
// }
U8 can_init(U8 mode,long baud)
{
if ((Can_bit_timing(mode))==0) return (0); // c.f. macro in "can_drv.h"
can_clear_all_mob(); // c.f. function in "can_drv.c"
Can_enable(); // c.f. macro in "can_drv.h"
return (1);
switch(baud){
case 100000:
CANBT1 = 0x12;
CANBT2 = 0x0C;
CANBT3 = 0x37;
break;
case 125000:
CANBT1 = 0x0E;
CANBT2 = 0x0C;
CANBT3 = 0x37;
break;
case 200000:
CANBT1 = 0x08;
CANBT2 = 0x0C;
CANBT3 = 0x37;
break;
case 250000:
CANBT1 = 0x06;
CANBT2 = 0x0C;
CANBT3 = 0x37;
break;
case 500000:
CANBT1 = 0x06;
CANBT2 = 0x04;
CANBT3 = 0x13;
break;
case 1000000:
CANBT1 = 0x02;
CANBT2 = 0x04;
CANBT3 = 0x13;
break;
default:
return(0);
}
can_clear_all_mob(); // c.f. function in "can_drv.c"
Can_enable(); // c.f. macro in "can_drv.h"
return (1);
}

//------------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion Libraries/Arduino/AST_CAN/src/can_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ typedef struct{
//! ==0: research of bit timing configuration failed
//! ==1: baudrate performed
//!
extern U8 can_init(U8 mode);
extern U8 can_init(U8 mode,long baud);

//------------------------------------------------------------------------------
// @fn can_cmd
Expand Down

0 comments on commit 75ee6db

Please sign in to comment.