Author Topic: [gcc C] concatenating text to make up code  (Read 19880 times)

0 Members and 1 Guest are viewing this topic.

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #25 on: October 07, 2018, 09:52:02 am »
I take it I can use string variables to put a bit of text in? As the compiler is efficient this will all come out anyway in optimisation. The idea is to simplify program writing.

Not in the way you are thinking, not unless you use the C preprocessor as indicated higher up the thread.

I do think you are somehow barking up the wrong tree here. "Concatenating text to make up code" is not the solution to problem you are trying to solve.

Instead of this, you should be thinking of function parameters and various operations in involving bit patterns like shifting, AND'ing and OR'ing them together. In the computer's world the "text" that needs concatenating is groups of bits, not human letters and numbers.

I am using defines but yes you are right, I just wrote the below but yes if i mathematically used the right hand digit I could have done a bit of automation there. i have a set of definitions that translate a text to the numbers like "#define PORT_A_PIN_0 10" Which i have made intentionally wordy to avoid accidentally using an existing manufacturer definition.

Code: [Select]
void set_input_w_pullup(uint8_t port_pin){
switch (port_pin){

case 10: PA0_input_w_pullup();
break;

case 11: PA1_input_w_pullup();
break;

case 12: PA2_input_w_pullup();
break;

case 13: PA3_input_w_pullup();
break;

case 14: PA4_input_w_pullup();
break;

case 15: PA5_input_w_pullup();
break;

case 16: PA6_input_w_pullup();
break;

case 17: PA7_input_w_pullup();
break;

case 20: PB0_input_w_pullup();
break;

case 21: PB1_input_w_pullup();
break;

case 22: PB2_input_w_pullup();
break;

case 23: PB3_input_w_pullup();
break;

case 24: PB4_input_w_pullup();
break;

case 25: PB5_input_w_pullup();
break;

case 26: PB6_input_w_pullup();
break;

case 27: PB7_input_w_pullup();
break;

case 30: PC0_input_w_pullup();
break;

case 31: PC1_input_w_pullup();
break;

case 32: PC2_input_w_pullup();
break;

case 33: PC3_input_w_pullup();
break;

case 34: PC4_input_w_pullup();
break;

case 35: PC5_input_w_pullup();
break;

case 36: PC6_input_w_pullup();
break;.

case 37: PC7_input_w_pullup();
break;

case 40: PD0_input_w_pullup();
break;

case 41: PD1_input_w_pullup();
break;

case 42: PD2_input_w_pullup();
break;

case 43: PD3_input_w_pullup();
break;

case 44: PD4_input_w_pullup();
break;

case 45: PD5_input_w_pullup();
break;

case 46: PD6_input_w_pullup();
break;

case 47: PD7_input_w_pullup();
break;

case 50: PE0_input_w_pullup();
break;

case 51: PE1_input_w_pullup();
break;

case 52: PE2_input_w_pullup();
break;

case 53: PE3_input_w_pullup();
break;

case 54: PE4_input_w_pullup();
break;

case 55: PE5_input_w_pullup();
break;

case 56: PE6_input_w_pullup();
break;

case 57: PE7_input_w_pullup();
break;

case 60: PF0_input_w_pullup();
break;

case 61: PF1_input_w_pullup();
break;

case 62: PF2_input_w_pullup();
break;

case 63: PF3_input_w_pullup();
break;

case 64: PF4_input_w_pullup();
break;

case 65: PF5_input_w_pullup();
break;

case 66: PF6_input_w_pullup();
break;

case 67: PF7_input_w_pullup();
break;
}
}
 

Offline hamster_nz

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
Re: [gcc C] concatenating text to make up code
« Reply #26 on: October 07, 2018, 10:06:51 am »
why not...

Code: [Select]
void set_input_w_pullup(uint8_t port_pin){
   uint8_t port = port_pin/8;
   uint8_t pin = port_pin%8;
   uint8_t mask = 1<<pin;
   switch( (port) {
      case 0:
         PORTA.DIRCLR = mask;
         break;
      case 1:
         PORTB.DIRCLR = mask;
         break;
      case 2:
         PORTB.DIRCLR = mask;
         break;
      ... and so on ...
      default:
         /* Error */
         break;
   }
}

Then just have #defines for port a pins as 0 through 7,  port b pins as 8 through 15, port c pins as 16 through 23 and so on.

You can do the '/8' and '%8' using  ">>3" and '&7' to make it more efficient.
« Last Edit: October 07, 2018, 10:13:15 am by hamster_nz »
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #27 on: October 07, 2018, 10:28:35 am »
Yes I should have done something like that.
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1722
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: [gcc C] concatenating text to make up code
« Reply #28 on: October 07, 2018, 01:13:04 pm »
why not...

Code: [Select]
void set_input_w_pullup(uint8_t port_pin){
   uint8_t port = port_pin/8;
   uint8_t pin = port_pin%8;
   uint8_t mask = 1<<pin;
   switch( (port) {
      case 0:
         PORTA.DIRCLR = mask;
         break;
      case 1:
         PORTB.DIRCLR = mask;
         break;
      case 2:
         PORTB.DIRCLR = mask;
         break;
      ... and so on ...
      default:
         /* Error */
         break;
   }
}

Then just have #defines for port a pins as 0 through 7,  port b pins as 8 through 15, port c pins as 16 through 23 and so on.

You can do the '/8' and '%8' using  ">>3" and '&7' to make it more efficient.
This method can process only 1 pin at a time.  If you want to process all pins in one function call, then you can set in one byte all the pin settings and then loop bit shifting and processing the pins that are set and ignore the ones that are not set.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3143
  • Country: ca
Re: [gcc C] concatenating text to make up code
« Reply #29 on: October 07, 2018, 03:06:10 pm »
Yes I should have done something like that.

It is ok if you really need to select the pin at run time.

If you just want to set/clear a specific pin, there's no reason for doing the parsing at run-time.

Of course, if you inline the function and call it with a constant as a parameter, the compiler may indeed be able to optimize everything out. But even if it can do such optimizations, the compiler is very likely not to honor your inlining when you call the same function 20 or 30 times from different places.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #30 on: October 07, 2018, 03:22:46 pm »
This method can process only 1 pin at a time.  If you want to process all pins in one function call, then you can set in one byte all the pin settings and then loop bit shifting and processing the pins that are set and ignore the ones that are not set.

i would only be using one pin at a time anyway as it will be used with a periphery setup.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #31 on: October 07, 2018, 03:59:28 pm »
Well I have been able to replace the massive switch with the following, but this does not include the pull-ups which are a register per pin so it would be another massive switch to choose the register unless i can play with raw memory address numbers, um safe in the knowledge that the addresses will not change.

so for example: "static inline void PF3_pullup_on(){         BIT1(PORTF.PIN3CTRL, 3); }"

Code: [Select]
void set_input(uint8_t port_pin){
uint8_t port = (port_pin / 8); // Calculate the part number
uint8_t pin = (port_pin % 8); // Calculate the pin number

switch (port){

case 0: (PORTA.DIRCLR = 0x01 << pin);
break;

case 1: (PORTB.DIRCLR = 0x01 << pin);
break;

case 2: (PORTC.DIRCLR = 0x01 << pin);
break;

case 3: (PORTD.DIRCLR = 0x01 << pin);
break;

case 4: (PORTE.DIRCLR = 0x01 << pin);
break;

case 5: (PORTF.DIRCLR = 0x01 << pin);
break;
}
}
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: [gcc C] concatenating text to make up code
« Reply #32 on: October 07, 2018, 04:28:37 pm »
Its much more fun when you can get away from the preprocessor-

Code: [Select]
#include <stdint.h>

typedef enum {
    A0=0x400, A1, A2, A3, A4, A5, A6, A7,
    B0=0x420, B1, B2, B3, B4, B5, B6, B7,
    C0=0x440, C1, C2, C3, C4, C5, C6, C7,
    D0=0x460, D1, D2, D3, D4, D5, D6, D7,
    E0=0x480, E1, E2, E3, E4, E5, E6, E7,
    F0=0x4F0, F1, F2, F3, F4, F5, F6, F7,
} PIN_t;

typedef enum { OUT=1, IN } DIR_t;
typedef enum { PULLUP_OFF, PULLUP_ON } PULLUP_t;

static inline void pin_dir(PIN_t pin, DIR_t io){
    ((volatile uint8_t*)(pin & 0XFF0))[io] = 1<<(pin&7);
}

static inline void pin_pullup(PIN_t pin, PULLUP_t pu){
    //with pullup wanted, also assume is an input
    if(pu){
        pin_dir(pin, IN);
        ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3;
    } else {
        ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] &= ~(1<<3);
    }
}

static inline void pin_set(PIN_t pin, uint8_t hl){
    if(hl)((volatile uint8_t*)(pin & 0XFF0))[5] = 1<<(pin&7);
    else ((volatile uint8_t*)(pin & 0XFF0))[6] = 1<<(pin&7);
}

static inline void pin_toggle(PIN_t pin){
    ((volatile uint8_t*)(pin & 0XFF0))[7] = 1<<(pin&7);
}

static inline uint8_t pin_get(PIN_t pin){
    return ((volatile uint8_t*)(pin & 0XFF0))[8] & (1<<(pin&7));
}

#define sw1 A2
#define led1 B3

int main(){
    pin_pullup(sw1, PULLUP_ON);
    pin_dir(led1, OUT);

    //led1 lights when sw1 pressed
    for(;;){
        pin_set(led1, !pin_get(sw1));
    }
}

/*
Disassembly of section .text:

00000000 <main>:
//sw1 input
   0:   84 e0           ldi     r24, 0x04       ; 4
   2:   80 93 02 04     sts     0x0402, r24     ; 0x800402 <_edata+0x3a2>
//sw1 pullup on
   6:   80 91 12 04     lds     r24, 0x0412     ; 0x800412 <_edata+0x3b2>
   a:   88 60           ori     r24, 0x08       ; 8
   c:   80 93 12 04     sts     0x0412, r24     ; 0x800412 <_edata+0x3b2>
//led1 output
  10:   88 e0           ldi     r24, 0x08       ; 8
  12:   80 93 21 04     sts     0x0421, r24     ; 0x800421 <_edata+0x3c1>
//get sw1  (loop starts here)
  16:   90 91 08 04     lds     r25, 0x0408     ; 0x800408 <_edata+0x3a8>
  1a:   92 fd           sbrc    r25, 2
  1c:   03 c0           rjmp    .+6             ; 0x24 <__zero_reg__+0x23>
//sw1=0, so set led1=1
  1e:   80 93 25 04     sts     0x0425, r24     ; 0x800425 <_edata+0x3c5>
  22:   f9 cf           rjmp    .-14            ; 0x16 <__zero_reg__+0x15>
//else sw1=1, so led1=0
  24:   80 93 26 04     sts     0x0426, r24     ; 0x800426 <_edata+0x3c6>
  28:   f6 cf           rjmp    .-20            ; 0x16 <__zero_reg__+0x15>
*/

I'm no expert, but the more you let the compiler handle things, the better.

The above may not be perfect, bu the only 2 defines above are to give friendly names for the pins. I don't need a thousand lines of defines, and each function- whether inline or not can be easily tested/tweaked.

As can be seen above, the compiler does a pretty good job (I used -Os).
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #33 on: October 07, 2018, 05:03:53 pm »
so can I do something like.

given the offset for pin control register is: 0x10 + n*0x01 [n=0..7]
Code: [Select]

uint8_t pin;
#define PORTA_OFFSET 0x0400

PORTA_OFFSET + 0x10 + pin*0x01 = to an expression ;

so basically can I assign a value to a register address who's number is calculated. This would make for some very slick code as a single line would do all ports and pins.
« Last Edit: October 07, 2018, 05:13:51 pm by Simon »
 

Offline TK

  • Super Contributor
  • ***
  • Posts: 1722
  • Country: us
  • I am a Systems Analyst who plays with Electronics
Re: [gcc C] concatenating text to make up code
« Reply #34 on: October 07, 2018, 05:17:37 pm »
so can I do something like.

given the offset for pin control register is: 0x10 + n*0x01 [n=0..7]
Code: [Select]

uint8_t pin;
#define PORTA_OFFSET 0x0400

PORTA_OFFSET + 0x10 + pin*0x01 = to an expression ;

so basically can I assign a value to a register address who's number is calculated. This would make for some very slick code as a single line would do all ports and pins.

pin * 0x01 is equal to pin.
« Last Edit: October 07, 2018, 05:29:51 pm by Simon »
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #35 on: October 07, 2018, 05:32:50 pm »
I don't understand enums and even if i learned that bit now how does it help to make another variable that represents a memory address when i can just write to that memory address and calculate it as well, all i need to do is extract the part number and pin number.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #36 on: October 07, 2018, 05:33:20 pm »
so can I do something like.

given the offset for pin control register is: 0x10 + n*0x01 [n=0..7]
Code: [Select]

uint8_t pin;
#define PORTA_OFFSET 0x0400

PORTA_OFFSET + 0x10 + pin*0x01 = to an expression ;

so basically can I assign a value to a register address who's number is calculated. This would make for some very slick code as a single line would do all ports and pins.

pin * 0x01 is equal to pin.

Yes true, but does it work in principle?
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: [gcc C] concatenating text to make up code
« Reply #37 on: October 07, 2018, 05:33:34 pm »
Did you even look at the code I posted? Compile it, look at the code produced, change it, whatever.

Every pin is in the enum- A0 - F7
the port address is 'encoded' in the pin enum- A2 = 0x0402,  PORTA = (A2 & 0xFF0) = 0x400,  pin = (A2 & 7) = 2

static inline void pin_pullup(PIN_t pin, PULLUP_t pu){
    //with pullup wanted, also assume is an input
    if(pu){
        pin_dir(pin, IN);
        ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3;
    } else {
        ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] &= ~(1<<3);
    }
}

