separate c file and header file

This commit is contained in:
weiting2
2021-06-21 16:14:25 +08:00
parent a0c7fac713
commit 0db0572e7e
8 changed files with 428 additions and 611 deletions
@@ -0,0 +1,281 @@
#include "mem_board_central.h"
#include "mem_uart.h"
#include "mem_central_spi.h"
#include "mem_central_handle_notify.h"
#include "mem_event.h"
#include "util.h"
#define BLE_CDR_SAMLL_SIZE 10
#define BLE_CHAR2_HANDLE 0x0021 // read CIS return data
#define BLE_CHAR3_HANDLE 0x0024 // send instruction
#define BLE_CHAR4_CONFIG_HANDLE 0x0028 // notify enable: 0100=enable; 0000=disable
#define RET_INFO_BUF_SIZE 52
#define CIS_BUFF_PREFIX 3
#define CIS_BUFF_SUFFIX 2
static uint16_t CIS_length = 10;
static char CIS_data[50];
static void mem_central_event();
static void mem_connect_device(uint8_t addrType, uint8_t *peerAddr);
static void mem_send_ins(uint16_t hadnle, uint8_t *value);
static void mem_read_data_and_return();
static void mem_central_get_cis(uint8_t handle);
static void mem_connection_timeout();
// Application states
typedef enum
{
BLE_STATE_IDLE,
BLE_STATE_BROWSING,
BLE_STATE_CONNECTING,
BLE_STATE_CONNECTED,
BLE_STATE_DISCOVERED,
BLE_STATE_DISCONNECTING
} bleState_t;
extern bleState_t state;
void MemBoard_init(void){
mem_UART_init();
master_spi_open();
central_mem_init();
central_reset();
flag_enable(EVT_MEM_UART_ROUTINE);
}
/**
* system event handle. It go through [EVENT_TABLE] and invoke event callback
* if event_mask bits set.
*/
void mem_event_handle() {
for (EventTableEntry *entry = EVENT_TABLE; EventMask && entry->event_mask; entry++) {
if (entry->event_callback && flag_mask(entry->event_mask)) {
entry->event_callback();
}
}
}
static void mem_central_event() {
if(flag_mask(EVT_MEM_NOTIFY_HANDLE)){
flag_disable(EVT_MEM_NOTIFY_HANDLE);
notify_handle_done = false;
central_handle_notify();
}
if (flag_mask(EVT_PIN_REQST)){
flag_disable(EVT_PIN_REQST);
// spi idle
if (notify_handle_done){
master_switch_memory();
}
// spi busy
else{
ram_sw = true;
}
}
if(flag_mask(EVT_MEM_UART_ROUTINE)){
flag_disable(EVT_MEM_UART_ROUTINE);
mem_uart_routine();
}
if(flag_mask(EVT_MEM_INS_SCAN)){
flag_disable(EVT_MEM_INS_SCAN);
SimpleBLECentral_discoverDevices();
}
if(flag_mask(EVT_MEM_INS_CONNECT)){
flag_disable(EVT_MEM_INS_CONNECT);
mem_connect_device(store_rxBuf[2], store_rxBuf+3);
}
if(flag_mask(EVT_MEM_INS_WRITE)){
flag_disable(EVT_MEM_INS_WRITE);
if(state == BLE_STATE_CONNECTED){
// selectedMenuItem should be MENU_ITEM_READ_WRITE
// This is done by SimpleBLECentral_processRoleEvent() in the case "GAP_LINK_ESTABLISHED_EVENT"
mem_send_ins(store_rxBuf[2], store_rxBuf+3);
}
}
if(flag_mask(EVT_MEM_INS_READ)){
flag_disable(EVT_MEM_INS_READ);
if(state == BLE_STATE_CONNECTED){
mem_central_get_cis(store_rxBuf[2]);
}
}
if(flag_mask(EVT_MEM_INS_DISCONNECT)){
flag_disable(EVT_MEM_INS_DISCONNECT);
GAPCentralRole_TerminateLink(connHandle);
state = BLE_STATE_DISCONNECTING;
}
if(flag_mask(EVT_MEM_RETURN_DATA)){
flag_disable(EVT_MEM_RETURN_DATA);
mem_read_data_and_return();
}
if(flag_mask(EVT_MEM_CONN_TIMEOUT)){
flag_disable(EVT_MEM_CONN_TIMEOUT);
mem_connection_timeout();
}
}
static EventTableEntry EVENT_TABLE[] = { //
{EVT_ALL, &mem_central_event},
// terminated
{0, NULL}
};
static void mem_connect_device(uint8_t addrType, uint8_t *peerAddr) {
uint8_t Addr[B_ADDR_LEN];
for(int i=0 ; i<B_ADDR_LEN ; i++){
Addr[5-i] = *(peerAddr+i);
}
// UART_write(uart_handle, Addr, 6);
state = BLE_STATE_CONNECTING;
Util_startClock(&connectingClock);
extern bStatus_t GAPCentralRole_EstablishLink(uint8_t highDutyCycle, uint8_t whiteList,
uint8_t addrTypePeer, uint8_t *peerAddr);
// TRUE to use high scan duty cycle when creating link
#define DEFAULT_LINK_HIGH_DUTY_CYCLE FALSE
// TRUE to use white list when creating link
#define DEFAULT_LINK_WHITE_LIST FALSE
GAPCentralRole_EstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,
addrType, Addr);
}
static void mem_send_ins(uint16_t handle, uint8_t *value){
if (state == BLE_STATE_CONNECTED &&
charHdl != 0 &&
procedureInProgress == FALSE)
{
uint8_t ACK[15] = {0}; //ack success
ACK[1] = handle;
ACK[2] = value[0];
ACK[3] = value[1];
UART_write(uart_handle, ACK, 4);
uint8_t status;
// Do a read or write as long as no other read or write is in progress
// Do a write
attWriteReq_t req;
// enable notify
req.handle = handle;
req.len = 19;
if(handle == BLE_CHAR3_HANDLE){
// RIS, VIS, CIS instruction
req.len = 19;
}
else if(handle == BLE_CHAR4_CONFIG_HANDLE){
// notify enable
req.len = 2;
}
else{
uint8_t error_handle[13] = "error handle";
UART_write(uart_handle, error_handle, 13);
status = bleMemAllocError;
return;
}
req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, req.len, NULL);
if ( req.pValue != NULL )
{
// send instruction
for(int i=0 ; i < req.len ; i++){
req.pValue[i] = *(value+i);
}
req.sig = 0;
req.cmd = 0;
status = GATT_WriteCharValue(connHandle, &req, selfEntity);
// uncomment GATT_WriteLongCharValue if we need a long instruction;
// However, there are some error to fix when using GATT_WriteLongCharValue
// status = GATT_WriteLongCharValue(connHandle, &req, selfEntity);
if ( status != SUCCESS )
{
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
}
}
else
{
uint8_t error_msg[10] = "err";
error_msg[3] = req.handle;
error_msg[4] = *value;
error_msg[5] = *(value+1);
error_msg[6] = *(value+2);
error_msg[7] = *(value+3);
error_msg[8] = *(value+4);
error_msg[9] = *(value+5);
UART_write(uart_handle, error_msg, 10);
status = bleMemAllocError;
}
if (status == SUCCESS)
{
procedureInProgress = TRUE;
}
}
}
static void mem_central_get_cis(uint8_t handle){
// get the actual CIS data from simple_central.c > SimpleBLECentral_processGATTMsg()
if (state == BLE_STATE_CONNECTED &&
charHdl != 0 &&
procedureInProgress == FALSE)
{
uint8_t status;
// send read command
attReadReq_t read_cis_req;
read_cis_req.handle = handle;
status = GATT_ReadCharValue(connHandle, &read_cis_req, selfEntity);
if (status == SUCCESS)
{
procedureInProgress = TRUE;
}
}
}
static void mem_read_data_and_return(){
// CIS_BUFF_SUFFIX = {0,0}
// add prefix and suffix to sync communication
uint16_t ret_length;
if (CIS_length < 22) {
ret_length = 20;
} else {
ret_length = CIS_length - 2;
}
CIS_data[0] = 4;
CIS_data[1] = 0;
CIS_data[2] = ret_length;
CIS_data[CIS_length++] = 0;
CIS_data[CIS_length++] = 0;
UART_write(uart_handle, CIS_data, ret_length + 3);
}
static void mem_connection_timeout(){
}
@@ -1,4 +1,7 @@
#ifndef MEM_BOARD_CENTRAL
#define MEM_BOARD_CENTRAL
/*
* Reference web page https://github.com/ti-simplelink/ble_examples/tree/ble_examples-2.2
* http://software-dl.ti.com/dsps/dsps_public_sw/sdo_sb/targetcontent/tirtos/2_20_00_06/
@@ -6,400 +9,9 @@
* doxygen/html/_u_a_r_t_c_c26_x_x_8h.html
*/
#ifndef MEM_BOARD_CENTRAL
#define MEM_BOARD_CENTRAL
#include <ti/sysbios/knl/Clock.h>
#include "mem_central_pin.h"
#include "mem_uart.h"
#include "mem_central_handle_notify.h"
#include "mem_uart_routine.h"
//#include "uart_printf.h"
//#include <xdc/runtime/System.h>
#define BLE_CDR_SAMLL_SIZE 10
#define BLE_CHAR2_HANDLE 0x0021 // read CIS return data
#define BLE_CHAR3_HANDLE 0x0024 // send instruction
#define BLE_CHAR4_CONFIG_HANDLE 0x0028 // notify enable: 0100=enable; 0000=disable
#define RET_INFO_BUF_SIZE 52
#define CIS_BUFF_PREFIX 3
#define CIS_BUFF_SUFFIX 2
static uint16_t CIS_length = 10;
static char CIS_data[50];
static void mem_central_event();
static void mem_recv_ins();
static void mem_connect_device(uint8_t addrType, uint8_t *peerAddr);
static void mem_send_ins(uint16_t hadnle, uint8_t *value);
static void mem_read_data_and_return();
static void mem_central_get_cis(uint8_t handle);
static void mem_connection_timeout();
static void MemBoard_init(void){
mem_UART_init();
master_spi_open();
central_mem_init();
central_reset();
flag_enable(EVT_MEM_UART_ROUTINE);
// uint8_t txBuf[] = "Simple Central memory board MA\n\r"; // Transmit buffer
// UART_txBuf = txBuf;
// UART_write(uart_handle, txBuf, sizeof(txBuf));
// mem_UART_display(txBuf);
}
static EventTableEntry EVENT_TABLE[] = { //
{EVT_ALL, &mem_central_event},
// terminated
{0, NULL}
};
/**
* system event handle. It go through [EVENT_TABLE] and invoke event callback
* if event_mask bits set.
*/
static void mem_event_handle() {
for (EventTableEntry *entry = EVENT_TABLE; EventMask && entry->event_mask; entry++) {
if (entry->event_callback && flag_mask(entry->event_mask)) {
entry->event_callback();
}
}
}
typedef enum{
LED_OFF,
LED_ON
} LED_Status;
typedef enum{
UART_NO_DATA,
UART_RECEIVED_DATA
} UART_DATA_Status;
static LED_Status GLEDStatus = LED_OFF;
static UART_DATA_Status UARTStatus = UART_NO_DATA;
static void mem_central_event() {
if(flag_mask(EVT_MEM_NOTIFY_HANDLE)){
flag_disable(EVT_MEM_NOTIFY_HANDLE);
notify_handle_done = false;
central_handle_notify();
}
if (flag_mask(EVT_PIN_REQST)){
flag_disable(EVT_PIN_REQST);
// spi idle
if (notify_handle_done){
master_switch_memory();
}
// spi busy
else{
ram_sw = true;
}
}
if(flag_mask(EVT_MEM_UART_ROUTINE)){
flag_disable(EVT_MEM_UART_ROUTINE);
mem_uart_routine();
}
// if (flag_mask(EVT_MEM_LED)) {
// flag_disable(EVT_MEM_LED);
// GLEDStatus = LED_ON;
//// SetMemOutputPIN(Board_GLED, 1);
// }
//
// if(flag_mask(EVT_MEM_LED_OFF)){
// flag_disable(EVT_MEM_LED_OFF);
// GLEDStatus = LED_OFF;
//// SetMemOutputPIN(Board_GLED, 0);
// }
if(flag_mask(EVT_MEM_INS_SCAN)){
flag_disable(EVT_MEM_INS_SCAN);
SimpleBLECentral_discoverDevices();
}
if(flag_mask(EVT_MEM_INS_CONNECT)){
flag_disable(EVT_MEM_INS_CONNECT);
mem_connect_device(store_rxBuf[2], store_rxBuf+3);
}
if(flag_mask(EVT_MEM_INS_WRITE)){
flag_disable(EVT_MEM_INS_WRITE);
if(state == BLE_STATE_CONNECTED){
// selectedMenuItem should be MENU_ITEM_READ_WRITE
// This is done by SimpleBLECentral_processRoleEvent() in the case "GAP_LINK_ESTABLISHED_EVENT"
mem_send_ins(store_rxBuf[2], store_rxBuf+3);
}
}
if(flag_mask(EVT_MEM_INS_READ)){
flag_disable(EVT_MEM_INS_READ);
if(state == BLE_STATE_CONNECTED){
mem_central_get_cis(store_rxBuf[2]);
}
}
if(flag_mask(EVT_MEM_INS_DISCONNECT)){
flag_disable(EVT_MEM_INS_DISCONNECT);
GAPCentralRole_TerminateLink(connHandle);
state = BLE_STATE_DISCONNECTING;
}
if(flag_mask(EVT_MEM_RETURN_DATA)){
flag_disable(EVT_MEM_RETURN_DATA);
mem_read_data_and_return();
}
if(flag_mask(EVT_MEM_CONN_TIMEOUT)){
flag_disable(EVT_MEM_CONN_TIMEOUT);
mem_connection_timeout();
}
}
static Control_Ins ins = INS_IDLE;
static void mem_recv_ins(){
extern uint8_t keysPressed;
extern Clock_Struct keyChangeClock;
static bool prepare_to_connect = false;
switch(UART_rxBuf[0]){
case INS_IDLE:{
break;
}
case INS_RESET:{
state = BLE_STATE_IDLE;
connHandle = GAP_CONNHANDLE_INIT;
discState = BLE_DISC_STATE_IDLE;
break;
}
case INS_KEY:{
if(UART_rxBuf[2] == 1){
keysPressed = KEY_LEFT;
Util_startClock(&keyChangeClock);
}
else if(UART_rxBuf[2] == 2){
keysPressed = KEY_RIGHT;
Util_startClock(&keyChangeClock);
}
break;
}
case INS_SCAN:{
SimpleBLECentral_discoverDevices();
break;
}
// instruction format:
// ins[0]: INS_SCAN_RESPONSE = 0x04
// ins[1]: scan_response (device number)=0, a certain device = device_id
// ins[2]: attr_length=0, e.g. len(addr)=6, len(company_code)=4, len(localName)=20
// addr=1, localName=2, company_code=3, version_info=4, battery_info=5
case INS_CONNECT:{
mem_connect_device(UART_rxBuf[2], UART_rxBuf+3);
break;
}
case INS_WRITE:{
if(state == BLE_STATE_CONNECTED){
// selectedMenuItem should be MENU_ITEM_READ_WRITE
// This is done by SimpleBLECentral_processRoleEvent() in the case "GAP_LINK_ESTABLISHED_EVENT"
mem_send_ins(UART_rxBuf[2], UART_rxBuf+3);
}
break;
}
case INS_READ:{
if(state == BLE_STATE_CONNECTED){
mem_central_get_cis(UART_rxBuf[2]);
}
break;
}
case INS_DISCONNECT:{
// clear scan result
GAPCentralRole_TerminateLink(connHandle);
state = BLE_STATE_DISCONNECTING;
// scanRes = 0;
// memset(devList, NULL, sizeof(devList[0])*DEFAULT_MAX_SCAN_RES);
//
// state = BLE_STATE_IDLE;
// connHandle = GAP_CONNHANDLE_INIT;
// discState = BLE_DISC_STATE_IDLE;
// charHdl = 0;
// procedureInProgress = FALSE;
// SimpleBLECentral_CancelRssi(pEvent->linkTerminate.connectionHandle);
// selectedMenuItem = MENU_ITEM_CONN_PARAM_UPDATE;
break;
}
default:{
uint8_t error_msg[35] = "error";
error_msg[6] = state;
error_msg[7] = 0xFF;
for(int i = 0 ; i < UART_BUFF_SIZE ; i++){
error_msg[8+i] = UART_rxBuf[i];
}
UART_write(uart_handle, error_msg, 35);
break;
}
}
}
static void mem_connect_device(uint8_t addrType, uint8_t *peerAddr) {
uint8_t Addr[B_ADDR_LEN];
for(int i=0 ; i<B_ADDR_LEN ; i++){
Addr[5-i] = *(peerAddr+i);
}
// UART_write(uart_handle, Addr, 6);
state = BLE_STATE_CONNECTING;
Util_startClock(&connectingClock);
GAPCentralRole_EstablishLink(DEFAULT_LINK_HIGH_DUTY_CYCLE,
DEFAULT_LINK_WHITE_LIST,
addrType, Addr);
}
static void mem_send_ins(uint16_t handle, uint8_t *value){
if (state == BLE_STATE_CONNECTED &&
charHdl != 0 &&
procedureInProgress == FALSE)
{
uint8_t ACK[15] = {0}; //ack success
ACK[1] = handle;
ACK[2] = value[0];
ACK[3] = value[1];
UART_write(uart_handle, ACK, 4);
uint8_t status;
// Do a read or write as long as no other read or write is in progress
// Do a write
attWriteReq_t req;
// enable notify
req.handle = handle;
req.len = 19;
if(handle == BLE_CHAR3_HANDLE){
// RIS, VIS, CIS instruction
req.len = 19;
}
else if(handle == BLE_CHAR4_CONFIG_HANDLE){
// notify enable
req.len = 2;
}
else{
uint8_t error_handle[13] = "error handle";
UART_write(uart_handle, error_handle, 13);
status = bleMemAllocError;
return;
}
req.pValue = GATT_bm_alloc(connHandle, ATT_WRITE_REQ, req.len, NULL);
if ( req.pValue != NULL )
{
// send instruction
for(int i=0 ; i < req.len ; i++){
req.pValue[i] = *(value+i);
}
req.sig = 0;
req.cmd = 0;
status = GATT_WriteCharValue(connHandle, &req, selfEntity);
// uncomment GATT_WriteLongCharValue if we need a long instruction;
// However, there are some error to fix when using GATT_WriteLongCharValue
// status = GATT_WriteLongCharValue(connHandle, &req, selfEntity);
if ( status != SUCCESS )
{
GATT_bm_free((gattMsg_t *)&req, ATT_WRITE_REQ);
}
}
else
{
uint8_t error_msg[10] = "err";
error_msg[3] = req.handle;
error_msg[4] = *value;
error_msg[5] = *(value+1);
error_msg[6] = *(value+2);
error_msg[7] = *(value+3);
error_msg[8] = *(value+4);
error_msg[9] = *(value+5);
UART_write(uart_handle, error_msg, 10);
status = bleMemAllocError;
}
if (status == SUCCESS)
{
procedureInProgress = TRUE;
}
}
}
static void mem_central_get_cis(uint8_t handle){
// get the actual CIS data from simple_central.c > SimpleBLECentral_processGATTMsg()
if (state == BLE_STATE_CONNECTED &&
charHdl != 0 &&
procedureInProgress == FALSE)
{
uint8_t status;
// send read command
attReadReq_t read_cis_req;
read_cis_req.handle = handle;
status = GATT_ReadCharValue(connHandle, &read_cis_req, selfEntity);
if (status == SUCCESS)
{
procedureInProgress = TRUE;
}
}
}
static void mem_read_data_and_return(){
// CIS_BUFF_SUFFIX = {0,0}
// add prefix and suffix to sync communication
uint16_t ret_length;
if (CIS_length < 22) {
ret_length = 20;
} else {
ret_length = CIS_length - 2;
}
CIS_data[0] = 4;
CIS_data[1] = 0;
CIS_data[2] = ret_length;
CIS_data[CIS_length++] = 0;
CIS_data[CIS_length++] = 0;
UART_write(uart_handle, CIS_data, ret_length + 3);
}
static void mem_connection_timeout(){
}
void MemBoard_init(void);
void mem_event_handle();
#endif
@@ -2,6 +2,8 @@
#ifndef MEM_EVENT_H
#define MEM_EVENT_H
extern void ext_call_sem();
/**
* test event [flag] has been enabled.
*/
@@ -35,7 +37,7 @@
uint8 __key = Hwi_disable(); \
EventMask |= (uint16_t)(flag); \
Hwi_restore(__key); \
Semaphore_post(sem); \
ext_call_sem(); \
} while (0)
/** event */
@@ -49,7 +51,7 @@
#define EVT_MEM_NOTIFY_HANDLE 0x0040
#define EVT_MEM_UART_ROUTINE 0x0080
static uint16_t EventMask = 0;
uint16_t EventMask = 0;
/**
* event table entry.
@@ -0,0 +1,121 @@
#include "mem_uart.h"
#include "mem_event.h"
#define IS_EVT_MEM_DECODE_INS(_b) ((_b)[0] == INS_SCAN || \
(_b)[0] == INS_CONNECT || \
(_b)[0] == INS_WRITE || \
(_b)[0] == INS_READ || \
(_b)[0] == INS_DISCONNECT)
#define IS_FINAL_RX_BYTE(_i, _l) (((_i) == (_l) + 1) && (store_rxBuf[(_l) + 1] == 0xF1))
static UART_Handle uart_handle;
static UART_Params params;
static uint8_t UART_txBuf[UART_BUFF_SIZE];
static uint8_t UART_rxBuf[UART_BUFF_SIZE];
static uint8_t store_rxBuf[UART_BUFF_SIZE];
static void mem_recv_uart();
// Callback function
static void readCallback(UART_Handle handle, void *rxBuf, size_t size)
{
// static uint8_t test_pin_state = 0;
// PIN_setOutputValue(hKeyPins, PIN_MEM_TEST, test_pin_state);
// test_pin_state = ~test_pin_state;
uint8_t *uart_rxBuf = (uint8_t *)rxBuf;
static int index = 0;
static int length = 0;
static bool rx_fi = false;
if (rx_fi) {
memset(store_rxBuf, 0, UART_BUFF_SIZE);
rx_fi = false;
}
if (IS_EVT_MEM_DECODE_INS(uart_rxBuf) && index == 0) {
store_rxBuf[0] = uart_rxBuf[0];
index++;
} else if (IS_EVT_MEM_DECODE_INS(store_rxBuf) && index == 1) {
store_rxBuf[1] = uart_rxBuf[0];
length = uart_rxBuf[0];
index++;
} else if (IS_EVT_MEM_DECODE_INS(store_rxBuf) && index > 1) {
store_rxBuf[index] = uart_rxBuf[0];
if (index >= length + 2) { //num = 0 when over length
store_rxBuf[index] = 0;
}
index++;
}
//0xF1 = 241
if(index > 1 && store_rxBuf[0] == INS_SCAN && store_rxBuf[length + 1] == 0xF1 ) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_SCAN);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_CONNECT && store_rxBuf[length + 1] == 0xF1) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_CONNECT);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_WRITE && store_rxBuf[length + 1] == 0xF1) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_WRITE);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_READ && store_rxBuf[length + 1] == 0xF1) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_READ);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_DISCONNECT && store_rxBuf[length + 1] == 0xF1) {
flag_notify(EVT_MEM_INS_DISCONNECT);
index = 0;
length = 0;
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
}
flag_notify(EVT_MEM_UART_ROUTINE);
}
void mem_UART_init(){
uint32_t timeoutUs = 5000; // 5ms timeout, default timeout is no timeout (BIOS_WAIT_FOREVER)
// Init UART and specify non-default parameters
UART_Params_init(&params);
params.baudRate = 115200;
params.writeDataMode = UART_DATA_BINARY;
params.readMode = UART_MODE_CALLBACK;
params.readDataMode = UART_DATA_BINARY;
params.readCallback = readCallback;
// params.readTimeout = timeoutUs / Clock_tickPeriod; // Default tick period is 10us
// params.readTimeout = ti_sysbios_BIOS_WAIT_FOREVER;
// Open the UART and do the read
uart_handle = UART_open(Board_UART, &params);
// int rxBytes = UART_read(uart_handle, rxBuf, 100);
return;
}
int mem_uart_read_byte(){
return UART_read(uart_handle, UART_rxBuf, 1)
}
void mem_get_uart_ins(uint8_t* ins){
ins = UART_rxBuf;
}
@@ -14,14 +14,6 @@
#define UART_BUFF_SIZE 25
static UART_Handle uart_handle;
static UART_Params params;
static uint8_t UART_txBuf[UART_BUFF_SIZE];
static uint8_t UART_rxBuf[UART_BUFF_SIZE];
static uint8_t store_rxBuf[UART_BUFF_SIZE];
typedef enum{
INS_IDLE,
INS_RESET,
@@ -52,212 +44,10 @@ typedef enum{
#define EVT_MEM_INS_DISCONNECT 0x1000
#define IS_EVT_MEM_DECODE_INS(_b) ((_b)[0] == INS_SCAN || \
(_b)[0] == INS_CONNECT || \
(_b)[0] == INS_WRITE || \
(_b)[0] == INS_READ || \
(_b)[0] == INS_DISCONNECT)
#define IS_FINAL_RX_BYTE(_i, _l) (((_i) == (_l) + 1) && (store_rxBuf[(_l) + 1] == 0xF1))
static void mem_recv_uart();
// Callback function
static void readCallback(UART_Handle handle, void *rxBuf, size_t size)
{
// static uint8_t test_pin_state = 0;
// PIN_setOutputValue(hKeyPins, PIN_MEM_TEST, test_pin_state);
// test_pin_state = ~test_pin_state;
uint8_t *uart_rxBuf = (uint8_t *)rxBuf;
static int index = 0;
static int length = 0;
static bool rx_fi = false;
if (rx_fi) {
memset(store_rxBuf, 0, UART_BUFF_SIZE);
rx_fi = false;
}
if (IS_EVT_MEM_DECODE_INS(uart_rxBuf) && index == 0) {
store_rxBuf[0] = uart_rxBuf[0];
index++;
} else if (IS_EVT_MEM_DECODE_INS(store_rxBuf) && index == 1) {
store_rxBuf[1] = uart_rxBuf[0];
length = uart_rxBuf[0];
index++;
} else if (IS_EVT_MEM_DECODE_INS(store_rxBuf) && index > 1) {
store_rxBuf[index] = uart_rxBuf[0];
if (index >= length + 2) { //num = 0 when over length
store_rxBuf[index] = 0;
}
index++;
}
//0xF1 = 241
if(index > 1 && store_rxBuf[0] == INS_SCAN && store_rxBuf[length + 1] == 0xF1 ) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_SCAN);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_CONNECT && store_rxBuf[length + 1] == 0xF1) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_CONNECT);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_WRITE && store_rxBuf[length + 1] == 0xF1) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_WRITE);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_READ && store_rxBuf[length + 1] == 0xF1) {
index = 0;
length = 0;
flag_notify(EVT_MEM_INS_READ);
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
} else if (index > 1 && store_rxBuf[0] == INS_DISCONNECT && store_rxBuf[length + 1] == 0xF1) {
flag_notify(EVT_MEM_INS_DISCONNECT);
index = 0;
length = 0;
flag_notify(EVT_MEM_UART_ROUTINE);
rx_fi = true;
return;
}
flag_notify(EVT_MEM_UART_ROUTINE);
}
static void mem_UART_init(){
uint32_t timeoutUs = 5000; // 5ms timeout, default timeout is no timeout (BIOS_WAIT_FOREVER)
// Init UART and specify non-default parameters
UART_Params_init(&params);
params.baudRate = 115200;
params.writeDataMode = UART_DATA_BINARY;
params.readMode = UART_MODE_CALLBACK;
params.readDataMode = UART_DATA_BINARY;
params.readCallback = readCallback;
// params.readTimeout = timeoutUs / Clock_tickPeriod; // Default tick period is 10us
// params.readTimeout = ti_sysbios_BIOS_WAIT_FOREVER;
// Open the UART and do the read
uart_handle = UART_open(Board_UART, &params);
// int rxBytes = UART_read(uart_handle, rxBuf, 100);
return;
}
static void mem_UART_display(char *str){
// calculate string size
uint8_t str_size;
for(str_size=0 ; *(str+str_size)!='\0' && str_size < 10; str_size++){
// do nothing
}
str_size ++;
char *out_str = malloc((str_size) * sizeof(char));
// char *out_str = str;
for(int i=0 ; i<str_size ; i++){
*(out_str+i) = *(str+i);
}
UART_write(uart_handle, out_str, str_size);
free(out_str);
out_str = NULL;
}
static void mem_UART_clear_screen(){
uint8_t cls[] = "\033[2J";
UART_write(uart_handle, cls, sizeof(cls));
}
static void mem_UART_display_u8(uint8_t *str, uint8_t len){
// calculate string size
uint8_t str_size;
// for(str_size=0 ; *(str+str_size)!='\0' && str_size < 400; str_size+=2){
// // do nothing
// }
str_size = 2*len + 1;
uint8_t *out_str = malloc((str_size) * sizeof(uint8_t));
for(int i=0 ; i<str_size/2 ; i++){
uint8_t MSB = (*(str+i) >> 4) & 0x0F;
uint8_t LSB = *(str+i) & 0x0F;
if(0 <= MSB && MSB <= 9){
*(out_str + 2*i) = MSB + '0';
}
else if(10 <= MSB && MSB <= 15){
*(out_str + 2*i) = MSB - 10 + 'A';
}
else{
*(out_str + 2*i) = 'x';
}
if(0 <= LSB && LSB <= 9){
*(out_str + 2*i+1) = LSB + '0';
}
else if(10 <= LSB && LSB <= 15){
*(out_str + 2*i+1) = LSB - 10 + 'A';
}
else{
*(out_str + 2*i+1) = 'x';
}
}
UART_write(uart_handle, out_str, str_size);
free(out_str);
out_str = NULL;
}
static void mem_UART_display_u16(uint16_t u16){
// calculate string size
uint8_t *out_str = malloc(4 * sizeof(uint8_t));
uint8_t u8[4];
for(int i=0 ; i<4 ; i++){
u8[3-i] = (u16 >> (4*i)) & 0x0F;
}
for(int i=0 ; i<4 ; i++){
if(0 <= u8[i] && u8[i] <= 9){
out_str[i] = u8[i] + '0';
}
else if(10 <= u8[i] && u8[i] <= 15){
out_str[i] = u8[i] - 10 + 'A';
}
else{
out_str[i] = 'x';
}
}
UART_write(uart_handle, out_str, 4);
free(out_str);
out_str = NULL;
}
static void mem_UART_newline(){
char out_str[2] = {'\n', '\r'};
UART_write(uart_handle, out_str, 2);
}
static void mem_recv_uart(){
UART_read(uart_handle, UART_rxBuf, 10);
}
static void mem_send_recv_uart(){
UART_write(uart_handle, UART_rxBuf, 10);
}
void mem_UART_init();
int mem_uart_read_byte();
void mem_get_uart_ins(uint8_t* ins);
#endif
@@ -0,0 +1,11 @@
#include "mem_uart_routine.h"
void mem_uart_routine(){
if (uartProcedureInProgress == FALSE) {
// uartProcedureInProgress = TRUE;
int rxBytes = mem_uart_read_byte();
} else {
flag_notify(EVT_MEM_UART_ROUTINE);
}
}
@@ -5,6 +5,8 @@
#include "mem_uart.h"
#include "mem_event.h"
void mem_uart_routine();
static void mem_uart_routine(){
if (uartProcedureInProgress == FALSE) {
// uartProcedureInProgress = TRUE;
@@ -350,7 +350,7 @@ static bool scanningStarted = FALSE;
static uint16_t connHandle = GAP_CONNHANDLE_INIT;
// Application state
static bleState_t state = BLE_STATE_IDLE;
bleState_t state = BLE_STATE_IDLE;
// Discovery state
static bleDiscState_t discState = BLE_DISC_STATE_IDLE;
@@ -390,8 +390,6 @@ static readRssi_t readRssi[MAX_NUM_BLE_CONNS];
static void SimpleBLECentral_init(void);
static void SimpleBLECentral_taskFxn(UArg a0, UArg a1);
static void MemBoard_init(void);
static void SimpleBLECentral_processGATTMsg(gattMsgEvent_t *pMsg);
static void SimpleBLECentral_handleKeys(uint8_t shift, uint8_t keys);
static void SimpleBLECentral_processStackMsg(ICall_Hdr *pMsg);