/** * @brief SPI event handler, from SPI driver */ void spi_event_handler(spi_slave_evt_t evt) { switch (evt.evt_type) { case SPI_SLAVE_BUFFERS_SET_DONE: if (has_pending_tx) { NRF_GPIO->OUTCLR = (1 << PIN_RDYN); } has_pending_tx = false; break; case SPI_SLAVE_XFER_DONE: NRF_GPIO->OUTSET = (1 << PIN_RDYN); nrf_gpio_pin_set(1); nrf_gpio_pin_clear(1); /* handle incoming */ if (rx_buffer.buffer[SERIAL_LENGTH_POS] > 0) { if (fifo_push(&rx_fifo, &rx_buffer) == NRF_SUCCESS) { /* notify ACI handler */ async_event_t async_evt; async_evt.callback.generic = mesh_aci_command_check; async_evt.type = EVENT_TYPE_GENERIC; event_handler_push(&async_evt); } else { APP_ERROR_CHECK(NRF_ERROR_NO_MEM); } } if (fifo_is_empty(&tx_fifo)) { serial_state = SERIAL_STATE_IDLE; prepare_rx(); enable_pin_listener(true); } else if (fifo_is_full(&rx_fifo)) { prepare_rx(); serial_state = SERIAL_STATE_WAIT_FOR_QUEUE; } else { do_transmit(); } break; default: /* no implementation necessary */ break; } }
void inits( void) { cli(); USART_init(MYUBRR, TRUE); can_init(); spi_init_master(); fm_init(); timer0_init(); timer1_init(); //TODO: RUN timer3_init(); timer3_init(); prepare_rx(1, ID_steeringWheel, MASK_FRONT_MODULE, fm_msg_handler); printf("\r\nFront module initialized"); sei(); set_bit(DDRB, DDB6); clear_bit(PORTB, PB6); }
/***** main loop *****/ int main(){ unsigned char i; init_status = no_init; volatile U8 led_toggle_counter = 0; U8 reset_cause = MCUSR; MCUSR = 0x00; //red_LED_toggle(); //external crystal, no prescaler CLKPR = 0x80; CLKPR = 0x00; //prepare the incremental_in_value_packet incremental_in_value_packet.id = 0x07; incremental_in_value_packet.length = 6; for (i = 0; i < incremental_in_value_packet.length; i++) { incremental_in_value_packet.data[i] = 0; } //prepare the init_ack_async_packet init_ack_sync_packet.id = 0x02; init_ack_sync_packet.length = 1; init_ack_sync_packet.data[0] = 0x14; communication_init(); //EICRA = 0x0C; //INT1 pin triggers async interrupt on rising edge //EICRB = 0xAA; //INT7-4 pins trigger sync interrupt on falling edge //EIMSK = 0xF2; //INT7-4 and INT1 are enabled //global interrupt enable sei(); if (prepare_rx(1,0x01,0xFF,incoming_init)) red_LED_on(); //debug for incoming message //TWBR = reset_cause; update_DA_A(STOP_DA_VAL); update_DA_B(STOP_DA_VAL); update_DA_C(STOP_DA_VAL); DA_LDAC_high(); DA_LDAC_low(); green_LED_on(); while(1){ if (init_status == init_2){ init_status = running; prepare_rx(3, 0x05, 0xFF, incoming_stop); prepare_rx(4, 0x06, 0xFF, incoming_DA_out_value); prepare_rx(6, 0x0B, 0xFF, incoming_request_incremental_value); prepare_rx(7, 0x0E, 0xFF, incoming_sync_command); } if (init_status == running) { prepare_incremental_in_value_packet(); while(!can_tx(5, &incremental_in_value_packet)) {}; if ((++led_toggle_counter) == 0xFF) { yellow_LED_toggle(); led_toggle_counter = 0; } } } return 0; }
int main(void) { //USART init (mark) USART0_Init(MYUBRR0); fdevopen(uart_putc0, USART0_Receive); printf("Hello World!\n"); //SPI_MasterInit(); //SPI_config for spi adc module ??? (mark) DDR_SPI |= (1<<DD_MOSI) | (1<<DD_SCK) | (1<<DD_SS); DDR_SPI &= ~(1<<DD_MISO); PORT_SPI |= (1<<DD_SS); SPCR = (1<<SPIE) | (1<<SPE) | (1<<MSTR); //Can init with id 20. (mark) can_init(); CAN_packet p; p.id = ID_power_measure; p.length = 4; //Estop can message recv funcion setup -> id 1 prepare_rx(1, ID_e_stop, 0x7ff, receiveEStop); //prepare_rx(2, MOTOR_STATUS_ID, 0x7ff, receiveMotorStatus); //leds and e_stop relay DDRD |= (1<<LED1) | (1<<LED2) | (1<<LED3); DDRE |= (1<<E_STOP); PORTD &= ~((1<<LED1) | (1<<LED2) | (1<<LED3)); PORTE &= ~(1<<E_STOP); //ADC1 positive differential input, ADC0 negative differential input //ADMUX = (1<<REFS0) | (1<<REFS1) | 0x10; //Enable, start conversion, interrupt enable, prescaler 64 (8MHz/64 = 125KHz) //ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1); TCCR1B |= (1<<WGM12) | (1<<CS12); //CTC mode, prescaler 256 TIMSK1 |= (1<<OCIE1A); //compare match A interrupt OCR1A = 31249; PORT_SPI &= ~(1<<DD_SS); //initate new reading SPDR = 0x06; //single ended spi_seq = 1; spi_ch = 0; sei(); uint32_t temp_voltage, temp_current_motor; uint16_t temp_counter; for (unsigned char i = 0; i < p.length; i++) { p.data[i] = i; } while(1) { if (timer_done == true) { timer_done = false; //heartbeat (mark) PORTD ^= (1<<LED1); //saving sampled data (mark) cli(); temp_voltage = voltage_ADC_sum; temp_current_motor = current_motor_ADC_sum; temp_counter = counter; voltage_ADC_sum = 0; current_motor_ADC_sum = 0; counter = 0; sei(); //Average and scale data (mark) int voltage = temp_voltage*100/temp_counter/VOLTAGE_SCALE; //TODO chage to uint32_t ?? uint32_t currentM = ((uint32_t)(temp_current_motor/temp_counter) - CURRENT_MOTOR_ZERO + 5)/CURRENT_MOTOR_SCALE ; //What is 455??? (mark) if (voltage > 455) { PORTD |= (1<<LED3); } else { PORTD &= ~(1<<LED3); } //Preapere can message (mark) p.data[0] = voltage>>8; p.data[1] = voltage; p.data[2] = currentM>>8; p.data[3] = currentM; //For debugging printf("Average over %d samples:\n\r", temp_counter); printf("Voltage: %d\n\r", voltage); printf("Current motor: %lu\n\r", currentM); //printf("Current without scaling: %lu\n\r", (uint32_t)(temp_current_motor/temp_counter)); //printf("calc: (%u/%d - %d + 5)/%d*100)\n\r", temp_current_motor, temp_counter, CURRENT_MOTOR_ZERO, CURRENT_MOTOR_SCALE); //printf("Current one: %lu\n\r", current_motor_one_value); //printf("CAn packet: %s\n", str); can_tx(14, &p); PORTD ^= (1<<LED1); //heartbeat (mark) } }