Details | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 403 | arune | 1 | #include <inttypes.h> |
| 2 | #include <avr/interrupt.h> |
||
| 3 | #include <stdio.h> |
||
| 4 | #include <string.h> |
||
| 5 | #include <config.h> // All configuration parameters |
||
| 6 | #include <bios.h> // BIOS interface declarations, including CAN structure and ID defines. |
||
| 611 | arune | 7 | |
| 403 | arune | 8 | #include <drivers/timer/timebase.h> |
| 603 | arune | 9 | #include <drivers/ir/transceiver/irtransceiver.h> |
| 611 | arune | 10 | #include <drivers/ir/protocols.h> |
| 403 | arune | 11 | |
| 486 | arune | 12 | #define APP_TYPE CAN_APPTYPES_IRRECEIVER |
| 611 | arune | 13 | #define APP_VERSION 0x0004 |
| 403 | arune | 14 | |
| 611 | arune | 15 | #define STATE_IDLE 0 |
| 16 | #define STATE_IR_REPEAT 1 |
||
| 17 | #define STATE_START_RECEIVE 2 |
||
| 18 | #define STATE_RECEIVING 3 |
||
| 19 | #define STATE_START_PAUSE 4 |
||
| 20 | #define STATE_PAUSING 5 |
||
| 614 | arune | 21 | #define STATE_START_IDLE 6 |
| 403 | arune | 22 | |
| 527 | arune | 23 | #define IR_ID_DATA 0 |
| 24 | #define IR_ID_RAW 1 |
||
| 25 | #define IR_ID_DEBUG 10 |
||
| 439 | arune | 26 | |
| 403 | arune | 27 | // A simple message "queue", with space for one message only. |
| 28 | // These are declared volatile to tell the compiler not to optimize away accesses. |
||
| 29 | volatile Can_Message_t rxMsg; // Message storage |
||
| 30 | volatile uint8_t rxMsgFull; // Synchronization flag |
||
| 31 | |||
| 32 | // CAN message reception callback. |
||
| 33 | // This function runs with interrupts disabled, keep it as short as possible. |
||
| 34 | void can_receive(Can_Message_t *msg) { |
||
| 35 | if (!rxMsgFull) { |
||
| 36 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
||
| 37 | rxMsgFull = 1; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | |||
| 616 | arune | 41 | #if (SEND_DEBUG) |
| 42 | void send_debug(uint16_t *buffer, uint8_t len) { |
||
| 43 | Can_Message_t txMsg; |
||
| 44 | |||
| 45 | /* the protocol is unknown so the raw ir-data is sent, makes it easier to develop a new protocol */ |
||
| 46 | txMsg.DataLength = 8; |
||
| 47 | 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)); |
||
| 48 | for (uint8_t i = 0; i < len>>2; i++) { |
||
| 49 | uint8_t index = i<<2; |
||
| 50 | txMsg.Data.bytes[0] = (buffer[index]>>8)&0xff; |
||
| 51 | txMsg.Data.bytes[1] = (buffer[index]>>0)&0xff; |
||
| 52 | txMsg.Data.bytes[2] = (buffer[index+1]>>8)&0xff; |
||
| 53 | txMsg.Data.bytes[3] = (buffer[index+1]>>0)&0xff; |
||
| 54 | txMsg.Data.bytes[4] = (buffer[index+2]>>8)&0xff; |
||
| 55 | txMsg.Data.bytes[5] = (buffer[index+2]>>0)&0xff; |
||
| 56 | txMsg.Data.bytes[6] = (buffer[index+3]>>8)&0xff; |
||
| 57 | txMsg.Data.bytes[7] = (buffer[index+3]>>0)&0xff; |
||
| 58 | |||
| 59 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
||
| 1044 | arune | 60 | while (BIOS_CanSend(&txMsg) != CAN_OK) {} |
| 616 | arune | 61 | } |
| 62 | |||
| 63 | uint8_t lastpacketcnt = len&0x03; |
||
| 64 | if (lastpacketcnt > 0) { |
||
| 65 | txMsg.DataLength = lastpacketcnt<<1; |
||
| 66 | for (uint8_t i = 0; i < lastpacketcnt; i++) { |
||
| 67 | txMsg.Data.bytes[i<<1] = (buffer[(len&0xfc)|i]>>8)&0xff; |
||
| 68 | txMsg.Data.bytes[(i<<1)+1] = (buffer[(len&0xfc)|i]>>0)&0xff; |
||
| 69 | } |
||
| 70 | /* buffers will be filled when sending more than 2-3 messages, so retry until sent */ |
||
| 1044 | arune | 71 | while (BIOS_CanSend(&txMsg) != CAN_OK) {} |
| 72 | //BIOS_CanSend(&txMsg); |
||
| 616 | arune | 73 | } |
| 74 | |||
| 75 | } |
||
| 76 | #endif |
||
| 77 | |||
| 403 | arune | 78 | int main(void) |
| 79 | { |
||
| 80 | // Enable interrupts as early as possible |
||
| 81 | sei(); |
||
| 82 | |||
| 83 | Timebase_Init(); |
||
| 611 | arune | 84 | IrTransceiver_Init(); |
| 403 | arune | 85 | |
| 86 | Can_Message_t txMsg; |
||
| 87 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
||
| 88 | txMsg.DataLength = 4; |
||
| 89 | txMsg.RemoteFlag = 0; |
||
| 90 | txMsg.ExtendedFlag = 1; |
||
| 91 | txMsg.Data.words[0] = APP_TYPE; |
||
| 92 | txMsg.Data.words[1] = APP_VERSION; |
||
| 93 | |||
| 94 | // Set up callback for CAN reception, this is optional if only sending is required. |
||
| 95 | BIOS_CanCallback = &can_receive; |
||
| 96 | // Send CAN_NMT_APP_START |
||
| 97 | BIOS_CanSend(&txMsg); |
||
| 519 | arune | 98 | |
| 611 | arune | 99 | uint32_t time = Timebase_CurrentTime(); |
| 100 | uint32_t timePauseStarted = 0; |
||
| 403 | arune | 101 | |
| 102 | uint8_t state = STATE_IDLE; |
||
| 611 | arune | 103 | |
| 104 | Ir_Protocol_Data_t proto; |
||
| 105 | proto.timeout=0; proto.data=0; proto.repeats=0; proto.protocol=0; |
||
| 106 | uint8_t len=0; |
||
| 107 | uint16_t rxbuffer[MAX_NR_TIMES]; |
||
| 108 | |||
| 403 | arune | 109 | while (1) { |
| 527 | arune | 110 | if (state == STATE_IDLE) { |
| 611 | arune | 111 | IrTransceiver_Receive_Start(rxbuffer); |
| 112 | state = STATE_START_RECEIVE; |
||
| 113 | } else if (state == STATE_START_RECEIVE) { |
||
| 114 | state = STATE_RECEIVING; |
||
| 115 | } else if (state == STATE_RECEIVING) { |
||
| 614 | arune | 116 | uint8_t res = IrTransceiver_Receive_Poll(&len); |
| 117 | if ((res == IR_OK) && (len > 0)) { |
||
| 611 | arune | 118 | //låt protocols parsa och skicka på can |
| 616 | arune | 119 | uint8_t res2 = parseProtocol(rxbuffer, len, &proto); |
| 120 | if (res2 == IR_OK && proto.protocol != IR_PROTO_UNKNOWN) { |
||
| 121 | txMsg.Data.bytes[0] = IR_BUTTON_DOWN; |
||
| 122 | txMsg.Data.bytes[1] = proto.protocol; |
||
| 123 | txMsg.Data.bytes[2] = (proto.data>>24)&0xff; |
||
| 124 | txMsg.Data.bytes[3] = (proto.data>>16)&0xff; |
||
| 125 | txMsg.Data.bytes[4] = (proto.data>>8)&0xff; |
||
| 126 | txMsg.Data.bytes[5] = proto.data&0xff; |
||
| 127 | 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)); |
||
| 128 | txMsg.DataLength = 6; |
||
| 129 | BIOS_CanSend(&txMsg); |
||
| 130 | } else if (proto.protocol == IR_PROTO_UNKNOWN) { |
||
| 131 | #if (SEND_DEBUG) |
||
| 132 | send_debug(rxbuffer, len); |
||
| 133 | proto.timeout=300; |
||
| 134 | #endif |
||
| 135 | |||
| 527 | arune | 136 | } |
| 137 | |||
| 611 | arune | 138 | state = STATE_START_PAUSE; |
| 139 | } |
||
| 140 | } else if (state == STATE_START_PAUSE) { |
||
| 614 | arune | 141 | IrTransceiver_Receive_Start(rxbuffer); |
| 611 | arune | 142 | timePauseStarted = Timebase_CurrentTime(); |
| 143 | state = STATE_PAUSING; |
||
| 144 | } else if (state == STATE_PAUSING) { |
||
| 616 | arune | 145 | //resetta timebase-var om ir finns |
| 614 | arune | 146 | uint8_t res = IrTransceiver_Receive_Poll(&len); |
| 147 | if (res == IR_NOT_FINISHED || res == IR_OK) { |
||
| 148 | timePauseStarted = Timebase_CurrentTime(); |
||
| 527 | arune | 149 | } |
| 614 | arune | 150 | if (res == IR_OK) { |
| 616 | arune | 151 | /* start a new reception */ |
| 614 | arune | 152 | IrTransceiver_Receive_Start(rxbuffer); |
| 153 | } |
||
| 611 | arune | 154 | |
| 155 | time = Timebase_CurrentTime(); |
||
| 156 | if (time - timePauseStarted >= proto.timeout) { |
||
| 614 | arune | 157 | state = STATE_START_IDLE; |
| 527 | arune | 158 | } |
| 614 | arune | 159 | } else if (state == STATE_START_IDLE) { |
| 160 | if (proto.protocol != IR_PROTO_UNKNOWN) { |
||
| 161 | //skicka på can |
||
| 162 | txMsg.Data.bytes[0] = IR_BUTTON_UP; |
||
| 163 | BIOS_CanSend(&txMsg); |
||
| 164 | } |
||
| 165 | state = STATE_IDLE; |
||
| 527 | arune | 166 | } |
| 403 | arune | 167 | |
| 611 | arune | 168 | |
| 614 | arune | 169 | |
| 403 | arune | 170 | if (rxMsgFull) { |
| 527 | arune | 171 | /* No message reception functionality implemented */ |
| 172 | /* Flush the received message */ |
||
| 403 | arune | 173 | rxMsgFull = 0; // |
| 174 | } |
||
| 175 | } |
||
| 176 | return 0; |
||
| 177 | } |