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_slave.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 
26 #include BOARD_CONFIG
27 #include "mcu.h"
28 #include "mcu_periph/sys_time.h"
30 #include "led.h"
31 
32 static inline void main_init( void );
33 static inline void main_periodic_task( void );
34 static inline void main_event_task( void );
35 
36 static inline void main_spi_slave_init( void );
37 
38 
39 int main(void) {
40  main_init();
41 
42  while(1) {
46  }
47 
48  return 0;
49 }
50 
51 
52 static inline void main_init( void ) {
53  mcu_init();
56 }
57 
58 static inline void main_periodic_task( void ) {
59 #if 0
60  static uint8_t foo = FALSE;
61  if (foo)
62  GPIOC->BRR = GPIO_Pin_4;
63  else
64  GPIOC->BSRR = GPIO_Pin_4;
65  foo = !foo;
66 #endif
67  RunOnceEvery(10, {DOWNLINK_SEND_BOOT(DefaultChannel, DefaultDevice, &sys_time.nb_sec);});
68  LED_PERIODIC();
69 }
70 
71 static inline void main_event_task( void ) {
72 
73 }
74 
75 
76 static inline void main_spi_slave_init( void ) {
77 
78  /* Enable SPI1 Periph clock -------------------------------------------------*/
79  RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
80 
81  /* Configure GPIOs: SCK, MISO and MOSI -------------------------------------*/
82  GPIO_InitTypeDef GPIO_InitStructure;
83  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
84  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
85  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
86  GPIO_Init(GPIOA, &GPIO_InitStructure);
87 
88  /* Configure the nested vectored interrupt controller. ----------------------*/
89  NVIC_InitTypeDef NVIC_InitStructure;
90  /* 1 bit for pre-emption priority, 3 bits for subpriority */
91  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
92  NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn;
93  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
94  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
95  NVIC_Init(&NVIC_InitStructure);
96 
97  /* SPI_SLAVE configuration --------------------------------------------------*/
98  SPI_InitTypeDef SPI_InitStructure;
99  // SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx;
100  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
101  SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
102  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
103  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
104  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
105  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
106  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
107  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
108  SPI_InitStructure.SPI_CRCPolynomial = 7;
109  SPI_Init(SPI1, &SPI_InitStructure);
110 
111  /* Enable SPI_SLAVE RXNE interrupt */
112  SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE);
113  /* Enable SPI_SLAVE */
114  SPI_Cmd(SPI1, ENABLE);
115 
116 #if 0
117  /* configure DATA_READY on PC4 */
118  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
119  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
120  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
121  GPIO_Init(GPIOC, &GPIO_InitStructure);
122 #endif
123 }
124 
125 void spi1_irq_handler(void) {
126  uint8_t foo = SPI_I2S_ReceiveData(SPI1);
127  static uint8_t cnt = 0;
128  SPI_I2S_SendData(SPI1, cnt);
129  cnt++;
130  LED_TOGGLE(3);
131  DOWNLINK_SEND_DEBUG_MCU_LINK(DefaultChannel, DefaultDevice, &foo, &foo, &cnt);
132 
133 
134 }
135 
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:61
#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: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
static void main_spi_slave_init(void)
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_event_task(void)
static void main_periodic_task(void)