Download presentation
Presentation is loading. Please wait.
Published byChristina Spencer Modified over 6 years ago
1
Build a microSD Bootloader using a PIC microcontroller
Bootloader Firmware Design 02/06/2013 Fred Eady
2
Bootloader Firmware Topics
BOOTLOADER HARDWARE MAPPING HardwareProfile.h microSD Card SPI Portal LCD Controls/LCD SPI Portal Push Buttons BOOTLOADER FILES BOOTLOADER OPERATION SUMMARY
3
Bootloader Firmware Topics
BOOTLOADER HARDWARE MAPPING HardwareProfile.h microSD Card SPI Portal LCD Controls/LCD SPI Portal Push Buttons BOOTLOADER FILES BOOTLOADER OPERATION SUMMARY
4
Bootloader HardwareProfile.h
microSD CARD SPI PORTAL #define USE_SD_INTERFACE_WITH_SPI #define SD_CS LATDbits.LATD0 #define SD_CS_TRIS TRISDbits.TRISD0 #define SPICLOCK TRISCbits.TRISC3 #define SPIIN TRISCbits.TRISC4 #define SPIOUT TRISCbits.TRISC5 #define SPICLOCKLAT LATCbits.LATC3 #define SPIINLAT LATCbits.LATC4 #define SPIOUTLAT LATCbits.LATC5 #define SPICLOCKPORT PORTCbits.RC3 #define SPIINPORT PORTCbits.RC4
5
Bootloader HardwareProfile.h
PIC18F47J13 SPI1 Module Registers #define SPICON SSP1CON1 #define SPISTAT SSP1STAT #define SPIBUF SSP1BUF #define SPISTAT_RBF SSP1STATbits.BF #define SPICON1bits SSP1CON1bits #define SPISTATbits SSP1STATbits #define SPI_INTERRUPT_FLAG PIR1bits.SSP1IF #define SPI_INTERRUPT_FLAG_ASM PIR1, 3 #define SPIENABLE SSP1CON1bits.SSPEN
6
Bootloader SD-SPI.c } SD-SPI.c Implements the SD Card API
void MDD_SDSPI_InitIO (void) { // Turn off the card SD_CS = 1; SD_CS_TRIS = OUTPUT; } SD-SPI.c Implements the SD Card API MDD_SDSPI_MediaDetect MDD_SDSPI_MediaInitialize MDD_SDSPI_ReadCapacity MDD_SDSPI_ReadSectorSize MDD_SDSPI_InitIO MDD_SDSPI_SectorRead MDD_SDSPI_SectorWrite MDD_SDSPI_AsyncReadTasks MDD_SDSPI_AsyncWriteTasks MDD_SDSPI_WriteProtectState MDD_SDSPI_ShutdownMedia
7
Bootloader SD-SPI.h SD-SPI.h Contains SPI Init Definitions
#define USE_SD_INTERFACE_WITH_SPI #ifdef USE_SD_INTERFACE_WITH_SPI #include "MDD File System/SD-SPI.h" #endif SD-SPI.h Contains SPI Init Definitions Defines SD Card States Defines SD Card Command Codes Defines SD Card Command Structures Defines SD Card Response Types Contains Prototypes of API Functions Provided in SD-SPI.c
8
Bootloader Firmware Topics
BOOTLOADER HARDWARE MAPPING HardwareProfile.h microSD Card SPI Portal LCD Controls/LCD SPI Portal Push Buttons BOOTLOADER FILES BOOTLOADER OPERATION SUMMARY
9
Bootloader LCD Controls
LCD_RST Active Low Reset Signal LCD_RS Register Select Signal 0 – Instruction 1 – Data LCD_CS Active Low Chip Select *SHDN LCD Power Switch
10
Bootloader HardwareProfile.h
LCD MODULE CONTROL BITS #define LCD_SHDN_IOLATDbits.LATD4 #define LCD_SHDN_TRISTRISDbits.TRISD4 #define LCD_RSTLATDbits.LATD3 #define LCD_RST_TRISTRISDbits.TRISD3 #define LCD_RSLATDbits.LATD2 #define LCD_RS_TRISTRISDbits.TRISD2 #define LCD_CSLATDbits.LATD1 #define LCD_CS_TRISTRISDbits.TRISD1
11
Bootloader LCD SPI Portal
Code Found in bootio.c Function: BLIO_InitializeIO EECON2 = 0x55; EECON2 = 0xAA; PPSCONbits.IOLOCK = 0; RPINR21 = 8;//SDI2 AT RB5 RP8 RPOR6 = 11; //SCK2 AT RB3 RP6 RPOR7 = 10; //SDO2 AT RB4 RP7 PPSCONbits.IOLOCK = 1;
12
Bootloader Firmware Topics
BOOTLOADER HARDWARE MAPPING HardwareProfile.h microSD Card SPI Portal LCD Controls/LCD SPI Portal Push Buttons BOOTLOADER FILES BOOTLOADER OPERATION SUMMARY
13
Bootloader Pushbuttons
#define BUTTON1PORTDbits.RD5 #define BUTTON1_TRISTRISDbits.TRISD5 #define BUTTON2PORTDbits.RD6 #define BUTTON2_TRISTRISDbits.TRISD6
14
Bootloader Firmware Topics
BOOTLOADER HARDWARE MAPPING HardwareProfile.h microSD Card SPI Portal LCD Controls/LCD SPI Portal Push Buttons BOOTLOADER FILES BOOTLOADER OPERATION SUMMARY
15
Bootloader Files boot_config.h Defines Application Address
Defines Remapped Interrupt Vector Addresses Defines Maximum Size of FLASH Block boot_io.c Initialize PIC18F47J13 I/O Routines Functions to Scan Load and Abort Push Buttons SD-SPI.c Contains SD Card API FSIO.c File System Operations (FSfopen, FSfclose, etc.) FSconfig.h Define Locations of Data and FAT Buffers Defines File System Behavior (ALLOW_FILESEARCH)
16
Bootloader Files boot_load_hex.c
Contains ASCII helper functions for boot_media_msd.c boot_media_msd.c Processes Intel HEX File Programs PIC18F47J13 FLASH with Application Code xxxxxxxx.hex A User Generated Intel HEX File Containing the Application Code HardwareProfile.h Contains Hardware Mapping for the PIC18F47J13 lcd.c The LCD Module Driver
17
Bootloader Firmware Topics
BOOTLOADER HARDWARE MAPPING HardwareProfile.h microSD Card SPI Portal LCD Controls/LCD SPI Portal Push Buttons BOOTLOADER FILES BOOTLOADER OPERATION SUMMARY
18
Bootloader Process The Original SD-SPI.c API Call Names Are Aliased in FSconfig.h as Follows: #define MDD_MediaInitialize MDD_SDSPI_MediaInitialize #define MDD_MediaDetect MDD_SDSPI_MediaDetect #define MDD_SectorRead MDD_SDSPI_SectorRead #define MDD_SectorWrite MDD_SDSPI_SectorWrite #define MDD_InitIO MDD_SDSPI_InitIO #define MDD_ShutdownMedia MDD_SDSPI_ShutdownMedia #define MDD_WriteProtectState MDD_SDSPI_WriteProtectState #define MDD_ReadSectorSize MDD_SDSPI_ReadSectorSize #define MDD_ReadCapacity MDD_SDSPI_ReadCapacity
19
Bootloader Process The Original SD-SPI.c API and FSIO.c Calls Are Aliased in boot_config.h as Follows: #define BLMedia_InitializeTransport() FSInit() #define BLMedia_DeinitializeTransport() TRUE #define BLMedia_MediumAttached() MDD_MediaDetect()
20
Bootloader Process Remap the Interrupt Vectors
Remapped Interrupt Vector Addresses Found in boot_config.h #define APPLICATION_HIGH_ISR xA008 #define APPLICATION_LOW_ISR xA018 #pragma code High_ISR = 0x08 void Remapped_High_ISR (void) { _asm goto APPLICATION_HIGH_ISR _endasm } #pragma code Low_ISR = 0x18 void Remapped_Low_ISR (void) _asm goto APPLICATION_LOW_ISR _endasm
21
Bootloader Process Call BLIO_InitializeIO in bootio.c
Disable Analog to Digital Converter Set TRIS Registers (PORTA-PORTE) Initialize the LCD Control Pins Deselect microSD Card at SPI Portal Level Use Peripheral Pin Select to Enable SPI2 for LCD Initialize the LCD Module Display “microSD Bootloader” for 3 Seconds sLCDInit(); strcpypgm2ram((char*)sLCDText, " microSD Bootloader "); //strcpypgm2ram((char*)sLCDText, " "); sLCDUpdate(); DelayMs(3000);
22
Bootloader Process Initialize the LOAD Countdown Timer
Initialize the count array with 0x20 Load the last byte of the count array with 0x00 unsigned char j; unsigned char count[17]; for(j=0;j<sizeof(count);j++) { count[j]=' '; } count[16] = 0x00;
23
Bootloader Process Display “PRESS BUTTON 1 TO LOAD” on Line 1
Alternately Place the Countdown Number at LCD Position 8 on Line 1 j = 0x39; while (j > 0x30 ) { i = 0; count[8] = j; strcpypgm2ram((char*)sLCDText, " PRESS BUTTON TO LOAD "); //strcpypgm2ram((char*)sLCDText, " "); sLCDUpdate(); DelayMs(1000); sLCDErase(); sLCDWriteLine(1,count);
24
Bootloader Process Check Push Button 1 for a Bootload Request
If the Push Button is Depressed: Display “Bootloader Enabled” Wait for Push Button 1 to be Released then Start the Bootload Process Otherwise, Time Out and Decrement the Count Down Timer while (i < ) { // Check to see if the user wants to load new application if (BLIO_LoaderEnabled() == TRUE) strcpypgm2ram((char*)sLCDText," Bootloader Enabled "); //strcpypgm2ram((char*)sLCDText," "); sLCDUpdate(); while(BLIO_LoaderEnabled() == TRUE){} LoadApplication(); } i++; j--;
25
Bootloader Process BOOL BLIO_LoaderEnabled ( void ) {
TRISDbits.TRISD5 = 1; //BTN1 if (PORTDbits.RD5 == 0) DelayMs(10); if(PORTDbits.RD5 == 0) return 1; } else return 0;
26
Bootloader Process Make Sure microSD Interface to SPI1 is Disabled (MDD_InitIO()) Look for a mounted microSD Card If Not Found, Inform the User and Keep Looking for a Card or an Abort Command (BTN2 Depressed) MDD_InitIO(); // Loader main loop while (LoadingApplication) { if(BootMediumAttached == 0) BootMediumInitialized = FALSE; strcpypgm2ram((char*)sLCDText, " microSD Card Not Found "); //strcpypgm2ram((char*)sLCDText, " "); sLCDUpdate(); do{ BootMediumAttached = BLMedia_MediumAttached(); if (BLIO_AbortLoad()) Reset(); } }while(BootMediumAttached == 0);
27
Bootloader Process Perform a Media Detect (BootMediumAttached)
If Media Detect is TRUE, Perform a Media Initialize Check If Media is Initialized, Set State Machine to FILE_FETCH and Clear the Error Flag (BootImageFileError) if (BootMediumAttached) { // The boot media is attached. //Make sure the boot meia is also initialized if (BootMediumInitialized) //Media is attached and initialized BootImageFileError = FALSE; state = FILE_FETCH;
28
Bootloader Process Media Not Mounted
Inform the User and Start the Process Again Loop Until a microSD Card is Mounted or an Abort Command is Issued (BTN2 Depressed) else { //Media was not mounted BootMediumInitialized = FALSE; strcpypgm2ram((char*)sLCDText," microSD Card Not Found "); //strcpypgm2ram((char*)sLCDText," "); sLCDUpdate(); }
29
Bootloader Process Use FindFirstpgm to Find User Generated .HEX File
Stuff the Filename into fileList Array and Increment filesCounter while (BootImageFileError == FALSE) { switch (state) case FILE_FETCH: status = FindFirstpgm("*.HEX", ATTR_ARCHIVE | ATTR_READ_ONLY | ATTR_HIDDEN, &searchRecord); if (status == 0) // If a file was found, add it to the file list strcpy((char*)&fileList[filesCounter*sizeof(searchRecord.filename)], (const char*)&searchRecord.filename); filesCounter ++;
30
Bootloader Process Display the Filename if (filesCounter != 0) {
// User .HEX file found state = SCREEN_UPDATE; else BootImageFileError = TRUE; }
31
Bootloader Process case SCREEN_UPDATE: { sLCDErase(); sLCDUpdate();
sprintf(sLineBuf,"Loading"); sLCDWriteLine(1,sLineBuf); sLCDWriteLine(2,searchRecord.filename); state = LOAD_IT; break; }
32
Bootloader Process // Read the boot image file and program it to Flash
if (BLMedia_LoadFile(searchRecord.filename)) { LoadingApplication = FALSE; sLCDErase(); sLCDUpdate(); sprintf(sLineBuf,"Launching"); sLCDWriteLine(1,sLineBuf); sLCDWriteLine(2,searchRecord.filename); DelayMs(2000); BootApplication(); state = FILE_FETCH; } else BootImageFileError = TRUE;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.