📄 evm5509_keypad.c
字号:
/*
* Copyright 2004 by Spectrum Digital Incorporated.
* All rights reserved. Property of Spectrum Digital Incorporated.
*/
/*
* ======== evm5509_keypad.c ========
* EVM642_SROM_write() implementation
*/
#include <csl.h>
#include <csl_i2c.h>
#include <csl_adc.h>
#include "evm5509.h"
#include "evm5509_keypad.h"
#include "evm5509_lcd.h"
static Uint16 databuf[4];
/* Analog switch constants */
#define SWITCH_ERROR 0x10
#define SWITCH_OFF 0x300
#define SWITCH_NOCHANGE 0
#define SWITCH_CHANGE 1
#define AIN_KEY 1
#define AIN_JOG 0
typedef struct {
int adcval;
int keymapping;
} SWITCH_LIST_ENTRY;
SWITCH_LIST_ENTRY keylist[] = {
{0x150, 0x01}, // Switch 01
{0x0f2, 0x02}, // Switch 02
{0x17c, 0x03}, // Switch 03
{0x0bf, 0x04}, // Switch 04
{0x1b9, 0x05}, // Switch 05
{0x07b, 0x06}, // Switch 06
{0x200, 0x07}, // Switch 07
{0x03d, 0x08}, // Switch 08
{0x23a, 0x09}, // Switch 09
{0x000, 0x00}
};
SWITCH_LIST_ENTRY joglist[] = {
{0x07b, 0x0A}, // Switch 0A (Jog down)
{0x150, 0x0B}, // Switch 0B (Jog up)
{0x200, 0x0C}, // Switch 0C (Jog in)
{0x000, 0x00}
};
void EVM5509_KEYPAD_init()
{
ADC_Config adcCfg = {
ADC_ADCCTL_RMK( ADC_ADCCTL_ADCSTART_OFF,
ADC_ADCCTL_CHSELECT_AIN1),
ADC_ADCCLKDIV_RMK( ADC_ADCCDR_SAMPTIMEDIV_OF(60),
ADC_ADCCDR_CONVRATEDIV_8DIV),
ADC_ADCCLKCTL_RMK( ADC_ADCCCR_IDLEEN_ACTIVE,
ADC_ADCCCR_CPUCLKDIV_OF(15))
};
ADC_config(&adcCfg);
}
int Switch_KeyMap(int adcreading, SWITCH_LIST_ENTRY *list)
{
int diff;
SWITCH_LIST_ENTRY *s;
s = list;
while (s -> keymapping != 0)
{
diff = s -> adcval - adcreading;
if (diff < 0)
diff = -diff;
if (diff <= SWITCH_ERROR)
return s -> keymapping;
s++;
}
if (adcreading < SWITCH_OFF)
return -1;
else
return 0;
}
/*
* ======== EVM5509_KEYPAD_readPot ========
* Read the value of a potentiometer
*/
Uint16 EVM5509_KEYPAD_readPot(Int16 potnum)
{
Uint16 acc;
Int16 i, id;
/* Pot #1 = I2C Address + 2, Pot #2 = I2C Address + 1 */
if (potnum == 2)
id = 1;
else
id = 2;
/* Take I2C controller out of reset, put in master transmitter mode */
I2C_RSET(I2CMDR, 0);
I2C_FSET(I2CMDR, MST, 1);
I2C_FSET(I2CMDR, FREE, 1);
I2C_FSET(I2CMDR, TRX, 1);
I2C_FSET(I2CMDR, IRS, 1);
/* Set the I2C controller to write 2 byte address + length bytes data */
I2C_RSET(I2CCNT, 1);
I2C_RSET(I2CSAR, EVM5509_KEYPAD_I2CADDR + id);
/* Generate start condition */
I2C_FSET(I2CMDR, STT, 1);
/* Transmit a null data byte to initiate A/D transfer */
I2C_RSET(I2CDXR, 0);
/* Wait until finished */
while(!I2C_xrdy());
/* Generate stop condition */
I2C_FSET(I2CMDR, STP, 1);
/* Wait until bus free */
while(I2C_FGET(I2CSTR, BB) != 0);
/* Take I2C controller out of reset, put in master receiver mode */
I2C_RSET(I2CMDR, 0);
I2C_FSET(I2CMDR, MST, 1);
I2C_FSET(I2CMDR, FREE, 1);
I2C_FSET(I2CMDR, TRX, 0);
I2C_FSET(I2CMDR, IRS, 1);
/* Set the I2C controller to read 4 samples */
I2C_RSET(I2CCNT, 4);
I2C_RSET(I2CSAR, EVM5509_KEYPAD_I2CADDR + id);
/* Generate Start condition */
I2C_FSET(I2CMDR, STT, 1);
acc = 0;
for (i = 0; i < 4; i++)
{
/* Wait for receive data to come back */
while(!I2C_rrdy());
/* Copy the data out */
if ((i & 1) == 0)
databuf[i >> 1] = I2C_RGET(I2CDRR) << 8;
else {
databuf[i >> 1] |= I2C_RGET(I2CDRR);
acc += databuf[i >> 1];
}
}
/* Generate stop condition */
I2C_FSET(I2CMDR, STP, 1);
/* Wait until bus free */
while(I2C_FGET(I2CSTR, BB) != 0);
return (acc >> 1);
}
/*
* ======== EVM5509_KEYPAD_readKey ========
* Read the value of a keyswitch
*/
Uint16 EVM5509_KEYPAD_readKey()
{
Uint16 keyval;
/* Read analog key value */
ADC_read(AIN_KEY, &keyval, 1);
return Switch_KeyMap(keyval, keylist);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -