Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
imu_crista_arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-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 "subsystems/imu.h"
23 
24 #include <stm32/gpio.h>
25 #include <stm32/rcc.h>
26 #include <stm32/spi.h>
27 #include <stm32/misc.h>
28 #include <stm32/dma.h>
29 
30 static volatile uint8_t channel;
31 static uint8_t buf_in[4];
32 static uint8_t buf_out[4];
33 
34 #define POWER_MODE (1 << 1 | 1)
35 #define SGL_DIF 1 // Single ended
36 
37 #define ADS8344Unselect() GPIOB->BSRR = GPIO_Pin_12
38 #define ADS8344Select() GPIOB->BRR = GPIO_Pin_12
39 
40 extern void dma1_c4_irq_handler(void);
41 static void ADS8344_read_channel(void);
42 
44 {
45 
46  channel = 0;
47  /* Enable SPI2 Periph clock -------------------------------------------------*/
48  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
49  /* Enable SPI_2 DMA clock ---------------------------------------------------*/
50  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
51  /* Enable PORTB GPIO clock --------------------------------------------------*/
52  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
53  /* Configure GPIOs: SCK, MISO and MOSI -------------------------------------*/
54  GPIO_InitTypeDef GPIO_InitStructure;
55  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
56  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
57  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
58  GPIO_Init(GPIOB, &GPIO_InitStructure);
59  /* set slave select as output and assert it ( on PB12) */
60  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
61  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
62  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
63  GPIO_Init(GPIOB, &GPIO_InitStructure);
65  /* configure SPI after enabling it*/
66  SPI_Cmd(SPI2, ENABLE);
67  SPI_InitTypeDef SPI_InitStructure;
68  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
69  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
70  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
71  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
72  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
73  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
74  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
75  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
76  SPI_InitStructure.SPI_CRCPolynomial = 7;
77  SPI_Init(SPI2, &SPI_InitStructure);
78 
79  /* Enable DMA1 channel4 IRQ Channel */
80  NVIC_InitTypeDef NVIC_init_struct = {
81  .NVIC_IRQChannel = DMA1_Channel4_IRQn,
82  .NVIC_IRQChannelPreemptionPriority = 0,
83  .NVIC_IRQChannelSubPriority = 0,
84  .NVIC_IRQChannelCmd = ENABLE
85  };
86  NVIC_Init(&NVIC_init_struct);
87 
88 }
89 
90 
91 void ADS8344_start(void)
92 {
93 
94  ADS8344Select();
95  channel = 0;
97 
98 }
99 
100 static void ADS8344_read_channel(void)
101 {
102 
103  // control byte
104  buf_out[0] = 1 << 7 | channel << 4 | SGL_DIF << 2 | POWER_MODE;
105 
106  /* trigger 4 bytes read */
107  /* SPI2_Rx_DMA_Channel configuration ------------------------------------*/
108  DMA_DeInit(DMA1_Channel4);
109  DMA_InitTypeDef DMA_initStructure_4 = {
110  .DMA_PeripheralBaseAddr = (uint32_t)(SPI2_BASE + 0x0C),
111  .DMA_MemoryBaseAddr = (uint32_t)buf_in,
112  .DMA_DIR = DMA_DIR_PeripheralSRC,
113  .DMA_BufferSize = 4,
114  .DMA_PeripheralInc = DMA_PeripheralInc_Disable,
115  .DMA_MemoryInc = DMA_MemoryInc_Enable,
116  .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
117  .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
118  .DMA_Mode = DMA_Mode_Normal,
119  .DMA_Priority = DMA_Priority_VeryHigh,
120  .DMA_M2M = DMA_M2M_Disable
121  };
122  DMA_Init(DMA1_Channel4, &DMA_initStructure_4);
123 
124  /* SPI2_Tx_DMA_Channel configuration ------------------------------------*/
125  DMA_DeInit(DMA1_Channel5);
126  DMA_InitTypeDef DMA_initStructure_5 = {
127  .DMA_PeripheralBaseAddr = (uint32_t)(SPI2_BASE + 0x0C),
128  .DMA_MemoryBaseAddr = (uint32_t)buf_out,
129  .DMA_DIR = DMA_DIR_PeripheralDST,
130  .DMA_BufferSize = 4,
131  .DMA_PeripheralInc = DMA_PeripheralInc_Disable,
132  .DMA_MemoryInc = DMA_MemoryInc_Enable,
133  .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte,
134  .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte,
135  .DMA_Mode = DMA_Mode_Normal,
136  .DMA_Priority = DMA_Priority_Medium,
137  .DMA_M2M = DMA_M2M_Disable
138  };
139  DMA_Init(DMA1_Channel5, &DMA_initStructure_5);
140 
141  /* Enable SPI_2 Rx request */
142  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, ENABLE);
143  /* Enable DMA1 Channel4 */
144  DMA_Cmd(DMA1_Channel4, ENABLE);
145 
146  /* Enable SPI_2 Tx request */
147  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, ENABLE);
148  /* Enable DMA1 Channel5 */
149  DMA_Cmd(DMA1_Channel5, ENABLE);
150 
151  /* Enable DMA1 Channel4 Transfer Complete interrupt */
152  DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE);
153 
154 }
155 
156 
158 {
159 
160  ADS8344_values[channel] = (buf_in[1] << 8 | buf_in[2]) << 1 | buf_in[3] >> 7;
161  channel++;
162  if (channel > 6) {
163  ADS8344_available = true;
164  ADS8344Unselect();
165  DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, DISABLE);
166  /* Disable SPI_2 Rx and TX request */
167  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Rx, DISABLE);
168  SPI_I2S_DMACmd(SPI2, SPI_I2S_DMAReq_Tx, DISABLE);
169  /* Disable DMA1 Channel4 and 5 */
170  DMA_Cmd(DMA1_Channel4, DISABLE);
171  DMA_Cmd(DMA1_Channel5, DISABLE);
172  } else {
174  }
175 }
#define SGL_DIF
#define POWER_MODE
void dma1_c4_irq_handler(void)
uint16_t ADS8344_values[NB_CHANNELS]
Definition: ADS8344.c:39
#define GPIOB
Definition: gpio_arch.h:35
bool ADS8344_available
Definition: ADS8344.c:38
static uint8_t buf_in[4]
#define ADS8344Unselect()
unsigned long uint32_t
Definition: types.h:18
static volatile uint8_t channel
Inertial Measurement Unit interface.
void ADS8344_start(void)
unsigned char uint8_t
Definition: types.h:14
void imu_crista_arch_init(void)
#define ADS8344Select()
static void ADS8344_read_channel(void)
static uint8_t buf_out[4]