I added setting the pin_dir since it mostly makes no sense to have a pullup unless an input, I also added the ability to specify PULLUP_ON or PULLUP_OFF instead of having separate functions for ON and OFF.

but it boils down to this to set the pullup bit-
((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3;

if A2 (0x402) passed into function, result is-
 ((volatile uint8_t*)(0x400))[0x12)] |= 1<<3;
or
PINCTRL2 |= 8;
which sets the PULLUPEN bit for PORTA/PIN2


With the pin enums, you have all the info needed to do about anything with a pin- you have the port base address and the pin number and the various port registers are at known offsets from the base register.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3143
  • Country: ca
Re: [gcc C] concatenating text to make up code
« Reply #38 on: October 07, 2018, 05:49:30 pm »
I don't need a thousand lines of defines, and each function- whether inline or not can be easily tested/tweaked.

If you used defines instead of static functions, it would be about the same number of lines, not thousands.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #39 on: October 07, 2018, 06:00:40 pm »
Did you even look at the code I posted? Compile it, look at the code produced, change it, whatever.

Every pin is in the enum- A0 - F7
the port address is 'encoded' in the pin enum- A2 = 0x0402,  PORTA = (A2 & 0xFF0) = 0x400,  pin = (A2 & 7) = 2

static inline void pin_pullup(PIN_t pin, PULLUP_t pu){
    //with pullup wanted, also assume is an input
    if(pu){
        pin_dir(pin, IN);
        ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3;
    } else {
        ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] &= ~(1<<3);
    }
}

