-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathSTM32_HAL_Interface.h
More file actions
86 lines (75 loc) · 2.66 KB
/
STM32_HAL_Interface.h
File metadata and controls
86 lines (75 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
* Copyright (C) 2020 Bruce MacKinnon KC1FSZ
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _STM32_HAL_Interface_h
#define _STM32_HAL_Interface_h
#include <stdint.h>
// NOTE: Add the appropriate HAL include here:
#include "stm32f4xx_hal.h"
#include "I2CInterface.h"
// An STM32/HAL based implementation of the I2CInterface.
//
// NOT IMPLEMENTED YET!!
//
class STM32_HAL_Interface : public I2CInterface {
public:
STM32_HAL_Interface(I2C_HandleTypeDef* hi2c)
: _hi2c(hi2c),
_errorCount(0),
_timeoutMs(10) {
}
uint8_t check_address(uint8_t i2c_bus_addr) {
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(_hi2c, (uint16_t)(i2c_bus_addr << 1), 1, _timeoutMs);
if (status == HAL_OK) {
return 0;
} else {
return -1;
}
}
uint8_t read(uint8_t i2c_bus_addr, uint8_t addr) {
uint8_t reg_val = 0;
// NOTE: PER HAL DOCUMENTATION, ADDRESS NEEDS TO BE SHIFTED LEFT
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(_hi2c, (uint16_t)(i2c_bus_addr << 1), (uint16_t)(addr), 1,
®_val, 1, _timeoutMs);
if (status != HAL_OK) {
_errorCount++;
}
return reg_val;
}
uint8_t write(uint8_t i2c_bus_addr, uint8_t addr, uint8_t data) {
// NOTE: PER HAL DOCUMENTATION, ADDRESS NEEDS TO BE SHIFTED LEFT
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(_hi2c, (uint16_t)(i2c_bus_addr << 1), addr, 1,
&data, 1, _timeoutMs);
if (status != HAL_OK) {
_errorCount++;
}
return 1;
}
uint8_t write_bulk(uint8_t i2c_bus_addr, uint8_t addr, uint8_t bytes, uint8_t *data) {
// NOTE: PER HAL DOCUMENTATION, ADDRESS NEEDS TO BE SHIFTED LEFT
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(_hi2c, (uint16_t)(i2c_bus_addr << 1), addr, 1,
data, bytes, _timeoutMs);
if (status != HAL_OK) {
_errorCount++;
}
return bytes;
}
private:
I2C_HandleTypeDef* _hi2c;
int _errorCount;
uint32_t _timeoutMs;
};
#endif