Chinaunix首页 | 论坛 | 博客
  • 博客访问: 9465049
  • 博文数量: 1750
  • 博客积分: 12961
  • 博客等级: 上将
  • 技术积分: 20091
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-09 11:25
个人简介

偷得浮生半桶水(半日闲), 好记性不如抄下来(烂笔头). 信息爆炸的时代, 学习是一项持续的工作.

文章分类

全部博文(1750)

文章存档

2024年(26)

2023年(26)

2022年(112)

2021年(217)

2020年(157)

2019年(192)

2018年(81)

2017年(78)

2016年(70)

2015年(52)

2014年(40)

2013年(51)

2012年(85)

2011年(45)

2010年(231)

2009年(287)

分类: 其他平台

2015-06-25 19:05:21


点击(此处)折叠或打开

  1. #include "gconf.h"

  2. #include "peripheral.h"

  3. #include "IrDA.h"



  4. //############## recv ####################################################

  5. static uint16_t ir_data[512];

  6. static uint16_t oft;



  7. static const char *IrDARecv_thread_name = "IrDARecv_thread";

  8. static os_thread_t IrDARecv_thread;

  9. static os_thread_stack_define(IrDARecv_thread_stack, 1024);





  10. //???????è???í???é.

  11. static int IrDA_RECV_PushBtn_Gpio;

  12. static int IrDA_RECV_LED_Gpio, IrDA_MODE_LED_Gpio;

  13. static volatile bool bInIrDARecvMode = false;



  14. static void IrDARecv_task_exec(os_thread_arg_t arg)

  15. {    

  16.     volatile uint8_t curr, lasted;

  17.         

  18.     bInIrDARecvMode= true;

  19.     led_on(IrDA_RECV_LED_Gpio);

  20.     led_on(IrDA_MODE_LED_Gpio);

  21.         

  22.     lasted = GPIO_ReadPinLevel(CONFIG_IrDA_RECV);

  23.     while ((curr = GPIO_ReadPinLevel(CONFIG_IrDA_RECV)) == lasted)

  24.     {

  25.     //    nd_delay_ms(1);

  26.         os_thread_relinquish();

  27.     }

  28.     

  29.     nd_reset_stopwatch();    

  30.     lasted = curr;

  31.     oft= 0;

  32.     led_off(IrDA_MODE_LED_Gpio);

  33.     

  34.     while (1) {

  35.         curr = GPIO_ReadPinLevel(CONFIG_IrDA_RECV);        

  36.         if (curr != lasted) {

  37.             ir_data[oft] =     nd_get_stopwatch();

  38.             nd_reset_stopwatch();

  39.             lasted = curr;

  40.             oft++;

  41.         }

  42.         else {

  43.             if (nd_get_stopwatch() > 10000) {

  44.                 if (oft > 0) {

  45.                     break;

  46.                 }

  47.             }

  48.             os_thread_relinquish();

  49.         }

  50.     }

  51.     nd_close_stopwatch();

  52.     if (oft > 0) {

  53.         dump_data_u16("ir_data",ir_data,oft);

  54.     }

  55.                         

  56.     led_off(IrDA_RECV_LED_Gpio);

  57.     bInIrDARecvMode= false;



  58.     os_thread_self_complete(NULL);

  59. }





  60. static void IrDARecvMode_callback(void)

  61. {

  62.     if (bInIrDARecvMode) return;

  63.     

  64.     PMU_PowerOnVDDIO(PMU_VDDIO_D0);

  65.     GPIO_PinMuxFun(CONFIG_IrDA_RECV,GPIO9_GPIO9);

  66.     GPIO_SetPinDir(CONFIG_IrDA_RECV,GPIO_INPUT);    



  67.     //when uart is OK, then prepare zigbee TASK and queue TASK.

  68.     int ret = os_thread_create(&IrDARecv_thread,

  69.         IrDARecv_thread_name, (void *)IrDARecv_task_exec, 0,

  70.         &IrDARecv_thread_stack, OS_PRIO_1);


  71.     if (ret)
  72.     {
  73.         dbg("Failed to create uart_monitor_thread");
  74.         //appln_critical_error_handler(NULL);

  75.     }

  76. }



  77. void init_IrDA_env(void)

  78. {

  79.     // push_button sw1 to indicate start SIR_RECV mode, and LED_2 for RUNNING

  80.     IrDA_RECV_PushBtn_Gpio = board_button_1();

  81.     IrDA_RECV_LED_Gpio = board_led_1();

  82.     IrDA_MODE_LED_Gpio = board_led_2();

  83.     gpio_drv_set_cb(NULL, IrDA_RECV_PushBtn_Gpio, GPIO_INT_FALLING_EDGE, IrDARecvMode_callback);    

  84.     return;

  85. }



  86. void final_IrDA_env(void)

  87. {

  88.     gpio_drv_set_cb(NULL, IrDA_RECV_PushBtn_Gpio, GPIO_INT_FALLING_EDGE, NULL);

  89.     return;

  90. }

  91. //######################### SIR send API ##############################################

  92. static void IrDA_load(void)

  93. {

  94.     int div, ton, toff, period;



  95.     /* Set GPT3 at ~ 32.768KHz */

  96.     PMU_PowerOnVDDIO(PMU_VDDIO_D2);

  97.     GPIO_PinMuxFun(CONFIG_IrDA_PWM, GPIO63_GPT3_CH2);    

  98.     GPIO_PinMuxFun(CONFIG_IrDA_DATA,GPIO64_GPIO64);

  99.     GPIO_SetPinDir(CONFIG_IrDA_DATA,GPIO_OUTPUT);

  100.     

  101.     CLK_GPTInternalClkSrc(CLK_GPT_ID_3, CLK_MAINXTAL); //32MHz. so cycle time is 1000000us/32M = 0.03125 us



  102.     div = 8;

  103.     period = (32000/div)/38; //cycles -- (1000000ns/38KHz) / (1000000/(32MHz/div))

  104.     ton = period / 3;

  105.     toff = period - ton;



  106.     //dbg("pwm div=%d period=%d ton=%d toff=%d", div, period, ton, toff);

  107.     

  108.     CLK_ModuleClkDivider(CLK_GPT3, div);

  109.     gpt_drv_pwm(g_rt_context.gpt3_dev, GPT_CH_2, ton, toff);

  110.     

  111. }



  112. static void IrDA_unload(void)

  113. {

  114.     GPIO_PinMuxFun(CONFIG_IrDA_PWM, GPIO63_GPIO63);    

  115.     GPIO_SetPinDir(CONFIG_IrDA_DATA,GPIO_INPUT);

  116.     //PMU_PowerOffVDDIO(PMU_VDDIO_D2);

  117. }



  118. #if 0

  119. static inline void IrDA_Level_Change(bool high_level, uint16_t timeout)

  120. {

  121.     if (high_level) {

  122.         GPIO_WritePinOutput(CONFIG_IrDA_DATA, GPIO_IO_LOW);

  123.     }

  124.     else {

  125.         GPIO_WritePinOutput(CONFIG_IrDA_DATA, GPIO_IO_HIGH);

  126.     }

  127.     nd_delay_us(timeout);



  128. }



  129. static void IrDA_Send_Buffer(uint16_t *buf, uint16_t length)

  130. {

  131.     uint16_t idx;

  132.     bool high_level = true;

  133.     gpt_drv_start(g_rt_context.gpt3_dev);

  134.     

  135.     for (idx=0; idx<length; idx++) {

  136.         IrDA_Level_Change(high_level, buf[idx]);

  137.         high_level = !high_level;

  138.     }

  139.     

  140.     gpt_drv_stop(g_rt_context.gpt3_dev);

  141.     GPIO_WritePinOutput(CONFIG_IrDA_DATA, GPIO_IO_LOW);

  142. }



  143. #else

  144. #define Ir_int_time     10

  145. uint16_t ir_idx = 0, ir_length=0;

  146. uint16_t *ir_buf, ir_count;

  147. bool high_level = true;



  148. static inline void IrDA_Gptimer_cb(void);



  149. static void IrDA_Level_Change(bool high_level, uint16_t timeout)

  150. {

  151.     if (high_level) {

  152.         GPIO_WritePinOutput(CONFIG_IrDA_DATA, GPIO_IO_LOW);

  153.     }

  154.     else {

  155.         GPIO_WritePinOutput(CONFIG_IrDA_DATA, GPIO_IO_HIGH);

  156.     }

  157. }



  158. static void IrDA_Gptimer_cb(void)

  159. {

  160.     ir_count += Ir_int_time * 10;                 

  161.     if (ir_buf[ir_idx] <= ir_count) {     

  162.         ir_count = 0;                            

  163.         ir_idx++;                                

  164.         high_level= !high_level;            

  165.         

  166.         IrDA_Level_Change(high_level, ir_buf[ir_idx]);

  167.         if (ir_idx >= ir_length) {

  168.             gpt_drv_setcb(g_rt_context.gpt3_dev, NULL);

  169.             gpt_drv_stop(g_rt_context.gpt3_dev);

  170.             GPIO_WritePinOutput(CONFIG_IrDA_DATA, GPIO_IO_LOW);

  171.         }

  172.     }

  173. }



  174. static void IrDA_Send_Buffer(uint16_t *buf, uint16_t length)

  175. {

  176.     ir_length = length;

  177.     ir_idx = 0;

  178.     high_level = true;

  179.     ir_buf= buf;

  180.     ir_count=0;



  181.     dbg("IrDA_Send_Buffer...");

  182.     gpt_drv_set(g_rt_context.gpt3_dev, Ir_int_time);

  183.     gpt_drv_setcb(g_rt_context.gpt3_dev, IrDA_Gptimer_cb);



  184.     IrDA_Level_Change(high_level, ir_buf[ir_idx]);

  185.     gpt_drv_start(g_rt_context.gpt3_dev);



  186.     while (ir_idx < ir_length) {

  187.         //os_thread_relinquish();

  188.         os_thread_sleep(os_msec_to_ticks(100));

  189.     }

  190. }



  191. #endif





  192. #ifdef _TEST_CODE_INC_



  193. void test_IrDA_func(void)

  194. {

  195.     //vTaskPrioritySet(NULL, OS_PRIO_0);



  196.     uint16_t gree_shake_data[] = {

  197.         1310, 386,1256, 436, 410,1280,1306, 388,1258, 434, 412,1278, 406,1286, 462,1230,

  198.          462,1230, 462,1232,1258, 436, 410,8046,1260, 436,1256, 436, 410,1280,1258, 436,

  199.         1256, 436, 410,1280, 412,1280, 460,1232, 462,1230, 460,1232,1306, 388, 410,8048,

  200.         1306, 388,1258, 434, 410,1280,1302, 392,1256, 436, 410,1280, 462,1230, 412,1280,

  201.          458,1236, 456,1234,1304, 390, 412,8046,1260, 434,1258, 436, 410,1278,1304, 392,

  202.         1256, 436, 410,1278, 462,1230, 462,1230, 464,1228, 462,1232,1258, 436, 410,8046,

  203.         1310, 386,1256, 436, 410,1280,1258, 436,1256, 436, 410,1280, 412,1280, 412,1280,

  204.          460,1232, 412,1280,1258, 436, 410,8048,1310, 386,1256, 436, 410,1280,1258, 436,

  205.         1256, 436, 410,1280, 460,1230, 414,1280, 458,1234, 412,1280,1308, 386, 410

  206.     };



  207.     dbg("Enter IrDA test...");

  208.     IrDA_load();

  209.     IrDA_Send_Buffer(gree_shake_data, ARRAYSIZE(gree_shake_data));

  210.     IrDA_unload();

  211.     dbg("Exit IrDA test...");



  212.     //vTaskPrioritySet(NULL, OS_PRIO_3);

  213. }



  214. #endif

点击(此处)折叠或打开

  1. #ifndef _PERIPERAL_IrDA_INC_

  2. #define _PERIPERAL_IrDA_INC_





  3. #define CONFIG_IrDA_PWM         GPIO_63        //GPT3 channel 2

  4. #define CONFIG_IrDA_DATA            GPIO_64     //GPT3 channel 3



  5. #define CONFIG_IrDA_RECV            GPIO_9        //?ì???????·



  6. void init_IrDA_env(void);

  7. void final_IrDA_env(void);



  8. #ifdef _TEST_CODE_INC_

  9. void test_IrDA_func(void);

  10. #endif





  11. #endif //_PERIPERAL_IrDA_INC_


阅读(3108) | 评论(0) | 转发(0) |
0

上一篇:433 发送 + marvell

下一篇:精彩论述

给主人留下些什么吧!~~