-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmx.c
204 lines (163 loc) · 5.78 KB
/
dmx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*
IBEX UK LTD http://www.ibexuk.com
Electronic Product Design Specialists
RELEASED SOFTWARE
The MIT License (MIT)
Copyright (c) 2013, IBEX UK Ltd, http://ibexuk.com
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
//Project Name: PIC32 DMX Receive Driver
#include "main.h" //Global data type definitions (see https://github.com/ibexuk/C_Generic_Header_File )
#define AP_DMX
#include "dmx.h"
//********************************
//********************************
//********** INTERRUPTS **********
//********************************
//********************************
void __ISR(_UART_2_VECTOR, ipl5) Uart2InterruptHandler (void) //(ipl# must match the priority level assigned to the irq where its enabled)
{
static BYTE data;
static BYTE status;
static WORD dmx_rx_channel_number;
static BYTE waiting_for_header = 1;
static BYTE waiting_for_break = 1;
Nop();
/*
if (
(INTGetEnable(INT_SOURCE_UART_TX(DMX_UART_NAME))) &&
(INTGetFlag(INT_SOURCE_UART_TX(DMX_UART_NAME)))
)
{
//----------------------------------
//----------------------------------
//---------- TX INTERRUPT ----------
//----------------------------------
//----------------------------------
while (UARTTransmitterIsReady(DMX_UART_NAME))
{
//UARTSendDataByte(DMX_UART_NAME, comms_tx_buffer[comms_tx_byte++]);
} //while (UARTTransmitterIsReady(DMX_UART_NAME))
INTClearFlag(INT_SOURCE_UART_TX(DMX_UART_NAME)); //Do after sending bytes
}
*/
if (
(INTGetEnable(INT_SOURCE_UART_ERROR(DMX_UART_NAME))) &&
(INTGetFlag(INT_SOURCE_UART_ERROR(DMX_UART_NAME)))
)
{
//----------------------------------------
//----------------------------------------
//---------- RX ERROR INTERRUPT ----------
//----------------------------------------
//----------------------------------------
status = UARTGetLineStatus(DMX_UART_NAME);
if (status & UART_PARITY_ERROR)
{
waiting_for_break = 1;
}
if (status & UART_OVERRUN_ERROR) //OERR must be cleared, clearing will reset RX FIFO
{
DMX_UART_STATUS = (DMX_UART_STATUS & ~UART_OVERRUN_ERROR); //Clear OERR bit
waiting_for_break = 1;
}
if (status & UART_FRAMING_ERROR)
{
//--------------------------
//----- BREAK RECEIVED -----
//--------------------------
waiting_for_break = 0;
waiting_for_header = 1; //Flag waiting for header
dmx_rx_channel_number = 1; //Reset the channel counter
}
//Clear RX buffer of bad data
while (UARTReceivedDataIsAvailable(DMX_UART_NAME))
data = UARTGetDataByte(DMX_UART_NAME);
INTClearFlag(INT_SOURCE_UART_RX(DMX_UART_NAME));
INTClearFlag(INT_SOURCE_UART_ERROR(DMX_UART_NAME));
}
if (
(INTGetEnable(INT_SOURCE_UART_RX(DMX_UART_NAME))) &&
(INTGetFlag(INT_SOURCE_UART_RX(DMX_UART_NAME)))
)
{
//----------------------------------
//----------------------------------
//---------- RX INTERRUPT ----------
//----------------------------------
//----------------------------------
while (UARTReceivedDataIsAvailable(DMX_UART_NAME))
{
//----- GET DATA BYTE -----
data = UARTGetDataByte(DMX_UART_NAME);
if (waiting_for_header)
{
//---------------------------
//----- HEADER RECEIVED -----
//---------------------------
if (data == DMX_HEADER_STANDARD)
{
//------------------------------------
//----- STANDARD HEADER RECEIVED -----
//------------------------------------
waiting_for_header = 0;
}
//<<<<< PUT ANY OTHER HEADER TYPES HERE
else
{
//-----------------------------------
//----- INVALID HEADER RECEIVED -----
//-----------------------------------
waiting_for_break = 1; //Non recognised header - ignore packet
}
}
else if (waiting_for_break)
{
//------------------------------------
//----- WE ARE WAITING FOR BREAK -----
//------------------------------------
//Ignore rx until break is received
}
else
{
//-------------------------
//----- DATA RECEIVED -----
//-------------------------
//RESET THE TIMEOUT TIMER
dmx_rx_active_10ms_timer = 120;
//GET THE DATA BYTE IF IN OUR CHANNEL RANGE
if (
(dmx_rx_channel_number >= dmx_start_address) &&
(dmx_rx_channel_number < (dmx_start_address + DMX_NO_OF_CHANS_TO_RX))
)
{
//THIS CHANNEL IS IN OUR RX WINDOW
dmx_rx_buffer[dmx_rx_channel_number - dmx_start_address] = data;
}
//FLAG NEW FRAME RECEVIED IF WE HAVE RECEIVED ALL THE CHANNELS WE WANT TO RECEIVE
if (dmx_rx_channel_number == (dmx_start_address + DMX_NO_OF_CHANS_TO_RX - 1))
dmx_new_frame_received = 1;
//INCREMENT THE CHANNEL NUMBER READY FOR THE NEXT BYTE
dmx_rx_channel_number++;
if (dmx_rx_channel_number > 512)
{
waiting_for_break = 1;
}
}
}
INTClearFlag(INT_SOURCE_UART_RX(DMX_UART_NAME)); //Do after reading data as any data in RX FIFO will stop irq bit being cleared
}
}