etisserant@0: /* etisserant@0: This file is part of CanFestival, a library implementing CanOpen Stack. etisserant@0: etisserant@0: Copyright (C): Edouard TISSERANT and Francis DUPIN etisserant@0: etisserant@0: See COPYING file for copyrights details. etisserant@0: etisserant@0: This library is free software; you can redistribute it and/or etisserant@0: modify it under the terms of the GNU Lesser General Public etisserant@0: License as published by the Free Software Foundation; either etisserant@0: version 2.1 of the License, or (at your option) any later version. etisserant@0: etisserant@0: This library is distributed in the hope that it will be useful, etisserant@0: but WITHOUT ANY WARRANTY; without even the implied warranty of etisserant@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU etisserant@0: Lesser General Public License for more details. etisserant@0: etisserant@0: You should have received a copy of the GNU Lesser General Public etisserant@0: License along with this library; if not, write to the Free Software etisserant@0: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA etisserant@0: */ etisserant@0: etisserant@0: #ifndef __CANDRIVER__ etisserant@0: #define __CANDRIVER__ etisserant@0: etisserant@0: //#include DEBUG_CAN etisserant@0: etisserant@0: #include etisserant@0: #include etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: The CAN message received are stored in a fifo stack etisserant@0: We consider one stack for all the 5 can devices. It is a choice ! etisserant@0: */ etisserant@0: etisserant@0: /* Be free to change this value */ etisserant@0: #define MAX_STACK_MSG_RCV 5 etisserant@0: etisserant@0: /* Number of incomings and outcomings CAN Line. The layer CanOpen must be etisserant@0: used only for ONE line CAN. But you may used one or more CAN drivers, without etisserant@0: a CanOpen layer. etisserant@0: Only 2 lines are implemented. If more lines are needed, copy void __attribute__((interrupt)) can0HdlRcv (void) to void __attribute__((interrupt)) canXHdlRcv (void) and make etisserant@0: changes : [0] to [x], CAN0 to CANX, etc etisserant@0: */ etisserant@0: #define NB_LINE_CAN 1 etisserant@0: etisserant@0: /* Whose hardware HCS12 CAN block is used for CanOpen ? Chose between CAN0, ..., CAN4 etisserant@0: If you use CANOPEN_LINE_NUMBER_USED = CANI, the value of NB_LINE_CAN must be etisserant@0: more or equal to I + 1 etisserant@0: Value other than CAN0 not tested, but should work fine. etisserant@0: */ etisserant@0: #define CANOPEN_LINE_NUMBER_USED CAN0 etisserant@0: etisserant@0: /* Stack of received messages etisserant@0: MSG received on CAN0 module is stored in stackMsgRcv[0], etc etisserant@0: */ etisserant@0: extern volatile Message stackMsgRcv[NB_LINE_CAN][MAX_STACK_MSG_RCV]; etisserant@0: etisserant@0: etisserant@0: /* Copy from the stack of the message to treat */ etisserant@0: extern Message msgRcv; etisserant@0: etisserant@0: etisserant@0: /* To move on the stack of messages etisserant@0: */ etisserant@0: typedef struct { etisserant@0: UNS8 w ; /* received */ etisserant@0: UNS8 r ; /* To transmit */ etisserant@0: } t_pointerStack; etisserant@0: etisserant@0: etisserant@0: /* Pointer for write or read a message in/from the reception stack */ etisserant@0: extern volatile t_pointerStack ptrMsgRcv[NB_LINE_CAN]; etisserant@0: etisserant@0: /* etisserant@0: Parameters to define the clock system for the CAN bus etisserant@0: example : etisserant@0: CAN_BUS_TIME clk = { etisserant@0: 1, // clksrc: Use the bus clock : 16 MHz, the freq. of the quartz's board etisserant@0: 0, // brp : chose btw 0 and 63 (6 bits). freq time quantum = 16MHz / (brp + 1) etisserant@0: 1, // sjw : chose btw 0 and 3 (2 bits). Sync on (sjw + 1 ) time quantum etisserant@0: 1, // samp : chose btw 0 and 3 (2 bits) (samp + 1 ) samples per bit etisserant@0: 4, // tseg2 : chose btw 0 and 7 (3 bits) Segment 2 width = (tseg2 + 1) tq etisserant@0: 9, // tseg1 : chose btw 0 and 15 (4 bits) Segment 1 width = (tseg1 + 1) tq etisserant@0: etisserant@0: etisserant@0: With these values, etisserant@0: - The width of the bit time is 16 time quantum : etisserant@0: - 1 tq for the SYNC segment (could not be modified) etisserant@0: - 10 tq for the TIME 1 segment (tseg1 = 9) etisserant@0: - 5 tq for the TIME 2 segment (tseg2 = 4) etisserant@0: - Because the bus clock of the MSCAN is 16 MHZ, and the etisserant@0: freq of the time quantum is 16 MHZ (brp = 0), and there are 16 tq in the bit time, etisserant@0: so the freq of the bit time is 1 MHz. etisserant@0: etisserant@0: }; etisserant@0: */ etisserant@0: typedef struct { etisserant@0: UNS8 clksrc; /* use of internal clock or clock bus */ etisserant@0: UNS8 brp; /* define the bus speed */ etisserant@0: UNS8 sjw; /* Number of time quantum for synchro - 1 */ etisserant@0: UNS8 samp; /* Number of sample per bit (1 or 3) */ etisserant@0: UNS8 tseg2; /* Width of the time segment 2 (in tq) - 1 */ etisserant@0: UNS8 tseg1; /* Width of the time segment 1 (in tq) - 1 */ etisserant@0: } canBusTime; etisserant@0: etisserant@0: /* etisserant@0: Parameters to init the filters for received messages etisserant@0: */ etisserant@0: typedef struct { etisserant@0: UNS8 idam; /* Put 0x01 for 16 bits acceptance filter */ etisserant@0: UNS8 canidar0; etisserant@0: UNS8 canidmr0; etisserant@0: UNS8 canidar1; etisserant@0: UNS8 canidmr1; etisserant@0: UNS8 canidar2; etisserant@0: UNS8 canidmr2; etisserant@0: UNS8 canidar3; etisserant@0: UNS8 canidmr3; etisserant@0: UNS8 canidar4; etisserant@0: UNS8 canidmr4; etisserant@0: UNS8 canidar5; etisserant@0: UNS8 canidmr5; etisserant@0: UNS8 canidar6; etisserant@0: UNS8 canidmr6; etisserant@0: UNS8 canidar7; etisserant@0: UNS8 canidmr7; etisserant@0: } canBusFilterInit; etisserant@0: etisserant@0: /* etisserant@0: Parameters to init MSCAN etisserant@0: Example etisserant@0: CAN_BUS_INIT bi = { etisserant@0: 0, no low power etisserant@0: 0, no time stamp etisserant@0: 1, enable MSCAN etisserant@0: 0, clock source : oscillator etisserant@0: 0, no loop back etisserant@0: 0, no listen only etisserant@0: 0, no low pass filter for wk up etisserant@0: { etisserant@0: 1, Use the oscillator clock etisserant@0: 0, Quartz oscillator : freq time quantum = freq oscillator clock / (0 + 1) etisserant@0: 1, Sync on (1 + 1) time quantum etisserant@0: 1, 1 sample per bit etisserant@0: 4, time segment 2 width : (4 + 1) tq etisserant@0: 9, time segment 1 width : (9 + 1) tq etisserant@0: } etisserant@0: }; etisserant@0: */ etisserant@0: etisserant@0: typedef struct { etisserant@0: UNS8 cswai; /* Low power/normal in wait mode (1/0) */ etisserant@0: UNS8 time; /* Timer for time-stamp enable/disable (1/0) */ etisserant@0: UNS8 cane; /* Enable MSCAN (yes=1) Do it ! */ etisserant@0: UNS8 clksrc; /* clock source bus/oscillator (1/0) */ etisserant@0: UNS8 loopb; /* loop back mode for test (yes=1/no=0) */ etisserant@0: UNS8 listen; /* MSCAN is listen only (yes=1/no=0 ie normal)*/ etisserant@0: UNS8 wupm; /* low pas filter for wake up (yes=1/no=0) */ etisserant@0: canBusTime etisserant@0: clk; /* Values for clock system init */ etisserant@0: canBusFilterInit etisserant@0: fi; /* Values for filter acceptance msg init */ etisserant@0: etisserant@0: } canBusInit; etisserant@0: etisserant@0: extern canBusInit bi; etisserant@0: etisserant@0: dejoigny@7: etisserant@0: /* etisserant@0: For the received messsage, add a Identificator to etisserant@0: the list of ID to accept. etisserant@0: You can use several times this function to accept several messages. etisserant@0: It configures registers on 16 bits. etisserant@0: Automatically, it configure the filter to etisserant@0: - not accepting the msg on 29 bits (ide=1 refused) etisserant@0: - not filtering on rtr state (rtr = 1 and rtr = 0 are accepted) etisserant@0: Algo : etisserant@0: if CANIDARx = 0 then CANIDARx = id . else do nothing etisserant@0: CANIDMRx = CANIDMRx OR (CANIDARx XOR id ) etisserant@0: nFilter : 0 to 3 etisserant@0: Must be in init mode before. etisserant@0: */ etisserant@0: char canAddIdToFilter ( etisserant@0: UNS16 adrCAN, etisserant@0: UNS8 nFilter, etisserant@0: UNS16 id /* 11 bits, the 5 msb not used */ etisserant@0: ); etisserant@0: etisserant@0: /* dejoigny@7: Use this function to change the CAN message acceptance filters and masks. dejoigny@7: */ dejoigny@7: char canChangeFilter (UNS16 adrCAN, canBusFilterInit fi); dejoigny@7: dejoigny@7: dejoigny@7: /* etisserant@0: Enable one of the 5 MSCAN. etisserant@0: Must be done only one time after a reset of the CPU. etisserant@0: To do before any CAN initialisation etisserant@0: */ etisserant@0: char canEnable ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: Initialize one of the 5 mscan etisserant@0: can be done multiple times in your code etisserant@0: Return 0 : OK etisserant@0: When it return from the function, etisserant@0: mscan is on sleep mode with wake up disabled. etisserant@0: is not on init mode etisserant@0: */ etisserant@0: char canInit ( etisserant@0: UNS16 adrCAN, /* First address of MSCANx registers */ etisserant@0: canBusInit etisserant@0: bi /* All the parameters to init the bus */ etisserant@0: ); etisserant@0: /* etisserant@0: Initialize the parameters of the system clock for the MSCAN etisserant@0: You must put the MSCAN in sleep mode before with canSleepMode() etisserant@0: Return 0 : OK etisserant@0: 1 : Not in sleep mode. Unable to init MSCAN etisserant@0: */ etisserant@0: char canInitClock ( etisserant@0: UNS16 adrCAN, /* First address of MSCANx registers */ etisserant@0: canBusTime clk); etisserant@0: etisserant@0: /* etisserant@0: Initialize one filter for acceptance of received msg. etisserant@0: Filters MUST be configured on 16 bits etisserant@0: (See doc Motorola mscan bloc guide fig 4.3) etisserant@0: Must be in init mode before. etisserant@0: adrCAN : adress of the first register of the mscan module etisserant@0: nFilter : the filter : 0 to 3. etisserant@0: ar : Value to write in acceptance register etisserant@0: Beware ! hight byte and low byte inverted. etisserant@0: for example if nFilter = 0, hight byte of ar -> CANIDAR0 etisserant@0: low byte of ar -> CANIDAR1 etisserant@0: mr : Value to write in mask register etisserant@0: Beware ! hight byte and low byte inverted. etisserant@0: for example if nFilter = 2, hight byte of ar -> CANIDMR4 etisserant@0: low byte of ar -> CANIDMR5 etisserant@0: */ etisserant@0: char canInit1Filter ( etisserant@0: UNS16 adrCAN, etisserant@0: UNS8 nFilter, etisserant@0: UNS16 ar, etisserant@0: UNS16 mr etisserant@0: ); etisserant@0: etisserant@0: /* etisserant@0: Initialise the parameters for filtering the messages received. etisserant@0: You must put the MSCAN in init mode before with canInitMode() etisserant@0: Return 0 : OK etisserant@0: 1 : Not in init mode. Unable to init MSCAN etisserant@0: */ etisserant@0: etisserant@0: char canInitFilter ( etisserant@0: UNS16 adrCAN, /* First address of MSCANx registers */ etisserant@0: canBusFilterInit fi); etisserant@0: /* etisserant@0: Put one of the 5 mscan in Init mode etisserant@0: Loop until init mode is reached. etisserant@0: */ etisserant@0: etisserant@0: char canInitMode ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: /* etisserant@0: Leave the Init mode etisserant@0: loop until init mode leaved. etisserant@0: */ etisserant@0: etisserant@0: char canInitModeQ ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: etisserant@0: etisserant@0: /* etisserant@0: Transmit a msg on CAN "adrCan" etisserant@0: Return : 0 No error etisserant@0: other error : no buffer available to make the transmission etisserant@0: */ etisserant@0: etisserant@0: char canMsgTransmit ( etisserant@0: UNS16 adrCAN, /* First address of MSCANx registers */ etisserant@0: Message msg /* Message to transmit */ etisserant@0: ); etisserant@0: etisserant@0: /* dejoigny@7: Set the interruptions. Must be call just after having left the init mode. dejoigny@7: */ dejoigny@7: char canSetInterrupt (UNS16 adrCAN); dejoigny@7: dejoigny@7: /* etisserant@0: Put one of the 5 mscan in sleep mode etisserant@0: Beware! If some messages are to be sent, etisserant@0: or if it is receiving, going into sleep mode etisserant@0: may take time. etisserant@0: Wake up is disabled : stay in sleep mode even if etisserant@0: bus traffic detected. etisserant@0: return 0 if 0K, other if error : mscan is on init mode. etisserant@0: Stay in this function until the sleep mode etisserant@0: is reached. etisserant@0: */ etisserant@0: char canSleepMode ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: /* etisserant@0: Leave the sleep mode etisserant@0: loop until sleep mode leaved. etisserant@0: return 0 : OK etisserant@0: return 1 : error : in init mode etisserant@0: */ etisserant@0: char canSleepModeQ ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: /* etisserant@0: Put one of the 5 mscan in sleep mode etisserant@0: MSCAN must not be in init mode. etisserant@0: wake up is enabled : wake up if traffic on CAN is detected etisserant@0: Beware! If some messages are to be sent, etisserant@0: or if it is receiving, going into sleep mode etisserant@0: may take time. etisserant@0: Loop until sleep mode reached. etisserant@0: return 0 if 0K, other if error etisserant@0: */ etisserant@0: char canSleepWupMode ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: /* etisserant@0: Test if one of the 5 mscan is in init mode. etisserant@0: Return etisserant@0: 0 -> Not in init mode etisserant@0: other -> In init mode etisserant@0: */ etisserant@0: char canTestInitMode ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: /* etisserant@0: Test if one of the 5 mscan is in sleep mode. etisserant@0: Return etisserant@0: 0 -> Not in sleep mode etisserant@0: other -> In sleep mode etisserant@0: */ etisserant@0: char canTestSleepMode ( etisserant@0: UNS16 adrCAN /* First address of MSCANx registers */ etisserant@0: ); etisserant@0: etisserant@0: etisserant@0: etisserant@0: #endif /*__CANDRIVER__*/ etisserant@0: