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 fw 103 hw emmaxing #330

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 13 deletions libraries/ms-common/src/x86/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,12 @@ StatusCode i2c_write(I2CPort i2c, I2CAddress addr, uint8_t *tx_data, size_t tx_l

StatusCode i2c_read_reg(I2CPort i2c, I2CAddress addr, uint8_t reg, uint8_t *rx_data,
size_t rx_len) {
if (i2c >= NUM_I2C_PORTS) {
return status_msg(STATUS_CODE_INVALID_ARGS, "Invalid I2C port.");
}
uint8_t reg_to_read = reg;
status_ok_or_return(i2c_write(i2c, addr, &reg_to_read, sizeof(reg)));
status_ok_or_return(i2c_read(i2c, addr, rx_data, rx_len));
rx_data[0] = 0x40;
rx_data[1] = 0x50;
return STATUS_CODE_OK;
}

StatusCode i2c_write_reg(I2CPort i2c, I2CAddress addr, uint8_t reg, uint8_t *tx_data,
size_t tx_len) {
if (i2c >= NUM_I2C_PORTS) {
return status_msg(STATUS_CODE_INVALID_ARGS, "Invalid I2C port.");
}

uint8_t reg_to_write = reg;
status_ok_or_return(i2c_write(i2c, addr, &reg_to_write, sizeof(reg)));
status_ok_or_return(i2c_write(i2c, addr, tx_data, tx_len));
return STATUS_CODE_OK;
}
15 changes: 15 additions & 0 deletions projects/fw_103/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)
-->
# fw_103
6 changes: 6 additions & 0 deletions projects/fw_103/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/fw_103/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, uint16_t *reading);

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

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

StatusCode ads1115_init(ADS1115_Config *config, ADS1115_Address i2c_addr, GpioAddress *ready_pin) {
if (config == NULL) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

config->i2c_addr = i2c_addr;

uint16_t cmd;

// Write Config register
/* TODO: fill out this value */
cmd = 0x0000;
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 = 0x0000;
i2c_write_reg(config->i2c_port, i2c_addr, ADS1115_REG_LO_THRESH, (uint8_t *)(&cmd), 2);

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

return STATUS_CODE_OK;
}

StatusCode ads1115_select_channel(ADS1115_Config *config, ADS1115_Channel channel) {
if (config == NULL) {
return status_code(STATUS_CODE_INVALID_ARGS);
}

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, uint16_t *reading) {
/* TODO: complete function */
return STATUS_CODE_OK;
}

StatusCode ads1115_read_converted(ADS1115_Config *config, ADS1115_Channel channel, float *reading) {
/* TODO: complete function */
return STATUS_CODE_OK;
}
27 changes: 27 additions & 0 deletions projects/fw_103/src/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
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 <stdio.h>

#include "log.h"
#include "tasks.h"

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

tasks_start();

LOG_DEBUG("exiting main?\n");
return 0;
}
Loading