Rev 543 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 543 | Rev 602 | ||
|---|---|---|---|
| Line 2... | Line 2... | ||
| 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> |
- | |
| 8 | #include <drivers/timer/timebase.h> |
7 | #include <drivers/timer/timebase.h> |
| 9 | #include <drivers/ir/ |
8 | #include <drivers/ir/transceiver/irtransceiver.h> |
| 10 | 9 | ||
| 11 | #define APP_TYPE CAN_APPTYPES_IRTRANSMITTER |
10 | #define APP_TYPE CAN_APPTYPES_IRTRANSMITTER |
| 12 | #define APP_VERSION 0x0001 |
11 | #define APP_VERSION 0x0001 |
| 13 | 12 | ||
| 14 | #define STATE_IDLE 0 |
13 | #define STATE_IDLE 0 |
| - | 14 | #define STATE_START_TRANSMIT 1 |
|
| - | 15 | #define STATE_TRANSMITTING 2 |
|
| - | 16 | #define STATE_START_PAUSE 3 |
|
| 15 | #define |
17 | #define STATE_PAUSING 4 |
| 16 | #define |
18 | #define STATE_STOP 5 |
| 17 | 19 | ||
| 18 | // A simple message "queue", with space for one message only. |
20 | // A simple message "queue", with space for one message only. |
| 19 | // These are declared volatile to tell the compiler not to optimize away accesses. |
21 | // These are declared volatile to tell the compiler not to optimize away accesses. |
| 20 | volatile Can_Message_t rxMsg; // Message storage |
22 | volatile Can_Message_t rxMsg; // Message storage |
| 21 | volatile uint8_t rxMsgFull; // Synchronization flag |
23 | volatile uint8_t rxMsgFull; // Synchronization flag |
| Line 33... | Line 35... | ||
| 33 | { |
35 | { |
| 34 | // Enable interrupts as early as possible |
36 | // Enable interrupts as early as possible |
| 35 | sei(); |
37 | sei(); |
| 36 | 38 | ||
| 37 | Timebase_Init(); |
39 | Timebase_Init(); |
| 38 | //Serial_Init(); |
- | |
| 39 |
|
40 | IrTransceiver_Init(); |
| 40 | 41 | ||
| 41 | Can_Message_t txMsg; |
42 | Can_Message_t txMsg; |
| 42 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
43 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 43 | txMsg.DataLength = 4; |
44 | txMsg.DataLength = 4; |
| 44 | txMsg.RemoteFlag = 0; |
45 | txMsg.RemoteFlag = 0; |
| Line 49... | Line 50... | ||
| 49 | // Set up callback for CAN reception, this is optional if only sending is required. |
50 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 50 | BIOS_CanCallback = &can_receive; |
51 | BIOS_CanCallback = &can_receive; |
| 51 | // Send CAN_NMT_APP_START |
52 | // Send CAN_NMT_APP_START |
| 52 | BIOS_CanSend(&txMsg); |
53 | BIOS_CanSend(&txMsg); |
| 53 | 54 | ||
| 54 |
|
55 | uint32_t time = Timebase_CurrentTime(); |
| - | 56 | uint32_t timePauseStarted = 0; |
|
| 55 | 57 | ||
| 56 | //unsigned long time = Timebase_CurrentTime(); |
- | |
| 57 | //unsigned long irTimeoutTime = 100; |
- | |
| 58 | - | ||
| 59 | //uint8_t ir_protocol, ir_address, ir_command; |
- | |
| 60 | //uint16_t ir_timeout; |
- | |
| 61 | uint8_t state = STATE_IDLE; |
58 | uint8_t state=STATE_IDLE; |
| - | 59 | uint8_t repeatcnt=0; |
|
| - | 60 | uint8_t stop=0; |
|
| - | 61 | uint8_t protocol=0; uint32_t ir_data=0; uint16_t timeout=0; uint8_t repeats=0; |
|
| 62 | 62 | ||
| 63 | while (1) { |
63 | while (1) { |
| 64 |
|
64 | time = Timebase_CurrentTime(); |
| 65 | if (state == STATE_IDLE) { |
65 | if (state == STATE_IDLE) { |
| - | 66 | } else if (state == STATE_START_TRANSMIT) { |
|
| - | 67 | if (IrTransceiver_Transmit(protocol, ir_data, &timeout, &repeats) == IR_OK) { |
|
| - | 68 | state = STATE_TRANSMITTING; |
|
| - | 69 | } else { |
|
| - | 70 | state = STATE_IDLE; |
|
| - | 71 | } |
|
| - | 72 | } else if (state == STATE_TRANSMITTING) { |
|
| - | 73 | //polla om den är klar, om klar gå till STATE_START_PAUSE |
|
| - | 74 | if (IrTransceiver_Transmit_Poll() != IR_NOT_FINISHED) { |
|
| - | 75 | state = STATE_START_PAUSE; |
|
| - | 76 | } |
|
| 66 | } else if (state == |
77 | } else if (state == STATE_START_PAUSE) { |
| - | 78 | if (repeatcnt<repeats) { |
|
| 67 |
|
79 | repeatcnt++; |
| - | 80 | } |
|
| - | 81 | timePauseStarted = Timebase_CurrentTime(); |
|
| - | 82 | state = STATE_PAUSING; |
|
| 68 | } else if (state == |
83 | } else if (state == STATE_PAUSING) { |
| - | 84 | //när timeout har gått (timebase) så gå till STATE_START_TRANSMIT |
|
| - | 85 | if (time - timePauseStarted >= timeout) { |
|
| - | 86 | state = STATE_START_TRANSMIT; |
|
| - | 87 | } |
|
| - | 88 | if (stop == 1 && repeatcnt >= repeats) { |
|
| 69 |
|
89 | state = STATE_STOP; |
| - | 90 | } |
|
| - | 91 | ||
| 70 |
|
92 | } else if (state == STATE_STOP) { |
| - | 93 | stop = 0; |
|
| - | 94 | timeout = 0; |
|
| - | 95 | repeatcnt = 0; |
|
| 71 | state = STATE_IDLE; |
96 | state = STATE_IDLE; |
| 72 | } |
97 | } |
| 73 | } |
- | |
| 74 | 98 | ||
| 75 | 99 | ||
| 76 | if (rxMsgFull) { |
100 | if (rxMsgFull) { |
| 77 | if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) { |
101 | if ( ((rxMsg.Id & CAN_MASK_CLASS)>>CAN_SHIFT_CLASS) == CAN_ACT) { |
| 78 | uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE); |
102 | uint16_t acttype =(uint16_t)((rxMsg.Id & CAN_MASK_ACT_TYPE) >> CAN_SHIFT_ACT_TYPE); |
| 79 | uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID); |
103 | uint8_t actid = (uint8_t)((rxMsg.Id & CAN_MASK_ACT_ID) >> CAN_SHIFT_ACT_ID); |
| 80 | 104 | ||
| 81 | if (rxMsg.DataLength== |
105 | if (rxMsg.DataLength==6) { |
| 82 | if (acttype == ACT_TYPE_IR) { |
106 | if (acttype == ACT_TYPE_IR) { |
| 83 | if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) { |
107 | if (state == STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_DOWN) { |
| - | 108 | protocol = rxMsg.Data.bytes[1]; |
|
| - | 109 | ir_data = rxMsg.Data.bytes[5]; |
|
| - | 110 | ir_data |= (rxMsg.Data.bytes[4]<<8); |
|
| 84 |
|
111 | ir_data |= ((uint32_t)rxMsg.Data.bytes[3]<<16); |
| - | 112 | ir_data |= ((uint32_t)rxMsg.Data.bytes[2]<<24); |
|
| - | 113 | ||
| 85 |
|
114 | state = STATE_START_TRANSMIT; |
| 86 | } |
- | |
| 87 | } else if (state |
115 | } else if (state != STATE_IDLE && rxMsg.Data.bytes[0] == IR_BUTTON_UP) { |
| 88 |
|
116 | stop = 1; |
| 89 | } |
117 | } |
| 90 | } |
118 | } |
| 91 | } |
119 | } |
| 92 | } |
120 | } |
| 93 | 121 | ||