📄 device_new.c
字号:
//
// Arguments:
// None.
//
// Return:
// None.
//
// Since:
// fmc-1.0
//------------------------------------------------------------------------------
static void dev_power_up(void) reentrant;
static void dev_power_up() reentrant
{
uint8 ps;
TRACE0(4, dev, 1, "dev_power_up()" );
// temporary - disable card status change irq's on power down, enable on power up
irq_control(k_irq_crd_sts_chg, kbm_irqctl_clear |kbm_irqctl_unmask);
ps = _mcu_register_rd(x_crd_ps);
// compact flash
#if (k_log_lun_cf < k_max_log_lun)
if (ps & kbm_crd_ps_cf)
{
TRACE0(5, dev, 0, "_cf_pwr_on()") ;
_cf_pwr_on();
_inserted(k_lun_cf);
#ifdef k_pfm_led
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio13);
#endif
}
#endif
// memory stick
#if (k_log_lun_ms < k_max_log_lun)
if (ps & kbm_crd_ps_ms)
{
_ms_pwr_on();
_inserted(k_lun_ms);
#ifdef k_pfm_led
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio12);
#endif
}
#endif
// smart media
#if (k_log_lun_sm < k_max_log_lun)
if (ps & kbm_crd_ps_sm)
{
_sm_pwr_on();
_inserted(k_lun_sm);
#ifdef k_pfm_led
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio14);
#endif
}
#endif
// secure digital / multi media card
#if (k_log_lun_sd < k_max_log_lun)
_sd_pwr_off();
#ifdef k_pfm_led
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio15);
#endif
_sd_inserted_ejected();
_lun_map_log2phy(k_log_lun_sd, k_lun_sd);
#endif
// nand drive
#if (k_log_lun_nand < k_max_log_lun)
_nand_pwr_on();
// $$$ cds - this doesn't really make sense for a fixed drive.
// Will have to figure something out... like moving this to a lun-specific
// location
_inserted(k_lun_nand);
#ifdef k_pfm_led
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio14);
#endif
#endif
}
//+-----------------------------------------------------------------------------
// Name:
// dev_power_dn
//
// Declaration:
// void dev_power_dn(void);
//
// Purpose:
// Called on set-config(0), and suspend if configuration is non-zero.
// Power off the flash media sockets and mark the media as ejected.
//
// Arguments:
// None.
//
// Return:
// None.
//
// Since:
// fmc-1.0
//------------------------------------------------------------------------------
static void dev_power_dn(void) reentrant;
static void dev_power_dn() reentrant
{
TRACE0(6, dev, 1, "dev_power_dn()" );
// temporary - disable card status change irq's on power down, enable on power up
irq_control(k_irq_crd_sts_chg, kbm_irqctl_mask);
// simulate eject on all cards
_ejected(k_lun_cf);
_ejected(k_lun_ms);
_ejected(k_lun_sm);
//_sd_inserted_ejected();
// turn off all power to cards, indicator leds, etc
gpio_clrbit(0);
_mcu_register_set_bits(x_gpiob_out, kbm_gpio8 |kbm_gpio9 |kbm_gpio10 |kbm_gpio11 |kbm_gpio12 |kbm_gpio13 |kbm_gpio14 |kbm_gpio15);
#ifdef k_pfm_led
// turn off the activity/inserted LEDs
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio12 |kbm_gpio13 |kbm_gpio14 |kbm_gpio15);
#endif
}
//------------------------------------------------------------------------------
// prototypes
// top level state machine for built only transport
static void dev_thread_wait_csw(void) reentrant;
static void dev_thread_wait_cbw(void) reentrant;
static void dev_thread_wait_create1(void) reentrant;
static void dev_thread_wait_create(void) reentrant;
//------------------------------------------------------------------------------
// Declaration:
// t_result dev_mngr(t_message *msgp);
//
// Purpose:
// This is the manager for all things related to the whole device.
//
// Arguments:
// None.
//
// Return:
// k_success
// k_error
//
// Note:
// Do NOT yield from this routine. It is NOT part of any thread. It does
// not execute in an any thread's context. Yielding from within this function
// will cause your firmware to crash.
//
// Since:
// u2dp-1.0
//------------------------------------------------------------------------------
t_result dev_mngr(t_message_ref msgp) reentrant
{
uint8 lun;
TRACE0(31, dev, 0, "dev_mngr()");
switch (message_rd_id(msgp))
{
case k_msg_initialize:
TRACE0(32, dev, 0, "k_msg_initialize");
//fmc_dump_registers();
// create threads
g_ix_dev_thread = thread_create(dev_thread_wait_create, NULL, k_yes);
// set up the gpio interface
gpio_initialize();
// initialize the application
dev_initialize();
// set the output bits, turn them all off
gpio_bit_is_output(0);
gpio_bit_is_output(5);
_mcu_register_set_bits(x_gpiob_dir, kbm_gpio8 |kbm_gpio9 |kbm_gpio10 |kbm_gpio11 |kbm_gpio12 |kbm_gpio13 |kbm_gpio14 |kbm_gpio15);
_mcu_register_set_bits(x_gpiob_out, kbm_gpio8 |kbm_gpio9 |kbm_gpio10 |kbm_gpio11 |kbm_gpio12 |kbm_gpio13 |kbm_gpio14 |kbm_gpio15);
// data access led off
gpio_clrbit(0); // 0 is off
gpio_setbit(5); // 1 is off
// card power
#if (k_log_lun_cf < k_max_log_lun)
TRACE0(33, dev, 0, "_cf_pwr_off()") ;
_cf_pwr_off();
#endif
#if (k_log_lun_ms < k_max_log_lun)
_ms_pwr_off();
#endif
#if (k_log_lun_sm < k_max_log_lun)
_sm_pwr_on(); // must be on at power up to initialize inserted card, if present
#endif
#if (k_log_lun_sd < k_max_log_lun)
_sd_pwr_off();
#endif
#if (k_log_lun_nand < k_max_log_lun)
_nand_pwr_on(); // must be on at power up to initialize chips & retrieve boot block
#endif
#ifdef k_pfm_demo
// turn off the debug lights of science
_mcu_register_set_bits(x_gpiob_out, kbm_gpio12 |kbm_gpio13 |kbm_gpio14 |kbm_gpio15);
#endif
#ifdef k_pfm_led
// turn off the activity/inserted LEDs
_mcu_register_clr_bits(x_gpiob_out, kbm_gpio12 |kbm_gpio13 |kbm_gpio14 |kbm_gpio15);
#endif
#ifdef k_pfm_profile
_profile_initialize();
#endif
// initialize the application specific interface managers
mscbot_mngr(msgp);
// setup the default logical to physical lun mapping
if (k_log_lun_cf < k_max_log_lun)
{
_lun_map_log2phy(k_log_lun_cf, k_lun_cf);
}
if (k_log_lun_ms < k_max_log_lun)
{
_lun_map_log2phy(k_log_lun_ms, k_lun_ms);
}
if (k_log_lun_sm < k_max_log_lun)
{
_lun_map_log2phy(k_log_lun_sm, k_lun_sm);
}
if (k_log_lun_sd < k_max_log_lun)
{
_lun_map_log2phy(k_log_lun_sd, k_lun_sd);
}
if (k_log_lun_nand < k_max_log_lun)
{
_lun_map_log2phy(k_log_lun_nand, k_lun_nand);
}
// initialize the physical luns
for (lun=0; lun<k_max_phy_lun; lun++)
{
strncpy(_lun_data(g_active_lun, device_id), "FLASH CARD", k_lun_max_devid_sz);
_lun_data(lun, sensep) = &sense_por;
_lun_data(lun, media) = kbm_lun_media_unknown |kbm_lun_media_removable;
_lun_data(lun, device_type) = k_device_type_none;
}
// initialize the logical luns
for (lun=0; lun<k_max_log_lun; lun++)
{
g_active_lun = _lun_log2phy(lun);
_lun_reset_controller(g_active_lun)();
_lun_initialize_controller(g_active_lun)();
}
// before connecting, try to detect eeprom w/ valid serialnumber. we need to
// know _before_ the host tries to enumerate us and asks for device descriptors
dev_validate_nvram();
return k_success;
case k_msg_usbrst:
TRACE0(34, dev, 0, "k_msg_usbrst");
// propagate the message to the application specific interface managers
dfu_mngr(msgp);
mscbot_mngr(msgp);
// clear this -after- the interface managers process the usbrst
dev_wr_most_recent_config(0);
//dev_power_dn();
thread_clr_sync_ex(g_ix_dev_thread, kbm_sync_usbrst |kbm_sync_abort);
return k_success;
case k_msg_resume:
TRACE0(35, dev, 0, "k_msg_resume");
// on windows, there is always a usbrst following resume.
// so, there is no sense powering up from the resume and then
// immediately powering down from the reset.
// but, don't power up peripherals until after set config, anyhow.
if (!g_usbrst)
if (sie_rd_configuration())
dev_power_up();
// propagate the message to the application specific interface managers
mscbot_mngr(msgp);
return k_success;
case k_msg_suspend:
TRACE0(36, dev, 0, "k_msg_suspend");
// propagate the message to the application specific interface managers
mscbot_mngr(msgp);
// avoid the initial suspend after por because it causes cards detected
// upon por to be lost (after pwrdn/pwrup the insert irq doesn't fire)
if (!g_usbrst)
if (sie_rd_configuration())
dev_power_dn();
return k_success;
}
// unexpected message
TRACE0(37, dev, 0, "warning: dev_mngr() unexpected message");
return k_error;
}
//------------------------------------------------------------------------------
// Declaration:
// t_result dev_cpex(t_message_ref msgp);
//
// Purpose:
// Handles requests targeted to the device: standard, class, vendor.
//
// Arguments:
// None.
//
// Return:
// k_success - Causes the protocol engine to complete the status stage successfully.
// k_error - Causes the protocol engine to stall the control pipe.
// k_in_progress - For k_msg_source_payload, causes the protocol engine to deliver
// another empty packet buffer to the cpex to be loaded for transmission.
// For k_msg_sink_payload, informs the protocol engine that the cpex expects at
// least one more payload buffer to be delivered to it by the protocol engine.
// k_finished - For k_msg_source_payload and k_msg_sink_payload, informs the
// protocol engine that the data phase is complete and no more data is expected
// in either direction.
//
// Note:
// Do NOT yield from this routine. It is part of the protocol engine's thread.
// It executes in the context of g_ix_ctl_thread. Yielding from within this
// function will cause the protocol engine's state machine to float belly up.
// And that will cause your firmware to shuffle off this mortal coil, post haste.
//
// Since:
// u2dp-1.0
//------------------------------------------------------------------------------
t_result dev_cpex(t_message_ref msgp) reentrant
{
uint8 pktsz;
uint8 pnr;
t_usb_rqst *rqstp;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -