2 \author Travis Goodspeed
3 \brief STM32F407 port definitions.
9 //#include "stm322xg_eval.h"
10 #include <stm32f4xx_gpio.h>
11 #include <stm32f4xx_rcc.h>
12 #include <stm32f4xx_usart.h>
13 #include "stm32f4_discovery.h"
16 GPIO_InitTypeDef GPIO_InitStructure;
18 /* GPIOD Periph clock enable */
19 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
21 /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
22 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
23 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
24 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
25 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
26 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
27 GPIO_Init(GPIOD, &GPIO_InitStructure);
31 //IO so it doesn't get swapped out.
32 __IO uint32_t count= 0x1000; // >1kbit/s
33 //__IO uint32_t count= 0x100000; // 5 bits per second, for testing
37 /* Used for debugging only.
40 GPIO_SetBits(GPIOD, GPIO_Pin_14);
43 GPIO_ResetBits(GPIOD, GPIO_Pin_14);
46 GPIO_SetBits(GPIOD, GPIO_Pin_12);
49 GPIO_ResetBits(GPIOD, GPIO_Pin_12);
63 void spiword(uint32_t word){
67 //manchesterbit(word&1);
72 void spibyte(uint8_t word){
76 //manchesterbit(word&1);
84 //! Count the length of a string.
85 uint32_t strlen(const char *s){
92 /**************************************************************************************/
94 void RCC_Configuration(void)
96 /* --------------------------- System Clocks Configuration -----------------*/
97 /* USART1 clock enable */
98 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
100 /* GPIOB clock enable */
101 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
104 /**************************************************************************************/
106 void GPIO_Configuration(void)
108 GPIO_InitTypeDef GPIO_InitStructure;
110 /*-------------------------- GPIO Configuration ----------------------------*/
111 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
112 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
113 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
114 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
115 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
116 GPIO_Init(GPIOB, &GPIO_InitStructure);
118 /* Connect USART pins to AF */
119 GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // USART1_TX
120 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); // USART1_RX
123 /**************************************************************************************/
125 void USART1_Configuration(void)
127 USART_InitTypeDef USART_InitStructure;
129 /* USARTx configuration ------------------------------------------------------*/
130 /* USARTx configured as follow:
131 - BaudRate = 115200 baud
132 - Word Length = 8 Bits
135 - Hardware flow control disabled (RTS and CTS signals)
136 - Receive and transmit enabled
138 //USART_InitStructure.USART_BaudRate = 9600;
139 //USART_InitStructure.USART_BaudRate = 10000; //Close enough to 9600
140 //USART_InitStructure.USART_BaudRate = 115200;
141 //USART_InitStructure.USART_BaudRate = 125200; //Close enough to 115200
145 USART_InitStructure.USART_BaudRate = 125000; //Close enough to 115200
147 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
148 USART_InitStructure.USART_StopBits = USART_StopBits_1;
149 USART_InitStructure.USART_Parity = USART_Parity_No;
150 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
152 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
154 USART_Init(USART1, &USART_InitStructure);
156 USART_Cmd(USART1, ENABLE);
160 //! Initialize the USART
164 GPIO_Configuration();
166 USART1_Configuration();
170 //! Initialize the STM32F4xx ports and USB.
171 void stm32f4xx_init(){
178 while(i--) stmdelay();
184 unsigned char serial0_rx(){
185 while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Character
186 return USART_ReceiveData(USART1);
190 unsigned char serial1_rx(){
194 void serial0_tx(unsigned char x){
196 //Send through USART1 on PB6
197 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //original
198 USART_SendData(USART1, (uint16_t) x);
204 //! Transmit a byte on the second UART.
205 void serial1_tx(unsigned char x){
209 //! Set the baud rate.
210 void setbaud0(unsigned char rate){
211 //Ignore this, as we'll be in USB.
214 //! Set the baud rate of the second uart.
215 void setbaud1(unsigned char rate){
221 void nmi_handler(void);
222 void hardfault_handler(void);
226 void Reset_Handler(void);
228 // Define the vector table
229 unsigned int * myvectors[50]
230 __attribute__ ((section("vectors")))= {
231 (unsigned int *) 0x20000800, // stack pointer
232 (unsigned int *) Reset_Handler, // code entry point
233 (unsigned int *) main, // NMI handler (not really)
234 (unsigned int *) main, // hard fault handler (let's hope not)