Thursday, July 20, 2017

Developing code from scratch for Bare metal microcontroller

Step1:

Go through the datasheet and technical reference manual of the micro-controller (MCU). Search on the manufacturer website for 

  1. Header files of registers of MCU
  2. HAL layer to reduce the effort of creating from scartch.
  3. If above options are not available then prepare header file on your own by referring to memory map
Example
mcu123.h
...
...
#define ADC1_base 0x08000100
#deinfe ADC2_base 0x08000200
...
...

Step2:

Create user file where all structures will be there to address every register in MCU
user_mcu123.h

/**
 * Bit fields
 */
typedef struct {
  uint32_t ADRDY_MST : 1;  // BIT 0
  uint32_t EOSMP_MST : 1;  // BIT 1
.
.
.
} ADC_CFGR_t


typedef struct {
  uint32_t RESERVED : 3;  //  Bits 0,1,2 reserved
  uint32_t SMP1 : 3;  // BIT 3,4,5
  uint32_t SMP2 : 3;  // BIT 6,7,8
.
.
.
} ADC_SMPR_t

/**
* Structures
*/
typedef struct {
  ADC_CFGR_t CFGR;  // Offset = 0x00000000
  ADC_SMPR_t SMPR;  // Offset = 0x00000004 these are arranged as per the memory map
} ADC_t

Step 3:

user_main.c
volatile ADC_t * const ADC1, * ADC2  //declare two pointers which will of required data type
ADC1 = (ADC_t *) ADC1_base;
ADC2 = (ADC_t *) ADC2_base;

NOTE: There is reason for declaring volatile and const in such fashion for ADC1 and ADC2.
ADC1/2 is a pointer which is constant, but the memory map location to which is points can change. Memory location to which it points is volatile because we are telling compiler that the content of that memory location can change out of sync to the program flow. So restrains compiler from making and optimization to this object  and removing from compiler output.

No comments:

Post a Comment

PROFILE

My photo
India
Design Engineer ( IFM Engineering Private Limited )

Followers