Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fw 999 103 hw rodney #297

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions projects/fw103/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# fw103
6 changes: 6 additions & 0 deletions projects/fw103/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"libs": [
"FreeRTOS",
"ms-common"
]
}
45 changes: 45 additions & 0 deletions projects/fw103/inc/ads1115.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#pragma once

#include "gpio.h"
#include "i2c.h"
#include "tasks.h"

#define ALRT_EVENT 0

#define ADS1115_I2C_PORT 0

typedef enum {
ADS1115_ADDR_GND = 0x48, // 0b1001000
ADS1115_ADDR_VDD = 0x49, // 0b1001001
ADS1115_ADDR_SDA = 0x4A, // 0b1001010
ADS1115_ADDR_SCL = 0x4B, // 0b1001011
} ADS1115_Address;

typedef enum {
ADS1115_CHANNEL_0 = 0,
ADS1115_CHANNEL_1,
ADS1115_CHANNEL_2,
ADS1115_CHANNEL_3,
} ADS1115_Channel;

typedef enum {
ADS1115_REG_CONVERSION = 0x00,
ADS1115_REG_CONFIG,
ADS1115_REG_LO_THRESH,
ADS1115_REG_HI_THRESH,
} ADS1115_Reg;

typedef struct ADS1115_Config {
I2CPort i2c_port;
I2CAddress i2c_addr;
Task *handler_task;
GpioAddress *ready_pin;
} ADS1115_Config;

StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAddress *ready_pin);

StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channel);

StatusCode ads1115_read_raw(ADS1115_Config *config, ADS1115_Channel channel, int16_t *reading);

StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading);
64 changes: 64 additions & 0 deletions projects/fw103/src/ads1115.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "ads1115.h"

#include "gpio_it.h"
#include "i2c.h"
#include "status.h"
#include "log.h"

StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAddress *ready_pin) {
LOG_DEBUG("Entered ads1115 init\n");
config->i2c_addr = i2c_addr;

uint16_t cmd;

// Write Config register
/* TODO: fill out this value */
cmd = 0x0483;
i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2);

/* TODO (optional) */
// Set low thresh to zero
// cmd = 0x0000;
// i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_LO_THRESH, (uint8_t *)(&cmd), 2);

/* TODO (optional) */
// Set high thresh to 1V
// cmd = 0x7C1El;
// i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_HI_THRESH, (uint8_t *)(&cmd), 2);

// Register the ALRT pin
/* TODO (optional) */

LOG_DEBUG("Exited ads1115 init\n");
return STATUS_CODE_OK;
}

StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channel) {

uint16_t cmd;

// Write Config register
cmd = 0x0000;
i2c_write_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONFIG, (uint8_t *)(&cmd), 2);
return STATUS_CODE_OK;
}

StatusCode ads1115_read_raw(ADS1115_Config *config, ADS1115_Channel channel, int16_t *reading) {

// Read 2 bytes from the ADS1115 conversion register
StatusCode status = i2c_read_reg(config->i2c_port, config->i2c_addr, ADS1115_REG_CONVERSION, (uint8_t *)reading, 2);

return STATUS_CODE_OK; // Return OK if everything is successful
}

StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading) {

int16_t raw_adc_value;

ads1115_read_raw(config, channel, &raw_adc_value);

// Convert raw ADC value to voltage (assuming range of 0 to 2.048V)
*reading = (float)raw_adc_value / 65535.0f * 2.048f;

return STATUS_CODE_OK; // Return OK if conversion is successful
}
65 changes: 65 additions & 0 deletions projects/fw103/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Project Code for FW 103

Assignment: Create an ADC driver to interface with the ADS1115 Multi-Channel ADC IC.

Requirements:
- Implement the ADC driver functions (set config, select and read from a channel)
- ADC task to periodically measure the voltage of channel 0
- Overvoltage interrupt (configure the interrupt to be on channel 0 with thresholds of 0V - 1V)
*/

#include "FreeRTOS.h"
#include <stdio.h>
#include "ads1115.h"
#include "log.h"
#include "tasks.h"
#include "gpio.h"
#include "delay.h"


TASK(task1, TASK_STACK_512) {
LOG_DEBUG("Task 1 initialized!\n");
GpioAddress ready_pin = {
.port = GPIO_PORT_B,
.pin = GPIO_Pin_0,
};

ADS1115_Config config = {
.handler_task = task1,
.i2c_addr = ADS1115_ADDR_GND,
.i2c_port = ADS1115_I2C_PORT,
.ready_pin = &ready_pin,
};

// Initialize the ADS1115
ads1115_init(&config, ADS1115_ADDR_GND, &ready_pin);

float voltage;

while (true) {
LOG_DEBUG("Entered While Loop\n");

ads1115_read_converted(&config, ADS1115_CHANNEL_0, &voltage);

LOG_DEBUG("Channel 0 Voltage: %.3f V\n", voltage);

delay_ms(1000);
}
}

int main() {
tasks_init();
log_init();
gpio_init();
LOG_DEBUG("Welcome to FW 103!\n");


tasks_init_task(task1, TASK_PRIORITY(2), NULL);
LOG_DEBUG("task init\n");

tasks_start();

LOG_DEBUG("exiting main?\n");
return 0;
}
15 changes: 15 additions & 0 deletions projects/hello_world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# hello_world
7 changes: 7 additions & 0 deletions projects/hello_world/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"libs": [
"FreeRTOS",
"ms-common",
"master"
]
}
41 changes: 41 additions & 0 deletions projects/hello_world/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

// #include <stdio.h>

#include "log.h"
// #include "master_task.h"
// #include "tasks.h"

// void pre_loop_init() {}

// void run_fast_cycle() {}

// void run_medium_cycle() {}

// void run_slow_cycle() {}

void increment(int *input) {
(*input)++;
}


int main() {

// tasks_init();
// log_init();
// LOG_DEBUG("Welcome to TEST!");

// init_master_task();

// tasks_start();

int my_int = 0;

LOG_DEBUG("Before entering main loop");

while(true){
increment(&my_int);
LOG_DEBUG("Hello World %d\n", my_int);
}

return 0;
}
15 changes: 15 additions & 0 deletions projects/task1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!--
General guidelines
These are just guidelines, not strict rules - document however seems best.
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions:
- What is it?
- What problem does it solve?
- How do I use it? (with usage examples / example commands, etc)
- How does it work? (architectural overview)
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions:
- What is the purpose of the board?
- What are all the things that the firmware needs to do?
- How does it fit into the overall system?
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware)
-->
# task1
6 changes: 6 additions & 0 deletions projects/task1/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"libs": [
"FreeRTOS",
"ms-common"
]
}
47 changes: 47 additions & 0 deletions projects/task1/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdbool.h>
#include <stdint.h>

#include "FreeRTOS.h"
#include "tasks.h"

#include "gpio.h"
#include "log.h"
#include "misc.h"
#include "delay.h"

// Non blocking delay. Simply consumes cpu cycles until a given time has passed
static void prv_delay(const TickType_t delay_ms) {
vTaskDelay(pdMS_TO_TICKS(delay_ms));
}

TASK(task1, TASK_STACK_512) {
int counter1 = 0;
while (true) {
LOG_DEBUG("Task1 %d\n", counter1);
counter1++;
prv_delay(1000);
}
}

TASK(task2, TASK_STACK_512) {
int counter2 = 0;
while (true) {
LOG_DEBUG("Task2 %d\n", counter2);
counter2++;
prv_delay(1000);
}
}

int main(void) {
log_init();
tasks_init();

LOG_DEBUG("Program start...\n");

tasks_init_task(task1, TASK_PRIORITY(2), NULL);
tasks_init_task(task2, TASK_PRIORITY(3), NULL);

tasks_start();

return 0;
}
Loading
Loading