在线时间21 小时
UID2003041
注册时间2013-4-19
NXP金币0
该用户从未签到
注册会员

- 积分
- 138
- 最后登录
- 1970-1-1
|

楼主 |
发表于 2014-12-9 10:33:39
|
显示全部楼层
/** ###################################################################
** Filename : mqx_tasks.c
** Project : ProcessorExpert
** Processor : MKL25Z128VLK4
** Component : Events
** Version : Driver 01.00
** Compiler : GNU C Compiler
** Date/Time : 2012-09-12, 23:41, # CodeGen: 3
** Abstract :
** This is user's event module.
** Put your event handler code here.
** Settings :
** Contents :
** Task1_task - void Task1_task(uint32_t task_init_data);
**
** ###################################################################*/
/* MODULE mqx_tasks */
#include "Cpu.h"
#include "Events.h"
#include "mqx_tasks.h"
#include "lwtimer.h"
/* User includes (#include below this line is not maintained by Processor Expert) */
/* MMA8451Q IOMap */
/* External 3-axis accelerometer data register addresses */
#define OUT_X_MSB 0x01
#define OUT_X_LSB 0x02
#define OUT_Y_MSB 0x03
#define OUT_Y_LSB 0x04
#define OUT_Z_MSB 0x05
#define OUT_Z_LSB 0x06
/* External 3-axis accelerometer control register addresses */
#define CTRL_REG_1 0x2A
/* External 3-axis accelerometer control register bit masks */
#define ACTIVE_BIT_MASK 0x01
#define F_READ_BIT_MASK 0x02
#define ACC_REG_SIZE 1U
#define READ_COUNT 5U
LDD_TDeviceData *I2C_DeviceData = NULL;
TDataState DataState;
LDD_TDeviceData *PWMTimerRG_DeviceData = NULL;
LDD_TDeviceData *PWMTimerB_DeviceData = NULL;
MUTEX_STRUCT print_mutex;
LWSEM_STRUCT TEST_SEM;
LWEVENT_STRUCT my_event;
LWTIMER_PERIOD_STRUCT mytimer_queue;
LWTIMER_STRUCT mytimer1;
LWTIMER_STRUCT mytimer2;
#define NUM_MESSAGES 4
#define MSG_SIZE 1
uint32 mymsg_queue[sizeof(LWMSGQ_STRUCT)/sizeof(uint_32) + NUM_MESSAGES * MSG_SIZE];
char Buffer[] = " ";
volatile bool DataWrittenFlg = FALSE;
char Data[] = "Hello1world";
LDD_TError Error;
LDD_FLASH_TOperationStatus OpStatus;
extern LDD_TDeviceData *MyFLASH_Ptr;
void ReadAccRegs(LDD_TDeviceData *DeviceDataPtr,TDataState *UserData,uint8 Address,uint8 ByteNum,uint8 *Buffer)
{
LDD_I2C_TBusState BusState;
UserData->Sent = 0;
I2C_MasterSendBlock(DeviceDataPtr, &Address, sizeof(Address), LDD_I2C_NO_SEND_STOP);
while(!UserData->Sent); //等待发送
UserData->Received = 0;
I2C_MasterReceiveBlock(DeviceDataPtr, Buffer, ByteNum, LDD_I2C_SEND_STOP);
while(!UserData->Received); //等待接受
do{
I2C_CheckBus(DeviceDataPtr, &BusState);
}while(BusState != LDD_I2C_IDLE); //等待接受完成
}
void WriteAccRegs(LDD_TDeviceData *DeviceDataPtr,TDataState *UserData,uint8 Address,uint8 ByteNum,uint8 *Buffer)
{
LDD_I2C_TBusState BusState;
UserData->Sent = 0;
I2C_MasterSendBlock(DeviceDataPtr, Buffer, ByteNum+1 , LDD_I2C_SEND_STOP);
while(!UserData->Sent);
do{
I2C_CheckBus(DeviceDataPtr, &BusState);
}while(BusState != LDD_I2C_IDLE); //等待发送完成
}
void Myprint1()
{
printf("Test LWTIMER OK!\n");
}
void Myprint2()
{
printf("%s\n",Buffer);
}
uint16 Led_Brightness = 10000;
void Task1_task(uint32_t task_init_data)
{
int counter = 0;
_mqx_uint result;
uint8 Data = (ACTIVE_BIT_MASK | F_READ_BIT_MASK);
signed char Accbuffer[3] = {0,127,127}; // initialize to turqoise
I2C_DeviceData = I2C_Init(&DataState);
WriteAccRegs(I2C_DeviceData, &DataState, CTRL_REG_1,ACC_REG_SIZE, &Data);//
PWMTimerRG_DeviceData = PWMTimerRG_Init(NULL);
PWMTimerB_DeviceData = PWMTimerB_Init(NULL);
//MyFLASH_Ptr = FLASH1_Init(NULL); /* Initialization of FLASH1 component */
/* Initialize the mutex */
result = _lwsem_create(&TEST_SEM, 0);
if(result != MQX_OK)
{
printf("Error: semaphore component could not be created\n");
}
result = _lwevent_create(&my_event, 0);
if (result != MQX_OK)
{
printf("Error: event component could not be created\n");
}
_lwmsgq_init((pointer) mymsg_queue, NUM_MESSAGES, MSG_SIZE);
_lwtimer_create_periodic_queue(&mytimer_queue,100,0);
_lwtimer_add_timer_to_queue(&mytimer_queue,&mytimer1,50,Myprint1,0);
_lwtimer_add_timer_to_queue(&mytimer_queue,&mytimer2,99,Myprint2,0);
printf("I'm in Task1!\n");
PWMTimerRG_Enable(PWMTimerRG_DeviceData);
PWMTimerB_Enable(PWMTimerB_DeviceData);
/************************************/
DataWrittenFlg = FALSE;
Error = FLASH1_Write(MyFLASH_Ptr, Data, MY_FLASH_LOCATION, sizeof(Data)); /* Start writing to the flash memory */
/* Here some other application code can be placed */
while (!DataWrittenFlg) { /* Wait until the data are written */
FLASH1_Main(MyFLASH_Ptr); /* Run the main method */
}
DataWrittenFlg = FALSE;
if (FLASH1_GetOperationStatus(MyFLASH_Ptr) == LDD_FLASH_FAILED) { /* Check if the operation has successfully ended */
/* Error state solution */
}
// Error = FLASH1_Read(MyFLASH_Ptr, MY_FLASH_LOCATION, Buffer, sizeof(Buffer)); /* Start reading from the flash memory */
// do {
// FLASH1_Main(MyFLASH_Ptr);
// OpStatus = FLASH1_GetOperationStatus(MyFLASH_Ptr);
// } while (!((OpStatus == LDD_FLASH_IDLE) | (OpStatus == LDD_FLASH_FAILED)));
//
// if (OpStatus == LDD_FLASH_FAILED) { /* Check if the operation has successfully ended */
// /* Error state solution */
// }
/************************************/
while(1)
{
counter++;
ReadAccRegs(I2C_DeviceData, &DataState, OUT_X_MSB, 3 * ACC_REG_SIZE, (uint8_t*)Accbuffer);
//printf("ACC:%d\n",Accbuffer[1]);
PWMTimerRG_SetOffsetTicks(PWMTimerRG_DeviceData, 0,Led_Brightness); //x axis - red LED
PWMTimerRG_SetOffsetTicks(PWMTimerRG_DeviceData, 1, 0); // y axis - green LED
PWMTimerB_SetOffsetTicks(PWMTimerB_DeviceData, 0, 0); //z axis - blue LED
_time_delay_ticks(20);
Led_Brightness += 1000;
if (Led_Brightness > 5000) Led_Brightness = 0;
}
}
/*
** ===================================================================
** Event : Task2_task (module mqx_tasks)
**
** Component : Task2 [MQXLite_task]
** Description :
** MQX task routine. The routine is generated into mqx_tasks.c
** file.
** Parameters :
** NAME - DESCRIPTION
** task_init_data -
** Returns : Nothing
** ===================================================================
*/
void Task2_task(uint32_t task_init_data)
{
_mqx_uint msg[MSG_SIZE];
int counter1 = 0,counter2 = 0;
while(1) {
counter1++;
_lwmsgq_receive((pointer) mymsg_queue, msg, LWMSGQ_RECEIVE_BLOCK_ON_EMPTY, 0, 0);
printf("Erase!\n");
// Error = FLASH1_Erase(MyFLASH_Ptr,MY_FLASH_LOCATION,1024);
// /* Here some other application code can be placed */
// while (!DataWrittenFlg) { /* Wait until the data are written */
// FLASH1_Main(MyFLASH_Ptr); /* Run the main method */
// }
// DataWrittenFlg = FALSE;
// if (FLASH1_GetOperationStatus(MyFLASH_Ptr) == LDD_FLASH_FAILED) { /* Check if the operation has successfully ended */
// /* Error state solution */
// }
//
_lwevent_clear(&my_event, 1);
_time_delay_ticks(100);
// _mutex_unlock(&print_mutex);
}
}
/*
** ===================================================================
** Event : Task3_task (module mqx_tasks)
**
** Component : Task3 [MQXLite_task]
** Description :
** MQX task routine. The routine is generated into mqx_tasks.c
** file.
** Parameters :
** NAME - DESCRIPTION
** task_init_data -
** Returns : Nothing
** ===================================================================
*/
void Task3_task(uint32_t task_init_data)
{
_mqx_uint msg[MSG_SIZE] = {5000};
int counter1 = 0;
static bool Key_Press = FALSE;
while(1) {
counter1++;
if (KEY_GetVal(NULL) == FALSE)
{
_time_delay_ticks(2);
if (KEY_GetVal(NULL) == FALSE)
{
Key_Press = TRUE;
}
}
if (KEY_GetVal(NULL) == TRUE && Key_Press == TRUE)
{
Key_Press = FALSE;
//_lwsem_post(&TEST_SEM);
_lwevent_set(&my_event,1);
printf("You have pressed the key!\n");
_lwtimer_cancel_timer(&mytimer2);
_lwmsgq_send((pointer) mymsg_queue, msg, LWMSGQ_SEND_BLOCK_ON_FULL);
}
_time_delay_ticks(2);
}
}
/* END mqx_tasks */
/*
** ###################################################################
**
** This file was created by Processor Expert 10.0 [05.02]
** for the Freescale Kinetis series of microcontrollers.
**
** ###################################################################
*/
|
|