📄 bsp.c
字号:
/*********************************************************************
*
* IAR PowerPac - RTOS
*
* (c) Copyright IAR Systems 2007. All rights reserved.
*
**********************************************************************
----------------------------------------------------------------------
File : LED.c (for LPC211x Starter boards and IAR compiler)
Purpose : LED interface for NXP-Giveaway board
-------- END-OF-HEADER ---------------------------------------------
*/
#define BSP_C
#include <NXP/iolpc2119.h>
#include <assert.h>
#include "BSP.h"
#include "RTOS.h"
/*********************************************************************
*
* Defines
*
**********************************************************************
*/
/****** Assign LEDs to Ports ****************************************/
// IAR NXP Giveaway demo board
#define _PORT1_LED1_BIT 23
#define _PORT1_LED2_BIT 22
#define _PORT1_LED3_BIT 21
#define _PORT1_LED4_BIT 20
#define _PORT1_LED5_BIT 19
#define _PORT1_LED6_BIT 18
#define _PORT1_LED7_BIT 17
#define _PORT1_LED8_BIT 16
#define _PORT0_LED9_BIT 25
#define _PORT0_LED10_BIT 24
#define _PORT1_LED11_BIT 25
#define _PORT1_LED12_BIT 24
#define _PORT0_LED13_BIT 4
#define _PORT0_LED14_BIT 5
#define _PORT0_LED15_BIT 6
#define _PORT0_LED16_BIT 7
#define _PORT0_LED_COMM_BIT 9
#define _BUT_MAX_SCAN_PER 200
#define _BUT_MIN_SCAN_PER 10
#define _PDIR_OFFSET (((unsigned int)&IO0DIR - (unsigned int)&IO0PIN)/sizeof(unsigned int))
#define _PSET_OFFSET (((unsigned int)&IO0SET - (unsigned int)&IO0PIN)/sizeof(unsigned int))
#define _PCLR_OFFSET (((unsigned int)&IO0CLR - (unsigned int)&IO0PIN)/sizeof(unsigned int))
#define _PIN_OFFSET (((unsigned int)&IO0PIN - (unsigned int)&IO0PIN)/sizeof(unsigned int))
#define _PORT0_BUT1_BIT 15
#define _PORT0_BUT2_BIT 16
#define _PORT0_BUT3_BIT 21
#define _PORT0_BUT4_BIT 22
#define _PORT0_BUT5_BIT 23
#define _PORT0_BUT6_BIT 28
#define _PORT0_BUT7_BIT 29
#define _PORT0_BUT8_BIT 8
#define _PORT0_BUT9_BIT 10
#define _PORT0_BUT10_BIT 11
#define _PORT0_BUT11_BIT 12
#define _PORT0_BUT12_BIT 13
typedef struct _ButtonCtrl_t
{
const volatile unsigned long * pButBaseReg;
unsigned int ButBit;
} ButtonCtrl_t, *pButtonCtrl_t;
typedef struct _ButtonsPairCtrl_t
{
ButtonCtrl_t Ba;
ButtonCtrl_t Bb;
} ButtonsPairCtrl_t, *pButtonsPairCtrl_t;
const ButtonsPairCtrl_t ButtonsCtrl[_CAP_BUTTONS_NUMB] =
{
{
.Ba=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_1
.ButBit = _PORT0_BUT1_BIT,
},
.Bb=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_2
.ButBit = _PORT0_BUT2_BIT,
}
},
{
.Ba=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_3
.ButBit = _PORT0_BUT3_BIT,
},
.Bb=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_4
.ButBit = _PORT0_BUT4_BIT,
}
},
{
.Ba=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_5
.ButBit = _PORT0_BUT5_BIT,
},
.Bb=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_6
.ButBit = _PORT0_BUT6_BIT,
}
},
{
.Ba=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_7
.ButBit = _PORT0_BUT7_BIT,
},
.Bb=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_8
.ButBit = _PORT0_BUT8_BIT,
}
},
{
.Ba=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_9
.ButBit = _PORT0_BUT9_BIT,
},
.Bb=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_10
.ButBit = _PORT0_BUT10_BIT,
}
},
{
.Ba=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_11
.ButBit = _PORT0_BUT11_BIT,
},
.Bb=
{
.pButBaseReg = &IO0PIN, // CAP_BUTT_12
.ButBit = _PORT0_BUT12_BIT,
}
},
};
/*********************************************************************
*
* Global functions
*
**********************************************************************
*/
/*********************************************************************
*
* BSP_Init()
*/
void BSP_Init(void)
{
// Set all pins LEDs' and buttons to GPIO
PINSEL0 &= ~((3UL<<(4 *2)) | (3UL<<(5 *2)) | (3UL<<(6 *2)) | (3UL<<(7 *2)) |
(3UL<<(8 *2)) | (3UL<<(10*2)) | (3UL<<(11*2)) | (3UL<<(12*2)) |
(3UL<<(13*2)) | (3UL<<(15*2)));
PINSEL1 &= ~((3UL<<((24-16)*2)) | (3UL<<((25-16)*2)) | (3UL<<((16-16)*2)) |
(3UL<<((21-16)*2)) | (3UL<<((22-16)*2)) | (3UL<<((23-16)*2)) |
(3UL<<((28-16)*2)) | (3UL<<((29-16)*2)));
PINSEL2 &= ~(1UL<<3);
// Switch off LEDs and initialize buttons' state
IO0SET = ((1UL << _PORT0_LED9_BIT ) | (1UL << _PORT0_LED10_BIT) |
(1UL << _PORT0_LED13_BIT) | (1UL << _PORT0_LED14_BIT) |
(1UL << _PORT0_LED15_BIT) | (1UL << _PORT0_LED16_BIT) |
(1UL << _PORT0_LED_COMM_BIT) |
(1UL << _PORT0_BUT1_BIT) | (1UL << _PORT0_BUT2_BIT) |
(1UL << _PORT0_BUT3_BIT) | (1UL << _PORT0_BUT4_BIT) |
(1UL << _PORT0_BUT5_BIT) | (1UL << _PORT0_BUT6_BIT) |
(1UL << _PORT0_BUT7_BIT) | (1UL << _PORT0_BUT8_BIT) |
(1UL << _PORT0_BUT9_BIT) | (1UL << _PORT0_BUT10_BIT) |
(1UL << _PORT0_BUT11_BIT) | (1UL << _PORT0_BUT12_BIT));
IO0CLR = (1UL << _PORT0_LED_COMM_BIT);
IO1SET = ((1UL << _PORT1_LED1_BIT ) | (1UL << _PORT1_LED2_BIT ) |
(1UL << _PORT1_LED3_BIT ) | (1UL << _PORT1_LED4_BIT ) |
(1UL << _PORT1_LED5_BIT ) | (1UL << _PORT1_LED6_BIT ) |
(1UL << _PORT1_LED7_BIT ) | (1UL << _PORT1_LED8_BIT ) |
(1UL << _PORT1_LED11_BIT) | (1UL << _PORT1_LED12_BIT));
// Set LED-Port and Buttons to output
IO0DIR |= ((1UL << _PORT0_LED9_BIT ) | (1UL << _PORT0_LED10_BIT) |
(1UL << _PORT0_LED13_BIT) | (1UL << _PORT0_LED14_BIT) |
(1UL << _PORT0_LED15_BIT) | (1UL << _PORT0_LED16_BIT) |
(1UL << _PORT0_LED_COMM_BIT) |
(1UL << _PORT0_BUT1_BIT) | (1UL << _PORT0_BUT2_BIT) |
(1UL << _PORT0_BUT3_BIT) | (1UL << _PORT0_BUT4_BIT) |
(1UL << _PORT0_BUT5_BIT) | (1UL << _PORT0_BUT6_BIT) |
(1UL << _PORT0_BUT7_BIT) | (1UL << _PORT0_BUT8_BIT) |
(1UL << _PORT0_BUT9_BIT) | (1UL << _PORT0_BUT10_BIT) |
(1UL << _PORT0_BUT11_BIT) | (1UL << _PORT0_BUT12_BIT));
IO1DIR |= ((1UL << _PORT1_LED1_BIT ) | (1UL << _PORT1_LED2_BIT ) |
(1UL << _PORT1_LED3_BIT ) | (1UL << _PORT1_LED4_BIT ) |
(1UL << _PORT1_LED5_BIT ) | (1UL << _PORT1_LED6_BIT ) |
(1UL << _PORT1_LED7_BIT ) | (1UL << _PORT1_LED8_BIT ) |
(1UL << _PORT1_LED11_BIT) | (1UL << _PORT1_LED12_BIT));
#ifdef _LED_INTENSITY_CTRL
// Init PWM Ch 6
PCONP_bit.PCPWM0 = 1; // Enable PWM0
PINSEL0_bit.P0_9 = 2; // Assign GPIO0.9 to PWM ch6
PWMTCR_bit.CE = 0; // Disable PWM counter
PWMTCR_bit.CR = 1; // reset the counter
PWMPR = 0; // set PWM prescaler (PWM Period 256 us)
PWMMCR = 2; // reset the counter by Ch0
PWMMR0 = 0xFF; // 8 Bit PWM
PWMMR6 = _LED_INTENSITY_MAX - _LED_INTENSITY_DEFAULT;
PWMPCR = 1<<14; // only PWM Ch6 output is enable
PWMLER_bit.EM6L = 1; // latch ch6 Match value
PWMTCR_bit.CR = 0; // release counter's reset
PWMTCR_bit.CE = 1; // enable counting
PWMTCR_bit.PWMEN = 1; // Enable PWM
#else
PINSEL0_bit.P0_9 = 0; // Set GPIO0.9 GPIO
IO0CLR = 1UL << _PORT0_LED_COMM_BIT;
#endif // _LED_INTENSITY_CTRL
// Init capacitive BUTTONS
// Init Timer1
PCONP_bit.PCTIM1 = 1; // Enable Timer1
T1TCR_bit.CE = 0; // counter disable
T1PR = 0; // set prescaler /1
T1MCR = 0; // disable match act
T1EMR = 0; // disable external match act
}
/*********************************************************************
*
* BSP_IntensityLED()
*/
void BSP_IntensityLED(unsigned int Intensity)
{
#ifdef _LED_INTENSITY_CTRL
assert((Intensity <= _LED_INTENSITY_MAX));
// Calculate true mach value (The LEDs' driver invert PWM output)
PWMMR6 = _LED_INTENSITY_MAX - Intensity;
// latch ch6 Match value
PWMLER_bit.EM6L = 1;
#else
if(Intensity)
{
IO0CLR = 1UL << _PORT0_LED_COMM_BIT;
}
else
{
IOSET = 1UL << _PORT0_LED_COMM_BIT;
}
#endif // _LED_INTENSITY_CTRL
}
/*********************************************************************
*
* LED switching routines
*/
void BSP_SetLED(unsigned int Mask)
{
if(Mask & (1UL<<0))
{
IO1CLR = (1 << _PORT1_LED1_BIT); // Switch on LED 1
}
if(Mask & (1UL<<1))
{
IO1CLR = (1 << _PORT1_LED2_BIT); // Switch on LED 2
}
if(Mask & (1UL<<2))
{
IO1CLR = (1 << _PORT1_LED3_BIT); // Switch on LED 3
}
if(Mask & (1UL<<3))
{
IO1CLR = (1 << _PORT1_LED4_BIT); // Switch on LED 4
}
if(Mask & (1UL<<4))
{
IO1CLR = (1 << _PORT1_LED5_BIT); // Switch on LED 5
}
if(Mask & (1UL<<5))
{
IO1CLR = (1 << _PORT1_LED6_BIT); // Switch on LED 6
}
if(Mask & (1UL<<6))
{
IO1CLR = (1 << _PORT1_LED7_BIT); // Switch on LED 7
}
if(Mask & (1UL<<7))
{
IO1CLR = (1 << _PORT1_LED8_BIT); // Switch on LED 8
}
if(Mask & (1UL<<8))
{
IO0CLR = (1 << _PORT0_LED9_BIT); // Switch on LED 9
}
if(Mask & (1UL<<9))
{
IO0CLR = (1 << _PORT0_LED10_BIT); // Switch on LED 10
}
if(Mask & (1UL<<10))
{
IO1CLR = (1 << _PORT1_LED11_BIT); // Switch on LED 11
}
if(Mask & (1UL<<11))
{
IO1CLR = (1 << _PORT1_LED12_BIT); // Switch on LED 12
}
if(Mask & (1UL<<12))
{
IO0CLR = (1 << _PORT0_LED13_BIT); // Switch on LED 13
}
if(Mask & (1UL<<13))
{
IO0CLR = (1 << _PORT0_LED14_BIT); // Switch on LED 14
}
if(Mask & (1UL<<14))
{
IO0CLR = (1 << _PORT0_LED15_BIT); // Switch on LED 15
}
if(Mask & (1UL<<15))
{
IO0CLR = (1 << _PORT0_LED16_BIT); // Switch on LED 16
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -