OpenCores
URL https://opencores.org/ocsvn/minsoc/minsoc/trunk

Subversion Repositories minsoc

[/] [minsoc/] [branches/] [rc-1.0/] [sw/] [drivers/] [can.c] - Blame information for rev 131

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 80 rfajardo
#include <board.h>
2
#include <support.h>
3 36 rfajardo
#include "can.h"
4
 
5
int can_rx_done, can_tx_done;
6
int can_rx_rd_ptr;
7
int can_rx_wr_ptr;
8
int can_rx_buf_overflow;
9
 
10
can_type can_rx_data[CAN_BUF_LEN], can_tx_data;
11
 
12
can_type * can_get(void)
13
{
14 53 ConX.
        if ( !can_rx_done )
15
                return NULL;
16 36 rfajardo
 
17 53 ConX.
        can_rx_done--;
18 36 rfajardo
 
19 53 ConX.
        int tmp;
20
        tmp = can_rx_rd_ptr;
21 36 rfajardo
 
22 53 ConX.
        if (can_rx_rd_ptr < CAN_BUF_LEN-1)
23
                can_rx_rd_ptr++;
24
        else
25
                can_rx_rd_ptr = 0;
26 36 rfajardo
 
27 53 ConX.
        return &can_rx_data[tmp];
28 36 rfajardo
}
29
 
30
void can_init(void)
31
{
32 53 ConX.
        unsigned char sync_jmp, baudrate_presc, timing_seg1, timing_seg2, tripple_samp = 0;
33
        unsigned char acpt_code, acpt_mask = 0;
34
        unsigned char clk_div = 0 & CAN_BUS_CLKDIV_MASK;
35 36 rfajardo
 
36 53 ConX.
        sync_jmp = 1;
37
        baudrate_presc = 1;
38
        timing_seg1 = 11;
39
        timing_seg2 = 2;
40
        tripple_samp = 1;
41 36 rfajardo
 
42 53 ConX.
        acpt_code = 0x81;
43
        acpt_mask = 0xFF;
44 36 rfajardo
 
45 53 ConX.
        char timing0, timing1 = 0;
46 36 rfajardo
 
47 53 ConX.
        timing0 = (sync_jmp << CAN_BUS_TIMING_0_SYNC_JMP_SHIFT) & CAN_BUS_TIMING_0_SYNC_JMP;
48
        timing0 |= baudrate_presc & CAN_BUS_TIMING_0_BAUD_PRESC;
49 36 rfajardo
 
50 53 ConX.
        timing1 = (tripple_samp << CAN_BUS_TIMING_1_TRIPLE_SAMP_SHIFT) & CAN_BUS_TIMING_1_TRIPLE_SAMP;
51
        timing1 |= (timing_seg2 << CAN_BUS_TIMING_1_TIME_SEG2_SHIFT) & CAN_BUS_TIMING_1_TIME_SEG2;
52
        timing1 |= timing_seg1 & CAN_BUS_TIMING_1_TIME_SEG1;
53 36 rfajardo
 
54 53 ConX.
        REG8(CAN_BASE+CAN_MODE) = CAN_MODE_RESET;
55 36 rfajardo
 
56 53 ConX.
        REG8(CAN_BASE+CAN_BUS_TIMING_0) = timing0;
57
        REG8(CAN_BASE+CAN_BUS_TIMING_1) = timing1;
58 36 rfajardo
 
59 53 ConX.
        REG8(CAN_BASE+CAN_ACPT_CODE0) = acpt_code;
60
        REG8(CAN_BASE+CAN_ACPT_MASK0) = acpt_mask;
61 36 rfajardo
 
62 53 ConX.
        REG8(CAN_BASE+CAN_BUS_MODE) &= ~CAN_BUS_MODE_CLOCK_OFF & ~CAN_BUS_MODE_EXTENDED_MODE;
63 36 rfajardo
 
64 53 ConX.
        REG8(CAN_BASE+CAN_MODE) &= ~CAN_MODE_RESET;
65
        REG8(CAN_BASE+CAN_BUS_CLKDIV) = clk_div;
66 36 rfajardo
 
67 53 ConX.
        REG8(CAN_BASE+CAN_MODE) |= CAN_MODE_TX_IRQ_EN | CAN_MODE_RECV_IRQ_EN;
68 36 rfajardo
 
69 53 ConX.
        can_tx_done = 1;
70
        can_rx_done = 0;
71
        can_rx_rd_ptr = 0;
72
        can_rx_wr_ptr = 0;
73
        can_rx_buf_overflow = 0;
74 36 rfajardo
}
75
 
