My pin detection change from first to second run

Go To Last Post
17 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a project on an old atmega328 (12MHz) that I use a wile loop to detect a pin change. I'm not sure the pinchange interrupt would help or be my issue here but for debugging I'm using this for now. I also realize this is better suited for ASM but figured it was a simple enough loop.

 

I removed the break and stripped down the loop to this point so I could debug this but I'm still confused by what I see?

 

while(1)
    {   

    PORTB |= 0x04;PORTB &= ~0x04;//debug
        if  ( (PIND & 0x05) != 0x05)
        {
        PORTB |= 0x04;//debug
            for (char i = 0; i < 8; i++) { command >>=1; if ( (PIND & _DATA) != _DATA) command |= 0x80; _delay_us(4); }//read command.
            break;

        }
    } 

The fist time I use this loop the debug points are .5us apart, the second time and each there after I get 4.5 us. Is this just the nature of compiled code, a 4us difference ? Is some global variable not initialed yet. PIND 1 and 3 are always high prior to this loop. After about 6-to 20ms the line will come down. It is always caught at the right moment but only the first time do I get that .5 us short lead in.

 

 

 

 

Attachment(s): 

Last Edited: Wed. Aug 19, 2020 - 04:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

S_K_U_N_X wrote:
The fist [sic] time I use this loop the debug points are .5us apart, the second time and each there after I get 4.5 us

With a _delay_us(4); sitting there what else did you expect ?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The 4 us delay is occurring from

 PORTB |= 0x04;PORTB &= ~0x04;

to

  PORTB |= 0x04;

 

the _delay_us(4) your speaking of is long after my debug point. That is the byte spacing. Even if that were true why does the 4us not show up but once.

 

I added another image to illustrate my point.

 

 

 

 

 

 

Attachment(s): 

Last Edited: Wed. Aug 19, 2020 - 04:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

S_K_U_N_X wrote:

The 4 us delay is occurring from

 PORTB |= 0x04;PORTB &= ~0x04;

to

  PORTB |= 0x04;

 

the _delay_us(4) your speaking of is long after my debug point. That is the byte spacing. Even if that were true why does the 4us not show up but once.

 

 

Have a look at the lss file (generated assembly code)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looking at the lst file I see only the one function and its not 4us.

 

 106 0090 2A9A              sbi 0x5,2<-----debug.

 107 0092 2A98              cbi 0x5,2<-----debug.
 107 0092 89B1              in r24,0x9
 108 0094 8570              andi r24,lo8(5)
 109 0096 8530              cpi r24,lo8(5)
 110 0098 01F0              breq .L12
 111 009a 2A98              cbi 0x5,2 <-----debug.

 

is is .global though not sure that an issue.

 

If I see two outputs (my image I posted shows the two I get)  I would have expected the compiler made two instances of this. I did try including the __attribute__ ((noinline))   but it didnt help.

 

 89                   .global    getBridgeCommand
  91                   getBridgeCommand:
  92                   /* prologue: function */
  93                   /* frame size = 0 */
  94                   /* stack size = 0 */
  95                   .L__stack_usage = 0
  96 007e 8AB1              in r24,0xa
  97 0080 8A7F              andi r24,lo8(-6)
  98 0082 8AB9              out 0xa,r24
  99 0084 8BB1              in r24,0xb
 100 0086 8560              ori r24,lo8(5)
 101 0088 8BB9              out 0xb,r24
 102 008a 3CE0              ldi r19,lo8(12)
 103 008c 3A95              1: dec r19
 104 008e 01F4              brne 1b
 105                   .L12:
 106 0090 2A9A              sbi 0x5,2<-----debug.
 107 0092 2A98              cbi 0x5,2<-----debug.
 108 0094 89B1              in r24,0x9
 109 0096 8570              andi r24,lo8(5)
 110 0098 8530              cpi r24,lo8(5)
 111 009a 01F0              breq .L12
 112 009c 2A98              cbi 0x5,2<-----debug.
 113 009e 88E0              ldi r24,lo8(8)
 114 00a0 6FEF              ldi r22,lo8(-1)
 115                   .L15:
 116 00a2 6695              lsr r22
 117 00a4 489B              sbis 0x9,0
 118 00a6 6068              ori r22,lo8(-128)
 119                   .L13:
 120 00a8 40E1              ldi r20,lo8(16)
 121 00aa 4A95              1: dec r20
 122 00ac 01F4              brne 1b
 123 00ae 8150              subi r24,lo8(-(-1))
 124 00b0 01F4              brne .L15
 125 00b2 8CE0              ldi r24,lo8(12)
 126 00b4 8A95              1: dec r24
 127 00b6 01F4              brne 1b

 

Last Edited: Wed. Aug 19, 2020 - 05:11 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I rewrote it in assembly and its doing the same thing.

 


PORTB |= 0x04;PORTB &= ~0x04;//debug
	asm volatile(
	"wait%=:\			\n"
	"	sbic	%0,0	\n"
	"	breq wait%=		\n"
	"	sbic	%0,2	\n"
	"	breq wait%=		\n"
	: : "I" (_SFR_IO_ADDR(PIND))	// %0
		);
PORTB |= 0x04;

I can not figure out what makes it take an extra 4us to leave the loop after the pin is high.  I can only guess the pin is not "fully" at0v but I'll have to dig out my analog scope for that.

 

 

