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
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  /* set spi_peripheral */
50  ms->spi_p = spi_p;
51 
52  /* configure spi transaction for the request */
55  ms->req_trans.dss = SPIDss8bit;
57  ms->req_trans.cdiv = SPIDiv64;
58 
59  ms->req_trans.slave_idx = slave_idx;
61  ms->req_trans.output_buf = ms->req_buf;
62  ms->req_trans.output_length = 1;
63  ms->req_trans.input_buf = NULL;
64  ms->req_trans.input_length = 0;
65  // ms2100 has to be reset before each measurement: implemented in ms2100_arch.c
68 
69  /* configure spi transaction to read the result */
74  ms->read_trans.cdiv = SPIDiv64;
75 
76  ms->read_trans.slave_idx = slave_idx;
78  ms->read_trans.output_buf = NULL;
79  ms->read_trans.output_length = 0;
80  ms->read_trans.input_buf = ms->read_buf;
81  ms->read_trans.input_length = 2;
82  ms->read_trans.before_cb = NULL;
83  ms->read_trans.after_cb = NULL;
85 
87 
89  ms->cur_axe = 0;
90 
91  ms->status = MS2100_IDLE;
92 }
93 
95 void ms2100_read(struct Ms2100 *ms) {
96  ms->req_buf[0] = (ms->cur_axe+1) << 0 | MS2100_DIVISOR << 4;
97  spi_submit(ms->spi_p, &(ms->req_trans));
99 }
100 
101 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
102 
103 void ms2100_event(struct Ms2100 *ms) {
104  // handle request transaction
105  if (ms->req_trans.status == SPITransDone) {
106  if (ms->status == MS2100_GOT_EOC) {
107  // eoc occurs, submit reading req
108  spi_submit(ms->spi_p, &(ms->read_trans));
110  }
111  }
112  else if (ms->req_trans.status == SPITransSuccess) {
114  }
115  else if (ms->req_trans.status == SPITransFailed) {
116  ms->status = MS2100_IDLE;
117  ms->cur_axe = 0;
119  }
120 
121  // handle reading transaction
122  if (ms->read_trans.status == SPITransSuccess) {
123  if (ms->status == MS2100_READING_RES) {
124  // store value
125  int16_t new_val = Int16FromBuf(ms->read_buf,0);
126  // what is this check about?
127  if (abs(new_val) < 2000) {
128  ms->data.value[ms->cur_axe] = new_val;
129  }
130  ms->cur_axe++;
131  if (ms->cur_axe > 2) {
132  ms->cur_axe = 0;
134  }
135  else {
136  ms->status = MS2100_IDLE;
137  }
139  }
140  }
141  else if (ms->read_trans.status == SPITransFailed) {
142  ms->status = MS2100_IDLE;
143  ms->cur_axe = 0;
145  }
146 }
147 
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
enum Ms2100Status status
Definition: ms2100.h:50
int16_t value[3]
data values accessible by channel index
Definition: ms2100.h:54
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
#define MS2100_DIVISOR
Definition: ms2100.c:40
uint8_t input_length
number of data words to read
Definition: spi.h:145
void ms2100_read(struct Ms2100 *ms)
send request to read next axis
Definition: ms2100.c:95
#define Int16FromBuf(_buf, _idx)
Definition: ms2100.c:101
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
union Ms2100::@27 data
Definition: ms2100.h:44
Driver for the ms2100 magnetic sensor from PNI.
void ms2100_reset_cb(struct spi_transaction *t)
Reset callback.
Definition: ms2100_arch.c:69
void ms2100_event(struct Ms2100 *ms)
Definition: ms2100.c:103
volatile uint8_t req_buf[1]
SPI buffer for the command byte.
Definition: ms2100.h:48
struct spi_transaction req_trans
Definition: ms2100.h:46
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
struct spi_transaction read_trans
Definition: ms2100.h:47
volatile uint8_t cur_axe
Definition: ms2100.h:51
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
struct Int16Vect3 vect
data vector in mag coordinate system
Definition: ms2100.h:53
enum SPITransactionStatus status
Definition: spi.h:156
Architecture independent SPI (Serial Peripheral Interface) API.
Definition: spi.h:84
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
void ms2100_arch_init(void)
Definition: ms2100_arch.c:38
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
volatile uint8_t read_buf[2]
SPI buffer for reading a single axis.
Definition: ms2100.h:49
struct Ms2100 ms2100
Definition: ms2100.c:44
CPOL = 0.
Definition: spi.h:77
#define INT_VECT3_ZERO(_v)
unsigned char uint8_t
Definition: types.h:14
CPHA = 0.
Definition: spi.h:68
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
void ms2100_init(struct Ms2100 *ms, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: ms2100.c:47
Definition: spi.h:119
struct spi_periph * spi_p
Definition: ms2100.h:45
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155