原创 【原创】ZigBee学习之26——HalDriverInit()

2010-2-11 10:22 2491 3 3 分类: MCU/ 嵌入式

  // 初始化硬件驱动,这个估计跟硬件更加紧密了,只要板子不同就要做相应的修改<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />


  HalDriverInit();


//hal_drivers.c


void HalDriverInit (void)


{


  HalTimerInit();//定时器初始化


//hal_timer.c


//一些宏定义


#define HAL_TIMER1_16_PRESCALE      HAL_TIMER1_16_TC_DIV128


#define HAL_TIMER1_16_PRESCALE_VAL  128


#define HAL_TIMER3_8_PRESCALE       HAL_TIMER34_8_TC_DIV128


#define HAL_TIMER3_8_PRESCALE_VAL   128


#define HAL_TIMER4_8_PRESCALE       HAL_TIMER34_8_TC_DIV128


#define HAL_TIMER4_8_PRESCALE_VAL   128


 


/* Clock settings */


#define HAL_TIMER_16MHZ           16


#define HAL_TIMER_32MHZ           32


#define HAL_TIMER1_16_TC_DIV1     0x00  /* No clock pre-scaling */


#define HAL_TIMER1_16_TC_DIV8     0x04  /* Clock pre-scaled by 8 */


#define HAL_TIMER1_16_TC_DIV32    0x08  /* Clock pre-scaled by 32 */


#define HAL_TIMER1_16_TC_DIV128   0x<?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />0c  /* Clock pre-scaled by 128 */


#define HAL_TIMER1_16_TC_BITS     0x0c  /* Bits 3:2 */


//定时器的初始化很简单就是设置了各个寄存器的初始值,然后屏蔽了中断,就不多说了


void HalTimerInit (void)


{


  T1CCTL0 = 0;    /* Make sure interrupts are disabled */


  T1CCTL1 = 0;    /* Make sure interrupts are disabled */


  T1CCTL2 = 0;    /* Make sure interrupts are disabled */


  T3CCTL0 = 0;    /* Make sure interrupts are disabled */


  T3CCTL1 = 0;    /* Make sure interrupts are disabled */


  T4CCTL0 = 0;    /* Make sure interrupts are disabled */


  T4CCTL1 = 0;    /* Make sure interrupts are disabled */


//禁止定时器的中断


  /* Setup prescale & clock for timer0 */


  halTimerRecord[HW_TIMER_1].prescale    = HAL_TIMER1_16_PRESCALE;


  halTimerRecord[HW_TIMER_1].clock       = HAL_TIMER_32MHZ;


  halTimerRecord[HW_TIMER_1].prescaleVal = HAL_TIMER1_16_PRESCALE_VAL;


 


  /* Setup prescale & clock for timer2 */


  halTimerRecord[HW_TIMER_3].prescale    = HAL_TIMER3_8_PRESCALE;


  halTimerRecord[HW_TIMER_3].clock       = HAL_TIMER_32MHZ;


  halTimerRecord[HW_TIMER_3].prescaleVal = HAL_TIMER3_8_PRESCALE_VAL;


 


  /* Setup prescale & clock for timer3 */


  halTimerRecord[HW_TIMER_4].prescale    = HAL_TIMER4_8_PRESCALE;


  halTimerRecord[HW_TIMER_4].clock       = HAL_TIMER_32MHZ;


  halTimerRecord[HW_TIMER_4].prescaleVal = HAL_TIMER4_8_PRESCALE_VAL;


 


//设置定时器134的通道数据结构,其实就是在数据结构中设置了定时器相关寄存器的地址。默认所有的定时器使用通道0


  /* Setup Timer1 Channel structure */


  halTimerChannel[HW_TIMER_1].TxCCTL =  TCHN_T1CCTL;


// #define TCHN_T1CCTL   &(X_T1CCTL0)


  halTimerChannel[HW_TIMER_1].TxCCL =   TCHN_T1CCL;


  halTimerChannel[HW_TIMER_1].TxCCH =   TCHN_T1CCH;


  halTimerChannel[HW_TIMER_1].TxOVF =   TCNH_T1OVF;


  halTimerChannel[HW_TIMER_1].ovfbit =  TCHN_T1OVFBIT;


  halTimerChannel[HW_TIMER_1].intbit =  TCHN_T1INTBIT;


 


  /* Setup Timer3 Channel structure */


  halTimerChannel[HW_TIMER_3].TxCCTL =  TCHN_T3CCTL;


  halTimerChannel[HW_TIMER_3].TxCCL =   TCHN_T3CCL;


  halTimerChannel[HW_TIMER_3].TxCCH =   TCHN_T3CCH;


  halTimerChannel[HW_TIMER_3].TxOVF =   TCNH_T3OVF;


  halTimerChannel[HW_TIMER_3].ovfbit =  TCHN_T3OVFBIT;


  halTimerChannel[HW_TIMER_3].intbit =  TCHN_T3INTBIT;


 


  /* Setup Timer4 Channel structure */


  halTimerChannel[HW_TIMER_4].TxCCTL =  TCHN_T4CCTL;


  halTimerChannel[HW_TIMER_4].TxCCL =   TCHN_T4CCL;


  halTimerChannel[HW_TIMER_4].TxCCH =   TCHN_T4CCH;


  halTimerChannel[HW_TIMER_4].TxOVF =   TCNH_T4OVF;


  halTimerChannel[HW_TIMER_4].ovfbit =  TCHN_T4OVFBIT;


  halTimerChannel[HW_TIMER_4].intbit =  TCHN_T4INTBIT;


}


#if (defined HAL_ADC) && (HAL_ADC == TRUE)


//如果开启了硬件ADC服务则进行ADC的初始化


  HalAdcInit();


//hal_adc.c


void HalAdcInit (void)


{


#if (HAL_ADC == TRUE)


  volatile uint8  tmp;


 


  ADCCON1 = HAL_ADC_STSEL | HAL_ADC_RAND_GEN | 0x03;


// #define HAL_ADC_STSEL       HAL_ADC_STSEL_ST


// #define HAL_ADC_STSEL_ST    0x30


//设置ADCCON1.ST =1为启动前提条件


// #define HAL_ADC_RAND_GEN    HAL_ADC_RAND_STOP


// #define HAL_ADC_RAND_STOP   0x0c


//停止随机数发生器


//这些配置都可以在【hal_adc.c】中的常数定义部分找到


  ADCCON2 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_SCHN;


//AVDD_SOC为参考电压,8位分辨率,AVDD/3为输入


//以上只是ADC的初始状态,使用的时候需要根据情况进行更改


  tmp = ADCL;     /* 通过读取转换值来清除EOC */


  tmp = ADCH;


//因为每次重启后读取的ADC对外部的转换都是地电平,所以这里启动两次对地的ADC转换来消除这个bug


  ADCCON3 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_ECHN;


  while ((ADCCON1 & HAL_ADC_EOC) != HAL_ADC_EOC);   /* Wait for conversion */


  tmp = ADCL;     /* read ADCL,ADCH to clear EOC */


  tmp = ADCH;


  ADCCON3 = HAL_ADC_REF_VOLT | HAL_ADC_DEC_RATE | HAL_ADC_ECHN;


  while ((ADCCON1 & HAL_ADC_EOC) != HAL_ADC_EOC);   /* Wait for conversion */


  tmp = ADCL;     /* read ADCL,ADCH to clear EOC */


  tmp = ADCH;


#endif


}


#endif


 


#if (defined HAL_DMA) && (HAL_DMA == TRUE)


  HalDmaInit();


//DMA初始化【hal_dma.c


void HalDmaInit( void )


{


  HAL_DMA_SET_ADDR_DESC0( &dmaCh0 );


//hal_dma.h


//DMA配置数据结构


typedef struct {


  uint8 srcAddrH;


  uint8 srcAddrL;


  uint8 dstAddrH;


  uint8 dstAddrL;


  uint8 xferLenV;


  uint8 xferLenL;


  uint8 ctrlA;


  uint8 ctrlB;


} halDMADesc_t;


//设置DMA通道0的配置地址


#define HAL_DMA_SET_ADDR_DESC0( a ) \


  st( \


    DMA0CFGH = (uint8)( (uint16)(a) >> 8 );  \


    DMA0CFGL = (uint8)( (uint16)(a) );       \


  )


  HAL_DMA_SET_ADDR_DESC1234( dmaCh1234 );


}


#endif


 


  /* AES高级加密单元,暂时不说吧 */


#if (defined HAL_AES) && (HAL_AES == TRUE)


  HalAesInit();


#endif


 


  /* LCD初始化,这个要大改了,相信每个人的板子上面的LCD设计都有可能不相同的吧 */


//使用LCD的前提是要定义了LCD_HW


#if (defined HAL_LCD) && (HAL_LCD == TRUE)


  HalLcdInit();


#endif


 


  /* LED初始化,也跟硬件关系很大 */


#if (defined HAL_LED) && (HAL_LED == TRUE)


  HalLedInit();


//hal_led.c


void HalLedInit (void)


{


#if (HAL_LED == TRUE)


  /* 关闭所有LED */


  HalLedSet (HAL_LED_ALL, HAL_LED_MODE_OFF);


  /* 这个sleepActive 不晓得是用作什么用的,后面应该会提到,初始化为FALSE */


  HalLedStatusControl.sleepActive = FALSE;


#endif /* HAL_LED */


}


#endif


 


  /* UART初始化,这里只是配置了UART的端口以及UART-DMA模式的配置,并没有配置UART数据传输格式 */


#if (defined HAL_UART) && (HAL_UART == TRUE)


  HalUARTInit();


//hal_uart.c


void HalUARTInit( void )


{


#if HAL_UART_DMA


  halDMADesc_t *ch;


//如果允许UARTDMA则定义一个DMA配置数据结构变量


#endif


 


  // 初始设置USART0优先级高于USART1


  P2DIR &= ~P2DIR_PRIPO;


// #define P2DIR_PRIPO               0xC0


  P2DIR |= HAL_UART_PRIPO;


//#if HAL_UART_0_ENABLE


//  #define HAL_UART_PRIPO          0x00


//#else


//  #define HAL_UART_PRIPO          0x40


//#endif


//如果定义HAL_UART_0_ENABLEUART0有最高优先级,否则UART1高优先级。在我的板子上是使用UART0所以要定义HAL_UART_0_ENABLE了,呵呵


#if HAL_UART_0_ENABLE


//设置UART0 I/OP0.哈哈刚好符合我板子的情况,太妙了


//#define HAL_UART_0_PERCFG_BIT     0x01


  PERCFG &= ~HAL_UART_0_PERCFG_BIT;


 


  /* 配置P0上的Tx and Rx */


// #define HAL_UART_0_P0_RX_TX       0x0c


  P0SEL |= HAL_UART_0_P0_RX_TX;


 


  /* 保证ADC不用这几个引脚作为输入,其实重启后的状态就是不用的 */


  ADCCFG &= ~HAL_UART_0_P0_RX_TX;


 


  /*UART Mode */


  U0CSR = CSR_MODE;


 


  /* 清空缓存 */


  U0UCR = UCR_FLUSH;


#endif


//对于UART1的配置如出一辙,只是换了一下管脚而已,把UART配置到P1


#if HAL_UART_1_ENABLE


  // Set UART1 I/O location to P1.


  PERCFG |= HAL_UART_1_PERCFG_BIT;


 


  /* Enable Tx and Rx on P1 */


  P1SEL  |= HAL_UART_1_P1_RX_TX;


 


  /* Make sure ADC doesnt use this */


  ADCCFG &= ~HAL_UART_1_P1_RX_TX;


 


  /* Mode is UART Mode */


  U1CSR = CSR_MODE;


 


  /* Flush it */


  U1UCR = UCR_FLUSH;


#endif


//下面是UARTDMA配置,需要先定义HAL_UART_DMATRUE


#if HAL_UART_DMA


  ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_TX );


// #define HAL_DMA_GET_DESC1234( a )     (dmaCh1234+((a)-1))


// #define HAL_DMA_CH_TX    3


//以上是配置TXDMA通道2


    HAL_DMA_SET_DEST( ch, DMA_UDBUF );


//通道的目的地址设为UxDBUFhal_uart.c


//#define DMA_UDBUF   HAL_DMA_U0DBUF


// #define HAL_DMA_U0DBUF  0xDFC1


//#define HAL_DMA_U1DBUF  0xDFF9


//TI的这个驱动中只支持两个USART中的一个使用DMA,不支持同时使用


 


  //配置VLEN域使用LEN字段


  HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN );


 


  //初始化为一次传输一字节


  HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_BYTE );


 


  //一次触发只传输一个字节,触发源为UART0 TX完成


  HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE );


  HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_TX );


                     


