/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/bootloader887/bloader.PJT
0,0 → 1,58
[Directories]
Include=C:\Program Files (x86)\PICC\Devices;C:\Program Files (x86)\PICC\Drivers;
Library=
LinkerScript=
[defines]
enabled=0
D1=
V1=
[fileformats]
forcecoff=0
debug=4
error=2
object=0
list=0
scratch=0
sta=1
tre=1
allerrors=1
allwarnings=1
stayupne=3
stayupoe=0
[PROJECT]
Target=bloader.hex
Development_Mode=
Processor=0x887F
Processor_Text=PIC16F887
ToolSuite=CCS
[Target Data]
BuildTool=C-COMPILER
OptionString=+FM
AdditionalOptionString=
BuildRequired=1
FileList=D:\MLAB\Designs\Measuring_instruments\AWS01A\SW\PIC16F887\bootloader887\bloader.c
[bloader.c]
Type=4
Path=
FileList=
BuildTool=
OptionString=
AdditionalOptionString=
[mru-list]
1=bloader.c
[Windows]
0=0000 %S 0 0 796 451 3 0
[Opened Files]
1=bloader.c
2=..\common\bloader_defs.h
3=bloader.h
4=C:\Program Files\PICC\devices\16F887.h
5=C:\Program Files\PICC\drivers\string.h
6=C:\Program Files\PICC\drivers\stddef.h
7=C:\Program Files\PICC\drivers\ctype.h
8=
9=
10=
[Units]
Count=1
1=bloader (main)
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/bootloader887/bloader.c
0,0 → 1,302
/**** BootLoader for PIC16F887
 
Ussage:
ascii-xfr -s -v -l 110 ./bltest.hex > /dev/ttyUSB0
ascii-xfr is part of 'minicom' package
 
Add "uf\n\r" to the first line of .HEX. or use this script:
echo uf > /dev/ttyUSB$1
ascii-xfr -s -v -l 100 ./bltest.hex > /dev/ttyUSB$1
Or add "uf\n\r" and add some dummy characters at the end or begin of each line (for 100 ms delay) and use:
cp ./bltest.hex > /dev/ttyUSB0
For adding characters you can use this:
sed -i 's/^/pppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppp/' ./bltest.hex
sed -i 1i"uf" ./bltest.hex
Windows script:
mode com1 baud=9600 parity=n data=8 stop=1 rts=off dtr=off xon=off
echo uf > com1
ping 1.1.1.1 -n 1 -w 100 > nul
FOR /F "tokens=1*" %%i in (bltest.hex) do @echo %%i > com1 & ping 1.1.1.1 -n 1 -w 100 > nul
*/
 
#define ID "$Id: bloader.c 2898 2013-04-09 22:15:55Z kakl $"
 
#CASE // Case sensitive compiler
 
#define ERR_BUFFER_OVERRUN 1 // Error 1 - Buffer Overrun
#define ERR_CHECKSUM 2 // Error 2 - Bad CheckSum
#define ERR_TOO_MANY_BYTES 3 // Error 3 - Too many bytes in one line
#define ERR_UNSUPORTED_LINETYPE 4 // Error 4 - Unsuported Line type
 
#define BUFFER_LEN_LOD 46 // Length of Working buffer for HEX
 
#include "..\common\bloader_defs.h"
#include "bloader.h"
#include <string.h>
 
#INT_RDA
void rs232_handler() // Test of interrupt
{
putchar(getc()); // Just echo for test
}
 
void welcome(void) // Welcome message
{
char REV[50]=ID; // Buffer for concatenate of a version string
 
if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
printf("\r\n\r\n# BLoader 887 (C) 2013 KAKL\r\n"); // Welcome message
printf("#%s\r\n",&REV[4]);
}
 
 
/*-------------------------------- MAIN --------------------------------------*/
#SEPARATE
void real_main() // Main of loaded program
{
int8 i=0;
 
i=rs232_errors; // Just for compiler pleasure (supress Warning)
welcome();
 
printf("# Reserved: %Lu\r\n", RESERVED_BLOCKS*FLASH_BLOCK_SIZE);
printf("# FLASH_ERASE_SIZE: %Lu\r\n",getenv("FLASH_ERASE_SIZE"));
printf("# FLASH_WRITE_SIZE: %Lu\r\n",getenv("FLASH_WRITE_SIZE"));
printf("# Boot Loader Test >>>\r\n# ");
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
while(TRUE)
{
printf("%u|",i++); // Do something
delay_ms(100);
}
}
 
 
/*------------------- BOOT LOADER --------------------------------------------*/
 
