//***************************************************************************** // (C) Automotive Lighting Reutlingen GmbH // Tuebinger Strasse 123, 72762 Reutlingen, Germany // // Automotive Lighting Reutlingen GmbH owns all the rights to this work. // This work shall not be copied, reproduced, used, modified, transferred // or its information shall not be disclosed without the prior written // authorization of Automotive Lighting Reutlingen GmbH. //***************************************************************************** //----------------------------------------------------------------------------- /// \file CddLed_NCV7870.cpp /// /// \brief /// /// \descr /// /// \author Alex Mertic (f98665c) /// mailTo:alexandru-laurentiu.mertic(at)marelli.com //----------------------------------------------------------------------------- //============================================================================= // includes //============================================================================= #include #include #include //============================================================================= // macros //============================================================================= #define SPI_BOOST_READ_FRAME(addr) ((uint16)(addr) << 10U) #define SPI_BOOST_WRITE_FRAME(addr, regval) ((uint16)((uint16)(addr) << 11U) | (uint16)(regval) | (uint16)((uint16)1U << 15U )) //============================================================================= // functions //============================================================================= //----------------------------------------------------------------------------- // \brief cdd::led::NCV7870::init // // \descr This function will initialize the NCV7870 drv registers. // // \param bRegValType specifies if specifc initialization has to be performed // on the driver or the default values are used(no init) // // \return void //----------------------------------------------------------------------------- void cdd::led::ncv7870::init() { uint16 u16RxData = 0U; for (uint8 index = 0; index < NCV7870_INIT_REG; index++) { cdd::led::ncv7870::read_write_reg(e_cdd_led_write_reg, NCV7870_Registers[index].boost_reg_addr, (uint16 *)&NCV7870_Registers[index].boost_reg_value, &u16RxData); if (*NCV7870_Registers[index].custom_action) { NCV7870_Registers[index].custom_action(); } } } //----------------------------------------------------------------------------- // \brief read_write_reg // // \descr This function is responsible writing and reading the BOOST registers // // \param spi_request - type of request Read - 0, Write - 1 // puc_addr - pointer to Boost reg addr // puc_tx_data - pointer to data to be sent ovet SPI, discarded if read request // pun_rx_data - pinter to received data over SPI // // \return ucRet - error code //----------------------------------------------------------------------------- uint8 cdd::led::ncv7870::read_write_reg(te_cdd_led_spi_req_type spi_request, uint8 puc_addr, uint16 *puc_tx_data, uint16 *pun_rx_data) { uint8 unRet = 0; uint16 un_spi_tx; if (spi_request == 0U) { un_spi_tx = SPI_BOOST_READ_FRAME(puc_addr); //Calculate parity bit. spi_set_parity_bit(&un_spi_tx, 9U); //Send SPI request. // PRQA S 303 1 // correct usage (comes from the definition of SCB) Spi_Transmit(e_active_config_BOOST, &un_spi_tx, pun_rx_data, 1U); } else { un_spi_tx = SPI_BOOST_WRITE_FRAME(puc_addr, *puc_tx_data); //Calculate parity bit. spi_set_parity_bit(&un_spi_tx, 10U); //Send SPI request. // PRQA S 303 1 // correct usage (comes from the definition of SCB) Spi_Transmit(e_active_config_BOOST, &un_spi_tx, pun_rx_data, 1U); } return unRet; } //----------------------------------------------------------------------------- // \brief read_write_reg_all // // \descr This function is responsible writing and reading all the BOOST registers // // \param spi_request - type of request Read - 0, Write - 1 // puc_addr - pointer to Boost reg addr // puc_tx_data - pointer to data to be sent ovet SPI, discarded if read request // pun_rx_data - pinter to received data over SPI // // \return ucRet - error code //----------------------------------------------------------------------------- uint8 cdd::led::ncv7870::read_write_reg_all(te_cdd_led_spi_req_type spi_request, uint16 *puc_tx_data, uint16 *pun_rx_data, uint16* response_len) { uint8 ret = 1; if (spi_request == e_cdd_led_read_reg) { for (uint8 i = 0; i < sizeof(reg_map_NCV7870) / sizeof(reg_map_NCV7870[0]); ++i) { if (reg_map_NCV7870[i].read_write == reg_read_only || reg_map_NCV7870[i].read_write == reg_read_write) { ret &= read_write_reg(e_cdd_led_read_reg, reg_map_NCV7870[i].reg_addr, (uint16 *)(puc_tx_data + (i * sizeof(*puc_tx_data))), (uint16 *)(pun_rx_data + (i * sizeof(*pun_rx_data)))); *response_len += 1; } } } if (spi_request == e_cdd_led_write_reg) { for (uint8 i = 0; i < sizeof(reg_map_NCV7870) / sizeof(reg_map_NCV7870[0]); ++i) { if (reg_map_NCV7870[i].read_write == reg_write_only || reg_map_NCV7870[i].read_write == reg_read_write) { ret &= read_write_reg(e_cdd_led_write_reg, reg_map_NCV7870[i].reg_addr, (uint16 *)(puc_tx_data + (i * sizeof(*puc_tx_data))), (uint16 *)(pun_rx_data + (i * sizeof(*pun_rx_data)))); } } } return ret; } //----------------------------------------------------------------------------- // \brief set_params // // \descr This function is responsible setting Boost Enable Register // // \param BoostNr - Boost number [1,2] // status - enable or disable // // \return ucRet - error code //----------------------------------------------------------------------------- uint8 cdd::led::ncv7870::set_params(uint8 BoostNr, uint8 status) { uint8 ucRet = 0xff; uint16 BoostReg5Content = 0; uint16 mask_lut[2][2] = { {0xFFFE, 0x0001}, {0xFFFD, 0x0002} }; uint16 dummy = 0; status = status >= 1U ? 1U : 0U; if (BoostNr == 1 || BoostNr == 2) { ucRet = 0; cdd::led::ncv7870::read_write_reg(e_cdd_led_read_reg, tREG_Boost_NCV7870Reg05_ADDR, &dummy, &BoostReg5Content); status ? BoostReg5Content |= mask_lut[BoostNr - 1][status] : BoostReg5Content &= mask_lut[BoostNr - 1][status]; cdd::led::ncv7870::read_write_reg(e_cdd_led_write_reg, tREG_Boost_NCV7870Reg05_ADDR, &BoostReg5Content, &dummy); } return ucRet; }