我写的代码如下,定时器是每200US发信号量给任务,然后任务是改变PWM的输出频率。但是我在示波器下观察,发现时没有波形输出的?请问是什么原因?
#define TASK_STACKSIZE 2048
OS_STK task1_stk[TASK_STACKSIZE];
#define TASK1_PRIORITY 4
OS_EVENT *sem;
typedef struct
{
volatile unsigned int divi;
volatile unsigned int duty;
volatile unsigned int enable;
}PWM;
unsigned int divi_1[3]={5000,3000,2000};
unsigned int duty_1[3]={2500,1500,1000};
static void ISR_timer1(void *context, alt_u32 id)
{
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE, 0x00);
OSSemPost(sem);
}
void task1(void* pdata)
{
INT8U i,err;
PWM *pwm = (PWM *)PWM_0_NAME;
OS_CPU_SR cpu_sr;
sem=OSSemCreate(0);
//初始化定时器,200US中断一次
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE, 0x00);
IOWR_ALTERA_AVALON_TIMER_PERIODL(TIMER1_BASE,20000);
IOWR_ALTERA_AVALON_TIMER_PERIODH(TIMER1_BASE,20000>> 16);
IOWR_ALTERA_AVALON_TIMER_CONTROL(TIMER1_BASE, 0x07);
//注册中断函数
alt_irq_register(TIMER1_IRQ, (void *)TIMER1_BASE, ISR_timer1);
while(1)
{
for(i=0;i<3;i++)
{
OSSemPend(sem,0,&err);
OS_ENTER_CRITICAL();
//改变PWM的占空比
pwm->divi=divi_1;
pwm->duty=duty_1;
pwm->enable = 1;
OS_EXIT_CRITICAL();
}
}
}
int main(void)
{
OSTaskCreateExt(task1,
NULL,
(void *)&task1_stk[TASK_STACKSIZE-1],
TASK1_PRIORITY,
TASK1_PRIORITY,
task1_stk,
TASK_STACKSIZE,
NULL,
0);
OSStart();
return 0;
}
debug看task进去没进去 进去的话就是avalon总线到IO连接问题
static void ISR_timer1(void *context, alt_u32 id)
{
IOWR_ALTERA_AVALON_TIMER_STATUS(TIMER1_BASE, 0x00);
OSSemPost(sem);
}
没有OSINTENTER及OSINTEXIT
淘宝小店:http://brightpoint.taobao.com
回复 板凳llpanda 的帖子
可是OSSemPost不是可以进行任务切换的吗?
回复 沙发astwyg 的帖子
我单步运行时,它有进去的。而且PWM的值是有改变。
IO接口没问题的。
回复 4楼guaiguaidou 的帖子
在POST的时候会调用OS_Sched,但是请看源码,在中断服务程序ISR中OS_Sched判断到OSIntNesting != 0u,就不会做任务切换了,而将任务切换留给OSIntExit来执行的
void OS_Sched (void)
{
#if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr = 0u;
#endif
OS_ENTER_CRITICAL();
if (OSIntNesting == 0u) { /* Schedule only if all ISRs done and ... */
if (OSLockNesting == 0u) { /* ... scheduler is not locked */
OS_SchedNew();
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
if (OSPrioHighRdy != OSPrioCur) { /* No Ctx Sw if current task is highest rdy */
#if OS_TASK_PROFILE_EN > 0u
OSTCBHighRdy->OSTCBCtxSwCtr++; /* Inc. # of context switches to this task */
#endif
OSCtxSwCtr++; /* Increment context switch counter */
OS_TASK_SW(); /* Perform a context switch */
}
}
}
OS_EXIT_CRITICAL();
}
淘宝小店:http://brightpoint.taobao.com