I added setting the pin_dir since it mostly makes no sense to have a pullup unless an input, I also added the ability to specify PULLUP_ON or PULLUP_OFF instead of having separate functions for ON and OFF.

but it boils down to this to set the pullup bit-
((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3;

if A2 (0x402) passed into function, result is-
 ((volatile uint8_t*)(0x400))[0x12)] |= 1<<3;
or
PINCTRL2 |= 8;
which sets the PULLUPEN bit for PORTA/PIN2


With the pin enums, you have all the info needed to do about anything with a pin- you have the port base address and the pin number and the various port registers are at known offsets from the base register.

No sorry I did not understand it but that made more sense, essentially it does the same as what I proposed.

All i need to make this work with defines is:

Code: [Select]
/* Memory location offsets for ATmega 0-series */

#define PORTS_OFFSET 0x4000
#define PORTS_OFFSET_GAP 0X20

#define PORTA_OFFSET 0x0400
#define PARTB_OFFSET 0x0420
#define PORTC_OFFSET 0x0440
#define PORTD_OFFSET 0x0460
#define PORTE_OFFSET 0x0480
#define PORTF_OFFSET 0x04A0
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: [gcc C] concatenating text to make up code
« Reply #40 on: October 07, 2018, 06:02:07 pm »
Quote
I don't understand enums
There is the internet, which can give lots of info.

Basically the typdef enum turns a set of numbers into a specific type which the compiler can enforce usage like any other type..

In my example, using the PIN_t for the parameter to these functions prevents one from misusing the function. Only a PIN_t value can be used, which means only  the numbers in the typedef enum can be used. You can't pass arbitrary numbers, only specific ones which the compiler enforces. So the compiler does the checking for valid 'pins'- if we use a 'normal' number passed into these functions (by mistake) the compiler will complain and prevent you from doing it (of which the end result would be a write to who-knows-where).

If one allowed any number to be used by the function, you would either have to check if its in a valid range or just 'hope' its not used with an invalid number.

Quote
If you used defines instead of static functions, it would be about the same number of lines, not thousands.
I was referring to his current system of a thousand defines.
Yes, you could also turn these into defines, but how do you then enforce type?
#define PULLUP_ON(pin)  ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3
I can use about any number for 'pin' and there will be nothing to prevent incorrect use
Why not let the compiler help out.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #41 on: October 07, 2018, 06:14:28 pm »


Quote
If you used defines instead of static functions, it would be about the same number of lines, not thousands.
I was referring to his current system of a thousand defines.
Yes, you could also turn these into defines, but how do you then enforce type?
#define PULLUP_ON(pin)  ((volatile uint8_t*)(pin & 0XFF0))[0x10+(pin&7)] |= 1<<3
I can use about any number for 'pin' and there will be nothing to prevent incorrect use
Why not let the compiler help out.

I no longer use the defines, I use functions but then I can end up writing as many functions as defines. Doing it this way does cut down massively on either, but defines will still be key. because I can't have C take text as a variable and I human don't see the meaning of random numbers so i have to use defines to convert sensible text into numbers that are sensible for the compiler. I am not using any defines to paste code as i used to. Even the manufacturer uses defines to convert register names to numbers, i am simply proposing to bypass all of that and write my own stuff straight to memory numbers.

So if I do have a wrong number what actually happens? does it come out in compile. the numbers are set in defines, if i write a wrong define that too flags an error.
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: [gcc C] concatenating text to make up code
« Reply #42 on: October 07, 2018, 06:53:34 pm »
Maybe we should back up- what do you want your end result to look like when you finally write code and use these functions-

using my example (for pins), I would want a simple way to get a friendly name for my pins, and also an easy way to change them-

#define sw1 A2
#define led1 B3
OR
I could simple add these to the end of my PIN_t typedef enum (probably a better idea, as all pin info will now be in one location)
...F6, F7,
sw1=A2, led1=B3
} PIN_t;
OR
const PIN_t sw1=A2;
const PIN_t led1=B3;


