  | 
	  | 
		 
	 
	
		| View previous topic :: View next topic   | 
	 
	
	
		| Author | 
		Message | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				| DSPIC33EP512GP502 -- PCD v5.115 Bootloader UART issue | 
			 
			
				 Posted: Thu Jul 04, 2024 3:56 am     | 
				     | 
			 
			
				
  | 
			 
			
				Hi,
 
 
I'm using this bootloader code:
 
 
 	  | Code: | 	 		  
 
//#define ROM_WRITE_CAN_MODIFY_CONFIG_BITS
 
#include <33EP512GP502.h>
 
#FUSES NOPROTECT                                                              // Allow a Programmer device (eg. Pickit) to read Microcontroller firmware
 
#FUSES NOIOL1WAY                                                              // Used to enable peripheral deselection (useful to reduce power-consumption )
 
 
#use delay(internal=20MHz,clock_out)
 
 
#define UART_2_TX           PIN_B11                                              // DsPIC UART2 TX
 
#define UART_2_RX           PIN_B12                                              // DsPIC UART2 RX
 
#PIN_SELECT U2TX=UART_2_TX
 
#PIN_SELECT U2RX=UART_2_RX
 
#use rs232(uart2,baud=115200,PARITY=N, BITS=8, ERRORS, RECEIVE_BUFFER=100, stream=SPECTRUM)
 
 
#define _bootloader
 
#define BOOTLOADER_MODE2X
 
#define BOOTLOADER_STREAM SPECTRUM
 
 
#include <pcd_bootloader.h>
 
#include <loader_pcd.c>
 
 
#org APPLICATION_START
 
void jump_to_application(void)
 
{
 
   #asm
 
   GOTO APPLICATION_START
 
   #endasm
 
}
 
 
 
void main(void)
 
{
 
   char ans='0';
 
   do 
 
   {
 
      ans=fgetc(SPECTRUM);
 
   }
 
   while(ans!='A'&&ans!='M'&&ans!='!');
 
   
 
   if(ans=='!')
 
   {
 
      delay_ms(140); // wait for PLL
 
      fputc('!',SPECTRUM);
 
      load_program();
 
   }
 
   else if(ans=='M')
 
   {
 
      fputc('M',SPECTRUM);
 
   }
 
   jump_to_application(); // Start Application
 
}
 
 
#int_default
 
void isr(void)
 
{
 
   jump_to_isr(LOADER_END+5);
 
}
 
 | 	  
 
 
In order to load this application:
 
 
 	  | Code: | 	 		  
 
/* 
 
 * File:   main.c
 
 * Author: Marco Croci M.C.
 
 *
 
 * Created on 01 August 2023, 9.00
 
 * Release M.C.
 
 */
 
 
//--------------------------CCSC Library--------------------------------------//
 
#include <33EP512GP502.h>
 
#include <pcd_bootloader.h>
 
#include <dsp_data_util.c>
 
//#include <pcd_traps.c>                                                      // Include for Debug: Modify TrapPutc(char c) using "fputc(c, SPECTRUM);" in both cases
 
 
//--------------------------3Bee Library--------------------------------------//
 
#include <Lib/main.h>
 
#include <Lib/fft_3Bee.h>
 
#include <Lib/MCP346.c>   
 
 
//-----------------------------ISRs Definitions-------------------------------//
 
 
//External ISR used to acquire data from ADC
 
#INT_EXT1                                                                 
 
void MCP346_manager()
 
{
 
   //Isr code...
 
}
 
 
//-----------------------------Foos Definitions-------------------------------//
 
 
//Every time it is switched on again for anything, I initialize//
 
void DsPic_Init()
 
{
 
   wk_cause = restart_cause();
 
   output_low(LED_ONBOARD);                                                   
 
   enable_interrupts(GLOBAL);
 
}
 
 
//SPECTRUM Driver Foos//
 
void SPECTRUM_Init()
 
{
 
   DsPic_Init();
 
   delay_ms(1000);
 
   MCP346_Restart();
 
   fft_init();
 
    output_high(LED_ONBOARD);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[0]=fgetc(SPECTRUM);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[1]=fgetc(SPECTRUM);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[2]=fgetc(SPECTRUM);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[3]=fgetc(SPECTRUM);
 
    while(!kbhit(SPECTRUM));
 
   THS_array[4]=fgetc(SPECTRUM);
 
    while(!kbhit(SPECTRUM));
 
   THS_array[5]=fgetc(SPECTRUM);
 
    while(!kbhit(SPECTRUM));
 
   THS_array[6]=fgetc(SPECTRUM);
 
    output_low(LED_ONBOARD);
 
}
 
