【感谢】非常感谢@GrootBrain 大佬的耐心教导,让我学习到了在linux下面开发AC7802x的乐趣。
【实现步聚】
1、在【杰发科技AC7802x测评】1.开箱+linux开发环境搭建+点灯 - - 21ic电子技术开**坛下面下载到工程示例包。我们拿到这个示例包,主要与mdk工程不同的文件有AC78022MBQA_FLASH.ld、Makefile、startup_AC7802x.s这三个文件。
其中startup_AC7802x.s与MDK工程的startup_AC7802x.s区别就是汇编语言的格式不对。
2、用vscode找开工程文件夹后。添加Usre文件夹,下面分别添加Inc和Src文件夹。在Inc下面新建gpio.h,Src下面新建gpio.c。内容如下:
#ifndef _GPIO_H__
#define _GPIO_H__
#include "ac780x_gpio.h"
#define LED1_PORT (GPIOA)
#define LED1_PIN (GPIO_PIN2)
#define LED2_PORT (GPIOA)
#define LED2_PIN (GPIO_PIN3)
/*LED1动作定义.*/
#define LED1_ON do{GPIO_SetPinLevel(LED1_PORT, LED1_PIN, GPIO_LEVEL_HIGH);}while(0)
#define LED1_OFF do{GPIO_SetPinLevel(LED1_PORT, LED1_PIN, GPIO_LEVEL_LOW);}while(0)
#define LED1_TOGGLE do{if(GPIO_GetPinLevel(LED1_PORT, LED1_PIN)){LED1_OFF;}else{LED1_ON;}}while(0)
/*LED2动作定义.*/
#define LED2_ON do{GPIO_SetPinLevel(LED2_PORT, LED2_PIN, GPIO_LEVEL_HIGH);}while(0)
#define LED2_OFF do{GPIO_SetPinLevel(LED2_PORT, LED2_PIN, GPIO_LEVEL_LOW);}while(0)
#define LED2_TOGGLE do{if(GPIO_GetPinLevel(LED2_PORT, LED2_PIN)){LED2_OFF;}else{LED2_ON;}}while(0)
#define KEY_PORT (GPIOA)
#define KEY_PIN (GPIO_PIN11)
#define NMI_PORT (GPIOB)
#define NMI_PIN (GPIO_PIN2)
void GPIO_LedInit(void);
void GPIO_KeyInit(void);
#endif
gpio.c
/* Copyright Statement:
*
* This software/firmware and related documentation ("AutoChips Software") are
* protected under relevant copyright laws. The information contained herein is
* confidential and proprietary to AutoChips Inc. and/or its licensors. Without
* the prior written permission of AutoChips inc. and/or its licensors, any
* reproduction, modification, use or disclosure of AutoChips Software, and
* information contained herein, in whole or in part, shall be strictly
* prohibited.
*
* AutoChips Inc. (C) 2022. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
* RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
* ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
* RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
* INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
* TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
* RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
* OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
* SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
* RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
* ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
* RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
* AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
* CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
*/
/******************************************************************************
* @version: V1.0.0 demo
******************************************************************************/
/*!
* [url=home.php?mod=space&uid=288409]@file[/url] gpio.c
*
* [url=home.php?mod=space&uid=247401]@brief[/url] This file contains the user gpio function..
*
*/
/* =========================================== Includes =========================================== */
#include "gpio.h"
#include "stdbool.h"
/* ============================================ Define ============================================ */
#define GET_KEY1_STS() (bool)(GPIO_GetPinLevel(KEY_PORT, KEY_PIN))
#define GET_NMI_STS() (bool)(GPIO_GetPinLevel(NMI_PORT, NMI_PIN))
/* =========================================== Typedef ============================================ */
/* ========================================== Variables =========================================== */
/* ==================================== Functions declaration ===================================== */
/* ====================================== Functions define ======================================== */
/*!
* @brief EXTI_Key_Callback(void *device, uint32_t wpara, uint32_t lpara)
*
* @param device:
* @param wpara:
* @param lpara: 0
*
* [url=home.php?mod=space&uid=266161]@return[/url] none
*/
void EXTI_Key_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
if (GET_KEY1_STS() == false) /*判断是否为KEY1唤醒*/
{
LED1_ON;
LED2_ON;
}
else
{
/*None*/
}
}
/*!
* [url=home.php?mod=space&uid=555622]@prototype[/url] SPM_Callback(void *device, uint32_t wpara, uint32_t lpara)
*
* @param device:
* @param wpara:
* @param lpara: 0
*
* @return void
*
*/
void SPM_Callback(void *device, uint32_t wpara, uint32_t lpara)
{
}
/*!
* @brief GPIO_LedInit(void)
*
* @param none
*
* @return none
*/
void GPIO_LedInit(void)
{
/*初始化引脚功能,如果引脚上电后默认为GPIO,可省略掉初始化步骤.
有部分引脚上电默认为非GPIO,则必须选择其功能为GPIO才能作为GPIO使用.*/
GPIO_SetFunc(LED1_PORT, LED1_PIN, GPIO_FUN0);/*! 功能复用选择.*/
GPIO_SetFunc(LED2_PORT, LED2_PIN, GPIO_FUN0);
/*! 设置LED引脚为GPIO输出.*/
GPIO_SetDir(LED1_PORT, LED1_PIN, GPIO_OUT);
GPIO_SetDir(LED2_PORT, LED2_PIN, GPIO_OUT);
LED1_ON;
LED2_ON;
}
/*!
* @brief GPIO_KeyInit(void)
*
* @param none
*
* @return none
*/
void GPIO_KeyInit(void)
{
/*初始化引脚功能,如果引脚上电后默认为GPIO,可省略掉初始化步骤.
有部分引脚上电默认为非GPIO,则必须选择其功能为GPIO才能作为GPIO使用.*/
GPIO_SetFunc(KEY_PORT, KEY_PIN, GPIO_FUN0); /*功能复用选择.*/
/*设置按键引脚为输入,检测按键动作.*/
GPIO_SetDir(KEY_PORT, KEY_PIN, GPIO_IN); /*设置为输入模式.*/
/*使能KEY1的外部中断,设置为下降沿产生中断,当按下按键时产生中断.*/
GPIO_EnableExtInterrupt(KEY_PORT, KEY_PIN, EXTI_TRIGGER_FALLING);
/*设置外部中断,中断回调函数.*/
GPIO_SetCallback(KEY_PIN, EXTI_Key_Callback);
SPM_EnableModuleWakeup(SPM_MODULE_GPIO, ENABLE);
/*设置中断回调函数*/ /*注意,GPIO唤醒,只会进GPIO口中断,不会进SPM中断,部分外设会进SPM中断*/
/*SPM_SetCallback(SPM_Callback);*/
}
/*!
* @prototype GPIO_MCUGotoSleep(void)
*
* @param[in] void
*
* @return void
*
*/
void GPIO_MCUGotoSleep(void)
{
mdelay(2000);
/*休眠前关闭LED1/LED2.*/
LED1_OFF;
LED2_OFF;
/*进入STOP模式.*/
SysStop();
}
/* ============================================= EOF ============================================== */
3、添加新加的.c文件、.h头文件到makefile文件中。
4、为了在烧写时避免书写太长的命令,添加.pack包到工程里面,在工程目录下面新建pyocd.yaml文件,里面包含pack包:
5、make工程,在build目录下生成的.bin、.elf、.hex文件:
6、接上stlink到开发板,开发板接12V,并打开电源开关。虚拟机会提示USB设备接入到哪里,我们选择接入到虚拟机:
7、烧写固件到开发板:
输入命令:pyocd load ./build/AC7802_GPIO.elf --target ac78022mbqa
lugl@lugl-virtual-machine:~/ac7802/AC7802_GPIO$ pyocd load ./build/AC7802_GPIO.elf --target ac78022mbqa
0000345 I Loading /home/lugl/ac7802/AC7802_GPIO/build/AC7802_GPIO.elf [load_cmd]
[==================================================] 100%
0000782 I Erased 0 bytes (0 sectors), programmed 0 bytes (0 pages), skipped 4608 bytes (9 pages) at 10.31 kB/s [loader]
8、重启开发板后,两个LED灯亮,2秒钟后灯熄灭进入睡眠模式,我们按下KEY1,可以唤醒MCU,两个LED灯又亮了,如此循环。
【总结】
优点:
使用gcc编译,相比mdk的编译速度要快很多。而且可以跨平台开发。
缺点:
新建工程有点难度,对汇编、makefile的基础知识要有所了解。烧写需要用到命令行,交互不是很方便。
|