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; Thu, 30 Jul 2020 17:52:08 -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 06V0hHDo015134; Thu, 30 Jul 2020 20:43:56 -0400 Received: from outgoing-exchange-7.mit.edu (OUTGOING-EXCHANGE-7.MIT.EDU [18.9.28.58]) by PCH.mit.edu (8.14.7/8.12.8) with ESMTP id 06V0hFil015131 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Thu, 30 Jul 2020 20:43:15 -0400 Received: from w92exedge4.exchange.mit.edu (W92EXEDGE4.EXCHANGE.MIT.EDU [18.7.73.16]) by outgoing-exchange-7.mit.edu (8.14.7/8.12.4) with ESMTP id 06V0fC01003740 for ; Thu, 30 Jul 2020 20:41:26 -0400 Received: from oc11expo14.exchange.mit.edu (18.9.4.19) by w92exedge4.exchange.mit.edu (18.7.73.16) with Microsoft SMTP Server (TLS) id 15.0.1293.2; Thu, 30 Jul 2020 20:42:37 -0400 Received: from oc11exhyb7.exchange.mit.edu (18.9.1.112) by oc11expo14.exchange.mit.edu (18.9.4.19) with Microsoft SMTP Server (TLS) id 15.0.1365.1; Thu, 30 Jul 2020 20:43:07 -0400 Received: from NAM04-CO1-obe.outbound.protection.outlook.com (104.47.45.51) by oc11exhyb7.exchange.mit.edu (18.9.1.112) with Microsoft SMTP Server (TLS) id 15.0.1395.4 via Frontend Transport; Thu, 30 Jul 2020 20:43:07 -0400 Received: from MWHPR17CA0071.namprd17.prod.outlook.com (2603:10b6:300:93::33) by BYAPR01MB4246.prod.exchangelabs.com (2603:10b6:a03:14::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.24; Fri, 31 Jul 2020 00:43:04 +0000 Received: from CO1NAM03FT034.eop-NAM03.prod.protection.outlook.com (2603:10b6:300:93:cafe::4e) by MWHPR17CA0071.outlook.office365.com (2603:10b6:300:93::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.16 via Frontend Transport; Fri, 31 Jul 2020 00:43:04 +0000 Received: from NAM12-MW2-obe.outbound.protection.outlook.com (40.92.23.63) by CO1NAM03FT034.mail.protection.outlook.com (10.152.80.177) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3216.10 via Frontend Transport; Fri, 31 Jul 2020 00:43:03 +0000 Received: from BN8NAM12FT065.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc66::4d) by BN8NAM12HT229.eop-nam12.prod.protection.outlook.com (2a01:111:e400:fc66::340) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.14; Fri, 31 Jul 2020 00:43:02 +0000 Received: from BYAPR02MB4055.namprd02.prod.outlook.com (2a01:111:e400:fc66::51) by BN8NAM12FT065.mail.protection.outlook.com (2a01:111:e400:fc66::134) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3239.14 via Frontend Transport; Fri, 31 Jul 2020 00:43:02 +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.3216.034; Fri, 31 Jul 2020 00:43:02 +0000 From: Bob Blick To: Microcontroller discussion list - Public. Sender: "piclist-bounces@mit.edu" Date: Thu, 30 Jul 2020 17:43:02 -0700 Subject: Re: [PIC] Simple GCC question Thread-Topic: [PIC] Simple GCC question Thread-Index: AQHWZotc+nNhZovUu0iBK4Bhj1/IWakgpd6AgAAPvzM= 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: Pass 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.23.63 as permitted sender) receiver=protection.outlook.com; client-ip=40.92.23.63; helo=NAM12-MW2-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=VAszFPmuQZeRFVdz2YIIVx7k93f5zgXlTsIIeqHPWyk=; b=Vacq6zVoGge1actm/JJKyEKqIg2Gsjj4G5XE+0y718/xaVLptBh11Oqqu3R7ibov0mKiHrbEZBt8r65EQF2Phk20TOrgcKix/I8YPO1NHm2lnEtTarfyVVHBULBR8Z9FGm2mij7lvygztazmz3PjvBkqERLl49UZDT5M/qOyn/WJhlf81YXgu3o/S1v2D9Tc1gJ6TCpuk5kPzwACNmRRCw8/sE1K9+LbskUbpnBM8OL6HPvp10n1bwdRnIoLwquqfNIo1QlJ1adJC4vGLMLxzAEHwvBTTB8vua2NaOuzNJLZzpDqPuzZlU/Z2g+fmMMEzzyfFLojTir7MaLCZMVmwA== authentication-results: spf=pass (sender IP is 40.92.23.63) 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 06V0hFil015131 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Hi Mike, That was simple, and not only did it compile, but my LCD display still work= s. Thank you so much and thanks for all the other answers! Bob ________________________________________ From: piclist-bounces@mit.edu on behalf of Mike=20 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 c= onstant */ Lcd_PortType ports[] =3D {LCD_D4_GPIO_Port, LCD_D5_GPIO_Port, LCD_D6_GPIO_P= ort, 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 ini= tialiser 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_GPI= O_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 works= 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 point= .. 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_G= PIO_Port, LCD_D7_GPIO_Port}; > Lcd_PinType pins[] =3D {LCD_D4_Pin, LCD_D5_Pin, LCD_D6_Pin, LCD_D7_Pin= }; > Lcd_HandleTypeDef lcd =3D Lcd_create(ports, pins, LCD_RS_GPIO_Port, LC= D_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" on= ly exists in main() > > If I try to make it all global by creating lcd outside of main(), GCC doe= sn'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 somethin= g with ports and pins hardcoded, but I'm hoping a real expert would give me= some gentle help, which will certainly educate me about doing things the c= orrect 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 .