Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e79b448

Browse files
changed volatile uint8_t button_flag to be a semaphore! works perfectly.
1 parent 9ee89e6 commit e79b448

File tree

11 files changed

+120
-106
lines changed

11 files changed

+120
-106
lines changed

‎.vscode/settings.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"files.associations": {
3-
"freertos.h": "c"
3+
"freertos.h": "c",
4+
"main.h": "c"
45
}
56
}

‎FreeRTOSConfig.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
#define configUSE_IDLE_HOOK 0
5858
#define configUSE_TICK_HOOK 0
5959
#define configCPU_CLOCK_HZ ( 48000000U )
60-
#define configTICK_RATE_HZ ( ( TickType_t ) 10 )
60+
#define configTICK_RATE_HZ ( ( TickType_t ) 100 )
6161
#define configMAX_PRIORITIES ( 7 )
6262
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 512 )
6363
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 20 * 1024 ) )
@@ -69,7 +69,7 @@
6969
#define configQUEUE_REGISTRY_SIZE 8
7070
#define configCHECK_FOR_STACK_OVERFLOW 2
7171
#define configUSE_RECURSIVE_MUTEXES 1
72-
#define configUSE_MALLOC_FAILED_HOOK 0
72+
#define configUSE_MALLOC_FAILED_HOOK 1
7373
#define configUSE_APPLICATION_TASK_TAG 0
7474
#define configUSE_COUNTING_SEMAPHORES 1
7575
#define configGENERATE_RUN_TIME_STATS 0

‎include/dma.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Author: Gabriel Rodgers
3+
Date: 1/9/2025
4+
*/
5+
6+
#ifndef DMA_H
7+
#define DMA_H
8+
9+
#define DMA1 0x40003800
10+
11+
#define DMA1_CR1 (*((volatile uint32_t *) (SPI2)))
12+
13+
void dma1_ch5_init();
14+
15+
#endif /* DMA_H */

‎include/i2c.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ Date: 12/30/2024
3232
#define I2C2_TIMING_VALS ((0x1<<28)|(0x3<<20)|(0x2<<16)|(0x3<<8)|(0x9))
3333

3434
typedef enum{
35+
I2C2_START_POLL,
3536
I2C2_POST_WRITE,
3637
I2C2_POST_READ,
3738
I2C2_RECEIVING,
38-
I2C2_POST_RECEIVE,
39-
I2C2_POST_DISPLAY
39+
I2C2_POST_RECEIVE
4040
} i2c2_stages_t;
4141

4242
void i2c2_init(void);
43-
void i2c2_write(const uint8_t NBYTES, const uint16_t w_buffer);
44-
void i2c2_read(const uint8_t NBYTES);
43+
uint8_t i2c2_write(const uint8_t NBYTES, const uint16_t w_buffer);
44+
uint8_t i2c2_read(const uint8_t NBYTES);
4545
uint8_t i2c2_check_bus(void);
4646

4747
#endif /* I2C_H */

‎include/lcd.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ Author: Gabriel Rodgers
33
Date: 12/27/2024
44
*/
55

6-
#ifndef LCD_H_
7-
#define LCD_H_
6+
#ifndef LCD_H
7+
#define LCD_H
88

99
#define BITMAP_WIDTH 5
1010

@@ -51,4 +51,4 @@ void lcd_clear(void);
5151
void lcd_output_text(lcd_text_buffer_t const buf);
5252
//void lcd_output_pixels(lcd_pixel_buffer_t const buf);
5353

54-
#endif /* LCD_H_ */
54+
#endif /* LCD_H */

‎src/dma.c‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
Author: Gabriel Rodgers
3+
Date: 1/9/2025
4+
Purpose: Configure DMA for I2C2 Reception.
5+
*/
6+
7+
#include <stdint.h>
8+
9+
#include "dma.h"
10+
#include "i2c.h"
11+
#include "tcnt.h"
12+
13+
void dma1_ch5_init(void);
14+
void DMA1_Stream5_IRQHandler(void);

‎src/exti.c‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ Purpose: to enable interrupts and handle some IRQs
66

77
#include <stdint.h>
88

9+
#include <FreeRTOS.h>
10+
#include <task.h>
11+
#include <semphr.h>
12+
913
#include "exti.h"
1014
#include "clock.h"
1115
#include "nvic.h"
1216
#include "gpio.h"
1317
#include "lcd.h"
1418

15-
//flag for button press
16-
volatileuint8_tg_button_flag;
19+
//semaphore to synchronize button presses to task actions.
20+
externSemaphoreHandle_tp_button_binary_semaphore;
1721

