APP(应用层)

OSAL_SampleApp.c

#include "ZComDef.h"
#include "hal_drivers.h"
#include "OSAL.h"
#include "OSAL_Tasks.h"#if defined ( MT_TASK )#include "MT.h"#include "MT_TASK.h"
#endif#include "nwk.h"
#include "APS.h"
#include "ZDApp.h"
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )#include "ZDNwkMgr.h"
#endif
#if defined ( ZIGBEE_FRAGMENTATION )#include "aps_frag.h"
#endif#include "SampleApp.h"// The order in this table must be identical to the task initialization calls below in osalInitTask.
const pTaskEventHandlerFn tasksArr[] = {macEventLoop,nwk_event_loop,Hal_ProcessEvent,
#if defined( MT_TASK )MT_ProcessEvent,
#endifAPS_event_loop,
#if defined ( ZIGBEE_FRAGMENTATION )APSF_ProcessEvent,
#endifZDApp_event_loop,
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )ZDNwkMgr_event_loop,
#endifSampleApp_ProcessEvent
};const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
uint16 *tasksEvents;void osalInitTasks( void )
{uint8 taskID = 0;tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);// 设置所分配的内存空间单元值为0osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));macTaskInit( taskID++ ); nwk_init( taskID++ );   Hal_Init( taskID++ );
#if defined( MT_TASK )MT_TaskInit( taskID++ );
#endifAPS_Init( taskID++ );
#if defined ( ZIGBEE_FRAGMENTATION )APSF_Init( taskID++ );
#endifZDApp_Init( taskID++ );
#if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )ZDNwkMgr_Init( taskID++ );
#endif//用户创建的任务SampleApp_Init( taskID );
}

SampleApp.c

