Paparazzi UAS  v4.0.4_stable-3-gf39211a
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
alt_srf08.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2005 Pascal Brisset, Antoine Drouin
5  * Copyright (C) 2002 Chris efstathiou hendrix@otenet.gr
6  *
7  * This file is part of paparazzi.
8  *
9  * paparazzi is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * paparazzi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with paparazzi; see the file COPYING. If not, write to
21  * the Free Software Foundation, 59 Temple Place - Suite 330,
22  * Boston, MA 02111-1307, USA.
23  *
24  */
30 #include "mcu_periph/i2c.h"
31 #include "alt_srf08.h"
32 #include "mcu_periph/uart.h"
33 #include "messages.h"
35 #include "led.h"
36 
37 #ifndef DOWNLINK_DEVICE
38 #define DOWNLINK_DEVICE DOWNLINK_AP_DEVICE
39 #endif
40 
41 #ifndef SRF08_I2C_DEV
42 #define SRF08_I2C_DEV i2c0
43 #endif
44 
45 /* Global Variables */
49 
50 
51 /*###########################################################################*/
52 
53 void srf08_init(void)
54 {
56  srf08_got = FALSE;
57 
58  srf_trans.buf[0] = 0x00;
59  srf_trans.buf[1] = 0x51;
61 
66 
67  return;
68 }
69 /*###########################################################################*/
70 
72 LED_ON(2);
76 }
77 
79 void srf08_receive(void) {
80 LED_OFF(2);
84 }
85 
87 void srf08_read(void) {
88  srf08_got = TRUE;
90 }
91 
93 void srf08_copy(void) {
94  srf08_range = srf_trans.buf[0] << 8 | srf_trans.buf[1];
95 }
96 
97 void srf08_ping()
98 {
100  while (srf_trans.status != I2CTransSuccess); /* blocking */
101 
102  srf08_receive();
103 }
104 /*###########################################################################*/
105 
107 {
108  uint8_t cnt;
109 
110  union i2c_union {
111  uint32_t rx_word;
112  uint8_t rx_byte[2];
113  } i2c;
114 
115 
116  srf_trans.buf[0] = srf08_register;
117 
118  /* get high byte msb first */
119  if (srf08_register>=2)
120  cnt = 2;
121  else
122  cnt = 1;
123 
125 
126  /* get high byte msb first */
127  if(srf08_register>=2) {
128  i2c.rx_byte[1]=srf_trans.buf[1];
129  }
130 
131  /* get low byte msb first */
132  i2c.rx_byte[0]=srf_trans.buf[0];
133 
134  return(i2c.rx_word);
135 }
136 
137 void srf08_event(void)
138 {
139  float f=0;
140  uint8_t i=0;
141 
144  if (srf08_received) {
146  srf08_read();
147  }
148  else if (srf08_got) {
149  srf08_got = FALSE;
150  srf08_copy();
151  DOWNLINK_SEND_RANGEFINDER(DefaultChannel, DefaultDevice, &srf08_range, &f, &f, &f, &f, &f, &i);
152  }
153  }
154 }
void srf08_initiate_ranging(void)
Definition: alt_srf08.c:71
unsigned short uint16_t
Definition: types.h:16
arch independent UART (Universal Asynchronous Receiver/Transmitter) API
#define I2CReceive(_p, _t, _s_addr, _len)
Definition: i2c.h:140
void srf08_ping()
Definition: alt_srf08.c:97
void srf08_event(void)
Definition: alt_srf08.c:137
#define I2CTransmit(_p, _t, _s_addr, _len)
Definition: i2c.h:148
uint32_t srf08_read_register(uint8_t srf08_register)
Definition: alt_srf08.c:106
bool_t srf08_got
Definition: alt_srf08.c:46
#define LED_ON(i)
Definition: led_hw.h:28
void srf08_read(void)
Read values on the bus.
Definition: alt_srf08.c:87
#define SRF08_ECHO_1
Definition: alt_srf08.h:75
#define FALSE
Definition: imu_chimu.h:141
#define SRF08_CENTIMETERS
Definition: alt_srf08.h:68
void srf08_copy(void)
Copy the I2C buffer.
Definition: alt_srf08.c:93
bool_t srf08_received
Definition: alt_srf08.c:46
struct i2c_transaction srf_trans
Definition: alt_srf08.c:47
unsigned long uint32_t
Definition: types.h:18
#define SRF08_UNIT_0
Definition: alt_srf08.h:39
enum I2CTransactionStatus status
Definition: i2c.h:47
#define TRUE
Definition: imu_chimu.h:144
void srf08_init(void)
Definition: alt_srf08.c:53
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
#define SRF08_SET_GAIN
Definition: alt_srf08.h:73
unsigned char uint8_t
Definition: types.h:14
#define I2CTransceive(_p, _t, _s_addr, _len_w, _len_r)
Definition: i2c.h:156
#define SRF08_MIN_GAIN
Definition: alt_srf08.h:62
#define LED_OFF(i)
Definition: led_hw.h:29
arch independent LED (Light Emitting Diodes) API
#define SRF08_I2C_DEV
Definition: alt_srf08.c:42
uint16_t srf08_range
Definition: alt_srf08.c:48
#define SRF08_COMMAND
Definition: alt_srf08.h:72
void srf08_receive(void)
Ask the value to the device.
Definition: alt_srf08.c:79