[Sdcc-user] C8051 clock and timers
I have some trouble understanding the C8051 clock and timers. In the tutorial code at http://www.colecovision.eu/mcs51/C8051F340%20380%20LED.shtml I get a timer interrupt at 1000 Hz. But I got the values for configuring the timer experimentally, and can't seem to get them from the datasheet. According to the datsheet, at startup the internal oscillator is running at 24.5 Mhz (OSCICL reset value), then gets divided by 128 (OSCICN reset value) and then used for SYSCLK (CLKSEL rest value). Thus SYSCLK is 24.5 Mhz / 128 about 0.1914 Mhz. In the timer, this then gets divided by 12 (CKCON reset value) adn then sued as the frequency for incrementing the timer (TMOD value as in the tutorial code). Thus the timer increment frequency is 24.5 Mhz / 128 / 12 about 0.0159 Mhz. The tutorial code ssets the timer register to (65536 - 125) in the interrupt handler. Thus the timer interrupt frequency is 24.5 Mhz / 128 / 12 / 125 about 127 Hz. So from the datasheet I get 127 Hz. But on the hardware I see 1000 Hz. Philipp -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] C8051 clock and timers
On 28.08.2016 10:48, Philipp Klaus Krause wrote: > I have some trouble understanding the C8051 clock and timers. > In the tutorial code at > http://www.colecovision.eu/mcs51/C8051F340%20380%20LED.shtml I get a > timer interrupt at 1000 Hz. But I got the values for configuring the > timer experimentally, and can't seem to get them from the datasheet. > > According to the datsheet, at startup the internal oscillator is running > at 24.5 Mhz (OSCICL reset value), then gets divided by 128 (OSCICN reset > value) and then used for SYSCLK (CLKSEL rest value). > > Thus SYSCLK is 24.5 Mhz / 128 about 0.1914 Mhz. > > In the timer, this then gets divided by 12 (CKCON reset value) adn then > sued as the frequency for incrementing the timer (TMOD value as in the > tutorial code). > > Thus the timer increment frequency is 24.5 Mhz / 128 / 12 about 0.0159 Mhz. > > The tutorial code ssets the timer register to (65536 - 125) in the > interrupt handler. > > Thus the timer interrupt frequency is 24.5 Mhz / 128 / 12 / 125 about > 127 Hz. > > So from the datasheet I get 127 Hz. But on the hardware I see 1000 Hz. > > Philipp P.S.: I have verified that the OSCICN, CLKSEL and CKCON reset values are as in the datasheet. Initializing these registers explicitly to their reset value does not change anything. -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] C8051 clock and timers
> On 28.08.2016 10:48, Philipp Klaus Krause wrote: >> I have some trouble understanding the C8051 clock and timers. >> In the tutorial code at >> http://www.colecovision.eu/mcs51/C8051F340%20380%20LED.shtml I get a >> timer interrupt at 1000 Hz. But I got the values for configuring the >> timer experimentally, and can't seem to get them from the datasheet. >> >> According to the datsheet, at startup the internal oscillator is running >> at 24.5 Mhz (OSCICL reset value), then gets divided by 128 (OSCICN reset >> value) and then used for SYSCLK (CLKSEL rest value). >> >> Thus SYSCLK is 24.5 Mhz / 128 about 0.1914 Mhz. >> >> In the timer, this then gets divided by 12 (CKCON reset value) adn then >> sued as the frequency for incrementing the timer (TMOD value as in the >> tutorial code). >> >> Thus the timer increment frequency is 24.5 Mhz / 128 / 12 about 0.0159 >> Mhz. >> >> The tutorial code ssets the timer register to (65536 - 125) in the >> interrupt handler. >> >> Thus the timer interrupt frequency is 24.5 Mhz / 128 / 12 / 125 about >> 127 Hz. >> >> So from the datasheet I get 127 Hz. But on the hardware I see 1000 Hz. >> >> Philipp > > P.S.: I have verified that the OSCICN, CLKSEL and CKCON reset values are > as in the datasheet. Initializing these registers explicitly to their > reset value does not change anything. First of all which MCU are you using exactly? The C8051F340 as in the URL name does not run the internal RC oscillator at 24.5MHz but runs at 12MHz. And the default OSCICN value makes SYSCLK=HFRCO/8. Next there is a 4x clock multiplier which if off after reset (CLKMUL). And CLKSEL can choose this multiplied clock /2 or /1. Maarten -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] C8051 clock and timers
On 28.08.2016 12:11, Maarten Brock wrote: >> On 28.08.2016 10:48, Philipp Klaus Krause wrote: >>> I have some trouble understanding the C8051 clock and timers. >>> In the tutorial code at >>> http://www.colecovision.eu/mcs51/C8051F340%20380%20LED.shtml I get a >>> timer interrupt at 1000 Hz. But I got the values for configuring the >>> timer experimentally, and can't seem to get them from the datasheet. >>> >>> According to the datsheet, at startup the internal oscillator is running >>> at 24.5 Mhz (OSCICL reset value), then gets divided by 128 (OSCICN reset >>> value) and then used for SYSCLK (CLKSEL rest value). >>> >>> Thus SYSCLK is 24.5 Mhz / 128 about 0.1914 Mhz. >>> >>> In the timer, this then gets divided by 12 (CKCON reset value) adn then >>> sued as the frequency for incrementing the timer (TMOD value as in the >>> tutorial code). >>> >>> Thus the timer increment frequency is 24.5 Mhz / 128 / 12 about 0.0159 >>> Mhz. >>> >>> The tutorial code ssets the timer register to (65536 - 125) in the >>> interrupt handler. >>> >>> Thus the timer interrupt frequency is 24.5 Mhz / 128 / 12 / 125 about >>> 127 Hz. >>> >>> So from the datasheet I get 127 Hz. But on the hardware I see 1000 Hz. >>> >>> Philipp >> >> P.S.: I have verified that the OSCICN, CLKSEL and CKCON reset values are >> as in the datasheet. Initializing these registers explicitly to their >> reset value does not change anything. > > First of all which MCU are you using exactly? The C8051F340 as in the URL > name does not run the internal RC oscillator at 24.5MHz but runs at 12MHz. > And the default OSCICN value makes SYSCLK=HFRCO/8. Next there is a 4x > clock multiplier which if off after reset (CLKMUL). And CLKSEL can choose > this multiplied clock /2 or /1. > > Maarten For some reason, I was looking at the C8051F410 data sheet instead of the C8051F340. Sorry for the noise. Philipp signature.asc Description: OpenPGP digital signature -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 19.08.2016 23:11, benja...@southpole.se wrote: > Hi, I created some small examples for the C8051F*** > > https://github.com/merbanan/C8051F300_examples > > I tried these examples on a F340 but something doesnt work correctly when > I send data over the serial port and sdcc. If I use Keil it works fine. > And the same code works fine on the F300. > > I'd be happy to supply you with hardware if you are interested to > investigate the issue. Currently I use Keil which works with what I want > to do, but doing open stuff and needing a non free compiler causes an itch > I want to scratch. > > MvH > Benjamin Larsson > I have serial output on the C8051F330 board RDM6300 working via putchar() (see attached code). But whenever I try to use printf(), everything gets messed up: Garbage output on the serial line or the device resets. I've tried different memeory models and reentrency, but don't see a change in behaviour. printf() takes us over 4K of flash, but the C8051F330 has 8KB, so we should be ok. Philipp #include __sfr __at(0xe1) XBR0; __sfr __at(0xe2) XBR1; __sfr __at(0xa4) P0MDOUT; __sfr __at(0xd9) PCA0MD; __sfr __at(0x88) TCON; __sfr __at(0x89) TMOD; __sfr __at(0x8b) TL1; __sfr __at(0x8d) TH1; __sfr __at(0x98) SCON0; __sfr __at(0x99) SBUF0; int putchar(int c) { while(!(SCON0 & 0x02)); SCON0 &= ~0x02; SBUF0 = c; return (c); } unsigned char _sdcc_external_startup(void) { PCA0MD = 0; // Disable watchdog timer return 0; // perform normal initialization } void main(void) { unsigned long int i = 0; // Initialize I/O pins P0MDOUT = 0x10;// Set port P0.4 (Uart tx) to push-pull XBR0 = 0x01;// UART0 on P0.4 and P0.5 XBR1 = 0x40;// Enable push/pull for Tx // Configure UART for 1200 baud, 8 data bits, 1 stop bit. TMOD = 0x20; TH1 = 150; TCON |= 0x40; SCON0 = 0x42; for(;;) { putchar('A'); putchar('B'); putchar('C'); for(i = 0; i < 147456; i++); // Sleep putchar('D'); // Proof that its not just the watchdog resetting us for(i = 0; i < 147456; i++); // Sleep printf("Hello World!\n"); for(;;); // Proof that the device gets reset during printf() } } -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
Hi. > > I have serial output on the C8051F330 board RDM6300 working via > putchar() (see attached code). But whenever I try to use printf(), > everything gets messed up: Garbage output on the serial line or the > device resets. That is exactly the same behaviour I got on the F330. My code is a mess but it triggers exactly in the same way. > > I've tried different memeory models and reentrency, but don't see a > change in behaviour. printf() takes us over 4K of flash, but the > C8051F330 has 8KB, so we should be ok. > > Philipp I used my own function to send strings and tried all kinds of tricks but couldn't get around this issue. The Keil compiler that you get with the Silabs Simplicity Studio works without any problems for me. Maybe it would be worth porting your small example to Keil and see the difference in assembly ? MvH Benjamin Larsson -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
[Sdcc-user] PIC compilation weirdness
Hi, I've got this statement in main.c: // hid_tx_buffer.uint8[35] = g_stepper_state[0].position.uint8[3]; // and it compiles to: // DA C??? F??? 00630 MOVFF (_g_stepper_state + 3), (_g_stepper_state + 3) DE 51?? 00631 MOVF(_g_stepper_state + 3), W, B 00632 ; removed redundant BANKSEL E0 6F?? 00633 MOVWF (_hid_tx_buffer + 35), B // Now my question is why is there this MOVFF which looks like totally superfluous? I'm not an expert in PIC assembly so maybe I'm missing something... The declarations for the data structures are as follows. In a header file included in main.c // typedef union { uint8_t uint8[4]; uint16_t uint16[2]; uint32_t uint32; } uint32_t_multi_view; typedef struct { uint32_t_multi_view position; } stepper_status_t; extern volatile stepper_status_t g_stepper_state[]; // and in an other header file also included in main.c // typedef struct { union { uint8_t uint8[64]; uint16_t uint16[32]; uint32_t uint32[16]; int8_t int8[64]; int16_t int16[32]; int32_t int32[16]; }; } hid_buffer_t; extern volatile hid_buffer_t hid_tx_buffer; // and oh yeah, this is my SDCC on Mac OS X Yosemite: // /Users/nyholku/sdcc-3.4.0/bin/sdcc -v SDCC : mcs51/z80/z180/r2k/r3ka/gbz80/tlcs90/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8 3.4.0 #8981 (Apr 5 2014) (Mac OS X i386) published under GNU General Public License (GPL) // wbr Kusti This e-mail may contain confidential or privileged information. If you are not the intended recipient (or have received this e-mail in error) please notify the sender immediately and destroy this e-mail. Any unauthorized copying, disclosure or distribution of the material in this e-mail is strictly forbidden. We will not be liable for direct, indirect, special or consequential damages arising from alteration of the contents of this message by a third party or as a result of any virus being passed on or as of transmission of this e-mail in general. -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
> Hi. > >> >> I have serial output on the C8051F330 board RDM6300 working via >> putchar() (see attached code). But whenever I try to use printf(), >> everything gets messed up: Garbage output on the serial line or the >> device resets. > > That is exactly the same behaviour I got on the F330. My code is a mess > but it triggers exactly in the same way. > >> >> I've tried different memeory models and reentrency, but don't see a >> change in behaviour. printf() takes us over 4K of flash, but the >> C8051F330 has 8KB, so we should be ok. >> >> Philipp > > I used my own function to send strings and tried all kinds of tricks but > couldn't get around this issue. The Keil compiler that you get with the > Silabs Simplicity Studio works without any problems for me. Maybe it > would be worth porting your small example to Keil and see the difference > in assembly ? > > MvH > Benjamin Larsson See my earlier comments. You can't just dump your data into SBUF without checking it is free. A simple solution is to set TI initially, then in putchar wait for TI to be set, clear it and place the char into SBUF. A real solution would use interrupts and fifo ringbuffers for transmission and reception. Maarten -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 28.08.2016 15:05, Benjamin Larsson wrote: > Hi. > >> >> I have serial output on the C8051F330 board RDM6300 working via >> putchar() (see attached code). But whenever I try to use printf(), >> everything gets messed up: Garbage output on the serial line or the >> device resets. > > That is exactly the same behaviour I got on the F330. My code is a mess > but it triggers exactly in the same way. > >> >> I've tried different memeory models and reentrency, but don't see a >> change in behaviour. printf() takes us over 4K of flash, but the >> C8051F330 has 8KB, so we should be ok. >> >> Philipp > > I used my own function to send strings and tried all kinds of tricks but > couldn't get around this issue. The Keil compiler that you get with the > Silabs Simplicity Studio works without any problems for me. Maybe it > would be worth porting your small example to Keil and see the difference > in assembly ? > > MvH > Benjamin Larsson Actually, printf() does not really reset the device (sfr contents are not reset to their default values after terminating a program via printf()). That means it jumps to the start of main() some other way. Philipp -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 28.08.2016 21:44, Philipp Klaus Krause wrote: > On 28.08.2016 15:05, Benjamin Larsson wrote: >> Hi. >> >>> >>> I have serial output on the C8051F330 board RDM6300 working via >>> putchar() (see attached code). But whenever I try to use printf(), >>> everything gets messed up: Garbage output on the serial line or the >>> device resets. >> >> That is exactly the same behaviour I got on the F330. My code is a mess >> but it triggers exactly in the same way. >> >>> >>> I've tried different memeory models and reentrency, but don't see a >>> change in behaviour. printf() takes us over 4K of flash, but the >>> C8051F330 has 8KB, so we should be ok. >>> >>> Philipp >> >> I used my own function to send strings and tried all kinds of tricks but >> couldn't get around this issue. The Keil compiler that you get with the >> Silabs Simplicity Studio works without any problems for me. Maybe it >> would be worth porting your small example to Keil and see the difference >> in assembly ? >> >> MvH >> Benjamin Larsson > > Actually, printf() does not really reset the device (sfr contents are > not reset to their default values after terminating a program via > printf()). That means it jumps to the start of main() some other way. > > Philipp It seems any printf() call causes some problem (hang, jump to main somehow, whatever) - even printf(""). Philipp -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
Wondering how printf("") might cause problems, I looked at the executed code in printf_large. Basically it should just do a read from the pointer, see that we are at the end of the string and return. So I found the read from a pointer is the problem. Placing a read from a generic pointer in main() causes the same issues as the call to printf() (as long a printf() is linked into the binary). The attached example code printf "Hello " again and again, but never "World!". Philipp #include __sfr __at(0xe1) XBR0; __sfr __at(0xe2) XBR1; __sfr __at(0xa4) P0MDOUT; __sfr __at(0xd9) PCA0MD; __sfr __at(0x88) TCON; __sfr __at(0x89) TMOD; __sfr __at(0x8b) TL1; __sfr __at(0x8d) TH1; __sfr __at(0x98) SCON0; __sfr __at(0x99) SBUF0; __sfr __at(0xef) SRTSRC; int putchar(int c) { while(!(SCON0 & 0x02)); SCON0 &= ~0x02; SBUF0 = c; return (c); } unsigned char _sdcc_external_startup(void) { PCA0MD = 0; // Disable watchdog timer return 0; // perform normal initialization } char c; char *gptr; void main(void) { unsigned long int i = 0; // Initialize I/O pins P0MDOUT = 0x10;// Set port P0.4 (Uart tx) to push-pull XBR0 = 0x01;// UART0 on P0.4 and P0.5 XBR1 = 0x40;// Enable push/pull for Tx // Configure UART for 1200 baud, 8 data bits, 1 stop bit. TMOD = 0x20; TH1 = 150; TCON |= 0x40; SCON0 = 0x42; for(;;) { putchar('H'); putchar('e'); putchar('l'); putchar('l'); putchar('o'); putchar(' '); for(i = 0; i < 5; i++); // Sleep c = 'W'; gptr = &c; putchar(*gptr); for(i = 0; i < 5; i++); // Sleep putchar('o'); putchar('r'); putchar('l'); putchar('d'); printf("!"); for(;;); } } -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
Dear Benjamin, what tool are you using to write your program onto the board? Philipp -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 08/28/2016 11:03 PM, Philipp Klaus Krause wrote: > Dear Benjamin, > > what tool are you using to write your program onto the board? > > Philipp Under windows I use simplicity studio with a UC6 device. Under linux I use ec2-new from https://github.com/paragonRobotics/ec2-new with the same device. My own arduino programmer did not work with the F330. MvH Benjamin Larsson -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 28.08.2016 23:13, Benjamin Larsson wrote: > On 08/28/2016 11:03 PM, Philipp Klaus Krause wrote: >> Dear Benjamin, >> >> what tool are you using to write your program onto the board? >> >> Philipp > > > Under windows I use simplicity studio with a UC6 device. Under linux I > use ec2-new from https://github.com/paragonRobotics/ec2-new with the > same device. My own arduino programmer did not work with the F330. > > MvH > Benjamin Larsson I so far only used ec2-new. Did you try to write the program compiled with SDCC using simplicity studio? That would help rule out an ec2-new bug. Philipp -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 08/28/2016 11:00 PM, Philipp Klaus Krause wrote: > Wondering how printf("") might cause problems, I looked at the executed > code in printf_large. Basically it should just do a read from the > pointer, see that we are at the end of the string and return. > > So I found the read from a pointer is the problem. Placing a read from a > generic pointer in main() causes the same issues as the call to printf() > (as long a printf() is linked into the binary). The attached example > code printf "Hello " again and again, but never "World!". > > Philipp > Just to confirm your findings I have this function as my "printf": void send_string(unsigned char *string) { while(*string) send_byte(*string++); // Send string 1 byte at a time } And writing chars was fine but not strings. MvH Benjamin Larsson -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user
Re: [Sdcc-user] getting started with C8051
On 08/28/2016 11:30 PM, Philipp Klaus Krause wrote: > On 28.08.2016 23:13, Benjamin Larsson wrote: >> On 08/28/2016 11:03 PM, Philipp Klaus Krause wrote: >>> Dear Benjamin, >>> >>> what tool are you using to write your program onto the board? >>> >>> Philipp >> >> >> Under windows I use simplicity studio with a UC6 device. Under linux I >> use ec2-new from https://github.com/paragonRobotics/ec2-new with the >> same device. My own arduino programmer did not work with the F330. >> >> MvH >> Benjamin Larsson > > I so far only used ec2-new. Did you try to write the program compiled > with SDCC using simplicity studio? That would help rule out an ec2-new bug. > > Philipp > I did lots of tinkering trying to get this to work, I am pretty sure I did the reverse. Keil compiled hex flashed with ec2-new. But I can't really trust my memory on that. I am pretty sure I dumped the flash and compared the output. But I can redo it if you want. MvH Benjamin Larsson -- ___ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user