Skip to content

Commit

Permalink
added asr_NeoPixel.c
Browse files Browse the repository at this point in the history
  • Loading branch information
Heltec-Aaron-Lee committed Nov 1, 2022
1 parent 538fac1 commit 1d262a6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
69 changes: 69 additions & 0 deletions cores/asr650x/asr_NeoPixel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <ASR_Arduino.h>
#include <CyLib.h>



#define CYCLES_800_T0H 19 // 0.4us
#define CYCLES_800_T1H 38// 0.8us
#define CYCLES_800 60 // 1.25us per bit
#define CYCLES_400_T0H 24// 0.5uS
#define CYCLES_400_T1H 30// 1.2us
#define CYCLES_400 75 // 2.5us per bit

void ASR_NeoPixelShow(
uint8_t pin, uint8_t *pixels, uint32_t numBytes, bool is800KHz) {

uint8_t *p, *end, pix, mask;
uint32_t t, time0, time1, period, startTime, pinMask,saveLoad,pinHigh,pinLow;

uint8 port=pin / PIN_NUMBER_IN_PORT;
uint8 PIN_SHIFT=pin % PIN_NUMBER_IN_PORT;
uint32 REG_DR=CYREG_GPIO_PRT0_DR + port * PORT_REG_SHFIT;
uint8 Pin_MASK=0x01<<PIN_SHIFT;
uint8 drVal = (uint8)((* (reg32 *) REG_DR) & (~Pin_MASK));

pinHigh=drVal|Pin_MASK;
pinLow=drVal & (~(uint32_t)Pin_MASK);

p = pixels;
end = p + numBytes;
pix = *p++;
mask = 0x80;
startTime = 0;

period = CYCLES_800;
time0 = period - CYCLES_800_T0H;
time1 = period - CYCLES_800_T1H;

saveLoad = CY_SYS_SYST_RVR_REG;
CY_SYS_SYST_RVR_REG = period;
CY_SYS_SYST_CVR_REG = 0u;
//pinMode(pin,OUTPUT);

for(t=time0;;t=time0) {
while(CY_SYS_SYST_CVR_REG < period);//wait for bit start
(* (reg32 *) REG_DR)=pinHigh; //write high
if(pix & mask) t = time1;

while(CY_SYS_SYST_CVR_REG > t); // Wait high duration
(* (reg32 *) REG_DR)=pinLow;//write low

if(!(mask >>= 1)) { // Next bit/byte
if(p >= end) break;
pix = *p++;
mask = 0x80;
}
}
while(CY_SYS_SYST_CVR_REG < period); //wait for the last bit.
// (* (reg32 *) REG_DR)=pinLow;
CY_SYS_SYST_RVR_REG = saveLoad;

}




1 change: 0 additions & 1 deletion cores/asr6601/lora/system/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ void TimerIrqHandler( void )
n++;
cur = TimerListHead;
TimerListHead = TimerListHead->Next;
cur->IsRunning = false;
exec_cb( cur->Callback );
//update timestamps after callbacks
TimeStampsUpdate();
Expand Down
20 changes: 11 additions & 9 deletions cores/asr6601/peripheral/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ bool HardwareSerial::begin(uint32_t baud, uint32_t config, int rxPin, int txPin,
_txPin = txPin;

_uart = uartStart(baud,config,_rxPin,_txPin);
_uartSymbolTime=1000000/_baud*11;

if(_uart<0)
return false;
else
Expand All @@ -50,6 +52,7 @@ void HardwareSerial::updateBaudRate(unsigned long baud)
{
_baud = baud;
uartStart(baud,_config,_rxPin,_txPin);
_uartSymbolTime=1000000/_baud*11;
}

void HardwareSerial::end()
Expand Down Expand Up @@ -119,6 +122,11 @@ size_t HardwareSerial::write(const uint8_t *buffer, size_t size)
#endif
}

void HardwareSerial::delayByte(void)
{
delayMicroseconds(11000000/_baud);
}

uint32_t HardwareSerial::baudRate()
{
return _baud;
Expand All @@ -140,18 +148,12 @@ int HardwareSerial::read(uint8_t* buff, uint32_t timeout)
}
}
int serialBuffer_index=0;
int i=0;
while(available())
{
buff[serialBuffer_index++]=read();

int i = 0;
while(i<1000)
{
if(available())
break;
delayMicroseconds(1);
i++;
}
if(available() == 0)
delayByte();//wait for a byte
}
return serialBuffer_index;
//Serial.write(serialBuffer,serialBuffer_index);
Expand Down
2 changes: 2 additions & 0 deletions cores/asr6601/peripheral/HardwareSerial.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class HardwareSerial: public Print
int read(uint8_t* buff, uint32_t timeout);
void flush(void);
void flush( bool txOnly);
void delayByte();
size_t write(uint8_t c);
size_t write(const uint8_t *buffer, size_t size);
bool busy(void);
Expand Down Expand Up @@ -55,6 +56,7 @@ class HardwareSerial: public Print
int _rxPin=-1;
int _txPin=-1;
uint32_t _config;
uint32_t _uartSymbolTime;
};

extern HardwareSerial Serial;
Expand Down

0 comments on commit 1d262a6

Please sign in to comment.