//源地址递增,目的地址不递增


  HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_1 );


  HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_0 );


 


  //传输完成不发送IRQ请求


  HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_DISABLE );


 


//8位一字节,高优先级模式


  HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS );


  HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH );


 


  //一下是配置RXDMA的源,接收使用DMA模式,大同小异就不多说了.


  ch = HAL_DMA_GET_DESC1234( HAL_DMA_CH_RX );


 


  // The start address of the source.


  HAL_DMA_SET_SOURCE( ch, DMA_UDBUF );


 


  // Using the length field to determine how many bytes to transfer.


  HAL_DMA_SET_VLEN( ch, HAL_DMA_VLEN_USE_LEN );


 


  HAL_DMA_SET_WORD_SIZE( ch, HAL_DMA_WORDSIZE_WORD );


 


  // The bytes are transferred 1-by-1 on Rx Complete trigger.


  HAL_DMA_SET_TRIG_MODE( ch, HAL_DMA_TMODE_SINGLE );


  HAL_DMA_SET_TRIG_SRC( ch, DMATRIG_RX );


 


  // The source address is constant - the Rx Data Buffer.


  HAL_DMA_SET_SRC_INC( ch, HAL_DMA_SRCINC_0 );


 


  // The destination address is incremented by 1 word after each transfer.


  HAL_DMA_SET_DST_INC( ch, HAL_DMA_DSTINC_1 );


 


  // The DMA is to be polled and shall not issue an IRQ upon completion.


  HAL_DMA_SET_IRQ( ch, HAL_DMA_IRQMASK_DISABLE );


 


  // Xfer all 8 bits of a byte xfer.


  HAL_DMA_SET_M8( ch, HAL_DMA_M8_USE_8_BITS );


 


  // DMA has highest priority for memory access.


  HAL_DMA_SET_PRIORITY( ch, HAL_DMA_PRI_HIGH );


