Electronics > Microcontrollers

Why do most MCU code start by resetting peripherals?

(1/5) > >>

simonlasnier:
Hi :)

Looking at examples of written startup code for MCU (for example the STM32 HAL lib), I see often that the first thing the code does, right in the ResetHandler interrupt, is resetting the peripheral it is about to configure.
Is there any reasons for this other than "you never know"? Because if we are in the Reset interrupt routine, well the whole system has been reset so the peripheral should already be at reset state?

Thanks

PS: I am using an ARM Cortex M-23 (GD32E230), but I am asking this question generally for all MCU.

JPortici:
"You never know" is a very good reason.
Also don't know about your specific MCU but for many you have register that initialize at predefined value only at POR (and even then you better not trust it) but will not change at other resets

semir-t:
I would assume that when we reset the MCU through the external pin, there would be some internal circuit that would bring the MCU in some default/starting state. But what about when we initiate software reset through CPU instruction. So from my point of view it is better to do this stuff in the startup script then to leave it hanging.

Note: I don't have any reference for the things that I have stated above, it is just my understanding and logic thing that I would do if i designed MCU  :D

jpanhalt:

--- Quote from: semir-t on October 15, 2021, 10:03:45 am ---I would assume that when we reset the MCU through the external pin, there would be some internal circuit that would bring the MCU in some default/starting state. But what about when we initiate software reset through CPU instruction. So from my point of view it is better to do this stuff in the startup script then to leave it hanging.

Note: I don't have any reference for the things that I have stated above, it is just my understanding and logic thing that I would do if i designed MCU  :D

--- End quote ---

Such matters are described in the respective datasheets.  Here is a sample from a Microchip PIC.  It takes 11 pages:

[attach=1]



As for the logic, in Microchip PIC templates, the starting code is like this:
[plain]
;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------
    ORG     0x0000            ; processor reset vector
    PAGESEL START
    GOTO    START             ; When using debug header, first inst.
                                       ; may be passed over by ICD2. 
;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------
    ORG      0x0004
;------------------------------------------------------------------------------
; USER INTERRUPT SERVICE ROUTINE GOES HERE
;------------------------------------------------------------------------------
[/plain]

For that device , the reset and interrupt vectors are set, but you can start the code anywhere.  In fact, I have seen code where the authors put all the set-up housekeeping at the end, then vector back to something after the interrupt vector for the meat.  While that start-up code still runs first, it is not at the top.

tszaboo:
Cause maybe your MCU is reset because of some irregular behavior. For example an ESD event can set the PC to a wrong address, and then it just starts executing code from 0x0000 without resetting the peripherals. Or brownout. Or a dozen different kind of issues.

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version