#BUILD(INTERRUPT=FLASH_BLOCK_SIZE) // Redirect Interrupt routine above first flash block
#ROM 0x0004={0,0,0,0,0,0,0,0,0,0,0,0} // 12x NOP from interrupt vector to interrupt routine
 
 
#ORG LOADER_RESERVED,LOADER_RESERVED+FLASH_BLOCK_SIZE-1 auto=0
#SEPARATE
void dummy_main() // Main on the fix position. It will be overwriten by downloaded program reset vector.
{
real_main();
}
 
#ORG LOADER_RESERVED+FLASH_BLOCK_SIZE,getenv("PROGRAM_MEMORY")-1 auto=0 default //Start of BootLoader
 
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS) //RS232 control routine for BootLoader
 
#SEPARATE
unsigned int8 atoi_b16(char *s) // Convert two hex characters to an int8
{
unsigned int8 result = 0;
int i;
 
for (i=0; i<2; i++,s++) {
if (*s >= 'A')
result = 16*result + (*s) - 'A' + 10;
else
result = 16*result + (*s) - '0';
}
 
return(result);
}
 
void assert(int1 Condition, int8 ErrorCode) // Send error number to the serial line
{
if(Condition)
{
putchar('e');
putchar(ErrorCode+'0');
reset_cpu();
}
}
 
void pause()
{
int16 timeout;
 
for(timeout=0; timeout<65535; timeout++); // Delay cca 300ms
}
 
