improvements and better example main.c

master
Ondřej Hruška 7 years ago
parent ffe99c1ed5
commit f961a639f4
  1. 27
      CMakeLists.txt.clion
  2. 4
      Library/SPL/stm8s_itc.h
  3. 20
      Library/SPL/stm8s_uart1.h
  4. 4
      Makefile
  5. 3
      README.md
  6. 66
      User/main.c
  7. 0
      User/stm8s_it.c.nope
  8. 0
      User/stm8s_it.h.nope

@ -0,0 +1,27 @@
cmake_minimum_required(VERSION 3.7)
project(stm8 C)
project(STM8S)
set(CMAKE_CXX_STANDARD GNU99)
add_definitions(
-DSKIP_TRAPS=1
-DSTM8S103
-D__SDCC__
-D_SDCC_
-DINTERRUPT=
-D__interrupt\(n\)=__attribute__\(\(interrupt\(n\)\)\)
-D__SDCC)
# User program
include_directories(User)
include_directories(Library/SPL)
#include_directories(Libraries/SPL/inc)
file(GLOB_RECURSE USER_SOURCES "User/*.c" "User/*.h")
file(GLOB_RECURSE LIB_SOURCES "Library/SPL/*.h")
# Systemmake
include_directories(/usr/share/sdcc/include/)
link_directories(/usr/share/sdcc/include/)
add_executable(firmware ${USER_SOURCES} ${LIB_SOURCES})

@ -169,8 +169,8 @@ typedef enum {
/** @addtogroup ITC_Exported_Functions /** @addtogroup ITC_Exported_Functions
* @{ * @{
*/ */
#if 0
#if 0
uint8_t ITC_GetCPUCC(void); uint8_t ITC_GetCPUCC(void);
void ITC_DeInit(void); void ITC_DeInit(void);
@ -180,10 +180,10 @@ uint8_t ITC_GetSoftIntStatus(void);
void ITC_SetSoftwarePriority(ITC_Irq_TypeDef IrqNum, ITC_PriorityLevel_TypeDef PriorityValue); void ITC_SetSoftwarePriority(ITC_Irq_TypeDef IrqNum, ITC_PriorityLevel_TypeDef PriorityValue);
ITC_PriorityLevel_TypeDef ITC_GetSoftwarePriority(ITC_Irq_TypeDef IrqNum); ITC_PriorityLevel_TypeDef ITC_GetSoftwarePriority(ITC_Irq_TypeDef IrqNum);
#endif #endif
/** @addtogroup STM8S_StdPeriph_Driver /** @addtogroup STM8S_StdPeriph_Driver
* @{ * @{
*/ */

@ -181,12 +181,13 @@ typedef enum {
* Baud rates at 16 MHz * Baud rates at 16 MHz
*/ */
typedef enum { typedef enum {
UART_BAUD_9600 = (uint16_t) 0x0693, UART_BAUD_9600 = (uint16_t) 0x0368,
UART_BAUD_19200 = (uint16_t) 0x0341, UART_BAUD_19200 = (uint16_t) 0x0134,
UART_BAUD_57600 = (uint16_t) 0x0116, UART_BAUD_57600 = (uint16_t) 0x0611,
UART_BAUD_115200 = (uint16_t) 0x0008B, UART_BAUD_115200 = (uint16_t) 0x0B08,
UART_BAUD_230400 = (uint16_t) 0x00045, UART_BAUD_230400 = (uint16_t) 0x0504,
UART_BAUD_460800 = (uint16_t) 0x00023 UART_BAUD_460800 = (uint16_t) 0x0302,
UART_BAUD_921600 = (uint16_t) 0x0101
} UART_Baud_TypeDef; } UART_Baud_TypeDef;
/** /**
@ -196,14 +197,11 @@ typedef enum {
* @param baud - UART_BAUD_* * @param baud - UART_BAUD_*
*/ */
void inline UART_SimpleInit(UART_Baud_TypeDef baud) { void inline UART_SimpleInit(UART_Baud_TypeDef baud) {
UART1->BRR1 = (uint8_t) ((baud) & 0xFF);
UART1->BRR2 = (uint8_t) (((baud) >> 8) & 0xFF); UART1->BRR2 = (uint8_t) (((baud) >> 8) & 0xFF);
UART1->CR2 |= (uint8_t) (UART1_CR2_TEN | UART1_CR2_REN); UART1->BRR1 = (uint8_t) ((baud) & 0xFF);
UART1->CR3 |= (uint8_t) UART1_CR3_CKEN; UART1->CR2 = (uint8_t) (UART1_CR2_TEN | UART1_CR2_REN);
} }
/** /**
* @} * @}
*/ */

@ -38,6 +38,8 @@ PRJ_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(PRJ_SOURCE:.c=.rel))
#SPL_SRC_DIR = /usr/share/sdcc/lib/src/stm8/ #SPL_SRC_DIR = /usr/share/sdcc/lib/src/stm8/
#SPL_INC_DIR = /usr/share/sdcc/include/stm8/ #SPL_INC_DIR = /usr/share/sdcc/include/stm8/
LIB_INC_DIR = /usr/share/sdcc/include/
#SPL_SRC_DIR = Libraries/SPL/src/ #SPL_SRC_DIR = Libraries/SPL/src/
SPL_INC_DIR = Library/SPL/ SPL_INC_DIR = Library/SPL/
# add all library sources used here # add all library sources used here
@ -46,7 +48,7 @@ SPL_SOURCE =
SPL_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(SPL_SOURCE:.c=.rel)) SPL_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(SPL_SOURCE:.c=.rel))
# collect all include folders # collect all include folders
INCLUDE = -I$(PRJ_SRC_DIR) -I$(SPL_INC_DIR) INCLUDE = -I$(PRJ_SRC_DIR) -I$(SPL_INC_DIR) -I$(LIB_INC_DIR)
# collect all source directories # collect all source directories
VPATH=$(PRJ_SRC_DIR):$(SPL_SRC_DIR) VPATH=$(PRJ_SRC_DIR):$(SPL_SRC_DIR)

