Difference between revisions of "MCBSTM32C/Blinky simple"

From Teknologisk videncenter
Jump to: navigation, search
m (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 ...")
 
m
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Assembler file: startup.c=
+
A simpe program thats blinks the eight LED's on Keil's [[MCBSTM32C]] board using the [[STM32F107VC/GPIO|GPIO]] Port-E[15:8]. See [http://www.keil.com/mcbstm32c/mcbstm32c-base-board-schematics.pdf Schematics]
 +
=Creating a project=
 +
*[[/Create project]]
 +
 
 +
=Assembler file: startup.s=
 
<source lang=asm>
 
<source lang=asm>
 
; Minimum startup from startup_stm32f10x_cl.s
 
; Minimum startup from startup_stm32f10x_cl.s
Line 60: Line 64:
 
END
 
END
 
</source>
 
</source>
 +
 
=Headerfile: minimum gpio.h=
 
=Headerfile: minimum gpio.h=
 
<source lang=c>
 
<source lang=c>
Line 124: Line 129:
 
=C-File: main.c=
 
=C-File: main.c=
 
<source lang=c>
 
<source lang=c>
 
 
//Main
 
//Main
 
/****************************************************************
 
/****************************************************************
Line 134: Line 138:
 
#include "minimum gpio.h"
 
#include "minimum gpio.h"
  
#define forward 1
+
#define FORWARD 1
#define backward 0
+
#define BACKWARD 0
  
 +
//Define bitmask for LED's
 +
/*
 +
  BSRR = Bit Set Reset Register
  
//Define bitmask for LED's  
+
  Each port has a BSRR port which can set or reset the pin, when the port
const long led_on[] = { 0x100, 0x200,0x400,0x800,0x1000,0x2000,0x4000,0x8000 };
+
  is configured for Output.
const long led_off[] = { 0x1000000, 0x2000000,0x4000000,0x8000000,0x10000000,
+
 
0x20000000,0x40000000,0x80000000 };
+
  Setting bit  0 to 15 will set - put a high - on the Ports corresponding pin
 +
  Setting bit 16 to 31 will reset - put a low - on the Ports corresponding pin - 16
 +
 
 +
  Writing 0x100 to GPIO port E's BSRR will set Port E[0] to high (1)
 +
  Writing 0x10000000 to GPIO port E's BSRR wilt reset Port E[0] to low (0)
 +
*/ 
 +
 
 +
//Setting BSRR bit 8 to 15 bitmasks (lowest 16 bit set bits on)
 +
// LED's are attached to bit 8 to 15 on the evaluation board.
 +
const long led_on[] = { 0x00000100, // PORT-E bit  8 ON
 +
                        0x00000200, // PORT-E bit  9 ON
 +
0x00000400, // PORT-E bit 10 ON
 +
0x00000800, // PORT-E bit 11 ON
 +
0x00001000, // PORT-E bit 12 ON
 +
0x00002000, // PORT-E bit 13 ON
 +
0x00004000, // PORT-E bit 14 ON
 +
0x00008000 // PORT-E bit 15 ON
 +
};
  
 +
//Resetting BSRR bit 8 to 15 bitmasks (highest 16 bits reset pins)
 +
const long led_off[] = { 0x01000000, // PORT-E bit  8 OFF
 +
0x02000000, // PORT-E bit  9 OFF
 +
0x04000000, // PORT-E bit 10 OFF
 +
0x08000000, // PORT-E bit 11 OFF
 +
0x10000000, // PORT-E bit 12 OFF
 +
0x20000000, // PORT-E bit 13 OFF
 +
0x40000000, // PORT-E bit 14 OFF
 +
0x80000000 // PORT-E bit 15 OFF
 +
};
  
  
Line 148: Line 182:
 
int j;
 
int j;
 
 
int led = 0; //Start with first LED ( 0 throug 7 as defined in led_mask)
+
//led = Light Emitting Diode
int direction=forward;
+
int led = 0; //Start with first LED ( 0 throug 7 as defined in led_on and led_off)
 +
int direction=FORWARD;
  
// Start Clock on GPIO Port E
+
// Start Clock on GPIO Port E (Enable port E)
 
RCC->APB2ENR |=  1 <<  6; // Bit 6 = Enable IO Port E Clock
 
RCC->APB2ENR |=  1 <<  6; // Bit 6 = Enable IO Port E Clock
  
 +
//Setting bit 8 to 15 of PORT E to Generel Purpose output push-pull max 50Mhz
 
GPIOE->CRH=0x33333333;
 
GPIOE->CRH=0x33333333;
//GPIOE->ODR=0xf0f0;
+
while (1) {
+
while (1) { // Do forever
 
for ( j=0; j <= 100000 ; j++); //Loop for pause
 
for ( j=0; j <= 100000 ; j++); //Loop for pause
  
Line 162: Line 198:
 
 
 
//Forward Direction
 
//Forward Direction
if ( direction == forward ) {
+
if ( direction == FORWARD ) {
 
if ( led == 7 ) { // Finish in forward direction?
 
if ( led == 7 ) { // Finish in forward direction?
direction = backward;
+
direction = BACKWARD;
 
} else {
 
} else {
 
led++; // Next led forward
 
led++; // Next led forward
Line 172: Line 208:
  
 
//Backward Direction
 
//Backward Direction
if ( direction == backward ) {
+
if ( direction == BACKWARD ) {
 
if ( led == 0 ) { // Finish in forward direction
 
if ( led == 0 ) { // Finish in forward direction
direction = forward;
+
direction = FORWARD;
 
} else {
 
} else {
 
led--; // Next led backward
 
led--; // Next led backward
Line 180: Line 216:
 
}
 
}
 
}
 
}
 
 
}
 
}
 
}
 
}
 
</source>
 
</source>
 
[[Category:ARM]]
 
[[Category:ARM]]

Latest revision as of 10:29, 19 September 2011

A simpe program thats blinks the eight LED's on Keil's MCBSTM32C board using the GPIO Port-E[15:8]. See Schematics

Creating a project

Assembler file: startup.s

; 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 
/* 
   BSRR = Bit Set Reset Register

   Each port has a BSRR port which can set or reset the pin, when the port
   is configured for Output.

   Setting bit  0 to 15 will set - put a high - on the Ports corresponding pin
   Setting bit 16 to 31 will reset - put a low - on the Ports corresponding pin - 16
   
   Writing 0x100 to GPIO port E's BSRR will set Port E[0] to high (1)
   Writing 0x10000000 to GPIO port E's BSRR wilt reset Port E[0] to low (0)
*/  

//Setting BSRR bit 8 to 15 bitmasks	(lowest 16 bit set bits on)
// LED's are attached to bit 8 to 15 on the evaluation board.
const long led_on[]  = { 0x00000100, // PORT-E bit  8 ON
                         0x00000200, // PORT-E bit  9 ON
					 	 0x00000400, // PORT-E bit 10 ON
					 	 0x00000800, // PORT-E bit 11 ON
						 0x00001000, // PORT-E bit 12 ON
						 0x00002000, // PORT-E bit 13 ON
						 0x00004000, // PORT-E bit 14 ON
						 0x00008000	 // PORT-E bit 15 ON
};

//Resetting BSRR bit 8 to 15 bitmasks (highest 16 bits reset pins)
const long led_off[] = { 0x01000000, // PORT-E bit  8 OFF
						 0x02000000, // PORT-E bit  9 OFF
						 0x04000000, // PORT-E bit 10 OFF
						 0x08000000, // PORT-E bit 11 OFF
						 0x10000000, // PORT-E bit 12 OFF
						 0x20000000, // PORT-E bit 13 OFF
						 0x40000000, // PORT-E bit 14 OFF
						 0x80000000	 // PORT-E bit 15 OFF
};


int main (void) {
	int j;
	
	//led = Light Emitting Diode
	int led = 0; //Start with first LED ( 0 throug 7 as defined in led_on and led_off)
	int direction=FORWARD;

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

	//Setting bit 8 to 15 of PORT E to Generel Purpose output push-pull max 50Mhz
	GPIOE->CRH=0x33333333;
	
	while (1) { 		// Do forever
		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];
			}
		}
	}
}