// // RL_Init: C // // Initialize the REBOL interpreter. // // Returns: // Zero on success, otherwise an error indicating that the // host library is not compatible with this release. // Arguments: // rargs - REBOL command line args and options structure. // See the host-args.c module for details. // lib - the host lib (OS_ functions) to be used by REBOL. // See host-lib.c for details. // Notes: // This function will allocate and initialize all memory // structures used by the REBOL interpreter. This is an // extensive process that takes time. // RL_API int RL_Init(REBARGS *rargs, void *lib) { int marker; REBUPT bounds; const char *env_legacy = NULL; Host_Lib = cast(REBOL_HOST_LIB *, lib); if (Host_Lib->size < HOST_LIB_SIZE) return 1; if (((HOST_LIB_VER << 16) + HOST_LIB_SUM) != Host_Lib->ver_sum) return 2; bounds = (REBUPT)OS_CONFIG(1, 0); if (bounds == 0) bounds = (REBUPT)STACK_BOUNDS; #ifdef OS_STACK_GROWS_UP Stack_Limit = (REBUPT)(&marker) + bounds; #else if (bounds > (REBUPT) &marker) Stack_Limit = 100; else Stack_Limit = (REBUPT)&marker - bounds; #endif Init_Core(rargs); GC_Active = TRUE; // Turn on GC if (rargs->options & RO_TRACE) { Trace_Level = 9999; Trace_Flags = 1; } return 0; }
*/ RL_API int RL_Init(REBARGS *rargs, void *lib) /* ** Initialize the REBOL interpreter. ** ** Returns: ** Zero on success, otherwise an error indicating that the ** host library is not compatible with this release. ** Arguments: ** rargs - REBOL command line args and options structure. ** See the host-args.c module for details. ** lib - the host lib (OS_ functions) to be used by REBOL. ** See host-lib.c for details. ** Notes: ** This function will allocate and initialize all memory ** structures used by the REBOL interpreter. This is an ** extensive process that takes time. ** ***********************************************************************/ { int marker; REBUPT bounds; const char *env_legacy = NULL; Host_Lib = cast(REBOL_HOST_LIB *, lib); if (Host_Lib->size < HOST_LIB_SIZE) return 1; if (((HOST_LIB_VER << 16) + HOST_LIB_SUM) != Host_Lib->ver_sum) return 2; bounds = (REBUPT)OS_CONFIG(1, 0); if (bounds == 0) bounds = (REBUPT)STACK_BOUNDS; #ifdef OS_STACK_GROWS_UP Stack_Limit = (REBUPT)(&marker) + bounds; #else if (bounds > (REBUPT) &marker) Stack_Limit = 100; else Stack_Limit = (REBUPT)&marker - bounds; #endif Init_Core(rargs); GC_Active = TRUE; // Turn on GC if (rargs->options & RO_TRACE) { Trace_Level = 9999; Trace_Flags = 1; } return 0; }
/** * @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; }