Paparazzi UAS  v4.2.2_stable-4-gcc32f65
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
i2c_abuse_test.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (C) 2011 Christophe De Wagter
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  *
23  */
24 
25 #include "i2c_abuse_test.h"
26 #include "led.h"
27 #include "mcu_periph/i2c.h"
28 
31 
34 
35 void init_i2c_abuse_test(void) {
36  //LED_INIT(DEMO_MODULE_LED);
37  //LED_OFF(DEMO_MODULE_LED);
38 
40  i2c_test1.slave_addr = 0x3C;
41 
43  i2c_abuse_test_bitrate = 10000;
44 
46 
47 }
48 
50 {
51 
52  i2c_test1.slave_addr = 0x3C;
53  i2c_test1.len_w = 0;
54  i2c_test1.len_r = 0;
55 
56  switch (_init)
57  {
58  case 1:
60  i2c_test1.buf[0] = 0x00; // set to rate to 50Hz
61  i2c_test1.buf[1] = 0x00 | (0x06 << 2);
62  i2c_test1.buf[2] = 0x01<<5;
63  i2c_test1.buf[3] = 0x00;
64  i2c_test1.len_w = 4;
65  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
66  break;
67  case 2:
69  i2c_test1.buf[0] = 0x01; // set to gain to 1 Gauss
70  i2c_test1.buf[1] = 0x01<<5;
71  i2c_test1.len_w = 2;
72  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
73  break;
74  case 3:
76  i2c_test1.buf[0] = 0x00; // set to continuous mode
77  i2c_test1.len_w = 1;
78  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
79  break;
80  case 4:
82  i2c_test1.len_r = 1;
83  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
84  break;
85  case 5:
87  i2c_test1.len_r = 2;
88  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
89  break;
90  case 6:
92  i2c_test1.len_r = 3;
93  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
94  break;
95  case 7:
97  i2c_test1.len_r = 4;
98  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
99  break;
100  case 8:
102  i2c_test1.len_r = 5;
103  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
104  break;
105  case 9:
106  // bad addr
107  i2c_test1.slave_addr = 0x3C + 2;
109  i2c_test1.len_w = 1;
110  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
111  break;
112  case 10:
113  // 2 consecutive
115  i2c_test1.buf[0] = 0x00; // set to continuous mode
116  i2c_test1.len_w = 1;
117  i2c_submit(&I2C_ABUSE_PORT,&i2c_test1);
118  break;
119  case 11:
120  i2c_test1.slave_addr = 0x3C;
122  i2c_test1.len_r = 1;
123  i2c_test1.len_w = 1;
124  i2c_test1.buf[0] = 0x03;
125  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
126  break;
127  case 12:
128  i2c_test1.slave_addr = 0x3C;
130  i2c_test1.len_r = 2;
131  i2c_test1.len_w = 1;
132  i2c_test1.buf[0] = 0x03;
133  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
134  break;
135  case 13:
136  i2c_test1.slave_addr = 0x3C;
138  i2c_test1.len_r = 3;
139  i2c_test1.len_w = 1;
140  i2c_test1.buf[0] = 0x03;
141  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
142  break;
143  case 14:
144  i2c_test1.slave_addr = 0x3C;
146  i2c_test1.len_r = 4;
147  i2c_test1.len_w = 1;
148  i2c_test1.buf[0] = 0x03;
149  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
150  break;
151  case 15:
152  i2c_test1.slave_addr = 0x3C;
154  i2c_test1.len_r = 4;
155  i2c_test1.len_w = 2;
156  i2c_test1.buf[0] = 0x03;
157  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
158  break;
159  default:
160  i2c_test1.slave_addr = 0x3C;
162  i2c_test1.len_r = 5;
163  i2c_test1.len_w = 1;
164  i2c_test1.buf[0] = 0x03;
165  i2c_submit(&I2C_ABUSE_PORT, &i2c_test1);
166  }
167 }
168 
169 
171 {
172  if (i2c_idle(&I2C_ABUSE_PORT))
173  {
174  LED_ON(5); // green = idle
175  LED_OFF(4);
176  }
177  else
178  {
179  LED_ON(4); // red = busy
180  LED_OFF(5);
181  }
182 
183  // Wait for I2C transaction object to be released by the I2C driver before changing anything
185  {
187  {
188  //i2c_test2.slave_addr = 0x90;
190  i2c_test2.slave_addr = 0x92;
191  i2c_test2.len_r = 2;
192  i2c_submit(&I2C_ABUSE_PORT,&i2c_test2);
193  }
194  }
195 
196 
198  {
199  if (i2c_abuse_test_counter < 16)
200  {
202  }
203  else
204  {
205  // wait until ready:
206  if (i2c_idle(&I2C_ABUSE_PORT))
207  {
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  {
215  i2c_abuse_test_bitrate -= 410000;
216  }
217  }
218  }
219 
220  if (i2c_abuse_test_counter < 16)
221  {
222  RunOnceEvery(100,LED_TOGGLE(I2C_ABUSE_LED));
224  }
225  }
226 }
227 
229 }
230 
231 
232 
static void i2c_abuse_send_transaction(uint8_t _init)
struct i2c_transaction i2c_test1
struct i2c_transaction i2c_test2
void periodic_50Hz_i2c_abuse_test(void)
#define LED_ON(i)
Definition: led_hw.h:28
void init_i2c_abuse_test(void)
bool_t i2c_idle(struct i2c_periph *p)
Definition: i2c_arch.c:317
uint8_t slave_addr
Definition: i2c.h:43
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:321
volatile uint8_t i2c_abuse_test_counter
void event_i2c_abuse_test(void)
unsigned long uint32_t
Definition: types.h:18
enum I2CTransactionStatus status
Definition: i2c.h:47
#define LED_TOGGLE(i)
Definition: led_hw.h:30
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:46
uint8_t len_w
Definition: i2c.h:45
uint16_t len_r
Definition: i2c.h:44
unsigned char uint8_t
Definition: types.h:14
enum I2CTransactionType type
Definition: i2c.h:42
#define LED_OFF(i)
Definition: led_hw.h:29
arch independent LED (Light Emitting Diodes) API
Definition: i2c.h:10
Definition: i2c.h:9
volatile uint32_t i2c_abuse_test_bitrate
void i2c_setbitrate(struct i2c_periph *p, int bitrate)
Definition: i2c_arch.c:346