Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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...
45
46
47void 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 */
59
60 ms->req_trans.slave_idx = slave_idx;
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 */
76
77 ms->read_trans.slave_idx = slave_idx;
86
88
89 INT_VECT3_ZERO(ms->data.vect);
90 ms->cur_axe = 0;
91
92 ms->status = MS2100_IDLE;
93}
94
96void 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
105void 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
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
#define INT_VECT3_ZERO(_v)
enum SPIClockPolarity cpol
clock polarity control
Definition spi.h:151
enum SPIClockPhase cpha
clock phase control
Definition spi.h:152
enum SPISlaveSelect select
slave selection behavior
Definition spi.h:150
SPICallback before_cb
NULL or function called before the transaction.
Definition spi.h:156
SPICallback after_cb
NULL or function called after the transaction.
Definition spi.h:157
enum SPIDataSizeSelect dss
data transfer word size
Definition spi.h:153
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition spi.h:146
uint16_t input_length
number of data words to read
Definition spi.h:147
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition spi.h:155
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition spi.h:145
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition spi.h:149
enum SPIBitOrder bitorder
MSB/LSB order.
Definition spi.h:154
uint16_t output_length
number of data words to write
Definition spi.h:148
enum SPITransactionStatus status
Definition spi.h:158
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition spi_arch.c:548
@ SPICphaEdge1
CPHA = 0.
Definition spi.h:70
@ SPITransFailed
Definition spi.h:96
@ SPITransSuccess
Definition spi.h:95
@ SPITransDone
Definition spi.h:97
@ SPICpolIdleLow
CPOL = 0.
Definition spi.h:79
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition spi.h:59
@ SPIMSBFirst
Definition spi.h:108
@ SPIDiv64
Definition spi.h:121
@ SPIDss8bit
Definition spi.h:86
SPI peripheral structure.
Definition spi.h:171
uint16_t foo
Definition main_demo5.c:58
struct Ms2100 ms2100
Definition ms2100.c:44
void ms2100_event(struct Ms2100 *ms)
Definition ms2100.c:105
#define MS2100_DIVISOR
Definition ms2100.c:40
void ms2100_init(struct Ms2100 *ms, struct spi_periph *spi_p, uint8_t slave_idx)
Definition ms2100.c:47
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.
@ MS2100_DATA_AVAILABLE
Definition ms2100.h:41
@ MS2100_IDLE
Definition ms2100.h:37
@ MS2100_SENDING_REQ
Definition ms2100.h:38
@ MS2100_READING_RES
Definition ms2100.h:40
@ MS2100_GOT_EOC
Definition ms2100.h:39
enum Ms2100Status status
Definition ms2100.h:50
volatile uint8_t req_buf[1]
SPI buffer for the command byte.
Definition ms2100.h:48
struct spi_transaction read_trans
Definition ms2100.h:47
volatile uint8_t cur_axe
Definition ms2100.h:51
union Ms2100::@357 data
void ms2100_arch_init(void)
volatile uint8_t read_buf[2]
SPI buffer for reading a single axis.
Definition ms2100.h:49
struct spi_transaction req_trans
Definition ms2100.h:46
struct spi_periph * spi_p
Definition ms2100.h:45
Architecture independent SPI (Serial Peripheral Interface) API.
short int16_t
Typedef defining 16 bit short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.