Adding analog.c lib from teensyduino.
- Mostly for reference, you shouldn't include it verbatim in your module.
This commit is contained in:
parent
dc22fa757a
commit
8ee8e3cb55
@ -53,8 +53,8 @@ void cliFunc_macroDebug ( char* args );
|
|||||||
|
|
||||||
// ----- Variables -----
|
// ----- Variables -----
|
||||||
|
|
||||||
// Output Module command dictionary
|
// Macro Module command dictionary
|
||||||
char* macroCLIDictName = "Macro Module Commands";
|
char* macroCLIDictName = "Macro Module Commands (Not all commands fully work yet...)";
|
||||||
CLIDictItem macroCLIDict[] = {
|
CLIDictItem macroCLIDict[] = {
|
||||||
{ "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
|
{ "capList", "Prints an indexed list of all non USB keycode capabilities.", cliFunc_capList },
|
||||||
{ "capSelect", "Triggers the specified capability. U10 - USB Code 0x0A. K11 - Keyboard Capability 0x0B. S10 - Scancode 0x0A", cliFunc_capSelect },
|
{ "capSelect", "Triggers the specified capability. U10 - USB Code 0x0A. K11 - Keyboard Capability 0x0B. S10 - Scancode 0x0A", cliFunc_capSelect },
|
||||||
|
@ -33,8 +33,9 @@
|
|||||||
#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
|
#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_)
|
||||||
#include "avr/usb_keyboard_serial.h"
|
#include "avr/usb_keyboard_serial.h"
|
||||||
#elif defined(_mk20dx128_) || defined(_mk20dx256_)
|
#elif defined(_mk20dx128_) || defined(_mk20dx256_)
|
||||||
#include "arm/usb_keyboard.h"
|
|
||||||
#include "arm/usb_dev.h"
|
#include "arm/usb_dev.h"
|
||||||
|
#include "arm/usb_keyboard.h"
|
||||||
|
#include "arm/usb_serial.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Local Includes
|
// Local Includes
|
||||||
@ -54,7 +55,7 @@ void cliFunc_setMod ( char* args );
|
|||||||
// ----- Variables -----
|
// ----- Variables -----
|
||||||
|
|
||||||
// Output Module command dictionary
|
// Output Module command dictionary
|
||||||
char* outputCLIDictName = "USB Module Commands";
|
char* outputCLIDictName = "USB Module Commands (Not all commands fully work yet...)";
|
||||||
CLIDictItem outputCLIDict[] = {
|
CLIDictItem outputCLIDict[] = {
|
||||||
{ "readLEDs", "Read LED byte. See \033[35msetLEDs\033[0m.", cliFunc_readLEDs },
|
{ "readLEDs", "Read LED byte. See \033[35msetLEDs\033[0m.", cliFunc_readLEDs },
|
||||||
{ "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
|
{ "sendKeys", "Send the prepared list of USB codes and modifier byte.", cliFunc_sendKeys },
|
||||||
|
420
Scan/ADCTest/analog.c
Normal file
420
Scan/ADCTest/analog.c
Normal file
@ -0,0 +1,420 @@
|
|||||||
|
/* Teensyduino Core Library
|
||||||
|
* http://www.pjrc.com/teensy/
|
||||||
|
* Copyright (c) 2013 PJRC.COM, LLC.
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
* a copy of this software and associated documentation files (the
|
||||||
|
* "Software"), to deal in the Software without restriction, including
|
||||||
|
* without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
* permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
* the following conditions:
|
||||||
|
*
|
||||||
|
* 1. The above copyright notice and this permission notice shall be
|
||||||
|
* included in all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* 2. If the Software is incorporated into a build system that allows
|
||||||
|
* selection among a list of target devices, then similar target
|
||||||
|
* devices manufactured by PJRC.COM must be included in the list of
|
||||||
|
* target devices and selectable in the same manner.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||||
|
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Lib/ScanLib.h>
|
||||||
|
|
||||||
|
static uint8_t calibrating;
|
||||||
|
static uint8_t analog_right_shift = 0;
|
||||||
|
static uint8_t analog_config_bits = 10;
|
||||||
|
static uint8_t analog_num_average = 4;
|
||||||
|
static uint8_t analog_reference_internal = 0;
|
||||||
|
|
||||||
|
// the alternate clock is connected to OSCERCLK (16 MHz).
|
||||||
|
// datasheet says ADC clock should be 2 to 12 MHz for 16 bit mode
|
||||||
|
// datasheet says ADC clock should be 1 to 18 MHz for 8-12 bit mode
|
||||||
|
|
||||||
|
#if F_BUS == 48000000
|
||||||
|
#define ADC_CFG1_6MHZ ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(1)
|
||||||
|
#define ADC_CFG1_12MHZ ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(1)
|
||||||
|
#define ADC_CFG1_24MHZ ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(1)
|
||||||
|
#elif F_BUS == 24000000
|
||||||
|
#define ADC_CFG1_6MHZ ADC_CFG1_ADIV(2) + ADC_CFG1_ADICLK(0)
|
||||||
|
#define ADC_CFG1_12MHZ ADC_CFG1_ADIV(1) + ADC_CFG1_ADICLK(0)
|
||||||
|
#define ADC_CFG1_24MHZ ADC_CFG1_ADIV(0) + ADC_CFG1_ADICLK(0)
|
||||||
|
#else
|
||||||
|
#error
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void analog_init(void)
|
||||||
|
{
|
||||||
|
uint32_t num;
|
||||||
|
|
||||||
|
VREF_TRM = 0x60;
|
||||||
|
VREF_SC = 0xE1; // enable 1.2 volt ref
|
||||||
|
|
||||||
|
if (analog_config_bits == 8) {
|
||||||
|
ADC0_CFG1 = ADC_CFG1_24MHZ + ADC_CFG1_MODE(0);
|
||||||
|
ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_CFG1 = ADC_CFG1_24MHZ + ADC_CFG1_MODE(0);
|
||||||
|
ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
|
||||||
|
#endif
|
||||||
|
} else if (analog_config_bits == 10) {
|
||||||
|
ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
|
||||||
|
ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(2) + ADC_CFG1_ADLSMP;
|
||||||
|
ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(3);
|
||||||
|
#endif
|
||||||
|
} else if (analog_config_bits == 12) {
|
||||||
|
ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
|
||||||
|
ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(1) + ADC_CFG1_ADLSMP;
|
||||||
|
ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
ADC0_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
|
||||||
|
ADC0_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_CFG1 = ADC_CFG1_12MHZ + ADC_CFG1_MODE(3) + ADC_CFG1_ADLSMP;
|
||||||
|
ADC1_CFG2 = ADC_CFG2_MUXSEL + ADC_CFG2_ADLSTS(2);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
if (analog_reference_internal) {
|
||||||
|
ADC0_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC2 = ADC_SC2_REFSEL(1); // 1.2V ref
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
ADC0_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC2 = ADC_SC2_REFSEL(0); // vcc/ext ref
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
num = analog_num_average;
|
||||||
|
if (num <= 1) {
|
||||||
|
ADC0_SC3 = ADC_SC3_CAL; // begin cal
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = ADC_SC3_CAL; // begin cal
|
||||||
|
#endif
|
||||||
|
} else if (num <= 4) {
|
||||||
|
ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(0);
|
||||||
|
#endif
|
||||||
|
} else if (num <= 8) {
|
||||||
|
ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(1);
|
||||||
|
#endif
|
||||||
|
} else if (num <= 16) {
|
||||||
|
ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(2);
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
ADC0_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = ADC_SC3_CAL + ADC_SC3_AVGE + ADC_SC3_AVGS(3);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
calibrating = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void wait_for_cal(void)
|
||||||
|
{
|
||||||
|
uint16_t sum;
|
||||||
|
|
||||||
|
//serial_print("wait_for_cal\n");
|
||||||
|
#if defined(_mk20dx128_)
|
||||||
|
while (ADC0_SC3 & ADC_SC3_CAL) {
|
||||||
|
// wait
|
||||||
|
}
|
||||||
|
#elif defined(_mk20dx256_)
|
||||||
|
while ((ADC0_SC3 & ADC_SC3_CAL) || (ADC1_SC3 & ADC_SC3_CAL)) {
|
||||||
|
// wait
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
__disable_irq();
|
||||||
|
if (calibrating) {
|
||||||
|
//serial_print("\n");
|
||||||
|
sum = ADC0_CLPS + ADC0_CLP4 + ADC0_CLP3 + ADC0_CLP2 + ADC0_CLP1 + ADC0_CLP0;
|
||||||
|
sum = (sum / 2) | 0x8000;
|
||||||
|
ADC0_PG = sum;
|
||||||
|
//serial_print("ADC0_PG = ");
|
||||||
|
//serial_phex16(sum);
|
||||||
|
//serial_print("\n");
|
||||||
|
sum = ADC0_CLMS + ADC0_CLM4 + ADC0_CLM3 + ADC0_CLM2 + ADC0_CLM1 + ADC0_CLM0;
|
||||||
|
sum = (sum / 2) | 0x8000;
|
||||||
|
ADC0_MG = sum;
|
||||||
|
//serial_print("ADC0_MG = ");
|
||||||
|
//serial_phex16(sum);
|
||||||
|
//serial_print("\n");
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
sum = ADC1_CLPS + ADC1_CLP4 + ADC1_CLP3 + ADC1_CLP2 + ADC1_CLP1 + ADC1_CLP0;
|
||||||
|
sum = (sum / 2) | 0x8000;
|
||||||
|
ADC1_PG = sum;
|
||||||
|
sum = ADC1_CLMS + ADC1_CLM4 + ADC1_CLM3 + ADC1_CLM2 + ADC1_CLM1 + ADC1_CLM0;
|
||||||
|
sum = (sum / 2) | 0x8000;
|
||||||
|
ADC1_MG = sum;
|
||||||
|
#endif
|
||||||
|
calibrating = 0;
|
||||||
|
}
|
||||||
|
__enable_irq();
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADCx_SC2[REFSEL] bit selects the voltage reference sources for ADC.
|
||||||
|
// VREFH/VREFL - connected as the primary reference option
|
||||||
|
// 1.2 V VREF_OUT - connected as the VALT reference option
|
||||||
|
|
||||||
|
|
||||||
|
#define DEFAULT 0
|
||||||
|
#define INTERNAL 2
|
||||||
|
#define INTERNAL1V2 2
|
||||||
|
#define INTERNAL1V1 2
|
||||||
|
#define EXTERNAL 0
|
||||||
|
|
||||||
|
void analogReference(uint8_t type)
|
||||||
|
{
|
||||||
|
if (type) {
|
||||||
|
// internal reference requested
|
||||||
|
if (!analog_reference_internal) {
|
||||||
|
analog_reference_internal = 1;
|
||||||
|
if (calibrating) {
|
||||||
|
ADC0_SC3 = 0; // cancel cal
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = 0; // cancel cal
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
analog_init();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// vcc or external reference requested
|
||||||
|
if (analog_reference_internal) {
|
||||||
|
analog_reference_internal = 0;
|
||||||
|
if (calibrating) {
|
||||||
|
ADC0_SC3 = 0; // cancel cal
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
ADC1_SC3 = 0; // cancel cal
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
analog_init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void analogReadRes(unsigned int bits)
|
||||||
|
{
|
||||||
|
unsigned int config;
|
||||||
|
|
||||||
|
if (bits >= 13) {
|
||||||
|
if (bits > 16) bits = 16;
|
||||||
|
config = 16;
|
||||||
|
} else if (bits >= 11) {
|
||||||
|
config = 12;
|
||||||
|
} else if (bits >= 9) {
|
||||||
|
config = 10;
|
||||||
|
} else {
|
||||||
|
config = 8;
|
||||||
|
}
|
||||||
|
analog_right_shift = config - bits;
|
||||||
|
if (config != analog_config_bits) {
|
||||||
|
analog_config_bits = config;
|
||||||
|
if (calibrating) ADC0_SC3 = 0; // cancel cal
|
||||||
|
analog_init();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void analogReadAveraging(unsigned int num)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (calibrating) wait_for_cal();
|
||||||
|
if (num <= 1) {
|
||||||
|
num = 0;
|
||||||
|
ADC0_SC3 = 0;
|
||||||
|
} else if (num <= 4) {
|
||||||
|
num = 4;
|
||||||
|
ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(0);
|
||||||
|
} else if (num <= 8) {
|
||||||
|
num = 8;
|
||||||
|
ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(1);
|
||||||
|
} else if (num <= 16) {
|
||||||
|
num = 16;
|
||||||
|
ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(2);
|
||||||
|
} else {
|
||||||
|
num = 32;
|
||||||
|
ADC0_SC3 = ADC_SC3_AVGE + ADC_SC3_AVGS(3);
|
||||||
|
}
|
||||||
|
analog_num_average = num;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The SC1A register is used for both software and hardware trigger modes of operation.
|
||||||
|
|
||||||
|
#if defined(_mk20dx128_)
|
||||||
|
static const uint8_t channel2sc1a[] = {
|
||||||
|
5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
|
||||||
|
0, 19, 3, 21, 26, 22, 23
|
||||||
|
};
|
||||||
|
#elif defined(_mk20dx256_)
|
||||||
|
static const uint8_t channel2sc1a[] = {
|
||||||
|
5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
|
||||||
|
0, 19, 3, 19+128, 26, 22, 23,
|
||||||
|
5+192, 5+128, 4+128, 6+128, 7+128, 4+192
|
||||||
|
// A15 26 E1 ADC1_SE5a 5+64
|
||||||
|
// A16 27 C9 ADC1_SE5b 5
|
||||||
|
// A17 28 C8 ADC1_SE4b 4
|
||||||
|
// A18 29 C10 ADC1_SE6b 6
|
||||||
|
// A19 30 C11 ADC1_SE7b 7
|
||||||
|
// A20 31 E0 ADC1_SE4a 4+64
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// TODO: perhaps this should store the NVIC priority, so it works recursively?
|
||||||
|
static volatile uint8_t analogReadBusyADC0 = 0;
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
static volatile uint8_t analogReadBusyADC1 = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int analogRead(uint8_t pin)
|
||||||
|
{
|
||||||
|
int result;
|
||||||
|
uint8_t index, channel;
|
||||||
|
|
||||||
|
//serial_phex(pin);
|
||||||
|
//serial_print(" ");
|
||||||
|
|
||||||
|
if (pin <= 13) {
|
||||||
|
index = pin; // 0-13 refer to A0-A13
|
||||||
|
} else if (pin <= 23) {
|
||||||
|
index = pin - 14; // 14-23 are A0-A9
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
} else if (pin >= 26 && pin <= 31) {
|
||||||
|
index = pin - 9; // 26-31 are A15-A20
|
||||||
|
#endif
|
||||||
|
} else if (pin >= 34 && pin <= 40) {
|
||||||
|
index = pin - 24; // 34-37 are A10-A13, 38 is temp sensor,
|
||||||
|
// 39 is vref, 40 is unused (A14 on Teensy 3.1)
|
||||||
|
} else {
|
||||||
|
return 0; // all others are invalid
|
||||||
|
}
|
||||||
|
|
||||||
|
//serial_phex(index);
|
||||||
|
//serial_print(" ");
|
||||||
|
|
||||||
|
channel = channel2sc1a[index];
|
||||||
|
//serial_phex(channel);
|
||||||
|
//serial_print(" ");
|
||||||
|
|
||||||
|
//serial_print("analogRead");
|
||||||
|
//return 0;
|
||||||
|
if (calibrating) wait_for_cal();
|
||||||
|
//pin = 5; // PTD1/SE5b, pin 14, analog 0
|
||||||
|
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
if (channel & 0x80) goto beginADC1;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
__disable_irq();
|
||||||
|
startADC0:
|
||||||
|
//serial_print("startADC0\n");
|
||||||
|
ADC0_SC1A = channel;
|
||||||
|
analogReadBusyADC0 = 1;
|
||||||
|
__enable_irq();
|
||||||
|
while (1) {
|
||||||
|
__disable_irq();
|
||||||
|
if ((ADC0_SC1A & ADC_SC1_COCO)) {
|
||||||
|
result = ADC0_RA;
|
||||||
|
analogReadBusyADC0 = 0;
|
||||||
|
__enable_irq();
|
||||||
|
result >>= analog_right_shift;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
// detect if analogRead was used from an interrupt
|
||||||
|
// if so, our analogRead got canceled, so it must
|
||||||
|
// be restarted.
|
||||||
|
if (!analogReadBusyADC0) goto startADC0;
|
||||||
|
__enable_irq();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
beginADC1:
|
||||||
|
__disable_irq();
|
||||||
|
startADC1:
|
||||||
|
//serial_print("startADC0\n");
|
||||||
|
// ADC1_CFG2[MUXSEL] bit selects between ADCx_SEn channels a and b.
|
||||||
|
if (channel & 0x40) {
|
||||||
|
ADC1_CFG2 &= ~ADC_CFG2_MUXSEL;
|
||||||
|
} else {
|
||||||
|
ADC1_CFG2 |= ADC_CFG2_MUXSEL;
|
||||||
|
}
|
||||||
|
ADC1_SC1A = channel & 0x3F;
|
||||||
|
analogReadBusyADC1 = 1;
|
||||||
|
__enable_irq();
|
||||||
|
while (1) {
|
||||||
|
__disable_irq();
|
||||||
|
if ((ADC1_SC1A & ADC_SC1_COCO)) {
|
||||||
|
result = ADC1_RA;
|
||||||
|
analogReadBusyADC1 = 0;
|
||||||
|
__enable_irq();
|
||||||
|
result >>= analog_right_shift;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
// detect if analogRead was used from an interrupt
|
||||||
|
// if so, our analogRead got canceled, so it must
|
||||||
|
// be restarted.
|
||||||
|
if (!analogReadBusyADC1) goto startADC1;
|
||||||
|
__enable_irq();
|
||||||
|
yield();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void analogWriteDAC0(int val)
|
||||||
|
{
|
||||||
|
#if defined(_mk20dx256_)
|
||||||
|
SIM_SCGC2 |= SIM_SCGC2_DAC0;
|
||||||
|
if (analog_reference_internal) {
|
||||||
|
DAC0_C0 = DAC_C0_DACEN; // 1.2V ref is DACREF_1
|
||||||
|
} else {
|
||||||
|
DAC0_C0 = DAC_C0_DACEN | DAC_C0_DACRFS; // 3.3V VDDA is DACREF_2
|
||||||
|
}
|
||||||
|
if (val < 0) val = 0; // TODO: saturate instruction?
|
||||||
|
else if (val > 4095) val = 4095;
|
||||||
|
*(int16_t *)&(DAC0_DAT0L) = val;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
120
Scan/ADCTest/defaultMap.h
Normal file
120
Scan/ADCTest/defaultMap.h
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/* Copyright (C) 2014 by Jacob Alexander
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __KEYMAP_H
|
||||||
|
#define __KEYMAP_H
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Variables -----
|
||||||
|
|
||||||
|
// Default 1-indexed key mappings
|
||||||
|
static uint8_t DefaultMap_Lookup[] = {
|
||||||
|
0, // 0x00
|
||||||
|
KEY_1, // 0x01
|
||||||
|
KEY_Q, // 0x02
|
||||||
|
KEY_A, // 0x03
|
||||||
|
KEY_2, // 0x04
|
||||||
|
KEY_Z, // 0x05
|
||||||
|
KEY_W, // 0x06
|
||||||
|
KEY_S, // 0x07
|
||||||
|
KEY_3, // 0x08
|
||||||
|
KEY_X, // 0x09
|
||||||
|
KEY_E, // 0x0A
|
||||||
|
KEY_D, // 0x0B
|
||||||
|
KEY_4, // 0x0C
|
||||||
|
KEY_C, // 0x0D
|
||||||
|
KEY_R, // 0x0E
|
||||||
|
KEY_F, // 0x0F
|
||||||
|
KEY_5, // 0x10
|
||||||
|
KEY_V, // 0x11
|
||||||
|
KEY_T, // 0x12
|
||||||
|
KEY_G, // 0x13
|
||||||
|
KEY_6, // 0x14
|
||||||
|
KEY_B, // 0x15
|
||||||
|
KEY_Y, // 0x16
|
||||||
|
KEY_H, // 0x17
|
||||||
|
KEY_7, // 0x18
|
||||||
|
KEY_N, // 0x19
|
||||||
|
KEY_U, // 0x1A
|
||||||
|
KEY_J, // 0x1B
|
||||||
|
KEY_8, // 0x1C
|
||||||
|
KEY_M, // 0x1D
|
||||||
|
KEY_I, // 0x1E
|
||||||
|
KEY_K, // 0x1F
|
||||||
|
KEY_9, // 0x20
|
||||||
|
KEY_COMMA, // 0x21
|
||||||
|
KEY_O, // 0x22
|
||||||
|
KEY_L, // 0x23
|
||||||
|
KEY_0, // 0x24
|
||||||
|
KEY_PERIOD, // 0x25
|
||||||
|
KEY_P, // 0x26
|
||||||
|
KEY_SEMICOLON, // 0x27
|
||||||
|
KEY_MINUS, // 0x28
|
||||||
|
KEY_SLASH, // 0x29
|
||||||
|
KEY_LEFT_BRACE, // 0x2A (1/4)
|
||||||
|
KEY_QUOTE, // 0x2B
|
||||||
|
KEY_EQUAL, // 0x2C
|
||||||
|
KEY_RIGHT_BRACE, // 0x2D
|
||||||
|
0, // 0x2E
|
||||||
|
0, // 0x2F
|
||||||
|
KEY_TILDE, // 0x30
|
||||||
|
KEY_TAB, // 0x31
|
||||||
|
0, // 0x32
|
||||||
|
0, // 0x33
|
||||||
|
KEY_SHIFT, // 0x34
|
||||||
|
KEY_ENTER, // 0x35
|
||||||
|
KEY_BACKSPACE, // 0x36
|
||||||
|
KEY_DELETE, // 0x37
|
||||||
|
KEY_CTRL, // 0x38 (MAR LEFT)
|
||||||
|
KEY_SPACE, // 0x39
|
||||||
|
KEY_ALT, // 0x3A (EXPRESS / MAR RIGHT)
|
||||||
|
0, // 0x3B
|
||||||
|
0, // 0x3C
|
||||||
|
KEY_ESC, // 0x3D (MAR REL)
|
||||||
|
0, // 0x3E (STORE)
|
||||||
|
0, // 0x3F (RECALL)
|
||||||
|
KEY_GUI, // 0x40 (CODE)
|
||||||
|
0, // 0x41
|
||||||
|
0, // 0x42
|
||||||
|
0, // 0x43
|
||||||
|
0, // 0x44
|
||||||
|
0, // 0x45
|
||||||
|
0, // 0x46
|
||||||
|
0, // 0x47
|
||||||
|
0, // 0x48 (DEC TAB)
|
||||||
|
0, // 0x49 (SET TAB)
|
||||||
|
0, // 0x4A (TAB CLEAR)
|
||||||
|
0, // 0x4B (INDEX)
|
||||||
|
0, // 0x4C (RELOC)
|
||||||
|
0, // 0x4D
|
||||||
|
0, // 0x4E
|
||||||
|
0, // 0x4F
|
||||||
|
0, // 0x50 (REV INDEX)
|
||||||
|
0, // 0x51
|
||||||
|
0, // 0x52
|
||||||
|
0, // 0x53
|
||||||
|
0, // 0x54
|
||||||
|
0, // 0x55
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
133
Scan/ADCTest/scan_loop.c
Normal file
133
Scan/ADCTest/scan_loop.c
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
/* Copyright (C) 2014 by Jacob Alexander
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// ----- Includes -----
|
||||||
|
|
||||||
|
// Compiler Includes
|
||||||
|
#include <Lib/ScanLib.h>
|
||||||
|
|
||||||
|
// Project Includes
|
||||||
|
#include <cli.h>
|
||||||
|
#include <led.h>
|
||||||
|
#include <print.h>
|
||||||
|
|
||||||
|
// Local Includes
|
||||||
|
#include "scan_loop.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Defines -----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Macros -----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Function Declarations -----
|
||||||
|
|
||||||
|
void cliFunc_echo( char* args );
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Variables -----
|
||||||
|
|
||||||
|
// Buffer used to inform the macro processing module which keys have been detected as pressed
|
||||||
|
volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
|
||||||
|
volatile uint8_t KeyIndex_BufferUsed;
|
||||||
|
|
||||||
|
|
||||||
|
// Scan Module command dictionary
|
||||||
|
char* scanCLIDictName = "ADC Test Module Commands";
|
||||||
|
CLIDictItem scanCLIDict[] = {
|
||||||
|
{ "echo", "Example command, echos the arguments.", cliFunc_echo },
|
||||||
|
{ 0, 0, 0 } // Null entry for dictionary end
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Functions -----
|
||||||
|
|
||||||
|
// Setup
|
||||||
|
inline void Scan_setup()
|
||||||
|
#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
|
||||||
|
{
|
||||||
|
// Register Scan CLI dictionary
|
||||||
|
CLI_registerDictionary( scanCLIDict, scanCLIDictName );
|
||||||
|
}
|
||||||
|
#elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
|
||||||
|
{
|
||||||
|
// Register Scan CLI dictionary
|
||||||
|
CLI_registerDictionary( scanCLIDict, scanCLIDictName );
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
// Main Detection Loop
|
||||||
|
inline uint8_t Scan_loop()
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Signal KeyIndex_Buffer that it has been properly read
|
||||||
|
void Scan_finishedWithBuffer( uint8_t sentKeys )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Signal that the keys have been properly sent over USB
|
||||||
|
void Scan_finishedWithUSBBuffer( uint8_t sentKeys )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reset Keyboard
|
||||||
|
void Scan_resetKeyboard()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ----- CLI Command Functions -----
|
||||||
|
|
||||||
|
// XXX Just an example command showing how to parse arguments (more complex than generally needed)
|
||||||
|
void cliFunc_echo( char* args )
|
||||||
|
{
|
||||||
|
char* curArgs;
|
||||||
|
char* arg1Ptr;
|
||||||
|
char* arg2Ptr = args;
|
||||||
|
|
||||||
|
print( NL ); // No \n by default after the command is entered
|
||||||
|
|
||||||
|
// Parse args until a \0 is found
|
||||||
|
while ( 1 )
|
||||||
|
{
|
||||||
|
curArgs = arg2Ptr; // Use the previous 2nd arg pointer to separate the next arg from the list
|
||||||
|
CLI_argumentIsolation( curArgs, &arg1Ptr, &arg2Ptr );
|
||||||
|
|
||||||
|
// Stop processing args if no more are found
|
||||||
|
if ( *arg1Ptr == '\0' )
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Print out the arg
|
||||||
|
dPrint( arg1Ptr );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
65
Scan/ADCTest/scan_loop.h
Normal file
65
Scan/ADCTest/scan_loop.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/* Copyright (C) 2014 by Jacob Alexander
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in
|
||||||
|
* all copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
* THE SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __SCAN_LOOP_H
|
||||||
|
#define __SCAN_LOOP_H
|
||||||
|
|
||||||
|
// ----- Includes -----
|
||||||
|
|
||||||
|
// Compiler Includes
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
// Local Includes
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Defines -----
|
||||||
|
|
||||||
|
#define KEYBOARD_KEYS 0x7F // 127 - Size of the array space for the keyboard(max index)
|
||||||
|
#define KEYBOARD_BUFFER 24 // Max number of key signals to buffer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Variables -----
|
||||||
|
|
||||||
|
extern volatile uint8_t KeyIndex_Buffer[KEYBOARD_BUFFER];
|
||||||
|
extern volatile uint8_t KeyIndex_BufferUsed;
|
||||||
|
extern volatile uint8_t KeyIndex_Add_InputSignal;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ----- Functions -----
|
||||||
|
|
||||||
|
// Functions used by main.c
|
||||||
|
void Scan_setup();
|
||||||
|
uint8_t Scan_loop();
|
||||||
|
|
||||||
|
|
||||||
|
// Functions available to macro.c
|
||||||
|
uint8_t Scan_sendData( uint8_t dataPayload );
|
||||||
|
|
||||||
|
void Scan_finishedWithBuffer( uint8_t sentKeys );
|
||||||
|
void Scan_finishedWithUSBBuffer( uint8_t sentKeys );
|
||||||
|
|
||||||
|
void Scan_resetKeyboard();
|
||||||
|
|
||||||
|
#endif // __SCAN_LOOP_H
|
||||||
|
|
33
Scan/ADCTest/setup.cmake
Normal file
33
Scan/ADCTest/setup.cmake
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
###| CMake Kiibohd Controller Scan Module |###
|
||||||
|
#
|
||||||
|
# Written by Jacob Alexander in 2014 for the Kiibohd Controller
|
||||||
|
#
|
||||||
|
# Released into the Public Domain
|
||||||
|
#
|
||||||
|
# ADC/DAC example. DAC only works on microcontrollers that support it.
|
||||||
|
#
|
||||||
|
###
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
# Module C files
|
||||||
|
#
|
||||||
|
|
||||||
|
set( SCAN_SRCS
|
||||||
|
scan_loop.c
|
||||||
|
analog.c
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
# Module Specific Options
|
||||||
|
#
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
# Compiler Family Compatibility
|
||||||
|
#
|
||||||
|
set( ScanModuleCompatibility
|
||||||
|
arm
|
||||||
|
)
|
||||||
|
|
4
main.c
4
main.c
@ -146,7 +146,7 @@ int main(void)
|
|||||||
// Setup Modules
|
// Setup Modules
|
||||||
Output_setup();
|
Output_setup();
|
||||||
Macro_setup();
|
Macro_setup();
|
||||||
//scan_setup();
|
Scan_setup();
|
||||||
|
|
||||||
// Setup ISR Timer for flagging a kepress send to USB
|
// Setup ISR Timer for flagging a kepress send to USB
|
||||||
usbTimerSetup();
|
usbTimerSetup();
|
||||||
@ -184,7 +184,7 @@ int main(void)
|
|||||||
// USB Keyboard Data Send Counter Interrupt
|
// USB Keyboard Data Send Counter Interrupt
|
||||||
#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
|
#if defined(_at90usb162_) || defined(_atmega32u4_) || defined(_at90usb646_) || defined(_at90usb1286_) // AVR
|
||||||
ISR( TIMER0_OVF_vect )
|
ISR( TIMER0_OVF_vect )
|
||||||
#elif defined(_mk20dx128_) // ARM
|
#elif defined(_mk20dx128_) || defined(_mk20dx256_) // ARM
|
||||||
void pit0_isr(void)
|
void pit0_isr(void)
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user