Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 "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 {
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 LED_ON(2);
73 }
74 
76 void srf08_receive(void) {
77 LED_OFF(2);
81 }
82 
84 void srf08_read(void) {
85  srf08_got = TRUE;
87 }
88 
90 void srf08_copy(void) {
91  srf08_range = srf_trans.buf[0] << 8 | srf_trans.buf[1];
92 }
93 
94 void srf08_ping()
95 {
97  while (srf_trans.status != I2CTransSuccess); /* blocking */
98 
99  srf08_receive();
100 }
101 /*###########################################################################*/
102 
104 {
105  uint8_t cnt;
106 
107  union i2c_union {
108  uint32_t rx_word;
109  uint8_t rx_byte[2];
110  } i2c;
111 
112 
113  srf_trans.buf[0] = srf08_register;
114 
115  /* get high byte msb first */
116  if (srf08_register>=2)
117  cnt = 2;
118  else
119  cnt = 1;
120 
122 
123  /* get high byte msb first */
124  if(srf08_register>=2) {
125  i2c.rx_byte[1]=srf_trans.buf[1];
126  }
127 
128  /* get low byte msb first */
129  i2c.rx_byte[0]=srf_trans.buf[0];
130 
131  return(i2c.rx_word);
132 }
133 
134 void srf08_event(void)
135 {
136  float f=0;
137  uint8_t i=0;
138 
141  if (srf08_received) {
143  srf08_read();
144  }
145  else if (srf08_got) {
146  srf08_got = FALSE;
147  srf08_copy();
148  DOWNLINK_SEND_RANGEFINDER(DefaultChannel, DefaultDevice, &srf08_range, &f, &f, &f, &f, &f, &i);
149  }
150  }
151 }
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:94
void srf08_event(void)
Definition: alt_srf08.c:134
transaction successfully finished by I2C driver
Definition: i2c.h:57
I2C transaction structure.
Definition: i2c.h:93
uint32_t srf08_read_register(uint8_t srf08_register)
Definition: alt_srf08.c:103
bool_t srf08_got
Definition: alt_srf08.c:43
#define LED_ON(i)
Definition: led_hw.h:28
void srf08_read(void)
Read values on the bus.
Definition: alt_srf08.c:84
bool_t 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:258
#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:90
bool_t srf08_received
Definition: alt_srf08.c:43
struct i2c_transaction srf_trans
Definition: alt_srf08.c:44
bool_t 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:238
unsigned long uint32_t
Definition: types.h:18
#define SRF08_UNIT_0
Definition: alt_srf08.h:39
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
#define TRUE
Definition: imu_chimu.h:144
void srf08_init(void)
Definition: alt_srf08.c:50
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
#define SRF08_SET_GAIN
Definition: alt_srf08.h:73
unsigned char uint8_t
Definition: types.h:14
#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
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
bool_t 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:248
void srf08_receive(void)
Ask the value to the device.
Definition: alt_srf08.c:76
Architecture independent I2C (Inter-Integrated Circuit Bus) API.