Subversion Repositories HomeAutomation

Rev

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

Rev 1764 Rev 1769
Line -... Line 1...
-
 
1
/* ----------------------------------------------------------------------------
1
 
2
 * Inclusions
-
 
3
 * --------------------------------------------------------------------------*/
2
#include "sns_Serial.h"
4
#include "sns_Serial.h"
3
 
5
 
4
#ifdef sns_Serial_USEEEPROM
-
 
5
#include "sns_Serial_eeprom.h"
-
 
6
struct eeprom_sns_Serial EEMEM eeprom_sns_Serial =
-
 
7
{
-
 
8
    {
-
 
9
        ///TODO: Define initialization values on the EEPROM variables here, this will generate a *.eep file that can be used to store this values to the node, can in future be done with a EEPROM module and the make-scrips. Write the values in the exact same order as the struct is defined in the *.h file.
-
 
10
        0xAB,   // x
-
 
11
        0x1234  // y
-
 
12
    },
-
 
13
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
-
 
14
};
-
 
15
#endif
-
 
16
 
6
 
-
 
7
/* ----------------------------------------------------------------------------
-
 
8
 * Definitions
-
 
9
 * --------------------------------------------------------------------------*/
-
 
10
#define sns_Serial_DEBUG 0
-
 
11
 
-
 
12
#ifndef min
-
 
13
#define min(_a,_b)      ((_a)<(_b) ? (_a) : (_b))
-
 
14
#endif
-
 
15
 
-
 
16
#ifndef max
-
 
17
#define max(_a,_b)      ((_a)>(_b) ? (_a) : (_b))
-
 
18
#endif
-
 
19
 
-
 
20
#ifndef lim
-
 
21
#define lim(_a,_minval,_maxval)     (max(min((_a),(_maxval)),(_minval)))
-
 
22
#endif
-
 
23
 
-
 
24
typedef enum {
-
 
25
    PARITY_NONE = 0,
-
 
26
    PARITY_EVEN = 1,
-
 
27
    PARITY_ODD = 2,
-
 
28
} parityMode_t;
-
 
29
 
-
 
30
 
-
 
31
/* ----------------------------------------------------------------------------
17
// internal variables
32
 * Variables
-
 
33
 * --------------------------------------------------------------------------*/
-
 
34
 
18
static uint32_t baudRate = 9600;
35
static uint32_t baudRate = 9600;
19
//static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK;
-
 
20
static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232;
36
static uint16_t format = CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_RS232;
21
static uint8_t databits = 8;
37
static uint8_t databits = 8;
22
static uint8_t stopbits = 1;
38
static uint8_t stopbits = 1;
23
 
-
 
24
static enum {
-
 
25
    PARITY_NONE = 0,
-
 
26
    PARITY_EVEN = 1,
-
 
27
    PARITY_ODD = 2,
-
 
28
} parityMode = 1;
39
static parityMode_t parityMode = PARITY_NONE;
29
 
-
 
30
// filter variables
-
 
31
static uint8_t dataTimeout = 0;
40
static uint8_t dataTimeout = 255;
32
static uint8_t packetLength = 0;
41
static uint8_t packetLength = 0;
33
 
42
 
34
static struct __attribute__ ((packed)) {
43
static struct __attribute__ ((packed)) {
35
    uint8_t prefixLength:2;
44
    uint8_t prefixLength:2;
36
    uint8_t suffixLength:2;
45
    uint8_t suffixLength:2;
37
    uint8_t prefixPattern[3];
46
    uint8_t prefixPattern[3];
38
    uint8_t suffixPattern[3];
47
    uint8_t suffixPattern[3];
39
} filter;
48
} filter;
40
 
49
 
41
// prepare a new message, in case new data is available
-
 
42
static StdCan_Msg_t msg;
50
static StdCan_Msg_t msg;
43
 
51
 
44
#define sns_Serial_DEBUG 0
52
#ifdef sns_Serial_USEEEPROM
-
 
53
#include "sns_Serial_eeprom.h"
-
 
54
struct eeprom_sns_Serial EEMEM eeprom_sns_Serial =
45
 
55
{
46
 
56
    {
-
 
57
        9600,   // baudRate
-
 
58
        CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK,    // format
-
 
59
        8,      // databits
47
#ifndef min
60
        1,      // stopbits
48
#define min(_a,_b)      ((_a)<(_b) ? (_a) : (_b))
61
        PARITY_NONE,    // parityMode
-
 
62
        255,    // dataTimeout
-
 
63
        8,      // packetLength
-
 
64
        0,      // prefixLength
-
 
65
        0,      // suffixLength
-
 
66
        {0,0,0},    // prefixPattern
-
 
67
        {0,0,0},    // suffixPattern
49
#endif
68
    },
-
 
69
    0   // crc, must be a correct value, but this will also be handled by the EEPROM module or make scripts
50
 
70
};
51
#ifndef max
-
 
52
#define max(_a,_b)      ((_a)>(_b) ? (_a) : (_b))
-
 
53
#endif
71
#endif
54
 
72
 
