Skip to content

Commit ec35fc6

Browse files
ameba ir
1 parent 4baa87d commit ec35fc6

File tree

11 files changed

+147
-92
lines changed

11 files changed

+147
-92
lines changed

platforms/RTL8720D/OpenBeken.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CSRC += hal/realtek/hal_wifi_realtek.c
1717
OBK_SRCS =
1818
include $(EFDIR)/../platforms/obk_main.mk
1919
CSRC += $(OBKM_SRC)
20-
#CPPSRC += $(OBKM_SRC_CXX)
20+
CPPSRC += $(OBKM_SRC_CXX)
2121
MODULE_IFLAGS += $(OBK_CFLAGS)
2222

2323
CSRC += libraries/easyflash/ports/ef_port.c

platforms/RTL8720E/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ target_sources(
5757
${OBK_SRCS}hal/realtek/rtl8721da/hal_uart_rtl8721da.c
5858

5959
${OBKM_SRC}
60-
#${OBKM_SRC_CXX}
60+
${OBKM_SRC_CXX}
6161
${BERRY_SRC_C}
6262
../../libraries/mqtt_patched.c
6363
)

platforms/RTL8721DA/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ target_sources(
5757
${OBK_SRCS}hal/realtek/rtl8721da/hal_uart_rtl8721da.c
5858

5959
${OBKM_SRC}
60-
#${OBKM_SRC_CXX}
60+
${OBKM_SRC_CXX}
6161
${BERRY_SRC_C}
6262
../../libraries/mqtt_patched.c
6363
)

platforms/RTL87X0C/OpenBeken.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ SRC_C += $(OBK_DIR)/src/hal/realtek/hal_wifi_realtek.c
1919
OBK_SRCS = $(OBK_DIR)/src/
2020
include $(OBK_DIR)/platforms/obk_main.mk
2121
SRC_C += $(OBKM_SRC)
22-
#SRC_CPP += $(OBKM_SRC_CXX)
22+
SRC_CPP += $(OBKM_SRC_CXX)
2323
CFLAGS += $(OBK_CFLAGS)
2424
CPPFLAGS += $(INCLUDES) -fpermissive
2525

src/driver/drv_ir_new.cpp