@ -12,3 +12,6 @@ there's lots of crap I don't understand, so you'll do better just keeping it
intact or re-implementing it). intact or re-implementing it).
This is intended to be used with SDCC on linux. This is intended to be used with SDCC on linux.
**The CMakeLists file is for CLion to stop bitching about missing includes and fake syntax errors.
It's not used for building. Remove the .clion suffix if you want to use it**

@ -1,12 +1,70 @@
#include <stdio.h> #include "stm8s.h"
#include <stdlib.h>
#include <stm8s.h> void Delay(uint16_t nCount) {
#include "stm8s_it.h" uint8_t i;
for (; nCount != 0; nCount--) {
for (i = 255; i != 0; i--) {}
}
}
void putchar(char c) {
while ((UART1->SR & UART1_SR_TXE) == 0);
UART1->DR = (u8)c;
}
void puts(const char *ch) {
char c;
while ((c = *ch++) != 0)
putchar(c);
}
void puts_itoa(int32_t n, unsigned char radix) {
char s[10], i, c;
_ltoa(n, s, radix);
i = 0;
while((c = s[i++]) != 0) {
putchar(c);
}
}
void main(void) void main(void)
{ {
// Disable div8
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
// LED for blinking
GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
// minimal uart init
UART_SimpleInit(UART_BAUD_115200); UART_SimpleInit(UART_BAUD_115200);
while(1); // irq conf
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
enableInterrupts();
// Clear screen & print system frequency
puts("\033c\033[?25lClock freq = "); // cls
puts_itoa(CLK_GetClockFreq(), 10);
puts(" Hz"); // cls
// echo & blinking
while (1) {
Delay(2000);
GPIOB->ODR ^= GPIO_PIN_5;
}
}
/**
* @brief UART1 RX Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
{
if (UART1->SR & UART1_SR_RXNE)
UART1->DR = (u8) (UART1->DR); // echo
if (UART1->SR & UART1_SR_OR)
UART1->SR &= ~UART1_SR_OR; // clear OR flag
} }

Loading…
Cancel
Save