2009-09-21 11:23 PM
Embedding assembler in C
2011-05-17 04:24 AM
What registers are safe to use in embedded assembler? How do I read the value of an input port? How do I write a bit? I want to make a bit-banged serial interface to clock in data from a device. Anyone done any code like that or knows a good link? I need to generate chip select and clock signals and read data on a port pin on each transition shifting as I go. Planning to use assembler for speed.
How fast would it be if done in peripheral library function calls? (No hardware yet so can't breadboard it).2011-05-17 04:24 AM
Quote:
What registers are safe to use in embedded assembler?
It is impossible to answer that question without knowing precisely what compiler, version, etc, you are using.Quote:
Planning to use assembler for speed.
Have you actually checked if the speed of code produced by your compiler is adequate? If you really do need assembler, you would probably be far better to write the entire routine in assembler to be called from 'C', rather than messing about with dropping odd bits of inline assembler into your 'C' source...2011-05-17 04:24 AM
Thanks for your reply st7. I am using Rowley CrossWorks (GCC) and have been playing with different ways of coding and optimisation. Reading the GPIO (Port A (did I get the address right?)) with,
if((*(__I uint16_t *)(0x40010808)) & 0x0100) Result |= 1; seems to produce sensible assembler (rather than using the library GPIO_ReadInputData(GPIOA)) I have no hardware yet and no way (as far as I know) of checking if the speed of code produced by the compiler is adequate. Any suggestions of different / better ways to write this are welcome. I just want to clock data from a shift register and read it in bit by bit. (Obviously I need to add control of the chip select and clock output pins).2011-05-17 04:24 AM
If you do find that assembly is essential for speed, I think that the simplest approach is to keep the assembly as a separate subroutine. As st7 says, it could depend on the compiler you use but it is likely that the ''Arm Procedure Call Standard'' aapcs will be used.
My copy says to check for later releases. Some compilers such as gcc, have a syntax for embedded assembly where you explicitly state which registers you are clobbering. The syntax is not trivial and I find the examples in the help files hard to understand. But it can be made to work. As for I/O, I find that I get best performance with the DMA hardware built into the stm32. This might require that I do not call the st peripheral library but instead use it as a guide to writing my own code. (There are dangers to this approach...) Hope this helps, Danish