Last Edited: Wed. Aug 19, 2020 - 05:55 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Could it be this:

        if  ( (PIND & 0x05) != 0x05)

That's 2 pins you're checking there.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Voltages are the same... I still do no understand this.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That's 2 pins you're checking there.

 

 Yeah in theory I want both. I'd be ok with just one. Though my testing shows the same results with either or low. I can try testing the one with the lower voltage I guess.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Looking at the disassembly in #5; there some code in there you've not shown us.

 

Your #1 is clearly an extract. Best to post the entire getBridgeCommand()

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

its a lot of irreverent stuff...  but I do not mind.  This version has my asm function waiting only on the latch pin (top of the two) and has the same issue. Apologized for change the code but I'm really trying to solve this issue atm.  Happy to undo and put it all back, but all versions I posted have the same oddity.

 

I can not attach lst files I guess so here it is.  This file is small, just two functions and a few varables. The getBridgeCommand  is in there.

 

 

 

   1               		.file	"bridge.c"
   2               	__SP_H__ = 0x3e
   3               	__SP_L__ = 0x3d
   4               	__SREG__ = 0x3f
   5               	__tmp_reg__ = 0
   6               	__zero_reg__ = 1
   7               		.text
   8               	.global	sendBridgeData
  10               	sendBridgeData:
  11               	/* prologue: function */
  12               	/* frame size = 0 */
  13               	/* stack size = 0 */
  14               	.L__stack_usage = 0
  15 0000 24E1      		ldi r18,lo8(20)
  16 0002 2A95      		1: dec r18
  17 0004 01F4      		brne 1b
  18 0006 9BB1      		in r25,0xb
  19 0008 9560      		ori r25,lo8(5)
  20 000a 9BB9      		out 0xb,r25
  21 000c 44E0      		ldi r20,lo8(4)
  22 000e 4A95      		1: dec r20
  23 0010 01F4      		brne 1b
  24 0012 9BB1      		in r25,0xb
  25 0014 9A7F      		andi r25,lo8(-6)
  26 0016 9BB9      		out 0xb,r25
  27 0018 92E0      		ldi r25,lo8(2)
  28 001a 9A95      		1: dec r25
  29 001c 01F4      		brne 1b
  30 001e 30E0      		ldi r19,0
  31 0020 90E0      		ldi r25,0
  32               	.L2:
  33 0022 E32F      		mov r30,r19
  34 0024 FF27      		clr r31
  35 0026 E7FD      		sbrc r30,7
  36 0028 F095      		com r31
  37 002a E817      		cp r30,r24
  38 002c F907      		cpc r31,r25
  39 002e 04F4      		brge .L6
  40 0030 28E0      		ldi r18,lo8(8)
  41 0032 E050      		subi r30,lo8(-(outData))
  42 0034 F040      		sbci r31,hi8(-(outData))
  43               	.L7:
  44 0036 589A      		sbi 0xb,0
  45 0038 46E0      		ldi r20,lo8(6)
  46 003a 4A95      		1: dec r20
  47 003c 01F4      		brne 1b
  48 003e 00C0      		rjmp .
  49 0040 4081      		ld r20,Z
  50 0042 40FD      		sbrc r20,0
  51 0044 00C0      		rjmp .L3
  52 0046 45E0      		ldi r20,lo8(5)
  53 0048 4A95      		1: dec r20
  54 004a 01F4      		brne 1b
  55 004c 00C0      		rjmp .
  56 004e 00C0      		rjmp .L8
  57               	.L3:
  58 0050 5898      		cbi 0xb,0
  59 0052 45E0      		ldi r20,lo8(5)
  60 0054 4A95      		1: dec r20
  61 0056 01F4      		brne 1b
  62 0058 0000      		nop
  63               	.L8:
  64               	/* #APP */
  65               	 ;  42 "bridge.c" 1
  66 005a 0000      		nop
  67
  68               	 ;  0 "" 2
  69               	/* #NOAPP */
  70 005c 4081      		ld r20,Z
  71 005e 4695      		lsr r20
  72 0060 4083      		st Z,r20
  73 0062 2150      		subi r18,lo8(-(-1))
  74 0064 01F4      		brne .L7
  75 0066 589A      		sbi 0xb,0
  76 0068 23E0      		ldi r18,lo8(3)
  77 006a 2A95      		1: dec r18
  78 006c 01F4      		brne 1b
  79 006e 0000      		nop
  80 0070 5898      		cbi 0xb,0
  81 0072 3F5F      		subi r19,lo8(-(1))
  82 0074 00C0      		rjmp .L2
  83               	.L6:
  84 0076 8AB1      		in r24,0xa
  85 0078 8560      		ori r24,lo8(5)
  86 007a 8AB9      		out 0xa,r24
  87 007c 0895      		ret
  89               	.global	getBridgeCommand
  91               	getBridgeCommand:
  92               	/* prologue: function */
  93               	/* frame size = 0 */
  94               	/* stack size = 0 */
  95               	.L__stack_usage = 0
  96 007e 8AB1      		in r24,0xa
  97 0080 8A7F      		andi r24,lo8(-6)
  98 0082 8AB9      		out 0xa,r24
  99 0084 8BB1      		in r24,0xb
 100 0086 8560      		ori r24,lo8(5)
 101 0088 8BB9      		out 0xb,r24
 102 008a 3CE0      		ldi r19,lo8(12)
 103 008c 3A95      		1: dec r19
 104 008e 01F4      		brne 1b
 105 0090 229A      		sbi 0x4,2
 106 0092 2A9A      		sbi 0x5,2
 107 0094 2A98      		cbi 0x5,2
 108               	/* #APP */
 109               	 ;  85 "bridge.c" 1
 110               		wait96:
 111 0096 4899      		sbic	9,0
 112 0098 01F0      		breq wait96
 113
 114               	 ;  0 "" 2
 115               	/* #NOAPP */
 116 009a 2A9A      		sbi 0x5,2
 117 009c 88E0      		ldi r24,lo8(8)
 118 009e 6FEF      		ldi r22,lo8(-1)
 119               	.L13:
 120 00a0 6695      		lsr r22
 121 00a2 489B      		sbis 0x9,0
 122 00a4 6068      		ori r22,lo8(-128)
 123               	.L11:
 124 00a6 40E1      		ldi r20,lo8(16)
 125 00a8 4A95      		1: dec r20
 126 00aa 01F4      		brne 1b
 127 00ac 8150      		subi r24,lo8(-(-1))
 128 00ae 01F4      		brne .L13
 129 00b0 8CE0      		ldi r24,lo8(12)
 130 00b2 8A95      		1: dec r24
 131 00b4 01F4      		brne 1b
 132 00b6 8AB1      		in r24,0xa
 133 00b8 8560      		ori r24,lo8(5)
 134 00ba 8AB9      		out 0xa,r24
 135 00bc 8BB1      		in r24,0xb
 136 00be 8A7F      		andi r24,lo8(-6)
 137 00c0 8BB9      		out 0xb,r24
 138 00c2 8091 0000 		lds r24,setParm
 139 00c6 9091 0000 		lds r25,setParm+1
 140 00ca 0097      		sbiw r24,0
 141 00cc 01F4      		brne .+2
 142 00ce 00C0      		rjmp .L14
 143 00d0 2091 0000 		lds r18,parmType
 144 00d4 2230      		cpi r18,lo8(2)
 145 00d6 01F4      		brne .L15
 146 00d8 63FF      		sbrs r22,3
 147 00da 00C0      		rjmp .L16
 148 00dc 81E0      		ldi r24,lo8(1)
 149 00de 8093 0000 		sts _HOTSWAP_DISABLED,r24
 150 00e2 00C0      		rjmp .L17
 151               	.L16:
 152 00e4 1092 0000 		sts _HOTSWAP_DISABLED,__zero_reg__
 153               	.L17:
 154 00e8 64FF      		sbrs r22,4
 155 00ea 00C0      		rjmp .L18
 156 00ec 81E0      		ldi r24,lo8(1)
 157 00ee 8093 0000 		sts _UDLR,r24
 158 00f2 00C0      		rjmp .L19
 159               	.L18:
 160 00f4 1092 0000 		sts _UDLR,__zero_reg__
 161               	.L19:
 162 00f8 66FF      		sbrs r22,6
 163 00fa 00C0      		rjmp .L20
 164 00fc 81E0      		ldi r24,lo8(1)
 165 00fe 8093 0000 		sts _AUTO_PAUSE_DIABLED,r24
 166 0102 00C0      		rjmp .L21
 167               	.L20:
 168 0104 1092 0000 		sts _AUTO_PAUSE_DIABLED,__zero_reg__
 169 0108 00C0      		rjmp .L21
 170               	.L15:
 171 010a 2430      		cpi r18,lo8(4)
 172 010c 01F4      		brne .L22
 173 010e 0197      		sbiw r24,1
 174 0110 01F4      		brne .L21
 175 0112 81E1      		ldi r24,lo8(17)
 176 0114 90E0      		ldi r25,0
 177 0116 0E94 0000 		call __eeupd_byte_m328p
 178 011a 00C0      		rjmp .L21
 179               	.L22:
 180 011c 2530      		cpi r18,lo8(5)
 181 011e 01F4      		brne .L24
 182 0120 0197      		sbiw r24,1
 183 0122 01F4      		brne .L21
 184 0124 6093 0000 		sts _MAPPING_SELECTION,r22
 185 0128 00C0      		rjmp .L21
 186               	.L24:
 187 012a 2130      		cpi r18,lo8(1)
 188 012c 01F4      		brne .L26
 189 012e 0197      		sbiw r24,1
 190 0130 01F4      		brne .L27
 191 0132 6093 0000 		sts rumbleLevel,r22
 192 0136 00C0      		rjmp .L21
 193               	.L27:
 194 0138 6093 0000 		sts rumbleLoop,r22
 195 013c 00C0      		rjmp .L21
 196               	.L26:
 197 013e 2330      		cpi r18,lo8(3)
 198 0140 01F4      		brne .L21
 199 0142 E0E0      		ldi r30,lo8(mapper+23)
 200 0144 F0E0      		ldi r31,hi8(mapper+23)
 201 0146 E81B      		sub r30,r24
 202 0148 F90B      		sbc r31,r25
 203 014a 6083      		st Z,r22
 204 014c E0E0      		ldi r30,lo8(mapper)
 205 014e F0E0      		ldi r31,hi8(mapper)
 206 0150 81E0      		ldi r24,lo8(1)
 207               	.L28:
 208 0152 8193      		st Z+,r24
 209 0154 8F5F      		subi r24,lo8(-(1))
 210 0156 8931      		cpi r24,lo8(25)
 211 0158 01F4      		brne .L28
 212               	.L21:
 213 015a 8091 0000 		lds r24,setParm
 214 015e 9091 0000 		lds r25,setParm+1
 215 0162 0197      		sbiw r24,1
 216 0164 9093 0000 		sts setParm+1,r25
 217 0168 8093 0000 		sts setParm,r24
 218 016c 00C0      		rjmp .L57
 219               	.L14:
 220 016e 1092 0000 		sts parmType,__zero_reg__
 221 0172 6623      		tst r22
 222 0174 01F4      		brne .+2
 223 0176 00C0      		rjmp .L49
 224 0178 6130      		cpi r22,lo8(1)
 225 017a 01F4      		brne .L30
 226 017c 8091 0000 		lds r24,_cntrl
 227 0180 8093 0000 		sts outData,r24
 228 0184 E0E0      		ldi r30,lo8(reportBuffer+1)
 229 0186 F0E0      		ldi r31,hi8(reportBuffer+1)
 230 0188 A0E0      		ldi r26,lo8(outData+1)
 231 018a B0E0      		ldi r27,hi8(outData+1)
 232               	.L32:
 233 018c 8191      		ld r24,Z+
 234 018e 8D93      		st X+,r24
 235 0190 90E0      		ldi r25,hi8(reportBuffer+14)
 236 0192 E030      		cpi r30,lo8(reportBuffer+14)
 237 0194 F907      		cpc r31,r25
 238 0196 01F4      		brne .L32
 239               	.L31:
 240 0198 8DE0      		ldi r24,lo8(13)
 241 019a 00C0      		rjmp .L62
 242               	.L30:
 243 019c 6230      		cpi r22,lo8(2)
 244 019e 01F4      		brne .L33
 245 01a0 8091 0000 		lds r24,reportBuffer
 246 01a4 8093 0000 		sts outData,r24
 247 01a8 E0E0      		ldi r30,lo8(psx_pressure_data)
 248 01aa F0E0      		ldi r31,hi8(psx_pressure_data)
 249 01ac A0E0      		ldi r26,lo8(outData+1)
 250 01ae B0E0      		ldi r27,hi8(outData+1)
 251               	.L34:
 252 01b0 8191      		ld r24,Z+
 253 01b2 8D93      		st X+,r24
 254 01b4 30E0      		ldi r19,hi8(psx_pressure_data+13)
 255 01b6 E030      		cpi r30,lo8(psx_pressure_data+13)
 256 01b8 F307      		cpc r31,r19
 257 01ba 01F4      		brne .L34
 258 01bc 00C0      		rjmp .L31
 259               	.L33:
 260 01be 6330      		cpi r22,lo8(3)
 261 01c0 01F4      		brne .L35
 262 01c2 8091 0000 		lds r24,reportBuffer
 263 01c6 8370      		andi r24,lo8(3)
 264 01c8 00C0      		rjmp .L64
 265               	.L35:
 266 01ca 6131      		cpi r22,lo8(17)
 267 01cc 01F4      		brne .L36
 268 01ce E091 0000 		lds r30,curGamepad
 269 01d2 F091 0000 		lds r31,curGamepad+1
 270 01d6 0284      		ldd __tmp_reg__,Z+10
 271 01d8 F385      		ldd r31,Z+11
 272 01da E02D      		mov r30,__tmp_reg__
 273 01dc 00C0      		rjmp .L61
 274               	.L36:
 275 01de 6231      		cpi r22,lo8(18)
 276 01e0 01F4      		brne .L37
 277 01e2 E091 0000 		lds r30,curGamepad
 278 01e6 F091 0000 		lds r31,curGamepad+1
 279 01ea 0284      		ldd __tmp_reg__,Z+10
 280 01ec F385      		ldd r31,Z+11
 281 01ee E02D      		mov r30,__tmp_reg__
 282               	.L58:
 283 01f0 60E0      		ldi r22,0
 284 01f2 80E0      		ldi r24,0
 285               	.L59:
 286 01f4 0995      		icall
 287               	.L57:
 288 01f6 81E0      		ldi r24,lo8(1)
 289 01f8 0895      		ret
 290               	.L37:
 291 01fa 6431      		cpi r22,lo8(20)
 292 01fc 01F4      		brne .L38
 293 01fe E091 0000 		lds r30,curGamepad
 294 0202 F091 0000 		lds r31,curGamepad+1
 295 0206 0084      		ldd __tmp_reg__,Z+8
 296 0208 F185      		ldd r31,Z+9
 297 020a E02D      		mov r30,__tmp_reg__
 298               	.L61:
 299 020c 6091 0000 		lds r22,rumbleLoop
 300 0210 8091 0000 		lds r24,rumbleLevel
 301 0214 00C0      		rjmp .L59
 302               	.L38:
 303 0216 6831      		cpi r22,lo8(24)
 304 0218 01F4      		brne .L39
 305 021a E091 0000 		lds r30,curGamepad
 306 021e F091 0000 		lds r31,curGamepad+1
 307 0222 0084      		ldd __tmp_reg__,Z+8
 308 0224 F185      		ldd r31,Z+9
 309 0226 E02D      		mov r30,__tmp_reg__
 310 0228 00C0      		rjmp .L58
 311               	.L39:
 312 022a 6A31      		cpi r22,lo8(26)
 313 022c 01F4      		brne .L40
 314 022e E091 0000 		lds r30,curGamepad
 315 0232 F091 0000 		lds r31,curGamepad+1
 316 0236 0284      		ldd __tmp_reg__,Z+10
 317 0238 F385      		ldd r31,Z+11
 318 023a E02D      		mov r30,__tmp_reg__
 319 023c 00C0      		rjmp .L60
 320               	.L40:
 321 023e 6B31      		cpi r22,lo8(27)
 322 0240 01F4      		brne .L41
 323 0242 E091 0000 		lds r30,curGamepad
 324 0246 F091 0000 		lds r31,curGamepad+1
 325 024a 0084      		ldd __tmp_reg__,Z+8
 326 024c F185      		ldd r31,Z+9
 327 024e E02D      		mov r30,__tmp_reg__
 328               	.L60:
 329 0250 6AE0      		ldi r22,lo8(10)
 330 0252 8FEF      		ldi r24,lo8(-1)
 331 0254 00C0      		rjmp .L59
 332               	.L41:
 333 0256 6C31      		cpi r22,lo8(28)
 334 0258 01F4      		brne .L42
 335 025a 82E0      		ldi r24,lo8(2)
 336 025c 90E0      		ldi r25,0
 337 025e 9093 0000 		sts setParm+1,r25
 338 0262 8093 0000 		sts setParm,r24
 339 0266 81E0      		ldi r24,lo8(1)
 340 0268 8093 0000 		sts parmType,r24
 341 026c 0895      		ret
 342               	.L42:
 343 026e 6032      		cpi r22,lo8(32)
 344 0270 01F4      		brne .L43
 345 0272 81E0      		ldi r24,lo8(1)
 346 0274 90E0      		ldi r25,0
 347 0276 9093 0000 		sts setParm+1,r25
 348 027a 8093 0000 		sts setParm,r24
 349 027e 82E0      		ldi r24,lo8(2)
 350 0280 00C0      		rjmp .L63
 351               	.L43:
 352 0282 6132      		cpi r22,lo8(33)
 353 0284 01F4      		brne .L44
 354 0286 2091 0000 		lds r18,_HOTSWAP_DISABLED
 355 028a 48E0      		ldi r20,lo8(8)
 356 028c 249F      		mul r18,r20
 357 028e 9001      		movw r18,r0
 358 0290 1124      		clr __zero_reg__
 359 0292 8091 0000 		lds r24,_UDLR
 360 0296 40E1      		ldi r20,lo8(16)
 361 0298 849F      		mul r24,r20
 362 029a C001      		movw r24,r0
 363 029c 1124      		clr __zero_reg__
 364 029e 282B      		or r18,r24
 365 02a0 8091 0000 		lds r24,_autoPause
 366 02a4 282B      		or r18,r24
 367 02a6 8091 0000 		lds r24,_AUTO_PAUSE_DIABLED
 368 02aa 30E4      		ldi r19,lo8(64)
 369 02ac 839F      		mul r24,r19
 370 02ae C001      		movw r24,r0
 371 02b0 1124      		clr __zero_reg__
 372 02b2 282B      		or r18,r24
 373 02b4 8091 0000 		lds r24,_DPAD_ONLY
 374 02b8 40E8      		ldi r20,lo8(-128)
 375 02ba 849F      		mul r24,r20
 376 02bc C001      		movw r24,r0
 377 02be 1124      		clr __zero_reg__
 378 02c0 282B      		or r18,r24
 379 02c2 2093 0000 		sts outData,r18
 380 02c6 00C0      		rjmp .L65
 381               	.L44:
 382 02c8 6232      		cpi r22,lo8(34)
 383 02ca 01F4      		brne .L45
 384 02cc 87E1      		ldi r24,lo8(23)
 385 02ce 90E0      		ldi r25,0
 386 02d0 9093 0000 		sts setParm+1,r25
 387 02d4 8093 0000 		sts setParm,r24
 388 02d8 83E0      		ldi r24,lo8(3)
 389 02da 00C0      		rjmp .L63
 390               	.L45:
 391 02dc 6332      		cpi r22,lo8(35)
 392 02de 01F4      		brne .L46
 393 02e0 81E0      		ldi r24,lo8(1)
 394 02e2 90E0      		ldi r25,0
 395 02e4 9093 0000 		sts setParm+1,r25
 396 02e8 8093 0000 		sts setParm,r24
 397 02ec 84E0      		ldi r24,lo8(4)
 398 02ee 00C0      		rjmp .L63
 399               	.L46:
 400 02f0 6432      		cpi r22,lo8(36)
 401 02f2 01F4      		brne .L47
 402 02f4 81E0      		ldi r24,lo8(1)
 403 02f6 90E0      		ldi r25,0
 404 02f8 9093 0000 		sts setParm+1,r25
 405 02fc 8093 0000 		sts setParm,r24
 406 0300 85E0      		ldi r24,lo8(5)
 407               	.L63:
 408 0302 8093 0000 		sts parmType,r24
 409 0306 00C0      		rjmp .L57
 410               	.L47:
 411 0308 6F3F      		cpi r22,lo8(-1)
 412 030a 01F4      		brne .L48
 413 030c 6093 0000 		sts outData,r22
 414 0310 81E0      		ldi r24,lo8(1)
 415 0312 0E94 0000 		call sendBridgeData
 416 0316 8FEF      		ldi r24,lo8(-1)
 417 0318 0895      		ret
 418               	.L48:
 419 031a 81EF      		ldi r24,lo8(-15)
 420               	.L64:
 421 031c 8093 0000 		sts outData,r24
 422               	.L65:
 423 0320 81E0      		ldi r24,lo8(1)
 424               	.L62:
 425 0322 0E94 0000 		call sendBridgeData
 426 0326 00C0      		rjmp .L57
 427               	.L49:
 428 0328 80E0      		ldi r24,0
 429 032a 0895      		ret
 431               		.data
 434               	rumbleLoop:
 435 0000 FF        		.byte	-1
 438               	rumbleLevel:
 439 0001 FF        		.byte	-1
 440               		.local	outData
 441               		.comm	outData,15,1
 442               		.local	parmType
 443               		.comm	parmType,1,1
 444               		.local	setParm
 445               		.comm	setParm,2,1
 446               		.comm	curGamepad,2,1
 447               		.comm	stupid,1,1
 448               		.comm	_cntrl,1,1
 449               		.comm	_DoNotReport,1,1
 450               		.comm	_autoPause,1,1
 451               		.comm	_PSX,1,1
 452               		.comm	_NeedsLCDWrite,1,1
 453               		.comm	_NeedsWrite,1,1
 454               		.comm	_MAPPING_SELECTION,1,1
 455               		.comm	_DPAD_ONLY,1,1
 456               		.comm	_AUTO_PAUSE_DIABLED,1,1
 457               		.comm	_HOTSWAP_DISABLED,1,1
 458               		.comm	_ROTATE_CCW_90,1,1
 459               		.comm	_ROTATE,1,1
 460               		.comm	_CENTER,1,1
 461               		.comm	_UDLR,1,1
 462               		.comm	pressureButton4,1,1
 463               		.comm	pressureButton3,1,1
 464               		.comm	pressureButton2,1,1
 465               		.comm	pressureButton1,1,1
 466               		.comm	mapper,24,1
 467               		.comm	reportBuffer,14,1
 468               		.comm	sega_buffer,9,1
 469               		.comm	psx_pressure_data,12,1
 470               		.comm	payload_data_size,1,1
 471               		.comm	payload_data,257,1
 472               		.comm	_BRIDGE_MODE,1,1
 473               		.comm	_Control_Data,1,1
 474               		.comm	_Control_Data_Occured,1,1
 475               		.ident	"GCC: (AVR_8_bit_GNU_Toolchain_3.4.5_1522) 4.8.1"
 476               	.global __do_copy_data
 477               	.global __do_clear_bss
