Class module for NCP5623B I2C LED driver
Dependents: mDotEVBM2X MTDOT-EVB-LinkCheck-AL MTDOT-EVBDemo-DRH MTDOT_BOX_EVB_Blinky ... more
NCP5623B.cpp@0:b28a2dfe05fd, 2015-07-06 (annotated)
- Committer:
- falingtrea
- Date:
- Mon Jul 06 19:29:08 2015 +0000
- Revision:
- 0:b28a2dfe05fd
- Child:
- 1:f0efe8462d0e
Initial release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
falingtrea | 0:b28a2dfe05fd | 1 | /** |
falingtrea | 0:b28a2dfe05fd | 2 | * @file NCP5623B.cpp |
falingtrea | 0:b28a2dfe05fd | 3 | * @brief Device driver - NCP5623B Triple LED Driver IC w/RTOS support |
falingtrea | 0:b28a2dfe05fd | 4 | * @author Tim Barr |
falingtrea | 0:b28a2dfe05fd | 5 | * @version 1.0 |
falingtrea | 0:b28a2dfe05fd | 6 | * @see http://www.onsemi.com/pub/Collateral/NCP5623B-D.PDF |
falingtrea | 0:b28a2dfe05fd | 7 | * |
falingtrea | 0:b28a2dfe05fd | 8 | * Copyright (c) 2015 |
falingtrea | 0:b28a2dfe05fd | 9 | * |
falingtrea | 0:b28a2dfe05fd | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
falingtrea | 0:b28a2dfe05fd | 11 | * you may not use this file except in compliance with the License. |
falingtrea | 0:b28a2dfe05fd | 12 | * You may obtain a copy of the License at |
falingtrea | 0:b28a2dfe05fd | 13 | * |
falingtrea | 0:b28a2dfe05fd | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
falingtrea | 0:b28a2dfe05fd | 15 | * |
falingtrea | 0:b28a2dfe05fd | 16 | * Unless required by applicable law or agreed to in writing, software |
falingtrea | 0:b28a2dfe05fd | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
falingtrea | 0:b28a2dfe05fd | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
falingtrea | 0:b28a2dfe05fd | 19 | * See the License for the specific language governing permissions and |
falingtrea | 0:b28a2dfe05fd | 20 | * limitations under the License. |
falingtrea | 0:b28a2dfe05fd | 21 | */ |
falingtrea | 0:b28a2dfe05fd | 22 | |
falingtrea | 0:b28a2dfe05fd | 23 | #include "NCP5623B.h" |
falingtrea | 0:b28a2dfe05fd | 24 | #include "mbed_debug.h" |
falingtrea | 0:b28a2dfe05fd | 25 | #include "rtos.h" |
falingtrea | 0:b28a2dfe05fd | 26 | |
falingtrea | 0:b28a2dfe05fd | 27 | NCP5623B::NCP5623B(I2C &i2c) |
falingtrea | 0:b28a2dfe05fd | 28 | { |
falingtrea | 0:b28a2dfe05fd | 29 | _i2c = &i2c; |
falingtrea | 0:b28a2dfe05fd | 30 | |
falingtrea | 0:b28a2dfe05fd | 31 | NCP5623B::init(); |
falingtrea | 0:b28a2dfe05fd | 32 | |
falingtrea | 0:b28a2dfe05fd | 33 | return; |
falingtrea | 0:b28a2dfe05fd | 34 | } |
falingtrea | 0:b28a2dfe05fd | 35 | |
falingtrea | 0:b28a2dfe05fd | 36 | uint8_t NCP5623B::init(void) |
falingtrea | 0:b28a2dfe05fd | 37 | { |
falingtrea | 0:b28a2dfe05fd | 38 | uint8_t result = 0; |
falingtrea | 0:b28a2dfe05fd | 39 | |
falingtrea | 0:b28a2dfe05fd | 40 | _i2c->frequency(400000); |
falingtrea | 0:b28a2dfe05fd | 41 | |
falingtrea | 0:b28a2dfe05fd | 42 | // Turn off all LEDs and initialize all registers |
falingtrea | 0:b28a2dfe05fd | 43 | result |= NCP5623B::writeRegister(NCP5623B::DIMDWNSET, 0x00); |
falingtrea | 0:b28a2dfe05fd | 44 | result |= NCP5623B::writeRegister(NCP5623B::DIMTIME, 0x01); |
falingtrea | 0:b28a2dfe05fd | 45 | osDelay (1000); |
falingtrea | 0:b28a2dfe05fd | 46 | result |= NCP5623B::writeRegister(NCP5623B::LEDCURR, 0x00); |
falingtrea | 0:b28a2dfe05fd | 47 | result |= NCP5623B::writeRegister(NCP5623B::PWMLED1, 0x00); |
falingtrea | 0:b28a2dfe05fd | 48 | result |= NCP5623B::writeRegister(NCP5623B::PWMLED2, 0x00); |
falingtrea | 0:b28a2dfe05fd | 49 | result |= NCP5623B::writeRegister(NCP5623B::PWMLED3, 0x00); |
falingtrea | 0:b28a2dfe05fd | 50 | |
falingtrea | 0:b28a2dfe05fd | 51 | if(result != 0) |
falingtrea | 0:b28a2dfe05fd | 52 | { |
falingtrea | 0:b28a2dfe05fd | 53 | debug("ILS29011:init failed\n\r"); |
falingtrea | 0:b28a2dfe05fd | 54 | } |
falingtrea | 0:b28a2dfe05fd | 55 | |
falingtrea | 0:b28a2dfe05fd | 56 | return result; |
falingtrea | 0:b28a2dfe05fd | 57 | } |
falingtrea | 0:b28a2dfe05fd | 58 | |
falingtrea | 0:b28a2dfe05fd | 59 | /** Shutdown LEDS |
falingtrea | 0:b28a2dfe05fd | 60 | * @return status of command |
falingtrea | 0:b28a2dfe05fd | 61 | */ |
falingtrea | 0:b28a2dfe05fd | 62 | uint8_t NCP5623B::shutdown(void) const |
falingtrea | 0:b28a2dfe05fd | 63 | { |
falingtrea | 0:b28a2dfe05fd | 64 | uint8_t result = 0; |
falingtrea | 0:b28a2dfe05fd | 65 | |
falingtrea | 0:b28a2dfe05fd | 66 | result |= NCP5623B::writeRegister(NCP5623B::SHUTDWN, 0x00); |
falingtrea | 0:b28a2dfe05fd | 67 | return result; |
falingtrea | 0:b28a2dfe05fd | 68 | |
falingtrea | 0:b28a2dfe05fd | 69 | } |
falingtrea | 0:b28a2dfe05fd | 70 | |
falingtrea | 0:b28a2dfe05fd | 71 | /** Set static LED Current |
falingtrea | 0:b28a2dfe05fd | 72 | * data - value of current draw for all LEDs range 0-31 |
falingtrea | 0:b28a2dfe05fd | 73 | * @return status of command |
falingtrea | 0:b28a2dfe05fd | 74 | */ |
falingtrea | 0:b28a2dfe05fd | 75 | uint8_t NCP5623B::setLEDCurrent(uint8_t data) const |
falingtrea | 0:b28a2dfe05fd | 76 | { |
falingtrea | 0:b28a2dfe05fd | 77 | uint8_t result = 0; |
falingtrea | 0:b28a2dfe05fd | 78 | |
falingtrea | 0:b28a2dfe05fd | 79 | result |= NCP5623B::writeRegister(NCP5623B::LEDCURR, data); |
falingtrea | 0:b28a2dfe05fd | 80 | return result; |
falingtrea | 0:b28a2dfe05fd | 81 | } |
falingtrea | 0:b28a2dfe05fd | 82 | |
falingtrea | 0:b28a2dfe05fd | 83 | /** Set PWM mode for specific LED |
falingtrea | 0:b28a2dfe05fd | 84 | * @lednum - selects LED |
falingtrea | 0:b28a2dfe05fd | 85 | * @data - PWM value to set range 0-31 0-100% Pulse width |
falingtrea | 0:b28a2dfe05fd | 86 | * @return status of command |
falingtrea | 0:b28a2dfe05fd | 87 | */ |
falingtrea | 0:b28a2dfe05fd | 88 | uint8_t NCP5623B::setPWM(LEDNUM lednum, int8_t data ) const |
falingtrea | 0:b28a2dfe05fd | 89 | { |
falingtrea | 0:b28a2dfe05fd | 90 | uint8_t result = 0; |
falingtrea | 0:b28a2dfe05fd | 91 | |
falingtrea | 0:b28a2dfe05fd | 92 | switch (lednum) { |
falingtrea | 0:b28a2dfe05fd | 93 | case NCP5623B::LED_1: |
falingtrea | 0:b28a2dfe05fd | 94 | result |= NCP5623B::writeRegister(NCP5623B::PWMLED1, data); |
falingtrea | 0:b28a2dfe05fd | 95 | break; |
falingtrea | 0:b28a2dfe05fd | 96 | case NCP5623B::LED_2: |
falingtrea | 0:b28a2dfe05fd | 97 | result |= NCP5623B::writeRegister(NCP5623B::PWMLED2, data); |
falingtrea | 0:b28a2dfe05fd | 98 | break; |
falingtrea | 0:b28a2dfe05fd | 99 | case NCP5623B::LED_3: |
falingtrea | 0:b28a2dfe05fd | 100 | result |= NCP5623B::writeRegister(NCP5623B::PWMLED3, data); |
falingtrea | 0:b28a2dfe05fd | 101 | break; |
falingtrea | 0:b28a2dfe05fd | 102 | } |
falingtrea | 0:b28a2dfe05fd | 103 | return result; |
falingtrea | 0:b28a2dfe05fd | 104 | } |
falingtrea | 0:b28a2dfe05fd | 105 | |
falingtrea | 0:b28a2dfe05fd | 106 | /** Set Dimming mode for all LEDs |
falingtrea | 0:b28a2dfe05fd | 107 | * @dimdir - direction of dimming |
falingtrea | 0:b28a2dfe05fd | 108 | * @endstep - ending step of ramp up or ramp down range 0-31 |
falingtrea | 0:b28a2dfe05fd | 109 | * @steptime - time per step range 0-31 in 8 msec multiples |
falingtrea | 0:b28a2dfe05fd | 110 | * @return status of command |
falingtrea | 0:b28a2dfe05fd | 111 | */ |
falingtrea | 0:b28a2dfe05fd | 112 | uint8_t NCP5623B::setDimming(DIMDIRECTION dimdir, uint8_t endstep, uint8_t steptime) const |
falingtrea | 0:b28a2dfe05fd | 113 | { |
falingtrea | 0:b28a2dfe05fd | 114 | uint8_t result = 0; |
falingtrea | 0:b28a2dfe05fd | 115 | |
falingtrea | 0:b28a2dfe05fd | 116 | if (dimdir == NCP5623B::DIMDWN) |
falingtrea | 0:b28a2dfe05fd | 117 | result |= NCP5623B::writeRegister(NCP5623B::DIMDWNSET, endstep); |
falingtrea | 0:b28a2dfe05fd | 118 | else |
falingtrea | 0:b28a2dfe05fd | 119 | result |= NCP5623B::writeRegister(NCP5623B::DIMUPSET, endstep); |
falingtrea | 0:b28a2dfe05fd | 120 | |
falingtrea | 0:b28a2dfe05fd | 121 | result |= NCP5623B::writeRegister(NCP5623B::DIMTIME, steptime); |
falingtrea | 0:b28a2dfe05fd | 122 | |
falingtrea | 0:b28a2dfe05fd | 123 | return result; |
falingtrea | 0:b28a2dfe05fd | 124 | } |
falingtrea | 0:b28a2dfe05fd | 125 | /** Write to a register (exposed for debugging reasons) |
falingtrea | 0:b28a2dfe05fd | 126 | * @param reg - The register to be written |
falingtrea | 0:b28a2dfe05fd | 127 | * @param data - The data to be written |
falingtrea | 0:b28a2dfe05fd | 128 | */ |
falingtrea | 0:b28a2dfe05fd | 129 | uint8_t NCP5623B::writeRegister(NCP5623B::REGISTER const reg, uint8_t const data) const |
falingtrea | 0:b28a2dfe05fd | 130 | { |
falingtrea | 0:b28a2dfe05fd | 131 | char buf[1]; |
falingtrea | 0:b28a2dfe05fd | 132 | uint8_t result = 0; |
falingtrea | 0:b28a2dfe05fd | 133 | |
falingtrea | 0:b28a2dfe05fd | 134 | buf[0] = reg | (data & NCP5623B::DATAMASK); |
falingtrea | 0:b28a2dfe05fd | 135 | |
falingtrea | 0:b28a2dfe05fd | 136 | // __disable_irq(); // // Tickers and other timebase events can jack up the I2C bus for some devices |
falingtrea | 0:b28a2dfe05fd | 137 | result |= _i2c->write(_i2c_addr, buf, 1); |
falingtrea | 0:b28a2dfe05fd | 138 | // __enable_irq(); // Just need to block during the transaction |
falingtrea | 0:b28a2dfe05fd | 139 | |
falingtrea | 0:b28a2dfe05fd | 140 | if(result != 0) |
falingtrea | 0:b28a2dfe05fd | 141 | { |
falingtrea | 0:b28a2dfe05fd | 142 | debug("NCP5623B:writeRegister failed\n\r"); |
falingtrea | 0:b28a2dfe05fd | 143 | } |
falingtrea | 0:b28a2dfe05fd | 144 | |
falingtrea | 0:b28a2dfe05fd | 145 | return result; |
falingtrea | 0:b28a2dfe05fd | 146 | } |
falingtrea | 0:b28a2dfe05fd | 147 | |
falingtrea | 0:b28a2dfe05fd | 148 |