fix iar warning

This commit is contained in:
hathach
2014-03-12 14:01:38 +07:00
parent c92b03edfb
commit 8f03dea95a
22 changed files with 3972 additions and 3014 deletions
@@ -1,175 +1,175 @@
/**************************************************************************/
/*!
@file board_ea4357.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#include "../../board.h"
#if BOARD == BOARD_EA4357
#define BOARD_UART_PORT LPC_USART0
#define BOARD_UART_PIN_PORT 0x0f
#define BOARD_UART_PIN_TX 10 // PF.10 : UART0_TXD
#define BOARD_UART_PIN_RX 11 // PF.11 : UART0_RXD
const static struct {
uint8_t mux_port;
uint8_t mux_pin;
uint8_t gpio_port;
uint8_t gpio_pin;
}buttons[] =
{
{0x0a, 3, 4, 10 }, // Joystick up
{0x09, 1, 4, 13 }, // Joystick down
{0x0a, 2, 4, 9 }, // Joystick left
{0x09, 0, 4, 12 }, // Joystick right
{0x0a, 1, 4, 8 }, // Joystick press
{0x02, 7, 0, 7 }, // SW6
};
enum {
BOARD_BUTTON_COUNT = sizeof(buttons) / sizeof(buttons[0])
};
void board_init(void)
{
CGU_Init();
#if TUSB_CFG_OS == TUSB_OS_NONE // TODO may move to main.c
SysTick_Config(CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE) / CFG_TICKS_PER_SECOND); // 1 msec tick timer
#endif
//------------- USB -------------//
// USB0 Power: EA4357 channel B U20 GPIO26 active low (base board), P2_3 on LPC4357
scu_pinmux(0x02, 3, MD_PUP | MD_EZI, FUNC7); // USB0 VBus Power
#if TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE
scu_pinmux(0x09, 5, GPIO_PDN, FUNC4); // P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low.
GPIO_SetDir(5, BIT_(18), 1);
#endif
// USB1 Power: EA4357 channel A U20 is enabled by SJ5 connected to pad 1-2, no more action required
// TODO Remove R170, R171, solder a pair of 15K to USB1 D+/D- to test with USB1 Host
//------------- LED -------------//
I2C_Init(LPC_I2C0, 100000);
I2C_Cmd(LPC_I2C0, ENABLE);
pca9532_init();
//------------- BUTTON -------------//
for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++)
{
scu_pinmux(buttons[i].mux_port, buttons[i].mux_pin, GPIO_NOPULL, FUNC0);
GPIO_SetDir(buttons[i].gpio_port, BIT_(buttons[i].gpio_pin), 0);
}
//------------- UART -------------//
scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_TX, MD_PDN, FUNC1);
scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_RX, MD_PLN | MD_EZI | MD_ZI, FUNC1);
UART_CFG_Type UARTConfigStruct;
UART_ConfigStructInit(&UARTConfigStruct);
UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE;
UARTConfigStruct.Clock_Speed = 0;
UART_Init(BOARD_UART_PORT, &UARTConfigStruct);
UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit
//------------- NAND Flash (K9FXX) Size = 128M, Page Size = 2K, Block Size = 128K, Number of Block = 1024 -------------//
// nand_init();
#if 0
//------------- Ethernet -------------//
LPC_CREG->CREG6 &= ~0x7;
/* RMII mode setup only */
LPC_CREG->CREG6 |= 0x4;
scu_pinmux(0x1, 18, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD0
scu_pinmux(0x1, 20, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD1
scu_pinmux(0x0, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC6); // ENET TX Enable
scu_pinmux(0x1, 15, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET RXD0
scu_pinmux(0x0, 0 , (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC2); // ENET RXD1
scu_pinmux(0x1, 16, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC7); // ENET RX Data Valid
scu_pinmux(0x1, 19, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC0); // ENET REF CLK
scu_pinmux(0x1, 17, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET MDIO
scu_pinmux(0xC, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET MDC
#endif
}
//--------------------------------------------------------------------+
// LEDS
//--------------------------------------------------------------------+
void board_leds(uint32_t on_mask, uint32_t off_mask)
{
pca9532_setLeds( on_mask << 8, off_mask << 8);
}
//--------------------------------------------------------------------+
// BUTTONS
//--------------------------------------------------------------------+
static bool button_read(uint8_t id)
{
return !BIT_TEST_( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low
}
uint32_t board_buttons(void)
{
uint32_t result = 0;
for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++) result |= (button_read(i) ? BIT_(i) : 0);
return result;
}
//--------------------------------------------------------------------+
// UART
//--------------------------------------------------------------------+
uint8_t board_uart_getchar(void)
{
return UART_ReceiveByte(BOARD_UART_PORT);
}
void board_uart_putchar(uint8_t c)
{
UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING);
}
#endif
/**************************************************************************/
/*!
@file board_ea4357.c
@author hathach (tinyusb.org)
@section LICENSE
Software License Agreement (BSD License)
Copyright (c) 2013, hathach (tinyusb.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holders nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
INCLUDING NEGLIGENCE OR OTHERWISE ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
This file is part of the tinyusb stack.
*/
/**************************************************************************/
#include "../../board.h"
#if BOARD == BOARD_EA4357
#define BOARD_UART_PORT LPC_USART0
#define BOARD_UART_PIN_PORT 0x0f
#define BOARD_UART_PIN_TX 10 // PF.10 : UART0_TXD
#define BOARD_UART_PIN_RX 11 // PF.11 : UART0_RXD
const static struct {
uint8_t mux_port;
uint8_t mux_pin;
uint8_t gpio_port;
uint8_t gpio_pin;
}buttons[] =
{
{0x0a, 3, 4, 10 }, // Joystick up
{0x09, 1, 4, 13 }, // Joystick down
{0x0a, 2, 4, 9 }, // Joystick left
{0x09, 0, 4, 12 }, // Joystick right
{0x0a, 1, 4, 8 }, // Joystick press
{0x02, 7, 0, 7 }, // SW6
};
enum {
BOARD_BUTTON_COUNT = sizeof(buttons) / sizeof(buttons[0])
};
void board_init(void)
{
CGU_Init();
#if TUSB_CFG_OS == TUSB_OS_NONE // TODO may move to main.c
SysTick_Config(CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE) / CFG_TICKS_PER_SECOND); // 1 msec tick timer
#endif
//------------- USB -------------//
// USB0 Power: EA4357 channel B U20 GPIO26 active low (base board), P2_3 on LPC4357
scu_pinmux(0x02, 3, MD_PUP | MD_EZI, FUNC7); // USB0 VBus Power
#if TUSB_CFG_CONTROLLER_0_MODE & TUSB_MODE_DEVICE
scu_pinmux(0x09, 5, GPIO_PDN, FUNC4); // P9_5 (GPIO5[18]) (GPIO28 on oem base) as USB connect, active low.
GPIO_SetDir(5, BIT_(18), 1);
#endif
// USB1 Power: EA4357 channel A U20 is enabled by SJ5 connected to pad 1-2, no more action required
// TODO Remove R170, R171, solder a pair of 15K to USB1 D+/D- to test with USB1 Host
//------------- LED -------------//
I2C_Init(LPC_I2C0, 100000);
I2C_Cmd(LPC_I2C0, ENABLE);
pca9532_init();
//------------- BUTTON -------------//
for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++)
{
scu_pinmux(buttons[i].mux_port, buttons[i].mux_pin, GPIO_NOPULL, FUNC0);
GPIO_SetDir(buttons[i].gpio_port, BIT_(buttons[i].gpio_pin), 0);
}
//------------- UART -------------//
scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_TX, MD_PDN, FUNC1);
scu_pinmux(BOARD_UART_PIN_PORT, BOARD_UART_PIN_RX, MD_PLN | MD_EZI | MD_ZI, FUNC1);
UART_CFG_Type UARTConfigStruct;
UART_ConfigStructInit(&UARTConfigStruct);
UARTConfigStruct.Baud_rate = CFG_UART_BAUDRATE;
UARTConfigStruct.Clock_Speed = 0;
UART_Init(BOARD_UART_PORT, &UARTConfigStruct);
UART_TxCmd(BOARD_UART_PORT, ENABLE); // Enable UART Transmit
//------------- NAND Flash (K9FXX) Size = 128M, Page Size = 2K, Block Size = 128K, Number of Block = 1024 -------------//
// nand_init();
#if 0
//------------- Ethernet -------------//
LPC_CREG->CREG6 &= ~0x7;
/* RMII mode setup only */
LPC_CREG->CREG6 |= 0x4;
scu_pinmux(0x1, 18, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD0
scu_pinmux(0x1, 20, (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET TXD1
scu_pinmux(0x0, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC6); // ENET TX Enable
scu_pinmux(0x1, 15, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET RXD0
scu_pinmux(0x0, 0 , (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC2); // ENET RXD1
scu_pinmux(0x1, 16, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC7); // ENET RX Data Valid
scu_pinmux(0x1, 19, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC0); // ENET REF CLK
scu_pinmux(0x1, 17, (MD_EHS | MD_PLN | MD_EZI | MD_ZI), FUNC3); // ENET MDIO
scu_pinmux(0xC, 1 , (MD_EHS | MD_PLN | MD_ZI) , FUNC3); // ENET MDC
#endif
}
//--------------------------------------------------------------------+
// LEDS
//--------------------------------------------------------------------+
void board_leds(uint32_t on_mask, uint32_t off_mask)
{
pca9532_setLeds( on_mask << 8, off_mask << 8);
}
//--------------------------------------------------------------------+
// BUTTONS
//--------------------------------------------------------------------+
static bool button_read(uint8_t id)
{
return !BIT_TEST_( GPIO_ReadValue(buttons[id].gpio_port), buttons[id].gpio_pin ); // button is active low
}
uint32_t board_buttons(void)
{
uint32_t result = 0;
for(uint8_t i=0; i<BOARD_BUTTON_COUNT; i++) result |= (button_read(i) ? BIT_(i) : 0);
return result;
}
//--------------------------------------------------------------------+
// UART
//--------------------------------------------------------------------+
uint8_t board_uart_getchar(void)
{
return UART_ReceiveByte(BOARD_UART_PORT);
}
void board_uart_putchar(uint8_t c)
{
UART_Send(BOARD_UART_PORT, &c, 1, BLOCKING);
}
#endif
@@ -1,349 +1,349 @@
/*****************************************************************************
*
* Copyright(C) 2011, Embedded Artists AB
* All rights reserved.
*
******************************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* Embedded Artists AB assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. Embedded Artists AB
* reserves the right to make changes in the software without
* notification. Embedded Artists AB also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
*****************************************************************************/
/*
* NOTE: I2C must have been initialized before calling any functions in this
* file.
*/
/******************************************************************************
* Includes
*****************************************************************************/
#include "../../board.h"
#if BOARD == BOARD_EA4357
#include "lpc43xx_i2c.h"
#include "lpc43xx_cgu.h"
#include "lpc_types.h"
#include "pca9532.h"
/******************************************************************************
* Defines and typedefs
*****************************************************************************/
#define I2C_PORT (LPC_I2C0)
#define LS_MODE_ON 0x01
#define LS_MODE_BLINK0 0x02
#define LS_MODE_BLINK1 0x03
/******************************************************************************
* External global variables
*****************************************************************************/
/******************************************************************************
* Local variables
*****************************************************************************/
static uint16_t blink0Shadow = 0;
static uint16_t blink1Shadow = 0;
static uint16_t ledStateShadow = 0;
/******************************************************************************
* Local Functions
*****************************************************************************/
static Status I2CWrite(uint32_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type i2cData;
i2cData.sl_addr7bit = addr;
i2cData.tx_data = buf;
i2cData.tx_length = len;
i2cData.rx_data = NULL;
i2cData.rx_length = 0;
i2cData.retransmissions_max = 3;
return I2C_MasterTransferData(I2C_PORT, &i2cData, I2C_TRANSFER_POLLING);
}
static Status I2CRead(uint32_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type i2cData;
i2cData.sl_addr7bit = addr;
i2cData.tx_data = NULL;
i2cData.tx_length = 0;
i2cData.rx_data = buf;
i2cData.rx_length = len;
i2cData.retransmissions_max = 3;
return I2C_MasterTransferData(I2C_PORT, &i2cData, I2C_TRANSFER_POLLING);
}
static void setLsStates(uint16_t states, uint8_t* ls, uint8_t mode)
{
#define IS_LED_SET(bit, x) ( ( ((x) & (bit)) != 0 ) ? 1 : 0 )
int i = 0;
for (i = 0; i < 4; i++) {
ls[i] |= ( (IS_LED_SET(0x0001, states)*mode << 0)
| (IS_LED_SET(0x0002, states)*mode << 2)
| (IS_LED_SET(0x0004, states)*mode << 4)
| (IS_LED_SET(0x0008, states)*mode << 6) );
states >>= 4;
}
}
static void setLeds(void)
{
uint8_t buf[5];
uint8_t ls[4] = {0,0,0,0};
uint16_t states = ledStateShadow;
/* LEDs in On/Off state */
setLsStates(states, ls, LS_MODE_ON);
/* set the LEDs that should blink */
setLsStates(blink0Shadow, ls, LS_MODE_BLINK0);
setLsStates(blink1Shadow, ls, LS_MODE_BLINK1);
buf[0] = PCA9532_LS0 | PCA9532_AUTO_INC;
buf[1] = ls[0];
buf[2] = ls[1];
buf[3] = ls[2];
buf[4] = ls[3];
I2CWrite(PCA9532_I2C_ADDR, buf, 5);
}
/******************************************************************************
* Public Functions
*****************************************************************************/
/******************************************************************************
*
* Description:
* Initialize the PCA9532 Device
*
*****************************************************************************/
void pca9532_init (void)
{
/* nothing to initialize */
}
/******************************************************************************
*
* Description:
* Get the LED states
*
* Params:
* [in] shadow - TRUE if the states should be retrieved from the shadow
* variables. The shadow variable are updated when any
* of setLeds, setBlink0Leds and/or setBlink1Leds are
* called.
*
* FALSE if the state should be retrieved from the PCA9532
* device. A blinkin LED may be reported as on or off
* depending on the state when calling the function.
*
* Returns:
* A mask where a 1 indicates that a LED is on (or blinking).
*
*****************************************************************************/
uint16_t pca9532_getLedState (uint32_t shadow)
{
uint8_t buf[2];
uint16_t ret = 0;
if (shadow) {
/* a blink LED is reported as on*/
ret = (ledStateShadow | blink0Shadow | blink1Shadow);
}
else {
/*
* A blinking LED may be reported as on or off depending on
* its state when reading the Input register.
*/
buf[0] = PCA9532_INPUT0;
I2CWrite(PCA9532_I2C_ADDR, buf, 1);
I2CRead(PCA9532_I2C_ADDR, buf, 1);
ret = buf[0];
buf[0] = PCA9532_INPUT1;
I2CWrite(PCA9532_I2C_ADDR, buf, 1);
I2CRead(PCA9532_I2C_ADDR, buf, 1);
ret |= (buf[0] << 8);
/* invert since LEDs are active low */
ret = ((~ret) & 0xFFFF);
}
return (ret & ~PCA9532_NOT_USED);
}
/******************************************************************************
*
* Description:
* Set LED states (on or off).
*
* Params:
* [in] ledOnMask - The LEDs that should be turned on. This mask has
* priority over ledOffMask
* [in] ledOffMask - The LEDs that should be turned off.
*
*****************************************************************************/
void pca9532_setLeds (uint16_t ledOnMask, uint16_t ledOffMask)
{
/* turn off leds */
ledStateShadow &= (~(ledOffMask) & 0xffff);
/* ledOnMask has priority over ledOffMask */
ledStateShadow |= ledOnMask;
/* turn off blinking */
blink0Shadow &= (~(ledOffMask) & 0xffff);
blink1Shadow &= (~(ledOffMask) & 0xffff);
setLeds();
}
/******************************************************************************
*
* Description:
* Set the blink period for PWM0. Valid values are 0 - 255 where 0
* means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz.
*
* Params:
* [in] period - the period for pwm0
*
*****************************************************************************/
void pca9532_setBlink0Period(uint8_t period)
{
uint8_t buf[2];
buf[0] = PCA9532_PSC0;
buf[1] = period;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the duty cycle for PWM0. Valid values are 0 - 100. 25 means the LED
* is on 25% of the period.
*
* Params:
* [in] duty - duty cycle
*
*****************************************************************************/
void pca9532_setBlink0Duty(uint8_t duty)
{
uint8_t buf[2];
uint32_t tmp = duty;
if (tmp > 100) {
tmp = 100;
}
tmp = (256 * tmp)/100;
buf[0] = PCA9532_PWM0;
buf[1] = tmp;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the LEDs that should blink with rate and duty cycle from PWM0.
* Blinking is turned off with pca9532_setLeds.
*
* Params:
* [in] ledMask - LEDs that should blink.
*
*****************************************************************************/
void pca9532_setBlink0Leds(uint16_t ledMask)
{
blink0Shadow |= ledMask;
setLeds();
}
/******************************************************************************
*
* Description:
* Set the blink period for PWM1. Valid values are 0 - 255 where 0
* means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz.
*
* Params:
* [in] period - The period for PWM1
*
*****************************************************************************/
void pca9532_setBlink1Period(uint8_t period)
{
uint8_t buf[2];
buf[0] = PCA9532_PSC1;
buf[1] = period;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the duty cycle for PWM1. Valid values are 0 - 100. 25 means the LED
* is on 25% of the period.
*
* Params:
* [in] duty - duty cycle.
*
*****************************************************************************/
void pca9532_setBlink1Duty(uint8_t duty)
{
uint8_t buf[2];
uint32_t tmp = duty;
if (tmp > 100) {
tmp = 100;
}
tmp = (256 * tmp)/100;
buf[0] = PCA9532_PWM1;
buf[1] = tmp;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the LEDs that should blink with rate and duty cycle from PWM1.
* Blinking is turned off with pca9532_setLeds.
*
* Params:
* [in] ledMask - LEDs that should blink.
*
*****************************************************************************/
void pca9532_setBlink1Leds(uint16_t ledMask)
{
blink1Shadow |= ledMask;
setLeds();
}
#endif
/*****************************************************************************
*
* Copyright(C) 2011, Embedded Artists AB
* All rights reserved.
*
******************************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* Embedded Artists AB assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. Embedded Artists AB
* reserves the right to make changes in the software without
* notification. Embedded Artists AB also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
*****************************************************************************/
/*
* NOTE: I2C must have been initialized before calling any functions in this
* file.
*/
/******************************************************************************
* Includes
*****************************************************************************/
#include "../../board.h"
#if BOARD == BOARD_EA4357
#include "lpc43xx_i2c.h"
#include "lpc43xx_cgu.h"
#include "lpc_types.h"
#include "pca9532.h"
/******************************************************************************
* Defines and typedefs
*****************************************************************************/
#define I2C_PORT (LPC_I2C0)
#define LS_MODE_ON 0x01
#define LS_MODE_BLINK0 0x02
#define LS_MODE_BLINK1 0x03
/******************************************************************************
* External global variables
*****************************************************************************/
/******************************************************************************
* Local variables
*****************************************************************************/
static uint16_t blink0Shadow = 0;
static uint16_t blink1Shadow = 0;
static uint16_t ledStateShadow = 0;
/******************************************************************************
* Local Functions
*****************************************************************************/
static Status I2CWrite(uint32_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type i2cData;
i2cData.sl_addr7bit = addr;
i2cData.tx_data = buf;
i2cData.tx_length = len;
i2cData.rx_data = NULL;
i2cData.rx_length = 0;
i2cData.retransmissions_max = 3;
return I2C_MasterTransferData(I2C_PORT, &i2cData, I2C_TRANSFER_POLLING);
}
static Status I2CRead(uint32_t addr, uint8_t* buf, uint32_t len)
{
I2C_M_SETUP_Type i2cData;
i2cData.sl_addr7bit = addr;
i2cData.tx_data = NULL;
i2cData.tx_length = 0;
i2cData.rx_data = buf;
i2cData.rx_length = len;
i2cData.retransmissions_max = 3;
return I2C_MasterTransferData(I2C_PORT, &i2cData, I2C_TRANSFER_POLLING);
}
static void setLsStates(uint16_t states, uint8_t* ls, uint8_t mode)
{
#define IS_LED_SET(bit, x) ( ( ((x) & (bit)) != 0 ) ? 1 : 0 )
int i = 0;
for (i = 0; i < 4; i++) {
ls[i] |= ( (IS_LED_SET(0x0001, states)*mode << 0)
| (IS_LED_SET(0x0002, states)*mode << 2)
| (IS_LED_SET(0x0004, states)*mode << 4)
| (IS_LED_SET(0x0008, states)*mode << 6) );
states >>= 4;
}
}
static void setLeds(void)
{
uint8_t buf[5];
uint8_t ls[4] = {0,0,0,0};
uint16_t states = ledStateShadow;
/* LEDs in On/Off state */
setLsStates(states, ls, LS_MODE_ON);
/* set the LEDs that should blink */
setLsStates(blink0Shadow, ls, LS_MODE_BLINK0);
setLsStates(blink1Shadow, ls, LS_MODE_BLINK1);
buf[0] = PCA9532_LS0 | PCA9532_AUTO_INC;
buf[1] = ls[0];
buf[2] = ls[1];
buf[3] = ls[2];
buf[4] = ls[3];
I2CWrite(PCA9532_I2C_ADDR, buf, 5);
}
/******************************************************************************
* Public Functions
*****************************************************************************/
/******************************************************************************
*
* Description:
* Initialize the PCA9532 Device
*
*****************************************************************************/
void pca9532_init (void)
{
/* nothing to initialize */
}
/******************************************************************************
*
* Description:
* Get the LED states
*
* Params:
* [in] shadow - TRUE if the states should be retrieved from the shadow
* variables. The shadow variable are updated when any
* of setLeds, setBlink0Leds and/or setBlink1Leds are
* called.
*
* FALSE if the state should be retrieved from the PCA9532
* device. A blinkin LED may be reported as on or off
* depending on the state when calling the function.
*
* Returns:
* A mask where a 1 indicates that a LED is on (or blinking).
*
*****************************************************************************/
uint16_t pca9532_getLedState (uint32_t shadow)
{
uint8_t buf[2];
uint16_t ret = 0;
if (shadow) {
/* a blink LED is reported as on*/
ret = (ledStateShadow | blink0Shadow | blink1Shadow);
}
else {
/*
* A blinking LED may be reported as on or off depending on
* its state when reading the Input register.
*/
buf[0] = PCA9532_INPUT0;
I2CWrite(PCA9532_I2C_ADDR, buf, 1);
I2CRead(PCA9532_I2C_ADDR, buf, 1);
ret = buf[0];
buf[0] = PCA9532_INPUT1;
I2CWrite(PCA9532_I2C_ADDR, buf, 1);
I2CRead(PCA9532_I2C_ADDR, buf, 1);
ret |= (buf[0] << 8);
/* invert since LEDs are active low */
ret = ((~ret) & 0xFFFF);
}
return (ret & ~PCA9532_NOT_USED);
}
/******************************************************************************
*
* Description:
* Set LED states (on or off).
*
* Params:
* [in] ledOnMask - The LEDs that should be turned on. This mask has
* priority over ledOffMask
* [in] ledOffMask - The LEDs that should be turned off.
*
*****************************************************************************/
void pca9532_setLeds (uint16_t ledOnMask, uint16_t ledOffMask)
{
/* turn off leds */
ledStateShadow &= (~(ledOffMask) & 0xffff);
/* ledOnMask has priority over ledOffMask */
ledStateShadow |= ledOnMask;
/* turn off blinking */
blink0Shadow &= (~(ledOffMask) & 0xffff);
blink1Shadow &= (~(ledOffMask) & 0xffff);
setLeds();
}
/******************************************************************************
*
* Description:
* Set the blink period for PWM0. Valid values are 0 - 255 where 0
* means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz.
*
* Params:
* [in] period - the period for pwm0
*
*****************************************************************************/
void pca9532_setBlink0Period(uint8_t period)
{
uint8_t buf[2];
buf[0] = PCA9532_PSC0;
buf[1] = period;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the duty cycle for PWM0. Valid values are 0 - 100. 25 means the LED
* is on 25% of the period.
*
* Params:
* [in] duty - duty cycle
*
*****************************************************************************/
void pca9532_setBlink0Duty(uint8_t duty)
{
uint8_t buf[2];
uint32_t tmp = duty;
if (tmp > 100) {
tmp = 100;
}
tmp = (256 * tmp)/100;
buf[0] = PCA9532_PWM0;
buf[1] = tmp;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the LEDs that should blink with rate and duty cycle from PWM0.
* Blinking is turned off with pca9532_setLeds.
*
* Params:
* [in] ledMask - LEDs that should blink.
*
*****************************************************************************/
void pca9532_setBlink0Leds(uint16_t ledMask)
{
blink0Shadow |= ledMask;
setLeds();
}
/******************************************************************************
*
* Description:
* Set the blink period for PWM1. Valid values are 0 - 255 where 0
* means 152 Hz and 255 means 0.59 Hz. A value of 151 means 1 Hz.
*
* Params:
* [in] period - The period for PWM1
*
*****************************************************************************/
void pca9532_setBlink1Period(uint8_t period)
{
uint8_t buf[2];
buf[0] = PCA9532_PSC1;
buf[1] = period;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the duty cycle for PWM1. Valid values are 0 - 100. 25 means the LED
* is on 25% of the period.
*
* Params:
* [in] duty - duty cycle.
*
*****************************************************************************/
void pca9532_setBlink1Duty(uint8_t duty)
{
uint8_t buf[2];
uint32_t tmp = duty;
if (tmp > 100) {
tmp = 100;
}
tmp = (256 * tmp)/100;
buf[0] = PCA9532_PWM1;
buf[1] = tmp;
I2CWrite(PCA9532_I2C_ADDR, buf, 2);
}
/******************************************************************************
*
* Description:
* Set the LEDs that should blink with rate and duty cycle from PWM1.
* Blinking is turned off with pca9532_setLeds.
*
* Params:
* [in] ledMask - LEDs that should blink.
*
*****************************************************************************/
void pca9532_setBlink1Leds(uint16_t ledMask)
{
blink1Shadow |= ledMask;
setLeds();
}
#endif