#SEPARATE
void boot_loader() // Loads a new program
{
/*
:100240001F2999000A300C1E232999006430A4004C
10 = 16 bytes
0249 = address 0x0120 (0x240/2 because of words)
00 = data
...data...
4C = checksum
 
:00000001FF
00 = 0 bytes
0000 = address 0
01 = END
FF = checksum
 
http://cs.wikipedia.org/wiki/Intel_HEX
*/
int buffidx;
char buffer[BUFFER_LEN_LOD]; // Buffer for HEX line
 
int8 checksum, num_of_bytes, line_type; // Extracted values from HEX line
int16 l_addr,h_addr=0;
int16 addr; // Address of word in PIC
int32 next_addr; // Helper variable for for
 
int8 dataidx, i; // Buffer for program bytes and pointers
union program_data {
int8 i8[16];
int16 i16[8];
} data;
 
disable_interrupts(GLOBAL);
 
putchar('@'); //Start Erase
/*
//Erase program memory is not necessary.
{
int8 i;
for(i=0;i<32;i++)buffer[i]=0xFF;
}
for(addr=FLASH_BLOCK_SIZE;addr<LOADER_RESERVED+FLASH_BLOCK_SIZE;addr+=FLASH_BLOCK_SIZE)
{
write_program_memory(addr, &buffer[0], 32);
putchar('.');
restart_wdt();
}
*/
putchar('!'); //Erase completed
 
//---WDT
while(!kbhit()) restart_wdt(); //Wait for HEX
putc('\r'); putc('\n');
while(TRUE)
{
//---WDT
while (getc()!=':') restart_wdt(); // Only process data blocks that starts with ':'
putchar(':');
 
buffidx = 0; // Read into the buffer until CR is received or buffer is full
do
{
buffer[buffidx] = getc();
putc(buffer[buffidx]);
} while ( (buffer[buffidx] != '\r') && (buffer[buffidx] != '\n') && (buffer[buffidx] != ' ') && (++buffidx < BUFFER_LEN_LOD) );
assert(buffidx == BUFFER_LEN_LOD, ERR_BUFFER_OVERRUN); // Error 1 - Buffer Overrun
//---WDT
restart_wdt();
 
checksum = 0; // Sum the bytes to find the check sum value
for (i=0; i<(buffidx-1); i+=2)
{
checksum += atoi_b16 (&buffer[i]);
//!!! printf(".%x",checksum);
}
assert(checksum != 0, ERR_CHECKSUM); // Error 2 - Bad CheckSum
 
// Get the lower 16 bits of address
l_addr = make16(atoi_b16(&buffer[2]),atoi_b16(&buffer[4]));
 
line_type = atoi_b16 (&buffer[6]);
num_of_bytes = atoi_b16 (&buffer[0]);
assert (num_of_bytes > 16, ERR_TOO_MANY_BYTES); // Error 3 - Too many bytes in one line
 
addr = make32(h_addr,l_addr);
 
addr /= 2; // PIC16 uses word addresses
 
// If the line type is 1 then END
if (line_type == 1)
{
putchar('#');
reset_cpu();
}
 
assert (line_type != 0, ERR_UNSUPORTED_LINETYPE); // Error 4 - Unsuported Line type
 
{
// Read old program memory content
for (i=0,next_addr=addr;i<8;i++)
data.i16[i]=read_program_eeprom(next_addr++);
// Loops through all of the data and stores it in data
// The last 2 bytes are the check sum, hence buffidx-4
for (i=8,dataidx=0; i < (buffidx-3); i += 2)
data.i8[dataidx++]=atoi_b16(&buffer[i]);
 
if (addr == 0)
{
// Write 8 words to the Loader location (jump to the main())
addr=LOADER_RESERVED;
write_program_memory(addr, &data.i8[0], 16); // It works only with 16 !!!
putchar('%');
}
else
if ( (addr >= FLASH_BLOCK_SIZE) && (addr <= (LOADER_RESERVED-16)) ) // Do not overwrite BootLoader
{
// Write program
write_program_memory(addr, &data.i8[0], 16); // It works only with 16 !!!
putchar('$');
}
else putchar('.'); // Possibly there was prevented write to the location of BootLoader
putc('\r'); putc('\n');
 
//---WDT
restart_wdt();
}
}
}
 
 
void main()
{
int8 timeout;
 
disable_interrupts(GLOBAL);
setup_wdt(WDT_2304MS); // Setup Watch Dog
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_oscillator(OSC_8MHZ|OSC_INTRC);
 
for(timeout=0; timeout<255; timeout++) //cca 50s
{
if (kbhit())
if (getc()=='u') // Send "uf" for Update Firmware
{
putchar('*');
if (getc()=='f')
{
restart_wdt();
boot_loader(); // Update Firmware starter
}
}
else break;
putchar('u'); putchar('f'); putchar('?');
pause();
restart_wdt();
};
 
restart_wdt();
goto_address(LOADER_RESERVED); // Jump to the location where is the jump to the main
}
#ORG default // End of BootLoader
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/bootloader887/bloader.h
0,0 → 1,20
#include <16F887.h>
#device adc=10
 
