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
test_spi_slave2.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
22 #include <stm32/flash.h>
23 #include <stm32/misc.h>
24 #include <stm32/spi.h>
25 #include <stm32/dma.h>
26 
27 #include BOARD_CONFIG
28 #include "mcu.h"
29 #include "mcu_periph/sys_time.h"
31 #include "fms/fms_autopilot_msg.h"
32 #include "led.h"
33 
34 static inline void main_init( void );
35 static inline void main_periodic_task( void );
36 static inline void main_event_task( void );
37 
38 #define USE_DMA 1
39 
40 #define BufferSize sizeof(struct AutopilotMessageFoo)
43 static inline void main_spi_slave_init(void);
44 static void main_setup_dma(void);
45 static void main_enable_dma(void);
46 
47 int main(void) {
48  main_init();
49 
50  while(1) {
54  }
55 
56  return 0;
57 }
58 
59 
60 static inline void main_init( void ) {
61  mcu_init();
64 }
65 
66 static inline void main_periodic_task( void ) {
67  RunOnceEvery(10,
68  {
69  DOWNLINK_SEND_BOOT(DefaultChannel, DefaultDevice, &sys_time.nb_sec);
70  LED_PERIODIC();
71  });
72 
73 }
74 
75 static inline void main_event_task( void ) {
76 
77 #ifdef USE_DMA
78  if (DMA_GetFlagStatus(DMA1_FLAG_TC2)) {
79  LED_TOGGLE(3);
80  RunOnceEvery(10, {DOWNLINK_SEND_DEBUG_MCU_LINK(DefaultChannel, DefaultDevice, &SPI_SLAVE_Buffer_Rx[0],
85  }
86 #endif
87 
88 }
89 
90 
91 static inline void main_spi_slave_init( void ) {
92 
93 #ifdef USE_DMA
94  /* Enable SPI_1 DMA clock ---------------------------------------------------*/
95  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
96 #endif
97 
98  /* Enable SPI1 Periph clock -------------------------------------------------*/
99  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
100 
101  /* Configure GPIOs: NSS, SCK, MISO and MOSI --------------------------------*/
102  GPIO_InitTypeDef GPIO_InitStructure;
103  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
104  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
105  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
106  GPIO_Init(GPIOA, &GPIO_InitStructure);
107 
108 
109  /* SPI_SLAVE configuration --------------------------------------------------*/
110  SPI_InitTypeDef SPI_InitStructure;
111  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
112  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
113  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
114  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
115  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
116  SPI_InitStructure.SPI_NSS = SPI_NSS_Hard;
117  // SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
118  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
119  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
120  SPI_InitStructure.SPI_CRCPolynomial = 7;
121  SPI_Init(SPI1, &SPI_InitStructure);
122 
123  /* Enable SPI_SLAVE */
124  SPI_Cmd(SPI1, ENABLE);
125 
126 #ifdef USE_DMA
127  /* SPI_SLAVE_Rx_DMA_Channel configuration ------------------------------------*/
128  main_setup_dma();
129  main_enable_dma();
130 #endif
131 
132 
133 }
134 
135 
136 static void main_setup_dma(void) {
137  /* SPI_SLAVE_Rx_DMA_Channel configuration ------------------------------------*/
138  DMA_InitTypeDef DMA_InitStructure;
139  DMA_DeInit(DMA1_Channel2);
140  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(SPI1_BASE+0x0C);
141  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Rx;
142  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
143  DMA_InitStructure.DMA_BufferSize = BufferSize;
144  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
145  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
146  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
147  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
148  DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
149  DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh;
150  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
151  DMA_Init(DMA1_Channel2, &DMA_InitStructure);
152  /* SPI_SLAVE_Tx_DMA_Channel configuration ------------------------------------*/
153  DMA_DeInit(DMA1_Channel3);
154  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(SPI1_BASE+0x0C);
155  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Tx;
156  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
157  DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;
158  DMA_Init(DMA1_Channel3, &DMA_InitStructure);
159 }
160 
161 static void main_enable_dma(void) {
162  /* Enable SPI_1 Rx request */
163  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Rx, ENABLE);
164  /* Enable DMA1 Channel2 */
165  DMA_Cmd(DMA1_Channel2, ENABLE);
166  /* Enable SPI_1 Rx request */
167  SPI_I2S_DMACmd(SPI1, SPI_I2S_DMAReq_Tx, ENABLE);
168  /* Enable DMA1 Channel3 */
169  DMA_Cmd(DMA1_Channel3, ENABLE);
170 }
171 
172 
173 
174 #if 0
175 void spi1_irq_handler(void) {
176  uint8_t foo = SPI_I2S_ReceiveData(SPI1);
177  static uint8_t cnt = 0;
178  SPI_I2S_SendData(SPI1, cnt);
179  cnt++;
180  LED_TOGGLE(4);
181 }
182 #endif
183 
184 
186  LED_TOGGLE(5);
187 }
static void main_spi_slave_init(void)
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:61
static void main_event_task(void)
static void main_enable_dma(void)
static void main_setup_dma(void)
uint8_t SPI_SLAVE_Buffer_Rx[BufferSize]
int main(void)
uint8_t SPI_SLAVE_Buffer_Tx[BufferSize]
Architecture independent timing functions.
static void main_periodic_task(void)
unsigned long uint32_t
Definition: types.h:18
uint16_t foo
Definition: main_demo5.c:54
#define BufferSize
int sys_time_register_timer(float duration, sys_time_cb cb)
Register a new system timer.
Definition: sys_time.c:37
#define LED_TOGGLE(i)
Definition: led_hw.h:30
static bool_t sys_time_check_and_ack_timer(tid_t id)
Check if timer has elapsed.
Definition: sys_time.h:111
#define LED_PERIODIC()
Definition: led_hw.h:8
Arch independent mcu ( Micro Controller Unit ) utilities.
unsigned char uint8_t
Definition: types.h:14
volatile uint32_t nb_sec
full seconds since startup
Definition: sys_time.h:69
void spi1_irq_handler(void)
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
arch independent LED (Light Emitting Diodes) API
#define SPI1
Definition: LPC21xx.h:210
static void main_init(void)
void spi1_dma_irq_handler(void)