-
Notifications
You must be signed in to change notification settings - Fork 0
/
dmx_ch32v.h
202 lines (159 loc) · 4.57 KB
/
dmx_ch32v.h
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
202
/*
Guus 2023. channel vals are 0-511 (NOT 1-512)
tested on ch32v003
This library does not set the rs485 driver in the right direction. Do so before starting or permanent by hardware.
*/
#ifndef dmx_ch32v_h
#define dmx_ch32v_h
// #define DMX_UART1 // defines the uart to use
#define DMX_UART2 // defines the uart to use
#if defined(CH32V00X)
#include <ch32v00x.h>
#include <ch32v00x_gpio.h>
#include <ch32v00x_rcc.h>
#include <ch32v00x_usart.h>
#define GPIOPORT GPIOD
#define RCC_APB2Periph_GPIOPORT RCC_APB2Periph_GPIOD
#define GPIOTXPIN GPIO_Pin_5
#define GPIORXPIN GPIO_Pin_6
#define UARTNUM USART1
#define RCC_APB2Periph_USARTNUM RCC_APB2Periph_USART1
#define USARTNUM_IRQn USART1_IRQn
#if !defined(DMX_UART1)
#define DMX_UART1
#endif
#endif
#if defined(CH32X035)
#include <ch32x035.h>
#include <ch32x035_gpio.h>
#include <ch32x035_rcc.h>
#include <ch32x035_usart.h>
#endif
#if defined(CH32V10X)
#include <ch32v10x.h>
#include <ch32v10x_gpio.h>
#include <ch32v10x_rcc.h>
#include <ch32v10x_usart.h>
#endif
#if defined(CH32V20X)
#include <ch32v20x.h>
#include <ch32v20x_gpio.h>
#include <ch32v20x_rcc.h>
#include <ch32v20x_usart.h>
#endif
#if defined(CH32V30X)
#include <ch32v30x.h>
#include <ch32v30x_gpio.h>
#include <ch32v30x_rcc.h>
#include <ch32v30x_usart.h>
#endif
#if defined CH32V30X || CH32V20X || CH32V30X || CH32X035
#if defined(DMX_UART2)
#define GPIOPORT GPIOA
#define RCC_APB2Periph_GPIOPORT RCC_APB2Periph_GPIOA
#define GPIOTXPIN GPIO_Pin_2
#define GPIORXPIN GPIO_Pin_3
#define UARTNUM USART2
#define RCC_APB2Periph_USARTNUM RCC_APB1Periph_USART2
#define USARTNUM_IRQn USART2_IRQn
#elif defined(DMX_UART3)
#define GPIOPORT GPIOB
#define RCC_APB2Periph_GPIOPORT RCC_APB2Periph_GPIOB
#define GPIOTXPIN GPIO_Pin_9
#define GPIORXPIN GPIO_Pin_10
#define UARTNUM USART3
#define RCC_APB2Periph_USARTNUM RCC_APB1Periph_USART3
#define USARTNUM_IRQn USART3_IRQn
#elif defined(DMX_UART4)
#define GPIOPORT GPIOB
#define RCC_APB2Periph_GPIOPORT RCC_APB2Periph_GPIOB
#define GPIOTXPIN GPIO_Pin_0
#define GPIORXPIN GPIO_Pin_1
#define UARTNUM UART4
#define RCC_APB2Periph_USARTNUM RCC_APB1Periph_UART4
#define USARTNUM_IRQn UART4_IRQn
#else // use uart1
#define GPIOPORT GPIOB
#define RCC_APB2Periph_GPIOPORT RCC_APB2Periph_GPIOB
#define GPIOTXPIN GPIO_Pin_10
#define GPIORXPIN GPIO_Pin_11
#define UARTNUM USART1
#define RCC_APB2Periph_USARTNUM RCC_APB2Periph_USART1
#define USARTNUM_IRQn USART1_IRQn
#if !defined(DMX_UART1)
#define DMX_UART1
#endif
#endif
#endif
extern volatile unsigned char dmx_data[512];
extern volatile unsigned char dmx_startcode;
extern volatile unsigned char dmx_newdata;
extern volatile unsigned short dmx_pktsize;
typedef enum
{
DMX_STOP = 0x00,
DMX_IDLE = 0x01,
DMX_BREAK = 0x02,
DMX_START = 0x04,
DMX_RUN = 0x05
} dmx_state_t;
typedef enum
{
DMX_DIRNOTSET = 0x00,
DMX_RX = 0x01,
DMX_TX = 0x02,
} dmx_dir_t;
// avail dmx speeds in fps. Changing the speed wil change the idle time between frames.
typedef enum
{
DMX_FPS_MIN,
DMX_FPS_20,
DMX_FPS_25,
DMX_FPS_30,
DMX_FPS_35,
DMX_FPS_40,
DMX_FPS_44,
DMX_FPS_MAX,
} dmx_speed_fps;
// starts the transmitter
void dmx_beginTX();
#define dmx_transmitter_begin dmx_beginTX
// starts the receiver
void dmx_beginRX();
#define dmx_receiver_begin dmx_beginRX
// stop
void dmx_stop();
// sets (an approximation) of the breaktime in usec.
void dmx_setBreakTime(unsigned short usec);
// sets the speed of tx mode
void dmx_setFPS(dmx_speed_fps f);
// sets single value to addr
signed short dmx_setValue(unsigned short chan, unsigned char value);
// sets vals from arr to
signed short dmx_setValues(unsigned short startchan, unsigned char *p, unsigned short len);
// set the startcode
void dmx_setStartcode(unsigned char sc);
// get single value from a channel
signed short dmx_getValue(unsigned short chan);
// get values from dmx into arr
signed short dmx_getValues(unsigned short startchan, unsigned char *p, unsigned short len);
// returns last packet startcode
unsigned char dmx_getStartcode();
// returns 0xff if new frame is avail since last check
unsigned char dmx_newPacket();
// return dmx state machine state
dmx_state_t dmx_getState();
// set all to zero
void dmx_clear();
// returns size of last received packet
unsigned short dmx_getPacketSize();
#if defined(DMX_UART1)
extern void USART1_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
#if defined(DMX_UART2)
extern void USART2_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
#if defined(DMX_UART3)
extern void USART3_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
#endif
#endif