//
 
[.....]
 
//
 
 
//****************************************************************************//
 
//                            Main Loop                                       //
 
//****************************************************************************//
 
void main(void) 
 
{
 
   SPECTRUM_Init();
 
 
   while(TRUE)
 
   {
 
        //Application stuff...
 
    }
 
}
 
 
 | 	  
 
 
I perform successfully hex file bootloader via uart (I checked in debug ACK and data flow and work fine) but I have a problem with UART when I reach this point:
 
 
 	  | Code: | 	 		  
 
output_high(LED_ONBOARD);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[0]=fgetc(SPECTRUM);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[1]=fgetc(SPECTRUM);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[2]=fgetc(SPECTRUM);
 
   while(!kbhit(SPECTRUM));
 
   THS_array[3]=fgetc(SPECTRUM);
 
    while(!kbhit(SPECTRUM));
 
   THS_array[4]=fgetc(SPECTRUM);
 
    while(!kbhit(SPECTRUM));
 
   THS_array[5]=fgetc(SPECTRUM);
 
    while(!kbhit(SPECTRUM));
 
   THS_array[6]=fgetc(SPECTRUM);
 
    output_low(LED_ONBOARD);
 
 | 	  
 
 
The led turns on, I send the bytes but the application stops in the first while like UART is not working anymore...
 
 
FUSES are the same (I checked LST files of bootloader and application):
 
 
 	  | Code: | 	 		  
 
Configuration Fuses:
 
   Word  3L: FFCF   ICSP1 NOJTAG NODEBUG
 
          H: 0000  
 
   Word  4L: FFFF   NOALTI2C1 NOALTI2C2 WDTWIN_25%
 
          H: 0000  
 
   Word  5L: FF7F   WPOSTS16 WPRES128 PLLWAIT WINDIS NOWDT
 
          H: 0000  
 
   Word  6L: FFDF   NOPR NOOSCIO NOIOL1WAY
 
          H: 0000  
 
   Word  7L: FFF9   FRC_PLL IESO
 
          H: 0000  
 
   Word  8L: FFFF   NOWRT NOPROTECT
 
          H: 0000  
 
   Word  9L: FFFF  
 
          H: 0000  
 
   Word 10L: FFFF  
 
          H: 0000  
 
 | 	  
 
 
Please help me!
 
 
regards,
 
 
Marco | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 4:04 am     | 
				     | 
			 
			
				
  | 
			 
			
				The application also includes main.h:
 
 
 	  | Code: | 	 		  
 
//   I define all the delays,variables,fuses,pins and costants                //                                                                                                                                                                                                                                             //La scheda รจ 3.3V o 3.6V?
 
#ifndef __MAIN_H
 
#define __MAIN_H
 
 
#FUSES NOPROTECT                                                              // Allow a Programmer device (eg. Pickit) to read Microcontroller firmware
 
#FUSES NOIOL1WAY                                                              // Used to enable peripheral deselection (useful to reduce power-consumption )
 
 
#use delay(internal=20MHz,clock_out) 
 
 
//-----------------------------Global Constants-------------------------------//
 
[...]
 
//      SPECTRUM Pins Definition                                                //
 
//      SPECTRUM DsPIC is Slave, Control Module PIC18F is Master                //
 
 
// Generic Pins
 
#define LED_ONBOARD         PIN_B4                                              // Led on Board
 
 
// ISRs
 
#define IRQ_INT1            PIN_B13                                              // Interrupt 1 ADC triggers ISR on DsPIC
 
#PIN_SELECT INT1=IRQ_INT1
 
 
// PIC18F(MASTER)-DsPIC(SLAVE) UART
 
#define UART_2_TX           PIN_B11                                              // DsPIC UART2 TX
 
#define UART_2_RX           PIN_B12                                              // DsPIC UART2 RX
 
#PIN_SELECT U2TX=UART_2_TX
 
#PIN_SELECT U2RX=UART_2_RX
 
#use rs232(uart2,baud=115200,PARITY=N, BITS=8, ERRORS, RECEIVE_BUFFER=100, stream=SPECTRUM)
 
 
// DsPIC-ADC SPI 
 
#define SPI1_PIN_SELECT     PIN_B6                                               // DsPIC-ADC SPI1 CHIP_SELECT
 
#use spi(MASTER,SPI1, BITS=8, MSB_FIRST, MODE=3, stream=SLAVE_ADC)
 
 
//-----------------------------Global Variables----------------------------------//
 
