Rev 625 | Rev 815 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 625 | Rev 694 | ||
|---|---|---|---|
| 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/ |
8 | #include <drivers/DTMF/mt8870/mt8870.h> |
| 9 | 9 | ||
| 10 | #define STATE_IDLE 0 |
10 | #define STATE_IDLE 0 |
| 11 | #define STATE_GET_DATA 1 |
11 | #define STATE_GET_DATA 1 |
| 12 | #define STATE_START_WAIT 2 |
12 | #define STATE_START_WAIT 2 |
| 13 | #define STATE_WAIT 3 |
13 | #define STATE_WAIT 3 |
| 14 | #define STATE_STOP 4 |
14 | #define STATE_STOP 4 |
| 15 | 15 | ||
| 16 | #define APP_TYPE 0xf001 |
16 | #define APP_TYPE 0xf001 |
| 17 | #define APP_VERSION 0x0002 |
17 | #define APP_VERSION 0x0002 |
| 18 | 18 | ||
| 19 | // A simple message "queue", with space for one message only. |
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. |
20 | // These are declared volatile to tell the compiler not to optimize away accesses. |
| 21 | volatile Can_Message_t rxMsg; // Message storage |
21 | volatile Can_Message_t rxMsg; // Message storage |
| 22 | volatile uint8_t rxMsgFull; // Synchronization flag |
22 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 23 | 23 | ||
| 24 | // CAN message reception callback. |
24 | // CAN message reception callback. |
| 25 | // This function runs with interrupts disabled, keep it as short as possible. |
25 | // This function runs with interrupts disabled, keep it as short as possible. |
| 26 | void can_receive(Can_Message_t *msg) { |
26 | void can_receive(Can_Message_t *msg) { |
| 27 | if (!rxMsgFull) { |
27 | if (!rxMsgFull) { |
| 28 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
28 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
| 29 | rxMsgFull = 1; |
29 | rxMsgFull = 1; |
| 30 | } |
30 | } |
| 31 | } |
31 | } |
| 32 | 32 | ||
| 33 | int main(void) |
33 | int main(void) |
| 34 | { |
34 | { |
| 35 | // Enable interrupts as early as possible |
35 | // Enable interrupts as early as possible |
| 36 | sei(); |
36 | sei(); |
| 37 | 37 | ||
| 38 | Serial_Init(); |
38 | Serial_Init(); |
| 39 | 39 | ||
| 40 | Can_Message_t txMsg; |
40 | Can_Message_t txMsg; |
| 41 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
41 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 42 | txMsg.DataLength = 4; |
42 | txMsg.DataLength = 4; |
| 43 | txMsg.RemoteFlag = 0; |
43 | txMsg.RemoteFlag = 0; |
| 44 | txMsg.ExtendedFlag = 1; |
44 | txMsg.ExtendedFlag = 1; |
| 45 | txMsg.Data.words[0] = APP_TYPE; |
45 | txMsg.Data.words[0] = APP_TYPE; |
| 46 | txMsg.Data.words[1] = APP_VERSION; |
46 | txMsg.Data.words[1] = APP_VERSION; |
| 47 | 47 | ||
| 48 | // Set up callback for CAN reception, this is optional if only sending is required. |
48 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 49 | BIOS_CanCallback = &can_receive; |
49 | BIOS_CanCallback = &can_receive; |
| 50 | // Send CAN_NMT_APP_START |
50 | // Send CAN_NMT_APP_START |
| 51 | BIOS_CanSend(&txMsg); |
51 | BIOS_CanSend(&txMsg); |
| 52 | 52 | ||
| 53 | DTMFin_Init(); |
53 | DTMFin_Init(); |
| 54 | 54 | ||
| 55 | uint8_t rxbuffer[MAX_TONES]; |
55 | uint8_t rxbuffer[MAX_TONES]; |
| 56 | uint8_t state=0; |
56 | uint8_t state=0; |
| 57 | uint8_t rxlen=0; |
57 | uint8_t rxlen=0; |
| 58 | 58 | ||
| 59 | while (1) { |
59 | while (1) { |
| 60 | 60 | ||
| 61 | if (state == STATE_IDLE) { |
61 | if (state == STATE_IDLE) { |
| 62 | DTMFin_Start(rxbuffer); |
62 | DTMFin_Start(rxbuffer); |
| 63 | state = STATE_START_WAIT; |
63 | state = STATE_START_WAIT; |
| 64 | } else if (state == STATE_START_WAIT) { |
64 | } else if (state == STATE_START_WAIT) { |
| 65 | state = STATE_WAIT; |
65 | state = STATE_WAIT; |
| 66 | } else if (state == STATE_WAIT) { |
66 | } else if (state == STATE_WAIT) { |
| 67 | uint8_t retval = DTMFin_Poll(&rxlen); |
67 | uint8_t retval = DTMFin_Poll(&rxlen); |
| 68 | if (retval == RET_FINISHED) { |
68 | if (retval == RET_FINISHED) { |
| 69 | state = STATE_STOP; |
69 | state = STATE_STOP; |
| 70 | } else if (retval == RET_OVERFLOW) { |
70 | } else if (retval == RET_OVERFLOW) { |
| 71 | state = STATE_IDLE; |
71 | state = STATE_IDLE; |
| 72 | } |
72 | } |
| 73 | } else if (state == STATE_STOP) { |
73 | } else if (state == STATE_STOP) { |
| 74 | //if ((rxbuffer[0] == 0xa || rxbuffer[0] == 0xb) && rxbuffer[rxlen-1] == 0xc) { |
74 | //if ((rxbuffer[0] == 0xa || rxbuffer[0] == 0xb) && rxbuffer[rxlen-1] == 0xc) { |
| 75 | for (uint8_t i = 0; i < 16; i=i+2) { |
75 | for (uint8_t i = 0; i < 16; i=i+2) { |
| 76 | txMsg.Data.bytes[i>>1] = rxbuffer[i]<<4; |
76 | txMsg.Data.bytes[i>>1] = rxbuffer[i]<<4; |
| 77 | if (i+1 < rxlen) { |
77 | if (i+1 < rxlen) { |
| 78 | txMsg.Data.bytes[i>>1] |= rxbuffer[i+1]; |
78 | txMsg.Data.bytes[i>>1] |= rxbuffer[i+1]; |
| 79 | } |
79 | } |
| 80 | } |
80 | } |
| 81 | txMsg.DataLength = (rxlen>>1)+(rxlen&1); |
81 | txMsg.DataLength = (rxlen>>1)+(rxlen&1); |
| 82 | 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)); |
82 | 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)); |
| 83 | BIOS_CanSend(&txMsg); |
83 | BIOS_CanSend(&txMsg); |
| 84 | 84 | ||
| 85 | //} |
85 | //} |
| 86 | state = STATE_IDLE; |
86 | state = STATE_IDLE; |
| 87 | } |
87 | } |
| 88 | } |
88 | } |
| 89 | 89 | ||
| 90 | return 0; |
90 | return 0; |
| 91 | } |
91 | } |
| 92 | 92 | ||