main.c


1
#include <stdint.h>
2
3
/************* Takt ************************************/
4
5
#define F_Quarz       8000000        
6
#define F_Platform   72000000        
7
#define F_APB        36000000        
8
9
/************* Register defs ***************************/
10
11
#define FLASH_ACR     (*((volatile uint32_t *) 0x40022000)) // 
12
13
#define PWR     0x40007000 // Power control PWR Section 4.4.3 on page 66
14
#define PWR_CR       (*((volatile uint32_t *) (PWR + 0)))
15
16
#define SYST_RVR    (*((volatile uint32_t *) 0xE000E014)) 
17
#define SYST_CSR    (*((volatile uint32_t *) 0xE000E010)) 
18
19
/************* GPIO and AFIO register ******************/
20
21
#define AFIO    0x40010000 // AFIO
22
#define AFIO_MAPR    (*((volatile uint32_t *) (AFIO + 0x04)))
23
24
#define GPIOA   0x40010800 // GPIO Port A Section 8.5 on page 169
25
#define GPIOB   0x40010C00 // GPIO Port B Section 8.5 on page 169
26
#define GPIOC   0x40011000 // GPIO Port C Section 8.5 on page 169
27
#define GPIOD   0x40011400 // GPIO Port D Section 8.5 on page 169
28
29
#define GPIOA_CRL   (*((volatile uint32_t *) (GPIOA + 0x00)))
30
#define GPIOA_CRH   (*((volatile uint32_t *) (GPIOA + 0x04)))
31
#define GPIOA_IDR   (*((volatile uint32_t *) (GPIOA + 0x08)))
32
#define GPIOA_ODR   (*((volatile uint32_t *) (GPIOA + 0x0C))) 
33
#define GPIOA_BSRR  (*((volatile uint32_t *) (GPIOA + 0x10)))
34
#define GPIOA_BRR   (*((volatile uint32_t *) (GPIOA + 0x14)))
35
#define GPIOA_LCKR  (*((volatile uint32_t *) (GPIOA + 0x18)))
36
37
#define GPIOB_CRL   (*((volatile uint32_t *) (GPIOB + 0x00)))
38
#define GPIOB_CRH   (*((volatile uint32_t *) (GPIOB + 0x04)))
39
#define GPIOB_IDR   (*((volatile uint32_t *) (GPIOB + 0x08)))
40
#define GPIOB_ODR   (*((volatile uint32_t *) (GPIOB + 0x0C))) 
41
#define GPIOB_BSRR  (*((volatile uint32_t *) (GPIOB + 0x10)))
42
#define GPIOB_BRR   (*((volatile uint32_t *) (GPIOB + 0x14)))
43
#define GPIOB_LCKR  (*((volatile uint32_t *) (GPIOB + 0x18)))
44
45
#define GPIOC_CRL   (*((volatile uint32_t *) (GPIOC + 0x00)))
46
#define GPIOC_CRH   (*((volatile uint32_t *) (GPIOC + 0x04)))
47
#define GPIOC_IDR   (*((volatile uint32_t *) (GPIOC + 0x08)))
48
#define GPIOC_ODR   (*((volatile uint32_t *) (GPIOC + 0x0C))) 
49
#define GPIOC_BSRR  (*((volatile uint32_t *) (GPIOC + 0x10)))
50
#define GPIOC_BRR   (*((volatile uint32_t *) (GPIOC + 0x14)))
51
#define GPIOC_LCKR  (*((volatile uint32_t *) (GPIOC + 0x18)))
52
53
#define GPIOD_CRL   (*((volatile uint32_t *) (GPIOD + 0x00)))
54
#define GPIOD_CRH   (*((volatile uint32_t *) (GPIOD + 0x04)))
55
#define GPIOD_IDR   (*((volatile uint32_t *) (GPIOD + 0x08)))
56
#define GPIOD_ODR   (*((volatile uint32_t *) (GPIOD + 0x0C))) 
57
#define GPIOD_BSRR  (*((volatile uint32_t *) (GPIOD + 0x10)))
58
#define GPIOD_BRR   (*((volatile uint32_t *) (GPIOD + 0x14)))
59
#define GPIOD_LCKR  (*((volatile uint32_t *) (GPIOD + 0x18)))
60
61
/************* Reset and clock control (RCC) ************/
62
#define RCC     0x40021000 // Reset and clock control Section 6.3.11 on page 103
63
64
#define RCC_CR       (*((volatile uint32_t *) (RCC + 0)))
65
#define RCC_CFGR     (*((volatile uint32_t *) (RCC + 4)))
66
#define RCC_CIR      (*((volatile uint32_t *) (RCC + 8)))
67
#define RCC_APB2RSTR (*((volatile uint32_t *) (RCC + 0x0C)))
68
#define RCC_APB1RSTR (*((volatile uint32_t *) (RCC + 0x10)))
69
#define RCC_AHBENR   (*((volatile uint32_t *) (RCC + 0x14)))
70
#define RCC_APB2ENR  (*((volatile uint32_t *) (RCC + 0x18)))
71
#define RCC_APB1ENR  (*((volatile uint32_t *) (RCC + 0x1C)))
72
#define RCC_BDCR     (*((volatile uint32_t *) (RCC + 0x20)))
73
#define RCC_CSR      (*((volatile uint32_t *) (RCC + 0x24)))
74
#define RCC_AHBRSTR  (*((volatile uint32_t *) (RCC + 0x28)))
75
#define RCC_CFGR2    (*((volatile uint32_t *) (RCC + 0x2C)))
76
77
#define RCC_APB2ENR_SPI1EN                   (1U<<12)
78
79
/************** Serial peripheral interface (SPI) **********/
80
#define SPI1    0x40013000 // SPI1 Section 23.5 on page 627
81
82
#define SPI1_CR1     (*((volatile uint32_t *) (SPI1 + 0x0)))
83
#define SPI1_CR2     (*((volatile uint32_t *) (SPI1 + 0x4)))
84
#define SPI1_SR      (*((volatile uint32_t *) (SPI1 + 0x8)))
85
#define SPI1_DR      (*((volatile uint32_t *) (SPI1 + 0xC)))
86
#define SPI1_CRCPR   (*((volatile uint32_t *) (SPI1 + 0x10)))
87
#define SPI1_RXCRCR  (*((volatile uint32_t *) (SPI1 + 0x14)))
88
#define SPI1_TXCRCR  (*((volatile uint32_t *) (SPI1 + 0x18)))
89
#define SPI1_I2SCFGR (*((volatile uint32_t *) (SPI1 + 0x1C)))
90
#define SPI1_I2SPR   (*((volatile uint32_t *) (SPI1 + 0x20)))
91
92
#define SPI_CR1_CPHA                        (1<<0)
93
#define SPI_CR1_CPOL                        (1<<1)
94
#define SPI_CR1_MSTR                        (1<<2)
95
#define SPI_CR1_BR_DIV2                     (0u << 3)
96
#define SPI_CR1_BR_DIV4                     (1u << 3)
97
#define SPI_CR1_BR_DIV8                     (2u << 3)
98
#define SPI_CR1_BR_DIV16                    (3u << 3)
99
#define SPI_CR1_BR_DIV32                    (4u << 3)
100
#define SPI_CR1_BR_DIV64                    (5u << 3)
101
#define SPI_CR1_BR_DIV128                   (6u << 3)
102
#define SPI_CR1_BR_DIV256                   (7u << 3)
103
104
#define SPI_CR1_SPE                         (1<<6)
105
#define SPI_CR1_LSBFIRST                    (1<<7)
106
#define SPI_CR1_SSI                         (1<<8)
107
#define SPI_CR1_SSM                         (1<<9)
108
#define SPI_CR1_RXONLY                      (1<<10)
109
#define SPI_CR1_DFF                         (1<<11)
110
#define SPI_CR1_CRCNEXT                     (1<<12)
111
#define SPI_CR1_CRCEN                       (1<<13)
112
#define SPI_CR1_BIDIOE                      (1<<14)
113
#define SPI_CR1_BIDIMODE                    (1<<15)
114
115
#define SPI_CR2_RXDMAEN                     (1<<0)
116
#define SPI_CR2_TXDMAEN                     (1<<1)
117
#define SPI_CR2_SSOE                        (1<<2)
118
#define SPI_CR2_reserved1                   (1<<3)
119
#define SPI_CR2_reserved2                   (1<<4)
120
#define SPI_CR2_ERRIE                       (1<<5)
121
#define SPI_CR2_RXNEIE                      (1<<6)
122
#define SPI_CR2_TXEIE                       (1<<7)
123
124
#define SPI_SR_RXNE                         (1<<0)
125
#define SPI_SR_TXE                          (1<<1)
126
#define SPI_SR_CHSIDE                       (1<<2)
127
#define SPI_SR_UDR                          (1<<3)
128
#define SPI_SR_CRCERR                       (1<<4)
129
#define SPI_SR_MODF                         (1<<5)
130
#define SPI_SR_OVR                          (1<<6)
131
#define SPI_SR_BSY                          (1<<7)
132
133
// -------------------- GPIOx_CRx bits --------------------                    
134
135
#define  noPin      4     // floating input, reset state
136
#define  ANALOG     0     /* analoger Eingang (oder Ausgang?) */ 
137
#define  OUT_10     1     /* out, 10 MHz */ 
138
#define  OUT_2      2     /* out,  2 MHz */ 
139
#define  OUT_50     3     /* out, 50 MHz */ 
140
#define  IN         4     /* digitaler Eingang */ 
141
#define  OUT_10_OD  5     /* out, 10 MHz, OpenDrain */ 
142
#define  OUT_2_OD   6     /* out,  2 MHz, OpenDrain */ 
143
#define  OUT_50_OD  7     /* out, 50 MHz, OpenDrain */ 
144
#define  IN_PUPD    8     /* digitaler Eingang, Pullup/down je nach GPIOx_ODR */ 
145
#define  ALTF_10    9     /* Alternativ-Funktion, 10 MHz */ 
146
#define  ALTF_2     10    /* Alternativ-Funktion,  2 MHz */ 
147
#define  ALTF_50    11    /* Alternativ-Funktion, 50 MHz */ 
148
#define  ALTF_10_OD 13    /* Alternativ-Funktion, 10 MHz, OpenDrain */ 
149
#define  ALTF_2_OD  14    /* Alternativ-Funktion,  2 MHz, OpenDrain */ 
150
#define  ALTF_50_OD 15    /* Alternativ-Funktion, 50 MHz, OpenDrain */ 
151
152
// ---------------- Port A --------------------
153
#define wf_PA0      OUT_2_OD      
154
#define wf_PA1      OUT_2_OD      
155
#define wf_PA2      OUT_2_OD      
156
#define wf_PA3      OUT_2_OD      
157
#define wf_PA4      OUT_10        
158
#define wf_PA5      ALTF_50       
159
#define wf_PA6      OUT_10        
160
#define wf_PA7      ALTF_50       
161
#define wf_PA8      noPin         
162
#define wf_PA9      noPin         
163
#define wf_PA10     noPin         
164
#define wf_PA11     ALTF_50       
165
#define wf_PA12     ALTF_50       
166
#define wf_PA13     IN            
167
#define wf_PA14     IN            
168
#define wf_PA15     IN_PUPD       
169
170
// ---------------- Port B --------------------
171
#define wf_PB0      noPin         
172
#define wf_PB1      noPin         
173
#define wf_PB2      IN            
174
#define wf_PB3      IN_PUPD       
175
#define wf_PB4      OUT_2_OD      
176
#define wf_PB5      OUT_2_OD      
177
#define wf_PB6      OUT_2_OD      
178
#define wf_PB7      OUT_2_OD      
179
#define wf_PB8      ALTF_50_OD    
180
#define wf_PB9      ALTF_50_OD    
181
#define wf_PB10     OUT_2         
182
#define wf_PB11     OUT_2         
183
#define wf_PB12     OUT_2         
184
#define wf_PB13     ALTF_50       
185
#define wf_PB14     OUT_2         
186
#define wf_PB15     ALTF_50       
187
188
// ---------------- Port C --------------------
189
#define wf_PC0      noPin           
190
#define wf_PC1      noPin           
191
#define wf_PC2      noPin           
192
#define wf_PC3      noPin           
193
#define wf_PC4      noPin           
194
#define wf_PC5      noPin           
195
#define wf_PC6      noPin           
196
#define wf_PC7      noPin           
197
#define wf_PC8      noPin           
198
#define wf_PC9      noPin           
199
#define wf_PC10     noPin           
200
#define wf_PC11     noPin           
201
#define wf_PC12     noPin           
202
#define wf_PC13     OUT_2           
203
#define wf_PC14     IN              
204
#define wf_PC15     IN              
205
206
// ---------------- Port D --------------------
207
#define wf_PD0      IN              
208
#define wf_PD1      IN              
209
#define wf_PD2      noPin           
210
#define wf_PD3      noPin           
211
#define wf_PD4      noPin           
212
#define wf_PD5      noPin           
213
#define wf_PD6      noPin           
214
#define wf_PD7      noPin           
215
#define wf_PD8      noPin           
216
#define wf_PD9      noPin           
217
#define wf_PD10     noPin           
218
#define wf_PD11     noPin           
219
#define wf_PD12     noPin           
220
#define wf_PD13     noPin           
221
#define wf_PD14     noPin           
222
#define wf_PD15     noPin           
223
224
/**************** Remap setup ******************/
225
226
#define SPI1_REMAP          0     /* PA4,5,6,7 --> PA15,3,4,5 */
227
#define I2C1_REMAP          1     /* PB6,7 --> PB8,9 */
228
#define USART1_REMAP        0     /* PA9,10 --> PB6,7 */
229
#define USART2_REMAP        0     /* PA0,1,2,3,4 --> PD3,4,5,6,7 */
230
231
#define USART3_REMAP0_3     0     /* 0 = PB10,11,12,13,14  */
232
                                  /* 1 = PC10,11,12,PB13,14 */
