C interrupts on Cortex M3 - c

I'm currently trying to implement interrupts on the STM32L152. I'm not using the standard peripheral libraries because I find them very confusing and difficult to get my head around. I'm not too competent with C for micro controllers yet.
I currently do everything through registers. Is there a way to implement interrupts in C through registers? There doesn't seem to be any information that actually makes sense out there. I did find learning C to be very inaccessible in the first place tbh.
Thanks

Of course you can implement interrupts by setting registers.
The registers-values tell the STM how to deal with interrupts, which interrupt is enabled, how the interrupt-controller has to work.
You'll need an Interrupt-vector-table. When an interrupt occurs a the program-counter will be set to an specific address of program-memory. There you should place a jump-command (assembler jmp) to your interrupt-service-routine.
You should read chapter 10 in this reference manual.
Hope this helps.

Related

C code modification from stm32F to stm32L

I'm trying to learn and master embedded C, so I was just gonna test a code that I found on Github, which is built for STM32F4 discovery board. The board that I'm actually working on is STM32L152. when I tried to build/run the code (obviously it won't work) the errors I'm getting are mostly related to functions not being defined or "identifier 'function_name' is undefined", note that the code file includes a library (lib).
take a look at the code file:
https://github.com/TDAbboud/STM32F4_Examples/tree/master/04_PWM_Servo
Generally speaking, What modifications should be done to successfully run the code on STM32L1?
Thanks
These are two different chips. Just because they are from ST just because they are ARM based, they are not the same chip. for starters the stm32f4 is a cortex-m4 the stm32l might not be, 99.9% of your code wont care, just some assembly might. if the stm32l is a cortex-m0 then you have far fewer instructions so the assembly will matter if it is a cortex-m3 then it wont.
The real isssue is peripherals not that this is two st chips not that this is two arm chips, the peripherals can/will vary. ST has a number of chips that use the same uart or same gpio or other, but they have more than one uart they use for STM32 chips, and more than one GPIO. And they mix and match as they make new chips, so if you want to port from one to the other you need to go peripheral by peripheral reading the new and old docs to see what if anything changed.
Sounds like you are using a library so the tool may know from the chip you have chosen what peripherals you have and which library you need, so it might not be finding them because for that chip that peripheral and thus that function and those defines do not apply. Take it one perpheral at a time and port between chips.
stm32 (L) serise incloud EEPROM which are important when they go to sleep mode and want to recover variables after wakup . so make sure application is running in sleep modes need to save data somewhere. so if you use STM (F) series care needs to attache a external EEPROM

Where to find I bit and how to edit it to enable interrupts in ARM Cortex-M4

In ARM Cortex-M4F MCU (TM4C1294NCPDT specifically), to deal with interrupts (GPIO interrupts), one of the steps to get the interrupts working is to clear the I BIT.
I searched a lot but I couldn't find any useful information about that, could anybody please tell me where to find that bit exactly and how to edit it if I need some special procedures?
And that will be great if I have been told where to find that information exactly after the explanation please (to learn how to answer myself on any other questions).
The CMSIS provides a standard cross-vendor software interface to Cortex-M based devices. The CMSIS defines a number of functions for interacting with the NVIC and PRIMASK including the intrinsics __disable_irq()/__enable_irq()
The ARM Cortex-M interrupt system is quite complicated and very well thought. It consists of CPU registers and a tightly coupled interrupt controller (NVIC). Interrupts are prioritized and vectored. There is no single interrupt-enable flag as for smaller 8/16 bit MCUs.
For each interrupt, there are two ARM-core instances to gate the event to the CPU: The CPU PRIMASK register (single bit), which can be seen most similar to the classical interrupt-enable flag. Second is one enable bit in the NVIC. For these, there is an ARM standard in the CMSIS headers. These provide functions __enable_irq() and __disable_irq() for the PRIMASK bit. The peripheral interrupt itself has to be controlled by NVIC_EnableIRQ(IRQn_Type IRQn) where IRQn is the interrupt number as defined in the MCU-specific header file.
Finally, there are most times also interrupt enable bits in each peripheral module as know by the smaller MCUs.
Note that to have an interrupt pass through all gates have to be open (all bits set to "enable"). Use the CMSIS functions to manipuate the bits. They very likely will not take more instructions than a hand-crafted version.
Edit:
There is no actual need to fiddle yourself with assembler or the registers. Just use the CMSIS functions, you can very likely not do better yourself, but possibly worse. That's actually the intention of CMSIS.
(end edit)
Start reading in the reference manual for the MCU and the vendor's homepage. That should provide references and app-notes for the device. You also should read the technical reference manual, architecture reference manual from ARM. Actually, just have a close look at all related documents there for the CPU (M4 for you). These are for free, some require registering.
For the NVIC, you should not access it directly, but using the CMSIS header files as provided by TI for exactly this MCU (the headers require some device-specific settings). If not available,you can get them from ARM, but have to provide the device-specific settings yourself (they are few and are given in the MCU's reference manual).
As the ARM Cortex-M4 has multiple interrupts, you need their symbolic names to enable/disable. These have to be defined in the MCU header which defines all peripheral modules, too (there might be multiple such headers). The names end with _IRQn, just search for that.
To use the Cortex-M4 you should read the documents given, or you can try with a good book. However, as this is no tutorial site, nor is it allowed to recommend books, please search yourself.
OK, the easiest answer for my question is:
To use " CPSID I" or " CPSIE I" inline assembly code which will set or clear the PRIMASK (I) Bit respectively. (of course that will work just in privileged mode).
And both assembly instructions are equivalent to __disable_irq() and __enable_irq() functions in CMSIS respectively.

Why FIQ need to be written in assembly not in C?

I'm going through how FIQ works on ARM and came across the statement that FIQ should always be written in assembly not in C but couldn't understand why?
I have gone through the following link
http://comments.gmane.org/gmane.linux.ports.arm.kernel/14004
But still couldn't make out why is it required?
Can any one please point me out the need of writing FIQ in assembly through some example?
My guess is based on this:
Also, it's a little difficult to write the FIQ code in C, since you lack a stack :)
If there's no stack, that would mean that the compiler is restricted to only using registers for all variables, which I'm not sure how you'd even express.
You can put register on all the local variables, but that doesn't mean that the compiler has to comply.
Writing the code in assembly of course goes around this restriction, and makes it possible to combine registers and global state to do things.
See also this question's answers for more about the difference between an ordinary interrupt and a fast one.
Because what is the point, you are using an extra bank of registers to save a handful of clock cycles in saving the state, then to use C and completely blow that tiny cost savings? If you are not interested in optimizing to that level then dont bother with fiq just use irq.

How do you make a timer for PIC18?

For the life of me I can't make a timer for PIC18 or precisely PIC18f87j11. All I want to do is to have a counter that increments every 1 second. I just want to monitor how long PIC18 been running in terms of seconds.
Most of the tutorials out there are for PIC16 and are in assembly. I am trying to do this using C programming. For someone who is beginner I understand better if I see examples, so without examples I can't progress.
please someone show me an example, thank you!
To begin understanding timers you will need to have a look at their documentation. I suggest searching the manufacturers website. There are 5 timer available. If you use Timer0 which is either a 8 bit or 16bit timer. You will need to set the correct configuration for T0CON. Also you will need to have correct interrupt settings (INTCONbits), so a an event can be generated by interrupt service routine once timer expired.
You will most likely need to calculate the prescale value you require for correct timing, in your case 1 second. It does depend on FOsc: Here is a tutorial
Here an example of C code using MPLab and another one based on MPLab and hi-tech compiler. It is not for same chip, however it is based on PIC18F family.
It's been a awhile since I have visited PIC18F, so if any correction required, please do so.
If you use HIGH-TECH compiler, you can simply use its special function for close, open, read and write timers modules in below directory:
...\HI-TECH Software\PICC-18\9.80\sources\plib\Timers
According to your microcontroller name, you can figure out which version of these function is written for your microcontroller. So for first step, open pconfig.h file and search your microcontroller name to see for every module which version is fit to your microcontroller registers.
Good Luck

How to write an assembly ISR in a C program (AVR)?

Following on from my other question, Help optimising this C (AVR) code? :), I just realised that I don't really know the mechanics of how to implement an ISR in assembly within my C program. I googled, but I didn't find anything too useful.
Basically I want to set up everything else in my C project (including the registers to get the interrupt to fire), but I want the interrupt to execute some assembly code that I've written.
I'm using AVR studio 6 on Windows if that helps. I've been told I need to use .s files but apart from that I'm not really sure what to do. Is the basic structure just the following?
#include <avr\io.h>
TIMER1_COMPA_vect:
; assembly instructions here
If I want the assembly to run when the TIMER1_COMPA_vect interrupt is fired (this is the name of the ISR in C). Or am I totally off track? What is a basic template of what I need to do? Or if it's a dumb question, is there a link where I can find more information?
The Art of Assembly Language (by Randall Hyde) has two chapters about ISRs (17th and 18th, specifically sections 18.5 and 18.6 might help you). Shortly:
Go to the IVT (interrupt vector table) and modify it with your ISR segment and offset, saving the old values
You should do a TSR (terminate and stay resident) program, so that your ISR stays resident into memory even when the user closes the window
Remember to call the old ISR after you're done with the work (this is called interrupt chaining)
your ISR should be re-entrant (so that if the interrupt is fired again when your ISR is still running the pc won't explode ;) )
By the way, you can obtain a pdf copy of that great book here

Resources