MAXREFDES63# Code Documentation  V01.00
8-Channel Digital Output MicroPLC
 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)
82 {
83  usart_init_structure.USART_BaudRate = 921600;
84  usart_init_structure.USART_WordLength = USART_WordLength_8b;
85  usart_init_structure.USART_StopBits = USART_StopBits_1;
86  usart_init_structure.USART_Parity = USART_Parity_No;
87  usart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
88  usart_init_structure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
89 
90  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
91  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
92 
93  /* Configure USART Tx as alternate function push-pull */
94  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
95  gpio_init_structure.GPIO_Pin = GPIO_Pin_9;
96  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
97  GPIO_Init(GPIOA, &gpio_init_structure);
98 
99  /* Configure USART Rx as input floating */
100  gpio_init_structure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
101  gpio_init_structure.GPIO_Pin = GPIO_Pin_10;
102  GPIO_Init(GPIOA, &gpio_init_structure);
103 
104  /* USART configuration */
105  USART_Init(USART1, &usart_init_structure);
106 
107  /* Enable USART */
108  USART_Cmd(USART1, ENABLE);
109 }
110 
111 
112 void maxim_gpio_init(void)
122 {
123  /* GPIOB Periph clock enable */
124  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
125 
126  /* Configure PB0 (EN), PB10 (FLTR), PB11 (CNFG), and PB12 (NCS) in output pushpull mode
127  PB5 (FAULT) in input mode*/
128  gpio_init_structure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
129  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
130  gpio_init_structure.GPIO_Mode = GPIO_Mode_Out_PP;
131  GPIO_Init(GPIOB, &gpio_init_structure);
132 
133  gpio_init_structure.GPIO_Pin = GPIO_Pin_5;
134  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
135  gpio_init_structure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
136  GPIO_Init(GPIOB, &gpio_init_structure);
137 
138  /* Output clock on MCO pin ---------------------------------------------*/
139  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
140  gpio_init_structure.GPIO_Pin = GPIO_Pin_8;
141  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
142  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
143  GPIO_Init(GPIOA, &gpio_init_structure);
144  RCC_MCOConfig(RCC_MCO_SYSCLK); // Put on MCO pin the: System clock selected
145 }
146 
147 
148 
149 void maxim_spi_init(void)
160 {
161  // Enable the SPI periph
162  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
163 
164  // Enable SCK, MOSI and MISO GPIO clocks
165  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
166 
167  /* Configure SPI2 pins: SCK, MISO and MOSI ---------------------------------*/
168  gpio_init_structure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
169  gpio_init_structure.GPIO_Speed = GPIO_Speed_50MHz;
170 
171  /* Configure SCK and MOSI pins as Alternate Function Push Pull */
172  gpio_init_structure.GPIO_Mode = GPIO_Mode_AF_PP;
173 
174  GPIO_Init(GPIOB, &gpio_init_structure);
175 
176  gpio_init_structure.GPIO_Pin = GPIO_Pin_14;
177 
178  /* Configure MISO pin as Input Floating */
179  gpio_init_structure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
180  GPIO_Init(GPIOB, &gpio_init_structure);
181 
182  /* SPI2 Config -------------------------------------------------------------*/
183  spi_init_structure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
184  spi_init_structure.SPI_Mode = SPI_Mode_Master;
185  spi_init_structure.SPI_DataSize = SPI_DataSize_16b;
186  spi_init_structure.SPI_CPOL = SPI_CPOL_Low;
187  spi_init_structure.SPI_CPHA = SPI_CPHA_1Edge;
188  spi_init_structure.SPI_NSS = SPI_NSS_Soft;
189  spi_init_structure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
190  spi_init_structure.SPI_FirstBit = SPI_FirstBit_MSB;
191  spi_init_structure.SPI_CRCPolynomial = 7;
192  SPI_Init(SPI2, &spi_init_structure);
193 
194  /* Enable SPI2 */
195  SPI_Cmd(SPI2, ENABLE);
196 }