Compare commits

..

2 Commits

Author SHA1 Message Date
Ta-Shun Su 84afb22d93 version 0.4 2019-04-30 17:32:24 +08:00
Ta-Shun Su 3ca5c026c9 version 0.3 2019-04-30 17:32:15 +08:00
4 changed files with 208 additions and 36 deletions
@@ -84,10 +84,10 @@ extern const PIN_Config BoardGpioInitTable[];
#define Board_UART_RTS IOID_18 /* RTS */
/* SPI Board */
#define Board_SPI0_MISO IOID_8 /* RF1.20 */
#define Board_SPI0_MOSI IOID_9 /* RF1.18 */
#define Board_SPI0_CLK IOID_10 /* RF1.16 */
#define Board_SPI0_CSN IOID_11
#define Board_SPI0_MISO IOID_9 /* RF1.20 */
#define Board_SPI0_MOSI IOID_8 /* RF1.18 */
#define Board_SPI0_CLK IOID_13 /* RF1.16 */
#define Board_SPI0_CSN PIN_UNASSIGNED
#define Board_SPI1_MISO PIN_UNASSIGNED
#define Board_SPI1_MOSI PIN_UNASSIGNED
#define Board_SPI1_CLK PIN_UNASSIGNED
@@ -5,69 +5,237 @@
#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 native 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,
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_REQST 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 = 0; // 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_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;
}
static void HTA_switch_buffer() {
uint16_t cnt_offset = not_offset;
not_offset = MEM_META_LENGTH;
// localize current buffer
uint8_t *p = spi_tx_buffer;
// write length meta information
*p++ = MEM_INS_WRITE;
// target address
*p++ = 0;
*p++ = 0;
// data content
*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 & 0x01));
PIN_setOutputValue(hta_pins_handle, HTA_LED_RD, (value & 0x10) ? 1 : 0);
}
/**
* notify handle
*/
static void HTA_main_handle_notify(uint16 length, uint8 *value) {
not_counter = (not_counter + 1) & 0xFF;
// update counter
uint8_t counter;
// 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);
not_offset = 0;
if (PIN_getInputValue(HTA_PIN_RS)) {
// reset, ignore all incoming notify
return;
} else if (flag_mask(FLAG_RESET)) {
// reset
flag_disable(FLAG_RESET);
not_offset = MEM_META_LENGTH;
counter = not_counter = 0;
} else {
counter = not_counter++;
}
// 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;
// instruction
*p++ = MEM_INS_WRITE;
// target address
*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/switch
if ((not_offset >= SPI_MEM_SWITCH_THRESHOLD || flag_mask(FLAG_REQST)) && !PIN_getInputValue(HTA_PIN_BZ)) {
flag_disable(FLAG_REQST);
HTA_switch_buffer();
flag_disable(FLAG_REQST);
}
}
#endif // _CC2650_MASTER_H_
/**
* pin callback
*/
static void HTA_main_pin_callback(PIN_Handle handle, PIN_Id pin) {
switch(pin) {
case HTA_PIN_RS:
// reset
flag_enable(FLAG_RESET);
break;
case HTA_PIN_RQ:
// memory switch request
flag_enable(FLAG_REQST);
break;
}
}
#endif // _CC2650_MASTER_H_
@@ -165,7 +165,7 @@ static void HTA_main_init() {
}
/*
* spi function
* notify use variable
*/
static uint8_t not_counter = 0; // writing counter, increase when notify
@@ -95,9 +95,13 @@
#include "host_test_app.h"
//#define PERIODIC_USE_GPTIMER
#include "cc2650/cc2650_util.h"
#include "cc2650/cc2650_slave.h"
//#include "cc2650/cc2650_slave.h"
#include "cc2650/cc2650_master.h"
// LE Event Lengths
#define HCI_CMD_COMPLETE_EVENT_LEN 3
@@ -331,12 +335,12 @@ static void HTA_handle_att_event(gattMsgEvent_t *message) {
} else if ((method == ATT_WRITE_RSP) || (method == ATT_ERROR_RSP && message->msg.errorRsp.reqOpcode == ATT_WRITE_REQ)) {
// pass
} else if (method == ATT_HANDLE_VALUE_NOTI) {
#ifndef HTA_PIN_RS
#error "HTA_PIN_RS not defined"
#else
#ifdef HTA_PIN_RS
if (!PIN_getInputValue(HTA_PIN_RS)) {
#endif
attHandleValueNoti_t *att_notify = (attHandleValueNoti_t *)(&message->msg);
HTA_main_handle_notify(att_notify->len, att_notify->pValue);
#ifdef HTA_PIN_RS
}
#endif
}