#endif


}


#endif


 


  /* KEY初始化 */


#if (defined HAL_KEY) && (HAL_KEY == TRUE)


  HalKeyInit();


//hal_key.c】在这个文件中定义了SW6,SW5的端口,看来要大改了,我只有3个独立按键,而且用的是P1


void HalKeyInit( void )


{


#if (HAL_KEY == TRUE)


  halKeySavedKeys = 0;


 


#if defined (HAL_KEY_SW_6_ENABLE)


// #define HAL_KEY_SW_6_BIT      HAL_KEY_BIT1


//#define HAL_KEY_BIT1   0x02


// #define HAL_KEY_SW_5_BIT      HAL_KEY_BIT0


//对于CC2430DBSW6用的是P0_1,SW5用的是P2_0;对于CC2430EB,没有SW6SW5用的是P0_5


  HAL_KEY_SW_6_SEL &= ~(HAL_KEY_SW_6_BIT);


  HAL_KEY_SW_6_DIR &= ~(HAL_KEY_SW_6_BIT);


#endif


 


#if defined (HAL_KEY_SW_5_ENABLE)


  HAL_KEY_SW_5_SEL &= ~(HAL_KEY_SW_5_BIT);  


  HAL_KEY_SW_5_DIR &= ~(HAL_KEY_SW_5_BIT);   


  HAL_KEY_SW_5_INP |= HAL_KEY_SW_5_BIT;     


#endif


 


  /* 初始化回调函数 */


  pHalKeyProcessFunction  = NULL;


 


  /* Start with key is not configured */


  HalKeyConfigured = FALSE;


#endif /* HAL_KEY */


}


#endif


}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
3
关闭 站长推荐上一条 /3 下一条