void AVALON_PWM_Init(void) { /* System CLK 48MHz */ Chip_TIMER_Init(LPC_TIMER16_0); Chip_TIMER_Disable(LPC_TIMER16_0); /* CT16B0_MAT0/CT16B0_MAT1/CT16B0_MAT2 Init */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 8, IOCON_FUNC2 | IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 9, IOCON_FUNC2 | IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 15, IOCON_FUNC2 | IOCON_MODE_INACT); /* CT16B0_MAT0 duty:50% */ Chip_TIMER_ExtMatchControlSet(LPC_TIMER16_0, 1, TIMER_EXTMATCH_TOGGLE, 0); Chip_TIMER_SetMatch(LPC_TIMER16_0, 0, DUTY_50); /* CT16B0_MAT1 duty:25% */ Chip_TIMER_ExtMatchControlSet(LPC_TIMER16_0, 1, TIMER_EXTMATCH_TOGGLE, 1); Chip_TIMER_SetMatch(LPC_TIMER16_0, 1, DUTY_25); /* CT16B0_MAT2 duty:10% */ Chip_TIMER_ExtMatchControlSet(LPC_TIMER16_0, 1, TIMER_EXTMATCH_TOGGLE, 2); Chip_TIMER_SetMatch(LPC_TIMER16_0, 2, DUTY_10); /* Prescale 0 */ Chip_TIMER_PrescaleSet(LPC_TIMER16_0, 0); /* PWM Period 800Hz */ Chip_TIMER_SetMatch(LPC_TIMER16_0, 3, DUTY_100); Chip_TIMER_ResetOnMatchEnable(LPC_TIMER16_0, 3); /* CT16B0_MAT0/CT16B0_MAT1/CT16B0_MAT2 Enable */ LPC_TIMER16_0->PWMC = 0x7;//pwm Chip_TIMER_Enable(LPC_TIMER16_0); }
uint32_t PWMSetPeriod(uint8_t channel, uint32_t period) { if (channel > CHANNEL_C_TIMER_INDEX) { return 1; } if (eDVSMode != EDVS_MODE_INTERNAL && channel == 0) { return 1; // channel 0 taken for master/slave mode } LPC_TIMER_T * timer = halTimers[channel].timer; halTimers[channel].period = period; /** * If the period equal 0, the timer is disable and its outputs are set as GPIO and driven low. */ if (period == 0) { Chip_TIMER_DeInit(timer); //Stop the timer Chip_TIMER_SetMatch(timer, 2, 0); halTimers[channel].enabled[0] = DISABLE; halTimers[channel].enabled[1] = DISABLE; Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, halTimers[channel].portGpio[0], halTimers[channel].pinGpio[0]); Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, halTimers[channel].portGpio[1], halTimers[channel].pinGpio[1]); Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, halTimers[channel].portGpio[0], halTimers[channel].pinGpio[0]); Chip_GPIO_SetPinOutLow(LPC_GPIO_PORT, halTimers[channel].portGpio[1], halTimers[channel].pinGpio[1]); Chip_SCU_PinMuxSet(halTimers[channel].port[0], halTimers[channel].pin[0], halTimers[channel].gpioMode[0]); Chip_SCU_PinMuxSet(halTimers[channel].port[1], halTimers[channel].pin[1], halTimers[channel].gpioMode[1]); } else { /** * The channel match 2 is used as the controller of the base frequency. * When there is a match on this channel, the timer is reset and the external match bit * is set to 1. * The M0 core is looking for this change and it sets the output of the channels to high. */ Chip_TIMER_Init(timer); Chip_TIMER_Disable(timer); Chip_TIMER_Reset(timer); /** * The Main clock is running at 192Mhz so set the Prescaler in order to have * a 1 Mhz timer. Timer_CLK = Main_CLK/ (PR+1) */ Chip_TIMER_PrescaleSet(timer, 191); Chip_TIMER_ResetOnMatchEnable(timer, 2); Chip_TIMER_StopOnMatchDisable(timer, 2); Chip_TIMER_MatchDisableInt(timer, 2); Chip_TIMER_SetMatch(timer, 2, period); //Reconfigure match channels! if (halTimers[channel].enabled[0]) { PWMSetWidth(channel, 0, halTimers[channel].witdh[0]); } if (halTimers[channel].enabled[1]) { PWMSetWidth(channel, 1, halTimers[channel].witdh[1]); } Chip_TIMER_ExtMatchControlSet(timer, 0, TIMER_EXTMATCH_SET, 2); // Clear interrupt pending timer->IR = 0xFFFFFFFF; Chip_TIMER_Enable(timer); } return 0; }
void AVALON_PWM_Test(void) { static Bool bPwmInit = FALSE; if(!bPwmInit) { bPwmInit = TRUE; AVALON_PWM_Init(); } Chip_TIMER_Enable(LPC_TIMER16_0); AVALON_Delay(9000000); Chip_TIMER_Disable(LPC_TIMER16_0); }
void Board_TIMER_Init(void) { int i; for(i=0; i<4;i++) { Chip_TIMER_Init(getTimerFomIndex(i)); Chip_TIMER_Disable(getTimerFomIndex(i)); timersInfo[i].callback = NULL; } NVIC_EnableIRQ(TIMER0_IRQn); NVIC_EnableIRQ(TIMER1_IRQn); NVIC_EnableIRQ(TIMER2_IRQn); NVIC_EnableIRQ(TIMER3_IRQn); }
void AVALON_PWM_Init(void) { /* System CLK 48MHz */ Chip_TIMER_Init(LPC_TIMER16_0); Chip_TIMER_Disable(LPC_TIMER16_0); /* CT16B0_MAT0/CT16B0_MAT1/CT16B0_MAT2 Init */ Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 8, IOCON_FUNC2 | IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 0, 9, IOCON_FUNC2 | IOCON_MODE_INACT); Chip_IOCON_PinMuxSet(LPC_IOCON, 1, 15, IOCON_FUNC2 | IOCON_MODE_INACT); /* CT16B0_MAT0 duty:50% */ Chip_TIMER_ExtMatchControlSet(LPC_TIMER16_0, 1, TIMER_EXTMATCH_SET, 0); Chip_TIMER_SetMatch(LPC_TIMER16_0, 0, DUTY_0); /* CT16B0_MAT1 duty:25% */ Chip_TIMER_ExtMatchControlSet(LPC_TIMER16_0, 1, TIMER_EXTMATCH_SET, 1); Chip_TIMER_SetMatch(LPC_TIMER16_0, 1, DUTY_0); /* CT16B0_MAT2 duty:10% */ Chip_TIMER_ExtMatchControlSet(LPC_TIMER16_0, 1, TIMER_EXTMATCH_SET, 2); Chip_TIMER_SetMatch(LPC_TIMER16_0, 2, DUTY_0); }
void Board_TIMER_DisableTimer(uint8_t timerNum) { Chip_TIMER_Disable(getTimerFomIndex(timerNum)); }
/** * @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; }
void AVALON_PWM_Disable(void) { Chip_TIMER_Disable(LPC_TIMER16_0); }
/* * @Brief Disables timer peripheral * @param timerNumber: Timer number, 0 to 3 * @return nothing */ void Timer_DeInit(uint8_t timerNumber){ NVIC_DisableIRQ(timer_sd[timerNumber].IRQn); Chip_TIMER_Disable(timer_sd[timerNumber].name); Chip_TIMER_DeInit(timer_sd[timerNumber].name); }