Paparazzi UAS  v5.15_devel-88-gb3ad7fe
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
struct spi_transaction req_trans
Definition: ms2100.h:46
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
#define MS2100_DIVISOR
Definition: ms2100.c:40
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
uint16_t output_length
number of data words to write
Definition: spi.h:146
void ms2100_read(struct Ms2100 *ms)
send request to read next axis
Definition: ms2100.c:96
#define Int16FromBuf(_buf, _idx)
Definition: ms2100.c:103
Driver for the ms2100 magnetic sensor from PNI.
volatile uint8_t req_buf[1]
SPI buffer for the command byte.
Definition: ms2100.h:48
void ms2100_reset_cb(struct spi_transaction *t)
Reset callback.
Definition: ms2100_arch.c:71
void ms2100_event(struct Ms2100 *ms)
Definition: ms2100.c:105
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:505
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
volatile uint8_t read_buf[2]
SPI buffer for reading a single axis.
Definition: ms2100.h:49
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
Architecture independent SPI (Serial Peripheral Interface) API.
Definition: ms2100.h:44
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
SPI peripheral structure.
Definition: spi.h:168
Definition: spi.h:84
struct spi_transaction read_trans
Definition: ms2100.h:47
void ms2100_arch_init(void)
Definition: ms2100_arch.c:38
volatile uint8_t cur_axe
Definition: ms2100.h:51
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
signed short int16_t
Definition: types.h:17
struct Ms2100 ms2100
Definition: ms2100.c:44
CPOL = 0.
Definition: spi.h:77
uint16_t input_length
number of data words to read
Definition: spi.h:145
#define INT_VECT3_ZERO(_v)
union Ms2100::@330 data
unsigned char uint8_t
Definition: types.h:14
struct spi_periph * spi_p
Definition: ms2100.h:45
CPHA = 0.
Definition: spi.h:68
slave is selected before transaction and unselected after
Definition: spi.h:57
void ms2100_init(struct Ms2100 *ms, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ms2100.c:47
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
Definition: spi.h:119
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
enum Ms2100Status status
Definition: ms2100.h:50
enum SPITransactionStatus status
Definition: spi.h:156