DEFINED SYMBOLS
                            *ABS*:00000000 bridge.c
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:2      *ABS*:0000003e __SP_H__
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:3      *ABS*:0000003d __SP_L__
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:4      *ABS*:0000003f __SREG__
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:5      *ABS*:00000000 __tmp_reg__
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:6      *ABS*:00000001 __zero_reg__
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:10     .text:00000000 sendBridgeData
                             .bss:00000000 outData
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:91     .text:0000007e getBridgeCommand
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:110    .text:00000096 wait96
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:443    .bss:00000010 setParm
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:441    .bss:0000000f parmType
                            *COM*:00000001 _HOTSWAP_DISABLED
                            *COM*:00000001 _UDLR
                            *COM*:00000001 _AUTO_PAUSE_DIABLED
                            *COM*:00000001 _MAPPING_SELECTION
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:438    .data:00000001 rumbleLevel
C:\Users\ulao\AppData\Local\Temp\ccum7KaN.s:434    .data:00000000 rumbleLoop
                            *COM*:00000018 mapper
                            *COM*:00000001 _cntrl
                            *COM*:0000000e reportBuffer
                            *COM*:0000000c psx_pressure_data
                            *COM*:00000002 curGamepad
                            *COM*:00000001 _autoPause
                            *COM*:00000001 _DPAD_ONLY
                            *COM*:00000001 stupid
                            *COM*:00000001 _DoNotReport
                            *COM*:00000001 _PSX
                            *COM*:00000001 _NeedsLCDWrite
                            *COM*:00000001 _NeedsWrite
                            *COM*:00000001 _ROTATE_CCW_90
                            *COM*:00000001 _ROTATE
                            *COM*:00000001 _CENTER
                            *COM*:00000001 pressureButton4
                            *COM*:00000001 pressureButton3
                            *COM*:00000001 pressureButton2
                            *COM*:00000001 pressureButton1
                            *COM*:00000009 sega_buffer
                            *COM*:00000001 payload_data_size
                            *COM*:00000101 payload_data
                            *COM*:00000001 _BRIDGE_MODE
                            *COM*:00000001 _Control_Data
                            *COM*:00000001 _Control_Data_Occured

