Rev 884 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 620 | 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. |
||
| 884 | arune | 7 | #include <drivers/DTMF/dtmf.h> |
| 8 | //#include <drivers/DTMF/mt8870/mt8870.h> |
||
| 9 | #include <drivers/timer/timer.h> |
||
| 620 | arune | 10 | |
| 11 | #define STATE_IDLE 0 |
||
| 884 | arune | 12 | #define STATE_COPYDATA 1 |
| 13 | #define STATE_WAIT 2 |
||
| 14 | #define STATE_SEND 3 |
||
| 620 | arune | 15 | |
| 16 | #define APP_TYPE 0xf001 |
||
| 884 | arune | 17 | #define APP_VERSION 0x0003 |
| 620 | arune | 18 | |
| 19 | // A simple message "queue", with space for one message only. |
||
| 20 | // These are declared volatile to tell the compiler not to optimize away accesses. |
||
| 21 | volatile Can_Message_t rxMsg; // Message storage |
||
| 22 | volatile uint8_t rxMsgFull; // Synchronization flag |
||
| 23 | |||
| 24 | // CAN message reception callback. |
||
| 25 | // This function runs with interrupts disabled, keep it as short as possible. |
||
| 26 | void can_receive(Can_Message_t *msg) { |
||
| 27 | if (!rxMsgFull) { |
||
| 28 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
||
| 29 | rxMsgFull = 1; |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | int main(void) |
||
| 34 | { |
||
| 35 | // Enable interrupts as early as possible |
||
| 36 | sei(); |
||
| 37 | |||
| 884 | arune | 38 | Timer_Init(); |
| 39 | |||
| 40 | DTMFin_Init(); |
||
| 41 | |||
| 620 | arune | 42 | Can_Message_t txMsg; |
| 43 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
||
| 44 | txMsg.DataLength = 4; |
||
| 45 | txMsg.RemoteFlag = 0; |
||
| 46 | txMsg.ExtendedFlag = 1; |
||
| 47 | txMsg.Data.words[0] = APP_TYPE; |
||
| 48 | txMsg.Data.words[1] = APP_VERSION; |
||
| 49 | |||
| 50 | // Set up callback for CAN reception, this is optional if only sending is required. |
||
| 51 | BIOS_CanCallback = &can_receive; |
||
| 52 | // Send CAN_NMT_APP_START |
||
| 53 | BIOS_CanSend(&txMsg); |
||
| 54 | |||
| 55 | |||
| 884 | arune | 56 | uint8_t pollrxbuffer[MT_MAX_TONES]; |
| 57 | uint8_t pollrxlen=0; |
||
| 58 | uint8_t rxbuffer[MT_MAX_TONES]; |
||
| 624 | arune | 59 | uint8_t rxlen=0; |
| 884 | arune | 60 | uint8_t state=STATE_IDLE; |
| 620 | arune | 61 | |
| 62 | while (1) { |
||
| 63 | if (state == STATE_IDLE) { |
||
| 884 | arune | 64 | if (DTMFin_Pop(pollrxbuffer, &pollrxlen) == DTMF_Ret_Data_avail) { |
| 65 | state = STATE_COPYDATA; |
||
| 66 | } else { |
||
| 67 | //go into sleep or something, incoming data from mt8870 is interrupted so we will wake up |
||
| 68 | } |
||
| 69 | } else if (state == STATE_COPYDATA) { |
||
| 624 | arune | 70 | state = STATE_WAIT; |
| 884 | arune | 71 | for (uint8_t i=0; i<pollrxlen;i++) { |
| 72 | rxbuffer[rxlen++] = pollrxbuffer[i]; |
||
| 73 | if (rxlen >= MT_MAX_TONES) { |
||
| 74 | //overflow |
||
| 75 | rxlen = 0; |
||
| 76 | state = STATE_IDLE; |
||
| 77 | break; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | Timer_SetTimeout(0, MT_DIAL_TIMEOUT, TimerTypeOneShot, 0); |
||
| 624 | arune | 81 | } else if (state == STATE_WAIT) { |
| 884 | arune | 82 | if (Timer_Expired(0)) { |
| 83 | state = STATE_SEND; |
||
| 84 | } |
||
| 85 | |||
| 86 | DTMF_Ret_t retval = DTMFin_Pop(pollrxbuffer, &pollrxlen); |
||
| 87 | if (retval == DTMF_Ret_Data_avail) { |
||
| 88 | state = STATE_COPYDATA; |
||
| 89 | } else if (retval == DTMF_Ret_Overflow) { |
||
| 90 | rxlen=0; |
||
| 624 | arune | 91 | state = STATE_IDLE; |
| 620 | arune | 92 | } |
| 884 | arune | 93 | } else if (state == STATE_SEND) { |
| 624 | arune | 94 | //if ((rxbuffer[0] == 0xa || rxbuffer[0] == 0xb) && rxbuffer[rxlen-1] == 0xc) { |
| 884 | arune | 95 | for (uint8_t i = 0; i < 16; i=i+2) { |
| 96 | txMsg.Data.bytes[i>>1] = rxbuffer[i]<<4; |
||
| 97 | if (i+1 < rxlen) { |
||
| 98 | txMsg.Data.bytes[i>>1] |= rxbuffer[i+1]; |
||
| 620 | arune | 99 | } |
| 884 | arune | 100 | } |
| 101 | txMsg.DataLength = (rxlen>>1)+(rxlen&1); |
||
| 102 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_PHONENR << CAN_SHIFT_SNS_TYPE) | (0<<CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
||
| 103 | BIOS_CanSend(&txMsg); |
||
| 624 | arune | 104 | |
| 105 | //} |
||
| 884 | arune | 106 | rxlen = 0; |
| 620 | arune | 107 | state = STATE_IDLE; |
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | return 0; |
||
| 112 | } |