and to use them, have some set of functions with obvious names to do things to the pins-

pin_pullup(sw1, PULLUP_ON); pin_pullup(sw1, PULLUP_OFF);
pin_dir(led1, OUT); pin_dir(led1, IN);
pin_set(led1, 1); pin_set(led1, 0);

whatever you want it to look like, you now have a target and just have to figure out how to get there.


Maybe show us what you want your end result to look like for pins- how would you get a friendly pin name and how would you use it to do something.
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #43 on: October 07, 2018, 07:00:37 pm »
Pin_input(port, pin);
So i either use one (defined) number for the port and then do /8 and %8 to or input them individually as above.

So with a number for the port and pin I can carry out the math to calculate the memory address of the register in question, this is much easier that caring what the register names are.

This but a simple example. I strarted trying to write a function to setup a TCB counter to read a PWM input:

TCB_pwm_setup(port, pin, event_channel);

I was going to make that a channel specific function and write it all 4 times over but this way it can be one set of code for any number of counters.

So essentially i am bypassing the manufacturers defines and writing my own coding system like the arduino.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 11859
  • Country: us
Re: [gcc C] concatenating text to make up code
« Reply #44 on: October 07, 2018, 07:28:23 pm »
...because I can't have C take text as a variable...

Well you can, sort of, if you use the C preprocessor and the concatenation operators.

