formatted code.

This commit is contained in:
Gatsby Van
2023-12-26 22:04:40 +08:00
parent 5cb5ffff59
commit 83e43f791c
42 changed files with 4688 additions and 3313 deletions
+4 -2
View File
@@ -39,7 +39,9 @@
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void AppAssert(const char * file, int line){ void AppAssert(const char *file, int line)
{
DBG_PRINT("[assert]\tin %s:%4d.\n", file, line); DBG_PRINT("[assert]\tin %s:%4d.\n", file, line);
while(1); while (1)
;
} }
+21 -12
View File
@@ -26,7 +26,9 @@
/************************** Constant Definitions *****************************/ /************************** Constant Definitions *****************************/
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
#define __PRINT_BLOCK(block) do {\ #define __PRINT_BLOCK(block) \
do \
{ \
block \ block \
} while (0) } while (0)
@@ -43,34 +45,41 @@
#endif #endif
#if _814_DEBUG_LEVEL >= 1 #if _814_DEBUG_LEVEL >= 1
#define DBG_ERROR(...) __PRINT_BLOCK( \ #define DBG_ERROR(...) __PRINT_BLOCK( \
printf("[error]\tin "__FILE__":%4d ",__LINE__); \ printf("[error]\tin "__FILE__ \
printf(__VA_ARGS__); \ ":%4d ", \
) __LINE__); \
printf(__VA_ARGS__);)
#endif #endif
#if _814_DEBUG_LEVEL >= 2 #if _814_DEBUG_LEVEL >= 2
#define DBG_WARNING(...) __PRINT_BLOCK( \ #define DBG_WARNING(...) __PRINT_BLOCK( \
printf("[warning]\t"); \ printf("[warning]\t"); \
printf(__VA_ARGS__); \ printf(__VA_ARGS__);)
)
#endif #endif
#if _814_DEBUG_LEVEL >= 3 #if _814_DEBUG_LEVEL >= 3
#define DBG_INFO(...) __PRINT_BLOCK( \ #define DBG_INFO(...) __PRINT_BLOCK( \
printf("[Info]\t"); \ printf("[Info]\t"); \
printf(__VA_ARGS__); \ printf(__VA_ARGS__);)
)
#endif #endif
#ifndef DBG_ERROR #ifndef DBG_ERROR
#define DBG_ERROR(...) {} #define DBG_ERROR(...) \
{ \
}
#endif #endif
#ifndef DBG_WARNING #ifndef DBG_WARNING
#define DBG_WARNING(...) {} #define DBG_WARNING(...) \
{ \
}
#endif #endif
#ifndef DBG_INFO #ifndef DBG_INFO
#define DBG_INFO(...) {} #define DBG_INFO(...) \
{ \
}
#endif #endif
#ifndef DBG_PRINT #ifndef DBG_PRINT
#define DBG_PRINT(...) {} #define DBG_PRINT(...) \
{ \
}
#endif #endif
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
+45 -35
View File
@@ -25,16 +25,14 @@
/************************** Constant Definitions *****************************/ /************************** Constant Definitions *****************************/
// Definition of each slot content // Definition of each slot content
L2_MSG_TYPE intraBmFrameMsgs[1] = { L2_MSG_TYPE intraBmFrameMsgs[1] = {
L2_MSG_BM L2_MSG_BM};
};
L2_MSG_TYPE intraW2AcqFrameMsgs[6] = { L2_MSG_TYPE intraW2AcqFrameMsgs[6] = {
L2_MSG_SCAN_INIT, L2_MSG_SCAN_INIT,
L2_MSG_SCAN_CONT_1, L2_MSG_SCAN_CONT_1,
L2_MSG_SCAN_CONT_2, L2_MSG_SCAN_CONT_2,
L2_MSG_SCAN_CONT_3, L2_MSG_SCAN_CONT_3,
L2_MSG_SCAN_CONT_4, L2_MSG_SCAN_CONT_4,
L2_MSG_SCAN_CONT_5 L2_MSG_SCAN_CONT_5};
};
L2_MSG_TYPE intraMgmtFrameMsgs[6] = { L2_MSG_TYPE intraMgmtFrameMsgs[6] = {
L2_MSG_TK_INIT, L2_MSG_TK_INIT,
@@ -42,33 +40,27 @@ L2_MSG_TYPE intraMgmtFrameMsgs[6] = {
L2_MSG_TK_CONT_1, L2_MSG_TK_CONT_1,
L2_MSG_TK_CONT_2, L2_MSG_TK_CONT_2,
L2_MSG_TK_CONT_3, L2_MSG_TK_CONT_3,
L2_MSG_NQT_INIT L2_MSG_NQT_INIT};
};
L2_MSG_TYPE taFrameMsgs[4] = { L2_MSG_TYPE taFrameMsgs[4] = {
L2_MSG_TA_INIT, L2_MSG_TA_INIT,
L2_MSG_TA_EXTD, L2_MSG_TA_EXTD,
L2_MSG_TA_CONT_1, L2_MSG_TA_CONT_1,
L2_MSG_TA_CONT_2 L2_MSG_TA_CONT_2};
};
L2_MSG_TYPE interSvcFrameMsgs[2] = { L2_MSG_TYPE interSvcFrameMsgs[2] = {
L2_MSG_LINK_INIT, L2_MSG_LINK_INIT,
L2_MSG_LINK_CONT_1 L2_MSG_LINK_CONT_1};
};
L2_MSG_TYPE rttReqFrameMsgs[3] = { L2_MSG_TYPE rttReqFrameMsgs[3] = {
L2_MSG_RTT_REQ_INIT, L2_MSG_RTT_REQ_INIT,
L2_MSG_RTT_REQ_EXTD, L2_MSG_RTT_REQ_EXTD,
L2_MSG_RTT_REQ_CONT L2_MSG_RTT_REQ_CONT};
};
L2_MSG_TYPE rttRspFrameMsgs[3] = { L2_MSG_TYPE rttRspFrameMsgs[3] = {
L2_MSG_RTT_RSP_INIT, L2_MSG_RTT_RSP_INIT,
L2_MSG_RTT_RSP_EXTD, L2_MSG_RTT_RSP_EXTD,
L2_MSG_RTT_RSP_CONT L2_MSG_RTT_RSP_CONT};
};
/*****************************************************************************/ /*****************************************************************************/
/** /**
@@ -84,7 +76,8 @@ L2_MSG_TYPE rttRspFrameMsgs[3] = {
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
void FrameInsertChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint64_t chunk){ void FrameInsertChunk(BIT_ARRAY *bitarr, uint32_t offset, uint32_t numBit, uint64_t chunk)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
ASSERT(bitarr->num_of_bits >= offset + numBit && numBit <= 64); ASSERT(bitarr->num_of_bits >= offset + numBit && numBit <= 64);
#ifdef CHUNK_FILL_STYLE_XJY #ifdef CHUNK_FILL_STYLE_XJY
@@ -94,7 +87,8 @@ void FrameInsertChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint6
uint32_t byteOff = 0; uint32_t byteOff = 0;
uint32_t bitWr = 0; uint32_t bitWr = 0;
uint32_t bitWrCnt = 0; uint32_t bitWrCnt = 0;
while (bitRmn > 0){ while (bitRmn > 0)
{
bitOff = offset % 8; bitOff = offset % 8;
byteOff = offset / 8; byteOff = offset / 8;
bitWr = (bitRmn > (8 - bitOff)) ? (8 - bitOff) : bitRmn; bitWr = (bitRmn > (8 - bitOff)) ? (8 - bitOff) : bitRmn;
@@ -123,7 +117,8 @@ void FrameInsertChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint6
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
void FrameGetChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint64_t* chunk){ void FrameGetChunk(BIT_ARRAY *bitarr, uint32_t offset, uint32_t numBit, uint64_t *chunk)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
ASSERT(bitarr->num_of_bits >= offset + numBit && numBit <= 64); ASSERT(bitarr->num_of_bits >= offset + numBit && numBit <= 64);
@@ -134,7 +129,8 @@ void FrameGetChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint64_t
uint32_t byteOff = offset / 8; uint32_t byteOff = offset / 8;
uint32_t bitRd = (bitRmn > (8 - bitOff)) ? (8 - bitOff) : bitRmn; uint32_t bitRd = (bitRmn > (8 - bitOff)) ? (8 - bitOff) : bitRmn;
uint32_t bitRdCnt = 0; uint32_t bitRdCnt = 0;
while (bitRmn > 0){ while (bitRmn > 0)
{
dataRd |= bit_array_get_wordn(bitarr, byteOff * 8 + (8 - bitOff - bitRd), bitRd); dataRd |= bit_array_get_wordn(bitarr, byteOff * 8 + (8 - bitOff - bitRd), bitRd);
bitRdCnt += bitRd; bitRdCnt += bitRd;
bitRmn -= bitRd; bitRmn -= bitRd;
@@ -164,11 +160,13 @@ void FrameGetChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint64_t
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int32_t L2_CrcGen(uint8_t* dataBuff, uint32_t dataLen, L2_CRC_TYPE crcType, uint32_t* crc){ static int32_t L2_CrcGen(uint8_t *dataBuff, uint32_t dataLen, L2_CRC_TYPE crcType, uint32_t *crc)
{
// ASSERT(dataBuff != NULL); // ASSERT(dataBuff != NULL);
int32_t Status = SUCCESS; int32_t Status = SUCCESS;
uint32_t crcVal = 0; uint32_t crcVal = 0;
switch (crcType){ switch (crcType)
{
case L2_CRC16_1021: case L2_CRC16_1021:
Status = crc16_gen(dataBuff, dataLen, 0x1021, (uint16_t *)&crcVal); Status = crc16_gen(dataBuff, dataLen, 0x1021, (uint16_t *)&crcVal);
break; break;
@@ -182,7 +180,8 @@ static int32_t L2_CrcGen(uint8_t* dataBuff, uint32_t dataLen, L2_CRC_TYPE crcTyp
Status = EPARAM; Status = EPARAM;
break; break;
} }
if(Status == SUCCESS){ if (Status == SUCCESS)
{
*crc = crcVal; *crc = crcVal;
} }
return Status; return Status;
@@ -201,17 +200,20 @@ static int32_t L2_CrcGen(uint8_t* dataBuff, uint32_t dataLen, L2_CRC_TYPE crcTyp
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_CrcAppend(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TYPE crcType){ int32_t L2_CrcAppend(uint8_t *dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TYPE crcType)
{
// ASSERT(dataBuff != NULL); // ASSERT(dataBuff != NULL);
int32_t Status = SUCCESS; int32_t Status = SUCCESS;
uint32_t crcVal = 0; uint32_t crcVal = 0;
uint8_t *crcPtr = dataBuff + crcPos; uint8_t *crcPtr = dataBuff + crcPos;
Status = L2_CrcGen(dataBuff, dataLen, crcType, &crcVal); Status = L2_CrcGen(dataBuff, dataLen, crcType, &crcVal);
if(Status != SUCCESS){ if (Status != SUCCESS)
{
return Status; return Status;
} }
#ifdef CHUNK_FILL_STYLE_XJY #ifdef CHUNK_FILL_STYLE_XJY
switch (crcType){ switch (crcType)
{
case L2_CRC16_1021: case L2_CRC16_1021:
*crcPtr = (uint8_t)(crcVal >> 8); *crcPtr = (uint8_t)(crcVal >> 8);
*(crcPtr + 1) = (uint8_t)(crcVal); *(crcPtr + 1) = (uint8_t)(crcVal);
@@ -228,7 +230,8 @@ int32_t L2_CrcAppend(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CR
break; break;
} }
#else #else
switch (crcType){ switch (crcType)
{
case L2_CRC16_1021: case L2_CRC16_1021:
*(crcPtr + 1) = (uint8_t)(crcVal >> 8); *(crcPtr + 1) = (uint8_t)(crcVal >> 8);
*crcPtr = (uint8_t)(crcVal); *crcPtr = (uint8_t)(crcVal);
@@ -261,7 +264,8 @@ int32_t L2_CrcAppend(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CR
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
bool L2_CrcCheck(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TYPE crcType){ bool L2_CrcCheck(uint8_t *dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TYPE crcType)
{
ASSERT(dataBuff != NULL); ASSERT(dataBuff != NULL);
int32_t Status = SUCCESS; int32_t Status = SUCCESS;
@@ -269,11 +273,13 @@ bool L2_CrcCheck(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TY
uint32_t crcComp = 0; uint32_t crcComp = 0;
uint8_t *crcPtr = dataBuff + crcPos; uint8_t *crcPtr = dataBuff + crcPos;
Status = L2_CrcGen(dataBuff, dataLen, crcType, &crcVal); Status = L2_CrcGen(dataBuff, dataLen, crcType, &crcVal);
if(Status != SUCCESS){ if (Status != SUCCESS)
{
return false; return false;
} }
#ifdef CHUNK_FILL_STYLE_XJY #ifdef CHUNK_FILL_STYLE_XJY
switch (crcType){ switch (crcType)
{
case L2_CRC16_1021: case L2_CRC16_1021:
crcComp = ((uint32_t)(*crcPtr)) << 8; crcComp = ((uint32_t)(*crcPtr)) << 8;
crcComp |= (uint32_t)(*(crcPtr + 1)); crcComp |= (uint32_t)(*(crcPtr + 1));
@@ -290,7 +296,8 @@ bool L2_CrcCheck(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TY
break; break;
} }
#else #else
switch (crcType){ switch (crcType)
{
case L2_CRC16_1021: case L2_CRC16_1021:
crcComp = ((uint32_t)(*(crcPtr + 1))) << 8; crcComp = ((uint32_t)(*(crcPtr + 1))) << 8;
crcComp |= (uint32_t)(*crcPtr); crcComp |= (uint32_t)(*crcPtr);
@@ -307,21 +314,24 @@ bool L2_CrcCheck(uint8_t* dataBuff, uint32_t dataLen, uint32_t crcPos, L2_CRC_TY
break; break;
} }
#endif #endif
if(Status == SUCCESS){ if (Status == SUCCESS)
{
// if ( crcVal == 0 ) // if ( crcVal == 0 )
// { // {
// return false ; // return false ;
// } // }
// else // else
if(crcComp == crcVal){ if (crcComp == crcVal)
{
return true; return true;
} }
else{ else
{
return false; return false;
} }
} }
else{ else
{
return false; return false;
} }
} }
+36 -20
View File
@@ -7,7 +7,6 @@
#include <math.h> #include <math.h>
#include "coord.h" #include "coord.h"
const COORD COORD_NIL = {NAN, NAN, NAN}; const COORD COORD_NIL = {NAN, NAN, NAN};
const COORD COORD_ZERO = {0.0, 0.0, 0.0}; const COORD COORD_ZERO = {0.0, 0.0, 0.0};
const COORD COORD_ONE = {1.0, 1.0, 1.0}; const COORD COORD_ONE = {1.0, 1.0, 1.0};
@@ -16,7 +15,8 @@ const COORD COORD_Y_AXIS = {0.0, 1.0, 0.0};
const COORD COORD_Z_AXIS = {0.0, 0.0, 1.0}; const COORD COORD_Z_AXIS = {0.0, 0.0, 1.0};
/** @brief Subtracts two coordinate vectors. */ /** @brief Subtracts two coordinate vectors. */
COORD Coord_Sub(COORD a, COORD b) { COORD Coord_Sub(COORD a, COORD b)
{
COORD ret; COORD ret;
ret.x = a.x - b.x; ret.x = a.x - b.x;
ret.y = a.y - b.y; ret.y = a.y - b.y;
@@ -25,7 +25,8 @@ COORD Coord_Sub(COORD a, COORD b) {
} }
/** @brief Adds two coordinate vectors. */ /** @brief Adds two coordinate vectors. */
COORD Coord_Add(COORD a, COORD b) { COORD Coord_Add(COORD a, COORD b)
{
COORD ret; COORD ret;
ret.x = a.x + b.x; ret.x = a.x + b.x;
ret.y = a.y + b.y; ret.y = a.y + b.y;
@@ -34,14 +35,16 @@ COORD Coord_Add(COORD a, COORD b) {
} }
/** @brief Multiplies a coordinate vector by a real number. */ /** @brief Multiplies a coordinate vector by a real number. */
void Coord_ScalarMult(COORD *a, double t) { void Coord_ScalarMult(COORD *a, double t)
{
a->x *= t; a->x *= t;
a->y *= t; a->y *= t;
a->z *= t; a->z *= t;
} }
/** @brief Multiplies a coordinate vector by a real number. */ /** @brief Multiplies a coordinate vector by a real number. */
COORD Coord_GetScalarMult(COORD a, double t) { COORD Coord_GetScalarMult(COORD a, double t)
{
COORD ret; COORD ret;
ret.x = a.x * t; ret.x = a.x * t;
ret.y = a.y * t; ret.y = a.y * t;
@@ -50,14 +53,16 @@ COORD Coord_GetScalarMult(COORD a, double t) {
} }
/** @brief Divides a coordinate vector by a real number. */ /** @brief Divides a coordinate vector by a real number. */
void Coord_ScalarDiv(COORD *a, double t) { void Coord_ScalarDiv(COORD *a, double t)
{
a->x /= t; a->x /= t;
a->y /= t; a->y /= t;
a->z /= t; a->z /= t;
} }
/** @brief Divides a coordinate vector by a real number. */ /** @brief Divides a coordinate vector by a real number. */
COORD Coord_GetScalarDiv(COORD a, double t) { COORD Coord_GetScalarDiv(COORD a, double t)
{
COORD ret; COORD ret;
ret.x = a.x / t; ret.x = a.x / t;
ret.y = a.y / t; ret.y = a.y / t;
@@ -66,12 +71,14 @@ COORD Coord_GetScalarDiv(COORD a, double t) {
} }
/** @brief Dot product. */ /** @brief Dot product. */
inline double Coord_Dot(COORD a, COORD b) { inline double Coord_Dot(COORD a, COORD b)
{
return (a.x * b.x + a.y * b.y + a.z * b.z); return (a.x * b.x + a.y * b.y + a.z * b.z);
} }
/** @brief Cross product. */ /** @brief Cross product. */
COORD Coord_Cross(COORD a, COORD b) { COORD Coord_Cross(COORD a, COORD b)
{
COORD ret; COORD ret;
ret.x = a.y * b.z - a.z * b.y; ret.x = a.y * b.z - a.z * b.y;
ret.y = a.z * b.x - a.x * b.z; ret.y = a.z * b.x - a.x * b.z;
@@ -80,7 +87,8 @@ COORD Coord_Cross(COORD a, COORD b) {
} }
/** @brief Multiplies two coordinate vector in elementwise. */ /** @brief Multiplies two coordinate vector in elementwise. */
COORD Coord_ElementwiseMult(COORD a, COORD b) { COORD Coord_ElementwiseMult(COORD a, COORD b)
{
COORD ret; COORD ret;
ret.x = a.x * b.x; ret.x = a.x * b.x;
ret.y = a.y * b.y; ret.y = a.y * b.y;
@@ -89,7 +97,8 @@ COORD Coord_ElementwiseMult(COORD a, COORD b) {
} }
/** @brief Divides two coordinate vector in elementwise. */ /** @brief Divides two coordinate vector in elementwise. */
COORD Coord_ElementwiseDiv(COORD a, COORD b) { COORD Coord_ElementwiseDiv(COORD a, COORD b)
{
COORD ret; COORD ret;
ret.x = a.x / b.x; ret.x = a.x / b.x;
ret.y = a.y / b.y; ret.y = a.y / b.y;
@@ -98,43 +107,49 @@ COORD Coord_ElementwiseDiv(COORD a, COORD b) {
} }
/** @brief Calculate a coordinate vector length^2 (omits calling square root). */ /** @brief Calculate a coordinate vector length^2 (omits calling square root). */
double Coord_SquareLength(COORD a) { double Coord_SquareLength(COORD a)
{
return (a.x * a.x + a.y * a.y + a.z * a.z); return (a.x * a.x + a.y * a.y + a.z * a.z);
} }
/** @brief Calculate a coordinate vector length. */ /** @brief Calculate a coordinate vector length. */
double Coord_Length(COORD a) { double Coord_Length(COORD a)
{
return sqrt(Coord_SquareLength(a)); return sqrt(Coord_SquareLength(a));
} }
/** @brief Calculate two coordinates' distance. */ /** @brief Calculate two coordinates' distance. */
double Coord_Distance(COORD a, COORD b) { double Coord_Distance(COORD a, COORD b)
{
return Coord_Length(Coord_Sub(a, b)); return Coord_Length(Coord_Sub(a, b));
} }
/** @brief Normalize coordinate vector. */ /** @brief Normalize coordinate vector. */
void Coord_Normalize(COORD *a) { void Coord_Normalize(COORD *a)
{
Coord_ScalarDiv(a, Coord_Length(*a)); Coord_ScalarDiv(a, Coord_Length(*a));
} }
/** @brief Normalize coordinate vector. */ /** @brief Normalize coordinate vector. */
COORD Coord_GetNormalized(COORD a) { COORD Coord_GetNormalized(COORD a)
{
Coord_Normalize(&a); Coord_Normalize(&a);
return a; return a;
} }
/** @brief Returns the angle between the two vectors. */ /** @brief Returns the angle between the two vectors. */
double Coord_Angle(COORD a, COORD b) { double Coord_Angle(COORD a, COORD b)
{
Coord_Normalize(&a); Coord_Normalize(&a);
Coord_Normalize(&b); Coord_Normalize(&b);
return acos(Coord_Dot(a, b)); return acos(Coord_Dot(a, b));
} }
// Additional Coordinates convert // Additional Coordinates convert
// TODO: Confirm correctness // TODO: Confirm correctness
/** @brief Convert coordinate from Launch coordinate system to ECEF. */ /** @brief Convert coordinate from Launch coordinate system to ECEF. */
COORD Coord_Launch2Ecef(COORD o, COORD p, double A, double B, double lambda) { COORD Coord_Launch2Ecef(COORD o, COORD p, double A, double B, double lambda)
{
COORD ret; COORD ret;
double sinA, cosA, sinB, cosB, sinL, cosL; double sinA, cosA, sinB, cosB, sinL, cosL;
double S11, S12, S13, S21, S22, S23, S31, S32, S33; double S11, S12, S13, S21, S22, S23, S31, S32, S33;
@@ -165,7 +180,8 @@ COORD Coord_Launch2Ecef(COORD o, COORD p, double A, double B, double lambda) {
} }
/** @brief Convert coordinate from launch coordinate system to Inertia coordinate system. */ /** @brief Convert coordinate from launch coordinate system to Inertia coordinate system. */
COORD Coord_Launch2Inertia(COORD o, COORD p, double A, double B, double we, double t) { COORD Coord_Launch2Inertia(COORD o, COORD p, double A, double B, double we, double t)
{
COORD ret; COORD ret;
double OmgX, OmgY, OmgZ, sinwet, coswet; double OmgX, OmgY, OmgZ, sinwet, coswet;
double M11, M12, M13, M21, M22, M23, M31, M32, M33; double M11, M12, M13, M21, M22, M23, M31, M32, M33;
@@ -4,7 +4,6 @@
// SPDX-License-Identifier: LGPL-3.0-or-later // SPDX-License-Identifier: LGPL-3.0-or-later
// //
#ifndef _COORD_H #ifndef _COORD_H
#define _COORD_H #define _COORD_H
@@ -16,7 +15,8 @@
* @ingroup utils * @ingroup utils
*/ */
typedef struct{ typedef struct
{
double x; double x;
double y; double y;
double z; double z;
@@ -78,4 +78,3 @@ COORD Coord_GetNormalized(COORD a);
double Coord_Angle(COORD a, COORD b); double Coord_Angle(COORD a, COORD b);
#endif #endif
@@ -7,13 +7,12 @@
#include <math.h> #include <math.h>
#include "eulerangles.h" #include "eulerangles.h"
const EULERANGLES EULERANGLES_NIL = {NAN, NAN, NAN}; const EULERANGLES EULERANGLES_NIL = {NAN, NAN, NAN};
const EULERANGLES EULERANGLES_ZERO = {0.0, 0.0, 0.0}; const EULERANGLES EULERANGLES_ZERO = {0.0, 0.0, 0.0};
/** @brief Add two EulerAngles. */ /** @brief Add two EulerAngles. */
EULERANGLES EulerAngles_Add(EULERANGLES e1, EULERANGLES e2) { EULERANGLES EulerAngles_Add(EULERANGLES e1, EULERANGLES e2)
{
EULERANGLES ret; EULERANGLES ret;
ret.alpha = e1.alpha + e2.alpha; ret.alpha = e1.alpha + e2.alpha;
ret.beta = e1.beta + e2.beta; ret.beta = e1.beta + e2.beta;
@@ -22,7 +21,8 @@ EULERANGLES EulerAngles_Add(EULERANGLES e1, EULERANGLES e2) {
} }
/** @brief Subtracts two EulerAngles. */ /** @brief Subtracts two EulerAngles. */
EULERANGLES EulerAngles_Sub(EULERANGLES e1, EULERANGLES e2) { EULERANGLES EulerAngles_Sub(EULERANGLES e1, EULERANGLES e2)
{
EULERANGLES ret; EULERANGLES ret;
ret.alpha = e1.alpha - e2.alpha; ret.alpha = e1.alpha - e2.alpha;
ret.beta = e1.beta - e2.beta; ret.beta = e1.beta - e2.beta;
@@ -31,27 +31,31 @@ EULERANGLES EulerAngles_Sub(EULERANGLES e1, EULERANGLES e2) {
} }
/** @brief Multiplies EulerAngles by a real number. */ /** @brief Multiplies EulerAngles by a real number. */
void EulerAngles_ScalarMult(EULERANGLES *e, double f) { void EulerAngles_ScalarMult(EULERANGLES *e, double f)
{
e->alpha *= f; e->alpha *= f;
e->beta *= f; e->beta *= f;
e->gamma *= f; e->gamma *= f;
} }
/** @brief Multiplies EulerAngles by a real number. */ /** @brief Multiplies EulerAngles by a real number. */
EULERANGLES EulerAngles_GetScalarMult(EULERANGLES e, double f) { EULERANGLES EulerAngles_GetScalarMult(EULERANGLES e, double f)
{
EulerAngles_ScalarMult(&e, f); EulerAngles_ScalarMult(&e, f);
return e; return e;
} }
/** @brief Normalize EulerAngles. */ /** @brief Normalize EulerAngles. */
void EulerAngles_Normalize(EULERANGLES *e) { void EulerAngles_Normalize(EULERANGLES *e)
{
e->alpha = fmod(e->alpha, 2 * EUL_PI); e->alpha = fmod(e->alpha, 2 * EUL_PI);
e->beta = fmod(e->beta, 2 * EUL_PI); e->beta = fmod(e->beta, 2 * EUL_PI);
e->gamma = fmod(e->gamma, 2 * EUL_PI); e->gamma = fmod(e->gamma, 2 * EUL_PI);
} }
/** @brief Normalize EulerAngles. */ /** @brief Normalize EulerAngles. */
EULERANGLES EulerAngles_GetNormalize(EULERANGLES e) { EULERANGLES EulerAngles_GetNormalize(EULERANGLES e)
{
EulerAngles_Normalize(&e); EulerAngles_Normalize(&e);
return e; return e;
} }
@@ -16,13 +16,15 @@
* rotation along the Y axis rotates Z into X (leans forward), positive rotation * rotation along the Y axis rotates Z into X (leans forward), positive rotation
* along the X axis rotates Y into Z (leans right). * along the X axis rotates Y into Z (leans right).
*/ */
typedef struct { typedef struct
{
double alpha; // unit rad double alpha; // unit rad
double beta; // unit rad double beta; // unit rad
double gamma; // unit rad double gamma; // unit rad
} EULERANGLES; } EULERANGLES;
typedef enum { typedef enum
{
EULERANGLES_NONE = 0, EULERANGLES_NONE = 0,
EULERANGLES_ZYX = 1, EULERANGLES_ZYX = 1,
EULERANGLES_ZYZ = 2, EULERANGLES_ZYZ = 2,
@@ -51,4 +53,3 @@ void EulerAngles_Normalize(EULERANGLES *e);
EULERANGLES EulerAngles_GetNormalize(EULERANGLES e); EULERANGLES EulerAngles_GetNormalize(EULERANGLES e);
#endif #endif
@@ -11,12 +11,14 @@
#include "eulerangles.h" #include "eulerangles.h"
#include "quaternion.h" #include "quaternion.h"
typedef union { typedef union
{
float value; float value;
uint32_t raw; uint32_t raw;
} SOFT_FLOAT; } SOFT_FLOAT;
typedef union { typedef union
{
double value; double value;
uint64_t raw; uint64_t raw;
} SOFT_DOUBLE; } SOFT_DOUBLE;
@@ -27,7 +29,8 @@ const SOFT_DOUBLE EPS_DOUBLE = {.raw = 0x3CB0000000000000};
const QUATERNION QUATERNION_IDENTITY = {1.0, 0.0, 0.0, 0.0}; const QUATERNION QUATERNION_IDENTITY = {1.0, 0.0, 0.0, 0.0};
const QUATERNION QUATERNION_NIL = {NAN, NAN, NAN, NAN}; const QUATERNION QUATERNION_NIL = {NAN, NAN, NAN, NAN};
/** @brief Subtracts two quaternions. */ /** @brief Subtracts two quaternions. */
QUATERNION Quaternion_Sub(QUATERNION q1, QUATERNION q2) { QUATERNION Quaternion_Sub(QUATERNION q1, QUATERNION q2)
{
QUATERNION ret; QUATERNION ret;
ret.s = q1.s - q2.s; ret.s = q1.s - q2.s;
ret.i = q1.i - q2.i; ret.i = q1.i - q2.i;
@@ -37,7 +40,8 @@ QUATERNION Quaternion_Sub(QUATERNION q1, QUATERNION q2) {
} }
/** @brief Adds two quaternions. */ /** @brief Adds two quaternions. */
QUATERNION Quaternion_Add(QUATERNION q1, QUATERNION q2) { QUATERNION Quaternion_Add(QUATERNION q1, QUATERNION q2)
{
QUATERNION ret; QUATERNION ret;
ret.s = q1.s + q2.s; ret.s = q1.s + q2.s;
ret.i = q1.i + q2.i; ret.i = q1.i + q2.i;
@@ -47,7 +51,8 @@ QUATERNION Quaternion_Add(QUATERNION q1, QUATERNION q2) {
} }
/** @brief Multiplies a quaternion by a real number. */ /** @brief Multiplies a quaternion by a real number. */
void Quaternion_ScalarMult(QUATERNION *q, double t) { void Quaternion_ScalarMult(QUATERNION *q, double t)
{
q->s *= t; q->s *= t;
q->i *= t; q->i *= t;
q->j *= t; q->j *= t;
@@ -55,13 +60,15 @@ void Quaternion_ScalarMult(QUATERNION *q, double t) {
} }
/** @brief Multiplies a quaternion by a real number. */ /** @brief Multiplies a quaternion by a real number. */
QUATERNION Quaternion_GetScalarMult(QUATERNION a, double t) { QUATERNION Quaternion_GetScalarMult(QUATERNION a, double t)
{
Quaternion_ScalarMult(&a, t); Quaternion_ScalarMult(&a, t);
return a; return a;
} }
/** @brief Divides a quaternion by a real number. */ /** @brief Divides a quaternion by a real number. */
void Quaternion_ScalarDiv(QUATERNION *q, double t) { void Quaternion_ScalarDiv(QUATERNION *q, double t)
{
q->s /= t; q->s /= t;
q->i /= t; q->i /= t;
q->j /= t; q->j /= t;
@@ -69,13 +76,15 @@ void Quaternion_ScalarDiv(QUATERNION *q, double t) {
} }
/** @brief Divides a quaternion by a real number. */ /** @brief Divides a quaternion by a real number. */
QUATERNION Quaternion_GetScalarDiv(QUATERNION q, double t) { QUATERNION Quaternion_GetScalarDiv(QUATERNION q, double t)
{
Quaternion_ScalarDiv(&q, t); Quaternion_ScalarDiv(&q, t);
return q; return q;
} }
/** @brief Multiplies two quaternions. */ /** @brief Multiplies two quaternions. */
QUATERNION Quaternion_Mult(QUATERNION q1, QUATERNION q2) { QUATERNION Quaternion_Mult(QUATERNION q1, QUATERNION q2)
{
QUATERNION ret; QUATERNION ret;
ret.s = q1.s * q2.s - (q1.i * q2.i + q1.j * q2.j + q1.k * q2.k); ret.s = q1.s * q2.s - (q1.i * q2.i + q1.j * q2.j + q1.k * q2.k);
ret.i = q1.j * q2.k - q1.k * q2.j + q1.s * q2.i + q1.i * q2.s; ret.i = q1.j * q2.k - q1.k * q2.j + q1.s * q2.i + q1.i * q2.s;
@@ -85,69 +94,80 @@ QUATERNION Quaternion_Mult(QUATERNION q1, QUATERNION q2) {
} }
/** @brief Divides two quaternions. */ /** @brief Divides two quaternions. */
QUATERNION Quaternion_Div(QUATERNION q1, QUATERNION q2) { QUATERNION Quaternion_Div(QUATERNION q1, QUATERNION q2)
{
Quaternion_Inverse(&q2); Quaternion_Inverse(&q2);
return Quaternion_Mult(q1, q2); return Quaternion_Mult(q1, q2);
} }
/** @brief Dot product. */ /** @brief Dot product. */
inline double Quaternion_Dot(QUATERNION q1, QUATERNION q2) { inline double Quaternion_Dot(QUATERNION q1, QUATERNION q2)
{
return (q1.s * q2.s + q1.i * q2.i + q1.j * q2.j + q1.k * q2.k); return (q1.s * q2.s + q1.i * q2.i + q1.j * q2.j + q1.k * q2.k);
} }
/** @brief Calculate a quaternion length^2 (omits calling square root). */ /** @brief Calculate a quaternion length^2 (omits calling square root). */
inline double Quaternion_SquareLength(QUATERNION q) { inline double Quaternion_SquareLength(QUATERNION q)
{
return (q.s * q.s + q.i * q.i + q.j * q.j + q.k * q.k); return (q.s * q.s + q.i * q.i + q.j * q.j + q.k * q.k);
} }
/** @brief Calculate a quaternion length. */ /** @brief Calculate a quaternion length. */
inline double Quaternion_Length(QUATERNION q) { inline double Quaternion_Length(QUATERNION q)
{
return sqrt(Quaternion_SquareLength(q)); return sqrt(Quaternion_SquareLength(q));
} }
/** @brief Normalize quaternion. */ /** @brief Normalize quaternion. */
void Quaternion_Normalize(QUATERNION *q) { void Quaternion_Normalize(QUATERNION *q)
{
Quaternion_ScalarDiv(q, Quaternion_Length(*q)); Quaternion_ScalarDiv(q, Quaternion_Length(*q));
} }
/** @brief Normalize quaternion. */ /** @brief Normalize quaternion. */
QUATERNION Quaternion_GetNormalized(QUATERNION q) { QUATERNION Quaternion_GetNormalized(QUATERNION q)
{
Quaternion_Normalize(&q); Quaternion_Normalize(&q);
return q; return q;
} }
/** @brief Conjugated quaternion. */ /** @brief Conjugated quaternion. */
void Quaternion_Conjugated(QUATERNION *q) { void Quaternion_Conjugated(QUATERNION *q)
{
q->i = -q->i; q->i = -q->i;
q->j = -q->j; q->j = -q->j;
q->k = -q->k; q->k = -q->k;
} }
/** @brief Conjugated quaternion. */ /** @brief Conjugated quaternion. */
QUATERNION Quaternion_GetConjugated(QUATERNION q) { QUATERNION Quaternion_GetConjugated(QUATERNION q)
{
Quaternion_Conjugated(&q); Quaternion_Conjugated(&q);
return q; return q;
} }
/** @brief Inverse quaternion. */ /** @brief Inverse quaternion. */
void Quaternion_Inverse(QUATERNION *q) { void Quaternion_Inverse(QUATERNION *q)
{
Quaternion_Conjugated(q); Quaternion_Conjugated(q);
Quaternion_ScalarDiv(q, Quaternion_SquareLength(*q)); Quaternion_ScalarDiv(q, Quaternion_SquareLength(*q));
} }
/** @brief Inverse quaternion. */ /** @brief Inverse quaternion. */
QUATERNION Quaternion_GeInverse(QUATERNION q) { QUATERNION Quaternion_GeInverse(QUATERNION q)
{
Quaternion_Inverse(&q); Quaternion_Inverse(&q);
return q; return q;
} }
inline double Quaternion_GetRotationAngle(QUATERNION q) { inline double Quaternion_GetRotationAngle(QUATERNION q)
{
return (acos(q.s) * 2); return (acos(q.s) * 2);
} }
/** @brief Get axis of quaternion. */ /** @brief Get axis of quaternion. */
COORD Quaternion_GetRotationAxis(QUATERNION q) { COORD Quaternion_GetRotationAxis(QUATERNION q)
{
double sin_theta_inv; double sin_theta_inv;
COORD ret; COORD ret;
sin_theta_inv = 1.0 / sin(acos(q.s)); sin_theta_inv = 1.0 / sin(acos(q.s));
@@ -158,7 +178,8 @@ COORD Quaternion_GetRotationAxis(QUATERNION q) {
} }
/** @brief Get the axis and rotate angle of a unit Quaternion */ /** @brief Get the axis and rotate angle of a unit Quaternion */
void Quaternion_GetRotationAxisAndAngle(QUATERNION q, COORD *axis, double *angle) { void Quaternion_GetRotationAxisAndAngle(QUATERNION q, COORD *axis, double *angle)
{
double sin_theta_inv; double sin_theta_inv;
*angle = acos(q.s); *angle = acos(q.s);
@@ -173,7 +194,8 @@ void Quaternion_GetRotationAxisAndAngle(QUATERNION q, COORD *axis, double *angle
} }
/** @brief Decompose the rotation on to 2 parts. */ /** @brief Decompose the rotation on to 2 parts. */
void Quaternion_GetSwingAndTwist(QUATERNION q, COORD axis, QUATERNION *swing, QUATERNION *twist) { void Quaternion_GetSwingAndTwist(QUATERNION q, COORD axis, QUATERNION *swing, QUATERNION *twist)
{
double p; double p;
COORD v; COORD v;
v.x = q.i; v.x = q.i;
@@ -191,7 +213,8 @@ void Quaternion_GetSwingAndTwist(QUATERNION q, COORD axis, QUATERNION *swing, QU
} }
/** @brief rotates v by this quaternion (quaternion must be unit) */ /** @brief rotates v by this quaternion (quaternion must be unit) */
COORD Quaternion_Rotate(QUATERNION q, COORD v) { COORD Quaternion_Rotate(QUATERNION q, COORD v)
{
QUATERNION temp; QUATERNION temp;
COORD ret; COORD ret;
temp.s = 0; temp.s = 0;
@@ -208,10 +231,13 @@ COORD Quaternion_Rotate(QUATERNION q, COORD v) {
} }
/** @brief returns the euler angles from a rotation Quaternion */ /** @brief returns the euler angles from a rotation Quaternion */
EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type) { EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type)
{
EULERANGLES ret; EULERANGLES ret;
switch (type) { switch (type)
case EULERANGLES_ZYX : { {
case EULERANGLES_ZYX:
{
/* Covert form matlab 2023a */ /* Covert form matlab 2023a */
// Cap all inputs to asin to 1, since values >1 produce complex // Cap all inputs to asin to 1, since values >1 produce complex
// results // results
@@ -230,11 +256,13 @@ EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type) {
if (mask2) if (mask2)
asinValue = -1; asinValue = -1;
if(mask1 || mask2) { if (mask1 || mask2)
{
ret.alpha = (asinValue > 0) ? (-2.0 * atan2(q.i, q.s)) : (2.0 * atan2(q.i, q.s)); ret.alpha = (asinValue > 0) ? (-2.0 * atan2(q.i, q.s)) : (2.0 * atan2(q.i, q.s));
ret.gamma = 0; ret.gamma = 0;
} }
else { else
{
ret.alpha = atan2(2.0 * (q.i * q.j + q.s * q.k), q.s * q.s + q.i * q.i - q.j * q.j - q.k * q.k); ret.alpha = atan2(2.0 * (q.i * q.j + q.s * q.k), q.s * q.s + q.i * q.i - q.j * q.j - q.k * q.k);
ret.gamma = atan2(2.0 * (q.j * q.k + q.s * q.i), q.s * q.s - q.i * q.i - q.j * q.j + q.k * q.k); ret.gamma = atan2(2.0 * (q.j * q.k + q.s * q.i), q.s * q.s - q.i * q.i - q.j * q.j + q.k * q.k);
} }
@@ -242,13 +270,15 @@ EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type) {
break; break;
} }
case EULERANGLES_ZYZ : { case EULERANGLES_ZYZ:
{
// TODO; // TODO;
ret = EULERANGLES_NIL; ret = EULERANGLES_NIL;
break; break;
} }
case EULERANGLES_XYZ : { case EULERANGLES_XYZ:
{
/* Covert form matlab 2023a */ /* Covert form matlab 2023a */
// Prevent singularities as done in ZYX case // Prevent singularities as done in ZYX case
// Alternative to rotm2eul(quat2rotm(q), 'XYZ') with fewer // Alternative to rotm2eul(quat2rotm(q), 'XYZ') with fewer
@@ -271,11 +301,13 @@ EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type) {
asinValue = 1; asinValue = 1;
if (mask2) if (mask2)
asinValue = -1; asinValue = -1;
if(mask1 || mask2) { if (mask1 || mask2)
{
ret.alpha = 0; ret.alpha = 0;
ret.gamma = (asinValue > 0) ? (2.0 * atan2(q.i, q.s)) : (-2.0 * atan2(q.i, q.s)); ret.gamma = (asinValue > 0) ? (2.0 * atan2(q.i, q.s)) : (-2.0 * atan2(q.i, q.s));
} }
else { else
{
ret.alpha = atan2(-2.0 * (q.j * q.k - q.s * q.i), q.s * q.s - q.i * q.i - q.j * q.j + q.k * q.k); ret.alpha = atan2(-2.0 * (q.j * q.k - q.s * q.i), q.s * q.s - q.i * q.i - q.j * q.j + q.k * q.k);
ret.gamma = atan2(-2.0 * (q.i * q.j - q.s * q.k), q.s * q.s + q.i * q.i - q.j * q.j - q.k * q.k); ret.gamma = atan2(-2.0 * (q.i * q.j - q.s * q.k), q.s * q.s + q.i * q.i - q.j * q.j - q.k * q.k);
} }
@@ -283,7 +315,6 @@ EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type) {
break; break;
} }
default: default:
ret = EULERANGLES_NIL; ret = EULERANGLES_NIL;
} }
@@ -291,7 +322,8 @@ EULERANGLES Quaternion_ToEulerAngles(QUATERNION q, EULERANGLES_TYPE type) {
} }
/** @brief returns the rotation Quaternion from a euler angles */ /** @brief returns the rotation Quaternion from a euler angles */
QUATERNION Quaternion_FromeEulerAngles(EULERANGLES e, EULERANGLES_TYPE type) { QUATERNION Quaternion_FromeEulerAngles(EULERANGLES e, EULERANGLES_TYPE type)
{
QUATERNION ret; QUATERNION ret;
double cosx, sinx, cosy, siny, cosz, sinz; double cosx, sinx, cosy, siny, cosz, sinz;
cosx = cos(e.alpha * 0.5); cosx = cos(e.alpha * 0.5);
@@ -300,7 +332,8 @@ QUATERNION Quaternion_FromeEulerAngles(EULERANGLES e, EULERANGLES_TYPE type) {
siny = sin(e.beta * 0.5); siny = sin(e.beta * 0.5);
cosz = cos(e.gamma * 0.5); cosz = cos(e.gamma * 0.5);
sinz = sin(e.gamma * 0.5); sinz = sin(e.gamma * 0.5);
switch (type) { switch (type)
{
case EULERANGLES_ZYX: case EULERANGLES_ZYX:
ret.s = cosx * cosy * cosz + sinx * siny * sinz; ret.s = cosx * cosy * cosz + sinx * siny * sinz;
ret.i = cosx * cosy * sinz - sinx * siny * cosz; ret.i = cosx * cosy * sinz - sinx * siny * cosz;
@@ -338,10 +371,12 @@ QUATERNION Quaternion_RotationFromTo(COORD from, COORD to)
c = Coord_Dot(from, to); c = Coord_Dot(from, to);
if (c >= 1.0) { // If dot == 1, vectors are the same if (c >= 1.0)
{ // If dot == 1, vectors are the same
return QUATERNION_IDENTITY; // identity return QUATERNION_IDENTITY; // identity
} }
else if (c <= -1.0) { // exactly opposite else if (c <= -1.0)
{ // exactly opposite
axis = Coord_Cross(COORD_X_AXIS, from); axis = Coord_Cross(COORD_X_AXIS, from);
if (Coord_Length(axis) == 0) if (Coord_Length(axis) == 0)
@@ -367,9 +402,9 @@ QUATERNION Quaternion_RotationFromTo(COORD from, COORD to)
return ret; return ret;
} }
/** @brief linear quaternion interpolation */ /** @brief linear quaternion interpolation */
QUATERNION Quaternion_Lerp(QUATERNION q1, QUATERNION q2, double t) { QUATERNION Quaternion_Lerp(QUATERNION q1, QUATERNION q2, double t)
{
QUATERNION ret; QUATERNION ret;
Quaternion_ScalarMult(&q1, 1 - t); Quaternion_ScalarMult(&q1, 1 - t);
Quaternion_ScalarMult(&q1, t); Quaternion_ScalarMult(&q1, t);
@@ -378,27 +413,30 @@ QUATERNION Quaternion_Lerp(QUATERNION q1, QUATERNION q2, double t) {
return ret; return ret;
} }
/** @brief spherical linear interpolation */ /** @brief spherical linear interpolation */
QUATERNION Quaternion_Slerp(QUATERNION q1, QUATERNION q2, double t) { QUATERNION Quaternion_Slerp(QUATERNION q1, QUATERNION q2, double t)
{
double dot; double dot;
QUATERNION q3, ret; QUATERNION q3, ret;
dot = Quaternion_Dot(q1, q2); dot = Quaternion_Dot(q1, q2);
/* dot = cos(theta) /* dot = cos(theta)
if (dot < 0), q1 and q2 are more than 90 degrees apart, if (dot < 0), q1 and q2 are more than 90 degrees apart,
so we can invert one to reduce spinning */ so we can invert one to reduce spinning */
if (dot < 0) { if (dot < 0)
{
dot = -dot; dot = -dot;
q3.s = -q2.s; q3.s = -q2.s;
q3.i = -q2.i; q3.i = -q2.i;
q3.j = -q2.j; q3.j = -q2.j;
q3.k = -q2.k; q3.k = -q2.k;
} }
else { else
{
q3 = q2; q3 = q2;
} }
if (dot < 0.95) { if (dot < 0.95)
{
double angle = acos(dot); double angle = acos(dot);
Quaternion_ScalarMult(&q1, sin(angle * (1 - t))); Quaternion_ScalarMult(&q1, sin(angle * (1 - t)));
Quaternion_ScalarMult(&q3, sin(angle * t)); Quaternion_ScalarMult(&q3, sin(angle * t));
@@ -407,7 +445,8 @@ QUATERNION Quaternion_Slerp(QUATERNION q1, QUATERNION q2, double t) {
return ret; return ret;
} }
// if the angle is small, use linear interpolation // if the angle is small, use linear interpolation
else { else
{
return Quaternion_Lerp(q1, q3, t); return Quaternion_Lerp(q1, q3, t);
} }
} }
@@ -6,7 +6,8 @@
#include "eulerangles.h" #include "eulerangles.h"
// borrowed and adapted from https://github.com/MegaManSE/willperone/blob/master/Math/quaternion.h // borrowed and adapted from https://github.com/MegaManSE/willperone/blob/master/Math/quaternion.h
typedef struct { typedef struct
{
double s; double s;
double i; double i;
double j; double j;
@@ -106,4 +107,3 @@ QUATERNION Quaternion_Lerp(QUATERNION q1, QUATERNION q2, double t);
QUATERNION Quaternion_Slerp(QUATERNION q1, QUATERNION q2, double t); QUATERNION Quaternion_Slerp(QUATERNION q1, QUATERNION q2, double t);
#endif #endif
+238 -123
View File
@@ -27,7 +27,6 @@
#include "../util/util.h" #include "../util/util.h"
#include "../common.h" #include "../common.h"
/************************** Constant Definitions *****************************/ /************************** Constant Definitions *****************************/
#define NET_START_TIME_INFTY UINT32_MAX #define NET_START_TIME_INFTY UINT32_MAX
/************************** Variable Definitions *****************************/ /************************** Variable Definitions *****************************/
@@ -45,7 +44,8 @@ static void L2_DataAvailUpdate(L2_INST* instPtr, bool status);
/** /**
* @brief helper function to update linkelayer state * @brief helper function to update linkelayer state
*/ */
static inline void UpdateMainSate(L2_INST* instPtr, L2_NODE_STATE newSate) { static inline void UpdateMainSate(L2_INST *instPtr, L2_NODE_STATE newSate)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
DBG_INFO("MainStateUpdate: %d -> %d\n", instPtr->mainState, newSate); DBG_INFO("MainStateUpdate: %d -> %d\n", instPtr->mainState, newSate);
instPtr->nextState = newSate; instPtr->nextState = newSate;
@@ -54,7 +54,8 @@ static inline void UpdateMainSate(L2_INST* instPtr, L2_NODE_STATE newSate) {
/** /**
* @brief helper function to set slot number. * @brief helper function to set slot number.
*/ */
static void L2_SetSlotNum(L2_INST* instPtr, uint32_t newSlotNum){ static void L2_SetSlotNum(L2_INST *instPtr, uint32_t newSlotNum)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
DBG_INFO("Set new slot number to %u\n", newSlotNum); DBG_INFO("Set new slot number to %u\n", newSlotNum);
instPtr->slotNum.next = newSlotNum; instPtr->slotNum.next = newSlotNum;
@@ -63,7 +64,8 @@ static void L2_SetSlotNum(L2_INST* instPtr, uint32_t newSlotNum){
/** /**
* @brief helper function to set frame number * @brief helper function to set frame number
*/ */
static void L2_SetFrameNum(L2_INST* instPtr, uint32_t newFrameNum){ static void L2_SetFrameNum(L2_INST *instPtr, uint32_t newFrameNum)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
DBG_INFO("Set new frame number to %u\n", newFrameNum); DBG_INFO("Set new frame number to %u\n", newFrameNum);
instPtr->frameNum.next = newFrameNum; instPtr->frameNum.next = newFrameNum;
@@ -72,7 +74,8 @@ static void L2_SetFrameNum(L2_INST* instPtr, uint32_t newFrameNum){
/** /**
* @brief Update frame number * @brief Update frame number
*/ */
static inline void L2_UpdateSlotNum(L2_INST* instPtr) { static inline void L2_UpdateSlotNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint32_t slotCnt; uint32_t slotCnt;
// Shift record // Shift record
@@ -90,7 +93,8 @@ static inline void L2_UpdateSlotNum(L2_INST* instPtr) {
* @brief Update l2 frame buffer state * @brief Update l2 frame buffer state
* infer that if there is a new valid l2 frame up. * infer that if there is a new valid l2 frame up.
*/ */
static inline void L2_DataAvailUpdate(L2_INST* instPtr, bool status) { static inline void L2_DataAvailUpdate(L2_INST *instPtr, bool status)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
instPtr->dataAvail = status; instPtr->dataAvail = status;
} }
@@ -98,7 +102,8 @@ static inline void L2_DataAvailUpdate(L2_INST* instPtr, bool status) {
/** /**
* @brief Get the current node ID. * @brief Get the current node ID.
*/ */
inline uint8_t L2_GetNodeId(L2_INST* instPtr){ inline uint8_t L2_GetNodeId(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->myInfo.nodeId; return instPtr->myInfo.nodeId;
} }
@@ -106,7 +111,8 @@ inline uint8_t L2_GetNodeId(L2_INST* instPtr){
/** /**
* @brief Get the current cluster ID. * @brief Get the current cluster ID.
*/ */
inline uint8_t L2_GetClusterId(L2_INST* instPtr){ inline uint8_t L2_GetClusterId(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->myInfo.clustId; return instPtr->myInfo.clustId;
} }
@@ -114,7 +120,8 @@ inline uint8_t L2_GetClusterId(L2_INST* instPtr){
/** /**
* @brief Get the head node ID. * @brief Get the head node ID.
*/ */
uint8_t L2_GetHeadId(L2_INST* instPtr){ uint8_t L2_GetHeadId(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->myInfo.headId; return instPtr->myInfo.headId;
} }
@@ -122,7 +129,8 @@ uint8_t L2_GetHeadId(L2_INST* instPtr){
/** /**
* @brief Get the current node type. * @brief Get the current node type.
*/ */
inline L2_NODE_TYPE L2_GetNodeType(L2_INST* instPtr){ inline L2_NODE_TYPE L2_GetNodeType(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->myInfo.nodeType; return instPtr->myInfo.nodeType;
} }
@@ -130,7 +138,8 @@ inline L2_NODE_TYPE L2_GetNodeType(L2_INST* instPtr){
/** /**
* @brief Get the current linklayer state. * @brief Get the current linklayer state.
*/ */
inline L2_NODE_STATE L2_GetMainState(L2_INST* instPtr){ inline L2_NODE_STATE L2_GetMainState(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->mainState; return instPtr->mainState;
} }
@@ -138,7 +147,8 @@ inline L2_NODE_STATE L2_GetMainState(L2_INST* instPtr){
/** /**
* @brief Check if there is a valid l2 frame. * @brief Check if there is a valid l2 frame.
*/ */
inline bool L2_isDataAvail(L2_INST* instPtr) { inline bool L2_isDataAvail(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->dataAvail; return instPtr->dataAvail;
} }
@@ -146,7 +156,8 @@ inline bool L2_isDataAvail(L2_INST* instPtr) {
/** /**
* @brief Check if node is connected to the network * @brief Check if node is connected to the network
*/ */
inline bool L2_isCxn2Net(L2_INST* instPtr) { inline bool L2_isCxn2Net(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->cxn2Net; return instPtr->cxn2Net;
} }
@@ -154,7 +165,8 @@ inline bool L2_isCxn2Net(L2_INST* instPtr) {
/** /**
* @brief Get last frame number. * @brief Get last frame number.
*/ */
inline uint32_t L2_GetLastFrameNum(L2_INST* instPtr) { inline uint32_t L2_GetLastFrameNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->frameNum.last; return instPtr->frameNum.last;
} }
@@ -162,7 +174,8 @@ inline uint32_t L2_GetLastFrameNum(L2_INST* instPtr) {
/** /**
* @brief Get last slot number. * @brief Get last slot number.
*/ */
inline uint32_t L2_GetLastSlotNum(L2_INST* instPtr) { inline uint32_t L2_GetLastSlotNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->slotNum.last; return instPtr->slotNum.last;
} }
@@ -170,7 +183,8 @@ inline uint32_t L2_GetLastSlotNum(L2_INST* instPtr) {
/** /**
* @brief Get the frame type of last slot. * @brief Get the frame type of last slot.
*/ */
inline L2_FRAME_TYPE L2_GetLastFrameType(L2_INST* instPtr) { inline L2_FRAME_TYPE L2_GetLastFrameType(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->frame.last; return instPtr->frame.last;
} }
@@ -178,7 +192,8 @@ inline L2_FRAME_TYPE L2_GetLastFrameType(L2_INST* instPtr) {
/** /**
* @brief Get the tx/rx state of last slot. * @brief Get the tx/rx state of last slot.
*/ */
inline L2_TOR_TYPE L2_GetLastTor(L2_INST* instPtr) { inline L2_TOR_TYPE L2_GetLastTor(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->tor.last; return instPtr->tor.last;
} }
@@ -186,7 +201,8 @@ inline L2_TOR_TYPE L2_GetLastTor(L2_INST* instPtr) {
/** /**
* @brief Get current frame number. * @brief Get current frame number.
*/ */
inline uint32_t L2_GetCurrentFrameNum(L2_INST* instPtr) { inline uint32_t L2_GetCurrentFrameNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->frameNum.curr; return instPtr->frameNum.curr;
} }
@@ -194,7 +210,8 @@ inline uint32_t L2_GetCurrentFrameNum(L2_INST* instPtr) {
/** /**
* @brief Get current slot number. * @brief Get current slot number.
*/ */
inline uint32_t L2_GetCurrentSlotNum(L2_INST* instPtr) { inline uint32_t L2_GetCurrentSlotNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->slotNum.curr; return instPtr->slotNum.curr;
} }
@@ -202,7 +219,8 @@ inline uint32_t L2_GetCurrentSlotNum(L2_INST* instPtr) {
/** /**
* @brief Get the frame type of current slot. * @brief Get the frame type of current slot.
*/ */
inline L2_FRAME_TYPE L2_GetCurrentFrameType(L2_INST* instPtr) { inline L2_FRAME_TYPE L2_GetCurrentFrameType(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->frame.curr; return instPtr->frame.curr;
} }
@@ -210,7 +228,8 @@ inline L2_FRAME_TYPE L2_GetCurrentFrameType(L2_INST* instPtr) {
/** /**
* @brief Get the tx/rx state of current slot. * @brief Get the tx/rx state of current slot.
*/ */
inline L2_TOR_TYPE L2_GetCurrentTor(L2_INST* instPtr) { inline L2_TOR_TYPE L2_GetCurrentTor(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->tor.curr; return instPtr->tor.curr;
} }
@@ -218,14 +237,16 @@ inline L2_TOR_TYPE L2_GetCurrentTor(L2_INST* instPtr) {
/** /**
* @brief Get next frame number. * @brief Get next frame number.
*/ */
inline uint32_t L2_GetNextFrameNum(L2_INST* instPtr) { inline uint32_t L2_GetNextFrameNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->frameNum.next; return instPtr->frameNum.next;
} }
/** /**
* @brief Get next slot number. * @brief Get next slot number.
*/ */
inline uint32_t L2_GetNextSlotNum(L2_INST* instPtr) { inline uint32_t L2_GetNextSlotNum(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->slotNum.next; return instPtr->slotNum.next;
} }
@@ -233,7 +254,8 @@ inline uint32_t L2_GetNextSlotNum(L2_INST* instPtr) {
/** /**
* @brief Get the frame type of next slot. * @brief Get the frame type of next slot.
*/ */
inline L2_FRAME_TYPE L2_GetNextFrameType(L2_INST* instPtr) { inline L2_FRAME_TYPE L2_GetNextFrameType(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->frame.next; return instPtr->frame.next;
} }
@@ -241,7 +263,8 @@ inline L2_FRAME_TYPE L2_GetNextFrameType(L2_INST* instPtr) {
/** /**
* @brief Get the tx/rx state of next slot. * @brief Get the tx/rx state of next slot.
*/ */
inline L2_TOR_TYPE L2_GetNextTor(L2_INST* instPtr) { inline L2_TOR_TYPE L2_GetNextTor(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->tor.next; return instPtr->tor.next;
} }
@@ -250,7 +273,8 @@ inline L2_TOR_TYPE L2_GetNextTor(L2_INST* instPtr) {
/** /**
* @brief Get the RTT FSM. * @brief Get the RTT FSM.
*/ */
inline L2_RTT_FSM* L2_GetRttFsm(L2_INST* instPtr){ inline L2_RTT_FSM *L2_GetRttFsm(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->rttFSM; return instPtr->rttFSM;
} }
@@ -264,7 +288,8 @@ inline L2_RTT_FSM* L2_GetRttFsm(L2_INST* instPtr){
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
inline void L2_RcdShift(L2_RCD* rcd, int32_t netxVal) { inline void L2_RcdShift(L2_RCD *rcd, int32_t netxVal)
{
ASSERT(rcd != NULL); ASSERT(rcd != NULL);
rcd->last = rcd->curr; rcd->last = rcd->curr;
rcd->curr = rcd->next; rcd->curr = rcd->next;
@@ -281,12 +306,14 @@ inline void L2_RcdShift(L2_RCD* rcd, int32_t netxVal) {
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void L2_FrameStateUpdate(L2_INST* instPtr) { void L2_FrameStateUpdate(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
PHY_INST *phyRef = instPtr->config.phyInstPtr; PHY_INST *phyRef = instPtr->config.phyInstPtr;
// Update frame state. // Update frame state.
if (Phy_IsFrameValid(phyRef) == false) { if (Phy_IsFrameValid(phyRef) == false)
{
L2_DataAvailUpdate(instPtr, false); L2_DataAvailUpdate(instPtr, false);
return; return;
} }
@@ -295,17 +322,20 @@ void L2_FrameStateUpdate(L2_INST* instPtr) {
Phy_RetrieveRecvData(phyRef, instPtr->recvPktData); Phy_RetrieveRecvData(phyRef, instPtr->recvPktData);
instPtr->log.frameRecv++; instPtr->log.frameRecv++;
switch (instPtr->frame.last) { switch (instPtr->frame.last)
{
case L2_INTRA_SCAN_FRAME: case L2_INTRA_SCAN_FRAME:
// INTRA_BM_FRAME could be received if current frame type is INTRA_BM_FRAME // INTRA_BM_FRAME could be received if current frame type is INTRA_BM_FRAME
case L2_INTRA_BM_FRAME: case L2_INTRA_BM_FRAME:
instPtr->log.bmRecvCnt++; instPtr->log.bmRecvCnt++;
if (L2_CrcCheck(instPtr->recvPktData, BM_MSG_CRC_OFFSET / 8, if (L2_CrcCheck(instPtr->recvPktData, BM_MSG_CRC_OFFSET / 8,
BM_MSG_CRC_OFFSET / 8, L2_CRC8_31) == true) { BM_MSG_CRC_OFFSET / 8, L2_CRC8_31) == true)
{
L2_DataAvailUpdate(instPtr, true); L2_DataAvailUpdate(instPtr, true);
instPtr->log.bmCorrectCnt++; instPtr->log.bmCorrectCnt++;
} }
else { else
{
L2_DataAvailUpdate(instPtr, false); L2_DataAvailUpdate(instPtr, false);
} }
break; break;
@@ -313,11 +343,13 @@ void L2_FrameStateUpdate(L2_INST* instPtr) {
instPtr->log.mgmtRecvCnt++; instPtr->log.mgmtRecvCnt++;
if (L2_CrcCheck(instPtr->recvPktData, if (L2_CrcCheck(instPtr->recvPktData,
W2ACQ_FRAME_CRC_DLEN, W2ACQ_FRAME_CRC_POS, W2ACQ_FRAME_CRC_DLEN, W2ACQ_FRAME_CRC_POS,
L2_CRC16_8005) == true) { L2_CRC16_8005) == true)
{
L2_DataAvailUpdate(instPtr, true); L2_DataAvailUpdate(instPtr, true);
instPtr->log.mgmtCorrectCnt++; instPtr->log.mgmtCorrectCnt++;
} }
else { else
{
L2_DataAvailUpdate(instPtr, false); L2_DataAvailUpdate(instPtr, false);
} }
break; break;
@@ -335,11 +367,13 @@ void L2_FrameStateUpdate(L2_INST* instPtr) {
instPtr->log.svcRecvCnt++; instPtr->log.svcRecvCnt++;
if (L2_CrcCheck(instPtr->recvPktData, if (L2_CrcCheck(instPtr->recvPktData,
PDUH0_HEADER_CRC_OFFSET / 8, PDUH0_HEADER_CRC_OFFSET / 8, PDUH0_HEADER_CRC_OFFSET / 8, PDUH0_HEADER_CRC_OFFSET / 8,
L2_CRC8_31) == true) { L2_CRC8_31) == true)
{
L2_DataAvailUpdate(instPtr, true); L2_DataAvailUpdate(instPtr, true);
instPtr->log.svcCorrectCnt++; instPtr->log.svcCorrectCnt++;
} }
else { else
{
L2_DataAvailUpdate(instPtr, false); L2_DataAvailUpdate(instPtr, false);
} }
break; break;
@@ -347,7 +381,8 @@ void L2_FrameStateUpdate(L2_INST* instPtr) {
L2_DataAvailUpdate(instPtr, false); L2_DataAvailUpdate(instPtr, false);
break; break;
} }
if(L2_isDataAvail(instPtr)) { if (L2_isDataAvail(instPtr))
{
instPtr->log.frameCorrect++; instPtr->log.frameCorrect++;
} }
} }
@@ -364,18 +399,22 @@ void L2_FrameStateUpdate(L2_INST* instPtr) {
* L2_FRAME_NONE_LEN if the frame type code cannot be recognized. * L2_FRAME_NONE_LEN if the frame type code cannot be recognized.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE frameType) { int32_t L2_SetupNextSlot(L2_INST *instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE frameType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
PHY_INST *phyRef = instPtr->config.phyInstPtr; PHY_INST *phyRef = instPtr->config.phyInstPtr;
int32_t Status; int32_t Status;
if (frameType < L2_INTER_SCAN_FRAME || frameType > L2_INTRA_SVC_FRAME) { if (frameType < L2_INTER_SCAN_FRAME || frameType > L2_INTRA_SVC_FRAME)
{
return FAILURE; return FAILURE;
} }
// logging // logging
if(txrxMode == L2_TOR_TX) { if (txrxMode == L2_TOR_TX)
{
instPtr->log.frameTx++; instPtr->log.frameTx++;
switch(frameType){ switch (frameType)
{
case L2_INTRA_BM_FRAME: case L2_INTRA_BM_FRAME:
instPtr->log.bmTxCnt++; instPtr->log.bmTxCnt++;
break; break;
@@ -394,12 +433,12 @@ int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE f
default: default:
break; break;
} }
} }
else if (txrxMode == L2_TOR_RX) else if (txrxMode == L2_TOR_RX)
{ {
instPtr->log.frameRx++; instPtr->log.frameRx++;
switch(frameType){ switch (frameType)
{
case L2_INTRA_BM_FRAME: case L2_INTRA_BM_FRAME:
instPtr->log.bmRxCnt++; instPtr->log.bmRxCnt++;
break; break;
@@ -423,7 +462,8 @@ int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE f
#if _814_DEBUG_LEVEL >= 3 #if _814_DEBUG_LEVEL >= 3
char *torStr = NULL; char *torStr = NULL;
char *frameStr = NULL; char *frameStr = NULL;
switch(txrxMode){ switch (txrxMode)
{
case L2_TOR_SUSPEND: case L2_TOR_SUSPEND:
torStr = "SUSPEND"; torStr = "SUSPEND";
break; break;
@@ -434,7 +474,8 @@ int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE f
torStr = "RX"; torStr = "RX";
break; break;
} }
switch(frameType){ switch (frameType)
{
case L2_INTER_SCAN_FRAME: case L2_INTER_SCAN_FRAME:
frameStr = "intER Scan"; frameStr = "intER Scan";
break; break;
@@ -488,12 +529,14 @@ int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE f
TIME_WORD pullTime = {0, 0}; TIME_WORD pullTime = {0, 0};
pullTime.int_part = TimeCtrl_GetIntCnt(instPtr->config.tmcInstPtr) + 1; pullTime.int_part = TimeCtrl_GetIntCnt(instPtr->config.tmcInstPtr) + 1;
Status = Phy_SetTimeWord(phyRef, &pullTime, TIME_WORD_POLL); Status = Phy_SetTimeWord(phyRef, &pullTime, TIME_WORD_POLL);
if(Status != SUCCESS){ if (Status != SUCCESS)
{
DBG_ERROR("Physical set pull time failed, with error code: %d\n", Status); DBG_ERROR("Physical set pull time failed, with error code: %d\n", Status);
} }
PHY_TOR_TYPE tor; PHY_TOR_TYPE tor;
switch(txrxMode) { switch (txrxMode)
{
case L2_TOR_SUSPEND: case L2_TOR_SUSPEND:
tor = PHY_TOR_SUSPEND; tor = PHY_TOR_SUSPEND;
break; break;
@@ -509,7 +552,8 @@ int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE f
} }
Status = Phy_SetupNextSlot(phyRef, tor, L2_L2frame2L1frame(frameType), instPtr->sendPktData); Status = Phy_SetupNextSlot(phyRef, tor, L2_L2frame2L1frame(frameType), instPtr->sendPktData);
if(Status != SUCCESS){ if (Status != SUCCESS)
{
DBG_ERROR("Physical setup failed, with error code: %d\n", Status); DBG_ERROR("Physical setup failed, with error code: %d\n", Status);
} }
return Status; return Status;
@@ -525,7 +569,8 @@ int32_t L2_SetupNextSlot(L2_INST* instPtr, L2_TOR_TYPE txrxMode, L2_FRAME_TYPE f
* L2_FRAME_NONE_LEN if the frame type code cannot be recognized. * L2_FRAME_NONE_LEN if the frame type code cannot be recognized.
* *
******************************************************************************/ ******************************************************************************/
uint32_t L2_GetFrameLen(L2_FRAME_TYPE l2FrameType) { uint32_t L2_GetFrameLen(L2_FRAME_TYPE l2FrameType)
{
return Phy_GetFrameLen(L2_L2frame2L1frame(l2FrameType)); return Phy_GetFrameLen(L2_L2frame2L1frame(l2FrameType));
} }
@@ -539,8 +584,10 @@ uint32_t L2_GetFrameLen(L2_FRAME_TYPE l2FrameType) {
* *
******************************************************************************/ ******************************************************************************/
#ifndef TEST_200KBPS #ifndef TEST_200KBPS
L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame) { L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame)
switch (l2Frame) { {
switch (l2Frame)
{
case L2_INTER_SCAN_FRAME: case L2_INTER_SCAN_FRAME:
return L1_2KBPS_20MS_FRAME; return L1_2KBPS_20MS_FRAME;
case L2_INTRA_SCAN_FRAME: case L2_INTRA_SCAN_FRAME:
@@ -571,8 +618,10 @@ L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame) {
} }
// FIXME: Only for test 1.2Mbps Frame // FIXME: Only for test 1.2Mbps Frame
#else #else
L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame) { L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame)
switch (l2Frame) { {
switch (l2Frame)
{
case L2_INTER_SCAN_FRAME: case L2_INTER_SCAN_FRAME:
return L1_2KBPS_20MS_FRAME; return L1_2KBPS_20MS_FRAME;
case L2_INTRA_SCAN_FRAME: case L2_INTRA_SCAN_FRAME:
@@ -613,20 +662,24 @@ L1_FRAME_TYPE L2_L2frame2L1frame(L2_FRAME_TYPE l2Frame) {
* Other error code. * Other error code.
* *
******************************************************************************/ ******************************************************************************/
int32_t Linklayer_Init(L2_INST** instPtr, const L2_CONFIG *config) { int32_t Linklayer_Init(L2_INST **instPtr, const L2_CONFIG *config)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(config != NULL); ASSERT(config != NULL);
int32_t Status; int32_t Status;
if (config->phyInstPtr == NULL) { if (config->phyInstPtr == NULL)
{
return EDINVAL; return EDINVAL;
} }
if (config->tmcInstPtr == NULL) { if (config->tmcInstPtr == NULL)
{
return EDINVAL; return EDINVAL;
} }
(*instPtr) = (L2_INST *)malloc(sizeof(L2_INST)); (*instPtr) = (L2_INST *)malloc(sizeof(L2_INST));
if (*instPtr == NULL) { if (*instPtr == NULL)
{
DBG_ERROR("Allocate memory for L2_INST failed.\n"); DBG_ERROR("Allocate memory for L2_INST failed.\n");
return EALLOC; return EALLOC;
} }
@@ -635,13 +688,15 @@ int32_t Linklayer_Init(L2_INST** instPtr, const L2_CONFIG *config) {
// Packet buffer // Packet buffer
(*instPtr)->sendPktData = calloc(L2_PACKET_LEN_MAX / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->sendPktData = calloc(L2_PACKET_LEN_MAX / sizeof(uint64_t), sizeof(uint64_t));
if((*instPtr)->sendPktData == NULL) { if ((*instPtr)->sendPktData == NULL)
{
DBG_ERROR("Allocate memory for sendPktData failed.\n"); DBG_ERROR("Allocate memory for sendPktData failed.\n");
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
(*instPtr)->recvPktData = calloc(L2_PACKET_LEN_MAX / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->recvPktData = calloc(L2_PACKET_LEN_MAX / sizeof(uint64_t), sizeof(uint64_t));
if((*instPtr)->recvPktData == NULL) { if ((*instPtr)->recvPktData == NULL)
{
DBG_ERROR("Allocate memory for recvPktData failed.\n"); DBG_ERROR("Allocate memory for recvPktData failed.\n");
Status = EALLOC; Status = EALLOC;
goto error; goto error;
@@ -649,25 +704,29 @@ int32_t Linklayer_Init(L2_INST** instPtr, const L2_CONFIG *config) {
// Mpc buff Init // Mpc buff Init
(*instPtr)->mgmtMsgRecvBuff0 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->mgmtMsgRecvBuff0 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t));
if((*instPtr)->mgmtMsgRecvBuff0 == NULL) { if ((*instPtr)->mgmtMsgRecvBuff0 == NULL)
{
DBG_ERROR("Allocate memory for mgmtMsgRecvBuff0 failed.\n"); DBG_ERROR("Allocate memory for mgmtMsgRecvBuff0 failed.\n");
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
(*instPtr)->svcMsgRecvBuff0 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->svcMsgRecvBuff0 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t));
if((*instPtr)->svcMsgRecvBuff0 == NULL) { if ((*instPtr)->svcMsgRecvBuff0 == NULL)
{
DBG_ERROR("Allocate memory for svcMsgRecvBuff0 failed.\n"); DBG_ERROR("Allocate memory for svcMsgRecvBuff0 failed.\n");
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
(*instPtr)->mgmtMsgRecvBuff1 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->mgmtMsgRecvBuff1 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t));
if((*instPtr)->mgmtMsgRecvBuff1 == NULL) { if ((*instPtr)->mgmtMsgRecvBuff1 == NULL)
{
DBG_ERROR("Allocate memory for mgmtMsgRecvBuff1 failed.\n"); DBG_ERROR("Allocate memory for mgmtMsgRecvBuff1 failed.\n");
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
(*instPtr)->svcMsgRecvBuff1 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->svcMsgRecvBuff1 = calloc(L2_MSG_STROAGE_BUFF_MAX / sizeof(uint64_t), sizeof(uint64_t));
if((*instPtr)->svcMsgRecvBuff1 == NULL) { if ((*instPtr)->svcMsgRecvBuff1 == NULL)
{
DBG_ERROR("Allocate memory for svcMsgRecvBuff1 failed.\n"); DBG_ERROR("Allocate memory for svcMsgRecvBuff1 failed.\n");
Status = EALLOC; Status = EALLOC;
goto error; goto error;
@@ -716,37 +775,43 @@ int32_t Linklayer_Init(L2_INST** instPtr, const L2_CONFIG *config) {
(*instPtr)->buffSwitch = 0; (*instPtr)->buffSwitch = 0;
Status = L2_CreateSchedEventPool(&((*instPtr)->schdEventPool)); Status = L2_CreateSchedEventPool(&((*instPtr)->schdEventPool));
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("L2_CreateSchedEventPool runs failed, with error code: %d.\n", Status); DBG_ERROR("L2_CreateSchedEventPool runs failed, with error code: %d.\n", Status);
goto error; goto error;
} }
Status = L2_NodeList_Init(&((*instPtr)->nodeList)); Status = L2_NodeList_Init(&((*instPtr)->nodeList));
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("NodeList_Init runs failed, with error code: %d.\n", Status); DBG_ERROR("NodeList_Init runs failed, with error code: %d.\n", Status);
goto error; goto error;
} }
Status = ScanNet_Init(&((*instPtr)->scanNetFSM)); Status = ScanNet_Init(&((*instPtr)->scanNetFSM));
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("ScanNet_Init runs failed, with error code: %d.\n", Status); DBG_ERROR("ScanNet_Init runs failed, with error code: %d.\n", Status);
goto error; goto error;
} }
Status = MgmtNet_Init(&((*instPtr)->mgmtNetFSM)); Status = MgmtNet_Init(&((*instPtr)->mgmtNetFSM));
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("MgmtNet_Init runs failed, with error code: %d.\n", Status); DBG_ERROR("MgmtNet_Init runs failed, with error code: %d.\n", Status);
goto error; goto error;
} }
Status = ServiceNet_Init(&((*instPtr)->serviceNetFSM)); Status = ServiceNet_Init(&((*instPtr)->serviceNetFSM));
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("ServiceNet_Init runs failed, with error code: %d.\n", Status); DBG_ERROR("ServiceNet_Init runs failed, with error code: %d.\n", Status);
goto error; goto error;
} }
Status = RTT_Init(&((*instPtr)->rttFSM)); Status = RTT_Init(&((*instPtr)->rttFSM));
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("RTT_Init runs failed, with error code: %d.\n", Status); DBG_ERROR("RTT_Init runs failed, with error code: %d.\n", Status);
goto error; goto error;
} }
@@ -756,14 +821,17 @@ error:
return Status; return Status;
} }
int32_t Linklayer_Reconfig(L2_INST* instPtr, const L2_CONFIG *config) { int32_t Linklayer_Reconfig(L2_INST *instPtr, const L2_CONFIG *config)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(config != NULL); ASSERT(config != NULL);
if (config->phyInstPtr == NULL) { if (config->phyInstPtr == NULL)
{
return EDINVAL; return EDINVAL;
} }
if (config->tmcInstPtr == NULL) { if (config->tmcInstPtr == NULL)
{
return EDINVAL; return EDINVAL;
} }
memcpy(&(instPtr->config), config, sizeof(L2_CONFIG)); memcpy(&(instPtr->config), config, sizeof(L2_CONFIG));
@@ -820,8 +888,8 @@ int32_t Linklayer_Reconfig(L2_INST* instPtr, const L2_CONFIG *config) {
return SUCCESS; return SUCCESS;
} }
void Linklayer_Destroy(L2_INST *instPtr)
void Linklayer_Destroy(L2_INST* instPtr){ {
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
free(instPtr->sendPktData); free(instPtr->sendPktData);
free(instPtr->recvPktData); free(instPtr->recvPktData);
@@ -839,15 +907,16 @@ void Linklayer_Destroy(L2_INST* instPtr){
free(instPtr); free(instPtr);
} }
int32_t Linklayer_Start(L2_INST *instPtr)
int32_t Linklayer_Start(L2_INST* instPtr){ {
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
instPtr->netStartTime = TimeCtrl_GetIntCnt(instPtr->config.tmcInstPtr) / L2_SLOTS_PER_FRAME + 1; instPtr->netStartTime = TimeCtrl_GetIntCnt(instPtr->config.tmcInstPtr) / L2_SLOTS_PER_FRAME + 1;
instPtr->isEnable = true; instPtr->isEnable = true;
return SUCCESS; return SUCCESS;
} }
int32_t Linklayer_Halt(L2_INST* instPtr){ int32_t Linklayer_Halt(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
instPtr->isEnable = false; instPtr->isEnable = false;
return SUCCESS; return SUCCESS;
@@ -864,44 +933,55 @@ int32_t Linklayer_Halt(L2_INST* instPtr){
* FAIL - Event cannot be recognized or error occurred while process. * FAIL - Event cannot be recognized or error occurred while process.
* *
******************************************************************************/ ******************************************************************************/
int32_t Linklayer_EventHandler(L2_INST* instPtr, const L2_EVENT* l2Event) { int32_t Linklayer_EventHandler(L2_INST *instPtr, const L2_EVENT *l2Event)
switch (l2Event->event) { {
case L2_EV_SET_FRAME_NUM: { switch (l2Event->event)
{
case L2_EV_SET_FRAME_NUM:
{
DBG_INFO("Event L2_EV_FRAME_NO_UPDATE asserted....\n"); DBG_INFO("Event L2_EV_FRAME_NO_UPDATE asserted....\n");
L2_SetFrameNum(instPtr, *((uint32_t *)l2Event->msg)); L2_SetFrameNum(instPtr, *((uint32_t *)l2Event->msg));
break; break;
} }
case L2_EV_SET_SLOT_NUM: { case L2_EV_SET_SLOT_NUM:
{
DBG_INFO("Event L2_EV_SLOT_NO_UPDATE asserted....\n"); DBG_INFO("Event L2_EV_SLOT_NO_UPDATE asserted....\n");
L2_SetSlotNum(instPtr, *((uint32_t *)l2Event->msg)); L2_SetSlotNum(instPtr, *((uint32_t *)l2Event->msg));
break; break;
} }
case L2_EV_MAIN_STATE_UPDATE: { case L2_EV_MAIN_STATE_UPDATE:
{
DBG_INFO("Event L2_EV_MAIN_STATE_UPDATE asserted....\n"); DBG_INFO("Event L2_EV_MAIN_STATE_UPDATE asserted....\n");
UpdateMainSate(instPtr, *((L2_NODE_STATE *)l2Event->msg)); UpdateMainSate(instPtr, *((L2_NODE_STATE *)l2Event->msg));
break; break;
} }
case L2_EV_SCAN_PROC_DONE: { case L2_EV_SCAN_PROC_DONE:
{
DBG_INFO("Event L2_EV_SCAN_PROC_DONE asserted....\n"); DBG_INFO("Event L2_EV_SCAN_PROC_DONE asserted....\n");
// TODO: Clear ACK // TODO: Clear ACK
break; break;
} }
case L2_EV_MGMT_PROC_DONE: { case L2_EV_MGMT_PROC_DONE:
{
DBG_INFO("Event L2_EV_MGMT_PROC_DONE asserted....\n"); DBG_INFO("Event L2_EV_MGMT_PROC_DONE asserted....\n");
break; break;
} }
case L2_EV_NET_SETUP_DONE: { case L2_EV_NET_SETUP_DONE:
{
DBG_INFO("Event L2_EV_NETSETUP_DONE asserted....\n"); DBG_INFO("Event L2_EV_NETSETUP_DONE asserted....\n");
if(L2_GetNodeType(instPtr) != L2_NODE_HEAD){ if (L2_GetNodeType(instPtr) != L2_NODE_HEAD)
{
instPtr->cxn2Net = true; instPtr->cxn2Net = true;
instPtr->netStartTime = *((uint32_t *)l2Event->msg); instPtr->netStartTime = *((uint32_t *)l2Event->msg);
} }
else{ // Head node else
{ // Head node
UpdateMainSate(instPtr, L2_NORMAL_STATE); UpdateMainSate(instPtr, L2_NORMAL_STATE);
} }
break; break;
} }
case L2_EV_TIME_SYNC: { case L2_EV_TIME_SYNC:
{
DBG_INFO("Event L2_EV_TIME_SYNC asserted....\n"); DBG_INFO("Event L2_EV_TIME_SYNC asserted....\n");
break; break;
} }
@@ -909,55 +989,66 @@ int32_t Linklayer_EventHandler(L2_INST* instPtr, const L2_EVENT* l2Event) {
DBG_INFO("Event L2_EV_RTT_RECV_UPDATE asserted....\n"); DBG_INFO("Event L2_EV_RTT_RECV_UPDATE asserted....\n");
RTT_FsmUpdate(instPtr, L2_TOR_RX); RTT_FsmUpdate(instPtr, L2_TOR_RX);
break; break;
case L2_EV_LINK_SET_REQ: { case L2_EV_LINK_SET_REQ:
{
DBG_INFO("Event L2_EV_LINK_SET_REQ asserted....\n"); DBG_INFO("Event L2_EV_LINK_SET_REQ asserted....\n");
break; break;
} }
case L2_EV_LOST_CXN: { case L2_EV_LOST_CXN:
{
DBG_INFO("Event L2_EV_LOST_CXN asserted....\n"); DBG_INFO("Event L2_EV_LOST_CXN asserted....\n");
DBG_WARNING("Lost connection with head node [%d].\n", L2_GetHeadId(instPtr)); DBG_WARNING("Lost connection with head node [%d].\n", L2_GetHeadId(instPtr));
instPtr->cxn2Net = false; instPtr->cxn2Net = false;
break; break;
} }
case L2_EV_LOST_NODE: { case L2_EV_LOST_NODE:
{
DBG_INFO("Event L2_EV_LOST_NODE asserted....\n"); DBG_INFO("Event L2_EV_LOST_NODE asserted....\n");
break; break;
} }
case L2_ROUTINE_FINISH: { case L2_ROUTINE_FINISH:
{
DBG_INFO("Event L2_ROUTINE_FINISH asserted....\n"); DBG_INFO("Event L2_ROUTINE_FINISH asserted....\n");
if(instPtr->nextState == L2_INIT_STATE){ if (instPtr->nextState == L2_INIT_STATE)
{
// Reset scan net FMS for next scan process. // Reset scan net FMS for next scan process.
ScanNet_Reset(instPtr->scanNetFSM); ScanNet_Reset(instPtr->scanNetFSM);
UpdateMainSate(instPtr, L2_NORMAL_STATE); UpdateMainSate(instPtr, L2_NORMAL_STATE);
instPtr->rtneConfig.rtneTable = normalTable; instPtr->rtneConfig.rtneTable = normalTable;
instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(normalTable); instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(normalTable);
} }
else if(instPtr->nextState == L2_NORMAL_STATE){ else if (instPtr->nextState == L2_NORMAL_STATE)
{
instPtr->rtneConfig.rtneTable = normalTable; instPtr->rtneConfig.rtneTable = normalTable;
instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(normalTable); instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(normalTable);
} }
else if(instPtr->nextState == L2_SEEK_STATE){ else if (instPtr->nextState == L2_SEEK_STATE)
{
instPtr->rtneConfig.rtneTable = seekTable; instPtr->rtneConfig.rtneTable = seekTable;
instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(seekTable); instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(seekTable);
} }
else if(instPtr->nextState == L2_INTRA_SEEK_STATE){ else if (instPtr->nextState == L2_INTRA_SEEK_STATE)
{
instPtr->rtneConfig.rtneTable = intraSeekTable; instPtr->rtneConfig.rtneTable = intraSeekTable;
instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(intraSeekTable); instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(intraSeekTable);
} }
else if(instPtr->nextState == L2_INTER_SEEK_STATE){ else if (instPtr->nextState == L2_INTER_SEEK_STATE)
{
instPtr->rtneConfig.rtneTable = interSeekTable; instPtr->rtneConfig.rtneTable = interSeekTable;
instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(interSeekTable); instPtr->rtneConfig.rtnePrd = ARRAY_SIZE(interSeekTable);
} }
break; break;
} }
case L2_ROUTINE_UPDATE: { case L2_ROUTINE_UPDATE:
{
DBG_INFO("Event L2_ROUTINE_UPDATE asserted....\n"); DBG_INFO("Event L2_ROUTINE_UPDATE asserted....\n");
L2_RTNE_CONFIG *newRtneConfig = (L2_RTNE_CONFIG *)l2Event->msg; L2_RTNE_CONFIG *newRtneConfig = (L2_RTNE_CONFIG *)l2Event->msg;
instPtr->rtneConfig.rtneTable = newRtneConfig->rtneTable; instPtr->rtneConfig.rtneTable = newRtneConfig->rtneTable;
instPtr->rtneConfig.rtnePrd = newRtneConfig->rtnePrd; instPtr->rtneConfig.rtnePrd = newRtneConfig->rtnePrd;
break; break;
} }
default: { default:
{
DBG_WARNING("UNKNOWN Event %d asserted.\n", l2Event->event); DBG_WARNING("UNKNOWN Event %d asserted.\n", l2Event->event);
break; break;
} }
@@ -977,15 +1068,18 @@ int32_t Linklayer_EventHandler(L2_INST* instPtr, const L2_EVENT* l2Event) {
******************************************************************************/ ******************************************************************************/
int mgmtFrameRecv = 0; int mgmtFrameRecv = 0;
int svcFrameRecv = 0; int svcFrameRecv = 0;
int32_t Linklayer_PktHandler(L2_INST* instPtr, L2_FRAME_TYPE frameType) { int32_t Linklayer_PktHandler(L2_INST *instPtr, L2_FRAME_TYPE frameType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status = SUCCESS; int32_t Status = SUCCESS;
if (instPtr->dataAvail == false) { if (instPtr->dataAvail == false)
{
return FAILURE; return FAILURE;
} }
switch (frameType) { switch (frameType)
{
case L2_INTER_SCAN_FRAME: case L2_INTER_SCAN_FRAME:
case L2_INTRA_SCAN_FRAME: case L2_INTRA_SCAN_FRAME:
case L2_INTER_BM_FRAME: case L2_INTER_BM_FRAME:
@@ -998,13 +1092,16 @@ int32_t Linklayer_PktHandler(L2_INST* instPtr, L2_FRAME_TYPE frameType) {
case L2_INTRA_MGMT_FRAME: case L2_INTRA_MGMT_FRAME:
case L2_INTER_TSYNC_FRAME: case L2_INTER_TSYNC_FRAME:
case L2_INTRA_TSYNC_FRAME: case L2_INTRA_TSYNC_FRAME:
if(frameType == L2_INTRA_MGMT_FRAME){ if (frameType == L2_INTRA_MGMT_FRAME)
if (instPtr->buffSwitch == 0) { {
if (instPtr->buffSwitch == 0)
{
memcpy(instPtr->mgmtMsgRecvBuff0 + (size_t)(instPtr->mgmtMsgRecvCnt0 * (MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8)), memcpy(instPtr->mgmtMsgRecvBuff0 + (size_t)(instPtr->mgmtMsgRecvCnt0 * (MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8)),
instPtr->recvPktData, (size_t)(MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8)); instPtr->recvPktData, (size_t)(MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8));
instPtr->mgmtMsgRecvCnt0++; instPtr->mgmtMsgRecvCnt0++;
} }
else { else
{
memcpy(instPtr->mgmtMsgRecvBuff1 + (size_t)(instPtr->mgmtMsgRecvCnt1 * (MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8)), memcpy(instPtr->mgmtMsgRecvBuff1 + (size_t)(instPtr->mgmtMsgRecvCnt1 * (MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8)),
instPtr->recvPktData, (size_t)(MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8)); instPtr->recvPktData, (size_t)(MGMT_MSG_LEN * ARRAY_SIZE(intraMgmtFrameMsgs) / 8));
instPtr->mgmtMsgRecvCnt1++; instPtr->mgmtMsgRecvCnt1++;
@@ -1015,13 +1112,16 @@ int32_t Linklayer_PktHandler(L2_INST* instPtr, L2_FRAME_TYPE frameType) {
break; break;
case L2_INTER_SVC_FRAME: case L2_INTER_SVC_FRAME:
case L2_INTRA_SVC_FRAME: case L2_INTRA_SVC_FRAME:
if(frameType == L2_INTRA_SVC_FRAME){ if (frameType == L2_INTRA_SVC_FRAME)
if (instPtr->buffSwitch == 0) { {
if (instPtr->buffSwitch == 0)
{
memcpy(instPtr->svcMsgRecvBuff0 + (size_t)(instPtr->svcMsgRecvCnt0 * (INTRA_SVC_FRAME_LENGTH_MAX)), memcpy(instPtr->svcMsgRecvBuff0 + (size_t)(instPtr->svcMsgRecvCnt0 * (INTRA_SVC_FRAME_LENGTH_MAX)),
instPtr->recvPktData, (size_t)(INTRA_SVC_FRAME_LENGTH_MAX)); instPtr->recvPktData, (size_t)(INTRA_SVC_FRAME_LENGTH_MAX));
instPtr->svcMsgRecvCnt0++; instPtr->svcMsgRecvCnt0++;
} }
else { else
{
memcpy(instPtr->svcMsgRecvBuff1 + (size_t)(instPtr->svcMsgRecvCnt1 * (INTRA_SVC_FRAME_LENGTH_MAX)), memcpy(instPtr->svcMsgRecvBuff1 + (size_t)(instPtr->svcMsgRecvCnt1 * (INTRA_SVC_FRAME_LENGTH_MAX)),
instPtr->recvPktData, (size_t)(INTRA_SVC_FRAME_LENGTH_MAX)); instPtr->recvPktData, (size_t)(INTRA_SVC_FRAME_LENGTH_MAX));
instPtr->svcMsgRecvCnt1++; instPtr->svcMsgRecvCnt1++;
@@ -1048,14 +1148,16 @@ int32_t Linklayer_PktHandler(L2_INST* instPtr, L2_FRAME_TYPE frameType) {
* Other error code. * Other error code.
* *
******************************************************************************/ ******************************************************************************/
static int32_t Linklayer_Routine (L2_INST* instPtr) { static int32_t Linklayer_Routine(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status; int32_t Status;
uint32_t tableIndx, entryIndx; uint32_t tableIndx, entryIndx;
// FIXME: Consider time wrapping. // FIXME: Consider time wrapping.
uint32_t nextTimeUnit = (TimeCtrl_GetIntCnt(instPtr->config.tmcInstPtr) + 1) / L2_SLOTS_PER_FRAME; uint32_t nextTimeUnit = (TimeCtrl_GetIntCnt(instPtr->config.tmcInstPtr) + 1) / L2_SLOTS_PER_FRAME;
if(nextTimeUnit < instPtr->netStartTime) { if (nextTimeUnit < instPtr->netStartTime)
{
DBG_WARNING("Linklayer not start yet, next time unit: %u, start time unit: %u.\n", DBG_WARNING("Linklayer not start yet, next time unit: %u, start time unit: %u.\n",
nextTimeUnit, instPtr->netStartTime); nextTimeUnit, instPtr->netStartTime);
return EIGNORE; return EIGNORE;
@@ -1073,7 +1175,8 @@ static int32_t Linklayer_Routine (L2_INST* instPtr) {
rtneEntry = instPtr->rtneConfig.rtneTable[tableIndx][entryIndx]; rtneEntry = instPtr->rtneConfig.rtneTable[tableIndx][entryIndx];
DBG_INFO("## Routine: %u-%u, Routine period: %u.\n", tableIndx, entryIndx, instPtr->rtneConfig.rtnePrd); DBG_INFO("## Routine: %u-%u, Routine period: %u.\n", tableIndx, entryIndx, instPtr->rtneConfig.rtnePrd);
switch (rtneEntry.slotType) { switch (rtneEntry.slotType)
{
case L2_SLOT_IDLE: case L2_SLOT_IDLE:
// Skip a 5ms slot. // Skip a 5ms slot.
Status = SUCCESS; Status = SUCCESS;
@@ -1103,7 +1206,8 @@ static int32_t Linklayer_Routine (L2_INST* instPtr) {
} }
// check if routine is finished. // check if routine is finished.
if(entryIndx >= L2_SLOTS_PER_FRAME - 1 && tableIndx >= instPtr->rtneConfig.rtnePrd - 1){ if (entryIndx >= L2_SLOTS_PER_FRAME - 1 && tableIndx >= instPtr->rtneConfig.rtnePrd - 1)
{
L2_EVENT rtneFinishEvent = {L2_ROUTINE_FINISH, &(instPtr->rtneConfig)}; L2_EVENT rtneFinishEvent = {L2_ROUTINE_FINISH, &(instPtr->rtneConfig)};
AssertL2Event(instPtr, &rtneFinishEvent); AssertL2Event(instPtr, &rtneFinishEvent);
} }
@@ -1129,11 +1233,13 @@ static int32_t Linklayer_Routine (L2_INST* instPtr) {
* Other error code. * Other error code.
* *
******************************************************************************/ ******************************************************************************/
int32_t Linklayer_Proc(L2_INST* instPtr) { int32_t Linklayer_Proc(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status; int32_t Status;
if(instPtr->isEnable == false) { if (instPtr->isEnable == false)
{
DBG_INFO("######## Link Layer is not Enabled ########\n"); DBG_INFO("######## Link Layer is not Enabled ########\n");
return SUCCESS; return SUCCESS;
} }
@@ -1148,27 +1254,33 @@ int32_t Linklayer_Proc(L2_INST* instPtr) {
L2_FrameStateUpdate(instPtr); L2_FrameStateUpdate(instPtr);
// Handle frame received in last slot. // Handle frame received in last slot.
if(L2_isDataAvail(instPtr) == true){ if (L2_isDataAvail(instPtr) == true)
{
Linklayer_PktHandler(instPtr, L2_GetLastFrameType(instPtr)); Linklayer_PktHandler(instPtr, L2_GetLastFrameType(instPtr));
} }
// Run Link layer routine, setup next slot procedure. // Run Link layer routine, setup next slot procedure.
if (L2_GetNodeType(instPtr) == L2_NODE_HEAD) { if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
{
Phy_SetMode(instPtr->config.phyInstPtr, PHY_MODE_PULL); Phy_SetMode(instPtr->config.phyInstPtr, PHY_MODE_PULL);
Status = Linklayer_Routine(instPtr); Status = Linklayer_Routine(instPtr);
} }
else { //L2_NODE_MEMBER else
if (L2_isCxn2Net(instPtr)) { { // L2_NODE_MEMBER
if (L2_isCxn2Net(instPtr))
{
Phy_SetMode(instPtr->config.phyInstPtr, PHY_MODE_PULL); Phy_SetMode(instPtr->config.phyInstPtr, PHY_MODE_PULL);
Status = Linklayer_Routine(instPtr); Status = Linklayer_Routine(instPtr);
} }
else { else
{
Phy_SetMode(instPtr->config.phyInstPtr, PHY_MODE_LISTEN); Phy_SetMode(instPtr->config.phyInstPtr, PHY_MODE_LISTEN);
Status = ScanNet_Proc(instPtr); Status = ScanNet_Proc(instPtr);
} }
} }
// Check if previous runs has error occurred. // Check if previous runs has error occurred.
if (Status != SUCCESS) { if (Status != SUCCESS)
{
return Status; return Status;
} }
@@ -1179,14 +1291,17 @@ int32_t Linklayer_Proc(L2_INST* instPtr) {
instPtr->mainState = instPtr->nextState; instPtr->mainState = instPtr->nextState;
// Upload data per 500ms // Upload data per 500ms
if (instPtr->slotNum.next % DATA_STORE_INTERVAL == 0) { if (instPtr->slotNum.next % DATA_STORE_INTERVAL == 0)
{
instPtr->buffSwitch = (instPtr->buffSwitch == 0) ? 1 : 0; instPtr->buffSwitch = (instPtr->buffSwitch == 0) ? 1 : 0;
if(instPtr->buffSwitch == 0){ if (instPtr->buffSwitch == 0)
{
instPtr->mgmtMsgRecvCnt0 = 0; instPtr->mgmtMsgRecvCnt0 = 0;
instPtr->svcMsgRecvCnt0 = 0; instPtr->svcMsgRecvCnt0 = 0;
} }
else{ else
{
instPtr->mgmtMsgRecvCnt1 = 0; instPtr->mgmtMsgRecvCnt1 = 0;
instPtr->svcMsgRecvCnt1 = 0; instPtr->svcMsgRecvCnt1 = 0;
} }
+6 -5
View File
@@ -43,7 +43,8 @@
#define AssertL2Event(instPtr, event) Linklayer_EventHandler(instPtr, event) #define AssertL2Event(instPtr, event) Linklayer_EventHandler(instPtr, event)
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef struct { typedef struct
{
L2_NODE_TYPE myType; L2_NODE_TYPE myType;
bool isInterHead; bool isInterHead;
uint8_t clustId; uint8_t clustId;
@@ -55,7 +56,8 @@ typedef struct {
TIMECTRL_INST *tmcInstPtr; TIMECTRL_INST *tmcInstPtr;
} L2_CONFIG; } L2_CONFIG;
typedef struct { typedef struct
{
uint32_t frameRx; uint32_t frameRx;
uint32_t frameTx; uint32_t frameTx;
uint32_t frameRecv; uint32_t frameRecv;
@@ -87,7 +89,8 @@ typedef struct {
uint32_t svcCorrectCnt; uint32_t svcCorrectCnt;
} L2_LOG; } L2_LOG;
typedef struct { typedef struct
{
L2_CONFIG config; L2_CONFIG config;
L2_NODE_INFO myInfo; L2_NODE_INFO myInfo;
NODE_LIST *nodeList; NODE_LIST *nodeList;
@@ -171,7 +174,6 @@ void MgmtNet_Destroy(L2_MGMT_FSM* mgmtFSM);
int32_t MgmtNet_PktHandler(L2_INST *instPtr); int32_t MgmtNet_PktHandler(L2_INST *instPtr);
int32_t MgmtNet_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry); int32_t MgmtNet_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry);
// Service net // Service net
int32_t ServiceNet_Init(L2_SVC_FSM **serviceFSM); int32_t ServiceNet_Init(L2_SVC_FSM **serviceFSM);
void ServiceNet_Reset(L2_SVC_FSM *serviceFSM); void ServiceNet_Reset(L2_SVC_FSM *serviceFSM);
@@ -212,5 +214,4 @@ void RTT_Reset(L2_RTT_FSM* rttFsmPtr);
void RTT_Destroy(L2_RTT_FSM *rttFsmPtr); void RTT_Destroy(L2_RTT_FSM *rttFsmPtr);
int32_t RTT_FsmUpdate(L2_INST *instPtr, L2_TOR_TYPE torType); int32_t RTT_FsmUpdate(L2_INST *instPtr, L2_TOR_TYPE torType);
#endif /* LINKLAYER814_H_ */ #endif /* LINKLAYER814_H_ */
@@ -53,7 +53,8 @@
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef struct { typedef struct
{
uint32_t last; uint32_t last;
uint32_t curr; uint32_t curr;
uint32_t next; uint32_t next;
@@ -61,7 +62,8 @@ typedef struct {
/** @name Tx/Rx mode code /** @name Tx/Rx mode code
*/ */
typedef enum { typedef enum
{
L2_TOR_SUSPEND = 0, L2_TOR_SUSPEND = 0,
L2_TOR_TX = 1, L2_TOR_TX = 1,
L2_TOR_RX = 2 L2_TOR_RX = 2
@@ -69,7 +71,8 @@ typedef enum {
/** @name Prior information type definition /** @name Prior information type definition
*/ */
typedef enum { typedef enum
{
L2_NODE_NONE = 0, L2_NODE_NONE = 0,
L2_NODE_HEAD = 1, L2_NODE_HEAD = 1,
L2_NODE_MEMBER = 2, L2_NODE_MEMBER = 2,
@@ -78,7 +81,8 @@ typedef enum {
/** @name Node state type definition /** @name Node state type definition
*/ */
typedef enum { typedef enum
{
L2_NONE_STATE = 0, L2_NONE_STATE = 0,
L2_INIT_STATE = 1, L2_INIT_STATE = 1,
L2_NORMAL_STATE = 2, L2_NORMAL_STATE = 2,
@@ -87,7 +91,8 @@ typedef enum {
L2_SEEK_STATE = 5 L2_SEEK_STATE = 5
} L2_NODE_STATE; } L2_NODE_STATE;
typedef struct { typedef struct
{
uint8_t nodeId; uint8_t nodeId;
uint8_t clustId; uint8_t clustId;
uint8_t headId; uint8_t headId;
@@ -100,7 +105,8 @@ typedef struct {
/** @name L2 event enum /** @name L2 event enum
* A enum of L2 event types. * A enum of L2 event types.
*/ */
typedef enum { typedef enum
{
L2_EV_NONE = 0, L2_EV_NONE = 0,
L2_EV_SCAN_PROC_DONE = 1, L2_EV_SCAN_PROC_DONE = 1,
L2_EV_MGMT_PROC_DONE = 2, L2_EV_MGMT_PROC_DONE = 2,
@@ -120,7 +126,8 @@ typedef enum {
/** @name L2 event /** @name L2 event
* A definition of L2 event instance. * A definition of L2 event instance.
*/ */
typedef struct { typedef struct
{
L2_EVENT_TYPE event; L2_EVENT_TYPE event;
void *msg; void *msg;
} L2_EVENT; } L2_EVENT;
@@ -128,7 +135,8 @@ typedef struct {
/** @name Slot types /** @name Slot types
* A definition of different slots. * A definition of different slots.
*/ */
typedef enum { typedef enum
{
L2_SLOT_NONE = 0, L2_SLOT_NONE = 0,
L2_SLOT_IDLE = 1, L2_SLOT_IDLE = 1,
L2_SLOT_INTER_SCAN_ACQ = 2, L2_SLOT_INTER_SCAN_ACQ = 2,
@@ -148,7 +156,8 @@ typedef enum {
/** @name Frame types /** @name Frame types
* A definition of different frame types. * A definition of different frame types.
*/ */
typedef enum { typedef enum
{
L2_CRC_NONE = 0, L2_CRC_NONE = 0,
L2_CRC16_1021 = 1, L2_CRC16_1021 = 1,
L2_CRC16_8005 = 2, L2_CRC16_8005 = 2,
@@ -158,7 +167,8 @@ typedef enum {
/** @name Frame types /** @name Frame types
* A definition of different frame types. * A definition of different frame types.
*/ */
typedef enum { typedef enum
{
L2_NONE_FRAME = 0, L2_NONE_FRAME = 0,
L2_INTER_SCAN_FRAME = 1, L2_INTER_SCAN_FRAME = 1,
L2_INTRA_SCAN_FRAME = 2, L2_INTRA_SCAN_FRAME = 2,
@@ -177,7 +187,8 @@ typedef enum {
/** @name Schedule subtable entry /** @name Schedule subtable entry
* A definition of Schedule subtable entry. * A definition of Schedule subtable entry.
*/ */
typedef struct { typedef struct
{
L2_SLOT_TYPE slotType : 8; L2_SLOT_TYPE slotType : 8;
uint8_t slotSeq; uint8_t slotSeq;
uint8_t src0; uint8_t src0;
@@ -187,7 +198,8 @@ typedef struct {
} L2_RTNE_TABLE_ENTRY; } L2_RTNE_TABLE_ENTRY;
// Scan net // Scan net
typedef struct { typedef struct
{
uint8_t NodeId; uint8_t NodeId;
uint8_t ClusterId; uint8_t ClusterId;
uint32_t frameNum; uint32_t frameNum;
@@ -200,7 +212,8 @@ typedef struct {
uint32_t Latitude; uint32_t Latitude;
} L2_BM_LOG; } L2_BM_LOG;
typedef struct { typedef struct
{
uint8_t NodeId; uint8_t NodeId;
uint8_t ClusterId; uint8_t ClusterId;
uint16_t EastSpeed; uint16_t EastSpeed;
@@ -213,20 +226,23 @@ typedef struct {
uint32_t NetScanTime; uint32_t NetScanTime;
} L2_W2ACQ_LOG; } L2_W2ACQ_LOG;
typedef enum { typedef enum
{
L2_SCAN_NONE_STAGE = 0, L2_SCAN_NONE_STAGE = 0,
L2_SCAN_INIT_STAGE = 1, L2_SCAN_INIT_STAGE = 1,
L2_SCAN_BMADVT_STAGE = 2, // Benchmark advertisement L2_SCAN_BMADVT_STAGE = 2, // Benchmark advertisement
L2_SCANT_2WAYACQ_STAGE = 3 L2_SCANT_2WAYACQ_STAGE = 3
} SCAN_STAGE; } SCAN_STAGE;
typedef enum { typedef enum
{
L2_SCAN_NONE_MODE = 0, L2_SCAN_NONE_MODE = 0,
L2_SCAN_INTRA_MODE = 1, L2_SCAN_INTRA_MODE = 1,
L2_SCAN_INTER_MODE = 2 L2_SCAN_INTER_MODE = 2
} L2_SCAN_MODE; } L2_SCAN_MODE;
typedef struct { typedef struct
{
bool isLocked; // init acq done. bool isLocked; // init acq done.
bool isBmRcved; // bm received. bool isBmRcved; // bm received.
bool isAcked; // 2way acq done. bool isAcked; // 2way acq done.
@@ -240,16 +256,17 @@ typedef struct {
} L2_SCAN_FSM; } L2_SCAN_FSM;
// Service Net // Service Net
typedef struct { typedef struct
{
bool isBisar; bool isBisar;
bool isBisarEven; // Send frame at even slot at bisar mode. bool isBisarEven; // Send frame at even slot at bisar mode.
uint8_t bisarDst; uint8_t bisarDst;
uint32_t bisarEndframe; uint32_t bisarEndframe;
} L2_SVC_FSM; } L2_SVC_FSM;
// Management net // Management net
typedef struct { typedef struct
{
uint8_t wordTag; uint8_t wordTag;
uint8_t msgTag; uint8_t msgTag;
uint8_t msgSubTag; uint8_t msgSubTag;
@@ -266,7 +283,8 @@ typedef struct {
bool isValid; bool isValid;
} L2_MGMT_MSG_RECORD; } L2_MGMT_MSG_RECORD;
typedef struct { typedef struct
{
uint16_t wordTag; uint16_t wordTag;
uint16_t msgTag; uint16_t msgTag;
uint16_t msgSubTag; uint16_t msgSubTag;
@@ -278,7 +296,8 @@ typedef struct {
int32_t doppler; int32_t doppler;
} L2_RTT_MSG_RECORD; } L2_RTT_MSG_RECORD;
typedef struct { typedef struct
{
bool isBisarProc; bool isBisarProc;
bool isPendTimeAlloc; bool isPendTimeAlloc;
bool isPendSlotAlloc; bool isPendSlotAlloc;
@@ -287,7 +306,8 @@ typedef struct {
L2_RTT_MSG_RECORD rttMsgReqLog; L2_RTT_MSG_RECORD rttMsgReqLog;
} L2_MGMT_FSM; } L2_MGMT_FSM;
typedef struct{ typedef struct
{
L2_RTNE_TABLE_ENTRY **rtneTable; L2_RTNE_TABLE_ENTRY **rtneTable;
uint32_t rtnePrd; uint32_t rtnePrd;
L2_RTNE_TABLE_ENTRY *last; L2_RTNE_TABLE_ENTRY *last;
File diff suppressed because it is too large Load Diff
+4 -5
View File
@@ -290,7 +290,6 @@
#define TK_MSG_CONT_3_UPPER_ACC_LEN 16 #define TK_MSG_CONT_3_UPPER_ACC_LEN 16
#define TK_MSG_CONT_3_UPPER_ACC_OFFSET 56 #define TK_MSG_CONT_3_UPPER_ACC_OFFSET 56
// ** 94 Time Announcements Initial ** // // ** 94 Time Announcements Initial ** //
#define TA_MSG_SER_TAG_LEN 2 #define TA_MSG_SER_TAG_LEN 2
#define TA_MSG_SER_TAG_OFFSET 0 #define TA_MSG_SER_TAG_OFFSET 0
@@ -339,7 +338,6 @@
#define TA_MSG_CONT_1_RESV1_LEN 10 #define TA_MSG_CONT_1_RESV1_LEN 10
#define TA_MSG_CONT_1_RESV1_OFFSET 62 #define TA_MSG_CONT_1_RESV1_OFFSET 62
// ** 97 Time Announcements Continue 2 ** // // ** 97 Time Announcements Continue 2 ** //
#define TA_MSG_CONT_2_RESV0_LEN 2 #define TA_MSG_CONT_2_RESV0_LEN 2
#define TA_MSG_CONT_2_RESV0_OFFSET 0 #define TA_MSG_CONT_2_RESV0_OFFSET 0
@@ -443,7 +441,6 @@
#define RTT_REQ_CONT_RESV1_LEN 40 #define RTT_REQ_CONT_RESV1_LEN 40
#define RTT_REQ_CONT_RESV1_OFFSET 32 #define RTT_REQ_CONT_RESV1_OFFSET 32
// ** 101 RTT Response Initial Word ** // // ** 101 RTT Response Initial Word ** //
#define RTT_RSP_SER_TAG_LEN 2 #define RTT_RSP_SER_TAG_LEN 2
#define RTT_RSP_SER_TAG_OFFSET 0 #define RTT_RSP_SER_TAG_OFFSET 0
@@ -546,7 +543,8 @@
#define BCU_MSG_RESV_LEN 19 #define BCU_MSG_RESV_LEN 19
#define BCU_MSG_RESV_OFFSET 53 #define BCU_MSG_RESV_OFFSET 53
typedef enum{ typedef enum
{
L2_MSG_NONE = 0, L2_MSG_NONE = 0,
L2_MSG_BM = 1, L2_MSG_BM = 1,
L2_MSG_SCAN_INIT = 2, L2_MSG_SCAN_INIT = 2,
@@ -579,7 +577,8 @@ typedef enum{
L2_MSG_LINK_CONT_1 = 29 L2_MSG_LINK_CONT_1 = 29
} L2_MSG_TYPE; } L2_MSG_TYPE;
typedef enum{ typedef enum
{
L2_PDU_HEADER_NONE = 0, L2_PDU_HEADER_NONE = 0,
L2_PDU_HEADER_0 = 1, L2_PDU_HEADER_0 = 1,
L2_PDU_HEADER_1 = 2 L2_PDU_HEADER_1 = 2
+156 -93
View File
@@ -37,7 +37,8 @@
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef enum { typedef enum
{
MGMTPKT_NONE = 0, MGMTPKT_NONE = 0,
MGMTPKT_INTER_MGMT = 1, MGMTPKT_INTER_MGMT = 1,
MGMTPKT_INTRA_MGMT = 2, MGMTPKT_INTRA_MGMT = 2,
@@ -63,25 +64,32 @@ static int32_t MgmtPkt_Packing(L2_INST* instPtr, MGMTPKT_TYPE pktType);
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int32_t MgmtMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE msgType) { static int32_t MgmtMsg_Packing(L2_INST *instPtr, BIT_ARRAY *bitarr, L2_MSG_TYPE msgType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
uint64_t dataChunk; uint64_t dataChunk;
L2_RTT_FSM *rttFsmPtr = L2_GetRttFsm(instPtr); L2_RTT_FSM *rttFsmPtr = L2_GetRttFsm(instPtr);
if (msgType < L2_MSG_TK_INIT || msgType > L2_MSG_BCU_INIT) { if (msgType < L2_MSG_TK_INIT || msgType > L2_MSG_BCU_INIT)
{
return EPARAM; return EPARAM;
} }
if(msgType >= L2_MSG_TK_INIT && msgType <= L2_MSG_MADJ_EXTD) { if (msgType >= L2_MSG_TK_INIT && msgType <= L2_MSG_MADJ_EXTD)
if (bitarr->num_of_bits < MGMT_MSG_LEN) { {
if (bitarr->num_of_bits < MGMT_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
} }
if(msgType >= L2_MSG_RTT_REQ_INIT && msgType <= L2_MSG_RTT_RSP_CONT) { if (msgType >= L2_MSG_RTT_REQ_INIT && msgType <= L2_MSG_RTT_RSP_CONT)
if (bitarr->num_of_bits < RTT_MSG_LEN) { {
if (bitarr->num_of_bits < RTT_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
} }
switch (msgType) { switch (msgType)
{
case L2_MSG_TK_INIT: case L2_MSG_TK_INIT:
// Series tag // Series tag
dataChunk = 0; dataChunk = 0;
@@ -104,8 +112,10 @@ static int32_t MgmtMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
FrameInsertChunk(bitarr, TK_MSG_INI_OFFSET, TK_MSG_INI_LEN, dataChunk); FrameInsertChunk(bitarr, TK_MSG_INI_OFFSET, TK_MSG_INI_LEN, dataChunk);
// Next net stage // Next net stage
if (L2_GetNodeType(instPtr) == L2_NODE_HEAD) { if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
switch (instPtr->nextState) { {
switch (instPtr->nextState)
{
case L2_INIT_STATE: case L2_INIT_STATE:
dataChunk = NET_INI_STAGE; dataChunk = NET_INI_STAGE;
break; break;
@@ -430,7 +440,6 @@ static int32_t MgmtMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Packing management message to a bit array. * @brief Packing management message to a bit array.
@@ -442,64 +451,79 @@ static int32_t MgmtMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
* error code * error code
* *
******************************************************************************/ ******************************************************************************/
static int32_t MgmtPkt_Packing(L2_INST* instPtr, MGMTPKT_TYPE pktType) { static int32_t MgmtPkt_Packing(L2_INST *instPtr, MGMTPKT_TYPE pktType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint8_t *buff = instPtr->sendPktData; uint8_t *buff = instPtr->sendPktData;
uint32_t filedSize = 0; uint32_t filedSize = 0;
BIT_ARRAY *mgmtPkt = bit_array_create(L2_MSG_MAX); BIT_ARRAY *mgmtPkt = bit_array_create(L2_MSG_MAX);
if (mgmtPkt == NULL) { if (mgmtPkt == NULL)
{
DBG_ERROR("Failed to allocate memory for scanMsg.\n"); DBG_ERROR("Failed to allocate memory for scanMsg.\n");
return FAILURE; return FAILURE;
} }
switch (pktType) { switch (pktType)
{
case MGMTPKT_INTRA_MGMT: case MGMTPKT_INTRA_MGMT:
for (int i = 0; i < ARRAY_SIZE(intraMgmtFrameMsgs); i++) { for (int i = 0; i < ARRAY_SIZE(intraMgmtFrameMsgs); i++)
if (MgmtMsg_Packing(instPtr, mgmtPkt, intraMgmtFrameMsgs[i]) == SUCCESS) { {
if (MgmtMsg_Packing(instPtr, mgmtPkt, intraMgmtFrameMsgs[i]) == SUCCESS)
{
memcpy((uint8_t *)buff + (size_t)(i * (MGMT_MSG_LEN / 8)), memcpy((uint8_t *)buff + (size_t)(i * (MGMT_MSG_LEN / 8)),
mgmtPkt->words, (size_t)(MGMT_MSG_LEN / 8)); mgmtPkt->words, (size_t)(MGMT_MSG_LEN / 8));
} }
else { else
{
DBG_ERROR("Failed to packing intraMgmtFrameMsgs[%d].\n", i); DBG_ERROR("Failed to packing intraMgmtFrameMsgs[%d].\n", i);
goto failed; goto failed;
} }
filedSize += MGMT_MSG_LEN; filedSize += MGMT_MSG_LEN;
if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message is too long to be properly packed.\n"); DBG_ERROR("Message is too long to be properly packed.\n");
goto failed; goto failed;
} }
} }
break; break;
case MGMTPKT_INTRA_RTTREQ: case MGMTPKT_INTRA_RTTREQ:
for(int i = 0; i < ARRAY_SIZE(rttReqFrameMsgs); i++) { for (int i = 0; i < ARRAY_SIZE(rttReqFrameMsgs); i++)
if (MgmtMsg_Packing(instPtr, mgmtPkt, rttReqFrameMsgs[i]) == SUCCESS) { {
if (MgmtMsg_Packing(instPtr, mgmtPkt, rttReqFrameMsgs[i]) == SUCCESS)
{
memcpy(buff + (size_t)(i * (MGMT_MSG_LEN / 8)), memcpy(buff + (size_t)(i * (MGMT_MSG_LEN / 8)),
mgmtPkt->words, mgmtPkt->words,
(size_t)(MGMT_MSG_LEN / 8)); (size_t)(MGMT_MSG_LEN / 8));
} }
else { else
{
DBG_ERROR("Failed to packing rttReqFrameMsgs[%d].\n", i); DBG_ERROR("Failed to packing rttReqFrameMsgs[%d].\n", i);
goto failed; goto failed;
} }
filedSize += MGMT_MSG_LEN; filedSize += MGMT_MSG_LEN;
if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message is too long to be properly packed.\n"); DBG_ERROR("Message is too long to be properly packed.\n");
goto failed; goto failed;
} }
} }
break; break;
case MGMTPKT_INTRA_RTTRSP: case MGMTPKT_INTRA_RTTRSP:
for (int i = 0; i < ARRAY_SIZE(rttRspFrameMsgs); i++) { for (int i = 0; i < ARRAY_SIZE(rttRspFrameMsgs); i++)
if (MgmtMsg_Packing(instPtr, mgmtPkt, rttRspFrameMsgs[i]) == SUCCESS) { {
if (MgmtMsg_Packing(instPtr, mgmtPkt, rttRspFrameMsgs[i]) == SUCCESS)
{
memcpy(buff + (size_t)(i * (MGMT_MSG_LEN / 8)), memcpy(buff + (size_t)(i * (MGMT_MSG_LEN / 8)),
mgmtPkt->words, mgmtPkt->words,
(size_t)(MGMT_MSG_LEN / 8)); (size_t)(MGMT_MSG_LEN / 8));
} }
else { else
{
DBG_ERROR("Failed to packing rttRspFrameMsgs[%d].\n", i); DBG_ERROR("Failed to packing rttRspFrameMsgs[%d].\n", i);
goto failed; goto failed;
} }
filedSize += MGMT_MSG_LEN; filedSize += MGMT_MSG_LEN;
if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message is too long to be properly packed.\n"); DBG_ERROR("Message is too long to be properly packed.\n");
goto failed; goto failed;
} }
@@ -526,9 +550,11 @@ failed:
* EALLOC Memory allocation error. * EALLOC Memory allocation error.
* *
******************************************************************************/ ******************************************************************************/
int32_t MgmtNet_Init(L2_MGMT_FSM** mgmtFSM) { int32_t MgmtNet_Init(L2_MGMT_FSM **mgmtFSM)
{
(*mgmtFSM) = (L2_MGMT_FSM *)malloc(sizeof(L2_MGMT_FSM)); (*mgmtFSM) = (L2_MGMT_FSM *)malloc(sizeof(L2_MGMT_FSM));
if (*mgmtFSM == NULL) { if (*mgmtFSM == NULL)
{
return EALLOC; return EALLOC;
} }
MgmtNet_Reset(*mgmtFSM); MgmtNet_Reset(*mgmtFSM);
@@ -544,7 +570,8 @@ int32_t MgmtNet_Init(L2_MGMT_FSM** mgmtFSM) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void MgmtNet_Reset(L2_MGMT_FSM* mgmtFSM) { void MgmtNet_Reset(L2_MGMT_FSM *mgmtFSM)
{
mgmtFSM->isBisarProc = false; mgmtFSM->isBisarProc = false;
mgmtFSM->isPendSlotAlloc = false; mgmtFSM->isPendSlotAlloc = false;
mgmtFSM->isPendTimeAlloc = false; mgmtFSM->isPendTimeAlloc = false;
@@ -571,7 +598,6 @@ void MgmtNet_Reset(L2_MGMT_FSM* mgmtFSM) {
memset(&(mgmtFSM->rttMsgReqLog), 0x00, sizeof(L2_RTT_MSG_RECORD)); memset(&(mgmtFSM->rttMsgReqLog), 0x00, sizeof(L2_RTT_MSG_RECORD));
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Destroy management net FSM, free all allocated memory. * @brief Destroy management net FSM, free all allocated memory.
@@ -581,7 +607,8 @@ void MgmtNet_Reset(L2_MGMT_FSM* mgmtFSM) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void MgmtNet_Destroy(L2_MGMT_FSM* mgmtFSM){ void MgmtNet_Destroy(L2_MGMT_FSM *mgmtFSM)
{
free(mgmtFSM); free(mgmtFSM);
} }
@@ -596,7 +623,8 @@ void MgmtNet_Destroy(L2_MGMT_FSM* mgmtFSM){
* Other Packed was dumped(not addressed to me, unrecognized frame type) * Other Packed was dumped(not addressed to me, unrecognized frame type)
* *
******************************************************************************/ ******************************************************************************/
int32_t MgmtNet_PktHandler(L2_INST* instPtr) { int32_t MgmtNet_PktHandler(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status; int32_t Status;
uint32_t filedSize = 0; uint32_t filedSize = 0;
@@ -608,18 +636,23 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
DBG_INFO("Management net handling packet...\n"); DBG_INFO("Management net handling packet...\n");
BIT_ARRAY *mgmtMsg = bit_array_create(L2_MSG_MAX); BIT_ARRAY *mgmtMsg = bit_array_create(L2_MSG_MAX);
if (mgmtMsg == NULL) { if (mgmtMsg == NULL)
{
DBG_INFO("Failed to start handling process...\n"); DBG_INFO("Failed to start handling process...\n");
return EALLOC; return EALLOC;
} }
switch (L2_GetLastFrameType(instPtr)) { switch (L2_GetLastFrameType(instPtr))
{
case L2_INTER_MGMT_FRAME: case L2_INTER_MGMT_FRAME:
case L2_INTRA_MGMT_FRAME: { case L2_INTRA_MGMT_FRAME:
for (int i = 0; i < ARRAY_SIZE(intraMgmtFrameMsgs); i++) { {
for (int i = 0; i < ARRAY_SIZE(intraMgmtFrameMsgs); i++)
{
memcpy(mgmtMsg->words, memcpy(mgmtMsg->words,
instPtr->recvPktData + (size_t)(i * (MGMT_MSG_LEN / 8)), instPtr->recvPktData + (size_t)(i * (MGMT_MSG_LEN / 8)),
(size_t)(MGMT_MSG_LEN / 8)); (size_t)(MGMT_MSG_LEN / 8));
switch (intraMgmtFrameMsgs[i]) { switch (intraMgmtFrameMsgs[i])
{
case L2_MSG_TK_INIT: case L2_MSG_TK_INIT:
FrameGetChunk(mgmtMsg, TK_MSG_INI_NET_STATE_OFFSET, TK_MSG_INI_NET_STATE_LEN, &dataChunk); FrameGetChunk(mgmtMsg, TK_MSG_INI_NET_STATE_OFFSET, TK_MSG_INI_NET_STATE_LEN, &dataChunk);
mgmtMsgLog->nextNetState = (uint8_t)(dataChunk); mgmtMsgLog->nextNetState = (uint8_t)(dataChunk);
@@ -634,12 +667,12 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
mgmtMsgLog->msgSubTag = (uint8_t)(dataChunk); mgmtMsgLog->msgSubTag = (uint8_t)(dataChunk);
// Preventing the error response caused by unexpected bit error. // Preventing the error response caused by unexpected bit error.
if ( mgmtMsgLog->wordTag == 3 if (mgmtMsgLog->wordTag == 3 && mgmtMsgLog->msgTag == 28 && mgmtMsgLog->msgSubTag == 1)
&& mgmtMsgLog->msgTag == 28 {
&& mgmtMsgLog->msgSubTag == 1 ) {
mgmtMsgLog->isValid = true; mgmtMsgLog->isValid = true;
} }
else { else
{
mgmtMsgLog->isValid = false; mgmtMsgLog->isValid = false;
} }
break; break;
@@ -678,14 +711,17 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
case L2_MSG_NQT_INIT: case L2_MSG_NQT_INIT:
FrameGetChunk(mgmtMsg, NQT_MSG_OBJ_OFFSET, NQT_MSG_OBJ_LEN, &dataChunk); FrameGetChunk(mgmtMsg, NQT_MSG_OBJ_OFFSET, NQT_MSG_OBJ_LEN, &dataChunk);
mgmtMsgLog->quitNodeId = (uint8_t)(dataChunk & 0x1F); mgmtMsgLog->quitNodeId = (uint8_t)(dataChunk & 0x1F);
if (mgmtMsgLog->quitNodeId != L2_GetNodeId(instPtr)) { if (mgmtMsgLog->quitNodeId != L2_GetNodeId(instPtr))
{
FrameGetChunk(mgmtMsg, NQT_MSG_CTRL_MUTE_OFFSET, NQT_MSG_CTRL_MUTE_LEN, &dataChunk); FrameGetChunk(mgmtMsg, NQT_MSG_CTRL_MUTE_OFFSET, NQT_MSG_CTRL_MUTE_LEN, &dataChunk);
mgmtMsgLog->quitCtrlWord = (uint8_t)(dataChunk & 0x7F); mgmtMsgLog->quitCtrlWord = (uint8_t)(dataChunk & 0x7F);
if (mgmtMsgLog->quitCtrlWord == 0x01) { if (mgmtMsgLog->quitCtrlWord == 0x01)
{
L2_DeleteNodeById(instPtr->nodeList, mgmtMsgLog->quitNodeId); L2_DeleteNodeById(instPtr->nodeList, mgmtMsgLog->quitNodeId);
} }
} }
else { else
{
DBG_WARNING("Ignore node quit request(ID not mine), target node id is [%d]\n", mgmtMsgLog->quitNodeId); DBG_WARNING("Ignore node quit request(ID not mine), target node id is [%d]\n", mgmtMsgLog->quitNodeId);
} }
break; break;
@@ -695,16 +731,19 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
goto failed; goto failed;
} }
filedSize += MGMT_MSG_LEN; filedSize += MGMT_MSG_LEN;
if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_MGMT_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message decapsulate exceeds frame length.\n"); DBG_ERROR("Message decapsulate exceeds frame length.\n");
Status = EPARAM; Status = EPARAM;
goto failed; goto failed;
} }
} }
// Member Node update state with received message // Member Node update state with received message
if(L2_GetNodeId(instPtr) != L2_NODE_HEAD && mgmtMsgLog->isValid == true ){ if (L2_GetNodeId(instPtr) != L2_NODE_HEAD && mgmtMsgLog->isValid == true)
{
L2_NODE_STATE newState = L2_NONE_STATE; L2_NODE_STATE newState = L2_NONE_STATE;
switch (mgmtMsgLog->nextNetState) { switch (mgmtMsgLog->nextNetState)
{
case NET_INI_STAGE: case NET_INI_STAGE:
newState = L2_INIT_STATE; newState = L2_INIT_STATE;
break; break;
@@ -727,26 +766,30 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
} }
L2_EVENT stateUpEvent = { L2_EVENT stateUpEvent = {
.event = L2_EV_MAIN_STATE_UPDATE, .event = L2_EV_MAIN_STATE_UPDATE,
.msg = &newState .msg = &newState};
};
AssertL2Event(instPtr, &stateUpEvent); AssertL2Event(instPtr, &stateUpEvent);
} }
L2_RstNodeTTL(instPtr->nodeList, mgmtMsgLog->srcNodeId); L2_RstNodeTTL(instPtr->nodeList, mgmtMsgLog->srcNodeId);
// TODO: Check // TODO: Check
if(mgmtMsgLog->srcNodeId == L2_GetHeadId(instPtr)){ if (mgmtMsgLog->srcNodeId == L2_GetHeadId(instPtr))
{
L2_TimeAlign(instPtr, L2_GetLastFrameNum(instPtr), L2_GetLastSlotNum(instPtr), true); L2_TimeAlign(instPtr, L2_GetLastFrameNum(instPtr), L2_GetLastSlotNum(instPtr), true);
} }
break; break;
} }
case L2_INTER_TSYNC_FRAME: case L2_INTER_TSYNC_FRAME:
case L2_INTRA_TSYNC_FRAME: { case L2_INTRA_TSYNC_FRAME:
if(L2_GetNodeType(instPtr) == L2_NODE_HEAD){ {
for (int i = 0; i < ARRAY_SIZE(rttReqFrameMsgs); i++) { if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
{
for (int i = 0; i < ARRAY_SIZE(rttReqFrameMsgs); i++)
{
memcpy(mgmtMsg->words, memcpy(mgmtMsg->words,
instPtr->recvPktData + (size_t)(i * (RTT_MSG_LEN / 8)), instPtr->recvPktData + (size_t)(i * (RTT_MSG_LEN / 8)),
(size_t)(RTT_MSG_LEN / 8)); (size_t)(RTT_MSG_LEN / 8));
switch (rttReqFrameMsgs[i]) { switch (rttReqFrameMsgs[i])
{
case L2_MSG_RTT_REQ_INIT: case L2_MSG_RTT_REQ_INIT:
FrameGetChunk(mgmtMsg, RTT_REQ_WTAG_OFFSET, RTT_REQ_WTAG_LEN, &dataChunk); FrameGetChunk(mgmtMsg, RTT_REQ_WTAG_OFFSET, RTT_REQ_WTAG_LEN, &dataChunk);
rttMsgReqLog->wordTag = (uint16_t)(dataChunk); rttMsgReqLog->wordTag = (uint16_t)(dataChunk);
@@ -786,19 +829,23 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
goto failed; goto failed;
} }
filedSize += RTT_MSG_LEN; filedSize += RTT_MSG_LEN;
if (filedSize / 8 >= INTRA_RTT_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_RTT_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message decapsulate exceeds frame length.\n"); DBG_ERROR("Message decapsulate exceeds frame length.\n");
Status = EPARAM; Status = EPARAM;
goto failed; goto failed;
} }
} }
} }
else { else
for (int i = 0; i < ARRAY_SIZE(rttRspFrameMsgs); i++) { {
for (int i = 0; i < ARRAY_SIZE(rttRspFrameMsgs); i++)
{
memcpy(mgmtMsg->words, memcpy(mgmtMsg->words,
instPtr->recvPktData + (size_t)(i * (RTT_MSG_LEN / 8)), instPtr->recvPktData + (size_t)(i * (RTT_MSG_LEN / 8)),
(size_t)(RTT_MSG_LEN / 8)); (size_t)(RTT_MSG_LEN / 8));
switch (rttRspFrameMsgs[i]) { switch (rttRspFrameMsgs[i])
{
case L2_MSG_RTT_RSP_INIT: case L2_MSG_RTT_RSP_INIT:
FrameGetChunk(mgmtMsg, RTT_RSP_WTAG_OFFSET, RTT_RSP_WTAG_LEN, &dataChunk); FrameGetChunk(mgmtMsg, RTT_RSP_WTAG_OFFSET, RTT_RSP_WTAG_LEN, &dataChunk);
rttMsgRspLog->wordTag = (uint16_t)(dataChunk); rttMsgRspLog->wordTag = (uint16_t)(dataChunk);
@@ -839,7 +886,8 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
goto failed; goto failed;
} }
filedSize += RTT_MSG_LEN; filedSize += RTT_MSG_LEN;
if (filedSize / 8 >= INTRA_RTT_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_RTT_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message decapsulate exceeds frame length.\n"); DBG_ERROR("Message decapsulate exceeds frame length.\n");
Status = EPARAM; Status = EPARAM;
goto failed; goto failed;
@@ -849,7 +897,8 @@ int32_t MgmtNet_PktHandler(L2_INST* instPtr) {
L2_RstNodeTTL(instPtr->nodeList, rttMsgRspLog->srcAddr & 0x001F); L2_RstNodeTTL(instPtr->nodeList, rttMsgRspLog->srcAddr & 0x001F);
// TODO: Check // TODO: Check
if((rttMsgRspLog->srcAddr&0x001F) == L2_GetHeadId(instPtr)){ if ((rttMsgRspLog->srcAddr & 0x001F) == L2_GetHeadId(instPtr))
{
L2_TimeAlign(instPtr, L2_GetLastFrameNum(instPtr), L2_GetLastSlotNum(instPtr), true); L2_TimeAlign(instPtr, L2_GetLastFrameNum(instPtr), L2_GetLastSlotNum(instPtr), true);
} }
break; break;
@@ -866,18 +915,17 @@ failed:
return Status; return Status;
} }
/** /**
* @brief Helper function for check node TTL. * @brief Helper function for check node TTL.
* @param entry * @param entry
* @return trur/false * @return trur/false
*/ */
static bool IsTTLValid(NODE_ENTRY* entry) { static bool IsTTLValid(NODE_ENTRY *entry)
{
ASSERT(entry != NULL); ASSERT(entry != NULL);
return (entry->ttlCnt <= CONX_LOST_TH); return (entry->ttlCnt <= CONX_LOST_TH);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Management net FSM routine. * @brief Management net FSM routine.
@@ -888,12 +936,14 @@ static bool IsTTLValid(NODE_ENTRY* entry) {
* @return Slot has be used. * @return Slot has be used.
* *
******************************************************************************/ ******************************************************************************/
int32_t MgmtNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry){ int32_t MgmtNet_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status; int32_t Status;
DBG_INFO("-------- MgmtNet Routine --------\n"); DBG_INFO("-------- MgmtNet Routine --------\n");
if(rtneEntry->slotSeq != 0){ if (rtneEntry->slotSeq != 0)
{
return SUCCESS; return SUCCESS;
} }
@@ -902,27 +952,31 @@ int32_t MgmtNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry){
// Determine next slot TOR state. // Determine next slot TOR state.
L2_TOR_TYPE torState = L2_TOR_SUSPEND; L2_TOR_TYPE torState = L2_TOR_SUSPEND;
if ((rtneEntry->src0 == L2_GetNodeId(instPtr)) if ((rtneEntry->src0 == L2_GetNodeId(instPtr)) || (rtneEntry->src1 == L2_GetNodeId(instPtr)))
|| (rtneEntry->src1 == L2_GetNodeId(instPtr))) { {
torState = L2_TOR_TX; torState = L2_TOR_TX;
} }
else if ((rtneEntry->dst0 == L2_GetNodeId(instPtr)) else if ((rtneEntry->dst0 == L2_GetNodeId(instPtr)) || (rtneEntry->dst1 == L2_GetNodeId(instPtr)))
|| (rtneEntry->dst1 == L2_GetNodeId(instPtr))) { {
torState = L2_TOR_RX; torState = L2_TOR_RX;
} }
else { else
{
torState = L2_TOR_SUSPEND; torState = L2_TOR_SUSPEND;
} }
// Update RTT FSM if needed. // Update RTT FSM if needed.
if (rtneEntry->slotType == L2_SLOT_INTRA_TSYNC) { if (rtneEntry->slotType == L2_SLOT_INTRA_TSYNC)
if(torState == L2_TOR_TX) { {
if (torState == L2_TOR_TX)
{
RTT_FsmUpdate(instPtr, L2_TOR_TX); RTT_FsmUpdate(instPtr, L2_TOR_TX);
} }
else if (torState == L2_TOR_RX) else if (torState == L2_TOR_RX)
{ {
L2_SCHD_EVENT *rttRxEvent = L2_CreatNewSchdEvent(instPtr->schdEventPool, L2_EV_RTT_RECV_UPDATE, true); L2_SCHD_EVENT *rttRxEvent = L2_CreatNewSchdEvent(instPtr->schdEventPool, L2_EV_RTT_RECV_UPDATE, true);
if(rttRxEvent == NULL) { if (rttRxEvent == NULL)
{
DBG_ERROR("Failed to initialize rtt event.\n"); DBG_ERROR("Failed to initialize rtt event.\n");
} }
// Schedule rtt update after receive process end. // Schedule rtt update after receive process end.
@@ -930,15 +984,18 @@ int32_t MgmtNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry){
} }
} }
switch (rtneEntry->slotType) { switch (rtneEntry->slotType)
{
case L2_SLOT_INTER_MGMT: case L2_SLOT_INTER_MGMT:
// TODO: // TODO:
L2_SetupNextSlot(instPtr, torState, L2_INTER_MGMT_FRAME); L2_SetupNextSlot(instPtr, torState, L2_INTER_MGMT_FRAME);
break; break;
case L2_SLOT_INTRA_MGMT: case L2_SLOT_INTRA_MGMT:
memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX); memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX);
if (torState == L2_TOR_TX) { if (torState == L2_TOR_TX)
if(MgmtPkt_Packing(instPtr, MGMTPKT_INTRA_MGMT) != SUCCESS) { {
if (MgmtPkt_Packing(instPtr, MGMTPKT_INTRA_MGMT) != SUCCESS)
{
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
} }
@@ -950,16 +1007,21 @@ int32_t MgmtNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry){
L2_SetupNextSlot(instPtr, torState, L2_INTER_TSYNC_FRAME); L2_SetupNextSlot(instPtr, torState, L2_INTER_TSYNC_FRAME);
break; break;
case L2_SLOT_INTRA_TSYNC: case L2_SLOT_INTRA_TSYNC:
if (torState == L2_TOR_TX) { if (torState == L2_TOR_TX)
{
memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX); memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX);
if(L2_GetNodeType(instPtr) == L2_NODE_HEAD){ if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
if(MgmtPkt_Packing(instPtr, MGMTPKT_INTRA_RTTREQ) != SUCCESS) { {
if (MgmtPkt_Packing(instPtr, MGMTPKT_INTRA_RTTREQ) != SUCCESS)
{
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
} }
} }
else { else
if(MgmtPkt_Packing(instPtr, MGMTPKT_INTRA_RTTRSP) != SUCCESS) { {
if (MgmtPkt_Packing(instPtr, MGMTPKT_INTRA_RTTRSP) != SUCCESS)
{
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
} }
@@ -975,35 +1037,36 @@ int32_t MgmtNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry){
/*************************************************/ /*************************************************/
// Update link layer state... // Update link layer state...
if (L2_GetNodeType(instPtr) == L2_NODE_HEAD) { if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
{
L2_NODE_STATE newState = L2_NONE_STATE; L2_NODE_STATE newState = L2_NONE_STATE;
L2_EVENT stateEvent = { L2_EVENT stateEvent = {
.event = L2_EV_MAIN_STATE_UPDATE, .event = L2_EV_MAIN_STATE_UPDATE,
.msg = &newState .msg = &newState};
}; if (L2_GetNodeListLen(instPtr->nodeList) < instPtr->config.netScale && L2_GetMainState(instPtr) != L2_INTRA_SEEK_STATE)
if (L2_GetNodeListLen(instPtr->nodeList) < instPtr->config.netScale {
&& L2_GetMainState(instPtr) != L2_INTRA_SEEK_STATE) {
newState = L2_INTRA_SEEK_STATE; newState = L2_INTRA_SEEK_STATE;
AssertL2Event(instPtr, &stateEvent); AssertL2Event(instPtr, &stateEvent);
} }
else if (L2_GetNodeListLen(instPtr->nodeList) >= instPtr->config.netScale && else if (L2_GetNodeListLen(instPtr->nodeList) >= instPtr->config.netScale &&
L2_GetMainState(instPtr) != L2_NORMAL_STATE){ L2_GetMainState(instPtr) != L2_NORMAL_STATE)
{
newState = L2_NORMAL_STATE; newState = L2_NORMAL_STATE;
AssertL2Event(instPtr, &stateEvent); AssertL2Event(instPtr, &stateEvent);
} }
} }
else{ // L2_NODE_MEMBER or BACKUP_HEAD else
if(L2_isNodeInList(instPtr->nodeList, L2_GetHeadId(instPtr)) == false){ { // L2_NODE_MEMBER or BACKUP_HEAD
if (L2_isNodeInList(instPtr->nodeList, L2_GetHeadId(instPtr)) == false)
{
L2_NODE_STATE newState = L2_INIT_STATE; L2_NODE_STATE newState = L2_INIT_STATE;
L2_EVENT stateEvent = { L2_EVENT stateEvent = {
.event = L2_EV_MAIN_STATE_UPDATE, .event = L2_EV_MAIN_STATE_UPDATE,
.msg = &newState .msg = &newState};
};
AssertL2Event(instPtr, &stateEvent); AssertL2Event(instPtr, &stateEvent);
L2_EVENT lostCxnEvent = { L2_EVENT lostCxnEvent = {
.event = L2_EV_LOST_CXN, .event = L2_EV_LOST_CXN,
.msg = NULL .msg = NULL};
};
AssertL2Event(instPtr, &lostCxnEvent); AssertL2Event(instPtr, &lostCxnEvent);
DBG_WARNING("Lost connection with head node [%d].\n", L2_GetHeadId(instPtr)); DBG_WARNING("Lost connection with head node [%d].\n", L2_GetHeadId(instPtr));
} }
+199 -112
View File
@@ -51,7 +51,8 @@
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef enum { typedef enum
{
SCANPKT_NONE = 0, SCANPKT_NONE = 0,
SCANPKT_INTER_BM = 1, SCANPKT_INTER_BM = 1,
SCANPKT_INTRA_BM = 2, SCANPKT_INTRA_BM = 2,
@@ -76,7 +77,8 @@ static int32_t ScanPkt_Packing(L2_INST* instPtr, SCANPKT_TYPE pktType);
/** /**
* @brief Update Init-Scan lock state. * @brief Update Init-Scan lock state.
*/ */
static inline void LockStateUpdate(L2_SCAN_FSM* scanFsmPtr, bool state){ static inline void LockStateUpdate(L2_SCAN_FSM *scanFsmPtr, bool state)
{
ASSERT(scanFsmPtr != NULL); ASSERT(scanFsmPtr != NULL);
DBG_INFO("Update BeamLockState from [%d] to [%d].\n", DBG_INFO("Update BeamLockState from [%d] to [%d].\n",
scanFsmPtr->isLocked, state); scanFsmPtr->isLocked, state);
@@ -87,7 +89,8 @@ static inline void LockStateUpdate(L2_SCAN_FSM* scanFsmPtr, bool state){
/** /**
* @brief Update Base-info Advertisement state. * @brief Update Base-info Advertisement state.
*/ */
static void BmrecvStateUpdate(L2_SCAN_FSM* scanFsmPtr, bool state){ static void BmrecvStateUpdate(L2_SCAN_FSM *scanFsmPtr, bool state)
{
ASSERT(scanFsmPtr != NULL); ASSERT(scanFsmPtr != NULL);
DBG_INFO("Update BmRecvState from [%d] to [%d].\n", DBG_INFO("Update BmRecvState from [%d] to [%d].\n",
scanFsmPtr->isBmRcved, state); scanFsmPtr->isBmRcved, state);
@@ -98,7 +101,8 @@ static void BmrecvStateUpdate(L2_SCAN_FSM* scanFsmPtr, bool state){
/** /**
* @brief Update 2-Way Acquisition ack state. * @brief Update 2-Way Acquisition ack state.
*/ */
static void W2ackStateUpdate(L2_SCAN_FSM* scanFsmPtr, bool state){ static void W2ackStateUpdate(L2_SCAN_FSM *scanFsmPtr, bool state)
{
ASSERT(scanFsmPtr != NULL); ASSERT(scanFsmPtr != NULL);
DBG_INFO("Update W2acqAckState from [%d] to [%d].\n", DBG_INFO("Update W2acqAckState from [%d] to [%d].\n",
scanFsmPtr->isAcked, state); scanFsmPtr->isAcked, state);
@@ -117,7 +121,8 @@ static void W2ackStateUpdate(L2_SCAN_FSM* scanFsmPtr, bool state){
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
static void StageUpdate(L2_SCAN_FSM* scanFsmPtr, SCAN_STAGE newStage) { static void StageUpdate(L2_SCAN_FSM *scanFsmPtr, SCAN_STAGE newStage)
{
DBG_INFO("ScanNet current stage: %u -> ScanNet new stage: %u \n", DBG_INFO("ScanNet current stage: %u -> ScanNet new stage: %u \n",
scanFsmPtr->scanStage, newStage); scanFsmPtr->scanStage, newStage);
scanFsmPtr->scanStage = newStage; scanFsmPtr->scanStage = newStage;
@@ -133,17 +138,20 @@ static void StageUpdate(L2_SCAN_FSM* scanFsmPtr, SCAN_STAGE newStage) {
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE msgType) { static int32_t ScanMsg_Packing(L2_INST *instPtr, BIT_ARRAY *bitarr, L2_MSG_TYPE msgType)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
int32_t Status = SUCCESS; int32_t Status = SUCCESS;
if (bitarr->num_of_bits < NS_MSG_LEN || msgType < L2_MSG_BM if (bitarr->num_of_bits < NS_MSG_LEN || msgType < L2_MSG_BM || msgType > L2_MSG_SCAN_CONT_5)
|| msgType > L2_MSG_SCAN_CONT_5) { {
return (EPARAM); return (EPARAM);
} }
uint64_t dataChunk = 0; uint64_t dataChunk = 0;
switch (msgType) { switch (msgType)
{
case L2_MSG_BM: case L2_MSG_BM:
if (bitarr->num_of_bits < BM_MSG_LEN) { if (bitarr->num_of_bits < BM_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* message type all zero */ /* message type all zero */
@@ -200,7 +208,8 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
break; break;
case L2_MSG_SCAN_INIT: case L2_MSG_SCAN_INIT:
if (bitarr->num_of_bits < NS_MSG_LEN) { if (bitarr->num_of_bits < NS_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* Sequence tag */ /* Sequence tag */
@@ -228,18 +237,18 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
FrameInsertChunk(bitarr, NS_MSG_INI_TYPE_OFFSET, NS_MSG_INI_TYPE_LEN, dataChunk); FrameInsertChunk(bitarr, NS_MSG_INI_TYPE_OFFSET, NS_MSG_INI_TYPE_LEN, dataChunk);
/* source node id */ /* source node id */
dataChunk = (L2_GetNodeId(instPtr) & 0x1F) dataChunk = (L2_GetNodeId(instPtr) & 0x1F) | (((uint16_t)(L2_GetClusterId(instPtr) & 0x1F)) << 5);
| (((uint16_t)(L2_GetClusterId(instPtr) & 0x1F)) << 5);
FrameInsertChunk(bitarr, NS_MSG_INI_SRC_ADDR_OFFSET, NS_MSG_INI_SRC_ADDR_LEN, dataChunk); FrameInsertChunk(bitarr, NS_MSG_INI_SRC_ADDR_OFFSET, NS_MSG_INI_SRC_ADDR_LEN, dataChunk);
/* destination node id */ /* destination node id */
dataChunk = (L2_GetNodeId(instPtr) == L2_NODE_HEAD) ? dataChunk = (L2_GetNodeId(instPtr) == L2_NODE_HEAD) ? (L2_BROADCAST_ID | (L2_BROADCAST_ID << 5))
(L2_BROADCAST_ID | (L2_BROADCAST_ID << 5))
: (L2_GetHeadId(instPtr) & 0x1F) | (((uint16_t)(L2_GetClusterId(instPtr) & 0x1F)) << 5); : (L2_GetHeadId(instPtr) & 0x1F) | (((uint16_t)(L2_GetClusterId(instPtr) & 0x1F)) << 5);
FrameInsertChunk(bitarr, NS_MSG_INI_LEN_OFFSET, NS_MSG_INI_LEN_LEN, dataChunk); FrameInsertChunk(bitarr, NS_MSG_INI_LEN_OFFSET, NS_MSG_INI_LEN_LEN, dataChunk);
if (L2_GetNodeType(instPtr) == L2_NODE_HEAD) { if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
{
/* current net stage */ /* current net stage */
switch (instPtr->mainState) { switch (instPtr->mainState)
{
case L2_INIT_STATE: case L2_INIT_STATE:
dataChunk = NET_INI_STAGE; dataChunk = NET_INI_STAGE;
break; break;
@@ -261,7 +270,8 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
} }
break; break;
case L2_MSG_SCAN_CONT_1: case L2_MSG_SCAN_CONT_1:
if (bitarr->num_of_bits < NS_MSG_LEN) { if (bitarr->num_of_bits < NS_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* word tag */ /* word tag */
@@ -282,7 +292,8 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
break; break;
case L2_MSG_SCAN_CONT_2: case L2_MSG_SCAN_CONT_2:
if (bitarr->num_of_bits < NS_MSG_LEN) { if (bitarr->num_of_bits < NS_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* word tag */ /* word tag */
@@ -307,7 +318,8 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
break; break;
case L2_MSG_SCAN_CONT_3: case L2_MSG_SCAN_CONT_3:
if (bitarr->num_of_bits < NS_MSG_LEN) { if (bitarr->num_of_bits < NS_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* word tag */ /* word tag */
@@ -328,7 +340,8 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
break; break;
case L2_MSG_SCAN_CONT_4: case L2_MSG_SCAN_CONT_4:
if (bitarr->num_of_bits < NS_MSG_LEN) { if (bitarr->num_of_bits < NS_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* word tag */ /* word tag */
@@ -344,7 +357,8 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
FrameInsertChunk(bitarr, NS_MSG_CONT_4_KEY_OFFSET, NS_MSG_CONT_4_KEY_LEN, dataChunk); FrameInsertChunk(bitarr, NS_MSG_CONT_4_KEY_OFFSET, NS_MSG_CONT_4_KEY_LEN, dataChunk);
break; break;
case L2_MSG_SCAN_CONT_5: case L2_MSG_SCAN_CONT_5:
if (bitarr->num_of_bits < NS_MSG_LEN) { if (bitarr->num_of_bits < NS_MSG_LEN)
{
return EPARAM; return EPARAM;
} }
/* word tag */ /* word tag */
@@ -366,34 +380,42 @@ static int32_t ScanMsg_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_MSG_TYPE
return Status; return Status;
} }
static int32_t ScanPkt_Packing(L2_INST* instPtr, SCANPKT_TYPE pktType){ static int32_t ScanPkt_Packing(L2_INST *instPtr, SCANPKT_TYPE pktType)
{
uint32_t filedSize = 0; uint32_t filedSize = 0;
uint8_t *buff = instPtr->sendPktData; uint8_t *buff = instPtr->sendPktData;
BIT_ARRAY *scanPkt = bit_array_create(L2_MSG_MAX); BIT_ARRAY *scanPkt = bit_array_create(L2_MSG_MAX);
if (scanPkt == NULL) { if (scanPkt == NULL)
{
DBG_ERROR("Failed to allocate memory for scanMsg.\n"); DBG_ERROR("Failed to allocate memory for scanMsg.\n");
return FAILURE; return FAILURE;
} }
switch (pktType) { switch (pktType)
{
case SCANPKT_INTRA_BM: case SCANPKT_INTRA_BM:
if (ScanMsg_Packing(instPtr, scanPkt, L2_MSG_BM) != SUCCESS) { if (ScanMsg_Packing(instPtr, scanPkt, L2_MSG_BM) != SUCCESS)
{
DBG_ERROR("Failed to packing L2_MSG_BM frame.\n"); DBG_ERROR("Failed to packing L2_MSG_BM frame.\n");
goto failed; goto failed;
} }
memcpy(buff, scanPkt->words, (size_t)(BM_MSG_LEN / 8)); memcpy(buff, scanPkt->words, (size_t)(BM_MSG_LEN / 8));
break; break;
case SCANPKT_INTRA_2WACQ: case SCANPKT_INTRA_2WACQ:
for (int i = 0; i < ARRAY_SIZE(intraW2AcqFrameMsgs); i++) { for (int i = 0; i < ARRAY_SIZE(intraW2AcqFrameMsgs); i++)
if (ScanMsg_Packing(instPtr, scanPkt, intraW2AcqFrameMsgs[i]) == SUCCESS) { {
if (ScanMsg_Packing(instPtr, scanPkt, intraW2AcqFrameMsgs[i]) == SUCCESS)
{
memcpy((uint8_t *)buff + (size_t)(i * (NS_MSG_LEN / 8)), memcpy((uint8_t *)buff + (size_t)(i * (NS_MSG_LEN / 8)),
scanPkt->words, (size_t)(NS_MSG_LEN / 8)); scanPkt->words, (size_t)(NS_MSG_LEN / 8));
} }
else { else
{
DBG_ERROR("Failed to packing intraW2AcqFrameMsgs[%d].\n", i); DBG_ERROR("Failed to packing intraW2AcqFrameMsgs[%d].\n", i);
goto failed; goto failed;
} }
filedSize += NS_MSG_LEN; filedSize += NS_MSG_LEN;
if (filedSize / 8 >= INTRA_W2ACQ_FRAME_LENGTH_MAX) { if (filedSize / 8 >= INTRA_W2ACQ_FRAME_LENGTH_MAX)
{
DBG_ERROR("Message is too long to be properly packed.\n"); DBG_ERROR("Message is too long to be properly packed.\n");
goto failed; goto failed;
} }
@@ -420,36 +442,44 @@ failed:
* @return used slot (5ms slot). * @return used slot (5ms slot).
* *
******************************************************************************/ ******************************************************************************/
static int32_t Scan_InitScanHandler(L2_INST* instPtr) { static int32_t Scan_InitScanHandler(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM; L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM;
DBG_INFO("** Netsetup - InitScan Procedure (Attempts: %u)**\n", scanFsmPtr->scanCnt); DBG_INFO("** Netsetup - InitScan Procedure (Attempts: %u)**\n", scanFsmPtr->scanCnt);
if(scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE){ if (scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE)
if (scanFsmPtr->isLocked == true) { {
if (scanFsmPtr->isLocked == true)
{
StageUpdate(scanFsmPtr, L2_SCAN_BMADVT_STAGE); StageUpdate(scanFsmPtr, L2_SCAN_BMADVT_STAGE);
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_BM_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_BM_FRAME);
} /* Otherwise remain L2_SCAN_INIT_STAGE */ } /* Otherwise remain L2_SCAN_INIT_STAGE */
else { else
{
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_SCAN_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_SCAN_FRAME);
scanFsmPtr->scanCnt++; scanFsmPtr->scanCnt++;
} }
} }
else if(scanFsmPtr->scanMode == L2_SCAN_INTER_MODE){ else if (scanFsmPtr->scanMode == L2_SCAN_INTER_MODE)
if (scanFsmPtr->isLocked == true) { {
if (scanFsmPtr->isLocked == true)
{
StageUpdate(scanFsmPtr, L2_SCAN_BMADVT_STAGE); StageUpdate(scanFsmPtr, L2_SCAN_BMADVT_STAGE);
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_BM_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_BM_FRAME);
} /* Otherwise remain L2_SCAN_INIT_STAGE */ } /* Otherwise remain L2_SCAN_INIT_STAGE */
else { else
{
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_SCAN_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_SCAN_FRAME);
scanFsmPtr->scanCnt++; scanFsmPtr->scanCnt++;
} }
} }
else{ else
{
DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode); DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode);
return FAILURE; return FAILURE;
} }
@@ -467,12 +497,14 @@ static int32_t Scan_InitScanHandler(L2_INST* instPtr) {
* @return used slot (5ms slot). * @return used slot (5ms slot).
* *
******************************************************************************/ ******************************************************************************/
static int32_t Scan_BmAdvtHandler(L2_INST* instPtr) { static int32_t Scan_BmAdvtHandler(L2_INST *instPtr)
{
L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM; L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM;
DBG_INFO("** Netsetup - BmAdvt Procedure (Timeout: %u) **\n", scanFsmPtr->bmTimeout); DBG_INFO("** Netsetup - BmAdvt Procedure (Timeout: %u) **\n", scanFsmPtr->bmTimeout);
// if received a valid bm frame. // if received a valid bm frame.
if (scanFsmPtr->isBmRcved == true) { if (scanFsmPtr->isBmRcved == true)
{
scanFsmPtr->bmTimeout = 0; /* reset listen counter. */ scanFsmPtr->bmTimeout = 0; /* reset listen counter. */
DBG_INFO("Received Bm frame, skipping 10ms for turn around...\n"); DBG_INFO("Received Bm frame, skipping 10ms for turn around...\n");
// TODO: check functionality. // TODO: check functionality.
@@ -480,18 +512,23 @@ static int32_t Scan_BmAdvtHandler(L2_INST* instPtr) {
return SUCCESS; return SUCCESS;
} }
// no valid bm frame was received. // no valid bm frame was received.
else { else
{
/* if time out reached, go back to init scan stage. */ /* if time out reached, go back to init scan stage. */
if (scanFsmPtr->bmTimeout > BM_LSTEN_TIMEOUT) { if (scanFsmPtr->bmTimeout > BM_LSTEN_TIMEOUT)
if(scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE){ {
if (scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE)
{
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_SCAN_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_SCAN_FRAME);
} }
else if(scanFsmPtr->scanMode == L2_SCAN_INTER_MODE){ else if (scanFsmPtr->scanMode == L2_SCAN_INTER_MODE)
{
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_SCAN_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_SCAN_FRAME);
} }
else{ else
{
DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode); DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode);
return FAILURE; return FAILURE;
} }
@@ -499,16 +536,20 @@ static int32_t Scan_BmAdvtHandler(L2_INST* instPtr) {
scanFsmPtr->bmTimeout = 0; /* reset listen counter. */ scanFsmPtr->bmTimeout = 0; /* reset listen counter. */
StageUpdate(scanFsmPtr, L2_SCAN_INIT_STAGE); StageUpdate(scanFsmPtr, L2_SCAN_INIT_STAGE);
} }
else { else
if(scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE){ {
if (scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE)
{
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_BM_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTRA_BM_FRAME);
} }
else if(scanFsmPtr->scanMode == L2_SCAN_INTER_MODE){ else if (scanFsmPtr->scanMode == L2_SCAN_INTER_MODE)
{
/* Antenna setting (reserved.) */ /* Antenna setting (reserved.) */
L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_BM_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_RX, L2_INTER_BM_FRAME);
} }
else{ else
{
DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode); DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode);
return FAILURE; return FAILURE;
} }
@@ -527,29 +568,35 @@ static int32_t Scan_BmAdvtHandler(L2_INST* instPtr) {
* @return used slot (5ms slot). * @return used slot (5ms slot).
* *
******************************************************************************/ ******************************************************************************/
static int32_t Scan_2WAcqHandler (L2_INST* instPtr){ static int32_t Scan_2WAcqHandler(L2_INST *instPtr)
{
L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM; L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM;
DBG_INFO("** Netsetup - 2WAcq Procedure (Timeout: %u) **\n", scanFsmPtr->w2acqTimeout); DBG_INFO("** Netsetup - 2WAcq Procedure (Timeout: %u) **\n", scanFsmPtr->w2acqTimeout);
L2_FRAME_TYPE frameType = L2_NONE_FRAME; L2_FRAME_TYPE frameType = L2_NONE_FRAME;
if(scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE){ if (scanFsmPtr->scanMode == L2_SCAN_INTRA_MODE)
{
frameType = L2_INTRA_W2_ACQ_FRAME; frameType = L2_INTRA_W2_ACQ_FRAME;
} }
else if(scanFsmPtr->scanMode == L2_SCAN_INTER_MODE){ else if (scanFsmPtr->scanMode == L2_SCAN_INTER_MODE)
{
frameType = L2_INTER_W2_ACQ_FRAME; frameType = L2_INTER_W2_ACQ_FRAME;
} }
else{ else
{
DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode); DBG_ERROR("UNKNOWN Scan Mode: %d.\n", scanFsmPtr->scanMode);
return FAILURE; return FAILURE;
} }
// 2 way acq failed, back to init stage. // 2 way acq failed, back to init stage.
if (scanFsmPtr->w2acqTimeout >= WAY2_ACQ_SLOT_MAX) { if (scanFsmPtr->w2acqTimeout >= WAY2_ACQ_SLOT_MAX)
{
DBG_WARNING("2WAcq timeout, restart net scan from L2_SCAN_INIT_STAGE.\n"); DBG_WARNING("2WAcq timeout, restart net scan from L2_SCAN_INIT_STAGE.\n");
scanFsmPtr->w2acqTimeout = 0; // reset counter for next scan. scanFsmPtr->w2acqTimeout = 0; // reset counter for next scan.
StageUpdate(scanFsmPtr, L2_SCAN_INIT_STAGE); StageUpdate(scanFsmPtr, L2_SCAN_INIT_STAGE);
} }
else { else
{
scanFsmPtr->w2acqTimeout++; // increase counter. scanFsmPtr->w2acqTimeout++; // increase counter.
} }
// using pull mode to receive 2wacq frame // using pull mode to receive 2wacq frame
@@ -570,7 +617,8 @@ static int32_t Scan_2WAcqHandler (L2_INST* instPtr){
* error code * error code
* *
******************************************************************************/ ******************************************************************************/
int32_t ScanNet_PktHandler(L2_INST* instPtr) { int32_t ScanNet_PktHandler(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint32_t Status; uint32_t Status;
L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM; L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM;
@@ -579,11 +627,13 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
DBG_INFO("Scan net handling packet...\n"); DBG_INFO("Scan net handling packet...\n");
BIT_ARRAY *scanMsg = bit_array_create(L2_MSG_MAX); BIT_ARRAY *scanMsg = bit_array_create(L2_MSG_MAX);
if (scanMsg == NULL) { if (scanMsg == NULL)
{
DBG_INFO("Failed to start handling process...\n"); DBG_INFO("Failed to start handling process...\n");
return EALLOC; return EALLOC;
} }
switch (L2_GetLastFrameType(instPtr)) { switch (L2_GetLastFrameType(instPtr))
{
case L2_INTER_SCAN_FRAME: case L2_INTER_SCAN_FRAME:
case L2_INTRA_SCAN_FRAME: case L2_INTRA_SCAN_FRAME:
// INTRA_BM_FRAME could be received if current frame type is L2_INTRA_SCAN_FRAME // INTRA_BM_FRAME could be received if current frame type is L2_INTRA_SCAN_FRAME
@@ -592,12 +642,15 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
LockStateUpdate(scanFsmPtr, true); LockStateUpdate(scanFsmPtr, true);
StageUpdate(scanFsmPtr, L2_SCAN_BMADVT_STAGE); StageUpdate(scanFsmPtr, L2_SCAN_BMADVT_STAGE);
case L2_INTER_BM_FRAME: case L2_INTER_BM_FRAME:
case L2_INTRA_BM_FRAME: { case L2_INTRA_BM_FRAME:
for (int i = 0; i < ARRAY_SIZE(intraBmFrameMsgs); i++) { {
for (int i = 0; i < ARRAY_SIZE(intraBmFrameMsgs); i++)
{
memcpy(scanMsg->words, memcpy(scanMsg->words,
instPtr->recvPktData + (size_t)(i * (BM_MSG_LEN / 8)), instPtr->recvPktData + (size_t)(i * (BM_MSG_LEN / 8)),
(size_t)(BM_MSG_LEN / 8)); (size_t)(BM_MSG_LEN / 8));
switch (intraBmFrameMsgs[i]) { switch (intraBmFrameMsgs[i])
{
case L2_MSG_BM: case L2_MSG_BM:
FrameGetChunk(scanMsg, BM_MSG_SRC_ADDR_OFFSET, BM_MSG_SRC_ADDR_LEN, &dataChunk); FrameGetChunk(scanMsg, BM_MSG_SRC_ADDR_OFFSET, BM_MSG_SRC_ADDR_LEN, &dataChunk);
scanFsmPtr->BMrcvLog.NodeId = dataChunk & 0x000000000000001F; scanFsmPtr->BMrcvLog.NodeId = dataChunk & 0x000000000000001F;
@@ -636,25 +689,30 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
goto failed; goto failed;
} }
} }
if(L2_GetNodeType(instPtr) != L2_NODE_HEAD){ if (L2_GetNodeType(instPtr) != L2_NODE_HEAD)
{
NODE_ENTRY *nodeEntry = L2_GetNodeById(instPtr->nodeList, scanFsmPtr->BMrcvLog.NodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(instPtr->nodeList, scanFsmPtr->BMrcvLog.NodeId);
if(nodeEntry == NULL) { if (nodeEntry == NULL)
{
nodeEntry = L2_AppendNode(instPtr->nodeList, scanFsmPtr->BMrcvLog.NodeId, L2_NODE_HEAD); nodeEntry = L2_AppendNode(instPtr->nodeList, scanFsmPtr->BMrcvLog.NodeId, L2_NODE_HEAD);
// TODO: Insert other node information. // TODO: Insert other node information.
} }
else { else
{
// TODO: Update node information. // TODO: Update node information.
} }
if ( L2_isCxn2Net(instPtr) == false ) { if (L2_isCxn2Net(instPtr) == false)
{
// TODO: Check // TODO: Check
L2_TimeAlign(instPtr, scanFsmPtr->BMrcvLog.frameNum, scanFsmPtr->BMrcvLog.slotNum, true); L2_TimeAlign(instPtr, scanFsmPtr->BMrcvLog.frameNum, scanFsmPtr->BMrcvLog.slotNum, true);
} }
// trigger flag // trigger flag
BmrecvStateUpdate(scanFsmPtr, true); BmrecvStateUpdate(scanFsmPtr, true);
} }
else{ else
{
DBG_ERROR("Node type [%d] is not supposed to receive BM frame", L2_GetNodeType(instPtr)); DBG_ERROR("Node type [%d] is not supposed to receive BM frame", L2_GetNodeType(instPtr));
Status = EUNEXP; Status = EUNEXP;
goto failed; goto failed;
@@ -662,19 +720,23 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
break; break;
} }
case L2_INTER_W2_ACQ_FRAME: case L2_INTER_W2_ACQ_FRAME:
case L2_INTRA_W2_ACQ_FRAME: { case L2_INTRA_W2_ACQ_FRAME:
for (int i = 0; i < ARRAY_SIZE(intraW2AcqFrameMsgs); i++) { {
for (int i = 0; i < ARRAY_SIZE(intraW2AcqFrameMsgs); i++)
{
memcpy(scanMsg->words, memcpy(scanMsg->words,
instPtr->recvPktData + (size_t)(i * (NS_MSG_LEN / 8)), instPtr->recvPktData + (size_t)(i * (NS_MSG_LEN / 8)),
(size_t)(NS_MSG_LEN / 8)); (size_t)(NS_MSG_LEN / 8));
switch (intraW2AcqFrameMsgs[i]) { switch (intraW2AcqFrameMsgs[i])
{
case L2_MSG_SCAN_INIT: case L2_MSG_SCAN_INIT:
FrameGetChunk(scanMsg, NS_MSG_INI_SRC_ADDR_OFFSET, NS_MSG_INI_SRC_ADDR_LEN, &dataChunk); FrameGetChunk(scanMsg, NS_MSG_INI_SRC_ADDR_OFFSET, NS_MSG_INI_SRC_ADDR_LEN, &dataChunk);
scanFsmPtr->W2ACQrcvLog.NodeId = dataChunk & 0x001F; scanFsmPtr->W2ACQrcvLog.NodeId = dataChunk & 0x001F;
scanFsmPtr->W2ACQrcvLog.ClusterId = (dataChunk >> 5) & 0x0001F; scanFsmPtr->W2ACQrcvLog.ClusterId = (dataChunk >> 5) & 0x0001F;
FrameGetChunk(scanMsg, NS_MSG_INI_NET_STAGE_OFFSET, NS_MSG_INI_NET_STAGE_LEN, &dataChunk); FrameGetChunk(scanMsg, NS_MSG_INI_NET_STAGE_OFFSET, NS_MSG_INI_NET_STAGE_LEN, &dataChunk);
switch (dataChunk) { switch (dataChunk)
{
case NET_INI_STAGE: case NET_INI_STAGE:
scanFsmPtr->W2ACQrcvLog.NetStage = L2_INIT_STATE; scanFsmPtr->W2ACQrcvLog.NetStage = L2_INIT_STATE;
break; break;
@@ -732,24 +794,27 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
} }
NODE_ENTRY *nodeEntry = L2_GetNodeById(instPtr->nodeList, scanFsmPtr->W2ACQrcvLog.NodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(instPtr->nodeList, scanFsmPtr->W2ACQrcvLog.NodeId);
if(nodeEntry == NULL) { if (nodeEntry == NULL)
{
nodeEntry = L2_AppendNode(instPtr->nodeList, scanFsmPtr->W2ACQrcvLog.NodeId, L2_NODE_MEMBER); nodeEntry = L2_AppendNode(instPtr->nodeList, scanFsmPtr->W2ACQrcvLog.NodeId, L2_NODE_MEMBER);
// TODO: Insert other node information. // TODO: Insert other node information.
} }
else { else
{
// TODO: Update node information. // TODO: Update node information.
} }
if(L2_GetNodeType(instPtr) != L2_NODE_HEAD){ if (L2_GetNodeType(instPtr) != L2_NODE_HEAD)
{
// check if net setup finished. // check if net setup finished.
if(L2_GetMainState(instPtr) == L2_INIT_STATE && L2_isCxn2Net(instPtr) == false){ if (L2_GetMainState(instPtr) == L2_INIT_STATE && L2_isCxn2Net(instPtr) == false)
{
// Assert net setup done event... // Assert net setup done event...
uint32_t netStartTime = scanFsmPtr->W2ACQrcvLog.NetScanTime; uint32_t netStartTime = scanFsmPtr->W2ACQrcvLog.NetScanTime;
L2_EVENT netSetupDoneEvent = { L2_EVENT netSetupDoneEvent = {
.event = L2_EV_NET_SETUP_DONE, .event = L2_EV_NET_SETUP_DONE,
.msg = &netStartTime .msg = &netStartTime};
};
AssertL2Event(instPtr, &netSetupDoneEvent); AssertL2Event(instPtr, &netSetupDoneEvent);
// Update time sync since netStartTime updated. // Update time sync since netStartTime updated.
@@ -759,14 +824,15 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
L2_RTNE_CONFIG newRtneConfig = {0}; L2_RTNE_CONFIG newRtneConfig = {0};
L2_EVENT rtneUpEvent = { L2_EVENT rtneUpEvent = {
.event = L2_ROUTINE_UPDATE, .event = L2_ROUTINE_UPDATE,
.msg = &newRtneConfig .msg = &newRtneConfig};
}; if (scanFsmPtr->W2ACQrcvLog.NetStage == L2_INIT_STATE)
if(scanFsmPtr->W2ACQrcvLog.NetStage == L2_INIT_STATE){ {
newRtneConfig.rtneTable = initTable; newRtneConfig.rtneTable = initTable;
newRtneConfig.rtnePrd = ARRAY_SIZE(initTable); newRtneConfig.rtnePrd = ARRAY_SIZE(initTable);
AssertL2Event(instPtr, &rtneUpEvent); AssertL2Event(instPtr, &rtneUpEvent);
} }
else if(scanFsmPtr->W2ACQrcvLog.NetStage == L2_INTRA_SEEK_STATE){ else if (scanFsmPtr->W2ACQrcvLog.NetStage == L2_INTRA_SEEK_STATE)
{
newRtneConfig.rtneTable = intraSeekTable; newRtneConfig.rtneTable = intraSeekTable;
newRtneConfig.rtnePrd = ARRAY_SIZE(intraSeekTable); newRtneConfig.rtnePrd = ARRAY_SIZE(intraSeekTable);
AssertL2Event(instPtr, &rtneUpEvent); AssertL2Event(instPtr, &rtneUpEvent);
@@ -777,15 +843,13 @@ int32_t ScanNet_PktHandler(L2_INST* instPtr) {
L2_NODE_STATE newState = scanFsmPtr->W2ACQrcvLog.NetStage; L2_NODE_STATE newState = scanFsmPtr->W2ACQrcvLog.NetStage;
L2_EVENT stateUpEvent = { L2_EVENT stateUpEvent = {
.event = L2_EV_MAIN_STATE_UPDATE, .event = L2_EV_MAIN_STATE_UPDATE,
.msg = &newState .msg = &newState};
};
AssertL2Event(instPtr, &stateUpEvent); AssertL2Event(instPtr, &stateUpEvent);
} }
break; break;
} }
default: { default:
{
DBG_ERROR("Cannot handler message type [%d] properly.\n", L2_GetLastFrameType(instPtr)); DBG_ERROR("Cannot handler message type [%d] properly.\n", L2_GetLastFrameType(instPtr));
Status = EIGNORE; Status = EIGNORE;
goto failed; goto failed;
@@ -808,9 +872,11 @@ failed:
* EMAC_MEMORY_ALLOC_ERROR Memory allocate error. * EMAC_MEMORY_ALLOC_ERROR Memory allocate error.
* *
******************************************************************************/ ******************************************************************************/
int32_t ScanNet_Init(L2_SCAN_FSM** scanFSMPtr) { int32_t ScanNet_Init(L2_SCAN_FSM **scanFSMPtr)
{
*scanFSMPtr = (L2_SCAN_FSM *)malloc(sizeof(L2_SCAN_FSM)); *scanFSMPtr = (L2_SCAN_FSM *)malloc(sizeof(L2_SCAN_FSM));
if (*scanFSMPtr == NULL) { if (*scanFSMPtr == NULL)
{
DBG_ERROR("Failed to allocate memory for scanFsmPtr.\n"); DBG_ERROR("Failed to allocate memory for scanFsmPtr.\n");
return EALLOC; return EALLOC;
} }
@@ -818,7 +884,6 @@ int32_t ScanNet_Init(L2_SCAN_FSM** scanFSMPtr) {
return (SUCCESS); return (SUCCESS);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Reset ScanNet FSM * @brief Reset ScanNet FSM
@@ -828,7 +893,8 @@ int32_t ScanNet_Init(L2_SCAN_FSM** scanFSMPtr) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void ScanNet_Reset(L2_SCAN_FSM* scanFSMPtr) { void ScanNet_Reset(L2_SCAN_FSM *scanFSMPtr)
{
ASSERT(scanFSMPtr != NULL); ASSERT(scanFSMPtr != NULL);
scanFSMPtr->isLocked = false; scanFSMPtr->isLocked = false;
scanFSMPtr->isBmRcved = false; scanFSMPtr->isBmRcved = false;
@@ -865,7 +931,8 @@ void ScanNet_Reset(L2_SCAN_FSM* scanFSMPtr) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void ScanNet_Destroy(L2_SCAN_FSM* scanFSMPtr) { void ScanNet_Destroy(L2_SCAN_FSM *scanFSMPtr)
{
free(scanFSMPtr); free(scanFSMPtr);
} }
@@ -878,15 +945,18 @@ void ScanNet_Destroy(L2_SCAN_FSM* scanFSMPtr) {
* @return used slot (5ms slot). * @return used slot (5ms slot).
* *
******************************************************************************/ ******************************************************************************/
int32_t ScanNet_Proc(L2_INST* instPtr) { int32_t ScanNet_Proc(L2_INST *instPtr)
{
L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM; L2_SCAN_FSM *scanFsmPtr = instPtr->scanNetFSM;
if (L2_GetNodeType(instPtr) == L2_NODE_HEAD) { if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
{
DBG_ERROR("Only member node needs to execute ScanNet_Proc. \n"); DBG_ERROR("Only member node needs to execute ScanNet_Proc. \n");
return FAILURE; return FAILURE;
} }
int32_t Status; int32_t Status;
DBG_INFO("-------- ScanNet Process --------\n"); DBG_INFO("-------- ScanNet Process --------\n");
switch (scanFsmPtr->scanStage) { switch (scanFsmPtr->scanStage)
{
case L2_SCAN_NONE_STAGE: case L2_SCAN_NONE_STAGE:
/* Start Scan. */ /* Start Scan. */
StageUpdate(scanFsmPtr, L2_SCAN_INIT_STAGE); StageUpdate(scanFsmPtr, L2_SCAN_INIT_STAGE);
@@ -920,47 +990,59 @@ int32_t ScanNet_Proc(L2_INST* instPtr) {
* *
******************************************************************************/ ******************************************************************************/
int acqSend = 0; int acqSend = 0;
int32_t ScanNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry) { int32_t ScanNet_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
L2_TOR_TYPE torState; L2_TOR_TYPE torState;
int32_t Status; int32_t Status;
DBG_INFO("-------- ScanNet Routine --------\n"); DBG_INFO("-------- ScanNet Routine --------\n");
if(rtneEntry->slotSeq != 0){ if (rtneEntry->slotSeq != 0)
{
return SQUENCE_SCAN_SLOTS; return SQUENCE_SCAN_SLOTS;
} }
if(L2_GetNodeType(instPtr) != L2_NODE_HEAD){ if (L2_GetNodeType(instPtr) != L2_NODE_HEAD)
if (rtneEntry->dst0 == L2_GetNodeId(instPtr) || rtneEntry->dst0 == L2_BROADCAST_ID){ {
if (rtneEntry->dst0 == L2_GetNodeId(instPtr) || rtneEntry->dst0 == L2_BROADCAST_ID)
{
torState = L2_TOR_RX; torState = L2_TOR_RX;
} }
else if (rtneEntry->src0 == L2_GetNodeId(instPtr) || rtneEntry->src0 == L2_BROADCAST_ID){ else if (rtneEntry->src0 == L2_GetNodeId(instPtr) || rtneEntry->src0 == L2_BROADCAST_ID)
{
torState = L2_TOR_TX; torState = L2_TOR_TX;
} }
else { else
{
torState = L2_TOR_SUSPEND; torState = L2_TOR_SUSPEND;
} }
} }
else{ else
if (rtneEntry->dst0 == L2_GetNodeId(instPtr)){ {
if (rtneEntry->dst0 == L2_GetNodeId(instPtr))
{
torState = L2_TOR_RX; torState = L2_TOR_RX;
} }
else if (rtneEntry->src0 == L2_GetNodeId(instPtr)){ else if (rtneEntry->src0 == L2_GetNodeId(instPtr))
{
torState = L2_TOR_TX; torState = L2_TOR_TX;
} }
else { else
{
torState = L2_TOR_SUSPEND; torState = L2_TOR_SUSPEND;
} }
} }
switch (rtneEntry->slotType) { switch (rtneEntry->slotType)
{
case L2_SLOT_INTER_SCAN_ACQ: case L2_SLOT_INTER_SCAN_ACQ:
// TODO: // TODO:
L2_SetupNextSlot(instPtr, L2_TOR_SUSPEND, L2_INTER_SCAN_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_SUSPEND, L2_INTER_SCAN_FRAME);
break; break;
case L2_SLOT_INTRA_SCAN_ACQ: case L2_SLOT_INTRA_SCAN_ACQ:
if (L2_GetNodeType(instPtr) != L2_NODE_HEAD && torState == L2_TOR_TX) { if (L2_GetNodeType(instPtr) != L2_NODE_HEAD && torState == L2_TOR_TX)
{
DBG_ERROR("Only head node can send init-scan Frame.\n"); DBG_ERROR("Only head node can send init-scan Frame.\n");
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
@@ -974,14 +1056,17 @@ int32_t ScanNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry)
break; break;
case L2_SLOT_INTRA_SCAN_BM: case L2_SLOT_INTRA_SCAN_BM:
if (torState == L2_TOR_TX) { if (torState == L2_TOR_TX)
if (L2_GetNodeType(instPtr) != L2_NODE_HEAD) { {
if (L2_GetNodeType(instPtr) != L2_NODE_HEAD)
{
DBG_ERROR("Only head node can send BM Frame.\n"); DBG_ERROR("Only head node can send BM Frame.\n");
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
} }
memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX); memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX);
if(ScanPkt_Packing(instPtr, SCANPKT_INTRA_BM) != SUCCESS) { if (ScanPkt_Packing(instPtr, SCANPKT_INTRA_BM) != SUCCESS)
{
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
} }
@@ -994,15 +1079,18 @@ int32_t ScanNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry)
L2_SetupNextSlot(instPtr, L2_TOR_SUSPEND, L2_INTER_W2_ACQ_FRAME); L2_SetupNextSlot(instPtr, L2_TOR_SUSPEND, L2_INTER_W2_ACQ_FRAME);
break; break;
case L2_SLOT_INTRA_SCAN_2W: case L2_SLOT_INTRA_SCAN_2W:
if (torState == L2_TOR_TX) { if (torState == L2_TOR_TX)
if(L2_GetNodeType(instPtr) != L2_NODE_HEAD && L2_isDataAvail(instPtr) == false) { {
if (L2_GetNodeType(instPtr) != L2_NODE_HEAD && L2_isDataAvail(instPtr) == false)
{
// skip... // skip...
break; break;
} }
/* Prepare Request/ACK data. */ /* Prepare Request/ACK data. */
memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX); memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX);
/* Pack message words */ /* Pack message words */
if(ScanPkt_Packing(instPtr, SCANPKT_INTRA_2WACQ) != SUCCESS) { if (ScanPkt_Packing(instPtr, SCANPKT_INTRA_2WACQ) != SUCCESS)
{
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
} }
@@ -1021,4 +1109,3 @@ int32_t ScanNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry)
routine_err: routine_err:
return Status; return Status;
} }
+49 -29
View File
@@ -22,7 +22,6 @@
#include "../error.h" #include "../error.h"
#include "../common.h" #include "../common.h"
#ifdef _MPC_CTRL #ifdef _MPC_CTRL
#endif #endif
@@ -59,9 +58,11 @@
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int UduHeader_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, uint8_t dst, uint32_t dataLen){ static int UduHeader_Packing(L2_INST *instPtr, BIT_ARRAY *bitarr, uint8_t dst, uint32_t dataLen)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
if(bitarr->num_of_bits < UDUH_LEN){ if (bitarr->num_of_bits < UDUH_LEN)
{
return EPARAM; return EPARAM;
} }
uint64_t dataChunk = 0; uint64_t dataChunk = 0;
@@ -107,14 +108,18 @@ static int UduHeader_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, uint8_t dst, u
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int PduHeader_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_PDU_HEADER_TYPE headerType, uint8_t dst){ static int PduHeader_Packing(L2_INST *instPtr, BIT_ARRAY *bitarr, L2_PDU_HEADER_TYPE headerType, uint8_t dst)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
if(headerType < L2_PDU_HEADER_0 || headerType > L2_PDU_HEADER_1){ if (headerType < L2_PDU_HEADER_0 || headerType > L2_PDU_HEADER_1)
{
return EPARAM; return EPARAM;
} }
uint64_t dataChunk = 0; uint64_t dataChunk = 0;
if(headerType == L2_PDU_HEADER_0){ if (headerType == L2_PDU_HEADER_0)
if(bitarr->num_of_bits < PDUH0_LEN){ {
if (bitarr->num_of_bits < PDUH0_LEN)
{
return EPARAM; return EPARAM;
} }
// PDU type // PDU type
@@ -135,8 +140,10 @@ static int PduHeader_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_PDU_HEADER_
// Append CRC // Append CRC
L2_CrcAppend((uint8_t *)bitarr->words, PDUH0_HEADER_CRC_OFFSET / 8, PDUH0_HEADER_CRC_OFFSET / 8, L2_CRC8_31); L2_CrcAppend((uint8_t *)bitarr->words, PDUH0_HEADER_CRC_OFFSET / 8, PDUH0_HEADER_CRC_OFFSET / 8, L2_CRC8_31);
} }
else if(headerType == L2_PDU_HEADER_1){ else if (headerType == L2_PDU_HEADER_1)
if(bitarr->num_of_bits < PDUH1_LEN){ {
if (bitarr->num_of_bits < PDUH1_LEN)
{
return EPARAM; return EPARAM;
} }
// PDU type // PDU type
@@ -155,7 +162,8 @@ static int PduHeader_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_PDU_HEADER_
// Append CRC // Append CRC
L2_CrcAppend((uint8_t *)bitarr->words, PDUH1_HEADER_CRC_OFFSET / 8, PDUH1_HEADER_CRC_OFFSET / 8, L2_CRC8_31); L2_CrcAppend((uint8_t *)bitarr->words, PDUH1_HEADER_CRC_OFFSET / 8, PDUH1_HEADER_CRC_OFFSET / 8, L2_CRC8_31);
} }
else{ else
{
return FAILURE; return FAILURE;
} }
return SUCCESS; return SUCCESS;
@@ -171,23 +179,26 @@ static int PduHeader_Packing(L2_INST* instPtr, BIT_ARRAY* bitarr, L2_PDU_HEADER_
* Other Initialize failed. * Other Initialize failed.
* *
******************************************************************************/ ******************************************************************************/
int32_t ServiceNet_Init(L2_SVC_FSM** serviceFSM) { int32_t ServiceNet_Init(L2_SVC_FSM **serviceFSM)
{
(*serviceFSM) = (L2_SVC_FSM *)malloc(sizeof(L2_SVC_FSM)); (*serviceFSM) = (L2_SVC_FSM *)malloc(sizeof(L2_SVC_FSM));
if (*serviceFSM == NULL) { if (*serviceFSM == NULL)
{
return EALLOC; return EALLOC;
} }
ServiceNet_Reset(*serviceFSM); ServiceNet_Reset(*serviceFSM);
return SUCCESS; return SUCCESS;
} }
void ServiceNet_Reset(L2_SVC_FSM *serviceFSM)
void ServiceNet_Reset(L2_SVC_FSM* serviceFSM) { {
serviceFSM->isBisar = false; serviceFSM->isBisar = false;
serviceFSM->isBisarEven = false; serviceFSM->isBisarEven = false;
serviceFSM->bisarDst = L2_NONE_ID; serviceFSM->bisarDst = L2_NONE_ID;
} }
void ServiceNet_Destroy(L2_SVC_FSM* serviceFSM){ void ServiceNet_Destroy(L2_SVC_FSM *serviceFSM)
{
free(serviceFSM); free(serviceFSM);
} }
/*****************************************************************************/ /*****************************************************************************/
@@ -200,7 +211,8 @@ void ServiceNet_Destroy(L2_SVC_FSM* serviceFSM){
* other error occurred. * other error occurred.
* *
******************************************************************************/ ******************************************************************************/
int32_t ServiceNet_PktHandler(L2_INST* instPtr) { int32_t ServiceNet_PktHandler(L2_INST *instPtr)
{
// TODO: // TODO:
DBG_INFO("Scan net handling packet...\n"); DBG_INFO("Scan net handling packet...\n");
@@ -217,42 +229,51 @@ int32_t ServiceNet_PktHandler(L2_INST* instPtr) {
* @return Slot has been used. * @return Slot has been used.
* *
******************************************************************************/ ******************************************************************************/
int32_t ServiceNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntry) { int32_t ServiceNet_Routine(L2_INST *instPtr, const L2_RTNE_TABLE_ENTRY *rtneEntry)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status; int32_t Status;
L2_TOR_TYPE torState; L2_TOR_TYPE torState;
DBG_INFO("-------- ServiceNet Routine --------\n"); DBG_INFO("-------- ServiceNet Routine --------\n");
if(rtneEntry->slotSeq != 0){ if (rtneEntry->slotSeq != 0)
{
return SQUENCE_SVC_SLOTS; return SQUENCE_SVC_SLOTS;
} }
// Determine TOR State. // Determine TOR State.
if(rtneEntry->src0 == L2_GetNodeId(instPtr)){ if (rtneEntry->src0 == L2_GetNodeId(instPtr))
{
torState = L2_TOR_TX; torState = L2_TOR_TX;
} }
else if(rtneEntry->dst0 == L2_GetNodeId(instPtr)){ else if (rtneEntry->dst0 == L2_GetNodeId(instPtr))
{
torState = L2_TOR_RX; torState = L2_TOR_RX;
} }
else{ else
{
torState = L2_TOR_SUSPEND; torState = L2_TOR_SUSPEND;
} }
BIT_ARRAY *svcMsg = bit_array_create(L2_MSG_MAX); BIT_ARRAY *svcMsg = bit_array_create(L2_MSG_MAX);
if(svcMsg == NULL){ if (svcMsg == NULL)
{
DBG_ERROR("Failed to allocate memory for svcMsg.\n"); DBG_ERROR("Failed to allocate memory for svcMsg.\n");
return FAILURE; return FAILURE;
} }
switch(rtneEntry->slotType){ switch (rtneEntry->slotType)
{
case L2_SLOT_INTER_SVC: case L2_SLOT_INTER_SVC:
L2_SetupNextSlot(instPtr, torState, L2_INTER_SVC_FRAME); L2_SetupNextSlot(instPtr, torState, L2_INTER_SVC_FRAME);
break; break;
case L2_SLOT_INTRA_SVC: case L2_SLOT_INTRA_SVC:
if(torState == L2_TOR_TX){ if (torState == L2_TOR_TX)
{
// TODO: // TODO:
memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX); memset(instPtr->sendPktData, 0xAA, L2_PACKET_LEN_MAX);
// Pack PDU header // Pack PDU header
if(PduHeader_Packing(instPtr, svcMsg, L2_PDU_HEADER_0, rtneEntry->dst0) != SUCCESS){ if (PduHeader_Packing(instPtr, svcMsg, L2_PDU_HEADER_0, rtneEntry->dst0) != SUCCESS)
{
DBG_ERROR("Failed to packing PduHeader.\n"); DBG_ERROR("Failed to packing PduHeader.\n");
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
@@ -260,8 +281,8 @@ int32_t ServiceNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntr
memcpy(instPtr->sendPktData, svcMsg->words, (size_t)(PDUH0_LEN / 8)); memcpy(instPtr->sendPktData, svcMsg->words, (size_t)(PDUH0_LEN / 8));
// Pack UDU Header // Pack UDU Header
if (UduHeader_Packing(instPtr, svcMsg, rtneEntry->dst0, if (UduHeader_Packing(instPtr, svcMsg, rtneEntry->dst0,
INTRA_SVC_FRAME_LENGTH_MAX-(PDUH0_LEN+MSG_KEY_LENGTH_LEN)/8) INTRA_SVC_FRAME_LENGTH_MAX - (PDUH0_LEN + MSG_KEY_LENGTH_LEN) / 8) != SUCCESS)
!= SUCCESS){ {
DBG_ERROR("Failed to packing UduHeader.\n"); DBG_ERROR("Failed to packing UduHeader.\n");
Status = FAILURE; Status = FAILURE;
goto routine_err; goto routine_err;
@@ -274,8 +295,7 @@ int32_t ServiceNet_Routine(L2_INST* instPtr, const L2_RTNE_TABLE_ENTRY* rtneEntr
L2_CrcAppend(instPtr->sendPktData + ((PDUH0_LEN + MSG_KEY_LENGTH_LEN) / 8), L2_CrcAppend(instPtr->sendPktData + ((PDUH0_LEN + MSG_KEY_LENGTH_LEN) / 8),
INTRA_SVC_FRAME_LENGTH_MAX - (PDUH0_LEN + MSG_KEY_LENGTH_LEN + PDU_CRC_LEN) / 8, INTRA_SVC_FRAME_LENGTH_MAX - (PDUH0_LEN + MSG_KEY_LENGTH_LEN + PDU_CRC_LEN) / 8,
INTRA_SVC_FRAME_LENGTH_MAX - (PDUH0_LEN + MSG_KEY_LENGTH_LEN + PDU_CRC_LEN) / 8, INTRA_SVC_FRAME_LENGTH_MAX - (PDUH0_LEN + MSG_KEY_LENGTH_LEN + PDU_CRC_LEN) / 8,
L2_CRC16_8005 L2_CRC16_8005);
);
} }
L2_SetupNextSlot(instPtr, torState, L2_INTRA_SVC_FRAME); L2_SetupNextSlot(instPtr, torState, L2_INTRA_SVC_FRAME);
break; break;
+102 -57
View File
@@ -26,7 +26,8 @@
#include "../error.h" #include "../error.h"
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
inline uint32_t L2_GetNodeListLen(NODE_LIST *list) { inline uint32_t L2_GetNodeListLen(NODE_LIST *list)
{
ASSERT(list != NULL); ASSERT(list != NULL);
return list->len; return list->len;
} }
@@ -38,14 +39,16 @@ static NODE_ENTRY util_entry;
/** /**
* @brief helper function to free node entry. * @brief helper function to free node entry.
*/ */
static void entry_free(void* val){ static void entry_free(void *val)
{
free(val); free(val);
} }
/** /**
* @brief helper function to match node entries by nodeIds * @brief helper function to match node entries by nodeIds
*/ */
static int8_t entry_compare(void* a, void* b){ static int8_t entry_compare(void *a, void *b)
{
ASSERT(a != NULL); ASSERT(a != NULL);
ASSERT(b != NULL); ASSERT(b != NULL);
register uint8_t idA, idB; register uint8_t idA, idB;
@@ -55,13 +58,14 @@ static int8_t entry_compare(void* a, void* b){
{ {
return 0; return 0;
} }
else if(idA < idB) { else if (idA < idB)
{
return -1; return -1;
} }
else { else
{
return 1; return 1;
} }
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
@@ -73,9 +77,11 @@ static int8_t entry_compare(void* a, void* b){
* ENOMEM - not enough memory. * ENOMEM - not enough memory.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_NodeList_Init(NODE_LIST **list) { int32_t L2_NodeList_Init(NODE_LIST **list)
{
(*list) = list_new(); (*list) = list_new();
if (*list == NULL) { if (*list == NULL)
{
return ENOMEM; return ENOMEM;
} }
(*list)->free = entry_free; (*list)->free = entry_free;
@@ -91,7 +97,8 @@ int32_t L2_NodeList_Init(NODE_LIST **list) {
*NONE *NONE
* *
******************************************************************************/ ******************************************************************************/
void L2_NodeList_Clear(NODE_LIST *list){ void L2_NodeList_Clear(NODE_LIST *list)
{
ASSERT(list != NULL); ASSERT(list != NULL);
list_clear(list); list_clear(list);
} }
@@ -105,7 +112,8 @@ void L2_NodeList_Clear(NODE_LIST *list){
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void L2_NodeList_Destroy(NODE_LIST *list){ void L2_NodeList_Destroy(NODE_LIST *list)
{
list_destroy(list); list_destroy(list);
} }
/*****************************************************************************/ /*****************************************************************************/
@@ -119,20 +127,21 @@ void L2_NodeList_Destroy(NODE_LIST *list){
* NULL if node dose not exist. * NULL if node dose not exist.
* *
******************************************************************************/ ******************************************************************************/
NODE_ENTRY* L2_GetNodeById(NODE_LIST *list, uint8_t nodeId) { NODE_ENTRY *L2_GetNodeById(NODE_LIST *list, uint8_t nodeId)
{
ASSERT(list != NULL); ASSERT(list != NULL);
util_entry.nodeInfo.nodeId = nodeId; util_entry.nodeInfo.nodeId = nodeId;
list_node_t *node = list_find(list, &util_entry); list_node_t *node = list_find(list, &util_entry);
if(node == NULL) { if (node == NULL)
{
return NULL; return NULL;
} }
else { else
{
return ((NODE_ENTRY *)node->val); return ((NODE_ENTRY *)node->val);
} }
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Append a new node entry to the node list * @brief Append a new node entry to the node list
@@ -148,18 +157,21 @@ NODE_ENTRY* L2_GetNodeById(NODE_LIST *list, uint8_t nodeId) {
NODE_ENTRY *L2_AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType) NODE_ENTRY *L2_AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType)
{ {
ASSERT(list != NULL); ASSERT(list != NULL);
if(L2_isNodeInList(list, nodeId)){ if (L2_isNodeInList(list, nodeId))
{
DBG_WARNING("Node [%d] already exist.\n", nodeId); DBG_WARNING("Node [%d] already exist.\n", nodeId);
return NULL; return NULL;
} }
DBG_INFO("Link layer appending node [%d] to node list.\n", nodeId); DBG_INFO("Link layer appending node [%d] to node list.\n", nodeId);
NODE_ENTRY *nodeEntry = (NODE_ENTRY *)malloc(sizeof(NODE_ENTRY)); NODE_ENTRY *nodeEntry = (NODE_ENTRY *)malloc(sizeof(NODE_ENTRY));
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
DBG_ERROR("Failed to allocated memory for nodeEntry.\n"); DBG_ERROR("Failed to allocated memory for nodeEntry.\n");
return NULL; return NULL;
} }
list_node_t *node = list_node_new(nodeEntry); list_node_t *node = list_node_new(nodeEntry);
if(node == NULL) { if (node == NULL)
{
DBG_ERROR("Failed to allocated memory for list node.\n"); DBG_ERROR("Failed to allocated memory for list node.\n");
free(nodeEntry); free(nodeEntry);
return NULL; return NULL;
@@ -189,11 +201,13 @@ NODE_ENTRY* L2_AppendNode(NODE_LIST *list, uint8_t nodeId, L2_NODE_TYPE nodeType
* Other Node not exist. * Other Node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_DeleteNodeById(NODE_LIST *list, uint8_t nodeId) { int32_t L2_DeleteNodeById(NODE_LIST *list, uint8_t nodeId)
{
ASSERT(list != NULL); ASSERT(list != NULL);
util_entry.nodeInfo.nodeId = nodeId; util_entry.nodeInfo.nodeId = nodeId;
list_node_t *node = list_find(list, &util_entry); list_node_t *node = list_find(list, &util_entry);
if(node == NULL) { if (node == NULL)
{
DBG_WARNING("Node [%d] in not in node list, skip deleting process.\n", nodeId); DBG_WARNING("Node [%d] in not in node list, skip deleting process.\n", nodeId);
return SUCCESS; return SUCCESS;
} }
@@ -213,7 +227,8 @@ int32_t L2_DeleteNodeById(NODE_LIST *list, uint8_t nodeId) {
* false node not in the list * false node not in the list
* *
******************************************************************************/ ******************************************************************************/
bool L2_isNodeInList(NODE_LIST *list, uint8_t nodeId) { bool L2_isNodeInList(NODE_LIST *list, uint8_t nodeId)
{
ASSERT(list != NULL); ASSERT(list != NULL);
return (L2_GetNodeById(list, nodeId) != NULL); return (L2_GetNodeById(list, nodeId) != NULL);
} }
@@ -231,13 +246,16 @@ bool L2_isNodeInList(NODE_LIST *list, uint8_t nodeId) {
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double* dst) { int32_t L2_GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double *dst)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
*dst = Coord_Distance(myCoord, nodeEntry->nodeInfo.pos); *dst = Coord_Distance(myCoord, nodeEntry->nodeInfo.pos);
return SUCCESS; return SUCCESS;
} }
@@ -255,19 +273,21 @@ int32_t L2_GetDistance(NODE_LIST *list, COORD myCoord, uint8_t nodeId, double* d
* FAILURE node not exist. * FAILURE node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos) { int32_t L2_UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
memcpy(&(nodeEntry->nodeInfo.pos), newPos, sizeof(COORD)); memcpy(&(nodeEntry->nodeInfo.pos), newPos, sizeof(COORD));
} }
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Get node positions information. * @brief Get node positions information.
@@ -280,13 +300,16 @@ int32_t L2_UpdateNodePos(NODE_LIST *list, uint8_t nodeId, const COORD *newPos) {
* FAILURE node not exist. * FAILURE node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *pos) { int32_t L2_GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *pos)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
memcpy(pos, &(nodeEntry->nodeInfo.pos), sizeof(COORD)); memcpy(pos, &(nodeEntry->nodeInfo.pos), sizeof(COORD));
} }
return SUCCESS; return SUCCESS;
@@ -304,19 +327,21 @@ int32_t L2_GetNodePos(NODE_LIST *list, uint8_t nodeId, COORD *pos) {
* FAILURE node not exist. * FAILURE node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpeed) { int32_t L2_UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpeed)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
memcpy(&(nodeEntry->nodeInfo.speed), newSpeed, sizeof(COORD)); memcpy(&(nodeEntry->nodeInfo.speed), newSpeed, sizeof(COORD));
} }
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Get node speed information. * @brief Get node speed information.
@@ -329,13 +354,16 @@ int32_t L2_UpdateNodeSpeed(NODE_LIST *list, uint8_t nodeId, const COORD *newSpee
* FAILURE node not exist. * FAILURE node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed) { int32_t L2_GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
memcpy(speed, &(nodeEntry->nodeInfo.speed), sizeof(COORD)); memcpy(speed, &(nodeEntry->nodeInfo.speed), sizeof(COORD));
} }
return SUCCESS; return SUCCESS;
@@ -352,13 +380,16 @@ int32_t L2_GetNodeSpeed(NODE_LIST *list, uint8_t nodeId, COORD *speed) {
* FAILURE Node not exist. * FAILURE Node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi) { int32_t L2_UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
nodeEntry->timeDevi = devi; nodeEntry->timeDevi = devi;
} }
return SUCCESS; return SUCCESS;
@@ -375,13 +406,16 @@ int32_t L2_UpdateNodeTimeDeviation(NODE_LIST *list, uint8_t nodeId, int32_t devi
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc) { int32_t L2_IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
nodeEntry->ttlCnt += inc; nodeEntry->ttlCnt += inc;
} }
return SUCCESS; return SUCCESS;
@@ -398,13 +432,16 @@ int32_t L2_IncNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t inc) {
* FAILURE Node not exist. * FAILURE Node not exist.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_RstNodeTTL(NODE_LIST *list, uint8_t nodeId) { int32_t L2_RstNodeTTL(NODE_LIST *list, uint8_t nodeId)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
nodeEntry->ttlCnt = 0; nodeEntry->ttlCnt = 0;
} }
return SUCCESS; return SUCCESS;
@@ -421,13 +458,16 @@ int32_t L2_RstNodeTTL(NODE_LIST *list, uint8_t nodeId) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl) { int32_t L2_GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl)
{
ASSERT(list != NULL); ASSERT(list != NULL);
NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId); NODE_ENTRY *nodeEntry = L2_GetNodeById(list, nodeId);
if (nodeEntry == NULL) { if (nodeEntry == NULL)
{
return FAILURE; return FAILURE;
} }
else { else
{
*ttl = nodeEntry->ttlCnt; *ttl = nodeEntry->ttlCnt;
} }
return SUCCESS; return SUCCESS;
@@ -446,19 +486,22 @@ int32_t L2_GetNodeTTL(NODE_LIST *list, uint8_t nodeId, uint32_t *ttl) {
* Other Error occurred. * Other Error occurred.
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)(NODE_ENTRY *, const void *)) { int32_t L2_IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)(NODE_ENTRY *, const void *))
{
ASSERT(list != NULL); ASSERT(list != NULL);
int32_t Status; int32_t Status;
list_node_t *node; list_node_t *node;
list_iterator_t *iter = list_iterator_new(list, LIST_HEAD); list_iterator_t *iter = list_iterator_new(list, LIST_HEAD);
if(iter == NULL) { if (iter == NULL)
{
DBG_ERROR("Failed to initialize list iteration process.\n"); DBG_ERROR("Failed to initialize list iteration process.\n");
return EALLOC; return EALLOC;
} }
while ((node = list_iterator_next(iter))) while ((node = list_iterator_next(iter)))
{ {
Status = operation((NODE_ENTRY *)node->val, param); Status = operation((NODE_ENTRY *)node->val, param);
if(Status != SUCCESS){ if (Status != SUCCESS)
{
DBG_ERROR("Error occurred when iteratively edit node entry.\n"); DBG_ERROR("Error occurred when iteratively edit node entry.\n");
break; break;
} }
@@ -478,21 +521,23 @@ int32_t L2_IterEditNode(NODE_LIST *list, const void *param, int32_t (*operation)
* @return SUCCESS * @return SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_IterDeleteNode(NODE_LIST *list, bool (*condition)(NODE_ENTRY *)) { int32_t L2_IterDeleteNode(NODE_LIST *list, bool (*condition)(NODE_ENTRY *))
{
ASSERT(list != NULL); ASSERT(list != NULL);
list_node_t *node; list_node_t *node;
list_iterator_t *iter = list_iterator_new(list, LIST_HEAD); list_iterator_t *iter = list_iterator_new(list, LIST_HEAD);
if(iter == NULL) { if (iter == NULL)
{
DBG_ERROR("Failed to initialize list iteration process.\n"); DBG_ERROR("Failed to initialize list iteration process.\n");
return EALLOC; return EALLOC;
} }
while ((node = list_iterator_next(iter))) while ((node = list_iterator_next(iter)))
{ {
if (condition((NODE_ENTRY*) node->val) == false) { if (condition((NODE_ENTRY *)node->val) == false)
{
list_remove(list, node); list_remove(list, node);
} }
} }
list_iterator_destroy(iter); list_iterator_destroy(iter);
return SUCCESS; return SUCCESS;
} }
+2 -1
View File
@@ -29,7 +29,8 @@
/************************** Constant Definitions *****************************/ /************************** Constant Definitions *****************************/
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef struct NODE_LIST_ENTRY { typedef struct NODE_LIST_ENTRY
{
L2_NODE_INFO nodeInfo; L2_NODE_INFO nodeInfo;
uint32_t ttlCnt; uint32_t ttlCnt;
int32_t timeDevi; int32_t timeDevi;
+6 -3
View File
@@ -29,7 +29,8 @@
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
// TODO: Multi-node RTT // TODO: Multi-node RTT
typedef struct { typedef struct
{
TIME_WORD Tt1; TIME_WORD Tt1;
TIME_WORD Tr1; TIME_WORD Tr1;
TIME_WORD Tt2; TIME_WORD Tt2;
@@ -48,7 +49,8 @@ typedef struct {
TIME_WORD TreplyB; TIME_WORD TreplyB;
} RTT_RECORD; } RTT_RECORD;
typedef enum { typedef enum
{
RTT_NONE_STAGE = 0, RTT_NONE_STAGE = 0,
RTT_REQ1 = 1, RTT_REQ1 = 1,
RTT_RSP1 = 2, RTT_RSP1 = 2,
@@ -56,7 +58,8 @@ typedef enum {
RTT_RSP2 = 4, // Rtt Stages RTT_RSP2 = 4, // Rtt Stages
} L2_RTT_STGE; } L2_RTT_STGE;
typedef struct { typedef struct
{
L2_RTT_STGE rttStage; L2_RTT_STGE rttStage;
bool isR1Valid; bool isR1Valid;
bool isR2Valid; bool isR2Valid;
+76 -40
View File
@@ -29,8 +29,10 @@ static L2_SCHD_EVENT util_schdevent;
/** /**
* @brief helper function to free schedule events. * @brief helper function to free schedule events.
*/ */
static void schdevent_free(void* val){ static void schdevent_free(void *val)
if(val == NULL) { {
if (val == NULL)
{
return; return;
} }
free(((L2_SCHD_EVENT *)val)->l2Event.msg); free(((L2_SCHD_EVENT *)val)->l2Event.msg);
@@ -40,19 +42,23 @@ static void schdevent_free(void* val){
/** /**
* @brief helper function to match events by ids. * @brief helper function to match events by ids.
*/ */
static int8_t schdevent_compare(void* a, void* b){ static int8_t schdevent_compare(void *a, void *b)
{
ASSERT(a == NULL); ASSERT(a == NULL);
ASSERT(b != NULL); ASSERT(b != NULL);
register uint32_t idA, idB; register uint32_t idA, idB;
idA = ((L2_SCHD_EVENT *)a)->eventId; idA = ((L2_SCHD_EVENT *)a)->eventId;
idB = ((L2_SCHD_EVENT *)b)->eventId; idB = ((L2_SCHD_EVENT *)b)->eventId;
if( idA== idB) { if (idA == idB)
{
return 0; return 0;
} }
else if (idA < idB) { else if (idA < idB)
{
return -1; return -1;
} }
else { else
{
return 1; return 1;
} }
} }
@@ -66,14 +72,17 @@ static int8_t schdevent_compare(void* a, void* b){
* @return SUCCESS * @return SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_CreateSchedEventPool(L2_SCHD_POOL** schdPool) { int32_t L2_CreateSchedEventPool(L2_SCHD_POOL **schdPool)
{
(*schdPool) = malloc(sizeof(L2_SCHD_POOL)); (*schdPool) = malloc(sizeof(L2_SCHD_POOL));
if(*schdPool == NULL) { if (*schdPool == NULL)
{
DBG_ERROR("Failed to allocated memory for schdPool.\n"); DBG_ERROR("Failed to allocated memory for schdPool.\n");
return EALLOC; return EALLOC;
} }
(*schdPool)->schdList = list_new(); (*schdPool)->schdList = list_new();
if((*schdPool)->schdList == NULL) { if ((*schdPool)->schdList == NULL)
{
DBG_ERROR("Failed to allocated memory for schdPool->schdList.\n"); DBG_ERROR("Failed to allocated memory for schdPool->schdList.\n");
free(*schdPool); free(*schdPool);
return EALLOC; return EALLOC;
@@ -94,7 +103,8 @@ int32_t L2_CreateSchedEventPool(L2_SCHD_POOL** schdPool) {
* @return SUCCESS * @return SUCCESS
* *
******************************************************************************/ ******************************************************************************/
void L2_ClearSchedEventPool(L2_SCHD_POOL* schdPool) { void L2_ClearSchedEventPool(L2_SCHD_POOL *schdPool)
{
ASSERT(schdPool != NULL); ASSERT(schdPool != NULL);
list_clear(schdPool->schdList); list_clear(schdPool->schdList);
schdPool->envCnt = 0; schdPool->envCnt = 0;
@@ -109,8 +119,10 @@ void L2_ClearSchedEventPool(L2_SCHD_POOL* schdPool) {
* @return SUCCESS * @return SUCCESS
* *
******************************************************************************/ ******************************************************************************/
void L2_DestroySchedEventPool(L2_SCHD_POOL* schdPool) { void L2_DestroySchedEventPool(L2_SCHD_POOL *schdPool)
if(schdPool == NULL) { {
if (schdPool == NULL)
{
return; return;
} }
list_destroy(schdPool->schdList); list_destroy(schdPool->schdList);
@@ -129,19 +141,23 @@ void L2_DestroySchedEventPool(L2_SCHD_POOL* schdPool) {
* Pointer to schedule event. * Pointer to schedule event.
* *
******************************************************************************/ ******************************************************************************/
L2_SCHD_EVENT* L2_CreatNewSchdEvent(L2_SCHD_POOL* schdPool, L2_EVENT_TYPE event, bool isVolatile){ L2_SCHD_EVENT *L2_CreatNewSchdEvent(L2_SCHD_POOL *schdPool, L2_EVENT_TYPE event, bool isVolatile)
{
ASSERT(schdPool != NULL); ASSERT(schdPool != NULL);
if(event < L2_EV_SCAN_PROC_DONE || event > L2_ROUTINE_UPDATE){ if (event < L2_EV_SCAN_PROC_DONE || event > L2_ROUTINE_UPDATE)
{
DBG_ERROR("Unknown event type: %d.\n", event); DBG_ERROR("Unknown event type: %d.\n", event);
return NULL; return NULL;
} }
L2_SCHD_EVENT *schdEvent = (L2_SCHD_EVENT *)malloc(sizeof(L2_SCHD_EVENT)); L2_SCHD_EVENT *schdEvent = (L2_SCHD_EVENT *)malloc(sizeof(L2_SCHD_EVENT));
if(schdEvent == NULL){ if (schdEvent == NULL)
{
DBG_ERROR("Failed to allocate schedule event.\n"); DBG_ERROR("Failed to allocate schedule event.\n");
return NULL; return NULL;
} }
list_node_t *node = list_node_new(schdEvent); list_node_t *node = list_node_new(schdEvent);
if(node == NULL) { if (node == NULL)
{
DBG_ERROR("Failed to allocate schedule event list node.\n"); DBG_ERROR("Failed to allocate schedule event list node.\n");
free(schdEvent); free(schdEvent);
return NULL; return NULL;
@@ -169,11 +185,13 @@ L2_SCHD_EVENT* L2_CreatNewSchdEvent(L2_SCHD_POOL* schdPool, L2_EVENT_TYPE event,
* Pointer to the schedule event. * Pointer to the schedule event.
* *
******************************************************************************/ ******************************************************************************/
L2_SCHD_EVENT* L2_GetEventById(L2_SCHD_POOL* schdPool, uint32_t id) { L2_SCHD_EVENT *L2_GetEventById(L2_SCHD_POOL *schdPool, uint32_t id)
{
ASSERT(schdPool != NULL); ASSERT(schdPool != NULL);
util_schdevent.eventId = id; util_schdevent.eventId = id;
list_node_t *node = list_find(schdPool->schdList, &util_schdevent); list_node_t *node = list_find(schdPool->schdList, &util_schdevent);
if(node != NULL) { if (node != NULL)
{
DBG_WARNING("Event with id=%d, dose not exist.\n", id); DBG_WARNING("Event with id=%d, dose not exist.\n", id);
return NULL; return NULL;
} }
@@ -192,11 +210,13 @@ L2_SCHD_EVENT* L2_GetEventById(L2_SCHD_POOL* schdPool, uint32_t id) {
* error code * error code
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_DeleteSchdEventById(L2_SCHD_POOL* schdPool, uint32_t id) { int32_t L2_DeleteSchdEventById(L2_SCHD_POOL *schdPool, uint32_t id)
{
ASSERT(schdPool != NULL); ASSERT(schdPool != NULL);
util_schdevent.eventId = id; util_schdevent.eventId = id;
list_node_t *node = list_find(schdPool->schdList, &util_schdevent); list_node_t *node = list_find(schdPool->schdList, &util_schdevent);
if(node != NULL) { if (node != NULL)
{
DBG_ERROR("Event with id=%d, dose not exist.\n", id); DBG_ERROR("Event with id=%d, dose not exist.\n", id);
return EIGNORE; return EIGNORE;
} }
@@ -217,13 +237,16 @@ int32_t L2_DeleteSchdEventById(L2_SCHD_POOL* schdPool, uint32_t id) {
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_ScheduleEventAt(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32_t frameNum, uint32_t slotNum){ int32_t L2_ScheduleEventAt(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t frameNum, uint32_t slotNum)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
if(L2_isEventScheduled(schdEvent) == true){ if (L2_isEventScheduled(schdEvent) == true)
{
DBG_ERROR("Event %d has already been scheduled.\n", schdEvent->eventId); DBG_ERROR("Event %d has already been scheduled.\n", schdEvent->eventId);
return FAILURE; return FAILURE;
} }
if(frameNum <= L2_GetCurrentFrameNum(instPtr) && slotNum < L2_GetCurrentSlotNum(instPtr)){ if (frameNum <= L2_GetCurrentFrameNum(instPtr) && slotNum < L2_GetCurrentSlotNum(instPtr))
{
DBG_ERROR("Cannot schedule event %d to the past(current: [%u-%u], schedule: [%u-%u]).\n", DBG_ERROR("Cannot schedule event %d to the past(current: [%u-%u], schedule: [%u-%u]).\n",
schdEvent->eventId, L2_GetCurrentFrameNum(instPtr), L2_GetCurrentSlotNum(instPtr), frameNum, slotNum); schdEvent->eventId, L2_GetCurrentFrameNum(instPtr), L2_GetCurrentSlotNum(instPtr), frameNum, slotNum);
return FAILURE; return FAILURE;
@@ -249,9 +272,11 @@ int32_t L2_ScheduleEventAt(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32_t
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_ScheduleEventAfter(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32_t numSlots){ int32_t L2_ScheduleEventAfter(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t numSlots)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
if(L2_isEventScheduled(schdEvent) == true){ if (L2_isEventScheduled(schdEvent) == true)
{
DBG_ERROR("Event %d has already been scheduled.\n", schdEvent->eventId); DBG_ERROR("Event %d has already been scheduled.\n", schdEvent->eventId);
return FAILURE; return FAILURE;
} }
@@ -273,9 +298,11 @@ int32_t L2_ScheduleEventAfter(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_RescheduleEventAt(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32_t frameNum, uint32_t slotNum){ int32_t L2_RescheduleEventAt(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t frameNum, uint32_t slotNum)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
if(schdEvent->isSchd == false){ if (schdEvent->isSchd == false)
{
DBG_WARNING("Event %d has not been scheduled yet.\n", schdEvent->eventId); DBG_WARNING("Event %d has not been scheduled yet.\n", schdEvent->eventId);
} }
schdEvent->isSchd = false; schdEvent->isSchd = false;
@@ -294,15 +321,16 @@ int32_t L2_RescheduleEventAt(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32_
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t L2_RescheduleEventAfter(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint32_t numSlots){ int32_t L2_RescheduleEventAfter(L2_INST *instPtr, L2_SCHD_EVENT *schdEvent, uint32_t numSlots)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
if(schdEvent->isSchd == false){ if (schdEvent->isSchd == false)
{
DBG_WARNING("Event %d has not been scheduled yet.\n", schdEvent->eventId); DBG_WARNING("Event %d has not been scheduled yet.\n", schdEvent->eventId);
} }
return L2_ScheduleEventAfter(instPtr, schdEvent, numSlots); return L2_ScheduleEventAfter(instPtr, schdEvent, numSlots);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Check and excute schedule event in event pool illiterate. * @brief Check and excute schedule event in event pool illiterate.
@@ -312,7 +340,8 @@ int32_t L2_RescheduleEventAfter(L2_INST* instPtr, L2_SCHD_EVENT* schdEvent, uint
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void L2_EventPoolHandler(L2_INST* instPtr){ void L2_EventPoolHandler(L2_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
list_node_t *node; list_node_t *node;
L2_SCHD_EVENT *event; L2_SCHD_EVENT *event;
@@ -326,16 +355,19 @@ void L2_EventPoolHandler(L2_INST* instPtr){
while ((node = list_iterator_next(iter))) while ((node = list_iterator_next(iter)))
{ {
event = (L2_SCHD_EVENT *)node->val; event = (L2_SCHD_EVENT *)node->val;
if(L2_isEventScheduled(event)){ if (L2_isEventScheduled(event))
if(event->schdFrame == L2_GetCurrentFrameNum(instPtr) {
&& event->schdSlot == L2_GetCurrentSlotNum(instPtr)){ if (event->schdFrame == L2_GetCurrentFrameNum(instPtr) && event->schdSlot == L2_GetCurrentSlotNum(instPtr))
{
DBG_INFO("Asserting scheduled event [%d].\n", event->eventId); DBG_INFO("Asserting scheduled event [%d].\n", event->eventId);
AssertL2Event(instPtr, &(event->l2Event)); AssertL2Event(instPtr, &(event->l2Event));
if(event->isVolatile == true) { if (event->isVolatile == true)
{
DBG_INFO("Deleting scheduled event [%d], since it is volatile.\n", event->eventId); DBG_INFO("Deleting scheduled event [%d], since it is volatile.\n", event->eventId);
list_remove(pool->schdList, node); list_remove(pool->schdList, node);
} }
else { else
{
event->occurCnt++; event->occurCnt++;
} }
} }
@@ -355,7 +387,8 @@ void L2_EventPoolHandler(L2_INST* instPtr){
* error code. * error code.
* *
******************************************************************************/ ******************************************************************************/
inline void L2_SchdEventAppendMsg(L2_SCHD_EVENT* schdEvent, void* msg){ inline void L2_SchdEventAppendMsg(L2_SCHD_EVENT *schdEvent, void *msg)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
schdEvent->l2Event.msg = msg; schdEvent->l2Event.msg = msg;
} }
@@ -369,9 +402,11 @@ inline void L2_SchdEventAppendMsg(L2_SCHD_EVENT* schdEvent, void* msg){
* @return SUCCESS. * @return SUCCESS.
* *
******************************************************************************/ ******************************************************************************/
inline int32_t L2_CancelEvent(L2_SCHD_EVENT* schdEvent){ inline int32_t L2_CancelEvent(L2_SCHD_EVENT *schdEvent)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
if(schdEvent->isSchd == false){ if (schdEvent->isSchd == false)
{
DBG_WARNING("Event %d has not been scheduled yet.\n", schdEvent->eventId); DBG_WARNING("Event %d has not been scheduled yet.\n", schdEvent->eventId);
} }
schdEvent->isSchd = false; schdEvent->isSchd = false;
@@ -388,7 +423,8 @@ inline int32_t L2_CancelEvent(L2_SCHD_EVENT* schdEvent){
* false. * false.
* *
******************************************************************************/ ******************************************************************************/
inline bool L2_isEventScheduled(const L2_SCHD_EVENT* schdEvent){ inline bool L2_isEventScheduled(const L2_SCHD_EVENT *schdEvent)
{
ASSERT(schdEvent != NULL); ASSERT(schdEvent != NULL);
return schdEvent->isSchd; return schdEvent->isSchd;
} }
+4 -2
View File
@@ -27,7 +27,8 @@
/** @name L2 event /** @name L2 event
* A definition of L2 event instance. * A definition of L2 event instance.
*/ */
typedef struct { typedef struct
{
bool isSchd; bool isSchd;
bool isVolatile; bool isVolatile;
L2_EVENT l2Event; L2_EVENT l2Event;
@@ -40,7 +41,8 @@ typedef struct {
/** @name L2 event /** @name L2 event
* A definition of L2 event instance. * A definition of L2 event instance.
*/ */
typedef struct { typedef struct
{
list_t *schdList; list_t *schdList;
uint32_t envCnt; uint32_t envCnt;
} L2_SCHD_POOL; } L2_SCHD_POOL;
+97 -52
View File
@@ -32,8 +32,7 @@
const TIME_WORD RTT_TIME_NONE = { const TIME_WORD RTT_TIME_NONE = {
.int_part = UINT32_MAX, .int_part = UINT32_MAX,
.frac_part = UINT32_MAX .frac_part = UINT32_MAX};
};
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
/************************** Variable Definitions *****************************/ /************************** Variable Definitions *****************************/
@@ -42,7 +41,6 @@ const TIME_WORD RTT_TIME_NONE = {
static void RTT_StageUpdate(L2_RTT_FSM *rttFsmPtr, L2_RTT_STGE newStage); static void RTT_StageUpdate(L2_RTT_FSM *rttFsmPtr, L2_RTT_STGE newStage);
static void RTT_GetFrameStart(L2_INST *instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE torType); static void RTT_GetFrameStart(L2_INST *instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE torType);
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief a slot alignment, base on physical layer information. * @brief a slot alignment, base on physical layer information.
@@ -57,10 +55,12 @@ static void RTT_GetFrameStart(L2_INST* instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE
* *
******************************************************************************/ ******************************************************************************/
// TODO: Check // TODO: Check
int32_t L2_TimeAlign(L2_INST* instPtr, uint32_t frameNum, uint32_t slotNum, bool relative){ int32_t L2_TimeAlign(L2_INST *instPtr, uint32_t frameNum, uint32_t slotNum, bool relative)
{
L1_FRAME_TYPE lastRecvdFrame; L1_FRAME_TYPE lastRecvdFrame;
lastRecvdFrame = Phy_GetLastRecvFrameType(instPtr->config.phyInstPtr); lastRecvdFrame = Phy_GetLastRecvFrameType(instPtr->config.phyInstPtr);
if(lastRecvdFrame == L1_NONE_FRAME){ if (lastRecvdFrame == L1_NONE_FRAME)
{
return EIGNORE; return EIGNORE;
} }
@@ -71,7 +71,8 @@ int32_t L2_TimeAlign(L2_INST* instPtr, uint32_t frameNum, uint32_t slotNum, bool
expctTime = ((uint64_t)(frameNum * L2_SLOTS_PER_FRAME + slotNum)) * TIME_FRAC_MAX; expctTime = ((uint64_t)(frameNum * L2_SLOTS_PER_FRAME + slotNum)) * TIME_FRAC_MAX;
expctTime += Phy_GetTimeComp(lastRecvdFrame); expctTime += Phy_GetTimeComp(lastRecvdFrame);
if(relative) { if (relative)
{
expctTime += (uint64_t)instPtr->netStartTime * L2_SLOTS_PER_FRAME * TIME_FRAC_MAX; expctTime += (uint64_t)instPtr->netStartTime * L2_SLOTS_PER_FRAME * TIME_FRAC_MAX;
} }
@@ -97,7 +98,8 @@ int32_t L2_TimeAlign(L2_INST* instPtr, uint32_t frameNum, uint32_t slotNum, bool
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
static void RTT_StageUpdate(L2_RTT_FSM* rttFsmPtr, L2_RTT_STGE newStage) { static void RTT_StageUpdate(L2_RTT_FSM *rttFsmPtr, L2_RTT_STGE newStage)
{
ASSERT(rttFsmPtr != NULL); ASSERT(rttFsmPtr != NULL);
ASSERT(newStage >= RTT_NONE_STAGE && newStage <= RTT_RSP2); ASSERT(newStage >= RTT_NONE_STAGE && newStage <= RTT_RSP2);
DBG_INFO("RTT Stage Update: %d -> %d.\n", rttFsmPtr->rttStage, newStage); DBG_INFO("RTT Stage Update: %d -> %d.\n", rttFsmPtr->rttStage, newStage);
@@ -113,7 +115,8 @@ static void RTT_StageUpdate(L2_RTT_FSM* rttFsmPtr, L2_RTT_STGE newStage) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void RTT_Reset(L2_RTT_FSM* rttFsmPtr){ void RTT_Reset(L2_RTT_FSM *rttFsmPtr)
{
ASSERT(rttFsmPtr != NULL); ASSERT(rttFsmPtr != NULL);
memset(&(rttFsmPtr->rttRecord), 0x00, sizeof(RTT_RECORD)); memset(&(rttFsmPtr->rttRecord), 0x00, sizeof(RTT_RECORD));
rttFsmPtr->rttStage = RTT_REQ1; rttFsmPtr->rttStage = RTT_REQ1;
@@ -135,11 +138,11 @@ void RTT_Reset(L2_RTT_FSM* rttFsmPtr){
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void RTT_Destroy(L2_RTT_FSM* rttFsmPtr){ void RTT_Destroy(L2_RTT_FSM *rttFsmPtr)
{
free(rttFsmPtr); free(rttFsmPtr);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Get the frame start time. If torType == L2_TOR_TX, get the next * @brief Get the frame start time. If torType == L2_TOR_TX, get the next
@@ -153,16 +156,19 @@ void RTT_Destroy(L2_RTT_FSM* rttFsmPtr){
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void RTT_GetFrameStart(L2_INST* instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE torType){ void RTT_GetFrameStart(L2_INST *instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE torType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(tmStamp != NULL); ASSERT(tmStamp != NULL);
// Get the next frame start time // Get the next frame start time
if(torType == L2_TOR_TX) { if (torType == L2_TOR_TX)
{
tmStamp->int_part = L2_GetNextFrameNum(instPtr) * L2_SLOTS_PER_FRAME + L2_GetNextSlotNum(instPtr); tmStamp->int_part = L2_GetNextFrameNum(instPtr) * L2_SLOTS_PER_FRAME + L2_GetNextSlotNum(instPtr);
tmStamp->frac_part = 0; tmStamp->frac_part = 0;
} }
// Get the last reiceive frame start time // Get the last reiceive frame start time
else if(torType == L2_TOR_RX) { else if (torType == L2_TOR_RX)
{
PHY_INST *phyRef = instPtr->config.phyInstPtr; PHY_INST *phyRef = instPtr->config.phyInstPtr;
TIME_WORD timeStamp; TIME_WORD timeStamp;
int64_t timeDiff; int64_t timeDiff;
@@ -173,14 +179,16 @@ void RTT_GetFrameStart(L2_INST* instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE torType
timeDiff -= ((uint64_t)tmStamp->int_part) * TIME_FRAC_MAX; timeDiff -= ((uint64_t)tmStamp->int_part) * TIME_FRAC_MAX;
timeDiff -= (uint64_t)tmStamp->frac_part; timeDiff -= (uint64_t)tmStamp->frac_part;
timeDiff = (int64_t)Phy_GetTimeComp(L2_L2frame2L1frame(L2_GetLastFrameType(instPtr))) - timeDiff; timeDiff = (int64_t)Phy_GetTimeComp(L2_L2frame2L1frame(L2_GetLastFrameType(instPtr))) - timeDiff;
if (timeDiff < 0) { if (timeDiff < 0)
{
timeDiff += TIME_FRAC_MAX; timeDiff += TIME_FRAC_MAX;
tmStamp->int_part--; tmStamp->int_part--;
_nop_(); _nop_();
} }
tmStamp->frac_part = (TIME_FRAC)timeDiff; tmStamp->frac_part = (TIME_FRAC)timeDiff;
} }
else { else
{
*tmStamp = RTT_TIME_NONE; *tmStamp = RTT_TIME_NONE;
DBG_ERROR("Unexpected tx/rx mode, setting dummy values.\n"); DBG_ERROR("Unexpected tx/rx mode, setting dummy values.\n");
} }
@@ -196,9 +204,11 @@ void RTT_GetFrameStart(L2_INST* instPtr, TIME_WORD *tmStamp, L2_TOR_TYPE torType
* EMAC_MEMORY_ALLOC_ERROR Memory allocate error. * EMAC_MEMORY_ALLOC_ERROR Memory allocate error.
* *
******************************************************************************/ ******************************************************************************/
int32_t RTT_Init(L2_RTT_FSM** rttFsmPtr){ int32_t RTT_Init(L2_RTT_FSM **rttFsmPtr)
{
(*rttFsmPtr) = (L2_RTT_FSM *)malloc(sizeof(L2_RTT_FSM)); (*rttFsmPtr) = (L2_RTT_FSM *)malloc(sizeof(L2_RTT_FSM));
if (*rttFsmPtr == NULL) { if (*rttFsmPtr == NULL)
{
free(*rttFsmPtr); free(*rttFsmPtr);
return EALLOC; return EALLOC;
} }
@@ -217,7 +227,8 @@ int32_t RTT_Init(L2_RTT_FSM** rttFsmPtr){
* error code * error code
* *
******************************************************************************/ ******************************************************************************/
int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) { int32_t RTT_FsmUpdate(L2_INST *instPtr, L2_TOR_TYPE torType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
L2_RTT_FSM *rttFsmPtr = L2_GetRttFsm(instPtr); L2_RTT_FSM *rttFsmPtr = L2_GetRttFsm(instPtr);
RTT_RECORD *rttRecord = &(rttFsmPtr->rttRecord); RTT_RECORD *rttRecord = &(rttFsmPtr->rttRecord);
@@ -226,10 +237,13 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
DBG_INFO("-------- RTT FSM Update --------\n"); DBG_INFO("-------- RTT FSM Update --------\n");
// Stage trans // Stage trans
// For Head Node: Tx->Rx->Tx->Rx // For Head Node: Tx->Rx->Tx->Rx
if(L2_GetNodeType(instPtr) == L2_NODE_HEAD){ if (L2_GetNodeType(instPtr) == L2_NODE_HEAD)
switch(rttFsmPtr->rttStage) { {
switch (rttFsmPtr->rttStage)
{
case RTT_REQ1: case RTT_REQ1:
if(torType == L2_TOR_TX){ if (torType == L2_TOR_TX)
{
DBG_INFO("Prepare data for RTT REQ-1\n"); DBG_INFO("Prepare data for RTT REQ-1\n");
// Update Tt1 // Update Tt1
RTT_GetFrameStart(instPtr, &(rttRecord->Tt1), torType); RTT_GetFrameStart(instPtr, &(rttRecord->Tt1), torType);
@@ -241,13 +255,16 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
rttFsmPtr->doppler = RTT_DOPPLER_NONE; rttFsmPtr->doppler = RTT_DOPPLER_NONE;
RTT_StageUpdate(rttFsmPtr, RTT_RSP1); RTT_StageUpdate(rttFsmPtr, RTT_RSP1);
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
case RTT_RSP1: case RTT_RSP1:
if(torType == L2_TOR_RX){ if (torType == L2_TOR_RX)
if(L2_isDataAvail(instPtr) == true){ {
if (L2_isDataAvail(instPtr) == true)
{
DBG_INFO("RTT RSP-1 have been received.\n"); DBG_INFO("RTT RSP-1 have been received.\n");
DBG_INFO("Preparing data for RTT REQ-2.\n"); DBG_INFO("Preparing data for RTT REQ-2.\n");
rttFsmPtr->isR2Valid = true; rttFsmPtr->isR2Valid = true;
@@ -258,7 +275,8 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
// Calculate TroundA // Calculate TroundA
TimeCtrl_CalculateTimeGap(&(rttRecord->Tt1), &(rttRecord->Tr2), &(rttRecord->TroundA)); TimeCtrl_CalculateTimeGap(&(rttRecord->Tt1), &(rttRecord->Tr2), &(rttRecord->TroundA));
} }
else{ else
{
DBG_WARNING("RTT RSP-1 had not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT RSP-1 had not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set Tr2 dopplerR2 and TroundA to dummy value.\n"); DBG_WARNING("Set Tr2 dopplerR2 and TroundA to dummy value.\n");
rttFsmPtr->isR2Valid = false; rttFsmPtr->isR2Valid = false;
@@ -273,21 +291,25 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
DBG_INFO("TroundA-timeH:%u.\n", rttRecord->TroundA.int_part); DBG_INFO("TroundA-timeH:%u.\n", rttRecord->TroundA.int_part);
RTT_StageUpdate(rttFsmPtr, RTT_REQ2); RTT_StageUpdate(rttFsmPtr, RTT_REQ2);
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
case RTT_REQ2: case RTT_REQ2:
if(torType == L2_TOR_TX){ if (torType == L2_TOR_TX)
{
DBG_INFO("Prepare data for RTT REQ-2\n"); DBG_INFO("Prepare data for RTT REQ-2\n");
// Update Tt3 // Update Tt3
RTT_GetFrameStart(instPtr, &(rttRecord->Tt3), torType); RTT_GetFrameStart(instPtr, &(rttRecord->Tt3), torType);
if ( rttFsmPtr->isR2Valid ) { if (rttFsmPtr->isR2Valid)
{
// Calculate reply A // Calculate reply A
TimeCtrl_CalculateTimeGap(&(rttRecord->Tt3), &(rttRecord->Tr2), &(rttRecord->TreplyA)); TimeCtrl_CalculateTimeGap(&(rttRecord->Tt3), &(rttRecord->Tr2), &(rttRecord->TreplyA));
} }
else { else
{
DBG_WARNING("RTT RSP-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT RSP-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set TreplyA to dummy value.\n"); DBG_WARNING("Set TreplyA to dummy value.\n");
rttRecord->TreplyA = RTT_TIME_NONE; rttRecord->TreplyA = RTT_TIME_NONE;
@@ -302,13 +324,16 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
rttFsmPtr->doppler = rttRecord->dopplerR2; rttFsmPtr->doppler = rttRecord->dopplerR2;
RTT_StageUpdate(rttFsmPtr, RTT_RSP2); RTT_StageUpdate(rttFsmPtr, RTT_RSP2);
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
case RTT_RSP2: case RTT_RSP2:
if(torType == L2_TOR_RX){ if (torType == L2_TOR_RX)
if(L2_isDataAvail(instPtr) == true){ {
if (L2_isDataAvail(instPtr) == true)
{
DBG_INFO("RTT RSP-2 have been received.\n"); DBG_INFO("RTT RSP-2 have been received.\n");
rttFsmPtr->isR4Valid = true; rttFsmPtr->isR4Valid = true;
// Update doppler // Update doppler
@@ -319,7 +344,8 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
// Calculate TroundA // Calculate TroundA
TimeCtrl_CalculateTimeGap(&(rttRecord->Tt3), &(rttRecord->Tr4), &(rttRecord->TroundA)); TimeCtrl_CalculateTimeGap(&(rttRecord->Tt3), &(rttRecord->Tr4), &(rttRecord->TroundA));
} }
else{ else
{
DBG_WARNING("RTT RSP-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT RSP-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set Tr4 dopplerR4 and TroundA to dummy value.\n"); DBG_WARNING("Set Tr4 dopplerR4 and TroundA to dummy value.\n");
rttFsmPtr->isR4Valid = false; rttFsmPtr->isR4Valid = false;
@@ -335,7 +361,8 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
RTT_StageUpdate(rttFsmPtr, RTT_REQ1); RTT_StageUpdate(rttFsmPtr, RTT_REQ1);
DBG_INFO("RTT procedure end...\n"); DBG_INFO("RTT procedure end...\n");
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
@@ -345,11 +372,15 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
} }
} }
// For Member Node: Rx->Tx->Rx->Tx // For Member Node: Rx->Tx->Rx->Tx
else{ else
switch(rttFsmPtr->rttStage) { {
switch (rttFsmPtr->rttStage)
{
case RTT_REQ1: case RTT_REQ1:
if(torType == L2_TOR_RX){ if (torType == L2_TOR_RX)
if(L2_isDataAvail(instPtr) == true){ {
if (L2_isDataAvail(instPtr) == true)
{
DBG_INFO("RTT REQ-1 have been received.\n"); DBG_INFO("RTT REQ-1 have been received.\n");
DBG_INFO("Preparing data for RTT RSP-1.\n"); DBG_INFO("Preparing data for RTT RSP-1.\n");
rttFsmPtr->isR2Valid = true; rttFsmPtr->isR2Valid = true;
@@ -358,7 +389,8 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
// Update Tr1 // Update Tr1
RTT_GetFrameStart(instPtr, &(rttRecord->Tr1), torType); RTT_GetFrameStart(instPtr, &(rttRecord->Tr1), torType);
} }
else{ else
{
DBG_WARNING("RTT REQ-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT REQ-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set dopplerR1 and Tr1 to dummy value.\n"); DBG_WARNING("Set dopplerR1 and Tr1 to dummy value.\n");
rttFsmPtr->isR1Valid = false; rttFsmPtr->isR1Valid = false;
@@ -370,20 +402,24 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
DBG_INFO("Tr1-timeH:%u.\n", rttRecord->Tr1.int_part); DBG_INFO("Tr1-timeH:%u.\n", rttRecord->Tr1.int_part);
RTT_StageUpdate(rttFsmPtr, RTT_RSP1); RTT_StageUpdate(rttFsmPtr, RTT_RSP1);
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
case RTT_RSP1: case RTT_RSP1:
if(torType == L2_TOR_TX){ if (torType == L2_TOR_TX)
{
DBG_INFO("Prepare data for RTT RSP-1\n"); DBG_INFO("Prepare data for RTT RSP-1\n");
// Update Tt2 // Update Tt2
RTT_GetFrameStart(instPtr, &(rttRecord->Tt2), torType); RTT_GetFrameStart(instPtr, &(rttRecord->Tt2), torType);
if ( rttFsmPtr->isR1Valid ) { if (rttFsmPtr->isR1Valid)
{
// Calculate reply B // Calculate reply B
TimeCtrl_CalculateTimeGap(&(rttRecord->Tr1), &(rttRecord->Tt2), &(rttRecord->TreplyB)); TimeCtrl_CalculateTimeGap(&(rttRecord->Tr1), &(rttRecord->Tt2), &(rttRecord->TreplyB));
} }
else { else
{
rttRecord->TreplyB = RTT_TIME_NONE; rttRecord->TreplyB = RTT_TIME_NONE;
DBG_WARNING("RTT REQ-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT REQ-1 have not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set ReplayB to dummy value.\n"); DBG_WARNING("Set ReplayB to dummy value.\n");
@@ -398,13 +434,16 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
rttFsmPtr->doppler = rttRecord->dopplerR1; rttFsmPtr->doppler = rttRecord->dopplerR1;
RTT_StageUpdate(rttFsmPtr, RTT_REQ2); RTT_StageUpdate(rttFsmPtr, RTT_REQ2);
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
case RTT_REQ2: case RTT_REQ2:
if(torType == L2_TOR_RX){ if (torType == L2_TOR_RX)
if(L2_isDataAvail(instPtr) == true){ {
if (L2_isDataAvail(instPtr) == true)
{
DBG_INFO("RTT REQ-2 have been received.\n"); DBG_INFO("RTT REQ-2 have been received.\n");
DBG_INFO("Preparing data for RTT RSP-2.\n"); DBG_INFO("Preparing data for RTT RSP-2.\n");
rttFsmPtr->isR3Valid = true; rttFsmPtr->isR3Valid = true;
@@ -415,7 +454,8 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
// Calculate TroundB // Calculate TroundB
TimeCtrl_CalculateTimeGap(&(rttRecord->Tt2), &(rttRecord->Tr3), &(rttRecord->TroundB)); TimeCtrl_CalculateTimeGap(&(rttRecord->Tt2), &(rttRecord->Tr3), &(rttRecord->TroundB));
} }
else{ else
{
DBG_WARNING("RTT REQ-2 have not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT REQ-2 have not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set dopplerR3 and ReplayB to dummy value.\n"); DBG_WARNING("Set dopplerR3 and ReplayB to dummy value.\n");
rttFsmPtr->isR3Valid = false; rttFsmPtr->isR3Valid = false;
@@ -429,21 +469,25 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
DBG_INFO("TroundB-timeH:%u.\n", rttRecord->TroundB.int_part); DBG_INFO("TroundB-timeH:%u.\n", rttRecord->TroundB.int_part);
RTT_StageUpdate(rttFsmPtr, RTT_RSP2); RTT_StageUpdate(rttFsmPtr, RTT_RSP2);
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
case RTT_RSP2: case RTT_RSP2:
if(torType == L2_TOR_TX){ if (torType == L2_TOR_TX)
{
DBG_INFO("Prepare data for RTT RSP-2\n"); DBG_INFO("Prepare data for RTT RSP-2\n");
// Update Tt4 // Update Tt4
RTT_GetFrameStart(instPtr, &(rttRecord->Tt4), torType); RTT_GetFrameStart(instPtr, &(rttRecord->Tt4), torType);
if ( rttFsmPtr->isR3Valid ) { if (rttFsmPtr->isR3Valid)
{
// Calculate reply B // Calculate reply B
TimeCtrl_CalculateTimeGap(&(rttRecord->Tr3), &(rttRecord->Tt4), &(rttRecord->TreplyB)); TimeCtrl_CalculateTimeGap(&(rttRecord->Tr3), &(rttRecord->Tt4), &(rttRecord->TreplyB));
// TODO: implement a time adjustment. // TODO: implement a time adjustment.
} }
else { else
{
DBG_WARNING("RTT REQ-2 have not been received [Stage: %d].\n", rttFsmPtr->rttStage); DBG_WARNING("RTT REQ-2 have not been received [Stage: %d].\n", rttFsmPtr->rttStage);
DBG_WARNING("Set ReplayB to dummy value.\n"); DBG_WARNING("Set ReplayB to dummy value.\n");
rttRecord->TreplyB = RTT_TIME_NONE; rttRecord->TreplyB = RTT_TIME_NONE;
@@ -459,7 +503,8 @@ int32_t RTT_FsmUpdate(L2_INST* instPtr, L2_TOR_TYPE torType) {
RTT_StageUpdate(rttFsmPtr, RTT_REQ1); RTT_StageUpdate(rttFsmPtr, RTT_REQ1);
DBG_INFO("RTT procedure end...\n"); DBG_INFO("RTT procedure end...\n");
} }
else{ else
{
DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage); DBG_INFO("RTT FSM remain current state [%d].\n", rttFsmPtr->rttStage);
} }
break; break;
+73 -43
View File
@@ -47,21 +47,18 @@ L2_CONFIG l2Config = {
.bkpHeadId = 1, .bkpHeadId = 1,
.netScale = 2, .netScale = 2,
.phyInstPtr = NULL, .phyInstPtr = NULL,
.tmcInstPtr = NULL .tmcInstPtr = NULL};
};
TIMECTRL_CONFIG tmcConfig = { TIMECTRL_CONFIG tmcConfig = {
.baseAddr = TIME_CONTROLLER_BASEADDR, .baseAddr = TIME_CONTROLLER_BASEADDR,
.reset = 10 .reset = 10};
};
PHY_CONFIG l1Config = { PHY_CONFIG l1Config = {
.devId = PHY_DEVICE_ID, .devId = PHY_DEVICE_ID,
.txBramId = TXBUFF_BRAM_ID, .txBramId = TXBUFF_BRAM_ID,
.rxBramId = RXBUFF_BRAM_ID, .rxBramId = RXBUFF_BRAM_ID,
.antBramId = ANTCONFIG_BRAM_ID, .antBramId = ANTCONFIG_BRAM_ID,
.baseAddr = PHY_CTRL_BASEADDR .baseAddr = PHY_CTRL_BASEADDR};
};
MPC_CONFIG mpcConfig = { MPC_CONFIG mpcConfig = {
.bramDevId = MPC_PCIE_BRAM_ID, .bramDevId = MPC_PCIE_BRAM_ID,
.msgLenMax = MPC_BUFFER_LENGTH // byte .msgLenMax = MPC_BUFFER_LENGTH // byte
@@ -96,7 +93,8 @@ XGpio configGpio; /* The Instance of the GPIO Driver */
* FAILURE if initialization failed. * FAILURE if initialization failed.
* *
*****************************************************************************/ *****************************************************************************/
int SetupInterruptSystem(XScuGic* instPtr, uint32_t devId){ int SetupInterruptSystem(XScuGic *instPtr, uint32_t devId)
{
XScuGic_Config *configPtr; XScuGic_Config *configPtr;
int Status; int Status;
uint32_t intrState; uint32_t intrState;
@@ -104,13 +102,15 @@ int SetupInterruptSystem(XScuGic* instPtr, uint32_t devId){
configPtr = XScuGic_LookupConfig(devId); configPtr = XScuGic_LookupConfig(devId);
Status = XScuGic_CfgInitialize(instPtr, configPtr, Status = XScuGic_CfgInitialize(instPtr, configPtr,
configPtr->CpuBaseAddress); configPtr->CpuBaseAddress);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
return Status; return Status;
} }
// Perform a self-test to ensure that the hardware was built correctly. // Perform a self-test to ensure that the hardware was built correctly.
Status = XScuGic_SelfTest(instPtr); Status = XScuGic_SelfTest(instPtr);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
return Status; return Status;
} }
// Clear any pending interrupt flags // Clear any pending interrupt flags
@@ -141,7 +141,8 @@ int SetupInterruptSystem(XScuGic* instPtr, uint32_t devId){
// Connect the interrupt service routine isr1 to the interrupt controller // Connect the interrupt service routine isr1 to the interrupt controller
Status = XScuGic_Connect(instPtr, PHY_INTR_ID, Status = XScuGic_Connect(instPtr, PHY_INTR_ID,
(Xil_ExceptionHandler)PhysicalIntr_Handler, (void *)phyInstPtr); (Xil_ExceptionHandler)PhysicalIntr_Handler, (void *)phyInstPtr);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
return Status; return Status;
} }
@@ -151,7 +152,8 @@ int SetupInterruptSystem(XScuGic* instPtr, uint32_t devId){
// Connect the interrupt service routine isr1 to the interrupt controller // Connect the interrupt service routine isr1 to the interrupt controller
Status = XScuGic_Connect(instPtr, PERIODIC_5MS_INTR_ID, Status = XScuGic_Connect(instPtr, PERIODIC_5MS_INTR_ID,
(Xil_ExceptionHandler)PeriodicPull_Handler, (void *)linkInstPtr); (Xil_ExceptionHandler)PeriodicPull_Handler, (void *)linkInstPtr);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
return Status; return Status;
} }
@@ -161,7 +163,8 @@ int SetupInterruptSystem(XScuGic* instPtr, uint32_t devId){
// Connect the interrupt service routine isr1 to the interrupt controller // Connect the interrupt service routine isr1 to the interrupt controller
Status = XScuGic_Connect(instPtr, MPC_RX_DONE_INTR_ID, Status = XScuGic_Connect(instPtr, MPC_RX_DONE_INTR_ID,
(Xil_ExceptionHandler)MPC_IntrHandler, (void *)mpcInstPtr); (Xil_ExceptionHandler)MPC_IntrHandler, (void *)mpcInstPtr);
if (Status != XST_SUCCESS) { if (Status != XST_SUCCESS)
{
return Status; return Status;
} }
XScuGic_Enable(instPtr, MPC_RX_DONE_INTR_ID); XScuGic_Enable(instPtr, MPC_RX_DONE_INTR_ID);
@@ -185,10 +188,9 @@ int SetupInterruptSystem(XScuGic* instPtr, uint32_t devId){
return Status; return Status;
} // SetupInterruptSystem } // SetupInterruptSystem
#ifdef _MPC_CTRL #ifdef _MPC_CTRL
void MPC_PCIESentStart(){ void MPC_PCIESentStart()
{
uint32_t GpioConfig = XGpio_DiscreteRead(&configGpio, 2); uint32_t GpioConfig = XGpio_DiscreteRead(&configGpio, 2);
GpioConfig ^= 0x00000002; GpioConfig ^= 0x00000002;
XGpio_DiscreteWrite(&configGpio, 2, GpioConfig); XGpio_DiscreteWrite(&configGpio, 2, GpioConfig);
@@ -203,7 +205,8 @@ void MPC_PCIESentStart(){
* @return None. * @return None.
* *
******************************************************************************/ ******************************************************************************/
void MPC_IntrHandler(void* CallBackRef) { void MPC_IntrHandler(void *CallBackRef)
{
MPC_MsgHandler(mpcInstPtr); MPC_MsgHandler(mpcInstPtr);
} }
#endif #endif
@@ -217,14 +220,18 @@ void MPC_IntrHandler(void* CallBackRef) {
* @return None. * @return None.
* *
******************************************************************************/ ******************************************************************************/
void PhysicalIntr_Handler(void *callBackRef) { void PhysicalIntr_Handler(void *callBackRef)
{
uint32_t GpioConfig = XGpio_DiscreteRead(&configGpio, 2); uint32_t GpioConfig = XGpio_DiscreteRead(&configGpio, 2);
if(L2_isDataAvail(linkInstPtr) == true ){ if (L2_isDataAvail(linkInstPtr) == true)
{
GpioConfig ^= 0x00000001; GpioConfig ^= 0x00000001;
} }
Phy_UpdateState(phyInstPtr); Phy_UpdateState(phyInstPtr);
if(phyInstPtr->TOR.curr == PHY_TOR_TX){ if (phyInstPtr->TOR.curr == PHY_TOR_TX)
switch(phyInstPtr->frameType.curr){ {
switch (phyInstPtr->frameType.curr)
{
case L1_1M2BPS_5MS_FRAME: case L1_1M2BPS_5MS_FRAME:
mpcRecord->frame1Sent++; mpcRecord->frame1Sent++;
break; break;
@@ -253,8 +260,10 @@ void PhysicalIntr_Handler(void *callBackRef) {
break; break;
} }
} }
if(phyInstPtr->TOR.last == PHY_TOR_RX && phyInstPtr->isFrameSync){ if (phyInstPtr->TOR.last == PHY_TOR_RX && phyInstPtr->isFrameSync)
switch(phyInstPtr->frameType.last){ {
switch (phyInstPtr->frameType.last)
{
case L1_1M2BPS_5MS_FRAME: case L1_1M2BPS_5MS_FRAME:
mpcRecord->frame1Recv++; mpcRecord->frame1Recv++;
break; break;
@@ -295,16 +304,19 @@ void PhysicalIntr_Handler(void *callBackRef) {
* @return None. * @return None.
* *
*****************************************************************************/ *****************************************************************************/
void PeriodicPull_Handler(void *callBackRef) { void PeriodicPull_Handler(void *callBackRef)
{
int32_t Status; int32_t Status;
Status = Linklayer_Proc(linkInstPtr); Status = Linklayer_Proc(linkInstPtr);
if(Status != SUCCESS){ if (Status != SUCCESS)
{
DBG_ERROR("Link layer runs failed, with error code: %ld\n", Status); DBG_ERROR("Link layer runs failed, with error code: %ld\n", Status);
_nop_(); _nop_();
} }
if (linkInstPtr->slotNum.next % DATA_STORE_INTERVAL == 1) { if (linkInstPtr->slotNum.next % DATA_STORE_INTERVAL == 1)
{
mpcStatusUpAssert = true; mpcStatusUpAssert = true;
mpcRecord->frame1Cnt = mpcRecord->frame1Recv - mpcRecord->frame1CntLast; mpcRecord->frame1Cnt = mpcRecord->frame1Recv - mpcRecord->frame1CntLast;
mpcRecord->frame2Cnt = mpcRecord->frame2Recv - mpcRecord->frame2CntLast; mpcRecord->frame2Cnt = mpcRecord->frame2Recv - mpcRecord->frame2CntLast;
@@ -337,19 +349,23 @@ void PeriodicPull_Handler(void *callBackRef) {
* @return * @return
* *
******************************************************************************/ ******************************************************************************/
int main(void) { int main(void)
{
int Status; int Status;
Status = HARDWARE_initialize(); Status = HARDWARE_initialize();
if (Status == SUCCESS) { if (Status == SUCCESS)
{
DBG_PRINT("Hardware Init SUCCESS! \n"); DBG_PRINT("Hardware Init SUCCESS! \n");
} }
else { else
{
DBG_PRINT("Hardware Init FAILED! \n"); DBG_PRINT("Hardware Init FAILED! \n");
return Status; return Status;
} }
Status = XGpio_Initialize(&configGpio, XPAR_CONFIG_GPIO_DEVICE_ID); Status = XGpio_Initialize(&configGpio, XPAR_CONFIG_GPIO_DEVICE_ID);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_PRINT("Config Gpio Initialization Failed\n"); DBG_PRINT("Config Gpio Initialization Failed\n");
return FAILURE; return FAILURE;
} }
@@ -358,20 +374,24 @@ int main(void) {
// Init time controller // Init time controller
Status = TimeCtrl_Init(&tmcInstPtr, &tmcConfig); Status = TimeCtrl_Init(&tmcInstPtr, &tmcConfig);
if (Status == SUCCESS) { if (Status == SUCCESS)
{
DBG_PRINT("TmcCtrl Init SUCCESS! \n"); DBG_PRINT("TmcCtrl Init SUCCESS! \n");
} }
else { else
{
DBG_PRINT("TmcCtrl Init FAILED! \n"); DBG_PRINT("TmcCtrl Init FAILED! \n");
return Status; return Status;
} }
// Init physical layer // Init physical layer
Status = Phy_InstInit(&phyInstPtr, &l1Config); Status = Phy_InstInit(&phyInstPtr, &l1Config);
if (Status == SUCCESS) { if (Status == SUCCESS)
{
DBG_PRINT("PhyCtrl Init SUCCESS! \n"); DBG_PRINT("PhyCtrl Init SUCCESS! \n");
} }
else { else
{
DBG_PRINT("PhyCtrl Init FAILED! \n"); DBG_PRINT("PhyCtrl Init FAILED! \n");
return Status; return Status;
} }
@@ -381,30 +401,36 @@ int main(void) {
l2Config.tmcInstPtr = tmcInstPtr; l2Config.tmcInstPtr = tmcInstPtr;
Status = Linklayer_Init(&linkInstPtr, &l2Config); Status = Linklayer_Init(&linkInstPtr, &l2Config);
if (Status == SUCCESS) { if (Status == SUCCESS)
{
DBG_PRINT("LinkLayer Init SUCCESS! \n"); DBG_PRINT("LinkLayer Init SUCCESS! \n");
} }
else { else
{
DBG_PRINT("Linklayer Init FAILED! \n"); DBG_PRINT("Linklayer Init FAILED! \n");
return Status; return Status;
} }
// Init MPC // Init MPC
Status = MPC_Init(&mpcInstPtr, &mpcConfig); Status = MPC_Init(&mpcInstPtr, &mpcConfig);
if (Status == SUCCESS) { if (Status == SUCCESS)
{
DBG_PRINT("MPC Init SUCCESS! \n"); DBG_PRINT("MPC Init SUCCESS! \n");
} }
else { else
{
DBG_PRINT("MPC Init FAILED! \n"); DBG_PRINT("MPC Init FAILED! \n");
return Status; return Status;
} }
// Initialize GIC. // Initialize GIC.
Status = SetupInterruptSystem(&gicInst, SCUGIC_DEVICE_ID); Status = SetupInterruptSystem(&gicInst, SCUGIC_DEVICE_ID);
if (Status == SUCCESS) { if (Status == SUCCESS)
{
DBG_PRINT("Intc Init SUCCESS! \n"); DBG_PRINT("Intc Init SUCCESS! \n");
} }
else { else
{
DBG_PRINT("Intc Init FAILED! \n"); DBG_PRINT("Intc Init FAILED! \n");
return Status; return Status;
} }
@@ -415,14 +441,18 @@ int main(void) {
// FIXME: only for debug, linklayer should be start by MPC // FIXME: only for debug, linklayer should be start by MPC
Linklayer_Start(linkInstPtr); Linklayer_Start(linkInstPtr);
while(true) { while (true)
if (mpcStatusUpAssert == true) { {
if (mpcStatusUpAssert == true)
{
mpcStatusUpAssert = false; mpcStatusUpAssert = false;
if (linkInstPtr->buffSwitch == 0) { if (linkInstPtr->buffSwitch == 0)
{
MPC_StateUpload(mpcInstPtr, linkInstPtr->mgmtMsgRecvBuff1, linkInstPtr->mgmtMsgRecvCnt1, MPC_StateUpload(mpcInstPtr, linkInstPtr->mgmtMsgRecvBuff1, linkInstPtr->mgmtMsgRecvCnt1,
linkInstPtr->svcMsgRecvBuff1, linkInstPtr->svcMsgRecvCnt1, &(linkInstPtr->mgmtNetFSM->mgmtMsgLog)); linkInstPtr->svcMsgRecvBuff1, linkInstPtr->svcMsgRecvCnt1, &(linkInstPtr->mgmtNetFSM->mgmtMsgLog));
} }
else { else
{
MPC_StateUpload(mpcInstPtr, linkInstPtr->mgmtMsgRecvBuff0, linkInstPtr->mgmtMsgRecvCnt0, MPC_StateUpload(mpcInstPtr, linkInstPtr->mgmtMsgRecvBuff0, linkInstPtr->mgmtMsgRecvCnt0,
linkInstPtr->svcMsgRecvBuff0, linkInstPtr->svcMsgRecvCnt0, &(linkInstPtr->mgmtNetFSM->mgmtMsgLog)); linkInstPtr->svcMsgRecvBuff0, linkInstPtr->svcMsgRecvCnt0, &(linkInstPtr->mgmtNetFSM->mgmtMsgLog));
} }
+64 -36
View File
@@ -56,7 +56,6 @@ static MPC_MSG_HEADER mpcHeader = {
.head = 0x5302, .head = 0x5302,
}; };
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Insert a data chunk to a bit array. * @brief Insert a data chunk to a bit array.
@@ -71,9 +70,11 @@ static MPC_MSG_HEADER mpcHeader = {
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int32_t MPC_InsertChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint64_t chunk){ static int32_t MPC_InsertChunk(BIT_ARRAY *bitarr, uint32_t offset, uint32_t numBit, uint64_t chunk)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
if(bitarr->num_of_bits < offset+numBit){ if (bitarr->num_of_bits < offset + numBit)
{
return FAILURE; return FAILURE;
} }
bit_array_set_wordn(bitarr, offset, chunk, numBit); bit_array_set_wordn(bitarr, offset, chunk, numBit);
@@ -94,17 +95,21 @@ static int32_t MPC_InsertChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numB
* SUCCESS * SUCCESS
* *
******************************************************************************/ ******************************************************************************/
static int32_t MPC_GetChunk(BIT_ARRAY* bitarr, uint32_t offset, uint32_t numBit, uint64_t* chunk){ static int32_t MPC_GetChunk(BIT_ARRAY *bitarr, uint32_t offset, uint32_t numBit, uint64_t *chunk)
{
ASSERT(bitarr != NULL); ASSERT(bitarr != NULL);
if(bitarr->num_of_bits < offset+numBit || numBit > 64){ if (bitarr->num_of_bits < offset + numBit || numBit > 64)
{
return FAILURE; return FAILURE;
} }
*chunk = bit_array_get_wordn(bitarr, offset, numBit); *chunk = bit_array_get_wordn(bitarr, offset, numBit);
return SUCCESS; return SUCCESS;
} }
static uint32_t MPC_GetMsgLen(MPC_MSG_TYPE type){ static uint32_t MPC_GetMsgLen(MPC_MSG_TYPE type)
switch(type){ {
switch (type)
{
case MPC_MSG_TEST: case MPC_MSG_TEST:
return (MPC_MSG_TEST_LEN / 8); return (MPC_MSG_TEST_LEN / 8);
case MPC_MSG_STATE: case MPC_MSG_STATE:
@@ -124,13 +129,15 @@ static uint32_t MPC_GetMsgLen(MPC_MSG_TYPE type){
* error code. * error code.
* *
******************************************************************************/ ******************************************************************************/
int MPC_Init(MPC_INST** instPtr, MPC_CONFIG* configPtr){ int MPC_Init(MPC_INST **instPtr, MPC_CONFIG *configPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(configPtr != NULL); ASSERT(configPtr != NULL);
int Status = SUCCESS; int Status = SUCCESS;
XBram_Config *bramConfigPtr; XBram_Config *bramConfigPtr;
(*instPtr) = (MPC_INST *)malloc(sizeof(MPC_INST)); (*instPtr) = (MPC_INST *)malloc(sizeof(MPC_INST));
if((*instPtr) == NULL){ if ((*instPtr) == NULL)
{
Status = EALLOC; Status = EALLOC;
goto init_failed; goto init_failed;
} }
@@ -138,35 +145,41 @@ int MPC_Init(MPC_INST** instPtr, MPC_CONFIG* configPtr){
(*instPtr)->configs.msgLenMax = configPtr->msgLenMax; (*instPtr)->configs.msgLenMax = configPtr->msgLenMax;
bramConfigPtr = XBram_LookupConfig(configPtr->bramDevId); bramConfigPtr = XBram_LookupConfig(configPtr->bramDevId);
if (bramConfigPtr == NULL) { if (bramConfigPtr == NULL)
{
Status = EDINVAL; Status = EDINVAL;
goto init_failed; goto init_failed;
} }
Status = XBram_CfgInitialize(&((*instPtr)->pcieBram), bramConfigPtr, Status = XBram_CfgInitialize(&((*instPtr)->pcieBram), bramConfigPtr,
bramConfigPtr->CtrlBaseAddress); bramConfigPtr->CtrlBaseAddress);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto init_failed; goto init_failed;
} }
/* Execute the BRAM driver selftest. */ /* Execute the BRAM driver selftest. */
Status = XBram_SelfTest(&((*instPtr)->pcieBram), 0); Status = XBram_SelfTest(&((*instPtr)->pcieBram), 0);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto init_failed; goto init_failed;
} }
(*instPtr)->txBaseAddr = (uint8_t *)((*instPtr)->pcieBram.Config.MemBaseAddress); (*instPtr)->txBaseAddr = (uint8_t *)((*instPtr)->pcieBram.Config.MemBaseAddress);
(*instPtr)->rxBaseAddr = (uint8_t *)((*instPtr)->pcieBram.Config.MemBaseAddress); (*instPtr)->rxBaseAddr = (uint8_t *)((*instPtr)->pcieBram.Config.MemBaseAddress);
mpcRecord = (MPC_RECORD *)malloc(sizeof(MPC_RECORD)); mpcRecord = (MPC_RECORD *)malloc(sizeof(MPC_RECORD));
if(mpcRecord == NULL){ if (mpcRecord == NULL)
{
Status = EALLOC; Status = EALLOC;
goto init_failed; goto init_failed;
} }
mpcTxMsgBuff = bit_array_create(STATE_STROAGE_BUFF_MAX * 8); mpcTxMsgBuff = bit_array_create(STATE_STROAGE_BUFF_MAX * 8);
if(mpcTxMsgBuff == NULL){ if (mpcTxMsgBuff == NULL)
{
goto init_failed; goto init_failed;
} }
mpcRxMsgBuff = bit_array_create(MPC_MSG_LEN_MAX); mpcRxMsgBuff = bit_array_create(MPC_MSG_LEN_MAX);
if(mpcRxMsgBuff == NULL){ if (mpcRxMsgBuff == NULL)
{
goto init_failed; goto init_failed;
} }
return Status; return Status;
@@ -193,25 +206,30 @@ init_failed:
* error code. * error code.
* *
******************************************************************************/ ******************************************************************************/
int MPC_MsgHandler(MPC_INST* instPtr){ int MPC_MsgHandler(MPC_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
MPC_MSG_HEADER *pktHeader = (MPC_MSG_HEADER *)(instPtr->rxBaseAddr); MPC_MSG_HEADER *pktHeader = (MPC_MSG_HEADER *)(instPtr->rxBaseAddr);
// instPtr->txBaseAddr + MPC_MSG_HEAD_LEN; // instPtr->txBaseAddr + MPC_MSG_HEAD_LEN;
uint64_t dataChunk = 0; uint64_t dataChunk = 0;
switch(pktHeader->type){ switch (pktHeader->type)
{
case MPC_MSG_TEST_CODE: case MPC_MSG_TEST_CODE:
memcpy(mpcRxMsgBuff->words, memcpy(mpcRxMsgBuff->words,
instPtr->rxBaseAddr + (MPC_MSG_HEAD_LEN / 8), instPtr->rxBaseAddr + (MPC_MSG_HEAD_LEN / 8),
DIV_ROUND_UP(MPC_MSG_TEST_LEN, 8)); DIV_ROUND_UP(MPC_MSG_TEST_LEN, 8));
MPC_GetChunk(mpcRxMsgBuff, TEST_REQ_CMD_OFFSET, TEST_REQ_CMD_LEN, &dataChunk); MPC_GetChunk(mpcRxMsgBuff, TEST_REQ_CMD_OFFSET, TEST_REQ_CMD_LEN, &dataChunk);
if((uint8_t)dataChunk == TEST_REQ_CMD_START){ if ((uint8_t)dataChunk == TEST_REQ_CMD_START)
{
MPC_GetChunk(mpcRxMsgBuff, TEST_MODE_SET_OFFSET, TEST_MODE_SET_LEN, &dataChunk); MPC_GetChunk(mpcRxMsgBuff, TEST_MODE_SET_OFFSET, TEST_MODE_SET_LEN, &dataChunk);
// TODO: check. // TODO: check.
if((uint8_t)dataChunk == TEST_MODE_HEAD){ if ((uint8_t)dataChunk == TEST_MODE_HEAD)
{
l2Config.myType = L2_NODE_HEAD; l2Config.myType = L2_NODE_HEAD;
l2Config.myId = 1; l2Config.myId = 1;
} }
else{ else
{
l2Config.myType = L2_NODE_MEMBER; l2Config.myType = L2_NODE_MEMBER;
l2Config.myId = 2; l2Config.myId = 2;
} }
@@ -222,7 +240,8 @@ int MPC_MsgHandler(MPC_INST* instPtr){
Linklayer_Reconfig(linkInstPtr, &l2Config); Linklayer_Reconfig(linkInstPtr, &l2Config);
Linklayer_Start(linkInstPtr); Linklayer_Start(linkInstPtr);
} }
else if((uint8_t)dataChunk == TEST_REQ_CMD_END){ else if ((uint8_t)dataChunk == TEST_REQ_CMD_END)
{
MPC_InsertChunk(mpcTxMsgBuff, TEST_REQ_FPGA_ST_OFFSET, TEST_REQ_FPGA_ST_LEN, 0x00); MPC_InsertChunk(mpcTxMsgBuff, TEST_REQ_FPGA_ST_OFFSET, TEST_REQ_FPGA_ST_LEN, 0x00);
MPC_InsertChunk(mpcTxMsgBuff, TEST_REQ_AD_ST_OFFSET, TEST_REQ_AD_ST_LEN, 0x00); MPC_InsertChunk(mpcTxMsgBuff, TEST_REQ_AD_ST_OFFSET, TEST_REQ_AD_ST_LEN, 0x00);
MPC_InsertChunk(mpcTxMsgBuff, TEST_REQ_RF_ST_OFFSET, TEST_REQ_RF_ST_LEN, 0x00); MPC_InsertChunk(mpcTxMsgBuff, TEST_REQ_RF_ST_OFFSET, TEST_REQ_RF_ST_LEN, 0x00);
@@ -232,9 +251,9 @@ int MPC_MsgHandler(MPC_INST* instPtr){
Linklayer_Halt(linkInstPtr); Linklayer_Halt(linkInstPtr);
MPC_SendMsg(instPtr, MPC_MSG_STATE, (uint8_t *)mpcTxMsgBuff->words, STATE_STROAGE_BUFF_MAX); MPC_SendMsg(instPtr, MPC_MSG_STATE, (uint8_t *)mpcTxMsgBuff->words, STATE_STROAGE_BUFF_MAX);
Phy_SetupNextSlot(phyInstPtr, PHY_TOR_SUSPEND, L1_200KBPS_5MS_FRAME, NULL); // any frame type is okay Phy_SetupNextSlot(phyInstPtr, PHY_TOR_SUSPEND, L1_200KBPS_5MS_FRAME, NULL); // any frame type is okay
} }
else{ else
{
DBG_WARNING("Unexpected self-test command.\n"); DBG_WARNING("Unexpected self-test command.\n");
} }
break; break;
@@ -242,12 +261,12 @@ int MPC_MsgHandler(MPC_INST* instPtr){
memcpy(mpcRxMsgBuff->words, memcpy(mpcRxMsgBuff->words,
instPtr->rxBaseAddr + ((MPC_MSG_HEAD_LEN / 8) / 2), instPtr->rxBaseAddr + ((MPC_MSG_HEAD_LEN / 8) / 2),
DIV_ROUND_UP(MPC_MSG_TSYNC_LEN, 8)); DIV_ROUND_UP(MPC_MSG_TSYNC_LEN, 8));
//FIXME: // // FIXME:
// MPC_GetChunk(mpcRxMsgBuff, RTT_ADJ_OFFSET, RTT_ADJ_LEN, &dataChunk); // MPC_GetChunk(mpcRxMsgBuff, RTT_ADJ_OFFSET, RTT_ADJ_LEN, &dataChunk);
// rttRecord->rttAdj = (double)dataChunk; // rttRecord->rttAdj = (double)dataChunk;
// MPC_GetChunk(mpcRxMsgBuff, TSYNC_ADJ_OFFSET, TSYNC_ADJ_LEN, &dataChunk); // MPC_GetChunk(mpcRxMsgBuff, TSYNC_ADJ_OFFSET, TSYNC_ADJ_LEN, &dataChunk);
// double tsyncAdj = (double)dataChunk * 0.192; // double tsyncAdj = (double)dataChunk * 0.192;
//FIXME: // // FIXME:
// PhyWrite(TSYNC_PULS_ADJ, (uint32_t)tsyncAdj); // PhyWrite(TSYNC_PULS_ADJ, (uint32_t)tsyncAdj);
// MPC_GetChunk(mpcRxMsgBuff, DOPPLER_OFFSET, DOPPLER_LEN, &dataChunk); // MPC_GetChunk(mpcRxMsgBuff, DOPPLER_OFFSET, DOPPLER_LEN, &dataChunk);
// double dopplerAdj = (double)((dataChunk << 32) / 192000000); // double dopplerAdj = (double)((dataChunk << 32) / 192000000);
@@ -274,18 +293,22 @@ int MPC_MsgHandler(MPC_INST* instPtr){
* error code. * error code.
* *
******************************************************************************/ ******************************************************************************/
int MPC_SendMsg(MPC_INST* instPtr, MPC_MSG_TYPE type, uint8_t* buffer, uint32_t dlen){ int MPC_SendMsg(MPC_INST *instPtr, MPC_MSG_TYPE type, uint8_t *buffer, uint32_t dlen)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
if (buffer == NULL || if (buffer == NULL ||
dlen >= instPtr->configs.msgLenMax || dlen >= instPtr->configs.msgLenMax ||
(type <= MPC_MSG_NONE || type > MPC_MSG_STATE)){ (type <= MPC_MSG_NONE || type > MPC_MSG_STATE))
{
return EPARAM; return EPARAM;
} }
if(instPtr->pcieBram.IsReady == false){ if (instPtr->pcieBram.IsReady == false)
{
return EDINVAL; return EDINVAL;
} }
int Status = SUCCESS; int Status = SUCCESS;
switch(type){ switch (type)
{
case MPC_MSG_TEST: case MPC_MSG_TEST:
mpcHeader.type = MPC_MSG_TEST_CODE; mpcHeader.type = MPC_MSG_TEST_CODE;
break; break;
@@ -302,7 +325,6 @@ int MPC_SendMsg(MPC_INST* instPtr, MPC_MSG_TYPE type, uint8_t* buffer, uint32_t
return Status; return Status;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Upload current state to master pc.(content detail defined in mpc * @brief Upload current state to master pc.(content detail defined in mpc
@@ -315,16 +337,19 @@ int MPC_SendMsg(MPC_INST* instPtr, MPC_MSG_TYPE type, uint8_t* buffer, uint32_t
* *
******************************************************************************/ ******************************************************************************/
int MPC_StateUpload(MPC_INST *instPtr, uint8_t *mgmtBuffer, uint32_t mgmtMsgCnt, int MPC_StateUpload(MPC_INST *instPtr, uint8_t *mgmtBuffer, uint32_t mgmtMsgCnt,
uint8_t* svcBuffer , uint32_t svcMsgCnt , L2_MGMT_MSG_RECORD* mgmtMsg){ uint8_t *svcBuffer, uint32_t svcMsgCnt, L2_MGMT_MSG_RECORD *mgmtMsg)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status = SUCCESS; int32_t Status = SUCCESS;
uint64_t dataChunk = 0; uint64_t dataChunk = 0;
if(instPtr->pcieBram.IsReady == false){ if (instPtr->pcieBram.IsReady == false)
{
return EDINVAL; return EDINVAL;
} }
// Stage // Stage
switch (linkInstPtr->mainState) { switch (linkInstPtr->mainState)
{
case L2_INIT_STATE: case L2_INIT_STATE:
dataChunk = STATE_STAGE_SETUP; dataChunk = STATE_STAGE_SETUP;
break; break;
@@ -434,9 +459,12 @@ int MPC_StateUpload(MPC_INST* instPtr , uint8_t* mgmtBuffer , uint32_t mgmtMsgC
// TODO : Currently DJ is node 1 , only Byte 1 is activate , others is useless // TODO : Currently DJ is node 1 , only Byte 1 is activate , others is useless
// Topo node 1 // Topo node 1
dataChunk = BIT(L2_GetNodeId(linkInstPtr) - 1); dataChunk = BIT(L2_GetNodeId(linkInstPtr) - 1);
if (L2_GetNodeListLen(linkInstPtr->nodeList) > 1 ) { if (L2_GetNodeListLen(linkInstPtr->nodeList) > 1)
for(int i = 1; i <= linkInstPtr->config.netScale; i++){ {
if (L2_isNodeInList(linkInstPtr->nodeList, i)) { for (int i = 1; i <= linkInstPtr->config.netScale; i++)
{
if (L2_isNodeInList(linkInstPtr->nodeList, i))
{
dataChunk |= BIT(i - 1); dataChunk |= BIT(i - 1);
} }
} }
+11 -8
View File
@@ -30,32 +30,37 @@
#include "../phy_ctrl/phy_ctrl.h" #include "../phy_ctrl/phy_ctrl.h"
#include "../linklayer/linklayer814.h" #include "../linklayer/linklayer814.h"
typedef struct { typedef struct
{
const uint16_t head; const uint16_t head;
uint16_t type; uint16_t type;
uint8_t reserve[4]; uint8_t reserve[4];
} MPC_MSG_HEADER; } MPC_MSG_HEADER;
typedef struct{ typedef struct
{
uint32_t bramDevId; uint32_t bramDevId;
uint32_t msgLenMax; uint32_t msgLenMax;
} MPC_CONFIG; } MPC_CONFIG;
typedef struct{ typedef struct
{
MPC_CONFIG configs; MPC_CONFIG configs;
XBram pcieBram; XBram pcieBram;
uint8_t *txBaseAddr; uint8_t *txBaseAddr;
uint8_t *rxBaseAddr; uint8_t *rxBaseAddr;
} MPC_INST; } MPC_INST;
typedef enum{ typedef enum
{
MPC_MSG_NONE = 0, MPC_MSG_NONE = 0,
MPC_MSG_TEST = 1, MPC_MSG_TEST = 1,
MPC_MSG_STATE = 2, MPC_MSG_STATE = 2,
} MPC_MSG_TYPE; } MPC_MSG_TYPE;
// FIXME: Simplify this part. // FIXME: Simplify this part.
typedef struct { typedef struct
{
uint8_t frame1Cnt; uint8_t frame1Cnt;
uint8_t frame2Cnt; uint8_t frame2Cnt;
uint8_t frame3Cnt; uint8_t frame3Cnt;
@@ -90,15 +95,13 @@ typedef struct {
uint16_t frame8Recv; uint16_t frame8Recv;
} MPC_RECORD; } MPC_RECORD;
extern void MPC_PCIESentStart(); extern void MPC_PCIESentStart();
extern MPC_RECORD *mpcRecord; extern MPC_RECORD *mpcRecord;
extern MPC_INST *mpcInstPtr; extern MPC_INST *mpcInstPtr;
int MPC_Init(MPC_INST **instPtr, MPC_CONFIG *configPtr); int MPC_Init(MPC_INST **instPtr, MPC_CONFIG *configPtr);
int MPC_MsgHandler(MPC_INST *instPtr); int MPC_MsgHandler(MPC_INST *instPtr);
int MPC_StateUpload(MPC_INST* instPtr , uint8_t* mgmtBuffer , uint32_t mgmtMsgCnt int MPC_StateUpload(MPC_INST *instPtr, uint8_t *mgmtBuffer, uint32_t mgmtMsgCnt, uint8_t *svcBuffer, uint32_t svcMsgCnt, L2_MGMT_MSG_RECORD *mgmtmsg);
, uint8_t* svcBuffer , uint32_t svcMsgCnt , L2_MGMT_MSG_RECORD* mgmtmsg);
int MPC_SendMsg(MPC_INST *instPtr, MPC_MSG_TYPE type, uint8_t *buffer, uint32_t dlen); int MPC_SendMsg(MPC_INST *instPtr, MPC_MSG_TYPE type, uint8_t *buffer, uint32_t dlen);
#endif //_MPC_PCIE #endif //_MPC_PCIE
-1
View File
@@ -119,7 +119,6 @@
#define STORE_ADDR_START_OFFSET 544 #define STORE_ADDR_START_OFFSET 544
// ** Time SyncAdj Message ** // // ** Time SyncAdj Message ** //
// Type code // Type code
#define MPC_MSG_TSYNC_CODE 0x00FF #define MPC_MSG_TSYNC_CODE 0x00FF
+65 -30
View File
@@ -48,39 +48,67 @@
#define MODECTRL_DEFAULT 0x00 #define MODECTRL_DEFAULT 0x00
uint16_t hopPattern_Config[L1_FRAME_MAX_HOPS] = {0}; uint16_t hopPattern_Config[L1_FRAME_MAX_HOPS] =
{
0
};
uint16_t hopPattern_2KBPS_20MS[L1_FRAME_2KBPS_20MS_HOPS] = {1500}; uint16_t hopPattern_2KBPS_20MS[L1_FRAME_2KBPS_20MS_HOPS] =
{
1500
};
uint16_t hopPattern_2KBPS_100MS[L1_FRAME_2KBPS_100MS_HOPS] = { uint16_t hopPattern_2KBPS_100MS[L1_FRAME_2KBPS_100MS_HOPS] = {
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400
}; };
uint16_t hopPattern_10KBPS_10MS[L1_FRAME_10KBPS_10MS_HOPS] = {1500}; uint16_t hopPattern_10KBPS_10MS[L1_FRAME_10KBPS_10MS_HOPS] =
{
uint16_t hopPattern_10KBPS_20MS[L1_FRAME_10KBPS_20MS_HOPS] = { 1500, 1600, 1700, 1500
1800, 1900, 2000, 1500, 1600, 1700, 1800, 1900 };
uint16_t hopPattern_40KBPS_10MS[L1_FRAME_40KBPS_10MS_HOPS] = {
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400
}; };
uint16_t hopPattern_40KBPS_20MS[L1_FRAME_40KBPS_20MS_HOPS] = { uint16_t hopPattern_10KBPS_20MS[L1_FRAME_10KBPS_20MS_HOPS] =
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400, {
2400, 2400, 2400, 2400, 2400, 2400, 2400 1500, 1600, 1700, 1800, 1900, 2000, 1500, 1600,
1700, 1800, 1900
}; };
uint16_t hopPattern_200KBPS_5MS[L1_FRAME_200KBPS_5MS_HOPS] ={ 1500, 1600, 1700, 1800, 1900, 2000, 1500, 1600 , 1700, uint16_t hopPattern_40KBPS_10MS[L1_FRAME_40KBPS_10MS_HOPS] =
1800, 1900, 2000, 1500, 1600 , 1700, 1800, 1900, 2000, 1500, 1500}; {
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400
};
uint16_t hopPattern_1M2BPS_5MS[L1_FRAME_1M2BPS_5MS_HOPS] = { 500, 600, 700, 800, 900, 1000, 500, 600 , 700, uint16_t hopPattern_40KBPS_20MS[L1_FRAME_40KBPS_20MS_HOPS] =
800, 900, 1000, 500, 600 , 700, 800, 900, 1000, 500, 500 }; {
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400, 2400, 2400, 2400, 2400, 2400,
2400, 2400, 2400
};
uint16_t hopPattern_200KBPS_5MS[L1_FRAME_200KBPS_5MS_HOPS] =
{
1500, 1600, 1700, 1800, 1900, 2000, 1500, 1600,
1700, 1800, 1900, 2000, 1500, 1600, 1700, 1800,
1900, 2000, 1500, 1500
};
uint16_t hopPattern_1M2BPS_5MS[L1_FRAME_1M2BPS_5MS_HOPS] =
{
500 , 600 , 700 , 800 , 900 , 1000, 500 , 600 ,
700 , 800 , 900 , 1000, 500 , 600 , 700 , 800 ,
900 , 1000, 500 , 500
};
// Pair by regex // Pair by regex
// Regex: ((\{\.oaxis = [0-9]+[ ,]+\.raxis = [0-9]+[ ]*\}, ){2}) // Regex: ((\{\.oaxis = [0-9]+[ ,]+\.raxis = [0-9]+[ ]*\}, ){2})
@@ -195,8 +223,14 @@ ANT_ANGLE antNrrwPattern[NUM_ANT_NRRW_PATTERN] = {
{.oaxis = 600, .raxis =2520} {.oaxis = 600, .raxis =2520}
}; };
#else #else
PHY_ANT_ANGLE antSubnrrwPattern[NUM_ANT_SUBNRRW_PATTERN] = {0}; PHY_ANT_ANGLE antSubnrrwPattern[NUM_ANT_SUBNRRW_PATTERN] =
PHY_ANT_ANGLE antNrrwPattern[NUM_ANT_NRRW_PATTERN] = {0}; {
0
};
PHY_ANT_ANGLE antNrrwPattern[NUM_ANT_NRRW_PATTERN] =
{
0
};
#endif #endif
/*****************************************************************************/ /*****************************************************************************/
/** /**
@@ -222,7 +256,6 @@ int32_t Phy_AntCoinfoInit(PHY_ANT_CONFIG *antCoinfoPtr)
antCoinfoPtr->antMode = ATN_MODE_DEFAULT; antCoinfoPtr->antMode = ATN_MODE_DEFAULT;
#endif #endif
// FIXME: Dummy default value. // FIXME: Dummy default value.
// off axis angle setting // off axis angle setting
antCoinfoPtr->oaxis[0] = 0; antCoinfoPtr->oaxis[0] = 0;
@@ -277,7 +310,8 @@ int32_t Phy_AntCoinfoInit(PHY_ANT_CONFIG *antCoinfoPtr)
int32_t Phy_SetBeamNo(PHY_INST *instPtr, int32_t beamNo) int32_t Phy_SetBeamNo(PHY_INST *instPtr, int32_t beamNo)
{ {
if(instPtr->beamWd.next == ANT_BEAM_8){ if (instPtr->beamWd.next == ANT_BEAM_8)
{
if (beamNo >= NUM_ANT_NRRW_PATTERN) if (beamNo >= NUM_ANT_NRRW_PATTERN)
{ {
return FAILURE; return FAILURE;
@@ -292,7 +326,8 @@ int32_t Phy_SetBeamNo(PHY_INST *instPtr, int32_t beamNo)
return SUCCESS; return SUCCESS;
} }
} }
else if(instPtr->beamWd.next == ANT_BEAM_25){ else if (instPtr->beamWd.next == ANT_BEAM_25)
{
if (beamNo >= NUM_ANT_SUBNRRW_PATTERN) if (beamNo >= NUM_ANT_SUBNRRW_PATTERN)
{ {
return FAILURE; return FAILURE;
@@ -307,7 +342,8 @@ int32_t Phy_SetBeamNo(PHY_INST *instPtr, int32_t beamNo)
return SUCCESS; return SUCCESS;
} }
} }
else{ else
{
// Beam number assign not support. // Beam number assign not support.
// Assign default angle (0,0) // Assign default angle (0,0)
instPtr->beamNo.next = 0; instPtr->beamNo.next = 0;
@@ -368,4 +404,3 @@ int32_t Phy_SetBeamAngle(PHY_INST *instPtr, double offAx, double rndAx)
instPtr->antCoinfoPtr.raxis[0] = (uint8_t)(rndAxDeg >> 8); instPtr->antCoinfoPtr.raxis[0] = (uint8_t)(rndAxDeg >> 8);
return SUCCESS; return SUCCESS;
} }
+135 -78
View File
@@ -50,14 +50,16 @@ static int32_t Phy_SetSendData(PHY_INST* instPtr, L1_FRAME_TYPE frameType, uint8
* SUCCESS Setting data successful. * SUCCESS Setting data successful.
* *
******************************************************************************/ ******************************************************************************/
static int32_t Phy_SetSendData(PHY_INST* instPtr, L1_FRAME_TYPE frameType, uint8_t* dataBuff) { static int32_t Phy_SetSendData(PHY_INST *instPtr, L1_FRAME_TYPE frameType, uint8_t *dataBuff)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(dataBuff != NULL); ASSERT(dataBuff != NULL);
uint32_t frameLen, sendCmd; uint32_t frameLen, sendCmd;
frameLen = Phy_GetFrameLen(frameType); frameLen = Phy_GetFrameLen(frameType);
if (frameLen == L1_FRAME_NONE_LEN) { if (frameLen == L1_FRAME_NONE_LEN)
{
DBG_ERROR("Invalid frame length.\n"); DBG_ERROR("Invalid frame length.\n");
return EPARAM; return EPARAM;
} }
@@ -72,7 +74,6 @@ static int32_t Phy_SetSendData(PHY_INST* instPtr, L1_FRAME_TYPE frameType, uint8
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Check whether a frame was detected. (Do not check if the frame was * @brief Check whether a frame was detected. (Do not check if the frame was
@@ -84,12 +85,15 @@ static int32_t Phy_SetSendData(PHY_INST* instPtr, L1_FRAME_TYPE frameType, uint8
* false * false
* *
******************************************************************************/ ******************************************************************************/
inline bool Phy_IsFrameDetected(PHY_INST* instPtr) { inline bool Phy_IsFrameDetected(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
if(instPtr->isAcq && instPtr->isFrameSync) { if (instPtr->isAcq && instPtr->isFrameSync)
{
return true; return true;
} }
else { else
{
return false; return false;
} }
} }
@@ -105,12 +109,15 @@ inline bool Phy_IsFrameDetected(PHY_INST* instPtr) {
* false * false
* *
******************************************************************************/ ******************************************************************************/
inline bool Phy_IsFrameReceived(PHY_INST* instPtr) { inline bool Phy_IsFrameReceived(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
if(instPtr->isAcq && instPtr->isFrameSync && instPtr->isDecode) { if (instPtr->isAcq && instPtr->isFrameSync && instPtr->isDecode)
{
return true; return true;
} }
else { else
{
return false; return false;
} }
} }
@@ -126,7 +133,8 @@ inline bool Phy_IsFrameReceived(PHY_INST* instPtr) {
* false * false
* *
******************************************************************************/ ******************************************************************************/
inline bool Phy_IsFrameValid(PHY_INST* instPtr) { inline bool Phy_IsFrameValid(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return instPtr->isValid; return instPtr->isValid;
} }
@@ -140,13 +148,14 @@ inline bool Phy_IsFrameValid(PHY_INST* instPtr) {
* @return Layer2 frame type. Refer "frame_def.h" * @return Layer2 frame type. Refer "frame_def.h"
* *
******************************************************************************/ ******************************************************************************/
inline L1_FRAME_TYPE Phy_GetLastRecvFrameType(PHY_INST *instPtr) { inline L1_FRAME_TYPE Phy_GetLastRecvFrameType(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
if (Phy_IsFrameReceived(instPtr) == false) { if (Phy_IsFrameReceived(instPtr) == false)
{
return L1_NONE_FRAME; return L1_NONE_FRAME;
} }
int32_t frame_type = (PhyRead(instPtr, PHY_MODE_CTRL) & PHY_MODE_CTRL_FRAME_MASK) int32_t frame_type = (PhyRead(instPtr, PHY_MODE_CTRL) & PHY_MODE_CTRL_FRAME_MASK) >> PHY_MODE_CTRL_FRAME_SHIFT;
>> PHY_MODE_CTRL_FRAME_SHIFT;
if (frame_type < L1_1M2BPS_5MS_FRAME) if (frame_type < L1_1M2BPS_5MS_FRAME)
return frame_type; return frame_type;
else else
@@ -162,9 +171,11 @@ inline L1_FRAME_TYPE Phy_GetLastRecvFrameType(PHY_INST *instPtr) {
* @return Layer2 frame type. Refer "frame_def.h" * @return Layer2 frame type. Refer "frame_def.h"
* *
******************************************************************************/ ******************************************************************************/
inline L1_FRAME_TYPE Phy_GetLastSendFrameType(PHY_INST* instPtr) { inline L1_FRAME_TYPE Phy_GetLastSendFrameType(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
if (instPtr->TOR.last != PHY_TOR_TX) { if (instPtr->TOR.last != PHY_TOR_TX)
{
return L1_NONE_FRAME; return L1_NONE_FRAME;
} }
return instPtr->frameType.last; return instPtr->frameType.last;
@@ -179,7 +190,8 @@ inline L1_FRAME_TYPE Phy_GetLastSendFrameType(PHY_INST* instPtr) {
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
static void Phy_RcdShift(PHY_RCD* rcd, int32_t netxVal){ static void Phy_RcdShift(PHY_RCD *rcd, int32_t netxVal)
{
ASSERT(rcd != NULL); ASSERT(rcd != NULL);
rcd->last = rcd->curr; rcd->last = rcd->curr;
rcd->curr = rcd->next; rcd->curr = rcd->next;
@@ -196,8 +208,10 @@ static void Phy_RcdShift(PHY_RCD* rcd, int32_t netxVal){
* L1_FRAME_NONE_LEN if the frame type code cannot be recognized. * L1_FRAME_NONE_LEN if the frame type code cannot be recognized.
* *
******************************************************************************/ ******************************************************************************/
uint32_t Phy_GetFrameLen(L1_FRAME_TYPE frameType) { uint32_t Phy_GetFrameLen(L1_FRAME_TYPE frameType)
switch (frameType) { {
switch (frameType)
{
case L1_2KBPS_20MS_FRAME: case L1_2KBPS_20MS_FRAME:
return L1_FRAME_2KBPS_20MS_LEN; return L1_FRAME_2KBPS_20MS_LEN;
case L1_2KBPS_100MS_FRAME: case L1_2KBPS_100MS_FRAME:
@@ -228,7 +242,8 @@ uint32_t Phy_GetFrameLen(L1_FRAME_TYPE frameType) {
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void Phy_UpdateState(PHY_INST* instPtr) { void Phy_UpdateState(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
/* Update Frame Sate. */ /* Update Frame Sate. */
@@ -240,10 +255,12 @@ void Phy_UpdateState(PHY_INST* instPtr) {
instPtr->isAcq = true; instPtr->isAcq = true;
instPtr->isFrameSync = true; instPtr->isFrameSync = true;
instPtr->isDecode = ((txrxSate & TXRX_STATE_DECODE_MASK) == TXRX_STATE_DECODE_SUCCESS) ? true : false; instPtr->isDecode = ((txrxSate & TXRX_STATE_DECODE_MASK) == TXRX_STATE_DECODE_SUCCESS) ? true : false;
if (Phy_IsFrameReceived(instPtr)) { if (Phy_IsFrameReceived(instPtr))
{
instPtr->isValid = ((txrxSate & TXRX_STATE_CRC_MASK) == TXRX_STATE_CRC_OK) ? true : false; instPtr->isValid = ((txrxSate & TXRX_STATE_CRC_MASK) == TXRX_STATE_CRC_OK) ? true : false;
} }
else { else
{
instPtr->isValid = false; instPtr->isValid = false;
} }
instPtr->recvSlotTime.int_part = PhyRead(instPtr, TM_TARGET_H); instPtr->recvSlotTime.int_part = PhyRead(instPtr, TM_TARGET_H);
@@ -252,7 +269,6 @@ void Phy_UpdateState(PHY_INST* instPtr) {
instPtr->recvFsTime.frac_part = PhyRead(instPtr, FSTIME_L); instPtr->recvFsTime.frac_part = PhyRead(instPtr, FSTIME_L);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Initialize physical layer instance accordingly. * @brief Initialize physical layer instance accordingly.
@@ -263,13 +279,15 @@ void Phy_UpdateState(PHY_INST* instPtr) {
* @return SUCCESS * @return SUCCESS
* *
******************************************************************************/ ******************************************************************************/
int32_t Phy_InstInit(PHY_INST** instPtr, PHY_CONFIG *phyConfig) { int32_t Phy_InstInit(PHY_INST **instPtr, PHY_CONFIG *phyConfig)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(phyConfig != NULL); ASSERT(phyConfig != NULL);
int32_t Status; int32_t Status;
*instPtr = (PHY_INST *)malloc(sizeof(PHY_INST)); *instPtr = (PHY_INST *)malloc(sizeof(PHY_INST));
if ((*instPtr) == NULL) { if ((*instPtr) == NULL)
{
return EALLOC; return EALLOC;
} }
memcpy(&(*instPtr)->phyCofig, phyConfig, sizeof(PHY_CONFIG)); memcpy(&(*instPtr)->phyCofig, phyConfig, sizeof(PHY_CONFIG));
@@ -301,56 +319,65 @@ int32_t Phy_InstInit(PHY_INST** instPtr, PHY_CONFIG *phyConfig) {
XBram_Config *ConfigPtr; XBram_Config *ConfigPtr;
ConfigPtr = XBram_LookupConfig(phyConfig->antBramId); ConfigPtr = XBram_LookupConfig(phyConfig->antBramId);
if (ConfigPtr == NULL) { if (ConfigPtr == NULL)
{
Status = EDINVAL; Status = EDINVAL;
goto error; goto error;
} }
Status = XBram_CfgInitialize(&antInfoBram, ConfigPtr, Status = XBram_CfgInitialize(&antInfoBram, ConfigPtr,
ConfigPtr->CtrlBaseAddress); ConfigPtr->CtrlBaseAddress);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto error; goto error;
} }
/* /*
* Execute the BRAM driver selftest. * Execute the BRAM driver selftest.
*/ */
Status = XBram_SelfTest(&antInfoBram, 0); Status = XBram_SelfTest(&antInfoBram, 0);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto error; goto error;
} }
ConfigPtr = XBram_LookupConfig(phyConfig->txBramId); ConfigPtr = XBram_LookupConfig(phyConfig->txBramId);
if (ConfigPtr == NULL) { if (ConfigPtr == NULL)
{
Status = EDINVAL; Status = EDINVAL;
goto error; goto error;
} }
Status = XBram_CfgInitialize(&txFrameBram, ConfigPtr, Status = XBram_CfgInitialize(&txFrameBram, ConfigPtr,
ConfigPtr->CtrlBaseAddress); ConfigPtr->CtrlBaseAddress);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto error; goto error;
} }
/* /*
* Execute the BRAM driver selftest. * Execute the BRAM driver selftest.
*/ */
Status = XBram_SelfTest(&txFrameBram, 0); Status = XBram_SelfTest(&txFrameBram, 0);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto error; goto error;
} }
ConfigPtr = XBram_LookupConfig(phyConfig->rxBramId); ConfigPtr = XBram_LookupConfig(phyConfig->rxBramId);
if (ConfigPtr == NULL) { if (ConfigPtr == NULL)
{
Status = EDINVAL; Status = EDINVAL;
goto error; goto error;
} }
Status = XBram_CfgInitialize(&rxFrameBram, ConfigPtr, Status = XBram_CfgInitialize(&rxFrameBram, ConfigPtr,
ConfigPtr->CtrlBaseAddress); ConfigPtr->CtrlBaseAddress);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto error; goto error;
} }
/* /*
* Execute the BRAM driver selftest. * Execute the BRAM driver selftest.
*/ */
Status = XBram_SelfTest(&rxFrameBram, 0); Status = XBram_SelfTest(&rxFrameBram, 0);
if (Status != SUCCESS) { if (Status != SUCCESS)
{
goto error; goto error;
} }
@@ -359,17 +386,20 @@ int32_t Phy_InstInit(PHY_INST** instPtr, PHY_CONFIG *phyConfig) {
(*instPtr)->sendDataAddr = txFrameBram.Config.MemBaseAddress; (*instPtr)->sendDataAddr = txFrameBram.Config.MemBaseAddress;
#else #else
(*instPtr)->antConfigAddr = (uintptr_t)calloc(ANTCONFIG_MAX_LEN / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->antConfigAddr = (uintptr_t)calloc(ANTCONFIG_MAX_LEN / sizeof(uint64_t), sizeof(uint64_t));
if ((*instPtr)->antConfigAddr == (uintptr_t)NULL) { if ((*instPtr)->antConfigAddr == (uintptr_t)NULL)
{
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
(*instPtr)->recvDataAddr = (uintptr_t)calloc(TXRXDATA_MAX_LEN / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->recvDataAddr = (uintptr_t)calloc(TXRXDATA_MAX_LEN / sizeof(uint64_t), sizeof(uint64_t));
if ((*instPtr)->recvDataAddr == (uintptr_t)NULL) { if ((*instPtr)->recvDataAddr == (uintptr_t)NULL)
{
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
(*instPtr)->sendDataAddr = (uintptr_t)calloc(TXRXDATA_MAX_LEN / sizeof(uint64_t), sizeof(uint64_t)); (*instPtr)->sendDataAddr = (uintptr_t)calloc(TXRXDATA_MAX_LEN / sizeof(uint64_t), sizeof(uint64_t));
if ((*instPtr)->sendDataAddr == (uintptr_t)NULL) { if ((*instPtr)->sendDataAddr == (uintptr_t)NULL)
{
Status = EALLOC; Status = EALLOC;
goto error; goto error;
} }
@@ -389,8 +419,10 @@ error:
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_Destroy(PHY_INST* instPtr) { void Phy_Destroy(PHY_INST *instPtr)
if (instPtr == NULL){ {
if (instPtr == NULL)
{
return; return;
} }
#ifdef EMULATION #ifdef EMULATION
@@ -410,10 +442,12 @@ void Phy_Destroy(PHY_INST* instPtr) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_Start(PHY_INST* instPtr) { void Phy_Start(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint32_t phyMainCtrl = PhyRead(instPtr, PHY_MAIN_CTRL); uint32_t phyMainCtrl = PhyRead(instPtr, PHY_MAIN_CTRL);
if((phyMainCtrl & PHY_MAIN_CTRL_EN_MASK) == PHY_MAIN_CTRL_EN_EN) { if ((phyMainCtrl & PHY_MAIN_CTRL_EN_MASK) == PHY_MAIN_CTRL_EN_EN)
{
DBG_WARNING("Physical layer was allready enabled.\n"); DBG_WARNING("Physical layer was allready enabled.\n");
} }
instPtr->isPhyEn = true; instPtr->isPhyEn = true;
@@ -430,10 +464,12 @@ void Phy_Start(PHY_INST* instPtr) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_Halt(PHY_INST* instPtr) { void Phy_Halt(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint32_t phyMainCtrl = PhyRead(instPtr, PHY_MAIN_CTRL); uint32_t phyMainCtrl = PhyRead(instPtr, PHY_MAIN_CTRL);
if((phyMainCtrl & PHY_MAIN_CTRL_EN_MASK) == PHY_MAIN_CTRL_EN_DSBL) { if ((phyMainCtrl & PHY_MAIN_CTRL_EN_MASK) == PHY_MAIN_CTRL_EN_DSBL)
{
DBG_WARNING("Physical layer was allready halted.\n"); DBG_WARNING("Physical layer was allready halted.\n");
} }
instPtr->isPhyEn = false; instPtr->isPhyEn = false;
@@ -451,11 +487,13 @@ void Phy_Halt(PHY_INST* instPtr) {
* other * other
* *
******************************************************************************/ ******************************************************************************/
int32_t Phy_SetMode(PHY_INST* instPtr, PHY_MODE modeCod) { int32_t Phy_SetMode(PHY_INST *instPtr, PHY_MODE modeCod)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint32_t phyMainCtrl = PhyRead(instPtr, PHY_MAIN_CTRL) & ~PHY_MAIN_CTRL_MODE_MASK; uint32_t phyMainCtrl = PhyRead(instPtr, PHY_MAIN_CTRL) & ~PHY_MAIN_CTRL_MODE_MASK;
switch (modeCod) { switch (modeCod)
{
case PHY_MODE_NONE: case PHY_MODE_NONE:
phyMainCtrl |= PHY_MAIN_CTRL_MODE_NONE; phyMainCtrl |= PHY_MAIN_CTRL_MODE_NONE;
break; break;
@@ -489,7 +527,8 @@ int32_t Phy_SetMode(PHY_INST* instPtr, PHY_MODE modeCod) {
* *
******************************************************************************/ ******************************************************************************/
int32_t Phy_SetupNextSlot(PHY_INST *instPtr, PHY_TOR_TYPE txrxMode, int32_t Phy_SetupNextSlot(PHY_INST *instPtr, PHY_TOR_TYPE txrxMode,
L1_FRAME_TYPE frameType, uint8_t* dataBuff) { L1_FRAME_TYPE frameType, uint8_t *dataBuff)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
int32_t Status; int32_t Status;
uint32_t phyModeCtrl, hopNum; uint32_t phyModeCtrl, hopNum;
@@ -501,7 +540,8 @@ int32_t Phy_SetupNextSlot(PHY_INST* instPtr, PHY_TOR_TYPE txrxMode,
instPtr->TOR.next = txrxMode; instPtr->TOR.next = txrxMode;
instPtr->frameType.next = frameType; instPtr->frameType.next = frameType;
switch (txrxMode) { switch (txrxMode)
{
case PHY_TOR_SUSPEND: case PHY_TOR_SUSPEND:
phyModeCtrl = PHY_MODE_CTRL_TOR_SUSPEND; phyModeCtrl = PHY_MODE_CTRL_TOR_SUSPEND;
break; break;
@@ -514,7 +554,8 @@ int32_t Phy_SetupNextSlot(PHY_INST* instPtr, PHY_TOR_TYPE txrxMode,
default: default:
return EPARAM; return EPARAM;
} }
switch (frameType) { switch (frameType)
{
case L1_2KBPS_20MS_FRAME: case L1_2KBPS_20MS_FRAME:
phyModeCtrl |= PHY_MODE_CTRL_FRAME_2KBPS_20MS; phyModeCtrl |= PHY_MODE_CTRL_FRAME_2KBPS_20MS;
hopNum = L1_FRAME_2KBPS_20MS_HOPS; hopNum = L1_FRAME_2KBPS_20MS_HOPS;
@@ -562,16 +603,14 @@ int32_t Phy_SetupNextSlot(PHY_INST* instPtr, PHY_TOR_TYPE txrxMode,
hopPattern = (instPtr->isPresetHop) ? hopPattern : hopPattern_Config; hopPattern = (instPtr->isPresetHop) ? hopPattern : hopPattern_Config;
uint8_t *dataSrcPtr = (uint8_t *)(&(instPtr->antCoinfoPtr)); uint8_t *dataSrcPtr = (uint8_t *)(&(instPtr->antCoinfoPtr));
for (int32_t i = 0; i < hopNum; i++) { for (int32_t i = 0; i < hopNum; i++)
{
instPtr->antCoinfoPtr.antFreq[1] = (uint8_t)(hopPattern[i] & 0x00FF); instPtr->antCoinfoPtr.antFreq[1] = (uint8_t)(hopPattern[i] & 0x00FF);
instPtr->antCoinfoPtr.antFreq[0] = (uint8_t)( instPtr->antCoinfoPtr.antFreq[0] = (uint8_t)((hopPattern[i] >> 8) & 0x00FF);
(hopPattern[i] >> 8) & 0x00FF);
memcpy((uint8_t *)(antConfigAddr + i * sizeof(PHY_ANT_CONFIG)), memcpy((uint8_t *)(antConfigAddr + i * sizeof(PHY_ANT_CONFIG)),
dataSrcPtr, sizeof(PHY_ANT_CONFIG)); dataSrcPtr, sizeof(PHY_ANT_CONFIG));
} }
uint32_t antCtrl = (instPtr->antConfigOffset & ANT_CONFIG_OFFSET_MASK) uint32_t antCtrl = (instPtr->antConfigOffset & ANT_CONFIG_OFFSET_MASK) | ((hopNum << ANT_CONFIG_NUM_SHIFT) & ANT_CONFIG_NUM_MASK) | ((sizeof(PHY_ANT_CONFIG) << ANT_CONFIG_LEN_SHIFT) & ANT_CONFIG_LEN_MASK);
| ((hopNum << ANT_CONFIG_NUM_SHIFT) & ANT_CONFIG_NUM_MASK)
| ((sizeof(PHY_ANT_CONFIG) << ANT_CONFIG_LEN_SHIFT) & ANT_CONFIG_LEN_MASK);
/* /*
memcpy((uint8_t*)antConfigDataAddr, hopPattern, hopNum*sizeof(uint16_t)); memcpy((uint8_t*)antConfigDataAddr, hopPattern, hopNum*sizeof(uint16_t));
@@ -582,11 +621,12 @@ int32_t Phy_SetupNextSlot(PHY_INST* instPtr, PHY_TOR_TYPE txrxMode,
PhyWrite(instPtr, PHY_MODE_CTRL, phyModeCtrl | PHY_MODE_CTRL_CRC_EN); PhyWrite(instPtr, PHY_MODE_CTRL, phyModeCtrl | PHY_MODE_CTRL_CRC_EN);
PhyWrite(instPtr, ANT_CONFIG, antCtrl); PhyWrite(instPtr, ANT_CONFIG, antCtrl);
// Trigger physical configuration. // Trigger physical configuration.
if(txrxMode == PHY_TOR_TX) { if (txrxMode == PHY_TOR_TX)
{
Status = Phy_SetSendData(instPtr, frameType, dataBuff); Status = Phy_SetSendData(instPtr, frameType, dataBuff);
if(Status != SUCCESS) { if (Status != SUCCESS)
{
DBG_ERROR("Failed to apply send data.\n"); DBG_ERROR("Failed to apply send data.\n");
return Status; return Status;
} }
@@ -602,7 +642,6 @@ int32_t Phy_SetupNextSlot(PHY_INST* instPtr, PHY_TOR_TYPE txrxMode,
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Get coarse frequency estimation of last valid frame. * @brief Get coarse frequency estimation of last valid frame.
@@ -612,7 +651,8 @@ int32_t Phy_SetupNextSlot(PHY_INST* instPtr, PHY_TOR_TYPE txrxMode,
* @return frequency estimation * @return frequency estimation
* *
******************************************************************************/ ******************************************************************************/
double Phy_GetCoarseFreqEst(PHY_INST* instPtr) { double Phy_GetCoarseFreqEst(PHY_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
uint32_t regValue; uint32_t regValue;
uint8_t acqFreqIdx; uint8_t acqFreqIdx;
@@ -621,7 +661,8 @@ double Phy_GetCoarseFreqEst(PHY_INST* instPtr) {
double freqEst; double freqEst;
frameType = Phy_GetLastRecvFrameType(instPtr); frameType = Phy_GetLastRecvFrameType(instPtr);
if(frameType > L1_1M2BPS_5MS_FRAME || frameType == L1_NONE_FRAME){ if (frameType > L1_1M2BPS_5MS_FRAME || frameType == L1_NONE_FRAME)
{
return INFINITY; // FIXME return INFINITY; // FIXME
} }
regValue = PhyRead(instPtr, COASE_FREQEST); regValue = PhyRead(instPtr, COASE_FREQEST);
@@ -629,11 +670,13 @@ double Phy_GetCoarseFreqEst(PHY_INST* instPtr) {
acqFreqIdx = 80 - acqFreqIdx; acqFreqIdx = 80 - acqFreqIdx;
syncFreqIdx = (regValue >> COASE_FREQEST_SYNC_SHIFT) && COASE_FREQEST_SYNC_MASK; syncFreqIdx = (regValue >> COASE_FREQEST_SYNC_SHIFT) && COASE_FREQEST_SYNC_MASK;
syncFreqIdx = 8 - syncFreqIdx; syncFreqIdx = 8 - syncFreqIdx;
if(frameType >= L1_200KBPS_5MS_FRAME) { if (frameType >= L1_200KBPS_5MS_FRAME)
{
freqEst = (double)acqFreqIdx * (250e3 / 4); freqEst = (double)acqFreqIdx * (250e3 / 4);
freqEst += (double)syncFreqIdx * 1.5e6 / 64 / 128; freqEst += (double)syncFreqIdx * 1.5e6 / 64 / 128;
} }
else { else
{
freqEst = acqFreqIdx * (250e3 / 4); freqEst = acqFreqIdx * (250e3 / 4);
freqEst += (double)syncFreqIdx * 12e6 / 64 / 128; freqEst += (double)syncFreqIdx * 12e6 / 64 / 128;
} }
@@ -652,8 +695,10 @@ double Phy_GetCoarseFreqEst(PHY_INST* instPtr) {
* FAILURE frame type cannot be recognized * FAILURE frame type cannot be recognized
* *
******************************************************************************/ ******************************************************************************/
uint32_t Phy_GetHopLength(L1_FRAME_TYPE frameType){ uint32_t Phy_GetHopLength(L1_FRAME_TYPE frameType)
switch (frameType) { {
switch (frameType)
{
case L1_2KBPS_20MS_FRAME: case L1_2KBPS_20MS_FRAME:
return L1_FRAME_2KBPS_20MS_HOPS; return L1_FRAME_2KBPS_20MS_HOPS;
case L1_2KBPS_100MS_FRAME: case L1_2KBPS_100MS_FRAME:
@@ -686,8 +731,10 @@ uint32_t Phy_GetHopLength(L1_FRAME_TYPE frameType){
* FAILURE frame type cannot be recognized * FAILURE frame type cannot be recognized
* *
******************************************************************************/ ******************************************************************************/
uint32_t Phy_GetTimeComp(L1_FRAME_TYPE frameType){ uint32_t Phy_GetTimeComp(L1_FRAME_TYPE frameType)
switch (frameType) { {
switch (frameType)
{
case L1_2KBPS_20MS_FRAME: case L1_2KBPS_20MS_FRAME:
return L1_FRAME_2KBPS_20MS_COMP; return L1_FRAME_2KBPS_20MS_COMP;
case L1_2KBPS_100MS_FRAME: case L1_2KBPS_100MS_FRAME:
@@ -720,9 +767,11 @@ uint32_t Phy_GetTimeComp(L1_FRAME_TYPE frameType){
* FAILURE pattern length exceeds maximum length. * FAILURE pattern length exceeds maximum length.
* *
******************************************************************************/ ******************************************************************************/
int32_t Phy_SetHopPattern(void* patternBuff, uint32_t patternLen){ int32_t Phy_SetHopPattern(void *patternBuff, uint32_t patternLen)
{
ASSERT(patternBuff != NULL); ASSERT(patternBuff != NULL);
if(patternLen > L1_FRAME_MAX_HOPS){ if (patternLen > L1_FRAME_MAX_HOPS)
{
return FAILURE; return FAILURE;
} }
memcpy(hopPattern_Config, patternBuff, patternLen * sizeof(uint16_t)); memcpy(hopPattern_Config, patternBuff, patternLen * sizeof(uint16_t));
@@ -740,12 +789,15 @@ int32_t Phy_SetHopPattern(void* patternBuff, uint32_t patternLen){
* FAILURE pattern length exceeds maximum length. * FAILURE pattern length exceeds maximum length.
* *
******************************************************************************/ ******************************************************************************/
int32_t Phy_SetTimeWord(PHY_INST* instPtr, const TIME_WORD* tmWord, PHY_TIME_WORD_TYPE tmType){ int32_t Phy_SetTimeWord(PHY_INST *instPtr, const TIME_WORD *tmWord, PHY_TIME_WORD_TYPE tmType)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
if(tmWord->frac_part>=PHY_TIME_WORD_L_MAX){ if (tmWord->frac_part >= PHY_TIME_WORD_L_MAX)
{
return EPARAM; return EPARAM;
} }
switch(tmType){ switch (tmType)
{
case TIME_WORD_POLL: case TIME_WORD_POLL:
PhyWrite(instPtr, POLLTIME_L, tmWord->frac_part); PhyWrite(instPtr, POLLTIME_L, tmWord->frac_part);
PhyWrite(instPtr, POLLTIME_H, tmWord->int_part); PhyWrite(instPtr, POLLTIME_H, tmWord->int_part);
@@ -760,7 +812,6 @@ int32_t Phy_SetTimeWord(PHY_INST* instPtr, const TIME_WORD* tmWord, PHY_TIME_WOR
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Get the frame sync time of last frame. * @brief Get the frame sync time of last frame.
@@ -771,7 +822,8 @@ int32_t Phy_SetTimeWord(PHY_INST* instPtr, const TIME_WORD* tmWord, PHY_TIME_WOR
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_GetFrameSyncTimeStamp(PHY_INST* instPtr, TIME_WORD* frameSyncT) { void Phy_GetFrameSyncTimeStamp(PHY_INST *instPtr, TIME_WORD *frameSyncT)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(frameSyncT != NULL); ASSERT(frameSyncT != NULL);
frameSyncT->int_part = PhyRead(instPtr, FSTIME_H); frameSyncT->int_part = PhyRead(instPtr, FSTIME_H);
@@ -788,7 +840,8 @@ void Phy_GetFrameSyncTimeStamp(PHY_INST* instPtr, TIME_WORD* frameSyncT) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_GetLastReceivedFrameSyncTimeStamp(PHY_INST* instPtr, TIME_WORD* frameSyncT) { void Phy_GetLastReceivedFrameSyncTimeStamp(PHY_INST *instPtr, TIME_WORD *frameSyncT)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(frameSyncT != NULL); ASSERT(frameSyncT != NULL);
memcpy(frameSyncT, &(instPtr->recvFsTime), sizeof(TIME_WORD)); memcpy(frameSyncT, &(instPtr->recvFsTime), sizeof(TIME_WORD));
@@ -804,7 +857,8 @@ void Phy_GetLastReceivedFrameSyncTimeStamp(PHY_INST* instPtr, TIME_WORD* frameSy
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_GetSlotStartTimeStamp(PHY_INST* instPtr, TIME_WORD* slotStart) { void Phy_GetSlotStartTimeStamp(PHY_INST *instPtr, TIME_WORD *slotStart)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(slotStart != NULL); ASSERT(slotStart != NULL);
slotStart->int_part = PhyRead(instPtr, TM_TARGET_H); slotStart->int_part = PhyRead(instPtr, TM_TARGET_H);
@@ -821,7 +875,8 @@ void Phy_GetSlotStartTimeStamp(PHY_INST* instPtr, TIME_WORD* slotStart) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_GetLastReceivedSlotStartTimeStamp(PHY_INST* instPtr, TIME_WORD* slotStart) { void Phy_GetLastReceivedSlotStartTimeStamp(PHY_INST *instPtr, TIME_WORD *slotStart)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(slotStart != NULL); ASSERT(slotStart != NULL);
memcpy(slotStart, &(instPtr->recvSlotTime), sizeof(TIME_WORD)); memcpy(slotStart, &(instPtr->recvSlotTime), sizeof(TIME_WORD));
@@ -836,7 +891,8 @@ void Phy_GetLastReceivedSlotStartTimeStamp(PHY_INST* instPtr, TIME_WORD* slotSta
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_PeekRecvData(PHY_INST* instPtr, uint8_t* dataBuff) { void Phy_PeekRecvData(PHY_INST *instPtr, uint8_t *dataBuff)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(dataBuff != NULL); ASSERT(dataBuff != NULL);
if (instPtr->isValid == false) if (instPtr->isValid == false)
@@ -861,7 +917,8 @@ void Phy_PeekRecvData(PHY_INST* instPtr, uint8_t* dataBuff) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void Phy_RetrieveRecvData(PHY_INST* instPtr, uint8_t* dataBuff) { void Phy_RetrieveRecvData(PHY_INST *instPtr, uint8_t *dataBuff)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(dataBuff != NULL); ASSERT(dataBuff != NULL);
if (instPtr->isValid == false) if (instPtr->isValid == false)
+20 -11
View File
@@ -69,14 +69,14 @@
#define L1_FRAME_200KBPS_5MS_COMP 19725 // *10ns(Timer_L) #define L1_FRAME_200KBPS_5MS_COMP 19725 // *10ns(Timer_L)
#define L1_FRAME_1M2BPS_5MS_COMP 19725 // *10ns(Timer_L) #define L1_FRAME_1M2BPS_5MS_COMP 19725 // *10ns(Timer_L)
#define PHY_TIME_WORD_H_MAX UINT32_MAX #define PHY_TIME_WORD_H_MAX UINT32_MAX
#define PHY_TIME_WORD_L_MAX 0x0007A120 // 5ms/10ns #define PHY_TIME_WORD_L_MAX 0x0007A120 // 5ms/10ns
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef enum { typedef enum
{
L1_NONE_FRAME = 0, L1_NONE_FRAME = 0,
L1_2KBPS_20MS_FRAME = 1, L1_2KBPS_20MS_FRAME = 1,
L1_2KBPS_100MS_FRAME = 2, L1_2KBPS_100MS_FRAME = 2,
@@ -88,7 +88,8 @@ typedef enum {
L1_1M2BPS_5MS_FRAME = 8 L1_1M2BPS_5MS_FRAME = 8
} L1_FRAME_TYPE; } L1_FRAME_TYPE;
typedef struct { typedef struct
{
/* Antenna control frame head 0xAA */ /* Antenna control frame head 0xAA */
uint8_t head; uint8_t head;
/* default 0, reserved for further use */ /* default 0, reserved for further use */
@@ -146,7 +147,8 @@ typedef struct {
uint8_t dummy[6]; uint8_t dummy[6];
} PHY_ANT_CONFIG; } PHY_ANT_CONFIG;
typedef struct { typedef struct
{
/* off-axis angle range is 0~60, /* off-axis angle range is 0~60,
* 0.1 degree each LSB */ * 0.1 degree each LSB */
uint16_t oaxis; uint16_t oaxis;
@@ -155,13 +157,15 @@ typedef struct {
uint16_t raxis; uint16_t raxis;
} PHY_ANT_ANGLE; } PHY_ANT_ANGLE;
typedef enum { typedef enum
{
TIME_WORD_NONE = 0, TIME_WORD_NONE = 0,
TIME_WORD_POLL = 1, TIME_WORD_POLL = 1,
TIME_WORD_LISTEN_TIMEOUT = 2 TIME_WORD_LISTEN_TIMEOUT = 2
} PHY_TIME_WORD_TYPE; } PHY_TIME_WORD_TYPE;
typedef enum { typedef enum
{
ANT_BEAM_NONE = 0, ANT_BEAM_NONE = 0,
ANT_BEAM_120 = 1, ANT_BEAM_120 = 1,
ANT_BEAM_60 = 2, ANT_BEAM_60 = 2,
@@ -169,20 +173,23 @@ typedef enum {
ANT_BEAM_8 = 4 ANT_BEAM_8 = 4
} PHY_ANT_BEAM_WIDTH; } PHY_ANT_BEAM_WIDTH;
typedef enum { typedef enum
{
PHY_TOR_SUSPEND = 0, PHY_TOR_SUSPEND = 0,
PHY_TOR_TX = 1, PHY_TOR_TX = 1,
PHY_TOR_RX = 2 PHY_TOR_RX = 2
} PHY_TOR_TYPE; } PHY_TOR_TYPE;
typedef enum { typedef enum
{
PHY_MODE_NONE = 0, PHY_MODE_NONE = 0,
PHY_MODE_LISTEN = 1, PHY_MODE_LISTEN = 1,
PHY_MODE_SCHEDULE = 2, PHY_MODE_SCHEDULE = 2,
PHY_MODE_PULL = 3 PHY_MODE_PULL = 3
} PHY_MODE; } PHY_MODE;
typedef struct { typedef struct
{
uint32_t devId; uint32_t devId;
uint32_t txBramId; uint32_t txBramId;
uint32_t rxBramId; uint32_t rxBramId;
@@ -190,13 +197,15 @@ typedef struct {
uintptr_t baseAddr; uintptr_t baseAddr;
} PHY_CONFIG; } PHY_CONFIG;
typedef struct { typedef struct
{
int32_t last; int32_t last;
int32_t curr; int32_t curr;
int32_t next; int32_t next;
} PHY_RCD; } PHY_RCD;
typedef struct { typedef struct
{
PHY_CONFIG phyCofig; // Device address configs PHY_CONFIG phyCofig; // Device address configs
uintptr_t recvDataAddr; uintptr_t recvDataAddr;
uintptr_t sendDataAddr; uintptr_t sendDataAddr;
@@ -93,7 +93,6 @@
#define POLLTIME_H_WIDTH 32 #define POLLTIME_H_WIDTH 32
#define POLLTIME_H_MASK 0xFFFFFFFFU #define POLLTIME_H_MASK 0xFFFFFFFFU
/** /**
* Register: TIMEOUT_L * Register: TIMEOUT_L
*/ */
@@ -176,7 +175,6 @@
#define TXRX_STATE_CRC_OK 0x01000000U #define TXRX_STATE_CRC_OK 0x01000000U
#define TXRX_STATE_CRC_ERROR 0x02000000U #define TXRX_STATE_CRC_ERROR 0x02000000U
/** /**
* Register: RECVDATA_STATE * Register: RECVDATA_STATE
*/ */
+30 -22
View File
@@ -40,7 +40,6 @@
#define TMC_Read(instPtr, addr) IO_In32(instPtr->config.baseAddr + addr) #define TMC_Read(instPtr, addr) IO_In32(instPtr->config.baseAddr + addr)
#define TMC_Write(instPtr, addr, value) IO_Out32(instPtr->config.baseAddr + addr, value) #define TMC_Write(instPtr, addr, value) IO_Out32(instPtr->config.baseAddr + addr, value)
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Initialize Time-Controller Process * @brief Initialize Time-Controller Process
@@ -52,7 +51,8 @@
* FAILURE Initialization failed * FAILURE Initialization failed
* *
******************************************************************************/ ******************************************************************************/
int32_t TimeCtrl_Init(TIMECTRL_INST **instPtr, TIMECTRL_CONFIG *config){ int32_t TimeCtrl_Init(TIMECTRL_INST **instPtr, TIMECTRL_CONFIG *config)
{
ASSERT(config != NULL); ASSERT(config != NULL);
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
(*instPtr) = malloc(sizeof(TIMECTRL_INST)); (*instPtr) = malloc(sizeof(TIMECTRL_INST));
@@ -61,11 +61,13 @@ int32_t TimeCtrl_Init(TIMECTRL_INST **instPtr, TIMECTRL_CONFIG *config){
return SUCCESS; return SUCCESS;
} }
void TimeCtrl_Destroy(TIMECTRL_INST *instPtr) { void TimeCtrl_Destroy(TIMECTRL_INST *instPtr)
{
free(instPtr); free(instPtr);
} }
uint32_t TimeCtrl_GetVersion(TIMECTRL_INST *instPtr){ uint32_t TimeCtrl_GetVersion(TIMECTRL_INST *instPtr)
{
return TMC_Read(instPtr, VERSION_REG); return TMC_Read(instPtr, VERSION_REG);
} }
/*****************************************************************************/ /*****************************************************************************/
@@ -78,12 +80,12 @@ uint32_t TimeCtrl_GetVersion(TIMECTRL_INST *instPtr){
* FAILURE Initialization failed * FAILURE Initialization failed
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_Reset(TIMECTRL_INST *instPtr){ void TimeCtrl_Reset(TIMECTRL_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
TMC_Write(instPtr, CNTSTEP_OPT, TM_RESET_CODE); TMC_Write(instPtr, CNTSTEP_OPT, TM_RESET_CODE);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Assert time controller reset high * @brief Assert time controller reset high
@@ -93,12 +95,12 @@ void TimeCtrl_Reset(TIMECTRL_INST *instPtr){
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_ResetHigh(TIMECTRL_INST *instPtr){ void TimeCtrl_ResetHigh(TIMECTRL_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
// TODO: // TODO:
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Assert time controller reset low * @brief Assert time controller reset low
@@ -108,12 +110,12 @@ void TimeCtrl_ResetHigh(TIMECTRL_INST *instPtr){
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_ResetLow(TIMECTRL_INST *instPtr){ void TimeCtrl_ResetLow(TIMECTRL_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
// TODO: // TODO:
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Get current time integer value. * @brief Get current time integer value.
@@ -123,7 +125,8 @@ void TimeCtrl_ResetLow(TIMECTRL_INST *instPtr){
* @return integer value * @return integer value
* *
******************************************************************************/ ******************************************************************************/
inline TIME_INT TimeCtrl_GetIntCnt(TIMECTRL_INST *instPtr){ inline TIME_INT TimeCtrl_GetIntCnt(TIMECTRL_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return TMC_Read(instPtr, TIME_INT_REG); return TMC_Read(instPtr, TIME_INT_REG);
} }
@@ -137,7 +140,8 @@ inline TIME_INT TimeCtrl_GetIntCnt(TIMECTRL_INST *instPtr){
* @return fractional value * @return fractional value
* *
******************************************************************************/ ******************************************************************************/
inline TIME_FRAC TimeCtrl_GetFracCnt(TIMECTRL_INST *instPtr){ inline TIME_FRAC TimeCtrl_GetFracCnt(TIMECTRL_INST *instPtr)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
return TMC_Read(instPtr, TIME_FRAC_REG); return TMC_Read(instPtr, TIME_FRAC_REG);
} }
@@ -152,7 +156,8 @@ inline TIME_FRAC TimeCtrl_GetFracCnt(TIMECTRL_INST *instPtr){
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_GetCurrentTime(TIMECTRL_INST *instPtr, TIME_WORD* tmWord){ void TimeCtrl_GetCurrentTime(TIMECTRL_INST *instPtr, TIME_WORD *tmWord)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(tmWord != NULL); ASSERT(tmWord != NULL);
tmWord->frac_part = TMC_Read(instPtr, TIME_FRAC_REG); tmWord->frac_part = TMC_Read(instPtr, TIME_FRAC_REG);
@@ -169,13 +174,13 @@ void TimeCtrl_GetCurrentTime(TIMECTRL_INST *instPtr, TIME_WORD* tmWord){
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_GetCurrentStep(TIMECTRL_INST *instPtr, TIME_FRAC* tmFrac) { void TimeCtrl_GetCurrentStep(TIMECTRL_INST *instPtr, TIME_FRAC *tmFrac)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(tmFrac != NULL); ASSERT(tmFrac != NULL);
*tmFrac = TMC_Read(instPtr, CNTSTEP_OPT); *tmFrac = TMC_Read(instPtr, CNTSTEP_OPT);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Set a new optimal adjustment value to the counter step. * @brief Set a new optimal adjustment value to the counter step.
@@ -187,15 +192,17 @@ void TimeCtrl_GetCurrentStep(TIMECTRL_INST *instPtr, TIME_FRAC* tmFrac) {
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_TimeAdjust(TIMECTRL_INST *instPtr, const TIME_WORD* tmWord, TIMECTRL_ADJ_TYPE type){ void TimeCtrl_TimeAdjust(TIMECTRL_INST *instPtr, const TIME_WORD *tmWord, TIMECTRL_ADJ_TYPE type)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(tmWord != NULL); ASSERT(tmWord != NULL);
if(type < TM_ADJ_ADD || TM_ADJ_ADD > TM_ADJ_SET){ if (type < TM_ADJ_ADD || TM_ADJ_ADD > TM_ADJ_SET)
{
DBG_ERROR("Unsupported adjustment type: %d", type); DBG_ERROR("Unsupported adjustment type: %d", type);
return; return;
} }
if(((int32_t)tmWord->frac_part) > TIME_FRAC_MAX if (((int32_t)tmWord->frac_part) > TIME_FRAC_MAX || ((int32_t)tmWord->frac_part) < -TIME_FRAC_MAX)
|| ((int32_t)tmWord->frac_part) < -TIME_FRAC_MAX) { {
DBG_WARNING("Invalid fractional value %d (should between %d~%d)\n", DBG_WARNING("Invalid fractional value %d (should between %d~%d)\n",
tmWord->frac_part, -TIME_FRAC_MAX, TIME_FRAC_MAX); tmWord->frac_part, -TIME_FRAC_MAX, TIME_FRAC_MAX);
return; return;
@@ -216,13 +223,13 @@ void TimeCtrl_TimeAdjust(TIMECTRL_INST *instPtr, const TIME_WORD* tmWord, TIMECT
* @return NONE. * @return NONE.
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_SetOptimizeStep(TIMECTRL_INST* instPtr, TIME_FRAC* tmFrac) { void TimeCtrl_SetOptimizeStep(TIMECTRL_INST *instPtr, TIME_FRAC *tmFrac)
{
ASSERT(instPtr != NULL); ASSERT(instPtr != NULL);
ASSERT(tmFrac != NULL); ASSERT(tmFrac != NULL);
TMC_Write(instPtr, CNTSTEP_OPT, *tmFrac); TMC_Write(instPtr, CNTSTEP_OPT, *tmFrac);
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief Calculate b - a. * @brief Calculate b - a.
@@ -234,7 +241,8 @@ void TimeCtrl_SetOptimizeStep(TIMECTRL_INST* instPtr, TIME_FRAC* tmFrac) {
* @return NONE * @return NONE
* *
******************************************************************************/ ******************************************************************************/
void TimeCtrl_CalculateTimeGap(const TIME_WORD *a, const TIME_WORD *b, TIME_WORD *gap){ void TimeCtrl_CalculateTimeGap(const TIME_WORD *a, const TIME_WORD *b, TIME_WORD *gap)
{
ASSERT(a != NULL); ASSERT(a != NULL);
ASSERT(b != NULL); ASSERT(b != NULL);
ASSERT(gap != NULL); ASSERT(gap != NULL);
+8 -5
View File
@@ -23,7 +23,8 @@
#define TIME_FRAC_MAX 500000 // how many 10ns in a 5ms #define TIME_FRAC_MAX 500000 // how many 10ns in a 5ms
/***************** Macros (Inline Functions) Definitions *********************/ /***************** Macros (Inline Functions) Definitions *********************/
/**************************** Type Definitions *******************************/ /**************************** Type Definitions *******************************/
typedef enum { typedef enum
{
TM_ADJ_NONE = 0, TM_ADJ_NONE = 0,
TM_ADJ_ADD = 1, TM_ADJ_ADD = 1,
TM_ADJ_SET = 2 TM_ADJ_SET = 2
@@ -32,19 +33,22 @@ typedef enum {
typedef uint32_t TIME_FRAC; typedef uint32_t TIME_FRAC;
typedef uint32_t TIME_INT; typedef uint32_t TIME_INT;
typedef struct { typedef struct
{
/* 5ms each LSB */ /* 5ms each LSB */
TIME_INT int_part; TIME_INT int_part;
/* 10ns each LSB */ /* 10ns each LSB */
TIME_FRAC frac_part; TIME_FRAC frac_part;
} TIME_WORD; } TIME_WORD;
typedef struct { typedef struct
{
uintptr_t baseAddr; uintptr_t baseAddr;
uint32_t reset; // time for reset (unit: us) uint32_t reset; // time for reset (unit: us)
} TIMECTRL_CONFIG; } TIMECTRL_CONFIG;
typedef struct { typedef struct
{
TIMECTRL_CONFIG config; TIMECTRL_CONFIG config;
uint32_t modelVer; uint32_t modelVer;
} TIMECTRL_INST; } TIMECTRL_INST;
@@ -67,5 +71,4 @@ void TimeCtrl_SetOptimizeStep(TIMECTRL_INST *instPtr, TIME_FRAC* tmFrac);
void TimeCtrl_TimeAdjust(TIMECTRL_INST *instPtr, const TIME_WORD *tmWord, TIMECTRL_ADJ_TYPE type); void TimeCtrl_TimeAdjust(TIMECTRL_INST *instPtr, const TIME_WORD *tmWord, TIMECTRL_ADJ_TYPE type);
void TimeCtrl_CalculateTimeGap(const TIME_WORD *a, const TIME_WORD *b, TIME_WORD *gap); void TimeCtrl_CalculateTimeGap(const TIME_WORD *a, const TIME_WORD *b, TIME_WORD *gap);
#endif /* TIME_SYNC_H_ */ #endif /* TIME_SYNC_H_ */
+48 -26
View File
@@ -22,7 +22,8 @@
#ifdef __CRC_LSB #ifdef __CRC_LSB
// CRC-16-CCITT G(X)=X16+X12+X5+1 // CRC-16-CCITT G(X)=X16+X12+X5+1
// Reversed // Reversed
static const uint16_t crc16_1021_lsb_lut[256] = { static const uint16_t crc16_1021_lsb_lut[256] =
{
0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF, 0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7, 0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E, 0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
@@ -58,7 +59,8 @@ static const uint16_t crc16_1021_lsb_lut[256] = {
}; };
// CRC-16-MAXIM G(X)=X16+X15+X2+1 // CRC-16-MAXIM G(X)=X16+X15+X2+1
const uint16_t crc16_8005_lsb_lut[256] = { const uint16_t crc16_8005_lsb_lut[256] =
{
0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241, 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440, 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40, 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
@@ -94,7 +96,8 @@ const uint16_t crc16_8005_lsb_lut[256] = {
}; };
// CRC-8-MAXIM G(X)=X8+X5+X4+1 // CRC-8-MAXIM G(X)=X8+X5+X4+1
const uint8_t crc8_31_lsb_lut[256] = { const uint8_t crc8_31_lsb_lut[256] =
{
0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83, 0x00, 0x5E, 0xBC, 0xE2, 0x61, 0x3F, 0xDD, 0x83,
0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41, 0xC2, 0x9C, 0x7E, 0x20, 0xA3, 0xFD, 0x1F, 0x41,
0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E, 0x9D, 0xC3, 0x21, 0x7F, 0xFC, 0xA2, 0x40, 0x1E,
@@ -130,7 +133,8 @@ const uint8_t crc8_31_lsb_lut[256] = {
}; };
#else #else
// CRC-16-CCITT G(X)=X16+X12+X5+1 // CRC-16-CCITT G(X)=X16+X12+X5+1
static const uint16_t crc16_1021_msb_lut[256] = { static const uint16_t crc16_1021_msb_lut[256] =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
@@ -166,7 +170,8 @@ static const uint16_t crc16_1021_msb_lut[256] = {
}; };
// CRC-16-MAXIM G(X)=X16+X15+X2+1 // CRC-16-MAXIM G(X)=X16+X15+X2+1
const uint16_t crc16_8005_msb_lut[256] = { const uint16_t crc16_8005_msb_lut[256] =
{
0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011, 0x0000, 0x8005, 0x800F, 0x000A, 0x801B, 0x001E, 0x0014, 0x8011,
0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022, 0x8033, 0x0036, 0x003C, 0x8039, 0x0028, 0x802D, 0x8027, 0x0022,
0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072, 0x8063, 0x0066, 0x006C, 0x8069, 0x0078, 0x807D, 0x8077, 0x0072,
@@ -202,7 +207,8 @@ const uint16_t crc16_8005_msb_lut[256] = {
}; };
// CRC-8-MAXIM G(X)=X8+X5+X4+1 // CRC-8-MAXIM G(X)=X8+X5+X4+1
const uint8_t crc8_31_msb_lut[256] = { const uint8_t crc8_31_msb_lut[256] =
{
0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97, 0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E, 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4, 0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4,
@@ -268,11 +274,13 @@ for(int i = 0; i < sizeof(dataBuff); i++)
* FAILURE * FAILURE
* *
******************************************************************************/ ******************************************************************************/
int crc8_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint8_t* crcVal) { int crc8_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint8_t *crcVal)
{
uint8_t crc = CRC8_INIT; uint8_t crc = CRC8_INIT;
const uint8_t *crc_lut = NULL; const uint8_t *crc_lut = NULL;
switch(poly){ switch (poly)
{
#ifdef __CRC_LSB #ifdef __CRC_LSB
case 0x31: case 0x31:
crc_lut = crc8_31_lsb_lut; crc_lut = crc8_31_lsb_lut;
@@ -288,14 +296,14 @@ int crc8_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint8_t* crcVal)
#endif #endif
} }
for(int i = 0; i < dataLen; i++){ for (int i = 0; i < dataLen; i++)
{
crc = crc_lut[(uint8_t)(crc ^ buffPtr[i])]; crc = crc_lut[(uint8_t)(crc ^ buffPtr[i])];
} }
*crcVal = crc; *crcVal = crc;
return SUCCESS; return SUCCESS;
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief CRC16 generator * @brief CRC16 generator
@@ -309,10 +317,12 @@ int crc8_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint8_t* crcVal)
* FAILURE * FAILURE
* *
******************************************************************************/ ******************************************************************************/
int crc16_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint16_t* crcVal) { int crc16_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint16_t *crcVal)
{
uint16_t crc = CRC16_INIT; uint16_t crc = CRC16_INIT;
const uint16_t *crc_lut = NULL; const uint16_t *crc_lut = NULL;
switch(poly){ switch (poly)
{
#ifdef __CRC_LSB #ifdef __CRC_LSB
case 0x8005: case 0x8005:
crc_lut = crc16_8005_lsb_lut; crc_lut = crc16_8005_lsb_lut;
@@ -333,7 +343,8 @@ int crc16_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint16_t* crcVa
return FAILURE; return FAILURE;
#endif #endif
} }
for(int i = 0; i < dataLen; i++){ for (int i = 0; i < dataLen; i++)
{
#ifdef __CRC_LSB #ifdef __CRC_LSB
crc = (crc >> 8) ^ crc_lut[(uint8_t)(crc ^ buffPtr[i])]; crc = (crc >> 8) ^ crc_lut[(uint8_t)(crc ^ buffPtr[i])];
#else #else
@@ -357,29 +368,34 @@ int crc16_gen(uint8_t* buffPtr, uint32_t dataLen, uint16_t poly, uint16_t* crcVa
* false * false
* *
******************************************************************************/ ******************************************************************************/
bool crc8_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly) { bool crc8_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly)
{
int Status = SUCCESS; int Status = SUCCESS;
uint8_t crc_comp; uint8_t crc_comp;
uint8_t crc; uint8_t crc;
if (dataLen == 0) { if (dataLen == 0)
{
return false; return false;
} }
crc_comp = (uint8_t)(*(buffPtr + crcOffset)); crc_comp = (uint8_t)(*(buffPtr + crcOffset));
Status = crc8_gen(buffPtr, dataLen, poly, &crc); Status = crc8_gen(buffPtr, dataLen, poly, &crc);
if(Status == SUCCESS){ if (Status == SUCCESS)
if (crc == crc_comp) { {
if (crc == crc_comp)
{
return true; return true;
} }
else { else
{
return false; return false;
} }
} }
else{ else
{
return false; return false;
} }
} }
/*****************************************************************************/ /*****************************************************************************/
/** /**
* @brief CRC16 compare cheker * @brief CRC16 compare cheker
@@ -393,25 +409,31 @@ bool crc8_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t
* fasle * fasle
* *
******************************************************************************/ ******************************************************************************/
bool crc16_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly) { bool crc16_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly)
{
int Status = SUCCESS; int Status = SUCCESS;
uint16_t crc_comp; uint16_t crc_comp;
uint16_t crc; uint16_t crc;
if (dataLen == 0) { if (dataLen == 0)
{
return false; return false;
} }
crc_comp = (uint16_t)(*(buffPtr + crcOffset)); crc_comp = (uint16_t)(*(buffPtr + crcOffset));
crc_comp |= (uint16_t)((*(buffPtr + crcOffset + 1)) << 8); crc_comp |= (uint16_t)((*(buffPtr + crcOffset + 1)) << 8);
Status = crc16_gen(buffPtr, dataLen, poly, &crc); Status = crc16_gen(buffPtr, dataLen, poly, &crc);
if(Status == SUCCESS){ if (Status == SUCCESS)
if (crc == crc_comp) { {
if (crc == crc_comp)
{
return true; return true;
} }
else { else
{
return false; return false;
} }
} }
else{ else
{
return false; return false;
} }
} }
+83 -43
View File
@@ -20,7 +20,8 @@
*/ */
list_node_t * list_node_t *
list_node_new(void *val) { list_node_new(void *val)
{
list_node_t *self; list_node_t *self;
if (!(self = LIST_MALLOC(sizeof(list_node_t)))) if (!(self = LIST_MALLOC(sizeof(list_node_t))))
return NULL; return NULL;
@@ -35,7 +36,8 @@ list_node_new(void *val) {
*/ */
list_t * list_t *
list_new(void) { list_new(void)
{
list_t *self; list_t *self;
if (!(self = LIST_MALLOC(sizeof(list_t)))) if (!(self = LIST_MALLOC(sizeof(list_t))))
return NULL; return NULL;
@@ -52,15 +54,17 @@ list_new(void) {
* @self: Pointer to the list * @self: Pointer to the list
*/ */
void void list_clear(list_t *self)
list_clear(list_t *self) { {
uint32_t len = self->len; uint32_t len = self->len;
list_node_t *next; list_node_t *next;
list_node_t *curr = self->head; list_node_t *curr = self->head;
while (len) { while (len)
{
next = curr->next; next = curr->next;
if (self->free) self->free(curr->val); if (self->free)
self->free(curr->val);
LIST_FREE(curr); LIST_FREE(curr);
curr = next; curr = next;
len--; len--;
@@ -72,8 +76,8 @@ list_clear(list_t *self) {
* @self: Pointer to the list * @self: Pointer to the list
*/ */
void void list_destroy(list_t *self)
list_destroy(list_t *self) { {
list_clear(self); list_clear(self);
LIST_FREE(self); LIST_FREE(self);
} }
@@ -86,15 +90,20 @@ list_destroy(list_t *self) {
*/ */
list_node_t * list_node_t *
list_rpush(list_t *self, list_node_t *node) { list_rpush(list_t *self, list_node_t *node)
if (!node) return NULL; {
if (!node)
return NULL;
if (self->len) { if (self->len)
{
node->prev = self->tail; node->prev = self->tail;
node->next = NULL; node->next = NULL;
self->tail->next = node; self->tail->next = node;
self->tail = node; self->tail = node;
} else { }
else
{
self->head = self->tail = node; self->head = self->tail = node;
node->prev = node->next = NULL; node->prev = node->next = NULL;
} }
@@ -109,14 +118,19 @@ list_rpush(list_t *self, list_node_t *node) {
*/ */
list_node_t * list_node_t *
list_rpop(list_t *self) { list_rpop(list_t *self)
if (!self->len) return NULL; {
if (!self->len)
return NULL;
list_node_t *node = self->tail; list_node_t *node = self->tail;
if (--self->len) { if (--self->len)
{
(self->tail = node->prev)->next = NULL; (self->tail = node->prev)->next = NULL;
} else { }
else
{
self->tail = self->head = NULL; self->tail = self->head = NULL;
} }
@@ -130,14 +144,19 @@ list_rpop(list_t *self) {
*/ */
list_node_t * list_node_t *
list_lpop(list_t *self) { list_lpop(list_t *self)
if (!self->len) return NULL; {
if (!self->len)
return NULL;
list_node_t *node = self->head; list_node_t *node = self->head;
if (--self->len) { if (--self->len)
{
(self->head = node->next)->prev = NULL; (self->head = node->next)->prev = NULL;
} else { }
else
{
self->head = self->tail = NULL; self->head = self->tail = NULL;
} }
@@ -153,15 +172,20 @@ list_lpop(list_t *self) {
*/ */
list_node_t * list_node_t *
list_lpush(list_t *self, list_node_t *node) { list_lpush(list_t *self, list_node_t *node)
if (!node) return NULL; {
if (!node)
return NULL;
if (self->len) { if (self->len)
{
node->next = self->head; node->next = self->head;
node->prev = NULL; node->prev = NULL;
self->head->prev = node; self->head->prev = node;
self->head = node; self->head = node;
} else { }
else
{
self->head = self->tail = node; self->head = self->tail = node;
node->prev = node->next = NULL; node->prev = node->next = NULL;
} }
@@ -177,18 +201,25 @@ list_lpush(list_t *self, list_node_t *node) {
*/ */
list_node_t * list_node_t *
list_find(list_t *self, void *val) { list_find(list_t *self, void *val)
{
list_iterator_t *it = list_iterator_new(self, LIST_HEAD); list_iterator_t *it = list_iterator_new(self, LIST_HEAD);
list_node_t *node; list_node_t *node;
while ((node = list_iterator_next(it))) { while ((node = list_iterator_next(it)))
if (self->compare) { {
if (self->compare(val, node->val) == 0) { if (self->compare)
{
if (self->compare(val, node->val) == 0)
{
list_iterator_destroy(it); list_iterator_destroy(it);
return node; return node;
} }
} else { }
if (val == node->val) { else
{
if (val == node->val)
{
list_iterator_destroy(it); list_iterator_destroy(it);
return node; return node;
} }
@@ -206,18 +237,22 @@ list_find(list_t *self, void *val) {
*/ */
list_node_t * list_node_t *
list_at(list_t *self, int index) { list_at(list_t *self, int index)
{
list_direction_t direction = LIST_HEAD; list_direction_t direction = LIST_HEAD;
if (index < 0) { if (index < 0)
{
direction = LIST_TAIL; direction = LIST_TAIL;
index = ~index; index = ~index;
} }
if ((unsigned)index < self->len) { if ((unsigned)index < self->len)
{
list_iterator_t *it = list_iterator_new(self, direction); list_iterator_t *it = list_iterator_new(self, direction);
list_node_t *node = list_iterator_next(it); list_node_t *node = list_iterator_next(it);
while (index--) node = list_iterator_next(it); while (index--)
node = list_iterator_next(it);
list_iterator_destroy(it); list_iterator_destroy(it);
return node; return node;
} }
@@ -231,8 +266,8 @@ list_at(list_t *self, int index) {
* @node: Pointer the node to be deleted * @node: Pointer the node to be deleted
*/ */
void void list_remove(list_t *self, list_node_t *node)
list_remove(list_t *self, list_node_t *node) { {
node->prev node->prev
? (node->prev->next = node->next) ? (node->prev->next = node->next)
: (self->head = node->next); : (self->head = node->next);
@@ -241,7 +276,8 @@ list_remove(list_t *self, list_node_t *node) {
? (node->next->prev = node->prev) ? (node->next->prev = node->prev)
: (self->tail = node->prev); : (self->tail = node->prev);
if (self->free) self->free(node->val); if (self->free)
self->free(node->val);
LIST_FREE(node); LIST_FREE(node);
--self->len; --self->len;
@@ -253,7 +289,8 @@ list_remove(list_t *self, list_node_t *node) {
*/ */
list_iterator_t * list_iterator_t *
list_iterator_new(list_t *list, list_direction_t direction) { list_iterator_new(list_t *list, list_direction_t direction)
{
list_node_t *node = direction == LIST_HEAD list_node_t *node = direction == LIST_HEAD
? list->head ? list->head
: list->tail; : list->tail;
@@ -266,7 +303,8 @@ list_iterator_new(list_t *list, list_direction_t direction) {
*/ */
list_iterator_t * list_iterator_t *
list_iterator_new_from_node(list_node_t *node, list_direction_t direction) { list_iterator_new_from_node(list_node_t *node, list_direction_t direction)
{
list_iterator_t *self; list_iterator_t *self;
if (!(self = LIST_MALLOC(sizeof(list_iterator_t)))) if (!(self = LIST_MALLOC(sizeof(list_iterator_t))))
return NULL; return NULL;
@@ -281,9 +319,11 @@ list_iterator_new_from_node(list_node_t *node, list_direction_t direction) {
*/ */
list_node_t * list_node_t *
list_iterator_next(list_iterator_t *self) { list_iterator_next(list_iterator_t *self)
{
list_node_t *curr = self->next; list_node_t *curr = self->next;
if (curr) { if (curr)
{
self->next = self->direction == LIST_HEAD self->next = self->direction == LIST_HEAD
? curr->next ? curr->next
: curr->prev; : curr->prev;
@@ -295,8 +335,8 @@ list_iterator_next(list_iterator_t *self) {
* Free the list iterator. * Free the list iterator.
*/ */
void void list_iterator_destroy(list_iterator_t *self)
list_iterator_destroy(list_iterator_t *self) { {
LIST_FREE(self); LIST_FREE(self);
self = NULL; self = NULL;
} }
+10 -5
View File
@@ -9,7 +9,8 @@
#define __LIST_H__ #define __LIST_H__
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
#include <stdlib.h> #include <stdlib.h>
@@ -19,7 +20,8 @@ extern "C" {
* list_t iterator direction. * list_t iterator direction.
*/ */
typedef enum { typedef enum
{
LIST_HEAD, LIST_HEAD,
LIST_TAIL LIST_TAIL
} list_direction_t; } list_direction_t;
@@ -28,7 +30,8 @@ typedef enum {
* list_t node struct. * list_t node struct.
*/ */
typedef struct list_node { typedef struct list_node
{
struct list_node *prev; struct list_node *prev;
struct list_node *next; struct list_node *next;
void *val; void *val;
@@ -38,7 +41,8 @@ typedef struct list_node {
* list_t struct. * list_t struct.
*/ */
typedef struct { typedef struct
{
list_node_t *head; list_node_t *head;
list_node_t *tail; list_node_t *tail;
uint32_t len; uint32_t len;
@@ -50,7 +54,8 @@ typedef struct {
* list_t iterator struct. * list_t iterator struct.
*/ */
typedef struct { typedef struct
{
list_node_t *next; list_node_t *next;
list_direction_t direction; list_direction_t direction;
} list_iterator_t; } list_iterator_t;
+36 -19
View File
@@ -11,7 +11,8 @@
/************************** Functions Implementation **************************/ /************************** Functions Implementation **************************/
/******************************************************************************/ /******************************************************************************/
#ifdef __BITSWAP_TABLE #ifdef __BITSWAP_TABLE
static uint8_t byte_flipLR[256] = { static uint8_t byte_flipLR[256] =
{
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0,
0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8, 0x08, 0x88, 0x48, 0xC8, 0x28, 0xA8, 0x68, 0xE8,
@@ -47,16 +48,19 @@ static uint8_t byte_flipLR[256] = {
}; };
#endif #endif
inline void IO_Out32(uintptr_t addr, uint32_t value){ inline void IO_Out32(uintptr_t addr, uint32_t value)
{
volatile uint32_t *localAddr = (volatile uint32_t *)addr; volatile uint32_t *localAddr = (volatile uint32_t *)addr;
*localAddr = value; *localAddr = value;
} }
inline uint32_t IO_In32(uintptr_t addr){ inline uint32_t IO_In32(uintptr_t addr)
{
return *(volatile uint32_t *)addr; return *(volatile uint32_t *)addr;
} }
uint8_t byte_bit_swap(uint8_t byte){ uint8_t byte_bit_swap(uint8_t byte)
{
#ifdef __BITSWAP_TABLE #ifdef __BITSWAP_TABLE
return byte_flipLR[byte]; return byte_flipLR[byte];
#else #else
@@ -64,7 +68,8 @@ uint8_t byte_bit_swap(uint8_t byte){
#endif #endif
} }
uint32_t word_bit_swap(uint32_t word){ uint32_t word_bit_swap(uint32_t word)
{
#ifdef __BITSWAP_TABLE #ifdef __BITSWAP_TABLE
uint32_t rv_word = (byte_flipLR[word & 0xff] << 24) | uint32_t rv_word = (byte_flipLR[word & 0xff] << 24) |
(byte_flipLR[(word >> 8) & 0xff] << 16) | (byte_flipLR[(word >> 8) & 0xff] << 16) |
@@ -80,12 +85,14 @@ uint32_t word_bit_swap(uint32_t word){
#endif #endif
} }
uint32_t count_ones(uint32_t word){ uint32_t count_ones(uint32_t word)
{
#ifdef __GNUC__ #ifdef __GNUC__
return __builtin_popcount(word); return __builtin_popcount(word);
#else #else
uint32_t one_count = 0; uint32_t one_count = 0;
while (word) { while (word)
{
if (word & 0x00000001) if (word & 0x00000001)
one_count++; one_count++;
word >>= 1; word >>= 1;
@@ -94,7 +101,8 @@ uint32_t count_ones(uint32_t word){
#endif #endif
} }
double fix2double(int32_t val, uint8_t frac_bits){ double fix2double(int32_t val, uint8_t frac_bits)
{
return (((double)val) / (BIT(frac_bits))); return (((double)val) / (BIT(frac_bits)));
} }
@@ -104,16 +112,19 @@ double fix2double(int32_t val, uint8_t frac_bits){
uint32_t find_first_set_bit(uint32_t word) uint32_t find_first_set_bit(uint32_t word)
{ {
#ifdef __GNUC__ #ifdef __GNUC__
if(word != 0){ if (word != 0)
{
return __builtin_ctz(word); return __builtin_ctz(word);
} }
else{ else
{
return 32; return 32;
} }
#else #else
uint32_t first_set_bit = 0; uint32_t first_set_bit = 0;
while (word) { while (word)
{
if (word & 0x1) if (word & 0x1)
return first_set_bit; return first_set_bit;
word >>= 1; word >>= 1;
@@ -130,17 +141,20 @@ uint32_t find_first_set_bit(uint32_t word)
uint32_t find_last_set_bit(uint32_t word) uint32_t find_last_set_bit(uint32_t word)
{ {
#ifdef __GNUC__ #ifdef __GNUC__
if(word != 0){ if (word != 0)
{
return (31 - __builtin_clz(word)); return (31 - __builtin_clz(word));
} }
else{ else
{
return 32; return 32;
} }
#else #else
uint32_t bit = 0; uint32_t bit = 0;
uint32_t last_set_bit = 32; uint32_t last_set_bit = 32;
while (word) { while (word)
{
if (word & 0x1) if (word & 0x1)
last_set_bit = bit; last_set_bit = bit;
word >>= 1; word >>= 1;
@@ -162,8 +176,10 @@ uint32_t find_closest(int32_t val,
uint32_t ret = 0; uint32_t ret = 0;
uint32_t i; uint32_t i;
for (i = 1; i < size; i++) { for (i = 1; i < size; i++)
if (abs(array[i] - val) < diff) { {
if (abs(array[i] - val) < diff)
{
diff = abs(array[i] - val); diff = abs(array[i] - val);
ret = i; ret = i;
} }
@@ -233,7 +249,8 @@ void rational_best_approximation(uint32_t given_numerator,
*best_denominator = given_denominator / gcd; *best_denominator = given_denominator / gcd;
if ((*best_numerator > max_numerator) || if ((*best_numerator > max_numerator) ||
(*best_denominator > max_denominator)) { (*best_denominator > max_denominator))
{
*best_numerator = 0; *best_numerator = 0;
*best_denominator = 0; *best_denominator = 0;
} }
@@ -246,7 +263,8 @@ uint32_t hweight8(uint32_t word)
{ {
uint32_t count = 0; uint32_t count = 0;
while (word) { while (word)
{
if (word & 0x1) if (word & 0x1)
count++; count++;
word >>= 1; word >>= 1;
@@ -348,4 +366,3 @@ uint32_t srt_to_uint32(const char *str)
else else
return value; return value;
} }
+7 -3
View File
@@ -1,4 +1,5 @@
/***************************************************************************//** /***************************************************************************
//**
* @file util.h * @file util.h
* @brief Implementation of utility functions. * @brief Implementation of utility functions.
* @author DBogdan (dragos.bogdan@analog.com) * @author DBogdan (dragos.bogdan@analog.com)
@@ -82,7 +83,11 @@
(type) clamp((type)(val), (type)(min_val), (type)(max_val)) (type) clamp((type)(val), (type)(min_val), (type)(max_val))
#define swap(x, y) \ #define swap(x, y) \
{typeof(x) _tmp_ = (x); (x) = (y); (y) = _tmp_;} { \
typeof(x) _tmp_ = (x); \
(x) = (y); \
(y) = _tmp_; \
}
#define round_up(x, y) \ #define round_up(x, y) \
(((x) + (y)-1) / (y)) (((x) + (y)-1) / (y))
@@ -189,4 +194,3 @@ bool crc16_check(uint8_t* buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_
int crc8_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint8_t *crcVal); int crc8_gen(uint8_t *buffPtr, uint32_t dataLen, uint16_t poly, uint8_t *crcVal);
bool crc8_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly); bool crc8_check(uint8_t *buffPtr, uint32_t dataLen, uint32_t crcOffset, uint16_t poly);
#endif // __UTIL_H_ #endif // __UTIL_H_
+33 -24
View File
@@ -7,30 +7,32 @@
#define HARDWARE_VERSION 0x00000001 #define HARDWARE_VERSION 0x00000001
#define MAX_OPT_DELAY 100 // 1us = 100*10ns #define MAX_OPT_DELAY 100 // 1us = 100*10ns
TIMECTRL_CONFIG tmcConfig = { TIMECTRL_CONFIG tmcConfig = {
.baseAddr = 0 /*TODO: fill withe real base address*/, .baseAddr = 0 /*TODO: fill withe real base address*/,
.reset = 10 .reset = 10};
};
TIMECTRL_INST *tmcInstPtr = NULL; TIMECTRL_INST *tmcInstPtr = NULL;
void tmc_setup(void){ void tmc_setup(void)
{
int32_t Status; int32_t Status;
Status = TimeCtrl_Init(&tmcInstPtr, &tmcConfig); Status = TimeCtrl_Init(&tmcInstPtr, &tmcConfig);
ASSERT_TRUE(SUCCESS == Status); ASSERT_TRUE(SUCCESS == Status);
} }
void tmc_teardown(void){ void tmc_teardown(void)
{
TimeCtrl_Destroy(tmcInstPtr); TimeCtrl_Destroy(tmcInstPtr);
} }
TEST(tmc_version) { TEST(tmc_version)
{
ASSERT_EQUAL(HARDWARE_VERSION, TimeCtrl_GetVersion(tmcInstPtr)); ASSERT_EQUAL(HARDWARE_VERSION, TimeCtrl_GetVersion(tmcInstPtr));
return; return;
} }
TEST(tmc_read_time) { TEST(tmc_read_time)
{
TIME_WORD timeStamp; TIME_WORD timeStamp;
TIME_INT timeH; TIME_INT timeH;
TIME_FRAC timeL; TIME_FRAC timeL;
@@ -39,17 +41,20 @@ TEST(tmc_read_time) {
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStamp); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStamp);
timeH = TimeCtrl_GetIntCnt(tmcInstPtr); timeH = TimeCtrl_GetIntCnt(tmcInstPtr);
timeL = TimeCtrl_GetFracCnt(tmcInstPtr); timeL = TimeCtrl_GetFracCnt(tmcInstPtr);
if(timeL >= timeStamp.frac_part) { if (timeL >= timeStamp.frac_part)
{
ASSERT_EQUAL(timeH, timeStamp.int_part); ASSERT_EQUAL(timeH, timeStamp.int_part);
ASSERT_TRUE(timeL - timeStamp.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeL - timeStamp.frac_part < MAX_OPT_DELAY);
} }
else { else
{
ASSERT_EQUAL(timeH, timeStamp.int_part + 1); ASSERT_EQUAL(timeH, timeStamp.int_part + 1);
ASSERT_TRUE(timeL + TIME_FRAC_MAX - timeStamp.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeL + TIME_FRAC_MAX - timeStamp.frac_part < MAX_OPT_DELAY);
} }
} }
TEST(tmc_reset) { TEST(tmc_reset)
{
TIME_WORD timeStamp; TIME_WORD timeStamp;
TimeCtrl_Reset(tmcInstPtr); TimeCtrl_Reset(tmcInstPtr);
@@ -58,7 +63,8 @@ TEST(tmc_reset) {
ASSERT_TRUE(timeStamp.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeStamp.frac_part < MAX_OPT_DELAY);
} }
TEST(tmc_adjustment) { TEST(tmc_adjustment)
{
TIME_WORD timeStampPre, timeStampPost, timeAdj; TIME_WORD timeStampPre, timeStampPost, timeAdj;
// Adjustment -- set // Adjustment -- set
@@ -81,11 +87,13 @@ TEST(tmc_adjustment) {
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPre); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPre);
TimeCtrl_TimeAdjust(tmcInstPtr, &timeAdj, TM_ADJ_SET); TimeCtrl_TimeAdjust(tmcInstPtr, &timeAdj, TM_ADJ_SET);
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPost); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPost);
if(timeStampPost.frac_part >= timeStampPre.frac_part) { if (timeStampPost.frac_part >= timeStampPre.frac_part)
{
ASSERT_EQUAL(timeStampPost.int_part, timeStampPre.int_part); ASSERT_EQUAL(timeStampPost.int_part, timeStampPre.int_part);
ASSERT_TRUE(timeStampPost.frac_part - timeStampPre.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeStampPost.frac_part - timeStampPre.frac_part < MAX_OPT_DELAY);
} }
else { else
{
ASSERT_EQUAL(timeStampPost.int_part, timeStampPre.int_part + 1); ASSERT_EQUAL(timeStampPost.int_part, timeStampPre.int_part + 1);
ASSERT_TRUE(timeStampPost.frac_part + TIME_FRAC_MAX - timeStampPre.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeStampPost.frac_part + TIME_FRAC_MAX - timeStampPre.frac_part < MAX_OPT_DELAY);
} }
@@ -96,10 +104,10 @@ TEST(tmc_adjustment) {
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPre); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPre);
TimeCtrl_TimeAdjust(tmcInstPtr, &timeAdj, TM_ADJ_ADD); TimeCtrl_TimeAdjust(tmcInstPtr, &timeAdj, TM_ADJ_ADD);
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPost); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPost);
if(timeStampPre.frac_part + timeAdj.frac_part > TIME_FRAC_MAX) { if (timeStampPre.frac_part + timeAdj.frac_part > TIME_FRAC_MAX)
{
ASSERT_TRUE(timeStampPost.int_part <= timeStampPre.int_part + timeAdj.int_part + 2); ASSERT_TRUE(timeStampPost.int_part <= timeStampPre.int_part + timeAdj.int_part + 2);
ASSERT_TRUE(timeStampPost.frac_part + TIME_FRAC_MAX - timeStampPre.frac_part ASSERT_TRUE(timeStampPost.frac_part + TIME_FRAC_MAX - timeStampPre.frac_part < MAX_OPT_DELAY + timeAdj.frac_part);
< MAX_OPT_DELAY + timeAdj.frac_part);
} }
timeAdj.frac_part = -253; timeAdj.frac_part = -253;
@@ -107,19 +115,21 @@ TEST(tmc_adjustment) {
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPre); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPre);
TimeCtrl_TimeAdjust(tmcInstPtr, &timeAdj, TM_ADJ_ADD); TimeCtrl_TimeAdjust(tmcInstPtr, &timeAdj, TM_ADJ_ADD);
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPost); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStampPost);
if(timeStampPre.frac_part + timeAdj.frac_part > TIME_FRAC_MAX) { if (timeStampPre.frac_part + timeAdj.frac_part > TIME_FRAC_MAX)
{
ASSERT_TRUE(timeStampPost.int_part <= timeStampPre.int_part + timeAdj.int_part + 2); ASSERT_TRUE(timeStampPost.int_part <= timeStampPre.int_part + timeAdj.int_part + 2);
ASSERT_TRUE(timeStampPost.frac_part + TIME_FRAC_MAX - timeStampPre.frac_part ASSERT_TRUE(timeStampPost.frac_part + TIME_FRAC_MAX - timeStampPre.frac_part < MAX_OPT_DELAY + timeAdj.frac_part);
< MAX_OPT_DELAY + timeAdj.frac_part);
} }
} }
TEST(tmc_setopt) { TEST(tmc_setopt)
{
// TODO: // TODO:
return; return;
} }
TEST_SUITE(tmc_unit_test) { TEST_SUITE(tmc_unit_test)
{
TEST_SUITE_CONFIGURE(tmc_setup, tmc_teardown); TEST_SUITE_CONFIGURE(tmc_setup, tmc_teardown);
RUN_TEST(tmc_version); RUN_TEST(tmc_version);
RUN_TEST(tmc_read_time); RUN_TEST(tmc_read_time);
@@ -128,10 +138,9 @@ TEST_SUITE(tmc_unit_test) {
RUN_TEST(tmc_setopt); RUN_TEST(tmc_setopt);
} }
int main(void) { int main(void)
{
RUN_TEST_SUITE(tmc_unit_test); RUN_TEST_SUITE(tmc_unit_test);
TEST_REPORT(); TEST_REPORT();
return TEST_EXIT_CODE; return TEST_EXIT_CODE;
} }
@@ -13,12 +13,12 @@ uint32_t tmcReg[32] = {0};
TIMECTRL_CONFIG tmcConfig = { TIMECTRL_CONFIG tmcConfig = {
.baseAddr = 0, .baseAddr = 0,
.reset = 10 .reset = 10};
};
TIMECTRL_INST *tmcInstPtr = NULL; TIMECTRL_INST *tmcInstPtr = NULL;
void tmc_setup(void){ void tmc_setup(void)
{
int32_t Status; int32_t Status;
tmcReg[1] = HARDWARE_VERSION; // Version tmcReg[1] = HARDWARE_VERSION; // Version
tmcReg[2] = 253; // Int tmcReg[2] = 253; // Int
@@ -35,27 +35,32 @@ void tmc_setup(void){
ASSERT_EQUAL(SUCCESS, Status); ASSERT_EQUAL(SUCCESS, Status);
} }
void tmc_teardown(void){ void tmc_teardown(void)
{
TimeCtrl_Destroy(tmcInstPtr); TimeCtrl_Destroy(tmcInstPtr);
} }
TEST(tmc_version) { TEST(tmc_version)
{
ASSERT_EQUAL(HARDWARE_VERSION, TimeCtrl_GetVersion(tmcInstPtr)); ASSERT_EQUAL(HARDWARE_VERSION, TimeCtrl_GetVersion(tmcInstPtr));
return; return;
} }
TEST(tmc_basic_operations) { TEST(tmc_basic_operations)
{
TIME_WORD timeStamp; TIME_WORD timeStamp;
TIME_INT timeH; TIME_INT timeH;
TIME_FRAC timeL; TIME_FRAC timeL;
TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStamp); TimeCtrl_GetCurrentTime(tmcInstPtr, &timeStamp);
timeH = TimeCtrl_GetIntCnt(tmcInstPtr); timeH = TimeCtrl_GetIntCnt(tmcInstPtr);
timeL = TimeCtrl_GetFracCnt(tmcInstPtr); timeL = TimeCtrl_GetFracCnt(tmcInstPtr);
if(timeL >= timeStamp.frac_part) { if (timeL >= timeStamp.frac_part)
{
ASSERT_EQUAL(timeH, timeStamp.int_part); ASSERT_EQUAL(timeH, timeStamp.int_part);
ASSERT_TRUE(timeL - timeStamp.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeL - timeStamp.frac_part < MAX_OPT_DELAY);
} }
else { else
{
ASSERT_EQUAL(timeH, timeStamp.int_part + 1); ASSERT_EQUAL(timeH, timeStamp.int_part + 1);
ASSERT_TRUE(timeL + TIME_FRAC_MAX - timeStamp.frac_part < MAX_OPT_DELAY); ASSERT_TRUE(timeL + TIME_FRAC_MAX - timeStamp.frac_part < MAX_OPT_DELAY);
} }
@@ -69,16 +74,16 @@ TEST(tmc_basic_operations) {
return; return;
} }
TEST_SUITE(tmc_unit_test) { TEST_SUITE(tmc_unit_test)
{
TEST_SUITE_CONFIGURE(tmc_setup, tmc_teardown); TEST_SUITE_CONFIGURE(tmc_setup, tmc_teardown);
RUN_TEST(tmc_version); RUN_TEST(tmc_version);
RUN_TEST(tmc_basic_operations); RUN_TEST(tmc_basic_operations);
} }
int main(void) { int main(void)
{
RUN_TEST_SUITE(tmc_unit_test); RUN_TEST_SUITE(tmc_unit_test);
TEST_REPORT(); TEST_REPORT();
return TEST_EXIT_CODE; return TEST_EXIT_CODE;
} }
+25 -24
View File
@@ -35,7 +35,8 @@
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C"
{
#endif #endif
#include <stdbool.h> #include <stdbool.h>
@@ -56,8 +57,7 @@
#define ASSERT_ALMOST_EQUAL(first, second, precision) \ #define ASSERT_ALMOST_EQUAL(first, second, precision) \
assert_almost_equal((first), (second), (precision), __LINE__, \ assert_almost_equal((first), (second), (precision), __LINE__, \
"ASSERT_ALMOST_EQUAL(" #first ", " #second ", " \ "ASSERT_ALMOST_EQUAL(" #first ", " #second ", " #precision ")");
#precision ")");
#if defined(_WIN32) #if defined(_WIN32)
#include <Windows.h> #include <Windows.h>
@@ -149,7 +149,9 @@ static void (*unit_teardown)(void) = NULL;
#define TEST(method_name) static void method_name(void) #define TEST(method_name) static void method_name(void)
#define TEST_SUITE(suite_name) static void suite_name(void) #define TEST_SUITE(suite_name) static void suite_name(void)
#define __SAFE_BLOCK(block) do {\ #define __SAFE_BLOCK(block) \
do \
{ \
block \ block \
} while (0) } while (0)
@@ -157,22 +159,19 @@ static void (*unit_teardown)(void) = NULL;
#define RUN_TEST_SUITE(suite_name) __SAFE_BLOCK( \ #define RUN_TEST_SUITE(suite_name) __SAFE_BLOCK( \
suite_name(); \ suite_name(); \
unit_setup = NULL; \ unit_setup = NULL; \
unit_teardown = NULL;\ unit_teardown = NULL;)
)
/* Configure setup and teardown functions */ /* Configure setup and teardown functions */
#define TEST_SUITE_CONFIGURE(setup_fun, teardown_fun) __SAFE_BLOCK( \ #define TEST_SUITE_CONFIGURE(setup_fun, teardown_fun) __SAFE_BLOCK( \
unit_setup = setup_fun; \ unit_setup = setup_fun; \
unit_teardown = teardown_fun;\ unit_teardown = teardown_fun;)
)
/* Test runner */ /* Test runner */
#define RUN_TEST(test) __SAFE_BLOCK( \ #define RUN_TEST(test) __SAFE_BLOCK( \
if (unit_real_timer == 0 && unit_proc_timer == 0) { \ if (unit_real_timer == 0 && unit_proc_timer == 0) { \
unit_real_timer = timer_real(); \ unit_real_timer = timer_real(); \
unit_proc_timer = timer_cpu(); \ unit_proc_timer = timer_cpu(); \
}\ } if (unit_setup) (*unit_setup)(); \
if (unit_setup) (*unit_setup)();\
unit_status = 0; \ unit_status = 0; \
test(); \ test(); \
unit_run++; \ unit_run++; \
@@ -180,13 +179,10 @@ static void (*unit_teardown)(void) = NULL;
unit_fail++; \ unit_fail++; \
test_printf("%s\n\t--FAIL\n", #test); \ test_printf("%s\n\t--FAIL\n", #test); \
test_printf("%s\n", unit_last_message); \ test_printf("%s\n", unit_last_message); \
}\ } else { \
else {\
test_printf("%s\n\t--PASS\n", #test); \ test_printf("%s\n\t--PASS\n", #test); \
}\ }(void)fflush(stdout); \
(void)fflush(stdout);\ if (unit_teardown)(*unit_teardown)();)
if (unit_teardown) (*unit_teardown)();\
)
/* Report */ /* Report */
#define TEST_REPORT() __SAFE_BLOCK( \ #define TEST_REPORT() __SAFE_BLOCK( \
@@ -197,14 +193,15 @@ static void (*unit_teardown)(void) = NULL;
unit_end_proc_timer = timer_cpu(); \ unit_end_proc_timer = timer_cpu(); \
test_printf("\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n", \ test_printf("\nFinished in %.8f seconds (real) %.8f seconds (proc)\n\n", \
unit_end_real_timer - unit_real_timer, \ unit_end_real_timer - unit_real_timer, \
unit_end_proc_timer - unit_proc_timer);\ unit_end_proc_timer - unit_proc_timer);)
)
#define TEST_EXIT_CODE unit_fail #define TEST_EXIT_CODE unit_fail
/* Assertions */ /* Assertions */
void assert_true(bool value, int line_number, const char* assertion_text) { void assert_true(bool value, int line_number, const char *assertion_text)
{
unit_assert++; unit_assert++;
if (value) { if (value)
{
return; return;
} }
unit_status = 1; unit_status = 1;
@@ -212,9 +209,11 @@ void assert_true(bool value, int line_number, const char* assertion_text) {
test_printf("Expected true, but was false.\n"); test_printf("Expected true, but was false.\n");
} }
void assert_false(bool value, int line_number, const char* assertion_text) { void assert_false(bool value, int line_number, const char *assertion_text)
{
unit_assert++; unit_assert++;
if (!value) { if (!value)
{
return; return;
} }
unit_status = 1; unit_status = 1;
@@ -223,9 +222,11 @@ void assert_false(bool value, int line_number, const char* assertion_text) {
} }
void assert_almost_equal(double first, double second, double precision, void assert_almost_equal(double first, double second, double precision,
int line_number, const char* assertion_text) { int line_number, const char *assertion_text)
{
unit_assert++; unit_assert++;
if (fabs(first - second) <= precision) { if (fabs(first - second) <= precision)
{
return; return;
} }
unit_status = 1; unit_status = 1;