#FUSES WDT // Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, NO CLK OUT
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V
 
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,ERRORS)
 
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/bootloader887/bloader.hex
0,0 → 1,202
:080000001F308A00602F000090
:10002000FF00030E8301A1000A08A0008A01040852
:10003000A2007708A3007808A4007908A5007A0830
:10004000A600831383128C308400801E29288C1A0A
:100050003A28220884002308F7002408F80025081D
:10006000F9002608FA0020088A00210E8300FF0EFE
:100070007F0E09008A110A123D288C1E3D2818089F
:10008000A7001A08F800A71C4728181218167808A5
:100090008316B90083120C1E4B2899008C128A110A
:1000A0000A1229280D050D0523104226EF30E432EF
:1000B0007210381C3710A82129103218B119A02548
:1000C000C125CC060A002310D232F332723B6532CE
:1000D0003A102526F5060A0023104626C129C82F06
:1000E0004529C129C52FD324DA223A102526F50641
:1000F0000A0023104626C129C82F5729492AC52F8F
:10010000D324DA223A102526F5060A002310C23736
:100110006F3A2026EF30E4327210D432733A201F47
:100120003E1F0D05231000015F08E1005E08E0009E
:100130006108FA006008840083137A188317800826
:100140000319A628E00A0319E10A98285E0860024C
:10015000F7006108FA005F08031C5F0FFA027708D6
:10016000F8000800831603178C170C140000000019
:1001700083120C087F390319F8280313DE000317D4
:100180000D080313DF0003170F080313E0005E08D8
:100190000C1EC82899005F0803178D000313600820
:1001A00003178F0083168C170C14000000008312B5
:1001B0000C0D0E0D7F390319F8280313DE00031709
:1001C0000D080313DF0003170F080313E0005E0898
:1001D0000C1EE82899005F0803178D0003136008C0
:1001E00003178F008D0A03198F0A0313B228031710
:1001F00003130800831603178C170C14000000006B
:1002000003182A2983120C087F390313AD00031742
:100210000D080313AE0003170F080313AF002D08DA
:100220000C1E102999002E0803178D0003132F08A8
:1002300003178F0083168C170C1400000000831224
:100240000313AC0B24292729831603172A294A29CB
:100250008316031783120C0D0E0D7F390313AD00A7
:1002600003170D080313AE0003170F080313AF00A5
:100270002D080C1E392999002E0803178D00031331
:100280002F0803178F008D0A03198F0A0310031319
:10029000AC0BFA280800B4010408B3003410831B27
:1002A00034142D0EF038AF00AF07E23EB000323EFE
:1002B000B2002D080F39B007B007B207E93EB10010
:1002C000B107B1072C0E0F39B107B207B10DB20D4E
:1002D000B209B20D2C080F39B207AF0D0730AE00CE
:1002E0000A30B207B103031C7129B107B003031C24
:1002F0007529B007AF03031C7929AF07AE03031CB0
:100300007D292E308400831307303305331384078F
:1003100032300402031933170008F700031D98292F
:10032000331B9829331AA029B319982920309B2907
:10033000B31533123030F70777080C1E9D2999004A
:10034000840A331F882908003008F8012F02031897
:10035000AC292F08F700B829F7010830B100AF0D1C
:10036000F70D300877020318F700F80DB10BAF292D
:100370000800AB012708AB002430AC004930AD00C9
:100380006430AE003A30AF002030B0006230B100CF
:100390006C30B2006F30B3006130B4006430B5002F
:1003A0006530B6007230B7002E30B8006330B90047
:1003B0002030BA003230BB003830BC003930BD00CC
:1003C0003830BE002030BF003230C0003030C100B5
:1003D0003130C2003330C3002D30C4003030C5008E
:1003E0003430C6002D30C7003030C8003930C90065
:1003F0002030CA003230CB00CC003A30CD00313052
:10040000CE003530CF003A30D0003530D100D200A8
:100410005A30D3002030D4006B30D5006130D60084
:100420006B30D7006C30D8002030D9002430DA008F
:10043000DB01DF012C30DE009420013078022C3EFD
:10044000840083130008243C031D302ADF012C3074
:10045000DE009420013078022C3E8400831380015A
:10046000523003178D0000308F000313B220233069
:100470000C1E382A99003030840083138008031939
:10048000542ADF010408DE005F10831B5F1400089C
:100490000C1E482A99005E08840083135F18831796
:1004A000840A0319850A3E2A0D300C1E552A99002C
:1004B0000A300C1E592A9900633003178D00003052
:1004C0008F0003100C300313AC00FA2010308400AE
:1004D0000330AD002030AC004B210D300C1E6E2AD5
:1004E00099000A300C1E722A99006C3003178D0097
:1004F00000308F00031014300313AC00FA201030CA
:100500008400AD012030AC004B210D300C1E862A3A
:1005100099000A300C1E8A2A9900793003178D0041
:1005200000308F00031014300313AC00FA20103099
:100530008400AD01AC004B210D300C1E9D2A9900AA
:100540000A300C1EA12A9900863003178D00003056
:100550008F000313B22083168C16C03083128B04D5
:100560002B08AB0AAC002C08AD001B30AE002D08E8
:10057000AF006430B000A4217708AD0078083030B7
:10058000031DC92AAE1CD12AAE19D12A2E1A203039
:10059000CC2AAE112E122E14F80778080C1ECE2A83
:1005A00099002D08AF000A30B000A4217708AD00F3
:1005B00078083030031DE22AAE19E72A2E1CE72AFC
:1005C0002E1A2030F80778080C1EE42A99003030E3
:1005D000AD072D080C1EEA2A99007C300C1EEE2A6D
:1005E00099006430AC002C30840083130008031998
:1005F000062B0230F800F701F70BFC2AF80BFB2A58
:100600009730F700F70B022B800BF92AB02A8A15D6
:040610000A16E32CB7
:0C39C0000A128A11B9290A168A1508009B
:1039E0008C1EF02C1808A7001A08F800A71CFA2C47
:1039F0001812181608008316B10803190F2D653028
:103A000083120C1E012D9900303083163207B3004B
:103A100083120C1E092D99008A0100288316831237
:103A200008008316B601B7013708013C031C452D79
:103A30003508FA003408840083137A1883170008C5
:103A4000403C0318322D360EB800F030B80534086B
:103A5000840083133518831700083807BF3E0A3ED9
:103A6000B6003F2D360EB800F030B805340884009B
:103A700083133518831700083807D03EB60037087F
:103A8000B70AB40A0319B50A142D3608F8008312D0
:103A900008000B08F8008B138316031755308D00B0
:103AA000AA308D008C14000000008C18552D0C11CC
:103AB0007808831203138B040800831603178C17EE
:103AC00083120D08033903197A2DF9000D08FC390A
:103AD0008D0083160C1483128D00000000008316E5
:103AE0000C1583120313492503178D0A03198F0A36
:103AF000F90B6D2D831603133008F9000310F90C30
:103B00000F30831203170D05031D902D83160C161D
:103B10000C15831203134925831603170C12831205
:103B200000088C00840A00088E00840A83160C1595
:103B300083120313492503178D0A03198F0AF90B02
:103B4000A22DA52D83160313802D0D080739031907
:103B5000B92D043CF900FF308C008E0083160C1543
:103B600083120313492503178D0A03198F0AF90BD2
:103B7000AB2D03130800E001DF010B138B138B1B2C
:103B8000BE2D40300C1EC22D990021300C1EC62DBA
:103B900099008C1ACD2D6400C92D0D300C1ECE2D30
:103BA00099000A300C1ED22D9900F02478083A3C76
:103BB0000319DC2D6400D52D3A300C1EDD2D990043
:103BC000AB012C302B0784008313EC010408EB00BD
:103BD0006C10831B6C14F0246B08840083136C1826
:103BE0008317780880002C302B078400831300088B
:103BF000E90069080C1EFA2D99002C302B0784006F
:103C0000831300080D3C03191A2E2C302B07840057
:103C1000831300080A3C03191A2E2C302B0784004A
:103C200083130008203C03191A2EAB0A2B082D3CE5
:103C30000318E12D2B082E3C0319202E0030212ED5
:103C40000130E9008316B1000130B2008312FB2479
:103C50006400DA01E80101302B0268020318442EE7
:103C60002C306807E900EA010318EA0A6A0883169B
:103C7000B500831269088316B400831211257808F1
:103C8000DA070230E8072B2EDA08031D492E003030
:103C90004A2E0130E9008316B1000230B2008312CF
:103CA000FB248316B5012E30B40083121125780849
:103CB000E9008316B5013030B4008312112569087C
:103CC000DE007808DD008316B5013230B4008312BF
:103CD00011257808DC008316B5012C30B40083125E
:103CE00011257808DB005B08103C031C792E00309E
:103CF0007A2E0130E9008316B1000330B20083123E
:103D0000FB245D08E1005E08E2000310E20CE10C18
:103D10005C0B902E23300C1E8B2E99008A010028FC
:103D2000DC08031D952E0030962E0130E900831625
:103D3000B1000430B2008312FB24E801E601E50182
:103D40006208E4006108E3006808073C031CD42E05
:103D50000310680DA03E840083136608FA0065080E
:103D6000F9006408F8006308F7000130E30703185E
:103D7000E40A0319E50A0319E60A7A08EE0077084F
:103D800003178D000313780803178F0083168C1711
:103D90000C140000000083120C088000840A0E0836
:103DA00080000313E80AA42E0830E800E70103307E
:103DB0002B0268020318002F6708E70AA03EF800EC
:103DC000FA010318FA0A7808E9007A08EA002C30A8
:103DD0006807EB00EC010318EC0A6C088316B500C9
:103DE00083126B088316B4008312112569088400BE
:103DF00083136A188317780880000230E807D72EEB
:103E0000E108031D1F2FE208031D1F2F1C30E200D5
:103E1000E030E100620803178F0003136108031705
:103E20008D00A03084008313103083160313B0007C
:103E300083125D2525300C1E1B2F9900492FE208A7
:103E4000031D262F61080F3C0318452F62081C3CF8
:103E5000031C452F031D302F6108D03C031C452F48
:103E6000620803178F000313610803178D00A03049
:103E700084008313103083160313B00083125D2572
:103E800024300C1E412F9900492F2E300C1E462F36
:103E900099000D300C1E4A2F99000A300C1E4E2F2F
:103EA00099006400D52D0800AC01AB012B0F5B2FEE
:103EB0002C0F5B2F5F2FAB0A0319AC0A562F08009B
:103EC000840183131F308305713083168F000F0820
:103ED000031787110C3003139900A230980090301B
:103EE000831298008316031787110C30031399006F
:103EF000A2309800903083129800831603170908A7
:103F0000C039890003131F129F1200300317880065
:103F100083128701880189010313A701A801A90160
:103F20000B138B138B1B912F0930031785000F3058
:103F3000F700031381018130840083130008F039F6
:103F40000738800064000008F739F719F039770462
:103F50008000831603170908C039890003131F1254
:103F60009F12003003178800831203131F1083165B
:103F70000108C73908388100831290010030F80029
:103F80009200003083169200831203178701880184
:103F9000890183160313970171308F000F08831274
:103FA000AA012A0FD42FF82F8C1EE82FF0247808AE
:103FB000753C031DE72F2A300C1EDC2F9900F024DE
:103FC0007808663C031DE62F6400BB25E82FF82F18
:103FD00075300C1EE92F990066300C1EED2F9900EC
:103FE0003F300C1EF12F990054276400AA0AD12FEC
:0C3FF00064001C308A00E0308200630096
:1000080000000000000000000000000000000000E8
:080018000000000000000000E0
:04400E00FC2CFF3F48
:00000001FF
;PIC16F887
;CRC=A6FF CREATED="13-IV-13 18:54"
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/common/bloader_defs.h
0,0 → 1,7
/*------------------- BOOT LOADER Common Definitions --------------------------------------------*/
 
#define FLASH_BLOCK_SIZE getenv("FLASH_ERASE_SIZE")/2 // Minimal length of Flash Block Size
#define RESERVED_BLOCKS 50 // Number of reserved flash blocks for BootLoader
#define LOADER_RESERVED (getenv("PROGRAM_MEMORY")-(RESERVED_BLOCKS*FLASH_BLOCK_SIZE)) // begining of BootLoader
 
 
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/common/dbloader.h
0,0 → 1,26
/*------------------- DUMMY BOOT LOADER --------------------------------------------*/
#include "..\common\bloader_defs.h"
 
#BUILD(INTERRUPT=FLASH_BLOCK_SIZE) // Redirect Interrupt routine above first flash block
 
/*
#ORG 4,FLASH_BLOCK_SIZE-1
void JumpToTheInterrupt() // Jump to the Interrupt Handler
{ #asm
NOP
NOP
NOP
NOP
GOTO FLASH_BLOCK_SIZE
#endasm }
//#ORG 6,FLASH_BLOCK_SIZE-1 {} // First Flash block is reserved
*/
 
#ROM 0x0004={0,0,0,0,0,0,0,0,0,0,0,0} // 12x NOP from interrupt vector to interrupt routine
 
#ORG LOADER_RESERVED,getenv("PROGRAM_MEMORY")-1 auto=0
#SEPARATE
void dummy_main() // Main on the fix position
{
reset_cpu();
}
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/i2c_wind_sensor/README.cs.txt
0,0 → 1,8
I2C snimac vycitajici rychlost větru a srážky.
 
 
===== Měření srážek =====
 
V MCU je čítač použitý pro počítání překlopení člunku ve srážkoměru. Je ošetřeno jeho přetečení tak, aby mohl čítat z hlediska srážek až do velkých čísel. Tento čítač se nenuluje, ale nechává se přetéct. K němu paralelně běží uptimo hodiny v MCU. A MCU se chová jako I²C SLAVE a hodnotu čítače tak lze vyčíst přes I2C.
MCU pak ve zbytku výpočetního času kontroluje hodnotu čítače a do dalšího registu zapisuje čas jeho poslední změny. (podobne jako dmesg, ale zaokrouhleno na cele sekundy)
Uptime bude zaznamenavan do registru, ktere je mozne precist pres I2C. Tento registr může být použit jako zdroj korekce v případě komplikací s nastavením aktuálního času.
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/i2c_wind_sensor/main.c
0,0 → 1,157
#define VERSION "0.1"
#define ID "$Id: main.c 2916 2013-04-14 17:42:03Z kaklik $"
 
#include "main.h"
#use i2c(SLAVE,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,address=0xA2) // Motor 2
 
const int8 buf_len=8;
 
int8 buffer[buf_len]; // I2C buffer
 
int8 address=0;
 
#include "..\common\dbloader.h"
 
unsigned int16 timer0_overflow_count;
unsigned int16 anemo=0;
unsigned int16 rain=0;
 
