i2c.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017, James Jackson
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * * Neither the name of the copyright holder nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #pragma once
33 
34 #include <stdint.h>
35 
36 #include "revo_f4.h"
37 
38 #include "gpio.h"
39 
40 class I2C
41 {
42 // BUSY, MSL, ADDR, TXE and TRA flags
43 private:
44  // [SR2 << 8 | SR1] Bits
45  enum {
46  SB = 0x0001,
47  ADDR = 0x0002,
48  BTF = 0x0004,
49  ADD10 = 0x0008,
50  STOPF = 0x0010,
51  RES1 = 0x0020,
52  RXNE = 0x0040,
53  TXE = 0x0080,
54  BERR = 0x0100,
55  ARLO = 0x0200,
56  AF = 0x0400,
57  OVR = 0x0800,
58  PEC_ERR = 0x1000,
59  RES2 = 0x2000,
60  TIMEOUT = 0x4000,
61  SMB_ALERT = 0x8000,
62  MSL = 0x1 << 16,
63  BUSY = 0x2 << 16,
64  TRA = 0x4 << 16,
65  RES3 = 0x8 << 16,
66  GEN_CALL = 0x10 << 16,
67  SMBDE_FAULT = 0x20 << 16,
68  DUALF = 0x40 << 16,
69  };
70 
72 
73 
74 
75  typedef enum
76  {
81 
84 
85  uint16_t error_count_ = 0;
86 
87  //Variables for current job:
88  volatile current_status_t current_status_;
89  volatile uint8_t return_code_;
90  bool subaddress_sent_ = false;
91  bool done_ = false;
92 
93  volatile uint8_t addr_;
94  volatile uint8_t reg_;
95  volatile uint8_t len_;
96  volatile uint8_t data_;
97 
99 
101 
102 public:
103 
104  enum : int8_t
105  {
109  };
110 
112  {
113  uint32_t history_[60];
114  uint32_t head_ = 0;
115 
116  public:
117  void add_event(uint32_t event)
118  {
119  history_[head_] = event;
120  head_ = (head_ + 1) % 60;
121  }
122  void clear()
123  {
124  memset(history_, 0, sizeof(history_));
125  }
126  };
129 
130  uint64_t last_event_us_;
131 
132  void (*cb_)(uint8_t);
133 
134  void init(const i2c_hardware_struct_t *c);
135  void unstick();
136  void hardware_failure();
137  bool check_busy();
138  int8_t read(uint8_t addr, uint8_t reg, uint8_t num_bytes, uint8_t* data, void(*callback)(uint8_t) = nullptr, bool blocking = false);
139  int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking = false);
140 
141  int8_t write(uint8_t addr, uint8_t reg, uint8_t data);
142  int8_t read(uint8_t addr, uint8_t reg, uint8_t *data);
143 
144  inline uint16_t num_errors() { return error_count_; }
145 
146  //interrupt handlers
147  void handle_error();
148  void handle_event();
149  void transfer_complete_cb();
150 };
151 
152 //global i2c ptrs used by the event interrupts
153 extern I2C* I2C1_Ptr;
154 extern I2C* I2C2_Ptr;
155 extern I2C* I2C3_Ptr;
void transfer_complete_cb()
Definition: i2c.cpp:227
void(* cb_)(uint8_t)
Definition: i2c.h:132
Debug_History event_history_
Definition: i2c.h:127
DMA Init structure definition.
Definition: stm32f4xx_dma.h:54
Definition: i2c.h:57
uint16_t error_count_
Definition: i2c.h:85
void clear()
Definition: i2c.h:122
Definition: i2c.h:50
Debug_History interrupt_history_
Definition: i2c.h:128
void init(const i2c_hardware_struct_t *c)
Definition: i2c.cpp:53
void add_event(uint32_t event)
Definition: i2c.h:117
uint32_t head_
Definition: i2c.h:114
Definition: i2c.h:62
DMA_InitTypeDef DMA_InitStructure_
Definition: i2c.h:98
volatile uint8_t data_
Definition: i2c.h:96
volatile current_status_t current_status_
Definition: i2c.h:88
Definition: i2c.h:54
I2C * I2C2_Ptr
Definition: i2c.cpp:50
Definition: i2c.h:46
GPIO sda_
Definition: i2c.h:83
uint16_t num_errors()
Definition: i2c.h:144
Definition: i2c.h:47
void hardware_failure()
Definition: i2c.h:52
Definition: i2c.h:59
static volatile uint8_t addr
Definition: drv_i2c.c:95
void handle_hardware_failure()
Definition: i2c.cpp:384
static volatile uint8_t reg
Definition: drv_i2c.c:96
bool done_
Definition: i2c.h:91
Definition: i2c.h:49
Definition: i2c.h:64
Definition: i2c.h:56
I2C * I2C1_Ptr
Definition: i2c.cpp:49
void unstick()
Definition: i2c.cpp:125
Definition: i2c.h:68
Definition: i2c.h:53
bool check_busy()
Definition: i2c.cpp:509
Definition: i2c.h:77
volatile uint8_t addr_
Definition: i2c.h:93
int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking=false)
Definition: i2c.cpp:298
Definition: gpio.h:37
Definition: i2c.h:40
void handle_event()
Definition: i2c.cpp:411
volatile uint8_t len_
Definition: i2c.h:95
volatile uint8_t return_code_
Definition: i2c.h:89
const i2c_hardware_struct_t * c_
Definition: i2c.h:100
uint32_t history_[60]
Definition: i2c.h:113
I2C * I2C3_Ptr
Definition: i2c.cpp:51
Definition: i2c.h:48
Definition: i2c.h:51
int8_t read(uint8_t addr, uint8_t reg, uint8_t num_bytes, uint8_t *data, void(*callback)(uint8_t)=nullptr, bool blocking=false)
Definition: i2c.cpp:179
volatile uint8_t reg_
Definition: i2c.h:94
void handle_error()
Definition: i2c.cpp:393
Definition: i2c.h:63
bool subaddress_sent_
Definition: i2c.h:90
GPIO scl_
Definition: i2c.h:82
Definition: i2c.h:55
uint64_t last_event_us_
Definition: i2c.h:130
Definition: i2c.h:65
current_status_t
Definition: i2c.h:75


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Oct 24 2019 03:17:18