DOC PREVIEW
UNCC ECGR 4101 - C Start-Up and Simple Digital IO

This preview shows page 1-2-23-24 out of 24 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 24 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

C Start-Up Module and 7-1Embedded Systemsand Simple Digital I/O Lecture 7In these notes . . . C Start-Up Module– Why is it needed?– How is it done?– MCPM, section 2.2Simple Digital I/O– Port• Data Direction•DataEmbedded Systems 7-2•Data• Drive Capacity– Use• Initialization• Reading• Writing– Example• Echo LEDsC Start-Up Module – Why?MCU must be configured to run in correct mode– Are memory wait states needed?Program sections must be allocated to specific parts of memory– Code, data, interrupt vectorsSome C constructs must be initialized before the program starts running– Where does the stack start?– What about initialized static variables?Other C constructs are created or managed “on the fly”Embedded Systems 7-3Other C constructs are created or managed “on the fly”– Building a stack frame (activation record) to call a subroutine– Dynamically allocating blocks of memory– Don’t have to do anything about theseMain() function must be calledC Start-Up Module Functions – ncrt0.a30Include sect30.inc – provides supporting informationInitialize stack pointer to top of stack areaConfigure MCU to use external clock without frequency divisionLoad pointer for relocatable (variable) interrupt vector tableZero out bss section– use a macro to write zeroesInitialize data section–use a macro to copy dataEmbedded Systems 7-4–use a macro to copy dataInitialize heap, if it existsCall main as a subroutineExecute infinite loop if main ends and control returns heresect30.incDefine sizes and macros– heap size– user stack size (if RTOS used)– interrupt stack size– interrupt vector address– define macros for • zeroing data (N_BZERO) • copying data (N_BCOPY)•initializing heap control – Near RAM• data_NE, bss_NE• data_NO,bss_NO• stack• interrupt stack• heap– Near ROM• rom_NE, rom_NO–Far ROMEmbedded Systems 7-5•initializing heap control variables (HEAPINIT)• special page interrupt vectorsArrange memory (sub)sections– SBDATA (static base data)• data_SE, bss_SE• data_SO, bss_SO–Far ROM• rom_FE, rom_FO• data_SEI, data_SOI, data_NEI, data_NOI, data_FEI, data_FOI• switch tableDefine interrupt vectors– variable vectors– fixed vectorsSetting Stack Size in Sect30.incSTACKSIZE .equ 0hISTACKSIZE .equ 80hStack is used for calling subroutines, handling interrupts, and temporary local storageTwo stacks pointers available: USP and ISP– Which pointer is active depends upon the stack pointer select bit (“U”) in the flag register. See M16C Software Manual, p. 5–U is cleared on reset, so ISP is selected initiallyEmbedded Systems 7-6–U is cleared on reset, so ISP is selected initially– USP is used if an RTOS (real-time operating system) is present– We will use ISP onlyHow big of a stack do we need? Coming up later…– Too small and the program will crash as stack overwrites static variables, and static variables overwrite stack frame– Too large and we waste RAMAllocating Sections to RAM or ROMdata_SEAEmbedded Systems 7-7data and bss sections are subdivided for efficiency at run-time– Addressable from static base register? (within 64K of SB?)• yes, put in S subsection• no, if address in range 0-0FFFFh put in N subsection (near)• no, else put in F subsection (far)– Even data size? (can ensure all accesses are aligned with bus)• yes, put in E subsection• no, put in O subsectionAlso have I subsection (in ROM) for initial data valuesdata_SEbss_SEdata_SObss_SOdata_NEbss_NEdata_NObss_NOstackistackheapDirectives for Arranging Sectionssect30.inc defines where each section goes in memorySection directive has two formats– Allocate section to a memory area – this version is used here• .section name, area_type[CODE|ROMDATA|DATA], [ALIGN]–Place the following code in a certain ; Near RAM data area; SBDATA area.section data_SE,DATA.org 400Hdata_SE_top:.section bss_SE,DATA,ALIGNbss_SE_top:.section data_SO,DATAdata_SO_top:.section bss_SO,DATAbss_SO_top:; near RAM area.section data_NE,DATA,ALIGNdata_NE_top:.sectionbss_NE,DATA,ALIGN. . .Embedded Systems 7-8–Place the following code in a certain section• .section name.sectionbss_NE,DATA,ALIGN. . ..blkb STACKSIZEstack_top:.blkb ISTACKSIZEistack_top:.section heap,DATAheap_top:.blkb HEAPSIZE.section rom_FE,ROMDATA.org 0A0000Hrom_FE_top:.section rom_FO,ROMDATArom_FO_top:Macros to Initialize Memory Sections – sect30.incDefine two assembly-language macrosN_BZERO – zero out bytes – Two arguments• TOP_: start address of memory section• SECT_: name of memory section – Uses instruction sstr.b (string store - byte) to store R0L at addressses A1 to A1+R3–sizeof is assembler directiveN_BZERO .macro TOP_ ,SECT_mov.b #00H, R0Lmov.w #(TOP_ & 0FFFFH), A1mov.w #sizeof SECT_ , R3sstr.b.endmN_BCOPY .macro FROM_,TO_,SECT_mov.w #(FROM_ & 0FFFFH),A0mov.b#(FROM_ >>16),R1HEmbedded Systems 7-9–sizeof is assembler directiveN_BCOPY– Three arguments• FROM_: start address of source data• TO_: start address of destination• SECT_: name of memory section– Uses instruction smovf.b (string move forward - byte) to copy R3 bytes of data from address A0 to address A1mov.b#(FROM_ >>16),R1Hmov.w #TO_ ,A1mov.w #sizeof SECT_ , R3smovf.b.endmInitialize Memory Sections – ncrt30.a30ncrt0.a30 calls macros defined in sect30.incFill bss sections with zeros– N_BZERO Copy initialized data from ROM– N_BCOPY==========================================; Variable area initialize. This code uses ; the macros in "sect30.inc“ for initializing ; C variables. Clears global variables, sets ; initialized variables, etc.;==========================================; NEAR area initialize.;------------------------------------------------; bss zero clear;------------------------------------------------N_BZERO bss_SE_top,bss_SEN_BZERO bss_SO_top,bss_SON_BZERO bss_NE_top,bss_NEN_BZERObss_NO_top,bss_NOEmbedded Systems 7-10N_BZERObss_NO_top,bss_NO;------------------------------------------------; initialize data section;------------------------------------------------N_BCOPY data_SEI_top,data_SE_top,data_SEN_BCOPY data_SOI_top,data_SO_top,data_SON_BCOPY data_NEI_top,data_NE_top,data_NEN_BCOPY data_NOI_top,data_NO_top,data_NODefine Fixed Interrupt Vector Table - sect30.incFirst locate it in the fixed vector section (MCPM p.85)– Hardware expects it to be at 0FFFDCh, so put it there.section fvector.org 0FFFDChThen fill in entries– insert name of ISR at appropriate


View Full Document

UNCC ECGR 4101 - C Start-Up and Simple Digital IO

Documents in this Course
Load more
Download C Start-Up and Simple Digital IO
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view C Start-Up and Simple Digital IO and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view C Start-Up and Simple Digital IO 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?