I2C hang recover function added
Dependencies: UniGraphic mbed vt100
In this version, check_i2c_pins function was added in edge_mgr.cpp.
プログラムの起動時、I2Cモジュールを初期化する前に、I2Cに使用するピンの電位を確認し
もし一方でも Low に張り付いていた場合、SCL を GPIO 出力に設定して
所定回数 (I2C_UNLOCK_TRIAL_CYCLE) 反転させることにより、疑似リセットクロックを生成します。
その後は、通常の起動手順に復帰し、以降はこれまでと同様の動作をします。
sensors/LM75B.cpp@0:d895cd1cd897, 2018-04-03 (annotated)
- Committer:
- Rhyme
- Date:
- Tue Apr 03 08:30:29 2018 +0000
- Revision:
- 0:d895cd1cd897
Initial I2C Pin force reset function added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:d895cd1cd897 | 1 | #include "mbed.h" |
Rhyme | 0:d895cd1cd897 | 2 | #include "LM75B.h" |
Rhyme | 0:d895cd1cd897 | 3 | #include "af_mgr.h" |
Rhyme | 0:d895cd1cd897 | 4 | |
Rhyme | 0:d895cd1cd897 | 5 | /* Register list */ |
Rhyme | 0:d895cd1cd897 | 6 | #define PTR_CONF 0x01 |
Rhyme | 0:d895cd1cd897 | 7 | #define PTR_TEMP 0x00 |
Rhyme | 0:d895cd1cd897 | 8 | #define PTR_TOS 0x03 |
Rhyme | 0:d895cd1cd897 | 9 | #define PTR_THYST 0x02 |
Rhyme | 0:d895cd1cd897 | 10 | |
Rhyme | 0:d895cd1cd897 | 11 | /* Configuration register */ |
Rhyme | 0:d895cd1cd897 | 12 | /* B[7:5] : Reserved */ |
Rhyme | 0:d895cd1cd897 | 13 | /* B[4:3] : OS_F_QUE[1:0] OS fault queue value */ |
Rhyme | 0:d895cd1cd897 | 14 | #define CONFIG_QUE_1 0x00 |
Rhyme | 0:d895cd1cd897 | 15 | #define CONFIG_QUE_2 (0x01 << 3) |
Rhyme | 0:d895cd1cd897 | 16 | #define CONFIG_QUE_4 (0x10 << 3) |
Rhyme | 0:d895cd1cd897 | 17 | #define CONFIG_QUE_6 (0x11 << 3) |
Rhyme | 0:d895cd1cd897 | 18 | /* B[2] : OS_POL 0 = OS active LOW, 1 = OS active HIGH */ |
Rhyme | 0:d895cd1cd897 | 19 | #define CONFIG_OS_POL_L 0x00 |
Rhyme | 0:d895cd1cd897 | 20 | #define CONFIG_OS_POL_H (0x01 << 2) |
Rhyme | 0:d895cd1cd897 | 21 | /* B[1] : OS_COMP_INT 0 = OS comparator, 1 = OS interrupt */ |
Rhyme | 0:d895cd1cd897 | 22 | #define CONFIG_OS_COMP 0x00 |
Rhyme | 0:d895cd1cd897 | 23 | #define CONFIG_OS_INT (0x01 << 1) |
Rhyme | 0:d895cd1cd897 | 24 | /* B[0] : SHUTDOWN 0 = normal, 1 = shutdown */ |
Rhyme | 0:d895cd1cd897 | 25 | #define CONFIG_NORMARL 0x00 |
Rhyme | 0:d895cd1cd897 | 26 | #define CONFIG_SHUTDOWN 0x01 |
Rhyme | 0:d895cd1cd897 | 27 | |
Rhyme | 0:d895cd1cd897 | 28 | /* Temperature register */ |
Rhyme | 0:d895cd1cd897 | 29 | /* D[15:5] = 11 bit data 0.125 * temp data */ |
Rhyme | 0:d895cd1cd897 | 30 | /* D[4:0] : reserved */ |
Rhyme | 0:d895cd1cd897 | 31 | |
Rhyme | 0:d895cd1cd897 | 32 | /* Tos register */ |
Rhyme | 0:d895cd1cd897 | 33 | /* D[15:7] = 9 bit data */ |
Rhyme | 0:d895cd1cd897 | 34 | /* D[6:0] : reserved */ |
Rhyme | 0:d895cd1cd897 | 35 | |
Rhyme | 0:d895cd1cd897 | 36 | /* Thyst register */ |
Rhyme | 0:d895cd1cd897 | 37 | /* D[15:7] = 9 ibt data */ |
Rhyme | 0:d895cd1cd897 | 38 | /* D[6:0] : reserved */ |
Rhyme | 0:d895cd1cd897 | 39 | |
Rhyme | 0:d895cd1cd897 | 40 | LM75B::LM75B(I2C *i2c, int addr) : m_addr(addr<<1) { |
Rhyme | 0:d895cd1cd897 | 41 | p_i2c = i2c ; |
Rhyme | 0:d895cd1cd897 | 42 | p_i2c->frequency(100000); /* 100kHz */ |
Rhyme | 0:d895cd1cd897 | 43 | // activate the peripheral |
Rhyme | 0:d895cd1cd897 | 44 | } |
Rhyme | 0:d895cd1cd897 | 45 | |
Rhyme | 0:d895cd1cd897 | 46 | LM75B::~LM75B() { } |
Rhyme | 0:d895cd1cd897 | 47 | |
Rhyme | 0:d895cd1cd897 | 48 | int LM75B::temp(int8_t *temp) |
Rhyme | 0:d895cd1cd897 | 49 | { |
Rhyme | 0:d895cd1cd897 | 50 | int result ; |
Rhyme | 0:d895cd1cd897 | 51 | char t[1] = { 0x00 } ; |
Rhyme | 0:d895cd1cd897 | 52 | result = p_i2c->write(m_addr, t, 1, true) ; |
Rhyme | 0:d895cd1cd897 | 53 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 54 | result = p_i2c->read(m_addr, t, 1) ; |
Rhyme | 0:d895cd1cd897 | 55 | } |
Rhyme | 0:d895cd1cd897 | 56 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 57 | *temp = (int8_t)t[0] ; |
Rhyme | 0:d895cd1cd897 | 58 | } |
Rhyme | 0:d895cd1cd897 | 59 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 60 | } |
Rhyme | 0:d895cd1cd897 | 61 | |
Rhyme | 0:d895cd1cd897 | 62 | int LM75B::getTemp(float *temp) |
Rhyme | 0:d895cd1cd897 | 63 | { |
Rhyme | 0:d895cd1cd897 | 64 | int result ; |
Rhyme | 0:d895cd1cd897 | 65 | char t[2] = { 0, 0 } ; |
Rhyme | 0:d895cd1cd897 | 66 | int16_t iTemp = 0 ; |
Rhyme | 0:d895cd1cd897 | 67 | result = p_i2c->write(m_addr, t, 1) ; /* write pointer byte 0x00 */ |
Rhyme | 0:d895cd1cd897 | 68 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 69 | result = p_i2c->read(m_addr, t, 2) ; /* read MSB, LSB */ |
Rhyme | 0:d895cd1cd897 | 70 | } |
Rhyme | 0:d895cd1cd897 | 71 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 72 | iTemp = (t[0] << 8) | t[1] ; |
Rhyme | 0:d895cd1cd897 | 73 | iTemp >>= 5 ; |
Rhyme | 0:d895cd1cd897 | 74 | *temp = 0.125 * iTemp ; |
Rhyme | 0:d895cd1cd897 | 75 | } |
Rhyme | 0:d895cd1cd897 | 76 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 77 | } |
Rhyme | 0:d895cd1cd897 | 78 | |
Rhyme | 0:d895cd1cd897 | 79 | int LM75B::getConfig(uint8_t ptr_byte, uint8_t *config_data) |
Rhyme | 0:d895cd1cd897 | 80 | { |
Rhyme | 0:d895cd1cd897 | 81 | int result ; |
Rhyme | 0:d895cd1cd897 | 82 | char config = 0x00 ; /* default value */ |
Rhyme | 0:d895cd1cd897 | 83 | result = p_i2c->write(m_addr, (char*)(&ptr_byte), 1, true) ; |
Rhyme | 0:d895cd1cd897 | 84 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 85 | result = p_i2c->read(m_addr, &config, 1) ; |
Rhyme | 0:d895cd1cd897 | 86 | } |
Rhyme | 0:d895cd1cd897 | 87 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 88 | *config_data = config ; |
Rhyme | 0:d895cd1cd897 | 89 | } |
Rhyme | 0:d895cd1cd897 | 90 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 91 | } |
Rhyme | 0:d895cd1cd897 | 92 | |
Rhyme | 0:d895cd1cd897 | 93 | int LM75B::setConfig(uint8_t ptr_byte, uint8_t config_data) |
Rhyme | 0:d895cd1cd897 | 94 | { |
Rhyme | 0:d895cd1cd897 | 95 | int result ; |
Rhyme | 0:d895cd1cd897 | 96 | char t[2] ; |
Rhyme | 0:d895cd1cd897 | 97 | t[0] = ptr_byte ; |
Rhyme | 0:d895cd1cd897 | 98 | t[1] = config_data ; |
Rhyme | 0:d895cd1cd897 | 99 | result = p_i2c->write(m_addr, t, 2, true) ; |
Rhyme | 0:d895cd1cd897 | 100 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 101 | } |
Rhyme | 0:d895cd1cd897 | 102 | |
Rhyme | 0:d895cd1cd897 | 103 | int LM75B::readRegs(int addr, uint8_t * data, int len) |
Rhyme | 0:d895cd1cd897 | 104 | { |
Rhyme | 0:d895cd1cd897 | 105 | int result ; |
Rhyme | 0:d895cd1cd897 | 106 | char t[1] = {addr}; |
Rhyme | 0:d895cd1cd897 | 107 | __disable_irq() ; // Disable Interrupts |
Rhyme | 0:d895cd1cd897 | 108 | result = p_i2c->write(m_addr, t, 1, true); |
Rhyme | 0:d895cd1cd897 | 109 | if (result == 0) { |
Rhyme | 0:d895cd1cd897 | 110 | result = p_i2c->read(m_addr, (char *)data, len); |
Rhyme | 0:d895cd1cd897 | 111 | } |
Rhyme | 0:d895cd1cd897 | 112 | __enable_irq() ; // Enable Interrupts |
Rhyme | 0:d895cd1cd897 | 113 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 114 | } |
Rhyme | 0:d895cd1cd897 | 115 | |
Rhyme | 0:d895cd1cd897 | 116 | int LM75B::writeRegs(uint8_t * data, int len) { |
Rhyme | 0:d895cd1cd897 | 117 | int result ; |
Rhyme | 0:d895cd1cd897 | 118 | __disable_irq() ; // Disable Interrupts |
Rhyme | 0:d895cd1cd897 | 119 | result = p_i2c->write(m_addr, (char *)data, len); |
Rhyme | 0:d895cd1cd897 | 120 | __enable_irq() ; // Enable Interrupts |
Rhyme | 0:d895cd1cd897 | 121 | return( result ) ; |
Rhyme | 0:d895cd1cd897 | 122 | } |