From: SourceForge.net <no...@so...> - 2006-11-02 04:38:36
|
Bugs item #1589054, was opened at 2006-11-01 20:26 Message generated for change (Tracker Item Submitted) made by Item Submitter You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=100599&aid=1589054&group_id=599 Please note that this message will contain a full copy of the comment thread, including the initial issue submission, for this request, not just the latest update. Category: None Group: None Status: Open Resolution: None Priority: 5 Private: No Submitted By: Nobody/Anonymous (nobody) Assigned to: Nobody/Anonymous (nobody) Summary: Segmentation fault? Initial Comment: When I try to compile this FFT implementation I wrote for the PIC (not complete yet) I get what I assume is a seg fault (signal 11: SIGSEGV) (At least everything I've read online says it's a segfault.) The fault has been isolated to using TRISB in (the second) main(). I did have that in the port_config() function, but it errors there too. If TRISB is taken out it compiles fine. I have two mains setup because I wanted to run it on a standard computer to see the output (for algorithim testing purposes). When PIC is defined, it's SDCC, when not, it's windows. E-mail: amishandroid at gmail dot com sentinelzx at hotmail dot com CPU Specs: KDS ThinNote PIII 600 MHz 128 MB Ram Windows XP SDCC Specs: C:\...>sdcc -v SDCC : mcs51/gbz80/z80/avr/ds390/pic16/pic14/TININative/xa51/ds400/hc08 2.6.0 #4309 (Jul 28 2006) (MINGW32) Command and resulting error: C:\...>sdcc -mpic14 -p16f873a fft.c Caught signal 11: SIGSEGV With the offending file (fft.c): /* * FFT Implementation * 2005-2006 * Paul Schnapp */ #define PIC /* */ #ifndef PIC #include <stdio.h> #include <stdlib.h> unsigned char PORTB; unsigned char SSPBUF; unsigned char SSPIF; unsigned char TRISB; #else #include <pic16f873a.h> #endif #define N 0x10 /* Number of points of FFT */ #define NMASK 0x0F /* Bit mask for N */ #define NDIV2 0x08 /* Number of points divided by 2*/ #define MAX_BAND_VAL 0x1F /* Max value for band */ void port_config(); void fft(); void scale(); void zeroes(); void display(); void bandout(unsigned char band); void digitout(unsigned char digit); void decimate(); void butterfly(unsigned char indx1, unsigned char indx2); void mulreim(unsigned char indx1, unsigned char indx2); unsigned char sqrti(unsigned short x); /**** ****/ /**** Tables ****/ /**** ****/ /* 0 1 2 3 4 5 6 7 8 9 A B C D E F*/ unsigned char bit_rev_tbl[N] = {0x0,0x8,0x4,0xC,0x2,0xA,0x6,0xE,0x1,0x9,0x5,0xD,0x3,0xB,0x7,0xF}; unsigned char bit_inv_rev[2] = {0x1,0x2}; unsigned char bit_rev[2] = {0x3,0x5}; unsigned char bfly_indx[NDIV2]; /* * Data for the segment display from binary number * eg: * Binary | Segments * 00011 -> 00111 * 00101 -> 11111 */ unsigned char digit_seg[8] = {0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F}; char Wre[NDIV2] = {127,117,90,49,0,-49,-90,-117}; char Wim[NDIV2] = {0,-49,-90,-117,-127,-117,-90,-49}; /**** ****/ /**** Channels ****/ /**** ****/ char lchre[N]= /*{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; {5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5}; {5,0,-5,0,5,0,-5,0,5,0,-5,0,5,0,-5,0}; {30,0,-30,0,30,0,-30,0,30,0,-30,0,30,0,-30,0}; {15,10,5,10,15,10,5,10,15,10,5,10,15,10,5,10}; */{5,15,5,15,5,15,5,15,5,15,5,15,5,15,5,15};/* {5,-5,5,-5,5,-5,5,-5,5,-5,5,-5,5,-5,5,-5}; {30,-30,30,-30,30,-30,30,-30,30,-30,30,-30,30,-30,30,-30}; */ char lchim[N] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; char rchre[N]; char rchim[N] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; char *curchre; char *curchim; /**** ****/ /**** Chip Addressing ****/ /**** ****/ unsigned char digit_addr = 0; unsigned char chip_select = 0; #ifndef PIC /**** ****/ /**** NON-PIC ****/ /**** ****/ void output(void) { int i; for (i = 0; i < N; i++) { printf("%d ", curchre[i]); } printf("\n\n"); } int main(void) { curchre = lchre; curchim = lchim; fft(); scale(); output(); system("pause"); return 0; } #else /**** ****/ /**** PIC Main ****/ /**** ****/ void main(void) { PORTB = 0x00; TRISB = 0x00; port_config(); /* analog(); */ curchre = lchre; curchim = lchim; fft(); scale(); display(); curchre = rchre; curchim = rchim; fft(); scale(); display(); } #endif void port_config() { } void fft() { unsigned char count = 0; unsigned char cntup = 0; unsigned char indx_offset = 1; unsigned char indx_update = 3; decimate(); /* first layer butterfly, setup indexes for later butterflys */ while (count < N) { unsigned char indx1 = count; unsigned char indx2 = count + indx_offset; bfly_indx[cntup] = count; butterfly(indx1, indx2); count+=2; cntup++; } /* remaining sets with pre multiplication */ count = 2; while (count < N) { unsigned char Wtbl_indx = 0; count = count << 1; indx_offset = indx_offset << 1; for (cntup = 0; cntup < NDIV2; cntup++) /* perform set butterfly */ { unsigned char indx1, indx2; if (bfly_indx[cntup]&indx_update) bfly_indx[cntup] ^= indx_update; indx1 = bfly_indx[cntup]; indx2 = indx1 + indx_offset; mulreim(indx2, Wtbl_indx); butterfly(indx1, indx2); Wtbl_indx++; if (Wtbl_indx < indx_offset) { if (indx_offset==2) Wtbl_indx++; } else { Wtbl_indx = 0; } } indx_update = indx_update << 1; } } void scale() { unsigned char i; for (i = 0; i < N; i++) /* Shift by 3 to scale to 32 from 256 */ { unsigned short re = curchre[i] * curchre[i]; unsigned short im = curchim[i] * curchim[i]; curchre[i] = sqrti(re + im) >> 3; curchre[i] = curchre[i] > MAX_BAND_VAL ? MAX_BAND_VAL : curchre[i]; } } void zeroes() { unsigned char i; for (i = 0; i < N; i++) curchim[i] = 0; } void display() { } /**** ****/ /**** MSSP Output ****/ /**** ****/ /* * Bar Diagram: * |-- 1 digit ---||-- 1 digit ---||--------------||----------- * {}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{}{} * * The lowest 3 bits in the data byte are how many segments are filled * in the highest digit addressed. The 4th and 5th bits are how many * digits preceed the highest digit addressed. * * eg: 00010110 -> 000 10 110 * * 10 - there are two filled digits * 110 - there are 6 active segments in the thrid digit * * So, in this case the first two digits will be fully on, and * the next digit will have 6 segments on: * * |------------- 10 -------------||---- 110 -----| * |-- 1 digit ---||-- 1 digit ---||--------------||----------- * [][][][][][][][][][][][][][][][][][][][][][]{}{}{}{}{}{}{}{} * * {} = off LED * [] = on LED */ void bandout(unsigned char band) { unsigned char hi_digit = band & 0x07; unsigned char lo_digits = band >> 3; unsigned char digit_cnt = 4; unsigned char out = 0xFF; while (lo_digits) { digitout(out); lo_digits--; digit_cnt--; } out = hi_digit; while (digit_cnt) { digitout(out); out = 0; digit_cnt--; } } void digitout(unsigned char digit) { SSPBUF = ++digit_addr; /* Wait for transfer */ while (!SSPIF); SSPIF = 0; SSPBUF = digit; /* Wait for transfer */ while (!SSPIF); SSPIF = 0; /* Load */ PORTB = chip_select; PORTB = 0; } void decimate() { char temp; unsigned char i; for (i = 0; i < 2; i++) { unsigned char i2 = bit_inv_rev[i]; unsigned char i1 = bit_rev_tbl[i2]; temp = curchre[i1]; curchre[i1] = curchre[i2]; curchre[i2] = temp; i1 = (~i1)&NMASK; i2 = (~i2)&NMASK; temp = curchre[i1]; curchre[i1] = curchre[i2]; curchre[i2] = temp; i2 = bit_rev[i]; i1 = bit_rev_tbl[i2]; temp = curchre[i1]; curchre[i1] = curchre[i2]; curchre[i2] = temp; } } void butterfly(unsigned char indx1, unsigned char indx2) { char are = curchre[indx1]; char aim = curchim[indx1]; char bre = curchre[indx2]; char bim = curchim[indx2]; curchre[indx1] = are + bre; curchim[indx1] = aim + bim; curchre[indx2] = are - bre; curchim[indx2] = aim - bim; } /**** ****/ /**** Complex Multiplication ****/ /**** ****/ /* * x = a + ib, y = c + id * x*y = (ac - bd) + i(ad + bc) */ void mulreim(unsigned char indx1, unsigned char indx2) { char a = curchre[indx1]; char b = curchim[indx1]; char c = Wre[indx2]; char d = Wim[indx2]; short ta = (a*c-b*d)>>7; short tb = (a*d+b*c)>>7; curchre[indx1] = ta; curchim[indx1] = tb; } /**** ****/ /**** Square Root ****/ /**** ****/ /* * Genius function for integer square root from: * http://www.pedrofreire.com/sqrt * * x is arg to take root of, r is root, with x remainder */ unsigned char sqrti(unsigned short x) { unsigned char r; x = (x + 1) >> 1; /* init x */ for(r = 0; x > r; x -= r++); return r; } ---------------------------------------------------------------------- You can respond by visiting: https://sourceforge.net/tracker/?func=detail&atid=100599&aid=1589054&group_id=599 |