Browse Source

improvements and better example main.c

Ondřej Hruška 3 years ago
parent
commit
f961a639f4
8 changed files with 106 additions and 18 deletions
  1. 27 0
      CMakeLists.txt.clion
  2. 2 2
      Library/SPL/stm8s_itc.h
  3. 9 11
      Library/SPL/stm8s_uart1.h
  4. 3 1
      Makefile
  5. 3 0
      README.md
  6. 62 4
      User/main.c
  7. 0 0
      User/stm8s_it.c.nope
  8. 0 0
      User/stm8s_it.h.nope

+ 27 - 0
CMakeLists.txt.clion View File

@@ -0,0 +1,27 @@
1
+cmake_minimum_required(VERSION 3.7)
2
+project(stm8 C)
3
+project(STM8S)
4
+set(CMAKE_CXX_STANDARD GNU99)
5
+
6
+add_definitions(
7
+        -DSKIP_TRAPS=1
8
+        -DSTM8S103
9
+        -D__SDCC__
10
+        -D_SDCC_
11
+        -DINTERRUPT=
12
+        -D__interrupt\(n\)=__attribute__\(\(interrupt\(n\)\)\)
13
+        -D__SDCC)
14
+
15
+# User program
16
+include_directories(User)
17
+include_directories(Library/SPL)
18
+#include_directories(Libraries/SPL/inc)
19
+file(GLOB_RECURSE USER_SOURCES "User/*.c" "User/*.h")
20
+
21
+file(GLOB_RECURSE LIB_SOURCES "Library/SPL/*.h")
22
+
23
+# Systemmake
24
+include_directories(/usr/share/sdcc/include/)
25
+link_directories(/usr/share/sdcc/include/)
26
+
27
+add_executable(firmware ${USER_SOURCES} ${LIB_SOURCES})

+ 2 - 2
Library/SPL/stm8s_itc.h View File

@@ -169,8 +169,8 @@ typedef enum {
169 169
 /** @addtogroup ITC_Exported_Functions
170 170
   * @{
171 171
   */
172
-#if 0
173 172
 
173
+#if 0
174 174
 uint8_t ITC_GetCPUCC(void);
175 175
 
176 176
 void ITC_DeInit(void);
@@ -180,10 +180,10 @@ uint8_t ITC_GetSoftIntStatus(void);
180 180
 void ITC_SetSoftwarePriority(ITC_Irq_TypeDef IrqNum, ITC_PriorityLevel_TypeDef PriorityValue);
181 181
 
182 182
 ITC_PriorityLevel_TypeDef ITC_GetSoftwarePriority(ITC_Irq_TypeDef IrqNum);
183
-
184 183
 #endif
185 184
 
186 185
 
186
+
187 187
 /** @addtogroup STM8S_StdPeriph_Driver
188 188
   * @{
189 189
   */

+ 9 - 11
Library/SPL/stm8s_uart1.h View File

@@ -181,12 +181,13 @@ typedef enum {
181 181
  * Baud rates at 16 MHz
182 182
  */
183 183
 typedef enum {
184
-	UART_BAUD_9600 = (uint16_t) 0x0693,
185
-	UART_BAUD_19200 = (uint16_t) 0x0341,
186
-	UART_BAUD_57600 = (uint16_t) 0x0116,
187
-	UART_BAUD_115200 = (uint16_t) 0x0008B,
188
-	UART_BAUD_230400 = (uint16_t) 0x00045,
189
-	UART_BAUD_460800 = (uint16_t) 0x00023
184
+	UART_BAUD_9600 = (uint16_t) 0x0368,
185
+	UART_BAUD_19200 = (uint16_t) 0x0134,
186
+	UART_BAUD_57600 = (uint16_t) 0x0611,
187
+	UART_BAUD_115200 = (uint16_t) 0x0B08,
188
+	UART_BAUD_230400 = (uint16_t) 0x0504,
189
+	UART_BAUD_460800 = (uint16_t) 0x0302,
190
+	UART_BAUD_921600 = (uint16_t) 0x0101
190 191
 } UART_Baud_TypeDef;
191 192
 
192 193
 /**
@@ -196,14 +197,11 @@ typedef enum {
196 197
  * @param baud - UART_BAUD_*
197 198
  */
198 199
 void inline UART_SimpleInit(UART_Baud_TypeDef baud) {
199
-	UART1->BRR1 = (uint8_t) ((baud) & 0xFF);
200 200
 	UART1->BRR2 = (uint8_t) (((baud) >> 8) & 0xFF);
201
-	UART1->CR2 |= (uint8_t) (UART1_CR2_TEN | UART1_CR2_REN);
202
-	UART1->CR3 |= (uint8_t) UART1_CR3_CKEN;
201
+	UART1->BRR1 = (uint8_t) ((baud) & 0xFF);
202
+	UART1->CR2 = (uint8_t) (UART1_CR2_TEN | UART1_CR2_REN);
203 203
 }
204 204
 
205
-
206
-
207 205
 /**
208 206
   * @}
209 207
   */

+ 3 - 1
Makefile View File

@@ -38,6 +38,8 @@ PRJ_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(PRJ_SOURCE:.c=.rel))
38 38
 #SPL_SRC_DIR = /usr/share/sdcc/lib/src/stm8/
39 39
 #SPL_INC_DIR = /usr/share/sdcc/include/stm8/
40 40
 
41
+LIB_INC_DIR = /usr/share/sdcc/include/
42
+
41 43
 #SPL_SRC_DIR = Libraries/SPL/src/
42 44
 SPL_INC_DIR = Library/SPL/
43 45
 # add all library sources used here
@@ -46,7 +48,7 @@ SPL_SOURCE  =
46 48
 SPL_OBJECTS := $(addprefix $(OUTPUT_DIR)/, $(SPL_SOURCE:.c=.rel))
47 49
 
48 50
 # collect all include folders
49
-INCLUDE = -I$(PRJ_SRC_DIR) -I$(SPL_INC_DIR)
51
+INCLUDE = -I$(PRJ_SRC_DIR) -I$(SPL_INC_DIR) -I$(LIB_INC_DIR)
50 52
 
51 53
 # collect all source directories
52 54
 VPATH=$(PRJ_SRC_DIR):$(SPL_SRC_DIR)

+ 3 - 0
README.md View File

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

+ 62 - 4
User/main.c View File

@@ -1,12 +1,70 @@
1
-#include <stdio.h>
1
+#include "stm8s.h"
2
+#include <stdlib.h>
2 3
 
3
-#include <stm8s.h>
4
-#include "stm8s_it.h"
4
+void Delay(uint16_t nCount) {
5
+	uint8_t i;
6
+	for (; nCount != 0; nCount--) {
7
+		for (i = 255; i != 0; i--) {}
8
+	}
9
+}
10
+
11
+void putchar(char c) {
12
+	while ((UART1->SR & UART1_SR_TXE) == 0);
13
+	UART1->DR = (u8)c;
14
+}
15
+
16
+void puts(const char *ch) {
17
+	char c;
18
+	while ((c = *ch++) != 0)
19
+		putchar(c);
20
+}
21
+
22
+void puts_itoa(int32_t n, unsigned char radix) {
23
+	char s[10], i, c;
24
+	_ltoa(n, s, radix);
25
+	i = 0;
26
+	while((c = s[i++]) != 0) {
27
+		putchar(c);
28
+	}
29
+}
5 30
 
6 31
 void main(void)
7 32
 {
33
+	// Disable div8
8 34
 	CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
35
+
36
+	// LED for blinking
37
+	GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
38
+
39
+	// minimal uart init
9 40
 	UART_SimpleInit(UART_BAUD_115200);
10 41
 
11
-	while(1);
42
+	// irq conf
43
+	UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
44
+	enableInterrupts();
45
+
46
+	// Clear screen & print system frequency
47
+	puts("\033c\033[?25lClock freq = "); // cls
48
+	puts_itoa(CLK_GetClockFreq(), 10);
49
+	puts(" Hz"); // cls
50
+
51
+	// echo & blinking
52
+	while (1) {
53
+		Delay(2000);
54
+		GPIOB->ODR ^= GPIO_PIN_5;
55
+	}
56
+}
57
+
58
+/**
59
+  * @brief UART1 RX Interrupt routine.
60
+  * @param  None
61
+  * @retval None
62
+  */
63
+INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
64
+{
65
+	if (UART1->SR & UART1_SR_RXNE)
66
+		UART1->DR = (u8) (UART1->DR); // echo
67
+
68
+	if (UART1->SR & UART1_SR_OR)
69
+		UART1->SR &= ~UART1_SR_OR; // clear OR flag
12 70
 }

User/stm8s_it.c → User/stm8s_it.c.nope View File


User/stm8s_it.h → User/stm8s_it.h.nope View File