1 #include "can/lpc17xx_can.h"
3 static void CAN_configPin();
4 static void CAN_setBusTiming(struct canchip_t *chip);
6 extern struct canhardware_t *hardware_p;
8 //---------------------------------------------------------------------------------
9 //---------------------------------------------------------------------------------
12 static inline void can_write_register(struct canchip_t *chip, uint32_t data, uint32_t reg_offs){
14 uint32_t address = chip->chip_base_addr + reg_offs;
15 (*(volatile uint32_t*)(address)) = data;
19 static inline uint32_t can_read_register(struct canchip_t *chip, uint32_t reg_offs){
21 uint32_t address = chip->chip_base_addr + reg_offs;
22 return (*(volatile uint32_t*)(address));
26 //---------------------------------------------------------------------------------
27 //---------------------------------------------------------------------------------
31 // for transmitting - irq when one message was transmitted (for check if another message is pending in can msg queue)
32 // for receiving - irq when message was received and is available in Receive buffer
34 void CAN_IRQHandler(){
38 struct canchip_t *chip;
39 chip = hardware_p->candevice[0]->chip[0];
41 i = can_read_register(chip, CAN_ICR_o);
43 if(i & (CAN_ICR_TI1 | CAN_ICR_RI))
44 lpc17xx_irq_handler(0, chip);
47 can_write_register(chip, CAN_CMR_CDO, CAN_CMR_o);
52 //---------------------------------------------------------------------------------
53 //---------------------------------------------------------------------------------
56 // board can-lmc1 specific functions:
58 int can_lmc1_register(struct hwspecops_t *hwspecops){
60 hwspecops->request_io = can_lmc1_request_io;
61 hwspecops->reset = can_lmc1_reset;
62 hwspecops->init_hw_data = can_lmc1_init_hw_data;
63 hwspecops->init_chip_data = can_lmc1_init_chip_data;
64 hwspecops->init_obj_data = can_lmc1_init_obj_data;
65 hwspecops->write_register = can_lmc1_write_register;
66 hwspecops->read_register = can_lmc1_read_register;
71 int can_lmc1_init_hw_data(struct candevice_t *candev){
74 candev->nr_82527_chips=0;
75 candev->nr_sja1000_chips=0;
76 candev->nr_all_chips=1;
82 int can_lmc1_init_chip_data(struct candevice_t *candev, int chipnr){
84 lpc17xx_fill_chipspecops(candev->chip[chipnr]);
86 candev->chip[chipnr]->flags|= CHIP_IRQ_CUSTOM;
89 candev->chip[chipnr]->chip_base_addr = CAN1_REGS_BASE;
91 candev->chip[chipnr]->chip_data=(void *)malloc(sizeof(struct can_lmc1_chip_data));
92 if (candev->chip[chipnr]->chip_data==NULL)
98 int can_lmc1_init_obj_data(struct canchip_t *chip, int objnr){
103 void can_lmc1_write_register(unsigned data, unsigned long address){
105 printf("UNSUPPORTED NOW\n");
108 unsigned can_lmc1_read_register(unsigned long address){
110 printf("UNSUPPORTED NOW\n");
115 int can_lmc1_request_io(struct candevice_t *candev)
120 int can_lmc1_reset(struct candevice_t *candev)
126 //---------------------------------------------------------------------------------
127 //---------------------------------------------------------------------------------
130 // lpc17xx can chip specific functions:
133 int lpc17xx_chip_config(struct canchip_t *chip){
140 int lpc17xx_pre_write_config(struct canchip_t *chip, struct msgobj_t *obj,
141 struct canmsg_t *msg)
149 int lpc17xx_send_msg(struct canchip_t *chip, struct msgobj_t *obj,
150 struct canmsg_t *msg)
153 // write transmission request
154 can_write_register(chip, (CAN_CMR_TR | CAN_CMR_STB1), CAN_CMR_o);
159 int lpc17xx_wakeup_tx(struct canchip_t *chip, struct msgobj_t *obj)
162 can_preempt_disable();
164 can_msgobj_set_fl(obj,TX_PENDING);
165 can_msgobj_set_fl(obj,TX_REQUEST);
167 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
168 can_msgobj_clear_fl(obj,TX_REQUEST);
170 if (can_read_register(chip, CAN_SR_o) & CAN_SR_TBS1){
172 lpc17xx_irq_write_handler(chip, obj);
175 can_msgobj_clear_fl(obj,TX_LOCK);
176 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
180 can_preempt_enable();
185 int lpc17xx_irq_handler(int irq, struct canchip_t *chip)
188 struct msgobj_t *obj;
189 obj = chip->msgobj[0];
191 if(can_read_register(chip, CAN_SR_o) & CAN_SR_RBS) {
192 lpc17xx_read(chip,obj);
197 if ((can_msgobj_test_fl(obj,TX_PENDING)) || (can_msgobj_test_fl(obj,TX_REQUEST))) {
199 can_msgobj_set_fl(obj,TX_REQUEST);
201 while(!can_msgobj_test_and_set_fl(obj,TX_LOCK)){
204 can_msgobj_clear_fl(obj,TX_REQUEST);
206 if (can_read_register(chip, CAN_SR_o) & CAN_SR_TBS1){
208 lpc17xx_irq_write_handler(chip, obj);
211 can_msgobj_clear_fl(obj,TX_LOCK);
212 if(!can_msgobj_test_fl(obj,TX_REQUEST)) break;
218 return CANCHIP_IRQ_HANDLED;
222 void lpc17xx_irq_write_handler(struct canchip_t *chip, struct msgobj_t *obj)
227 /* Do local transmitted message distribution if enabled */
229 /* fill CAN message timestamp */
230 can_filltimestamp(&obj->tx_slot->msg.timestamp);
232 obj->tx_slot->msg.flags |= MSG_LOCAL;
233 canque_filter_msg2edges(obj->qends, &obj->tx_slot->msg);
235 /* Free transmitted slot */
236 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
240 can_msgobj_clear_fl(obj,TX_PENDING);
241 cmd=canque_test_outslot(obj->qends, &obj->tx_qedge, &obj->tx_slot);
244 can_msgobj_set_fl(obj,TX_PENDING);
246 if (chip->chipspecops->pre_write_config(chip, obj, &obj->tx_slot->msg)) {
248 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_PREP);
249 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
253 if (chip->chipspecops->send_msg(chip, obj, &obj->tx_slot->msg)) {
255 canque_notify_inends(obj->tx_qedge, CANQUEUE_NOTIFY_ERRTX_SEND);
256 canque_free_outslot(obj->qends, obj->tx_qedge, obj->tx_slot);
263 void lpc17xx_read(struct canchip_t *chip, struct msgobj_t *obj) {
266 CAN_recv(chip, &obj->rx_msg);
268 // fill CAN message timestamp
269 can_filltimestamp(&obj->rx_msg.timestamp);
271 canque_filter_msg2edges(obj->qends, &obj->rx_msg);
273 // release Receive buffer
274 can_write_register(chip, CAN_CMR_RRB, CAN_CMR_o);
278 int lpc17xx_fill_chipspecops(struct canchip_t *chip){
282 lpc17xx_register(chip->chipspecops);
287 int lpc17xx_register(struct chipspecops_t *chipspecops){
289 CANMSG("initializing lpc17xx can chip operations\n");
291 chipspecops->attach_to_chip = lpc17xx_attach_to_chip;
292 chipspecops->pre_read_config = lpc17xx_pre_read_config;
293 chipspecops->chip_config = lpc17xx_chip_config;
294 chipspecops->pre_write_config = lpc17xx_pre_write_config;
295 chipspecops->send_msg = lpc17xx_send_msg;
296 chipspecops->wakeup_tx = lpc17xx_wakeup_tx;
297 chipspecops->irq_handler = lpc17xx_irq_handler;
304 int lpc17xx_attach_to_chip(struct canchip_t *chip){
309 int lpc17xx_pre_read_config(struct canchip_t *chip, struct msgobj_t *obj){
316 //---------------------------------------------------------------------------------
317 //---------------------------------------------------------------------------------
320 static void CAN_configPin(){
322 // CAN1 - select P0.0 as RD1. P0.1 as TD1
326 uint32_t pinmode_od0;
327 uint8_t pinsel0_val = 1;
328 uint8_t pinmode0_val = 0;
329 uint8_t pinmode_od0_val = 0;
332 pinsel0 = PINCON->PINSEL0;
333 pinsel0 &= ~CAN1_RX_MASK;
334 pinsel0 &= ~CAN1_TX_MASK;
335 pinsel0 |= __val2mfld(CAN1_RX_MASK, pinsel0_val);
336 pinsel0 |= __val2mfld(CAN1_TX_MASK, pinsel0_val);
337 PINCON->PINSEL0 = pinsel0;
339 pinmode0 = PINCON->PINMODE0;
340 pinmode0 &= ~CAN1_RX_MASK;
341 pinmode0 &= ~CAN1_TX_MASK;
342 pinmode0 |= __val2mfld(CAN1_RX_MASK, pinmode0_val);
343 pinmode0 |= __val2mfld(CAN1_TX_MASK, pinmode0_val);
344 PINCON->PINMODE0 = pinmode0;
346 pinmode_od0 = PINCON->PINMODE_OD0;
347 if (pinmode_od0_val){
348 pinmode_od0 |= CAN1_RX_BIT;
349 pinmode_od0 |= CAN1_TX_BIT;
352 pinmode_od0 &= ~CAN1_RX_BIT;
353 pinmode_od0 &= ~CAN1_TX_BIT;
355 PINCON->PINMODE_OD0 = pinmode_od0;
360 void CAN_recv(struct canchip_t *chip, canmsg_t* msg){
362 volatile uint32_t data;
366 msg->length = (can_read_register(chip, CAN_RFS_o)>>16) & 0xF;
369 msg->id = can_read_register(chip, CAN_RID_o);
372 if(can_read_register(chip, CAN_RFS_o) & CAN_RFS_EXT)
373 msg->flags |= MSG_EXT;
375 msg->flags &= ~MSG_EXT;
379 if(can_read_register(chip, CAN_RFS_o) & CAN_RFS_RTR)
380 msg->flags |= MSG_RTR;
382 msg->flags &= ~MSG_RTR;
386 data = can_read_register(chip, CAN_RDA_o);
388 msg->data[i] = (data>>(i*8)) & 0xFF;
390 data = can_read_register(chip, CAN_RDB_o);
392 msg->data[i] = (data>>((i-4)*8)) & 0xFF;
396 void CAN_send(struct canchip_t *chip, canmsg_t* msg){
398 volatile uint32_t data;
399 volatile uint32_t can_tfi1;
402 // check status of TB1
403 while (!(can_read_register(chip, CAN_SR_o) & CAN_SR_TBS1)){}
405 can_tfi1 = can_read_register(chip, CAN_TFI1_o);
407 can_tfi1 &= ~0x000F0000;
408 can_tfi1 |= (msg->length)<<16;
411 if(msg->flags & MSG_EXT)
412 can_tfi1 |= CAN_TFI1_EXT;
414 can_tfi1 &= ~CAN_TFI1_EXT;
417 if(msg->flags & MSG_RTR)
418 can_tfi1 |= CAN_TFI1_RTR;
420 can_tfi1 &= ~CAN_TFI1_RTR;
422 can_write_register(chip, can_tfi1, CAN_TFI1_o);
426 can_write_register(chip, msg->id, CAN_TID1_o);
429 // write first 4 data bytes
432 data |= (msg->data[i])<<(i*8);
434 can_write_register(chip, data, CAN_TDA1_o);
436 // write second 4 data bytes
439 data |= (msg->data[i])<<((i-4)*8);
441 can_write_register(chip, data, CAN_TDB1_o);
445 void CAN_setBusTiming(struct canchip_t *chip){
450 uint8_t tq_numb; // number of time quantum
451 uint8_t TSEG1, TSEG2;
457 // 0 = the bus is sampled once
460 // the Synchronization Jump Width (this value plus one)
463 // get clock divide for CAN1
464 div = __mfld2val(PCLK_CAN1_MASK, SC->PCLKSEL0);
476 // only for CAN, for other peripherials '11' value means div=8
482 PCLK_CAN = system_frequency / div;
484 res = PCLK_CAN / (chip->baudrate);
487 // calculation of tq_numb - number of time quantum (must be in <8,25>)
488 // tq_numb = TSEG1 + TSEG2 + 3
490 for(tq_numb=25; tq_numb>=8; tq_numb--){
492 if ((res%tq_numb)==0){
494 // Baud Rate Prescaler. The PCLK clock is divided by (this value plus one) to produce the CAN clock.
495 BRP = (res / tq_numb) - 1;
497 // sync. segment allways 1 tq
500 // number of tq from the sample point to the next nominal Sync. point (this value plus one)
501 TSEG2 = (tq_numb/3) - 1;
503 // number of tq from Sync. point to the sample point (this value plus one)
504 TSEG1 = tq_numb - (tq_numb/3) - 1;
510 can_write_register(chip, ((SAM<<23)|(TSEG2<<20)|(TSEG1<<16)|(SJW<<14)|(BRP<<0)), CAN_BTR_o);
514 void CAN_init(struct canchip_t *chip){
521 printf("CAN INIT, baudrate: %d\n", chip->baudrate);
523 // configure CAN1 pins
526 // turn on power and clock for CAN1
529 // set clock divide for CAN1
531 val = 0x00; // 00 PCLK_peripheral = CCLK/4
532 pclksel0 = SC->PCLKSEL0;
533 pclksel0 &= ~PCLK_CAN1_MASK;
534 pclksel0 &= ~PCLK_CAN2_MASK;
535 pclksel0 &= ~PCLK_ACF_MASK;
536 pclksel0 |= __val2mfld(PCLK_CAN1_MASK, val);
537 pclksel0 |= __val2mfld(PCLK_CAN2_MASK, val);
538 pclksel0 |= __val2mfld(PCLK_ACF_MASK, val);
539 SC->PCLKSEL0 = pclksel0;
542 can_write_register(chip, 1, CAN_MOD_o);
544 // disable all CAN interrupts
545 can_write_register(chip, 0, CAN_IER_o);
547 // reset value of Global Status Register (global controller status and error counters)
548 can_write_register(chip, 0x3C, CAN_GSR_o);
550 // request command to release Rx, Tx buffer and clear data overrun
551 can_write_register(chip, (CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO), CAN_CMR_o);
553 // read to clear interrupt pending in Interrupt Capture Register
554 tmp = can_read_register(chip, CAN_ICR_o);
557 CAN_setBusTiming(chip);
559 // return to normal operating
560 can_write_register(chip, 0, CAN_MOD_o);
563 //--------------------------
565 // Acceptance Filter Off Mode
569 for (i = 0; i < 512; i++) {
570 CANAF_RAM->mask[i] = 0x00;
574 CANAF_SFF_GRP_sa = 0x00;
576 CANAF_EFF_GRP_sa = 0x00;
577 CANAF_ENDofTable = 0x00;
579 // Acceptance Filter Bypass Mode - all messages accepted
582 //--------------------------
585 // enable interrupt after transmit
586 // enable receive interrupt
587 // enable data overrun interrupt
588 can_write_register(chip, (CAN_IER_TIE1 | CAN_IER_RIE | CAN_IER_DOIE), CAN_IER_o);
590 // enable CAN interrupt
591 NVIC_EnableIRQ(CAN_IRQn);