From f961a639f48f5ba947a8e78a79cb08b80c7baaf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Fri, 10 Feb 2017 01:15:10 +0100 Subject: [PATCH] improvements and better example main.c --- CMakeLists.txt.clion | 27 ++++++++++++ Library/SPL/stm8s_itc.h | 4 +- Library/SPL/stm8s_uart1.h | 20 ++++----- Makefile | 4 +- README.md | 3 ++ User/main.c | 66 ++++++++++++++++++++++++++-- User/{stm8s_it.c => stm8s_it.c.nope} | 0 User/{stm8s_it.h => stm8s_it.h.nope} | 0 8 files changed, 106 insertions(+), 18 deletions(-) create mode 100644 CMakeLists.txt.clion rename User/{stm8s_it.c => stm8s_it.c.nope} (100%) rename User/{stm8s_it.h => stm8s_it.h.nope} (100%) diff --git a/CMakeLists.txt.clion b/CMakeLists.txt.clion new file mode 100644 index 0000000..7a77898 --- /dev/null +++ b/CMakeLists.txt.clion @@ -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}) diff --git a/Library/SPL/stm8s_itc.h b/Library/SPL/stm8s_itc.h index 4d44f39..85c9151 100644 --- a/Library/SPL/stm8s_itc.h +++ b/Library/SPL/stm8s_itc.h @@ -169,8 +169,8 @@ typedef enum { /** @addtogroup ITC_Exported_Functions * @{ */ -#if 0 +#if 0 uint8_t ITC_GetCPUCC(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); ITC_PriorityLevel_TypeDef ITC_GetSoftwarePriority(ITC_Irq_TypeDef IrqNum); - #endif + /** @addtogroup STM8S_StdPeriph_Driver * @{ */ diff --git a/Library/SPL/stm8s_uart1.h b/Library/SPL/stm8s_uart1.h index d960b32..b9f7af2 100644 --- a/Library/SPL/stm8s_uart1.h +++ b/Library/SPL/stm8s_uart1.h @@ -181,12 +181,13 @@ typedef enum { * Baud rates at 16 MHz */ typedef enum { - UART_BAUD_9600 = (uint16_t) 0x0693, - UART_BAUD_19200 = (uint16_t) 0x0341, - UART_BAUD_57600 = (uint16_t) 0x0116, - UART_BAUD_115200 = (uint16_t) 0x0008B, - UART_BAUD_230400 = (uint16_t) 0x00045, - UART_BAUD_460800 = (uint16_t) 0x00023 + UART_BAUD_9600 = (uint16_t) 0x0368, + UART_BAUD_19200 = (uint16_t) 0x0134, + UART_BAUD_57600 = (uint16_t) 0x0611, + UART_BAUD_115200 = (uint16_t) 0x0B08, + UART_BAUD_230400 = (uint16_t) 0x0504, + UART_BAUD_460800 = (uint16_t) 0x0302, + UART_BAUD_921600 = (uint16_t) 0x0101 } UART_Baud_TypeDef; /** @@ -196,14 +197,11 @@ typedef enum { * @param baud - UART_BAUD_* */ void inline UART_SimpleInit(UART_Baud_TypeDef baud) { - UART1->BRR1 = (uint8_t) ((baud) & 0xFF); UART1->BRR2 = (uint8_t) (((baud) >> 8) & 0xFF); - UART1->CR2 |= (uint8_t) (UART1_CR2_TEN | UART1_CR2_REN); - UART1->CR3 |= (uint8_t) UART1_CR3_CKEN; + UART1->BRR1 = (uint8_t) ((baud) & 0xFF); + UART1->CR2 = (uint8_t) (UART1_CR2_TEN | UART1_CR2_REN); } - - /** * @} */ diff --git a/Makefile b/Makefile index 5c86b9d..6253902 100644 --- a/Makefile +++ b/Makefile @@ -38,6 +38,8 @@ PRJ_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(PRJ_SOURCE:.c=.rel)) #SPL_SRC_DIR = /usr/share/sdcc/lib/src/stm8/ #SPL_INC_DIR = /usr/share/sdcc/include/stm8/ +LIB_INC_DIR = /usr/share/sdcc/include/ + #SPL_SRC_DIR = Libraries/SPL/src/ SPL_INC_DIR = Library/SPL/ # add all library sources used here @@ -46,7 +48,7 @@ SPL_SOURCE = SPL_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(SPL_SOURCE:.c=.rel)) # 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 VPATH=$(PRJ_SRC_DIR):$(SPL_SRC_DIR) diff --git a/README.md b/README.md index 7dbba62..e30f1b0 100644 --- a/README.md +++ b/README.md @@ -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). 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** diff --git a/User/main.c b/User/main.c index 6d62d5c..dc7f1a6 100644 --- a/User/main.c +++ b/User/main.c @@ -1,12 +1,70 @@ -#include +#include "stm8s.h" +#include -#include -#include "stm8s_it.h" +void Delay(uint16_t nCount) { + 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) { + // Disable div8 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); - 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 } diff --git a/User/stm8s_it.c b/User/stm8s_it.c.nope similarity index 100% rename from User/stm8s_it.c rename to User/stm8s_it.c.nope diff --git a/User/stm8s_it.h b/User/stm8s_it.h.nope similarity index 100% rename from User/stm8s_it.h rename to User/stm8s_it.h.nope