Probably, what cv007 said makes sense. Show what you want your end result to look like, and maybe someone can show you how to make it happen. Also, be wary of reinventing wheels. Whatever you are trying to do, there must be many people before you trying to do the same thing. Can you find out what solution they used?
 

Offline cv007

  • Frequent Contributor
  • **
  • Posts: 825
Re: [gcc C] concatenating text to make up code
« Reply #45 on: October 07, 2018, 07:29:26 pm »
still don't know how you get a friendly pin name, so-

How about this- write some code to duplicate what I wrote earlier in whatever syntax/format/functions/defines you want so you end up with a main function that simply-
1) sets a pin to an input with pullup on (pin I called sw1)
2) set a pin as an output (pin I called led1)
3) loops with the pin output set to the input pin inverted value (sw1 pressed=led1 on)

(don't care about anything else but pins, and only needed functions- just enough to do 1,2,3)
should be easy enough, then 'we' can see where you are heading and maybe help by pointing out improvements that can be made

if you want
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3143
  • Country: ca
Re: [gcc C] concatenating text to make up code
« Reply #46 on: October 07, 2018, 07:58:31 pm »
TCB_pwm_setup(port, pin, event_channel);

Port is basically an array in memory, so you can simply pass a pointer to the first register. Then you can access other registers as elements of the array as cv007 did in his. However, this is, of course, architecture specific.
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 3143
  • Country: ca
Re: [gcc C] concatenating text to make up code
« Reply #47 on: October 07, 2018, 08:07:07 pm »
I can use about any number for 'pin' and there will be nothing to prevent incorrect use
Why not let the compiler help out.

You can still pass a wrong value of a correct type. This will still mess up your program.

I'm not a big fan of strong typification - it often requires lots of typecasts which are time-consuming
and clutter the code. Moreover, once you apply the typecast, there's no longer any checks for types.

 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #48 on: October 07, 2018, 08:24:49 pm »
Code: [Select]

#define PORTS__OFFSET 0x4000
#define PORTS__OFFSET_GAP 0X20
#define PORTA__OFFSET 0x0400
#define PARTB__OFFSET 0x0420
#define PORTC__OFFSET 0x0440
#define PORTD__OFFSET 0x0460
#define PORTE__OFFSET 0x0480
#define PORTF__OFFSET 0x04A0

#define DATA_DIRECTION 0x00
#define DATA_DIR_SET 0x01
#define DATA_DIR_CLEAR 0x02
#define DATA_DIR_TOGGLE 0x03
#define DATA_OUTPUT 0x04
#define DATA_OUTPUT_SET 0x05
#define DATA_OUTPUT_CLEAR 0x06
#define DATA_OUTPUT_TOGGLE 0x07
#define DATA_INPUT_PIN_VALUE 0x08
#define PIN_INTERRUPT_FLAGS 0x09
#define PORT_CONTROL 0x0A
#define PIN_CONTROL 0x10

#define PORT_A 0x00
#define PORT_B 0x01
#define PORT_C 0x02
#define PORT_D 0x03
#define PORT_E 0x04
#define PORT_F 0x05

#define PIN_0 0x00
#define PIN_1 0x01
#define PIN_2 0x02
#define PIN_3 0x03
#define PIN_4 0x04
#define PIN_5 0x05
#define PIN_6 0x06
#define PIN_7 0x07

#define PULLUP 0x03

void setup_input_w_pullup(uint8_t port, uint8_t pin){
PORTS__OFFSET + PORTS__OFFSET_GAP * port + DATA_DIR_SET = 0x01 << pin;
PORTS__OFFSET + PORTS__OFFSET_GAP * port + PIN_CONTROL + pin = 0x01 << PULLUP;
}
 

Offline SimonTopic starter

  • Global Moderator
  • *****
  • Posts: 17814
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: [gcc C] concatenating text to make up code
« Reply #49 on: October 07, 2018, 08:28:41 pm »
That one function replaces 48 + 48 inline functions, I guess the compiler will pre-calculate the addresses in optimisation.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf