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_2MHz;
26 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
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 GPIO_SetBits(GPIOD, GPIO_Pin_14);
40 GPIO_ResetBits(GPIOD, GPIO_Pin_14);
43 GPIO_SetBits(GPIOD, GPIO_Pin_12);
46 GPIO_ResetBits(GPIOD, GPIO_Pin_12);
58 void spiword(uint32_t word){
62 //manchesterbit(word&1);
67 void spibyte(uint8_t word){
71 //manchesterbit(word&1);
79 //! Count the length of a string.
80 uint32_t strlen(const char *s){
87 //! Initialize the USART
89 GPIO_InitTypeDef GPIO_InitStructure;
90 USART_InitTypeDef USART_InitStructure;
92 /* --------------------------- System Clocks Configuration -----------------*/
93 /* USART1 clock enable */
94 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
96 /* GPIOB clock enable */
97 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
101 /*-------------------------- GPIO Configuration ----------------------------*/
102 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
103 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
104 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
105 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
106 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
107 GPIO_Init(GPIOB, &GPIO_InitStructure);
109 /* Connect USART pins to AF */
110 GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // USART1_TX
111 GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); // USART1_RX
113 /* USARTx configuration ------------------------------------------------------*/
114 /* USARTx configured as follow:
115 - BaudRate = 9600 baud
116 - Word Length = 8 Bits
119 - Hardware flow control disabled (RTS and CTS signals)
120 - Receive and transmit enabled
122 USART_InitStructure.USART_BaudRate = 9600;
123 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
124 USART_InitStructure.USART_StopBits = USART_StopBits_1;
125 USART_InitStructure.USART_Parity = USART_Parity_No;
126 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
128 //USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
129 USART_InitStructure.USART_Mode = USART_Mode_Tx;
131 USART_Init(USART1, &USART_InitStructure);
133 USART_Cmd(USART1, ENABLE);
137 //! Initialize the STM32F4xx ports and USB.
138 void stm32f4xx_init(){
150 unsigned char serial0_rx(){
151 while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Character
152 return USART_ReceiveData(USART1);
156 unsigned char serial1_rx(){
160 void serial0_tx(unsigned char x){
164 while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
165 USART_SendData(USART1, (uint16_t) x);
170 //! Transmit a byte on the second UART.
171 void serial1_tx(unsigned char x){
175 //! Set the baud rate.
176 void setbaud0(unsigned char rate){
177 //Ignore this, as we'll be in USB.
180 //! Set the baud rate of the second uart.
181 void setbaud1(unsigned char rate){
187 void nmi_handler(void);
188 void hardfault_handler(void);
192 void Reset_Handler(void);
194 // Define the vector table
195 unsigned int * myvectors[50]
196 __attribute__ ((section("vectors")))= {
197 (unsigned int *) 0x20000800, // stack pointer
198 (unsigned int *) Reset_Handler, // code entry point
199 (unsigned int *) main, // NMI handler (not really)
200 (unsigned int *) main, // hard fault handler (let's hope not)