233
                                  /* 2 = not used */
234
          /* 3 = PD8,9,10,11,12 */
235
236
#define TIM1_REMAP0_3       3     /* 0 = PA12,8,9,10,11,PB12,13,14,15 */
237
                                  /* 1 = PA12,8,9,10,11,6,7,PB0,1 */
238
          /* 2 = not used */
239
          /* 3 = PE7,9,11,13,14,15,8,10,12 */
240
          
241
#define TIM2_REMAP0_3       0     /* 0 = PA0,1,2,3 */
242
                                  /* 1 = PA15,PB3,PA2,PA3  */
243
          /* 2 = PA0,PA1,PB10,PB11 */
244
          /* 3 = PA15,PB3,PB10,PB11*/
245
          
246
#define TIM3_REMAP0_3       0     /* 0 = PA6,7,PB0,1 */
247
                                  /* 1 = not used */
248
          /* 2 = PB4,5,0,1 */
249
          /* 3 = PC6,7,8,9 */
250
          
251
#define TIM4_REMAP          0     /* PB6,7,8,9 --> PD12,13,14,15 */
252
#define CAN1_REMAP0_3       0     /* 0 = PA11,12 */
253
                                  /* 1 = not used */
254
          /* 2 = PB8,9 */
255
          /* 3 = PD0,1 */