-
 
73
 
-
 
74
/* ----------------------------------------------------------------------------
55
#ifndef lim
75
 * Internal Functions
-
 
76
 * --------------------------------------------------------------------------*/
-
 
77
static uint16_t getTimeoutValue(void)
-
 
78
{
-
 
79
    // configurations 0-250 translate directly to milliseconds (0 means DIRECT)
-
 
80
    if (dataTimeout>=0 && dataTimeout<=250) {
-
 
81
        return dataTimeout;    
-
 
82
    }
-
 
83
    // configurations 251..254 are baudrate-dependent
-
 
84
    else if (dataTimeout>=251 && dataTimeout<=254) {
56
#define lim(_a,_minval,_maxval)     (max(min((_a),(_maxval)),(_minval)))
85
        return (uint16_t)(baudRate/(uint32_t)1000UL*((uint32_t)dataTimeout-250UL));
-
 
86
    }
-
 
87
    // configuration 255 means use the compiled default value
57
#endif
88
    else {
-
 
89
        return sns_Serial_FORCE_SEND_TIME_MS;
-
 
90
    }
-
 
91
}
58
 
92
 
59
 
93
 
-
 
94
/* ----------------------------------------------------------------------------
-
 
95
 * Function Implementations
-
 
96
 * --------------------------------------------------------------------------*/
60
 
97
 
61
uint8_t sns_Serial_setSettings(void)
98
uint8_t sns_Serial_setSettings(void)
62
{
99
{
63
    uint8_t returnval = 1;
100
    uint8_t returnval = 1;
64
   
101
   
Line 126... Line 163...
126
    else
163
    else
127
    {
164
    {
128
        //invalid format
165
        //invalid format
129
#if sns_Serial_DEBUG==1
166
#if sns_Serial_DEBUG==1
130
        printf("ERROR!\n");
167
        printf("ERROR!\n");
131
#endif
168
#endif
132
        //return 0, not successful
169
        //return 0, not successful
133
        returnval = 0;
170
        returnval = 0;
134
    }
171
    }
135
   
172
   
136
    return returnval;
173
    return returnval;
137
}
174
}
138
 
175
 
-
 
176
 
