Rev 1044 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1044 | Rev 1045 | ||
|---|---|---|---|
| 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> |
- | |
| 8 | #include <drivers/timer/timer.h> |
7 | #include <drivers/timer/timer.h> |
| 9 | #include <drivers/adc/adc.h> |
8 | #include <drivers/adc/adc.h> |
| 10 | 9 | ||
| 11 | #define APP_TYPE 0xf001 |
10 | #define APP_TYPE 0xf001 |
| 12 | #define APP_VERSION 0x0002 |
11 | #define APP_VERSION 0x0002 |
| 13 | 12 | ||
| 14 | 13 | ||
| 15 | #define R51 12000 |
14 | #define R51 12000 |
| 16 | #define R50 47000 |
15 | #define R50 47000 |
| 17 | 16 | ||
| 18 | #if (5 * (R51 + R50))/(R51) > 64 |
17 | #if (5 * (R51 + R50))/(R51) > 64 |
| 19 | #error "ADC_FACTOR must be less then 64, change R51 and R52" |
18 | #error "ADC_FACTOR must be less then 64, change R51 and R52" |
| 20 | #else |
19 | #else |
| 21 | #define ADC_FACTOR (5 * (R51 + R50))/(R51) |
20 | #define ADC_FACTOR (5 * (R51 + R50))/(R51) |
| 22 | #define ADC_SCALE 10 |
21 | #define ADC_SCALE 10 |
| 23 | #endif |
22 | #endif |
| 24 | 23 | ||
| 25 | // A simple message "queue", with space for one message only. |
24 | // A simple message "queue", with space for one message only. |
| 26 | // These are declared volatile to tell the compiler not to optimize away accesses. |
25 | // These are declared volatile to tell the compiler not to optimize away accesses. |
| 27 | volatile Can_Message_t rxMsg; // Message storage |
26 | volatile Can_Message_t rxMsg; // Message storage |
| 28 | volatile uint8_t rxMsgFull; // Synchronization flag |
27 | volatile uint8_t rxMsgFull; // Synchronization flag |
| 29 | 28 | ||
| 30 | // CAN message reception callback. |
29 | // CAN message reception callback. |
| 31 | // This function runs with interrupts disabled, keep it as short as possible. |
30 | // This function runs with interrupts disabled, keep it as short as possible. |
| 32 | void can_receive(Can_Message_t *msg) { |
31 | void can_receive(Can_Message_t *msg) { |
| 33 | if (!rxMsgFull) { |
32 | if (!rxMsgFull) { |
| 34 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
33 | memcpy((void*)&rxMsg, msg, sizeof(rxMsg)); |
| 35 | rxMsgFull = 1; |
34 | rxMsgFull = 1; |
| 36 | } |
35 | } |
| 37 | } |
36 | } |
| 38 | 37 | ||
| 39 | // Timer callback function used for some timer tests |
38 | // Timer callback function used for some timer tests |
| 40 | void timer_callback(uint8_t timer) { |
39 | void timer_callback(uint8_t timer) { |
| 41 | } |
40 | } |
| 42 | 41 | ||
| 43 | int main(void) |
42 | int main(void) |
| 44 | { |
43 | { |
| 45 | // Enable interrupts as early as possible |
44 | // Enable interrupts as early as possible |
| 46 | sei(); |
45 | sei(); |
| 47 | 46 | ||
| 48 | Timer_Init(); |
47 | Timer_Init(); |
| 49 | Serial_Init(); |
- | |
| 50 | ADC_Init(); |
48 | ADC_Init(); |
| 51 | 49 | ||
| 52 | unsigned long time; |
50 | unsigned long time; |
| 53 | 51 | ||
| 54 | Can_Message_t txMsg; |
52 | Can_Message_t txMsg; |
| 55 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
53 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
| 56 | txMsg.DataLength = 4; |
54 | txMsg.DataLength = 4; |
| 57 | txMsg.RemoteFlag = 0; |
55 | txMsg.RemoteFlag = 0; |
| 58 | txMsg.ExtendedFlag = 1; |
56 | txMsg.ExtendedFlag = 1; |
| 59 | txMsg.Data.words[0] = APP_TYPE; |
57 | txMsg.Data.words[0] = APP_TYPE; |
| 60 | txMsg.Data.words[1] = APP_VERSION; |
58 | txMsg.Data.words[1] = APP_VERSION; |
| 61 | 59 | ||
| 62 | uint16_t reg5Vfeedback; |
60 | uint16_t reg5Vfeedback; |
| 63 | uint16_t currentfeedback; |
61 | uint16_t currentfeedback; |
| 64 | uint8_t DUTconnected=0; |
62 | uint8_t DUTconnected=0; |
| 65 | uint8_t DUTconnectcnt=0; |
63 | uint8_t DUTconnectcnt=0; |
| 66 | 64 | ||
| 67 | // Set up callback for CAN reception, this is optional if only sending is required. |
65 | // Set up callback for CAN reception, this is optional if only sending is required. |
| 68 | BIOS_CanCallback = &can_receive; |
66 | BIOS_CanCallback = &can_receive; |
| 69 | // Send CAN_NMT_APP_START |
67 | // Send CAN_NMT_APP_START |
| 70 | BIOS_CanSend(&txMsg); |
68 | BIOS_CanSend(&txMsg); |
| 71 | 69 | ||
| 72 | // Set up three timers (assume at least three has been defined) |
70 | // Set up three timers (assume at least three has been defined) |
| 73 | // The timeout is specified in ticks, which is equal to ms if |
71 | // The timeout is specified in ticks, which is equal to ms if |
| 74 | // the tick frequency is set to 1000. |
72 | // the tick frequency is set to 1000. |
| 75 | Timer_SetTimeout(0, 3000, TimerTypeFreeRunning, 0); |
73 | Timer_SetTimeout(0, 3000, TimerTypeFreeRunning, 0); |
| 76 | Timer_SetTimeout(1, 100, TimerTypeFreeRunning, 0); |
74 | Timer_SetTimeout(1, 100, TimerTypeFreeRunning, 0); |
| 77 | DDRB |= (1<<PB7); |
75 | DDRB |= (1<<PB7); |
| 78 | 76 | ||
| 79 | PORTD &= ~(1<<PD7);//turn off output |
77 | PORTD &= ~(1<<PD7);//turn off output |
| 80 | DDRD |= (1<<PD7); |
78 | DDRD |= (1<<PD7); |
| 81 | DDRC &= ~(1<<PC2); //set EXP_N to input |
79 | DDRC &= ~(1<<PC2); //set EXP_N to input |
| 82 | PORTC |= (1<<PC2); //set EXP_N to pullup |
80 | PORTC |= (1<<PC2); //set EXP_N to pullup |
| 83 | 81 | ||
| 84 | while (1) { |
82 | while (1) { |
| 85 | if (Timer_Expired(1)) { |
83 | if (Timer_Expired(1)) { |
| 86 | if (!(PINC & (1<<PC2))) { |
84 | if (!(PINC & (1<<PC2))) { |
| - | 85 | if (DUTconnectcnt < 10) { |
|
| 87 | DUTconnectcnt++; |
86 | DUTconnectcnt++; |
| - | 87 | } |
|
| 88 | } else { |
88 | } else { |
| 89 | DUTconnected = 0; |
89 | DUTconnected = 0; |
| - | 90 | //DUTconnectcnt --; |
|
| 90 | DUTconnectcnt = 0; |
91 | DUTconnectcnt = 0; |
| 91 | } |
92 | } |
| 92 | if (DUTconnectcnt |
93 | if (DUTconnectcnt == 10) { |
| 93 | DUTconnected = 1; |
94 | DUTconnected = 1; |
| - | 95 | } //else if (DUTconnectcnt == 0) { |
|
| - | 96 | //DUTconnected = 0; |
|
| 94 |
|
97 | //} |
| 95 | 98 | ||
| 96 | reg5Vfeedback = ADC_Get(ADREG5VFEEDBACK); |
99 | reg5Vfeedback = ADC_Get(ADREG5VFEEDBACK); |
| 97 | reg5Vfeedback = (reg5Vfeedback & 0x03ff) * ADC_FACTOR; //get voltage in mV (typical 5000mV) |
100 | reg5Vfeedback = (reg5Vfeedback & 0x03ff) * ADC_FACTOR; //get voltage in mV (typical 5000mV) |
| 98 | currentfeedback = ADC_Get(ADCURRENTFEEDBACK); |
101 | currentfeedback = ADC_Get(ADCURRENTFEEDBACK); |
| 99 | currentfeedback = (currentfeedback>>1); //get current in mA ( cur [A] = (ad*Vcc /1024)/R, R=10 ) (typical 20mA) |
102 | currentfeedback = (currentfeedback>>1); //get current in mA ( cur [A] = (ad*Vcc /1024)/R, R=10 ) (typical 20mA) |
| 100 | 103 | ||
| 101 |
|
104 | if (reg5Vfeedback > 6000 || currentfeedback > 40) { |
| 102 | PORTD &= ~(1<<PD7); //turn off output |
105 | PORTD &= ~(1<<PD7); //turn off output |
| 103 |
|
106 | } |
| 104 | } |
107 | } |
| 105 | if (Timer_Expired(0)) { |
108 | if (Timer_Expired(0)) { |
| 106 | /*reg5Vfeedback = ADC_Get(ADREG5VFEEDBACK); |
109 | /*reg5Vfeedback = ADC_Get(ADREG5VFEEDBACK); |
| 107 | reg5Vfeedback = (reg5Vfeedback & 0x03ff) * ADC_FACTOR; //get voltage in mV (typical 5000mV) |
110 | reg5Vfeedback = (reg5Vfeedback & 0x03ff) * ADC_FACTOR; //get voltage in mV (typical 5000mV) |
| 108 | currentfeedback = ADC_Get(ADCURRENTFEEDBACK); |
111 | currentfeedback = ADC_Get(ADCURRENTFEEDBACK); |
| 109 | currentfeedback = (currentfeedback>>1); //get current in mA ( cur [A] = (ad*Vcc /1024)/R, R=10 ) (typical 20mA) |
112 | currentfeedback = (currentfeedback>>1); //get current in mA ( cur [A] = (ad*Vcc /1024)/R, R=10 ) (typical 20mA) |
| 110 | */ |
113 | */ |
| 111 | PORTB |
114 | //PORTB ^= (1<<PB7); |
| 112 | PORTD |
115 | //PORTD ^= (1<<PD7); //toggle output |
| 113 | 116 | ||
| - | 117 | /*if (DUTconnected) { |
|
| - | 118 | PORTD |= (1<<PD7); //toggle output |
|
| - | 119 | }*/ |
|
| 114 | 120 | ||
| 115 | txMsg.Id = 0; |
121 | txMsg.Id = 0; |
| 116 | txMsg.DataLength = |
122 | txMsg.DataLength = 6; |
| 117 | txMsg.RemoteFlag = 0; |
123 | txMsg.RemoteFlag = 0; |
| 118 | txMsg.ExtendedFlag = 1; |
124 | txMsg.ExtendedFlag = 1; |
| 119 | txMsg.Data.bytes[0] = (reg5Vfeedback>>8)&0xff; |
125 | txMsg.Data.bytes[0] = (reg5Vfeedback>>8)&0xff; |
| 120 | txMsg.Data.bytes[1] = reg5Vfeedback&0xff; |
126 | txMsg.Data.bytes[1] = reg5Vfeedback&0xff; |
| 121 | txMsg.Data.bytes[2] = (currentfeedback>>8)&0xff; |
127 | txMsg.Data.bytes[2] = (currentfeedback>>8)&0xff; |
| 122 | txMsg.Data.bytes[3] = currentfeedback&0xff; |
128 | txMsg.Data.bytes[3] = currentfeedback&0xff; |
| 123 | 129 | ||
| 124 | txMsg.Data.bytes[4] = DUTconnected; |
130 | txMsg.Data.bytes[4] = DUTconnected; |
| - | 131 | txMsg.Data.bytes[5] = ((PORTD & (1<<PD7))>>PD7); |
|
| 125 | 132 | ||
| 126 | // Send CAN_NMT_APP_START |
133 | // Send CAN_NMT_APP_START |
| 127 | BIOS_CanSend(&txMsg); |
134 | BIOS_CanSend(&txMsg); |
| 128 | } |
135 | } |
| 129 | 136 | ||
| 130 | if (rxMsgFull) { |
137 | if (rxMsgFull) { |
| 131 | 138 | ||
| 132 | rxMsgFull = 0; // |
139 | rxMsgFull = 0; // |
| 133 | } |
140 | } |
| 134 | } |
141 | } |
| 135 | 142 | ||
| 136 | return 0; |
143 | return 0; |
| 137 | } |
144 | } |
| 138 | 145 | ||