76
void can_recv_basic()
77
{
78 53 ConX.
        unsigned char byte0, byte1;
79 36 rfajardo
 
80 53 ConX.
        byte0 = REG8(CAN_BASE+CAN_RX_BUF);
81
        byte1 = REG8(CAN_BASE+CAN_RX_BUF+1);
82 36 rfajardo
 
83 53 ConX.
        can_rx_data[can_rx_wr_ptr].data[0] = REG8(CAN_BASE+CAN_RX_BUF+2);
84
        can_rx_data[can_rx_wr_ptr].data[1] = REG8(CAN_BASE+CAN_RX_BUF+3);
85
        can_rx_data[can_rx_wr_ptr].data[2] = REG8(CAN_BASE+CAN_RX_BUF+4);
86
        can_rx_data[can_rx_wr_ptr].data[3] = REG8(CAN_BASE+CAN_RX_BUF+5);
87
        can_rx_data[can_rx_wr_ptr].data[4] = REG8(CAN_BASE+CAN_RX_BUF+6);
88
        can_rx_data[can_rx_wr_ptr].data[5] = REG8(CAN_BASE+CAN_RX_BUF+7);
89
        can_rx_data[can_rx_wr_ptr].data[6] = REG8(CAN_BASE+CAN_RX_BUF+8);
90
        can_rx_data[can_rx_wr_ptr].data[7] = REG8(CAN_BASE+CAN_RX_BUF+9);
91 36 rfajardo
 
92 53 ConX.
        REG8(CAN_BASE+CAN_CMD) = CAN_CMD_RELEASE_BUFFER;
93 36 rfajardo
 
94 53 ConX.
        can_rx_data[can_rx_wr_ptr].identifier = (byte0 << 3) | (byte1 >> 5);
95
        can_rx_data[can_rx_wr_ptr].rtr = byte1 & 0x10;
96
        can_rx_data[can_rx_wr_ptr].len = byte1 & 0x0F;
97 36 rfajardo
 
98 53 ConX.
        if (can_rx_wr_ptr < CAN_BUF_LEN-1)
99
                can_rx_wr_ptr++;
100
        else
101
                can_rx_wr_ptr = 0;
102 36 rfajardo
 
103 53 ConX.
        if (can_rx_wr_ptr == can_rx_rd_ptr+1)       //buffer overflow
104
        {
105
                can_rx_done = 1;
106
                can_rx_buf_overflow++;
107
        }
108
        else
109
                can_rx_done++;
110 36 rfajardo
}
111
 
112
int can_send_basic()
113
{
114 53 ConX.
        if (!can_tx_done)       //if previous command not fully processed, bail out
115
                return -1;
116 36 rfajardo
 
117 53 ConX.
        can_tx_done = 0;
118
        REG8(CAN_BASE+CAN_TX_BUF) = can_tx_data.identifier >> 3;
119
        REG8(CAN_BASE+CAN_TX_BUF+1) = (can_tx_data.identifier << 5) | ((can_tx_data.rtr << 4) & 0x10) | (can_tx_data.len & 0x0F);
120 36 rfajardo
 
121 53 ConX.
        REG8(CAN_BASE+CAN_TX_BUF+2) = can_tx_data.data[0];
122
        REG8(CAN_BASE+CAN_TX_BUF+3) = can_tx_data.data[1];
123
        REG8(CAN_BASE+CAN_TX_BUF+4) = can_tx_data.data[2];
124
        REG8(CAN_BASE+CAN_TX_BUF+5) = can_tx_data.data[3];
125
        REG8(CAN_BASE+CAN_TX_BUF+6) = can_tx_data.data[4];
126
        REG8(CAN_BASE+CAN_TX_BUF+7) = can_tx_data.data[5];
127
        REG8(CAN_BASE+CAN_TX_BUF+8) = can_tx_data.data[6];
128
        REG8(CAN_BASE+CAN_TX_BUF+9) = can_tx_data.data[7];
129 36 rfajardo
 
130 53 ConX.
        REG8(CAN_BASE+CAN_CMD) = CAN_CMD_TX_REQ;
131 36 rfajardo
 
132 53 ConX.
        return can_tx_data.len;
133 36 rfajardo
}
134
 
135
void can_irq(void)
136
{
137 53 ConX.
        unsigned char irq_req, rx_done;
138
        irq_req = REG8(CAN_BASE+IRQ_READ);
139
        rx_done = irq_req & CAN_IRQ_READ_RX;
140
        can_tx_done = irq_req & CAN_IRQ_READ_TX;
141
        if (rx_done)
142
                can_recv_basic();
143 36 rfajardo
}
144
 
145
void can_abort(void)
146
{
147 53 ConX.
        REG8(CAN_BASE+CAN_CMD) = CAN_CMD_ABORT_TX;
148 36 rfajardo
}
149
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.