在网上爬行了很久才找到一个介绍USBMEM_BOOTLOADER的且有源代码的论坛,用KEIL 编译后结果不能运行,后来才找到问题的根源:
//usbhw.c
void USB_Init (void) {
PINSEL1 &= ~0xC000C000;
//PINSEL1 |= 0x40004000; //这是原来的程序
PINSEL1 |= 0x80004000; /* Select USB Link, VBUS */
原来得改一下才能用到我的板子上面。
另外,原来的程序是在复位时检测P0.15是否为低来判断是进入用户程序,还是USB IAP程序,我针对我的板子改了一下,我用了两个按钮来控制它,当按下P0.21的按钮时运行用户程序,当按下P0.22的按钮时则运行USB IAP程序:
首先要在sbl_config.h中做一下修改:
#define USER_START_SECTOR 2 //用户程序起始扇区
#define MAX_USER_SECTOR 26 //最大扇区 LPC2146是 14,LPC2148是26
#define ISP_ENTRY_GPIO_REG 0xE0028000 /* Port */
#define ISP_ENTRY_PIN21 21 /* User Code Pin */ //这里原来是 15
#define ISP_ENTRY_PIN22 22 /* USB IAP Pin */ //这个是增加的
然后把sbl_iap.c里面的相关内容修改一下:
void check_isp_entry_pin(void)
{
while(1)
{
if(!( (*(volatile unsigned *)ISP_ENTRY_GPIO_REG) & (0x1<<isp_entry_pin21)) )[="" size][="" font][="" backcolor][="" color]
{
execute_user_code(); break;
}
if(!( (*(volatile unsigned *)ISP_ENTRY_GPIO_REG) & (0x1<<isp_entry_pin22)) )[="" size][="" font][="" backcolor][="" color]
{
break; // Enter ISP mode
}
}
}
现在就可以来实验一下了:
usbmem程序的设置
boot程序按照这个设置编译程序,要输出HEX文件,然后试用ISP工具将其下载到LPC2148中。
现在按一下复位按钮,按K1(P0.21)没有反应,因为现在还没有用户程序;按一下K2(P0.22),指示灯亮了,此时电脑会显示发现了新硬件:
电脑中出现了一个可移动磁盘
打开这个磁盘,会发现里面有一个文件是492K,
得把这个文件删除掉才能拷贝入你自己的程序
然后再编译用户程序,要把起始位置修改为0x2000;同时要输出BIN文件:(在user run 写入fromelf --bin .FlashBlinky.axf -o .FlashBlinky.bin)把得到的BIN文件拷贝入空白的磁盘:
拷入用户程序
按一下复位键,再按一下K1,就开始运行流水灯程序了。
单片机源程序如下:
- /*----------------------------------------------------------------------------
- * U S B - K e r n e l
- *----------------------------------------------------------------------------
- * Name: USBUSER.C
- * Purpose: USB Custom User Module
- * Version: V1.10
- *----------------------------------------------------------------------------
- * This software is supplied "AS IS" without any warranties, express,
- * implied or statutory, including but not limited to the implied
- * warranties of fitness for purpose, satisfactory quality and
- * noninfringement. Keil extends you a royalty-free right to reproduce
- * and distribute executable files created using this software for use
- * on Philips LPC2xxx microcontroller devices only. Nothing else gives
- * you the right to use this software.
- *
- * Copyright (c) 2005-2006 Keil Software.
- *---------------------------------------------------------------------------*/
- #include <LPC214X.H> /* LPC214x definitions */
- #include "type.h"
- #include "usb.h"
- #include "usbcfg.h"
- #include "usbhw.h"
- #include "usbcore.h"
- #include "usbuser.h"
- #include "mscuser.h"
- #include "memory.h"
- /*
- * USB Power Event Callback
- * Called automatically on USB Power Event
- * Parameter: power: On(TRUE)/Off(FALSE)
- */
- #if USB_POWER_EVENT
- void USB_Power_Event (BOOL power) {
- }
- #endif
- /*
- * USB Reset Event Callback
- * Called automatically on USB Reset Event
- */
- #if USB_RESET_EVENT
- void USB_Reset_Event (void) {
- USB_ResetCore();
- IOCLR1 = LED_CFG; /* Turn Off Cfg LED */
- }
- #endif
- /*
- * USB Suspend Event Callback
- * Called automatically on USB Suspend Event
- */
- #if USB_SUSPEND_EVENT
- void USB_Suspend_Event (void) {
- IOSET1 = LED_SUSP; /* Turn On Suspend LED */
- }
- #endif
- /*
- * USB Resume Event Callback
- * Called automatically on USB Resume Event
- */
- #if USB_RESUME_EVENT
- void USB_Resume_Event (void) {
- IOCLR1 = LED_SUSP; /* Turn Off Suspend LED */
- }
- #endif
- /*
- * USB Remote Wakeup Event Callback
- * Called automatically on USB Remote Wakeup Event
- */
- #if USB_WAKEUP_EVENT
- void USB_WakeUp_Event (void) {
- }
- #endif
- /*
- * USB Start of Frame Event Callback
- * Called automatically on USB Start of Frame Event
- */
- #if USB_SOF_EVENT
- void USB_SOF_Event (void) {
- }
- #endif
- /*
- * USB Error Event Callback
- * Called automatically on USB Error Event
- * Parameter: error: Error Code
- */
- #if USB_ERROR_EVENT
- void USB_Error_Event (DWORD error) {
- }
- #endif
- /*
- * USB Set Configuration Event Callback
- * Called automatically on USB Set Configuration Request
- */
- #if USB_CONFIGURE_EVENT
- void USB_Configure_Event (void) {
- if (USB_Configuration) { /* Check if USB is configured */
- IOSET1 = LED_CFG; /* Turn On Cfg LED */
- } else {
- IOCLR1 = LED_CFG; /* Turn Off Cfg LED */
- }
- }
- #endif
- /*
- * USB Set Interface Event Callback
- * Called automatically on USB Set Interface Request
- */
- #if USB_INTERFACE_EVENT
- void USB_Interface_Event (void) {
- }
- #endif
- /*
- * USB Set/Clear Feature Event Callback
- * Called automatically on USB Set/Clear Feature Request
- */
- #if USB_FEATURE_EVENT
- void USB_Feature_Event (void) {
- }
- #endif
- #define P_EP(n) ((USB_EP_EVENT & (1 << (n))) ? USB_EndPoint##n : NULL)
- /* USB Endpoint Events Callback Pointers */
- void (* const USB_P_EP[16]) (DWORD event) = {
- P_EP(0),
- P_EP(1),
- P_EP(2),
- P_EP(3),
- P_EP(4),
- P_EP(5),
- P_EP(6),
- P_EP(7),
- P_EP(8),
- P_EP(9),
- P_EP(10),
- P_EP(11),
- P_EP(12),
- P_EP(13),
- P_EP(14),
- P_EP(15),
- };
- /*
- * USB Endpoint 1 Event Callback
- * Called automatically on USB Endpoint 1 Event
- * Parameter: event
- */
- void USB_EndPoint1 (DWORD event) {
- }
- /*
- * USB Endpoint 2 Event Callback
- * Called automatically on USB Endpoint 2 Event
- * Parameter: event
- */
- void USB_EndPoint2 (DWORD event) {
- switch (event) {
- case USB_EVT_OUT:
- MSC_BulkOut();
- break;
- case USB_EVT_IN:
- MSC_BulkIn();
- break;
- }
- }
- /*
- * USB Endpoint 3 Event Callback
- * Called automatically on USB Endpoint 3 Event
- * Parameter: event
- */
- void USB_EndPoint3 (DWORD event) {
- }
- /*
- * USB Endpoint 4 Event Callback
- * Called automatically on USB Endpoint 4 Event
- * Parameter: event
- */
- void USB_EndPoint4 (DWORD event) {
- }
- /*
- * USB Endpoint 5 Event Callback
- * Called automatically on USB Endpoint 5 Event
- * Parameter: event
- */
- void USB_EndPoint5 (DWORD event) {
- }
- /*
- * USB Endpoint 6 Event Callback
- * Called automatically on USB Endpoint 6 Event
- * Parameter: event
- */
- void USB_EndPoint6 (DWORD event) {
- }
- /*
- * USB Endpoint 7 Event Callback
- * Called automatically on USB Endpoint 7 Event
- * Parameter: event
- */
- void USB_EndPoint7 (DWORD event) {
- }
- /*
- * USB Endpoint 8 Event Callback
- * Called automatically on USB Endpoint 8 Event
- * Parameter: event
- */
- void USB_EndPoint8 (DWORD event) {
- }
- /*
- * USB Endpoint 9 Event Callback
- * Called automatically on USB Endpoint 9 Event
- * Parameter: event
- */
- void USB_EndPoint9 (DWORD event) {
- }
- /*
- * USB Endpoint 10 Event Callback
- * Called automatically on USB Endpoint 10 Event
- * Parameter: event
- */
- void USB_EndPoint10 (DWORD event) {
- }
- /*
- * USB Endpoint 11 Event Callback
- * Called automatically on USB Endpoint 11 Event
- * Parameter: event
- */
- void USB_EndPoint11 (DWORD event) {
- }
- /*
- * USB Endpoint 12 Event Callback
- * Called automatically on USB Endpoint 12 Event
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
USB IAP 程序.rar
(427.99 KB, 下载次数: 18)
|