139
void sns_Serial_Init(void)
177
void sns_Serial_Init()
140
{
178
{
141
#ifdef sns_Serial_USEEEPROM
179
#ifdef sns_Serial_USEEEPROM
142
    //if (EEDATA_OK) {
180
    if (EEDATA_OK) {
143
    if (0) {
181
    //if (0) {
144
#if ((__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1)
182
#if ((__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ == 6 && __AVR_LIBC_REVISION__ >=2)||(__AVR_LIBC_MAJOR__ == 1  && __AVR_LIBC_MINOR__ > 6)||__AVR_LIBC_MAJOR__ > 1)
145
        baudRate = eeprom_read_dword(EEDATA32.baudRate);
183
        baudRate = eeprom_read_dword(EEDATA32.baudRate);
146
#else
184
#else
147
#warning This version of AVR-libc does not have support for eeprom read dword
185
#warning This version of AVR-libc does not have support for eeprom read dword
148
#endif
186
#endif
Line 170... Line 208...
170
#endif
208
#endif
171
        eeprom_write_word_crc(EEDATA16.format, CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK, WITHOUT_CRC);
209
        eeprom_write_word_crc(EEDATA16.format, CAN_MODULE_ENUM_SERIAL_SERIALCONFIG_PHYSICALFORMAT_LOOPBACK, WITHOUT_CRC);
172
        eeprom_write_byte_crc(EEDATA.databits, 8, WITHOUT_CRC);
210
        eeprom_write_byte_crc(EEDATA.databits, 8, WITHOUT_CRC);
173
        eeprom_write_byte_crc(EEDATA.stopbits, 1, WITHOUT_CRC);
211
        eeprom_write_byte_crc(EEDATA.stopbits, 1, WITHOUT_CRC);
174
        eeprom_write_byte_crc(EEDATA.parityMode, PARITY_NONE, WITHOUT_CRC);
212
        eeprom_write_byte_crc(EEDATA.parityMode, PARITY_NONE, WITHOUT_CRC);
175
        eeprom_write_byte_crc(EEDATA.dataTimeout, 0, WITHOUT_CRC);
213
        eeprom_write_byte_crc(EEDATA.dataTimeout, 255, WITHOUT_CRC);
176
        eeprom_write_byte_crc(EEDATA.packetLength, 8, WITHOUT_CRC);
214
        eeprom_write_byte_crc(EEDATA.packetLength, 8, WITHOUT_CRC);
177
        eeprom_write_byte_crc(EEDATA.prefixLength, 0, WITHOUT_CRC);
215
        eeprom_write_byte_crc(EEDATA.prefixLength, 0, WITHOUT_CRC);
178
        eeprom_write_byte_crc(EEDATA.suffixLength, 0, WITHOUT_CRC);
216
        eeprom_write_byte_crc(EEDATA.suffixLength, 0, WITHOUT_CRC);
179
        eeprom_write_byte_crc(EEDATA.prefixPattern[0], 0, WITHOUT_CRC);
217
        eeprom_write_byte_crc(EEDATA.prefixPattern[0], 0, WITHOUT_CRC);
180
        eeprom_write_byte_crc(EEDATA.prefixPattern[1], 0, WITHOUT_CRC);
218
        eeprom_write_byte_crc(EEDATA.prefixPattern[1], 0, WITHOUT_CRC);
Line 207... Line 245...
207
    msg.Length = 0; // no data so far
245
    msg.Length = 0; // no data so far
208
   
246
   
209
    //gpio_set_pin(sns_Serial_TXEN);            // enable TX
247
    //gpio_set_pin(sns_Serial_TXEN);            // enable TX
210
    //uart_putc('7');
248
    //uart_putc('7');
211
}
249
}
-
 
250
 
212
 
251
 
213
void sns_Serial_Process(void)
252
void sns_Serial_Process(void)
214
{
253
{
215
    StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
254
    StdCan_Set_class(msg.Header, CAN_MODULE_CLASS_SNS);
216
    StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER);
255
    StdCan_Set_direction(msg.Header, DIRECTIONFLAG_TO_OWNER);
Line 224... Line 263...
224
    {
263
    {
225
        // ask uart driver for more data
264
        // ask uart driver for more data
226
        unsigned int data = uart_getc();
265
        unsigned int data = uart_getc();
227
        // character is contained in LSB
266
        // character is contained in LSB
228
        unsigned char c = (unsigned char)(data & 0x00FF);
267
        unsigned char c = (unsigned char)(data & 0x00FF);
-
 
268
 
229
        // status is contained in MSB
269
        // status is contained in MSB
230
        status = (unsigned char)((data & 0xFF00) >> 8);
270
        status = (unsigned char)((data & 0xFF00) >> 8);
231
       
271
       
232
        // status == 0 means we just received a new char
272
        // status == 0 means we just received a new char
233
        if (status == 0)
273
        if (status == 0)
Line 236... Line 276...
236
            printf("RX\n");
276
            printf("RX\n");
237
#endif
277
#endif
238
            // insert it into the message
278
            // insert it into the message
239
            msg.Data[(uint8_t)msg.Length] = c;
279
            msg.Data[(uint8_t)msg.Length] = c;
240
            msg.Length++;
280
            msg.Length++;
241
           
281
           
-
 
282
            uint16_t timeout;
242
#if sns_Serial_FORCE_SEND_TIME_MS > 0
283
            if ((timeout = getTimeoutValue()) != 0) {
243
            Timer_SetTimeout(sns_Serial_FORCE_SEND_TIMER, sns_Serial_FORCE_SEND_TIME_MS , TimerTypeOneShot, 0);
284
                Timer_SetTimeout(sns_Serial_FORCE_SEND_TIMER, timeout , TimerTypeOneShot, 0);
244
#endif
285
            }
245
        }
286
        }
246
    // keep going until max data length reached, or until uart RXBUF is empty
287
    // keep going until max data length reached, or until uart RXBUF is empty
247
    } while (status == 0 && msg.Length < packetLength);
288
    } while (status == 0 && msg.Length < packetLength);
248
   
289
   
249
    // check if force send or send-frame full
290
    // check if force send or send-frame full
250
    if ((Timer_Expired(sns_Serial_FORCE_SEND_TIMER) && msg.Length>0) || msg.Length == packetLength || (sns_Serial_FORCE_SEND_TIME_MS == 0 && msg.Length>0))
291
    if ((Timer_Expired(sns_Serial_FORCE_SEND_TIMER) && msg.Length>0) || msg.Length==packetLength || (getTimeoutValue()==0 && msg.Length>0))
251
    {
292
    {
252
        // transmit this chunk of data (max 8 chars)
293
        // transmit this chunk of data (max 8 chars)
253
        while(StdCan_Put(&msg) != StdCan_Ret_OK);
294
        while(StdCan_Put(&msg) != StdCan_Ret_OK);
254
        msg.Length = 0; // no data so far
295
        msg.Length = 0; // no data so far
255
    }
296
    }
256
}
297
}
-
 
298
 
257
 
299
 
258
void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
300
void sns_Serial_HandleMessage(StdCan_Msg_t *rxMsg)
259
{
301
{
260
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
302
    if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
261
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
303
        StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_FROM_OWNER &&
262
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
304
        rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_SERIAL &&
263
        rxMsg->Header.ModuleId == sns_Serial_ID)
305
        rxMsg->Header.ModuleId == sns_Serial_ID)
264
    {
306
    {
Line 335... Line 377...
335
               
377
               
336
                break;
378
                break;
337
        }
379
        }
338
    }
380
    }
339
}
381
}
-
 
382
 
340
 
383
 
341
void sns_Serial_List(uint8_t ModuleSequenceNumber)
384
void sns_Serial_List(uint8_t ModuleSequenceNumber)
342
{
385
{
343
    StdCan_Msg_t txMsg;
386
    StdCan_Msg_t txMsg;
344
 
387