-
Notifications
You must be signed in to change notification settings - Fork 0
/
interrupt.cpp
281 lines (240 loc) · 6.56 KB
/
interrupt.cpp
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Technische Richtlinie FA205
// Bibliothek: interrupt.c
// Controller: ESP32
//
// Version: 1.0
// erstellt am: 4.9.2023
// letzte Änderung: 27.9.2023
// Autor: Rahm
#include "interrupt.h"
#include "math.h"
// Lokale initialisierungen
static uint8_t trigger;
static hw_timer_t *timer = NULL;
const int timerNr = 0;
#define _DIVIDER_ 80
void note_isr( void );
// Pointer auf Interrupt-Service-Routinen
void (*my_interrupt0) (void);
void (*my_interrupt1) (void);
void (*my_datareceived) (void);
void (*my_timer) (void);
//externer Interrupt 0
void ext_interrupt_init( void (*ip) (void) )
{
my_interrupt0 = ip;
#if defined(_FALLING_EDGE_TRIGGER_)
trigger = FALLING;
#elif defined(_RISING_EDGE_TRIGGER_)
trigger = RISING;
#elif defined(_ANY_EDGE_TRIGGER_)
trigger = CHANGE;
#endif
#ifdef INT_DEBUG
rs232_init();
rs232_print("\r\nInterrupt0 init");
rs232_print("\r\n on GPIO: ");rs232_byte(_INTERRUPT_PIN0_);
rs232_print("\r\n Trigger: ");
switch (trigger)
{
case 1: rs232_byte(trigger); rs232_print(" (Rising)"); break;
case 2: rs232_byte(trigger); rs232_print(" (Falling)"); break;
case 3: rs232_byte(trigger); rs232_print(" (Change)"); break;
}
#endif
}
void IRAM_ATTR interrupt0_isr(void)
{
my_interrupt0();
}
void ext_interrupt_enable( void )
{
attachInterrupt(_INTERRUPT_PIN0_,interrupt0_isr,trigger);
#ifdef INT_DEBUG
rs232_print("\r\nInterrupt0 enable");
rs232_print("\r\n on GPIO: ");rs232_byte(_INTERRUPT_PIN0_);
rs232_print("\r\n Trigger: ");
switch (trigger)
{
case 1: rs232_byte(trigger); rs232_print(" (Rising)"); break;
case 2: rs232_byte(trigger); rs232_print(" (Falling)"); break;
case 3: rs232_byte(trigger); rs232_print(" (Change)"); break;
}
#endif
}
void ext_interrupt_disable( void )
{
detachInterrupt(_INTERRUPT_PIN0_);
#ifdef INT_DEBUG
rs232_print("\r\nInterrupt0 disable");
rs232_print("\r\n on GPIO: ");rs232_byte(_INTERRUPT_PIN0_);
#endif
}
// Externer Interrupt 1
void ext_interrupt1_init( void (*ip) (void) )
{
my_interrupt1 = ip;
#if defined(_FALLING_EDGE_TRIGGER_)
trigger = FALLING;
#elif defined(_RISING_EDGE_TRIGGER_)
trigger = RISING;
#elif defined(_ANY_EDGE_TRIGGER_)
trigger = CHANGE;
#endif
#ifdef INT_DEBUG
rs232_init();
rs232_print("\r\nInterrupt1 init");
rs232_print("\r\n on GPIO: ");rs232_byte(_INTERRUPT_PIN1_);
rs232_print("\r\n Trigger: ");
switch (trigger)
{
case 1: rs232_byte(trigger); rs232_print(" (Rising)"); break;
case 2: rs232_byte(trigger); rs232_print(" (Falling)"); break;
case 3: rs232_byte(trigger); rs232_print(" (Change)"); break;
}
#endif
}
void IRAM_ATTR interrupt1_isr(void)
{
my_interrupt1();
}
void ext_interrupt1_enable( void )
{
attachInterrupt(_INTERRUPT_PIN1_,interrupt1_isr,trigger);
#ifdef INT_DEBUG
rs232_print("\r\nInterrupt1 enable");
rs232_print("\r\n on GPIO: ");rs232_byte(_INTERRUPT_PIN1_);
rs232_print("\r\n Trigger: ");
switch (trigger)
{
case 1: rs232_byte(trigger); rs232_print(" (Rising)"); break;
case 2: rs232_byte(trigger); rs232_print(" (Falling)"); break;
case 3: rs232_byte(trigger); rs232_print(" (Change)"); break;
}
#endif
}
void ext_interrupt1_disable( void )
{
detachInterrupt(_INTERRUPT_PIN1_);
#ifdef INT_DEBUG
rs232_print("\r\nInterrupt1 disable");
rs232_print("\r\n on GPIO: ");rs232_byte(_INTERRUPT_PIN1_);
#endif
}
//*******************************************************************
//** ab hier timer-Funktionen
//*******************************************************************
void timer1ms_init ( void (*ti) (void))
{
timer_ms_init(ti,1.0);
}
void timer1ms_enable( void )
{
timer_ms_enable();
}
void timer1ms_disable( void )
{
timer_ms_disable();
}
// Die timer-Interrupt-Serviceroutine timer1ms_isr() muss
// selbst geschrieben werden!!
uint64_t calculate_alarm ( float time)
{ // time in ms
return ((uint64_t)((float)(F_CPU/_DIVIDER_/1000) * time));
}
void IRAM_ATTR timer0_isr(void)
{
my_timer(); // aufrufen der Interrupt-Serviceroutine
}
void timer_ms_init ( void (*ti) (void), float time)
{
my_timer = ti; // Pointer auf isr im User-Code (normalerweise: timer_ms_isr) !!
timer = timerBegin(timerNr,_DIVIDER_,true);
timerAlarmWrite(timer,calculate_alarm(time),true);
timerAttachInterrupt(timer,timer0_isr,true);
}
void timer_ms_enable( void )
{
timerStart(timer);
timerAlarmEnable(timer);
}
void timer_ms_disable( void )
{
timerStop(timer);
timerAlarmDisable(timer);
}
// Ab hier keine FA205-Funktionen
/* Soundausgabe auf Lautsprecher an PORTy,1 (GPIO 27).
Verwendet timer_ms
*/
#ifndef TON_PORT
#define TON_PORT PORTy
#endif
#ifndef TON_BIT
#define TON_BIT 1
#endif
void note_isr( void )
{
bit_write(TON_PORT,TON_BIT,~bit_read(TON_PORT,TON_BIT));
}
void sound_init(void)
{
bit_init(TON_PORT,TON_BIT,OUT);
timer_ms_init(note_isr, 1000);
timer_ms_disable;
}
void note_on(float frequenz)
{
float millisec;
millisec = 500/frequenz; // Zeit für Halbe Periodendauer in ms
timerAlarmWrite(timer,calculate_alarm(millisec),true);
//timer_ms_init(note_isr, millisec);
timer_ms_enable();
}
void note_off(void)
{
timer_ms_disable();
}
void play_note(uint8_t notenwert, uint16_t duration, uint16_t silence)
{
float exponent = ((float)notenwert - 69.0) / 12.0;
float frequenz = 440 * pow(2,exponent);
note_on(frequenz);
delay_ms(duration);
note_off();
delay_ms(silence);
}
/*
// Data Received Interrupt (not working yet!!)
void IRAM_ATTR uart_isr(void *arg) // Interrupt-Vektor
{
uint8_t buf;
uart_read_bytes(EX_UART_NUM,&buf,1,1);
my_datareceived(); // Aufruf der Benutzer-ISR
}
void serial_interrupt_init( void (*sr) (void))
{
my_datareceived = sr;
//rs232_init(); // Serielle Schnittstelle initialisieren
uart_config_t uart_config = {
uart_config.baud_rate = 9600,
uart_config.data_bits = UART_DATA_8_BITS,
uart_config.parity = UART_PARITY_DISABLE,
uart_config.stop_bits = UART_STOP_BITS_1,
uart_config.flow_ctrl = UART_HW_FLOWCTRL_DISABLE
};
uart_param_config(EX_UART_NUM, &uart_config);
uart_set_pin(EX_UART_NUM, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(EX_UART_NUM, BUF_SIZE * 2, 0, 0, NULL, 0);
uart_isr_free(EX_UART_NUM);
uart_isr_register(EX_UART_NUM,uart_isr,NULL, ESP_INTR_FLAG_IRAM, rs232_handle);
}
extern void serial_interrupt_enable ( void )
{
uart_enable_rx_intr(EX_UART_NUM);
}
extern void serial_interrupt_disable ( void )
{
uart_disable_rx_intr(EX_UART_NUM); // Empfangsinterrupt ausschalten
}
*/