1822
//enable interrupts
1923
void exti_init(void) {
@@ -29,7 +33,11 @@ void exti_disable(void) {
2933

3034
//IRQ handler for button push interrupt
3135
void EXTI15_10_IRQHandler(void) {
32-
g_button_flag = 1;
36+
BaseType_t xHigherPriorityTaskWoken = pdFALSE; //init a var to hold whether a higher priority task is being blocked by the button binary semaphore
37+
xSemaphoreGiveFromISR(p_button_binary_semaphore, &xHigherPriorityTaskWoken); //give button_binary_semaphore away
38+
3339
EXTI_PR1 |= (1 << 13); //clear pending interrupts on this line
40+
41+
portYIELD_FROM_ISR(xHigherPriorityTaskWoken); //force context switch to higher priority blocked task (if there is one)
3442
}
3543

‎src/i2c.c‎

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ Purpose: configure I2C2 and implement read and write functions
1414
#include "nvic.h"
1515
#include "tcnt.h"
1616

17-
volatile uint8_t g_i2c2_stage = I2C2_POST_DISPLAY;
18-
extern volatile uint32_t g_read_buffer;
19-
2017
//initialize I2C2 registers: 100KHz SCL Frequency, 7-bit addressing mode
2118
void i2c2_init(void) {
2219
gpio_i2c2_init(); //set up gpio for i2c2
2320

2421
I2C2_CR1 &= ~1; //disable I2C2 Peripheral
22+
I2C2_CR1 |= (1 << 15); //enable receive DMA
2523
I2C2_CR1 |= (1 << 2); //enable receive interrupt
2624

2725
I2C2_TIMINGR |= I2C2_TIMING_VALS;
@@ -31,7 +29,7 @@ void i2c2_init(void) {
3129
}
3230

3331
//write to the target; this function only allows up to 2 bytes of data transmission at once
34-
void i2c2_write(const uint8_t NBYTES, const uint16_t w_buffer) {
32+
uint8_t i2c2_write(const uint8_t NBYTES, const uint16_t w_buffer) {
3533
I2C2_CR2 &= ~(1 << 10); //set to Write
3634
I2C2_CR2 &= ~(0xFF << 16); //clear NBYTES; if this is not done then sometimes no stop condition
3735
I2C2_CR2 |= (NBYTES << 16); //NBYTES = NBYTES parameter
@@ -57,11 +55,11 @@ void i2c2_write(const uint8_t NBYTES, const uint16_t w_buffer) {
5755
I2C2_CR2 |= (1 << 14); //send STOP condition
5856
I2C2_ICR |= (1 << 5); //clear stop flag
5957

60-
g_i2c2_stage= I2C2_POST_WRITE;
58+
return I2C2_POST_WRITE;
6159
}
6260

6361
//read from the i2c bus
64-
void i2c2_read(const uint8_t NBYTES) {
62+
uint8_t i2c2_read(const uint8_t NBYTES) {
6563
I2C2_CR2 |= (1 << 10); //set to Read
6664
I2C2_CR2 &= ~(0xFF << 16); //clear NBYTES; if this is not done then sometimes no stop condition
6765
I2C2_CR2 |= (NBYTES << 16); //NBYTES = 4 for the TSL2591 data registers
@@ -73,8 +71,7 @@ void i2c2_read(const uint8_t NBYTES) {
7371

7472
I2C2_CR2 |= (1 << 13); //send start condition
7573

76-
g_read_buffer = 0;
77-
g_i2c2_stage = I2C2_POST_READ;
74+
return I2C2_POST_READ;
7875
}
7976

8077
//Use timers to check if the bus goes idle; returns 1 if idle and 0 if not idle
@@ -97,36 +94,16 @@ uint8_t i2c2_check_bus(void) {
9794
}
9895
}
9996

97+
//PROBABLY DON'T NEED THIS!!!
10098
//IRQ handler for I2C2 event interrupts - for receives!
10199
void I2C2_EV_IRQHandler(void) {
102-
nvic_disable();
103-
104-
static uint8_t bytes_rx; //holds number of bytes received
105-
106-
//if this is the first received byte, init a count var to 1
107-
if (g_i2c2_stage == I2C2_POST_READ) {
108-
///* BEFORE DMA
109-
bytes_rx = 0;
110-
g_read_buffer |= ((uint8_t) I2C2_RXDR);
111-
//*/
112-
113-
bytes_rx = 1;
114-
g_i2c2_stage = I2C2_RECEIVING;
115-
}
116-
else {
117-
///* BEFORE DMA
118-
g_read_buffer |= ((uint8_t) I2C2_RXDR) << (bytes_rx*8);
119-
//*/
120-
121-
bytes_rx++;
122-
}
100+
static uint8_t bytes_rx = 0; //holds number of bytes received
101+
bytes_rx++;
123102

124103
if (bytes_rx == I2C2_NBYTES) {
125104
I2C2_CR2 |= (1 << 14); //send STOP condition
126105
I2C2_ICR |= (1 << 5); //clear stop flag
127106
bytes_rx = 0;
128-
g_i2c2_stage = I2C2_POST_RECEIVE;
129107
}
130-
nvic_enable();
131108
}
132109

‎src/lcd.c‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,23 +210,26 @@ void lcd_clear(void){
210210
}
211211

212212
void lcd_output_text(lcd_text_buffer_t const buf){
213-
char c;
213+
214+
lcd_command();
215+
spi2_write(LCD_SET_Y_ADDRESS(0));
216+
spi2_write(LCD_SET_X_ADDRESS(0));
217+
218+
const uint8_t* ascii_array;
214219
for (uint8_t y = 0; y < LCD_Y_COUNT; y++) { //loop for every byte row
215220
lcd_command();
216221
spi2_write(LCD_SET_Y_ADDRESS(y)); //set y address to equal the index of the array of the current string
217222

218-
//for (uint8_t x = 0; (buf[y][x] != '0円'); x++){ //loop for every bit column until null terminator is hit
219-
for (uint8_t x = 0; x < 5; x++){ //loop for every bit column until null terminator is hit
220-
221-
c = buf[y][x];
222-
const uint8_t* ascii_array = ascii_to_bitmap(c); //convert each char to an array of bytes representing pixels
223+
for (uint8_t x = 0; (buf[y][x] != '0円'); x++){ //loop for every bit column until null terminator is hit
224+
ascii_array = ascii_to_bitmap(buf[y][x]); //convert each char to an array of bytes representing pixels
223225

224226
for (uint8_t column = 0; column < 6; column++){
225227
if (column < BITMAP_WIDTH) {
226228
lcd_data();
227229
spi2_write(ascii_array[column]);
228230
}
229231
else { //this section adds a space between each character
232+
lcd_data();
230233
spi2_write(0x00);
231234
}
232235
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /