aboutsummaryrefslogtreecommitdiff
path: root/src/blinky.c
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2024-11-13 23:05:32 -0700
committerJosh Rahm <joshuarahm@gmail.com>2024-11-13 23:05:32 -0700
commitc9402e5a5d67ef877fa7f5f67c07a794574ded35 (patch)
treeaad50c7d861f3f4c68d985abe1b8fce79d10bc86 /src/blinky.c
parentda45a7210ef634fcdb0270bffc90d4da97c61230 (diff)
downloadch573-c9402e5a5d67ef877fa7f5f67c07a794574ded35.tar.gz
ch573-c9402e5a5d67ef877fa7f5f67c07a794574ded35.tar.bz2
ch573-c9402e5a5d67ef877fa7f5f67c07a794574ded35.zip
Added a whole bunch of fiddle files. Started improving boot process.
It still works.
Diffstat (limited to 'src/blinky.c')
-rw-r--r--src/blinky.c134
1 files changed, 77 insertions, 57 deletions
diff --git a/src/blinky.c b/src/blinky.c
index 7e46389..d8a29bd 100644
--- a/src/blinky.c
+++ b/src/blinky.c
@@ -1,5 +1,12 @@
#include <stdint.h>
+#include "ch573/gpio.h"
+
+#define GPIO_PORT_A ch573_gpio__gpio_port_a
+#define GPIO_PORT CH573_GPIO__GPIO_PORT_T_INTF
+
+static void start(void);
+
/*
* Function (or really pointer to code) for the reset interrupt handler.
*/
@@ -13,62 +20,15 @@ void delay(void);
/* Type def to a void function to make things mor ereadable. */
typedef void (*isr_routine)(void);
-/* Gpio configuration structure. This is exactly how it is laid out in
- * memory. In these registers bit X referes to pin X. */
-typedef struct {
- /* The direction of the gpio pin. 1 = output, 0 = input. */
- uint32_t dir;
-
- /* The value of the gpio pin (for input). */
- uint32_t in;
-
- /* Write to set the output value for the pin. */
- uint32_t out;
-
- /* The clear value. Resets the pin to what it is at reset. */
- uint32_t clr;
-
- /* Sets the pin to be pull-up on logical 1 in input mode. */
- uint32_t pu;
-
- /* Sets whether the pin should be pull-down (open-drain) for logical 0 or
- * drive for logical 1. 1 = pull-down, 0 = drive. */
- uint32_t pd_drv;
-} gpio_t;
-
/** The ISR Vector structure. This is linked to starting at address 0. */
-__attribute((__section__(".isr_vector"))) volatile struct {
- // What NULL points to. nothing useful.
- uint32_t reserved__;
- // Called when the device boots or reset is pressed.
- isr_routine reset_cb;
- isr_routine nmi_cb;
- isr_routine exc_cb;
-} isr_vectors = {.reset_cb = on_reset};
-
-// GPIO configuration registers. These are defined in the linker script.
-extern volatile gpio_t gpio_a;
-extern volatile gpio_t gpio_b;
-
-/* Main routine. This is called on_reset once everything else has been set up.
- */
-static void start(void)
-{
- uint32_t bit = 1 << 8;
- gpio_a.dir |= bit; // Set to "out"
- gpio_a.pd_drv |= bit; // Set to "open drain"
-
- for (;;) {
- gpio_a.out &= ~bit; // Pin low (turns on LED)
- delay();
- delay();
- delay();
- gpio_a.out |= bit; // Pin high (turns off LED)
- delay();
- delay();
- delay();
- }
-}
+// __attribute((__section__(".isr_vector"))) volatile struct {
+// // What NULL points to. nothing useful.
+// uint32_t reserved__;
+// // Called when the device boots or reset is pressed.
+// isr_routine reset_cb;
+// isr_routine nmi_cb;
+// isr_routine exc_cb;
+// } isr_vectors = {.reset_cb = on_reset};
/*
* The reset callback.This has to be a naked function because the stack pointer
@@ -87,8 +47,68 @@ __attribute((naked)) void on_reset(void)
: "r"(start));
}
+uint32_t collatz(uint32_t n)
+{
+ uint32_t c = 0;
+
+ while (n > 1) {
+ if (n % 2 == 0) {
+ n /= 2;
+ } else {
+ n = n * 3 + 1;
+ }
+ c++;
+ }
+
+ return c;
+}
+
+void blink_n(int n)
+{
+ uint32_t bit = 1 << 8;
+ while (n > 0) {
+ GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8);
+ delay();
+ GPIO_PORT.out.set(GPIO_PORT_A, ON, 8);
+ delay();
+ --n;
+ }
+}
+
void delay(void)
{
- for (volatile uint32_t i = 0; i < 10000; ++i)
- ;
+ for (volatile uint32_t i = 0; i < 10000; ++i);
+}
+
+/* Main routine. This is called on_reset once everything else has been set up.
+ */
+static void start(void)
+{
+ GPIO_PORT.dir.set(GPIO_PORT_A, DIR_OUT, 8);
+ GPIO_PORT.pd_drv.set(GPIO_PORT_A, PD_DRV_OPEN_DRAIN, 8);
+
+ for (;;) {
+ GPIO_PORT.out.set(GPIO_PORT_A, ON, 8);
+ delay();
+ delay();
+ delay();
+ GPIO_PORT.out.set(GPIO_PORT_A, OFF, 8);
+ delay();
+ }
+
+ // uint32_t i;
+ // for (i = 1;; ++i) {
+ // uint32_t c = collatz(i);
+ // blink_n(c);
+
+ // delay();
+ // delay();
+ // delay();
+ // delay();
+ // delay();
+ // delay();
+ // delay();
+ // delay();
+ // delay();
+ // }
}