1 | #include "semihosting.h"
|
2 | #include <stdio.h>
|
3 | #include <stm32f0xx_conf.h>
|
4 |
|
5 | GPIO_InitTypeDef GPIO_InitStructure;
|
6 | SPI_InitTypeDef SPI_InitStructure;
|
7 |
|
8 |
|
9 | void SPI_Configure(void)
|
10 | {
|
11 | SPI_InitTypeDef SPI_InitStructure;
|
12 |
|
13 | RCC_APB1PeriphResetCmd(RCC_APB1Periph_SPI2, ENABLE);
|
14 |
|
15 | SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
|
16 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //
|
17 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
18 | SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
|
19 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
|
20 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
21 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; //
|
22 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
23 | SPI_InitStructure.SPI_CRCPolynomial = 7;
|
24 | SPI_Init(SPI2, &SPI_InitStructure);
|
25 | SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
|
26 | SPI_Cmd(SPI2, ENABLE);
|
27 |
|
28 |
|
29 |
|
30 | }
|
31 |
|
32 |
|
33 | /*void LED_init(void){
|
34 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
35 | GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;
|
36 | GPIO_InitStructure.GPIO_Pin=(GPIO_Pin_9 | GPIO_Pin_8);
|
37 | GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
|
38 | GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
|
39 | GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
|
40 | GPIO_Init(GPIOC,&GPIO_InitStructure);
|
41 |
|
42 | }*/
|
43 |
|
44 | void CS_config1(void)
|
45 | {
|
46 |
|
47 | // Definition von CLK und MOSI
|
48 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
49 |
|
50 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
|
51 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
|
52 | GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
53 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
54 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
55 |
|
56 |
|
57 |
|
58 | // Defintion von Slave Select Pins
|
59 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
|
60 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12;
|
61 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
|
62 | GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
|
63 | GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
64 |
|
65 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
66 |
|
67 |
|
68 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_0);
|
69 | GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_0);
|
70 |
|
71 |
|
72 | }
|
73 |
|
74 | uint8_t send_and_read_byte(uint8_t cmd) // Funktionsdefinition
|
75 | {
|
76 | // cmd ist die zu sendende Variable
|
77 | uint8_t result;
|
78 | GPIO_SetBits(GPIOB, GPIO_Pin_12);
|
79 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET); //wait buffer empty
|
80 | SPI_SendData8(SPI2, cmd);
|
81 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_BSY) == SET); //wait finish sending // Read receiving FIFO until it is empty
|
82 | while (SPI_GetReceptionFIFOStatus(SPI2) != SPI_ReceptionFIFOStatus_Empty) {
|
83 | SPI_ReceiveData8(SPI2);
|
84 | }
|
85 | SPI_BiDirectionalLineConfig(SPI2, SPI_Direction_Rx);
|
86 | while (!(SPI2->SR & SPI_I2S_FLAG_RXNE)); // wait data received
|
87 | GPIO_ResetBits(GPIOB, GPIO_Pin_12); // CS high
|
88 | SPI2->CR1 |= SPI_Direction_Tx; // Set Tx mode to stop Rx clock
|
89 | result = SPI_ReceiveData8(SPI2);
|
90 | return result;
|
91 | }
|
92 |
|
93 | int main(void)
|
94 | {
|
95 | uint8_t counter=0;
|
96 | //LED_init();
|
97 | //input_config();
|
98 | //CS_config2();
|
99 | CS_config1();
|
100 | SPI_Configure();
|
101 |
|
102 | GPIO_ResetBits(GPIOB, GPIO_Pin_11);
|
103 |
|
104 | while(1) {
|
105 |
|
106 | counter=send_and_read_byte(0x0F);
|
107 |
|
108 | }
|
109 |
|
110 | }
|