@@ -29,6 +29,7 @@ extern "C" {
29
29
30
30
#include <stdio.h>
31
31
#include <stdlib.h>
32
+ #include <string.h>
32
33
33
34
#include "avr_bitbang.h"
34
35
@@ -99,7 +100,16 @@ static void avr_bitbang_write_bit(avr_bitbang_t *p)
99
100
100
101
// output to HW pin
101
102
if ( p -> p_out .port ) {
102
- avr_raise_irq (avr_io_getirq (p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_out .port ), p -> p_out .pin ), bit );
103
+ avr_raise_irq (p -> p_out .irq , bit );
104
+ uint16_t addr = p -> p_out .ioport -> r_port ;
105
+ uint8_t value = p -> avr -> data [addr ];
106
+ if (bit )
107
+ value |= 1 << p -> p_out .pin ;
108
+ else
109
+ value &= ~(1 << p -> p_out .pin );
110
+ // at least avr->data[addr] update is required, otherwise port state is broken
111
+ // also triggering vcd signal would be nice here
112
+ avr_core_watch_write (p -> avr , addr , value );
103
113
}
104
114
105
115
// module callback
@@ -130,7 +140,16 @@ static void avr_bitbang_clk_edge(avr_bitbang_t *p)
130
140
131
141
// generate clock output on HW pin
132
142
if ( p -> clk_generate && p -> p_clk .port ) {
133
- avr_raise_irq (avr_io_getirq (p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_clk .port ), p -> p_clk .pin ), clk );
143
+ avr_raise_irq (p -> p_clk .irq , clk );
144
+ uint16_t addr = p -> p_clk .ioport -> r_port ;
145
+ uint8_t value = p -> avr -> data [addr ];
146
+ if (clk )
147
+ value |= 1 << p -> p_clk .pin ;
148
+ else
149
+ value &= ~(1 << p -> p_clk .pin );
150
+ // at least avr->data[addr] update is required, otherwise port state is broken
151
+ // also triggering vcd signal would be nice here
152
+ avr_core_watch_write (p -> avr , addr , value );
134
153
}
135
154
136
155
if ( phase ) {
@@ -175,6 +194,36 @@ static void avr_bitbang_clk_hook(struct avr_irq_t * irq, uint32_t value, void *
175
194
avr_bitbang_clk_edge (p );
176
195
}
177
196
197
+ /**
198
+ * define bitbang pins
199
+ *
200
+ * @param p bitbang structure
201
+ * @param clk clock pin
202
+ * @param in incoming data pin
203
+ * @param out outgoing data pin
204
+ */
205
+ void avr_bitbang_defpins (avr_bitbang_t * p , avr_iopin_t * clk , avr_iopin_t * in , avr_iopin_t * out )
206
+ {
207
+ if (clk ) {
208
+ p -> p_clk .port = clk -> port ;
209
+ p -> p_clk .pin = clk -> pin ;
210
+ p -> p_clk .ioport = (avr_ioport_t * )avr_io_findinstance (p -> avr , "port" , clk -> port );
211
+ p -> p_clk .irq = avr_io_getirq (p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_clk .port ), p -> p_clk .pin );
212
+ }
213
+ if (in ) {
214
+ p -> p_in .port = in -> port ;
215
+ p -> p_in .pin = in -> pin ;
216
+ p -> p_in .ioport = (avr_ioport_t * )avr_io_findinstance (p -> avr , "port" , in -> port );
217
+ p -> p_in .irq = avr_io_getirq (p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_in .port ), p -> p_in .pin );
218
+ }
219
+ if (out ) {
220
+ p -> p_out .port = out -> port ;
221
+ p -> p_out .pin = out -> pin ;
222
+ p -> p_out .ioport = (avr_ioport_t * )avr_io_findinstance (p -> avr , "port" , out -> port );
223
+ p -> p_out .irq = avr_io_getirq (p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_out .port ), p -> p_out .pin );
224
+ }
225
+ }
226
+
178
227
/**
179
228
* reset bitbang sub-module
180
229
*
@@ -220,7 +269,7 @@ void avr_bitbang_start(avr_bitbang_t * p)
220
269
} else {
221
270
// slave mode -> attach clock function to clock pin
222
271
///@todo test
223
- avr_irq_register_notify ( avr_io_getirq ( p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_clk .port ), p -> p_clk . pin ) , avr_bitbang_clk_hook , p );
272
+ avr_irq_register_notify ( p -> p_clk .irq , avr_bitbang_clk_hook , p );
224
273
}
225
274
226
275
}
@@ -238,7 +287,7 @@ void avr_bitbang_stop(avr_bitbang_t * p)
238
287
239
288
p -> enabled = 0 ;
240
289
avr_cycle_timer_cancel (p -> avr , avr_bitbang_clk_timer , p );
241
- avr_irq_unregister_notify ( avr_io_getirq ( p -> avr , AVR_IOCTL_IOPORT_GETIRQ ( p -> p_clk .port ), p -> p_clk . pin ) , avr_bitbang_clk_hook , p );
290
+ avr_irq_unregister_notify ( p -> p_clk .irq , avr_bitbang_clk_hook , p );
242
291
}
243
292
244
293
#ifdef __cplusplus
0 commit comments