Subversion Repositories HomeAutomation

Rev

Rev 620 | Rev 625 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 620 Rev 624
Line 3... Line 3...
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/timer/timer.h>
8
#include <drivers/callerid/mt8870/mt8870.h>
9
 
-
 
10
/*
-
 
11
 * De få signaler som man behöver koppla in från MT8870 är
-
 
12
 * StD, Q1, Q2, Q3 och Q4
-
 
13
 */
-
 
14
 
-
 
15
#define MT_StD_PIN      PIND
-
 
16
#define MT_StD_DDR      DDRD
-
 
17
#define MT_StD_BIT      PD2
-
 
18
 
-
 
19
#define MT_Q1_PIN       PINC
-
 
20
#define MT_Q1_DDR       DDRC
-
 
21
#define MT_Q1_BIT       PC0
-
 
22
 
-
 
23
#define MT_Q2_PIN       PINC
-
 
24
#define MT_Q2_DDR       DDRC
-
 
25
#define MT_Q2_BIT       PC1
-
 
26
 
-
 
27
#define MT_Q3_PIN       PINC
-
 
28
#define MT_Q3_DDR       DDRC
-
 
29
#define MT_Q3_BIT       PC2
-
 
30
 
-
 
31
#define MT_Q4_PIN       PINC
-
 
32
#define MT_Q4_DDR       DDRC
-
 
33
#define MT_Q4_BIT       PC3
-
 
34
 
-
 
35
#define DIAL_TIMEOUT    1800
-
 
36
 
-
 
37
 
-
 
38
 
9
 
39
#define STATE_IDLE          0
10
#define STATE_IDLE          0
40
#define STATE_GET_DATA      1
11
#define STATE_GET_DATA      1
41
#define STATE_WAIT1         2
12
#define STATE_START_WAIT    2
42
#define STATE_WAIT2         3
13
#define STATE_WAIT          3
43
#define STATE_STOP          4
14
#define STATE_STOP          4
44
 
15
 
45
#define APP_TYPE    0xf001
16
#define APP_TYPE    0xf001
46
#define APP_VERSION 0x0002
17
#define APP_VERSION 0x0002
47
 
18
 
Line 55... Line 26...
55
void can_receive(Can_Message_t *msg) {
26
void can_receive(Can_Message_t *msg) {
56
    if (!rxMsgFull) {
27
    if (!rxMsgFull) {
57
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
28
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
58
        rxMsgFull = 1;
29
        rxMsgFull = 1;
59
    }
30
    }
60
}
-
 
61
 
-
 
62
// Timer callback function used for some timer tests
-
 
63
void timer_callback(uint8_t timer) {
-
 
64
    Can_Message_t msg;
-
 
65
   
-
 
66
    msg.ExtendedFlag = 1;
-
 
67
    msg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID;
-
 
68
    msg.RemoteFlag = 0;
-
 
69
    msg.DataLength = 1;
-
 
70
    msg.Data.bytes[0] = timer;
-
 
71
   
-
 
72
    BIOS_CanSend(&msg);
-
 
73
}
31
}
74
 
32
 
