Paparazzi UAS  v5.12_stable-4-g9b43e9b
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
alt_srf08.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 Pascal Brisset, Antoine Drouin
3  * Copyright (C) 2002 Chris efstathiou hendrix@otenet.gr
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  */
23 
30 #include "mcu_periph/i2c.h"
31 #include "alt_srf08.h"
32 #include "mcu_periph/uart.h"
33 #include "pprzlink/messages.h"
35 #include "led.h"
36 
37 
38 #ifndef SRF08_I2C_DEV
39 #define SRF08_I2C_DEV i2c0
40 #endif
41 
42 /* Global Variables */
46 
47 
48 /*###########################################################################*/
49 
50 void srf08_init(void)
51 {
52  srf08_received = false;
53  srf08_got = false;
54 
55  srf_trans.buf[0] = 0x00;
56  srf_trans.buf[1] = 0x51;
58 
63 
64  return;
65 }
66 /*###########################################################################*/
67 
69 {
70  LED_ON(2);
74 }
75 
77 void srf08_receive(void)
78 {
79  LED_OFF(2);
81  srf08_received = true;
83 }
84 
86 void srf08_read(void)
87 {
88  srf08_got = true;
90 }
91 
93 void srf08_copy(void)
94 {
95  srf08_range = srf_trans.buf[0] << 8 | srf_trans.buf[1];
96 }
97 
98 void srf08_ping()
99 {
101  while (srf_trans.status != I2CTransSuccess); /* blocking */
102 
103  srf08_receive();
104 }
105 /*###########################################################################*/
106 
108 {
109  uint8_t cnt;
110 
111  union i2c_union {
112  uint32_t rx_word;
113  uint8_t rx_byte[2];
114  } i2c;
115 
116 
117  srf_trans.buf[0] = srf08_register;
118 
119  /* get high byte msb first */
120  if (srf08_register >= 2) {
121  cnt = 2;
122  } else {
123  cnt = 1;
124  }
125 
127 
128  /* get high byte msb first */
129  if (srf08_register >= 2) {
130  i2c.rx_byte[1] = srf_trans.buf[1];
131  }
132 
133  /* get low byte msb first */
134  i2c.rx_byte[0] = srf_trans.buf[0];
135 
136  return (i2c.rx_word);
137 }
138 
139 void srf08_event(void)
140 {
141  float f = 0;
142  uint8_t i = 0;
143 
146  if (srf08_received) {
147  srf08_received = false;
148  srf08_read();
149  } else if (srf08_got) {
150  srf08_got = false;
151  srf08_copy();
152  DOWNLINK_SEND_RANGEFINDER(DefaultChannel, DefaultDevice, &srf08_range, &f, &f, &f, &f, &f, &i);
153  }
154  }
155 }
void srf08_initiate_ranging(void)
Definition: alt_srf08.c:68
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
void srf08_ping()
Definition: alt_srf08.c:98
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
void srf08_event(void)
Definition: alt_srf08.c:139
transaction successfully finished by I2C driver
Definition: i2c.h:57
uint32_t srf08_read_register(uint8_t srf08_register)
Definition: alt_srf08.c:107
#define LED_OFF(i)
Definition: led_hw.h:51
bool srf08_received
Definition: alt_srf08.c:43
void srf08_read(void)
Read values on the bus.
Definition: alt_srf08.c:86
#define SRF08_ECHO_1
Definition: alt_srf08.h:75
#define SRF08_CENTIMETERS
Definition: alt_srf08.h:68
void srf08_copy(void)
Copy the I2C buffer.
Definition: alt_srf08.c:93
struct i2c_transaction srf_trans
Definition: alt_srf08.c:44
unsigned long uint32_t
Definition: types.h:18
#define SRF08_UNIT_0
Definition: alt_srf08.h:39
bool i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:278
I2C transaction structure.
Definition: i2c.h:93
bool srf08_got
Definition: alt_srf08.c:43
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
bool i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:258
void srf08_init(void)
Definition: alt_srf08.c:50
#define SRF08_SET_GAIN
Definition: alt_srf08.h:73
unsigned char uint8_t
Definition: types.h:14
bool i2c_receive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint16_t len)
Submit a read only transaction.
Definition: i2c.c:268
#define SRF08_MIN_GAIN
Definition: alt_srf08.h:62
arch independent LED (Light Emitting Diodes) API
#define LED_ON(i)
Definition: led_hw.h:50
Basic library for SRF08 telemeter.
#define SRF08_I2C_DEV
Definition: alt_srf08.c:39
uint16_t srf08_range
Definition: alt_srf08.c:45
#define SRF08_COMMAND
Definition: alt_srf08.h:72
void srf08_receive(void)
Ask the value to the device.
Definition: alt_srf08.c:77
Architecture independent I2C (Inter-Integrated Circuit Bus) API.