diff --git a/01-SRC/01-MAIN/Misc.c b/01-SRC/01-MAIN/Misc.c index 7e0b24a..855c866 100644 --- a/01-SRC/01-MAIN/Misc.c +++ b/01-SRC/01-MAIN/Misc.c @@ -1,9 +1,45 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: Misc.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include #include #include "Misc.h" #include "../../mcc_generated_files/tmr1.h" -void BlockingDelay(uint16_t u16msDelay) +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ + +/** + * @biref Blocking delay + * @param[in] u16msDelay Delay time in milliseconds + */ +void MMisc_DelayMs(uint16_t u16msDelay) { uint16_t u16Timeout = u16msDelay + TMR1_SoftwareCounterGet(); while (u16Timeout > TMR1_SoftwareCounterGet()) @@ -13,12 +49,12 @@ void BlockingDelay(uint16_t u16msDelay) }; } -void WatchdogEnable(void) +void MMisc_WatchdogEnable(void) { RCONbits.SWDTEN = 1; } -void WatchdogDisable(void) +void MMisc_WatchdogDisable(void) { RCONbits.SWDTEN = 0; } \ No newline at end of file diff --git a/01-SRC/01-MAIN/Misc.h b/01-SRC/01-MAIN/Misc.h index 45e56ce..432cc98 100644 --- a/01-SRC/01-MAIN/Misc.h +++ b/01-SRC/01-MAIN/Misc.h @@ -1,6 +1,25 @@ /* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * * File: Misc.h * Author: Nello + * Mail: nello.chom@protonmail.com * * Created on January 15, 2023, 2:27 PM */ @@ -11,9 +30,9 @@ #include #include -void BlockingDelay(uint16_t u16Timeout); -void WatchdogEnable(void); -void WatchdogDisable(void); +void MMisc_DelayMs(uint16_t u16Timeout); +void MMisc_WatchdogEnable(void); +void MMisc_WatchdogDisable(void); #endif /* MISC_H */ diff --git a/01-SRC/01-MAIN/main.c b/01-SRC/01-MAIN/main.c index a1790cf..c9281fa 100644 --- a/01-SRC/01-MAIN/main.c +++ b/01-SRC/01-MAIN/main.c @@ -1,3 +1,31 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: main.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include "../../mcc_generated_files/system.h" #include "../../mcc_generated_files/pin_manager.h" #include "../../mcc_generated_files/tmr1.h" @@ -8,8 +36,9 @@ #include "../05-BOOTLOADER/bootloader.h" #include "Misc.h" -volatile uint32_t BootRequest __attribute__((address(0x1080), persistent)); - +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ #ifndef _IS_RELEASE const char __attribute__((address(ADDR_APPL_DESC), space(prog))) text[FLASH_LOGISTIC_CHAR_SIZE] = __DATE__" "__TIME__" : Bootloader standalone"; const uint16_t SWVersion __attribute__((address(ADDR_APPL_VERSION), space(prog))) = 0x0001; @@ -20,28 +49,27 @@ void main(void) teMainStates teCurrentState = eStateTransition; SYSTEM_Initialize(); - WatchdogEnable(); - MCU_FPWM_SetHigh(); - InitBackTask(); + MMisc_WatchdogEnable(); + MTarget_InitBackTask(); TMR1_Start(); - InitBootloader(); + Mbootloader_InitBoot(); while(1) { TMR1_Tasks_16BitOperation(); /* SW timer management */ - ManageBackTask(); /* UART frame management */ - manageTimeout(); + MTarget_BackTaskMng(); /* UART frame management */ + Mbootloader_TimeoutMng(); switch(teCurrentState) { case eStateTransition: - teCurrentState = State_Transition(); + teCurrentState = Mbootloader_StateTransition(); break; case eStateIdle: - teCurrentState = State_BootIdle(); + teCurrentState = Mbootloader_StateIdle(); break; case eStateFlash: - teCurrentState = State_Bootloading(); + teCurrentState = Mbootloader_StateLoading(); break; default: diff --git a/01-SRC/02-FLAH_ROUTINES/flash_routines.c b/01-SRC/02-FLAH_ROUTINES/flash_routines.c index c9eac0e..2467740 100644 --- a/01-SRC/02-FLAH_ROUTINES/flash_routines.c +++ b/01-SRC/02-FLAH_ROUTINES/flash_routines.c @@ -1,3 +1,31 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: flash_routines.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include #include "../../mcc_generated_files/system.h" #include "../../mcc_generated_files/memory/flash.h" @@ -5,7 +33,18 @@ #include "../05-BOOTLOADER/bootloader.h" #include "flash_routines.h" -teOperationRetVal FlashReadBufferU8(uint8_t* Fpu8Buffer, uint16_t Fu16Size, uint32_t Fu32FlashAddr) +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ + +/** + * @brief Read an array of uint8 data in flash memory. Uint8 array are also written as uint16 array in flash memory + * @param Fpu8Buffer[out] Pointer to the reading buffer + * @param Fu16Size[in] Number of byte to read + * @param Fu32FlashAddr[in] Flash memory start address (must be modulo 2) + * @return teOperationRetVal + */ +teOperationRetVal MFlash_ReadBufferU8(uint8_t* Fpu8Buffer, uint16_t Fu16Size, uint32_t Fu32FlashAddr) { teOperationRetVal eRetVal = eOperationSuccess; uint16_t u16W = 0; @@ -31,7 +70,13 @@ teOperationRetVal FlashReadBufferU8(uint8_t* Fpu8Buffer, uint16_t Fu16Size, uint return eRetVal; } -teOperationRetVal FlashReadRow(uint8_t* Fpu8Buffer, uint32_t Fu32FlashAddr) +/** + * @brief Read an array of 64 word instructions from flash memory (256 bytes) + * @param Fpu8Buffer[out] Pointer to the reading buffer + * @param Fu32FlashAddr[in] Flash memory start address (must be modulo 2) + * @return teOperationRetVal + */ +teOperationRetVal MFlash_ReadRow(uint8_t* Fpu8Buffer, uint32_t Fu32FlashAddr) { teOperationRetVal eRetVal = eOperationSuccess; uint8_t *pu8Byte = Fpu8Buffer; @@ -42,11 +87,16 @@ teOperationRetVal FlashReadRow(uint8_t* Fpu8Buffer, uint32_t Fu32FlashAddr) { pu32RowData24[u16Cnt] = FLASH_ReadWord24(Fu32FlashAddr + (u16Cnt * 2)); } - WordToCharBuffer(pu32RowData24, pu8Byte, BOOT_ROW_SIZE_WORD); + MFlash_WordToCharBuffer(pu32RowData24, pu8Byte, BOOT_ROW_SIZE_WORD); return eRetVal; } -teOperationRetVal FlashCheckRow(DataBlock_t* FptsWrittenBlock) +/** + * @brief Compare written data into flash with received datablock buffer + * @param FptsWrittenBlock[in] Pointer to the datablock which have been written into flash memory + * @return teOperationRetVal + */ +teOperationRetVal MFlash_CheckRow(DataBlock_t* FptsWrittenBlock) { teOperationRetVal eRetVal = eOperationSuccess; uint32_t u32FlashWord; @@ -66,7 +116,14 @@ teOperationRetVal FlashCheckRow(DataBlock_t* FptsWrittenBlock) return eRetVal; } -uint16_t CharToWordBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16CharSize) +/** + * @brief Transform an array of uint8 into an array of 24bit (uint32) data + * @param Fpu32WordData[out] Pointer to 24bit(uint32) word buffer, the upper byte is interpreted as 0 + * @param Fpu8CharData[in] Pointer to uint8 buffer + * @param Fu16CharSize[in] Number of byte to process + * @return Number of available words + */ +uint16_t MFlash_CharToWordBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16CharSize) { uint16_t u16CharCnt = 0; uint16_t u16WordCnt = 0; @@ -105,7 +162,13 @@ uint16_t CharToWordBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16 return u16WordCnt; } -void WordToCharBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16WordSize) +/** + * @brief Transform an array of 24bit (uint32) data into an array of uint8 data + * @param Fpu32WordData[in] Pointer to 24bit (uint32) word buffer + * @param Fpu8CharData[out] Pointer to uint8 buffer + * @param Fu16WordSize[in] Number of words to process + */ +void MFlash_WordToCharBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16WordSize) { uint32_t *pu32Word = Fpu32WordData; uint8_t *pu8Byte = Fpu8CharData; diff --git a/01-SRC/02-FLAH_ROUTINES/flash_routines.h b/01-SRC/02-FLAH_ROUTINES/flash_routines.h index 656d83f..83b6c4b 100644 --- a/01-SRC/02-FLAH_ROUTINES/flash_routines.h +++ b/01-SRC/02-FLAH_ROUTINES/flash_routines.h @@ -1,12 +1,37 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: flash_routines.h + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + #ifndef FLASH_ROUTINE_H #define FLASH_ROUTINE_H #include #include "../05-BOOTLOADER/BootloaderTypes.h" -teOperationRetVal FlashReadBufferU8(uint8_t* Fpu8Buffer, uint16_t Fu16Size, uint32_t Fu32FlashAddr); -teOperationRetVal FlashReadRow(uint8_t* Fpu8Buffer, uint32_t Fu32FlashAddr); -uint16_t CharToWordBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16CharSize); -teOperationRetVal FlashCheckRow(DataBlock_t* FptsWrittenBlock); -void WordToCharBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16WordSize); +teOperationRetVal MFlash_ReadBufferU8(uint8_t* Fpu8Buffer, uint16_t Fu16Size, uint32_t Fu32FlashAddr); +teOperationRetVal MFlash_ReadRow(uint8_t* Fpu8Buffer, uint32_t Fu32FlashAddr); +uint16_t MFlash_CharToWordBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16CharSize); +teOperationRetVal MFlash_CheckRow(DataBlock_t* FptsWrittenBlock); +void MFlash_WordToCharBuffer(uint32_t* Fpu32WordData, uint8_t* Fpu8CharData, uint16_t Fu16WordSize); #endif \ No newline at end of file diff --git a/01-SRC/03-TARGET/FrameMng.c b/01-SRC/03-TARGET/FrameMng.c index cfa9aa5..ee091ae 100644 --- a/01-SRC/03-TARGET/FrameMng.c +++ b/01-SRC/03-TARGET/FrameMng.c @@ -1,23 +1,60 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: FrameMng.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include #include "../05-BOOTLOADER/BootloaderTypes.h" #include "target.h" #include "FrameMng.h" +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ + +/** + */ static tsFrameSize tsFrameLength[REGISTERED_FRAMES] = { - /* Service ID MIN MAX */ - {eBoot, 2, 2 }, - {eService_gotoBoot, 1, 1 }, - {eService_echo, 2, 65 }, - {eService_getInfo, 2, 2 }, - {eService_eraseFlash, 1, 1 }, - {eService_dataTransfer, 7, 262 }, - {eService_checkFlash, 5, 5 }, - {eService_writePin, 2, 2 }, - {eService_readPin, 2, 2 }, + /* Service ID MIN MAX */ + {eBoot, 2, 2 }, + {eReq_gotoBoot, 1, 1 }, + {eReq_getInfo, 2, 2 }, + {eReq_eraseFlash, 1, 1 }, + {eReq_dataTransfer, 7, MAX_FRM_LEN }, + {eReq_checkFlash, 5, 5 }, }; -teOperationRetVal RxFrameHandler(tsUartFrm * UARTFrm, tsGenericMsg * FpGenMsg) +/** + * + * @param UARTFrm + * @param FpGenMsg + * @return + */ +teOperationRetVal MFrameMng_RxHandler(tsUartFrm * UARTFrm, tsGenericMsg * FpGenMsg) { uint16_t u16i = 0; uint16_t u16j = 0; @@ -102,10 +139,10 @@ teOperationRetVal RxFrameHandler(tsUartFrm * UARTFrm, tsGenericMsg * FpGenMsg) if (RetVal > eOperationNotAvailable) { - FpGenMsg->u8ID += 0x90; + FpGenMsg->u8ID |= 0x90; FpGenMsg->pu8Data[0] = RetVal; FpGenMsg->u16Length = 1; - sendFrame(FpGenMsg); + MTarget_SendFrame(FpGenMsg); } return RetVal; } diff --git a/01-SRC/03-TARGET/FrameMng.h b/01-SRC/03-TARGET/FrameMng.h index 166d044..8ec93b9 100644 --- a/01-SRC/03-TARGET/FrameMng.h +++ b/01-SRC/03-TARGET/FrameMng.h @@ -1,3 +1,28 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: FrameMng.h + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + #ifndef FRAME_MNG_H #define FRAME_MNG_H @@ -5,7 +30,6 @@ #include "../05-BOOTLOADER/BootloaderTypes.h" -#define REGISTERED_FRAMES 9 #define Mcr_ResetAvailable(tsFrm) ((tsFrm).u8Flag &= 0xFE) #define Mcr_SetAvailable(tsFrm) ((tsFrm).u8Flag |= 0x01) #define Mcr_GetAvailable(tsFrm) (uint8_t)((tsFrm).u8Flag & 1) @@ -14,6 +38,6 @@ #define Mcr_SetStartFrame(tsFrm) ((tsFrm).u8Flag |= 0x02) #define Mcr_GetStartFrame(tsFrm) (uint8_t)(((tsFrm).u8Flag >> 1) & 1) -teOperationRetVal RxFrameHandler(tsUartFrm * UARTFrm, tsGenericMsg * BootMsg); +teOperationRetVal MFrameMng_RxHandler(tsUartFrm * UARTFrm, tsGenericMsg * BootMsg); #endif \ No newline at end of file diff --git a/01-SRC/03-TARGET/target.c b/01-SRC/03-TARGET/target.c index 09382fa..0b32991 100644 --- a/01-SRC/03-TARGET/target.c +++ b/01-SRC/03-TARGET/target.c @@ -1,3 +1,31 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: target.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include #include #include "../../mcc_generated_files/system.h" @@ -7,16 +35,25 @@ #include "FrameMng.h" #include "target.h" +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ static teBackTaskStates teCurrentState; static tsGenericMsg tsBootMsg; static tsUartFrm tsUartFrame; -void InitBackTask(void) +/** + * + */ +void MTarget_InitBackTask(void) { teCurrentState = eBackTask_Idle; } -void ManageBackTask(void) +/** + * + */ +void MTarget_BackTaskMng(void) { uint8_t RxData; teOperationRetVal eRetVal; @@ -59,7 +96,7 @@ void ManageBackTask(void) case eBackTask_Data: tsUartFrame.pu8Data[tsUartFrame.u16Index] = RxData; /* Load incoming data into frame buffer */ tsUartFrame.u16Index ++; - eRetVal = RxFrameHandler(&tsUartFrame, &tsBootMsg); + eRetVal = MFrameMng_RxHandler(&tsUartFrame, &tsBootMsg); if (eRetVal == eOperationSuccess) { @@ -83,7 +120,11 @@ void ManageBackTask(void) } } -void sendFrame(tsGenericMsg* FptsTxMsg) +/** + * + * @param FptsTxMsg + */ +void MTarget_SendFrame(tsGenericMsg* FptsTxMsg) { uint8_t u8Checksum = 0; uint8_t pu8TxBuffer[256]; @@ -113,13 +154,19 @@ void sendFrame(tsGenericMsg* FptsTxMsg) FptsTxMsg->u8ID = 0; FptsTxMsg->u16Length = 0; - for (u16i = 0; u16i < MAX_FRM_LEN; u16i++) - { - FptsTxMsg->pu8Data[u16i] = 0; - } +// for (u16i = 0; u16i < MAX_FRM_LEN; u16i++) +// { +// FptsTxMsg->pu8Data[u16i] = 0; +// } + memset(FptsTxMsg->pu8Data, 0, MAX_FRM_LEN); } -teOperationRetVal FrameAvailable(tsGenericMsg* FptsBootMsg) +/** + * + * @param FptsBootMsg + * @return + */ +teOperationRetVal MTarget_FrameAvailable(tsGenericMsg* FptsBootMsg) { teOperationRetVal teRetVal = eOperationNotAvailable; // uint16_t u16i = 0; @@ -130,10 +177,6 @@ teOperationRetVal FrameAvailable(tsGenericMsg* FptsBootMsg) FptsBootMsg->u8ID = tsBootMsg.u8ID; FptsBootMsg->u16Length = tsBootMsg.u16Length; memcpy(FptsBootMsg->pu8Data, tsBootMsg.pu8Data, tsBootMsg.u16Length); -// for (u16i = 0; u16i < tsBootMsg.u16Length; u16i++) -// { -// FptsBootMsg->pu8Data[u16i] = tsBootMsg.pu8Data[u16i]; -// } } else { @@ -141,12 +184,3 @@ teOperationRetVal FrameAvailable(tsGenericMsg* FptsBootMsg) } return teRetVal; } - -//void BufCopy(uint8_t* pu8Dest, uint8_t* pu8Src, uint16_t u16Size) -//{ -// uint16_t u16i; -// for (u16i = 0; u16i < u16Size; u16i++) -// { -// pu8Dest[u16i] = pu8Src[u16i]; -// } -//} diff --git a/01-SRC/03-TARGET/target.h b/01-SRC/03-TARGET/target.h index 27849fe..b44fce7 100644 --- a/01-SRC/03-TARGET/target.h +++ b/01-SRC/03-TARGET/target.h @@ -1,3 +1,28 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: target.h + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + #ifndef TARGET_H #define TARGET_H @@ -10,10 +35,10 @@ typedef enum eBackTask_Data = 1 }teBackTaskStates; -void InitBackTask(void); -void ManageBackTask(void); -teOperationRetVal FrameAvailable(tsGenericMsg* FptsBootMsg); -void sendFrame(tsGenericMsg *FptsTxMsg); +void MTarget_InitBackTask(void); +void MTarget_BackTaskMng(void); +teOperationRetVal MTarget_FrameAvailable(tsGenericMsg* FptsBootMsg); +void MTarget_SendFrame(tsGenericMsg *FptsTxMsg); //void BufCopy(uint8_t* pu8Dest, uint8_t* pu8Src, uint16_t u16Size); #endif \ No newline at end of file diff --git a/01-SRC/04-CRC/crc.c b/01-SRC/04-CRC/crc.c index 66e305f..5ee8a66 100644 --- a/01-SRC/04-CRC/crc.c +++ b/01-SRC/04-CRC/crc.c @@ -1,5 +1,36 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: crc.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include "crc.h" +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ const uint16_t KermitCRC16_Table[256] = { 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, @@ -34,7 +65,12 @@ const uint16_t KermitCRC16_Table[256] = { 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78}; -void updateCrc16(uint16_t* Fpu16Input, uint8_t Fu8Data) +/** + * + * @param Fpu16Input + * @param Fu8Data + */ +void Mcrc_update(uint16_t* Fpu16Input, uint8_t Fu8Data) { uint16_t *pu16InOut = Fpu16Input; uint16_t u16short = 0x00FF & (uint16_t)Fu8Data; @@ -42,7 +78,13 @@ void updateCrc16(uint16_t* Fpu16Input, uint8_t Fu8Data) *pu16InOut = (*(pu16InOut) >> 8) ^ KermitCRC16_Table[u16Tmp & 0xFF]; } -void BufUpdateCrc16(uint16_t* Fpu16Input, uint8_t* Fpu8Data, uint16_t Fu16Length) +/** + * + * @param Fpu16Input + * @param Fpu8Data + * @param Fu16Length + */ +void Mcrc_UpdateBuf(uint16_t* Fpu16Input, uint8_t* Fpu8Data, uint16_t Fu16Length) { uint16_t u16i = 0; uint16_t* pu16CRC = Fpu16Input; @@ -50,6 +92,6 @@ void BufUpdateCrc16(uint16_t* Fpu16Input, uint8_t* Fpu8Data, uint16_t Fu16Length for (u16i = 0; u16i < Fu16Length; u16i++) { - updateCrc16(pu16CRC, *(pu8Data + u16i)); + Mcrc_update(pu16CRC, *(pu8Data + u16i)); } } \ No newline at end of file diff --git a/01-SRC/04-CRC/crc.h b/01-SRC/04-CRC/crc.h index 115e406..a279c4e 100644 --- a/01-SRC/04-CRC/crc.h +++ b/01-SRC/04-CRC/crc.h @@ -1,9 +1,34 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: crc.h + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + #ifndef CRC_H #define CRC_H #include -void updateCrc16(uint16_t* Fpu16Input, uint8_t Fu8Data); -void BufUpdateCrc16(uint16_t* Fpu16Input, uint8_t* Fpu8Data, uint16_t Fu16Length); +void Mcrc_update(uint16_t* Fpu16Input, uint8_t Fu8Data); +void Mcrc_UpdateBuf(uint16_t* Fpu16Input, uint8_t* Fpu8Data, uint16_t Fu16Length); #endif \ No newline at end of file diff --git a/01-SRC/05-BOOTLOADER/BootloaderTypes.h b/01-SRC/05-BOOTLOADER/BootloaderTypes.h index 5053eac..c089f68 100644 --- a/01-SRC/05-BOOTLOADER/BootloaderTypes.h +++ b/01-SRC/05-BOOTLOADER/BootloaderTypes.h @@ -1,3 +1,28 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: BootloaderTypes.h + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + #ifndef BOOT_TYPES_H #define BOOT_TYPES_H @@ -36,17 +61,16 @@ typedef enum typedef enum { - eBoot = 0x00, - eService_gotoBoot = 0x01, - eService_echo = 0x02, - eService_getInfo = 0x03, - eService_eraseFlash = 0x04, - eService_dataTransfer = 0x05, - eService_checkFlash = 0x06, - eService_writePin = 0x0A, - eService_readPin = 0x0B, + eBoot = 0x00, + eReq_gotoBoot = 0x01, + eReq_getInfo = 0x03, + eReq_eraseFlash = 0x04, + eReq_dataTransfer = 0x05, + eReq_checkFlash = 0x06, }teBootServices; +#define REGISTERED_FRAMES 6 + typedef enum { eStateTransition, diff --git a/01-SRC/05-BOOTLOADER/bootloader.c b/01-SRC/05-BOOTLOADER/bootloader.c index 735262c..bfcbff2 100644 --- a/01-SRC/05-BOOTLOADER/bootloader.c +++ b/01-SRC/05-BOOTLOADER/bootloader.c @@ -1,3 +1,31 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: bootloader.c + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + +/******************************************************************************/ +/* INCLUDE */ +/******************************************************************************/ #include #include #include "../../mcc_generated_files/system.h" @@ -11,6 +39,9 @@ #include "BootloaderTypes.h" #include "bootloader.h" +/******************************************************************************/ +/* GLOBAL */ +/******************************************************************************/ static uint8_t u8AppPresentFlag; static uint8_t u8BlankFlashFlag; static uint8_t u8BootloadingFlag; @@ -19,10 +50,12 @@ static DataBlock_t tsDataBlock; static uint32_t pu32SaveRstVector[2] = {0}; static uint32_t u32PrevBlockAddr; static tsGenericMsg tsInternalMsg; +static void Ms_SendBootFrame(uint8_t Fu8Byte0); -static void SendBootFrame(uint8_t Fu8Byte0); - -void resetBootState(void) +/** + * + */ +void Mbootloader_ResetState(void) { u8BlankFlashFlag = 0; u8BootloadingFlag = 0; @@ -31,113 +64,126 @@ void resetBootState(void) pu32SaveRstVector[1] = 0; } -void updateTimeout(void) +/** + * + */ +void Mbootloader_updateTimeout(void) { u16Timeout1 = TMR1_SoftwareCounterGet() + BOOT_TIMEOUT; } -void manageTimeout(void) +/** + * + */ +void Mbootloader_TimeoutMng(void) { TMR1_Tasks_16BitOperation(); if (u16Timeout1 < TMR1_SoftwareCounterGet()) { if ((u8AppPresentFlag != 0) || (u8BootloadingFlag != 0)) { - //SendBootFrame(eBootSessionTimeout); - resetBootState(); - WatchdogDisable(); + Mbootloader_ResetState(); + MMisc_WatchdogDisable(); RESET(); } } } -void SendBootFrame(uint8_t Fu8Byte0) +/** + * + * @param Fu8Byte0 + */ +void Ms_SendBootFrame(uint8_t Fu8Byte0) { tsGenericMsg tsTxMsg; tsTxMsg.u8ID = 0; tsTxMsg.pu8Data[0] = Fu8Byte0; tsTxMsg.u16Length = 1; - sendFrame(&tsTxMsg); + MTarget_SendFrame(&tsTxMsg); } -void InitBootloader(void) +/** + * + */ +void Mbootloader_InitBoot(void) { uint32_t AppliPresent = (uint32_t) FLASH_ReadWord16(ADDR_APPL_FLAG) | ((uint32_t) FLASH_ReadWord16(ADDR_APPL_FLAG + 2) << 16); u8AppPresentFlag = (AppliPresent == APPLIVALID) ? 1 : 0; - resetBootState(); + Mbootloader_ResetState(); ClrWdt(); } -teMainStates State_Transition(void) +/** + * + * @return + */ +teMainStates Mbootloader_StateTransition(void) { teMainStates eRetState = eStateTransition; teOperationRetVal eRetVal = eOperationNotAvailable; tsGenericMsg tsRxMsg; static uint16_t su16TM = 0; static uint8_t su8Cnt = 0; - updateTimeout(); - eRetVal = FrameAvailable(&tsRxMsg); - if ((u8AppPresentFlag != 0) && (BootRequest != BOOTFLAG)) /* Application is present, no bootloader flag*/ + Mbootloader_updateTimeout(); + eRetVal = MTarget_FrameAvailable(&tsRxMsg); + if (u8AppPresentFlag != 0) /* Application is present */ { - /* Sending boot attention message every 50ms, waiting for any rx message */ + /* Sending boot attention message every 100ms, waiting for any rx message */ if (TMR1_SoftwareCounterGet() > su16TM) { - SendBootFrame(eBootAttention); + Ms_SendBootFrame(eBootAttention); su16TM = TMR1_SoftwareCounterGet() + 100; su8Cnt++; } if (eRetVal == eOperationSuccess) { - SendBootFrame(eBootIdle); + Ms_SendBootFrame(eBootIdle); eRetState = eStateIdle; - BootRequest = 0; } else if ((TMR1_SoftwareCounterGet() > 300) || (su8Cnt > 3)) { TMR1_Stop(); - WatchdogDisable(); + MMisc_WatchdogDisable(); StartApplication(); } } else { - SendBootFrame(eBootIdle); + Ms_SendBootFrame(eBootIdle); eRetState = eStateIdle; } return eRetState; } -teMainStates State_BootIdle(void) +/** + * + * @return + */ +teMainStates Mbootloader_StateIdle(void) { tsGenericMsg tsRxMsg; teMainStates eRetState = eStateIdle; teOperationRetVal eRetVal = eOperationNotAvailable; ClrWdt(); - if (FrameAvailable(&tsRxMsg) == eOperationSuccess) /* On Rx frame */ + if (MTarget_FrameAvailable(&tsRxMsg) == eOperationSuccess) /* On Rx frame */ { - updateTimeout(); + Mbootloader_updateTimeout(); switch (tsRxMsg.u8ID) { - case eService_gotoBoot: - eRetVal = serviceGoToBoot(&tsRxMsg); - break; - - case eService_echo: - eRetVal = serviceEcho(&tsRxMsg); - break; - - case eService_getInfo: - eRetVal = serviceGetInfo(&tsRxMsg); + case eReq_gotoBoot: + eRetVal = Mbootloader_RqStartBoot(&tsRxMsg); break; - case eService_writePin: + case eReq_getInfo: + eRetVal = Mbootloader_RqGetInfo(&tsRxMsg); break; - case eService_readPin: - break; - default : + tsInternalMsg.u8ID = tsRxMsg.u8ID | 0x80; + tsInternalMsg.pu8Data[0] = eOperationNotAllowed; + tsInternalMsg.u16Length = 1; + MTarget_SendFrame(&tsInternalMsg); break; } if (u8BootloadingFlag != 0) @@ -148,82 +194,78 @@ teMainStates State_BootIdle(void) return eRetState; } -teMainStates State_Bootloading(void) +/** + * + * @return + */ +teMainStates Mbootloader_StateLoading(void) { tsGenericMsg tsRxMsg; teOperationRetVal eRetVal; teMainStates eRetState = eStateFlash; - if (FrameAvailable(&tsRxMsg) == eOperationSuccess) /* On Rx frame */ + if (MTarget_FrameAvailable(&tsRxMsg) == eOperationSuccess) /* On Rx frame */ { - updateTimeout(); + Mbootloader_updateTimeout(); ClrWdt(); switch (tsRxMsg.u8ID) { - case eService_eraseFlash: - eRetVal = serviceEraseFlash(&tsRxMsg); + case eReq_eraseFlash: + eRetVal = Mbootloader_RqEraseFlash(&tsRxMsg); break; - case eService_dataTransfer: - eRetVal = serviceDataTransfer(&tsRxMsg); + case eReq_dataTransfer: + eRetVal = Mbootloader_RqDataTransfer(&tsRxMsg); break; - case eService_checkFlash: - eRetVal = serviceCheckFlash(&tsRxMsg); + case eReq_checkFlash: + eRetVal = Mbootloader_RqCheckFlash(&tsRxMsg); break; default: + tsInternalMsg.u8ID = tsRxMsg.u8ID | 0x80; + tsInternalMsg.pu8Data[0] = eOperationNotAllowed; + tsInternalMsg.u16Length = 1; + MTarget_SendFrame(&tsInternalMsg); break; } if (eRetVal != eOperationSuccess) { - SendBootFrame(eBootIdle); + Ms_SendBootFrame(eBootIdle); eRetState = eStateIdle; - resetBootState(); + Mbootloader_ResetState(); } } return eRetState; } -teOperationRetVal serviceGoToBoot(tsGenericMsg* FptsGenMsg) +/** + * + * @param FptsGenMsg + * @return + */ +teOperationRetVal Mbootloader_RqStartBoot(tsGenericMsg* FptsGenMsg) { teOperationRetVal eRetVal = eOperationSuccess; u8BootloadingFlag = 1; - updateTimeout(); + Mbootloader_updateTimeout(); TMR1_Tasks_16BitOperation(); tsInternalMsg.u8ID = FptsGenMsg->u8ID | 0x80; tsInternalMsg.pu8Data[0] = eRetVal; tsInternalMsg.u16Length = 1; - sendFrame(&tsInternalMsg); - - return eRetVal; -} - -teOperationRetVal serviceEcho(tsGenericMsg* FptsGenMsg) -{ - teOperationRetVal eRetVal = eOperationSuccess; - - tsInternalMsg.u8ID = FptsGenMsg->u8ID | 0x80; - tsInternalMsg.u16Length = 1 + FptsGenMsg->u16Length; - -// for (u8i = 0; u8i < FptsGenMsg->u16Length; u8i++) -// { -// u8TmpBuff[u8i + 1] = FptsGenMsg->pu8Data[u8i]; -// tsInternalMsg.u16Length++; -// } - tsInternalMsg.pu8Data[0] = eRetVal; - memcpy(&tsInternalMsg.pu8Data[1], FptsGenMsg->pu8Data, FptsGenMsg->u16Length); - -// BufCopy(tsInternalMsg.pu8Data, u8TmpBuff, tsInternalMsg.u16Length); - - sendFrame(&tsInternalMsg); + MTarget_SendFrame(&tsInternalMsg); return eRetVal; } -teOperationRetVal serviceGetInfo(tsGenericMsg * FptsGenMsg) +/** + * + * @param FptsGenMsg + * @return + */ +teOperationRetVal Mbootloader_RqGetInfo(tsGenericMsg * FptsGenMsg) { teOperationRetVal eRetVal = eOperationSuccess; uint8_t* pu8Buff = tsInternalMsg.pu8Data; @@ -255,7 +297,7 @@ teOperationRetVal serviceGetInfo(tsGenericMsg * FptsGenMsg) break; case 2: /* Get application logistic ascii string */ - FlashReadBufferU8(&pu8Buff[1], FLASH_LOGISTIC_CHAR_SIZE, ADDR_APPL_DESC); + MFlash_ReadBufferU8(&pu8Buff[1], FLASH_LOGISTIC_CHAR_SIZE, ADDR_APPL_DESC); tsInternalMsg.u16Length += FLASH_LOGISTIC_CHAR_SIZE; break; @@ -269,12 +311,17 @@ teOperationRetVal serviceGetInfo(tsGenericMsg * FptsGenMsg) tsInternalMsg.pu8Data[0] = eRetVal; } - sendFrame(&tsInternalMsg); + MTarget_SendFrame(&tsInternalMsg); return eRetVal; } -teOperationRetVal serviceEraseFlash(tsGenericMsg * FptsGenMsg) +/** + * + * @param FptsGenMsg + * @return + */ +teOperationRetVal Mbootloader_RqEraseFlash(tsGenericMsg * FptsGenMsg) { teOperationRetVal eRetVal = eOperationSuccess; uint32_t u32i = 0; @@ -301,7 +348,7 @@ teOperationRetVal serviceEraseFlash(tsGenericMsg * FptsGenMsg) u8Err &= FLASH_ErasePage(ADDR_FLASH_APPLI + (u32i * FLASH_ERASE_PAGE_SIZE_IN_PC_UNITS)); u32i++; TMR1_Tasks_16BitOperation(); - updateTimeout(); + Mbootloader_updateTimeout(); ClrWdt(); } if ((u8Err == 1) && (u32i == FLASH_APPLI_PAGES)) @@ -321,12 +368,17 @@ teOperationRetVal serviceEraseFlash(tsGenericMsg * FptsGenMsg) tsInternalMsg.u16Length = 1; tsInternalMsg.pu8Data[0] = eRetVal; - sendFrame(&tsInternalMsg); + MTarget_SendFrame(&tsInternalMsg); return eRetVal; } -teOperationRetVal serviceDataTransfer(tsGenericMsg * FptsGenMsg) +/** + * + * @param FptsGenMsg + * @return + */ +teOperationRetVal Mbootloader_RqDataTransfer(tsGenericMsg * FptsGenMsg) { teOperationRetVal eRetVal = eOperationSuccess; @@ -341,7 +393,7 @@ teOperationRetVal serviceDataTransfer(tsGenericMsg * FptsGenMsg) if(eRetVal == eOperationSuccess) { - eRetVal = manageDataBlock(FptsGenMsg, &tsDataBlock); + eRetVal = Mbootloader_ProcessDataBlock(FptsGenMsg, &tsDataBlock); } if (eRetVal == eOperationSuccess) @@ -356,18 +408,23 @@ teOperationRetVal serviceDataTransfer(tsGenericMsg * FptsGenMsg) if (eRetVal == eOperationSuccess) { - eRetVal = FlashCheckRow(&tsDataBlock); + eRetVal = MFlash_CheckRow(&tsDataBlock); } tsInternalMsg.u8ID = FptsGenMsg->u8ID | 0x80; tsInternalMsg.u16Length = 1; tsInternalMsg.pu8Data[0] = eRetVal; - sendFrame(&tsInternalMsg); + MTarget_SendFrame(&tsInternalMsg); return eRetVal; } -teOperationRetVal serviceCheckFlash(tsGenericMsg * FptsGenMsg) +/** + * + * @param FptsGenMsg + * @return + */ +teOperationRetVal Mbootloader_RqCheckFlash(tsGenericMsg * FptsGenMsg) { teOperationRetVal eRetVal = eOperationSuccess; uint16_t u16Cnt = 0; @@ -393,7 +450,7 @@ teOperationRetVal serviceCheckFlash(tsGenericMsg * FptsGenMsg) { /* Calc first page (IVT + logistic info data), one page is 8 rows */ u32RowAddr = u16Cnt * BOOT_ROW_SIZE_WORD * 2; - FlashReadRow(pu8DataRowByte, u32RowAddr); + MFlash_ReadRow(pu8DataRowByte, u32RowAddr); if (u32RowAddr == 0) { /* Blank reset vector address */ @@ -406,19 +463,19 @@ teOperationRetVal serviceCheckFlash(tsGenericMsg * FptsGenMsg) pu8DataRowByte[6] = 0xFF; pu8DataRowByte[7] = 0x00; } - BufUpdateCrc16(&u16CRC, pu8DataRowByte, BOOT_ROW_SIZE_BYTE); + Mcrc_UpdateBuf(&u16CRC, pu8DataRowByte, BOOT_ROW_SIZE_BYTE); } u16CrcIvt = u16CRC; ClrWdt(); for (u16Cnt = 0; u16Cnt < u16AppliRowCnt - 8; u16Cnt++) { u32RowAddr = ADDR_FLASH_APPLI + (u16Cnt * BOOT_ROW_SIZE_WORD * 2); - FlashReadRow(pu8DataRowByte, u32RowAddr); - BufUpdateCrc16(&u16CRC, pu8DataRowByte, BOOT_ROW_SIZE_BYTE); + MFlash_ReadRow(pu8DataRowByte, u32RowAddr); + Mcrc_UpdateBuf(&u16CRC, pu8DataRowByte, BOOT_ROW_SIZE_BYTE); } u16CrcApp = u16CRC; - updateCrc16(&u16CRC, FptsGenMsg->pu8Data[0]); - updateCrc16(&u16CRC, FptsGenMsg->pu8Data[1]); + Mcrc_update(&u16CRC, FptsGenMsg->pu8Data[0]); + Mcrc_update(&u16CRC, FptsGenMsg->pu8Data[1]); if (u16CRC == 0xF0B8) { ClrWdt(); @@ -444,12 +501,12 @@ teOperationRetVal serviceCheckFlash(tsGenericMsg * FptsGenMsg) tsInternalMsg.pu8Data[4] = u16CrcApp; tsInternalMsg.pu8Data[5] = u16CRC >> 8; tsInternalMsg.pu8Data[6] = u16CRC; - sendFrame(&tsInternalMsg); + MTarget_SendFrame(&tsInternalMsg); if (eRetVal == eOperationSuccess) { - BlockingDelay(50); + MMisc_DelayMs(50); TMR1_Stop(); - resetBootState(); + Mbootloader_ResetState(); RESET(); } } @@ -457,20 +514,13 @@ teOperationRetVal serviceCheckFlash(tsGenericMsg * FptsGenMsg) return eRetVal; } -teOperationRetVal serviceWritePin(tsGenericMsg * FptsGenMsg) -{ - teOperationRetVal eRetVal = eOperationSuccess; - return eRetVal; -} - -teOperationRetVal serviceReadPin(tsGenericMsg * FptsGenMsg) -{ - - teOperationRetVal eRetVal = eOperationSuccess; - return eRetVal; -} - -teOperationRetVal manageDataBlock(tsGenericMsg * FptsGenMsg, DataBlock_t * FptsBlock) +/** + * + * @param FptsGenMsg + * @param FptsBlock + * @return + */ +teOperationRetVal Mbootloader_ProcessDataBlock(tsGenericMsg * FptsGenMsg, DataBlock_t * FptsBlock) { teOperationRetVal eRetVal = eOperationSuccess; uint8_t u8DataCnt = 0; @@ -478,7 +528,7 @@ teOperationRetVal manageDataBlock(tsGenericMsg * FptsGenMsg, DataBlock_t * FptsB uint16_t u16CRC = 0; /* STEP 1: CRC check */ - BufUpdateCrc16(&u16CRC, FptsGenMsg->pu8Data, FptsGenMsg->u16Length); + Mcrc_UpdateBuf(&u16CRC, FptsGenMsg->pu8Data, FptsGenMsg->u16Length); if (u16CRC != 0xF0B8) { eRetVal = eBadCRCBlock; @@ -532,7 +582,7 @@ teOperationRetVal manageDataBlock(tsGenericMsg * FptsGenMsg, DataBlock_t * FptsB } } - u16Tmp = CharToWordBuffer(FptsBlock->pu32Word, FptsBlock->pu8Data, BOOT_ROW_SIZE_BYTE); + u16Tmp = MFlash_CharToWordBuffer(FptsBlock->pu32Word, FptsBlock->pu8Data, BOOT_ROW_SIZE_BYTE); if (FptsBlock->u32BlockAddr == 0) /* Restor reset vector */ { diff --git a/01-SRC/05-BOOTLOADER/bootloader.h b/01-SRC/05-BOOTLOADER/bootloader.h index fe30864..69e520c 100644 --- a/01-SRC/05-BOOTLOADER/bootloader.h +++ b/01-SRC/05-BOOTLOADER/bootloader.h @@ -1,3 +1,28 @@ +/* + * The dsPIC33EP-Bootloader is a basic and simple UART bootloaloader that + * is designed to work with all dsPIC33EP 16bit Microchip MCU family. + * + * Copyright (C) 2023 Nello Chommanivong + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * File: bootloader.h + * Author: Nello + * Mail: nello.chom@protonmail.com + * + */ + #ifndef BOOTLOADER_H #define BOOTLOADER_H @@ -31,25 +56,20 @@ #define ADDR_APPL_DESC (0x280) #define ADDR_APPL_VERSION (0x300) -extern volatile uint32_t BootRequest; - -void resetBootState(void); -void updateTimeout(void); -void manageTimeout(void); -void InitBootloader(void); - -teMainStates State_Transition(void); -teMainStates State_Bootloading(void); -teMainStates State_BootIdle(void); - -teOperationRetVal serviceGoToBoot(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceEcho(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceGetInfo(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceEraseFlash(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceDataTransfer(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceCheckFlash(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceWritePin(tsGenericMsg* FptsGenMsg); -teOperationRetVal serviceReadPin(tsGenericMsg* FptsGenMsg); -teOperationRetVal manageDataBlock(tsGenericMsg * FptsGenMsg, DataBlock_t * FptsBlock); +void Mbootloader_ResetState(void); +void Mbootloader_updateTimeout(void); +void Mbootloader_TimeoutMng(void); +void Mbootloader_InitBoot(void); + +teMainStates Mbootloader_StateTransition(void); +teMainStates Mbootloader_StateLoading(void); +teMainStates Mbootloader_StateIdle(void); + +teOperationRetVal Mbootloader_RqStartBoot(tsGenericMsg* FptsGenMsg); +teOperationRetVal Mbootloader_RqGetInfo(tsGenericMsg* FptsGenMsg); +teOperationRetVal Mbootloader_RqEraseFlash(tsGenericMsg* FptsGenMsg); +teOperationRetVal Mbootloader_RqDataTransfer(tsGenericMsg* FptsGenMsg); +teOperationRetVal Mbootloader_RqCheckFlash(tsGenericMsg* FptsGenMsg); +teOperationRetVal Mbootloader_ProcessDataBlock(tsGenericMsg * FptsGenMsg, DataBlock_t * FptsBlock); #endif \ No newline at end of file diff --git a/mcc_generated_files/pin_manager.c b/mcc_generated_files/pin_manager.c index 39da048..097e478 100644 --- a/mcc_generated_files/pin_manager.c +++ b/mcc_generated_files/pin_manager.c @@ -61,16 +61,16 @@ void PIN_MANAGER_Initialize (void) /**************************************************************************** * Setting the Output Latch SFR(s) ***************************************************************************/ - LATA = 0x0000; + LATA = 0x0200; LATB = 0x0100; - LATC = 0x0000; + LATC = 0x0008; /**************************************************************************** * Setting the GPIO Direction SFR(s) ***************************************************************************/ - TRISA = 0x0387; + TRISA = 0x0187; TRISB = 0xFEFF; - TRISC = 0x03BF; + TRISC = 0x03B7; /**************************************************************************** * Setting the Weak Pull Up and Weak Pull Down SFR(s) @@ -92,18 +92,25 @@ void PIN_MANAGER_Initialize (void) /**************************************************************************** * Setting the Analog/Digital Configuration SFR(s) ***************************************************************************/ - ANSELA = 0x020A; + ANSELA = 0x000A; ANSELB = 0x0203; - ANSELC = 0x003F; + ANSELC = 0x0037; /**************************************************************************** * Set the PPS ***************************************************************************/ __builtin_write_OSCCONL(OSCCON & 0xbf); // unlock PPS + RPINR26bits.C1RXR = 0x0030; //RC0->ECAN1:C1RX + RPINR19bits.U2RXR = 0x0037; //RC7->UART2:U2RX + RPOR7bits.RP57R = 0x000F; //RC9->ECAN2:C2TX + RPINR18bits.U1RXR = 0x0027; //RB7->UART1:U1RX RPOR0bits.RP20R = 0x0031; //RA4->INTERNAL OSCILLATOR:REFCLK + RPOR3bits.RP41R = 0x0003; //RB9->UART2:U2TX + RPOR1bits.RP36R = 0x000F; //RB4->ECAN2:C2TX + RPINR26bits.C2RXR = 0x0018; //RA8->ECAN2:C2RX RPOR3bits.RP40R = 0x0001; //RB8->UART1:U1TX - RPINR18bits.U1RXR = 0x0027; //RB7->UART1:U1RX + RPOR5bits.RP49R = 0x000E; //RC1->ECAN1:C1TX __builtin_write_OSCCONL(OSCCON | 0x40); // lock PPS } diff --git a/mcc_generated_files/pin_manager.h b/mcc_generated_files/pin_manager.h index a1cc89d..41b6073 100644 --- a/mcc_generated_files/pin_manager.h +++ b/mcc_generated_files/pin_manager.h @@ -492,6 +492,152 @@ */ #define MCU_FPWM_SetDigitalOutput() (_TRISA10 = 0) +/** + @Summary + Sets the GPIO pin, RA9, high using LATA9. + + @Description + Sets the GPIO pin, RA9, high using LATA9. + + @Preconditions + The RA9 must be set to an output. + + @Returns + None. + + @Param + None. + + @Example + + // Set RA9 high (1) + CAN1_RS_SetHigh(); + + +*/ +#define CAN1_RS_SetHigh() (_LATA9 = 1) +/** + @Summary + Sets the GPIO pin, RA9, low using LATA9. + + @Description + Sets the GPIO pin, RA9, low using LATA9. + + @Preconditions + The RA9 must be set to an output. + + @Returns + None. + + @Param + None. + + @Example + + // Set RA9 low (0) + CAN1_RS_SetLow(); + + +*/ +#define CAN1_RS_SetLow() (_LATA9 = 0) +/** + @Summary + Toggles the GPIO pin, RA9, using LATA9. + + @Description + Toggles the GPIO pin, RA9, using LATA9. + + @Preconditions + The RA9 must be set to an output. + + @Returns + None. + + @Param + None. + + @Example + + // Toggle RA9 + CAN1_RS_Toggle(); + + +*/ +#define CAN1_RS_Toggle() (_LATA9 ^= 1) +/** + @Summary + Reads the value of the GPIO pin, RA9. + + @Description + Reads the value of the GPIO pin, RA9. + + @Preconditions + None. + + @Returns + None. + + @Param + None. + + @Example + + uint16_t portValue; + + // Read RA9 + postValue = CAN1_RS_GetValue(); + + +*/ +#define CAN1_RS_GetValue() _RA9 +/** + @Summary + Configures the GPIO pin, RA9, as an input. + + @Description + Configures the GPIO pin, RA9, as an input. + + @Preconditions + None. + + @Returns + None. + + @Param + None. + + @Example + + // Sets the RA9 as an input + CAN1_RS_SetDigitalInput(); + + +*/ +#define CAN1_RS_SetDigitalInput() (_TRISA9 = 1) +/** + @Summary + Configures the GPIO pin, RA9, as an output. + + @Description + Configures the GPIO pin, RA9, as an output. + + @Preconditions + None. + + @Returns + None. + + @Param + None. + + @Example + + // Sets the RA9 as an output + CAN1_RS_SetDigitalOutput(); + + +*/ +#define CAN1_RS_SetDigitalOutput() (_TRISA9 = 0) /** @Summary Sets the GPIO pin, RB1, high using LATB1. @@ -638,6 +784,152 @@ */ #define MCU_MEAS_TEMP_SetDigitalOutput() (_TRISB1 = 0) +/** + @Summary + Sets the GPIO pin, RC3, high using LATC3. + + @Description + Sets the GPIO pin, RC3, high using LATC3. + + @Preconditions + The RC3 must be set to an output. + + @Returns + None. + + @Param + None. + + @Example + + // Set RC3 high (1) + CAN2_RS_SetHigh(); + + +*/ +#define CAN2_RS_SetHigh() (_LATC3 = 1) +/** + @Summary + Sets the GPIO pin, RC3, low using LATC3. + + @Description + Sets the GPIO pin, RC3, low using LATC3. + + @Preconditions + The RC3 must be set to an output. + + @Returns + None. + + @Param + None. + + @Example + + // Set RC3 low (0) + CAN2_RS_SetLow(); + + +*/ +#define CAN2_RS_SetLow() (_LATC3 = 0) +/** + @Summary + Toggles the GPIO pin, RC3, using LATC3. + + @Description + Toggles the GPIO pin, RC3, using LATC3. + + @Preconditions + The RC3 must be set to an output. + + @Returns + None. + + @Param + None. + + @Example + + // Toggle RC3 + CAN2_RS_Toggle(); + + +*/ +#define CAN2_RS_Toggle() (_LATC3 ^= 1) +/** + @Summary + Reads the value of the GPIO pin, RC3. + + @Description + Reads the value of the GPIO pin, RC3. + + @Preconditions + None. + + @Returns + None. + + @Param + None. + + @Example + + uint16_t portValue; + + // Read RC3 + postValue = CAN2_RS_GetValue(); + + +*/ +#define CAN2_RS_GetValue() _RC3 +/** + @Summary + Configures the GPIO pin, RC3, as an input. + + @Description + Configures the GPIO pin, RC3, as an input. + + @Preconditions + None. + + @Returns + None. + + @Param + None. + + @Example + + // Sets the RC3 as an input + CAN2_RS_SetDigitalInput(); + + +*/ +#define CAN2_RS_SetDigitalInput() (_TRISC3 = 1) +/** + @Summary + Configures the GPIO pin, RC3, as an output. + + @Description + Configures the GPIO pin, RC3, as an output. + + @Preconditions + None. + + @Returns + None. + + @Param + None. + + @Example + + // Sets the RC3 as an output + CAN2_RS_SetDigitalOutput(); + + +*/ +#define CAN2_RS_SetDigitalOutput() (_TRISC3 = 0) /** @Summary Sets the GPIO pin, RC6, high using LATC6. diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index c5a4f8a..e93696b 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -125,7 +125,7 @@ ICD3PlatformTool XC16 - 1.41 + 2.00 3 @@ -501,7 +501,7 @@ noID XC16 - 1.41 + 2.00 3