//we are using the rtctimer.c library, in which a counter is incremented
//every time the timer2 interrupt occurs (timer2 overflow). the time math
//needs to know what rate the timer2 interrupt occurs. this definition
//must match the rate the timer2 is configured for.
#define CLOCKS_PER_SECOND 1000
 
#INT_SSP
void ssp_interupt ()
{
BYTE incoming, state;
 
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
if(state == 1) //First received byte is address
{
address = incoming;
if (incoming == 2)
{
buffer[0]=make8(anemo,0);
buffer[1]=make8(anemo,1);
buffer[2]=make8(rain,0);
buffer[3]=make8(rain,1);
}
}
if(state == 2) //Second received byte is data
buffer[address] = incoming;
}
if(state == 0x80) //Master is requesting data
{
if(address <= buf_len) i2c_write(buffer[address]);
else i2c_write(ID[address - buf_len]);
}
}
 
 
#int_TIMER0 //osetruje preteceni citace od anemometru (RA4)
void TIMER0_isr(void)
{
timer0_overflow_count++;
}
 
#int_TIMER1
void TIMER1_isr(void)
{
// 32.768 kHz krystal pro timer1 oscilátor
anemo = ((timer0_overflow_count * 0xFF) + get_timer0()); // pocet pulzu za 1s
 
timer0_overflow_count=0; //nulovani
set_timer0(0);
set_timer1(0);
output_toggle(PIN_E0);
}
 
#int_TIMER2
void TIMER2_isr(void)
{
 
}
 
#INT_EXT
void EXT_isr() //interrup from rain sensor clip.
{
rain++;
// if (input(PIN_B0)) ext_int_edge( H_TO_L ); osetreni pro pripad, ze by bylo treba cist obe hrany impulzu
// if (!input(PIN_B0)) ext_int_edge( L_TO_H );
}
 
 
void welcome(void) // uvodni zprava
{
printf("\r\n\r\n# Meteorologicka stanice %s (C) 2013 www.mlab.cz \r\n",VERSION);
printf("\r\n %s \r\n",ID);// Welcome message
printf("# ver poradi ");
printf("check\r\n\r\n");
}
 
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
// setup_adc(ADC_CLOCK_DIV_2);
setup_adc(ADC_OFF);
// setup_spi(SPI_SS_DISABLED); //must not be set if I2C are in use!
setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
// setup_timer_0(RTCC_INTERNAL);setup_wdt(WDT_144MS);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
// setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
// setup_oscillator(OSC_8MHZ|OSC_INTRC);
 
 
/* Setup timer 2
* On a 4 Mhz clock, this will trigger a timer2 interrupt every 1.0 ms
* For time.h to work properly, Timer2 must overflow every millisecond
* OverflowTime = 4 * (1/OscFrequency) * Prescale * Period * Postscale
* For 4 Mhz: .001 seconds = 4 * (1/4000000 seconds) * 4 * 250 * 1
*/
#if getenv("CLOCK")==4000000)
setup_timer_2(T2_DIV_BY_1,250,4);
#elif getenv("CLOCK")==20000000)
setup_timer_2(T2_DIV_BY_4,250,5);
#else
#error Configure TIMER2 so it interrupts at a rate defined by CLOCKS_PER_SECOND
#endif
 
enable_interrupts(INT_SSP);
enable_interrupts(INT_TIMER2);
enable_interrupts(INT_TIMER1);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
 
 
set_timer0(0);
set_timer1(0);
timer0_overflow_count=0;
 
buffer[2]=0;
buffer[3]=0;
buffer[4]=0;
buffer[5]=0;
 
welcome();
 
