5367012430f6e1c0118169e4e3388d7bf841bda9
[goodfet] / firmware / lib / stm32f407.c
1 /*! \file stm32f407.h
2   \author Travis Goodspeed
3   \brief STM32F407 port definitions.
4 */
5
6 #include "platform.h"
7
8 #include "stm32f4xx.h"
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"
14
15 void ioinit(){
16   GPIO_InitTypeDef  GPIO_InitStructure;
17   
18   /* GPIOD Periph clock enable */
19   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
20
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);
28 }
29
30 void stmdelay(){
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
34   while(count--);
35 }
36
37 /* Used for debugging only.
38
39 void ledon(){
40   GPIO_SetBits(GPIOD, GPIO_Pin_14);
41 }
42 void ledoff(){
43   GPIO_ResetBits(GPIOD, GPIO_Pin_14);
44 }
45 void clkon(){
46   GPIO_SetBits(GPIOD, GPIO_Pin_12);
47 }
48 void clkoff(){
49   GPIO_ResetBits(GPIOD, GPIO_Pin_12);
50 }
51
52
53
54 void spibit(int one){
55   if(one) ledon();
56   else    ledoff();
57   clkon();
58   stmdelay();
59   clkoff();
60   stmdelay();
61 }
62
63 void spiword(uint32_t word){
64   int i=32;
65   while(i--){
66     //morsebit(word&1);
67     //manchesterbit(word&1);
68     spibit(word&1);
69     word=(word>>1);
70   }
71 }
72 void spibyte(uint8_t word){
73   int i=8;
74   while(i--){
75     //morsebit(word&1);
76     //manchesterbit(word&1);
77     spibit(word&1);
78     word=(word>>1);
79   }
80 }
81
82 */
83
84 //! Count the length of a string.
85 uint32_t strlen(const char *s){
86   int i=0;
87   while(s[i++]);
88   return i-1;
89 }
90
91
92 /**************************************************************************************/
93 void Repair_Data();
94 void RCC_Configuration(void)
95 {
96   /* --------------------------- System Clocks Configuration -----------------*/
97   /* USART1 clock enable */
98   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
99  
100   /* GPIOB clock enable */
101   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
102 }
103  
104 /**************************************************************************************/
105  
106 void GPIO_Configuration(void)
107 {
108   GPIO_InitTypeDef GPIO_InitStructure;
109  
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);
117  
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
121 }
122  
123 /**************************************************************************************/
124  
125 void USART1_Configuration(void)
126 {
127   USART_InitTypeDef USART_InitStructure;
128  
129   /* USARTx configuration ------------------------------------------------------*/
130   /* USARTx configured as follow:
131         - BaudRate = 115200 baud
132         - Word Length = 8 Bits
133         - One Stop Bit
134         - No parity
135         - Hardware flow control disabled (RTS and CTS signals)
136         - Receive and transmit enabled
137   */
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
142   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
143   USART_InitStructure.USART_StopBits = USART_StopBits_1;
144   USART_InitStructure.USART_Parity = USART_Parity_No;
145   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
146  
147   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
148  
149   USART_Init(USART1, &USART_InitStructure);
150  
151   USART_Cmd(USART1, ENABLE);
152 }
153  
154
155 //! Initialize the USART
156 void usartinit(){
157   RCC_Configuration();
158  
159   GPIO_Configuration();
160  
161   USART1_Configuration();
162 }
163
164
165 //! Initialize the STM32F4xx ports and USB.
166 void stm32f4xx_init(){
167   int i=20;
168   
169   SystemInit();
170   ioinit();
171   usartinit();
172   
173   while(i--) stmdelay();
174   
175   serial0_tx(0xAA);
176   
177   return;
178 }
179
180 //! Receive a byte.
181 unsigned char serial0_rx(){
182   while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Character
183   return USART_ReceiveData(USART1);
184 }
185
186 //! Receive a byte.
187 unsigned char serial1_rx(){
188 }
189
190 //! Transmit a byte.
191 void serial0_tx(unsigned char x){
192   
193   //Send through USART1 on PB6
194   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //original
195   USART_SendData(USART1, (uint16_t) x);
196   
197   //Spare goes to SPI.
198   //spibyte(x);
199 }
200
201 //! Transmit a byte on the second UART.
202 void serial1_tx(unsigned char x){
203
204 }
205
206 //! Set the baud rate.
207 void setbaud0(unsigned char rate){
208   //Ignore this, as we'll be in USB.
209 }
210
211 //! Set the baud rate of the second uart.
212 void setbaud1(unsigned char rate){
213
214 }
215
216
217 //Declarations
218 void nmi_handler(void);
219 void hardfault_handler(void);
220 int main(void);
221
222 //From min.s
223 void Reset_Handler(void);
224
225 // Define the vector table
226 unsigned int * myvectors[50] 
227    __attribute__ ((section("vectors")))= {
228         (unsigned int *)        0x20000800,             // stack pointer
229         (unsigned int *)        Reset_Handler,                  // code entry point
230         (unsigned int *)        main,           // NMI handler (not really)
231         (unsigned int *)        main,   // hard fault handler (let's hope not)  
232 };