Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test_spi_slave.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 
28 #include BOARD_CONFIG
29 #include "mcu.h"
30 #include "mcu_periph/sys_time.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 static inline void main_spi_slave_init( void );
39 
40 
41 int main(void) {
42  main_init();
43 
44  while(1) {
48  }
49 
50  return 0;
51 }
52 
53 
54 static inline void main_init( void ) {
55  mcu_init();
58 }
59 
60 static inline void main_periodic_task( void ) {
61 #if 0
62  static uint8_t foo = FALSE;
63  if (foo)
64  GPIOC->BRR = GPIO_Pin_4;
65  else
66  GPIOC->BSRR = GPIO_Pin_4;
67  foo = !foo;
68 #endif
69  RunOnceEvery(10, {DOWNLINK_SEND_BOOT(DefaultChannel, DefaultDevice, &sys_time.nb_sec);});
70  LED_PERIODIC();
71 }
72 
73 static inline void main_event_task( void ) {
74 
75 }
76 
77 
78 static inline void main_spi_slave_init( void ) {
79 
80  /* Enable SPI1 Periph clock -------------------------------------------------*/
81  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
82 
83  /* Configure GPIOs: SCK, MISO and MOSI -------------------------------------*/
84  GPIO_InitTypeDef GPIO_InitStructure;
85  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
86  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
87  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
88  GPIO_Init(GPIOA, &GPIO_InitStructure);
89 
90  /* Configure the nested vectored interrupt controller. ----------------------*/
91  NVIC_InitTypeDef NVIC_InitStructure;
92  /* 1 bit for pre-emption priority, 3 bits for subpriority */
93  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
94  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
95  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
96  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
97  NVIC_Init(&NVIC_InitStructure);
98 
99  /* SPI_SLAVE configuration --------------------------------------------------*/
100  SPI_InitTypeDef SPI_InitStructure;
101  // SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
102  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
103  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
104  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
105  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
106  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
107  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
108  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
109  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
110  SPI_InitStructure.SPI_CRCPolynomial = 7;
111  SPI_Init(SPI1, &SPI_InitStructure);
112 
113  /* Enable SPI_SLAVE RXNE interrupt */
114  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
115  /* Enable SPI_SLAVE */
116  SPI_Cmd(SPI1, ENABLE);
117 
118 #if 0
119  /* configure DATA_READY on PC4 */
120  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
121  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
122  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
123  GPIO_Init(GPIOC, &GPIO_InitStructure);
124 #endif
125 }
126 
127 void spi1_irq_handler(void) {
128  uint8_t foo = SPI_I2S_ReceiveData(SPI1);
129  static uint8_t cnt = 0;
130  SPI_I2S_SendData(SPI1, cnt);
131  cnt++;
132  LED_TOGGLE(3);
133  DOWNLINK_SEND_DEBUG_MCU_LINK(DefaultChannel, DefaultDevice, &foo, &foo, &cnt);
134 
135 
136 }
137 
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
#define FALSE
Definition: imu_chimu.h:141
static void main_init(void)
int main(void)
Architecture independent timing functions.
uint16_t foo
Definition: main_demo5.c:54
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
static void main_spi_slave_init(void)
void spi1_irq_handler(void)
arch independent LED (Light Emitting Diodes) API
#define SPI1
Definition: LPC21xx.h:210
static void main_event_task(void)
static void main_periodic_task(void)
void mcu_init(void)
Definition: mcu.c:57