UNDEFINED SYMBOLS
__eeupd_byte_m328p
__do_copy_data
__do_clear_bss

 

 

 

Last Edited: Wed. Aug 19, 2020 - 06:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And the matching source for "bridge.c" ?

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Perhaps it would help if you posted a small complete project that demo's the problem, so others can try it out!

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is the source, its got a few comment sections atm.  Gamepad just has a few variables.

 

ki0bk : Yeah kind of working on that for my self, there is a lot going on in this project. If I get it isolated ill share.

 

#include "../BASE/gamepad.h"
#include <util/delay.h>
#include <avr/eeprom.h>
#include <avr/wdt.h>
static unsigned int setParm=0;
static unsigned char parmType=0;
#define ERROR_UNKNOWN_COMMAND 0xf1;
#define ERROR_NO_ERROR 0xf0;
#define ERROR_NO_COMMAND 0xff;
#define PARM_RUMBLE 01
#define PARM_MODES 02
#define PARM_MAPPER 03
#define PARM_PLAYER 04
#define PARM_ALT_MAP 05
#define _DATA 0x01
#define _LATCH 0x04
#define debug 0x04

#define END_SIG do {PORTD |= _DATA;_delay_us(.8);PORTD &= ~_DATA;} while(0)
#define END_TALK do { DDRD |= 0x05;} while(0)
static unsigned char outData[15];
static unsigned char rumbleLevel=255;
static unsigned char rumbleLoop=255;

