/* Gets a character from the UART, returns EOF if no character is ready */ int Board_UARTGetChar(void) { #if defined(DEBUG_UART) uint8_t ch; if (Chip_UART_Read(DEBUG_UART, &ch, 1) == 1) { return (int) ch; } #endif return EOF; }
/* Gets a character from the UART, returns EOF if no character is ready */ int Board_UARTGetChar(void) { #if defined(DEBUG_UART) uint8_t data; if (Chip_UART_Read(DEBUG_UART, &data, 1) == 1) { return (int) data; } #endif return EOF; }
/* Read data through the UART peripheral (blocking) */ int Chip_UART_ReadBlocking(LPC_USART_T *pUART, void *data, int numBytes) { int pass, readBytes = 0; uint8_t *p8 = (uint8_t *) data; while (readBytes < numBytes) { pass = Chip_UART_Read(pUART, p8, numBytes); numBytes -= pass; readBytes += pass; p8 += pass; } return readBytes; }
/** * @brief UART interrupt handler sub-routine * @return Nothing */ void UART_IRQHandler(void) { uint32_t count = 0; /* Handle transmit interrupt if enabled */ if (LPC_USART->IER & UART_IER_THREINT) { if (g_uCOM.txBuf_count > 0) { count = Chip_UART_Send(LPC_USART, &g_uCOM.txBuf[g_uCOM.txBuf_uartIndex], g_uCOM.txBuf_count); g_uCOM.txBuf_count -= count; g_uCOM.txBuf_uartIndex += count; } /* If usbRxBuf empty check if any packet pending USB EP RAM */ if (g_uCOM.txBuf_count < 1) { if (g_uCOM.usbRxPending > 0) { g_uCOM.usbRxPending--; g_uCOM.txBuf_count = USBD_API->hw->ReadEP(g_uCOM.hUsb, USB_CDC_OUT_EP, g_uCOM.txBuf); g_uCOM.txBuf_uartIndex = 0; } else { /* all data transmitted on UART disable UART_IER_THREINT */ Chip_UART_IntDisable(LPC_USART, UART_IER_THREINT); } } } /* Handle receive interrupt */ count = Chip_UART_Read(LPC_USART, &g_uCOM.rxBuf[g_uCOM.rxBuf_uartIndex], UCOM_BUF_SZ - g_uCOM.rxBuf_uartIndex); if (count) { /* Note, following logic works if UCOM_BUF_SZ is 2^n size only. */ g_uCOM.rxBuf_uartIndex = (g_uCOM.rxBuf_uartIndex + count) & (UCOM_BUF_SZ - 1); /* If USB Tx is not busy kick start USB Tx */ if (g_uCOM.usbTxBusy == 0) { g_uCOM.usbTxBusy = 1; count = USBD_API->hw->WriteEP(g_uCOM.hUsb, USB_CDC_IN_EP, &g_uCOM.rxBuf[g_uCOM.rxBuf_usbIndex], count); g_uCOM.rxBuf_usbIndex = (g_uCOM.rxBuf_usbIndex + count) & (UCOM_BUF_SZ - 1); } } }
void UART_IRQHandler(UCOM_DATA_T *pUcom ) { uint32_t count = 0; /* Handle transmit interrupt if enabled */ if (pUcom->selected->IER & UART_IER_THREINT) { if (pUcom->txBuf_count > 0) { count = Chip_UART_Send(pUcom->selected, &pUcom->txBuf[pUcom->txBuf_uartIndex], pUcom->txBuf_count); pUcom->txBuf_count -= count; pUcom->txBuf_uartIndex += count; } /* If usbRxBuf empty check if any packet pending USB EP RAM */ if (pUcom->txBuf_count < 1) { if ((pUcom->usbRxPending > 0) && USB_IsConfigured(pUcom->hUsb)) { pUcom->usbRxPending--; pUcom->txBuf_count = USBD_API->hw->ReadEP(pUcom->hUsb, pUcom->outEndpoint, pUcom->txBuf); pUcom->txBuf_uartIndex = 0; } else { /* all data transmitted on UART disable UART_IER_THREINT */ Chip_UART_IntDisable(pUcom->selected, UART_IER_THREINT); } } } /* Handle receive interrupt */ count = Chip_UART_Read(pUcom->selected, &pUcom->rxBuf[pUcom->rxBuf_uartIndex], UCOM_BUF_SZ - pUcom->rxBuf_uartIndex); if (count) { /* Note, following logic works if UCOM_BUF_SZ is 2^n size only. */ pUcom->rxBuf_uartIndex = (pUcom->rxBuf_uartIndex + count) & (UCOM_BUF_SZ - 1); /* If USB Tx is not busy kick start USB Tx */ if ((pUcom->usbTxBusy == 0) && USB_IsConfigured(pUcom->hUsb)) { pUcom->usbTxBusy = 1; count = USBD_API->hw->WriteEP(pUcom->hUsb, pUcom->inEndpoint, &pUcom->rxBuf[pUcom->rxBuf_usbIndex], count); pUcom->rxBuf_usbIndex = (pUcom->rxBuf_usbIndex + count) & (UCOM_BUF_SZ - 1); } } }
int8_t Board_UART_Read(void *data, uint8_t num_bytes) { return Chip_UART_Read(LPC_USART, data, num_bytes); }
int main(void) { if (Board_SysTick_Init()) { while(1); } Board_LEDs_Init(); Board_LED_On(LED0); Board_ADC_Init(); uint16_t tps_data = 0; int16_t tps_error = 0; Board_UART_Init(9600); DEBUG_Print("Started up\n\r"); RingBuffer_Init(&rx_buffer, _rx_buffer, sizeof(CCAN_MSG_OBJ_T), 8); RingBuffer_Flush(&rx_buffer); Board_CAN_Init(TEST_CCAN_BAUD_RATE, CAN_rx, CAN_tx, CAN_error); msg_obj.msgobj = 1; msg_obj.mode_id = 0x301; msg_obj.mask = 0x000; LPC_CCAN_API->config_rxmsgobj(&msg_obj); can_error_flag = false; can_error_info = 0; bool send = false; while (1) { Board_TPS_ADC_Read(&tps_data); // itoa(tps_data,tx_buffer_str,10); // DEBUG_Print("TPS_DATA:"); // DEBUG_Print(tx_buffer_str); // DEBUG_Print("\r\n"); if (!RingBuffer_IsEmpty(&rx_buffer)) { CCAN_MSG_OBJ_T temp_msg; RingBuffer_Pop(&rx_buffer, &temp_msg); DEBUG_Print("Received Message ID: 0x"); itoa(temp_msg.mode_id, str, 16); DEBUG_Print(str); DEBUG_Print("\r\n"); } if (can_error_flag) { can_error_flag = false; DEBUG_Print("CAN Error: 0b"); itoa(can_error_info, str, 2); DEBUG_Print(str); DEBUG_Print("\r\n"); } if (send) { DEBUG_Print("Sending CAN with ID: 0x301\r\n"); msg_obj.msgobj = 2; msg_obj.mode_id = 0x301; msg_obj.dlc = 4; msg_obj.data[0] = tps_data>>2; msg_obj.data[1] = (uint8_t) 0x00; msg_obj.data[2] = (uint8_t) tps_error; LPC_CCAN_API->can_transmit(&msg_obj); Board_UART_PrintNum(msg_obj.data[0],10,true); Board_UART_PrintNum(msg_obj.data[1],10,true); Board_UART_PrintNum(msg_obj.data[2],10,true); } uint8_t count; count = Chip_UART_Read(LPC_USART, uart_rx_buf, UART_RX_BUFFER_SIZE); if (count != 0) { switch (uart_rx_buf[0]) { case 'a': send = true; break; case 'z': send = false; break; case 'r': DEBUG_Print("Sending CAN with ID: 0x301\r\n"); msg_obj.msgobj = 2; msg_obj.mode_id = 0x301; msg_obj.dlc = 2; msg_obj.data[0] = tps_data; msg_obj.data[1] = tps_error; LPC_CCAN_API->can_transmit(&msg_obj); Board_UART_PrintNum(msg_obj.data[0],10,true); Board_UART_PrintNum(msg_obj.data[1],10,true); break; default: DEBUG_Print("Invalid Command\r\n"); break; } } } }
/** * @details Where all the magic happens * @return Shouldn't return */ int main(void) { Init_Core(); Init_SM(); Init_Board(); Init_Globals(); Init_CAN(); Init_Timers(); // ------------------------------------------------ // Begin DEBUG_Print("Started Up\r\n"); while(1) { uint8_t count; if ((count = Chip_UART_Read(LPC_USART, Rx_Buf, UART_RX_BUF_SIZE)) != 0) { switch (Rx_Buf[0]) { case 'a': // Print out Brusa Mains Info DEBUG_Print("Actual Mains Voltage: "); itoa(brusa_actual_1.mains_mVolts, str, 10); DEBUG_Print(str); DEBUG_Print("\r\n"); DEBUG_Print("Mains type: "); itoa(brusa_actual_1.mains_cAmps, str, 10); DEBUG_Print(str); DEBUG_Print("\r\n"); DEBUG_Print("Temp: "); itoa((brusa_temp.power_temp / 10) - 40 , str, 10); DEBUG_Print(str); DEBUG_Print("\r\n"); DEBUG_Print("Temp: 0x"); itoa(brusa_temp.power_temp , str, 16); DEBUG_Print(str); DEBUG_Print("\r\n"); break; case 'b': // Print out Actual Brusa Output DEBUG_Print("Actual Out Voltage: "); itoa(brusa_actual_1.output_mVolts, str, 10); DEBUG_Println(str); DEBUG_Print("Actual Out Current: "); itoa(brusa_actual_1.output_cAmps, str, 10); DEBUG_Println(str); break; case 'f': // Print out Pack State itoa(pack_state.pack_min_mVolts, str, 10); DEBUG_Print("Pack Min Voltage: "); DEBUG_Print(str); DEBUG_Print("\r\n"); itoa(pack_state.pack_max_mVolts, str, 10); DEBUG_Print("Pack Max Voltage: "); DEBUG_Print(str); DEBUG_Print("\r\n"); break; case 'y': // Print out Module Balance State itoa(PackManager_GetExtModId(0), str, 16); DEBUG_Print("Mod 0x"); DEBUG_Print(str); itoa(PackManager_GetExtBal(0), str, 2); DEBUG_Print(": 0b"); DEBUG_Println(str); itoa(PackManager_GetExtModId(1), str, 16); DEBUG_Print("Mod 0x"); DEBUG_Print(str); itoa(PackManager_GetExtBal(1), str, 2); DEBUG_Print(": 0b"); DEBUG_Println(str); break; case 'e': itoa(brusa_error,str, 2); DEBUG_Println(str); break; case 'm': // Print out charge mode and brusa error DEBUG_Print("Charge Mode: "); itoa(Charge_GetMode(), str, 10); DEBUG_Println(str); DEBUG_Print("Error Messages: "); itoa((uint64_t)brusa_error, str, 2); DEBUG_Println(str); break; default: DEBUG_Print("Unknown Command\r\n"); } } //----------------------------- // Detect Input Changes (Default to IDLE) MODE_INPUT_T inp = INP_IDLE; if (!Board_Switch_Read()) { inp = INP_CHRG; } else { inp = INP_IDLE; } //----------------------------- // Update pack_state pack_state.contactors_closed = Board_Contactors_Closed(); pack_state.msTicks = msTicks; pack_state.brusa_error = brusa_error; pack_state.pack_cAmps_in = brusa_actual_1.output_cAmps; //----------------------------- // SSM Step ERROR_T result = SSM_Step(&pack_state, inp, &out_state); if (result != ERROR_NONE) { _error(result, true, false); } //----------------------------- // Check if SSM has Changed State // Currently only changes Poll Frequency // [TODO] Set a status LED!! if (SSM_GetMode() != mode) { mode = SSM_GetMode(); switch (SSM_GetMode()) { case IDLE: Chip_TIMER_SetMatch(LPC_TIMER32_1, 0, Hertz2Ticks(BCM_POLL_IDLE_FREQ)); Chip_TIMER_Reset(LPC_TIMER32_1); // Otherwise shit gets F****D break; case CHARGING: Chip_TIMER_SetMatch(LPC_TIMER32_1, 0, Hertz2Ticks(BCM_POLL_CHARGING_FREQ)); Chip_TIMER_Reset(LPC_TIMER32_1); break; case DRAINING: Chip_TIMER_SetMatch(LPC_TIMER32_1, 0, Hertz2Ticks(BCM_POLL_DRAINING_FREQ)); Chip_TIMER_Reset(LPC_TIMER32_1); break; } } //----------------------------- // Carry out out_state if (out_state.close_contactors && !Board_Contactors_Closed()) { Board_Close_Contactors(true); } else if (!out_state.close_contactors && Board_Contactors_Closed()) { Board_Close_Contactors(false); } if (out_state.brusa_output) { brusa_control.clear_error = out_state.brusa_clear_latch; brusa_control.output_mVolts = out_state.brusa_mVolts; brusa_control.output_cAmps = out_state.brusa_cAmps; Chip_TIMER_Enable(LPC_TIMER32_0); } else { brusa_control.output_mVolts = 0; brusa_control.output_cAmps = 0; Chip_TIMER_Disable(LPC_TIMER32_0); } //----------------------------- // Retrieve available brusa messages int8_t tmp = MCP2515_GetFullReceiveBuffer(); int8_t res = 0; if (tmp == 2) { MCP2515_ReadBuffer(&mcp_msg_obj, 0); res = Brusa_Decode(&brusa_messages, &mcp_msg_obj); if (res == -1) { DEBUG_Println("Brusa Decode Error"); res = 0; } MCP2515_ReadBuffer(&mcp_msg_obj, 1); res = Brusa_Decode(&brusa_messages, &mcp_msg_obj); } else if (tmp == 0) { // Receive Buffer 0 Full MCP2515_ReadBuffer(&mcp_msg_obj, tmp); res = Brusa_Decode(&brusa_messages, &mcp_msg_obj); } else if (tmp == 1) { //Receive buffer 1 full MCP2515_ReadBuffer(&mcp_msg_obj, tmp); res = Brusa_Decode(&brusa_messages, &mcp_msg_obj); } if (res == -1) { DEBUG_Println("Brusa Decode Error"); res = 0; } //----------------------------- // Send brusa message if its time if (brusa_message_send) { brusa_message_send = false; Brusa_MakeCTL(&brusa_control, &mcp_msg_obj); MCP2515_LoadBuffer(0, &mcp_msg_obj); MCP2515_SendBuffer(0); } //----------------------------- // Check for and decode A123 Messages if (!RingBuffer_IsEmpty(&rx_buffer)) { CCAN_MSG_OBJ_T temp_msg; RingBuffer_Pop(&rx_buffer, &temp_msg); res = PackManager_Update(&temp_msg); if (new_std_msg_sent) { PackManager_Commit(&pack_state); new_std_msg_sent = false; } } if (res == -1) { DEBUG_Println("A123 Decode Error"); } //----------------------------- // Timed output if (msTicks - last_debug_message > TIMED_MESSAGE_DELAY) { message_count++; last_debug_message = msTicks; switch (message_count % 7) { case 0: if (out_state.balance) { itoa(mbb_cmd.balance_target_mVolts, str, 10); DEBUG_Print("Balancing to: "); DEBUG_Println(str); } else { DEBUG_Println("Not balancing"); } break; case 1: itoa(brusa_control.output_mVolts, str, 10); DEBUG_Print("Brusa out V: "); DEBUG_Println(str); break; case 2: itoa(brusa_control.output_cAmps, str, 10); DEBUG_Print("Brusa out C: "); DEBUG_Println(str); break; case 3: DEBUG_Print("Actual Out Voltage: "); itoa(brusa_actual_1.output_mVolts, str, 10); DEBUG_Println(str); break; case 4: DEBUG_Print("Actual Out Current: "); itoa(brusa_actual_1.output_cAmps, str, 10); DEBUG_Println(str); break; case 5: DEBUG_Print("Mode: "); DEBUG_Println((SSM_GetMode() == CHARGING) ? "Chrg":"Idle"); break; case 6: DEBUG_Print("Brusa Output: "); itoa(out_state.brusa_output, str, 2); DEBUG_Println(str); DEBUG_Print("\r\n"); break; } } } return 0; }
/** * @brief Main UART program body * @return Always returns -1 */ int main(void) { FlagStatus exitflag; uint8_t buffer[10]; int ret = 0; int len; SystemCoreClockUpdate(); Board_Init(); Board_UART_Init(LPC_UART); #if !((defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357) && UARTNum==3) || ((!(defined(CHIP_LPC43XX) && defined(BOARD_KEIL_MCB_18574357))) && UARTNum==0)) Chip_UART_Init(LPC_UART); Chip_UART_SetBaud(LPC_UART, 115200); Chip_UART_ConfigData(LPC_UART, UART_LCR_WLEN8 | UART_LCR_SBS_1BIT); /* Default 8-N-1 */ /* Enable UART Transmit */ Chip_UART_TXEnable(LPC_UART); #endif /* Reset FIFOs, Enable FIFOs and DMA mode in UART */ Chip_UART_SetupFIFOS(LPC_UART, (UART_FCR_FIFO_EN | UART_FCR_RX_RS | UART_FCR_TX_RS | UART_FCR_DMAMODE_SEL | UART_FCR_TRG_LEV0)); /* Enable UART End of Auto baudrate & Auto baudrate timeout interrupts */ Chip_UART_IntEnable(LPC_UART, (UART_IER_ABEOINT | UART_IER_ABTOINT)); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(UARTx_IRQn, 1); /* Enable Interrupt for UART0 channel */ NVIC_EnableIRQ(UARTx_IRQn); /* Send UART Autobaud completed message */ Chip_UART_SendBlocking(LPC_UART, uartABStart, sizeof(uartABStart)); /* ---------------------- Auto baud rate section ----------------------- */ /* Start auto baudrate mode */ Chip_UART_ABCmd(LPC_UART, UART_ACR_MODE0, true, ENABLE); /* Loop until auto baudrate mode complete */ while (Chip_UART_GetABEOStatus(LPC_UART) == RESET) {} /* Send UART Autobaud completed message */ Chip_UART_SendBlocking(LPC_UART, uartABComplete, sizeof(uartABComplete)); /* Disable UART Interrupt */ NVIC_DisableIRQ(UARTx_IRQn); /* Print welcome screen */ Print_Menu_Polling(); exitflag = RESET; /* Read some data from the buffer */ while (exitflag == RESET) { len = 0; while (len == 0) { len = Chip_UART_Read(LPC_UART, buffer, 1); } if (buffer[0] == 27) { /* ESC key, set exit flag */ Chip_UART_SendBlocking(LPC_UART, uartPolling_menu3, sizeof(uartPolling_menu3)); ret = -1; exitflag = SET; } else if (buffer[0] == 'c') { Chip_UART_SendBlocking(LPC_UART, uartPolling_menu4, sizeof(uartPolling_menu4)); len = 0; while (len == 0) { len = Chip_UART_Read(LPC_UART, buffer, sizeof(buffer)); if ((buffer[0] != '1') && (buffer[0] != '2') && (buffer[0] != '3')) { len = 0; } } switch (buffer[0]) { case '1': /* Polling Mode */ Chip_UART_SendBlocking(LPC_UART, uartPolling_menu5, sizeof(uartPolling_menu5)); break; case '2': /* Interrupt Mode */ ret = 2; /* Exitflag = SET; */ App_Interrupt_Test(); Print_Menu_Polling(); break; case '3': /* DMA mode */ ret = 3; App_DMA_Test(); Print_Menu_Polling(); break; } } } /* Wait for current transmission complete - THR must be empty */ while (Chip_UART_CheckBusy(LPC_UART) == SET) {} /* DeInitialize UART0 peripheral */ Chip_UART_DeInit(LPC_UART); return ret; }