cebf746
Dependents: CEBF746_Master CEBF746_Slave_withTPM CEBF746_Master_New
Diff: lib_CEBF746.cpp
- Revision:
- 15:a640557e1778
- Parent:
- 14:9a48accab5fe
--- a/lib_CEBF746.cpp Thu Oct 20 13:13:06 2016 +0000 +++ b/lib_CEBF746.cpp Fri Oct 28 06:02:38 2016 +0000 @@ -4,8 +4,8 @@ #include "lib_spi_master.h" static cube_spi_master_t cebf746_master; -uint8_t readSelfTestValidateData[CEB_BUF_SIZE] = READ_SELF_TEST_DATA; // self test 버퍼 -uint8_t writeSelfTestValidateData[CEB_BUF_SIZE] = WRITE_SELF_TEST_DATA; // self test 버퍼 +uint8_t readSelfTestValidateData[CEB_BUF_SIZE] = READ_SELF_TEST_DATA; // self test 踰꾪띁 +uint8_t writeSelfTestValidateData[CEB_BUF_SIZE] = WRITE_SELF_TEST_DATA; // self test 踰꾪띁 void cebf746_use_init(void) { @@ -64,8 +64,9 @@ spi_master_start(&cebf746_master); cebf746_cmd_write(FUNC_READ_STATUS); - SPI_WAIT(1000); // wait return code + //SPI_WAIT(1000); // wait return code ret = cebf746_master.spi->write(0); + printf("-----------------\n"); spi_master_end(&cebf746_master); return ret; @@ -123,11 +124,11 @@ return ret; } -/** @brief CEBF746 패킷 Write +/** @brief CEBF746 �뙣�궥 Write * - * @param spi_handler SPI 핸들러 - * @param cebf746_command 송신 커맨드 OP-Code - * @param sendPacketStruct 전송 패킷 + * @param spi_handler SPI �빖�뱾�윭 + * @param cebf746_command �넚�떊 而ㅻ㎤�뱶 OP-Code + * @param sendPacketStruct �쟾�넚 �뙣�궥 * @return 0 is valid hex format, other is error */ int16_t cebf746_packet_write(CEBF716_FUNCTION cebf746_command, spiDataStr* sendPacketStruct) @@ -158,7 +159,7 @@ cebf746_master.spi->write((crc >> 8) & 0xFF); // crc upper cebf746_master.spi->write((crc ) & 0xFF); // crc lower - SPI_WAIT(1000); // wait return code + //SPI_WAIT(1000); // wait return code ret = cebf746_master.spi->write(0); spi_master_end(&cebf746_master); // ----- SPI End @@ -169,6 +170,30 @@ break; /* FUNC_WRITE_ENC_DATA */ case FUNC_WRITE_DEC_DATA: /* FUNC_WRITE_DEC_DATA */ + printf("FUNC_WRITE_DEC_DATA\n"); + cube_crc_16(sendPacketStruct->size, sendPacketStruct->buf, &crc); + sendPacketStruct->crc16 = crc; + + // ----- SPI Start + spi_master_start(&cebf746_master); + cebf746_cmd_write(FUNC_WRITE_DEC_DATA); + + cebf746_master.spi->write((sendPacketStruct->size >> 8) & 0xFF); // size upper + cebf746_master.spi->write((sendPacketStruct->size ) & 0xFF); // size lower + + for(int iCnt = 0 ; iCnt < sendPacketStruct->size ; ++iCnt) // data sendPacketStruct + { + cebf746_master.spi->write(*(sendPacketStruct->buf+iCnt)); + } + cebf746_master.spi->write((crc >> 8) & 0xFF); // crc upper + cebf746_master.spi->write((crc ) & 0xFF); // crc lower + + //SPI_WAIT(1000); // wait return code + ret = cebf746_master.spi->write(0); + spi_master_end(&cebf746_master); + // ----- SPI End + + cebf746_print_packet(sendPacketStruct); break; /* FUNC_WRITE_DEC_DATA */ case FUNC_WRITE_SELF_TEST: /* FUNC_WRITE_SELF_TEST */ @@ -178,7 +203,8 @@ // ----- SPI Start spi_master_start(&cebf746_master); cebf746_cmd_write(FUNC_WRITE_SELF_TEST); - + + cebf746_master.spi->write((sendPacketStruct->size >> 8) & 0xFF); // size upper cebf746_master.spi->write((sendPacketStruct->size ) & 0xFF); // size lower @@ -189,14 +215,15 @@ cebf746_master.spi->write((crc >> 8) & 0xFF); // crc upper cebf746_master.spi->write((crc ) & 0xFF); // crc lower - SPI_WAIT(1000); // wait return code + SPI_WAIT(50); // 161024 test ok : 50us ret = cebf746_master.spi->write(0); spi_master_end(&cebf746_master); // ----- SPI End if(WRITE_SELF_TEST_RET_CODE == ret) { - printf("[INFO] CEBF746 Write Self Test : (OK)\n"); + printf("[INFO] CEBF746 Write Self Test : (OK[%02X])\n", ret); + cebf746_print_packet(sendPacketStruct); } else { @@ -207,9 +234,9 @@ break; /* FUNC_WRITE_SELF_TEST */ default : - ret = -1; - printf("[ERROR] cebf746_packet_read();\n"); - printf("[ERROR] unknown command code=[%d]\n", cebf746_command); + ret = -1; + printf("[ERROR] cebf746_packet_read();\n"); + printf("[ERROR] unknown command code=[%d]\n", cebf746_command); return ret; } @@ -221,11 +248,11 @@ } -/** @brief CEBF746 패킷 Read +/** @brief CEBF746 �뙣�궥 Read * - * @param spi_handler SPI 핸들러 - * @param cebf746_command 송신 커맨드 OP-Code - * @param readPacketStruct 전송 패킷 + * @param spi_handler SPI �빖�뱾�윭 + * @param cebf746_command �넚�떊 而ㅻ㎤�뱶 OP-Code + * @param readPacketStruct �쟾�넚 �뙣�궥 * @return 0 is valid hex format, other is error */ int16_t cebf746_packet_read(CEBF716_FUNCTION cebf746_command, spiDataStr* readPacketStruct) @@ -253,19 +280,22 @@ // ---- SPI Start spi_master_start(&cebf746_master); cebf746_cmd_write(FUNC_READ_ENC_DATA); - - wait_ms(1000); // ★★★★ 패킷 생성이 스레드에서 처리되면 테스트후 코드 삭제 해야함 + //SPI_WAIT(500); + readPacketStruct->size = ((cebf746_master.spi->write(0) & 0xBB) << 8); // upper size get - readPacketStruct->size = (cebf746_master.spi->write(0) & 0xBB); // lower size get - printf("size : %d\n",readPacketStruct->size ); + readPacketStruct->size |= (cebf746_master.spi->write(0) & 0xBB); // lower size get + if(0 >= readPacketStruct->size || 20 <= readPacketStruct->size) + return -1; + //printf("size : %d\n",readPacketStruct->size ); for(int iCnt = 0 ; iCnt < readPacketStruct->size ; ++iCnt) // packet get { + //wait_us(10); *((readPacketStruct->buf) + iCnt) = (uint8_t)cebf746_master.spi->write(0); } readPacketStruct->crc16 = ((cebf746_master.spi->write(0) & 0xFF) << 8); // upper crc get - readPacketStruct->crc16 = (cebf746_master.spi->write(0) & 0xFF); // lower crc get + readPacketStruct->crc16 |= (cebf746_master.spi->write(0) & 0xFF); // lower crc get spi_master_end(&cebf746_master); // ---- SPI End @@ -277,10 +307,46 @@ case FUNC_READ_DEC_DATA: /* FUNC_READ_DEC_DATA */ printf("FUNC_READ_DEC_DATA:\n"); + memset(readPacketStruct->buf, 0, readPacketStruct->size); + readPacketStruct->size = 0; + readPacketStruct->crc16 = 0; + + // ---- SPI Start + spi_master_start(&cebf746_master); + cebf746_cmd_write(FUNC_READ_DEC_DATA); + //SPI_WAIT(500); + + + readPacketStruct->size = ((cebf746_master.spi->write(0) & 0xBB) << 8); // upper size get + readPacketStruct->size |= (cebf746_master.spi->write(0) & 0xBB); // lower size get + if(0 >= readPacketStruct->size || 20 <= readPacketStruct->size) + { + printf("예상 패킷 사이즈가 다릅니다. [%d]\n", readPacketStruct->size); + return -1; + } + //printf("size : %d\n",readPacketStruct->size ); + for(int iCnt = 0 ; iCnt < readPacketStruct->size ; ++iCnt) // packet get + { + //wait_us(10); + *((readPacketStruct->buf) + iCnt) = (uint8_t)cebf746_master.spi->write(0); + } + + readPacketStruct->crc16 = ((cebf746_master.spi->write(0) & 0xFF) << 8); // upper crc get + readPacketStruct->crc16 |= (cebf746_master.spi->write(0) & 0xFF); // lower crc get + spi_master_end(&cebf746_master); + // ---- SPI End + + cebf746_print_packet(readPacketStruct); break; /* FUNC_READ_DEC_DATA */ - case FUNC_READ_SELF_TEST: /* FUNC_READ_SELF_TEST */ - uint8_t tempBuf = 0; + + + + + + + + case FUNC_READ_SELF_TEST: /* FUNC_READ_SELF_TEST */ printf("FUNC_READ_SELF_TEST:\n"); memset(readPacketStruct->buf, 0, readPacketStruct->size); @@ -291,9 +357,10 @@ // ---- SPI Start spi_master_start(&cebf746_master); cebf746_cmd_write(FUNC_READ_SELF_TEST); - + + readPacketStruct->size = ((cebf746_master.spi->write(0) & 0xBB) << 8); // upper size get - readPacketStruct->size = (cebf746_master.spi->write(0) & 0xBB); // lower size get + readPacketStruct->size |= (cebf746_master.spi->write(0) & 0xBB); // lower size get for(int iCnt = 0 ; iCnt < readPacketStruct->size ; ++iCnt) // packet get { @@ -301,13 +368,15 @@ } readPacketStruct->crc16 = ((cebf746_master.spi->write(0) & 0xFF) << 8); // upper crc get - readPacketStruct->crc16 = (cebf746_master.spi->write(0) & 0xFF); // lower crc get + readPacketStruct->crc16 |= (cebf746_master.spi->write(0) & 0xFF); // lower crc get spi_master_end(&cebf746_master); // ---- SPI End printf("[INFO] CEBF746 Read Self Test : "); cebf746_selfTest_packet_compare(readSelfTestValidateData, readPacketStruct); + + cebf746_print_packet(readPacketStruct); break; /* FUNC_READ_SELF_TEST */ default : @@ -335,7 +404,7 @@ printf("data : "); for(int i=0;i<size;i++) { - printf("%02X ", buf[i]); + printf("%02X", buf[i]); } printf("\n"); @@ -347,4 +416,4 @@ void cebf746_print_packet(spiDataStr* in) { _cebf746_print_packet(in->size, in->buf, in->crc16); -} \ No newline at end of file +}