📄 evmdrv.c
字号:
UWord16 ledIoctlPWMLED_OFF( handle_t hndl, unsigned long params )
{
return ledPWMWrite(hndl, "\0x00", 1 );
}
/*******************************************************************************
*
* NAME: ledPWMBarWrite
*
* DESCRIPTION: To toggle LedPWM as progress bar
*
********************************************************************************
* PARAMETERS: hndl - handle to PWM device
* buf - state of bar (ON/OFF="\1"/"\0")
* len - length of bar
*
* SIDE EFFECTS: led's station : state len
* ------ 0
* +----- 1
* ++---- 2
* +++--- 3
* ++++-- 4
* +++++- 5
* ++++++ 6
* PWM: 012345
* + ON
* - OFF
*
* DESIGNER NOTES: Direction could be inverted!
*
* DEPENDENCIES: None
*
*
*******************************************************************************/
size_t ledPWMBarWrite( handle_t hndl, const void *buf, size_t len )
{
volatile arch_sPWM* pPwm = ((led_PWM_Config*) hndl)->pPwm;
unsigned int val = 0, i, flags = ((led_PWM_Config*) hndl)->flags;
if (len>6) len = 6;
if(*(const char*)buf)
{
val = 0x7fff;
}
if (flags & O_INVERT_DIRECTION)
{
for(i=0; i<len; i++)
{
periphMemWrite(val, (volatile UWord16 *)&pPwm->ValueReg[5-i]);
}
for(i=len; i<6; i++)
{
periphMemWrite(0, (volatile UWord16 *)&pPwm->ValueReg[5-i]);
}
}
else
{
for(i=0; i<len; i++)
{
periphMemWrite(val, (volatile UWord16 *)&pPwm->ValueReg[i]);
}
for(i=len; i<6; i++)
{
periphMemWrite(0, (volatile UWord16 *)&pPwm->ValueReg[i]);
}
}
periphBitSet( 0x0002, &pPwm->ControlReg);
return 1;
}
void LedInit(const led_Config* ledport, int num )
{
int i;
for( i = 0; i < num; i++ )
{
periphBitClear(ledport->pin, &( ledport->port->PeripheralReg ) );
periphBitSet (ledport->pin, &( ledport->port->DataDirectionReg) );
periphBitClear(ledport->pin, &( ledport->port->DataReg ) );
ledport++;
}
}
/*******************************************************************************
*
* NAME: ledPWMInit
*
* DESCRIPTION: To initialize pwm device for working with pwm led
*
********************************************************************************
* PARAMETERS: arch_sPWM - handle to PWM registers structure
*
* SIDE EFFECTS: None Known
*
* DESIGNER NOTES: inline function (hardware turning)
* DisableMapping1Reg put 0xffff to shut all PWM pins
*
* DEPENDENCIES: None
*
*
*******************************************************************************/
inline void ledPWMInit(volatile arch_sPWM* pPwm)
{
periphMemWrite(0x0000, &pPwm->FaultControlReg);
periphMemWrite(0x0000, &pPwm->FaultStatusReg);
periphMemWrite(0x7fff, &pPwm->CounterModuloReg);
periphMemWrite(0x002f, &pPwm->DeadtimeReg);
periphMemWrite(0xffff, &pPwm->DisableMapping1Reg);
periphMemWrite(0x00ff, &pPwm->DisableMapping2Reg);
periphMemWrite(0x0000, &pPwm->ChannelControlReg);
periphMemWrite(0x800e, &pPwm->ConfigReg);
periphMemWrite(0xf0c1, &pPwm->ControlReg);
periphMemWrite(0x8000, &pPwm->OutputControlReg);
periphMemWrite(0,(volatile UWord16 *)&pPwm->ValueReg[0]);
periphMemWrite(0,(volatile UWord16 *)&pPwm->ValueReg[1]);
periphMemWrite(0,(volatile UWord16 *)&pPwm->ValueReg[2]);
periphMemWrite(0,(volatile UWord16 *)&pPwm->ValueReg[3]);
periphMemWrite(0,(volatile UWord16 *)&pPwm->ValueReg[4]);
periphMemWrite(0,(volatile UWord16 *)&pPwm->ValueReg[5]);
periphBitSet( 0x0002, &pPwm->ControlReg );
return;
}
extern const struct switch_sConfig switchdrvDevice;
/*****************************************************************************/
handle_t switchOpen(const char * pName, int OFlags,...)
{
#ifdef BSP_DEVICE_NAME_GPIO_D
if( pName == BSP_DEVICE_NAME_SWITCH_0 )
{
const struct switch_sConfig * switchHndl = &switchdrvDevice;
gpioIoctlGPIO_SETAS_GPIO( switchHndl->port, switchHndl->pin );
gpioIoctlGPIO_SETAS_INPUT( switchHndl->port, switchHndl->pin );
return (handle_t)switchHndl;
}
#endif
return (handle_t) -1;
}
/* define the ioctl calls */
UWord16 switchIoctllSWITCH_GET_STATE( handle_t hndl, void* pParams )
{
return gpioIoctlGPIO_READ( ((struct switch_sConfig *) hndl)->port, ((struct switch_sConfig *) hndl)->pin );
}
/*****************************************************************************/
int switchClose(handle_t FileDesc)
{
return 0;
}
/* Library part */
inline static bool timeGE(struct timespec * pTime1,struct timespec * pTime2)
{
if (pTime1->tv_sec > pTime2->tv_sec)
{
return true;
}
if (pTime1->tv_sec < pTime2->tv_sec)
{
return false;
}
return (pTime1->tv_nsec >= pTime2->tv_nsec);
}
#define NSECSINSEC 1000000000
inline static void timeAdd (struct timespec * pTimeres, struct timespec * pTime1, struct timespec * pTime2)
{
long sec;
long nsec;
sec = pTime1 -> tv_sec + pTime2 -> tv_sec;
nsec = pTime1 -> tv_nsec + pTime2 -> tv_nsec;
while (nsec >= NSECSINSEC) {
sec++;
nsec -= NSECSINSEC;
}
while (nsec <= -NSECSINSEC) {
sec--;
nsec += NSECSINSEC;
}
while (sec > 0 && nsec < 0) {
sec--;
nsec += NSECSINSEC;
}
while (sec < 0 && nsec > 0) {
sec++;
nsec -= NSECSINSEC;
}
pTimeres -> tv_sec = sec;
pTimeres -> tv_nsec = nsec;
}
/********************************************************************************/
/* Flags for controling open device */
/********************************************************************************/
static unsigned short openA = 0, openB = 0;
/*****************************************************************************
*
* Module: buttonISR()
*
* Description: Specific part of button isr handler. Call user defined callback.
*
*
* Returns: None
*
* Arguments: pButtonTable - button context
* Callback - user callback
*
* Range Issues: None
*
* Test Method: butSwtst.mcp
*
*****************************************************************************/
#pragma interrupt saveall
#pragma interrupt called
void buttonISR(void *, struct button_sButton * pButtonTable, button_tCallback Callback)
{
/* call transmit callback */
/* Debounce the button */
static struct timespec Milliseconds = {0,150000000};
static struct timespec Now;
clock_get_system_time(&Now);
if (timeGE (&Now, &(pButtonTable->DebounceTimeExp)))
if ( ( pButtonTable == &ButtonADeviceContext ) && openA )
Callback(pButtonTable, (unsigned long)BSP_DEVICE_NAME_BUTTON_A);
else
if ( ( pButtonTable == &ButtonBDeviceContext ) && openB )
Callback(pButtonTable, (unsigned long)BSP_DEVICE_NAME_BUTTON_B);
timeAdd (&(pButtonTable->DebounceTimeExp), &Now, &Milliseconds);
}
/*****************************************************************************/
handle_t buttonOpen(const char * pName, int OFlags,...)
{
#ifdef BSP_DEVICE_NAME_BUTTON_A
if( pName == BSP_DEVICE_NAME_BUTTON_A )
{
openA = 1;
return (handle_t)&ButtonBDeviceContext; /* device detected */
}
else
#endif
{
#ifdef BSP_DEVICE_NAME_BUTTON_B
if(pName == BSP_DEVICE_NAME_BUTTON_B)
{
openB = 1;
return (handle_t)&ButtonBDeviceContext; /* device detected */
}
#endif
}
return (handle_t)-1; /* not my device */
}
/*****************************************************************************/
int buttonClose(handle_t FileDesc)
{
if (FileDesc == (handle_t)&ButtonADeviceContext)
openA = 0;
if (FileDesc == (handle_t)&ButtonBDeviceContext)
openB = 0;
FileDesc = (handle_t) 0;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -