Subversion Repositories HomeAutomation

Rev

Go to most recent revision | Details | 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.
7
#include <drivers/uart/serial.h>
8
#include <drivers/timer/timer.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
 
39
#define STATE_IDLE          0
40
#define STATE_GET_DATA      1
41
#define STATE_WAIT1         2
42
#define STATE_WAIT2         3
43
#define STATE_STOP          4
44
 
45
#define APP_TYPE    0xf001
46
#define APP_VERSION 0x0002
47
 
48
// A simple message "queue", with space for one message only.
49
// These are declared volatile to tell the compiler not to optimize away accesses.
50
volatile Can_Message_t rxMsg; // Message storage
51
volatile uint8_t rxMsgFull;   // Synchronization flag
52
 
53
// CAN message reception callback.
54
// This function runs with interrupts disabled, keep it as short as possible.
55
void can_receive(Can_Message_t *msg) {
56
    if (!rxMsgFull) {
57
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
58
        rxMsgFull = 1;
59
    }
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
}
74
 
75
int main(void)
76
{
77
    // Enable interrupts as early as possible
78
    sei();
79
 
80
    Timer_Init();
81
    Serial_Init();
82
 
83
    Can_Message_t txMsg;
84
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
85
    txMsg.DataLength = 4;
86
    txMsg.RemoteFlag = 0;
87
    txMsg.ExtendedFlag = 1;
88
    txMsg.Data.words[0] = APP_TYPE;
89
    txMsg.Data.words[1] = APP_VERSION;
90
 
91
    // Set up callback for CAN reception, this is optional if only sending is required.
92
    BIOS_CanCallback = &can_receive;
93
    // Send CAN_NMT_APP_START
94
    BIOS_CanSend(&txMsg);
95
 
96
    //set ports to input
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
 
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;
111
    uint8_t datain = 0;
112
    uint8_t state=0;
113
 
114
    while (1) {
115
 
116
        if (state == STATE_IDLE) {
117
            if ((MT_StD_PIN & (1<<MT_StD_BIT)) != 0) {
118
                state = STATE_GET_DATA;
119
            }
120
        } else if (state == STATE_GET_DATA) {
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
136
 
137
            if ((receviercounter & 0x1) == 0) {
138
                txMsg.Data.bytes[(receviercounter>>1)] = (datain<<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)) {
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);
167
                } else {
168
                    txMsg.DataLength = (receviercounter>>1)+1;
169
                    //txMsg.Data.bytes[(receviercounter>>1)] |= 0xc; //spara ett extra c om udda antal
170
                }
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));
172
                BIOS_CanSend(&txMsg);
173
            }              
174
 
175
            receviercounter = 0;
176
            state = STATE_IDLE;
177
        }
178
 
179
    }
180
 
181
    return 0;
182
}