|
|
|
@@ -5,69 +5,276 @@
|
|
|
|
|
#ifndef _CC2650_MASTER_H_
|
|
|
|
|
#define _CC2650_MASTER_H_
|
|
|
|
|
|
|
|
|
|
#include <Board.h>
|
|
|
|
|
|
|
|
|
|
#include <ti/drivers/SPI.h>
|
|
|
|
|
|
|
|
|
|
#include <ti/drivers/spi/SPICC26XXDMA.h>
|
|
|
|
|
|
|
|
|
|
#include <ti/drivers/dma/UDMACC26XX.h>
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* SPI parameter and function declaration
|
|
|
|
|
*/
|
|
|
|
|
static SPI_Handle spi_handle;
|
|
|
|
|
static SPI_Params spi_parameter;
|
|
|
|
|
static SPI_Transaction spi_transaction;
|
|
|
|
|
|
|
|
|
|
static uint8_t spi_ms_value; // memory select
|
|
|
|
|
static uint8_t spi_instruction[32] = {0};
|
|
|
|
|
/*
|
|
|
|
|
* pin parameter, configuration and function declaration
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define SPI_MEM_BUFFER_SIZE 256
|
|
|
|
|
#define HTA_LED_RD IOID_6
|
|
|
|
|
#define HTA_LED_GN IOID_7
|
|
|
|
|
#define HTA_PIN_MS IOID_15
|
|
|
|
|
#define HTA_PIN_BZ IOID_11
|
|
|
|
|
#define HTA_PIN_RQ IOID_12
|
|
|
|
|
#define HTA_PIN_CS IOID_14
|
|
|
|
|
#define HTA_PIN_RS IOID_10
|
|
|
|
|
#define HTA_SPI_CS HTA_PIN_CS
|
|
|
|
|
#define HTA_SPI_CK Board_SPI0_CLK
|
|
|
|
|
#define HTA_SPI_DI Board_SPI0_MISO
|
|
|
|
|
#define HTA_SPI_DO Board_SPI0_MOSI
|
|
|
|
|
#define HTA_PIN_D1 IOID_4
|
|
|
|
|
#define HTA_PIN_D2 IOID_5
|
|
|
|
|
#define HTA_PIN_D3 IOID_21
|
|
|
|
|
#define HTA_PIN_D4 IOID_22
|
|
|
|
|
|
|
|
|
|
static PIN_State hta_pins;
|
|
|
|
|
static PIN_Handle hta_pins_handle;
|
|
|
|
|
|
|
|
|
|
static PIN_Config HTA_pin_config_table[] = { //
|
|
|
|
|
// LED
|
|
|
|
|
HTA_LED_RD | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
|
|
|
|
|
HTA_LED_GN | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL | PIN_DRVSTR_MAX,
|
|
|
|
|
// memory select
|
|
|
|
|
HTA_PIN_MS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL,
|
|
|
|
|
// spi chip select, drop netive CS pin
|
|
|
|
|
HTA_PIN_CS | PIN_GPIO_OUTPUT_EN | PIN_GPIO_HIGH | PIN_PUSHPULL,
|
|
|
|
|
// master side is reading buffer.
|
|
|
|
|
HTA_PIN_BZ | PIN_INPUT_EN | PIN_PULLDOWN | PIN_HYSTERESIS,
|
|
|
|
|
// master side request to switch buffer
|
|
|
|
|
HTA_PIN_RQ | PIN_INPUT_EN | PIN_PULLDOWN | PIN_HYSTERESIS,
|
|
|
|
|
// spi reset
|
|
|
|
|
HTA_PIN_RS | PIN_INPUT_EN | PIN_PULLDOWN | PIN_HYSTERESIS,
|
|
|
|
|
// debug use pin
|
|
|
|
|
#ifdef HTA_PIN_D1
|
|
|
|
|
HTA_PIN_D1 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL,
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HTA_PIN_D2
|
|
|
|
|
HTA_PIN_D2 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL,
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HTA_PIN_D3
|
|
|
|
|
HTA_PIN_D3 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL,
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef HTA_PIN_D4
|
|
|
|
|
HTA_PIN_D4 | PIN_GPIO_OUTPUT_EN | PIN_GPIO_LOW | PIN_PUSHPULL,
|
|
|
|
|
#endif
|
|
|
|
|
PIN_TERMINATE};
|
|
|
|
|
|
|
|
|
|
#define HTA_main_spi_transaction(len, tx, rx) \
|
|
|
|
|
do { \
|
|
|
|
|
spi_transaction.txBuf = (tx); \
|
|
|
|
|
spi_transaction.rxBuf = (rx); \
|
|
|
|
|
spi_transaction.count = (len); \
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_CS, 0); \
|
|
|
|
|
SPI_transfer(spi_handle, &spi_transaction); \
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_CS, 1); \
|
|
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define FLAG_RESET 0x010
|
|
|
|
|
#define FLAG_REQUEST 0x020
|
|
|
|
|
|
|
|
|
|
static void HTA_main_pin_callback(PIN_Handle handle, PIN_Id pin);
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* SPI
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define SPI_MEM_SWITCH_THRESHOLD 0x1000
|
|
|
|
|
#define SPI_TX_BUFFER_SIZE 32
|
|
|
|
|
|
|
|
|
|
#define MSM_REG_WRITE 0x01
|
|
|
|
|
#define MEM_INS_WRITE 0x02
|
|
|
|
|
#define MEM_INS_READ 0x03
|
|
|
|
|
#define MEM_REG_READ 0x05
|
|
|
|
|
|
|
|
|
|
#define MEM_META_LENGTH 4
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* notify use variable
|
|
|
|
|
*/
|
|
|
|
|
static uint16_t not_counter = 0; // writing counter, increase when notify
|
|
|
|
|
static uint16_t not_offset = 0; // writing pointer, current not writing index
|
|
|
|
|
|
|
|
|
|
static uint8_t spi_ms_value; // memory select
|
|
|
|
|
static uint8_t spi_tx_buffer[SPI_TX_BUFFER_SIZE] = {0};
|
|
|
|
|
|
|
|
|
|
static void HTA_main_periodic_handle();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* initialization function
|
|
|
|
|
*/
|
|
|
|
|
static void HTA_main_init() {
|
|
|
|
|
// SPI initial
|
|
|
|
|
SPI_init();
|
|
|
|
|
|
|
|
|
|
// SPI parameters initialize
|
|
|
|
|
SPI_Params spi_parameter;
|
|
|
|
|
SPI_Params_init(&spi_parameter);
|
|
|
|
|
// spi_parameter.transferMode = SPI_MODE_CALLBACK;
|
|
|
|
|
// spi_parameter.transferCallbackFxn = HTA_main_spi_callback;
|
|
|
|
|
spi_parameter.transferMode = SPI_MODE_BLOCKING;
|
|
|
|
|
spi_parameter.mode = SPI_MASTER;
|
|
|
|
|
spi_parameter.bitRate = 8000000; // 12 MHz
|
|
|
|
|
spi_parameter.bitRate = 12000000; // 12 MHz
|
|
|
|
|
spi_parameter.transferTimeout = 1000;
|
|
|
|
|
spi_parameter.dataSize = 8;
|
|
|
|
|
spi_parameter.frameFormat = SPI_POL0_PHA1;
|
|
|
|
|
spi_parameter.frameFormat = SPI_POL0_PHA0;
|
|
|
|
|
|
|
|
|
|
// SPI open
|
|
|
|
|
spi_handle = SPI_open(Board_SPI0, &spi_parameter);
|
|
|
|
|
|
|
|
|
|
// pin initialize
|
|
|
|
|
hta_pins_handle = PIN_open(&hta_pins, HTA_pin_config_table);
|
|
|
|
|
PIN_registerIntCb(hta_pins_handle, HTA_main_pin_callback);
|
|
|
|
|
PIN_setInterrupt(hta_pins_handle, HTA_PIN_RS | PIN_IRQ_NEGEDGE);
|
|
|
|
|
PIN_setInterrupt(hta_pins_handle, HTA_PIN_RQ | PIN_IRQ_POSEDGE);
|
|
|
|
|
|
|
|
|
|
// initial external memory
|
|
|
|
|
spi_tx_buffer[0] = MSM_REG_WRITE; // Write STATUS register
|
|
|
|
|
spi_tx_buffer[1] = 0b01000011; // Sequential mode with HOLD function disable
|
|
|
|
|
|
|
|
|
|
// set first memory
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_MS, 1);
|
|
|
|
|
HTA_main_spi_transaction(2, spi_tx_buffer, NULL);
|
|
|
|
|
|
|
|
|
|
// second memory
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_MS, 0);
|
|
|
|
|
HTA_main_spi_transaction(2, spi_tx_buffer, NULL);
|
|
|
|
|
|
|
|
|
|
// init
|
|
|
|
|
not_offset = MEM_META_LENGTH;
|
|
|
|
|
spi_ms_value = 0;
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
/*HTA_periodic_start();
|
|
|
|
|
HTA_periodic_set_frequency(100);
|
|
|
|
|
HTA_set_event_callback(HTA_PERIODIC_EVT, HTA_main_periodic_handle);*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void HTA_switch_buffer() {
|
|
|
|
|
#ifdef HTA_PIN_D2
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_D2, PIN_getOutputValue(HTA_PIN_D2) ? 0 : 1);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
uint16_t cnt_offset = not_offset;
|
|
|
|
|
|
|
|
|
|
// localize current buffer
|
|
|
|
|
uint8_t *p = spi_tx_buffer;
|
|
|
|
|
|
|
|
|
|
// write length meta information
|
|
|
|
|
*p++ = MEM_INS_WRITE;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
*p++ = (uint8_t)((cnt_offset >> 8) & 0xFF);
|
|
|
|
|
*p++ = (uint8_t)(cnt_offset & 0xFF);
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
*p++ = 0;
|
|
|
|
|
|
|
|
|
|
HTA_main_spi_transaction(p - spi_tx_buffer, spi_tx_buffer, NULL);
|
|
|
|
|
|
|
|
|
|
// switch memory
|
|
|
|
|
uint8_t value = spi_ms_value++;
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_MS, (value & 1));
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_LED_RD, (value & 0x10) ? 1 : 0);
|
|
|
|
|
|
|
|
|
|
not_offset = MEM_META_LENGTH;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* notify handle
|
|
|
|
|
*/
|
|
|
|
|
static void HTA_main_handle_notify(uint16 length, uint8 *value) {
|
|
|
|
|
not_counter = (not_counter + 1) & 0xFF;
|
|
|
|
|
#ifdef HTA_PIN_D1
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_D1, PIN_getOutputValue(HTA_PIN_D1) ? 0 : 1);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// memory select
|
|
|
|
|
if (not_offset + length > SPI_MEM_BUFFER_SIZE) {
|
|
|
|
|
spi_ms_value = (spi_ms_value > 0) ? 0 : 1;
|
|
|
|
|
PIN_setOutputValue(sbp_pins_handle, Board_DIO12, spi_ms_value);
|
|
|
|
|
PIN_setOutputValue(sbp_pins_handle, Board_RLED, spi_ms_value);
|
|
|
|
|
// update counter
|
|
|
|
|
uint8_t counter = not_counter++;
|
|
|
|
|
|
|
|
|
|
not_offset = 0;
|
|
|
|
|
// reset
|
|
|
|
|
if (flag_mask(FLAG_RESET)) {
|
|
|
|
|
flag_disable(FLAG_RESET);
|
|
|
|
|
not_offset = MEM_META_LENGTH;
|
|
|
|
|
counter = not_counter = 0;
|
|
|
|
|
|
|
|
|
|
#ifdef HTA_PIN_D4
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_D4, 0);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// buffer
|
|
|
|
|
spi_instruction[0] = 0x02; // instruction write
|
|
|
|
|
spi_instruction[1] = (uint8_t)(not_offset & 0xFF); // instruction address
|
|
|
|
|
spi_instruction[2] = (uint8_t)((not_offset >> 8) & 0xFF);
|
|
|
|
|
memcpy(spi_instruction + 3, value, length);
|
|
|
|
|
not_offset += length;
|
|
|
|
|
// localize current buffer
|
|
|
|
|
uint8_t *p = spi_tx_buffer;
|
|
|
|
|
|
|
|
|
|
// update offset
|
|
|
|
|
uint32_t cnt_offset = not_offset;
|
|
|
|
|
not_offset = cnt_offset + 3 + length;
|
|
|
|
|
|
|
|
|
|
// spi tx content
|
|
|
|
|
*p++ = MEM_INS_WRITE;
|
|
|
|
|
*p++ = (uint8_t)((cnt_offset >> 8) & 0xFF);
|
|
|
|
|
*p++ = (uint8_t)(cnt_offset & 0xFF);
|
|
|
|
|
|
|
|
|
|
// data header
|
|
|
|
|
*p++ = 0xFF;
|
|
|
|
|
*p++ = counter;
|
|
|
|
|
*p++ = length;
|
|
|
|
|
|
|
|
|
|
// data content
|
|
|
|
|
memcpy(p, value, length);
|
|
|
|
|
|
|
|
|
|
// SPI transaction
|
|
|
|
|
spi_transaction.rxBuf = NULL;
|
|
|
|
|
spi_transaction.txBuf = spi_instruction;
|
|
|
|
|
spi_transaction.count = length + 3;
|
|
|
|
|
|
|
|
|
|
SPI_transfer(spi_handle, &spi_transaction);
|
|
|
|
|
HTA_main_spi_transaction(p - spi_tx_buffer + length, spi_tx_buffer, NULL);
|
|
|
|
|
|
|
|
|
|
// update LED state
|
|
|
|
|
PIN_setOutputValue(sbp_pins_handle, Board_GLED, (not_counter & 0x10) ? Board_LED_ON : Board_LED_OFF);
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_LED_GN, (not_counter & 0x10) ? 1 : 0);
|
|
|
|
|
|
|
|
|
|
// memory select
|
|
|
|
|
if ((cnt_offset > SPI_MEM_SWITCH_THRESHOLD || flag_mask(FLAG_REQUEST)) && !PIN_getInputValue(HTA_PIN_BZ)) {
|
|
|
|
|
flag_disable(FLAG_REQUEST);
|
|
|
|
|
#ifdef HTA_PIN_D3
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_D3, 0);
|
|
|
|
|
#endif
|
|
|
|
|
HTA_switch_buffer();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif // _CC2650_MASTER_H_
|
|
|
|
|
/**
|
|
|
|
|
* pin callback
|
|
|
|
|
*/
|
|
|
|
|
static void HTA_main_pin_callback(PIN_Handle handle, PIN_Id pin) {
|
|
|
|
|
if (pin == HTA_PIN_RS) {
|
|
|
|
|
// reset
|
|
|
|
|
flag_enable(FLAG_RESET);
|
|
|
|
|
#ifdef HTA_PIN_D4
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_D4, 1);
|
|
|
|
|
#endif
|
|
|
|
|
} else if (pin == HTA_PIN_RQ) {
|
|
|
|
|
flag_enable(FLAG_REQUEST);
|
|
|
|
|
#ifdef HTA_PIN_D3
|
|
|
|
|
PIN_setOutputValue(hta_pins_handle, HTA_PIN_D3, 1);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*static void HTA_main_periodic_handle() {
|
|
|
|
|
uint8_t tmp[16];
|
|
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < 16; i++) {
|
|
|
|
|
tmp[i] = i;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HTA_main_handle_notify(16, tmp);
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
|
|
#endif // _CC2650_MASTER_H_
|
|
|
|
|