while(true)
{
printf("count: %X %X %X %X\r\n", buffer[0],buffer[1],buffer[2],buffer[3]);
printf("%Lu %Lu \n\r", anemo, rain);
delay_ms(1000);
 
}
}
 
 
 
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/i2c_wind_sensor/main.h
0,0 → 1,22
#include <16F887.h>
#device adc=8
 
//#FUSES WDT // Watch Dog Timer
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //external crystal oscillator
#FUSES NOPUT //No Power Up Timer
#FUSES MCLR //Master Clear pin enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOCPD //No EE protection
#FUSES NOBROWNOUT //No brownout reset
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOWRT //Program memory not write protected
#FUSES BORV40 //Brownout reset at 4.0V
 
#use delay(clock=20000000)
 
//set RS232
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
/Designs/Measuring_instruments/AWS02A/SW/PIC16F887/i2c_wind_sensor/main.pjt
0,0 → 1,40
[PROJECT]
Target=motor.HEX
Development_Mode=
Processor=0x688F
ToolSuite=CCS
 
[Directories]
Include=C:\Program Files\PICC\devices\;C:\Program Files\PICC\Dr
Library=
LinkerScript=
 
[Target Data]
FileList=C:\dokumenty\svn\Kaklik\roboti\Robotour\SW\motor\motor.c
BuildTool=C-COMPILER
OptionString=+FM
AdditionalOptionString=
BuildRequired=1
 
[motor.c]
Type=4
Path=
FileList=
BuildTool=
OptionString=
AdditionalOptionString=
 
[mru-list]
1=motor.c
 
[Windows]
0=0000 motor.c 0 0 796 451 3 0
 
[Opened Files]
1=D:\svn\svnrobozor\roboti\Robotour\SW\motor\motor.c
2=C:\Program Files\PICC\devices\16F88.h
3=
4=
[Units]
Count=1
1=C:\dokumenty\svn\Kaklik\roboti\Robotour\SW\motor\motor.c (main)
/Designs/Measuring_instruments/AWS02A/SW/Python/AWS_read.py
0,0 → 1,86
#!/usr/bin/python
 
# Python driver for MLAB MAG01A module with HMC5888L Magnetometer sensor wrapper class
 
import time
import datetime
import sys
 
from pymlab import config
 
#uncomment for debbug purposes
import logging
logging.basicConfig(level=logging.DEBUG)
 
 
#### Script Arguments ###############################################
 
if len(sys.argv) != 2:
sys.stderr.write("Invalid number of arguments.\n")
sys.stderr.write("Usage: %s #I2CPORT\n" % (sys.argv[0], ))
sys.exit(1)
 
port = eval(sys.argv[1])
 
 
#### Sensor Configuration ###########################################
 
cfg = config.Config(
i2c = {
"port": port,
},
 
bus = [
{
"type": "i2chub",
"address": 0x72,
"children": [
{
"type": "i2chub",
"address": 0x70,
"channel": 0,
"children": [
{"name": "barometer", "type": "altimet01" , "channel": 0, },
{"name": "hum_temp", "type": "sht25" , "channel": 1, },
# {"name": "wind_direction", "type": "mag01" , "channel": 1, },
# {"name": "thermometer", "type": "lts01" , "channel": 2, },
],
},
# {"name": "barometer2", "type": "altimet01" , "channel": 6, },
],
},
],
)
 
cfg.initialize()
 
barometer = cfg.get_device("barometer")
hum_temp = cfg.get_device("hum_temp")
#wind_direction = cfg.get_device("wind_direction")
#thermometer = cfg.get_device("thermometer")
time.sleep(0.5)
 
#### Data Logging ###################################################
 
filename = datetime.datetime.now().isoformat()
 
 
try:
with open(filename+".log", "a") as f:
while True:
barometer.route()
(t1, p1) = barometer.get_tp()
hum_temp.route()
t2 = hum_temp.get_temp()
# t3 = thermometer.get_temp()
t3 = -200
h1 = hum_temp.get_hum()
 
sys.stdout.write(" Temperature: %.2f Pressure: %d TempSHT: %.2f HumSHT: %.1f TempLTS: %.2f" % (t1, p1, t2, h1, t3))
f.write("%d\t%.2f\t%d\t%.2f\t%.1f\t%.2f\n" % (time.time(),t1, p1, t2, h1, t3))
sys.stdout.flush()
time.sleep(10)
except KeyboardInterrupt:
sys.exit(0)