Lines changed: 97 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,40 @@ extern "C" {
88
// these cause error: conflicting declaration of 'int bk_wlan_mcu_suppress_and_sleep(unsigned int)' with 'C' linkage
99
#include "../new_common.h"
1010

11-
#include "include.h"
12-
#include "arm_arch.h"
1311
#include "../new_pins.h"
1412
#include "../new_cfg.h"
1513
#include "../logging/logging.h"
1614
#include "../obk_config.h"
1715
#include "../cmnds/cmd_public.h"
16+
#include "../hal/hal_pins.h"
17+
#include "../hal/hal_generic.h"
18+
19+
#if PLATFORM_BEKEN
20+
#include "include.h"
21+
#include "arm_arch.h"
1822
#include "bk_timer_pub.h"
1923
#include "drv_model_pub.h"
20-
21-
// why can;t I call this?
22-
#include "../mqtt/new_mqtt.h"
23-
2424
#include <gpio_pub.h>
2525
//#include "pwm.h"
2626
#include "pwm_pub.h"
27-
2827
#include "../../beken378/func/include/net_param_pub.h"
2928
#include "../../beken378/func/user_driver/BkDriverPwm.h"
3029
#include "../../beken378/func/user_driver/BkDriverI2c.h"
3130
#include "../../beken378/driver/i2c/i2c1.h"
3231
#include "../../beken378/driver/gpio/gpio.h"
32+
#elif PLATFORM_REALTEK
33+
#define MBED_PERIPHERALNAMES_H
34+
#include "timer_api.h"
35+
#include "pwmout_api.h"
36+
#include "../hal/realtek/hal_pinmap_realtek.h"
37+
void pwmout_start(pwmout_t* obj);
38+
void pwmout_stop(pwmout_t* obj);
39+
#endif
40+
41+
// why can;t I call this?
42+
#include "../mqtt/new_mqtt.h"
43+
44+
3345

3446

3547
unsigned long ir_counter = 0;
@@ -39,6 +51,7 @@ extern "C" {
3951
uint8_t gIRPinPolarity = 0;
4052

4153
extern int my_strnicmp(const char* a, const char* b, int len);
54+
extern unsigned int g_timeMs;
4255
}
4356

4457

@@ -59,24 +72,34 @@ typedef unsigned short uint16_t;
5972
#define __FlashStringHelper char
6073

6174
// dummy functions
62-
void noInterrupts() {}
63-
void interrupts() {}
64-
65-
unsigned long millis() {
75+
#if PLATFORM_BEKEN
76+
void noInterrupts() { }
77+
void interrupts() { }
78+
void delay(int n) { }
79+
void delayMicroseconds(int n) { }
80+
unsigned long millis()
81+
{
6682
return 0;
6783
}
68-
unsigned long micros() {
84+
unsigned long micros()
85+
{
6986
return 0;
7087
}
71-
72-
73-
void delay(int n) {
74-
return;
88+
#else
89+
void noInterrupts() { taskENTER_CRITICAL(); }
90+
void interrupts() { taskEXIT_CRITICAL(); }
91+
void delay(int n) { delay_ms(n); }
92+
void delayMicroseconds(int n) { HAL_Delay_us(n); }
93+
unsigned long millis()
94+
{
95+
return g_timeMs;
7596
}
76-
77-
void delayMicroseconds(int n) {
78-
return;
97+
unsigned long micros()
98+
{
99+
return g_timeMs * 1000;
79100
}
101+
#endif
102+
80103

81104
class Print {
82105
public:
@@ -107,10 +130,21 @@ Print Serial;
107130
// #define ISR void IR_ISR
108131

109132
// THIS function is defined in src/libraries/IRremoteESP8266/src/IRrecv.cpp
110-
extern "C" void DRV_IR_ISR(UINT8 t);
133+
extern "C" void
134+
#if PLATFORM_BEKEN
135+
DRV_IR_ISR(UINT8 t)
136+
#else
137+
DRV_IR_ISR()
138+
#endif
139+
;
111140
extern void IR_ISR();
112141

142+
#if PLATFORM_BEKEN
113143
static UINT32 ir_chan = BKTIMER0;
144+
#elif PLATFORM_REALTEK
145+
static gtimer_t ir_timer;
146+
static UINT32 ir_chan = TIMER2;
147+
#endif
114148
static UINT32 ir_div = 1;
115149
static UINT32 ir_periodus = 50;
116150

@@ -121,6 +155,7 @@ void timerConfigForReceive() {
121155
void _timerConfigForReceive() {
122156
ir_counter = 0;
123157

158+
#if PLATFORM_BEKEN
124159
timer_param_t params = {
125160
(unsigned char)ir_chan,
126161
(unsigned char)ir_div, // div
@@ -156,20 +191,31 @@ void _timerConfigForReceive() {
156191
ADDLOG_INFO(LOG_FEATURE_IR, (char *)"ir timer setup %u", res);
157192
res = sddev_control((char *)TIMER_DEV_NAME, CMD_TIMER_UNIT_ENABLE, &ir_chan);
158193
ADDLOG_INFO(LOG_FEATURE_IR, (char *)"ir timer enabled %u", res);
194+
#elif PLATFORM_REALTEK
195+
gtimer_init(&ir_timer, ir_chan);
196+
#endif
159197
}
160198

161199
static void timer_enable() {
162200
}
163201
static void timer_disable() {
164202
}
165203
static void _timer_enable() {
166-
UINT32 res;
204+
UINT32 res = 0;
205+
#if PLATFORM_BEKEN
167206
res = sddev_control((char *)TIMER_DEV_NAME, CMD_TIMER_UNIT_ENABLE, &ir_chan);
207+
#elif PLATFORM_REALTEK
208+
gtimer_start_periodical(&ir_timer, ir_periodus, (void*)&DRV_IR_ISR, (uint32_t)&ir_timer);
209+
#endif
168210
ADDLOG_INFO(LOG_FEATURE_IR, (char *)"ir timer enabled %u", res);
169211
}
170212
static void _timer_disable() {
171-
UINT32 res;
213+
UINT32 res = 0;
214+
#if PLATFORM_BEKEN
172215
res = sddev_control((char *)TIMER_DEV_NAME, CMD_TIMER_UNIT_DISABLE, &ir_chan);
216+
#elif PLATFORM_REALTEK
217+
gtimer_stop(&ir_timer);
218+
#endif
173219
ADDLOG_INFO(LOG_FEATURE_IR, (char *)"ir timer disabled %u", res);
174220
}
175221

@@ -197,8 +243,6 @@ SpoofIrReceiver IrReceiver;
197243
#include "../libraries/IRremoteESP8266/src/IRproto.h"
198244
#include "../libraries/IRremoteESP8266/src/digitalWriteFast.h"
199245

200-
extern "C" int PIN_GetPWMIndexForPinIndex(int pin);
201-
202246
// override aspects of sending for our own interrupt driven sends
203247
// basically, IRsend calls mark(us) and space(us) to send.
204248
// we simply note the numbers into a rolling buffer, assume the first is a mark()
@@ -262,19 +306,10 @@ class myIRsend : public IRsend {
262306
ADDLOG_INFO(LOG_FEATURE_IR, (char *)"enableIROut %d freq %d duty",(int)freq, (int)duty);
263307
if(duty<1)
264308
duty=1;
265-
if(duty>100)
266-
duty=100;
267-
// just setup variables for use in ISR
268-
//pwmfrequency = ((uint32_t)aFrequencyKHz) * 1000;
269-
pwmperiod = (26000000 / freq);
270-
pwmduty = pwmperiod / (100/duty);
271-
272-
273-
#if PLATFORM_BK7231N
274-
bk_pwm_update_param((bk_pwm_t)this->pwmIndex, this->pwmperiod, pwmduty, 0, 0);
275-
#else
276-
bk_pwm_update_param((bk_pwm_t)this->pwmIndex, this->pwmperiod, pwmduty);
277-
#endif
309+
pwmduty = duty;
310+
311+
HAL_PIN_PWM_Start(this->sendPin, freq);
312+
HAL_PIN_PWM_Update(this->sendPin, duty);
278313
}
279314

280315
void resetsendqueue() {
@@ -302,14 +337,21 @@ class myIRsend : public IRsend {
302337
}
303338
return val;
304339
}
340+
#if PLATFORM_REALTEK
341+
void ledOff()
342+
{
343+
pwmout_start(g_pins[sendPin].pwm);
344+
}
305345

346+
void ledOn()
347+
{
348+
pwmout_stop(g_pins[sendPin].pwm);
349+
}
350+
#endif
306351
int currentsendtime;
307352
int currentbitval;
308353

309354
uint8_t sendPin;
310-
uint8_t pwmIndex;
311-
uint32_t pwmfrequency;
312-
uint32_t pwmperiod;
313355
uint32_t pwmduty;
314356

315357
uint32_t our_ms;
@@ -323,9 +365,15 @@ IRrecv *ourReceiver = NULL;
323365

324366
// this is our ISR.
325367
// it is called every 50us, so we need to work on making it as efficient as possible.
326-
extern "C" void DRV_IR_ISR(UINT8 t) {
368+
extern "C" void
369+
#if PLATFORM_BEKEN
370+
DRV_IR_ISR(UINT8 t)
371+
#else
372+
DRV_IR_ISR()
373+
#endif
374+
{
327375
int sending = 0;
328-
if (pIRsend && (pIRsend->pwmIndex >= 0)) {
376+
if (pIRsend) {
329377
pIRsend->our_us += 50;
330378
if (pIRsend->our_us > 1000) {
331379
pIRsend->our_ms++;
@@ -373,17 +421,13 @@ extern "C" void DRV_IR_ISR(UINT8 t) {
373421
uint32_t duty = pIRsend->pwmduty;
374422
if (!pinval) {
375423
if (gIRPinPolarity) {
376-
duty = pIRsend->pwmperiod;
424+
duty = 100;
377425
}
378426
else {
379427
duty = 0;
380428
}
381429
}
382-
#if PLATFORM_BK7231N
383-
bk_pwm_update_param((bk_pwm_t)pIRsend->pwmIndex, pIRsend->pwmperiod, duty, 0, 0);
384-
#else
385-
bk_pwm_update_param((bk_pwm_t)pIRsend->pwmIndex, pIRsend->pwmperiod, duty);
386-
#endif
430+
HAL_PIN_PWM_Update(pIRsend->sendPin, duty);
387431
}
388432

389433
// is someone really wants rx and TX at the same time, then allow it.
@@ -729,28 +773,16 @@ extern "C" void DRV_IR_Init() {
729773
}
730774

731775
if (txpin > 0) {
732-
int pwmIndex = PIN_GetPWMIndexForPinIndex(txpin);
733776
// is this pin capable of PWM?
734-
if (pwmIndex != -1) {
777+
if (HAL_PIN_CanThisPinBePWM(txpin)) {
735778
uint32_t pwmfrequency = 38000;
736-
uint32_t period = (26000000 / pwmfrequency);
737-
uint32_t duty = period / 2;
738-
#if PLATFORM_BK7231N
739-
// OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);
740-
bk_pwm_initialize((bk_pwm_t)pwmIndex, period, duty, 0, 0);
741-
#else
742-
bk_pwm_initialize((bk_pwm_t)pwmIndex, period, duty);
743-
#endif
744-
bk_pwm_start((bk_pwm_t)pwmIndex);
779+
HAL_PIN_PWM_Start(txpin, pwmfrequency);
745780
myIRsend *pIRsendTemp = new myIRsend((uint_fast8_t)txpin);
746781
pIRsendTemp->resetsendqueue();
747-
pIRsendTemp->enableIROut(pwmfrequency,50);
748-
pIRsendTemp->pwmIndex = pwmIndex;
749-
pIRsendTemp->pwmduty = duty;
750-
pIRsendTemp->pwmperiod = period;
782+
pIRsendTemp->enableIROut(pwmfrequency, 50);
783+
pIRsendTemp->pwmduty = 50;
751784

752785
pIRsend = pIRsendTemp;
753-
//bk_pwm_stop((bk_pwm_t)pIRsend->pwmIndex);
754786

755787
//cmddetail:{"name":"IRSend","args":"[PROT-ADDR-CMD-REP]",
756788
//cmddetail:"descr":"Sends IR commands in the form PROT-ADDR-CMD-REP, e.g. NEC-1-1A-0",
@@ -779,6 +811,7 @@ extern "C" void DRV_IR_Init() {
779811
if ((pin > 0) || (txpin > 0)) {
780812
// both tx and rx need the interrupt
781813
_timerConfigForReceive();
814+
delay_ms(10);
782815
_timer_enable();
783816
}
784817
}

src/hal/bk7231/hal_pins_bk7231.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "../../beken378/driver/gpio/gpio.h"
1414

1515
// must fit all pwm indexes
16-
static uint32_t g_periods[6];
16+
static uint32_t g_periods[6] = { 0 };
1717

1818
int PIN_GetPWMIndexForPinIndex(int pin)
1919
{
@@ -94,6 +94,7 @@ void HAL_PIN_PWM_Stop(int index) {
9494
return;
9595
}
9696

97+
g_periods[pwmIndex] = 0;
9798
bk_pwm_stop(pwmIndex);
9899
}
99100

@@ -108,6 +109,12 @@ void HAL_PIN_PWM_Start(int index, int freq) {
108109
}
109110

110111
uint32_t period = (26000000 / freq);
112+
if(g_periods[pwmIndex] != 0 && g_periods[pwmIndex] != period)
113+
{
114+
g_periods[pwmIndex] = period;
115+
return;
116+
}
117+
111118
g_periods[pwmIndex] = period;
112119
#if defined(PLATFORM_BK7231N) && !defined(PLATFORM_BEKEN_NEW)
113120
// OSStatus bk_pwm_initialize(bk_pwm_t pwm, uint32_t frequency, uint32_t duty_cycle);

src/libraries/IRremoteESP8266/src/IRremoteESP8266.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,9 @@ extern "C" {
15101510
#define DPRINTLN(x)
15111511
#endif
15121512
#endif // DEBUG
1513+
#if PLATFORM_REALTEK
1514+
#define strcasecmp strcmp
1515+
#endif
15131516

15141517
#ifdef UNIT_TEST
15151518
#ifndef F

0 commit comments

Comments
 (0)