void ZigBee_ReadFrame(uint8_t * data, uint8_t * len) { uint8_t resp; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); //read off any bytes in the buffer while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //enable select pin PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); //send the read command SPI_SendData8(SPI, 0x20); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //receive the buffer length SPI_SendData8(SPI, 0); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); uint8_t nbytes = resp+3; //packet + lqi + ed + status (link quality indicatior) (energy detection) *len = nbytes; //set the return value // Datasheet says max buffer access is 5 + frame length = 132 // 2 reads come before frame, 3 after // First 2 reads not included in nbytes // Therefore, max nbytes size is 132 - 2 = 130 if (nbytes > 130) { *len = 0; _delay_us(1); PullUp(); return; } for (uint8_t ii=0; ii<nbytes; ii++) { SPI_SendData8(SPI, 0); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //receive data *data++ = resp; } _delay_us(1); PullUp(); }
void ZigBee_WriteReg(uint8_t addr, uint8_t val) { SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); //read off any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) SPI_ReceiveData8(SPI); //enable NSS pin PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); uint8_t cmd = addr; cmd |= 0xC0; // write cmd SPI_SendData8(SPI, cmd); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); SPI_SendData8(SPI, val); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); _delay_us(1); PullUp(); }
uint8_t ZigBee_WriteFrame(const void* data, uint8_t len) { if (len > MAX_ZIGBEE_PACKET_BYTES) return -1; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); const uint8_t *tx_ptr = data; //clear any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) SPI_ReceiveData8(SPI); PullDown(); _delay_us(1); //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); SPI_SendData8(SPI, 0x60); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); SPI_SendData8(SPI, len+2); //two extra bytes for the 16-bit CRC while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); for(int ii = 0; ii < len; ii++) { SPI_SendData8(SPI, tx_ptr[ii]); while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); SPI_ReceiveData8(SPI); } _delay_us(1); PullUp(); ZigBee_WriteReg(REG_TRX_STATE, 0x02); //alternate method for signaling to send return 0; }
void ZigBee_ReadReg(uint8_t addr, uint8_t len, uint8_t * dest) { uint8_t resp; SPI_TypeDef* SPI = MAKENAME(SPI,ZIGBEE_SPI); //read off any remaining bytes while(SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)) resp = SPI_ReceiveData8(SPI); PullDown(); _delay_us(1); for(uint8_t ii=0; ii<len; ii++) { //wait until TX buffer is empty while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_TXE)); uint8_t cmd = addr; cmd |= 0x80; // read cmd SPI_SendData8(SPI, cmd); //send the command while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); //this is PHY_STATUS byte from ZigBee SPI_SendData8(SPI, 0); // send dummy while(!SPI_I2S_GetFlagStatus(SPI, SPI_I2S_FLAG_RXNE)); resp = SPI_ReceiveData8(SPI); *dest++ = resp; } _delay_us(1); PullUp(); }
void ZigBee_InitPins() { GPIO_InitTypeDef GPIO_InitStructure; RCC_AHBPeriphClockCmd(MAKENAME(RCC_AHBPeriph_GPIO,ZIGBEE_SCK_PORT), ENABLE); RCC_AHBPeriphClockCmd(MAKENAME(RCC_AHBPeriph_GPIO,ZIGBEE_MISO_PORT), ENABLE); RCC_AHBPeriphClockCmd(MAKENAME(RCC_AHBPeriph_GPIO,ZIGBEE_MOSI_PORT), ENABLE); RCC_AHBPeriphClockCmd(MAKENAME(RCC_AHBPeriph_GPIO,ZIGBEE_SS_PORT), ENABLE); //SCK GPIO_InitStructure.GPIO_Pin = MAKENAME(GPIO_Pin_,ZIGBEE_SCK_PIN); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MAKENAME(GPIO,ZIGBEE_SCK_PORT), &GPIO_InitStructure); //MISO GPIO_InitStructure.GPIO_Pin = MAKENAME(GPIO_Pin_,ZIGBEE_MISO_PIN); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MAKENAME(GPIO,ZIGBEE_MISO_PORT), &GPIO_InitStructure); //MOSI GPIO_InitStructure.GPIO_Pin = MAKENAME(GPIO_Pin_,ZIGBEE_MOSI_PIN); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MAKENAME(GPIO,ZIGBEE_MOSI_PORT), &GPIO_InitStructure); // slave select PullUp(); GPIO_InitStructure.GPIO_Pin = MAKENAME(GPIO_Pin_,ZIGBEE_SS_PIN); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MAKENAME(GPIO,ZIGBEE_SS_PORT), &GPIO_InitStructure); // Configure SPI pins: SCK, MISO and MOSI GPIO_PinAFConfig(MAKENAME(GPIO,ZIGBEE_SCK_PORT), MAKENAME(GPIO_PinSource,ZIGBEE_SCK_PIN), MAKENAME(GPIO_AF_,ZIGBEE_SCK_AF)); GPIO_PinAFConfig(MAKENAME(GPIO,ZIGBEE_MISO_PORT), MAKENAME(GPIO_PinSource,ZIGBEE_MISO_PIN), MAKENAME(GPIO_AF_,ZIGBEE_MISO_AF)); GPIO_PinAFConfig(MAKENAME(GPIO,ZIGBEE_MOSI_PORT), MAKENAME(GPIO_PinSource,ZIGBEE_MOSI_PIN), MAKENAME(GPIO_AF_,ZIGBEE_MOSI_AF)); #ifdef ZIGBEE_RESET_PIN RCC_AHBPeriphClockCmd(MAKENAME(RCC_AHBPeriph_GPIO,ZIGBEE_RESET_PORT), ENABLE); GPIO_InitStructure.GPIO_Pin = MAKENAME(GPIO_Pin_,ZIGBEE_RESET_PIN); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MAKENAME(GPIO,ZIGBEE_RESET_PORT), &GPIO_InitStructure); #endif #ifdef ZIGBEE_HGM_PIN RCC_AHBPeriphClockCmd(MAKENAME(RCC_AHBPeriph_GPIO,ZIGBEE_HGM_PORT), ENABLE); GPIO_InitStructure.GPIO_Pin = 1<<ZIGBEE_HGM_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(MAKENAME(GPIO,ZIGBEE_HGM_PORT), &GPIO_InitStructure); GPIO_PinLockConfig(MAKENAME(GPIO,ZIGBEE_HGM_PORT),1<<ZIGBEE_HGM_PIN); #endif }
void GPIO_PORT::SetupGPIO_InPullUp (GPIO_PIN_N pin) { *_pGPIO_MODER &= ~(3<<pin*2); *_pGPIO_MODER |= GPIO_MODE_IN<<pin*2; PullUp(pin); }