1 | /********************************************************************************************************************************************************/
|
2 | void GPIO_Configuration(void)
|
3 | {
|
4 | GPIO_InitTypeDef GPIO_InitStructure;
|
5 | /* Configure SPI2 pins: SCK, MISO and MOSI -------------------------------*/
|
6 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
|
7 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
8 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
9 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
10 | }
|
11 |
|
12 | /*******************************************************************************
|
13 | *******************************************************************************/
|
14 | static void RFM12_SPI_Init(void)
|
15 | {
|
16 | SPI_InitTypeDef SPI_InitStructure;
|
17 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
|
18 | /* DISABLE SPI1 */
|
19 | SPI_Cmd(SPI1, DISABLE);
|
20 |
|
21 | /* SPI1 Config -------------------------------------------------------------*/
|
22 | SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
|
23 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
24 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
|
25 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
26 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
27 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
28 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
29 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
30 | SPI_InitStructure.SPI_CRCPolynomial = 7;
|
31 | SPI_Init(SPI2, &SPI_InitStructure);
|
32 |
|
33 | /* Enable SPI2 */
|
34 | SPI_Cmd(SPI2, ENABLE);
|
35 | }
|
36 |
|
37 | /*******************************************************************************
|
38 | *******************************************************************************/
|
39 | unsigned short rf12_trans(unsigned short wert){
|
40 |
|
41 | // Wait for SPI1 Tx buffer empty
|
42 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
|
43 | // Send SPI1 data
|
44 | SPI_I2S_SendData(SPI2,wert);
|
45 | // Wait for SPI1 data reception
|
46 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
|
47 | // Read SPI1 received data
|
48 | return SPI_I2S_ReceiveData(SPI2);
|
49 |
|
50 | }
|
51 |
|
52 | /******************************************************************
|
53 |
|
54 | *****************************************************************/
|
55 | void rf12_init(void)
|
56 | {
|
57 | /* GPIO configuration ----------------------------------------------------*/
|
58 | GPIO_Configuration();
|
59 | RFM12_SPI_Init();
|
60 | ...
|
61 | }
|
62 |
|
63 | /******************************************************************
|
64 | *****************************************************************/
|
65 | static void rf12_ready(void)
|
66 | {
|
67 | RFM12_CS(0);
|
68 | //while ((GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14))); // wait until FIFO ready
|
69 | while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
|
70 | RFM12_CS(1);
|
71 | }
|
72 |
|
73 | #define RFM12_CS(a) if (a) \
|
74 | GPIO_SetBits(GPIOB,GPIO_Pin_12);\
|
75 | else \
|
76 | GPIO_ResetBits(GPIOB,GPIO_Pin_12)
|