Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crc checksum #284

Draft
wants to merge 29 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a060923
made some small changes
PratyushMakkar Mar 21, 2024
a74c587
Attribution
PratyushMakkar Mar 21, 2024
cd449d1
Made some changes
PratyushMakkar Mar 25, 2024
e13429f
Added tests
PratyushMakkar Mar 28, 2024
268df5a
bl commit
PratyushMakkar Mar 30, 2024
97bced3
Generated HAL code in hal/source directories
kepler452b123 Mar 30, 2024
c443b98
Typo in launchpad HAL
kepler452b123 Mar 31, 2024
6fca382
Made changes to bootloader CRC module
PratyushMakkar Apr 13, 2024
40faa9e
Merge branch 'main' into crc_checksum
PratyushMakkar Apr 13, 2024
523be7c
Revert "Typo in launchpad HAL"
PratyushMakkar Apr 13, 2024
eb5f0d1
Revert "Generated HAL code in hal/source directories"
PratyushMakkar Apr 13, 2024
6b11dd3
Placeholder for tests
PratyushMakkar Apr 13, 2024
81ea36c
Finished bl_common
PratyushMakkar Apr 13, 2024
2fb596d
Removed cmake bl_utils
PratyushMakkar Apr 13, 2024
d69ea40
Modfied bl_crc
PratyushMakkar Apr 13, 2024
479fa2b
Modified bl_common
PratyushMakkar Apr 13, 2024
cc33388
Mistaken const
PratyushMakkar Apr 13, 2024
45c9408
Cast to uin32_t
PratyushMakkar Apr 13, 2024
c51f42c
error code
PratyushMakkar Apr 13, 2024
c0a767c
crc -> blCrc
PratyushMakkar Apr 13, 2024
af3c62d
CommitMesg
PratyushMakkar Apr 13, 2024
b2c6a6e
yeat
PratyushMakkar Apr 13, 2024
6668e6e
yeat
PratyushMakkar Apr 13, 2024
ff333af
yeat
PratyushMakkar Apr 13, 2024
41191f4
initializing static variable
PratyushMakkar Apr 13, 2024
2e65ea7
flags
PratyushMakkar Apr 13, 2024
ba4d378
Changes to crc
PratyushMakkar Apr 14, 2024
76839e2
Null check
PratyushMakkar Apr 15, 2024
9d23520
Modified shared hcg files to have CRC settings
kepler452b123 Apr 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
116 changes: 116 additions & 0 deletions obc/app/drivers/rm46/obc_crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
#include "obc_crc.h"
#include "obc_errors.h"

#include "crc.h"
#include "reg_crc.h"
#include "sys_dma.h"

#include "FreeRTOS.h"
#include "os_semphr.h"

#include <stdint.h>

#define CRC_MAX_TIMEOUT 0x00U // Setting it to 0, disables the timeout.
#define CRC_SECTOR_COUNT 0x01U
#define CRC_CHANNEL CRC_CH1
#define CRC_MODE CRC_SEMI_CPU
#define CRC_BASE crcREG

// DMA associated signals
#define CRC_DMA_PORT 0x04 // Only PORT B seems to be valid
#define CRC_DMA_FRAME_COUNT 0x01U
#define CRC_DMA_CHANNEL DMA_CH26
#define CRC_DMA_REQLINE 26U

#define CRC_QUEUE_LENGTH 1U

typedef enum { CRC_COMPLETE, CRC_FAILURE } crc_result_t;

static SemaphoreHandle_t crcMutex = NULL;
static StaticSemaphore_t crcMutexBuffer = {0};
static QueueHandle_t crcCompleteQueue = NULL;

static crcConfig_t crcConfig = {0};
static obc_error_code_t requestCrcDmaTransfer(uint32_t startAddress, uint32_t patternCount);

obc_error_code_t obcCrcInit() {
crcInit();
crcEnableNotification(CRC_BASE, CRC_CH1_CC | CRC_CH1_TO);

crcMutex = xSemaphoreCreateMutexStatic(&crcMutexBuffer);
ASSERT(crcMutex != NULL);

crcCompleteQueue = xQueueCreate(CRC_QUEUE_LENGTH, sizeof(crc_result_t));
ASSERT(crcCompleteQueue != NULL);

crcConfig.mode = CRC_MODE;
crcConfig.crc_channel = CRC_CHANNEL;
crcConfig.scount = CRC_SECTOR_COUNT;
crcConfig.wdg_preload = CRC_MAX_TIMEOUT;
return OBC_ERR_CODE_SUCCESS;
}

