Rev 527 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 527 | Rev 528 | ||
|---|---|---|---|
| 1 | #include <inttypes.h> |
1 | #include <inttypes.h> |
| 2 | #include <avr/interrupt.h> |
2 | #include <avr/interrupt.h> |
| 3 | #include <stdio.h> |
3 | #include <stdio.h> |
| 4 | #include <string.h> |
4 | #include <string.h> |
| 5 | #include <config.h> // All configuration parameters |
5 | #include <config.h> // All configuration parameters |
| 6 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
6 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
| 7 | //#include <drivers/uart/serial.h> |
7 | //#include <drivers/uart/serial.h> |
| 8 | #include <drivers/timer/timebase.h> |
8 | #include <drivers/timer/timebase.h> |
| 9 | #include <drivers/ir/receiver/irreceiver.h> |
9 | #include <drivers/ir/receiver/irreceiver.h> |
| 10 | 10 | ||
| 11 | #define APP_TYPE CAN_APPTYPES_IRRECEIVER |
11 | #define APP_TYPE CAN_APPTYPES_IRRECEIVER |
| 12 | #define APP_VERSION 0x0003 |
12 | #define APP_VERSION 0x0003 |
| 13 | 13 | ||
| 14 | #define STATE_IDLE 0 |
14 | #define STATE_IDLE 0 |
| 15 | #define STATE_IR_REPEAT 1 |
15 | #define STATE_IR_REPEAT 1 |
| 16 | 16 | ||
| 17 | #define IR_ID_DATA 0 |
17 | #define IR_ID_DATA 0 |
| 18 | #define IR_ID_RAW 1 |
18 | #define IR_ID_RAW 1 |
| 19 | #define IR_ID_DEBUG 10 |
19 | #define IR_ID_DEBUG 10 |
| 20 | 20 | ||
| 21 | // A simple message "queue", with space for one message only. |
21 | // A simple message "queue", with space for one message only. |
| 22 | // These are declared volatile to tell the compiler not to optimize away accesses. |
22 | // These are declared volatile to tell the compiler not to optimize away accesses. |
| 23 | volatile Can_Message_t rxMsg; // Message storage |
23 | volatile Can_Message_t rxMsg; // Message storage |
| 24 | volatile uint8_t rxMsgFull; // Synchronization flag |
24 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 25 | 25 | ||
| 26 | // CAN message reception callback. |
26 | // CAN message reception callback. |
| 27 | // This function runs with interrupts disabled, keep it as short as possible. |
27 | // This function runs with interrupts disabled, keep it as short as possible. |
| 28 | void can_receive(Can_Message_t *msg) { |
28 | void can_receive(Can_Message_t *msg) { |
| 29 | if (!rxMsgFull) { |
29 | if (!rxMsgFull) { |
| 30 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
30 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
| 31 | rxMsgFull = 1; |
31 | rxMsgFull = 1; |
| 32 | } |
32 | } |
| 33 | } |
33 | } |
| 34 | 34 | ||
| 35 | int main(void) |
35 | int main(void) |
| 36 | { |
36 | { |
| 37 | // Enable interrupts as early as possible |
37 | // Enable interrupts as early as possible |
| 38 | sei(); |
38 | sei(); |
| 39 | 39 | ||
| 40 | Timebase_Init(); |
40 | Timebase_Init(); |
| 41 | //Serial_Init(); |
41 | //Serial_Init(); |
| 42 | IrReceive_Init(); |
42 | IrReceive_Init(); |
| 43 | 43 | ||
| 44 | Can_Message_t txMsg; |
44 | Can_Message_t txMsg; |
| 45 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
45 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 46 | txMsg.DataLength = 4; |
46 | txMsg.DataLength = 4; |
| 47 | txMsg.RemoteFlag = 0; |
47 | txMsg.RemoteFlag = 0; |
| 48 | txMsg.ExtendedFlag = 1; |
48 | txMsg.ExtendedFlag = 1; |
| 49 | txMsg.Data.words[0] = APP_TYPE; |
49 | txMsg.Data.words[0] = APP_TYPE; |
| 50 | txMsg.Data.words[1] = APP_VERSION; |
50 | txMsg.Data.words[1] = APP_VERSION; |
| 51 | 51 | ||
| 52 | // Set up callback for CAN reception, this is optional if only sending is required. |
52 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 53 | BIOS_CanCallback = &can_receive; |
53 | BIOS_CanCallback = &can_receive; |
| 54 | // Send CAN_NMT_APP_START |
54 | // Send CAN_NMT_APP_START |
| 55 | BIOS_CanSend(&txMsg); |
55 | BIOS_CanSend(&txMsg); |
| 56 | 56 | ||
| 57 | //txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
57 | //txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 58 | 58 | ||
| 59 | unsigned long time = Timebase_CurrentTime(); |
59 | unsigned long time = Timebase_CurrentTime(); |
| 60 | unsigned long irTimeoutTime = 100; |
60 | unsigned long irTimeoutTime = 100; |
| 61 | 61 | ||
| 62 | uint8_t ir_protocol, ir_address, ir_command; |
62 | uint8_t ir_protocol, ir_address, ir_command; |
| 63 | uint16_t ir_timeout; |
63 | uint16_t ir_timeout; |
| 64 | uint8_t state = STATE_IDLE; |
64 | uint8_t state = STATE_IDLE; |
| 65 | 65 | ||
| 66 | while (1) { |
66 | while (1) { |
| 67 | #if 0 |
67 | #if 0 |
| 68 | time = Timebase_CurrentTime(); |
68 | time = Timebase_CurrentTime(); |
| 69 | if (state == STATE_IDLE) { |
69 | if (state == STATE_IDLE) { |
| 70 | if (IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout) == IR_OK) { |
70 | if (IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout) == IR_OK) { |
| 71 | txMsg.Data.bytes[0] = 0x00; |
71 | txMsg.Data.bytes[0] = 0x00; |
| 72 | txMsg.Data.bytes[1] = ir_protocol; |
72 | txMsg.Data.bytes[1] = ir_protocol; |
| 73 | txMsg.Data.bytes[2] = ir_address; |
73 | txMsg.Data.bytes[2] = ir_address; |
| 74 | txMsg.Data.bytes[3] = ir_command; |
74 | txMsg.Data.bytes[3] = ir_command; |
| 75 | txMsg.DataLength = 4; |
75 | txMsg.DataLength = 4; |
| 76 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
76 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 77 | BIOS_CanSend(&txMsg); |
77 | BIOS_CanSend(&txMsg); |
| 78 | irTimeoutTime = Timebase_CurrentTime(); |
78 | irTimeoutTime = Timebase_CurrentTime(); |
| 79 | state = STATE_IR_REPEAT; |
79 | state = STATE_IR_REPEAT; |
| 80 | } |
80 | } |
| 81 | } else if (state == STATE_IR_REPEAT) { |
81 | } else if (state == STATE_IR_REPEAT) { |
| 82 | //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE |
82 | //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE |
| 83 | if (time - irTimeoutTime >= ir_timeout) { |
83 | if (time - irTimeoutTime >= ir_timeout) { |
| 84 | state = STATE_IDLE; |
84 | state = STATE_IDLE; |
| 85 | txMsg.Data.bytes[0] = 0x0f; |
85 | txMsg.Data.bytes[0] = 0x0f; |
| 86 | BIOS_CanSend(&txMsg); |
86 | BIOS_CanSend(&txMsg); |
| 87 | } |
87 | } |
| 88 | if (IrReceive_CheckIdle() == IR_OK) { |
88 | if (IrReceive_CheckIdle() == IR_OK) { |
| 89 | irTimeoutTime = Timebase_CurrentTime(); |
89 | irTimeoutTime = Timebase_CurrentTime(); |
| 90 | } |
90 | } |
| 91 | } |
91 | } |
| 92 | #endif |
92 | #endif |
| 93 | 93 | ||
| 94 | #if 1 |
94 | #if 1 |
| 95 | time = Timebase_CurrentTime(); |
95 | time = Timebase_CurrentTime(); |
| 96 | 96 | ||
| 97 | if (state == STATE_IDLE) { |
97 | if (state == STATE_IDLE) { |
| 98 | /* test for irdata */ |
98 | /* test for irdata */ |
| 99 | uint8_t returnval = IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout); |
99 | uint8_t returnval = IrReceive_CheckIR(&ir_protocol, &ir_address, &ir_command, &ir_timeout); |
| 100 | if (returnval == IR_OK) { |
100 | if (returnval == IR_OK) { |
| 101 | /* a protocol was found for irdata, send on can */ |
101 | /* a protocol was found for irdata, send on can */ |
| 102 | txMsg.Data.bytes[0] = 0x00; |
102 | txMsg.Data.bytes[0] = 0x00; |
| 103 | txMsg.Data.bytes[1] = ir_protocol; |
103 | txMsg.Data.bytes[1] = ir_protocol; |
| 104 | txMsg.Data.bytes[2] = ir_address; |
104 | txMsg.Data.bytes[2] = ir_address; |
| 105 | txMsg.Data.bytes[3] = ir_command; |
105 | txMsg.Data.bytes[3] = ir_command; |
| 106 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
106 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DATA<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 107 | txMsg.DataLength = 4; |
107 | txMsg.DataLength = 4; |
| 108 | BIOS_CanSend(&txMsg); |
108 | BIOS_CanSend(&txMsg); |
| 109 | 109 | ||
| 110 | /* set up timeout */ |
110 | /* set up timeout */ |
| 111 | irTimeoutTime = Timebase_CurrentTime(); |
111 | irTimeoutTime = Timebase_CurrentTime(); |
| 112 | state = STATE_IR_REPEAT; |
112 | state = STATE_IR_REPEAT; |
| 113 | 113 | ||
| 114 | } else if (returnval == IR_NO_PROTOCOL) { |
114 | } else if (returnval == IR_NO_PROTOCOL) { |
| 115 | /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */ |
115 | /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */ |
| 116 | ir_protocol = IR_PROTO_UNKNOWN; //used in STATE_IR_REPEATE, to know if to send a release |
116 | ir_protocol = IR_PROTO_UNKNOWN; //used in STATE_IR_REPEATE, to know if to send a release |
| 117 | txMsg.DataLength = 8; |
117 | txMsg.DataLength = 8; |
| 118 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_RAW<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
118 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_RAW<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 119 | uint8_t datacnt = getRawDataCnt(); |
119 | uint8_t datacnt = getRawDataCnt(); |
| 120 | for (uint8_t i = 0; i < datacnt>>2; i++) { |
120 | for (uint8_t i = 0; i < datacnt>>2; i++) { |
| 121 | uint8_t index = i<<2; |
121 | uint8_t index = i<<2; |
| 122 | txMsg.Data.words[0] = getRawData(index); |
122 | txMsg.Data.words[0] = getRawData(index); |
| 123 | txMsg.Data.words[1] = getRawData(index+1); |
123 | txMsg.Data.words[1] = getRawData(index+1); |
| 124 | txMsg.Data.words[2] = getRawData(index+2); |
124 | txMsg.Data.words[2] = getRawData(index+2); |
| 125 | txMsg.Data.words[3] = getRawData(index+3); |
125 | txMsg.Data.words[3] = getRawData(index+3); |
| 126 | 126 | ||
| 127 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
127 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
| 128 |
|
128 | //while (BIOS_CanSend(&txMsg) != CAN_OK) {} |
| - | 129 | BIOS_CanSend(&txMsg); |
|
| - | 130 | ||
| - | 131 | uint16_t dummycnt = 1; |
|
| - | 132 | while (dummycnt > 0) { |
|
| - | 133 | dummycnt++; |
|
| - | 134 | asm("nop"); |
|
| - | 135 | asm("nop"); |
|
| - | 136 | } |
|
| 129 | } |
137 | } |
| 130 | 138 | ||
| 131 | uint8_t lastpacketcnt = datacnt&0x03; |
139 | uint8_t lastpacketcnt = datacnt&0x03; |
| 132 | if (lastpacketcnt > 0) { |
140 | if (lastpacketcnt > 0) { |
| 133 | txMsg.DataLength = lastpacketcnt<<1; |
141 | txMsg.DataLength = lastpacketcnt<<1; |
| 134 | for (uint8_t i = 0; i < lastpacketcnt; i++) { |
142 | for (uint8_t i = 0; i < lastpacketcnt; i++) { |
| 135 | txMsg.Data.words[i] = getRawData((datacnt&0xfc)|i); |
143 | txMsg.Data.words[i] = getRawData((datacnt&0xfc)|i); |
| 136 | } |
144 | } |
| - | 145 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
|
| - | 146 | //while (BIOS_CanSend(&txMsg) != CAN_OK) {} |
|
| 137 | BIOS_CanSend(&txMsg); |
147 | BIOS_CanSend(&txMsg); |
| 138 | } |
148 | } |
| 139 | 149 | ||
| 140 | irTimeoutTime = Timebase_CurrentTime(); |
150 | irTimeoutTime = Timebase_CurrentTime(); |
| 141 | state = STATE_IR_REPEAT; |
151 | state = STATE_IR_REPEAT; |
| 142 | ir_timeout = 200; /* Set a default timeout */ |
152 | ir_timeout = 200; /* Set a default timeout */ |
| 143 | } else if (returnval != IR_NO_DATA) { |
153 | } else if (returnval != IR_NO_DATA) { |
| 144 | /* debug, send the returned message on errors (except for IR_NO_DATA-errors) */ |
154 | /* debug, send the returned message on errors (except for IR_NO_DATA-errors) */ |
| 145 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DEBUG<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
155 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_IR << CAN_SHIFT_SNS_TYPE) | (IR_ID_DEBUG<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 146 | txMsg.DataLength = 1; |
156 | txMsg.DataLength = 1; |
| 147 | txMsg.Data.bytes[0] = returnval; |
157 | txMsg.Data.bytes[0] = returnval; |
| 148 | BIOS_CanSend(&txMsg); |
158 | BIOS_CanSend(&txMsg); |
| 149 | } |
159 | } |
| 150 | 160 | ||
| 151 | } else if (state == STATE_IR_REPEAT) { |
161 | } else if (state == STATE_IR_REPEAT) { |
| 152 | /* check for new ir-data, if no new data during the timout-time then set state to IDLE */ |
162 | /* check for new ir-data, if no new data during the timout-time then set state to IDLE */ |
| 153 | if (time - irTimeoutTime >= ir_timeout) { |
163 | if (time - irTimeoutTime >= ir_timeout) { |
| 154 | state = STATE_IDLE; |
164 | state = STATE_IDLE; |
| 155 | /* if the protocol was known then send a release-message */ |
165 | /* if the protocol was known then send a release-message */ |
| 156 | if (ir_protocol != IR_PROTO_UNKNOWN) { |
166 | if (ir_protocol != IR_PROTO_UNKNOWN) { |
| 157 | txMsg.Data.bytes[0] = 0x0f; |
167 | txMsg.Data.bytes[0] = 0x0f; |
| 158 | BIOS_CanSend(&txMsg); |
168 | BIOS_CanSend(&txMsg); |
| 159 | } |
169 | } |
| 160 | } |
170 | } |
| 161 | if (IrReceive_CheckIdle() == IR_OK) { |
171 | if (IrReceive_CheckIdle() == IR_OK) { |
| 162 | irTimeoutTime = Timebase_CurrentTime(); |
172 | irTimeoutTime = Timebase_CurrentTime(); |
| 163 | } |
173 | } |
| 164 | } |
174 | } |
| 165 | 175 | ||
| 166 | #endif |
176 | #endif |
| 167 | 177 | ||
| 168 | 178 | ||
| 169 | if (rxMsgFull) { |
179 | if (rxMsgFull) { |
| 170 | /* No message reception functionality implemented */ |
180 | /* No message reception functionality implemented */ |
| 171 | 181 | ||
| 172 | /* Flush the received message */ |
182 | /* Flush the received message */ |
| 173 | rxMsgFull = 0; // |
183 | rxMsgFull = 0; // |
| 174 | } |
184 | } |
| 175 | } |
185 | } |
| 176 | 186 | ||
| 177 | return 0; |
187 | return 0; |
| 178 | } |
188 | } |
| 179 | 189 | ||