MCBSTM32C/Blinky simple

From Teknologisk videncenter
< MCBSTM32C
Revision as of 08:42, 29 August 2011 by Heth (talk | contribs) (Created page with "=Assembler file: startup.c= <source lang=asm> ; Minimum startup from startup_stm32f10x_cl.s ; Author: heth@mercantec.dk ; Revision 1 - 29-aug-2011 ;Define Stack Stack_Size ...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Assembler file: startup.c

; Minimum startup from startup_stm32f10x_cl.s
; Author: heth@mercantec.dk
; Revision 1 - 29-aug-2011

;Define Stack
Stack_Size      EQU     0x00000200

                AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp


;Define Heapsize
Heap_Size       EQU     0x00000000

                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit


                     
		AREA    RESET, DATA, READONLY
				EXPORT  __initial_sp
                EXPORT  __heap_base
                EXPORT  __heap_limit
		       
			    EXPORT  __Vectors
                EXPORT  __Vectors_End
                EXPORT  __Vectors_Size
		 		
				PRESERVE8
                THUMB  ; Use THUMB instruction set  

; Vector Table Mapped to Address 0 at Reset
__Vectors       DCD     __initial_sp              ; Top of Stack
                DCD     Reset_Handler             ; Reset Handler
__Vectors_End

__Vectors_Size 	EQU 	__Vectors_End - __Vectors

				; Not nessesary HeTh IMPORT  __use_two_region_memory
; Code start
		AREA     startup, CODE, READONLY
				

				;ENTRY
; Reset Handler (Line 150)

Reset_Handler   PROC
                EXPORT  Reset_Handler             [WEAK]
                IMPORT  __main
                LDR     R0, =__main
                BX      R0
                ENDP
              

		END

Headerfile: minimum gpio.h

/****************************************************************
* Author: heth@mercantec.dk
* Revision 1 - 29-aug-2011 
*****************************************************************
* Minimum setup of GPIO 
****************************************************************/
 
//related definitions from stdint.h
typedef unsigned           int uint32_t;

// GPIO related definitions in stm32f10x_cl.h
#define PERIPH_BASE           ((     uint32_t)0x40000000)
#define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)

#define GPIOA_BASE            (APB2PERIPH_BASE + 0x0800)
#define GPIOB_BASE            (APB2PERIPH_BASE + 0x0C00)
#define GPIOC_BASE            (APB2PERIPH_BASE + 0x1000)
#define GPIOD_BASE            (APB2PERIPH_BASE + 0x1400)
#define GPIOE_BASE            (APB2PERIPH_BASE + 0x1800)

#define     __IO    volatile                  /*!< defines 'read / write' permissions   */

typedef struct
{
  __IO uint32_t CRL;
  __IO uint32_t CRH;
  __IO uint32_t IDR;
  __IO uint32_t ODR;
  __IO uint32_t BSRR;
  __IO uint32_t BRR;
  __IO uint32_t LCKR;
} GPIO_TypeDef;

#define GPIOA                 ((GPIO_TypeDef   *) GPIOA_BASE   )
#define GPIOB                 ((GPIO_TypeDef   *) GPIOB_BASE   )
#define GPIOC                 ((GPIO_TypeDef   *) GPIOC_BASE   )
#define GPIOD                 ((GPIO_TypeDef   *) GPIOD_BASE   )
#define GPIOE                 ((GPIO_TypeDef   *) GPIOE_BASE   )

// RCC	 - Reset and Clock Control
#define AHBPERIPH_BASE        (PERIPH_BASE + 0x20000)
#define RCC_BASE              (AHBPERIPH_BASE  + 0x1000)
#define RCC                   ((RCC_TypeDef    *) RCC_BASE     )
typedef struct
{
  __IO uint32_t CR;
  __IO uint32_t CFGR;
  __IO uint32_t CIR;
  __IO uint32_t APB2RSTR;
  __IO uint32_t APB1RSTR;
  __IO uint32_t AHBENR;
  __IO uint32_t APB2ENR;
  __IO uint32_t APB1ENR;
  __IO uint32_t BDCR;
  __IO uint32_t CSR;
  __IO uint32_t AHBSTR;
  __IO uint32_t CFGR2;
} RCC_TypeDef;

C-File: main.c

//Main
/****************************************************************
* Author: heth@mercantec.dk
* Revision 1 - 29-aug-2011 
*****************************************************************
* Minimum setup of GPIO 
****************************************************************/
#include "minimum gpio.h"

#define forward	1
#define backward 0


//Define bitmask for LED's 
const long led_on[] = { 0x100, 0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000 };
const long led_off[] = { 0x1000000, 0x2000000,0x4000000,0x8000000,0x10000000,
						 0x20000000,0x40000000,0x80000000 };



int main (void) {
	int j;
	
	int led = 0; //Start with first LED ( 0 throug 7 as defined in led_mask)
	int direction=forward;

	// Start Clock on GPIO Port E
	RCC->APB2ENR |=  1 <<  6;	// Bit 6 = Enable IO Port E Clock

	GPIOE->CRH=0x33333333;
	//GPIOE->ODR=0xf0f0;
	while (1) {
		for ( j=0; j <= 100000 ; j++); //Loop for pause

		GPIOE->BSRR = led_off[led]; //Turn off Current Led
		
		//Forward Direction
		if ( direction == forward ) {
			if ( led == 7 ) { // Finish in forward direction?
				direction = backward;
			} else {
				led++; // Next led forward
				GPIOE->BSRR = led_on[led];
			}
		}

		//Backward Direction
		if ( direction == backward ) {
			if ( led == 0 ) { // Finish in forward direction
				direction = forward;
			} else {
				led--; // Next led backward
				GPIOE->BSRR = led_on[led];
			}
		}

	}
}