-
Notifications
You must be signed in to change notification settings - Fork 0
/
byteworker.c
137 lines (107 loc) · 1.71 KB
/
byteworker.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <stdint.h>
#include <avr/io.h>
#include "can/can.h"
#include "byteworker.h"
//
// I/O
//
void bw_ledSet(uint8_t ledId, uint8_t state) {
switch(ledId) {
case 0: {
PORTC &= ~(1<<PC5);
DDRC |= (1<<PC5);
if(state) {
PORTC |= (1<<PC5);
}
break;
}
case 1: {
PORTC &= ~(1<<PC6);
DDRC |= (1<<PC6);
if(state) {
PORTC |= (1<<PC6);
}
break;
}
default: {
break;
}
}
}
void bw_ledToggle(uint8_t ledId) {
switch(ledId) {
case 0: {
PORTC ^= (1<<PC5);
break;
}
case 1: {
PORTC ^= (1<<PC6);
break;
}
default: {
break;
}
}
}
void bw_outputSet(uint8_t channel, uint8_t state) {
switch(channel) {
case 0: {
PORTB &= ~(1<<PB6);
DDRB |= (1<<PB6);
if(state) {
PORTB |= (1<<PB6);
}
break;
}
case 1: {
PORTC &= ~(1<<PC1);
DDRC |= (1<<PC1);
if(state) {
PORTC |= (1<<PC1);
}
break;
}
case 2: {
PORTD &= ~(1<<PD7);
DDRD |= (1<<PD7);
if(state) {
PORTD |= (1<<PD7);
}
break;
}
default: {
break;
}
}
}
//
// CAN
//
int8_t bw_canInit( uint16_t bitrate /* kbits */ ) {
switch( bitrate ) {
case 125: {
can_init( BITRATE_125_KBPS );
break;
}
case 500: {
can_init( BITRATE_500_KBPS );
break;
}
case 1000: {
can_init( BITRATE_1_MBPS );
break;
}
default: {
return -1;
break;
}
}
can_filter_t can_filter = { .id = 0, .mask = 0, .flags = { .rtr = 0, .extended = 0 } };
can_set_filter( 0, &can_filter );
can_set_filter( 1, &can_filter );
can_set_filter( 2, &can_filter );
// This is the TJA1043's standby and enable line.
DDRB |= (1<<PB3) | (1<<PB4); // !STB! | EN
PORTB |= (1<<PB3) | (1<<PB4); // ready for sending data
return 0;
}