Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
lisa_overo_link_arch.c
Go to the documentation of this file.
1 #include "lisa/lisa_overo_link.h"
2 
3 #include <stm32/rcc.h>
4 #include <stm32/gpio.h>
5 #include <stm32/flash.h>
6 #include <stm32/misc.h>
7 #include <stm32/spi.h>
8 #include <stm32/dma.h>
9 
10 void dma1_c2_irq_handler(void);
11 
13 
14  /* Enable SPI_1 DMA clock ---------------------------------------------------*/
15  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
16  /* Enable SPI1 Periph clock -------------------------------------------------*/
17  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
18  /* Configure GPIOs: NSS, SCK, MISO and MOSI --------------------------------*/
19  GPIO_InitTypeDef GPIO_InitStructure;
20  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
21  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
22  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
23  GPIO_Init(GPIOA, &GPIO_InitStructure);
24  /* SPI_SLAVE configuration --------------------------------------------------*/
25  SPI_InitTypeDef SPI_InitStructure;
26  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
27  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
28  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
29  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
30  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
31  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
32  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
33  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
34  SPI_InitStructure.SPI_CRCPolynomial = 0x31;
35  SPI_Init(SPI1, &SPI_InitStructure);
36 
37  SPI_CalculateCRC(SPI1, ENABLE);
38  /* Enable SPI_SLAVE */
39  // SPI_Cmd(SPI1, DISABLE); why that ?
40  SPI_Cmd(SPI1, ENABLE);
41 
42  /* Configure DMA1 channel2 IRQ Channel */
43  NVIC_InitTypeDef NVIC_init_struct = {
44  .NVIC_IRQChannel = DMA1_Channel2_IRQn,
45  .NVIC_IRQChannelPreemptionPriority = 0,
46  .NVIC_IRQChannelSubPriority = 0,
47  .NVIC_IRQChannelCmd = ENABLE
48  };
49  NVIC_Init(&NVIC_init_struct);
50 
51  /* setup DMA for first transfert */
53 
54 }
55 
56 
58 
59  /* Disable SPI module */
60  SPI_Cmd(SPI1, DISABLE);
61 
62  /* Make sure RX register is empty */
63  uint8_t foo __attribute__ ((unused)) = SPI1->DR;
64  /* Read status register to clear OVR, UDR, MODF flags */
65  foo = SPI1->SR;
66  /* clear possible CRC_ERR flag */
67  SPI1->SR = (uint16_t)~SPI_FLAG_CRCERR;
68 
69  /* SPI_SLAVE_Rx_DMA_Channel configuration ------------------------------------*/
70  DMA_DeInit(DMA1_Channel2);
71  const DMA_InitTypeDef DMA_InitStructure_rx = {
72  .DMA_PeripheralBaseAddr = (uint32_t)(SPI1_BASE+0x0C),
73  .DMA_MemoryBaseAddr = (uint32_t)overo_link.down.array,
74  .DMA_DIR = DMA_DIR_PeripheralSRC,
75  .DMA_BufferSize = sizeof(union AutopilotMessage),
76  .DMA_PeripheralInc = DMA_PeripheralInc_Disable,
77  .DMA_MemoryInc = DMA_MemoryInc_Enable,
78  .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
79  .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
80  .DMA_Mode = DMA_Mode_Normal,
81  .DMA_Priority = DMA_Priority_VeryHigh,
82  .DMA_M2M = DMA_M2M_Disable
83  };
84  DMA_Init(DMA1_Channel2, (DMA_InitTypeDef*)&DMA_InitStructure_rx);
85 
86  /* SPI_SLAVE_Tx_DMA_Channel configuration ------------------------------------*/
87  DMA_DeInit(DMA1_Channel3);
88  const DMA_InitTypeDef DMA_InitStructure_tx = {
89  .DMA_PeripheralBaseAddr = (uint32_t)(SPI1_BASE+0x0C),
90  .DMA_MemoryBaseAddr = (uint32_t)overo_link.up.array,
91  .DMA_DIR = DMA_DIR_PeripheralDST,
92  .DMA_BufferSize = sizeof(union AutopilotMessage),
93  .DMA_PeripheralInc = DMA_PeripheralInc_Disable,
94  .DMA_MemoryInc = DMA_MemoryInc_Enable,
95  .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
96  .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
97  .DMA_Mode = DMA_Mode_Normal,
98  .DMA_Priority = DMA_Priority_Medium,
99  .DMA_M2M = DMA_M2M_Disable
100  };
101  DMA_Init(DMA1_Channel3, (DMA_InitTypeDef*)&DMA_InitStructure_tx);
102 
103  /* Enable SPI_1 Rx request */
104  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
105  /* Enable DMA1 Channel2 */
106  DMA_Cmd(DMA1_Channel2, ENABLE);
107  /* Enable SPI_1 Tx request */
108  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
109  /* Enable DMA1 Channel3 */
110  DMA_Cmd(DMA1_Channel3, ENABLE);
111 
112  /* Enable DMA1 Channel2 Transfer Complete interrupt */
113  DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE);
114 
115  /* resets CRC module */
116  SPI_CalculateCRC(SPI1, DISABLE);
117  SPI_CalculateCRC(SPI1, ENABLE);
118 
119  /* enable SPI */
120  SPI_Cmd(SPI1, ENABLE);
121 }
122 
124 
125  DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, DISABLE);
126 
128 
129 }
unsigned short uint16_t
Definition: types.h:16
unsigned long uint32_t
Definition: types.h:18
uint16_t foo
Definition: main_demo5.c:54
unsigned char uint8_t
Definition: types.h:14
#define SPI1
Definition: LPC21xx.h:210