main.c 1.78 KB
Newer Older
Jake Read's avatar
Jake Read committed
1
2
3
4
5
6
7
8
9
/*
 * mkbldcdriver-v03.c
 *
 * Created: 1/14/2018 8:20:39 PM
 * Author : Jake
 */ 


#include "sam.h"
10
11
12
#include "pwm_foc.h"
#include "pin.h"
#include "hardware.h"
Jake Read's avatar
Jake Read committed
13
14
15
16
#include <stdio.h>

// stlb is PA23

Jake Read's avatar
Jake Read committed
17
18
19
20
21
22
int main(void)
{
    /* Initialize the SAM system */
    SystemInit();
	SysTick_Config(5000000);
	
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	// LED
	stlb = pin_new(&PORT->Group[0], 23);
	pin_output(&stlb);
	
	// interrupts
	__enable_irq();
	NVIC_EnableIRQ(SERCOM4_0_IRQn); //up1tx
	NVIC_EnableIRQ(SERCOM4_2_IRQn); //up1rx
	NVIC_EnableIRQ(SERCOM5_0_IRQn);
	NVIC_EnableIRQ(SERCOM5_2_IRQn);
	
	// Rinbuffers for UARTs
	rb_init(&up1_rbrx);
	rb_init(&up1_rbtx);
	rb_init(&up1_rbrx);
	rb_init(&up1_rbtx);
	
	// UARTS 
	// UP1:
	// SERCOM4 | RX: PB13 | TX: PB12
	// UP2:
	// SERCOM5 | RX: PB3 | TX: PB2
	up1 = uart_new(SERCOM4, &PORT->Group[1], &up1_rbrx, &up1_rbtx, 12, 13, HARDWARE_IS_APBD, HARDWARE_ON_PERIPHERAL_C); 
	MCLK->APBDMASK.reg |= MCLK_APBDMASK_SERCOM4;
	uart_init(&up1, 6, SERCOM4_GCLK_ID_CORE, 63018); // baud: 45402 for 921600, 63018 for 115200
	up2 = uart_new(SERCOM5, &PORT->Group[1], &up2_rbrx, &up2_rbtx, 3, 2, HARDWARE_IS_APBD, HARDWARE_ON_PERIPHERAL_D);
	MCLK->APBDMASK.reg |= MCLK_APBDMASK_SERCOM5;
	uart_init(&up2, 7, SERCOM5_GCLK_ID_CORE, 63018);
		
	// check all PWM lines
	// pins for enable, fault, pwmmode, etc
Jake Read's avatar
Jake Read committed
54
	
55
	// integrate SPI, test AS5147, library
Jake Read's avatar
Jake Read committed
56
	
57
58
	// do closed loop sinucommutate, following old ATSAMS70 code
	// do BLDC closed, open loop
Jake Read's avatar
Jake Read committed
59
	
60
	pwmsetup_foc();
Jake Read's avatar
Jake Read committed
61
	
Jake Read's avatar
Jake Read committed
62
63
    while (1) 
    {
64
		pwmupdate_foc(126,126,126);
Jake Read's avatar
Jake Read committed
65
66
67
68
    }
}

void SysTick_Handler(void){
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	pin_toggle(&stlb);
	uart_sendchar_buffered(&up1, 125);
}

void SERCOM4_0_Handler(void){
	uart_txhandler(&up1);
}

void SERCOM4_2_Handler(void){
	uart_rxhandler(&up1);
}

void SERCOM5_0_Handler(void){
	uart_txhandler(&up2);
}

void SERCOM5_2_Handler(void){
	uart_rxhandler(&up2);
}