Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
i2c_abuse_test.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2011 Christophe De Wagter
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
35 #include "i2c_abuse_test.h"
36 #include "led.h"
37 #include "mcu_periph/i2c.h"
38 
41 
44 
46 {
47  //LED_INIT(DEMO_MODULE_LED);
48  //LED_OFF(DEMO_MODULE_LED);
49 
51  i2c_test1.slave_addr = 0x3C;
52 
54  i2c_abuse_test_bitrate = 10000;
55 
57 
58 }
59 
61 {
62 
63  i2c_test1.slave_addr = 0x3C;
64  i2c_test1.len_w = 0;
65  i2c_test1.len_r = 0;
66 
67  switch (_init) {
68  case 1:
70  i2c_test1.buf[0] = 0x00; // set to rate to 50Hz
71  i2c_test1.buf[1] = 0x00 | (0x06 << 2);
72  i2c_test1.buf[2] = 0x01 << 5;
73  i2c_test1.buf[3] = 0x00;
74  i2c_test1.len_w = 4;
75  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
76  break;
77  case 2:
79  i2c_test1.buf[0] = 0x01; // set to gain to 1 Gauss
80  i2c_test1.buf[1] = 0x01 << 5;
81  i2c_test1.len_w = 2;
82  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
83  break;
84  case 3:
86  i2c_test1.buf[0] = 0x00; // set to continuous mode
87  i2c_test1.len_w = 1;
88  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
89  break;
90  case 4:
92  i2c_test1.len_r = 1;
93  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
94  break;
95  case 5:
97  i2c_test1.len_r = 2;
98  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
99  break;
100  case 6:
102  i2c_test1.len_r = 3;
103  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
104  break;
105  case 7:
107  i2c_test1.len_r = 4;
108  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
109  break;
110  case 8:
112  i2c_test1.len_r = 5;
113  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
114  break;
115  case 9:
116  // bad addr
117  i2c_test1.slave_addr = 0x3C + 2;
119  i2c_test1.len_w = 1;
120  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
121  break;
122  case 10:
123  // 2 consecutive
125  i2c_test1.buf[0] = 0x00; // set to continuous mode
126  i2c_test1.len_w = 1;
127  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
128  break;
129  case 11:
130  i2c_test1.slave_addr = 0x3C;
132  i2c_test1.len_r = 1;
133  i2c_test1.len_w = 1;
134  i2c_test1.buf[0] = 0x03;
135  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
136  break;
137  case 12:
138  i2c_test1.slave_addr = 0x3C;
140  i2c_test1.len_r = 2;
141  i2c_test1.len_w = 1;
142  i2c_test1.buf[0] = 0x03;
143  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
144  break;
145  case 13:
146  i2c_test1.slave_addr = 0x3C;
148  i2c_test1.len_r = 3;
149  i2c_test1.len_w = 1;
150  i2c_test1.buf[0] = 0x03;
151  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
152  break;
153  case 14:
154  i2c_test1.slave_addr = 0x3C;
156  i2c_test1.len_r = 4;
157  i2c_test1.len_w = 1;
158  i2c_test1.buf[0] = 0x03;
159  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
160  break;
161  case 15:
162  i2c_test1.slave_addr = 0x3C;
164  i2c_test1.len_r = 4;
165  i2c_test1.len_w = 2;
166  i2c_test1.buf[0] = 0x03;
167  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
168  break;
169  default:
170  i2c_test1.slave_addr = 0x3C;
172  i2c_test1.len_r = 5;
173  i2c_test1.len_w = 1;
174  i2c_test1.buf[0] = 0x03;
175  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
176  }
177 }
178 
179 
181 {
182  if (i2c_idle(&I2C_ABUSE_PORT)) {
183  LED_ON(5); // green = idle
184  LED_OFF(4);
185  } else {
186  LED_ON(4); // red = busy
187  LED_OFF(5);
188  }
189 
190  // Wait for I2C transaction object to be released by the I2C driver before changing anything
191  if ((i2c_abuse_test_counter < 12) && (i2c_abuse_test_counter > 3)) {
193  //i2c_test2.slave_addr = 0x90;
195  i2c_test2.slave_addr = 0x92;
196  i2c_test2.len_r = 2;
197  i2c_submit(&I2C_ABUSE_PORT, &i2c_test2);
198  }
199  }
200 
201 
203  if (i2c_abuse_test_counter < 16) {
205  } else {
206  // wait until ready:
207  if (i2c_idle(&I2C_ABUSE_PORT)) {
209 
210  i2c_setbitrate(&I2C_ABUSE_PORT, i2c_abuse_test_bitrate);
211 
212  i2c_abuse_test_bitrate += 17000;
213  if (i2c_abuse_test_bitrate > 410000) {
214  i2c_abuse_test_bitrate -= 410000;
215  }
216  }
217  }
218 
219  if (i2c_abuse_test_counter < 16) {
220  RunOnceEvery(100, LED_TOGGLE(I2C_ABUSE_LED));
222  }
223  }
224 }
225 
227 {
228 }
229 
230 
231 
#define LED_ON(i)
Definition: led_hw.h:51
#define LED_OFF(i)
Definition: led_hw.h:52
#define LED_TOGGLE(i)
Definition: led_hw.h:53
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
static bool i2c_idle(struct i2c_periph *p)
Check if I2C bus is idle.
Definition: i2c.h:256
static void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Set I2C bitrate.
Definition: i2c.h:274
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
@ I2CTransRx
receive only transaction
Definition: i2c.h:48
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
I2C transaction structure.
Definition: i2c.h:93
Architecture independent I2C (Inter-Integrated Circuit Bus) API.
void init_i2c_abuse_test(void)
void periodic_50Hz_i2c_abuse_test(void)
volatile uint32_t i2c_abuse_test_bitrate
struct i2c_transaction i2c_test1
void event_i2c_abuse_test(void)
static void i2c_abuse_send_transaction(uint8_t _init)
struct i2c_transaction i2c_test2
volatile uint8_t i2c_abuse_test_counter
Total I2C Abuse:
arch independent LED (Light Emitting Diodes) API
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98