/** * @brief Initializes the OLED display. * @retval None */ void Blox_OLED_Init(void) { uint8_t garbage; OLED_RCC_Configuration(); OLED_GPIO_Configuration(); SysTick_Init(); Blox_VUSART_Init(OLED_USART_ID); OLED_Reset(); SysTick_Wait(2000); Blox_VUSART_TryReceive(2, &garbage); //receive any garbage data Blox_OLED_Send(OLED_AUTOBAUD); Blox_OLED_Receive(); }
/** * @brief Receives a BloxFrame from the XBee. * @retval The received frame or NULL on error. */ BloxFrame * Blox_XBee_Receive(void) { XBeeRxFrame frame; BloxFrame *retFrame; uint8_t ret = 0; uint8_t checksum = 0; uint32_t i; if (Blox_VUSART_TryReceive(XBEE_VUSART_ID, &ret) != VUSART_SUCCESS) return NULL; if (ret != 0x7E) return NULL; Blox_VUSART_Receive(XBEE_VUSART_ID, &ret); frame.length = ret << 8; Blox_VUSART_Receive(XBEE_VUSART_ID, &ret); frame.length |= ret; if(frame.length > 100) { return NULL; } Blox_VUSART_Receive(XBEE_VUSART_ID, &(frame.api)); checksum += frame.api; Blox_VUSART_Receive(XBEE_VUSART_ID, &ret); //Source Address MSB frame.source = ret << 8; checksum += ret; Blox_VUSART_Receive(XBEE_VUSART_ID, &ret); //Source Address LSB frame.source |= ret; checksum += ret; Blox_VUSART_Receive(XBEE_VUSART_ID, &ret); //RSSI frame.rssi = ret; checksum += ret; Blox_VUSART_Receive(XBEE_VUSART_ID, &ret); //Options frame.options = ret; checksum += ret; for(i = 0; i < frame.length-5; i++) { Blox_VUSART_Receive(XBEE_VUSART_ID, ((uint8_t *)&(frame.blox_frame))+i); checksum += ((char *)&(frame.blox_frame))[i]; } Blox_VUSART_Receive(XBEE_VUSART_ID, &(frame.checksum)); if (frame.checksum != 0xFF-checksum) return NULL; retFrame = (BloxFrame *)malloc(sizeof(BloxFrame)); memcpy(retFrame, &(frame.blox_frame), sizeof(BloxFrame)); return retFrame; }
/** * @brief Prints out the configuration options of the XBee. * @retval XBEE_OK if successfull, XBEE_INIT_FAIL on failure. */ XBEE_STATUS Blox_XBee_Print(void) { uint8_t garbage; Blox_VUSART_TryReceive(XBEE_VUSART_ID, &garbage); //clear VUSART buffer before send/receive Blox_VUSART_Send(XBEE_VUSART_ID, 'X'); // Junk character to before init SysTick_Wait(1100); Blox_VUSART_SendData(XBEE_VUSART_ID, "+++", 3) ; //Enter Command Mode if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(1100); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATMY\r", 5) ; //Read source address Blox_DebugStr("ATMY:"); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c\r\n", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); //\r Blox_VUSART_SendData(XBEE_VUSART_ID, "ATDL\r", 5) ; //Read source address Blox_DebugStr("ATDL:"); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c\r\n", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); //\r Blox_VUSART_SendData(XBEE_VUSART_ID, "ATDH\r", 5) ; //Read source address Blox_DebugStr("ATDH:"); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c\r\n", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); //\r Blox_VUSART_SendData(XBEE_VUSART_ID, "ATAP\r", 5) ; //Read source address Blox_DebugStr("ATAP:"); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); Blox_DebugPat("%c\r\n", garbage); Blox_VUSART_Receive(XBEE_VUSART_ID, &garbage); //\r Blox_VUSART_SendData(XBEE_VUSART_ID, "ATCN\r", 5) ; //Exit command mode if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); return XBEE_OK; }
/** * @brief Initializes the XBees sleep and reset pins, and then resets the XBee. * @retval None */ XBEE_STATUS Blox_XBee_Init(void) { uint8_t garbage; XBee_RCC_Configuration(); XBee_GPIO_Configuration(); Blox_System_Init(); SysTick_Init(); XBEE_SLEEP_GPIO->ODR &= ~(XBEE_SLEEP_PIN); XBEE_RESET_GPIO->ODR |= XBEE_RESET_PIN; SysTick_Wait(1); XBEE_RESET_GPIO->ODR &= ~(XBEE_RESET_PIN); SysTick_Wait(1); XBEE_RESET_GPIO->ODR |= XBEE_RESET_PIN; SysTick_Wait(300); Blox_VUSART_Init(XBEE_VUSART_ID); Blox_VUSART_TryReceive(XBEE_VUSART_ID, &garbage); //clear VUSART buffer before send/receive Blox_VUSART_Disable_RXNE_IRQ(XBEE_VUSART_ID); Blox_VUSART_Register_RXNE_IRQ(XBEE_VUSART_ID, &Blox_XBee_VUSART_RXNE_IRQ); Blox_VUSART_Enable_RXNE_IRQ(XBEE_VUSART_ID); return XBEE_OK; }
/** * @brief Configures the XBee and writes the configuration to non-volatile mem. * @retval XBEE_OK if successfull, XBEE_INIT_FAIL on failure. */ XBEE_STATUS Blox_XBee_Config(void) { char buffer[10]; uint8_t garbage; SysVar sys; XBee_RCC_Configuration(); XBee_GPIO_Configuration(); Blox_VUSART_Init(XBEE_VUSART_ID); SysTick_Init(); Blox_System_Init(); Blox_System_GetVars(&sys); XBEE_SLEEP_GPIO->ODR &= ~(XBEE_SLEEP_PIN); XBEE_RESET_GPIO->ODR |= XBEE_RESET_PIN; SysTick_Wait(1); XBEE_RESET_GPIO->ODR &= ~(XBEE_RESET_PIN); SysTick_Wait(1); XBEE_RESET_GPIO->ODR |= XBEE_RESET_PIN; SysTick_Wait(1100); Blox_VUSART_TryReceive(XBEE_VUSART_ID, &garbage); //clear VUSART buffer before send/receive Blox_VUSART_Send(XBEE_VUSART_ID, 'X'); // Junk character to before init SysTick_Wait(1100); Blox_VUSART_SendData(XBEE_VUSART_ID, "+++", 3) ; //Enter Command Mode if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(1100); sprintf(buffer, "ATMY%d\r", sys.id); Blox_VUSART_SendData(XBEE_VUSART_ID, (uint8_t *)buffer, strlen(buffer)) ; //Enter Command Mode if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATDLFFFF\r", 9) ; //Set broadcast dest ID if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATDH0\r", 6) ; //Set high of dest ID if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATAP1\r", 6) ; //API Mode 1 (no escapes) if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATRN1\r", 6) ; //API Mode 1 (no escapes) if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATWR\r", 9) ; //Write to non-volatile memory if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); Blox_VUSART_SendData(XBEE_VUSART_ID, "ATCN\r", 5) ; //Exit command mode if (XBee_CheckOkResponse() == FALSE) return XBEE_INIT_FAIL; SysTick_Wait(20); return XBEE_OK; }
/** * @brief The function that XBee registers with VUSART to execute on byte received. * @retval None. */ void Blox_XBee_VUSART_RXNE_IRQ(void) { static uint8_t num = 0; static uint8_t checksum = 0; static XBeeFrame frame; uint8_t data; if (Blox_VUSART_TryReceive(XBEE_VUSART_ID, &data) == RX_EMPTY) return; if (num == 0) { if (data == 0x7E) { num = 1; //Start of a frame checksum = 0; } } else if (num == 1) { frame.length = data << 8; num++; } else if (num == 2) { frame.length |= data; if (frame.length > 100) num = 0; else num++; } else if (num > 2) { if (num == frame.length+3) { if(0xFF-checksum != data) num = 0; else { XBeeTxStatusFrame *status; switch(frame.data[0]) { case API_TX_STATUS: status = (XBeeTxStatusFrame *)&frame; if (status->status == 0 && data == 0xFF-checksum) XBee_TxStatus_Flag = XBEE_TXSTATUS_SUCCESS; else XBee_TxStatus_Flag = XBEE_TXSTATUS_ERROR; break; case API_RX_FRAME: if (XBee_RX_Handler != NULL && XBee_RX_Enable == TRUE) { XBeeRxFrame rx_frame; BloxFrame *retFrame; rx_frame.length = frame.length; rx_frame.api = frame.data[0]; rx_frame.source = frame.data[1] << 8; rx_frame.source = frame.data[2]; rx_frame.rssi = frame.data[3]; rx_frame.options = frame.data[4]; memcpy(&(rx_frame.blox_frame), &(frame.data[5]), rx_frame.length-5); rx_frame.checksum = data; retFrame = (BloxFrame *)malloc(sizeof(BloxFrame)); memcpy(retFrame, &(rx_frame.blox_frame), sizeof(BloxFrame)); XBee_RX_Handler(retFrame); free(retFrame); } break; } } num = 0; } else { frame.data[num-3] = data; checksum += data; num++; } } }
/** * @brief Receive a byte from the OLED. Wrapper around USART * @retval The received command or 0 on error. */ uint8_t Blox_OLED_Receive(void) { uint8_t data; while(Blox_VUSART_TryReceive(2, &data) == RX_EMPTY); return data; }