c - Redefining a Declared (undefined?) Struct as Another Struct -
from question: "accessing member of pointer (variable) struct within struct". use (undefined?) struct accept (unknown type?) parameters: c function configures struct contains many members, *base (pointer struct),id,mode; base struct might defined (say) "struct a", "struct b", "...", depending of interface. after discussion in previous question, arrived struct declaration:
typedef struct _interface_t{ struct periph_base * base; u32_t id; u32_t mode; } interface_t; //typo edit interface_t usart0_dev = {(struct periph_base *)at91c_base_us0, at91c_id_us0, 0}; // <-- at91c_base_us0 struct *base interface_t tc0_dev = {(struct periph_base *)at91c_base_tc0, at91c_id_tc0, 0}; // <-- at91c_base_tc0 struct *base interface_t twi_dev = {(struct periph_base *)at91c_base_twi, at91c_id_twi, 0}; // <-- at91c_base_twi struct *base ... unsigned char configuredevice(interface_t interface, u32_t config, u32_t speed, u32_t irq_trigger, u32_t irq_mode, void (*interface_irq_handler)(void)){ ... usart_configure(interface, config, speed, peripherals_clock); //error0 if (*interface_irq_handler != null){ ((at91s_usart)interface->base)->us_ier = irq_trigger; //error1 } usart_settransmitterenabled(interface->base, 1); //error1 usart_setreceiverenabled(interface->base, 1); //error1 }
in way eclipse code editor did not throw warning or error, builded project , compiler complained:
incompatible type argument 1 of 'usart_configure'
(error0)
invalid type argument of '->' (have 'interface_t')
(error1)
before recieving following error: "cast union type type not present in union"
;
so, think maybe ater know type of base
struct i'm "recieving" (at91s_pwmc, at91s_usart, at91s_aic,... :please read accessing member of pointer (variable) struct within struct) can (re)define periph_base struct been recieved struct. please correct me if i'm wrong...
best regards!, in advance answers!
edit: adding usart_configure code:
void usart_configure(at91s_usart *usart, unsigned int mode, unsigned int baudrate, unsigned int masterclock) { // reset , disable receiver & transmitter usart->us_cr = at91c_us_rstrx | at91c_us_rsttx | at91c_us_rxdis | at91c_us_txdis; // configure mode usart->us_mr = mode; // configure baudrate // asynchronous, no oversampling if (((mode & at91c_us_sync) == 0) && ((mode & at91c_us_over) == 0)) { usart->us_brgr = (masterclock / baudrate) / 16; } }
edit 2: full code
#include "config_file.h" #include "board.h" #include "aic/aic.h" #include "pmc/pmc.h" #include "twi/twi.h" #include "usart/usart.h" typedef volatile unsigned int u32_t; typedef struct _interface_t{ struct periph_base * base; u32_t id; u32_t mode; } interface_t; interface_t usart0_dev = {(struct periph_base *)at91c_base_us0, at91c_id_us0, 0}; interface_t usart1_dev = {(struct periph_base *)at91c_base_us1, at91c_id_us1, 0}; interface_t tc0_dev = {(struct periph_base *)at91c_base_tc0, at91c_id_tc0, 0}; interface_t tc1_dev = {(struct periph_base *)at91c_base_tc1, at91c_id_tc1, 0}; interface_t tc2_dev = {(struct periph_base *)at91c_base_tc2, at91c_id_tc2, 0}; interface_t twi_dev = {(struct periph_base *)at91c_base_twi, at91c_id_twi, 0}; ... unsigned char configuredevice(interface_t *interface, u32_t config, u32_t speed, u32_t irq_trigger, u32_t irq_mode, void (*interface_irq_handler)(void)){ pmc_enableperipheral(interface->id); switch(interface->id){ case at91c_id_fiq: // interface fiq! trace_error("attempt configure fiq system peripheral!"); break; case at91c_id_sys: // interface sys! if (*interface_irq_handler != null){ ((at91s_sys)interface->base)->aic_iecr = irq_trigger; } break; case at91c_id_pioa|at91c_id_piob: // interface pio! // pio_configure(); ///xxx: pio_configure modified if (*interface_irq_handler != null){ ((at91s_pio)interface->base)->pio_ier= irq_trigger; } break; case at91c_id_spi0|at91c_id_spi1: // interface spi! // spi_configure(interface, config, speed, peripherals_clock); //xxx: spi_configure written if (*interface_irq_handler != null){ ((at91s_spi)interface->base)->spi_ier = irq_trigger; } break; case at91c_id_us0|at91c_id_us1: // interface usart! (at91s_usart)interface->base; usart_configure(interface->base, config, speed, peripherals_clock); if (*interface_irq_handler != null){ ((at91s_usart)interface->base)->us_ier = irq_trigger; } usart_settransmitterenabled(interface->base, 1); usart_setreceiverenabled(interface->base, 1); break; case at91c_id_ssc: // interface ssc! if (*interface_irq_handler != null){ ((at91s_ssc)interface->base)->ssc_ier = irq_trigger; } break; case at91c_id_twi: // interface twi! if (*interface_irq_handler != null){ ((at91s_twi)interface->base)->twi_ier = irq_trigger; twi_configuremaster(interface->base, speed, peripherals_clock); } break; case at91c_id_pwmc: // interface pwm! if (*interface_irq_handler != null){ ((at91s_pwmc)interface->base)->pwmc_ier = irq_trigger; } break; case at91c_id_udp: // interface usb! if (*interface_irq_handler != null){ ((at91s_udp)interface->base)->udp_ier = irq_trigger; } break; case at91c_id_tc0|at91c_id_tc1|at91c_id_tc2: // interface tc! unsigned int div, tcclks; pmc_enableperipheral(interface->id); // enable tc0 peripheral clock tc_findmckdivisor(speed, peripherals_clock, &div, &tcclks); // configure tc (u32_t speed) in hertz tc_configure(interface->base, tcclks | config); // interrupt configuration ((at91s_tc)interface->base)->tc_rc = (peripherals_clock / (2 * div)); if (*interface_irq_handler != null){ ((at91s_tc)interface->base)->tc_ier = irq_trigger; } break; case at91c_id_can: // interface can! if (*interface_irq_handler != null){ ((at91s_can)interface->base)->can_ier = irq_trigger; } break; case at91c_id_emac: // interface emac! if (*interface_irq_handler != null){ ((at91s_emac)interface->base)->emac_ier = irq_trigger; } break; case at91c_id_adc: // interface adc! if (*interface_irq_handler != null){ ((at91s_adc)interface->base)->adc_ier = irq_trigger; } break; case at91c_id_irq0|at91c_id_irq1: // interface irq! trace_error("attempt configure irq system peripheral!"); break; default: trace_error("attempt configure undefined irq!"); // unknown interface! break; } return 0; } void configureaic(interface_t *interface, u32_t irq_mode, void (*interface_irq_handler)(void)){ aic_configureit(interface->id, irq_mode, *interface_irq_handler); aic_enableit(interface->id); }
originally question said function took
interface_t * interface
as first parameter. have changed to
interface_t interface
if code, change lines using
interface->
to
interface.
and wild stab in dark, usart_configure
might want base pointer:
usart_configure(interface.base, config, speed, peripherals_clock);
Comments
Post a Comment