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 "gpio.h"
35 #include "revo_f4.h"
36 
37 #include <stdint.h>
38 
39 class I2C
40 {
41  // BUSY, MSL, ADDR, TXE and TRA flags
42 private:
43  // [SR2 << 8 | SR1] Bits
44  enum
45  {
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  typedef enum
74  {
79 
82 
83  uint16_t error_count_ = 0;
84 
85  // Variables for current job:
86  volatile current_status_t current_status_;
87  volatile uint8_t return_code_;
88  bool subaddress_sent_ = false;
89  bool done_ = false;
90  bool initialized_ = false;
91 
92  volatile uint8_t addr_;
93  volatile uint8_t reg_;
94  volatile uint8_t len_;
95  volatile uint8_t data_;
96 
98 
100 
101 public:
102  enum : int8_t
103  {
107  };
108 
110  {
111  uint32_t history_[60];
112  uint32_t head_ = 0;
113 
114  public:
115  void add_event(uint32_t event)
116  {
117  history_[head_] = event;
118  head_ = (head_ + 1) % 60;
119  }
120  void clear() { memset(history_, 0, sizeof(history_)); }
121  };
124 
125  uint64_t last_event_us_;
126 
127  void (*cb_)(uint8_t);
128 
129  void init(const i2c_hardware_struct_t *c);
130  void unstick();
131  void hardware_failure();
132  bool check_busy();
133  bool is_initialized() { return initialized_; }
134  int8_t read(uint8_t addr,
135  uint8_t reg,
136  uint8_t num_bytes,
137  uint8_t *data,
138  void (*callback)(uint8_t) = nullptr,
139  bool blocking = false);
140  int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void (*callback)(uint8_t), bool blocking = false);
141 
142  int8_t write(uint8_t addr, uint8_t reg, uint8_t data);
143  int8_t read(uint8_t addr, uint8_t reg, uint8_t *data);
144 
145  inline uint16_t num_errors() { return error_count_; }
146 
147  // interrupt handlers
148  void handle_error();
149  void handle_event();
150  void transfer_complete_cb();
151 };
152 
153 // global i2c ptrs used by the event interrupts
154 extern I2C *I2C1_Ptr;
155 extern I2C *I2C2_Ptr;
156 extern I2C *I2C3_Ptr;
void transfer_complete_cb()
Definition: i2c.cpp:225
void(* cb_)(uint8_t)
Definition: i2c.h:127
Debug_History event_history_
Definition: i2c.h:122
bool is_initialized()
Definition: i2c.h:133
DMA Init structure definition.
Definition: stm32f4xx_dma.h:54
Definition: i2c.h:57
uint16_t error_count_
Definition: i2c.h:83
void clear()
Definition: i2c.h:120
Definition: i2c.h:50
Debug_History interrupt_history_
Definition: i2c.h:123
void init(const i2c_hardware_struct_t *c)
Definition: i2c.cpp:52
void add_event(uint32_t event)
Definition: i2c.h:115
uint32_t head_
Definition: i2c.h:112
Definition: i2c.h:62
DMA_InitTypeDef DMA_InitStructure_
Definition: i2c.h:97
volatile uint8_t data_
Definition: i2c.h:95
volatile current_status_t current_status_
Definition: i2c.h:86
Definition: i2c.h:54
I2C * I2C2_Ptr
Definition: i2c.cpp:49
Definition: i2c.h:46
GPIO sda_
Definition: i2c.h:81
uint16_t num_errors()
Definition: i2c.h:145
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:89
Definition: i2c.h:49
Definition: i2c.h:64
Definition: i2c.h:56
I2C * I2C1_Ptr
Definition: i2c.cpp:48
void unstick()
Definition: i2c.cpp:125
Definition: i2c.h:68
bool initialized_
Definition: i2c.h:90
Definition: i2c.h:53
bool check_busy()
Definition: i2c.cpp:508
Definition: i2c.h:75
volatile uint8_t addr_
Definition: i2c.h:92
int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking=false)
Definition: i2c.cpp:297
Definition: gpio.h:37
Definition: i2c.h:39
void handle_event()
Definition: i2c.cpp:411
volatile uint8_t len_
Definition: i2c.h:94
volatile uint8_t return_code_
Definition: i2c.h:87
const i2c_hardware_struct_t * c_
Definition: i2c.h:99
uint32_t history_[60]
Definition: i2c.h:111
I2C * I2C3_Ptr
Definition: i2c.cpp:50
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:177
volatile uint8_t reg_
Definition: i2c.h:93
void handle_error()
Definition: i2c.cpp:393
Definition: i2c.h:63
bool subaddress_sent_
Definition: i2c.h:88
GPIO scl_
Definition: i2c.h:80
Definition: i2c.h:55
uint64_t last_event_us_
Definition: i2c.h:125
Definition: i2c.h:65
current_status_t
Definition: i2c.h:73


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Thu Apr 15 2021 05:07:46