The STM32 UART is finally running.
[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 void ledon(){
37   GPIO_SetBits(GPIOD, GPIO_Pin_14);
38 }
39 void ledoff(){
40   GPIO_ResetBits(GPIOD, GPIO_Pin_14);
41 }
42 void clkon(){
43   GPIO_SetBits(GPIOD, GPIO_Pin_12);
44 }
45 void clkoff(){
46   GPIO_ResetBits(GPIOD, GPIO_Pin_12);
47 }
48
49 void spibit(int one){
50   if(one) ledon();
51   else    ledoff();
52   clkon();
53   stmdelay();
54   clkoff();
55   stmdelay();
56 }
57
58 void spiword(uint32_t word){
59   int i=32;
60   while(i--){
61     //morsebit(word&1);
62     //manchesterbit(word&1);
63     spibit(word&1);
64     word=(word>>1);
65   }
66 }
67 void spibyte(uint8_t word){
68   int i=8;
69   while(i--){
70     //morsebit(word&1);
71     //manchesterbit(word&1);
72     spibit(word&1);
73     word=(word>>1);
74   }
75 }
76
77
78
79 //! Count the length of a string.
80 uint32_t strlen(const char *s){
81   int i=0;
82   while(s[i++]);
83   return i-1;
84 }
85
86
87 /**************************************************************************************/
88 void Repair_Data();
89 void RCC_Configuration(void)
90 {
91   /* --------------------------- System Clocks Configuration -----------------*/
92   /* USART1 clock enable */
93   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
94  
95   /* GPIOB clock enable */
96   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
97 }
98  
99 /**************************************************************************************/
100  
101 void GPIO_Configuration(void)
102 {
103   GPIO_InitTypeDef GPIO_InitStructure;
104  
105   /*-------------------------- GPIO Configuration ----------------------------*/
106   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
107   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
108   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
109   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
110   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
111   GPIO_Init(GPIOB, &GPIO_InitStructure);
112  
113   /* Connect USART pins to AF */
114   GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // USART1_TX
115   GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); // USART1_RX
116 }
117  
118 /**************************************************************************************/
119  
120 void USART1_Configuration(void)
121 {
122   USART_InitTypeDef USART_InitStructure;
123  
124   /* USARTx configuration ------------------------------------------------------*/
125   /* USARTx configured as follow:
126         - BaudRate = 9600 baud
127         - Word Length = 8 Bits
128         - One Stop Bit
129         - No parity
130         - Hardware flow control disabled (RTS and CTS signals)
131         - Receive and transmit enabled
132   */
133   USART_InitStructure.USART_BaudRate = 9600;
134   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
135   USART_InitStructure.USART_StopBits = USART_StopBits_1;
136   USART_InitStructure.USART_Parity = USART_Parity_No;
137   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
138  
139   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
140  
141   USART_Init(USART1, &USART_InitStructure);
142  
143   USART_Cmd(USART1, ENABLE);
144 }
145  
146
147 //! Initialize the USART
148 void usartinit(){
149   RCC_Configuration();
150  
151   GPIO_Configuration();
152  
153   USART1_Configuration();
154 }
155
156
157 //! Initialize the STM32F4xx ports and USB.
158 void stm32f4xx_init(){
159   int i=20;
160   
161   SystemInit();
162   ioinit();
163   usartinit();
164   
165   while(i--) stmdelay();
166   
167   serial0_tx(0xAA);
168   
169   return;
170 }
171
172 //! Receive a byte.
173 unsigned char serial0_rx(){
174   while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); // Wait for Character
175   return USART_ReceiveData(USART1);
176 }
177
178 //! Receive a byte.
179 unsigned char serial1_rx(){
180 }
181
182 //! Transmit a byte.
183 void serial0_tx(unsigned char x){
184   
185   spiword(0xdeadbeef);
186   
187   spiword(USART1->SR);
188   
189   while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) //original
190     spiword(USART1->SR);
191   USART_SendData(USART1, (uint16_t) x);
192   
193   spiword(USART1->SR);
194   
195   stmdelay();
196   stmdelay();
197   stmdelay();
198   stmdelay();
199   stmdelay();
200   stmdelay();
201   
202 }
203
204 //! Transmit a byte on the second UART.
205 void serial1_tx(unsigned char x){
206
207 }
208
209 //! Set the baud rate.
210 void setbaud0(unsigned char rate){
211   //Ignore this, as we'll be in USB.
212 }
213
214 //! Set the baud rate of the second uart.
215 void setbaud1(unsigned char rate){
216
217 }
218
219
220 //Declarations
221 void nmi_handler(void);
222 void hardfault_handler(void);
223 int main(void);
224
225 //From min.s
226 void Reset_Handler(void);
227
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)  
235 };