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  {
47  SB = 0x0001,
48  ADDR = 0x0002,
49  BTF = 0x0004,
50  ADD10 = 0x0008,
51  STOPF = 0x0010,
52  RES1 = 0x0020,
53  RXNE = 0x0040,
54  TXE = 0x0080,
55  BERR = 0x0100,
56  ARLO = 0x0200,
57  AF = 0x0400,
58  OVR = 0x0800,
59  PEC_ERR = 0x1000,
60  RES2 = 0x2000,
61  TIMEOUT = 0x4000,
62  SMB_ALERT = 0x8000,
63  MSL = 0x1 << 16,
64  BUSY = 0x2 << 16,
65  TRA = 0x4 << 16,
66  RES3 = 0x8 << 16,
67  GEN_CALL = 0x10 << 16,
68  SMBDE_FAULT = 0x20 << 16,
69  DUALF = 0x40 << 16,
70  };
71 
73 
74 
75 
76  typedef enum
77  {
82 
85 
86  uint16_t error_count_ = 0;
87 
88  //Variables for current job:
89  volatile current_status_t current_status_;
90  volatile uint8_t return_code_;
91  bool subaddress_sent_ = false;
92  bool done_ = false;
93 
94  volatile uint8_t addr_;
95  volatile uint8_t reg_;
96  volatile uint8_t len_;
97  volatile uint8_t data_;
98 
100 
102 
103 public:
104 
105  enum : int8_t
106  {
110  };
111 
113  {
114  uint32_t history_[60];
115  uint32_t head_ = 0;
116 
117  public:
118  void add_event(uint32_t event)
119  {
120  history_[head_] = event;
121  head_ = (head_ + 1) % 60;
122  }
123  void clear()
124  {
125  memset(history_, 0, sizeof(history_));
126  }
127  };
130 
131  uint64_t last_event_us_;
132 
133  void (*cb_)(uint8_t);
134 
135  void init(const i2c_hardware_struct_t *c);
136  void unstick();
137  void hardware_failure();
138  bool check_busy();
139  int8_t read(uint8_t addr, uint8_t reg, uint8_t num_bytes, uint8_t *data, void(*callback)(uint8_t) = nullptr,
140  bool blocking = false);
141  int8_t write(uint8_t addr, uint8_t reg, uint8_t data, void(*callback)(uint8_t), bool blocking = false);
142 
143  int8_t write(uint8_t addr, uint8_t reg, uint8_t data);
144  int8_t read(uint8_t addr, uint8_t reg, uint8_t *data);
145 
146  inline uint16_t num_errors()
147  {
148  return error_count_;
149  }
150 
151  //interrupt handlers
152  void handle_error();
153  void handle_event();
154  void transfer_complete_cb();
155 };
156 
157 //global i2c ptrs used by the event interrupts
158 extern I2C *I2C1_Ptr;
159 extern I2C *I2C2_Ptr;
160 extern I2C *I2C3_Ptr;
void transfer_complete_cb()
Definition: i2c.cpp:227
void(* cb_)(uint8_t)
Definition: i2c.h:133
Debug_History event_history_
Definition: i2c.h:128
DMA Init structure definition.
Definition: stm32f4xx_dma.h:54
Definition: i2c.h:58
uint16_t error_count_
Definition: i2c.h:86
void clear()
Definition: i2c.h:123
Definition: i2c.h:51
Debug_History interrupt_history_
Definition: i2c.h:129
void init(const i2c_hardware_struct_t *c)
Definition: i2c.cpp:53
void add_event(uint32_t event)
Definition: i2c.h:118
uint32_t head_
Definition: i2c.h:115
Definition: i2c.h:63
DMA_InitTypeDef DMA_InitStructure_
Definition: i2c.h:99
volatile uint8_t data_
Definition: i2c.h:97
volatile current_status_t current_status_
Definition: i2c.h:89
Definition: i2c.h:55
I2C * I2C2_Ptr
Definition: i2c.cpp:50
Definition: i2c.h:47
GPIO sda_
Definition: i2c.h:84
uint16_t num_errors()
Definition: i2c.h:146
Definition: i2c.h:48
void hardware_failure()
Definition: i2c.h:53
Definition: i2c.h:60
static volatile uint8_t addr
Definition: drv_i2c.c:95
void handle_hardware_failure()
Definition: i2c.cpp:385
static volatile uint8_t reg
Definition: drv_i2c.c:96
bool done_
Definition: i2c.h:92
Definition: i2c.h:50
Definition: i2c.h:65
Definition: i2c.h:57
I2C * I2C1_Ptr
Definition: i2c.cpp:49
void unstick()
Definition: i2c.cpp:125
Definition: i2c.h:69
Definition: i2c.h:54
bool check_busy()
Definition: i2c.cpp:511
Definition: i2c.h:78
volatile uint8_t addr_
Definition: i2c.h:94
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:413
volatile uint8_t len_
Definition: i2c.h:96
volatile uint8_t return_code_
Definition: i2c.h:90
const i2c_hardware_struct_t * c_
Definition: i2c.h:101
uint32_t history_[60]
Definition: i2c.h:114
I2C * I2C3_Ptr
Definition: i2c.cpp:51
Definition: i2c.h:49
Definition: i2c.h:52
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:95
void handle_error()
Definition: i2c.cpp:395
Definition: i2c.h:64
bool subaddress_sent_
Definition: i2c.h:91
GPIO scl_
Definition: i2c.h:83
Definition: i2c.h:56
uint64_t last_event_us_
Definition: i2c.h:131
Definition: i2c.h:66
current_status_t
Definition: i2c.h:76


rosflight_firmware
Author(s): Daniel Koch , James Jackson
autogenerated on Wed Jul 3 2019 19:59:24