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_test_adxl345_dma.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 "libopencm3/stm32/f1/gpio.h"
23 #include "libopencm3/stm32/f1/nvic.h"
24 #include "libopencm3/stm32/exti.h"
25 
26 #include BOARD_CONFIG
27 #include "mcu.h"
28 #include "mcu_periph/sys_time.h"
29 #include "mcu_periph/uart.h"
31 #include "mcu_periph/spi.h"
32 
34 #include "led.h"
35 
36 #ifndef ADXL345_SLAVE_IDX
37 #define ADXL345_SLAVE_IDX SPI_SLAVE2
38 #endif
39 
40 #ifndef ADXL345_SPI_DEV
41 #define ADXL345_SPI_DEV spi2
42 #endif
43 
44 #define CONFIGURED 6
46 static volatile uint8_t acc_data_available = FALSE;
47 static volatile uint8_t foo = FALSE;
49 
50 static uint8_t dma_tx_buf[7];
51 static uint8_t dma_rx_buf[7];
52 
53 static void write_to_reg(uint8_t addr, uint8_t val);
54 static void adxl345_trans_cb(struct spi_transaction *trans);
55 static inline void init_adxl345_spi_trans( void );
56 static void read_data(void);
57 
58 static inline void main_init( void );
59 static inline void main_periodic_task( void );
60 static inline void main_event_task( void );
61 
62 void hw_init(void);
63 
64 int main(void) {
65  main_init();
66 
67  while(1) {
71  }
72 
73  return 0;
74 }
75 
76 static inline void main_init( void ) {
77  mcu_init();
78  hw_init();
81 }
82 
83 static inline void main_periodic_task( void ) {
84  RunOnceEvery(10,
85  {
86  DOWNLINK_SEND_ALIVE(DefaultChannel, DefaultDevice, 16, MD5SUM);
87  LED_PERIODIC();
88  });
89 
90  if (acc_status != CONFIGURED) {
91  /* set data rate to 800Hz */
93  /* enable data ready interrupt */
95  /* Enable full res and interrupt active low */
97  /* reads data once to bring interrupt line up */
98  /* switch to measurememnt mode */
100  read_data();
102  }
103  else {
104  read_data();
105  }
106 }
107 
108 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx+1]<<8) | _buf[_idx]))
109 
110 static inline void main_event_task( void ) {
111  if (foo) {
112  foo = FALSE;
113  RunOnceEvery(100, {LED_TOGGLE(3);});
114  }
120  int32_t iax = ax;
121  int32_t iay = ay;
122  int32_t iaz = az;
123  RunOnceEvery(10, {DOWNLINK_SEND_IMU_ACCEL_RAW(DefaultChannel, DefaultDevice, &iax, &iay, &iaz);});
124  }
125 }
126 
127 static void write_to_reg(uint8_t addr, uint8_t val) {
130  dma_tx_buf[0] = addr;
131  dma_tx_buf[1] = val;
133  // FIXME: no busy waiting! if really needed add a timeout!!!!
135 }
136 
137 static void read_data(void) {
140  dma_tx_buf[0] = (1<<7|1<<6|ADXL345_REG_DATA_X0);
142 }
143 
144 
145 static void adxl345_trans_cb( struct spi_transaction *trans ) {
147 }
148 
149 static inline void init_adxl345_spi_trans( void ) {
162 }
163 
164 void hw_init(void) {
165  /* configure external interrupt exti2 on PB2( accel int ) */
166  rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN | RCC_APB2ENR_AFIOEN);
167  gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO2);
168 
169  exti_select_source(EXTI2, GPIOB);
170  exti_set_trigger(EXTI2, EXTI_TRIGGER_FALLING);
171  exti_enable_request(EXTI2);
172 
173  nvic_set_priority(NVIC_EXTI2_IRQ, 0xF);
174  nvic_enable_irq(NVIC_EXTI2_IRQ);
175 }
176 
177 
178 void exti2_isr(void) {
179  /* clear EXTI */
180  exti_reset_request(EXTI2);
181 
182  LED_TOGGLE(2);
183 
184  foo = TRUE;
185 }
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
struct spi_transaction adxl345_spi_trans
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
void mcu_init(void)
Microcontroller peripherals initialization.
Definition: mcu.c:61
uint8_t input_length
number of data words to read
Definition: spi.h:145
static volatile uint8_t foo
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
CPHA = 1.
Definition: spi.h:69
int main(void)
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
CPOL = 1.
Definition: spi.h:78
static uint8_t dma_tx_buf[7]
#define ADXL345_REG_INT_ENABLE
Definition: adxl345_regs.h:38
#define Int16FromBuf(_buf, _idx)
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
static uint8_t dma_rx_buf[7]
#define FALSE
Definition: imu_chimu.h:141
enum SPITransactionStatus status
Definition: spi.h:156
Architecture independent SPI (Serial Peripheral Interface) API.
Register and address definitions for ADXL345 accelerometer.
static void init_adxl345_spi_trans(void)
static void main_periodic_task(void)
#define ADXL345_REG_BW_RATE
Definition: adxl345_regs.h:36
Definition: spi.h:84
static void main_init(void)
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
bool_t spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:469
Architecture independent timing functions.
static volatile uint8_t acc_data_available
uint16_t val[TCOUPLE_NB]
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
signed short int16_t
Definition: types.h:17
uint8_t output_length
number of data words to write
Definition: spi.h:146
void exti2_isr(void)
#define CONFIGURED
#define ADXL345_SLAVE_IDX
#define ADXL345_REG_DATA_FORMAT
Definition: adxl345_regs.h:39
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
signed long int32_t
Definition: types.h:19
#define TRUE
Definition: imu_chimu.h:144
#define LED_PERIODIC()
Definition: led_hw.h:8
Arch independent mcu ( Micro Controller Unit ) utilities.
unsigned char uint8_t
Definition: types.h:14
slave is selected before transaction and unselected after
Definition: spi.h:57
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
static void read_data(void)
static void adxl345_trans_cb(struct spi_transaction *trans)
#define ADXL345_REG_POWER_CTL
Definition: adxl345_regs.h:37
#define PERIODIC_FREQUENCY
Definition: imu_aspirin2.c:54
Definition: spi.h:119
arch independent LED (Light Emitting Diodes) API
static void write_to_reg(uint8_t addr, uint8_t val)
static void main_event_task(void)
#define ADXL345_SPI_DEV
SPI transaction structure.
Definition: spi.h:142
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
#define ADXL345_REG_DATA_X0
Definition: adxl345_regs.h:40
static uint8_t acc_status
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
void hw_init(void)