void sendBridgeData(unsigned char size )
{
    //reply command needs a buffer and time to reply.
    _delay_us(5);//leave some us in-between.
    PORTD |= 0x05; _delay_us(1);//let bridge sync in (any amount of time is ok here)
    PORTD &= ~0x05;_delay_us(.5); //Low for 1.5 us before the first rising  bit.
    for (char data=0;data<size;data++)
    {
        //_delay_us(.2);
        char bit=0;
        for (;bit<8;bit++)
        {

            PORTD |= _DATA;//set bit hi
            _delay_us(1.6);
            if ( ~outData[data] & 0x01 ) {_delay_us(1.4); asm("nop\n");} else {PORTD &= ~_DATA;_delay_us(1.31); asm("nop\n");}
            outData[data] >>=1;
        }
        END_SIG; //end byte signature. leave latch low.
    }
    END_TALK;//both high
}
char getBridgeCommand( void )
{
// DDRB |= debug;
// PORTB |= debug;
// PORTB &= ~debug;

    unsigned int count =0;
    unsigned char command=0xff;
    //send the ok to talk signal
    DDRD &= ~0x05;//set both sending or not.
    PORTD |= 0x05;//pull up/in, update is finished.
    _delay_us(3);

    //at this point the lines should go high, because this is where we send the ok to talk, otherwise its low anyways.
    //while(1) //wait for talk. loop while high
//    {
    /* - Why even count?  when will it be high during normal conditions?
         1. A command is on the way, it will go low
         2. LLAPI lines removed, go to USB, it will go low.
         3. lines unplugged, let WDT reset. It's unplugged anyways.
        count++;//this is not timed yet but should be around 10 ms or so. There is no matter of time that is required. We just need to break to check others steps.
        if (count > 65000) return -1;//just reject, never saw a command.
    */

    /*
PORTB |= 0x04;PORTB &= ~0x04;//debug
        if  ( (PIND & 0x05) != 0x05)
        {
PORTB &= ~0x04;
            for (char i = 0; i < 8; i++) { command >>=1; if ( (PIND & _DATA) != _DATA) command |= 0x80; _delay_us(4); }//read command.
            break;
        }
    }
    */
DDRB |= 0x04;
PORTB |= 0x04;PORTB &= ~0x04;//debug
    asm volatile(
    "wait%=:\            \n"
    "    sbic    %0,0    \n"//latch line
    "    breq wait%=        \n"
    : : "I" (_SFR_IO_ADDR(PIND))    // %0
        );
PORTB |= 0x04;    

    for (char i = 0; i < 8; i++) { command >>=1; if ( (PIND & _DATA) != _DATA) command |= 0x80; _delay_us(4); }//read command.

    //_delay_us(3.4);//sync
    _delay_us(3); //let wave form finish.  

    DDRD |= 0x05;
    PORTD &= ~0x05;//set both sending or not. Remember in all cases leave out and low returning 1 or 0. Once it loops it will set hi and in.

    // set setParm gets data for the length of the parm
    // i.e setParm=3 means 3 more bytes to come.
    if (setParm)
    {
        if ( parmType == PARM_MODES)
        {
            //na
            //if (data[4] & 2)
            //if (data[4] & _DATA) _TURBO_MODE=1; else _TURBO_MODE=0;
            if (command & 0x08) _HOTSWAP_DISABLED=1; else _HOTSWAP_DISABLED=0;
            if (command & 0x10) _UDLR=1; else _UDLR=0;
            //if (command & 0x20) _ANALOG_TO_DPAD=1;   else _ANALOG_TO_DPAD=0;
            if (command & 0x40) _AUTO_PAUSE_DIABLED=1;else _AUTO_PAUSE_DIABLED=0;
        }
        else if ( parmType == PARM_PLAYER && setParm == 1)  eeprom_update_byte(EE_PLAYER_NUMBER,command);
        else if ( parmType == PARM_ALT_MAP&& setParm == 1)  _MAPPING_SELECTION=command;
        else if ( parmType == PARM_RUMBLE )
        {
                if (setParm == 1) rumbleLevel=command; //TODO test this
                else /* == 2 */  rumbleLoop=command;
        }
        else if (parmType == PARM_MAPPER )
        {//mapper[24]={1,2,3,4,5, 6, 7, 8, 9,10,11 ,12 ,13 ,14 ,15,16,17,18,19,20,21,22,23,24};
             mapper[23 - setParm]=command;
             for (char i=0;i<24;++i) mapper[i]=i+1;
        }
        setParm --;
        return 1; //no need for replies and this could repeat a command so we best exit here.
    } else  { parmType = 0 ;} //clear parm type

        //carry out command.
    if ( command==0) { return 0;}//return with poll

    else if (command==1) //get payload.
    {
        outData[0]=_cntrl;
        for (char i =1;i<14;i++) { outData[i]=reportBuffer[i];}
        sendBridgeData(13);
        return 1; //for no error
    }
    else if (command==2) //get pressure.
    {
        outData[0]=reportBuffer[0];
        for (char i =0;i<13;i++) { outData[i+1]= psx_pressure_data[i] ;}
        sendBridgeData(13);
        return 1; //for no error
    }
    else if (command==3) //get status.
    {
        char states =0;
        states = reportBuffer[0] & 3;//get gun trigger and light.
        outData[0]=states;
        sendBridgeData(1);
        return 1; //for no error
    }    

    //1X commands Rumble
    else if (command==0x11) { curGamepad->rumbleConst(rumbleLevel,rumbleLoop);}//start parm
    else if (command==0x12) { curGamepad->rumbleConst(0,0);    }//end
    else if (command==0x14) { curGamepad->rumbleSine(rumbleLevel,rumbleLoop);}//start parm
    else if (command==0x18) { curGamepad->rumbleSine(0,0);      }//end
    else if (command==0x1A) { curGamepad->rumbleConst(255,10); }//jolt
    else if (command==0x1B) { curGamepad->rumbleSine(255,10);  }//jolt
    else if (command==0x1C) { setParm = 2;parmType=PARM_RUMBLE;}//set rumble parms. (level, loop)

    //sets  gets  2x commands
    else if (command==0x20) {setParm = 1; parmType=PARM_MODES;}
    else if (command==0x21)
    {
        outData[0]= _autoPause;
        //1
        //free outBuffer[1] |= (_TURBO_MODE << 2);
        outData[0] |= (_HOTSWAP_DISABLED << 3);
        outData[0] |= (_UDLR << 4);
        //outData[0] |= (_ANALOG_TO_DPAD<< 5);
        outData[0] |= (_AUTO_PAUSE_DIABLED<< 6);
        outData[0] |= (_DPAD_ONLY << 7);
        sendBridgeData(1);
    }
    else if (command==0x22) {setParm = 23; parmType=PARM_MAPPER;}
    else if (command==0x23) {setParm = 1; parmType=PARM_PLAYER;}
    else if (command==0x24) {setParm = 1; parmType=PARM_ALT_MAP;}
    else if (command==0xff){outData[0] = ERROR_NO_COMMAND; sendBridgeData(1); return 0xff; }
    else  {outData[0] = ERROR_UNKNOWN_COMMAND; sendBridgeData(1); }

    //this seems not to be working? If I put this in the reply steps on the next write, so we;d have to extend the righ.
    //best to leave there here for debug as it would take a long time to do many write with this reply here.
    //outData[0] = ERROR_NO_ERROR; sendBridgeData(1);

    return 1; //return with no poll and show return response
}
    

   

