How to write programs?

Discussion around product based on ARM Cortex M3 core.

Moderators: nferre, ncollot

Freya
Posts: 58
Joined: Mon Jun 11, 2012 11:57 am

How to write programs?

Wed Feb 04, 2015 10:40 am

Hi to all,
every time I write a program, I have strange behavior such interrupt not working after lighting a led, or a crash after writing a variable. My code seems to be right, so I would check the whole program structure to investigate if problem may reside in there.

I use the following schema:

GENERAL FILES
- device.h: only IC registers definitions
- newtype.h: I define structures to store data to transfer to other devices (usually pc)
- global.h: where global variables and classes are defined and initialized
- global.cpp: for global subroutines (delay(), AsciiToDec(), ......)
- init.cpp: where IC and LCD are initialized
- interrupt.cpp: only interrupt routines (UART0_Handler(), PIOA_Handler(), ...)
- acquire.cpp: this is the main loop where all sensors data are collected and transmitted to pc
- main.h and main.cpp: just init the board and call the acquire routines

CLASS FILES
Here there are classes files where used pins are defined and initialized, such as class variables
- IOclass.h and IOclass.cpp: uses UART0 and exchange data with pc
- LedClass.h and LedClass.cpp: switches LED's on or off
- SpiClass.h and SpiClass.cpp
- DisplayClass.h and DisplayClass.cpp: uses spi class
- SDClass.h and SDClass.cpp: uses spi class
- ADCClass.h and ADCClass.cpp
- ..........................


When I need to use some variables or classes in two or more files, I define them in Global.h and in the other files I declare them as extern.
Compiler found no errors, but the program is not working, so I have some questions:
- Do I use too much files?
- Is it possible compiler uses the same space for more than one variable in optimization phase?
- Maybe the use of "extern" confuses the compiler?
- More or less I need 17KB and stack is 16KB should remain free about 12KB. May this be a problem? (I use a sam3s4b)

Which is the structure of your programs?
Do I do something wrong?
Where should I check problems?

Thank you
Freya
Freya
Posts: 58
Joined: Mon Jun 11, 2012 11:57 am

Re: How to write programs?

Mon Feb 09, 2015 6:04 pm

Hi to all,
I feel myself stupid, but I cannot understand this one.
I use an 160*104 pixel display and the 104 vertical pixels uses 2bits per pixel, therefore I need 26 bytes per column.
In other words I use the following:

Code: Select all

 uint8_t dispp[160][26] 
to represent display.

My code is:

GLOBAL.H

Code: Select all

#include "LedClass.h"
#include "SPIClass.h"
#include "DisplayClass.h"

MYLED myled;
MYSPI myspi;
MYDS mydisp(&myspi);

INIT.H

Code: Select all

#include "sam3s4b.h"
#include "DisplayClass.h"

extern MYDS mydisp;

void DisplayInit()
{
	mydisp.DsClear();
//	mydisp.DrawCircle(80,50,40,3);
//	mydisp.DrawLine(0,50,160,50,3);
//	mydisp.DrawLine(80, 0, 80, 104, 3);
//	mydisp.DrawLine(0, 0, 160, 104, 3);
//	mydisp.Display();
//	mdelay(2000);
	return;
}

LCD.H (main program header)

Code: Select all

#include "sam3s4b.h"
#include "LedClass.h"


extern MYLED myled;
extern void mdelay(int);
extern void DisplayInit();

LCD.CPP (main program)

Code: Select all

#include "LCD.h"

 extern uint8_t dispp[161][27];



int main(void)
{
	uint8_t i, j;
	
    /* Initialize the SAM system */
	ClockInit();
	MATRIX->CCFG_SYSIO = 0xF0;	// PB7 | PB6 | PB5 |PB4 ( SD0_CS | LED3 | LED4 | SD0_!CD)
	
	/* Main loop */
	while (1)
	{
		myled.SetPinLow(LED0);	// set Led in "start" configuration
		myled.SetPinHigh(LED1);	// set Led in "start" configuration
		
//		DisplayInit();
	for(j=0; j<26; j++)
	{
		for(i=0; i<160; i++) dispp[i][j] = 3;
	}
		
		mdelay(1000);
		myled.SetPinHigh(LED0);	// set Led in "end" configuration
		myled.SetPinLow(LED1);	// set Led in "end" configuration
		mdelay(500);
		
			
	}
}

CISPLAYCLASS.CPP

Code: Select all

uint8_t dispp[161][27];

void MYDS::DsClear()
{
	uint8_t i, j;
	
	for(j=0; j<26; j++)
	{
		for(i=0; i<160; i++) dispp[i][j] = 3;
	}
	return;
}
Now, If I uncomment DisplayInit() in main() program crashes, otherwise it works correctly.
Array dispp[][] is the same and also the clear routine is the same, so why in main() it works and in DisplayClass not?

Thank you
Freya
Posts: 58
Joined: Mon Jun 11, 2012 11:57 am

Re: How to write programs?

Wed Feb 18, 2015 4:26 pm

Hi to all,
I am still analyzing the problem (when I initialize a 2-dimensional array the subroutine complete its task, but when it returns the program crashes) and I need some hints.
All functions both stand alone or class funztions are in flash memory, so it is no possible code corruption.
In RAM there are class variables and global variables and class variables are before global variables (before means lower addresses).
Initialization routine remain within its boundaries
Is it possible variables area interferes with stack pointer or program counter?
How can I read stack?
How stack grow? (eg fi stack is xxxx, the next address is xxxx+1 or xxxx-1?)

Thank you
Freya
Freya
Posts: 58
Joined: Mon Jun 11, 2012 11:57 am

Re: How to write programs?

Sat Feb 21, 2015 12:44 pm

Yes I know, but the problem is another one.
Compiler assign an address to all variables and display area has a fiexed one.
The question is: why I found data that shouldn't be modified in the area reserved to another variable?
Who writes in display area except me?
Is it stack? Then, how can I read stack pointer?

Thank you

Return to “SAM3 Cortex-M3 MCU”

Who is online

Users browsing this forum: No registered users and 3 guests