1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2021-08-14     Jonas        first version
9  */
10 
11 #include <hk32f0xx.h>
12 #include <rtthread.h>
13 #include "hk32_msp.h"
14 
15 #ifdef BSP_USING_UART
hk32_msp_usart_init(void * Instance)16 void hk32_msp_usart_init(void *Instance)
17 {
18     GPIO_InitTypeDef GPIO_InitStruct;
19     USART_TypeDef *USARTx = (USART_TypeDef *)Instance;
20 
21     GPIO_StructInit(&GPIO_InitStruct);
22     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
23 #ifdef BSP_USING_UART1
24     if (USART1 == USARTx)
25     {
26 #define USART1_REMAP
27 #ifndef USART1_REMAP
28         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
29         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
30 
31         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
32         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
33         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
34         GPIO_Init(GPIOA, &GPIO_InitStruct);
35 
36         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
37         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
38         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
39         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
40         GPIO_Init(GPIOA, &GPIO_InitStruct);
41 
42         GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
43         GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
44 
45 #else
46         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
47         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
48 
49         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
50         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
51         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
52         GPIO_Init(GPIOB, &GPIO_InitStruct);
53 
54         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
55         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
56         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
57         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
58         GPIO_Init(GPIOB, &GPIO_InitStruct);
59 
60         GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_0);
61         GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_0);
62 #endif
63     }
64 #endif
65 #ifdef BSP_USING_UART2
66     if (USART2 == USARTx)
67     {
68         RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
69         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
70 
71         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
72         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
73         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
74         GPIO_Init(GPIOA, &GPIO_InitStruct);
75 
76         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
77         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
78         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
79         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
80         GPIO_Init(GPIOA, &GPIO_InitStruct);
81 
82         GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
83         GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
84     }
85 #endif
86     /* Add others */
87 }
88 #endif /* BSP_USING_SERIAL */
89 #ifdef BSP_USING_I2C
hk32_msp_i2c_init(void * Instance)90 void hk32_msp_i2c_init(void *Instance)
91 {
92     GPIO_InitTypeDef GPIO_InitStruct;
93     I2C_TypeDef *I2Cx = (I2C_TypeDef *)Instance;
94 
95     GPIO_StructInit(&GPIO_InitStruct);
96     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
97 #ifdef BSP_USING_I2C1
98     if (I2C1 == I2Cx)
99     {
100 #ifndef I2C1_REMAP
101         RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
102         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
103 
104         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
105         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
106         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
107         GPIO_Init(GPIOB, &GPIO_InitStruct);
108 
109         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
110         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
111         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
112         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
113         GPIO_Init(GPIOB, &GPIO_InitStruct);
114 
115         GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_1);
116         GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_1);
117 #else
118         RCC_APB2PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
119         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
120 
121         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
122         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
123         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
124         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
125         GPIO_Init(GPIOB, &GPIO_InitStruct);
126 
127         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
128         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
129         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
130         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
131         GPIO_Init(GPIOB, &GPIO_InitStruct);
132 
133         GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_1);
134         GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_1);
135 #endif
136     }
137 #endif
138     /* Add others */
139 }
140 #endif /* BSP_USING_I2C */
141 
142 
143 #ifdef BSP_USING_SPI
hk32_msp_spi_init(void * Instance)144 void hk32_msp_spi_init(void *Instance)
145 {
146     GPIO_InitTypeDef GPIO_InitStruct;
147     SPI_TypeDef *SPIx = (SPI_TypeDef *)Instance;
148 
149     GPIO_StructInit(&GPIO_InitStruct);
150     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
151 #ifdef BSP_USING_SPI1
152     if (SPI1 == SPIx)
153     {
154 #ifndef SPI1_REMAP
155         RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
156         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
157 
158         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
159         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
160         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
161         GPIO_Init(GPIOA, &GPIO_InitStruct);
162 
163         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
164         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
165         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
166         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
167         GPIO_Init(GPIOA, &GPIO_InitStruct);
168 
169         GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
170         GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
171         GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
172 #else
173         RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
174         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
175         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
176 
177         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
178         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
179         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
180         GPIO_Init(GPIOA, &GPIO_InitStruct);
181 
182         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
183         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
184         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
185         GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
186         GPIO_Init(GPIOB, &GPIO_InitStruct);
187 
188         GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_0);
189         GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_0);
190         GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_0);
191 #endif
192     }
193 #endif
194     /* Add others */
195 }
196 #endif /* BSP_USING_I2C */
197