Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
ms2100.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2012 Gautier Hattenberger
4  *
5  * This file is part of paparazzi.
6  *
7  * paparazzi is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2, or (at your option)
10  * any later version.
11  *
12  * paparazzi is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with paparazzi; see the file COPYING. If not, write to
19  * the Free Software Foundation, 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22 
28 #include "peripherals/ms2100.h"
29 #include "mcu_periph/spi.h"
30 
31 #include <stdlib.h> // for abs
32 
33 
34 #define MS2100_DIVISOR_128 2
35 #define MS2100_DIVISOR_256 3
36 #define MS2100_DIVISOR_512 4
37 #define MS2100_DIVISOR_1024 5
38 
39 #ifndef MS2100_DIVISOR
40 #define MS2100_DIVISOR MS2100_DIVISOR_1024
41 #endif
42 
43 // keep stupid global variable for now...
44 struct Ms2100 ms2100;
45 
46 
47 void ms2100_init(struct Ms2100 *ms, struct spi_periph *spi_p, uint8_t slave_idx)
48 {
49 
50  /* set spi_peripheral */
51  ms->spi_p = spi_p;
52 
53  /* configure spi transaction for the request */
56  ms->req_trans.dss = SPIDss8bit;
58  ms->req_trans.cdiv = SPIDiv64;
59 
60  ms->req_trans.slave_idx = slave_idx;
62  ms->req_trans.output_buf = ms->req_buf;
63  ms->req_trans.output_length = 1;
64  ms->req_trans.input_buf = NULL;
65  ms->req_trans.input_length = 0;
66  // ms2100 has to be reset before each measurement: implemented in ms2100_arch.c
69 
70  /* configure spi transaction to read the result */
75  ms->read_trans.cdiv = SPIDiv64;
76 
77  ms->read_trans.slave_idx = slave_idx;
79  ms->read_trans.output_buf = NULL;
80  ms->read_trans.output_length = 0;
81  ms->read_trans.input_buf = ms->read_buf;
82  ms->read_trans.input_length = 2;
83  ms->read_trans.before_cb = NULL;
84  ms->read_trans.after_cb = NULL;
86 
88 
89  INT_VECT3_ZERO(ms->data.vect);
90  ms->cur_axe = 0;
91 
92  ms->status = MS2100_IDLE;
93 }
94 
96 void ms2100_read(struct Ms2100 *ms)
97 {
98  ms->req_buf[0] = (ms->cur_axe + 1) << 0 | MS2100_DIVISOR << 4;
99  spi_submit(ms->spi_p, &(ms->req_trans));
101 }
102 
103 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
104 
105 void ms2100_event(struct Ms2100 *ms)
106 {
107  // handle request transaction
108  if (ms->req_trans.status == SPITransDone) {
109  if (ms->status == MS2100_GOT_EOC) {
110  // eoc occurs, submit reading req
111  spi_submit(ms->spi_p, &(ms->read_trans));
113  }
114  } else if (ms->req_trans.status == SPITransSuccess) {
116  } else if (ms->req_trans.status == SPITransFailed) {
117  ms->status = MS2100_IDLE;
118  ms->cur_axe = 0;
120  }
121 
122  // handle reading transaction
123  if (ms->read_trans.status == SPITransSuccess) {
124  if (ms->status == MS2100_READING_RES) {
125  // store value
126  int16_t new_val = Int16FromBuf(ms->read_buf, 0);
127  // what is this check about?
128  if (abs(new_val) < 2000) {
129  ms->data.value[ms->cur_axe] = new_val;
130  }
131  ms->cur_axe++;
132  if (ms->cur_axe > 2) {
133  ms->cur_axe = 0;
135  } else {
136  ms->status = MS2100_IDLE;
137  }
139  }
140  } else if (ms->read_trans.status == SPITransFailed) {
141  ms->status = MS2100_IDLE;
142  ms->cur_axe = 0;
144  }
145 }
146 
Ms2100::cur_axe
volatile uint8_t cur_axe
Definition: ms2100.h:51
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
ms2100_read
void ms2100_read(struct Ms2100 *ms)
send request to read next axis
Definition: ms2100.c:96
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
spi.h
MS2100_READING_RES
@ MS2100_READING_RES
Definition: ms2100.h:40
MS2100_DIVISOR
#define MS2100_DIVISOR
Definition: ms2100.c:40
MS2100_SENDING_REQ
@ MS2100_SENDING_REQ
Definition: ms2100.h:38
ms2100.h
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: ms2100.c:103
MS2100_IDLE
@ MS2100_IDLE
Definition: ms2100.h:37
Ms2100::req_buf
volatile uint8_t req_buf[1]
SPI buffer for the command byte.
Definition: ms2100.h:48
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
ms2100
struct Ms2100 ms2100
Definition: ms2100.c:44
spi_transaction::output_buf
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
spi_periph
SPI peripheral structure.
Definition: spi.h:174
spi_transaction::select
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
SPICphaEdge1
@ SPICphaEdge1
CPHA = 0.
Definition: spi.h:74
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
SPICpolIdleLow
@ SPICpolIdleLow
CPOL = 0.
Definition: spi.h:83
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
spi_transaction::after_cb
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
spi_submit
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:511
Ms2100::status
enum Ms2100Status status
Definition: ms2100.h:50
SPITransFailed
@ SPITransFailed
Definition: spi.h:100
MS2100_DATA_AVAILABLE
@ MS2100_DATA_AVAILABLE
Definition: ms2100.h:41
int16_t
signed short int16_t
Definition: types.h:17
Ms2100::spi_p
struct spi_periph * spi_p
Definition: ms2100.h:45
uint8_t
unsigned char uint8_t
Definition: types.h:14
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
SPIDiv64
@ SPIDiv64
Definition: spi.h:125
Ms2100::data
union Ms2100::@339 data
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
ms2100_arch_init
void ms2100_arch_init(void)
Definition: ms2100_arch.c:38
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
SPITransDone
@ SPITransDone
Definition: spi.h:101
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
Ms2100::read_trans
struct spi_transaction read_trans
Definition: ms2100.h:47
Ms2100::read_buf
volatile uint8_t read_buf[2]
SPI buffer for reading a single axis.
Definition: ms2100.h:49
INT_VECT3_ZERO
#define INT_VECT3_ZERO(_v)
Definition: pprz_algebra_int.h:287
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
Ms2100
Definition: ms2100.h:44
ms2100_reset_cb
void ms2100_reset_cb(struct spi_transaction *t)
Reset callback.
Definition: ms2100_arch.c:71
ms2100_init
void ms2100_init(struct Ms2100 *ms, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ms2100.c:47
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
ms2100_event
void ms2100_event(struct Ms2100 *ms)
Definition: ms2100.c:105
MS2100_GOT_EOC
@ MS2100_GOT_EOC
Definition: ms2100.h:39
Ms2100::req_trans
struct spi_transaction req_trans
Definition: ms2100.h:46