obc_error_code_t performCrcVerification(crc_dma_request_t* request, TickType_t mutexTimeout, TickType_t dmaTimeout) {
if (request == NULL) {
return OBC_ERR_CODE_INVALID_ARG;
}

if (xSemaphoreTake(crcMutex, mutexTimeout) != pdPASS) {
return OBC_ERR_CODE_MUTEX_TIMEOUT;
}

crcConfig.pcount = request->crcElementCount;
crcConfig.block_preload = request->crcSeedValue;
crcSetConfig(CRC_BASE, &crcConfig);
RETURN_IF_ERROR_CODE(requestCrcDmaTransfer((uint32_t)request->crcStartAddress, request->crcElementCount));

crc_result_t result = 0;
obc_error_code_t errCode = OBC_ERR_CODE_SUCCESS;
if (xQueueReceive(crcCompleteQueue, &result, dmaTimeout) != pdPASS) {
errCode = OBC_ERR_CODE_CRC_DMA_TIMEOUT;
}

if (result != CRC_COMPLETE) {
errCode = OBC_ERR_CODE_CRC_INTERRUPT_FAILURE;
}

uint64_t crcResult = crcGetSectorSig(CRC_BASE, CRC_CHANNEL);
if (crcResult != request->crcExpectedValue) {
errCode = OBC_ERR_CODE_CRC_FAILURE;
}

xSemaphoreGive(crcMutex);
return errCode;
}

void crcNotification(crcBASE_t* crc, uint32 flags) {
BaseType_t higherPriorityTaskWoken = pdFALSE;

crc_result_t crcResult = CRC_FAILURE;

if (flags & CRC_CH1_CC) {
result = CRC_COMPLETE;
}

xQueueSendToBackFromISR(crcCompleteQueue, &crcResult, &higherPriorityTaskWoken);
portYIELD_FROM_ISR(higherPriorityTaskWoken);
}

static obc_error_code_t requestCrcDmaTransfer(uint32_t startAddress, uint32_t patternCount) {
g_dmaCTRL dmaCtrlPkt = {0};
dmaCtrlPkt.SADD = startAddress;
dmaCtrlPkt.DADD = (uint32_t)(&CRC_BASE->PSA_SIGREGH1);
dmaCtrlPkt.FRCNT = CRC_DMA_FRAME_COUNT;
dmaCtrlPkt.ELCNT = patternCount;
dmaCtrlPkt.ADDMODERD = ADDR_INC1;
dmaCtrlPkt.ADDMODEWR = ADDR_FIXED;
dmaCtrlPkt.PORTASGN = CRC_DMA_PORT;
dmaCtrlPkt.RDSIZE = ACCESS_8_BIT;
dmaCtrlPkt.WRSIZE = ACCESS_64_BIT;
dmaCtrlPkt.TTYPE = FRAME_TRANSFER;

dmaSetCtrlPacket(CRC_DMA_CHANNEL, dmaCtrlPkt);
dmaReqAssign(CRC_DMA_CHANNEL, CRC_DMA_REQLINE);
dmaSetChEnable(CRC_CHANNEL, DMA_SW);
return OBC_ERR_CODE_SUCCESS;
}
23 changes: 23 additions & 0 deletions obc/app/drivers/rm46/obc_crc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef __OBC_CRC_H__
#define __OBC_CRC_H__

#include "obc_errors.h"
#include <os_portmacro.h>
#include <stdint.h>

// Default suggested timeout.
#define OBC_CRC_MUTEX_TIMEOUT pdMS_TO_TICKS(10)
#define OBC_CRC_DMA_TIMEOUT pdMS_TO_TICKS(15)

typedef struct obc_dma_request {
uint32_t crcStartAddress;
uint32_t crcElementCount;
uint64_t crcSeedValue;
uint64_t crcExpectedValue;
} obc_dma_request_t;

obc_error_code_t obcCrcInit();

obc_error_code_t performCrcVerification(crc_dma_request_t* request, TickType_t mutexTimeout, TickType_t dmaTimeout);

#endif
4 changes: 4 additions & 0 deletions obc/app/sys/obc_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,8 @@ typedef enum {
/** Operation is not supported. */
OBC_ERR_CODE_RED_ENOTSUPP = 1524,

OBC_ERR_CODE_CRC_DMA_TIMEOUT = 1100,
OBC_ERR_CODE_CRC_FAILURE,
OBC_ERR_CODE_CRC_INTERRUPT_FAILURE

} obc_error_code_t;
2 changes: 2 additions & 0 deletions obc/bl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ set(BL_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/bl_main.c
${CMAKE_CURRENT_SOURCE_DIR}/source/bl_flash.c
${CMAKE_CURRENT_SOURCE_DIR}/source/bl_uart.c
${CMAKE_CURRENT_SOURCE_DIR}/source/bl_common.c
${CMAKE_CURRENT_SOURCE_DIR}/source/bl_crc.c
${CMAKE_CURRENT_SOURCE_DIR}/F021_Flash_API/source/Fapi_UserDefinedFunctions.c
)

Expand Down
7 changes: 1 addition & 6 deletions obc/bl/bl_main.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "bl_config.h"
#include "bl_flash.h"
#include "bl_common.h"
#include "bl_uart.h"

#include <stdio.h>
Expand All @@ -21,12 +22,6 @@ extern uint32_t __ramFuncsRunEnd__;
/* TYPEDEFS */
typedef void (*appStartFunc_t)(void);

// If this header changes, update the host utility
typedef struct {
uint32_t version;
uint32_t size;
} app_header_t;

typedef enum {
BL_STATE_IDLE,
BL_STATE_DOWNLOAD_IMAGE,
Expand Down
21 changes: 21 additions & 0 deletions obc/bl/include/bl_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __BL_COMMON__
#define __BL_COMMON__

#include "bl_errors.h"
#include <stdint.h>

typedef struct {
uint32_t crc32Checksum;
uint32_t seed;
} bl_checksum_t;

// If this header changes, update the host utility
typedef struct {
uint32_t version;
uint32_t size;
bl_checksum_t blChecksum;
} app_header_t;

bl_error_code_t verifyBinaryChecksum(app_header_t* header);

#endif
18 changes: 18 additions & 0 deletions obc/bl/include/bl_crc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef __BL_CRC_H__
#define __BL_CRC_H__

#include "bl_errors.h"
#include <stdint.h>

typedef struct crc_dma_request {
uint32_t crcStartAddress;
uint32_t crcElementCount;
uint64_t crcSeedValue;
uint64_t crcExpectedValue;
} crc_dma_request_t;

bl_error_code_t blCrcInit();

bl_error_code_t performCrcVerification(crc_dma_request_t* request);

#endif
10 changes: 10 additions & 0 deletions obc/bl/include/bl_errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,14 @@ typedef enum {
BL_ERR_CODE_FAPI_ERASE = 101,
BL_ERR_CODE_FAPI_PROGRAM = 102,

BL_ERR_CODE_CRC_FAILURE = 200,
BL_ERR_CODE_CRC_TIMEOUT = 201,
} bl_error_code_t;

#define RETURN_IF_ERROR_CODE(err) \
{ \
bl_error_code_t _err = err; \
if (_err != BL_ERR_CODE_SUCCESS) { \
return err; \
} \
}
17 changes: 17 additions & 0 deletions obc/bl/source/bl_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "bl_common.h"
#include "bl_config.h"
#include "bl_errors.h"
#include "bl_crc.h"

#include <stdint.h>

bl_error_code_t verifyBinaryChecksum(app_header_t* header) {
crc_dma_request_t request = {0};
request.crcStartAddress = (uint32_t)APP_START_ADDRESS;
request.crcElementCount = header->size;
request.crcSeedValue = header->blChecksum.seed;
request.crcExpectedValue = header->blChecksum.crc32Checksum;

RETURN_IF_ERROR_CODE(performCrcVerification(&request));
return BL_ERR_CODE_SUCCESS;
}
89 changes: 89 additions & 0 deletions obc/bl/source/bl_crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include "bl_crc.h"
#include "bl_errors.h"

#include "crc.h"
#include "reg_crc.h"
#include "sys_dma.h"

#include <stdint.h>

#define CRC_MAX_TIMEOUT 0x00U // Setting it to 0, disables the timeout.
#define CRC_SECTOR_COUNT 0x01U
#define CRC_CHANNEL CRC_CH1
#define CRC_MODE CRC_SEMI_CPU
#define CRC_BASE crcREG

// DMA associated signals
#define CRC_DMA_PORT 0x04 // Only PORT B seems to be valid
#define CRC_DMA_FRAME_COUNT 0x01U
#define CRC_DMA_CHANNEL DMA_CH26
#define CRC_DMA_REQLINE 26U

static enum { CRC_FLAG_CLEAR, CRC_FLAG_SET } crcCompleteFlag = CRC_FLAG_CLEAR;
static enum { CRC_TIMED_OUT, CRC_NO_TIMEOUT } crcTimeoutFlag = CRC_NO_TIMEOUT;
static crcConfig_t crcConfig = {0};

static bl_error_code_t requestCrcDmaTransfer(uint32_t startAddress, uint32_t patternCount);

bl_error_code_t blCrcInit() {
crcInit();
crcEnableNotification(CRC_BASE, CRC_CH1_CC | CRC_CH1_TO);

crcCompleteFlag = CRC_FLAG_CLEAR;
crcTimeoutFlag = CRC_NO_TIMEOUT;

crcConfig.mode = CRC_MODE;
crcConfig.crc_channel = CRC_CHANNEL;
crcConfig.scount = CRC_SECTOR_COUNT;
crcConfig.wdg_preload = CRC_MAX_TIMEOUT;
return BL_ERR_CODE_SUCCESS;
}

bl_error_code_t performCrcVerification(crc_dma_request_t* request) {
crcConfig.pcount = request->crcElementCount;
crcConfig.block_preload = request->crcSeedValue;

crcSetConfig(CRC_BASE, &crcConfig);
RETURN_IF_ERROR_CODE(requestCrcDmaTransfer((uint32_t)request->crcStartAddress, request->crcElementCount));

while (crcCompleteFlag != CRC_FLAG_SET || crcTimeoutFlag != CRC_TIMED_OUT) continue;
if (crcTimeoutFlag == CRC_TIMED_OUT) {
return BL_ERR_CODE_CRC_TIMEOUT;
}

uint64_t crcResult = crcGetSectorSig(CRC_BASE, CRC_CHANNEL);
if (crcResult != request->crcExpectedValue) {
return BL_ERR_CODE_CRC_FAILURE;
}

crcCompleteFlag = CRC_FLAG_CLEAR;
crcTimeoutFlag = CRC_NO_TIMEOUT;
return BL_ERR_CODE_SUCCESS;
}

void crcNotification(crcBASE_t* crc, uint32 flags) {
if (flags == CRC_CH1_CC) {
crcCompleteFlag = CRC_FLAG_SET;
return;
}
crcTimeoutFlag = CRC_TIMED_OUT;
}

static bl_error_code_t requestCrcDmaTransfer(uint32_t startAddress, uint32_t patternCount) {
g_dmaCTRL dmaCtrlPkt = {0};
dmaCtrlPkt.SADD = startAddress;
dmaCtrlPkt.DADD = (uint32_t)(&CRC_BASE->PSA_SIGREGH1);
dmaCtrlPkt.FRCNT = CRC_DMA_FRAME_COUNT;
dmaCtrlPkt.ELCNT = patternCount;
dmaCtrlPkt.ADDMODERD = ADDR_INC1;
dmaCtrlPkt.ADDMODEWR = ADDR_FIXED;
dmaCtrlPkt.PORTASGN = CRC_DMA_PORT;
dmaCtrlPkt.RDSIZE = ACCESS_8_BIT;
dmaCtrlPkt.WRSIZE = ACCESS_64_BIT;
dmaCtrlPkt.TTYPE = FRAME_TRANSFER;

dmaSetCtrlPacket(CRC_DMA_CHANNEL, dmaCtrlPkt);
dmaReqAssign(CRC_DMA_CHANNEL, CRC_DMA_REQLINE);
dmaSetChEnable(CRC_CHANNEL, DMA_SW);
return BL_ERR_CODE_SUCCESS;
}
Loading
Loading