Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 24
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
Steve Conner (46)


Next birthdays
04/29 GODSFUSION (37)
04/29 Zajcek (37)
04/29 ElectroDog (33)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

PIC "C" macro definitions for PORTB and *pointers on PIC18.

Move Thread LAN_403
Carbon_Rod
Fri Apr 21 2006, 12:10AM Print
Carbon_Rod Registered Member #65 Joined: Thu Feb 09 2006, 06:43AM
Location:
Posts: 1155
The situation:
Declared an array of const function pointers that scheduled to run round robin style for consistent timing. A helper process may be assigned to a given port address and asserts a test-and-set mutex to perform the bus transaction.

The problem: The C macro definitions for PORTB on PIC18.

Normally I would just write it in asm and do an extern call to get around this problem using the indirect de-referencing reregister trick. However, the code must be portable without violating the other procs register states when interrupted. The compiler documentation/code support, google, microchip, and Usenet searches have not been very helpful. (I am rather amazed it took this long to run into this issue)

Any ideas on how a simple macro to a non-constant (far unsigned char*) pointer translation can be done on the fly in C?

Cheers, =D
Back to top
Steve Conner
Fri Apr 21 2006, 10:14AM
Steve Conner Registered Member #30 Joined: Fri Feb 03 2006, 10:52AM
Location: Glasgow, Scotland
Posts: 6706
I'm not sure I even understand what the problem is. I think you are saying that you are writing a multitasking OS for a PIC, and you want several processes to be able to access the same I/O port, but it won't work?

I have the same problem doing firmware for our instruments at work, where I sometimes find that I want the main code to toggle a few bits in a given port, and an interrupt service routine to toggle a few other bits in the same port. The read-modify-write behaviour of the PIC's ports doesn't seem to work quite right under these conditions. I'm no computer scientist, so rather than mess around with locks and mutexes, I just try and avoid this situation of multiple processes accessing the same hardware resource altogether, and if I have to do it, I keep a spare copy of the port output states in another register, and use that for the read-modify-write, instead of reading the port itself.

*edit* I just re-read your post, and it looks like I totally misunderstood what the question was. Can't you write your helper process as a function that takes an argument of type (whatever type holds a pointer to a PIC18xxx memory location), and then when you want to assign your process to PORTB, take the constant "PORTB" as defined in the header file, load it into a variable of the appropriate type, and call the function with this variable as its argument?

Another possibility would be to do it in assembler, but save the states of any registers you use and restore them afterwards.
Back to top
Carbon_Rod
Sat Apr 22 2006, 12:55AM
Carbon_Rod Registered Member #65 Joined: Thu Feb 09 2006, 06:43AM
Location:
Posts: 1155
// OK so I think I solved the issue:
// Can you spot the syntax error(s) that refuses to compile foo() ?
// Yet bar() compiles just fine.

unsigned char* io_port_Alias_Ptr; //port alias

//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\
//This Compiles in Hi-tech C
//This chokes with a syntax error in Microchip C.
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\
void foo(void)
{
io_Port_Alias_Ptr = &PORTB;
*io_Port_Alias_Ptr = 0xF0;

}


//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\//\\
//This Compiles in Hi-tech C
//This Compiles in Microchip C.
//Microchip C syntax error bug magically disappears –POOF!
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\/ /\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\// \\

void bar(void)
{
io_Port_Alias_Ptr = &PORTB;
*io_Port_Alias_Ptr = 0x0F;
}
Back to top
...
Sat Apr 22 2006, 01:31AM
... Registered Member #56 Joined: Thu Feb 09 2006, 05:02AM
Location: Southern Califorina, USA
Posts: 2445
could it have been related to the fact that one one refers to 0xf0 and one to 0x0f?
I asume that the extra return isn't related to it...
Back to top
Carbon_Rod
Sat Apr 22 2006, 03:16AM
Carbon_Rod Registered Member #65 Joined: Thu Feb 09 2006, 06:43AM
Location:
Posts: 1155
1.) Variable name case: (duoh! borken)
2.) "Second is using a backslash" <- Weird Microchip multi-line issue
3.) Variable declaration:
volatile near unsigned char *portb_ptr;

Personally I took way too long to spot 2 when porting the code as the other compiler never complained

Cheers, =D
Back to top

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.