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 USART_InitTypeDef USART_InitStructure;
90 GPIO_InitTypeDef GPIO_InitStructure;
91 USART_ClockInitTypeDef USART_ClockInitStruct;
93 /* Enable GPIOA and USART1 clock */
94 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
96 //USART1 and USART6, other in APB1
97 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
99 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
105 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
106 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
107 GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_USART1);
108 GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_USART1);
110 /* Configure USART Tx and Rx as alternate function push-pull */
111 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
112 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
113 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
114 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
115 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
116 GPIO_Init(GPIOA, &GPIO_InitStructure);
119 USART_StructInit(&USART_InitStructure);
120 USART_InitStructure.USART_BaudRate = 9600;
121 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
122 USART_InitStructure.USART_StopBits = USART_StopBits_1;
123 USART_InitStructure.USART_Parity = USART_Parity_No;
124 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
125 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
126 USART_Init(USART1, &USART_InitStructure);
129 USART_ClockStructInit(&USART_ClockInitStruct);
130 USART_ClockInit(USART1,&USART_ClockInitStruct);
132 USART_Cmd(USART1, ENABLE);
136 //! Initialize the STM32F4xx ports and USB.
137 void stm32f4xx_init(){
151 unsigned char serial0_rx(){
155 unsigned char serial1_rx(){
159 void serial0_tx(unsigned char x){
160 USART_SendData(USART1, (uint16_t) x);
161 //while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
166 //! Transmit a byte on the second UART.
167 void serial1_tx(unsigned char x){
171 //! Set the baud rate.
172 void setbaud0(unsigned char rate){
173 //Ignore this, as we'll be in USB.
176 //! Set the baud rate of the second uart.
177 void setbaud1(unsigned char rate){
183 void nmi_handler(void);
184 void hardfault_handler(void);
188 void Reset_Handler(void);
190 // Define the vector table
191 unsigned int * myvectors[50]
192 __attribute__ ((section("vectors")))= {
193 (unsigned int *) 0x20000800, // stack pointer
194 (unsigned int *) Reset_Handler, // code entry point
195 (unsigned int *) main, // NMI handler (not really)
196 (unsigned int *) main, // hard fault handler (let's hope not)