#include "OSAL.h"
#include "ZGlobals.h"
#include "AF.h"
#include "aps_groups.h"
#include "ZDApp.h"
#include "SampleApp.h"
#include "SampleAppHw.h"
#include "OnBoard.h"
/* HAL */
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_key.h"
#include "MT_UART.h"
#include "MT_APP.h"
#include "MT.h"uint8 AppTitle[] = "ALD2530 LED"; //应用程序名称// This list should be filled with Application specific Cluster IDs.
const cId_t SampleApp_ClusterList[SAMPLEAPP_MAX_CLUSTERS] =
{SAMPLEAPP_PERIODIC_CLUSTERID,SAMPLEAPP_FLASH_CLUSTERID
};const SimpleDescriptionFormat_t SampleApp_SimpleDesc =
{SAMPLEAPP_ENDPOINT,              //  int Endpoint;SAMPLEAPP_PROFID,                //  uint16 AppProfId[2];SAMPLEAPP_DEVICEID,              //  uint16 AppDeviceId[2];SAMPLEAPP_DEVICE_VERSION,        //  int   AppDevVer:4;SAMPLEAPP_FLAGS,                 //  int   AppFlags:4;SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;(cId_t *)SampleApp_ClusterList,  //  uint8 *pAppInClusterList;SAMPLEAPP_MAX_CLUSTERS,          //  uint8  AppNumInClusters;(cId_t *)SampleApp_ClusterList   //  uint8 *pAppInClusterList;
};// This is the Endpoint/Interface description.  It is defined here, but
// filled-in in SampleApp_Init().  Another way to go would be to fill
// in the structure here and make it a "const" (in code space).  The
// way it's defined in this sample app it is define in RAM.
endPointDesc_t SampleApp_epDesc;uint8 SampleApp_TaskID;   // Task ID for internal task/event processing// This variable will be received when// SampleApp_Init() is called.
devStates_t SampleApp_NwkState;uint8 SampleApp_TransID;  // This is the unique message ID (counter)afAddrType_t SampleApp_Periodic_DstAddr;
afAddrType_t SampleApp_Flash_DstAddr;aps_Group_t SampleApp_Group;uint8 SampleAppPeriodicCounter = 0;
uint8 SampleAppFlashCounter = 0;void SampleApp_HandleKeys( uint8 shift, uint8 keys );
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pckt );
void SampleApp_SendPeriodicMessage( void );
void SampleApp_SendFlashMessage( uint16 flashTime );void SampleApp_Init( uint8 task_id )
{ SampleApp_TaskID = task_id;   //osal分配的任务ID随着用户添加任务的增多而改变SampleApp_NwkState = DEV_INIT;//设备状态设定为ZDO层中定义的初始化状态SampleApp_TransID = 0;        //消息发送ID(多消息时有顺序之分)// Device hardware initialization can be added here or in main() (Zmain.c).// If the hardware is application specific - add it here.// If the hardware is other parts of the device add it in main().#if defined ( BUILD_ALL_DEVICES )// The "Demo" target is setup to have BUILD_ALL_DEVICES and HOLD_AUTO_START// We are looking at a jumper (defined in SampleAppHw.c) to be jumpered// together - if they are - we will start up a coordinator. Otherwise,// the device will start as a router.if ( readCoordinatorJumper() )zgDeviceLogicalType = ZG_DEVICETYPE_COORDINATOR;elsezgDeviceLogicalType = ZG_DEVICETYPE_ROUTER;
#endif // BUILD_ALL_DEVICES//该段的意思是,如果设置了HOLD_AUTO_START宏定义,将会在启动芯片的时候会暂停启动
//流程,只有外部触发以后才会启动芯片。其实就是需要一个按钮触发它的启动流程。
#if defined ( HOLD_AUTO_START )// HOLD_AUTO_START is a compile option that will surpress ZDApp//  from starting the device and wait for the application to//  start the device.ZDOInitDevice(0);
#endif// Setup for the periodic message's destination address 设置发送数据的方式和目的地址寻址模式// Broadcast to everyone 发送模式:广播发送SampleApp_Periodic_DstAddr.addrMode = (afAddrMode_t)AddrBroadcast;//广播SampleApp_Periodic_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号SampleApp_Periodic_DstAddr.addr.shortAddr = 0xFFFF;//指定目的网络地址为广播地址// Setup for the flash command's destination address - Group 1 组播发送SampleApp_Flash_DstAddr.addrMode = (afAddrMode_t)afAddrGroup; //组寻址SampleApp_Flash_DstAddr.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号SampleApp_Flash_DstAddr.addr.shortAddr = SAMPLEAPP_FLASH_GROUP;//组号0x0001// Fill out the endpoint description. 定义本设备用来通信的APS层端点描述符SampleApp_epDesc.endPoint = SAMPLEAPP_ENDPOINT; //指定端点号SampleApp_epDesc.task_id = &SampleApp_TaskID;   //SampleApp 描述符的任务IDSampleApp_epDesc.simpleDesc= (SimpleDescriptionFormat_t *)&SampleApp_SimpleDesc;//SampleApp简单描述符SampleApp_epDesc.latencyReq = noLatencyReqs;    //延时策略// Register the endpoint description with the AFafRegister( &SampleApp_epDesc );    //向AF层登记描述符// Register for all key events - This app will handle all key eventsRegisterForKeys( SampleApp_TaskID ); // 登记所有的按键事件// By default, all devices start out in Group 1SampleApp_Group.ID = 0x0001;//组号osal_memcpy( SampleApp_Group.name, "Group 1", 7  );//设定组名aps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );//把该组登记添加到APS中#if defined ( LCD_SUPPORTED )HalLcdWriteString( "SampleApp", HAL_LCD_LINE_1 ); //如果支持LCD,显示提示信息
#endif
}//用户应用任务的事件处理函数
uint16 SampleApp_ProcessEvent( uint8 task_id, uint16 events )
{afIncomingMSGPacket_t *MSGpkt;(void)task_id;  // Intentionally unreferenced parameterif ( events & SYS_EVENT_MSG ) //接收系统消息再进行判断{//接收属于本应用任务SampleApp的消息,以SampleApp_TaskID标记MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );while ( MSGpkt ){switch ( MSGpkt->hdr.event ){// Received when a key is pressedcase KEY_CHANGE://按键事件SampleApp_HandleKeys( ((keyChange_t *)MSGpkt)->state, ((keyChange_t *)MSGpkt)->keys );break;// Received when a messages is received (OTA) for this endpointcase AF_INCOMING_MSG_CMD://接收数据事件,调用函数AF_DataRequest()接收数据SampleApp_MessageMSGCB( MSGpkt );//调用回调函数对收到的数据进行处理break;// Received whenever the device changes state in the networkcase ZDO_STATE_CHANGE://只要网络状态发生改变,就通过ZDO_STATE_CHANGE事件通知所有的任务。//同时完成对协调器,路由器,终端的设置SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);//if ( (SampleApp_NwkState == DEV_ZB_COORD)//实验中协调器只接收数据所以取消发送事件if ( (SampleApp_NwkState == DEV_ROUTER) || (SampleApp_NwkState == DEV_END_DEVICE) ){// Start sending the periodic message in a regular interval.//这个定时器只是为发送周期信息开启的,设备启动初始化后从这里开始//触发第一个周期信息的发送,然后周而复始下去osal_start_timerEx( SampleApp_TaskID,SAMPLEAPP_SEND_PERIODIC_MSG_EVT,SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );}else{// Device is no longer in the network}break;default:break;}// Release the memory 事件处理完了,释放消息占用的内存osal_msg_deallocate( (uint8 *)MSGpkt );// Next - if one is available 指针指向下一个放在缓冲区的待处理的事件,//返回while ( MSGpkt )重新处理事件,直到缓冲区没有等待处理事件为止MSGpkt = (afIncomingMSGPacket_t *)osal_msg_receive( SampleApp_TaskID );}// return unprocessed events 返回未处理的事件return (events ^ SYS_EVENT_MSG);}// Send a message out - This event is generated by a timer//  (setup in SampleApp_Init()).if ( events & SAMPLEAPP_SEND_PERIODIC_MSG_EVT ){// Send the periodic message 处理周期性事件,//利用SampleApp_SendPeriodicMessage()处理完当前的周期性事件,然后启动定时器//开启下一个周期性事情,这样一种循环下去,也即是上面说的周期性事件了,//可以做为传感器定时采集、上传任务SampleApp_SendPeriodicMessage();// Setup to send message again in normal period (+ a little jitter)osal_start_timerEx( SampleApp_TaskID, SAMPLEAPP_SEND_PERIODIC_MSG_EVT,(SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT + (osal_rand() & 0x00FF)) );// return unprocessed events 返回未处理的事件return (events ^ SAMPLEAPP_SEND_PERIODIC_MSG_EVT);}// Discard unknown eventsreturn 0;
}void SampleApp_HandleKeys( uint8 shift, uint8 keys ) //此实验没有用到,后面再分析
{(void)shift;  // Intentionally unreferenced parameterif ( keys & HAL_KEY_SW_1 ){/* This key sends the Flash Command is sent to Group 1.* This device will not receive the Flash Command from this* device (even if it belongs to group 1).*/SampleApp_SendFlashMessage( SAMPLEAPP_FLASH_DURATION );}if ( keys & HAL_KEY_SW_2 ){aps_Group_t *grp;grp = aps_FindGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );if ( grp ){// Remove from the groupaps_RemoveGroup( SAMPLEAPP_ENDPOINT, SAMPLEAPP_FLASH_GROUP );}else{// Add to the flash groupaps_AddGroup( SAMPLEAPP_ENDPOINT, &SampleApp_Group );}}
}//接收数据,参数为接收到的数据
void SampleApp_MessageMSGCB( afIncomingMSGPacket_t *pkt )
{uint16 flashTime;byte buf[3]; switch ( pkt->clusterId ) //判断簇ID{case SAMPLEAPP_PERIODIC_CLUSTERID: //收到广播数据osal_memset(buf, 0 , 3);osal_memcpy(buf, pkt->cmd.Data, 2); //复制数据到缓冲区中if(buf[0]=='D' && buf[1]=='1')      //判断收到的数据是否为"D1"         {HalLedBlink(HAL_LED_1, 0, 50, 500);//如果是则Led1间隔500ms闪烁
#if defined(ZDO_COORDINATOR) //协调器收到"D1"后,返回"D1"给终端,让终端Led1也闪烁SampleApp_SendPeriodicMessage();
#endif}else{HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);                   }break;case SAMPLEAPP_FLASH_CLUSTERID: //收到组播数据flashTime = BUILD_UINT16(pkt->cmd.Data[1], pkt->cmd.Data[2] );HalLedBlink( HAL_LED_4, 4, 50, (flashTime / 4) );break;}
}//分析发送周期信息
void SampleApp_SendPeriodicMessage( void )
{byte SendData[3]="D1";// 调用AF_DataRequest将数据无线广播出去if( AF_DataRequest( &SampleApp_Periodic_DstAddr,//发送目的地址+端点地址和传送模式&SampleApp_epDesc,//源(答复或确认)终端的描述(比如操作系统中任务ID等)源EPSAMPLEAPP_PERIODIC_CLUSTERID, //被Profile指定的有效的集群号2,       // 发送数据长度SendData,// 发送数据缓冲区&SampleApp_TransID,     // 任务ID号AF_DISCV_ROUTE,      // 有效位掩码的发送选项AF_DEFAULT_RADIUS ) == afStatus_SUCCESS )  //传送跳数,通常设置为AF_DEFAULT_RADIUS{}else{HalLedSet(HAL_LED_1, HAL_LED_MODE_ON);// Error occurred in request to send.}
}void SampleApp_SendFlashMessage( uint16 flashTime )
{uint8 buffer[3];buffer[0] = (uint8)(SampleAppFlashCounter++);buffer[1] = LO_UINT16( flashTime );buffer[2] = HI_UINT16( flashTime );if ( AF_DataRequest( &SampleApp_Flash_DstAddr, &SampleApp_epDesc,SAMPLEAPP_FLASH_CLUSTERID,3,buffer,&SampleApp_TransID,AF_DISCV_ROUTE,AF_DEFAULT_RADIUS ) == afStatus_SUCCESS ){}else{// Error occurred in request to send.}
}

SampleApp.h

SampleAppHw.c

#include "ZComDef.h"
#include "hal_mcu.h"
#include "hal_defs.h"#include "SampleAppHw.h"#define JUMPERIN_BIT  BV(2)  //P0.2
#define JUMPERIN_SEL  P0SEL
#define JUMPERIN_DIR  P0DIR
#define JUMPERIN      P0#define JUMPEROUT_BIT BV(3)  //P0.3
#define JUMPEROUT_SEL P0SEL
#define JUMPEROUT_DIR P0DIR
#define JUMPEROUT     P0uint8 readCoordinatorJumper( void )
{uint8 i,j;uint8 result;JUMPERIN_SEL  &= ~(JUMPERIN_BIT);JUMPERIN_DIR  &= ~(JUMPERIN_BIT);JUMPEROUT_SEL &= ~(JUMPEROUT_BIT);JUMPEROUT_DIR |= JUMPEROUT_BIT;JUMPEROUT &= ~(JUMPEROUT_BIT);for ( i = 0; i < 8; i++ ){for ( j = 0; j < 8; j++ ){// Burn time for input to see changeresult = JUMPERIN & JUMPERIN_BIT;}j = i & 0x01;if (((j == 0) && (result != 0)) ||((j != 0) && (result != JUMPERIN_BIT))){return ( FALSE );}// Toggle the output bitJUMPEROUT ^= JUMPEROUT_BIT;}return ( TRUE );
}

SampleAppHw.h

HAL(硬件层)

Common

hal_assert.c

#include "hal_assert.h"
#include "hal_types.h"
#include "hal_board.h"
#include "hal_defs.h"
#include "hal_mcu.h"#if (defined HAL_MCU_AVR) || (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || \(defined HAL_MCU_CC2533) || (defined HAL_MCU_MSP430)/* for access to debug data */
#include "mac_rx.h"
#include "mac_tx.h"
#endifvoid halAssertHazardLights(void);void halAssertHandler(void)
{/* execute code that handles asserts */
#ifdef ASSERT_RESETHAL_SYSTEM_RESET();
#elif !defined ASSERT_WHILEhalAssertHazardLights();
#elsewhile(1);
#endif
}#if !defined ASSERT_WHILEvoid halAssertHazardLights(void)
{enum{DEBUG_DATA_RSTACK_HIGH_OFS,DEBUG_DATA_RSTACK_LOW_OFS,DEBUG_DATA_TX_ACTIVE_OFS,DEBUG_DATA_RX_ACTIVE_OFS,#if (defined HAL_MCU_AVR) || (defined HAL_MCU_CC2430)DEBUG_DATA_INT_MASK_OFS,
#elif (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533)DEBUG_DATA_INT_MASK0_OFS,DEBUG_DATA_INT_MASK1_OFS,
#endifDEBUG_DATA_SIZE};uint8 buttonHeld;uint8 debugData[DEBUG_DATA_SIZE];/* disable all interrupts before anything else */HAL_DISABLE_INTERRUPTS();HAL_BOARD_INIT();HAL_TURN_OFF_LED1();HAL_TURN_OFF_LED2();HAL_TURN_OFF_LED3();HAL_TURN_OFF_LED4();for (;;){buttonHeld = 0;do{HAL_LED_BLINK_DELAY();#if (HAL_NUM_LEDS >= 1)HAL_TOGGLE_LED1();
#if (HAL_NUM_LEDS >= 2)HAL_TOGGLE_LED2();
#if (HAL_NUM_LEDS >= 3)HAL_TOGGLE_LED3();
#if (HAL_NUM_LEDS >= 4)HAL_TOGGLE_LED4();
#endif
#endif
#endif
#endif{static uint8 escape = 0;if (escape){escape = 0;return;}}/* break out of loop if button is held long enough */if (HAL_PUSH_BUTTON1()){buttonHeld++;}else{buttonHeld = 0;}}while (buttonHeld != 10);  /* loop until button is held specified number of loops *//* turn off all LEDs */HAL_TURN_OFF_LED1();HAL_TURN_OFF_LED2();HAL_TURN_OFF_LED3();HAL_TURN_OFF_LED4();/* wait for button release */HAL_DEBOUNCE(!HAL_PUSH_BUTTON1());#ifdef HAL_MCU_AVR{uint8 * pStack;pStack = (uint8 *) SP;pStack++; /* point to return address on stack */debugData[DEBUG_DATA_RSTACK_HIGH_OFS] = *pStack;pStack++;debugData[DEBUG_DATA_RSTACK_LOW_OFS] = *pStack;}debugData[DEBUG_DATA_INT_MASK_OFS] = EIMSK;
#endif#if (defined HAL_MCU_CC2430)debugData[DEBUG_DATA_INT_MASK_OFS] = RFIM;
#elif (defined HAL_MCU_CC2530) || (defined HAL_MCU_CC2533)debugData[DEBUG_DATA_INT_MASK0_OFS] = RFIRQM0;debugData[DEBUG_DATA_INT_MASK1_OFS] = RFIRQM1;
#endif#if (defined HAL_MCU_AVR) || (defined HAL_MCU_CC2430) || (defined HAL_MCU_CC2530) || \(defined HAL_MCU_CC2533) || (defined HAL_MCU_MSP430)debugData[DEBUG_DATA_TX_ACTIVE_OFS] = macTxActive;debugData[DEBUG_DATA_RX_ACTIVE_OFS] = macRxActive;
#endif/* initialize for data dump loop */{uint8 iBit;uint8 iByte;iBit  = 0;iByte = 0;while (iByte < DEBUG_DATA_SIZE){/* wait for key press */while(!HAL_PUSH_BUTTON1());/* turn on all LEDs for first bit of byte, turn on three LEDs if not first bit */HAL_TURN_ON_LED1();HAL_TURN_ON_LED2();HAL_TURN_ON_LED3();if (iBit == 0){HAL_TURN_ON_LED4();}else{HAL_TURN_OFF_LED4();}/* wait for debounced key release */HAL_DEBOUNCE(!HAL_PUSH_BUTTON1());/* turn off all LEDs */HAL_TURN_OFF_LED1();HAL_TURN_OFF_LED2();HAL_TURN_OFF_LED3();HAL_TURN_OFF_LED4();/* output value of data bit to LED1 */if (debugData[iByte] & (1 << (7 - iBit))){HAL_TURN_ON_LED1();}else{HAL_TURN_OFF_LED1();}/* advance to next bit */iBit++;if (iBit == 8){iBit = 0;iByte++;}}}HAL_TURN_OFF_LED1();}
}
#endif/* integrity check of type sizes */
HAL_ASSERT_SIZE(  int8, 1);
HAL_ASSERT_SIZE( uint8, 1);
HAL_ASSERT_SIZE( int16, 2);
HAL_ASSERT_SIZE(uint16, 2);
HAL_ASSERT_SIZE( int32, 4);
HAL_ASSERT_SIZE(uint32, 4);

hal_drivers.c

#include "hal_types.h"
#include "OSAL.h"
#include "hal_drivers.h"
#include "hal_adc.h"
#if (defined HAL_DMA) && (HAL_DMA == TRUE)#include "hal_dma.h"
#endif
#include "hal_key.h"
#include "hal_lcd.h"
#include "hal_led.h"
#include "hal_timer.h"
#include "hal_uart.h"
#include "hal_sleep.h"
#if (defined HAL_AES) && (HAL_AES == TRUE)#include "hal_aes.h"
#endif#if (defined HAL_SPI) && (HAL_SPI == TRUE)#include "hal_spi.h"
#endif
#if (defined HAL_HID) && (HAL_HID == TRUE)#include "usb_hid.h"
#endif#ifdef CC2591_COMPRESSION_WORKAROUND#include "mac_rx.h"
#endifuint8 Hal_TaskID;extern void HalLedUpdate( void );
void Hal_Init( uint8 task_id )
{/* Register task ID */Hal_TaskID = task_id;#ifdef CC2591_COMPRESSION_WORKAROUND  osal_start_reload_timer( Hal_TaskID, PERIOD_RSSI_RESET_EVT, PERIOD_RSSI_RESET_TIMEOUT );
#endif
}void HalDriverInit (void)
{/* TIMER */
#if (defined HAL_TIMER) && (HAL_TIMER == TRUE)#error "The hal timer driver module is removed."
#endif/* ADC */
#if (defined HAL_ADC) && (HAL_ADC == TRUE)HalAdcInit();
#endif/* DMA */
#if (defined HAL_DMA) && (HAL_DMA == TRUE)// Must be called before the init call to any module that uses DMA.HalDmaInit();
#endif/* AES */
#if (defined HAL_AES) && (HAL_AES == TRUE)HalAesInit();
#endif/* LCD */
#if (defined HAL_LCD) && (HAL_LCD == TRUE)HalLcdInit();
#endif/* LED */
#if (defined HAL_LED) && (HAL_LED == TRUE)HalLedInit();
#endif/* UART */
#if (defined HAL_UART) && (HAL_UART == TRUE)HalUARTInit();
#endif/* KEY */
#if (defined HAL_KEY) && (HAL_KEY == TRUE)HalKeyInit();
#endif/* SPI */
#if (defined HAL_SPI) && (HAL_SPI == TRUE)HalSpiInit();
#endif/* HID */
#if (defined HAL_HID) && (HAL_HID == TRUE)usbHidInit();
#endif
}uint16 Hal_ProcessEvent( uint8 task_id, uint16 events )
{uint8 *msgPtr;(void)task_id;  // Intentionally unreferenced parameterif ( events & SYS_EVENT_MSG ){msgPtr = osal_msg_receive(Hal_TaskID);while (msgPtr){msgPtr = osal_msg_receive( Hal_TaskID );}return events ^ SYS_EVENT_MSG;}if ( events & HAL_LED_BLINK_EVENT ){
#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)HalLedUpdate();
#endif /* BLINK_LEDS && HAL_LED */return events ^ HAL_LED_BLINK_EVENT;}if (events & HAL_KEY_EVENT){#if (defined HAL_KEY) && (HAL_KEY == TRUE)/* Check for keys */HalKeyPoll();/* if interrupt disabled, do next polling */if (!Hal_KeyIntEnable){osal_start_timerEx( Hal_TaskID, HAL_KEY_EVENT, 100);}
#endif // HAL_KEYreturn events ^ HAL_KEY_EVENT;}#ifdef POWER_SAVINGif ( events & HAL_SLEEP_TIMER_EVENT ){halRestoreSleepLevel();return events ^ HAL_SLEEP_TIMER_EVENT;}
#endif#ifdef CC2591_COMPRESSION_WORKAROUNDif ( events & PERIOD_RSSI_RESET_EVT ){macRxResetRssi();return (events ^ PERIOD_RSSI_RESET_EVT);}
#endif  /* Nothing interested, discard the message */return 0;}void Hal_ProcessPoll ()
{/* Timer Poll */
#if (defined HAL_TIMER) && (HAL_TIMER == TRUE)#error "The hal timer driver module is removed."
#endif/* UART Poll */
#if (defined HAL_UART) && (HAL_UART == TRUE)HalUARTPoll();
#endif  /* SPI Poll */
#if (defined HAL_SPI) && (HAL_SPI == TRUE)HalSpiPoll();
#endif#if (defined HAL_HID) && (HAL_HID == TRUE)usbHidProcessEvents();
#endif#if defined( POWER_SAVING )/* Allow sleep before the next OSAL event loop */ALLOW_SLEEP_MODE();
#endif}

Include

hal_adc.h

#ifndef HAL_ADC_H
#define HAL_ADC_H#ifdef __cplusplus
extern "C"
{
#endif/* Resolution */
#define HAL_ADC_RESOLUTION_8       0x01
#define HAL_ADC_RESOLUTION_10      0x02
#define HAL_ADC_RESOLUTION_12      0x03
#define HAL_ADC_RESOLUTION_14      0x04/* Channels */
#define HAL_ADC_CHANNEL_0          0x00
#define HAL_ADC_CHANNEL_1          0x01
#define HAL_ADC_CHANNEL_2          0x02
#define HAL_ADC_CHANNEL_3          0x03
#define HAL_ADC_CHANNEL_4          0x04
#define HAL_ADC_CHANNEL_5          0x05
#define HAL_ADC_CHANNEL_6          0x06
#define HAL_ADC_CHANNEL_7          0x07#define HAL_ADC_CHN_AIN0    0x00    /* AIN0 */
#define HAL_ADC_CHN_AIN1    0x01    /* AIN1 */
#define HAL_ADC_CHN_AIN2    0x02    /* AIN2 */
#define HAL_ADC_CHN_AIN3    0x03    /* AIN3 */
#define HAL_ADC_CHN_AIN4    0x04    /* AIN4 */
#define HAL_ADC_CHN_AIN5    0x05    /* AIN5 */
#define HAL_ADC_CHN_AIN6    0x06    /* AIN6 */
#define HAL_ADC_CHN_AIN7    0x07    /* AIN7 */
#define HAL_ADC_CHN_A0A1    0x08    /* AIN0,AIN1 */
#define HAL_ADC_CHN_A2A3    0x09    /* AIN2,AIN3 */
#define HAL_ADC_CHN_A4A5    0x0a    /* AIN4,AIN5 */
#define HAL_ADC_CHN_A6A7    0x0b    /* AIN6,AIN7 */
#define HAL_ADC_CHN_GND     0x0c    /* GND */
#define HAL_ADC_CHN_VREF    0x0d    /* Positive voltage reference */
#define HAL_ADC_CHN_TEMP    0x0e    /* Temperature sensor */
#define HAL_ADC_CHN_VDD3    0x0f    /* VDD/3 */
#define HAL_ADC_CHN_BITS    0x0f    /* Bits [3:0] */#define HAL_ADC_CHANNEL_TEMP       HAL_ADC_CHN_TEMP
#define HAL_ADC_CHANNEL_VDD        HAL_ADC_CHN_VDD3   /* channel VDD divided by 3 *//* Vdd Limits */
#define HAL_ADC_VDD_LIMIT_0        0x00
#define HAL_ADC_VDD_LIMIT_1        0x01
#define HAL_ADC_VDD_LIMIT_2        0x02
#define HAL_ADC_VDD_LIMIT_3        0x03
#define HAL_ADC_VDD_LIMIT_4        0x04
#define HAL_ADC_VDD_LIMIT_5        0x05
#define HAL_ADC_VDD_LIMIT_6        0x06
#define HAL_ADC_VDD_LIMIT_7        0x07/* Reference Voltages */
#define HAL_ADC_REF_125V          0x00    /* Internal Reference (1.25V-CC2430)(1.15V-CC2530) */
#define HAL_ADC_REF_AIN7          0x40    /* AIN7 Reference */
#define HAL_ADC_REF_AVDD          0x80    /* AVDD_SOC Pin Reference */
#define HAL_ADC_REF_DIFF          0xc0    /* AIN7,AIN6 Differential Reference */
#define HAL_ADC_REF_BITS          0xc0    /* Bits [7:6] */extern void HalAdcInit ( void );extern uint16 HalAdcRead ( uint8 channel, uint8 resolution );extern void HalAdcSetReference ( uint8 reference );extern bool HalAdcCheckVdd(uint8 vdd);#ifdef __cplusplus
}
#endif#endif

hal_assert.h

#ifndef HAL_ASSERT_H
#define HAL_ASSERT_H
#ifdef HALNODEBUG
#define HAL_ASSERT(expr)
#define HAL_ASSERT_FORCED()
#define HAL_ASSERT_STATEMENT(statement)
#define HAL_ASSERT_DECLARATION(declaration)
#else
#define HAL_ASSERT(expr)                        st( if (!( expr )) halAssertHandler(); )
#define HAL_ASSERT_FORCED()                     halAssertHandler()
#define HAL_ASSERT_STATEMENT(statement)         st( statement )
#define HAL_ASSERT_DECLARATION(declaration)     declaration
#endif
#define HAL_ASSERT_SIZE(x,y) typedef char x ## _assert_size_t[-1+10*(sizeof(x) == (y))]
void halAssertHandler(void);
extern void halAssertHazardLights(void);
#endif

hal_board.h

#include "hal_board_cfg.h"

hal_ccm.h


#ifndef     HAL_CCM_H_
#define     HAL_CCM_H_Status_t SSP_CCM_Auth (uint8, uint8 *, uint8 *, uint16, uint8 *, uint16, uint8 *, uint8 *);
Status_t SSP_CCM_Encrypt (uint8, uint8 *, uint8 *, uint16, uint8 *, uint8 *);
Status_t SSP_CCM_Decrypt (uint8, uint8 *, uint8 *, uint16, uint8 *, uint8 *);
Status_t SSP_CCM_InvAuth (uint8, uint8 *, uint8 *, uint16, uint8 *, uint16, uint8 *, uint8 *);#endif

hal_defs.h


#ifndef HAL_DEFS_H
#define HAL_DEFS_H#ifndef BV
#define BV(n)      (1 << (n))
#endif#ifndef BF
#define BF(x,b,s)  (((x) & (b)) >> (s))
#endif#ifndef MIN
#define MIN(n,m)   (((n) < (m)) ? (n) : (m))
#endif#ifndef MAX
#define MAX(n,m)   (((n) < (m)) ? (m) : (n))
#endif#ifndef ABS
#define ABS(n)     (((n) < 0) ? -(n) : (n))
#endif/* takes a byte out of a uint32 : var - uint32,  ByteNum - byte to take out (0 - 3) */
#define BREAK_UINT32( var, ByteNum ) \(uint8)((uint32)(((var) >>((ByteNum) * 8)) & 0x00FF))#define BUILD_UINT32(Byte0, Byte1, Byte2, Byte3) \((uint32)((uint32)((Byte0) & 0x00FF) \+ ((uint32)((Byte1) & 0x00FF) << 8) \+ ((uint32)((Byte2) & 0x00FF) << 16) \+ ((uint32)((Byte3) & 0x00FF) << 24)))#define BUILD_UINT16(loByte, hiByte) \((uint16)(((loByte) & 0x00FF) + (((hiByte) & 0x00FF) << 8)))#define HI_UINT16(a) (((a) >> 8) & 0xFF)
#define LO_UINT16(a) ((a) & 0xFF)#define BUILD_UINT8(hiByte, loByte) \((uint8)(((loByte) & 0x0F) + (((hiByte) & 0x0F) << 4)))
#define HI_UINT8(a) (((a) >> 4) & 0x0F)
#define LO_UINT8(a) ((a) & 0x0F)
#define st(x)      do { x } while (__LINE__ == -1)
#endif

hal_drivers.h

#ifndef HAL_DRIVER_H
#define HAL_DRIVER_H#ifdef __cplusplus
extern "C"
{
#endif#define HAL_KEY_EVENT         0x0001
#define HAL_LED_BLINK_EVENT   0x0002
#define HAL_SLEEP_TIMER_EVENT 0x0004
#define PERIOD_RSSI_RESET_EVT 0x0008
#define PERIOD_RSSI_RESET_TIMEOUT           10
extern uint8 Hal_TaskID;
extern void Hal_Init ( uint8 task_id );extern uint16 Hal_ProcessEvent ( uint8 task_id, uint16 events );
extern void Hal_ProcessPoll (void);
extern void HalDriverInit (void);
#ifdef __cplusplus
}
#endif
#endif

hal_flash.h


#ifndef HAL_FLASH_H
#define HAL_FLASH_H#ifdef __cplusplus
extern "C"
{
#endif#include "hal_board.h"
#include "hal_types.h"void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt);void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt);void HalFlashErase(uint8 pg);#ifdef __cplusplus
};
#endif#endif

hal_key.h

#ifndef HAL_KEY_H
#define HAL_KEY_H#ifdef __cplusplus
extern "C"
{
#endif
/* Interrupt option - Enable or disable */
#define HAL_KEY_INTERRUPT_DISABLE    0x00
#define HAL_KEY_INTERRUPT_ENABLE     0x01/* Key state - shift or nornal */
#define HAL_KEY_STATE_NORMAL          0x00
#define HAL_KEY_STATE_SHIFT           0x01/* Switches (keys) */
#define HAL_KEY_SW_1 0x01  // Joystick up
#define HAL_KEY_SW_2 0x02  // Joystick right
#define HAL_KEY_SW_5 0x04  // Joystick center
#define HAL_KEY_SW_4 0x08  // Joystick left
#define HAL_KEY_SW_3 0x10  // Joystick down
#define HAL_KEY_SW_6 0x20  // Button S1 if available
#define HAL_KEY_SW_7 0x40  // Button S2 if available/* Joystick */
#define HAL_KEY_UP     0x01  // Joystick up
#define HAL_KEY_RIGHT  0x02  // Joystick right
#define HAL_KEY_CENTER 0x04  // Joystick center
#define HAL_KEY_LEFT   0x08  // Joystick left
#define HAL_KEY_DOWN   0x10  // Joystick downtypedef void (*halKeyCBack_t) (uint8 keys, uint8 state);extern bool Hal_KeyIntEnable;extern void HalKeyInit( void );extern void HalKeyConfig( bool interruptEnable, const halKeyCBack_t cback);extern uint8 HalKeyRead( void);extern void HalKeyEnterSleep ( void );extern uint8 HalKeyExitSleep ( void );extern void HalKeyPoll ( void );extern bool HalKeyPressed( void );#ifdef __cplusplus
}
#endif#endif

hal_lcd.h


#ifndef HAL_LCD_H
#define HAL_LCD_H#ifdef __cplusplus
extern "C"
{
#endif/* These are used to specify which line the text will be printed */
#define HAL_LCD_LINE_1      0x00
#define HAL_LCD_LINE_2      0x01#define HAL_LCD_LINE_3      0x02
#define HAL_LCD_LINE_4      0x03
#define HAL_LCD_LINE_5      0x04
#define HAL_LCD_LINE_6      0x05
#define HAL_LCD_LINE_7      0x06
#define HAL_LCD_LINE_8      0x07/* Max number of chars on a single LCD line */
#define HAL_LCD_MAX_CHARS   16
#define HAL_LCD_MAX_BUFF    25extern void HalLcdInit(void);extern void HalLcdWriteString ( char *str, uint8 option);extern void HalLcdWriteValue ( uint32 value, const uint8 radix, uint8 option);extern void HalLcdWriteScreen( char *line1, char *line2 );extern void HalLcdWriteStringValue( char *title, uint16 value, uint8 format, uint8 line );extern void HalLcdWriteStringValueValue( char *title, uint16 value1, uint8 format1, uint16 value2, uint8 format2, uint8 line );extern void HalLcdDisplayPercentBar( char *title, uint8 value );//LCD 颜色
#define  WHITE          0xFFFF
#define  BLACK          0x0000
#define  GREY           0xF7DE
#define  GREY2          0xF79E
#define  DARK_GREY      0x6B4D
#define  DARK_GREY2     0x52AA
#define  LIGHT_GREY     0xE71C
#define  BLUE           0x001F
#define  BLUE2          0x051F
#define  RED            0xF800
#define  MAGENTA        0xF81F
#define  GREEN          0x07E0
#define  CYAN           0x7FFF
#define  YELLOW         0xFFE0//显示界面颜色
#define WINDOW_BK_COLOR 0xDFFF //窗口背景色
#define WINDOW_COLOR    0x11FA //窗口前景色
#define TITLE_BK_COLOR  0x11FA //标题栏背景色
#define TITLE_COLOR     0xDFFF //标题栏前景色
#define STATUS_BK_COLOR 0x0014 //状态栏背景色
#define STATUS_COLOR    0xDFFF //状态栏前景色extern unsigned int  Color ;   // 前景颜色
extern unsigned int  Color_BK; // 背景颜色
extern void DrawRectFill(unsigned int Xpos  , unsigned int Ypos, unsigned int Width, unsigned int Height, unsigned Color);
extern void LCD_write_EN_string(unsigned char X,unsigned char Y,uint8 *s);
extern void LCD_write_CN_string(unsigned char X,unsigned char Y,uint8 *s);#ifdef __cplusplus
}
#endif#endif

hal_led.h


#ifndef HAL_LED_H
#define HAL_LED_H#ifdef __cplusplus
extern "C"
{
#endif#include "hal_board.h"/* LEDS - The LED number is the same as the bit position */
#define HAL_LED_1     0x01
#define HAL_LED_2     0x02
#define HAL_LED_3     0x04
#define HAL_LED_4     0x08
#define HAL_LED_ALL   (HAL_LED_1 | HAL_LED_2 | HAL_LED_3 | HAL_LED_4)/* Modes */
#define HAL_LED_MODE_OFF     0x00
#define HAL_LED_MODE_ON      0x01
#define HAL_LED_MODE_BLINK   0x02
#define HAL_LED_MODE_FLASH   0x04
#define HAL_LED_MODE_TOGGLE  0x08/* Defaults */
#define HAL_LED_DEFAULT_MAX_LEDS      4
#define HAL_LED_DEFAULT_DUTY_CYCLE    5
#define HAL_LED_DEFAULT_FLASH_COUNT   50
#define HAL_LED_DEFAULT_FLASH_TIME    1000extern void HalLedInit( void );extern uint8 HalLedSet( uint8 led, uint8 mode );extern void HalLedBlink( uint8 leds, uint8 cnt, uint8 duty, uint16 time );extern void HalLedEnterSleep( void );extern void HalLedExitSleep( void );extern uint8 HalLedGetState ( void );#ifdef __cplusplus
}
#endif
#endif

hal_sleep.h


#ifndef HAL_SLEEP_H
#define HAL_SLEEP_H#ifdef __cplusplus
extern "C"
{
#endif
extern void halSleep( uint16 osal_timer );
extern void halSleepWait(uint16 duration);
extern void halRestoreSleepLevel( void );
extern void halSleepExit(void);
extern void halSetMaxSleepLoopTime(uint32 rolloverTime);
#ifdef __cplusplus
}
#endif#endif

hal_timer.h

#ifndef HAL_TIMER_H
#define HAL_TIMER_H#ifdef __cplusplus
extern "C"
{
#endif
#include "hal_board.h"
/* Timer ID definitions */
#define HAL_TIMER_0                0x00    // 8bit timer
#define HAL_TIMER_1                0x01    // 16bit Mac timer
#define HAL_TIMER_2                0x02    // 8bit timer
#define HAL_TIMER_3                0x03    // 16bit timer
#define HAL_TIMER_MAX              4       // Max number of timer
/* Operation Modes for timer */
#define HAL_TIMER_MODE_NORMAL     0x01    // Normal Mode
#define HAL_TIMER_MODE_CTC        0x02    // Clear Timer On Compare
#define HAL_TIMER_MODE_MASK       (HAL_TIMER_MODE_NORMAL | HAL_TIMER_MODE_CTC)
/* Channel definitions */
#define HAL_TIMER_CHANNEL_SINGLE   0x01    // Single Channel - default
#define HAL_TIMER_CHANNEL_A        0x02    // Channel A
#define HAL_TIMER_CHANNEL_B        0x04    // Channel B
#define HAL_TIMER_CHANNEL_C        0x08    // Channel C
#define HAL_TIMER_CHANNEL_MASK    (HAL_TIMER_CHANNEL_SINGLE |  \HAL_TIMER_CHANNEL_A |       \HAL_TIMER_CHANNEL_B |       \HAL_TIMER_CHANNEL_C)/* Channel mode definitions */
#define HAL_TIMER_CH_MODE_INPUT_CAPTURE   0x01    // Channel Mode Input-Capture
#define HAL_TIMER_CH_MODE_OUTPUT_COMPARE  0x02    // Channel Mode Output_Compare
#define HAL_TIMER_CH_MODE_OVERFLOW        0x04    // Channel Mode Overflow
#define HAL_TIMER_CH_MODE_MASK            (HAL_TIMER_CH_MODE_INPUT_CAPTURE |  \HAL_TIMER_CH_MODE_OUTPUT_COMPARE | \HAL_TIMER_CH_MODE_OVERFLOW)/* Error Code */
#define HAL_TIMER_OK              0x00
#define HAL_TIMER_NOT_OK          0x01
#define HAL_TIMER_PARAMS_ERROR    0x02
#define HAL_TIMER_NOT_CONFIGURED  0x03
#define HAL_TIMER_INVALID_ID      0x04
#define HAL_TIMER_INVALID_CH_MODE 0x05
#define HAL_TIMER_INVALID_OP_MODE 0x06/* Timer clock pre-scaler definitions for 16bit timer1 and timer3 */
#define HAL_TIMER3_16_TC_STOP    0x00  // No clock, timer stopped
#define HAL_TIMER3_16_TC_DIV1    0x01  // No clock pre-scaling
#define HAL_TIMER3_16_TC_DIV8    0x02  // Clock pre-scaled by 8
#define HAL_TIMER3_16_TC_DIV64   0x03  // Clock pre-scaled by 64
#define HAL_TIMER3_16_TC_DIV256  0x04  // Clock pre-scaled by 256
#define HAL_TIMER3_16_TC_DIV1024 0x05  // Clock pre-scaled by 1024
#define HAL_TIMER3_16_TC_EXTFE   0x06  // External clock (T2), falling edge
#define HAL_TIMER3_16_TC_EXTRE   0x07  // External clock (T2), rising edge/* Timer clock pre-scaler definitions for 8bit timer0 and timer2 */
#define HAL_TIMER0_8_TC_STOP    0x00  // No clock, timer stopped
#define HAL_TIMER0_8_TC_DIV1    0x01  // No clock pre-scaling
#define HAL_TIMER0_8_TC_DIV8    0x02  // Clock pre-scaled by 8
#define HAL_TIMER0_8_TC_DIV32   0x03  // Clock pre-scaled by 32
#define HAL_TIMER0_8_TC_DIV64   0x04  // Clock pre-scaled by 64
#define HAL_TIMER0_8_TC_DIV128  0x05  // Clock pre-scaled by 128
#define HAL_TIMER0_8_TC_DIV256  0x06  // Clock pre-scaled by 256
#define HAL_TIMER0_8_TC_DIV1024 0x07  // Clock pre-scaled by 1024/* Timer clock pre-scaler definitions for 8bit timer2 */
#define HAL_TIMER2_8_TC_STOP    0x00  // No clock, timer stopped
#define HAL_TIMER2_8_TC_DIV1    0x01  // No clock pre-scaling
#define HAL_TIMER2_8_TC_DIV8    0x02  // Clock pre-scaled by 8
#define HAL_TIMER2_8_TC_DIV64   0x03  // Clock pre-scaled by 32
#define HAL_TIMER2_8_TC_DIV256  0x04  // Clock pre-scaled by 64
#define HAL_TIMER2_8_TC_DIV1024 0x05  // Clock pre-scaled by 128
#define HAL_TIMER2_8_TC_EXTFE   0x06  // External clock (T2), falling edge
#define HAL_TIMER2_8_TC_EXTRE   0x07  // External clock (T2), rising edgeextern void HalTimerInit ( void );extern uint8 HalTimerConfig ( uint8 timerId,uint8 opMode,uint8 channel,uint8 channelMode,bool intEnable,halTimerCBack_t cback );extern uint8 HalTimerStart ( uint8 timerId, uint32 timePerTick );extern uint8 HalTimerStop ( uint8 timerId );extern void HalTimerTick ( void );extern uint8 HalTimerInterruptEnable (uint8 timerId, uint8 channelMode, bool enable);#ifdef __cplusplus
}
#endif#endif

hal_uart.h

#ifndef HAL_UART_H
#define HAL_UART_H#ifdef __cplusplus
extern "C"
{
#endif#include "hal_board.h"#define HAL_UART_BR_9600   0x00
#define HAL_UART_BR_19200  0x01
#define HAL_UART_BR_38400  0x02
#define HAL_UART_BR_57600  0x03
#define HAL_UART_BR_115200 0x04/* Frame Format constant *//* Stop Bits */
#define HAL_UART_ONE_STOP_BIT       0x00
#define HAL_UART_TWO_STOP_BITS      0x01/* Parity settings */
#define HAL_UART_NO_PARITY          0x00
#define HAL_UART_EVEN_PARITY        0x01
#define HAL_UART_ODD_PARITY         0x02/* Character Size */
#define HAL_UART_8_BITS_PER_CHAR    0x00
#define HAL_UART_9_BITS_PER_CHAR    0x01/* Flow control */
#define HAL_UART_FLOW_OFF   0x00
#define HAL_UART_FLOW_ON    0x01/* Ports */
#define HAL_UART_PORT_0   0x00
#define HAL_UART_PORT_1   0x01
#define HAL_UART_PORT_MAX 0x02/* UART Status */
#define  HAL_UART_SUCCESS        0x00
#define  HAL_UART_UNCONFIGURED   0x01
#define  HAL_UART_NOT_SUPPORTED  0x02
#define  HAL_UART_MEM_FAIL       0x03
#define  HAL_UART_BAUDRATE_ERROR 0x04/* UART Events */
#define HAL_UART_RX_FULL         0x01
#define HAL_UART_RX_ABOUT_FULL   0x02
#define HAL_UART_RX_TIMEOUT      0x04
#define HAL_UART_TX_FULL         0x08
#define HAL_UART_TX_EMPTY        0x10typedef void (*halUARTCBack_t) (uint8 port, uint8 event);typedef struct
{// The head or tail is updated by the Tx or Rx ISR respectively, when not polled.volatile uint16 bufferHead;volatile uint16 bufferTail;uint16 maxBufSize;uint8 *pBuffer;
} halUARTBufControl_t;typedef struct
{bool                configured;uint8               baudRate;bool                flowControl;uint16              flowControlThreshold;uint8               idleTimeout;halUARTBufControl_t rx;halUARTBufControl_t tx;bool                intEnable;uint32              rxChRvdTime;halUARTCBack_t      callBackFunc;
}halUARTCfg_t;typedef union
{bool paramCTS;bool paramRTS;bool paramDSR;bool paramDTR;bool paramCD;bool paramRI;uint16 baudRate;bool flowControl;bool flushControl;
}halUARTIoctl_t;extern void HalUARTInit ( void );extern uint8 HalUARTOpen ( uint8 port, halUARTCfg_t *config );extern void HalUARTClose ( uint8 port );extern uint16 HalUARTRead ( uint8 port, uint8 *pBuffer, uint16 length );extern uint16 HalUARTWrite ( uint8 port, uint8 *pBuffer, uint16 length );extern uint8 HalUARTIoctl ( uint8 port, uint8 cmd, halUARTIoctl_t *pIoctl );extern void HalUARTPoll( void );extern uint16 Hal_UART_RxBufLen ( uint8 port );extern uint16 Hal_UART_TxBufLen ( uint8 port );extern void Hal_UART_FlowControlSet ( uint8 port, bool status );extern uint8 HalUART_HW_Init(uint8 port);extern void HalUARTSuspend(void);extern void HalUARTResume(void);#ifdef __cplusplus
}
#endif
#endif

Target

CC2530EB

Config

hal_board_cfg.h

Drivers

hal_adc.c
在这里插入代码片#include  "hal_adc.h"
#include  "hal_defs.h"
#include  "hal_mcu.h"
#include  "hal_types.h"#define HAL_ADC_EOC         0x80    /* End of Conversion bit */
#define HAL_ADC_START       0x40    /* Starts Conversion */#define HAL_ADC_STSEL_EXT   0x00    /* External Trigger */
#define HAL_ADC_STSEL_FULL  0x10    /* Full Speed, No Trigger */
#define HAL_ADC_STSEL_T1C0  0x20    /* Timer1, Channel 0 Compare Event Trigger */
#define HAL_ADC_STSEL_ST    0x30    /* ADCCON1.ST =1 Trigger */#define HAL_ADC_RAND_NORM   0x00    /* Normal Operation */
#define HAL_ADC_RAND_LFSR   0x04    /* Clock LFSR */
#define HAL_ADC_RAND_SEED   0x08    /* Seed Modulator */
#define HAL_ADC_RAND_STOP   0x0c    /* Stop Random Generator */
#define HAL_ADC_RAND_BITS   0x0c    /* Bits [3:2] */#define HAL_ADC_DEC_064     0x00    /* Decimate by 64 : 8-bit resolution */
#define HAL_ADC_DEC_128     0x10    /* Decimate by 128 : 10-bit resolution */
#define HAL_ADC_DEC_256     0x20    /* Decimate by 256 : 12-bit resolution */
#define HAL_ADC_DEC_512     0x30    /* Decimate by 512 : 14-bit resolution */
#define HAL_ADC_DEC_BITS    0x30    /* Bits [5:4] */#define HAL_ADC_STSEL       HAL_ADC_STSEL_ST
#define HAL_ADC_RAND_GEN    HAL_ADC_RAND_STOP
#define HAL_ADC_REF_VOLT    HAL_ADC_REF_AVDD
#define HAL_ADC_DEC_RATE    HAL_ADC_DEC_064
#define HAL_ADC_SCHN        HAL_ADC_CHN_VDD3
#define HAL_ADC_ECHN        HAL_ADC_CHN_GND#if (HAL_ADC == TRUE)
static uint8 adcRef;
#endifvoid HalAdcInit (void)
{
#if (HAL_ADC == TRUE)adcRef = HAL_ADC_REF_VOLT;
#endif
}uint16 HalAdcRead (uint8 channel, uint8 resolution)
{int16  reading = 0;#if (HAL_ADC == TRUE)uint8   i, resbits;uint8  adcChannel = 1;if (channel < 8){for (i=0; i < channel; i++){adcChannel <<= 1;}}/* Enable channel */ADCCFG |= adcChannel;/* Convert resolution to decimation rate */switch (resolution){case HAL_ADC_RESOLUTION_8:resbits = HAL_ADC_DEC_064;break;case HAL_ADC_RESOLUTION_10:resbits = HAL_ADC_DEC_128;break;case HAL_ADC_RESOLUTION_12:resbits = HAL_ADC_DEC_256;break;case HAL_ADC_RESOLUTION_14:default:resbits = HAL_ADC_DEC_512;break;}/* writing to this register starts the extra conversion */ADCCON3 = channel | resbits | adcRef;/* Wait for the conversion to be done */while (!(ADCCON1 & HAL_ADC_EOC));/* Disable channel after done conversion */ADCCFG &= (adcChannel ^ 0xFF);/* Read the result */reading = (int16) (ADCL);reading |= (int16) (ADCH << 8);/* Treat small negative as 0 */if (reading < 0)reading = 0;switch (resolution){case HAL_ADC_RESOLUTION_8:reading >>= 8;break;case HAL_ADC_RESOLUTION_10:reading >>= 6;break;case HAL_ADC_RESOLUTION_12:reading >>= 4;break;case HAL_ADC_RESOLUTION_14:default:reading >>= 2;break;}
#else// unused arguments(void) channel;(void) resolution;
#endifreturn ((uint16)reading);
}void HalAdcSetReference ( uint8 reference )
{
#if (HAL_ADC == TRUE)adcRef = reference;
#endif
}bool HalAdcCheckVdd(uint8 vdd)
{ADCCON3 = 0x0F;while (!(ADCCON1 & 0x80));return (ADCH > vdd);
}
hal_dma.c
#include "hal_types.h"
#include "hal_defs.h"
#include "hal_dma.h"
#include "hal_mcu.h"
#include "hal_uart.h"#if (defined HAL_IRGEN) && (HAL_IRGEN == TRUE)
#include "hal_irgen.h"
#endif#if (defined HAL_SPI) && (HAL_SPI == TRUE)
#include "hal_spi.h"
#endif#if ((defined HAL_DMA) && (HAL_DMA == TRUE))halDMADesc_t dmaCh0;
halDMADesc_t dmaCh1234[4];void HalDmaInit( void )
{HAL_DMA_SET_ADDR_DESC0( &dmaCh0 );HAL_DMA_SET_ADDR_DESC1234( dmaCh1234 );
#if (HAL_UART_DMA || \((defined HAL_SPI) && (HAL_SPI == TRUE))  || \((defined HAL_IRGEN) && (HAL_IRGEN == TRUE)))DMAIE = 1;
#endif
}#if (HAL_UART_DMA || \((defined HAL_SPI) && (HAL_SPI == TRUE))  || \((defined HAL_IRGEN) && (HAL_IRGEN == TRUE)))HAL_ISR_FUNCTION( halDmaIsr, DMA_VECTOR )
{extern void HalUARTIsrDMA(void);HAL_ENTER_ISR();DMAIF = 0;#if HAL_UART_DMAif (HAL_DMA_CHECK_IRQ(HAL_DMA_CH_TX)){HalUARTIsrDMA();}
#endif // HAL_UART_DMA#if (defined HAL_SPI) && (HAL_SPI == TRUE)if ( HAL_DMA_CHECK_IRQ( HAL_DMA_CH_RX ) ){HAL_DMA_CLEAR_IRQ( HAL_DMA_CH_RX );HalSpiRxIsr();}if ( HAL_DMA_CHECK_IRQ( HAL_DMA_CH_TX ) ){HAL_DMA_CLEAR_IRQ( HAL_DMA_CH_TX );HalSpiTxIsr();}
#endif // (defined HAL_SPI) && (HAL_SPI == TRUE)#if (defined HAL_IRGEN) && (HAL_IRGEN == TRUE)if ( HAL_IRGEN == TRUE && HAL_DMA_CHECK_IRQ( HAL_IRGEN_DMA_CH ) ){HAL_DMA_CLEAR_IRQ( HAL_IRGEN_DMA_CH );HalIrGenDmaIsr();}
#endif // (defined HAL_IRGEN) && (HAL_IRGEN == TRUE)CLEAR_SLEEP_MODE();HAL_EXIT_ISR();
}
#endif
#endif  // #if ((defined HAL_DMA) && (HAL_DMA == TRUE))
hal_flash.c
#include "hal_board_cfg.h"
#include "hal_dma.h"
#include "hal_flash.h"
#include "hal_mcu.h"
#include "hal_types.h"void HalFlashRead(uint8 pg, uint16 offset, uint8 *buf, uint16 cnt)
{// Calculate the offset into the containing flash bank as it gets mapped into XDATA.uint8 *pData = (uint8 *)(offset + HAL_FLASH_PAGE_MAP) +((pg % HAL_FLASH_PAGE_PER_BANK) * HAL_FLASH_PAGE_SIZE);uint8 memctr = MEMCTR;  // Save to restore.#if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE)halIntState_t is;
#endifpg /= HAL_FLASH_PAGE_PER_BANK;  // Calculate the flash bank from the flash page.#if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE)HAL_ENTER_CRITICAL_SECTION(is);
#endif// Calculate and map the containing flash bank into XDATA.MEMCTR = (MEMCTR & 0xF8) | pg;while (cnt--){*buf++ = *pData++;}MEMCTR = memctr;#if (!defined HAL_OAD_BOOT_CODE) && (!defined HAL_OTA_BOOT_CODE)HAL_EXIT_CRITICAL_SECTION(is);
#endif
}void HalFlashWrite(uint16 addr, uint8 *buf, uint16 cnt)
{
#if (defined HAL_DMA) && (HAL_DMA == TRUE)halDMADesc_t *ch = HAL_NV_DMA_GET_DESC();HAL_DMA_SET_SOURCE(ch, buf);HAL_DMA_SET_DEST(ch, &FWDATA);HAL_DMA_SET_VLEN(ch, HAL_DMA_VLEN_USE_LEN);HAL_DMA_SET_LEN(ch, (cnt * HAL_FLASH_WORD_SIZE));HAL_DMA_SET_WORD_SIZE(ch, HAL_DMA_WORDSIZE_BYTE);HAL_DMA_SET_TRIG_MODE(ch, HAL_DMA_TMODE_SINGLE);HAL_DMA_SET_TRIG_SRC(ch, HAL_DMA_TRIG_FLASH);HAL_DMA_SET_SRC_INC(ch, HAL_DMA_SRCINC_1);HAL_DMA_SET_DST_INC(ch, HAL_DMA_DSTINC_0);// The DMA is to be polled and shall not issue an IRQ upon completion.HAL_DMA_SET_IRQ(ch, HAL_DMA_IRQMASK_DISABLE);HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS);HAL_DMA_SET_PRIORITY(ch, HAL_DMA_PRI_HIGH);HAL_DMA_CLEAR_IRQ(HAL_NV_DMA_CH);HAL_DMA_ARM_CH(HAL_NV_DMA_CH);FADDRL = (uint8)addr;FADDRH = (uint8)(addr >> 8);FCTL |= 0x02;         // Trigger the DMA writes.while (FCTL & 0x80);  // Wait until writing is done.
#endif
}void HalFlashErase(uint8 pg)
{FADDRH = pg * (HAL_FLASH_PAGE_SIZE / HAL_FLASH_WORD_SIZE / 256);FCTL |= 0x01;
}
hal_key.c
在这里插入代码片#include "hal_mcu.h"
#include "hal_defs.h"
#include "hal_types.h"
#include "hal_board.h"
#include "hal_drivers.h"
#include "hal_adc.h"
#include "hal_key.h"
#include "osal.h"#if (defined HAL_KEY) && (HAL_KEY == TRUE)#define HAL_KEY_RISING_EDGE   0
#define HAL_KEY_FALLING_EDGE  1#define HAL_KEY_DEBOUNCE_VALUE  25
#define HAL_KEY_POLLING_VALUE   100/* CPU port interrupt */
#define HAL_KEY_CPU_PORT_0_IF P0IF
#define HAL_KEY_CPU_PORT_2_IF P2IF/* SW_6 is at P0.1 */
#define HAL_KEY_SW_6_PORT   P0
#define HAL_KEY_SW_6_BIT    BV(1)
#define HAL_KEY_SW_6_SEL    P0SEL
#define HAL_KEY_SW_6_DIR    P0DIR/* edge interrupt */
#define HAL_KEY_SW_6_EDGEBIT  BV(0)
#define HAL_KEY_SW_6_EDGE     HAL_KEY_FALLING_EDGE/* SW_6 interrupts */
#define HAL_KEY_SW_6_IEN      IEN1  /* CPU interrupt mask register */
#define HAL_KEY_SW_6_IENBIT   BV(5) /* Mask bit for all of Port_0 */
#define HAL_KEY_SW_6_ICTL     P0IEN /* Port Interrupt Control register */
#define HAL_KEY_SW_6_ICTLBIT  BV(1) /* P0IEN - P0.1 enable/disable bit */
#define HAL_KEY_SW_6_PXIFG    P0IFG /* Interrupt flag at source *//* Joy stick move at P2.0 */
#define HAL_KEY_JOY_MOVE_PORT   P2
#define HAL_KEY_JOY_MOVE_BIT    BV(0)
#define HAL_KEY_JOY_MOVE_SEL    P2SEL
#define HAL_KEY_JOY_MOVE_DIR    P2DIR/* edge interrupt */
#define HAL_KEY_JOY_MOVE_EDGEBIT  BV(3)
#define HAL_KEY_JOY_MOVE_EDGE     HAL_KEY_FALLING_EDGE/* Joy move interrupts */
#define HAL_KEY_JOY_MOVE_IEN      IEN2  /* CPU interrupt mask register */
#define HAL_KEY_JOY_MOVE_IENBIT   BV(1) /* Mask bit for all of Port_2 */
#define HAL_KEY_JOY_MOVE_ICTL     P2IEN /* Port Interrupt Control register */
#define HAL_KEY_JOY_MOVE_ICTLBIT  BV(0) /* P2IENL - P2.0<->P2.3 enable/disable bit */
#define HAL_KEY_JOY_MOVE_PXIFG    P2IFG /* Interrupt flag at source */#define HAL_KEY_JOY_CHN   HAL_ADC_CHANNEL_6static uint8 halKeySavedKeys;     /* used to store previous key state in polling mode */
static halKeyCBack_t pHalKeyProcessFunction;
static uint8 HalKeyConfigured;
bool Hal_KeyIntEnable;            /* interrupt enable/disable flag */void halProcessKeyInterrupt(void);
uint8 halGetJoyKeyInput(void);void HalKeyInit( void )
{/* Initialize previous key to 0 */halKeySavedKeys = 0;HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT);    /* Set pin function to GPIO */HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT);    /* Set pin direction to Input */HAL_KEY_JOY_MOVE_SEL &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin function to GPIO */HAL_KEY_JOY_MOVE_DIR &= ~(HAL_KEY_JOY_MOVE_BIT); /* Set pin direction to Input *//* Initialize callback function */pHalKeyProcessFunction  = NULL;/* Start with key is not configured */HalKeyConfigured = FALSE;
}void HalKeyConfig (bool interruptEnable, halKeyCBack_t cback)
{/* Enable/Disable Interrupt or */Hal_KeyIntEnable = interruptEnable;/* Register the callback fucntion */pHalKeyProcessFunction = cback;/* Determine if interrupt is enable or not */if (Hal_KeyIntEnable){/* Rising/Falling edge configuratinn */PICTL &= ~(HAL_KEY_SW_6_EDGEBIT);    /* Clear the edge bit *//* For falling edge, the bit must be set. */#if (HAL_KEY_SW_6_EDGE == HAL_KEY_FALLING_EDGE)PICTL |= HAL_KEY_SW_6_EDGEBIT;#endifHAL_KEY_SW_6_ICTL |= HAL_KEY_SW_6_ICTLBIT;HAL_KEY_SW_6_IEN |= HAL_KEY_SW_6_IENBIT;HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT);/* Rising/Falling edge configuratinn */HAL_KEY_JOY_MOVE_ICTL &= ~(HAL_KEY_JOY_MOVE_EDGEBIT);    /* Clear the edge bit *//* For falling edge, the bit must be set. */#if (HAL_KEY_JOY_MOVE_EDGE == HAL_KEY_FALLING_EDGE)HAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_EDGEBIT;#endifHAL_KEY_JOY_MOVE_ICTL |= HAL_KEY_JOY_MOVE_ICTLBIT;HAL_KEY_JOY_MOVE_IEN |= HAL_KEY_JOY_MOVE_IENBIT;HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT);/* Do this only after the hal_key is configured - to work with sleep stuff */if (HalKeyConfigured == TRUE){osal_stop_timerEx( Hal_TaskID, HAL_KEY_EVENT);  /* Cancel polling if active */}}else    /* Interrupts NOT enabled */{HAL_KEY_SW_6_ICTL &= ~(HAL_KEY_SW_6_ICTLBIT); /* don't generate interrupt */HAL_KEY_SW_6_IEN &= ~(HAL_KEY_SW_6_IENBIT);   /* Clear interrupt enable bit */osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_POLLING_VALUE);    /* Kick off polling */}/* Key now is configured */HalKeyConfigured = TRUE;
}uint8 HalKeyRead ( void )
{uint8 keys = 0;if (HAL_PUSH_BUTTON1()){keys |= HAL_KEY_SW_6;}/*  if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  // Key is active low {keys |= halGetJoyKeyInput();}
*/return keys;
}void HalKeyPoll (void)
{uint8 keys = 0;/* if ((HAL_KEY_JOY_MOVE_PORT & HAL_KEY_JOY_MOVE_BIT))  // Key is active HIGH {keys = halGetJoyKeyInput();}
*/if (!HAL_PUSH_BUTTON2())//S0{keys |= HAL_KEY_SW_1; }if (!HAL_PUSH_BUTTON1())//S1 {keys |= HAL_KEY_SW_6; }if (!Hal_KeyIntEnable){if (keys == halKeySavedKeys){/* Exit - since no keys have changed */return;}/* Store the current keys for comparation next time */halKeySavedKeys = keys;}else{/* Key interrupt handled here */}/* Invoke Callback if new keys were depressed */if (keys && (pHalKeyProcessFunction)){(pHalKeyProcessFunction) (keys, HAL_KEY_STATE_NORMAL);}
}uint8 halGetJoyKeyInput(void)
{/* The joystick control is encoded as an analog voltage.* Read the JOY_LEVEL analog value and map it to joy movement.*/uint8 adc;uint8 ksave0 = 0;uint8 ksave1;/* Keep on reading the ADC until two consecutive key decisions are the same. */do{ksave1 = ksave0;    /* save previouse key reading */adc = HalAdcRead (HAL_KEY_JOY_CHN, HAL_ADC_RESOLUTION_8);if ((adc >= 2) && (adc <= 38)){ksave0 |= HAL_KEY_UP;}else if ((adc >= 74) && (adc <= 88)){ksave0 |= HAL_KEY_RIGHT;}else if ((adc >= 60) && (adc <= 73)){ksave0 |= HAL_KEY_LEFT;}else if ((adc >= 39) && (adc <= 59)){ksave0 |= HAL_KEY_DOWN;}else if ((adc >= 89) && (adc <= 100)){ksave0 |= HAL_KEY_CENTER;}} while (ksave0 != ksave1);return ksave0;
}void halProcessKeyInterrupt (void)
{bool valid=FALSE;if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT)  /* Interrupt Flag has been set */{HAL_KEY_SW_6_PXIFG = ~(HAL_KEY_SW_6_BIT); /* Clear Interrupt Flag */valid = TRUE;}if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT)  /* Interrupt Flag has been set */{HAL_KEY_JOY_MOVE_PXIFG = ~(HAL_KEY_JOY_MOVE_BIT); /* Clear Interrupt Flag */valid = TRUE;}if (valid){osal_start_timerEx (Hal_TaskID, HAL_KEY_EVENT, HAL_KEY_DEBOUNCE_VALUE);}
}void HalKeyEnterSleep ( void )
{
}uint8 HalKeyExitSleep ( void )
{/* Wake up and read keys */return ( HalKeyRead () );
}HAL_ISR_FUNCTION( halKeyPort0Isr, P0INT_VECTOR )
{if (HAL_KEY_SW_6_PXIFG & HAL_KEY_SW_6_BIT){halProcessKeyInterrupt();}/*Clear the CPU interrupt flag for Port_0PxIFG has to be cleared before PxIF*/HAL_KEY_SW_6_PXIFG = 0;HAL_KEY_CPU_PORT_0_IF = 0;
}HAL_ISR_FUNCTION( halKeyPort2Isr, P2INT_VECTOR )
{if (HAL_KEY_JOY_MOVE_PXIFG & HAL_KEY_JOY_MOVE_BIT){halProcessKeyInterrupt();}HAL_KEY_JOY_MOVE_PXIFG = 0;HAL_KEY_CPU_PORT_2_IF = 0;
}#elsevoid HalKeyInit(void){}
void HalKeyConfig(bool interruptEnable, halKeyCBack_t cback){}
uint8 HalKeyRead(void){ return 0;}
void HalKeyPoll(void){}#endif /* HAL_KEY */
hal_lcd.c
#include "hal_types.h"
#include "hal_lcd.h"
#include "OSAL.h"
#include "OnBoard.h"
#include "hal_assert.h"
#include <string.h>#if defined (ZTOOL_P1) || defined (ZTOOL_P2)#include "DebugTrace.h"
#endif#define LCD_COMMAND 0x00
#define LCD_DATA    0x01#define LCD_MAX_BUF 25#define SOFTWARE_SPI
//#define HARDWARE_SPI //采用硬件SPI通讯#define COL_SPACE   2  //字符间距
#define ROW_SPACE   4  //行间距
#define ALL_CNS     128
#define X_MAX_PIXEL 128
#define Y_MAX_PIXEL 128// SPI TFT LCD端口定义
#define LCD_DC           P0_0  // 数据/命令选择
#define LCD_SDA          P1_3  // 双向数据
#define LCD_SCL          P1_2  // 时钟
//#define LCD_CS         P1_2  // 片选, L_S 将片选接地
#define LCD_RST          P1_7  // 控制lcd复位   //液晶控制口置1操作语句宏定义
//#define LCD_CS_SET    (LCD_CS_CLRx01)
#define LCD_DC_SET      (LCD_DC = 0x01)
#define LCD_SDA_SET     (LCD_SDA = 0x01)
#define LCD_SCL_SET     (LCD_SCL = 0x01)
#define LCD_RST_SET     (LCD_RST = 0x01)  //液晶控制口置0操作语句宏定义
//#define LCD_CS_CLR    (LCD_CS_CLRx00)
#define LCD_DC_CLR      (LCD_DC = 0x00)
#define LCD_SDA_CLR     (LCD_SDA = 0x00)
#define LCD_SCL_CLR     (LCD_SCL = 0x00)
#define LCD_RST_CLR     (LCD_RST = 0x00)   void SPI_Init(void);
void LCD_clear(unsigned int color);
void LCD_write_EN_string(unsigned char X,unsigned char Y,uint8 *s);
static void LCD_set_window(unsigned int X, unsigned int Y, unsigned Width,unsigned int Height);
static void LCD_write_EN(unsigned int X, unsigned int Y, unsigned char c);
static void LCD_write_CN(unsigned int X, unsigned int Y, unsigned char c);unsigned int  Color    = BLUE; //默认前景颜色
unsigned int  Color_BK = GREEN;//默认背景颜色#define XLevelL            0x00
#define XLevelH            0x10
#define XLevel             ((XLevelH&0x0F)*16+XLevelL)
#define Max_Column         128
#define Max_Row            64
#define Brightness         0xCF
#define X_WIDTH            128
#define Y_WIDTH            64#define LCD_MAX_BUF        25
#define HAL_LCD_MAX_CHARS  16   // Max number of chars on a single LCD line #if (HAL_LCD == TRUE)
void HalLcd_HW_Init(void);
void HalLcd_HW_WaitUs(uint16 i);
void HalLcd_HW_Clear(void);
void HalLcd_HW_Control(uint8 cmd);
void HalLcd_HW_Write(uint8 data);
void HalLcd_HW_SetContrast(uint8 value);
void HalLcd_HW_WriteChar(uint8 line, uint8 col, char text);
void HalLcd_HW_WriteLine(uint8 line, const char *pText);void DelayMS(unsigned int msec);
void LCD_WrDat(unsigned char dat);
void LCD_WrCmd(unsigned char cmd);
void LCD_Set_Pos(unsigned char x, unsigned char y);
void LCD_Fill(unsigned char bmp_dat) ;
void LCD_CLS(void);void DrawRect(unsigned int Xpos, unsigned int Ypos, unsigned int Width, unsigned int Height,unsigned Color);
void DrawRectFill(unsigned int Xpos  , unsigned int Ypos, unsigned int Width, unsigned int Height, unsigned Color);
void  SendByte(unsigned char Data);
void  SPI_WriteData(unsigned char Data);
void LCD_write_byte(unsigned char data, unsigned char command);
void LCD_WriteData_16Bit(unsigned short Data);
void LCD_write_CN_string(unsigned char X,unsigned char Y,uint8 *s);
void ShowImage(const unsigned char *p);void DrawPixel(unsigned int x, unsigned int y, unsigned int color);
void DrawVerticalLine(unsigned int x, unsigned int y, unsigned int Length,unsigned int Color);
void DrawHorizonLine(unsigned int x, unsigned int y, unsigned int Length,unsigned int Color);
void Lcd_Reset(void);//中文14x14点阵字符索引 ALD
__code const  unsigned char CN14x14_Index[] =
{"不吃橘子的猫"
};//中文14x14点阵字符 aldconst unsigned char CN14x14[][28] =
{
0x00,0x00,0xFF,0xF8,0x01,0x00,0x01,0x00,0x02,0x00,0x06,0x00,0x0A,0x40,0x12,0x20,
0x22,0x10,0x42,0x08,0x82,0x08,0x02,0x00,0x02,0x00,0x02,0x00,/*"不",0*/0x02,0x00,0x02,0x00,0xF3,0xF8,0x94,0x00,0x98,0x00,0x90,0x00,0x93,0xF0,0x90,0x20,
0x90,0x40,0xF0,0x80,0x91,0x00,0x02,0x08,0x02,0x08,0x01,0xF8,/*"吃",1*/0x23,0xF0,0x20,0xA0,0x20,0x40,0xF7,0xF8,0x21,0x48,0x22,0x50,0x70,0xC0,0x6F,0xF8,
0xA5,0x28,0x26,0x18,0x25,0xE8,0x25,0x28,0x25,0xE8,0x24,0x18,/*"橘",2*/0x7F,0xF0,0x00,0x20,0x00,0x40,0x00,0x80,0x01,0x00,0x02,0x00,0xFF,0xF8,0x02,0x00,
0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x02,0x00,0x0E,0x00,/*"子",3*/0x10,0x80,0x10,0x80,0x20,0xF8,0x7D,0x08,0x46,0x08,0x44,0x08,0x44,0x88,0x7C,0x48,
0x44,0x48,0x44,0x08,0x44,0x08,0x44,0x08,0x7C,0x08,0x44,0x30,/*"的",4*/0x12,0x20,0x92,0x20,0x57,0xF8,0x22,0x20,0x50,0x00,0x97,0xF0,0x14,0x90,0x34,0x90,
0x57,0xF0,0x94,0x90,0x14,0x90,0x14,0x90,0x17,0xF0,0x64,0x10,/*"猫",5*/};//英文7x14点阵字符 andy
__code const unsigned char ASCII7x14[][14] =
{
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//" ",0//
{0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,0x30,0x30,0x00,0x00},//"!",1//
{0x00,0x14,0x28,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//""",2//
{0x00,0x00,0x00,0x28,0x28,0xFC,0x28,0x48,0x50,0xFC,0x50,0x50,0x00,0x00},//"#",3//
{0x00,0x00,0x10,0x38,0x54,0x54,0x30,0x18,0x14,0x54,0x54,0x38,0x10,0x00},//"$",4//
{0x00,0x00,0x00,0x44,0xA8,0xA8,0xB0,0x54,0x1A,0x2A,0x2A,0x44,0x00,0x00},//"%",5//
{0x00,0x00,0x00,0x20,0x50,0x50,0x60,0xAC,0xA8,0x98,0x92,0x7C,0x00,0x00},//"&",6//
{0x00,0x60,0x60,0x20,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//"'",7//
{0x00,0x04,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x08,0x08,0x04,0x00},//"(",8//
{0x00,0x00,0x40,0x20,0x10,0x08,0x04,0x08,0x10,0x20,0x40,0x00,0x00,0x00},//">",9//
{0x00,0x00,0x00,0x10,0x10,0xD6,0x38,0x38,0xD6,0x10,0x10,0x00,0x00,0x00},//"*",10//
{0x00,0x00,0x00,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x00,0x00,0x00,0x00},//"+",11//
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x20,0xC0},//",",12//
{0x00,0x00,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//"-",13//
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x60,0x00,0x00},//".",14//
{0x00,0x02,0x04,0x04,0x04,0x08,0x08,0x10,0x10,0x20,0x20,0x20,0x40,0x00},//"/",15//
{0x00,0x00,0x00,0x30,0x48,0x84,0x84,0x84,0x84,0x84,0x48,0x30,0x00,0x00},//"0",16//
{0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00},//"1",17//
{0x00,0x00,0x00,0x38,0x44,0x44,0x04,0x08,0x10,0x20,0x44,0x7C,0x00,0x00},//"2",18//
{0x00,0x00,0x00,0x38,0x44,0x44,0x18,0x04,0x04,0x44,0x44,0x38,0x00,0x00},//"3",19//
{0x00,0x00,0x08,0x08,0x18,0x28,0x48,0x48,0x88,0x7C,0x08,0x1C,0x00,0x00},//"4",20//
{0x00,0x00,0x00,0x7C,0x40,0x40,0x78,0x44,0x04,0x44,0x44,0x38,0x00,0x00},//"5",21//
{0x00,0x00,0x00,0x3C,0x44,0x80,0xB8,0xC4,0x84,0x84,0x84,0x78,0x00,0x00},//"6",22//
{0x00,0x00,0x00,0x7C,0x44,0x48,0x08,0x10,0x10,0x10,0x10,0x10,0x00,0x00},//"7",23//
{0x00,0x00,0x00,0x78,0x84,0x84,0x48,0x78,0x84,0x84,0x84,0x78,0x00,0x00},//"8",24//
{0x00,0x00,0x00,0x78,0x84,0x84,0x84,0x8C,0x74,0x04,0x88,0xF0,0x00,0x00},//"9",25//
{0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x30,0x30,0x00,0x00},//":",26//
{0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x10,0x20},//";",27//
{0x00,0x00,0x04,0x08,0x10,0x20,0x40,0x20,0x10,0x08,0x04,0x00,0x00,0x00},//"<",28//
{0x00,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00},//"=",29//
{0x00,0x00,0x40,0x20,0x10,0x08,0x04,0x08,0x10,0x20,0x40,0x00,0x00,0x00},//">",30//
{0x00,0x00,0x78,0x84,0x84,0x84,0x08,0x10,0x10,0x00,0x30,0x30,0x00,0x00},//"?",31//
{0x00,0x00,0x00,0x38,0x44,0x9A,0xAA,0xAA,0xAA,0xB4,0x42,0x3C,0x00,0x00},//"@",32//
{0x00,0x00,0x00,0x10,0x10,0x28,0x28,0x28,0x38,0x44,0x44,0xEE,0x00,0x00},//"A",33//
{0x00,0x00,0x00,0xF8,0x44,0x44,0x78,0x44,0x44,0x44,0x44,0xF8,0x00,0x00},//"B",34//
{0x00,0x00,0x00,0x3C,0x44,0x84,0x80,0x80,0x80,0x80,0x44,0x38,0x00,0x00},//"C",35//
{0x00,0x00,0x00,0xF0,0x48,0x44,0x44,0x44,0x44,0x44,0x48,0xF0,0x00,0x00},//"D",36//
{0x00,0x00,0x00,0xFC,0x44,0x40,0x48,0x78,0x48,0x40,0x44,0xFC,0x00,0x00},//"E",37//
{0x00,0x00,0x00,0xFC,0x42,0x40,0x48,0x78,0x48,0x40,0x40,0xE0,0x00,0x00},//"F",38//
{0x00,0x00,0x00,0x3C,0x44,0x84,0x80,0x80,0x8E,0x84,0x44,0x38,0x00,0x00},//"G",39//
{0x00,0x00,0x00,0xEE,0x44,0x44,0x44,0x7C,0x44,0x44,0x44,0xEE,0x00,0x00},//"H",40//
{0x00,0x00,0x00,0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00},//"I",41//
{0x00,0x00,0x00,0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x90,0xE0},//"J",42//
{0x00,0x00,0x00,0xEE,0x44,0x48,0x50,0x70,0x48,0x48,0x44,0xEE,0x00,0x00},//"K",43//
{0x00,0x00,0x00,0xE0,0x40,0x40,0x40,0x40,0x40,0x40,0x44,0xFC,0x00,0x00},//"L",44//
{0x00,0x00,0x00,0xEE,0x6C,0x6C,0x6C,0x54,0x54,0x54,0x54,0xD6,0x00,0x00},//"M",45//
{0x00,0x00,0x00,0xEE,0x64,0x64,0x54,0x54,0x54,0x4C,0x4C,0xE4,0x00,0x00},//"N",46//
{0x00,0x00,0x00,0x30,0x48,0x84,0x84,0x84,0x84,0x84,0x48,0x30,0x00,0x00},//"O",47//
{0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x78,0x40,0x40,0x40,0xE0,0x00,0x00},//"P",48//
{0x00,0x00,0x00,0x30,0x48,0x84,0x84,0x84,0x84,0xB4,0x48,0x38,0x0C,0x00},//"Q",49//
{0x00,0x00,0x00,0xF8,0x44,0x44,0x78,0x50,0x48,0x48,0x44,0xE6,0x00,0x00},//"R",50//
{0x00,0x00,0x00,0x3C,0x44,0x40,0x20,0x18,0x04,0x04,0x44,0x78,0x00,0x00},//"S",51//
{0x00,0x00,0x00,0xFE,0x92,0x10,0x10,0x10,0x10,0x10,0x10,0x38,0x00,0x00},//"T",52//
{0x00,0x00,0x00,0xEE,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0x38,0x00,0x00},//"U",53//
{0x00,0x00,0x00,0xEE,0x44,0x44,0x28,0x28,0x28,0x28,0x10,0x10,0x00,0x00},//"V",54//
{0x00,0x00,0x00,0xD6,0x54,0x54,0x54,0x54,0x6C,0x28,0x28,0x28,0x00,0x00},//"W",55//
{0x00,0x00,0x00,0xEE,0x44,0x28,0x28,0x10,0x28,0x28,0x44,0xEE,0x00,0x00},//"X",56//
{0x00,0x00,0x00,0xEE,0x44,0x28,0x28,0x10,0x10,0x10,0x10,0x38,0x00,0x00},//"Y",57//
{0x00,0x00,0x00,0x7C,0x88,0x08,0x10,0x20,0x20,0x40,0x44,0xFC,0x00,0x00},//"Z",58//
{0x00,0x1C,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1C,0x00},//"[",59//
{0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x70,0x00},//"]",60//
{0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x70,0x00},//"]",61//
{0x00,0x10,0x28,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//"^",62//
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFE},//"_",63//
{0x00,0x60,0x60,0x20,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},//"'",64//
{0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x44,0x1C,0x24,0x44,0x3E,0x00,0x00},//"a",65//
{0x00,0x00,0x00,0xC0,0x40,0x40,0x78,0x44,0x44,0x44,0x44,0x78,0x00,0x00},//"b",66//
{0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x44,0x40,0x40,0x44,0x38,0x00,0x00},//"c",67//
{0x00,0x00,0x00,0x0C,0x04,0x04,0x3C,0x44,0x44,0x44,0x44,0x3E,0x00,0x00},//"d",68//
{0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x44,0x7C,0x40,0x44,0x38,0x00,0x00},//"e",69//
{0x00,0x00,0x00,0x1C,0x24,0x20,0xF8,0x20,0x20,0x20,0x20,0x78,0x00,0x00},//"f",70//
{0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x48,0x48,0x30,0x40,0x78,0x44,0x38},//"g",71//
{0x00,0x00,0x00,0xC0,0x40,0x40,0x58,0x64,0x44,0x44,0x44,0xEE,0x00,0x00},//"h",72//
{0x00,0x00,0x00,0x10,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x7C,0x00,0x00},//"i",73//
{0x00,0x00,0x00,0x08,0x00,0x00,0x38,0x08,0x08,0x08,0x08,0x08,0x08,0x70},//"j",74//
{0x00,0x00,0x00,0xC0,0x40,0x40,0x5C,0x48,0x50,0x70,0x48,0xEC,0x00,0x00},//"k",75//
{0x00,0x00,0x00,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00},//"l",76//
{0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x54,0x54,0x54,0x54,0xD6,0x00,0x00},//"m",77//
{0x00,0x00,0x00,0x00,0x00,0x00,0xD8,0x64,0x44,0x44,0x44,0xEE,0x00,0x00},//"n",78//
{0x00,0x00,0x00,0x00,0x00,0x00,0x38,0x44,0x44,0x44,0x44,0x38,0x00,0x00},//"o",79//
{0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x44,0x44,0x44,0x44,0x78,0x40,0xE0},//"p",80//
{0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x44,0x44,0x44,0x44,0x3C,0x04,0x0E},//"q",81//
{0x00,0x00,0x00,0x00,0x00,0x00,0xEC,0x30,0x20,0x20,0x20,0xF8,0x00,0x00},//"r",82//
{0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x44,0x30,0x08,0x44,0x78,0x00,0x00},//"s",83//
{0x00,0x00,0x00,0x00,0x10,0x10,0x7C,0x10,0x10,0x10,0x10,0x0C,0x00,0x00},//"t",84//
{0x00,0x00,0x00,0x00,0x00,0x00,0xCC,0x44,0x44,0x44,0x44,0x3E,0x00,0x00},//"u",85//
{0x00,0x00,0x00,0x00,0x00,0x00,0xEE,0x44,0x28,0x28,0x28,0x10,0x00,0x00},//"v",86//
{0x00,0x00,0x00,0x00,0x00,0x00,0xD6,0x54,0x54,0x54,0x28,0x28,0x00,0x00},//"w",87//
{0x00,0x00,0x00,0x00,0x00,0x00,0xDC,0x48,0x30,0x30,0x48,0xEC,0x00,0x00},//"x",88//
{0x00,0x00,0x00,0x00,0x00,0x00,0xEE,0x44,0x28,0x28,0x10,0x10,0x20,0xE0},//"y",89//
{0x00,0x00,0x00,0x00,0x00,0x00,0x7C,0x48,0x10,0x10,0x24,0x7C,0x00,0x00},//"z",90//
{0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10},//"|",91//
};
#endif //LCDvoid HalLcdInit(void)
{
#if (HAL_LCD == TRUE)HalLcd_HW_Init();
#endif
}void HalLcdWriteValue ( uint32 value, const uint8 radix, uint8 option)
{
#if (HAL_LCD == TRUE)uint8 buf[LCD_MAX_BUF];_ltoa( value, &buf[0], radix );HalLcdWriteString( (char*)buf, option );
#endif
}void HalLcdWriteScreen( char *line1, char *line2 )
{
#if (HAL_LCD == TRUE)HalLcdWriteString( line1, 0 );//andy HalLcdWriteString( line2, 1 );
#endif
}void HalLcdWriteStringValue( char *title, uint16 value, uint8 format, uint8 line )
{
#if (HAL_LCD == TRUE)uint8 tmpLen;uint8 buf[LCD_MAX_BUF];uint32 err;tmpLen = (uint8)osal_strlen( (char*)title );osal_memcpy( buf, title, tmpLen );//buf[tmpLen] = ' ';err = (uint32)(value);_ltoa( err, &buf[tmpLen], format );HalLcdWriteString( (char*)buf, line );
#endif
}void HalLcdWriteStringValueValue( char *title, uint16 value1, uint8 format1,uint16 value2, uint8 format2, uint8 line )
{
#if (HAL_LCD == TRUE)uint8 tmpLen;uint8 buf[LCD_MAX_BUF];uint32 err;tmpLen = (uint8)osal_strlen( (char*)title );if ( tmpLen ){osal_memcpy( buf, title, tmpLen );//buf[tmpLen++] = ' ';}err = (uint32)(value1);_ltoa( err, &buf[tmpLen], format1 );tmpLen = (uint8)osal_strlen( (char*)buf );buf[tmpLen++] = ',';buf[tmpLen++] = ' ';err = (uint32)(value2);_ltoa( err, &buf[tmpLen], format2 );HalLcdWriteString( (char *)buf, line );     #endif
}#if (HAL_LCD == TRUE)
void DelayMS(unsigned int msec)
{ unsigned int i;while(msec >0){for (i=0; i<8000; i++) asm("NOP"); //1msmsec--;}
}//option 1=第1行  2=第2行 其它类推
void HalLcdWriteString (char *s, uint8 option)
{  Color = BLUE;Color_BK = WHITE; DrawRectFill(7,option*15+20,118,15,WHITE); LCD_write_EN_string(7,option*15+20,(unsigned char *)s);
}//向SPI总线传输一个8位数据
void SendByte(unsigned char Data)
{unsigned char i=0;for(i=8;i>0;i--){if(Data&0x80)  LCD_SDA_SET;//输出数据else LCD_SDA_CLR;LCD_SCL_CLR;       LCD_SCL_SET;Data<<=1; }
}//向SPI总线传输一个8位数据
void  SPI_WriteData(unsigned char Data)
{unsigned char i=0;for(i=8;i>0;i--){if(Data&0x80)  LCD_SDA_SET;//输出数据else LCD_SDA_CLR;LCD_SCL_CLR;       LCD_SCL_SET;Data<<=1; }
}//向液晶屏写一个8位指令
void LCD_WrCmd(unsigned char Index)
{ //LCD_CS_CLR;  //ALD del  SPI写命令时序开始LCD_DC_CLR;SPI_WriteData(Index);//LCD_CS_SET;  //ALD del
}//向液晶屏写一个8位数据
void LCD_WrDat(unsigned char Data)
{//LCD_CS_CLR;  //ALD delLCD_DC_SET;SPI_WriteData(Data);//LCD_CS_SET;  //ALD del
}//向液晶屏写一个16位数据
void LCD_WriteData_16Bit(unsigned short Data)
{//LCD_CS_CLR;  //ALD delLCD_DC_SET;SPI_WriteData(Data>>8); //写入高8位数据SPI_WriteData(Data);       //写入低8位数据//LCD_CS_SET;  //ALD del
}static void LCD_write_byte(unsigned char data, unsigned char command)
{LCD_CS_CLR ;// 使能LCDif (command == 0)LCD_WrCmd(data); // 传送命令elseLCD_WrDat(data) ; // 传送数据//SendByte(data);//LCD_CS_SET; // 关闭LCD//LCD_DC_SET;
}// 复位lcd
void Lcd_Reset(void)
{LCD_RST_CLR;DelayMS(110);LCD_RST_SET;DelayMS(50);
}static void LCD_set_window(unsigned int X    , unsigned int Y, unsigned int Width, unsigned int Height)
{LCD_write_byte(0x2A,LCD_COMMAND);         //列地址设置命令LCD_write_byte(X>>8,LCD_DATA) ;           //设置显示窗口左上角横坐标高位LCD_write_byte((X+2)&0xFF,LCD_DATA);      //设置显示窗口左上角横坐标低位     LCD_write_byte((X+Width+1)>>8,LCD_DATA);  //设置宽度高位  LCD_write_byte((X+Width+1)&0xFF,LCD_DATA);// 设置宽度低位   LCD_write_byte(0x2B,LCD_COMMAND);         //行地址设置命令LCD_write_byte(Y>>8,LCD_DATA) ;           //设置显示窗口左上角纵坐标高位LCD_write_byte((Y+3)&0xFF,LCD_DATA);      //设置显示窗口左上角纵坐标低位 LCD_write_byte((Y+Height+2)>>8,LCD_DATA); // 设置显示窗口高度高位  LCD_write_byte((Y+Height+2)&0xFF,LCD_DATA);// 设置显示窗口高度低位    LCD_write_byte(0x2C,LCD_COMMAND) ;
}void DrawPixel(unsigned int x, unsigned int y, unsigned int color)
{//LCD_set_XY(x,y);  //设定显示位置 andyLCD_write_byte(color>>8,LCD_DATA);  //发送显示颜色数据高位字节LCD_write_byte(color&0xFF,LCD_DATA);//发送显示颜色数据低位字节
}void DrawVerticalLine(unsigned int x, unsigned int y, unsigned int Length,unsigned int Color)
{register int  index = 0;register char ch,cl;ch = (unsigned char)(Color>>8);cl = (unsigned char)(Color);LCD_set_window(x,y,1,Length);for(index=0;index<Length;index++){LCD_write_byte(ch,LCD_DATA) ;LCD_write_byte(cl,LCD_DATA) ;}
}void DrawHorizonLine(unsigned int x, unsigned int y, unsigned int Length,unsigned int Color)
{register int  index = 0;register char ch,cl;ch = (unsigned char)(Color>>8);cl = (unsigned char)(Color);LCD_set_window(x,y,Length,1);for(index=0;index<Length;index++){LCD_write_byte(ch,LCD_DATA);LCD_write_byte(cl,LCD_DATA);}
}void DrawRect(unsigned int Xpos, unsigned int Ypos, unsigned int Width, unsigned int Height,unsigned Color)
{DrawHorizonLine(Xpos, Ypos, Width, Color) ;DrawHorizonLine(Xpos, (Ypos + Height), Width, Color) ;DrawVerticalLine(Xpos, Ypos, Height, Color) ;DrawVerticalLine((Xpos+Width-1) ,Ypos, Height, Color);
}void DrawRectFill(unsigned int Xpos  , unsigned int Ypos, unsigned int Width, unsigned int Height, unsigned Color                        )
{register unsigned int i;register unsigned int index = Width*Height/16+1;register char ch,cl;ch = (unsigned char)(Color>>8);cl = (unsigned char)(Color);LCD_set_window(Xpos,Ypos,Width,Height);LCD_write_byte(0x2C,LCD_COMMAND);//LCD_CS_CLR ; andyLCD_DC_SET ;for(i=0;i<index;i++){SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl); SendByte(ch);SendByte(cl);      SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);}
}void LCD_clear(unsigned int color)
{register unsigned int  i=0,j=0;register unsigned char ch;register unsigned char cl;LCD_set_window(0,0,X_MAX_PIXEL,Y_MAX_PIXEL);ch = color>>8;cl = color&0xFF;LCD_write_byte(0x2C,LCD_COMMAND);//LCD_CS_CLR;LCD_DC_SET;for (i=0;i<Y_MAX_PIXEL;i++)for (j=0;j<2;j++){SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);      SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);SendByte(ch);SendByte(cl);}LCD_DC_SET;//LCD_CS_SET;
}static void LCD_write_EN(unsigned int X, unsigned int Y, unsigned char c)
{unsigned char i,j,temp;LCD_set_window(X,Y,7,14);//LCD_CS_CLR ;LCD_DC_SET ;for(i=0;i<14;i++){temp = ASCII7x14[c-32][i];for(j=0;j<7;j++){if(temp&0x80){SendByte(Color>>8);  SendByte(Color) ;}else{SendByte(Color_BK>>8) ;  SendByte(Color_BK);}            temp <<= 1 ;}}//LCD_CS_SET ; andyLCD_DC_SET ;
}void LCD_write_EN_string(unsigned char X,unsigned char Y,uint8 *s)
{unsigned char i=0;while (*s){LCD_write_EN(X+i*7,Y,*s);s++;i++;if(i>=HAL_LCD_MAX_CHARS)return;}return;
}static void LCD_write_CN(unsigned int X, unsigned int Y, unsigned char c)
{unsigned char i,j,temp;LCD_set_window(X,Y,14,14);  //按字符点阵大小设置显示窗口//LCD_CS_CLR;               //液晶SPI使能andyLCD_DC_SET;                 //发送数据信号使能for(i=0;i<28;i++)           // 14x14字符点阵数据大小为2x14=28字节{temp = CN14x14[c][i] ;    //读字模数组for(j=0;j<8;j++)          // 按位显示{if(temp&0x80)           //如果位值为1,显示字符色{SendByte(Color>>8);  SendByte(Color) ;}else                    //如果位值为0,显示背景色{SendByte(Color_BK>>8) ;  SendByte(Color_BK);}            temp <<= 1 ;}i++ ;                     //指向下一字节temp = CN14x14[c][i];for(j=0;j<6;j++)          // 按位显示,点阵横向为14像素,{                         // 所以每行第2字节最后2位抛弃if(temp&0x80)           // 如果位值为1,显示字符色{SendByte(Color>>8);  SendByte(Color) ;}else                    // 如果位值为0,显示背景色{SendByte(Color_BK>>8) ;  SendByte(Color_BK);}            temp <<= 1 ;}}//LCD_CS_SET;               //液晶SPI禁止LCD_DC_SET;                 //数据控制信号禁止
}void LCD_write_CN_string(unsigned char X,unsigned char Y,uint8 *s)
{unsigned char i=0;static unsigned char j=0;while (*s){for(j=0;j<ALL_CNS;j++)                  //搜索点阵字模{ if(  *s    ==CN14x14_Index[j*2]            &&*(s+1)==CN14x14_Index[j*2+1])    //汉字内码存在于字模索引数组中{LCD_write_CN(X+i*14,Y,j) ;          //显示汉字break; }}if(j>=ALL_CNS)                          //无此字的点阵字模{DrawRectFill(X+i*14,Y,14,14,Color_BK);//显示空格}s++; s++ ;                              //字符指针移位i++;if(i>=HAL_LCD_MAX_CHARS/2)  return;                               //超过最大显示字符数,返回}return ;
}void ShowImage(const unsigned char *p)
{int i; unsigned char picH,picL;LCD_set_window(24,5,80,40); //坐标设置for(i=0;i<80*40;i++){   picL=*(p+i*2);    //数据低位在前picH=*(p+i*2+1);             LCD_WriteData_16Bit(picH<<8|picL);                        }
}//初始化LCD
void HalLcd_HW_Init(void)
{P0SEL &= 0xFE; //设置P00为普通IO口,P0DIR |= 0x01; //设置P00为输出P1SEL &= 0x73; //设置P12,P13,P17为普通IO口P1DIR |= 0x8c; //设置P12,P13,P17为输出 Lcd_Reset();#ifdef HARDWARE_SPI                                         P1DIR &= 0x9F;SPI_Init();   //初始化SPI端口
#endifLCD_write_byte(0x11,LCD_COMMAND); //退出睡眠模式DelayMS(1); ///ST7735R Frame RateLCD_write_byte(0xB1,LCD_COMMAND); LCD_write_byte(0x01,LCD_DATA); LCD_write_byte(0x2C,LCD_DATA); LCD_write_byte(0x2D,LCD_DATA); LCD_write_byte(0xB2,LCD_COMMAND); LCD_write_byte(0x01,LCD_DATA); LCD_write_byte(0x2C,LCD_DATA); LCD_write_byte(0x2D,LCD_DATA); LCD_write_byte(0xB3,LCD_COMMAND); LCD_write_byte(0x01,LCD_DATA); LCD_write_byte(0x2C,LCD_DATA); LCD_write_byte(0x2D,LCD_DATA); LCD_write_byte(0x01,LCD_DATA); LCD_write_byte(0x2C,LCD_DATA); LCD_write_byte(0x2D,LCD_DATA); LCD_write_byte(0xB4,LCD_COMMAND);//列对调LCD_write_byte(0x07,LCD_DATA); //ST7735R加电序列LCD_write_byte(0xC0,LCD_COMMAND); LCD_write_byte(0xA2,LCD_DATA); LCD_write_byte(0x02,LCD_DATA); LCD_write_byte(0x84,LCD_DATA); LCD_write_byte(0xC1,LCD_COMMAND); LCD_write_byte(0xC5,LCD_DATA); LCD_write_byte(0xC2,LCD_COMMAND); LCD_write_byte(0x0A,LCD_DATA); LCD_write_byte(0x00,LCD_DATA); LCD_write_byte(0xC3,LCD_COMMAND); LCD_write_byte(0x8A,LCD_DATA); LCD_write_byte(0x2A,LCD_DATA); LCD_write_byte(0xC4,LCD_COMMAND); LCD_write_byte(0x8A,LCD_DATA); LCD_write_byte(0xEE,LCD_DATA); LCD_write_byte(0xC5,LCD_COMMAND); //加VCOM LCD_write_byte(0x0E,LCD_DATA); LCD_write_byte(0x36,LCD_COMMAND); //RGB模式 LCD_write_byte(0xC8,LCD_DATA); // ST7735R Gamma 校正序列LCD_write_byte(0xe0,LCD_COMMAND); LCD_write_byte(0x0f,LCD_DATA); LCD_write_byte(0x1a,LCD_DATA); LCD_write_byte(0x0f,LCD_DATA); LCD_write_byte(0x18,LCD_DATA); LCD_write_byte(0x2f,LCD_DATA); LCD_write_byte(0x28,LCD_DATA); LCD_write_byte(0x20,LCD_DATA); LCD_write_byte(0x22,LCD_DATA); LCD_write_byte(0x1f,LCD_DATA); LCD_write_byte(0x1b,LCD_DATA); LCD_write_byte(0x23,LCD_DATA); LCD_write_byte(0x37,LCD_DATA); LCD_write_byte(0x00,LCD_DATA); LCD_write_byte(0x07,LCD_DATA); LCD_write_byte(0x02,LCD_DATA); LCD_write_byte(0x10,LCD_DATA); LCD_write_byte(0xe1,LCD_COMMAND); LCD_write_byte(0x0f,LCD_DATA); LCD_write_byte(0x1b,LCD_DATA); LCD_write_byte(0x0f,LCD_DATA); LCD_write_byte(0x17,LCD_DATA); LCD_write_byte(0x33,LCD_DATA); LCD_write_byte(0x2c,LCD_DATA); LCD_write_byte(0x29,LCD_DATA); LCD_write_byte(0x2e,LCD_DATA); LCD_write_byte(0x30,LCD_DATA); LCD_write_byte(0x30,LCD_DATA); LCD_write_byte(0x39,LCD_DATA); LCD_write_byte(0x3f,LCD_DATA); LCD_write_byte(0x00,LCD_DATA); LCD_write_byte(0x07,LCD_DATA); LCD_write_byte(0x03,LCD_DATA); LCD_write_byte(0x10,LCD_DATA);  LCD_write_byte(0x2a,LCD_COMMAND);LCD_write_byte(0x00,LCD_DATA);LCD_write_byte(0x00,LCD_DATA);LCD_write_byte(0x00,LCD_DATA);LCD_write_byte(0x7f,LCD_DATA);LCD_write_byte(0x2b,LCD_COMMAND);LCD_write_byte(0x00,LCD_DATA);LCD_write_byte(0x00,LCD_DATA);LCD_write_byte(0x00,LCD_DATA);LCD_write_byte(0x9f,LCD_DATA);LCD_write_byte(0xF0,LCD_COMMAND); //使能测试命令  LCD_write_byte(0x01,LCD_DATA); LCD_write_byte(0xF6,LCD_COMMAND); //关闭ram省电模式 LCD_write_byte(0x00,LCD_DATA); LCD_write_byte(0x3A,LCD_COMMAND); //色深16bit LCD_write_byte(0x05,LCD_DATA); LCD_write_byte(0x29,LCD_COMMAND); //开显示  LCD_clear(GREEN);                 //清屏
}
#endif
hal_led.c
#include "hal_mcu.h"
#include "hal_defs.h"
#include "hal_types.h"
#include "hal_drivers.h"
#include "hal_led.h"
#include "osal.h"
#include "hal_board.h"/* LED control structure */
typedef struct {uint8 mode;       /* Operation mode */uint8 todo;       /* Blink cycles left */uint8 onPct;      /* On cycle percentage */uint16 time;      /* On/off cycle time (msec) */uint32 next;      /* Time for next change */
} HalLedControl_t;typedef struct
{HalLedControl_t HalLedControlTable[HAL_LED_DEFAULT_MAX_LEDS];uint8           sleepActive;
} HalLedStatus_t;static uint8 HalLedState;              // LED state at last set/clr/blink update#if HAL_LED == TRUE
static uint8 HalSleepLedState;         // LED state at last set/clr/blink update
static uint8 preBlinkState;            // Original State before going to blink mode// bit 0, 1, 2, 3 represent led 0, 1, 2, 3
#endif#ifdef BLINK_LEDSstatic HalLedStatus_t HalLedStatusControl;
#endifvoid HalLedInit (void)
{
#if (HAL_LED == TRUE)/* Initialize all LEDs to OFF */HalLedSet (HAL_LED_ALL, HAL_LED_MODE_OFF);
#endif /* HAL_LED */
#ifdef BLINK_LEDS/* Initialize sleepActive to FALSE */HalLedStatusControl.sleepActive = FALSE;
#endif
}uint8 HalLedSet (uint8 leds, uint8 mode)
{#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)uint8 led;HalLedControl_t *sts;switch (mode){case HAL_LED_MODE_BLINK:/* Default blink, 1 time, D% duty cycle */HalLedBlink (leds, 1, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);break;case HAL_LED_MODE_FLASH:/* Default flash, N times, D% duty cycle */HalLedBlink (leds, HAL_LED_DEFAULT_FLASH_COUNT, HAL_LED_DEFAULT_DUTY_CYCLE, HAL_LED_DEFAULT_FLASH_TIME);break;case HAL_LED_MODE_ON:case HAL_LED_MODE_OFF:case HAL_LED_MODE_TOGGLE:led = HAL_LED_1;leds &= HAL_LED_ALL;sts = HalLedStatusControl.HalLedControlTable;while (leds){if (leds & led){if (mode != HAL_LED_MODE_TOGGLE){sts->mode = mode;  /* ON or OFF */}else{sts->mode ^= HAL_LED_MODE_ON;  /* Toggle */}HalLedOnOff (led, sts->mode);leds ^= led;}led <<= 1;sts++;}break;default:break;}#elif (HAL_LED == TRUE)LedOnOff(leds, mode);
#else// HAL LED is disabled, suppress unused argument warnings(void) leds;(void) mode;
#endif /* BLINK_LEDS && HAL_LED   */return ( HalLedState );}void HalLedBlink (uint8 leds, uint8 numBlinks, uint8 percent, uint16 period)
{
#if (defined (BLINK_LEDS)) && (HAL_LED == TRUE)uint8 led;HalLedControl_t *sts;if (leds && percent && period){if (percent < 100){led = HAL_LED_1;leds &= HAL_LED_ALL;sts = HalLedStatusControl.HalLedControlTable;while (leds){if (leds & led){/* Store the current state of the led before going to blinking if not already blinking */if(sts->mode < HAL_LED_MODE_BLINK )preBlinkState |= (led & HalLedState);sts->mode  = HAL_LED_MODE_OFF;                    /* Stop previous blink */sts->time  = period;                              /* Time for one on/off cycle */sts->onPct = percent;                             /* % of cycle LED is on */sts->todo  = numBlinks;                           /* Number of blink cycles */if (!numBlinks) sts->mode |= HAL_LED_MODE_FLASH;  /* Continuous */sts->next = osal_GetSystemClock();                /* Start now */sts->mode |= HAL_LED_MODE_BLINK;                  /* Enable blinking */leds ^= led;}led <<= 1;sts++;}// Cancel any overlapping timer for blink eventsosal_stop_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT);osal_set_event (Hal_TaskID, HAL_LED_BLINK_EVENT);}else{HalLedSet (leds, HAL_LED_MODE_ON);                    /* >= 100%, turn on */}}else{HalLedSet (leds, HAL_LED_MODE_OFF);                     /* No on time, turn off */}
#elif (HAL_LED == TRUE)percent = (leds & HalLedState) ? HAL_LED_MODE_OFF : HAL_LED_MODE_ON;HalLedOnOff (leds, percent);                              /* Toggle */
#else// HAL LED is disabled, suppress unused argument warnings(void) leds;(void) numBlinks;(void) percent;(void) period;
#endif /* BLINK_LEDS && HAL_LED */
}#if (HAL_LED == TRUE)void HalLedUpdate (void)
{uint8 led;uint8 pct;uint8 leds;HalLedControl_t *sts;uint32 time;uint16 next;uint16 wait;next = 0;led  = HAL_LED_1;leds = HAL_LED_ALL;sts = HalLedStatusControl.HalLedControlTable;/* Check if sleep is active or not */if (!HalLedStatusControl.sleepActive){while (leds){if (leds & led){if (sts->mode & HAL_LED_MODE_BLINK){time = osal_GetSystemClock();if (time >= sts->next){if (sts->mode & HAL_LED_MODE_ON){pct = 100 - sts->onPct;               /* Percentage of cycle for off */sts->mode &= ~HAL_LED_MODE_ON;        /* Say it's not on */HalLedOnOff (led, HAL_LED_MODE_OFF);  /* Turn it off */if (!(sts->mode & HAL_LED_MODE_FLASH)){sts->todo--;                        /* Not continuous, reduce count */}}            else if ( (!sts->todo) && !(sts->mode & HAL_LED_MODE_FLASH) ){sts->mode ^= HAL_LED_MODE_BLINK;      /* No more blinks */  }            else{pct = sts->onPct;                     /* Percentage of cycle for on */sts->mode |= HAL_LED_MODE_ON;         /* Say it's on */HalLedOnOff (led, HAL_LED_MODE_ON);   /* Turn it on */}if (sts->mode & HAL_LED_MODE_BLINK){wait = (((uint32)pct * (uint32)sts->time) / 100);sts->next = time + wait;}else{/* no more blink, no more wait */wait = 0;/* After blinking, set the LED back to the state before it blinks */HalLedSet (led, ((preBlinkState & led)!=0)?HAL_LED_MODE_ON:HAL_LED_MODE_OFF);/* Clear the saved bit */preBlinkState &= (led ^ 0xFF);}}else{wait = sts->next - time;  /* Time left */}if (!next || ( wait && (wait < next) )){next = wait;}}leds ^= led;}led <<= 1;sts++;}if (next){osal_start_timerEx(Hal_TaskID, HAL_LED_BLINK_EVENT, next);   /* Schedule event */}}
}void HalLedOnOff (uint8 leds, uint8 mode)
{if (leds & HAL_LED_1){if (mode == HAL_LED_MODE_ON){HAL_TURN_ON_LED1();}else{HAL_TURN_OFF_LED1();}}if (leds & HAL_LED_2){if (mode == HAL_LED_MODE_ON){HAL_TURN_ON_LED2();}else{HAL_TURN_OFF_LED2();}}if (leds & HAL_LED_3){if (mode == HAL_LED_MODE_ON){HAL_TURN_ON_LED3();}else{HAL_TURN_OFF_LED3();}}if (leds & HAL_LED_4){if (mode == HAL_LED_MODE_ON){HAL_TURN_ON_LED4();}else{HAL_TURN_OFF_LED4();}}/* Remember current state */if (mode){HalLedState |= leds;}else{HalLedState &= (leds ^ 0xFF);}
}
#endif /* HAL_LED */uint8 HalLedGetState ()
{
#if (HAL_LED == TRUE)return HalLedState;
#elsereturn 0;
#endif
}void HalLedEnterSleep( void )
{
#ifdef BLINK_LEDS/* Sleep ON */HalLedStatusControl.sleepActive = TRUE;
#endif /* BLINK_LEDS */#if (HAL_LED == TRUE)/* Save the state of each led */HalSleepLedState = 0;HalSleepLedState |= HAL_STATE_LED1();HalSleepLedState |= HAL_STATE_LED2() << 1;HalSleepLedState |= HAL_STATE_LED3() << 2;HalSleepLedState |= HAL_STATE_LED4() << 3;/* TURN OFF all LEDs to save power */HalLedOnOff (HAL_LED_ALL, HAL_LED_MODE_OFF);
#endif /* HAL_LED */}void HalLedExitSleep( void )
{
#if (HAL_LED == TRUE)/* Load back the saved state */HalLedOnOff(HalSleepLedState, HAL_LED_MODE_ON);/* Restart - This takes care BLINKING LEDS */HalLedUpdate();
#endif /* HAL_LED */#ifdef BLINK_LEDS/* Sleep OFF */HalLedStatusControl.sleepActive = FALSE;
#endif /* BLINK_LEDS */
}
hal_sleep.c
#include "hal_types.h"
#include "hal_mcu.h"
#include "hal_board.h"
#include "hal_sleep.h"
#include "hal_led.h"
#include "hal_key.h"
#include "mac_api.h"
#include "OSAL.h"
#include "OSAL_Timers.h"
#include "OSAL_Tasks.h"
#include "OSAL_PwrMgr.h"
#include "OnBoard.h"
#include "hal_drivers.h"
#include "hal_assert.h"
#include "mac_mcu.h"#ifndef ZG_BUILD_ENDDEVICE_TYPE
# define ZG_BUILD_ENDDEVICE_TYPE FALSE
#endif#if ZG_BUILD_ENDDEVICE_TYPE && defined (NWK_AUTO_POLL)
#include "nwk_globals.h"
#include "ZGlobals.h"
#endif/* POWER CONSERVATION DEFINITIONS* Sleep mode H/W definitions (enabled with POWER_SAVING compile option)*/
#define CC2530_PM0            0  /* PM0, Clock oscillators on, voltage regulator on */
#define CC2530_PM1            1  /* PM1, 32.768 kHz oscillators on, voltage regulator on */
#define CC2530_PM2            2  /* PM2, 32.768 kHz oscillators on, voltage regulator off */
#define CC2530_PM3            3  /* PM3, All clock oscillators off, voltage regulator off */#define HAL_SLEEP_OFF         CC2530_PM0
#define HAL_SLEEP_TIMER       CC2530_PM2
#define HAL_SLEEP_DEEP        CC2530_PM3#define MAX_SLEEP_TIME                   510000
#define TICKS_SUBTRACTED                 2#if !defined (PM_MIN_SLEEP_TIME)
#define PM_MIN_SLEEP_TIME                14
#endif#pragma location = "SLEEP_CODE"
void halSetSleepMode(void);#define HAL_SLEEP_ADJ_TICKS   (11 + 12)#ifndef HAL_SLEEP_DEBUG_POWER_MODE
/* set CC2530 power mode; always use PM2 */
#define HAL_SLEEP_PREP_POWER_MODE(mode)     st( SLEEPCMD &= ~PMODE; /* clear mode bits */    \SLEEPCMD |= mode;   /* set mode bits   */    \while (!(STLOAD & LDRDY));                   \halSleepPconValue = PCON_IDLE;               \)
#define HAL_SLEEP_SET_POWER_MODE()          halSetSleepMode()
#else
/* Debug: don't set power mode, just block until sleep timer interrupt */
#define HAL_SLEEP_PREP_POWER_MODE(mode)     /* nothing */
#define HAL_SLEEP_SET_POWER_MODE()          st( while(halSleepInt == FALSE); \halSleepInt = FALSE;         \HAL_DISABLE_INTERRUPTS();    \)
#endif/* sleep and external interrupt port masks */
#define STIE_BV                             BV(5)
#define P0IE_BV                             BV(5)
#define P1IE_BV                             BV(4)
#define P2IE_BV                             BV(1)/* sleep timer interrupt control */
#define HAL_SLEEP_TIMER_ENABLE_INT()        st(IEN0 |= STIE_BV;)     /* enable sleep timer interrupt */
#define HAL_SLEEP_TIMER_DISABLE_INT()       st(IEN0 &= ~STIE_BV;)    /* disable sleep timer interrupt */
#define HAL_SLEEP_TIMER_CLEAR_INT()         st(STIF = 0;)            /* clear sleep interrupt flag *//* backup interrupt enable registers before sleep */
#define HAL_SLEEP_IE_BACKUP_AND_DISABLE(ien0, ien1, ien2) st(ien0  = IEN0;    /* backup IEN0 register */ \ien1  = IEN1;    /* backup IEN1 register */ \ien2  = IEN2;    /* backup IEN2 register */ \IEN0 &= STIE_BV; /* disable IEN0 except STIE */ \IEN1 &= P0IE_BV; /* disable IEN1 except P0IE */ \IEN2 &= (P1IE_BV|P2IE_BV);) /* disable IEN2 except P1IE, P2IE *//* restore interrupt enable registers before sleep */
#define HAL_SLEEP_IE_RESTORE(ien0, ien1, ien2) st(IEN0 = ien0;   /* restore IEN0 register */ \IEN1 = ien1;   /* restore IEN1 register */ \IEN2 = ien2;)  /* restore IEN2 register *//* convert msec to 320 usec units with round */
#define HAL_SLEEP_MS_TO_320US(ms)           (((((uint32) (ms)) * 100) + 31) / 32)/* for optimized indexing of uint32's */
#if HAL_MCU_LITTLE_ENDIAN()
#define UINT32_NDX0   0
#define UINT32_NDX1   1
#define UINT32_NDX2   2
#define UINT32_NDX3   3
#else
#define UINT32_NDX0   3
#define UINT32_NDX1   2
#define UINT32_NDX2   1
#define UINT32_NDX3   0
#endifstatic uint32 maxSleepLoopTime =  HAL_SLEEP_MS_TO_320US(MAX_SLEEP_TIME);static uint8 halPwrMgtMode = HAL_SLEEP_OFF;#ifdef HAL_SLEEP_DEBUG_POWER_MODE
static bool halSleepInt = FALSE;
#endifvoid halSleepSetTimer(uint32 timeout);void halSetSleepMode(void)
{PCON = halSleepPconValue;HAL_DISABLE_INTERRUPTS();
}void halSetMaxSleepLoopTime(uint32 rolloverTime)
{if( rolloverTime > HAL_SLEEP_MS_TO_320US(MAX_SLEEP_TIME) ){maxSleepLoopTime = HAL_SLEEP_MS_TO_320US(MAX_SLEEP_TIME);}maxSleepLoopTime = (rolloverTime - TICKS_SUBTRACTED);
}void halSleep( uint16 osal_timeout )
{uint32        timeout;uint32        macTimeout = 0;/* get next OSAL timer expiration converted to 320 usec units */timeout = HAL_SLEEP_MS_TO_320US(osal_timeout);if (timeout == 0){timeout = MAC_PwrNextTimeout();}else{/* get next MAC timer expiration */macTimeout = MAC_PwrNextTimeout();/* get lesser of two timeouts */if ((macTimeout != 0) && (macTimeout < timeout)){timeout = macTimeout;}}halPwrMgtMode = (timeout == 0) ? HAL_SLEEP_DEEP : HAL_SLEEP_TIMER;#if ZG_BUILD_ENDDEVICE_TYPE && defined (NWK_AUTO_POLL)if ((timeout > HAL_SLEEP_MS_TO_320US(PM_MIN_SLEEP_TIME)) ||(timeout == 0 && zgPollRate == 0))
#elseif ((timeout > HAL_SLEEP_MS_TO_320US(PM_MIN_SLEEP_TIME)) ||(timeout == 0))
#endif{halIntState_t ien0, ien1, ien2;HAL_ASSERT(HAL_INTERRUPTS_ARE_ENABLED());HAL_DISABLE_INTERRUPTS();/* always use "deep sleep" to turn off radio VREG on CC2530 */if (halSleepPconValue != 0 && MAC_PwrOffReq(MAC_PWR_SLEEP_DEEP) == MAC_SUCCESS){#if ((defined HAL_KEY) && (HAL_KEY == TRUE))/* get peripherals ready for sleep */HalKeyEnterSleep();
#endif#ifdef HAL_SLEEP_DEBUG_LEDHAL_TURN_OFF_LED3();
#else/* use this to turn LEDs off during sleep */HalLedEnterSleep();
#endifif(timeout > maxSleepLoopTime){timeout = maxSleepLoopTime;}  /* enable sleep timer interrupt */if (timeout != 0){if (timeout > HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME )){timeout -= HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME );halSleepSetTimer(HAL_SLEEP_MS_TO_320US( MAX_SLEEP_TIME ));}else{/* set sleep timer */halSleepSetTimer(timeout);}/* set up sleep timer interrupt */HAL_SLEEP_TIMER_CLEAR_INT();HAL_SLEEP_TIMER_ENABLE_INT();}#ifdef HAL_SLEEP_DEBUG_LEDif (halPwrMgtMode == CC2530_PM1){HAL_TURN_ON_LED1();}else{HAL_TURN_OFF_LED1();}
#endif/* Prep CC2530 power mode */HAL_SLEEP_PREP_POWER_MODE(halPwrMgtMode);/* save interrupt enable registers and disable all interrupts */HAL_SLEEP_IE_BACKUP_AND_DISABLE(ien0, ien1, ien2);HAL_ENABLE_INTERRUPTS();HAL_SLEEP_SET_POWER_MODE();/* the interrupt is disabled - see halSetSleepMode() *//* restore interrupt enable registers */HAL_SLEEP_IE_RESTORE(ien0, ien1, ien2);/* disable sleep timer interrupt */HAL_SLEEP_TIMER_DISABLE_INT();#ifdef HAL_SLEEP_DEBUG_LEDHAL_TURN_ON_LED3();
#else/* use this to turn LEDs back on after sleep */HalLedExitSleep();
#endif#if ((defined HAL_KEY) && (HAL_KEY == TRUE))/* handle peripherals */(void)HalKeyExitSleep();
#endif/* power on the MAC; blocks until completion */MAC_PwrOnReq();HAL_ENABLE_INTERRUPTS();macMcuTimer2OverflowWorkaround();}else{HAL_ENABLE_INTERRUPTS();}}
}void halSleepSetTimer(uint32 timeout)
{uint32 ticks;/* read the sleep timer; ST0 must be read first */((uint8 *) &ticks)[UINT32_NDX0] = ST0;((uint8 *) &ticks)[UINT32_NDX1] = ST1;((uint8 *) &ticks)[UINT32_NDX2] = ST2;((uint8 *) &ticks)[UINT32_NDX3] = 0;ticks += (timeout * 671) / 64;/* subtract the processing time spent in function halSleep() */ticks -= HAL_SLEEP_ADJ_TICKS;/* set sleep timer compare; ST0 must be written last */ST2 = ((uint8 *) &ticks)[UINT32_NDX2];ST1 = ((uint8 *) &ticks)[UINT32_NDX1];ST0 = ((uint8 *) &ticks)[UINT32_NDX0];
}uint32 TimerElapsed( void )
{/* Stubs */return (0);
}void halRestoreSleepLevel( void )
{/* Stubs */
}HAL_ISR_FUNCTION(halSleepTimerIsr, ST_VECTOR)
{HAL_ENTER_ISR();HAL_SLEEP_TIMER_CLEAR_INT();#ifdef HAL_SLEEP_DEBUG_POWER_MODEhalSleepInt = TRUE;
#endifCLEAR_SLEEP_MODE();HAL_EXIT_ISR();
}
hal_startup.c
#include "hal_board.h"
#include "hal_types.h"#ifdef __cplusplus
extern "C" {
#endif#pragma language=extended//
// Locate low_level_init in the CSTART module
//
#pragma location="CSTART"
//
// If the code model is banked, low_level_init must be declared
// __near_func elsa a ?BRET is performed
//
#if (__CODE_MODEL__ == 2)
__near_func __root char
#else
__root char
#endif
__low_level_init(void);#if (__CODE_MODEL__ == 2)
__near_func __root char
#else
__root char
#endif
__low_level_init(void)
{// Map flash bank #1 into XDATA for access to "ROM mapped as data".MEMCTR = (MEMCTR & 0xF8) | 0x01;return 1;
}#pragma language=default#ifdef __cplusplus
}
#endif
hal_timer.c
hal_uart.c
#include "hal_board_cfg.h"
#include "hal_defs.h"
#include "hal_types.h"
#include "hal_uart.h"#if HAL_UART_DMA
#include "_hal_uart_dma.c"
#endif
#if HAL_UART_ISR
#include "_hal_uart_isr.c"
#endif
#if HAL_UART_USB
#include "_hal_uart_usb.c"
#endifvoid HalUARTInit(void)
{
#if HAL_UART_DMAHalUARTInitDMA();
#endif
#if HAL_UART_ISRHalUARTInitISR();
#endif
#if HAL_UART_USBHalUARTInitUSB();
#endif
}uint8 HalUARTOpen(uint8 port, halUARTCfg_t *config)
{(void)port;(void)config;#if (HAL_UART_DMA == 1)if (port == HAL_UART_PORT_0)  HalUARTOpenDMA(config);
#endif
#if (HAL_UART_DMA == 2)if (port == HAL_UART_PORT_1)  HalUARTOpenDMA(config);
#endif
#if (HAL_UART_ISR == 1)if (port == HAL_UART_PORT_0)  HalUARTOpenISR(config);
#endif
#if (HAL_UART_ISR == 2)if (port == HAL_UART_PORT_1)  HalUARTOpenISR(config);
#endif
#if (HAL_UART_USB)HalUARTOpenUSB(config);
#endifreturn HAL_UART_SUCCESS;
}uint16 HalUARTRead(uint8 port, uint8 *buf, uint16 len)
{(void)port;(void)buf;(void)len;#if (HAL_UART_DMA == 1)if (port == HAL_UART_PORT_0)  return HalUARTReadDMA(buf, len);
#endif
#if (HAL_UART_DMA == 2)if (port == HAL_UART_PORT_1)  return HalUARTReadDMA(buf, len);
#endif
#if (HAL_UART_ISR == 1)if (port == HAL_UART_PORT_0)  return HalUARTReadISR(buf, len);
#endif
#if (HAL_UART_ISR == 2)if (port == HAL_UART_PORT_1)  return HalUARTReadISR(buf, len);
#endif#if HAL_UART_USBreturn HalUARTRx(buf, len);
#elsereturn 0;
#endif
}uint16 HalUARTWrite(uint8 port, uint8 *buf, uint16 len)
{(void)port;(void)buf;(void)len;#if (HAL_UART_DMA == 1)if (port == HAL_UART_PORT_0)  return HalUARTWriteDMA(buf, len);
#endif
#if (HAL_UART_DMA == 2)if (port == HAL_UART_PORT_1)  return HalUARTWriteDMA(buf, len);
#endif
#if (HAL_UART_ISR == 1)if (port == HAL_UART_PORT_0)  return HalUARTWriteISR(buf, len);
#endif
#if (HAL_UART_ISR == 2)if (port == HAL_UART_PORT_1)  return HalUARTWriteISR(buf, len);
#endif#if HAL_UART_USBHalUARTTx(buf, len);return len;
#elsereturn 0;
#endif
}void HalUARTSuspend( void )
{
#if HAL_UART_DMAHalUARTSuspendDMA();
#endif
#if HAL_UART_ISRHalUARTSuspendISR();
#endif
}void HalUARTResume( void )
{
#if HAL_UART_DMAHalUARTResumeDMA();
#endif
#if HAL_UART_ISRHalUARTResumeISR();
#endif
}void HalUARTPoll(void)
{
#if HAL_UART_DMAHalUARTPollDMA();
#endif
#if HAL_UART_ISRHalUARTPollISR();
#endif
#if HAL_UART_USBHalUARTPollUSB();
#endif
}uint16 Hal_UART_RxBufLen( uint8 port )
{(void)port;#if (HAL_UART_DMA == 1)if (port == HAL_UART_PORT_0)  return HalUARTRxAvailDMA();
#endif
#if (HAL_UART_DMA == 2)if (port == HAL_UART_PORT_1)  return HalUARTRxAvailDMA();
#endif
#if (HAL_UART_ISR == 1)if (port == HAL_UART_PORT_0)  return HalUARTRxAvailISR();
#endif
#if (HAL_UART_ISR == 2)if (port == HAL_UART_PORT_1)  return HalUARTRxAvailISR();
#endif
#if HAL_UART_USBreturn HalUARTRxAvailUSB();
#elsereturn 0;
#endif
}

Includes

hal_aes.h
#ifndef     HAL_AES_H_
#define     HAL_AES_H_#include "ZComDef.h"#define     STATE_BLENGTH   16      // Number of bytes in State
#define     KEY_BLENGTH     16      // Number of bytes in Key
#define     KEY_EXP_LENGTH  176     // Nb * (Nr+1) * 4#if ((defined SOFTWARE_AES) && (SOFTWARE_AES == TRUE)) && ((defined SW_AES_AND_KEY_EXP) && (SW_AES_AND_KEY_EXP == TRUE))
#error "SOFTWARE_AES and SW_AES_AND_KEY_EXP cannot be both defined."
#endifextern void HalAesInit( void );
extern void AesLoadBlock( uint8 * );
extern void AesStartBlock( uint8 *, uint8 * );
extern void AesStartShortBlock( uint8 *, uint8 * );
extern void AesLoadIV(uint8 *);
extern void AesDmaSetup( uint8 *, uint16, uint8 *, uint16 );
extern void AesLoadKey( uint8 * );extern void (*pSspAesEncrypt)( uint8 *, uint8 * );
extern void ssp_HW_KeyInit (uint8 *);
extern void sspKeyExpansion (uint8 *, uint8 *);extern void sspAesEncryptHW (uint8 *, uint8 *);
extern void sspAesEncryptKeyExp (uint8 *, uint8 *);
extern void sspAesEncryptBasic (uint8 *, uint8 *);
extern void sspAesEncrypt( uint8 *key, uint8 *buf );#define AES_BUSY    0x08
#define ENCRYPT     0x00
#define DECRYPT     0x01// Macro for setting the mode of the AES operation
#define AES_SETMODE(mode) do { ENCCS &= ~0x70; ENCCS |= mode; } while (0)// _mode_ is one of
#define CBC         0x00
#define CFB         0x10
#define OFB         0x20
#define CTR         0x30
#define ECB         0x40
#define CBC_MAC     0x50// Macro for starting or stopping encryption or decryption
#define AES_SET_ENCR_DECR_KEY_IV(mode) \do {                                \ENCCS = (ENCCS & ~0x07) | mode     \} while(0)// Where _mode_ is one of
#define AES_ENCRYPT     0x00;
#define AES_DECRYPT     0x02;
#define AES_LOAD_KEY    0x04;
#define AES_LOAD_IV     0x06;// Macro for starting the AES module for either encryption, decryption,
// key or initialisation vector loading.
#define AES_START()     ENCCS |= 0x01/* Used by DMA macros to shift 1 to create a mask for DMA registers. */
#define  HAL_DMA_AES_IN    1
#define  HAL_DMA_AES_OUT   2/* AES registers */
#define  HAL_AES_IN_ADDR   0x70B1
#define  HAL_AES_OUT_ADDR  0x70B2#if !defined (HAL_AES_DMA) || (HAL_AES_DMA == FALSE)
#define HAL_AES_DELAY()   \do {                    \uint8 delay = 15;  \while(delay--);    \} while(0)
#endif// End of CC2530 hardware AES engine definitions#endif  // HAL_AES_H_
hal_dma.h
#ifndef HAL_DMA_H
#define HAL_DMA_H#ifdef __cplusplus
extern "C"
{
#endif#include "hal_board.h"
#include "hal_types.h"#if ((defined HAL_DMA) && (HAL_DMA == TRUE))#define HAL_DMA_SET_ADDR_DESC0( a ) \st( \DMA0CFGH = (uint8)( (uint16)(a) >> 8 );  \DMA0CFGL = (uint8)( (uint16)(a) & 0xFF );       \)#define HAL_DMA_SET_ADDR_DESC1234( a ) \st( \DMA1CFGH = (uint8)( (uint16)(a) >> 8 );  \DMA1CFGL = (uint8)( (uint16)(a) & 0xFF );       \)#define HAL_DMA_GET_DESC0()           &dmaCh0#define HAL_DMA_GET_DESC1234( a )     (dmaCh1234+((a)-1))#define HAL_DMA_ARM_CH( ch )           DMAARM = (0x01 << (ch))#define HAL_DMA_CH_ARMED( ch )        (DMAARM & (0x01 << (ch)))#define HAL_DMA_ABORT_CH( ch )         DMAARM = (0x80 | (0x01 << (ch)))
#define HAL_DMA_MAN_TRIGGER( ch )      DMAREQ = (0x01 << (ch))
#define HAL_DMA_START_CH( ch )         HAL_DMA_MAN_TRIGGER( (ch) )#define HAL_DMA_CLEAR_IRQ( ch )        DMAIRQ = ((0x01 << (ch)) ^ 0xFF)#define HAL_DMA_CHECK_IRQ( ch )       (DMAIRQ & (0x01 << (ch)))// Macro for quickly setting the source address of a DMA structure.
#define HAL_DMA_SET_SOURCE( pDesc, src ) \st( \pDesc->srcAddrH = (uint8)( (uint16)(src) >> 8 ); \pDesc->srcAddrL = (uint8)( (uint16)(src) & 0xFF ); \)// Macro for quickly setting the destination address of a DMA structure.
#define HAL_DMA_SET_DEST( pDesc, dst ) \st( \pDesc->dstAddrH = (uint8)( (uint16)(dst) >> 8 ); \pDesc->dstAddrL = (uint8)( (uint16)(dst) & 0xFF ); \)// Macro for quickly setting the number of bytes to be transferred by the DMA,
// max length is 0x1FFF.
#define HAL_DMA_SET_LEN( pDesc, len ) \st( \pDesc->xferLenL = (uint8)( (uint16)(len) & 0xFF); \pDesc->xferLenV &= ~HAL_DMA_LEN_H; \pDesc->xferLenV |= (uint8)((uint16)(len) >> 8); \)#define HAL_DMA_GET_LEN( pDesc ) \(((uint16)(pDesc->xferLenV & HAL_DMA_LEN_H) << 8) | pDesc->xferLenL)#define HAL_DMA_SET_VLEN( pDesc, vMode ) \st( \pDesc->xferLenV &= ~HAL_DMA_LEN_V; \pDesc->xferLenV |= (vMode << 5); \)#define HAL_DMA_SET_WORD_SIZE( pDesc, xSz ) \st( \pDesc->ctrlA &= ~HAL_DMA_WORD_SIZE; \pDesc->ctrlA |= (xSz << 7); \)#define HAL_DMA_SET_TRIG_MODE( pDesc, tMode ) \st( \pDesc->ctrlA &= ~HAL_DMA_TRIG_MODE; \pDesc->ctrlA |= (tMode << 5); \)#define HAL_DMA_GET_TRIG_MODE( pDesc ) ((pDesc->ctrlA >> 5) & 0x3)#define HAL_DMA_SET_TRIG_SRC( pDesc, tSrc ) \st( \pDesc->ctrlA &= ~HAL_DMA_TRIG_SRC; \pDesc->ctrlA |= tSrc; \)#define HAL_DMA_SET_SRC_INC( pDesc, srcInc ) \st( \pDesc->ctrlB &= ~HAL_DMA_SRC_INC; \pDesc->ctrlB |= (srcInc << 6); \)#define HAL_DMA_SET_DST_INC( pDesc, dstInc ) \st( \pDesc->ctrlB &= ~HAL_DMA_DST_INC; \pDesc->ctrlB |= (dstInc << 4); \)#define HAL_DMA_SET_IRQ( pDesc, enable ) \st( \pDesc->ctrlB &= ~HAL_DMA_IRQ_MASK; \pDesc->ctrlB |= (enable << 3); \)#define HAL_DMA_SET_M8( pDesc, m8 ) \st( \pDesc->ctrlB &= ~HAL_DMA_M8; \pDesc->ctrlB |= (m8 << 2); \)#define HAL_DMA_SET_PRIORITY( pDesc, pri ) \st( \pDesc->ctrlB &= ~HAL_DMA_PRIORITY; \pDesc->ctrlB |= pri; \)// Use LEN for transfer count
#define HAL_DMA_VLEN_USE_LEN            0x00
// Transfer the first byte + the number of bytes indicated by the first byte
#define HAL_DMA_VLEN_1_P_VALOFFIRST     0x01
// Transfer the number of bytes indicated by the first byte (starting with the first byte)
#define HAL_DMA_VLEN_VALOFFIRST         0x02
// Transfer the first byte + the number of bytes indicated by the first byte + 1 more byte
#define HAL_DMA_VLEN_1_P_VALOFFIRST_P_1 0x03
// Transfer the first byte + the number of bytes indicated by the first byte + 2 more bytes
#define HAL_DMA_VLEN_1_P_VALOFFIRST_P_2 0x04#define HAL_DMA_WORDSIZE_BYTE           0x00 /* Transfer a byte at a time. */
#define HAL_DMA_WORDSIZE_WORD           0x01 /* Transfer a 16-bit word at a time. */#define HAL_DMA_TMODE_SINGLE            0x00 /* Transfer a single byte/word after each DMA trigger. */
#define HAL_DMA_TMODE_BLOCK             0x01 /* Transfer block of data (length len) after each DMA trigger. */
#define HAL_DMA_TMODE_SINGLE_REPEATED   0x02 /* Transfer single byte/word (after len transfers, rearm DMA). */
#define HAL_DMA_TMODE_BLOCK_REPEATED    0x03 /* Transfer block of data (after len transfers, rearm DMA). */#define HAL_DMA_TRIG_NONE           0   /* No trigger, setting DMAREQ.DMAREQx bit starts transfer. */
#define HAL_DMA_TRIG_PREV           1   /* DMA channel is triggered by completion of previous channel. */
#define HAL_DMA_TRIG_T1_CH0         2   /* Timer 1, compare, channel 0. */
#define HAL_DMA_TRIG_T1_CH1         3   /* Timer 1, compare, channel 1. */
#define HAL_DMA_TRIG_T1_CH2         4   /* Timer 1, compare, channel 2. */
#define HAL_DMA_TRIG_T2_COMP        5   /* Timer 2, compare. */
#define HAL_DMA_TRIG_T2_OVFL        6   /* Timer 2, overflow. */
#define HAL_DMA_TRIG_T3_CH0         7   /* Timer 3, compare, channel 0. */
#define HAL_DMA_TRIG_T3_CH1         8   /* Timer 3, compare, channel 1. */
#define HAL_DMA_TRIG_T4_CH0         9   /* Timer 4, compare, channel 0. */
#define HAL_DMA_TRIG_T4_CH1        10   /* Timer 4, compare, channel 1. */
#define HAL_DMA_TRIG_ST            11   /* Sleep Timer compare. */
#define HAL_DMA_TRIG_IOC_0         12   /* Port 0 I/O pin input transition. */
#define HAL_DMA_TRIG_IOC_1         13   /* Port 1 I/O pin input transition. */
#define HAL_DMA_TRIG_URX0          14   /* USART0 RX complete. */
#define HAL_DMA_TRIG_UTX0          15   /* USART0 TX complete. */
#define HAL_DMA_TRIG_URX1          16   /* USART1 RX complete. */
#define HAL_DMA_TRIG_UTX1          17   /* USART1 TX complete. */
#define HAL_DMA_TRIG_FLASH         18   /* Flash data write complete. */
#define HAL_DMA_TRIG_RADIO         19   /* RF packet byte received/transmit. */
#define HAL_DMA_TRIG_ADC_CHALL     20   /* ADC end of a conversion in a sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH0       21   /* ADC end of conversion channel 0 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH1       22   /* ADC end of conversion channel 1 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH2       23   /* ADC end of conversion channel 2 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH3       24   /* ADC end of conversion channel 3 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH4       25   /* ADC end of conversion channel 4 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH5       26   /* ADC end of conversion channel 5 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH6       27   /* ADC end of conversion channel 6 in sequence, sample ready. */
#define HAL_DMA_TRIG_ADC_CH7       28   /* ADC end of conversion channel 7 in sequence, sample ready. */
#define HAL_DMA_TRIG_ENC_DW        29   /* AES encryption processor requests download input data. */
#define HAL_DMA_TRIG_ENC_UP        30   /* AES encryption processor requests upload output data. */#define HAL_DMA_SRCINC_0         0x00 /* Increment source pointer by 0 bytes/words after each transfer. */
#define HAL_DMA_SRCINC_1         0x01 /* Increment source pointer by 1 bytes/words after each transfer. */
#define HAL_DMA_SRCINC_2         0x02 /* Increment source pointer by 2 bytes/words after each transfer. */
#define HAL_DMA_SRCINC_M1        0x03 /* Decrement source pointer by 1 bytes/words after each transfer. */#define HAL_DMA_DSTINC_0         0x00 /* Increment destination pointer by 0 bytes/words after each transfer. */
#define HAL_DMA_DSTINC_1         0x01 /* Increment destination pointer by 1 bytes/words after each transfer. */
#define HAL_DMA_DSTINC_2         0x02 /* Increment destination pointer by 2 bytes/words after each transfer. */
#define HAL_DMA_DSTINC_M1        0x03 /* Decrement destination pointer by 1 bytes/words after each transfer. */#define HAL_DMA_IRQMASK_DISABLE  0x00 /* Disable interrupt generation. */
#define HAL_DMA_IRQMASK_ENABLE   0x01 /* Enable interrupt generation upon DMA channel done. */#define HAL_DMA_M8_USE_8_BITS    0x00 /* Use all 8 bits for transfer count. */
#define HAL_DMA_M8_USE_7_BITS    0x01 /* Use 7 LSB for transfer count. */#define HAL_DMA_PRI_LOW          0x00 /* Low, CPU has priority. */
#define HAL_DMA_PRI_GUARANTEED   0x01 /* Guaranteed, DMA at least every second try. */
#define HAL_DMA_PRI_HIGH         0x02 /* High, DMA has priority. */
#define HAL_DMA_PRI_ABSOLUTE     0x03 /* Highest, DMA has priority. Reserved for DMA port access.. */#define HAL_DMA_MAX_ARM_CLOCKS   45   // Maximum number of clocks required if arming all 5 at once.// Bit fields of the 'lenModeH'
#define HAL_DMA_LEN_V     0xE0
#define HAL_DMA_LEN_H     0x1F// Bit fields of the 'ctrlA'
#define HAL_DMA_WORD_SIZE 0x80
#define HAL_DMA_TRIG_MODE 0x60
#define HAL_DMA_TRIG_SRC  0x1F// Bit fields of the 'ctrlB'
#define HAL_DMA_SRC_INC   0xC0
#define HAL_DMA_DST_INC   0x30
#define HAL_DMA_IRQ_MASK  0x08
#define HAL_DMA_M8        0x04
#define HAL_DMA_PRIORITY  0x03typedef struct {uint8 srcAddrH;uint8 srcAddrL;uint8 dstAddrH;uint8 dstAddrL;uint8 xferLenV;uint8 xferLenL;uint8 ctrlA;uint8 ctrlB;
} halDMADesc_t;extern halDMADesc_t dmaCh0;
extern halDMADesc_t dmaCh1234[4];void HalDmaInit( void );#endif  // #if (defined HAL_DMA) && (HAL_DMA == TRUE)#ifdef __cplusplus
}
#endif#endif  // #ifndef HAL_DMA_H
hal_mcu.h`
#ifndef _HAL_MCU_H
#define _HAL_MCU_H
#include "hal_defs.h"
#include "hal_types.h"
#define HAL_MCU_CC2530
#ifdef __IAR_SYSTEMS_ICC__
#include <ioCC2530.h>
#define HAL_COMPILER_IAR
#define HAL_MCU_LITTLE_ENDIAN()   __LITTLE_ENDIAN__
#define _PRAGMA(x) _Pragma(#x)
#define HAL_ISR_FUNC_DECLARATION(f,v)   _PRAGMA(vector=v) __near_func __interrupt void f(void)
#define HAL_ISR_FUNC_PROTOTYPE(f,v)     _PRAGMA(vector=v) __near_func __interrupt void f(void)
#define HAL_ISR_FUNCTION(f,v)           HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v)#elif defined __KEIL__
#include <CC2530.h>
#define HAL_COMPILER_KEIL
#define HAL_MCU_LITTLE_ENDIAN()   0
#define HAL_ISR_FUNC_DECLARATION(f,v)   void f(void) interrupt v
#define HAL_ISR_FUNC_PROTOTYPE(f,v)     void f(void)
#define HAL_ISR_FUNCTION(f,v)           HAL_ISR_FUNC_PROTOTYPE(f,v); HAL_ISR_FUNC_DECLARATION(f,v)#else
#error "ERROR: Unknown compiler."
#endif#define HAL_ENABLE_INTERRUPTS()         st( EA = 1; )
#define HAL_DISABLE_INTERRUPTS()        st( EA = 0; )
#define HAL_INTERRUPTS_ARE_ENABLED()    (EA)typedef unsigned char halIntState_t;
#define HAL_ENTER_CRITICAL_SECTION(x)   st( x = EA;  HAL_DISABLE_INTERRUPTS(); )
#define HAL_EXIT_CRITICAL_SECTION(x)    st( EA = x; )
#define HAL_CRITICAL_STATEMENT(x)       st( halIntState_t _s; HAL_ENTER_CRITICAL_SECTION(_s); x; HAL_EXIT_CRITICAL_SECTION(_s); )#ifdef __IAR_SYSTEMS_ICC__/* IAR library uses XCH instruction with EA. It may cause the higher priority interrupt to be * locked out, therefore, may increase interrupt latency.  It may also create a lockup condition. * This workaround should only be used with 8051 using IAR compiler. When IAR fixes this by * removing XCH usage in its library, compile the following macros to null to disable them.*/#define HAL_ENTER_ISR()               { halIntState_t _isrIntState = EA; HAL_ENABLE_INTERRUPTS();#define HAL_EXIT_ISR()                  EA = _isrIntState; }
#else#define HAL_ENTER_ISR()     #define HAL_EXIT_ISR()
#endif /* __IAR_SYSTEMS_ICC__ */#define WD_EN               BV(3)
#define WD_MODE             BV(2)
#define WD_INT_1900_USEC    (BV(0) | BV(1))
#define WD_RESET1           (0xA0 | WD_EN | WD_INT_1900_USEC)
#define WD_RESET2           (0x50 | WD_EN | WD_INT_1900_USEC)
#define WD_KICK()           st( WDCTL = (0xA0 | WDCTL & 0x0F); WDCTL = (0x50 | WDCTL & 0x0F); )/* disable interrupts, set watchdog timer, wait for reset */
#define HAL_SYSTEM_RESET()  st( HAL_DISABLE_INTERRUPTS(); WDCTL = WD_RESET1; WDCTL = WD_RESET2; for(;;); )#define REV_A          0x00    /* workaround turned off */
#define REV_B          0x11    /* PG1.1 */
#define REV_C          0x20    /* PG2.0 */
#define REV_D          0x21    /* PG2.1 *//* PCON bit definitions */
#define PCON_IDLE  BV(0)            /* Writing 1 to force CC2530 to enter sleep mode *//* SLEEPCMD bit definitions */
#define OSC_PD     BV(2)            /* Idle Osc: powered down=1 */
#define PMODE     (BV(1) | BV(0))   /* Power mode bits *//* SLEEPSTA bit definitions */
#define XOSC_STB   BV(6)  /* XOSC: powered, stable=1 */
#define HFRC_STB   BV(5)  /* HFRCOSC: powered, stable=1 *//* SLEEPCMD and SLEEPSTA bit definitions */
#define OSC_PD     BV(2)  /* 0: Both oscillators powered up and stable* 1: oscillators not stable *//* CLKCONCMD bit definitions */
#define OSC              BV(6)
#define TICKSPD(x)       (x << 3)
#define CLKSPD(x)        (x << 0)
#define CLKCONCMD_32MHZ  (0)
#define CLKCONCMD_16MHZ  (CLKSPD(1) | TICKSPD(1) | OSC)/* STLOAD */
#define LDRDY            BV(0) /* Load Ready. This bit is 0 while the sleep timer* loads the 24-bit compare value and 1 when the sleep* timer is ready to start loading a newcompare value. */#ifdef POWER_SAVING
extern volatile __data uint8 halSleepPconValue;#define CLEAR_SLEEP_MODE()        st( halSleepPconValue = 0; )
#define ALLOW_SLEEP_MODE()        st( halSleepPconValue = PCON_IDLE; )
#else
#define CLEAR_SLEEP_MODE()
#define ALLOW_SLEEP_MODE()
#endif#endif

hal_types.h

#ifndef _HAL_TYPES_H
#define _HAL_TYPES_H/* Texas Instruments CC2530 */typedef signed   char   int8;
typedef unsigned char   uint8;typedef signed   short  int16;
typedef unsigned short  uint16;typedef signed   long   int32;
typedef unsigned long   uint32;typedef unsigned char   bool;typedef uint8           halDataAlign_t;#ifdef __IAR_SYSTEMS_ICC__
#define  CODE   __code
#define  XDATA  __xdata#elif defined __KEIL__
#define  CODE   code
#define  XDATA  xdata#else
#error "ERROR: Unknown compiler."
#endif#ifndef TRUE
#define TRUE 1
#endif#ifndef FALSE
#define FALSE 0
#endif#ifndef NULL
#define NULL 0
#endif#endif

CC2530,zigbee,协议栈,代码集(应用层,硬件层)相关推荐

  1. CC2530 ZigBee协议栈 学习心得

    最近一直在学习研究cc2530这款单片机,感觉自己的C语言水平还是不够有得提升的空间,但还是有不少收获.    CC2530是一款支持ZigBee无线组网协议的低功耗单片机,cc2530主要的应用场景 ...

  2. CC2530/Zigbee协议栈外部中断配置详解

    CC2530/Zigbee协议栈配置外部中断 一.I/O端口 二.配置寄存器 三.中断处理函数 一.I/O端口 1.在CC2530芯片中,其I/O口用作通用 I/O 时,引脚可以组成 3 个 8 位端 ...

  3. zigbee协议栈之中终端绑定

    1193628586 个人中心 好友 消息 退出 聪明女人必撒的九个谎 发博文 博文 痴白孺子牛 http://blog.sina.com.cn/u/1326039221 [订阅] [手机订阅] 首页 ...

  4. 【无线通信篇 | Zstack协议栈】CC2530 Zigbee Zstack协议栈组网项目及详细讲解篇

    演示视频:https://www.bilibili.com/video/BV1Ew411o7Fp 物联网无线通信技术,ZigBee无线传感网络 CC2530最大的特点就是一个拥有无线收发器(RF)的单 ...

  5. CC2530下ZigBee协议栈中添加BH1750数字光照传感器

    项目里需要获得光照强度,之前用的是光敏电阻,但是光敏电阻实在不精确,换用BH1750数字光照传感器.网上购买后,送的资料都是单片机的,移植到ZigBee协议栈里面,时序有些问题,在网上找的一些资料,很 ...

  6. ZigBee协议栈浅显解读(一)

    目前ZigBee开发主要用的是TI的CC2530.CC2530内部集成看增强型的51所以对于只学过51的小白入门不成问题. 本篇文章我只会介绍ZigBee的协议栈. 我们打开ZigBee协议栈后可以加 ...

  7. Zigbee协议栈ZStack构架

     协议栈版本信息: ZigBee2006\ZStack-1.4.3-1.2.1 1.ZStack协议栈构架 Zigbee协议栈就是将各个层定义的协议都集合在一起,以函数的形式实现,并给用户提供一些 ...

  8. ZigBee协议栈Zstack介绍

    文中所讲述的协议栈是基于ZigBee2006\Zstack-1.4.3-1.2.1 1.ZSTACK协议栈的架构 ZigBee协议栈就是将各个层定义的协议都集合在一起,以函数的形式实现,并给用户提供一 ...

  9. ZIGBEE通讯-7.ZigBee协议栈简介

    想要学习协议栈,必须先知道协议是什么.协议定义的是一系列的通信标准,通信双方需要共同按照这一标准进行正常的数据收发,而协议栈是协议的具体实现形式,通俗的理解为用代码实现的函数库,以便于开发人员调用. ...

最新文章

  1. SIEM部署的几条最佳实践
  2. 如何在同一台电脑开启多个tomcat服务器
  3. 去重查询_《前端算法系列》数组去重
  4. 14条建议 使你的IT职业生涯更上一层楼
  5. Python的一些小技巧小知识
  6. Git在版本2.13中继续改进了安全性和UI
  7. Android内存泄漏分析及调试
  8. app传输数据到php,安卓app客户端和使用php的服务器端数据交互
  9. 求浮点数的幂的精确值
  10. 蚂蚁区块链在司法存证领域的探索与实践
  11. tab weui 页面切换_weui tabbar 到底怎么用啊?tab页面为什么不能切换?
  12. ssm项目_什么是SSM,以及javaweb项目包的含义(三层架构)
  13. css设置背景虚化,vue移动端登录页
  14. bin code led_LED混Bin生控制文件.pdf
  15. LaTex 表格的单元格划横线,分成两部分
  16. 精益技术简历之道——改善技术简历的47条原则
  17. 双系统下Ubuntu完整卸载
  18. 如何用手机来拍证件照
  19. 电力爱陆通公专一体模块,国网加密模块,国网硬件加密模块的工作原理
  20. 0day攻击防护措施有哪些?

热门文章

  1. 动端开发者福利-免费收费api收藏
  2. HTTP状态码一共分5种类型
  3. 神经网络从被人忽悠到忽悠人(二)
  4. 回复删除的文件---RecoverMyFiles
  5. 营销活动的8大工具,各大平台推荐
  6. 鼓励孩子最常用的101句赞赏语
  7. 酒厂酒业IP网络广播系统建设方案-基于局域网的新一代交互智慧酒厂酒业IP广播设计指南
  8. 借助 Windows 事件转发进行入侵检测
  9. Oracle PrimaveraP6(Professional)单机版的安装
  10. 梯度下降和随机梯度下降的区别