Skip to content

Commit 18bd0a9

Browse files
committed
spi branch
1 parent d2df66d commit 18bd0a9

File tree

1 file changed

+122
-14
lines changed

1 file changed

+122
-14
lines changed

src/main.cpp

Lines changed: 122 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,138 @@
88
#include "gimbal_servos.h"
99
#include <Arduino.h>
1010

11+
// TODO - this should be included - don't know why I had to write it manually
12+
void SystemClock_Config(void) {
13+
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
14+
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
15+
16+
/** Supply configuration update enable
17+
*/
18+
HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
19+
20+
/** Configure the main internal regulator output voltage
21+
*/
22+
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
23+
24+
while (!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {
25+
}
26+
27+
/** Initializes the RCC Oscillators according to the specified parameters
28+
* in the RCC_OscInitTypeDef structure.
29+
*/
30+
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
31+
RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
32+
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
33+
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
34+
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
35+
RCC_OscInitStruct.PLL.PLLM = 4;
36+
RCC_OscInitStruct.PLL.PLLN = 24;
37+
RCC_OscInitStruct.PLL.PLLP = 2;
38+
RCC_OscInitStruct.PLL.PLLQ = 15;
39+
RCC_OscInitStruct.PLL.PLLR = 2;
40+
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
41+
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
42+
RCC_OscInitStruct.PLL.PLLFRACN = 0;
43+
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
44+
Error_Handler();
45+
}
46+
47+
/** Initializes the CPU, AHB and APB buses clocks
48+
*/
49+
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_D3PCLK1 | RCC_CLOCKTYPE_D1PCLK1;
50+
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
51+
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
52+
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
53+
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
54+
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
55+
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
56+
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
57+
58+
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) {
59+
Error_Handler();
60+
}
61+
62+
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
63+
64+
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_SPI123;
65+
PeriphClkInit.Spi123ClockSelection = RCC_SPI123CLKSOURCE_PLL;
66+
67+
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
68+
Error_Handler();
69+
}
70+
}
71+
1172
void ping(const char *args) {
1273
Router::println("pong");
1374
Router::print("args: ");
1475
Router::println(args == nullptr ? "null" : args);
1576
}
1677

1778
void setup() {
79+
// pulse the debug LED at boot
80+
pinMode(PA7, OUTPUT);
81+
digitalWrite(PA7, HIGH);
82+
delay(1000);
83+
digitalWrite(PA7, LOW);
84+
delay(1000);
85+
86+
pinMode(PC3_C, OUTPUT);
87+
digitalWrite(PC3_C, HIGH);
88+
delay(1);
89+
digitalWrite(PC3_C, LOW);
90+
delay(1);
91+
1892
delay(3000);
1993
SPI.begin(); // spi is a shared interface, so we always begin here
20-
Router::begin();
21-
Router::println("Controller started.");
22-
23-
Prop::begin();
24-
Mag::begin();
25-
GPS::begin();
26-
IMU::begin();
27-
GimbalServos::begin();
28-
TrajectoryLoader::begin();
29-
TrajectoryFollower::begin();
30-
31-
Router::add({ping, "ping"}); // example registration
32-
Router::add({Router::print_all_cmds, "help"});
94+
95+
#define SCK PB10
96+
#define MOSI PC3_C
97+
#define MISO PC2_C
98+
#define CS PB6 // Replace with your actual CS pin
99+
100+
SPIClass my_spi(MOSI, MISO, SCK);
101+
pinMode(CS, OUTPUT);
102+
digitalWrite(CS, HIGH); // Idle high
103+
104+
my_spi.begin();
105+
// my_spi.setHwCs(false); // Software CS
106+
// my_spi.setClockDivider(SPI_CLOCK_DIV64); // Adjust divider for your device
107+
// my_spi.setDataMode(SPI_MODE0); // CPOL=1, CPHA=1
108+
109+
uint8_t tx[2];
110+
uint8_t rx[2];
111+
112+
/* WHO_AM_I register read */
113+
tx[0] = 0x75 | 0x80; // Read bit set
114+
tx[1] = 0x00; // Dummy byte
115+
116+
digitalWrite(CS, LOW); // Start transaction
117+
SPISettings ss = SPISettings();
118+
my_spi.beginTransaction(ss);
119+
my_spi.transfer(tx, rx, 2);
120+
my_spi.endTransaction();
121+
digitalWrite(CS, HIGH); // End transaction
122+
123+
volatile uint8_t who_am_id = rx[1];
124+
125+
delay(10);
126+
delay(10);
127+
128+
// Router::begin();
129+
// Router::println("Controller started.");
130+
131+
// Prop::begin();
132+
// Mag::begin();
133+
// GPS::begin();
134+
// IMU::begin();
135+
// GimbalServos::begin();
136+
// TrajectoryLoader::begin();
137+
// TrajectoryFollower::begin();
138+
139+
// Router::add({ping, "ping"}); // example registration
140+
// Router::add({Router::print_all_cmds, "help"});
33141
}
34142

35143
void loop() {
36-
Router::run(); // loop only runs once, since there is an internal loop in Router::run()
144+
// Router::run(); // loop only runs once, since there is an internal loop in Router::run()
37145
}

0 commit comments

Comments
 (0)