Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Question about type casting |
| Date | 2015-09-16 20:01 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <874miudw96.fsf@bsb.me.uk> (permalink) |
| References | <2fa66797-9877-4310-8ec4-dd52ccd37048@googlegroups.com> |
fl <rxjwg98@gmail.com> writes:
> I read an example .c embedded firmware project, which has a device driver
> level library. Here is the board level .c file, which has some board specific
> variable definitions:
>
> This line is from a header file:
> typedef uint32_t GPIO_PinConfig;
>
> Below is from the board level .c file:
> GPIO_PinConfig gpioPinConfigs[] = {
> /* Input pins */
> /* EK_TM4C1294XL_USR_SW1 */
> GPIOTiva_PJ_0 | GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_RISING,
> /* EK_TM4C1294XL_USR_SW2 */
> GPIOTiva_PJ_1 | GPIO_CFG_IN_PU | GPIO_CFG_IN_INT_RISING,
>
> /* Output pins */
> /* EK_TM4C1294XL_USR_D1 */
> GPIOTiva_PN_1 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH,
> /* EK_TM4C1294XL_USR_D2 */
> GPIOTiva_PN_0 | GPIO_CFG_OUT_STD | GPIO_CFG_OUT_STR_HIGH
> };
>
> /* The device-specific GPIO_config structure */
> const GPIOTiva_Config GPIOTiva_config = {
> .pinConfigs = (GPIO_PinConfig *) gpioPinConfigs,
> .callbacks = (GPIO_CallbackFxn *) gpioCallbackFunctions,
> .numberOfPinConfigs = sizeof(gpioPinConfigs) / sizeof(GPIO_PinConfig),
> .numberOfCallbacks = sizeof(gpioCallbackFunc)/sizeof(GPIO_CallbackFxn),
> .intPriority = (~0)
> };
>
> Here is from the library definition (.c file):
> /* Device specific interpretation of the GPIO_PinConfig content */
> typedef struct PinConfig {
> uint8_t pin;
> uint8_t port;
> uint16_t config;
> } PinConfig;
>
> void GPIO_clearInt(unsigned int index)
> {
> PinConfig *config = (PinConfig *) &GPIOTiva_config.pinConfigs[index];
> ...
> }
>
> I have a question about the casting of (PinConfig *) from the last line.
> 'GPIOTiva_config.pinConfigs[index]' has been initialized with
> gpioPinConfigs, which is in fact a 4 unsigned int array. Now, in the last
> line, it is cast to a PinConfig, which has two uint_8t one uint16_t
> struct, pointer. '4 unsigned int' is obvious different from struct
> PinConfig.
>
> Could you help me understanding this casting?
The author(s) want(s) to have have two different views of the same data.
For some purposes they want to view the config as an uint23_t, probably
to make the setting using an "or" of some constants easy, but at other
times they want to view it as two bytes and a short integer.
There are all sorts of pitfalls doing this, but since the code is very
system-specific, no doubt they've checked that it will all work with the
compiler they are using.
It's usually better to do this using a union because the cast is a lie
since the data in question are really uint32_t objects not PinConfig
objects.
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Question about type casting fl <rxjwg98@gmail.com> - 2015-09-16 10:52 -0700
Re: Question about type casting Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-16 20:01 +0100
Re: Question about type casting David Brown <david.brown@hesbynett.no> - 2015-09-17 09:02 +0200
Re: Question about type casting supercat@casperkitty.com - 2015-09-17 12:59 -0700
csiph-web