Browse Source

added bootstrap.c/.h

Ondřej Hruška 3 years ago
parent
commit
f064bc6fb6
4 changed files with 154 additions and 61 deletions
  1. 3 0
      Library/SPL/stm8s.h
  2. 85 0
      User/bootstrap.c
  3. 58 0
      User/bootstrap.h
  4. 8 61
      User/main.c

+ 3 - 0
Library/SPL/stm8s.h View File

@@ -240,6 +240,9 @@ typedef uint8_t u8;
240 240
 typedef enum {
241 241
 	FALSE = 0, TRUE = !FALSE
242 242
 } bool;
243
+//#include <stdbool.h>
244
+//#define TRUE true
245
+//#define FALSE false
243 246
 
244 247
 typedef enum {
245 248
 	RESET = 0, SET = !RESET

+ 85 - 0
User/bootstrap.c View File

@@ -0,0 +1,85 @@
1
+//
2
+// Created by MightyPork on 2017/02/10.
3
+//
4
+
5
+#include <stm8s.h>
6
+#include "bootstrap.h"
7
+
8
+/** Global time base */
9
+volatile uint32_t time_ms;
10
+
11
+/**
12
+ * Putchar for printf
13
+ * @param c - char to print
14
+ */
15
+void putchar(char c)
16
+{
17
+	while ((UART1->SR & UART1_SR_TXE) == 0);
18
+	UART1->DR = (u8)c;
19
+}
20
+
21
+/**
22
+ * Init for the chinese STM8 board
23
+ * - enable LED
24
+ * - enable UART @ 115200
25
+ * - set up UART rx irq
26
+ */
27
+void SimpleInit(void)
28
+{
29
+	// Disable default div/8 HSI prescaller
30
+	CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
31
+
32
+	// LED
33
+	GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_HIGH_FAST);
34
+
35
+	// UART init & enable IRQ
36
+	UART_SimpleInit(UART_BAUD_115200);
37
+	UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
38
+
39
+	// Timebase generation counter
40
+	TIM4_UpdateRequestConfig(TIM4_UPDATESOURCE_REGULAR);
41
+	TIM4_PrescalerConfig(TIM4_PRESCALER_128, TIM4_PSCRELOADMODE_IMMEDIATE);
42
+	TIM4_SetAutoreload(0xFF);
43
+	TIM4_ARRPreloadConfig(ENABLE);
44
+	TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
45
+	TIM4_Cmd(ENABLE);
46
+
47
+	enableInterrupts();
48
+}
49
+
50
+/**
51
+ * @brief Timer4 Update/Overflow Interrupt routine.
52
+ * @param  None
53
+ * @retval None
54
+ */
55
+INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
56
+{
57
+	time_ms++;
58
+	TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
59
+}
60
+
61
+/** Delay ms */
62
+void Delay(uint16_t ms)
63
+{
64
+	uint32_t start = time_ms;
65
+	while ((time_ms - start) < ms);
66
+}
67
+
68
+/**
69
+ * UART1 RX Interrupt routine.
70
+ */
71
+INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
72
+{
73
+	if (UART1->SR & UART1_SR_RXNE) {
74
+		UART_HandleRx(UART1->DR);
75
+	}
76
+}
77
+
78
+// Comment out if custom rx handler is added
79
+#if 1
80
+void UART_HandleRx(char c)
81
+{
82
+	// echo
83
+	putchar(c);
84
+}
85
+#endif

+ 58 - 0
User/bootstrap.h View File

@@ -0,0 +1,58 @@
1
+//
2
+// Created by MightyPork on 2017/02/10.
3
+//
4
+
5
+#ifndef STM8S_STDINIT_H
6
+#define STM8S_STDINIT_H
7
+
8
+/** Global timebase */
9
+extern volatile uint32_t time_ms;
10
+
11
+/** Uart IRQ handler */
12
+void UART1_RX_IRQHandler(void) INTERRUPT(18);
13
+
14
+/** SysTick handler */
15
+void TIM4_UPD_OVF_IRQHandler(void) INTERRUPT(23);
16
+
17
+/** putchar, used by the SDCC stdlib */
18
+void putchar(char c);
19
+
20
+/**
21
+ * Simple init (UART, LED, timebase)
22
+ */
23
+void SimpleInit(void);
24
+
25
+/**
26
+ * Millisecond delay
27
+ *
28
+ * @param ms - nr of milliseconds
29
+ */
30
+void Delay(uint16_t ms);
31
+
32
+/**
33
+ * User UART rx handler
34
+ *
35
+ * If adding custom handler, comment out the defualt echo impl in bootstrap.c
36
+ *
37
+ * @param c
38
+ */
39
+extern void UART_HandleRx(char c);
40
+
41
+/** Toggle indicator LED */
42
+inline void LED_Toggle(void)
43
+{
44
+	GPIOB->ODR ^= GPIO_PIN_5;
45
+}
46
+
47
+/** Set indicator LED */
48
+inline void LED_Set(bool state)
49
+{
50
+	if (state) {
51
+		GPIOB->ODR &= ~GPIO_PIN_5;
52
+	} else {
53
+		GPIOB->ODR |= GPIO_PIN_5;
54
+	}
55
+}
56
+
57
+
58
+#endif //STM8S_DEBUG_H

+ 8 - 61
User/main.c View File

@@ -1,70 +1,17 @@
1 1
 #include "stm8s.h"
2
-#include <stdlib.h>
3
-
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
-}
2
+#include <stdio.h>
3
+#include "bootstrap.h"
30 4
 
31 5
 void main(void)
32 6
 {
33
-	// Disable div8
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
40
-	UART_SimpleInit(UART_BAUD_115200);
41
-
42
-	// irq conf
43
-	UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
44
-	enableInterrupts();
7
+	SimpleInit();
45 8
 
46
-	// Clear screen & print system frequency
47
-	puts("\033c\033[?25lClock freq = "); // cls
48
-	puts_itoa(CLK_GetClockFreq(), 10);
49
-	puts(" Hz"); // cls
9
+	// clear screen, hide cursor
10
+	printf("\033c\033[?25lClock freq = \033[32m%lu Hz\033[0m\r\n", CLK_GetClockFreq());
50 11
 
51
-	// echo & blinking
52 12
 	while (1) {
53
-		Delay(2000);
54
-		GPIOB->ODR ^= GPIO_PIN_5;
13
+		LED_Toggle();
14
+		printf("Time = %lu ms\r", time_ms);
15
+		Delay(500);
55 16
 	}
56 17
 }
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
70
-}