/* Set Bus Timing */ STATIC void setBusTiming(LPC_CAN_T *pCAN, IP_CAN_BUS_TIMING_T *pBusTiming) { /* Enter to Reset Mode */ Chip_CAN_SetMode(pCAN, CAN_RESET_MODE, ENABLE); /* Set Bus Timing */ pCAN->BTR = CAN_BTR_BRP(pBusTiming->BRP) | CAN_BTR_SJW(pBusTiming->SJW) | CAN_BTR_TESG1(pBusTiming->TESG1) | CAN_BTR_TESG2(pBusTiming->TESG2); if (pBusTiming->SAM) { pCAN->BTR |= CAN_BTR_SAM; } /* Exit from Reset Mode */ Chip_CAN_SetMode(pCAN, CAN_RESET_MODE, DISABLE); }
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ #include "ch.h" #include "hal.h" /* * Internal loopback mode, 500KBaud, automatic wakeup, automatic recover * from abort mode. */ static const CANConfig cancfg = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, CAN_BTR_LBKM | CAN_BTR_SJW(0) | CAN_BTR_TS2(1) | CAN_BTR_TS1(8) | CAN_BTR_BRP(6) }; /* * Receiver thread. */ static THD_WORKING_AREA(can_rx_wa, 256); static THD_FUNCTION(can_rx, p) { event_listener_t el; CANRxFrame rxmsg; (void)p; chRegSetThreadName("receiver"); chEvtRegister(&CAND1.rxfull_event, &el, 0); while(!chThdShouldTerminateX()) { if (chEvtWaitAnyTimeout(ALL_EVENTS, MS2ST(100)) == 0)
struct can_instance { CANDriver *canp; ioline_t led; }; static const struct can_instance can1 = {&CAND1, LINE_LED_GREEN}; /* * Internal loopback mode, 500KBaud, automatic wakeup, automatic recover * from abort mode. * See section 42.7.7 on the STM32 reference manual. */ static const CANConfig cancfg = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, CAN_BTR_LBKM | CAN_BTR_SJW(0) | CAN_BTR_TS2(1) | CAN_BTR_TS1(12) | CAN_BTR_BRP(9) }; /* * Receiver thread. */ static THD_WORKING_AREA(can_rx1_wa, 256); static THD_FUNCTION(can_rx, p) { struct can_instance *cip = p; event_listener_t el; CANRxFrame rxmsg; (void)p; chRegSetThreadName("receiver"); chEvtRegister(&cip->canp->rxfull_event, &el, 0); while(!chThdShouldTerminateX()) {
static THD_WORKING_AREA(canTreadStack, UTILITY_THREAD_STACK_SIZE); /* * 500KBaud * automatic wakeup * automatic recover from abort mode * See section 22.7.7 on the STM32 reference manual. * * speed = 42000000 / (BRP + 1) / (1 + TS1 + 1 + TS2 + 1) * 42000000 / 7 / 12 = 500000 * * */ static const CANConfig canConfig = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, CAN_BTR_SJW(0) | CAN_BTR_TS2(1) | CAN_BTR_TS1(8) | CAN_BTR_BRP(6) }; static CANRxFrame rxBuffer; CANTxFrame txmsg; // todo: we would need a data structure here static int engine_rpm = 0; static float engine_clt = 0; static int rand = 1212321311; //static CANDriver *getCanDevice() { // if(board) //} static void printPacket(CANRxFrame *rx) {
50, /* Initial PWM period 10mS. */ NULL, { {PWM_OUTPUT_ACTIVE_HIGH, NULL}, {PWM_OUTPUT_ACTIVE_HIGH, NULL}, {PWM_OUTPUT_DISABLED, NULL}, {PWM_OUTPUT_DISABLED, NULL} }, 0, 0 }; const CANConfig cancfg = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, CAN_BTR_SJW(1) | CAN_BTR_TS2(4) | CAN_BTR_TS1(5) | CAN_BTR_BRP(5) }; /*===========================================================================*/ /* Threads */ /*===========================================================================*/ THD_WORKING_AREA(waThreadCAN, 256); CCM_FUNC static THD_FUNCTION(ThreadCAN, arg) { event_listener_t el; CANTxFrame txmsg; CANRxFrame rxmsg; uint16_t i; bool pidserved;
*/ /* ****************************************************************************** * GLOBAL VARIABLES ****************************************************************************** */ /* * Internal loopback mode, 250KBaud, automatic wakeup, automatic recover * from abort mode. * See section 22.7.7 on the STM32 reference manual. */ static const CANConfig cancfg = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, //CAN_BTR_LBKM | CAN_BTR_SJW(1) | CAN_BTR_TS2(2) | CAN_BTR_TS1(11) | CAN_BTR_BRP(5), CAN_BTR_SJW(1) | CAN_BTR_TS2(2) | CAN_BTR_TS1(11) | CAN_BTR_BRP(5), 0, NULL }; static Thread *can_tp; /* ****************************************************************************** ****************************************************************************** * LOCAL FUNCTIONS ****************************************************************************** ****************************************************************************** */ /* * Transmitter thread.
#include "chprintf.h" #include <pb_encode.h> #include "protocols.pb.h" /* * 500KBaud, automatic wakeup, automatic recover * from abort mode. * See: http://www.chibios.com/forum/viewtopic.php?t=1307 * baudrate=Fclk/((1+BRP)*(3+ts1+ts2)) and APB1 runs at 36MHz * 500 000 = (36*10^6)/((1 + 5) * (3 + 5 + 4)) */ static const CANConfig cancfg = { CAN_MCR_ABOM | CAN_MCR_AWUM | CAN_MCR_TXFP, CAN_BTR_SJW(0) | CAN_BTR_TS2(4) | CAN_BTR_TS1(5) | CAN_BTR_BRP(5) | CAN_BTR_LBKM | CAN_BTR_SILM //loopback and silent mode enabled }; // The loopback and silent mode fully disconnect the CAN transceiver static const uint8_t buf[] = "Ping\n\r"; /* * Receiver thread. */ static THD_WORKING_AREA(can_rx_wa, 256); static THD_FUNCTION(can_rx, p) { event_listener_t el; CANRxFrame rxmsg; (void)p;