c75e631d6b84e170c031db6366666a0d92e842dd
[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_2MHz;
26   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
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 //! Initialize the USART
88 void usartinit(){
89   USART_InitTypeDef USART_InitStructure;
90   GPIO_InitTypeDef GPIO_InitStructure;
91   USART_ClockInitTypeDef USART_ClockInitStruct;
92   
93   /* Enable GPIOA and USART1 clock */
94   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
95   
96   //USART1 and USART6, other in APB1
97   RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
98   
99   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
100   /* A 9 to USART1_Tx
101    * A10 to USART1_Rx
102    * A11 to USART1_CTS
103    * A12 to USART1_RTS 
104    */
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);
109  
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);
117   
118   
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);
127   
128   
129   USART_ClockStructInit(&USART_ClockInitStruct);
130   USART_ClockInit(USART1,&USART_ClockInitStruct);
131   
132   USART_Cmd(USART1, ENABLE);
133 }
134
135
136 //! Initialize the STM32F4xx ports and USB.
137 void stm32f4xx_init(){
138   int i=20;
139   
140   //SystemInit();
141   usartinit();
142   ioinit();
143   
144   ledoff();
145   clkoff();
146   
147   return;
148 }
149
150 //! Receive a byte.
151 unsigned char serial0_rx(){
152 }
153
154 //! Receive a byte.
155 unsigned char serial1_rx(){
156 }
157
158 //! Transmit a byte.
159 void serial0_tx(unsigned char x){
160   USART_SendData(USART1, (uint16_t) x);
161   //while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
162   
163   spibyte(x);
164 }
165
166 //! Transmit a byte on the second UART.
167 void serial1_tx(unsigned char x){
168
169 }
170
171 //! Set the baud rate.
172 void setbaud0(unsigned char rate){
173   //Ignore this, as we'll be in USB.
174 }
175
176 //! Set the baud rate of the second uart.
177 void setbaud1(unsigned char rate){
178
179 }
180
181
182 //Declarations
183 void nmi_handler(void);
184 void hardfault_handler(void);
185 int main(void);
186
187 //From min.s
188 void Reset_Handler(void);
189
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)  
197 };