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