RGB LED Driver demo
Dependents: NJU6063_HelloWorld
Diff: NJU6063.cpp
- Revision:
- 3:f83202c2ae59
- Parent:
- 2:137675ac48ab
- Child:
- 4:7a4b9d444b87
--- a/NJU6063.cpp Sun Jan 10 11:49:59 2016 +0000 +++ b/NJU6063.cpp Mon Jan 18 05:07:27 2016 +0000 @@ -1,92 +1,103 @@ #include "NJU6063.h" - +#ifndef TARGET_STM NJU6063::NJU6063( PinName sda, PinName scl, PinName rst) : _i2c(sda, scl), _rst(rst) { - uint32_t i2cAddr = I2C_1; //_i2c.frequency(400000); - _i2c_cr1 = (uint32_t *)i2cAddr; - _i2c_dr = (uint32_t *)(i2cAddr + 0x10); - _i2c_sr1 = (uint32_t *)(i2cAddr + 0x14); - _i2c_sr2 = (uint32_t *)(i2cAddr + 0x18); } void NJU6063::reset(void) { - //_i2c_type->CR1 &= CR1_ACK_Reset; - //SetCR1(0x200); - wait_ms(100); + NJU6063_WAIT_MS; _rst = 0; - wait_ms(100); + NJU6063_WAIT_MS; _rst = 1; } uint8_t NJU6063::set_multi_device(uint8_t n) { + //_myI2c = (I2C_TypeDef*)I2C_1; + //IS_I2C_NO_STRETCH(I2C_NOSTRETCH_ENABLE); + uint8_t ret; char data[3]; - data[0] = 0x00; // Initial chip addres - data[1] = 0x0d; // multi device address + ret = n; + data[0] = 0x00; // Initial chip addres + data[1] = NJU6063_MADRES; // multi device address for (uint8_t i=1; i<=n; i++) { data[2] = i; ack= _i2c.write(NJU6063_SLAVE, data, 3); - wait_us(500*3); + //printf("%d %02X\n\r", i, ack); + NJU6063_WAIT; if (ack) { - n = i-1; + //_myI2c->CR1 = + ret = i-1; + //int tmp = _myI2c->DR; + //__I2C1_FORCE_RESET(); + //__I2C1_RELEASE_RESET(); + //__HAL_I2C_CLEAR_FLAG(&_myI2cH, I2C_FLAG_AF); + //__HAL_I2C_CLEAR_FLAG(&_myI2cH, I2C_FLAG_AF); + //printf("%x %x %x\n\r", _myI2c->CR1, _myI2c->CR2, _myI2c->DR); + break; } + /* + else { + ack= _i2c.write(NJU6063_SLAVE, data, 3); + } + */ } - return(n); + return(ret); } void NJU6063::init(uint8_t chip_addr, uint8_t d) { char data[3]; data[0] = chip_addr; - data[1] = 0x00; + data[1] = NJU6063_INIT; data[2] = d; ack = _i2c.write(NJU6063_SLAVE, data, 3); - wait_us(500*3); + NJU6063_WAIT; } void NJU6063::set_iled(uint8_t chip_addr, uint8_t d1, uint8_t d2, uint8_t d3) { char data[3]; data[0] = chip_addr; - data[1] = 0x01; + data[1] = NJU6063_ILED; data[2] = (0x03&d1) | (0x03&d2)<<2 | (0x03&d3) <<4; ack = _i2c.write(NJU6063_SLAVE, data, 3); - wait_us(500*3); + NJU6063_WAIT; } void NJU6063::set_pwm(uint8_t chip_addr, uint8_t d1, uint8_t d2, uint8_t d3, uint8_t loop, uint8_t son) { char data[7]; data[0] = chip_addr; - data[1] = 0x02; + data[1] = NJU6063_PWM1; data[2] = d1; data[3] = d2; data[4] = d3; data[5] = loop; data[6] = son; ack = _i2c.write(NJU6063_SLAVE, data, 7); - wait_us(500*7); + NJU6063_WAIT; } void NJU6063::dim_start(uint8_t chip_addr) { char data[3]; data[0] = chip_addr; - data[1] = 0x07; // reg addres + data[1] = NJU6063_START; // reg addres data[2] = 0x01; // start ack = _i2c.write(NJU6063_SLAVE, data, 3); - wait_us(500*3); + NJU6063_WAIT; } void NJU6063::dim_stop(uint8_t chip_addr) { char data[3]; data[0] = chip_addr; - data[1] = 0x07; // reg addres + data[1] = NJU6063_START; // reg addres data[2] = 0x02; // stop ack = _i2c.write(NJU6063_SLAVE, data, 3); - wait_us(500*3); + NJU6063_WAIT; } void NJU6063::check_dim(void) @@ -94,15 +105,15 @@ uint8_t timeout = 0x0f; char data[3]; data[0] = 0xff; - data[1] = 0x0b; + data[1] = NJU6063_DCHK; data[2] = 0x00; - /* do { ack = _i2c.write(NJU6063_SLAVE, data, 3); - wait_us(200*3); + NJU6063_WAIT; timeout--; - if (timeout==0) ACK = 1; + if (timeout==0) break; } while(!ack); - */ - wait_ms(500); + NJU6063_WAIT_MS; } +#endif +