nkjnm
Dependencies: MAX44000 nexpaq_mdk
Fork of LED_Demo by
Revision 0:b86eda0e990d, committed 2016-09-17
- Comitter:
- nexpaq
- Date:
- Sat Sep 17 16:21:40 2016 +0000
- Child:
- 1:55a6170b404f
- Commit message:
- checking in to share
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,80 @@ +#include "mbed.h" +#include "nexpaq_mdk.h" + +DigitalOut ledR(P2_4, LED_OFF); +DigitalOut ledG(P2_5, LED_OFF); +DigitalOut ledB(P2_6, LED_OFF); +DigitalIn button(P0_1, PullUp); + +/***** Definitions *****/ +#define FUNCTION_TABLE_NUM 1 +#define UUID_NUM 16 //UUID number is 16, don't change it +#define BUTTON_DEBOUNCE 5 +#define DBG_MSG + +/***** Globals *****/ +void my_function_CMD_2700(unsigned char *pData, unsigned char len); +const MDK_REGISTER_CMD my_cmd_func_table[FUNCTION_TABLE_NUM] = { + {0x2700, my_function_CMD_2700}, // Command -> function +}; + +int btnCnt = 0; +int btnSts = 0; +unsigned char btnPress = 0x01; + +/***** Functions *****/ +void my_function_CMD_2700(unsigned char *pData, unsigned char len){ + unsigned char response = 0x00; + ledR = (pData[0]>0) ? LED_ON : LED_OFF ; + ledG = (pData[1]>0) ? LED_ON : LED_OFF ; + ledB = (pData[2]>0) ? LED_ON : LED_OFF ; + np_api_upload(0x2701, &response, 1); +#ifdef DBG_MSG + printf("LED Command Received\n\r"); +#endif +} + +/******************************************************************************/ +void app_setup(){ +// np_api_set_app_version(0, 0, 3); + if ( np_api_register((MDK_REGISTER_CMD*)my_cmd_func_table, FUNCTION_TABLE_NUM) == MDK_REGISTER_FAILD ) { + // Register failed handle code + error("MDK Register Failed"); + } +#ifdef DBG_MSG + printf("MDK Commands Registered\n\r"); +#endif +} + +void app_loop() { + if (btnSts) { + btnCnt = (button) ? 0 : (btnCnt + 1); + if (btnCnt >= BUTTON_DEBOUNCE) { + np_api_upload(0x2800, &btnPress, 1); + btnSts = !btnSts; + btnCnt = 0; +#ifdef DBG_MSG + printf("Button Pressed\n\r"); +#endif + } + } else { + btnCnt = (!button) ? 0 : (btnCnt + 1); + if (btnCnt >= BUTTON_DEBOUNCE) { + btnSts = !btnSts; + btnCnt = 0; + } + } +} + +int main(void){ + np_api_init(); + app_setup(); + np_api_start(); + while(1){ + app_loop(); + np_api_bsl_chk(); + Thread::wait(50); + } + return 0; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os_max32625.lib Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,1 @@ +https://github.com/maximmbed/mbed.git/#6f697745dd94a40c7767f279250a15fe4efe35d6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/nexpaq_mdk.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,112 @@ +/* + * np_module_mdk_v1.h + * developer call the API of MDK by the include file. + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_MODULE_MDK_V1_H_ +#define NP_MODULE_MDK_V1_H_ + +#include <stdint.h> + +#ifdef __cplusplus + extern "C" { +#endif + +typedef void (*my_VOID_UCUC)(uint8_t*, uint8_t); +typedef void (*app_function)(void); + +typedef struct { + uint16_t command; + my_VOID_UCUC function; +} MDK_REGISTER_CMD; + +#define MDK_REGISTER_SUCCESS 0x00 +#define MDK_REGISTER_FAILD 0x01 + +void delay_ms(uint32_t t_ms); +/* + * Description: API to make the app initial by the api + * Parameter: np_app_setup-the initial function of APP + * Return: null + */ +void np_api_setup(app_function np_app_setup); + +/* + * Description: initialize mdk + * Parameter: null + * Return: null + */ +void np_api_init(); + +/* + * Description: signal ready to start + * Parameter: null + * Return: null + */ +void np_api_start(); + +/* + * Description: check for bootloader request + * Parameter: null + * Return: null + */ +void np_api_bsl_chk(); + +/* + * Description: API to run the loop function of APP, "np_app_loop()" will run on while(1),running forever when software on app mode + * Parameter: np_app_loop-the loop function of APP + * Return: null + */ +void np_api_loop(app_function np_app_loop); + +/* + * Description: API to set app firmware version + * Parameter: null + * Return: null + */ +void np_api_set_app_version(uint8_t HV, uint8_t MV, uint8_t LV); +/* + * Description: API to register developer's command callback function + * Parameter : + * cmd_func_table:the callback function about the command. + * The callback function will be called when MDK get the corresponding command. + * num :The callback function number + * Return : 0-success; 1-fail + */ +uint8_t np_api_register(MDK_REGISTER_CMD* cmd_func_table, uint8_t num); + +/* + * Description: API to upload data to Phone + * Parameter: + * rcmd : The command of the message. + * pData: Pointer of the space for hold the parameter of the message. + * pLen : The length of the "pData" + * Return : 0-success; 1-fail + */ +uint8_t np_api_upload(uint16_t rcmd,uint8_t *pData, uint8_t pLen); + +/* + * Description: API to upload data to Gateway + * rcmd : The command of the message. + * pData: Pointer of the space for hold the parameter of the message. + * pLen : The length of the "pData" + * Return : 0-success; 1-fail + */ +uint8_t np_api_upload_to_station(uint16_t rcmd, uint8_t *pData, uint8_t pLen); + +/* + * Description: API to set a manually output address for next POST message + * Parameter: address-the source address of next message + * Return: null + */ +void np_api_set_post_address(uint8_t address); + +#ifdef __cplusplus + } +#endif + +#endif /* NP_MODULE_MDK_V1_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_apis.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,207 @@ +/* + * np_apis.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_apis.h" +#include "np_app_ncn_interface.h" +#include "np_app_spi.h" +#include "np_system.h" + +MDK_REGISTER_CMD *mdk_cmd_func_table_ptr; +uint8_t mdk_cmd_func_number; + +/* + * Description: APP to set firmware version of APP by the api + * Parameter: null + * Return: null + */ +void np_api_set_app_version(uint8_t HV, uint8_t MV, uint8_t LV) { + mdk_app_version[0] = HV; + mdk_app_version[1] = MV; + mdk_app_version[2] = LV; +} + +/* + * Description: APP to register processing function of command by the api + * Parameter: + * cmd_func_table: pointer of the space for hold the pointer of processing functions. + * num : the number of the processing function. + * + * Return: 0-success; 1-fail + */ +uint8_t np_api_register(MDK_REGISTER_CMD* cmd_func_table, uint8_t num) { + uint8_t t_i = 0; + uint8_t t_num = num; + + for ( ; t_i < t_num; t_i++ ) { + if ( (cmd_func_table[t_i].command > 0x27ff) || (cmd_func_table[t_i].command < 0x2700) ) { + return 1; + } + } + mdk_cmd_func_table_ptr = cmd_func_table; + mdk_cmd_func_number = num; + + return 0; +} + +/* + * Description: APP to upload message to Phone/ble by the api + * Parameter : + * rcmd : The command of the message. + * pData: Pointer of the space for hold the parameter of the message. + * pLen : The length of the "pData" + * + * Return : 0-success; 1-fail + */ +uint8_t np_api_upload(uint16_t rcmd, uint8_t *pData, uint8_t pLen) { + // Over the fifo buffer + if ( spi_post_buf.used_buffer_length + pLen > FIFO_BUFFER_OVERLOAD ) + return 1; + np_function_ncn_interface_post_message(rcmd, pData, pLen); + + return 0; +} + +/* + * Description: APP to upload data to Gateway by the api + * Parameter : + * rcmd : The command of the message. + * pData: Pointer of the space for hold the parameter of the message. + * pLen : The length of the "pData" + * + * Return : 0-success; 1-fail + + */ +uint8_t np_api_upload_to_station(uint16_t rcmd, uint8_t *pData, uint8_t pLen) { + // Over the fifo buffer + if ( spi_post_buf.used_buffer_length + pLen > FIFO_BUFFER_OVERLOAD ) + return 1; + np_function_ncn_interface_post_message_to_station(rcmd, pData, pLen); + + return 0; +} + +/* + * Description:APP set a manually output address for next POST message + * Parameter: null + * Return: null + */ +void np_api_set_post_address(uint8_t address) { + source_address = address; +} + +/* + * Description: system initial ,include MDK initial and app initial + * Parameter: null + * Return: null + */ +void np_api_setup(app_function np_app_setup){ + np_system_initial(np_app_setup); +} + +/* + * Description: system loop function, will loop forever. + * Parameter: null + * Return: null + */ +void np_api_loop(app_function np_app_loop){ + np_system_loop(np_app_loop); +} + +/* + * Description: initialize + * Parameter: null + * Return: null + */ +void np_api_init(){ + np_sys_init(); +} + +/* + * Description: signal ready + * Parameter: null + * Return: null + */ +void np_api_start(){ + np_sys_start(); +} + +/* + * Description: bootloader check + * Parameter: null + * Return: null + */ +void np_api_bsl_chk(){ + np_sys_bsl_chk(); +} + +#if 0 +/* + * Description: API to set auto enter lpm0 mode + * Parameter: null + * Return: null + */ +void np_api_lpm0_automode_set(void) { + np_function_lpm0_automode_set(); +} + +/* + * Description: API to set auto exit lpm0 mode + * Parameter: null + * Return: null + */ +void np_api_lpm0_automode_clear(void) { + np_function_lpm0_automode_clear(); +} + + +uint8_t np_api_lpm_mode_get(void){ + return np_function_pm_mode_get(); +} + +/* + * Description: API to get the sleep status about sleeping on lpm0 or lpm4 + * Parameter: null + * Return: LPM0/LPM4/LPM_NONE + */ +uint8_t np_api_lpm_status_get(void){ + return np_function_get_lpm_status(); +} + +void np_mdk_set_run_the_loop(uint8_t t_data){ + np_function_set_run_the_loop(t_data); +} + +void np_mdk_start_loop_head_set(void){ + wakeup_run_loop_flag = 1; +} + +void np_mdk_lpm4_automode_clear_flag(void){ + np_function_lpm4_automode_clear(); +} + +void np_api_lpm4_automode_set(void) { + if(np_function_pm_mode_get() & LPM_0){ + np_mdk_set_run_the_loop(TRUE);//if developer set lpm4 on interrupt and firmware is running on lpm0,need wakup lpm0 and enter lpm4 + } + + np_function_lpm4_automode_set(); + developer_lpm4_setting = 1; +} + +void np_api_lpm4_automode_clear(void) { + np_mdk_lpm4_automode_clear_flag(); + np_mdk_start_loop_head_set(); + developer_lpm4_setting = 0; +} + +void np_mdk_exit_lpm4_work(void){ + np_function_exit_lpm4_work(); +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_apis.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,30 @@ +/* + * np_apis.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_APIS_H_ +#define NP_APIS_H_ + +#include <stdint.h> +#ifdef __cplusplus +extern "C" { +#endif + +typedef void (*my_VOID_UCUC)(uint8_t*, uint8_t); +typedef struct { + uint16_t command; + my_VOID_UCUC function; +} MDK_REGISTER_CMD; + +extern MDK_REGISTER_CMD *mdk_cmd_func_table_ptr; +extern uint8_t mdk_cmd_func_number; +#ifdef __cplusplus +} +#endif + +#endif /* NP_APIS_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_crc.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,77 @@ +/* + * np_app_crc.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_config.h" + +// CRC16 table +const uint16_t crc_table[256]={ + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, + 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, + 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, + 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, + 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, + 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, + 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, + 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, + 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, + 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, + 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, + 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, + 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, + 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, + 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, + 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, + 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, + 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, + 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, + 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, + 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, + 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, + 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 +}; + +/* + * Description: to calculate the CRC16 for a serial number + * Parameter : pData - pointer of the serial number, len - length of the serial number + * Return : the result of CRC16 calculation + */ +uint16_t np_function_crc_calculate(uint8_t* pData, uint16_t len) { + uint16_t crc16 = 0x0000; + + while ( len > 0 ) { + crc16 = (crc16<<8) ^ crc_table[((crc16>>8)^*pData)&0xff]; + len--; + pData++; + } + + return crc16; +} + +/* + * Description: to check if the giving CRC16 is correct + * Parameter : pData - pointer of the serial number, len - lenght of the serial number, + * crc_value - the giving CRC16 + * Return : 0:correct, 1:wrong + */ +uint8_t np_function_crc_check(uint8_t* pData, uint16_t len, uint16_t crc_value) { + if ( crc_value == np_function_crc_calculate(pData, len) ) + return 0; + else + return 1; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_crc.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,24 @@ +/* + * np_app_crc.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_APP_CRC_H_ +#define NP_APP_CRC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +uint16_t np_function_crc_calculate(uint8_t* pData, uint16_t len); +uint8_t np_function_crc_check(uint8_t* pData, uint16_t len, uint16_t crc_value); + +#ifdef __cplusplus +} +#endif + +#endif /* NP_APP_CRC_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_ncn_interface.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,352 @@ +/* + * np_app_ncn_interface.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_config.h" +#include "np_app_ncn_interface.h" +#include "np_app_spi.h" +#include "np_app_crc.h" +#include "np_command.h" +#include "np_apis.h" +#include "np_app_pm.h" +#include "np_driver_pm.h" +#include "np_system.h" + +uint8_t mdk_app_version[3] = {0x00}; //the code of developer version +uint8_t mdk_mdk_version[3] = {0x00}; //the mdk version +uint8_t t_message[64]; // buffer for message cache +uint8_t source_address; // the message is coming from this port address +volatile uint8_t flag_jump_bsl; //uint8_t flag_jump_bsl = 0x00; +uint8_t my_node = 0x00; // To record "this" module's node number + +/* + * Description:MDK set a manually output address for next POST message + * Parameter: null + * Return: null + */ +void np_function_set_post_address(uint8_t address) { + source_address = address; +} + +/* + * Description: get one message from the FIFO buffer + * Parameter : pData - pointer of the space for hold the one message + * Return : the length of the message + */ +uint8_t np_function_ncn_interface_get_message(uint8_t *pData) { + uint8_t t_i = 0x00; + + // Clean the usage + spi_get_buf.used_buffer_length -= spi_get_buf.message_len[spi_get_buf.readout_message_pointer]; + // Case If: + // One message over 79 to another 0 to 79 loop + // Case else: + // One message from n to m, but n and m is one same 0 to 79 loop + if ( (spi_get_buf.readout_buffer_pointer + spi_get_buf.message_len[spi_get_buf.readout_message_pointer]) > FIFO_BUFFER_OVERLOAD ) { + // Read until pointer reach 79 + while (spi_get_buf.readout_buffer_pointer <= FIFO_BUFFER_OVERLOAD ) { + pData[t_i++] = spi_get_buf.buffer[spi_get_buf.readout_buffer_pointer++]; + spi_get_buf.message_len[spi_get_buf.readout_message_pointer]--; + } + // Pointer reset to 0 + spi_get_buf.readout_buffer_pointer = 0; + // Read out the rest + while ( spi_get_buf.message_len[spi_get_buf.readout_message_pointer] ) { + pData[t_i++] = spi_get_buf.buffer[spi_get_buf.readout_buffer_pointer++]; + spi_get_buf.message_len[spi_get_buf.readout_message_pointer]--; + } + } else { + while ( spi_get_buf.message_len[spi_get_buf.readout_message_pointer] ) { + pData[t_i++] = spi_get_buf.buffer[spi_get_buf.readout_buffer_pointer++]; + spi_get_buf.message_len[spi_get_buf.readout_message_pointer]--; + } + } + + spi_get_buf.readout_message_pointer++; + if ( spi_get_buf.readout_message_pointer > FIFO_MESSAGE_OVERLOAD) + spi_get_buf.readout_message_pointer = 0; + return t_i; +} + +/* + * Description: add one message into the FIFO buffer + * Parameter: + * command :The command of this message; + * pData :pointer of the new parameters inside the message, + * pLen :the length of the parameters + * Return: null + */ +uint8_t np_function_ncn_interface_post_message(uint16_t command, uint8_t *pData, uint8_t pLen) { + uint8_t t_i = 0x00; + uint16_t t_crc16 = 0x0000; + uint8_t message_len = 0x00; + + message_len = pLen+11; + if ( (spi_post_buf.used_buffer_length+message_len) > FIFO_BUFFER_OVERLOAD ) { + // Over buffer, ERROR! + return 1; + } + // Assembling a message for sending out + t_message[0] = 0x7E; + t_message[1] = my_node; + t_message[2] = source_address; + t_message[3] = pLen+3; + t_message[4] = command>>8; + t_message[5] = command; + t_message[6] = pLen; + for ( t_i = 0; t_i < pLen; t_i++ ) + t_message[7+t_i] = pData[t_i]; + t_crc16 = np_function_crc_calculate(&t_message[1], pLen+6); + t_message[7+pLen] = t_crc16>>8; + t_message[8+pLen] = t_crc16; + t_message[9+pLen] = 0x7E; + t_message[10+pLen] = '$'; //keep first TX byte of next communication is '$' .Meaningless on here + + // Fill in the information into the POST FIFO structure + spi_post_buf.used_buffer_length += message_len; + spi_post_buf.message_len[spi_post_buf.fillin_message_pointer++] = message_len; + if ( spi_post_buf.fillin_message_pointer > FIFO_MESSAGE_OVERLOAD ) { + spi_post_buf.fillin_message_pointer = 0; + } + t_i = 0; + // Caes if: + // The reset of the space need to put into the next buffer loop + // Case else: + // The reset of the space enough in the same buffer loop + if ( (spi_post_buf.fillin_buffer_pointer+message_len) > FIFO_BUFFER_OVERLOAD ) { + while ( spi_post_buf.fillin_buffer_pointer <= FIFO_BUFFER_OVERLOAD ) { + spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; + message_len--; + } + spi_post_buf.fillin_buffer_pointer = 0; + while ( message_len ) { + spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; + message_len--; + } + } else { + while ( message_len ) { + spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; + message_len--; + } + } + + spi_post_buf.message_num++; + + return 0; +} + +/* + * Description: add one message into the FIFO buffer, but information especially to Gateway + * Parameter: + * command :The command of this message; + * pData :pointer of the new parameters inside the message, + * pLen :the length of the parameters + * Return: null + */ +uint8_t np_function_ncn_interface_post_message_to_station(uint16_t command, uint8_t *pData, uint8_t pLen) { + uint8_t t_i = 0x00; + uint16_t t_crc16 = 0x0000; + uint8_t message_len = 0x00; + + message_len = pLen+11; + if ( (spi_post_buf.used_buffer_length+message_len) > FIFO_BUFFER_OVERLOAD ) { + // Over buffer, ERROR! + return 1; + } + // Assembling a message for sending out + t_message[0] = 0x7E; + t_message[1] = my_node; + t_message[2] = 0x01; + t_message[3] = pLen+3; + t_message[4] = command>>8; + t_message[5] = command; + t_message[6] = pLen; + for ( t_i = 0; t_i < pLen; t_i++ ) + t_message[7+t_i] = pData[t_i]; + t_crc16 = np_function_crc_calculate(&t_message[1], pLen+6); + t_message[7+pLen] = t_crc16>>8; + t_message[8+pLen] = t_crc16; + t_message[9+pLen] = 0x7E; + t_message[10+pLen] = '$'; //keep first TX byte of next communication is '$' .Meaningless on here + + // Fill in the information into the POST FIFO structure + spi_post_buf.used_buffer_length += message_len; + spi_post_buf.message_len[spi_post_buf.fillin_message_pointer++] = message_len; + if ( spi_post_buf.fillin_message_pointer > FIFO_MESSAGE_OVERLOAD ) { + spi_post_buf.fillin_message_pointer = 0; + } + t_i = 0; + // Caes if: + // The reset of the space need to put into the next buffer loop + // Case else: + // The reset of the space enough in the same buffer loop + if ( (spi_post_buf.fillin_buffer_pointer+message_len) > FIFO_BUFFER_OVERLOAD ) { + while ( spi_post_buf.fillin_buffer_pointer <= FIFO_BUFFER_OVERLOAD ) { + spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; + message_len--; + } + spi_post_buf.fillin_buffer_pointer = 0; + while ( message_len ) { + spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; + message_len--; + } + } else { + while ( message_len ) { + spi_post_buf.buffer[spi_post_buf.fillin_buffer_pointer++] = t_message[t_i++]; + message_len--; + } + } + + spi_post_buf.message_num++; + + + return 0; +} + + +/* + * Description: unpack the message and process it, here is the core of the message(s), + * received -> understanding -> process + * Parameter: null + * Return: null + */ +void np_function_ncn_interface_parse(void) { + uint16_t t_crc_value = 0x0000; + uint16_t command = 0x0000; + + np_function_ncn_interface_get_message(t_message); + + // CRC16 Check + t_crc_value = t_message[4+t_message[3]]; + t_crc_value = (t_crc_value<<8)|t_message[5+t_message[3]]; + if ( np_function_crc_check(&t_message[1], t_message[3]+3, t_crc_value) ) { + // CRC16 NOT pass + return; + } + + // If this message is for me? + if ( t_message[2] != my_node ) { + // NOT for me + return; + } + + np_function_set_post_address(t_message[1]); + + // Only one command inside payload, if more than one, this logic need to change + command = t_message[4]; + command = (command<<8)|t_message[5]; + + // Developer command code from 0x2700-0x27ff + if ( (command >= 0x2700)&&(command <= 0x27ff) ) { + uint8_t t_i = 0; + uint8_t t_we_have_response = FALSE; + // Search and run the right function + for ( ; t_i < mdk_cmd_func_number; t_i++ ) { + if ( command == mdk_cmd_func_table_ptr[t_i].command ) { + mdk_cmd_func_table_ptr[t_i].function(&t_message[7], t_message[6]); + t_we_have_response = TRUE; + } + } + // If the command is not existing, then we need to response a ERROR + if ( t_we_have_response == FALSE ) { + uint8_t data[2]; + data[0] = command >> 8; + data[0] = command & 0xFF; + np_function_ncn_interface_post_message(ERROR_NO_CMD, data, 0x02); + } + } else { + // NP define command code + switch (command) { + case ASK_APP_INFO:{ + np_function_ncn_interface_post_message(ANS_APP_INFO, + mdk_app_version, 0x03); + } + break; + + case REQ_INTO_BSL:{ + RAM_SOURCE_ADDR = source_address; + flag_jump_bsl = 0x01; + } + break; + + case CMD_MDK_REQ_START: { + uint8_t data = SUCCESS; +// np_api_start(); + np_function_ncn_interface_post_message(CMD_MDK_RSP_START, &data, + 0x01); + break; + } + case CMD_MDK_REQ_STOP: { + uint8_t data = SUCCESS; +// np_api_stop(); + np_function_ncn_interface_post_message(CMD_MDK_RSP_STOP, &data, + 0x01); + break; + } + + case CMD_MDK_REQ_LPM4:{ + uint8_t data = SUCCESS; +// if(t_message[7] == 1){ +// np_function_lpm4_automode_set(); +// if(np_function_pm_mode_get() & LPM_0){ +// np_function_set_run_the_loop(TRUE); +// } +// }else if(t_message[7] == 0){ +// if(developer_lpm4_setting == 0){ +// np_function_lpm4_automode_clear(); +// } +// } + np_function_ncn_interface_post_message(CMD_MDK_RSP_LPM4, &data,0x01); + } + break; + + case ASK_STATUS: { + uint8_t t_data[1] = { 0x01 }; + np_function_ncn_interface_post_message(ANS_STATUS, t_data, 0x01); + break; + } + + case REQ_TEST_COMM:{ + #if CASE_TEST_CODE_ENABLE + #else + //MDK version + np_function_ncn_interface_post_message(RSP_TEST_COMM,mdk_mdk_version, 0x03); + #endif + } + break; + + default: { + uint8_t data[2]; + data[0] = command >> 8; + data[1] = command; + np_function_ncn_interface_post_message(ERROR_NO_CMD, data, 0x02); + break; + } + } + } +} + +/* + * Description: the message handler + * Parameter: null + * Return: null + */ +void np_function_ncn_interface_loop(void) { + if ( spi_get_buf.message_num ) { + np_function_ncn_interface_parse(); + spi_get_buf.message_num--; + } + if ( spi_status == SPI_BUSY ) + return; + if ( spi_post_buf.message_num ) { + if ( np_app_spi_is_aviable() ) { + np_app_spi_apply_post(); + } + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_ncn_interface.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,41 @@ +/* + * np_app_ncn_interface.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_APP_NCN_INTERFACE_H_ +#define NP_APP_NCN_INTERFACE_H_ + +#include <stdint.h> +#ifdef __cplusplus +extern "C" { +#endif + +typedef enum RESULT_WORD { + SUCCESS = 0x00, + ERROR = 0x01 +}result_Word; + +extern uint8_t mdk_app_version[3] ; +extern uint8_t mdk_mdk_version[3]; +extern uint8_t source_address; +extern volatile uint8_t flag_jump_bsl; +extern uint8_t my_node; + +void np_function_ncn_interface_loop(void); +void np_function_set_post_address(uint8_t address); +uint8_t np_function_ncn_interface_get_message(uint8_t *pData); +uint8_t np_function_ncn_interface_post_message(uint16_t command, uint8_t *pData, uint8_t pLen); +uint8_t np_function_ncn_interface_post_message_to_station(uint16_t command, uint8_t *pData, uint8_t pLen); +//extern void np_api_start(); +//extern void np_api_stop(); + +#ifdef __cplusplus +} +#endif + +#endif /* NP_APP_NCN_INTERFACE_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_pm.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,147 @@ +/* + * np_app_pm.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include <stdint.h> + +uint8_t run_the_loop = 0; +uint8_t flag_on_lpm = 0; +uint8_t developer_lpm4_setting = 0; //record the developer setting about the lpm4 +uint8_t wakeup_run_loop_flag = 0; +#if 0 +extern void np_api_enter_lpm4_work();// for developer +extern void np_api_exit_lpm4_work(); // for developer + +void np_function_set_run_the_loop(uint8_t t_data){ + run_the_loop = t_data; +} + +uint8_t np_function_get_run_the_loop(void){ + return run_the_loop; +} + +/* + * Description: set auto enter lpm0 mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_function_lpm0_automode_set(void) { + np_driver_lpm0_automode_set(); +} + +/* + * Description: exit auto exit lpm0 mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_function_lpm0_automode_clear(void) { + np_driver_lpm0_automode_clear(); +} + +void np_function_enter_lpm4_work(void){ + np_api_enter_lpm4_work(); + np_driver_spi_function_close(); + np_driver_enable_pin13_io_interrupt(); +} + +void np_function_exit_lpm4_work(void){ + np_driver_spi_function_open(); + np_api_exit_lpm4_work(); + //np_driver_disalbe_pin13_io_interrupt(); +} + +uint8_t flag_lpm4_enter = 0; +/* + * Description: run the power save mode loop + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_function_pm_loop(void) { + if(wakeup_run_loop_flag){ + return; + } + + if((np_function_pm_mode_get() & LPM_4)){ + //v1_0_3--1 please reference the version.txt + if((spi_status != SPI_BUSY)&&(np_app_spi_is_aviable()) + &&(spi_post_buf.message_num == 0)&&(spi_get_buf.message_num == 0)){ + + flag_on_lpm = LPM_4; + np_function_enter_lpm4_work(); + if(wakeup_run_loop_flag){ + np_function_exit_lpm4_work(); + }else{//bug: if io interrupt wakeup on here, the module will no way to process. + __bis_SR_register(LPM4_bits); + } + + flag_on_lpm = LPM_NONE; + } + }else if (np_function_pm_mode_get() & LPM_0){ + flag_on_lpm = LPM_0; + __bis_SR_register(LPM0_bits); + flag_on_lpm = LPM_NONE; + } +} + +/* + * Description: set auto enter lpm0 mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_function_lpm4_automode_set(void) { + np_driver_lpm4_automode_set(); +} + +/* + * Description: exit auto exit lpm0 mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_function_lpm4_automode_clear(void) { + np_driver_lpm4_automode_clear(); +} + + +/* + * Description: get the mode of auto lpm mode (lpm0/lpm4) + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +uint8_t np_function_pm_mode_get(void) { + return np_driver_lpm_mode_get(); +} + +uint8_t np_function_get_lpm_status(void){ + return flag_on_lpm; +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_pm.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,151 @@ +/* + * np_app_pm.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_APP_PM_H_ +#define NP_APP_PM_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +extern uint8_t wakeup_run_loop_flag; +extern uint8_t flag_on_lpm; +extern uint8_t developer_lpm4_setting; + +void np_function_set_run_the_loop(uint8_t t_data); +uint8_t np_function_get_run_the_loop(void); +void np_function_lpm0_automode_set(void); +void np_function_lpm0_automode_clear(void); +uint8_t np_function_pm_mode_get(void); +void np_function_pm_loop(void); +void np_function_lpm4_automode_set(void) ; +void np_function_lpm4_automode_clear(void) ; +void np_function_enter_lpm4_work(void); +void np_function_exit_lpm4_work(void); +uint8_t np_function_get_lpm_status(void); + +#if 0 +void np_api_start(); //unused +void np_api_stop(); //unused + +//The low power consumption of np_module_mdk_v1.h +//low power consumption function. Function temporarily don't open. +typedef enum{ + LPM_NONE = 0x00, + LPM_0 = 0x01, + LPM_1 = 0x02, //unused + LPM_2 = 0x04, //unused + LPM_3 = 0x08, //unused + LPM_4 = 0x10 +}E_LPM; + +void np_api_enter_lpm4_work(); +void np_api_exit_lpm4_work(); + +/* + * Description: set auto power lpm0 mode + * Parameter: null + * Return: null + */ +void np_api_lpm0_automode_set(void); +/* + * Description: clear auto power lpm0 mode + * Parameter: null + * Return: null + */ +void np_api_lpm0_automode_clear(void); + + +/* + * Description: set auto power lpm4 mode + * Parameter: null + * Return: null + */ +void np_api_lpm4_automode_set(void) ; + +/* + * Description: clear auto power lpm4 mode. only clear the lpm4 enable flag. + It is effective when software is not on lpm4. + You can use "np_api_lpm4_automode_clear_and_wakeup() " to clear the lpm4 mode when wakeup lpm4 by io interrupt. + * Parameter: null + * Return: null + */ +void np_api_lpm4_automode_clear(void); + +/* + * Description: clear auto power lpm4 mode and exit lpm4 sleep status. + The api is used at io interrupt for wakeup lpm4 and clear lpm4 mode. + The api only available within interrupt routines. + * Parameter: null + * Return: null + */ +#define np_api_lpm4_automode_clear_and_wakeup() do{\ + if (np_api_lpm_status_get() == LPM_4){\ + np_mdk_exit_lpm4_work();\ + __bic_SR_register_on_exit(LPM4_bits);\ + }\ + np_mdk_lpm4_automode_clear_flag();\ + np_mdk_start_loop_head_set();\ + }while(0) + +/* + * Description: API to run the loop once by wake up lpm0/lpm4 once.After the loop once, software will enter lpm again. + f The api only available within interrupt routines. + If you want to exit lpm4, please using the api to wakeup lpm4 when io interrupt is comming. + The api also can wakeup lpm0. + * Parameter: null + * Return: null + */ +#define np_api_wake_up_lpm_once() do{\ + if (np_api_lpm_status_get() == LPM_4){\ + np_mdk_exit_lpm4_work();\ + __bic_SR_register_on_exit(LPM4_bits);\ + }else if(np_api_lpm_mode_get() & LPM_0){np_mdk_set_run_the_loop(1);}\ + np_mdk_start_loop_head_set();\ + }while(0) + +/* +* Description: run the loop once by wake up lpm0 once.After the loop once, software will enter lpm again. +* Parameter: null +* Return: null +*/ +#define np_api_wake_up_lpm0_once() do{\ + if(np_api_lpm_mode_get() & LPM_0){np_mdk_set_run_the_loop(1);}\ + }while(0) + + +/* + * Description: get power status(), now software sleep at LPM_X; + * Parameter: null + * Return: E_LPM + */ +uint8_t np_api_lpm_status_get(void); + +/* + * Description: get the power mode, please reference "E_LPM"; + * Parameter: null + * Return: pm mode.Such as: + if only set the lpm0/lpm4 mode, return "lpm0"/"lpm4". + if set the lpm0 and lpm4, return "lpm0 | lpm4". + */ +uint8_t np_api_lpm_mode_get(void); + +/******************The following API functions are not used by developer. please not used it*******************/ +void np_mdk_lpm4_automode_clear_flag(void); +void np_mdk_exit_lpm4_work(void); +void np_mdk_set_run_the_loop(uint8_t t_data); +void np_mdk_start_loop_head_set(void); +/*********************************************************************************************************/ +#endif + +#ifdef __cplusplus +} +#endif + +#endif /* NP_APP_PM_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_spi.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,157 @@ +/* + * np_app_spi.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_driver_spi.h" +#include "np_app_spi.h" +#include "mxc_sys.h" + +typedef enum +{ + SPI_IS_READ = 0x00, + SPI_IS_WRITE = 0x80 +}E_SPIReadWrite; + +uint8_t spi_count = 0x00; // The counter inside the SPI transfer process +uint8_t spi_status = SPI_FREE; // SPI_FREE ( we can use the SPI to transfer message), SPI_BUSY (we can NOT use the SPI to transfer message) +uint8_t spi_rw = 0x00; // To record this SPI transfer is a POST or GET process +// The fifo buffer structure design for POST/GET message(s) +FIFO_GET spi_get_buf = { {0x00}, {0x00}, 0x00, 0x00, 0x00, 0x00, 0x00 }; +FIFO_POST spi_post_buf = { {0x00}, {0x00}, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; + +const gpio_cfg_t gpio_req_spi[] = { + { PORT_4, PIN_3, GPIO_FUNC_GPIO, GPIO_PAD_INPUT_PULLUP}, /* Input*/ + { PORT_4, PIN_3, GPIO_FUNC_GPIO, GPIO_PAD_NORMAL}, /* Output*/ + }; + +/* + * Description: to get the message from spi reading data + * Parameter : spi_read-the data received by spi slave + * Return : null + */ +void np_app_spis_message (uint8_t spi_read) { + + if ( (spi_status == SPI_FREE)&&(spi_read == '$') ) { //Check the first byte is '$'(0x24). + spi_status = SPI_BUSY; + spi_count = 0x00; + np_driver_spis_put_byte_to_tx_buff(0); + } else if ( spi_status == SPI_BUSY ) { + if ( spi_count == 0x00 ) { + // Get this byte to know if Gateway want to POST/SET message + spi_rw = spi_read; + if ( spi_rw == SPI_IS_WRITE ) { + // Send the high Byte of message length, but the message for module will least then 64 bytes. + np_driver_spis_put_byte_to_tx_buff(0); + } else if ( spi_rw == SPI_IS_READ ) { + np_driver_spis_put_byte_to_tx_buff(0); + } else { + np_driver_spis_put_byte_to_tx_buff(0); + spi_status = SPI_FREE; + } + } else if ( spi_count == 0x01 ) { + if ( spi_rw == SPI_IS_WRITE ) { + if ( spi_post_buf.message_num ) { + // Send the low Byte of message length + np_driver_spis_put_byte_to_tx_buff(spi_post_buf.message_len[spi_post_buf.post_message_pointer]-1); + } else { + np_driver_spis_put_byte_to_tx_buff(0); + spi_status = SPI_FREE; + } + } else if ( spi_rw == SPI_IS_READ ) { + np_driver_spis_put_byte_to_tx_buff(0); + // Received the high Byte of message length, but the message for module will least then 64 bytes. + // So we will skip here + } + } else if ( spi_count == 0x02 ) { + if ( spi_rw == SPI_IS_WRITE ) { + // Send the first byte here + np_driver_spis_put_byte_to_tx_buff(spi_post_buf.buffer[spi_post_buf.post_buffer_pointer++]); + // Do NOT over the buffer + if ( spi_post_buf.post_buffer_pointer > FIFO_BUFFER_OVERLOAD ) + spi_post_buf.post_buffer_pointer = 0; + } else if ( spi_rw == SPI_IS_READ ) { + np_driver_spis_put_byte_to_tx_buff(0); + // Received the low Byte of message length + spi_get_buf.message_len[spi_get_buf.get_message_pointer] = spi_read; + } + } else { + if ( spi_rw == SPI_IS_WRITE ) { + // Send the first byte here + np_driver_spis_put_byte_to_tx_buff(spi_post_buf.buffer[spi_post_buf.post_buffer_pointer++]); + // Do NOT over the buffer + if ( spi_post_buf.post_buffer_pointer > FIFO_BUFFER_OVERLOAD ) + spi_post_buf.post_buffer_pointer = 0; + + if ( spi_post_buf.message_len[spi_post_buf.post_message_pointer]+1 <= spi_count ) { + // If we do NOT sent more bytes in 1ms, means we finished the sending + spi_status = SPI_FREE; + + spi_post_buf.used_buffer_length -= spi_post_buf.message_len[spi_post_buf.post_message_pointer]; + spi_post_buf.post_message_pointer++; + // Do NOT over the buffer + if ( spi_post_buf.post_message_pointer > FIFO_MESSAGE_OVERLOAD ) + spi_post_buf.post_message_pointer = 0; + spi_post_buf.message_num--; + } + } else if ( spi_rw == SPI_IS_READ ) { + // Start to received the message body + spi_get_buf.buffer[spi_get_buf.get_buffer_pointer++] = spi_read; + // Do NOT over the buffer + if ( spi_get_buf.get_buffer_pointer > FIFO_BUFFER_OVERLOAD ) + spi_get_buf.get_buffer_pointer = 0; + + if ( spi_get_buf.message_len[spi_get_buf.get_message_pointer]+2 <= spi_count ) { + // If we do NOT received more bytes in 1ms, means we finished the last receiving + // Then free the SPI and add one more message + spi_status = SPI_FREE; + + spi_get_buf.used_buffer_length += spi_get_buf.message_len[spi_get_buf.get_message_pointer]; + spi_get_buf.get_message_pointer++; + // Do NOT over the buffer + if ( spi_get_buf.get_message_pointer > FIFO_MESSAGE_OVERLOAD ) + spi_get_buf.get_message_pointer = 0; + spi_get_buf.message_num++; + np_driver_spis_put_byte_to_tx_buff('$'); //keep first TX byte of next communication is '$'.Meaningless on here + }else{ + np_driver_spis_put_byte_to_tx_buff(0); + } + } + } + spi_count++; + } +} + + +/* + * Description: check the CS pin if it got selected, if yes, we could not use it, SPI bus is busy + * Parameter : null + * Return : 0x01-BUSY , 0x00-FREE + */ +uint32_t np_app_spi_is_aviable(void) { + #if 0//mxc32625 high level available + return GPIO_InGet(&gpio_req_spi[0]); + #else + return (!GPIO_InGet(&gpio_req_spi[0])); + #endif +} + +/* + * Description: give a pulse on the SPI bus, signal to Gateway,spi slave want to have a spi comunication. + * Parameter : null + * Return : null + */ +void np_app_spi_apply_post(void) { + + GPIO_Config(&gpio_req_spi[1]);//Output + + GPIO_OutSet(&gpio_req_spi[1]); + GPIO_OutClr(&gpio_req_spi[1]); + + GPIO_Config(&gpio_req_spi[0]);//Input +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_app_spi.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,58 @@ +/* + * np_app_spi.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_APP_SPI_H_ +#define NP_APP_SPI_H_ + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define FIFO_SPI_DATA_LENGTH 80 +#define FIFO_BUFFER_OVERLOAD FIFO_SPI_DATA_LENGTH-1 +#define FIFO_MESSAGE_LENGTH 8 +#define FIFO_MESSAGE_OVERLOAD FIFO_MESSAGE_LENGTH-1 +#define SPI_BUSY 0x01 +#define SPI_FREE 0x00 +typedef struct { + uint8_t buffer[FIFO_SPI_DATA_LENGTH]; + uint8_t message_len[FIFO_MESSAGE_LENGTH]; + uint8_t get_buffer_pointer; + uint8_t readout_buffer_pointer; + uint8_t get_message_pointer; + uint8_t readout_message_pointer; + uint8_t message_num; + uint8_t used_buffer_length; +} FIFO_GET; +typedef struct { + uint8_t buffer[FIFO_SPI_DATA_LENGTH]; + uint8_t message_len[FIFO_MESSAGE_LENGTH]; + uint8_t fillin_buffer_pointer; + uint8_t post_buffer_pointer; + uint8_t fillin_message_pointer; + uint8_t post_message_pointer; + uint8_t message_num; + uint8_t used_buffer_length; +} FIFO_POST; + +extern FIFO_GET spi_get_buf; +extern FIFO_POST spi_post_buf; +extern uint8_t spi_status; + +void np_app_spis_message (uint8_t spi_read); +void np_app_spi_apply_post(void); +uint32_t np_app_spi_is_aviable(void); + +#ifdef __cplusplus +} +#endif + +#endif /* NP_APP_SPI_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_command.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,94 @@ +/* + * np_comand.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_COMAND_H_ +#define NP_COMAND_H_ + +typedef enum COMMAND_WORD { + //All the Lowest bit of the request command is 0, + //and Lowest bit of it's response command is 1, + //If the Lowest bit of the command is 1,means that it doesn't need a response command. + REQ_INTO_APP = 0x0800, //0x07D3 + RSP_INTO_APP = 0x0801, + REQ_EARSE_FLASH = 0x0802, //0x07D4 + RSP_EARSE_FLASH = 0x0803, + REQ_WRITE_FLASH = 0x0804, //0x07D5 + RSP_WRITE_FLASH = 0x0805, + REQ_INTO_BSL = 0x0806, //0x07D6 + RSP_INTO_BSL = 0x0807, + ASK_UUID = 0x0808, //0x07E7 + ANS_UUID = 0x0809, //0x07EA + ASK_BSL_INFO = 0x080A, //0x07E8 + ANS_BSL_INFO = 0x080B, //0x07EB + ASK_APP_INFO = 0x080C, //0x07E9 + ANS_APP_INFO = 0x080D, //0x07EC + ASK_STATUS = 0x080E, //0x07C0 + ANS_STATUS = 0x080F, //0x07C1 + REQ_EARSE_APP_AND_INFO = 0x0810, + RSP_EARSE_APP_AND_INFO = 0x0811, + + CMD_MDK_REQ_START = 0x0812, + CMD_MDK_RSP_START = 0x0813, + CMD_MDK_REQ_STOP = 0x0814, + CMD_MDK_RSP_STOP = 0x0815, + CMD_MDK_REQ_LPM4 = 0x0816, + CMD_MDK_RSP_LPM4 = 0X0817, + + ASK_MODULE_NODE = 0x0900, //0x07D1 + ANS_MODULE_NODE = 0x0901, + ASK_MODULE_LAST_CTR = 0x0902, //0x07C6 + ANS_MODULE_LAST_CTR = 0x0903, //0x07C7 + REQ_KILL_MODULE = 0x0904, //0x0802 + RSP_KILL_MODULE = 0x0905, + + REQ_ALLOT_NODE = 0x0B00, //0x07D0 + RSP_ALLOT_NODE = 0x0B01, + ASK_MODULE_NUM = 0x0B02, //0x07EE + ANS_MODULE_NUM = 0x0B03, //0x07EF + REQ_REGISTER_USB = 0x0B04, + RSP_REGISTER_USB = 0x0B05, + REQ_CANCEL_USB = 0x0B06, + RSP_CANCEL_USB = 0x0B07, + REQ_UPDATE_PH_POWER = 0x0B08, + RSP_UPDATE_PH_POWER = 0x0B09, + + CTR_BLE_SWITCH = 0x0A00, //0x07D8 + STATUS_BLE_SWITCH = 0x0A02, //0x07D9 + CASE_APPLY_INTO_APP = 0x0A04, //0x07E3 + + REQ_USB_SW = 0x1F00, + RSP_USB_SW = 0x1F01, + /*Data message DOWNLOAD*/ + REQ_DOWNLOAD_GROUP = 0x2700, + REQ_DOWNLOAD = 0x2700, //0x0801 + RSP_DOWNLOAD = 0x2701, + REQ_DOWNLOAD_02 = 0x2702, //0x0811 + RSP_DOWNLOAD_02 = 0x2703, + REQ_DOWNLOAD_04 = 0x2704, //0x0812 + RSP_DOWNLOAD_04 = 0x2705, + REQ_DOWNLOAD_06 = 0x2706, + RSP_DOWNLOAD_06 = 0x2707, + /*Data message UPLOAD*/ + REQ_UPLOAD = 0x2800, //0x0901 + // RSP_UPLOAD = 0x1281, + BAT_POWER_MSG = 0x4600, //0x0901(Now just for battery message) - CASE + REQ_BAT_CTR = 0x4602, //0x0801(Now just for control battery) - CASE + RSP_BAT_CTR = 0x4603, + + /*ERROR cmd*/ + ERROR_NO_CMD = 0x0480, //Answer if can not parse the command. + + REQ_TEST_COMM = 0x5100, // 1.get mdk version ; 2.for Pc_software using ,testing communication and reback data. default 1. + RSP_TEST_COMM = 0x5101, + REQ_SYSTEM_SWITCH = 0x1F1D, //only for test + RSP_SYSTEM_SWITCH = 0x1F1E + +} COMMAND_words; + +#endif /* NP_COMMAND_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_config.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,34 @@ +/* + * np_config.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_CONFIG_H_ +#define NP_CONFIG_H_ +//np_config.h can't be used on head file + +#include <stdio.h> +#include <stdint.h> +#include "mxc_config.h" +#include "clkman.h" +#include "ioman.h" +#include <string.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define RAM_SOURCE_ADDR *(uint32_t*)(0x20000000) +#define RAM_MY_NODE *(uint32_t*)(0x20000004) +// *(uint32_t*)(0x20000008) //the address which save the bootloader address for jumping + + +#ifdef __cplusplus +} +#endif + +#endif /* NP_CONFIG_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_driver_pm.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,102 @@ +/* + * np_driver_pm.c + * + * Created on: 2016Äê7ÔÂ14ÈÕ + * Author: Alan.Lin + */ + +#if 0 +uint8_t mdk_auto_lpm_flag = 0; + +/* + * Description: enable the global interrupt + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_driver_pm_gie_set(void) { + __bis_SR_register(GIE); +} + +/* + * Description: disable the global interrupt + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_driver_pm_gie_clear(void) { + __bic_SR_register(GIE); +} + +/* + * Description: entry auto power save mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_driver_lpm0_automode_set(void) { + mdk_auto_lpm_flag |= LPM_0; +} + +/* + * Description: exit auto power save mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +void np_driver_lpm0_automode_clear(void) { + mdk_auto_lpm_flag &= ~LPM_0; +} + +/* + * Description: enter auto power lpm4 mode + * Parameter: null + * Return: null + * + * Author: Alan.Lin + * + * Copyright: NexPack.Ltd + */ +void np_driver_lpm4_automode_set(void){ + mdk_auto_lpm_flag |= LPM_4; +} +/* + * Description: exit auto power lpm4 mode + * Parameter: null + * Return: null + * + * Author: Alan.Lin + * + * Copyright: NexPack.Ltd + */ + +void np_driver_lpm4_automode_clear(void){ + mdk_auto_lpm_flag &= ~LPM_4; +} + +/* + * Description: get the status of auto power save mode + * Parameter: null + * Return: null + * + * Author: Coody.Liu + * + * Copyright: NexPack.Ltd + */ +uint8_t np_driver_lpm_mode_get(void) { + return mdk_auto_lpm_flag; +} + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_driver_pm.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,39 @@ +/* + * np_driver_pm.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_DRIVER_PM_H_ +#define NP_DRIVER_PM_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + +typedef enum{ + LPM_NONE = 0x00, + LPM_0 = 0x01, + LPM_1 = 0x02, + LPM_2 = 0x04, + LPM_3 = 0x08, + LPM_4 = 0x10 +}E_LPM; + +void np_driver_lpm0_automode_set(void); +void np_driver_lpm0_automode_clear(void); +uint8_t np_driver_lpm_mode_get(void); +void np_driver_pm_gie_set(void); +void np_driver_pm_gie_clear(void); +void np_driver_lpm4_automode_set(void); +void np_driver_lpm4_automode_clear(void); + +#ifdef __cplusplus +} +#endif + +#endif /* NP_DRIVER_PM_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_driver_spi.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,56 @@ +/* + * np_driver_spi.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_config.h" +#include "spis.h" +#include "np_driver_spi.h" +#include "np_app_spi.h" + +int driver_spi_slave_initial(void){ + uint8_t spis_req_value = 0x10; + //IO INITIAL + MXC_IOMAN->spis_req = spis_req_value; + //IO initial IS OK + if(spis_req_value != MXC_IOMAN->spis_ack){ + return E_BUSY; + } + CLKMAN_SetClkScale((clkman_clk_t)(CLKMAN_CLK_SPIS), CLKMAN_SCALE_DIV_1); + + //spis function intial + MXC_SPIS->gen_ctrl = 0; + MXC_SPIS->gen_ctrl = (MXC_F_SPIS_GEN_CTRL_SPI_SLAVE_EN | MXC_F_SPIS_GEN_CTRL_TX_FIFO_EN | + MXC_F_SPIS_GEN_CTRL_RX_FIFO_EN |(0x01 << MXC_F_SPIS_GEN_CTRL_SPI_MODE_POS)); + + MXC_SPIS_FIFO->tx_8[0] = '$'; //keep first TX byte of next communication is '$'.Meaningless on here + + MXC_SPIS->fifo_ctrl &= (~MXC_F_SPIS_FIFO_CTRL_RX_FIFO_AF_LVL); + MXC_SPIS->fifo_ctrl |= (0x0 << MXC_F_SPIS_FIFO_CTRL_RX_FIFO_AF_LVL_POS); + + MXC_SPIS->inten = MXC_F_SPIS_INTEN_RX_FIFO_AF;// | MXC_F_SPIS_INTEN_TX_FIFO_AE; + NVIC_EnableIRQ(SPIS_IRQn); + + return E_NO_ERROR; +} + +void np_driver_spis_put_byte_to_tx_buff(uint8_t t_tx_byte){ + MXC_SPIS_FIFO->tx_8[0] = t_tx_byte; +} + +//------------------------interrupt function--------------------------------- +void SPIS_IRQHandler(void){ + uint32_t spis_int_flags = MXC_SPIS->intfl; + + if (spis_int_flags & MXC_F_SPIS_INTFL_RX_FIFO_AF) { + // Check for data + while(MXC_SPIS->fifo_stat & MXC_F_SPIS_FIFO_STAT_RX_FIFO_USED) { + np_app_spis_message(MXC_SPIS_FIFO->rx_8[0]); + } + MXC_SPIS->intfl = MXC_F_SPIS_INTFL_RX_FIFO_AF; + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_driver_spi.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,28 @@ +/* + * np_driver_spi.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_DRIVER_SPI_H_ +#define NP_DRIVER_SPI_H_ + +#include <stdint.h> + +#ifdef __cplusplus +extern "C" { +#endif + + +int driver_spi_slave_initial(void); +void np_driver_spis_put_byte_to_tx_buff(uint8_t t_tx_byte); + +#ifdef __cplusplus +} +#endif + + +#endif /* NP_DRIVER_SPI_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_driver_timer.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,46 @@ +/* + * np_driver_timer.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_config.h" +#include "tmr_regs.h" +#include "mxc_sys.h" +#include "np_driver_timer.h" +#include "np_app_ncn_interface.h" + +void TMR0_IRQHandler(void){ + MXC_TMR0->intfl = MXC_TMR0->intfl; + + np_function_ncn_interface_loop(); + +// if ( !(np_function_pm_mode_get() & LPM_0)) { +// __bic_SR_register_on_exit(LPM0_bits); +// wakeup_run_loop_flag = 1; +// } else if ( np_function_get_run_the_loop()) { +// __bic_SR_register_on_exit(LPM0_bits); +// wakeup_run_loop_flag = 1; +// np_function_set_run_the_loop(FALSE); +// } +} + + +//32 bit timer0 initial, 1 MS trigger interrupt, the lowest priority of interrupt +void Timer0_initial(void){ + MXC_TMR0->ctrl &= ~MXC_F_TMR_CTRL_ENABLE0; + + MXC_TMR0->ctrl &= ~MXC_F_TMR_CTRL_TMR2X16; + MXC_TMR0->ctrl |= (0x01 << MXC_F_TMR_CTRL_MODE_POS)|(12 << MXC_F_TMR_CTRL_PRESCALE_POS); + MXC_TMR0->count32 = 24; + MXC_TMR0->term_cnt32 = 24; + MXC_TMR0->inten |= MXC_F_TMR_INTEN_TIMER0; + + MXC_TMR0->ctrl |= MXC_F_TMR_CTRL_ENABLE0; + + NVIC_SetPriority(TMR0_0_IRQn,15); + NVIC_EnableIRQ(TMR0_0_IRQn); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_driver_timer.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,24 @@ +/* + * np_driver_timer.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_DRIVER_TIMER_H_ +#define NP_DRIVER_TIMER_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + +void Timer0_initial(void); + +#ifdef __cplusplus +} +#endif + +#endif /* NP_DRIVER_TIMER_H_ */
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_system.c Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,200 @@ +/* + * np_system.c + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#include "np_config.h" +#include "np_driver_timer.h" +#include "np_driver_spi.h" +#include "np_app_spi.h" +#include "np_app_ncn_interface.h" +#include "spis.h" +#include "np_system.h" +#include "np_command.h" + +extern const gpio_cfg_t gpio_req_spi[]; + +/* Description: delay function + * t_ms: the time for delay.MS as the unit + * + */ +void delay_ms(uint32_t t_ms) +{ + while(t_ms) { + SYS_SysTick_Delay(96000); + t_ms--; + } +} + +/* + * Description: disable all interrupt used on MDK + * Parameter : null + * Return : null + */ +void np_system_disable_interrupt(void) +{ + NVIC_DisableIRQ(TMR0_0_IRQn); + NVIC_DisableIRQ(SPIS_IRQn); +} + +/* + * Description: Setting the version of MDK + * Parameter: mdk version + * Return: null + */ +void np_function_set_mdk_version(uint8_t HV, uint8_t MV, uint8_t LV) +{ + mdk_mdk_version[0] = HV; + mdk_mdk_version[1] = MV; + mdk_mdk_version[2] = LV; +} + +/* + * Description: MDK initial + * Parameter : null + * Return : null + */ +void np_mdk_setup(void) +{ + int t_reback = E_BUSY; + + GPIO_Config(&gpio_req_spi[0]); //Input + //ignore clock initial + while(t_reback != E_NO_ERROR) { + t_reback = driver_spi_slave_initial(); + delay_ms(10); + } + + Timer0_initial(); + np_function_set_post_address(RAM_SOURCE_ADDR); + + spi_status = SPI_FREE; + my_node = RAM_MY_NODE; +} + +/* + * Description: system initial,include app initial and mdk initial + * Parameter : null + * Return : null + */ +void np_system_initial(app_function np_app_setup) +{ + uint8_t answer = 0; + + np_function_set_mdk_version(1,0,7); + np_mdk_setup(); + np_app_setup(); + np_function_ncn_interface_post_message(RSP_INTO_APP, &answer, 1); + flag_jump_bsl = 0; +} + +/* + * Description: mdk initialization + * Parameter : null + * Return : null + */ +void np_sys_init() +{ + np_function_set_mdk_version(1,0,7); + np_mdk_setup(); +#ifdef DBG_MSG + printf("NP System Initialized\n\r"); +#endif +} + +/* + * Description: signal when system is ready + * Parameter : null + * Return : null + */ +void np_sys_start() +{ + uint8_t answer = 0; + np_function_ncn_interface_post_message(RSP_INTO_APP, &answer, 1); + flag_jump_bsl = 0; +#ifdef DBG_MSG + printf("NP System Started\n\r"); +#endif +} + +/* + * Description: function to check and jump to boot loader + * Parameter : null + * Return : null + */ +#ifdef TOOLCHAIN_ARM_STD +register int _r0 __asm("r0"); +register int _pc __asm("pc"); +#endif + +void np_sys_bsl_chk() +{ + if( flag_jump_bsl > 0x00 ) { + flag_jump_bsl = 0x00; + np_system_disable_interrupt(); +#ifdef DBG_MSG + printf("Entering Bootloader\n\r"); +#endif +#ifdef TOOLCHAIN_ARM_STD + __asm { + mov _r0, #0x20000008 + ldr _pc, [_r0] + } + } + __asm { + mov _r0, _r0 + mov _r0, _r0 + } +#else + __asm volatile( + " LDR R0, =0x20000008 \n" + " LDR PC, [R0] \n" + ); + } + __asm volatile("nop \n"); + __asm volatile("nop \n"); +#endif +} + +/* + * Description: system loop,software will run the function forever when software is on app mode. + * Parameter : null + * Return : null + */ +void np_system_loop(app_function np_api_loop) +{ + while(1) { + np_api_loop(); + //jumping to the bootloader + if( flag_jump_bsl > 0x00 ) { + flag_jump_bsl = 0x00; + np_system_disable_interrupt(); +#ifdef DBG_MSG + printf("Entering NP Bootloader\n\r"); +#endif +#ifdef TOOLCHAIN_ARM_STD + __asm { + mov _r0, #0x20000008 + ldr _pc, [_r0] + } + } + __asm { + mov _r0, _r0 + mov _r0, _r0 + } +#else + __asm volatile( + " LDR R0, =0x20000008 \n" + " LDR PC, [R0] \n" + ); + } + __asm volatile("nop \n"); + __asm volatile("nop \n"); +#endif + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/nexpaq_mdk_src/source/np_system.h Sat Sep 17 16:21:40 2016 +0000 @@ -0,0 +1,31 @@ +/* + * np_system.h + * + * Created on: July 14, 2016 + * Author: Alan.Lin + * + * Copyright: NexPack Ltd. + */ + +#ifndef NP_SYSTEM_H_ +#define NP_SYSTEM_H_ + +#ifdef __cplusplus +extern "C" { +#endif +#include <stdint.h> + +typedef void (*app_function)(void); +void np_system_disable_interrupt(void); +void np_system_disable_interrupt(void); +void np_system_initial(app_function np_app_setup); +void np_system_loop(app_function np_api_loop); +void np_sys_init(); +void np_sys_start(); +void np_sys_bsl_chk(); +void delay_ms(uint32_t t_ms); +#ifdef __cplusplus +} +#endif + +#endif /* NP_SYSTEM_H_ */