Received: from PCH.mit.edu (18.7.21.50) by mail.efplus.com (192.168.0.8) with Microsoft SMTP Server (TLS) id 8.3.485.1; Sat, 1 Aug 2020 18:34:11 -0700 Received: from PCH.MIT.EDU (localhost.localdomain [127.0.0.1]) by PCH.mit.edu (8.14.7/8.12.8) with ESMTP id 0721PaSJ017381; Sat, 1 Aug 2020 21:26:19 -0400 Received: from outgoing-exchange-5.mit.edu (OUTGOING-EXCHANGE-5.MIT.EDU [18.9.28.59]) by PCH.mit.edu (8.14.7/8.12.8) with ESMTP id 0721PZpo017378 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Sat, 1 Aug 2020 21:25:35 -0400 Received: from oc11exedge1.exchange.mit.edu (OC11EXEDGE1.EXCHANGE.MIT.EDU [18.9.3.17]) by outgoing-exchange-5.mit.edu (8.14.7/8.12.4) with ESMTP id 0721PK7X020150 for ; Sat, 1 Aug 2020 21:25:20 -0400 Received: from oc11expo7.exchange.mit.edu (18.9.4.12) by oc11exedge1.exchange.mit.edu (18.9.3.17) with Microsoft SMTP Server (TLS) id 15.0.1293.2; Sat, 1 Aug 2020 21:25:02 -0400 Received: from oc11exhyb7.exchange.mit.edu (18.9.1.112) by oc11expo7.exchange.mit.edu (18.9.4.12) with Microsoft SMTP Server (TLS) id 15.0.1365.1; Sat, 1 Aug 2020 21:25:34 -0400 Received: from NAM10-MW2-obe.outbound.protection.outlook.com (104.47.55.102) by oc11exhyb7.exchange.mit.edu (18.9.1.112) with Microsoft SMTP Server (TLS) id 15.0.1395.4 via Frontend Transport; Sat, 1 Aug 2020 21:25:33 -0400 Received: from MWHPR21CA0062.namprd21.prod.outlook.com (2603:10b6:300:db::24) by BN6PR01MB3219.prod.exchangelabs.com (2603:10b6:404:cd::22) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.20; Sun, 2 Aug 2020 01:25:32 +0000 Received: from CO1NAM03FT011.eop-NAM03.prod.protection.outlook.com (2603:10b6:300:db:cafe::41) by MWHPR21CA0062.outlook.office365.com (2603:10b6:300:db::24) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3283.2 via Frontend Transport; Sun, 2 Aug 2020 01:25:32 +0000 Received: from NAM02-CY1-obe.outbound.protection.outlook.com (40.92.4.68) by CO1NAM03FT011.mail.protection.outlook.com (10.152.80.87) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.20 via Frontend Transport; Sun, 2 Aug 2020 01:25:31 +0000 Received: from CY1NAM02FT031.eop-nam02.prod.protection.outlook.com (10.152.74.51) by CY1NAM02HT213.eop-nam02.prod.protection.outlook.com (10.152.74.191) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.20; Sun, 2 Aug 2020 01:25:30 +0000 Received: from BYAPR02MB4055.namprd02.prod.outlook.com (2a01:111:e400:7e45::4f) by CY1NAM02FT031.mail.protection.outlook.com (2a01:111:e400:7e45::436) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.17 via Frontend Transport; Sun, 2 Aug 2020 01:25:30 +0000 Received: from BYAPR02MB4055.namprd02.prod.outlook.com ([fe80::e04e:e8e2:57be:7927]) by BYAPR02MB4055.namprd02.prod.outlook.com ([fe80::e04e:e8e2:57be:7927%7]) with mapi id 15.20.3239.020; Sun, 2 Aug 2020 01:25:30 +0000 From: Bob Blick To: Microcontroller discussion list - Public. Sender: "piclist-bounces@mit.edu" Date: Sat, 1 Aug 2020 18:25:30 -0700 Subject: Re: [PIC] Simple GCC question Thread-Topic: [PIC] Simple GCC question Thread-Index: AQHWZotc+nNhZovUu0iBK4Bhj1/IWakgpd6AgAAPvzOAAK98gIACo93A Message-ID: References: , , List-Help: List-Subscribe: , List-Unsubscribe: , In-Reply-To: Reply-To: Microcontroller discussion list - Public. Accept-Language: en-US Content-Language: en-US X-MS-Exchange-Organization-AuthAs: Anonymous X-MS-Exchange-Organization-AuthSource: TS500.efplus4.local X-MS-Has-Attach: X-Auto-Response-Suppress: All X-MS-Exchange-Organization-SenderIdResult: TempError X-MS-Exchange-Organization-PRD: mit.edu X-MS-TNEF-Correlator: acceptlanguage: en-US received-spf: Pass (protection.outlook.com: domain of outlook.com designates 40.92.4.68 as permitted sender) receiver=protection.outlook.com; client-ip=40.92.4.68; helo=NAM02-CY1-obe.outbound.protection.outlook.com; dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=outlook.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Rw/RbBIBBwK6xdzpKDWjonDgO9wtxS9CAlx9ISWiYp4=; b=FSyBbvpXn8hQ4CPyJOZM7t/tivUmRscTVP9YBvghxWwYZCKPsteD9e9I0nkOc09m0ImXXLn20NaWSNAOnlP1ebZqgyHv7SLE5Jz9rGC7Tu8iMHRwhN3SKoH+Hs+VnqmUEc9fgJG0iunsidLg/SJqZHhj6c3s65ajnTFZVqUR9/8TQXBvs4Vor/6eDHEpEfZOxS2ljgc8iFAoKhW0eI4HYXeL9Oqzz+rkPdJ6TrstcY7tX+qsa+VEGN3zlQHup9/xocgNbaV0mfTL/XqY22f8oRpqb24j2QuCPqEotBp0bORVmmix0JbRfoUiNSeA8GPn94NkFBUhEGb1nh28Rbk+Fg== authentication-results: spf=pass (sender IP is 40.92.4.68) smtp.mailfrom=outlook.com; mit.edu; dkim=pass (signature was verified) header.d=outlook.com; mit.edu; dmarc=pass action=none header.from=outlook.com; errors-to: piclist-bounces@mit.edu list-id: "Microcontroller discussion list - Public." list-post: x-beenthere: piclist@mit.edu x-mailman-version: 2.1.6 x-topics: [PIC] x-mime-autoconverted: from quoted-printable to 8bit by PCH.mit.edu id 0721PZpo017378 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Hi Sergio, Thank you very much for this. I appreciate the detail, since I am not a nat= ural programmer, it helps me a lot to see the right way to move when I impr= ove this library. Which I will when I do the next project that needs it. Ri= ght now I must press on to finish this one. Many thanks. Bob ________________________________________ From: piclist-bounces@mit.edu on behalf of sergio= =20 Sent: Friday, July 31, 2020 2:00 AM To: Microcontroller discussion list - Public. Subject: Re: [PIC] Simple GCC question Sorry I'm late to the party but for future reference this might help: Try to convert the offending function to a macro and use that to initialise your structure. Looking at the "Lcd_create" function I can see that all it is doing is initialising a copy of the structure, calling "Lcd_init" with a pointer to that copy and then returning the copy to be copied into the actual "lcd" structure (all (mostly) incredibly redundent and inefficent). What I would do in this instance is remove the call to "Lcd_init" from "Lcd_create" and then convert "Lcd_create" to a macro thus: #define LCD_CREATE( \ port, pin, \ rs_port, rs_pin, \ en_port, en_pin, mode) \ { \ .mode =3D mode, \ \ .en_pin =3D en_pin, \ .en_port =3D en_port, \ \ .rs_pin =3D rs_pin, \ .rs_port =3D rs_port, \ \ .data_pin =3D pin, \ .data_port =3D port \ } BEWARE: the "\" at the end of the line should be the last character before the newline. An unseen space such as "\ " will cause problems Your code should then change to this: Lcd_PortType ports[] =3D { LCD_D4_GPIO_Port, LCD_D5_GPIO_Port, LCD_D6_GPIO_Port, LCD_D7_GPIO_Port }; Lcd_PinType pins[] =3D { LCD_D4_Pin, LCD_D5_Pin, LCD_D6_Pin, LCD_D7_Pin }; /* can't remember if you need the "\" below */ Lcd_HandleTypeDef lcd =3D LCD_CREATE(ports, \ pins, \ LCD_RS_GPIO_Port, \ LCD_RS_Pin, \ LCD_EN_GPIO_Port, \ LCD_EN_Pin, \ LCD_4_BIT_MODE); void main(void) { /* Initialise the lcd here */ Lcd_init(&lcd); /* Other stuff */ for(;;); } I haven't actually tried compiling this (too much effort) but it should work :-) Regards Sergio Masci On Fri, 31 Jul 2020, Bob Blick wrote: > Hi Mike, > > That was simple, and not only did it compile, but my LCD display still wo= rks. > > Thank you so much and thanks for all the other answers! > > Bob > > ________________________________________ > From: piclist-bounces@mit.edu on behalf of Mike > Sent: Thursday, July 30, 2020 2:36 PM > To: piclist@mit.edu > Subject: Re: [PIC] Simple GCC question > > I strongly suspect the non-constant value GCC is complaining about is > the return value from Lcd_create(). Since the lcd structure is being > initialased via a function call you cannot do this outside of a > function. You can however define an instance of the lcd struct outside > of a function, and then initialise it later. > > Something like this should work: > > main.c > > #include > > /* These can be initialised outside a function since the initialisers are= constant */ > Lcd_PortType ports[] =3D {LCD_D4_GPIO_Port, LCD_D5_GPIO_Port, LCD_D6_GPIO= _Port, LCD_D7_GPIO_Port}; > Lcd_PinType pins[] =3D {LCD_D4_Pin, LCD_D5_Pin, LCD_D6_Pin, LCD_D7_Pin}; > /* You can only define an instance of Lcd_HandleTypeDef here, since the i= nitialiser is a function */ > Lcd_HandleTypeDef lcd; > > void main(void) > { > /* Initialise the lcd here */ > lcd =3D Lcd_create(ports, pins, LCD_RS_GPIO_Port, LCD_RS_Pin, LCD_EN_GP= IO_Port, LCD_EN_Pin, LCD_4_BIT_MODE); > > /* Other stuff */ > for(;;); > } > > > mymodule.c > > #include > > /* Declare the lcd struct defined in main */ > extern Lcd_HandleTypeDef lcd; > > void myfunction(void) > { > Lcd_clear(&lcd); > } > > > This is the simplest way I could show this, but using global variables > isn't typically the nicest way to do things. > > Regards > > Mike > > > On 30/07/2020 17:26, Bob Blick wrote: >> I'm using a nice little HD44780 LCD library in an STM32 project, it work= s great but I'm stuck with a variable scope problem that I can only seem to= make worse because GCC is a bit stricter than I want it to be at this poin= t. Sorry if the code example doesn't wordwrap properly. >> >> If, somewhere in main() I create an instance of the LCD like this: >> Lcd_PortType ports[] =3D {LCD_D4_GPIO_Port, LCD_D5_GPIO_Port, LCD_D6_= GPIO_Port, LCD_D7_GPIO_Port}; >> Lcd_PinType pins[] =3D {LCD_D4_Pin, LCD_D5_Pin, LCD_D6_Pin, LCD_D7_Pi= n}; >> Lcd_HandleTypeDef lcd =3D Lcd_create(ports, pins, LCD_RS_GPIO_Port, L= CD_RS_Pin, LCD_EN_GPIO_Port, LCD_EN_Pin, LCD_4_BIT_MODE); >> >> then I can use it like this: >> Lcd_cursor(&lcd, 1,6); >> >> but of course I can't do that from within another function since "lcd" o= nly exists in main() >> >> If I try to make it all global by creating lcd outside of main(), GCC do= esn't like that because initializer elements must be constants and it doesn= 't consider "ports" and "pins" to be constants. >> >> Not being a real C programmer this has stopped me in my tracks. If I was= stuck on a deserted island I would use this library as a basis for somethi= ng with ports and pins hardcoded, but I'm hoping a real expert would give m= e some gentle help, which will certainly educate me about doing things the = correct way and also get me past this little hurdle. >> >> Thanks, Bob > --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .