123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771 |
- #include "main_task.h"
- #include "uart.h"
- #include "gpio.h"
- #include "cfg.h"
- #include "IAP.h"
- #include "protocol.h"
- #include "process.h"
- #include "crc16.h"
- #include <string.h>
- typedef struct _mb_ctx
- {
- uint8_t* rxbuf;
- uint8_t* txbuf;
- uint16_t rx_len; //recv data length
- uint16_t tx_len; //will be transfer data length
- uint16_t txbuf_size;
- uint8_t exception_code;
- uint8_t func_code; // function code
- uint16_t cmd; // register
- }MB_CTX;
- static MB_CTX g_mb_ctx;
- void mb_03_handle(void)
- {
-
- uint16_t tx_offset = 0;
- uint16_t crc;
- uint16_t ret_len = 0;
- uint16_t reg_num = ((uint16_t)g_mb_ctx.rxbuf[4]<<8)|g_mb_ctx.rxbuf[5];
- uint16_t read_len = reg_num<<1; // wanted bytes
- //
- g_mb_ctx.txbuf[0] = g_mb_ctx.rxbuf[0]; // slave addr
- g_mb_ctx.txbuf[1] = g_mb_ctx.rxbuf[1]; // func code
- g_mb_ctx.txbuf[2] = 0x00;
-
- tx_offset = 3;
-
- switch(g_mb_ctx.cmd){
- case Cmd_FirmwareVersion:
- if(16 == read_len){
- tx_offset += Read_FirmwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(18 == read_len){
- tx_offset += Read_FirmwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_HardwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(22 == read_len){
- tx_offset += Read_FirmwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_HardwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Deviceid(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(24 == read_len){
- tx_offset += Read_FirmwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_HardwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Deviceid(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Devicetype(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else {
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_HardwareVersion:
- if(2 == read_len){
- tx_offset += Read_HardwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(6 == read_len){
- tx_offset += Read_HardwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Deviceid(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(8 == read_len){
- tx_offset += Read_HardwareVersion(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Deviceid(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Devicetype(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_DeviceID:
- if(4 == read_len){
- tx_offset += Read_Deviceid(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(6 == read_len){
- tx_offset += Read_Deviceid(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Devicetype(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_DeviceType:
- if(2 == read_len){
- tx_offset += Read_Devicetype(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Addr:
- if(2 == read_len){
- tx_offset += Read_Addr(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else if(4 == read_len){
- tx_offset += Read_Addr(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- tx_offset += Read_Baudrate(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else {
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Baudrate:
- if(2 == read_len){
- tx_offset += Read_Baudrate(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_LockStatus:
- if(2 == read_len){
- tx_offset += Read_LockStatus(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_ReadThreshold:
- if(8 == read_len){
- tx_offset += Read_Threshold(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_DateTime:
- if(8 == read_len){
- tx_offset += Read_DateTime(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_ReadAngle:
- if(4 == read_len){
- tx_offset += Read_Angle(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_ReadRecordNum:
- if(2 == read_len){
- tx_offset += Read_RecordsNum(g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_ReadRecords:
- if((read_len>0)&(0 == read_len%SIZE_RECORD)&(read_len/SIZE_RECORD <= MAX_RECORDS_PERTIME)){
- tx_offset += Read_Records(read_len/SIZE_RECORD,g_mb_ctx.txbuf+tx_offset, g_mb_ctx.txbuf_size-tx_offset);
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- default:
- g_mb_ctx.exception_code = INVALID_COMMAND;
- break;
- };
-
- if(NO_EXCEPTION == g_mb_ctx.exception_code){
- g_mb_ctx.txbuf[2] = (uint8_t)read_len;
- g_mb_ctx.tx_len = tx_offset;
-
- }else{
- g_mb_ctx.txbuf[1] |= 0x80; // func code
- g_mb_ctx.txbuf[2] = g_mb_ctx.exception_code;
- g_mb_ctx.tx_len = 3;
- }
-
- crc = crc16(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)(crc & 0x00ff);
-
- rs485_TransmitData(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
-
- }
- void mb_06_handle(void)
- {
- uint16_t crc;
- uint8_t ret_write = RET_OK;
- memcpy(g_mb_ctx.txbuf, g_mb_ctx.rxbuf, g_mb_ctx.rx_len);
-
- switch(g_mb_ctx.cmd){
- case Cmd_HardwareVersion:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_HardwareVersion(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_DeviceType:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_Devicetype(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Addr:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_Addr(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Baudrate:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_Baudrate(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Reboot:
- //NVIC_SystemReset();
- ret_write = RET_NEED_REBOOT;
- break;
- case Cmd_Reset:
- ResetConfig();
- ret_write = RET_NEED_SAVE|RET_NEED_REBOOT;
- //NVIC_SystemReset();
- break;
- case Cmd_LockOp:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_LockOp(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Threshold:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_ThresholdCalibration(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_RecordDelete:
- if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_RecordsDelete(g_mb_ctx.rxbuf+4, 2);
- if(RET_DATAINVALID == ret_write)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- default:
- g_mb_ctx.exception_code = INVALID_COMMAND;
- break;
- };
-
- if(NO_EXCEPTION == g_mb_ctx.exception_code){
- g_mb_ctx.tx_len = g_mb_ctx.rx_len;
-
- }else{
- g_mb_ctx.txbuf[1] |= 0x80; // func code
- g_mb_ctx.txbuf[2] = g_mb_ctx.exception_code;
- g_mb_ctx.tx_len = 3;
-
- crc = crc16(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)(crc & 0x00ff);
- }
-
- rs485_TransmitData(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
-
- if((ret_write&RET_NEED_SAVE) > 0 ){
- SaveConfig();
- }
-
- if((ret_write&RET_NEED_REBOOT) > 0 ){
- mdelay(200);
- NVIC_SystemReset();
- }
-
- }
- void mb_10_handle(void)
- {
- uint16_t crc;
- uint8_t ret_write = RET_OK;
- uint16_t reg_num = ((uint16_t)g_mb_ctx.rxbuf[4]<<8)|g_mb_ctx.rxbuf[5];
- uint16_t data_len = g_mb_ctx.rxbuf[6];
- memcpy(g_mb_ctx.txbuf, g_mb_ctx.rxbuf, g_mb_ctx.rx_len);
-
- // 先判段数据长度是否一致
- if(data_len != reg_num*2){
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- if(data_len != g_mb_ctx.rx_len-7-2){
- g_mb_ctx.exception_code = INVALID_DATA;
- }
-
- }
-
- if(NO_EXCEPTION == g_mb_ctx.exception_code){
-
- switch(g_mb_ctx.cmd){
- case Cmd_HardwareVersion:
- if(8 == data_len){
- ret_write |= Write_HardwareVersion(g_mb_ctx.rxbuf+7, 2);
- ret_write |= Write_Deviceid(g_mb_ctx.rxbuf+7+2, 4);
- ret_write |= Write_Devicetype(g_mb_ctx.rxbuf+7+6, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else if(6 == data_len){
- ret_write |= Write_HardwareVersion(g_mb_ctx.rxbuf+7, 2);
- ret_write |= Write_Deviceid(g_mb_ctx.rxbuf+7+2, 4);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else if(2 == g_mb_ctx.rx_len-4-2){
- ret_write = Write_HardwareVersion(g_mb_ctx.rxbuf+7, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
-
- case Cmd_DeviceID:
- if(6 == data_len){
- ret_write |= Write_Deviceid(g_mb_ctx.rxbuf+7, 4);
- ret_write |= Write_Devicetype(g_mb_ctx.rxbuf+7+4, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else if(4 == data_len){
- ret_write |= Write_Deviceid(g_mb_ctx.rxbuf+7, 4);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_DeviceType:
- if(2 == data_len){
- ret_write |= Write_Devicetype(g_mb_ctx.rxbuf+7, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Addr:
- if(4 == data_len){
- ret_write |= Write_Addr(g_mb_ctx.rxbuf+7, 2);
- ret_write |= Write_Addr(g_mb_ctx.rxbuf+7+2, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else if(2 == data_len){
- ret_write |= Write_Addr(g_mb_ctx.rxbuf+7, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_Baudrate:
- if(2 == data_len){
- ret_write |= Write_Baudrate(g_mb_ctx.rxbuf+7, 2);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- case Cmd_DateTime:
- if(8 == data_len){
- ret_write |= Write_DateTime(g_mb_ctx.rxbuf+7, 8);
- if(ret_write&RET_DATAINVALID)
- g_mb_ctx.exception_code = INVALID_DATA;
- }else{
- g_mb_ctx.exception_code = INVALID_DATA;
- }
- break;
- default:
- g_mb_ctx.exception_code = INVALID_COMMAND;
- break;
- };
- }
-
-
-
- if(NO_EXCEPTION == g_mb_ctx.exception_code){
- //只需要收到的前面6个字节的数据
- g_mb_ctx.tx_len = 6;
-
- }else{
- g_mb_ctx.txbuf[1] |= 0x80; // func code
- g_mb_ctx.txbuf[2] = g_mb_ctx.exception_code;
- g_mb_ctx.tx_len = 3;
- }
-
- crc = crc16(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)(crc & 0x00ff);
-
- rs485_TransmitData(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
-
- if((ret_write&RET_NEED_SAVE) > 0 ){
- SaveConfig();
- }
-
- if((ret_write&RET_NEED_REBOOT) > 0 ){
- mdelay(200);
- NVIC_SystemReset();
- }
- }
- void mb_42_handle(void)
- {
- uint16_t crc;
- uint8_t ret_write = RET_OK;
-
- memcpy(g_mb_ctx.txbuf, g_mb_ctx.rxbuf, 4);
- g_mb_ctx.tx_len = 4;
-
- if(Cmd_IapUpgrade == g_mb_ctx.cmd){
-
- g_mb_ctx.tx_len += IAP_CmdHandle(g_mb_ctx.rxbuf+g_mb_ctx.tx_len, g_mb_ctx.rx_len-4-2, g_mb_ctx.txbuf+g_mb_ctx.tx_len);
- if(1 == iap_reboot){
- ret_write |= RET_NEED_REBOOT;
- }
-
- }else{
- g_mb_ctx.exception_code = INVALID_COMMAND;
- }
-
- if(NO_EXCEPTION == g_mb_ctx.exception_code){
- //啥都不用做
-
- }else{
- g_mb_ctx.txbuf[1] |= 0x80; // func code
- g_mb_ctx.txbuf[2] = g_mb_ctx.exception_code;
- g_mb_ctx.tx_len = 3;
- }
-
- crc = crc16(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)(crc & 0x00ff);
-
- rs485_TransmitData(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
-
- if((ret_write&RET_NEED_SAVE) > 0 ){
- SaveConfig();
- }
-
- if((ret_write&RET_NEED_REBOOT) > 0 ){
- mdelay(200);
- NVIC_SystemReset();
- }
-
- }
- void mb_funccodeerror_handle(void)
- {
- uint16_t crc;
- uint8_t ret_write = RET_OK;
-
- memcpy(g_mb_ctx.txbuf, g_mb_ctx.rxbuf, 4);
-
-
- g_mb_ctx.txbuf[1] |= 0x80;
- g_mb_ctx.txbuf[2] = INVALID_FUNCTION_CODE;
- g_mb_ctx.tx_len = 3;
-
- crc = crc16(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- g_mb_ctx.txbuf[g_mb_ctx.tx_len++] = (uint8_t)(crc & 0x00ff);
-
- rs485_TransmitData(g_mb_ctx.txbuf, g_mb_ctx.tx_len);
-
- }
- void mb_handle(void)
- {
- g_mb_ctx.func_code = g_mb_ctx.rxbuf[1];
- g_mb_ctx.cmd = ((uint16_t)g_mb_ctx.rxbuf[2]<<8) | g_mb_ctx.rxbuf[3];
-
- if(MB_FUNC_03 == g_mb_ctx.func_code){
- mb_03_handle();
- }else if(MB_FUNC_06 == g_mb_ctx.func_code){
- mb_06_handle();
- }else if(MB_FUNC_10 == g_mb_ctx.func_code){
- mb_10_handle();
- }else if(MB_FUNC_42 == g_mb_ctx.func_code){
- mb_42_handle();
- }else{
- mb_funccodeerror_handle();
- }
- }
- /**
- * Uart0_RxDataHandle
- *
- * @return none
- *
- * @brief 主任务处理函数
- */
- void Uart0_RxDataHandle(void)
- {
- g_mb_ctx.rxbuf = rs485_info.recv_buffer;
- g_mb_ctx.rx_len = rs485_info.recv_len;
- g_mb_ctx.txbuf = rs485_info.send_buffer;
- g_mb_ctx.txbuf_size = UART_TRANSMIT_DATA_POOL_COUNT;
- g_mb_ctx.tx_len = 0;
- g_mb_ctx.exception_code = NO_EXCEPTION;
-
- uint16_t crc;
- uint16_t ret_len = 0;
- uint8_t ret_write = RET_OK;
-
- if(g_mb_ctx.rx_len > 2){
-
- //printf("len:%d, addr:0x%x \r\n", g_mb_ctx.rx_len, g_mb_ctx.rxbuf[0]);
- //check rs485 addr
- if(config->addr == g_mb_ctx.rxbuf[0] || BROADCAST_ADDR == g_mb_ctx.rxbuf[0]){
-
- #if 1
- // check crc
- crc = ((uint16_t)g_mb_ctx.rxbuf[g_mb_ctx.rx_len-2]<<8) | g_mb_ctx.rxbuf[g_mb_ctx.rx_len-1];
- if (crc == crc16(g_mb_ctx.rxbuf, g_mb_ctx.rx_len-2))
- {
- mb_handle();
-
- #if 0
- //copy 地址、功能码、命令、到发送buf
- memcpy(txbuf, (void *)rxbuf, 4);
- tx_len +=4;
-
- if(MODBUS_FUNC_READ == rxbuf[1]){
- cmd = ((uint16_t)rxbuf[2]<<8) | rxbuf[3];
- switch(cmd){
- case Cmd_FirmwareVersion:
- ret_len = Read_FirmwareVersion(txbuf+tx_len, txbuf_size-tx_len);
- break;
- case Cmd_HardwareVersion:
- ret_len = Read_HardwareVersion(txbuf+tx_len, txbuf_size-tx_len);
- break;
- case Cmd_DeviceID:
- ret_len = Read_Deviceid(txbuf+tx_len, txbuf_size-tx_len);
- break;
- case Cmd_DeviceType:
- ret_len = Read_Devicetype(txbuf+tx_len, txbuf_size-tx_len);
- break;
- case Cmd_Addr:
- ret_len = Read_Addr(txbuf+tx_len, txbuf_size-tx_len);
- break;
- case Cmd_Baudrate:
- ret_len = Read_Baudrate(txbuf+tx_len, txbuf_size-tx_len);
- break;
- case Cmd_LockStatus:
- ret_len = Read_LockStatus(txbuf+tx_len, txbuf_size-tx_len);
- break;
- default:
- txbuf[1] |= 0x80;
- txbuf[tx_len] = INVALID_COMMAND;
- ret_len = 1;
- break;
- };
-
- if(0 == ret_len){
- // error occur
- txbuf[1] |= 0x80;
- txbuf[tx_len] = DEVICE_FAULT;
- ret_len = 1;
- }
-
- tx_len += ret_len;
- crc = crc16(txbuf, tx_len);
- txbuf[tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- txbuf[tx_len++] = (uint8_t)(crc & 0x00ff);
-
- uart0_TransmitData(txbuf, tx_len);
-
- }else if(MODBUS_FUNC_WRITE == rxbuf[1]){
-
- ret_write = RET_OK;
- cmd = ((uint16_t)rxbuf[2]<<8) | rxbuf[3];
- switch(cmd){
- case Cmd_HardwareVersion:
- ret_write = Write_HardwareVersion(rxbuf+4, rx_len-4-2);
- break;
- case Cmd_DeviceID:
- ret_write = Write_Deviceid(rxbuf+4, rx_len-4-2);
- break;
- case Cmd_DeviceType:
- ret_write = Write_Devicetype(rxbuf+4, rx_len-4-2);
- break;
- case Cmd_Addr:
- ret_write = Write_Addr(rxbuf+4, rx_len-4-2);
- break;
- case Cmd_Baudrate:
- ret_write = Write_Baudrate(rxbuf+4, rx_len-4-2);
- break;
- case Cmd_Reboot:
- //NVIC_SystemReset();
- ret_write = RET_NEED_REBOOT;
- break;
- case Cmd_Reset:
- ResetConfig();
- ret_write = RET_NEED_SAVE|RET_NEED_REBOOT;
- //NVIC_SystemReset();
- break;
- case Cmd_IapUpgrade:
- tx_len += IAP_CmdHandle(rxbuf+tx_len, rx_len-4-2, txbuf+tx_len);
- if(1 == iap_reboot){
- ret_write |= RET_NEED_REBOOT;
- }
- break;
- default:
- ret_write = RET_CMDINVALID;
- break;
- };
-
- if((ret_write&RET_ERROR_MASK) > 0){
-
- if((ret_write&RET_DATAINVALID) > 0){
- txbuf[1] |= 0x80;
- txbuf[tx_len++] = INVALID_DATA;
- }else if((ret_write&RET_CMDINVALID) > 0){
- txbuf[1] |= 0x80;
- txbuf[tx_len++] = INVALID_COMMAND;
- }
-
- }else{
- if(Cmd_IapUpgrade != cmd){
-
- //copy the whole rx frame
- memcpy(txbuf, (void *)rxbuf, rx_len-2);
- tx_len =rx_len-2;
-
- }else{
- //升级命令,啥都不用做
-
- }
-
- }
-
- crc = crc16(txbuf, tx_len);
- txbuf[tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- txbuf[tx_len++] = (uint8_t)(crc & 0x00ff);
-
- uart0_TransmitData(txbuf, tx_len);
-
- if((ret_write&RET_NEED_SAVE) > 0 ){
- SaveConfig();
- }
-
- if((ret_write&RET_NEED_REBOOT) > 0 ){
- mdelay(100);
- NVIC_SystemReset();
- }
-
-
- }else{
-
- //function code no exist
- txbuf[1] |= 0x80;
- txbuf[tx_len++] = INVALID_FUNCTION_CODE;
-
- crc = crc16(txbuf, tx_len);
- txbuf[tx_len++] = (uint8_t)((crc>>8) & 0x00ff);
- txbuf[tx_len++] = (uint8_t)(crc & 0x00ff);
-
- uart0_TransmitData(txbuf, tx_len);
- }
- #endif
-
- }
- #else
- memcpy(txbuf, (void *)rxbuf, rx_len);
- tx_len +=rx_len;
-
- uart0_TransmitData(txbuf, tx_len);
-
- #endif
-
- }
-
- } //END rx_len
-
- if(g_mb_ctx.rx_len > 0){
- rs485_RecvData();
- }
-
-
-
- //处理 485 收发状态
- if(rs485_info.dmasend_count >= UART_DMASEND_COUNT_RESET){
- rs485_info.dmasend_count = 0;
- RS485_RX_EN;
- }
- }
- /**
- * Task_Init
- *
- * @return none
- *
- * @brief 初始化
- */
- void Task_Init(void)
- {
- rs485_Initialize();
- memcpy(rs485_info.send_buffer, (void *)"Booted", 6);
-
- rs485_info.send_len = 6;
-
- rs485_TransmitData(rs485_info.send_buffer, rs485_info.send_len);
- rs485_RecvData();
-
-
- }
- /**
- * Task_Handle
- *
- * @return none
- *
- * @brief 主任务处理函数
- */
- void Task_Handle(void)
- {
-
- Process_RunPrd();
- Process_Storage();
- Uart0_RxDataHandle();
-
- Process_Poweroff();
- }
|