NFC capability started
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
#include "em_cryotimer.h"
|
||||
#include "em_crypto.h"
|
||||
#include "em_gpio.h"
|
||||
#include "em_ldma.h"
|
||||
#include "em_i2c.h"
|
||||
#include "em_usart.h"
|
||||
// [Library includes]$
|
||||
|
||||
@@ -39,7 +39,7 @@ extern void enter_DefaultMode_from_RESET(void) {
|
||||
ADC0_enter_DefaultMode_from_RESET();
|
||||
USART0_enter_DefaultMode_from_RESET();
|
||||
USART1_enter_DefaultMode_from_RESET();
|
||||
LDMA_enter_DefaultMode_from_RESET();
|
||||
I2C0_enter_DefaultMode_from_RESET();
|
||||
CRYOTIMER_enter_DefaultMode_from_RESET();
|
||||
PORTIO_enter_DefaultMode_from_RESET();
|
||||
// [Config Calls]$
|
||||
@@ -104,7 +104,7 @@ extern void CMU_enter_DefaultMode_from_RESET(void) {
|
||||
CMU_HFXOInit(&hfxoInit);
|
||||
|
||||
/* Setting system HFRCO frequency */
|
||||
CMU_HFRCOFreqSet (cmuHFRCOFreq_38M0Hz);
|
||||
CMU_HFRCOFreqSet (cmuHFRCOFreq_13M0Hz);
|
||||
|
||||
/* Using HFRCO as high frequency clock, HFCLK */
|
||||
CMU_ClockSelectSet(cmuClock_HF, cmuSelect_HFRCO);
|
||||
@@ -135,8 +135,8 @@ extern void CMU_enter_DefaultMode_from_RESET(void) {
|
||||
/* Enable clock for CRYOTIMER */
|
||||
CMU_ClockEnable(cmuClock_CRYOTIMER, true);
|
||||
|
||||
/* Enable clock for LDMA */
|
||||
CMU_ClockEnable(cmuClock_LDMA, true);
|
||||
/* Enable clock for I2C0 */
|
||||
CMU_ClockEnable(cmuClock_I2C0, true);
|
||||
|
||||
/* Enable clock for USART0 */
|
||||
CMU_ClockEnable(cmuClock_USART0, true);
|
||||
@@ -455,9 +455,24 @@ extern void WDOG0_enter_DefaultMode_from_RESET(void) {
|
||||
extern void I2C0_enter_DefaultMode_from_RESET(void) {
|
||||
|
||||
// $[I2C0 I/O setup]
|
||||
/* Set up SCL */
|
||||
I2C0->ROUTEPEN = I2C0->ROUTEPEN | I2C_ROUTEPEN_SCLPEN;
|
||||
I2C0->ROUTELOC0 = (I2C0->ROUTELOC0 & (~_I2C_ROUTELOC0_SCLLOC_MASK))
|
||||
| I2C_ROUTELOC0_SCLLOC_LOC3;
|
||||
/* Set up SDA */
|
||||
I2C0->ROUTEPEN = I2C0->ROUTEPEN | I2C_ROUTEPEN_SDAPEN;
|
||||
I2C0->ROUTELOC0 = (I2C0->ROUTELOC0 & (~_I2C_ROUTELOC0_SDALOC_MASK))
|
||||
| I2C_ROUTELOC0_SDALOC_LOC27;
|
||||
// [I2C0 I/O setup]$
|
||||
|
||||
// $[I2C0 initialization]
|
||||
I2C_Init_TypeDef init = I2C_INIT_DEFAULT;
|
||||
|
||||
init.enable = 1;
|
||||
init.master = 1;
|
||||
init.freq = I2C_FREQ_STANDARD_MAX;
|
||||
init.clhr = i2cClockHLRStandard;
|
||||
I2C_Init(I2C0, &init);
|
||||
// [I2C0 initialization]$
|
||||
|
||||
}
|
||||
@@ -610,6 +625,9 @@ extern void PORTIO_enter_DefaultMode_from_RESET(void) {
|
||||
/* Pin PA3 is configured to Push-pull */
|
||||
GPIO_PinModeSet(gpioPortA, 3, gpioModePushPull, 0);
|
||||
|
||||
/* Pin PA4 is configured to Open-drain with pull-up and filter */
|
||||
GPIO_PinModeSet(gpioPortA, 4, gpioModeWiredAndPullUpFilter, 0);
|
||||
|
||||
/* Pin PA5 is configured to Push-pull */
|
||||
GPIO_PinModeSet(gpioPortA, 5, gpioModePushPull, 1);
|
||||
// [Port A Configuration]$
|
||||
@@ -649,6 +667,9 @@ extern void PORTIO_enter_DefaultMode_from_RESET(void) {
|
||||
|
||||
// $[Port F Configuration]
|
||||
|
||||
/* Pin PF3 is configured to Open-drain with pull-up and filter */
|
||||
GPIO_PinModeSet(gpioPortF, 3, gpioModeWiredAndPullUpFilter, 0);
|
||||
|
||||
/* Pin PF4 is configured to Push-pull */
|
||||
GPIO_PinModeSet(gpioPortF, 4, gpioModePushPull, 0);
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "em_adc.h"
|
||||
#include "em_cmu.h"
|
||||
#include "em_msc.h"
|
||||
#include "em_i2c.h"
|
||||
|
||||
#include "InitDevice.h"
|
||||
#include "cbor.h"
|
||||
@@ -23,6 +24,7 @@
|
||||
#include "app.h"
|
||||
#include "uECC.h"
|
||||
#include "crypto.h"
|
||||
#include "nfc.h"
|
||||
|
||||
#define MSG_AVAIL_PIN gpioPortC,9
|
||||
#define RDY_PIN gpioPortC,10
|
||||
@@ -117,17 +119,19 @@ int ctap_user_presence_test()
|
||||
|
||||
// Must be implemented by application
|
||||
// data is HID_MESSAGE_SIZE long in bytes
|
||||
#ifndef TEST_POWER
|
||||
void ctaphid_write_block(uint8_t * data)
|
||||
{
|
||||
printf1(TAG_DUMP,"<< "); dump_hex1(TAG_DUMP, data, HID_MESSAGE_SIZE);
|
||||
usbhid_send(data);
|
||||
}
|
||||
#endif
|
||||
|
||||
void heartbeat()
|
||||
{
|
||||
GPIO_PinOutToggle(gpioPortF,4);
|
||||
GPIO_PinOutToggle(gpioPortF,5);
|
||||
|
||||
nfc_test();
|
||||
// printf("heartbeat %d %d\r\n", beat++,CRYOTIMER->CNT);
|
||||
}
|
||||
|
||||
@@ -158,6 +162,7 @@ static void wait_for_efm8_busy()
|
||||
;
|
||||
}
|
||||
|
||||
#ifndef TEST_POWER
|
||||
int usbhid_recv(uint8_t * msg)
|
||||
{
|
||||
int i;
|
||||
@@ -188,6 +193,8 @@ int usbhid_recv(uint8_t * msg)
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void usbhid_send(uint8_t * msg)
|
||||
{
|
||||
int i;
|
||||
|
||||
178
efm32/src/nfc.c
Normal file
178
efm32/src/nfc.c
Normal file
@@ -0,0 +1,178 @@
|
||||
/*
|
||||
* nfc.c
|
||||
*
|
||||
* Created on: Jul 22, 2018
|
||||
* Author: conor
|
||||
*/
|
||||
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "em_chip.h"
|
||||
#include "em_gpio.h"
|
||||
#include "em_i2c.h"
|
||||
|
||||
#include "log.h"
|
||||
#include "util.h"
|
||||
#include "nfc.h"
|
||||
#include "app.h"
|
||||
|
||||
|
||||
I2C_TransferReturn_TypeDef I2CSPM_Transfer(I2C_TypeDef *i2c, I2C_TransferSeq_TypeDef *seq)
|
||||
{
|
||||
I2C_TransferReturn_TypeDef ret;
|
||||
uint32_t timeout = 10000;
|
||||
/* Do a polled transfer */
|
||||
ret = I2C_TransferInit(i2c, seq);
|
||||
while (ret == i2cTransferInProgress && timeout--)
|
||||
{
|
||||
ret = I2C_Transfer(i2c);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// data must be 16 bytes
|
||||
void read_block(uint8_t block, uint8_t * data)
|
||||
{
|
||||
uint8_t addr = 0xAA;
|
||||
I2C_TransferSeq_TypeDef seq;
|
||||
I2C_TransferReturn_TypeDef ret;
|
||||
uint8_t i2c_read_data[16];
|
||||
uint8_t i2c_write_data[1];
|
||||
|
||||
seq.addr = addr;
|
||||
seq.flags = I2C_FLAG_WRITE_READ;
|
||||
/* Select command to issue */
|
||||
i2c_write_data[0] = block;
|
||||
seq.buf[0].data = i2c_write_data;
|
||||
seq.buf[0].len = 1;
|
||||
/* Select location/length of data to be read */
|
||||
seq.buf[1].data = i2c_read_data;
|
||||
seq.buf[1].len = 16;
|
||||
|
||||
ret = I2CSPM_Transfer(I2C0, &seq);
|
||||
|
||||
if (ret != i2cTransferDone) {
|
||||
printf("I2C fail %04x\r\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
memmove(data, i2c_read_data, 16);
|
||||
}
|
||||
void write_reg_flash(uint8_t reg_addr, uint8_t mask,uint8_t data)
|
||||
{
|
||||
uint8_t addr = 0xAA;
|
||||
I2C_TransferSeq_TypeDef seq;
|
||||
I2C_TransferReturn_TypeDef ret;
|
||||
uint8_t i2c_write_data[4];
|
||||
|
||||
seq.addr = addr;
|
||||
seq.flags = I2C_FLAG_WRITE;
|
||||
i2c_write_data[0] = 0x3a;
|
||||
i2c_write_data[1] = reg_addr;
|
||||
i2c_write_data[2] = mask;
|
||||
i2c_write_data[3] = data;
|
||||
|
||||
seq.buf[0].data = i2c_write_data;
|
||||
seq.buf[0].len = 4;
|
||||
seq.buf[1].data = NULL;
|
||||
seq.buf[1].len = 0;
|
||||
|
||||
ret = I2CSPM_Transfer(I2C0, &seq);
|
||||
|
||||
if (ret != i2cTransferDone) {
|
||||
printf("I2C fail %04x\r\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
void write_reg(uint8_t reg_addr, uint8_t mask,uint8_t data)
|
||||
{
|
||||
uint8_t addr = 0xAA;
|
||||
I2C_TransferSeq_TypeDef seq;
|
||||
I2C_TransferReturn_TypeDef ret;
|
||||
uint8_t i2c_write_data[4];
|
||||
|
||||
seq.addr = addr;
|
||||
seq.flags = I2C_FLAG_WRITE;
|
||||
i2c_write_data[0] = 0xFE;
|
||||
i2c_write_data[1] = reg_addr;
|
||||
i2c_write_data[2] = mask;
|
||||
i2c_write_data[3] = data;
|
||||
|
||||
seq.buf[0].data = i2c_write_data;
|
||||
seq.buf[0].len = 4;
|
||||
seq.buf[1].data = NULL;
|
||||
seq.buf[1].len = 0;
|
||||
|
||||
ret = I2CSPM_Transfer(I2C0, &seq);
|
||||
|
||||
if (ret != i2cTransferDone) {
|
||||
printf("I2C fail %04x\r\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t read_reg(uint8_t reg_addr)
|
||||
{
|
||||
I2C_TransferSeq_TypeDef seq;
|
||||
I2C_TransferReturn_TypeDef ret;
|
||||
uint8_t write_data[2];
|
||||
uint8_t read_data[1];
|
||||
|
||||
seq.addr = 0xAA;
|
||||
seq.flags = I2C_FLAG_WRITE_READ;
|
||||
write_data[0] = 0xFE;
|
||||
write_data[1] = reg_addr;
|
||||
|
||||
seq.buf[0].data = write_data;
|
||||
seq.buf[0].len = 2;
|
||||
seq.buf[1].data = read_data;
|
||||
seq.buf[1].len = 1;
|
||||
|
||||
ret = I2CSPM_Transfer(I2C0, &seq);
|
||||
|
||||
if (ret != i2cTransferDone) {
|
||||
printf("I2C fail %04x\r\n",ret);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
return read_data[0];
|
||||
}
|
||||
|
||||
void read_reg_block(uint8_t * data)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < 7; i++)
|
||||
{
|
||||
*data = read_reg(i);
|
||||
// printf("data %d: %x\n" ,i,(int)(*data));
|
||||
data++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void nfc_test()
|
||||
{
|
||||
uint8_t data[16];
|
||||
printf("-NFC test-\n");
|
||||
|
||||
read_block(0x00, data);
|
||||
printf("block 00: "); dump_hex(data,16);
|
||||
|
||||
read_reg_block(data);
|
||||
printf("block 3A [init]:"); dump_hex(data,8);
|
||||
|
||||
write_reg(0, 0xff, 0x43);
|
||||
write_reg_flash(0, 0xff, 0x43);
|
||||
write_reg(2, 0xff, 0x01);
|
||||
|
||||
read_reg_block(data);
|
||||
printf("block 3A [done]:"); dump_hex(data,8);
|
||||
//
|
||||
// read_block(0x3A, data);
|
||||
// printf("block 3A [done]:"); dump_hex(data,16);
|
||||
|
||||
while(1)
|
||||
;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user