256
          
257
#define PD01_REMAP          1     /* PD0,1 -->  OSC_IN, OSC_OUT */
258
#define TIM5CH4_REMAP       0     /* TIM5_CH4: PA3 --> LSI internal Clock */
259
#define ADC1_ETRGINJ_REMAP  0     
260
#define ADC1_ETRGREG_REMAP  0     
261
#define ADC2_ETRGINJ_REMAP  0     
262
#define ADC3_ETRGREG_REMAP  0     
263
#define SWJ_CFG0_7          0     
264
265
/************* power and clock configuration ***************/
266
267
/* AHB */
268
#define en_DMA1     (0<<0)   /* DMA 1 Clock */
269
#define en_DMA2     (0<<1)   /* DMA 2 Clock */
270
#define en_SRAM     (1<<2)   /* SRAM während Schlafmodus */
271
                             /* 3 nix */
272
#define en_FLASH    (1<<4)   /* Flash Interface */
273
                             /* 5 nix */
274
#define en_CRC      (0<<6)   /* CRC-32 (ethernet) Calculator */
275
                             /* 7 nix */
276
#define en_FSMC     (0<<8)   /* externer Speichercontroller */
277
                             /* 9 nix */
278
#define en_SDIO     (0<<10)  /* SD-karten-Interface */
279
280
/***** APB1 ************/
281
                         /* 31..30 nix */
282
#define en1_DAC     0    /* 29 */
283
#define en1_PWR     1    /* 28 Power Interface */
284
#define en1_BKP     1    /* 27 Backup Interface */
285
                         /* 26 nix */
286
#define en1_CAN     0    /* 25 */
287
                         /* 24 nix */
288
#define en1_USB     1    /* 23 */
289
#define en1_I2C2    0    /* 22 */
290
#define en1_I2C1    0    /* 21 */
291
#define en1_UART5   0    /* 20 */
292
#define en1_UART4   0    /* 19 konkurriert mit SDIO */
293
#define en1_USART3  1    /* 18 */
294
#define en1_USART2  1    /* 17 */
295
                         /* 16 nix */
296
#define en1_SPI3    0    /* 15 */
297
#define en1_SPI2    0    /* 14 */
298
                         /* 13..12 nix */
299
#define en1_WWD     0    /* 11 Watchdog */
300
                         /* 10..6 nix */
301
#define en1_TIM7    0    /* 5 */
302
#define en1_TIM6    0    /* 4 */
303
#define en1_TIM5    0    /* 3 */
304
#define en1_TIM4    0    /* 2 */
305
#define en1_TIM3    0    /* 1 */
306
#define en1_TIM2    0    /* 0 */
307
308
309
/***** APB2 *****/
310
                         /* 31..16 nix */
311
#define en2_ADC3    0    /* 15 */
312
#define en2_USART1  1    /* 14 */
313
#define en2_TIM8    0    /* 13 konkurriert mit SDIO */
314
#define en2_SPI1    1    /* 12 */
315
#define en2_TIM1    0    /* 11 */
316
#define en2_ADC2    0    /* 10 */
317
#define en2_ADC1    0    /* 9 */
318
#define en2_IOPG    0    /* 8 Port G */
319
#define en2_IOPF    0    /* 7 Port F */
320
#define en2_IOPE    0    /* 6 Port E */
321
#define en2_IOPD    1    /* 5 Port D */
322
#define en2_IOPC    1    /* 4 Port C */
323
#define en2_IOPB    1    /* 3 Port B */
324
#define en2_IOPA    1    /* 2 Port A */
325
                         /* 1 nix */
326
#define en2_AFIO    1    /* 0 Alternative Portbelegungen */
327
328
329
#define wf_AFIO_MAPR  (SPI1_REMAP)|         \
330
                      (I2C1_REMAP<<1)|      \
331
          (USART1_REMAP<<2)|    \
332
          (USART2_REMAP<<3)|    \
333
          (USART3_REMAP0_3<<4)| \
334
          (TIM1_REMAP0_3<<6)|   \
335
          (TIM2_REMAP0_3<8)|    \
336
          (TIM3_REMAP0_3<10)|   \
337
          (TIM4_REMAP<<12)|     \
338
          (CAN1_REMAP0_3<<13)|  \
339
          (PD01_REMAP<<15)|     \
340
          (TIM5CH4_REMAP<<16)|  \
341
          (ADC1_ETRGINJ_REMAP<<17)| \
342
          (ADC1_ETRGREG_REMAP<<18)| \
343
          (ADC2_ETRGINJ_REMAP<<19)| \
344
                      (ADC3_ETRGREG_REMAP<<20)| \
345
          (SWJ_CFG0_7<<24)
346
347
#define wf_GPIOA_CRL  (wf_PA0 | (wf_PA1<<4) | (wf_PA2<<8)  | (wf_PA3<<12)  | (wf_PA4<<16)  | (wf_PA5<<20)  | (wf_PA6<<24)  | (((unsigned)wf_PA7)<<28))
348
#define wf_GPIOA_CRH  (wf_PA8 | (wf_PA9<<4) | (wf_PA10<<8) | (wf_PA11<<12) | (wf_PA12<<16) | (wf_PA13<<20) | (wf_PA14<<24) | (((unsigned)wf_PA15)<<28))
349
350
#define wf_GPIOB_CRL  (wf_PB0 | (wf_PB1<<4) | (wf_PB2<<8)  | (wf_PB3<<12)  | (wf_PB4<<16)  | (wf_PB5<<20)  | (wf_PB6<<24)  | (((unsigned)wf_PB7)<<28))
351
#define wf_GPIOB_CRH  (wf_PB8 | (wf_PB9<<4) | (wf_PB10<<8) | (wf_PB11<<12) | (wf_PB12<<16) | (wf_PB13<<20) | (wf_PB14<<24) | (((unsigned)wf_PB15)<<28))
352
353
#define wf_GPIOC_CRL  (wf_PC0 | (wf_PC1<<4) | (wf_PC2<<8)  | (wf_PC3<<12)  | (wf_PC4<<16)  | (wf_PC5<<20)  | (wf_PC6<<24)  | (((unsigned)wf_PC7)<<28))
354
#define wf_GPIOC_CRH  (wf_PC8 | (wf_PC9<<4) | (wf_PC10<<8) | (wf_PC11<<12) | (wf_PC12<<16) | (wf_PC13<<20) | (wf_PC14<<24) | (((unsigned)wf_PC15)<<28))
355
356
#define wf_GPIOD_CRL  (wf_PD0 | (wf_PD1<<4) | (wf_PD2<<8)  | (wf_PD3<<12)  | (wf_PD4<<16)  | (wf_PD5<<20)  | (wf_PD6<<24)  | (((unsigned)wf_PD7)<<28))
357
#define wf_GPIOD_CRH  (wf_PD8 | (wf_PD9<<4) | (wf_PD10<<8) | (wf_PD11<<12) | (wf_PD12<<16) | (wf_PD13<<20) | (wf_PD14<<24) | (((unsigned)wf_PD15)<<28))
358
359
#define wf_AHBENR   (en_SDIO | en_FSMC | en_CRC | en_FLASH | en_SRAM | en_DMA2 | en_DMA1)
360
361
#define wf_APB1ENR  (en1_TIM2<<0)   | \
362
                    (en1_TIM3<<1)   | \
363
        (en1_TIM4<<2)   | \
364
        (en1_TIM5<<3)   | \
365
        (en1_TIM6<<4)   | \
366
        (en1_TIM7<<5)   | \
367
        (en1_WWD<<11)   | \
368
        (en1_SPI2<<14)  | \
369
        (en1_SPI3<<15)  | \
370
        (en1_USART2<<17)| \
371
        (en1_USART3<<18)| \
372
        (en1_UART4<<19) | \
373
        (en1_UART5<<20) | \
374
        (en1_I2C1<<21)  | \
375
        (en1_I2C2<<22)  | \
376
        (en1_USB<<23)   | \
377
        (en1_CAN<<25)   | \
378
        (en1_BKP<<27)   | \
379
        (en1_PWR<<28)   | \
380
        (en1_DAC<<29)
381
382
#define wf_APB2ENR  (en2_AFIO)    |  \
383
                    (en2_IOPA<<2) |  \
384
        (en2_IOPB<<3) |  \
385
        (en2_IOPC<<4) |  \
386
        (en2_IOPD<<5) |  \
387
        (en2_IOPE<<6) |  \
388
        (en2_IOPF<<7) |  \
389
        (en2_IOPG<<8) |  \
390
        (en2_ADC1<<9) |  \
391
        (en2_ADC2<<10) | \
392
        (en2_TIM1<<11) | \
393
        (en2_SPI1<<12) | \
394
        (en2_TIM8<<13) | \
395
        (en2_USART1<<14) | \
396
        (en2_ADC3<<15)
397
398
/* ---------- systick ----------- */
399
400
volatile uint32_t ticks = 0;
401
402
void SysTick_Handler (void)
403
{
404
  ticks++;
405
}
406
407
void systick_init (void)
408
{
409
  ticks = 0;
410
  SYST_RVR = (F_Platform/1000)-1;     
411
  SYST_CSR = 7;                       
412
}
413
414
void delay_ms (uint32_t ms)
415
{
416
  const uint32_t start = ticks;
417
  while(ticks - start < ms);    
418
}
419
420
/* ---------- sysconfig ---------- */
421
422
void sysconfig (void)
423
{
424
  long L;
425
426
  RCC_CIR      = 0;            
427
  RCC_APB1RSTR = 0;            
428
  RCC_APB2RSTR = 0;
429
  FLASH_ACR    = (1<<4) |      
430
    (0<<3) |                   
431
    2;                         
432
433
  RCC_CFGR     = (5<<24);      
434
435
  RCC_AHBENR  = wf_AHBENR;     
436
  RCC_APB1ENR = wf_APB1ENR;    
437
  RCC_APB2ENR = wf_APB2ENR;    
438
439
  GPIOA_BSRR = (1U<<4);        
440
  GPIOA_CRL = wf_GPIOA_CRL;
441
  GPIOA_CRH = wf_GPIOA_CRH;
442
443
  GPIOB_CRL = wf_GPIOB_CRL;
444
  GPIOB_CRH = wf_GPIOB_CRH;
445
446
  GPIOC_CRL = wf_GPIOC_CRL;
447
  GPIOC_CRH = wf_GPIOC_CRH;
448
  
449
  AFIO_MAPR = wf_AFIO_MAPR;
450
451
  RCC_CR    =  (8<<2)|1; 
452
  while ((RCC_CR & 2)==0); 
453
454
  RCC_CR |= (1<<16);      
455
  L = 10000;
456
  while (--L) {
457
    --L;
458
    if (RCC_CR & (1<<17))    
459
      break;
460
  }        
461
462
  if (L) {                       
463
    FLASH_ACR   = (1<<4)|2;      
464
    RCC_CFGR    = (7<<24) |      
465
                  (0<<22) |      
466
                  (7<<18) |      
467
                  (1<<16) |      
468
                  (3<<14) |      
469
                  (4<<11) |      
470
                  (4<<8);        
471
472
    RCC_CR     |= (1<<24);       
473
474
    while ((RCC_CR & (1<<25)) == 0); 
475
    RCC_CFGR = (RCC_CFGR & 0xFFFFFFFC) | 2;
476
    do {
477
      L = RCC_CFGR & (3<<2);
478
    } while (L != (2<<2)); 
479
  }
480
481
  if ((RCC_BDCR & 2)==0) {
482
    PWR_CR |= (1<<8);
483
    RCC_BDCR = (1<<16)|(1<<15)|1;
484
    RCC_BDCR = (1<<15)|(1<<8)|1; 
485
  }
486
}
487
488
/* ---------- main ---------- */
489
490
int main(void)
491
{
492
  systick_init();
493
  
494
  // initialisiere SPI1
495
  SPI1_CR2 = 0x00;
496
  SPI1_CR1 = SPI_CR1_CPHA
497
    | SPI_CR1_CPHA
498
    | SPI_CR1_BR_DIV256
499
    | SPI_CR1_SSM
500
    | SPI_CR1_SPE
501
    | SPI_CR1_SSI
502
    | SPI_CR1_MSTR;
503
504
  // auf SPI1 senden
505
  for(;;) {
506
    // Test ob was kommt -> Signal da auf PA6 ist da!
507
    GPIOA_BSRR = (1u<<6);
508
    delay_ms(1);
509
    GPIOA_BRR = (1u<<6);
510
    delay_ms(10);
511
512
    while ( ! (SPI1_SR & SPI_SR_TXE) );   // wait until TXE = 1
513
    SPI1_DR = 0x33;  
514
    while ( ! (SPI1_SR & SPI_SR_TXE) );   // wait until TXE = 1
515
    while ( (SPI1_SR & SPI_SR_BSY) );     // wait until BSY = 0
516
  }
517
}