Rev 221 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 221 | Rev 322 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | /** |
1 | /** |
| 2 | * |
2 | * CanIRreceive |
| 3 | * |
3 | * Receives IR data and sends packages to CAN |
| 4 | * |
4 | * |
| 5 | * @date 2006-11-21 |
5 | * @date 2006-11-21 |
| 6 | * @author Jimmy |
6 | * @author Jimmy Myhrman, Anders Runeson |
| - | 7 | * Converted to BIOS version by Erik Larsson 2007-02-12 |
|
| 7 | * |
8 | * |
| 8 | */ |
9 | */ |
| 9 | 10 | ||
| 10 | /*----------------------------------------------------------------------------- |
11 | /*----------------------------------------------------------------------------- |
| 11 | * Includes |
12 | * Includes |
| 12 | *---------------------------------------------------------------------------*/ |
13 | *---------------------------------------------------------------------------*/ |
| 13 | /* system files */ |
14 | /* system files */ |
| 14 | #include <avr/io.h> |
15 | #include <avr/io.h> |
| 15 | #include <avr/interrupt.h> |
16 | #include <avr/interrupt.h> |
| 16 | #include <avr/wdt.h> |
- | |
| 17 | #include <stdio.h> |
17 | #include <stdio.h> |
| 18 | /* lib files */ |
18 | /* lib files */ |
| 19 | #include < |
19 | #include <bios.h> |
| 20 | #include <serial.h> |
- | |
| 21 | #include <timebase.h> |
- | |
| 22 | #include <irreceiver.h> |
20 | #include <irreceiver.h> |
| - | 21 | #include <eqlazer_funcdefs.h> |
|
| 23 | 22 | ||
| 24 | #define STATE_IDLE 0 |
23 | #define STATE_IDLE 0 |
| 25 | #define STATE_IR_REPEAT 1 |
24 | #define STATE_IR_REPEAT 1 |
| 26 | 25 | ||
| 27 | /*----------------------------------------------------------------------------- |
26 | /*----------------------------------------------------------------------------- |
| 28 | * Main Program |
27 | * Main Program |
| 29 | *---------------------------------------------------------------------------*/ |
28 | *---------------------------------------------------------------------------*/ |
| 30 | int main(void) { |
29 | int main(void) { |
| 31 | Mcu_Init(); |
- | |
| 32 | Timebase_Init(); |
- | |
| 33 | Serial_Init(); |
- | |
| 34 | 30 | ||
| 35 | sei(); |
31 | sei(); |
| 36 | 32 | ||
| 37 | IRDDR &= ~(1<<IRBIT); |
33 | IRDDR &= ~(1<<IRBIT); |
| 38 | - | ||
| 39 | printf("\n------------------------------------------------------------\n"); |
- | |
| 40 | printf( " CAN Test: Periodic Transmission\n"); |
- | |
| 41 | printf( "------------------------------------------------------------\n"); |
- | |
| 42 | - | ||
| 43 | printf("CanInit..."); |
- | |
| 44 | if (Can_Init() != CAN_OK) { |
- | |
| 45 | printf("FAILED!\n"); |
- | |
| 46 | } |
- | |
| 47 | else { |
- | |
| 48 | printf("OK!\n"); |
- | |
| 49 | } |
- | |
| 50 | 34 | ||
| 51 | uint32_t timeStamp = 0; |
35 | uint32_t timeStamp = 0; |
| 52 | uint32_t irTimeoutTimeStamp = 0; |
36 | uint32_t irTimeoutTimeStamp = 0; |
| 53 | 37 | ||
| 54 | Can_Message_t txMsg; |
38 | Can_Message_t txMsg; |
| 55 | Can_Message_t rxMsg; |
39 | Can_Message_t rxMsg; |
| 56 | txMsg.Id = |
40 | txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(SNS_IR_RECEIVE << TYPE_MASK_BITS)|(NODE_ID); |
| 57 | 41 | ||
| 58 | uint8_t ir_protocol, ir_address, ir_command; |
42 | uint8_t ir_protocol, ir_address, ir_command; |
| 59 | uint8_t state = STATE_IDLE; |
43 | uint8_t state = STATE_IDLE; |
| 60 | 44 | ||
| 61 | /* main loop */ |
45 | /* main loop */ |
| 62 | while (1) { |
46 | while (1) { |
| 63 | /* service the CAN routines */ |
- | |
| 64 | Can_Service(); |
- | |
| 65 | - | ||
| 66 | if (state == STATE_IDLE) { |
47 | if (state == STATE_IDLE) { |
| 67 | if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) { |
48 | if (checkIr(&ir_protocol, &ir_address, &ir_command) == IR_OK) { |
| 68 | txMsg.Data.bytes[0] = 0x00; |
49 | txMsg.Data.bytes[0] = 0x00; |
| 69 | txMsg.Data.bytes[1] = ir_protocol; |
50 | txMsg.Data.bytes[1] = ir_protocol; |
| 70 | txMsg.Data.bytes[2] = ir_address; |
51 | txMsg.Data.bytes[2] = ir_address; |
| 71 | txMsg.Data.bytes[3] = ir_command; |
52 | txMsg.Data.bytes[3] = ir_command; |
| 72 | txMsg.DataLength = 4; |
53 | txMsg.DataLength = 4; |
| 73 |
|
54 | bios->can_send(&txMsg); |
| 74 | irTimeoutTimeStamp = |
55 | irTimeoutTimeStamp = bios->timebase_get(); |
| 75 | state = STATE_IR_REPEAT; |
56 | state = STATE_IR_REPEAT; |
| 76 | } |
57 | } |
| 77 | } else if (state == STATE_IR_REPEAT) { |
58 | } else if (state == STATE_IR_REPEAT) { |
| 78 | //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE |
59 | //kolla efter ny ir, om ingen ny ir och timeout så sätt state till IDLE |
| 79 | if ( |
60 | if (bios->timebase_get() - irTimeoutTimeStamp >= 105) { //getLastProtoTimeout() |
| 80 | state = STATE_IDLE; |
61 | state = STATE_IDLE; |
| 81 | txMsg.Data.bytes[0] = 0x0f; |
62 | txMsg.Data.bytes[0] = 0x0f; |
| 82 |
|
63 | bios->can_send(&txMsg); |
| 83 | } |
64 | } |
| 84 | if (checkIrIdle() == IR_OK) { //funktion som borde finnas i irreceiver.c |
65 | if (checkIrIdle() == IR_OK) { //funktion som borde finnas i irreceiver.c |
| 85 | irTimeoutTimeStamp = |
66 | irTimeoutTimeStamp = bios->timebase_get(); |
| 86 | } |
- | |
| 87 | } |
- | |
| 88 | - | ||
| 89 | - | ||
| 90 | /* check if any messages have been received */ |
- | |
| 91 | while (Can_Receive(&rxMsg) == CAN_OK) { |
- | |
| 92 | /*printf("MSG Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag)); |
- | |
| 93 | printf("data={ "); |
- | |
| 94 | for (uint8_t i=0; i<rxMsg.DataLength; i++) { |
- | |
| 95 | printf("%x ", rxMsg.Data.bytes[i]); |
- | |
| 96 | } |
67 | } |
| 97 | printf("}\n");*/ |
- | |
| 98 | } |
68 | } |
| 99 | } |
69 | } |
| 100 | 70 | ||
| 101 | return 0; |
71 | return 0; |
| 102 | } |
72 | } |