Search This Blog

Monday, July 30, 2018

STM8S header file and peripheral library

Projects / Misc 8-bit uC projects  Original post date:02/04/2017

Normally I wouldn't bother downloading the standard peripheral driver from a vendor. I see that as much work as trying to untangle a breakout board done in fritzing wiring diagram back into a proper schematic. i.e. it take more effort than reading the darn user manual than to trace through the mess.

In this case, I'll make an exception as the header file that comes with Cosmic C is missing the register bit definitions.

STM8S/A Standard peripheral library can be downloaded: here

The file I am interested is:
en.stsw-stm8069.zip/STM8S_StdPeriph_Lib/Libraries/STM8S_StdPeriph_Driver/inc/stm8s.h

Use the following line for including the header file. Normally __CSMC__ is automatically defined by the compiler, but the IDE does some basic check on its own and the header file complains about compiler not supported. On the other hand, the compiler doesn't like it being redefined, so that is enclosed by the "#ifdef" and "#endif".

"#define STM8S003" tells the header file which microcontroller you are using.
#ifndef __CSMC__
#define __CSMC__
#endif
#define STM8S003
#include "stm8s.h"

I could have commented out the following too, but I would rather leave the file unchanged. I created an empty "stm8s_conf.h" to keep the compiler happy. Normally this is where you put all the configuration stuff for the peripheral library.


#if !defined  USE_STDPERIPH_DRIVER
/* Comment the line below if you will not use the peripherals drivers.
   In this case, these drivers will not be included and the application code will be
   based on direct access to peripherals registers */
 #define USE_STDPERIPH_DRIVER
#endif

The header file also declares a bunch of standard integer types which comes in handy as is not provided by the compiler.  It also throws in some extensions for dealing with the different memory types/regions.


 #define FAR  @far
 #define NEAR @near
 #define TINY @tiny
 #define EEPROM @eeprom
 #define CONST  const

The registers are declared as structures. You can access them in the form Peripheral -> Register The register bit definition is in the form: Peripheral_Register_Bit and the actual bit values.
The following example shows the usage.


It adds the following intrinsics:


 #define enableInterrupts()    {_asm("rim\n");}  /* enable interrupts */
 #define disableInterrupts()   {_asm("sim\n");}  /* disable interrupts */
 #define rim()                 {_asm("rim\n");}  /* enable interrupts */
 #define sim()                 {_asm("sim\n");}  /* disable interrupts */
 #define nop()                 {_asm("nop\n");}  /* No Operation */
 #define trap()                {_asm("trap\n");} /* Trap (soft IT) */
 #define wfi()                 {_asm("wfi\n");}  /* Wait For Interrupt */
 #define halt()                {_asm("halt\n");} /* Halt */

For interrupt declaration:


 #define INTERRUPT @far @interrupt
 #define INTERRUPT_HANDLER(a,b) @far @interrupt void a(void)
 #define INTERRUPT_HANDLER_TRAP(a) void @far @interrupt a(void)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.