comparison pru_rtaudio.p @ 39:638bc1ae2500 staging

Improved readibility of the DIGITAL code in the PRU, using register names instead of aliases and expanding some of the macros, removing unused macros. Binaries were not modified
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 13 May 2015 12:18:10 +0100
parents a9af130097e8
children 419ce4ebfc4c
comparison
equal deleted inserted replaced
38:a9af130097e8 39:638bc1ae2500
187 // r1, r2, r3 are used for temporary storage 187 // r1, r2, r3 are used for temporary storage
188 #define MEM_DIGITAL_BASE 0x11000 //Base address for DIGITAL : Shared RAM + 0x400 188 #define MEM_DIGITAL_BASE 0x11000 //Base address for DIGITAL : Shared RAM + 0x400
189 #define MEM_DIGITAL_BUFFER1_OFFSET 0x400 //Start pointer to DIGITAL_BUFFER1, which is 256 words after. 189 #define MEM_DIGITAL_BUFFER1_OFFSET 0x400 //Start pointer to DIGITAL_BUFFER1, which is 256 words after.
190 // 256 is the maximum number of frames allowed 190 // 256 is the maximum number of frames allowed
191 191
192 #define reg_digital_current r6 // Pointer to current storage location of DIGITAL 192 #define r27_current r6 // Pointer to current storage location of DIGITAL
193 #define reg_num_channels r9 // Number of SPI ADC/DAC channels to use 193 #define reg_num_channels r9 // Number of SPI ADC/DAC channels to use
194 #define reg_frame_current r10 // Current frame count in SPI ADC/DAC transfer 194 #define reg_frame_current r10 // Current frame count in SPI ADC/DAC transfer
195 #define reg_frame_total r11 // Total frame count for SPI ADC/DAC 195 #define reg_frame_total r11 // Total frame count for SPI ADC/DAC
196 #define reg_dac_data r12 // Current dword for SPI DAC 196 #define reg_dac_data r12 // Current dword for SPI DAC
197 #define reg_adc_data r13 // Current dword for SPI ADC 197 #define reg_adc_data r13 // Current dword for SPI ADC
236 #define GPIO2 0x481ac000 236 #define GPIO2 0x481ac000
237 //#define GPIO_CLEARDATAOUT 0x190 //SETDATAOUT is CLEARDATAOUT+4 237 //#define GPIO_CLEARDATAOUT 0x190 //SETDATAOUT is CLEARDATAOUT+4
238 #define GPIO_OE 0x134 238 #define GPIO_OE 0x134
239 #define GPIO_DATAIN 0x138 239 #define GPIO_DATAIN 0x138
240 240
241 //gpioX_oe must be adjacent to gpioX_datain
242 //gpioX_cleardataout must be adjacent to gpioX_setdataout 241 //gpioX_cleardataout must be adjacent to gpioX_setdataout
243 #define reg_gpio1_oe r2 242 #define reg_gpio1_oe r2
244 #define reg_gpio2_oe r3 243 #define reg_gpio2_oe r3
245 #define reg_gpio2_cleardataout r4 244 #define reg_gpio2_cleardataout r4
246 #define reg_gpio2_setdataout r5 245 #define reg_gpio2_setdataout r5
247 #define reg_gpio1_cleardataout r7 246 #define reg_gpio1_cleardataout r7
248 #define reg_gpio1_setdataout r8 247 #define reg_gpio1_setdataout r8
249 #define reg_digital r27 //will first contain digitalOut from render() and digitalIn to render() later 248 #define reg_digital r27 //will first contain digitalOut from render() and digitalIn to render() later
250 //aliases 249 //aliases
251 #define reg_gpio1_datain reg_gpio1_oe 250 #define reg_gpio1_datain r2
252 #define reg_gpio2_datain reg_gpio2_oe 251 #define reg_gpio2_datain r3
253
254 .macro SET_GPIO1_BITS
255 .mparam gpio_num_bit, digital_bit
256 SET_GPIO_BITS reg_gpio1_oe, reg_gpio1_setdataout, reg_gpio1_cleardataout, gpio_num_bit, digital_bit, reg_digital
257 .endm
258 .macro SET_GPIO2_BITS
259 .mparam gpio_num_bit, digital_bit
260 SET_GPIO_BITS reg_gpio2_oe, reg_gpio2_setdataout, reg_gpio2_cleardataout, gpio_num_bit, digital_bit, reg_digital
261 .endm
262 .macro READ_GPIO1_BITS
263 .mparam gpio_num_bit, digital_bit
264 READ_GPIO_BITS reg_gpio1_datain, gpio_num_bit, digital_bit, reg_digital
265 .endm
266 .macro READ_GPIO2_BITS
267 .mparam gpio_num_bit, digital_bit
268 READ_GPIO_BITS reg_gpio2_datain, gpio_num_bit, digital_bit, reg_digital
269 .endm
270 252
271 .macro READ_GPIO_BITS 253 .macro READ_GPIO_BITS
272 .mparam gpio_data, gpio_num_bit, digital_bit, digital 254 .mparam gpio_data, gpio_num_bit, digital_bit, digital
273 QBBC DONE, digital, digital_bit //if the pin is set as an output, nothing to do here 255 QBBC DONE, digital, digital_bit //if the pin is set as an output, nothing to do here
274 QBBC CLEAR, gpio_data, gpio_num_bit 256 QBBC CLEAR, gpio_data, gpio_num_bit
297 SET gpio_oe, gpio_num_bit 279 SET gpio_oe, gpio_num_bit
298 QBA DONE 280 QBA DONE
299 DONE: 281 DONE:
300 .endm 282 .endm
301 283
302 .macro PROCESS_GPIO1_BITS
303 //- sets appropriate bits for output in reg_gpio1_oe, reg_gpio1_cleardataout, reg_gpio1_data
304 //- sets appropriate bits in reg_digital to reflect the input values
305 .mparam gpio_num_bit, digital_bit
306 // params to SET_GPIO_BITS gpio_oe, gpio_setdataout, gpio_cleardataout, gpio_num_bit, digital_bit, digital //sets the bits in GPIO_OE, GPIO_SETDATAOUT and GPIO_CLEARDATAOUT
307 SET_GPIO_BITS reg_gpio1_oe, reg_gpio1_setdataout, reg_gpio1_cleardataout, gpio_num_bit, digital_bit, reg_digital
308 READ_GPIO_BITS reg_gpio1_oe, reg_gpio1_datain, gpio_num_bit, digital_bit, reg_digital
309 .endm
310
311 .macro PROCESS_GPIO2_BITS
312 //- sets appropriate bits for output in reg_gpio2_oe, reg_gpio2_cleardataout, reg_gpio2_data
313 //- sets appropriate bits in reg_digital to reflect the input values
314 .mparam gpio_num_bit, digital_bit
315 SET_GPIO_BITS reg_gpio2_oe, reg_gpio2_setdataout, reg_gpio2_cleardataout, gpio_num_bit, digital_bit, reg_digital
316 READ_GPIO_BITS reg_gpio2_oe, reg_gpio2_datain, gpio_num_bit, digital_bit, reg_digital
317 .endm
318
319 QBA START // when first starting, go to START, skipping this section. 284 QBA START // when first starting, go to START, skipping this section.
320 285
321 DIGITAL: 286 DIGITAL:
322 //reg_digital is now the input word passed in render(), one word per frame 287 //IMPORTANT: do NOT use r28 in this macro, as it contains the return address for JAL
288 //r27 is now the input word passed in render(), one word per frame
323 //[31:16]: data(1=high, 0=low), [15:0]: direction (0=output, 1=input) ) 289 //[31:16]: data(1=high, 0=low), [15:0]: direction (0=output, 1=input) )
290
291
324 //Preparing the gpio_oe, gpio_cleardataout and gpio_setdataout for each module 292 //Preparing the gpio_oe, gpio_cleardataout and gpio_setdataout for each module
325 293 //r2 will hold GPIO1_OE
326 //load current status of GPIO_OE in reg_gpioX_oe 294 //load current status of GPIO_OE in r2
327 MOV reg_gpio1_oe, GPIO1 | GPIO_OE 295 MOV r2, GPIO1 | GPIO_OE
328 //it takes 190ns to go through the next instruction 296 //it takes 190ns to go through the next instruction
329 LBBO reg_gpio1_oe, reg_gpio1_oe, 0, 4 297 LBBO r2, r2, 0, 4
330 //GPIO1-start 298 //GPIO1-start
331 //process oe and datain and prepare dataout for GPIO1 299 //process oe and datain and prepare dataout for GPIO1
332 LDI reg_gpio1_setdataout, 0 300 //r7 will contain GPIO1_CLEARDATAOUT
333 LDI reg_gpio1_cleardataout, 0 301 //r8 will contain GPIO1_SETDATAOUT
334 //map GPIO_ANALOG to gpio1 pins, affects reg_gpio1_oe, reg_gpio1_cleardataout, reg_gpio1_data, reg_digital 302 MOV r8, 0
335 SET_GPIO1_BITS 13, 4 303 MOV r7, 0
336 SET_GPIO1_BITS 12, 5 304 //map GPIO_ANALOG to gpio1 pins,
337 SET_GPIO1_BITS 28, 6 305 //r2 is gpio1_oe, r8 is gpio1_setdataout, r7 is gpio1_cleardataout, r27 is the input word
338 SET_GPIO1_BITS 18, 7 306 //the following operations will read from r27 and update r2,r7,r8
339 SET_GPIO1_BITS 15, 8 307 SET_GPIO_BITS r2, r8, r7, 13, 4, r27
340 SET_GPIO1_BITS 14, 9 308 SET_GPIO_BITS r2, r8, r7, 12, 5, r27
341 SET_GPIO1_BITS 19, 10 309 SET_GPIO_BITS r2, r8, r7, 28, 6, r27
310 SET_GPIO_BITS r2, r8, r7, 18, 7, r27
311 SET_GPIO_BITS r2, r8, r7, 15, 8, r27
312 SET_GPIO_BITS r2, r8, r7, 14, 9, r27
313 SET_GPIO_BITS r2, r8, r7, 19, 10, r27
342 //set the output enable register for gpio1. 314 //set the output enable register for gpio1.
343 MOV reg_gpio2_oe, GPIO1 | GPIO_OE //reg_gpio2_oe is now unused. It is here used as a temp 315 MOV r3, GPIO1 | GPIO_OE //use r3 as a temp register
344 SBBO reg_gpio1_oe, reg_gpio2_oe, 0, 4 //takes two cycles (10ns) 316 SBBO r2, r3, 0, 4 //takes two cycles (10ns)
345 //GPIO1-end 317 //GPIO1-end
346 318 // r2 is now unused
347 // reg_gpio1_oe is now unused, its register is taken by gpio2_cleardataout 319
348 // reg_gpio1_datain now unused, its register is taken by gpio2_setdataout
349 //GPIO2-start 320 //GPIO2-start
350 //load current status of GPIO_OE in reg_gpioX_oe 321 //r3 will hold GPIO1_OE
351 MOV reg_gpio2_oe, GPIO2 | GPIO_OE 322 //load current status of GPIO_OE in r3
323 MOV r3, GPIO2 | GPIO_OE
352 //it takes 200ns to go through the next instructions 324 //it takes 200ns to go through the next instructions
353 LBBO reg_gpio2_oe, reg_gpio2_oe, 0, 4 325 LBBO r3, r3, 0, 4
354 //process oe and datain and prepare dataout for GPIO2 326 //process oe and datain and prepare dataout for GPIO2
355 LDI reg_gpio2_setdataout, 0 327 //r4 will contain GPIO2_CLEARDATAOUT
356 LDI reg_gpio2_cleardataout, 0 328 //r5 will contain GPIO2_SETDATAOUT
357 //map GPIO_ANALOG to gpio2 pins, affects reg_gpio2_oe, reg_gpio2_cleardataout, reg_gpio2_data, reg_digital 329 MOV r5, 0
358 SET_GPIO2_BITS 2, 0 330 MOV r4, 0
359 SET_GPIO2_BITS 3, 1 331 //map GPIO_ANALOG to gpio2 pins
360 SET_GPIO2_BITS 5, 2 332 //r3 is gpio2_oe, r5 is gpio2_setdataout, r4 is gpio2_cleardataout, r27 is the input word
361 SET_GPIO2_BITS 4, 3 333 //the following operations will read from r27 and update r3,r4,r5
362 SET_GPIO2_BITS 1, 11 334 SET_GPIO_BITS r3, r5, r4, 2, 0, r27
363 SET_GPIO2_BITS 22, 12 335 SET_GPIO_BITS r3, r5, r4, 3, 1, r27
364 SET_GPIO2_BITS 24, 13 336 SET_GPIO_BITS r3, r5, r4, 5, 2, r27
365 SET_GPIO2_BITS 23, 14 337 SET_GPIO_BITS r3, r5, r4, 4, 3, r27
366 SET_GPIO2_BITS 25, 15 338 SET_GPIO_BITS r3, r5, r4, 1, 11, r27
339 SET_GPIO_BITS r3, r5, r4, 22, 12, r27
340 SET_GPIO_BITS r3, r5, r4, 24, 13, r27
341 SET_GPIO_BITS r3, r5, r4, 23, 14, r27
342 SET_GPIO_BITS r3, r5, r4, 25, 15, r27
367 //set the output enable register for gpio2. 343 //set the output enable register for gpio2.
368 MOV reg_gpio1_oe, GPIO2 | GPIO_OE //reg_gpio1_oe is now unused. It is here used as a temp 344 MOV r2, GPIO2 | GPIO_OE //use r2 as a temp registerp
369 SBBO reg_gpio2_oe, reg_gpio1_oe, 0, 4 //takes two cycles (10ns) 345 SBBO r3, r2, 0, 4 //takes two cycles (10ns)
370 //GPIO2-end 346 //GPIO2-end
371 347 //r3 is now unused
372 //load current inputs in reg_gpioX_datain 348
373 MOV reg_gpio1_datain, GPIO1 | GPIO_DATAIN 349 //load current inputs in r2, r3
374 MOV reg_gpio2_datain, GPIO2 | GPIO_DATAIN 350 //r2 will contain GPIO1_DATAIN
351 //r3 will contain GPIO2_DATAIN
352 //load the memory locations
353 MOV r2, GPIO1 | GPIO_DATAIN
354 MOV r3, GPIO2 | GPIO_DATAIN
375 //takes 375 nns to go through the next two instructions 355 //takes 375 nns to go through the next two instructions
376 LBBO reg_gpio1_datain, reg_gpio1_datain, 0, 4 356 //read the datain
377 LBBO reg_gpio2_datain, reg_gpio2_datain, 0, 4 357 LBBO r2, r2, 0, 4
378 READ_GPIO1_BITS 13, 4 358 LBBO r3, r3, 0, 4
379 READ_GPIO1_BITS 12, 5 359 //now read from r2 and r3 only the channels that are set as input in the lower word of r27
380 READ_GPIO1_BITS 28, 6 360 // and set their value in the high word of r27
381 READ_GPIO1_BITS 18, 7 361 //GPIO1
382 READ_GPIO1_BITS 15, 8 362 READ_GPIO_BITS r2, 13, 4, r27
383 READ_GPIO1_BITS 14, 9 363 READ_GPIO_BITS r2, 12, 5, r27
384 READ_GPIO1_BITS 19, 10 364 READ_GPIO_BITS r2, 28, 6, r27
385 READ_GPIO2_BITS 2, 0 365 READ_GPIO_BITS r2, 18, 7, r27
386 READ_GPIO2_BITS 3, 1 366 READ_GPIO_BITS r2, 15, 8, r27
387 READ_GPIO2_BITS 5, 2 367 READ_GPIO_BITS r2, 14, 9, r27
388 READ_GPIO2_BITS 4, 3 368 READ_GPIO_BITS r2, 19, 10, r27
389 READ_GPIO2_BITS 1, 11 369 //GPIO2
390 READ_GPIO2_BITS 22, 12 370 READ_GPIO_BITS r3, 2, 0, r27
391 READ_GPIO2_BITS 24, 13 371 READ_GPIO_BITS r3, 3, 1, r27
392 READ_GPIO2_BITS 23, 14 372 READ_GPIO_BITS r3, 5, 2, r27
393 READ_GPIO2_BITS 25, 15 373 READ_GPIO_BITS r3, 4, 3, r27
394 //reg_gpio2_oe is now unused, so reg_temp1 is available for temporary storage from now on 374 READ_GPIO_BITS r3, 1, 11, r27
395 //reg_gpio2_datain is now unsued, so reg_temp2 is available for temporary storage from now on 375 READ_GPIO_BITS r3, 22, 12, r27
376 READ_GPIO_BITS r3, 24, 13, r27
377 READ_GPIO_BITS r3, 23, 14, r27
378 READ_GPIO_BITS r3, 25, 15, r27
379 //r2, r3 are now unused
396 380
397 //now all the setdataout and cleardataout are ready to be written to the GPIO register. 381 //now all the setdataout and cleardataout are ready to be written to the GPIO register.
398 //CLEARDATAOUT and SETDATAOUT are consecutive positions in memory, so we write 8 bytes to CLEARDATAOUT. 382 //CLEARDATAOUT and SETDATAOUT are consecutive positions in memory, so we just write 8 bytes to CLEARDATAOUT.
399 //We can do this because we chose reg_gpio1_cleardataout and reg_gpioX_setdataout to be consecutive 383 //We can do this because we chose cleardata and setdata registers for a given GPIO to be consecutive
400 //load the memory addresses to be written to 384 //load the memory addresses to be written to
401 MOV reg_gpio1_datain, GPIO1 | GPIO_CLEARDATAOUT //reg_gpio1_datain is now unused and is used here as a temp 385 MOV r2, GPIO1 | GPIO_CLEARDATAOUT //use r2 as a temp register
402 MOV reg_gpio2_datain, GPIO2 | GPIO_CLEARDATAOUT //reg_gpio2_datain is now unused and is used here as a temp 386 MOV r3, GPIO2 | GPIO_CLEARDATAOUT //use r3 as a temp register
403 //write 8 bytes for each GPIO 387 //write 8 bytes for each GPIO
404 //takes 30ns in total to go through the following two lines 388 //takes 30ns in total to go through the following two instructions
405 SBBO reg_gpio1_cleardataout, reg_gpio1_datain, 0, 8 // takes 145ns to be effective when going low, 185ns when going high 389 SBBO r7, r2, 0, 8 //store r7 and r8 in GPIO1_CLEARDATAOUT and GPIO1_SETDATAOUT
406 SBBO reg_gpio2_cleardataout, reg_gpio2_datain, 0, 8 //takes 95ns to be effective when going low, 130ns when going high 390 //takes 145ns to be effective when going low, 185ns when going high
391 SBBO r4, r3, 0, 8 //store r4 and r5 in GPIO2_CLEARDATAOUT and GPIO2_SETDATAOUT
392 //takes 95ns to be effective when going low, 130ns when going high
407 //reversing the order of the two lines above will swap the performances between the GPIO modules 393 //reversing the order of the two lines above will swap the performances between the GPIO modules
408 //i.e.: the first line will always take 145ns/185ns and the second one will always take 95ns/130ns, 394 //i.e.: the first line will always take 145ns/185ns and the second one will always take 95ns/130ns,
409 //regardless of whether the order is gpio1-gpio2 or gpio2-gpio1 395 //regardless of whether the order is gpio1-gpio2 or gpio2-gpio1
410 JMP r28.w0 // go back to ADC_WRITE_GPIO 396 JMP r28.w0 // go back to ADC_WRITE_AND_PROCESS_GPIO
411 397
412 // DIGITAL new code ends here 398 .macro HANG //useful for debugging
413
414
415 .macro HANG
416 DALOOP: 399 DALOOP:
417 set r30.t14 400 set r30.t14
418 clr r30.t14 401 clr r30.t14
419 QBA DALOOP 402 QBA DALOOP
420 .endm 403 .endm
404
421 // Bring CS line low to write to DAC 405 // Bring CS line low to write to DAC
422 .macro DAC_CS_ASSERT 406 .macro DAC_CS_ASSERT
423 MOV r27, DAC_CS_PIN 407 MOV r27, DAC_CS_PIN
424 MOV r28, DAC_GPIO + GPIO_CLEARDATAOUT 408 MOV r28, DAC_GPIO + GPIO_CLEARDATAOUT
425 SBBO r27, r28, 0, 4 409 SBBO r27, r28, 0, 4
513 QBNE GPIO_DONE, r27, 0 497 QBNE GPIO_DONE, r27, 0
514 //from here to GPIO_DONE takes 1.8us, while usually ADC_WAIT_FOR_FINISH only waits for 1.14us. 498 //from here to GPIO_DONE takes 1.8us, while usually ADC_WAIT_FOR_FINISH only waits for 1.14us.
515 //TODO: it would be better to split the DIGITAL stuff in two parts: 499 //TODO: it would be better to split the DIGITAL stuff in two parts:
516 //- one taking place during DAC_WRITE which sets the GPIO_OE 500 //- one taking place during DAC_WRITE which sets the GPIO_OE
517 //- and the other during ADC_WRITE which actually reads DATAIN and writes CLEAR/SET DATAOUT 501 //- and the other during ADC_WRITE which actually reads DATAIN and writes CLEAR/SET DATAOUT
518 //reg_digital is actually r27, so do not use r27 from here to ... 502 //r27 is actually r27, so do not use r27 from here to ...
519 LBBO reg_digital, reg_digital_current, 0, 4 503 LBBO r27, r27_current, 0, 4
520 JAL r28.w0, DIGITAL 504 JAL r28.w0, DIGITAL // note that this is not called as a macro, but with JAL. r28 will contain the return address
521 SBBO reg_digital, reg_digital_current, 0, 4 505 SBBO r27, r27_current, 0, 4
522 //..here you can start using r27 again 506 //..here you can start using r27 again
523 ADD reg_digital_current, reg_digital_current, 4 //increment pointer 507 ADD r27_current, r27_current, 4 //increment pointer
524 GPIO_DONE: 508 GPIO_DONE:
525 ADC_WAIT_FOR_FINISH 509 ADC_WAIT_FOR_FINISH
526 ADC_RX out 510 ADC_RX out
527 ADC_CS_UNASSERT 511 ADC_CS_UNASSERT
528 .endm 512 .endm
836 LSL reg_mcasp_adc_current, reg_mcasp_adc_current, 1 820 LSL reg_mcasp_adc_current, reg_mcasp_adc_current, 1
837 ADC reg_mcasp_adc_current, reg_mcasp_adc_current, reg_mcasp_dac_current 821 ADC reg_mcasp_adc_current, reg_mcasp_adc_current, reg_mcasp_dac_current
838 MOV reg_frame_current, 0 822 MOV reg_frame_current, 0
839 QBBS DIGITAL_BASE_CHECK_SET, reg_flags, FLAG_BIT_BUFFER1 //check which buffer we are using for DIGITAL 823 QBBS DIGITAL_BASE_CHECK_SET, reg_flags, FLAG_BIT_BUFFER1 //check which buffer we are using for DIGITAL
840 // if we are here, we are using buffer0 824 // if we are here, we are using buffer0
841 MOV reg_digital_current, MEM_DIGITAL_BASE 825 MOV r27_current, MEM_DIGITAL_BASE
842 QBA DIGITAL_BASE_CHECK_DONE 826 QBA DIGITAL_BASE_CHECK_DONE
843 DIGITAL_BASE_CHECK_SET: //if we are here, we are using buffer1 827 DIGITAL_BASE_CHECK_SET: //if we are here, we are using buffer1
844 MOV reg_digital_current, MEM_DIGITAL_BASE+MEM_DIGITAL_BUFFER1_OFFSET //so adjust offset appropriately 828 MOV r27_current, MEM_DIGITAL_BASE+MEM_DIGITAL_BUFFER1_OFFSET //so adjust offset appropriately
845 DIGITAL_BASE_CHECK_DONE: 829 DIGITAL_BASE_CHECK_DONE:
846 830
847 WRITE_LOOP: 831 WRITE_LOOP:
848 // Write N channels to DAC from successive values in memory 832 // Write N channels to DAC from successive values in memory
849 // At the same time, read N channels from ADC 833 // At the same time, read N channels from ADC