[....]
 
 
//-----------------------------Foos Prototypes-----------------------------------//
 
[....]
 
 
 
#endif
 
 | 	  
 
 
Moreover the UART issue persists both I start application suddenly after a bootloader reflash and also when I reprogram, turn-off DSPIC, turn-on, enter directly in application avoiding bootloader branch... | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 4:34 am     | 
				     | 
			 
			
				
  | 
			 
			
				I noted that 33EP512GP502.h file does not contain NOIOL1WAY and NOPROTECT fuses...
 
 
 	  | Code: | 	 		  
 
//////// Program memory: 175094x24  Data RAM: 49152  Stack: 31
 
//////// I/O: 21   Analog Pins: 6
 
//////// Fuses: ICSP3,ICSP2,ICSP1,JTAG,DEBUG,ALTI2C1,ALTI2C2,WDTWIN_75%
 
//////// Fuses: WDTWIN_50%,WDTWIN_37%,WDTWIN_25%,WPOSTS1,WPOSTS2,WPOSTS3
 
//////// Fuses: WPOSTS4,WPOSTS5,WPOSTS6,WPOSTS7,WPOSTS8,WPOSTS9,WPOSTS10
 
//////// Fuses: WPOSTS11,WPOSTS12,WPOSTS13,WPOSTS14,WPOSTS15,WPOSTS16
 
//////// Fuses: WPRES32,WPRES128,PLLWAIT,WINDIS,WDT,EC,XT,HS,NOPR,OSCIO
 
//////// Fuses: IOL1WAY,CKSFSM,CKSNOFSM,NOCKSNOFSM,FRC,FRC_PLL,PR,PR_PLL
 
//////// Fuses: LPRC,FRC_PS,IESO,WRT,PROTECT
 
//////// 
 
 | 	  
 
 
Is it a problem ?? | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 6:42 am     | 
				     | 
			 
			
				
  | 
			 
			
				No. 'NO', means 'turn off this fuse'. So the NOIOL1WAY setting turns off
 
IOL1WAY fuse. Same for PROTECT. The listing shows this.
 
 
Check your stack usage at the top of the listing. You don't give any idea of
 
how large the program is, but having to expand the stack is normal on
 
any code where a lot of maths, or I/O is done. I'd suspect you are
 
overflowing the stack. | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 6:57 am     | 
				     | 
			 
			
				
  | 
			 
			
				This is stack info from LST file:
 
 
 	  | Code: | 	 		  
 
CCS PCD C Compiler, Version 5.115, 84               03-lug-24 18:05
 
 
               Filename:   C:\Users\3Bee\MPLABXProjects\Hive_Tech_V1\build\default\production\main.lst
 
 
               ROM used:   6288 bytes (2%)
 
                           Largest free fragment is 65536
 
               RAM used:   13743 (28%) at main() level
 
                           13953 (28%) worst case
 
               Stack used: 82 locations (42 in main + 40 for interrupts)
 
               Stack size: 128
 
 | 	  
 
 
Is it a problem ? How can I fix this problem ?
 
I really need uart communication...
 
 
Regards,
 
 
Marco | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 7:22 am     | 
				     | 
			 
			
				
  | 
			 
			
				Maybe I have to define LOADER_PAGES ??
 
 
 	  | Code: | 	 		  
 
////  Depending on the PIC the bootloader is being built for or if     ////
 
////  changes are made to the code the bootloader may not fit in the   ////
 
////  program memory set a side for it by default for this example.    ////
 
////  If that happens the size set aside for the bootloader can be     ////
 
////  changed by defining LOADER_PAGES to the number of erase pages    ////
 
////  to set aside for it before the pcd_bootloader.h is included,     ////
 
////  this define must be made in both the bootloader and application  ////
 
////  programs for the bootloader to work correctly.                   ////
 
 | 	  
 
 
How can I compute the correct value ? | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 7:37 am     | 
				     | 
			 
			
				
  | 
			 
			
				Two parts.
 
First on the stack, expand this to at least 200 bytes. Say 256. Use #build
 
to set this. This is the main code. 
 
On the bootloader size, build the bootloader. Then look at it's size. You need
 
to set it's pages used to the next page size over your size. Your bootloader 
 
does not look much larger than standard, but it could be that it is going too
 
large. | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 7:44 am     | 
				     | 
			 
			
				
  | 
			 
			
				So If I understood correctly:
 
 
Flash erase size 1024 instructions (in PCD 3072 bytes) info from datasheet.
 
 
bootloader hex file properties tells me size is 12.746 bytes.
 
 
so Loader_Pages = 12746/3072 = 4.15 -> LOADER_PAGES = 5
 
 
is it correct ? Have I to define loader_pages both in bootloader and application code ? Where ?
 
 
How can I enlarge stack using build ? could you provide me an example ?
 
Where Have I to put this directive ? Both application and bootloader ? | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 8:02 am     | 
				     | 
			 
			
				
  | 
			 
			
				No. Look at the size at the top of the listing, not the hex file size. The latter
 
will be much bigger. That is huge for a bootloader. The standard PCD 
 
bootloader compiles to just over 2Kbytes. ROM used figure. 
 
You just define LOADER_PAGES before loading pcd_bootloader.h.
 
I'd suspect you will need 3 pages, so:
 
 	  | Code: | 	 		  
 
#define LOADER_PAGES 3
 
#include "pcd_bootloader.h"
 
 | 	 
  | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 8:07 am     | 
				     | 
			 
			
				
  | 
			 
			
				I have: 
 
 
 	  | Code: | 	 		  
 
CCS PCD C Compiler, Version 5.115, 84               04-lug-24 16:05
 
 
               Filename:   C:\Users\3Bee\MPLABXProjects\Hive_Tech_V1\build\default\production\main.lst
 
 
               ROM used:   2324 bytes (1%)
 
                           Largest free fragment is 65536
 
               RAM used:   4468 (9%) at main() level
 
                           4541 (9%) worst case
 
               Stack used: 80 locations (42 in main + 38 for interrupts)
 
               Stack size: 128
 
 | 	  
 
 
So LOADER_PAGES is ??
 
 
 
 	  | Code: | 	 		  
 
 
#define _bootloader
 
#define BOOTLOADER_MODE2X
 
#define BOOTLOADER_STREAM SPECTRUM
 
#define LOADER_PAGES 3
 
#include <pcd_bootloader.h>
 
#include <loader_pcd.c>
 
 
 | 	  
 
 
Is correct ??
  Last edited by Marco27293 on Thu Jul 04, 2024 8:09 am; edited 1 time in total | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 8:08 am     | 
				     | 
			 
			
				
  | 
			 
			
				Please, give me example in order to use #build and expand stack...
 
Do I need it only in application or also bootloader code ?
 
 
In application I have: 
 
 
 	  | Code: | 	 		  
 
#use delay(internal=20MHz,clock_out) 
 
#build(STACK=256)
 
 
// Fom lst:
 
Stack used: 82 locations (42 in main + 40 for interrupts)
 
Stack size: 256
 
 | 	  
 
 
I'm using LOADER_PAGES 5 (I also tried 3)....
 
 
The issue persists, UART2 seems blocked!! | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 9:37 am     | 
				     | 
			 
			
				
  | 
			 
			
				You do realise that if you specify a receive buffer for the UART, you must 
 
enable the receive interrupt for the UART (and GLOBAL)?. Won't work
 
otherwise.You don't show this in your init routine...... | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 9:39 am     | 
				     | 
			 
			
				
  | 
			 
			
				| When I load the application without bootloader part it works fine... | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Ttelmah
 
 
  Joined: 11 Mar 2010 Posts: 19967
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Thu Jul 04, 2024 11:27 am     | 
				     | 
			 
			
				
  | 
			 
			
				Change the UART setup in the bootloader to:
 
 	  | Code: | 	 		  
 
#use rs232(uart2,baud=115200,PARITY=N, BITS=8, ERRORS, stream=SPECTRUM) 
 
 | 	  
 
 
Having the buffer enabled here will completely b*884r things up. Having this
 
will create interrupt handler code in the bootloader. This will prevent the
 
bootloader's interrupt relocation code from working.
 
 
You had not said at any point that it was working without the bootloader...  
 
  | 
			 
		  | 
	 
	
		  | 
	 
	
		
			Marco27293
 
 
  Joined: 09 May 2020 Posts: 136
  
			
			 
			 
			
			
			
			
			
			
			
  
		  | 
		
			
				 | 
			 
			
				 Posted: Fri Jul 05, 2024 2:19 am     | 
				     | 
			 
			
				
  | 
			 
			
				OK. 
 
I removed receive_buffer from bootloader part.
 
 
Can I keep it in application part ? Or could it be a problem ? | 
			 
		  | 
	 
	
		  | 
	 
	
		 | 
	 
 
  
	 
	    
	   | 
	
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
  | 
   
 
  
Powered by phpBB © 2001, 2005 phpBB Group
  
		 |