MAXREFDES62# Code Documentation  V01.00
RS-485 Micro PLC
 All Files Functions Variables Macros Pages
init_config.c
Go to the documentation of this file.
1 
29 /*
30 * Copyright (C) 2012 Maxim Integrated Products, Inc., All Rights Reserved.
31 *
32 * Permission is hereby granted, free of charge, to any person obtaining a
33 * copy of this software and associated documentation files (the "Software"),
34 * to deal in the Software without restriction, including without limitation
35 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36 * and/or sell copies of the Software, and to permit persons to whom the
37 * Software is furnished to do so, subject to the following conditions:
38 *
39 * The above copyright notice and this permission notice shall be included
40 * in all copies or substantial portions of the Software.
41 *
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
43 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
46 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
47 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
48 * OTHER DEALINGS IN THE SOFTWARE.
49 *
50 * Except as contained in this notice, the name of Maxim Integrated Products
51 * shall not be used except as stated in the Maxim Integrated Products
52 * Branding Policy.
53 *
54 * The mere transfer of this software does not imply any licenses
55 * of trade secrets, proprietary technology, copyrights, patents,
56 * trademarks, maskwork rights, or any other form of intellectual
57 * property whatsoever. Maxim Integrated Products retains all ownership rights.
58 *
59 ***************************************************************************/
60 
61 #include "stm32f10x.h"
62 #include "stm32f10x_gpio.h"
63 #include "stm32f10x_usart.h"
64 #include "stm32f10x_spi.h"
65 #include "init_config.h"
66 
67 GPIO_InitTypeDef gpio_init_structure; //GPIO structure
68 SPI_InitTypeDef spi_init_structure; //SPI structure
69 USART_InitTypeDef usart_init_structure; //USART structure
70 
71 void maxim_uart_init(void)
85 {
86  usart_init_structure.USART_BaudRate = 921600;
87  usart_init_structure.USART_WordLength = USART_WordLength_8b;
88  usart_init_structure.USART_StopBits = USART_StopBits_1;
89  usart_init_structure.USART_Parity = USART_Parity_No;
90  usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
91  usart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
92 
93  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
94  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
95 
96  /* Configure USART Tx as alternate function push-pull */
97  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
98  gpio_init_structure.GPIO_Pin = GPIO_Pin_9;
99  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
100  GPIO_Init(GPIOA, &gpio_init_structure);
101 
102  /* Configure USART Rx as input floating */
103  gpio_init_structure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
104  gpio_init_structure.GPIO_Pin = GPIO_Pin_10;
105  GPIO_Init(GPIOA, &gpio_init_structure);
106 
107  /* USART configuration */
108  USART_Init(USART1, &usart_init_structure);
109 
110  /* Enable USART */
111  USART_Cmd(USART1, ENABLE);
112 
113 
114  usart_init_structure.USART_BaudRate = 4000000;
115  usart_init_structure.USART_WordLength = USART_WordLength_8b;
116  usart_init_structure.USART_StopBits = USART_StopBits_1;
117  usart_init_structure.USART_Parity = USART_Parity_No;
118  usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
119  usart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
120 
121  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
122  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
123 
124  /* Configure USART Tx as alternate function push-pull */
125  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
126  gpio_init_structure.GPIO_Pin = GPIO_Pin_2;
127  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
128  GPIO_Init(GPIOA, &gpio_init_structure);
129 
130  /* Configure USART Rx as input floating */
131  gpio_init_structure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
132  gpio_init_structure.GPIO_Pin = GPIO_Pin_3;
133  GPIO_Init(GPIOA, &gpio_init_structure);
134 
135  /* USART configuration */
136  USART_Init(USART2, &usart_init_structure);
137 
138  /* Enable USART */
139  USART_Cmd(USART2, ENABLE);
140 
141 
142  usart_init_structure.USART_BaudRate = 4000000;
143  usart_init_structure.USART_WordLength = USART_WordLength_8b;
144  usart_init_structure.USART_StopBits = USART_StopBits_1;
145  usart_init_structure.USART_Parity = USART_Parity_No;
146  usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
147  usart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
148 
149  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
150  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
151 
152  /* Configure USART Tx as alternate function push-pull */
153  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
154  gpio_init_structure.GPIO_Pin = GPIO_Pin_10;
155  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
156  GPIO_Init(GPIOB, &gpio_init_structure);
157 
158  /* Configure USART Rx as input floating */
159  gpio_init_structure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
160  gpio_init_structure.GPIO_Pin = GPIO_Pin_11;
161  GPIO_Init(GPIOB, &gpio_init_structure);
162 
163  /* USART configuration */
164  USART_Init(USART3, &usart_init_structure);
165 
166  /* Enable USART */
167  USART_Cmd(USART3, ENABLE);
168 }
169 
170 
171 void maxim_gpio_init(void)
181 {
182  /* GPIOB Periph clock enable */
183  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
184  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
185  gpio_init_structure.GPIO_Pin = GPIO_Pin_1;
186  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
187  gpio_init_structure.GPIO_Mode = GPIO_Mode_Out_PP;
188  GPIO_Init(GPIOA, &gpio_init_structure);
189 
190  /* Output clock on MCO pin ---------------------------------------------*/
191  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
192  gpio_init_structure.GPIO_Pin = GPIO_Pin_8;
193  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
194  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
195  GPIO_Init(GPIOA, &gpio_init_structure);
196  RCC_MCOConfig(RCC_MCO_SYSCLK); // Put on MCO pin the: System clock selected
197 }