Added a GPIO to power on/off for external I2C sensor(s) (with LEDs)

Dependencies:   UniGraphic mbed vt100

18-Jun-2018 外部センサの電源オン・オフ機能は下位互換の為に無効になっていました。 この版で再度有効にしました。

Committer:
Rhyme
Date:
Mon Jun 18 01:56:00 2018 +0000
Revision:
1:8d65cfc3a2e2
Parent:
0:846e2321c637
External sensor power on/off function enabled. (Previously disabled)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:846e2321c637 1 #include "mbed.h"
Rhyme 0:846e2321c637 2 #include "LM75B.h"
Rhyme 0:846e2321c637 3 #include "af_mgr.h"
Rhyme 0:846e2321c637 4
Rhyme 0:846e2321c637 5 /* Register list */
Rhyme 0:846e2321c637 6 #define PTR_CONF 0x01
Rhyme 0:846e2321c637 7 #define PTR_TEMP 0x00
Rhyme 0:846e2321c637 8 #define PTR_TOS 0x03
Rhyme 0:846e2321c637 9 #define PTR_THYST 0x02
Rhyme 0:846e2321c637 10
Rhyme 0:846e2321c637 11 /* Configuration register */
Rhyme 0:846e2321c637 12 /* B[7:5] : Reserved */
Rhyme 0:846e2321c637 13 /* B[4:3] : OS_F_QUE[1:0] OS fault queue value */
Rhyme 0:846e2321c637 14 #define CONFIG_QUE_1 0x00
Rhyme 0:846e2321c637 15 #define CONFIG_QUE_2 (0x01 << 3)
Rhyme 0:846e2321c637 16 #define CONFIG_QUE_4 (0x10 << 3)
Rhyme 0:846e2321c637 17 #define CONFIG_QUE_6 (0x11 << 3)
Rhyme 0:846e2321c637 18 /* B[2] : OS_POL 0 = OS active LOW, 1 = OS active HIGH */
Rhyme 0:846e2321c637 19 #define CONFIG_OS_POL_L 0x00
Rhyme 0:846e2321c637 20 #define CONFIG_OS_POL_H (0x01 << 2)
Rhyme 0:846e2321c637 21 /* B[1] : OS_COMP_INT 0 = OS comparator, 1 = OS interrupt */
Rhyme 0:846e2321c637 22 #define CONFIG_OS_COMP 0x00
Rhyme 0:846e2321c637 23 #define CONFIG_OS_INT (0x01 << 1)
Rhyme 0:846e2321c637 24 /* B[0] : SHUTDOWN 0 = normal, 1 = shutdown */
Rhyme 0:846e2321c637 25 #define CONFIG_NORMARL 0x00
Rhyme 0:846e2321c637 26 #define CONFIG_SHUTDOWN 0x01
Rhyme 0:846e2321c637 27
Rhyme 0:846e2321c637 28 /* Temperature register */
Rhyme 0:846e2321c637 29 /* D[15:5] = 11 bit data 0.125 * temp data */
Rhyme 0:846e2321c637 30 /* D[4:0] : reserved */
Rhyme 0:846e2321c637 31
Rhyme 0:846e2321c637 32 /* Tos register */
Rhyme 0:846e2321c637 33 /* D[15:7] = 9 bit data */
Rhyme 0:846e2321c637 34 /* D[6:0] : reserved */
Rhyme 0:846e2321c637 35
Rhyme 0:846e2321c637 36 /* Thyst register */
Rhyme 0:846e2321c637 37 /* D[15:7] = 9 ibt data */
Rhyme 0:846e2321c637 38 /* D[6:0] : reserved */
Rhyme 0:846e2321c637 39
Rhyme 0:846e2321c637 40 LM75B::LM75B(I2C *i2c, int addr) : m_addr(addr<<1) {
Rhyme 0:846e2321c637 41 p_i2c = i2c ;
Rhyme 0:846e2321c637 42 p_i2c->frequency(100000); /* 100kHz */
Rhyme 0:846e2321c637 43 // activate the peripheral
Rhyme 0:846e2321c637 44 }
Rhyme 0:846e2321c637 45
Rhyme 0:846e2321c637 46 LM75B::~LM75B() { }
Rhyme 0:846e2321c637 47
Rhyme 0:846e2321c637 48 int LM75B::temp(int8_t *temp)
Rhyme 0:846e2321c637 49 {
Rhyme 0:846e2321c637 50 int result ;
Rhyme 0:846e2321c637 51 char t[1] = { 0x00 } ;
Rhyme 0:846e2321c637 52 result = p_i2c->write(m_addr, t, 1, true) ;
Rhyme 0:846e2321c637 53 if (result == 0) {
Rhyme 0:846e2321c637 54 result = p_i2c->read(m_addr, t, 1) ;
Rhyme 0:846e2321c637 55 }
Rhyme 0:846e2321c637 56 if (result == 0) {
Rhyme 0:846e2321c637 57 *temp = (int8_t)t[0] ;
Rhyme 0:846e2321c637 58 }
Rhyme 0:846e2321c637 59 return( result ) ;
Rhyme 0:846e2321c637 60 }
Rhyme 0:846e2321c637 61
Rhyme 0:846e2321c637 62 int LM75B::getTemp(float *temp)
Rhyme 0:846e2321c637 63 {
Rhyme 0:846e2321c637 64 int result ;
Rhyme 0:846e2321c637 65 char t[2] = { 0, 0 } ;
Rhyme 0:846e2321c637 66 int16_t iTemp = 0 ;
Rhyme 0:846e2321c637 67 result = p_i2c->write(m_addr, t, 1) ; /* write pointer byte 0x00 */
Rhyme 0:846e2321c637 68 if (result == 0) {
Rhyme 0:846e2321c637 69 result = p_i2c->read(m_addr, t, 2) ; /* read MSB, LSB */
Rhyme 0:846e2321c637 70 }
Rhyme 0:846e2321c637 71 if (result == 0) {
Rhyme 0:846e2321c637 72 iTemp = (t[0] << 8) | t[1] ;
Rhyme 0:846e2321c637 73 iTemp >>= 5 ;
Rhyme 0:846e2321c637 74 *temp = 0.125 * iTemp ;
Rhyme 0:846e2321c637 75 }
Rhyme 0:846e2321c637 76 return( result ) ;
Rhyme 0:846e2321c637 77 }
Rhyme 0:846e2321c637 78
Rhyme 0:846e2321c637 79 int LM75B::getConfig(uint8_t ptr_byte, uint8_t *config_data)
Rhyme 0:846e2321c637 80 {
Rhyme 0:846e2321c637 81 int result ;
Rhyme 0:846e2321c637 82 char config = 0x00 ; /* default value */
Rhyme 0:846e2321c637 83 result = p_i2c->write(m_addr, (char*)(&ptr_byte), 1, true) ;
Rhyme 0:846e2321c637 84 if (result == 0) {
Rhyme 0:846e2321c637 85 result = p_i2c->read(m_addr, &config, 1) ;
Rhyme 0:846e2321c637 86 }
Rhyme 0:846e2321c637 87 if (result == 0) {
Rhyme 0:846e2321c637 88 *config_data = config ;
Rhyme 0:846e2321c637 89 }
Rhyme 0:846e2321c637 90 return( result ) ;
Rhyme 0:846e2321c637 91 }
Rhyme 0:846e2321c637 92
Rhyme 0:846e2321c637 93 int LM75B::setConfig(uint8_t ptr_byte, uint8_t config_data)
Rhyme 0:846e2321c637 94 {
Rhyme 0:846e2321c637 95 int result ;
Rhyme 0:846e2321c637 96 char t[2] ;
Rhyme 0:846e2321c637 97 t[0] = ptr_byte ;
Rhyme 0:846e2321c637 98 t[1] = config_data ;
Rhyme 0:846e2321c637 99 result = p_i2c->write(m_addr, t, 2, true) ;
Rhyme 0:846e2321c637 100 return( result ) ;
Rhyme 0:846e2321c637 101 }
Rhyme 0:846e2321c637 102
Rhyme 0:846e2321c637 103 int LM75B::readRegs(int addr, uint8_t * data, int len)
Rhyme 0:846e2321c637 104 {
Rhyme 0:846e2321c637 105 int result ;
Rhyme 0:846e2321c637 106 char t[1] = {addr};
Rhyme 0:846e2321c637 107 __disable_irq() ; // Disable Interrupts
Rhyme 0:846e2321c637 108 result = p_i2c->write(m_addr, t, 1, true);
Rhyme 0:846e2321c637 109 if (result == 0) {
Rhyme 0:846e2321c637 110 result = p_i2c->read(m_addr, (char *)data, len);
Rhyme 0:846e2321c637 111 }
Rhyme 0:846e2321c637 112 __enable_irq() ; // Enable Interrupts
Rhyme 0:846e2321c637 113 return( result ) ;
Rhyme 0:846e2321c637 114 }
Rhyme 0:846e2321c637 115
Rhyme 0:846e2321c637 116 int LM75B::writeRegs(uint8_t * data, int len) {
Rhyme 0:846e2321c637 117 int result ;
Rhyme 0:846e2321c637 118 __disable_irq() ; // Disable Interrupts
Rhyme 0:846e2321c637 119 result = p_i2c->write(m_addr, (char *)data, len);
Rhyme 0:846e2321c637 120 __enable_irq() ; // Enable Interrupts
Rhyme 0:846e2321c637 121 return( result ) ;
Rhyme 0:846e2321c637 122 }