Last Edited: Wed. Aug 19, 2020 - 08:14 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yikes it's getting a bit messy now. Time for a clean-up I think.

 

Are you sure you've identified the correct section of code that pertains to the logic analyser ?

Is there another section of code that also toggles PORTB2 ?

 

The while(1) seems to have dropped out. With the while(1) commented back in; the posted code generates a tight 9-cycle loop. We can't be looking at the right section.

 

Last Edited: Wed. Aug 19, 2020 - 09:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm sorry that's why I'd like to stick with the relevant section. I know this is the relent code for a fact as I'm staring right at it. I'm pretty sure that was all well explained up top but lets narrow this down.

 

 

I decided to go with ASM in my code to make things much easier, and I really can not see a reason to include all that other code.  I'm happy to of course, but like you said it just gets messy.

 

Take this code for example. And for simplicity let's just forget about the two versions I see. At this point I just want to know why 4 extra us? I'm only wanting the top line now and it clearly goes down 4.5 us before my end debug. I put a analog scope on this (seem my image up top) and the voltage is good.

 

DDRB |= 0x04;//allow debug
PORTB |= 0x04; _delay_us(4);PORTB &= ~0x04;//debug so I can find it on my anylyzer.
    asm volatile(
    "wait%=:\            \n"
    "    sbic    %0,2    \n"//latch line
    "    breq wait%=        \n"//iuf latch is low we done.
    : : "I" (_SFR_IO_ADDR(PIND))    // %0
        );
PORTB |= 0x04;  //end the debug so I can see it

    for (char i = 0; i < 8; i++) { command >>=1; if ( (PIND & _DATA) != _DATA) command |= 0x80; _delay_us(4); }//read command.

Attached the analyzer with my markup. My main point was indeed why 4.5 us one time and .5us another, but the simple fact is that there is no reason to spend 4us to exit the loop. This is what has me confused.

 

 

 

Attachment(s): 

Last Edited: Wed. Aug 19, 2020 - 10:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why it is the crap you think is not it, always turns out to be the problem? It was in fact the interrupts cli(); fixed it. Really should have listen to that voince in the back of my head but no.... my smart ass was convenienced interrupts would not cause 4us. I can see it cause a hiccup up here and there but make it always return 4 extra us?

Last Edited: Thu. Aug 20, 2020 - 12:21 AM