Paparazzi UAS v7.0_unstable
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...
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 */
54 ms->req_trans.cpol = SPICpolIdleLow;
55 ms->req_trans.cpha = SPICphaEdge1;
56 ms->req_trans.dss = SPIDss8bit;
57 ms->req_trans.bitorder = SPIMSBFirst;
58 ms->req_trans.cdiv = SPIDiv64;
59
60 ms->req_trans.slave_idx = slave_idx;
61 ms->req_trans.select = SPISelectUnselect;
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
67 ms->req_trans.before_cb = ms2100_reset_cb;
68 ms->req_trans.status = SPITransDone;
69
70 /* configure spi transaction to read the result */
71 ms->read_trans.cpol = SPICpolIdleLow;
72 ms->read_trans.cpha = SPICphaEdge1;
73 ms->read_trans.dss = SPIDss8bit;
74 ms->read_trans.bitorder = SPIMSBFirst;
75 ms->read_trans.cdiv = SPIDiv64;
76
77 ms->read_trans.slave_idx = slave_idx;
78 ms->read_trans.select = SPISelectUnselect;
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;
85 ms->read_trans.status = SPITransDone;
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));
100 ms->status = MS2100_SENDING_REQ;
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));
112 ms->status = MS2100_READING_RES;
113 }
114 } else if (ms->req_trans.status == SPITransSuccess) {
115 ms->req_trans.status = SPITransDone;
116 } else if (ms->req_trans.status == SPITransFailed) {
117 ms->status = MS2100_IDLE;
118 ms->cur_axe = 0;
119 ms->req_trans.status = SPITransDone;
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;
134 ms->status = MS2100_DATA_AVAILABLE;
135 } else {
136 ms->status = MS2100_IDLE;
137 }
138 ms->read_trans.status = SPITransDone;
139 }
140 } else if (ms->read_trans.status == SPITransFailed) {
141 ms->status = MS2100_IDLE;
142 ms->cur_axe = 0;
143 ms->read_trans.status = SPITransDone;
144 }
145}
146
#define INT_VECT3_ZERO(_v)
enum SPIStatus status
internal state of the peripheral
Definition spi.h:180
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition spi_arch.c:533
@ SPICphaEdge1
CPHA = 0.
Definition spi.h:74
@ SPITransFailed
Definition spi.h:100
@ SPITransSuccess
Definition spi.h:99
@ SPITransDone
Definition spi.h:101
@ SPICpolIdleLow
CPOL = 0.
Definition spi.h:83
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition spi.h:63
@ SPIMSBFirst
Definition spi.h:112
@ SPIDiv64
Definition spi.h:125
@ SPIDss8bit
Definition spi.h:90
SPI peripheral structure.
Definition spi.h:174
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
struct spi_periph * spi_p
Definition ms2100.h:45
void ms2100_arch_init(void)
Definition ms2100_arch.c:40
void ms2100_reset_cb(struct spi_transaction *t)
Reset callback.
Definition ms2100_arch.c:63
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.