75
int main(void)
33
int main(void)
76
{
34
{
77
    // Enable interrupts as early as possible
35
    // Enable interrupts as early as possible
78
    sei();
36
    sei();
79
   
37
   
80
    Timer_Init();
-
 
81
    Serial_Init();
38
    Serial_Init();
82
   
39
   
83
    Can_Message_t txMsg;
40
    Can_Message_t txMsg;
84
    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);
85
    txMsg.DataLength = 4;
42
    txMsg.DataLength = 4;
Line 91... Line 48...
91
    // 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.
92
    BIOS_CanCallback = &can_receive;
49
    BIOS_CanCallback = &can_receive;
93
    // Send CAN_NMT_APP_START
50
    // Send CAN_NMT_APP_START
94
    BIOS_CanSend(&txMsg);
51
    BIOS_CanSend(&txMsg);
95
   
52
   
96
    //set ports to input
53
    Callerid_Init();
97
    MT_StD_DDR &= ~(1<<MT_StD_BIT);
-
 
98
    MT_Q1_DDR &= ~(1<<MT_Q1_BIT);
-
 
99
    MT_Q2_DDR &= ~(1<<MT_Q2_BIT);
-
 
100
    MT_Q3_DDR &= ~(1<<MT_Q3_BIT);
-
 
101
    MT_Q4_DDR &= ~(1<<MT_Q4_BIT);
-
 
102
 
54
 
103
   
-
 
104
    printf("AVR Test Application\n");
-
 
105
   
-
 
106
    txMsg.Id = (CAN_TST << CAN_SHIFT_CLASS) | NODE_ID;
-
 
107
    txMsg.Data.dwords[0] = 0x01020304;
-
 
108
    txMsg.DataLength = 8;
-
 
109
 
-
 
110
    uint8_t receviercounter = 0;
55
    uint8_t rxbuffer[MAX_TONES];
111
    uint8_t datain = 0;
56
    uint8_t state=0;
112
    uint8_t state=0;
57
    uint8_t rxlen=0;
113
 
58
 
114
    while (1) {
59
    while (1) {
115
       
60
       
116
        if (state == STATE_IDLE) {
61
        if (state == STATE_IDLE) {
-
 
62
            Callerid_Start(rxbuffer);
-
 
63
            state = STATE_START_WAIT;
-
 
64
        } else if (state == STATE_START_WAIT) {
-
 
65
            state = STATE_WAIT;
-
 
66
        } else if (state == STATE_WAIT) {
-
 
67
            uint8_t retval = Callerid_Poll(&rxlen);
117
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
68
            if (retval == RET_FINISHED) {
-
 
69
                state = STATE_STOP;
-
 
70
            } else if (retval == RET_OVERFLOW) {
118
                state = STATE_GET_DATA;
71
                state = STATE_IDLE;
119
            }
72
            }
120
        } else if (state == STATE_GET_DATA) {
73
        } else if (state == STATE_STOP) {
121
            //nu finns data tillgänglig på Qx
-
 
122
            datain = ((MT_Q1_PIN >> MT_Q1_BIT) & 0x1);
-
 
123
            datain |= (((MT_Q2_PIN >> MT_Q2_BIT) & 0x1)<<1);
-
 
124
            datain |= (((MT_Q3_PIN >> MT_Q3_BIT) & 0x1)<<2);
-
 
125
            datain |= (((MT_Q4_PIN >> MT_Q4_BIT) & 0x1)<<3);
-
 
126
            if (datain == 0) { datain = 0x1d; }     //DTMF0  = D    //this is temporary
-
 
127
            if (datain == 11) { datain = 0x1e; }    //DTMF12 = *    //this is temporary
-
 
128
            if (datain == 12) { datain = 0x1f; }    //DTMF11 = #    //this is temporary
-
 
129
            if (datain == 15) { datain = 0xc; }     //DTMF11 = C
-
 
130
            if (datain == 10) { datain = 0; }       //DTMF10 = number 0
-
 
131
            if (datain == 13) { datain = 0xa; }     //DTMF13 = A
-
 
132
            if (datain == 14) { datain = 0xb; }     //DTMF14 = B
-
 
133
            if (datain == 0x1d) { datain = 0xd; }   //DTMF0  = D    //now permanent
-
 
134
            if (datain == 0x1e) { datain = 0xe; }   //DTMF12 = *    //now permanent
-
 
135
            if (datain == 0x1f) { datain = 0xf; }   //DTMF11 = #    //now permanent
74
            //if ((rxbuffer[0] == 0xa || rxbuffer[0] == 0xb) && rxbuffer[rxlen-1] == 0xc) {
136
       
-
 
137
            if ((receviercounter & 0x1) == 0) {
75
                for (uint8_t i = 0; i < 16; i=i+2) {
138
                txMsg.Data.bytes[(receviercounter>>1)] = (datain<<4);
76
                    txMsg.Data.bytes[i>>1] = rxbuffer[i]<<4;
139
            } else {
-
 
140
                txMsg.Data.bytes[(receviercounter>>1)] |= datain;
-
 
141
            }
-
 
142
            receviercounter++;
-
 
143
 
-
 
144
            state = STATE_WAIT1;
-
 
145
        } else if (state == STATE_WAIT1) {
-
 
146
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) == 0) {
-
 
147
                Timer_SetTimeout(0, DIAL_TIMEOUT, TimerTypeOneShot, 0);
-
 
148
                state = STATE_WAIT2;
-
 
149
            }  
-
 
150
           
-
 
151
        } else if (state == STATE_WAIT2) {
-
 
152
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
-
 
153
                if (receviercounter != 16) {
-
 
154
                    state = STATE_GET_DATA;
-
 
155
                } else {
-
 
156
                    state = STATE_WAIT1;
-
 
157
                }
-
 
158
            }
-
 
159
           
-
 
160
            if (Timer_Expired(0)) {
77
                    if (i+1 < rxlen) {
161
                state = STATE_STOP;
-
 
162
            }
-
 
163
        } else if (state == STATE_STOP) {
-
 
164
            if (datain == 0xc) {    //om sista tonen var c
-
 
165
                if ((receviercounter & 0x1) == 0) {
-
 
166
                    txMsg.DataLength = (receviercounter>>1);
78
                        txMsg.Data.bytes[i>>1] |= rxbuffer[i+1];
167
                } else {
-
 
168
                    txMsg.DataLength = (receviercounter>>1)+1;
-
 
169
                    //txMsg.Data.bytes[(receviercounter>>1)] |= 0xc; //spara ett extra c om udda antal
-
 
170
                }
79
                    }
-
 
80
                }
-
 
81
                txMsg.DataLength = (rxlen>>1)+(rxlen&1);
171
                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));
172
                BIOS_CanSend(&txMsg);
83
                BIOS_CanSend(&txMsg);
173
            }              
84
   
174
           
85
            //}
175
            receviercounter = 0;
-
 
176
            state = STATE_IDLE;
86
            state = STATE_IDLE;
177
        }
87
        }
178
       
-
 
179
    }
88
    }
180
   
89
   
181
    return 0;
90
    return 0;
182
}
91
}